]>
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
;
172 TAILQ_HEAD(ws_win_list
, ws_win
);
174 /* define work spaces */
175 #define SWM_WS_MAX (10)
177 int visible
; /* workspace visible */
178 int restack
; /* restack on switch */
179 struct layout
*cur_layout
; /* current layout handlers */
180 struct ws_win
*focus
; /* which win has focus */
181 struct ws_win_list winlist
; /* list of windows in ws */
185 /* args to functions */
188 #define SWM_ARG_ID_FOCUSNEXT (0)
189 #define SWM_ARG_ID_FOCUSPREV (1)
190 #define SWM_ARG_ID_FOCUSMAIN (2)
191 #define SWM_ARG_ID_SWAPNEXT (3)
192 #define SWM_ARG_ID_SWAPPREV (4)
193 #define SWM_ARG_ID_SWAPMAIN (5)
197 /* conf file stuff */
198 #define SWM_CONF_WS "\n= \t"
199 #define SWM_CONF_FILE "scrotwm.conf"
201 conf_load(char *filename
)
204 char *line
, *cp
, *var
, *val
;
205 size_t len
, lineno
= 0;
207 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
209 if (filename
== NULL
)
212 if ((config
= fopen(filename
, "r")) == NULL
)
216 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
220 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
226 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
228 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
229 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
232 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
235 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
236 bar_enabled
= atoi(val
);
237 else if (!strncmp(var
, "bar_border",
238 strlen("bar_border")))
239 bar_border
= strtol(val
, NULL
, 16);
240 else if (!strncmp(var
, "bar_color",
241 strlen("bar_color")))
242 bar_color
= strtol(val
, NULL
, 16);
243 else if (!strncmp(var
, "bar_font_color",
244 strlen("bar_font_color")))
245 bar_font_color
= strtol(val
, NULL
, 16);
246 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
247 asprintf(&bar_fonts
[0], "%s", val
);
253 if (!strncmp(var
, "color_focus", strlen("color_focus")))
254 color_focus
= strtol(val
, NULL
, 16);
255 else if (!strncmp(var
, "color_unfocus",
256 strlen("color_unfocus")))
257 color_unfocus
= strtol(val
, NULL
, 16);
263 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
264 asprintf(&spawn_term
[0], "%s", val
); /* XXX args? */
275 errx(1, "invalid conf file entry: %s=%s", var
, val
);
284 if (bar_enabled
== 0)
288 XSetForeground(display
, bar_gc
, bar_color
);
289 XDrawString(display
, bar_window
, bar_gc
, 4, bar_fs
->ascent
, bar_text
,
294 localtime_r(&tmt
, &tm
);
295 strftime(bar_text
, sizeof bar_text
, "%a %b %d %R %Z %Y", &tm
);
296 XSetForeground(display
, bar_gc
, bar_font_color
);
297 XDrawString(display
, bar_window
, bar_gc
, 4, bar_fs
->ascent
, bar_text
,
299 XSync(display
, False
);
307 /* XXX yeah yeah byte me */
312 bar_toggle(union arg
*args
)
316 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
320 height
+= bar_height
; /* correct screen height */
321 XUnmapWindow(display
, bar_window
);
324 height
-= bar_height
; /* correct screen height */
325 XMapWindow(display
, bar_window
);
327 XSync(display
, False
);
328 for (i
= 0; i
< SWM_WS_MAX
; i
++)
332 bar_print(); /* must be after stack */
340 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
341 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
345 if (bar_fonts
[i
] == NULL
)
346 errx(1, "couldn't load font");
347 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
349 bar_window
= XCreateSimpleWindow(display
, root
, 0, 0, width
,
350 bar_height
- 2, 1, bar_border
, bar_color
);
351 bar_gc
= XCreateGC(display
, bar_window
, 0, &bar_gcv
);
352 XSetFont(display
, bar_gc
, bar_fs
->fid
);
353 XSelectInput(display
, bar_window
, VisibilityChangeMask
);
355 height
-= bar_height
; /* correct screen height */
356 XMapWindow(display
, bar_window
);
358 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %d\n", (int)bar_window
);
360 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
361 err(1, "could not install bar_signal");
366 count_win(int wsid
, int count_transient
)
371 TAILQ_FOREACH (win
, &ws
[wsid
].winlist
, entry
) {
372 if (count_transient
== 0 && win
->transient
)
376 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
381 quit(union arg
*args
)
383 DNPRINTF(SWM_D_MISC
, "quit\n");
389 restart(union arg
*args
)
391 XCloseDisplay(display
);
392 execvp(args
->argv
[0], args
->argv
);
393 fprintf(stderr
, "execvp failed\n");
399 spawn(union arg
*args
)
401 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
403 * The double-fork construct avoids zombie processes and keeps the code
404 * clean from stupid signal handlers.
409 close(ConnectionNumber(display
));
411 execvp(args
->argv
[0], args
->argv
);
412 fprintf(stderr
, "execvp failed\n");
421 focus_win(struct ws_win
*win
)
423 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
->id
);
424 XSetWindowBorder(display
, win
->id
, color_focus
);
425 XSetInputFocus(display
, win
->id
, RevertToPointerRoot
, CurrentTime
);
426 ws
[current_ws
].focus
= win
;
430 unfocus_win(struct ws_win
*win
)
432 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: id: %lu\n", win
->id
);
433 XSetWindowBorder(display
, win
->id
, color_unfocus
);
434 if (ws
[current_ws
].focus
== win
)
435 ws
[current_ws
].focus
= NULL
;
439 switchws(union arg
*args
)
444 DNPRINTF(SWM_D_WS
, "switchws: %d\n", wsid
+ 1);
446 if (wsid
== current_ws
)
449 /* map new window first to prevent ugly blinking */
450 TAILQ_FOREACH (win
, &ws
[wsid
].winlist
, entry
)
451 XMapRaised(display
, win
->id
);
452 ws
[wsid
].visible
= 1;
454 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
)
455 XUnmapWindow(display
, win
->id
);
456 ws
[current_ws
].visible
= 0;
461 if (ws
[wsid
].restack
) {
465 if (ws
[wsid
].focus
!= NULL
)
466 focus_win(ws
[wsid
].focus
);
467 XSync(display
, False
);
472 swapwin(union arg
*args
)
474 struct ws_win
*target
;
476 DNPRINTF(SWM_D_MOVE
, "swapwin: id %d\n", args
->id
);
477 if (ws
[current_ws
].focus
== NULL
)
481 case SWM_ARG_ID_SWAPPREV
:
482 target
= TAILQ_PREV(ws
[current_ws
].focus
, ws_win_list
, entry
);
483 TAILQ_REMOVE(&ws
[current_ws
].winlist
,
484 ws
[current_ws
].focus
, entry
);
486 TAILQ_INSERT_TAIL(&ws
[current_ws
].winlist
,
487 ws
[current_ws
].focus
, entry
);
489 TAILQ_INSERT_BEFORE(target
, ws
[current_ws
].focus
,
492 case SWM_ARG_ID_SWAPNEXT
:
493 target
= TAILQ_NEXT(ws
[current_ws
].focus
, entry
);
494 TAILQ_REMOVE(&ws
[current_ws
].winlist
,
495 ws
[current_ws
].focus
, entry
);
497 TAILQ_INSERT_HEAD(&ws
[current_ws
].winlist
,
498 ws
[current_ws
].focus
, entry
);
500 TAILQ_INSERT_AFTER(&ws
[current_ws
].winlist
, target
,
501 ws
[current_ws
].focus
, entry
);
503 case SWM_ARG_ID_SWAPMAIN
:
504 target
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
505 if (target
== ws
[current_ws
].focus
)
507 TAILQ_REMOVE(&ws
[current_ws
].winlist
, target
, entry
);
508 TAILQ_INSERT_BEFORE(ws
[current_ws
].focus
, target
, entry
);
509 TAILQ_REMOVE(&ws
[current_ws
].winlist
,
510 ws
[current_ws
].focus
, entry
);
511 TAILQ_INSERT_HEAD(&ws
[current_ws
].winlist
,
512 ws
[current_ws
].focus
, entry
);
515 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
524 focus(union arg
*args
)
526 struct ws_win
*winfocus
, *winlostfocus
;
528 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
529 if (ws
[current_ws
].focus
== NULL
)
532 winlostfocus
= ws
[current_ws
].focus
;
535 case SWM_ARG_ID_FOCUSPREV
:
536 winfocus
= TAILQ_PREV(ws
[current_ws
].focus
, ws_win_list
, entry
);
537 if (winfocus
== NULL
)
539 TAILQ_LAST(&ws
[current_ws
].winlist
, ws_win_list
);
542 case SWM_ARG_ID_FOCUSNEXT
:
543 winfocus
= TAILQ_NEXT(ws
[current_ws
].focus
, entry
);
544 if (winfocus
== NULL
)
545 winfocus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
548 case SWM_ARG_ID_FOCUSMAIN
:
549 winfocus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
556 if (winfocus
== winlostfocus
)
559 unfocus_win(winlostfocus
);
561 XSync(display
, False
);
565 cycle_layout(union arg
*args
)
567 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", current_ws
);
569 ws
[current_ws
].cur_layout
++;
570 if (ws
[current_ws
].cur_layout
->l_stack
== NULL
)
571 ws
[current_ws
].cur_layout
= &layouts
[0];
577 DNPRINTF(SWM_D_EVENT
, "stack: workspace: %d\n", current_ws
);
579 ws
[current_ws
].restack
= 0;
580 ws
[current_ws
].cur_layout
->l_stack();
581 XSync(display
, False
);
585 stack_floater(struct ws_win
*win
)
590 bzero(&wc
, sizeof wc
);
592 mask
= CWX
| CWY
| CWBorderWidth
;
594 /* use obsolete width height */
595 if (win
->sh
.flags
& USPosition
) {
596 win
->width
= wc
.width
= win
->sh
.width
;
597 win
->height
= wc
.height
= win
->sh
.height
;
598 mask
|= CWWidth
| CWHeight
;
602 if (win
->sh
.flags
& PMinSize
) {
603 /* some hints are retarded */
604 if (win
->sh
.min_width
< width
/ 10)
605 win
->sh
.min_width
= width
/ 3;
606 if (win
->sh
.min_height
< height
/ 10)
607 win
->wa
.height
= height
/ 3;
609 win
->width
= wc
.width
= win
->sh
.min_width
* 2;
610 win
->height
= wc
.height
= win
->sh
.min_height
* 2;
611 mask
|= CWWidth
| CWHeight
;
613 if (win
->sh
.flags
& PMaxSize
) {
614 /* potentially override min values */
615 if (win
->sh
.max_width
< width
) {
616 win
->width
= wc
.width
= win
->sh
.max_width
;
619 if (win
->sh
.max_height
< height
) {
620 win
->height
= wc
.height
= win
->sh
.max_height
;
625 /* make sure we don't clobber the screen */
626 if ((mask
& CWWidth
) && win
->wa
.width
> width
)
627 win
->wa
.width
= width
- 4;
628 if ((mask
& CWHeight
) && win
->wa
.height
> height
)
629 win
->wa
.height
= height
- 4;
631 /* supposed to be obsolete */
632 if (win
->sh
.flags
& USPosition
) {
633 win
->x
= wc
.x
= win
->sh
.x
;
634 win
->y
= wc
.y
= win
->sh
.y
;
636 win
->x
= wc
.x
= (width
- win
->wa
.width
) / 2;
637 win
->y
= wc
.y
= (height
- win
->wa
.height
) / 2;
639 DNPRINTF(SWM_D_EVENT
, "stack_floater: win %d x %d y %d w %d h %d\n",
640 win
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
642 XConfigureWindow(display
, win
->id
, mask
, &wc
);
648 DNPRINTF(SWM_D_EVENT
, "vertical_init: workspace: %d\n", current_ws
);
651 /* I know this sucks but it works well enough */
653 vertical_stack(void) {
655 struct ws_win wf
, *win
, *winfocus
= &wf
;
656 int i
, h
, w
, x
, y
, hrh
, winno
;
659 DNPRINTF(SWM_D_EVENT
, "vertical_stack: workspace: %d\n", current_ws
);
663 winno
= count_win(current_ws
, 0);
673 hrh
= height
/ (winno
- 1);
678 y
= bar_enabled
? bar_height
: 0;
681 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
686 if (i
!= 0 && hrh
!= 0) {
687 /* correct the last window for lost pixels */
688 if (win
== TAILQ_LAST(&ws
[current_ws
].winlist
,
690 h
= height
- (i
* hrh
);
698 /* leave first right hand window at y = 0 */
704 if (win
->transient
!= 0 || win
->floating
!= 0)
707 bzero(&wc
, sizeof wc
);
711 win
->width
= wc
.width
= w
;
712 win
->height
= wc
.height
= h
;
713 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
714 XConfigureWindow(display
, win
->id
, mask
, &wc
);
717 if (win
== ws
[current_ws
].focus
)
721 XMapRaised(display
, win
->id
);
725 focus_win(winfocus
); /* this has to be done outside of the loop */
729 horizontal_init(void)
731 DNPRINTF(SWM_D_EVENT
, "horizontal_init: workspace: %d\n", current_ws
);
735 horizontal_stack(void) {
737 struct ws_win wf
, *win
, *winfocus
= &wf
;
738 int i
, h
, w
, x
, y
, hrw
, winno
;
741 DNPRINTF(SWM_D_EVENT
, "horizontal_stack: workspace: %d\n", current_ws
);
745 winno
= count_win(current_ws
, 0);
755 hrw
= width
/ (winno
- 1);
760 y
= bar_enabled
? bar_height
: 0;
763 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
766 h
-= (2 - height
% 2);
768 if (i
!= 0 && hrw
!= 0) {
769 /* correct the last window for lost pixels */
770 if (win
== TAILQ_LAST(&ws
[current_ws
].winlist
,
772 w
= width
- (i
* hrw
);
780 /* leave first bottom window at x = 0 */
786 if (win
->transient
!= 0 || win
->floating
!= 0)
789 bzero(&wc
, sizeof wc
);
793 win
->width
= wc
.width
= w
;
794 win
->height
= wc
.height
= h
;
795 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
796 XConfigureWindow(display
, win
->id
, mask
, &wc
);
799 if (win
== ws
[current_ws
].focus
)
803 XMapRaised(display
, win
->id
);
807 focus_win(winfocus
); /* this has to be done outside of the loop */
810 /* fullscreen view */
814 DNPRINTF(SWM_D_EVENT
, "max_init: workspace: %d\n", current_ws
);
820 struct ws_win wf
, *win
, *winfocus
= &wf
;
821 int i
, h
, w
, x
, y
, winno
;
824 DNPRINTF(SWM_D_EVENT
, "max_stack: workspace: %d\n", current_ws
);
828 winno
= count_win(current_ws
, 0);
833 y
= bar_enabled
? bar_height
: 0;
834 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
835 if (i
== 1 && win
->transient
== 0 && win
->floating
!= 0) {
841 bzero(&wc
, sizeof wc
);
845 win
->width
= wc
.width
= w
;
846 win
->height
= wc
.height
= h
;
847 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
848 XConfigureWindow(display
, win
->id
, mask
, &wc
);
850 XMapRaised(display
, win
->id
);
852 /* hide all but the master window */
853 XUnmapWindow(display
, win
->id
);
858 focus_win(winfocus
); /* this has to be done outside of the loop */
862 send_to_ws(union arg
*args
)
865 struct ws_win
*win
= ws
[current_ws
].focus
;
867 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
869 XUnmapWindow(display
, win
->id
);
871 /* find a window to focus */
872 ws
[current_ws
].focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
873 if (ws
[current_ws
].focus
== NULL
)
874 ws
[current_ws
].focus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
875 if (ws
[current_ws
].focus
== win
)
876 ws
[current_ws
].focus
= NULL
;
878 TAILQ_REMOVE(&ws
[current_ws
].winlist
, win
, entry
);
880 TAILQ_INSERT_TAIL(&ws
[wsid
].winlist
, win
, entry
);
881 if (count_win(wsid
, 1) == 1)
882 ws
[wsid
].focus
= win
;
883 ws
[wsid
].restack
= 1;
888 /* key definitions */
892 void (*func
)(union arg
*);
895 /* modifier key function argument */
896 /* XXX alt-c is temporary, until I figure out how to grab spacebar */
897 { MODKEY
, XK_c
, cycle_layout
, {0} },
898 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
899 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
900 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
901 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
902 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
903 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
904 { MODKEY
, XK_p
, spawn
, {.argv
= spawn_menu
} },
905 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
906 { MODKEY
, XK_q
, restart
, {.argv
= spawn_scrotwm
} },
907 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
908 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
909 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
910 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
911 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
912 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
913 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
914 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
915 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
916 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
917 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
918 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
919 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
920 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
921 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
922 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
923 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
924 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
925 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
926 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
927 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
928 { MODKEY
, XK_b
, bar_toggle
, {0} },
929 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
930 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
934 updatenumlockmask(void)
937 XModifierKeymap
*modmap
;
939 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
941 modmap
= XGetModifierMapping(display
);
942 for (i
= 0; i
< 8; i
++)
943 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
944 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
945 == XKeysymToKeycode(display
, XK_Num_Lock
))
946 numlockmask
= (1 << i
);
948 XFreeModifiermap(modmap
);
956 unsigned int modifiers
[] =
957 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
959 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
962 XUngrabKey(display
, AnyKey
, AnyModifier
, root
);
963 for(i
= 0; i
< LENGTH(keys
); i
++) {
964 if((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
965 for(j
= 0; j
< LENGTH(modifiers
); j
++)
966 XGrabKey(display
, code
,
967 keys
[i
].mod
| modifiers
[j
], root
,
968 True
, GrabModeAsync
, GrabModeAsync
);
974 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
982 XKeyEvent
*ev
= &e
->xkey
;
984 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
986 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
987 for(i
= 0; i
< LENGTH(keys
); i
++)
988 if(keysym
== keys
[i
].keysym
989 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
991 keys
[i
].func(&(keys
[i
].args
));
995 buttonpress(XEvent
*e
)
997 XButtonPressedEvent
*ev
= &e
->xbutton
;
998 #ifdef SWM_CLICKTOFOCUS
1003 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
1005 if (ev
->window
== root
)
1007 if (ev
->window
== ws
[current_ws
].focus
->id
)
1009 #ifdef SWM_CLICKTOFOCUS
1010 TAILQ_FOREACH(win
, &ws
[current_ws
].winlist
, entry
)
1011 if (win
->id
== ev
->window
) {
1012 /* focus in the clicked window */
1013 XSetWindowBorder(display
, ev
->window
, 0xff0000);
1014 XSetWindowBorder(display
,
1015 ws
[current_ws
].focus
->id
, 0x888888);
1016 XSetInputFocus(display
, ev
->window
, RevertToPointerRoot
,
1018 ws
[current_ws
].focus
= win
;
1019 XSync(display
, False
);
1026 manage_window(Window id
)
1030 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
1032 return (win
); /* already being managed */
1035 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
1036 errx(1, "calloc: failed to allocate memory for new window");
1039 TAILQ_INSERT_TAIL(&ws
[current_ws
].winlist
, win
, entry
);
1041 XSelectInput(display
, id
, ButtonPressMask
| EnterWindowMask
|
1042 FocusChangeMask
| ExposureMask
);
1048 configurerequest(XEvent
*e
)
1050 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
1054 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: %lu\n", ev
->window
);
1057 win
= manage_window(ev
->window
);
1058 ws
[current_ws
].focus
= win
; /* make new win focused */
1060 XGetTransientForHint(display
, win
->id
, &trans
);
1062 win
->transient
= trans
;
1063 DNPRINTF(SWM_D_MISC
, "configurerequest: win %u transient %u\n",
1064 (unsigned)win
->id
, win
->transient
);
1066 XGetWindowAttributes(display
, win
->id
, &win
->wa
);
1067 XGetNormalHints(display
, win
->id
, &win
->sh
);
1069 XClassHint ch
= { 0 };
1070 if(XGetClassHint(display
, win
->id
, &ch
)) {
1071 fprintf(stderr
, "class: %s name: %s\n", ch
.res_class
, ch
.res_name
);
1072 if (!strcmp(ch
.res_class
, "Gvim") && !strcmp(ch
.res_name
, "gvim")) {
1076 XFree(ch
.res_class
);
1085 configurenotify(XEvent
*e
)
1088 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
1089 e
->xconfigure
.window
);
1093 destroynotify(XEvent
*e
)
1096 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
1098 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
1100 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
1101 if (ev
->window
== win
->id
) {
1102 /* find a window to focus */
1103 ws
[current_ws
].focus
= TAILQ_PREV(win
,
1104 ws_win_list
, entry
);
1105 if (ws
[current_ws
].focus
== NULL
)
1106 ws
[current_ws
].focus
=
1107 TAILQ_FIRST(&ws
[current_ws
].winlist
);
1108 if (win
== ws
[current_ws
].focus
)
1109 ws
[current_ws
].focus
= NULL
;
1111 TAILQ_REMOVE(&ws
[current_ws
].winlist
, win
, entry
);
1121 enternotify(XEvent
*e
)
1123 XCrossingEvent
*ev
= &e
->xcrossing
;
1126 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
1128 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) &&
1132 /* eat event(s) to prevent autofocus */
1136 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
1137 if (win
->id
== ev
->window
)
1147 XFocusChangeEvent
*ev
= &e
->xfocus
;
1149 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", ev
->window
);
1151 XSync(display
, False
); /* not sure this helps redrawing graphic apps */
1153 if (ev
->window
== root
)
1156 * kill grab for now so that we can cut and paste , this screws up
1160 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1161 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1162 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1167 mappingnotify(XEvent
*e
)
1169 XMappingEvent
*ev
= &e
->xmapping
;
1171 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
1173 XRefreshKeyboardMapping(ev
);
1174 if(ev
->request
== MappingKeyboard
)
1179 maprequest(XEvent
*e
)
1181 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
1182 e
->xmaprequest
.window
);
1184 manage_window(e
->xmaprequest
.window
);
1189 propertynotify(XEvent
*e
)
1191 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
1192 e
->xproperty
.window
);
1196 unmapnotify(XEvent
*e
)
1198 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
1202 visibilitynotify(XEvent
*e
)
1204 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n", e
->xvisibility
.window
);
1206 if (e
->xvisibility
.window
== bar_window
&&
1207 e
->xvisibility
.state
== VisibilityUnobscured
)
1211 void (*handler
[LASTEvent
])(XEvent
*) = {
1213 [KeyPress
] = keypress
,
1214 [ButtonPress
] = buttonpress
,
1215 [ConfigureRequest
] = configurerequest
,
1216 [ConfigureNotify
] = configurenotify
,
1217 [DestroyNotify
] = destroynotify
,
1218 [EnterNotify
] = enternotify
,
1219 [FocusIn
] = focusin
,
1220 [MappingNotify
] = mappingnotify
,
1221 [MapRequest
] = maprequest
,
1222 [PropertyNotify
] = propertynotify
,
1223 [UnmapNotify
] = unmapnotify
,
1224 [VisibilityNotify
] = visibilitynotify
,
1228 xerror_start(Display
*d
, XErrorEvent
*ee
)
1235 xerror(Display
*d
, XErrorEvent
*ee
)
1237 fprintf(stderr
, "error: %p %p\n", display
, ee
);
1246 xerrorxlib
= XSetErrorHandler(xerror_start
);
1248 /* this causes an error if some other window manager is running */
1249 XSelectInput(display
, DefaultRootWindow(display
),
1250 SubstructureRedirectMask
);
1251 XSync(display
, False
);
1255 XSetErrorHandler(xerror
);
1256 XSync(display
, False
);
1261 main(int argc
, char *argv
[])
1264 char conf
[PATH_MAX
], *cfile
= NULL
;
1267 unsigned int i
, num
;
1268 Window d1
, d2
, *wins
= NULL
;
1269 XWindowAttributes wa
;
1271 fprintf(stderr
, "Welcome to scrotwm V%s\n", SWM_VERSION
);
1272 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1273 warnx("no locale support");
1275 if(!(display
= XOpenDisplay(0)))
1276 errx(1, "can not open display");
1279 errx(1, "other wm running");
1281 screen
= DefaultScreen(display
);
1282 root
= RootWindow(display
, screen
);
1283 width
= DisplayWidth(display
, screen
) - 2;
1284 height
= DisplayHeight(display
, screen
) - 2;
1286 /* look for local and global conf file */
1287 pwd
= getpwuid(getuid());
1289 errx(1, "invalid user %d", getuid());
1291 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
1292 if (stat(conf
, &sb
) != -1) {
1293 if (S_ISREG(sb
.st_mode
))
1296 /* try global conf file */
1297 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
1298 if (!stat(conf
, &sb
))
1299 if (S_ISREG(sb
.st_mode
))
1305 /* init all workspaces */
1306 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
1310 TAILQ_INIT(&ws
[i
].winlist
);
1311 ws
[i
].cur_layout
= &layouts
[0];
1313 /* make work space 1 active */
1316 /* grab existing windows */
1317 if (XQueryTree(display
, root
, &d1
, &d2
, &wins
, &num
)) {
1318 for (i
= 0; i
< num
; i
++) {
1319 if (!XGetWindowAttributes(display
, wins
[i
], &wa
)
1320 || wa
.override_redirect
||
1321 XGetTransientForHint(display
, wins
[i
], &d1
))
1323 manage_window(wins
[i
]);
1328 ws
[0].focus
= TAILQ_FIRST(&ws
[0].winlist
);
1330 /* setup status bar */
1333 XSelectInput(display
, root
, SubstructureRedirectMask
|
1334 SubstructureNotifyMask
| ButtonPressMask
| KeyPressMask
|
1335 EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
|
1336 FocusChangeMask
| PropertyChangeMask
| ExposureMask
);
1342 XNextEvent(display
, &e
);
1343 if (handler
[e
.type
])
1344 handler
[e
.type
](&e
);
1347 XCloseDisplay(display
);