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