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