]> code.delx.au - spectrwm/blob - spectrwm.c
missing xcb_icccm_get_wm_hints_reply #define for __OpenBSD__
[spectrwm] / spectrwm.c
1 /*
2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-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>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 *
12 * Permission to use, copy, modify, and distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24 /*
25 * Much code and ideas taken from dwm under the following license:
26 * MIT/X Consortium License
27 *
28 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
29 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
30 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
31 * 2007 Premysl Hruby <dfenze at gmail dot com>
32 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
33 * 2007 Christof Musik <christof at sendfax dot de>
34 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
35 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
36 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
37 *
38 * Permission is hereby granted, free of charge, to any person obtaining a
39 * copy of this software and associated documentation files (the "Software"),
40 * to deal in the Software without restriction, including without limitation
41 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
42 * and/or sell copies of the Software, and to permit persons to whom the
43 * Software is furnished to do so, subject to the following conditions:
44 *
45 * The above copyright notice and this permission notice shall be included in
46 * all copies or substantial portions of the Software.
47 *
48 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
51 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
53 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
54 * DEALINGS IN THE SOFTWARE.
55 */
56
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <locale.h>
63 #include <unistd.h>
64 #include <time.h>
65 #include <signal.h>
66 #include <string.h>
67 #include <util.h>
68 #include <pwd.h>
69 #include <paths.h>
70 #include <ctype.h>
71
72 #include <sys/types.h>
73 #include <sys/time.h>
74 #include <sys/stat.h>
75 #include <sys/wait.h>
76 #include <sys/queue.h>
77 #include <sys/param.h>
78 #include <sys/select.h>
79 #if defined(__linux__)
80 #include "tree.h"
81 #elif defined(__OpenBSD__)
82 #include <sys/tree.h>
83 #elif defined(__FreeBSD__)
84 #include <sys/tree.h>
85 #else
86 #include "tree.h"
87 #endif
88
89 #include <X11/cursorfont.h>
90 #include <X11/keysym.h>
91 #include <X11/XKBlib.h>
92 #include <X11/Xatom.h>
93 #include <X11/Xlib-xcb.h>
94 #include <xcb/randr.h>
95 #include <xcb/xcb_icccm.h>
96 #include <xcb/xcb_keysyms.h>
97 #include <X11/Xproto.h>
98 #include <X11/Xutil.h>
99 #include <X11/extensions/Xrandr.h>
100 #include <X11/extensions/XTest.h>
101
102 #ifdef __OSX__
103 #include <osx.h>
104 #endif
105
106 #include "version.h"
107
108 #ifdef SPECTRWM_BUILDSTR
109 static const char *buildstr = SPECTRWM_BUILDSTR;
110 #else
111 static const char *buildstr = SPECTRWM_VERSION;
112 #endif
113
114 #if RANDR_MAJOR < 1
115 # error XRandR versions less than 1.0 are not supported
116 #endif
117
118 #if RANDR_MAJOR >= 1
119 #if RANDR_MINOR >= 2
120 #define SWM_XRR_HAS_CRTC
121 #endif
122 #endif
123
124 #if defined(__OpenBSD__)
125 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
126 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
127 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
128 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
129 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
130 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
131 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
132 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
133 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
134 #endif
135
136 /*#define SWM_DEBUG*/
137 #ifdef SWM_DEBUG
138 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
139 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
140 #define SWM_D_MISC 0x0001
141 #define SWM_D_EVENT 0x0002
142 #define SWM_D_WS 0x0004
143 #define SWM_D_FOCUS 0x0008
144 #define SWM_D_MOVE 0x0010
145 #define SWM_D_STACK 0x0020
146 #define SWM_D_MOUSE 0x0040
147 #define SWM_D_PROP 0x0080
148 #define SWM_D_CLASS 0x0100
149 #define SWM_D_KEY 0x0200
150 #define SWM_D_QUIRK 0x0400
151 #define SWM_D_SPAWN 0x0800
152 #define SWM_D_EVENTQ 0x1000
153 #define SWM_D_CONF 0x2000
154 #define SWM_D_BAR 0x4000
155
156 u_int32_t swm_debug = 0
157 | SWM_D_MISC
158 | SWM_D_EVENT
159 | SWM_D_WS
160 | SWM_D_FOCUS
161 | SWM_D_MOVE
162 | SWM_D_STACK
163 | SWM_D_MOUSE
164 | SWM_D_PROP
165 | SWM_D_CLASS
166 | SWM_D_KEY
167 | SWM_D_QUIRK
168 | SWM_D_SPAWN
169 | SWM_D_EVENTQ
170 | SWM_D_CONF
171 | SWM_D_BAR
172 ;
173 #else
174 #define DPRINTF(x...)
175 #define DNPRINTF(n,x...)
176 #endif
177
178 #define LENGTH(x) (sizeof x / sizeof x[0])
179 #define MODKEY Mod1Mask
180 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
181 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
182 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
183 #define SWM_PROPLEN (16)
184 #define SWM_FUNCNAME_LEN (32)
185 #define SWM_KEYS_LEN (255)
186 #define SWM_QUIRK_LEN (64)
187 #define X(r) (r)->g.x
188 #define Y(r) (r)->g.y
189 #define WIDTH(r) (r)->g.w
190 #define HEIGHT(r) (r)->g.h
191 #define BORDER(w) (w->bordered ? border_width : 0)
192 #define MAX_X(r) ((r)->g.x + (r)->g.w)
193 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
194 #define SH_MIN(w) (w)->sh_mask & PMinSize
195 #define SH_MIN_W(w) (w)->sh.min_width
196 #define SH_MIN_H(w) (w)->sh.min_height
197 #define SH_MAX(w) (w)->sh_mask & PMaxSize
198 #define SH_MAX_W(w) (w)->sh.max_width
199 #define SH_MAX_H(w) (w)->sh.max_height
200 #define SH_INC(w) (w)->sh_mask & PResizeInc
201 #define SH_INC_W(w) (w)->sh.width_inc
202 #define SH_INC_H(w) (w)->sh.height_inc
203 #define SWM_MAX_FONT_STEPS (3)
204 #define WINID(w) ((w) ? (w)->id : 0)
205 #define YESNO(x) ((x) ? "yes" : "no")
206
207 #define SWM_FOCUS_DEFAULT (0)
208 #define SWM_FOCUS_SYNERGY (1)
209 #define SWM_FOCUS_FOLLOW (2)
210
211 #define SWM_CONF_DEFAULT (0)
212 #define SWM_CONF_KEYMAPPING (1)
213
214 #ifndef SWM_LIB
215 #define SWM_LIB "/usr/local/lib/libswmhack.so"
216 #endif
217
218 char **start_argv;
219 xcb_atom_t astate;
220 xcb_atom_t aprot;
221 xcb_atom_t adelete;
222 xcb_atom_t takefocus;
223 xcb_atom_t a_wmname;
224 xcb_atom_t a_netwmname;
225 xcb_atom_t a_utf8_string;
226 xcb_atom_t a_string;
227 xcb_atom_t a_swm_iconic;
228 volatile sig_atomic_t running = 1;
229 volatile sig_atomic_t restart_wm = 0;
230 int outputs = 0;
231 int last_focus_event = FocusOut;
232 int (*xerrorxlib)(Display *, XErrorEvent *);
233 int other_wm;
234 int ss_enabled = 0;
235 int xrandr_support;
236 int xrandr_eventbase;
237 unsigned int numlockmask = 0;
238 Display *display;
239 xcb_connection_t *conn;
240
241 int cycle_empty = 0;
242 int cycle_visible = 0;
243 int term_width = 0;
244 int font_adjusted = 0;
245 unsigned int mod_key = MODKEY;
246
247 /* dmenu search */
248 struct swm_region *search_r;
249 int select_list_pipe[2];
250 int select_resp_pipe[2];
251 pid_t searchpid;
252 volatile sig_atomic_t search_resp;
253 int search_resp_action;
254
255 struct search_window {
256 TAILQ_ENTRY(search_window) entry;
257 int idx;
258 struct ws_win *win;
259 GC gc;
260 xcb_window_t indicator;
261 };
262 TAILQ_HEAD(search_winlist, search_window);
263 struct search_winlist search_wl;
264
265 /* search actions */
266 enum {
267 SWM_SEARCH_NONE,
268 SWM_SEARCH_UNICONIFY,
269 SWM_SEARCH_NAME_WORKSPACE,
270 SWM_SEARCH_SEARCH_WORKSPACE,
271 SWM_SEARCH_SEARCH_WINDOW
272 };
273
274 #define SWM_STACK_TOP (0)
275 #define SWM_STACK_BOTTOM (1)
276 #define SWM_STACK_ABOVE (2)
277 #define SWM_STACK_BELOW (3)
278
279 /* dialog windows */
280 double dialog_ratio = 0.6;
281 /* status bar */
282 #define SWM_BAR_MAX (256)
283 #define SWM_BAR_JUSTIFY_LEFT (0)
284 #define SWM_BAR_JUSTIFY_CENTER (1)
285 #define SWM_BAR_JUSTIFY_RIGHT (2)
286 #define SWM_BAR_OFFSET (4)
287 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
288 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
289 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
290 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
291 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
292
293 #ifdef X_HAVE_UTF8_STRING
294 #define DRAWSTRING(x...) Xutf8DrawString(x)
295 #else
296 #define DRAWSTRING(x...) XmbDrawString(x)
297 #endif
298
299 char *bar_argv[] = { NULL, NULL };
300 int bar_pipe[2];
301 unsigned char bar_ext[SWM_BAR_MAX];
302 char bar_vertext[SWM_BAR_MAX];
303 int bar_version = 0;
304 sig_atomic_t bar_alarm = 0;
305 int bar_delay = 30;
306 int bar_enabled = 1;
307 int bar_border_width = 1;
308 int bar_at_bottom = 0;
309 int bar_extra = 1;
310 int bar_extra_running = 0;
311 int bar_verbose = 1;
312 int bar_height = 0;
313 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
314 char *bar_format = NULL;
315 int stack_enabled = 1;
316 int clock_enabled = 1;
317 int urgent_enabled = 0;
318 char *clock_format = NULL;
319 int title_name_enabled = 0;
320 int title_class_enabled = 0;
321 int window_name_enabled = 0;
322 int focus_mode = SWM_FOCUS_DEFAULT;
323 int focus_close = SWM_STACK_BELOW;
324 int focus_close_wrap = 1;
325 int focus_default = SWM_STACK_TOP;
326 int spawn_position = SWM_STACK_TOP;
327 int disable_border = 0;
328 int border_width = 1;
329 int verbose_layout = 0;
330 pid_t bar_pid;
331 XFontSet bar_fs;
332 XFontSetExtents *bar_fs_extents;
333 char *bar_fonts;
334 struct passwd *pwd;
335
336 #define SWM_MENU_FN (2)
337 #define SWM_MENU_NB (4)
338 #define SWM_MENU_NF (6)
339 #define SWM_MENU_SB (8)
340 #define SWM_MENU_SF (10)
341
342 /* layout manager data */
343 struct swm_geometry {
344 int x;
345 int y;
346 int w;
347 int h;
348 };
349
350 struct swm_screen;
351 struct workspace;
352
353 struct swm_bar {
354 xcb_window_t id;
355 xcb_pixmap_t buffer;
356 struct swm_geometry g;
357 };
358
359 /* virtual "screens" */
360 struct swm_region {
361 TAILQ_ENTRY(swm_region) entry;
362 struct swm_geometry g;
363 struct workspace *ws; /* current workspace on this region */
364 struct workspace *ws_prior; /* prior workspace on this region */
365 struct swm_screen *s; /* screen idx */
366 struct swm_bar *bar;
367 };
368 TAILQ_HEAD(swm_region_list, swm_region);
369
370 struct ws_win {
371 TAILQ_ENTRY(ws_win) entry;
372 xcb_window_t id;
373 xcb_window_t transient;
374 struct ws_win *child_trans; /* transient child window */
375 struct swm_geometry g; /* current geometry */
376 struct swm_geometry g_float; /* region coordinates */
377 int g_floatvalid; /* g_float geometry validity */
378 int floatmaxed; /* whether maxed by max_stack */
379 int floating;
380 int manual;
381 int iconic;
382 int bordered;
383 unsigned int ewmh_flags;
384 int font_size_boundary[SWM_MAX_FONT_STEPS];
385 int font_steps;
386 int last_inc;
387 int can_delete;
388 int take_focus;
389 int java;
390 unsigned long quirks;
391 struct workspace *ws; /* always valid */
392 struct swm_screen *s; /* always valid, never changes */
393 XWindowAttributes wa;
394 XSizeHints sh;
395 long sh_mask;
396 XClassHint ch;
397 XWMHints *hints;
398 };
399 TAILQ_HEAD(ws_win_list, ws_win);
400
401 /* pid goo */
402 struct pid_e {
403 TAILQ_ENTRY(pid_e) entry;
404 long pid;
405 int ws;
406 };
407 TAILQ_HEAD(pid_list, pid_e);
408 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
409
410 /* layout handlers */
411 void stack(void);
412 void vertical_config(struct workspace *, int);
413 void vertical_stack(struct workspace *, struct swm_geometry *);
414 void horizontal_config(struct workspace *, int);
415 void horizontal_stack(struct workspace *, struct swm_geometry *);
416 void max_stack(struct workspace *, struct swm_geometry *);
417 void plain_stacker(struct workspace *);
418 void fancy_stacker(struct workspace *);
419
420 struct ws_win *find_window(xcb_window_t);
421
422 void grabbuttons(struct ws_win *, int);
423 void new_region(struct swm_screen *, int, int, int, int);
424 void unmanage_window(struct ws_win *);
425 uint16_t getstate(xcb_window_t);
426
427 int conf_load(char *, int);
428
429 struct layout {
430 void (*l_stack)(struct workspace *, struct swm_geometry *);
431 void (*l_config)(struct workspace *, int);
432 u_int32_t flags;
433 #define SWM_L_FOCUSPREV (1<<0)
434 #define SWM_L_MAPONFOCUS (1<<1)
435 void (*l_string)(struct workspace *);
436 } layouts[] = {
437 /* stack, configure */
438 { vertical_stack, vertical_config, 0, plain_stacker },
439 { horizontal_stack, horizontal_config, 0, plain_stacker },
440 { max_stack, NULL,
441 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
442 { NULL, NULL, 0, NULL },
443 };
444
445 /* position of max_stack mode in the layouts array, index into layouts! */
446 #define SWM_V_STACK (0)
447 #define SWM_H_STACK (1)
448 #define SWM_MAX_STACK (2)
449
450 #define SWM_H_SLICE (32)
451 #define SWM_V_SLICE (32)
452
453 /* define work spaces */
454 struct workspace {
455 int idx; /* workspace index */
456 char *name; /* workspace name */
457 int always_raise; /* raise windows on focus */
458 struct layout *cur_layout; /* current layout handlers */
459 struct ws_win *focus; /* may be NULL */
460 struct ws_win *focus_prev; /* may be NULL */
461 struct swm_region *r; /* may be NULL */
462 struct swm_region *old_r; /* may be NULL */
463 struct ws_win_list winlist; /* list of windows in ws */
464 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
465 char stacker[10]; /* display stacker and layout */
466
467 /* stacker state */
468 struct {
469 int horizontal_msize;
470 int horizontal_mwin;
471 int horizontal_stacks;
472 int horizontal_flip;
473 int vertical_msize;
474 int vertical_mwin;
475 int vertical_stacks;
476 int vertical_flip;
477 } l_state;
478 };
479
480 enum {
481 SWM_S_COLOR_BAR,
482 SWM_S_COLOR_BAR_BORDER,
483 SWM_S_COLOR_BAR_FONT,
484 SWM_S_COLOR_FOCUS,
485 SWM_S_COLOR_UNFOCUS,
486 SWM_S_COLOR_MAX
487 };
488
489 /* physical screen mapping */
490 #define SWM_WS_MAX (22) /* hard limit */
491 int workspace_limit = 10; /* soft limit */
492
493 struct swm_screen {
494 int idx; /* screen index */
495 struct swm_region_list rl; /* list of regions on this screen */
496 struct swm_region_list orl; /* list of old regions */
497 xcb_window_t root;
498 struct workspace ws[SWM_WS_MAX];
499
500 /* colors */
501 struct {
502 uint32_t color;
503 char *name;
504 } c[SWM_S_COLOR_MAX];
505
506 GC bar_gc;
507 };
508 struct swm_screen *screens;
509
510 /* args to functions */
511 union arg {
512 int id;
513 #define SWM_ARG_ID_FOCUSNEXT (0)
514 #define SWM_ARG_ID_FOCUSPREV (1)
515 #define SWM_ARG_ID_FOCUSMAIN (2)
516 #define SWM_ARG_ID_FOCUSCUR (4)
517 #define SWM_ARG_ID_SWAPNEXT (10)
518 #define SWM_ARG_ID_SWAPPREV (11)
519 #define SWM_ARG_ID_SWAPMAIN (12)
520 #define SWM_ARG_ID_MOVELAST (13)
521 #define SWM_ARG_ID_MASTERSHRINK (20)
522 #define SWM_ARG_ID_MASTERGROW (21)
523 #define SWM_ARG_ID_MASTERADD (22)
524 #define SWM_ARG_ID_MASTERDEL (23)
525 #define SWM_ARG_ID_FLIPLAYOUT (24)
526 #define SWM_ARG_ID_STACKRESET (30)
527 #define SWM_ARG_ID_STACKINIT (31)
528 #define SWM_ARG_ID_CYCLEWS_UP (40)
529 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
530 #define SWM_ARG_ID_CYCLESC_UP (42)
531 #define SWM_ARG_ID_CYCLESC_DOWN (43)
532 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
533 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
534 #define SWM_ARG_ID_STACKINC (50)
535 #define SWM_ARG_ID_STACKDEC (51)
536 #define SWM_ARG_ID_SS_ALL (60)
537 #define SWM_ARG_ID_SS_WINDOW (61)
538 #define SWM_ARG_ID_DONTCENTER (70)
539 #define SWM_ARG_ID_CENTER (71)
540 #define SWM_ARG_ID_KILLWINDOW (80)
541 #define SWM_ARG_ID_DELETEWINDOW (81)
542 #define SWM_ARG_ID_WIDTHGROW (90)
543 #define SWM_ARG_ID_WIDTHSHRINK (91)
544 #define SWM_ARG_ID_HEIGHTGROW (92)
545 #define SWM_ARG_ID_HEIGHTSHRINK (93)
546 #define SWM_ARG_ID_MOVEUP (100)
547 #define SWM_ARG_ID_MOVEDOWN (101)
548 #define SWM_ARG_ID_MOVELEFT (102)
549 #define SWM_ARG_ID_MOVERIGHT (103)
550 char **argv;
551 };
552
553 void focus(struct swm_region *, union arg *);
554 void focus_magic(struct ws_win *);
555
556 /* quirks */
557 struct quirk {
558 TAILQ_ENTRY(quirk) entry;
559 char *class;
560 char *name;
561 unsigned long quirk;
562 #define SWM_Q_FLOAT (1<<0) /* float this window */
563 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
564 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
565 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
566 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
567 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
568 };
569 TAILQ_HEAD(quirk_list, quirk);
570 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
571
572 /*
573 * Supported EWMH hints should be added to
574 * both the enum and the ewmh array
575 */
576 enum {
577 _NET_ACTIVE_WINDOW,
578 _NET_CLOSE_WINDOW,
579 _NET_MOVERESIZE_WINDOW,
580 _NET_WM_ACTION_CLOSE,
581 _NET_WM_ACTION_FULLSCREEN,
582 _NET_WM_ACTION_MOVE,
583 _NET_WM_ACTION_RESIZE,
584 _NET_WM_ALLOWED_ACTIONS,
585 _NET_WM_STATE,
586 _NET_WM_STATE_ABOVE,
587 _NET_WM_STATE_FULLSCREEN,
588 _NET_WM_STATE_HIDDEN,
589 _NET_WM_STATE_MAXIMIZED_HORZ,
590 _NET_WM_STATE_MAXIMIZED_VERT,
591 _NET_WM_STATE_SKIP_PAGER,
592 _NET_WM_STATE_SKIP_TASKBAR,
593 _NET_WM_WINDOW_TYPE,
594 _NET_WM_WINDOW_TYPE_DIALOG,
595 _NET_WM_WINDOW_TYPE_DOCK,
596 _NET_WM_WINDOW_TYPE_NORMAL,
597 _NET_WM_WINDOW_TYPE_SPLASH,
598 _NET_WM_WINDOW_TYPE_TOOLBAR,
599 _NET_WM_WINDOW_TYPE_UTILITY,
600 _SWM_WM_STATE_MANUAL,
601 SWM_EWMH_HINT_MAX
602 };
603
604 struct ewmh_hint {
605 char *name;
606 xcb_atom_t atom;
607 } ewmh[SWM_EWMH_HINT_MAX] = {
608 /* must be in same order as in the enum */
609 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
610 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
611 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
612 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
613 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
614 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
615 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
616 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
617 {"_NET_WM_STATE", XCB_ATOM_NONE},
618 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
619 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
620 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
621 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
622 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
623 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
624 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
625 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
626 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
627 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
628 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
629 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
630 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
631 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
632 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
633 };
634
635 void store_float_geom(struct ws_win *, struct swm_region *);
636 int floating_toggle_win(struct ws_win *);
637 void constrain_window(struct ws_win *, struct swm_region *, int);
638 void update_window(struct ws_win *);
639 void spawn_select(struct swm_region *, union arg *, char *, int *);
640 unsigned char *get_win_name(xcb_window_t);
641 xcb_atom_t get_atom_from_string(const char *);
642 void map_window_raised(xcb_window_t);
643 void do_sync(void);
644
645 void
646 do_sync(void)
647 {
648 xcb_get_input_focus_cookie_t c;
649 xcb_get_input_focus_reply_t *r;
650
651 /* mimic XSync() */
652 c = xcb_get_input_focus(conn);
653 xcb_flush(conn);
654 r = xcb_get_input_focus_reply(conn, c, NULL);
655 if (r)
656 free(r);
657 }
658
659 void
660 map_window_raised(xcb_window_t win)
661 {
662 uint32_t val = XCB_STACK_MODE_ABOVE;
663
664 xcb_configure_window(conn, win,
665 XCB_CONFIG_WINDOW_STACK_MODE, &val);
666
667 xcb_map_window(conn, win);
668 xcb_flush(conn);
669 }
670
671 xcb_atom_t
672 get_atom_from_string(const char *str)
673 {
674 xcb_intern_atom_cookie_t c;
675 xcb_intern_atom_reply_t *r;
676 xcb_atom_t atom;
677
678 c = xcb_intern_atom(conn, False, strlen(str), str);
679 r = xcb_intern_atom_reply(conn, c, NULL);
680 if (r) {
681 atom = r->atom;
682 free(r);
683
684 return (atom);
685 }
686
687 return (XCB_ATOM_NONE);
688 }
689
690 int
691 get_property(Window id, Atom atom, long count, Atom type, unsigned long *nitems,
692 unsigned long *nbytes, unsigned char **data)
693 {
694 int format, status;
695 unsigned long *nbytes_ret, *nitems_ret;
696 unsigned long nbytes_tmp, nitems_tmp;
697 Atom real;
698
699 nbytes_ret = nbytes != NULL ? nbytes : &nbytes_tmp;
700 nitems_ret = nitems != NULL ? nitems : &nitems_tmp;
701
702 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
703 &real, &format, nitems_ret, nbytes_ret, data);
704
705 if (status != Success)
706 return (False);
707 if (real != type)
708 return (False);
709
710 return (True);
711 }
712
713 void
714 update_iconic(struct ws_win *win, int newv)
715 {
716 int32_t v = newv;
717 xcb_atom_t iprop;
718
719 win->iconic = newv;
720
721 iprop = get_atom_from_string("_SWM_ICONIC");
722 if (iprop == XCB_ATOM_NONE)
723 return;
724
725 if (newv)
726 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
727 iprop, XCB_ATOM_INTEGER, 32, 1, &v);
728 else
729 xcb_delete_property(conn, win->id, iprop);
730 }
731
732 int
733 get_iconic(struct ws_win *win)
734 {
735 int32_t v = 0, *vtmp;
736 xcb_atom_t iprop;
737 xcb_get_property_cookie_t pc;
738 xcb_get_property_reply_t *pr = NULL;
739
740 iprop = get_atom_from_string("_SWM_ICONIC");
741 if (iprop == XCB_ATOM_NONE)
742 goto out;
743
744 pc = xcb_get_property(conn, False, win->id, iprop, XCB_ATOM_INTEGER,
745 0, 1);
746 pr = xcb_get_property_reply(conn, pc, NULL);
747 if (!pr)
748 goto out;
749 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
750 goto out;
751 vtmp = xcb_get_property_value(pr);
752 v = *vtmp;
753 out:
754 if (pr != NULL)
755 free(pr);
756 return (v);
757 }
758
759 void
760 setup_ewmh(void)
761 {
762 xcb_atom_t sup_list;
763 int i, j, num_screens;
764
765 sup_list = get_atom_from_string("_NET_SUPPORTED");
766
767 for (i = 0; i < LENGTH(ewmh); i++)
768 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
769
770 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
771 for (i = 0; i < num_screens; i++) {
772 /* Support check window will be created by workaround(). */
773
774 /* Report supported atoms */
775 xcb_delete_property(conn, screens[i].root, sup_list);
776 for (j = 0; j < LENGTH(ewmh); j++)
777 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
778 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
779 &ewmh[j].atom);
780 }
781 }
782
783 void
784 teardown_ewmh(void)
785 {
786 int i, num_screens;
787 xcb_atom_t sup_check, sup_list;
788 xcb_window_t id;
789 xcb_get_property_cookie_t pc;
790 xcb_get_property_reply_t *pr;
791
792 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
793 sup_list = get_atom_from_string("_NET_SUPPORTED");
794 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
795
796 for (i = 0; i < num_screens; i++) {
797 /* Get the support check window and destroy it */
798 pc = xcb_get_property(conn, False, screens[i].root, sup_check,
799 XCB_ATOM_WINDOW, 0, 1);
800 pr = xcb_get_property_reply(conn, pc, NULL);
801 if (pr) {
802 id = *((xcb_window_t *)xcb_get_property_value(pr));
803
804 xcb_destroy_window(conn, id);
805 xcb_delete_property(conn, screens[i].root, sup_check);
806 xcb_delete_property(conn, screens[i].root, sup_list);
807
808 free(pr);
809 }
810 }
811 }
812
813 void
814 ewmh_autoquirk(struct ws_win *win)
815 {
816 int i;
817 unsigned long n;
818 xcb_atom_t type;
819
820 xcb_get_property_cookie_t c;
821 xcb_get_property_reply_t *r;
822
823 c = xcb_get_property(conn, False, win->id,
824 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, (~0L));
825 r = xcb_get_property_reply(conn, c, NULL);
826 if (!r)
827 return;
828 n = xcb_get_property_value_length(r);
829
830 for (i = 0; i < n; i++) {
831 type = *((xcb_atom_t *)xcb_get_property_value(r));
832 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
833 break;
834 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
835 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
836 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
837 win->floating = 1;
838 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
839 break;
840 }
841 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
842 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
843 win->floating = 1;
844 win->quirks = SWM_Q_FLOAT;
845 break;
846 }
847 }
848 free(r);
849 }
850
851 #define SWM_EWMH_ACTION_COUNT_MAX (6)
852 #define EWMH_F_FULLSCREEN (1<<0)
853 #define EWMH_F_ABOVE (1<<1)
854 #define EWMH_F_HIDDEN (1<<2)
855 #define EWMH_F_SKIP_PAGER (1<<3)
856 #define EWMH_F_SKIP_TASKBAR (1<<4)
857 #define SWM_F_MANUAL (1<<5)
858
859 int
860 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
861 {
862 if (!win->ws->r)
863 return (0);
864
865 if (!win->floating)
866 return (0);
867
868 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
869 "fullscreen %s\n", win->id, YESNO(fs));
870
871 if (fs) {
872 if (!win->g_floatvalid)
873 store_float_geom(win, win->ws->r);
874
875 win->g = win->ws->r->g;
876 win->bordered = 0;
877 } else {
878 if (win->g_floatvalid) {
879 /* refloat at last floating relative position */
880 win->g = win->g_float;
881 X(win) += X(win->ws->r);
882 Y(win) += Y(win->ws->r);
883 }
884 }
885
886 return (1);
887 }
888
889 void
890 ewmh_update_actions(struct ws_win *win)
891 {
892 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
893 int n = 0;
894
895 if (win == NULL)
896 return;
897
898 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
899
900 if (win->floating) {
901 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
902 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
903 }
904
905 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
906 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1,
907 actions);
908 }
909
910 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
911 #define _NET_WM_STATE_ADD 1 /* add/set property */
912 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
913
914 void
915 ewmh_update_win_state(struct ws_win *win, long state, long action)
916 {
917 unsigned int mask = 0;
918 unsigned int changed = 0;
919 unsigned int orig_flags;
920
921 if (win == NULL)
922 return;
923
924 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
925 mask = EWMH_F_FULLSCREEN;
926 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
927 mask = EWMH_F_ABOVE;
928 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
929 mask = SWM_F_MANUAL;
930 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
931 mask = EWMH_F_SKIP_PAGER;
932 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
933 mask = EWMH_F_SKIP_TASKBAR;
934
935
936 orig_flags = win->ewmh_flags;
937
938 switch (action) {
939 case _NET_WM_STATE_REMOVE:
940 win->ewmh_flags &= ~mask;
941 break;
942 case _NET_WM_STATE_ADD:
943 win->ewmh_flags |= mask;
944 break;
945 case _NET_WM_STATE_TOGGLE:
946 win->ewmh_flags ^= mask;
947 break;
948 }
949
950 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
951
952 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
953 if (changed)
954 if (!floating_toggle_win(win))
955 win->ewmh_flags = orig_flags; /* revert */
956 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
957 if (changed)
958 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
959 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
960 if (changed)
961 if (!ewmh_set_win_fullscreen(win,
962 win->ewmh_flags & EWMH_F_FULLSCREEN))
963 win->ewmh_flags = orig_flags; /* revert */
964
965 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
966
967 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
968 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
969 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
970 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
971 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
972 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
973 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
974 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
975 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
976 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
977 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
978 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
979 if (win->ewmh_flags & EWMH_F_ABOVE)
980 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
981 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
982 &ewmh[_NET_WM_STATE_ABOVE].atom);
983 if (win->ewmh_flags & SWM_F_MANUAL)
984 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
985 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
986 &ewmh[_SWM_WM_STATE_MANUAL].atom);
987 }
988
989 void
990 ewmh_get_win_state(struct ws_win *win)
991 {
992 xcb_atom_t *states;
993 xcb_get_property_cookie_t c;
994 xcb_get_property_reply_t *r;
995 int i, n;
996
997 if (win == NULL)
998 return;
999
1000 win->ewmh_flags = 0;
1001 if (win->floating)
1002 win->ewmh_flags |= EWMH_F_ABOVE;
1003 if (win->manual)
1004 win->ewmh_flags |= SWM_F_MANUAL;
1005
1006 c = xcb_get_property(conn, False, win->id, ewmh[_NET_WM_STATE].atom,
1007 XCB_ATOM_ATOM, 0, (~0L));
1008 r = xcb_get_property_reply(conn, c, NULL);
1009 if (!r)
1010 return;
1011
1012 states = xcb_get_property_value(r);
1013 n = xcb_get_property_value_length(r);
1014
1015 free(r);
1016
1017 for (i = 0; i < n; i++)
1018 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1019 }
1020
1021 /* events */
1022 #ifdef SWM_DEBUG
1023 char *
1024 geteventname(XEvent *e)
1025 {
1026 char *name = NULL;
1027
1028 switch (e->type) {
1029 case KeyPress:
1030 name = "KeyPress";
1031 break;
1032 case KeyRelease:
1033 name = "KeyRelease";
1034 break;
1035 case ButtonPress:
1036 name = "ButtonPress";
1037 break;
1038 case ButtonRelease:
1039 name = "ButtonRelease";
1040 break;
1041 case MotionNotify:
1042 name = "MotionNotify";
1043 break;
1044 case EnterNotify:
1045 name = "EnterNotify";
1046 break;
1047 case LeaveNotify:
1048 name = "LeaveNotify";
1049 break;
1050 case FocusIn:
1051 name = "FocusIn";
1052 break;
1053 case FocusOut:
1054 name = "FocusOut";
1055 break;
1056 case KeymapNotify:
1057 name = "KeymapNotify";
1058 break;
1059 case Expose:
1060 name = "Expose";
1061 break;
1062 case GraphicsExpose:
1063 name = "GraphicsExpose";
1064 break;
1065 case NoExpose:
1066 name = "NoExpose";
1067 break;
1068 case VisibilityNotify:
1069 name = "VisibilityNotify";
1070 break;
1071 case CreateNotify:
1072 name = "CreateNotify";
1073 break;
1074 case DestroyNotify:
1075 name = "DestroyNotify";
1076 break;
1077 case UnmapNotify:
1078 name = "UnmapNotify";
1079 break;
1080 case MapNotify:
1081 name = "MapNotify";
1082 break;
1083 case MapRequest:
1084 name = "MapRequest";
1085 break;
1086 case ReparentNotify:
1087 name = "ReparentNotify";
1088 break;
1089 case ConfigureNotify:
1090 name = "ConfigureNotify";
1091 break;
1092 case ConfigureRequest:
1093 name = "ConfigureRequest";
1094 break;
1095 case GravityNotify:
1096 name = "GravityNotify";
1097 break;
1098 case ResizeRequest:
1099 name = "ResizeRequest";
1100 break;
1101 case CirculateNotify:
1102 name = "CirculateNotify";
1103 break;
1104 case CirculateRequest:
1105 name = "CirculateRequest";
1106 break;
1107 case PropertyNotify:
1108 name = "PropertyNotify";
1109 break;
1110 case SelectionClear:
1111 name = "SelectionClear";
1112 break;
1113 case SelectionRequest:
1114 name = "SelectionRequest";
1115 break;
1116 case SelectionNotify:
1117 name = "SelectionNotify";
1118 break;
1119 case ColormapNotify:
1120 name = "ColormapNotify";
1121 break;
1122 case ClientMessage:
1123 name = "ClientMessage";
1124 break;
1125 case MappingNotify:
1126 name = "MappingNotify";
1127 break;
1128 default:
1129 name = "Unknown";
1130 }
1131
1132 return (name);
1133 }
1134
1135 char *
1136 xrandr_geteventname(XEvent *e)
1137 {
1138 char *name = NULL;
1139
1140 switch(e->type - xrandr_eventbase) {
1141 case RRScreenChangeNotify:
1142 name = "RRScreenChangeNotify";
1143 break;
1144 default:
1145 name = "Unknown";
1146 }
1147
1148 return (name);
1149 }
1150
1151 void
1152 dumpwins(struct swm_region *r, union arg *args)
1153 {
1154 struct ws_win *win;
1155 uint16_t state;
1156 xcb_get_window_attributes_cookie_t c;
1157 xcb_get_window_attributes_reply_t *wa;
1158
1159 if (r->ws == NULL) {
1160 warnx("dumpwins: invalid workspace");
1161 return;
1162 }
1163
1164 warnx("=== managed window list ws %02d ===", r->ws->idx);
1165 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1166 state = getstate(win->id);
1167 c = xcb_get_window_attributes(conn, win->id);
1168 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1169 if (wa) {
1170 warnx("window: 0x%x, map_state: %d, state: %u, "
1171 "transient: 0x%x", win->id, wa->map_state,
1172 state, win->transient);
1173 free(wa);
1174 } else
1175 warnx("window: 0x%x, failed xcb_get_window_attributes",
1176 win->id);
1177 }
1178
1179 warnx("===== unmanaged window list =====");
1180 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1181 state = getstate(win->id);
1182 c = xcb_get_window_attributes(conn, win->id);
1183 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1184 if (wa) {
1185 warnx("window: 0x%x, map_state: %d, state: %u, "
1186 "transient: 0x%x", win->id, wa->map_state,
1187 state, win->transient);
1188 free(wa);
1189 } else
1190 warnx("window: 0x%x, failed xcb_get_window_attributes",
1191 win->id);
1192 }
1193
1194 warnx("=================================");
1195 }
1196 #else
1197 void
1198 dumpwins(struct swm_region *r, union arg *args)
1199 {
1200 }
1201 #endif /* SWM_DEBUG */
1202
1203 void expose(XEvent *);
1204 void keypress(XEvent *);
1205 void buttonpress(XEvent *);
1206 void configurerequest(XEvent *);
1207 void configurenotify(XEvent *);
1208 void destroynotify(XEvent *);
1209 void enternotify(XEvent *);
1210 void focusevent(XEvent *);
1211 void mapnotify(XEvent *);
1212 void mappingnotify(XEvent *);
1213 void maprequest(XEvent *);
1214 void propertynotify(XEvent *);
1215 void unmapnotify(XEvent *);
1216 void visibilitynotify(XEvent *);
1217 void clientmessage(XEvent *);
1218
1219 void (*handler[LASTEvent])(XEvent *) = {
1220 [Expose] = expose,
1221 [KeyPress] = keypress,
1222 [ButtonPress] = buttonpress,
1223 [ConfigureRequest] = configurerequest,
1224 [ConfigureNotify] = configurenotify,
1225 [DestroyNotify] = destroynotify,
1226 [EnterNotify] = enternotify,
1227 [FocusIn] = focusevent,
1228 [FocusOut] = focusevent,
1229 [MapNotify] = mapnotify,
1230 [MappingNotify] = mappingnotify,
1231 [MapRequest] = maprequest,
1232 [PropertyNotify] = propertynotify,
1233 [UnmapNotify] = unmapnotify,
1234 [VisibilityNotify] = visibilitynotify,
1235 [ClientMessage] = clientmessage,
1236 };
1237
1238 void
1239 sighdlr(int sig)
1240 {
1241 int saved_errno, status;
1242 pid_t pid;
1243
1244 saved_errno = errno;
1245
1246 switch (sig) {
1247 case SIGCHLD:
1248 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1249 if (pid == -1) {
1250 if (errno == EINTR)
1251 continue;
1252 #ifdef SWM_DEBUG
1253 if (errno != ECHILD)
1254 warn("sighdlr: waitpid");
1255 #endif /* SWM_DEBUG */
1256 break;
1257 }
1258 if (pid == searchpid)
1259 search_resp = 1;
1260
1261 #ifdef SWM_DEBUG
1262 if (WIFEXITED(status)) {
1263 if (WEXITSTATUS(status) != 0)
1264 warnx("sighdlr: child exit status: %d",
1265 WEXITSTATUS(status));
1266 } else
1267 warnx("sighdlr: child is terminated "
1268 "abnormally");
1269 #endif /* SWM_DEBUG */
1270 }
1271 break;
1272
1273 case SIGHUP:
1274 restart_wm = 1;
1275 break;
1276 case SIGINT:
1277 case SIGTERM:
1278 case SIGQUIT:
1279 running = 0;
1280 break;
1281 }
1282
1283 errno = saved_errno;
1284 }
1285
1286 struct pid_e *
1287 find_pid(long pid)
1288 {
1289 struct pid_e *p = NULL;
1290
1291 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1292
1293 if (pid == 0)
1294 return (NULL);
1295
1296 TAILQ_FOREACH(p, &pidlist, entry) {
1297 if (p->pid == pid)
1298 return (p);
1299 }
1300
1301 return (NULL);
1302 }
1303
1304 uint32_t
1305 name_to_color(const char *colorname)
1306 {
1307 uint32_t result = 0;
1308 char cname[32] = "#";
1309 xcb_screen_t *screen;
1310 xcb_colormap_t cmap;
1311 xcb_alloc_named_color_cookie_t c;
1312 xcb_alloc_named_color_reply_t *r;
1313
1314 /* XXX - does not support rgb:/RR/GG/BB
1315 * will need to use xcb_alloc_color
1316 */
1317 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
1318 cmap = screen->default_colormap;
1319
1320 c = xcb_alloc_named_color(conn, cmap, strlen(colorname), colorname);
1321 r = xcb_alloc_named_color_reply(conn, c, NULL);
1322 if (!r) {
1323 strlcat(cname, colorname + 2, sizeof cname - 1);
1324 c = xcb_alloc_named_color(conn, cmap, strlen(cname),
1325 cname);
1326 r = xcb_alloc_named_color_reply(conn, c, NULL);
1327 }
1328 if (r) {
1329 result = r->pixel;
1330 free(r);
1331 } else
1332 warnx("color '%s' not found", colorname);
1333
1334 return (result);
1335 }
1336
1337 void
1338 setscreencolor(char *val, int i, int c)
1339 {
1340 int num_screens;
1341
1342 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1343 if (i > 0 && i <= num_screens) {
1344 screens[i - 1].c[c].color = name_to_color(val);
1345 free(screens[i - 1].c[c].name);
1346 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1347 err(1, "strdup");
1348 } else if (i == -1) {
1349 for (i = 0; i < num_screens; i++) {
1350 screens[i].c[c].color = name_to_color(val);
1351 free(screens[i].c[c].name);
1352 if ((screens[i].c[c].name = strdup(val)) == NULL)
1353 err(1, "strdup");
1354 }
1355 } else
1356 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1357 i, num_screens);
1358 }
1359
1360 void
1361 fancy_stacker(struct workspace *ws)
1362 {
1363 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1364 if (ws->cur_layout->l_stack == vertical_stack)
1365 snprintf(ws->stacker, sizeof ws->stacker,
1366 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1367 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1368 if (ws->cur_layout->l_stack == horizontal_stack)
1369 snprintf(ws->stacker, sizeof ws->stacker,
1370 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1371 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1372 }
1373
1374 void
1375 plain_stacker(struct workspace *ws)
1376 {
1377 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1378 if (ws->cur_layout->l_stack == vertical_stack)
1379 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1380 sizeof ws->stacker);
1381 if (ws->cur_layout->l_stack == horizontal_stack)
1382 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1383 sizeof ws->stacker);
1384 }
1385
1386 void
1387 custom_region(char *val)
1388 {
1389 unsigned int sidx, x, y, w, h;
1390 int num_screens;
1391
1392 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1393 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1394 errx(1, "invalid custom region, "
1395 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1396 if (sidx < 1 || sidx > num_screens)
1397 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1398 sidx, num_screens);
1399 sidx--;
1400
1401 if (w < 1 || h < 1)
1402 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1403
1404 if (x > DisplayWidth(display, sidx) ||
1405 y > DisplayHeight(display, sidx) ||
1406 w + x > DisplayWidth(display, sidx) ||
1407 h + y > DisplayHeight(display, sidx)) {
1408 warnx("ignoring region %ux%u+%u+%u - not within screen "
1409 "boundaries (%ux%u)", w, h, x, y,
1410 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1411 return;
1412 }
1413
1414 new_region(&screens[sidx], x, y, w, h);
1415 }
1416
1417 void
1418 socket_setnonblock(int fd)
1419 {
1420 int flags;
1421
1422 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1423 err(1, "fcntl F_GETFL");
1424 flags |= O_NONBLOCK;
1425 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1426 err(1, "fcntl F_SETFL");
1427 }
1428
1429 void
1430 bar_print(struct swm_region *r, const char *s)
1431 {
1432 int x = 0;
1433 size_t len;
1434 XRectangle ibox, lbox;
1435
1436 len = strlen(s);
1437 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1438
1439 switch (bar_justify) {
1440 case SWM_BAR_JUSTIFY_LEFT:
1441 x = SWM_BAR_OFFSET;
1442 break;
1443 case SWM_BAR_JUSTIFY_CENTER:
1444 x = (WIDTH(r) - lbox.width) / 2;
1445 break;
1446 case SWM_BAR_JUSTIFY_RIGHT:
1447 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1448 break;
1449 }
1450
1451 if (x < SWM_BAR_OFFSET)
1452 x = SWM_BAR_OFFSET;
1453
1454 /* clear back buffer */
1455 XSetForeground(display, r->s->bar_gc, r->s->c[SWM_S_COLOR_BAR].color);
1456 XFillRectangle(display, r->bar->buffer, r->s->bar_gc, 0, 0,
1457 WIDTH(r->bar), HEIGHT(r->bar));
1458
1459 /* draw back buffer */
1460 XSetForeground(display, r->s->bar_gc,
1461 r->s->c[SWM_S_COLOR_BAR_FONT].color);
1462 DRAWSTRING(display, r->bar->buffer, bar_fs, r->s->bar_gc,
1463 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1464 lbox.y, s, len);
1465
1466 /* blt */
1467 XCopyArea(display, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1468 WIDTH(r->bar), HEIGHT(r->bar), 0, 0);
1469 }
1470
1471 void
1472 bar_extra_stop(void)
1473 {
1474 if (bar_pipe[0]) {
1475 close(bar_pipe[0]);
1476 bzero(bar_pipe, sizeof bar_pipe);
1477 }
1478 if (bar_pid) {
1479 kill(bar_pid, SIGTERM);
1480 bar_pid = 0;
1481 }
1482 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1483 bar_extra = 0;
1484 }
1485
1486 void
1487 bar_class_name(char *s, size_t sz, struct swm_region *r)
1488 {
1489 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1490 return;
1491 if (r->ws->focus->ch.res_class != NULL)
1492 strlcat(s, r->ws->focus->ch.res_class, sz);
1493 }
1494
1495 void
1496 bar_title_name(char *s, size_t sz, struct swm_region *r)
1497 {
1498 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1499 return;
1500 if (r->ws->focus->ch.res_name != NULL)
1501 strlcat(s, r->ws->focus->ch.res_name, sz);
1502 }
1503
1504 void
1505 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1506 {
1507 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1508 return;
1509
1510 bar_class_name(s, sz, r);
1511 strlcat(s, ":", sz);
1512 bar_title_name(s, sz, r);
1513 }
1514
1515 void
1516 bar_window_float(char *s, size_t sz, struct swm_region *r)
1517 {
1518 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1519 return;
1520 if (r->ws->focus->floating)
1521 strlcat(s, "(f)", sz);
1522 }
1523
1524 void
1525 bar_window_name(char *s, size_t sz, struct swm_region *r)
1526 {
1527 unsigned char *title;
1528
1529 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1530 return;
1531 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1532 return;
1533
1534 strlcat(s, (char *)title, sz);
1535 XFree(title);
1536 }
1537
1538 int urgent[SWM_WS_MAX];
1539 void
1540 bar_urgent(char *s, size_t sz)
1541 {
1542 struct ws_win *win;
1543 int i, j, num_screens;
1544 char b[8];
1545 xcb_get_property_cookie_t c;
1546 xcb_icccm_wm_hints_t hints;
1547
1548 for (i = 0; i < workspace_limit; i++)
1549 urgent[i] = 0;
1550
1551 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1552 for (i = 0; i < num_screens; i++)
1553 for (j = 0; j < workspace_limit; j++)
1554 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1555 c = xcb_icccm_get_wm_hints(conn, win->id);
1556 if (xcb_icccm_get_wm_hints_reply(conn, c,
1557 &hints, NULL) == 0)
1558 continue;
1559 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
1560 urgent[j] = 1;
1561 }
1562
1563 for (i = 0; i < workspace_limit; i++) {
1564 if (urgent[i])
1565 snprintf(b, sizeof b, "%d ", i + 1);
1566 else
1567 snprintf(b, sizeof b, "- ");
1568 strlcat(s, b, sz);
1569 }
1570 }
1571
1572 void
1573 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1574 {
1575 if (r == NULL || r->ws == NULL)
1576 return;
1577 if (r->ws->name != NULL)
1578 strlcat(s, r->ws->name, sz);
1579 }
1580
1581 /* build the default bar format according to the defined enabled options */
1582 void
1583 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1584 {
1585 /* if format provided, just copy the buffers */
1586 if (bar_format != NULL) {
1587 strlcpy(fmtnew, fmtexp, sz);
1588 return;
1589 }
1590
1591 /* reset the output buffer */
1592 *fmtnew = '\0';
1593
1594 strlcat(fmtnew, "+N:+I ", sz);
1595 if (stack_enabled)
1596 strlcat(fmtnew, "+S", sz);
1597 strlcat(fmtnew, " ", sz);
1598
1599 /* only show the workspace name if there's actually one */
1600 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1601 strlcat(fmtnew, "<+D>", sz);
1602 strlcat(fmtnew, "+3<", sz);
1603
1604 if (clock_enabled) {
1605 strlcat(fmtnew, fmtexp, sz);
1606 strlcat(fmtnew, "+4<", sz);
1607 }
1608
1609 /* bar_urgent already adds the space before the last asterisk */
1610 if (urgent_enabled)
1611 strlcat(fmtnew, "* +U*+4<", sz);
1612
1613 if (title_class_enabled) {
1614 strlcat(fmtnew, "+C", sz);
1615 if (title_name_enabled == 0)
1616 strlcat(fmtnew, "+4<", sz);
1617 }
1618
1619 /* checks needed by the colon and floating strlcat(3) calls below */
1620 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1621 if (title_name_enabled) {
1622 if (title_class_enabled)
1623 strlcat(fmtnew, ":", sz);
1624 strlcat(fmtnew, "+T+4<", sz);
1625 }
1626 if (window_name_enabled) {
1627 if (r->ws->focus->floating)
1628 strlcat(fmtnew, "+F ", sz);
1629 strlcat(fmtnew, "+64W ", sz);
1630 }
1631 }
1632
1633 /* finally add the action script output and the version */
1634 strlcat(fmtnew, "+4<+A+4<+V", sz);
1635 }
1636
1637 void
1638 bar_replace_pad(char *tmp, int *limit, size_t sz)
1639 {
1640 /* special case; no limit given, pad one space, instead */
1641 if (*limit == sz - 1)
1642 *limit = 1;
1643 snprintf(tmp, sz, "%*s", *limit, " ");
1644 }
1645
1646 /* replaces the bar format character sequences (like in tmux(1)) */
1647 char *
1648 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1649 size_t sz)
1650 {
1651 char *ptr;
1652 char tmp[SWM_BAR_MAX];
1653 int limit, size;
1654 size_t len;
1655
1656 /* reset strlcat(3) buffer */
1657 *tmp = '\0';
1658
1659 /* get number, if any */
1660 fmt++;
1661 size = 0;
1662 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
1663 limit = sizeof tmp - 1;
1664 if (limit <= 0 || limit >= sizeof tmp)
1665 limit = sizeof tmp - 1;
1666
1667 /* there is nothing to replace (ie EOL) */
1668 fmt += size;
1669 if (*fmt == '\0')
1670 return (fmt);
1671
1672 switch (*fmt) {
1673 case '<':
1674 bar_replace_pad(tmp, &limit, sizeof tmp);
1675 break;
1676 case 'A':
1677 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1678 break;
1679 case 'C':
1680 bar_class_name(tmp, sizeof tmp, r);
1681 break;
1682 case 'D':
1683 bar_workspace_name(tmp, sizeof tmp, r);
1684 break;
1685 case 'F':
1686 bar_window_float(tmp, sizeof tmp, r);
1687 break;
1688 case 'I':
1689 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1690 break;
1691 case 'N':
1692 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1693 break;
1694 case 'P':
1695 bar_class_title_name(tmp, sizeof tmp, r);
1696 break;
1697 case 'S':
1698 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1699 break;
1700 case 'T':
1701 bar_title_name(tmp, sizeof tmp, r);
1702 break;
1703 case 'U':
1704 bar_urgent(tmp, sizeof tmp);
1705 break;
1706 case 'V':
1707 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1708 break;
1709 case 'W':
1710 bar_window_name(tmp, sizeof tmp, r);
1711 break;
1712 default:
1713 /* unknown character sequence; copy as-is */
1714 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1715 break;
1716 }
1717
1718 len = strlen(tmp);
1719 ptr = tmp;
1720 if (len < limit)
1721 limit = len;
1722 while (limit-- > 0) {
1723 if (*offrep >= sz - 1)
1724 break;
1725 fmtrep[(*offrep)++] = *ptr++;
1726 }
1727
1728 fmt++;
1729 return (fmt);
1730 }
1731
1732 void
1733 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1734 {
1735 size_t off;
1736
1737 off = 0;
1738 while (*fmt != '\0') {
1739 if (*fmt != '+') {
1740 /* skip ordinary characters */
1741 if (off >= sz - 1)
1742 break;
1743 fmtrep[off++] = *fmt++;
1744 continue;
1745 }
1746
1747 /* character sequence found; replace it */
1748 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1749 if (off >= sz - 1)
1750 break;
1751 }
1752
1753 fmtrep[off] = '\0';
1754 }
1755
1756 void
1757 bar_fmt_expand(char *fmtexp, size_t sz)
1758 {
1759 char *fmt = NULL;
1760 size_t len;
1761 struct tm tm;
1762 time_t tmt;
1763
1764 /* start by grabbing the current time and date */
1765 time(&tmt);
1766 localtime_r(&tmt, &tm);
1767
1768 /* figure out what to expand */
1769 if (bar_format != NULL)
1770 fmt = bar_format;
1771 else if (bar_format == NULL && clock_enabled)
1772 fmt = clock_format;
1773 /* if nothing to expand bail out */
1774 if (fmt == NULL) {
1775 *fmtexp = '\0';
1776 return;
1777 }
1778
1779 /* copy as-is, just in case the format shouldn't be expanded below */
1780 strlcpy(fmtexp, fmt, sz);
1781 /* finally pass the string through strftime(3) */
1782 #ifndef SWM_DENY_CLOCK_FORMAT
1783 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1784 warnx("format too long");
1785 fmtexp[len] = '\0';
1786 #endif
1787 }
1788
1789 void
1790 bar_fmt_print(void)
1791 {
1792 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1793 char fmtrep[SWM_BAR_MAX];
1794 int i, num_screens;
1795 struct swm_region *r;
1796
1797 /* expand the format by first passing it through strftime(3) */
1798 bar_fmt_expand(fmtexp, sizeof fmtexp);
1799
1800 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1801 for (i = 0; i < num_screens; i++) {
1802 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1803 if (r->bar == NULL)
1804 continue;
1805 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1806 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1807 bar_print(r, fmtrep);
1808 }
1809 }
1810 }
1811
1812 void
1813 bar_update(void)
1814 {
1815 size_t len;
1816 char *b;
1817
1818 if (bar_enabled == 0)
1819 return;
1820 if (bar_extra && bar_extra_running) {
1821 /* ignore short reads; it'll correct itself */
1822 while ((b = fgetln(stdin, &len)) != NULL)
1823 if (b && b[len - 1] == '\n') {
1824 b[len - 1] = '\0';
1825 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1826 }
1827 if (b == NULL && errno != EAGAIN) {
1828 warn("bar_update: bar_extra failed");
1829 bar_extra_stop();
1830 }
1831 } else
1832 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1833
1834 bar_fmt_print();
1835 alarm(bar_delay);
1836 }
1837
1838 void
1839 bar_signal(int sig)
1840 {
1841 bar_alarm = 1;
1842 }
1843
1844 void
1845 bar_toggle(struct swm_region *r, union arg *args)
1846 {
1847 struct swm_region *tmpr;
1848 int i, num_screens;
1849
1850 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1851
1852 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1853 if (bar_enabled) {
1854 for (i = 0; i < num_screens; i++)
1855 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1856 if (tmpr->bar)
1857 xcb_unmap_window(conn, tmpr->bar->id);
1858 } else {
1859 for (i = 0; i < num_screens; i++)
1860 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1861 if (tmpr->bar)
1862 map_window_raised(tmpr->bar->id);
1863 }
1864
1865 bar_enabled = !bar_enabled;
1866
1867 stack();
1868 /* must be after stack */
1869 bar_update();
1870 }
1871
1872 void
1873 bar_refresh(void)
1874 {
1875 struct swm_region *r;
1876 uint32_t wa[2];
1877 int i, num_screens;
1878
1879 /* do this here because the conf file is in memory */
1880 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1881 /* launch external status app */
1882 bar_extra_running = 1;
1883 if (pipe(bar_pipe) == -1)
1884 err(1, "pipe error");
1885 socket_setnonblock(bar_pipe[0]);
1886 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1887 if (dup2(bar_pipe[0], 0) == -1)
1888 err(1, "dup2");
1889 if (dup2(bar_pipe[1], 1) == -1)
1890 err(1, "dup2");
1891 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1892 err(1, "could not disable SIGPIPE");
1893 switch (bar_pid = fork()) {
1894 case -1:
1895 err(1, "cannot fork");
1896 break;
1897 case 0: /* child */
1898 close(bar_pipe[0]);
1899 execvp(bar_argv[0], bar_argv);
1900 err(1, "%s external app failed", bar_argv[0]);
1901 break;
1902 default: /* parent */
1903 close(bar_pipe[1]);
1904 break;
1905 }
1906 }
1907
1908 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
1909 for (i = 0; i < num_screens; i++)
1910 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1911 if (r->bar == NULL)
1912 continue;
1913 wa[0] = screens[i].c[SWM_S_COLOR_BAR].color;
1914 wa[1] = screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1915 xcb_change_window_attributes(conn, r->bar->id,
1916 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
1917 }
1918 bar_update();
1919 }
1920
1921 void
1922 bar_setup(struct swm_region *r)
1923 {
1924 char *default_string;
1925 char **missing_charsets;
1926 int num_missing_charsets = 0;
1927 int i;
1928
1929 if (bar_fs) {
1930 XFreeFontSet(display, bar_fs);
1931 bar_fs = NULL;
1932 }
1933
1934 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
1935 err(1, "bar_setup: calloc: failed to allocate memory.");
1936
1937 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1938
1939 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1940 &num_missing_charsets, &default_string);
1941
1942 if (num_missing_charsets > 0) {
1943 warnx("Unable to load charset(s):");
1944
1945 for (i = 0; i < num_missing_charsets; ++i)
1946 warnx("%s", missing_charsets[i]);
1947
1948 XFreeStringList(missing_charsets);
1949
1950 if (strcmp(default_string, ""))
1951 warnx("Glyphs from those sets will be replaced "
1952 "by '%s'.", default_string);
1953 else
1954 warnx("Glyphs from those sets won't be drawn.");
1955 }
1956
1957 if (bar_fs == NULL)
1958 errx(1, "Error creating font set structure.");
1959
1960 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1961
1962 bar_height = bar_fs_extents->max_logical_extent.height +
1963 2 * bar_border_width;
1964
1965 if (bar_height < 1)
1966 bar_height = 1;
1967
1968 X(r->bar) = X(r);
1969 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1970 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
1971 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
1972
1973 r->bar->id = XCreateSimpleWindow(display,
1974 r->s->root, X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
1975 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1976 r->s->c[SWM_S_COLOR_BAR].color);
1977
1978 r->bar->buffer = XCreatePixmap(display, r->bar->id, WIDTH(r->bar),
1979 HEIGHT(r->bar), DefaultDepth(display, r->s->idx));
1980
1981 xcb_randr_select_input(conn, r->bar->id,
1982 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
1983
1984 if (bar_enabled)
1985 map_window_raised(r->bar->id);
1986
1987 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
1988 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
1989 HEIGHT(r->bar));
1990
1991 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1992 err(1, "could not install bar_signal");
1993 bar_refresh();
1994 }
1995
1996 void
1997 bar_cleanup(struct swm_region *r)
1998 {
1999 if (r->bar == NULL)
2000 return;
2001 xcb_destroy_window(conn, r->bar->id);
2002 xcb_free_pixmap(conn, r->bar->buffer);
2003 free(r->bar);
2004 r->bar = NULL;
2005 }
2006
2007 void
2008 drain_enter_notify(void)
2009 {
2010 int i = 0;
2011
2012 while (xcb_poll_for_event(conn))
2013 i++;
2014
2015 DNPRINTF(SWM_D_EVENT, "drain_enter_notify: drained: %d\n", i);
2016 }
2017
2018 void
2019 set_win_state(struct ws_win *win, uint16_t state)
2020 {
2021 uint16_t data[2] = { state, XCB_ATOM_NONE };
2022
2023 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x\n", win->id);
2024
2025 if (win == NULL)
2026 return;
2027
2028 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, astate,
2029 astate, 32, 2, data);
2030 }
2031
2032 uint16_t
2033 getstate(xcb_window_t w)
2034 {
2035 uint16_t result = 0;
2036 xcb_get_property_cookie_t c;
2037 xcb_get_property_reply_t *r;
2038
2039 c = xcb_get_property(conn, False, w, astate, astate, 0L, 2L);
2040 r = xcb_get_property_reply(conn, c, NULL);
2041
2042 if (r) {
2043 result = *((uint16_t *)xcb_get_property_value(r));
2044 free(r);
2045 }
2046
2047 return (result);
2048 }
2049
2050 void
2051 version(struct swm_region *r, union arg *args)
2052 {
2053 bar_version = !bar_version;
2054 if (bar_version)
2055 snprintf(bar_vertext, sizeof bar_vertext,
2056 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2057 else
2058 strlcpy(bar_vertext, "", sizeof bar_vertext);
2059 bar_update();
2060 }
2061
2062 void
2063 client_msg(struct ws_win *win, xcb_atom_t a)
2064 {
2065 xcb_client_message_event_t ev;
2066
2067 if (win == NULL)
2068 return;
2069
2070 bzero(&ev, sizeof ev);
2071 ev.response_type = XCB_CLIENT_MESSAGE;
2072 ev.window = win->id;
2073 ev.type = aprot;
2074 ev.format = 32;
2075 ev.data.data32[0] = a;
2076 ev.data.data32[1] = XCB_CURRENT_TIME;
2077
2078 xcb_send_event(conn, False, win->id,
2079 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2080 xcb_flush(conn);
2081 }
2082
2083 /* synthetic response to a ConfigureRequest when not making a change */
2084 void
2085 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
2086 {
2087 xcb_configure_notify_event_t ce;
2088
2089 if (win == NULL)
2090 return;
2091
2092 /* send notification of unchanged state. */
2093 bzero(&ce, sizeof(ce));
2094 ce.response_type = XCB_CONFIGURE_NOTIFY;
2095 ce.x = X(win);
2096 ce.y = Y(win);
2097 ce.width = WIDTH(win);
2098 ce.height = HEIGHT(win);
2099 ce.override_redirect = False;
2100
2101 if (ev == NULL) {
2102 /* EWMH */
2103 ce.event = win->id;
2104 ce.window = win->id;
2105 ce.border_width = BORDER(win);
2106 ce.above_sibling = XCB_WINDOW_NONE;
2107 } else {
2108 /* normal */
2109 ce.event = ev->window;
2110 ce.window = ev->window;
2111
2112 /* make response appear more WM_SIZE_HINTS-compliant */
2113 if (win->sh_mask)
2114 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2115 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
2116 "%d x %d\n", win->id, win->sh_mask, SH_MIN_W(win),
2117 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2118 SH_INC_W(win), SH_INC_H(win));
2119
2120 /* min size */
2121 if (SH_MIN(win)) {
2122 /* the hint may be set... to 0! */
2123 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2124 ce.width = SH_MIN_W(win);
2125 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2126 ce.height = SH_MIN_H(win);
2127 }
2128
2129 /* max size */
2130 if (SH_MAX(win)) {
2131 /* may also be advertized as 0 */
2132 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2133 ce.width = SH_MAX_W(win);
2134 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2135 ce.height = SH_MAX_H(win);
2136 }
2137
2138 /* resize increment. */
2139 if (SH_INC(win)) {
2140 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2141 ce.width -= (ce.width - SH_MIN_W(win)) %
2142 SH_INC_W(win);
2143 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2144 ce.height -= (ce.height - SH_MIN_H(win)) %
2145 SH_INC_H(win);
2146 }
2147
2148 /* adjust x and y for requested border_width. */
2149 ce.x += BORDER(win) - ev->border_width;
2150 ce.y += BORDER(win) - ev->border_width;
2151 ce.border_width = ev->border_width;
2152 ce.above_sibling = ev->above;
2153 }
2154
2155 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2156 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2157 ce.y, ce.width, ce.height, ce.border_width);
2158
2159 xcb_send_event(conn, False, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2160 (char *)&ce);
2161 xcb_flush(conn);
2162 }
2163
2164 int
2165 count_win(struct workspace *ws, int count_transient)
2166 {
2167 struct ws_win *win;
2168 int count = 0;
2169
2170 TAILQ_FOREACH(win, &ws->winlist, entry) {
2171 if (count_transient == 0 && win->floating)
2172 continue;
2173 if (count_transient == 0 && win->transient)
2174 continue;
2175 if (win->iconic)
2176 continue;
2177 count++;
2178 }
2179 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2180
2181 return (count);
2182 }
2183
2184 void
2185 quit(struct swm_region *r, union arg *args)
2186 {
2187 DNPRINTF(SWM_D_MISC, "quit\n");
2188 running = 0;
2189 }
2190
2191 void
2192 unmap_window(struct ws_win *win)
2193 {
2194 if (win == NULL)
2195 return;
2196
2197 /* don't unmap again */
2198 if (getstate(win->id) == XCB_ICCCM_WM_STATE_ICONIC)
2199 return;
2200
2201 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2202
2203 xcb_unmap_window(conn, win->id);
2204 xcb_change_window_attributes(conn, win->id,
2205 XCB_CW_BORDER_PIXEL, &win->s->c[SWM_S_COLOR_UNFOCUS].color);
2206 }
2207
2208 void
2209 unmap_all(void)
2210 {
2211 struct ws_win *win;
2212 int i, j, num_screens;
2213
2214 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2215 for (i = 0; i < num_screens; i++)
2216 for (j = 0; j < workspace_limit; j++)
2217 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2218 unmap_window(win);
2219 }
2220
2221 void
2222 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2223 {
2224 xcb_key_press_event_t event;
2225 xcb_key_symbols_t *syms;
2226 xcb_keycode_t *keycode;
2227
2228 if (win == NULL)
2229 return;
2230
2231 syms = xcb_key_symbols_alloc(conn);
2232 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2233
2234 event.event = win->id;
2235 event.root = win->s->root;
2236 event.child = XCB_WINDOW_NONE;
2237 event.time = XCB_CURRENT_TIME;
2238 event.event_x = X(win);
2239 event.event_y = Y(win);
2240 event.root_x = 1;
2241 event.root_y = 1;
2242 event.same_screen = True;
2243 event.detail = *keycode;
2244 event.state = modifiers;
2245
2246 event.response_type = XCB_KEY_PRESS;
2247 xcb_send_event(conn, win->id, True,
2248 XCB_EVENT_MASK_KEY_PRESS, (char *)&event);
2249
2250 event.response_type = XCB_KEY_RELEASE;
2251 xcb_send_event(conn, win->id, True,
2252 XCB_EVENT_MASK_KEY_RELEASE, (char *)&event);
2253 xcb_flush(conn);
2254
2255 xcb_key_symbols_free(syms);
2256 }
2257
2258 void
2259 restart(struct swm_region *r, union arg *args)
2260 {
2261 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2262
2263 /* disable alarm because the following code may not be interrupted */
2264 alarm(0);
2265 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2266 err(1, "can't disable alarm");
2267
2268 bar_extra_stop();
2269 bar_extra = 1;
2270 unmap_all();
2271 XCloseDisplay(display);
2272 execvp(start_argv[0], start_argv);
2273 warn("execvp failed");
2274 quit(NULL, NULL);
2275 }
2276
2277 struct swm_region *
2278 root_to_region(xcb_window_t root)
2279 {
2280 struct swm_region *r = NULL;
2281 int i, num_screens;
2282 xcb_query_pointer_cookie_t qpc;
2283 xcb_query_pointer_reply_t *qpr;
2284
2285 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2286
2287 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2288 for (i = 0; i < num_screens; i++)
2289 if (screens[i].root == root)
2290 break;
2291
2292 qpc = xcb_query_pointer(conn, screens[i].root);
2293 qpr = xcb_query_pointer_reply(conn, qpc, NULL);
2294
2295 if (qpr) {
2296 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: (%d,%d)\n",
2297 qpr->root_x, qpr->root_y);
2298 /* choose a region based on pointer location */
2299 TAILQ_FOREACH(r, &screens[i].rl, entry)
2300 if (X(r) <= qpr->root_x && qpr->root_x < MAX_X(r) &&
2301 Y(r) <= qpr->root_y && qpr->root_y < MAX_Y(r))
2302 break;
2303 free(qpr);
2304 }
2305
2306 if (r == NULL)
2307 r = TAILQ_FIRST(&screens[i].rl);
2308
2309 return (r);
2310 }
2311
2312 struct ws_win *
2313 find_unmanaged_window(xcb_window_t id)
2314 {
2315 struct ws_win *win;
2316 int i, j, num_screens;
2317
2318 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2319 for (i = 0; i < num_screens; i++)
2320 for (j = 0; j < workspace_limit; j++)
2321 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2322 entry)
2323 if (id == win->id)
2324 return (win);
2325 return (NULL);
2326 }
2327
2328 struct ws_win *
2329 find_window(xcb_window_t id)
2330 {
2331 struct ws_win *win;
2332 int i, j, num_screens;
2333 xcb_query_tree_cookie_t c;
2334 xcb_query_tree_reply_t *r;
2335
2336 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2337 for (i = 0; i < num_screens; i++)
2338 for (j = 0; j < workspace_limit; j++)
2339 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2340 if (id == win->id)
2341 return (win);
2342
2343 c = xcb_query_tree(conn, id);
2344 r = xcb_query_tree_reply(conn, c, NULL);
2345 if (!r)
2346 return (NULL);
2347
2348 /* if we were looking for the parent return that window instead */
2349 if (r->parent == 0 || r->root == r->parent)
2350 return (NULL);
2351
2352 /* look for parent */
2353 for (i = 0; i < num_screens; i++)
2354 for (j = 0; j < workspace_limit; j++)
2355 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2356 if (r->parent == win->id) {
2357 free(r);
2358 return (win);
2359 }
2360
2361 free(r);
2362 return (NULL);
2363 }
2364
2365 void
2366 spawn(int ws_idx, union arg *args, int close_fd)
2367 {
2368 int fd;
2369 char *ret = NULL;
2370
2371 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2372
2373 if (display)
2374 close(xcb_get_file_descriptor(conn));
2375
2376 setenv("LD_PRELOAD", SWM_LIB, 1);
2377
2378 if (asprintf(&ret, "%d", ws_idx) == -1) {
2379 warn("spawn: asprintf SWM_WS");
2380 _exit(1);
2381 }
2382 setenv("_SWM_WS", ret, 1);
2383 free(ret);
2384 ret = NULL;
2385
2386 if (asprintf(&ret, "%d", getpid()) == -1) {
2387 warn("spawn: asprintf _SWM_PID");
2388 _exit(1);
2389 }
2390 setenv("_SWM_PID", ret, 1);
2391 free(ret);
2392 ret = NULL;
2393
2394 if (setsid() == -1) {
2395 warn("spawn: setsid");
2396 _exit(1);
2397 }
2398
2399 if (close_fd) {
2400 /*
2401 * close stdin and stdout to prevent interaction between apps
2402 * and the baraction script
2403 * leave stderr open to record errors
2404 */
2405 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2406 warn("spawn: open");
2407 _exit(1);
2408 }
2409 dup2(fd, STDIN_FILENO);
2410 dup2(fd, STDOUT_FILENO);
2411 if (fd > 2)
2412 close(fd);
2413 }
2414
2415 execvp(args->argv[0], args->argv);
2416
2417 warn("spawn: execvp");
2418 _exit(1);
2419 }
2420
2421 void
2422 kill_refs(struct ws_win *win)
2423 {
2424 int i, x, num_screens;
2425 struct swm_region *r;
2426 struct workspace *ws;
2427
2428 if (win == NULL)
2429 return;
2430
2431 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2432 for (i = 0; i < num_screens; i++)
2433 TAILQ_FOREACH(r, &screens[i].rl, entry)
2434 for (x = 0; x < workspace_limit; x++) {
2435 ws = &r->s->ws[x];
2436 if (win == ws->focus)
2437 ws->focus = NULL;
2438 if (win == ws->focus_prev)
2439 ws->focus_prev = NULL;
2440 }
2441 }
2442
2443 int
2444 validate_win(struct ws_win *testwin)
2445 {
2446 struct ws_win *win;
2447 struct workspace *ws;
2448 struct swm_region *r;
2449 int i, x, num_screens;
2450
2451 if (testwin == NULL)
2452 return (0);
2453
2454 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2455 for (i = 0; i < num_screens; i++)
2456 TAILQ_FOREACH(r, &screens[i].rl, entry)
2457 for (x = 0; x < workspace_limit; x++) {
2458 ws = &r->s->ws[x];
2459 TAILQ_FOREACH(win, &ws->winlist, entry)
2460 if (win == testwin)
2461 return (0);
2462 }
2463 return (1);
2464 }
2465
2466 int
2467 validate_ws(struct workspace *testws)
2468 {
2469 struct swm_region *r;
2470 struct workspace *ws;
2471 int i, x, num_screens;
2472
2473 /* validate all ws */
2474 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2475 for (i = 0; i < num_screens; i++)
2476 TAILQ_FOREACH(r, &screens[i].rl, entry)
2477 for (x = 0; x < workspace_limit; x++) {
2478 ws = &r->s->ws[x];
2479 if (ws == testws)
2480 return (0);
2481 }
2482 return (1);
2483 }
2484
2485 void
2486 unfocus_win(struct ws_win *win)
2487 {
2488 XEvent cne;
2489 xcb_window_t none = XCB_WINDOW_NONE;
2490
2491 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
2492
2493 if (win == NULL)
2494 return;
2495 if (win->ws == NULL)
2496 return;
2497
2498 if (validate_ws(win->ws))
2499 return; /* XXX this gets hit with thunderbird, needs fixing */
2500
2501 if (win->ws->r == NULL)
2502 return;
2503
2504 if (validate_win(win)) {
2505 kill_refs(win);
2506 return;
2507 }
2508
2509 if (win->ws->focus == win) {
2510 win->ws->focus = NULL;
2511 win->ws->focus_prev = win;
2512 }
2513
2514 if (validate_win(win->ws->focus)) {
2515 kill_refs(win->ws->focus);
2516 win->ws->focus = NULL;
2517 }
2518 if (validate_win(win->ws->focus_prev)) {
2519 kill_refs(win->ws->focus_prev);
2520 win->ws->focus_prev = NULL;
2521 }
2522
2523 /* drain all previous unfocus events */
2524 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2525 ;
2526
2527 grabbuttons(win, 0);
2528 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2529 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2530 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2531 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2532 &none);
2533 }
2534
2535 void
2536 unfocus_all(void)
2537 {
2538 struct ws_win *win;
2539 int i, j, num_screens;
2540
2541 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2542
2543 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2544 for (i = 0; i < num_screens; i++)
2545 for (j = 0; j < workspace_limit; j++)
2546 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2547 unfocus_win(win);
2548 }
2549
2550 void
2551 focus_win(struct ws_win *win)
2552 {
2553 XEvent cne;
2554 struct ws_win *cfw = NULL;
2555 xcb_get_input_focus_cookie_t c;
2556 xcb_get_input_focus_reply_t *r;
2557 xcb_window_t cur_focus = XCB_WINDOW_NONE;
2558
2559 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2560
2561 if (win == NULL)
2562 return;
2563 if (win->ws == NULL)
2564 return;
2565
2566 if (validate_ws(win->ws))
2567 return; /* XXX this gets hit with thunderbird, needs fixing */
2568
2569 if (validate_win(win)) {
2570 kill_refs(win);
2571 return;
2572 }
2573
2574 if (validate_win(win)) {
2575 kill_refs(win);
2576 return;
2577 }
2578
2579 c = xcb_get_input_focus(conn);
2580 r = xcb_get_input_focus_reply(conn, c, NULL);
2581 if (r) {
2582 cur_focus = r->focus;
2583 free(r);
2584 }
2585 if ((cfw = find_window(cur_focus)) != NULL)
2586 unfocus_win(cfw);
2587 else {
2588 /* use larger hammer since the window was killed somehow */
2589 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2590 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2591 xcb_change_window_attributes(conn, cfw->id,
2592 XCB_CW_BORDER_PIXEL,
2593 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2594 }
2595
2596 win->ws->focus = win;
2597
2598 if (win->ws->r != NULL) {
2599 /* drain all previous focus events */
2600 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2601 ;
2602
2603 if (win->java == 0)
2604 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2605 win->id, XCB_CURRENT_TIME);
2606 grabbuttons(win, 1);
2607 xcb_change_window_attributes(conn, win->id,
2608 XCB_CW_BORDER_PIXEL,
2609 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2610 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2611 win->ws->always_raise)
2612 map_window_raised(win->id);
2613
2614 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2615 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2616 &win->id);
2617 }
2618
2619 bar_update();
2620 }
2621
2622 void
2623 switchws(struct swm_region *r, union arg *args)
2624 {
2625 int wsid = args->id, unmap_old = 0;
2626 struct swm_region *this_r, *other_r;
2627 struct ws_win *win;
2628 struct workspace *new_ws, *old_ws;
2629 union arg a;
2630
2631 if (!(r && r->s))
2632 return;
2633
2634 if (wsid >= workspace_limit)
2635 return;
2636
2637 this_r = r;
2638 old_ws = this_r->ws;
2639 new_ws = &this_r->s->ws[wsid];
2640
2641 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2642 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2643
2644 if (new_ws == NULL || old_ws == NULL)
2645 return;
2646 if (new_ws == old_ws)
2647 return;
2648
2649 other_r = new_ws->r;
2650 if (other_r == NULL) {
2651 /* the other workspace is hidden, hide this one */
2652 old_ws->r = NULL;
2653 unmap_old = 1;
2654 } else {
2655 /* the other ws is visible in another region, exchange them */
2656 other_r->ws_prior = new_ws;
2657 other_r->ws = old_ws;
2658 old_ws->r = other_r;
2659 }
2660 this_r->ws_prior = old_ws;
2661 this_r->ws = new_ws;
2662 new_ws->r = this_r;
2663
2664 /* this is needed so that we can click on a window after a restart */
2665 unfocus_all();
2666
2667 stack();
2668 a.id = SWM_ARG_ID_FOCUSCUR;
2669 focus(new_ws->r, &a);
2670
2671 /* unmap old windows */
2672 if (unmap_old)
2673 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2674 unmap_window(win);
2675
2676 if (focus_mode == SWM_FOCUS_DEFAULT)
2677 drain_enter_notify();
2678 }
2679
2680 void
2681 cyclews(struct swm_region *r, union arg *args)
2682 {
2683 union arg a;
2684 struct swm_screen *s = r->s;
2685 int cycle_all = 0;
2686
2687 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2688 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2689
2690 a.id = r->ws->idx;
2691 do {
2692 switch (args->id) {
2693 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2694 cycle_all = 1;
2695 /* FALLTHROUGH */
2696 case SWM_ARG_ID_CYCLEWS_UP:
2697 if (a.id < workspace_limit - 1)
2698 a.id++;
2699 else
2700 a.id = 0;
2701 break;
2702 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2703 cycle_all = 1;
2704 /* FALLTHROUGH */
2705 case SWM_ARG_ID_CYCLEWS_DOWN:
2706 if (a.id > 0)
2707 a.id--;
2708 else
2709 a.id = workspace_limit - 1;
2710 break;
2711 default:
2712 return;
2713 };
2714
2715 if (!cycle_all &&
2716 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2717 continue;
2718 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2719 continue;
2720
2721 switchws(r, &a);
2722 } while (a.id != r->ws->idx);
2723 }
2724
2725 void
2726 priorws(struct swm_region *r, union arg *args)
2727 {
2728 union arg a;
2729
2730 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2731 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2732
2733 if (r->ws_prior == NULL)
2734 return;
2735
2736 a.id = r->ws_prior->idx;
2737 switchws(r, &a);
2738 }
2739
2740 void
2741 cyclescr(struct swm_region *r, union arg *args)
2742 {
2743 struct swm_region *rr = NULL;
2744 union arg a;
2745 int i, x, y, num_screens;
2746
2747 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
2748 /* do nothing if we don't have more than one screen */
2749 if (!(num_screens > 1 || outputs > 1))
2750 return;
2751
2752 i = r->s->idx;
2753 switch (args->id) {
2754 case SWM_ARG_ID_CYCLESC_UP:
2755 rr = TAILQ_NEXT(r, entry);
2756 if (rr == NULL)
2757 rr = TAILQ_FIRST(&screens[i].rl);
2758 break;
2759 case SWM_ARG_ID_CYCLESC_DOWN:
2760 rr = TAILQ_PREV(r, swm_region_list, entry);
2761 if (rr == NULL)
2762 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2763 break;
2764 default:
2765 return;
2766 };
2767 if (rr == NULL)
2768 return;
2769
2770 /* move mouse to region */
2771 x = X(rr) + 1;
2772 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2773 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0, 0,
2774 x, y);
2775
2776 a.id = SWM_ARG_ID_FOCUSCUR;
2777 focus(rr, &a);
2778
2779 if (rr->ws->focus) {
2780 /* move to focus window */
2781 x = X(rr->ws->focus) + 1;
2782 y = Y(rr->ws->focus) + 1;
2783 xcb_warp_pointer(conn, XCB_WINDOW_NONE, rr->s[i].root, 0, 0, 0,
2784 0, x, y);
2785 }
2786 }
2787
2788 void
2789 sort_windows(struct ws_win_list *wl)
2790 {
2791 struct ws_win *win, *parent, *nxt;
2792
2793 if (wl == NULL)
2794 return;
2795
2796 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2797 nxt = TAILQ_NEXT(win, entry);
2798 if (win->transient) {
2799 parent = find_window(win->transient);
2800 if (parent == NULL) {
2801 warnx("not possible bug");
2802 continue;
2803 }
2804 TAILQ_REMOVE(wl, win, entry);
2805 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2806 }
2807 }
2808
2809 }
2810
2811 void
2812 swapwin(struct swm_region *r, union arg *args)
2813 {
2814 struct ws_win *target, *source;
2815 struct ws_win *cur_focus;
2816 struct ws_win_list *wl;
2817
2818
2819 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2820 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2821
2822 cur_focus = r->ws->focus;
2823 if (cur_focus == NULL)
2824 return;
2825
2826 source = cur_focus;
2827 wl = &source->ws->winlist;
2828
2829 switch (args->id) {
2830 case SWM_ARG_ID_SWAPPREV:
2831 if (source->transient)
2832 source = find_window(source->transient);
2833 target = TAILQ_PREV(source, ws_win_list, entry);
2834 if (target && target->transient)
2835 target = find_window(target->transient);
2836 TAILQ_REMOVE(wl, source, entry);
2837 if (target == NULL)
2838 TAILQ_INSERT_TAIL(wl, source, entry);
2839 else
2840 TAILQ_INSERT_BEFORE(target, source, entry);
2841 break;
2842 case SWM_ARG_ID_SWAPNEXT:
2843 target = TAILQ_NEXT(source, entry);
2844 /* move the parent and let the sort handle the move */
2845 if (source->transient)
2846 source = find_window(source->transient);
2847 TAILQ_REMOVE(wl, source, entry);
2848 if (target == NULL)
2849 TAILQ_INSERT_HEAD(wl, source, entry);
2850 else
2851 TAILQ_INSERT_AFTER(wl, target, source, entry);
2852 break;
2853 case SWM_ARG_ID_SWAPMAIN:
2854 target = TAILQ_FIRST(wl);
2855 if (target == source) {
2856 if (source->ws->focus_prev != NULL &&
2857 source->ws->focus_prev != target)
2858 source = source->ws->focus_prev;
2859 else
2860 return;
2861 }
2862 if (target == NULL || source == NULL)
2863 return;
2864 source->ws->focus_prev = target;
2865 TAILQ_REMOVE(wl, target, entry);
2866 TAILQ_INSERT_BEFORE(source, target, entry);
2867 TAILQ_REMOVE(wl, source, entry);
2868 TAILQ_INSERT_HEAD(wl, source, entry);
2869 break;
2870 case SWM_ARG_ID_MOVELAST:
2871 TAILQ_REMOVE(wl, source, entry);
2872 TAILQ_INSERT_TAIL(wl, source, entry);
2873 break;
2874 default:
2875 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2876 return;
2877 }
2878
2879 sort_windows(wl);
2880
2881 stack();
2882 }
2883
2884 void
2885 focus_prev(struct ws_win *win)
2886 {
2887 struct ws_win *winfocus = NULL;
2888 struct ws_win *cur_focus = NULL;
2889 struct ws_win_list *wl = NULL;
2890 struct workspace *ws = NULL;
2891
2892 if (!(win && win->ws))
2893 return;
2894
2895 ws = win->ws;
2896 wl = &ws->winlist;
2897 cur_focus = ws->focus;
2898
2899 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2900 WINID(win), WINID(cur_focus));
2901
2902 /* pickle, just focus on whatever */
2903 if (cur_focus == NULL) {
2904 /* use prev_focus if valid */
2905 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2906 find_window(WINID(ws->focus_prev)))
2907 winfocus = ws->focus_prev;
2908 goto done;
2909 }
2910
2911 /* if transient focus on parent */
2912 if (cur_focus->transient) {
2913 winfocus = find_window(cur_focus->transient);
2914 goto done;
2915 }
2916
2917 /* if in max_stack try harder */
2918 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2919 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2920 if (cur_focus != ws->focus_prev)
2921 winfocus = ws->focus_prev;
2922 else
2923 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2924 if (winfocus)
2925 goto done;
2926 }
2927
2928 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2929
2930 if (winfocus == NULL || winfocus == win) {
2931 switch (focus_close) {
2932 case SWM_STACK_BOTTOM:
2933 winfocus = TAILQ_FIRST(wl);
2934 break;
2935 case SWM_STACK_TOP:
2936 winfocus = TAILQ_LAST(wl, ws_win_list);
2937 break;
2938 case SWM_STACK_ABOVE:
2939 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2940 if (focus_close_wrap)
2941 winfocus = TAILQ_FIRST(wl);
2942 else
2943 winfocus = TAILQ_PREV(cur_focus,
2944 ws_win_list, entry);
2945 }
2946 break;
2947 case SWM_STACK_BELOW:
2948 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2949 entry)) == NULL) {
2950 if (focus_close_wrap)
2951 winfocus = TAILQ_LAST(wl, ws_win_list);
2952 else
2953 winfocus = TAILQ_NEXT(cur_focus, entry);
2954 }
2955 break;
2956 }
2957 }
2958 done:
2959 if (winfocus == NULL) {
2960 if (focus_default == SWM_STACK_TOP)
2961 winfocus = TAILQ_LAST(wl, ws_win_list);
2962 else
2963 winfocus = TAILQ_FIRST(wl);
2964 }
2965
2966 kill_refs(win);
2967 focus_magic(winfocus);
2968 }
2969
2970 void
2971 focus(struct swm_region *r, union arg *args)
2972 {
2973 struct ws_win *winfocus = NULL, *head;
2974 struct ws_win *cur_focus = NULL;
2975 struct ws_win_list *wl = NULL;
2976 struct workspace *ws = NULL;
2977 int all_iconics;
2978
2979 if (!(r && r->ws))
2980 return;
2981
2982 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2983
2984 /* treat FOCUS_CUR special */
2985 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2986 if (r->ws->focus && r->ws->focus->iconic == 0)
2987 winfocus = r->ws->focus;
2988 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2989 winfocus = r->ws->focus_prev;
2990 else
2991 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2992 if (winfocus->iconic == 0)
2993 break;
2994
2995 focus_magic(winfocus);
2996 return;
2997 }
2998
2999 if ((cur_focus = r->ws->focus) == NULL)
3000 return;
3001 ws = r->ws;
3002 wl = &ws->winlist;
3003 if (TAILQ_EMPTY(wl))
3004 return;
3005 /* make sure there is at least one uniconified window */
3006 all_iconics = 1;
3007 TAILQ_FOREACH(winfocus, wl, entry)
3008 if (winfocus->iconic == 0) {
3009 all_iconics = 0;
3010 break;
3011 }
3012 if (all_iconics)
3013 return;
3014
3015 switch (args->id) {
3016 case SWM_ARG_ID_FOCUSPREV:
3017 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3018 if (head == NULL)
3019 head = TAILQ_LAST(wl, ws_win_list);
3020 winfocus = head;
3021 if (WINID(winfocus) == cur_focus->transient) {
3022 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3023 if (head == NULL)
3024 head = TAILQ_LAST(wl, ws_win_list);
3025 winfocus = head;
3026 }
3027
3028 /* skip iconics */
3029 if (winfocus && winfocus->iconic) {
3030 while (winfocus != cur_focus) {
3031 if (winfocus == NULL)
3032 winfocus = TAILQ_LAST(wl, ws_win_list);
3033 if (winfocus->iconic == 0)
3034 break;
3035 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3036 entry);
3037 }
3038 }
3039 break;
3040
3041 case SWM_ARG_ID_FOCUSNEXT:
3042 head = TAILQ_NEXT(cur_focus, entry);
3043 if (head == NULL)
3044 head = TAILQ_FIRST(wl);
3045 winfocus = head;
3046
3047 /* skip iconics */
3048 if (winfocus && winfocus->iconic) {
3049 while (winfocus != cur_focus) {
3050 if (winfocus == NULL)
3051 winfocus = TAILQ_FIRST(wl);
3052 if (winfocus->iconic == 0)
3053 break;
3054 winfocus = TAILQ_NEXT(winfocus, entry);
3055 }
3056 }
3057 break;
3058
3059 case SWM_ARG_ID_FOCUSMAIN:
3060 winfocus = TAILQ_FIRST(wl);
3061 if (winfocus == cur_focus)
3062 winfocus = cur_focus->ws->focus_prev;
3063 break;
3064
3065 default:
3066 return;
3067 }
3068
3069 focus_magic(winfocus);
3070 }
3071
3072 void
3073 cycle_layout(struct swm_region *r, union arg *args)
3074 {
3075 struct workspace *ws = r->ws;
3076 union arg a;
3077
3078 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3079
3080 ws->cur_layout++;
3081 if (ws->cur_layout->l_stack == NULL)
3082 ws->cur_layout = &layouts[0];
3083
3084 stack();
3085 if (focus_mode == SWM_FOCUS_DEFAULT)
3086 drain_enter_notify();
3087 a.id = SWM_ARG_ID_FOCUSCUR;
3088 focus(r, &a);
3089 }
3090
3091 void
3092 stack_config(struct swm_region *r, union arg *args)
3093 {
3094 struct workspace *ws = r->ws;
3095
3096 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3097 args->id, ws->idx);
3098
3099 if (ws->cur_layout->l_config != NULL)
3100 ws->cur_layout->l_config(ws, args->id);
3101
3102 if (args->id != SWM_ARG_ID_STACKINIT)
3103 stack();
3104 bar_update();
3105 }
3106
3107 void
3108 stack(void) {
3109 struct swm_geometry g;
3110 struct swm_region *r;
3111 int i, num_screens;
3112 #ifdef SWM_DEBUG
3113 int j;
3114 #endif
3115
3116 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3117
3118 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3119 for (i = 0; i < num_screens; i++) {
3120 #ifdef SWM_DEBUG
3121 j = 0;
3122 #endif
3123 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3124 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3125 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3126
3127 /* start with screen geometry, adjust for bar */
3128 g = r->g;
3129 g.w -= 2 * border_width;
3130 g.h -= 2 * border_width;
3131 if (bar_enabled) {
3132 if (!bar_at_bottom)
3133 g.y += bar_height;
3134 g.h -= bar_height;
3135 }
3136 r->ws->cur_layout->l_stack(r->ws, &g);
3137 r->ws->cur_layout->l_string(r->ws);
3138 /* save r so we can track region changes */
3139 r->ws->old_r = r;
3140 }
3141 }
3142 if (font_adjusted)
3143 font_adjusted--;
3144
3145 if (focus_mode == SWM_FOCUS_DEFAULT)
3146 drain_enter_notify();
3147
3148 DNPRINTF(SWM_D_STACK, "stack: end\n");
3149 }
3150
3151 void
3152 store_float_geom(struct ws_win *win, struct swm_region *r)
3153 {
3154 /* retain window geom and region geom */
3155 win->g_float = win->g;
3156 win->g_float.x -= X(r);
3157 win->g_float.y -= Y(r);
3158 win->g_floatvalid = 1;
3159 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3160 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3161 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3162 win->g_float.w, win->g_float.h);
3163 }
3164
3165 void
3166 stack_floater(struct ws_win *win, struct swm_region *r)
3167 {
3168 if (win == NULL)
3169 return;
3170
3171 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3172
3173 /*
3174 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3175 * geom on ws switches or return from max mode
3176 */
3177 if (win->g_floatvalid && (win->floatmaxed || (r != r->ws->old_r &&
3178 !(win->ewmh_flags & EWMH_F_FULLSCREEN)))) {
3179 /* refloat at last floating relative position */
3180 win->g = win->g_float;
3181 X(win) += X(r);
3182 Y(win) += Y(r);
3183 }
3184
3185 win->floatmaxed = 0;
3186
3187 /*
3188 * if set to fullscreen mode, configure window to maximum size.
3189 */
3190 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3191 if (!win->g_floatvalid)
3192 store_float_geom(win, win->ws->r);
3193
3194 win->g = win->ws->r->g;
3195 }
3196
3197 /*
3198 * remove border on fullscreen floater when in fullscreen mode or when
3199 * the quirk is present.
3200 */
3201 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3202 ((win->quirks & SWM_Q_FULLSCREEN) &&
3203 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3204 if (win->bordered) {
3205 win->bordered = 0;
3206 X(win) += border_width;
3207 Y(win) += border_width;
3208 }
3209 } else if (!win->bordered) {
3210 win->bordered = 1;
3211 X(win) -= border_width;
3212 Y(win) -= border_width;
3213 }
3214
3215 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3216 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3217 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3218 }
3219
3220 if (!win->manual) {
3221 /*
3222 * floaters and transients are auto-centred unless moved
3223 * or resized
3224 */
3225 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3226 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3227 }
3228
3229 /* keep window within region bounds */
3230 constrain_window(win, r, 0);
3231
3232 update_window(win);
3233 }
3234
3235 /*
3236 * Send keystrokes to terminal to decrease/increase the font size as the
3237 * window size changes.
3238 */
3239 void
3240 adjust_font(struct ws_win *win)
3241 {
3242 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3243 win->floating || win->transient)
3244 return;
3245
3246 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3247 WIDTH(win) / win->sh.width_inc < term_width &&
3248 win->font_steps < SWM_MAX_FONT_STEPS) {
3249 win->font_size_boundary[win->font_steps] =
3250 (win->sh.width_inc * term_width) + win->sh.base_width;
3251 win->font_steps++;
3252 font_adjusted++;
3253 win->last_inc = win->sh.width_inc;
3254 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3255 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3256 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3257 win->font_steps--;
3258 font_adjusted++;
3259 win->last_inc = win->sh.width_inc;
3260 fake_keypress(win, XK_KP_Add, ShiftMask);
3261 }
3262 }
3263
3264 #define SWAPXY(g) do { \
3265 int tmp; \
3266 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3267 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3268 } while (0)
3269 void
3270 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3271 {
3272 XWindowAttributes wa;
3273 struct swm_geometry win_g, r_g = *g;
3274 struct ws_win *win, *fs_win = NULL;
3275 int i, j, s, stacks;
3276 int w_inc = 1, h_inc, w_base = 1, h_base;
3277 int hrh, extra = 0, h_slice, last_h = 0;
3278 int split, colno, winno, mwin, msize, mscale;
3279 int remain, missing, v_slice, reconfigure;
3280 int bordered = 1;
3281
3282 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3283 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3284
3285 winno = count_win(ws, 0);
3286 if (winno == 0 && count_win(ws, 1) == 0)
3287 return;
3288
3289 TAILQ_FOREACH(win, &ws->winlist, entry)
3290 if (win->transient == 0 && win->floating == 0
3291 && win->iconic == 0)
3292 break;
3293
3294 if (win == NULL)
3295 goto notiles;
3296
3297 if (rot) {
3298 w_inc = win->sh.width_inc;
3299 w_base = win->sh.base_width;
3300 mwin = ws->l_state.horizontal_mwin;
3301 mscale = ws->l_state.horizontal_msize;
3302 stacks = ws->l_state.horizontal_stacks;
3303 SWAPXY(&r_g);
3304 } else {
3305 w_inc = win->sh.height_inc;
3306 w_base = win->sh.base_height;
3307 mwin = ws->l_state.vertical_mwin;
3308 mscale = ws->l_state.vertical_msize;
3309 stacks = ws->l_state.vertical_stacks;
3310 }
3311 win_g = r_g;
3312
3313 if (stacks > winno - mwin)
3314 stacks = winno - mwin;
3315 if (stacks < 1)
3316 stacks = 1;
3317
3318 h_slice = r_g.h / SWM_H_SLICE;
3319 if (mwin && winno > mwin) {
3320 v_slice = r_g.w / SWM_V_SLICE;
3321
3322 split = mwin;
3323 colno = split;
3324 win_g.w = v_slice * mscale;
3325
3326 if (w_inc > 1 && w_inc < v_slice) {
3327 /* adjust for window's requested size increment */
3328 remain = (win_g.w - w_base) % w_inc;
3329 win_g.w -= remain;
3330 }
3331
3332 msize = win_g.w;
3333 if (flip)
3334 win_g.x += r_g.w - msize;
3335 } else {
3336 msize = -2;
3337 colno = split = winno / stacks;
3338 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3339 2 * border_width) / stacks);
3340 }
3341 hrh = r_g.h / colno;
3342 extra = r_g.h - (colno * hrh);
3343 win_g.h = hrh - 2 * border_width;
3344
3345 /* stack all the tiled windows */
3346 i = j = 0, s = stacks;
3347 TAILQ_FOREACH(win, &ws->winlist, entry) {
3348 if (win->transient != 0 || win->floating != 0)
3349 continue;
3350 if (win->iconic != 0)
3351 continue;
3352
3353 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3354 fs_win = win;
3355 continue;
3356 }
3357
3358 if (split && i == split) {
3359 colno = (winno - mwin) / stacks;
3360 if (s <= (winno - mwin) % stacks)
3361 colno++;
3362 split = split + colno;
3363 hrh = (r_g.h / colno);
3364 extra = r_g.h - (colno * hrh);
3365 if (flip)
3366 win_g.x = r_g.x;
3367 else
3368 win_g.x += win_g.w + 2 * border_width;
3369 win_g.w = (r_g.w - msize -
3370 (stacks * 2 * border_width)) / stacks;
3371 if (s == 1)
3372 win_g.w += (r_g.w - msize -
3373 (stacks * 2 * border_width)) % stacks;
3374 s--;
3375 j = 0;
3376 }
3377 win_g.h = hrh - 2 * border_width;
3378 if (rot) {
3379 h_inc = win->sh.width_inc;
3380 h_base = win->sh.base_width;
3381 } else {
3382 h_inc = win->sh.height_inc;
3383 h_base = win->sh.base_height;
3384 }
3385 if (j == colno - 1) {
3386 win_g.h = hrh + extra;
3387 } else if (h_inc > 1 && h_inc < h_slice) {
3388 /* adjust for window's requested size increment */
3389 remain = (win_g.h - h_base) % h_inc;
3390 missing = h_inc - remain;
3391
3392 if (missing <= extra || j == 0) {
3393 extra -= missing;
3394 win_g.h += missing;
3395 } else {
3396 win_g.h -= remain;
3397 extra += remain;
3398 }
3399 }
3400
3401 if (j == 0)
3402 win_g.y = r_g.y;
3403 else
3404 win_g.y += last_h + 2 * border_width;
3405
3406 if (disable_border && bar_enabled == 0 && winno == 1){
3407 bordered = 0;
3408 win_g.w += 2 * border_width;
3409 win_g.h += 2 * border_width;
3410 } else {
3411 bordered = 1;
3412 }
3413 if (rot) {
3414 if (X(win) != win_g.y || Y(win) != win_g.x ||
3415 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3416 reconfigure = 1;
3417 X(win) = win_g.y;
3418 Y(win) = win_g.x;
3419 WIDTH(win) = win_g.h;
3420 HEIGHT(win) = win_g.w;
3421 }
3422 } else {
3423 if (X(win) != win_g.x || Y(win) != win_g.y ||
3424 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3425 reconfigure = 1;
3426 X(win) = win_g.x;
3427 Y(win) = win_g.y;
3428 WIDTH(win) = win_g.w;
3429 HEIGHT(win) = win_g.h;
3430 }
3431 }
3432
3433 if (bordered != win->bordered) {
3434 reconfigure = 1;
3435 win->bordered = bordered;
3436 }
3437
3438 if (reconfigure) {
3439 adjust_font(win);
3440 update_window(win);
3441 }
3442
3443 if (XGetWindowAttributes(display, win->id, &wa))
3444 if (wa.map_state == IsUnmapped)
3445 map_window_raised(win->id);
3446
3447 last_h = win_g.h;
3448 i++;
3449 j++;
3450 }
3451
3452 notiles:
3453 /* now, stack all the floaters and transients */
3454 TAILQ_FOREACH(win, &ws->winlist, entry) {
3455 if (win->transient == 0 && win->floating == 0)
3456 continue;
3457 if (win->iconic == 1)
3458 continue;
3459 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3460 fs_win = win;
3461 continue;
3462 }
3463
3464 stack_floater(win, ws->r);
3465 map_window_raised(win->id);
3466 }
3467
3468 if (fs_win) {
3469 stack_floater(fs_win, ws->r);
3470 map_window_raised(fs_win->id);
3471 }
3472 }
3473
3474 void
3475 vertical_config(struct workspace *ws, int id)
3476 {
3477 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3478 id, ws->idx);
3479
3480 switch (id) {
3481 case SWM_ARG_ID_STACKRESET:
3482 case SWM_ARG_ID_STACKINIT:
3483 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3484 ws->l_state.vertical_mwin = 1;
3485 ws->l_state.vertical_stacks = 1;
3486 break;
3487 case SWM_ARG_ID_MASTERSHRINK:
3488 if (ws->l_state.vertical_msize > 1)
3489 ws->l_state.vertical_msize--;
3490 break;
3491 case SWM_ARG_ID_MASTERGROW:
3492 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3493 ws->l_state.vertical_msize++;
3494 break;
3495 case SWM_ARG_ID_MASTERADD:
3496 ws->l_state.vertical_mwin++;
3497 break;
3498 case SWM_ARG_ID_MASTERDEL:
3499 if (ws->l_state.vertical_mwin > 0)
3500 ws->l_state.vertical_mwin--;
3501 break;
3502 case SWM_ARG_ID_STACKINC:
3503 ws->l_state.vertical_stacks++;
3504 break;
3505 case SWM_ARG_ID_STACKDEC:
3506 if (ws->l_state.vertical_stacks > 1)
3507 ws->l_state.vertical_stacks--;
3508 break;
3509 case SWM_ARG_ID_FLIPLAYOUT:
3510 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3511 break;
3512 default:
3513 return;
3514 }
3515 }
3516
3517 void
3518 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3519 {
3520 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3521
3522 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3523 }
3524
3525 void
3526 horizontal_config(struct workspace *ws, int id)
3527 {
3528 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3529
3530 switch (id) {
3531 case SWM_ARG_ID_STACKRESET:
3532 case SWM_ARG_ID_STACKINIT:
3533 ws->l_state.horizontal_mwin = 1;
3534 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3535 ws->l_state.horizontal_stacks = 1;
3536 break;
3537 case SWM_ARG_ID_MASTERSHRINK:
3538 if (ws->l_state.horizontal_msize > 1)
3539 ws->l_state.horizontal_msize--;
3540 break;
3541 case SWM_ARG_ID_MASTERGROW:
3542 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3543 ws->l_state.horizontal_msize++;
3544 break;
3545 case SWM_ARG_ID_MASTERADD:
3546 ws->l_state.horizontal_mwin++;
3547 break;
3548 case SWM_ARG_ID_MASTERDEL:
3549 if (ws->l_state.horizontal_mwin > 0)
3550 ws->l_state.horizontal_mwin--;
3551 break;
3552 case SWM_ARG_ID_STACKINC:
3553 ws->l_state.horizontal_stacks++;
3554 break;
3555 case SWM_ARG_ID_STACKDEC:
3556 if (ws->l_state.horizontal_stacks > 1)
3557 ws->l_state.horizontal_stacks--;
3558 break;
3559 case SWM_ARG_ID_FLIPLAYOUT:
3560 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3561 break;
3562 default:
3563 return;
3564 }
3565 }
3566
3567 void
3568 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3569 {
3570 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3571
3572 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3573 }
3574
3575 /* fullscreen view */
3576 void
3577 max_stack(struct workspace *ws, struct swm_geometry *g)
3578 {
3579 struct swm_geometry gg = *g;
3580 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3581 int winno, num_screens;
3582
3583 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3584
3585 if (ws == NULL)
3586 return;
3587
3588 winno = count_win(ws, 0);
3589 if (winno == 0 && count_win(ws, 1) == 0)
3590 return;
3591
3592 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3593 TAILQ_FOREACH(win, &ws->winlist, entry) {
3594 if (win->transient) {
3595 wintrans = win;
3596 parent = find_window(win->transient);
3597 continue;
3598 }
3599
3600 if (win->floating && win->floatmaxed == 0 ) {
3601 /*
3602 * retain geometry for retrieval on exit from
3603 * max_stack mode
3604 */
3605 store_float_geom(win, ws->r);
3606 win->floatmaxed = 1;
3607 }
3608
3609 /* only reconfigure if necessary */
3610 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3611 HEIGHT(win) != gg.h) {
3612 win->g = gg;
3613 if (bar_enabled){
3614 win->bordered = 1;
3615 } else {
3616 win->bordered = 0;
3617 WIDTH(win) += 2 * border_width;
3618 HEIGHT(win) += 2 * border_width;
3619 }
3620
3621 update_window(win);
3622 }
3623 /* unmap only if we don't have multi screen */
3624 if (win != ws->focus)
3625 if (!(num_screens > 1 || outputs > 1))
3626 unmap_window(win);
3627 }
3628
3629 /* put the last transient on top */
3630 if (wintrans) {
3631 if (parent)
3632 map_window_raised(parent->id);
3633 stack_floater(wintrans, ws->r);
3634 focus_magic(wintrans);
3635 }
3636 }
3637
3638 void
3639 send_to_ws(struct swm_region *r, union arg *args)
3640 {
3641 int wsid = args->id;
3642 struct ws_win *win = NULL, *parent;
3643 struct workspace *ws, *nws;
3644 xcb_atom_t ws_idx_atom = XCB_ATOM_NONE;
3645 char ws_idx_str[SWM_PROPLEN];
3646 union arg a;
3647
3648 if (wsid >= workspace_limit)
3649 return;
3650
3651 if (r && r->ws && r->ws->focus)
3652 win = r->ws->focus;
3653 else
3654 return;
3655 if (win == NULL)
3656 return;
3657 if (win->ws->idx == wsid)
3658 return;
3659
3660 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%x\n", win->id);
3661
3662 ws = win->ws;
3663 nws = &win->s->ws[wsid];
3664
3665 a.id = SWM_ARG_ID_FOCUSPREV;
3666 focus(r, &a);
3667 if (win->transient) {
3668 parent = find_window(win->transient);
3669 if (parent) {
3670 unmap_window(parent);
3671 TAILQ_REMOVE(&ws->winlist, parent, entry);
3672 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3673 parent->ws = nws;
3674 }
3675 }
3676 unmap_window(win);
3677 TAILQ_REMOVE(&ws->winlist, win, entry);
3678 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3679 if (TAILQ_EMPTY(&ws->winlist))
3680 r->ws->focus = NULL;
3681 win->ws = nws;
3682
3683 /* Try to update the window's workspace property */
3684 ws_idx_atom = get_atom_from_string("_SWM_WS");
3685 if (ws_idx_atom &&
3686 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3687 SWM_PROPLEN) {
3688 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3689 ws_idx_str);
3690 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
3691 ws_idx_atom, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
3692 ws_idx_str);
3693 }
3694
3695 stack();
3696 bar_update();
3697 }
3698
3699 void
3700 pressbutton(struct swm_region *r, union arg *args)
3701 {
3702 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3703 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3704 }
3705
3706 void
3707 raise_toggle(struct swm_region *r, union arg *args)
3708 {
3709 if (r == NULL || r->ws == NULL)
3710 return;
3711
3712 r->ws->always_raise = !r->ws->always_raise;
3713
3714 /* bring floaters back to top */
3715 if (r->ws->always_raise == 0)
3716 stack();
3717 }
3718
3719 void
3720 iconify(struct swm_region *r, union arg *args)
3721 {
3722 union arg a;
3723
3724 if (r->ws->focus == NULL)
3725 return;
3726 unmap_window(r->ws->focus);
3727 update_iconic(r->ws->focus, 1);
3728 stack();
3729 if (focus_mode == SWM_FOCUS_DEFAULT)
3730 drain_enter_notify();
3731 r->ws->focus = NULL;
3732 a.id = SWM_ARG_ID_FOCUSCUR;
3733 focus(r, &a);
3734 }
3735
3736 unsigned char *
3737 get_win_name(xcb_window_t win)
3738 {
3739 unsigned char *prop = NULL;
3740 unsigned long nbytes, nitems;
3741
3742 /* try _NET_WM_NAME first */
3743 if (get_property(win, a_netwmname, 0L, a_utf8_string, NULL, &nbytes,
3744 &prop)) {
3745 XFree(prop);
3746 if (get_property(win, a_netwmname, nbytes, a_utf8_string,
3747 &nitems, NULL, &prop))
3748 return (prop);
3749 }
3750
3751 /* fallback to WM_NAME */
3752 if (!get_property(win, a_wmname, 0L, a_string, NULL, &nbytes, &prop))
3753 return (NULL);
3754 XFree(prop);
3755 if (get_property(win, a_wmname, nbytes, a_string, &nitems, NULL, &prop))
3756 return (prop);
3757
3758 return (NULL);
3759 }
3760
3761 void
3762 uniconify(struct swm_region *r, union arg *args)
3763 {
3764 struct ws_win *win;
3765 FILE *lfile;
3766 unsigned char *name;
3767 int count = 0;
3768
3769 DNPRINTF(SWM_D_MISC, "uniconify\n");
3770
3771 if (r == NULL || r->ws == NULL)
3772 return;
3773
3774 /* make sure we have anything to uniconify */
3775 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3776 if (win->ws == NULL)
3777 continue; /* should never happen */
3778 if (win->iconic == 0)
3779 continue;
3780 count++;
3781 }
3782 if (count == 0)
3783 return;
3784
3785 search_r = r;
3786 search_resp_action = SWM_SEARCH_UNICONIFY;
3787
3788 spawn_select(r, args, "search", &searchpid);
3789
3790 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3791 return;
3792
3793 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3794 if (win->ws == NULL)
3795 continue; /* should never happen */
3796 if (win->iconic == 0)
3797 continue;
3798
3799 name = get_win_name(win->id);
3800 if (name == NULL)
3801 continue;
3802 fprintf(lfile, "%s.%u\n", name, win->id);
3803 XFree(name);
3804 }
3805
3806 fclose(lfile);
3807 }
3808
3809 void
3810 name_workspace(struct swm_region *r, union arg *args)
3811 {
3812 FILE *lfile;
3813
3814 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3815
3816 if (r == NULL)
3817 return;
3818
3819 search_r = r;
3820 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3821
3822 spawn_select(r, args, "name_workspace", &searchpid);
3823
3824 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3825 return;
3826
3827 fprintf(lfile, "%s", "");
3828 fclose(lfile);
3829 }
3830
3831 void
3832 search_workspace(struct swm_region *r, union arg *args)
3833 {
3834 int i;
3835 struct workspace *ws;
3836 FILE *lfile;
3837
3838 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3839
3840 if (r == NULL)
3841 return;
3842
3843 search_r = r;
3844 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3845
3846 spawn_select(r, args, "search", &searchpid);
3847
3848 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3849 return;
3850
3851 for (i = 0; i < workspace_limit; i++) {
3852 ws = &r->s->ws[i];
3853 if (ws == NULL)
3854 continue;
3855 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3856 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3857 }
3858
3859 fclose(lfile);
3860 }
3861
3862 void
3863 search_win_cleanup(void)
3864 {
3865 struct search_window *sw = NULL;
3866
3867 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3868 xcb_destroy_window(conn, sw->indicator);
3869 XFreeGC(display, sw->gc);
3870 TAILQ_REMOVE(&search_wl, sw, entry);
3871 free(sw);
3872 }
3873 }
3874
3875 void
3876 search_win(struct swm_region *r, union arg *args)
3877 {
3878 struct ws_win *win = NULL;
3879 struct search_window *sw = NULL;
3880 xcb_window_t w;
3881 XGCValues gcv;
3882 int i;
3883 char s[8];
3884 FILE *lfile;
3885 size_t len;
3886 XRectangle ibox, lbox;
3887
3888 DNPRINTF(SWM_D_MISC, "search_win\n");
3889
3890 search_r = r;
3891 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3892
3893 spawn_select(r, args, "search", &searchpid);
3894
3895 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3896 return;
3897
3898 TAILQ_INIT(&search_wl);
3899
3900 i = 1;
3901 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3902 if (win->iconic == 1)
3903 continue;
3904
3905 sw = calloc(1, sizeof(struct search_window));
3906 if (sw == NULL) {
3907 warn("search_win: calloc");
3908 fclose(lfile);
3909 search_win_cleanup();
3910 return;
3911 }
3912 sw->idx = i;
3913 sw->win = win;
3914
3915 snprintf(s, sizeof s, "%d", i);
3916 len = strlen(s);
3917
3918 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3919
3920 w = XCreateSimpleWindow(display,
3921 win->id, 0, 0,lbox.width + 4,
3922 bar_fs_extents->max_logical_extent.height, 1,
3923 r->s->c[SWM_S_COLOR_UNFOCUS].color,
3924 r->s->c[SWM_S_COLOR_FOCUS].color);
3925
3926 sw->indicator = w;
3927 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3928
3929 sw->gc = XCreateGC(display, w, 0, &gcv);
3930 map_window_raised(w);
3931 XSetForeground(display, sw->gc, r->s->c[SWM_S_COLOR_BAR].color);
3932
3933 DRAWSTRING(display, w, bar_fs, sw->gc, 2,
3934 (bar_fs_extents->max_logical_extent.height -
3935 lbox.height) / 2 - lbox.y, s, len);
3936
3937 fprintf(lfile, "%d\n", i);
3938 i++;
3939 }
3940
3941 fclose(lfile);
3942 }
3943
3944 void
3945 search_resp_uniconify(char *resp, unsigned long len)
3946 {
3947 unsigned char *name;
3948 struct ws_win *win;
3949 char *s;
3950
3951 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3952
3953 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3954 if (win->iconic == 0)
3955 continue;
3956 name = get_win_name(win->id);
3957 if (name == NULL)
3958 continue;
3959 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
3960 XFree(name);
3961 continue;
3962 }
3963 XFree(name);
3964 if (strncmp(s, resp, len) == 0) {
3965 /* XXX this should be a callback to generalize */
3966 update_iconic(win, 0);
3967 free(s);
3968 break;
3969 }
3970 free(s);
3971 }
3972 }
3973
3974 void
3975 search_resp_name_workspace(char *resp, unsigned long len)
3976 {
3977 struct workspace *ws;
3978
3979 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3980
3981 if (search_r->ws == NULL)
3982 return;
3983 ws = search_r->ws;
3984
3985 if (ws->name) {
3986 free(search_r->ws->name);
3987 search_r->ws->name = NULL;
3988 }
3989
3990 if (len > 1) {
3991 ws->name = strdup(resp);
3992 if (ws->name == NULL) {
3993 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
3994 "strdup: %s", strerror(errno));
3995 return;
3996 }
3997 }
3998 }
3999
4000 void
4001 search_resp_search_workspace(char *resp, unsigned long len)
4002 {
4003 char *p, *q;
4004 int ws_idx;
4005 const char *errstr;
4006 union arg a;
4007
4008 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4009
4010 q = strdup(resp);
4011 if (!q) {
4012 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4013 strerror(errno));
4014 return;
4015 }
4016 p = strchr(q, ':');
4017 if (p != NULL)
4018 *p = '\0';
4019 ws_idx = strtonum(q, 1, workspace_limit, &errstr);
4020 if (errstr) {
4021 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4022 errstr, q);
4023 free(q);
4024 return;
4025 }
4026 free(q);
4027 a.id = ws_idx - 1;
4028 switchws(search_r, &a);
4029 }
4030
4031 void
4032 search_resp_search_window(char *resp, unsigned long len)
4033 {
4034 char *s;
4035 int idx;
4036 const char *errstr;
4037 struct search_window *sw;
4038
4039 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4040
4041 s = strdup(resp);
4042 if (!s) {
4043 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4044 strerror(errno));
4045 return;
4046 }
4047
4048 idx = strtonum(s, 1, INT_MAX, &errstr);
4049 if (errstr) {
4050 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4051 errstr, s);
4052 free(s);
4053 return;
4054 }
4055 free(s);
4056
4057 TAILQ_FOREACH(sw, &search_wl, entry)
4058 if (idx == sw->idx) {
4059 focus_win(sw->win);
4060 break;
4061 }
4062 }
4063
4064 #define MAX_RESP_LEN 1024
4065
4066 void
4067 search_do_resp(void)
4068 {
4069 ssize_t rbytes;
4070 char *resp;
4071 unsigned long len;
4072
4073 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4074
4075 search_resp = 0;
4076 searchpid = 0;
4077
4078 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4079 warn("search: calloc");
4080 goto done;
4081 }
4082
4083 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4084 if (rbytes <= 0) {
4085 warn("search: read error");
4086 goto done;
4087 }
4088 resp[rbytes] = '\0';
4089
4090 /* XXX:
4091 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4092 * newline, so work around that by sanitizing the resp now.
4093 */
4094 resp[strcspn(resp, "\n")] = '\0';
4095 len = strlen(resp);
4096
4097 switch (search_resp_action) {
4098 case SWM_SEARCH_UNICONIFY:
4099 search_resp_uniconify(resp, len);
4100 break;
4101 case SWM_SEARCH_NAME_WORKSPACE:
4102 search_resp_name_workspace(resp, len);
4103 break;
4104 case SWM_SEARCH_SEARCH_WORKSPACE:
4105 search_resp_search_workspace(resp, len);
4106 break;
4107 case SWM_SEARCH_SEARCH_WINDOW:
4108 search_resp_search_window(resp, len);
4109 break;
4110 }
4111
4112 done:
4113 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4114 search_win_cleanup();
4115
4116 search_resp_action = SWM_SEARCH_NONE;
4117 close(select_resp_pipe[0]);
4118 free(resp);
4119 }
4120
4121 void
4122 wkill(struct swm_region *r, union arg *args)
4123 {
4124 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4125
4126 if (r->ws->focus == NULL)
4127 return;
4128
4129 if (args->id == SWM_ARG_ID_KILLWINDOW)
4130 xcb_kill_client(conn, r->ws->focus->id);
4131 else
4132 if (r->ws->focus->can_delete)
4133 client_msg(r->ws->focus, adelete);
4134 }
4135
4136
4137 int
4138 floating_toggle_win(struct ws_win *win)
4139 {
4140 struct swm_region *r;
4141
4142 if (win == NULL)
4143 return (0);
4144
4145 if (!win->ws->r)
4146 return (0);
4147
4148 r = win->ws->r;
4149
4150 /* reject floating toggles in max stack mode */
4151 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4152 return (0);
4153
4154 if (win->floating) {
4155 if (!win->floatmaxed) {
4156 /* retain position for refloat */
4157 store_float_geom(win, r);
4158 }
4159 win->floating = 0;
4160 } else {
4161 if (win->g_floatvalid) {
4162 /* refloat at last floating relative position */
4163 X(win) = win->g_float.x + X(r);
4164 Y(win) = win->g_float.y + Y(r);
4165 WIDTH(win) = win->g_float.w;
4166 HEIGHT(win) = win->g_float.h;
4167 }
4168 win->floating = 1;
4169 }
4170
4171 ewmh_update_actions(win);
4172
4173 return (1);
4174 }
4175
4176 void
4177 floating_toggle(struct swm_region *r, union arg *args)
4178 {
4179 struct ws_win *win = r->ws->focus;
4180 union arg a;
4181
4182 if (win == NULL)
4183 return;
4184
4185 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4186 _NET_WM_STATE_TOGGLE);
4187
4188 stack();
4189 if (focus_mode == SWM_FOCUS_DEFAULT)
4190 drain_enter_notify();
4191
4192 if (win == win->ws->focus) {
4193 a.id = SWM_ARG_ID_FOCUSCUR;
4194 focus(win->ws->r, &a);
4195 }
4196 }
4197
4198 void
4199 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4200 {
4201 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4202 if (resizable)
4203 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4204 else
4205 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4206 }
4207
4208 if (X(win) + BORDER(win) < X(r)) {
4209 if (resizable)
4210 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4211
4212 X(win) = X(r) - BORDER(win);
4213 }
4214
4215 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4216 if (resizable)
4217 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4218 else
4219 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4220 }
4221
4222 if (Y(win) + BORDER(win) < Y(r)) {
4223 if (resizable)
4224 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4225
4226 Y(win) = Y(r) - BORDER(win);
4227 }
4228
4229 if (resizable) {
4230 if (WIDTH(win) < 1)
4231 WIDTH(win) = 1;
4232 if (HEIGHT(win) < 1)
4233 HEIGHT(win) = 1;
4234 }
4235 }
4236
4237 void
4238 update_window(struct ws_win *win)
4239 {
4240 uint16_t mask;
4241 uint32_t wc[5];
4242
4243 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4244 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4245 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4246 wc[0] = X(win);
4247 wc[1] = Y(win);
4248 wc[2] = WIDTH(win);
4249 wc[3] = HEIGHT(win);
4250 wc[4] = BORDER(win);
4251
4252 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4253 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4254 wc[3], YESNO(win->bordered));
4255
4256 xcb_configure_window(conn, win->id, mask, wc);
4257 }
4258
4259 #define SWM_RESIZE_STEPS (50)
4260
4261 void
4262 resize(struct ws_win *win, union arg *args)
4263 {
4264 XEvent ev;
4265 Time time = 0;
4266 struct swm_region *r = NULL;
4267 int resize_step = 0;
4268 Window rr, cr;
4269 int x, y, wx, wy;
4270 unsigned int mask;
4271 struct swm_geometry g;
4272 int top = 0, left = 0;
4273 int dx, dy;
4274 Cursor cursor;
4275 unsigned int shape; /* cursor style */
4276
4277 if (win == NULL)
4278 return;
4279 r = win->ws->r;
4280
4281 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4282 return;
4283
4284 DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%x, floating: %s, "
4285 "transient: 0x%x\n", win->id, YESNO(win->floating),
4286 win->transient);
4287
4288 if (!(win->transient != 0 || win->floating != 0))
4289 return;
4290
4291 /* reject resizes in max mode for floaters (transient ok) */
4292 if (win->floatmaxed)
4293 return;
4294
4295 win->manual = 1;
4296 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4297 _NET_WM_STATE_ADD);
4298
4299 stack();
4300
4301 switch (args->id) {
4302 case SWM_ARG_ID_WIDTHSHRINK:
4303 WIDTH(win) -= SWM_RESIZE_STEPS;
4304 resize_step = 1;
4305 break;
4306 case SWM_ARG_ID_WIDTHGROW:
4307 WIDTH(win) += SWM_RESIZE_STEPS;
4308 resize_step = 1;
4309 break;
4310 case SWM_ARG_ID_HEIGHTSHRINK:
4311 HEIGHT(win) -= SWM_RESIZE_STEPS;
4312 resize_step = 1;
4313 break;
4314 case SWM_ARG_ID_HEIGHTGROW:
4315 HEIGHT(win) += SWM_RESIZE_STEPS;
4316 resize_step = 1;
4317 break;
4318 default:
4319 break;
4320 }
4321 if (resize_step) {
4322 constrain_window(win, r, 1);
4323 update_window(win);
4324 store_float_geom(win,r);
4325 return;
4326 }
4327
4328 if (focus_mode == SWM_FOCUS_DEFAULT)
4329 drain_enter_notify();
4330
4331 /* get cursor offset from window root */
4332 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4333 return;
4334
4335 g = win->g;
4336
4337 if (wx < WIDTH(win) / 2)
4338 left = 1;
4339
4340 if (wy < HEIGHT(win) / 2)
4341 top = 1;
4342
4343 if (args->id == SWM_ARG_ID_CENTER)
4344 shape = XC_sizing;
4345 else if (top)
4346 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4347 else
4348 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4349
4350 cursor = XCreateFontCursor(display, shape);
4351
4352 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4353 GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess) {
4354 XFreeCursor(display, cursor);
4355 return;
4356 }
4357
4358 do {
4359 XMaskEvent(display, MOUSEMASK | ExposureMask |
4360 SubstructureRedirectMask, &ev);
4361 switch (ev.type) {
4362 case ConfigureRequest:
4363 case Expose:
4364 case MapRequest:
4365 handler[ev.type](&ev);
4366 break;
4367 case MotionNotify:
4368 /* cursor offset/delta from start of the operation */
4369 dx = ev.xmotion.x_root - x;
4370 dy = ev.xmotion.y_root - y;
4371
4372 /* vertical */
4373 if (top)
4374 dy = -dy;
4375
4376 if (args->id == SWM_ARG_ID_CENTER) {
4377 if (g.h / 2 + dy < 1)
4378 dy = 1 - g.h / 2;
4379
4380 Y(win) = g.y - dy;
4381 HEIGHT(win) = g.h + 2 * dy;
4382 } else {
4383 if (g.h + dy < 1)
4384 dy = 1 - g.h;
4385
4386 if (top)
4387 Y(win) = g.y - dy;
4388
4389 HEIGHT(win) = g.h + dy;
4390 }
4391
4392 /* horizontal */
4393 if (left)
4394 dx = -dx;
4395
4396 if (args->id == SWM_ARG_ID_CENTER) {
4397 if (g.w / 2 + dx < 1)
4398 dx = 1 - g.w / 2;
4399
4400 X(win) = g.x - dx;
4401 WIDTH(win) = g.w + 2 * dx;
4402 } else {
4403 if (g.w + dx < 1)
4404 dx = 1 - g.w;
4405
4406 if (left)
4407 X(win) = g.x - dx;
4408
4409 WIDTH(win) = g.w + dx;
4410 }
4411
4412 constrain_window(win, r, 1);
4413
4414 /* not free, don't sync more than 120 times / second */
4415 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4416 time = ev.xmotion.time;
4417 do_sync();
4418 update_window(win);
4419 }
4420 break;
4421 }
4422 } while (ev.type != ButtonRelease);
4423 if (time) {
4424 do_sync();
4425 update_window(win);
4426 }
4427 store_float_geom(win,r);
4428
4429 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4430 XFreeCursor(display, cursor);
4431
4432 /* drain events */
4433 drain_enter_notify();
4434 }
4435
4436 void
4437 resize_step(struct swm_region *r, union arg *args)
4438 {
4439 struct ws_win *win = NULL;
4440
4441 if (r && r->ws && r->ws->focus)
4442 win = r->ws->focus;
4443 else
4444 return;
4445
4446 resize(win, args);
4447 }
4448
4449 #define SWM_MOVE_STEPS (50)
4450
4451 void
4452 move(struct ws_win *win, union arg *args)
4453 {
4454 XEvent ev;
4455 Time time = 0;
4456 int move_step = 0;
4457 struct swm_region *r = NULL;
4458
4459 Window rr, cr;
4460 int x, y, wx, wy;
4461 unsigned int mask;
4462
4463 if (win == NULL)
4464 return;
4465 r = win->ws->r;
4466
4467 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4468 return;
4469
4470 DNPRINTF(SWM_D_MOUSE, "move: window: 0x%x, floating: %s, transient: "
4471 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4472
4473 /* in max_stack mode should only move transients */
4474 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4475 return;
4476
4477 win->manual = 1;
4478 if (win->floating == 0 && !win->transient) {
4479 store_float_geom(win, r);
4480 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4481 _NET_WM_STATE_ADD);
4482 }
4483 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4484 _NET_WM_STATE_ADD);
4485
4486 stack();
4487
4488 move_step = 0;
4489 switch (args->id) {
4490 case SWM_ARG_ID_MOVELEFT:
4491 X(win) -= (SWM_MOVE_STEPS - border_width);
4492 move_step = 1;
4493 break;
4494 case SWM_ARG_ID_MOVERIGHT:
4495 X(win) += (SWM_MOVE_STEPS - border_width);
4496 move_step = 1;
4497 break;
4498 case SWM_ARG_ID_MOVEUP:
4499 Y(win) -= (SWM_MOVE_STEPS - border_width);
4500 move_step = 1;
4501 break;
4502 case SWM_ARG_ID_MOVEDOWN:
4503 Y(win) += (SWM_MOVE_STEPS - border_width);
4504 move_step = 1;
4505 break;
4506 default:
4507 break;
4508 }
4509 if (move_step) {
4510 constrain_window(win, r, 0);
4511 update_window(win);
4512 store_float_geom(win, r);
4513 return;
4514 }
4515
4516 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4517 GrabModeAsync, None, XCreateFontCursor(display, XC_fleur),
4518 CurrentTime) != GrabSuccess)
4519 return;
4520
4521 /* get cursor offset from window root */
4522 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4523 return;
4524
4525 do {
4526 XMaskEvent(display, MOUSEMASK | ExposureMask |
4527 SubstructureRedirectMask, &ev);
4528 switch (ev.type) {
4529 case ConfigureRequest:
4530 case Expose:
4531 case MapRequest:
4532 handler[ev.type](&ev);
4533 break;
4534 case MotionNotify:
4535 X(win) = ev.xmotion.x_root - wx - border_width;
4536 Y(win) = ev.xmotion.y_root - wy - border_width;
4537
4538 constrain_window(win, r, 0);
4539
4540 /* not free, don't sync more than 120 times / second */
4541 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4542 time = ev.xmotion.time;
4543 do_sync();
4544 update_window(win);
4545 }
4546 break;
4547 }
4548 } while (ev.type != ButtonRelease);
4549 if (time) {
4550 do_sync();
4551 update_window(win);
4552 }
4553 store_float_geom(win, r);
4554 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4555
4556 /* drain events */
4557 drain_enter_notify();
4558 }
4559
4560 void
4561 move_step(struct swm_region *r, union arg *args)
4562 {
4563 struct ws_win *win = NULL;
4564
4565 if (r && r->ws && r->ws->focus)
4566 win = r->ws->focus;
4567 else
4568 return;
4569
4570 if (!(win->transient != 0 || win->floating != 0))
4571 return;
4572
4573 move(win, args);
4574 }
4575
4576
4577 /* user/key callable function IDs */
4578 enum keyfuncid {
4579 kf_cycle_layout,
4580 kf_flip_layout,
4581 kf_stack_reset,
4582 kf_master_shrink,
4583 kf_master_grow,
4584 kf_master_add,
4585 kf_master_del,
4586 kf_stack_inc,
4587 kf_stack_dec,
4588 kf_swap_main,
4589 kf_focus_next,
4590 kf_focus_prev,
4591 kf_swap_next,
4592 kf_swap_prev,
4593 kf_quit,
4594 kf_restart,
4595 kf_focus_main,
4596 kf_ws_1,
4597 kf_ws_2,
4598 kf_ws_3,
4599 kf_ws_4,
4600 kf_ws_5,
4601 kf_ws_6,
4602 kf_ws_7,
4603 kf_ws_8,
4604 kf_ws_9,
4605 kf_ws_10,
4606 kf_ws_11,
4607 kf_ws_12,
4608 kf_ws_13,
4609 kf_ws_14,
4610 kf_ws_15,
4611 kf_ws_16,
4612 kf_ws_17,
4613 kf_ws_18,
4614 kf_ws_19,
4615 kf_ws_20,
4616 kf_ws_21,
4617 kf_ws_22,
4618 kf_ws_next,
4619 kf_ws_prev,
4620 kf_ws_next_all,
4621 kf_ws_prev_all,
4622 kf_ws_prior,
4623 kf_screen_next,
4624 kf_screen_prev,
4625 kf_mvws_1,
4626 kf_mvws_2,
4627 kf_mvws_3,
4628 kf_mvws_4,
4629 kf_mvws_5,
4630 kf_mvws_6,
4631 kf_mvws_7,
4632 kf_mvws_8,
4633 kf_mvws_9,
4634 kf_mvws_10,
4635 kf_mvws_11,
4636 kf_mvws_12,
4637 kf_mvws_13,
4638 kf_mvws_14,
4639 kf_mvws_15,
4640 kf_mvws_16,
4641 kf_mvws_17,
4642 kf_mvws_18,
4643 kf_mvws_19,
4644 kf_mvws_20,
4645 kf_mvws_21,
4646 kf_mvws_22,
4647 kf_bar_toggle,
4648 kf_wind_kill,
4649 kf_wind_del,
4650 kf_float_toggle,
4651 kf_version,
4652 kf_spawn_custom,
4653 kf_iconify,
4654 kf_uniconify,
4655 kf_raise_toggle,
4656 kf_button2,
4657 kf_width_shrink,
4658 kf_width_grow,
4659 kf_height_shrink,
4660 kf_height_grow,
4661 kf_move_left,
4662 kf_move_right,
4663 kf_move_up,
4664 kf_move_down,
4665 kf_name_workspace,
4666 kf_search_workspace,
4667 kf_search_win,
4668 kf_dumpwins, /* MUST BE LAST */
4669 kf_invalid
4670 };
4671
4672 /* key definitions */
4673 void
4674 dummykeyfunc(struct swm_region *r, union arg *args)
4675 {
4676 };
4677
4678 struct keyfunc {
4679 char name[SWM_FUNCNAME_LEN];
4680 void (*func)(struct swm_region *r, union arg *);
4681 union arg args;
4682 } keyfuncs[kf_invalid + 1] = {
4683 /* name function argument */
4684 { "cycle_layout", cycle_layout, {0} },
4685 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4686 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4687 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4688 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4689 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4690 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4691 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4692 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4693 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4694 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4695 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4696 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4697 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4698 { "quit", quit, {0} },
4699 { "restart", restart, {0} },
4700 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4701 { "ws_1", switchws, {.id = 0} },
4702 { "ws_2", switchws, {.id = 1} },
4703 { "ws_3", switchws, {.id = 2} },
4704 { "ws_4", switchws, {.id = 3} },
4705 { "ws_5", switchws, {.id = 4} },
4706 { "ws_6", switchws, {.id = 5} },
4707 { "ws_7", switchws, {.id = 6} },
4708 { "ws_8", switchws, {.id = 7} },
4709 { "ws_9", switchws, {.id = 8} },
4710 { "ws_10", switchws, {.id = 9} },
4711 { "ws_11", switchws, {.id = 10} },
4712 { "ws_12", switchws, {.id = 11} },
4713 { "ws_13", switchws, {.id = 12} },
4714 { "ws_14", switchws, {.id = 13} },
4715 { "ws_15", switchws, {.id = 14} },
4716 { "ws_16", switchws, {.id = 15} },
4717 { "ws_17", switchws, {.id = 16} },
4718 { "ws_18", switchws, {.id = 17} },
4719 { "ws_19", switchws, {.id = 18} },
4720 { "ws_20", switchws, {.id = 19} },
4721 { "ws_21", switchws, {.id = 20} },
4722 { "ws_22", switchws, {.id = 21} },
4723 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4724 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4725 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4726 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4727 { "ws_prior", priorws, {0} },
4728 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4729 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4730 { "mvws_1", send_to_ws, {.id = 0} },
4731 { "mvws_2", send_to_ws, {.id = 1} },
4732 { "mvws_3", send_to_ws, {.id = 2} },
4733 { "mvws_4", send_to_ws, {.id = 3} },
4734 { "mvws_5", send_to_ws, {.id = 4} },
4735 { "mvws_6", send_to_ws, {.id = 5} },
4736 { "mvws_7", send_to_ws, {.id = 6} },
4737 { "mvws_8", send_to_ws, {.id = 7} },
4738 { "mvws_9", send_to_ws, {.id = 8} },
4739 { "mvws_10", send_to_ws, {.id = 9} },
4740 { "mvws_11", send_to_ws, {.id = 10} },
4741 { "mvws_12", send_to_ws, {.id = 11} },
4742 { "mvws_13", send_to_ws, {.id = 12} },
4743 { "mvws_14", send_to_ws, {.id = 13} },
4744 { "mvws_15", send_to_ws, {.id = 14} },
4745 { "mvws_16", send_to_ws, {.id = 15} },
4746 { "mvws_17", send_to_ws, {.id = 16} },
4747 { "mvws_18", send_to_ws, {.id = 17} },
4748 { "mvws_19", send_to_ws, {.id = 18} },
4749 { "mvws_20", send_to_ws, {.id = 19} },
4750 { "mvws_21", send_to_ws, {.id = 20} },
4751 { "mvws_22", send_to_ws, {.id = 21} },
4752 { "bar_toggle", bar_toggle, {0} },
4753 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4754 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4755 { "float_toggle", floating_toggle,{0} },
4756 { "version", version, {0} },
4757 { "spawn_custom", dummykeyfunc, {0} },
4758 { "iconify", iconify, {0} },
4759 { "uniconify", uniconify, {0} },
4760 { "raise_toggle", raise_toggle, {0} },
4761 { "button2", pressbutton, {2} },
4762 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4763 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4764 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4765 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4766 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4767 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4768 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4769 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4770 { "name_workspace", name_workspace, {0} },
4771 { "search_workspace", search_workspace, {0} },
4772 { "search_win", search_win, {0} },
4773 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4774 { "invalid key func", NULL, {0} },
4775 };
4776 struct key {
4777 RB_ENTRY(key) entry;
4778 unsigned int mod;
4779 KeySym keysym;
4780 enum keyfuncid funcid;
4781 char *spawn_name;
4782 };
4783 RB_HEAD(key_tree, key);
4784
4785 int
4786 key_cmp(struct key *kp1, struct key *kp2)
4787 {
4788 if (kp1->keysym < kp2->keysym)
4789 return (-1);
4790 if (kp1->keysym > kp2->keysym)
4791 return (1);
4792
4793 if (kp1->mod < kp2->mod)
4794 return (-1);
4795 if (kp1->mod > kp2->mod)
4796 return (1);
4797
4798 return (0);
4799 }
4800
4801 RB_GENERATE(key_tree, key, entry, key_cmp);
4802 struct key_tree keys;
4803
4804 /* mouse */
4805 enum { client_click, root_click };
4806 struct button {
4807 unsigned int action;
4808 unsigned int mask;
4809 unsigned int button;
4810 void (*func)(struct ws_win *, union arg *);
4811 union arg args;
4812 } buttons[] = {
4813 /* action key mouse button func args */
4814 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4815 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4816 { client_click, MODKEY, Button1, move, {0} },
4817 };
4818
4819 void
4820 update_modkey(unsigned int mod)
4821 {
4822 int i;
4823 struct key *kp;
4824
4825 mod_key = mod;
4826 RB_FOREACH(kp, key_tree, &keys)
4827 if (kp->mod & ShiftMask)
4828 kp->mod = mod | ShiftMask;
4829 else
4830 kp->mod = mod;
4831
4832 for (i = 0; i < LENGTH(buttons); i++)
4833 if (buttons[i].mask & ShiftMask)
4834 buttons[i].mask = mod | ShiftMask;
4835 else
4836 buttons[i].mask = mod;
4837 }
4838
4839 /* spawn */
4840 struct spawn_prog {
4841 TAILQ_ENTRY(spawn_prog) entry;
4842 char *name;
4843 int argc;
4844 char **argv;
4845 };
4846 TAILQ_HEAD(spawn_list, spawn_prog);
4847 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4848
4849 int
4850 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4851 char ***ret_args)
4852 {
4853 struct spawn_prog *prog = NULL;
4854 int i;
4855 char *ap, **real_args;
4856
4857 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4858
4859 /* find program */
4860 TAILQ_FOREACH(prog, &spawns, entry) {
4861 if (!strcasecmp(spawn_name, prog->name))
4862 break;
4863 }
4864 if (prog == NULL) {
4865 warnx("spawn_custom: program %s not found", spawn_name);
4866 return (-1);
4867 }
4868
4869 /* make room for expanded args */
4870 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4871 err(1, "spawn_custom: calloc real_args");
4872
4873 /* expand spawn_args into real_args */
4874 for (i = 0; i < prog->argc; i++) {
4875 ap = prog->argv[i];
4876 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4877 if (!strcasecmp(ap, "$bar_border")) {
4878 if ((real_args[i] =
4879 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4880 == NULL)
4881 err(1, "spawn_custom border color");
4882 } else if (!strcasecmp(ap, "$bar_color")) {
4883 if ((real_args[i] =
4884 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4885 == NULL)
4886 err(1, "spawn_custom bar color");
4887 } else if (!strcasecmp(ap, "$bar_font")) {
4888 if ((real_args[i] = strdup(bar_fonts))
4889 == NULL)
4890 err(1, "spawn_custom bar fonts");
4891 } else if (!strcasecmp(ap, "$bar_font_color")) {
4892 if ((real_args[i] =
4893 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4894 == NULL)
4895 err(1, "spawn_custom color font");
4896 } else if (!strcasecmp(ap, "$color_focus")) {
4897 if ((real_args[i] =
4898 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4899 == NULL)
4900 err(1, "spawn_custom color focus");
4901 } else if (!strcasecmp(ap, "$color_unfocus")) {
4902 if ((real_args[i] =
4903 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4904 == NULL)
4905 err(1, "spawn_custom color unfocus");
4906 } else {
4907 /* no match --> copy as is */
4908 if ((real_args[i] = strdup(ap)) == NULL)
4909 err(1, "spawn_custom strdup(ap)");
4910 }
4911 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4912 real_args[i]);
4913 }
4914
4915 #ifdef SWM_DEBUG
4916 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4917 for (i = 0; i < prog->argc; i++)
4918 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4919 DNPRINTF(SWM_D_SPAWN, "\n");
4920 #endif
4921 *ret_args = real_args;
4922 return (prog->argc);
4923 }
4924
4925 void
4926 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4927 {
4928 union arg a;
4929 char **real_args;
4930 int spawn_argc, i;
4931
4932 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4933 return;
4934 a.argv = real_args;
4935 if (fork() == 0)
4936 spawn(r->ws->idx, &a, 1);
4937
4938 for (i = 0; i < spawn_argc; i++)
4939 free(real_args[i]);
4940 free(real_args);
4941 }
4942
4943 void
4944 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4945 {
4946 union arg a;
4947 char **real_args;
4948 int i, spawn_argc;
4949
4950 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4951 return;
4952 a.argv = real_args;
4953
4954 if (pipe(select_list_pipe) == -1)
4955 err(1, "pipe error");
4956 if (pipe(select_resp_pipe) == -1)
4957 err(1, "pipe error");
4958
4959 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4960 err(1, "could not disable SIGPIPE");
4961 switch (*pid = fork()) {
4962 case -1:
4963 err(1, "cannot fork");
4964 break;
4965 case 0: /* child */
4966 if (dup2(select_list_pipe[0], 0) == -1)
4967 err(1, "dup2");
4968 if (dup2(select_resp_pipe[1], 1) == -1)
4969 err(1, "dup2");
4970 close(select_list_pipe[1]);
4971 close(select_resp_pipe[0]);
4972 spawn(r->ws->idx, &a, 0);
4973 break;
4974 default: /* parent */
4975 close(select_list_pipe[0]);
4976 close(select_resp_pipe[1]);
4977 break;
4978 }
4979
4980 for (i = 0; i < spawn_argc; i++)
4981 free(real_args[i]);
4982 free(real_args);
4983 }
4984
4985 void
4986 spawn_insert(char *name, char *args)
4987 {
4988 char *arg, *cp, *ptr;
4989 struct spawn_prog *sp;
4990
4991 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
4992
4993 if ((sp = calloc(1, sizeof *sp)) == NULL)
4994 err(1, "spawn_insert: malloc");
4995 if ((sp->name = strdup(name)) == NULL)
4996 err(1, "spawn_insert: strdup");
4997
4998 /* convert the arguments to an argument list */
4999 if ((ptr = cp = strdup(args)) == NULL)
5000 err(1, "spawn_insert: strdup");
5001 while ((arg = strsep(&ptr, " \t")) != NULL) {
5002 /* empty field; skip it */
5003 if (*arg == '\0')
5004 continue;
5005
5006 sp->argc++;
5007 if ((sp->argv = realloc(sp->argv, sp->argc *
5008 sizeof *sp->argv)) == NULL)
5009 err(1, "spawn_insert: realloc");
5010 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5011 err(1, "spawn_insert: strdup");
5012 }
5013 free(cp);
5014
5015 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5016 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5017 }
5018
5019 void
5020 spawn_remove(struct spawn_prog *sp)
5021 {
5022 int i;
5023
5024 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5025
5026 TAILQ_REMOVE(&spawns, sp, entry);
5027 for (i = 0; i < sp->argc; i++)
5028 free(sp->argv[i]);
5029 free(sp->argv);
5030 free(sp->name);
5031 free(sp);
5032
5033 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5034 }
5035
5036 void
5037 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5038 {
5039 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5040
5041 spawn_remove(sp);
5042 spawn_insert(name, args);
5043
5044 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5045 }
5046
5047 void
5048 setspawn(char *name, char *args)
5049 {
5050 struct spawn_prog *sp;
5051
5052 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5053
5054 if (name == NULL)
5055 return;
5056
5057 TAILQ_FOREACH(sp, &spawns, entry) {
5058 if (!strcmp(sp->name, name)) {
5059 if (*args == '\0')
5060 spawn_remove(sp);
5061 else
5062 spawn_replace(sp, name, args);
5063 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5064 return;
5065 }
5066 }
5067 if (*args == '\0') {
5068 warnx("error: setspawn: cannot find program: %s", name);
5069 return;
5070 }
5071
5072 spawn_insert(name, args);
5073 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5074 }
5075
5076 int
5077 setconfspawn(char *selector, char *value, int flags)
5078 {
5079 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
5080
5081 setspawn(selector, value);
5082
5083 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
5084 return (0);
5085 }
5086
5087 void
5088 setup_spawn(void)
5089 {
5090 setconfspawn("term", "xterm", 0);
5091 setconfspawn("spawn_term", "xterm", 0);
5092 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5093 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5094 setconfspawn("lock", "xlock", 0);
5095 setconfspawn("initscr", "initscreen.sh", 0);
5096 setconfspawn("menu", "dmenu_run"
5097 " -fn $bar_font"
5098 " -nb $bar_color"
5099 " -nf $bar_font_color"
5100 " -sb $bar_border"
5101 " -sf $bar_color", 0);
5102 setconfspawn("search", "dmenu"
5103 " -i"
5104 " -fn $bar_font"
5105 " -nb $bar_color"
5106 " -nf $bar_font_color"
5107 " -sb $bar_border"
5108 " -sf $bar_color", 0);
5109 setconfspawn("name_workspace", "dmenu"
5110 " -p Workspace"
5111 " -fn $bar_font"
5112 " -nb $bar_color"
5113 " -nf $bar_font_color"
5114 " -sb $bar_border"
5115 " -sf $bar_color", 0);
5116 }
5117
5118 /* key bindings */
5119 #define SWM_MODNAME_SIZE 32
5120 #define SWM_KEY_WS "\n+ \t"
5121 int
5122 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5123 {
5124 char *cp, *name;
5125 KeySym uks;
5126 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5127 if (mod == NULL || ks == NULL) {
5128 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5129 return (1);
5130 }
5131 if (keystr == NULL || strlen(keystr) == 0) {
5132 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5133 return (1);
5134 }
5135 cp = keystr;
5136 *ks = NoSymbol;
5137 *mod = 0;
5138 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5139 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5140 if (cp)
5141 cp += (long)strspn(cp, SWM_KEY_WS);
5142 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5143 *mod |= currmod;
5144 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5145 *mod |= Mod1Mask;
5146 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5147 *mod += Mod2Mask;
5148 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5149 *mod |= Mod3Mask;
5150 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5151 *mod |= Mod4Mask;
5152 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5153 *mod |= ShiftMask;
5154 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5155 *mod |= ControlMask;
5156 else {
5157 *ks = XStringToKeysym(name);
5158 XConvertCase(*ks, ks, &uks);
5159 if (ks == NoSymbol) {
5160 DNPRINTF(SWM_D_KEY,
5161 "parsekeys: invalid key %s\n",
5162 name);
5163 return (1);
5164 }
5165 }
5166 }
5167 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5168 return (0);
5169 }
5170
5171 char *
5172 strdupsafe(char *str)
5173 {
5174 if (str == NULL)
5175 return (NULL);
5176 else
5177 return (strdup(str));
5178 }
5179
5180 void
5181 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5182 {
5183 struct key *kp;
5184
5185 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5186 keyfuncs[kfid].name, spawn_name);
5187
5188 if ((kp = malloc(sizeof *kp)) == NULL)
5189 err(1, "key_insert: malloc");
5190
5191 kp->mod = mod;
5192 kp->keysym = ks;
5193 kp->funcid = kfid;
5194 kp->spawn_name = strdupsafe(spawn_name);
5195 RB_INSERT(key_tree, &keys, kp);
5196
5197 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5198 }
5199
5200 struct key *
5201 key_lookup(unsigned int mod, KeySym ks)
5202 {
5203 struct key kp;
5204
5205 kp.keysym = ks;
5206 kp.mod = mod;
5207
5208 return (RB_FIND(key_tree, &keys, &kp));
5209 }
5210
5211 void
5212 key_remove(struct key *kp)
5213 {
5214 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5215
5216 RB_REMOVE(key_tree, &keys, kp);
5217 free(kp->spawn_name);
5218 free(kp);
5219
5220 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5221 }
5222
5223 void
5224 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5225 char *spawn_name)
5226 {
5227 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5228 spawn_name);
5229
5230 key_remove(kp);
5231 key_insert(mod, ks, kfid, spawn_name);
5232
5233 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5234 }
5235
5236 void
5237 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5238 char *spawn_name)
5239 {
5240 struct key *kp;
5241
5242 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5243 keyfuncs[kfid].name, spawn_name);
5244
5245 if ((kp = key_lookup(mod, ks)) != NULL) {
5246 if (kfid == kf_invalid)
5247 key_remove(kp);
5248 else
5249 key_replace(kp, mod, ks, kfid, spawn_name);
5250 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5251 return;
5252 }
5253 if (kfid == kf_invalid) {
5254 warnx("error: setkeybinding: cannot find mod/key combination");
5255 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5256 return;
5257 }
5258
5259 key_insert(mod, ks, kfid, spawn_name);
5260 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5261 }
5262
5263 int
5264 setconfbinding(char *selector, char *value, int flags)
5265 {
5266 enum keyfuncid kfid;
5267 unsigned int mod;
5268 KeySym ks;
5269 struct spawn_prog *sp;
5270 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5271 if (selector == NULL) {
5272 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5273 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5274 kfid = kf_invalid;
5275 setkeybinding(mod, ks, kfid, NULL);
5276 return (0);
5277 } else
5278 return (1);
5279 }
5280 /* search by key function name */
5281 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
5282 if (strncasecmp(selector, keyfuncs[kfid].name,
5283 SWM_FUNCNAME_LEN) == 0) {
5284 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5285 selector);
5286 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5287 setkeybinding(mod, ks, kfid, NULL);
5288 return (0);
5289 } else
5290 return (1);
5291 }
5292 }
5293 /* search by custom spawn name */
5294 TAILQ_FOREACH(sp, &spawns, entry) {
5295 if (strcasecmp(selector, sp->name) == 0) {
5296 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5297 selector);
5298 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5299 setkeybinding(mod, ks, kf_spawn_custom,
5300 sp->name);
5301 return (0);
5302 } else
5303 return (1);
5304 }
5305 }
5306 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5307 return (1);
5308 }
5309
5310 void
5311 setup_keys(void)
5312 {
5313 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
5314 setkeybinding(MODKEY|ShiftMask, XK_backslash, kf_flip_layout, NULL);
5315 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
5316 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
5317 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
5318 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
5319 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
5320 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
5321 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
5322 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
5323 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
5324 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
5325 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
5326 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
5327 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_custom,"term");
5328 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
5329 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
5330 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
5331 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
5332 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
5333 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
5334 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
5335 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
5336 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
5337 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
5338 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
5339 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
5340 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
5341 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
5342 setkeybinding(MODKEY, XK_F1, kf_ws_11, NULL);
5343 setkeybinding(MODKEY, XK_F2, kf_ws_12, NULL);
5344 setkeybinding(MODKEY, XK_F3, kf_ws_13, NULL);
5345 setkeybinding(MODKEY, XK_F4, kf_ws_14, NULL);
5346 setkeybinding(MODKEY, XK_F5, kf_ws_15, NULL);
5347 setkeybinding(MODKEY, XK_F6, kf_ws_16, NULL);
5348 setkeybinding(MODKEY, XK_F7, kf_ws_17, NULL);
5349 setkeybinding(MODKEY, XK_F8, kf_ws_18, NULL);
5350 setkeybinding(MODKEY, XK_F9, kf_ws_19, NULL);
5351 setkeybinding(MODKEY, XK_F10, kf_ws_20, NULL);
5352 setkeybinding(MODKEY, XK_F11, kf_ws_21, NULL);
5353 setkeybinding(MODKEY, XK_F12, kf_ws_22, NULL);
5354 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
5355 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
5356 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
5357 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
5358 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
5359 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
5360 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
5361 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
5362 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
5363 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
5364 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
5365 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
5366 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
5367 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
5368 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
5369 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
5370 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
5371 setkeybinding(MODKEY|ShiftMask, XK_F1, kf_mvws_11, NULL);
5372 setkeybinding(MODKEY|ShiftMask, XK_F2, kf_mvws_12, NULL);
5373 setkeybinding(MODKEY|ShiftMask, XK_F3, kf_mvws_13, NULL);
5374 setkeybinding(MODKEY|ShiftMask, XK_F4, kf_mvws_14, NULL);
5375 setkeybinding(MODKEY|ShiftMask, XK_F5, kf_mvws_15, NULL);
5376 setkeybinding(MODKEY|ShiftMask, XK_F6, kf_mvws_16, NULL);
5377 setkeybinding(MODKEY|ShiftMask, XK_F7, kf_mvws_17, NULL);
5378 setkeybinding(MODKEY|ShiftMask, XK_F8, kf_mvws_18, NULL);
5379 setkeybinding(MODKEY|ShiftMask, XK_F9, kf_mvws_19, NULL);
5380 setkeybinding(MODKEY|ShiftMask, XK_F10, kf_mvws_20, NULL);
5381 setkeybinding(MODKEY|ShiftMask, XK_F11, kf_mvws_21, NULL);
5382 setkeybinding(MODKEY|ShiftMask, XK_F12, kf_mvws_22, NULL);
5383 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
5384 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
5385 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
5386 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
5387 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
5388 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
5389 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
5390 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
5391 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
5392 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
5393 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
5394 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
5395 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
5396 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
5397 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
5398 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
5399 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
5400 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
5401 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
5402 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
5403 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
5404 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
5405 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
5406 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
5407 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
5408 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
5409 #ifdef SWM_DEBUG
5410 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
5411 #endif
5412 }
5413
5414 void
5415 clear_keys(void)
5416 {
5417 struct key *kp;
5418
5419 while (RB_EMPTY(&keys) == 0) {
5420 kp = RB_ROOT(&keys);
5421 key_remove(kp);
5422 }
5423 }
5424
5425 int
5426 setkeymapping(char *selector, char *value, int flags)
5427 {
5428 char keymapping_file[PATH_MAX];
5429 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5430 if (value[0] == '~')
5431 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5432 pwd->pw_dir, &value[1]);
5433 else
5434 strlcpy(keymapping_file, value, sizeof keymapping_file);
5435 clear_keys();
5436 /* load new key bindings; if it fails, revert to default bindings */
5437 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5438 clear_keys();
5439 setup_keys();
5440 }
5441 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5442 return (0);
5443 }
5444
5445 void
5446 updatenumlockmask(void)
5447 {
5448 unsigned int i, j;
5449 XModifierKeymap *modmap;
5450
5451 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5452 numlockmask = 0;
5453 modmap = XGetModifierMapping(display);
5454 for (i = 0; i < 8; i++)
5455 for (j = 0; j < modmap->max_keypermod; j++)
5456 if (modmap->modifiermap[i * modmap->max_keypermod + j]
5457 == XKeysymToKeycode(display, XK_Num_Lock))
5458 numlockmask = (1 << i);
5459
5460 XFreeModifiermap(modmap);
5461 }
5462
5463 void
5464 grabkeys(void)
5465 {
5466 int num_screens;
5467 unsigned int j, k;
5468 KeyCode code;
5469 unsigned int modifiers[] =
5470 { 0, LockMask, numlockmask, numlockmask | LockMask };
5471 struct key *kp;
5472
5473 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5474 updatenumlockmask();
5475
5476 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5477 for (k = 0; k < num_screens; k++) {
5478 if (TAILQ_EMPTY(&screens[k].rl))
5479 continue;
5480 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5481 XCB_MOD_MASK_ANY);
5482 RB_FOREACH(kp, key_tree, &keys) {
5483 if ((code = XKeysymToKeycode(display, kp->keysym)))
5484 for (j = 0; j < LENGTH(modifiers); j++)
5485 XGrabKey(display, code,
5486 kp->mod | modifiers[j],
5487 screens[k].root, True,
5488 GrabModeAsync, GrabModeAsync);
5489 }
5490 }
5491 }
5492
5493 void
5494 grabbuttons(struct ws_win *win, int focused)
5495 {
5496 unsigned int i, j;
5497 unsigned int modifiers[] =
5498 { 0, LockMask, numlockmask, numlockmask|LockMask };
5499
5500 updatenumlockmask();
5501 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, win->id,
5502 XCB_BUTTON_MASK_ANY);
5503 if (focused) {
5504 for (i = 0; i < LENGTH(buttons); i++)
5505 if (buttons[i].action == client_click)
5506 for (j = 0; j < LENGTH(modifiers); j++)
5507 xcb_grab_button(conn, False, win->id,
5508 BUTTONMASK,
5509 XCB_GRAB_MODE_ASYNC,
5510 XCB_GRAB_MODE_SYNC,
5511 XCB_WINDOW_NONE,
5512 XCB_CURSOR_NONE,
5513 buttons[i].button,
5514 buttons[i].mask);
5515 } else
5516 xcb_grab_button(conn, False, win->id, BUTTONMASK,
5517 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_SYNC,
5518 XCB_WINDOW_NONE, XCB_CURSOR_NONE, XCB_BUTTON_INDEX_ANY,
5519 XCB_BUTTON_MASK_ANY);
5520 }
5521
5522 const char *quirkname[] = {
5523 "NONE", /* config string for "no value" */
5524 "FLOAT",
5525 "TRANSSZ",
5526 "ANYWHERE",
5527 "XTERM_FONTADJ",
5528 "FULLSCREEN",
5529 "FOCUSPREV",
5530 };
5531
5532 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5533 #define SWM_Q_WS "\n|+ \t"
5534 int
5535 parsequirks(char *qstr, unsigned long *quirk)
5536 {
5537 char *cp, *name;
5538 int i;
5539
5540 if (quirk == NULL)
5541 return (1);
5542
5543 cp = qstr;
5544 *quirk = 0;
5545 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5546 if (cp)
5547 cp += (long)strspn(cp, SWM_Q_WS);
5548 for (i = 0; i < LENGTH(quirkname); i++) {
5549 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5550 DNPRINTF(SWM_D_QUIRK,
5551 "parsequirks: %s\n", name);
5552 if (i == 0) {
5553 *quirk = 0;
5554 return (0);
5555 }
5556 *quirk |= 1 << (i-1);
5557 break;
5558 }
5559 }
5560 if (i >= LENGTH(quirkname)) {
5561 DNPRINTF(SWM_D_QUIRK,
5562 "parsequirks: invalid quirk [%s]\n", name);
5563 return (1);
5564 }
5565 }
5566 return (0);
5567 }
5568
5569 void
5570 quirk_insert(const char *class, const char *name, unsigned long quirk)
5571 {
5572 struct quirk *qp;
5573
5574 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5575 quirk);
5576
5577 if ((qp = malloc(sizeof *qp)) == NULL)
5578 err(1, "quirk_insert: malloc");
5579 if ((qp->class = strdup(class)) == NULL)
5580 err(1, "quirk_insert: strdup");
5581 if ((qp->name = strdup(name)) == NULL)
5582 err(1, "quirk_insert: strdup");
5583
5584 qp->quirk = quirk;
5585 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5586
5587 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5588 }
5589
5590 void
5591 quirk_remove(struct quirk *qp)
5592 {
5593 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5594 qp->name, qp->quirk);
5595
5596 TAILQ_REMOVE(&quirks, qp, entry);
5597 free(qp->class);
5598 free(qp->name);
5599 free(qp);
5600
5601 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5602 }
5603
5604 void
5605 quirk_replace(struct quirk *qp, const char *class, const char *name,
5606 unsigned long quirk)
5607 {
5608 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5609 qp->name, qp->quirk);
5610
5611 quirk_remove(qp);
5612 quirk_insert(class, name, quirk);
5613
5614 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5615 }
5616
5617 void
5618 setquirk(const char *class, const char *name, unsigned long quirk)
5619 {
5620 struct quirk *qp;
5621
5622 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5623 quirk);
5624
5625 TAILQ_FOREACH(qp, &quirks, entry) {
5626 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5627 if (!quirk)
5628 quirk_remove(qp);
5629 else
5630 quirk_replace(qp, class, name, quirk);
5631 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5632 return;
5633 }
5634 }
5635 if (!quirk) {
5636 warnx("error: setquirk: cannot find class/name combination");
5637 return;
5638 }
5639
5640 quirk_insert(class, name, quirk);
5641 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5642 }
5643
5644 int
5645 setconfquirk(char *selector, char *value, int flags)
5646 {
5647 char *cp, *class, *name;
5648 int retval;
5649 unsigned long quirks;
5650 if (selector == NULL)
5651 return (0);
5652 if ((cp = strchr(selector, ':')) == NULL)
5653 return (0);
5654 *cp = '\0';
5655 class = selector;
5656 name = cp + 1;
5657 if ((retval = parsequirks(value, &quirks)) == 0)
5658 setquirk(class, name, quirks);
5659 return (retval);
5660 }
5661
5662 void
5663 setup_quirks(void)
5664 {
5665 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5666 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5667 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5668 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5669 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5670 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5671 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5672 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5673 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5674 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5675 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5676 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5677 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5678 }
5679
5680 /* conf file stuff */
5681 #define SWM_CONF_FILE "spectrwm.conf"
5682 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5683
5684 enum {
5685 SWM_S_BAR_ACTION,
5686 SWM_S_BAR_AT_BOTTOM,
5687 SWM_S_BAR_BORDER_WIDTH,
5688 SWM_S_BAR_DELAY,
5689 SWM_S_BAR_ENABLED,
5690 SWM_S_BAR_FONT,
5691 SWM_S_BAR_FORMAT,
5692 SWM_S_BAR_JUSTIFY,
5693 SWM_S_BORDER_WIDTH,
5694 SWM_S_CLOCK_ENABLED,
5695 SWM_S_CLOCK_FORMAT,
5696 SWM_S_CYCLE_EMPTY,
5697 SWM_S_CYCLE_VISIBLE,
5698 SWM_S_DIALOG_RATIO,
5699 SWM_S_DISABLE_BORDER,
5700 SWM_S_FOCUS_CLOSE,
5701 SWM_S_FOCUS_CLOSE_WRAP,
5702 SWM_S_FOCUS_DEFAULT,
5703 SWM_S_FOCUS_MODE,
5704 SWM_S_SPAWN_ORDER,
5705 SWM_S_SPAWN_TERM,
5706 SWM_S_SS_APP,
5707 SWM_S_SS_ENABLED,
5708 SWM_S_STACK_ENABLED,
5709 SWM_S_TERM_WIDTH,
5710 SWM_S_TITLE_CLASS_ENABLED,
5711 SWM_S_TITLE_NAME_ENABLED,
5712 SWM_S_URGENT_ENABLED,
5713 SWM_S_VERBOSE_LAYOUT,
5714 SWM_S_WINDOW_NAME_ENABLED,
5715 SWM_S_WORKSPACE_LIMIT
5716 };
5717
5718 int
5719 setconfvalue(char *selector, char *value, int flags)
5720 {
5721 int i;
5722 char *b;
5723
5724 switch (flags) {
5725 case SWM_S_BAR_ACTION:
5726 free(bar_argv[0]);
5727 if ((bar_argv[0] = strdup(value)) == NULL)
5728 err(1, "setconfvalue: bar_action");
5729 break;
5730 case SWM_S_BAR_AT_BOTTOM:
5731 bar_at_bottom = atoi(value);
5732 break;
5733 case SWM_S_BAR_BORDER_WIDTH:
5734 bar_border_width = atoi(value);
5735 if (bar_border_width < 0)
5736 bar_border_width = 0;
5737 break;
5738 case SWM_S_BAR_DELAY:
5739 bar_delay = atoi(value);
5740 break;
5741 case SWM_S_BAR_ENABLED:
5742 bar_enabled = atoi(value);
5743 break;
5744 case SWM_S_BAR_FONT:
5745 b = bar_fonts;
5746 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5747 err(1, "setconfvalue: asprintf: failed to allocate "
5748 "memory for bar_fonts.");
5749
5750 free(b);
5751 break;
5752 case SWM_S_BAR_FORMAT:
5753 free(bar_format);
5754 if ((bar_format = strdup(value)) == NULL)
5755 err(1, "setconfvalue: bar_format");
5756 break;
5757 case SWM_S_BAR_JUSTIFY:
5758 if (!strcmp(value, "left"))
5759 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5760 else if (!strcmp(value, "center"))
5761 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5762 else if (!strcmp(value, "right"))
5763 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5764 else
5765 errx(1, "invalid bar_justify");
5766 break;
5767 case SWM_S_BORDER_WIDTH:
5768 border_width = atoi(value);
5769 if (border_width < 0)
5770 border_width = 0;
5771 break;
5772 case SWM_S_CLOCK_ENABLED:
5773 clock_enabled = atoi(value);
5774 break;
5775 case SWM_S_CLOCK_FORMAT:
5776 #ifndef SWM_DENY_CLOCK_FORMAT
5777 free(clock_format);
5778 if ((clock_format = strdup(value)) == NULL)
5779 err(1, "setconfvalue: clock_format");
5780 #endif
5781 break;
5782 case SWM_S_CYCLE_EMPTY:
5783 cycle_empty = atoi(value);
5784 break;
5785 case SWM_S_CYCLE_VISIBLE:
5786 cycle_visible = atoi(value);
5787 break;
5788 case SWM_S_DIALOG_RATIO:
5789 dialog_ratio = atof(value);
5790 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5791 dialog_ratio = .6;
5792 break;
5793 case SWM_S_DISABLE_BORDER:
5794 disable_border = atoi(value);
5795 break;
5796 case SWM_S_FOCUS_CLOSE:
5797 if (!strcmp(value, "first"))
5798 focus_close = SWM_STACK_BOTTOM;
5799 else if (!strcmp(value, "last"))
5800 focus_close = SWM_STACK_TOP;
5801 else if (!strcmp(value, "next"))
5802 focus_close = SWM_STACK_ABOVE;
5803 else if (!strcmp(value, "previous"))
5804 focus_close = SWM_STACK_BELOW;
5805 else
5806 errx(1, "focus_close");
5807 break;
5808 case SWM_S_FOCUS_CLOSE_WRAP:
5809 focus_close_wrap = atoi(value);
5810 break;
5811 case SWM_S_FOCUS_DEFAULT:
5812 if (!strcmp(value, "last"))
5813 focus_default = SWM_STACK_TOP;
5814 else if (!strcmp(value, "first"))
5815 focus_default = SWM_STACK_BOTTOM;
5816 else
5817 errx(1, "focus_default");
5818 break;
5819 case SWM_S_FOCUS_MODE:
5820 if (!strcmp(value, "default"))
5821 focus_mode = SWM_FOCUS_DEFAULT;
5822 else if (!strcmp(value, "follow_cursor"))
5823 focus_mode = SWM_FOCUS_FOLLOW;
5824 else if (!strcmp(value, "synergy"))
5825 focus_mode = SWM_FOCUS_SYNERGY;
5826 else
5827 errx(1, "focus_mode");
5828 break;
5829 case SWM_S_SPAWN_ORDER:
5830 if (!strcmp(value, "first"))
5831 spawn_position = SWM_STACK_BOTTOM;
5832 else if (!strcmp(value, "last"))
5833 spawn_position = SWM_STACK_TOP;
5834 else if (!strcmp(value, "next"))
5835 spawn_position = SWM_STACK_ABOVE;
5836 else if (!strcmp(value, "previous"))
5837 spawn_position = SWM_STACK_BELOW;
5838 else
5839 errx(1, "spawn_position");
5840 break;
5841 case SWM_S_SPAWN_TERM:
5842 setconfspawn("term", value, 0);
5843 setconfspawn("spawn_term", value, 0);
5844 break;
5845 case SWM_S_SS_APP:
5846 break;
5847 case SWM_S_SS_ENABLED:
5848 ss_enabled = atoi(value);
5849 break;
5850 case SWM_S_STACK_ENABLED:
5851 stack_enabled = atoi(value);
5852 break;
5853 case SWM_S_TERM_WIDTH:
5854 term_width = atoi(value);
5855 if (term_width < 0)
5856 term_width = 0;
5857 break;
5858 case SWM_S_TITLE_CLASS_ENABLED:
5859 title_class_enabled = atoi(value);
5860 break;
5861 case SWM_S_TITLE_NAME_ENABLED:
5862 title_name_enabled = atoi(value);
5863 break;
5864 case SWM_S_URGENT_ENABLED:
5865 urgent_enabled = atoi(value);
5866 break;
5867 case SWM_S_VERBOSE_LAYOUT:
5868 verbose_layout = atoi(value);
5869 for (i = 0; layouts[i].l_stack != NULL; i++) {
5870 if (verbose_layout)
5871 layouts[i].l_string = fancy_stacker;
5872 else
5873 layouts[i].l_string = plain_stacker;
5874 }
5875 break;
5876 case SWM_S_WINDOW_NAME_ENABLED:
5877 window_name_enabled = atoi(value);
5878 break;
5879 case SWM_S_WORKSPACE_LIMIT:
5880 workspace_limit = atoi(value);
5881 if (workspace_limit > SWM_WS_MAX)
5882 workspace_limit = SWM_WS_MAX;
5883 else if (workspace_limit < 1)
5884 workspace_limit = 1;
5885 break;
5886 default:
5887 return (1);
5888 }
5889 return (0);
5890 }
5891
5892 int
5893 setconfmodkey(char *selector, char *value, int flags)
5894 {
5895 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5896 update_modkey(Mod1Mask);
5897 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5898 update_modkey(Mod2Mask);
5899 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5900 update_modkey(Mod3Mask);
5901 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5902 update_modkey(Mod4Mask);
5903 else
5904 return (1);
5905 return (0);
5906 }
5907
5908 int
5909 setconfcolor(char *selector, char *value, int flags)
5910 {
5911 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5912 return (0);
5913 }
5914
5915 int
5916 setconfregion(char *selector, char *value, int flags)
5917 {
5918 custom_region(value);
5919 return (0);
5920 }
5921
5922 int
5923 setautorun(char *selector, char *value, int flags)
5924 {
5925 int ws_id;
5926 char s[1024];
5927 char *ap, *sp = s;
5928 union arg a;
5929 int argc = 0;
5930 long pid;
5931 struct pid_e *p;
5932
5933 if (getenv("SWM_STARTED"))
5934 return (0);
5935
5936 bzero(s, sizeof s);
5937 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5938 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5939 ws_id--;
5940 if (ws_id < 0 || ws_id >= workspace_limit)
5941 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5942
5943 /*
5944 * This is a little intricate
5945 *
5946 * If the pid already exists we simply reuse it because it means it was
5947 * used before AND not claimed by manage_window. We get away with
5948 * altering it in the parent after INSERT because this can not be a race
5949 */
5950 a.argv = NULL;
5951 while ((ap = strsep(&sp, " \t")) != NULL) {
5952 if (*ap == '\0')
5953 continue;
5954 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5955 argc++;
5956 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5957 err(1, "setautorun: realloc");
5958 a.argv[argc - 1] = ap;
5959 }
5960
5961 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5962 err(1, "setautorun: realloc");
5963 a.argv[argc] = NULL;
5964
5965 if ((pid = fork()) == 0) {
5966 spawn(ws_id, &a, 1);
5967 /* NOTREACHED */
5968 _exit(1);
5969 }
5970 free(a.argv);
5971
5972 /* parent */
5973 p = find_pid(pid);
5974 if (p == NULL) {
5975 p = calloc(1, sizeof *p);
5976 if (p == NULL)
5977 return (1);
5978 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5979 }
5980
5981 p->pid = pid;
5982 p->ws = ws_id;
5983
5984 return (0);
5985 }
5986
5987 int
5988 setlayout(char *selector, char *value, int flags)
5989 {
5990 int ws_id, i, x, mg, ma, si, raise, f = 0;
5991 int st = SWM_V_STACK, num_screens;
5992 char s[1024];
5993 struct workspace *ws;
5994
5995 if (getenv("SWM_STARTED"))
5996 return (0);
5997
5998 bzero(s, sizeof s);
5999 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6000 &ws_id, &mg, &ma, &si, &raise, s) != 6)
6001 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6002 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6003 "<type>'");
6004 ws_id--;
6005 if (ws_id < 0 || ws_id >= workspace_limit)
6006 errx(1, "layout: invalid workspace %d", ws_id + 1);
6007
6008 if (!strcasecmp(s, "vertical"))
6009 st = SWM_V_STACK;
6010 else if (!strcasecmp(s, "vertical_flip")) {
6011 st = SWM_V_STACK;
6012 f = 1;
6013 } else if (!strcasecmp(s, "horizontal"))
6014 st = SWM_H_STACK;
6015 else if (!strcasecmp(s, "horizontal_flip")) {
6016 st = SWM_H_STACK;
6017 f = 1;
6018 } else if (!strcasecmp(s, "fullscreen"))
6019 st = SWM_MAX_STACK;
6020 else
6021 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6022 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6023 "<type>'");
6024
6025 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6026 for (i = 0; i < num_screens; i++) {
6027 ws = (struct workspace *)&screens[i].ws;
6028 ws[ws_id].cur_layout = &layouts[st];
6029
6030 ws[ws_id].always_raise = raise;
6031 if (st == SWM_MAX_STACK)
6032 continue;
6033
6034 /* master grow */
6035 for (x = 0; x < abs(mg); x++) {
6036 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6037 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6038 SWM_ARG_ID_MASTERSHRINK);
6039 stack();
6040 }
6041 /* master add */
6042 for (x = 0; x < abs(ma); x++) {
6043 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6044 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6045 SWM_ARG_ID_MASTERDEL);
6046 stack();
6047 }
6048 /* stack inc */
6049 for (x = 0; x < abs(si); x++) {
6050 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6051 si >= 0 ? SWM_ARG_ID_STACKINC :
6052 SWM_ARG_ID_STACKDEC);
6053 stack();
6054 }
6055 /* Apply flip */
6056 if (f) {
6057 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6058 SWM_ARG_ID_FLIPLAYOUT);
6059 stack();
6060 }
6061 }
6062
6063 return (0);
6064 }
6065
6066 /* config options */
6067 struct config_option {
6068 char *optname;
6069 int (*func)(char*, char*, int);
6070 int funcflags;
6071 };
6072 struct config_option configopt[] = {
6073 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6074 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6075 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6076 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6077 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6078 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6079 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6080 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6081 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6082 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6083 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6084 { "keyboard_mapping", setkeymapping, 0 },
6085 { "bind", setconfbinding, 0 },
6086 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6087 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6088 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6089 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6090 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6091 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6092 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6093 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6094 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6095 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6096 { "modkey", setconfmodkey, 0 },
6097 { "program", setconfspawn, 0 },
6098 { "quirk", setconfquirk, 0 },
6099 { "region", setconfregion, 0 },
6100 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6101 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6102 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6103 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6104 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6105 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6106 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6107 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6108 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6109 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6110 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6111 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6112 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6113 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6114 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6115 { "autorun", setautorun, 0 },
6116 { "layout", setlayout, 0 },
6117 };
6118
6119
6120 int
6121 conf_load(char *filename, int keymapping)
6122 {
6123 FILE *config;
6124 char *line, *cp, *optsub, *optval;
6125 size_t linelen, lineno = 0;
6126 int wordlen, i, optind;
6127 struct config_option *opt;
6128
6129 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6130
6131 if (filename == NULL) {
6132 warnx("conf_load: no filename");
6133 return (1);
6134 }
6135 if ((config = fopen(filename, "r")) == NULL) {
6136 warn("conf_load: fopen: %s", filename);
6137 return (1);
6138 }
6139
6140 while (!feof(config)) {
6141 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6142 == NULL) {
6143 if (ferror(config))
6144 err(1, "%s", filename);
6145 else
6146 continue;
6147 }
6148 cp = line;
6149 cp += strspn(cp, " \t\n"); /* eat whitespace */
6150 if (cp[0] == '\0') {
6151 /* empty line */
6152 free(line);
6153 continue;
6154 }
6155 /* get config option */
6156 wordlen = strcspn(cp, "=[ \t\n");
6157 if (wordlen == 0) {
6158 warnx("%s: line %zd: no option found",
6159 filename, lineno);
6160 goto out;
6161 }
6162 optind = -1;
6163 for (i = 0; i < LENGTH(configopt); i++) {
6164 opt = &configopt[i];
6165 if (!strncasecmp(cp, opt->optname, wordlen) &&
6166 strlen(opt->optname) == wordlen) {
6167 optind = i;
6168 break;
6169 }
6170 }
6171 if (optind == -1) {
6172 warnx("%s: line %zd: unknown option %.*s",
6173 filename, lineno, wordlen, cp);
6174 goto out;
6175 }
6176 if (keymapping && strcmp(opt->optname, "bind")) {
6177 warnx("%s: line %zd: invalid option %.*s",
6178 filename, lineno, wordlen, cp);
6179 goto out;
6180 }
6181 cp += wordlen;
6182 cp += strspn(cp, " \t\n"); /* eat whitespace */
6183 /* get [selector] if any */
6184 optsub = NULL;
6185 if (*cp == '[') {
6186 cp++;
6187 wordlen = strcspn(cp, "]");
6188 if (*cp != ']') {
6189 if (wordlen == 0) {
6190 warnx("%s: line %zd: syntax error",
6191 filename, lineno);
6192 goto out;
6193 }
6194
6195 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6196 -1) {
6197 warnx("%s: line %zd: unable to allocate"
6198 "memory for selector", filename,
6199 lineno);
6200 goto out;
6201 }
6202 }
6203 cp += wordlen;
6204 cp += strspn(cp, "] \t\n"); /* eat trailing */
6205 }
6206 cp += strspn(cp, "= \t\n"); /* eat trailing */
6207 /* get RHS value */
6208 optval = strdup(cp);
6209 /* call function to deal with it all */
6210 if (configopt[optind].func(optsub, optval,
6211 configopt[optind].funcflags) != 0)
6212 errx(1, "%s: line %zd: invalid data for %s",
6213 filename, lineno, configopt[optind].optname);
6214 free(optval);
6215 free(optsub);
6216 free(line);
6217 }
6218
6219 fclose(config);
6220 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6221
6222 return (0);
6223
6224 out:
6225 free(line);
6226 fclose(config);
6227 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6228
6229 return (1);
6230 }
6231
6232 void
6233 set_child_transient(struct ws_win *win, Window *trans)
6234 {
6235 struct ws_win *parent, *w;
6236 XWMHints *wmh = NULL;
6237 struct swm_region *r;
6238 struct workspace *ws;
6239
6240 parent = find_window(win->transient);
6241 if (parent)
6242 parent->child_trans = win;
6243 else {
6244 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6245 " for 0x%x trans 0x%x\n", win->id, win->transient);
6246
6247 if (win->hints == NULL) {
6248 warnx("no hints for 0x%x", win->id);
6249 return;
6250 }
6251
6252 r = root_to_region(win->wa.root);
6253 ws = r->ws;
6254 /* parent doen't exist in our window list */
6255 TAILQ_FOREACH(w, &ws->winlist, entry) {
6256 if (wmh)
6257 XFree(wmh);
6258
6259 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
6260 warnx("can't get hints for 0x%x", w->id);
6261 continue;
6262 }
6263
6264 if (win->hints->window_group != wmh->window_group)
6265 continue;
6266
6267 w->child_trans = win;
6268 win->transient = w->id;
6269 *trans = w->id;
6270 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
6271 "transient to 0x%x\n", win->transient);
6272 break;
6273 }
6274 }
6275
6276 if (wmh)
6277 XFree(wmh);
6278 }
6279
6280 long
6281 window_get_pid(xcb_window_t win)
6282 {
6283 long ret = 0;
6284 const char *errstr;
6285 xcb_atom_t apid;
6286 xcb_get_property_cookie_t pc;
6287 xcb_get_property_reply_t *pr;
6288
6289 apid = get_atom_from_string("_NET_WM_PID");
6290 if (apid == XCB_ATOM_NONE)
6291 goto tryharder;
6292
6293 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6294 pr = xcb_get_property_reply(conn, pc, NULL);
6295 if (!pr)
6296 goto tryharder;
6297 if (pr->type != XCB_ATOM_CARDINAL)
6298 goto tryharder;
6299
6300 ret = *(long *)xcb_get_property_value(pr);
6301 free(pr);
6302
6303 return (ret);
6304
6305 tryharder:
6306 apid = get_atom_from_string("_SWM_PID");
6307 pc = xcb_get_property(conn, False, win, apid, XCB_ATOM_STRING,
6308 0, SWM_PROPLEN);
6309 pr = xcb_get_property_reply(conn, pc, NULL);
6310 if (!pr)
6311 return (0);
6312 if (pr->type != XCB_ATOM_STRING)
6313 free(pr);
6314 return (0);
6315 ret = strtonum(xcb_get_property_value(pr), 0, UINT_MAX, &errstr);
6316 free(pr);
6317
6318 return (ret);
6319 }
6320
6321 struct ws_win *
6322 manage_window(xcb_window_t id)
6323 {
6324 Window trans = 0;
6325 struct workspace *ws;
6326 struct ws_win *win, *ww;
6327 int format, i, ws_idx, n, border_me = 0;
6328 unsigned long nitems, bytes;
6329 Atom ws_idx_atom = 0, type;
6330 Atom *prot = NULL, *pp;
6331 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6332 struct swm_region *r;
6333 const char *errstr;
6334 struct pid_e *p;
6335 struct quirk *qp;
6336 uint32_t event_mask;
6337
6338 if ((win = find_window(id)) != NULL)
6339 return (win); /* already being managed */
6340
6341 /* see if we are on the unmanaged list */
6342 if ((win = find_unmanaged_window(id)) != NULL) {
6343 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6344 "window: 0x%x\n", win->id);
6345 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6346 if (win->transient)
6347 set_child_transient(win, &trans);
6348
6349 if (trans && (ww = find_window(trans)))
6350 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6351 else if ((ww = win->ws->focus) &&
6352 spawn_position == SWM_STACK_ABOVE)
6353 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus,
6354 win, entry);
6355 else if (ww && spawn_position == SWM_STACK_BELOW)
6356 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6357 else switch (spawn_position) {
6358 default:
6359 case SWM_STACK_TOP:
6360 case SWM_STACK_ABOVE:
6361 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6362 break;
6363 case SWM_STACK_BOTTOM:
6364 case SWM_STACK_BELOW:
6365 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6366 }
6367
6368 ewmh_update_actions(win);
6369 return (win);
6370 }
6371
6372 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6373 err(1, "manage_window: calloc: failed to allocate memory for "
6374 "new window");
6375
6376 win->id = id;
6377 win->bordered = 0;
6378
6379 /* see if we need to override the workspace */
6380 p = find_pid(window_get_pid(id));
6381
6382 /* Get all the window data in one shot */
6383 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
6384 if (ws_idx_atom) {
6385 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
6386 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
6387 }
6388 XGetWindowAttributes(display, id, &win->wa);
6389 XGetWMNormalHints(display, id, &win->sh, &win->sh_mask);
6390 win->hints = XGetWMHints(display, id);
6391 XGetTransientForHint(display, id, &trans);
6392 if (trans) {
6393 win->transient = trans;
6394 set_child_transient(win, &trans);
6395 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, "
6396 "transient: 0x%x\n", win->id, win->transient);
6397 }
6398
6399 /* get supported protocols */
6400 if (XGetWMProtocols(display, id, &prot, &n)) {
6401 for (i = 0, pp = prot; i < n; i++, pp++) {
6402 if (*pp == takefocus)
6403 win->take_focus = 1;
6404 if (*pp == adelete)
6405 win->can_delete = 1;
6406 }
6407 if (prot)
6408 XFree(prot);
6409 }
6410
6411 win->iconic = get_iconic(win);
6412
6413 /*
6414 * Figure out where to put the window. If it was previously assigned to
6415 * a workspace (either by spawn() or manually moving), and isn't
6416 * transient, * put it in the same workspace
6417 */
6418 r = root_to_region(win->wa.root);
6419 if (p) {
6420 ws = &r->s->ws[p->ws];
6421 TAILQ_REMOVE(&pidlist, p, entry);
6422 free(p);
6423 p = NULL;
6424 } else if (prop && win->transient == 0) {
6425 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
6426 ws_idx = strtonum((const char *)prop, 0, workspace_limit - 1,
6427 &errstr);
6428 if (errstr) {
6429 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
6430 errstr, prop);
6431 }
6432 ws = &r->s->ws[ws_idx];
6433 } else {
6434 ws = r->ws;
6435 /* this should launch transients in the same ws as parent */
6436 if (id && trans)
6437 if ((ww = find_window(trans)) != NULL)
6438 if (ws->r) {
6439 ws = ww->ws;
6440 if (ww->ws->r)
6441 r = ww->ws->r;
6442 else
6443 warnx("manage_window: fix this "
6444 "bug mcbride");
6445 border_me = 1;
6446 }
6447 }
6448
6449 /* set up the window layout */
6450 win->id = id;
6451 win->ws = ws;
6452 win->s = r->s; /* this never changes */
6453 if (trans && (ww = find_window(trans)))
6454 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6455 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
6456 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
6457 entry);
6458 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
6459 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
6460 else switch (spawn_position) {
6461 default:
6462 case SWM_STACK_TOP:
6463 case SWM_STACK_ABOVE:
6464 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6465 break;
6466 case SWM_STACK_BOTTOM:
6467 case SWM_STACK_BELOW:
6468 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6469 }
6470
6471 /* ignore window border if there is one. */
6472 WIDTH(win) = win->wa.width;
6473 HEIGHT(win) = win->wa.height;
6474 X(win) = win->wa.x + win->wa.border_width;
6475 Y(win) = win->wa.y + win->wa.border_width;
6476 win->bordered = 0;
6477 win->g_floatvalid = 0;
6478 win->floatmaxed = 0;
6479 win->ewmh_flags = 0;
6480
6481 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%x, (x,y) w x h: "
6482 "(%d,%d) %d x %d, ws: %d\n", win->id, X(win), Y(win), WIDTH(win),
6483 HEIGHT(win), ws->idx);
6484
6485 constrain_window(win, r, 0);
6486
6487 /* Set window properties so we can remember this after reincarnation */
6488 if (ws_idx_atom && prop == NULL &&
6489 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6490 SWM_PROPLEN) {
6491 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6492 ws_idx_str);
6493 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
6494 PropModeReplace, ws_idx_str, strlen((char *)ws_idx_str));
6495 }
6496 if (prop)
6497 XFree(prop);
6498
6499 ewmh_autoquirk(win);
6500
6501 if (XGetClassHint(display, win->id, &win->ch)) {
6502 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6503 win->ch.res_class, win->ch.res_name);
6504
6505 /* java is retarded so treat it special */
6506 if (strstr(win->ch.res_name, "sun-awt")) {
6507 win->java = 1;
6508 border_me = 1;
6509 }
6510
6511 TAILQ_FOREACH(qp, &quirks, entry) {
6512 if (!strcmp(win->ch.res_class, qp->class) &&
6513 !strcmp(win->ch.res_name, qp->name)) {
6514 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6515 "class: %s, name: %s\n", win->ch.res_class,
6516 win->ch.res_name);
6517 if (qp->quirk & SWM_Q_FLOAT) {
6518 win->floating = 1;
6519 border_me = 1;
6520 }
6521 win->quirks = qp->quirk;
6522 }
6523 }
6524 }
6525
6526 /* alter window position if quirky */
6527 if (win->quirks & SWM_Q_ANYWHERE) {
6528 win->manual = 1; /* don't center the quirky windows */
6529 if (bar_enabled && Y(win) < bar_height)
6530 Y(win) = bar_height;
6531 if (WIDTH(win) + X(win) > WIDTH(r))
6532 X(win) = WIDTH(r) - WIDTH(win) - 2;
6533 border_me = 1;
6534 }
6535
6536 /* Reset font sizes (the bruteforce way; no default keybinding). */
6537 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6538 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6539 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6540 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6541 fake_keypress(win, XK_KP_Add, ShiftMask);
6542 }
6543
6544 ewmh_get_win_state(win);
6545 ewmh_update_actions(win);
6546 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6547
6548 /* border me */
6549 if (border_me) {
6550 win->bordered = 1;
6551 X(win) -= border_width;
6552 Y(win) -= border_width;
6553 update_window(win);
6554 }
6555
6556 event_mask = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE |
6557 XCB_EVENT_MASK_PROPERTY_CHANGE |
6558 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
6559
6560 xcb_change_window_attributes(conn, id, XCB_CW_EVENT_MASK, &event_mask);
6561
6562 /* floaters need to be mapped if they are in the current workspace */
6563 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6564 map_window_raised(win->id);
6565
6566 return (win);
6567 }
6568
6569 void
6570 free_window(struct ws_win *win)
6571 {
6572 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
6573
6574 if (win == NULL)
6575 return;
6576
6577 /* needed for restart wm */
6578 set_win_state(win, XCB_ICCCM_WM_STATE_WITHDRAWN);
6579
6580 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6581
6582 if (win->ch.res_class)
6583 XFree(win->ch.res_class);
6584 if (win->ch.res_name)
6585 XFree(win->ch.res_name);
6586
6587 kill_refs(win);
6588
6589 /* paint memory */
6590 memset(win, 0xff, sizeof *win); /* XXX kill later */
6591
6592 free(win);
6593 }
6594
6595 void
6596 unmanage_window(struct ws_win *win)
6597 {
6598 struct ws_win *parent;
6599 xcb_screen_t *screen;
6600
6601 if (win == NULL)
6602 return;
6603
6604 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
6605
6606 if (win->transient) {
6607 parent = find_window(win->transient);
6608 if (parent)
6609 parent->child_trans = NULL;
6610 }
6611
6612 /* focus on root just in case */
6613 screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
6614 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
6615 screen->root, XCB_CURRENT_TIME);
6616
6617 focus_prev(win);
6618
6619 if (win->hints) {
6620 XFree(win->hints);
6621 win->hints = NULL;
6622 }
6623
6624 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6625 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6626 }
6627
6628 void
6629 focus_magic(struct ws_win *win)
6630 {
6631 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%x\n", WINID(win));
6632
6633 if (win == NULL) {
6634 /* if there are no windows clear the status-bar */
6635 bar_update();
6636 return;
6637 }
6638
6639 if (win->child_trans) {
6640 /* win = parent & has a transient so focus on that */
6641 if (win->java) {
6642 focus_win(win->child_trans);
6643 if (win->child_trans->take_focus)
6644 client_msg(win, takefocus);
6645 } else {
6646 /* make sure transient hasn't disappeared */
6647 if (validate_win(win->child_trans) == 0) {
6648 focus_win(win->child_trans);
6649 if (win->child_trans->take_focus)
6650 client_msg(win->child_trans, takefocus);
6651 } else {
6652 win->child_trans = NULL;
6653 focus_win(win);
6654 if (win->take_focus)
6655 client_msg(win, takefocus);
6656 }
6657 }
6658 } else {
6659 /* regular focus */
6660 focus_win(win);
6661 if (win->take_focus)
6662 client_msg(win, takefocus);
6663 }
6664 }
6665
6666 void
6667 expose(XEvent *e)
6668 {
6669 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%lx\n", e->xexpose.window);
6670 }
6671
6672 void
6673 keypress(XEvent *e)
6674 {
6675 KeySym keysym;
6676 XKeyEvent *ev = &e->xkey;
6677 struct key *kp;
6678 struct swm_region *r;
6679
6680 keysym = XkbKeycodeToKeysym(display, (KeyCode)ev->keycode, 0, 0);
6681 if ((kp = key_lookup(CLEANMASK(ev->state), keysym)) == NULL)
6682 return;
6683 if (keyfuncs[kp->funcid].func == NULL)
6684 return;
6685
6686 r = root_to_region(ev->root);
6687 if (kp->funcid == kf_spawn_custom)
6688 spawn_custom(r, &(keyfuncs[kp->funcid].args), kp->spawn_name);
6689 else
6690 keyfuncs[kp->funcid].func(r, &(keyfuncs[kp->funcid].args));
6691 }
6692
6693 void
6694 buttonpress(XEvent *e)
6695 {
6696 struct ws_win *win;
6697 int i, action;
6698 XButtonPressedEvent *ev = &e->xbutton;
6699
6700 if ((win = find_window(ev->window)) == NULL)
6701 return;
6702
6703 focus_magic(win);
6704 action = client_click;
6705
6706 for (i = 0; i < LENGTH(buttons); i++)
6707 if (action == buttons[i].action && buttons[i].func &&
6708 buttons[i].button == ev->button &&
6709 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
6710 buttons[i].func(win, &buttons[i].args);
6711 }
6712
6713 void
6714 configurerequest(XEvent *e)
6715 {
6716 XConfigureRequestEvent *ev = &e->xconfigurerequest;
6717 struct ws_win *win;
6718 int new = 0;
6719 XWindowChanges wc;
6720
6721 if ((win = find_window(ev->window)) == NULL)
6722 if ((win = find_unmanaged_window(ev->window)) == NULL)
6723 new = 1;
6724
6725 if (new) {
6726 bzero(&wc, sizeof wc);
6727 wc.x = ev->x;
6728 wc.y = ev->y;
6729 wc.width = ev->width;
6730 wc.height = ev->height;
6731 wc.border_width = ev->border_width;
6732 wc.sibling = ev->above;
6733 wc.stack_mode = ev->detail;
6734
6735 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: 0x%lx, "
6736 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", ev->window,
6737 YESNO(new), wc.x, wc.y, wc.width, wc.height);
6738
6739 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
6740 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
6741 !(win->sh_mask & EWMH_F_FULLSCREEN)) {
6742 win->g_float.x = ev->x - X(win->ws->r);
6743 win->g_float.y = ev->y - Y(win->ws->r);
6744 win->g_float.w = ev->width;
6745 win->g_float.h = ev->height;
6746 win->g_floatvalid = 1;
6747
6748 if (win->floating) {
6749 win->g = win->g_float;
6750 win->g.x += X(win->ws->r);
6751 win->g.y += Y(win->ws->r);
6752 update_window(win);
6753 } else {
6754 config_win(win, ev);
6755 }
6756 } else {
6757 config_win(win, ev);
6758 }
6759 }
6760
6761 void
6762 configurenotify(XEvent *e)
6763 {
6764 struct ws_win *win;
6765
6766 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%lx\n",
6767 e->xconfigure.window);
6768
6769 win = find_window(e->xconfigure.window);
6770 if (win) {
6771 XGetWMNormalHints(display, win->id, &win->sh, &win->sh_mask);
6772 adjust_font(win);
6773 if (font_adjusted)
6774 stack();
6775 if (focus_mode == SWM_FOCUS_DEFAULT)
6776 drain_enter_notify();
6777 }
6778 }
6779
6780 void
6781 destroynotify(XEvent *e)
6782 {
6783 struct ws_win *win;
6784 XDestroyWindowEvent *ev = &e->xdestroywindow;
6785
6786 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%lx\n", ev->window);
6787
6788 if ((win = find_window(ev->window)) == NULL) {
6789 if ((win = find_unmanaged_window(ev->window)) == NULL)
6790 return;
6791 free_window(win);
6792 return;
6793 }
6794
6795 /* make sure we focus on something */
6796 win->floating = 0;
6797
6798 unmanage_window(win);
6799 stack();
6800 if (focus_mode == SWM_FOCUS_DEFAULT)
6801 drain_enter_notify();
6802 free_window(win);
6803 }
6804
6805 void
6806 enternotify(XEvent *e)
6807 {
6808 XCrossingEvent *ev = &e->xcrossing;
6809 XEvent cne;
6810 struct ws_win *win;
6811 #if 0
6812 struct ws_win *w;
6813 Window focus_return;
6814 int revert_to_return;
6815 #endif
6816 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%lx, mode: %d, detail: "
6817 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6818 "state: %d\n", ev->window, ev->mode, ev->detail, ev->root,
6819 ev->subwindow, YESNO(ev->same_screen), YESNO(ev->focus), ev->state);
6820
6821 if (ev->mode != NotifyNormal) {
6822 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6823 "cursor grab.\n");
6824 return;
6825 }
6826
6827 switch (focus_mode) {
6828 case SWM_FOCUS_DEFAULT:
6829 break;
6830 case SWM_FOCUS_FOLLOW:
6831 break;
6832 case SWM_FOCUS_SYNERGY:
6833 #if 0
6834 /*
6835 * all these checks need to be in this order because the
6836 * XCheckTypedWindowEvent relies on weeding out the previous events
6837 *
6838 * making this code an option would enable a follow mouse for focus
6839 * feature
6840 */
6841
6842 /*
6843 * state is set when we are switching workspaces and focus is set when
6844 * the window or a subwindow already has focus (occurs during restart).
6845 *
6846 * Only honor the focus flag if last_focus_event is not FocusOut,
6847 * this allows spectrwm to continue to control focus when another
6848 * program is also playing with it.
6849 */
6850 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6851 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6852 return;
6853 }
6854
6855 /*
6856 * happens when a window is created or destroyed and the border
6857 * crosses the mouse pointer and when switching ws
6858 *
6859 * we need the subwindow test to see if we came from root in order
6860 * to give focus to floaters
6861 */
6862 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6863 ev->subwindow == 0) {
6864 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6865 return;
6866 }
6867
6868 /* this window already has focus */
6869 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6870 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6871 return;
6872 }
6873
6874 /* this window is being deleted or moved to another ws */
6875 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6876 &cne) == True) {
6877 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6878 XPutBackEvent(display, &cne);
6879 return;
6880 }
6881
6882 if ((win = find_window(ev->window)) == NULL) {
6883 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6884 return;
6885 }
6886
6887 /*
6888 * In fullstack kill all enters unless they come from a different ws
6889 * (i.e. another region) or focus has been grabbed externally.
6890 */
6891 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6892 last_focus_event != FocusOut) {
6893 XGetInputFocus(display, &focus_return, &revert_to_return);
6894 if ((w = find_window(focus_return)) == NULL ||
6895 w->ws == win->ws) {
6896 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6897 return;
6898 }
6899 }
6900 #endif
6901 break;
6902 }
6903
6904 if ((win = find_window(ev->window)) == NULL) {
6905 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
6906 return;
6907 }
6908
6909 /*
6910 * if we have more enternotifies let them handle it in due time
6911 */
6912 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
6913 DNPRINTF(SWM_D_EVENT,
6914 "ignoring enternotify: got more enternotify\n");
6915 XPutBackEvent(display, &cne);
6916 return;
6917 }
6918
6919 focus_magic(win);
6920 }
6921
6922 /* lets us use one switch statement for arbitrary mode/detail combinations */
6923 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6924
6925 void
6926 focusevent(XEvent *e)
6927 {
6928 #if 0
6929 struct ws_win *win;
6930 u_int32_t mode_detail;
6931 XFocusChangeEvent *ev = &e->xfocus;
6932
6933 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: 0x%lx mode: %d "
6934 "detail: %d\n", ev->type == FocusIn ? "entering" : "leaving",
6935 ev->window, ev->mode, ev->detail);
6936
6937 if (last_focus_event == ev->type) {
6938 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
6939 return;
6940 }
6941
6942 last_focus_event = ev->type;
6943 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
6944
6945 switch (mode_detail) {
6946 /* synergy client focus operations */
6947 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
6948 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
6949
6950 /* synergy server focus operations */
6951 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
6952
6953 /* Entering applications like rdesktop that mangle the pointer */
6954 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
6955
6956 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
6957 XSetWindowBorder(display, win->id,
6958 win->ws->r->s->c[ev->type == FocusIn ?
6959 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
6960 break;
6961 default:
6962 warnx("ignoring focusevent");
6963 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
6964 break;
6965 }
6966 #endif
6967 }
6968
6969 void
6970 mapnotify(XEvent *e)
6971 {
6972 struct ws_win *win;
6973 XMapEvent *ev = &e->xmap;
6974
6975 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%lx\n", ev->window);
6976
6977 win = manage_window(ev->window);
6978 if (win)
6979 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
6980
6981 /*
6982 * focus_win can only set input focus on a mapped window.
6983 * make sure the window really has focus since it is just being mapped.
6984 */
6985 if (win->ws->focus == win)
6986 focus_win(win);
6987 }
6988
6989 void
6990 mappingnotify(XEvent *e)
6991 {
6992 XMappingEvent *ev = &e->xmapping;
6993
6994 XRefreshKeyboardMapping(ev);
6995 if (ev->request == MappingKeyboard)
6996 grabkeys();
6997 }
6998
6999 void
7000 maprequest(XEvent *e)
7001 {
7002 struct ws_win *win;
7003 struct swm_region *r;
7004 XWindowAttributes wa;
7005 XMapRequestEvent *ev = &e->xmaprequest;
7006
7007 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%lx\n",
7008 e->xmaprequest.window);
7009
7010 if (!XGetWindowAttributes(display, ev->window, &wa))
7011 return;
7012 if (wa.override_redirect)
7013 return;
7014
7015 win = manage_window(e->xmaprequest.window);
7016 if (win == NULL)
7017 return; /* can't happen */
7018
7019 stack();
7020
7021 /* make new win focused */
7022 r = root_to_region(win->wa.root);
7023 if (win->ws == r->ws)
7024 focus_magic(win);
7025 }
7026
7027 void
7028 propertynotify(XEvent *e)
7029 {
7030 struct ws_win *win;
7031 XPropertyEvent *ev = &e->xproperty;
7032 #ifdef SWM_DEBUG
7033 char *name;
7034 name = XGetAtomName(display, ev->atom);
7035 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%lx, atom: %s\n",
7036 ev->window, name);
7037 XFree(name);
7038 #endif
7039
7040 win = find_window(ev->window);
7041 if (win == NULL)
7042 return;
7043
7044 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
7045 update_iconic(win, 0);
7046 map_window_raised(win->id);
7047 stack();
7048 focus_win(win);
7049 return;
7050 }
7051
7052 switch (ev->atom) {
7053 #if 0
7054 case XA_WM_NORMAL_HINTS:
7055 long mask;
7056 XGetWMNormalHints(display, win->id, &win->sh, &mask);
7057 warnx("normal hints: flag 0x%x", win->sh.flags);
7058 if (win->sh.flags & PMinSize) {
7059 WIDTH(win) = win->sh.min_width;
7060 HEIGHT(win) = win->sh.min_height;
7061 warnx("min %d %d", WIDTH(win), HEIGHT(win));
7062 }
7063 XMoveResizeWindow(display, win->id,
7064 X(win), Y(win), WIDTH(win), HEIGHT(win));
7065 #endif
7066 case XA_WM_CLASS:
7067 case XA_WM_NAME:
7068 bar_update();
7069 break;
7070 default:
7071 break;
7072 }
7073 }
7074
7075 void
7076 unmapnotify(XEvent *e)
7077 {
7078 struct ws_win *win;
7079
7080 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%lx\n", e->xunmap.window);
7081
7082 /* determine if we need to help unmanage this window */
7083 win = find_window(e->xunmap.window);
7084 if (win == NULL)
7085 return;
7086
7087 if (getstate(e->xunmap.window) == XCB_ICCCM_WM_STATE_NORMAL) {
7088 unmanage_window(win);
7089 stack();
7090
7091 /* giant hack for apps that don't destroy transient windows */
7092 /* eat a bunch of events to prevent remanaging the window */
7093 XEvent cne;
7094 while (XCheckWindowEvent(display, e->xunmap.window,
7095 EnterWindowMask, &cne))
7096 ;
7097 while (XCheckWindowEvent(display, e->xunmap.window,
7098 StructureNotifyMask, &cne))
7099 ;
7100 while (XCheckWindowEvent(display, e->xunmap.window,
7101 SubstructureNotifyMask, &cne))
7102 ;
7103 /* resend unmap because we ated it */
7104 XUnmapWindow(display, e->xunmap.window);
7105 }
7106
7107 if (focus_mode == SWM_FOCUS_DEFAULT)
7108 drain_enter_notify();
7109 }
7110
7111 void
7112 visibilitynotify(XEvent *e)
7113 {
7114 int i, num_screens;
7115 struct swm_region *r;
7116
7117 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%lx\n",
7118 e->xvisibility.window);
7119
7120 if (e->xvisibility.state == VisibilityUnobscured) {
7121 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7122 for (i = 0; i < num_screens; i++)
7123 TAILQ_FOREACH(r, &screens[i].rl, entry)
7124 if (e->xvisibility.window == WINID(r->bar))
7125 bar_update();
7126 }
7127 }
7128
7129 void
7130 clientmessage(XEvent *e)
7131 {
7132 XClientMessageEvent *ev;
7133 struct ws_win *win;
7134
7135 ev = &e->xclient;
7136
7137 win = find_window(ev->window);
7138 if (win == NULL) {
7139 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7140 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
7141 "unmanaged window.\n");
7142 e->xmaprequest.window = ev->window;
7143 maprequest(e);
7144 }
7145 return;
7146 }
7147
7148 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx, type: %ld\n",
7149 ev->window, ev->message_type);
7150
7151 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7152 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7153 focus_win(win);
7154 }
7155 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
7156 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7157 if (win->can_delete)
7158 client_msg(win, adelete);
7159 else
7160 xcb_kill_client(conn, win->id);
7161 }
7162 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7163 DNPRINTF(SWM_D_EVENT,
7164 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7165 if (win->floating) {
7166 if (ev->data.l[0] & (1<<8)) /* x */
7167 X(win) = ev->data.l[1];
7168 if (ev->data.l[0] & (1<<9)) /* y */
7169 Y(win) = ev->data.l[2];
7170 if (ev->data.l[0] & (1<<10)) /* width */
7171 WIDTH(win) = ev->data.l[3];
7172 if (ev->data.l[0] & (1<<11)) /* height */
7173 HEIGHT(win) = ev->data.l[4];
7174
7175 update_window(win);
7176 }
7177 else {
7178 /* TODO: Change stack sizes */
7179 /* notify no change was made. */
7180 config_win(win, NULL);
7181 }
7182 }
7183 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
7184 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7185 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
7186 if (ev->data.l[2])
7187 ewmh_update_win_state(win, ev->data.l[2],
7188 ev->data.l[0]);
7189
7190 stack();
7191 }
7192 }
7193
7194 int
7195 xerror_start(Display *d, XErrorEvent *ee)
7196 {
7197 other_wm = 1;
7198 return (-1);
7199 }
7200
7201 int
7202 xerror(Display *d, XErrorEvent *ee)
7203 {
7204 /* warnx("error: %p %p", display, ee); */
7205 return (-1);
7206 }
7207
7208 int
7209 active_wm(void)
7210 {
7211 other_wm = 0;
7212 xerrorxlib = XSetErrorHandler(xerror_start);
7213
7214 /* this causes an error if some other window manager is running */
7215 XSelectInput(display, DefaultRootWindow(display),
7216 SubstructureRedirectMask);
7217 do_sync();
7218 if (other_wm)
7219 return (1);
7220
7221 XSetErrorHandler(xerror);
7222 do_sync();
7223 return (0);
7224 }
7225
7226 void
7227 new_region(struct swm_screen *s, int x, int y, int w, int h)
7228 {
7229 struct swm_region *r, *n;
7230 struct workspace *ws = NULL;
7231 int i;
7232
7233 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7234 s->idx, w, h, x, y);
7235
7236 /* remove any conflicting regions */
7237 n = TAILQ_FIRST(&s->rl);
7238 while (n) {
7239 r = n;
7240 n = TAILQ_NEXT(r, entry);
7241 if (X(r) < (x + w) &&
7242 (X(r) + WIDTH(r)) > x &&
7243 Y(r) < (y + h) &&
7244 (Y(r) + HEIGHT(r)) > y) {
7245 if (r->ws->r != NULL)
7246 r->ws->old_r = r->ws->r;
7247 r->ws->r = NULL;
7248 bar_cleanup(r);
7249 TAILQ_REMOVE(&s->rl, r, entry);
7250 TAILQ_INSERT_TAIL(&s->orl, r, entry);
7251 }
7252 }
7253
7254 /* search old regions for one to reuse */
7255
7256 /* size + location match */
7257 TAILQ_FOREACH(r, &s->orl, entry)
7258 if (X(r) == x && Y(r) == y &&
7259 HEIGHT(r) == h && WIDTH(r) == w)
7260 break;
7261
7262 /* size match */
7263 TAILQ_FOREACH(r, &s->orl, entry)
7264 if (HEIGHT(r) == h && WIDTH(r) == w)
7265 break;
7266
7267 if (r != NULL) {
7268 TAILQ_REMOVE(&s->orl, r, entry);
7269 /* try to use old region's workspace */
7270 if (r->ws->r == NULL)
7271 ws = r->ws;
7272 } else
7273 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7274 err(1, "new_region: calloc: failed to allocate memory "
7275 "for screen");
7276
7277 /* if we don't have a workspace already, find one */
7278 if (ws == NULL) {
7279 for (i = 0; i < workspace_limit; i++)
7280 if (s->ws[i].r == NULL) {
7281 ws = &s->ws[i];
7282 break;
7283 }
7284 }
7285
7286 if (ws == NULL)
7287 errx(1, "new_region: no free workspaces");
7288
7289 X(r) = x;
7290 Y(r) = y;
7291 WIDTH(r) = w;
7292 HEIGHT(r) = h;
7293 r->s = s;
7294 r->ws = ws;
7295 r->ws_prior = NULL;
7296 ws->r = r;
7297 outputs++;
7298 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7299 }
7300
7301 void
7302 scan_xrandr(int i)
7303 {
7304 #ifdef SWM_XRR_HAS_CRTC
7305 int c;
7306 int ncrtc = 0;
7307 #endif /* SWM_XRR_HAS_CRTC */
7308 struct swm_region *r;
7309 int num_screens;
7310 xcb_randr_get_screen_resources_current_cookie_t src;
7311 xcb_randr_get_screen_resources_current_reply_t *srr;
7312 xcb_randr_get_crtc_info_cookie_t cic;
7313 xcb_randr_get_crtc_info_reply_t *cir = NULL;
7314 xcb_randr_crtc_t *crtc;
7315
7316 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7317 if (i >= num_screens)
7318 errx(1, "scan_xrandr: invalid screen");
7319
7320 /* remove any old regions */
7321 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7322 r->ws->old_r = r->ws->r = NULL;
7323 bar_cleanup(r);
7324 TAILQ_REMOVE(&screens[i].rl, r, entry);
7325 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7326 }
7327 outputs = 0;
7328
7329 /* map virtual screens onto physical screens */
7330 #ifdef SWM_XRR_HAS_CRTC
7331 if (xrandr_support) {
7332 src = xcb_randr_get_screen_resources_current(conn,
7333 screens[i].root);
7334 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
7335 NULL);
7336 if (srr == NULL)
7337 new_region(&screens[i], 0, 0,
7338 DisplayWidth(display, i),
7339 DisplayHeight(display, i));
7340 else
7341 ncrtc = srr->num_crtcs;
7342 for (c = 0; c < ncrtc; c++) {
7343 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
7344 cic = xcb_randr_get_crtc_info(conn, crtc[c],
7345 XCB_CURRENT_TIME);
7346 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
7347 if (cir && cir->num_outputs == 0)
7348 continue;
7349
7350 if (cir == NULL || cir->mode == 0)
7351 new_region(&screens[i], 0, 0,
7352 DisplayWidth(display, i),
7353 DisplayHeight(display, i));
7354 else
7355 new_region(&screens[i],
7356 cir->x, cir->y, cir->width, cir->height);
7357 }
7358 if (srr)
7359 free(srr);
7360 if (cir)
7361 free(cir);
7362 } else
7363 #endif /* SWM_XRR_HAS_CRTC */
7364 {
7365 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
7366 DisplayHeight(display, i));
7367 }
7368 }
7369
7370 void
7371 screenchange(XEvent *e)
7372 {
7373 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
7374 struct swm_region *r;
7375 int i, num_screens;
7376
7377 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%lx\n", xe->root);
7378
7379 if (!XRRUpdateConfiguration(e))
7380 return;
7381
7382 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7383 /* silly event doesn't include the screen index */
7384 for (i = 0; i < num_screens; i++)
7385 if (screens[i].root == xe->root)
7386 break;
7387 if (i >= num_screens)
7388 errx(1, "screenchange: screen not found");
7389
7390 /* brute force for now, just re-enumerate the regions */
7391 scan_xrandr(i);
7392
7393 /* add bars to all regions */
7394 for (i = 0; i < num_screens; i++)
7395 TAILQ_FOREACH(r, &screens[i].rl, entry)
7396 bar_setup(r);
7397 stack();
7398 if (focus_mode == SWM_FOCUS_DEFAULT)
7399 drain_enter_notify();
7400 }
7401
7402 void
7403 grab_windows(void)
7404 {
7405 xcb_window_t *wins = NULL;
7406 int no;
7407 int i, j, num_screens;
7408 uint16_t state, manage;
7409
7410 xcb_query_tree_cookie_t qtc;
7411 xcb_query_tree_reply_t *qtr;
7412 xcb_get_window_attributes_cookie_t c;
7413 xcb_get_window_attributes_reply_t *r;
7414 xcb_get_property_cookie_t pc;
7415
7416 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7417 for (i = 0; i < num_screens; i++) {
7418 qtc = xcb_query_tree(conn, screens[i].root);
7419 qtr = xcb_query_tree_reply(conn, qtc, NULL);
7420 if (!qtr)
7421 continue;
7422 wins = xcb_query_tree_children(qtr);
7423 no = xcb_query_tree_children_length(qtr);
7424 /* attach windows to a region */
7425 /* normal windows */
7426 for (j = 0; j < no; j++) {
7427 c = xcb_get_window_attributes(conn, wins[j]);
7428 r = xcb_get_window_attributes_reply(conn, c, NULL);
7429 if (!r)
7430 continue;
7431 if (r->override_redirect) {
7432 free(r);
7433 continue;
7434 }
7435
7436 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7437 if (xcb_icccm_get_wm_transient_for_reply(conn, pc, &wins[j],
7438 NULL)) {
7439 free(r);
7440 continue;
7441 }
7442
7443 state = getstate(wins[j]);
7444 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7445 if (r->map_state == XCB_MAP_STATE_VIEWABLE || manage)
7446 manage_window(wins[j]);
7447 free(r);
7448 }
7449 /* transient windows */
7450 for (j = 0; j < no; j++) {
7451 c = xcb_get_window_attributes(conn, wins[j]);
7452 r = xcb_get_window_attributes_reply(conn, c, NULL);
7453 if (!r)
7454 continue;
7455 if (r->override_redirect) {
7456 free(r);
7457 continue;
7458 }
7459 free(r);
7460
7461 state = getstate(wins[j]);
7462 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
7463 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
7464 if (xcb_icccm_get_wm_transient_for_reply(conn, pc, &wins[j],
7465 NULL) && manage)
7466 manage_window(wins[j]);
7467 }
7468 free(qtr);
7469 }
7470 }
7471
7472 void
7473 setup_screens(void)
7474 {
7475 int i, j, k, num_screens;
7476 struct workspace *ws;
7477 XGCValues gcv;
7478 const xcb_query_extension_reply_t *qep;
7479 xcb_randr_query_version_cookie_t c;
7480 xcb_randr_query_version_reply_t *r;
7481
7482 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7483 if ((screens = calloc(num_screens,
7484 sizeof(struct swm_screen))) == NULL)
7485 err(1, "setup_screens: calloc: failed to allocate memory for "
7486 "screens");
7487
7488 /* initial Xrandr setup */
7489 xrandr_support = False;
7490 c = xcb_randr_query_version(conn, True, True);
7491 r = xcb_randr_query_version_reply(conn, c, NULL);
7492 if (r) {
7493 if (r->major_version >= 1)
7494 xrandr_support = True;
7495 free(r);
7496 }
7497 qep = xcb_get_extension_data(conn, &xcb_randr_id);
7498 xrandr_eventbase = qep->first_event;
7499
7500 /* map physical screens */
7501 for (i = 0; i < num_screens; i++) {
7502 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7503 screens[i].idx = i;
7504 TAILQ_INIT(&screens[i].rl);
7505 TAILQ_INIT(&screens[i].orl);
7506 screens[i].root = RootWindow(display, i);
7507
7508 /* set default colors */
7509 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7510 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7511 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7512 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7513 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7514
7515 /* create graphics context on screen */
7516 gcv.graphics_exposures = 0;
7517 screens[i].bar_gc = XCreateGC(display, screens[i].root,
7518 GCGraphicsExposures, &gcv);
7519
7520 /* set default cursor */
7521 XDefineCursor(display, screens[i].root,
7522 XCreateFontCursor(display, XC_left_ptr));
7523
7524 /* init all workspaces */
7525 /* XXX these should be dynamically allocated too */
7526 for (j = 0; j < SWM_WS_MAX; j++) {
7527 ws = &screens[i].ws[j];
7528 ws->idx = j;
7529 ws->name = NULL;
7530 ws->focus = NULL;
7531 ws->r = NULL;
7532 ws->old_r = NULL;
7533 TAILQ_INIT(&ws->winlist);
7534 TAILQ_INIT(&ws->unmanagedlist);
7535
7536 for (k = 0; layouts[k].l_stack != NULL; k++)
7537 if (layouts[k].l_config != NULL)
7538 layouts[k].l_config(ws,
7539 SWM_ARG_ID_STACKINIT);
7540 ws->cur_layout = &layouts[0];
7541 ws->cur_layout->l_string(ws);
7542 }
7543
7544 scan_xrandr(i);
7545
7546 if (xrandr_support)
7547 xcb_randr_select_input(conn, screens[i].root,
7548 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
7549 }
7550 }
7551
7552 void
7553 setup_globals(void)
7554 {
7555 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7556 err(1, "setup_globals: strdup: failed to allocate memory.");
7557
7558 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7559 err(1, "setup_globals: strdup: failed to allocate memory.");
7560 }
7561
7562 void
7563 workaround(void)
7564 {
7565 int i, num_screens;
7566 xcb_atom_t netwmcheck, netwmname, utf8_string;
7567 xcb_window_t root, win;
7568
7569 /* work around sun jdk bugs, code from wmname */
7570 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7571 netwmname = get_atom_from_string("_NET_WM_NAME");
7572 utf8_string = get_atom_from_string("UTF8_STRING");
7573
7574 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7575 for (i = 0; i < num_screens; i++) {
7576 root = screens[i].root;
7577 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
7578 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
7579 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
7580
7581 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
7582 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7583 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7584 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
7585 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
7586 netwmname, utf8_string, 8, strlen("LG3D"), "LG3D");
7587 }
7588 }
7589
7590 int
7591 main(int argc, char *argv[])
7592 {
7593 struct swm_region *r, *rr;
7594 struct ws_win *winfocus = NULL;
7595 struct timeval tv;
7596 union arg a;
7597 char conf[PATH_MAX], *cfile = NULL;
7598 struct stat sb;
7599 XEvent e;
7600 int xfd, i, num_screens;
7601 fd_set rd;
7602 struct sigaction sact;
7603
7604 start_argv = argv;
7605 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7606 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
7607 !XSupportsLocale())
7608 warnx("no locale support");
7609
7610 if (!X_HAVE_UTF8_STRING)
7611 warnx("no UTF-8 support");
7612
7613 if (!(display = XOpenDisplay(0)))
7614 errx(1, "can not open display");
7615
7616 if (!(conn = XGetXCBConnection(display)))
7617 errx(1, "can not get XCB connection");
7618
7619 if (active_wm())
7620 errx(1, "other wm running");
7621
7622 /* handle some signals */
7623 bzero(&sact, sizeof(sact));
7624 sigemptyset(&sact.sa_mask);
7625 sact.sa_flags = 0;
7626 sact.sa_handler = sighdlr;
7627 sigaction(SIGINT, &sact, NULL);
7628 sigaction(SIGQUIT, &sact, NULL);
7629 sigaction(SIGTERM, &sact, NULL);
7630 sigaction(SIGHUP, &sact, NULL);
7631
7632 sact.sa_handler = sighdlr;
7633 sact.sa_flags = SA_NOCLDSTOP;
7634 sigaction(SIGCHLD, &sact, NULL);
7635
7636 astate = get_atom_from_string("WM_STATE");
7637 aprot = get_atom_from_string("WM_PROTOCOLS");
7638 adelete = get_atom_from_string("WM_DELETE_WINDOW");
7639 takefocus = get_atom_from_string("WM_TAKE_FOCUS");
7640 a_wmname = get_atom_from_string("WM_NAME");
7641 a_netwmname = get_atom_from_string("_NET_WM_NAME");
7642 a_utf8_string = get_atom_from_string("UTF8_STRING");
7643 a_string = get_atom_from_string("STRING");
7644 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
7645
7646 /* look for local and global conf file */
7647 pwd = getpwuid(getuid());
7648 if (pwd == NULL)
7649 errx(1, "invalid user: %d", getuid());
7650
7651 setup_globals();
7652 setup_screens();
7653 setup_keys();
7654 setup_quirks();
7655 setup_spawn();
7656
7657 /* load config */
7658 for (i = 0; ; i++) {
7659 conf[0] = '\0';
7660 switch (i) {
7661 case 0:
7662 /* ~ */
7663 snprintf(conf, sizeof conf, "%s/.%s",
7664 pwd->pw_dir, SWM_CONF_FILE);
7665 break;
7666 case 1:
7667 /* global */
7668 snprintf(conf, sizeof conf, "/etc/%s",
7669 SWM_CONF_FILE);
7670 break;
7671 case 2:
7672 /* ~ compat */
7673 snprintf(conf, sizeof conf, "%s/.%s",
7674 pwd->pw_dir, SWM_CONF_FILE_OLD);
7675 break;
7676 case 3:
7677 /* global compat */
7678 snprintf(conf, sizeof conf, "/etc/%s",
7679 SWM_CONF_FILE_OLD);
7680 break;
7681 default:
7682 goto noconfig;
7683 }
7684
7685 if (strlen(conf) && stat(conf, &sb) != -1)
7686 if (S_ISREG(sb.st_mode)) {
7687 cfile = conf;
7688 break;
7689 }
7690 }
7691 noconfig:
7692
7693 /* load conf (if any) */
7694 if (cfile)
7695 conf_load(cfile, SWM_CONF_DEFAULT);
7696
7697 setup_ewmh();
7698 /* set some values to work around bad programs */
7699 workaround();
7700 /* grab existing windows (before we build the bars) */
7701 grab_windows();
7702
7703 if (getenv("SWM_STARTED") == NULL)
7704 setenv("SWM_STARTED", "YES", 1);
7705
7706 /* setup all bars */
7707 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7708 for (i = 0; i < num_screens; i++)
7709 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7710 if (winfocus == NULL)
7711 winfocus = TAILQ_FIRST(&r->ws->winlist);
7712 bar_setup(r);
7713 }
7714
7715 unfocus_all();
7716
7717 grabkeys();
7718 stack();
7719 if (focus_mode == SWM_FOCUS_DEFAULT)
7720 drain_enter_notify();
7721
7722 xfd = xcb_get_file_descriptor(conn);
7723 while (running) {
7724 while (XPending(display)) {
7725 XNextEvent(display, &e);
7726 if (running == 0)
7727 goto done;
7728 if (e.type < LASTEvent) {
7729 DNPRINTF(SWM_D_EVENTQ ,"XEvent: handled: %s, "
7730 "window: 0x%lx, type: %s (%d), %d remaining"
7731 "\n", YESNO(handler[e.type]),
7732 e.xany.window, geteventname(&e),
7733 e.type, QLength(display));
7734
7735 if (handler[e.type])
7736 handler[e.type](&e);
7737 } else {
7738 DNPRINTF(SWM_D_EVENTQ, "XRandr Event: window: "
7739 "0x%lx, type: %s (%d)\n", e.xany.window,
7740 xrandr_geteventname(&e), e.type);
7741
7742 switch (e.type - xrandr_eventbase) {
7743 case XCB_RANDR_SCREEN_CHANGE_NOTIFY:
7744 screenchange(&e);
7745 break;
7746 default:
7747 break;
7748 }
7749 }
7750 }
7751
7752 /* if we are being restarted go focus on first window */
7753 if (winfocus) {
7754 rr = winfocus->ws->r;
7755 if (rr == NULL) {
7756 /* not a visible window */
7757 winfocus = NULL;
7758 continue;
7759 }
7760 /* move pointer to first screen if multi screen */
7761 if (num_screens > 1 || outputs > 1)
7762 xcb_warp_pointer(conn, XCB_WINDOW_NONE,
7763 rr->s[0].root, 0, 0, 0, 0, X(rr),
7764 Y(rr) + (bar_enabled ? bar_height : 0));
7765
7766 a.id = SWM_ARG_ID_FOCUSCUR;
7767 focus(rr, &a);
7768 winfocus = NULL;
7769 continue;
7770 }
7771
7772 FD_ZERO(&rd);
7773 FD_SET(xfd, &rd);
7774 tv.tv_sec = 1;
7775 tv.tv_usec = 0;
7776 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7777 if (errno != EINTR)
7778 DNPRINTF(SWM_D_MISC, "select failed");
7779 if (restart_wm == 1)
7780 restart(NULL, NULL);
7781 if (search_resp == 1)
7782 search_do_resp();
7783 if (running == 0)
7784 goto done;
7785 if (bar_alarm) {
7786 bar_alarm = 0;
7787 bar_update();
7788 }
7789 }
7790 done:
7791 teardown_ewmh();
7792 bar_extra_stop();
7793
7794 for (i = 0; i < num_screens; ++i)
7795 if (screens[i].bar_gc != NULL)
7796 XFreeGC(display, screens[i].bar_gc);
7797 XFreeFontSet(display, bar_fs);
7798 xcb_disconnect(conn);
7799 XCloseDisplay(display);
7800
7801 return (0);
7802 }