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