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