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