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