]>
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);
719 fprintf(stderr
, "h: %d mod: %d\n", h
, height
% 2);
721 if (i
!= 0 && hrw
!= 0) {
722 /* correct the last window for lost pixels */
723 if (win
== TAILQ_LAST(&ws
[current_ws
].winlist
,
725 w
= width
- (i
* hrw
);
733 /* leave first bottom window at x = 0 */
739 if (win
->transient
!= 0 || win
->floating
!= 0)
744 bzero(&wc
, sizeof wc
);
749 win
->width
= wc
.width
= w
;
750 win
->height
= wc
.height
= h
;
751 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
753 /* make sure we don't clobber the screen */
754 if (win
->wa
.width
> width
)
755 win
->wa
.width
= width
;
756 if (win
->wa
.height
> height
)
757 win
->wa
.width
= height
;
758 win
->x
= wc
.x
= (width
- win
->wa
.width
) / 2;
759 win
->y
= wc
.y
= (height
- win
->wa
.height
) / 2;
760 mask
= CWX
| CWY
| CWBorderWidth
;
762 XConfigureWindow(display
, win
->id
, mask
, &wc
);
764 if (win
== ws
[current_ws
].focus
)
768 XMapRaised(display
, win
->id
);
772 focus_win(winfocus
); /* this has to be done outside of the loop */
775 /* fullscreen view */
779 DNPRINTF(SWM_D_EVENT
, "max_init: workspace: %d\n", current_ws
);
785 struct ws_win wf
, *win
, *winfocus
= &wf
;
786 int i
, h
, w
, x
, y
, winno
;
789 DNPRINTF(SWM_D_EVENT
, "max_stack: workspace: %d\n", current_ws
);
793 winno
= count_win(current_ws
, 0);
798 y
= bar_enabled
? bar_height
: 0;
799 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
800 if (i
== 1 && win
->transient
== 0 && win
->floating
!= 0) {
806 bzero(&wc
, sizeof wc
);
810 win
->width
= wc
.width
= w
;
811 win
->height
= wc
.height
= h
;
812 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
813 XConfigureWindow(display
, win
->id
, mask
, &wc
);
815 XMapRaised(display
, win
->id
);
817 /* hide all but the master window */
818 XUnmapWindow(display
, win
->id
);
823 focus_win(winfocus
); /* this has to be done outside of the loop */
827 send_to_ws(union arg
*args
)
830 struct ws_win
*win
= ws
[current_ws
].focus
;
832 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
834 XUnmapWindow(display
, win
->id
);
836 /* find a window to focus */
837 ws
[current_ws
].focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
838 if (ws
[current_ws
].focus
== NULL
)
839 ws
[current_ws
].focus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
840 if (ws
[current_ws
].focus
== win
)
841 ws
[current_ws
].focus
= NULL
;
843 TAILQ_REMOVE(&ws
[current_ws
].winlist
, win
, entry
);
845 TAILQ_INSERT_TAIL(&ws
[wsid
].winlist
, win
, entry
);
846 if (count_win(wsid
, 1) == 0)
847 ws
[wsid
].focus
= win
;
848 ws
[wsid
].restack
= 1;
853 /* key definitions */
857 void (*func
)(union arg
*);
860 /* modifier key function argument */
861 /* XXX alt-c is temporary, until I figure out how to grab spacebar */
862 { MODKEY
, XK_c
, cycle_layout
, {0} },
863 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
864 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
865 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
866 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
867 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
868 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
869 { MODKEY
, XK_p
, spawn
, {.argv
= spawn_menu
} },
870 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
871 { MODKEY
, XK_q
, restart
, {.argv
= spawn_scrotwm
} },
872 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
873 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
874 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
875 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
876 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
877 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
878 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
879 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
880 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
881 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
882 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
883 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
884 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
885 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
886 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
887 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
888 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
889 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
890 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
891 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
892 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
893 { MODKEY
, XK_b
, bar_toggle
, {0} },
894 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
895 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
899 updatenumlockmask(void)
902 XModifierKeymap
*modmap
;
904 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
906 modmap
= XGetModifierMapping(display
);
907 for (i
= 0; i
< 8; i
++)
908 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
909 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
910 == XKeysymToKeycode(display
, XK_Num_Lock
))
911 numlockmask
= (1 << i
);
913 XFreeModifiermap(modmap
);
921 unsigned int modifiers
[] =
922 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
924 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
927 XUngrabKey(display
, AnyKey
, AnyModifier
, root
);
928 for(i
= 0; i
< LENGTH(keys
); i
++) {
929 if((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
930 for(j
= 0; j
< LENGTH(modifiers
); j
++)
931 XGrabKey(display
, code
,
932 keys
[i
].mod
| modifiers
[j
], root
,
933 True
, GrabModeAsync
, GrabModeAsync
);
939 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
947 XKeyEvent
*ev
= &e
->xkey
;
949 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
951 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
952 for(i
= 0; i
< LENGTH(keys
); i
++)
953 if(keysym
== keys
[i
].keysym
954 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
956 keys
[i
].func(&(keys
[i
].args
));
960 buttonpress(XEvent
*e
)
962 XButtonPressedEvent
*ev
= &e
->xbutton
;
963 #ifdef SWM_CLICKTOFOCUS
968 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
970 if (ev
->window
== root
)
972 if (ev
->window
== ws
[current_ws
].focus
->id
)
974 #ifdef SWM_CLICKTOFOCUS
975 TAILQ_FOREACH(win
, &ws
[current_ws
].winlist
, entry
)
976 if (win
->id
== ev
->window
) {
977 /* focus in the clicked window */
978 XSetWindowBorder(display
, ev
->window
, 0xff0000);
979 XSetWindowBorder(display
,
980 ws
[current_ws
].focus
->id
, 0x888888);
981 XSetInputFocus(display
, ev
->window
, RevertToPointerRoot
,
983 ws
[current_ws
].focus
= win
;
984 XSync(display
, False
);
991 manage_window(Window id
)
995 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
997 return (win
); /* already being managed */
1000 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
1001 errx(1, "calloc: failed to allocate memory for new window");
1004 TAILQ_INSERT_TAIL(&ws
[current_ws
].winlist
, win
, entry
);
1006 XSelectInput(display
, id
, ButtonPressMask
| EnterWindowMask
|
1007 FocusChangeMask
| ExposureMask
);
1014 configurerequest(XEvent
*e
)
1016 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
1020 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: %lu\n", ev
->window
);
1023 win
= manage_window(ev
->window
);
1024 ws
[current_ws
].focus
= win
; /* make new win focused */
1026 XGetTransientForHint(display
, win
->id
, &trans
);
1028 win
->transient
= trans
;
1029 DNPRINTF(SWM_D_MISC
, "configurerequest: win %u transient %u\n",
1030 (unsigned)win
->id
, win
->transient
);
1032 XGetWindowAttributes(display
, win
->id
, &win
->wa
);
1034 XClassHint ch
= { 0 };
1035 if(XGetClassHint(display
, win
->id
, &ch
)) {
1036 fprintf(stderr
, "class: %s name: %s\n", ch
.res_class
, ch
.res_name
);
1037 if (!strcmp(ch
.res_class
, "Gvim") && !strcmp(ch
.res_name
, "gvim")) {
1041 XFree(ch
.res_class
);
1050 configurenotify(XEvent
*e
)
1052 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
1053 e
->xconfigure
.window
);
1057 destroynotify(XEvent
*e
)
1060 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
1062 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
1064 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
1065 if (ev
->window
== win
->id
) {
1066 /* find a window to focus */
1067 ws
[current_ws
].focus
= TAILQ_PREV(win
,
1068 ws_win_list
, entry
);
1069 if (ws
[current_ws
].focus
== NULL
)
1070 ws
[current_ws
].focus
=
1071 TAILQ_FIRST(&ws
[current_ws
].winlist
);
1072 if (win
== ws
[current_ws
].focus
)
1073 ws
[current_ws
].focus
= NULL
;
1075 TAILQ_REMOVE(&ws
[current_ws
].winlist
, win
, entry
);
1085 enternotify(XEvent
*e
)
1087 XCrossingEvent
*ev
= &e
->xcrossing
;
1090 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
1092 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) &&
1096 /* eat event(s) to prevent autofocus */
1100 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
1101 if (win
->id
== ev
->window
)
1111 XFocusChangeEvent
*ev
= &e
->xfocus
;
1113 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", ev
->window
);
1115 XSync(display
, False
); /* not sure this helps redrawing graphic apps */
1117 if (ev
->window
== root
)
1120 * kill grab for now so that we can cut and paste , this screws up
1124 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1125 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1126 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1131 mappingnotify(XEvent
*e
)
1133 XMappingEvent
*ev
= &e
->xmapping
;
1135 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
1137 XRefreshKeyboardMapping(ev
);
1138 if(ev
->request
== MappingKeyboard
)
1143 maprequest(XEvent
*e
)
1145 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
1146 e
->xmaprequest
.window
);
1148 manage_window(e
->xmaprequest
.window
);
1153 propertynotify(XEvent
*e
)
1155 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
1156 e
->xproperty
.window
);
1160 unmapnotify(XEvent
*e
)
1162 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
1166 visibilitynotify(XEvent
*e
)
1168 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n", e
->xvisibility
.window
);
1170 if (e
->xvisibility
.window
== bar_window
&&
1171 e
->xvisibility
.state
== VisibilityUnobscured
)
1175 void (*handler
[LASTEvent
])(XEvent
*) = {
1177 [KeyPress
] = keypress
,
1178 [ButtonPress
] = buttonpress
,
1179 [ConfigureRequest
] = configurerequest
,
1180 [ConfigureNotify
] = configurenotify
,
1181 [DestroyNotify
] = destroynotify
,
1182 [EnterNotify
] = enternotify
,
1183 [FocusIn
] = focusin
,
1184 [MappingNotify
] = mappingnotify
,
1185 [MapRequest
] = maprequest
,
1186 [PropertyNotify
] = propertynotify
,
1187 [UnmapNotify
] = unmapnotify
,
1188 [VisibilityNotify
] = visibilitynotify
,
1192 xerror_start(Display
*d
, XErrorEvent
*ee
)
1199 xerror(Display
*d
, XErrorEvent
*ee
)
1201 fprintf(stderr
, "error: %p %p\n", display
, ee
);
1210 xerrorxlib
= XSetErrorHandler(xerror_start
);
1212 /* this causes an error if some other window manager is running */
1213 XSelectInput(display
, DefaultRootWindow(display
),
1214 SubstructureRedirectMask
);
1215 XSync(display
, False
);
1219 XSetErrorHandler(xerror
);
1220 XSync(display
, False
);
1225 main(int argc
, char *argv
[])
1228 char conf
[PATH_MAX
], *cfile
= NULL
;
1231 unsigned int i
, num
;
1232 Window d1
, d2
, *wins
= NULL
;
1233 XWindowAttributes wa
;
1235 fprintf(stderr
, "Welcome to scrotwm V%s\n", SWM_VERSION
);
1236 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1237 warnx("no locale support");
1239 if(!(display
= XOpenDisplay(0)))
1240 errx(1, "can not open display");
1243 errx(1, "other wm running");
1245 screen
= DefaultScreen(display
);
1246 root
= RootWindow(display
, screen
);
1247 width
= DisplayWidth(display
, screen
) - 2;
1248 height
= DisplayHeight(display
, screen
) - 2;
1250 /* look for local and global conf file */
1251 pwd
= getpwuid(getuid());
1253 errx(1, "invalid user %d", getuid());
1255 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
1256 if (stat(conf
, &sb
) != -1) {
1257 if (S_ISREG(sb
.st_mode
))
1260 /* try global conf file */
1261 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
1262 if (!stat(conf
, &sb
))
1263 if (S_ISREG(sb
.st_mode
))
1269 /* init all workspaces */
1270 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
1274 TAILQ_INIT(&ws
[i
].winlist
);
1275 ws
[i
].cur_layout
= &layouts
[0];
1277 /* make work space 1 active */
1280 /* grab existing windows */
1281 if (XQueryTree(display
, root
, &d1
, &d2
, &wins
, &num
)) {
1282 for (i
= 0; i
< num
; i
++) {
1283 if (!XGetWindowAttributes(display
, wins
[i
], &wa
)
1284 || wa
.override_redirect
||
1285 XGetTransientForHint(display
, wins
[i
], &d1
))
1287 manage_window(wins
[i
]);
1292 ws
[0].focus
= TAILQ_FIRST(&ws
[0].winlist
);
1294 /* setup status bar */
1297 XSelectInput(display
, root
, SubstructureRedirectMask
|
1298 SubstructureNotifyMask
| ButtonPressMask
| KeyPressMask
|
1299 EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
|
1300 FocusChangeMask
| PropertyChangeMask
| ExposureMask
);
1306 XNextEvent(display
, &e
);
1307 if (handler
[e
.type
])
1308 handler
[e
.type
](&e
);
1311 XCloseDisplay(display
);