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