2 * Copyright (c) 2009-2010-2011 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2010-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Much code and ideas taken from dwm under the following license:
23 * MIT/X Consortium License
25 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
26 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
27 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
28 * 2007 Premysl Hruby <dfenze at gmail dot com>
29 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
30 * 2007 Christof Musik <christof at sendfax dot de>
31 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
32 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
33 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
35 * Permission is hereby granted, free of charge, to any person obtaining a
36 * copy of this software and associated documentation files (the "Software"),
37 * to deal in the Software without restriction, including without limitation
38 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
39 * and/or sell copies of the Software, and to permit persons to whom the
40 * Software is furnished to do so, subject to the following conditions:
42 * The above copyright notice and this permission notice shall be included in
43 * all copies or substantial portions of the Software.
45 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
48 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
50 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
51 * DEALINGS IN THE SOFTWARE.
69 #include <sys/types.h>
73 #include <sys/queue.h>
74 #include <sys/param.h>
75 #include <sys/select.h>
77 #include <X11/cursorfont.h>
78 #include <X11/keysym.h>
79 #include <X11/Xatom.h>
81 #include <X11/Xproto.h>
82 #include <X11/Xutil.h>
83 #include <X11/extensions/Xrandr.h>
84 #include <X11/extensions/XTest.h>
92 #ifdef SCROTWM_BUILDSTR
93 static const char *buildstr = SCROTWM_BUILDSTR;
95 static const char *buildstr = SCROTWM_VERSION;
99 # error XRandR versions less than 1.0 are not supported
104 #define SWM_XRR_HAS_CRTC
108 /*#define SWM_DEBUG*/
110 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
111 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
112 #define SWM_D_MISC 0x0001
113 #define SWM_D_EVENT 0x0002
114 #define SWM_D_WS 0x0004
115 #define SWM_D_FOCUS 0x0008
116 #define SWM_D_MOVE 0x0010
117 #define SWM_D_STACK 0x0020
118 #define SWM_D_MOUSE 0x0040
119 #define SWM_D_PROP 0x0080
120 #define SWM_D_CLASS 0x0100
121 #define SWM_D_KEY 0x0200
122 #define SWM_D_QUIRK 0x0400
123 #define SWM_D_SPAWN 0x0800
124 #define SWM_D_EVENTQ 0x1000
125 #define SWM_D_CONF 0x2000
127 u_int32_t swm_debug = 0
144 #define DPRINTF(x...)
145 #define DNPRINTF(n,x...)
148 #define LENGTH(x) (sizeof x / sizeof x[0])
149 #define MODKEY Mod1Mask
150 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
151 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
152 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
153 #define SWM_PROPLEN (16)
154 #define SWM_FUNCNAME_LEN (32)
155 #define SWM_KEYS_LEN (255)
156 #define SWM_QUIRK_LEN (64)
157 #define X(r) (r)->g.x
158 #define Y(r) (r)->g.y
159 #define WIDTH(r) (r)->g.w
160 #define HEIGHT(r) (r)->g.h
161 #define SWM_MAX_FONT_STEPS (3)
162 #define WINID(w) (w ? w->id : 0)
164 #define SWM_FOCUS_DEFAULT (0)
165 #define SWM_FOCUS_SYNERGY (1)
166 #define SWM_FOCUS_FOLLOW (2)
168 #define SWM_CONF_DEFAULT (0)
169 #define SWM_CONF_KEYMAPPING (1)
172 #define SWM_LIB "/usr/local/lib/libswmhack.so"
184 volatile sig_atomic_t running = 1;
185 volatile sig_atomic_t restart_wm = 0;
187 int last_focus_event = FocusOut;
188 int (*xerrorxlib)(Display *, XErrorEvent *);
192 int xrandr_eventbase;
193 unsigned int numlockmask = 0;
197 int cycle_visible = 0;
199 int font_adjusted = 0;
200 unsigned int mod_key = MODKEY;
203 struct swm_region *search_r;
204 int select_list_pipe[2];
205 int select_resp_pipe[2];
207 volatile sig_atomic_t search_resp;
208 int search_resp_action;
217 double dialog_ratio = .6;
219 #define SWM_BAR_MAX (256)
220 #define SWM_BAR_JUSTIFY_LEFT (0)
221 #define SWM_BAR_JUSTIFY_CENTER (1)
222 #define SWM_BAR_JUSTIFY_RIGHT (2)
223 #define SWM_BAR_OFFSET (4)
224 char *bar_argv[] = { NULL, NULL };
226 char bar_ext[SWM_BAR_MAX];
227 char bar_vertext[SWM_BAR_MAX];
229 sig_atomic_t bar_alarm = 0;
232 int bar_border_width = 1;
233 int bar_at_bottom = 0;
235 int bar_extra_running = 0;
238 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
239 int stack_enabled = 1;
240 int clock_enabled = 1;
241 int urgent_enabled = 0;
242 char *clock_format = NULL;
243 int title_name_enabled = 0;
244 int title_class_enabled = 0;
245 int window_name_enabled = 0;
246 int focus_mode = SWM_FOCUS_DEFAULT;
247 int disable_border = 0;
248 int border_width = 1;
249 int verbose_layout = 0;
255 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
256 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
259 #define SWM_MENU_FN (2)
260 #define SWM_MENU_NB (4)
261 #define SWM_MENU_NF (6)
262 #define SWM_MENU_SB (8)
263 #define SWM_MENU_SF (10)
265 /* layout manager data */
266 struct swm_geometry {
276 /* virtual "screens" */
278 TAILQ_ENTRY(swm_region) entry;
279 struct swm_geometry g;
280 struct workspace *ws; /* current workspace on this region */
281 struct workspace *ws_prior; /* prior workspace on this region */
282 struct swm_screen *s; /* screen idx */
285 TAILQ_HEAD(swm_region_list, swm_region);
288 TAILQ_ENTRY(ws_win) entry;
291 struct ws_win *child_trans; /* transient child window */
292 struct swm_geometry g; /* current geometry */
293 struct swm_geometry g_float; /* geometry when floating */
294 struct swm_geometry rg_float; /* region geom when floating */
295 int g_floatvalid; /* flag: geometry in g_float is valid */
296 int floatmaxed; /* flag: floater was maxed in max_stack */
300 unsigned int ewmh_flags;
301 int font_size_boundary[SWM_MAX_FONT_STEPS];
307 unsigned long quirks;
308 struct workspace *ws; /* always valid */
309 struct swm_screen *s; /* always valid, never changes */
310 XWindowAttributes wa;
315 TAILQ_HEAD(ws_win_list, ws_win);
319 TAILQ_ENTRY(pid_e) entry;
323 TAILQ_HEAD(pid_list, pid_e);
324 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
326 /* layout handlers */
328 void vertical_config(struct workspace *, int);
329 void vertical_stack(struct workspace *, struct swm_geometry *);
330 void horizontal_config(struct workspace *, int);
331 void horizontal_stack(struct workspace *, struct swm_geometry *);
332 void max_stack(struct workspace *, struct swm_geometry *);
333 void plain_stacker(struct workspace *);
334 void fancy_stacker(struct workspace *);
336 struct ws_win *find_window(Window);
338 void grabbuttons(struct ws_win *, int);
339 void new_region(struct swm_screen *, int, int, int, int);
340 void unmanage_window(struct ws_win *);
341 long getstate(Window);
343 int conf_load(char *, int);
346 void (*l_stack)(struct workspace *, struct swm_geometry *);
347 void (*l_config)(struct workspace *, int);
349 #define SWM_L_FOCUSPREV (1<<0)
350 #define SWM_L_MAPONFOCUS (1<<1)
351 void (*l_string)(struct workspace *);
353 /* stack, configure */
354 { vertical_stack, vertical_config, 0, plain_stacker },
355 { horizontal_stack, horizontal_config, 0, plain_stacker },
357 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
358 { NULL, NULL, 0, NULL },
361 /* position of max_stack mode in the layouts array, index into layouts! */
362 #define SWM_V_STACK (0)
363 #define SWM_H_STACK (1)
364 #define SWM_MAX_STACK (2)
366 #define SWM_H_SLICE (32)
367 #define SWM_V_SLICE (32)
369 /* define work spaces */
371 int idx; /* workspace index */
372 int always_raise; /* raise windows on focus */
373 struct layout *cur_layout; /* current layout handlers */
374 struct ws_win *focus; /* may be NULL */
375 struct ws_win *focus_prev; /* may be NULL */
376 struct swm_region *r; /* may be NULL */
377 struct swm_region *old_r; /* may be NULL */
378 struct ws_win_list winlist; /* list of windows in ws */
379 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
380 char stacker[10]; /* display stacker and layout */
384 int horizontal_msize;
386 int horizontal_stacks;
393 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
394 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
396 /* physical screen mapping */
397 #define SWM_WS_MAX (10)
399 int idx; /* screen index */
400 struct swm_region_list rl; /* list of regions on this screen */
401 struct swm_region_list orl; /* list of old regions */
403 struct workspace ws[SWM_WS_MAX];
409 } c[SWM_S_COLOR_MAX];
411 struct swm_screen *screens;
414 /* args to functions */
417 #define SWM_ARG_ID_FOCUSNEXT (0)
418 #define SWM_ARG_ID_FOCUSPREV (1)
419 #define SWM_ARG_ID_FOCUSMAIN (2)
420 #define SWM_ARG_ID_FOCUSCUR (4)
421 #define SWM_ARG_ID_SWAPNEXT (10)
422 #define SWM_ARG_ID_SWAPPREV (11)
423 #define SWM_ARG_ID_SWAPMAIN (12)
424 #define SWM_ARG_ID_MOVELAST (13)
425 #define SWM_ARG_ID_MASTERSHRINK (20)
426 #define SWM_ARG_ID_MASTERGROW (21)
427 #define SWM_ARG_ID_MASTERADD (22)
428 #define SWM_ARG_ID_MASTERDEL (23)
429 #define SWM_ARG_ID_STACKRESET (30)
430 #define SWM_ARG_ID_STACKINIT (31)
431 #define SWM_ARG_ID_CYCLEWS_UP (40)
432 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
433 #define SWM_ARG_ID_CYCLESC_UP (42)
434 #define SWM_ARG_ID_CYCLESC_DOWN (43)
435 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
436 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
437 #define SWM_ARG_ID_STACKINC (50)
438 #define SWM_ARG_ID_STACKDEC (51)
439 #define SWM_ARG_ID_SS_ALL (60)
440 #define SWM_ARG_ID_SS_WINDOW (61)
441 #define SWM_ARG_ID_DONTCENTER (70)
442 #define SWM_ARG_ID_CENTER (71)
443 #define SWM_ARG_ID_KILLWINDOW (80)
444 #define SWM_ARG_ID_DELETEWINDOW (81)
445 #define SWM_ARG_ID_WIDTHGROW (90)
446 #define SWM_ARG_ID_WIDTHSHRINK (91)
447 #define SWM_ARG_ID_HEIGHTGROW (92)
448 #define SWM_ARG_ID_HEIGHTSHRINK (93)
449 #define SWM_ARG_ID_MOVEUP (100)
450 #define SWM_ARG_ID_MOVEDOWN (101)
451 #define SWM_ARG_ID_MOVELEFT (102)
452 #define SWM_ARG_ID_MOVERIGHT (103)
456 void focus(struct swm_region *, union arg *);
457 void focus_magic(struct ws_win *);
464 #define SWM_Q_FLOAT (1<<0) /* float this window */
465 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
466 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
467 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
468 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
469 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
471 int quirks_size = 0, quirks_length = 0;
472 struct quirk *quirks = NULL;
475 * Supported EWMH hints should be added to
476 * both the enum and the ewmh array
478 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
479 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
480 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
481 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
482 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
483 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
484 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
485 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
486 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
487 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
493 } ewmh[SWM_EWMH_HINT_MAX] = {
494 /* must be in same order as in the enum */
495 {"_NET_ACTIVE_WINDOW", None},
496 {"_NET_MOVERESIZE_WINDOW", None},
497 {"_NET_CLOSE_WINDOW", None},
498 {"_NET_WM_WINDOW_TYPE", None},
499 {"_NET_WM_WINDOW_TYPE_DOCK", None},
500 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
501 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
502 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
503 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
504 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
505 {"_NET_WM_STATE", None},
506 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
507 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
508 {"_NET_WM_STATE_SKIP_TASKBAR", None},
509 {"_NET_WM_STATE_SKIP_PAGER", None},
510 {"_NET_WM_STATE_HIDDEN", None},
511 {"_NET_WM_STATE_ABOVE", None},
512 {"_SWM_WM_STATE_MANUAL", None},
513 {"_NET_WM_STATE_FULLSCREEN", None},
514 {"_NET_WM_ALLOWED_ACTIONS", None},
515 {"_NET_WM_ACTION_MOVE", None},
516 {"_NET_WM_ACTION_RESIZE", None},
517 {"_NET_WM_ACTION_FULLSCREEN", None},
518 {"_NET_WM_ACTION_CLOSE", None},
521 void store_float_geom(struct ws_win *win, struct swm_region *r);
522 int floating_toggle_win(struct ws_win *win);
523 void spawn_select(struct swm_region *, union arg *, char *, int *);
526 get_property(Window id, Atom atom, long count, Atom type,
527 unsigned long *n, unsigned char **data)
530 unsigned long tmp, extra;
531 unsigned long *nitems;
534 nitems = n != NULL ? n : &tmp;
535 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
536 &real, &format, nitems, &extra, data);
538 if (status != Success)
547 update_iconic(struct ws_win *win, int newv)
554 iprop = XInternAtom(display, "_SWM_ICONIC", False);
558 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
559 PropModeReplace, (unsigned char *)&v, 1);
561 XDeleteProperty(display, win->id, iprop);
565 get_iconic(struct ws_win *win)
570 unsigned long nitems, extra;
571 unsigned char *prop = NULL;
573 iprop = XInternAtom(display, "_SWM_ICONIC", False);
576 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
577 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
578 if (status != Success)
580 if (rettype != XA_INTEGER || retfmt != 32)
584 v = *((int32_t *)prop);
598 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
600 for (i = 0; i < LENGTH(ewmh); i++)
601 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
603 for (i = 0; i < ScreenCount(display); i++) {
604 /* Support check window will be created by workaround(). */
606 /* Report supported atoms */
607 XDeleteProperty(display, screens[i].root, sup_list);
608 for (j = 0; j < LENGTH(ewmh); j++)
609 XChangeProperty(display, screens[i].root,
610 sup_list, XA_ATOM, 32,
611 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
619 unsigned char *data = NULL;
621 Atom sup_check, sup_list;
624 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
625 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
627 for (i = 0; i < ScreenCount(display); i++) {
628 /* Get the support check window and destroy it */
629 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
634 XDestroyWindow(display, id);
635 XDeleteProperty(display, screens[i].root, sup_check);
636 XDeleteProperty(display, screens[i].root, sup_list);
644 ewmh_autoquirk(struct ws_win *win)
647 unsigned long *data = NULL, n;
650 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
651 XA_ATOM, &n, (void *)&data);
658 for (i = 0; i < n; i++) {
660 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
662 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
663 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
664 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
666 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
669 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
670 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
672 win->quirks = SWM_Q_FLOAT;
680 #define SWM_EWMH_ACTION_COUNT_MAX (6)
681 #define EWMH_F_FULLSCREEN (1<<0)
682 #define EWMH_F_ABOVE (1<<1)
683 #define EWMH_F_HIDDEN (1<<2)
684 #define EWMH_F_SKIP_PAGER (1<<3)
685 #define EWMH_F_SKIP_TASKBAR (1<<4)
686 #define SWM_F_MANUAL (1<<5)
689 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
691 struct swm_geometry rg;
699 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
705 store_float_geom(win, win->ws->r);
712 if (win->g_floatvalid) {
713 /* refloat at last floating relative position */
714 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
715 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
716 win->g.w = win->g_float.w;
717 win->g.h = win->g_float.h;
725 ewmh_update_actions(struct ws_win *win)
727 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
733 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
736 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
737 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
740 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
741 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
744 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
745 #define _NET_WM_STATE_ADD 1 /* add/set property */
746 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
749 ewmh_update_win_state(struct ws_win *win, long state, long action)
751 unsigned int mask = 0;
752 unsigned int changed = 0;
753 unsigned int orig_flags;
758 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
759 mask = EWMH_F_FULLSCREEN;
760 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
762 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
764 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
765 mask = EWMH_F_SKIP_PAGER;
766 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
767 mask = EWMH_F_SKIP_TASKBAR;
770 orig_flags = win->ewmh_flags;
773 case _NET_WM_STATE_REMOVE:
774 win->ewmh_flags &= ~mask;
776 case _NET_WM_STATE_ADD:
777 win->ewmh_flags |= mask;
779 case _NET_WM_STATE_TOGGLE:
780 win->ewmh_flags ^= mask;
784 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
786 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
788 if (!floating_toggle_win(win))
789 win->ewmh_flags = orig_flags; /* revert */
790 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
792 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
793 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
795 if (!ewmh_set_win_fullscreen(win,
796 win->ewmh_flags & EWMH_F_FULLSCREEN))
797 win->ewmh_flags = orig_flags; /* revert */
799 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
801 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
802 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
803 XA_ATOM, 32, PropModeAppend,
804 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
805 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
806 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
807 XA_ATOM, 32, PropModeAppend,
808 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
809 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
810 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
811 XA_ATOM, 32, PropModeAppend,
812 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
813 if (win->ewmh_flags & EWMH_F_ABOVE)
814 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
815 XA_ATOM, 32, PropModeAppend,
816 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
817 if (win->ewmh_flags & SWM_F_MANUAL)
818 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
819 XA_ATOM, 32, PropModeAppend,
820 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
824 ewmh_get_win_state(struct ws_win *win)
835 win->ewmh_flags |= EWMH_F_ABOVE;
837 win->ewmh_flags |= SWM_F_MANUAL;
839 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
840 (~0L), XA_ATOM, &n, (void *)&states);
845 for (i = 0; i < n; i++)
846 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
866 name = "ButtonPress";
869 name = "ButtonRelease";
872 name = "MotionNotify";
875 name = "EnterNotify";
878 name = "LeaveNotify";
887 name = "KeymapNotify";
893 name = "GraphicsExpose";
898 case VisibilityNotify:
899 name = "VisibilityNotify";
902 name = "CreateNotify";
905 name = "DestroyNotify";
908 name = "UnmapNotify";
917 name = "ReparentNotify";
919 case ConfigureNotify:
920 name = "ConfigureNotify";
922 case ConfigureRequest:
923 name = "ConfigureRequest";
926 name = "GravityNotify";
929 name = "ResizeRequest";
931 case CirculateNotify:
932 name = "CirculateNotify";
934 case CirculateRequest:
935 name = "CirculateRequest";
938 name = "PropertyNotify";
941 name = "SelectionClear";
943 case SelectionRequest:
944 name = "SelectionRequest";
946 case SelectionNotify:
947 name = "SelectionNotify";
950 name = "ColormapNotify";
953 name = "ClientMessage";
956 name = "MappingNotify";
961 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
963 e->xany.window, name, e->type, QLength(display));
965 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
967 e->xany.window, e->type, QLength(display));
971 dumpwins(struct swm_region *r, union arg *args)
975 XWindowAttributes wa;
978 fprintf(stderr, "invalid workspace\n");
982 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
984 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
985 state = getstate(win->id);
986 if (!XGetWindowAttributes(display, win->id, &wa))
987 fprintf(stderr, "window: %lu failed "
988 "XGetWindowAttributes\n", win->id);
989 fprintf(stderr, "window: %lu map_state: %d state: %d "
991 win->id, wa.map_state, state, win->transient);
994 fprintf(stderr, "===== unmanaged window list =====\n");
995 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
996 state = getstate(win->id);
997 if (!XGetWindowAttributes(display, win->id, &wa))
998 fprintf(stderr, "window: %lu failed "
999 "XGetWindowAttributes\n", win->id);
1000 fprintf(stderr, "window: %lu map_state: %d state: %d "
1002 win->id, wa.map_state, state, win->transient);
1005 fprintf(stderr, "=================================\n");
1008 #define dumpevent(e)
1010 dumpwins(struct swm_region *r, union arg *args)
1013 #endif /* SWM_DEBUG */
1015 void expose(XEvent *);
1016 void keypress(XEvent *);
1017 void buttonpress(XEvent *);
1018 void configurerequest(XEvent *);
1019 void configurenotify(XEvent *);
1020 void destroynotify(XEvent *);
1021 void enternotify(XEvent *);
1022 void focusevent(XEvent *);
1023 void mapnotify(XEvent *);
1024 void mappingnotify(XEvent *);
1025 void maprequest(XEvent *);
1026 void propertynotify(XEvent *);
1027 void unmapnotify(XEvent *);
1028 void visibilitynotify(XEvent *);
1029 void clientmessage(XEvent *);
1031 void (*handler[LASTEvent])(XEvent *) = {
1033 [KeyPress] = keypress,
1034 [ButtonPress] = buttonpress,
1035 [ConfigureRequest] = configurerequest,
1036 [ConfigureNotify] = configurenotify,
1037 [DestroyNotify] = destroynotify,
1038 [EnterNotify] = enternotify,
1039 [FocusIn] = focusevent,
1040 [FocusOut] = focusevent,
1041 [MapNotify] = mapnotify,
1042 [MappingNotify] = mappingnotify,
1043 [MapRequest] = maprequest,
1044 [PropertyNotify] = propertynotify,
1045 [UnmapNotify] = unmapnotify,
1046 [VisibilityNotify] = visibilitynotify,
1047 [ClientMessage] = clientmessage,
1053 int saved_errno, status;
1056 saved_errno = errno;
1060 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1065 if (errno != ECHILD)
1066 warn("sighdlr: waitpid");
1067 #endif /* SWM_DEBUG */
1070 if (pid == searchpid)
1074 if (WIFEXITED(status)) {
1075 if (WEXITSTATUS(status) != 0)
1076 warnx("sighdlr: child exit status: %d",
1077 WEXITSTATUS(status));
1079 warnx("sighdlr: child is terminated "
1081 #endif /* SWM_DEBUG */
1095 errno = saved_errno;
1101 struct pid_e *p = NULL;
1103 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1108 TAILQ_FOREACH(p, &pidlist, entry) {
1117 name_to_color(char *colorname)
1121 XColor screen_def, exact_def;
1122 unsigned long result = 0;
1123 char cname[32] = "#";
1125 cmap = DefaultColormap(display, screens[0].idx);
1126 status = XAllocNamedColor(display, cmap, colorname,
1127 &screen_def, &exact_def);
1129 strlcat(cname, colorname + 2, sizeof cname - 1);
1130 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1134 result = screen_def.pixel;
1136 fprintf(stderr, "color '%s' not found.\n", colorname);
1142 setscreencolor(char *val, int i, int c)
1144 if (i > 0 && i <= ScreenCount(display)) {
1145 screens[i - 1].c[c].color = name_to_color(val);
1146 free(screens[i - 1].c[c].name);
1147 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1149 } else if (i == -1) {
1150 for (i = 0; i < ScreenCount(display); i++) {
1151 screens[i].c[c].color = name_to_color(val);
1152 free(screens[i].c[c].name);
1153 if ((screens[i].c[c].name = strdup(val)) == NULL)
1157 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1158 i, ScreenCount(display));
1162 fancy_stacker(struct workspace *ws)
1164 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1165 if (ws->cur_layout->l_stack == vertical_stack)
1166 snprintf(ws->stacker, sizeof ws->stacker, "[%d|%d]",
1167 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1168 if (ws->cur_layout->l_stack == horizontal_stack)
1169 snprintf(ws->stacker, sizeof ws->stacker, "[%d-%d]",
1170 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1174 plain_stacker(struct workspace *ws)
1176 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1177 if (ws->cur_layout->l_stack == vertical_stack)
1178 strlcpy(ws->stacker, "[|]", sizeof ws->stacker);
1179 if (ws->cur_layout->l_stack == horizontal_stack)
1180 strlcpy(ws->stacker, "[-]", sizeof ws->stacker);
1184 custom_region(char *val)
1186 unsigned int sidx, x, y, w, h;
1188 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1189 errx(1, "invalid custom region, "
1190 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1191 if (sidx < 1 || sidx > ScreenCount(display))
1192 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1193 sidx, ScreenCount(display));
1197 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1199 if (x > DisplayWidth(display, sidx) ||
1200 y > DisplayHeight(display, sidx) ||
1201 w + x > DisplayWidth(display, sidx) ||
1202 h + y > DisplayHeight(display, sidx)) {
1203 fprintf(stderr, "ignoring region %ux%u+%u+%u "
1204 "- not within screen boundaries "
1205 "(%ux%u)\n", w, h, x, y,
1206 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1210 new_region(&screens[sidx], x, y, w, h);
1214 socket_setnonblock(int fd)
1218 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1219 err(1, "fcntl F_GETFL");
1220 flags |= O_NONBLOCK;
1221 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1222 err(1, "fcntl F_SETFL");
1226 bar_print(struct swm_region *r, char *s)
1231 XClearWindow(display, r->bar_window);
1232 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1235 textwidth = XTextWidth(bar_fs, s, len);
1237 switch (bar_justify) {
1238 case SWM_BAR_JUSTIFY_LEFT:
1241 case SWM_BAR_JUSTIFY_CENTER:
1242 x = (WIDTH(r) - textwidth) / 2;
1244 case SWM_BAR_JUSTIFY_RIGHT:
1245 x = WIDTH(r) - textwidth - SWM_BAR_OFFSET;
1249 if (x < SWM_BAR_OFFSET)
1252 XDrawString(display, r->bar_window, bar_gc, x, bar_fs->ascent, s, len);
1256 bar_extra_stop(void)
1260 bzero(bar_pipe, sizeof bar_pipe);
1263 kill(bar_pid, SIGTERM);
1266 strlcpy(bar_ext, "", sizeof bar_ext);
1271 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1273 int do_class, do_name;
1275 XClassHint *xch = NULL;
1277 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1278 cur_focus != NULL) {
1279 if ((xch = XAllocClassHint()) == NULL)
1281 status = XGetClassHint(display, cur_focus->id, xch);
1282 if (status == BadWindow || status == BadAlloc)
1284 do_class = (title_class_enabled && xch->res_class != NULL);
1285 do_name = (title_name_enabled && xch->res_name != NULL);
1287 strlcat(s, xch->res_class, sz);
1288 if (do_class && do_name)
1289 strlcat(s, ":", sz);
1291 strlcat(s, xch->res_name, sz);
1292 strlcat(s, " ", sz);
1300 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1304 if (window_name_enabled && cur_focus != NULL) {
1305 XFetchName(display, cur_focus->id, &title);
1307 if (cur_focus->floating)
1308 strlcat(s, "(f) ", sz);
1309 strlcat(s, title, sz);
1310 strlcat(s, " ", sz);
1316 int urgent[SWM_WS_MAX];
1318 bar_urgent(char *s, ssize_t sz)
1320 XWMHints *wmh = NULL;
1325 if (urgent_enabled == 0)
1328 for (i = 0; i < SWM_WS_MAX; i++)
1331 for (i = 0; i < ScreenCount(display); i++)
1332 for (j = 0; j < SWM_WS_MAX; j++)
1333 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1334 wmh = XGetWMHints(display, win->id);
1338 if (wmh->flags & XUrgencyHint)
1343 strlcat(s, "* ", sz);
1344 for (i = 0; i < SWM_WS_MAX; i++) {
1346 snprintf(b, sizeof b, "%d ", i + 1);
1348 snprintf(b, sizeof b, "- ");
1351 strlcat(s, "* ", sz);
1359 struct swm_region *r;
1362 char s[SWM_BAR_MAX];
1363 char cn[SWM_BAR_MAX];
1364 char loc[SWM_BAR_MAX];
1365 char *b, *stack = "";
1367 if (bar_enabled == 0)
1369 if (bar_extra && bar_extra_running) {
1370 /* ignore short reads; it'll correct itself */
1371 while ((b = fgetln(stdin, &len)) != NULL)
1372 if (b && b[len - 1] == '\n') {
1374 strlcpy(bar_ext, b, sizeof bar_ext);
1376 if (b == NULL && errno != EAGAIN) {
1377 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1378 errno, strerror(errno));
1382 strlcpy(bar_ext, "", sizeof bar_ext);
1384 if (clock_enabled == 0)
1385 strlcpy(s, "", sizeof s);
1388 localtime_r(&tmt, &tm);
1389 strftime(s, sizeof s, clock_format, &tm);
1390 strlcat(s, " ", sizeof s);
1393 for (i = 0; i < ScreenCount(display); i++) {
1395 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1396 strlcpy(cn, "", sizeof cn);
1398 bar_urgent(cn, sizeof cn);
1399 bar_class_name(cn, sizeof cn, r->ws->focus);
1400 bar_window_name(cn, sizeof cn, r->ws->focus);
1403 stack = r->ws->stacker;
1405 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1406 x++, r->ws->idx + 1, stack, s, cn, bar_ext, bar_vertext);
1420 bar_toggle(struct swm_region *r, union arg *args)
1422 struct swm_region *tmpr;
1423 int i, sc = ScreenCount(display);
1425 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1428 for (i = 0; i < sc; i++)
1429 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1430 XUnmapWindow(display, tmpr->bar_window);
1432 for (i = 0; i < sc; i++)
1433 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1434 XMapRaised(display, tmpr->bar_window);
1436 bar_enabled = !bar_enabled;
1439 /* must be after stack */
1446 XSetWindowAttributes wa;
1447 struct swm_region *r;
1450 /* do this here because the conf file is in memory */
1451 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1452 /* launch external status app */
1453 bar_extra_running = 1;
1454 if (pipe(bar_pipe) == -1)
1455 err(1, "pipe error");
1456 socket_setnonblock(bar_pipe[0]);
1457 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1458 if (dup2(bar_pipe[0], 0) == -1)
1460 if (dup2(bar_pipe[1], 1) == -1)
1462 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1463 err(1, "could not disable SIGPIPE");
1464 switch (bar_pid = fork()) {
1466 err(1, "cannot fork");
1470 execvp(bar_argv[0], bar_argv);
1471 err(1, "%s external app failed", bar_argv[0]);
1473 default: /* parent */
1479 bzero(&wa, sizeof wa);
1480 for (i = 0; i < ScreenCount(display); i++)
1481 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1483 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1484 wa.background_pixel =
1485 screens[i].c[SWM_S_COLOR_BAR].color;
1486 XChangeWindowAttributes(display, r->bar_window,
1487 CWBackPixel | CWBorderPixel, &wa);
1493 bar_setup(struct swm_region *r)
1498 XFreeFont(display, bar_fs);
1502 for (i = 0; bar_fonts[i] != NULL; i++) {
1503 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1509 if (bar_fonts[i] == NULL)
1510 errx(1, "couldn't load font");
1512 errx(1, "couldn't create font structure");
1514 bar_height = bar_fs->ascent + bar_fs->descent + 1 +
1515 2 * bar_border_width;
1517 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1519 r->bar_window = XCreateSimpleWindow(display,
1520 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1521 bar_height - 2 * bar_border_width,
1522 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1523 r->s->c[SWM_S_COLOR_BAR].color);
1524 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1525 XSetFont(display, bar_gc, bar_fs->fid);
1526 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1528 XMapRaised(display, r->bar_window);
1529 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1531 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1532 err(1, "could not install bar_signal");
1537 drain_enter_notify(void)
1542 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1545 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained %d\n", i);
1549 set_win_state(struct ws_win *win, long state)
1551 long data[] = {state, None};
1553 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1558 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1559 (unsigned char *)data, 2);
1566 unsigned char *p = NULL;
1569 if (!get_property(w, astate, 2L, astate, &n, &p))
1572 result = *((long *)p);
1578 version(struct swm_region *r, union arg *args)
1580 bar_version = !bar_version;
1582 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s Build: %s",
1583 SCROTWM_VERSION, buildstr);
1585 strlcpy(bar_vertext, "", sizeof bar_vertext);
1590 client_msg(struct ws_win *win, Atom a)
1592 XClientMessageEvent cm;
1597 bzero(&cm, sizeof cm);
1598 cm.type = ClientMessage;
1599 cm.window = win->id;
1600 cm.message_type = aprot;
1603 cm.data.l[1] = CurrentTime;
1604 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1608 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1616 DNPRINTF(SWM_D_MISC,
1617 "config_win: win %lu x %d y %d w %d h %d\n",
1618 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1620 ce.type = ConfigureNotify;
1621 ce.display = display;
1623 ce.window = win->id;
1626 ce.width = win->g.w;
1627 ce.height = win->g.h;
1628 ce.border_width = border_width;
1630 ce.override_redirect = False;
1632 DNPRINTF(SWM_D_MISC,
1633 "config_win: ev win %lu x %d y %d w %d h %d\n",
1634 ev->window, ev->x, ev->y, ev->width, ev->height);
1635 ce.type = ConfigureNotify;
1636 ce.display = ev->display;
1637 ce.event = ev->window;
1638 ce.window = ev->window;
1641 ce.width = ev->width;
1642 ce.height = ev->height;
1643 ce.border_width = ev->border_width;
1644 ce.above = ev->above;
1645 ce.override_redirect = False;
1648 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1652 count_win(struct workspace *ws, int count_transient)
1657 TAILQ_FOREACH(win, &ws->winlist, entry) {
1658 if (count_transient == 0 && win->floating)
1660 if (count_transient == 0 && win->transient)
1666 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1672 quit(struct swm_region *r, union arg *args)
1674 DNPRINTF(SWM_D_MISC, "quit\n");
1679 unmap_window(struct ws_win *win)
1684 /* don't unmap again */
1685 if (getstate(win->id) == IconicState)
1688 set_win_state(win, IconicState);
1690 XUnmapWindow(display, win->id);
1691 XSetWindowBorder(display, win->id,
1692 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1701 for (i = 0; i < ScreenCount(display); i++)
1702 for (j = 0; j < SWM_WS_MAX; j++)
1703 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1708 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1715 event.display = display; /* Ignored, but what the hell */
1716 event.window = win->id;
1717 event.root = win->s->root;
1718 event.subwindow = None;
1719 event.time = CurrentTime;
1724 event.same_screen = True;
1725 event.keycode = XKeysymToKeycode(display, keysym);
1726 event.state = modifiers;
1728 event.type = KeyPress;
1729 XSendEvent(event.display, event.window, True,
1730 KeyPressMask, (XEvent *)&event);
1732 event.type = KeyRelease;
1733 XSendEvent(event.display, event.window, True,
1734 KeyPressMask, (XEvent *)&event);
1739 restart(struct swm_region *r, union arg *args)
1741 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1743 /* disable alarm because the following code may not be interrupted */
1745 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1746 errx(1, "can't disable alarm");
1751 XCloseDisplay(display);
1752 execvp(start_argv[0], start_argv);
1753 fprintf(stderr, "execvp failed\n");
1759 root_to_region(Window root)
1761 struct swm_region *r = NULL;
1763 int i, x, y, wx, wy;
1766 for (i = 0; i < ScreenCount(display); i++)
1767 if (screens[i].root == root)
1770 if (XQueryPointer(display, screens[i].root,
1771 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1772 /* choose a region based on pointer location */
1773 TAILQ_FOREACH(r, &screens[i].rl, entry)
1774 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1775 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1780 r = TAILQ_FIRST(&screens[i].rl);
1786 find_unmanaged_window(Window id)
1791 for (i = 0; i < ScreenCount(display); i++)
1792 for (j = 0; j < SWM_WS_MAX; j++)
1793 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1801 find_window(Window id)
1804 Window wrr, wpr, *wcr = NULL;
1808 for (i = 0; i < ScreenCount(display); i++)
1809 for (j = 0; j < SWM_WS_MAX; j++)
1810 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1814 /* if we were looking for the parent return that window instead */
1815 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1820 /* ignore not found and root */
1821 if (wpr == 0 || wrr == wpr)
1824 /* look for parent */
1825 for (i = 0; i < ScreenCount(display); i++)
1826 for (j = 0; j < SWM_WS_MAX; j++)
1827 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1835 spawn(int ws_idx, union arg *args, int close_fd)
1840 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1843 close(ConnectionNumber(display));
1845 setenv("LD_PRELOAD", SWM_LIB, 1);
1847 if (asprintf(&ret, "%d", ws_idx) == -1) {
1851 setenv("_SWM_WS", ret, 1);
1855 if (asprintf(&ret, "%d", getpid()) == -1) {
1859 setenv("_SWM_PID", ret, 1);
1863 if (setsid() == -1) {
1870 * close stdin and stdout to prevent interaction between apps
1871 * and the baraction script
1872 * leave stderr open to record errors
1874 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1878 dup2(fd, STDIN_FILENO);
1879 dup2(fd, STDOUT_FILENO);
1884 execvp(args->argv[0], args->argv);
1891 spawnterm(struct swm_region *r, union arg *args)
1893 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1897 setenv("_SWM_XTERM_FONTADJ", "", 1);
1898 spawn(r->ws->idx, args, 1);
1903 kill_refs(struct ws_win *win)
1906 struct swm_region *r;
1907 struct workspace *ws;
1912 for (i = 0; i < ScreenCount(display); i++)
1913 TAILQ_FOREACH(r, &screens[i].rl, entry)
1914 for (x = 0; x < SWM_WS_MAX; x++) {
1916 if (win == ws->focus)
1918 if (win == ws->focus_prev)
1919 ws->focus_prev = NULL;
1924 validate_win(struct ws_win *testwin)
1927 struct workspace *ws;
1928 struct swm_region *r;
1929 int i, x, foundit = 0;
1931 if (testwin == NULL)
1934 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1935 TAILQ_FOREACH(r, &screens[i].rl, entry)
1936 for (x = 0; x < SWM_WS_MAX; x++) {
1938 TAILQ_FOREACH(win, &ws->winlist, entry)
1946 validate_ws(struct workspace *testws)
1948 struct swm_region *r;
1949 struct workspace *ws;
1952 /* validate all ws */
1953 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1954 TAILQ_FOREACH(r, &screens[i].rl, entry)
1955 for (x = 0; x < SWM_WS_MAX; x++) {
1964 unfocus_win(struct ws_win *win)
1969 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1973 if (win->ws == NULL)
1976 if (validate_ws(win->ws))
1977 return; /* XXX this gets hit with thunderbird, needs fixing */
1979 if (win->ws->r == NULL)
1982 if (validate_win(win)) {
1987 if (win->ws->focus == win) {
1988 win->ws->focus = NULL;
1989 win->ws->focus_prev = win;
1992 if (validate_win(win->ws->focus)) {
1993 kill_refs(win->ws->focus);
1994 win->ws->focus = NULL;
1996 if (validate_win(win->ws->focus_prev)) {
1997 kill_refs(win->ws->focus_prev);
1998 win->ws->focus_prev = NULL;
2001 /* drain all previous unfocus events */
2002 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2005 grabbuttons(win, 0);
2006 XSetWindowBorder(display, win->id,
2007 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2009 XChangeProperty(display, win->s->root,
2010 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2011 PropModeReplace, (unsigned char *)&none,1);
2020 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2022 for (i = 0; i < ScreenCount(display); i++)
2023 for (j = 0; j < SWM_WS_MAX; j++)
2024 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2029 focus_win(struct ws_win *win)
2034 struct ws_win *cfw = NULL;
2037 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
2041 if (win->ws == NULL)
2044 if (validate_ws(win->ws))
2045 return; /* XXX this gets hit with thunderbird, needs fixing */
2047 if (validate_win(win)) {
2052 if (validate_win(win)) {
2057 XGetInputFocus(display, &cur_focus, &rr);
2058 if ((cfw = find_window(cur_focus)) != NULL)
2061 /* use larger hammer since the window was killed somehow */
2062 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2063 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2064 XSetWindowBorder(display, cfw->id,
2065 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2068 win->ws->focus = win;
2070 if (win->ws->r != NULL) {
2071 /* drain all previous focus events */
2072 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2076 XSetInputFocus(display, win->id,
2077 RevertToParent, CurrentTime);
2078 grabbuttons(win, 1);
2079 XSetWindowBorder(display, win->id,
2080 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2081 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2082 win->ws->always_raise)
2083 XMapRaised(display, win->id);
2085 XChangeProperty(display, win->s->root,
2086 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2087 PropModeReplace, (unsigned char *)&win->id,1);
2090 if (window_name_enabled)
2095 switchws(struct swm_region *r, union arg *args)
2097 int wsid = args->id, unmap_old = 0;
2098 struct swm_region *this_r, *other_r;
2100 struct workspace *new_ws, *old_ws;
2107 old_ws = this_r->ws;
2108 new_ws = &this_r->s->ws[wsid];
2110 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
2111 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
2114 if (new_ws == NULL || old_ws == NULL)
2116 if (new_ws == old_ws)
2119 other_r = new_ws->r;
2120 if (other_r == NULL) {
2121 /* the other workspace is hidden, hide this one */
2125 /* the other ws is visible in another region, exchange them */
2126 other_r->ws_prior = new_ws;
2127 other_r->ws = old_ws;
2128 old_ws->r = other_r;
2130 this_r->ws_prior = old_ws;
2131 this_r->ws = new_ws;
2134 /* this is needed so that we can click on a window after a restart */
2138 a.id = SWM_ARG_ID_FOCUSCUR;
2139 focus(new_ws->r, &a);
2143 /* unmap old windows */
2145 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2148 if (focus_mode == SWM_FOCUS_DEFAULT)
2149 drain_enter_notify();
2153 cyclews(struct swm_region *r, union arg *args)
2156 struct swm_screen *s = r->s;
2159 DNPRINTF(SWM_D_WS, "cyclews id %d "
2160 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2161 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2166 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2169 case SWM_ARG_ID_CYCLEWS_UP:
2170 if (a.id < SWM_WS_MAX - 1)
2175 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2178 case SWM_ARG_ID_CYCLEWS_DOWN:
2182 a.id = SWM_WS_MAX - 1;
2189 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2191 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2195 } while (a.id != r->ws->idx);
2199 priorws(struct swm_region *r, union arg *args)
2203 DNPRINTF(SWM_D_WS, "priorws id %d "
2204 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2205 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2207 if (r->ws_prior == NULL)
2210 a.id = r->ws_prior->idx;
2215 cyclescr(struct swm_region *r, union arg *args)
2217 struct swm_region *rr = NULL;
2221 /* do nothing if we don't have more than one screen */
2222 if (!(ScreenCount(display) > 1 || outputs > 1))
2227 case SWM_ARG_ID_CYCLESC_UP:
2228 rr = TAILQ_NEXT(r, entry);
2230 rr = TAILQ_FIRST(&screens[i].rl);
2232 case SWM_ARG_ID_CYCLESC_DOWN:
2233 rr = TAILQ_PREV(r, swm_region_list, entry);
2235 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2243 /* move mouse to region */
2245 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
2246 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2248 a.id = SWM_ARG_ID_FOCUSCUR;
2251 if (rr->ws->focus) {
2252 /* move to focus window */
2253 x = rr->ws->focus->g.x + 1;
2254 y = rr->ws->focus->g.y + 1;
2255 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2260 sort_windows(struct ws_win_list *wl)
2262 struct ws_win *win, *parent, *nxt;
2267 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2268 nxt = TAILQ_NEXT(win, entry);
2269 if (win->transient) {
2270 parent = find_window(win->transient);
2271 if (parent == NULL) {
2272 fprintf(stderr, "not possible bug\n");
2275 TAILQ_REMOVE(wl, win, entry);
2276 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2283 swapwin(struct swm_region *r, union arg *args)
2285 struct ws_win *target, *source;
2286 struct ws_win *cur_focus;
2287 struct ws_win_list *wl;
2290 DNPRINTF(SWM_D_WS, "swapwin id %d "
2291 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2292 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2294 cur_focus = r->ws->focus;
2295 if (cur_focus == NULL)
2299 wl = &source->ws->winlist;
2302 case SWM_ARG_ID_SWAPPREV:
2303 if (source->transient)
2304 source = find_window(source->transient);
2305 target = TAILQ_PREV(source, ws_win_list, entry);
2306 if (target && target->transient)
2307 target = find_window(target->transient);
2308 TAILQ_REMOVE(wl, source, entry);
2310 TAILQ_INSERT_TAIL(wl, source, entry);
2312 TAILQ_INSERT_BEFORE(target, source, entry);
2314 case SWM_ARG_ID_SWAPNEXT:
2315 target = TAILQ_NEXT(source, entry);
2316 /* move the parent and let the sort handle the move */
2317 if (source->transient)
2318 source = find_window(source->transient);
2319 TAILQ_REMOVE(wl, source, entry);
2321 TAILQ_INSERT_HEAD(wl, source, entry);
2323 TAILQ_INSERT_AFTER(wl, target, source, entry);
2325 case SWM_ARG_ID_SWAPMAIN:
2326 target = TAILQ_FIRST(wl);
2327 if (target == source) {
2328 if (source->ws->focus_prev != NULL &&
2329 source->ws->focus_prev != target)
2331 source = source->ws->focus_prev;
2335 if (target == NULL || source == NULL)
2337 source->ws->focus_prev = target;
2338 TAILQ_REMOVE(wl, target, entry);
2339 TAILQ_INSERT_BEFORE(source, target, entry);
2340 TAILQ_REMOVE(wl, source, entry);
2341 TAILQ_INSERT_HEAD(wl, source, entry);
2343 case SWM_ARG_ID_MOVELAST:
2344 TAILQ_REMOVE(wl, source, entry);
2345 TAILQ_INSERT_TAIL(wl, source, entry);
2348 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2358 focus_prev(struct ws_win *win)
2360 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2361 struct ws_win *cur_focus = NULL;
2362 struct ws_win_list *wl = NULL;
2363 struct workspace *ws = NULL;
2365 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2367 if (!(win && win->ws))
2372 cur_focus = ws->focus;
2373 winlostfocus = cur_focus;
2375 /* pickle, just focus on whatever */
2376 if (cur_focus == NULL) {
2377 /* use prev_focus if valid */
2378 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2379 find_window(WINID(ws->focus_prev)))
2380 winfocus = ws->focus_prev;
2381 if (winfocus == NULL)
2382 winfocus = TAILQ_FIRST(wl);
2386 /* if transient focus on parent */
2387 if (cur_focus->transient) {
2388 winfocus = find_window(cur_focus->transient);
2392 /* if in max_stack try harder */
2393 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2394 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2395 if (cur_focus != ws->focus_prev)
2396 winfocus = ws->focus_prev;
2397 else if (cur_focus != ws->focus)
2398 winfocus = ws->focus;
2400 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2405 if (cur_focus == win)
2406 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2407 if (winfocus == NULL)
2408 winfocus = TAILQ_LAST(wl, ws_win_list);
2409 if (winfocus == NULL || winfocus == win)
2410 winfocus = TAILQ_NEXT(cur_focus, entry);
2412 if (winfocus == winlostfocus || winfocus == NULL)
2415 focus_magic(winfocus);
2419 focus(struct swm_region *r, union arg *args)
2421 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2422 struct ws_win *cur_focus = NULL;
2423 struct ws_win_list *wl = NULL;
2424 struct workspace *ws = NULL;
2429 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2431 /* treat FOCUS_CUR special */
2432 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2433 if (r->ws->focus && r->ws->focus->iconic == 0)
2434 winfocus = r->ws->focus;
2435 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2436 winfocus = r->ws->focus_prev;
2438 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2439 if (winfocus->iconic == 0)
2442 focus_magic(winfocus);
2446 if ((cur_focus = r->ws->focus) == NULL)
2451 winlostfocus = cur_focus;
2454 case SWM_ARG_ID_FOCUSPREV:
2455 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2457 head = TAILQ_LAST(wl, ws_win_list);
2459 if (WINID(winfocus) == cur_focus->transient) {
2460 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2462 head = TAILQ_LAST(wl, ws_win_list);
2467 if (winfocus && winfocus->iconic) {
2468 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
2469 if (winfocus->iconic == 0)
2474 case SWM_ARG_ID_FOCUSNEXT:
2475 head = TAILQ_NEXT(cur_focus, entry);
2477 head = TAILQ_FIRST(wl);
2481 if (winfocus && winfocus->iconic) {
2482 TAILQ_FOREACH(winfocus, wl, entry)
2483 if (winfocus->iconic == 0)
2488 case SWM_ARG_ID_FOCUSMAIN:
2489 winfocus = TAILQ_FIRST(wl);
2490 if (winfocus == cur_focus)
2491 winfocus = cur_focus->ws->focus_prev;
2497 if (winfocus == winlostfocus || winfocus == NULL)
2500 focus_magic(winfocus);
2504 cycle_layout(struct swm_region *r, union arg *args)
2506 struct workspace *ws = r->ws;
2507 struct ws_win *winfocus;
2510 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2512 winfocus = ws->focus;
2515 if (ws->cur_layout->l_stack == NULL)
2516 ws->cur_layout = &layouts[0];
2519 if (focus_mode == SWM_FOCUS_DEFAULT)
2520 drain_enter_notify();
2521 a.id = SWM_ARG_ID_FOCUSCUR;
2527 stack_config(struct swm_region *r, union arg *args)
2529 struct workspace *ws = r->ws;
2531 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2534 if (ws->cur_layout->l_config != NULL)
2535 ws->cur_layout->l_config(ws, args->id);
2537 if (args->id != SWM_ARG_ID_STACKINIT)
2544 struct swm_geometry g;
2545 struct swm_region *r;
2548 DNPRINTF(SWM_D_STACK, "stack\n");
2550 for (i = 0; i < ScreenCount(display); i++) {
2552 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2553 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2554 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2556 /* start with screen geometry, adjust for bar */
2558 g.w -= 2 * border_width;
2559 g.h -= 2 * border_width;
2565 r->ws->cur_layout->l_stack(r->ws, &g);
2566 r->ws->cur_layout->l_string(r->ws);
2567 /* save r so we can track region changes */
2574 if (focus_mode == SWM_FOCUS_DEFAULT)
2575 drain_enter_notify();
2579 store_float_geom(struct ws_win *win, struct swm_region *r)
2581 /* retain window geom and region geom */
2582 win->g_float.x = win->g.x;
2583 win->g_float.y = win->g.y;
2584 win->g_float.w = win->g.w;
2585 win->g_float.h = win->g.h;
2586 win->rg_float.x = r->g.x;
2587 win->rg_float.y = r->g.y;
2588 win->rg_float.w = r->g.w;
2589 win->rg_float.h = r->g.h;
2590 win->g_floatvalid = 1;
2594 stack_floater(struct ws_win *win, struct swm_region *r)
2602 bzero(&wc, sizeof wc);
2603 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2606 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2607 * geom on ws switches or return from max mode
2610 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2611 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2613 * use stored g and rg to set relative position and size
2614 * as in old region or before max stack mode
2616 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2617 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2618 win->g.w = win->g_float.w;
2619 win->g.h = win->g_float.h;
2620 win->g_floatvalid = 0;
2623 win->floatmaxed = 0;
2625 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2626 (win->g.h >= HEIGHT(r)))
2627 wc.border_width = 0;
2629 wc.border_width = border_width;
2630 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2631 win->g.w = (double)WIDTH(r) * dialog_ratio;
2632 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2637 * floaters and transients are auto-centred unless moved
2640 win->g.x = r->g.x + (WIDTH(r) - win->g.w) /
2641 2 - wc.border_width;
2642 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) /
2643 2 - wc.border_width;
2646 /* win can be outside r if new r smaller than old r */
2647 /* Ensure top left corner inside r (move probs otherwise) */
2648 if (win->g.x < r->g.x - wc.border_width)
2649 win->g.x = r->g.x - wc.border_width;
2650 if (win->g.x > r->g.x + r->g.w - 1)
2651 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2652 (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
2653 if (win->g.y < r->g.y - wc.border_width)
2654 win->g.y = r->g.y - wc.border_width;
2655 if (win->g.y > r->g.y + r->g.h - 1)
2656 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2657 (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
2661 wc.width = win->g.w;
2662 wc.height = win->g.h;
2665 * Retain floater and transient geometry for correct positioning
2666 * when ws changes region
2668 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2669 store_float_geom(win, r);
2671 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2672 win->id, wc.x, wc.y, wc.width, wc.height);
2674 XConfigureWindow(display, win->id, mask, &wc);
2678 * Send keystrokes to terminal to decrease/increase the font size as the
2679 * window size changes.
2682 adjust_font(struct ws_win *win)
2684 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2685 win->floating || win->transient)
2688 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2689 win->g.w / win->sh.width_inc < term_width &&
2690 win->font_steps < SWM_MAX_FONT_STEPS) {
2691 win->font_size_boundary[win->font_steps] =
2692 (win->sh.width_inc * term_width) + win->sh.base_width;
2695 win->last_inc = win->sh.width_inc;
2696 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2697 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2698 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2701 win->last_inc = win->sh.width_inc;
2702 fake_keypress(win, XK_KP_Add, ShiftMask);
2706 #define SWAPXY(g) do { \
2708 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2709 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2712 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2715 XWindowAttributes wa;
2716 struct swm_geometry win_g, r_g = *g;
2717 struct ws_win *win, *fs_win = 0;
2718 int i, j, s, stacks;
2719 int w_inc = 1, h_inc, w_base = 1, h_base;
2720 int hrh, extra = 0, h_slice, last_h = 0;
2721 int split, colno, winno, mwin, msize, mscale;
2722 int remain, missing, v_slice, reconfigure;
2725 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2726 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2728 winno = count_win(ws, 0);
2729 if (winno == 0 && count_win(ws, 1) == 0)
2732 TAILQ_FOREACH(win, &ws->winlist, entry)
2733 if (win->transient == 0 && win->floating == 0
2734 && win->iconic == 0)
2741 w_inc = win->sh.width_inc;
2742 w_base = win->sh.base_width;
2743 mwin = ws->l_state.horizontal_mwin;
2744 mscale = ws->l_state.horizontal_msize;
2745 stacks = ws->l_state.horizontal_stacks;
2748 w_inc = win->sh.height_inc;
2749 w_base = win->sh.base_height;
2750 mwin = ws->l_state.vertical_mwin;
2751 mscale = ws->l_state.vertical_msize;
2752 stacks = ws->l_state.vertical_stacks;
2756 if (stacks > winno - mwin)
2757 stacks = winno - mwin;
2761 h_slice = r_g.h / SWM_H_SLICE;
2762 if (mwin && winno > mwin) {
2763 v_slice = r_g.w / SWM_V_SLICE;
2767 win_g.w = v_slice * mscale;
2769 if (w_inc > 1 && w_inc < v_slice) {
2770 /* adjust for window's requested size increment */
2771 remain = (win_g.w - w_base) % w_inc;
2772 missing = w_inc - remain;
2779 win_g.x += r_g.w - msize;
2782 colno = split = winno / stacks;
2783 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2784 2 * border_width) / stacks);
2786 hrh = r_g.h / colno;
2787 extra = r_g.h - (colno * hrh);
2788 win_g.h = hrh - 2 * border_width;
2790 /* stack all the tiled windows */
2791 i = j = 0, s = stacks;
2792 TAILQ_FOREACH(win, &ws->winlist, entry) {
2793 if (win->transient != 0 || win->floating != 0)
2795 if (win->iconic != 0)
2798 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2803 if (split && i == split) {
2804 colno = (winno - mwin) / stacks;
2805 if (s <= (winno - mwin) % stacks)
2807 split = split + colno;
2808 hrh = (r_g.h / colno);
2809 extra = r_g.h - (colno * hrh);
2813 win_g.x += win_g.w + 2 * border_width;
2814 win_g.w = (r_g.w - msize -
2815 (stacks * 2 * border_width)) / stacks;
2817 win_g.w += (r_g.w - msize -
2818 (stacks * 2 * border_width)) % stacks;
2822 win_g.h = hrh - 2 * border_width;
2824 h_inc = win->sh.width_inc;
2825 h_base = win->sh.base_width;
2827 h_inc = win->sh.height_inc;
2828 h_base = win->sh.base_height;
2830 if (j == colno - 1) {
2831 win_g.h = hrh + extra;
2832 } else if (h_inc > 1 && h_inc < h_slice) {
2833 /* adjust for window's requested size increment */
2834 remain = (win_g.h - h_base) % h_inc;
2835 missing = h_inc - remain;
2837 if (missing <= extra || j == 0) {
2849 win_g.y += last_h + 2 * border_width;
2851 bzero(&wc, sizeof wc);
2852 if (disable_border && bar_enabled == 0 && winno == 1){
2853 wc.border_width = 0;
2854 win_g.w += 2 * border_width;
2855 win_g.h += 2 * border_width;
2857 wc.border_width = border_width;
2860 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2861 win->g.w != win_g.h || win->g.h != win_g.w) {
2863 win->g.x = wc.x = win_g.y;
2864 win->g.y = wc.y = win_g.x;
2865 win->g.w = wc.width = win_g.h;
2866 win->g.h = wc.height = win_g.w;
2869 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2870 win->g.w != win_g.w || win->g.h != win_g.h) {
2872 win->g.x = wc.x = win_g.x;
2873 win->g.y = wc.y = win_g.y;
2874 win->g.w = wc.width = win_g.w;
2875 win->g.h = wc.height = win_g.h;
2880 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2881 XConfigureWindow(display, win->id, mask, &wc);
2884 if (XGetWindowAttributes(display, win->id, &wa))
2885 if (wa.map_state == IsUnmapped)
2886 XMapRaised(display, win->id);
2894 /* now, stack all the floaters and transients */
2895 TAILQ_FOREACH(win, &ws->winlist, entry) {
2896 if (win->transient == 0 && win->floating == 0)
2898 if (win->iconic == 1)
2900 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2905 stack_floater(win, ws->r);
2906 XMapRaised(display, win->id);
2910 stack_floater(fs_win, ws->r);
2911 XMapRaised(display, fs_win->id);
2916 vertical_config(struct workspace *ws, int id)
2918 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2921 case SWM_ARG_ID_STACKRESET:
2922 case SWM_ARG_ID_STACKINIT:
2923 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2924 ws->l_state.vertical_mwin = 1;
2925 ws->l_state.vertical_stacks = 1;
2927 case SWM_ARG_ID_MASTERSHRINK:
2928 if (ws->l_state.vertical_msize > 1)
2929 ws->l_state.vertical_msize--;
2931 case SWM_ARG_ID_MASTERGROW:
2932 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2933 ws->l_state.vertical_msize++;
2935 case SWM_ARG_ID_MASTERADD:
2936 ws->l_state.vertical_mwin++;
2938 case SWM_ARG_ID_MASTERDEL:
2939 if (ws->l_state.vertical_mwin > 0)
2940 ws->l_state.vertical_mwin--;
2942 case SWM_ARG_ID_STACKINC:
2943 ws->l_state.vertical_stacks++;
2945 case SWM_ARG_ID_STACKDEC:
2946 if (ws->l_state.vertical_stacks > 1)
2947 ws->l_state.vertical_stacks--;
2955 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2957 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2959 stack_master(ws, g, 0, 0);
2963 horizontal_config(struct workspace *ws, int id)
2965 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2968 case SWM_ARG_ID_STACKRESET:
2969 case SWM_ARG_ID_STACKINIT:
2970 ws->l_state.horizontal_mwin = 1;
2971 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2972 ws->l_state.horizontal_stacks = 1;
2974 case SWM_ARG_ID_MASTERSHRINK:
2975 if (ws->l_state.horizontal_msize > 1)
2976 ws->l_state.horizontal_msize--;
2978 case SWM_ARG_ID_MASTERGROW:
2979 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2980 ws->l_state.horizontal_msize++;
2982 case SWM_ARG_ID_MASTERADD:
2983 ws->l_state.horizontal_mwin++;
2985 case SWM_ARG_ID_MASTERDEL:
2986 if (ws->l_state.horizontal_mwin > 0)
2987 ws->l_state.horizontal_mwin--;
2989 case SWM_ARG_ID_STACKINC:
2990 ws->l_state.horizontal_stacks++;
2992 case SWM_ARG_ID_STACKDEC:
2993 if (ws->l_state.horizontal_stacks > 1)
2994 ws->l_state.horizontal_stacks--;
3002 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3004 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3006 stack_master(ws, g, 1, 0);
3009 /* fullscreen view */
3011 max_stack(struct workspace *ws, struct swm_geometry *g)
3014 struct swm_geometry gg = *g;
3015 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3019 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3024 winno = count_win(ws, 0);
3025 if (winno == 0 && count_win(ws, 1) == 0)
3028 TAILQ_FOREACH(win, &ws->winlist, entry) {
3029 if (win->transient) {
3031 parent = find_window(win->transient);
3035 if (win->floating && win->floatmaxed == 0 ) {
3037 * retain geometry for retrieval on exit from
3040 store_float_geom(win, ws->r);
3041 win->floatmaxed = 1;
3044 /* only reconfigure if necessary */
3045 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
3047 bzero(&wc, sizeof wc);
3048 win->g.x = wc.x = gg.x;
3049 win->g.y = wc.y = gg.y;
3051 wc.border_width = border_width;
3052 win->g.w = wc.width = gg.w;
3053 win->g.h = wc.height = gg.h;
3055 wc.border_width = 0;
3056 win->g.w = wc.width = gg.w + 2 * border_width;
3057 win->g.h = wc.height = gg.h + 2 * border_width;
3059 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3060 XConfigureWindow(display, win->id, mask, &wc);
3062 /* unmap only if we don't have multi screen */
3063 if (win != ws->focus)
3064 if (!(ScreenCount(display) > 1 || outputs > 1))
3068 /* put the last transient on top */
3071 XMapRaised(display, parent->id);
3072 stack_floater(wintrans, ws->r);
3073 focus_magic(wintrans);
3078 send_to_ws(struct swm_region *r, union arg *args)
3080 int wsid = args->id;
3081 struct ws_win *win = NULL, *parent;
3082 struct workspace *ws, *nws;
3083 Atom ws_idx_atom = 0;
3084 unsigned char ws_idx_str[SWM_PROPLEN];
3093 if (win->ws->idx == wsid)
3096 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
3099 nws = &win->s->ws[wsid];
3101 a.id = SWM_ARG_ID_FOCUSPREV;
3103 if (win->transient) {
3104 parent = find_window(win->transient);
3106 unmap_window(parent);
3107 TAILQ_REMOVE(&ws->winlist, parent, entry);
3108 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3113 TAILQ_REMOVE(&ws->winlist, win, entry);
3114 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3117 /* Try to update the window's workspace property */
3118 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3120 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3122 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3124 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3125 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3132 pressbutton(struct swm_region *r, union arg *args)
3134 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3135 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3139 raise_toggle(struct swm_region *r, union arg *args)
3141 if (r && r->ws == NULL)
3144 r->ws->always_raise = !r->ws->always_raise;
3146 /* bring floaters back to top */
3147 if (r->ws->always_raise == 0)
3152 iconify(struct swm_region *r, union arg *args)
3156 if (r->ws->focus == NULL)
3158 unmap_window(r->ws->focus);
3159 update_iconic(r->ws->focus, 1);
3161 if (focus_mode == SWM_FOCUS_DEFAULT)
3162 drain_enter_notify();
3163 r->ws->focus = NULL;
3164 a.id = SWM_ARG_ID_FOCUSCUR;
3169 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3170 unsigned long *slen)
3173 unsigned long nitems, nbytes, nextra;
3174 unsigned char *prop = NULL;
3177 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3178 &rettype, &retfmt, &nitems, &nbytes, &prop);
3179 if (status != Success)
3183 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3184 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3185 if (status != Success) {
3189 if (rettype != stype) {
3198 uniconify(struct swm_region *r, union arg *args)
3202 unsigned char *name;
3206 DNPRINTF(SWM_D_MISC, "uniconify\n");
3208 if (r && r->ws == NULL)
3211 /* make sure we have anything to uniconify */
3212 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3213 if (win->ws == NULL)
3214 continue; /* should never happen */
3215 if (win->iconic == 0)
3223 search_resp_action = SWM_SEARCH_UNICONIFY;
3225 spawn_select(r, args, "search", &searchpid);
3227 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3230 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3231 if (win->ws == NULL)
3232 continue; /* should never happen */
3233 if (win->iconic == 0)
3236 name = get_win_name(display, win->id, a_wmname, a_string,
3240 fprintf(lfile, "%s.%lu\n", name, win->id);
3248 search_resp_uniconify(char *resp, unsigned long len)
3250 unsigned char *name;
3254 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp %s\n", resp);
3256 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3257 if (win->iconic == 0)
3259 name = get_win_name(display, win->id, a_wmname, a_string, &len);
3262 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3267 if (strncmp(s, resp, len) == 0) {
3268 /* XXX this should be a callback to generalize */
3269 update_iconic(win, 0);
3277 #define MAX_RESP_LEN 1024
3280 search_do_resp(void)
3286 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3291 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3292 fprintf(stderr, "search: calloc\n");
3296 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3298 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3301 resp[rbytes] = '\0';
3304 switch (search_resp_action) {
3305 case SWM_SEARCH_UNICONIFY:
3306 search_resp_uniconify(resp, len);
3311 search_resp_action = SWM_SEARCH_NONE;
3312 close(select_resp_pipe[0]);
3317 wkill(struct swm_region *r, union arg *args)
3319 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3321 if (r->ws->focus == NULL)
3324 if (args->id == SWM_ARG_ID_KILLWINDOW)
3325 XKillClient(display, r->ws->focus->id);
3327 if (r->ws->focus->can_delete)
3328 client_msg(r->ws->focus, adelete);
3333 floating_toggle_win(struct ws_win *win)
3335 struct swm_region *r;
3345 /* reject floating toggles in max stack mode */
3346 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3349 if (win->floating) {
3350 if (!win->floatmaxed) {
3351 /* retain position for refloat */
3352 store_float_geom(win, r);
3356 if (win->g_floatvalid) {
3357 /* refloat at last floating relative position */
3358 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3359 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3360 win->g.w = win->g_float.w;
3361 win->g.h = win->g_float.h;
3366 ewmh_update_actions(win);
3372 floating_toggle(struct swm_region *r, union arg *args)
3374 struct ws_win *win = r->ws->focus;
3380 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3381 _NET_WM_STATE_TOGGLE);
3384 if (focus_mode == SWM_FOCUS_DEFAULT)
3385 drain_enter_notify();
3387 if (win == win->ws->focus) {
3388 a.id = SWM_ARG_ID_FOCUSCUR;
3389 focus(win->ws->r, &a);
3394 resize_window(struct ws_win *win, int center)
3398 struct swm_region *r;
3400 r = root_to_region(win->wa.root);
3401 bzero(&wc, sizeof wc);
3402 mask = CWBorderWidth | CWWidth | CWHeight;
3403 wc.border_width = border_width;
3404 wc.width = win->g.w;
3405 wc.height = win->g.h;
3406 if (center == SWM_ARG_ID_CENTER) {
3407 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
3408 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
3412 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
3413 win->id, wc.x, wc.y, wc.width, wc.height);
3415 XConfigureWindow(display, win->id, mask, &wc);
3418 #define SWM_RESIZE_STEPS (50)
3421 resize(struct ws_win *win, union arg *args)
3425 struct swm_region *r = NULL;
3427 int resize_step = 0;
3433 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3434 win->id, win->floating, win->transient);
3436 if (!(win->transient != 0 || win->floating != 0))
3439 /* reject resizes in max mode for floaters (transient ok) */
3440 if (win->floatmaxed)
3444 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3450 case SWM_ARG_ID_WIDTHSHRINK:
3451 win->g.w -= SWM_RESIZE_STEPS;
3454 case SWM_ARG_ID_WIDTHGROW:
3455 win->g.w += SWM_RESIZE_STEPS;
3458 case SWM_ARG_ID_HEIGHTSHRINK:
3459 win->g.h -= SWM_RESIZE_STEPS;
3462 case SWM_ARG_ID_HEIGHTGROW:
3463 win->g.h += SWM_RESIZE_STEPS;
3470 resize_window(win, 0);
3471 store_float_geom(win,r);
3475 if (focus_mode == SWM_FOCUS_DEFAULT)
3476 drain_enter_notify();
3478 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3479 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3482 /* place pointer at bottom left corner or nearest point inside r */
3483 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
3484 relx = win->g.w - 1;
3486 relx = r->g.x + r->g.w - win->g.x - 1;
3488 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
3489 rely = win->g.h - 1;
3491 rely = r->g.y + r->g.h - win->g.y - 1;
3493 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
3495 XMaskEvent(display, MOUSEMASK | ExposureMask |
3496 SubstructureRedirectMask, &ev);
3498 case ConfigureRequest:
3501 handler[ev.type](&ev);
3504 /* do not allow resize outside of region */
3505 if ( ev.xmotion.x_root < r->g.x ||
3506 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3507 ev.xmotion.y_root < r->g.y ||
3508 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3511 if (ev.xmotion.x <= 1)
3513 if (ev.xmotion.y <= 1)
3515 win->g.w = ev.xmotion.x + 1;
3516 win->g.h = ev.xmotion.y + 1;
3518 /* not free, don't sync more than 120 times / second */
3519 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3520 time = ev.xmotion.time;
3521 XSync(display, False);
3522 resize_window(win, args->id);
3526 } while (ev.type != ButtonRelease);
3528 XSync(display, False);
3529 resize_window(win, args->id);
3531 store_float_geom(win,r);
3533 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
3535 XUngrabPointer(display, CurrentTime);
3538 drain_enter_notify();
3542 resize_step(struct swm_region *r, union arg *args)
3544 struct ws_win *win = NULL;
3546 if (r && r->ws && r->ws->focus)
3556 move_window(struct ws_win *win)
3560 struct swm_region *r;
3562 r = root_to_region(win->wa.root);
3563 bzero(&wc, sizeof wc);
3567 wc.border_width = border_width;
3569 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3570 win->id, wc.x, wc.y, wc.width, wc.height);
3572 XConfigureWindow(display, win->id, mask, &wc);
3575 #define SWM_MOVE_STEPS (50)
3578 move(struct ws_win *win, union arg *args)
3583 struct swm_region *r = NULL;
3589 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3590 win->id, win->floating, win->transient);
3592 /* in max_stack mode should only move transients */
3593 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3597 if (win->floating == 0 && !win->transient) {
3598 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3601 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3608 case SWM_ARG_ID_MOVELEFT:
3609 win->g.x -= (SWM_MOVE_STEPS - border_width);
3612 case SWM_ARG_ID_MOVERIGHT:
3613 win->g.x += (SWM_MOVE_STEPS - border_width);
3616 case SWM_ARG_ID_MOVEUP:
3617 win->g.y -= (SWM_MOVE_STEPS - border_width);
3620 case SWM_ARG_ID_MOVEDOWN:
3621 win->g.y += (SWM_MOVE_STEPS - border_width);
3629 store_float_geom(win,r);
3634 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3635 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3637 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3639 XMaskEvent(display, MOUSEMASK | ExposureMask |
3640 SubstructureRedirectMask, &ev);
3642 case ConfigureRequest:
3645 handler[ev.type](&ev);
3648 /* don't allow to move window origin out of region */
3649 if ( ev.xmotion.x_root < r->g.x ||
3650 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3651 ev.xmotion.y_root < r->g.y ||
3652 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3655 win->g.x = ev.xmotion.x_root - border_width;
3656 win->g.y = ev.xmotion.y_root - border_width;
3658 /* not free, don't sync more than 120 times / second */
3659 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3660 time = ev.xmotion.time;
3661 XSync(display, False);
3666 } while (ev.type != ButtonRelease);
3668 XSync(display, False);
3671 store_float_geom(win,r);
3672 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3673 XUngrabPointer(display, CurrentTime);
3676 drain_enter_notify();
3680 move_step(struct swm_region *r, union arg *args)
3682 struct ws_win *win = NULL;
3684 if (r && r->ws && r->ws->focus)
3689 if (!(win->transient != 0 || win->floating != 0))
3696 /* user/key callable function IDs */
3765 kf_dumpwins, /* MUST BE LAST */
3769 /* key definitions */
3771 dummykeyfunc(struct swm_region *r, union arg *args)
3776 legacyfunc(struct swm_region *r, union arg *args)
3781 char name[SWM_FUNCNAME_LEN];
3782 void (*func)(struct swm_region *r, union arg *);
3784 } keyfuncs[kf_invalid + 1] = {
3785 /* name function argument */
3786 { "cycle_layout", cycle_layout, {0} },
3787 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3788 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3789 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3790 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3791 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3792 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3793 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3794 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3795 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3796 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3797 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3798 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3799 { "spawn_term", spawnterm, {.argv = spawn_term} },
3800 { "spawn_menu", legacyfunc, {0} },
3801 { "quit", quit, {0} },
3802 { "restart", restart, {0} },
3803 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3804 { "ws_1", switchws, {.id = 0} },
3805 { "ws_2", switchws, {.id = 1} },
3806 { "ws_3", switchws, {.id = 2} },
3807 { "ws_4", switchws, {.id = 3} },
3808 { "ws_5", switchws, {.id = 4} },
3809 { "ws_6", switchws, {.id = 5} },
3810 { "ws_7", switchws, {.id = 6} },
3811 { "ws_8", switchws, {.id = 7} },
3812 { "ws_9", switchws, {.id = 8} },
3813 { "ws_10", switchws, {.id = 9} },
3814 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3815 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3816 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
3817 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
3818 { "ws_prior", priorws, {0} },
3819 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3820 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3821 { "mvws_1", send_to_ws, {.id = 0} },
3822 { "mvws_2", send_to_ws, {.id = 1} },
3823 { "mvws_3", send_to_ws, {.id = 2} },
3824 { "mvws_4", send_to_ws, {.id = 3} },
3825 { "mvws_5", send_to_ws, {.id = 4} },
3826 { "mvws_6", send_to_ws, {.id = 5} },
3827 { "mvws_7", send_to_ws, {.id = 6} },
3828 { "mvws_8", send_to_ws, {.id = 7} },
3829 { "mvws_9", send_to_ws, {.id = 8} },
3830 { "mvws_10", send_to_ws, {.id = 9} },
3831 { "bar_toggle", bar_toggle, {0} },
3832 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3833 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3834 { "screenshot_all", legacyfunc, {0} },
3835 { "screenshot_wind", legacyfunc, {0} },
3836 { "float_toggle", floating_toggle,{0} },
3837 { "version", version, {0} },
3838 { "spawn_lock", legacyfunc, {0} },
3839 { "spawn_initscr", legacyfunc, {0} },
3840 { "spawn_custom", dummykeyfunc, {0} },
3841 { "iconify", iconify, {0} },
3842 { "uniconify", uniconify, {0} },
3843 { "raise_toggle", raise_toggle, {0} },
3844 { "button2", pressbutton, {2} },
3845 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
3846 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
3847 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
3848 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
3849 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
3850 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
3851 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
3852 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
3853 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
3854 { "invalid key func", NULL, {0} },
3859 enum keyfuncid funcid;
3862 int keys_size = 0, keys_length = 0;
3863 struct key *keys = NULL;
3866 enum { client_click, root_click };
3868 unsigned int action;
3870 unsigned int button;
3871 void (*func)(struct ws_win *, union arg *);
3874 /* action key mouse button func args */
3875 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3876 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3877 { client_click, MODKEY, Button1, move, {0} },
3881 update_modkey(unsigned int mod)
3886 for (i = 0; i < keys_length; i++)
3887 if (keys[i].mod & ShiftMask)
3888 keys[i].mod = mod | ShiftMask;
3892 for (i = 0; i < LENGTH(buttons); i++)
3893 if (buttons[i].mask & ShiftMask)
3894 buttons[i].mask = mod | ShiftMask;
3896 buttons[i].mask = mod;
3906 int spawns_size = 0, spawns_length = 0;
3907 struct spawn_prog *spawns = NULL;
3910 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
3913 struct spawn_prog *prog = NULL;
3915 char *ap, **real_args;
3917 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
3920 for (i = 0; i < spawns_length; i++) {
3921 if (!strcasecmp(spawn_name, spawns[i].name))
3925 fprintf(stderr, "spawn_custom: program %s not found\n",
3930 /* make room for expanded args */
3931 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3932 err(1, "spawn_custom: calloc real_args");
3934 /* expand spawn_args into real_args */
3935 for (i = 0; i < prog->argc; i++) {
3937 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3938 if (!strcasecmp(ap, "$bar_border")) {
3940 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3942 err(1, "spawn_custom border color");
3943 } else if (!strcasecmp(ap, "$bar_color")) {
3945 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3947 err(1, "spawn_custom bar color");
3948 } else if (!strcasecmp(ap, "$bar_font")) {
3949 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3951 err(1, "spawn_custom bar fonts");
3952 } else if (!strcasecmp(ap, "$bar_font_color")) {
3954 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3956 err(1, "spawn_custom color font");
3957 } else if (!strcasecmp(ap, "$color_focus")) {
3959 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3961 err(1, "spawn_custom color focus");
3962 } else if (!strcasecmp(ap, "$color_unfocus")) {
3964 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3966 err(1, "spawn_custom color unfocus");
3968 /* no match --> copy as is */
3969 if ((real_args[i] = strdup(ap)) == NULL)
3970 err(1, "spawn_custom strdup(ap)");
3972 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3977 if ((swm_debug & SWM_D_SPAWN) != 0) {
3978 fprintf(stderr, "spawn_custom: result = ");
3979 for (i = 0; i < prog->argc; i++)
3980 fprintf(stderr, "\"%s\" ", real_args[i]);
3981 fprintf(stderr, "\n");
3984 *ret_args = real_args;
3985 return (prog->argc);
3989 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3995 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3999 spawn(r->ws->idx, &a, 1);
4001 for (i = 0; i < spawn_argc; i++)
4007 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4013 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4017 if (pipe(select_list_pipe) == -1)
4018 err(1, "pipe error");
4019 if (pipe(select_resp_pipe) == -1)
4020 err(1, "pipe error");
4022 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4023 err(1, "could not disable SIGPIPE");
4024 switch (*pid = fork()) {
4026 err(1, "cannot fork");
4029 if (dup2(select_list_pipe[0], 0) == -1)
4031 if (dup2(select_resp_pipe[1], 1) == -1)
4033 close(select_list_pipe[1]);
4034 close(select_resp_pipe[0]);
4035 spawn(r->ws->idx, &a, 0);
4037 default: /* parent */
4038 close(select_list_pipe[0]);
4039 close(select_resp_pipe[1]);
4043 for (i = 0; i < spawn_argc; i++)
4049 setspawn(struct spawn_prog *prog)
4053 if (prog == NULL || prog->name == NULL)
4057 for (i = 0; i < spawns_length; i++) {
4058 if (!strcmp(spawns[i].name, prog->name)) {
4060 if (prog->argv == NULL) {
4062 DNPRINTF(SWM_D_SPAWN,
4063 "setspawn: delete #%d %s\n",
4065 free(spawns[i].name);
4066 for (j = 0; j < spawns[i].argc; j++)
4067 free(spawns[i].argv[j]);
4068 free(spawns[i].argv);
4069 j = spawns_length - 1;
4071 spawns[i] = spawns[j];
4076 DNPRINTF(SWM_D_SPAWN,
4077 "setspawn: replace #%d %s\n",
4079 free(spawns[i].name);
4080 for (j = 0; j < spawns[i].argc; j++)
4081 free(spawns[i].argv[j]);
4082 free(spawns[i].argv);
4085 /* found case handled */
4091 if (prog->argv == NULL) {
4093 "error: setspawn: cannot find program %s", prog->name);
4098 /* not found: add */
4099 if (spawns_size == 0 || spawns == NULL) {
4101 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
4102 spawns = malloc((size_t)spawns_size *
4103 sizeof(struct spawn_prog));
4104 if (spawns == NULL) {
4105 fprintf(stderr, "setspawn: malloc failed\n");
4109 } else if (spawns_length == spawns_size) {
4111 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
4112 spawns = realloc(spawns, (size_t)spawns_size *
4113 sizeof(struct spawn_prog));
4114 if (spawns == NULL) {
4115 fprintf(stderr, "setspawn: realloc failed\n");
4121 if (spawns_length < spawns_size) {
4122 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
4123 spawns_length, prog->name);
4124 i = spawns_length++;
4127 fprintf(stderr, "spawns array problem?\n");
4128 if (spawns == NULL) {
4129 fprintf(stderr, "spawns array is NULL!\n");
4137 setconfspawn(char *selector, char *value, int flags)
4139 struct spawn_prog *prog;
4140 char *vp, *cp, *word;
4142 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4143 if ((prog = calloc(1, sizeof *prog)) == NULL)
4144 err(1, "setconfspawn: calloc prog");
4145 prog->name = strdup(selector);
4146 if (prog->name == NULL)
4147 err(1, "setconfspawn prog->name");
4148 if ((cp = vp = strdup(value)) == NULL)
4149 err(1, "setconfspawn: strdup(value) ");
4150 while ((word = strsep(&cp, " \t")) != NULL) {
4151 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
4153 cp += (long)strspn(cp, " \t");
4154 if (strlen(word) > 0) {
4156 if ((prog->argv = realloc(prog->argv,
4157 prog->argc * sizeof(char *))) == NULL)
4158 err(1, "setconfspawn: realloc");
4159 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
4160 err(1, "setconfspawn: strdup");
4167 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4174 setconfspawn("term", "xterm", 0);
4175 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4176 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4177 setconfspawn("lock", "xlock", 0);
4178 setconfspawn("initscr", "initscreen.sh", 0);
4179 setconfspawn("menu", "dmenu_run"
4182 " -nf $bar_font_color"
4184 " -sf $bar_color", 0);
4185 setconfspawn("search", "dmenu"
4189 " -nf $bar_font_color"
4191 " -sf $bar_color", 0);
4195 #define SWM_MODNAME_SIZE 32
4196 #define SWM_KEY_WS "\n+ \t"
4198 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4202 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4203 if (mod == NULL || ks == NULL) {
4204 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4207 if (keystr == NULL || strlen(keystr) == 0) {
4208 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4214 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4215 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4217 cp += (long)strspn(cp, SWM_KEY_WS);
4218 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4220 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4222 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4224 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4226 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4228 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4230 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4231 *mod |= ControlMask;
4233 *ks = XStringToKeysym(name);
4234 XConvertCase(*ks, ks, &uks);
4235 if (ks == NoSymbol) {
4237 "parsekeys: invalid key %s\n",
4243 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4248 strdupsafe(char *str)
4253 return (strdup(str));
4257 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
4261 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
4262 keyfuncs[kfid].name, spawn_name);
4264 for (i = 0; i < keys_length; i++) {
4265 if (keys[i].mod == mod && keys[i].keysym == ks) {
4266 if (kfid == kf_invalid) {
4269 "setkeybinding: delete #%d %s\n",
4270 i, keyfuncs[keys[i].funcid].name);
4271 free(keys[i].spawn_name);
4272 j = keys_length - 1;
4276 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4279 /* found: replace */
4281 "setkeybinding: replace #%d %s %s\n",
4282 i, keyfuncs[keys[i].funcid].name,
4284 free(keys[i].spawn_name);
4286 keys[i].keysym = ks;
4287 keys[i].funcid = kfid;
4288 keys[i].spawn_name = strdupsafe(spawn_name);
4289 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4294 if (kfid == kf_invalid) {
4296 "error: setkeybinding: cannot find mod/key combination");
4297 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4300 /* not found: add */
4301 if (keys_size == 0 || keys == NULL) {
4303 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
4304 keys = malloc((size_t)keys_size * sizeof(struct key));
4306 fprintf(stderr, "malloc failed\n");
4310 } else if (keys_length == keys_size) {
4312 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
4313 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
4315 fprintf(stderr, "realloc failed\n");
4320 if (keys_length < keys_size) {
4322 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
4323 j, keyfuncs[kfid].name, spawn_name);
4325 keys[j].keysym = ks;
4326 keys[j].funcid = kfid;
4327 keys[j].spawn_name = strdupsafe(spawn_name);
4329 fprintf(stderr, "keys array problem?\n");
4331 fprintf(stderr, "keys array problem\n");
4335 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4339 setconfbinding(char *selector, char *value, int flags)
4341 enum keyfuncid kfid;
4345 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4346 if (selector == NULL) {
4347 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4348 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4350 setkeybinding(mod, ks, kfid, NULL);
4355 /* search by key function name */
4356 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4357 if (strncasecmp(selector, keyfuncs[kfid].name,
4358 SWM_FUNCNAME_LEN) == 0) {
4359 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4361 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4362 setkeybinding(mod, ks, kfid, NULL);
4368 /* search by custom spawn name */
4369 for (i = 0; i < spawns_length; i++) {
4370 if (strcasecmp(selector, spawns[i].name) == 0) {
4371 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4373 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4374 setkeybinding(mod, ks, kf_spawn_custom,
4381 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4388 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4389 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4390 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4391 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4392 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4393 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4394 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4395 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4396 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4397 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4398 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4399 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4400 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4401 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4402 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
4403 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4404 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4405 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4406 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4407 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4408 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4409 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4410 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4411 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4412 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4413 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4414 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4415 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4416 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4417 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4418 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
4419 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
4420 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4421 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4422 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4423 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4424 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4425 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4426 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4427 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4428 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4429 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4430 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4431 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4432 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4433 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4434 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4435 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4436 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4437 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4438 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
4439 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
4440 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4441 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4442 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
4443 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
4444 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4445 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4446 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
4447 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
4448 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
4449 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
4450 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
4451 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
4452 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
4453 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
4454 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
4455 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
4457 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4466 /* clear all key bindings, if any */
4467 for (i = 0; i < keys_length; i++)
4468 free(keys[i].spawn_name);
4473 setkeymapping(char *selector, char *value, int flags)
4475 char keymapping_file[PATH_MAX];
4476 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
4477 if (value[0] == '~')
4478 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
4479 pwd->pw_dir, &value[1]);
4481 strlcpy(keymapping_file, value, sizeof keymapping_file);
4483 /* load new key bindings; if it fails, revert to default bindings */
4484 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
4488 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
4493 updatenumlockmask(void)
4496 XModifierKeymap *modmap;
4498 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4500 modmap = XGetModifierMapping(display);
4501 for (i = 0; i < 8; i++)
4502 for (j = 0; j < modmap->max_keypermod; j++)
4503 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4504 == XKeysymToKeycode(display, XK_Num_Lock))
4505 numlockmask = (1 << i);
4507 XFreeModifiermap(modmap);
4513 unsigned int i, j, k;
4515 unsigned int modifiers[] =
4516 { 0, LockMask, numlockmask, numlockmask | LockMask };
4518 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4519 updatenumlockmask();
4521 for (k = 0; k < ScreenCount(display); k++) {
4522 if (TAILQ_EMPTY(&screens[k].rl))
4524 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4525 for (i = 0; i < keys_length; i++) {
4526 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4527 for (j = 0; j < LENGTH(modifiers); j++)
4528 XGrabKey(display, code,
4529 keys[i].mod | modifiers[j],
4530 screens[k].root, True,
4531 GrabModeAsync, GrabModeAsync);
4537 grabbuttons(struct ws_win *win, int focused)
4540 unsigned int modifiers[] =
4541 { 0, LockMask, numlockmask, numlockmask|LockMask };
4543 updatenumlockmask();
4544 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4546 for (i = 0; i < LENGTH(buttons); i++)
4547 if (buttons[i].action == client_click)
4548 for (j = 0; j < LENGTH(modifiers); j++)
4549 XGrabButton(display, buttons[i].button,
4550 buttons[i].mask | modifiers[j],
4551 win->id, False, BUTTONMASK,
4552 GrabModeAsync, GrabModeSync, None,
4555 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4556 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4559 const char *quirkname[] = {
4560 "NONE", /* config string for "no value" */
4569 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4570 #define SWM_Q_WS "\n|+ \t"
4572 parsequirks(char *qstr, unsigned long *quirk)
4582 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4584 cp += (long)strspn(cp, SWM_Q_WS);
4585 for (i = 0; i < LENGTH(quirkname); i++) {
4586 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4587 DNPRINTF(SWM_D_QUIRK,
4588 "parsequirks: %s\n", name);
4593 *quirk |= 1 << (i-1);
4597 if (i >= LENGTH(quirkname)) {
4598 DNPRINTF(SWM_D_QUIRK,
4599 "parsequirks: invalid quirk [%s]\n", name);
4607 setquirk(const char *class, const char *name, const int quirk)
4612 for (i = 0; i < quirks_length; i++) {
4613 if (!strcmp(quirks[i].class, class) &&
4614 !strcmp(quirks[i].name, name)) {
4617 DNPRINTF(SWM_D_QUIRK,
4618 "setquirk: delete #%d %s:%s\n",
4619 i, quirks[i].class, quirks[i].name);
4620 free(quirks[i].class);
4621 free(quirks[i].name);
4622 j = quirks_length - 1;
4624 quirks[i] = quirks[j];
4628 /* found: replace */
4629 DNPRINTF(SWM_D_QUIRK,
4630 "setquirk: replace #%d %s:%s\n",
4631 i, quirks[i].class, quirks[i].name);
4632 free(quirks[i].class);
4633 free(quirks[i].name);
4634 quirks[i].class = strdup(class);
4635 quirks[i].name = strdup(name);
4636 quirks[i].quirk = quirk;
4643 "error: setquirk: cannot find class/name combination");
4646 /* not found: add */
4647 if (quirks_size == 0 || quirks == NULL) {
4649 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
4650 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
4651 if (quirks == NULL) {
4652 fprintf(stderr, "setquirk: malloc failed\n");
4656 } else if (quirks_length == quirks_size) {
4658 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
4659 quirks = realloc(quirks,
4660 (size_t)quirks_size * sizeof(struct quirk));
4661 if (quirks == NULL) {
4662 fprintf(stderr, "setquirk: realloc failed\n");
4667 if (quirks_length < quirks_size) {
4668 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
4669 j = quirks_length++;
4670 quirks[j].class = strdup(class);
4671 quirks[j].name = strdup(name);
4672 quirks[j].quirk = quirk;
4674 fprintf(stderr, "quirks array problem?\n");
4675 if (quirks == NULL) {
4676 fprintf(stderr, "quirks array problem!\n");
4683 setconfquirk(char *selector, char *value, int flags)
4685 char *cp, *class, *name;
4687 unsigned long quirks;
4688 if (selector == NULL)
4690 if ((cp = strchr(selector, ':')) == NULL)
4695 if ((retval = parsequirks(value, &quirks)) == 0)
4696 setquirk(class, name, quirks);
4703 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
4704 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
4705 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
4706 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
4707 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4708 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
4709 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4710 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4711 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4712 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4713 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
4714 setquirk("pcb", "pcb", SWM_Q_FLOAT);
4715 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
4718 /* conf file stuff */
4719 #define SWM_CONF_FILE "scrotwm.conf"
4721 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
4722 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
4723 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
4724 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
4725 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED, SWM_S_URGENT_ENABLED,
4726 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
4727 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
4728 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM,
4729 SWM_S_VERBOSE_LAYOUT, SWM_S_BAR_JUSTIFY
4733 setconfvalue(char *selector, char *value, int flags)
4738 case SWM_S_BAR_DELAY:
4739 bar_delay = atoi(value);
4741 case SWM_S_BAR_ENABLED:
4742 bar_enabled = atoi(value);
4744 case SWM_S_BAR_BORDER_WIDTH:
4745 bar_border_width = atoi(value);
4747 case SWM_S_BAR_AT_BOTTOM:
4748 bar_at_bottom = atoi(value);
4750 case SWM_S_BAR_JUSTIFY:
4751 if (!strcmp(value, "left"))
4752 bar_justify = SWM_BAR_JUSTIFY_LEFT;
4753 else if (!strcmp(value, "center"))
4754 bar_justify = SWM_BAR_JUSTIFY_CENTER;
4755 else if (!strcmp(value, "right"))
4756 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
4758 errx(1, "invalid bar_justify");
4760 case SWM_S_STACK_ENABLED:
4761 stack_enabled = atoi(value);
4763 case SWM_S_CLOCK_ENABLED:
4764 clock_enabled = atoi(value);
4766 case SWM_S_CLOCK_FORMAT:
4767 #ifndef SWM_DENY_CLOCK_FORMAT
4769 if ((clock_format = strdup(value)) == NULL)
4770 err(1, "setconfvalue: clock_format");
4773 case SWM_S_CYCLE_EMPTY:
4774 cycle_empty = atoi(value);
4776 case SWM_S_CYCLE_VISIBLE:
4777 cycle_visible = atoi(value);
4779 case SWM_S_SS_ENABLED:
4780 ss_enabled = atoi(value);
4782 case SWM_S_TERM_WIDTH:
4783 term_width = atoi(value);
4785 case SWM_S_TITLE_CLASS_ENABLED:
4786 title_class_enabled = atoi(value);
4788 case SWM_S_WINDOW_NAME_ENABLED:
4789 window_name_enabled = atoi(value);
4791 case SWM_S_TITLE_NAME_ENABLED:
4792 title_name_enabled = atoi(value);
4794 case SWM_S_URGENT_ENABLED:
4795 urgent_enabled = atoi(value);
4797 case SWM_S_FOCUS_MODE:
4798 if (!strcmp(value, "default"))
4799 focus_mode = SWM_FOCUS_DEFAULT;
4800 else if (!strcmp(value, "follow_cursor"))
4801 focus_mode = SWM_FOCUS_FOLLOW;
4802 else if (!strcmp(value, "synergy"))
4803 focus_mode = SWM_FOCUS_SYNERGY;
4805 err(1, "focus_mode");
4807 case SWM_S_DISABLE_BORDER:
4808 disable_border = atoi(value);
4810 case SWM_S_BORDER_WIDTH:
4811 border_width = atoi(value);
4813 case SWM_S_BAR_FONT:
4815 if ((bar_fonts[0] = strdup(value)) == NULL)
4816 err(1, "setconfvalue: bar_font");
4818 case SWM_S_BAR_ACTION:
4820 if ((bar_argv[0] = strdup(value)) == NULL)
4821 err(1, "setconfvalue: bar_action");
4823 case SWM_S_SPAWN_TERM:
4824 free(spawn_term[0]);
4825 if ((spawn_term[0] = strdup(value)) == NULL)
4826 err(1, "setconfvalue: spawn_term");
4830 case SWM_S_DIALOG_RATIO:
4831 dialog_ratio = atof(value);
4832 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4835 case SWM_S_VERBOSE_LAYOUT:
4836 verbose_layout = atoi(value);
4837 for (i = 0; layouts[i].l_stack != NULL; i++) {
4839 layouts[i].l_string = fancy_stacker;
4841 layouts[i].l_string = plain_stacker;
4851 setconfmodkey(char *selector, char *value, int flags)
4853 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4854 update_modkey(Mod1Mask);
4855 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4856 update_modkey(Mod2Mask);
4857 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4858 update_modkey(Mod3Mask);
4859 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4860 update_modkey(Mod4Mask);
4867 setconfcolor(char *selector, char *value, int flags)
4869 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4874 setconfregion(char *selector, char *value, int flags)
4876 custom_region(value);
4881 setautorun(char *selector, char *value, int flags)
4891 if (getenv("SWM_STARTED"))
4895 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
4896 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'\n");
4898 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4899 errx(1, "autorun: invalid workspace %d\n", ws_id + 1);
4902 * This is a little intricate
4904 * If the pid already exists we simply reuse it because it means it was
4905 * used before AND not claimed by manage_window. We get away with
4906 * altering it in the parent after INSERT because this can not be a race
4909 while ((ap = strsep(&sp, " \t")) != NULL) {
4912 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
4914 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
4915 err(1, "setautorun: realloc");
4916 a.argv[argc - 1] = ap;
4919 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
4920 err(1, "setautorun: realloc");
4921 a.argv[argc] = NULL;
4923 if ((pid = fork()) == 0) {
4924 spawn(ws_id, &a, 1);
4933 p = calloc(1, sizeof *p);
4936 TAILQ_INSERT_TAIL(&pidlist, p, entry);
4946 setlayout(char *selector, char *value, int flags)
4948 int ws_id, i, x, mg, ma, si, raise;
4949 int st = SWM_V_STACK;
4951 struct workspace *ws;
4953 if (getenv("SWM_STARTED"))
4957 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
4958 &ws_id, &mg, &ma, &si, &raise, s) != 6)
4959 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4960 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4963 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4964 errx(1, "layout: invalid workspace %d\n", ws_id + 1);
4966 if (!strcasecmp(s, "vertical"))
4968 else if (!strcasecmp(s, "horizontal"))
4970 else if (!strcasecmp(s, "fullscreen"))
4973 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4974 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4977 for (i = 0; i < ScreenCount(display); i++) {
4978 ws = (struct workspace *)&screens[i].ws;
4979 ws[ws_id].cur_layout = &layouts[st];
4981 ws[ws_id].always_raise = raise;
4982 if (st == SWM_MAX_STACK)
4986 for (x = 0; x < abs(mg); x++) {
4987 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4988 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
4989 SWM_ARG_ID_MASTERSHRINK);
4993 for (x = 0; x < abs(ma); x++) {
4994 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4995 ma >= 0 ? SWM_ARG_ID_MASTERADD :
4996 SWM_ARG_ID_MASTERDEL);
5000 for (x = 0; x < abs(si); x++) {
5001 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5002 si >= 0 ? SWM_ARG_ID_STACKINC :
5003 SWM_ARG_ID_STACKDEC);
5011 /* config options */
5012 struct config_option {
5014 int (*func)(char*, char*, int);
5017 struct config_option configopt[] = {
5018 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
5019 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
5020 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
5021 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
5022 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
5023 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
5024 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
5025 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
5026 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
5027 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
5028 { "keyboard_mapping", setkeymapping, 0 },
5029 { "bind", setconfbinding, 0 },
5030 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
5031 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
5032 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
5033 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
5034 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
5035 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
5036 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
5037 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
5038 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
5039 { "modkey", setconfmodkey, 0 },
5040 { "program", setconfspawn, 0 },
5041 { "quirk", setconfquirk, 0 },
5042 { "region", setconfregion, 0 },
5043 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
5044 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
5045 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
5046 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
5047 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
5048 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
5049 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
5050 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
5051 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
5052 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
5053 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
5054 { "autorun", setautorun, 0 },
5055 { "layout", setlayout, 0 },
5060 conf_load(char *filename, int keymapping)
5063 char *line, *cp, *optsub, *optval;
5064 size_t linelen, lineno = 0;
5065 int wordlen, i, optind;
5066 struct config_option *opt;
5068 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
5070 if (filename == NULL) {
5071 fprintf(stderr, "conf_load: no filename\n");
5074 if ((config = fopen(filename, "r")) == NULL) {
5075 warn("conf_load: fopen: %s", filename);
5079 while (!feof(config)) {
5080 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
5083 err(1, "%s", filename);
5088 cp += strspn(cp, " \t\n"); /* eat whitespace */
5089 if (cp[0] == '\0') {
5094 /* get config option */
5095 wordlen = strcspn(cp, "=[ \t\n");
5097 warnx("%s: line %zd: no option found",
5102 for (i = 0; i < LENGTH(configopt); i++) {
5103 opt = &configopt[i];
5104 if (!strncasecmp(cp, opt->optname, wordlen) &&
5105 strlen(opt->optname) == wordlen) {
5111 warnx("%s: line %zd: unknown option %.*s",
5112 filename, lineno, wordlen, cp);
5115 if (keymapping && strcmp(opt->optname, "bind")) {
5116 warnx("%s: line %zd: invalid option %.*s",
5117 filename, lineno, wordlen, cp);
5121 cp += strspn(cp, " \t\n"); /* eat whitespace */
5122 /* get [selector] if any */
5126 wordlen = strcspn(cp, "]");
5129 warnx("%s: line %zd: syntax error",
5133 asprintf(&optsub, "%.*s", wordlen, cp);
5136 cp += strspn(cp, "] \t\n"); /* eat trailing */
5138 cp += strspn(cp, "= \t\n"); /* eat trailing */
5140 optval = strdup(cp);
5141 /* call function to deal with it all */
5142 if (configopt[optind].func(optsub, optval,
5143 configopt[optind].funcflags) != 0) {
5144 fprintf(stderr, "%s line %zd: %s\n",
5145 filename, lineno, line);
5146 errx(1, "%s: line %zd: invalid data for %s",
5147 filename, lineno, configopt[optind].optname);
5155 DNPRINTF(SWM_D_CONF, "conf_load end\n");
5161 set_child_transient(struct ws_win *win, Window *trans)
5163 struct ws_win *parent, *w;
5164 XWMHints *wmh = NULL;
5165 struct swm_region *r;
5166 struct workspace *ws;
5168 parent = find_window(win->transient);
5170 parent->child_trans = win;
5172 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
5173 " for %lu trans %lu\n", win->id, win->transient);
5175 if (win->hints == NULL) {
5176 fprintf(stderr, "no hints for %lu\n", win->id);
5180 r = root_to_region(win->wa.root);
5182 /* parent doen't exist in our window list */
5183 TAILQ_FOREACH(w, &ws->winlist, entry) {
5187 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
5188 fprintf(stderr, "can't get hints for %lu\n",
5193 if (win->hints->window_group != wmh->window_group)
5196 w->child_trans = win;
5197 win->transient = w->id;
5199 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
5200 "transient to %lu\n", win->transient);
5210 window_get_pid(Window win)
5212 Atom actual_type_return;
5213 int actual_format_return = 0;
5214 unsigned long nitems_return = 0;
5215 unsigned long bytes_after_return = 0;
5219 unsigned char *prop = NULL;
5221 if (XGetWindowProperty(display, win,
5222 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
5223 XA_CARDINAL, &actual_type_return, &actual_format_return,
5224 &nitems_return, &bytes_after_return,
5225 (unsigned char**)(void*)&pid) != Success)
5227 if (actual_type_return != XA_CARDINAL)
5238 if (XGetWindowProperty(display, win,
5239 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
5240 XA_STRING, &actual_type_return, &actual_format_return,
5241 &nitems_return, &bytes_after_return, &prop) != Success)
5243 if (actual_type_return != XA_STRING)
5248 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
5249 /* ignore error because strtonum returns 0 anyway */
5256 manage_window(Window id)
5259 struct workspace *ws;
5260 struct ws_win *win, *ww;
5261 int format, i, ws_idx, n, border_me = 0;
5262 unsigned long nitems, bytes;
5263 Atom ws_idx_atom = 0, type;
5264 Atom *prot = NULL, *pp;
5265 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
5266 struct swm_region *r;
5272 if ((win = find_window(id)) != NULL)
5273 return (win); /* already being managed */
5275 /* see if we are on the unmanaged list */
5276 if ((win = find_unmanaged_window(id)) != NULL) {
5277 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
5279 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5280 if (win->transient) {
5281 set_child_transient(win, &trans);
5282 } if (trans && (ww = find_window(trans)))
5283 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
5285 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
5286 ewmh_update_actions(win);
5290 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
5291 errx(1, "calloc: failed to allocate memory for new window");
5295 /* see if we need to override the workspace */
5296 p = find_pid(window_get_pid(id));
5298 /* Get all the window data in one shot */
5299 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5301 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
5302 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
5304 XGetWindowAttributes(display, id, &win->wa);
5305 XGetWMNormalHints(display, id, &win->sh, &mask);
5306 win->hints = XGetWMHints(display, id);
5307 XGetTransientForHint(display, id, &trans);
5309 win->transient = trans;
5310 set_child_transient(win, &trans);
5311 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
5312 win->id, win->transient);
5315 /* get supported protocols */
5316 if (XGetWMProtocols(display, id, &prot, &n)) {
5317 for (i = 0, pp = prot; i < n; i++, pp++) {
5318 if (*pp == takefocus)
5319 win->take_focus = 1;
5321 win->can_delete = 1;
5327 win->iconic = get_iconic(win);
5330 * Figure out where to put the window. If it was previously assigned to
5331 * a workspace (either by spawn() or manually moving), and isn't
5332 * transient, * put it in the same workspace
5334 r = root_to_region(win->wa.root);
5336 ws = &r->s->ws[p->ws];
5337 TAILQ_REMOVE(&pidlist, p, entry);
5340 } else if (prop && win->transient == 0) {
5341 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
5342 ws_idx = strtonum((const char *)prop, 0, 9, &errstr);
5344 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
5347 ws = &r->s->ws[ws_idx];
5350 /* this should launch transients in the same ws as parent */
5352 if ((ww = find_window(trans)) != NULL)
5359 "fix this bug mcbride\n");
5364 /* set up the window layout */
5367 win->s = r->s; /* this never changes */
5368 if (trans && (ww = find_window(trans)))
5369 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5371 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5373 win->g.w = win->wa.width;
5374 win->g.h = win->wa.height;
5375 win->g.x = win->wa.x;
5376 win->g.y = win->wa.y;
5377 win->g_floatvalid = 0;
5378 win->floatmaxed = 0;
5379 win->ewmh_flags = 0;
5381 /* Set window properties so we can remember this after reincarnation */
5382 if (ws_idx_atom && prop == NULL &&
5383 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
5385 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
5387 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5388 PropModeReplace, ws_idx_str, SWM_PROPLEN);
5393 ewmh_autoquirk(win);
5395 if (XGetClassHint(display, win->id, &win->ch)) {
5396 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
5397 win->ch.res_class, win->ch.res_name);
5399 /* java is retarded so treat it special */
5400 if (strstr(win->ch.res_name, "sun-awt")) {
5405 for (i = 0; i < quirks_length; i++){
5406 if (!strcmp(win->ch.res_class, quirks[i].class) &&
5407 !strcmp(win->ch.res_name, quirks[i].name)) {
5408 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
5409 win->ch.res_class, win->ch.res_name);
5410 if (quirks[i].quirk & SWM_Q_FLOAT) {
5414 win->quirks = quirks[i].quirk;
5419 /* alter window position if quirky */
5420 if (win->quirks & SWM_Q_ANYWHERE) {
5421 win->manual = 1; /* don't center the quirky windows */
5422 bzero(&wc, sizeof wc);
5424 if (bar_enabled && win->g.y < bar_height) {
5425 win->g.y = wc.y = bar_height;
5428 if (win->g.w + win->g.x > WIDTH(r)) {
5429 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
5435 /* Reset font sizes (the bruteforce way; no default keybinding). */
5436 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5437 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5438 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5439 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5440 fake_keypress(win, XK_KP_Add, ShiftMask);
5443 ewmh_get_win_state(win);
5444 ewmh_update_actions(win);
5445 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5449 bzero(&wc, sizeof wc);
5450 wc.border_width = border_width;
5451 mask = CWBorderWidth;
5452 XConfigureWindow(display, win->id, mask, &wc);
5455 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5456 PropertyChangeMask | StructureNotifyMask);
5458 /* floaters need to be mapped if they are in the current workspace */
5459 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5460 XMapRaised(display, win->id);
5466 free_window(struct ws_win *win)
5468 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
5473 /* needed for restart wm */
5474 set_win_state(win, WithdrawnState);
5476 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5478 if (win->ch.res_class)
5479 XFree(win->ch.res_class);
5480 if (win->ch.res_name)
5481 XFree(win->ch.res_name);
5486 memset(win, 0xff, sizeof *win); /* XXX kill later */
5492 unmanage_window(struct ws_win *win)
5494 struct ws_win *parent;
5499 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
5501 if (win->transient) {
5502 parent = find_window(win->transient);
5504 parent->child_trans = NULL;
5507 /* focus on root just in case */
5508 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
5512 TAILQ_REMOVE(&win->ws->winlist, win, entry);
5513 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
5519 focus_magic(struct ws_win *win)
5521 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
5526 if (win->child_trans) {
5527 /* win = parent & has a transient so focus on that */
5529 focus_win(win->child_trans);
5530 if (win->child_trans->take_focus)
5531 client_msg(win, takefocus);
5533 /* make sure transient hasn't dissapeared */
5534 if (validate_win(win->child_trans) == 0) {
5535 focus_win(win->child_trans);
5536 if (win->child_trans->take_focus)
5537 client_msg(win->child_trans, takefocus);
5539 win->child_trans = NULL;
5541 if (win->take_focus)
5542 client_msg(win, takefocus);
5548 if (win->take_focus)
5549 client_msg(win, takefocus);
5556 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
5564 XKeyEvent *ev = &e->xkey;
5566 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
5568 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
5569 for (i = 0; i < keys_length; i++)
5570 if (keysym == keys[i].keysym
5571 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
5572 && keyfuncs[keys[i].funcid].func) {
5573 if (keys[i].funcid == kf_spawn_custom)
5575 root_to_region(ev->root),
5576 &(keyfuncs[keys[i].funcid].args),
5580 keyfuncs[keys[i].funcid].func(
5581 root_to_region(ev->root),
5582 &(keyfuncs[keys[i].funcid].args)
5588 buttonpress(XEvent *e)
5592 XButtonPressedEvent *ev = &e->xbutton;
5594 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
5596 if ((win = find_window(ev->window)) == NULL)
5600 action = client_click;
5602 for (i = 0; i < LENGTH(buttons); i++)
5603 if (action == buttons[i].action && buttons[i].func &&
5604 buttons[i].button == ev->button &&
5605 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5606 buttons[i].func(win, &buttons[i].args);
5610 configurerequest(XEvent *e)
5612 XConfigureRequestEvent *ev = &e->xconfigurerequest;
5617 if ((win = find_window(ev->window)) == NULL)
5618 if ((win = find_unmanaged_window(ev->window)) == NULL)
5622 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
5624 bzero(&wc, sizeof wc);
5627 wc.width = ev->width;
5628 wc.height = ev->height;
5629 wc.border_width = ev->border_width;
5630 wc.sibling = ev->above;
5631 wc.stack_mode = ev->detail;
5632 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
5634 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
5636 config_win(win, ev);
5641 configurenotify(XEvent *e)
5646 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
5647 e->xconfigure.window);
5649 win = find_window(e->xconfigure.window);
5651 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5655 if (focus_mode == SWM_FOCUS_DEFAULT)
5656 drain_enter_notify();
5661 destroynotify(XEvent *e)
5664 XDestroyWindowEvent *ev = &e->xdestroywindow;
5666 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
5668 if ((win = find_window(ev->window)) == NULL) {
5669 if ((win = find_unmanaged_window(ev->window)) == NULL)
5675 /* make sure we focus on something */
5678 unmanage_window(win);
5680 if (focus_mode == SWM_FOCUS_DEFAULT)
5681 drain_enter_notify();
5686 enternotify(XEvent *e)
5688 XCrossingEvent *ev = &e->xcrossing;
5693 Window focus_return;
5694 int revert_to_return;
5696 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
5697 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5698 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
5699 ev->same_screen, ev->focus, ev->state);
5701 switch (focus_mode) {
5702 case SWM_FOCUS_DEFAULT:
5704 case SWM_FOCUS_FOLLOW:
5706 case SWM_FOCUS_SYNERGY:
5709 * all these checks need to be in this order because the
5710 * XCheckTypedWindowEvent relies on weeding out the previous events
5712 * making this code an option would enable a follow mouse for focus
5717 * state is set when we are switching workspaces and focus is set when
5718 * the window or a subwindow already has focus (occurs during restart).
5720 * Only honor the focus flag if last_focus_event is not FocusOut,
5721 * this allows scrotwm to continue to control focus when another
5722 * program is also playing with it.
5724 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
5725 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
5730 * happens when a window is created or destroyed and the border
5731 * crosses the mouse pointer and when switching ws
5733 * we need the subwindow test to see if we came from root in order
5734 * to give focus to floaters
5736 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
5737 ev->subwindow == 0) {
5738 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
5742 /* this window already has focus */
5743 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
5744 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
5748 /* this window is being deleted or moved to another ws */
5749 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
5751 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
5752 XPutBackEvent(display, &cne);
5756 if ((win = find_window(ev->window)) == NULL) {
5757 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5762 * In fullstack kill all enters unless they come from a different ws
5763 * (i.e. another region) or focus has been grabbed externally.
5765 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
5766 last_focus_event != FocusOut) {
5767 XGetInputFocus(display, &focus_return, &revert_to_return);
5768 if ((w = find_window(focus_return)) == NULL ||
5770 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
5778 if ((win = find_window(ev->window)) == NULL) {
5779 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5784 * if we have more enternotifies let them handle it in due time
5786 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
5787 DNPRINTF(SWM_D_EVENT,
5788 "ignoring enternotify: got more enternotify\n");
5789 XPutBackEvent(display, &cne);
5796 /* lets us use one switch statement for arbitrary mode/detail combinations */
5797 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
5800 focusevent(XEvent *e)
5804 u_int32_t mode_detail;
5805 XFocusChangeEvent *ev = &e->xfocus;
5807 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
5808 ev->type == FocusIn ? "entering" : "leaving",
5809 ev->window, ev->mode, ev->detail);
5811 if (last_focus_event == ev->type) {
5812 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
5816 last_focus_event = ev->type;
5817 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
5819 switch (mode_detail) {
5820 /* synergy client focus operations */
5821 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
5822 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
5824 /* synergy server focus operations */
5825 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
5827 /* Entering applications like rdesktop that mangle the pointer */
5828 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
5830 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
5831 XSetWindowBorder(display, win->id,
5832 win->ws->r->s->c[ev->type == FocusIn ?
5833 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
5836 fprintf(stderr, "ignoring focusevent\n");
5837 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
5844 mapnotify(XEvent *e)
5847 XMapEvent *ev = &e->xmap;
5849 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
5851 win = manage_window(ev->window);
5853 set_win_state(win, NormalState);
5857 mappingnotify(XEvent *e)
5859 XMappingEvent *ev = &e->xmapping;
5861 XRefreshKeyboardMapping(ev);
5862 if (ev->request == MappingKeyboard)
5867 maprequest(XEvent *e)
5870 struct swm_region *r;
5871 XWindowAttributes wa;
5872 XMapRequestEvent *ev = &e->xmaprequest;
5874 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
5875 e->xmaprequest.window);
5877 if (!XGetWindowAttributes(display, ev->window, &wa))
5879 if (wa.override_redirect)
5882 win = manage_window(e->xmaprequest.window);
5884 return; /* can't happen */
5888 /* make new win focused */
5889 r = root_to_region(win->wa.root);
5890 if (win->ws == r->ws)
5895 propertynotify(XEvent *e)
5898 XPropertyEvent *ev = &e->xproperty;
5900 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
5903 win = find_window(ev->window);
5907 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
5908 update_iconic(win, 0);
5909 XMapRaised(display, win->id);
5916 case XA_WM_NORMAL_HINTS:
5919 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5920 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
5921 if (win->sh.flags & PMinSize) {
5922 win->g.w = win->sh.min_width;
5923 win->g.h = win->sh.min_height;
5924 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
5926 XMoveResizeWindow(display, win->id,
5927 win->g.x, win->g.y, win->g.w, win->g.h);
5929 if (window_name_enabled)
5938 unmapnotify(XEvent *e)
5942 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
5944 /* determine if we need to help unmanage this window */
5945 win = find_window(e->xunmap.window);
5949 if (getstate(e->xunmap.window) == NormalState) {
5950 unmanage_window(win);
5953 /* giant hack for apps that don't destroy transient windows */
5954 /* eat a bunch of events to prevent remanaging the window */
5956 while (XCheckWindowEvent(display, e->xunmap.window,
5957 EnterWindowMask, &cne))
5959 while (XCheckWindowEvent(display, e->xunmap.window,
5960 StructureNotifyMask, &cne))
5962 while (XCheckWindowEvent(display, e->xunmap.window,
5963 SubstructureNotifyMask, &cne))
5965 /* resend unmap because we ated it */
5966 XUnmapWindow(display, e->xunmap.window);
5969 if (focus_mode == SWM_FOCUS_DEFAULT)
5970 drain_enter_notify();
5974 visibilitynotify(XEvent *e)
5977 struct swm_region *r;
5979 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
5980 e->xvisibility.window);
5981 if (e->xvisibility.state == VisibilityUnobscured)
5982 for (i = 0; i < ScreenCount(display); i++)
5983 TAILQ_FOREACH(r, &screens[i].rl, entry)
5984 if (e->xvisibility.window == r->bar_window)
5989 clientmessage(XEvent *e)
5991 XClientMessageEvent *ev;
5996 win = find_window(ev->window);
6000 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
6001 ev->window, ev->message_type);
6003 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
6004 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
6007 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
6008 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
6009 if (win->can_delete)
6010 client_msg(win, adelete);
6012 XKillClient(display, win->id);
6014 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
6015 DNPRINTF(SWM_D_EVENT,
6016 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
6017 if (win->floating) {
6018 if (ev->data.l[0] & (1<<8)) /* x */
6019 win->g.x = ev->data.l[1];
6020 if (ev->data.l[0] & (1<<9)) /* y */
6021 win->g.y = ev->data.l[2];
6022 if (ev->data.l[0] & (1<<10)) /* width */
6023 win->g.w = ev->data.l[3];
6024 if (ev->data.l[0] & (1<<11)) /* height */
6025 win->g.h = ev->data.l[4];
6028 /* TODO: Change stack sizes */
6030 config_win(win, NULL);
6032 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
6033 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
6034 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
6036 ewmh_update_win_state(win, ev->data.l[2],
6044 xerror_start(Display *d, XErrorEvent *ee)
6051 xerror(Display *d, XErrorEvent *ee)
6053 /* fprintf(stderr, "error: %p %p\n", display, ee); */
6061 xerrorxlib = XSetErrorHandler(xerror_start);
6063 /* this causes an error if some other window manager is running */
6064 XSelectInput(display, DefaultRootWindow(display),
6065 SubstructureRedirectMask);
6066 XSync(display, False);
6070 XSetErrorHandler(xerror);
6071 XSync(display, False);
6076 new_region(struct swm_screen *s, int x, int y, int w, int h)
6078 struct swm_region *r, *n;
6079 struct workspace *ws = NULL;
6082 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
6083 s->idx, w, h, x, y);
6085 /* remove any conflicting regions */
6086 n = TAILQ_FIRST(&s->rl);
6089 n = TAILQ_NEXT(r, entry);
6090 if (X(r) < (x + w) &&
6091 (X(r) + WIDTH(r)) > x &&
6093 (Y(r) + HEIGHT(r)) > y) {
6094 if (r->ws->r != NULL)
6095 r->ws->old_r = r->ws->r;
6097 XDestroyWindow(display, r->bar_window);
6098 TAILQ_REMOVE(&s->rl, r, entry);
6099 TAILQ_INSERT_TAIL(&s->orl, r, entry);
6103 /* search old regions for one to reuse */
6105 /* size + location match */
6106 TAILQ_FOREACH(r, &s->orl, entry)
6107 if (X(r) == x && Y(r) == y &&
6108 HEIGHT(r) == h && WIDTH(r) == w)
6112 TAILQ_FOREACH(r, &s->orl, entry)
6113 if (HEIGHT(r) == h && WIDTH(r) == w)
6117 TAILQ_REMOVE(&s->orl, r, entry);
6118 /* try to use old region's workspace */
6119 if (r->ws->r == NULL)
6122 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
6123 errx(1, "calloc: failed to allocate memory for screen");
6125 /* if we don't have a workspace already, find one */
6127 for (i = 0; i < SWM_WS_MAX; i++)
6128 if (s->ws[i].r == NULL) {
6135 errx(1, "no free workspaces\n");
6146 TAILQ_INSERT_TAIL(&s->rl, r, entry);
6152 #ifdef SWM_XRR_HAS_CRTC
6154 XRRScreenResources *sr;
6157 #endif /* SWM_XRR_HAS_CRTC */
6158 struct swm_region *r;
6161 if (i >= ScreenCount(display))
6162 errx(1, "invalid screen");
6164 /* remove any old regions */
6165 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
6166 r->ws->old_r = r->ws->r = NULL;
6167 XDestroyWindow(display, r->bar_window);
6168 TAILQ_REMOVE(&screens[i].rl, r, entry);
6169 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
6173 /* map virtual screens onto physical screens */
6174 #ifdef SWM_XRR_HAS_CRTC
6175 if (xrandr_support) {
6176 sr = XRRGetScreenResources(display, screens[i].root);
6178 new_region(&screens[i], 0, 0,
6179 DisplayWidth(display, i),
6180 DisplayHeight(display, i));
6184 for (c = 0, ci = NULL; c < ncrtc; c++) {
6185 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
6186 if (ci->noutput == 0)
6189 if (ci != NULL && ci->mode == None)
6190 new_region(&screens[i], 0, 0,
6191 DisplayWidth(display, i),
6192 DisplayHeight(display, i));
6194 new_region(&screens[i],
6195 ci->x, ci->y, ci->width, ci->height);
6198 XRRFreeCrtcInfo(ci);
6199 XRRFreeScreenResources(sr);
6201 #endif /* SWM_XRR_HAS_CRTC */
6203 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
6204 DisplayHeight(display, i));
6209 screenchange(XEvent *e) {
6210 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
6211 struct swm_region *r;
6214 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
6216 if (!XRRUpdateConfiguration(e))
6219 /* silly event doesn't include the screen index */
6220 for (i = 0; i < ScreenCount(display); i++)
6221 if (screens[i].root == xe->root)
6223 if (i >= ScreenCount(display))
6224 errx(1, "screenchange: screen not found\n");
6226 /* brute force for now, just re-enumerate the regions */
6229 /* add bars to all regions */
6230 for (i = 0; i < ScreenCount(display); i++)
6231 TAILQ_FOREACH(r, &screens[i].rl, entry)
6234 if (focus_mode == SWM_FOCUS_DEFAULT)
6235 drain_enter_notify();
6241 Window d1, d2, *wins = NULL;
6242 XWindowAttributes wa;
6247 for (i = 0; i < ScreenCount(display); i++) {
6248 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
6251 /* attach windows to a region */
6252 /* normal windows */
6253 for (j = 0; j < no; j++) {
6254 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6255 wa.override_redirect ||
6256 XGetTransientForHint(display, wins[j], &d1))
6259 state = getstate(wins[j]);
6260 manage = state == IconicState;
6261 if (wa.map_state == IsViewable || manage)
6262 manage_window(wins[j]);
6264 /* transient windows */
6265 for (j = 0; j < no; j++) {
6266 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6267 wa.override_redirect)
6270 state = getstate(wins[j]);
6271 manage = state == IconicState;
6272 if (XGetTransientForHint(display, wins[j], &d1) &&
6274 manage_window(wins[j]);
6287 int errorbase, major, minor;
6288 struct workspace *ws;
6290 if ((screens = calloc(ScreenCount(display),
6291 sizeof(struct swm_screen))) == NULL)
6292 errx(1, "calloc: screens");
6294 /* initial Xrandr setup */
6295 xrandr_support = XRRQueryExtension(display,
6296 &xrandr_eventbase, &errorbase);
6298 if (XRRQueryVersion(display, &major, &minor) && major < 1)
6301 /* map physical screens */
6302 for (i = 0; i < ScreenCount(display); i++) {
6303 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
6305 TAILQ_INIT(&screens[i].rl);
6306 TAILQ_INIT(&screens[i].orl);
6307 screens[i].root = RootWindow(display, i);
6309 /* set default colors */
6310 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
6311 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
6312 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
6313 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
6314 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
6316 /* set default cursor */
6317 XDefineCursor(display, screens[i].root,
6318 XCreateFontCursor(display, XC_left_ptr));
6320 /* init all workspaces */
6321 /* XXX these should be dynamically allocated too */
6322 for (j = 0; j < SWM_WS_MAX; j++) {
6323 ws = &screens[i].ws[j];
6328 TAILQ_INIT(&ws->winlist);
6329 TAILQ_INIT(&ws->unmanagedlist);
6331 for (k = 0; layouts[k].l_stack != NULL; k++)
6332 if (layouts[k].l_config != NULL)
6333 layouts[k].l_config(ws,
6334 SWM_ARG_ID_STACKINIT);
6335 ws->cur_layout = &layouts[0];
6336 ws->cur_layout->l_string(ws);
6342 XRRSelectInput(display, screens[i].root,
6343 RRScreenChangeNotifyMask);
6350 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
6352 err(1, "setup_globals: strdup");
6353 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
6355 err(1, "setup_globals: strdup");
6356 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
6358 err(1, "setup_globals: strdup");
6359 if ((spawn_term[0] = strdup("xterm")) == NULL)
6360 err(1, "setup_globals: strdup");
6361 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6369 Atom netwmcheck, netwmname, utf8_string;
6372 /* work around sun jdk bugs, code from wmname */
6373 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6374 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6375 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6376 for (i = 0; i < ScreenCount(display); i++) {
6377 root = screens[i].root;
6378 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6379 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6380 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6382 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6383 PropModeReplace, (unsigned char *)&win,1);
6384 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6385 PropModeReplace, (unsigned char *)&win,1);
6386 XChangeProperty(display, win, netwmname, utf8_string, 8,
6387 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6392 main(int argc, char *argv[])
6394 struct swm_region *r, *rr;
6395 struct ws_win *winfocus = NULL;
6398 char conf[PATH_MAX], *cfile = NULL;
6403 struct sigaction sact;
6406 fprintf(stderr, "Welcome to scrotwm V%s Build: %s\n",
6407 SCROTWM_VERSION, buildstr);
6408 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
6410 warnx("no locale support");
6412 if (!(display = XOpenDisplay(0)))
6413 errx(1, "can not open display");
6416 errx(1, "other wm running");
6418 /* handle some signals */
6419 bzero(&sact, sizeof(sact));
6420 sigemptyset(&sact.sa_mask);
6422 sact.sa_handler = sighdlr;
6423 sigaction(SIGINT, &sact, NULL);
6424 sigaction(SIGQUIT, &sact, NULL);
6425 sigaction(SIGTERM, &sact, NULL);
6426 sigaction(SIGHUP, &sact, NULL);
6428 sact.sa_handler = sighdlr;
6429 sact.sa_flags = SA_NOCLDSTOP;
6430 sigaction(SIGCHLD, &sact, NULL);
6432 astate = XInternAtom(display, "WM_STATE", False);
6433 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6434 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6435 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6436 a_wmname = XInternAtom(display, "WM_NAME", False);
6437 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6438 a_string = XInternAtom(display, "STRING", False);
6439 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6441 /* look for local and global conf file */
6442 pwd = getpwuid(getuid());
6444 errx(1, "invalid user %d", getuid());
6453 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6454 if (stat(conf, &sb) != -1) {
6455 if (S_ISREG(sb.st_mode))
6458 /* try global conf file */
6459 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6460 if (!stat(conf, &sb))
6461 if (S_ISREG(sb.st_mode))
6465 conf_load(cfile, SWM_CONF_DEFAULT);
6468 /* set some values to work around bad programs */
6470 /* grab existing windows (before we build the bars) */
6473 if (getenv("SWM_STARTED") == NULL)
6474 setenv("SWM_STARTED", "YES", 1);
6476 /* setup all bars */
6477 for (i = 0; i < ScreenCount(display); i++)
6478 TAILQ_FOREACH(r, &screens[i].rl, entry) {
6479 if (winfocus == NULL)
6480 winfocus = TAILQ_FIRST(&r->ws->winlist);
6488 if (focus_mode == SWM_FOCUS_DEFAULT)
6489 drain_enter_notify();
6491 xfd = ConnectionNumber(display);
6493 while (XPending(display)) {
6494 XNextEvent(display, &e);
6497 if (e.type < LASTEvent) {
6499 if (handler[e.type])
6500 handler[e.type](&e);
6502 DNPRINTF(SWM_D_EVENT,
6503 "win: %lu unknown event: %d\n",
6504 e.xany.window, e.type);
6506 switch (e.type - xrandr_eventbase) {
6507 case RRScreenChangeNotify:
6511 DNPRINTF(SWM_D_EVENT,
6512 "win: %lu unknown xrandr event: "
6513 "%d\n", e.xany.window, e.type);
6519 /* if we are being restarted go focus on first window */
6521 rr = winfocus->ws->r;
6523 /* not a visible window */
6527 /* move pointer to first screen if multi screen */
6528 if (ScreenCount(display) > 1 || outputs > 1)
6529 XWarpPointer(display, None, rr->s[0].root,
6530 0, 0, 0, 0, rr->g.x,
6531 rr->g.y + (bar_enabled ? bar_height : 0));
6533 a.id = SWM_ARG_ID_FOCUSCUR;
6543 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
6545 DNPRINTF(SWM_D_MISC, "select failed");
6546 if (restart_wm == 1)
6547 restart(NULL, NULL);
6548 if (search_resp == 1)
6560 XFreeGC(display, bar_gc);
6561 XCloseDisplay(display);