]>
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 0x8001
82 #define SWM_D_EVENT 0x0002
83 #define SWM_D_WS 0x0004
84 #define SWM_D_FOCUS 0x0008
85 #define SWM_D_FOCUS 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
};
138 TAILQ_ENTRY(ws_win
) entry
;
146 XWindowAttributes wa
;
149 TAILQ_HEAD(ws_win_list
, ws_win
);
151 /* define work spaces */
152 #define SWM_WS_MAX (10)
154 int visible
; /* workspace visible */
155 int restack
; /* restack on switch */
156 struct ws_win
*focus
; /* which win has focus */
157 struct ws_win_list winlist
; /* list of windows in ws */
161 /* args to functions */
164 #define SWM_ARG_ID_FOCUSNEXT (0)
165 #define SWM_ARG_ID_FOCUSPREV (1)
166 #define SWM_ARG_ID_FOCUSMAIN (2)
167 #define SWM_ARG_ID_SWAPNEXT (3)
168 #define SWM_ARG_ID_SWAPPREV (4)
169 #define SWM_ARG_ID_SWAPMAIN (5)
176 #define SWM_CONF_WS "\n= \t"
177 #define SWM_CONF_FILE "scrotwm.conf"
179 conf_load(char *filename
)
182 char *line
, *cp
, *var
, *val
;
183 size_t len
, lineno
= 0;
185 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
187 if (filename
== NULL
)
190 if ((config
= fopen(filename
, "r")) == NULL
)
194 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
198 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
204 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
206 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
207 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
210 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
213 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
214 bar_enabled
= atoi(val
);
215 else if (!strncmp(var
, "bar_border",
216 strlen("bar_border")))
217 bar_border
= strtol(val
, NULL
, 16);
218 else if (!strncmp(var
, "bar_color",
219 strlen("bar_color")))
220 bar_color
= strtol(val
, NULL
, 16);
221 else if (!strncmp(var
, "bar_font_color",
222 strlen("bar_font_color")))
223 bar_font_color
= strtol(val
, NULL
, 16);
224 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
225 asprintf(&bar_fonts
[0], "%s", val
);
231 if (!strncmp(var
, "color_focus", strlen("color_focus")))
232 color_focus
= strtol(val
, NULL
, 16);
233 else if (!strncmp(var
, "color_unfocus",
234 strlen("color_unfocus")))
235 color_unfocus
= strtol(val
, NULL
, 16);
241 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
242 asprintf(&spawn_term
[0], "%s", val
); /* XXX args? */
253 errx(1, "invalid conf file entry: %s=%s", var
, val
);
262 if (bar_enabled
== 0)
266 XSetForeground(display
, bar_gc
, bar_color
);
267 XDrawString(display
, bar_window
, bar_gc
, 4, bar_fs
->ascent
, bar_text
,
272 localtime_r(&tmt
, &tm
);
273 strftime(bar_text
, sizeof bar_text
, "%a %b %d %R %Z %Y", &tm
);
274 XSetForeground(display
, bar_gc
, bar_font_color
);
275 XDrawString(display
, bar_window
, bar_gc
, 4, bar_fs
->ascent
, bar_text
,
277 XSync(display
, False
);
285 /* XXX yeah yeah byte me */
290 bar_toggle(union arg
*args
)
294 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
298 height
+= bar_height
; /* correct screen height */
299 XUnmapWindow(display
, bar_window
);
302 height
-= bar_height
; /* correct screen height */
303 XMapWindow(display
, bar_window
);
305 XSync(display
, False
);
306 for (i
= 0; i
< SWM_WS_MAX
; i
++)
310 bar_print(); /* must be after stack */
318 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
319 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
323 if (bar_fonts
[i
] == NULL
)
324 errx(1, "couldn't load font");
325 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
327 bar_window
= XCreateSimpleWindow(display
, root
, 0, 0, width
,
328 bar_height
- 2, 1, bar_border
, bar_color
);
329 bar_gc
= XCreateGC(display
, bar_window
, 0, &bar_gcv
);
330 XSetFont(display
, bar_gc
, bar_fs
->fid
);
331 XSelectInput(display
, bar_window
, VisibilityChangeMask
);
333 height
-= bar_height
; /* correct screen height */
334 XMapWindow(display
, bar_window
);
336 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %d\n", (int)bar_window
);
338 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
339 err(1, "could not install bar_signal");
344 count_win(int wsid
, int count_transient
)
349 TAILQ_FOREACH (win
, &ws
[wsid
].winlist
, entry
) {
350 if (count_transient
== 0 && win
->transient
)
354 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
359 quit(union arg
*args
)
361 DNPRINTF(SWM_D_MISC
, "quit\n");
367 restart(union arg
*args
)
369 XCloseDisplay(display
);
370 execvp(args
->argv
[0], args
->argv
);
371 fprintf(stderr
, "execvp failed\n");
377 spawn(union arg
*args
)
379 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
381 * The double-fork construct avoids zombie processes and keeps the code
382 * clean from stupid signal handlers.
387 close(ConnectionNumber(display
));
389 execvp(args
->argv
[0], args
->argv
);
390 fprintf(stderr
, "execvp failed\n");
399 focus_win(struct ws_win
*win
)
401 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
->id
);
402 XSetWindowBorder(display
, win
->id
, color_focus
);
403 XSetInputFocus(display
, win
->id
, RevertToPointerRoot
, CurrentTime
);
404 ws
[current_ws
].focus
= win
;
408 unfocus_win(struct ws_win
*win
)
410 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: id: %lu\n", win
->id
);
411 XSetWindowBorder(display
, win
->id
, color_unfocus
);
412 if (ws
[current_ws
].focus
== win
)
413 ws
[current_ws
].focus
= NULL
;
417 switchws(union arg
*args
)
422 DNPRINTF(SWM_D_WS
, "switchws: %d\n", wsid
+ 1);
424 if (wsid
== current_ws
)
427 /* map new window first to prevent ugly blinking */
428 TAILQ_FOREACH (win
, &ws
[wsid
].winlist
, entry
)
429 XMapRaised(display
, win
->id
);
430 ws
[wsid
].visible
= 1;
432 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
)
433 XUnmapWindow(display
, win
->id
);
434 ws
[current_ws
].visible
= 0;
439 if (ws
[wsid
].restack
) {
443 if (ws
[wsid
].focus
!= NULL
)
444 focus_win(ws
[wsid
].focus
);
445 XSync(display
, False
);
450 swapwin(union arg
*args
)
452 struct ws_win
*target
;
454 DNPRINTF(SWM_D_MOVE
, "swapwin: id %d\n", args
->id
);
455 if (ws
[current_ws
].focus
== NULL
)
459 case SWM_ARG_ID_SWAPPREV
:
460 target
= TAILQ_PREV(ws
[current_ws
].focus
, ws_win_list
, entry
);
461 TAILQ_REMOVE(&ws
[current_ws
].winlist
,
462 ws
[current_ws
].focus
, entry
);
464 TAILQ_INSERT_TAIL(&ws
[current_ws
].winlist
,
465 ws
[current_ws
].focus
, entry
);
467 TAILQ_INSERT_BEFORE(target
, ws
[current_ws
].focus
,
470 case SWM_ARG_ID_SWAPNEXT
:
471 target
= TAILQ_NEXT(ws
[current_ws
].focus
, entry
);
472 TAILQ_REMOVE(&ws
[current_ws
].winlist
,
473 ws
[current_ws
].focus
, entry
);
475 TAILQ_INSERT_HEAD(&ws
[current_ws
].winlist
,
476 ws
[current_ws
].focus
, entry
);
478 TAILQ_INSERT_AFTER(&ws
[current_ws
].winlist
, target
,
479 ws
[current_ws
].focus
, entry
);
481 case SWM_ARG_ID_SWAPMAIN
:
482 target
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
483 if (target
== ws
[current_ws
].focus
)
485 TAILQ_REMOVE(&ws
[current_ws
].winlist
, target
, entry
);
486 TAILQ_INSERT_BEFORE(ws
[current_ws
].focus
, target
, entry
);
487 TAILQ_REMOVE(&ws
[current_ws
].winlist
,
488 ws
[current_ws
].focus
, entry
);
489 TAILQ_INSERT_HEAD(&ws
[current_ws
].winlist
,
490 ws
[current_ws
].focus
, entry
);
493 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
502 focus(union arg
*args
)
504 struct ws_win
*winfocus
, *winlostfocus
;
506 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
507 if (ws
[current_ws
].focus
== NULL
)
510 winlostfocus
= ws
[current_ws
].focus
;
513 case SWM_ARG_ID_FOCUSPREV
:
514 winfocus
= TAILQ_PREV(ws
[current_ws
].focus
, ws_win_list
, entry
);
515 if (winfocus
== NULL
)
517 TAILQ_LAST(&ws
[current_ws
].winlist
, ws_win_list
);
520 case SWM_ARG_ID_FOCUSNEXT
:
521 winfocus
= TAILQ_NEXT(ws
[current_ws
].focus
, entry
);
522 if (winfocus
== NULL
)
523 winfocus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
526 case SWM_ARG_ID_FOCUSMAIN
:
527 winfocus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
534 if (winfocus
== winlostfocus
)
537 unfocus_win(winlostfocus
);
539 XSync(display
, False
);
542 /* I know this sucks but it works well enough */
547 struct ws_win wf
, *win
, *winfocus
= &wf
;
548 int i
, h
, w
, x
, y
, hrh
, winno
;
552 DNPRINTF(SWM_D_EVENT
, "stack: workspace: %d\n", current_ws
);
556 ws
[current_ws
].restack
= 0;
558 winno
= count_win(current_ws
, 0);
568 hrh
= height
/ (winno
- 1);
573 y
= bar_enabled
? bar_height
: 0;
576 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
581 if (i
!= 0 && hrh
!= 0) {
582 /* correct the last window for lost pixels */
583 if (win
== TAILQ_LAST(&ws
[current_ws
].winlist
,
585 h
= height
- (i
* hrh
);
593 /* leave first right hand window at y = 0 */
599 if (win
->transient
!= 0 || win
->floating
!= 0)
604 bzero(&wc
, sizeof wc
);
609 win
->width
= wc
.width
= w
;
610 win
->height
= wc
.height
= h
;
611 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
613 /* make sure we don't clobber the screen */
614 if (win
->wa
.width
> width
)
615 win
->wa
.width
= width
;
616 if (win
->wa
.height
> height
)
617 win
->wa
.width
= height
;
618 win
->x
= wc
.x
= (width
- win
->wa
.width
) / 2;
619 win
->y
= wc
.y
= (height
- win
->wa
.height
) / 2;
620 mask
= CWX
| CWY
| CWBorderWidth
;
622 XConfigureWindow(display
, win
->id
, mask
, &wc
);
624 if (win
== ws
[current_ws
].focus
)
628 XMapRaised(display
, win
->id
);
632 focus_win(winfocus
); /* this has to be done outside of the loop */
633 XSync(display
, False
);
637 send_to_ws(union arg
*args
)
640 struct ws_win
*win
= ws
[current_ws
].focus
;
642 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
644 XUnmapWindow(display
, win
->id
);
646 /* find a window to focus */
647 ws
[current_ws
].focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
648 if (ws
[current_ws
].focus
== NULL
)
649 ws
[current_ws
].focus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
650 if (ws
[current_ws
].focus
== win
)
651 ws
[current_ws
].focus
= NULL
;
653 TAILQ_REMOVE(&ws
[current_ws
].winlist
, win
, entry
);
655 TAILQ_INSERT_TAIL(&ws
[wsid
].winlist
, win
, entry
);
656 if (count_win(wsid
, 1) == 0)
657 ws
[wsid
].focus
= win
;
658 ws
[wsid
].restack
= 1;
663 /* key definitions */
667 void (*func
)(union arg
*);
670 /* modifier key function argument */
671 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
672 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
673 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
674 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
675 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
676 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
677 { MODKEY
, XK_p
, spawn
, {.argv
= spawn_menu
} },
678 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
679 { MODKEY
, XK_q
, restart
, {.argv
= spawn_scrotwm
} },
680 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
681 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
682 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
683 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
684 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
685 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
686 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
687 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
688 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
689 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
690 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
691 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
692 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
693 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
694 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
695 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
696 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
697 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
698 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
699 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
700 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
701 { MODKEY
, XK_b
, bar_toggle
, {0} },
702 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
703 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
707 updatenumlockmask(void)
710 XModifierKeymap
*modmap
;
712 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
714 modmap
= XGetModifierMapping(display
);
715 for (i
= 0; i
< 8; i
++)
716 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
717 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
718 == XKeysymToKeycode(display
, XK_Num_Lock
))
719 numlockmask
= (1 << i
);
721 XFreeModifiermap(modmap
);
729 unsigned int modifiers
[] =
730 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
732 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
735 XUngrabKey(display
, AnyKey
, AnyModifier
, root
);
736 for(i
= 0; i
< LENGTH(keys
); i
++) {
737 if((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
738 for(j
= 0; j
< LENGTH(modifiers
); j
++)
739 XGrabKey(display
, code
,
740 keys
[i
].mod
| modifiers
[j
], root
,
741 True
, GrabModeAsync
, GrabModeAsync
);
747 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
755 XKeyEvent
*ev
= &e
->xkey
;
757 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
759 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
760 for(i
= 0; i
< LENGTH(keys
); i
++)
761 if(keysym
== keys
[i
].keysym
762 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
764 keys
[i
].func(&(keys
[i
].args
));
768 buttonpress(XEvent
*e
)
770 XButtonPressedEvent
*ev
= &e
->xbutton
;
771 #ifdef SWM_CLICKTOFOCUS
776 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
778 if (ev
->window
== root
)
780 if (ev
->window
== ws
[current_ws
].focus
->id
)
782 #ifdef SWM_CLICKTOFOCUS
783 TAILQ_FOREACH(win
, &ws
[current_ws
].winlist
, entry
)
784 if (win
->id
== ev
->window
) {
785 /* focus in the clicked window */
786 XSetWindowBorder(display
, ev
->window
, 0xff0000);
787 XSetWindowBorder(display
,
788 ws
[current_ws
].focus
->id
, 0x888888);
789 XSetInputFocus(display
, ev
->window
, RevertToPointerRoot
,
791 ws
[current_ws
].focus
= win
;
792 XSync(display
, False
);
799 manage_window(Window id
)
803 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
805 return (win
); /* already being managed */
808 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
809 errx(1, "calloc: failed to allocate memory for new window");
812 TAILQ_INSERT_TAIL(&ws
[current_ws
].winlist
, win
, entry
);
814 XSelectInput(display
, id
, ButtonPressMask
| EnterWindowMask
|
815 FocusChangeMask
| ExposureMask
);
822 configurerequest(XEvent
*e
)
824 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
828 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: %lu\n", ev
->window
);
831 win
= manage_window(ev
->window
);
832 ws
[current_ws
].focus
= win
; /* make new win focused */
834 XGetTransientForHint(display
, win
->id
, &trans
);
836 win
->transient
= trans
;
837 DNPRINTF(SWM_D_MISC
, "configurerequest: win %u transient %u\n",
838 (unsigned)win
->id
, win
->transient
);
840 XGetWindowAttributes(display
, win
->id
, &win
->wa
);
842 XClassHint ch
= { 0 };
843 if(XGetClassHint(display
, win
->id
, &ch
)) {
844 fprintf(stderr
, "class: %s name: %s\n", ch
.res_class
, ch
.res_name
);
845 if (!strcmp(ch
.res_class
, "Gvim") && !strcmp(ch
.res_name
, "gvim")) {
858 configurenotify(XEvent
*e
)
860 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
861 e
->xconfigure
.window
);
865 destroynotify(XEvent
*e
)
868 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
870 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
872 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
873 if (ev
->window
== win
->id
) {
874 /* find a window to focus */
875 ws
[current_ws
].focus
= TAILQ_PREV(win
,
877 if (ws
[current_ws
].focus
== NULL
)
878 ws
[current_ws
].focus
=
879 TAILQ_FIRST(&ws
[current_ws
].winlist
);
880 if (win
== ws
[current_ws
].focus
)
881 ws
[current_ws
].focus
= NULL
;
883 TAILQ_REMOVE(&ws
[current_ws
].winlist
, win
, entry
);
893 enternotify(XEvent
*e
)
895 XCrossingEvent
*ev
= &e
->xcrossing
;
898 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
900 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) &&
904 /* eat event(s) to prevent autofocus */
908 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
909 if (win
->id
== ev
->window
)
919 XFocusChangeEvent
*ev
= &e
->xfocus
;
921 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", ev
->window
);
923 XSync(display
, False
); /* not sure this helps redrawing graphic apps */
925 if (ev
->window
== root
)
928 * kill grab for now so that we can cut and paste , this screws up
932 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
933 XGrabButton(display, Button1, AnyModifier, ev->window, False,
934 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
939 mappingnotify(XEvent
*e
)
941 XMappingEvent
*ev
= &e
->xmapping
;
943 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
945 XRefreshKeyboardMapping(ev
);
946 if(ev
->request
== MappingKeyboard
)
951 maprequest(XEvent
*e
)
953 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
954 e
->xmaprequest
.window
);
956 manage_window(e
->xmaprequest
.window
);
961 propertynotify(XEvent
*e
)
963 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
964 e
->xproperty
.window
);
968 unmapnotify(XEvent
*e
)
970 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
974 visibilitynotify(XEvent
*e
)
976 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n", e
->xvisibility
.window
);
978 if (e
->xvisibility
.window
== bar_window
&&
979 e
->xvisibility
.state
== VisibilityUnobscured
)
983 void (*handler
[LASTEvent
])(XEvent
*) = {
985 [KeyPress
] = keypress
,
986 [ButtonPress
] = buttonpress
,
987 [ConfigureRequest
] = configurerequest
,
988 [ConfigureNotify
] = configurenotify
,
989 [DestroyNotify
] = destroynotify
,
990 [EnterNotify
] = enternotify
,
992 [MappingNotify
] = mappingnotify
,
993 [MapRequest
] = maprequest
,
994 [PropertyNotify
] = propertynotify
,
995 [UnmapNotify
] = unmapnotify
,
996 [VisibilityNotify
] = visibilitynotify
,
1000 xerror_start(Display
*d
, XErrorEvent
*ee
)
1007 xerror(Display
*d
, XErrorEvent
*ee
)
1009 fprintf(stderr
, "error: %p %p\n", display
, ee
);
1018 xerrorxlib
= XSetErrorHandler(xerror_start
);
1020 /* this causes an error if some other window manager is running */
1021 XSelectInput(display
, DefaultRootWindow(display
),
1022 SubstructureRedirectMask
);
1023 XSync(display
, False
);
1027 XSetErrorHandler(xerror
);
1028 XSync(display
, False
);
1033 main(int argc
, char *argv
[])
1036 char conf
[PATH_MAX
], *cfile
= NULL
;
1039 unsigned int i
, num
;
1040 Window d1
, d2
, *wins
= NULL
;
1041 XWindowAttributes wa
;
1043 fprintf(stderr
, "Welcome to scrotwm V%s\n", SWM_VERSION
);
1044 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1045 warnx("no locale support");
1047 if(!(display
= XOpenDisplay(0)))
1048 errx(1, "can not open display");
1051 errx(1, "other wm running");
1053 screen
= DefaultScreen(display
);
1054 root
= RootWindow(display
, screen
);
1055 width
= DisplayWidth(display
, screen
) - 2;
1056 height
= DisplayHeight(display
, screen
) - 2;
1058 /* look for local and global conf file */
1059 pwd
= getpwuid(getuid());
1061 errx(1, "invalid user %d", getuid());
1063 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
1064 if (stat(conf
, &sb
) != -1) {
1065 if (S_ISREG(sb
.st_mode
))
1068 /* try global conf file */
1069 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
1070 if (!stat(conf
, &sb
))
1071 if (S_ISREG(sb
.st_mode
))
1077 /* make work space 1 active */
1080 TAILQ_INIT(&ws
[0].winlist
);
1081 for (i
= 1; i
< SWM_WS_MAX
; i
++) {
1085 TAILQ_INIT(&ws
[i
].winlist
);
1088 /* grab existing windows */
1089 if (XQueryTree(display
, root
, &d1
, &d2
, &wins
, &num
)) {
1090 for (i
= 0; i
< num
; i
++) {
1091 if (!XGetWindowAttributes(display
, wins
[i
], &wa
)
1092 || wa
.override_redirect
||
1093 XGetTransientForHint(display
, wins
[i
], &d1
))
1095 manage_window(wins
[i
]);
1100 ws
[0].focus
= TAILQ_FIRST(&ws
[0].winlist
);
1102 /* setup status bar */
1105 XSelectInput(display
, root
, SubstructureRedirectMask
|
1106 SubstructureNotifyMask
| ButtonPressMask
| KeyPressMask
|
1107 EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
|
1108 FocusChangeMask
| PropertyChangeMask
| ExposureMask
);
1114 XNextEvent(display
, &e
);
1115 if (handler
[e
.type
])
1116 handler
[e
.type
](&e
);
1119 XCloseDisplay(display
);