]> code.delx.au - spectrwm/blob - spectrwm.c
5840b0d2b1d3df5e2d4e62065a9fa4ecda4aa0bc
[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 = NULL;
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
3705 /* If window isn't top-level, try to find managed ancestor. */
3706 qtr = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
3707 if (qtr) {
3708 if (qtr->parent != XCB_WINDOW_NONE && qtr->parent != qtr->root)
3709 win = find_window(qtr->parent);
3710
3711 #ifdef SWM_DEBUG
3712 if (win)
3713 DNPRINTF(SWM_D_MISC, "find_window: found child %#x "
3714 "of %#x.\n", win->id, qtr->parent);
3715 #endif
3716
3717 free(qtr);
3718 }
3719
3720 return (win);
3721 }
3722
3723 struct ws_win *
3724 find_unmanaged_window(xcb_window_t id)
3725 {
3726 struct ws_win *win;
3727 int i, j, num_screens;
3728
3729 num_screens = get_screen_count();
3730 for (i = 0; i < num_screens; i++)
3731 for (j = 0; j < workspace_limit; j++)
3732 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
3733 entry)
3734 if (id == win->id)
3735 return (win);
3736 return (NULL);
3737 }
3738
3739 void
3740 spawn(int ws_idx, union arg *args, bool close_fd)
3741 {
3742 int fd;
3743 char *ret = NULL;
3744
3745 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
3746
3747 close(xcb_get_file_descriptor(conn));
3748
3749 setenv("LD_PRELOAD", SWM_LIB, 1);
3750
3751 if (asprintf(&ret, "%d", ws_idx) == -1) {
3752 warn("spawn: asprintf SWM_WS");
3753 _exit(1);
3754 }
3755 setenv("_SWM_WS", ret, 1);
3756 free(ret);
3757 ret = NULL;
3758
3759 if (asprintf(&ret, "%d", getpid()) == -1) {
3760 warn("spawn: asprintf _SWM_PID");
3761 _exit(1);
3762 }
3763 setenv("_SWM_PID", ret, 1);
3764 free(ret);
3765 ret = NULL;
3766
3767 if (setsid() == -1) {
3768 warn("spawn: setsid");
3769 _exit(1);
3770 }
3771
3772 if (close_fd) {
3773 /*
3774 * close stdin and stdout to prevent interaction between apps
3775 * and the baraction script
3776 * leave stderr open to record errors
3777 */
3778 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
3779 warn("spawn: open");
3780 _exit(1);
3781 }
3782 dup2(fd, STDIN_FILENO);
3783 dup2(fd, STDOUT_FILENO);
3784 if (fd > 2)
3785 close(fd);
3786 }
3787
3788 execvp(args->argv[0], args->argv);
3789
3790 warn("spawn: execvp");
3791 _exit(1);
3792 }
3793
3794 void
3795 kill_refs(struct ws_win *win)
3796 {
3797 struct workspace *ws;
3798 struct ws_win *w;
3799 int i, j, num_screens;
3800
3801 if (win == NULL)
3802 return;
3803
3804 num_screens = get_screen_count();
3805 for (i = 0; i < num_screens; i++) {
3806 for (j = 0; j < workspace_limit; j++) {
3807 ws = &screens[i].ws[j];
3808
3809 if (win == ws->focus)
3810 ws->focus = NULL;
3811 if (win == ws->focus_prev)
3812 ws->focus_prev = NULL;
3813 if (win == ws->focus_pending)
3814 ws->focus_pending = NULL;
3815
3816 if (TRANS(win))
3817 TAILQ_FOREACH(w, &ws->winlist, entry)
3818 if (win == w->focus_child)
3819 w->focus_child = NULL;
3820 }
3821 }
3822 }
3823
3824 int
3825 validate_win(struct ws_win *testwin)
3826 {
3827 struct ws_win *win;
3828 struct workspace *ws;
3829 struct swm_region *r;
3830 int i, x, num_screens;
3831
3832 if (testwin == NULL)
3833 return (0);
3834
3835 num_screens = get_screen_count();
3836 for (i = 0; i < num_screens; i++)
3837 TAILQ_FOREACH(r, &screens[i].rl, entry)
3838 for (x = 0; x < workspace_limit; x++) {
3839 ws = &r->s->ws[x];
3840 TAILQ_FOREACH(win, &ws->winlist, entry)
3841 if (win == testwin)
3842 return (0);
3843 }
3844 return (1);
3845 }
3846
3847 int
3848 validate_ws(struct workspace *testws)
3849 {
3850 struct swm_region *r;
3851 struct workspace *ws;
3852 int i, x, num_screens;
3853
3854 /* validate all ws */
3855 num_screens = get_screen_count();
3856 for (i = 0; i < num_screens; i++)
3857 TAILQ_FOREACH(r, &screens[i].rl, entry)
3858 for (x = 0; x < workspace_limit; x++) {
3859 ws = &r->s->ws[x];
3860 if (ws == testws)
3861 return (0);
3862 }
3863 return (1);
3864 }
3865
3866 void
3867 unfocus_win(struct ws_win *win)
3868 {
3869 xcb_window_t none = XCB_WINDOW_NONE;
3870
3871 DNPRINTF(SWM_D_FOCUS, "unfocus_win: win %#x\n", WINID(win));
3872
3873 if (win == NULL)
3874 return;
3875
3876 if (win->ws == NULL) {
3877 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3878 return;
3879 }
3880
3881 if (validate_ws(win->ws)) {
3882 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3883 return;
3884 }
3885
3886 if (win->ws->r == NULL) {
3887 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3888 return;
3889 }
3890
3891 if (validate_win(win)) {
3892 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3893 kill_refs(win);
3894 return;
3895 }
3896
3897 if (win->ws->focus == win) {
3898 win->ws->focus = NULL;
3899 win->ws->focus_prev = win;
3900 }
3901
3902 if (validate_win(win->ws->focus)) {
3903 kill_refs(win->ws->focus);
3904 win->ws->focus = NULL;
3905 }
3906
3907 if (validate_win(win->ws->focus_prev)) {
3908 kill_refs(win->ws->focus_prev);
3909 win->ws->focus_prev = NULL;
3910 }
3911
3912 draw_frame(win);
3913
3914 /* Raise window to "top unfocused position." */
3915 if (win->ws->always_raise)
3916 raise_window(win);
3917
3918 /* Update border width */
3919 if (win->bordered && (win->quirks & SWM_Q_MINIMALBORDER) &&
3920 FLOATING(win)) {
3921 win->bordered = 0;
3922 X(win) += border_width;
3923 Y(win) += border_width;
3924 update_window(win);
3925 }
3926
3927 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3928 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3929
3930 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3931 }
3932
3933 void
3934 focus_win(struct ws_win *win)
3935 {
3936 struct ws_win *cfw = NULL, *parent = NULL, *w, *tmpw;
3937 struct workspace *ws;
3938 xcb_get_input_focus_reply_t *gifr = NULL;
3939 xcb_get_window_attributes_reply_t *war = NULL;
3940
3941 DNPRINTF(SWM_D_FOCUS, "focus_win: win %#x\n", WINID(win));
3942
3943 if (win == NULL || win->ws == NULL || !win->mapped)
3944 goto out;
3945
3946 ws = win->ws;
3947
3948 if (validate_ws(ws))
3949 goto out;
3950
3951 if (validate_win(win)) {
3952 kill_refs(win);
3953 goto out;
3954 }
3955
3956 gifr = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3957 if (gifr) {
3958 DNPRINTF(SWM_D_FOCUS, "focus_win: cur focus: %#x\n",
3959 gifr->focus);
3960
3961 cfw = find_window(gifr->focus);
3962 if (cfw) {
3963 if (cfw != win) {
3964 if (cfw->ws != ws && cfw->ws->r != NULL &&
3965 cfw->frame != XCB_WINDOW_NONE) {
3966 draw_frame(cfw);
3967 } else {
3968 unfocus_win(cfw);
3969 }
3970 }
3971 } else {
3972 war = xcb_get_window_attributes_reply(conn,
3973 xcb_get_window_attributes(conn, gifr->focus), NULL);
3974 if (war && war->override_redirect && ws->focus == win) {
3975 DNPRINTF(SWM_D_FOCUS, "focus_win: skip refocus "
3976 "from override_redirect.\n");
3977 goto out;
3978 }
3979 }
3980 }
3981
3982 if (ws->focus != win) {
3983 if (ws->focus && ws->focus != cfw)
3984 unfocus_win(ws->focus);
3985 ws->focus = win;
3986 }
3987
3988 /* Clear focus child redirect. */
3989 win->focus_child = NULL;
3990
3991 /* If transient, adjust parent's focus child for focus_magic. */
3992 if (TRANS(win)) {
3993 parent = find_window(win->transient);
3994 if (parent && parent->focus_child != win)
3995 parent->focus_child = win;
3996 }
3997
3998 /* Update window border even if workspace is hidden. */
3999 draw_frame(win);
4000
4001 if (cfw == win) {
4002 DNPRINTF(SWM_D_FOCUS, "focus_win: already focused.\n");
4003 goto out;
4004 }
4005
4006 if (ws->r) {
4007 /* Set input focus if no input hint, or indicated by hint. */
4008 if (ACCEPTS_FOCUS(win)) {
4009 DNPRINTF(SWM_D_FOCUS, "focus_win: set_input_focus: %#x,"
4010 " revert-to: parent, time: %#x\n", win->id,
4011 last_event_time);
4012 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
4013 win->id, last_event_time);
4014 } else if (!win->take_focus) {
4015 DNPRINTF(SWM_D_FOCUS, "focus_win: set_input_focus: %#x,"
4016 " revert-to: parent, time: 0\n", ws->r->id);
4017 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
4018 ws->r->id, XCB_CURRENT_TIME);
4019 }
4020
4021 /* Tell app it can adjust focus to a specific window. */
4022 if (win->take_focus) {
4023 /* java is special; always tell parent */
4024 if (TRANS(win) && win->java)
4025 client_msg(parent, a_takefocus,
4026 last_event_time);
4027 else
4028 client_msg(win, a_takefocus, last_event_time);
4029 }
4030
4031 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
4032 ws->always_raise) {
4033 /* If a parent exists, map it first. */
4034 if (parent) {
4035 raise_window(parent);
4036 map_window(parent);
4037
4038 /* Map siblings next. */
4039 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry,
4040 tmpw)
4041 if (w != win && !ICONIC(w) &&
4042 w->transient == parent->id) {
4043 raise_window(w);
4044 map_window(w);
4045 }
4046 }
4047
4048 /* Map focused window. */
4049 raise_window(win);
4050 map_window(win);
4051
4052 /* Stack any children of focus window. */
4053 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
4054 if (w->transient == win->id && !ICONIC(w)) {
4055 raise_window(w);
4056 map_window(w);
4057 }
4058 } else if (tile_gap < 0 && !ABOVE(win)) {
4059 /*
4060 * Windows overlap in the layout.
4061 * Raise focused win above all tiled wins.
4062 */
4063 raise_window(win);
4064 map_window(win);
4065 }
4066
4067 set_region(ws->r);
4068
4069 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
4070 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
4071 &win->id);
4072
4073 bar_draw(ws->r->bar);
4074 }
4075
4076 out:
4077 free(gifr);
4078 free(war);
4079 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
4080 }
4081
4082 /* If a child window should have focus instead, return it. */
4083 struct ws_win *
4084 get_focus_magic(struct ws_win *win)
4085 {
4086 struct ws_win *parent = NULL;
4087 struct ws_win *child = NULL;
4088
4089 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: win %#x\n", WINID(win));
4090 if (win == NULL)
4091 return win;
4092
4093 if (TRANS(win)) {
4094 parent = find_window(win->transient);
4095
4096 /* If parent prefers focus elsewhere, then try to do so. */
4097 if (parent && (child = parent->focus_child)) {
4098 if (validate_win(child) == 0 && child->mapped)
4099 win = child;
4100 else
4101 parent->focus_child = NULL;
4102 }
4103 }
4104
4105 /* If this window prefers focus elsewhere, then try to do so. */
4106 if ((child = win->focus_child)) {
4107 if (validate_win(child) == 0 && child->mapped)
4108 win = child;
4109 else
4110 win->focus_child = NULL;
4111 }
4112
4113 return win;
4114 }
4115
4116 void
4117 event_drain(uint8_t rt)
4118 {
4119 xcb_generic_event_t *evt;
4120
4121 /* ensure all pending requests have been processed before filtering. */
4122 xcb_aux_sync(conn);
4123 while ((evt = get_next_event(false))) {
4124 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
4125 event_handle(evt);
4126
4127 free(evt);
4128 }
4129 }
4130
4131 void
4132 set_region(struct swm_region *r)
4133 {
4134 struct swm_region *rf;
4135 int vals[2];
4136
4137 if (r == NULL)
4138 return;
4139
4140 rf = r->s->r_focus;
4141 /* Unfocus old region bar. */
4142 if (rf != NULL) {
4143 if (rf == r)
4144 return;
4145
4146 xcb_change_window_attributes(conn, rf->bar->id,
4147 XCB_CW_BORDER_PIXEL,
4148 &r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel);
4149 }
4150
4151 if (rf != NULL && rf != r && (X(rf) != X(r) || Y(rf) != Y(r) ||
4152 WIDTH(rf) != WIDTH(r) || HEIGHT(rf) != HEIGHT(r))) {
4153 /* Set _NET_DESKTOP_GEOMETRY. */
4154 vals[0] = WIDTH(r);
4155 vals[1] = HEIGHT(r);
4156 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, r->s->root,
4157 ewmh[_NET_DESKTOP_GEOMETRY].atom, XCB_ATOM_CARDINAL, 32, 2,
4158 &vals);
4159 }
4160
4161 /* Set region bar border to focus_color. */
4162 xcb_change_window_attributes(conn, r->bar->id,
4163 XCB_CW_BORDER_PIXEL, &r->s->c[SWM_S_COLOR_BAR_BORDER].pixel);
4164
4165 r->s->r_focus = r;
4166
4167 ewmh_update_current_desktop();
4168 }
4169
4170 void
4171 focus_region(struct swm_region *r)
4172 {
4173 struct ws_win *nfw;
4174 struct swm_region *old_r;
4175
4176 if (r == NULL)
4177 return;
4178
4179 old_r = r->s->r_focus;
4180 set_region(r);
4181
4182 nfw = get_region_focus(r);
4183 if (nfw) {
4184 focus_win(nfw);
4185 } else {
4186 /* New region is empty; need to manually unfocus win. */
4187 if (old_r) {
4188 unfocus_win(old_r->ws->focus);
4189 /* Clear bar since empty. */
4190 bar_draw(old_r->bar);
4191 }
4192
4193 DNPRINTF(SWM_D_FOCUS, "focus_region: set_input_focus: %#x, "
4194 "revert-to: parent, time: 0\n", r->id);
4195 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, r->id,
4196 XCB_CURRENT_TIME);
4197
4198 }
4199 }
4200
4201 void
4202 switchws(struct binding *bp, struct swm_region *r, union arg *args)
4203 {
4204 struct swm_region *this_r, *other_r;
4205 struct ws_win *win;
4206 struct workspace *new_ws, *old_ws;
4207 xcb_window_t none = XCB_WINDOW_NONE;
4208 int wsid = args->id;
4209 bool unmap_old = false;
4210
4211 (void)bp;
4212
4213 if (!(r && r->s))
4214 return;
4215
4216 if (wsid >= workspace_limit)
4217 return;
4218
4219 this_r = r;
4220 old_ws = this_r->ws;
4221 new_ws = &this_r->s->ws[wsid];
4222
4223 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
4224 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
4225
4226 if (new_ws == NULL || old_ws == NULL)
4227 return;
4228 if (new_ws == old_ws)
4229 return;
4230
4231 other_r = new_ws->r;
4232 if (other_r && workspace_clamp) {
4233 DNPRINTF(SWM_D_WS, "switchws: ws clamped.\n");
4234
4235 if (warp_focus) {
4236 DNPRINTF(SWM_D_WS, "switchws: warping focus to region "
4237 "with ws %d.\n", wsid);
4238 focus_region(other_r);
4239 center_pointer(other_r);
4240 focus_flush();
4241 }
4242 return;
4243 }
4244
4245 if ((win = old_ws->focus) != NULL) {
4246 draw_frame(win);
4247
4248 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
4249 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
4250 &none);
4251 }
4252
4253 if (other_r) {
4254 /* the other ws is visible in another region, exchange them */
4255 other_r->ws_prior = new_ws;
4256 other_r->ws = old_ws;
4257 old_ws->r = other_r;
4258 } else {
4259 /* the other workspace is hidden, hide this one */
4260 old_ws->r = NULL;
4261 unmap_old = true;
4262 }
4263
4264 this_r->ws_prior = old_ws;
4265 this_r->ws = new_ws;
4266 new_ws->r = this_r;
4267
4268 /* Set focus_pending before stacking, if needed. */
4269 if (focus_mode != SWM_FOCUS_FOLLOW && (!new_ws->focus_pending ||
4270 validate_win(new_ws->focus_pending))) {
4271 new_ws->focus_pending = get_region_focus(new_ws->r);
4272 new_ws->focus = new_ws->focus_prev;
4273 new_ws->focus_prev = NULL;
4274 }
4275
4276 new_ws->state = SWM_WS_STATE_MAPPING;
4277
4278 stack(other_r);
4279 stack(this_r);
4280
4281 /* unmap old windows */
4282 if (unmap_old) {
4283 TAILQ_FOREACH(win, &old_ws->winlist, entry)
4284 unmap_window(win);
4285 old_ws->state = SWM_WS_STATE_HIDDEN;
4286 }
4287
4288 /* if workspaces were swapped, then don't wait to set focus */
4289 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
4290 if (new_ws->focus_pending) {
4291 focus_win(new_ws->focus_pending);
4292 new_ws->focus_pending = NULL;
4293 }
4294 }
4295
4296 /* Clear bar and set focus on region input win if new ws is empty. */
4297 if (new_ws->focus_pending == NULL && new_ws->focus == NULL) {
4298 DNPRINTF(SWM_D_FOCUS, "switchws: set_input_focus: %#x, "
4299 "revert-to: parent, time: 0\n", r->id);
4300 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, r->id,
4301 XCB_CURRENT_TIME);
4302 bar_draw(r->bar);
4303 }
4304
4305 ewmh_update_current_desktop();
4306
4307 center_pointer(r);
4308 focus_flush();
4309 new_ws->state = SWM_WS_STATE_MAPPED;
4310
4311 DNPRINTF(SWM_D_WS, "switchws: done.\n");
4312 }
4313
4314 void
4315 cyclews(struct binding *bp, struct swm_region *r, union arg *args)
4316 {
4317 union arg a;
4318 struct swm_screen *s = r->s;
4319 bool cycle_all = false, mv = false;
4320
4321 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4322 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
4323
4324 a.id = r->ws->idx;
4325
4326 do {
4327 switch (args->id) {
4328 case SWM_ARG_ID_CYCLEWS_MOVE_UP:
4329 mv = true;
4330 /* FALLTHROUGH */
4331 case SWM_ARG_ID_CYCLEWS_UP_ALL:
4332 cycle_all = true;
4333 /* FALLTHROUGH */
4334 case SWM_ARG_ID_CYCLEWS_UP:
4335 a.id = (a.id < workspace_limit - 1) ? a.id + 1 : 0;
4336 break;
4337 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN:
4338 mv = true;
4339 /* FALLTHROUGH */
4340 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
4341 cycle_all = true;
4342 /* FALLTHROUGH */
4343 case SWM_ARG_ID_CYCLEWS_DOWN:
4344 a.id = (a.id > 0) ? a.id - 1 : workspace_limit - 1;
4345 break;
4346 default:
4347 return;
4348 };
4349
4350 if (!cycle_all &&
4351 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
4352 continue;
4353 if (!cycle_visible && s->ws[a.id].r != NULL)
4354 continue;
4355
4356 if (mv)
4357 send_to_ws(bp, r, &a);
4358
4359 switchws(bp, r, &a);
4360 } while (a.id != r->ws->idx);
4361
4362 DNPRINTF(SWM_D_FOCUS, "cyclews: done\n");
4363 }
4364
4365 void
4366 priorws(struct binding *bp, struct swm_region *r, union arg *args)
4367 {
4368 union arg a;
4369
4370 (void)args;
4371
4372 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4373 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
4374
4375 if (r->ws_prior == NULL)
4376 return;
4377
4378 a.id = r->ws_prior->idx;
4379 switchws(bp, r, &a);
4380 DNPRINTF(SWM_D_FOCUS, "priorws: done\n");
4381 }
4382
4383 void
4384 focusrg(struct binding *bp, struct swm_region *r, union arg *args)
4385 {
4386 int ridx = args->id, i, num_screens;
4387 struct swm_region *rr = NULL;
4388
4389 (void)bp;
4390
4391 num_screens = get_screen_count();
4392 /* do nothing if we don't have more than one screen */
4393 if (!(num_screens > 1 || outputs > 1))
4394 return;
4395
4396 DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
4397
4398 rr = TAILQ_FIRST(&r->s->rl);
4399 for (i = 0; i < ridx && rr != NULL; ++i)
4400 rr = TAILQ_NEXT(rr, entry);
4401
4402 if (rr == NULL)
4403 return;
4404
4405 focus_region(rr);
4406 center_pointer(rr);
4407 focus_flush();
4408 DNPRINTF(SWM_D_FOCUS, "focusrg: done\n");
4409 }
4410
4411 void
4412 cyclerg(struct binding *bp, struct swm_region *r, union arg *args)
4413 {
4414 union arg a;
4415 struct swm_region *rr = NULL;
4416 int i, num_screens;
4417
4418 num_screens = get_screen_count();
4419 /* do nothing if we don't have more than one screen */
4420 if (!(num_screens > 1 || outputs > 1))
4421 return;
4422
4423 i = r->s->idx;
4424 DNPRINTF(SWM_D_FOCUS, "cyclerg: id: %d, region: %d\n", args->id, i);
4425
4426 switch (args->id) {
4427 case SWM_ARG_ID_CYCLERG_UP:
4428 case SWM_ARG_ID_CYCLERG_MOVE_UP:
4429 rr = TAILQ_NEXT(r, entry);
4430 if (rr == NULL)
4431 rr = TAILQ_FIRST(&screens[i].rl);
4432 break;
4433 case SWM_ARG_ID_CYCLERG_DOWN:
4434 case SWM_ARG_ID_CYCLERG_MOVE_DOWN:
4435 rr = TAILQ_PREV(r, swm_region_list, entry);
4436 if (rr == NULL)
4437 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
4438 break;
4439 default:
4440 return;
4441 };
4442 if (rr == NULL)
4443 return;
4444
4445 switch (args->id) {
4446 case SWM_ARG_ID_CYCLERG_UP:
4447 case SWM_ARG_ID_CYCLERG_DOWN:
4448 focus_region(rr);
4449 center_pointer(rr);
4450 focus_flush();
4451 break;
4452 case SWM_ARG_ID_CYCLERG_MOVE_UP:
4453 case SWM_ARG_ID_CYCLERG_MOVE_DOWN:
4454 a.id = rr->ws->idx;
4455 switchws(bp, r, &a);
4456 break;
4457 default:
4458 return;
4459 };
4460
4461 DNPRINTF(SWM_D_FOCUS, "cyclerg: done\n");
4462 }
4463
4464 /* Sorts transients after parent. */
4465 void
4466 sort_windows(struct ws_win_list *wl)
4467 {
4468 struct ws_win *win, *parent, *nxt;
4469
4470 if (wl == NULL)
4471 return;
4472
4473 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
4474 nxt = TAILQ_NEXT(win, entry);
4475 if (TRANS(win)) {
4476 parent = find_window(win->transient);
4477 if (parent == NULL) {
4478 warnx("not possible bug");
4479 continue;
4480 }
4481 TAILQ_REMOVE(wl, win, entry);
4482 TAILQ_INSERT_AFTER(wl, parent, win, entry);
4483 }
4484 }
4485 }
4486
4487 void
4488 swapwin(struct binding *bp, struct swm_region *r, union arg *args)
4489 {
4490 struct ws_win *target, *source;
4491 struct ws_win *cur_focus;
4492 struct ws_win_list *wl;
4493
4494 (void)bp;
4495
4496 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4497 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
4498
4499 cur_focus = r->ws->focus;
4500 if (cur_focus == NULL || FULLSCREEN(cur_focus))
4501 return;
4502
4503 /* Adjust stacking in floating layer. */
4504 if (ABOVE(cur_focus)) {
4505 switch (args->id) {
4506 case SWM_ARG_ID_SWAPPREV:
4507 target = TAILQ_PREV(cur_focus, ws_win_stack,
4508 stack_entry);
4509 if (target != NULL && FLOATING(target)) {
4510 TAILQ_REMOVE(&cur_focus->ws->stack, cur_focus,
4511 stack_entry);
4512 TAILQ_INSERT_BEFORE(target, cur_focus,
4513 stack_entry);
4514 update_win_stacking(cur_focus);
4515 focus_flush();
4516 }
4517 break;
4518 case SWM_ARG_ID_SWAPNEXT:
4519 target = TAILQ_NEXT(cur_focus, stack_entry);
4520 if (target != NULL && FLOATING(target)) {
4521 TAILQ_REMOVE(&cur_focus->ws->stack, cur_focus,
4522 stack_entry);
4523 TAILQ_INSERT_AFTER(&cur_focus->ws->stack,
4524 target, cur_focus, stack_entry);
4525 update_win_stacking(cur_focus);
4526 focus_flush();
4527 }
4528 break;
4529 }
4530 goto out;
4531 }
4532
4533 if (r->ws->cur_layout == &layouts[SWM_MAX_STACK])
4534 return;
4535
4536 clear_maximized(r->ws);
4537
4538 source = cur_focus;
4539 wl = &source->ws->winlist;
4540
4541 switch (args->id) {
4542 case SWM_ARG_ID_SWAPPREV:
4543 if (TRANS(source))
4544 source = find_window(source->transient);
4545 target = TAILQ_PREV(source, ws_win_list, entry);
4546 if (target && target->transient)
4547 target = find_window(target->transient);
4548 TAILQ_REMOVE(wl, source, entry);
4549 if (target == NULL)
4550 TAILQ_INSERT_TAIL(wl, source, entry);
4551 else
4552 TAILQ_INSERT_BEFORE(target, source, entry);
4553 break;
4554 case SWM_ARG_ID_SWAPNEXT:
4555 target = TAILQ_NEXT(source, entry);
4556 /* move the parent and let the sort handle the move */
4557 if (TRANS(source))
4558 source = find_window(source->transient);
4559 TAILQ_REMOVE(wl, source, entry);
4560 if (target == NULL)
4561 TAILQ_INSERT_HEAD(wl, source, entry);
4562 else
4563 TAILQ_INSERT_AFTER(wl, target, source, entry);
4564 break;
4565 case SWM_ARG_ID_SWAPMAIN:
4566 target = TAILQ_FIRST(wl);
4567 if (target == source) {
4568 if (source->ws->focus_prev != NULL &&
4569 source->ws->focus_prev != target)
4570 source = source->ws->focus_prev;
4571 else
4572 return;
4573 }
4574 if (target == NULL || source == NULL)
4575 return;
4576 source->ws->focus_prev = target;
4577 TAILQ_REMOVE(wl, target, entry);
4578 TAILQ_INSERT_BEFORE(source, target, entry);
4579 TAILQ_REMOVE(wl, source, entry);
4580 TAILQ_INSERT_HEAD(wl, source, entry);
4581 break;
4582 case SWM_ARG_ID_MOVELAST:
4583 TAILQ_REMOVE(wl, source, entry);
4584 TAILQ_INSERT_TAIL(wl, source, entry);
4585 break;
4586 default:
4587 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
4588 return;
4589 }
4590
4591 sort_windows(wl);
4592 ewmh_update_client_list();
4593
4594 stack(r);
4595 center_pointer(r);
4596 focus_flush();
4597 out:
4598 DNPRINTF(SWM_D_MOVE, "swapwin: done\n");
4599 }
4600
4601 struct ws_win *
4602 get_focus_prev(struct ws_win *win)
4603 {
4604 struct ws_win *winfocus = NULL;
4605 struct ws_win *cur_focus = NULL;
4606 struct ws_win_list *wl = NULL;
4607 struct workspace *ws = NULL;
4608
4609 if (!(win && win->ws))
4610 return NULL;
4611
4612 ws = win->ws;
4613 wl = &ws->winlist;
4614 cur_focus = ws->focus;
4615
4616 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: win %#x, cur_focus: %#x, "
4617 "focus_prev: %#x\n", WINID(win), WINID(cur_focus),
4618 WINID(ws->focus_prev));
4619
4620 /* pickle, just focus on whatever */
4621 if (cur_focus == NULL) {
4622 /* use prev_focus if valid */
4623 if (ws->focus_prev && find_window(ws->focus_prev->id))
4624 winfocus = ws->focus_prev;
4625 goto done;
4626 }
4627
4628 /* if transient focus on parent */
4629 if (TRANS(cur_focus)) {
4630 winfocus = find_window(cur_focus->transient);
4631 goto done;
4632 }
4633
4634 /* if in max_stack try harder */
4635 if ((win->quirks & SWM_Q_FOCUSPREV) ||
4636 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
4637 if (cur_focus != ws->focus_prev)
4638 winfocus = ws->focus_prev;
4639 else
4640 winfocus = TAILQ_PREV(win, ws_win_list, entry);
4641 if (winfocus)
4642 goto done;
4643 }
4644
4645 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
4646
4647 if (winfocus == NULL || winfocus == win) {
4648 switch (focus_close) {
4649 case SWM_STACK_BOTTOM:
4650 TAILQ_FOREACH(winfocus, wl, entry)
4651 if (!ICONIC(winfocus) && winfocus != cur_focus)
4652 break;
4653 break;
4654 case SWM_STACK_TOP:
4655 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4656 if (!ICONIC(winfocus) && winfocus != cur_focus)
4657 break;
4658 break;
4659 case SWM_STACK_ABOVE:
4660 winfocus = TAILQ_NEXT(cur_focus, entry);
4661 while (winfocus && ICONIC(winfocus))
4662 winfocus = TAILQ_NEXT(winfocus, entry);
4663
4664 if (winfocus == NULL) {
4665 if (focus_close_wrap) {
4666 TAILQ_FOREACH(winfocus, wl, entry)
4667 if (!ICONIC(winfocus) &&
4668 winfocus != cur_focus)
4669 break;
4670 } else {
4671 TAILQ_FOREACH_REVERSE(winfocus, wl,
4672 ws_win_list, entry)
4673 if (!ICONIC(winfocus) &&
4674 winfocus != cur_focus)
4675 break;
4676 }
4677 }
4678 break;
4679 case SWM_STACK_BELOW:
4680 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
4681 while (winfocus && ICONIC(winfocus))
4682 winfocus = TAILQ_PREV(winfocus, ws_win_list,
4683 entry);
4684
4685 if (winfocus == NULL) {
4686 if (focus_close_wrap) {
4687 TAILQ_FOREACH_REVERSE(winfocus, wl,
4688 ws_win_list, entry)
4689 if (!ICONIC(winfocus) &&
4690 winfocus != cur_focus)
4691 break;
4692 } else {
4693 TAILQ_FOREACH(winfocus, wl, entry)
4694 if (!ICONIC(winfocus) &&
4695 winfocus != cur_focus)
4696 break;
4697 }
4698 }
4699 break;
4700 }
4701 }
4702 done:
4703 if (winfocus == NULL ||
4704 (winfocus && (ICONIC(winfocus) || winfocus == cur_focus))) {
4705 if (focus_default == SWM_STACK_TOP) {
4706 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4707 if (!ICONIC(winfocus) && winfocus != cur_focus)
4708 break;
4709 } else {
4710 TAILQ_FOREACH(winfocus, wl, entry)
4711 if (!ICONIC(winfocus) && winfocus != cur_focus)
4712 break;
4713 }
4714 }
4715
4716 kill_refs(win);
4717
4718 return get_focus_magic(winfocus);
4719 }
4720
4721 struct ws_win *
4722 get_region_focus(struct swm_region *r)
4723 {
4724 struct ws_win *winfocus = NULL;
4725
4726 if (!(r && r->ws))
4727 return NULL;
4728
4729 if (r->ws->focus && !ICONIC(r->ws->focus))
4730 winfocus = r->ws->focus;
4731 else if (r->ws->focus_prev && !ICONIC(r->ws->focus_prev))
4732 winfocus = r->ws->focus_prev;
4733 else
4734 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
4735 if (!ICONIC(winfocus))
4736 break;
4737
4738 return get_focus_magic(winfocus);
4739 }
4740
4741 void
4742 focus(struct binding *bp, struct swm_region *r, union arg *args)
4743 {
4744 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
4745 struct ws_win_list *wl = NULL;
4746 struct workspace *ws = NULL;
4747 union arg a;
4748 int i;
4749
4750 if (!(r && r->ws))
4751 goto out;
4752
4753 cur_focus = r->ws->focus;
4754 ws = r->ws;
4755 wl = &ws->winlist;
4756
4757 DNPRINTF(SWM_D_FOCUS, "focus: id: %d, cur_focus: %#x\n", args->id,
4758 WINID(cur_focus));
4759
4760 /* Make sure an uniconified window has focus, if one exists. */
4761 if (cur_focus == NULL) {
4762 cur_focus = TAILQ_FIRST(wl);
4763 while (cur_focus != NULL && ICONIC(cur_focus))
4764 cur_focus = TAILQ_NEXT(cur_focus, entry);
4765
4766 DNPRINTF(SWM_D_FOCUS, "focus: new cur_focus: %#x\n",
4767 WINID(cur_focus));
4768 }
4769
4770 switch (args->id) {
4771 case SWM_ARG_ID_FOCUSPREV:
4772 if (cur_focus == NULL)
4773 goto out;
4774
4775 winfocus = cur_focus;
4776 do {
4777 winfocus = TAILQ_PREV(winfocus, ws_win_list, entry);
4778 if (winfocus == NULL)
4779 winfocus = TAILQ_LAST(wl, ws_win_list);
4780 if (winfocus == cur_focus)
4781 break;
4782 } while (winfocus && (ICONIC(winfocus) ||
4783 winfocus->id == cur_focus->transient ||
4784 (cur_focus->transient != XCB_WINDOW_NONE &&
4785 winfocus->transient == cur_focus->transient) ||
4786 (winfocus->quirks & SWM_Q_NOFOCUSCYCLE)));
4787 break;
4788 case SWM_ARG_ID_FOCUSNEXT:
4789 if (cur_focus == NULL)
4790 goto out;
4791
4792 winfocus = cur_focus;
4793 do {
4794 winfocus = TAILQ_NEXT(winfocus, entry);
4795 if (winfocus == NULL)
4796 winfocus = TAILQ_FIRST(wl);
4797 if (winfocus == cur_focus)
4798 break;
4799 } while (winfocus && (ICONIC(winfocus) ||
4800 winfocus->id == cur_focus->transient ||
4801 (cur_focus->transient != XCB_WINDOW_NONE &&
4802 winfocus->transient == cur_focus->transient) ||
4803 (winfocus->quirks & SWM_Q_NOFOCUSCYCLE)));
4804 break;
4805 case SWM_ARG_ID_FOCUSMAIN:
4806 if (cur_focus == NULL)
4807 goto out;
4808
4809 winfocus = TAILQ_FIRST(wl);
4810 if (winfocus == cur_focus)
4811 winfocus = cur_focus->ws->focus_prev;
4812 break;
4813 case SWM_ARG_ID_FOCUSURGENT:
4814 /* Search forward for the next urgent window. */
4815 winfocus = NULL;
4816 head = cur_focus;
4817
4818 for (i = 0; i <= workspace_limit; ++i) {
4819 if (head == NULL)
4820 head = TAILQ_FIRST(&r->s->ws[(ws->idx + i) %
4821 workspace_limit].winlist);
4822
4823 while (head) {
4824 if (head == cur_focus) {
4825 if (i > 0) {
4826 winfocus = cur_focus;
4827 break;
4828 }
4829 } else if (get_urgent(head)) {
4830 winfocus = head;
4831 break;
4832 }
4833
4834 head = TAILQ_NEXT(head, entry);
4835 }
4836
4837 if (winfocus)
4838 break;
4839 }
4840
4841 /* Switch ws if new focus is on a different ws. */
4842 if (winfocus && winfocus->ws != ws) {
4843 a.id = winfocus->ws->idx;
4844 switchws(bp, r, &a);
4845 }
4846 break;
4847 default:
4848 goto out;
4849 }
4850
4851 if (clear_maximized(ws) > 0)
4852 stack(r);
4853
4854 focus_win(get_focus_magic(winfocus));
4855 center_pointer(r);
4856 focus_flush();
4857
4858 out:
4859 DNPRINTF(SWM_D_FOCUS, "focus: done\n");
4860 }
4861
4862 void
4863 focus_pointer(struct binding *bp, struct swm_region *r, union arg *args)
4864 {
4865 (void)args;
4866
4867 /* Not needed for buttons since this is already done in buttonpress. */
4868 if (bp->type == KEYBIND) {
4869 focus_win(get_pointer_win(r->s->root));
4870 focus_flush();
4871 }
4872 }
4873
4874 void
4875 cycle_layout(struct binding *bp, struct swm_region *r, union arg *args)
4876 {
4877 struct workspace *ws = r->ws;
4878
4879 /* suppress unused warning since var is needed */
4880 (void)bp;
4881 (void)args;
4882
4883 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
4884
4885 ws->cur_layout++;
4886 if (ws->cur_layout->l_stack == NULL)
4887 ws->cur_layout = &layouts[0];
4888
4889 clear_maximized(ws);
4890
4891 stack(r);
4892 bar_draw(r->bar);
4893
4894 focus_win(get_region_focus(r));
4895
4896 center_pointer(r);
4897 focus_flush();
4898 }
4899
4900 void
4901 stack_config(struct binding *bp, struct swm_region *r, union arg *args)
4902 {
4903 struct workspace *ws = r->ws;
4904
4905 (void)bp;
4906
4907 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
4908 args->id, ws->idx);
4909
4910 if (clear_maximized(ws) > 0)
4911 stack(r);
4912
4913 if (ws->cur_layout->l_config != NULL)
4914 ws->cur_layout->l_config(ws, args->id);
4915
4916 if (args->id != SWM_ARG_ID_STACKINIT)
4917 stack(r);
4918 bar_draw(r->bar);
4919
4920 center_pointer(r);
4921 focus_flush();
4922 }
4923
4924 void
4925 stack(struct swm_region *r) {
4926 struct swm_geometry g;
4927 struct swm_region *r_prev;
4928 uint32_t val[2];
4929
4930 if (r == NULL)
4931 return;
4932
4933 DNPRINTF(SWM_D_STACK, "stack: begin\n");
4934
4935 /* Adjust stack area for region bar and padding. */
4936 g = r->g;
4937 g.x += region_padding;
4938 g.y += region_padding;
4939 g.w -= 2 * border_width + 2 * region_padding;
4940 g.h -= 2 * border_width + 2 * region_padding;
4941 if (bar_enabled && r->ws->bar_enabled) {
4942 if (!bar_at_bottom)
4943 g.y += bar_height;
4944 g.h -= bar_height;
4945 }
4946
4947 DNPRINTF(SWM_D_STACK, "stack: workspace: %d (screen: "
4948 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4949 r->ws->idx, r->s->idx, get_region_index(r), g.x, g.y, g.w, g.h);
4950
4951 r_prev = TAILQ_PREV(r, swm_region_list, entry);
4952 if (r_prev) {
4953 /* Stack bar/input relative to prev. region. */
4954 val[1] = XCB_STACK_MODE_ABOVE;
4955
4956 val[0] = r_prev->id;
4957 DNPRINTF(SWM_D_STACK, "stack: region input %#x "
4958 "relative to %#x.\n", r->id, val[0]);
4959 xcb_configure_window(conn, r->id,
4960 XCB_CONFIG_WINDOW_SIBLING |
4961 XCB_CONFIG_WINDOW_STACK_MODE, val);
4962
4963 val[0] = r_prev->bar->id;
4964 DNPRINTF(SWM_D_STACK, "stack: region bar %#x "
4965 "relative to %#x.\n", r->bar->id, val[0]);
4966 xcb_configure_window(conn, r->bar->id,
4967 XCB_CONFIG_WINDOW_SIBLING |
4968 XCB_CONFIG_WINDOW_STACK_MODE, val);
4969 }
4970
4971 r->ws->cur_layout->l_stack(r->ws, &g);
4972 r->ws->cur_layout->l_string(r->ws);
4973 /* save r so we can track region changes */
4974 r->ws->old_r = r;
4975
4976 if (font_adjusted)
4977 font_adjusted--;
4978
4979 DNPRINTF(SWM_D_STACK, "stack: end\n");
4980 }
4981
4982 void
4983 store_float_geom(struct ws_win *win)
4984 {
4985 if (win == NULL || win->ws->r == NULL)
4986 return;
4987
4988 /* retain window geom and region geom */
4989 win->g_float = win->g;
4990 win->g_float.x -= X(win->ws->r);
4991 win->g_float.y -= Y(win->ws->r);
4992 win->g_floatvalid = true;
4993 DNPRINTF(SWM_D_MISC, "store_float_geom: win %#x, g: (%d,%d)"
4994 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
4995 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
4996 win->g_float.w, win->g_float.h);
4997 }
4998
4999 void
5000 load_float_geom(struct ws_win *win)
5001 {
5002 if (win == NULL || win->ws->r == NULL)
5003 return;
5004
5005 if (win->g_floatvalid) {
5006 win->g = win->g_float;
5007 X(win) += X(win->ws->r);
5008 Y(win) += Y(win->ws->r);
5009 DNPRINTF(SWM_D_MISC, "load_float_geom: win %#x, g: (%d,%d)"
5010 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
5011 HEIGHT(win));
5012 } else {
5013 DNPRINTF(SWM_D_MISC, "load_float_geom: win %#x, g_float "
5014 "is not set.\n", win->id);
5015 }
5016 }
5017
5018 void
5019 update_floater(struct ws_win *win)
5020 {
5021 struct workspace *ws;
5022 struct swm_region *r;
5023
5024 DNPRINTF(SWM_D_MISC, "update_floater: win %#x\n", WINID(win));
5025
5026 if (win == NULL)
5027 return;
5028
5029 ws = win->ws;
5030
5031 if ((r = ws->r) == NULL)
5032 return;
5033
5034 win->bordered = true;
5035
5036 if (FULLSCREEN(win)) {
5037 /* _NET_WM_FULLSCREEN: fullscreen without border. */
5038 if (!win->g_floatvalid)
5039 store_float_geom(win);
5040
5041 win->g = r->g;
5042 win->bordered = false;
5043 } else if (MAXIMIZED(win)) {
5044 /* Maximize: like a single stacked window. */
5045 if (!win->g_floatvalid)
5046 store_float_geom(win);
5047
5048 win->g = r->g;
5049
5050 if (bar_enabled && ws->bar_enabled && !maximize_hide_bar) {
5051 if (!bar_at_bottom)
5052 Y(win) += bar_height;
5053 HEIGHT(win) -= bar_height;
5054 } else if (disable_border) {
5055 win->bordered = false;
5056 }
5057
5058 if (win->bordered) {
5059 /* Window geometry excludes frame. */
5060 X(win) += border_width;
5061 Y(win) += border_width;
5062 HEIGHT(win) -= 2 * border_width;
5063 WIDTH(win) -= 2 * border_width;
5064 }
5065 } else {
5066 /* Normal floating window. */
5067 /* Update geometry if new region. */
5068 if (r != ws->old_r)
5069 load_float_geom(win);
5070
5071 if (((win->quirks & SWM_Q_FULLSCREEN) &&
5072 WIDTH(win) >= WIDTH(r) && HEIGHT(win) >= HEIGHT(r)) ||
5073 ((!WS_FOCUSED(win->ws) || win->ws->focus != win) &&
5074 (win->quirks & SWM_Q_MINIMALBORDER))) {
5075 /* Remove border */
5076 win->bordered = false;
5077 } else if (!MANUAL(win)) {
5078 if (TRANS(win) && (win->quirks & SWM_Q_TRANSSZ)) {
5079 /* Adjust size on TRANSSZ quirk. */
5080 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
5081 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
5082 }
5083
5084 if (!(win->quirks & SWM_Q_ANYWHERE)) {
5085 /*
5086 * Floaters and transients are auto-centred
5087 * unless manually moved, resized or ANYWHERE
5088 * quirk is set.
5089 */
5090 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2;
5091 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2;
5092 store_float_geom(win);
5093 }
5094 }
5095 }
5096
5097 /* Ensure at least 1 pixel of the window is in the region. */
5098 region_containment(win, r, SWM_CW_ALLSIDES);
5099
5100 update_window(win);
5101 }
5102
5103 /*
5104 * Send keystrokes to terminal to decrease/increase the font size as the
5105 * window size changes.
5106 */
5107 void
5108 adjust_font(struct ws_win *win)
5109 {
5110 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
5111 ABOVE(win) || TRANS(win))
5112 return;
5113
5114 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
5115 WIDTH(win) / win->sh.width_inc < term_width &&
5116 win->font_steps < SWM_MAX_FONT_STEPS) {
5117 win->font_size_boundary[win->font_steps] =
5118 (win->sh.width_inc * term_width) + win->sh.base_width;
5119 win->font_steps++;
5120 font_adjusted++;
5121 win->last_inc = win->sh.width_inc;
5122 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
5123 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
5124 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
5125 win->font_steps--;
5126 font_adjusted++;
5127 win->last_inc = win->sh.width_inc;
5128 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
5129 }
5130 }
5131
5132 #define SWAPXY(g) do { \
5133 int tmp; \
5134 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
5135 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
5136 } while (0)
5137 void
5138 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, bool flip)
5139 {
5140 struct swm_geometry cell, r_g = *g;
5141 struct ws_win *win;
5142 int i = 0, j = 0, s = 0, stacks = 0;
5143 int w_inc = 1, h_inc, w_base = 1, h_base;
5144 int hrh = 0, extra = 0, h_slice = 0, last_h = 0;
5145 int split = 0, colno = 0;
5146 int winno, mwin = 0, msize = 0;
5147 int remain, missing, v_slice, mscale;
5148 bool bordered = true, reconfigure = false;
5149
5150 /*
5151 * cell: geometry for window, including frame.
5152 * mwin: # of windows in master area.
5153 * mscale: size increment of master area.
5154 * stacks: # of stack columns
5155 */
5156
5157 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
5158 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
5159
5160 memset(&cell, 0, sizeof(cell));
5161
5162 /* Prepare tiling variables, if needed. */
5163 if ((winno = count_win(ws, false)) > 0) {
5164 /* Find first tiled window. */
5165 TAILQ_FOREACH(win, &ws->winlist, entry)
5166 if (!FLOATING(win) && !ICONIC(win))
5167 break;
5168
5169 /* Take into account size hints of first tiled window. */
5170 if (rot) {
5171 w_inc = win->sh.width_inc;
5172 w_base = win->sh.base_width;
5173 mwin = ws->l_state.horizontal_mwin;
5174 mscale = ws->l_state.horizontal_msize;
5175 stacks = ws->l_state.horizontal_stacks;
5176 SWAPXY(&r_g);
5177 } else {
5178 w_inc = win->sh.height_inc;
5179 w_base = win->sh.base_height;
5180 mwin = ws->l_state.vertical_mwin;
5181 mscale = ws->l_state.vertical_msize;
5182 stacks = ws->l_state.vertical_stacks;
5183 }
5184
5185 cell = r_g;
5186 cell.x += border_width;
5187 cell.y += border_width;
5188
5189 if (stacks > winno - mwin)
5190 stacks = winno - mwin;
5191 if (stacks < 1)
5192 stacks = 1;
5193
5194 h_slice = r_g.h / SWM_H_SLICE;
5195 if (mwin && winno > mwin) {
5196 v_slice = r_g.w / SWM_V_SLICE;
5197
5198 split = mwin;
5199 colno = split;
5200 cell.w = v_slice * mscale;
5201
5202 if (w_inc > 1 && w_inc < v_slice) {
5203 /* Adjust for requested size increment. */
5204 remain = (cell.w - w_base) % w_inc;
5205 cell.w -= remain;
5206 }
5207
5208 msize = cell.w;
5209 if (flip)
5210 cell.x += r_g.w - msize;
5211 } else {
5212 msize = -2;
5213 colno = split = winno / stacks;
5214 cell.w = ((r_g.w - (stacks * 2 * border_width) +
5215 2 * border_width) / stacks);
5216 }
5217
5218 hrh = r_g.h / colno;
5219 extra = r_g.h - (colno * hrh);
5220 cell.h = hrh - 2 * border_width;
5221 i = j = 0, s = stacks;
5222 }
5223
5224 /* Update window geometry. */
5225 TAILQ_FOREACH(win, &ws->winlist, entry) {
5226 if (ICONIC(win))
5227 continue;
5228
5229 if (FLOATING(win)) {
5230 update_floater(win);
5231 continue;
5232 }
5233
5234 /* Tiled. */
5235 if (split && i == split) {
5236 colno = (winno - mwin) / stacks;
5237 if (s <= (winno - mwin) % stacks)
5238 colno++;
5239 split += colno;
5240 hrh = r_g.h / colno;
5241 extra = r_g.h - (colno * hrh);
5242
5243 if (!flip)
5244 cell.x += cell.w + 2 * border_width +
5245 tile_gap;
5246
5247 cell.w = (r_g.w - msize -
5248 (stacks * (2 * border_width + tile_gap))) / stacks;
5249 if (s == 1)
5250 cell.w += (r_g.w - msize -
5251 (stacks * (2 * border_width + tile_gap))) %
5252 stacks;
5253
5254 if (flip)
5255 cell.x -= cell.w + 2 * border_width +
5256 tile_gap;
5257 s--;
5258 j = 0;
5259 }
5260
5261 cell.h = hrh - 2 * border_width - tile_gap;
5262
5263 if (rot) {
5264 h_inc = win->sh.width_inc;
5265 h_base = win->sh.base_width;
5266 } else {
5267 h_inc = win->sh.height_inc;
5268 h_base = win->sh.base_height;
5269 }
5270
5271 if (j == colno - 1) {
5272 cell.h = hrh + extra;
5273 } else if (h_inc > 1 && h_inc < h_slice) {
5274 /* adjust for window's requested size increment */
5275 remain = (cell.h - h_base) % h_inc;
5276 missing = h_inc - remain;
5277
5278 if (missing <= extra || j == 0) {
5279 extra -= missing;
5280 cell.h += missing;
5281 } else {
5282 cell.h -= remain;
5283 extra += remain;
5284 }
5285 }
5286
5287 if (j == 0)
5288 cell.y = r_g.y + border_width;
5289 else
5290 cell.y += last_h + 2 * border_width + tile_gap;
5291
5292 /* Window coordinates exclude frame. */
5293
5294 if (winno > 1 || !disable_border ||
5295 (bar_enabled && ws->bar_enabled)) {
5296 bordered = true;
5297 } else {
5298 bordered = false;
5299 }
5300
5301 if (rot) {
5302 if (X(win) != cell.y || Y(win) != cell.x ||
5303 WIDTH(win) != cell.h || HEIGHT(win) != cell.w) {
5304 reconfigure = true;
5305 X(win) = cell.y;
5306 Y(win) = cell.x;
5307 WIDTH(win) = cell.h;
5308 HEIGHT(win) = cell.w;
5309 }
5310 } else {
5311 if (X(win) != cell.x || Y(win) != cell.y ||
5312 WIDTH(win) != cell.w || HEIGHT(win) != cell.h) {
5313 reconfigure = true;
5314 X(win) = cell.x;
5315 Y(win) = cell.y;
5316 WIDTH(win) = cell.w;
5317 HEIGHT(win) = cell.h;
5318 }
5319 }
5320
5321 if (!bordered) {
5322 X(win) -= border_width;
5323 Y(win) -= border_width;
5324 WIDTH(win) += 2 * border_width;
5325 HEIGHT(win) += 2 * border_width;
5326 }
5327
5328 if (bordered != win->bordered) {
5329 reconfigure = true;
5330 win->bordered = bordered;
5331 }
5332
5333 if (reconfigure) {
5334 adjust_font(win);
5335 update_window(win);
5336 }
5337
5338 last_h = cell.h;
5339 i++;
5340 j++;
5341 }
5342
5343 /* Stack all windows from bottom up. */
5344 TAILQ_FOREACH_REVERSE(win, &ws->stack, ws_win_stack, stack_entry)
5345 if (!ICONIC(win))
5346 update_win_stacking(win);
5347
5348 /* Map all windows from top down. */
5349 TAILQ_FOREACH(win, &ws->stack, stack_entry)
5350 if (!ICONIC(win))
5351 map_window(win);
5352
5353 DNPRINTF(SWM_D_STACK, "stack_master: done\n");
5354 }
5355
5356 void
5357 vertical_config(struct workspace *ws, int id)
5358 {
5359 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
5360 id, ws->idx);
5361
5362 switch (id) {
5363 case SWM_ARG_ID_STACKRESET:
5364 case SWM_ARG_ID_STACKINIT:
5365 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
5366 ws->l_state.vertical_mwin = 1;
5367 ws->l_state.vertical_stacks = 1;
5368 break;
5369 case SWM_ARG_ID_MASTERSHRINK:
5370 if (ws->l_state.vertical_msize > 1)
5371 ws->l_state.vertical_msize--;
5372 break;
5373 case SWM_ARG_ID_MASTERGROW:
5374 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
5375 ws->l_state.vertical_msize++;
5376 break;
5377 case SWM_ARG_ID_MASTERADD:
5378 ws->l_state.vertical_mwin++;
5379 break;
5380 case SWM_ARG_ID_MASTERDEL:
5381 if (ws->l_state.vertical_mwin > 0)
5382 ws->l_state.vertical_mwin--;
5383 break;
5384 case SWM_ARG_ID_STACKBALANCE:
5385 ws->l_state.vertical_msize = SWM_V_SLICE / (ws->l_state.vertical_stacks + 1);
5386 break;
5387 case SWM_ARG_ID_STACKINC:
5388 ws->l_state.vertical_stacks++;
5389 break;
5390 case SWM_ARG_ID_STACKDEC:
5391 if (ws->l_state.vertical_stacks > 1)
5392 ws->l_state.vertical_stacks--;
5393 break;
5394 case SWM_ARG_ID_FLIPLAYOUT:
5395 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
5396 break;
5397 default:
5398 return;
5399 }
5400 }
5401
5402 void
5403 vertical_stack(struct workspace *ws, struct swm_geometry *g)
5404 {
5405 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
5406
5407 stack_master(ws, g, 0, ws->l_state.vertical_flip);
5408 }
5409
5410 void
5411 horizontal_config(struct workspace *ws, int id)
5412 {
5413 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
5414
5415 switch (id) {
5416 case SWM_ARG_ID_STACKRESET:
5417 case SWM_ARG_ID_STACKINIT:
5418 ws->l_state.horizontal_mwin = 1;
5419 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
5420 ws->l_state.horizontal_stacks = 1;
5421 break;
5422 case SWM_ARG_ID_MASTERSHRINK:
5423 if (ws->l_state.horizontal_msize > 1)
5424 ws->l_state.horizontal_msize--;
5425 break;
5426 case SWM_ARG_ID_MASTERGROW:
5427 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
5428 ws->l_state.horizontal_msize++;
5429 break;
5430 case SWM_ARG_ID_MASTERADD:
5431 ws->l_state.horizontal_mwin++;
5432 break;
5433 case SWM_ARG_ID_MASTERDEL:
5434 if (ws->l_state.horizontal_mwin > 0)
5435 ws->l_state.horizontal_mwin--;
5436 break;
5437 case SWM_ARG_ID_STACKBALANCE:
5438 ws->l_state.horizontal_msize = SWM_H_SLICE / (ws->l_state.horizontal_stacks + 1);
5439 break;
5440 case SWM_ARG_ID_STACKINC:
5441 ws->l_state.horizontal_stacks++;
5442 break;
5443 case SWM_ARG_ID_STACKDEC:
5444 if (ws->l_state.horizontal_stacks > 1)
5445 ws->l_state.horizontal_stacks--;
5446 break;
5447 case SWM_ARG_ID_FLIPLAYOUT:
5448 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
5449 break;
5450 default:
5451 return;
5452 }
5453 }
5454
5455 void
5456 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
5457 {
5458 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
5459
5460 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
5461 }
5462
5463 /* fullscreen view */
5464 void
5465 max_stack(struct workspace *ws, struct swm_geometry *g)
5466 {
5467 struct swm_geometry gg = *g;
5468 struct ws_win *w, *win = NULL, *parent = NULL, *tmpw;
5469 int winno;
5470
5471 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
5472
5473 if (ws == NULL)
5474 return;
5475
5476 winno = count_win(ws, false);
5477 if (winno == 0 && count_win(ws, true) == 0)
5478 return;
5479
5480 /* Figure out which top level window should be visible. */
5481 if (ws->focus_pending)
5482 win = ws->focus_pending;
5483 else if (ws->focus)
5484 win = ws->focus;
5485 else if (ws->focus_prev)
5486 win = ws->focus_prev;
5487 else
5488 win = TAILQ_FIRST(&ws->winlist);
5489
5490 DNPRINTF(SWM_D_STACK, "max_stack: focus_pending: %#x, focus: %#x, "
5491 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws->focus_pending),
5492 WINID(ws->focus), WINID(ws->focus_prev),
5493 WINID(TAILQ_FIRST(&ws->winlist)), win->id);
5494
5495 /* Update window geometry. */
5496 TAILQ_FOREACH(w, &ws->winlist, entry) {
5497 if (ICONIC(w))
5498 continue;
5499
5500 if (TRANS(w)) {
5501 update_floater(w);
5502 continue;
5503 }
5504
5505 /* Set maximized flag for all maxed windows. */
5506 if (!MAXIMIZED(w)) {
5507 /* Preserve floating geometry. */
5508 if (ABOVE(w))
5509 store_float_geom(w);
5510
5511 ewmh_apply_flags(w, w->ewmh_flags | EWMH_F_MAXIMIZED);
5512 ewmh_update_wm_state(w);
5513 }
5514
5515 /* Only reconfigure if necessary. */
5516 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
5517 HEIGHT(w) != gg.h) {
5518 w->g = gg;
5519
5520 if (disable_border && !(bar_enabled && ws->bar_enabled)) {
5521 w->bordered = false;
5522 WIDTH(w) += 2 * border_width;
5523 HEIGHT(w) += 2 * border_width;
5524 } else {
5525 w->bordered = true;
5526 X(w) += border_width;
5527 Y(w) += border_width;
5528 }
5529
5530 update_window(w);
5531 }
5532 }
5533
5534 /* If transient, stack parent and its children. */
5535 if (TRANS(win) && (parent = find_window(win->transient))) {
5536 raise_window(parent);
5537
5538 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
5539 if (w->transient == parent->id)
5540 raise_window(w);
5541 }
5542
5543 /* Make sure focus window is on top. */
5544 raise_window(win);
5545
5546 /* Stack any children of focus window. */
5547 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
5548 if (w->transient == win->id)
5549 raise_window(w);
5550
5551 /* Map all windows. */
5552 TAILQ_FOREACH(w, &ws->stack, stack_entry)
5553 if (!ICONIC(w))
5554 map_window(w);
5555 }
5556
5557 void
5558 send_to_rg(struct binding *bp, struct swm_region *r, union arg *args)
5559 {
5560 int ridx = args->id, i, num_screens;
5561 struct swm_region *rr = NULL;
5562 union arg a;
5563
5564 num_screens = get_screen_count();
5565 /* do nothing if we don't have more than one screen */
5566 if (!(num_screens > 1 || outputs > 1))
5567 return;
5568
5569 DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
5570
5571 rr = TAILQ_FIRST(&r->s->rl);
5572 for (i = 0; i < ridx && rr != NULL; ++i)
5573 rr = TAILQ_NEXT(rr, entry);
5574
5575 if (rr == NULL)
5576 return;
5577
5578 a.id = rr->ws->idx;
5579
5580 send_to_ws(bp, r, &a);
5581 }
5582
5583 struct swm_region *
5584 region_under(struct swm_screen *s, int x, int y)
5585 {
5586 struct swm_region *r = NULL;
5587
5588 TAILQ_FOREACH(r, &s->rl, entry) {
5589 DNPRINTF(SWM_D_MISC, "region_under: ws: %d, region g: (%d,%d) "
5590 "%d x %d, coords: (%d,%d)\n", r->ws->idx, X(r), Y(r),
5591 WIDTH(r), HEIGHT(r), x, y);
5592 if (X(r) <= x && x < MAX_X(r))
5593 if (Y(r) <= y && y < MAX_Y(r))
5594 return (r);
5595 }
5596
5597 return (NULL);
5598 }
5599
5600 /* Transfer focused window to target workspace and focus. */
5601 void
5602 send_to_ws(struct binding *bp, struct swm_region *r, union arg *args)
5603 {
5604 int wsid = args->id;
5605 struct ws_win *win = NULL;
5606
5607 (void)bp;
5608
5609 if (r && r->ws && r->ws->focus)
5610 win = r->ws->focus;
5611 else
5612 return;
5613
5614 DNPRINTF(SWM_D_MOVE, "send_to_ws: win %#x, ws %d\n", win->id, wsid);
5615
5616 if (wsid < 0 || wsid >= workspace_limit)
5617 return;
5618
5619 if (win->ws->idx == wsid)
5620 return;
5621
5622 win_to_ws(win, wsid, true);
5623
5624 /* Set new focus on target ws. */
5625 if (focus_mode != SWM_FOCUS_FOLLOW) {
5626 win->ws->focus_prev = win->ws->focus;
5627 win->ws->focus = win;
5628 win->ws->focus_pending = NULL;
5629
5630 if (win->ws->focus_prev)
5631 draw_frame(win->ws->focus_prev);
5632 }
5633
5634 DNPRINTF(SWM_D_STACK, "send_to_ws: focus_pending: %#x, focus: %#x, "
5635 "focus_prev: %#x, first: %#x, win: %#x\n",
5636 WINID(r->ws->focus_pending), WINID(r->ws->focus),
5637 WINID(r->ws->focus_prev), WINID(TAILQ_FIRST(&r->ws->winlist)),
5638 win->id);
5639
5640 ewmh_apply_flags(win, win->ewmh_flags & ~EWMH_F_MAXIMIZED);
5641 ewmh_update_wm_state(win);
5642
5643 /* Restack focused region. */
5644 stack(r);
5645
5646 /* If destination ws has a region, restack. */
5647 if (win->ws->r) {
5648 if (FLOATING(win))
5649 load_float_geom(win);
5650
5651 stack(win->ws->r);
5652 }
5653
5654 /* Set new focus on current ws. */
5655 if (focus_mode != SWM_FOCUS_FOLLOW) {
5656 if (r->ws->focus != NULL) {
5657 focus_win(r->ws->focus);
5658 } else {
5659 DNPRINTF(SWM_D_FOCUS, "send_to_ws: set_input_focus: "
5660 "%#x, revert-to: parent, time: 0\n", r->id);
5661 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
5662 r->id, XCB_CURRENT_TIME);
5663 bar_draw(r->bar);
5664 }
5665 }
5666
5667 center_pointer(r);
5668 focus_flush();
5669 }
5670
5671 void
5672 win_to_ws(struct ws_win *win, int wsid, bool unfocus)
5673 {
5674 struct ws_win *parent;
5675 struct workspace *ws, *nws, *pws;
5676
5677 if (wsid < 0 || wsid >= workspace_limit)
5678 return;
5679
5680 if (win->ws->idx == wsid)
5681 return;
5682
5683 ws = win->ws;
5684 nws = &win->s->ws[wsid];
5685
5686 DNPRINTF(SWM_D_MOVE, "win_to_ws: win %#x, ws %d -> %d\n", win->id,
5687 ws->idx, wsid);
5688
5689 /* Cleanup focus on source ws. */
5690 if (focus_mode != SWM_FOCUS_FOLLOW &&
5691 (ws->focus == win || ws->focus_pending == win))
5692 ws->focus_pending = get_focus_prev(win);
5693
5694 /* Move the parent if this is a transient window. */
5695 if (TRANS(win)) {
5696 parent = find_window(win->transient);
5697 if (parent) {
5698 pws = parent->ws;
5699 /* Set new focus in parent's ws if needed. */
5700 if (pws->focus == parent) {
5701 if (focus_mode != SWM_FOCUS_FOLLOW)
5702 pws->focus_pending =
5703 get_focus_prev(parent);
5704
5705 unfocus_win(parent);
5706
5707 if (focus_mode != SWM_FOCUS_FOLLOW) {
5708 pws->focus = pws->focus_pending;
5709 pws->focus_pending = NULL;
5710 }
5711 }
5712
5713 /* Don't unmap parent if new ws is visible */
5714 if (nws->r == NULL)
5715 unmap_window(parent);
5716
5717 /* Transfer */
5718 TAILQ_REMOVE(&ws->winlist, parent, entry);
5719 TAILQ_REMOVE(&ws->stack, parent, stack_entry);
5720 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
5721 TAILQ_INSERT_TAIL(&nws->stack, parent, stack_entry);
5722 parent->ws = nws;
5723
5724 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: "
5725 "_NET_WM_DESKTOP: %d\n", wsid);
5726 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5727 parent->id, ewmh[_NET_WM_DESKTOP].atom,
5728 XCB_ATOM_CARDINAL, 32, 1, &wsid);
5729 }
5730 }
5731
5732 if (unfocus)
5733 unfocus_win(win);
5734
5735 if (ws->focus_prev == win)
5736 ws->focus_prev = NULL;
5737
5738 if (focus_mode != SWM_FOCUS_FOLLOW && ws->focus_pending != NULL) {
5739 ws->focus = ws->focus_pending;
5740 ws->focus_pending = NULL;
5741 }
5742
5743 /* Don't unmap if new ws is visible */
5744 if (nws->r == NULL)
5745 unmap_window(win);
5746
5747 /* Transfer */
5748 TAILQ_REMOVE(&ws->winlist, win, entry);
5749 TAILQ_REMOVE(&ws->stack, win, stack_entry);
5750 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
5751 TAILQ_INSERT_TAIL(&nws->stack, win, stack_entry);
5752 win->ws = nws;
5753
5754 /* Update the window's workspace property: _NET_WM_DESKTOP */
5755 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: "
5756 "_NET_WM_DESKTOP: %d\n", wsid);
5757 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
5758 ewmh[_NET_WM_DESKTOP].atom, XCB_ATOM_CARDINAL, 32, 1, &wsid);
5759
5760 ewmh_update_client_list();
5761
5762 DNPRINTF(SWM_D_MOVE, "win_to_ws: done.\n");
5763 }
5764
5765 void
5766 pressbutton(struct binding *bp, struct swm_region *r, union arg *args)
5767 {
5768 /* suppress unused warning since var is needed */
5769 (void)bp;
5770 (void)r;
5771
5772 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
5773 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5774 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
5775 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5776 }
5777
5778 void
5779 raise_toggle(struct binding *bp, struct swm_region *r, union arg *args)
5780 {
5781 /* Suppress warning. */
5782 (void)bp;
5783 (void)args;
5784
5785 if (r == NULL || r->ws == NULL)
5786 return;
5787
5788 if (r->ws->focus && MAXIMIZED(r->ws->focus))
5789 return;
5790
5791 r->ws->always_raise = !r->ws->always_raise;
5792
5793 /* Update focused win stacking order based on new always_raise value. */
5794 raise_window(r->ws->focus);
5795
5796 focus_flush();
5797 }
5798
5799 void
5800 iconify(struct binding *bp, struct swm_region *r, union arg *args)
5801 {
5802 struct ws_win *w;
5803
5804 /* Suppress warning. */
5805 (void)bp;
5806 (void)args;
5807
5808 if ((w = r->ws->focus) == NULL)
5809 return;
5810
5811 ewmh_apply_flags(w, w->ewmh_flags | EWMH_F_HIDDEN);
5812 ewmh_update_wm_state(w);
5813
5814 stack(r);
5815
5816 focus_flush();
5817 }
5818
5819 char *
5820 get_win_name(xcb_window_t win)
5821 {
5822 char *name = NULL;
5823 xcb_get_property_cookie_t c;
5824 xcb_get_property_reply_t *r;
5825
5826 /* First try _NET_WM_NAME for UTF-8. */
5827 c = xcb_get_property(conn, 0, win, ewmh[_NET_WM_NAME].atom,
5828 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5829 r = xcb_get_property_reply(conn, c, NULL);
5830 if (r && r->type == XCB_NONE) {
5831 free(r);
5832 /* Use WM_NAME instead; no UTF-8. */
5833 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
5834 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5835 r = xcb_get_property_reply(conn, c, NULL);
5836 }
5837
5838 if (r && r->type != XCB_NONE && r->length > 0)
5839 name = strndup(xcb_get_property_value(r),
5840 xcb_get_property_value_length(r));
5841 else
5842 name = strdup("");
5843
5844 if (name == NULL)
5845 err(1, "get_win_name: failed to allocate memory.");
5846
5847 free(r);
5848
5849 return (name);
5850 }
5851
5852 void
5853 uniconify(struct binding *bp, struct swm_region *r, union arg *args)
5854 {
5855 struct ws_win *win;
5856 FILE *lfile;
5857 char *name;
5858 int count = 0;
5859
5860 (void)bp;
5861
5862 DNPRINTF(SWM_D_MISC, "uniconify\n");
5863
5864 if (r == NULL || r->ws == NULL)
5865 return;
5866
5867 /* make sure we have anything to uniconify */
5868 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5869 if (win->ws == NULL)
5870 continue; /* should never happen */
5871 if (!ICONIC(win))
5872 continue;
5873 count++;
5874 }
5875
5876 DNPRINTF(SWM_D_MISC, "uniconify: count: %d\n", count);
5877
5878 if (count == 0)
5879 return;
5880
5881 search_r = r;
5882 search_resp_action = SWM_SEARCH_UNICONIFY;
5883
5884 spawn_select(r, args, "search", &searchpid);
5885
5886 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5887 return;
5888
5889 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5890 if (win->ws == NULL)
5891 continue; /* should never happen */
5892 if (!ICONIC(win))
5893 continue;
5894
5895 name = get_win_name(win->id);
5896 fprintf(lfile, "%s.%u\n", name, win->id);
5897 free(name);
5898 }
5899
5900 fclose(lfile);
5901 }
5902
5903 void
5904 name_workspace(struct binding *bp, struct swm_region *r, union arg *args)
5905 {
5906 FILE *lfile;
5907
5908 (void)bp;
5909
5910 DNPRINTF(SWM_D_MISC, "name_workspace\n");
5911
5912 if (r == NULL)
5913 return;
5914
5915 search_r = r;
5916 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
5917
5918 spawn_select(r, args, "name_workspace", &searchpid);
5919
5920 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5921 return;
5922
5923 fprintf(lfile, "%s", "");
5924 fclose(lfile);
5925 }
5926
5927 void
5928 search_workspace(struct binding *bp, struct swm_region *r, union arg *args)
5929 {
5930 int i;
5931 struct workspace *ws;
5932 FILE *lfile;
5933
5934 (void)bp;
5935
5936 DNPRINTF(SWM_D_MISC, "search_workspace\n");
5937
5938 if (r == NULL)
5939 return;
5940
5941 search_r = r;
5942 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
5943
5944 spawn_select(r, args, "search", &searchpid);
5945
5946 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5947 return;
5948
5949 for (i = 0; i < workspace_limit; i++) {
5950 ws = &r->s->ws[i];
5951 if (ws == NULL)
5952 continue;
5953 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
5954 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
5955 }
5956
5957 fclose(lfile);
5958 }
5959
5960 void
5961 search_win_cleanup(void)
5962 {
5963 struct search_window *sw = NULL;
5964
5965 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
5966 xcb_destroy_window(conn, sw->indicator);
5967 TAILQ_REMOVE(&search_wl, sw, entry);
5968 free(sw);
5969 }
5970 }
5971
5972 void
5973 search_win(struct binding *bp, struct swm_region *r, union arg *args)
5974 {
5975 struct ws_win *win = NULL;
5976 struct search_window *sw = NULL;
5977 xcb_window_t w;
5978 uint32_t wa[3];
5979 xcb_screen_t *screen;
5980 int i, width, height;
5981 char s[8];
5982 FILE *lfile;
5983 size_t len;
5984 XftDraw *draw;
5985 XGlyphInfo info;
5986 GC l_draw;
5987 XGCValues l_gcv;
5988 XRectangle l_ibox, l_lbox;
5989
5990 (void)bp;
5991
5992 DNPRINTF(SWM_D_MISC, "search_win\n");
5993
5994 search_r = r;
5995 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
5996
5997 spawn_select(r, args, "search", &searchpid);
5998
5999 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
6000 return;
6001
6002 if ((screen = get_screen(r->s->idx)) == NULL)
6003 errx(1, "ERROR: can't get screen %d.", r->s->idx);
6004
6005 TAILQ_INIT(&search_wl);
6006
6007 i = 1;
6008 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
6009 if (ICONIC(win))
6010 continue;
6011
6012 sw = calloc(1, sizeof(struct search_window));
6013 if (sw == NULL) {
6014 warn("search_win: calloc");
6015 fclose(lfile);
6016 search_win_cleanup();
6017 return;
6018 }
6019 sw->idx = i;
6020 sw->win = win;
6021
6022 snprintf(s, sizeof s, "%d", i);
6023 len = strlen(s);
6024
6025 w = xcb_generate_id(conn);
6026 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
6027 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
6028 wa[2] = screen->default_colormap;
6029
6030 if (bar_font_legacy) {
6031 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
6032 width = l_lbox.width + 4;
6033 height = bar_fs_extents->max_logical_extent.height + 4;
6034 } else {
6035 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
6036 &info);
6037 width = info.width + 4;
6038 height = bar_font->height + 4;
6039 }
6040
6041 xcb_create_window(conn, screen->root_depth, w, win->frame, 0, 0,
6042 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
6043 screen->root_visual, XCB_CW_BACK_PIXEL |
6044 XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP, wa);
6045
6046 xcb_map_window(conn, w);
6047
6048 sw->indicator = w;
6049 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
6050
6051 if (bar_font_legacy) {
6052 l_gcv.graphics_exposures = 0;
6053 l_draw = XCreateGC(display, w, 0, &l_gcv);
6054
6055 XSetForeground(display, l_draw,
6056 r->s->c[SWM_S_COLOR_BAR].pixel);
6057
6058 DRAWSTRING(display, w, bar_fs, l_draw, 2,
6059 (bar_fs_extents->max_logical_extent.height -
6060 l_lbox.height) / 2 - l_lbox.y, s, len);
6061
6062 XFreeGC(display, l_draw);
6063 } else {
6064
6065 draw = XftDrawCreate(display, w,
6066 DefaultVisual(display, r->s->idx),
6067 DefaultColormap(display, r->s->idx));
6068
6069 XftDrawStringUtf8(draw, &search_font_color, bar_font, 2,
6070 (HEIGHT(r->bar) + bar_font->height) / 2 -
6071 bar_font->descent, (FcChar8 *)s, len);
6072
6073 XftDrawDestroy(draw);
6074 }
6075
6076 DNPRINTF(SWM_D_MISC, "search_win: mapped win %#x\n", w);
6077
6078 fprintf(lfile, "%d\n", i);
6079 i++;
6080 }
6081
6082 fclose(lfile);
6083
6084 xcb_flush(conn);
6085 }
6086
6087 void
6088 search_resp_uniconify(const char *resp, size_t len)
6089 {
6090 char *name;
6091 struct ws_win *win;
6092 char *s;
6093
6094 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
6095
6096 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
6097 if (!ICONIC(win))
6098 continue;
6099 name = get_win_name(win->id);
6100 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
6101 free(name);
6102 continue;
6103 }
6104 free(name);
6105 if (strncmp(s, resp, len) == 0) {
6106 /* XXX this should be a callback to generalize */
6107 ewmh_apply_flags(win, win->ewmh_flags & ~EWMH_F_HIDDEN);
6108 ewmh_update_wm_state(win);
6109 stack(search_r);
6110 free(s);
6111 break;
6112 }
6113 free(s);
6114 }
6115 }
6116
6117 void
6118 search_resp_name_workspace(const char *resp, size_t len)
6119 {
6120 struct workspace *ws;
6121
6122 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
6123
6124 if (search_r->ws == NULL)
6125 return;
6126 ws = search_r->ws;
6127
6128 if (ws->name) {
6129 free(search_r->ws->name);
6130 search_r->ws->name = NULL;
6131 }
6132
6133 if (len > 1) {
6134 ws->name = strdup(resp);
6135 if (ws->name == NULL) {
6136 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
6137 "strdup: %s", strerror(errno));
6138 return;
6139 }
6140
6141 ewmh_update_desktop_names();
6142 ewmh_get_desktop_names();
6143 }
6144 }
6145
6146 void
6147 ewmh_update_desktop_names(void)
6148 {
6149 char *name_list = NULL, *p;
6150 int num_screens, i, j, len = 0, tot = 0;
6151
6152 num_screens = get_screen_count();
6153 for (i = 0; i < num_screens; ++i) {
6154 for (j = 0; j < workspace_limit; ++j) {
6155 if (screens[i].ws[j].name != NULL)
6156 len += strlen(screens[i].ws[j].name);
6157 ++len;
6158 }
6159
6160 if((name_list = calloc(len, sizeof(char))) == NULL)
6161 err(1, "update_desktop_names: calloc: failed to "
6162 "allocate memory.");
6163
6164 p = name_list;
6165 for (j = 0; j < workspace_limit; ++j) {
6166 if (screens[i].ws[j].name != NULL) {
6167 len = strlen(screens[i].ws[j].name);
6168 memcpy(p, screens[i].ws[j].name, len);
6169 } else {
6170 len = 0;
6171 }
6172
6173 p += len + 1;
6174 tot += len + 1;
6175 }
6176
6177 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
6178 screens[i].root, ewmh[_NET_DESKTOP_NAMES].atom,
6179 a_utf8_string, 8, tot, name_list);
6180
6181 free(name_list);
6182 name_list = NULL;
6183 }
6184
6185 free(name_list);
6186 }
6187
6188 void
6189 ewmh_get_desktop_names(void)
6190 {
6191 char *names = NULL;
6192 xcb_get_property_cookie_t c;
6193 xcb_get_property_reply_t *r;
6194 int num_screens, i, j, n, k;
6195
6196 num_screens = get_screen_count();
6197 for (i = 0; i < num_screens; ++i) {
6198 for (j = 0; j < workspace_limit; ++j) {
6199 free(screens[i].ws[j].name);
6200 screens[i].ws[j].name = NULL;
6201 }
6202
6203 c = xcb_get_property(conn, 0, screens[i].root,
6204 ewmh[_NET_DESKTOP_NAMES].atom,
6205 a_utf8_string, 0, UINT32_MAX);
6206 r = xcb_get_property_reply(conn, c, NULL);
6207 if (r == NULL)
6208 continue;
6209
6210 names = xcb_get_property_value(r);
6211 n = xcb_get_property_value_length(r);
6212
6213 for (j = 0, k = 0; j < n; ++j) {
6214 if (*(names + j) != '\0') {
6215 screens[i].ws[k].name = strdup(names + j);
6216 j += strlen(names + j);
6217 }
6218 ++k;
6219 }
6220 free(r);
6221 }
6222 }
6223
6224 void
6225 ewmh_update_client_list(void)
6226 {
6227 struct ws_win *win;
6228 int num_screens, i, j, k = 0, count = 0;
6229 xcb_window_t *wins;
6230
6231 num_screens = get_screen_count();
6232 for (i = 0; i < num_screens; ++i) {
6233 for (j = 0; j < workspace_limit; ++j)
6234 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
6235 ++count;
6236
6237 DNPRINTF(SWM_D_PROP, "ewmh_update_client_list: win count: %d\n",
6238 count);
6239
6240 if (count == 0)
6241 continue;
6242
6243 wins = calloc(count, sizeof(xcb_window_t));
6244 if (wins == NULL)
6245 err(1, "ewmh_update_client_list: calloc: failed to "
6246 "allocate memory.");
6247
6248 for (j = 0, k = 0; j < workspace_limit; ++j)
6249 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
6250 wins[k++] = win->id;
6251
6252 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
6253 screens[i].root, ewmh[_NET_CLIENT_LIST].atom,
6254 XCB_ATOM_WINDOW, 32, count, wins);
6255
6256 free(wins);
6257 }
6258 }
6259
6260 void
6261 ewmh_update_current_desktop(void)
6262 {
6263 int num_screens, i;
6264
6265 num_screens = get_screen_count();
6266 for (i = 0; i < num_screens; ++i) {
6267 if (screens[i].r_focus)
6268 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
6269 screens[i].root, ewmh[_NET_CURRENT_DESKTOP].atom,
6270 XCB_ATOM_CARDINAL, 32, 1,
6271 &screens[i].r_focus->ws->idx);
6272 }
6273 }
6274
6275 void
6276 ewmh_update_desktops(void)
6277 {
6278 int num_screens, i, j;
6279 uint32_t *vals;
6280
6281 vals = calloc(workspace_limit * 2, sizeof(uint32_t));
6282 if (vals == NULL)
6283 err(1, "ewmh_update_desktops: calloc: failed to allocate "
6284 "memory.");
6285
6286 num_screens = get_screen_count();
6287 for (i = 0; i < num_screens; i++) {
6288 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
6289 screens[i].root, ewmh[_NET_NUMBER_OF_DESKTOPS].atom,
6290 XCB_ATOM_CARDINAL, 32, 1, &workspace_limit);
6291
6292 for (j = 0; j < workspace_limit; ++j) {
6293 if (screens[i].ws[j].r != NULL) {
6294 vals[j * 2] = X(screens[i].ws[j].r);
6295 vals[j * 2 + 1] = Y(screens[i].ws[j].r);
6296 } else if (screens[i].ws[j].old_r != NULL) {
6297 vals[j * 2] = X(screens[i].ws[j].old_r);
6298 vals[j * 2 + 1] = Y(screens[i].ws[j].old_r);
6299 } else {
6300 vals[j * 2] = vals[j * 2 + 1] = 0;
6301 }
6302 }
6303
6304 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
6305 screens[i].root, ewmh[_NET_DESKTOP_VIEWPORT].atom,
6306 XCB_ATOM_CARDINAL, 32, workspace_limit * 2, vals);
6307 }
6308
6309 free(vals);
6310 }
6311
6312 void
6313 search_resp_search_workspace(const char *resp)
6314 {
6315 char *p, *q;
6316 int ws_idx;
6317 const char *errstr;
6318 union arg a;
6319
6320 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
6321
6322 q = strdup(resp);
6323 if (q == NULL) {
6324 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
6325 strerror(errno));
6326 return;
6327 }
6328 p = strchr(q, ':');
6329 if (p != NULL)
6330 *p = '\0';
6331 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
6332 if (errstr) {
6333 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
6334 errstr, q);
6335 free(q);
6336 return;
6337 }
6338 free(q);
6339 a.id = ws_idx - 1;
6340 switchws(NULL, search_r, &a);
6341 }
6342
6343 void
6344 search_resp_search_window(const char *resp)
6345 {
6346 char *s;
6347 int idx;
6348 const char *errstr;
6349 struct search_window *sw;
6350
6351 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
6352
6353 s = strdup(resp);
6354 if (s == NULL) {
6355 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
6356 strerror(errno));
6357 return;
6358 }
6359
6360 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
6361 if (errstr) {
6362 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
6363 errstr, s);
6364 free(s);
6365 return;
6366 }
6367 free(s);
6368
6369 TAILQ_FOREACH(sw, &search_wl, entry)
6370 if (idx == sw->idx) {
6371 focus_win(sw->win);
6372 break;
6373 }
6374 }
6375
6376 #define MAX_RESP_LEN 1024
6377
6378 void
6379 search_do_resp(void)
6380 {
6381 ssize_t rbytes;
6382 char *resp;
6383 size_t len;
6384
6385 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
6386
6387 search_resp = 0;
6388 searchpid = 0;
6389
6390 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
6391 warn("search: calloc");
6392 goto done;
6393 }
6394
6395 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
6396 if (rbytes <= 0) {
6397 warn("search: read error");
6398 goto done;
6399 }
6400 resp[rbytes] = '\0';
6401
6402 /* XXX:
6403 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6404 * newline, so work around that by sanitizing the resp now.
6405 */
6406 resp[strcspn(resp, "\n")] = '\0';
6407 len = strlen(resp);
6408
6409 switch (search_resp_action) {
6410 case SWM_SEARCH_UNICONIFY:
6411 search_resp_uniconify(resp, len);
6412 break;
6413 case SWM_SEARCH_NAME_WORKSPACE:
6414 search_resp_name_workspace(resp, len);
6415 break;
6416 case SWM_SEARCH_SEARCH_WORKSPACE:
6417 search_resp_search_workspace(resp);
6418 break;
6419 case SWM_SEARCH_SEARCH_WINDOW:
6420 search_resp_search_window(resp);
6421 break;
6422 }
6423
6424 done:
6425 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
6426 search_win_cleanup();
6427
6428 search_resp_action = SWM_SEARCH_NONE;
6429 close(select_resp_pipe[0]);
6430 free(resp);
6431
6432 xcb_flush(conn);
6433 }
6434
6435 void
6436 wkill(struct binding *bp, struct swm_region *r, union arg *args)
6437 {
6438 (void)bp;
6439
6440 DNPRINTF(SWM_D_MISC, "wkill: win %#x, id: %d\n", WINID(r->ws->focus),
6441 args->id);
6442
6443 if (r->ws->focus == NULL)
6444 return;
6445
6446 if (args->id == SWM_ARG_ID_KILLWINDOW)
6447 xcb_kill_client(conn, r->ws->focus->id);
6448 else
6449 if (r->ws->focus->can_delete)
6450 client_msg(r->ws->focus, a_delete, 0);
6451
6452 focus_flush();
6453 }
6454
6455 int
6456 clear_maximized(struct workspace *ws)
6457 {
6458 struct ws_win *w;
6459 int count = 0;
6460
6461 /* Clear any maximized win(s) on ws, from bottom up. */
6462 TAILQ_FOREACH_REVERSE(w, &ws->stack, ws_win_stack, stack_entry)
6463 if (MAXIMIZED(w)) {
6464 ewmh_apply_flags(w, w->ewmh_flags & ~EWMH_F_MAXIMIZED);
6465 ewmh_update_wm_state(w);
6466 ++count;
6467 }
6468
6469 return count;
6470 }
6471
6472 void
6473 maximize_toggle(struct binding *bp, struct swm_region *r, union arg *args)
6474 {
6475 struct ws_win *w = r->ws->focus;
6476
6477 /* suppress unused warning since var is needed */
6478 (void)bp;
6479 (void)args;
6480
6481 if (w == NULL)
6482 return;
6483
6484 DNPRINTF(SWM_D_MISC, "maximize_toggle: win %#x\n", w->id);
6485
6486 if (FULLSCREEN(w))
6487 return;
6488
6489 if (w->ws->cur_layout == &layouts[SWM_MAX_STACK])
6490 return;
6491
6492 ewmh_apply_flags(w, w->ewmh_flags ^ EWMH_F_MAXIMIZED);
6493 ewmh_update_wm_state(w);
6494
6495 stack(r);
6496
6497 if (w == w->ws->focus)
6498 focus_win(w);
6499
6500 center_pointer(r);
6501 focus_flush();
6502 DNPRINTF(SWM_D_MISC, "maximize_toggle: done\n");
6503 }
6504
6505 void
6506 floating_toggle(struct binding *bp, struct swm_region *r, union arg *args)
6507 {
6508 struct ws_win *w = r->ws->focus;
6509
6510 /* suppress unused warning since var is needed */
6511 (void)bp;
6512 (void)args;
6513
6514 if (w == NULL)
6515 return;
6516
6517 DNPRINTF(SWM_D_MISC, "floating_toggle: win %#x\n", w->id);
6518
6519 if (FULLSCREEN(w) || TRANS(w))
6520 return;
6521
6522 if (w->ws->cur_layout == &layouts[SWM_MAX_STACK])
6523 return;
6524
6525 ewmh_apply_flags(w, w->ewmh_flags ^ EWMH_F_ABOVE);
6526 ewmh_update_wm_state(w);
6527
6528 stack(r);
6529
6530 if (w == w->ws->focus)
6531 focus_win(w);
6532
6533 center_pointer(r);
6534 focus_flush();
6535 DNPRINTF(SWM_D_MISC, "floating_toggle: done\n");
6536 }
6537
6538 void
6539 region_containment(struct ws_win *win, struct swm_region *r, int opts)
6540 {
6541 struct swm_geometry g = r->g;
6542 int rt, lt, tp, bm, bw;
6543
6544 bw = (opts & SWM_CW_SOFTBOUNDARY) ? boundary_width : 0;
6545
6546 /*
6547 * Perpendicular distance of each side of the window to the respective
6548 * side of the region boundary. Positive values indicate the side of
6549 * the window has passed beyond the region boundary.
6550 */
6551 rt = opts & SWM_CW_RIGHT ? MAX_X(win) - MAX_X(r) : bw;
6552 lt = opts & SWM_CW_LEFT ? X(r) - X(win) : bw;
6553 bm = opts & SWM_CW_BOTTOM ? MAX_Y(win) - MAX_Y(r) : bw;
6554 tp = opts & SWM_CW_TOP ? Y(r) - Y(win) : bw;
6555
6556 DNPRINTF(SWM_D_MISC, "region_containment: win %#x, rt: %d, lt: %d, "
6557 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win->id, rt,
6558 lt, bm, tp, YESNO(opts & SWM_CW_SOFTBOUNDARY),
6559 YESNO(opts & SWM_CW_HARDBOUNDARY));
6560
6561 /*
6562 * Disable containment if any of the flagged sides went beyond the
6563 * containment boundary, or if containment is disabled.
6564 */
6565 if (!(opts & SWM_CW_HARDBOUNDARY || opts & SWM_CW_SOFTBOUNDARY) ||
6566 (bw != 0 && ((rt > bw) || (lt > bw) || (bm > bw) || (tp > bw)))) {
6567 /* Make sure window has at least 1 pixel in the region */
6568 g.x += 1 - WIDTH(win);
6569 g.y += 1 - HEIGHT(win);
6570 g.w += 2 * WIDTH(win) - 2;
6571 g.h += 2 * HEIGHT(win) - 2;
6572 }
6573
6574 constrain_window(win, &g, &opts);
6575 }
6576
6577 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6578 void
6579 constrain_window(struct ws_win *win, struct swm_geometry *b, int *opts)
6580 {
6581 DNPRINTF(SWM_D_MISC, "constrain_window: win %#x, (x,y) w x h: "
6582 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6583 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win->id, X(win),
6584 Y(win), WIDTH(win), HEIGHT(win), b->x, b->y, b->w, b->h,
6585 YESNO(*opts & SWM_CW_RIGHT), YESNO(*opts & SWM_CW_LEFT),
6586 YESNO(*opts & SWM_CW_BOTTOM), YESNO(*opts & SWM_CW_TOP),
6587 YESNO(*opts & SWM_CW_RESIZABLE));
6588
6589 if ((*opts & SWM_CW_RIGHT) && MAX_X(win) > b->x + b->w) {
6590 if (*opts & SWM_CW_RESIZABLE)
6591 WIDTH(win) = b->x + b->w - X(win);
6592 else
6593 X(win) = b->x + b->w - WIDTH(win);
6594 }
6595
6596 if ((*opts & SWM_CW_LEFT) && X(win) < b->x) {
6597 if (*opts & SWM_CW_RESIZABLE)
6598 WIDTH(win) -= b->x - X(win);
6599
6600 X(win) = b->x;
6601 }
6602
6603 if ((*opts & SWM_CW_BOTTOM) && MAX_Y(win) > b->y + b->h) {
6604 if (*opts & SWM_CW_RESIZABLE)
6605 HEIGHT(win) = b->y + b->h - Y(win);
6606 else
6607 Y(win) = b->y + b->h - HEIGHT(win);
6608 }
6609
6610 if ((*opts & SWM_CW_TOP) && Y(win) < b->y) {
6611 if (*opts & SWM_CW_RESIZABLE)
6612 HEIGHT(win) -= b->y - Y(win);
6613
6614 Y(win) = b->y;
6615 }
6616
6617 if (*opts & SWM_CW_RESIZABLE) {
6618 if (WIDTH(win) < 1)
6619 WIDTH(win) = 1;
6620 if (HEIGHT(win) < 1)
6621 HEIGHT(win) = 1;
6622 }
6623 }
6624
6625 void
6626 draw_frame(struct ws_win *win)
6627 {
6628 xcb_rectangle_t rect[4];
6629 uint32_t *pixel;
6630 int n = 0;
6631
6632 if (win->frame == XCB_WINDOW_NONE) {
6633 DNPRINTF(SWM_D_EVENT, "draw_frame: win %#x not "
6634 "reparented.\n", win->id);
6635 return;
6636 }
6637
6638 if (!win->bordered) {
6639 DNPRINTF(SWM_D_EVENT, "draw_frame: win %#x frame "
6640 "disabled\n", win->id);
6641 }
6642
6643 if (WS_FOCUSED(win->ws) && win->ws->focus == win)
6644 pixel = MAXIMIZED(win) ?
6645 &win->s->c[SWM_S_COLOR_FOCUS_MAXIMIZED].pixel :
6646 &win->s->c[SWM_S_COLOR_FOCUS].pixel;
6647 else
6648 pixel = MAXIMIZED(win) ?
6649 &win->s->c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].pixel :
6650 &win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
6651
6652 /* Top (with corners) */
6653 rect[n].x = 0;
6654 rect[n].y = 0;
6655 rect[n].width = WIDTH(win) + 2 * border_width;
6656 rect[n].height = border_width;
6657 /* Left (without corners) */
6658 rect[++n].x = 0;
6659 rect[n].y = border_width;
6660 rect[n].width = border_width;
6661 rect[n].height = HEIGHT(win);
6662 /* Right (without corners) */
6663 rect[++n].x = border_width + WIDTH(win);
6664 rect[n].y = border_width;
6665 rect[n].width = border_width;
6666 rect[n].height = HEIGHT(win);
6667 /* Bottom (with corners)*/
6668 rect[++n].x = 0;
6669 rect[n].y = border_width + HEIGHT(win);
6670 rect[n].width = WIDTH(win) + 2 * border_width;
6671 rect[n].height = border_width;
6672
6673 xcb_change_gc(conn, win->s->bar_gc, XCB_GC_FOREGROUND, pixel);
6674 xcb_poly_fill_rectangle(conn, win->frame, win->s->bar_gc, 4, rect);
6675 }
6676
6677 void
6678 update_window(struct ws_win *win)
6679 {
6680 uint16_t mask;
6681 uint32_t wc[5];
6682
6683 if (win->frame == XCB_WINDOW_NONE) {
6684 DNPRINTF(SWM_D_EVENT, "update_window: skip win %#x; "
6685 "not reparented.\n", win->id);
6686 return;
6687 }
6688
6689 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
6690 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
6691 XCB_CONFIG_WINDOW_BORDER_WIDTH;
6692
6693 /* Reconfigure frame. */
6694 if (win->bordered) {
6695 wc[0] = X(win) - border_width;
6696 wc[1] = Y(win) - border_width;
6697 wc[2] = WIDTH(win) + 2 * border_width;
6698 wc[3] = HEIGHT(win) + 2 * border_width;
6699 } else {
6700 wc[0] = X(win);
6701 wc[1] = Y(win);
6702 wc[2] = WIDTH(win);
6703 wc[3] = HEIGHT(win);
6704 }
6705
6706 wc[4] = 0;
6707
6708 DNPRINTF(SWM_D_EVENT, "update_window: win %#x frame %#x, (x,y) w x h: "
6709 "(%d,%d) %d x %d, bordered: %s\n", win->id, win->frame, wc[0],
6710 wc[1], wc[2], wc[3], YESNO(win->bordered));
6711
6712 xcb_configure_window(conn, win->frame, mask, wc);
6713
6714 /* Reconfigure client window. */
6715 wc[0] = wc[1] = win->bordered ? border_width : 0;
6716 wc[2] = WIDTH(win);
6717 wc[3] = HEIGHT(win);
6718
6719 DNPRINTF(SWM_D_EVENT, "update_window: win %#x, (x,y) w x h: "
6720 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
6721 wc[3], YESNO(win->bordered));
6722 xcb_configure_window(conn, win->id, mask, wc);
6723
6724 /* ICCCM 4.2.3 Synthetic ConfigureNotify when moved and not resized. */
6725 if ((X(win) != win->g_prev.x || Y(win) != win->g_prev.y) &&
6726 (win->java || (WIDTH(win) == win->g_prev.w &&
6727 HEIGHT(win) == win->g_prev.h))) {
6728 /* Java has special needs when moved together with a resize. */
6729 config_win(win, NULL);
6730 }
6731
6732 win->g_prev = win->g;
6733 }
6734
6735 struct event {
6736 SIMPLEQ_ENTRY(event) entry;
6737 xcb_generic_event_t *ev;
6738 };
6739 SIMPLEQ_HEAD(event_queue, event) events = SIMPLEQ_HEAD_INITIALIZER(events);
6740
6741 xcb_generic_event_t *
6742 get_next_event(bool dowait)
6743 {
6744 struct event *ep;
6745 xcb_generic_event_t *evt;
6746
6747 /* Try queue first. */
6748 if ((ep = SIMPLEQ_FIRST(&events))) {
6749 evt = ep->ev;
6750 SIMPLEQ_REMOVE_HEAD(&events, entry);
6751 free(ep);
6752 } else if (dowait)
6753 evt = xcb_wait_for_event(conn);
6754 else
6755 evt = xcb_poll_for_event(conn);
6756
6757 return evt;
6758 }
6759
6760 void
6761 put_back_event(xcb_generic_event_t *evt)
6762 {
6763 struct event *ep;
6764 if ((ep = malloc(sizeof (struct event))) == NULL)
6765 err(1, "put_back_event: failed to allocate memory.");
6766 ep->ev = evt;
6767 SIMPLEQ_INSERT_HEAD(&events, ep, entry);
6768 }
6769
6770 /* Peeks at next event to detect auto-repeat. */
6771 bool
6772 keyrepeating(xcb_key_release_event_t *kre)
6773 {
6774 xcb_generic_event_t *evt;
6775
6776 /* Ensure repeating keypress is finished processing. */
6777 xcb_aux_sync(conn);
6778
6779 if ((evt = get_next_event(false))) {
6780 put_back_event(evt);
6781
6782 if (XCB_EVENT_RESPONSE_TYPE(evt) == XCB_KEY_PRESS &&
6783 kre->sequence == evt->sequence &&
6784 kre->detail == ((xcb_key_press_event_t *)evt)->detail)
6785 return true;
6786 }
6787
6788 return false;
6789 }
6790
6791 bool
6792 keybindreleased(struct binding *bp, xcb_key_release_event_t *kre)
6793 {
6794 if (bp->type == KEYBIND && !keyrepeating(kre) &&
6795 bp->value == xcb_key_press_lookup_keysym(syms, kre, 0))
6796 return true;
6797
6798 return false;
6799 }
6800
6801 #define SWM_RESIZE_STEPS (50)
6802
6803 void
6804 resize_win(struct ws_win *win, struct binding *bp, int opt)
6805 {
6806 xcb_timestamp_t timestamp = 0;
6807 struct swm_region *r = NULL;
6808 struct swm_geometry g;
6809 int top = 0, left = 0;
6810 int dx, dy;
6811 xcb_cursor_t cursor;
6812 xcb_query_pointer_reply_t *xpr = NULL;
6813 xcb_generic_event_t *evt;
6814 xcb_motion_notify_event_t *mne;
6815 bool resizing, step = false;
6816
6817 if (win == NULL)
6818 return;
6819 r = win->ws->r;
6820
6821 if (FULLSCREEN(win))
6822 return;
6823
6824 /* In max_stack mode, should only resize transients. */
6825 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !TRANS(win))
6826 return;
6827
6828 DNPRINTF(SWM_D_EVENT, "resize: win %#x, floating: %s, "
6829 "transient: %#x\n", win->id, YESNO(ABOVE(win)),
6830 win->transient);
6831
6832 if (MAXIMIZED(win))
6833 store_float_geom(win);
6834 else if (!(TRANS(win) || ABOVE(win)))
6835 return;
6836
6837 ewmh_apply_flags(win, (win->ewmh_flags | SWM_F_MANUAL | EWMH_F_ABOVE) &
6838 ~EWMH_F_MAXIMIZED);
6839 ewmh_update_wm_state(win);
6840
6841 stack(r);
6842
6843 focus_flush();
6844
6845 /* It's possible for win to have been freed during focus_flush(). */
6846 if (validate_win(win)) {
6847 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
6848 goto out;
6849 }
6850
6851 switch (opt) {
6852 case SWM_ARG_ID_WIDTHSHRINK:
6853 WIDTH(win) -= SWM_RESIZE_STEPS;
6854 step = true;
6855 break;
6856 case SWM_ARG_ID_WIDTHGROW:
6857 WIDTH(win) += SWM_RESIZE_STEPS;
6858 step = true;
6859 break;
6860 case SWM_ARG_ID_HEIGHTSHRINK:
6861 HEIGHT(win) -= SWM_RESIZE_STEPS;
6862 step = true;
6863 break;
6864 case SWM_ARG_ID_HEIGHTGROW:
6865 HEIGHT(win) += SWM_RESIZE_STEPS;
6866 step = true;
6867 break;
6868 default:
6869 break;
6870 }
6871 if (step) {
6872 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6873 SWM_CW_HARDBOUNDARY);
6874 update_window(win);
6875 store_float_geom(win);
6876 return;
6877 }
6878
6879 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6880 SWM_CW_SOFTBOUNDARY);
6881 update_window(win);
6882
6883 /* get cursor offset from window root */
6884 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
6885 NULL);
6886 if (xpr == NULL)
6887 return;
6888
6889 g = win->g;
6890
6891 if (xpr->win_x < WIDTH(win) / 2)
6892 left = 1;
6893
6894 if (xpr->win_y < HEIGHT(win) / 2)
6895 top = 1;
6896
6897 if (opt == SWM_ARG_ID_CENTER)
6898 cursor = cursors[XC_SIZING].cid;
6899 else if (top)
6900 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
6901 XC_TOP_RIGHT_CORNER].cid;
6902 else
6903 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
6904 XC_BOTTOM_RIGHT_CORNER].cid;
6905
6906 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
6907 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
6908 XCB_CURRENT_TIME);
6909
6910 /* Release keyboard freeze if called via keybind. */
6911 if (bp->type == KEYBIND)
6912 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
6913 XCB_CURRENT_TIME);
6914
6915 xcb_flush(conn);
6916 resizing = true;
6917 while (resizing && (evt = get_next_event(true))) {
6918 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
6919 case XCB_BUTTON_RELEASE:
6920 if (bp->type == BTNBIND && bp->value ==
6921 ((xcb_button_release_event_t *)evt)->detail)
6922 resizing = false;
6923 break;
6924 case XCB_KEY_RELEASE:
6925 if (keybindreleased(bp, (xcb_key_release_event_t *)evt))
6926 resizing = false;
6927 break;
6928 case XCB_MOTION_NOTIFY:
6929 mne = (xcb_motion_notify_event_t *)evt;
6930 DNPRINTF(SWM_D_EVENT, "resize: MOTION_NOTIFY: "
6931 "root: %#x\n", mne->root);
6932
6933 /* cursor offset/delta from start of the operation */
6934 dx = mne->root_x - xpr->root_x;
6935 dy = mne->root_y - xpr->root_y;
6936
6937 /* vertical */
6938 if (top)
6939 dy = -dy;
6940
6941 if (opt == SWM_ARG_ID_CENTER) {
6942 if (g.h / 2 + dy < 1)
6943 dy = 1 - g.h / 2;
6944
6945 Y(win) = g.y - dy;
6946 HEIGHT(win) = g.h + 2 * dy;
6947 } else {
6948 if (g.h + dy < 1)
6949 dy = 1 - g.h;
6950
6951 if (top)
6952 Y(win) = g.y - dy;
6953
6954 HEIGHT(win) = g.h + dy;
6955 }
6956
6957 /* horizontal */
6958 if (left)
6959 dx = -dx;
6960
6961 if (opt == SWM_ARG_ID_CENTER) {
6962 if (g.w / 2 + dx < 1)
6963 dx = 1 - g.w / 2;
6964
6965 X(win) = g.x - dx;
6966 WIDTH(win) = g.w + 2 * dx;
6967 } else {
6968 if (g.w + dx < 1)
6969 dx = 1 - g.w;
6970
6971 if (left)
6972 X(win) = g.x - dx;
6973
6974 WIDTH(win) = g.w + dx;
6975 }
6976
6977 /* not free, don't sync more than 120 times / second */
6978 if ((mne->time - timestamp) > (1000 / 120) ) {
6979 timestamp = mne->time;
6980 regionize(win, mne->root_x, mne->root_y);
6981 region_containment(win, r, SWM_CW_ALLSIDES |
6982 SWM_CW_RESIZABLE | SWM_CW_HARDBOUNDARY |
6983 SWM_CW_SOFTBOUNDARY);
6984 update_window(win);
6985 xcb_flush(conn);
6986 }
6987 break;
6988 case XCB_BUTTON_PRESS:
6989 /* Ignore. */
6990 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_PRESS ignored\n");
6991 xcb_allow_events(conn, XCB_ALLOW_ASYNC_POINTER,
6992 ((xcb_button_press_event_t *)evt)->time);
6993 xcb_flush(conn);
6994 break;
6995 case XCB_KEY_PRESS:
6996 /* Ignore. */
6997 DNPRINTF(SWM_D_EVENT, "resize: KEY_PRESS ignored\n");
6998 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
6999 ((xcb_key_press_event_t *)evt)->time);
7000 xcb_flush(conn);
7001 break;
7002 default:
7003 event_handle(evt);
7004
7005 /* It's possible for win to have been freed above. */
7006 if (validate_win(win)) {
7007 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
7008 goto out;
7009 }
7010 break;
7011 }
7012 free(evt);
7013 }
7014 if (timestamp) {
7015 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
7016 SWM_CW_HARDBOUNDARY | SWM_CW_SOFTBOUNDARY);
7017 update_window(win);
7018 xcb_flush(conn);
7019 }
7020 store_float_geom(win);
7021 out:
7022 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
7023 free(xpr);
7024 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
7025 }
7026
7027 void
7028 resize(struct binding *bp, struct swm_region *r, union arg *args)
7029 {
7030 struct ws_win *win = NULL;
7031 xcb_query_pointer_reply_t *qpr = NULL;
7032
7033 if (r == NULL)
7034 return;
7035
7036 if (args->id != SWM_ARG_ID_DONTCENTER &&
7037 args->id != SWM_ARG_ID_CENTER) {
7038 /* {height,width}_{grow,shrink} use the focus window. */
7039 if (r->ws)
7040 win = r->ws->focus;
7041 } else {
7042 /* resize uses window under pointer. */
7043 qpr = xcb_query_pointer_reply(conn,
7044 xcb_query_pointer(conn, r->s->root), NULL);
7045 if (qpr)
7046 win = find_window(qpr->child);
7047 }
7048
7049 if (win == NULL)
7050 return;
7051
7052 resize_win(win, bp, args->id);
7053
7054 if (args->id && bp->type == KEYBIND)
7055 center_pointer(r);
7056
7057 focus_flush();
7058 }
7059
7060 /* Try to set window region based on supplied coordinates or window center. */
7061 void
7062 regionize(struct ws_win *win, int x, int y)
7063 {
7064 struct swm_region *r = NULL;
7065
7066 r = region_under(win->s, x, y);
7067 if (r == NULL)
7068 r = region_under(win->s, X(win) + WIDTH(win) / 2,
7069 Y(win) + HEIGHT(win) / 2);
7070
7071 if (r && r != win->ws->r) {
7072 clear_maximized(r->ws);
7073
7074 win_to_ws(win, r->ws->idx, false);
7075
7076 /* Stack old and new region. */
7077 stack(r);
7078 stack(win->ws->r);
7079
7080 /* Set focus on new ws. */
7081 unfocus_win(r->ws->focus);
7082 r->ws->focus = win;
7083
7084 set_region(r);
7085 raise_window(win);
7086 }
7087 }
7088
7089 #define SWM_MOVE_STEPS (50)
7090
7091 void
7092 move_win(struct ws_win *win, struct binding *bp, int opt)
7093 {
7094 struct swm_region *r;
7095 xcb_timestamp_t timestamp = 0;
7096 xcb_query_pointer_reply_t *qpr = NULL;
7097 xcb_generic_event_t *evt;
7098 xcb_motion_notify_event_t *mne;
7099 bool moving, restack = false, step = false;
7100
7101 if (win == NULL)
7102 return;
7103
7104 if ((r = win->ws->r) == NULL)
7105 return;
7106
7107 if (FULLSCREEN(win))
7108 return;
7109
7110 DNPRINTF(SWM_D_EVENT, "move: win %#x, floating: %s, transient: "
7111 "%#x\n", win->id, YESNO(ABOVE(win)), win->transient);
7112
7113 /* in max_stack mode should only move transients */
7114 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !TRANS(win))
7115 return;
7116
7117 if (!(ABOVE(win) || TRANS(win)) || MAXIMIZED(win)) {
7118 store_float_geom(win);
7119 restack = true;
7120 }
7121
7122 ewmh_apply_flags(win, (win->ewmh_flags | SWM_F_MANUAL | EWMH_F_ABOVE) &
7123 ~EWMH_F_MAXIMIZED);
7124 ewmh_update_wm_state(win);
7125
7126 if (restack)
7127 stack(r);
7128
7129 focus_flush();
7130
7131 /* It's possible for win to have been freed during focus_flush(). */
7132 if (validate_win(win)) {
7133 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
7134 goto out;
7135 }
7136
7137 switch (opt) {
7138 case SWM_ARG_ID_MOVELEFT:
7139 X(win) -= (SWM_MOVE_STEPS - border_width);
7140 step = true;
7141 break;
7142 case SWM_ARG_ID_MOVERIGHT:
7143 X(win) += (SWM_MOVE_STEPS - border_width);
7144 step = true;
7145 break;
7146 case SWM_ARG_ID_MOVEUP:
7147 Y(win) -= (SWM_MOVE_STEPS - border_width);
7148 step = true;
7149 break;
7150 case SWM_ARG_ID_MOVEDOWN:
7151 Y(win) += (SWM_MOVE_STEPS - border_width);
7152 step = true;
7153 break;
7154 default:
7155 break;
7156 }
7157 if (step) {
7158 regionize(win, -1, -1);
7159 region_containment(win, win->ws->r, SWM_CW_ALLSIDES);
7160 update_window(win);
7161 store_float_geom(win);
7162 return;
7163 }
7164
7165 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
7166 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
7167 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
7168
7169 /* get cursor offset from window root */
7170 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
7171 NULL);
7172 if (qpr == NULL) {
7173 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
7174 return;
7175 }
7176
7177 /* Release keyboard freeze if called via keybind. */
7178 if (bp->type == KEYBIND)
7179 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
7180 XCB_CURRENT_TIME);
7181
7182 regionize(win, qpr->root_x, qpr->root_y);
7183 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
7184 SWM_CW_SOFTBOUNDARY);
7185 update_window(win);
7186 xcb_flush(conn);
7187 moving = true;
7188 while (moving && (evt = get_next_event(true))) {
7189 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
7190 case XCB_BUTTON_RELEASE:
7191 if (bp->type == BTNBIND && bp->value ==
7192 ((xcb_button_release_event_t *)evt)->detail)
7193 moving = false;
7194
7195 xcb_allow_events(conn, XCB_ALLOW_ASYNC_POINTER,
7196 ((xcb_button_release_event_t *)evt)->time);
7197 xcb_flush(conn);
7198 break;
7199 case XCB_KEY_RELEASE:
7200 if (keybindreleased(bp, (xcb_key_release_event_t *)evt))
7201 moving = false;
7202
7203 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
7204 ((xcb_key_release_event_t *)evt)->time);
7205 xcb_flush(conn);
7206 break;
7207 case XCB_MOTION_NOTIFY:
7208 mne = (xcb_motion_notify_event_t *)evt;
7209 DNPRINTF(SWM_D_EVENT, "move: MOTION_NOTIFY: "
7210 "root: %#x\n", mne->root);
7211 X(win) = mne->root_x - qpr->win_x - border_width;
7212 Y(win) = mne->root_y - qpr->win_y - border_width;
7213
7214 /* not free, don't sync more than 120 times / second */
7215 if ((mne->time - timestamp) > (1000 / 120) ) {
7216 timestamp = mne->time;
7217 regionize(win, mne->root_x, mne->root_y);
7218 region_containment(win, win->ws->r,
7219 SWM_CW_ALLSIDES | SWM_CW_SOFTBOUNDARY);
7220 update_window(win);
7221 xcb_flush(conn);
7222 }
7223 break;
7224 case XCB_BUTTON_PRESS:
7225 /* Thaw and ignore. */
7226 xcb_allow_events(conn, XCB_ALLOW_ASYNC_POINTER,
7227 ((xcb_button_press_event_t *)evt)->time);
7228 xcb_flush(conn);
7229 break;
7230 case XCB_KEY_PRESS:
7231 /* Ignore. */
7232 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
7233 ((xcb_key_press_event_t *)evt)->time);
7234 xcb_flush(conn);
7235 break;
7236 default:
7237 event_handle(evt);
7238
7239 /* It's possible for win to have been freed. */
7240 if (validate_win(win)) {
7241 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
7242 goto out;
7243 }
7244 break;
7245 }
7246 free(evt);
7247 }
7248 if (timestamp) {
7249 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
7250 SWM_CW_SOFTBOUNDARY);
7251 update_window(win);
7252 xcb_flush(conn);
7253 }
7254 store_float_geom(win);
7255
7256 /* New region set to fullscreen layout. */
7257 if (win->ws->r && win->ws->cur_layout == &layouts[SWM_MAX_STACK]) {
7258 stack(win->ws->r);
7259 focus_flush();
7260 }
7261
7262 out:
7263 free(qpr);
7264 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
7265 DNPRINTF(SWM_D_EVENT, "move: done.\n");
7266 }
7267
7268 void
7269 move(struct binding *bp, struct swm_region *r, union arg *args)
7270 {
7271 struct ws_win *win = NULL;
7272 xcb_query_pointer_reply_t *qpr = NULL;
7273
7274 if (r == NULL)
7275 return;
7276
7277 if (args->id) {
7278 /* move_* uses focus window. */
7279 if (r->ws)
7280 win = r->ws->focus;
7281
7282 /* Disallow move_ on tiled. */
7283 if (win && !(TRANS(win) || ABOVE(win)))
7284 return;
7285 } else {
7286 /* move uses window under pointer. */
7287 qpr = xcb_query_pointer_reply(conn,
7288 xcb_query_pointer(conn, r->s->root), NULL);
7289 if (qpr)
7290 win = find_window(qpr->child);
7291 }
7292
7293 if (win == NULL)
7294 return;
7295
7296 move_win(win, bp, args->id);
7297
7298 if (args->id && bp->type == KEYBIND)
7299 center_pointer(r);
7300
7301 focus_flush();
7302 }
7303
7304 /* action definitions */
7305 struct action {
7306 char name[SWM_FUNCNAME_LEN];
7307 void (*func)(struct binding *, struct swm_region *,
7308 union arg *);
7309 uint32_t flags;
7310 union arg args;
7311 } actions[FN_INVALID + 2] = {
7312 /* name function argument */
7313 { "bar_toggle", bar_toggle, 0, {.id = SWM_ARG_ID_BAR_TOGGLE} },
7314 { "bar_toggle_ws", bar_toggle, 0, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
7315 { "button2", pressbutton, 0, {.id = 2} },
7316 { "cycle_layout", cycle_layout, 0, {0} },
7317 { "flip_layout", stack_config, 0, {.id = SWM_ARG_ID_FLIPLAYOUT} },
7318 { "float_toggle", floating_toggle,0, {0} },
7319 { "focus", focus_pointer, 0, {0} },
7320 { "focus_main", focus, 0, {.id = SWM_ARG_ID_FOCUSMAIN} },
7321 { "focus_next", focus, 0, {.id = SWM_ARG_ID_FOCUSNEXT} },
7322 { "focus_prev", focus, 0, {.id = SWM_ARG_ID_FOCUSPREV} },
7323 { "focus_urgent", focus, 0, {.id = SWM_ARG_ID_FOCUSURGENT} },
7324 { "maximize_toggle", maximize_toggle,0, {0} },
7325 { "height_grow", resize, 0, {.id = SWM_ARG_ID_HEIGHTGROW} },
7326 { "height_shrink", resize, 0, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
7327 { "iconify", iconify, 0, {0} },
7328 { "master_shrink", stack_config, 0, {.id = SWM_ARG_ID_MASTERSHRINK} },
7329 { "master_grow", stack_config, 0, {.id = SWM_ARG_ID_MASTERGROW} },
7330 { "master_add", stack_config, 0, {.id = SWM_ARG_ID_MASTERADD} },
7331 { "master_del", stack_config, 0, {.id = SWM_ARG_ID_MASTERDEL} },
7332 { "move", move, FN_F_NOREPLAY, {0} },
7333 { "move_down", move, 0, {.id = SWM_ARG_ID_MOVEDOWN} },
7334 { "move_left", move, 0, {.id = SWM_ARG_ID_MOVELEFT} },
7335 { "move_right", move, 0, {.id = SWM_ARG_ID_MOVERIGHT} },
7336 { "move_up", move, 0, {.id = SWM_ARG_ID_MOVEUP} },
7337 { "mvrg_1", send_to_rg, 0, {.id = 0} },
7338 { "mvrg_2", send_to_rg, 0, {.id = 1} },
7339 { "mvrg_3", send_to_rg, 0, {.id = 2} },
7340 { "mvrg_4", send_to_rg, 0, {.id = 3} },
7341 { "mvrg_5", send_to_rg, 0, {.id = 4} },
7342 { "mvrg_6", send_to_rg, 0, {.id = 5} },
7343 { "mvrg_7", send_to_rg, 0, {.id = 6} },
7344 { "mvrg_8", send_to_rg, 0, {.id = 7} },
7345 { "mvrg_9", send_to_rg, 0, {.id = 8} },
7346 { "mvws_1", send_to_ws, 0, {.id = 0} },
7347 { "mvws_2", send_to_ws, 0, {.id = 1} },
7348 { "mvws_3", send_to_ws, 0, {.id = 2} },
7349 { "mvws_4", send_to_ws, 0, {.id = 3} },
7350 { "mvws_5", send_to_ws, 0, {.id = 4} },
7351 { "mvws_6", send_to_ws, 0, {.id = 5} },
7352 { "mvws_7", send_to_ws, 0, {.id = 6} },
7353 { "mvws_8", send_to_ws, 0, {.id = 7} },
7354 { "mvws_9", send_to_ws, 0, {.id = 8} },
7355 { "mvws_10", send_to_ws, 0, {.id = 9} },
7356 { "mvws_11", send_to_ws, 0, {.id = 10} },
7357 { "mvws_12", send_to_ws, 0, {.id = 11} },
7358 { "mvws_13", send_to_ws, 0, {.id = 12} },
7359 { "mvws_14", send_to_ws, 0, {.id = 13} },
7360 { "mvws_15", send_to_ws, 0, {.id = 14} },
7361 { "mvws_16", send_to_ws, 0, {.id = 15} },
7362 { "mvws_17", send_to_ws, 0, {.id = 16} },
7363 { "mvws_18", send_to_ws, 0, {.id = 17} },
7364 { "mvws_19", send_to_ws, 0, {.id = 18} },
7365 { "mvws_20", send_to_ws, 0, {.id = 19} },
7366 { "mvws_21", send_to_ws, 0, {.id = 20} },
7367 { "mvws_22", send_to_ws, 0, {.id = 21} },
7368 { "name_workspace", name_workspace, 0, {0} },
7369 { "quit", quit, 0, {0} },
7370 { "raise_toggle", raise_toggle, 0, {0} },
7371 { "resize", resize, FN_F_NOREPLAY, {.id = SWM_ARG_ID_DONTCENTER} },
7372 { "resize_centered", resize, FN_F_NOREPLAY, {.id = SWM_ARG_ID_CENTER} },
7373 { "restart", restart, 0, {0} },
7374 { "rg_1", focusrg, 0, {.id = 0} },
7375 { "rg_2", focusrg, 0, {.id = 1} },
7376 { "rg_3", focusrg, 0, {.id = 2} },
7377 { "rg_4", focusrg, 0, {.id = 3} },
7378 { "rg_5", focusrg, 0, {.id = 4} },
7379 { "rg_6", focusrg, 0, {.id = 5} },
7380 { "rg_7", focusrg, 0, {.id = 6} },
7381 { "rg_8", focusrg, 0, {.id = 7} },
7382 { "rg_9", focusrg, 0, {.id = 8} },
7383 { "rg_move_next", cyclerg, 0, {.id = SWM_ARG_ID_CYCLERG_MOVE_UP} },
7384 { "rg_move_prev", cyclerg, 0, {.id = SWM_ARG_ID_CYCLERG_MOVE_DOWN} },
7385 { "rg_next", cyclerg, 0, {.id = SWM_ARG_ID_CYCLERG_UP} },
7386 { "rg_prev", cyclerg, 0, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
7387 { "screen_next", cyclerg, 0, {.id = SWM_ARG_ID_CYCLERG_UP} },
7388 { "screen_prev", cyclerg, 0, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
7389 { "search_win", search_win, 0, {0} },
7390 { "search_workspace", search_workspace, 0, {0} },
7391 { "spawn_custom", NULL, 0, {0} },
7392 { "stack_balance", stack_config, 0, {.id = SWM_ARG_ID_STACKBALANCE} },
7393 { "stack_inc", stack_config, 0, {.id = SWM_ARG_ID_STACKINC} },
7394 { "stack_dec", stack_config, 0, {.id = SWM_ARG_ID_STACKDEC} },
7395 { "stack_reset", stack_config, 0, {.id = SWM_ARG_ID_STACKRESET} },
7396 { "swap_main", swapwin, 0, {.id = SWM_ARG_ID_SWAPMAIN} },
7397 { "swap_next", swapwin, 0, {.id = SWM_ARG_ID_SWAPNEXT} },
7398 { "swap_prev", swapwin, 0, {.id = SWM_ARG_ID_SWAPPREV} },
7399 { "uniconify", uniconify, 0, {0} },
7400 { "version", version, 0, {0} },
7401 { "width_grow", resize, 0, {.id = SWM_ARG_ID_WIDTHGROW} },
7402 { "width_shrink", resize, 0, {.id = SWM_ARG_ID_WIDTHSHRINK} },
7403 { "wind_del", wkill, 0, {.id = SWM_ARG_ID_DELETEWINDOW} },
7404 { "wind_kill", wkill, 0, {.id = SWM_ARG_ID_KILLWINDOW} },
7405 { "ws_1", switchws, 0, {.id = 0} },
7406 { "ws_2", switchws, 0, {.id = 1} },
7407 { "ws_3", switchws, 0, {.id = 2} },
7408 { "ws_4", switchws, 0, {.id = 3} },
7409 { "ws_5", switchws, 0, {.id = 4} },
7410 { "ws_6", switchws, 0, {.id = 5} },
7411 { "ws_7", switchws, 0, {.id = 6} },
7412 { "ws_8", switchws, 0, {.id = 7} },
7413 { "ws_9", switchws, 0, {.id = 8} },
7414 { "ws_10", switchws, 0, {.id = 9} },
7415 { "ws_11", switchws, 0, {.id = 10} },
7416 { "ws_12", switchws, 0, {.id = 11} },
7417 { "ws_13", switchws, 0, {.id = 12} },
7418 { "ws_14", switchws, 0, {.id = 13} },
7419 { "ws_15", switchws, 0, {.id = 14} },
7420 { "ws_16", switchws, 0, {.id = 15} },
7421 { "ws_17", switchws, 0, {.id = 16} },
7422 { "ws_18", switchws, 0, {.id = 17} },
7423 { "ws_19", switchws, 0, {.id = 18} },
7424 { "ws_20", switchws, 0, {.id = 19} },
7425 { "ws_21", switchws, 0, {.id = 20} },
7426 { "ws_22", switchws, 0, {.id = 21} },
7427 { "ws_next", cyclews, 0, {.id = SWM_ARG_ID_CYCLEWS_UP} },
7428 { "ws_next_all", cyclews, 0, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
7429 { "ws_next_move", cyclews, 0, {.id = SWM_ARG_ID_CYCLEWS_MOVE_UP} },
7430 { "ws_prev", cyclews, 0, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
7431 { "ws_prev_all", cyclews, 0, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
7432 { "ws_prev_move", cyclews, 0, {.id = SWM_ARG_ID_CYCLEWS_MOVE_DOWN} },
7433 { "ws_prior", priorws, 0, {0} },
7434 /* SWM_DEBUG actions MUST be here: */
7435 { "debug_toggle", debug_toggle, 0, {0} },
7436 { "dumpwins", dumpwins, 0, {0} },
7437 /* ALWAYS last: */
7438 { "invalid action", NULL, 0, {0} },
7439 };
7440
7441 void
7442 update_modkey(uint16_t mod)
7443 {
7444 struct binding *bp;
7445
7446 /* Replace all instances of the old mod key. */
7447 RB_FOREACH(bp, binding_tree, &bindings)
7448 if (bp->mod & mod_key)
7449 bp->mod = (bp->mod & ~mod_key) | mod;
7450 mod_key = mod;
7451 }
7452
7453 int
7454 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
7455 char ***ret_args)
7456 {
7457 struct spawn_prog *prog = NULL;
7458 int i, c;
7459 char *ap, **real_args;
7460
7461 /* suppress unused warning since var is needed */
7462 (void)args;
7463
7464 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
7465
7466 /* find program */
7467 TAILQ_FOREACH(prog, &spawns, entry) {
7468 if (strcasecmp(spawn_name, prog->name) == 0)
7469 break;
7470 }
7471 if (prog == NULL) {
7472 warnx("spawn_custom: program %s not found", spawn_name);
7473 return (-1);
7474 }
7475
7476 /* make room for expanded args */
7477 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
7478 err(1, "spawn_custom: calloc real_args");
7479
7480 /* expand spawn_args into real_args */
7481 for (i = c = 0; i < prog->argc; i++) {
7482 ap = prog->argv[i];
7483 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
7484 if (strcasecmp(ap, "$bar_border") == 0) {
7485 if ((real_args[c] =
7486 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
7487 == NULL)
7488 err(1, "spawn_custom border color");
7489 } else if (strcasecmp(ap, "$bar_color") == 0) {
7490 if ((real_args[c] =
7491 strdup(r->s->c[SWM_S_COLOR_BAR].name))
7492 == NULL)
7493 err(1, "spawn_custom bar color");
7494 } else if (strcasecmp(ap, "$bar_font") == 0) {
7495 if ((real_args[c] = strdup(bar_fonts))
7496 == NULL)
7497 err(1, "spawn_custom bar fonts");
7498 } else if (strcasecmp(ap, "$bar_font_color") == 0) {
7499 if ((real_args[c] =
7500 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
7501 == NULL)
7502 err(1, "spawn_custom color font");
7503 } else if (strcasecmp(ap, "$color_focus") == 0) {
7504 if ((real_args[c] =
7505 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
7506 == NULL)
7507 err(1, "spawn_custom color focus");
7508 } else if (strcasecmp(ap, "$color_focus_maximized") == 0) {
7509 if ((real_args[c] =
7510 strdup(r->s->c[SWM_S_COLOR_FOCUS_MAXIMIZED].name))
7511 == NULL)
7512 err(1, "spawn_custom color focus maximized");
7513 } else if (strcasecmp(ap, "$color_unfocus") == 0) {
7514 if ((real_args[c] =
7515 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
7516 == NULL)
7517 err(1, "spawn_custom color unfocus");
7518 } else if (strcasecmp(ap, "$color_unfocus_maximized") == 0) {
7519 if ((real_args[c] =
7520 strdup(r->s->c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].name))
7521 == NULL)
7522 err(1, "spawn_custom color unfocus maximized");
7523 } else if (strcasecmp(ap, "$region_index") == 0) {
7524 if (asprintf(&real_args[c], "%d",
7525 get_region_index(r) + 1) < 1)
7526 err(1, "spawn_custom region index");
7527 } else if (strcasecmp(ap, "$workspace_index") == 0) {
7528 if (asprintf(&real_args[c], "%d", r->ws->idx + 1) < 1)
7529 err(1, "spawn_custom workspace index");
7530 } else if (strcasecmp(ap, "$dmenu_bottom") == 0) {
7531 if (!bar_at_bottom)
7532 continue;
7533 if ((real_args[c] = strdup("-b")) == NULL)
7534 err(1, "spawn_custom workspace index");
7535 } else {
7536 /* no match --> copy as is */
7537 if ((real_args[c] = strdup(ap)) == NULL)
7538 err(1, "spawn_custom strdup(ap)");
7539 }
7540 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
7541 real_args[c]);
7542 ++c;
7543 }
7544
7545 #ifdef SWM_DEBUG
7546 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
7547 for (i = 0; i < c; ++i)
7548 DPRINTF("\"%s\" ", real_args[i]);
7549 DPRINTF("\n");
7550 #endif
7551 *ret_args = real_args;
7552 return (c);
7553 }
7554
7555 void
7556 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
7557 {
7558 union arg a;
7559 char **real_args;
7560 int spawn_argc, i;
7561
7562 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
7563 return;
7564 a.argv = real_args;
7565 if (fork() == 0)
7566 spawn(r->ws->idx, &a, true);
7567
7568 for (i = 0; i < spawn_argc; i++)
7569 free(real_args[i]);
7570 free(real_args);
7571 }
7572
7573 void
7574 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
7575 int *pid)
7576 {
7577 union arg a;
7578 char **real_args;
7579 int i, spawn_argc;
7580
7581 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
7582 return;
7583 a.argv = real_args;
7584
7585 if (pipe(select_list_pipe) == -1)
7586 err(1, "pipe error");
7587 if (pipe(select_resp_pipe) == -1)
7588 err(1, "pipe error");
7589
7590 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
7591 err(1, "could not disable SIGPIPE");
7592 switch (*pid = fork()) {
7593 case -1:
7594 err(1, "cannot fork");
7595 break;
7596 case 0: /* child */
7597 if (dup2(select_list_pipe[0], STDIN_FILENO) == -1)
7598 err(1, "dup2");
7599 if (dup2(select_resp_pipe[1], STDOUT_FILENO) == -1)
7600 err(1, "dup2");
7601 close(select_list_pipe[1]);
7602 close(select_resp_pipe[0]);
7603 spawn(r->ws->idx, &a, false);
7604 break;
7605 default: /* parent */
7606 close(select_list_pipe[0]);
7607 close(select_resp_pipe[1]);
7608 break;
7609 }
7610
7611 for (i = 0; i < spawn_argc; i++)
7612 free(real_args[i]);
7613 free(real_args);
7614 }
7615
7616 /* Argument tokenizer. */
7617 char *
7618 argsep(char **sp) {
7619 char *arg, *cp, *next;
7620 bool single_quoted = false, double_quoted = false;
7621
7622 if (*sp == NULL)
7623 return NULL;
7624
7625 /* Eat and move characters until end of argument is found. */
7626 for (arg = next = cp = *sp; *cp != '\0'; ++cp) {
7627 if (!double_quoted && *cp == '\'') {
7628 /* Eat single-quote. */
7629 single_quoted = !single_quoted;
7630 } else if (!single_quoted && *cp == '"') {
7631 /* Eat double-quote. */
7632 double_quoted = !double_quoted;
7633 } else if (!single_quoted && *cp == '\\' && *(cp + 1) == '"') {
7634 /* Eat backslash; copy escaped character to arg. */
7635 *next++ = *(++cp);
7636 } else if (!single_quoted && !double_quoted && *cp == '\\' &&
7637 (*(cp + 1) == '\'' || *(cp + 1) == ' ')) {
7638 /* Eat backslash; move escaped character. */
7639 *next++ = *(++cp);
7640 } else if (!single_quoted && !double_quoted &&
7641 (*cp == ' ' || *cp == '\t')) {
7642 /* Terminate argument. */
7643 *next++ = '\0';
7644 /* Point sp to beginning of next argument. */
7645 *sp = ++cp;
7646 break;
7647 } else {
7648 /* Move regular character. */
7649 *next++ = *cp;
7650 }
7651 }
7652
7653 /* Terminate argument if end of string. */
7654 if (*cp == '\0') {
7655 *next = '\0';
7656 *sp = NULL;
7657 }
7658
7659 return arg;
7660 }
7661
7662 void
7663 spawn_insert(const char *name, const char *args, int flags)
7664 {
7665 struct spawn_prog *sp;
7666 char *arg, *cp, *ptr;
7667
7668 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s[%s]\n", name, args);
7669
7670 if (args == NULL || *args == '\0')
7671 return;
7672
7673 if ((sp = calloc(1, sizeof *sp)) == NULL)
7674 err(1, "spawn_insert: calloc");
7675 if ((sp->name = strdup(name)) == NULL)
7676 err(1, "spawn_insert: strdup");
7677
7678 /* Convert the arguments to an argument list. */
7679 if ((ptr = cp = strdup(args)) == NULL)
7680 err(1, "spawn_insert: strdup");
7681 while ((arg = argsep(&ptr)) != NULL) {
7682 /* Null argument; skip it. */
7683 if (*arg == '\0')
7684 continue;
7685
7686 sp->argc++;
7687 if ((sp->argv = realloc(sp->argv, sp->argc *
7688 sizeof *sp->argv)) == NULL)
7689 err(1, "spawn_insert: realloc");
7690 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
7691 err(1, "spawn_insert: strdup");
7692 }
7693 free(cp);
7694
7695 sp->flags = flags;
7696
7697 DNPRINTF(SWM_D_SPAWN, "arg %d: [%s]\n", sp->argc, sp->argv[sp->argc-1]);
7698 TAILQ_INSERT_TAIL(&spawns, sp, entry);
7699 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
7700 }
7701
7702 void
7703 spawn_remove(struct spawn_prog *sp)
7704 {
7705 int i;
7706
7707 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
7708
7709 TAILQ_REMOVE(&spawns, sp, entry);
7710 for (i = 0; i < sp->argc; i++)
7711 free(sp->argv[i]);
7712 free(sp->argv);
7713 free(sp->name);
7714 free(sp);
7715
7716 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
7717 }
7718
7719 void
7720 clear_spawns(void)
7721 {
7722 struct spawn_prog *sp;
7723
7724 while ((sp = TAILQ_FIRST(&spawns)) != NULL) {
7725 spawn_remove(sp);
7726 }
7727 }
7728
7729 struct spawn_prog*
7730 spawn_find(const char *name)
7731 {
7732 struct spawn_prog *sp;
7733
7734 TAILQ_FOREACH(sp, &spawns, entry)
7735 if (strcasecmp(sp->name, name) == 0)
7736 return sp;
7737
7738 return NULL;
7739 }
7740
7741 void
7742 setspawn(const char *name, const char *args, int flags)
7743 {
7744 struct spawn_prog *sp;
7745
7746 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
7747
7748 if (name == NULL)
7749 return;
7750
7751 /* Remove any old spawn under the same name. */
7752 if ((sp = spawn_find(name)) != NULL)
7753 spawn_remove(sp);
7754
7755 if (*args != '\0')
7756 spawn_insert(name, args, flags);
7757 else
7758 warnx("error: setspawn: cannot find program: %s", name);
7759
7760 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
7761 }
7762
7763 int
7764 setconfspawn(const char *selector, const char *value, int flags)
7765 {
7766 char *args;
7767
7768 if (selector == NULL || strlen(selector) == 0)
7769 return (1);
7770
7771 args = expand_tilde(value);
7772
7773 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
7774
7775 setspawn(selector, args, flags);
7776 free(args);
7777
7778 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
7779 return (0);
7780 }
7781
7782 void
7783 validate_spawns(void)
7784 {
7785 struct binding *bp;
7786 struct spawn_prog *sp;
7787 char which[PATH_MAX];
7788 size_t i;
7789
7790 RB_FOREACH(bp, binding_tree, &bindings) {
7791 if (bp->action != FN_SPAWN_CUSTOM)
7792 continue;
7793
7794 /* find program */
7795 sp = spawn_find(bp->spawn_name);
7796 if (sp == NULL || sp->flags & SWM_SPAWN_OPTIONAL)
7797 continue;
7798
7799 /* verify we have the goods */
7800 snprintf(which, sizeof which, "which %s", sp->argv[0]);
7801 DNPRINTF(SWM_D_CONF, "validate_spawns: which %s\n",
7802 sp->argv[0]);
7803 for (i = strlen("which "); i < strlen(which); i++)
7804 if (which[i] == ' ') {
7805 which[i] = '\0';
7806 break;
7807 }
7808 if (system(which) != 0)
7809 add_startup_exception("could not find %s",
7810 &which[strlen("which ")]);
7811 }
7812 }
7813
7814 void
7815 setup_spawn(void)
7816 {
7817 setconfspawn("lock", "xlock", 0);
7818
7819 setconfspawn("term", "xterm", 0);
7820 setconfspawn("spawn_term", "xterm", 0);
7821
7822 setconfspawn("menu", "dmenu_run"
7823 " $dmenu_bottom"
7824 " -fn $bar_font"
7825 " -nb $bar_color"
7826 " -nf $bar_font_color"
7827 " -sb $bar_border"
7828 " -sf $bar_color", 0);
7829
7830 setconfspawn("search", "dmenu"
7831 " $dmenu_bottom"
7832 " -i"
7833 " -fn $bar_font"
7834 " -nb $bar_color"
7835 " -nf $bar_font_color"
7836 " -sb $bar_border"
7837 " -sf $bar_color", 0);
7838
7839 setconfspawn("name_workspace", "dmenu"
7840 " $dmenu_bottom"
7841 " -p Workspace"
7842 " -fn $bar_font"
7843 " -nb $bar_color"
7844 " -nf $bar_font_color"
7845 " -sb $bar_border"
7846 " -sf $bar_color", 0);
7847
7848 /* These are not verified for existence, even with a binding set. */
7849 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL);
7850 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL);
7851 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL);
7852 }
7853
7854 /* bindings */
7855 #define SWM_MODNAME_SIZE 32
7856 #define SWM_KEY_WS "\n+ \t"
7857 int
7858 parsebinding(const char *bindstr, uint16_t *mod, enum binding_type *type,
7859 uint32_t *val, uint32_t *flags)
7860 {
7861 char *str, *cp, *name;
7862 KeySym ks, lks, uks;
7863
7864 DNPRINTF(SWM_D_KEY, "parsebinding: enter [%s]\n", bindstr);
7865 if (mod == NULL || val == NULL) {
7866 DNPRINTF(SWM_D_KEY, "parsebinding: no mod or key vars\n");
7867 return (1);
7868 }
7869 if (bindstr == NULL || strlen(bindstr) == 0) {
7870 DNPRINTF(SWM_D_KEY, "parsebinding: no bindstr\n");
7871 return (1);
7872 }
7873
7874 if ((cp = str = strdup(bindstr)) == NULL)
7875 err(1, "parsebinding: strdup");
7876
7877 *val = XCB_NO_SYMBOL;
7878 *mod = 0;
7879 *flags = 0;
7880 *type = KEYBIND;
7881 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
7882 DNPRINTF(SWM_D_KEY, "parsebinding: entry [%s]\n", name);
7883 if (cp)
7884 cp += (long)strspn(cp, SWM_KEY_WS);
7885 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
7886 *mod |= mod_key;
7887 else if (strncasecmp(name, "Mod1", SWM_MODNAME_SIZE) == 0)
7888 *mod |= XCB_MOD_MASK_1;
7889 else if (strncasecmp(name, "Mod2", SWM_MODNAME_SIZE) == 0)
7890 *mod |= XCB_MOD_MASK_2;
7891 else if (strncmp(name, "Mod3", SWM_MODNAME_SIZE) == 0)
7892 *mod |= XCB_MOD_MASK_3;
7893 else if (strncmp(name, "Mod4", SWM_MODNAME_SIZE) == 0)
7894 *mod |= XCB_MOD_MASK_4;
7895 else if (strncmp(name, "Mod5", SWM_MODNAME_SIZE) == 0)
7896 *mod |= XCB_MOD_MASK_5;
7897 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
7898 *mod |= XCB_MOD_MASK_SHIFT;
7899 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
7900 *mod |= XCB_MOD_MASK_CONTROL;
7901 else if (strncasecmp(name, "ANYMOD", SWM_MODNAME_SIZE) == 0)
7902 *mod |= XCB_MOD_MASK_ANY;
7903 else if (strncasecmp(name, "REPLAY", SWM_MODNAME_SIZE) == 0)
7904 *flags |= BINDING_F_REPLAY;
7905 else if (sscanf(name, "Button%u", val) == 1) {
7906 DNPRINTF(SWM_D_KEY, "parsebinding: button %u\n", *val);
7907 *type = BTNBIND;
7908 if (*val > 255 || *val == 0) {
7909 DNPRINTF(SWM_D_KEY,
7910 "parsebinding: invalid button %u\n", *val);
7911 return (1);
7912 }
7913 } else {
7914 /* TODO: do this without Xlib. */
7915 ks = XStringToKeysym(name);
7916 if (ks == NoSymbol) {
7917 DNPRINTF(SWM_D_KEY,
7918 "parsebinding: invalid key %s\n", name);
7919 free(str);
7920 return (1);
7921 }
7922
7923 XConvertCase(ks, &lks, &uks);
7924 *val = (uint32_t)lks;
7925 }
7926 }
7927
7928 /* If ANYMOD was specified, ignore the rest. */
7929 if (*mod & XCB_MOD_MASK_ANY)
7930 *mod = XCB_MOD_MASK_ANY;
7931
7932 free(str);
7933 DNPRINTF(SWM_D_KEY, "parsebinding: leave ok\n");
7934 return (0);
7935 }
7936
7937 char *
7938 strdupsafe(const char *str)
7939 {
7940 if (str == NULL)
7941 return (NULL);
7942 else
7943 return (strdup(str));
7944 }
7945
7946 int
7947 binding_cmp(struct binding *bp1, struct binding *bp2)
7948 {
7949 if (bp1->type < bp2->type)
7950 return (-1);
7951 if (bp1->type > bp2->type)
7952 return (1);
7953
7954 if (bp1->value < bp2->value)
7955 return (-1);
7956 if (bp1->value > bp2->value)
7957 return (1);
7958
7959 if (bp1->mod < bp2->mod)
7960 return (-1);
7961 if (bp1->mod > bp2->mod)
7962 return (1);
7963
7964 return (0);
7965 }
7966
7967 void
7968 binding_insert(uint16_t mod, enum binding_type type, uint32_t val,
7969 enum actionid aid, uint32_t flags, const char *spawn_name)
7970 {
7971 struct binding *bp;
7972
7973 DNPRINTF(SWM_D_KEY, "binding_insert: mod: %u, type: %d, val: %u, "
7974 "action: %s(%d), spawn_name: %s\n", mod, type, val,
7975 actions[aid].name, aid, spawn_name);
7976
7977 if ((bp = malloc(sizeof *bp)) == NULL)
7978 err(1, "binding_insert: malloc");
7979
7980 bp->mod = mod;
7981 bp->type = type;
7982 bp->value = val;
7983 bp->action = aid;
7984 bp->flags = flags;
7985 bp->spawn_name = strdupsafe(spawn_name);
7986 RB_INSERT(binding_tree, &bindings, bp);
7987
7988 DNPRINTF(SWM_D_KEY, "binding_insert: leave\n");
7989 }
7990
7991 struct binding *
7992 binding_lookup(uint16_t mod, enum binding_type type, uint32_t val)
7993 {
7994 struct binding bp;
7995
7996 bp.mod = mod;
7997 bp.type = type;
7998 bp.value = val;
7999
8000 return (RB_FIND(binding_tree, &bindings, &bp));
8001 }
8002
8003 void
8004 binding_remove(struct binding *bp)
8005 {
8006 DNPRINTF(SWM_D_KEY, "binding_remove: mod: %u, type: %d, val: %u, "
8007 "action: %s(%d), spawn_name: %s\n", bp->mod, bp->type, bp->value,
8008 actions[bp->action].name, bp->action, bp->spawn_name);
8009
8010 RB_REMOVE(binding_tree, &bindings, bp);
8011 free(bp->spawn_name);
8012 free(bp);
8013
8014 DNPRINTF(SWM_D_KEY, "binding_remove: leave\n");
8015 }
8016
8017 void
8018 setbinding(uint16_t mod, enum binding_type type, uint32_t val,
8019 enum actionid aid, uint32_t flags, const char *spawn_name)
8020 {
8021 struct binding *bp;
8022
8023 DNPRINTF(SWM_D_KEY, "setbinding: enter %s [%s]\n",
8024 actions[aid].name, spawn_name);
8025
8026 /* Unbind any existing. Loop is to handle MOD_MASK_ANY. */
8027 while ((bp = binding_lookup(mod, type, val)))
8028 binding_remove(bp);
8029
8030 if (aid != FN_INVALID)
8031 binding_insert(mod, type, val, aid, flags, spawn_name);
8032
8033 DNPRINTF(SWM_D_KEY, "setbinding: leave\n");
8034 }
8035
8036 int
8037 setconfbinding(const char *selector, const char *value, int flags)
8038 {
8039 struct spawn_prog *sp;
8040 uint32_t keybtn, opts;
8041 uint16_t mod;
8042 enum actionid aid;
8043 enum binding_type type;
8044
8045 /* suppress unused warning since var is needed */
8046 (void)flags;
8047
8048 DNPRINTF(SWM_D_KEY, "setconfbinding: enter selector: [%s], "
8049 "value: [%s]\n", selector, value);
8050 if (selector == NULL || strlen(selector) == 0) {
8051 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
8052 if (parsebinding(value, &mod, &type, &keybtn, &opts) == 0) {
8053 setbinding(mod, type, keybtn, FN_INVALID, opts, NULL);
8054 return (0);
8055 } else
8056 return (1);
8057 }
8058 /* search by key function name */
8059 for (aid = 0; aid < FN_INVALID; aid++) {
8060 if (strncasecmp(selector, actions[aid].name,
8061 SWM_FUNCNAME_LEN) == 0) {
8062 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
8063 "action\n", selector);
8064 if (parsebinding(value, &mod, &type, &keybtn,
8065 &opts) == 0) {
8066 setbinding(mod, type, keybtn, aid, opts, NULL);
8067 return (0);
8068 } else
8069 return (1);
8070 }
8071 }
8072 /* search by custom spawn name */
8073 if ((sp = spawn_find(selector)) != NULL) {
8074 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
8075 "spawn\n", selector);
8076 if (parsebinding(value, &mod, &type, &keybtn, &opts) == 0) {
8077 setbinding(mod, type, keybtn, FN_SPAWN_CUSTOM, opts,
8078 sp->name);
8079 return (0);
8080 } else
8081 return (1);
8082 }
8083 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
8084 return (1);
8085 }
8086
8087 #define MODSHIFT MODKEY | XCB_MOD_MASK_SHIFT
8088 void
8089 setup_keybindings(void)
8090 {
8091 #define BINDKEY(m, k, a) setbinding(m, KEYBIND, k, a, 0, NULL)
8092 #define BINDKEYSPAWN(m, k, s) setbinding(m, KEYBIND, k, FN_SPAWN_CUSTOM, 0, s)
8093 BINDKEY(MODKEY, XK_b, FN_BAR_TOGGLE);
8094 BINDKEY(MODSHIFT, XK_b, FN_BAR_TOGGLE_WS);
8095 BINDKEY(MODKEY, XK_b, FN_BAR_TOGGLE);
8096 BINDKEY(MODSHIFT, XK_b, FN_BAR_TOGGLE_WS);
8097 BINDKEY(MODKEY, XK_v, FN_BUTTON2);
8098 BINDKEY(MODKEY, XK_space, FN_CYCLE_LAYOUT);
8099 BINDKEY(MODSHIFT, XK_backslash, FN_FLIP_LAYOUT);
8100 BINDKEY(MODKEY, XK_t, FN_FLOAT_TOGGLE);
8101 BINDKEY(MODKEY, XK_m, FN_FOCUS_MAIN);
8102 BINDKEY(MODKEY, XK_j, FN_FOCUS_NEXT);
8103 BINDKEY(MODKEY, XK_Tab, FN_FOCUS_NEXT);
8104 BINDKEY(MODKEY, XK_k, FN_FOCUS_PREV);
8105 BINDKEY(MODSHIFT, XK_Tab, FN_FOCUS_PREV);
8106 BINDKEY(MODKEY, XK_u, FN_FOCUS_URGENT);
8107 BINDKEY(MODKEY, XK_e, FN_MAXIMIZE_TOGGLE);
8108 BINDKEY(MODSHIFT, XK_equal, FN_HEIGHT_GROW);
8109 BINDKEY(MODSHIFT, XK_minus, FN_HEIGHT_SHRINK);
8110 BINDKEY(MODKEY, XK_w, FN_ICONIFY);
8111 BINDKEY(MODKEY, XK_h, FN_MASTER_SHRINK);
8112 BINDKEY(MODKEY, XK_l, FN_MASTER_GROW);
8113 BINDKEY(MODKEY, XK_comma, FN_MASTER_ADD);
8114 BINDKEY(MODKEY, XK_period, FN_MASTER_DEL);
8115 BINDKEY(MODSHIFT, XK_bracketright, FN_MOVE_DOWN);
8116 BINDKEY(MODKEY, XK_bracketleft, FN_MOVE_LEFT);
8117 BINDKEY(MODKEY, XK_bracketright, FN_MOVE_RIGHT);
8118 BINDKEY(MODSHIFT, XK_bracketleft, FN_MOVE_UP);
8119 BINDKEY(MODSHIFT, XK_KP_End, FN_MVRG_1);
8120 BINDKEY(MODSHIFT, XK_KP_Down, FN_MVRG_2);
8121 BINDKEY(MODSHIFT, XK_KP_Next, FN_MVRG_3);
8122 BINDKEY(MODSHIFT, XK_KP_Left, FN_MVRG_4);
8123 BINDKEY(MODSHIFT, XK_KP_Begin, FN_MVRG_5);
8124 BINDKEY(MODSHIFT, XK_KP_Right, FN_MVRG_6);
8125 BINDKEY(MODSHIFT, XK_KP_Home, FN_MVRG_7);
8126 BINDKEY(MODSHIFT, XK_KP_Up, FN_MVRG_8);
8127 BINDKEY(MODSHIFT, XK_KP_Prior, FN_MVRG_9);
8128 BINDKEY(MODSHIFT, XK_1, FN_MVWS_1);
8129 BINDKEY(MODSHIFT, XK_2, FN_MVWS_2);
8130 BINDKEY(MODSHIFT, XK_3, FN_MVWS_3);
8131 BINDKEY(MODSHIFT, XK_4, FN_MVWS_4);
8132 BINDKEY(MODSHIFT, XK_5, FN_MVWS_5);
8133 BINDKEY(MODSHIFT, XK_6, FN_MVWS_6);
8134 BINDKEY(MODSHIFT, XK_7, FN_MVWS_7);
8135 BINDKEY(MODSHIFT, XK_8, FN_MVWS_8);
8136 BINDKEY(MODSHIFT, XK_9, FN_MVWS_9);
8137 BINDKEY(MODSHIFT, XK_0, FN_MVWS_10);
8138 BINDKEY(MODSHIFT, XK_F1, FN_MVWS_11);
8139 BINDKEY(MODSHIFT, XK_F2, FN_MVWS_12);
8140 BINDKEY(MODSHIFT, XK_F3, FN_MVWS_13);
8141 BINDKEY(MODSHIFT, XK_F4, FN_MVWS_14);
8142 BINDKEY(MODSHIFT, XK_F5, FN_MVWS_15);
8143 BINDKEY(MODSHIFT, XK_F6, FN_MVWS_16);
8144 BINDKEY(MODSHIFT, XK_F7, FN_MVWS_17);
8145 BINDKEY(MODSHIFT, XK_F8, FN_MVWS_18);
8146 BINDKEY(MODSHIFT, XK_F9, FN_MVWS_19);
8147 BINDKEY(MODSHIFT, XK_F10, FN_MVWS_20);
8148 BINDKEY(MODSHIFT, XK_F11, FN_MVWS_21);
8149 BINDKEY(MODSHIFT, XK_F12, FN_MVWS_22);
8150 BINDKEY(MODSHIFT, XK_slash, FN_NAME_WORKSPACE);
8151 BINDKEY(MODSHIFT, XK_q, FN_QUIT);
8152 BINDKEY(MODSHIFT, XK_r, FN_RAISE_TOGGLE);
8153 BINDKEY(MODKEY, XK_q, FN_RESTART);
8154 BINDKEY(MODKEY, XK_KP_End, FN_RG_1);
8155 BINDKEY(MODKEY, XK_KP_Down, FN_RG_2);
8156 BINDKEY(MODKEY, XK_KP_Next, FN_RG_3);
8157 BINDKEY(MODKEY, XK_KP_Left, FN_RG_4);
8158 BINDKEY(MODKEY, XK_KP_Begin, FN_RG_5);
8159 BINDKEY(MODKEY, XK_KP_Right, FN_RG_6);
8160 BINDKEY(MODKEY, XK_KP_Home, FN_RG_7);
8161 BINDKEY(MODKEY, XK_KP_Up, FN_RG_8);
8162 BINDKEY(MODKEY, XK_KP_Prior, FN_RG_9);
8163 BINDKEY(MODSHIFT, XK_Right, FN_RG_NEXT);
8164 BINDKEY(MODSHIFT, XK_Left, FN_RG_PREV);
8165 BINDKEY(MODKEY, XK_f, FN_SEARCH_WIN);
8166 BINDKEY(MODKEY, XK_slash, FN_SEARCH_WORKSPACE);
8167 BINDKEYSPAWN(MODSHIFT, XK_i, "initscr");
8168 BINDKEYSPAWN(MODSHIFT, XK_Delete, "lock");
8169 BINDKEYSPAWN(MODKEY, XK_p, "menu");
8170 BINDKEYSPAWN(MODKEY, XK_s, "screenshot_all");
8171 BINDKEYSPAWN(MODSHIFT, XK_s, "screenshot_wind");
8172 BINDKEYSPAWN(MODSHIFT, XK_Return, "term");
8173 BINDKEY(MODSHIFT, XK_comma, FN_STACK_INC);
8174 BINDKEY(MODSHIFT, XK_period, FN_STACK_DEC);
8175 BINDKEY(MODSHIFT, XK_space, FN_STACK_RESET);
8176 BINDKEY(MODKEY, XK_Return, FN_SWAP_MAIN);
8177 BINDKEY(MODSHIFT, XK_j, FN_SWAP_NEXT);
8178 BINDKEY(MODSHIFT, XK_k, FN_SWAP_PREV);
8179 BINDKEY(MODSHIFT, XK_w, FN_UNICONIFY);
8180 BINDKEY(MODSHIFT, XK_v, FN_VERSION);
8181 BINDKEY(MODKEY, XK_equal, FN_WIDTH_GROW);
8182 BINDKEY(MODKEY, XK_minus, FN_WIDTH_SHRINK);
8183 BINDKEY(MODKEY, XK_x, FN_WIND_DEL);
8184 BINDKEY(MODSHIFT, XK_x, FN_WIND_KILL);
8185 BINDKEY(MODKEY, XK_1, FN_WS_1);
8186 BINDKEY(MODKEY, XK_2, FN_WS_2);
8187 BINDKEY(MODKEY, XK_3, FN_WS_3);
8188 BINDKEY(MODKEY, XK_4, FN_WS_4);
8189 BINDKEY(MODKEY, XK_5, FN_WS_5);
8190 BINDKEY(MODKEY, XK_6, FN_WS_6);
8191 BINDKEY(MODKEY, XK_7, FN_WS_7);
8192 BINDKEY(MODKEY, XK_8, FN_WS_8);
8193 BINDKEY(MODKEY, XK_9, FN_WS_9);
8194 BINDKEY(MODKEY, XK_0, FN_WS_10);
8195 BINDKEY(MODKEY, XK_F1, FN_WS_11);
8196 BINDKEY(MODKEY, XK_F2, FN_WS_12);
8197 BINDKEY(MODKEY, XK_F3, FN_WS_13);
8198 BINDKEY(MODKEY, XK_F4, FN_WS_14);
8199 BINDKEY(MODKEY, XK_F5, FN_WS_15);
8200 BINDKEY(MODKEY, XK_F6, FN_WS_16);
8201 BINDKEY(MODKEY, XK_F7, FN_WS_17);
8202 BINDKEY(MODKEY, XK_F8, FN_WS_18);
8203 BINDKEY(MODKEY, XK_F9, FN_WS_19);
8204 BINDKEY(MODKEY, XK_F10, FN_WS_20);
8205 BINDKEY(MODKEY, XK_F11, FN_WS_21);
8206 BINDKEY(MODKEY, XK_F12, FN_WS_22);
8207 BINDKEY(MODKEY, XK_Right, FN_WS_NEXT);
8208 BINDKEY(MODKEY, XK_Left, FN_WS_PREV);
8209 BINDKEY(MODKEY, XK_Up, FN_WS_NEXT_ALL);
8210 BINDKEY(MODKEY, XK_Down, FN_WS_PREV_ALL);
8211 BINDKEY(MODSHIFT, XK_Up, FN_WS_NEXT_MOVE);
8212 BINDKEY(MODSHIFT, XK_Down, FN_WS_PREV_MOVE);
8213 BINDKEY(MODKEY, XK_a, FN_WS_PRIOR);
8214 #ifdef SWM_DEBUG
8215 BINDKEY(MODKEY, XK_d, FN_DEBUG_TOGGLE);
8216 BINDKEY(MODSHIFT, XK_d, FN_DUMPWINS);
8217 #endif
8218 #undef BINDKEY
8219 #undef BINDKEYSPAWN
8220 }
8221
8222 void
8223 setup_btnbindings(void)
8224 {
8225 setbinding(ANYMOD, BTNBIND, XCB_BUTTON_INDEX_1, FN_FOCUS,
8226 BINDING_F_REPLAY, NULL);
8227 setbinding(MODKEY, BTNBIND, XCB_BUTTON_INDEX_3, FN_RESIZE, 0, NULL);
8228 setbinding(MODSHIFT, BTNBIND, XCB_BUTTON_INDEX_3, FN_RESIZE_CENTERED, 0,
8229 NULL);
8230 setbinding(MODKEY, BTNBIND, XCB_BUTTON_INDEX_1, FN_MOVE, 0, NULL);
8231 }
8232 #undef MODSHIFT
8233
8234 void
8235 clear_bindings(void)
8236 {
8237 struct binding *bp;
8238
8239 while ((bp = RB_ROOT(&bindings)))
8240 binding_remove(bp);
8241 }
8242
8243 void
8244 clear_keybindings(void)
8245 {
8246 struct binding *bp, *bptmp;
8247
8248 RB_FOREACH_SAFE(bp, binding_tree, &bindings, bptmp) {
8249 if (bp->type != KEYBIND)
8250 continue;
8251 binding_remove(bp);
8252 }
8253 }
8254
8255 int
8256 setkeymapping(const char *selector, const char *value, int flags)
8257 {
8258 char *keymapping_file;
8259
8260 /* suppress unused warnings since vars are needed */
8261 (void)selector;
8262 (void)flags;
8263
8264 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
8265
8266 keymapping_file = expand_tilde(value);
8267
8268 clear_keybindings();
8269 /* load new key bindings; if it fails, revert to default bindings */
8270 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
8271 clear_keybindings();
8272 setup_keybindings();
8273 }
8274
8275 free(keymapping_file);
8276
8277 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
8278 return (0);
8279 }
8280
8281 void
8282 updatenumlockmask(void)
8283 {
8284 unsigned int i, j;
8285 xcb_get_modifier_mapping_reply_t *modmap_r;
8286 xcb_keycode_t *modmap, kc, *keycode;
8287
8288 numlockmask = 0;
8289
8290 modmap_r = xcb_get_modifier_mapping_reply(conn,
8291 xcb_get_modifier_mapping(conn),
8292 NULL);
8293 if (modmap_r) {
8294 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
8295 for (i = 0; i < 8; i++) {
8296 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
8297 kc = modmap[i * modmap_r->keycodes_per_modifier
8298 + j];
8299 keycode = xcb_key_symbols_get_keycode(syms,
8300 XK_Num_Lock);
8301 if (keycode) {
8302 if (kc == *keycode)
8303 numlockmask = (1 << i);
8304 free(keycode);
8305 }
8306 }
8307 }
8308 free(modmap_r);
8309 }
8310 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
8311 }
8312
8313 void
8314 grabkeys(void)
8315 {
8316 struct binding *bp;
8317 int num_screens, k, j;
8318 uint16_t modifiers[4];
8319 xcb_keycode_t *code;
8320
8321 DNPRINTF(SWM_D_MISC, "grabkeys\n");
8322 updatenumlockmask();
8323
8324 modifiers[0] = 0;
8325 modifiers[1] = numlockmask;
8326 modifiers[2] = XCB_MOD_MASK_LOCK;
8327 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
8328
8329 num_screens = get_screen_count();
8330 for (k = 0; k < num_screens; k++) {
8331 if (TAILQ_EMPTY(&screens[k].rl))
8332 continue;
8333 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
8334 XCB_MOD_MASK_ANY);
8335 RB_FOREACH(bp, binding_tree, &bindings) {
8336 if (bp->type != KEYBIND)
8337 continue;
8338
8339 /* If there is a catch-all, only bind that. */
8340 if ((binding_lookup(ANYMOD, KEYBIND, bp->value)) &&
8341 bp->mod != ANYMOD)
8342 continue;
8343
8344 /* Skip unused ws binds. */
8345 if ((int)bp->action > FN_WS_1 + workspace_limit - 1 &&
8346 bp->action <= FN_WS_22)
8347 continue;
8348
8349 /* Skip unused mvws binds. */
8350 if ((int)bp->action > FN_MVWS_1 + workspace_limit - 1 &&
8351 bp->action <= FN_MVWS_22)
8352 continue;
8353
8354 if ((code = xcb_key_symbols_get_keycode(syms,
8355 bp->value)) == NULL)
8356 continue;
8357
8358 if (bp->mod == XCB_MOD_MASK_ANY) {
8359 /* All modifiers are grabbed in one pass. */
8360 DNPRINTF(SWM_D_MOUSE, "grabkeys: grab, "
8361 "key: %u, modifiers: %d\n", bp->value,
8362 bp->mod);
8363 xcb_grab_key(conn, 1, screens[k].root,
8364 bp->mod, *code, XCB_GRAB_MODE_ASYNC,
8365 XCB_GRAB_MODE_SYNC);
8366 } else {
8367 /* Need to grab each modifier permutation. */
8368 for (j = 0; j < LENGTH(modifiers); j++) {
8369 DNPRINTF(SWM_D_MOUSE, "grabkeys: grab, "
8370 "key: %u, modifiers: %d\n",
8371 bp->value, bp->mod | modifiers[j]);
8372 xcb_grab_key(conn, 1,
8373 screens[k].root,
8374 bp->mod | modifiers[j],
8375 *code, XCB_GRAB_MODE_ASYNC,
8376 XCB_GRAB_MODE_SYNC);
8377 }
8378 }
8379 free(code);
8380 }
8381 }
8382 }
8383
8384 void
8385 grabbuttons(void)
8386 {
8387 struct binding *bp;
8388 int num_screens, i, k;
8389 uint16_t modifiers[4];
8390
8391 DNPRINTF(SWM_D_MOUSE, "grabbuttons\n");
8392 updatenumlockmask();
8393
8394 modifiers[0] = 0;
8395 modifiers[1] = numlockmask;
8396 modifiers[2] = XCB_MOD_MASK_LOCK;
8397 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
8398
8399 num_screens = get_screen_count();
8400 for (k = 0; k < num_screens; k++) {
8401 if (TAILQ_EMPTY(&screens[k].rl))
8402 continue;
8403 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, screens[k].root,
8404 XCB_MOD_MASK_ANY);
8405 RB_FOREACH(bp, binding_tree, &bindings) {
8406 if (bp->type != BTNBIND)
8407 continue;
8408
8409 /* If there is a catch-all, only bind that. */
8410 if ((binding_lookup(ANYMOD, BTNBIND, bp->value)) &&
8411 bp->mod != ANYMOD)
8412 continue;
8413
8414 /* Skip unused ws binds. */
8415 if ((int)bp->action > FN_WS_1 + workspace_limit - 1 &&
8416 bp->action <= FN_WS_22)
8417 continue;
8418
8419 /* Skip unused mvws binds. */
8420 if ((int)bp->action > FN_MVWS_1 + workspace_limit - 1 &&
8421 bp->action <= FN_MVWS_22)
8422 continue;
8423
8424 if (bp->mod == XCB_MOD_MASK_ANY) {
8425 /* All modifiers are grabbed in one pass. */
8426 DNPRINTF(SWM_D_MOUSE, "grabbuttons: grab, "
8427 "button: %u, modifiers: %d\n", bp->value,
8428 bp->mod);
8429 xcb_grab_button(conn, 0, screens[k].root,
8430 BUTTONMASK, XCB_GRAB_MODE_SYNC,
8431 XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
8432 XCB_CURSOR_NONE, bp->value, bp->mod);
8433 } else {
8434 /* Need to grab each modifier permutation. */
8435 for (i = 0; i < LENGTH(modifiers); ++i) {
8436 DNPRINTF(SWM_D_MOUSE, "grabbuttons: "
8437 "grab, button: %u, modifiers: %u\n",
8438 bp->value, bp->mod | modifiers[i]);
8439 xcb_grab_button(conn, 0,
8440 screens[k].root, BUTTONMASK,
8441 XCB_GRAB_MODE_SYNC,
8442 XCB_GRAB_MODE_ASYNC,
8443 XCB_WINDOW_NONE,
8444 XCB_CURSOR_NONE, bp->value,
8445 bp->mod | modifiers[i]);
8446 }
8447 }
8448 }
8449 }
8450 }
8451
8452 const char *quirkname[] = {
8453 "NONE", /* config string for "no value" */
8454 "FLOAT",
8455 "TRANSSZ",
8456 "ANYWHERE",
8457 "XTERM_FONTADJ",
8458 "FULLSCREEN",
8459 "FOCUSPREV",
8460 "NOFOCUSONMAP",
8461 "FOCUSONMAP_SINGLE",
8462 "OBEYAPPFOCUSREQ",
8463 "IGNOREPID",
8464 "IGNORESPAWNWS",
8465 "NOFOCUSCYCLE",
8466 "MINIMALBORDER",
8467 };
8468
8469 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
8470 #define SWM_Q_DELIM "\n|+ \t"
8471 int
8472 parsequirks(const char *qstr, uint32_t *quirk, int *ws)
8473 {
8474 char *str, *cp, *name;
8475 int i;
8476
8477 if (quirk == NULL || qstr == NULL)
8478 return (1);
8479
8480 if ((cp = str = strdup(qstr)) == NULL)
8481 err(1, "parsequirks: strdup");
8482
8483 *quirk = 0;
8484 while ((name = strsep(&cp, SWM_Q_DELIM)) != NULL) {
8485 if (cp)
8486 cp += (long)strspn(cp, SWM_Q_DELIM);
8487
8488 if (sscanf(name, "WS[%d]", ws) == 1) {
8489 if (*ws > 0)
8490 *ws -= 1;
8491 continue;
8492 }
8493
8494 for (i = 0; i < LENGTH(quirkname); i++) {
8495 if (strncasecmp(name, quirkname[i],
8496 SWM_QUIRK_LEN) == 0) {
8497 DNPRINTF(SWM_D_QUIRK,
8498 "parsequirks: %s\n", name);
8499 if (i == 0) {
8500 *quirk = 0;
8501 free(str);
8502 return (0);
8503 }
8504 *quirk |= 1 << (i-1);
8505 break;
8506 }
8507 }
8508 if (i >= LENGTH(quirkname)) {
8509 DNPRINTF(SWM_D_QUIRK,
8510 "parsequirks: invalid quirk [%s]\n", name);
8511 free(str);
8512 return (1);
8513 }
8514 }
8515
8516 free(str);
8517 return (0);
8518 }
8519
8520 void
8521 quirk_insert(const char *class, const char *instance, const char *name,
8522 uint32_t quirk, int ws)
8523 {
8524 struct quirk *qp;
8525 char *str;
8526 bool failed = false;
8527
8528 DNPRINTF(SWM_D_QUIRK, "quirk_insert: class: %s, instance: %s, name: %s,"
8529 " value: %u, ws: %d\n", class, instance, name, quirk, ws);
8530
8531 if ((qp = malloc(sizeof *qp)) == NULL)
8532 err(1, "quirk_insert: malloc");
8533
8534 if ((qp->class = strdup(class)) == NULL)
8535 err(1, "quirk_insert: strdup");
8536 if ((qp->instance = strdup(instance)) == NULL)
8537 err(1, "quirk_insert: strdup");
8538 if ((qp->name = strdup(name)) == NULL)
8539 err(1, "quirk_insert: strdup");
8540
8541 if (asprintf(&str, "^%s$", class) == -1)
8542 err(1, "quirk_insert: asprintf");
8543 if (regcomp(&qp->regex_class, str, REG_EXTENDED | REG_NOSUB)) {
8544 add_startup_exception("regex failed to compile quirk 'class' "
8545 "field: %s", class);
8546 failed = true;
8547 }
8548 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
8549 free(str);
8550
8551 if (asprintf(&str, "^%s$", instance) == -1)
8552 err(1, "quirk_insert: asprintf");
8553 if (regcomp(&qp->regex_instance, str, REG_EXTENDED | REG_NOSUB)) {
8554 add_startup_exception("regex failed to compile quirk 'instance'"
8555 " field: %s", instance);
8556 failed = true;
8557 }
8558 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
8559 free(str);
8560
8561 if (asprintf(&str, "^%s$", name) == -1)
8562 err(1, "quirk_insert: asprintf");
8563 if (regcomp(&qp->regex_name, str, REG_EXTENDED | REG_NOSUB)) {
8564 add_startup_exception("regex failed to compile quirk 'name' "
8565 "field: %s", name);
8566 failed = true;
8567 }
8568 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
8569 free(str);
8570
8571 if (failed) {
8572 DNPRINTF(SWM_D_QUIRK, "quirk_insert: regex error; skipping.\n");
8573 quirk_free(qp);
8574 } else {
8575 qp->quirk = quirk;
8576 qp->ws = ws;
8577 TAILQ_INSERT_TAIL(&quirks, qp, entry);
8578 }
8579 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
8580 }
8581
8582 void
8583 quirk_remove(struct quirk *qp)
8584 {
8585 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%u]\n", qp->class,
8586 qp->name, qp->quirk);
8587
8588 TAILQ_REMOVE(&quirks, qp, entry);
8589 quirk_free(qp);
8590
8591 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
8592 }
8593
8594 void
8595 quirk_free(struct quirk *qp)
8596 {
8597 regfree(&qp->regex_class);
8598 regfree(&qp->regex_instance);
8599 regfree(&qp->regex_name);
8600 free(qp->class);
8601 free(qp->instance);
8602 free(qp->name);
8603 free(qp);
8604 }
8605
8606 void
8607 clear_quirks(void)
8608 {
8609 struct quirk *qp;
8610
8611 while ((qp = TAILQ_FIRST(&quirks)) != NULL) {
8612 quirk_remove(qp);
8613 }
8614 }
8615
8616 void
8617 quirk_replace(struct quirk *qp, const char *class, const char *instance,
8618 const char *name, uint32_t quirk, int ws)
8619 {
8620 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp->class,
8621 qp->instance, qp->name, qp->quirk, qp->ws);
8622
8623 quirk_remove(qp);
8624 quirk_insert(class, instance, name, quirk, ws);
8625
8626 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
8627 }
8628
8629 void
8630 setquirk(const char *class, const char *instance, const char *name,
8631 uint32_t quirk, int ws)
8632 {
8633 struct quirk *qp;
8634
8635 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
8636 instance, name, quirk, ws);
8637
8638 /* Remove/replace existing quirk. */
8639 TAILQ_FOREACH(qp, &quirks, entry) {
8640 if (strcmp(qp->class, class) == 0 &&
8641 strcmp(qp->instance, instance) == 0 &&
8642 strcmp(qp->name, name) == 0) {
8643 if (quirk == 0 && ws == -1)
8644 quirk_remove(qp);
8645 else
8646 quirk_replace(qp, class, instance, name, quirk,
8647 ws);
8648 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
8649 return;
8650 }
8651 }
8652
8653 /* Only insert if quirk is not NONE or forced ws is set. */
8654 if (quirk || ws != -1)
8655 quirk_insert(class, instance, name, quirk, ws);
8656
8657 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
8658 }
8659
8660 /* Eat '\' in str used to escape square brackets and colon. */
8661 void
8662 unescape_selector(char *str)
8663 {
8664 char *cp;
8665
8666 for (cp = str; *str != '\0'; ++str, ++cp) {
8667 if (*str == '\\' && (*(str + 1) == ':' || *(str + 1) == ']' ||
8668 *(str + 1) == '['))
8669 ++str;
8670
8671 *cp = *str;
8672 }
8673 *cp = '\0';
8674 }
8675
8676 int
8677 setconfquirk(const char *selector, const char *value, int flags)
8678 {
8679 char *str, *cp, *class;
8680 char *instance = NULL, *name = NULL;
8681 int retval, count = 0, ws = -1;
8682 uint32_t qrks;
8683
8684 /* suppress unused warning since var is needed */
8685 (void)flags;
8686
8687 if (selector == NULL || strlen(selector) == 0)
8688 return (0);
8689
8690 if ((str = strdup(selector)) == NULL)
8691 err(1, "setconfquirk: strdup");
8692
8693 /* Find non-escaped colon. */
8694 class = cp = str;
8695 if (*cp == ':') {
8696 *cp = '\0';
8697 ++count;
8698 }
8699
8700 for (++cp; *cp != '\0'; ++cp) {
8701 if (*cp == ':' && *(cp - 1) != '\\') {
8702 *cp = '\0';
8703 ++count;
8704 }
8705 }
8706
8707 unescape_selector(class);
8708 if (count) {
8709 instance = class + strlen(class) + 1;
8710 unescape_selector(instance);
8711 } else {
8712 instance = ".*";
8713 }
8714
8715 if (count > 1) {
8716 name = instance + strlen(instance) + 1;
8717 unescape_selector(name);
8718 } else {
8719 name = ".*";
8720 }
8721
8722 DNPRINTF(SWM_D_CONF, "setconfquirk: class: %s, instance: %s, "
8723 "name: %s\n", class, instance, name);
8724
8725 if ((retval = parsequirks(value, &qrks, &ws)) == 0)
8726 setquirk(class, instance, name, qrks, ws);
8727
8728 free(str);
8729 return (retval);
8730 }
8731
8732 void
8733 setup_quirks(void)
8734 {
8735 setquirk("MPlayer", "xv", ".*",
8736 SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV, -1);
8737 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8738 SWM_Q_FLOAT, -1);
8739 setquirk("Firefox-bin", "firefox-bin", ".*",
8740 SWM_Q_TRANSSZ, -1);
8741 setquirk("Firefox", "Dialog", ".*",
8742 SWM_Q_FLOAT, -1);
8743 setquirk("Gimp", "gimp", ".*",
8744 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
8745 setquirk("XTerm", "xterm", ".*",
8746 SWM_Q_XTERM_FONTADJ, -1);
8747 setquirk("xine", "Xine Window", ".*",
8748 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
8749 setquirk("Xitk", "Xitk Combo", ".*",
8750 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
8751 setquirk("xine", "xine Panel", ".*",
8752 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
8753 setquirk("Xitk", "Xine Window", ".*",
8754 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
8755 setquirk("xine", "xine Video Fullscreen Window", ".*",
8756 SWM_Q_FULLSCREEN | SWM_Q_FLOAT, -1);
8757 setquirk("pcb", "pcb", ".*",
8758 SWM_Q_FLOAT, -1);
8759 setquirk("SDL_App", "SDL_App", ".*",
8760 SWM_Q_FLOAT | SWM_Q_FULLSCREEN, -1);
8761 }
8762
8763 /* conf file stuff */
8764 #define SWM_CONF_FILE "spectrwm.conf"
8765 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8766
8767 enum {
8768 SWM_S_BAR_ACTION,
8769 SWM_S_BAR_AT_BOTTOM,
8770 SWM_S_BAR_BORDER_WIDTH,
8771 SWM_S_BAR_DELAY,
8772 SWM_S_BAR_ENABLED,
8773 SWM_S_BAR_ENABLED_WS,
8774 SWM_S_BAR_FONT,
8775 SWM_S_BAR_FORMAT,
8776 SWM_S_BAR_JUSTIFY,
8777 SWM_S_BORDER_WIDTH,
8778 SWM_S_BOUNDARY_WIDTH,
8779 SWM_S_CLOCK_ENABLED,
8780 SWM_S_CLOCK_FORMAT,
8781 SWM_S_CYCLE_EMPTY,
8782 SWM_S_CYCLE_VISIBLE,
8783 SWM_S_DIALOG_RATIO,
8784 SWM_S_DISABLE_BORDER,
8785 SWM_S_FOCUS_CLOSE,
8786 SWM_S_FOCUS_CLOSE_WRAP,
8787 SWM_S_FOCUS_DEFAULT,
8788 SWM_S_FOCUS_MODE,
8789 SWM_S_ICONIC_ENABLED,
8790 SWM_S_JAVA_WORKAROUND,
8791 SWM_S_MAXIMIZE_HIDE_BAR,
8792 SWM_S_REGION_PADDING,
8793 SWM_S_SPAWN_ORDER,
8794 SWM_S_SPAWN_TERM,
8795 SWM_S_SS_APP,
8796 SWM_S_SS_ENABLED,
8797 SWM_S_STACK_ENABLED,
8798 SWM_S_TERM_WIDTH,
8799 SWM_S_TILE_GAP,
8800 SWM_S_URGENT_COLLAPSE,
8801 SWM_S_URGENT_ENABLED,
8802 SWM_S_VERBOSE_LAYOUT,
8803 SWM_S_WARP_FOCUS,
8804 SWM_S_WARP_POINTER,
8805 SWM_S_WINDOW_CLASS_ENABLED,
8806 SWM_S_WINDOW_INSTANCE_ENABLED,
8807 SWM_S_WINDOW_NAME_ENABLED,
8808 SWM_S_WORKSPACE_CLAMP,
8809 SWM_S_WORKSPACE_LIMIT,
8810 SWM_S_WORKSPACE_NAME,
8811 };
8812
8813 int
8814 setconfvalue(const char *selector, const char *value, int flags)
8815 {
8816 struct workspace *ws;
8817 int i, ws_id, num_screens;
8818 char *b, *str, *sp, s[1024];
8819
8820 switch (flags) {
8821 case SWM_S_BAR_ACTION:
8822 free(bar_argv[0]);
8823 if ((bar_argv[0] = expand_tilde(value)) == NULL)
8824 err(1, "setconfvalue: bar_action");
8825 break;
8826 case SWM_S_BAR_AT_BOTTOM:
8827 bar_at_bottom = (atoi(value) != 0);
8828 break;
8829 case SWM_S_BAR_BORDER_WIDTH:
8830 bar_border_width = atoi(value);
8831 if (bar_border_width < 0)
8832 bar_border_width = 0;
8833 break;
8834 case SWM_S_BAR_DELAY:
8835 /* No longer needed; leave to not break old conf files. */
8836 break;
8837 case SWM_S_BAR_ENABLED:
8838 bar_enabled = (atoi(value) != 0);
8839 break;
8840 case SWM_S_BAR_ENABLED_WS:
8841 ws_id = atoi(selector) - 1;
8842 if (ws_id < 0 || ws_id >= workspace_limit)
8843 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8844 "workspace %d.", ws_id + 1);
8845
8846 num_screens = get_screen_count();
8847 for (i = 0; i < num_screens; i++) {
8848 ws = (struct workspace *)&screens[i].ws;
8849 ws[ws_id].bar_enabled = (atoi(value) != 0);
8850 }
8851 break;
8852 case SWM_S_BAR_FONT:
8853 b = bar_fonts;
8854 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
8855 err(1, "setconfvalue: asprintf: failed to allocate "
8856 "memory for bar_fonts.");
8857 free(b);
8858
8859 /* If already in xft mode, then we are done. */
8860 if (!bar_font_legacy)
8861 break;
8862
8863 if ((sp = str = strdup(value)) == NULL)
8864 err(1, "setconfvalue: strdup");
8865
8866 /* If there are any non-XLFD entries, switch to Xft mode. */
8867 while ((b = strsep(&sp, ",")) != NULL) {
8868 if (*b == '\0')
8869 continue;
8870 if (!isxlfd(b)) {
8871 bar_font_legacy = false;
8872 break;
8873 }
8874 }
8875
8876 free(str);
8877 break;
8878 case SWM_S_BAR_FORMAT:
8879 free(bar_format);
8880 if ((bar_format = strdup(value)) == NULL)
8881 err(1, "setconfvalue: bar_format");
8882 break;
8883 case SWM_S_BAR_JUSTIFY:
8884 if (strcmp(value, "left") == 0)
8885 bar_justify = SWM_BAR_JUSTIFY_LEFT;
8886 else if (strcmp(value, "center") == 0)
8887 bar_justify = SWM_BAR_JUSTIFY_CENTER;
8888 else if (strcmp(value, "right") == 0)
8889 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
8890 else
8891 errx(1, "invalid bar_justify");
8892 break;
8893 case SWM_S_BORDER_WIDTH:
8894 border_width = atoi(value);
8895 if (border_width < 0)
8896 border_width = 0;
8897 break;
8898 case SWM_S_BOUNDARY_WIDTH:
8899 boundary_width = atoi(value);
8900 if (boundary_width < 0)
8901 boundary_width = 0;
8902 break;
8903 case SWM_S_CLOCK_ENABLED:
8904 clock_enabled = (atoi(value) != 0);
8905 break;
8906 case SWM_S_CLOCK_FORMAT:
8907 #ifndef SWM_DENY_CLOCK_FORMAT
8908 free(clock_format);
8909 if ((clock_format = strdup(value)) == NULL)
8910 err(1, "setconfvalue: clock_format");
8911 #endif
8912 break;
8913 case SWM_S_CYCLE_EMPTY:
8914 cycle_empty = (atoi(value) != 0);
8915 break;
8916 case SWM_S_CYCLE_VISIBLE:
8917 cycle_visible = (atoi(value) != 0);
8918 break;
8919 case SWM_S_DIALOG_RATIO:
8920 dialog_ratio = atof(value);
8921 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
8922 dialog_ratio = .6;
8923 break;
8924 case SWM_S_DISABLE_BORDER:
8925 disable_border = (atoi(value) != 0);
8926 break;
8927 case SWM_S_FOCUS_CLOSE:
8928 if (strcmp(value, "first") == 0)
8929 focus_close = SWM_STACK_BOTTOM;
8930 else if (strcmp(value, "last") == 0)
8931 focus_close = SWM_STACK_TOP;
8932 else if (strcmp(value, "next") == 0)
8933 focus_close = SWM_STACK_ABOVE;
8934 else if (strcmp(value, "previous") == 0)
8935 focus_close = SWM_STACK_BELOW;
8936 else
8937 errx(1, "focus_close");
8938 break;
8939 case SWM_S_FOCUS_CLOSE_WRAP:
8940 focus_close_wrap = (atoi(value) != 0);
8941 break;
8942 case SWM_S_FOCUS_DEFAULT:
8943 if (strcmp(value, "last") == 0)
8944 focus_default = SWM_STACK_TOP;
8945 else if (strcmp(value, "first") == 0)
8946 focus_default = SWM_STACK_BOTTOM;
8947 else
8948 errx(1, "focus_default");
8949 break;
8950 case SWM_S_FOCUS_MODE:
8951 if (strcmp(value, "default") == 0)
8952 focus_mode = SWM_FOCUS_DEFAULT;
8953 else if (strcmp(value, "follow") == 0 ||
8954 strcmp(value, "follow_cursor") == 0)
8955 focus_mode = SWM_FOCUS_FOLLOW;
8956 else if (strcmp(value, "manual") == 0)
8957 focus_mode = SWM_FOCUS_MANUAL;
8958 else
8959 errx(1, "focus_mode");
8960 break;
8961 case SWM_S_ICONIC_ENABLED:
8962 iconic_enabled = (atoi(value) != 0);
8963 break;
8964 case SWM_S_JAVA_WORKAROUND:
8965 java_workaround = (atoi(value) != 0);
8966 break;
8967 case SWM_S_MAXIMIZE_HIDE_BAR:
8968 maximize_hide_bar = atoi(value);
8969 break;
8970 case SWM_S_REGION_PADDING:
8971 region_padding = atoi(value);
8972 if (region_padding < 0)
8973 region_padding = 0;
8974 break;
8975 case SWM_S_SPAWN_ORDER:
8976 if (strcmp(value, "first") == 0)
8977 spawn_position = SWM_STACK_BOTTOM;
8978 else if (strcmp(value, "last") == 0)
8979 spawn_position = SWM_STACK_TOP;
8980 else if (strcmp(value, "next") == 0)
8981 spawn_position = SWM_STACK_ABOVE;
8982 else if (strcmp(value, "previous") == 0)
8983 spawn_position = SWM_STACK_BELOW;
8984 else
8985 errx(1, "spawn_position");
8986 break;
8987 case SWM_S_SPAWN_TERM:
8988 setconfspawn("term", value, 0);
8989 setconfspawn("spawn_term", value, 0);
8990 break;
8991 case SWM_S_SS_APP:
8992 /* No longer needed; leave to not break old conf files. */
8993 break;
8994 case SWM_S_SS_ENABLED:
8995 /* No longer needed; leave to not break old conf files. */
8996 break;
8997 case SWM_S_STACK_ENABLED:
8998 stack_enabled = (atoi(value) != 0);
8999 break;
9000 case SWM_S_TERM_WIDTH:
9001 term_width = atoi(value);
9002 if (term_width < 0)
9003 term_width = 0;
9004 break;
9005 case SWM_S_TILE_GAP:
9006 tile_gap = atoi(value);
9007 break;
9008 case SWM_S_URGENT_COLLAPSE:
9009 urgent_collapse = (atoi(value) != 0);
9010 break;
9011 case SWM_S_URGENT_ENABLED:
9012 urgent_enabled = (atoi(value) != 0);
9013 break;
9014 case SWM_S_VERBOSE_LAYOUT:
9015 verbose_layout = (atoi(value) != 0);
9016 for (i = 0; layouts[i].l_stack != NULL; i++) {
9017 if (verbose_layout)
9018 layouts[i].l_string = fancy_stacker;
9019 else
9020 layouts[i].l_string = plain_stacker;
9021 }
9022 break;
9023 case SWM_S_WARP_FOCUS:
9024 warp_focus = (atoi(value) != 0);
9025 break;
9026 case SWM_S_WARP_POINTER:
9027 warp_pointer = (atoi(value) != 0);
9028 break;
9029 case SWM_S_WINDOW_CLASS_ENABLED:
9030 window_class_enabled = (atoi(value) != 0);
9031 break;
9032 case SWM_S_WINDOW_INSTANCE_ENABLED:
9033 window_instance_enabled = (atoi(value) != 0);
9034 break;
9035 case SWM_S_WINDOW_NAME_ENABLED:
9036 window_name_enabled = (atoi(value) != 0);
9037 break;
9038 case SWM_S_WORKSPACE_CLAMP:
9039 workspace_clamp = (atoi(value) != 0);
9040 break;
9041 case SWM_S_WORKSPACE_LIMIT:
9042 workspace_limit = atoi(value);
9043 if (workspace_limit > SWM_WS_MAX)
9044 workspace_limit = SWM_WS_MAX;
9045 else if (workspace_limit < 1)
9046 workspace_limit = 1;
9047
9048 ewmh_update_desktops();
9049 break;
9050 case SWM_S_WORKSPACE_NAME:
9051 if (getenv("SWM_STARTED") != NULL)
9052 return (0);
9053
9054 bzero(s, sizeof s);
9055 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
9056 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
9057 ws_id--;
9058 if (ws_id < 0 || ws_id >= workspace_limit)
9059 errx(1, "setconfvalue: workspace_name: invalid "
9060 "workspace %d.", ws_id + 1);
9061
9062 num_screens = get_screen_count();
9063 for (i = 0; i < num_screens; ++i) {
9064 ws = (struct workspace *)&screens[i].ws;
9065
9066 if (strlen(s) > 0) {
9067 free(ws[ws_id].name);
9068 if ((ws[ws_id].name = strdup(s)) == NULL)
9069 err(1, "setconfvalue: workspace_name.");
9070
9071 ewmh_update_desktop_names();
9072 ewmh_get_desktop_names();
9073 }
9074 }
9075 break;
9076 default:
9077 return (1);
9078 }
9079 return (0);
9080 }
9081
9082 int
9083 setconfmodkey(const char *selector, const char *value, int flags)
9084 {
9085 /* suppress unused warnings since vars are needed */
9086 (void)selector;
9087 (void)flags;
9088
9089 if (strncasecmp(value, "Mod1", strlen("Mod1")) == 0)
9090 update_modkey(XCB_MOD_MASK_1);
9091 else if (strncasecmp(value, "Mod2", strlen("Mod2")) == 0)
9092 update_modkey(XCB_MOD_MASK_2);
9093 else if (strncasecmp(value, "Mod3", strlen("Mod3")) == 0)
9094 update_modkey(XCB_MOD_MASK_3);
9095 else if (strncasecmp(value, "Mod4", strlen("Mod4")) == 0)
9096 update_modkey(XCB_MOD_MASK_4);
9097 else if (strncasecmp(value, "Mod5", strlen("Mod5")) == 0)
9098 update_modkey(XCB_MOD_MASK_5);
9099 else
9100 return (1);
9101 return (0);
9102 }
9103
9104 int
9105 setconfcolor(const char *selector, const char *value, int flags)
9106 {
9107 int first, last, i = 0, num_screens;
9108
9109 num_screens = get_screen_count();
9110
9111 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
9112 if (selector == NULL || strlen(selector) == 0 ||
9113 (last = atoi(selector) - 1) < 0) {
9114 first = 0;
9115 last = num_screens - 1;
9116 } else {
9117 first = last;
9118 }
9119
9120 if (last >= num_screens) {
9121 add_startup_exception("invalid screen index: %d out of bounds "
9122 "(maximum %d)", last + 1, num_screens);
9123 return (1);
9124 }
9125
9126 for (i = first; i <= last; ++i) {
9127 setscreencolor(value, i, flags);
9128
9129 /*
9130 * When setting focus/unfocus colors, we need to also
9131 * set maximize colors to match if they haven't been customized.
9132 */
9133 if (flags == SWM_S_COLOR_FOCUS &&
9134 !screens[i].c[SWM_S_COLOR_FOCUS_MAXIMIZED].manual)
9135 setscreencolor(value, i, SWM_S_COLOR_FOCUS_MAXIMIZED);
9136 else if (flags == SWM_S_COLOR_UNFOCUS &&
9137 !screens[i].c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].manual)
9138 setscreencolor(value, i, SWM_S_COLOR_UNFOCUS_MAXIMIZED);
9139
9140 screens[i].c[flags].manual = 1;
9141 }
9142
9143 return (0);
9144 }
9145
9146 int
9147 setconfregion(const char *selector, const char *value, int flags)
9148 {
9149 /* suppress unused warnings since vars are needed */
9150 (void)selector;
9151 (void)flags;
9152
9153 custom_region(value);
9154 return (0);
9155 }
9156
9157 int
9158 setautorun(const char *selector, const char *value, int flags)
9159 {
9160 int ws_id;
9161 char s[1024];
9162 char *ap, *sp, *str;
9163 union arg a;
9164 int argc = 0;
9165 pid_t pid;
9166 struct pid_e *p;
9167
9168 /* suppress unused warnings since vars are needed */
9169 (void)selector;
9170 (void)flags;
9171
9172 if (getenv("SWM_STARTED"))
9173 return (0);
9174
9175 bzero(s, sizeof s);
9176 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
9177 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
9178 ws_id--;
9179 if (ws_id < 0 || ws_id >= workspace_limit)
9180 errx(1, "autorun: invalid workspace %d", ws_id + 1);
9181
9182 sp = str = expand_tilde((char *)&s);
9183
9184 /*
9185 * This is a little intricate
9186 *
9187 * If the pid already exists we simply reuse it because it means it was
9188 * used before AND not claimed by manage_window. We get away with
9189 * altering it in the parent after INSERT because this can not be a race
9190 */
9191 a.argv = NULL;
9192 while ((ap = strsep(&sp, " \t")) != NULL) {
9193 if (*ap == '\0')
9194 continue;
9195 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
9196 argc++;
9197 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
9198 err(1, "setautorun: realloc");
9199 a.argv[argc - 1] = ap;
9200 }
9201
9202 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
9203 err(1, "setautorun: realloc");
9204 a.argv[argc] = NULL;
9205
9206 if ((pid = fork()) == 0) {
9207 spawn(ws_id, &a, true);
9208 /* NOTREACHED */
9209 _exit(1);
9210 }
9211 free(a.argv);
9212 free(str);
9213
9214 /* parent */
9215 p = find_pid(pid);
9216 if (p == NULL) {
9217 p = calloc(1, sizeof *p);
9218 if (p == NULL)
9219 return (1);
9220 TAILQ_INSERT_TAIL(&pidlist, p, entry);
9221 }
9222
9223 p->pid = pid;
9224 p->ws = ws_id;
9225
9226 return (0);
9227 }
9228
9229 int
9230 setlayout(const char *selector, const char *value, int flags)
9231 {
9232 struct workspace *ws;
9233 int ws_id, i, x, mg, ma, si, ar;
9234 int st = SWM_V_STACK, num_screens;
9235 char s[1024];
9236 bool f = false;
9237
9238 /* suppress unused warnings since vars are needed */
9239 (void)selector;
9240 (void)flags;
9241
9242 if (getenv("SWM_STARTED"))
9243 return (0);
9244
9245 bzero(s, sizeof s);
9246 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
9247 &ws_id, &mg, &ma, &si, &ar, s) != 6)
9248 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9249 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9250 "<type>'");
9251 ws_id--;
9252 if (ws_id < 0 || ws_id >= workspace_limit)
9253 errx(1, "layout: invalid workspace %d", ws_id + 1);
9254
9255 if (strcasecmp(s, "vertical") == 0)
9256 st = SWM_V_STACK;
9257 else if (strcasecmp(s, "vertical_flip") == 0) {
9258 st = SWM_V_STACK;
9259 f = true;
9260 } else if (strcasecmp(s, "horizontal") == 0)
9261 st = SWM_H_STACK;
9262 else if (strcasecmp(s, "horizontal_flip") == 0) {
9263 st = SWM_H_STACK;
9264 f = true;
9265 } else if (strcasecmp(s, "fullscreen") == 0)
9266 st = SWM_MAX_STACK;
9267 else
9268 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9269 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9270 "<type>'");
9271
9272 num_screens = get_screen_count();
9273 for (i = 0; i < num_screens; i++) {
9274 ws = (struct workspace *)&screens[i].ws;
9275 ws[ws_id].cur_layout = &layouts[st];
9276
9277 ws[ws_id].always_raise = (ar != 0);
9278 if (st == SWM_MAX_STACK)
9279 continue;
9280
9281 /* master grow */
9282 for (x = 0; x < abs(mg); x++) {
9283 ws[ws_id].cur_layout->l_config(&ws[ws_id],
9284 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
9285 SWM_ARG_ID_MASTERSHRINK);
9286 }
9287 /* master add */
9288 for (x = 0; x < abs(ma); x++) {
9289 ws[ws_id].cur_layout->l_config(&ws[ws_id],
9290 ma >= 0 ? SWM_ARG_ID_MASTERADD :
9291 SWM_ARG_ID_MASTERDEL);
9292 }
9293 /* stack inc */
9294 for (x = 0; x < abs(si); x++) {
9295 ws[ws_id].cur_layout->l_config(&ws[ws_id],
9296 si >= 0 ? SWM_ARG_ID_STACKINC :
9297 SWM_ARG_ID_STACKDEC);
9298 }
9299 /* Apply flip */
9300 if (f) {
9301 ws[ws_id].cur_layout->l_config(&ws[ws_id],
9302 SWM_ARG_ID_FLIPLAYOUT);
9303 }
9304 }
9305
9306 return (0);
9307 }
9308
9309 /* config options */
9310 struct config_option {
9311 char *optname;
9312 int (*func)(const char*, const char*, int);
9313 int funcflags;
9314 };
9315 struct config_option configopt[] = {
9316 { "autorun", setautorun, 0 },
9317 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
9318 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
9319 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
9320 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
9321 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
9322 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
9323 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
9324 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
9325 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
9326 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
9327 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
9328 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
9329 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
9330 { "bind", setconfbinding, 0 },
9331 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
9332 { "boundary_width", setconfvalue, SWM_S_BOUNDARY_WIDTH },
9333 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
9334 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
9335 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
9336 { "color_focus_maximized", setconfcolor, SWM_S_COLOR_FOCUS_MAXIMIZED },
9337 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
9338 { "color_unfocus_maximized", setconfcolor, SWM_S_COLOR_UNFOCUS_MAXIMIZED },
9339 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
9340 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
9341 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
9342 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
9343 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
9344 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
9345 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
9346 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
9347 { "iconic_enabled", setconfvalue, SWM_S_ICONIC_ENABLED },
9348 { "java_workaround", setconfvalue, SWM_S_JAVA_WORKAROUND },
9349 { "keyboard_mapping", setkeymapping, 0 },
9350 { "layout", setlayout, 0 },
9351 { "maximize_hide_bar", setconfvalue, SWM_S_MAXIMIZE_HIDE_BAR },
9352 { "modkey", setconfmodkey, 0 },
9353 { "program", setconfspawn, 0 },
9354 { "quirk", setconfquirk, 0 },
9355 { "region", setconfregion, 0 },
9356 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
9357 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
9358 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
9359 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
9360 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
9361 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
9362 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
9363 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
9364 { "title_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED }, /* For backwards compat. */
9365 { "title_name_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED }, /* For backwards compat. */
9366 { "urgent_collapse", setconfvalue, SWM_S_URGENT_COLLAPSE },
9367 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
9368 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
9369 { "warp_focus", setconfvalue, SWM_S_WARP_FOCUS },
9370 { "warp_pointer", setconfvalue, SWM_S_WARP_POINTER },
9371 { "window_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED },
9372 { "window_instance_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED },
9373 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
9374 { "workspace_clamp", setconfvalue, SWM_S_WORKSPACE_CLAMP },
9375 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
9376 { "name", setconfvalue, SWM_S_WORKSPACE_NAME },
9377 };
9378
9379 void
9380 _add_startup_exception(const char *fmt, va_list ap)
9381 {
9382 if (vasprintf(&startup_exception, fmt, ap) == -1)
9383 warn("%s: asprintf", __func__);
9384 }
9385
9386 void
9387 add_startup_exception(const char *fmt, ...)
9388 {
9389 va_list ap;
9390
9391 nr_exceptions++;
9392
9393 if (startup_exception)
9394 return;
9395
9396 /* force bar to be enabled due to exception */
9397 bar_enabled = true;
9398
9399 va_start(ap, fmt);
9400 _add_startup_exception(fmt, ap);
9401 va_end(ap);
9402 }
9403
9404 int
9405 conf_load(const char *filename, int keymapping)
9406 {
9407 FILE *config;
9408 char *line = NULL, *cp, *ce, *optsub, *optval = NULL;
9409 size_t linelen, lineno = 0;
9410 int wordlen, i, optidx, count;
9411 struct config_option *opt = NULL;
9412
9413 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
9414
9415 if (filename == NULL) {
9416 warnx("conf_load: no filename");
9417 return (1);
9418 }
9419
9420 DNPRINTF(SWM_D_CONF, "conf_load: open %s\n", filename);
9421
9422 if ((config = fopen(filename, "r")) == NULL) {
9423 warn("conf_load: fopen: %s", filename);
9424 return (1);
9425 }
9426
9427 while (!feof(config)) {
9428 if (line)
9429 free(line);
9430
9431 if ((line = fparseln(config, &linelen, &lineno, NULL,
9432 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT)) == NULL) {
9433 if (ferror(config))
9434 err(1, "%s", filename);
9435 else
9436 continue;
9437 }
9438 cp = line;
9439 cp += strspn(cp, " \t\n"); /* eat whitespace */
9440 if (cp[0] == '\0') {
9441 /* empty line */
9442 continue;
9443 }
9444 /* get config option */
9445 wordlen = strcspn(cp, "=[ \t\n");
9446 if (wordlen == 0) {
9447 add_startup_exception("%s: line %zd: no option found",
9448 filename, lineno);
9449 continue;
9450 }
9451 optidx = -1;
9452 for (i = 0; i < LENGTH(configopt); i++) {
9453 opt = &configopt[i];
9454 if (strncasecmp(cp, opt->optname, wordlen) == 0 &&
9455 (int)strlen(opt->optname) == wordlen) {
9456 optidx = i;
9457 break;
9458 }
9459 }
9460 if (optidx == -1) {
9461 add_startup_exception("%s: line %zd: unknown option "
9462 "%.*s", filename, lineno, wordlen, cp);
9463 continue;
9464 }
9465 if (keymapping && opt && strcmp(opt->optname, "bind")) {
9466 add_startup_exception("%s: line %zd: invalid option "
9467 "%.*s", filename, lineno, wordlen, cp);
9468 continue;
9469 }
9470 cp += wordlen;
9471 cp += strspn(cp, " \t\n"); /* eat whitespace */
9472
9473 /* get [selector] if any */
9474 optsub = NULL;
9475 count = 0;
9476 if (*cp == '[') {
9477 ++count;
9478 /* Get length of selector. */
9479 for (ce = ++cp; *ce != '\0'; ++ce) {
9480 /* Find matching (not escaped) bracket. */
9481 if (*ce == ']' && *(ce - 1) != '\\') {
9482 --count;
9483 break;
9484 }
9485 }
9486
9487 if (count > 0) {
9488 add_startup_exception("%s: line %zd: syntax "
9489 "error: unterminated selector", filename,
9490 lineno);
9491 continue;
9492 }
9493
9494 /* ce points at ']'; terminate optsub. */
9495 *ce = '\0';
9496 optsub = cp;
9497 cp = ce + 1;
9498 }
9499 cp += strspn(cp, "= \t\n"); /* eat trailing */
9500 /* get RHS value */
9501 optval = cp;
9502 if (strlen(optval) == 0) {
9503 add_startup_exception("%s: line %zd: must supply value "
9504 "to %s", filename, lineno,
9505 configopt[optidx].optname);
9506 continue;
9507 }
9508 /* call function to deal with it all */
9509 if (configopt[optidx].func(optsub, optval,
9510 configopt[optidx].funcflags) != 0) {
9511 add_startup_exception("%s: line %zd: invalid data for "
9512 "%s", filename, lineno, configopt[optidx].optname);
9513 continue;
9514 }
9515 }
9516
9517 if (line)
9518 free(line);
9519 fclose(config);
9520
9521 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
9522
9523 return (0);
9524 }
9525
9526 void
9527 set_child_transient(struct ws_win *win, xcb_window_t *trans)
9528 {
9529 struct ws_win *parent, *w;
9530 struct swm_region *r;
9531 struct workspace *ws;
9532 xcb_icccm_wm_hints_t wmh;
9533
9534 parent = find_window(win->transient);
9535 if (parent)
9536 parent->focus_child = win;
9537 else {
9538 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
9539 " for %#x trans %#x\n", win->id, win->transient);
9540
9541 r = root_to_region(win->s->root, SWM_CK_ALL);
9542 ws = r->ws;
9543 /* parent doen't exist in our window list */
9544 TAILQ_FOREACH(w, &ws->winlist, entry) {
9545 if (xcb_icccm_get_wm_hints_reply(conn,
9546 xcb_icccm_get_wm_hints(conn, w->id),
9547 &wmh, NULL) != 1) {
9548 warnx("can't get hints for %#x", w->id);
9549 continue;
9550 }
9551
9552 if (win->hints.window_group != wmh.window_group)
9553 continue;
9554
9555 w->focus_child = win;
9556 win->transient = w->id;
9557 *trans = w->id;
9558 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
9559 "transient to %#x\n", win->transient);
9560 break;
9561 }
9562 }
9563 }
9564
9565 pid_t
9566 window_get_pid(xcb_window_t win)
9567 {
9568 pid_t ret = 0;
9569 const char *errstr;
9570 xcb_atom_t apid;
9571 xcb_get_property_cookie_t pc;
9572 xcb_get_property_reply_t *pr;
9573
9574 apid = get_atom_from_string("_NET_WM_PID");
9575 if (apid == XCB_ATOM_NONE)
9576 goto tryharder;
9577
9578 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
9579 pr = xcb_get_property_reply(conn, pc, NULL);
9580 if (pr == NULL)
9581 goto tryharder;
9582 if (pr->type != XCB_ATOM_CARDINAL) {
9583 free(pr);
9584 goto tryharder;
9585 }
9586
9587 if (pr->type == apid && pr->format == 32)
9588 ret = *((pid_t *)xcb_get_property_value(pr));
9589 free(pr);
9590
9591 return (ret);
9592
9593 tryharder:
9594 apid = get_atom_from_string("_SWM_PID");
9595 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
9596 0, SWM_PROPLEN);
9597 pr = xcb_get_property_reply(conn, pc, NULL);
9598 if (pr == NULL)
9599 return (0);
9600 if (pr->type != apid) {
9601 free(pr);
9602 return (0);
9603 }
9604
9605 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
9606 free(pr);
9607
9608 return (ret);
9609 }
9610
9611 int
9612 get_swm_ws(xcb_window_t id)
9613 {
9614 int ws_idx = -1;
9615 char *prop = NULL;
9616 size_t proplen;
9617 const char *errstr;
9618 xcb_get_property_reply_t *gpr;
9619
9620 gpr = xcb_get_property_reply(conn,
9621 xcb_get_property(conn, 0, id, a_swm_ws,
9622 XCB_ATOM_STRING, 0, SWM_PROPLEN),
9623 NULL);
9624 if (gpr == NULL)
9625 return (-1);
9626 if (gpr->type) {
9627 proplen = xcb_get_property_value_length(gpr);
9628 if (proplen > 0) {
9629 prop = malloc(proplen + 1);
9630 if (prop) {
9631 memcpy(prop,
9632 xcb_get_property_value(gpr),
9633 proplen);
9634 prop[proplen] = '\0';
9635 }
9636 }
9637 }
9638 free(gpr);
9639
9640 if (prop) {
9641 DNPRINTF(SWM_D_PROP, "get_swm_ws: _SWM_WS: %s\n", prop);
9642 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
9643 if (errstr) {
9644 DNPRINTF(SWM_D_PROP, "get_swm_ws: win #%s: %s",
9645 errstr, prop);
9646 }
9647 free(prop);
9648 }
9649
9650 return ws_idx;
9651 }
9652
9653 int
9654 get_ws_idx(struct ws_win *win)
9655 {
9656 xcb_get_property_reply_t *gpr;
9657 int ws_idx = -1;
9658
9659 if (win == NULL)
9660 return -1;
9661
9662 gpr = xcb_get_property_reply(conn,
9663 xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_DESKTOP].atom,
9664 XCB_ATOM_CARDINAL, 0, 1),
9665 NULL);
9666 if (gpr) {
9667 if (gpr->type == XCB_ATOM_CARDINAL && gpr->format == 32)
9668 ws_idx = *((int *)xcb_get_property_value(gpr));
9669 free(gpr);
9670 }
9671
9672 if (ws_idx == -1 && !(win->quirks & SWM_Q_IGNORESPAWNWS))
9673 ws_idx = get_swm_ws(win->id);
9674
9675 if (ws_idx > workspace_limit - 1 || ws_idx < -1)
9676 ws_idx = -1;
9677
9678 DNPRINTF(SWM_D_PROP, "get_ws_idx: win %#x, ws_idx: %d\n", win->id,
9679 ws_idx);
9680
9681 return ws_idx;
9682 }
9683
9684 void
9685 reparent_window(struct ws_win *win)
9686 {
9687 xcb_void_cookie_t c;
9688 xcb_generic_error_t *error;
9689 uint32_t wa[2];
9690
9691 win->frame = xcb_generate_id(conn);
9692
9693 DNPRINTF(SWM_D_MISC, "reparent_window: win %#x, frame: %#x\n",
9694 win->id, win->frame);
9695
9696 wa[0] =
9697 XCB_EVENT_MASK_ENTER_WINDOW |
9698 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
9699 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
9700 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
9701 XCB_EVENT_MASK_EXPOSURE;
9702 #ifdef SWM_DEBUG
9703 wa[0] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
9704 #endif
9705
9706 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win->frame, win->s->root,
9707 X(win), Y(win), WIDTH(win), HEIGHT(win), 0,
9708 XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
9709 XCB_CW_EVENT_MASK, wa);
9710
9711 win->state = SWM_WIN_STATE_REPARENTING;
9712 c = xcb_reparent_window_checked(conn, win->id, win->frame, 0, 0);
9713 if ((error = xcb_request_check(conn, c))) {
9714 DNPRINTF(SWM_D_MISC, "reparent_window: error:\n");
9715 event_error(error);
9716 free(error);
9717
9718 /* Abort. */
9719 xcb_destroy_window(conn, win->frame);
9720 win->frame = XCB_WINDOW_NONE;
9721 } else {
9722 xcb_change_save_set(conn, XCB_SET_MODE_INSERT, win->id);
9723 }
9724 DNPRINTF(SWM_D_MISC, "reparent_window: done.\n");
9725 }
9726
9727 void
9728 unparent_window(struct ws_win *win)
9729 {
9730 xcb_change_save_set(conn, XCB_SET_MODE_DELETE, win->id);
9731 xcb_reparent_window(conn, win->id, win->s->root, X(win), Y(win));
9732 xcb_destroy_window(conn, win->frame);
9733 win->frame = XCB_WINDOW_NONE;
9734 win->state = SWM_WIN_STATE_UNPARENTING;
9735 }
9736
9737 struct ws_win *
9738 manage_window(xcb_window_t id, int spawn_pos, bool mapping)
9739 {
9740 struct ws_win *win = NULL, *ww;
9741 struct swm_region *r;
9742 struct pid_e *p;
9743 struct quirk *qp;
9744 xcb_get_geometry_reply_t *gr;
9745 xcb_get_window_attributes_reply_t *war = NULL;
9746 xcb_window_t trans = XCB_WINDOW_NONE;
9747 uint32_t i, wa[1], new_flags;
9748 int ws_idx, force_ws = -1;
9749 char *class, *instance, *name;
9750
9751 if (find_bar(id)) {
9752 DNPRINTF(SWM_D_MISC, "manage_window: win %#x is region bar; "
9753 "skipping.\n", id);
9754 goto out;
9755 }
9756
9757 if (find_region(id)) {
9758 DNPRINTF(SWM_D_MISC, "manage_window: win %#x is region window; "
9759 "skipping.\n", id);
9760 goto out;
9761 }
9762
9763 if ((win = find_window(id)) != NULL) {
9764 DNPRINTF(SWM_D_MISC, "manage_window: win %#x already "
9765 "managed; skipping.)\n", id);
9766 return (win); /* Already managed. */
9767 }
9768
9769 /* See if window is on the unmanaged list. */
9770 if ((win = find_unmanaged_window(id)) != NULL) {
9771 DNPRINTF(SWM_D_MISC, "manage_window: win %#x found on "
9772 "unmanaged list.\n", id);
9773 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
9774
9775 if (TRANS(win))
9776 set_child_transient(win, &trans);
9777
9778 goto remanage;
9779 } else {
9780 DNPRINTF(SWM_D_MISC, "manage_window: win %#x is new.\n", id);
9781 }
9782
9783 war = xcb_get_window_attributes_reply(conn,
9784 xcb_get_window_attributes(conn, id), NULL);
9785 if (war == NULL) {
9786 DNPRINTF(SWM_D_EVENT, "manage_window: window lost.\n");
9787 goto out;
9788 }
9789
9790 if (war->override_redirect) {
9791 DNPRINTF(SWM_D_EVENT, "manage_window: override_redirect; "
9792 "skipping.\n");
9793 goto out;
9794 }
9795
9796 if (!mapping && war->map_state == XCB_MAP_STATE_UNMAPPED &&
9797 get_win_state(id) == XCB_ICCCM_WM_STATE_WITHDRAWN) {
9798 DNPRINTF(SWM_D_EVENT, "manage_window: window withdrawn; "
9799 "skipping.\n");
9800 goto out;
9801 }
9802
9803 /* Try to get initial window geometry. */
9804 gr = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, id), NULL);
9805 if (gr == NULL) {
9806 DNPRINTF(SWM_D_MISC, "manage_window: get geometry failed.\n");
9807 goto out;
9808 }
9809
9810 /* Create and initialize ws_win object. */
9811 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
9812 err(1, "manage_window: calloc: failed to allocate memory for "
9813 "new window");
9814
9815 win->id = id;
9816
9817 /* Figureout which region the window belongs to. */
9818 r = root_to_region(gr->root, SWM_CK_ALL);
9819
9820 /* Ignore window border if there is one. */
9821 WIDTH(win) = gr->width;
9822 HEIGHT(win) = gr->height;
9823 X(win) = gr->x + gr->border_width;
9824 Y(win) = gr->y + gr->border_width;
9825 win->bordered = false;
9826 win->mapped = (war->map_state != XCB_MAP_STATE_UNMAPPED);
9827 win->s = r->s; /* this never changes */
9828
9829 free(gr);
9830
9831 /* Select which X events to monitor and set border pixel color. */
9832 wa[0] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
9833 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
9834
9835 xcb_change_window_attributes(conn, win->id, XCB_CW_EVENT_MASK, wa);
9836
9837 /* Get WM_SIZE_HINTS. */
9838 xcb_icccm_get_wm_normal_hints_reply(conn,
9839 xcb_icccm_get_wm_normal_hints(conn, win->id),
9840 &win->sh, NULL);
9841
9842 /* Get WM_HINTS. */
9843 xcb_icccm_get_wm_hints_reply(conn,
9844 xcb_icccm_get_wm_hints(conn, win->id),
9845 &win->hints, NULL);
9846
9847 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
9848 xcb_icccm_get_wm_transient_for_reply(conn,
9849 xcb_icccm_get_wm_transient_for(conn, win->id),
9850 &trans, NULL);
9851 if (trans) {
9852 win->transient = trans;
9853 set_child_transient(win, &win->transient);
9854 }
9855
9856 /* Get WM_PROTOCOLS. */
9857 get_wm_protocols(win);
9858
9859 #ifdef SWM_DEBUG
9860 /* Must be after getting WM_HINTS and WM_PROTOCOLS. */
9861 DNPRINTF(SWM_D_FOCUS, "manage_window: input_model: %s\n",
9862 get_win_input_model(win));
9863 #endif
9864
9865 /* Set initial quirks based on EWMH. */
9866 ewmh_autoquirk(win);
9867
9868 /* Determine initial quirks. */
9869 xcb_icccm_get_wm_class_reply(conn,
9870 xcb_icccm_get_wm_class(conn, win->id),
9871 &win->ch, NULL);
9872
9873 class = win->ch.class_name ? win->ch.class_name : "";
9874 instance = win->ch.instance_name ? win->ch.instance_name : "";
9875 name = get_win_name(win->id);
9876
9877 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, instance: %s, "
9878 "name: %s\n", class, instance, name);
9879
9880 /* java is retarded so treat it special */
9881 if (strstr(instance, "sun-awt")) {
9882 DNPRINTF(SWM_D_CLASS, "manage_window: java window detected.\n");
9883 win->java = true;
9884 }
9885
9886 TAILQ_FOREACH(qp, &quirks, entry) {
9887 if (regexec(&qp->regex_class, class, 0, NULL, 0) == 0 &&
9888 regexec(&qp->regex_instance, instance, 0, NULL, 0) == 0 &&
9889 regexec(&qp->regex_name, name, 0, NULL, 0) == 0) {
9890 DNPRINTF(SWM_D_CLASS, "manage_window: matched "
9891 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp->class,
9892 qp->instance, qp->name, qp->quirk, qp->ws);
9893 win->quirks = qp->quirk;
9894 if (qp->ws >= 0 && qp->ws < workspace_limit)
9895 force_ws = qp->ws;
9896 }
9897 }
9898
9899 free(name);
9900
9901 /* Reset font sizes (the bruteforce way; no default keybinding). */
9902 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
9903 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
9904 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
9905 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
9906 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
9907 }
9908
9909 /* Figure out which workspace the window belongs to. */
9910 if (!(win->quirks & SWM_Q_IGNOREPID) &&
9911 (p = find_pid(window_get_pid(win->id))) != NULL) {
9912 win->ws = &r->s->ws[p->ws];
9913 TAILQ_REMOVE(&pidlist, p, entry);
9914 free(p);
9915 p = NULL;
9916 } else if ((ws_idx = get_ws_idx(win)) != -1 &&
9917 !TRANS(win)) {
9918 /* _SWM_WS is set; use that. */
9919 win->ws = &r->s->ws[ws_idx];
9920 } else if (trans && (ww = find_window(trans)) != NULL) {
9921 /* Launch transients in the same ws as parent. */
9922 win->ws = ww->ws;
9923 } else {
9924 win->ws = r->ws;
9925 }
9926
9927 if (force_ws != -1)
9928 win->ws = &r->s->ws[force_ws];
9929
9930 /* Set the _NET_WM_DESKTOP atom. */
9931 DNPRINTF(SWM_D_PROP, "manage_window: set _NET_WM_DESKTOP: %d\n",
9932 win->ws->idx);
9933 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
9934 ewmh[_NET_WM_DESKTOP].atom, XCB_ATOM_CARDINAL, 32, 1, &win->ws->idx);
9935
9936 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
9937 xcb_delete_property(conn, win->id, a_swm_ws);
9938
9939 /* WS must already be set for this to work. */
9940 store_float_geom(win);
9941
9942 /* Make sure window is positioned inside its region, if its active. */
9943 if (win->ws->r) {
9944 region_containment(win, r, SWM_CW_ALLSIDES |
9945 SWM_CW_HARDBOUNDARY);
9946 update_window(win);
9947 }
9948
9949 remanage:
9950 /* Figure out where to insert the window in the workspace list. */
9951 if (trans && (ww = find_window(trans)))
9952 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
9953 else if (win->ws->focus && spawn_pos == SWM_STACK_ABOVE)
9954 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
9955 entry);
9956 else if (win->ws->focus && spawn_pos == SWM_STACK_BELOW)
9957 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
9958 else switch (spawn_pos) {
9959 default:
9960 case SWM_STACK_TOP:
9961 case SWM_STACK_ABOVE:
9962 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
9963 break;
9964 case SWM_STACK_BOTTOM:
9965 case SWM_STACK_BELOW:
9966 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
9967 }
9968
9969 ewmh_update_client_list();
9970
9971 TAILQ_INSERT_HEAD(&win->ws->stack, win, stack_entry);
9972 lower_window(win);
9973
9974 /* Get/apply initial _NET_WM_STATE */
9975 ewmh_get_wm_state(win);
9976
9977 /* Apply quirks. */
9978 new_flags = win->ewmh_flags;
9979
9980 if (win->quirks & SWM_Q_FLOAT)
9981 new_flags |= EWMH_F_ABOVE;
9982
9983 if (win->quirks & SWM_Q_ANYWHERE)
9984 new_flags |= SWM_F_MANUAL;
9985
9986 ewmh_apply_flags(win, new_flags);
9987 ewmh_update_wm_state(win);
9988
9989 /* Set initial _NET_WM_ALLOWED_ACTIONS */
9990 ewmh_update_actions(win);
9991
9992 reparent_window(win);
9993
9994 DNPRINTF(SWM_D_MISC, "manage_window: done. win %#x, (x,y) w x h: "
9995 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win->id,
9996 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
9997 YESNO(ICONIC(win)), win->transient);
9998 out:
9999 free(war);
10000 return (win);
10001 }
10002
10003 void
10004 free_window(struct ws_win *win)
10005 {
10006 DNPRINTF(SWM_D_MISC, "free_window: win %#x\n", WINID(win));
10007
10008 if (win == NULL)
10009 return;
10010
10011 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
10012
10013 /* paint memory */
10014 memset(win, 0xff, sizeof *win); /* XXX kill later */
10015
10016 free(win);
10017 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
10018 }
10019
10020 void
10021 unmanage_window(struct ws_win *win)
10022 {
10023 DNPRINTF(SWM_D_MISC, "unmanage_window: win %#x\n", WINID(win));
10024
10025 if (win == NULL)
10026 return;
10027
10028 kill_refs(win);
10029 unparent_window(win);
10030
10031 TAILQ_REMOVE(&win->ws->stack, win, stack_entry);
10032 TAILQ_REMOVE(&win->ws->winlist, win, entry);
10033 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
10034
10035 ewmh_update_client_list();
10036 }
10037
10038 void
10039 expose(xcb_expose_event_t *e)
10040 {
10041 struct ws_win *w;
10042 struct swm_bar *b;
10043 #ifdef SWM_DEBUG
10044 struct workspace *ws;
10045 #endif
10046
10047 DNPRINTF(SWM_D_EVENT, "expose: win %#x, count: %d\n", e->window,
10048 e->count);
10049
10050 if (e->count > 0)
10051 return;
10052
10053 if ((b = find_bar(e->window))) {
10054 bar_draw(b);
10055 xcb_flush(conn);
10056 } else if ((w = find_window(e->window)) && w->frame == e->window) {
10057 draw_frame(w);
10058 #ifdef SWM_DEBUG
10059 ws = w->ws;
10060 TAILQ_FOREACH(w, &ws->winlist, entry)
10061 debug_refresh(w);
10062 #endif
10063 xcb_flush(conn);
10064 }
10065
10066 DNPRINTF(SWM_D_EVENT, "expose: done\n");
10067 }
10068
10069 void
10070 focusin(xcb_focus_in_event_t *e)
10071 {
10072 struct ws_win *win;
10073
10074 DNPRINTF(SWM_D_EVENT, "focusin: win %#x, mode: %s(%u), "
10075 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
10076 e->mode, get_notify_detail_label(e->detail), e->detail);
10077 if ((win = find_window(e->event)) && win != win->ws->focus &&
10078 win != win->ws->focus_pending &&
10079 e->mode == XCB_NOTIFY_MODE_NORMAL) {
10080 win->ws->focus_prev = win->ws->focus;
10081 win->ws->focus = win;
10082 win->ws->focus_pending = NULL;
10083
10084 if (win->ws->focus_prev)
10085 draw_frame(win->ws->focus_prev);
10086 draw_frame(win);
10087 raise_window(win);
10088 }
10089 }
10090
10091 #ifdef SWM_DEBUG
10092 void
10093 focusout(xcb_focus_out_event_t *e)
10094 {
10095 DNPRINTF(SWM_D_EVENT, "focusout: win %#x, mode: %s(%u), "
10096 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
10097 e->mode, get_notify_detail_label(e->detail), e->detail);
10098 }
10099 #endif
10100
10101 void
10102 keypress(xcb_key_press_event_t *e)
10103 {
10104 struct action *ap;
10105 struct binding *bp;
10106 xcb_keysym_t keysym;
10107 bool replay = true;
10108
10109 last_event_time = e->time;
10110
10111 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
10112
10113 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
10114 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10115 "state: %u, cleaned: %u, same_screen: %s\n", keysym, e->event,
10116 e->event_x, e->event_y, e->detail, e->time, e->root, e->root_x,
10117 e->root_y, e->child, e->state, CLEANMASK(e->state),
10118 YESNO(e->same_screen));
10119
10120 bp = binding_lookup(CLEANMASK(e->state), KEYBIND, keysym);
10121 if (bp == NULL) {
10122 /* Look for catch-all. */
10123 if ((bp = binding_lookup(ANYMOD, KEYBIND, keysym)) == NULL)
10124 goto out;
10125 }
10126
10127 replay = bp->flags & BINDING_F_REPLAY;
10128
10129 if ((ap = &actions[bp->action]) == NULL)
10130 goto out;
10131
10132 if (bp->action == FN_SPAWN_CUSTOM)
10133 spawn_custom(root_to_region(e->root, SWM_CK_ALL), &ap->args,
10134 bp->spawn_name);
10135 else if (ap->func)
10136 ap->func(bp, root_to_region(e->root, SWM_CK_ALL), &ap->args);
10137
10138 replay = replay && !(ap->flags & FN_F_NOREPLAY);
10139
10140 out:
10141 if (replay) {
10142 DNPRINTF(SWM_D_EVENT, "keypress: replaying.\n");
10143 /* Pass keypress to event window and unfreeze keyboard queue. */
10144 xcb_allow_events(conn, XCB_ALLOW_REPLAY_KEYBOARD, e->time);
10145 } else {
10146 /* Release freeze. */
10147 xcb_allow_events(conn, XCB_ALLOW_SYNC_KEYBOARD, e->time);
10148 }
10149 xcb_flush(conn);
10150
10151 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
10152 }
10153
10154 void
10155 keyrelease(xcb_key_release_event_t *e)
10156 {
10157 struct action *ap;
10158 struct binding *bp;
10159 xcb_keysym_t keysym;
10160
10161 last_event_time = e->time;
10162
10163 keysym = xcb_key_release_lookup_keysym(syms, e, 0);
10164
10165 DNPRINTF(SWM_D_EVENT, "keyrelease: keysym: %u, win (x,y): %#x (%d,%d), "
10166 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10167 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
10168 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
10169 e->child, e->state, YESNO(e->same_screen));
10170
10171 bp = binding_lookup(CLEANMASK(e->state), KEYBIND, keysym);
10172 if (bp == NULL)
10173 /* Look for catch-all. */
10174 bp = binding_lookup(ANYMOD, KEYBIND, keysym);
10175
10176 if (bp && (ap = &actions[bp->action]) && !(ap->flags & FN_F_NOREPLAY) &&
10177 bp->flags & BINDING_F_REPLAY) {
10178 xcb_allow_events(conn, XCB_ALLOW_REPLAY_KEYBOARD, e->time);
10179 DNPRINTF(SWM_D_EVENT, "keyrelease: replaying.\n");
10180 } else {
10181 xcb_allow_events(conn, XCB_ALLOW_SYNC_KEYBOARD, e->time);
10182 DNPRINTF(SWM_D_EVENT, "keyrelease: unfreezing.\n");
10183 }
10184
10185 xcb_flush(conn);
10186
10187 DNPRINTF(SWM_D_EVENT, "keyrelease: done.\n");
10188 }
10189
10190 void
10191 buttonpress(xcb_button_press_event_t *e)
10192 {
10193 struct ws_win *win = NULL, *newf;
10194 struct swm_region *r, *old_r;
10195 struct action *ap;
10196 struct binding *bp;
10197 bool replay = true;
10198
10199 last_event_time = e->time;
10200
10201 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): %#x (%d,%d), "
10202 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10203 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
10204 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
10205 e->state, YESNO(e->same_screen));
10206
10207 if (e->event == e->root) {
10208 if (e->child) {
10209 win = find_window(e->child);
10210 } else {
10211 /* Focus on empty region */
10212 /* If no windows on region if its empty. */
10213 r = root_to_region(e->root, SWM_CK_POINTER);
10214 if (r && TAILQ_EMPTY(&r->ws->winlist)) {
10215 old_r = root_to_region(e->root, SWM_CK_FOCUS);
10216 if (old_r && old_r != r)
10217 unfocus_win(old_r->ws->focus);
10218
10219 DNPRINTF(SWM_D_FOCUS, "buttonpress: "
10220 "set_input_focus: %#x, revert-to: parent, "
10221 "time: %#x\n", e->root, e->time);
10222 xcb_set_input_focus(conn,
10223 XCB_INPUT_FOCUS_POINTER_ROOT,
10224 e->root, e->time);
10225
10226 /* Clear bar since empty. */
10227 bar_draw(r->bar);
10228
10229 /* No need to replay event. */
10230 replay = false;
10231 }
10232 }
10233 } else {
10234 win = find_window(e->event);
10235 }
10236
10237 if (win) {
10238 newf = get_focus_magic(win);
10239 if (win->ws->focus == newf && newf != win) {
10240 if (win->focus_child == win)
10241 win->focus_child = NULL;
10242 newf = win;
10243 }
10244 focus_win(newf);
10245 }
10246
10247 /* Handle any bound action. */
10248 bp = binding_lookup(CLEANMASK(e->state), BTNBIND, e->detail);
10249 if (bp == NULL) {
10250 /* Look for catch-all. */
10251 if ((bp = binding_lookup(ANYMOD, BTNBIND, e->detail)) == NULL)
10252 goto out;
10253
10254 }
10255
10256 replay = bp->flags & BINDING_F_REPLAY;
10257
10258 if ((ap = &actions[bp->action]) == NULL)
10259 goto out;
10260
10261 if (bp->action == FN_SPAWN_CUSTOM)
10262 spawn_custom(root_to_region(e->root, SWM_CK_ALL), &ap->args,
10263 bp->spawn_name);
10264 else if (ap->func)
10265 ap->func(bp, root_to_region(e->root, SWM_CK_ALL), &ap->args);
10266
10267 replay = replay && !(ap->flags & FN_F_NOREPLAY);
10268
10269 out:
10270 if (replay) {
10271 DNPRINTF(SWM_D_EVENT, "buttonpress: replaying.\n");
10272 /* Replay event to event window */
10273 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
10274 } else {
10275 /* Unfreeze grab events. */
10276 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
10277 }
10278
10279 focus_flush();
10280 }
10281
10282 void
10283 buttonrelease(xcb_button_release_event_t *e)
10284 {
10285 struct action *ap;
10286 struct binding *bp;
10287
10288 last_event_time = e->time;
10289
10290 DNPRINTF(SWM_D_EVENT, "buttonrelease: win (x,y): %#x (%d,%d), "
10291 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10292 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
10293 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
10294 e->state, YESNO(e->same_screen));
10295
10296 bp = binding_lookup(CLEANMASK(e->state), BTNBIND, e->detail);
10297 if (bp == NULL)
10298 /* Look for catch-all. */
10299 bp = binding_lookup(ANYMOD, BTNBIND, e->detail);
10300
10301 if (bp && (ap = &actions[bp->action]) && !(ap->flags & FN_F_NOREPLAY) &&
10302 bp->flags & BINDING_F_REPLAY) {
10303 DNPRINTF(SWM_D_EVENT, "buttonrelease: replaying.\n");
10304 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
10305 } else {
10306 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
10307 }
10308
10309 xcb_flush(conn);
10310 }
10311
10312 #ifdef SWM_DEBUG
10313 char *
10314 get_win_input_model(struct ws_win *win)
10315 {
10316 char *inputmodel;
10317 /*
10318 * Input Model Input Field WM_TAKE_FOCUS
10319 * No Input False Absent
10320 * Passive True Absent
10321 * Locally Active True Present
10322 * Globally Active False Present
10323 */
10324
10325 if (ACCEPTS_FOCUS(win))
10326 inputmodel = (win->take_focus) ? "Locally Active" : "Passive";
10327 else
10328 inputmodel = (win->take_focus) ? "Globally Active" : "No Input";
10329
10330 return inputmodel;
10331 }
10332
10333 void
10334 print_win_geom(xcb_window_t w)
10335 {
10336 xcb_get_geometry_reply_t *wa;
10337
10338 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
10339 if (wa == NULL) {
10340 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: %#x\n",
10341 w);
10342 return;
10343 }
10344
10345 DNPRINTF(SWM_D_MISC, "print_win_geom: win %#x, root: %#x, "
10346 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
10347 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
10348 wa->border_width);
10349
10350 free(wa);
10351 }
10352 #endif
10353
10354 #ifdef SWM_DEBUG
10355 char *
10356 get_stack_mode_name(uint8_t mode)
10357 {
10358 char *name;
10359
10360 switch(mode) {
10361 case XCB_STACK_MODE_ABOVE:
10362 name = "Above";
10363 break;
10364 case XCB_STACK_MODE_BELOW:
10365 name = "Below";
10366 break;
10367 case XCB_STACK_MODE_TOP_IF:
10368 name = "TopIf";
10369 break;
10370 case XCB_STACK_MODE_BOTTOM_IF:
10371 name = "BottomIf";
10372 break;
10373 case XCB_STACK_MODE_OPPOSITE:
10374 name = "Opposite";
10375 break;
10376 default:
10377 name = "Unknown";
10378 }
10379
10380 return name;
10381 }
10382 #endif
10383
10384 void
10385 configurerequest(xcb_configure_request_event_t *e)
10386 {
10387 struct ws_win *win;
10388 struct swm_region *r = NULL;
10389 int i = 0;
10390 uint32_t wc[7] = {0};
10391 uint16_t mask = 0;
10392 bool new = false;
10393
10394 if ((win = find_window(e->window)) == NULL)
10395 if ((win = find_unmanaged_window(e->window)) == NULL)
10396 new = true;
10397
10398 #ifdef SWM_DEBUG
10399 if (swm_debug & SWM_D_EVENT) {
10400 print_win_geom(e->window);
10401
10402 DNPRINTF(SWM_D_EVENT, "configurerequest: win %#x, "
10403 "parent: %#x, new: %s, value_mask: %u { ", e->window,
10404 e->parent, YESNO(new), e->value_mask);
10405 if (e->value_mask & XCB_CONFIG_WINDOW_X)
10406 DPRINTF("X: %d ", e->x);
10407 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
10408 DPRINTF("Y: %d ", e->y);
10409 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
10410 DPRINTF("W: %u ", e->width);
10411 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
10412 DPRINTF("H: %u ", e->height);
10413 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
10414 DPRINTF("Border: %u ", e->border_width);
10415 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
10416 DPRINTF("Sibling: %#x ", e->sibling);
10417 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
10418 DPRINTF("StackMode: %s(%u) ",
10419 get_stack_mode_name(e->stack_mode), e->stack_mode);
10420 DPRINTF("}\n");
10421 }
10422 #endif
10423
10424 if (new) {
10425 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
10426 mask |= XCB_CONFIG_WINDOW_X;
10427 wc[i++] = e->x;
10428 }
10429 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
10430 mask |= XCB_CONFIG_WINDOW_Y;
10431 wc[i++] = e->y;
10432 }
10433 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
10434 mask |= XCB_CONFIG_WINDOW_WIDTH;
10435 wc[i++] = e->width;
10436 }
10437 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
10438 mask |= XCB_CONFIG_WINDOW_HEIGHT;
10439 wc[i++] = e->height;
10440 }
10441 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
10442 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
10443 wc[i++] = e->border_width;
10444 }
10445 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
10446 mask |= XCB_CONFIG_WINDOW_SIBLING;
10447 wc[i++] = e->sibling;
10448 }
10449 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
10450 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
10451 wc[i++] = e->stack_mode;
10452 }
10453
10454 if (mask != 0) {
10455 xcb_configure_window(conn, e->window, mask, wc);
10456 xcb_flush(conn);
10457 }
10458 } else if ((!MANUAL(win) || win->quirks & SWM_Q_ANYWHERE) &&
10459 !FULLSCREEN(win) && !MAXIMIZED(win)) {
10460 if (win->ws->r)
10461 r = win->ws->r;
10462 else if (win->ws->old_r)
10463 r = win->ws->old_r;
10464
10465 /* windows are centered unless ANYWHERE quirk is set. */
10466 if (win->quirks & SWM_Q_ANYWHERE) {
10467 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
10468 win->g_float.x = e->x;
10469 if (r)
10470 win->g_float.x -= X(r);
10471 }
10472
10473 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
10474 win->g_float.y = e->y;
10475 if (r)
10476 win->g_float.y -= Y(r);
10477 }
10478 }
10479
10480 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
10481 win->g_float.w = e->width;
10482
10483 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
10484 win->g_float.h = e->height;
10485
10486 win->g_floatvalid = true;
10487
10488 if (!MAXIMIZED(win) && !FULLSCREEN(win) &&
10489 (TRANS(win) || (ABOVE(win) &&
10490 win->ws->cur_layout != &layouts[SWM_MAX_STACK]))) {
10491 WIDTH(win) = win->g_float.w;
10492 HEIGHT(win) = win->g_float.h;
10493
10494 if (r != NULL) {
10495 update_floater(win);
10496 focus_flush();
10497 } else {
10498 config_win(win, e);
10499 xcb_flush(conn);
10500 }
10501 } else {
10502 config_win(win, e);
10503 xcb_flush(conn);
10504 }
10505 } else {
10506 config_win(win, e);
10507 xcb_flush(conn);
10508 }
10509
10510 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
10511 }
10512
10513 void
10514 configurenotify(xcb_configure_notify_event_t *e)
10515 {
10516 struct ws_win *win;
10517
10518 DNPRINTF(SWM_D_EVENT, "configurenotify: win %#x, event win: %#x, "
10519 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
10520 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
10521 e->width, e->height, e->border_width, e->above_sibling,
10522 YESNO(e->override_redirect));
10523
10524 win = find_window(e->window);
10525 if (win) {
10526 adjust_font(win);
10527 if (font_adjusted && win->ws->r) {
10528 stack(win->ws->r);
10529 xcb_flush(conn);
10530 }
10531 }
10532 }
10533
10534 void
10535 destroynotify(xcb_destroy_notify_event_t *e)
10536 {
10537 struct ws_win *win;
10538 struct workspace *ws;
10539
10540 DNPRINTF(SWM_D_EVENT, "destroynotify: win %#x\n", e->window);
10541
10542 if ((win = find_window(e->window)) == NULL) {
10543 if ((win = find_unmanaged_window(e->window)) == NULL)
10544 goto out;
10545 /* Window is on unmanaged list. */
10546 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
10547 free_window(win);
10548 goto out;
10549 }
10550
10551 ws = win->ws;
10552
10553 if (win->frame == e->window) {
10554 DNPRINTF(SWM_D_EVENT, "destroynotify: frame for win %#x\n",
10555 win->id);
10556 win->frame = XCB_WINDOW_NONE;
10557 goto out;
10558 }
10559
10560 if (focus_mode != SWM_FOCUS_FOLLOW) {
10561 /* If we were focused, make sure we focus on something else. */
10562 if (win == ws->focus) {
10563 ws->focus_pending = get_focus_prev(win);
10564 if (ws->focus_pending == win)
10565 ws->focus_pending = NULL;
10566 }
10567 }
10568
10569 unmanage_window(win);
10570 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
10571 free_window(win);
10572 stack(ws->r);
10573
10574 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(ws)) {
10575 if (ws->focus_pending) {
10576 focus_win(ws->focus_pending);
10577 ws->focus_pending = NULL;
10578 } else if (ws->focus == NULL) {
10579 DNPRINTF(SWM_D_FOCUS, "destroynotify: set_input_focus: "
10580 "%#x, revert-to: parent, time: 0\n", ws->r->id);
10581 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
10582 ws->r->id, XCB_CURRENT_TIME);
10583 }
10584
10585 focus_flush();
10586 }
10587
10588 out:
10589 DNPRINTF(SWM_D_EVENT, "destroynotify: done.\n");
10590 }
10591
10592 #ifdef SWM_DEBUG
10593 char *
10594 get_notify_detail_label(uint8_t detail)
10595 {
10596 char *label;
10597
10598 switch (detail) {
10599 case XCB_NOTIFY_DETAIL_ANCESTOR:
10600 label = "Ancestor";
10601 break;
10602 case XCB_NOTIFY_DETAIL_VIRTUAL:
10603 label = "Virtual";
10604 break;
10605 case XCB_NOTIFY_DETAIL_INFERIOR:
10606 label = "Inferior";
10607 break;
10608 case XCB_NOTIFY_DETAIL_NONLINEAR:
10609 label = "Nonlinear";
10610 break;
10611 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
10612 label = "NonlinearVirtual";
10613 break;
10614 case XCB_NOTIFY_DETAIL_POINTER:
10615 label = "Pointer";
10616 break;
10617 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
10618 label = "PointerRoot";
10619 break;
10620 case XCB_NOTIFY_DETAIL_NONE:
10621 label = "None";
10622 break;
10623 default:
10624 label = "Unknown";
10625 }
10626
10627 return label;
10628 }
10629
10630 char *
10631 get_notify_mode_label(uint8_t mode)
10632 {
10633 char *label;
10634
10635 switch (mode) {
10636 case XCB_NOTIFY_MODE_NORMAL:
10637 label = "Normal";
10638 break;
10639 case XCB_NOTIFY_MODE_GRAB:
10640 label = "Grab";
10641 break;
10642 case XCB_NOTIFY_MODE_UNGRAB:
10643 label = "Ungrab";
10644 break;
10645 case XCB_NOTIFY_MODE_WHILE_GRABBED:
10646 label = "WhileGrabbed";
10647 break;
10648 default:
10649 label = "Unknown";
10650 }
10651
10652 return label;
10653 }
10654
10655 char *
10656 get_state_mask_label(uint16_t state)
10657 {
10658 char *label;
10659
10660 switch (state) {
10661 case XCB_KEY_BUT_MASK_SHIFT:
10662 label = "ShiftMask";
10663 break;
10664 case XCB_KEY_BUT_MASK_LOCK:
10665 label = "LockMask";
10666 break;
10667 case XCB_KEY_BUT_MASK_CONTROL:
10668 label = "ControlMask";
10669 break;
10670 case XCB_KEY_BUT_MASK_MOD_1:
10671 label = "Mod1Mask";
10672 break;
10673 case XCB_KEY_BUT_MASK_MOD_2:
10674 label = "Mod2Mask";
10675 break;
10676 case XCB_KEY_BUT_MASK_MOD_3:
10677 label = "Mod3Mask";
10678 break;
10679 case XCB_KEY_BUT_MASK_MOD_4:
10680 label = "Mod4Mask";
10681 break;
10682 case XCB_KEY_BUT_MASK_MOD_5:
10683 label = "Mod5Mask";
10684 break;
10685 case XCB_KEY_BUT_MASK_BUTTON_1:
10686 label = "Button1Mask";
10687 break;
10688 case XCB_KEY_BUT_MASK_BUTTON_2:
10689 label = "Button2Mask";
10690 break;
10691 case XCB_KEY_BUT_MASK_BUTTON_3:
10692 label = "Button3Mask";
10693 break;
10694 case XCB_KEY_BUT_MASK_BUTTON_4:
10695 label = "Button4Mask";
10696 break;
10697 case XCB_KEY_BUT_MASK_BUTTON_5:
10698 label = "Button5Mask";
10699 break;
10700 case 0:
10701 label = "None";
10702 break;
10703 default:
10704 label = "Unknown";
10705 }
10706
10707 return label;
10708 }
10709 #endif
10710
10711 void
10712 enternotify(xcb_enter_notify_event_t *e)
10713 {
10714 struct ws_win *win;
10715 struct swm_region *r;
10716
10717 last_event_time = e->time;
10718
10719 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): %#x "
10720 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10721 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10722 e->time, e->event, e->event_x, e->event_y,
10723 get_notify_mode_label(e->mode), e->mode,
10724 get_notify_detail_label(e->detail), e->detail,
10725 e->root, e->root_x, e->root_y, e->child,
10726 YESNO(e->same_screen_focus), get_state_mask_label(e->state),
10727 e->state);
10728
10729 if (e->event == e->root && e->child == XCB_WINDOW_NONE &&
10730 e->mode == XCB_NOTIFY_MODE_GRAB &&
10731 e->detail == XCB_NOTIFY_DETAIL_INFERIOR) {
10732 DNPRINTF(SWM_D_EVENT, "enternotify: grab inferior; ignoring.\n");
10733 return;
10734 }
10735
10736 if (focus_mode == SWM_FOCUS_MANUAL &&
10737 e->mode == XCB_NOTIFY_MODE_NORMAL) {
10738 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
10739 return;
10740 }
10741
10742 if (focus_mode != SWM_FOCUS_FOLLOW &&
10743 e->mode == XCB_NOTIFY_MODE_UNGRAB &&
10744 e->detail != XCB_NOTIFY_DETAIL_ANCESTOR) {
10745 DNPRINTF(SWM_D_EVENT, "enternotify: ungrab; ignoring.\n");
10746 return;
10747 }
10748
10749 if ((win = find_window(e->event)) == NULL) {
10750 if (e->event == e->root) {
10751 /* If no windows on pointer region, then focus root. */
10752 r = root_to_region(e->root, SWM_CK_POINTER);
10753 if (r == NULL) {
10754 DNPRINTF(SWM_D_EVENT, "enternotify: "
10755 "NULL region; ignoring.\n");
10756 return;
10757 }
10758
10759 focus_region(r);
10760 } else {
10761 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
10762 "ignoring\n");
10763 return;
10764 }
10765 } else {
10766 if (e->mode == XCB_NOTIFY_MODE_NORMAL &&
10767 e->detail == XCB_NOTIFY_DETAIL_INFERIOR) {
10768 DNPRINTF(SWM_D_EVENT, "enternotify: entering from "
10769 "inferior; ignoring\n");
10770 return;
10771 }
10772
10773 focus_win(get_focus_magic(win));
10774 }
10775
10776 DNPRINTF(SWM_D_EVENT, "enternotify: done\n");
10777
10778 xcb_flush(conn);
10779 }
10780
10781 #ifdef SWM_DEBUG
10782 void
10783 leavenotify(xcb_leave_notify_event_t *e)
10784 {
10785 last_event_time = e->time;
10786
10787 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): %#x "
10788 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10789 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10790 e->time, e->event, e->event_x, e->event_y,
10791 get_notify_mode_label(e->mode), e->mode,
10792 get_notify_detail_label(e->detail), e->detail,
10793 e->root, e->root_x, e->root_y, e->child,
10794 YESNO(e->same_screen_focus), get_state_mask_label(e->state),
10795 e->state);
10796 }
10797 #endif
10798
10799 void
10800 mapnotify(xcb_map_notify_event_t *e)
10801 {
10802 struct ws_win *win, *parent = NULL;
10803 struct workspace *ws;
10804
10805 DNPRINTF(SWM_D_EVENT, "mapnotify: win %#x\n", e->window);
10806
10807 if ((win = manage_window(e->window, spawn_position, false)) == NULL)
10808 return;
10809 ws = win->ws;
10810
10811 if (win->state == SWM_WIN_STATE_REPARENTING)
10812 return;
10813
10814 if (ws->r == NULL) {
10815 unmap_window(win);
10816 goto out;
10817 }
10818
10819 /* Need to know if win was mapped due to ws switch. */
10820 if (ws->state == SWM_WS_STATE_MAPPED) {
10821 if (ws->focus_pending && TRANS(ws->focus_pending))
10822 parent = find_window(win->transient);
10823
10824 /* If window's parent is maximized, don't clear it. */
10825 if ((parent == NULL) || !MAXIMIZED(parent))
10826 if (clear_maximized(ws) > 0)
10827 stack(ws->r);
10828 }
10829
10830 win->mapped = true;
10831 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
10832
10833 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
10834 if (ws->focus_pending == win) {
10835 focus_win(win);
10836 ws->focus_pending = NULL;
10837 center_pointer(ws->r);
10838 focus_flush();
10839 }
10840 }
10841
10842 out:
10843 DNPRINTF(SWM_D_EVENT, "mapnotify: done\n");
10844
10845 xcb_flush(conn);
10846 }
10847
10848 void
10849 mappingnotify(xcb_mapping_notify_event_t *e)
10850 {
10851 if (e->request != XCB_MAPPING_POINTER) {
10852 xcb_refresh_keyboard_mapping(syms, e);
10853 grabkeys();
10854 }
10855
10856 grabbuttons();
10857 }
10858
10859 void
10860 maprequest(xcb_map_request_event_t *e)
10861 {
10862 struct ws_win *win, *w = NULL;
10863
10864 DNPRINTF(SWM_D_EVENT, "maprequest: win %#x\n",
10865 e->window);
10866
10867 win = manage_window(e->window, spawn_position, true);
10868 if (win == NULL)
10869 goto out;
10870
10871 /* The new window should get focus; prepare. */
10872 if (focus_mode != SWM_FOCUS_FOLLOW &&
10873 !(win->quirks & SWM_Q_NOFOCUSONMAP) && ACCEPTS_FOCUS(win)) {
10874 if (win->quirks & SWM_Q_FOCUSONMAP_SINGLE) {
10875 /* See if other wins of same type are already mapped. */
10876 TAILQ_FOREACH(w, &win->ws->winlist, entry) {
10877 if (w == win || !w->mapped)
10878 continue;
10879
10880 if (w->ch.class_name &&
10881 win->ch.class_name &&
10882 strcmp(w->ch.class_name,
10883 win->ch.class_name) == 0 &&
10884 w->ch.instance_name &&
10885 win->ch.instance_name &&
10886 strcmp(w->ch.instance_name,
10887 win->ch.instance_name) == 0)
10888 break;
10889 }
10890 }
10891
10892 if (w == NULL)
10893 win->ws->focus_pending = get_focus_magic(win);
10894 }
10895
10896 /* All windows need to be mapped if they are in the current workspace.*/
10897 stack(win->ws->r);
10898
10899 /* Ignore EnterNotify to handle the mapnotify without interference. */
10900 if (focus_mode == SWM_FOCUS_DEFAULT)
10901 event_drain(XCB_ENTER_NOTIFY);
10902 out:
10903 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
10904 }
10905
10906 void
10907 motionnotify(xcb_motion_notify_event_t *e)
10908 {
10909 struct swm_region *r = NULL;
10910 int i, num_screens;
10911
10912 last_event_time = e->time;
10913
10914 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): %#x "
10915 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10916 "child: %#x, same_screen_focus: %s, state: %d\n",
10917 e->time, e->event, e->event_x, e->event_y,
10918 get_notify_detail_label(e->detail), e->detail,
10919 e->root, e->root_x, e->root_y, e->child,
10920 YESNO(e->same_screen), e->state);
10921
10922 if (focus_mode == SWM_FOCUS_MANUAL)
10923 return;
10924
10925 num_screens = get_screen_count();
10926 for (i = 0; i < num_screens; i++)
10927 if (screens[i].root == e->root)
10928 break;
10929
10930 TAILQ_FOREACH(r, &screens[i].rl, entry)
10931 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
10932 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
10933 break;
10934
10935 focus_region(r);
10936 }
10937
10938 #ifdef SWM_DEBUG
10939 char *
10940 get_atom_name(xcb_atom_t atom)
10941 {
10942 char *name = NULL;
10943 #ifdef SWM_DEBUG_ATOM_NAMES
10944 /*
10945 * This should be disabled during most debugging since
10946 * xcb_get_* causes an xcb_flush.
10947 */
10948 size_t len;
10949 xcb_get_atom_name_reply_t *r;
10950
10951 r = xcb_get_atom_name_reply(conn,
10952 xcb_get_atom_name(conn, atom),
10953 NULL);
10954 if (r) {
10955 len = xcb_get_atom_name_name_length(r);
10956 if (len > 0) {
10957 name = malloc(len + 1);
10958 if (name) {
10959 memcpy(name, xcb_get_atom_name_name(r), len);
10960 name[len] = '\0';
10961 }
10962 }
10963 free(r);
10964 }
10965 #else
10966 (void)atom;
10967 #endif
10968 return (name);
10969 }
10970 #endif
10971
10972 void
10973 propertynotify(xcb_property_notify_event_t *e)
10974 {
10975 struct ws_win *win;
10976 struct workspace *ws;
10977 #ifdef SWM_DEBUG
10978 char *name;
10979
10980 name = get_atom_name(e->atom);
10981 DNPRINTF(SWM_D_EVENT, "propertynotify: win %#x, atom: %s(%u), "
10982 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
10983 e->state);
10984 free(name);
10985 #endif
10986 last_event_time = e->time;
10987
10988 win = find_window(e->window);
10989 if (win == NULL)
10990 return;
10991
10992 ws = win->ws;
10993
10994 if (e->atom == a_state) {
10995 /* State just changed, make sure it gets focused if mapped. */
10996 if (e->state == XCB_PROPERTY_NEW_VALUE) {
10997 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(ws)) {
10998 if (win->mapped &&
10999 win->state == SWM_WIN_STATE_REPARENTED &&
11000 ws->focus_pending == win) {
11001 focus_win(ws->focus_pending);
11002 ws->focus_pending = NULL;
11003 }
11004 }
11005 }
11006 } else if (e->atom == XCB_ATOM_WM_CLASS ||
11007 e->atom == XCB_ATOM_WM_NAME) {
11008 if (ws->r)
11009 bar_draw(ws->r->bar);
11010 } else if (e->atom == a_prot) {
11011 get_wm_protocols(win);
11012 } else if (e->atom == XCB_ATOM_WM_NORMAL_HINTS) {
11013 xcb_icccm_get_wm_normal_hints_reply(conn,
11014 xcb_icccm_get_wm_normal_hints(conn, win->id),
11015 &win->sh, NULL);
11016 }
11017
11018 xcb_flush(conn);
11019 }
11020
11021 void
11022 reparentnotify(xcb_reparent_notify_event_t *e)
11023 {
11024 struct ws_win *win;
11025
11026 DNPRINTF(SWM_D_EVENT, "reparentnotify: event: %#x, win %#x, "
11027 "parent: %#x, (x,y): (%u,%u), override_redirect: %u\n",
11028 e->event, e->window, e->parent, e->x, e->y, e->override_redirect);
11029
11030 win = find_window(e->window);
11031 if (win) {
11032 if (win->state == SWM_WIN_STATE_REPARENTING) {
11033 win->state = SWM_WIN_STATE_REPARENTED;
11034
11035 if (win->ws->r)
11036 map_window(win);
11037 else
11038 unmap_window(win);
11039
11040 update_window(win);
11041 update_win_stacking(win);
11042 } else if (win->state == SWM_WIN_STATE_UNPARENTING) {
11043 win->state = SWM_WIN_STATE_UNPARENTED;
11044 }
11045 }
11046 }
11047
11048 void
11049 unmapnotify(xcb_unmap_notify_event_t *e)
11050 {
11051 struct ws_win *win;
11052 struct workspace *ws;
11053
11054 DNPRINTF(SWM_D_EVENT, "unmapnotify: win %#x\n", e->window);
11055
11056 /* If we aren't managing the window, then ignore. */
11057 win = find_window(e->window);
11058 if (win == NULL || win->id != e->window) {
11059 DNPRINTF(SWM_D_EVENT, "unmapnotify: ignore unmanaged.\n");
11060 return;
11061 }
11062
11063 /* Do nothing if already withdrawn. */
11064 if (!win->mapped && !ICONIC(win)) {
11065 DNPRINTF(SWM_D_EVENT, "unmapnotify: ignore withdrawn.\n");
11066 return;
11067 }
11068
11069 ws = win->ws;
11070 win->mapped = false;
11071
11072 /* Ignore if reparenting-related. */
11073 if (win->state != SWM_WIN_STATE_REPARENTED) {
11074 DNPRINTF(SWM_D_EVENT, "unmapnotify: ignore not reparented.\n");
11075 return;
11076 }
11077
11078 /* If win was focused, make sure to focus on something else. */
11079 if (win == ws->focus) {
11080 if (focus_mode != SWM_FOCUS_FOLLOW) {
11081 ws->focus_pending = get_focus_prev(win);
11082 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
11083 "focus_pending: %#x\n",
11084 WINID(ws->focus_pending));
11085 }
11086
11087 unfocus_win(win);
11088 }
11089
11090 if (ICONIC(win)) {
11091 /* Iconify. */
11092 DNPRINTF(SWM_D_EVENT, "unmapnotify: iconify.\n");
11093 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
11094 } else {
11095 /* Withdraw. */
11096 DNPRINTF(SWM_D_EVENT, "unmapnotify: withdraw.\n");
11097 set_win_state(win, XCB_ICCCM_WM_STATE_WITHDRAWN);
11098 unmanage_window(win);
11099 }
11100
11101 stack(ws->r);
11102
11103 /* Update focus if ws is active. */
11104 if (WS_FOCUSED(ws)) {
11105 if (focus_mode == SWM_FOCUS_FOLLOW) {
11106 focus_win(get_pointer_win(ws->r->s->root));
11107 } else if (ws->focus_pending) {
11108 focus_win(ws->focus_pending);
11109 ws->focus_pending = NULL;
11110 } else if (ws->focus == NULL) {
11111 DNPRINTF(SWM_D_FOCUS, "unmapnotify: set_input_focus: "
11112 "%#x, revert-to: parent, time: 0\n",
11113 ws->r->id);
11114 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
11115 ws->r->id, XCB_CURRENT_TIME);
11116 }
11117 }
11118
11119 center_pointer(ws->r);
11120 focus_flush();
11121 DNPRINTF(SWM_D_EVENT, "unmapnotify: done.\n");
11122 }
11123
11124 #ifdef SWM_DEBUG
11125 char *
11126 get_source_type_label(uint32_t type)
11127 {
11128 char *label;
11129
11130 switch (type) {
11131 case EWMH_SOURCE_TYPE_NONE:
11132 label = "None";
11133 break;
11134 case EWMH_SOURCE_TYPE_NORMAL:
11135 label = "Normal";
11136 break;
11137 case EWMH_SOURCE_TYPE_OTHER:
11138 label = "Other";
11139 break;
11140 default:
11141 label = "Invalid";
11142 }
11143
11144 return label;
11145 }
11146 #endif
11147
11148 void
11149 clientmessage(xcb_client_message_event_t *e)
11150 {
11151 struct ws_win *win;
11152 struct swm_region *r = NULL;
11153 union arg a;
11154 uint32_t vals[4];
11155 int num_screens, i;
11156 xcb_map_request_event_t mre;
11157 #ifdef SWM_DEBUG
11158 char *name;
11159
11160 name = get_atom_name(e->type);
11161 DNPRINTF(SWM_D_EVENT, "clientmessage: win %#x, atom: %s(%u)\n",
11162 e->window, name, e->type);
11163 free(name);
11164 #endif
11165
11166 if (e->type == ewmh[_NET_CURRENT_DESKTOP].atom) {
11167 num_screens = get_screen_count();
11168 for (i = 0; i < num_screens; i++)
11169 if (screens[i].root == e->window) {
11170 r = screens[i].r_focus;
11171 break;
11172 }
11173
11174 if (r && e->data.data32[0] < (uint32_t)workspace_limit) {
11175 a.id = e->data.data32[0];
11176 switchws(NULL, r, &a);
11177 focus_flush();
11178 }
11179
11180 return;
11181 } else if (e->type == ewmh[_NET_REQUEST_FRAME_EXTENTS].atom) {
11182 DNPRINTF(SWM_D_EVENT,
11183 "clientmessage: set _NET_FRAME_EXTENTS on window.\n");
11184 vals[0] = vals[1] = vals[2] = vals[3] = border_width;
11185 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, e->window,
11186 a_net_frame_extents, XCB_ATOM_CARDINAL, 32, 4, vals);
11187 xcb_flush(conn);
11188 return;
11189 }
11190
11191 win = find_window(e->window);
11192 if (win == NULL) {
11193 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
11194 /* Manage the window with maprequest. */
11195 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
11196 "unmanaged window.\n");
11197 mre.window = e->window;
11198 maprequest(&mre);
11199 }
11200 return;
11201 }
11202
11203 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
11204 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW, "
11205 "source_type: %s(%d)\n",
11206 get_source_type_label(e->data.data32[0]),
11207 e->data.data32[0]);
11208
11209 /*
11210 * Allow focus changes that are a result of direct user
11211 * action and from applications that use the old EWMH spec.
11212 */
11213 if (e->data.data32[0] != EWMH_SOURCE_TYPE_NORMAL ||
11214 win->quirks & SWM_Q_OBEYAPPFOCUSREQ) {
11215 if (WS_FOCUSED(win->ws))
11216 focus_win(win);
11217 else
11218 win->ws->focus_pending = win;
11219 }
11220 } else if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
11221 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
11222 if (win->can_delete)
11223 client_msg(win, a_delete, 0);
11224 else
11225 xcb_kill_client(conn, win->id);
11226 } else if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
11227 DNPRINTF(SWM_D_EVENT,
11228 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
11229 if (ABOVE(win)) {
11230 if (e->data.data32[0] & (1<<8)) /* x */
11231 X(win) = e->data.data32[1];
11232 if (e->data.data32[0] & (1<<9)) /* y */
11233 Y(win) = e->data.data32[2];
11234 if (e->data.data32[0] & (1<<10)) /* width */
11235 WIDTH(win) = e->data.data32[3];
11236 if (e->data.data32[0] & (1<<11)) /* height */
11237 HEIGHT(win) = e->data.data32[4];
11238
11239 update_window(win);
11240 } else {
11241 /* Notify no change was made. */
11242 config_win(win, NULL);
11243 /* TODO: Change stack sizes */
11244 }
11245 } else if (e->type == ewmh[_NET_RESTACK_WINDOW].atom) {
11246 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_RESTACK_WINDOW\n");
11247 vals[0] = e->data.data32[1]; /* Sibling window. */
11248 vals[1] = e->data.data32[2]; /* Stack mode detail. */
11249
11250 if (win->frame != XCB_WINDOW_NONE)
11251 xcb_configure_window(conn, win->frame,
11252 XCB_CONFIG_WINDOW_SIBLING |
11253 XCB_CONFIG_WINDOW_STACK_MODE, vals);
11254 } else if (e->type == ewmh[_NET_WM_STATE].atom) {
11255 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
11256 ewmh_change_wm_state(win, e->data.data32[1], e->data.data32[0]);
11257 if (e->data.data32[2])
11258 ewmh_change_wm_state(win, e->data.data32[2],
11259 e->data.data32[0]);
11260
11261 ewmh_update_wm_state(win);
11262 stack(win->ws->r);
11263 } else if (e->type == ewmh[_NET_WM_DESKTOP].atom) {
11264 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_DESKTOP\n");
11265 r = win->ws->r;
11266
11267 win_to_ws(win, e->data.data32[0], true);
11268
11269 /* Stack source and destination ws, if mapped. */
11270 if (r != win->ws->r) {
11271 if (r)
11272 stack(r);
11273
11274 if (win->ws->r) {
11275 if (FLOATING(win))
11276 load_float_geom(win);
11277
11278 stack(win->ws->r);
11279 }
11280 }
11281 }
11282
11283 focus_flush();
11284 }
11285
11286 void
11287 check_conn(void)
11288 {
11289 int errcode = xcb_connection_has_error(conn);
11290 #ifdef XCB_CONN_ERROR
11291 char *s;
11292 switch (errcode) {
11293 case XCB_CONN_ERROR:
11294 s = "Socket error, pipe error or other stream error.";
11295 break;
11296 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
11297 s = "Extension not supported.";
11298 break;
11299 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
11300 s = "Insufficient memory.";
11301 break;
11302 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
11303 s = "Request length was exceeded.";
11304 break;
11305 case XCB_CONN_CLOSED_PARSE_ERR:
11306 s = "Error parsing display string.";
11307 break;
11308 default:
11309 s = "Unknown error.";
11310 }
11311 if (errcode)
11312 errx(errcode, "X CONNECTION ERROR: %s", s);
11313 #else
11314 if (errcode)
11315 errx(errcode, "X CONNECTION ERROR");
11316 #endif
11317 }
11318
11319 int
11320 enable_wm(void)
11321 {
11322 int num_screens, i;
11323 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
11324 XCB_EVENT_MASK_ENTER_WINDOW |
11325 XCB_EVENT_MASK_OWNER_GRAB_BUTTON |
11326 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
11327 XCB_EVENT_MASK_ENTER_WINDOW |
11328 XCB_EVENT_MASK_LEAVE_WINDOW |
11329 XCB_EVENT_MASK_BUTTON_PRESS |
11330 XCB_EVENT_MASK_BUTTON_RELEASE |
11331 XCB_EVENT_MASK_KEY_PRESS |
11332 XCB_EVENT_MASK_KEY_RELEASE |
11333 XCB_EVENT_MASK_PROPERTY_CHANGE;
11334 xcb_screen_t *sc;
11335 xcb_void_cookie_t wac;
11336 xcb_generic_error_t *error;
11337
11338 /* this causes an error if some other window manager is running */
11339 num_screens = get_screen_count();
11340 for (i = 0; i < num_screens; i++) {
11341 if ((sc = get_screen(i)) == NULL)
11342 errx(1, "ERROR: can't get screen %d.", i);
11343 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: %#x\n",
11344 i, sc->root);
11345 wac = xcb_change_window_attributes_checked(conn, sc->root,
11346 XCB_CW_EVENT_MASK, &val);
11347 if ((error = xcb_request_check(conn, wac))) {
11348 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
11349 error->error_code);
11350 free(error);
11351 return 1;
11352 }
11353 }
11354
11355 return 0;
11356 }
11357
11358 void
11359 new_region(struct swm_screen *s, int x, int y, int w, int h)
11360 {
11361 struct swm_region *r = NULL, *n;
11362 struct workspace *ws = NULL;
11363 int i;
11364 uint32_t wa[1];
11365
11366 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
11367 s->idx, w, h, x, y);
11368
11369 /* remove any conflicting regions */
11370 n = TAILQ_FIRST(&s->rl);
11371 while (n) {
11372 r = n;
11373 n = TAILQ_NEXT(r, entry);
11374 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
11375 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
11376 if (r->ws->r != NULL)
11377 r->ws->old_r = r->ws->r;
11378 r->ws->r = NULL;
11379 bar_cleanup(r);
11380 xcb_destroy_window(conn, r->id);
11381 r->id = XCB_WINDOW_NONE;
11382 TAILQ_REMOVE(&s->rl, r, entry);
11383 TAILQ_INSERT_TAIL(&s->orl, r, entry);
11384 }
11385 }
11386
11387 /* search old regions for one to reuse */
11388
11389 /* size + location match */
11390 TAILQ_FOREACH(r, &s->orl, entry)
11391 if (X(r) == x && Y(r) == y &&
11392 HEIGHT(r) == h && WIDTH(r) == w)
11393 break;
11394
11395 /* size match */
11396 TAILQ_FOREACH(r, &s->orl, entry)
11397 if (HEIGHT(r) == h && WIDTH(r) == w)
11398 break;
11399
11400 if (r != NULL) {
11401 TAILQ_REMOVE(&s->orl, r, entry);
11402 /* try to use old region's workspace */
11403 if (r->ws->r == NULL)
11404 ws = r->ws;
11405 } else
11406 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
11407 err(1, "new_region: calloc: failed to allocate memory "
11408 "for screen");
11409
11410 /* if we don't have a workspace already, find one */
11411 if (ws == NULL) {
11412 for (i = 0; i < workspace_limit; i++)
11413 if (s->ws[i].r == NULL) {
11414 ws = &s->ws[i];
11415 break;
11416 }
11417 }
11418
11419 if (ws == NULL)
11420 errx(1, "new_region: no free workspaces");
11421
11422 if (ws->state == SWM_WS_STATE_HIDDEN)
11423 ws->state = SWM_WS_STATE_MAPPING;
11424
11425 X(r) = x;
11426 Y(r) = y;
11427 WIDTH(r) = w;
11428 HEIGHT(r) = h;
11429 r->bar = NULL;
11430 r->s = s;
11431 r->ws = ws;
11432 r->ws_prior = NULL;
11433 ws->r = r;
11434 outputs++;
11435 TAILQ_INSERT_TAIL(&s->rl, r, entry);
11436
11437 /* Invisible region window to detect pointer events on empty regions. */
11438 r->id = xcb_generate_id(conn);
11439 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
11440 XCB_EVENT_MASK_POINTER_MOTION_HINT;
11441
11442 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
11443 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
11444 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
11445
11446 /* Make sure region input is at the bottom. */
11447 wa[0] = XCB_STACK_MODE_BELOW;
11448 xcb_configure_window(conn, r->id, XCB_CONFIG_WINDOW_STACK_MODE, wa);
11449
11450 xcb_map_window(conn, r->id);
11451 }
11452
11453 void
11454 scan_randr(int idx)
11455 {
11456 #ifdef SWM_XRR_HAS_CRTC
11457 int c;
11458 int ncrtc = 0;
11459 #endif /* SWM_XRR_HAS_CRTC */
11460 struct swm_region *r;
11461 struct ws_win *win;
11462 int num_screens;
11463 xcb_randr_get_screen_resources_current_cookie_t src;
11464 xcb_randr_get_screen_resources_current_reply_t *srr;
11465 xcb_randr_get_crtc_info_cookie_t cic;
11466 xcb_randr_get_crtc_info_reply_t *cir = NULL;
11467 xcb_randr_crtc_t *crtc;
11468 xcb_screen_t *screen;
11469
11470 DNPRINTF(SWM_D_MISC, "scan_randr: screen: %d\n", idx);
11471
11472 if ((screen = get_screen(idx)) == NULL)
11473 errx(1, "ERROR: can't get screen %d.", idx);
11474
11475 num_screens = get_screen_count();
11476 if (idx >= num_screens)
11477 errx(1, "scan_randr: invalid screen");
11478
11479 /* remove any old regions */
11480 while ((r = TAILQ_FIRST(&screens[idx].rl)) != NULL) {
11481 r->ws->old_r = r->ws->r = NULL;
11482 bar_cleanup(r);
11483 xcb_destroy_window(conn, r->id);
11484 r->id = XCB_WINDOW_NONE;
11485 TAILQ_REMOVE(&screens[idx].rl, r, entry);
11486 TAILQ_INSERT_TAIL(&screens[idx].orl, r, entry);
11487 }
11488 outputs = 0;
11489
11490 /* map virtual screens onto physical screens */
11491 #ifdef SWM_XRR_HAS_CRTC
11492 if (randr_support) {
11493 src = xcb_randr_get_screen_resources_current(conn,
11494 screens[idx].root);
11495 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
11496 NULL);
11497 if (srr == NULL) {
11498 new_region(&screens[idx], 0, 0,
11499 screen->width_in_pixels,
11500 screen->height_in_pixels);
11501 goto out;
11502 } else
11503 ncrtc = srr->num_crtcs;
11504
11505 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
11506 for (c = 0; c < ncrtc; c++) {
11507 cic = xcb_randr_get_crtc_info(conn, crtc[c],
11508 XCB_CURRENT_TIME);
11509 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
11510 if (cir == NULL)
11511 continue;
11512 if (cir->num_outputs == 0) {
11513 free(cir);
11514 continue;
11515 }
11516
11517 if (cir->mode == 0)
11518 new_region(&screens[idx], 0, 0,
11519 screen->width_in_pixels,
11520 screen->height_in_pixels);
11521 else
11522 new_region(&screens[idx],
11523 cir->x, cir->y, cir->width, cir->height);
11524 free(cir);
11525 }
11526 free(srr);
11527 }
11528 #endif /* SWM_XRR_HAS_CRTC */
11529
11530 /* If detection failed, create a single region that spans the screen. */
11531 if (TAILQ_EMPTY(&screens[idx].rl))
11532 new_region(&screens[idx], 0, 0, screen->width_in_pixels,
11533 screen->height_in_pixels);
11534
11535 out:
11536 /* Cleanup unused previously visible workspaces. */
11537 TAILQ_FOREACH(r, &screens[idx].orl, entry) {
11538 TAILQ_FOREACH(win, &r->ws->winlist, entry)
11539 unmap_window(win);
11540 r->ws->state = SWM_WS_STATE_HIDDEN;
11541
11542 /* The screen shouldn't focus on an unused region. */
11543 if (screens[idx].r_focus == r)
11544 screens[idx].r_focus = NULL;
11545 }
11546
11547 DNPRINTF(SWM_D_MISC, "scan_randr: done.\n");
11548 }
11549
11550 void
11551 screenchange(xcb_randr_screen_change_notify_event_t *e)
11552 {
11553 struct swm_region *r;
11554 int i, num_screens;
11555
11556 DNPRINTF(SWM_D_EVENT, "screenchange: root: %#x\n", e->root);
11557
11558 num_screens = get_screen_count();
11559 /* silly event doesn't include the screen index */
11560 for (i = 0; i < num_screens; i++)
11561 if (screens[i].root == e->root)
11562 break;
11563 if (i >= num_screens)
11564 errx(1, "screenchange: screen not found");
11565
11566 /* brute force for now, just re-enumerate the regions */
11567 scan_randr(i);
11568
11569 #ifdef SWM_DEBUG
11570 print_win_geom(e->root);
11571 #endif
11572 /* add bars to all regions */
11573 TAILQ_FOREACH(r, &screens[i].rl, entry)
11574 bar_setup(r);
11575
11576 /* Stack all regions. */
11577 TAILQ_FOREACH(r, &screens[i].rl, entry)
11578 stack(r);
11579
11580 /* Make sure a region has focus. */
11581 if (screens[i].r_focus == NULL) {
11582 r = TAILQ_FIRST(&screens[i].rl);
11583 if (r != NULL)
11584 focus_region(r);
11585 }
11586
11587 focus_flush();
11588
11589 /* Update workspace state and bar on all regions. */
11590 TAILQ_FOREACH(r, &screens[i].rl, entry) {
11591 r->ws->state = SWM_WS_STATE_MAPPED;
11592 bar_draw(r->bar);
11593 }
11594 }
11595
11596 void
11597 grab_windows(void)
11598 {
11599 struct swm_region *r = NULL;
11600 xcb_query_tree_cookie_t qtc;
11601 xcb_query_tree_reply_t *qtr;
11602 xcb_get_property_cookie_t pc;
11603 xcb_get_property_reply_t *pr;
11604 xcb_window_t *wins = NULL, trans, *cwins = NULL;
11605 int i, j, k, n, no, num_screens;
11606
11607 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
11608 num_screens = get_screen_count();
11609 for (i = 0; i < num_screens; i++) {
11610 qtc = xcb_query_tree(conn, screens[i].root);
11611 qtr = xcb_query_tree_reply(conn, qtc, NULL);
11612 if (qtr == NULL)
11613 continue;
11614 wins = xcb_query_tree_children(qtr);
11615 no = xcb_query_tree_children_length(qtr);
11616
11617 /* Try to sort windows according to _NET_CLIENT_LIST. */
11618 pr = xcb_get_property_reply(conn, xcb_get_property(conn, 0,
11619 screens[i].root, ewmh[_NET_CLIENT_LIST].atom,
11620 XCB_ATOM_WINDOW, 0, UINT32_MAX), NULL);
11621 if (pr != NULL) {
11622 cwins = xcb_get_property_value(pr);
11623 n = xcb_get_property_value_length(pr) /
11624 sizeof(xcb_atom_t);
11625
11626 for (j = 0; j < n; ++j) {
11627 for (k = j; k < no; ++k) {
11628 if (wins[k] == cwins[j]) {
11629 /* Swap wins j and k. */
11630 wins[k] = wins[j];
11631 wins[j] = cwins[j];
11632 }
11633 }
11634 }
11635
11636 free(pr);
11637 }
11638
11639 /* Manage top-level windows first, then transients. */
11640 /* TODO: allow transients to be managed before leader. */
11641 DNPRINTF(SWM_D_INIT, "grab_windows: grab top-level windows.\n");
11642 for (j = 0; j < no; j++) {
11643 TAILQ_FOREACH(r, &screens[i].rl, entry) {
11644 if (r->id == wins[j]) {
11645 DNPRINTF(SWM_D_INIT, "grab_windows: "
11646 "skip %#x; region input window.\n",
11647 wins[j]);
11648 break;
11649 } else if (r->bar->id == wins[j]) {
11650 DNPRINTF(SWM_D_INIT, "grab_windows: "
11651 "skip %#x; region bar.\n",
11652 wins[j]);
11653 break;
11654 }
11655 }
11656
11657 if (r)
11658 continue;
11659
11660 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
11661 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
11662 &trans, NULL)) {
11663 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
11664 "is transient for %#x.\n", wins[j], trans);
11665 continue;
11666 }
11667
11668 manage_window(wins[j], SWM_STACK_TOP, false);
11669 }
11670
11671 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
11672 for (j = 0; j < no; j++) {
11673 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
11674 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
11675 &trans, NULL))
11676 manage_window(wins[j], SWM_STACK_TOP, false);
11677 }
11678 free(qtr);
11679 }
11680 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
11681 }
11682
11683 void
11684 setup_screens(void)
11685 {
11686 int i, j, k, num_screens;
11687 struct workspace *ws;
11688 uint32_t gcv[1], wa[1];
11689 const xcb_query_extension_reply_t *qep;
11690 xcb_screen_t *screen;
11691 xcb_randr_query_version_cookie_t c;
11692 xcb_randr_query_version_reply_t *r;
11693
11694 num_screens = get_screen_count();
11695 if ((screens = calloc(num_screens, sizeof(struct swm_screen))) == NULL)
11696 err(1, "setup_screens: calloc: failed to allocate memory for "
11697 "screens");
11698
11699 /* Initial RandR setup. */
11700 randr_support = false;
11701 qep = xcb_get_extension_data(conn, &xcb_randr_id);
11702 if (qep->present) {
11703 c = xcb_randr_query_version(conn, 1, 1);
11704 r = xcb_randr_query_version_reply(conn, c, NULL);
11705 if (r) {
11706 if (r->major_version >= 1) {
11707 randr_support = true;
11708 randr_eventbase = qep->first_event;
11709 }
11710 free(r);
11711 }
11712 }
11713
11714 wa[0] = cursors[XC_LEFT_PTR].cid;
11715
11716 /* map physical screens */
11717 for (i = 0; i < num_screens; i++) {
11718 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
11719 screens[i].idx = i;
11720 screens[i].r_focus = NULL;
11721
11722 TAILQ_INIT(&screens[i].rl);
11723 TAILQ_INIT(&screens[i].orl);
11724 if ((screen = get_screen(i)) == NULL)
11725 errx(1, "ERROR: can't get screen %d.", i);
11726 screens[i].root = screen->root;
11727
11728 /* set default colors */
11729 setscreencolor("red", i, SWM_S_COLOR_FOCUS);
11730 setscreencolor("rgb:88/88/88", i, SWM_S_COLOR_UNFOCUS);
11731 setscreencolor("rgb:00/80/80", i, SWM_S_COLOR_BAR_BORDER);
11732 setscreencolor("rgb:00/40/40", i,
11733 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
11734 setscreencolor("black", i, SWM_S_COLOR_BAR);
11735 setscreencolor("rgb:a0/a0/a0", i, SWM_S_COLOR_BAR_FONT);
11736 setscreencolor("red", i, SWM_S_COLOR_FOCUS_MAXIMIZED);
11737 setscreencolor("rgb:88/88/88", i,
11738 SWM_S_COLOR_UNFOCUS_MAXIMIZED);
11739
11740 /* create graphics context on screen */
11741 screens[i].bar_gc = xcb_generate_id(conn);
11742 gcv[0] = 0;
11743 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
11744 XCB_GC_GRAPHICS_EXPOSURES, gcv);
11745
11746 /* set default cursor */
11747 xcb_change_window_attributes(conn, screens[i].root,
11748 XCB_CW_CURSOR, wa);
11749
11750 /* init all workspaces */
11751 /* XXX these should be dynamically allocated too */
11752 for (j = 0; j < SWM_WS_MAX; j++) {
11753 ws = &screens[i].ws[j];
11754 ws->idx = j;
11755 ws->name = NULL;
11756 ws->bar_enabled = true;
11757 ws->focus = NULL;
11758 ws->focus_prev = NULL;
11759 ws->focus_pending = NULL;
11760 ws->r = NULL;
11761 ws->old_r = NULL;
11762 ws->state = SWM_WS_STATE_HIDDEN;
11763 TAILQ_INIT(&ws->stack);
11764 TAILQ_INIT(&ws->winlist);
11765 TAILQ_INIT(&ws->unmanagedlist);
11766
11767 for (k = 0; layouts[k].l_stack != NULL; k++)
11768 if (layouts[k].l_config != NULL)
11769 layouts[k].l_config(ws,
11770 SWM_ARG_ID_STACKINIT);
11771 ws->cur_layout = &layouts[0];
11772 ws->cur_layout->l_string(ws);
11773 }
11774
11775 scan_randr(i);
11776
11777 if (randr_support)
11778 xcb_randr_select_input(conn, screens[i].root,
11779 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
11780 }
11781 }
11782
11783 void
11784 setup_globals(void)
11785 {
11786 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
11787 err(1, "setup_globals: strdup: failed to allocate memory.");
11788
11789 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
11790 err(1, "setup_globals: strdup: failed to allocate memory.");
11791
11792 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
11793 errx(1, "unable to allocate key symbols");
11794
11795 a_state = get_atom_from_string("WM_STATE");
11796 a_prot = get_atom_from_string("WM_PROTOCOLS");
11797 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
11798 a_net_frame_extents = get_atom_from_string("_NET_FRAME_EXTENTS");
11799 a_net_supported = get_atom_from_string("_NET_SUPPORTED");
11800 a_net_wm_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
11801 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
11802 a_utf8_string = get_atom_from_string("UTF8_STRING");
11803 a_swm_ws = get_atom_from_string("_SWM_WS");
11804 }
11805
11806 void
11807 shutdown_cleanup(void)
11808 {
11809 struct swm_region *r;
11810 struct ws_win *w;
11811 struct workspace *ws;
11812 int i, num_screens;
11813
11814 /* disable alarm because the following code may not be interrupted */
11815 alarm(0);
11816 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
11817 err(1, "can't disable alarm");
11818
11819 bar_extra_stop();
11820 unmap_all();
11821
11822 cursors_cleanup();
11823
11824 clear_quirks();
11825 clear_spawns();
11826 clear_bindings();
11827
11828 teardown_ewmh();
11829
11830 num_screens = get_screen_count();
11831 for (i = 0; i < num_screens; ++i) {
11832 int j;
11833
11834 DNPRINTF(SWM_D_FOCUS, "shutdown_cleanup: set_input_focus: "
11835 "%#x, revert-to: root, time: 0\n", screens[i].root);
11836 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
11837 screens[i].root, XCB_CURRENT_TIME);
11838
11839 if (screens[i].bar_gc != XCB_NONE)
11840 xcb_free_gc(conn, screens[i].bar_gc);
11841 if (!bar_font_legacy) {
11842 XftColorFree(display, DefaultVisual(display, i),
11843 DefaultColormap(display, i), &bar_font_color);
11844 XftColorFree(display, DefaultVisual(display, i),
11845 DefaultColormap(display, i), &search_font_color);
11846 }
11847
11848 for (j = 0; j < SWM_S_COLOR_MAX; ++j) {
11849 free(screens[i].c[j].name);
11850 }
11851
11852 /* Free window memory. */
11853 for (j = 0; j < SWM_WS_MAX; ++j) {
11854 ws = &screens[i].ws[j];
11855 free(ws->name);
11856
11857 while ((w = TAILQ_FIRST(&ws->winlist)) != NULL) {
11858 TAILQ_REMOVE(&ws->winlist, w, entry);
11859 free_window(w);
11860 }
11861
11862 while ((w = TAILQ_FIRST(&ws->unmanagedlist)) != NULL) {
11863 TAILQ_REMOVE(&ws->unmanagedlist, w, entry);
11864 free_window(w);
11865 }
11866 }
11867
11868 /* Free region memory. */
11869 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
11870 TAILQ_REMOVE(&screens[i].rl, r, entry);
11871 free(r->bar);
11872 free(r);
11873 }
11874
11875 while ((r = TAILQ_FIRST(&screens[i].orl)) != NULL) {
11876 TAILQ_REMOVE(&screens[i].rl, r, entry);
11877 free(r->bar);
11878 free(r);
11879 }
11880 }
11881 free(screens);
11882
11883 free(bar_format);
11884 free(bar_fonts);
11885 free(clock_format);
11886 free(startup_exception);
11887
11888 if (bar_fs)
11889 XFreeFontSet(display, bar_fs);
11890 if (bar_font)
11891 XftFontClose(display, bar_font);
11892
11893 xcb_key_symbols_free(syms);
11894 xcb_flush(conn);
11895 xcb_aux_sync(conn);
11896 xcb_disconnect(conn);
11897 }
11898
11899 void
11900 event_error(xcb_generic_error_t *e)
11901 {
11902 (void)e;
11903
11904 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
11905 "resource_id: %u, minor_code: %u\n",
11906 xcb_event_get_error_label(e->error_code), e->error_code,
11907 xcb_event_get_request_label(e->major_code), e->major_code,
11908 e->sequence, e->resource_id, e->minor_code);
11909 }
11910
11911 void
11912 event_handle(xcb_generic_event_t *evt)
11913 {
11914 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
11915
11916 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
11917 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
11918 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
11919
11920 switch (type) {
11921 #define EVENT(type, callback) case type: callback((void *)evt); return
11922 EVENT(0, event_error);
11923 EVENT(XCB_BUTTON_PRESS, buttonpress);
11924 EVENT(XCB_BUTTON_RELEASE, buttonrelease);
11925 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
11926 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
11927 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
11928 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
11929 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
11930 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
11931 /*EVENT(XCB_CREATE_NOTIFY, );*/
11932 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
11933 EVENT(XCB_ENTER_NOTIFY, enternotify);
11934 EVENT(XCB_EXPOSE, expose);
11935 EVENT(XCB_FOCUS_IN, focusin);
11936 #ifdef SWM_DEBUG
11937 EVENT(XCB_FOCUS_OUT, focusout);
11938 #endif
11939 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
11940 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
11941 EVENT(XCB_KEY_PRESS, keypress);
11942 EVENT(XCB_KEY_RELEASE, keyrelease);
11943 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
11944 #ifdef SWM_DEBUG
11945 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
11946 #endif
11947 EVENT(XCB_MAP_NOTIFY, mapnotify);
11948 EVENT(XCB_MAP_REQUEST, maprequest);
11949 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
11950 EVENT(XCB_MOTION_NOTIFY, motionnotify);
11951 /*EVENT(XCB_NO_EXPOSURE, );*/
11952 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
11953 EVENT(XCB_REPARENT_NOTIFY, reparentnotify);
11954 /*EVENT(XCB_RESIZE_REQUEST, );*/
11955 /*EVENT(XCB_SELECTION_CLEAR, );*/
11956 /*EVENT(XCB_SELECTION_NOTIFY, );*/
11957 /*EVENT(XCB_SELECTION_REQUEST, );*/
11958 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
11959 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
11960 #undef EVENT
11961 }
11962 if (type - randr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
11963 screenchange((void *)evt);
11964 }
11965
11966 int
11967 main(int argc, char *argv[])
11968 {
11969 struct pollfd pfd[2];
11970 struct sigaction sact;
11971 struct stat sb;
11972 struct passwd *pwd;
11973 struct swm_region *r;
11974 xcb_generic_event_t *evt;
11975 int xfd, i, num_screens, num_readable;
11976 char conf[PATH_MAX], *cfile = NULL;
11977 bool stdin_ready = false, startup = true;
11978
11979 /* suppress unused warning since var is needed */
11980 (void)argc;
11981
11982 #ifdef SWM_DEBUG
11983 time_started = time(NULL);
11984 #endif
11985
11986 start_argv = argv;
11987 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
11988 if (setlocale(LC_CTYPE, "") == NULL || setlocale(LC_TIME, "") == NULL)
11989 warnx("no locale support");
11990
11991 /* handle some signals */
11992 bzero(&sact, sizeof(sact));
11993 sigemptyset(&sact.sa_mask);
11994 sact.sa_flags = 0;
11995 sact.sa_handler = sighdlr;
11996 sigaction(SIGINT, &sact, NULL);
11997 sigaction(SIGQUIT, &sact, NULL);
11998 sigaction(SIGTERM, &sact, NULL);
11999 sigaction(SIGHUP, &sact, NULL);
12000
12001 sact.sa_handler = sighdlr;
12002 sact.sa_flags = SA_NOCLDSTOP;
12003 sigaction(SIGCHLD, &sact, NULL);
12004
12005 if ((display = XOpenDisplay(0)) == NULL)
12006 errx(1, "can not open display");
12007
12008 conn = XGetXCBConnection(display);
12009 if (xcb_connection_has_error(conn))
12010 errx(1, "can not get XCB connection");
12011
12012 XSetEventQueueOwner(display, XCBOwnsEventQueue);
12013
12014 xcb_prefetch_extension_data(conn, &xcb_randr_id);
12015 xfd = xcb_get_file_descriptor(conn);
12016
12017 /* look for local and global conf file */
12018 pwd = getpwuid(getuid());
12019 if (pwd == NULL)
12020 errx(1, "invalid user: %d", getuid());
12021
12022 xcb_grab_server(conn);
12023 xcb_aux_sync(conn);
12024
12025 /* flush all events */
12026 while ((evt = get_next_event(false))) {
12027 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
12028 event_handle(evt);
12029 free(evt);
12030 }
12031
12032 if (enable_wm())
12033 errx(1, "another window manager is currently running");
12034
12035 /* Load Xcursors and/or cursorfont glyph cursors. */
12036 cursors_load();
12037
12038 xcb_aux_sync(conn);
12039
12040 setup_globals();
12041 setup_screens();
12042 setup_ewmh();
12043 setup_keybindings();
12044 setup_btnbindings();
12045 setup_quirks();
12046 setup_spawn();
12047
12048 /* load config */
12049 for (i = 0; ; i++) {
12050 conf[0] = '\0';
12051 switch (i) {
12052 case 0:
12053 /* ~ */
12054 snprintf(conf, sizeof conf, "%s/.%s",
12055 pwd->pw_dir, SWM_CONF_FILE);
12056 break;
12057 case 1:
12058 /* global */
12059 snprintf(conf, sizeof conf, "/etc/%s",
12060 SWM_CONF_FILE);
12061 break;
12062 case 2:
12063 /* ~ compat */
12064 snprintf(conf, sizeof conf, "%s/.%s",
12065 pwd->pw_dir, SWM_CONF_FILE_OLD);
12066 break;
12067 case 3:
12068 /* global compat */
12069 snprintf(conf, sizeof conf, "/etc/%s",
12070 SWM_CONF_FILE_OLD);
12071 break;
12072 default:
12073 goto noconfig;
12074 }
12075
12076 if (strlen(conf) && stat(conf, &sb) != -1)
12077 if (S_ISREG(sb.st_mode)) {
12078 cfile = conf;
12079 break;
12080 }
12081 }
12082 noconfig:
12083
12084 /* load conf (if any) */
12085 if (cfile)
12086 conf_load(cfile, SWM_CONF_DEFAULT);
12087
12088 validate_spawns();
12089
12090 if (getenv("SWM_STARTED") == NULL)
12091 setenv("SWM_STARTED", "YES", 1);
12092
12093 /* setup all bars */
12094 num_screens = get_screen_count();
12095 for (i = 0; i < num_screens; i++)
12096 TAILQ_FOREACH(r, &screens[i].rl, entry)
12097 bar_setup(r);
12098
12099 /* Manage existing windows. */
12100 grab_windows();
12101
12102 grabkeys();
12103 grabbuttons();
12104
12105 /* Stack all regions to trigger mapping. */
12106 for (i = 0; i < num_screens; i++)
12107 TAILQ_FOREACH(r, &screens[i].rl, entry)
12108 stack(r);
12109
12110 xcb_ungrab_server(conn);
12111 xcb_flush(conn);
12112
12113 /* Update state and bar of each newly mapped workspace. */
12114 for (i = 0; i < num_screens; i++)
12115 TAILQ_FOREACH(r, &screens[i].rl, entry) {
12116 r->ws->state = SWM_WS_STATE_MAPPED;
12117 bar_draw(r->bar);
12118 }
12119
12120 memset(&pfd, 0, sizeof(pfd));
12121 pfd[0].fd = xfd;
12122 pfd[0].events = POLLIN;
12123 pfd[1].fd = STDIN_FILENO;
12124 pfd[1].events = POLLIN;
12125
12126 while (running) {
12127 while ((evt = get_next_event(false))) {
12128 if (!running)
12129 goto done;
12130 event_handle(evt);
12131 free(evt);
12132 }
12133
12134 /* If just (re)started, set default focus if needed. */
12135 if (startup) {
12136 startup = false;
12137
12138 if (focus_mode != SWM_FOCUS_FOLLOW) {
12139 r = TAILQ_FIRST(&screens[0].rl);
12140 if (r) {
12141 focus_region(r);
12142 focus_flush();
12143 }
12144 continue;
12145 }
12146 }
12147
12148 if (search_resp)
12149 search_do_resp();
12150
12151 num_readable = poll(pfd, bar_extra ? 2 : 1, 1000);
12152 if (num_readable == -1) {
12153 DNPRINTF(SWM_D_MISC, "poll failed: %s",
12154 strerror(errno));
12155 } else if (num_readable > 0 && bar_extra &&
12156 pfd[1].revents & POLLIN) {
12157 stdin_ready = true;
12158 }
12159
12160 if (restart_wm)
12161 restart(NULL, NULL, NULL);
12162
12163 if (!running)
12164 goto done;
12165
12166 if (stdin_ready) {
12167 stdin_ready = false;
12168 bar_extra_update();
12169 }
12170
12171 /* Need to ensure the bar(s) are always updated. */
12172 for (i = 0; i < num_screens; i++)
12173 TAILQ_FOREACH(r, &screens[i].rl, entry)
12174 bar_draw(r->bar);
12175
12176 xcb_flush(conn);
12177 }
12178 done:
12179 shutdown_cleanup();
12180
12181 return (0);
12182 }