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