]>
code.delx.au - spectrwm/blob - scrotwm.c
3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Much code and ideas taken from dwm under the following license:
20 * MIT/X Consortium License
22 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
23 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
24 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
25 * 2007 Premysl Hruby <dfenze at gmail dot com>
26 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
27 * 2007 Christof Musik <christof at sendfax dot de>
28 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
29 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
30 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, including without limitation
35 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 * and/or sell copies of the Software, and to permit persons to whom the
37 * Software is furnished to do so, subject to the following conditions:
39 * The above copyright notice and this permission notice shall be included in
40 * all copies or substantial portions of the Software.
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
51 #define SWM_VERSION "0.5"
64 #include <sys/types.h>
67 #include <sys/queue.h>
68 #include <sys/param.h>
70 #include <X11/cursorfont.h>
71 #include <X11/keysym.h>
72 #include <X11/Xatom.h>
74 #include <X11/Xproto.h>
75 #include <X11/Xutil.h>
77 /* #define SWM_DEBUG */
79 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
80 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
81 #define SWM_D_MISC 0x0001
82 #define SWM_D_EVENT 0x0002
83 #define SWM_D_WS 0x0004
84 #define SWM_D_FOCUS 0x0008
85 #define SWM_D_MOVE 0x0010
87 u_int32_t swm_debug
= 0
96 #define DNPRINTF(n,x...)
99 #define LENGTH(x) (sizeof x / sizeof x[0])
100 #define MODKEY Mod1Mask
101 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
103 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
108 int ignore_enter
= 0;
109 unsigned int numlockmask
= 0;
110 unsigned long color_focus
= 0xff0000; /* XXX should this be per ws? */
111 unsigned long color_unfocus
= 0x888888;
118 unsigned long bar_border
= 0x008080;
119 unsigned long bar_color
= 0x000000;
120 unsigned long bar_font_color
= 0xa0a0a0;
126 char *bar_fonts
[] = {
127 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
128 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
132 /* terminal + args */
133 char *spawn_term
[] = { "xterm", NULL
};
134 char *spawn_menu
[] = { "dmenu_run", NULL
};
135 char *spawn_scrotwm
[] = { "scrotwm", NULL
};
137 /* layout manager data */
139 void vertical_init(void);
140 void vertical_stack(void);
141 void horizontal_init(void);
142 void horizontal_stack(void);
144 void max_stack(void);
147 void (*l_init
)(void); /* init/reset */
148 void (*l_stack
)(void); /* restack windows */
151 { vertical_init
, vertical_stack
},
152 { horizontal_init
, horizontal_stack
},
153 /* XXX not working yet
154 * { max_init, max_stack},
160 TAILQ_ENTRY(ws_win
) entry
;
168 XWindowAttributes wa
;
171 TAILQ_HEAD(ws_win_list
, ws_win
);
173 /* define work spaces */
174 #define SWM_WS_MAX (10)
176 int visible
; /* workspace visible */
177 int restack
; /* restack on switch */
178 struct layout
*cur_layout
; /* current layout handlers */
179 struct ws_win
*focus
; /* which win has focus */
180 struct ws_win_list winlist
; /* list of windows in ws */
184 /* args to functions */
187 #define SWM_ARG_ID_FOCUSNEXT (0)
188 #define SWM_ARG_ID_FOCUSPREV (1)
189 #define SWM_ARG_ID_FOCUSMAIN (2)
190 #define SWM_ARG_ID_SWAPNEXT (3)
191 #define SWM_ARG_ID_SWAPPREV (4)
192 #define SWM_ARG_ID_SWAPMAIN (5)
196 /* conf file stuff */
197 #define SWM_CONF_WS "\n= \t"
198 #define SWM_CONF_FILE "scrotwm.conf"
200 conf_load(char *filename
)
203 char *line
, *cp
, *var
, *val
;
204 size_t len
, lineno
= 0;
206 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
208 if (filename
== NULL
)
211 if ((config
= fopen(filename
, "r")) == NULL
)
215 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
219 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
225 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
227 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
228 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
231 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
234 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
235 bar_enabled
= atoi(val
);
236 else if (!strncmp(var
, "bar_border",
237 strlen("bar_border")))
238 bar_border
= strtol(val
, NULL
, 16);
239 else if (!strncmp(var
, "bar_color",
240 strlen("bar_color")))
241 bar_color
= strtol(val
, NULL
, 16);
242 else if (!strncmp(var
, "bar_font_color",
243 strlen("bar_font_color")))
244 bar_font_color
= strtol(val
, NULL
, 16);
245 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
246 asprintf(&bar_fonts
[0], "%s", val
);
252 if (!strncmp(var
, "color_focus", strlen("color_focus")))
253 color_focus
= strtol(val
, NULL
, 16);
254 else if (!strncmp(var
, "color_unfocus",
255 strlen("color_unfocus")))
256 color_unfocus
= strtol(val
, NULL
, 16);
262 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
263 asprintf(&spawn_term
[0], "%s", val
); /* XXX args? */
274 errx(1, "invalid conf file entry: %s=%s", var
, val
);
283 if (bar_enabled
== 0)
287 XSetForeground(display
, bar_gc
, bar_color
);
288 XDrawString(display
, bar_window
, bar_gc
, 4, bar_fs
->ascent
, bar_text
,
293 localtime_r(&tmt
, &tm
);
294 strftime(bar_text
, sizeof bar_text
, "%a %b %d %R %Z %Y", &tm
);
295 XSetForeground(display
, bar_gc
, bar_font_color
);
296 XDrawString(display
, bar_window
, bar_gc
, 4, bar_fs
->ascent
, bar_text
,
298 XSync(display
, False
);
306 /* XXX yeah yeah byte me */
311 bar_toggle(union arg
*args
)
315 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
319 height
+= bar_height
; /* correct screen height */
320 XUnmapWindow(display
, bar_window
);
323 height
-= bar_height
; /* correct screen height */
324 XMapWindow(display
, bar_window
);
326 XSync(display
, False
);
327 for (i
= 0; i
< SWM_WS_MAX
; i
++)
331 bar_print(); /* must be after stack */
339 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
340 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
344 if (bar_fonts
[i
] == NULL
)
345 errx(1, "couldn't load font");
346 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
348 bar_window
= XCreateSimpleWindow(display
, root
, 0, 0, width
,
349 bar_height
- 2, 1, bar_border
, bar_color
);
350 bar_gc
= XCreateGC(display
, bar_window
, 0, &bar_gcv
);
351 XSetFont(display
, bar_gc
, bar_fs
->fid
);
352 XSelectInput(display
, bar_window
, VisibilityChangeMask
);
354 height
-= bar_height
; /* correct screen height */
355 XMapWindow(display
, bar_window
);
357 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %d\n", (int)bar_window
);
359 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
360 err(1, "could not install bar_signal");
365 count_win(int wsid
, int count_transient
)
370 TAILQ_FOREACH (win
, &ws
[wsid
].winlist
, entry
) {
371 if (count_transient
== 0 && win
->transient
)
375 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
380 quit(union arg
*args
)
382 DNPRINTF(SWM_D_MISC
, "quit\n");
388 restart(union arg
*args
)
390 XCloseDisplay(display
);
391 execvp(args
->argv
[0], args
->argv
);
392 fprintf(stderr
, "execvp failed\n");
398 spawn(union arg
*args
)
400 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
402 * The double-fork construct avoids zombie processes and keeps the code
403 * clean from stupid signal handlers.
408 close(ConnectionNumber(display
));
410 execvp(args
->argv
[0], args
->argv
);
411 fprintf(stderr
, "execvp failed\n");
420 focus_win(struct ws_win
*win
)
422 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
->id
);
423 XSetWindowBorder(display
, win
->id
, color_focus
);
424 XSetInputFocus(display
, win
->id
, RevertToPointerRoot
, CurrentTime
);
425 ws
[current_ws
].focus
= win
;
429 unfocus_win(struct ws_win
*win
)
431 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: id: %lu\n", win
->id
);
432 XSetWindowBorder(display
, win
->id
, color_unfocus
);
433 if (ws
[current_ws
].focus
== win
)
434 ws
[current_ws
].focus
= NULL
;
438 switchws(union arg
*args
)
443 DNPRINTF(SWM_D_WS
, "switchws: %d\n", wsid
+ 1);
445 if (wsid
== current_ws
)
448 /* map new window first to prevent ugly blinking */
449 TAILQ_FOREACH (win
, &ws
[wsid
].winlist
, entry
)
450 XMapRaised(display
, win
->id
);
451 ws
[wsid
].visible
= 1;
453 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
)
454 XUnmapWindow(display
, win
->id
);
455 ws
[current_ws
].visible
= 0;
460 if (ws
[wsid
].restack
) {
464 if (ws
[wsid
].focus
!= NULL
)
465 focus_win(ws
[wsid
].focus
);
466 XSync(display
, False
);
471 swapwin(union arg
*args
)
473 struct ws_win
*target
;
475 DNPRINTF(SWM_D_MOVE
, "swapwin: id %d\n", args
->id
);
476 if (ws
[current_ws
].focus
== NULL
)
480 case SWM_ARG_ID_SWAPPREV
:
481 target
= TAILQ_PREV(ws
[current_ws
].focus
, ws_win_list
, entry
);
482 TAILQ_REMOVE(&ws
[current_ws
].winlist
,
483 ws
[current_ws
].focus
, entry
);
485 TAILQ_INSERT_TAIL(&ws
[current_ws
].winlist
,
486 ws
[current_ws
].focus
, entry
);
488 TAILQ_INSERT_BEFORE(target
, ws
[current_ws
].focus
,
491 case SWM_ARG_ID_SWAPNEXT
:
492 target
= TAILQ_NEXT(ws
[current_ws
].focus
, entry
);
493 TAILQ_REMOVE(&ws
[current_ws
].winlist
,
494 ws
[current_ws
].focus
, entry
);
496 TAILQ_INSERT_HEAD(&ws
[current_ws
].winlist
,
497 ws
[current_ws
].focus
, entry
);
499 TAILQ_INSERT_AFTER(&ws
[current_ws
].winlist
, target
,
500 ws
[current_ws
].focus
, entry
);
502 case SWM_ARG_ID_SWAPMAIN
:
503 target
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
504 if (target
== ws
[current_ws
].focus
)
506 TAILQ_REMOVE(&ws
[current_ws
].winlist
, target
, entry
);
507 TAILQ_INSERT_BEFORE(ws
[current_ws
].focus
, target
, entry
);
508 TAILQ_REMOVE(&ws
[current_ws
].winlist
,
509 ws
[current_ws
].focus
, entry
);
510 TAILQ_INSERT_HEAD(&ws
[current_ws
].winlist
,
511 ws
[current_ws
].focus
, entry
);
514 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
523 focus(union arg
*args
)
525 struct ws_win
*winfocus
, *winlostfocus
;
527 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
528 if (ws
[current_ws
].focus
== NULL
)
531 winlostfocus
= ws
[current_ws
].focus
;
534 case SWM_ARG_ID_FOCUSPREV
:
535 winfocus
= TAILQ_PREV(ws
[current_ws
].focus
, ws_win_list
, entry
);
536 if (winfocus
== NULL
)
538 TAILQ_LAST(&ws
[current_ws
].winlist
, ws_win_list
);
541 case SWM_ARG_ID_FOCUSNEXT
:
542 winfocus
= TAILQ_NEXT(ws
[current_ws
].focus
, entry
);
543 if (winfocus
== NULL
)
544 winfocus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
547 case SWM_ARG_ID_FOCUSMAIN
:
548 winfocus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
555 if (winfocus
== winlostfocus
)
558 unfocus_win(winlostfocus
);
560 XSync(display
, False
);
564 cycle_layout(union arg
*args
)
566 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", current_ws
);
568 ws
[current_ws
].cur_layout
++;
569 if (ws
[current_ws
].cur_layout
->l_stack
== NULL
)
570 ws
[current_ws
].cur_layout
= &layouts
[0];
576 DNPRINTF(SWM_D_EVENT
, "stack: workspace: %d\n", current_ws
);
578 ws
[current_ws
].restack
= 0;
579 ws
[current_ws
].cur_layout
->l_stack();
580 XSync(display
, False
);
586 DNPRINTF(SWM_D_EVENT
, "vertical_init: workspace: %d\n", current_ws
);
589 /* I know this sucks but it works well enough */
591 vertical_stack(void) {
593 struct ws_win wf
, *win
, *winfocus
= &wf
;
594 int i
, h
, w
, x
, y
, hrh
, winno
;
598 DNPRINTF(SWM_D_EVENT
, "vertical_stack: workspace: %d\n", current_ws
);
602 winno
= count_win(current_ws
, 0);
612 hrh
= height
/ (winno
- 1);
617 y
= bar_enabled
? bar_height
: 0;
620 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
625 if (i
!= 0 && hrh
!= 0) {
626 /* correct the last window for lost pixels */
627 if (win
== TAILQ_LAST(&ws
[current_ws
].winlist
,
629 h
= height
- (i
* hrh
);
637 /* leave first right hand window at y = 0 */
643 if (win
->transient
!= 0 || win
->floating
!= 0)
648 bzero(&wc
, sizeof wc
);
653 win
->width
= wc
.width
= w
;
654 win
->height
= wc
.height
= h
;
655 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
657 /* make sure we don't clobber the screen */
658 if (win
->wa
.width
> width
)
659 win
->wa
.width
= width
;
660 if (win
->wa
.height
> height
)
661 win
->wa
.width
= height
;
662 win
->x
= wc
.x
= (width
- win
->wa
.width
) / 2;
663 win
->y
= wc
.y
= (height
- win
->wa
.height
) / 2;
664 mask
= CWX
| CWY
| CWBorderWidth
;
666 XConfigureWindow(display
, win
->id
, mask
, &wc
);
668 if (win
== ws
[current_ws
].focus
)
672 XMapRaised(display
, win
->id
);
676 focus_win(winfocus
); /* this has to be done outside of the loop */
680 horizontal_init(void)
682 DNPRINTF(SWM_D_EVENT
, "horizontal_init: workspace: %d\n", current_ws
);
686 horizontal_stack(void) {
688 struct ws_win wf
, *win
, *winfocus
= &wf
;
689 int i
, h
, w
, x
, y
, hrw
, winno
;
693 DNPRINTF(SWM_D_EVENT
, "horizontal_stack: workspace: %d\n", current_ws
);
697 winno
= count_win(current_ws
, 0);
707 hrw
= width
/ (winno
- 1);
712 y
= bar_enabled
? bar_height
: 0;
715 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
718 h
-= (2 - height
% 2);
720 if (i
!= 0 && hrw
!= 0) {
721 /* correct the last window for lost pixels */
722 if (win
== TAILQ_LAST(&ws
[current_ws
].winlist
,
724 w
= width
- (i
* hrw
);
732 /* leave first bottom window at x = 0 */
738 if (win
->transient
!= 0 || win
->floating
!= 0)
743 bzero(&wc
, sizeof wc
);
748 win
->width
= wc
.width
= w
;
749 win
->height
= wc
.height
= h
;
750 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
752 /* make sure we don't clobber the screen */
753 if (win
->wa
.width
> width
)
754 win
->wa
.width
= width
;
755 if (win
->wa
.height
> height
)
756 win
->wa
.width
= height
;
757 win
->x
= wc
.x
= (width
- win
->wa
.width
) / 2;
758 win
->y
= wc
.y
= (height
- win
->wa
.height
) / 2;
759 mask
= CWX
| CWY
| CWBorderWidth
;
761 XConfigureWindow(display
, win
->id
, mask
, &wc
);
763 if (win
== ws
[current_ws
].focus
)
767 XMapRaised(display
, win
->id
);
771 focus_win(winfocus
); /* this has to be done outside of the loop */
774 /* fullscreen view */
778 DNPRINTF(SWM_D_EVENT
, "max_init: workspace: %d\n", current_ws
);
784 struct ws_win wf
, *win
, *winfocus
= &wf
;
785 int i
, h
, w
, x
, y
, winno
;
788 DNPRINTF(SWM_D_EVENT
, "max_stack: workspace: %d\n", current_ws
);
792 winno
= count_win(current_ws
, 0);
797 y
= bar_enabled
? bar_height
: 0;
798 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
799 if (i
== 1 && win
->transient
== 0 && win
->floating
!= 0) {
805 bzero(&wc
, sizeof wc
);
809 win
->width
= wc
.width
= w
;
810 win
->height
= wc
.height
= h
;
811 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
812 XConfigureWindow(display
, win
->id
, mask
, &wc
);
814 XMapRaised(display
, win
->id
);
816 /* hide all but the master window */
817 XUnmapWindow(display
, win
->id
);
822 focus_win(winfocus
); /* this has to be done outside of the loop */
826 send_to_ws(union arg
*args
)
829 struct ws_win
*win
= ws
[current_ws
].focus
;
831 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
833 XUnmapWindow(display
, win
->id
);
835 /* find a window to focus */
836 ws
[current_ws
].focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
837 if (ws
[current_ws
].focus
== NULL
)
838 ws
[current_ws
].focus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
839 if (ws
[current_ws
].focus
== win
)
840 ws
[current_ws
].focus
= NULL
;
842 TAILQ_REMOVE(&ws
[current_ws
].winlist
, win
, entry
);
844 TAILQ_INSERT_TAIL(&ws
[wsid
].winlist
, win
, entry
);
845 if (count_win(wsid
, 1) == 0)
846 ws
[wsid
].focus
= win
;
847 ws
[wsid
].restack
= 1;
852 /* key definitions */
856 void (*func
)(union arg
*);
859 /* modifier key function argument */
860 /* XXX alt-c is temporary, until I figure out how to grab spacebar */
861 { MODKEY
, XK_c
, cycle_layout
, {0} },
862 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
863 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
864 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
865 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
866 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
867 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
868 { MODKEY
, XK_p
, spawn
, {.argv
= spawn_menu
} },
869 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
870 { MODKEY
, XK_q
, restart
, {.argv
= spawn_scrotwm
} },
871 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
872 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
873 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
874 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
875 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
876 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
877 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
878 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
879 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
880 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
881 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
882 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
883 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
884 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
885 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
886 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
887 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
888 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
889 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
890 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
891 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
892 { MODKEY
, XK_b
, bar_toggle
, {0} },
893 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
894 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
898 updatenumlockmask(void)
901 XModifierKeymap
*modmap
;
903 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
905 modmap
= XGetModifierMapping(display
);
906 for (i
= 0; i
< 8; i
++)
907 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
908 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
909 == XKeysymToKeycode(display
, XK_Num_Lock
))
910 numlockmask
= (1 << i
);
912 XFreeModifiermap(modmap
);
920 unsigned int modifiers
[] =
921 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
923 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
926 XUngrabKey(display
, AnyKey
, AnyModifier
, root
);
927 for(i
= 0; i
< LENGTH(keys
); i
++) {
928 if((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
929 for(j
= 0; j
< LENGTH(modifiers
); j
++)
930 XGrabKey(display
, code
,
931 keys
[i
].mod
| modifiers
[j
], root
,
932 True
, GrabModeAsync
, GrabModeAsync
);
938 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
946 XKeyEvent
*ev
= &e
->xkey
;
948 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
950 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
951 for(i
= 0; i
< LENGTH(keys
); i
++)
952 if(keysym
== keys
[i
].keysym
953 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
955 keys
[i
].func(&(keys
[i
].args
));
959 buttonpress(XEvent
*e
)
961 XButtonPressedEvent
*ev
= &e
->xbutton
;
962 #ifdef SWM_CLICKTOFOCUS
967 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
969 if (ev
->window
== root
)
971 if (ev
->window
== ws
[current_ws
].focus
->id
)
973 #ifdef SWM_CLICKTOFOCUS
974 TAILQ_FOREACH(win
, &ws
[current_ws
].winlist
, entry
)
975 if (win
->id
== ev
->window
) {
976 /* focus in the clicked window */
977 XSetWindowBorder(display
, ev
->window
, 0xff0000);
978 XSetWindowBorder(display
,
979 ws
[current_ws
].focus
->id
, 0x888888);
980 XSetInputFocus(display
, ev
->window
, RevertToPointerRoot
,
982 ws
[current_ws
].focus
= win
;
983 XSync(display
, False
);
990 manage_window(Window id
)
994 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
996 return (win
); /* already being managed */
999 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
1000 errx(1, "calloc: failed to allocate memory for new window");
1003 TAILQ_INSERT_TAIL(&ws
[current_ws
].winlist
, win
, entry
);
1005 XSelectInput(display
, id
, ButtonPressMask
| EnterWindowMask
|
1006 FocusChangeMask
| ExposureMask
);
1013 configurerequest(XEvent
*e
)
1015 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
1019 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: %lu\n", ev
->window
);
1022 win
= manage_window(ev
->window
);
1023 ws
[current_ws
].focus
= win
; /* make new win focused */
1025 XGetTransientForHint(display
, win
->id
, &trans
);
1027 win
->transient
= trans
;
1028 DNPRINTF(SWM_D_MISC
, "configurerequest: win %u transient %u\n",
1029 (unsigned)win
->id
, win
->transient
);
1031 XGetWindowAttributes(display
, win
->id
, &win
->wa
);
1033 XClassHint ch
= { 0 };
1034 if(XGetClassHint(display
, win
->id
, &ch
)) {
1035 fprintf(stderr
, "class: %s name: %s\n", ch
.res_class
, ch
.res_name
);
1036 if (!strcmp(ch
.res_class
, "Gvim") && !strcmp(ch
.res_name
, "gvim")) {
1040 XFree(ch
.res_class
);
1049 configurenotify(XEvent
*e
)
1051 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
1052 e
->xconfigure
.window
);
1056 destroynotify(XEvent
*e
)
1059 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
1061 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
1063 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
1064 if (ev
->window
== win
->id
) {
1065 /* find a window to focus */
1066 ws
[current_ws
].focus
= TAILQ_PREV(win
,
1067 ws_win_list
, entry
);
1068 if (ws
[current_ws
].focus
== NULL
)
1069 ws
[current_ws
].focus
=
1070 TAILQ_FIRST(&ws
[current_ws
].winlist
);
1071 if (win
== ws
[current_ws
].focus
)
1072 ws
[current_ws
].focus
= NULL
;
1074 TAILQ_REMOVE(&ws
[current_ws
].winlist
, win
, entry
);
1084 enternotify(XEvent
*e
)
1086 XCrossingEvent
*ev
= &e
->xcrossing
;
1089 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
1091 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) &&
1095 /* eat event(s) to prevent autofocus */
1099 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
1100 if (win
->id
== ev
->window
)
1110 XFocusChangeEvent
*ev
= &e
->xfocus
;
1112 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", ev
->window
);
1114 XSync(display
, False
); /* not sure this helps redrawing graphic apps */
1116 if (ev
->window
== root
)
1119 * kill grab for now so that we can cut and paste , this screws up
1123 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1124 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1125 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1130 mappingnotify(XEvent
*e
)
1132 XMappingEvent
*ev
= &e
->xmapping
;
1134 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
1136 XRefreshKeyboardMapping(ev
);
1137 if(ev
->request
== MappingKeyboard
)
1142 maprequest(XEvent
*e
)
1144 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
1145 e
->xmaprequest
.window
);
1147 manage_window(e
->xmaprequest
.window
);
1152 propertynotify(XEvent
*e
)
1154 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
1155 e
->xproperty
.window
);
1159 unmapnotify(XEvent
*e
)
1161 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
1165 visibilitynotify(XEvent
*e
)
1167 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n", e
->xvisibility
.window
);
1169 if (e
->xvisibility
.window
== bar_window
&&
1170 e
->xvisibility
.state
== VisibilityUnobscured
)
1174 void (*handler
[LASTEvent
])(XEvent
*) = {
1176 [KeyPress
] = keypress
,
1177 [ButtonPress
] = buttonpress
,
1178 [ConfigureRequest
] = configurerequest
,
1179 [ConfigureNotify
] = configurenotify
,
1180 [DestroyNotify
] = destroynotify
,
1181 [EnterNotify
] = enternotify
,
1182 [FocusIn
] = focusin
,
1183 [MappingNotify
] = mappingnotify
,
1184 [MapRequest
] = maprequest
,
1185 [PropertyNotify
] = propertynotify
,
1186 [UnmapNotify
] = unmapnotify
,
1187 [VisibilityNotify
] = visibilitynotify
,
1191 xerror_start(Display
*d
, XErrorEvent
*ee
)
1198 xerror(Display
*d
, XErrorEvent
*ee
)
1200 fprintf(stderr
, "error: %p %p\n", display
, ee
);
1209 xerrorxlib
= XSetErrorHandler(xerror_start
);
1211 /* this causes an error if some other window manager is running */
1212 XSelectInput(display
, DefaultRootWindow(display
),
1213 SubstructureRedirectMask
);
1214 XSync(display
, False
);
1218 XSetErrorHandler(xerror
);
1219 XSync(display
, False
);
1224 main(int argc
, char *argv
[])
1227 char conf
[PATH_MAX
], *cfile
= NULL
;
1230 unsigned int i
, num
;
1231 Window d1
, d2
, *wins
= NULL
;
1232 XWindowAttributes wa
;
1234 fprintf(stderr
, "Welcome to scrotwm V%s\n", SWM_VERSION
);
1235 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1236 warnx("no locale support");
1238 if(!(display
= XOpenDisplay(0)))
1239 errx(1, "can not open display");
1242 errx(1, "other wm running");
1244 screen
= DefaultScreen(display
);
1245 root
= RootWindow(display
, screen
);
1246 width
= DisplayWidth(display
, screen
) - 2;
1247 height
= DisplayHeight(display
, screen
) - 2;
1249 /* look for local and global conf file */
1250 pwd
= getpwuid(getuid());
1252 errx(1, "invalid user %d", getuid());
1254 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
1255 if (stat(conf
, &sb
) != -1) {
1256 if (S_ISREG(sb
.st_mode
))
1259 /* try global conf file */
1260 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
1261 if (!stat(conf
, &sb
))
1262 if (S_ISREG(sb
.st_mode
))
1268 /* init all workspaces */
1269 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
1273 TAILQ_INIT(&ws
[i
].winlist
);
1274 ws
[i
].cur_layout
= &layouts
[0];
1276 /* make work space 1 active */
1279 /* grab existing windows */
1280 if (XQueryTree(display
, root
, &d1
, &d2
, &wins
, &num
)) {
1281 for (i
= 0; i
< num
; i
++) {
1282 if (!XGetWindowAttributes(display
, wins
[i
], &wa
)
1283 || wa
.override_redirect
||
1284 XGetTransientForHint(display
, wins
[i
], &d1
))
1286 manage_window(wins
[i
]);
1291 ws
[0].focus
= TAILQ_FIRST(&ws
[0].winlist
);
1293 /* setup status bar */
1296 XSelectInput(display
, root
, SubstructureRedirectMask
|
1297 SubstructureNotifyMask
| ButtonPressMask
| KeyPressMask
|
1298 EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
|
1299 FocusChangeMask
| PropertyChangeMask
| ExposureMask
);
1305 XNextEvent(display
, &e
);
1306 if (handler
[e
.type
])
1307 handler
[e
.type
](&e
);
1310 XCloseDisplay(display
);