]> code.delx.au - spectrwm/blob - spectrwm.c
Add new reorder stack action.
[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-2013 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 * Copyright (c) 2012-2013 David Hill <dhill@mindcry.org>
12 *
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 */
25 /*
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
28 *
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
38 *
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
45 *
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
48 *
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
56 */
57
58 /* kernel includes */
59 #include <sys/types.h>
60 #include <sys/time.h>
61 #include <sys/stat.h>
62 #include <sys/wait.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
67 #include "tree.h"
68 #elif defined(__OpenBSD__)
69 #include <sys/tree.h>
70 #elif defined(__FreeBSD__)
71 #include <sys/tree.h>
72 #else
73 #include "tree.h"
74 #endif
75
76 /* /usr/includes */
77 #include <ctype.h>
78 #include <err.h>
79 #include <errno.h>
80 #include <fcntl.h>
81 #include <locale.h>
82 #include <paths.h>
83 #include <pwd.h>
84 #include <regex.h>
85 #include <signal.h>
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <string.h>
89 #include <time.h>
90 #include <unistd.h>
91 #include <util.h>
92 #include <X11/cursorfont.h>
93 #include <X11/extensions/Xrandr.h>
94 #include <X11/Xcursor/Xcursor.h>
95 #include <X11/Xft/Xft.h>
96 #include <X11/Xlib-xcb.h>
97 #include <xcb/xcb_atom.h>
98 #include <xcb/xcb_aux.h>
99 #include <xcb/xcb_event.h>
100 #include <xcb/xcb_icccm.h>
101 #include <xcb/xcb_keysyms.h>
102 #include <xcb/xtest.h>
103 #include <xcb/randr.h>
104
105 /* local includes */
106 #include "version.h"
107 #ifdef __OSX__
108 #include <osx.h>
109 #endif
110
111 #ifdef SPECTRWM_BUILDSTR
112 static const char *buildstr = SPECTRWM_BUILDSTR;
113 #else
114 static const char *buildstr = SPECTRWM_VERSION;
115 #endif
116
117 #if !defined(__CYGWIN__) /* cygwin chokes on xrandr stuff */
118 # if RANDR_MAJOR < 1
119 # error XRandR versions less than 1.0 are not supported
120 #endif
121
122 # if RANDR_MAJOR >= 1
123 # if RANDR_MINOR >= 2
124 # define SWM_XRR_HAS_CRTC
125 # endif
126 # endif
127 #endif /* __CYGWIN__ */
128
129 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
130 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
132 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
133 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
134 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
135 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
136 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
137 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
138 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
139 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
140 #define xcb_icccm_get_wm_class xcb_get_wm_class
141 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
142 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
143 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
144 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
145 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
146 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
147 #define xcb_icccm_get_wm_name xcb_get_wm_name
148 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
149 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
150 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
151 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
152 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
153 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
154 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
155 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
156 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
157 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
158 #endif
159
160 /*#define SWM_DEBUG*/
161 #ifdef SWM_DEBUG
162 #define DPRINTF(x...) do { \
163 if (swm_debug) \
164 fprintf(stderr, x); \
165 } while (0)
166 #define DNPRINTF(n,x...) do { \
167 if (swm_debug & n) { \
168 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
169 fprintf(stderr, x); \
170 } \
171 } while (0)
172 #define SWM_D_MISC 0x0001
173 #define SWM_D_EVENT 0x0002
174 #define SWM_D_WS 0x0004
175 #define SWM_D_FOCUS 0x0008
176 #define SWM_D_MOVE 0x0010
177 #define SWM_D_STACK 0x0020
178 #define SWM_D_MOUSE 0x0040
179 #define SWM_D_PROP 0x0080
180 #define SWM_D_CLASS 0x0100
181 #define SWM_D_KEY 0x0200
182 #define SWM_D_QUIRK 0x0400
183 #define SWM_D_SPAWN 0x0800
184 #define SWM_D_EVENTQ 0x1000
185 #define SWM_D_CONF 0x2000
186 #define SWM_D_BAR 0x4000
187 #define SWM_D_INIT 0x8000
188
189 uint32_t swm_debug = 0
190 | SWM_D_MISC
191 | SWM_D_EVENT
192 | SWM_D_WS
193 | SWM_D_FOCUS
194 | SWM_D_MOVE
195 | SWM_D_STACK
196 | SWM_D_MOUSE
197 | SWM_D_PROP
198 | SWM_D_CLASS
199 | SWM_D_KEY
200 | SWM_D_QUIRK
201 | SWM_D_SPAWN
202 | SWM_D_EVENTQ
203 | SWM_D_CONF
204 | SWM_D_BAR
205 | SWM_D_INIT
206 ;
207 #else
208 #define DPRINTF(x...)
209 #define DNPRINTF(n,x...)
210 #endif
211
212 #define SWM_EWMH_ACTION_COUNT_MAX (8)
213 #define EWMH_F_FULLSCREEN (0x001)
214 #define EWMH_F_ABOVE (0x002)
215 #define EWMH_F_HIDDEN (0x004)
216 #define EWMH_F_MAXIMIZED_VERT (0x008)
217 #define EWMH_F_MAXIMIZED_HORZ (0x010)
218 #define EWMH_F_SKIP_PAGER (0x020)
219 #define EWMH_F_SKIP_TASKBAR (0x040)
220 #define SWM_F_MANUAL (0x080)
221
222 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
223
224 /* convert 8-bit to 16-bit */
225 #define RGB_8_TO_16(col) (((col) << 8) + (col))
226
227 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
228 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
229 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
230 xrc.blue = RGB_8_TO_16((px) & 0xff); \
231 xrc.alpha = 0xffff;
232
233 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
234 #define MODKEY XCB_MOD_MASK_1
235 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
236 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
237 XCB_EVENT_MASK_BUTTON_RELEASE)
238 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
239 #define SWM_PROPLEN (16)
240 #define SWM_FUNCNAME_LEN (32)
241 #define SWM_KEYS_LEN (255)
242 #define SWM_QUIRK_LEN (64)
243 #define X(r) ((r)->g.x)
244 #define Y(r) ((r)->g.y)
245 #define WIDTH(r) ((r)->g.w)
246 #define HEIGHT(r) ((r)->g.h)
247 #define BORDER(w) ((w)->bordered ? border_width : 0)
248 #define MAX_X(r) ((r)->g.x + (r)->g.w)
249 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
250 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
251 #define SH_MIN_W(w) ((w)->sh.min_width)
252 #define SH_MIN_H(w) ((w)->sh.min_height)
253 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
254 #define SH_MAX_W(w) ((w)->sh.max_width)
255 #define SH_MAX_H(w) ((w)->sh.max_height)
256 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
257 #define SH_INC_W(w) ((w)->sh.width_inc)
258 #define SH_INC_H(w) ((w)->sh.height_inc)
259 #define SWM_MAX_FONT_STEPS (3)
260 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
261 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
262 #define YESNO(x) ((x) ? "yes" : "no")
263 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
264 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
265 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
266 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
267 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
268 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
269 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
270 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
271 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
272 MAXIMIZED(w))
273
274 /* Constrain Window flags */
275 #define SWM_CW_RESIZABLE (0x01)
276 #define SWM_CW_SOFTBOUNDARY (0x02)
277 #define SWM_CW_HARDBOUNDARY (0x04)
278 #define SWM_CW_RIGHT (0x10)
279 #define SWM_CW_LEFT (0x20)
280 #define SWM_CW_BOTTOM (0x40)
281 #define SWM_CW_TOP (0x80)
282 #define SWM_CW_ALLSIDES (0xf0)
283
284 #define SWM_FOCUS_DEFAULT (0)
285 #define SWM_FOCUS_FOLLOW (1)
286 #define SWM_FOCUS_MANUAL (2)
287
288 #define SWM_CK_NONE (0)
289 #define SWM_CK_ALL (0xf)
290 #define SWM_CK_FOCUS (0x1)
291 #define SWM_CK_POINTER (0x2)
292 #define SWM_CK_FALLBACK (0x4)
293 #define SWM_CK_REGION (0x8)
294
295 #define SWM_G_ALL (0xf)
296 #define SWM_G_SIZE (0x1)
297 #define SWM_G_POS (0x2)
298
299 #define SWM_CONF_DEFAULT (0)
300 #define SWM_CONF_KEYMAPPING (1)
301
302 #ifndef SWM_LIB
303 #define SWM_LIB "/usr/local/lib/libswmhack.so"
304 #endif
305
306 char **start_argv;
307 xcb_atom_t a_state;
308 xcb_atom_t a_prot;
309 xcb_atom_t a_delete;
310 xcb_atom_t a_net_wm_check;
311 xcb_atom_t a_net_supported;
312 xcb_atom_t a_takefocus;
313 xcb_atom_t a_utf8_string;
314 xcb_atom_t a_swm_ws;
315 volatile sig_atomic_t running = 1;
316 volatile sig_atomic_t restart_wm = 0;
317 xcb_timestamp_t last_event_time = 0;
318 int outputs = 0;
319 int other_wm;
320 int xrandr_support;
321 int xrandr_eventbase;
322 unsigned int numlockmask = 0;
323
324 Display *display;
325 xcb_connection_t *conn;
326 xcb_key_symbols_t *syms;
327
328 int boundary_width = 50;
329 int cycle_empty = 0;
330 int cycle_visible = 0;
331 int term_width = 0;
332 int font_adjusted = 0;
333 unsigned int mod_key = MODKEY;
334
335 /* dmenu search */
336 struct swm_region *search_r;
337 int select_list_pipe[2];
338 int select_resp_pipe[2];
339 pid_t searchpid;
340 volatile sig_atomic_t search_resp;
341 int search_resp_action;
342
343 struct search_window {
344 TAILQ_ENTRY(search_window) entry;
345 int idx;
346 struct ws_win *win;
347 xcb_gcontext_t gc;
348 xcb_window_t indicator;
349 };
350 TAILQ_HEAD(search_winlist, search_window);
351 struct search_winlist search_wl;
352
353 /* search actions */
354 enum {
355 SWM_SEARCH_NONE,
356 SWM_SEARCH_UNICONIFY,
357 SWM_SEARCH_NAME_WORKSPACE,
358 SWM_SEARCH_SEARCH_WORKSPACE,
359 SWM_SEARCH_SEARCH_WINDOW
360 };
361
362 #define SWM_STACK_TOP (0)
363 #define SWM_STACK_BOTTOM (1)
364 #define SWM_STACK_ABOVE (2)
365 #define SWM_STACK_BELOW (3)
366
367 /* dialog windows */
368 double dialog_ratio = 0.6;
369 /* status bar */
370 #define SWM_BAR_MAX (256)
371 #define SWM_BAR_JUSTIFY_LEFT (0)
372 #define SWM_BAR_JUSTIFY_CENTER (1)
373 #define SWM_BAR_JUSTIFY_RIGHT (2)
374 #define SWM_BAR_OFFSET (4)
375 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
376 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
377 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
378 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
379 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
380
381 #ifdef X_HAVE_UTF8_STRING
382 #define DRAWSTRING(x...) Xutf8DrawString(x)
383 #else
384 #define DRAWSTRING(x...) XmbDrawString(x)
385 #endif
386
387 char *bar_argv[] = { NULL, NULL };
388 int bar_pipe[2];
389 char bar_ext[SWM_BAR_MAX];
390 char bar_ext_buf[SWM_BAR_MAX];
391 char bar_vertext[SWM_BAR_MAX];
392 int bar_version = 0;
393 int bar_enabled = 1;
394 int bar_border_width = 1;
395 int bar_at_bottom = 0;
396 int bar_extra = 0;
397 int bar_verbose = 1;
398 int bar_height = 0;
399 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
400 char *bar_format = NULL;
401 int stack_enabled = 1;
402 int clock_enabled = 1;
403 int iconic_enabled = 0;
404 int urgent_enabled = 0;
405 char *clock_format = NULL;
406 int window_class_enabled = 0;
407 int window_instance_enabled = 0;
408 int window_name_enabled = 0;
409 int focus_mode = SWM_FOCUS_DEFAULT;
410 int focus_close = SWM_STACK_BELOW;
411 int focus_close_wrap = 1;
412 int focus_default = SWM_STACK_TOP;
413 int spawn_position = SWM_STACK_TOP;
414 int disable_border = 0;
415 int border_width = 1;
416 int region_padding = 0;
417 int tile_gap = 0;
418 int java_workaround = 1;
419 int verbose_layout = 0;
420 time_t time_started;
421 pid_t bar_pid;
422 XFontSet bar_fs;
423 XFontSetExtents *bar_fs_extents;
424 XftFont *bar_font;
425 int bar_font_legacy = 1;
426 char *bar_fonts;
427 XftColor bar_font_color;
428 struct passwd *pwd;
429 char *startup_exception;
430 unsigned int nr_exceptions = 0;
431
432 /* layout manager data */
433 struct swm_geometry {
434 int x;
435 int y;
436 int w;
437 int h;
438 };
439
440 struct swm_screen;
441 struct workspace;
442
443 struct swm_bar {
444 xcb_window_t id;
445 xcb_pixmap_t buffer;
446 struct swm_geometry g;
447 };
448
449 /* virtual "screens" */
450 struct swm_region {
451 TAILQ_ENTRY(swm_region) entry;
452 xcb_window_t id;
453 struct swm_geometry g;
454 struct workspace *ws; /* current workspace on this region */
455 struct workspace *ws_prior; /* prior workspace on this region */
456 struct swm_screen *s; /* screen idx */
457 struct swm_bar *bar;
458 };
459 TAILQ_HEAD(swm_region_list, swm_region);
460
461 struct ws_win {
462 TAILQ_ENTRY(ws_win) entry;
463 TAILQ_ENTRY(ws_win) stack_entry;
464 xcb_window_t id;
465 xcb_window_t transient;
466 struct ws_win *focus_child; /* focus on child transient */
467 struct swm_geometry g; /* current geometry */
468 struct swm_geometry g_float; /* region coordinates */
469 int g_floatvalid; /* g_float geometry validity */
470 int mapped;
471 int bordered;
472 uint32_t ewmh_flags;
473 int font_size_boundary[SWM_MAX_FONT_STEPS];
474 int font_steps;
475 int last_inc;
476 int can_delete;
477 int take_focus;
478 int java;
479 unsigned long quirks;
480 struct workspace *ws; /* always valid */
481 struct swm_screen *s; /* always valid, never changes */
482 xcb_size_hints_t sh;
483 xcb_icccm_get_wm_class_reply_t ch;
484 xcb_icccm_wm_hints_t hints;
485 };
486 TAILQ_HEAD(ws_win_list, ws_win);
487 TAILQ_HEAD(ws_win_stack, ws_win);
488
489 /* pid goo */
490 struct pid_e {
491 TAILQ_ENTRY(pid_e) entry;
492 pid_t pid;
493 int ws;
494 };
495 TAILQ_HEAD(pid_list, pid_e);
496 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
497
498 /* layout handlers */
499 void stack(void);
500 void vertical_config(struct workspace *, int);
501 void vertical_stack(struct workspace *, struct swm_geometry *);
502 void horizontal_config(struct workspace *, int);
503 void horizontal_stack(struct workspace *, struct swm_geometry *);
504 void max_stack(struct workspace *, struct swm_geometry *);
505 void plain_stacker(struct workspace *);
506 void fancy_stacker(struct workspace *);
507
508 struct layout {
509 void (*l_stack)(struct workspace *, struct swm_geometry *);
510 void (*l_config)(struct workspace *, int);
511 uint32_t flags;
512 #define SWM_L_FOCUSPREV (1<<0)
513 #define SWM_L_MAPONFOCUS (1<<1)
514 void (*l_string)(struct workspace *);
515 } layouts[] = {
516 /* stack, configure */
517 { vertical_stack, vertical_config, 0, plain_stacker },
518 { horizontal_stack, horizontal_config, 0, plain_stacker },
519 { max_stack, NULL,
520 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
521 { NULL, NULL, 0, NULL },
522 };
523
524 /* position of max_stack mode in the layouts array, index into layouts! */
525 #define SWM_V_STACK (0)
526 #define SWM_H_STACK (1)
527 #define SWM_MAX_STACK (2)
528
529 #define SWM_H_SLICE (32)
530 #define SWM_V_SLICE (32)
531
532 /* define work spaces */
533 struct workspace {
534 int idx; /* workspace index */
535 char *name; /* workspace name */
536 int always_raise; /* raise windows on focus */
537 int bar_enabled; /* bar visibility */
538 struct layout *cur_layout; /* current layout handlers */
539 struct ws_win *focus; /* may be NULL */
540 struct ws_win *focus_prev; /* may be NULL */
541 struct ws_win *focus_pending; /* may be NULL */
542 struct swm_region *r; /* may be NULL */
543 struct swm_region *old_r; /* may be NULL */
544 struct ws_win_list winlist; /* list of windows in ws */
545 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
546 struct ws_win_stack stack; /* stacking order */
547 int state; /* mapping state */
548 char stacker[10]; /* display stacker and layout */
549
550 /* stacker state */
551 struct {
552 int horizontal_msize;
553 int horizontal_mwin;
554 int horizontal_stacks;
555 int horizontal_flip;
556 int vertical_msize;
557 int vertical_mwin;
558 int vertical_stacks;
559 int vertical_flip;
560 } l_state;
561 };
562
563 enum {
564 SWM_WS_STATE_HIDDEN,
565 SWM_WS_STATE_MAPPING,
566 SWM_WS_STATE_MAPPED,
567 };
568
569 enum {
570 SWM_S_COLOR_BAR,
571 SWM_S_COLOR_BAR_BORDER,
572 SWM_S_COLOR_BAR_BORDER_UNFOCUS,
573 SWM_S_COLOR_BAR_FONT,
574 SWM_S_COLOR_FOCUS,
575 SWM_S_COLOR_UNFOCUS,
576 SWM_S_COLOR_MAX
577 };
578
579 /* physical screen mapping */
580 #define SWM_WS_MAX (22) /* hard limit */
581 int workspace_limit = 10; /* soft limit */
582
583 struct swm_screen {
584 int idx; /* screen index */
585 struct swm_region_list rl; /* list of regions on this screen */
586 struct swm_region_list orl; /* list of old regions */
587 xcb_window_t root;
588 struct workspace ws[SWM_WS_MAX];
589 struct swm_region *r_focus;
590
591 /* colors */
592 struct {
593 uint32_t pixel;
594 char *name;
595 } c[SWM_S_COLOR_MAX];
596
597 xcb_gcontext_t bar_gc;
598 GC bar_gc_legacy;
599 };
600 struct swm_screen *screens;
601
602 /* args to functions */
603 union arg {
604 int id;
605 #define SWM_ARG_ID_FOCUSNEXT (0)
606 #define SWM_ARG_ID_FOCUSPREV (1)
607 #define SWM_ARG_ID_FOCUSMAIN (2)
608 #define SWM_ARG_ID_FOCUSURGENT (3)
609 #define SWM_ARG_ID_SWAPNEXT (10)
610 #define SWM_ARG_ID_SWAPPREV (11)
611 #define SWM_ARG_ID_SWAPMAIN (12)
612 #define SWM_ARG_ID_MOVELAST (13)
613 #define SWM_ARG_ID_MASTERSHRINK (20)
614 #define SWM_ARG_ID_MASTERGROW (21)
615 #define SWM_ARG_ID_MASTERADD (22)
616 #define SWM_ARG_ID_MASTERDEL (23)
617 #define SWM_ARG_ID_FLIPLAYOUT (24)
618 #define SWM_ARG_ID_STACKRESET (30)
619 #define SWM_ARG_ID_STACKINIT (31)
620 #define SWM_ARG_ID_CYCLEWS_UP (40)
621 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
622 #define SWM_ARG_ID_CYCLERG_UP (42)
623 #define SWM_ARG_ID_CYCLERG_DOWN (43)
624 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
625 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
626 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
627 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
628 #define SWM_ARG_ID_STACKINC (50)
629 #define SWM_ARG_ID_STACKDEC (51)
630 #define SWM_ARG_ID_SS_ALL (60)
631 #define SWM_ARG_ID_SS_WINDOW (61)
632 #define SWM_ARG_ID_DONTCENTER (70)
633 #define SWM_ARG_ID_CENTER (71)
634 #define SWM_ARG_ID_KILLWINDOW (80)
635 #define SWM_ARG_ID_DELETEWINDOW (81)
636 #define SWM_ARG_ID_WIDTHGROW (90)
637 #define SWM_ARG_ID_WIDTHSHRINK (91)
638 #define SWM_ARG_ID_HEIGHTGROW (92)
639 #define SWM_ARG_ID_HEIGHTSHRINK (93)
640 #define SWM_ARG_ID_MOVEUP (100)
641 #define SWM_ARG_ID_MOVEDOWN (101)
642 #define SWM_ARG_ID_MOVELEFT (102)
643 #define SWM_ARG_ID_MOVERIGHT (103)
644 #define SWM_ARG_ID_RAISE (105)
645 #define SWM_ARG_ID_LOWER (106)
646 #define SWM_ARG_ID_BAR_TOGGLE (110)
647 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
648 char **argv;
649 };
650
651 /* quirks */
652 struct quirk {
653 TAILQ_ENTRY(quirk) entry;
654 char *class; /* WM_CLASS:class */
655 char *instance; /* WM_CLASS:instance */
656 char *name; /* WM_NAME */
657 regex_t regex_class;
658 regex_t regex_instance;
659 regex_t regex_name;
660 unsigned long quirk;
661 #define SWM_Q_FLOAT (1<<0) /* float this window */
662 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
663 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
664 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
665 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
666 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
667 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
668 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
669 };
670 TAILQ_HEAD(quirk_list, quirk);
671 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
672
673 /*
674 * Supported EWMH hints should be added to
675 * both the enum and the ewmh array
676 */
677 enum {
678 _NET_ACTIVE_WINDOW,
679 _NET_CLIENT_LIST,
680 _NET_CLOSE_WINDOW,
681 _NET_CURRENT_DESKTOP,
682 _NET_DESKTOP_GEOMETRY,
683 _NET_DESKTOP_NAMES,
684 _NET_DESKTOP_VIEWPORT,
685 _NET_MOVERESIZE_WINDOW,
686 _NET_NUMBER_OF_DESKTOPS,
687 _NET_RESTACK_WINDOW,
688 _NET_WM_ACTION_ABOVE,
689 _NET_WM_ACTION_CLOSE,
690 _NET_WM_ACTION_FULLSCREEN,
691 _NET_WM_ACTION_MOVE,
692 _NET_WM_ACTION_RESIZE,
693 _NET_WM_ALLOWED_ACTIONS,
694 _NET_WM_DESKTOP,
695 _NET_WM_FULL_PLACEMENT,
696 _NET_WM_NAME,
697 _NET_WM_STATE,
698 _NET_WM_STATE_ABOVE,
699 _NET_WM_STATE_FULLSCREEN,
700 _NET_WM_STATE_HIDDEN,
701 _NET_WM_STATE_MAXIMIZED_VERT,
702 _NET_WM_STATE_MAXIMIZED_HORZ,
703 _NET_WM_STATE_SKIP_PAGER,
704 _NET_WM_STATE_SKIP_TASKBAR,
705 _NET_WM_WINDOW_TYPE,
706 _NET_WM_WINDOW_TYPE_DIALOG,
707 _NET_WM_WINDOW_TYPE_DOCK,
708 _NET_WM_WINDOW_TYPE_NORMAL,
709 _NET_WM_WINDOW_TYPE_SPLASH,
710 _NET_WM_WINDOW_TYPE_TOOLBAR,
711 _NET_WM_WINDOW_TYPE_UTILITY,
712 _SWM_WM_STATE_MANUAL,
713 SWM_EWMH_HINT_MAX
714 };
715
716 struct ewmh_hint {
717 char *name;
718 xcb_atom_t atom;
719 } ewmh[SWM_EWMH_HINT_MAX] = {
720 /* must be in same order as in the enum */
721 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
722 {"_NET_CLIENT_LIST", XCB_ATOM_NONE},
723 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
724 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE},
725 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE},
726 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE},
727 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE},
728 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
729 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE},
730 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE},
731 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
732 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
733 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
734 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
735 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
736 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
737 {"_NET_WM_DESKTOP", XCB_ATOM_NONE},
738 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE},
739 {"_NET_WM_NAME", XCB_ATOM_NONE},
740 {"_NET_WM_STATE", XCB_ATOM_NONE},
741 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
742 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
743 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
744 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
745 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
746 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
747 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
748 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
749 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
750 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
751 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
752 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
753 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
754 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
755 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
756 };
757
758 /* EWMH source type */
759 enum {
760 EWMH_SOURCE_TYPE_NONE = 0,
761 EWMH_SOURCE_TYPE_NORMAL = 1,
762 EWMH_SOURCE_TYPE_OTHER = 2,
763 };
764
765 /* Cursors */
766 enum {
767 XC_FLEUR,
768 XC_LEFT_PTR,
769 XC_BOTTOM_LEFT_CORNER,
770 XC_BOTTOM_RIGHT_CORNER,
771 XC_SIZING,
772 XC_TOP_LEFT_CORNER,
773 XC_TOP_RIGHT_CORNER,
774 XC_MAX
775 };
776
777 struct cursors {
778 char *name; /* Name used by Xcursor .*/
779 uint8_t cf_char; /* cursorfont index. */
780 xcb_cursor_t cid;
781 } cursors[XC_MAX] = {
782 {"fleur", XC_fleur, XCB_CURSOR_NONE},
783 {"left_ptr", XC_left_ptr, XCB_CURSOR_NONE},
784 {"bottom_left_corner", XC_bottom_left_corner, XCB_CURSOR_NONE},
785 {"bottom_right_corner", XC_bottom_right_corner, XCB_CURSOR_NONE},
786 {"sizing", XC_sizing, XCB_CURSOR_NONE},
787 {"top_left_corner", XC_top_left_corner, XCB_CURSOR_NONE},
788 {"top_right_corner", XC_top_right_corner, XCB_CURSOR_NONE},
789 };
790
791 #define SWM_SPAWN_OPTIONAL 0x1
792
793 /* spawn */
794 struct spawn_prog {
795 TAILQ_ENTRY(spawn_prog) entry;
796 char *name;
797 int argc;
798 char **argv;
799 int flags;
800 };
801 TAILQ_HEAD(spawn_list, spawn_prog);
802 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
803
804 /* user/key callable function IDs */
805 enum keyfuncid {
806 KF_BAR_TOGGLE,
807 KF_BAR_TOGGLE_WS,
808 KF_BUTTON2,
809 KF_CYCLE_LAYOUT,
810 KF_FLIP_LAYOUT,
811 KF_FLOAT_TOGGLE,
812 KF_FOCUS_MAIN,
813 KF_FOCUS_NEXT,
814 KF_FOCUS_PREV,
815 KF_FOCUS_URGENT,
816 KF_MAXIMIZE_TOGGLE,
817 KF_HEIGHT_GROW,
818 KF_HEIGHT_SHRINK,
819 KF_ICONIFY,
820 KF_MASTER_SHRINK,
821 KF_MASTER_GROW,
822 KF_MASTER_ADD,
823 KF_MASTER_DEL,
824 KF_MOVE_DOWN,
825 KF_MOVE_LEFT,
826 KF_MOVE_RIGHT,
827 KF_MOVE_UP,
828 KF_MVRG_1,
829 KF_MVRG_2,
830 KF_MVRG_3,
831 KF_MVRG_4,
832 KF_MVRG_5,
833 KF_MVRG_6,
834 KF_MVRG_7,
835 KF_MVRG_8,
836 KF_MVRG_9,
837 KF_MVWS_1,
838 KF_MVWS_2,
839 KF_MVWS_3,
840 KF_MVWS_4,
841 KF_MVWS_5,
842 KF_MVWS_6,
843 KF_MVWS_7,
844 KF_MVWS_8,
845 KF_MVWS_9,
846 KF_MVWS_10,
847 KF_MVWS_11,
848 KF_MVWS_12,
849 KF_MVWS_13,
850 KF_MVWS_14,
851 KF_MVWS_15,
852 KF_MVWS_16,
853 KF_MVWS_17,
854 KF_MVWS_18,
855 KF_MVWS_19,
856 KF_MVWS_20,
857 KF_MVWS_21,
858 KF_MVWS_22,
859 KF_NAME_WORKSPACE,
860 KF_QUIT,
861 KF_RAISE_TOGGLE,
862 KF_RESTART,
863 KF_RG_1,
864 KF_RG_2,
865 KF_RG_3,
866 KF_RG_4,
867 KF_RG_5,
868 KF_RG_6,
869 KF_RG_7,
870 KF_RG_8,
871 KF_RG_9,
872 KF_RG_NEXT,
873 KF_RG_PREV,
874 KF_SCREEN_NEXT,
875 KF_SCREEN_PREV,
876 KF_SEARCH_WIN,
877 KF_SEARCH_WORKSPACE,
878 KF_SPAWN_CUSTOM,
879 KF_STACK_INC,
880 KF_STACK_DEC,
881 KF_STACK_RESET,
882 KF_SWAP_MAIN,
883 KF_SWAP_NEXT,
884 KF_SWAP_PREV,
885 KF_UNICONIFY,
886 KF_VERSION,
887 KF_WIDTH_GROW,
888 KF_WIDTH_SHRINK,
889 KF_WIND_DEL,
890 KF_WIND_KILL,
891 KF_WS_1,
892 KF_WS_2,
893 KF_WS_3,
894 KF_WS_4,
895 KF_WS_5,
896 KF_WS_6,
897 KF_WS_7,
898 KF_WS_8,
899 KF_WS_9,
900 KF_WS_10,
901 KF_WS_11,
902 KF_WS_12,
903 KF_WS_13,
904 KF_WS_14,
905 KF_WS_15,
906 KF_WS_16,
907 KF_WS_17,
908 KF_WS_18,
909 KF_WS_19,
910 KF_WS_20,
911 KF_WS_21,
912 KF_WS_22,
913 KF_WS_NEXT,
914 KF_WS_NEXT_ALL,
915 KF_WS_NEXT_MOVE,
916 KF_WS_PREV,
917 KF_WS_PREV_ALL,
918 KF_WS_PREV_MOVE,
919 KF_WS_PRIOR,
920 KF_DUMPWINS, /* MUST BE LAST */
921 KF_INVALID
922 };
923
924 struct key {
925 RB_ENTRY(key) entry;
926 unsigned int mod;
927 KeySym keysym;
928 enum keyfuncid funcid;
929 char *spawn_name;
930 };
931 RB_HEAD(key_tree, key);
932
933 /* function prototypes */
934 void adjust_font(struct ws_win *);
935 char *argsep(char **);
936 void bar_cleanup(struct swm_region *);
937 void bar_extra_setup(void);
938 void bar_extra_stop(void);
939 int bar_extra_update(void);
940 void bar_fmt(const char *, char *, struct swm_region *, size_t);
941 void bar_fmt_expand(char *, size_t);
942 void bar_draw(void);
943 void bar_print(struct swm_region *, const char *);
944 void bar_print_legacy(struct swm_region *, const char *);
945 void bar_replace(char *, char *, struct swm_region *, size_t);
946 void bar_replace_pad(char *, int *, size_t);
947 char *bar_replace_seq(char *, char *, struct swm_region *, size_t *, size_t);
948 void bar_setup(struct swm_region *);
949 void bar_toggle(struct swm_region *, union arg *);
950 void bar_urgent(char *, size_t);
951 void bar_window_class(char *, size_t, struct swm_region *);
952 void bar_window_class_instance(char *, size_t, struct swm_region *);
953 void bar_window_float(char *, size_t, struct swm_region *);
954 void bar_window_instance(char *, size_t, struct swm_region *);
955 void bar_window_name(char *, size_t, struct swm_region *);
956 void bar_window_state(char *, size_t, struct swm_region *);
957 void bar_workspace_name(char *, size_t, struct swm_region *);
958 void buttonpress(xcb_button_press_event_t *);
959 void check_conn(void);
960 void clear_keys(void);
961 int clear_maximized(struct workspace *);
962 void clientmessage(xcb_client_message_event_t *);
963 void client_msg(struct ws_win *, xcb_atom_t, xcb_timestamp_t);
964 int conf_load(const char *, int);
965 void configurenotify(xcb_configure_notify_event_t *);
966 void configurerequest(xcb_configure_request_event_t *);
967 void config_win(struct ws_win *, xcb_configure_request_event_t *);
968 void constrain_window(struct ws_win *, struct swm_geometry *, int *);
969 int count_win(struct workspace *, int);
970 void cursors_cleanup(void);
971 void cursors_load(void);
972 void custom_region(const char *);
973 void cyclerg(struct swm_region *, union arg *);
974 void cyclews(struct swm_region *, union arg *);
975 void cycle_layout(struct swm_region *, union arg *);
976 void destroynotify(xcb_destroy_notify_event_t *);
977 void dumpwins(struct swm_region *, union arg *);
978 int enable_wm(void);
979 void enternotify(xcb_enter_notify_event_t *);
980 void event_drain(uint8_t);
981 void event_error(xcb_generic_error_t *);
982 void event_handle(xcb_generic_event_t *);
983 void ewmh_apply_flags(struct ws_win *, uint32_t);
984 void ewmh_autoquirk(struct ws_win *);
985 void ewmh_get_desktop_names(void);
986 void ewmh_get_wm_state(struct ws_win *);
987 void ewmh_update_actions(struct ws_win *);
988 void ewmh_update_client_list(void);
989 void ewmh_update_current_desktop(void);
990 void ewmh_update_desktop_names(void);
991 void ewmh_update_desktops(void);
992 void ewmh_change_wm_state(struct ws_win *, xcb_atom_t, long);
993 void ewmh_update_wm_state(struct ws_win *);
994 char *expand_tilde(const char *);
995 void expose(xcb_expose_event_t *);
996 void fake_keypress(struct ws_win *, xcb_keysym_t, uint16_t);
997 struct pid_e *find_pid(pid_t);
998 struct ws_win *find_unmanaged_window(xcb_window_t);
999 struct ws_win *find_window(xcb_window_t);
1000 void floating_toggle(struct swm_region *, union arg *);
1001 void focus(struct swm_region *, union arg *);
1002 #ifdef SWM_DEBUG
1003 void focusin(xcb_focus_in_event_t *);
1004 void focusout(xcb_focus_out_event_t *);
1005 #endif
1006 void focus_flush(void);
1007 void focus_region(struct swm_region *);
1008 void focusrg(struct swm_region *, union arg *);
1009 void focus_win(struct ws_win *);
1010 void fontset_init(void);
1011 void free_window(struct ws_win *);
1012 xcb_atom_t get_atom_from_string(const char *);
1013 #ifdef SWM_DEBUG
1014 char *get_atom_name(xcb_atom_t);
1015 #endif
1016 struct ws_win *get_focus_magic(struct ws_win *);
1017 struct ws_win *get_focus_prev(struct ws_win *);
1018 #ifdef SWM_DEBUG
1019 char *get_notify_detail_label(uint8_t);
1020 char *get_notify_mode_label(uint8_t);
1021 #endif
1022 struct ws_win *get_pointer_win(xcb_window_t);
1023 struct ws_win *get_region_focus(struct swm_region *);
1024 int get_region_index(struct swm_region *);
1025 xcb_screen_t *get_screen(int);
1026 int get_screen_count(void);
1027 #ifdef SWM_DEBUG
1028 char *get_stack_mode_name(uint8_t);
1029 #endif
1030 int32_t get_swm_ws(xcb_window_t);
1031 char *get_win_name(xcb_window_t);
1032 uint8_t get_win_state(xcb_window_t);
1033 void get_wm_protocols(struct ws_win *);
1034 int get_ws_idx(xcb_window_t);
1035 void grabbuttons(struct ws_win *);
1036 void grabkeys(void);
1037 void grab_windows(void);
1038 void iconify(struct swm_region *, union arg *);
1039 int isxlfd(char *);
1040 void keypress(xcb_key_press_event_t *);
1041 int key_cmp(struct key *, struct key *);
1042 void key_insert(unsigned int, KeySym, enum keyfuncid, const char *);
1043 struct key *key_lookup(unsigned int, KeySym);
1044 void key_remove(struct key *);
1045 void key_replace(struct key *, unsigned int, KeySym, enum keyfuncid,
1046 const char *);
1047 void kill_bar_extra_atexit(void);
1048 void kill_refs(struct ws_win *);
1049 #ifdef SWM_DEBUG
1050 void leavenotify(xcb_leave_notify_event_t *);
1051 #endif
1052 void load_float_geom(struct ws_win *);
1053 struct ws_win *manage_window(xcb_window_t, int);
1054 void map_window(struct ws_win *);
1055 void mapnotify(xcb_map_notify_event_t *);
1056 void mappingnotify(xcb_mapping_notify_event_t *);
1057 void maprequest(xcb_map_request_event_t *);
1058 void maximize_toggle(struct swm_region *, union arg *);
1059 void motionnotify(xcb_motion_notify_event_t *);
1060 void move(struct ws_win *, union arg *);
1061 void move_step(struct swm_region *, union arg *);
1062 uint32_t name_to_pixel(int, const char *);
1063 void name_workspace(struct swm_region *, union arg *);
1064 void new_region(struct swm_screen *, int, int, int, int);
1065 int parsekeys(const char *, unsigned int, unsigned int *, KeySym *);
1066 int parsequirks(const char *, unsigned long *);
1067 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1068 void pressbutton(struct swm_region *, union arg *);
1069 void priorws(struct swm_region *, union arg *);
1070 #ifdef SWM_DEBUG
1071 void print_win_geom(xcb_window_t);
1072 #endif
1073 void propertynotify(xcb_property_notify_event_t *);
1074 void quirk_free(struct quirk *);
1075 void quirk_insert(const char *, const char *, const char *,unsigned long);
1076 void quirk_remove(struct quirk *);
1077 void quirk_replace(struct quirk *, const char *, const char *, const char *,
1078 unsigned long);
1079 void quit(struct swm_region *, union arg *);
1080 void raise_toggle(struct swm_region *, union arg *);
1081 void raise_window(struct ws_win *);
1082 void region_containment(struct ws_win *, struct swm_region *, int);
1083 struct swm_region *region_under(struct swm_screen *, int, int);
1084 void regionize(struct ws_win *, int, int);
1085 void resize(struct ws_win *, union arg *);
1086 void resize_step(struct swm_region *, union arg *);
1087 void restart(struct swm_region *, union arg *);
1088 struct swm_region *root_to_region(xcb_window_t, int);
1089 void screenchange(xcb_randr_screen_change_notify_event_t *);
1090 void scan_xrandr(int);
1091 void search_do_resp(void);
1092 void search_resp_name_workspace(const char *, size_t);
1093 void search_resp_search_window(const char *);
1094 void search_resp_search_workspace(const char *);
1095 void search_resp_uniconify(const char *, size_t);
1096 void search_win(struct swm_region *, union arg *);
1097 void search_win_cleanup(void);
1098 void search_workspace(struct swm_region *, union arg *);
1099 void send_to_rg(struct swm_region *, union arg *);
1100 void send_to_ws(struct swm_region *, union arg *);
1101 void set_region(struct swm_region *);
1102 int setautorun(const char *, const char *, int);
1103 int setconfbinding(const char *, const char *, int);
1104 int setconfcolor(const char *, const char *, int);
1105 int setconfmodkey(const char *, const char *, int);
1106 int setconfquirk(const char *, const char *, int);
1107 int setconfregion(const char *, const char *, int);
1108 int setconfspawn(const char *, const char *, int);
1109 int setconfvalue(const char *, const char *, int);
1110 void setkeybinding(unsigned int, KeySym, enum keyfuncid, const char *);
1111 int setkeymapping(const char *, const char *, int);
1112 int setlayout(const char *, const char *, int);
1113 void setquirk(const char *, const char *, const char *,unsigned long);
1114 void setscreencolor(const char *, int, int);
1115 void setspawn(const char *, const char *, int);
1116 void setup_ewmh(void);
1117 void setup_globals(void);
1118 void setup_keys(void);
1119 void setup_quirks(void);
1120 void setup_screens(void);
1121 void setup_spawn(void);
1122 void set_child_transient(struct ws_win *, xcb_window_t *);
1123 void set_win_state(struct ws_win *, uint8_t);
1124 void shutdown_cleanup(void);
1125 void sighdlr(int);
1126 void socket_setnonblock(int);
1127 void sort_windows(struct ws_win_list *);
1128 void spawn(int, union arg *, int);
1129 void spawn_custom(struct swm_region *, union arg *, const char *);
1130 int spawn_expand(struct swm_region *, union arg *, const char *, char ***);
1131 void spawn_insert(const char *, const char *, int);
1132 struct spawn_prog *spawn_find(const char *);
1133 void spawn_remove(struct spawn_prog *);
1134 void spawn_replace(struct spawn_prog *, const char *, const char *, int);
1135 void spawn_select(struct swm_region *, union arg *, const char *, int *);
1136 void stack_config(struct swm_region *, union arg *);
1137 void stack_master(struct workspace *, struct swm_geometry *, int, int);
1138 void store_float_geom(struct ws_win *);
1139 char *strdupsafe(const char *);
1140 void swapwin(struct swm_region *, union arg *);
1141 void switchws(struct swm_region *, union arg *);
1142 void teardown_ewmh(void);
1143 void unescape_selector(char *);
1144 void unfocus_win(struct ws_win *);
1145 void uniconify(struct swm_region *, union arg *);
1146 void unmanage_window(struct ws_win *);
1147 void unmapnotify(xcb_unmap_notify_event_t *);
1148 void unmap_all(void);
1149 void unmap_window(struct ws_win *);
1150 void updatenumlockmask(void);
1151 void update_floater(struct ws_win *);
1152 void update_modkey(unsigned int);
1153 void update_win_stacking(struct ws_win *);
1154 void update_window(struct ws_win *);
1155 void update_wm_state(struct ws_win *win);
1156 void validate_spawns(void);
1157 int validate_win(struct ws_win *);
1158 int validate_ws(struct workspace *);
1159 void version(struct swm_region *, union arg *);
1160 void win_to_ws(struct ws_win *, int, int);
1161 pid_t window_get_pid(xcb_window_t);
1162 void wkill(struct swm_region *, union arg *);
1163 void update_ws_stack(struct workspace *);
1164 void xft_init(struct swm_region *);
1165 void _add_startup_exception(const char *, va_list);
1166 void add_startup_exception(const char *, ...);
1167
1168 RB_PROTOTYPE(key_tree, key, entry, key_cmp);
1169 RB_GENERATE(key_tree, key, entry, key_cmp);
1170 struct key_tree keys;
1171
1172 void
1173 cursors_load(void)
1174 {
1175 xcb_font_t cf = XCB_NONE;
1176 int i;
1177
1178 for (i = 0; i < LENGTH(cursors); ++i) {
1179 /* try to load Xcursor first. */
1180 cursors[i].cid = XcursorLibraryLoadCursor(display,
1181 cursors[i].name);
1182
1183 /* fallback to cursorfont. */
1184 if (cursors[i].cid == XCB_CURSOR_NONE) {
1185 if (cf == XCB_NONE) {
1186 cf = xcb_generate_id(conn);
1187 xcb_open_font(conn, cf, strlen("cursor"),
1188 "cursor");
1189 }
1190
1191 cursors[i].cid = xcb_generate_id(conn);
1192 xcb_create_glyph_cursor(conn, cursors[i].cid, cf, cf,
1193 cursors[i].cf_char, cursors[i].cf_char + 1, 0, 0, 0,
1194 0xffff, 0xffff, 0xffff);
1195
1196 }
1197 }
1198
1199 if (cf != XCB_NONE)
1200 xcb_close_font(conn, cf);
1201 }
1202
1203 void
1204 cursors_cleanup(void)
1205 {
1206 int i;
1207 for (i = 0; i < LENGTH(cursors); ++i)
1208 xcb_free_cursor(conn, cursors[i].cid);
1209 }
1210
1211 char *
1212 expand_tilde(const char *s)
1213 {
1214 struct passwd *ppwd;
1215 int i;
1216 long max;
1217 char *user;
1218 const char *sc = s;
1219 char *result;
1220
1221 if (s == NULL)
1222 errx(1, "expand_tilde: NULL string.");
1223
1224 if (s[0] != '~') {
1225 result = strdup(sc);
1226 goto out;
1227 }
1228
1229 ++s;
1230
1231 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
1232 errx(1, "expand_tilde: sysconf");
1233
1234 if ((user = calloc(1, max + 1)) == NULL)
1235 errx(1, "expand_tilde: calloc");
1236
1237 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
1238 user[i] = s[i];
1239 user[i] = '\0';
1240 s = &s[i];
1241
1242 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
1243 free(user);
1244
1245 if (ppwd == NULL)
1246 result = strdup(sc);
1247 else
1248 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
1249 result = NULL;
1250 out:
1251 if (result == NULL)
1252 errx(1, "expand_tilde: failed to allocate memory.");
1253
1254 return result;
1255 }
1256
1257 int
1258 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
1259 {
1260 unsigned int tmpr, tmpg, tmpb;
1261
1262 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
1263 return (-1);
1264
1265 *rr = RGB_8_TO_16(tmpr);
1266 *gg = RGB_8_TO_16(tmpg);
1267 *bb = RGB_8_TO_16(tmpb);
1268
1269 return (0);
1270 }
1271
1272 xcb_screen_t *
1273 get_screen(int screen)
1274 {
1275 const xcb_setup_t *r;
1276 xcb_screen_iterator_t iter;
1277
1278 if ((r = xcb_get_setup(conn)) == NULL) {
1279 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
1280 check_conn();
1281 }
1282
1283 iter = xcb_setup_roots_iterator(r);
1284 for (; iter.rem; --screen, xcb_screen_next(&iter))
1285 if (screen == 0)
1286 return (iter.data);
1287
1288 return (NULL);
1289 }
1290
1291 int
1292 get_screen_count(void)
1293 {
1294 const xcb_setup_t *r;
1295
1296 if ((r = xcb_get_setup(conn)) == NULL) {
1297 DNPRINTF(SWM_D_MISC, "get_screen_count: xcb_get_setup\n");
1298 check_conn();
1299 }
1300
1301 return xcb_setup_roots_length(r);
1302 }
1303
1304 int
1305 get_region_index(struct swm_region *r)
1306 {
1307 struct swm_region *rr;
1308 int ridx = 0;
1309
1310 if (r == NULL)
1311 return -1;
1312
1313 TAILQ_FOREACH(rr, &r->s->rl, entry) {
1314 if (rr == r)
1315 break;
1316 ++ridx;
1317 }
1318
1319 if (rr == NULL)
1320 return -1;
1321
1322 return ridx;
1323 }
1324
1325 void
1326 focus_flush(void)
1327 {
1328 if (focus_mode == SWM_FOCUS_DEFAULT)
1329 event_drain(XCB_ENTER_NOTIFY);
1330 else
1331 xcb_flush(conn);
1332 }
1333
1334 xcb_atom_t
1335 get_atom_from_string(const char *str)
1336 {
1337 xcb_intern_atom_cookie_t c;
1338 xcb_intern_atom_reply_t *r;
1339 xcb_atom_t atom;
1340
1341 c = xcb_intern_atom(conn, 0, strlen(str), str);
1342 r = xcb_intern_atom_reply(conn, c, NULL);
1343 if (r) {
1344 atom = r->atom;
1345 free(r);
1346
1347 return (atom);
1348 }
1349
1350 return (XCB_ATOM_NONE);
1351 }
1352
1353 void
1354 get_wm_protocols(struct ws_win *win) {
1355 int i;
1356 xcb_icccm_get_wm_protocols_reply_t wpr;
1357
1358 if (xcb_icccm_get_wm_protocols_reply(conn,
1359 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
1360 &wpr, NULL)) {
1361 for (i = 0; i < (int)wpr.atoms_len; i++) {
1362 if (wpr.atoms[i] == a_takefocus)
1363 win->take_focus = 1;
1364 if (wpr.atoms[i] == a_delete)
1365 win->can_delete = 1;
1366 }
1367 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
1368 }
1369 }
1370
1371 void
1372 setup_ewmh(void)
1373 {
1374 xcb_window_t root, win;
1375 int i, j, num_screens;
1376
1377 for (i = 0; i < LENGTH(ewmh); i++)
1378 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
1379
1380 num_screens = get_screen_count();
1381 for (i = 0; i < num_screens; i++) {
1382 root = screens[i].root;
1383
1384 /* Set up _NET_SUPPORTING_WM_CHECK. */
1385 win = xcb_generate_id(conn);
1386 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
1387 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
1388 XCB_COPY_FROM_PARENT, 0, NULL);
1389
1390 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
1391 a_net_wm_check, XCB_ATOM_WINDOW, 32, 1, &win);
1392 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
1393 a_net_wm_check, XCB_ATOM_WINDOW, 32, 1, &win);
1394
1395 /*
1396 * Impersonate LG3D non-reparenting WM, written by Sun, to
1397 * workaround a Java GUI rendering issue.
1398 */
1399 if (java_workaround)
1400 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
1401 ewmh[_NET_WM_NAME].atom, a_utf8_string,
1402 8, strlen("LG3D"), "LG3D");
1403 else
1404 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
1405 ewmh[_NET_WM_NAME].atom, a_utf8_string,
1406 8, strlen("spectrwm"), "spectrwm");
1407
1408 /* Report supported atoms */
1409 xcb_delete_property(conn, root, a_net_supported);
1410 for (j = 0; j < LENGTH(ewmh); j++)
1411 xcb_change_property(conn, XCB_PROP_MODE_APPEND, root,
1412 a_net_supported, XCB_ATOM_ATOM, 32, 1,
1413 &ewmh[j].atom);
1414
1415 }
1416
1417 ewmh_update_desktops();
1418 ewmh_get_desktop_names();
1419 }
1420
1421 void
1422 teardown_ewmh(void)
1423 {
1424 int i, num_screens;
1425 xcb_window_t id;
1426 xcb_get_property_cookie_t pc;
1427 xcb_get_property_reply_t *pr;
1428
1429 num_screens = get_screen_count();
1430
1431 for (i = 0; i < num_screens; i++) {
1432 /* Get the support check window and destroy it */
1433 pc = xcb_get_property(conn, 0, screens[i].root, a_net_wm_check,
1434 XCB_ATOM_WINDOW, 0, 1);
1435 pr = xcb_get_property_reply(conn, pc, NULL);
1436 if (pr == NULL)
1437 continue;
1438 if (pr->format == a_net_wm_check) {
1439 id = *((xcb_window_t *)xcb_get_property_value(pr));
1440
1441 xcb_destroy_window(conn, id);
1442 xcb_delete_property(conn, screens[i].root,
1443 a_net_wm_check);
1444 xcb_delete_property(conn, screens[i].root,
1445 a_net_supported);
1446 }
1447 free(pr);
1448 }
1449 }
1450
1451 void
1452 ewmh_autoquirk(struct ws_win *win)
1453 {
1454 xcb_get_property_reply_t *r;
1455 xcb_get_property_cookie_t c;
1456 xcb_atom_t *type;
1457 int i, n;
1458
1459 c = xcb_get_property(conn, 0, win->id,
1460 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
1461 r = xcb_get_property_reply(conn, c, NULL);
1462 if (r == NULL)
1463 return;
1464
1465 type = xcb_get_property_value(r);
1466 n = xcb_get_property_value_length(r) / sizeof(xcb_atom_t);
1467
1468 for (i = 0; i < n; i++) {
1469 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
1470 break;
1471 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
1472 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
1473 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
1474 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
1475 break;
1476 }
1477 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
1478 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
1479 win->quirks = SWM_Q_FLOAT;
1480 break;
1481 }
1482 }
1483 free(r);
1484 }
1485
1486 void
1487 ewmh_update_actions(struct ws_win *win)
1488 {
1489 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1490 int n = 0;
1491
1492 if (win == NULL)
1493 return;
1494
1495 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1496
1497 if (ABOVE(win)) {
1498 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1499 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1500 actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
1501 }
1502
1503 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1504 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1505 }
1506
1507 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1508 #define _NET_WM_STATE_ADD 1 /* add/set property */
1509 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1510
1511 void
1512 ewmh_change_wm_state(struct ws_win *win, xcb_atom_t state, long action)
1513 {
1514 uint32_t flag = 0;
1515 uint32_t new_flags;
1516 #ifdef SWM_DEBUG
1517 char *name;
1518
1519 name = get_atom_name(state);
1520 DNPRINTF(SWM_D_PROP, "ewmh_change_wm_state: win %#x, state: %s, "
1521 "action: %ld\n", WINID(win), name, action);
1522 free(name);
1523 #endif
1524 if (win == NULL)
1525 goto out;
1526
1527 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1528 flag = EWMH_F_FULLSCREEN;
1529 else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1530 flag = EWMH_F_ABOVE;
1531 else if (state == ewmh[_NET_WM_STATE_HIDDEN].atom)
1532 flag = EWMH_F_HIDDEN;
1533 else if (state == ewmh[_NET_WM_STATE_MAXIMIZED_VERT].atom ||
1534 state == ewmh[_NET_WM_STATE_MAXIMIZED_HORZ].atom)
1535 flag = EWMH_F_MAXIMIZED;
1536 else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1537 flag = SWM_F_MANUAL;
1538 else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1539 flag = EWMH_F_SKIP_PAGER;
1540 else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1541 flag = EWMH_F_SKIP_TASKBAR;
1542
1543 /* Disallow unfloating transients. */
1544 if (TRANS(win) && flag == EWMH_F_ABOVE)
1545 goto out;
1546
1547 new_flags = win->ewmh_flags;
1548
1549 switch (action) {
1550 case _NET_WM_STATE_REMOVE:
1551 new_flags &= ~flag;
1552 break;
1553 case _NET_WM_STATE_ADD:
1554 new_flags |= flag;
1555 break;
1556 case _NET_WM_STATE_TOGGLE:
1557 new_flags ^= flag;
1558 break;
1559 }
1560
1561 ewmh_apply_flags(win, new_flags);
1562
1563 out:
1564 DNPRINTF(SWM_D_PROP, "ewmh_change_wm_state: done.\n");
1565 }
1566
1567 void
1568 ewmh_apply_flags(struct ws_win *win, uint32_t pending)
1569 {
1570 struct workspace *ws;
1571 uint32_t changed;
1572
1573 changed = win->ewmh_flags ^ pending;
1574 if (changed == 0)
1575 return;
1576
1577 DNPRINTF(SWM_D_PROP, "ewmh_apply_flags: pending: %d\n", pending);
1578
1579 win->ewmh_flags = pending;
1580 ws = win->ws;
1581
1582 if (changed & EWMH_F_HIDDEN) {
1583 if (ICONIC(win)) {
1584 if (focus_mode != SWM_FOCUS_FOLLOW)
1585 ws->focus_pending = get_focus_prev(win);
1586
1587 unfocus_win(win);
1588 unmap_window(win);
1589 } else {
1590 /* Reload floating geometry in case region changed. */
1591 if (FLOATING(win))
1592 load_float_geom(win);
1593
1594 /* The window is no longer iconic, prepare focus. */
1595 if (focus_mode != SWM_FOCUS_FOLLOW)
1596 ws->focus_pending = get_focus_magic(win);
1597 raise_window(win);
1598 }
1599 }
1600
1601 if (changed & EWMH_F_ABOVE) {
1602 if (ws->cur_layout != &layouts[SWM_MAX_STACK]) {
1603 if (ABOVE(win))
1604 load_float_geom(win);
1605 else if (!MAXIMIZED(win))
1606 store_float_geom(win);
1607
1608 win->ewmh_flags &= ~EWMH_F_MAXIMIZED;
1609 changed &= ~EWMH_F_MAXIMIZED;
1610 raise_window(win);
1611 } else {
1612 /* Revert. */
1613 win->ewmh_flags ^= EWMH_F_ABOVE & pending;
1614 }
1615 }
1616
1617 if (changed & EWMH_F_MAXIMIZED) {
1618 /* VERT and/or HORZ changed. */
1619 if (ABOVE(win)) {
1620 if (!MAXIMIZED(win))
1621 load_float_geom(win);
1622 else
1623 store_float_geom(win);
1624 }
1625
1626 if (MAXIMIZED(win)) {
1627 if (focus_mode != SWM_FOCUS_FOLLOW &&
1628 ws->cur_layout != &layouts[SWM_MAX_STACK]) {
1629 if (WS_FOCUSED(ws))
1630 focus_win(win);
1631 else
1632 ws->focus_pending = win;
1633 }
1634 }
1635 raise_window(win);
1636 }
1637
1638 if (changed & EWMH_F_FULLSCREEN) {
1639 if (FULLSCREEN(win)) {
1640 if (focus_mode != SWM_FOCUS_FOLLOW) {
1641 if (WS_FOCUSED(ws))
1642 focus_win(win);
1643 else
1644 ws->focus_pending = win;
1645 }
1646 } else {
1647 load_float_geom(win);
1648 }
1649
1650 win->ewmh_flags &= ~EWMH_F_MAXIMIZED;
1651 raise_window(win);
1652 }
1653
1654 DNPRINTF(SWM_D_PROP, "ewmh_apply_flags: done.\n");
1655 }
1656
1657 void
1658 ewmh_update_wm_state(struct ws_win *win) {
1659 xcb_atom_t vals[SWM_EWMH_ACTION_COUNT_MAX];
1660 int n = 0;
1661
1662 if (ICONIC(win))
1663 vals[n++] = ewmh[_NET_WM_STATE_HIDDEN].atom;
1664 if (FULLSCREEN(win))
1665 vals[n++] = ewmh[_NET_WM_STATE_FULLSCREEN].atom;
1666 if (MAXIMIZED_VERT(win))
1667 vals[n++] = ewmh[_NET_WM_STATE_MAXIMIZED_VERT].atom;
1668 if (MAXIMIZED_HORZ(win))
1669 vals[n++] = ewmh[_NET_WM_STATE_MAXIMIZED_HORZ].atom;
1670 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1671 vals[n++] = ewmh[_NET_WM_STATE_SKIP_PAGER].atom;
1672 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1673 vals[n++] = ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom;
1674 if (win->ewmh_flags & EWMH_F_ABOVE)
1675 vals[n++] = ewmh[_NET_WM_STATE_ABOVE].atom;
1676 if (win->ewmh_flags & SWM_F_MANUAL)
1677 vals[n++] = ewmh[_SWM_WM_STATE_MANUAL].atom;
1678
1679 if (n > 0)
1680 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1681 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, n, vals);
1682 else
1683 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1684 }
1685
1686 void
1687 ewmh_get_wm_state(struct ws_win *win)
1688 {
1689 xcb_atom_t *states;
1690 xcb_get_property_cookie_t c;
1691 xcb_get_property_reply_t *r;
1692 int i, n;
1693
1694 if (win == NULL)
1695 return;
1696
1697 win->ewmh_flags = 0;
1698
1699 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1700 XCB_ATOM_ATOM, 0, UINT32_MAX);
1701 r = xcb_get_property_reply(conn, c, NULL);
1702 if (r == NULL)
1703 return;
1704
1705 states = xcb_get_property_value(r);
1706 n = xcb_get_property_value_length(r) / sizeof(xcb_atom_t);
1707
1708 for (i = 0; i < n; i++)
1709 ewmh_change_wm_state(win, states[i], _NET_WM_STATE_ADD);
1710
1711 free(r);
1712 }
1713
1714 /* events */
1715 #ifdef SWM_DEBUG
1716 void
1717 dumpwins(struct swm_region *r, union arg *args)
1718 {
1719 struct ws_win *w;
1720 uint32_t state;
1721 xcb_get_window_attributes_cookie_t c;
1722 xcb_get_window_attributes_reply_t *wa;
1723
1724 /* suppress unused warning since var is needed */
1725 (void)args;
1726
1727 if (r->ws == NULL) {
1728 DPRINTF("dumpwins: invalid workspace\n");
1729 return;
1730 }
1731
1732 DPRINTF("=== managed window list ws %02d ===\n", r->ws->idx);
1733 TAILQ_FOREACH(w, &r->ws->winlist, entry) {
1734 state = get_win_state(w->id);
1735 c = xcb_get_window_attributes(conn, w->id);
1736 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1737 if (wa) {
1738 DPRINTF("win %#x, map_state: %d, state: %u, "
1739 "transient: %#x\n", w->id, wa->map_state,
1740 state, w->transient);
1741 free(wa);
1742 } else
1743 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1744 w->id);
1745 }
1746
1747 DPRINTF("=== stacking order (top down) === \n");
1748 TAILQ_FOREACH(w, &r->ws->stack, stack_entry) {
1749 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
1750 "iconic: %s\n", w->id, YESNO(FULLSCREEN(w)),
1751 YESNO(MAXIMIZED(w)), YESNO(ABOVE(w)), YESNO(ICONIC(w)));
1752 }
1753
1754 DPRINTF("===== unmanaged window list =====\n");
1755 TAILQ_FOREACH(w, &r->ws->unmanagedlist, entry) {
1756 state = get_win_state(w->id);
1757 c = xcb_get_window_attributes(conn, w->id);
1758 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1759 if (wa) {
1760 DPRINTF("win %#x, map_state: %d, state: %u, "
1761 "transient: %#x\n", w->id, wa->map_state,
1762 state, w->transient);
1763 free(wa);
1764 } else
1765 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1766 w->id);
1767 }
1768
1769 DPRINTF("=================================\n");
1770 }
1771 #else
1772 void
1773 dumpwins(struct swm_region *r, union arg *s)
1774 {
1775 (void)r;
1776 (void)s;
1777 }
1778 #endif /* SWM_DEBUG */
1779
1780 void
1781 sighdlr(int sig)
1782 {
1783 int saved_errno, status;
1784 pid_t pid;
1785
1786 saved_errno = errno;
1787
1788 switch (sig) {
1789 case SIGCHLD:
1790 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1791 if (pid == -1) {
1792 if (errno == EINTR)
1793 continue;
1794 #ifdef SWM_DEBUG
1795 if (errno != ECHILD)
1796 warn("sighdlr: waitpid");
1797 #endif /* SWM_DEBUG */
1798 break;
1799 }
1800 if (pid == searchpid)
1801 search_resp = 1;
1802
1803 #ifdef SWM_DEBUG
1804 if (WIFEXITED(status)) {
1805 if (WEXITSTATUS(status) != 0)
1806 warnx("sighdlr: child exit status: %d",
1807 WEXITSTATUS(status));
1808 } else
1809 warnx("sighdlr: child is terminated "
1810 "abnormally");
1811 #endif /* SWM_DEBUG */
1812 }
1813 break;
1814
1815 case SIGHUP:
1816 restart_wm = 1;
1817 break;
1818 case SIGINT:
1819 case SIGTERM:
1820 case SIGQUIT:
1821 running = 0;
1822 break;
1823 }
1824
1825 errno = saved_errno;
1826 }
1827
1828 struct pid_e *
1829 find_pid(pid_t pid)
1830 {
1831 struct pid_e *p = NULL;
1832
1833 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1834
1835 if (pid == 0)
1836 return (NULL);
1837
1838 TAILQ_FOREACH(p, &pidlist, entry) {
1839 if (p->pid == pid)
1840 return (p);
1841 }
1842
1843 return (NULL);
1844 }
1845
1846 uint32_t
1847 name_to_pixel(int sidx, const char *colorname)
1848 {
1849 uint32_t result = 0;
1850 char cname[32] = "#";
1851 xcb_screen_t *screen;
1852 xcb_colormap_t cmap;
1853 xcb_alloc_color_reply_t *cr;
1854 xcb_alloc_named_color_reply_t *nr;
1855 uint16_t rr, gg, bb;
1856
1857 screen = get_screen(sidx);
1858 cmap = screen->default_colormap;
1859
1860 /* color is in format rgb://rr/gg/bb */
1861 if (strncmp(colorname, "rgb:", 4) == 0) {
1862 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1863 warnx("could not parse rgb %s", colorname);
1864 else {
1865 cr = xcb_alloc_color_reply(conn,
1866 xcb_alloc_color(conn, cmap, rr, gg, bb),
1867 NULL);
1868 if (cr) {
1869 result = cr->pixel;
1870 free(cr);
1871 } else
1872 warnx("color '%s' not found", colorname);
1873 }
1874 } else {
1875 nr = xcb_alloc_named_color_reply(conn,
1876 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1877 colorname), NULL);
1878 if (nr == NULL) {
1879 strlcat(cname, colorname + 2, sizeof cname - 1);
1880 nr = xcb_alloc_named_color_reply(conn,
1881 xcb_alloc_named_color(conn, cmap, strlen(cname),
1882 cname), NULL);
1883 }
1884 if (nr) {
1885 result = nr->pixel;
1886 free(nr);
1887 } else
1888 warnx("color '%s' not found", colorname);
1889 }
1890
1891 return (result);
1892 }
1893
1894 void
1895 setscreencolor(const char *val, int i, int c)
1896 {
1897 int num_screens;
1898
1899 num_screens = get_screen_count();
1900 if (i > 0 && i <= num_screens) {
1901 screens[i - 1].c[c].pixel = name_to_pixel(i - 1, val);
1902 free(screens[i - 1].c[c].name);
1903 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1904 err(1, "strdup");
1905 } else if (i == -1) {
1906 for (i = 0; i < num_screens; i++) {
1907 screens[i].c[c].pixel = name_to_pixel(0, val);
1908 free(screens[i].c[c].name);
1909 if ((screens[i].c[c].name = strdup(val)) == NULL)
1910 err(1, "strdup");
1911 }
1912 } else
1913 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1914 i, num_screens);
1915 }
1916
1917 void
1918 fancy_stacker(struct workspace *ws)
1919 {
1920 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1921 if (ws->cur_layout->l_stack == vertical_stack)
1922 snprintf(ws->stacker, sizeof ws->stacker,
1923 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1924 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1925 else if (ws->cur_layout->l_stack == horizontal_stack)
1926 snprintf(ws->stacker, sizeof ws->stacker,
1927 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1928 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1929 }
1930
1931 void
1932 plain_stacker(struct workspace *ws)
1933 {
1934 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1935 if (ws->cur_layout->l_stack == vertical_stack)
1936 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1937 sizeof ws->stacker);
1938 else if (ws->cur_layout->l_stack == horizontal_stack)
1939 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1940 sizeof ws->stacker);
1941 }
1942
1943 void
1944 custom_region(const char *val)
1945 {
1946 unsigned int x, y, w, h;
1947 int sidx, num_screens;
1948 xcb_screen_t *screen;
1949
1950 num_screens = get_screen_count();
1951 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1952 errx(1, "invalid custom region, "
1953 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1954 if (sidx < 1 || sidx > num_screens)
1955 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1956 sidx, num_screens);
1957 sidx--;
1958
1959 if ((screen = get_screen(sidx)) == NULL)
1960 errx(1, "ERROR: can't get screen %d.", sidx);
1961
1962 if (w < 1 || h < 1)
1963 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1964
1965 if (x > screen->width_in_pixels ||
1966 y > screen->height_in_pixels ||
1967 w + x > screen->width_in_pixels ||
1968 h + y > screen->height_in_pixels) {
1969 warnx("ignoring region %ux%u+%u+%u - not within screen "
1970 "boundaries (%ux%u)", w, h, x, y,
1971 screen->width_in_pixels, screen->height_in_pixels);
1972 return;
1973 }
1974
1975 new_region(&screens[sidx], x, y, w, h);
1976 }
1977
1978 void
1979 socket_setnonblock(int fd)
1980 {
1981 int flags;
1982
1983 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1984 err(1, "fcntl F_GETFL");
1985 flags |= O_NONBLOCK;
1986 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1987 err(1, "fcntl F_SETFL");
1988 }
1989
1990 void
1991 bar_print_legacy(struct swm_region *r, const char *s)
1992 {
1993 xcb_rectangle_t rect;
1994 uint32_t gcv[1];
1995 XGCValues gcvd;
1996 int x = 0;
1997 size_t len;
1998 XRectangle ibox, lbox;
1999 GC draw;
2000
2001 len = strlen(s);
2002 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
2003
2004 switch (bar_justify) {
2005 case SWM_BAR_JUSTIFY_LEFT:
2006 x = SWM_BAR_OFFSET;
2007 break;
2008 case SWM_BAR_JUSTIFY_CENTER:
2009 x = (WIDTH(r) - lbox.width) / 2;
2010 break;
2011 case SWM_BAR_JUSTIFY_RIGHT:
2012 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
2013 break;
2014 }
2015
2016 if (x < SWM_BAR_OFFSET)
2017 x = SWM_BAR_OFFSET;
2018
2019 rect.x = 0;
2020 rect.y = 0;
2021 rect.width = WIDTH(r->bar);
2022 rect.height = HEIGHT(r->bar);
2023
2024 /* clear back buffer */
2025 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2026 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
2027 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
2028
2029 /* draw back buffer */
2030 gcvd.graphics_exposures = 0;
2031 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
2032 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
2033 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
2034 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
2035 lbox.y, s, len);
2036 XFreeGC(display, draw);
2037
2038 /* blt */
2039 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
2040 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
2041 }
2042
2043 void
2044 bar_print(struct swm_region *r, const char *s)
2045 {
2046 size_t len;
2047 xcb_rectangle_t rect;
2048 uint32_t gcv[1];
2049 int32_t x = 0;
2050 XGlyphInfo info;
2051 XftDraw *draw;
2052
2053 len = strlen(s);
2054
2055 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
2056
2057 switch (bar_justify) {
2058 case SWM_BAR_JUSTIFY_LEFT:
2059 x = SWM_BAR_OFFSET;
2060 break;
2061 case SWM_BAR_JUSTIFY_CENTER:
2062 x = (WIDTH(r) - info.width) / 2;
2063 break;
2064 case SWM_BAR_JUSTIFY_RIGHT:
2065 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
2066 break;
2067 }
2068
2069 if (x < SWM_BAR_OFFSET)
2070 x = SWM_BAR_OFFSET;
2071
2072 rect.x = 0;
2073 rect.y = 0;
2074 rect.width = WIDTH(r->bar);
2075 rect.height = HEIGHT(r->bar);
2076
2077 /* clear back buffer */
2078 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2079 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
2080 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
2081
2082 /* draw back buffer */
2083 draw = XftDrawCreate(display, r->bar->buffer,
2084 DefaultVisual(display, r->s->idx),
2085 DefaultColormap(display, r->s->idx));
2086
2087 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
2088 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
2089 (FcChar8 *)s, len);
2090
2091 XftDrawDestroy(draw);
2092
2093 /* blt */
2094 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
2095 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
2096 }
2097
2098 void
2099 bar_extra_stop(void)
2100 {
2101 if (bar_pipe[0]) {
2102 close(bar_pipe[0]);
2103 bzero(bar_pipe, sizeof bar_pipe);
2104 }
2105 if (bar_pid) {
2106 kill(bar_pid, SIGTERM);
2107 bar_pid = 0;
2108 }
2109 strlcpy(bar_ext, "", sizeof bar_ext);
2110 bar_extra = 0;
2111 }
2112
2113 void
2114 bar_window_class(char *s, size_t sz, struct swm_region *r)
2115 {
2116 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2117 return;
2118 if (r->ws->focus->ch.class_name != NULL)
2119 strlcat(s, r->ws->focus->ch.class_name, sz);
2120 }
2121
2122 void
2123 bar_window_instance(char *s, size_t sz, struct swm_region *r)
2124 {
2125 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2126 return;
2127 if (r->ws->focus->ch.instance_name != NULL)
2128 strlcat(s, r->ws->focus->ch.instance_name, sz);
2129 }
2130
2131 void
2132 bar_window_class_instance(char *s, size_t sz, struct swm_region *r)
2133 {
2134 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2135 return;
2136
2137 bar_window_class(s, sz, r);
2138 strlcat(s, ":", sz);
2139 bar_window_instance(s, sz, r);
2140 }
2141
2142 void
2143 bar_window_state(char *s, size_t sz, struct swm_region *r)
2144 {
2145 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
2146 return;
2147 if (MAXIMIZED(r->ws->focus))
2148 strlcat(s, "(m)", sz);
2149 else if (ABOVE(r->ws->focus))
2150 strlcat(s, "(f)", sz);
2151 }
2152
2153 void
2154 bar_window_name(char *s, size_t sz, struct swm_region *r)
2155 {
2156 char *title;
2157
2158 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2159 return;
2160
2161 title = get_win_name(r->ws->focus->id);
2162 strlcat(s, title, sz);
2163 free(title);
2164 }
2165
2166 void
2167 bar_urgent(char *s, size_t sz)
2168 {
2169 struct ws_win *win;
2170 int i, j, num_screens;
2171 int urgent[SWM_WS_MAX];
2172 char b[8];
2173 xcb_get_property_cookie_t c;
2174 xcb_icccm_wm_hints_t hints;
2175
2176 for (i = 0; i < workspace_limit; i++)
2177 urgent[i] = 0;
2178
2179 num_screens = get_screen_count();
2180 for (i = 0; i < num_screens; i++)
2181 for (j = 0; j < workspace_limit; j++)
2182 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
2183 c = xcb_icccm_get_wm_hints(conn, win->id);
2184 if (xcb_icccm_get_wm_hints_reply(conn, c,
2185 &hints, NULL) == 0)
2186 continue;
2187 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
2188 urgent[j] = 1;
2189 }
2190
2191 for (i = 0; i < workspace_limit; i++) {
2192 if (urgent[i])
2193 snprintf(b, sizeof b, "%d ", i + 1);
2194 else
2195 snprintf(b, sizeof b, "- ");
2196 strlcat(s, b, sz);
2197 }
2198 }
2199
2200 void
2201 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
2202 {
2203 if (r == NULL || r->ws == NULL)
2204 return;
2205 if (r->ws->name != NULL)
2206 strlcat(s, r->ws->name, sz);
2207 }
2208
2209 /* build the default bar format according to the defined enabled options */
2210 void
2211 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
2212 {
2213 struct ws_win *w;
2214
2215 /* if format provided, just copy the buffers */
2216 if (bar_format != NULL) {
2217 strlcpy(fmtnew, fmtexp, sz);
2218 return;
2219 }
2220
2221 /* reset the output buffer */
2222 *fmtnew = '\0';
2223
2224 strlcat(fmtnew, "+N:+I ", sz);
2225 if (stack_enabled)
2226 strlcat(fmtnew, "+S", sz);
2227 strlcat(fmtnew, " ", sz);
2228
2229 /* only show the workspace name if there's actually one */
2230 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
2231 strlcat(fmtnew, "<+D>", sz);
2232
2233 /* If enabled, only show the iconic count if there are iconic wins. */
2234 if (iconic_enabled && r != NULL && r->ws != NULL)
2235 TAILQ_FOREACH(w, &r->ws->winlist, entry)
2236 if (ICONIC(w)) {
2237 strlcat(fmtnew, "{+M}", sz);
2238 break;
2239 }
2240
2241 strlcat(fmtnew, "+3<", sz);
2242
2243 if (clock_enabled) {
2244 strlcat(fmtnew, fmtexp, sz);
2245 strlcat(fmtnew, "+4<", sz);
2246 }
2247
2248 /* bar_urgent already adds the space before the last asterisk */
2249 if (urgent_enabled)
2250 strlcat(fmtnew, "* +U*+4<", sz);
2251
2252 if (window_class_enabled) {
2253 strlcat(fmtnew, "+C", sz);
2254 if (!window_instance_enabled)
2255 strlcat(fmtnew, "+4<", sz);
2256 }
2257
2258 /* checks needed by the colon and floating strlcat(3) calls below */
2259 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
2260 if (window_instance_enabled) {
2261 if (window_class_enabled)
2262 strlcat(fmtnew, ":", sz);
2263 strlcat(fmtnew, "+T+4<", sz);
2264 }
2265 if (window_name_enabled) {
2266 if (ABOVE(r->ws->focus) || MAXIMIZED(r->ws->focus))
2267 strlcat(fmtnew, "+F ", sz);
2268 strlcat(fmtnew, "+64W ", sz);
2269 }
2270 }
2271
2272 /* finally add the action script output and the version */
2273 strlcat(fmtnew, "+4<+A+4<+V", sz);
2274 }
2275
2276 void
2277 bar_replace_pad(char *tmp, int *limit, size_t sz)
2278 {
2279 /* special case; no limit given, pad one space, instead */
2280 if (*limit == (int)sz - 1)
2281 *limit = 1;
2282 snprintf(tmp, sz, "%*s", *limit, " ");
2283 }
2284
2285 /* replaces the bar format character sequences (like in tmux(1)) */
2286 char *
2287 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
2288 size_t sz)
2289 {
2290 struct ws_win *w;
2291 char *ptr;
2292 char tmp[SWM_BAR_MAX];
2293 int limit, size, count;
2294 size_t len;
2295
2296 /* reset strlcat(3) buffer */
2297 *tmp = '\0';
2298
2299 /* get number, if any */
2300 fmt++;
2301 size = 0;
2302 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
2303 limit = sizeof tmp - 1;
2304 if (limit <= 0 || limit >= (int)sizeof tmp)
2305 limit = sizeof tmp - 1;
2306
2307 /* there is nothing to replace (ie EOL) */
2308 fmt += size;
2309 if (*fmt == '\0')
2310 return (fmt);
2311
2312 switch (*fmt) {
2313 case '<':
2314 bar_replace_pad(tmp, &limit, sizeof tmp);
2315 break;
2316 case 'A':
2317 snprintf(tmp, sizeof tmp, "%s", bar_ext);
2318 break;
2319 case 'C':
2320 bar_window_class(tmp, sizeof tmp, r);
2321 break;
2322 case 'D':
2323 bar_workspace_name(tmp, sizeof tmp, r);
2324 break;
2325 case 'F':
2326 bar_window_state(tmp, sizeof tmp, r);
2327 break;
2328 case 'I':
2329 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
2330 break;
2331 case 'M':
2332 count = 0;
2333 TAILQ_FOREACH(w, &r->ws->winlist, entry)
2334 if (ICONIC(w))
2335 ++count;
2336
2337 snprintf(tmp, sizeof tmp, "%d", count);
2338 break;
2339 case 'N':
2340 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
2341 break;
2342 case 'P':
2343 bar_window_class_instance(tmp, sizeof tmp, r);
2344 break;
2345 case 'S':
2346 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
2347 break;
2348 case 'T':
2349 bar_window_instance(tmp, sizeof tmp, r);
2350 break;
2351 case 'U':
2352 bar_urgent(tmp, sizeof tmp);
2353 break;
2354 case 'V':
2355 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
2356 break;
2357 case 'W':
2358 bar_window_name(tmp, sizeof tmp, r);
2359 break;
2360 default:
2361 /* unknown character sequence; copy as-is */
2362 snprintf(tmp, sizeof tmp, "+%c", *fmt);
2363 break;
2364 }
2365
2366 len = strlen(tmp);
2367 ptr = tmp;
2368 if ((int)len < limit)
2369 limit = len;
2370 while (limit-- > 0) {
2371 if (*offrep >= sz - 1)
2372 break;
2373 fmtrep[(*offrep)++] = *ptr++;
2374 }
2375
2376 fmt++;
2377 return (fmt);
2378 }
2379
2380 void
2381 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
2382 {
2383 size_t off;
2384
2385 off = 0;
2386 while (*fmt != '\0') {
2387 if (*fmt != '+') {
2388 /* skip ordinary characters */
2389 if (off >= sz - 1)
2390 break;
2391 fmtrep[off++] = *fmt++;
2392 continue;
2393 }
2394
2395 /* character sequence found; replace it */
2396 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
2397 if (off >= sz - 1)
2398 break;
2399 }
2400
2401 fmtrep[off] = '\0';
2402 }
2403
2404 void
2405 bar_fmt_expand(char *fmtexp, size_t sz)
2406 {
2407 char *fmt = NULL;
2408 size_t len;
2409 struct tm tm;
2410 time_t tmt;
2411
2412 /* start by grabbing the current time and date */
2413 time(&tmt);
2414 localtime_r(&tmt, &tm);
2415
2416 /* figure out what to expand */
2417 if (bar_format != NULL)
2418 fmt = bar_format;
2419 else if (bar_format == NULL && clock_enabled)
2420 fmt = clock_format;
2421 /* if nothing to expand bail out */
2422 if (fmt == NULL) {
2423 *fmtexp = '\0';
2424 return;
2425 }
2426
2427 /* copy as-is, just in case the format shouldn't be expanded below */
2428 strlcpy(fmtexp, fmt, sz);
2429 /* finally pass the string through strftime(3) */
2430 #ifndef SWM_DENY_CLOCK_FORMAT
2431 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
2432 warnx("format too long");
2433 fmtexp[len] = '\0';
2434 #endif
2435 }
2436
2437 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2438 void
2439 bar_draw(void)
2440 {
2441 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
2442 char fmtrep[SWM_BAR_MAX];
2443 int i, num_screens;
2444 struct swm_region *r;
2445
2446 /* expand the format by first passing it through strftime(3) */
2447 bar_fmt_expand(fmtexp, sizeof fmtexp);
2448
2449 num_screens = get_screen_count();
2450 for (i = 0; i < num_screens; i++) {
2451 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2452 if (r->bar == NULL)
2453 continue;
2454
2455 if (bar_enabled && r->ws->bar_enabled)
2456 xcb_map_window(conn, r->bar->id);
2457 else {
2458 xcb_unmap_window(conn, r->bar->id);
2459 continue;
2460 }
2461
2462 if (startup_exception)
2463 snprintf(fmtrep, sizeof fmtrep, "total "
2464 "exceptions: %d, first exception: %s",
2465 nr_exceptions,
2466 startup_exception);
2467 else {
2468 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
2469 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
2470 }
2471 if (bar_font_legacy)
2472 bar_print_legacy(r, fmtrep);
2473 else
2474 bar_print(r, fmtrep);
2475 }
2476 }
2477 }
2478
2479 /*
2480 * Reads external script output; call when stdin is readable.
2481 * Returns 1 if bar_ext was updated; otherwise 0.
2482 */
2483 int
2484 bar_extra_update(void)
2485 {
2486 size_t len;
2487 char b[SWM_BAR_MAX];
2488 int changed = 0;
2489
2490 if (!bar_extra)
2491 return changed;
2492
2493 while (fgets(b, sizeof(b), stdin) != NULL) {
2494 if (bar_enabled) {
2495 len = strlen(b);
2496 if (b[len - 1] == '\n') {
2497 /* Remove newline. */
2498 b[--len] = '\0';
2499
2500 /* "Clear" bar_ext. */
2501 bar_ext[0] = '\0';
2502
2503 /* Flush buffered output. */
2504 strlcpy(bar_ext, bar_ext_buf, sizeof(bar_ext));
2505 bar_ext_buf[0] = '\0';
2506
2507 /* Append new output to bar. */
2508 strlcat(bar_ext, b, sizeof(bar_ext));
2509
2510 changed = 1;
2511 } else {
2512 /* Buffer output. */
2513 strlcat(bar_ext_buf, b, sizeof(bar_ext_buf));
2514 }
2515 }
2516 }
2517
2518 if (errno != EAGAIN) {
2519 warn("bar_action failed");
2520 bar_extra_stop();
2521 changed = 1;
2522 }
2523
2524 return changed;
2525 }
2526
2527 void
2528 bar_toggle(struct swm_region *r, union arg *args)
2529 {
2530 struct swm_region *tmpr;
2531 int i, num_screens;
2532
2533 /* suppress unused warnings since vars are needed */
2534 (void)r;
2535 (void)args;
2536
2537 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
2538
2539 switch (args->id) {
2540 case SWM_ARG_ID_BAR_TOGGLE_WS:
2541 /* Only change if master switch is enabled. */
2542 if (bar_enabled)
2543 r->ws->bar_enabled = !r->ws->bar_enabled;
2544 else
2545 bar_enabled = r->ws->bar_enabled = 1;
2546 break;
2547 case SWM_ARG_ID_BAR_TOGGLE:
2548 bar_enabled = !bar_enabled;
2549 break;
2550 }
2551
2552 /* update bars as necessary */
2553 num_screens = get_screen_count();
2554 for (i = 0; i < num_screens; i++)
2555 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
2556 if (tmpr->bar) {
2557 if (bar_enabled && tmpr->ws->bar_enabled)
2558 xcb_map_window(conn, tmpr->bar->id);
2559 else
2560 xcb_unmap_window(conn, tmpr->bar->id);
2561 }
2562
2563 stack();
2564
2565 /* must be after stack */
2566 bar_draw();
2567
2568 focus_flush();
2569 }
2570
2571 void
2572 bar_extra_setup(void)
2573 {
2574 /* do this here because the conf file is in memory */
2575 if (!bar_extra && bar_argv[0]) {
2576 /* launch external status app */
2577 bar_extra = 1;
2578 if (pipe(bar_pipe) == -1)
2579 err(1, "pipe error");
2580 socket_setnonblock(bar_pipe[0]);
2581 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
2582
2583 /* Set stdin to read from the pipe. */
2584 if (dup2(bar_pipe[0], STDIN_FILENO) == -1)
2585 err(1, "dup2");
2586
2587 /* Set stdout to write to the pipe. */
2588 if (dup2(bar_pipe[1], STDOUT_FILENO) == -1)
2589 err(1, "dup2");
2590
2591 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2592 err(1, "could not disable SIGPIPE");
2593 switch (bar_pid = fork()) {
2594 case -1:
2595 err(1, "cannot fork");
2596 break;
2597 case 0: /* child */
2598 close(bar_pipe[0]);
2599 execvp(bar_argv[0], bar_argv);
2600 err(1, "%s external app failed", bar_argv[0]);
2601 break;
2602 default: /* parent */
2603 close(bar_pipe[1]);
2604 break;
2605 }
2606
2607 atexit(kill_bar_extra_atexit);
2608 }
2609 }
2610
2611 void
2612 kill_bar_extra_atexit(void)
2613 {
2614 if (bar_pid)
2615 kill(bar_pid, SIGTERM);
2616 }
2617
2618 int
2619 isxlfd(char *s)
2620 {
2621 int count = 0;
2622
2623 while ((s = index(s, '-'))) {
2624 ++count;
2625 ++s;
2626 }
2627
2628 return (count == 14);
2629 }
2630
2631 void
2632 fontset_init(void)
2633 {
2634 char *default_string;
2635 char **missing_charsets;
2636 int num_missing_charsets = 0;
2637 int i;
2638
2639 if (bar_fs) {
2640 XFreeFontSet(display, bar_fs);
2641 bar_fs = NULL;
2642 }
2643
2644 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2645
2646 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2647 &num_missing_charsets, &default_string);
2648
2649 if (num_missing_charsets > 0) {
2650 warnx("Unable to load charset(s):");
2651
2652 for (i = 0; i < num_missing_charsets; ++i)
2653 warnx("%s", missing_charsets[i]);
2654
2655 XFreeStringList(missing_charsets);
2656
2657 if (strcmp(default_string, ""))
2658 warnx("Glyphs from those sets will be replaced "
2659 "by '%s'.", default_string);
2660 else
2661 warnx("Glyphs from those sets won't be drawn.");
2662 }
2663
2664 if (bar_fs == NULL)
2665 errx(1, "Error creating font set structure.");
2666
2667 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2668
2669 bar_height = bar_fs_extents->max_logical_extent.height +
2670 2 * bar_border_width;
2671
2672 if (bar_height < 1)
2673 bar_height = 1;
2674 }
2675
2676 void
2677 xft_init(struct swm_region *r)
2678 {
2679 char *font, *d, *search;
2680 XRenderColor color;
2681
2682 if (bar_font == NULL) {
2683 if ((d = strdup(bar_fonts)) == NULL)
2684 errx(1, "insufficient memory.");
2685 search = d;
2686 while ((font = strsep(&search, ",")) != NULL) {
2687 if (*font == '\0')
2688 continue;
2689
2690 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2691
2692 if (isxlfd(font)) {
2693 bar_font = XftFontOpenXlfd(display, r->s->idx,
2694 font);
2695 } else {
2696 bar_font = XftFontOpenName(display, r->s->idx,
2697 font);
2698 }
2699
2700 if (bar_font == NULL) {
2701 warnx("unable to load font %s", font);
2702 continue;
2703 } else {
2704 DNPRINTF(SWM_D_INIT, "successfully opened "
2705 "font %s\n", font);
2706 break;
2707 }
2708 }
2709 free(d);
2710 }
2711
2712 if (bar_font == NULL)
2713 errx(1, "unable to open a font");
2714
2715 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2716
2717 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2718 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2719 warn("Xft error: unable to allocate color.");
2720
2721 bar_height = bar_font->height + 2 * bar_border_width;
2722
2723 if (bar_height < 1)
2724 bar_height = 1;
2725 }
2726
2727 void
2728 bar_setup(struct swm_region *r)
2729 {
2730 xcb_screen_t *screen;
2731 uint32_t wa[3];
2732
2733 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2734 r->s->idx);
2735
2736 if ((screen = get_screen(r->s->idx)) == NULL)
2737 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2738
2739 if (r->bar != NULL)
2740 return;
2741
2742 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2743 err(1, "bar_setup: calloc: failed to allocate memory.");
2744
2745 if (bar_font_legacy)
2746 fontset_init();
2747 else
2748 xft_init(r);
2749
2750 X(r->bar) = X(r);
2751 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2752 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2753 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2754
2755 /* Assume region is unfocused when we create the bar. */
2756 r->bar->id = xcb_generate_id(conn);
2757 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2758 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel;
2759 wa[2] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_POINTER_MOTION |
2760 XCB_EVENT_MASK_POINTER_MOTION_HINT;
2761
2762 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2763 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2764 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2765 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2766 | XCB_CW_EVENT_MASK, wa);
2767
2768 r->bar->buffer = xcb_generate_id(conn);
2769 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2770 WIDTH(r->bar), HEIGHT(r->bar));
2771
2772 if (xrandr_support)
2773 xcb_randr_select_input(conn, r->bar->id,
2774 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2775
2776 if (bar_enabled)
2777 xcb_map_window(conn, r->bar->id);
2778
2779 DNPRINTF(SWM_D_BAR, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
2780 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2781 HEIGHT(r->bar));
2782
2783 bar_extra_setup();
2784 }
2785
2786 void
2787 bar_cleanup(struct swm_region *r)
2788 {
2789 if (r->bar == NULL)
2790 return;
2791 xcb_destroy_window(conn, r->bar->id);
2792 xcb_free_pixmap(conn, r->bar->buffer);
2793 free(r->bar);
2794 r->bar = NULL;
2795 }
2796
2797 void
2798 set_win_state(struct ws_win *win, uint8_t state)
2799 {
2800 uint16_t data[2] = { state, XCB_ATOM_NONE };
2801
2802 DNPRINTF(SWM_D_EVENT, "set_win_state: win %#x, state: %u\n",
2803 win->id, state);
2804
2805 if (win == NULL)
2806 return;
2807
2808 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2809 a_state, 32, 2, data);
2810 }
2811
2812 uint8_t
2813 get_win_state(xcb_window_t w)
2814 {
2815 xcb_get_property_reply_t *r;
2816 xcb_get_property_cookie_t c;
2817 uint32_t result = 0;
2818
2819 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2820 r = xcb_get_property_reply(conn, c, NULL);
2821 if (r) {
2822 if (r->type == a_state && r->format == 32 && r->length == 2)
2823 result = *((uint32_t *)xcb_get_property_value(r));
2824 free(r);
2825 }
2826
2827 DNPRINTF(SWM_D_MISC, "get_win_state property: win %#x state %u\n", w,
2828 result);
2829 return (result);
2830 }
2831
2832 void
2833 version(struct swm_region *r, union arg *args)
2834 {
2835 /* suppress unused warnings since vars are needed */
2836 (void)r;
2837 (void)args;
2838
2839 bar_version = !bar_version;
2840 if (bar_version)
2841 snprintf(bar_vertext, sizeof bar_vertext,
2842 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2843 else
2844 strlcpy(bar_vertext, "", sizeof bar_vertext);
2845
2846 bar_draw();
2847 xcb_flush(conn);
2848 }
2849
2850 void
2851 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2852 {
2853 xcb_client_message_event_t ev;
2854 #ifdef SWM_DEBUG
2855 char *name;
2856 #endif
2857
2858 if (win == NULL)
2859 return;
2860 #ifdef SWM_DEBUG
2861 name = get_atom_name(a);
2862 DNPRINTF(SWM_D_EVENT, "client_msg: win %#x, atom: %s(%u), "
2863 "time: %#x\n",
2864 win->id, name, a, t);
2865 free(name);
2866 #endif
2867
2868 bzero(&ev, sizeof ev);
2869 ev.response_type = XCB_CLIENT_MESSAGE;
2870 ev.window = win->id;
2871 ev.type = a_prot;
2872 ev.format = 32;
2873 ev.data.data32[0] = a;
2874 ev.data.data32[1] = t;
2875
2876 xcb_send_event(conn, 0, win->id,
2877 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2878 }
2879
2880 /* synthetic response to a ConfigureRequest when not making a change */
2881 void
2882 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2883 {
2884 xcb_configure_notify_event_t ce;
2885
2886 if (win == NULL)
2887 return;
2888
2889 /* send notification of unchanged state. */
2890 bzero(&ce, sizeof(ce));
2891 ce.response_type = XCB_CONFIGURE_NOTIFY;
2892 ce.x = X(win);
2893 ce.y = Y(win);
2894 ce.width = WIDTH(win);
2895 ce.height = HEIGHT(win);
2896 ce.override_redirect = 0;
2897
2898 if (ev == NULL) {
2899 /* EWMH */
2900 ce.event = win->id;
2901 ce.window = win->id;
2902 ce.border_width = BORDER(win);
2903 ce.above_sibling = XCB_WINDOW_NONE;
2904 } else {
2905 /* normal */
2906 ce.event = ev->window;
2907 ce.window = ev->window;
2908
2909 /* make response appear more WM_SIZE_HINTS-compliant */
2910 if (win->sh.flags) {
2911 DNPRINTF(SWM_D_MISC, "config_win: hints: win %#x,"
2912 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2913 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2914 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2915 SH_INC_W(win), SH_INC_H(win));
2916 }
2917
2918 /* min size */
2919 if (SH_MIN(win)) {
2920 /* the hint may be set... to 0! */
2921 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2922 ce.width = SH_MIN_W(win);
2923 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2924 ce.height = SH_MIN_H(win);
2925 }
2926
2927 /* max size */
2928 if (SH_MAX(win)) {
2929 /* may also be advertized as 0 */
2930 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2931 ce.width = SH_MAX_W(win);
2932 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2933 ce.height = SH_MAX_H(win);
2934 }
2935
2936 /* resize increment. */
2937 if (SH_INC(win)) {
2938 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2939 ce.width -= (ce.width - SH_MIN_W(win)) %
2940 SH_INC_W(win);
2941 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2942 ce.height -= (ce.height - SH_MIN_H(win)) %
2943 SH_INC_H(win);
2944 }
2945
2946 /* adjust x and y for requested border_width. */
2947 ce.x += BORDER(win) - ev->border_width;
2948 ce.y += BORDER(win) - ev->border_width;
2949 ce.border_width = ev->border_width;
2950 ce.above_sibling = ev->sibling;
2951 }
2952
2953 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
2954 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2955 ce.y, ce.width, ce.height, ce.border_width);
2956
2957 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2958 (char *)&ce);
2959 }
2960
2961 int
2962 count_win(struct workspace *ws, int count_transient)
2963 {
2964 struct ws_win *win;
2965 int count = 0;
2966
2967 TAILQ_FOREACH(win, &ws->winlist, entry) {
2968 if (!count_transient && FLOATING(win))
2969 continue;
2970 if (ICONIC(win))
2971 continue;
2972 count++;
2973 }
2974 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2975
2976 return (count);
2977 }
2978
2979 void
2980 quit(struct swm_region *r, union arg *args)
2981 {
2982 /* suppress unused warnings since vars are needed */
2983 (void)r;
2984 (void)args;
2985
2986 DNPRINTF(SWM_D_MISC, "quit\n");
2987 running = 0;
2988 }
2989
2990 void
2991 raise_window(struct ws_win *win)
2992 {
2993 struct ws_win *target = NULL;
2994 struct swm_region *r;
2995 struct workspace *ws;
2996
2997 if (win == NULL || (r = win->ws->r) == NULL)
2998 return;
2999 ws = win->ws;
3000
3001 DNPRINTF(SWM_D_EVENT, "raise_window: win %#x\n", win->id);
3002
3003 TAILQ_FOREACH(target, &ws->stack, stack_entry) {
3004 if (target == win || ICONIC(target))
3005 continue;
3006 if (ws->cur_layout == &layouts[SWM_MAX_STACK])
3007 break;
3008 if (TRANS(win) && (win->transient == target->transient ||
3009 win->transient == target->id))
3010 break;
3011 if (FULLSCREEN(win))
3012 break;
3013 if (FULLSCREEN(target))
3014 continue;
3015 if (MAXIMIZED(win))
3016 break;
3017 if (MAXIMIZED(target))
3018 continue;
3019 if (ABOVE(win) || TRANS(win))
3020 break;
3021 if (!ABOVE(target) && !TRANS(target))
3022 break;
3023 }
3024
3025 if (target != NULL) {
3026 /* Change stack position. */
3027 TAILQ_REMOVE(&ws->stack, win, stack_entry);
3028 TAILQ_INSERT_BEFORE(target, win, stack_entry);
3029 update_win_stacking(win);
3030 }
3031
3032 #ifdef SWM_DEBUG
3033 if (swm_debug & SWM_D_STACK) {
3034 DPRINTF("=== stacking order (top down) === \n");
3035 TAILQ_FOREACH(target, &r->ws->stack, stack_entry) {
3036 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3037 "iconic: %s\n", target->id, YESNO(FULLSCREEN(target)),
3038 YESNO(MAXIMIZED(target)), YESNO(ABOVE(target)),
3039 YESNO(ICONIC(target)));
3040 }
3041 }
3042 #endif
3043 DNPRINTF(SWM_D_EVENT, "raise_window: done\n");
3044 }
3045
3046 void
3047 update_win_stacking(struct ws_win *win)
3048 {
3049 struct ws_win *sibling;
3050 struct swm_region *r;
3051 uint32_t val[2];
3052
3053 if (win == NULL || (r = win->ws->r) == NULL)
3054 return;
3055
3056 sibling = TAILQ_NEXT(win, stack_entry);
3057 if (sibling != NULL && FLOATING(win) == FLOATING(sibling))
3058 val[0] = sibling->id;
3059 else
3060 val[0] = FLOATING(win) ? r->bar->id : r->id;
3061
3062 DNPRINTF(SWM_D_EVENT, "update_win_stacking: %#x, sibling %#x\n",
3063 win->id, val[0]);
3064
3065 val[1] = XCB_STACK_MODE_ABOVE;
3066
3067 xcb_configure_window(conn, win->id, XCB_CONFIG_WINDOW_SIBLING |
3068 XCB_CONFIG_WINDOW_STACK_MODE, val);
3069 }
3070
3071 void
3072 map_window(struct ws_win *win)
3073 {
3074 if (win == NULL)
3075 return;
3076
3077 DNPRINTF(SWM_D_EVENT, "map_window: win %#x, mapped: %s\n",
3078 win->id, YESNO(win->mapped));
3079
3080 if (win->mapped)
3081 return;
3082
3083 xcb_map_window(conn, win->id);
3084 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
3085 win->mapped = 1;
3086 }
3087
3088 void
3089 unmap_window(struct ws_win *win)
3090 {
3091 if (win == NULL)
3092 return;
3093
3094 DNPRINTF(SWM_D_EVENT, "unmap_window: win %#x, mapped: %s\n", win->id,
3095 YESNO(win->mapped));
3096
3097 if (!win->mapped)
3098 return;
3099
3100 xcb_unmap_window(conn, win->id);
3101 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
3102 win->mapped = 0;
3103 }
3104
3105 void
3106 unmap_all(void)
3107 {
3108 struct ws_win *win;
3109 int i, j, num_screens;
3110
3111 num_screens = get_screen_count();
3112 for (i = 0; i < num_screens; i++)
3113 for (j = 0; j < workspace_limit; j++)
3114 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3115 unmap_window(win);
3116 }
3117
3118 void
3119 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
3120 {
3121 xcb_key_press_event_t event;
3122 xcb_keycode_t *keycode;
3123
3124 if (win == NULL)
3125 return;
3126
3127 keycode = xcb_key_symbols_get_keycode(syms, keysym);
3128
3129 DNPRINTF(SWM_D_MISC, "fake_keypress: win %#x, keycode %u\n",
3130 win->id, *keycode);
3131
3132 bzero(&event, sizeof(event));
3133 event.event = win->id;
3134 event.root = win->s->root;
3135 event.child = XCB_WINDOW_NONE;
3136 event.time = XCB_CURRENT_TIME;
3137 event.event_x = X(win);
3138 event.event_y = Y(win);
3139 event.root_x = 1;
3140 event.root_y = 1;
3141 event.same_screen = 1;
3142 event.detail = *keycode;
3143 event.state = modifiers;
3144
3145 event.response_type = XCB_KEY_PRESS;
3146 xcb_send_event(conn, 1, win->id,
3147 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
3148
3149 event.response_type = XCB_KEY_RELEASE;
3150 xcb_send_event(conn, 1, win->id,
3151 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
3152
3153 free(keycode);
3154 }
3155
3156 void
3157 restart(struct swm_region *r, union arg *args)
3158 {
3159 /* suppress unused warning since var is needed */
3160 (void)r;
3161 (void)args;
3162
3163 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
3164
3165 shutdown_cleanup();
3166
3167 execvp(start_argv[0], start_argv);
3168 warn("execvp failed");
3169 quit(NULL, NULL);
3170 }
3171
3172 struct ws_win *
3173 get_pointer_win(xcb_window_t root)
3174 {
3175 struct ws_win *win = NULL;
3176 xcb_query_pointer_reply_t *r;
3177
3178 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: %#x.\n", root);
3179
3180 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
3181 if (r) {
3182 win = find_window(r->child);
3183 if (win) {
3184 DNPRINTF(SWM_D_EVENT, "get_pointer_win: %#x.\n",
3185 win->id);
3186 } else {
3187 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
3188 }
3189 free(r);
3190 }
3191
3192 return win;
3193 }
3194
3195 struct swm_region *
3196 root_to_region(xcb_window_t root, int check)
3197 {
3198 struct ws_win *cfw;
3199 struct swm_region *r = NULL;
3200 int i, num_screens;
3201 xcb_query_pointer_reply_t *qpr;
3202 xcb_get_input_focus_reply_t *gifr;
3203
3204 DNPRINTF(SWM_D_MISC, "root_to_region: win %#x\n", root);
3205
3206 num_screens = get_screen_count();
3207 for (i = 0; i < num_screens; i++)
3208 if (screens[i].root == root)
3209 break;
3210
3211 if (check & SWM_CK_REGION)
3212 r = screens[i].r_focus;
3213
3214 if (r == NULL && check & SWM_CK_FOCUS) {
3215 /* Try to find an actively focused window */
3216 gifr = xcb_get_input_focus_reply(conn,
3217 xcb_get_input_focus(conn), NULL);
3218 if (gifr) {
3219 cfw = find_window(gifr->focus);
3220 if (cfw && cfw->ws->r)
3221 r = cfw->ws->r;
3222
3223 free(gifr);
3224 }
3225 }
3226
3227 if (r == NULL && check & SWM_CK_POINTER) {
3228 /* No region with an active focus; try to use pointer. */
3229 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
3230 screens[i].root), NULL);
3231 if (qpr) {
3232 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
3233 "(%d,%d)\n", qpr->root_x, qpr->root_y);
3234 TAILQ_FOREACH(r, &screens[i].rl, entry)
3235 if (X(r) <= qpr->root_x &&
3236 qpr->root_x < MAX_X(r) &&
3237 Y(r) <= qpr->root_y &&
3238 qpr->root_y < MAX_Y(r))
3239 break;
3240 free(qpr);
3241 }
3242 }
3243
3244 /* Last resort. */
3245 if (r == NULL && check & SWM_CK_FALLBACK)
3246 r = TAILQ_FIRST(&screens[i].rl);
3247
3248 DNPRINTF(SWM_D_MISC, "root_to_region: idx: %d\n", get_region_index(r));
3249
3250 return (r);
3251 }
3252
3253 struct ws_win *
3254 find_unmanaged_window(xcb_window_t id)
3255 {
3256 struct ws_win *win;
3257 int i, j, num_screens;
3258
3259 num_screens = get_screen_count();
3260 for (i = 0; i < num_screens; i++)
3261 for (j = 0; j < workspace_limit; j++)
3262 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
3263 entry)
3264 if (id == win->id)
3265 return (win);
3266 return (NULL);
3267 }
3268
3269 struct ws_win *
3270 find_window(xcb_window_t id)
3271 {
3272 struct ws_win *win;
3273 int i, j, num_screens;
3274 xcb_query_tree_reply_t *r;
3275
3276 num_screens = get_screen_count();
3277 for (i = 0; i < num_screens; i++)
3278 for (j = 0; j < workspace_limit; j++)
3279 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3280 if (id == win->id)
3281 return (win);
3282
3283 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
3284 if (r == NULL)
3285 return (NULL);
3286
3287 /* if we were looking for the parent return that window instead */
3288 if (r->parent == 0 || r->root == r->parent) {
3289 free(r);
3290 return (NULL);
3291 }
3292
3293 /* look for parent */
3294 for (i = 0; i < num_screens; i++)
3295 for (j = 0; j < workspace_limit; j++)
3296 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3297 if (r->parent == win->id) {
3298 free(r);
3299 return (win);
3300 }
3301
3302 free(r);
3303 return (NULL);
3304 }
3305
3306 void
3307 spawn(int ws_idx, union arg *args, int close_fd)
3308 {
3309 int fd;
3310 char *ret = NULL;
3311
3312 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
3313
3314 close(xcb_get_file_descriptor(conn));
3315
3316 setenv("LD_PRELOAD", SWM_LIB, 1);
3317
3318 if (asprintf(&ret, "%d", ws_idx) == -1) {
3319 warn("spawn: asprintf SWM_WS");
3320 _exit(1);
3321 }
3322 setenv("_SWM_WS", ret, 1);
3323 free(ret);
3324 ret = NULL;
3325
3326 if (asprintf(&ret, "%d", getpid()) == -1) {
3327 warn("spawn: asprintf _SWM_PID");
3328 _exit(1);
3329 }
3330 setenv("_SWM_PID", ret, 1);
3331 free(ret);
3332 ret = NULL;
3333
3334 if (setsid() == -1) {
3335 warn("spawn: setsid");
3336 _exit(1);
3337 }
3338
3339 if (close_fd) {
3340 /*
3341 * close stdin and stdout to prevent interaction between apps
3342 * and the baraction script
3343 * leave stderr open to record errors
3344 */
3345 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
3346 warn("spawn: open");
3347 _exit(1);
3348 }
3349 dup2(fd, STDIN_FILENO);
3350 dup2(fd, STDOUT_FILENO);
3351 if (fd > 2)
3352 close(fd);
3353 }
3354
3355 execvp(args->argv[0], args->argv);
3356
3357 warn("spawn: execvp");
3358 _exit(1);
3359 }
3360
3361 void
3362 kill_refs(struct ws_win *win)
3363 {
3364 int i, x, num_screens;
3365 struct swm_region *r;
3366 struct workspace *ws;
3367
3368 if (win == NULL)
3369 return;
3370
3371 num_screens = get_screen_count();
3372 for (i = 0; i < num_screens; i++)
3373 TAILQ_FOREACH(r, &screens[i].rl, entry)
3374 for (x = 0; x < workspace_limit; x++) {
3375 ws = &r->s->ws[x];
3376 if (win == ws->focus)
3377 ws->focus = NULL;
3378 if (win == ws->focus_prev)
3379 ws->focus_prev = NULL;
3380 }
3381 }
3382
3383 int
3384 validate_win(struct ws_win *testwin)
3385 {
3386 struct ws_win *win;
3387 struct workspace *ws;
3388 struct swm_region *r;
3389 int i, x, num_screens;
3390
3391 if (testwin == NULL)
3392 return (0);
3393
3394 num_screens = get_screen_count();
3395 for (i = 0; i < num_screens; i++)
3396 TAILQ_FOREACH(r, &screens[i].rl, entry)
3397 for (x = 0; x < workspace_limit; x++) {
3398 ws = &r->s->ws[x];
3399 TAILQ_FOREACH(win, &ws->winlist, entry)
3400 if (win == testwin)
3401 return (0);
3402 }
3403 return (1);
3404 }
3405
3406 int
3407 validate_ws(struct workspace *testws)
3408 {
3409 struct swm_region *r;
3410 struct workspace *ws;
3411 int i, x, num_screens;
3412
3413 /* validate all ws */
3414 num_screens = get_screen_count();
3415 for (i = 0; i < num_screens; i++)
3416 TAILQ_FOREACH(r, &screens[i].rl, entry)
3417 for (x = 0; x < workspace_limit; x++) {
3418 ws = &r->s->ws[x];
3419 if (ws == testws)
3420 return (0);
3421 }
3422 return (1);
3423 }
3424
3425 void
3426 unfocus_win(struct ws_win *win)
3427 {
3428 xcb_window_t none = XCB_WINDOW_NONE;
3429
3430 DNPRINTF(SWM_D_FOCUS, "unfocus_win: win %#x\n", WINID(win));
3431
3432 if (win == NULL)
3433 return;
3434
3435 if (win->ws == NULL) {
3436 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3437 return;
3438 }
3439
3440 if (validate_ws(win->ws)) {
3441 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3442 return;
3443 }
3444
3445 if (win->ws->r == NULL) {
3446 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3447 return;
3448 }
3449
3450 if (validate_win(win)) {
3451 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3452 kill_refs(win);
3453 return;
3454 }
3455
3456 if (win->ws->focus == win) {
3457 win->ws->focus = NULL;
3458 win->ws->focus_prev = win;
3459 }
3460
3461 if (validate_win(win->ws->focus)) {
3462 kill_refs(win->ws->focus);
3463 win->ws->focus = NULL;
3464 }
3465
3466 if (validate_win(win->ws->focus_prev)) {
3467 kill_refs(win->ws->focus_prev);
3468 win->ws->focus_prev = NULL;
3469 }
3470
3471 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3472 &win->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3473
3474 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3475 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3476
3477 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3478 }
3479
3480 void
3481 focus_win(struct ws_win *win)
3482 {
3483 struct ws_win *cfw = NULL, *parent = NULL, *w;
3484 struct workspace *ws;
3485 xcb_get_input_focus_reply_t *gifr;
3486
3487 DNPRINTF(SWM_D_FOCUS, "focus_win: win %#x\n", WINID(win));
3488
3489 if (win == NULL)
3490 goto out;
3491
3492 if (win->ws == NULL)
3493 goto out;
3494
3495 if (!win->mapped)
3496 goto out;
3497
3498 ws = win->ws;
3499
3500 if (validate_ws(ws))
3501 goto out;
3502
3503 if (validate_win(win)) {
3504 kill_refs(win);
3505 goto out;
3506 }
3507
3508 gifr = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3509 if (gifr) {
3510 cfw = find_window(gifr->focus);
3511 if (cfw != NULL && cfw != win) {
3512 if (cfw->ws != ws && cfw->ws->r != NULL) {
3513 /* Change border to unfocused color. */
3514 xcb_change_window_attributes(conn, cfw->id,
3515 XCB_CW_BORDER_PIXEL,
3516 &cfw->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3517 } else {
3518 unfocus_win(cfw);
3519 }
3520 }
3521 free(gifr);
3522 }
3523
3524 if (ws->focus != win) {
3525 if (ws->focus && ws->focus != cfw)
3526 unfocus_win(ws->focus);
3527 ws->focus = win;
3528 }
3529
3530 /* If this window directs focus to a child window, then clear. */
3531 if (win->focus_child)
3532 win->focus_child = NULL;
3533
3534 /* If transient, adjust parent's focus child for focus_magic. */
3535 if (TRANS(win)) {
3536 parent = find_window(win->transient);
3537 if (parent && parent->focus_child != win)
3538 parent->focus_child = win;
3539 }
3540
3541 if (cfw != win && ws->r != NULL) {
3542 /* Set input focus if no input hint, or indicated by hint. */
3543 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
3544 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
3545 win->hints.input))
3546 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3547 win->id, last_event_time);
3548 else
3549 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3550 ws->r->id, XCB_CURRENT_TIME);
3551
3552 /* Tell app it can adjust focus to a specific window. */
3553 if (win->take_focus) {
3554 /* java is special; always tell parent */
3555 if (TRANS(win) && win->java)
3556 client_msg(parent, a_takefocus,
3557 last_event_time);
3558 else
3559 client_msg(win, a_takefocus, last_event_time);
3560 }
3561
3562 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3563 &win->s->c[SWM_S_COLOR_FOCUS].pixel);
3564
3565 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
3566 ws->always_raise) {
3567 /* If a parent exists, map it first. */
3568 if (parent) {
3569 raise_window(parent);
3570 map_window(parent);
3571
3572 /* Map siblings next. */
3573 TAILQ_FOREACH(w, &ws->winlist, entry)
3574 if (w != win && !ICONIC(w) &&
3575 win->transient == parent->id)
3576 map_window(w);
3577 }
3578
3579 /* Map focused window. */
3580 raise_window(win);
3581 map_window(win);
3582
3583 /* Finally, map children of focus window. */
3584 TAILQ_FOREACH(w, &ws->winlist, entry)
3585 if (w->transient == win->id && !ICONIC(w))
3586 map_window(w);
3587 } else if (tile_gap < 0 && !ABOVE(win)) {
3588 /*
3589 * Windows overlap in the layout.
3590 * Raise focused win above all tiled wins.
3591 */
3592 raise_window(win);
3593 map_window(win);
3594 }
3595
3596 set_region(ws->r);
3597
3598 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3599 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3600 &win->id);
3601 }
3602
3603 out:
3604 bar_draw();
3605
3606 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
3607 }
3608
3609 /* If a child window should have focus instead, return it. */
3610 struct ws_win *
3611 get_focus_magic(struct ws_win *win)
3612 {
3613 struct ws_win *parent = NULL;
3614 struct ws_win *child = NULL;
3615
3616 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: win %#x\n", WINID(win));
3617 if (win == NULL)
3618 return win;
3619
3620 if (TRANS(win)) {
3621 parent = find_window(win->transient);
3622
3623 /* If parent prefers focus elsewhere, then try to do so. */
3624 if (parent && (child = parent->focus_child)) {
3625 if (validate_win(child) == 0 && child->mapped)
3626 win = child;
3627 else
3628 parent->focus_child = NULL;
3629 }
3630 }
3631
3632 /* If this window prefers focus elsewhere, then try to do so. */
3633 if ((child = win->focus_child)) {
3634 if (validate_win(child) == 0 && child->mapped)
3635 win = child;
3636 else
3637 win->focus_child = NULL;
3638 }
3639
3640 return win;
3641 }
3642
3643 void
3644 event_drain(uint8_t rt)
3645 {
3646 xcb_generic_event_t *evt;
3647
3648 /* ensure all pending requests have been processed before filtering. */
3649 xcb_aux_sync(conn);
3650 while ((evt = xcb_poll_for_event(conn))) {
3651 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
3652 event_handle(evt);
3653
3654 free(evt);
3655 }
3656 }
3657
3658 void
3659 set_region(struct swm_region *r)
3660 {
3661 struct swm_region *rf;
3662 int vals[2];
3663
3664 if (r == NULL)
3665 return;
3666
3667 rf = r->s->r_focus;
3668 /* Unfocus old region bar. */
3669 if (rf != NULL) {
3670 if (rf == r)
3671 return;
3672
3673 xcb_change_window_attributes(conn, rf->bar->id,
3674 XCB_CW_BORDER_PIXEL,
3675 &r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel);
3676 }
3677
3678 if (rf != NULL && rf != r && (X(rf) != X(r) || Y(rf) != Y(r) ||
3679 WIDTH(rf) != WIDTH(r) || HEIGHT(rf) != HEIGHT(r))) {
3680 /* Set _NET_DESKTOP_GEOMETRY. */
3681 vals[0] = WIDTH(r);
3682 vals[1] = HEIGHT(r);
3683 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, r->s->root,
3684 ewmh[_NET_DESKTOP_GEOMETRY].atom, XCB_ATOM_CARDINAL, 32, 2,
3685 &vals);
3686 }
3687
3688 /* Set region bar border to focus_color. */
3689 xcb_change_window_attributes(conn, r->bar->id,
3690 XCB_CW_BORDER_PIXEL, &r->s->c[SWM_S_COLOR_BAR_BORDER].pixel);
3691
3692 r->s->r_focus = r;
3693
3694 ewmh_update_current_desktop();
3695 }
3696
3697 void
3698 focus_region(struct swm_region *r)
3699 {
3700 struct ws_win *nfw;
3701 struct swm_region *old_r;
3702
3703 if (r == NULL)
3704 return;
3705
3706 old_r = r->s->r_focus;
3707 set_region(r);
3708
3709 nfw = get_region_focus(r);
3710 if (nfw) {
3711 focus_win(nfw);
3712 } else {
3713 /* New region is empty; need to manually unfocus win. */
3714 if (old_r)
3715 unfocus_win(old_r->ws->focus);
3716
3717 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3718 XCB_CURRENT_TIME);
3719
3720 /* Clear bar since empty. */
3721 bar_draw();
3722 }
3723 }
3724
3725 void
3726 switchws(struct swm_region *r, union arg *args)
3727 {
3728 struct swm_region *this_r, *other_r;
3729 struct ws_win *win;
3730 struct workspace *new_ws, *old_ws;
3731 xcb_window_t none = XCB_WINDOW_NONE;
3732 int wsid = args->id, unmap_old = 0;
3733
3734 if (!(r && r->s))
3735 return;
3736
3737 if (wsid >= workspace_limit)
3738 return;
3739
3740 this_r = r;
3741 old_ws = this_r->ws;
3742 new_ws = &this_r->s->ws[wsid];
3743
3744 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3745 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
3746
3747 if (new_ws == NULL || old_ws == NULL)
3748 return;
3749 if (new_ws == old_ws)
3750 return;
3751
3752 if ((win = old_ws->focus) != NULL) {
3753 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3754 &win->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3755
3756 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3757 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3758 &none);
3759 }
3760
3761 other_r = new_ws->r;
3762 if (other_r == NULL) {
3763 /* the other workspace is hidden, hide this one */
3764 old_ws->r = NULL;
3765 unmap_old = 1;
3766 } else {
3767 /* the other ws is visible in another region, exchange them */
3768 other_r->ws_prior = new_ws;
3769 other_r->ws = old_ws;
3770 old_ws->r = other_r;
3771 }
3772 this_r->ws_prior = old_ws;
3773 this_r->ws = new_ws;
3774 new_ws->r = this_r;
3775
3776 /* Set focus_pending before stacking, if needed. */
3777 if (focus_mode != SWM_FOCUS_FOLLOW && (!new_ws->focus_pending ||
3778 validate_win(new_ws->focus_pending))) {
3779 new_ws->focus_pending = get_region_focus(new_ws->r);
3780 new_ws->focus = new_ws->focus_prev;
3781 new_ws->focus_prev = NULL;
3782 }
3783
3784 new_ws->state = SWM_WS_STATE_MAPPING;
3785 stack();
3786
3787 /* unmap old windows */
3788 if (unmap_old) {
3789 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3790 unmap_window(win);
3791 old_ws->state = SWM_WS_STATE_HIDDEN;
3792 }
3793
3794 /* if workspaces were swapped, then don't wait to set focus */
3795 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3796 if (new_ws->focus_pending) {
3797 focus_win(new_ws->focus_pending);
3798 new_ws->focus_pending = NULL;
3799 }
3800 }
3801
3802 /* Clear bar and set focus on region input win if new ws is empty. */
3803 if (new_ws->focus_pending == NULL && new_ws->focus == NULL) {
3804 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3805 XCB_CURRENT_TIME);
3806 bar_draw();
3807 }
3808
3809 ewmh_update_current_desktop();
3810
3811 focus_flush();
3812 new_ws->state = SWM_WS_STATE_MAPPED;
3813
3814 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3815 }
3816
3817 void
3818 cyclews(struct swm_region *r, union arg *args)
3819 {
3820 union arg a;
3821 struct swm_screen *s = r->s;
3822 int cycle_all = 0;
3823 int move = 0;
3824
3825 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3826 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3827
3828 a.id = r->ws->idx;
3829
3830 do {
3831 switch (args->id) {
3832 case SWM_ARG_ID_CYCLEWS_MOVE_UP:
3833 move = 1;
3834 /* FALLTHROUGH */
3835 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3836 cycle_all = 1;
3837 /* FALLTHROUGH */
3838 case SWM_ARG_ID_CYCLEWS_UP:
3839 a.id = (a.id < workspace_limit - 1) ? a.id + 1 : 0;
3840 break;
3841 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN:
3842 move = 1;
3843 /* FALLTHROUGH */
3844 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3845 cycle_all = 1;
3846 /* FALLTHROUGH */
3847 case SWM_ARG_ID_CYCLEWS_DOWN:
3848 a.id = (a.id > 0) ? a.id - 1 : workspace_limit - 1;
3849 break;
3850 default:
3851 return;
3852 };
3853
3854 if (!cycle_all &&
3855 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3856 continue;
3857 if (!cycle_visible && s->ws[a.id].r != NULL)
3858 continue;
3859
3860 if (move)
3861 send_to_ws(r, &a);
3862
3863 switchws(r, &a);
3864 } while (a.id != r->ws->idx);
3865
3866 DNPRINTF(SWM_D_FOCUS, "cyclews: done\n");
3867 }
3868
3869 void
3870 priorws(struct swm_region *r, union arg *args)
3871 {
3872 union arg a;
3873
3874 (void)args;
3875
3876 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3877 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3878
3879 if (r->ws_prior == NULL)
3880 return;
3881
3882 a.id = r->ws_prior->idx;
3883 switchws(r, &a);
3884 DNPRINTF(SWM_D_FOCUS, "priorws: done\n");
3885 }
3886
3887 void
3888 focusrg(struct swm_region *r, union arg *args)
3889 {
3890 int ridx = args->id, i, num_screens;
3891 struct swm_region *rr = NULL;
3892
3893 num_screens = get_screen_count();
3894 /* do nothing if we don't have more than one screen */
3895 if (!(num_screens > 1 || outputs > 1))
3896 return;
3897
3898 DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
3899
3900 rr = TAILQ_FIRST(&r->s->rl);
3901 for (i = 0; i < ridx && rr != NULL; ++i)
3902 rr = TAILQ_NEXT(rr, entry);
3903
3904 if (rr == NULL)
3905 return;
3906
3907 focus_region(rr);
3908 focus_flush();
3909 DNPRINTF(SWM_D_FOCUS, "focusrg: done\n");
3910 }
3911
3912 void
3913 cyclerg(struct swm_region *r, union arg *args)
3914 {
3915 struct swm_region *rr = NULL;
3916 int i, num_screens;
3917
3918 num_screens = get_screen_count();
3919 /* do nothing if we don't have more than one screen */
3920 if (!(num_screens > 1 || outputs > 1))
3921 return;
3922
3923 i = r->s->idx;
3924 DNPRINTF(SWM_D_FOCUS, "cyclerg: id: %d, region: %d\n", args->id, i);
3925
3926 switch (args->id) {
3927 case SWM_ARG_ID_CYCLERG_UP:
3928 rr = TAILQ_NEXT(r, entry);
3929 if (rr == NULL)
3930 rr = TAILQ_FIRST(&screens[i].rl);
3931 break;
3932 case SWM_ARG_ID_CYCLERG_DOWN:
3933 rr = TAILQ_PREV(r, swm_region_list, entry);
3934 if (rr == NULL)
3935 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3936 break;
3937 default:
3938 return;
3939 };
3940 if (rr == NULL)
3941 return;
3942
3943 focus_region(rr);
3944 focus_flush();
3945 DNPRINTF(SWM_D_FOCUS, "cyclerg: done\n");
3946 }
3947
3948 /* Sorts transients after parent. */
3949 void
3950 sort_windows(struct ws_win_list *wl)
3951 {
3952 struct ws_win *win, *parent, *nxt;
3953
3954 if (wl == NULL)
3955 return;
3956
3957 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3958 nxt = TAILQ_NEXT(win, entry);
3959 if (TRANS(win)) {
3960 parent = find_window(win->transient);
3961 if (parent == NULL) {
3962 warnx("not possible bug");
3963 continue;
3964 }
3965 TAILQ_REMOVE(wl, win, entry);
3966 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3967 }
3968 }
3969 }
3970
3971 void
3972 swapwin(struct swm_region *r, union arg *args)
3973 {
3974 struct ws_win *target, *source;
3975 struct ws_win *cur_focus;
3976 struct ws_win_list *wl;
3977
3978 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3979 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3980
3981 cur_focus = r->ws->focus;
3982 if (cur_focus == NULL || FULLSCREEN(cur_focus))
3983 return;
3984
3985 /* Adjust stacking in floating layer. */
3986 if (ABOVE(cur_focus)) {
3987 switch (args->id) {
3988 case SWM_ARG_ID_SWAPPREV:
3989 target = TAILQ_PREV(cur_focus, ws_win_stack,
3990 stack_entry);
3991 if (target != NULL && FLOATING(target)) {
3992 TAILQ_REMOVE(&cur_focus->ws->stack, cur_focus,
3993 stack_entry);
3994 TAILQ_INSERT_BEFORE(target, cur_focus,
3995 stack_entry);
3996 update_win_stacking(cur_focus);
3997 focus_flush();
3998 }
3999 break;
4000 case SWM_ARG_ID_SWAPNEXT:
4001 target = TAILQ_NEXT(cur_focus, stack_entry);
4002 if (target != NULL && FLOATING(target)) {
4003 TAILQ_REMOVE(&cur_focus->ws->stack, cur_focus,
4004 stack_entry);
4005 TAILQ_INSERT_AFTER(&cur_focus->ws->stack,
4006 target, cur_focus, stack_entry);
4007 update_win_stacking(cur_focus);
4008 focus_flush();
4009 }
4010 break;
4011 }
4012 goto out;
4013 }
4014
4015 if (r->ws->cur_layout == &layouts[SWM_MAX_STACK])
4016 return;
4017
4018 clear_maximized(r->ws);
4019
4020 source = cur_focus;
4021 wl = &source->ws->winlist;
4022
4023 switch (args->id) {
4024 case SWM_ARG_ID_SWAPPREV:
4025 if (TRANS(source))
4026 source = find_window(source->transient);
4027 target = TAILQ_PREV(source, ws_win_list, entry);
4028 if (target && target->transient)
4029 target = find_window(target->transient);
4030 TAILQ_REMOVE(wl, source, entry);
4031 if (target == NULL)
4032 TAILQ_INSERT_TAIL(wl, source, entry);
4033 else
4034 TAILQ_INSERT_BEFORE(target, source, entry);
4035 break;
4036 case SWM_ARG_ID_SWAPNEXT:
4037 target = TAILQ_NEXT(source, entry);
4038 /* move the parent and let the sort handle the move */
4039 if (TRANS(source))
4040 source = find_window(source->transient);
4041 TAILQ_REMOVE(wl, source, entry);
4042 if (target == NULL)
4043 TAILQ_INSERT_HEAD(wl, source, entry);
4044 else
4045 TAILQ_INSERT_AFTER(wl, target, source, entry);
4046 break;
4047 case SWM_ARG_ID_SWAPMAIN:
4048 target = TAILQ_FIRST(wl);
4049 if (target == source) {
4050 if (source->ws->focus_prev != NULL &&
4051 source->ws->focus_prev != target)
4052 source = source->ws->focus_prev;
4053 else
4054 return;
4055 }
4056 if (target == NULL || source == NULL)
4057 return;
4058 source->ws->focus_prev = target;
4059 TAILQ_REMOVE(wl, target, entry);
4060 TAILQ_INSERT_BEFORE(source, target, entry);
4061 TAILQ_REMOVE(wl, source, entry);
4062 TAILQ_INSERT_HEAD(wl, source, entry);
4063 break;
4064 case SWM_ARG_ID_MOVELAST:
4065 TAILQ_REMOVE(wl, source, entry);
4066 TAILQ_INSERT_TAIL(wl, source, entry);
4067 break;
4068 default:
4069 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
4070 return;
4071 }
4072
4073 sort_windows(wl);
4074 ewmh_update_client_list();
4075
4076 stack();
4077 focus_flush();
4078 out:
4079 DNPRINTF(SWM_D_MOVE, "swapwin: done\n");
4080 }
4081
4082 struct ws_win *
4083 get_focus_prev(struct ws_win *win)
4084 {
4085 struct ws_win *winfocus = NULL;
4086 struct ws_win *cur_focus = NULL;
4087 struct ws_win_list *wl = NULL;
4088 struct workspace *ws = NULL;
4089
4090 if (!(win && win->ws))
4091 return NULL;
4092
4093 ws = win->ws;
4094 wl = &ws->winlist;
4095 cur_focus = ws->focus;
4096
4097 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: win %#x, cur_focus: %#x, "
4098 "focus_prev: %#x\n", WINID(win), WINID(cur_focus),
4099 WINID(ws->focus_prev));
4100
4101 /* pickle, just focus on whatever */
4102 if (cur_focus == NULL) {
4103 /* use prev_focus if valid */
4104 if (ws->focus_prev && find_window(ws->focus_prev->id))
4105 winfocus = ws->focus_prev;
4106 goto done;
4107 }
4108
4109 /* if transient focus on parent */
4110 if (TRANS(cur_focus)) {
4111 winfocus = find_window(cur_focus->transient);
4112 goto done;
4113 }
4114
4115 /* if in max_stack try harder */
4116 if ((win->quirks & SWM_Q_FOCUSPREV) ||
4117 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
4118 if (cur_focus != ws->focus_prev)
4119 winfocus = ws->focus_prev;
4120 else
4121 winfocus = TAILQ_PREV(win, ws_win_list, entry);
4122 if (winfocus)
4123 goto done;
4124 }
4125
4126 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
4127
4128 if (winfocus == NULL || winfocus == win) {
4129 switch (focus_close) {
4130 case SWM_STACK_BOTTOM:
4131 TAILQ_FOREACH(winfocus, wl, entry)
4132 if (!ICONIC(winfocus) && winfocus != cur_focus)
4133 break;
4134 break;
4135 case SWM_STACK_TOP:
4136 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4137 if (!ICONIC(winfocus) && winfocus != cur_focus)
4138 break;
4139 break;
4140 case SWM_STACK_ABOVE:
4141 winfocus = TAILQ_NEXT(cur_focus, entry);
4142 while (winfocus && ICONIC(winfocus))
4143 winfocus = TAILQ_NEXT(winfocus, entry);
4144
4145 if (winfocus == NULL) {
4146 if (focus_close_wrap) {
4147 TAILQ_FOREACH(winfocus, wl, entry)
4148 if (!ICONIC(winfocus) &&
4149 winfocus != cur_focus)
4150 break;
4151 } else {
4152 TAILQ_FOREACH_REVERSE(winfocus, wl,
4153 ws_win_list, entry)
4154 if (!ICONIC(winfocus) &&
4155 winfocus != cur_focus)
4156 break;
4157 }
4158 }
4159 break;
4160 case SWM_STACK_BELOW:
4161 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
4162 while (winfocus && ICONIC(winfocus))
4163 winfocus = TAILQ_PREV(winfocus, ws_win_list,
4164 entry);
4165
4166 if (winfocus == NULL) {
4167 if (focus_close_wrap) {
4168 TAILQ_FOREACH_REVERSE(winfocus, wl,
4169 ws_win_list, entry)
4170 if (!ICONIC(winfocus) &&
4171 winfocus != cur_focus)
4172 break;
4173 } else {
4174 TAILQ_FOREACH(winfocus, wl, entry)
4175 if (!ICONIC(winfocus) &&
4176 winfocus != cur_focus)
4177 break;
4178 }
4179 }
4180 break;
4181 }
4182 }
4183 done:
4184 if (winfocus == NULL ||
4185 (winfocus && (ICONIC(winfocus) || winfocus == cur_focus))) {
4186 if (focus_default == SWM_STACK_TOP) {
4187 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4188 if (!ICONIC(winfocus) && winfocus != cur_focus)
4189 break;
4190 } else {
4191 TAILQ_FOREACH(winfocus, wl, entry)
4192 if (!ICONIC(winfocus) && winfocus != cur_focus)
4193 break;
4194 }
4195 }
4196
4197 kill_refs(win);
4198
4199 return get_focus_magic(winfocus);
4200 }
4201
4202 struct ws_win *
4203 get_region_focus(struct swm_region *r)
4204 {
4205 struct ws_win *winfocus = NULL;
4206
4207 if (!(r && r->ws))
4208 return NULL;
4209
4210 if (r->ws->focus && !ICONIC(r->ws->focus))
4211 winfocus = r->ws->focus;
4212 else if (r->ws->focus_prev && !ICONIC(r->ws->focus_prev))
4213 winfocus = r->ws->focus_prev;
4214 else
4215 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
4216 if (!ICONIC(winfocus))
4217 break;
4218
4219 return get_focus_magic(winfocus);
4220 }
4221
4222 void
4223 focus(struct swm_region *r, union arg *args)
4224 {
4225 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
4226 struct ws_win_list *wl = NULL;
4227 struct workspace *ws = NULL;
4228 union arg a;
4229 int i;
4230 xcb_icccm_wm_hints_t hints;
4231
4232 if (!(r && r->ws))
4233 goto out;
4234
4235 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
4236
4237 cur_focus = r->ws->focus;
4238 ws = r->ws;
4239 wl = &ws->winlist;
4240
4241 /* Make sure an uniconified window has focus, if one exists. */
4242 if (cur_focus == NULL) {
4243 cur_focus = TAILQ_FIRST(wl);
4244 while (cur_focus != NULL && ICONIC(cur_focus))
4245 cur_focus = TAILQ_NEXT(cur_focus, entry);
4246 }
4247
4248 switch (args->id) {
4249 case SWM_ARG_ID_FOCUSPREV:
4250 if (cur_focus == NULL)
4251 goto out;
4252
4253 winfocus = cur_focus;
4254 do {
4255 winfocus = TAILQ_PREV(winfocus, ws_win_list, entry);
4256 if (winfocus == NULL)
4257 winfocus = TAILQ_LAST(wl, ws_win_list);
4258 if (winfocus == cur_focus)
4259 break;
4260 } while (winfocus != NULL &&
4261 (ICONIC(winfocus) || winfocus->id == cur_focus->transient));
4262 break;
4263 case SWM_ARG_ID_FOCUSNEXT:
4264 if (cur_focus == NULL)
4265 goto out;
4266
4267 winfocus = cur_focus;
4268 do {
4269 winfocus = TAILQ_NEXT(winfocus, entry);
4270 if (winfocus == NULL)
4271 winfocus = TAILQ_FIRST(wl);
4272 if (winfocus == cur_focus)
4273 break;
4274 } while (winfocus != NULL &&
4275 (ICONIC(winfocus) || winfocus->id == cur_focus->transient));
4276 break;
4277 case SWM_ARG_ID_FOCUSMAIN:
4278 if (cur_focus == NULL)
4279 goto out;
4280
4281 winfocus = TAILQ_FIRST(wl);
4282 if (winfocus == cur_focus)
4283 winfocus = cur_focus->ws->focus_prev;
4284 break;
4285 case SWM_ARG_ID_FOCUSURGENT:
4286 /* Search forward for the next urgent window. */
4287 winfocus = NULL;
4288 head = cur_focus;
4289
4290 for (i = 0; i <= workspace_limit; ++i) {
4291 if (head == NULL)
4292 head = TAILQ_FIRST(&r->s->ws[(ws->idx + i) %
4293 workspace_limit].winlist);
4294
4295 while (head != NULL &&
4296 (head = TAILQ_NEXT(head, entry)) != NULL) {
4297 if (head == cur_focus) {
4298 winfocus = cur_focus;
4299 break;
4300 }
4301 if (xcb_icccm_get_wm_hints_reply(conn,
4302 xcb_icccm_get_wm_hints(conn, head->id),
4303 &hints, NULL) != 0 &&
4304 xcb_icccm_wm_hints_get_urgency(&hints)) {
4305 winfocus = head;
4306 break;
4307 }
4308 }
4309
4310 if (winfocus != NULL)
4311 break;
4312 }
4313
4314 /* Switch ws if new focus is on a different ws. */
4315 if (winfocus != NULL && winfocus->ws != ws) {
4316 a.id = winfocus->ws->idx;
4317 switchws(r, &a);
4318 }
4319 break;
4320 default:
4321 goto out;
4322 }
4323
4324 if (clear_maximized(ws) > 0)
4325 stack();
4326
4327 focus_win(get_focus_magic(winfocus));
4328 focus_flush();
4329
4330 out:
4331 DNPRINTF(SWM_D_FOCUS, "focus: done\n");
4332 }
4333
4334 void
4335 cycle_layout(struct swm_region *r, union arg *args)
4336 {
4337 struct workspace *ws = r->ws;
4338
4339 /* suppress unused warning since var is needed */
4340 (void)args;
4341
4342 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
4343
4344 ws->cur_layout++;
4345 if (ws->cur_layout->l_stack == NULL)
4346 ws->cur_layout = &layouts[0];
4347
4348 clear_maximized(ws);
4349
4350 stack();
4351 bar_draw();
4352
4353 focus_win(get_region_focus(r));
4354
4355 focus_flush();
4356 }
4357
4358 void
4359 stack_config(struct swm_region *r, union arg *args)
4360 {
4361 struct workspace *ws = r->ws;
4362
4363 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
4364 args->id, ws->idx);
4365
4366 if (clear_maximized(ws) > 0)
4367 stack();
4368
4369 if (ws->cur_layout->l_config != NULL)
4370 ws->cur_layout->l_config(ws, args->id);
4371
4372 if (args->id != SWM_ARG_ID_STACKINIT)
4373 stack();
4374 bar_draw();
4375
4376 focus_flush();
4377 }
4378
4379 void
4380 stack(void) {
4381 struct swm_geometry g;
4382 struct swm_region *r, *r_prev = NULL;
4383 int i, num_screens;
4384 uint32_t val[2];
4385 #ifdef SWM_DEBUG
4386 int j;
4387 #endif
4388
4389 DNPRINTF(SWM_D_STACK, "stack: begin\n");
4390
4391 num_screens = get_screen_count();
4392 for (i = 0; i < num_screens; i++) {
4393 #ifdef SWM_DEBUG
4394 j = 0;
4395 #endif
4396 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4397 /* Adjust stack area for region bar and padding. */
4398 g = r->g;
4399 g.x += region_padding;
4400 g.y += region_padding;
4401 g.w -= 2 * border_width + 2 * region_padding;
4402 g.h -= 2 * border_width + 2 * region_padding;
4403 if (bar_enabled && r->ws->bar_enabled) {
4404 if (!bar_at_bottom)
4405 g.y += bar_height;
4406 g.h -= bar_height;
4407 }
4408
4409 DNPRINTF(SWM_D_STACK, "stack: workspace: %d (screen: "
4410 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4411 r->ws->idx, i, j++, g.x, g.y, g.w, g.h);
4412
4413 if (r_prev) {
4414 /* Stack bar/input relative to prev. region. */
4415 val[1] = XCB_STACK_MODE_ABOVE;
4416
4417 val[0] = r_prev->id;
4418 DNPRINTF(SWM_D_STACK, "stack: region input %#x "
4419 "relative to %#x.\n", r->id, val[0]);
4420 xcb_configure_window(conn, r->id,
4421 XCB_CONFIG_WINDOW_SIBLING |
4422 XCB_CONFIG_WINDOW_STACK_MODE, val);
4423
4424 val[0] = r_prev->bar->id;
4425 DNPRINTF(SWM_D_STACK, "stack: region bar %#x "
4426 "relative to %#x.\n", r->bar->id, val[0]);
4427 xcb_configure_window(conn, r->bar->id,
4428 XCB_CONFIG_WINDOW_SIBLING |
4429 XCB_CONFIG_WINDOW_STACK_MODE, val);
4430 }
4431
4432 r->ws->cur_layout->l_stack(r->ws, &g);
4433 r->ws->cur_layout->l_string(r->ws);
4434 /* save r so we can track region changes */
4435 r->ws->old_r = r;
4436 r_prev = r;
4437 }
4438 }
4439 if (font_adjusted)
4440 font_adjusted--;
4441
4442 DNPRINTF(SWM_D_STACK, "stack: end\n");
4443 }
4444
4445 void
4446 store_float_geom(struct ws_win *win)
4447 {
4448 if (win == NULL || win->ws->r == NULL)
4449 return;
4450
4451 /* retain window geom and region geom */
4452 win->g_float = win->g;
4453 win->g_float.x -= X(win->ws->r);
4454 win->g_float.y -= Y(win->ws->r);
4455 win->g_floatvalid = 1;
4456 DNPRINTF(SWM_D_MISC, "store_float_geom: win %#x, g: (%d,%d)"
4457 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
4458 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
4459 win->g_float.w, win->g_float.h);
4460 }
4461
4462 void
4463 load_float_geom(struct ws_win *win)
4464 {
4465 if (win == NULL || win->ws->r == NULL)
4466 return;
4467
4468 if (win->g_floatvalid) {
4469 win->g = win->g_float;
4470 X(win) += X(win->ws->r);
4471 Y(win) += Y(win->ws->r);
4472 DNPRINTF(SWM_D_MISC, "load_float_geom: win %#x, g: (%d,%d)"
4473 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
4474 HEIGHT(win));
4475 } else {
4476 DNPRINTF(SWM_D_MISC, "load_float_geom: win %#x, g_float "
4477 "is not set.\n", win->id);
4478 }
4479 }
4480
4481 void
4482 update_floater(struct ws_win *win)
4483 {
4484 struct workspace *ws;
4485 struct swm_region *r;
4486
4487 if (win == NULL)
4488 return;
4489
4490 ws = win->ws;
4491
4492 if ((r = ws->r) == NULL)
4493 return;
4494
4495 DNPRINTF(SWM_D_MISC, "update_floater: win %#x\n", win->id);
4496
4497 if (FULLSCREEN(win)) {
4498 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4499 if (!win->g_floatvalid)
4500 store_float_geom(win);
4501
4502 win->g = r->g;
4503 win->bordered = 0;
4504 } else if (MAXIMIZED(win)) {
4505 /* Maximize: like a single stacked window. */
4506 if (!win->g_floatvalid)
4507 store_float_geom(win);
4508
4509 win->g = r->g;
4510
4511 if (bar_enabled && ws->bar_enabled) {
4512 win->bordered = 1;
4513 if (!bar_at_bottom)
4514 Y(win) += bar_height;
4515 HEIGHT(win) -= bar_height;
4516 } else if (disable_border) {
4517 win->bordered = 0;
4518 } else {
4519 win->bordered = 1;
4520 }
4521
4522 if (win->bordered) {
4523 HEIGHT(win) -= 2 * border_width;
4524 WIDTH(win) -= 2 * border_width;
4525 }
4526 } else {
4527 /* Normal floating window. */
4528 /* Update geometry if new region. */
4529 if (r != ws->old_r)
4530 load_float_geom(win);
4531
4532 if ((win->quirks & SWM_Q_FULLSCREEN) &&
4533 WIDTH(win) >= WIDTH(r) && HEIGHT(win) >= HEIGHT(r)) {
4534 /* Remove border for FULLSCREEN quirk. */
4535 win->bordered = 0;
4536 } else if (!MANUAL(win)) {
4537 if (TRANS(win) && (win->quirks & SWM_Q_TRANSSZ)) {
4538 /* Adjust size on TRANSSZ quirk. */
4539 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
4540 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
4541 }
4542
4543 if (!(win->quirks & SWM_Q_ANYWHERE)) {
4544 /*
4545 * Floaters and transients are auto-centred
4546 * unless manually moved, resized or ANYWHERE
4547 * quirk is set.
4548 */
4549 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 -
4550 BORDER(win);
4551 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 -
4552 BORDER(win);
4553 store_float_geom(win);
4554 }
4555 }
4556 }
4557
4558 /* Ensure at least 1 pixel of the window is in the region. */
4559 region_containment(win, r, SWM_CW_ALLSIDES);
4560
4561 update_window(win);
4562 }
4563
4564 /*
4565 * Send keystrokes to terminal to decrease/increase the font size as the
4566 * window size changes.
4567 */
4568 void
4569 adjust_font(struct ws_win *win)
4570 {
4571 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
4572 ABOVE(win) || TRANS(win))
4573 return;
4574
4575 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
4576 WIDTH(win) / win->sh.width_inc < term_width &&
4577 win->font_steps < SWM_MAX_FONT_STEPS) {
4578 win->font_size_boundary[win->font_steps] =
4579 (win->sh.width_inc * term_width) + win->sh.base_width;
4580 win->font_steps++;
4581 font_adjusted++;
4582 win->last_inc = win->sh.width_inc;
4583 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
4584 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
4585 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
4586 win->font_steps--;
4587 font_adjusted++;
4588 win->last_inc = win->sh.width_inc;
4589 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
4590 }
4591 }
4592
4593 #define SWAPXY(g) do { \
4594 int tmp; \
4595 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4596 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4597 } while (0)
4598 void
4599 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
4600 {
4601 struct swm_geometry win_g, r_g = *g;
4602 struct ws_win *win;
4603 int i, j, s, stacks;
4604 int w_inc = 1, h_inc, w_base = 1, h_base;
4605 int hrh, extra = 0, h_slice, last_h = 0;
4606 int split, colno, winno, mwin, msize, mscale;
4607 int remain, missing, v_slice, reconfigure = 0;
4608 int bordered = 1;
4609
4610 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
4611 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
4612
4613 /* Prepare tiling variables, if needed. */
4614 if ((winno = count_win(ws, 0)) > 0) {
4615 /* Find first tiled window. */
4616 TAILQ_FOREACH(win, &ws->winlist, entry)
4617 if (!FLOATING(win) && !ICONIC(win))
4618 break;
4619
4620 /* Take into account size hints of first tiled window. */
4621 if (rot) {
4622 w_inc = win->sh.width_inc;
4623 w_base = win->sh.base_width;
4624 mwin = ws->l_state.horizontal_mwin;
4625 mscale = ws->l_state.horizontal_msize;
4626 stacks = ws->l_state.horizontal_stacks;
4627 SWAPXY(&r_g);
4628 } else {
4629 w_inc = win->sh.height_inc;
4630 w_base = win->sh.base_height;
4631 mwin = ws->l_state.vertical_mwin;
4632 mscale = ws->l_state.vertical_msize;
4633 stacks = ws->l_state.vertical_stacks;
4634 }
4635 win_g = r_g;
4636
4637 if (stacks > winno - mwin)
4638 stacks = winno - mwin;
4639 if (stacks < 1)
4640 stacks = 1;
4641
4642 h_slice = r_g.h / SWM_H_SLICE;
4643 if (mwin && winno > mwin) {
4644 v_slice = r_g.w / SWM_V_SLICE;
4645
4646 split = mwin;
4647 colno = split;
4648 win_g.w = v_slice * mscale;
4649
4650 if (w_inc > 1 && w_inc < v_slice) {
4651 /* Adjust for requested size increment. */
4652 remain = (win_g.w - w_base) % w_inc;
4653 win_g.w -= remain;
4654 }
4655
4656 msize = win_g.w;
4657 if (flip)
4658 win_g.x += r_g.w - msize;
4659 } else {
4660 msize = -2;
4661 colno = split = winno / stacks;
4662 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
4663 2 * border_width) / stacks);
4664 }
4665 hrh = r_g.h / colno;
4666 extra = r_g.h - (colno * hrh);
4667 win_g.h = hrh - 2 * border_width;
4668 i = j = 0, s = stacks;
4669 }
4670
4671 /* Update window geometry. */
4672 TAILQ_FOREACH(win, &ws->winlist, entry) {
4673 if (ICONIC(win))
4674 continue;
4675
4676 if (FLOATING(win)) {
4677 update_floater(win);
4678 continue;
4679 }
4680
4681 /* Tiled. */
4682 if (split && i == split) {
4683 colno = (winno - mwin) / stacks;
4684 if (s <= (winno - mwin) % stacks)
4685 colno++;
4686 split += colno;
4687 hrh = r_g.h / colno;
4688 extra = r_g.h - (colno * hrh);
4689
4690 if (!flip)
4691 win_g.x += win_g.w + 2 * border_width +
4692 tile_gap;
4693
4694 win_g.w = (r_g.w - msize -
4695 (stacks * (2 * border_width + tile_gap))) / stacks;
4696 if (s == 1)
4697 win_g.w += (r_g.w - msize -
4698 (stacks * (2 * border_width + tile_gap))) %
4699 stacks;
4700
4701 if (flip)
4702 win_g.x -= win_g.w + 2 * border_width +
4703 tile_gap;
4704 s--;
4705 j = 0;
4706 }
4707
4708 win_g.h = hrh - 2 * border_width - tile_gap;
4709
4710 if (rot) {
4711 h_inc = win->sh.width_inc;
4712 h_base = win->sh.base_width;
4713 } else {
4714 h_inc = win->sh.height_inc;
4715 h_base = win->sh.base_height;
4716 }
4717
4718 if (j == colno - 1) {
4719 win_g.h = hrh + extra;
4720 } else if (h_inc > 1 && h_inc < h_slice) {
4721 /* adjust for window's requested size increment */
4722 remain = (win_g.h - h_base) % h_inc;
4723 missing = h_inc - remain;
4724
4725 if (missing <= extra || j == 0) {
4726 extra -= missing;
4727 win_g.h += missing;
4728 } else {
4729 win_g.h -= remain;
4730 extra += remain;
4731 }
4732 }
4733
4734 if (j == 0)
4735 win_g.y = r_g.y;
4736 else
4737 win_g.y += last_h + 2 * border_width + tile_gap;
4738
4739 if (disable_border && !(bar_enabled && ws->bar_enabled) &&
4740 winno == 1){
4741 bordered = 0;
4742 win_g.w += 2 * border_width;
4743 win_g.h += 2 * border_width;
4744 } else {
4745 bordered = 1;
4746 }
4747
4748 if (rot) {
4749 if (X(win) != win_g.y || Y(win) != win_g.x ||
4750 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
4751 reconfigure = 1;
4752 X(win) = win_g.y;
4753 Y(win) = win_g.x;
4754 WIDTH(win) = win_g.h;
4755 HEIGHT(win) = win_g.w;
4756 }
4757 } else {
4758 if (X(win) != win_g.x || Y(win) != win_g.y ||
4759 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
4760 reconfigure = 1;
4761 X(win) = win_g.x;
4762 Y(win) = win_g.y;
4763 WIDTH(win) = win_g.w;
4764 HEIGHT(win) = win_g.h;
4765 }
4766 }
4767
4768 if (bordered != win->bordered) {
4769 reconfigure = 1;
4770 win->bordered = bordered;
4771 }
4772
4773 if (reconfigure) {
4774 adjust_font(win);
4775 update_window(win);
4776 }
4777
4778 last_h = win_g.h;
4779 i++;
4780 j++;
4781 }
4782
4783 /* Stack all windows from bottom up. */
4784 TAILQ_FOREACH_REVERSE(win, &ws->stack, ws_win_stack, stack_entry)
4785 if (!ICONIC(win))
4786 update_win_stacking(win);
4787
4788 /* Map all windows from top down. */
4789 TAILQ_FOREACH(win, &ws->stack, stack_entry)
4790 if (!ICONIC(win))
4791 map_window(win);
4792
4793 DNPRINTF(SWM_D_STACK, "stack_master: done\n");
4794 }
4795
4796 void
4797 vertical_config(struct workspace *ws, int id)
4798 {
4799 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
4800 id, ws->idx);
4801
4802 switch (id) {
4803 case SWM_ARG_ID_STACKRESET:
4804 case SWM_ARG_ID_STACKINIT:
4805 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
4806 ws->l_state.vertical_mwin = 1;
4807 ws->l_state.vertical_stacks = 1;
4808 break;
4809 case SWM_ARG_ID_MASTERSHRINK:
4810 if (ws->l_state.vertical_msize > 1)
4811 ws->l_state.vertical_msize--;
4812 break;
4813 case SWM_ARG_ID_MASTERGROW:
4814 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
4815 ws->l_state.vertical_msize++;
4816 break;
4817 case SWM_ARG_ID_MASTERADD:
4818 ws->l_state.vertical_mwin++;
4819 break;
4820 case SWM_ARG_ID_MASTERDEL:
4821 if (ws->l_state.vertical_mwin > 0)
4822 ws->l_state.vertical_mwin--;
4823 break;
4824 case SWM_ARG_ID_STACKINC:
4825 ws->l_state.vertical_stacks++;
4826 break;
4827 case SWM_ARG_ID_STACKDEC:
4828 if (ws->l_state.vertical_stacks > 1)
4829 ws->l_state.vertical_stacks--;
4830 break;
4831 case SWM_ARG_ID_FLIPLAYOUT:
4832 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
4833 break;
4834 default:
4835 return;
4836 }
4837 }
4838
4839 void
4840 vertical_stack(struct workspace *ws, struct swm_geometry *g)
4841 {
4842 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
4843
4844 stack_master(ws, g, 0, ws->l_state.vertical_flip);
4845 }
4846
4847 void
4848 horizontal_config(struct workspace *ws, int id)
4849 {
4850 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
4851
4852 switch (id) {
4853 case SWM_ARG_ID_STACKRESET:
4854 case SWM_ARG_ID_STACKINIT:
4855 ws->l_state.horizontal_mwin = 1;
4856 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
4857 ws->l_state.horizontal_stacks = 1;
4858 break;
4859 case SWM_ARG_ID_MASTERSHRINK:
4860 if (ws->l_state.horizontal_msize > 1)
4861 ws->l_state.horizontal_msize--;
4862 break;
4863 case SWM_ARG_ID_MASTERGROW:
4864 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
4865 ws->l_state.horizontal_msize++;
4866 break;
4867 case SWM_ARG_ID_MASTERADD:
4868 ws->l_state.horizontal_mwin++;
4869 break;
4870 case SWM_ARG_ID_MASTERDEL:
4871 if (ws->l_state.horizontal_mwin > 0)
4872 ws->l_state.horizontal_mwin--;
4873 break;
4874 case SWM_ARG_ID_STACKINC:
4875 ws->l_state.horizontal_stacks++;
4876 break;
4877 case SWM_ARG_ID_STACKDEC:
4878 if (ws->l_state.horizontal_stacks > 1)
4879 ws->l_state.horizontal_stacks--;
4880 break;
4881 case SWM_ARG_ID_FLIPLAYOUT:
4882 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
4883 break;
4884 default:
4885 return;
4886 }
4887 }
4888
4889 void
4890 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
4891 {
4892 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
4893
4894 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
4895 }
4896
4897 /* fullscreen view */
4898 void
4899 max_stack(struct workspace *ws, struct swm_geometry *g)
4900 {
4901 struct swm_geometry gg = *g;
4902 struct ws_win *w, *win = NULL, *parent = NULL;
4903 int winno;
4904
4905 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
4906
4907 if (ws == NULL)
4908 return;
4909
4910 winno = count_win(ws, 0);
4911 if (winno == 0 && count_win(ws, 1) == 0)
4912 return;
4913
4914 /* Figure out which top level window should be visible. */
4915 if (ws->focus_pending)
4916 win = ws->focus_pending;
4917 else if (ws->focus)
4918 win = ws->focus;
4919 else if (ws->focus_prev)
4920 win = ws->focus_prev;
4921 else
4922 win = TAILQ_FIRST(&ws->winlist);
4923
4924 DNPRINTF(SWM_D_STACK, "max_stack: focus_pending: %#x, focus: %#x, "
4925 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws->focus_pending),
4926 WINID(ws->focus), WINID(ws->focus_prev),
4927 WINID(TAILQ_FIRST(&ws->winlist)), win->id);
4928
4929 /* Update window geometry. */
4930 TAILQ_FOREACH(w, &ws->winlist, entry) {
4931 if (ICONIC(w))
4932 continue;
4933
4934 if (TRANS(w)) {
4935 update_floater(w);
4936 continue;
4937 }
4938
4939 /* Set maximized flag for all maxed windows. */
4940 if (!MAXIMIZED(w)) {
4941 /* Preserve floating geometry. */
4942 if (ABOVE(w))
4943 store_float_geom(w);
4944
4945 ewmh_apply_flags(w, w->ewmh_flags | EWMH_F_MAXIMIZED);
4946 ewmh_update_wm_state(w);
4947 }
4948
4949 /* Only reconfigure if necessary. */
4950 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4951 HEIGHT(w) != gg.h) {
4952 w->g = gg;
4953 if (bar_enabled && ws->bar_enabled){
4954 w->bordered = 1;
4955 } else {
4956 w->bordered = 0;
4957 WIDTH(w) += 2 * border_width;
4958 HEIGHT(w) += 2 * border_width;
4959 }
4960
4961 update_window(w);
4962 }
4963 }
4964
4965 if (TRANS(win)) {
4966 parent = find_window(win->transient);
4967 raise_window(parent);
4968
4969 TAILQ_FOREACH(w, &ws->stack, stack_entry)
4970 if (w->transient == parent->id)
4971 raise_window(w);
4972 }
4973
4974 raise_window(win);
4975
4976 TAILQ_FOREACH(w, &ws->stack, stack_entry)
4977 if (w->transient == win->id)
4978 raise_window(w);
4979
4980 /* Map all windows. */
4981 TAILQ_FOREACH(w, &ws->stack, stack_entry)
4982 if (!ICONIC(w))
4983 map_window(w);
4984 }
4985
4986 void
4987 send_to_rg(struct swm_region *r, union arg *args)
4988 {
4989 int ridx = args->id, i, num_screens;
4990 struct swm_region *rr = NULL;
4991 union arg a;
4992
4993 num_screens = get_screen_count();
4994 /* do nothing if we don't have more than one screen */
4995 if (!(num_screens > 1 || outputs > 1))
4996 return;
4997
4998 DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
4999
5000 rr = TAILQ_FIRST(&r->s->rl);
5001 for (i = 0; i < ridx && rr != NULL; ++i)
5002 rr = TAILQ_NEXT(rr, entry);
5003
5004 if (rr == NULL)
5005 return;
5006
5007 a.id = rr->ws->idx;
5008
5009 send_to_ws(r, &a);
5010 }
5011
5012 struct swm_region *
5013 region_under(struct swm_screen *s, int x, int y)
5014 {
5015 struct swm_region *r = NULL;
5016
5017 TAILQ_FOREACH(r, &s->rl, entry) {
5018 DNPRINTF(SWM_D_MISC, "region_under: ws: %d, region g: (%d,%d) "
5019 "%d x %d, coords: (%d,%d)\n", r->ws->idx, X(r), Y(r),
5020 WIDTH(r), HEIGHT(r), x, y);
5021 if (X(r) <= x && x < MAX_X(r))
5022 if (Y(r) <= y && y < MAX_Y(r))
5023 return (r);
5024 }
5025
5026 return (NULL);
5027 }
5028
5029 /* Transfer focused window to target workspace and focus. */
5030 void
5031 send_to_ws(struct swm_region *r, union arg *args)
5032 {
5033 int wsid = args->id;
5034 struct ws_win *win = NULL;
5035
5036 if (r && r->ws && r->ws->focus)
5037 win = r->ws->focus;
5038 else
5039 return;
5040
5041 DNPRINTF(SWM_D_MOVE, "send_to_ws: win %#x, ws %d\n", win->id, wsid);
5042
5043 if (wsid < 0 || wsid >= workspace_limit)
5044 return;
5045
5046 if (win->ws->idx == wsid)
5047 return;
5048
5049 win_to_ws(win, wsid, 1);
5050
5051 /* Set window to be focus on target ws. */
5052 if (focus_mode != SWM_FOCUS_FOLLOW) {
5053 win->ws->focus = win;
5054 win->ws->focus_pending = NULL;
5055 }
5056
5057 DNPRINTF(SWM_D_STACK, "send_to_ws: focus_pending: %#x, focus: %#x, "
5058 "focus_prev: %#x, first: %#x, win: %#x\n",
5059 WINID(r->ws->focus_pending), WINID(r->ws->focus),
5060 WINID(r->ws->focus_prev), WINID(TAILQ_FIRST(&r->ws->winlist)),
5061 win->id);
5062
5063 ewmh_apply_flags(win, win->ewmh_flags & ~EWMH_F_MAXIMIZED);
5064 ewmh_update_wm_state(win);
5065
5066 /* Restack and set new focus. */
5067 if (FLOATING(win))
5068 load_float_geom(win);
5069
5070 stack();
5071
5072 if (focus_mode != SWM_FOCUS_FOLLOW) {
5073 if (r->ws->focus != NULL) {
5074 focus_win(r->ws->focus);
5075 } else {
5076 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
5077 XCB_CURRENT_TIME);
5078 bar_draw();
5079 }
5080 }
5081
5082 focus_flush();
5083 }
5084
5085 void
5086 win_to_ws(struct ws_win *win, int wsid, int unfocus)
5087 {
5088 struct ws_win *parent;
5089 struct workspace *ws, *nws, *pws;
5090
5091 if (wsid < 0 || wsid >= workspace_limit)
5092 return;
5093
5094 if (win->ws->idx == wsid)
5095 return;
5096
5097 ws = win->ws;
5098 nws = &win->s->ws[wsid];
5099
5100 DNPRINTF(SWM_D_MOVE, "win_to_ws: win %#x, ws %d -> %d\n", win->id,
5101 ws->idx, wsid);
5102
5103 /* Cleanup focus on source ws. */
5104 if (focus_mode != SWM_FOCUS_FOLLOW &&
5105 (ws->focus == win || ws->focus_pending == win))
5106 ws->focus_pending = get_focus_prev(win);
5107
5108 /* Move the parent if this is a transient window. */
5109 if (TRANS(win)) {
5110 parent = find_window(win->transient);
5111 if (parent) {
5112 pws = parent->ws;
5113 /* Set new focus in parent's ws if needed. */
5114 if (pws->focus == parent) {
5115 if (focus_mode != SWM_FOCUS_FOLLOW)
5116 pws->focus_pending =
5117 get_focus_prev(parent);
5118
5119 unfocus_win(parent);
5120
5121 if (focus_mode != SWM_FOCUS_FOLLOW) {
5122 pws->focus = pws->focus_pending;
5123 pws->focus_pending = NULL;
5124 }
5125 }
5126
5127 /* Don't unmap parent if new ws is visible */
5128 if (nws->r == NULL)
5129 unmap_window(parent);
5130
5131 /* Transfer */
5132 TAILQ_REMOVE(&ws->winlist, parent, entry);
5133 TAILQ_REMOVE(&ws->stack, parent, stack_entry);
5134 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
5135 TAILQ_INSERT_TAIL(&nws->stack, parent, stack_entry);
5136 parent->ws = nws;
5137
5138 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: "
5139 "_NET_WM_DESKTOP: %d\n", wsid);
5140 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5141 parent->id, ewmh[_NET_WM_DESKTOP].atom,
5142 XCB_ATOM_CARDINAL, 32, 1, &wsid);
5143 }
5144 }
5145
5146 if (unfocus)
5147 unfocus_win(win);
5148
5149 if (ws->focus_prev == win)
5150 ws->focus_prev = NULL;
5151
5152 if (focus_mode != SWM_FOCUS_FOLLOW && ws->focus_pending != NULL) {
5153 ws->focus = ws->focus_pending;
5154 ws->focus_pending = NULL;
5155 }
5156
5157 /* Don't unmap if new ws is visible */
5158 if (nws->r == NULL)
5159 unmap_window(win);
5160
5161 /* Transfer */
5162 TAILQ_REMOVE(&ws->winlist, win, entry);
5163 TAILQ_REMOVE(&ws->stack, win, stack_entry);
5164 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
5165 TAILQ_INSERT_TAIL(&nws->stack, win, stack_entry);
5166 win->ws = nws;
5167
5168 /* Update the window's workspace property: _NET_WM_DESKTOP */
5169 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: "
5170 "_NET_WM_DESKTOP: %d\n", wsid);
5171 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
5172 ewmh[_NET_WM_DESKTOP].atom, XCB_ATOM_CARDINAL, 32, 1, &wsid);
5173
5174 ewmh_update_client_list();
5175
5176 DNPRINTF(SWM_D_MOVE, "win_to_ws: done.\n");
5177 }
5178
5179 void
5180 pressbutton(struct swm_region *r, union arg *args)
5181 {
5182 /* suppress unused warning since var is needed */
5183 (void)r;
5184
5185 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
5186 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5187 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
5188 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5189 }
5190
5191 void
5192 raise_toggle(struct swm_region *r, union arg *args)
5193 {
5194 /* suppress unused warning since var is needed */
5195 (void)args;
5196
5197 if (r == NULL || r->ws == NULL)
5198 return;
5199
5200 if (r->ws->focus && MAXIMIZED(r->ws->focus))
5201 return;
5202
5203 r->ws->always_raise = !r->ws->always_raise;
5204
5205 /* bring floaters back to top */
5206 if (!r->ws->always_raise)
5207 stack();
5208
5209 focus_flush();
5210 }
5211
5212 void
5213 iconify(struct swm_region *r, union arg *args)
5214 {
5215 struct ws_win *w;
5216 /* suppress unused warning since var is needed */
5217 (void)args;
5218
5219 if ((w = r->ws->focus) == NULL)
5220 return;
5221
5222 ewmh_apply_flags(w, w->ewmh_flags | EWMH_F_HIDDEN);
5223 ewmh_update_wm_state(w);
5224
5225 stack();
5226
5227 focus_flush();
5228 }
5229
5230 char *
5231 get_win_name(xcb_window_t win)
5232 {
5233 char *name = NULL;
5234 xcb_get_property_cookie_t c;
5235 xcb_get_property_reply_t *r;
5236
5237 /* First try _NET_WM_NAME for UTF-8. */
5238 c = xcb_get_property(conn, 0, win, ewmh[_NET_WM_NAME].atom,
5239 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5240 r = xcb_get_property_reply(conn, c, NULL);
5241 if (r && r->type == XCB_NONE) {
5242 free(r);
5243 /* Use WM_NAME instead; no UTF-8. */
5244 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
5245 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5246 r = xcb_get_property_reply(conn, c, NULL);
5247 }
5248
5249 if (r && r->type != XCB_NONE && r->length > 0)
5250 name = strndup(xcb_get_property_value(r),
5251 xcb_get_property_value_length(r));
5252 else
5253 name = strdup("");
5254
5255 if (name == NULL)
5256 err(1, "get_win_name: failed to allocate memory.");
5257
5258 free(r);
5259
5260 return (name);
5261 }
5262
5263 void
5264 uniconify(struct swm_region *r, union arg *args)
5265 {
5266 struct ws_win *win;
5267 FILE *lfile;
5268 char *name;
5269 int count = 0;
5270
5271 DNPRINTF(SWM_D_MISC, "uniconify\n");
5272
5273 if (r == NULL || r->ws == NULL)
5274 return;
5275
5276 /* make sure we have anything to uniconify */
5277 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5278 if (win->ws == NULL)
5279 continue; /* should never happen */
5280 if (!ICONIC(win))
5281 continue;
5282 count++;
5283 }
5284
5285 DNPRINTF(SWM_D_MISC, "uniconify: count: %d\n", count);
5286
5287 if (count == 0)
5288 return;
5289
5290 search_r = r;
5291 search_resp_action = SWM_SEARCH_UNICONIFY;
5292
5293 spawn_select(r, args, "search", &searchpid);
5294
5295 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5296 return;
5297
5298 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5299 if (win->ws == NULL)
5300 continue; /* should never happen */
5301 if (!ICONIC(win))
5302 continue;
5303
5304 name = get_win_name(win->id);
5305 fprintf(lfile, "%s.%u\n", name, win->id);
5306 free(name);
5307 }
5308
5309 fclose(lfile);
5310 }
5311
5312 void
5313 name_workspace(struct swm_region *r, union arg *args)
5314 {
5315 FILE *lfile;
5316
5317 DNPRINTF(SWM_D_MISC, "name_workspace\n");
5318
5319 if (r == NULL)
5320 return;
5321
5322 search_r = r;
5323 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
5324
5325 spawn_select(r, args, "name_workspace", &searchpid);
5326
5327 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5328 return;
5329
5330 fprintf(lfile, "%s", "");
5331 fclose(lfile);
5332 }
5333
5334 void
5335 search_workspace(struct swm_region *r, union arg *args)
5336 {
5337 int i;
5338 struct workspace *ws;
5339 FILE *lfile;
5340
5341 DNPRINTF(SWM_D_MISC, "search_workspace\n");
5342
5343 if (r == NULL)
5344 return;
5345
5346 search_r = r;
5347 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
5348
5349 spawn_select(r, args, "search", &searchpid);
5350
5351 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5352 return;
5353
5354 for (i = 0; i < workspace_limit; i++) {
5355 ws = &r->s->ws[i];
5356 if (ws == NULL)
5357 continue;
5358 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
5359 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
5360 }
5361
5362 fclose(lfile);
5363 }
5364
5365 void
5366 search_win_cleanup(void)
5367 {
5368 struct search_window *sw = NULL;
5369
5370 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
5371 xcb_destroy_window(conn, sw->indicator);
5372 TAILQ_REMOVE(&search_wl, sw, entry);
5373 free(sw);
5374 }
5375 }
5376
5377 void
5378 search_win(struct swm_region *r, union arg *args)
5379 {
5380 struct ws_win *win = NULL;
5381 struct search_window *sw = NULL;
5382 xcb_window_t w;
5383 uint32_t wa[2];
5384 int i, width, height;
5385 char s[8];
5386 FILE *lfile;
5387 size_t len;
5388 XftDraw *draw;
5389 XGlyphInfo info;
5390 GC l_draw;
5391 XGCValues l_gcv;
5392 XRectangle l_ibox, l_lbox;
5393
5394 DNPRINTF(SWM_D_MISC, "search_win\n");
5395
5396 search_r = r;
5397 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
5398
5399 spawn_select(r, args, "search", &searchpid);
5400
5401 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5402 return;
5403
5404 TAILQ_INIT(&search_wl);
5405
5406 i = 1;
5407 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5408 if (ICONIC(win))
5409 continue;
5410
5411 sw = calloc(1, sizeof(struct search_window));
5412 if (sw == NULL) {
5413 warn("search_win: calloc");
5414 fclose(lfile);
5415 search_win_cleanup();
5416 return;
5417 }
5418 sw->idx = i;
5419 sw->win = win;
5420
5421 snprintf(s, sizeof s, "%d", i);
5422 len = strlen(s);
5423
5424 w = xcb_generate_id(conn);
5425 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
5426 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
5427
5428 if (bar_font_legacy) {
5429 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
5430 width = l_lbox.width + 4;
5431 height = bar_fs_extents->max_logical_extent.height + 4;
5432 } else {
5433 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
5434 &info);
5435 width = info.width + 4;
5436 height = bar_font->height + 4;
5437 }
5438
5439 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
5440 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
5441 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
5442 XCB_CW_BORDER_PIXEL, wa);
5443
5444 xcb_map_window(conn, w);
5445
5446 sw->indicator = w;
5447 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
5448
5449 if (bar_font_legacy) {
5450 l_gcv.graphics_exposures = 0;
5451 l_draw = XCreateGC(display, w, 0, &l_gcv);
5452
5453 XSetForeground(display, l_draw,
5454 r->s->c[SWM_S_COLOR_BAR].pixel);
5455
5456 DRAWSTRING(display, w, bar_fs, l_draw, 2,
5457 (bar_fs_extents->max_logical_extent.height -
5458 l_lbox.height) / 2 - l_lbox.y, s, len);
5459
5460 XFreeGC(display, l_draw);
5461 } else {
5462
5463 draw = XftDrawCreate(display, w,
5464 DefaultVisual(display, r->s->idx),
5465 DefaultColormap(display, r->s->idx));
5466
5467 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
5468 (HEIGHT(r->bar) + bar_font->height) / 2 -
5469 bar_font->descent, (FcChar8 *)s, len);
5470
5471 XftDrawDestroy(draw);
5472 }
5473
5474 DNPRINTF(SWM_D_MISC, "search_win: mapped win %#x\n", w);
5475
5476 fprintf(lfile, "%d\n", i);
5477 i++;
5478 }
5479
5480 fclose(lfile);
5481
5482 xcb_flush(conn);
5483 }
5484
5485 void
5486 search_resp_uniconify(const char *resp, size_t len)
5487 {
5488 char *name;
5489 struct ws_win *win;
5490 char *s;
5491
5492 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
5493
5494 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
5495 if (!ICONIC(win))
5496 continue;
5497 name = get_win_name(win->id);
5498 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
5499 free(name);
5500 continue;
5501 }
5502 free(name);
5503 if (strncmp(s, resp, len) == 0) {
5504 /* XXX this should be a callback to generalize */
5505 ewmh_apply_flags(win, win->ewmh_flags & ~EWMH_F_HIDDEN);
5506 ewmh_update_wm_state(win);
5507 stack();
5508 free(s);
5509 break;
5510 }
5511 free(s);
5512 }
5513 }
5514
5515 void
5516 search_resp_name_workspace(const char *resp, size_t len)
5517 {
5518 struct workspace *ws;
5519
5520 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
5521
5522 if (search_r->ws == NULL)
5523 return;
5524 ws = search_r->ws;
5525
5526 if (ws->name) {
5527 free(search_r->ws->name);
5528 search_r->ws->name = NULL;
5529 }
5530
5531 if (len > 1) {
5532 ws->name = strdup(resp);
5533 if (ws->name == NULL) {
5534 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
5535 "strdup: %s", strerror(errno));
5536 return;
5537 }
5538
5539 ewmh_update_desktop_names();
5540 ewmh_get_desktop_names();
5541 }
5542 }
5543
5544 void
5545 ewmh_update_desktop_names(void)
5546 {
5547 char *name_list = NULL, *p;
5548 int num_screens, i, j, len = 0, tot = 0;
5549
5550 num_screens = get_screen_count();
5551 for (i = 0; i < num_screens; ++i) {
5552 for (j = 0; j < workspace_limit; ++j) {
5553 if (screens[i].ws[j].name != NULL)
5554 len += strlen(screens[i].ws[j].name);
5555 ++len;
5556 }
5557
5558 if((name_list = calloc(sizeof(char *), len)) == NULL)
5559 err(1, "update_desktop_names: calloc: failed to "
5560 "allocate memory.");
5561
5562 p = name_list;
5563 for (j = 0; j < workspace_limit; ++j) {
5564 if (screens[i].ws[j].name != NULL) {
5565 len = strlen(screens[i].ws[j].name);
5566 memcpy(p, screens[i].ws[j].name, len);
5567 } else {
5568 len = 0;
5569 }
5570
5571 p += len + 1;
5572 tot += len + 1;
5573 }
5574
5575 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5576 screens[i].root, ewmh[_NET_DESKTOP_NAMES].atom,
5577 a_utf8_string, 8, tot, name_list);
5578
5579 free(name_list);
5580 name_list = NULL;
5581 }
5582
5583 free(name_list);
5584 }
5585
5586 void
5587 ewmh_get_desktop_names(void)
5588 {
5589 char *names = NULL;
5590 xcb_get_property_cookie_t c;
5591 xcb_get_property_reply_t *r;
5592 int num_screens, i, j, n, k;
5593
5594 num_screens = get_screen_count();
5595 for (i = 0; i < num_screens; ++i) {
5596 for (j = 0; j < workspace_limit; ++j) {
5597 free(screens[i].ws[j].name);
5598 screens[i].ws[j].name = NULL;
5599 }
5600
5601 c = xcb_get_property(conn, 0, screens[i].root,
5602 ewmh[_NET_DESKTOP_NAMES].atom,
5603 a_utf8_string, 0, UINT32_MAX);
5604 r = xcb_get_property_reply(conn, c, NULL);
5605 if (r == NULL)
5606 continue;
5607
5608 names = xcb_get_property_value(r);
5609 n = xcb_get_property_value_length(r);
5610
5611 for (j = 0, k = 0; j < n; ++j) {
5612 if (*(names + j) != '\0') {
5613 screens[i].ws[k].name = strdup(names + j);
5614 j += strlen(names + j);
5615 }
5616 ++k;
5617 }
5618 free(r);
5619 }
5620 }
5621
5622 void
5623 ewmh_update_client_list(void)
5624 {
5625 struct ws_win *win;
5626 int num_screens, i, j, k = 0, count = 0;
5627 xcb_window_t *wins;
5628
5629 num_screens = get_screen_count();
5630 for (i = 0; i < num_screens; ++i) {
5631 for (j = 0; j < workspace_limit; ++j)
5632 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
5633 ++count;
5634
5635 DNPRINTF(SWM_D_PROP, "ewmh_update_client_list: win count: %d\n",
5636 count);
5637
5638 if (count == 0)
5639 continue;
5640
5641 wins = calloc(sizeof(xcb_window_t), count);
5642 if (wins == NULL)
5643 err(1, "ewmh_update_client_list: calloc: failed to "
5644 "allocate memory.");
5645
5646 for (j = 0, k = 0; j < workspace_limit; ++j)
5647 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
5648 wins[k++] = win->id;
5649
5650 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5651 screens[i].root, ewmh[_NET_CLIENT_LIST].atom,
5652 XCB_ATOM_WINDOW, 32, count, wins);
5653
5654 free(wins);
5655 }
5656 }
5657
5658 void
5659 ewmh_update_current_desktop(void)
5660 {
5661 int num_screens, i;
5662
5663 num_screens = get_screen_count();
5664 for (i = 0; i < num_screens; ++i)
5665 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5666 screens[i].root, ewmh[_NET_CURRENT_DESKTOP].atom,
5667 XCB_ATOM_CARDINAL, 32, 1, &screens[i].r_focus->ws->idx);
5668 }
5669
5670 void
5671 ewmh_update_desktops(void)
5672 {
5673 int num_screens, i, j;
5674 uint32_t *vals;
5675
5676 vals = calloc(sizeof(uint32_t), workspace_limit * 2);
5677 if (vals == NULL)
5678 err(1, "ewmh_update_desktops: calloc: failed to allocate "
5679 "memory.");
5680
5681 num_screens = get_screen_count();
5682 for (i = 0; i < num_screens; i++) {
5683 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5684 screens[i].root, ewmh[_NET_NUMBER_OF_DESKTOPS].atom,
5685 XCB_ATOM_CARDINAL, 32, 1, &workspace_limit);
5686
5687 for (j = 0; j < workspace_limit; ++j) {
5688 if (screens[i].ws[j].r != NULL) {
5689 vals[j * 2] = X(screens[i].ws[j].r);
5690 vals[j * 2 + 1] = Y(screens[i].ws[j].r);
5691 } else if (screens[i].ws[j].old_r != NULL) {
5692 vals[j * 2] = X(screens[i].ws[j].old_r);
5693 vals[j * 2 + 1] = Y(screens[i].ws[j].old_r);
5694 } else {
5695 vals[j * 2] = vals[j * 2 + 1] = 0;
5696 }
5697 }
5698
5699 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5700 screens[i].root, ewmh[_NET_DESKTOP_VIEWPORT].atom,
5701 XCB_ATOM_CARDINAL, 32, workspace_limit * 2, vals);
5702 }
5703
5704 free(vals);
5705 }
5706
5707 void
5708 search_resp_search_workspace(const char *resp)
5709 {
5710 char *p, *q;
5711 int ws_idx;
5712 const char *errstr;
5713 union arg a;
5714
5715 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
5716
5717 q = strdup(resp);
5718 if (q == NULL) {
5719 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
5720 strerror(errno));
5721 return;
5722 }
5723 p = strchr(q, ':');
5724 if (p != NULL)
5725 *p = '\0';
5726 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
5727 if (errstr) {
5728 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
5729 errstr, q);
5730 free(q);
5731 return;
5732 }
5733 free(q);
5734 a.id = ws_idx - 1;
5735 switchws(search_r, &a);
5736 }
5737
5738 void
5739 search_resp_search_window(const char *resp)
5740 {
5741 char *s;
5742 int idx;
5743 const char *errstr;
5744 struct search_window *sw;
5745
5746 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
5747
5748 s = strdup(resp);
5749 if (s == NULL) {
5750 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
5751 strerror(errno));
5752 return;
5753 }
5754
5755 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
5756 if (errstr) {
5757 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
5758 errstr, s);
5759 free(s);
5760 return;
5761 }
5762 free(s);
5763
5764 TAILQ_FOREACH(sw, &search_wl, entry)
5765 if (idx == sw->idx) {
5766 focus_win(sw->win);
5767 break;
5768 }
5769 }
5770
5771 #define MAX_RESP_LEN 1024
5772
5773 void
5774 search_do_resp(void)
5775 {
5776 ssize_t rbytes;
5777 char *resp;
5778 size_t len;
5779
5780 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
5781
5782 search_resp = 0;
5783 searchpid = 0;
5784
5785 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
5786 warn("search: calloc");
5787 goto done;
5788 }
5789
5790 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
5791 if (rbytes <= 0) {
5792 warn("search: read error");
5793 goto done;
5794 }
5795 resp[rbytes] = '\0';
5796
5797 /* XXX:
5798 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5799 * newline, so work around that by sanitizing the resp now.
5800 */
5801 resp[strcspn(resp, "\n")] = '\0';
5802 len = strlen(resp);
5803
5804 switch (search_resp_action) {
5805 case SWM_SEARCH_UNICONIFY:
5806 search_resp_uniconify(resp, len);
5807 break;
5808 case SWM_SEARCH_NAME_WORKSPACE:
5809 search_resp_name_workspace(resp, len);
5810 break;
5811 case SWM_SEARCH_SEARCH_WORKSPACE:
5812 search_resp_search_workspace(resp);
5813 break;
5814 case SWM_SEARCH_SEARCH_WINDOW:
5815 search_resp_search_window(resp);
5816 break;
5817 }
5818
5819 done:
5820 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
5821 search_win_cleanup();
5822
5823 search_resp_action = SWM_SEARCH_NONE;
5824 close(select_resp_pipe[0]);
5825 free(resp);
5826
5827 xcb_flush(conn);
5828 }
5829
5830 void
5831 wkill(struct swm_region *r, union arg *args)
5832 {
5833 DNPRINTF(SWM_D_MISC, "wkill: win %#x, id: %d\n", WINID(r->ws->focus),
5834 args->id);
5835
5836 if (r->ws->focus == NULL)
5837 return;
5838
5839 if (args->id == SWM_ARG_ID_KILLWINDOW)
5840 xcb_kill_client(conn, r->ws->focus->id);
5841 else
5842 if (r->ws->focus->can_delete)
5843 client_msg(r->ws->focus, a_delete, 0);
5844
5845 focus_flush();
5846 }
5847
5848 int
5849 clear_maximized(struct workspace *ws)
5850 {
5851 struct ws_win *w;
5852 int count = 0;
5853
5854 /* Clear any maximized win(s) on ws, from bottom up. */
5855 TAILQ_FOREACH_REVERSE(w, &ws->stack, ws_win_stack, stack_entry)
5856 if (MAXIMIZED(w)) {
5857 ewmh_apply_flags(w, w->ewmh_flags & ~EWMH_F_MAXIMIZED);
5858 ewmh_update_wm_state(w);
5859 ++count;
5860 }
5861
5862 return count;
5863 }
5864
5865 void
5866 maximize_toggle(struct swm_region *r, union arg *args)
5867 {
5868 struct ws_win *w = r->ws->focus;
5869
5870 /* suppress unused warning since var is needed */
5871 (void)args;
5872
5873 if (w == NULL)
5874 return;
5875
5876 DNPRINTF(SWM_D_MISC, "maximize_toggle: win %#x\n", w->id);
5877
5878 if (FULLSCREEN(w))
5879 return;
5880
5881 if (w->ws->cur_layout == &layouts[SWM_MAX_STACK])
5882 return;
5883
5884 ewmh_apply_flags(w, w->ewmh_flags ^ EWMH_F_MAXIMIZED);
5885 ewmh_update_wm_state(w);
5886
5887 stack();
5888
5889 if (w == w->ws->focus)
5890 focus_win(w);
5891
5892 focus_flush();
5893 DNPRINTF(SWM_D_MISC, "maximize_toggle: done\n");
5894 }
5895
5896 void
5897 floating_toggle(struct swm_region *r, union arg *args)
5898 {
5899 struct ws_win *w = r->ws->focus;
5900
5901 /* suppress unused warning since var is needed */
5902 (void)args;
5903
5904 if (w == NULL)
5905 return;
5906
5907 DNPRINTF(SWM_D_MISC, "floating_toggle: win %#x\n", w->id);
5908
5909 if (FULLSCREEN(w) || TRANS(w))
5910 return;
5911
5912 if (w->ws->cur_layout == &layouts[SWM_MAX_STACK])
5913 return;
5914
5915 ewmh_apply_flags(w, w->ewmh_flags ^ EWMH_F_ABOVE);
5916 ewmh_update_wm_state(w);
5917
5918 stack();
5919
5920 if (w == w->ws->focus)
5921 focus_win(w);
5922
5923 focus_flush();
5924 DNPRINTF(SWM_D_MISC, "floating_toggle: done\n");
5925 }
5926
5927 void
5928 region_containment(struct ws_win *win, struct swm_region *r, int opts)
5929 {
5930 struct swm_geometry g = r->g;
5931 int rt, lt, tp, bm, bw;
5932
5933 bw = (opts & SWM_CW_SOFTBOUNDARY) ? boundary_width : 0;
5934
5935 /*
5936 * Perpendicular distance of each side of the window to the respective
5937 * side of the region boundary. Positive values indicate the side of
5938 * the window has passed beyond the region boundary.
5939 */
5940 rt = opts & SWM_CW_RIGHT ? MAX_X(win) + BORDER(win) - MAX_X(r) : bw;
5941 lt = opts & SWM_CW_LEFT ? X(r) - X(win) + BORDER(win) : bw;
5942 bm = opts & SWM_CW_BOTTOM ? MAX_Y(win) + BORDER(win) - MAX_Y(r) : bw;
5943 tp = opts & SWM_CW_TOP ? Y(r) - Y(win) + BORDER(win) : bw;
5944
5945 DNPRINTF(SWM_D_MISC, "region_containment: win %#x, rt: %d, lt: %d, "
5946 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win->id, rt,
5947 lt, bm, tp, YESNO(opts & SWM_CW_SOFTBOUNDARY),
5948 YESNO(opts & SWM_CW_HARDBOUNDARY));
5949
5950 /*
5951 * Disable containment if any of the flagged sides went beyond the
5952 * containment boundary, or if containment is disabled.
5953 */
5954 if (!(opts & SWM_CW_HARDBOUNDARY || opts & SWM_CW_SOFTBOUNDARY) ||
5955 (bw != 0 && ((rt > bw) || (lt > bw) || (bm > bw) || (tp > bw)))) {
5956 /* Make sure window has at least 1 pixel in the region */
5957 g.x += 1 - WIDTH(win);
5958 g.y += 1 - HEIGHT(win);
5959 g.w += 2 * WIDTH(win) - 2;
5960 g.h += 2 * HEIGHT(win) - 2;
5961 }
5962
5963 constrain_window(win, &g, &opts);
5964 }
5965
5966 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
5967 void
5968 constrain_window(struct ws_win *win, struct swm_geometry *b, int *opts)
5969 {
5970 DNPRINTF(SWM_D_MISC, "constrain_window: win %#x, (x,y) w x h: "
5971 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
5972 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win->id, X(win),
5973 Y(win), WIDTH(win), HEIGHT(win), b->x, b->y, b->w, b->h,
5974 YESNO(*opts & SWM_CW_RIGHT), YESNO(*opts & SWM_CW_LEFT),
5975 YESNO(*opts & SWM_CW_BOTTOM), YESNO(*opts & SWM_CW_TOP),
5976 YESNO(*opts & SWM_CW_RESIZABLE));
5977
5978 if ((*opts & SWM_CW_RIGHT) && MAX_X(win) + BORDER(win) > b->x + b->w) {
5979 if (*opts & SWM_CW_RESIZABLE)
5980 WIDTH(win) = b->x + b->w - X(win) - BORDER(win);
5981 else
5982 X(win) = b->x + b->w - WIDTH(win) - BORDER(win);
5983 }
5984
5985 if ((*opts & SWM_CW_LEFT) && X(win) + BORDER(win) < b->x) {
5986 if (*opts & SWM_CW_RESIZABLE)
5987 WIDTH(win) -= b->x - X(win) - BORDER(win);
5988
5989 X(win) = b->x - BORDER(win);
5990 }
5991
5992 if ((*opts & SWM_CW_BOTTOM) && MAX_Y(win) + BORDER(win) > b->y + b->h) {
5993 if (*opts & SWM_CW_RESIZABLE)
5994 HEIGHT(win) = b->y + b->h - Y(win) - BORDER(win);
5995 else
5996 Y(win) = b->y + b->h - HEIGHT(win) - BORDER(win);
5997 }
5998
5999 if ((*opts & SWM_CW_TOP) && Y(win) + BORDER(win) < b->y) {
6000 if (*opts & SWM_CW_RESIZABLE)
6001 HEIGHT(win) -= b->y - Y(win) - BORDER(win);
6002
6003 Y(win) = b->y - BORDER(win);
6004 }
6005
6006 if (*opts & SWM_CW_RESIZABLE) {
6007 if (WIDTH(win) < 1)
6008 WIDTH(win) = 1;
6009 if (HEIGHT(win) < 1)
6010 HEIGHT(win) = 1;
6011 }
6012 }
6013
6014 void
6015 update_window(struct ws_win *win)
6016 {
6017 uint16_t mask;
6018 uint32_t wc[5];
6019
6020 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
6021 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
6022 XCB_CONFIG_WINDOW_BORDER_WIDTH;
6023 wc[0] = X(win);
6024 wc[1] = Y(win);
6025 wc[2] = WIDTH(win);
6026 wc[3] = HEIGHT(win);
6027 wc[4] = BORDER(win);
6028
6029 DNPRINTF(SWM_D_EVENT, "update_window: win %#x, (x,y) w x h: "
6030 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
6031 wc[3], YESNO(win->bordered));
6032
6033 xcb_configure_window(conn, win->id, mask, wc);
6034 }
6035
6036 #define SWM_RESIZE_STEPS (50)
6037
6038 void
6039 resize(struct ws_win *win, union arg *args)
6040 {
6041 xcb_timestamp_t timestamp = 0;
6042 struct swm_region *r = NULL;
6043 struct swm_geometry g;
6044 int resize_stp = 0;
6045 int top = 0, left = 0, resizing;
6046 int dx, dy;
6047 xcb_cursor_t cursor;
6048 xcb_query_pointer_reply_t *xpr = NULL;
6049 xcb_generic_event_t *evt;
6050 xcb_motion_notify_event_t *mne;
6051
6052 if (win == NULL)
6053 return;
6054 r = win->ws->r;
6055
6056 if (FULLSCREEN(win))
6057 return;
6058
6059 /* In max_stack mode, should only resize transients. */
6060 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !TRANS(win))
6061 return;
6062
6063 DNPRINTF(SWM_D_EVENT, "resize: win %#x, floating: %s, "
6064 "transient: %#x\n", win->id, YESNO(ABOVE(win)),
6065 win->transient);
6066
6067 if (MAXIMIZED(win))
6068 store_float_geom(win);
6069 else if (!(TRANS(win) || ABOVE(win)))
6070 return;
6071
6072 ewmh_apply_flags(win, (win->ewmh_flags | SWM_F_MANUAL | EWMH_F_ABOVE) &
6073 ~EWMH_F_MAXIMIZED);
6074 ewmh_update_wm_state(win);
6075
6076 stack();
6077
6078 focus_flush();
6079
6080 /* It's possible for win to have been freed during focus_flush(). */
6081 if (validate_win(win)) {
6082 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
6083 goto out;
6084 }
6085
6086 switch (args->id) {
6087 case SWM_ARG_ID_WIDTHSHRINK:
6088 WIDTH(win) -= SWM_RESIZE_STEPS;
6089 resize_stp = 1;
6090 break;
6091 case SWM_ARG_ID_WIDTHGROW:
6092 WIDTH(win) += SWM_RESIZE_STEPS;
6093 resize_stp = 1;
6094 break;
6095 case SWM_ARG_ID_HEIGHTSHRINK:
6096 HEIGHT(win) -= SWM_RESIZE_STEPS;
6097 resize_stp = 1;
6098 break;
6099 case SWM_ARG_ID_HEIGHTGROW:
6100 HEIGHT(win) += SWM_RESIZE_STEPS;
6101 resize_stp = 1;
6102 break;
6103 default:
6104 break;
6105 }
6106 if (resize_stp) {
6107 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6108 SWM_CW_HARDBOUNDARY);
6109 update_window(win);
6110 store_float_geom(win);
6111 return;
6112 }
6113
6114 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6115 SWM_CW_SOFTBOUNDARY);
6116 update_window(win);
6117
6118 /* get cursor offset from window root */
6119 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
6120 NULL);
6121 if (xpr == NULL)
6122 return;
6123
6124 g = win->g;
6125
6126 if (xpr->win_x < WIDTH(win) / 2)
6127 left = 1;
6128
6129 if (xpr->win_y < HEIGHT(win) / 2)
6130 top = 1;
6131
6132 if (args->id == SWM_ARG_ID_CENTER)
6133 cursor = cursors[XC_SIZING].cid;
6134 else if (top)
6135 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
6136 XC_TOP_RIGHT_CORNER].cid;
6137 else
6138 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
6139 XC_BOTTOM_RIGHT_CORNER].cid;
6140
6141 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
6142 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
6143 XCB_CURRENT_TIME),
6144
6145 xcb_flush(conn);
6146 resizing = 1;
6147 while (resizing && (evt = xcb_wait_for_event(conn))) {
6148 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
6149 case XCB_BUTTON_RELEASE:
6150 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
6151 resizing = 0;
6152 break;
6153 case XCB_MOTION_NOTIFY:
6154 mne = (xcb_motion_notify_event_t *)evt;
6155 /* cursor offset/delta from start of the operation */
6156 dx = mne->root_x - xpr->root_x;
6157 dy = mne->root_y - xpr->root_y;
6158
6159 /* vertical */
6160 if (top)
6161 dy = -dy;
6162
6163 if (args->id == SWM_ARG_ID_CENTER) {
6164 if (g.h / 2 + dy < 1)
6165 dy = 1 - g.h / 2;
6166
6167 Y(win) = g.y - dy;
6168 HEIGHT(win) = g.h + 2 * dy;
6169 } else {
6170 if (g.h + dy < 1)
6171 dy = 1 - g.h;
6172
6173 if (top)
6174 Y(win) = g.y - dy;
6175
6176 HEIGHT(win) = g.h + dy;
6177 }
6178
6179 /* horizontal */
6180 if (left)
6181 dx = -dx;
6182
6183 if (args->id == SWM_ARG_ID_CENTER) {
6184 if (g.w / 2 + dx < 1)
6185 dx = 1 - g.w / 2;
6186
6187 X(win) = g.x - dx;
6188 WIDTH(win) = g.w + 2 * dx;
6189 } else {
6190 if (g.w + dx < 1)
6191 dx = 1 - g.w;
6192
6193 if (left)
6194 X(win) = g.x - dx;
6195
6196 WIDTH(win) = g.w + dx;
6197 }
6198
6199 /* not free, don't sync more than 120 times / second */
6200 if ((mne->time - timestamp) > (1000 / 120) ) {
6201 timestamp = mne->time;
6202 regionize(win, mne->root_x, mne->root_y);
6203 region_containment(win, r, SWM_CW_ALLSIDES |
6204 SWM_CW_RESIZABLE | SWM_CW_HARDBOUNDARY |
6205 SWM_CW_SOFTBOUNDARY);
6206 update_window(win);
6207 xcb_flush(conn);
6208 }
6209 break;
6210 case XCB_KEY_PRESS:
6211 /* Ignore. */
6212 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
6213 ((xcb_key_press_event_t *)evt)->time);
6214 xcb_flush(conn);
6215 break;
6216 default:
6217 event_handle(evt);
6218
6219 /* It's possible for win to have been freed above. */
6220 if (validate_win(win)) {
6221 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
6222 goto out;
6223 }
6224 break;
6225 }
6226 free(evt);
6227 }
6228 if (timestamp) {
6229 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6230 SWM_CW_HARDBOUNDARY | SWM_CW_SOFTBOUNDARY);
6231 update_window(win);
6232 xcb_flush(conn);
6233 }
6234 store_float_geom(win);
6235 out:
6236 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6237 free(xpr);
6238 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
6239 }
6240
6241 void
6242 resize_step(struct swm_region *r, union arg *args)
6243 {
6244 struct ws_win *win = NULL;
6245
6246 if (r && r->ws && r->ws->focus)
6247 win = r->ws->focus;
6248 else
6249 return;
6250
6251 resize(win, args);
6252 focus_flush();
6253 }
6254
6255 /* Try to set window region based on supplied coordinates or window center. */
6256 void
6257 regionize(struct ws_win *win, int x, int y)
6258 {
6259 struct swm_region *r = NULL;
6260
6261 r = region_under(win->s, x, y);
6262 if (r == NULL)
6263 r = region_under(win->s, X(win) + WIDTH(win) / 2,
6264 Y(win) + HEIGHT(win) / 2);
6265
6266 if (r != NULL && r != win->ws->r) {
6267 if (clear_maximized(r->ws) > 0)
6268 stack();
6269
6270 win_to_ws(win, r->ws->idx, 0);
6271
6272 /* Set focus on new ws. */
6273 unfocus_win(r->ws->focus);
6274 r->ws->focus = win;
6275
6276 set_region(r);
6277 raise_window(win);
6278 }
6279 }
6280
6281 #define SWM_MOVE_STEPS (50)
6282
6283 void
6284 move(struct ws_win *win, union arg *args)
6285 {
6286 struct swm_region *r;
6287 xcb_timestamp_t timestamp = 0;
6288 int move_stp = 0, moving, restack = 0;
6289 xcb_query_pointer_reply_t *qpr = NULL;
6290 xcb_generic_event_t *evt;
6291 xcb_motion_notify_event_t *mne;
6292
6293 if (win == NULL)
6294 return;
6295
6296 if ((r = win->ws->r) == NULL)
6297 return;
6298
6299 if (FULLSCREEN(win))
6300 return;
6301
6302 DNPRINTF(SWM_D_EVENT, "move: win %#x, floating: %s, transient: "
6303 "%#x\n", win->id, YESNO(ABOVE(win)), win->transient);
6304
6305 /* in max_stack mode should only move transients */
6306 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !TRANS(win))
6307 return;
6308
6309 if (!(ABOVE(win) || TRANS(win)) || MAXIMIZED(win)) {
6310 store_float_geom(win);
6311 restack = 1;
6312 }
6313
6314 ewmh_apply_flags(win, (win->ewmh_flags | SWM_F_MANUAL | EWMH_F_ABOVE) &
6315 ~EWMH_F_MAXIMIZED);
6316 ewmh_update_wm_state(win);
6317
6318 if (restack)
6319 stack();
6320
6321 focus_flush();
6322
6323 /* It's possible for win to have been freed during focus_flush(). */
6324 if (validate_win(win)) {
6325 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
6326 goto out;
6327 }
6328
6329 move_stp = 0;
6330 switch (args->id) {
6331 case SWM_ARG_ID_MOVELEFT:
6332 X(win) -= (SWM_MOVE_STEPS - border_width);
6333 move_stp = 1;
6334 break;
6335 case SWM_ARG_ID_MOVERIGHT:
6336 X(win) += (SWM_MOVE_STEPS - border_width);
6337 move_stp = 1;
6338 break;
6339 case SWM_ARG_ID_MOVEUP:
6340 Y(win) -= (SWM_MOVE_STEPS - border_width);
6341 move_stp = 1;
6342 break;
6343 case SWM_ARG_ID_MOVEDOWN:
6344 Y(win) += (SWM_MOVE_STEPS - border_width);
6345 move_stp = 1;
6346 break;
6347 default:
6348 break;
6349 }
6350 if (move_stp) {
6351 regionize(win, -1, -1);
6352 region_containment(win, win->ws->r, SWM_CW_ALLSIDES);
6353 update_window(win);
6354 store_float_geom(win);
6355 return;
6356 }
6357
6358 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
6359 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
6360 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
6361
6362 /* get cursor offset from window root */
6363 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
6364 NULL);
6365 if (qpr == NULL) {
6366 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6367 return;
6368 }
6369
6370 regionize(win, qpr->root_x, qpr->root_y);
6371 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
6372 SWM_CW_SOFTBOUNDARY);
6373 update_window(win);
6374 xcb_flush(conn);
6375 moving = 1;
6376 while (moving && (evt = xcb_wait_for_event(conn))) {
6377 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
6378 case XCB_BUTTON_RELEASE:
6379 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
6380 moving = 0;
6381 break;
6382 case XCB_MOTION_NOTIFY:
6383 mne = (xcb_motion_notify_event_t *)evt;
6384 DNPRINTF(SWM_D_EVENT, "motion: root: %#x\n", mne->root);
6385 X(win) = mne->root_x - qpr->win_x - border_width;
6386 Y(win) = mne->root_y - qpr->win_y - border_width;
6387
6388 /* not free, don't sync more than 120 times / second */
6389 if ((mne->time - timestamp) > (1000 / 120) ) {
6390 timestamp = mne->time;
6391 regionize(win, mne->root_x, mne->root_y);
6392 region_containment(win, win->ws->r,
6393 SWM_CW_ALLSIDES | SWM_CW_SOFTBOUNDARY);
6394 update_window(win);
6395 xcb_flush(conn);
6396 }
6397 break;
6398 case XCB_KEY_PRESS:
6399 /* Ignore. */
6400 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
6401 ((xcb_key_press_event_t *)evt)->time);
6402 xcb_flush(conn);
6403 break;
6404 default:
6405 event_handle(evt);
6406
6407 /* It's possible for win to have been freed above. */
6408 if (validate_win(win)) {
6409 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
6410 goto out;
6411 }
6412 break;
6413 }
6414 free(evt);
6415 }
6416 if (timestamp) {
6417 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
6418 SWM_CW_SOFTBOUNDARY);
6419 update_window(win);
6420 xcb_flush(conn);
6421 }
6422 store_float_geom(win);
6423
6424 /* New region set to fullscreen layout. */
6425 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK]) {
6426 stack();
6427 focus_flush();
6428 }
6429
6430 out:
6431 free(qpr);
6432 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6433 DNPRINTF(SWM_D_EVENT, "move: done.\n");
6434 }
6435
6436 void
6437 move_step(struct swm_region *r, union arg *args)
6438 {
6439 struct ws_win *win = NULL;
6440
6441 if (r && r->ws && r->ws->focus)
6442 win = r->ws->focus;
6443 else
6444 return;
6445
6446 if (!TRANS(win) && !ABOVE(win))
6447 return;
6448
6449 move(win, args);
6450 focus_flush();
6451 }
6452
6453 /* key definitions */
6454 struct keyfunc {
6455 char name[SWM_FUNCNAME_LEN];
6456 void (*func)(struct swm_region *r, union arg *);
6457 union arg args;
6458 } keyfuncs[KF_INVALID + 1] = {
6459 /* name function argument */
6460 { "bar_toggle", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE} },
6461 { "bar_toggle_ws", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
6462 { "button2", pressbutton, {2} },
6463 { "cycle_layout", cycle_layout, {0} },
6464 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
6465 { "float_toggle", floating_toggle,{0} },
6466 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
6467 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
6468 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
6469 { "focus_urgent", focus, {.id = SWM_ARG_ID_FOCUSURGENT} },
6470 { "maximize_toggle", maximize_toggle,{0} },
6471 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
6472 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
6473 { "iconify", iconify, {0} },
6474 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
6475 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
6476 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
6477 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
6478 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
6479 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
6480 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
6481 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
6482 { "mvrg_1", send_to_rg, {.id = 0} },
6483 { "mvrg_2", send_to_rg, {.id = 1} },
6484 { "mvrg_3", send_to_rg, {.id = 2} },
6485 { "mvrg_4", send_to_rg, {.id = 3} },
6486 { "mvrg_5", send_to_rg, {.id = 4} },
6487 { "mvrg_6", send_to_rg, {.id = 5} },
6488 { "mvrg_7", send_to_rg, {.id = 6} },
6489 { "mvrg_8", send_to_rg, {.id = 7} },
6490 { "mvrg_9", send_to_rg, {.id = 8} },
6491 { "mvws_1", send_to_ws, {.id = 0} },
6492 { "mvws_2", send_to_ws, {.id = 1} },
6493 { "mvws_3", send_to_ws, {.id = 2} },
6494 { "mvws_4", send_to_ws, {.id = 3} },
6495 { "mvws_5", send_to_ws, {.id = 4} },
6496 { "mvws_6", send_to_ws, {.id = 5} },
6497 { "mvws_7", send_to_ws, {.id = 6} },
6498 { "mvws_8", send_to_ws, {.id = 7} },
6499 { "mvws_9", send_to_ws, {.id = 8} },
6500 { "mvws_10", send_to_ws, {.id = 9} },
6501 { "mvws_11", send_to_ws, {.id = 10} },
6502 { "mvws_12", send_to_ws, {.id = 11} },
6503 { "mvws_13", send_to_ws, {.id = 12} },
6504 { "mvws_14", send_to_ws, {.id = 13} },
6505 { "mvws_15", send_to_ws, {.id = 14} },
6506 { "mvws_16", send_to_ws, {.id = 15} },
6507 { "mvws_17", send_to_ws, {.id = 16} },
6508 { "mvws_18", send_to_ws, {.id = 17} },
6509 { "mvws_19", send_to_ws, {.id = 18} },
6510 { "mvws_20", send_to_ws, {.id = 19} },
6511 { "mvws_21", send_to_ws, {.id = 20} },
6512 { "mvws_22", send_to_ws, {.id = 21} },
6513 { "name_workspace", name_workspace, {0} },
6514 { "quit", quit, {0} },
6515 { "raise_toggle", raise_toggle, {0} },
6516 { "restart", restart, {0} },
6517 { "rg_1", focusrg, {.id = 0} },
6518 { "rg_2", focusrg, {.id = 1} },
6519 { "rg_3", focusrg, {.id = 2} },
6520 { "rg_4", focusrg, {.id = 3} },
6521 { "rg_5", focusrg, {.id = 4} },
6522 { "rg_6", focusrg, {.id = 5} },
6523 { "rg_7", focusrg, {.id = 6} },
6524 { "rg_8", focusrg, {.id = 7} },
6525 { "rg_9", focusrg, {.id = 8} },
6526 { "rg_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
6527 { "rg_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
6528 { "screen_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
6529 { "screen_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
6530 { "search_win", search_win, {0} },
6531 { "search_workspace", search_workspace, {0} },
6532 { "spawn_custom", NULL, {0} },
6533 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
6534 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
6535 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
6536 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
6537 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
6538 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
6539 { "uniconify", uniconify, {0} },
6540 { "version", version, {0} },
6541 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
6542 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
6543 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
6544 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
6545 { "ws_1", switchws, {.id = 0} },
6546 { "ws_2", switchws, {.id = 1} },
6547 { "ws_3", switchws, {.id = 2} },
6548 { "ws_4", switchws, {.id = 3} },
6549 { "ws_5", switchws, {.id = 4} },
6550 { "ws_6", switchws, {.id = 5} },
6551 { "ws_7", switchws, {.id = 6} },
6552 { "ws_8", switchws, {.id = 7} },
6553 { "ws_9", switchws, {.id = 8} },
6554 { "ws_10", switchws, {.id = 9} },
6555 { "ws_11", switchws, {.id = 10} },
6556 { "ws_12", switchws, {.id = 11} },
6557 { "ws_13", switchws, {.id = 12} },
6558 { "ws_14", switchws, {.id = 13} },
6559 { "ws_15", switchws, {.id = 14} },
6560 { "ws_16", switchws, {.id = 15} },
6561 { "ws_17", switchws, {.id = 16} },
6562 { "ws_18", switchws, {.id = 17} },
6563 { "ws_19", switchws, {.id = 18} },
6564 { "ws_20", switchws, {.id = 19} },
6565 { "ws_21", switchws, {.id = 20} },
6566 { "ws_22", switchws, {.id = 21} },
6567 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
6568 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
6569 { "ws_next_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_UP} },
6570 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
6571 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
6572 { "ws_prev_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_DOWN} },
6573 { "ws_prior", priorws, {0} },
6574 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
6575 { "invalid key func", NULL, {0} },
6576 };
6577
6578 int
6579 key_cmp(struct key *kp1, struct key *kp2)
6580 {
6581 if (kp1->keysym < kp2->keysym)
6582 return (-1);
6583 if (kp1->keysym > kp2->keysym)
6584 return (1);
6585
6586 if (kp1->mod < kp2->mod)
6587 return (-1);
6588 if (kp1->mod > kp2->mod)
6589 return (1);
6590
6591 return (0);
6592 }
6593
6594 /* mouse */
6595 enum { client_click, root_click };
6596 struct button {
6597 unsigned int action;
6598 unsigned int mask;
6599 unsigned int button;
6600 void (*func)(struct ws_win *, union arg *);
6601 union arg args;
6602 } buttons[] = {
6603 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6604 /* action key mouse button func args */
6605 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
6606 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
6607 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
6608 #undef MODKEY_SHIFT
6609 };
6610
6611 void
6612 update_modkey(unsigned int mod)
6613 {
6614 int i;
6615 struct key *kp;
6616
6617 mod_key = mod;
6618 RB_FOREACH(kp, key_tree, &keys)
6619 if (kp->mod & XCB_MOD_MASK_SHIFT)
6620 kp->mod = mod | XCB_MOD_MASK_SHIFT;
6621 else
6622 kp->mod = mod;
6623
6624 for (i = 0; i < LENGTH(buttons); i++)
6625 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
6626 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
6627 else
6628 buttons[i].mask = mod;
6629 }
6630
6631 int
6632 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
6633 char ***ret_args)
6634 {
6635 struct spawn_prog *prog = NULL;
6636 int i, c;
6637 char *ap, **real_args;
6638
6639 /* suppress unused warning since var is needed */
6640 (void)args;
6641
6642 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
6643
6644 /* find program */
6645 TAILQ_FOREACH(prog, &spawns, entry) {
6646 if (strcasecmp(spawn_name, prog->name) == 0)
6647 break;
6648 }
6649 if (prog == NULL) {
6650 warnx("spawn_custom: program %s not found", spawn_name);
6651 return (-1);
6652 }
6653
6654 /* make room for expanded args */
6655 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
6656 err(1, "spawn_custom: calloc real_args");
6657
6658 /* expand spawn_args into real_args */
6659 for (i = c = 0; i < prog->argc; i++) {
6660 ap = prog->argv[i];
6661 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
6662 if (strcasecmp(ap, "$bar_border") == 0) {
6663 if ((real_args[c] =
6664 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
6665 == NULL)
6666 err(1, "spawn_custom border color");
6667 } else if (strcasecmp(ap, "$bar_color") == 0) {
6668 if ((real_args[c] =
6669 strdup(r->s->c[SWM_S_COLOR_BAR].name))
6670 == NULL)
6671 err(1, "spawn_custom bar color");
6672 } else if (strcasecmp(ap, "$bar_font") == 0) {
6673 if ((real_args[c] = strdup(bar_fonts))
6674 == NULL)
6675 err(1, "spawn_custom bar fonts");
6676 } else if (strcasecmp(ap, "$bar_font_color") == 0) {
6677 if ((real_args[c] =
6678 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
6679 == NULL)
6680 err(1, "spawn_custom color font");
6681 } else if (strcasecmp(ap, "$color_focus") == 0) {
6682 if ((real_args[c] =
6683 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
6684 == NULL)
6685 err(1, "spawn_custom color focus");
6686 } else if (strcasecmp(ap, "$color_unfocus") == 0) {
6687 if ((real_args[c] =
6688 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
6689 == NULL)
6690 err(1, "spawn_custom color unfocus");
6691 } else if (strcasecmp(ap, "$region_index") == 0) {
6692 if (asprintf(&real_args[c], "%d",
6693 get_region_index(r) + 1) < 1)
6694 err(1, "spawn_custom region index");
6695 } else if (strcasecmp(ap, "$workspace_index") == 0) {
6696 if (asprintf(&real_args[c], "%d", r->ws->idx + 1) < 1)
6697 err(1, "spawn_custom workspace index");
6698 } else if (strcasecmp(ap, "$dmenu_bottom") == 0) {
6699 if (!bar_at_bottom)
6700 continue;
6701 if ((real_args[c] = strdup("-b")) == NULL)
6702 err(1, "spawn_custom workspace index");
6703 } else {
6704 /* no match --> copy as is */
6705 if ((real_args[c] = strdup(ap)) == NULL)
6706 err(1, "spawn_custom strdup(ap)");
6707 }
6708 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
6709 real_args[c]);
6710 ++c;
6711 }
6712
6713 #ifdef SWM_DEBUG
6714 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
6715 for (i = 0; i < c; ++i)
6716 DPRINTF("\"%s\" ", real_args[i]);
6717 DPRINTF("\n");
6718 #endif
6719 *ret_args = real_args;
6720 return (c);
6721 }
6722
6723 void
6724 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
6725 {
6726 union arg a;
6727 char **real_args;
6728 int spawn_argc, i;
6729
6730 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6731 return;
6732 a.argv = real_args;
6733 if (fork() == 0)
6734 spawn(r->ws->idx, &a, 1);
6735
6736 for (i = 0; i < spawn_argc; i++)
6737 free(real_args[i]);
6738 free(real_args);
6739 }
6740
6741 void
6742 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
6743 int *pid)
6744 {
6745 union arg a;
6746 char **real_args;
6747 int i, spawn_argc;
6748
6749 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6750 return;
6751 a.argv = real_args;
6752
6753 if (pipe(select_list_pipe) == -1)
6754 err(1, "pipe error");
6755 if (pipe(select_resp_pipe) == -1)
6756 err(1, "pipe error");
6757
6758 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
6759 err(1, "could not disable SIGPIPE");
6760 switch (*pid = fork()) {
6761 case -1:
6762 err(1, "cannot fork");
6763 break;
6764 case 0: /* child */
6765 if (dup2(select_list_pipe[0], STDIN_FILENO) == -1)
6766 err(1, "dup2");
6767 if (dup2(select_resp_pipe[1], STDOUT_FILENO) == -1)
6768 err(1, "dup2");
6769 close(select_list_pipe[1]);
6770 close(select_resp_pipe[0]);
6771 spawn(r->ws->idx, &a, 0);
6772 break;
6773 default: /* parent */
6774 close(select_list_pipe[0]);
6775 close(select_resp_pipe[1]);
6776 break;
6777 }
6778
6779 for (i = 0; i < spawn_argc; i++)
6780 free(real_args[i]);
6781 free(real_args);
6782 }
6783
6784 /* Argument tokenizer. */
6785 char *
6786 argsep(char **sp) {
6787 int single_quoted = 0, double_quoted = 0;
6788 char *arg, *cp, *next;
6789
6790 if (*sp == NULL)
6791 return NULL;
6792
6793 /* Eat and move characters until end of argument is found. */
6794 for (arg = next = cp = *sp; *cp != '\0'; ++cp) {
6795 if (!double_quoted && *cp == '\'') {
6796 /* Eat single-quote. */
6797 single_quoted = !single_quoted;
6798 } else if (!single_quoted && *cp == '"') {
6799 /* Eat double-quote. */
6800 double_quoted = !double_quoted;
6801 } else if (!single_quoted && *cp == '\\' && *(cp + 1) == '"') {
6802 /* Eat backslash; copy escaped character to arg. */
6803 *next++ = *(++cp);
6804 } else if (!single_quoted && !double_quoted && *cp == '\\' &&
6805 (*(cp + 1) == '\'' || *(cp + 1) == ' ')) {
6806 /* Eat backslash; move escaped character. */
6807 *next++ = *(++cp);
6808 } else if (!single_quoted && !double_quoted &&
6809 (*cp == ' ' || *cp == '\t')) {
6810 /* Terminate argument. */
6811 *next++ = '\0';
6812 /* Point sp to beginning of next argument. */
6813 *sp = ++cp;
6814 break;
6815 } else {
6816 /* Move regular character. */
6817 *next++ = *cp;
6818 }
6819 }
6820
6821 /* Terminate argument if end of string. */
6822 if (*cp == '\0') {
6823 *next = '\0';
6824 *sp = NULL;
6825 }
6826
6827 return arg;
6828 }
6829
6830 void
6831 spawn_insert(const char *name, const char *args, int flags)
6832 {
6833 struct spawn_prog *sp;
6834 char *arg, *dup, *ptr;
6835
6836 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s[%s]\n", name, args);
6837
6838 if (args == NULL || *args == '\0')
6839 return;
6840
6841 if ((sp = calloc(1, sizeof *sp)) == NULL)
6842 err(1, "spawn_insert: calloc");
6843 if ((sp->name = strdup(name)) == NULL)
6844 err(1, "spawn_insert: strdup");
6845
6846 /* Convert the arguments to an argument list. */
6847 if ((ptr = dup = strdup(args)) == NULL)
6848 err(1, "spawn_insert: strdup");
6849 while ((arg = argsep(&ptr)) != NULL) {
6850 /* Null argument; skip it. */
6851 if (*arg == '\0')
6852 continue;
6853
6854 sp->argc++;
6855 if ((sp->argv = realloc(sp->argv, sp->argc *
6856 sizeof *sp->argv)) == NULL)
6857 err(1, "spawn_insert: realloc");
6858 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
6859 err(1, "spawn_insert: strdup");
6860 }
6861 free(dup);
6862
6863 sp->flags = flags;
6864
6865 DNPRINTF(SWM_D_SPAWN, "arg %d: [%s]\n", sp->argc, sp->argv[sp->argc-1]);
6866 TAILQ_INSERT_TAIL(&spawns, sp, entry);
6867 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
6868 }
6869
6870 void
6871 spawn_remove(struct spawn_prog *sp)
6872 {
6873 int i;
6874
6875 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
6876
6877 TAILQ_REMOVE(&spawns, sp, entry);
6878 for (i = 0; i < sp->argc; i++)
6879 free(sp->argv[i]);
6880 free(sp->argv);
6881 free(sp->name);
6882 free(sp);
6883
6884 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
6885 }
6886
6887 struct spawn_prog*
6888 spawn_find(const char *name)
6889 {
6890 struct spawn_prog *sp;
6891
6892 TAILQ_FOREACH(sp, &spawns, entry)
6893 if (strcasecmp(sp->name, name) == 0)
6894 return sp;
6895
6896 return NULL;
6897 }
6898
6899 void
6900 setspawn(const char *name, const char *args, int flags)
6901 {
6902 struct spawn_prog *sp;
6903
6904 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
6905
6906 if (name == NULL)
6907 return;
6908
6909 /* Remove any old spawn under the same name. */
6910 if ((sp = spawn_find(name)) != NULL)
6911 spawn_remove(sp);
6912
6913 if (*args != '\0')
6914 spawn_insert(name, args, flags);
6915 else
6916 warnx("error: setspawn: cannot find program: %s", name);
6917
6918 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
6919 }
6920
6921 int
6922 setconfspawn(const char *selector, const char *value, int flags)
6923 {
6924 char *args;
6925
6926 if (selector == NULL || strlen(selector) == 0)
6927 return (1);
6928
6929 args = expand_tilde(value);
6930
6931 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
6932
6933 setspawn(selector, args, flags);
6934 free(args);
6935
6936 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
6937 return (0);
6938 }
6939
6940 void
6941 validate_spawns(void)
6942 {
6943 struct spawn_prog *sp;
6944 char which[PATH_MAX];
6945 size_t i;
6946
6947 struct key *kp;
6948
6949 RB_FOREACH(kp, key_tree, &keys) {
6950 if (kp->funcid != KF_SPAWN_CUSTOM)
6951 continue;
6952
6953 /* find program */
6954 sp = spawn_find(kp->spawn_name);
6955 if (sp == NULL || sp->flags & SWM_SPAWN_OPTIONAL)
6956 continue;
6957
6958 /* verify we have the goods */
6959 snprintf(which, sizeof which, "which %s", sp->argv[0]);
6960 DNPRINTF(SWM_D_CONF, "validate_spawns: which %s\n",
6961 sp->argv[0]);
6962 for (i = strlen("which "); i < strlen(which); i++)
6963 if (which[i] == ' ') {
6964 which[i] = '\0';
6965 break;
6966 }
6967 if (system(which) != 0)
6968 add_startup_exception("could not find %s",
6969 &which[strlen("which ")]);
6970 }
6971 }
6972
6973 void
6974 setup_spawn(void)
6975 {
6976 setconfspawn("lock", "xlock", 0);
6977
6978 setconfspawn("term", "xterm", 0);
6979 setconfspawn("spawn_term", "xterm", 0);
6980
6981 setconfspawn("menu", "dmenu_run"
6982 " $dmenu_bottom"
6983 " -fn $bar_font"
6984 " -nb $bar_color"
6985 " -nf $bar_font_color"
6986 " -sb $bar_border"
6987 " -sf $bar_color", 0);
6988
6989 setconfspawn("search", "dmenu"
6990 " $dmenu_bottom"
6991 " -i"
6992 " -fn $bar_font"
6993 " -nb $bar_color"
6994 " -nf $bar_font_color"
6995 " -sb $bar_border"
6996 " -sf $bar_color", 0);
6997
6998 setconfspawn("name_workspace", "dmenu"
6999 " $dmenu_bottom"
7000 " -p Workspace"
7001 " -fn $bar_font"
7002 " -nb $bar_color"
7003 " -nf $bar_font_color"
7004 " -sb $bar_border"
7005 " -sf $bar_color", 0);
7006
7007 /* These are not verified for existence, even with a binding set. */
7008 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL);
7009 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL);
7010 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL);
7011 }
7012
7013 /* key bindings */
7014 #define SWM_MODNAME_SIZE 32
7015 #define SWM_KEY_WS "\n+ \t"
7016 int
7017 parsekeys(const char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
7018 {
7019 char *str, *cp, *name;
7020 KeySym uks;
7021
7022 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
7023 if (mod == NULL || ks == NULL) {
7024 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
7025 return (1);
7026 }
7027 if (keystr == NULL || strlen(keystr) == 0) {
7028 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
7029 return (1);
7030 }
7031
7032 if ((cp = str = strdup(keystr)) == NULL)
7033 err(1, "parsekeys: strdup");
7034
7035 *ks = XCB_NO_SYMBOL;
7036 *mod = 0;
7037 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
7038 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
7039 if (cp)
7040 cp += (long)strspn(cp, SWM_KEY_WS);
7041 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
7042 *mod |= currmod;
7043 else if (strncasecmp(name, "Mod1", SWM_MODNAME_SIZE) == 0)
7044 *mod |= XCB_MOD_MASK_1;
7045 else if (strncasecmp(name, "Mod2", SWM_MODNAME_SIZE) == 0)
7046 *mod += XCB_MOD_MASK_2;
7047 else if (strncmp(name, "Mod3", SWM_MODNAME_SIZE) == 0)
7048 *mod |= XCB_MOD_MASK_3;
7049 else if (strncmp(name, "Mod4", SWM_MODNAME_SIZE) == 0)
7050 *mod |= XCB_MOD_MASK_4;
7051 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
7052 *mod |= XCB_MOD_MASK_SHIFT;
7053 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
7054 *mod |= XCB_MOD_MASK_CONTROL;
7055 else {
7056 *ks = XStringToKeysym(name);
7057 XConvertCase(*ks, ks, &uks);
7058 if (ks == XCB_NO_SYMBOL) {
7059 DNPRINTF(SWM_D_KEY,
7060 "parsekeys: invalid key %s\n",
7061 name);
7062 free(str);
7063 return (1);
7064 }
7065 }
7066 }
7067
7068 free(str);
7069 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
7070 return (0);
7071 }
7072
7073 char *
7074 strdupsafe(const char *str)
7075 {
7076 if (str == NULL)
7077 return (NULL);
7078 else
7079 return (strdup(str));
7080 }
7081
7082 void
7083 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
7084 const char *spawn_name)
7085 {
7086 struct key *kp;
7087
7088 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
7089 keyfuncs[kfid].name, spawn_name);
7090
7091 if ((kp = malloc(sizeof *kp)) == NULL)
7092 err(1, "key_insert: malloc");
7093
7094 kp->mod = mod;
7095 kp->keysym = ks;
7096 kp->funcid = kfid;
7097 kp->spawn_name = strdupsafe(spawn_name);
7098 RB_INSERT(key_tree, &keys, kp);
7099
7100 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
7101 }
7102
7103 struct key *
7104 key_lookup(unsigned int mod, KeySym ks)
7105 {
7106 struct key kp;
7107
7108 kp.keysym = ks;
7109 kp.mod = mod;
7110
7111 return (RB_FIND(key_tree, &keys, &kp));
7112 }
7113
7114 void
7115 key_remove(struct key *kp)
7116 {
7117 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
7118
7119 RB_REMOVE(key_tree, &keys, kp);
7120 free(kp->spawn_name);
7121 free(kp);
7122
7123 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
7124 }
7125
7126 void
7127 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
7128 const char *spawn_name)
7129 {
7130 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
7131 spawn_name);
7132
7133 key_remove(kp);
7134 key_insert(mod, ks, kfid, spawn_name);
7135
7136 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
7137 }
7138
7139 void
7140 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
7141 const char *spawn_name)
7142 {
7143 struct key *kp;
7144
7145 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
7146 keyfuncs[kfid].name, spawn_name);
7147
7148 if ((kp = key_lookup(mod, ks)) != NULL) {
7149 if (kfid == KF_INVALID)
7150 key_remove(kp);
7151 else
7152 key_replace(kp, mod, ks, kfid, spawn_name);
7153 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7154 return;
7155 }
7156 if (kfid == KF_INVALID) {
7157 warnx("bind: Key combination already unbound.");
7158 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7159 return;
7160 }
7161
7162 key_insert(mod, ks, kfid, spawn_name);
7163 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
7164 }
7165
7166 int
7167 setconfbinding(const char *selector, const char *value, int flags)
7168 {
7169 enum keyfuncid kfid;
7170 unsigned int mod;
7171 KeySym ks;
7172 struct spawn_prog *sp;
7173
7174 /* suppress unused warning since var is needed */
7175 (void)flags;
7176
7177 DNPRINTF(SWM_D_KEY, "setconfbinding: enter selector: [%s], "
7178 "value: [%s]\n", selector, value);
7179 if (selector == NULL || strlen(selector) == 0) {
7180 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
7181 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7182 kfid = KF_INVALID;
7183 setkeybinding(mod, ks, kfid, NULL);
7184 return (0);
7185 } else
7186 return (1);
7187 }
7188 /* search by key function name */
7189 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
7190 if (strncasecmp(selector, keyfuncs[kfid].name,
7191 SWM_FUNCNAME_LEN) == 0) {
7192 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
7193 "keyfunc\n", selector);
7194 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7195 setkeybinding(mod, ks, kfid, NULL);
7196 return (0);
7197 } else
7198 return (1);
7199 }
7200 }
7201 /* search by custom spawn name */
7202 if ((sp = spawn_find(selector)) != NULL) {
7203 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
7204 "spawn\n", selector);
7205 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
7206 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
7207 sp->name);
7208 return (0);
7209 } else
7210 return (1);
7211 }
7212 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
7213 return (1);
7214 }
7215
7216 void
7217 setup_keys(void)
7218 {
7219 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
7220 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
7221 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
7222 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
7223 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
7224 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
7225 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
7226 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
7227 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
7228 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
7229 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
7230 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
7231 setkeybinding(MODKEY, XK_u, KF_FOCUS_URGENT,NULL);
7232 setkeybinding(MODKEY, XK_e, KF_MAXIMIZE_TOGGLE,NULL);
7233 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
7234 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
7235 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
7236 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
7237 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
7238 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
7239 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
7240 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
7241 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
7242 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
7243 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
7244 setkeybinding(MODKEY_SHIFT, XK_KP_End, KF_MVRG_1, NULL);
7245 setkeybinding(MODKEY_SHIFT, XK_KP_Down, KF_MVRG_2, NULL);
7246 setkeybinding(MODKEY_SHIFT, XK_KP_Next, KF_MVRG_3, NULL);
7247 setkeybinding(MODKEY_SHIFT, XK_KP_Left, KF_MVRG_4, NULL);
7248 setkeybinding(MODKEY_SHIFT, XK_KP_Begin, KF_MVRG_5, NULL);
7249 setkeybinding(MODKEY_SHIFT, XK_KP_Right, KF_MVRG_6, NULL);
7250 setkeybinding(MODKEY_SHIFT, XK_KP_Home, KF_MVRG_7, NULL);
7251 setkeybinding(MODKEY_SHIFT, XK_KP_Up, KF_MVRG_8, NULL);
7252 setkeybinding(MODKEY_SHIFT, XK_KP_Prior, KF_MVRG_9, NULL);
7253 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
7254 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
7255 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
7256 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
7257 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
7258 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
7259 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
7260 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
7261 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
7262 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
7263 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
7264 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
7265 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
7266 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
7267 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
7268 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
7269 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
7270 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
7271 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
7272 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
7273 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
7274 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
7275 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
7276 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
7277 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
7278 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
7279 setkeybinding(MODKEY, XK_KP_End, KF_RG_1, NULL);
7280 setkeybinding(MODKEY, XK_KP_Down, KF_RG_2, NULL);
7281 setkeybinding(MODKEY, XK_KP_Next, KF_RG_3, NULL);
7282 setkeybinding(MODKEY, XK_KP_Left, KF_RG_4, NULL);
7283 setkeybinding(MODKEY, XK_KP_Begin, KF_RG_5, NULL);
7284 setkeybinding(MODKEY, XK_KP_Right, KF_RG_6, NULL);
7285 setkeybinding(MODKEY, XK_KP_Home, KF_RG_7, NULL);
7286 setkeybinding(MODKEY, XK_KP_Up, KF_RG_8, NULL);
7287 setkeybinding(MODKEY, XK_KP_Prior, KF_RG_9, NULL);
7288 setkeybinding(MODKEY_SHIFT, XK_Right, KF_RG_NEXT, NULL);
7289 setkeybinding(MODKEY_SHIFT, XK_Left, KF_RG_PREV, NULL);
7290 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
7291 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
7292 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
7293 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
7294 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
7295 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
7296 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
7297 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
7298 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
7299 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
7300 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
7301 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
7302 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
7303 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
7304 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
7305 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
7306 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
7307 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
7308 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
7309 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
7310 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
7311 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
7312 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
7313 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
7314 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
7315 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
7316 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
7317 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
7318 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
7319 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
7320 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
7321 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
7322 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
7323 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
7324 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
7325 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
7326 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
7327 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
7328 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
7329 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
7330 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
7331 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
7332 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
7333 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
7334 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
7335 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
7336 setkeybinding(MODKEY_SHIFT, XK_Up, KF_WS_NEXT_MOVE,NULL);
7337 setkeybinding(MODKEY_SHIFT, XK_Down, KF_WS_PREV_MOVE,NULL);
7338 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
7339 #ifdef SWM_DEBUG
7340 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
7341 #endif
7342 #undef MODKEY_SHIFT
7343 }
7344
7345 void
7346 clear_keys(void)
7347 {
7348 struct key *kp;
7349
7350 while (RB_EMPTY(&keys) == 0) {
7351 kp = RB_ROOT(&keys);
7352 key_remove(kp);
7353 }
7354 }
7355
7356 int
7357 setkeymapping(const char *selector, const char *value, int flags)
7358 {
7359 char *keymapping_file;
7360
7361 /* suppress unused warnings since vars are needed */
7362 (void)selector;
7363 (void)flags;
7364
7365 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
7366
7367 keymapping_file = expand_tilde(value);
7368
7369 clear_keys();
7370 /* load new key bindings; if it fails, revert to default bindings */
7371 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
7372 clear_keys();
7373 setup_keys();
7374 }
7375
7376 free(keymapping_file);
7377
7378 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
7379 return (0);
7380 }
7381
7382 void
7383 updatenumlockmask(void)
7384 {
7385 unsigned int i, j;
7386 xcb_get_modifier_mapping_reply_t *modmap_r;
7387 xcb_keycode_t *modmap, kc, *keycode;
7388
7389 numlockmask = 0;
7390
7391 modmap_r = xcb_get_modifier_mapping_reply(conn,
7392 xcb_get_modifier_mapping(conn),
7393 NULL);
7394 if (modmap_r) {
7395 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
7396 for (i = 0; i < 8; i++) {
7397 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
7398 kc = modmap[i * modmap_r->keycodes_per_modifier
7399 + j];
7400 keycode = xcb_key_symbols_get_keycode(syms,
7401 XK_Num_Lock);
7402 if (keycode) {
7403 if (kc == *keycode)
7404 numlockmask = (1 << i);
7405 free(keycode);
7406 }
7407 }
7408 }
7409 free(modmap_r);
7410 }
7411 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
7412 }
7413
7414 void
7415 grabkeys(void)
7416 {
7417 struct key *kp;
7418 int num_screens, k, j;
7419 unsigned int modifiers[4];
7420 xcb_keycode_t *code;
7421
7422 DNPRINTF(SWM_D_MISC, "grabkeys\n");
7423 updatenumlockmask();
7424
7425 modifiers[0] = 0;
7426 modifiers[1] = numlockmask;
7427 modifiers[2] = XCB_MOD_MASK_LOCK;
7428 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
7429
7430 num_screens = get_screen_count();
7431 for (k = 0; k < num_screens; k++) {
7432 if (TAILQ_EMPTY(&screens[k].rl))
7433 continue;
7434 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
7435 XCB_MOD_MASK_ANY);
7436 RB_FOREACH(kp, key_tree, &keys) {
7437 /* Skip unused ws binds. */
7438 if ((int)kp->funcid > KF_WS_1 + workspace_limit - 1 &&
7439 kp->funcid <= KF_WS_22)
7440 continue;
7441
7442 /* Skip unused mvws binds. */
7443 if ((int)kp->funcid > KF_MVWS_1 + workspace_limit - 1 &&
7444 kp->funcid <= KF_MVWS_22)
7445 continue;
7446
7447 if ((code = xcb_key_symbols_get_keycode(syms,
7448 kp->keysym))) {
7449 for (j = 0; j < LENGTH(modifiers); j++)
7450 xcb_grab_key(conn, 1,
7451 screens[k].root,
7452 kp->mod | modifiers[j],
7453 *code, XCB_GRAB_MODE_SYNC,
7454 XCB_GRAB_MODE_SYNC);
7455 free(code);
7456 }
7457 }
7458 }
7459 }
7460
7461 void
7462 grabbuttons(struct ws_win *win)
7463 {
7464 unsigned int modifiers[4];
7465 int i, j;
7466
7467 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win %#x\n", win->id);
7468 updatenumlockmask();
7469
7470 modifiers[0] = 0;
7471 modifiers[1] = numlockmask;
7472 modifiers[2] = XCB_MOD_MASK_LOCK;
7473 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
7474
7475 for (i = 0; i < LENGTH(buttons); i++)
7476 if (buttons[i].action == client_click)
7477 for (j = 0; j < LENGTH(modifiers); ++j)
7478 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
7479 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
7480 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
7481 buttons[i].button, buttons[i].mask |
7482 modifiers[j]);
7483 }
7484
7485 const char *quirkname[] = {
7486 "NONE", /* config string for "no value" */
7487 "FLOAT",
7488 "TRANSSZ",
7489 "ANYWHERE",
7490 "XTERM_FONTADJ",
7491 "FULLSCREEN",
7492 "FOCUSPREV",
7493 "NOFOCUSONMAP",
7494 "FOCUSONMAP_SINGLE",
7495 };
7496
7497 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
7498 #define SWM_Q_WS "\n|+ \t"
7499 int
7500 parsequirks(const char *qstr, unsigned long *quirk)
7501 {
7502 char *str, *cp, *name;
7503 int i;
7504
7505 if (quirk == NULL || qstr == NULL)
7506 return (1);
7507
7508 if ((str = strdup(qstr)) == NULL)
7509 err(1, "parsequirks: strdup");
7510
7511 cp = str;
7512 *quirk = 0;
7513 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
7514 if (cp)
7515 cp += (long)strspn(cp, SWM_Q_WS);
7516 for (i = 0; i < LENGTH(quirkname); i++) {
7517 if (strncasecmp(name, quirkname[i],
7518 SWM_QUIRK_LEN) == 0) {
7519 DNPRINTF(SWM_D_QUIRK,
7520 "parsequirks: %s\n", name);
7521 if (i == 0) {
7522 *quirk = 0;
7523 free(str);
7524 return (0);
7525 }
7526 *quirk |= 1 << (i-1);
7527 break;
7528 }
7529 }
7530 if (i >= LENGTH(quirkname)) {
7531 DNPRINTF(SWM_D_QUIRK,
7532 "parsequirks: invalid quirk [%s]\n", name);
7533 free(str);
7534 return (1);
7535 }
7536 }
7537
7538 free(str);
7539 return (0);
7540 }
7541
7542 void
7543 quirk_insert(const char *class, const char *instance, const char *name,
7544 unsigned long quirk)
7545 {
7546 struct quirk *qp;
7547 int failed = 0;
7548 char *str;
7549
7550 DNPRINTF(SWM_D_QUIRK, "quirk_insert: class: %s, instance: %s, name: %s,"
7551 " value: %lu\n", class, instance, name, quirk);
7552
7553 if ((qp = malloc(sizeof *qp)) == NULL)
7554 err(1, "quirk_insert: malloc");
7555
7556 if ((qp->class = strdup(class)) == NULL)
7557 err(1, "quirk_insert: strdup");
7558 if ((qp->instance = strdup(instance)) == NULL)
7559 err(1, "quirk_insert: strdup");
7560 if ((qp->name = strdup(name)) == NULL)
7561 err(1, "quirk_insert: strdup");
7562
7563 if (asprintf(&str, "^%s$", class) == -1)
7564 err(1, "quirk_insert: asprintf");
7565 if (regcomp(&qp->regex_class, str, REG_EXTENDED | REG_NOSUB)) {
7566 add_startup_exception("regex failed to compile quirk 'class' "
7567 "field: %s", class);
7568 failed = 1;
7569 }
7570 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7571 free(str);
7572
7573 if (asprintf(&str, "^%s$", instance) == -1)
7574 err(1, "quirk_insert: asprintf");
7575 if (regcomp(&qp->regex_instance, str, REG_EXTENDED | REG_NOSUB)) {
7576 add_startup_exception("regex failed to compile quirk 'instance'"
7577 " field: %s", instance);
7578 failed = 1;
7579 }
7580 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7581 free(str);
7582
7583 if (asprintf(&str, "^%s$", name) == -1)
7584 err(1, "quirk_insert: asprintf");
7585 if (regcomp(&qp->regex_name, str, REG_EXTENDED | REG_NOSUB)) {
7586 add_startup_exception("regex failed to compile quirk 'name' "
7587 "field: %s", name);
7588 failed = 1;
7589 }
7590 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7591 free(str);
7592
7593 if (failed) {
7594 DNPRINTF(SWM_D_QUIRK, "quirk_insert: regex error; skipping.\n");
7595 quirk_free(qp);
7596 } else {
7597 qp->quirk = quirk;
7598 TAILQ_INSERT_TAIL(&quirks, qp, entry);
7599 }
7600 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
7601 }
7602
7603 void
7604 quirk_remove(struct quirk *qp)
7605 {
7606 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
7607 qp->name, qp->quirk);
7608
7609 TAILQ_REMOVE(&quirks, qp, entry);
7610 quirk_free(qp);
7611
7612 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
7613 }
7614
7615 void
7616 quirk_free(struct quirk *qp)
7617 {
7618 regfree(&qp->regex_class);
7619 regfree(&qp->regex_instance);
7620 regfree(&qp->regex_name);
7621 free(qp->class);
7622 free(qp->instance);
7623 free(qp->name);
7624 free(qp);
7625 }
7626
7627 void
7628 quirk_replace(struct quirk *qp, const char *class, const char *instance,
7629 const char *name, unsigned long quirk)
7630 {
7631 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s:%s [%lu]\n", qp->class,
7632 qp->instance, qp->name, qp->quirk);
7633
7634 quirk_remove(qp);
7635 quirk_insert(class, instance, name, quirk);
7636
7637 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
7638 }
7639
7640 void
7641 setquirk(const char *class, const char *instance, const char *name,
7642 unsigned long quirk)
7643 {
7644 struct quirk *qp;
7645
7646 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s:%s [%lu]\n", class,
7647 instance, name, quirk);
7648
7649 /* Remove/replace existing quirk. */
7650 TAILQ_FOREACH(qp, &quirks, entry) {
7651 if (strcmp(qp->class, class) == 0 &&
7652 strcmp(qp->instance, instance) == 0 &&
7653 strcmp(qp->name, name) == 0) {
7654 if (quirk == 0)
7655 quirk_remove(qp);
7656 else
7657 quirk_replace(qp, class, instance, name, quirk);
7658 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
7659 return;
7660 }
7661 }
7662
7663 /* Only insert if quirk is not NONE. */
7664 if (quirk)
7665 quirk_insert(class, instance, name, quirk);
7666
7667 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
7668 }
7669
7670 /* Eat '\' in str used to escape square brackets and colon. */
7671 void
7672 unescape_selector(char *str)
7673 {
7674 char *cp;
7675
7676 for (cp = str; *str != '\0'; ++str, ++cp) {
7677 if (*str == '\\' && (*(str + 1) == ':' || *(str + 1) == ']' ||
7678 *(str + 1) == '['))
7679 ++str;
7680
7681 *cp = *str;
7682 }
7683 *cp = '\0';
7684 }
7685
7686 int
7687 setconfquirk(const char *selector, const char *value, int flags)
7688 {
7689 char *str, *cp, *class;
7690 char *instance = NULL, *name = NULL;
7691 int retval, count = 0;
7692 unsigned long qrks;
7693
7694 /* suppress unused warning since var is needed */
7695 (void)flags;
7696
7697 if (selector == NULL || strlen(selector) == 0)
7698 return (0);
7699
7700 if ((str = strdup(selector)) == NULL)
7701 err(1, "setconfquirk: strdup");
7702
7703 /* Find non-escaped colon. */
7704 class = cp = str;
7705 if (*cp == ':') {
7706 *cp = '\0';
7707 ++count;
7708 }
7709
7710 for (++cp; *cp != '\0'; ++cp) {
7711 if (*cp == ':' && *(cp - 1) != '\\') {
7712 *cp = '\0';
7713 ++count;
7714 }
7715 }
7716
7717 unescape_selector(class);
7718 if (count) {
7719 instance = class + strlen(class) + 1;
7720 unescape_selector(instance);
7721 } else {
7722 instance = ".*";
7723 }
7724
7725 if (count > 1) {
7726 name = instance + strlen(instance) + 1;
7727 unescape_selector(name);
7728 } else {
7729 name = ".*";
7730 }
7731
7732 DNPRINTF(SWM_D_CONF, "setconfquirk: class: %s, instance: %s, "
7733 "name: %s\n", class, instance, name);
7734
7735 if ((retval = parsequirks(value, &qrks)) == 0)
7736 setquirk(class, instance, name, qrks);
7737
7738 free(str);
7739 return (retval);
7740 }
7741
7742 void
7743 setup_quirks(void)
7744 {
7745 setquirk("MPlayer", "xv", ".*", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
7746 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*", SWM_Q_FLOAT);
7747 setquirk("Firefox-bin", "firefox-bin", ".*", SWM_Q_TRANSSZ);
7748 setquirk("Firefox", "Dialog", ".*", SWM_Q_FLOAT);
7749 setquirk("Gimp", "gimp", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7750 setquirk("XTerm", "xterm", ".*", SWM_Q_XTERM_FONTADJ);
7751 setquirk("xine", "Xine Window", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7752 setquirk("Xitk", "Xitk Combo", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7753 setquirk("xine", "xine Panel", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7754 setquirk("Xitk", "Xine Window", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7755 setquirk("xine", "xine Video Fullscreen Window", ".*", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
7756 setquirk("pcb", "pcb", ".*", SWM_Q_FLOAT);
7757 setquirk("SDL_App", "SDL_App", ".*", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
7758 }
7759
7760 /* conf file stuff */
7761 #define SWM_CONF_FILE "spectrwm.conf"
7762 #define SWM_CONF_FILE_OLD "scrotwm.conf"
7763
7764 enum {
7765 SWM_S_BAR_ACTION,
7766 SWM_S_BAR_AT_BOTTOM,
7767 SWM_S_BAR_BORDER_WIDTH,
7768 SWM_S_BAR_DELAY,
7769 SWM_S_BAR_ENABLED,
7770 SWM_S_BAR_ENABLED_WS,
7771 SWM_S_BAR_FONT,
7772 SWM_S_BAR_FORMAT,
7773 SWM_S_BAR_JUSTIFY,
7774 SWM_S_BORDER_WIDTH,
7775 SWM_S_BOUNDARY_WIDTH,
7776 SWM_S_CLOCK_ENABLED,
7777 SWM_S_CLOCK_FORMAT,
7778 SWM_S_CYCLE_EMPTY,
7779 SWM_S_CYCLE_VISIBLE,
7780 SWM_S_DIALOG_RATIO,
7781 SWM_S_DISABLE_BORDER,
7782 SWM_S_FOCUS_CLOSE,
7783 SWM_S_FOCUS_CLOSE_WRAP,
7784 SWM_S_FOCUS_DEFAULT,
7785 SWM_S_FOCUS_MODE,
7786 SWM_S_ICONIC_ENABLED,
7787 SWM_S_REGION_PADDING,
7788 SWM_S_SPAWN_ORDER,
7789 SWM_S_SPAWN_TERM,
7790 SWM_S_SS_APP,
7791 SWM_S_SS_ENABLED,
7792 SWM_S_STACK_ENABLED,
7793 SWM_S_TERM_WIDTH,
7794 SWM_S_TILE_GAP,
7795 SWM_S_URGENT_ENABLED,
7796 SWM_S_VERBOSE_LAYOUT,
7797 SWM_S_WINDOW_CLASS_ENABLED,
7798 SWM_S_WINDOW_INSTANCE_ENABLED,
7799 SWM_S_WINDOW_NAME_ENABLED,
7800 SWM_S_WORKSPACE_LIMIT,
7801 SWM_S_WORKSPACE_NAME,
7802 };
7803
7804 int
7805 setconfvalue(const char *selector, const char *value, int flags)
7806 {
7807 struct workspace *ws;
7808 int i, ws_id, num_screens;
7809 char *b, *str, s[1024];
7810
7811 switch (flags) {
7812 case SWM_S_BAR_ACTION:
7813 free(bar_argv[0]);
7814 if ((bar_argv[0] = expand_tilde(value)) == NULL)
7815 err(1, "setconfvalue: bar_action");
7816 break;
7817 case SWM_S_BAR_AT_BOTTOM:
7818 bar_at_bottom = atoi(value);
7819 break;
7820 case SWM_S_BAR_BORDER_WIDTH:
7821 bar_border_width = atoi(value);
7822 if (bar_border_width < 0)
7823 bar_border_width = 0;
7824 break;
7825 case SWM_S_BAR_DELAY:
7826 /* No longer needed; leave to not break old conf files. */
7827 break;
7828 case SWM_S_BAR_ENABLED:
7829 bar_enabled = atoi(value);
7830 break;
7831 case SWM_S_BAR_ENABLED_WS:
7832 ws_id = atoi(selector) - 1;
7833 if (ws_id < 0 || ws_id >= workspace_limit)
7834 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7835 "workspace %d.", ws_id + 1);
7836
7837 num_screens = get_screen_count();
7838 for (i = 0; i < num_screens; i++) {
7839 ws = (struct workspace *)&screens[i].ws;
7840 ws[ws_id].bar_enabled = atoi(value);
7841 }
7842 break;
7843 case SWM_S_BAR_FONT:
7844 b = bar_fonts;
7845 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
7846 err(1, "setconfvalue: asprintf: failed to allocate "
7847 "memory for bar_fonts.");
7848 free(b);
7849
7850 /* If already in xft mode, then we are done. */
7851 if (!bar_font_legacy)
7852 break;
7853
7854 if ((str = strdup(value)) == NULL)
7855 err(1, "setconfvalue: strdup");
7856
7857 /* If there are any non-XLFD entries, switch to Xft mode. */
7858 while ((b = strsep(&str, ",")) != NULL) {
7859 if (*b == '\0')
7860 continue;
7861 if (!isxlfd(b)) {
7862 bar_font_legacy = 0;
7863 break;
7864 }
7865 }
7866
7867 free(str);
7868 break;
7869 case SWM_S_BAR_FORMAT:
7870 free(bar_format);
7871 if ((bar_format = strdup(value)) == NULL)
7872 err(1, "setconfvalue: bar_format");
7873 break;
7874 case SWM_S_BAR_JUSTIFY:
7875 if (strcmp(value, "left") == 0)
7876 bar_justify = SWM_BAR_JUSTIFY_LEFT;
7877 else if (strcmp(value, "center") == 0)
7878 bar_justify = SWM_BAR_JUSTIFY_CENTER;
7879 else if (strcmp(value, "right") == 0)
7880 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
7881 else
7882 errx(1, "invalid bar_justify");
7883 break;
7884 case SWM_S_BORDER_WIDTH:
7885 border_width = atoi(value);
7886 if (border_width < 0)
7887 border_width = 0;
7888 break;
7889 case SWM_S_BOUNDARY_WIDTH:
7890 boundary_width = atoi(value);
7891 if (boundary_width < 0)
7892 boundary_width = 0;
7893 break;
7894 case SWM_S_CLOCK_ENABLED:
7895 clock_enabled = atoi(value);
7896 break;
7897 case SWM_S_CLOCK_FORMAT:
7898 #ifndef SWM_DENY_CLOCK_FORMAT
7899 free(clock_format);
7900 if ((clock_format = strdup(value)) == NULL)
7901 err(1, "setconfvalue: clock_format");
7902 #endif
7903 break;
7904 case SWM_S_CYCLE_EMPTY:
7905 cycle_empty = atoi(value);
7906 break;
7907 case SWM_S_CYCLE_VISIBLE:
7908 cycle_visible = atoi(value);
7909 break;
7910 case SWM_S_DIALOG_RATIO:
7911 dialog_ratio = atof(value);
7912 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
7913 dialog_ratio = .6;
7914 break;
7915 case SWM_S_DISABLE_BORDER:
7916 disable_border = atoi(value);
7917 break;
7918 case SWM_S_FOCUS_CLOSE:
7919 if (strcmp(value, "first") == 0)
7920 focus_close = SWM_STACK_BOTTOM;
7921 else if (strcmp(value, "last") == 0)
7922 focus_close = SWM_STACK_TOP;
7923 else if (strcmp(value, "next") == 0)
7924 focus_close = SWM_STACK_ABOVE;
7925 else if (strcmp(value, "previous") == 0)
7926 focus_close = SWM_STACK_BELOW;
7927 else
7928 errx(1, "focus_close");
7929 break;
7930 case SWM_S_FOCUS_CLOSE_WRAP:
7931 focus_close_wrap = atoi(value);
7932 break;
7933 case SWM_S_FOCUS_DEFAULT:
7934 if (strcmp(value, "last") == 0)
7935 focus_default = SWM_STACK_TOP;
7936 else if (strcmp(value, "first") == 0)
7937 focus_default = SWM_STACK_BOTTOM;
7938 else
7939 errx(1, "focus_default");
7940 break;
7941 case SWM_S_FOCUS_MODE:
7942 if (strcmp(value, "default") == 0)
7943 focus_mode = SWM_FOCUS_DEFAULT;
7944 else if (strcmp(value, "follow") == 0 ||
7945 strcmp(value, "follow_cursor") == 0)
7946 focus_mode = SWM_FOCUS_FOLLOW;
7947 else if (strcmp(value, "manual") == 0)
7948 focus_mode = SWM_FOCUS_MANUAL;
7949 else
7950 errx(1, "focus_mode");
7951 break;
7952 case SWM_S_ICONIC_ENABLED:
7953 iconic_enabled = atoi(value);
7954 break;
7955 case SWM_S_REGION_PADDING:
7956 region_padding = atoi(value);
7957 if (region_padding < 0)
7958 region_padding = 0;
7959 break;
7960 case SWM_S_SPAWN_ORDER:
7961 if (strcmp(value, "first") == 0)
7962 spawn_position = SWM_STACK_BOTTOM;
7963 else if (strcmp(value, "last") == 0)
7964 spawn_position = SWM_STACK_TOP;
7965 else if (strcmp(value, "next") == 0)
7966 spawn_position = SWM_STACK_ABOVE;
7967 else if (strcmp(value, "previous") == 0)
7968 spawn_position = SWM_STACK_BELOW;
7969 else
7970 errx(1, "spawn_position");
7971 break;
7972 case SWM_S_SPAWN_TERM:
7973 setconfspawn("term", value, 0);
7974 setconfspawn("spawn_term", value, 0);
7975 break;
7976 case SWM_S_SS_APP:
7977 /* No longer needed; leave to not break old conf files. */
7978 break;
7979 case SWM_S_SS_ENABLED:
7980 /* No longer needed; leave to not break old conf files. */
7981 break;
7982 case SWM_S_STACK_ENABLED:
7983 stack_enabled = atoi(value);
7984 break;
7985 case SWM_S_TERM_WIDTH:
7986 term_width = atoi(value);
7987 if (term_width < 0)
7988 term_width = 0;
7989 break;
7990 case SWM_S_TILE_GAP:
7991 tile_gap = atoi(value);
7992 break;
7993 case SWM_S_URGENT_ENABLED:
7994 urgent_enabled = atoi(value);
7995 break;
7996 case SWM_S_VERBOSE_LAYOUT:
7997 verbose_layout = atoi(value);
7998 for (i = 0; layouts[i].l_stack != NULL; i++) {
7999 if (verbose_layout)
8000 layouts[i].l_string = fancy_stacker;
8001 else
8002 layouts[i].l_string = plain_stacker;
8003 }
8004 break;
8005 case SWM_S_WINDOW_CLASS_ENABLED:
8006 window_class_enabled = atoi(value);
8007 break;
8008 case SWM_S_WINDOW_INSTANCE_ENABLED:
8009 window_instance_enabled = atoi(value);
8010 break;
8011 case SWM_S_WINDOW_NAME_ENABLED:
8012 window_name_enabled = atoi(value);
8013 break;
8014 case SWM_S_WORKSPACE_LIMIT:
8015 workspace_limit = atoi(value);
8016 if (workspace_limit > SWM_WS_MAX)
8017 workspace_limit = SWM_WS_MAX;
8018 else if (workspace_limit < 1)
8019 workspace_limit = 1;
8020
8021 ewmh_update_desktops();
8022 break;
8023 case SWM_S_WORKSPACE_NAME:
8024 if (getenv("SWM_STARTED") != NULL)
8025 return (0);
8026
8027 bzero(s, sizeof s);
8028 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
8029 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
8030 ws_id--;
8031 if (ws_id < 0 || ws_id >= workspace_limit)
8032 errx(1, "setconfvalue: workspace_name: invalid "
8033 "workspace %d.", ws_id + 1);
8034
8035 num_screens = get_screen_count();
8036 for (i = 0; i < num_screens; ++i) {
8037 ws = (struct workspace *)&screens[i].ws;
8038
8039 if (strlen(s) > 0) {
8040 free(ws[ws_id].name);
8041 if ((ws[ws_id].name = strdup(s)) == NULL)
8042 err(1, "setconfvalue: workspace_name.");
8043
8044 ewmh_update_desktop_names();
8045 ewmh_get_desktop_names();
8046 }
8047 }
8048 break;
8049 default:
8050 return (1);
8051 }
8052 return (0);
8053 }
8054
8055 int
8056 setconfmodkey(const char *selector, const char *value, int flags)
8057 {
8058 /* suppress unused warnings since vars are needed */
8059 (void)selector;
8060 (void)flags;
8061
8062 if (strncasecmp(value, "Mod1", strlen("Mod1")) == 0)
8063 update_modkey(XCB_MOD_MASK_1);
8064 else if (strncasecmp(value, "Mod2", strlen("Mod2")) == 0)
8065 update_modkey(XCB_MOD_MASK_2);
8066 else if (strncasecmp(value, "Mod3", strlen("Mod3")) == 0)
8067 update_modkey(XCB_MOD_MASK_3);
8068 else if (strncasecmp(value, "Mod4", strlen("Mod4")) == 0)
8069 update_modkey(XCB_MOD_MASK_4);
8070 else
8071 return (1);
8072 return (0);
8073 }
8074
8075 int
8076 setconfcolor(const char *selector, const char *value, int flags)
8077 {
8078 setscreencolor(value,
8079 (selector == NULL || strlen(selector) == 0) ? -1 : atoi(selector),
8080 flags);
8081 return (0);
8082 }
8083
8084 int
8085 setconfregion(const char *selector, const char *value, int flags)
8086 {
8087 /* suppress unused warnings since vars are needed */
8088 (void)selector;
8089 (void)flags;
8090
8091 custom_region(value);
8092 return (0);
8093 }
8094
8095 int
8096 setautorun(const char *selector, const char *value, int flags)
8097 {
8098 int ws_id;
8099 char s[1024];
8100 char *ap, *sp;
8101 union arg a;
8102 int argc = 0;
8103 pid_t pid;
8104 struct pid_e *p;
8105
8106 /* suppress unused warnings since vars are needed */
8107 (void)selector;
8108 (void)flags;
8109
8110 if (getenv("SWM_STARTED"))
8111 return (0);
8112
8113 bzero(s, sizeof s);
8114 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
8115 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
8116 ws_id--;
8117 if (ws_id < 0 || ws_id >= workspace_limit)
8118 errx(1, "autorun: invalid workspace %d", ws_id + 1);
8119
8120 sp = expand_tilde((char *)&s);
8121
8122 /*
8123 * This is a little intricate
8124 *
8125 * If the pid already exists we simply reuse it because it means it was
8126 * used before AND not claimed by manage_window. We get away with
8127 * altering it in the parent after INSERT because this can not be a race
8128 */
8129 a.argv = NULL;
8130 while ((ap = strsep(&sp, " \t")) != NULL) {
8131 if (*ap == '\0')
8132 continue;
8133 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
8134 argc++;
8135 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
8136 err(1, "setautorun: realloc");
8137 a.argv[argc - 1] = ap;
8138 }
8139 free(sp);
8140
8141 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
8142 err(1, "setautorun: realloc");
8143 a.argv[argc] = NULL;
8144
8145 if ((pid = fork()) == 0) {
8146 spawn(ws_id, &a, 1);
8147 /* NOTREACHED */
8148 _exit(1);
8149 }
8150 free(a.argv);
8151
8152 /* parent */
8153 p = find_pid(pid);
8154 if (p == NULL) {
8155 p = calloc(1, sizeof *p);
8156 if (p == NULL)
8157 return (1);
8158 TAILQ_INSERT_TAIL(&pidlist, p, entry);
8159 }
8160
8161 p->pid = pid;
8162 p->ws = ws_id;
8163
8164 return (0);
8165 }
8166
8167 int
8168 setlayout(const char *selector, const char *value, int flags)
8169 {
8170 int ws_id, i, x, mg, ma, si, ar, f = 0;
8171 int st = SWM_V_STACK, num_screens;
8172 char s[1024];
8173 struct workspace *ws;
8174
8175 /* suppress unused warnings since vars are needed */
8176 (void)selector;
8177 (void)flags;
8178
8179 if (getenv("SWM_STARTED"))
8180 return (0);
8181
8182 bzero(s, sizeof s);
8183 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
8184 &ws_id, &mg, &ma, &si, &ar, s) != 6)
8185 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8186 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8187 "<type>'");
8188 ws_id--;
8189 if (ws_id < 0 || ws_id >= workspace_limit)
8190 errx(1, "layout: invalid workspace %d", ws_id + 1);
8191
8192 if (strcasecmp(s, "vertical") == 0)
8193 st = SWM_V_STACK;
8194 else if (strcasecmp(s, "vertical_flip") == 0) {
8195 st = SWM_V_STACK;
8196 f = 1;
8197 } else if (strcasecmp(s, "horizontal") == 0)
8198 st = SWM_H_STACK;
8199 else if (strcasecmp(s, "horizontal_flip") == 0) {
8200 st = SWM_H_STACK;
8201 f = 1;
8202 } else if (strcasecmp(s, "fullscreen") == 0)
8203 st = SWM_MAX_STACK;
8204 else
8205 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8206 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8207 "<type>'");
8208
8209 num_screens = get_screen_count();
8210 for (i = 0; i < num_screens; i++) {
8211 ws = (struct workspace *)&screens[i].ws;
8212 ws[ws_id].cur_layout = &layouts[st];
8213
8214 ws[ws_id].always_raise = ar;
8215 if (st == SWM_MAX_STACK)
8216 continue;
8217
8218 /* master grow */
8219 for (x = 0; x < abs(mg); x++) {
8220 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8221 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
8222 SWM_ARG_ID_MASTERSHRINK);
8223 stack();
8224 }
8225 /* master add */
8226 for (x = 0; x < abs(ma); x++) {
8227 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8228 ma >= 0 ? SWM_ARG_ID_MASTERADD :
8229 SWM_ARG_ID_MASTERDEL);
8230 stack();
8231 }
8232 /* stack inc */
8233 for (x = 0; x < abs(si); x++) {
8234 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8235 si >= 0 ? SWM_ARG_ID_STACKINC :
8236 SWM_ARG_ID_STACKDEC);
8237 stack();
8238 }
8239 /* Apply flip */
8240 if (f) {
8241 ws[ws_id].cur_layout->l_config(&ws[ws_id],
8242 SWM_ARG_ID_FLIPLAYOUT);
8243 stack();
8244 }
8245 }
8246
8247 focus_flush();
8248
8249 return (0);
8250 }
8251
8252 /* config options */
8253 struct config_option {
8254 char *optname;
8255 int (*func)(const char*, const char*, int);
8256 int funcflags;
8257 };
8258 struct config_option configopt[] = {
8259 { "autorun", setautorun, 0 },
8260 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
8261 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
8262 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
8263 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
8264 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
8265 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
8266 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
8267 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
8268 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
8269 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
8270 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
8271 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
8272 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
8273 { "bind", setconfbinding, 0 },
8274 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
8275 { "boundary_width", setconfvalue, SWM_S_BOUNDARY_WIDTH },
8276 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
8277 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
8278 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
8279 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
8280 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
8281 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
8282 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
8283 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
8284 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
8285 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
8286 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
8287 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
8288 { "iconic_enabled", setconfvalue, SWM_S_ICONIC_ENABLED },
8289 { "keyboard_mapping", setkeymapping, 0 },
8290 { "layout", setlayout, 0 },
8291 { "modkey", setconfmodkey, 0 },
8292 { "program", setconfspawn, 0 },
8293 { "quirk", setconfquirk, 0 },
8294 { "region", setconfregion, 0 },
8295 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
8296 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
8297 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
8298 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
8299 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
8300 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
8301 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
8302 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
8303 { "title_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED }, /* For backwards compat. */
8304 { "title_name_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED }, /* For backwards compat. */
8305 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
8306 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
8307 { "window_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED },
8308 { "window_instance_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED },
8309 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
8310 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
8311 { "name", setconfvalue, SWM_S_WORKSPACE_NAME },
8312 };
8313
8314 void
8315 _add_startup_exception(const char *fmt, va_list ap)
8316 {
8317 if (vasprintf(&startup_exception, fmt, ap) == -1)
8318 warn("%s: asprintf", __func__);
8319 }
8320
8321 void
8322 add_startup_exception(const char *fmt, ...)
8323 {
8324 va_list ap;
8325
8326 nr_exceptions++;
8327
8328 if (startup_exception)
8329 return;
8330
8331 /* force bar to be enabled due to exception */
8332 bar_enabled = 1;
8333
8334 va_start(ap, fmt);
8335 _add_startup_exception(fmt, ap);
8336 va_end(ap);
8337 }
8338
8339 int
8340 conf_load(const char *filename, int keymapping)
8341 {
8342 FILE *config;
8343 char *line = NULL, *cp, *ce, *optsub, *optval = NULL;
8344 size_t linelen, lineno = 0;
8345 int wordlen, i, optidx, count;
8346 struct config_option *opt = NULL;
8347
8348 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
8349
8350 if (filename == NULL) {
8351 warnx("conf_load: no filename");
8352 return (1);
8353 }
8354
8355 DNPRINTF(SWM_D_CONF, "conf_load: open %s\n", filename);
8356
8357 if ((config = fopen(filename, "r")) == NULL) {
8358 warn("conf_load: fopen: %s", filename);
8359 return (1);
8360 }
8361
8362 while (!feof(config)) {
8363 if (line)
8364 free(line);
8365
8366 if ((line = fparseln(config, &linelen, &lineno, NULL,
8367 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT)) == NULL) {
8368 if (ferror(config))
8369 err(1, "%s", filename);
8370 else
8371 continue;
8372 }
8373 cp = line;
8374 cp += strspn(cp, " \t\n"); /* eat whitespace */
8375 if (cp[0] == '\0') {
8376 /* empty line */
8377 continue;
8378 }
8379 /* get config option */
8380 wordlen = strcspn(cp, "=[ \t\n");
8381 if (wordlen == 0) {
8382 add_startup_exception("%s: line %zd: no option found",
8383 filename, lineno);
8384 continue;
8385 }
8386 optidx = -1;
8387 for (i = 0; i < LENGTH(configopt); i++) {
8388 opt = &configopt[i];
8389 if (strncasecmp(cp, opt->optname, wordlen) == 0 &&
8390 (int)strlen(opt->optname) == wordlen) {
8391 optidx = i;
8392 break;
8393 }
8394 }
8395 if (optidx == -1) {
8396 add_startup_exception("%s: line %zd: unknown option "
8397 "%.*s", filename, lineno, wordlen, cp);
8398 continue;
8399 }
8400 if (keymapping && opt && strcmp(opt->optname, "bind")) {
8401 add_startup_exception("%s: line %zd: invalid option "
8402 "%.*s", filename, lineno, wordlen, cp);
8403 continue;
8404 }
8405 cp += wordlen;
8406 cp += strspn(cp, " \t\n"); /* eat whitespace */
8407
8408 /* get [selector] if any */
8409 optsub = NULL;
8410 count = 0;
8411 if (*cp == '[') {
8412 ++count;
8413 /* Get length of selector. */
8414 for (ce = ++cp; *ce != '\0'; ++ce) {
8415 /* Find matching (not escaped) bracket. */
8416 if (*ce == ']' && *(ce - 1) != '\\') {
8417 --count;
8418 break;
8419 }
8420 }
8421
8422 if (count > 0) {
8423 add_startup_exception("%s: line %zd: syntax "
8424 "error: unterminated selector", filename,
8425 lineno);
8426 continue;
8427 }
8428
8429 /* ce points at ']'; terminate optsub. */
8430 *ce = '\0';
8431 optsub = cp;
8432 cp = ce + 1;
8433 }
8434 cp += strspn(cp, "= \t\n"); /* eat trailing */
8435 /* get RHS value */
8436 optval = cp;
8437 if (strlen(optval) == 0) {
8438 add_startup_exception("%s: line %zd: must supply value "
8439 "to %s", filename, lineno,
8440 configopt[optidx].optname);
8441 continue;
8442 }
8443 /* call function to deal with it all */
8444 if (configopt[optidx].func(optsub, optval,
8445 configopt[optidx].funcflags) != 0) {
8446 add_startup_exception("%s: line %zd: invalid data for "
8447 "%s", filename, lineno, configopt[optidx].optname);
8448 continue;
8449 }
8450 }
8451
8452 if (line)
8453 free(line);
8454 fclose(config);
8455 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
8456
8457 return (0);
8458 }
8459
8460 void
8461 set_child_transient(struct ws_win *win, xcb_window_t *trans)
8462 {
8463 struct ws_win *parent, *w;
8464 struct swm_region *r;
8465 struct workspace *ws;
8466 xcb_icccm_wm_hints_t wmh;
8467
8468 parent = find_window(win->transient);
8469 if (parent)
8470 parent->focus_child = win;
8471 else {
8472 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
8473 " for %#x trans %#x\n", win->id, win->transient);
8474
8475 r = root_to_region(win->s->root, SWM_CK_ALL);
8476 ws = r->ws;
8477 /* parent doen't exist in our window list */
8478 TAILQ_FOREACH(w, &ws->winlist, entry) {
8479 if (xcb_icccm_get_wm_hints_reply(conn,
8480 xcb_icccm_get_wm_hints(conn, w->id),
8481 &wmh, NULL) != 1) {
8482 warnx("can't get hints for %#x", w->id);
8483 continue;
8484 }
8485
8486 if (win->hints.window_group != wmh.window_group)
8487 continue;
8488
8489 w->focus_child = win;
8490 win->transient = w->id;
8491 *trans = w->id;
8492 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
8493 "transient to %#x\n", win->transient);
8494 break;
8495 }
8496 }
8497 }
8498
8499 pid_t
8500 window_get_pid(xcb_window_t win)
8501 {
8502 pid_t ret = 0;
8503 const char *errstr;
8504 xcb_atom_t apid;
8505 xcb_get_property_cookie_t pc;
8506 xcb_get_property_reply_t *pr;
8507
8508 apid = get_atom_from_string("_NET_WM_PID");
8509 if (apid == XCB_ATOM_NONE)
8510 goto tryharder;
8511
8512 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
8513 pr = xcb_get_property_reply(conn, pc, NULL);
8514 if (pr == NULL)
8515 goto tryharder;
8516 if (pr->type != XCB_ATOM_CARDINAL) {
8517 free(pr);
8518 goto tryharder;
8519 }
8520
8521 if (pr->type == apid && pr->format == 32)
8522 ret = *((pid_t *)xcb_get_property_value(pr));
8523 free(pr);
8524
8525 return (ret);
8526
8527 tryharder:
8528 apid = get_atom_from_string("_SWM_PID");
8529 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
8530 0, SWM_PROPLEN);
8531 pr = xcb_get_property_reply(conn, pc, NULL);
8532 if (pr == NULL)
8533 return (0);
8534 if (pr->type != apid) {
8535 free(pr);
8536 return (0);
8537 }
8538
8539 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
8540 free(pr);
8541
8542 return (ret);
8543 }
8544
8545 int
8546 get_swm_ws(xcb_window_t id)
8547 {
8548 int ws_idx = -1;
8549 char *prop = NULL;
8550 size_t proplen;
8551 const char *errstr;
8552 xcb_get_property_reply_t *gpr;
8553
8554 gpr = xcb_get_property_reply(conn,
8555 xcb_get_property(conn, 0, id, a_swm_ws,
8556 XCB_ATOM_STRING, 0, SWM_PROPLEN),
8557 NULL);
8558 if (gpr == NULL)
8559 return (-1);
8560 if (gpr->type) {
8561 proplen = xcb_get_property_value_length(gpr);
8562 if (proplen > 0) {
8563 prop = malloc(proplen + 1);
8564 if (prop) {
8565 memcpy(prop,
8566 xcb_get_property_value(gpr),
8567 proplen);
8568 prop[proplen] = '\0';
8569 }
8570 }
8571 }
8572 free(gpr);
8573
8574 if (prop) {
8575 DNPRINTF(SWM_D_PROP, "get_swm_ws: _SWM_WS: %s\n", prop);
8576 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
8577 if (errstr) {
8578 DNPRINTF(SWM_D_PROP, "get_swm_ws: win #%s: %s",
8579 errstr, prop);
8580 }
8581 free(prop);
8582 }
8583
8584 return ws_idx;
8585 }
8586
8587 int
8588 get_ws_idx(xcb_window_t id)
8589 {
8590 xcb_get_property_reply_t *gpr;
8591 int ws_idx = -1;
8592
8593 gpr = xcb_get_property_reply(conn,
8594 xcb_get_property(conn, 0, id, ewmh[_NET_WM_DESKTOP].atom,
8595 XCB_ATOM_CARDINAL, 0, 1),
8596 NULL);
8597 if (gpr) {
8598 if (gpr->type == XCB_ATOM_CARDINAL && gpr->format == 32)
8599 ws_idx = *((int *)xcb_get_property_value(gpr));
8600 free(gpr);
8601 }
8602
8603 if (ws_idx == -1)
8604 if ((ws_idx = get_swm_ws(id)) != -1)
8605 xcb_delete_property(conn, id, a_swm_ws);
8606
8607 if (ws_idx > workspace_limit - 1 || ws_idx < -1)
8608 ws_idx = -1;
8609
8610 DNPRINTF(SWM_D_PROP, "get_ws_idx: win %#x, ws_idx: %d\n", id, ws_idx);
8611
8612 return ws_idx;
8613 }
8614
8615 struct ws_win *
8616 manage_window(xcb_window_t id, int mapped)
8617 {
8618 struct ws_win *win, *ww;
8619 struct swm_region *r;
8620 struct pid_e *p;
8621 struct quirk *qp;
8622 xcb_get_geometry_reply_t *gr;
8623 xcb_window_t trans = XCB_WINDOW_NONE;
8624 uint32_t i, wa[2], new_flags;
8625 int ws_idx;
8626 char *class, *instance, *name;
8627
8628 if ((win = find_window(id)) != NULL) {
8629 DNPRINTF(SWM_D_MISC, "manage_window: win %#x already "
8630 "managed; skipping.)\n", id);
8631 return (win); /* Already managed. */
8632 }
8633
8634 /* See if window is on the unmanaged list. */
8635 if ((win = find_unmanaged_window(id)) != NULL) {
8636 DNPRINTF(SWM_D_MISC, "manage_window: win %#x found on "
8637 "unmanaged list.\n", id);
8638 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
8639
8640 if (TRANS(win))
8641 set_child_transient(win, &trans);
8642
8643 goto out;
8644 } else {
8645 DNPRINTF(SWM_D_MISC, "manage_window: win %#x is new.\n", id);
8646 }
8647
8648 /* Try to get initial window geometry. */
8649 gr = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, id), NULL);
8650 if (gr == NULL) {
8651 DNPRINTF(SWM_D_MISC, "manage_window: get geometry failed.\n");
8652 return (NULL);
8653 }
8654
8655 /* Create and initialize ws_win object. */
8656 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
8657 err(1, "manage_window: calloc: failed to allocate memory for "
8658 "new window");
8659
8660 win->id = id;
8661
8662 /* Figureout which region the window belongs to. */
8663 r = root_to_region(gr->root, SWM_CK_ALL);
8664
8665 /* Ignore window border if there is one. */
8666 WIDTH(win) = gr->width;
8667 HEIGHT(win) = gr->height;
8668 X(win) = gr->x + gr->border_width - border_width;
8669 Y(win) = gr->y + gr->border_width - border_width;
8670 win->bordered = 1;
8671 win->mapped = mapped;
8672 win->s = r->s; /* this never changes */
8673
8674 free(gr);
8675
8676 /* Select which X events to monitor and set border pixel color. */
8677 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
8678 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
8679 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
8680 #ifdef SWM_DEBUG
8681 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
8682 #endif
8683
8684 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
8685 XCB_CW_EVENT_MASK, wa);
8686
8687 /* Get WM_SIZE_HINTS. */
8688 xcb_icccm_get_wm_normal_hints_reply(conn,
8689 xcb_icccm_get_wm_normal_hints(conn, win->id),
8690 &win->sh, NULL);
8691
8692 /* Get WM_HINTS. */
8693 xcb_icccm_get_wm_hints_reply(conn,
8694 xcb_icccm_get_wm_hints(conn, win->id),
8695 &win->hints, NULL);
8696
8697 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
8698 xcb_icccm_get_wm_transient_for_reply(conn,
8699 xcb_icccm_get_wm_transient_for(conn, win->id),
8700 &trans, NULL);
8701 if (trans) {
8702 win->transient = trans;
8703 set_child_transient(win, &win->transient);
8704 }
8705
8706 /* Get WM_PROTOCOLS. */
8707 get_wm_protocols(win);
8708
8709 /* Figure out which workspace the window belongs to. */
8710 if ((p = find_pid(window_get_pid(win->id))) != NULL) {
8711 win->ws = &r->s->ws[p->ws];
8712 TAILQ_REMOVE(&pidlist, p, entry);
8713 free(p);
8714 p = NULL;
8715 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
8716 !TRANS(win)) {
8717 /* _SWM_WS is set; use that. */
8718 win->ws = &r->s->ws[ws_idx];
8719 } else if (trans && (ww = find_window(trans)) != NULL) {
8720 /* Launch transients in the same ws as parent. */
8721 win->ws = ww->ws;
8722 } else {
8723 win->ws = r->ws;
8724 }
8725
8726 /* Set the _NET_WM_DESKTOP atom. */
8727 DNPRINTF(SWM_D_PROP, "manage_window: set _NET_WM_DESKTOP: %d\n",
8728 win->ws->idx);
8729 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
8730 ewmh[_NET_WM_DESKTOP].atom, XCB_ATOM_CARDINAL, 32, 1, &win->ws->idx);
8731
8732 /* WS must already be set for this to work. */
8733 store_float_geom(win);
8734
8735 /* Set initial quirks based on EWMH. */
8736 ewmh_autoquirk(win);
8737
8738 /* Determine initial quirks. */
8739 xcb_icccm_get_wm_class_reply(conn,
8740 xcb_icccm_get_wm_class(conn, win->id),
8741 &win->ch, NULL);
8742
8743 class = win->ch.class_name ? win->ch.class_name : "";
8744 instance = win->ch.instance_name ? win->ch.instance_name : "";
8745 name = get_win_name(win->id);
8746
8747 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, instance: %s, "
8748 "name: %s\n", class, instance, name);
8749
8750 /* java is retarded so treat it special */
8751 if (strstr(instance, "sun-awt")) {
8752 DNPRINTF(SWM_D_CLASS, "manage_window: java window detected.\n");
8753 win->java = 1;
8754 }
8755
8756 TAILQ_FOREACH(qp, &quirks, entry) {
8757 if (regexec(&qp->regex_class, class, 0, NULL, 0) == 0 &&
8758 regexec(&qp->regex_instance, instance, 0, NULL, 0) == 0 &&
8759 regexec(&qp->regex_name, name, 0, NULL, 0) == 0) {
8760 DNPRINTF(SWM_D_CLASS, "manage_window: matched "
8761 "quirk: %s:%s:%s mask: %#lx\n", qp->class,
8762 qp->instance, qp->name, qp->quirk);
8763 win->quirks = qp->quirk;
8764 }
8765 }
8766
8767 free(name);
8768
8769 /* Reset font sizes (the bruteforce way; no default keybinding). */
8770 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
8771 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
8772 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
8773 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
8774 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
8775 }
8776
8777 /* Make sure window is positioned inside its region, if its active. */
8778 if (win->ws->r) {
8779 region_containment(win, r, SWM_CW_ALLSIDES |
8780 SWM_CW_HARDBOUNDARY);
8781 update_window(win);
8782 }
8783
8784 out:
8785 /* Figure out where to stack the window in the workspace. */
8786 if (trans && (ww = find_window(trans)))
8787 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
8788 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
8789 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
8790 entry);
8791 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
8792 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
8793 else switch (spawn_position) {
8794 default:
8795 case SWM_STACK_TOP:
8796 case SWM_STACK_ABOVE:
8797 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
8798 break;
8799 case SWM_STACK_BOTTOM:
8800 case SWM_STACK_BELOW:
8801 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
8802 }
8803
8804 ewmh_update_client_list();
8805
8806 TAILQ_INSERT_TAIL(&win->ws->stack, win, stack_entry);
8807
8808 /* Get/apply initial _NET_WM_STATE */
8809 ewmh_get_wm_state(win);
8810
8811 /* Apply quirks. */
8812 new_flags = win->ewmh_flags;
8813
8814 if (win->quirks & SWM_Q_FLOAT)
8815 new_flags |= EWMH_F_ABOVE;
8816
8817 if (win->quirks & SWM_Q_ANYWHERE)
8818 new_flags |= SWM_F_MANUAL;
8819
8820 ewmh_apply_flags(win, new_flags);
8821 ewmh_update_wm_state(win);
8822
8823 /* Set initial _NET_WM_ALLOWED_ACTIONS */
8824 ewmh_update_actions(win);
8825
8826 grabbuttons(win);
8827
8828 DNPRINTF(SWM_D_MISC, "manage_window: done. win %#x, (x,y) w x h: "
8829 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win->id,
8830 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
8831 YESNO(ICONIC(win)), win->transient);
8832
8833 return (win);
8834 }
8835
8836 void
8837 free_window(struct ws_win *win)
8838 {
8839 DNPRINTF(SWM_D_MISC, "free_window: win %#x\n", win->id);
8840
8841 if (win == NULL)
8842 return;
8843
8844 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
8845
8846 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
8847
8848 kill_refs(win);
8849
8850 /* paint memory */
8851 memset(win, 0xff, sizeof *win); /* XXX kill later */
8852
8853 free(win);
8854 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
8855 }
8856
8857 void
8858 unmanage_window(struct ws_win *win)
8859 {
8860 struct ws_win *parent;
8861
8862 if (win == NULL)
8863 return;
8864
8865 DNPRINTF(SWM_D_MISC, "unmanage_window: win %#x\n", win->id);
8866
8867 if (TRANS(win)) {
8868 parent = find_window(win->transient);
8869 if (parent)
8870 parent->focus_child = NULL;
8871 }
8872
8873 TAILQ_REMOVE(&win->ws->stack, win, stack_entry);
8874 TAILQ_REMOVE(&win->ws->winlist, win, entry);
8875 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
8876
8877 ewmh_update_client_list();
8878 }
8879
8880 void
8881 expose(xcb_expose_event_t *e)
8882 {
8883 int i, num_screens;
8884 struct swm_region *r;
8885
8886 DNPRINTF(SWM_D_EVENT, "expose: win %#x\n", e->window);
8887
8888 num_screens = get_screen_count();
8889 for (i = 0; i < num_screens; i++)
8890 TAILQ_FOREACH(r, &screens[i].rl, entry)
8891 if (e->window == WINID(r->bar))
8892 bar_draw();
8893
8894 xcb_flush(conn);
8895 }
8896
8897 #ifdef SWM_DEBUG
8898 void
8899 focusin(xcb_focus_in_event_t *e)
8900 {
8901 DNPRINTF(SWM_D_EVENT, "focusin: win %#x, mode: %s(%u), "
8902 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
8903 e->mode, get_notify_detail_label(e->detail), e->detail);
8904 }
8905
8906 void
8907 focusout(xcb_focus_out_event_t *e)
8908 {
8909 DNPRINTF(SWM_D_EVENT, "focusout: win %#x, mode: %s(%u), "
8910 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
8911 e->mode, get_notify_detail_label(e->detail), e->detail);
8912 }
8913 #endif
8914
8915 void
8916 keypress(xcb_key_press_event_t *e)
8917 {
8918 xcb_keysym_t keysym;
8919 struct key *kp;
8920
8921 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
8922
8923 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
8924 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
8925 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
8926 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
8927 e->child, e->state, YESNO(e->same_screen));
8928
8929 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
8930 goto out;
8931
8932 last_event_time = e->time;
8933
8934 if (kp->funcid == KF_SPAWN_CUSTOM)
8935 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
8936 &(keyfuncs[kp->funcid].args), kp->spawn_name);
8937 else if (keyfuncs[kp->funcid].func)
8938 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
8939 &(keyfuncs[kp->funcid].args));
8940
8941 out:
8942 /* Unfreeze grab events. */
8943 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD, e->time);
8944 xcb_flush(conn);
8945
8946 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
8947 }
8948
8949 void
8950 buttonpress(xcb_button_press_event_t *e)
8951 {
8952 struct ws_win *win = NULL;
8953 struct swm_region *r, *old_r;
8954 int i;
8955 int handled = 0;
8956
8957 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): %#x (%d,%d), "
8958 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
8959 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
8960 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
8961 e->state, YESNO(e->same_screen));
8962
8963 if (e->event == e->root) {
8964 if (e->child != 0) {
8965 win = find_window(e->child);
8966 /* Pass ButtonPress to window if it isn't managed. */
8967 if (win == NULL)
8968 goto out;
8969 } else {
8970 /* Focus on empty region */
8971 /* If no windows on region if its empty. */
8972 r = root_to_region(e->root, SWM_CK_POINTER);
8973 if (r == NULL) {
8974 DNPRINTF(SWM_D_EVENT, "buttonpress: "
8975 "NULL region; ignoring.\n");
8976 goto out;
8977 }
8978
8979 if (TAILQ_EMPTY(&r->ws->winlist)) {
8980 old_r = root_to_region(e->root, SWM_CK_FOCUS);
8981 if (old_r && old_r != r)
8982 unfocus_win(old_r->ws->focus);
8983
8984 xcb_set_input_focus(conn,
8985 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
8986
8987 /* Clear bar since empty. */
8988 bar_draw();
8989
8990 handled = 1;
8991 goto out;
8992 }
8993 }
8994 } else {
8995 win = find_window(e->event);
8996 }
8997
8998 if (win == NULL)
8999 goto out;
9000
9001 last_event_time = e->time;
9002
9003 focus_win(get_focus_magic(win));
9004
9005 for (i = 0; i < LENGTH(buttons); i++)
9006 if (client_click == buttons[i].action && buttons[i].func &&
9007 buttons[i].button == e->detail &&
9008 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
9009 buttons[i].func(win, &buttons[i].args);
9010 handled = 1;
9011 }
9012
9013 out:
9014 if (!handled) {
9015 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
9016 /* Replay event to event window */
9017 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
9018 } else {
9019 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
9020 /* Unfreeze grab events. */
9021 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
9022 }
9023
9024 xcb_flush(conn);
9025 }
9026
9027 #ifdef SWM_DEBUG
9028 void
9029 print_win_geom(xcb_window_t w)
9030 {
9031 xcb_get_geometry_reply_t *wa;
9032
9033 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
9034 if (wa == NULL) {
9035 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: %#x\n",
9036 w);
9037 return;
9038 }
9039
9040 DNPRINTF(SWM_D_MISC, "print_win_geom: win %#x, root: %#x, "
9041 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
9042 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
9043 wa->border_width);
9044
9045 free(wa);
9046 }
9047 #endif
9048
9049 #ifdef SWM_DEBUG
9050 char *
9051 get_stack_mode_name(uint8_t mode)
9052 {
9053 char *name;
9054
9055 switch(mode) {
9056 case XCB_STACK_MODE_ABOVE:
9057 name = "Above";
9058 break;
9059 case XCB_STACK_MODE_BELOW:
9060 name = "Below";
9061 break;
9062 case XCB_STACK_MODE_TOP_IF:
9063 name = "TopIf";
9064 break;
9065 case XCB_STACK_MODE_BOTTOM_IF:
9066 name = "BottomIf";
9067 break;
9068 case XCB_STACK_MODE_OPPOSITE:
9069 name = "Opposite";
9070 break;
9071 default:
9072 name = "Unknown";
9073 }
9074
9075 return name;
9076 }
9077 #endif
9078
9079 void
9080 configurerequest(xcb_configure_request_event_t *e)
9081 {
9082 struct ws_win *win;
9083 struct swm_region *r = NULL;
9084 int new = 0, i = 0;
9085 uint16_t mask = 0;
9086 uint32_t wc[7] = {0};
9087
9088 if ((win = find_window(e->window)) == NULL)
9089 if ((win = find_unmanaged_window(e->window)) == NULL)
9090 new = 1;
9091
9092 #ifdef SWM_DEBUG
9093 if (swm_debug & SWM_D_EVENT) {
9094 print_win_geom(e->window);
9095
9096 DNPRINTF(SWM_D_EVENT, "configurerequest: win %#x, "
9097 "parent: %#x, new: %s, value_mask: %u { ", e->window,
9098 e->parent, YESNO(new), e->value_mask);
9099 if (e->value_mask & XCB_CONFIG_WINDOW_X)
9100 DPRINTF("X: %d ", e->x);
9101 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
9102 DPRINTF("Y: %d ", e->y);
9103 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
9104 DPRINTF("W: %u ", e->width);
9105 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
9106 DPRINTF("H: %u ", e->height);
9107 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
9108 DPRINTF("Border: %u ", e->border_width);
9109 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
9110 DPRINTF("Sibling: %#x ", e->sibling);
9111 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
9112 DPRINTF("StackMode: %s(%u) ",
9113 get_stack_mode_name(e->stack_mode), e->stack_mode);
9114 DPRINTF("}\n");
9115 }
9116 #endif
9117
9118 if (new) {
9119 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
9120 mask |= XCB_CONFIG_WINDOW_X;
9121 wc[i++] = e->x;
9122 }
9123 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
9124 mask |= XCB_CONFIG_WINDOW_Y;
9125 wc[i++] = e->y;
9126 }
9127 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
9128 mask |= XCB_CONFIG_WINDOW_WIDTH;
9129 wc[i++] = e->width;
9130 }
9131 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
9132 mask |= XCB_CONFIG_WINDOW_HEIGHT;
9133 wc[i++] = e->height;
9134 }
9135 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
9136 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
9137 wc[i++] = e->border_width;
9138 }
9139 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
9140 mask |= XCB_CONFIG_WINDOW_SIBLING;
9141 wc[i++] = e->sibling;
9142 }
9143 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
9144 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
9145 wc[i++] = e->stack_mode;
9146 }
9147
9148 if (mask != 0) {
9149 xcb_configure_window(conn, e->window, mask, wc);
9150 xcb_flush(conn);
9151 }
9152 } else if ((!MANUAL(win) || win->quirks & SWM_Q_ANYWHERE) &&
9153 !FULLSCREEN(win) && !MAXIMIZED(win)) {
9154 if (win->ws->r)
9155 r = win->ws->r;
9156 else if (win->ws->old_r)
9157 r = win->ws->old_r;
9158
9159 /* windows are centered unless ANYWHERE quirk is set. */
9160 if (win->quirks & SWM_Q_ANYWHERE) {
9161 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
9162 win->g_float.x = e->x;
9163 if (r)
9164 win->g_float.x -= X(r);
9165 }
9166
9167 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
9168 win->g_float.y = e->y;
9169 if (r)
9170 win->g_float.y -= Y(r);
9171 }
9172 }
9173
9174 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
9175 win->g_float.w = e->width;
9176
9177 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
9178 win->g_float.h = e->height;
9179
9180 win->g_floatvalid = 1;
9181
9182 if (!MAXIMIZED(win) && !FULLSCREEN(win) &&
9183 (TRANS(win) || (ABOVE(win) &&
9184 win->ws->cur_layout != &layouts[SWM_MAX_STACK]))) {
9185 WIDTH(win) = win->g_float.w;
9186 HEIGHT(win) = win->g_float.h;
9187
9188 if (r != NULL) {
9189 update_floater(win);
9190 focus_flush();
9191 } else {
9192 config_win(win, e);
9193 xcb_flush(conn);
9194 }
9195 } else {
9196 config_win(win, e);
9197 xcb_flush(conn);
9198 }
9199 } else {
9200 config_win(win, e);
9201 xcb_flush(conn);
9202 }
9203
9204 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
9205 }
9206
9207 void
9208 configurenotify(xcb_configure_notify_event_t *e)
9209 {
9210 struct ws_win *win;
9211
9212 DNPRINTF(SWM_D_EVENT, "configurenotify: win %#x, event win: %#x, "
9213 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
9214 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
9215 e->width, e->height, e->border_width, e->above_sibling,
9216 YESNO(e->override_redirect));
9217
9218 win = find_window(e->window);
9219 if (win) {
9220 xcb_icccm_get_wm_normal_hints_reply(conn,
9221 xcb_icccm_get_wm_normal_hints(conn, win->id),
9222 &win->sh, NULL);
9223 adjust_font(win);
9224 if (font_adjusted) {
9225 stack();
9226 xcb_flush(conn);
9227 }
9228 }
9229 }
9230
9231 void
9232 destroynotify(xcb_destroy_notify_event_t *e)
9233 {
9234 struct ws_win *win;
9235
9236 DNPRINTF(SWM_D_EVENT, "destroynotify: win %#x\n", e->window);
9237
9238 if ((win = find_window(e->window)) == NULL) {
9239 if ((win = find_unmanaged_window(e->window)) == NULL)
9240 return;
9241 free_window(win);
9242 return;
9243 }
9244
9245 if (focus_mode != SWM_FOCUS_FOLLOW) {
9246 /* If we were focused, make sure we focus on something else. */
9247 if (win == win->ws->focus)
9248 win->ws->focus_pending = get_focus_prev(win);
9249 }
9250
9251 unmanage_window(win);
9252 stack();
9253
9254 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
9255 if (win->ws->focus_pending) {
9256 focus_win(win->ws->focus_pending);
9257 win->ws->focus_pending = NULL;
9258 } else if (win == win->ws->focus) {
9259 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
9260 win->ws->r->id, XCB_CURRENT_TIME);
9261 }
9262 }
9263
9264 free_window(win);
9265
9266 focus_flush();
9267 }
9268
9269 #ifdef SWM_DEBUG
9270 char *
9271 get_notify_detail_label(uint8_t detail)
9272 {
9273 char *label;
9274
9275 switch (detail) {
9276 case XCB_NOTIFY_DETAIL_ANCESTOR:
9277 label = "Ancestor";
9278 break;
9279 case XCB_NOTIFY_DETAIL_VIRTUAL:
9280 label = "Virtual";
9281 break;
9282 case XCB_NOTIFY_DETAIL_INFERIOR:
9283 label = "Inferior";
9284 break;
9285 case XCB_NOTIFY_DETAIL_NONLINEAR:
9286 label = "Nonlinear";
9287 break;
9288 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
9289 label = "NonlinearVirtual";
9290 break;
9291 case XCB_NOTIFY_DETAIL_POINTER:
9292 label = "Pointer";
9293 break;
9294 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
9295 label = "PointerRoot";
9296 break;
9297 case XCB_NOTIFY_DETAIL_NONE:
9298 label = "None";
9299 break;
9300 default:
9301 label = "Unknown";
9302 }
9303
9304 return label;
9305 }
9306
9307 char *
9308 get_notify_mode_label(uint8_t mode)
9309 {
9310 char *label;
9311
9312 switch (mode) {
9313 case XCB_NOTIFY_MODE_NORMAL:
9314 label = "Normal";
9315 break;
9316 case XCB_NOTIFY_MODE_GRAB:
9317 label = "Grab";
9318 break;
9319 case XCB_NOTIFY_MODE_UNGRAB:
9320 label = "Ungrab";
9321 break;
9322 case XCB_NOTIFY_MODE_WHILE_GRABBED:
9323 label = "WhileGrabbed";
9324 break;
9325 default:
9326 label = "Unknown";
9327 }
9328
9329 return label;
9330 }
9331 #endif
9332
9333 void
9334 enternotify(xcb_enter_notify_event_t *e)
9335 {
9336 struct ws_win *win;
9337 struct swm_region *r;
9338
9339 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): %#x "
9340 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9341 "child: %#x, same_screen_focus: %s, state: %d\n",
9342 e->time, e->event, e->event_x, e->event_y,
9343 get_notify_mode_label(e->mode), e->mode,
9344 get_notify_detail_label(e->detail), e->detail,
9345 e->root, e->root_x, e->root_y, e->child,
9346 YESNO(e->same_screen_focus), e->state);
9347
9348 if (focus_mode == SWM_FOCUS_MANUAL &&
9349 e->mode == XCB_NOTIFY_MODE_NORMAL) {
9350 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
9351 return;
9352 }
9353
9354 last_event_time = e->time;
9355
9356 if ((win = find_window(e->event)) == NULL) {
9357 if (e->event == e->root) {
9358 /* If no windows on pointer region, then focus root. */
9359 r = root_to_region(e->root, SWM_CK_POINTER);
9360 if (r == NULL) {
9361 DNPRINTF(SWM_D_EVENT, "enternotify: "
9362 "NULL region; ignoring.\n");
9363 return;
9364 }
9365
9366 focus_region(r);
9367 } else {
9368 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
9369 "ignoring\n");
9370 return;
9371 }
9372 } else {
9373 focus_win(get_focus_magic(win));
9374 }
9375
9376 xcb_flush(conn);
9377 }
9378
9379 #ifdef SWM_DEBUG
9380 void
9381 leavenotify(xcb_leave_notify_event_t *e)
9382 {
9383 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): %#x "
9384 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9385 "child: %#x, same_screen_focus: %s, state: %d\n",
9386 e->time, e->event, e->event_x, e->event_y,
9387 get_notify_mode_label(e->mode), e->mode,
9388 get_notify_detail_label(e->detail), e->detail,
9389 e->root, e->root_x, e->root_y, e->child,
9390 YESNO(e->same_screen_focus), e->state);
9391 }
9392 #endif
9393
9394 void
9395 mapnotify(xcb_map_notify_event_t *e)
9396 {
9397 struct ws_win *win, *parent = NULL;
9398 struct workspace *ws;
9399
9400 DNPRINTF(SWM_D_EVENT, "mapnotify: win %#x\n", e->window);
9401
9402 if ((win = manage_window(e->window, 1)) == NULL)
9403 return;
9404 ws = win->ws;
9405
9406 /* Need to know if win was mapped due to ws switch. */
9407 if (ws->state == SWM_WS_STATE_MAPPED) {
9408 if (ws->focus_pending && TRANS(ws->focus_pending))
9409 parent = find_window(win->transient);
9410
9411 /* If window's parent is maximized, don't clear it. */
9412 if ((parent == NULL) || !MAXIMIZED(parent))
9413 if (clear_maximized(ws) > 0)
9414 stack();
9415 }
9416
9417 win->mapped = 1;
9418 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
9419
9420 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
9421 if (ws->focus_pending == win) {
9422 focus_win(win);
9423 ws->focus_pending = NULL;
9424 focus_flush();
9425 }
9426 }
9427
9428 xcb_flush(conn);
9429 }
9430
9431 void
9432 mappingnotify(xcb_mapping_notify_event_t *e)
9433 {
9434 struct ws_win *w;
9435 int i, j, num_screens;
9436
9437 xcb_refresh_keyboard_mapping(syms, e);
9438
9439 if (e->request == XCB_MAPPING_KEYBOARD) {
9440 grabkeys();
9441
9442 /* Regrab buttons on all managed windows. */
9443 num_screens = get_screen_count();
9444 for (i = 0; i < num_screens; i++)
9445 for (j = 0; j < workspace_limit; j++)
9446 TAILQ_FOREACH(w, &screens[i].ws[j].winlist,
9447 entry)
9448 grabbuttons(w);
9449 }
9450 }
9451
9452 void
9453 maprequest(xcb_map_request_event_t *e)
9454 {
9455 struct ws_win *win, *w = NULL;
9456 xcb_get_window_attributes_reply_t *war;
9457
9458 DNPRINTF(SWM_D_EVENT, "maprequest: win %#x\n",
9459 e->window);
9460
9461 war = xcb_get_window_attributes_reply(conn,
9462 xcb_get_window_attributes(conn, e->window),
9463 NULL);
9464 if (war == NULL) {
9465 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
9466 goto out;
9467 }
9468
9469 if (war->override_redirect) {
9470 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
9471 "skipping.\n");
9472 goto out;
9473 }
9474
9475 win = manage_window(e->window,
9476 (war->map_state == XCB_MAP_STATE_VIEWABLE));
9477 if (win == NULL)
9478 goto out;
9479
9480 /* The new window should get focus; prepare. */
9481 if (focus_mode != SWM_FOCUS_FOLLOW &&
9482 !(win->quirks & SWM_Q_NOFOCUSONMAP) &&
9483 (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
9484 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
9485 win->hints.input))) {
9486 if (win->quirks & SWM_Q_FOCUSONMAP_SINGLE) {
9487 /* See if other wins of same type are already mapped. */
9488 TAILQ_FOREACH(w, &win->ws->winlist, entry) {
9489 if (w == win || !w->mapped)
9490 continue;
9491
9492 if (w->ch.class_name &&
9493 win->ch.class_name &&
9494 strcmp(w->ch.class_name,
9495 win->ch.class_name) == 0 &&
9496 w->ch.instance_name &&
9497 win->ch.instance_name &&
9498 strcmp(w->ch.instance_name,
9499 win->ch.instance_name) == 0)
9500 break;
9501 }
9502 }
9503
9504 if (w == NULL)
9505 win->ws->focus_pending = get_focus_magic(win);
9506 }
9507
9508 /* All windows need to be mapped if they are in the current workspace.*/
9509 if (win->ws->r)
9510 stack();
9511
9512 /* Ignore EnterNotify to handle the mapnotify without interference. */
9513 if (focus_mode == SWM_FOCUS_DEFAULT)
9514 event_drain(XCB_ENTER_NOTIFY);
9515 out:
9516 free(war);
9517 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
9518 }
9519
9520 void
9521 motionnotify(xcb_motion_notify_event_t *e)
9522 {
9523 struct swm_region *r = NULL;
9524 int i, num_screens;
9525
9526 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): %#x "
9527 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9528 "child: %#x, same_screen_focus: %s, state: %d\n",
9529 e->time, e->event, e->event_x, e->event_y,
9530 get_notify_detail_label(e->detail), e->detail,
9531 e->root, e->root_x, e->root_y, e->child,
9532 YESNO(e->same_screen), e->state);
9533
9534 last_event_time = e->time;
9535
9536 if (focus_mode == SWM_FOCUS_MANUAL)
9537 return;
9538
9539 num_screens = get_screen_count();
9540 for (i = 0; i < num_screens; i++)
9541 if (screens[i].root == e->root)
9542 break;
9543
9544 TAILQ_FOREACH(r, &screens[i].rl, entry)
9545 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
9546 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
9547 break;
9548
9549 focus_region(r);
9550 }
9551
9552 #ifdef SWM_DEBUG
9553 char *
9554 get_atom_name(xcb_atom_t atom)
9555 {
9556 char *name = NULL;
9557 #ifdef SWM_DEBUG_ATOM_NAMES
9558 /*
9559 * This should be disabled during most debugging since
9560 * xcb_get_* causes an xcb_flush.
9561 */
9562 size_t len;
9563 xcb_get_atom_name_reply_t *r;
9564
9565 r = xcb_get_atom_name_reply(conn,
9566 xcb_get_atom_name(conn, atom),
9567 NULL);
9568 if (r) {
9569 len = xcb_get_atom_name_name_length(r);
9570 if (len > 0) {
9571 name = malloc(len + 1);
9572 if (name) {
9573 memcpy(name, xcb_get_atom_name_name(r), len);
9574 name[len] = '\0';
9575 }
9576 }
9577 free(r);
9578 }
9579 #else
9580 (void)atom;
9581 #endif
9582 return (name);
9583 }
9584 #endif
9585
9586 void
9587 propertynotify(xcb_property_notify_event_t *e)
9588 {
9589 struct ws_win *win;
9590 struct workspace *ws;
9591 #ifdef SWM_DEBUG
9592 char *name;
9593
9594 name = get_atom_name(e->atom);
9595 DNPRINTF(SWM_D_EVENT, "propertynotify: win %#x, atom: %s(%u), "
9596 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
9597 e->state);
9598 free(name);
9599 #endif
9600 win = find_window(e->window);
9601 if (win == NULL)
9602 return;
9603
9604 ws = win->ws;
9605
9606 last_event_time = e->time;
9607
9608 if (e->atom == a_state) {
9609 /* State just changed, make sure it gets focused if mapped. */
9610 if (e->state == XCB_PROPERTY_NEW_VALUE) {
9611 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(ws)) {
9612 if (win->mapped &&
9613 ws->focus_pending == win) {
9614 focus_win(ws->focus_pending);
9615 ws->focus_pending = NULL;
9616 }
9617 }
9618 }
9619 } else if (e->atom == XCB_ATOM_WM_CLASS ||
9620 e->atom == XCB_ATOM_WM_NAME) {
9621 bar_draw();
9622 } else if (e->atom == a_prot) {
9623 get_wm_protocols(win);
9624 }
9625
9626 xcb_flush(conn);
9627 }
9628
9629 void
9630 unmapnotify(xcb_unmap_notify_event_t *e)
9631 {
9632 struct ws_win *win;
9633 struct workspace *ws;
9634
9635 DNPRINTF(SWM_D_EVENT, "unmapnotify: win %#x\n", e->window);
9636
9637 /* If we aren't managing the window, then ignore. */
9638 win = find_window(e->window);
9639 if (win == NULL || win->id != e->window)
9640 return;
9641
9642 /* Do nothing if already withdrawn. */
9643 if (!win->mapped && !ICONIC(win))
9644 return;
9645
9646 ws = win->ws;
9647 win->mapped = 0;
9648
9649 /* If win was focused, make sure to focus on something else. */
9650 if (win == ws->focus) {
9651 if (focus_mode != SWM_FOCUS_FOLLOW) {
9652 ws->focus_pending = get_focus_prev(win);
9653 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
9654 "focus_pending: %#x\n",
9655 WINID(ws->focus_pending));
9656 }
9657
9658 unfocus_win(win);
9659 }
9660
9661 if (ICONIC(win)) {
9662 /* Iconify. */
9663 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
9664 } else {
9665 /* Withdraw. */
9666 set_win_state(win, XCB_ICCCM_WM_STATE_WITHDRAWN);
9667 unmanage_window(win);
9668 }
9669
9670 if (ws->r)
9671 stack();
9672
9673 /* Update focus if ws is active. */
9674 if (WS_FOCUSED(ws)) {
9675 if (focus_mode == SWM_FOCUS_FOLLOW) {
9676 focus_win(get_pointer_win(ws->r->s->root));
9677 } else if (ws->focus_pending) {
9678 focus_win(ws->focus_pending);
9679 ws->focus_pending = NULL;
9680 } else if (ws->focus == NULL) {
9681 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
9682 ws->r->id, XCB_CURRENT_TIME);
9683 }
9684 }
9685
9686 focus_flush();
9687 }
9688
9689 void
9690 clientmessage(xcb_client_message_event_t *e)
9691 {
9692 struct ws_win *win;
9693 struct swm_region *r = NULL;
9694 union arg a;
9695 uint32_t val[2];
9696 int num_screens, i;
9697 xcb_map_request_event_t mre;
9698 #ifdef SWM_DEBUG
9699 char *name;
9700
9701 name = get_atom_name(e->type);
9702 DNPRINTF(SWM_D_EVENT, "clientmessage: win %#x, atom: %s(%u)\n",
9703 e->window, name, e->type);
9704 free(name);
9705 #endif
9706
9707 if (e->type == ewmh[_NET_CURRENT_DESKTOP].atom) {
9708 num_screens = get_screen_count();
9709 for (i = 0; i < num_screens; i++)
9710 if (screens[i].root == e->window) {
9711 r = screens[i].r_focus;
9712 break;
9713 }
9714
9715 if (r && e->data.data32[0] <
9716 (uint32_t)workspace_limit) {
9717 a.id = e->data.data32[0];
9718 switchws(r, &a);
9719 focus_flush();
9720 }
9721
9722 return;
9723 }
9724
9725 win = find_window(e->window);
9726 if (win == NULL) {
9727 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
9728 /* Manage the window with maprequest. */
9729 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
9730 "unmanaged window.\n");
9731 mre.window = e->window;
9732 maprequest(&mre);
9733 }
9734 return;
9735 }
9736
9737 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
9738 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
9739 if (WS_FOCUSED(win->ws))
9740 focus_win(win);
9741 else
9742 win->ws->focus_pending = win;
9743 } else if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
9744 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
9745 if (win->can_delete)
9746 client_msg(win, a_delete, 0);
9747 else
9748 xcb_kill_client(conn, win->id);
9749 } else if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
9750 DNPRINTF(SWM_D_EVENT,
9751 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
9752 if (ABOVE(win)) {
9753 if (e->data.data32[0] & (1<<8)) /* x */
9754 X(win) = e->data.data32[1];
9755 if (e->data.data32[0] & (1<<9)) /* y */
9756 Y(win) = e->data.data32[2];
9757 if (e->data.data32[0] & (1<<10)) /* width */
9758 WIDTH(win) = e->data.data32[3];
9759 if (e->data.data32[0] & (1<<11)) /* height */
9760 HEIGHT(win) = e->data.data32[4];
9761
9762 update_window(win);
9763 } else {
9764 /* Notify no change was made. */
9765 config_win(win, NULL);
9766 /* TODO: Change stack sizes */
9767 }
9768 } else if (e->type == ewmh[_NET_RESTACK_WINDOW].atom) {
9769 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_RESTACK_WINDOW\n");
9770 val[0] = e->data.data32[1]; /* Sibling window. */
9771 val[1] = e->data.data32[2]; /* Stack mode detail. */
9772
9773 xcb_configure_window(conn, win->id, XCB_CONFIG_WINDOW_SIBLING |
9774 XCB_CONFIG_WINDOW_STACK_MODE, val);
9775 } else if (e->type == ewmh[_NET_WM_STATE].atom) {
9776 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
9777 ewmh_change_wm_state(win, e->data.data32[1], e->data.data32[0]);
9778 if (e->data.data32[2])
9779 ewmh_change_wm_state(win, e->data.data32[2],
9780 e->data.data32[0]);
9781
9782 ewmh_update_wm_state(win);
9783 stack();
9784 } else if (e->type == ewmh[_NET_WM_DESKTOP].atom) {
9785 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_DESKTOP\n");
9786 r = win->ws->r;
9787
9788 win_to_ws(win, e->data.data32[0], 1);
9789
9790 /* Restack if either the source or destination ws is mapped. */
9791 if (r != NULL || win->ws->r != NULL) {
9792 if (FLOATING(win))
9793 load_float_geom(win);
9794
9795 stack();
9796 }
9797 }
9798
9799 focus_flush();
9800 }
9801
9802 void
9803 check_conn(void)
9804 {
9805 int errcode = xcb_connection_has_error(conn);
9806 #ifdef XCB_CONN_ERROR
9807 char *s;
9808 switch (errcode) {
9809 case XCB_CONN_ERROR:
9810 s = "Socket error, pipe error or other stream error.";
9811 break;
9812 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
9813 s = "Extension not supported.";
9814 break;
9815 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
9816 s = "Insufficient memory.";
9817 break;
9818 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
9819 s = "Request length was exceeded.";
9820 break;
9821 case XCB_CONN_CLOSED_PARSE_ERR:
9822 s = "Error parsing display string.";
9823 break;
9824 default:
9825 s = "Unknown error.";
9826 }
9827 if (errcode)
9828 errx(errcode, "X CONNECTION ERROR: %s", s);
9829 #else
9830 if (errcode)
9831 errx(errcode, "X CONNECTION ERROR");
9832 #endif
9833 }
9834
9835 int
9836 enable_wm(void)
9837 {
9838 int num_screens, i;
9839 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
9840 XCB_EVENT_MASK_ENTER_WINDOW;
9841 xcb_screen_t *sc;
9842 xcb_void_cookie_t wac;
9843 xcb_generic_error_t *error;
9844
9845 /* this causes an error if some other window manager is running */
9846 num_screens = get_screen_count();
9847 for (i = 0; i < num_screens; i++) {
9848 if ((sc = get_screen(i)) == NULL)
9849 errx(1, "ERROR: can't get screen %d.", i);
9850 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: %#x\n",
9851 i, sc->root);
9852 wac = xcb_change_window_attributes_checked(conn, sc->root,
9853 XCB_CW_EVENT_MASK, &val);
9854 if ((error = xcb_request_check(conn, wac))) {
9855 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
9856 error->error_code);
9857 free(error);
9858 return 1;
9859 }
9860
9861 /* click to focus on empty region */
9862 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
9863 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
9864 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
9865 }
9866
9867 return 0;
9868 }
9869
9870 void
9871 new_region(struct swm_screen *s, int x, int y, int w, int h)
9872 {
9873 struct swm_region *r = NULL, *n;
9874 struct workspace *ws = NULL;
9875 int i;
9876 uint32_t wa[1];
9877
9878 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
9879 s->idx, w, h, x, y);
9880
9881 /* remove any conflicting regions */
9882 n = TAILQ_FIRST(&s->rl);
9883 while (n) {
9884 r = n;
9885 n = TAILQ_NEXT(r, entry);
9886 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
9887 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
9888 if (r->ws->r != NULL)
9889 r->ws->old_r = r->ws->r;
9890 r->ws->r = NULL;
9891 bar_cleanup(r);
9892 xcb_destroy_window(conn, r->id);
9893 TAILQ_REMOVE(&s->rl, r, entry);
9894 TAILQ_INSERT_TAIL(&s->orl, r, entry);
9895 }
9896 }
9897
9898 /* search old regions for one to reuse */
9899
9900 /* size + location match */
9901 TAILQ_FOREACH(r, &s->orl, entry)
9902 if (X(r) == x && Y(r) == y &&
9903 HEIGHT(r) == h && WIDTH(r) == w)
9904 break;
9905
9906 /* size match */
9907 TAILQ_FOREACH(r, &s->orl, entry)
9908 if (HEIGHT(r) == h && WIDTH(r) == w)
9909 break;
9910
9911 if (r != NULL) {
9912 TAILQ_REMOVE(&s->orl, r, entry);
9913 /* try to use old region's workspace */
9914 if (r->ws->r == NULL)
9915 ws = r->ws;
9916 } else
9917 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
9918 err(1, "new_region: calloc: failed to allocate memory "
9919 "for screen");
9920
9921 /* if we don't have a workspace already, find one */
9922 if (ws == NULL) {
9923 for (i = 0; i < workspace_limit; i++)
9924 if (s->ws[i].r == NULL) {
9925 ws = &s->ws[i];
9926 break;
9927 }
9928 }
9929
9930 if (ws == NULL)
9931 errx(1, "new_region: no free workspaces");
9932
9933 if (ws->state == SWM_WS_STATE_HIDDEN)
9934 ws->state = SWM_WS_STATE_MAPPING;
9935
9936 X(r) = x;
9937 Y(r) = y;
9938 WIDTH(r) = w;
9939 HEIGHT(r) = h;
9940 r->s = s;
9941 r->ws = ws;
9942 r->ws_prior = NULL;
9943 ws->r = r;
9944 outputs++;
9945 TAILQ_INSERT_TAIL(&s->rl, r, entry);
9946
9947 /* Invisible region window to detect pointer events on empty regions. */
9948 r->id = xcb_generate_id(conn);
9949 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
9950 XCB_EVENT_MASK_POINTER_MOTION_HINT;
9951
9952 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
9953 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
9954 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
9955
9956 /* Make sure region input is at the bottom. */
9957 wa[0] = XCB_STACK_MODE_BELOW;
9958 xcb_configure_window(conn, r->id, XCB_CONFIG_WINDOW_STACK_MODE, wa);
9959
9960 xcb_map_window(conn, r->id);
9961 }
9962
9963 void
9964 scan_xrandr(int i)
9965 {
9966 #ifdef SWM_XRR_HAS_CRTC
9967 int c;
9968 int ncrtc = 0;
9969 #endif /* SWM_XRR_HAS_CRTC */
9970 struct swm_region *r;
9971 struct ws_win *win;
9972 int num_screens;
9973 xcb_randr_get_screen_resources_current_cookie_t src;
9974 xcb_randr_get_screen_resources_current_reply_t *srr;
9975 xcb_randr_get_crtc_info_cookie_t cic;
9976 xcb_randr_get_crtc_info_reply_t *cir = NULL;
9977 xcb_randr_crtc_t *crtc;
9978 xcb_screen_t *screen;
9979
9980 DNPRINTF(SWM_D_MISC, "scan_xrandr: screen: %d\n", i);
9981
9982 if ((screen = get_screen(i)) == NULL)
9983 errx(1, "ERROR: can't get screen %d.", i);
9984
9985 num_screens = get_screen_count();
9986 if (i >= num_screens)
9987 errx(1, "scan_xrandr: invalid screen");
9988
9989 /* remove any old regions */
9990 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
9991 r->ws->old_r = r->ws->r = NULL;
9992 bar_cleanup(r);
9993 xcb_destroy_window(conn, r->id);
9994 TAILQ_REMOVE(&screens[i].rl, r, entry);
9995 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
9996 }
9997 outputs = 0;
9998
9999 /* map virtual screens onto physical screens */
10000 #ifdef SWM_XRR_HAS_CRTC
10001 if (xrandr_support) {
10002 src = xcb_randr_get_screen_resources_current(conn,
10003 screens[i].root);
10004 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
10005 NULL);
10006 if (srr == NULL) {
10007 new_region(&screens[i], 0, 0,
10008 screen->width_in_pixels,
10009 screen->height_in_pixels);
10010 goto out;
10011 } else
10012 ncrtc = srr->num_crtcs;
10013
10014 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
10015 for (c = 0; c < ncrtc; c++) {
10016 cic = xcb_randr_get_crtc_info(conn, crtc[c],
10017 XCB_CURRENT_TIME);
10018 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
10019 if (cir == NULL)
10020 continue;
10021 if (cir->num_outputs == 0) {
10022 free(cir);
10023 continue;
10024 }
10025
10026 if (cir->mode == 0)
10027 new_region(&screens[i], 0, 0,
10028 screen->width_in_pixels,
10029 screen->height_in_pixels);
10030 else
10031 new_region(&screens[i],
10032 cir->x, cir->y, cir->width, cir->height);
10033 free(cir);
10034 }
10035 free(srr);
10036 }
10037 #endif /* SWM_XRR_HAS_CRTC */
10038
10039 /* If detection failed, create a single region that spans the screen. */
10040 if (TAILQ_EMPTY(&screens[i].rl))
10041 new_region(&screens[i], 0, 0, screen->width_in_pixels,
10042 screen->height_in_pixels);
10043
10044 out:
10045 /* Cleanup unused previously visible workspaces. */
10046 TAILQ_FOREACH(r, &screens[i].orl, entry) {
10047 TAILQ_FOREACH(win, &r->ws->winlist, entry)
10048 unmap_window(win);
10049 r->ws->state = SWM_WS_STATE_HIDDEN;
10050
10051 /* The screen shouldn't focus on an unused region. */
10052 if (screens[i].r_focus == r)
10053 screens[i].r_focus = NULL;
10054 }
10055
10056 DNPRINTF(SWM_D_MISC, "scan_xrandr: done.\n");
10057 }
10058
10059 void
10060 screenchange(xcb_randr_screen_change_notify_event_t *e)
10061 {
10062 struct swm_region *r;
10063 int i, num_screens;
10064
10065 DNPRINTF(SWM_D_EVENT, "screenchange: root: %#x\n", e->root);
10066
10067 num_screens = get_screen_count();
10068 /* silly event doesn't include the screen index */
10069 for (i = 0; i < num_screens; i++)
10070 if (screens[i].root == e->root)
10071 break;
10072 if (i >= num_screens)
10073 errx(1, "screenchange: screen not found");
10074
10075 /* brute force for now, just re-enumerate the regions */
10076 scan_xrandr(i);
10077
10078 #ifdef SWM_DEBUG
10079 print_win_geom(e->root);
10080 #endif
10081 /* add bars to all regions */
10082 for (i = 0; i < num_screens; i++) {
10083 TAILQ_FOREACH(r, &screens[i].rl, entry)
10084 bar_setup(r);
10085 }
10086
10087 stack();
10088
10089 /* Make sure a region has focus on each screen. */
10090 for (i = 0; i < num_screens; i++) {
10091 if (screens[i].r_focus == NULL) {
10092 r = TAILQ_FIRST(&screens[i].rl);
10093 if (r != NULL)
10094 focus_region(r);
10095 }
10096 }
10097
10098 bar_draw();
10099 focus_flush();
10100
10101 /* Update workspace state on all regions. */
10102 for (i = 0; i < num_screens; i++)
10103 TAILQ_FOREACH(r, &screens[i].rl, entry)
10104 r->ws->state = SWM_WS_STATE_MAPPED;
10105 }
10106
10107 void
10108 grab_windows(void)
10109 {
10110 struct swm_region *r = NULL;
10111 xcb_window_t *wins = NULL, trans, *cwins = NULL;
10112 int i, j, k, n, no, num_screens, manage, mapped;
10113 uint8_t state;
10114
10115 xcb_query_tree_cookie_t qtc;
10116 xcb_query_tree_reply_t *qtr;
10117 xcb_get_window_attributes_cookie_t gac;
10118 xcb_get_window_attributes_reply_t *gar;
10119 xcb_get_property_cookie_t pc;
10120 xcb_get_property_reply_t *pr;
10121
10122 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
10123 num_screens = get_screen_count();
10124 for (i = 0; i < num_screens; i++) {
10125 qtc = xcb_query_tree(conn, screens[i].root);
10126 qtr = xcb_query_tree_reply(conn, qtc, NULL);
10127 if (qtr == NULL)
10128 continue;
10129 wins = xcb_query_tree_children(qtr);
10130 no = xcb_query_tree_children_length(qtr);
10131
10132 /* Try to sort windows according to _NET_CLIENT_LIST. */
10133 pr = xcb_get_property_reply(conn, xcb_get_property(conn, 0,
10134 screens[i].root, ewmh[_NET_CLIENT_LIST].atom,
10135 XCB_ATOM_WINDOW, 0, UINT32_MAX), NULL);
10136 if (pr != NULL) {
10137 cwins = xcb_get_property_value(pr);
10138 n = xcb_get_property_value_length(pr) /
10139 sizeof(xcb_atom_t);
10140
10141 for (j = 0; j < n; ++j) {
10142 for (k = j; k < no; ++k) {
10143 if (wins[k] == cwins[j]) {
10144 /* Swap wins j and k. */
10145 wins[k] = wins[j];
10146 wins[j] = cwins[j];
10147 }
10148 }
10149 }
10150
10151 free(pr);
10152 }
10153
10154 /* attach windows to a region */
10155 /* normal windows */
10156 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
10157 for (j = 0; j < no; j++) {
10158 TAILQ_FOREACH(r, &screens[i].rl, entry) {
10159 if (r->id == wins[j]) {
10160 DNPRINTF(SWM_D_INIT, "grab_windows: "
10161 "skip %#x; region input window.\n",
10162 wins[j]);
10163 break;
10164 } else if (r->bar->id == wins[j]) {
10165 DNPRINTF(SWM_D_INIT, "grab_windows: "
10166 "skip %#x; region bar.\n",
10167 wins[j]);
10168 break;
10169 }
10170 }
10171
10172 if (r)
10173 continue;
10174
10175 gac = xcb_get_window_attributes(conn, wins[j]);
10176 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
10177 if (gar == NULL) {
10178 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10179 "doesn't exist.\n", wins[j]);
10180 continue;
10181 }
10182
10183 if (gar->override_redirect) {
10184 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10185 "override_redirect set.\n", wins[j]);
10186 free(gar);
10187 continue;
10188 }
10189
10190 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
10191 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
10192 &trans, NULL)) {
10193 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10194 "is transient for %#x.\n", wins[j], trans);
10195 free(gar);
10196 continue;
10197 }
10198
10199 state = get_win_state(wins[j]);
10200 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
10201 mapped = gar->map_state == XCB_MAP_STATE_VIEWABLE;
10202 if (mapped || manage)
10203 manage_window(wins[j], mapped);
10204 free(gar);
10205 }
10206 /* transient windows */
10207 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
10208 for (j = 0; j < no; j++) {
10209 gac = xcb_get_window_attributes(conn, wins[j]);
10210 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
10211 if (gar == NULL) {
10212 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10213 "doesn't exist.\n", wins[j]);
10214 continue;
10215 }
10216
10217 if (gar->override_redirect) {
10218 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
10219 "override_redirect set.\n", wins[j]);
10220 free(gar);
10221 continue;
10222 }
10223
10224 state = get_win_state(wins[j]);
10225 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
10226 mapped = gar->map_state == XCB_MAP_STATE_VIEWABLE;
10227 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
10228 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
10229 &trans, NULL) && manage)
10230 manage_window(wins[j], mapped);
10231 free(gar);
10232 }
10233 free(qtr);
10234 }
10235 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
10236 }
10237
10238 void
10239 setup_screens(void)
10240 {
10241 int i, j, k, num_screens;
10242 struct workspace *ws;
10243 uint32_t gcv[1], wa[1];
10244 const xcb_query_extension_reply_t *qep;
10245 xcb_screen_t *screen;
10246 xcb_randr_query_version_cookie_t c;
10247 xcb_randr_query_version_reply_t *r;
10248
10249 num_screens = get_screen_count();
10250 if ((screens = calloc(num_screens,
10251 sizeof(struct swm_screen))) == NULL)
10252 err(1, "setup_screens: calloc: failed to allocate memory for "
10253 "screens");
10254
10255 /* initial Xrandr setup */
10256 xrandr_support = 0;
10257 qep = xcb_get_extension_data(conn, &xcb_randr_id);
10258 if (qep->present) {
10259 c = xcb_randr_query_version(conn, 1, 1);
10260 r = xcb_randr_query_version_reply(conn, c, NULL);
10261 if (r) {
10262 if (r->major_version >= 1) {
10263 xrandr_support = 1;
10264 xrandr_eventbase = qep->first_event;
10265 }
10266 free(r);
10267 }
10268 }
10269
10270 wa[0] = cursors[XC_LEFT_PTR].cid;
10271
10272 /* map physical screens */
10273 for (i = 0; i < num_screens; i++) {
10274 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
10275 screens[i].idx = i;
10276 screens[i].r_focus = NULL;
10277
10278 TAILQ_INIT(&screens[i].rl);
10279 TAILQ_INIT(&screens[i].orl);
10280 if ((screen = get_screen(i)) == NULL)
10281 errx(1, "ERROR: can't get screen %d.", i);
10282 screens[i].root = screen->root;
10283
10284 /* set default colors */
10285 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
10286 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
10287 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
10288 setscreencolor("rgb:00/40/40", i + 1,
10289 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
10290 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
10291 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
10292
10293 /* create graphics context on screen */
10294 screens[i].bar_gc = xcb_generate_id(conn);
10295 gcv[0] = 0;
10296 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
10297 XCB_GC_GRAPHICS_EXPOSURES, gcv);
10298
10299 /* set default cursor */
10300 xcb_change_window_attributes(conn, screens[i].root,
10301 XCB_CW_CURSOR, wa);
10302
10303 /* init all workspaces */
10304 /* XXX these should be dynamically allocated too */
10305 for (j = 0; j < SWM_WS_MAX; j++) {
10306 ws = &screens[i].ws[j];
10307 ws->idx = j;
10308 ws->name = NULL;
10309 ws->bar_enabled = 1;
10310 ws->focus = NULL;
10311 ws->focus_prev = NULL;
10312 ws->focus_pending = NULL;
10313 ws->r = NULL;
10314 ws->old_r = NULL;
10315 ws->state = SWM_WS_STATE_HIDDEN;
10316 TAILQ_INIT(&ws->stack);
10317 TAILQ_INIT(&ws->winlist);
10318 TAILQ_INIT(&ws->unmanagedlist);
10319
10320 for (k = 0; layouts[k].l_stack != NULL; k++)
10321 if (layouts[k].l_config != NULL)
10322 layouts[k].l_config(ws,
10323 SWM_ARG_ID_STACKINIT);
10324 ws->cur_layout = &layouts[0];
10325 ws->cur_layout->l_string(ws);
10326 }
10327
10328 scan_xrandr(i);
10329
10330 if (xrandr_support)
10331 xcb_randr_select_input(conn, screens[i].root,
10332 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
10333 }
10334 }
10335
10336 void
10337 setup_globals(void)
10338 {
10339 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
10340 err(1, "setup_globals: strdup: failed to allocate memory.");
10341
10342 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
10343 err(1, "setup_globals: strdup: failed to allocate memory.");
10344
10345 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
10346 errx(1, "unable to allocate key symbols");
10347
10348 a_state = get_atom_from_string("WM_STATE");
10349 a_prot = get_atom_from_string("WM_PROTOCOLS");
10350 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
10351 a_net_supported = get_atom_from_string("_NET_SUPPORTED");
10352 a_net_wm_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
10353 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
10354 a_utf8_string = get_atom_from_string("UTF8_STRING");
10355 a_swm_ws = get_atom_from_string("_SWM_WS");
10356 }
10357
10358 void
10359 shutdown_cleanup(void)
10360 {
10361 int i, num_screens;
10362
10363 /* disable alarm because the following code may not be interrupted */
10364 alarm(0);
10365 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
10366 err(1, "can't disable alarm");
10367
10368 bar_extra_stop();
10369 unmap_all();
10370
10371 cursors_cleanup();
10372
10373 teardown_ewmh();
10374
10375 num_screens = get_screen_count();
10376 for (i = 0; i < num_screens; ++i) {
10377 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
10378 screens[i].root, XCB_CURRENT_TIME);
10379
10380 if (screens[i].bar_gc != XCB_NONE)
10381 xcb_free_gc(conn, screens[i].bar_gc);
10382 if (!bar_font_legacy)
10383 XftColorFree(display, DefaultVisual(display, i),
10384 DefaultColormap(display, i), &bar_font_color);
10385 }
10386
10387 if (bar_font_legacy)
10388 XFreeFontSet(display, bar_fs);
10389 else {
10390 XftFontClose(display, bar_font);
10391 }
10392
10393 xcb_key_symbols_free(syms);
10394 xcb_flush(conn);
10395 xcb_disconnect(conn);
10396 }
10397
10398 void
10399 event_error(xcb_generic_error_t *e)
10400 {
10401 (void)e;
10402
10403 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
10404 "resource_id: %u, minor_code: %u\n",
10405 xcb_event_get_error_label(e->error_code), e->error_code,
10406 xcb_event_get_request_label(e->major_code), e->major_code,
10407 e->sequence, e->resource_id, e->minor_code);
10408 }
10409
10410 void
10411 event_handle(xcb_generic_event_t *evt)
10412 {
10413 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
10414
10415 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
10416 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
10417 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
10418
10419 switch (type) {
10420 #define EVENT(type, callback) case type: callback((void *)evt); return
10421 EVENT(0, event_error);
10422 EVENT(XCB_BUTTON_PRESS, buttonpress);
10423 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
10424 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
10425 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
10426 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
10427 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
10428 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
10429 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
10430 /*EVENT(XCB_CREATE_NOTIFY, );*/
10431 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
10432 EVENT(XCB_ENTER_NOTIFY, enternotify);
10433 EVENT(XCB_EXPOSE, expose);
10434 #ifdef SWM_DEBUG
10435 EVENT(XCB_FOCUS_IN, focusin);
10436 EVENT(XCB_FOCUS_OUT, focusout);
10437 #endif
10438 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
10439 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
10440 EVENT(XCB_KEY_PRESS, keypress);
10441 /*EVENT(XCB_KEY_RELEASE, keypress);*/
10442 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
10443 #ifdef SWM_DEBUG
10444 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
10445 #endif
10446 EVENT(XCB_MAP_NOTIFY, mapnotify);
10447 EVENT(XCB_MAP_REQUEST, maprequest);
10448 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
10449 EVENT(XCB_MOTION_NOTIFY, motionnotify);
10450 /*EVENT(XCB_NO_EXPOSURE, );*/
10451 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
10452 /*EVENT(XCB_REPARENT_NOTIFY, );*/
10453 /*EVENT(XCB_RESIZE_REQUEST, );*/
10454 /*EVENT(XCB_SELECTION_CLEAR, );*/
10455 /*EVENT(XCB_SELECTION_NOTIFY, );*/
10456 /*EVENT(XCB_SELECTION_REQUEST, );*/
10457 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
10458 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
10459 #undef EVENT
10460 }
10461 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
10462 screenchange((void *)evt);
10463 }
10464
10465 int
10466 main(int argc, char *argv[])
10467 {
10468 struct swm_region *r;
10469 char conf[PATH_MAX], *cfile = NULL;
10470 struct stat sb;
10471 int xfd, i, num_screens, startup = 1;
10472 struct sigaction sact;
10473 xcb_generic_event_t *evt;
10474 struct timeval tv;
10475 fd_set rd;
10476 int rd_max;
10477 int stdin_ready = 0;
10478 int num_readable;
10479
10480 /* suppress unused warning since var is needed */
10481 (void)argc;
10482
10483 time_started = time(NULL);
10484
10485 start_argv = argv;
10486 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
10487 if (setlocale(LC_CTYPE, "") == NULL || setlocale(LC_TIME, "") == NULL)
10488 warnx("no locale support");
10489
10490 /* handle some signals */
10491 bzero(&sact, sizeof(sact));
10492 sigemptyset(&sact.sa_mask);
10493 sact.sa_flags = 0;
10494 sact.sa_handler = sighdlr;
10495 sigaction(SIGINT, &sact, NULL);
10496 sigaction(SIGQUIT, &sact, NULL);
10497 sigaction(SIGTERM, &sact, NULL);
10498 sigaction(SIGHUP, &sact, NULL);
10499
10500 sact.sa_handler = sighdlr;
10501 sact.sa_flags = SA_NOCLDSTOP;
10502 sigaction(SIGCHLD, &sact, NULL);
10503
10504 if ((display = XOpenDisplay(0)) == NULL)
10505 errx(1, "can not open display");
10506
10507 conn = XGetXCBConnection(display);
10508 if (xcb_connection_has_error(conn))
10509 errx(1, "can not get XCB connection");
10510
10511 XSetEventQueueOwner(display, XCBOwnsEventQueue);
10512
10513 xcb_prefetch_extension_data(conn, &xcb_randr_id);
10514 xfd = xcb_get_file_descriptor(conn);
10515
10516 /* look for local and global conf file */
10517 pwd = getpwuid(getuid());
10518 if (pwd == NULL)
10519 errx(1, "invalid user: %d", getuid());
10520
10521 xcb_grab_server(conn);
10522 xcb_aux_sync(conn);
10523
10524 /* flush all events */
10525 while ((evt = xcb_poll_for_event(conn))) {
10526 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
10527 event_handle(evt);
10528 free(evt);
10529 }
10530
10531 if (enable_wm())
10532 errx(1, "another window manager is currently running");
10533
10534 /* Load Xcursors and/or cursorfont glyph cursors. */
10535 cursors_load();
10536
10537 xcb_aux_sync(conn);
10538
10539 setup_globals();
10540 setup_screens();
10541 setup_ewmh();
10542 setup_keys();
10543 setup_quirks();
10544 setup_spawn();
10545
10546 /* load config */
10547 for (i = 0; ; i++) {
10548 conf[0] = '\0';
10549 switch (i) {
10550 case 0:
10551 /* ~ */
10552 snprintf(conf, sizeof conf, "%s/.%s",
10553 pwd->pw_dir, SWM_CONF_FILE);
10554 break;
10555 case 1:
10556 /* global */
10557 snprintf(conf, sizeof conf, "/etc/%s",
10558 SWM_CONF_FILE);
10559 break;
10560 case 2:
10561 /* ~ compat */
10562 snprintf(conf, sizeof conf, "%s/.%s",
10563 pwd->pw_dir, SWM_CONF_FILE_OLD);
10564 break;
10565 case 3:
10566 /* global compat */
10567 snprintf(conf, sizeof conf, "/etc/%s",
10568 SWM_CONF_FILE_OLD);
10569 break;
10570 default:
10571 goto noconfig;
10572 }
10573
10574 if (strlen(conf) && stat(conf, &sb) != -1)
10575 if (S_ISREG(sb.st_mode)) {
10576 cfile = conf;
10577 break;
10578 }
10579 }
10580 noconfig:
10581
10582 /* load conf (if any) */
10583 if (cfile)
10584 conf_load(cfile, SWM_CONF_DEFAULT);
10585
10586 validate_spawns();
10587
10588 if (getenv("SWM_STARTED") == NULL)
10589 setenv("SWM_STARTED", "YES", 1);
10590
10591 /* setup all bars */
10592 num_screens = get_screen_count();
10593 for (i = 0; i < num_screens; i++)
10594 TAILQ_FOREACH(r, &screens[i].rl, entry)
10595 bar_setup(r);
10596
10597 /* Manage existing windows. */
10598 grab_windows();
10599
10600 grabkeys();
10601 stack();
10602 bar_draw();
10603
10604 xcb_ungrab_server(conn);
10605 xcb_flush(conn);
10606
10607 /* Update state of each newly mapped workspace. */
10608 for (i = 0; i < num_screens; i++)
10609 TAILQ_FOREACH(r, &screens[i].rl, entry)
10610 r->ws->state = SWM_WS_STATE_MAPPED;
10611
10612 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
10613
10614 while (running) {
10615 while ((evt = xcb_poll_for_event(conn))) {
10616 if (!running)
10617 goto done;
10618 event_handle(evt);
10619 free(evt);
10620 }
10621
10622 /* If just (re)started, set default focus if needed. */
10623 if (startup) {
10624 startup = 0;
10625
10626 if (focus_mode != SWM_FOCUS_FOLLOW) {
10627 r = TAILQ_FIRST(&screens[0].rl);
10628 if (r) {
10629 focus_region(r);
10630 focus_flush();
10631 }
10632 continue;
10633 }
10634 }
10635
10636 FD_ZERO(&rd);
10637
10638 if (bar_extra)
10639 FD_SET(STDIN_FILENO, &rd);
10640
10641 FD_SET(xfd, &rd);
10642 tv.tv_sec = 1;
10643 tv.tv_usec = 0;
10644 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
10645 if (num_readable == -1 && errno != EINTR) {
10646 DNPRINTF(SWM_D_MISC, "select failed");
10647 } else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd)) {
10648 stdin_ready = 1;
10649 }
10650
10651 if (restart_wm)
10652 restart(NULL, NULL);
10653
10654 if (search_resp)
10655 search_do_resp();
10656
10657 if (!running)
10658 goto done;
10659
10660 if (stdin_ready) {
10661 stdin_ready = 0;
10662 bar_extra_update();
10663 }
10664
10665 bar_draw();
10666 xcb_flush(conn);
10667 }
10668 done:
10669 shutdown_cleanup();
10670
10671 return (0);
10672 }