]> code.delx.au - spectrwm/blob - spectrwm.c
d3ea727f1ee003c5c2330e3b8888c54ea7bf2790
[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 if (!(r && r->s))
4170 return;
4171
4172 if (wsid >= workspace_limit)
4173 return;
4174
4175 this_r = r;
4176 old_ws = this_r->ws;
4177 new_ws = &this_r->s->ws[wsid];
4178
4179 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
4180 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
4181
4182 if (new_ws == NULL || old_ws == NULL)
4183 return;
4184 if (new_ws == old_ws)
4185 return;
4186
4187 other_r = new_ws->r;
4188 if (other_r && workspace_clamp &&
4189 bp->action != FN_RG_MOVE_NEXT && bp->action != FN_RG_MOVE_PREV) {
4190 DNPRINTF(SWM_D_WS, "switchws: ws clamped.\n");
4191
4192 if (warp_focus) {
4193 DNPRINTF(SWM_D_WS, "switchws: warping focus to region "
4194 "with ws %d.\n", wsid);
4195 focus_region(other_r);
4196 center_pointer(other_r);
4197 focus_flush();
4198 }
4199 return;
4200 }
4201
4202 if ((win = old_ws->focus) != NULL) {
4203 draw_frame(win);
4204
4205 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
4206 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
4207 &none);
4208 }
4209
4210 if (other_r) {
4211 /* the other ws is visible in another region, exchange them */
4212 other_r->ws_prior = new_ws;
4213 other_r->ws = old_ws;
4214 old_ws->r = other_r;
4215 } else {
4216 /* the other workspace is hidden, hide this one */
4217 old_ws->r = NULL;
4218 unmap_old = true;
4219 }
4220
4221 this_r->ws_prior = old_ws;
4222 this_r->ws = new_ws;
4223 new_ws->r = this_r;
4224
4225 /* Set focus_pending before stacking, if needed. */
4226 if (focus_mode != SWM_FOCUS_FOLLOW && (!new_ws->focus_pending ||
4227 validate_win(new_ws->focus_pending))) {
4228 new_ws->focus_pending = get_region_focus(new_ws->r);
4229 new_ws->focus = new_ws->focus_prev;
4230 new_ws->focus_prev = NULL;
4231 }
4232
4233 new_ws->state = SWM_WS_STATE_MAPPING;
4234
4235 stack(other_r);
4236 stack(this_r);
4237
4238 /* unmap old windows */
4239 if (unmap_old) {
4240 TAILQ_FOREACH(win, &old_ws->winlist, entry)
4241 unmap_window(win);
4242 old_ws->state = SWM_WS_STATE_HIDDEN;
4243 }
4244
4245 /* if workspaces were swapped, then don't wait to set focus */
4246 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
4247 if (new_ws->focus_pending) {
4248 focus_win(new_ws->focus_pending);
4249 new_ws->focus_pending = NULL;
4250 }
4251 }
4252
4253 /* Clear bar and set focus on region input win if new ws is empty. */
4254 if (new_ws->focus_pending == NULL && new_ws->focus == NULL) {
4255 DNPRINTF(SWM_D_FOCUS, "switchws: set_input_focus: %#x, "
4256 "revert-to: parent, time: 0\n", r->id);
4257 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, r->id,
4258 XCB_CURRENT_TIME);
4259 bar_draw(r->bar);
4260 }
4261
4262 ewmh_update_current_desktop();
4263
4264 center_pointer(r);
4265 focus_flush();
4266 new_ws->state = SWM_WS_STATE_MAPPED;
4267
4268 DNPRINTF(SWM_D_WS, "switchws: done.\n");
4269 }
4270
4271 void
4272 cyclews(struct binding *bp, struct swm_region *r, union arg *args)
4273 {
4274 union arg a;
4275 struct swm_screen *s = r->s;
4276 bool cycle_all = false, mv = false;
4277
4278 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4279 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
4280
4281 a.id = r->ws->idx;
4282
4283 do {
4284 switch (args->id) {
4285 case SWM_ARG_ID_CYCLEWS_MOVE_UP:
4286 mv = true;
4287 /* FALLTHROUGH */
4288 case SWM_ARG_ID_CYCLEWS_UP_ALL:
4289 cycle_all = true;
4290 /* FALLTHROUGH */
4291 case SWM_ARG_ID_CYCLEWS_UP:
4292 a.id = (a.id < workspace_limit - 1) ? a.id + 1 : 0;
4293 break;
4294 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN:
4295 mv = true;
4296 /* FALLTHROUGH */
4297 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
4298 cycle_all = true;
4299 /* FALLTHROUGH */
4300 case SWM_ARG_ID_CYCLEWS_DOWN:
4301 a.id = (a.id > 0) ? a.id - 1 : workspace_limit - 1;
4302 break;
4303 default:
4304 return;
4305 };
4306
4307 if (!cycle_all &&
4308 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
4309 continue;
4310 if (!cycle_visible && s->ws[a.id].r != NULL)
4311 continue;
4312
4313 if (mv)
4314 send_to_ws(bp, r, &a);
4315
4316 switchws(bp, r, &a);
4317 } while (a.id != r->ws->idx);
4318
4319 DNPRINTF(SWM_D_FOCUS, "cyclews: done\n");
4320 }
4321
4322 void
4323 priorws(struct binding *bp, struct swm_region *r, union arg *args)
4324 {
4325 union arg a;
4326
4327 (void)args;
4328
4329 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4330 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
4331
4332 if (r->ws_prior == NULL)
4333 return;
4334
4335 a.id = r->ws_prior->idx;
4336 switchws(bp, r, &a);
4337 DNPRINTF(SWM_D_FOCUS, "priorws: done\n");
4338 }
4339
4340 void
4341 focusrg(struct binding *bp, struct swm_region *r, union arg *args)
4342 {
4343 int ridx = args->id, i, num_screens;
4344 struct swm_region *rr = NULL;
4345
4346 (void)bp;
4347
4348 num_screens = get_screen_count();
4349 /* do nothing if we don't have more than one screen */
4350 if (!(num_screens > 1 || outputs > 1))
4351 return;
4352
4353 DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
4354
4355 rr = TAILQ_FIRST(&r->s->rl);
4356 for (i = 0; i < ridx && rr != NULL; ++i)
4357 rr = TAILQ_NEXT(rr, entry);
4358
4359 if (rr == NULL)
4360 return;
4361
4362 focus_region(rr);
4363 center_pointer(rr);
4364 focus_flush();
4365 DNPRINTF(SWM_D_FOCUS, "focusrg: done\n");
4366 }
4367
4368 void
4369 cyclerg(struct binding *bp, struct swm_region *r, union arg *args)
4370 {
4371 union arg a;
4372 struct swm_region *rr = NULL;
4373 int i, num_screens;
4374
4375 num_screens = get_screen_count();
4376 /* do nothing if we don't have more than one screen */
4377 if (!(num_screens > 1 || outputs > 1))
4378 return;
4379
4380 i = r->s->idx;
4381 DNPRINTF(SWM_D_FOCUS, "cyclerg: id: %d, region: %d\n", args->id, i);
4382
4383 switch (args->id) {
4384 case SWM_ARG_ID_CYCLERG_UP:
4385 case SWM_ARG_ID_CYCLERG_MOVE_UP:
4386 rr = TAILQ_NEXT(r, entry);
4387 if (rr == NULL)
4388 rr = TAILQ_FIRST(&screens[i].rl);
4389 break;
4390 case SWM_ARG_ID_CYCLERG_DOWN:
4391 case SWM_ARG_ID_CYCLERG_MOVE_DOWN:
4392 rr = TAILQ_PREV(r, swm_region_list, entry);
4393 if (rr == NULL)
4394 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
4395 break;
4396 default:
4397 return;
4398 };
4399 if (rr == NULL)
4400 return;
4401
4402 switch (args->id) {
4403 case SWM_ARG_ID_CYCLERG_UP:
4404 case SWM_ARG_ID_CYCLERG_DOWN:
4405 focus_region(rr);
4406 center_pointer(rr);
4407 focus_flush();
4408 break;
4409 case SWM_ARG_ID_CYCLERG_MOVE_UP:
4410 case SWM_ARG_ID_CYCLERG_MOVE_DOWN:
4411 a.id = rr->ws->idx;
4412 switchws(bp, r, &a);
4413 break;
4414 default:
4415 return;
4416 };
4417
4418 DNPRINTF(SWM_D_FOCUS, "cyclerg: done\n");
4419 }
4420
4421 /* Sorts transients after parent. */
4422 void
4423 sort_windows(struct ws_win_list *wl)
4424 {
4425 struct ws_win *win, *parent, *nxt;
4426
4427 if (wl == NULL)
4428 return;
4429
4430 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
4431 nxt = TAILQ_NEXT(win, entry);
4432 if (TRANS(win)) {
4433 parent = find_window(win->transient);
4434 if (parent == NULL) {
4435 warnx("not possible bug");
4436 continue;
4437 }
4438 TAILQ_REMOVE(wl, win, entry);
4439 TAILQ_INSERT_AFTER(wl, parent, win, entry);
4440 }
4441 }
4442 }
4443
4444 void
4445 swapwin(struct binding *bp, struct swm_region *r, union arg *args)
4446 {
4447 struct ws_win *target, *source;
4448 struct ws_win *cur_focus;
4449 struct ws_win_list *wl;
4450
4451 (void)bp;
4452
4453 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4454 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
4455
4456 cur_focus = r->ws->focus;
4457 if (cur_focus == NULL || FULLSCREEN(cur_focus))
4458 return;
4459
4460 /* Adjust stacking in floating layer. */
4461 if (ABOVE(cur_focus)) {
4462 switch (args->id) {
4463 case SWM_ARG_ID_SWAPPREV:
4464 target = TAILQ_PREV(cur_focus, ws_win_stack,
4465 stack_entry);
4466 if (target != NULL && FLOATING(target)) {
4467 TAILQ_REMOVE(&cur_focus->ws->stack, cur_focus,
4468 stack_entry);
4469 TAILQ_INSERT_BEFORE(target, cur_focus,
4470 stack_entry);
4471 update_win_stacking(cur_focus);
4472 focus_flush();
4473 }
4474 break;
4475 case SWM_ARG_ID_SWAPNEXT:
4476 target = TAILQ_NEXT(cur_focus, stack_entry);
4477 if (target != NULL && FLOATING(target)) {
4478 TAILQ_REMOVE(&cur_focus->ws->stack, cur_focus,
4479 stack_entry);
4480 TAILQ_INSERT_AFTER(&cur_focus->ws->stack,
4481 target, cur_focus, stack_entry);
4482 update_win_stacking(cur_focus);
4483 focus_flush();
4484 }
4485 break;
4486 }
4487 goto out;
4488 }
4489
4490 if (r->ws->cur_layout == &layouts[SWM_MAX_STACK])
4491 return;
4492
4493 clear_maximized(r->ws);
4494
4495 source = cur_focus;
4496 wl = &source->ws->winlist;
4497
4498 switch (args->id) {
4499 case SWM_ARG_ID_SWAPPREV:
4500 if (TRANS(source))
4501 source = find_window(source->transient);
4502 target = TAILQ_PREV(source, ws_win_list, entry);
4503 if (target && target->transient)
4504 target = find_window(target->transient);
4505 TAILQ_REMOVE(wl, source, entry);
4506 if (target == NULL)
4507 TAILQ_INSERT_TAIL(wl, source, entry);
4508 else
4509 TAILQ_INSERT_BEFORE(target, source, entry);
4510 break;
4511 case SWM_ARG_ID_SWAPNEXT:
4512 target = TAILQ_NEXT(source, entry);
4513 /* move the parent and let the sort handle the move */
4514 if (TRANS(source))
4515 source = find_window(source->transient);
4516 TAILQ_REMOVE(wl, source, entry);
4517 if (target == NULL)
4518 TAILQ_INSERT_HEAD(wl, source, entry);
4519 else
4520 TAILQ_INSERT_AFTER(wl, target, source, entry);
4521 break;
4522 case SWM_ARG_ID_SWAPMAIN:
4523 target = TAILQ_FIRST(wl);
4524 if (target == source) {
4525 if (source->ws->focus_prev != NULL &&
4526 source->ws->focus_prev != target)
4527 source = source->ws->focus_prev;
4528 else
4529 return;
4530 }
4531 if (target == NULL || source == NULL)
4532 return;
4533 source->ws->focus_prev = target;
4534 TAILQ_REMOVE(wl, target, entry);
4535 TAILQ_INSERT_BEFORE(source, target, entry);
4536 TAILQ_REMOVE(wl, source, entry);
4537 TAILQ_INSERT_HEAD(wl, source, entry);
4538 break;
4539 case SWM_ARG_ID_MOVELAST:
4540 TAILQ_REMOVE(wl, source, entry);
4541 TAILQ_INSERT_TAIL(wl, source, entry);
4542 break;
4543 default:
4544 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
4545 return;
4546 }
4547
4548 sort_windows(wl);
4549 ewmh_update_client_list();
4550
4551 stack(r);
4552 center_pointer(r);
4553 focus_flush();
4554 out:
4555 DNPRINTF(SWM_D_MOVE, "swapwin: done\n");
4556 }
4557
4558 struct ws_win *
4559 get_focus_prev(struct ws_win *win)
4560 {
4561 struct ws_win *winfocus = NULL;
4562 struct ws_win *cur_focus = NULL;
4563 struct ws_win_list *wl = NULL;
4564 struct workspace *ws = NULL;
4565
4566 if (!(win && win->ws))
4567 return NULL;
4568
4569 ws = win->ws;
4570 wl = &ws->winlist;
4571 cur_focus = ws->focus;
4572
4573 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: win %#x, cur_focus: %#x, "
4574 "focus_prev: %#x\n", WINID(win), WINID(cur_focus),
4575 WINID(ws->focus_prev));
4576
4577 /* pickle, just focus on whatever */
4578 if (cur_focus == NULL) {
4579 /* use prev_focus if valid */
4580 if (ws->focus_prev && find_window(ws->focus_prev->id))
4581 winfocus = ws->focus_prev;
4582 goto done;
4583 }
4584
4585 /* if transient focus on parent */
4586 if (TRANS(cur_focus)) {
4587 winfocus = find_window(cur_focus->transient);
4588 goto done;
4589 }
4590
4591 /* if in max_stack try harder */
4592 if ((win->quirks & SWM_Q_FOCUSPREV) ||
4593 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
4594 if (cur_focus != ws->focus_prev)
4595 winfocus = ws->focus_prev;
4596 else
4597 winfocus = TAILQ_PREV(win, ws_win_list, entry);
4598 if (winfocus)
4599 goto done;
4600 }
4601
4602 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
4603
4604 if (winfocus == NULL || winfocus == win) {
4605 switch (focus_close) {
4606 case SWM_STACK_BOTTOM:
4607 TAILQ_FOREACH(winfocus, wl, entry)
4608 if (!ICONIC(winfocus) && winfocus != cur_focus)
4609 break;
4610 break;
4611 case SWM_STACK_TOP:
4612 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4613 if (!ICONIC(winfocus) && winfocus != cur_focus)
4614 break;
4615 break;
4616 case SWM_STACK_ABOVE:
4617 winfocus = TAILQ_NEXT(cur_focus, entry);
4618 while (winfocus && ICONIC(winfocus))
4619 winfocus = TAILQ_NEXT(winfocus, entry);
4620
4621 if (winfocus == NULL) {
4622 if (focus_close_wrap) {
4623 TAILQ_FOREACH(winfocus, wl, entry)
4624 if (!ICONIC(winfocus) &&
4625 winfocus != cur_focus)
4626 break;
4627 } else {
4628 TAILQ_FOREACH_REVERSE(winfocus, wl,
4629 ws_win_list, entry)
4630 if (!ICONIC(winfocus) &&
4631 winfocus != cur_focus)
4632 break;
4633 }
4634 }
4635 break;
4636 case SWM_STACK_BELOW:
4637 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
4638 while (winfocus && ICONIC(winfocus))
4639 winfocus = TAILQ_PREV(winfocus, ws_win_list,
4640 entry);
4641
4642 if (winfocus == NULL) {
4643 if (focus_close_wrap) {
4644 TAILQ_FOREACH_REVERSE(winfocus, wl,
4645 ws_win_list, entry)
4646 if (!ICONIC(winfocus) &&
4647 winfocus != cur_focus)
4648 break;
4649 } else {
4650 TAILQ_FOREACH(winfocus, wl, entry)
4651 if (!ICONIC(winfocus) &&
4652 winfocus != cur_focus)
4653 break;
4654 }
4655 }
4656 break;
4657 }
4658 }
4659 done:
4660 if (winfocus == NULL ||
4661 (winfocus && (ICONIC(winfocus) || winfocus == cur_focus))) {
4662 if (focus_default == SWM_STACK_TOP) {
4663 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4664 if (!ICONIC(winfocus) && winfocus != cur_focus)
4665 break;
4666 } else {
4667 TAILQ_FOREACH(winfocus, wl, entry)
4668 if (!ICONIC(winfocus) && winfocus != cur_focus)
4669 break;
4670 }
4671 }
4672
4673 kill_refs(win);
4674
4675 return get_focus_magic(winfocus);
4676 }
4677
4678 struct ws_win *
4679 get_region_focus(struct swm_region *r)
4680 {
4681 struct ws_win *winfocus = NULL;
4682
4683 if (!(r && r->ws))
4684 return NULL;
4685
4686 if (r->ws->focus && !ICONIC(r->ws->focus))
4687 winfocus = r->ws->focus;
4688 else if (r->ws->focus_prev && !ICONIC(r->ws->focus_prev))
4689 winfocus = r->ws->focus_prev;
4690 else
4691 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
4692 if (!ICONIC(winfocus))
4693 break;
4694
4695 return get_focus_magic(winfocus);
4696 }
4697
4698 void
4699 focus(struct binding *bp, struct swm_region *r, union arg *args)
4700 {
4701 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
4702 struct ws_win_list *wl = NULL;
4703 struct workspace *ws = NULL;
4704 union arg a;
4705 int i;
4706
4707 if (!(r && r->ws))
4708 goto out;
4709
4710 cur_focus = r->ws->focus;
4711 ws = r->ws;
4712 wl = &ws->winlist;
4713
4714 DNPRINTF(SWM_D_FOCUS, "focus: id: %d, cur_focus: %#x\n", args->id,
4715 WINID(cur_focus));
4716
4717 /* Make sure an uniconified window has focus, if one exists. */
4718 if (cur_focus == NULL) {
4719 cur_focus = TAILQ_FIRST(wl);
4720 while (cur_focus != NULL && ICONIC(cur_focus))
4721 cur_focus = TAILQ_NEXT(cur_focus, entry);
4722
4723 DNPRINTF(SWM_D_FOCUS, "focus: new cur_focus: %#x\n",
4724 WINID(cur_focus));
4725 }
4726
4727 switch (args->id) {
4728 case SWM_ARG_ID_FOCUSPREV:
4729 if (cur_focus == NULL)
4730 goto out;
4731
4732 winfocus = cur_focus;
4733 do {
4734 winfocus = TAILQ_PREV(winfocus, ws_win_list, entry);
4735 if (winfocus == NULL)
4736 winfocus = TAILQ_LAST(wl, ws_win_list);
4737 if (winfocus == cur_focus)
4738 break;
4739 } while (winfocus && (ICONIC(winfocus) ||
4740 winfocus->id == cur_focus->transient ||
4741 (cur_focus->transient != XCB_WINDOW_NONE &&
4742 winfocus->transient == cur_focus->transient) ||
4743 (winfocus->quirks & SWM_Q_NOFOCUSCYCLE)));
4744 break;
4745 case SWM_ARG_ID_FOCUSNEXT:
4746 if (cur_focus == NULL)
4747 goto out;
4748
4749 winfocus = cur_focus;
4750 do {
4751 winfocus = TAILQ_NEXT(winfocus, entry);
4752 if (winfocus == NULL)
4753 winfocus = TAILQ_FIRST(wl);
4754 if (winfocus == cur_focus)
4755 break;
4756 } while (winfocus && (ICONIC(winfocus) ||
4757 winfocus->id == cur_focus->transient ||
4758 (cur_focus->transient != XCB_WINDOW_NONE &&
4759 winfocus->transient == cur_focus->transient) ||
4760 (winfocus->quirks & SWM_Q_NOFOCUSCYCLE)));
4761 break;
4762 case SWM_ARG_ID_FOCUSMAIN:
4763 if (cur_focus == NULL)
4764 goto out;
4765
4766 winfocus = TAILQ_FIRST(wl);
4767 if (winfocus == cur_focus)
4768 winfocus = cur_focus->ws->focus_prev;
4769 break;
4770 case SWM_ARG_ID_FOCUSURGENT:
4771 /* Search forward for the next urgent window. */
4772 winfocus = NULL;
4773 head = cur_focus;
4774
4775 for (i = 0; i <= workspace_limit; ++i) {
4776 if (head == NULL)
4777 head = TAILQ_FIRST(&r->s->ws[(ws->idx + i) %
4778 workspace_limit].winlist);
4779
4780 while (head) {
4781 if (head == cur_focus) {
4782 if (i > 0) {
4783 winfocus = cur_focus;
4784 break;
4785 }
4786 } else if (get_urgent(head)) {
4787 winfocus = head;
4788 break;
4789 }
4790
4791 head = TAILQ_NEXT(head, entry);
4792 }
4793
4794 if (winfocus)
4795 break;
4796 }
4797
4798 /* Switch ws if new focus is on a different ws. */
4799 if (winfocus && winfocus->ws != ws) {
4800 a.id = winfocus->ws->idx;
4801 switchws(bp, r, &a);
4802 }
4803 break;
4804 default:
4805 goto out;
4806 }
4807
4808 if (clear_maximized(ws) > 0)
4809 stack(r);
4810
4811 focus_win(get_focus_magic(winfocus));
4812 center_pointer(r);
4813 focus_flush();
4814
4815 out:
4816 DNPRINTF(SWM_D_FOCUS, "focus: done\n");
4817 }
4818
4819 void
4820 focus_pointer(struct binding *bp, struct swm_region *r, union arg *args)
4821 {
4822 (void)args;
4823
4824 /* Not needed for buttons since this is already done in buttonpress. */
4825 if (bp->type == KEYBIND) {
4826 focus_win(get_pointer_win(r->s->root));
4827 focus_flush();
4828 }
4829 }
4830
4831 void
4832 cycle_layout(struct binding *bp, struct swm_region *r, union arg *args)
4833 {
4834 struct workspace *ws = r->ws;
4835
4836 /* suppress unused warning since var is needed */
4837 (void)bp;
4838 (void)args;
4839
4840 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
4841
4842 ws->cur_layout++;
4843 if (ws->cur_layout->l_stack == NULL)
4844 ws->cur_layout = &layouts[0];
4845
4846 clear_maximized(ws);
4847
4848 stack(r);
4849 bar_draw(r->bar);
4850
4851 focus_win(get_region_focus(r));
4852
4853 center_pointer(r);
4854 focus_flush();
4855 }
4856
4857 void
4858 stack_config(struct binding *bp, struct swm_region *r, union arg *args)
4859 {
4860 struct workspace *ws = r->ws;
4861
4862 (void)bp;
4863
4864 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
4865 args->id, ws->idx);
4866
4867 if (clear_maximized(ws) > 0)
4868 stack(r);
4869
4870 if (ws->cur_layout->l_config != NULL)
4871 ws->cur_layout->l_config(ws, args->id);
4872
4873 if (args->id != SWM_ARG_ID_STACKINIT)
4874 stack(r);
4875 bar_draw(r->bar);
4876
4877 center_pointer(r);
4878 focus_flush();
4879 }
4880
4881 void
4882 stack(struct swm_region *r) {
4883 struct swm_geometry g;
4884 struct swm_region *r_prev;
4885 uint32_t val[2];
4886
4887 if (r == NULL)
4888 return;
4889
4890 DNPRINTF(SWM_D_STACK, "stack: begin\n");
4891
4892 /* Adjust stack area for region bar and padding. */
4893 g = r->g;
4894 g.x += region_padding;
4895 g.y += region_padding;
4896 g.w -= 2 * border_width + 2 * region_padding;
4897 g.h -= 2 * border_width + 2 * region_padding;
4898 if (bar_enabled && r->ws->bar_enabled) {
4899 if (!bar_at_bottom)
4900 g.y += bar_height;
4901 g.h -= bar_height;
4902 }
4903
4904 DNPRINTF(SWM_D_STACK, "stack: workspace: %d (screen: "
4905 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4906 r->ws->idx, r->s->idx, get_region_index(r), g.x, g.y, g.w, g.h);
4907
4908 r_prev = TAILQ_PREV(r, swm_region_list, entry);
4909 if (r_prev) {
4910 /* Stack bar/input relative to prev. region. */
4911 val[1] = XCB_STACK_MODE_ABOVE;
4912
4913 val[0] = r_prev->id;
4914 DNPRINTF(SWM_D_STACK, "stack: region input %#x "
4915 "relative to %#x.\n", r->id, val[0]);
4916 xcb_configure_window(conn, r->id,
4917 XCB_CONFIG_WINDOW_SIBLING |
4918 XCB_CONFIG_WINDOW_STACK_MODE, val);
4919
4920 val[0] = r_prev->bar->id;
4921 DNPRINTF(SWM_D_STACK, "stack: region bar %#x "
4922 "relative to %#x.\n", r->bar->id, val[0]);
4923 xcb_configure_window(conn, r->bar->id,
4924 XCB_CONFIG_WINDOW_SIBLING |
4925 XCB_CONFIG_WINDOW_STACK_MODE, val);
4926 }
4927
4928 r->ws->cur_layout->l_stack(r->ws, &g);
4929 r->ws->cur_layout->l_string(r->ws);
4930 /* save r so we can track region changes */
4931 r->ws->old_r = r;
4932
4933 if (font_adjusted)
4934 font_adjusted--;
4935
4936 DNPRINTF(SWM_D_STACK, "stack: end\n");
4937 }
4938
4939 void
4940 store_float_geom(struct ws_win *win)
4941 {
4942 if (win == NULL || win->ws->r == NULL)
4943 return;
4944
4945 /* retain window geom and region geom */
4946 win->g_float = win->g;
4947 win->g_float.x -= X(win->ws->r);
4948 win->g_float.y -= Y(win->ws->r);
4949 win->g_floatvalid = true;
4950 DNPRINTF(SWM_D_MISC, "store_float_geom: win %#x, g: (%d,%d)"
4951 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
4952 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
4953 win->g_float.w, win->g_float.h);
4954 }
4955
4956 void
4957 load_float_geom(struct ws_win *win)
4958 {
4959 if (win == NULL || win->ws->r == NULL)
4960 return;
4961
4962 if (win->g_floatvalid) {
4963 win->g = win->g_float;
4964 X(win) += X(win->ws->r);
4965 Y(win) += Y(win->ws->r);
4966 DNPRINTF(SWM_D_MISC, "load_float_geom: win %#x, g: (%d,%d)"
4967 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
4968 HEIGHT(win));
4969 } else {
4970 DNPRINTF(SWM_D_MISC, "load_float_geom: win %#x, g_float "
4971 "is not set.\n", win->id);
4972 }
4973 }
4974
4975 void
4976 update_floater(struct ws_win *win)
4977 {
4978 struct workspace *ws;
4979 struct swm_region *r;
4980
4981 DNPRINTF(SWM_D_MISC, "update_floater: win %#x\n", WINID(win));
4982
4983 if (win == NULL)
4984 return;
4985
4986 ws = win->ws;
4987
4988 if ((r = ws->r) == NULL)
4989 return;
4990
4991 win->bordered = true;
4992
4993 if (FULLSCREEN(win)) {
4994 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4995 if (!win->g_floatvalid)
4996 store_float_geom(win);
4997
4998 win->g = r->g;
4999 win->bordered = false;
5000 } else if (MAXIMIZED(win)) {
5001 /* Maximize: like a single stacked window. */
5002 if (!win->g_floatvalid)
5003 store_float_geom(win);
5004
5005 win->g = r->g;
5006
5007 if (bar_enabled && ws->bar_enabled && !maximize_hide_bar) {
5008 if (!bar_at_bottom)
5009 Y(win) += bar_height;
5010 HEIGHT(win) -= bar_height;
5011 } else if (disable_border) {
5012 win->bordered = false;
5013 }
5014
5015 if (win->bordered) {
5016 /* Window geometry excludes frame. */
5017 X(win) += border_width;
5018 Y(win) += border_width;
5019 HEIGHT(win) -= 2 * border_width;
5020 WIDTH(win) -= 2 * border_width;
5021 }
5022 } else {
5023 /* Normal floating window. */
5024 /* Update geometry if new region. */
5025 if (r != ws->old_r)
5026 load_float_geom(win);
5027
5028 if (((win->quirks & SWM_Q_FULLSCREEN) &&
5029 WIDTH(win) >= WIDTH(r) && HEIGHT(win) >= HEIGHT(r)) ||
5030 ((!WS_FOCUSED(win->ws) || win->ws->focus != win) &&
5031 (win->quirks & SWM_Q_MINIMALBORDER))) {
5032 /* Remove border */
5033 win->bordered = false;
5034 } else if (!MANUAL(win)) {
5035 if (TRANS(win) && (win->quirks & SWM_Q_TRANSSZ)) {
5036 /* Adjust size on TRANSSZ quirk. */
5037 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
5038 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
5039 }
5040
5041 if (!(win->quirks & SWM_Q_ANYWHERE)) {
5042 /*
5043 * Floaters and transients are auto-centred
5044 * unless manually moved, resized or ANYWHERE
5045 * quirk is set.
5046 */
5047 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2;
5048 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2;
5049 store_float_geom(win);
5050 }
5051 }
5052 }
5053
5054 /* Ensure at least 1 pixel of the window is in the region. */
5055 region_containment(win, r, SWM_CW_ALLSIDES);
5056
5057 update_window(win);
5058 }
5059
5060 /*
5061 * Send keystrokes to terminal to decrease/increase the font size as the
5062 * window size changes.
5063 */
5064 void
5065 adjust_font(struct ws_win *win)
5066 {
5067 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
5068 ABOVE(win) || TRANS(win))
5069 return;
5070
5071 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
5072 WIDTH(win) / win->sh.width_inc < term_width &&
5073 win->font_steps < SWM_MAX_FONT_STEPS) {
5074 win->font_size_boundary[win->font_steps] =
5075 (win->sh.width_inc * term_width) + win->sh.base_width;
5076 win->font_steps++;
5077 font_adjusted++;
5078 win->last_inc = win->sh.width_inc;
5079 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
5080 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
5081 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
5082 win->font_steps--;
5083 font_adjusted++;
5084 win->last_inc = win->sh.width_inc;
5085 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
5086 }
5087 }
5088
5089 #define SWAPXY(g) do { \
5090 int tmp; \
5091 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
5092 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
5093 } while (0)
5094 void
5095 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, bool flip)
5096 {
5097 struct swm_geometry cell, r_g = *g;
5098 struct ws_win *win;
5099 int i = 0, j = 0, s = 0, stacks = 0;
5100 int w_inc = 1, h_inc, w_base = 1, h_base;
5101 int hrh = 0, extra = 0, h_slice = 0, last_h = 0;
5102 int split = 0, colno = 0;
5103 int winno, mwin = 0, msize = 0;
5104 int remain, missing, v_slice, mscale;
5105 bool bordered = true, reconfigure = false;
5106
5107 /*
5108 * cell: geometry for window, including frame.
5109 * mwin: # of windows in master area.
5110 * mscale: size increment of master area.
5111 * stacks: # of stack columns
5112 */
5113
5114 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
5115 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
5116
5117 memset(&cell, 0, sizeof(cell));
5118
5119 /* Prepare tiling variables, if needed. */
5120 if ((winno = count_win(ws, false)) > 0) {
5121 /* Find first tiled window. */
5122 TAILQ_FOREACH(win, &ws->winlist, entry)
5123 if (!FLOATING(win) && !ICONIC(win))
5124 break;
5125
5126 /* Take into account size hints of first tiled window. */
5127 if (rot) {
5128 w_inc = win->sh.width_inc;
5129 w_base = win->sh.base_width;
5130 mwin = ws->l_state.horizontal_mwin;
5131 mscale = ws->l_state.horizontal_msize;
5132 stacks = ws->l_state.horizontal_stacks;
5133 SWAPXY(&r_g);
5134 } else {
5135 w_inc = win->sh.height_inc;
5136 w_base = win->sh.base_height;
5137 mwin = ws->l_state.vertical_mwin;
5138 mscale = ws->l_state.vertical_msize;
5139 stacks = ws->l_state.vertical_stacks;
5140 }
5141
5142 cell = r_g;
5143 cell.x += border_width;
5144 cell.y += border_width;
5145
5146 if (stacks > winno - mwin)
5147 stacks = winno - mwin;
5148 if (stacks < 1)
5149 stacks = 1;
5150
5151 h_slice = r_g.h / SWM_H_SLICE;
5152 if (mwin && winno > mwin) {
5153 v_slice = r_g.w / SWM_V_SLICE;
5154
5155 split = mwin;
5156 colno = split;
5157 cell.w = v_slice * mscale;
5158
5159 if (w_inc > 1 && w_inc < v_slice) {
5160 /* Adjust for requested size increment. */
5161 remain = (cell.w - w_base) % w_inc;
5162 cell.w -= remain;
5163 }
5164
5165 msize = cell.w;
5166 if (flip)
5167 cell.x += r_g.w - msize;
5168 } else {
5169 msize = -2;
5170 colno = split = winno / stacks;
5171 cell.w = ((r_g.w - (stacks * 2 * border_width) +
5172 2 * border_width) / stacks);
5173 }
5174
5175 hrh = r_g.h / colno;
5176 extra = r_g.h - (colno * hrh);
5177 cell.h = hrh - 2 * border_width;
5178 i = j = 0, s = stacks;
5179 }
5180
5181 /* Update window geometry. */
5182 TAILQ_FOREACH(win, &ws->winlist, entry) {
5183 if (ICONIC(win))
5184 continue;
5185
5186 if (FLOATING(win)) {
5187 update_floater(win);
5188 continue;
5189 }
5190
5191 /* Tiled. */
5192 if (split && i == split) {
5193 colno = (winno - mwin) / stacks;
5194 if (s <= (winno - mwin) % stacks)
5195 colno++;
5196 split += colno;
5197 hrh = r_g.h / colno;
5198 extra = r_g.h - (colno * hrh);
5199
5200 if (!flip)
5201 cell.x += cell.w + 2 * border_width +
5202 tile_gap;
5203
5204 cell.w = (r_g.w - msize -
5205 (stacks * (2 * border_width + tile_gap))) / stacks;
5206 if (s == 1)
5207 cell.w += (r_g.w - msize -
5208 (stacks * (2 * border_width + tile_gap))) %
5209 stacks;
5210
5211 if (flip)
5212 cell.x -= cell.w + 2 * border_width +
5213 tile_gap;
5214 s--;
5215 j = 0;
5216 }
5217
5218 cell.h = hrh - 2 * border_width - tile_gap;
5219
5220 if (rot) {
5221 h_inc = win->sh.width_inc;
5222 h_base = win->sh.base_width;
5223 } else {
5224 h_inc = win->sh.height_inc;
5225 h_base = win->sh.base_height;
5226 }
5227
5228 if (j == colno - 1) {
5229 cell.h = hrh + extra;
5230 } else if (h_inc > 1 && h_inc < h_slice) {
5231 /* adjust for window's requested size increment */
5232 remain = (cell.h - h_base) % h_inc;
5233 missing = h_inc - remain;
5234
5235 if (missing <= extra || j == 0) {
5236 extra -= missing;
5237 cell.h += missing;
5238 } else {
5239 cell.h -= remain;
5240 extra += remain;
5241 }
5242 }
5243
5244 if (j == 0)
5245 cell.y = r_g.y + border_width;
5246 else
5247 cell.y += last_h + 2 * border_width + tile_gap;
5248
5249 /* Window coordinates exclude frame. */
5250
5251 if (winno > 1 || !disable_border ||
5252 (bar_enabled && ws->bar_enabled)) {
5253 bordered = true;
5254 } else {
5255 bordered = false;
5256 }
5257
5258 if (rot) {
5259 if (X(win) != cell.y || Y(win) != cell.x ||
5260 WIDTH(win) != cell.h || HEIGHT(win) != cell.w) {
5261 reconfigure = true;
5262 X(win) = cell.y;
5263 Y(win) = cell.x;
5264 WIDTH(win) = cell.h;
5265 HEIGHT(win) = cell.w;
5266 }
5267 } else {
5268 if (X(win) != cell.x || Y(win) != cell.y ||
5269 WIDTH(win) != cell.w || HEIGHT(win) != cell.h) {
5270 reconfigure = true;
5271 X(win) = cell.x;
5272 Y(win) = cell.y;
5273 WIDTH(win) = cell.w;
5274 HEIGHT(win) = cell.h;
5275 }
5276 }
5277
5278 if (!bordered) {
5279 X(win) -= border_width;
5280 Y(win) -= border_width;
5281 WIDTH(win) += 2 * border_width;
5282 HEIGHT(win) += 2 * border_width;
5283 }
5284
5285 if (bordered != win->bordered) {
5286 reconfigure = true;
5287 win->bordered = bordered;
5288 }
5289
5290 if (reconfigure) {
5291 adjust_font(win);
5292 update_window(win);
5293 }
5294
5295 last_h = cell.h;
5296 i++;
5297 j++;
5298 }
5299
5300 /* Stack all windows from bottom up. */
5301 TAILQ_FOREACH_REVERSE(win, &ws->stack, ws_win_stack, stack_entry)
5302 if (!ICONIC(win))
5303 update_win_stacking(win);
5304
5305 /* Map all windows from top down. */
5306 TAILQ_FOREACH(win, &ws->stack, stack_entry)
5307 if (!ICONIC(win))
5308 map_window(win);
5309
5310 DNPRINTF(SWM_D_STACK, "stack_master: done\n");
5311 }
5312
5313 void
5314 vertical_config(struct workspace *ws, int id)
5315 {
5316 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
5317 id, ws->idx);
5318
5319 switch (id) {
5320 case SWM_ARG_ID_STACKRESET:
5321 case SWM_ARG_ID_STACKINIT:
5322 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
5323 ws->l_state.vertical_mwin = 1;
5324 ws->l_state.vertical_stacks = 1;
5325 break;
5326 case SWM_ARG_ID_MASTERSHRINK:
5327 if (ws->l_state.vertical_msize > 1)
5328 ws->l_state.vertical_msize--;
5329 break;
5330 case SWM_ARG_ID_MASTERGROW:
5331 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
5332 ws->l_state.vertical_msize++;
5333 break;
5334 case SWM_ARG_ID_MASTERADD:
5335 ws->l_state.vertical_mwin++;
5336 break;
5337 case SWM_ARG_ID_MASTERDEL:
5338 if (ws->l_state.vertical_mwin > 0)
5339 ws->l_state.vertical_mwin--;
5340 break;
5341 case SWM_ARG_ID_STACKBALANCE:
5342 ws->l_state.vertical_msize = SWM_V_SLICE / (ws->l_state.vertical_stacks + 1);
5343 break;
5344 case SWM_ARG_ID_STACKINC:
5345 ws->l_state.vertical_stacks++;
5346 break;
5347 case SWM_ARG_ID_STACKDEC:
5348 if (ws->l_state.vertical_stacks > 1)
5349 ws->l_state.vertical_stacks--;
5350 break;
5351 case SWM_ARG_ID_FLIPLAYOUT:
5352 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
5353 break;
5354 default:
5355 return;
5356 }
5357 }
5358
5359 void
5360 vertical_stack(struct workspace *ws, struct swm_geometry *g)
5361 {
5362 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
5363
5364 stack_master(ws, g, 0, ws->l_state.vertical_flip);
5365 }
5366
5367 void
5368 horizontal_config(struct workspace *ws, int id)
5369 {
5370 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
5371
5372 switch (id) {
5373 case SWM_ARG_ID_STACKRESET:
5374 case SWM_ARG_ID_STACKINIT:
5375 ws->l_state.horizontal_mwin = 1;
5376 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
5377 ws->l_state.horizontal_stacks = 1;
5378 break;
5379 case SWM_ARG_ID_MASTERSHRINK:
5380 if (ws->l_state.horizontal_msize > 1)
5381 ws->l_state.horizontal_msize--;
5382 break;
5383 case SWM_ARG_ID_MASTERGROW:
5384 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
5385 ws->l_state.horizontal_msize++;
5386 break;
5387 case SWM_ARG_ID_MASTERADD:
5388 ws->l_state.horizontal_mwin++;
5389 break;
5390 case SWM_ARG_ID_MASTERDEL:
5391 if (ws->l_state.horizontal_mwin > 0)
5392 ws->l_state.horizontal_mwin--;
5393 break;
5394 case SWM_ARG_ID_STACKBALANCE:
5395 ws->l_state.horizontal_msize = SWM_H_SLICE / (ws->l_state.horizontal_stacks + 1);
5396 break;
5397 case SWM_ARG_ID_STACKINC:
5398 ws->l_state.horizontal_stacks++;
5399 break;
5400 case SWM_ARG_ID_STACKDEC:
5401 if (ws->l_state.horizontal_stacks > 1)
5402 ws->l_state.horizontal_stacks--;
5403 break;
5404 case SWM_ARG_ID_FLIPLAYOUT:
5405 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
5406 break;
5407 default:
5408 return;
5409 }
5410 }
5411
5412 void
5413 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
5414 {
5415 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
5416
5417 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
5418 }
5419
5420 /* fullscreen view */
5421 void
5422 max_stack(struct workspace *ws, struct swm_geometry *g)
5423 {
5424 struct swm_geometry gg = *g;
5425 struct ws_win *w, *win = NULL, *parent = NULL, *tmpw;
5426 int winno;
5427
5428 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
5429
5430 if (ws == NULL)
5431 return;
5432
5433 winno = count_win(ws, false);
5434 if (winno == 0 && count_win(ws, true) == 0)
5435 return;
5436
5437 /* Figure out which top level window should be visible. */
5438 if (ws->focus_pending)
5439 win = ws->focus_pending;
5440 else if (ws->focus)
5441 win = ws->focus;
5442 else if (ws->focus_prev)
5443 win = ws->focus_prev;
5444 else
5445 win = TAILQ_FIRST(&ws->winlist);
5446
5447 DNPRINTF(SWM_D_STACK, "max_stack: focus_pending: %#x, focus: %#x, "
5448 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws->focus_pending),
5449 WINID(ws->focus), WINID(ws->focus_prev),
5450 WINID(TAILQ_FIRST(&ws->winlist)), win->id);
5451
5452 /* Update window geometry. */
5453 TAILQ_FOREACH(w, &ws->winlist, entry) {
5454 if (ICONIC(w))
5455 continue;
5456
5457 if (TRANS(w)) {
5458 update_floater(w);
5459 continue;
5460 }
5461
5462 /* Set maximized flag for all maxed windows. */
5463 if (!MAXIMIZED(w)) {
5464 /* Preserve floating geometry. */
5465 if (ABOVE(w))
5466 store_float_geom(w);
5467
5468 ewmh_apply_flags(w, w->ewmh_flags | EWMH_F_MAXIMIZED);
5469 ewmh_update_wm_state(w);
5470 }
5471
5472 /* Only reconfigure if necessary. */
5473 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
5474 HEIGHT(w) != gg.h) {
5475 w->g = gg;
5476
5477 if (disable_border && !(bar_enabled && ws->bar_enabled)) {
5478 w->bordered = false;
5479 WIDTH(w) += 2 * border_width;
5480 HEIGHT(w) += 2 * border_width;
5481 } else {
5482 w->bordered = true;
5483 X(w) += border_width;
5484 Y(w) += border_width;
5485 }
5486
5487 update_window(w);
5488 }
5489 }
5490
5491 /* If transient, stack parent and its children. */
5492 if (TRANS(win) && (parent = find_window(win->transient))) {
5493 raise_window(parent);
5494
5495 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
5496 if (w->transient == parent->id)
5497 raise_window(w);
5498 }
5499
5500 /* Make sure focus window is on top. */
5501 raise_window(win);
5502
5503 /* Stack any children of focus window. */
5504 TAILQ_FOREACH_SAFE(w, &ws->stack, stack_entry, tmpw)
5505 if (w->transient == win->id)
5506 raise_window(w);
5507
5508 /* Map all windows. */
5509 TAILQ_FOREACH(w, &ws->stack, stack_entry)
5510 if (!ICONIC(w))
5511 map_window(w);
5512 }
5513
5514 void
5515 send_to_rg(struct binding *bp, struct swm_region *r, union arg *args)
5516 {
5517 int ridx = args->id, i, num_screens;
5518 struct swm_region *rr = NULL;
5519 union arg a;
5520
5521 num_screens = get_screen_count();
5522 /* do nothing if we don't have more than one screen */
5523 if (!(num_screens > 1 || outputs > 1))
5524 return;
5525
5526 DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
5527
5528 rr = TAILQ_FIRST(&r->s->rl);
5529 for (i = 0; i < ridx && rr != NULL; ++i)
5530 rr = TAILQ_NEXT(rr, entry);
5531
5532 if (rr == NULL)
5533 return;
5534
5535 a.id = rr->ws->idx;
5536
5537 send_to_ws(bp, r, &a);
5538 }
5539
5540 struct swm_region *
5541 region_under(struct swm_screen *s, int x, int y)
5542 {
5543 struct swm_region *r = NULL;
5544
5545 TAILQ_FOREACH(r, &s->rl, entry) {
5546 DNPRINTF(SWM_D_MISC, "region_under: ws: %d, region g: (%d,%d) "
5547 "%d x %d, coords: (%d,%d)\n", r->ws->idx, X(r), Y(r),
5548 WIDTH(r), HEIGHT(r), x, y);
5549 if (X(r) <= x && x < MAX_X(r))
5550 if (Y(r) <= y && y < MAX_Y(r))
5551 return (r);
5552 }
5553
5554 return (NULL);
5555 }
5556
5557 /* Transfer focused window to target workspace and focus. */
5558 void
5559 send_to_ws(struct binding *bp, struct swm_region *r, union arg *args)
5560 {
5561 int wsid = args->id;
5562 struct ws_win *win = NULL;
5563
5564 (void)bp;
5565
5566 if (r && r->ws && r->ws->focus)
5567 win = r->ws->focus;
5568 else
5569 return;
5570
5571 DNPRINTF(SWM_D_MOVE, "send_to_ws: win %#x, ws %d\n", win->id, wsid);
5572
5573 if (wsid < 0 || wsid >= workspace_limit)
5574 return;
5575
5576 if (win->ws->idx == wsid)
5577 return;
5578
5579 win_to_ws(win, wsid, true);
5580
5581 /* Set new focus on target ws. */
5582 if (focus_mode != SWM_FOCUS_FOLLOW) {
5583 win->ws->focus_prev = win->ws->focus;
5584 win->ws->focus = win;
5585 win->ws->focus_pending = NULL;
5586
5587 if (win->ws->focus_prev)
5588 draw_frame(win->ws->focus_prev);
5589 }
5590
5591 DNPRINTF(SWM_D_STACK, "send_to_ws: focus_pending: %#x, focus: %#x, "
5592 "focus_prev: %#x, first: %#x, win: %#x\n",
5593 WINID(r->ws->focus_pending), WINID(r->ws->focus),
5594 WINID(r->ws->focus_prev), WINID(TAILQ_FIRST(&r->ws->winlist)),
5595 win->id);
5596
5597 ewmh_apply_flags(win, win->ewmh_flags & ~EWMH_F_MAXIMIZED);
5598 ewmh_update_wm_state(win);
5599
5600 /* Restack focused region. */
5601 stack(r);
5602
5603 /* If destination ws has a region, restack. */
5604 if (win->ws->r) {
5605 if (FLOATING(win))
5606 load_float_geom(win);
5607
5608 stack(win->ws->r);
5609 }
5610
5611 /* Set new focus on current ws. */
5612 if (focus_mode != SWM_FOCUS_FOLLOW) {
5613 if (r->ws->focus != NULL) {
5614 focus_win(r->ws->focus);
5615 } else {
5616 DNPRINTF(SWM_D_FOCUS, "send_to_ws: set_input_focus: "
5617 "%#x, revert-to: parent, time: 0\n", r->id);
5618 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
5619 r->id, XCB_CURRENT_TIME);
5620 bar_draw(r->bar);
5621 }
5622 }
5623
5624 center_pointer(r);
5625 focus_flush();
5626 }
5627
5628 void
5629 win_to_ws(struct ws_win *win, int wsid, bool unfocus)
5630 {
5631 struct ws_win *parent;
5632 struct workspace *ws, *nws, *pws;
5633
5634 if (wsid < 0 || wsid >= workspace_limit)
5635 return;
5636
5637 if (win->ws->idx == wsid)
5638 return;
5639
5640 ws = win->ws;
5641 nws = &win->s->ws[wsid];
5642
5643 DNPRINTF(SWM_D_MOVE, "win_to_ws: win %#x, ws %d -> %d\n", win->id,
5644 ws->idx, wsid);
5645
5646 /* Cleanup focus on source ws. */
5647 if (focus_mode != SWM_FOCUS_FOLLOW &&
5648 (ws->focus == win || ws->focus_pending == win))
5649 ws->focus_pending = get_focus_prev(win);
5650
5651 /* Move the parent if this is a transient window. */
5652 if (TRANS(win)) {
5653 parent = find_window(win->transient);
5654 if (parent) {
5655 pws = parent->ws;
5656 /* Set new focus in parent's ws if needed. */
5657 if (pws->focus == parent) {
5658 if (focus_mode != SWM_FOCUS_FOLLOW)
5659 pws->focus_pending =
5660 get_focus_prev(parent);
5661
5662 unfocus_win(parent);
5663
5664 if (focus_mode != SWM_FOCUS_FOLLOW) {
5665 pws->focus = pws->focus_pending;
5666 pws->focus_pending = NULL;
5667 }
5668 }
5669
5670 /* Don't unmap parent if new ws is visible */
5671 if (nws->r == NULL)
5672 unmap_window(parent);
5673
5674 /* Transfer */
5675 TAILQ_REMOVE(&ws->winlist, parent, entry);
5676 TAILQ_REMOVE(&ws->stack, parent, stack_entry);
5677 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
5678 TAILQ_INSERT_TAIL(&nws->stack, parent, stack_entry);
5679 parent->ws = nws;
5680
5681 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: "
5682 "_NET_WM_DESKTOP: %d\n", wsid);
5683 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
5684 parent->id, ewmh[_NET_WM_DESKTOP].atom,
5685 XCB_ATOM_CARDINAL, 32, 1, &wsid);
5686 }
5687 }
5688
5689 if (unfocus)
5690 unfocus_win(win);
5691
5692 if (ws->focus_prev == win)
5693 ws->focus_prev = NULL;
5694
5695 if (focus_mode != SWM_FOCUS_FOLLOW && ws->focus_pending != NULL) {
5696 ws->focus = ws->focus_pending;
5697 ws->focus_pending = NULL;
5698 }
5699
5700 /* Don't unmap if new ws is visible */
5701 if (nws->r == NULL)
5702 unmap_window(win);
5703
5704 /* Transfer */
5705 TAILQ_REMOVE(&ws->winlist, win, entry);
5706 TAILQ_REMOVE(&ws->stack, win, stack_entry);
5707 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
5708 TAILQ_INSERT_TAIL(&nws->stack, win, stack_entry);
5709 win->ws = nws;
5710
5711 /* Update the window's workspace property: _NET_WM_DESKTOP */
5712 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: "
5713 "_NET_WM_DESKTOP: %d\n", wsid);
5714 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
5715 ewmh[_NET_WM_DESKTOP].atom, XCB_ATOM_CARDINAL, 32, 1, &wsid);
5716
5717 ewmh_update_client_list();
5718
5719 DNPRINTF(SWM_D_MOVE, "win_to_ws: done.\n");
5720 }
5721
5722 void
5723 pressbutton(struct binding *bp, struct swm_region *r, union arg *args)
5724 {
5725 /* suppress unused warning since var is needed */
5726 (void)bp;
5727 (void)r;
5728
5729 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
5730 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5731 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
5732 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5733 }
5734
5735 void
5736 raise_toggle(struct binding *bp, struct swm_region *r, union arg *args)
5737 {
5738 /* Suppress warning. */
5739 (void)bp;
5740 (void)args;
5741
5742 if (r == NULL || r->ws == NULL)
5743 return;
5744
5745 if (r->ws->focus && MAXIMIZED(r->ws->focus))
5746 return;
5747
5748 r->ws->always_raise = !r->ws->always_raise;
5749
5750 /* Update focused win stacking order based on new always_raise value. */
5751 raise_window(r->ws->focus);
5752
5753 focus_flush();
5754 }
5755
5756 void
5757 iconify(struct binding *bp, struct swm_region *r, union arg *args)
5758 {
5759 struct ws_win *w;
5760
5761 /* Suppress warning. */
5762 (void)bp;
5763 (void)args;
5764
5765 if ((w = r->ws->focus) == NULL)
5766 return;
5767
5768 ewmh_apply_flags(w, w->ewmh_flags | EWMH_F_HIDDEN);
5769 ewmh_update_wm_state(w);
5770
5771 stack(r);
5772
5773 focus_flush();
5774 }
5775
5776 char *
5777 get_win_name(xcb_window_t win)
5778 {
5779 char *name = NULL;
5780 xcb_get_property_cookie_t c;
5781 xcb_get_property_reply_t *r;
5782
5783 /* First try _NET_WM_NAME for UTF-8. */
5784 c = xcb_get_property(conn, 0, win, ewmh[_NET_WM_NAME].atom,
5785 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5786 r = xcb_get_property_reply(conn, c, NULL);
5787 if (r && r->type == XCB_NONE) {
5788 free(r);
5789 /* Use WM_NAME instead; no UTF-8. */
5790 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
5791 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5792 r = xcb_get_property_reply(conn, c, NULL);
5793 }
5794
5795 if (r && r->type != XCB_NONE && r->length > 0)
5796 name = strndup(xcb_get_property_value(r),
5797 xcb_get_property_value_length(r));
5798 else
5799 name = strdup("");
5800
5801 if (name == NULL)
5802 err(1, "get_win_name: failed to allocate memory.");
5803
5804 free(r);
5805
5806 return (name);
5807 }
5808
5809 void
5810 uniconify(struct binding *bp, struct swm_region *r, union arg *args)
5811 {
5812 struct ws_win *win;
5813 FILE *lfile;
5814 char *name;
5815 int count = 0;
5816
5817 (void)bp;
5818
5819 DNPRINTF(SWM_D_MISC, "uniconify\n");
5820
5821 if (r == NULL || r->ws == NULL)
5822 return;
5823
5824 /* make sure we have anything to uniconify */
5825 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5826 if (win->ws == NULL)
5827 continue; /* should never happen */
5828 if (!ICONIC(win))
5829 continue;
5830 count++;
5831 }
5832
5833 DNPRINTF(SWM_D_MISC, "uniconify: count: %d\n", count);
5834
5835 if (count == 0)
5836 return;
5837
5838 search_r = r;
5839 search_resp_action = SWM_SEARCH_UNICONIFY;
5840
5841 spawn_select(r, args, "search", &searchpid);
5842
5843 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5844 return;
5845
5846 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5847 if (win->ws == NULL)
5848 continue; /* should never happen */
5849 if (!ICONIC(win))
5850 continue;
5851
5852 name = get_win_name(win->id);
5853 fprintf(lfile, "%s.%u\n", name, win->id);
5854 free(name);
5855 }
5856
5857 fclose(lfile);
5858 }
5859
5860 void
5861 name_workspace(struct binding *bp, struct swm_region *r, union arg *args)
5862 {
5863 FILE *lfile;
5864
5865 (void)bp;
5866
5867 DNPRINTF(SWM_D_MISC, "name_workspace\n");
5868
5869 if (r == NULL)
5870 return;
5871
5872 search_r = r;
5873 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
5874
5875 spawn_select(r, args, "name_workspace", &searchpid);
5876
5877 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5878 return;
5879
5880 fprintf(lfile, "%s", "");
5881 fclose(lfile);
5882 }
5883
5884 void
5885 search_workspace(struct binding *bp, struct swm_region *r, union arg *args)
5886 {
5887 int i;
5888 struct workspace *ws;
5889 FILE *lfile;
5890
5891 (void)bp;
5892
5893 DNPRINTF(SWM_D_MISC, "search_workspace\n");
5894
5895 if (r == NULL)
5896 return;
5897
5898 search_r = r;
5899 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
5900
5901 spawn_select(r, args, "search", &searchpid);
5902
5903 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5904 return;
5905
5906 for (i = 0; i < workspace_limit; i++) {
5907 ws = &r->s->ws[i];
5908 if (ws == NULL)
5909 continue;
5910 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
5911 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
5912 }
5913
5914 fclose(lfile);
5915 }
5916
5917 void
5918 search_win_cleanup(void)
5919 {
5920 struct search_window *sw = NULL;
5921
5922 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
5923 xcb_destroy_window(conn, sw->indicator);
5924 TAILQ_REMOVE(&search_wl, sw, entry);
5925 free(sw);
5926 }
5927 }
5928
5929 void
5930 search_win(struct binding *bp, struct swm_region *r, union arg *args)
5931 {
5932 struct ws_win *win = NULL;
5933 struct search_window *sw = NULL;
5934 xcb_window_t w;
5935 uint32_t wa[3];
5936 xcb_screen_t *screen;
5937 int i, width, height;
5938 char s[8];
5939 FILE *lfile;
5940 size_t len;
5941 XftDraw *draw;
5942 XGlyphInfo info;
5943 GC l_draw;
5944 XGCValues l_gcv;
5945 XRectangle l_ibox, l_lbox;
5946
5947 (void)bp;
5948
5949 DNPRINTF(SWM_D_MISC, "search_win\n");
5950
5951 search_r = r;
5952 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
5953
5954 spawn_select(r, args, "search", &searchpid);
5955
5956 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5957 return;
5958
5959 if ((screen = get_screen(r->s->idx)) == NULL)
5960 errx(1, "ERROR: can't get screen %d.", r->s->idx);
5961
5962 TAILQ_INIT(&search_wl);
5963
5964 i = 1;
5965 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5966 if (ICONIC(win))
5967 continue;
5968
5969 sw = calloc(1, sizeof(struct search_window));
5970 if (sw == NULL) {
5971 warn("search_win: calloc");
5972 fclose(lfile);
5973 search_win_cleanup();
5974 return;
5975 }
5976 sw->idx = i;
5977 sw->win = win;
5978
5979 snprintf(s, sizeof s, "%d", i);
5980 len = strlen(s);
5981
5982 w = xcb_generate_id(conn);
5983 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
5984 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
5985 wa[2] = screen->default_colormap;
5986
5987 if (bar_font_legacy) {
5988 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
5989 width = l_lbox.width + 4;
5990 height = bar_fs_extents->max_logical_extent.height + 4;
5991 } else {
5992 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
5993 &info);
5994 width = info.width + 4;
5995 height = bar_font->height + 4;
5996 }
5997
5998 xcb_create_window(conn, screen->root_depth, w, win->frame, 0, 0,
5999 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
6000 screen->root_visual, XCB_CW_BACK_PIXEL |
6001 XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP, wa);
6002
6003 xcb_map_window(conn, w);
6004
6005 sw->indicator = w;
6006 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
6007
6008 if (bar_font_legacy) {
6009 l_gcv.graphics_exposures = 0;
6010 l_draw = XCreateGC(display, w, 0, &l_gcv);
6011
6012 XSetForeground(display, l_draw,
6013 r->s->c[SWM_S_COLOR_BAR].pixel);
6014
6015 DRAWSTRING(display, w, bar_fs, l_draw, 2,
6016 (bar_fs_extents->max_logical_extent.height -
6017 l_lbox.height) / 2 - l_lbox.y, s, len);
6018
6019 XFreeGC(display, l_draw);
6020 } else {
6021
6022 draw = XftDrawCreate(display, w,
6023 DefaultVisual(display, r->s->idx),
6024 DefaultColormap(display, r->s->idx));
6025
6026 XftDrawStringUtf8(draw, &search_font_color, bar_font, 2,
6027 (HEIGHT(r->bar) + bar_font->height) / 2 -
6028 bar_font->descent, (FcChar8 *)s, len);
6029
6030 XftDrawDestroy(draw);
6031 }
6032
6033 DNPRINTF(SWM_D_MISC, "search_win: mapped win %#x\n", w);
6034
6035 fprintf(lfile, "%d\n", i);
6036 i++;
6037 }
6038
6039 fclose(lfile);
6040
6041 xcb_flush(conn);
6042 }
6043
6044 void
6045 search_resp_uniconify(const char *resp, size_t len)
6046 {
6047 char *name;
6048 struct ws_win *win;
6049 char *s;
6050
6051 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
6052
6053 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
6054 if (!ICONIC(win))
6055 continue;
6056 name = get_win_name(win->id);
6057 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
6058 free(name);
6059 continue;
6060 }
6061 free(name);
6062 if (strncmp(s, resp, len) == 0) {
6063 /* XXX this should be a callback to generalize */
6064 ewmh_apply_flags(win, win->ewmh_flags & ~EWMH_F_HIDDEN);
6065 ewmh_update_wm_state(win);
6066 stack(search_r);
6067 free(s);
6068 break;
6069 }
6070 free(s);
6071 }
6072 }
6073
6074 void
6075 search_resp_name_workspace(const char *resp, size_t len)
6076 {
6077 struct workspace *ws;
6078
6079 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
6080
6081 if (search_r->ws == NULL)
6082 return;
6083 ws = search_r->ws;
6084
6085 if (ws->name) {
6086 free(search_r->ws->name);
6087 search_r->ws->name = NULL;
6088 }
6089
6090 if (len > 1) {
6091 ws->name = strdup(resp);
6092 if (ws->name == NULL) {
6093 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
6094 "strdup: %s", strerror(errno));
6095 return;
6096 }
6097
6098 ewmh_update_desktop_names();
6099 ewmh_get_desktop_names();
6100 }
6101 }
6102
6103 void
6104 ewmh_update_desktop_names(void)
6105 {
6106 char *name_list = NULL, *p;
6107 int num_screens, i, j, len = 0, tot = 0;
6108
6109 num_screens = get_screen_count();
6110 for (i = 0; i < num_screens; ++i) {
6111 for (j = 0; j < workspace_limit; ++j) {
6112 if (screens[i].ws[j].name != NULL)
6113 len += strlen(screens[i].ws[j].name);
6114 ++len;
6115 }
6116
6117 if((name_list = calloc(len, sizeof(char))) == NULL)
6118 err(1, "update_desktop_names: calloc: failed to "
6119 "allocate memory.");
6120
6121 p = name_list;
6122 for (j = 0; j < workspace_limit; ++j) {
6123 if (screens[i].ws[j].name != NULL) {
6124 len = strlen(screens[i].ws[j].name);
6125 memcpy(p, screens[i].ws[j].name, len);
6126 } else {
6127 len = 0;
6128 }
6129
6130 p += len + 1;
6131 tot += len + 1;
6132 }
6133
6134 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
6135 screens[i].root, ewmh[_NET_DESKTOP_NAMES].atom,
6136 a_utf8_string, 8, tot, name_list);
6137
6138 free(name_list);
6139 name_list = NULL;
6140 }
6141
6142 free(name_list);
6143 }
6144
6145 void
6146 ewmh_get_desktop_names(void)
6147 {
6148 char *names = NULL;
6149 xcb_get_property_cookie_t c;
6150 xcb_get_property_reply_t *r;
6151 int num_screens, i, j, n, k;
6152
6153 num_screens = get_screen_count();
6154 for (i = 0; i < num_screens; ++i) {
6155 for (j = 0; j < workspace_limit; ++j) {
6156 free(screens[i].ws[j].name);
6157 screens[i].ws[j].name = NULL;
6158 }
6159
6160 c = xcb_get_property(conn, 0, screens[i].root,
6161 ewmh[_NET_DESKTOP_NAMES].atom,
6162 a_utf8_string, 0, UINT32_MAX);
6163 r = xcb_get_property_reply(conn, c, NULL);
6164 if (r == NULL)
6165 continue;
6166
6167 names = xcb_get_property_value(r);
6168 n = xcb_get_property_value_length(r);
6169
6170 for (j = 0, k = 0; j < n; ++j) {
6171 if (*(names + j) != '\0') {
6172 screens[i].ws[k].name = strdup(names + j);
6173 j += strlen(names + j);
6174 }
6175 ++k;
6176 }
6177 free(r);
6178 }
6179 }
6180
6181 void
6182 ewmh_update_client_list(void)
6183 {
6184 struct ws_win *win;
6185 int num_screens, i, j, k = 0, count = 0;
6186 xcb_window_t *wins;
6187
6188 num_screens = get_screen_count();
6189 for (i = 0; i < num_screens; ++i) {
6190 for (j = 0; j < workspace_limit; ++j)
6191 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
6192 ++count;
6193
6194 DNPRINTF(SWM_D_PROP, "ewmh_update_client_list: win count: %d\n",
6195 count);
6196
6197 if (count == 0)
6198 continue;
6199
6200 wins = calloc(count, sizeof(xcb_window_t));
6201 if (wins == NULL)
6202 err(1, "ewmh_update_client_list: calloc: failed to "
6203 "allocate memory.");
6204
6205 for (j = 0, k = 0; j < workspace_limit; ++j)
6206 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
6207 wins[k++] = win->id;
6208
6209 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
6210 screens[i].root, ewmh[_NET_CLIENT_LIST].atom,
6211 XCB_ATOM_WINDOW, 32, count, wins);
6212
6213 free(wins);
6214 }
6215 }
6216
6217 void
6218 ewmh_update_current_desktop(void)
6219 {
6220 int num_screens, i;
6221
6222 num_screens = get_screen_count();
6223 for (i = 0; i < num_screens; ++i) {
6224 if (screens[i].r_focus)
6225 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
6226 screens[i].root, ewmh[_NET_CURRENT_DESKTOP].atom,
6227 XCB_ATOM_CARDINAL, 32, 1,
6228 &screens[i].r_focus->ws->idx);
6229 }
6230 }
6231
6232 void
6233 ewmh_update_desktops(void)
6234 {
6235 int num_screens, i, j;
6236 uint32_t *vals;
6237
6238 vals = calloc(workspace_limit * 2, sizeof(uint32_t));
6239 if (vals == NULL)
6240 err(1, "ewmh_update_desktops: calloc: failed to allocate "
6241 "memory.");
6242
6243 num_screens = get_screen_count();
6244 for (i = 0; i < num_screens; i++) {
6245 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
6246 screens[i].root, ewmh[_NET_NUMBER_OF_DESKTOPS].atom,
6247 XCB_ATOM_CARDINAL, 32, 1, &workspace_limit);
6248
6249 for (j = 0; j < workspace_limit; ++j) {
6250 if (screens[i].ws[j].r != NULL) {
6251 vals[j * 2] = X(screens[i].ws[j].r);
6252 vals[j * 2 + 1] = Y(screens[i].ws[j].r);
6253 } else if (screens[i].ws[j].old_r != NULL) {
6254 vals[j * 2] = X(screens[i].ws[j].old_r);
6255 vals[j * 2 + 1] = Y(screens[i].ws[j].old_r);
6256 } else {
6257 vals[j * 2] = vals[j * 2 + 1] = 0;
6258 }
6259 }
6260
6261 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
6262 screens[i].root, ewmh[_NET_DESKTOP_VIEWPORT].atom,
6263 XCB_ATOM_CARDINAL, 32, workspace_limit * 2, vals);
6264 }
6265
6266 free(vals);
6267 }
6268
6269 void
6270 search_resp_search_workspace(const char *resp)
6271 {
6272 char *p, *q;
6273 int ws_idx;
6274 const char *errstr;
6275 union arg a;
6276
6277 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
6278
6279 q = strdup(resp);
6280 if (q == NULL) {
6281 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
6282 strerror(errno));
6283 return;
6284 }
6285 p = strchr(q, ':');
6286 if (p != NULL)
6287 *p = '\0';
6288 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
6289 if (errstr) {
6290 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
6291 errstr, q);
6292 free(q);
6293 return;
6294 }
6295 free(q);
6296 a.id = ws_idx - 1;
6297 switchws(NULL, search_r, &a);
6298 }
6299
6300 void
6301 search_resp_search_window(const char *resp)
6302 {
6303 char *s;
6304 int idx;
6305 const char *errstr;
6306 struct search_window *sw;
6307
6308 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
6309
6310 s = strdup(resp);
6311 if (s == NULL) {
6312 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
6313 strerror(errno));
6314 return;
6315 }
6316
6317 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
6318 if (errstr) {
6319 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
6320 errstr, s);
6321 free(s);
6322 return;
6323 }
6324 free(s);
6325
6326 TAILQ_FOREACH(sw, &search_wl, entry)
6327 if (idx == sw->idx) {
6328 focus_win(sw->win);
6329 break;
6330 }
6331 }
6332
6333 #define MAX_RESP_LEN 1024
6334
6335 void
6336 search_do_resp(void)
6337 {
6338 ssize_t rbytes;
6339 char *resp;
6340 size_t len;
6341
6342 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
6343
6344 search_resp = 0;
6345 searchpid = 0;
6346
6347 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
6348 warn("search: calloc");
6349 goto done;
6350 }
6351
6352 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
6353 if (rbytes <= 0) {
6354 warn("search: read error");
6355 goto done;
6356 }
6357 resp[rbytes] = '\0';
6358
6359 /* XXX:
6360 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6361 * newline, so work around that by sanitizing the resp now.
6362 */
6363 resp[strcspn(resp, "\n")] = '\0';
6364 len = strlen(resp);
6365
6366 switch (search_resp_action) {
6367 case SWM_SEARCH_UNICONIFY:
6368 search_resp_uniconify(resp, len);
6369 break;
6370 case SWM_SEARCH_NAME_WORKSPACE:
6371 search_resp_name_workspace(resp, len);
6372 break;
6373 case SWM_SEARCH_SEARCH_WORKSPACE:
6374 search_resp_search_workspace(resp);
6375 break;
6376 case SWM_SEARCH_SEARCH_WINDOW:
6377 search_resp_search_window(resp);
6378 break;
6379 }
6380
6381 done:
6382 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
6383 search_win_cleanup();
6384
6385 search_resp_action = SWM_SEARCH_NONE;
6386 close(select_resp_pipe[0]);
6387 free(resp);
6388
6389 xcb_flush(conn);
6390 }
6391
6392 void
6393 wkill(struct binding *bp, struct swm_region *r, union arg *args)
6394 {
6395 (void)bp;
6396
6397 DNPRINTF(SWM_D_MISC, "wkill: win %#x, id: %d\n", WINID(r->ws->focus),
6398 args->id);
6399
6400 if (r->ws->focus == NULL)
6401 return;
6402
6403 if (args->id == SWM_ARG_ID_KILLWINDOW)
6404 xcb_kill_client(conn, r->ws->focus->id);
6405 else
6406 if (r->ws->focus->can_delete)
6407 client_msg(r->ws->focus, a_delete, 0);
6408
6409 focus_flush();
6410 }
6411
6412 int
6413 clear_maximized(struct workspace *ws)
6414 {
6415 struct ws_win *w;
6416 int count = 0;
6417
6418 /* Clear any maximized win(s) on ws, from bottom up. */
6419 TAILQ_FOREACH_REVERSE(w, &ws->stack, ws_win_stack, stack_entry)
6420 if (MAXIMIZED(w)) {
6421 ewmh_apply_flags(w, w->ewmh_flags & ~EWMH_F_MAXIMIZED);
6422 ewmh_update_wm_state(w);
6423 ++count;
6424 }
6425
6426 return count;
6427 }
6428
6429 void
6430 maximize_toggle(struct binding *bp, struct swm_region *r, union arg *args)
6431 {
6432 struct ws_win *w = r->ws->focus;
6433
6434 /* suppress unused warning since var is needed */
6435 (void)bp;
6436 (void)args;
6437
6438 if (w == NULL)
6439 return;
6440
6441 DNPRINTF(SWM_D_MISC, "maximize_toggle: win %#x\n", w->id);
6442
6443 if (FULLSCREEN(w))
6444 return;
6445
6446 if (w->ws->cur_layout == &layouts[SWM_MAX_STACK])
6447 return;
6448
6449 ewmh_apply_flags(w, w->ewmh_flags ^ EWMH_F_MAXIMIZED);
6450 ewmh_update_wm_state(w);
6451
6452 stack(r);
6453
6454 if (w == w->ws->focus)
6455 focus_win(w);
6456
6457 center_pointer(r);
6458 focus_flush();
6459 DNPRINTF(SWM_D_MISC, "maximize_toggle: done\n");
6460 }
6461
6462 void
6463 floating_toggle(struct binding *bp, struct swm_region *r, union arg *args)
6464 {
6465 struct ws_win *w = r->ws->focus;
6466
6467 /* suppress unused warning since var is needed */
6468 (void)bp;
6469 (void)args;
6470
6471 if (w == NULL)
6472 return;
6473
6474 DNPRINTF(SWM_D_MISC, "floating_toggle: win %#x\n", w->id);
6475
6476 if (FULLSCREEN(w) || TRANS(w))
6477 return;
6478
6479 if (w->ws->cur_layout == &layouts[SWM_MAX_STACK])
6480 return;
6481
6482 ewmh_apply_flags(w, w->ewmh_flags ^ EWMH_F_ABOVE);
6483 ewmh_update_wm_state(w);
6484
6485 stack(r);
6486
6487 if (w == w->ws->focus)
6488 focus_win(w);
6489
6490 center_pointer(r);
6491 focus_flush();
6492 DNPRINTF(SWM_D_MISC, "floating_toggle: done\n");
6493 }
6494
6495 void
6496 region_containment(struct ws_win *win, struct swm_region *r, int opts)
6497 {
6498 struct swm_geometry g = r->g;
6499 int rt, lt, tp, bm, bw;
6500
6501 bw = (opts & SWM_CW_SOFTBOUNDARY) ? boundary_width : 0;
6502
6503 /*
6504 * Perpendicular distance of each side of the window to the respective
6505 * side of the region boundary. Positive values indicate the side of
6506 * the window has passed beyond the region boundary.
6507 */
6508 rt = opts & SWM_CW_RIGHT ? MAX_X(win) - MAX_X(r) : bw;
6509 lt = opts & SWM_CW_LEFT ? X(r) - X(win) : bw;
6510 bm = opts & SWM_CW_BOTTOM ? MAX_Y(win) - MAX_Y(r) : bw;
6511 tp = opts & SWM_CW_TOP ? Y(r) - Y(win) : bw;
6512
6513 DNPRINTF(SWM_D_MISC, "region_containment: win %#x, rt: %d, lt: %d, "
6514 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win->id, rt,
6515 lt, bm, tp, YESNO(opts & SWM_CW_SOFTBOUNDARY),
6516 YESNO(opts & SWM_CW_HARDBOUNDARY));
6517
6518 /*
6519 * Disable containment if any of the flagged sides went beyond the
6520 * containment boundary, or if containment is disabled.
6521 */
6522 if (!(opts & SWM_CW_HARDBOUNDARY || opts & SWM_CW_SOFTBOUNDARY) ||
6523 (bw != 0 && ((rt > bw) || (lt > bw) || (bm > bw) || (tp > bw)))) {
6524 /* Make sure window has at least 1 pixel in the region */
6525 g.x += 1 - WIDTH(win);
6526 g.y += 1 - HEIGHT(win);
6527 g.w += 2 * WIDTH(win) - 2;
6528 g.h += 2 * HEIGHT(win) - 2;
6529 }
6530
6531 constrain_window(win, &g, &opts);
6532 }
6533
6534 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6535 void
6536 constrain_window(struct ws_win *win, struct swm_geometry *b, int *opts)
6537 {
6538 DNPRINTF(SWM_D_MISC, "constrain_window: win %#x, (x,y) w x h: "
6539 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6540 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win->id, X(win),
6541 Y(win), WIDTH(win), HEIGHT(win), b->x, b->y, b->w, b->h,
6542 YESNO(*opts & SWM_CW_RIGHT), YESNO(*opts & SWM_CW_LEFT),
6543 YESNO(*opts & SWM_CW_BOTTOM), YESNO(*opts & SWM_CW_TOP),
6544 YESNO(*opts & SWM_CW_RESIZABLE));
6545
6546 if ((*opts & SWM_CW_RIGHT) && MAX_X(win) > b->x + b->w) {
6547 if (*opts & SWM_CW_RESIZABLE)
6548 WIDTH(win) = b->x + b->w - X(win);
6549 else
6550 X(win) = b->x + b->w - WIDTH(win);
6551 }
6552
6553 if ((*opts & SWM_CW_LEFT) && X(win) < b->x) {
6554 if (*opts & SWM_CW_RESIZABLE)
6555 WIDTH(win) -= b->x - X(win);
6556
6557 X(win) = b->x;
6558 }
6559
6560 if ((*opts & SWM_CW_BOTTOM) && MAX_Y(win) > b->y + b->h) {
6561 if (*opts & SWM_CW_RESIZABLE)
6562 HEIGHT(win) = b->y + b->h - Y(win);
6563 else
6564 Y(win) = b->y + b->h - HEIGHT(win);
6565 }
6566
6567 if ((*opts & SWM_CW_TOP) && Y(win) < b->y) {
6568 if (*opts & SWM_CW_RESIZABLE)
6569 HEIGHT(win) -= b->y - Y(win);
6570
6571 Y(win) = b->y;
6572 }
6573
6574 if (*opts & SWM_CW_RESIZABLE) {
6575 if (WIDTH(win) < 1)
6576 WIDTH(win) = 1;
6577 if (HEIGHT(win) < 1)
6578 HEIGHT(win) = 1;
6579 }
6580 }
6581
6582 void
6583 draw_frame(struct ws_win *win)
6584 {
6585 xcb_rectangle_t rect[4];
6586 uint32_t *pixel;
6587 int n = 0;
6588
6589 if (win->frame == XCB_WINDOW_NONE) {
6590 DNPRINTF(SWM_D_EVENT, "draw_frame: win %#x not "
6591 "reparented.\n", win->id);
6592 return;
6593 }
6594
6595 if (!win->bordered) {
6596 DNPRINTF(SWM_D_EVENT, "draw_frame: win %#x frame "
6597 "disabled\n", win->id);
6598 }
6599
6600 if (WS_FOCUSED(win->ws) && win->ws->focus == win)
6601 pixel = MAXIMIZED(win) ?
6602 &win->s->c[SWM_S_COLOR_FOCUS_MAXIMIZED].pixel :
6603 &win->s->c[SWM_S_COLOR_FOCUS].pixel;
6604 else
6605 pixel = MAXIMIZED(win) ?
6606 &win->s->c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].pixel :
6607 &win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
6608
6609 /* Top (with corners) */
6610 rect[n].x = 0;
6611 rect[n].y = 0;
6612 rect[n].width = WIDTH(win) + 2 * border_width;
6613 rect[n].height = border_width;
6614 /* Left (without corners) */
6615 rect[++n].x = 0;
6616 rect[n].y = border_width;
6617 rect[n].width = border_width;
6618 rect[n].height = HEIGHT(win);
6619 /* Right (without corners) */
6620 rect[++n].x = border_width + WIDTH(win);
6621 rect[n].y = border_width;
6622 rect[n].width = border_width;
6623 rect[n].height = HEIGHT(win);
6624 /* Bottom (with corners)*/
6625 rect[++n].x = 0;
6626 rect[n].y = border_width + HEIGHT(win);
6627 rect[n].width = WIDTH(win) + 2 * border_width;
6628 rect[n].height = border_width;
6629
6630 xcb_change_gc(conn, win->s->bar_gc, XCB_GC_FOREGROUND, pixel);
6631 xcb_poly_fill_rectangle(conn, win->frame, win->s->bar_gc, 4, rect);
6632 }
6633
6634 void
6635 update_window(struct ws_win *win)
6636 {
6637 uint16_t mask;
6638 uint32_t wc[5];
6639
6640 if (win->frame == XCB_WINDOW_NONE) {
6641 DNPRINTF(SWM_D_EVENT, "update_window: skip win %#x; "
6642 "not reparented.\n", win->id);
6643 return;
6644 }
6645
6646 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
6647 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
6648 XCB_CONFIG_WINDOW_BORDER_WIDTH;
6649
6650 /* Reconfigure frame. */
6651 if (win->bordered) {
6652 wc[0] = X(win) - border_width;
6653 wc[1] = Y(win) - border_width;
6654 wc[2] = WIDTH(win) + 2 * border_width;
6655 wc[3] = HEIGHT(win) + 2 * border_width;
6656 } else {
6657 wc[0] = X(win);
6658 wc[1] = Y(win);
6659 wc[2] = WIDTH(win);
6660 wc[3] = HEIGHT(win);
6661 }
6662
6663 wc[4] = 0;
6664
6665 DNPRINTF(SWM_D_EVENT, "update_window: win %#x frame %#x, (x,y) w x h: "
6666 "(%d,%d) %d x %d, bordered: %s\n", win->id, win->frame, wc[0],
6667 wc[1], wc[2], wc[3], YESNO(win->bordered));
6668
6669 xcb_configure_window(conn, win->frame, mask, wc);
6670
6671 /* Reconfigure client window. */
6672 wc[0] = wc[1] = win->bordered ? border_width : 0;
6673 wc[2] = WIDTH(win);
6674 wc[3] = HEIGHT(win);
6675
6676 DNPRINTF(SWM_D_EVENT, "update_window: win %#x, (x,y) w x h: "
6677 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
6678 wc[3], YESNO(win->bordered));
6679 xcb_configure_window(conn, win->id, mask, wc);
6680
6681 /* ICCCM 4.2.3 Synthetic ConfigureNotify when moved and not resized. */
6682 if ((X(win) != win->g_prev.x || Y(win) != win->g_prev.y) &&
6683 (win->java || (WIDTH(win) == win->g_prev.w &&
6684 HEIGHT(win) == win->g_prev.h))) {
6685 /* Java has special needs when moved together with a resize. */
6686 config_win(win, NULL);
6687 }
6688
6689 win->g_prev = win->g;
6690 }
6691
6692 struct event {
6693 SIMPLEQ_ENTRY(event) entry;
6694 xcb_generic_event_t *ev;
6695 };
6696 SIMPLEQ_HEAD(event_queue, event) events = SIMPLEQ_HEAD_INITIALIZER(events);
6697
6698 xcb_generic_event_t *
6699 get_next_event(bool dowait)
6700 {
6701 struct event *ep;
6702 xcb_generic_event_t *evt;
6703
6704 /* Try queue first. */
6705 if ((ep = SIMPLEQ_FIRST(&events))) {
6706 evt = ep->ev;
6707 SIMPLEQ_REMOVE_HEAD(&events, entry);
6708 free(ep);
6709 } else if (dowait)
6710 evt = xcb_wait_for_event(conn);
6711 else
6712 evt = xcb_poll_for_event(conn);
6713
6714 return evt;
6715 }
6716
6717 void
6718 put_back_event(xcb_generic_event_t *evt)
6719 {
6720 struct event *ep;
6721 if ((ep = malloc(sizeof (struct event))) == NULL)
6722 err(1, "put_back_event: failed to allocate memory.");
6723 ep->ev = evt;
6724 SIMPLEQ_INSERT_HEAD(&events, ep, entry);
6725 }
6726
6727 /* Peeks at next event to detect auto-repeat. */
6728 bool
6729 keyrepeating(xcb_key_release_event_t *kre)
6730 {
6731 xcb_generic_event_t *evt;
6732
6733 /* Ensure repeating keypress is finished processing. */
6734 xcb_aux_sync(conn);
6735
6736 if ((evt = get_next_event(false))) {
6737 put_back_event(evt);
6738
6739 if (XCB_EVENT_RESPONSE_TYPE(evt) == XCB_KEY_PRESS &&
6740 kre->sequence == evt->sequence &&
6741 kre->detail == ((xcb_key_press_event_t *)evt)->detail)
6742 return true;
6743 }
6744
6745 return false;
6746 }
6747
6748 bool
6749 keybindreleased(struct binding *bp, xcb_key_release_event_t *kre)
6750 {
6751 if (bp->type == KEYBIND && !keyrepeating(kre) &&
6752 bp->value == xcb_key_press_lookup_keysym(syms, kre, 0))
6753 return true;
6754
6755 return false;
6756 }
6757
6758 #define SWM_RESIZE_STEPS (50)
6759
6760 void
6761 resize_win(struct ws_win *win, struct binding *bp, int opt)
6762 {
6763 xcb_timestamp_t timestamp = 0;
6764 struct swm_region *r = NULL;
6765 struct swm_geometry g;
6766 int top = 0, left = 0;
6767 int dx, dy;
6768 xcb_cursor_t cursor;
6769 xcb_query_pointer_reply_t *xpr = NULL;
6770 xcb_generic_event_t *evt;
6771 xcb_motion_notify_event_t *mne;
6772 bool resizing, step = false;
6773
6774 if (win == NULL)
6775 return;
6776 r = win->ws->r;
6777
6778 if (FULLSCREEN(win))
6779 return;
6780
6781 /* In max_stack mode, should only resize transients. */
6782 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !TRANS(win))
6783 return;
6784
6785 DNPRINTF(SWM_D_EVENT, "resize: win %#x, floating: %s, "
6786 "transient: %#x\n", win->id, YESNO(ABOVE(win)),
6787 win->transient);
6788
6789 if (MAXIMIZED(win))
6790 store_float_geom(win);
6791 else if (!(TRANS(win) || ABOVE(win)))
6792 return;
6793
6794 ewmh_apply_flags(win, (win->ewmh_flags | SWM_F_MANUAL | EWMH_F_ABOVE) &
6795 ~EWMH_F_MAXIMIZED);
6796 ewmh_update_wm_state(win);
6797
6798 stack(r);
6799
6800 focus_flush();
6801
6802 /* It's possible for win to have been freed during focus_flush(). */
6803 if (validate_win(win)) {
6804 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
6805 goto out;
6806 }
6807
6808 switch (opt) {
6809 case SWM_ARG_ID_WIDTHSHRINK:
6810 WIDTH(win) -= SWM_RESIZE_STEPS;
6811 step = true;
6812 break;
6813 case SWM_ARG_ID_WIDTHGROW:
6814 WIDTH(win) += SWM_RESIZE_STEPS;
6815 step = true;
6816 break;
6817 case SWM_ARG_ID_HEIGHTSHRINK:
6818 HEIGHT(win) -= SWM_RESIZE_STEPS;
6819 step = true;
6820 break;
6821 case SWM_ARG_ID_HEIGHTGROW:
6822 HEIGHT(win) += SWM_RESIZE_STEPS;
6823 step = true;
6824 break;
6825 default:
6826 break;
6827 }
6828 if (step) {
6829 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6830 SWM_CW_HARDBOUNDARY);
6831 update_window(win);
6832 store_float_geom(win);
6833 return;
6834 }
6835
6836 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6837 SWM_CW_SOFTBOUNDARY);
6838 update_window(win);
6839
6840 /* get cursor offset from window root */
6841 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
6842 NULL);
6843 if (xpr == NULL)
6844 return;
6845
6846 g = win->g;
6847
6848 if (xpr->win_x < WIDTH(win) / 2)
6849 left = 1;
6850
6851 if (xpr->win_y < HEIGHT(win) / 2)
6852 top = 1;
6853
6854 if (opt == SWM_ARG_ID_CENTER)
6855 cursor = cursors[XC_SIZING].cid;
6856 else if (top)
6857 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
6858 XC_TOP_RIGHT_CORNER].cid;
6859 else
6860 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
6861 XC_BOTTOM_RIGHT_CORNER].cid;
6862
6863 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
6864 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
6865 XCB_CURRENT_TIME);
6866
6867 /* Release keyboard freeze if called via keybind. */
6868 if (bp->type == KEYBIND)
6869 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
6870 XCB_CURRENT_TIME);
6871
6872 xcb_flush(conn);
6873 resizing = true;
6874 while (resizing && (evt = get_next_event(true))) {
6875 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
6876 case XCB_BUTTON_RELEASE:
6877 if (bp->type == BTNBIND && bp->value ==
6878 ((xcb_button_release_event_t *)evt)->detail)
6879 resizing = false;
6880 break;
6881 case XCB_KEY_RELEASE:
6882 if (keybindreleased(bp, (xcb_key_release_event_t *)evt))
6883 resizing = false;
6884 break;
6885 case XCB_MOTION_NOTIFY:
6886 mne = (xcb_motion_notify_event_t *)evt;
6887 DNPRINTF(SWM_D_EVENT, "resize: MOTION_NOTIFY: "
6888 "root: %#x\n", mne->root);
6889
6890 /* cursor offset/delta from start of the operation */
6891 dx = mne->root_x - xpr->root_x;
6892 dy = mne->root_y - xpr->root_y;
6893
6894 /* vertical */
6895 if (top)
6896 dy = -dy;
6897
6898 if (opt == SWM_ARG_ID_CENTER) {
6899 if (g.h / 2 + dy < 1)
6900 dy = 1 - g.h / 2;
6901
6902 Y(win) = g.y - dy;
6903 HEIGHT(win) = g.h + 2 * dy;
6904 } else {
6905 if (g.h + dy < 1)
6906 dy = 1 - g.h;
6907
6908 if (top)
6909 Y(win) = g.y - dy;
6910
6911 HEIGHT(win) = g.h + dy;
6912 }
6913
6914 /* horizontal */
6915 if (left)
6916 dx = -dx;
6917
6918 if (opt == SWM_ARG_ID_CENTER) {
6919 if (g.w / 2 + dx < 1)
6920 dx = 1 - g.w / 2;
6921
6922 X(win) = g.x - dx;
6923 WIDTH(win) = g.w + 2 * dx;
6924 } else {
6925 if (g.w + dx < 1)
6926 dx = 1 - g.w;
6927
6928 if (left)
6929 X(win) = g.x - dx;
6930
6931 WIDTH(win) = g.w + dx;
6932 }
6933
6934 /* not free, don't sync more than 120 times / second */
6935 if ((mne->time - timestamp) > (1000 / 120) ) {
6936 timestamp = mne->time;
6937 regionize(win, mne->root_x, mne->root_y);
6938 region_containment(win, r, SWM_CW_ALLSIDES |
6939 SWM_CW_RESIZABLE | SWM_CW_HARDBOUNDARY |
6940 SWM_CW_SOFTBOUNDARY);
6941 update_window(win);
6942 xcb_flush(conn);
6943 }
6944 break;
6945 case XCB_BUTTON_PRESS:
6946 /* Ignore. */
6947 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_PRESS ignored\n");
6948 xcb_allow_events(conn, XCB_ALLOW_ASYNC_POINTER,
6949 ((xcb_button_press_event_t *)evt)->time);
6950 xcb_flush(conn);
6951 break;
6952 case XCB_KEY_PRESS:
6953 /* Ignore. */
6954 DNPRINTF(SWM_D_EVENT, "resize: KEY_PRESS ignored\n");
6955 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
6956 ((xcb_key_press_event_t *)evt)->time);
6957 xcb_flush(conn);
6958 break;
6959 default:
6960 event_handle(evt);
6961
6962 /* It's possible for win to have been freed above. */
6963 if (validate_win(win)) {
6964 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
6965 goto out;
6966 }
6967 break;
6968 }
6969 free(evt);
6970 }
6971 if (timestamp) {
6972 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
6973 SWM_CW_HARDBOUNDARY | SWM_CW_SOFTBOUNDARY);
6974 update_window(win);
6975 xcb_flush(conn);
6976 }
6977 store_float_geom(win);
6978 out:
6979 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6980 free(xpr);
6981 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
6982 }
6983
6984 void
6985 resize(struct binding *bp, struct swm_region *r, union arg *args)
6986 {
6987 struct ws_win *win = NULL;
6988 xcb_query_pointer_reply_t *qpr = NULL;
6989
6990 if (r == NULL)
6991 return;
6992
6993 if (args->id != SWM_ARG_ID_DONTCENTER &&
6994 args->id != SWM_ARG_ID_CENTER) {
6995 /* {height,width}_{grow,shrink} use the focus window. */
6996 if (r->ws)
6997 win = r->ws->focus;
6998 } else {
6999 /* resize uses window under pointer. */
7000 qpr = xcb_query_pointer_reply(conn,
7001 xcb_query_pointer(conn, r->s->root), NULL);
7002 if (qpr)
7003 win = find_window(qpr->child);
7004 }
7005
7006 if (win == NULL)
7007 return;
7008
7009 resize_win(win, bp, args->id);
7010
7011 if (args->id && bp->type == KEYBIND)
7012 center_pointer(r);
7013
7014 focus_flush();
7015 }
7016
7017 /* Try to set window region based on supplied coordinates or window center. */
7018 void
7019 regionize(struct ws_win *win, int x, int y)
7020 {
7021 struct swm_region *r = NULL;
7022
7023 r = region_under(win->s, x, y);
7024 if (r == NULL)
7025 r = region_under(win->s, X(win) + WIDTH(win) / 2,
7026 Y(win) + HEIGHT(win) / 2);
7027
7028 if (r && r != win->ws->r) {
7029 clear_maximized(r->ws);
7030
7031 win_to_ws(win, r->ws->idx, false);
7032
7033 /* Stack old and new region. */
7034 stack(r);
7035 stack(win->ws->r);
7036
7037 /* Set focus on new ws. */
7038 unfocus_win(r->ws->focus);
7039 r->ws->focus = win;
7040
7041 set_region(r);
7042 raise_window(win);
7043 }
7044 }
7045
7046 #define SWM_MOVE_STEPS (50)
7047
7048 void
7049 move_win(struct ws_win *win, struct binding *bp, int opt)
7050 {
7051 struct swm_region *r;
7052 xcb_timestamp_t timestamp = 0;
7053 xcb_query_pointer_reply_t *qpr = NULL;
7054 xcb_generic_event_t *evt;
7055 xcb_motion_notify_event_t *mne;
7056 bool moving, restack = false, step = false;
7057
7058 if (win == NULL)
7059 return;
7060
7061 if ((r = win->ws->r) == NULL)
7062 return;
7063
7064 if (FULLSCREEN(win))
7065 return;
7066
7067 DNPRINTF(SWM_D_EVENT, "move: win %#x, floating: %s, transient: "
7068 "%#x\n", win->id, YESNO(ABOVE(win)), win->transient);
7069
7070 /* in max_stack mode should only move transients */
7071 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !TRANS(win))
7072 return;
7073
7074 if (!(ABOVE(win) || TRANS(win)) || MAXIMIZED(win)) {
7075 store_float_geom(win);
7076 restack = true;
7077 }
7078
7079 ewmh_apply_flags(win, (win->ewmh_flags | SWM_F_MANUAL | EWMH_F_ABOVE) &
7080 ~EWMH_F_MAXIMIZED);
7081 ewmh_update_wm_state(win);
7082
7083 if (restack)
7084 stack(r);
7085
7086 focus_flush();
7087
7088 /* It's possible for win to have been freed during focus_flush(). */
7089 if (validate_win(win)) {
7090 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
7091 goto out;
7092 }
7093
7094 switch (opt) {
7095 case SWM_ARG_ID_MOVELEFT:
7096 X(win) -= (SWM_MOVE_STEPS - border_width);
7097 step = true;
7098 break;
7099 case SWM_ARG_ID_MOVERIGHT:
7100 X(win) += (SWM_MOVE_STEPS - border_width);
7101 step = true;
7102 break;
7103 case SWM_ARG_ID_MOVEUP:
7104 Y(win) -= (SWM_MOVE_STEPS - border_width);
7105 step = true;
7106 break;
7107 case SWM_ARG_ID_MOVEDOWN:
7108 Y(win) += (SWM_MOVE_STEPS - border_width);
7109 step = true;
7110 break;
7111 default:
7112 break;
7113 }
7114 if (step) {
7115 regionize(win, -1, -1);
7116 region_containment(win, win->ws->r, SWM_CW_ALLSIDES);
7117 update_window(win);
7118 store_float_geom(win);
7119 return;
7120 }
7121
7122 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
7123 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
7124 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
7125
7126 /* get cursor offset from window root */
7127 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
7128 NULL);
7129 if (qpr == NULL) {
7130 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
7131 return;
7132 }
7133
7134 /* Release keyboard freeze if called via keybind. */
7135 if (bp->type == KEYBIND)
7136 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
7137 XCB_CURRENT_TIME);
7138
7139 regionize(win, qpr->root_x, qpr->root_y);
7140 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
7141 SWM_CW_SOFTBOUNDARY);
7142 update_window(win);
7143 xcb_flush(conn);
7144 moving = true;
7145 while (moving && (evt = get_next_event(true))) {
7146 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
7147 case XCB_BUTTON_RELEASE:
7148 if (bp->type == BTNBIND && bp->value ==
7149 ((xcb_button_release_event_t *)evt)->detail)
7150 moving = false;
7151
7152 xcb_allow_events(conn, XCB_ALLOW_ASYNC_POINTER,
7153 ((xcb_button_release_event_t *)evt)->time);
7154 xcb_flush(conn);
7155 break;
7156 case XCB_KEY_RELEASE:
7157 if (keybindreleased(bp, (xcb_key_release_event_t *)evt))
7158 moving = false;
7159
7160 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
7161 ((xcb_key_release_event_t *)evt)->time);
7162 xcb_flush(conn);
7163 break;
7164 case XCB_MOTION_NOTIFY:
7165 mne = (xcb_motion_notify_event_t *)evt;
7166 DNPRINTF(SWM_D_EVENT, "move: MOTION_NOTIFY: "
7167 "root: %#x\n", mne->root);
7168 X(win) = mne->root_x - qpr->win_x - border_width;
7169 Y(win) = mne->root_y - qpr->win_y - border_width;
7170
7171 /* not free, don't sync more than 120 times / second */
7172 if ((mne->time - timestamp) > (1000 / 120) ) {
7173 timestamp = mne->time;
7174 regionize(win, mne->root_x, mne->root_y);
7175 region_containment(win, win->ws->r,
7176 SWM_CW_ALLSIDES | SWM_CW_SOFTBOUNDARY);
7177 update_window(win);
7178 xcb_flush(conn);
7179 }
7180 break;
7181 case XCB_BUTTON_PRESS:
7182 /* Thaw and ignore. */
7183 xcb_allow_events(conn, XCB_ALLOW_ASYNC_POINTER,
7184 ((xcb_button_press_event_t *)evt)->time);
7185 xcb_flush(conn);
7186 break;
7187 case XCB_KEY_PRESS:
7188 /* Ignore. */
7189 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD,
7190 ((xcb_key_press_event_t *)evt)->time);
7191 xcb_flush(conn);
7192 break;
7193 default:
7194 event_handle(evt);
7195
7196 /* It's possible for win to have been freed. */
7197 if (validate_win(win)) {
7198 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
7199 goto out;
7200 }
7201 break;
7202 }
7203 free(evt);
7204 }
7205 if (timestamp) {
7206 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
7207 SWM_CW_SOFTBOUNDARY);
7208 update_window(win);
7209 xcb_flush(conn);
7210 }
7211 store_float_geom(win);
7212
7213 /* New region set to fullscreen layout. */
7214 if (win->ws->r && win->ws->cur_layout == &layouts[SWM_MAX_STACK]) {
7215 stack(win->ws->r);
7216 focus_flush();
7217 }
7218
7219 out:
7220 free(qpr);
7221 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
7222 DNPRINTF(SWM_D_EVENT, "move: done.\n");
7223 }
7224
7225 void
7226 move(struct binding *bp, struct swm_region *r, union arg *args)
7227 {
7228 struct ws_win *win = NULL;
7229 xcb_query_pointer_reply_t *qpr = NULL;
7230
7231 if (r == NULL)
7232 return;
7233
7234 if (args->id) {
7235 /* move_* uses focus window. */
7236 if (r->ws)
7237 win = r->ws->focus;
7238
7239 /* Disallow move_ on tiled. */
7240 if (win && !(TRANS(win) || ABOVE(win)))
7241 return;
7242 } else {
7243 /* move uses window under pointer. */
7244 qpr = xcb_query_pointer_reply(conn,
7245 xcb_query_pointer(conn, r->s->root), NULL);
7246 if (qpr)
7247 win = find_window(qpr->child);
7248 }
7249
7250 if (win == NULL)
7251 return;
7252
7253 move_win(win, bp, args->id);
7254
7255 if (args->id && bp->type == KEYBIND)
7256 center_pointer(r);
7257
7258 focus_flush();
7259 }
7260
7261 /* action definitions */
7262 struct action {
7263 char name[SWM_FUNCNAME_LEN];
7264 void (*func)(struct binding *, struct swm_region *,
7265 union arg *);
7266 uint32_t flags;
7267 union arg args;
7268 } actions[FN_INVALID + 2] = {
7269 /* name function argument */
7270 { "bar_toggle", bar_toggle, 0, {.id = SWM_ARG_ID_BAR_TOGGLE} },
7271 { "bar_toggle_ws", bar_toggle, 0, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
7272 { "button2", pressbutton, 0, {.id = 2} },
7273 { "cycle_layout", cycle_layout, 0, {0} },
7274 { "flip_layout", stack_config, 0, {.id = SWM_ARG_ID_FLIPLAYOUT} },
7275 { "float_toggle", floating_toggle,0, {0} },
7276 { "focus", focus_pointer, 0, {0} },
7277 { "focus_main", focus, 0, {.id = SWM_ARG_ID_FOCUSMAIN} },
7278 { "focus_next", focus, 0, {.id = SWM_ARG_ID_FOCUSNEXT} },
7279 { "focus_prev", focus, 0, {.id = SWM_ARG_ID_FOCUSPREV} },
7280 { "focus_urgent", focus, 0, {.id = SWM_ARG_ID_FOCUSURGENT} },
7281 { "maximize_toggle", maximize_toggle,0, {0} },
7282 { "height_grow", resize, 0, {.id = SWM_ARG_ID_HEIGHTGROW} },
7283 { "height_shrink", resize, 0, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
7284 { "iconify", iconify, 0, {0} },
7285 { "master_shrink", stack_config, 0, {.id = SWM_ARG_ID_MASTERSHRINK} },
7286 { "master_grow", stack_config, 0, {.id = SWM_ARG_ID_MASTERGROW} },
7287 { "master_add", stack_config, 0, {.id = SWM_ARG_ID_MASTERADD} },
7288 { "master_del", stack_config, 0, {.id = SWM_ARG_ID_MASTERDEL} },
7289 { "move", move, FN_F_NOREPLAY, {0} },
7290 { "move_down", move, 0, {.id = SWM_ARG_ID_MOVEDOWN} },
7291 { "move_left", move, 0, {.id = SWM_ARG_ID_MOVELEFT} },
7292 { "move_right", move, 0, {.id = SWM_ARG_ID_MOVERIGHT} },
7293 { "move_up", move, 0, {.id = SWM_ARG_ID_MOVEUP} },
7294 { "mvrg_1", send_to_rg, 0, {.id = 0} },
7295 { "mvrg_2", send_to_rg, 0, {.id = 1} },
7296 { "mvrg_3", send_to_rg, 0, {.id = 2} },
7297 { "mvrg_4", send_to_rg, 0, {.id = 3} },
7298 { "mvrg_5", send_to_rg, 0, {.id = 4} },
7299 { "mvrg_6", send_to_rg, 0, {.id = 5} },
7300 { "mvrg_7", send_to_rg, 0, {.id = 6} },
7301 { "mvrg_8", send_to_rg, 0, {.id = 7} },
7302 { "mvrg_9", send_to_rg, 0, {.id = 8} },
7303 { "mvws_1", send_to_ws, 0, {.id = 0} },
7304 { "mvws_2", send_to_ws, 0, {.id = 1} },
7305 { "mvws_3", send_to_ws, 0, {.id = 2} },
7306 { "mvws_4", send_to_ws, 0, {.id = 3} },
7307 { "mvws_5", send_to_ws, 0, {.id = 4} },
7308 { "mvws_6", send_to_ws, 0, {.id = 5} },
7309 { "mvws_7", send_to_ws, 0, {.id = 6} },
7310 { "mvws_8", send_to_ws, 0, {.id = 7} },
7311 { "mvws_9", send_to_ws, 0, {.id = 8} },
7312 { "mvws_10", send_to_ws, 0, {.id = 9} },
7313 { "mvws_11", send_to_ws, 0, {.id = 10} },
7314 { "mvws_12", send_to_ws, 0, {.id = 11} },
7315 { "mvws_13", send_to_ws, 0, {.id = 12} },
7316 { "mvws_14", send_to_ws, 0, {.id = 13} },
7317 { "mvws_15", send_to_ws, 0, {.id = 14} },
7318 { "mvws_16", send_to_ws, 0, {.id = 15} },
7319 { "mvws_17", send_to_ws, 0, {.id = 16} },
7320 { "mvws_18", send_to_ws, 0, {.id = 17} },
7321 { "mvws_19", send_to_ws, 0, {.id = 18} },
7322 { "mvws_20", send_to_ws, 0, {.id = 19} },
7323 { "mvws_21", send_to_ws, 0, {.id = 20} },
7324 { "mvws_22", send_to_ws, 0, {.id = 21} },
7325 { "name_workspace", name_workspace, 0, {0} },
7326 { "quit", quit, 0, {0} },
7327 { "raise_toggle", raise_toggle, 0, {0} },
7328 { "resize", resize, FN_F_NOREPLAY, {.id = SWM_ARG_ID_DONTCENTER} },
7329 { "resize_centered", resize, FN_F_NOREPLAY, {.id = SWM_ARG_ID_CENTER} },
7330 { "restart", restart, 0, {0} },
7331 { "rg_1", focusrg, 0, {.id = 0} },
7332 { "rg_2", focusrg, 0, {.id = 1} },
7333 { "rg_3", focusrg, 0, {.id = 2} },
7334 { "rg_4", focusrg, 0, {.id = 3} },
7335 { "rg_5", focusrg, 0, {.id = 4} },
7336 { "rg_6", focusrg, 0, {.id = 5} },
7337 { "rg_7", focusrg, 0, {.id = 6} },
7338 { "rg_8", focusrg, 0, {.id = 7} },
7339 { "rg_9", focusrg, 0, {.id = 8} },
7340 { "rg_move_next", cyclerg, 0, {.id = SWM_ARG_ID_CYCLERG_MOVE_UP} },
7341 { "rg_move_prev", cyclerg, 0, {.id = SWM_ARG_ID_CYCLERG_MOVE_DOWN} },
7342 { "rg_next", cyclerg, 0, {.id = SWM_ARG_ID_CYCLERG_UP} },
7343 { "rg_prev", cyclerg, 0, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
7344 { "screen_next", cyclerg, 0, {.id = SWM_ARG_ID_CYCLERG_UP} },
7345 { "screen_prev", cyclerg, 0, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
7346 { "search_win", search_win, 0, {0} },
7347 { "search_workspace", search_workspace, 0, {0} },
7348 { "spawn_custom", NULL, 0, {0} },
7349 { "stack_balance", stack_config, 0, {.id = SWM_ARG_ID_STACKBALANCE} },
7350 { "stack_inc", stack_config, 0, {.id = SWM_ARG_ID_STACKINC} },
7351 { "stack_dec", stack_config, 0, {.id = SWM_ARG_ID_STACKDEC} },
7352 { "stack_reset", stack_config, 0, {.id = SWM_ARG_ID_STACKRESET} },
7353 { "swap_main", swapwin, 0, {.id = SWM_ARG_ID_SWAPMAIN} },
7354 { "swap_next", swapwin, 0, {.id = SWM_ARG_ID_SWAPNEXT} },
7355 { "swap_prev", swapwin, 0, {.id = SWM_ARG_ID_SWAPPREV} },
7356 { "uniconify", uniconify, 0, {0} },
7357 { "version", version, 0, {0} },
7358 { "width_grow", resize, 0, {.id = SWM_ARG_ID_WIDTHGROW} },
7359 { "width_shrink", resize, 0, {.id = SWM_ARG_ID_WIDTHSHRINK} },
7360 { "wind_del", wkill, 0, {.id = SWM_ARG_ID_DELETEWINDOW} },
7361 { "wind_kill", wkill, 0, {.id = SWM_ARG_ID_KILLWINDOW} },
7362 { "ws_1", switchws, 0, {.id = 0} },
7363 { "ws_2", switchws, 0, {.id = 1} },
7364 { "ws_3", switchws, 0, {.id = 2} },
7365 { "ws_4", switchws, 0, {.id = 3} },
7366 { "ws_5", switchws, 0, {.id = 4} },
7367 { "ws_6", switchws, 0, {.id = 5} },
7368 { "ws_7", switchws, 0, {.id = 6} },
7369 { "ws_8", switchws, 0, {.id = 7} },
7370 { "ws_9", switchws, 0, {.id = 8} },
7371 { "ws_10", switchws, 0, {.id = 9} },
7372 { "ws_11", switchws, 0, {.id = 10} },
7373 { "ws_12", switchws, 0, {.id = 11} },
7374 { "ws_13", switchws, 0, {.id = 12} },
7375 { "ws_14", switchws, 0, {.id = 13} },
7376 { "ws_15", switchws, 0, {.id = 14} },
7377 { "ws_16", switchws, 0, {.id = 15} },
7378 { "ws_17", switchws, 0, {.id = 16} },
7379 { "ws_18", switchws, 0, {.id = 17} },
7380 { "ws_19", switchws, 0, {.id = 18} },
7381 { "ws_20", switchws, 0, {.id = 19} },
7382 { "ws_21", switchws, 0, {.id = 20} },
7383 { "ws_22", switchws, 0, {.id = 21} },
7384 { "ws_next", cyclews, 0, {.id = SWM_ARG_ID_CYCLEWS_UP} },
7385 { "ws_next_all", cyclews, 0, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
7386 { "ws_next_move", cyclews, 0, {.id = SWM_ARG_ID_CYCLEWS_MOVE_UP} },
7387 { "ws_prev", cyclews, 0, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
7388 { "ws_prev_all", cyclews, 0, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
7389 { "ws_prev_move", cyclews, 0, {.id = SWM_ARG_ID_CYCLEWS_MOVE_DOWN} },
7390 { "ws_prior", priorws, 0, {0} },
7391 /* SWM_DEBUG actions MUST be here: */
7392 { "debug_toggle", debug_toggle, 0, {0} },
7393 { "dumpwins", dumpwins, 0, {0} },
7394 /* ALWAYS last: */
7395 { "invalid action", NULL, 0, {0} },
7396 };
7397
7398 void
7399 update_modkey(uint16_t mod)
7400 {
7401 struct binding *bp;
7402
7403 /* Replace all instances of the old mod key. */
7404 RB_FOREACH(bp, binding_tree, &bindings)
7405 if (bp->mod & mod_key)
7406 bp->mod = (bp->mod & ~mod_key) | mod;
7407 mod_key = mod;
7408 }
7409
7410 int
7411 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
7412 char ***ret_args)
7413 {
7414 struct spawn_prog *prog = NULL;
7415 int i, c;
7416 char *ap, **real_args;
7417
7418 /* suppress unused warning since var is needed */
7419 (void)args;
7420
7421 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
7422
7423 /* find program */
7424 TAILQ_FOREACH(prog, &spawns, entry) {
7425 if (strcasecmp(spawn_name, prog->name) == 0)
7426 break;
7427 }
7428 if (prog == NULL) {
7429 warnx("spawn_custom: program %s not found", spawn_name);
7430 return (-1);
7431 }
7432
7433 /* make room for expanded args */
7434 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
7435 err(1, "spawn_custom: calloc real_args");
7436
7437 /* expand spawn_args into real_args */
7438 for (i = c = 0; i < prog->argc; i++) {
7439 ap = prog->argv[i];
7440 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
7441 if (strcasecmp(ap, "$bar_border") == 0) {
7442 if ((real_args[c] =
7443 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
7444 == NULL)
7445 err(1, "spawn_custom border color");
7446 } else if (strcasecmp(ap, "$bar_color") == 0) {
7447 if ((real_args[c] =
7448 strdup(r->s->c[SWM_S_COLOR_BAR].name))
7449 == NULL)
7450 err(1, "spawn_custom bar color");
7451 } else if (strcasecmp(ap, "$bar_font") == 0) {
7452 if ((real_args[c] = strdup(bar_fonts))
7453 == NULL)
7454 err(1, "spawn_custom bar fonts");
7455 } else if (strcasecmp(ap, "$bar_font_color") == 0) {
7456 if ((real_args[c] =
7457 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
7458 == NULL)
7459 err(1, "spawn_custom color font");
7460 } else if (strcasecmp(ap, "$color_focus") == 0) {
7461 if ((real_args[c] =
7462 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
7463 == NULL)
7464 err(1, "spawn_custom color focus");
7465 } else if (strcasecmp(ap, "$color_focus_maximized") == 0) {
7466 if ((real_args[c] =
7467 strdup(r->s->c[SWM_S_COLOR_FOCUS_MAXIMIZED].name))
7468 == NULL)
7469 err(1, "spawn_custom color focus maximized");
7470 } else if (strcasecmp(ap, "$color_unfocus") == 0) {
7471 if ((real_args[c] =
7472 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
7473 == NULL)
7474 err(1, "spawn_custom color unfocus");
7475 } else if (strcasecmp(ap, "$color_unfocus_maximized") == 0) {
7476 if ((real_args[c] =
7477 strdup(r->s->c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].name))
7478 == NULL)
7479 err(1, "spawn_custom color unfocus maximized");
7480 } else if (strcasecmp(ap, "$region_index") == 0) {
7481 if (asprintf(&real_args[c], "%d",
7482 get_region_index(r) + 1) < 1)
7483 err(1, "spawn_custom region index");
7484 } else if (strcasecmp(ap, "$workspace_index") == 0) {
7485 if (asprintf(&real_args[c], "%d", r->ws->idx + 1) < 1)
7486 err(1, "spawn_custom workspace index");
7487 } else if (strcasecmp(ap, "$dmenu_bottom") == 0) {
7488 if (!bar_at_bottom)
7489 continue;
7490 if ((real_args[c] = strdup("-b")) == NULL)
7491 err(1, "spawn_custom workspace index");
7492 } else {
7493 /* no match --> copy as is */
7494 if ((real_args[c] = strdup(ap)) == NULL)
7495 err(1, "spawn_custom strdup(ap)");
7496 }
7497 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
7498 real_args[c]);
7499 ++c;
7500 }
7501
7502 #ifdef SWM_DEBUG
7503 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
7504 for (i = 0; i < c; ++i)
7505 DPRINTF("\"%s\" ", real_args[i]);
7506 DPRINTF("\n");
7507 #endif
7508 *ret_args = real_args;
7509 return (c);
7510 }
7511
7512 void
7513 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
7514 {
7515 union arg a;
7516 char **real_args;
7517 int spawn_argc, i;
7518
7519 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
7520 return;
7521 a.argv = real_args;
7522 if (fork() == 0)
7523 spawn(r->ws->idx, &a, true);
7524
7525 for (i = 0; i < spawn_argc; i++)
7526 free(real_args[i]);
7527 free(real_args);
7528 }
7529
7530 void
7531 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
7532 int *pid)
7533 {
7534 union arg a;
7535 char **real_args;
7536 int i, spawn_argc;
7537
7538 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
7539 return;
7540 a.argv = real_args;
7541
7542 if (pipe(select_list_pipe) == -1)
7543 err(1, "pipe error");
7544 if (pipe(select_resp_pipe) == -1)
7545 err(1, "pipe error");
7546
7547 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
7548 err(1, "could not disable SIGPIPE");
7549 switch (*pid = fork()) {
7550 case -1:
7551 err(1, "cannot fork");
7552 break;
7553 case 0: /* child */
7554 if (dup2(select_list_pipe[0], STDIN_FILENO) == -1)
7555 err(1, "dup2");
7556 if (dup2(select_resp_pipe[1], STDOUT_FILENO) == -1)
7557 err(1, "dup2");
7558 close(select_list_pipe[1]);
7559 close(select_resp_pipe[0]);
7560 spawn(r->ws->idx, &a, false);
7561 break;
7562 default: /* parent */
7563 close(select_list_pipe[0]);
7564 close(select_resp_pipe[1]);
7565 break;
7566 }
7567
7568 for (i = 0; i < spawn_argc; i++)
7569 free(real_args[i]);
7570 free(real_args);
7571 }
7572
7573 /* Argument tokenizer. */
7574 char *
7575 argsep(char **sp) {
7576 char *arg, *cp, *next;
7577 bool single_quoted = false, double_quoted = false;
7578
7579 if (*sp == NULL)
7580 return NULL;
7581
7582 /* Eat and move characters until end of argument is found. */
7583 for (arg = next = cp = *sp; *cp != '\0'; ++cp) {
7584 if (!double_quoted && *cp == '\'') {
7585 /* Eat single-quote. */
7586 single_quoted = !single_quoted;
7587 } else if (!single_quoted && *cp == '"') {
7588 /* Eat double-quote. */
7589 double_quoted = !double_quoted;
7590 } else if (!single_quoted && *cp == '\\' && *(cp + 1) == '"') {
7591 /* Eat backslash; copy escaped character to arg. */
7592 *next++ = *(++cp);
7593 } else if (!single_quoted && !double_quoted && *cp == '\\' &&
7594 (*(cp + 1) == '\'' || *(cp + 1) == ' ')) {
7595 /* Eat backslash; move escaped character. */
7596 *next++ = *(++cp);
7597 } else if (!single_quoted && !double_quoted &&
7598 (*cp == ' ' || *cp == '\t')) {
7599 /* Terminate argument. */
7600 *next++ = '\0';
7601 /* Point sp to beginning of next argument. */
7602 *sp = ++cp;
7603 break;
7604 } else {
7605 /* Move regular character. */
7606 *next++ = *cp;
7607 }
7608 }
7609
7610 /* Terminate argument if end of string. */
7611 if (*cp == '\0') {
7612 *next = '\0';
7613 *sp = NULL;
7614 }
7615
7616 return arg;
7617 }
7618
7619 void
7620 spawn_insert(const char *name, const char *args, int flags)
7621 {
7622 struct spawn_prog *sp;
7623 char *arg, *cp, *ptr;
7624
7625 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s[%s]\n", name, args);
7626
7627 if (args == NULL || *args == '\0')
7628 return;
7629
7630 if ((sp = calloc(1, sizeof *sp)) == NULL)
7631 err(1, "spawn_insert: calloc");
7632 if ((sp->name = strdup(name)) == NULL)
7633 err(1, "spawn_insert: strdup");
7634
7635 /* Convert the arguments to an argument list. */
7636 if ((ptr = cp = strdup(args)) == NULL)
7637 err(1, "spawn_insert: strdup");
7638 while ((arg = argsep(&ptr)) != NULL) {
7639 /* Null argument; skip it. */
7640 if (*arg == '\0')
7641 continue;
7642
7643 sp->argc++;
7644 if ((sp->argv = realloc(sp->argv, sp->argc *
7645 sizeof *sp->argv)) == NULL)
7646 err(1, "spawn_insert: realloc");
7647 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
7648 err(1, "spawn_insert: strdup");
7649 }
7650 free(cp);
7651
7652 sp->flags = flags;
7653
7654 DNPRINTF(SWM_D_SPAWN, "arg %d: [%s]\n", sp->argc, sp->argv[sp->argc-1]);
7655 TAILQ_INSERT_TAIL(&spawns, sp, entry);
7656 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
7657 }
7658
7659 void
7660 spawn_remove(struct spawn_prog *sp)
7661 {
7662 int i;
7663
7664 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
7665
7666 TAILQ_REMOVE(&spawns, sp, entry);
7667 for (i = 0; i < sp->argc; i++)
7668 free(sp->argv[i]);
7669 free(sp->argv);
7670 free(sp->name);
7671 free(sp);
7672
7673 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
7674 }
7675
7676 void
7677 clear_spawns(void)
7678 {
7679 struct spawn_prog *sp;
7680
7681 while ((sp = TAILQ_FIRST(&spawns)) != NULL) {
7682 spawn_remove(sp);
7683 }
7684 }
7685
7686 struct spawn_prog*
7687 spawn_find(const char *name)
7688 {
7689 struct spawn_prog *sp;
7690
7691 TAILQ_FOREACH(sp, &spawns, entry)
7692 if (strcasecmp(sp->name, name) == 0)
7693 return sp;
7694
7695 return NULL;
7696 }
7697
7698 void
7699 setspawn(const char *name, const char *args, int flags)
7700 {
7701 struct spawn_prog *sp;
7702
7703 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
7704
7705 if (name == NULL)
7706 return;
7707
7708 /* Remove any old spawn under the same name. */
7709 if ((sp = spawn_find(name)) != NULL)
7710 spawn_remove(sp);
7711
7712 if (*args != '\0')
7713 spawn_insert(name, args, flags);
7714 else
7715 warnx("error: setspawn: cannot find program: %s", name);
7716
7717 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
7718 }
7719
7720 int
7721 setconfspawn(const char *selector, const char *value, int flags)
7722 {
7723 char *args;
7724
7725 if (selector == NULL || strlen(selector) == 0)
7726 return (1);
7727
7728 args = expand_tilde(value);
7729
7730 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
7731
7732 setspawn(selector, args, flags);
7733 free(args);
7734
7735 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
7736 return (0);
7737 }
7738
7739 void
7740 validate_spawns(void)
7741 {
7742 struct binding *bp;
7743 struct spawn_prog *sp;
7744 char which[PATH_MAX];
7745 size_t i;
7746
7747 RB_FOREACH(bp, binding_tree, &bindings) {
7748 if (bp->action != FN_SPAWN_CUSTOM)
7749 continue;
7750
7751 /* find program */
7752 sp = spawn_find(bp->spawn_name);
7753 if (sp == NULL || sp->flags & SWM_SPAWN_OPTIONAL)
7754 continue;
7755
7756 /* verify we have the goods */
7757 snprintf(which, sizeof which, "which %s", sp->argv[0]);
7758 DNPRINTF(SWM_D_CONF, "validate_spawns: which %s\n",
7759 sp->argv[0]);
7760 for (i = strlen("which "); i < strlen(which); i++)
7761 if (which[i] == ' ') {
7762 which[i] = '\0';
7763 break;
7764 }
7765 if (system(which) != 0)
7766 add_startup_exception("could not find %s",
7767 &which[strlen("which ")]);
7768 }
7769 }
7770
7771 void
7772 setup_spawn(void)
7773 {
7774 setconfspawn("lock", "xlock", 0);
7775
7776 setconfspawn("term", "xterm", 0);
7777 setconfspawn("spawn_term", "xterm", 0);
7778
7779 setconfspawn("menu", "dmenu_run"
7780 " $dmenu_bottom"
7781 " -fn $bar_font"
7782 " -nb $bar_color"
7783 " -nf $bar_font_color"
7784 " -sb $bar_border"
7785 " -sf $bar_color", 0);
7786
7787 setconfspawn("search", "dmenu"
7788 " $dmenu_bottom"
7789 " -i"
7790 " -fn $bar_font"
7791 " -nb $bar_color"
7792 " -nf $bar_font_color"
7793 " -sb $bar_border"
7794 " -sf $bar_color", 0);
7795
7796 setconfspawn("name_workspace", "dmenu"
7797 " $dmenu_bottom"
7798 " -p Workspace"
7799 " -fn $bar_font"
7800 " -nb $bar_color"
7801 " -nf $bar_font_color"
7802 " -sb $bar_border"
7803 " -sf $bar_color", 0);
7804
7805 /* These are not verified for existence, even with a binding set. */
7806 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL);
7807 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL);
7808 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL);
7809 }
7810
7811 /* bindings */
7812 #define SWM_MODNAME_SIZE 32
7813 #define SWM_KEY_WS "\n+ \t"
7814 int
7815 parsebinding(const char *bindstr, uint16_t *mod, enum binding_type *type,
7816 uint32_t *val, uint32_t *flags)
7817 {
7818 char *str, *cp, *name;
7819 KeySym ks, lks, uks;
7820
7821 DNPRINTF(SWM_D_KEY, "parsebinding: enter [%s]\n", bindstr);
7822 if (mod == NULL || val == NULL) {
7823 DNPRINTF(SWM_D_KEY, "parsebinding: no mod or key vars\n");
7824 return (1);
7825 }
7826 if (bindstr == NULL || strlen(bindstr) == 0) {
7827 DNPRINTF(SWM_D_KEY, "parsebinding: no bindstr\n");
7828 return (1);
7829 }
7830
7831 if ((cp = str = strdup(bindstr)) == NULL)
7832 err(1, "parsebinding: strdup");
7833
7834 *val = XCB_NO_SYMBOL;
7835 *mod = 0;
7836 *flags = 0;
7837 *type = KEYBIND;
7838 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
7839 DNPRINTF(SWM_D_KEY, "parsebinding: entry [%s]\n", name);
7840 if (cp)
7841 cp += (long)strspn(cp, SWM_KEY_WS);
7842 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
7843 *mod |= mod_key;
7844 else if (strncasecmp(name, "Mod1", SWM_MODNAME_SIZE) == 0)
7845 *mod |= XCB_MOD_MASK_1;
7846 else if (strncasecmp(name, "Mod2", SWM_MODNAME_SIZE) == 0)
7847 *mod |= XCB_MOD_MASK_2;
7848 else if (strncmp(name, "Mod3", SWM_MODNAME_SIZE) == 0)
7849 *mod |= XCB_MOD_MASK_3;
7850 else if (strncmp(name, "Mod4", SWM_MODNAME_SIZE) == 0)
7851 *mod |= XCB_MOD_MASK_4;
7852 else if (strncmp(name, "Mod5", SWM_MODNAME_SIZE) == 0)
7853 *mod |= XCB_MOD_MASK_5;
7854 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
7855 *mod |= XCB_MOD_MASK_SHIFT;
7856 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
7857 *mod |= XCB_MOD_MASK_CONTROL;
7858 else if (strncasecmp(name, "ANYMOD", SWM_MODNAME_SIZE) == 0)
7859 *mod |= XCB_MOD_MASK_ANY;
7860 else if (strncasecmp(name, "REPLAY", SWM_MODNAME_SIZE) == 0)
7861 *flags |= BINDING_F_REPLAY;
7862 else if (sscanf(name, "Button%u", val) == 1) {
7863 DNPRINTF(SWM_D_KEY, "parsebinding: button %u\n", *val);
7864 *type = BTNBIND;
7865 if (*val > 255 || *val == 0) {
7866 DNPRINTF(SWM_D_KEY,
7867 "parsebinding: invalid button %u\n", *val);
7868 return (1);
7869 }
7870 } else {
7871 /* TODO: do this without Xlib. */
7872 ks = XStringToKeysym(name);
7873 if (ks == NoSymbol) {
7874 DNPRINTF(SWM_D_KEY,
7875 "parsebinding: invalid key %s\n", name);
7876 free(str);
7877 return (1);
7878 }
7879
7880 XConvertCase(ks, &lks, &uks);
7881 *val = (uint32_t)lks;
7882 }
7883 }
7884
7885 /* If ANYMOD was specified, ignore the rest. */
7886 if (*mod & XCB_MOD_MASK_ANY)
7887 *mod = XCB_MOD_MASK_ANY;
7888
7889 free(str);
7890 DNPRINTF(SWM_D_KEY, "parsebinding: leave ok\n");
7891 return (0);
7892 }
7893
7894 char *
7895 strdupsafe(const char *str)
7896 {
7897 if (str == NULL)
7898 return (NULL);
7899 else
7900 return (strdup(str));
7901 }
7902
7903 int
7904 binding_cmp(struct binding *bp1, struct binding *bp2)
7905 {
7906 if (bp1->type < bp2->type)
7907 return (-1);
7908 if (bp1->type > bp2->type)
7909 return (1);
7910
7911 if (bp1->value < bp2->value)
7912 return (-1);
7913 if (bp1->value > bp2->value)
7914 return (1);
7915
7916 if (bp1->mod < bp2->mod)
7917 return (-1);
7918 if (bp1->mod > bp2->mod)
7919 return (1);
7920
7921 return (0);
7922 }
7923
7924 void
7925 binding_insert(uint16_t mod, enum binding_type type, uint32_t val,
7926 enum actionid aid, uint32_t flags, const char *spawn_name)
7927 {
7928 struct binding *bp;
7929
7930 DNPRINTF(SWM_D_KEY, "binding_insert: mod: %u, type: %d, val: %u, "
7931 "action: %s(%d), spawn_name: %s\n", mod, type, val,
7932 actions[aid].name, aid, spawn_name);
7933
7934 if ((bp = malloc(sizeof *bp)) == NULL)
7935 err(1, "binding_insert: malloc");
7936
7937 bp->mod = mod;
7938 bp->type = type;
7939 bp->value = val;
7940 bp->action = aid;
7941 bp->flags = flags;
7942 bp->spawn_name = strdupsafe(spawn_name);
7943 RB_INSERT(binding_tree, &bindings, bp);
7944
7945 DNPRINTF(SWM_D_KEY, "binding_insert: leave\n");
7946 }
7947
7948 struct binding *
7949 binding_lookup(uint16_t mod, enum binding_type type, uint32_t val)
7950 {
7951 struct binding bp;
7952
7953 bp.mod = mod;
7954 bp.type = type;
7955 bp.value = val;
7956
7957 return (RB_FIND(binding_tree, &bindings, &bp));
7958 }
7959
7960 void
7961 binding_remove(struct binding *bp)
7962 {
7963 DNPRINTF(SWM_D_KEY, "binding_remove: mod: %u, type: %d, val: %u, "
7964 "action: %s(%d), spawn_name: %s\n", bp->mod, bp->type, bp->value,
7965 actions[bp->action].name, bp->action, bp->spawn_name);
7966
7967 RB_REMOVE(binding_tree, &bindings, bp);
7968 free(bp->spawn_name);
7969 free(bp);
7970
7971 DNPRINTF(SWM_D_KEY, "binding_remove: leave\n");
7972 }
7973
7974 void
7975 setbinding(uint16_t mod, enum binding_type type, uint32_t val,
7976 enum actionid aid, uint32_t flags, const char *spawn_name)
7977 {
7978 struct binding *bp;
7979
7980 DNPRINTF(SWM_D_KEY, "setbinding: enter %s [%s]\n",
7981 actions[aid].name, spawn_name);
7982
7983 /* Unbind any existing. Loop is to handle MOD_MASK_ANY. */
7984 while ((bp = binding_lookup(mod, type, val)))
7985 binding_remove(bp);
7986
7987 if (aid != FN_INVALID)
7988 binding_insert(mod, type, val, aid, flags, spawn_name);
7989
7990 DNPRINTF(SWM_D_KEY, "setbinding: leave\n");
7991 }
7992
7993 int
7994 setconfbinding(const char *selector, const char *value, int flags)
7995 {
7996 struct spawn_prog *sp;
7997 uint32_t keybtn, opts;
7998 uint16_t mod;
7999 enum actionid aid;
8000 enum binding_type type;
8001
8002 /* suppress unused warning since var is needed */
8003 (void)flags;
8004
8005 DNPRINTF(SWM_D_KEY, "setconfbinding: enter selector: [%s], "
8006 "value: [%s]\n", selector, value);
8007 if (selector == NULL || strlen(selector) == 0) {
8008 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
8009 if (parsebinding(value, &mod, &type, &keybtn, &opts) == 0) {
8010 setbinding(mod, type, keybtn, FN_INVALID, opts, NULL);
8011 return (0);
8012 } else
8013 return (1);
8014 }
8015 /* search by key function name */
8016 for (aid = 0; aid < FN_INVALID; aid++) {
8017 if (strncasecmp(selector, actions[aid].name,
8018 SWM_FUNCNAME_LEN) == 0) {
8019 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
8020 "action\n", selector);
8021 if (parsebinding(value, &mod, &type, &keybtn,
8022 &opts) == 0) {
8023 setbinding(mod, type, keybtn, aid, opts, NULL);
8024 return (0);
8025 } else
8026 return (1);
8027 }
8028 }
8029 /* search by custom spawn name */
8030 if ((sp = spawn_find(selector)) != NULL) {
8031 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
8032 "spawn\n", selector);
8033 if (parsebinding(value, &mod, &type, &keybtn, &opts) == 0) {
8034 setbinding(mod, type, keybtn, FN_SPAWN_CUSTOM, opts,
8035 sp->name);
8036 return (0);
8037 } else
8038 return (1);
8039 }
8040 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
8041 return (1);
8042 }
8043
8044 #define MODSHIFT MODKEY | XCB_MOD_MASK_SHIFT
8045 void
8046 setup_keybindings(void)
8047 {
8048 #define BINDKEY(m, k, a) setbinding(m, KEYBIND, k, a, 0, NULL)
8049 #define BINDKEYSPAWN(m, k, s) setbinding(m, KEYBIND, k, FN_SPAWN_CUSTOM, 0, s)
8050 BINDKEY(MODKEY, XK_b, FN_BAR_TOGGLE);
8051 BINDKEY(MODSHIFT, XK_b, FN_BAR_TOGGLE_WS);
8052 BINDKEY(MODKEY, XK_b, FN_BAR_TOGGLE);
8053 BINDKEY(MODSHIFT, XK_b, FN_BAR_TOGGLE_WS);
8054 BINDKEY(MODKEY, XK_v, FN_BUTTON2);
8055 BINDKEY(MODKEY, XK_space, FN_CYCLE_LAYOUT);
8056 BINDKEY(MODSHIFT, XK_backslash, FN_FLIP_LAYOUT);
8057 BINDKEY(MODKEY, XK_t, FN_FLOAT_TOGGLE);
8058 BINDKEY(MODKEY, XK_m, FN_FOCUS_MAIN);
8059 BINDKEY(MODKEY, XK_j, FN_FOCUS_NEXT);
8060 BINDKEY(MODKEY, XK_Tab, FN_FOCUS_NEXT);
8061 BINDKEY(MODKEY, XK_k, FN_FOCUS_PREV);
8062 BINDKEY(MODSHIFT, XK_Tab, FN_FOCUS_PREV);
8063 BINDKEY(MODKEY, XK_u, FN_FOCUS_URGENT);
8064 BINDKEY(MODKEY, XK_e, FN_MAXIMIZE_TOGGLE);
8065 BINDKEY(MODSHIFT, XK_equal, FN_HEIGHT_GROW);
8066 BINDKEY(MODSHIFT, XK_minus, FN_HEIGHT_SHRINK);
8067 BINDKEY(MODKEY, XK_w, FN_ICONIFY);
8068 BINDKEY(MODKEY, XK_h, FN_MASTER_SHRINK);
8069 BINDKEY(MODKEY, XK_l, FN_MASTER_GROW);
8070 BINDKEY(MODKEY, XK_comma, FN_MASTER_ADD);
8071 BINDKEY(MODKEY, XK_period, FN_MASTER_DEL);
8072 BINDKEY(MODSHIFT, XK_bracketright, FN_MOVE_DOWN);
8073 BINDKEY(MODKEY, XK_bracketleft, FN_MOVE_LEFT);
8074 BINDKEY(MODKEY, XK_bracketright, FN_MOVE_RIGHT);
8075 BINDKEY(MODSHIFT, XK_bracketleft, FN_MOVE_UP);
8076 BINDKEY(MODSHIFT, XK_KP_End, FN_MVRG_1);
8077 BINDKEY(MODSHIFT, XK_KP_Down, FN_MVRG_2);
8078 BINDKEY(MODSHIFT, XK_KP_Next, FN_MVRG_3);
8079 BINDKEY(MODSHIFT, XK_KP_Left, FN_MVRG_4);
8080 BINDKEY(MODSHIFT, XK_KP_Begin, FN_MVRG_5);
8081 BINDKEY(MODSHIFT, XK_KP_Right, FN_MVRG_6);
8082 BINDKEY(MODSHIFT, XK_KP_Home, FN_MVRG_7);
8083 BINDKEY(MODSHIFT, XK_KP_Up, FN_MVRG_8);
8084 BINDKEY(MODSHIFT, XK_KP_Prior, FN_MVRG_9);
8085 BINDKEY(MODSHIFT, XK_1, FN_MVWS_1);
8086 BINDKEY(MODSHIFT, XK_2, FN_MVWS_2);
8087 BINDKEY(MODSHIFT, XK_3, FN_MVWS_3);
8088 BINDKEY(MODSHIFT, XK_4, FN_MVWS_4);
8089 BINDKEY(MODSHIFT, XK_5, FN_MVWS_5);
8090 BINDKEY(MODSHIFT, XK_6, FN_MVWS_6);
8091 BINDKEY(MODSHIFT, XK_7, FN_MVWS_7);
8092 BINDKEY(MODSHIFT, XK_8, FN_MVWS_8);
8093 BINDKEY(MODSHIFT, XK_9, FN_MVWS_9);
8094 BINDKEY(MODSHIFT, XK_0, FN_MVWS_10);
8095 BINDKEY(MODSHIFT, XK_F1, FN_MVWS_11);
8096 BINDKEY(MODSHIFT, XK_F2, FN_MVWS_12);
8097 BINDKEY(MODSHIFT, XK_F3, FN_MVWS_13);
8098 BINDKEY(MODSHIFT, XK_F4, FN_MVWS_14);
8099 BINDKEY(MODSHIFT, XK_F5, FN_MVWS_15);
8100 BINDKEY(MODSHIFT, XK_F6, FN_MVWS_16);
8101 BINDKEY(MODSHIFT, XK_F7, FN_MVWS_17);
8102 BINDKEY(MODSHIFT, XK_F8, FN_MVWS_18);
8103 BINDKEY(MODSHIFT, XK_F9, FN_MVWS_19);
8104 BINDKEY(MODSHIFT, XK_F10, FN_MVWS_20);
8105 BINDKEY(MODSHIFT, XK_F11, FN_MVWS_21);
8106 BINDKEY(MODSHIFT, XK_F12, FN_MVWS_22);
8107 BINDKEY(MODSHIFT, XK_slash, FN_NAME_WORKSPACE);
8108 BINDKEY(MODSHIFT, XK_q, FN_QUIT);
8109 BINDKEY(MODSHIFT, XK_r, FN_RAISE_TOGGLE);
8110 BINDKEY(MODKEY, XK_q, FN_RESTART);
8111 BINDKEY(MODKEY, XK_KP_End, FN_RG_1);
8112 BINDKEY(MODKEY, XK_KP_Down, FN_RG_2);
8113 BINDKEY(MODKEY, XK_KP_Next, FN_RG_3);
8114 BINDKEY(MODKEY, XK_KP_Left, FN_RG_4);
8115 BINDKEY(MODKEY, XK_KP_Begin, FN_RG_5);
8116 BINDKEY(MODKEY, XK_KP_Right, FN_RG_6);
8117 BINDKEY(MODKEY, XK_KP_Home, FN_RG_7);
8118 BINDKEY(MODKEY, XK_KP_Up, FN_RG_8);
8119 BINDKEY(MODKEY, XK_KP_Prior, FN_RG_9);
8120 BINDKEY(MODSHIFT, XK_Right, FN_RG_NEXT);
8121 BINDKEY(MODSHIFT, XK_Left, FN_RG_PREV);
8122 BINDKEY(MODKEY, XK_f, FN_SEARCH_WIN);
8123 BINDKEY(MODKEY, XK_slash, FN_SEARCH_WORKSPACE);
8124 BINDKEYSPAWN(MODSHIFT, XK_i, "initscr");
8125 BINDKEYSPAWN(MODSHIFT, XK_Delete, "lock");
8126 BINDKEYSPAWN(MODKEY, XK_p, "menu");
8127 BINDKEYSPAWN(MODKEY, XK_s, "screenshot_all");
8128 BINDKEYSPAWN(MODSHIFT, XK_s, "screenshot_wind");
8129 BINDKEYSPAWN(MODSHIFT, XK_Return, "term");
8130 BINDKEY(MODSHIFT, XK_comma, FN_STACK_INC);
8131 BINDKEY(MODSHIFT, XK_period, FN_STACK_DEC);
8132 BINDKEY(MODSHIFT, XK_space, FN_STACK_RESET);
8133 BINDKEY(MODKEY, XK_Return, FN_SWAP_MAIN);
8134 BINDKEY(MODSHIFT, XK_j, FN_SWAP_NEXT);
8135 BINDKEY(MODSHIFT, XK_k, FN_SWAP_PREV);
8136 BINDKEY(MODSHIFT, XK_w, FN_UNICONIFY);
8137 BINDKEY(MODSHIFT, XK_v, FN_VERSION);
8138 BINDKEY(MODKEY, XK_equal, FN_WIDTH_GROW);
8139 BINDKEY(MODKEY, XK_minus, FN_WIDTH_SHRINK);
8140 BINDKEY(MODKEY, XK_x, FN_WIND_DEL);
8141 BINDKEY(MODSHIFT, XK_x, FN_WIND_KILL);
8142 BINDKEY(MODKEY, XK_1, FN_WS_1);
8143 BINDKEY(MODKEY, XK_2, FN_WS_2);
8144 BINDKEY(MODKEY, XK_3, FN_WS_3);
8145 BINDKEY(MODKEY, XK_4, FN_WS_4);
8146 BINDKEY(MODKEY, XK_5, FN_WS_5);
8147 BINDKEY(MODKEY, XK_6, FN_WS_6);
8148 BINDKEY(MODKEY, XK_7, FN_WS_7);
8149 BINDKEY(MODKEY, XK_8, FN_WS_8);
8150 BINDKEY(MODKEY, XK_9, FN_WS_9);
8151 BINDKEY(MODKEY, XK_0, FN_WS_10);
8152 BINDKEY(MODKEY, XK_F1, FN_WS_11);
8153 BINDKEY(MODKEY, XK_F2, FN_WS_12);
8154 BINDKEY(MODKEY, XK_F3, FN_WS_13);
8155 BINDKEY(MODKEY, XK_F4, FN_WS_14);
8156 BINDKEY(MODKEY, XK_F5, FN_WS_15);
8157 BINDKEY(MODKEY, XK_F6, FN_WS_16);
8158 BINDKEY(MODKEY, XK_F7, FN_WS_17);
8159 BINDKEY(MODKEY, XK_F8, FN_WS_18);
8160 BINDKEY(MODKEY, XK_F9, FN_WS_19);
8161 BINDKEY(MODKEY, XK_F10, FN_WS_20);
8162 BINDKEY(MODKEY, XK_F11, FN_WS_21);
8163 BINDKEY(MODKEY, XK_F12, FN_WS_22);
8164 BINDKEY(MODKEY, XK_Right, FN_WS_NEXT);
8165 BINDKEY(MODKEY, XK_Left, FN_WS_PREV);
8166 BINDKEY(MODKEY, XK_Up, FN_WS_NEXT_ALL);
8167 BINDKEY(MODKEY, XK_Down, FN_WS_PREV_ALL);
8168 BINDKEY(MODSHIFT, XK_Up, FN_WS_NEXT_MOVE);
8169 BINDKEY(MODSHIFT, XK_Down, FN_WS_PREV_MOVE);
8170 BINDKEY(MODKEY, XK_a, FN_WS_PRIOR);
8171 #ifdef SWM_DEBUG
8172 BINDKEY(MODKEY, XK_d, FN_DEBUG_TOGGLE);
8173 BINDKEY(MODSHIFT, XK_d, FN_DUMPWINS);
8174 #endif
8175 #undef BINDKEY
8176 #undef BINDKEYSPAWN
8177 }
8178
8179 void
8180 setup_btnbindings(void)
8181 {
8182 setbinding(ANYMOD, BTNBIND, XCB_BUTTON_INDEX_1, FN_FOCUS,
8183 BINDING_F_REPLAY, NULL);
8184 setbinding(MODKEY, BTNBIND, XCB_BUTTON_INDEX_3, FN_RESIZE, 0, NULL);
8185 setbinding(MODSHIFT, BTNBIND, XCB_BUTTON_INDEX_3, FN_RESIZE_CENTERED, 0,
8186 NULL);
8187 setbinding(MODKEY, BTNBIND, XCB_BUTTON_INDEX_1, FN_MOVE, 0, NULL);
8188 }
8189 #undef MODSHIFT
8190
8191 void
8192 clear_bindings(void)
8193 {
8194 struct binding *bp;
8195
8196 while ((bp = RB_ROOT(&bindings)))
8197 binding_remove(bp);
8198 }
8199
8200 void
8201 clear_keybindings(void)
8202 {
8203 struct binding *bp, *bptmp;
8204
8205 RB_FOREACH_SAFE(bp, binding_tree, &bindings, bptmp) {
8206 if (bp->type != KEYBIND)
8207 continue;
8208 binding_remove(bp);
8209 }
8210 }
8211
8212 int
8213 setkeymapping(const char *selector, const char *value, int flags)
8214 {
8215 char *keymapping_file;
8216
8217 /* suppress unused warnings since vars are needed */
8218 (void)selector;
8219 (void)flags;
8220
8221 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
8222
8223 keymapping_file = expand_tilde(value);
8224
8225 clear_keybindings();
8226 /* load new key bindings; if it fails, revert to default bindings */
8227 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
8228 clear_keybindings();
8229 setup_keybindings();
8230 }
8231
8232 free(keymapping_file);
8233
8234 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
8235 return (0);
8236 }
8237
8238 void
8239 updatenumlockmask(void)
8240 {
8241 unsigned int i, j;
8242 xcb_get_modifier_mapping_reply_t *modmap_r;
8243 xcb_keycode_t *modmap, kc, *keycode;
8244
8245 numlockmask = 0;
8246
8247 modmap_r = xcb_get_modifier_mapping_reply(conn,
8248 xcb_get_modifier_mapping(conn),
8249 NULL);
8250 if (modmap_r) {
8251 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
8252 for (i = 0; i < 8; i++) {
8253 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
8254 kc = modmap[i * modmap_r->keycodes_per_modifier
8255 + j];
8256 keycode = xcb_key_symbols_get_keycode(syms,
8257 XK_Num_Lock);
8258 if (keycode) {
8259 if (kc == *keycode)
8260 numlockmask = (1 << i);
8261 free(keycode);
8262 }
8263 }
8264 }
8265 free(modmap_r);
8266 }
8267 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
8268 }
8269
8270 void
8271 grabkeys(void)
8272 {
8273 struct binding *bp;
8274 int num_screens, k, j;
8275 uint16_t modifiers[4];
8276 xcb_keycode_t *code;
8277
8278 DNPRINTF(SWM_D_MISC, "grabkeys\n");
8279 updatenumlockmask();
8280
8281 modifiers[0] = 0;
8282 modifiers[1] = numlockmask;
8283 modifiers[2] = XCB_MOD_MASK_LOCK;
8284 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
8285
8286 num_screens = get_screen_count();
8287 for (k = 0; k < num_screens; k++) {
8288 if (TAILQ_EMPTY(&screens[k].rl))
8289 continue;
8290 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
8291 XCB_MOD_MASK_ANY);
8292 RB_FOREACH(bp, binding_tree, &bindings) {
8293 if (bp->type != KEYBIND)
8294 continue;
8295
8296 /* If there is a catch-all, only bind that. */
8297 if ((binding_lookup(ANYMOD, KEYBIND, bp->value)) &&
8298 bp->mod != ANYMOD)
8299 continue;
8300
8301 /* Skip unused ws binds. */
8302 if ((int)bp->action > FN_WS_1 + workspace_limit - 1 &&
8303 bp->action <= FN_WS_22)
8304 continue;
8305
8306 /* Skip unused mvws binds. */
8307 if ((int)bp->action > FN_MVWS_1 + workspace_limit - 1 &&
8308 bp->action <= FN_MVWS_22)
8309 continue;
8310
8311 if ((code = xcb_key_symbols_get_keycode(syms,
8312 bp->value)) == NULL)
8313 continue;
8314
8315 if (bp->mod == XCB_MOD_MASK_ANY) {
8316 /* All modifiers are grabbed in one pass. */
8317 DNPRINTF(SWM_D_MOUSE, "grabkeys: grab, "
8318 "key: %u, modifiers: %d\n", bp->value,
8319 bp->mod);
8320 xcb_grab_key(conn, 1, screens[k].root,
8321 bp->mod, *code, XCB_GRAB_MODE_ASYNC,
8322 XCB_GRAB_MODE_SYNC);
8323 } else {
8324 /* Need to grab each modifier permutation. */
8325 for (j = 0; j < LENGTH(modifiers); j++) {
8326 DNPRINTF(SWM_D_MOUSE, "grabkeys: grab, "
8327 "key: %u, modifiers: %d\n",
8328 bp->value, bp->mod | modifiers[j]);
8329 xcb_grab_key(conn, 1,
8330 screens[k].root,
8331 bp->mod | modifiers[j],
8332 *code, XCB_GRAB_MODE_ASYNC,
8333 XCB_GRAB_MODE_SYNC);
8334 }
8335 }
8336 free(code);
8337 }
8338 }
8339 }
8340
8341 void
8342 grabbuttons(void)
8343 {
8344 struct binding *bp;
8345 int num_screens, i, k;
8346 uint16_t modifiers[4];
8347
8348 DNPRINTF(SWM_D_MOUSE, "grabbuttons\n");
8349 updatenumlockmask();
8350
8351 modifiers[0] = 0;
8352 modifiers[1] = numlockmask;
8353 modifiers[2] = XCB_MOD_MASK_LOCK;
8354 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
8355
8356 num_screens = get_screen_count();
8357 for (k = 0; k < num_screens; k++) {
8358 if (TAILQ_EMPTY(&screens[k].rl))
8359 continue;
8360 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, screens[k].root,
8361 XCB_MOD_MASK_ANY);
8362 RB_FOREACH(bp, binding_tree, &bindings) {
8363 if (bp->type != BTNBIND)
8364 continue;
8365
8366 /* If there is a catch-all, only bind that. */
8367 if ((binding_lookup(ANYMOD, BTNBIND, bp->value)) &&
8368 bp->mod != ANYMOD)
8369 continue;
8370
8371 /* Skip unused ws binds. */
8372 if ((int)bp->action > FN_WS_1 + workspace_limit - 1 &&
8373 bp->action <= FN_WS_22)
8374 continue;
8375
8376 /* Skip unused mvws binds. */
8377 if ((int)bp->action > FN_MVWS_1 + workspace_limit - 1 &&
8378 bp->action <= FN_MVWS_22)
8379 continue;
8380
8381 if (bp->mod == XCB_MOD_MASK_ANY) {
8382 /* All modifiers are grabbed in one pass. */
8383 DNPRINTF(SWM_D_MOUSE, "grabbuttons: grab, "
8384 "button: %u, modifiers: %d\n", bp->value,
8385 bp->mod);
8386 xcb_grab_button(conn, 0, screens[k].root,
8387 BUTTONMASK, XCB_GRAB_MODE_SYNC,
8388 XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
8389 XCB_CURSOR_NONE, bp->value, bp->mod);
8390 } else {
8391 /* Need to grab each modifier permutation. */
8392 for (i = 0; i < LENGTH(modifiers); ++i) {
8393 DNPRINTF(SWM_D_MOUSE, "grabbuttons: "
8394 "grab, button: %u, modifiers: %u\n",
8395 bp->value, bp->mod | modifiers[i]);
8396 xcb_grab_button(conn, 0,
8397 screens[k].root, BUTTONMASK,
8398 XCB_GRAB_MODE_SYNC,
8399 XCB_GRAB_MODE_ASYNC,
8400 XCB_WINDOW_NONE,
8401 XCB_CURSOR_NONE, bp->value,
8402 bp->mod | modifiers[i]);
8403 }
8404 }
8405 }
8406 }
8407 }
8408
8409 const char *quirkname[] = {
8410 "NONE", /* config string for "no value" */
8411 "FLOAT",
8412 "TRANSSZ",
8413 "ANYWHERE",
8414 "XTERM_FONTADJ",
8415 "FULLSCREEN",
8416 "FOCUSPREV",
8417 "NOFOCUSONMAP",
8418 "FOCUSONMAP_SINGLE",
8419 "OBEYAPPFOCUSREQ",
8420 "IGNOREPID",
8421 "IGNORESPAWNWS",
8422 "NOFOCUSCYCLE",
8423 "MINIMALBORDER",
8424 };
8425
8426 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
8427 #define SWM_Q_DELIM "\n|+ \t"
8428 int
8429 parsequirks(const char *qstr, uint32_t *quirk, int *ws)
8430 {
8431 char *str, *cp, *name;
8432 int i;
8433
8434 if (quirk == NULL || qstr == NULL)
8435 return (1);
8436
8437 if ((cp = str = strdup(qstr)) == NULL)
8438 err(1, "parsequirks: strdup");
8439
8440 *quirk = 0;
8441 while ((name = strsep(&cp, SWM_Q_DELIM)) != NULL) {
8442 if (cp)
8443 cp += (long)strspn(cp, SWM_Q_DELIM);
8444
8445 if (sscanf(name, "WS[%d]", ws) == 1) {
8446 if (*ws > 0)
8447 *ws -= 1;
8448 continue;
8449 }
8450
8451 for (i = 0; i < LENGTH(quirkname); i++) {
8452 if (strncasecmp(name, quirkname[i],
8453 SWM_QUIRK_LEN) == 0) {
8454 DNPRINTF(SWM_D_QUIRK,
8455 "parsequirks: %s\n", name);
8456 if (i == 0) {
8457 *quirk = 0;
8458 free(str);
8459 return (0);
8460 }
8461 *quirk |= 1 << (i-1);
8462 break;
8463 }
8464 }
8465 if (i >= LENGTH(quirkname)) {
8466 DNPRINTF(SWM_D_QUIRK,
8467 "parsequirks: invalid quirk [%s]\n", name);
8468 free(str);
8469 return (1);
8470 }
8471 }
8472
8473 free(str);
8474 return (0);
8475 }
8476
8477 void
8478 quirk_insert(const char *class, const char *instance, const char *name,
8479 uint32_t quirk, int ws)
8480 {
8481 struct quirk *qp;
8482 char *str;
8483 bool failed = false;
8484
8485 DNPRINTF(SWM_D_QUIRK, "quirk_insert: class: %s, instance: %s, name: %s,"
8486 " value: %u, ws: %d\n", class, instance, name, quirk, ws);
8487
8488 if ((qp = malloc(sizeof *qp)) == NULL)
8489 err(1, "quirk_insert: malloc");
8490
8491 if ((qp->class = strdup(class)) == NULL)
8492 err(1, "quirk_insert: strdup");
8493 if ((qp->instance = strdup(instance)) == NULL)
8494 err(1, "quirk_insert: strdup");
8495 if ((qp->name = strdup(name)) == NULL)
8496 err(1, "quirk_insert: strdup");
8497
8498 if (asprintf(&str, "^%s$", class) == -1)
8499 err(1, "quirk_insert: asprintf");
8500 if (regcomp(&qp->regex_class, str, REG_EXTENDED | REG_NOSUB)) {
8501 add_startup_exception("regex failed to compile quirk 'class' "
8502 "field: %s", class);
8503 failed = true;
8504 }
8505 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
8506 free(str);
8507
8508 if (asprintf(&str, "^%s$", instance) == -1)
8509 err(1, "quirk_insert: asprintf");
8510 if (regcomp(&qp->regex_instance, str, REG_EXTENDED | REG_NOSUB)) {
8511 add_startup_exception("regex failed to compile quirk 'instance'"
8512 " field: %s", instance);
8513 failed = true;
8514 }
8515 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
8516 free(str);
8517
8518 if (asprintf(&str, "^%s$", name) == -1)
8519 err(1, "quirk_insert: asprintf");
8520 if (regcomp(&qp->regex_name, str, REG_EXTENDED | REG_NOSUB)) {
8521 add_startup_exception("regex failed to compile quirk 'name' "
8522 "field: %s", name);
8523 failed = true;
8524 }
8525 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
8526 free(str);
8527
8528 if (failed) {
8529 DNPRINTF(SWM_D_QUIRK, "quirk_insert: regex error; skipping.\n");
8530 quirk_free(qp);
8531 } else {
8532 qp->quirk = quirk;
8533 qp->ws = ws;
8534 TAILQ_INSERT_TAIL(&quirks, qp, entry);
8535 }
8536 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
8537 }
8538
8539 void
8540 quirk_remove(struct quirk *qp)
8541 {
8542 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%u]\n", qp->class,
8543 qp->name, qp->quirk);
8544
8545 TAILQ_REMOVE(&quirks, qp, entry);
8546 quirk_free(qp);
8547
8548 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
8549 }
8550
8551 void
8552 quirk_free(struct quirk *qp)
8553 {
8554 regfree(&qp->regex_class);
8555 regfree(&qp->regex_instance);
8556 regfree(&qp->regex_name);
8557 free(qp->class);
8558 free(qp->instance);
8559 free(qp->name);
8560 free(qp);
8561 }
8562
8563 void
8564 clear_quirks(void)
8565 {
8566 struct quirk *qp;
8567
8568 while ((qp = TAILQ_FIRST(&quirks)) != NULL) {
8569 quirk_remove(qp);
8570 }
8571 }
8572
8573 void
8574 quirk_replace(struct quirk *qp, const char *class, const char *instance,
8575 const char *name, uint32_t quirk, int ws)
8576 {
8577 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp->class,
8578 qp->instance, qp->name, qp->quirk, qp->ws);
8579
8580 quirk_remove(qp);
8581 quirk_insert(class, instance, name, quirk, ws);
8582
8583 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
8584 }
8585
8586 void
8587 setquirk(const char *class, const char *instance, const char *name,
8588 uint32_t quirk, int ws)
8589 {
8590 struct quirk *qp;
8591
8592 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
8593 instance, name, quirk, ws);
8594
8595 /* Remove/replace existing quirk. */
8596 TAILQ_FOREACH(qp, &quirks, entry) {
8597 if (strcmp(qp->class, class) == 0 &&
8598 strcmp(qp->instance, instance) == 0 &&
8599 strcmp(qp->name, name) == 0) {
8600 if (quirk == 0 && ws == -1)
8601 quirk_remove(qp);
8602 else
8603 quirk_replace(qp, class, instance, name, quirk,
8604 ws);
8605 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
8606 return;
8607 }
8608 }
8609
8610 /* Only insert if quirk is not NONE or forced ws is set. */
8611 if (quirk || ws != -1)
8612 quirk_insert(class, instance, name, quirk, ws);
8613
8614 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
8615 }
8616
8617 /* Eat '\' in str used to escape square brackets and colon. */
8618 void
8619 unescape_selector(char *str)
8620 {
8621 char *cp;
8622
8623 for (cp = str; *str != '\0'; ++str, ++cp) {
8624 if (*str == '\\' && (*(str + 1) == ':' || *(str + 1) == ']' ||
8625 *(str + 1) == '['))
8626 ++str;
8627
8628 *cp = *str;
8629 }
8630 *cp = '\0';
8631 }
8632
8633 int
8634 setconfquirk(const char *selector, const char *value, int flags)
8635 {
8636 char *str, *cp, *class;
8637 char *instance = NULL, *name = NULL;
8638 int retval, count = 0, ws = -1;
8639 uint32_t qrks;
8640
8641 /* suppress unused warning since var is needed */
8642 (void)flags;
8643
8644 if (selector == NULL || strlen(selector) == 0)
8645 return (0);
8646
8647 if ((str = strdup(selector)) == NULL)
8648 err(1, "setconfquirk: strdup");
8649
8650 /* Find non-escaped colon. */
8651 class = cp = str;
8652 if (*cp == ':') {
8653 *cp = '\0';
8654 ++count;
8655 }
8656
8657 for (++cp; *cp != '\0'; ++cp) {
8658 if (*cp == ':' && *(cp - 1) != '\\') {
8659 *cp = '\0';
8660 ++count;
8661 }
8662 }
8663
8664 unescape_selector(class);
8665 if (count) {
8666 instance = class + strlen(class) + 1;
8667 unescape_selector(instance);
8668 } else {
8669 instance = ".*";
8670 }
8671
8672 if (count > 1) {
8673 name = instance + strlen(instance) + 1;
8674 unescape_selector(name);
8675 } else {
8676 name = ".*";
8677 }
8678
8679 DNPRINTF(SWM_D_CONF, "setconfquirk: class: %s, instance: %s, "
8680 "name: %s\n", class, instance, name);
8681
8682 if ((retval = parsequirks(value, &qrks, &ws)) == 0)
8683 setquirk(class, instance, name, qrks, ws);
8684
8685 free(str);
8686 return (retval);
8687 }
8688
8689 void
8690 setup_quirks(void)
8691 {
8692 setquirk("MPlayer", "xv", ".*",
8693 SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV, -1);
8694 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8695 SWM_Q_FLOAT, -1);
8696 setquirk("Firefox-bin", "firefox-bin", ".*",
8697 SWM_Q_TRANSSZ, -1);
8698 setquirk("Firefox", "Dialog", ".*",
8699 SWM_Q_FLOAT, -1);
8700 setquirk("Gimp", "gimp", ".*",
8701 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
8702 setquirk("XTerm", "xterm", ".*",
8703 SWM_Q_XTERM_FONTADJ, -1);
8704 setquirk("xine", "Xine Window", ".*",
8705 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
8706 setquirk("Xitk", "Xitk Combo", ".*",
8707 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
8708 setquirk("xine", "xine Panel", ".*",
8709 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
8710 setquirk("Xitk", "Xine Window", ".*",
8711 SWM_Q_FLOAT | SWM_Q_ANYWHERE, -1);
8712 setquirk("xine", "xine Video Fullscreen Window", ".*",
8713 SWM_Q_FULLSCREEN | SWM_Q_FLOAT, -1);
8714 setquirk("pcb", "pcb", ".*",
8715 SWM_Q_FLOAT, -1);
8716 setquirk("SDL_App", "SDL_App", ".*",
8717 SWM_Q_FLOAT | SWM_Q_FULLSCREEN, -1);
8718 }
8719
8720 /* conf file stuff */
8721 #define SWM_CONF_FILE "spectrwm.conf"
8722 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8723
8724 enum {
8725 SWM_S_BAR_ACTION,
8726 SWM_S_BAR_AT_BOTTOM,
8727 SWM_S_BAR_BORDER_WIDTH,
8728 SWM_S_BAR_DELAY,
8729 SWM_S_BAR_ENABLED,
8730 SWM_S_BAR_ENABLED_WS,
8731 SWM_S_BAR_FONT,
8732 SWM_S_BAR_FORMAT,
8733 SWM_S_BAR_JUSTIFY,
8734 SWM_S_BORDER_WIDTH,
8735 SWM_S_BOUNDARY_WIDTH,
8736 SWM_S_CLOCK_ENABLED,
8737 SWM_S_CLOCK_FORMAT,
8738 SWM_S_CYCLE_EMPTY,
8739 SWM_S_CYCLE_VISIBLE,
8740 SWM_S_DIALOG_RATIO,
8741 SWM_S_DISABLE_BORDER,
8742 SWM_S_FOCUS_CLOSE,
8743 SWM_S_FOCUS_CLOSE_WRAP,
8744 SWM_S_FOCUS_DEFAULT,
8745 SWM_S_FOCUS_MODE,
8746 SWM_S_ICONIC_ENABLED,
8747 SWM_S_JAVA_WORKAROUND,
8748 SWM_S_MAXIMIZE_HIDE_BAR,
8749 SWM_S_REGION_PADDING,
8750 SWM_S_SPAWN_ORDER,
8751 SWM_S_SPAWN_TERM,
8752 SWM_S_SS_APP,
8753 SWM_S_SS_ENABLED,
8754 SWM_S_STACK_ENABLED,
8755 SWM_S_TERM_WIDTH,
8756 SWM_S_TILE_GAP,
8757 SWM_S_URGENT_COLLAPSE,
8758 SWM_S_URGENT_ENABLED,
8759 SWM_S_VERBOSE_LAYOUT,
8760 SWM_S_WARP_FOCUS,
8761 SWM_S_WARP_POINTER,
8762 SWM_S_WINDOW_CLASS_ENABLED,
8763 SWM_S_WINDOW_INSTANCE_ENABLED,
8764 SWM_S_WINDOW_NAME_ENABLED,
8765 SWM_S_WORKSPACE_CLAMP,
8766 SWM_S_WORKSPACE_LIMIT,
8767 SWM_S_WORKSPACE_NAME,
8768 };
8769
8770 int
8771 setconfvalue(const char *selector, const char *value, int flags)
8772 {
8773 struct workspace *ws;
8774 int i, ws_id, num_screens;
8775 char *b, *str, *sp, s[1024];
8776
8777 switch (flags) {
8778 case SWM_S_BAR_ACTION:
8779 free(bar_argv[0]);
8780 if ((bar_argv[0] = expand_tilde(value)) == NULL)
8781 err(1, "setconfvalue: bar_action");
8782 break;
8783 case SWM_S_BAR_AT_BOTTOM:
8784 bar_at_bottom = (atoi(value) != 0);
8785 break;
8786 case SWM_S_BAR_BORDER_WIDTH:
8787 bar_border_width = atoi(value);
8788 if (bar_border_width < 0)
8789 bar_border_width = 0;
8790 break;
8791 case SWM_S_BAR_DELAY:
8792 /* No longer needed; leave to not break old conf files. */
8793 break;
8794 case SWM_S_BAR_ENABLED:
8795 bar_enabled = (atoi(value) != 0);
8796 break;
8797 case SWM_S_BAR_ENABLED_WS:
8798 ws_id = atoi(selector) - 1;
8799 if (ws_id < 0 || ws_id >= workspace_limit)
8800 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8801 "workspace %d.", ws_id + 1);
8802
8803 num_screens = get_screen_count();
8804 for (i = 0; i < num_screens; i++) {
8805 ws = (struct workspace *)&screens[i].ws;
8806 ws[ws_id].bar_enabled = (atoi(value) != 0);
8807 }
8808 break;
8809 case SWM_S_BAR_FONT:
8810 b = bar_fonts;
8811 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
8812 err(1, "setconfvalue: asprintf: failed to allocate "
8813 "memory for bar_fonts.");
8814 free(b);
8815
8816 /* If already in xft mode, then we are done. */
8817 if (!bar_font_legacy)
8818 break;
8819
8820 if ((sp = str = strdup(value)) == NULL)
8821 err(1, "setconfvalue: strdup");
8822
8823 /* If there are any non-XLFD entries, switch to Xft mode. */
8824 while ((b = strsep(&sp, ",")) != NULL) {
8825 if (*b == '\0')
8826 continue;
8827 if (!isxlfd(b)) {
8828 bar_font_legacy = false;
8829 break;
8830 }
8831 }
8832
8833 free(str);
8834 break;
8835 case SWM_S_BAR_FORMAT:
8836 free(bar_format);
8837 if ((bar_format = strdup(value)) == NULL)
8838 err(1, "setconfvalue: bar_format");
8839 break;
8840 case SWM_S_BAR_JUSTIFY:
8841 if (strcmp(value, "left") == 0)
8842 bar_justify = SWM_BAR_JUSTIFY_LEFT;
8843 else if (strcmp(value, "center") == 0)
8844 bar_justify = SWM_BAR_JUSTIFY_CENTER;
8845 else if (strcmp(value, "right") == 0)
8846 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
8847 else
8848 errx(1, "invalid bar_justify");
8849 break;
8850 case SWM_S_BORDER_WIDTH:
8851 border_width = atoi(value);
8852 if (border_width < 0)
8853 border_width = 0;
8854 break;
8855 case SWM_S_BOUNDARY_WIDTH:
8856 boundary_width = atoi(value);
8857 if (boundary_width < 0)
8858 boundary_width = 0;
8859 break;
8860 case SWM_S_CLOCK_ENABLED:
8861 clock_enabled = (atoi(value) != 0);
8862 break;
8863 case SWM_S_CLOCK_FORMAT:
8864 #ifndef SWM_DENY_CLOCK_FORMAT
8865 free(clock_format);
8866 if ((clock_format = strdup(value)) == NULL)
8867 err(1, "setconfvalue: clock_format");
8868 #endif
8869 break;
8870 case SWM_S_CYCLE_EMPTY:
8871 cycle_empty = (atoi(value) != 0);
8872 break;
8873 case SWM_S_CYCLE_VISIBLE:
8874 cycle_visible = (atoi(value) != 0);
8875 break;
8876 case SWM_S_DIALOG_RATIO:
8877 dialog_ratio = atof(value);
8878 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
8879 dialog_ratio = .6;
8880 break;
8881 case SWM_S_DISABLE_BORDER:
8882 disable_border = (atoi(value) != 0);
8883 break;
8884 case SWM_S_FOCUS_CLOSE:
8885 if (strcmp(value, "first") == 0)
8886 focus_close = SWM_STACK_BOTTOM;
8887 else if (strcmp(value, "last") == 0)
8888 focus_close = SWM_STACK_TOP;
8889 else if (strcmp(value, "next") == 0)
8890 focus_close = SWM_STACK_ABOVE;
8891 else if (strcmp(value, "previous") == 0)
8892 focus_close = SWM_STACK_BELOW;
8893 else
8894 errx(1, "focus_close");
8895 break;
8896 case SWM_S_FOCUS_CLOSE_WRAP:
8897 focus_close_wrap = (atoi(value) != 0);
8898 break;
8899 case SWM_S_FOCUS_DEFAULT:
8900 if (strcmp(value, "last") == 0)
8901 focus_default = SWM_STACK_TOP;
8902 else if (strcmp(value, "first") == 0)
8903 focus_default = SWM_STACK_BOTTOM;
8904 else
8905 errx(1, "focus_default");
8906 break;
8907 case SWM_S_FOCUS_MODE:
8908 if (strcmp(value, "default") == 0)
8909 focus_mode = SWM_FOCUS_DEFAULT;
8910 else if (strcmp(value, "follow") == 0 ||
8911 strcmp(value, "follow_cursor") == 0)
8912 focus_mode = SWM_FOCUS_FOLLOW;
8913 else if (strcmp(value, "manual") == 0)
8914 focus_mode = SWM_FOCUS_MANUAL;
8915 else
8916 errx(1, "focus_mode");
8917 break;
8918 case SWM_S_ICONIC_ENABLED:
8919 iconic_enabled = (atoi(value) != 0);
8920 break;
8921 case SWM_S_JAVA_WORKAROUND:
8922 java_workaround = (atoi(value) != 0);
8923 break;
8924 case SWM_S_MAXIMIZE_HIDE_BAR:
8925 maximize_hide_bar = atoi(value);
8926 break;
8927 case SWM_S_REGION_PADDING:
8928 region_padding = atoi(value);
8929 if (region_padding < 0)
8930 region_padding = 0;
8931 break;
8932 case SWM_S_SPAWN_ORDER:
8933 if (strcmp(value, "first") == 0)
8934 spawn_position = SWM_STACK_BOTTOM;
8935 else if (strcmp(value, "last") == 0)
8936 spawn_position = SWM_STACK_TOP;
8937 else if (strcmp(value, "next") == 0)
8938 spawn_position = SWM_STACK_ABOVE;
8939 else if (strcmp(value, "previous") == 0)
8940 spawn_position = SWM_STACK_BELOW;
8941 else
8942 errx(1, "spawn_position");
8943 break;
8944 case SWM_S_SPAWN_TERM:
8945 setconfspawn("term", value, 0);
8946 setconfspawn("spawn_term", value, 0);
8947 break;
8948 case SWM_S_SS_APP:
8949 /* No longer needed; leave to not break old conf files. */
8950 break;
8951 case SWM_S_SS_ENABLED:
8952 /* No longer needed; leave to not break old conf files. */
8953 break;
8954 case SWM_S_STACK_ENABLED:
8955 stack_enabled = (atoi(value) != 0);
8956 break;
8957 case SWM_S_TERM_WIDTH:
8958 term_width = atoi(value);
8959 if (term_width < 0)
8960 term_width = 0;
8961 break;
8962 case SWM_S_TILE_GAP:
8963 tile_gap = atoi(value);
8964 break;
8965 case SWM_S_URGENT_COLLAPSE:
8966 urgent_collapse = (atoi(value) != 0);
8967 break;
8968 case SWM_S_URGENT_ENABLED:
8969 urgent_enabled = (atoi(value) != 0);
8970 break;
8971 case SWM_S_VERBOSE_LAYOUT:
8972 verbose_layout = (atoi(value) != 0);
8973 for (i = 0; layouts[i].l_stack != NULL; i++) {
8974 if (verbose_layout)
8975 layouts[i].l_string = fancy_stacker;
8976 else
8977 layouts[i].l_string = plain_stacker;
8978 }
8979 break;
8980 case SWM_S_WARP_FOCUS:
8981 warp_focus = (atoi(value) != 0);
8982 break;
8983 case SWM_S_WARP_POINTER:
8984 warp_pointer = (atoi(value) != 0);
8985 break;
8986 case SWM_S_WINDOW_CLASS_ENABLED:
8987 window_class_enabled = (atoi(value) != 0);
8988 break;
8989 case SWM_S_WINDOW_INSTANCE_ENABLED:
8990 window_instance_enabled = (atoi(value) != 0);
8991 break;
8992 case SWM_S_WINDOW_NAME_ENABLED:
8993 window_name_enabled = (atoi(value) != 0);
8994 break;
8995 case SWM_S_WORKSPACE_CLAMP:
8996 workspace_clamp = (atoi(value) != 0);
8997 break;
8998 case SWM_S_WORKSPACE_LIMIT:
8999 workspace_limit = atoi(value);
9000 if (workspace_limit > SWM_WS_MAX)
9001 workspace_limit = SWM_WS_MAX;
9002 else if (workspace_limit < 1)
9003 workspace_limit = 1;
9004
9005 ewmh_update_desktops();
9006 break;
9007 case SWM_S_WORKSPACE_NAME:
9008 if (getenv("SWM_STARTED") != NULL)
9009 return (0);
9010
9011 bzero(s, sizeof s);
9012 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
9013 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
9014 ws_id--;
9015 if (ws_id < 0 || ws_id >= workspace_limit)
9016 errx(1, "setconfvalue: workspace_name: invalid "
9017 "workspace %d.", ws_id + 1);
9018
9019 num_screens = get_screen_count();
9020 for (i = 0; i < num_screens; ++i) {
9021 ws = (struct workspace *)&screens[i].ws;
9022
9023 if (strlen(s) > 0) {
9024 free(ws[ws_id].name);
9025 if ((ws[ws_id].name = strdup(s)) == NULL)
9026 err(1, "setconfvalue: workspace_name.");
9027
9028 ewmh_update_desktop_names();
9029 ewmh_get_desktop_names();
9030 }
9031 }
9032 break;
9033 default:
9034 return (1);
9035 }
9036 return (0);
9037 }
9038
9039 int
9040 setconfmodkey(const char *selector, const char *value, int flags)
9041 {
9042 /* suppress unused warnings since vars are needed */
9043 (void)selector;
9044 (void)flags;
9045
9046 if (strncasecmp(value, "Mod1", strlen("Mod1")) == 0)
9047 update_modkey(XCB_MOD_MASK_1);
9048 else if (strncasecmp(value, "Mod2", strlen("Mod2")) == 0)
9049 update_modkey(XCB_MOD_MASK_2);
9050 else if (strncasecmp(value, "Mod3", strlen("Mod3")) == 0)
9051 update_modkey(XCB_MOD_MASK_3);
9052 else if (strncasecmp(value, "Mod4", strlen("Mod4")) == 0)
9053 update_modkey(XCB_MOD_MASK_4);
9054 else if (strncasecmp(value, "Mod5", strlen("Mod5")) == 0)
9055 update_modkey(XCB_MOD_MASK_5);
9056 else
9057 return (1);
9058 return (0);
9059 }
9060
9061 int
9062 setconfcolor(const char *selector, const char *value, int flags)
9063 {
9064 int first, last, i = 0, num_screens;
9065
9066 num_screens = get_screen_count();
9067
9068 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
9069 if (selector == NULL || strlen(selector) == 0 ||
9070 (last = atoi(selector) - 1) < 0) {
9071 first = 0;
9072 last = num_screens - 1;
9073 } else {
9074 first = last;
9075 }
9076
9077 if (last >= num_screens) {
9078 add_startup_exception("invalid screen index: %d out of bounds "
9079 "(maximum %d)", last + 1, num_screens);
9080 return (1);
9081 }
9082
9083 for (i = first; i <= last; ++i) {
9084 setscreencolor(value, i, flags);
9085
9086 /*
9087 * When setting focus/unfocus colors, we need to also
9088 * set maximize colors to match if they haven't been customized.
9089 */
9090 if (flags == SWM_S_COLOR_FOCUS &&
9091 !screens[i].c[SWM_S_COLOR_FOCUS_MAXIMIZED].manual)
9092 setscreencolor(value, i, SWM_S_COLOR_FOCUS_MAXIMIZED);
9093 else if (flags == SWM_S_COLOR_UNFOCUS &&
9094 !screens[i].c[SWM_S_COLOR_UNFOCUS_MAXIMIZED].manual)
9095 setscreencolor(value, i, SWM_S_COLOR_UNFOCUS_MAXIMIZED);
9096
9097 screens[i].c[flags].manual = 1;
9098 }
9099
9100 return (0);
9101 }
9102
9103 int
9104 setconfregion(const char *selector, const char *value, int flags)
9105 {
9106 /* suppress unused warnings since vars are needed */
9107 (void)selector;
9108 (void)flags;
9109
9110 custom_region(value);
9111 return (0);
9112 }
9113
9114 int
9115 setautorun(const char *selector, const char *value, int flags)
9116 {
9117 int ws_id;
9118 char s[1024];
9119 char *ap, *sp, *str;
9120 union arg a;
9121 int argc = 0;
9122 pid_t pid;
9123 struct pid_e *p;
9124
9125 /* suppress unused warnings since vars are needed */
9126 (void)selector;
9127 (void)flags;
9128
9129 if (getenv("SWM_STARTED"))
9130 return (0);
9131
9132 bzero(s, sizeof s);
9133 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
9134 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
9135 ws_id--;
9136 if (ws_id < 0 || ws_id >= workspace_limit)
9137 errx(1, "autorun: invalid workspace %d", ws_id + 1);
9138
9139 sp = str = expand_tilde((char *)&s);
9140
9141 /*
9142 * This is a little intricate
9143 *
9144 * If the pid already exists we simply reuse it because it means it was
9145 * used before AND not claimed by manage_window. We get away with
9146 * altering it in the parent after INSERT because this can not be a race
9147 */
9148 a.argv = NULL;
9149 while ((ap = strsep(&sp, " \t")) != NULL) {
9150 if (*ap == '\0')
9151 continue;
9152 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
9153 argc++;
9154 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
9155 err(1, "setautorun: realloc");
9156 a.argv[argc - 1] = ap;
9157 }
9158
9159 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
9160 err(1, "setautorun: realloc");
9161 a.argv[argc] = NULL;
9162
9163 if ((pid = fork()) == 0) {
9164 spawn(ws_id, &a, true);
9165 /* NOTREACHED */
9166 _exit(1);
9167 }
9168 free(a.argv);
9169 free(str);
9170
9171 /* parent */
9172 p = find_pid(pid);
9173 if (p == NULL) {
9174 p = calloc(1, sizeof *p);
9175 if (p == NULL)
9176 return (1);
9177 TAILQ_INSERT_TAIL(&pidlist, p, entry);
9178 }
9179
9180 p->pid = pid;
9181 p->ws = ws_id;
9182
9183 return (0);
9184 }
9185
9186 int
9187 setlayout(const char *selector, const char *value, int flags)
9188 {
9189 struct workspace *ws;
9190 int ws_id, i, x, mg, ma, si, ar;
9191 int st = SWM_V_STACK, num_screens;
9192 char s[1024];
9193 bool f = false;
9194
9195 /* suppress unused warnings since vars are needed */
9196 (void)selector;
9197 (void)flags;
9198
9199 if (getenv("SWM_STARTED"))
9200 return (0);
9201
9202 bzero(s, sizeof s);
9203 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
9204 &ws_id, &mg, &ma, &si, &ar, s) != 6)
9205 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9206 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9207 "<type>'");
9208 ws_id--;
9209 if (ws_id < 0 || ws_id >= workspace_limit)
9210 errx(1, "layout: invalid workspace %d", ws_id + 1);
9211
9212 if (strcasecmp(s, "vertical") == 0)
9213 st = SWM_V_STACK;
9214 else if (strcasecmp(s, "vertical_flip") == 0) {
9215 st = SWM_V_STACK;
9216 f = true;
9217 } else if (strcasecmp(s, "horizontal") == 0)
9218 st = SWM_H_STACK;
9219 else if (strcasecmp(s, "horizontal_flip") == 0) {
9220 st = SWM_H_STACK;
9221 f = true;
9222 } else if (strcasecmp(s, "fullscreen") == 0)
9223 st = SWM_MAX_STACK;
9224 else
9225 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9226 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9227 "<type>'");
9228
9229 num_screens = get_screen_count();
9230 for (i = 0; i < num_screens; i++) {
9231 ws = (struct workspace *)&screens[i].ws;
9232 ws[ws_id].cur_layout = &layouts[st];
9233
9234 ws[ws_id].always_raise = (ar != 0);
9235 if (st == SWM_MAX_STACK)
9236 continue;
9237
9238 /* master grow */
9239 for (x = 0; x < abs(mg); x++) {
9240 ws[ws_id].cur_layout->l_config(&ws[ws_id],
9241 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
9242 SWM_ARG_ID_MASTERSHRINK);
9243 }
9244 /* master add */
9245 for (x = 0; x < abs(ma); x++) {
9246 ws[ws_id].cur_layout->l_config(&ws[ws_id],
9247 ma >= 0 ? SWM_ARG_ID_MASTERADD :
9248 SWM_ARG_ID_MASTERDEL);
9249 }
9250 /* stack inc */
9251 for (x = 0; x < abs(si); x++) {
9252 ws[ws_id].cur_layout->l_config(&ws[ws_id],
9253 si >= 0 ? SWM_ARG_ID_STACKINC :
9254 SWM_ARG_ID_STACKDEC);
9255 }
9256 /* Apply flip */
9257 if (f) {
9258 ws[ws_id].cur_layout->l_config(&ws[ws_id],
9259 SWM_ARG_ID_FLIPLAYOUT);
9260 }
9261 }
9262
9263 return (0);
9264 }
9265
9266 /* config options */
9267 struct config_option {
9268 char *optname;
9269 int (*func)(const char*, const char*, int);
9270 int funcflags;
9271 };
9272 struct config_option configopt[] = {
9273 { "autorun", setautorun, 0 },
9274 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
9275 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
9276 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
9277 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
9278 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
9279 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
9280 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
9281 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
9282 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
9283 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
9284 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
9285 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
9286 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
9287 { "bind", setconfbinding, 0 },
9288 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
9289 { "boundary_width", setconfvalue, SWM_S_BOUNDARY_WIDTH },
9290 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
9291 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
9292 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
9293 { "color_focus_maximized", setconfcolor, SWM_S_COLOR_FOCUS_MAXIMIZED },
9294 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
9295 { "color_unfocus_maximized", setconfcolor, SWM_S_COLOR_UNFOCUS_MAXIMIZED },
9296 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
9297 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
9298 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
9299 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
9300 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
9301 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
9302 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
9303 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
9304 { "iconic_enabled", setconfvalue, SWM_S_ICONIC_ENABLED },
9305 { "java_workaround", setconfvalue, SWM_S_JAVA_WORKAROUND },
9306 { "keyboard_mapping", setkeymapping, 0 },
9307 { "layout", setlayout, 0 },
9308 { "maximize_hide_bar", setconfvalue, SWM_S_MAXIMIZE_HIDE_BAR },
9309 { "modkey", setconfmodkey, 0 },
9310 { "program", setconfspawn, 0 },
9311 { "quirk", setconfquirk, 0 },
9312 { "region", setconfregion, 0 },
9313 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
9314 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
9315 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
9316 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
9317 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
9318 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
9319 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
9320 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
9321 { "title_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED }, /* For backwards compat. */
9322 { "title_name_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED }, /* For backwards compat. */
9323 { "urgent_collapse", setconfvalue, SWM_S_URGENT_COLLAPSE },
9324 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
9325 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
9326 { "warp_focus", setconfvalue, SWM_S_WARP_FOCUS },
9327 { "warp_pointer", setconfvalue, SWM_S_WARP_POINTER },
9328 { "window_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED },
9329 { "window_instance_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED },
9330 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
9331 { "workspace_clamp", setconfvalue, SWM_S_WORKSPACE_CLAMP },
9332 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
9333 { "name", setconfvalue, SWM_S_WORKSPACE_NAME },
9334 };
9335
9336 void
9337 _add_startup_exception(const char *fmt, va_list ap)
9338 {
9339 if (vasprintf(&startup_exception, fmt, ap) == -1)
9340 warn("%s: asprintf", __func__);
9341 }
9342
9343 void
9344 add_startup_exception(const char *fmt, ...)
9345 {
9346 va_list ap;
9347
9348 nr_exceptions++;
9349
9350 if (startup_exception)
9351 return;
9352
9353 /* force bar to be enabled due to exception */
9354 bar_enabled = true;
9355
9356 va_start(ap, fmt);
9357 _add_startup_exception(fmt, ap);
9358 va_end(ap);
9359 }
9360
9361 int
9362 conf_load(const char *filename, int keymapping)
9363 {
9364 FILE *config;
9365 char *line = NULL, *cp, *ce, *optsub, *optval = NULL;
9366 size_t linelen, lineno = 0;
9367 int wordlen, i, optidx, count;
9368 struct config_option *opt = NULL;
9369
9370 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
9371
9372 if (filename == NULL) {
9373 warnx("conf_load: no filename");
9374 return (1);
9375 }
9376
9377 DNPRINTF(SWM_D_CONF, "conf_load: open %s\n", filename);
9378
9379 if ((config = fopen(filename, "r")) == NULL) {
9380 warn("conf_load: fopen: %s", filename);
9381 return (1);
9382 }
9383
9384 while (!feof(config)) {
9385 if (line)
9386 free(line);
9387
9388 if ((line = fparseln(config, &linelen, &lineno, NULL,
9389 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT)) == NULL) {
9390 if (ferror(config))
9391 err(1, "%s", filename);
9392 else
9393 continue;
9394 }
9395 cp = line;
9396 cp += strspn(cp, " \t\n"); /* eat whitespace */
9397 if (cp[0] == '\0') {
9398 /* empty line */
9399 continue;
9400 }
9401 /* get config option */
9402 wordlen = strcspn(cp, "=[ \t\n");
9403 if (wordlen == 0) {
9404 add_startup_exception("%s: line %zd: no option found",
9405 filename, lineno);
9406 continue;
9407 }
9408 optidx = -1;
9409 for (i = 0; i < LENGTH(configopt); i++) {
9410 opt = &configopt[i];
9411 if (strncasecmp(cp, opt->optname, wordlen) == 0 &&
9412 (int)strlen(opt->optname) == wordlen) {
9413 optidx = i;
9414 break;
9415 }
9416 }
9417 if (optidx == -1) {
9418 add_startup_exception("%s: line %zd: unknown option "
9419 "%.*s", filename, lineno, wordlen, cp);
9420 continue;
9421 }
9422 if (keymapping && opt && strcmp(opt->optname, "bind")) {
9423 add_startup_exception("%s: line %zd: invalid option "
9424 "%.*s", filename, lineno, wordlen, cp);
9425 continue;
9426 }
9427 cp += wordlen;
9428 cp += strspn(cp, " \t\n"); /* eat whitespace */
9429
9430 /* get [selector] if any */
9431 optsub = NULL;
9432 count = 0;
9433 if (*cp == '[') {
9434 ++count;
9435 /* Get length of selector. */
9436 for (ce = ++cp; *ce != '\0'; ++ce) {
9437 /* Find matching (not escaped) bracket. */
9438 if (*ce == ']' && *(ce - 1) != '\\') {
9439 --count;
9440 break;
9441 }
9442 }
9443
9444 if (count > 0) {
9445 add_startup_exception("%s: line %zd: syntax "
9446 "error: unterminated selector", filename,
9447 lineno);
9448 continue;
9449 }
9450
9451 /* ce points at ']'; terminate optsub. */
9452 *ce = '\0';
9453 optsub = cp;
9454 cp = ce + 1;
9455 }
9456 cp += strspn(cp, "= \t\n"); /* eat trailing */
9457 /* get RHS value */
9458 optval = cp;
9459 if (strlen(optval) == 0) {
9460 add_startup_exception("%s: line %zd: must supply value "
9461 "to %s", filename, lineno,
9462 configopt[optidx].optname);
9463 continue;
9464 }
9465 /* call function to deal with it all */
9466 if (configopt[optidx].func(optsub, optval,
9467 configopt[optidx].funcflags) != 0) {
9468 add_startup_exception("%s: line %zd: invalid data for "
9469 "%s", filename, lineno, configopt[optidx].optname);
9470 continue;
9471 }
9472 }
9473
9474 if (line)
9475 free(line);
9476 fclose(config);
9477
9478 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
9479
9480 return (0);
9481 }
9482
9483 void
9484 set_child_transient(struct ws_win *win, xcb_window_t *trans)
9485 {
9486 struct ws_win *parent, *w;
9487 struct swm_region *r;
9488 struct workspace *ws;
9489 xcb_icccm_wm_hints_t wmh;
9490
9491 parent = find_window(win->transient);
9492 if (parent)
9493 parent->focus_child = win;
9494 else {
9495 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
9496 " for %#x trans %#x\n", win->id, win->transient);
9497
9498 r = root_to_region(win->s->root, SWM_CK_ALL);
9499 ws = r->ws;
9500 /* parent doen't exist in our window list */
9501 TAILQ_FOREACH(w, &ws->winlist, entry) {
9502 if (xcb_icccm_get_wm_hints_reply(conn,
9503 xcb_icccm_get_wm_hints(conn, w->id),
9504 &wmh, NULL) != 1) {
9505 warnx("can't get hints for %#x", w->id);
9506 continue;
9507 }
9508
9509 if (win->hints.window_group != wmh.window_group)
9510 continue;
9511
9512 w->focus_child = win;
9513 win->transient = w->id;
9514 *trans = w->id;
9515 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
9516 "transient to %#x\n", win->transient);
9517 break;
9518 }
9519 }
9520 }
9521
9522 pid_t
9523 window_get_pid(xcb_window_t win)
9524 {
9525 pid_t ret = 0;
9526 const char *errstr;
9527 xcb_atom_t apid;
9528 xcb_get_property_cookie_t pc;
9529 xcb_get_property_reply_t *pr;
9530
9531 apid = get_atom_from_string("_NET_WM_PID");
9532 if (apid == XCB_ATOM_NONE)
9533 goto tryharder;
9534
9535 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
9536 pr = xcb_get_property_reply(conn, pc, NULL);
9537 if (pr == NULL)
9538 goto tryharder;
9539 if (pr->type != XCB_ATOM_CARDINAL) {
9540 free(pr);
9541 goto tryharder;
9542 }
9543
9544 if (pr->type == apid && pr->format == 32)
9545 ret = *((pid_t *)xcb_get_property_value(pr));
9546 free(pr);
9547
9548 return (ret);
9549
9550 tryharder:
9551 apid = get_atom_from_string("_SWM_PID");
9552 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
9553 0, SWM_PROPLEN);
9554 pr = xcb_get_property_reply(conn, pc, NULL);
9555 if (pr == NULL)
9556 return (0);
9557 if (pr->type != apid) {
9558 free(pr);
9559 return (0);
9560 }
9561
9562 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
9563 free(pr);
9564
9565 return (ret);
9566 }
9567
9568 int
9569 get_swm_ws(xcb_window_t id)
9570 {
9571 int ws_idx = -1;
9572 char *prop = NULL;
9573 size_t proplen;
9574 const char *errstr;
9575 xcb_get_property_reply_t *gpr;
9576
9577 gpr = xcb_get_property_reply(conn,
9578 xcb_get_property(conn, 0, id, a_swm_ws,
9579 XCB_ATOM_STRING, 0, SWM_PROPLEN),
9580 NULL);
9581 if (gpr == NULL)
9582 return (-1);
9583 if (gpr->type) {
9584 proplen = xcb_get_property_value_length(gpr);
9585 if (proplen > 0) {
9586 prop = malloc(proplen + 1);
9587 if (prop) {
9588 memcpy(prop,
9589 xcb_get_property_value(gpr),
9590 proplen);
9591 prop[proplen] = '\0';
9592 }
9593 }
9594 }
9595 free(gpr);
9596
9597 if (prop) {
9598 DNPRINTF(SWM_D_PROP, "get_swm_ws: _SWM_WS: %s\n", prop);
9599 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
9600 if (errstr) {
9601 DNPRINTF(SWM_D_PROP, "get_swm_ws: win #%s: %s",
9602 errstr, prop);
9603 }
9604 free(prop);
9605 }
9606
9607 return ws_idx;
9608 }
9609
9610 int
9611 get_ws_idx(struct ws_win *win)
9612 {
9613 xcb_get_property_reply_t *gpr;
9614 int ws_idx = -1;
9615
9616 if (win == NULL)
9617 return -1;
9618
9619 gpr = xcb_get_property_reply(conn,
9620 xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_DESKTOP].atom,
9621 XCB_ATOM_CARDINAL, 0, 1),
9622 NULL);
9623 if (gpr) {
9624 if (gpr->type == XCB_ATOM_CARDINAL && gpr->format == 32)
9625 ws_idx = *((int *)xcb_get_property_value(gpr));
9626 free(gpr);
9627 }
9628
9629 if (ws_idx == -1 && !(win->quirks & SWM_Q_IGNORESPAWNWS))
9630 ws_idx = get_swm_ws(win->id);
9631
9632 if (ws_idx > workspace_limit - 1 || ws_idx < -1)
9633 ws_idx = -1;
9634
9635 DNPRINTF(SWM_D_PROP, "get_ws_idx: win %#x, ws_idx: %d\n", win->id,
9636 ws_idx);
9637
9638 return ws_idx;
9639 }
9640
9641 void
9642 reparent_window(struct ws_win *win)
9643 {
9644 xcb_void_cookie_t c;
9645 xcb_generic_error_t *error;
9646 uint32_t wa[2];
9647
9648 win->frame = xcb_generate_id(conn);
9649
9650 DNPRINTF(SWM_D_MISC, "reparent_window: win %#x, frame: %#x\n",
9651 win->id, win->frame);
9652
9653 wa[0] =
9654 XCB_EVENT_MASK_ENTER_WINDOW |
9655 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
9656 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
9657 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
9658 XCB_EVENT_MASK_EXPOSURE;
9659 #ifdef SWM_DEBUG
9660 wa[0] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
9661 #endif
9662
9663 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win->frame, win->s->root,
9664 X(win), Y(win), WIDTH(win), HEIGHT(win), 0,
9665 XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
9666 XCB_CW_EVENT_MASK, wa);
9667
9668 win->state = SWM_WIN_STATE_REPARENTING;
9669 c = xcb_reparent_window_checked(conn, win->id, win->frame, 0, 0);
9670 if ((error = xcb_request_check(conn, c))) {
9671 DNPRINTF(SWM_D_MISC, "reparent_window: error:\n");
9672 event_error(error);
9673 free(error);
9674
9675 /* Abort. */
9676 xcb_destroy_window(conn, win->frame);
9677 win->frame = XCB_WINDOW_NONE;
9678 } else {
9679 xcb_change_save_set(conn, XCB_SET_MODE_INSERT, win->id);
9680 }
9681 DNPRINTF(SWM_D_MISC, "reparent_window: done.\n");
9682 }
9683
9684 void
9685 unparent_window(struct ws_win *win)
9686 {
9687 xcb_change_save_set(conn, XCB_SET_MODE_DELETE, win->id);
9688 xcb_reparent_window(conn, win->id, win->s->root, X(win), Y(win));
9689 xcb_destroy_window(conn, win->frame);
9690 win->frame = XCB_WINDOW_NONE;
9691 win->state = SWM_WIN_STATE_UNPARENTING;
9692 }
9693
9694 struct ws_win *
9695 manage_window(xcb_window_t id, int spawn_pos, bool mapping)
9696 {
9697 struct ws_win *win = NULL, *ww;
9698 struct swm_region *r;
9699 struct pid_e *p;
9700 struct quirk *qp;
9701 xcb_get_geometry_reply_t *gr;
9702 xcb_get_window_attributes_reply_t *war = NULL;
9703 xcb_window_t trans = XCB_WINDOW_NONE;
9704 uint32_t i, wa[1], new_flags;
9705 int ws_idx, force_ws = -1;
9706 char *class, *instance, *name;
9707
9708 if (find_bar(id)) {
9709 DNPRINTF(SWM_D_MISC, "manage_window: win %#x is region bar; "
9710 "skipping.\n", id);
9711 goto out;
9712 }
9713
9714 if (find_region(id)) {
9715 DNPRINTF(SWM_D_MISC, "manage_window: win %#x is region window; "
9716 "skipping.\n", id);
9717 goto out;
9718 }
9719
9720 if ((win = find_window(id)) != NULL) {
9721 DNPRINTF(SWM_D_MISC, "manage_window: win %#x already "
9722 "managed; skipping.)\n", id);
9723 return (win); /* Already managed. */
9724 }
9725
9726 /* See if window is on the unmanaged list. */
9727 if ((win = find_unmanaged_window(id)) != NULL) {
9728 DNPRINTF(SWM_D_MISC, "manage_window: win %#x found on "
9729 "unmanaged list.\n", id);
9730 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
9731
9732 if (TRANS(win))
9733 set_child_transient(win, &trans);
9734
9735 goto remanage;
9736 } else {
9737 DNPRINTF(SWM_D_MISC, "manage_window: win %#x is new.\n", id);
9738 }
9739
9740 war = xcb_get_window_attributes_reply(conn,
9741 xcb_get_window_attributes(conn, id), NULL);
9742 if (war == NULL) {
9743 DNPRINTF(SWM_D_EVENT, "manage_window: window lost.\n");
9744 goto out;
9745 }
9746
9747 if (war->override_redirect) {
9748 DNPRINTF(SWM_D_EVENT, "manage_window: override_redirect; "
9749 "skipping.\n");
9750 goto out;
9751 }
9752
9753 if (!mapping && war->map_state == XCB_MAP_STATE_UNMAPPED &&
9754 get_win_state(id) == XCB_ICCCM_WM_STATE_WITHDRAWN) {
9755 DNPRINTF(SWM_D_EVENT, "manage_window: window withdrawn; "
9756 "skipping.\n");
9757 goto out;
9758 }
9759
9760 /* Try to get initial window geometry. */
9761 gr = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, id), NULL);
9762 if (gr == NULL) {
9763 DNPRINTF(SWM_D_MISC, "manage_window: get geometry failed.\n");
9764 goto out;
9765 }
9766
9767 /* Create and initialize ws_win object. */
9768 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
9769 err(1, "manage_window: calloc: failed to allocate memory for "
9770 "new window");
9771
9772 win->id = id;
9773
9774 /* Figureout which region the window belongs to. */
9775 r = root_to_region(gr->root, SWM_CK_ALL);
9776
9777 /* Ignore window border if there is one. */
9778 WIDTH(win) = gr->width;
9779 HEIGHT(win) = gr->height;
9780 X(win) = gr->x + gr->border_width;
9781 Y(win) = gr->y + gr->border_width;
9782 win->bordered = false;
9783 win->mapped = (war->map_state != XCB_MAP_STATE_UNMAPPED);
9784 win->s = r->s; /* this never changes */
9785
9786 free(gr);
9787
9788 /* Select which X events to monitor and set border pixel color. */
9789 wa[0] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
9790 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
9791
9792 xcb_change_window_attributes(conn, win->id, XCB_CW_EVENT_MASK, wa);
9793
9794 /* Get WM_SIZE_HINTS. */
9795 xcb_icccm_get_wm_normal_hints_reply(conn,
9796 xcb_icccm_get_wm_normal_hints(conn, win->id),
9797 &win->sh, NULL);
9798
9799 /* Get WM_HINTS. */
9800 xcb_icccm_get_wm_hints_reply(conn,
9801 xcb_icccm_get_wm_hints(conn, win->id),
9802 &win->hints, NULL);
9803
9804 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
9805 xcb_icccm_get_wm_transient_for_reply(conn,
9806 xcb_icccm_get_wm_transient_for(conn, win->id),
9807 &trans, NULL);
9808 if (trans) {
9809 win->transient = trans;
9810 set_child_transient(win, &win->transient);
9811 }
9812
9813 /* Get WM_PROTOCOLS. */
9814 get_wm_protocols(win);
9815
9816 #ifdef SWM_DEBUG
9817 /* Must be after getting WM_HINTS and WM_PROTOCOLS. */
9818 DNPRINTF(SWM_D_FOCUS, "manage_window: input_model: %s\n",
9819 get_win_input_model(win));
9820 #endif
9821
9822 /* Set initial quirks based on EWMH. */
9823 ewmh_autoquirk(win);
9824
9825 /* Determine initial quirks. */
9826 xcb_icccm_get_wm_class_reply(conn,
9827 xcb_icccm_get_wm_class(conn, win->id),
9828 &win->ch, NULL);
9829
9830 class = win->ch.class_name ? win->ch.class_name : "";
9831 instance = win->ch.instance_name ? win->ch.instance_name : "";
9832 name = get_win_name(win->id);
9833
9834 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, instance: %s, "
9835 "name: %s\n", class, instance, name);
9836
9837 /* java is retarded so treat it special */
9838 if (strstr(instance, "sun-awt")) {
9839 DNPRINTF(SWM_D_CLASS, "manage_window: java window detected.\n");
9840 win->java = true;
9841 }
9842
9843 TAILQ_FOREACH(qp, &quirks, entry) {
9844 if (regexec(&qp->regex_class, class, 0, NULL, 0) == 0 &&
9845 regexec(&qp->regex_instance, instance, 0, NULL, 0) == 0 &&
9846 regexec(&qp->regex_name, name, 0, NULL, 0) == 0) {
9847 DNPRINTF(SWM_D_CLASS, "manage_window: matched "
9848 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp->class,
9849 qp->instance, qp->name, qp->quirk, qp->ws);
9850 win->quirks = qp->quirk;
9851 if (qp->ws >= 0 && qp->ws < workspace_limit)
9852 force_ws = qp->ws;
9853 }
9854 }
9855
9856 free(name);
9857
9858 /* Reset font sizes (the bruteforce way; no default keybinding). */
9859 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
9860 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
9861 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
9862 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
9863 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
9864 }
9865
9866 /* Figure out which workspace the window belongs to. */
9867 if (!(win->quirks & SWM_Q_IGNOREPID) &&
9868 (p = find_pid(window_get_pid(win->id))) != NULL) {
9869 win->ws = &r->s->ws[p->ws];
9870 TAILQ_REMOVE(&pidlist, p, entry);
9871 free(p);
9872 p = NULL;
9873 } else if ((ws_idx = get_ws_idx(win)) != -1 &&
9874 !TRANS(win)) {
9875 /* _SWM_WS is set; use that. */
9876 win->ws = &r->s->ws[ws_idx];
9877 } else if (trans && (ww = find_window(trans)) != NULL) {
9878 /* Launch transients in the same ws as parent. */
9879 win->ws = ww->ws;
9880 } else {
9881 win->ws = r->ws;
9882 }
9883
9884 if (force_ws != -1)
9885 win->ws = &r->s->ws[force_ws];
9886
9887 /* Set the _NET_WM_DESKTOP atom. */
9888 DNPRINTF(SWM_D_PROP, "manage_window: set _NET_WM_DESKTOP: %d\n",
9889 win->ws->idx);
9890 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
9891 ewmh[_NET_WM_DESKTOP].atom, XCB_ATOM_CARDINAL, 32, 1, &win->ws->idx);
9892
9893 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
9894 xcb_delete_property(conn, win->id, a_swm_ws);
9895
9896 /* WS must already be set for this to work. */
9897 store_float_geom(win);
9898
9899 /* Make sure window is positioned inside its region, if its active. */
9900 if (win->ws->r) {
9901 region_containment(win, r, SWM_CW_ALLSIDES |
9902 SWM_CW_HARDBOUNDARY);
9903 update_window(win);
9904 }
9905
9906 remanage:
9907 /* Figure out where to insert the window in the workspace list. */
9908 if (trans && (ww = find_window(trans)))
9909 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
9910 else if (win->ws->focus && spawn_pos == SWM_STACK_ABOVE)
9911 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
9912 entry);
9913 else if (win->ws->focus && spawn_pos == SWM_STACK_BELOW)
9914 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
9915 else switch (spawn_pos) {
9916 default:
9917 case SWM_STACK_TOP:
9918 case SWM_STACK_ABOVE:
9919 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
9920 break;
9921 case SWM_STACK_BOTTOM:
9922 case SWM_STACK_BELOW:
9923 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
9924 }
9925
9926 ewmh_update_client_list();
9927
9928 TAILQ_INSERT_HEAD(&win->ws->stack, win, stack_entry);
9929 lower_window(win);
9930
9931 /* Get/apply initial _NET_WM_STATE */
9932 ewmh_get_wm_state(win);
9933
9934 /* Apply quirks. */
9935 new_flags = win->ewmh_flags;
9936
9937 if (win->quirks & SWM_Q_FLOAT)
9938 new_flags |= EWMH_F_ABOVE;
9939
9940 if (win->quirks & SWM_Q_ANYWHERE)
9941 new_flags |= SWM_F_MANUAL;
9942
9943 ewmh_apply_flags(win, new_flags);
9944 ewmh_update_wm_state(win);
9945
9946 /* Set initial _NET_WM_ALLOWED_ACTIONS */
9947 ewmh_update_actions(win);
9948
9949 reparent_window(win);
9950
9951 DNPRINTF(SWM_D_MISC, "manage_window: done. win %#x, (x,y) w x h: "
9952 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win->id,
9953 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
9954 YESNO(ICONIC(win)), win->transient);
9955 out:
9956 free(war);
9957 return (win);
9958 }
9959
9960 void
9961 free_window(struct ws_win *win)
9962 {
9963 DNPRINTF(SWM_D_MISC, "free_window: win %#x\n", WINID(win));
9964
9965 if (win == NULL)
9966 return;
9967
9968 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
9969
9970 /* paint memory */
9971 memset(win, 0xff, sizeof *win); /* XXX kill later */
9972
9973 free(win);
9974 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
9975 }
9976
9977 void
9978 unmanage_window(struct ws_win *win)
9979 {
9980 DNPRINTF(SWM_D_MISC, "unmanage_window: win %#x\n", WINID(win));
9981
9982 if (win == NULL)
9983 return;
9984
9985 kill_refs(win);
9986 unparent_window(win);
9987
9988 TAILQ_REMOVE(&win->ws->stack, win, stack_entry);
9989 TAILQ_REMOVE(&win->ws->winlist, win, entry);
9990 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
9991
9992 ewmh_update_client_list();
9993 }
9994
9995 void
9996 expose(xcb_expose_event_t *e)
9997 {
9998 struct ws_win *w;
9999 struct swm_bar *b;
10000 #ifdef SWM_DEBUG
10001 struct workspace *ws;
10002 #endif
10003
10004 DNPRINTF(SWM_D_EVENT, "expose: win %#x, count: %d\n", e->window,
10005 e->count);
10006
10007 if (e->count > 0)
10008 return;
10009
10010 if ((b = find_bar(e->window))) {
10011 bar_draw(b);
10012 xcb_flush(conn);
10013 } else if ((w = find_window(e->window)) && w->frame == e->window) {
10014 draw_frame(w);
10015 #ifdef SWM_DEBUG
10016 ws = w->ws;
10017 TAILQ_FOREACH(w, &ws->winlist, entry)
10018 debug_refresh(w);
10019 #endif
10020 xcb_flush(conn);
10021 }
10022
10023 DNPRINTF(SWM_D_EVENT, "expose: done\n");
10024 }
10025
10026 void
10027 focusin(xcb_focus_in_event_t *e)
10028 {
10029 struct ws_win *win;
10030
10031 DNPRINTF(SWM_D_EVENT, "focusin: win %#x, mode: %s(%u), "
10032 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
10033 e->mode, get_notify_detail_label(e->detail), e->detail);
10034 if ((win = find_window(e->event)) && win != win->ws->focus &&
10035 win != win->ws->focus_pending &&
10036 e->mode == XCB_NOTIFY_MODE_NORMAL) {
10037 win->ws->focus_prev = win->ws->focus;
10038 win->ws->focus = win;
10039 win->ws->focus_pending = NULL;
10040
10041 if (win->ws->focus_prev)
10042 draw_frame(win->ws->focus_prev);
10043 draw_frame(win);
10044 raise_window(win);
10045 }
10046 }
10047
10048 #ifdef SWM_DEBUG
10049 void
10050 focusout(xcb_focus_out_event_t *e)
10051 {
10052 DNPRINTF(SWM_D_EVENT, "focusout: win %#x, mode: %s(%u), "
10053 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
10054 e->mode, get_notify_detail_label(e->detail), e->detail);
10055 }
10056 #endif
10057
10058 void
10059 keypress(xcb_key_press_event_t *e)
10060 {
10061 struct action *ap;
10062 struct binding *bp;
10063 xcb_keysym_t keysym;
10064 bool replay = true;
10065
10066 last_event_time = e->time;
10067
10068 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
10069
10070 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
10071 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10072 "state: %u, cleaned: %u, same_screen: %s\n", keysym, e->event,
10073 e->event_x, e->event_y, e->detail, e->time, e->root, e->root_x,
10074 e->root_y, e->child, e->state, CLEANMASK(e->state),
10075 YESNO(e->same_screen));
10076
10077 bp = binding_lookup(CLEANMASK(e->state), KEYBIND, keysym);
10078 if (bp == NULL) {
10079 /* Look for catch-all. */
10080 if ((bp = binding_lookup(ANYMOD, KEYBIND, keysym)) == NULL)
10081 goto out;
10082 }
10083
10084 replay = bp->flags & BINDING_F_REPLAY;
10085
10086 if ((ap = &actions[bp->action]) == NULL)
10087 goto out;
10088
10089 if (bp->action == FN_SPAWN_CUSTOM)
10090 spawn_custom(root_to_region(e->root, SWM_CK_ALL), &ap->args,
10091 bp->spawn_name);
10092 else if (ap->func)
10093 ap->func(bp, root_to_region(e->root, SWM_CK_ALL), &ap->args);
10094
10095 replay = replay && !(ap->flags & FN_F_NOREPLAY);
10096
10097 out:
10098 if (replay) {
10099 DNPRINTF(SWM_D_EVENT, "keypress: replaying.\n");
10100 /* Pass keypress to event window and unfreeze keyboard queue. */
10101 xcb_allow_events(conn, XCB_ALLOW_REPLAY_KEYBOARD, e->time);
10102 } else {
10103 /* Release freeze. */
10104 xcb_allow_events(conn, XCB_ALLOW_SYNC_KEYBOARD, e->time);
10105 }
10106 xcb_flush(conn);
10107
10108 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
10109 }
10110
10111 void
10112 keyrelease(xcb_key_release_event_t *e)
10113 {
10114 struct action *ap;
10115 struct binding *bp;
10116 xcb_keysym_t keysym;
10117
10118 last_event_time = e->time;
10119
10120 keysym = xcb_key_release_lookup_keysym(syms, e, 0);
10121
10122 DNPRINTF(SWM_D_EVENT, "keyrelease: keysym: %u, win (x,y): %#x (%d,%d), "
10123 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10124 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
10125 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
10126 e->child, e->state, YESNO(e->same_screen));
10127
10128 bp = binding_lookup(CLEANMASK(e->state), KEYBIND, keysym);
10129 if (bp == NULL)
10130 /* Look for catch-all. */
10131 bp = binding_lookup(ANYMOD, KEYBIND, keysym);
10132
10133 if (bp && (ap = &actions[bp->action]) && !(ap->flags & FN_F_NOREPLAY) &&
10134 bp->flags & BINDING_F_REPLAY) {
10135 xcb_allow_events(conn, XCB_ALLOW_REPLAY_KEYBOARD, e->time);
10136 DNPRINTF(SWM_D_EVENT, "keyrelease: replaying.\n");
10137 } else {
10138 xcb_allow_events(conn, XCB_ALLOW_SYNC_KEYBOARD, e->time);
10139 DNPRINTF(SWM_D_EVENT, "keyrelease: unfreezing.\n");
10140 }
10141
10142 xcb_flush(conn);
10143
10144 DNPRINTF(SWM_D_EVENT, "keyrelease: done.\n");
10145 }
10146
10147 void
10148 buttonpress(xcb_button_press_event_t *e)
10149 {
10150 struct ws_win *win = NULL, *newf;
10151 struct swm_region *r, *old_r;
10152 struct action *ap;
10153 struct binding *bp;
10154 bool replay = true;
10155
10156 last_event_time = e->time;
10157
10158 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): %#x (%d,%d), "
10159 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10160 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
10161 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
10162 e->state, YESNO(e->same_screen));
10163
10164 if (e->event == e->root) {
10165 if (e->child) {
10166 win = find_window(e->child);
10167 } else {
10168 /* Focus on empty region */
10169 /* If no windows on region if its empty. */
10170 r = root_to_region(e->root, SWM_CK_POINTER);
10171 if (r && TAILQ_EMPTY(&r->ws->winlist)) {
10172 old_r = root_to_region(e->root, SWM_CK_FOCUS);
10173 if (old_r && old_r != r)
10174 unfocus_win(old_r->ws->focus);
10175
10176 DNPRINTF(SWM_D_FOCUS, "buttonpress: "
10177 "set_input_focus: %#x, revert-to: parent, "
10178 "time: %#x\n", e->root, e->time);
10179 xcb_set_input_focus(conn,
10180 XCB_INPUT_FOCUS_POINTER_ROOT,
10181 e->root, e->time);
10182
10183 /* Clear bar since empty. */
10184 bar_draw(r->bar);
10185
10186 /* No need to replay event. */
10187 replay = false;
10188 }
10189 }
10190 } else {
10191 win = find_window(e->event);
10192 }
10193
10194 if (win) {
10195 newf = get_focus_magic(win);
10196 if (win->ws->focus == newf && newf != win) {
10197 if (win->focus_child == win)
10198 win->focus_child = NULL;
10199 newf = win;
10200 }
10201 focus_win(newf);
10202 }
10203
10204 /* Handle any bound action. */
10205 bp = binding_lookup(CLEANMASK(e->state), BTNBIND, e->detail);
10206 if (bp == NULL) {
10207 /* Look for catch-all. */
10208 if ((bp = binding_lookup(ANYMOD, BTNBIND, e->detail)) == NULL)
10209 goto out;
10210
10211 }
10212
10213 replay = bp->flags & BINDING_F_REPLAY;
10214
10215 if ((ap = &actions[bp->action]) == NULL)
10216 goto out;
10217
10218 if (bp->action == FN_SPAWN_CUSTOM)
10219 spawn_custom(root_to_region(e->root, SWM_CK_ALL), &ap->args,
10220 bp->spawn_name);
10221 else if (ap->func)
10222 ap->func(bp, root_to_region(e->root, SWM_CK_ALL), &ap->args);
10223
10224 replay = replay && !(ap->flags & FN_F_NOREPLAY);
10225
10226 out:
10227 if (replay) {
10228 DNPRINTF(SWM_D_EVENT, "buttonpress: replaying.\n");
10229 /* Replay event to event window */
10230 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
10231 } else {
10232 /* Unfreeze grab events. */
10233 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
10234 }
10235
10236 focus_flush();
10237 }
10238
10239 void
10240 buttonrelease(xcb_button_release_event_t *e)
10241 {
10242 struct action *ap;
10243 struct binding *bp;
10244
10245 last_event_time = e->time;
10246
10247 DNPRINTF(SWM_D_EVENT, "buttonrelease: win (x,y): %#x (%d,%d), "
10248 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10249 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
10250 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
10251 e->state, YESNO(e->same_screen));
10252
10253 bp = binding_lookup(CLEANMASK(e->state), BTNBIND, e->detail);
10254 if (bp == NULL)
10255 /* Look for catch-all. */
10256 bp = binding_lookup(ANYMOD, BTNBIND, e->detail);
10257
10258 if (bp && (ap = &actions[bp->action]) && !(ap->flags & FN_F_NOREPLAY) &&
10259 bp->flags & BINDING_F_REPLAY) {
10260 DNPRINTF(SWM_D_EVENT, "buttonrelease: replaying.\n");
10261 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
10262 } else {
10263 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
10264 }
10265
10266 xcb_flush(conn);
10267 }
10268
10269 #ifdef SWM_DEBUG
10270 char *
10271 get_win_input_model(struct ws_win *win)
10272 {
10273 char *inputmodel;
10274 /*
10275 * Input Model Input Field WM_TAKE_FOCUS
10276 * No Input False Absent
10277 * Passive True Absent
10278 * Locally Active True Present
10279 * Globally Active False Present
10280 */
10281
10282 if (ACCEPTS_FOCUS(win))
10283 inputmodel = (win->take_focus) ? "Locally Active" : "Passive";
10284 else
10285 inputmodel = (win->take_focus) ? "Globally Active" : "No Input";
10286
10287 return inputmodel;
10288 }
10289
10290 void
10291 print_win_geom(xcb_window_t w)
10292 {
10293 xcb_get_geometry_reply_t *wa;
10294
10295 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
10296 if (wa == NULL) {
10297 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: %#x\n",
10298 w);
10299 return;
10300 }
10301
10302 DNPRINTF(SWM_D_MISC, "print_win_geom: win %#x, root: %#x, "
10303 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
10304 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
10305 wa->border_width);
10306
10307 free(wa);
10308 }
10309 #endif
10310
10311 #ifdef SWM_DEBUG
10312 char *
10313 get_stack_mode_name(uint8_t mode)
10314 {
10315 char *name;
10316
10317 switch(mode) {
10318 case XCB_STACK_MODE_ABOVE:
10319 name = "Above";
10320 break;
10321 case XCB_STACK_MODE_BELOW:
10322 name = "Below";
10323 break;
10324 case XCB_STACK_MODE_TOP_IF:
10325 name = "TopIf";
10326 break;
10327 case XCB_STACK_MODE_BOTTOM_IF:
10328 name = "BottomIf";
10329 break;
10330 case XCB_STACK_MODE_OPPOSITE:
10331 name = "Opposite";
10332 break;
10333 default:
10334 name = "Unknown";
10335 }
10336
10337 return name;
10338 }
10339 #endif
10340
10341 void
10342 configurerequest(xcb_configure_request_event_t *e)
10343 {
10344 struct ws_win *win;
10345 struct swm_region *r = NULL;
10346 int i = 0;
10347 uint32_t wc[7] = {0};
10348 uint16_t mask = 0;
10349 bool new = false;
10350
10351 if ((win = find_window(e->window)) == NULL)
10352 if ((win = find_unmanaged_window(e->window)) == NULL)
10353 new = true;
10354
10355 #ifdef SWM_DEBUG
10356 if (swm_debug & SWM_D_EVENT) {
10357 print_win_geom(e->window);
10358
10359 DNPRINTF(SWM_D_EVENT, "configurerequest: win %#x, "
10360 "parent: %#x, new: %s, value_mask: %u { ", e->window,
10361 e->parent, YESNO(new), e->value_mask);
10362 if (e->value_mask & XCB_CONFIG_WINDOW_X)
10363 DPRINTF("X: %d ", e->x);
10364 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
10365 DPRINTF("Y: %d ", e->y);
10366 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
10367 DPRINTF("W: %u ", e->width);
10368 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
10369 DPRINTF("H: %u ", e->height);
10370 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
10371 DPRINTF("Border: %u ", e->border_width);
10372 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
10373 DPRINTF("Sibling: %#x ", e->sibling);
10374 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
10375 DPRINTF("StackMode: %s(%u) ",
10376 get_stack_mode_name(e->stack_mode), e->stack_mode);
10377 DPRINTF("}\n");
10378 }
10379 #endif
10380
10381 if (new) {
10382 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
10383 mask |= XCB_CONFIG_WINDOW_X;
10384 wc[i++] = e->x;
10385 }
10386 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
10387 mask |= XCB_CONFIG_WINDOW_Y;
10388 wc[i++] = e->y;
10389 }
10390 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
10391 mask |= XCB_CONFIG_WINDOW_WIDTH;
10392 wc[i++] = e->width;
10393 }
10394 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
10395 mask |= XCB_CONFIG_WINDOW_HEIGHT;
10396 wc[i++] = e->height;
10397 }
10398 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
10399 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
10400 wc[i++] = e->border_width;
10401 }
10402 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
10403 mask |= XCB_CONFIG_WINDOW_SIBLING;
10404 wc[i++] = e->sibling;
10405 }
10406 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
10407 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
10408 wc[i++] = e->stack_mode;
10409 }
10410
10411 if (mask != 0) {
10412 xcb_configure_window(conn, e->window, mask, wc);
10413 xcb_flush(conn);
10414 }
10415 } else if ((!MANUAL(win) || win->quirks & SWM_Q_ANYWHERE) &&
10416 !FULLSCREEN(win) && !MAXIMIZED(win)) {
10417 if (win->ws->r)
10418 r = win->ws->r;
10419 else if (win->ws->old_r)
10420 r = win->ws->old_r;
10421
10422 /* windows are centered unless ANYWHERE quirk is set. */
10423 if (win->quirks & SWM_Q_ANYWHERE) {
10424 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
10425 win->g_float.x = e->x;
10426 if (r)
10427 win->g_float.x -= X(r);
10428 }
10429
10430 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
10431 win->g_float.y = e->y;
10432 if (r)
10433 win->g_float.y -= Y(r);
10434 }
10435 }
10436
10437 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
10438 win->g_float.w = e->width;
10439
10440 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
10441 win->g_float.h = e->height;
10442
10443 win->g_floatvalid = true;
10444
10445 if (!MAXIMIZED(win) && !FULLSCREEN(win) &&
10446 (TRANS(win) || (ABOVE(win) &&
10447 win->ws->cur_layout != &layouts[SWM_MAX_STACK]))) {
10448 WIDTH(win) = win->g_float.w;
10449 HEIGHT(win) = win->g_float.h;
10450
10451 if (r != NULL) {
10452 update_floater(win);
10453 focus_flush();
10454 } else {
10455 config_win(win, e);
10456 xcb_flush(conn);
10457 }
10458 } else {
10459 config_win(win, e);
10460 xcb_flush(conn);
10461 }
10462 } else {
10463 config_win(win, e);
10464 xcb_flush(conn);
10465 }
10466
10467 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
10468 }
10469
10470 void
10471 configurenotify(xcb_configure_notify_event_t *e)
10472 {
10473 struct ws_win *win;
10474
10475 DNPRINTF(SWM_D_EVENT, "configurenotify: win %#x, event win: %#x, "
10476 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
10477 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
10478 e->width, e->height, e->border_width, e->above_sibling,
10479 YESNO(e->override_redirect));
10480
10481 win = find_window(e->window);
10482 if (win) {
10483 adjust_font(win);
10484 if (font_adjusted && win->ws->r) {
10485 stack(win->ws->r);
10486 xcb_flush(conn);
10487 }
10488 }
10489 }
10490
10491 void
10492 destroynotify(xcb_destroy_notify_event_t *e)
10493 {
10494 struct ws_win *win;
10495 struct workspace *ws;
10496
10497 DNPRINTF(SWM_D_EVENT, "destroynotify: win %#x\n", e->window);
10498
10499 if ((win = find_window(e->window)) == NULL) {
10500 if ((win = find_unmanaged_window(e->window)) == NULL)
10501 goto out;
10502 /* Window is on unmanaged list. */
10503 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
10504 free_window(win);
10505 goto out;
10506 }
10507
10508 ws = win->ws;
10509
10510 if (win->frame == e->window) {
10511 DNPRINTF(SWM_D_EVENT, "destroynotify: frame for win %#x\n",
10512 win->id);
10513 win->frame = XCB_WINDOW_NONE;
10514 goto out;
10515 }
10516
10517 if (focus_mode != SWM_FOCUS_FOLLOW) {
10518 /* If we were focused, make sure we focus on something else. */
10519 if (win == ws->focus) {
10520 ws->focus_pending = get_focus_prev(win);
10521 if (ws->focus_pending == win)
10522 ws->focus_pending = NULL;
10523 }
10524 }
10525
10526 unmanage_window(win);
10527 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
10528 free_window(win);
10529 stack(ws->r);
10530
10531 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(ws)) {
10532 if (ws->focus_pending) {
10533 focus_win(ws->focus_pending);
10534 ws->focus_pending = NULL;
10535 } else if (ws->focus == NULL) {
10536 DNPRINTF(SWM_D_FOCUS, "destroynotify: set_input_focus: "
10537 "%#x, revert-to: parent, time: 0\n", ws->r->id);
10538 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
10539 ws->r->id, XCB_CURRENT_TIME);
10540 }
10541
10542 focus_flush();
10543 }
10544
10545 out:
10546 DNPRINTF(SWM_D_EVENT, "destroynotify: done.\n");
10547 }
10548
10549 #ifdef SWM_DEBUG
10550 char *
10551 get_notify_detail_label(uint8_t detail)
10552 {
10553 char *label;
10554
10555 switch (detail) {
10556 case XCB_NOTIFY_DETAIL_ANCESTOR:
10557 label = "Ancestor";
10558 break;
10559 case XCB_NOTIFY_DETAIL_VIRTUAL:
10560 label = "Virtual";
10561 break;
10562 case XCB_NOTIFY_DETAIL_INFERIOR:
10563 label = "Inferior";
10564 break;
10565 case XCB_NOTIFY_DETAIL_NONLINEAR:
10566 label = "Nonlinear";
10567 break;
10568 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
10569 label = "NonlinearVirtual";
10570 break;
10571 case XCB_NOTIFY_DETAIL_POINTER:
10572 label = "Pointer";
10573 break;
10574 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
10575 label = "PointerRoot";
10576 break;
10577 case XCB_NOTIFY_DETAIL_NONE:
10578 label = "None";
10579 break;
10580 default:
10581 label = "Unknown";
10582 }
10583
10584 return label;
10585 }
10586
10587 char *
10588 get_notify_mode_label(uint8_t mode)
10589 {
10590 char *label;
10591
10592 switch (mode) {
10593 case XCB_NOTIFY_MODE_NORMAL:
10594 label = "Normal";
10595 break;
10596 case XCB_NOTIFY_MODE_GRAB:
10597 label = "Grab";
10598 break;
10599 case XCB_NOTIFY_MODE_UNGRAB:
10600 label = "Ungrab";
10601 break;
10602 case XCB_NOTIFY_MODE_WHILE_GRABBED:
10603 label = "WhileGrabbed";
10604 break;
10605 default:
10606 label = "Unknown";
10607 }
10608
10609 return label;
10610 }
10611
10612 char *
10613 get_state_mask_label(uint16_t state)
10614 {
10615 char *label;
10616
10617 switch (state) {
10618 case XCB_KEY_BUT_MASK_SHIFT:
10619 label = "ShiftMask";
10620 break;
10621 case XCB_KEY_BUT_MASK_LOCK:
10622 label = "LockMask";
10623 break;
10624 case XCB_KEY_BUT_MASK_CONTROL:
10625 label = "ControlMask";
10626 break;
10627 case XCB_KEY_BUT_MASK_MOD_1:
10628 label = "Mod1Mask";
10629 break;
10630 case XCB_KEY_BUT_MASK_MOD_2:
10631 label = "Mod2Mask";
10632 break;
10633 case XCB_KEY_BUT_MASK_MOD_3:
10634 label = "Mod3Mask";
10635 break;
10636 case XCB_KEY_BUT_MASK_MOD_4:
10637 label = "Mod4Mask";
10638 break;
10639 case XCB_KEY_BUT_MASK_MOD_5:
10640 label = "Mod5Mask";
10641 break;
10642 case XCB_KEY_BUT_MASK_BUTTON_1:
10643 label = "Button1Mask";
10644 break;
10645 case XCB_KEY_BUT_MASK_BUTTON_2:
10646 label = "Button2Mask";
10647 break;
10648 case XCB_KEY_BUT_MASK_BUTTON_3:
10649 label = "Button3Mask";
10650 break;
10651 case XCB_KEY_BUT_MASK_BUTTON_4:
10652 label = "Button4Mask";
10653 break;
10654 case XCB_KEY_BUT_MASK_BUTTON_5:
10655 label = "Button5Mask";
10656 break;
10657 case 0:
10658 label = "None";
10659 break;
10660 default:
10661 label = "Unknown";
10662 }
10663
10664 return label;
10665 }
10666 #endif
10667
10668 void
10669 enternotify(xcb_enter_notify_event_t *e)
10670 {
10671 struct ws_win *win;
10672 struct swm_region *r;
10673
10674 last_event_time = e->time;
10675
10676 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): %#x "
10677 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10678 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10679 e->time, e->event, e->event_x, e->event_y,
10680 get_notify_mode_label(e->mode), e->mode,
10681 get_notify_detail_label(e->detail), e->detail,
10682 e->root, e->root_x, e->root_y, e->child,
10683 YESNO(e->same_screen_focus), get_state_mask_label(e->state),
10684 e->state);
10685
10686 if (e->event == e->root && e->child == XCB_WINDOW_NONE &&
10687 e->mode == XCB_NOTIFY_MODE_GRAB &&
10688 e->detail == XCB_NOTIFY_DETAIL_INFERIOR) {
10689 DNPRINTF(SWM_D_EVENT, "enternotify: grab inferior; ignoring.\n");
10690 return;
10691 }
10692
10693 if (focus_mode == SWM_FOCUS_MANUAL &&
10694 e->mode == XCB_NOTIFY_MODE_NORMAL) {
10695 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
10696 return;
10697 }
10698
10699 if (focus_mode != SWM_FOCUS_FOLLOW &&
10700 e->mode == XCB_NOTIFY_MODE_UNGRAB &&
10701 e->detail != XCB_NOTIFY_DETAIL_ANCESTOR) {
10702 DNPRINTF(SWM_D_EVENT, "enternotify: ungrab; ignoring.\n");
10703 return;
10704 }
10705
10706 if ((win = find_window(e->event)) == NULL) {
10707 if (e->event == e->root) {
10708 /* If no windows on pointer region, then focus root. */
10709 r = root_to_region(e->root, SWM_CK_POINTER);
10710 if (r == NULL) {
10711 DNPRINTF(SWM_D_EVENT, "enternotify: "
10712 "NULL region; ignoring.\n");
10713 return;
10714 }
10715
10716 focus_region(r);
10717 } else {
10718 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
10719 "ignoring\n");
10720 return;
10721 }
10722 } else {
10723 if (e->mode == XCB_NOTIFY_MODE_NORMAL &&
10724 e->detail == XCB_NOTIFY_DETAIL_INFERIOR) {
10725 DNPRINTF(SWM_D_EVENT, "enternotify: entering from "
10726 "inferior; ignoring\n");
10727 return;
10728 }
10729
10730 focus_win(get_focus_magic(win));
10731 }
10732
10733 DNPRINTF(SWM_D_EVENT, "enternotify: done\n");
10734
10735 xcb_flush(conn);
10736 }
10737
10738 #ifdef SWM_DEBUG
10739 void
10740 leavenotify(xcb_leave_notify_event_t *e)
10741 {
10742 last_event_time = e->time;
10743
10744 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): %#x "
10745 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10746 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10747 e->time, e->event, e->event_x, e->event_y,
10748 get_notify_mode_label(e->mode), e->mode,
10749 get_notify_detail_label(e->detail), e->detail,
10750 e->root, e->root_x, e->root_y, e->child,
10751 YESNO(e->same_screen_focus), get_state_mask_label(e->state),
10752 e->state);
10753 }
10754 #endif
10755
10756 void
10757 mapnotify(xcb_map_notify_event_t *e)
10758 {
10759 struct ws_win *win, *parent = NULL;
10760 struct workspace *ws;
10761
10762 DNPRINTF(SWM_D_EVENT, "mapnotify: win %#x\n", e->window);
10763
10764 if ((win = manage_window(e->window, spawn_position, false)) == NULL)
10765 return;
10766 ws = win->ws;
10767
10768 if (win->state == SWM_WIN_STATE_REPARENTING)
10769 return;
10770
10771 if (ws->r == NULL) {
10772 unmap_window(win);
10773 goto out;
10774 }
10775
10776 /* Need to know if win was mapped due to ws switch. */
10777 if (ws->state == SWM_WS_STATE_MAPPED) {
10778 if (ws->focus_pending && TRANS(ws->focus_pending))
10779 parent = find_window(win->transient);
10780
10781 /* If window's parent is maximized, don't clear it. */
10782 if ((parent == NULL) || !MAXIMIZED(parent))
10783 if (clear_maximized(ws) > 0)
10784 stack(ws->r);
10785 }
10786
10787 win->mapped = true;
10788 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
10789
10790 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
10791 if (ws->focus_pending == win) {
10792 focus_win(win);
10793 ws->focus_pending = NULL;
10794 center_pointer(ws->r);
10795 focus_flush();
10796 }
10797 }
10798
10799 out:
10800 DNPRINTF(SWM_D_EVENT, "mapnotify: done\n");
10801
10802 xcb_flush(conn);
10803 }
10804
10805 void
10806 mappingnotify(xcb_mapping_notify_event_t *e)
10807 {
10808 if (e->request != XCB_MAPPING_POINTER) {
10809 xcb_refresh_keyboard_mapping(syms, e);
10810 grabkeys();
10811 }
10812
10813 grabbuttons();
10814 }
10815
10816 void
10817 maprequest(xcb_map_request_event_t *e)
10818 {
10819 struct ws_win *win, *w = NULL;
10820
10821 DNPRINTF(SWM_D_EVENT, "maprequest: win %#x\n",
10822 e->window);
10823
10824 win = manage_window(e->window, spawn_position, true);
10825 if (win == NULL)
10826 goto out;
10827
10828 /* The new window should get focus; prepare. */
10829 if (focus_mode != SWM_FOCUS_FOLLOW &&
10830 !(win->quirks & SWM_Q_NOFOCUSONMAP) && ACCEPTS_FOCUS(win)) {
10831 if (win->quirks & SWM_Q_FOCUSONMAP_SINGLE) {
10832 /* See if other wins of same type are already mapped. */
10833 TAILQ_FOREACH(w, &win->ws->winlist, entry) {
10834 if (w == win || !w->mapped)
10835 continue;
10836
10837 if (w->ch.class_name &&
10838 win->ch.class_name &&
10839 strcmp(w->ch.class_name,
10840 win->ch.class_name) == 0 &&
10841 w->ch.instance_name &&
10842 win->ch.instance_name &&
10843 strcmp(w->ch.instance_name,
10844 win->ch.instance_name) == 0)
10845 break;
10846 }
10847 }
10848
10849 if (w == NULL)
10850 win->ws->focus_pending = get_focus_magic(win);
10851 }
10852
10853 /* All windows need to be mapped if they are in the current workspace.*/
10854 stack(win->ws->r);
10855
10856 /* Ignore EnterNotify to handle the mapnotify without interference. */
10857 if (focus_mode == SWM_FOCUS_DEFAULT)
10858 event_drain(XCB_ENTER_NOTIFY);
10859 out:
10860 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
10861 }
10862
10863 void
10864 motionnotify(xcb_motion_notify_event_t *e)
10865 {
10866 struct swm_region *r = NULL;
10867 int i, num_screens;
10868
10869 last_event_time = e->time;
10870
10871 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): %#x "
10872 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10873 "child: %#x, same_screen_focus: %s, state: %d\n",
10874 e->time, e->event, e->event_x, e->event_y,
10875 get_notify_detail_label(e->detail), e->detail,
10876 e->root, e->root_x, e->root_y, e->child,
10877 YESNO(e->same_screen), e->state);
10878
10879 if (focus_mode == SWM_FOCUS_MANUAL)
10880 return;
10881
10882 num_screens = get_screen_count();
10883 for (i = 0; i < num_screens; i++)
10884 if (screens[i].root == e->root)
10885 break;
10886
10887 TAILQ_FOREACH(r, &screens[i].rl, entry)
10888 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
10889 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
10890 break;
10891
10892 focus_region(r);
10893 }
10894
10895 #ifdef SWM_DEBUG
10896 char *
10897 get_atom_name(xcb_atom_t atom)
10898 {
10899 char *name = NULL;
10900 #ifdef SWM_DEBUG_ATOM_NAMES
10901 /*
10902 * This should be disabled during most debugging since
10903 * xcb_get_* causes an xcb_flush.
10904 */
10905 size_t len;
10906 xcb_get_atom_name_reply_t *r;
10907
10908 r = xcb_get_atom_name_reply(conn,
10909 xcb_get_atom_name(conn, atom),
10910 NULL);
10911 if (r) {
10912 len = xcb_get_atom_name_name_length(r);
10913 if (len > 0) {
10914 name = malloc(len + 1);
10915 if (name) {
10916 memcpy(name, xcb_get_atom_name_name(r), len);
10917 name[len] = '\0';
10918 }
10919 }
10920 free(r);
10921 }
10922 #else
10923 (void)atom;
10924 #endif
10925 return (name);
10926 }
10927 #endif
10928
10929 void
10930 propertynotify(xcb_property_notify_event_t *e)
10931 {
10932 struct ws_win *win;
10933 struct workspace *ws;
10934 #ifdef SWM_DEBUG
10935 char *name;
10936
10937 name = get_atom_name(e->atom);
10938 DNPRINTF(SWM_D_EVENT, "propertynotify: win %#x, atom: %s(%u), "
10939 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
10940 e->state);
10941 free(name);
10942 #endif
10943 last_event_time = e->time;
10944
10945 win = find_window(e->window);
10946 if (win == NULL)
10947 return;
10948
10949 ws = win->ws;
10950
10951 if (e->atom == a_state) {
10952 /* State just changed, make sure it gets focused if mapped. */
10953 if (e->state == XCB_PROPERTY_NEW_VALUE) {
10954 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(ws)) {
10955 if (win->mapped &&
10956 win->state == SWM_WIN_STATE_REPARENTED &&
10957 ws->focus_pending == win) {
10958 focus_win(ws->focus_pending);
10959 ws->focus_pending = NULL;
10960 }
10961 }
10962 }
10963 } else if (e->atom == XCB_ATOM_WM_CLASS ||
10964 e->atom == XCB_ATOM_WM_NAME) {
10965 if (ws->r)
10966 bar_draw(ws->r->bar);
10967 } else if (e->atom == a_prot) {
10968 get_wm_protocols(win);
10969 } else if (e->atom == XCB_ATOM_WM_NORMAL_HINTS) {
10970 xcb_icccm_get_wm_normal_hints_reply(conn,
10971 xcb_icccm_get_wm_normal_hints(conn, win->id),
10972 &win->sh, NULL);
10973 }
10974
10975 xcb_flush(conn);
10976 }
10977
10978 void
10979 reparentnotify(xcb_reparent_notify_event_t *e)
10980 {
10981 struct ws_win *win;
10982
10983 DNPRINTF(SWM_D_EVENT, "reparentnotify: event: %#x, win %#x, "
10984 "parent: %#x, (x,y): (%u,%u), override_redirect: %u\n",
10985 e->event, e->window, e->parent, e->x, e->y, e->override_redirect);
10986
10987 win = find_window(e->window);
10988 if (win) {
10989 if (win->state == SWM_WIN_STATE_REPARENTING) {
10990 win->state = SWM_WIN_STATE_REPARENTED;
10991
10992 if (win->ws->r)
10993 map_window(win);
10994 else
10995 unmap_window(win);
10996
10997 update_window(win);
10998 update_win_stacking(win);
10999 } else if (win->state == SWM_WIN_STATE_UNPARENTING) {
11000 win->state = SWM_WIN_STATE_UNPARENTED;
11001 }
11002 }
11003 }
11004
11005 void
11006 unmapnotify(xcb_unmap_notify_event_t *e)
11007 {
11008 struct ws_win *win;
11009 struct workspace *ws;
11010
11011 DNPRINTF(SWM_D_EVENT, "unmapnotify: win %#x\n", e->window);
11012
11013 /* If we aren't managing the window, then ignore. */
11014 win = find_window(e->window);
11015 if (win == NULL || win->id != e->window) {
11016 DNPRINTF(SWM_D_EVENT, "unmapnotify: ignore unmanaged.\n");
11017 return;
11018 }
11019
11020 /* Do nothing if already withdrawn. */
11021 if (!win->mapped && !ICONIC(win)) {
11022 DNPRINTF(SWM_D_EVENT, "unmapnotify: ignore withdrawn.\n");
11023 return;
11024 }
11025
11026 ws = win->ws;
11027 win->mapped = false;
11028
11029 /* Ignore if reparenting-related. */
11030 if (win->state != SWM_WIN_STATE_REPARENTED) {
11031 DNPRINTF(SWM_D_EVENT, "unmapnotify: ignore not reparented.\n");
11032 return;
11033 }
11034
11035 /* If win was focused, make sure to focus on something else. */
11036 if (win == ws->focus) {
11037 if (focus_mode != SWM_FOCUS_FOLLOW) {
11038 ws->focus_pending = get_focus_prev(win);
11039 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
11040 "focus_pending: %#x\n",
11041 WINID(ws->focus_pending));
11042 }
11043
11044 unfocus_win(win);
11045 }
11046
11047 if (ICONIC(win)) {
11048 /* Iconify. */
11049 DNPRINTF(SWM_D_EVENT, "unmapnotify: iconify.\n");
11050 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
11051 } else {
11052 /* Withdraw. */
11053 DNPRINTF(SWM_D_EVENT, "unmapnotify: withdraw.\n");
11054 set_win_state(win, XCB_ICCCM_WM_STATE_WITHDRAWN);
11055 unmanage_window(win);
11056 }
11057
11058 stack(ws->r);
11059
11060 /* Update focus if ws is active. */
11061 if (WS_FOCUSED(ws)) {
11062 if (focus_mode == SWM_FOCUS_FOLLOW) {
11063 focus_win(get_pointer_win(ws->r->s->root));
11064 } else if (ws->focus_pending) {
11065 focus_win(ws->focus_pending);
11066 ws->focus_pending = NULL;
11067 } else if (ws->focus == NULL) {
11068 DNPRINTF(SWM_D_FOCUS, "unmapnotify: set_input_focus: "
11069 "%#x, revert-to: parent, time: 0\n",
11070 ws->r->id);
11071 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
11072 ws->r->id, XCB_CURRENT_TIME);
11073 }
11074 }
11075
11076 center_pointer(ws->r);
11077 focus_flush();
11078 DNPRINTF(SWM_D_EVENT, "unmapnotify: done.\n");
11079 }
11080
11081 #ifdef SWM_DEBUG
11082 char *
11083 get_source_type_label(uint32_t type)
11084 {
11085 char *label;
11086
11087 switch (type) {
11088 case EWMH_SOURCE_TYPE_NONE:
11089 label = "None";
11090 break;
11091 case EWMH_SOURCE_TYPE_NORMAL:
11092 label = "Normal";
11093 break;
11094 case EWMH_SOURCE_TYPE_OTHER:
11095 label = "Other";
11096 break;
11097 default:
11098 label = "Invalid";
11099 }
11100
11101 return label;
11102 }
11103 #endif
11104
11105 void
11106 clientmessage(xcb_client_message_event_t *e)
11107 {
11108 struct ws_win *win;
11109 struct swm_region *r = NULL;
11110 union arg a;
11111 uint32_t vals[4];
11112 int num_screens, i;
11113 xcb_map_request_event_t mre;
11114 #ifdef SWM_DEBUG
11115 char *name;
11116
11117 name = get_atom_name(e->type);
11118 DNPRINTF(SWM_D_EVENT, "clientmessage: win %#x, atom: %s(%u)\n",
11119 e->window, name, e->type);
11120 free(name);
11121 #endif
11122
11123 if (e->type == ewmh[_NET_CURRENT_DESKTOP].atom) {
11124 num_screens = get_screen_count();
11125 for (i = 0; i < num_screens; i++)
11126 if (screens[i].root == e->window) {
11127 r = screens[i].r_focus;
11128 break;
11129 }
11130
11131 if (r && e->data.data32[0] < (uint32_t)workspace_limit) {
11132 a.id = e->data.data32[0];
11133 switchws(NULL, r, &a);
11134 focus_flush();
11135 }
11136
11137 return;
11138 } else if (e->type == ewmh[_NET_REQUEST_FRAME_EXTENTS].atom) {
11139 DNPRINTF(SWM_D_EVENT,
11140 "clientmessage: set _NET_FRAME_EXTENTS on window.\n");
11141 vals[0] = vals[1] = vals[2] = vals[3] = border_width;
11142 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, e->window,
11143 a_net_frame_extents, XCB_ATOM_CARDINAL, 32, 4, vals);
11144 xcb_flush(conn);
11145 return;
11146 }
11147
11148 win = find_window(e->window);
11149 if (win == NULL) {
11150 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
11151 /* Manage the window with maprequest. */
11152 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
11153 "unmanaged window.\n");
11154 mre.window = e->window;
11155 maprequest(&mre);
11156 }
11157 return;
11158 }
11159
11160 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
11161 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW, "
11162 "source_type: %s(%d)\n",
11163 get_source_type_label(e->data.data32[0]),
11164 e->data.data32[0]);
11165
11166 /*
11167 * Allow focus changes that are a result of direct user
11168 * action and from applications that use the old EWMH spec.
11169 */
11170 if (e->data.data32[0] != EWMH_SOURCE_TYPE_NORMAL ||
11171 win->quirks & SWM_Q_OBEYAPPFOCUSREQ) {
11172 if (WS_FOCUSED(win->ws))
11173 focus_win(win);
11174 else
11175 win->ws->focus_pending = win;
11176 }
11177 } else if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
11178 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
11179 if (win->can_delete)
11180 client_msg(win, a_delete, 0);
11181 else
11182 xcb_kill_client(conn, win->id);
11183 } else if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
11184 DNPRINTF(SWM_D_EVENT,
11185 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
11186 if (ABOVE(win)) {
11187 if (e->data.data32[0] & (1<<8)) /* x */
11188 X(win) = e->data.data32[1];
11189 if (e->data.data32[0] & (1<<9)) /* y */
11190 Y(win) = e->data.data32[2];
11191 if (e->data.data32[0] & (1<<10)) /* width */
11192 WIDTH(win) = e->data.data32[3];
11193 if (e->data.data32[0] & (1<<11)) /* height */
11194 HEIGHT(win) = e->data.data32[4];
11195
11196 update_window(win);
11197 } else {
11198 /* Notify no change was made. */
11199 config_win(win, NULL);
11200 /* TODO: Change stack sizes */
11201 }
11202 } else if (e->type == ewmh[_NET_RESTACK_WINDOW].atom) {
11203 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_RESTACK_WINDOW\n");
11204 vals[0] = e->data.data32[1]; /* Sibling window. */
11205 vals[1] = e->data.data32[2]; /* Stack mode detail. */
11206
11207 if (win->frame != XCB_WINDOW_NONE)
11208 xcb_configure_window(conn, win->frame,
11209 XCB_CONFIG_WINDOW_SIBLING |
11210 XCB_CONFIG_WINDOW_STACK_MODE, vals);
11211 } else if (e->type == ewmh[_NET_WM_STATE].atom) {
11212 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
11213 ewmh_change_wm_state(win, e->data.data32[1], e->data.data32[0]);
11214 if (e->data.data32[2])
11215 ewmh_change_wm_state(win, e->data.data32[2],
11216 e->data.data32[0]);
11217
11218 ewmh_update_wm_state(win);
11219 stack(win->ws->r);
11220 } else if (e->type == ewmh[_NET_WM_DESKTOP].atom) {
11221 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_DESKTOP\n");
11222 r = win->ws->r;
11223
11224 win_to_ws(win, e->data.data32[0], true);
11225
11226 /* Stack source and destination ws, if mapped. */
11227 if (r != win->ws->r) {
11228 if (r)
11229 stack(r);
11230
11231 if (win->ws->r) {
11232 if (FLOATING(win))
11233 load_float_geom(win);
11234
11235 stack(win->ws->r);
11236 }
11237 }
11238 }
11239
11240 focus_flush();
11241 }
11242
11243 void
11244 check_conn(void)
11245 {
11246 int errcode = xcb_connection_has_error(conn);
11247 #ifdef XCB_CONN_ERROR
11248 char *s;
11249 switch (errcode) {
11250 case XCB_CONN_ERROR:
11251 s = "Socket error, pipe error or other stream error.";
11252 break;
11253 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
11254 s = "Extension not supported.";
11255 break;
11256 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
11257 s = "Insufficient memory.";
11258 break;
11259 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
11260 s = "Request length was exceeded.";
11261 break;
11262 case XCB_CONN_CLOSED_PARSE_ERR:
11263 s = "Error parsing display string.";
11264 break;
11265 default:
11266 s = "Unknown error.";
11267 }
11268 if (errcode)
11269 errx(errcode, "X CONNECTION ERROR: %s", s);
11270 #else
11271 if (errcode)
11272 errx(errcode, "X CONNECTION ERROR");
11273 #endif
11274 }
11275
11276 int
11277 enable_wm(void)
11278 {
11279 int num_screens, i;
11280 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
11281 XCB_EVENT_MASK_ENTER_WINDOW |
11282 XCB_EVENT_MASK_OWNER_GRAB_BUTTON |
11283 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
11284 XCB_EVENT_MASK_ENTER_WINDOW |
11285 XCB_EVENT_MASK_LEAVE_WINDOW |
11286 XCB_EVENT_MASK_BUTTON_PRESS |
11287 XCB_EVENT_MASK_BUTTON_RELEASE |
11288 XCB_EVENT_MASK_KEY_PRESS |
11289 XCB_EVENT_MASK_KEY_RELEASE |
11290 XCB_EVENT_MASK_PROPERTY_CHANGE;
11291 xcb_screen_t *sc;
11292 xcb_void_cookie_t wac;
11293 xcb_generic_error_t *error;
11294
11295 /* this causes an error if some other window manager is running */
11296 num_screens = get_screen_count();
11297 for (i = 0; i < num_screens; i++) {
11298 if ((sc = get_screen(i)) == NULL)
11299 errx(1, "ERROR: can't get screen %d.", i);
11300 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: %#x\n",
11301 i, sc->root);
11302 wac = xcb_change_window_attributes_checked(conn, sc->root,
11303 XCB_CW_EVENT_MASK, &val);
11304 if ((error = xcb_request_check(conn, wac))) {
11305 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
11306 error->error_code);
11307 free(error);
11308 return 1;
11309 }
11310 }
11311
11312 return 0;
11313 }
11314
11315 void
11316 new_region(struct swm_screen *s, int x, int y, int w, int h)
11317 {
11318 struct swm_region *r = NULL, *n;
11319 struct workspace *ws = NULL;
11320 int i;
11321 uint32_t wa[1];
11322
11323 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
11324 s->idx, w, h, x, y);
11325
11326 /* remove any conflicting regions */
11327 n = TAILQ_FIRST(&s->rl);
11328 while (n) {
11329 r = n;
11330 n = TAILQ_NEXT(r, entry);
11331 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
11332 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
11333 if (r->ws->r != NULL)
11334 r->ws->old_r = r->ws->r;
11335 r->ws->r = NULL;
11336 bar_cleanup(r);
11337 xcb_destroy_window(conn, r->id);
11338 r->id = XCB_WINDOW_NONE;
11339 TAILQ_REMOVE(&s->rl, r, entry);
11340 TAILQ_INSERT_TAIL(&s->orl, r, entry);
11341 }
11342 }
11343
11344 /* search old regions for one to reuse */
11345
11346 /* size + location match */
11347 TAILQ_FOREACH(r, &s->orl, entry)
11348 if (X(r) == x && Y(r) == y &&
11349 HEIGHT(r) == h && WIDTH(r) == w)
11350 break;
11351
11352 /* size match */
11353 TAILQ_FOREACH(r, &s->orl, entry)
11354 if (HEIGHT(r) == h && WIDTH(r) == w)
11355 break;
11356
11357 if (r != NULL) {
11358 TAILQ_REMOVE(&s->orl, r, entry);
11359 /* try to use old region's workspace */
11360 if (r->ws->r == NULL)
11361 ws = r->ws;
11362 } else
11363 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
11364 err(1, "new_region: calloc: failed to allocate memory "
11365 "for screen");
11366
11367 /* if we don't have a workspace already, find one */
11368 if (ws == NULL) {
11369 for (i = 0; i < workspace_limit; i++)
11370 if (s->ws[i].r == NULL) {
11371 ws = &s->ws[i];
11372 break;
11373 }
11374 }
11375
11376 if (ws == NULL)
11377 errx(1, "new_region: no free workspaces");
11378
11379 if (ws->state == SWM_WS_STATE_HIDDEN)
11380 ws->state = SWM_WS_STATE_MAPPING;
11381
11382 X(r) = x;
11383 Y(r) = y;
11384 WIDTH(r) = w;
11385 HEIGHT(r) = h;
11386 r->bar = NULL;
11387 r->s = s;
11388 r->ws = ws;
11389 r->ws_prior = NULL;
11390 ws->r = r;
11391 outputs++;
11392 TAILQ_INSERT_TAIL(&s->rl, r, entry);
11393
11394 /* Invisible region window to detect pointer events on empty regions. */
11395 r->id = xcb_generate_id(conn);
11396 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
11397 XCB_EVENT_MASK_POINTER_MOTION_HINT;
11398
11399 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
11400 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
11401 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
11402
11403 /* Make sure region input is at the bottom. */
11404 wa[0] = XCB_STACK_MODE_BELOW;
11405 xcb_configure_window(conn, r->id, XCB_CONFIG_WINDOW_STACK_MODE, wa);
11406
11407 xcb_map_window(conn, r->id);
11408 }
11409
11410 void
11411 scan_randr(int idx)
11412 {
11413 #ifdef SWM_XRR_HAS_CRTC
11414 int c;
11415 int ncrtc = 0;
11416 #endif /* SWM_XRR_HAS_CRTC */
11417 struct swm_region *r;
11418 struct ws_win *win;
11419 int num_screens;
11420 xcb_randr_get_screen_resources_current_cookie_t src;
11421 xcb_randr_get_screen_resources_current_reply_t *srr;
11422 xcb_randr_get_crtc_info_cookie_t cic;
11423 xcb_randr_get_crtc_info_reply_t *cir = NULL;
11424 xcb_randr_crtc_t *crtc;
11425 xcb_screen_t *screen;
11426
11427 DNPRINTF(SWM_D_MISC, "scan_randr: screen: %d\n", idx);
11428
11429 if ((screen = get_screen(idx)) == NULL)
11430 errx(1, "ERROR: can't get screen %d.", idx);
11431
11432 num_screens = get_screen_count();
11433 if (idx >= num_screens)
11434 errx(1, "scan_randr: invalid screen");
11435
11436 /* remove any old regions */
11437 while ((r = TAILQ_FIRST(&screens[idx].rl)) != NULL) {
11438 r->ws->old_r = r->ws->r = NULL;
11439 bar_cleanup(r);
11440 xcb_destroy_window(conn, r->id);
11441 r->id = XCB_WINDOW_NONE;
11442 TAILQ_REMOVE(&screens[idx].rl, r, entry);
11443 TAILQ_INSERT_TAIL(&screens[idx].orl, r, entry);
11444 }
11445 outputs = 0;
11446
11447 /* map virtual screens onto physical screens */
11448 #ifdef SWM_XRR_HAS_CRTC
11449 if (randr_support) {
11450 src = xcb_randr_get_screen_resources_current(conn,
11451 screens[idx].root);
11452 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
11453 NULL);
11454 if (srr == NULL) {
11455 new_region(&screens[idx], 0, 0,
11456 screen->width_in_pixels,
11457 screen->height_in_pixels);
11458 goto out;
11459 } else
11460 ncrtc = srr->num_crtcs;
11461
11462 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
11463 for (c = 0; c < ncrtc; c++) {
11464 cic = xcb_randr_get_crtc_info(conn, crtc[c],
11465 XCB_CURRENT_TIME);
11466 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
11467 if (cir == NULL)
11468 continue;
11469 if (cir->num_outputs == 0) {
11470 free(cir);
11471 continue;
11472 }
11473
11474 if (cir->mode == 0)
11475 new_region(&screens[idx], 0, 0,
11476 screen->width_in_pixels,
11477 screen->height_in_pixels);
11478 else
11479 new_region(&screens[idx],
11480 cir->x, cir->y, cir->width, cir->height);
11481 free(cir);
11482 }
11483 free(srr);
11484 }
11485 #endif /* SWM_XRR_HAS_CRTC */
11486
11487 /* If detection failed, create a single region that spans the screen. */
11488 if (TAILQ_EMPTY(&screens[idx].rl))
11489 new_region(&screens[idx], 0, 0, screen->width_in_pixels,
11490 screen->height_in_pixels);
11491
11492 out:
11493 /* Cleanup unused previously visible workspaces. */
11494 TAILQ_FOREACH(r, &screens[idx].orl, entry) {
11495 TAILQ_FOREACH(win, &r->ws->winlist, entry)
11496 unmap_window(win);
11497 r->ws->state = SWM_WS_STATE_HIDDEN;
11498
11499 /* The screen shouldn't focus on an unused region. */
11500 if (screens[idx].r_focus == r)
11501 screens[idx].r_focus = NULL;
11502 }
11503
11504 DNPRINTF(SWM_D_MISC, "scan_randr: done.\n");
11505 }
11506
11507 void
11508 screenchange(xcb_randr_screen_change_notify_event_t *e)
11509 {
11510 struct swm_region *r;
11511 int i, num_screens;
11512
11513 DNPRINTF(SWM_D_EVENT, "screenchange: root: %#x\n", e->root);
11514
11515 num_screens = get_screen_count();
11516 /* silly event doesn't include the screen index */
11517 for (i = 0; i < num_screens; i++)
11518 if (screens[i].root == e->root)
11519 break;
11520 if (i >= num_screens)
11521 errx(1, "screenchange: screen not found");
11522
11523 /* brute force for now, just re-enumerate the regions */
11524 scan_randr(i);
11525
11526 #ifdef SWM_DEBUG
11527 print_win_geom(e->root);
11528 #endif
11529 /* add bars to all regions */
11530 TAILQ_FOREACH(r, &screens[i].rl, entry)
11531 bar_setup(r);
11532
11533 /* Stack all regions. */
11534 TAILQ_FOREACH(r, &screens[i].rl, entry)
11535 stack(r);
11536
11537 /* Make sure a region has focus. */
11538 if (screens[i].r_focus == NULL) {
11539 r = TAILQ_FIRST(&screens[i].rl);
11540 if (r != NULL)
11541 focus_region(r);
11542 }
11543
11544 focus_flush();
11545
11546 /* Update workspace state and bar on all regions. */
11547 TAILQ_FOREACH(r, &screens[i].rl, entry) {
11548 r->ws->state = SWM_WS_STATE_MAPPED;
11549 bar_draw(r->bar);
11550 }
11551 }
11552
11553 void
11554 grab_windows(void)
11555 {
11556 struct swm_region *r = NULL;
11557 xcb_query_tree_cookie_t qtc;
11558 xcb_query_tree_reply_t *qtr;
11559 xcb_get_property_cookie_t pc;
11560 xcb_get_property_reply_t *pr;
11561 xcb_window_t *wins = NULL, trans, *cwins = NULL;
11562 int i, j, k, n, no, num_screens;
11563
11564 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
11565 num_screens = get_screen_count();
11566 for (i = 0; i < num_screens; i++) {
11567 qtc = xcb_query_tree(conn, screens[i].root);
11568 qtr = xcb_query_tree_reply(conn, qtc, NULL);
11569 if (qtr == NULL)
11570 continue;
11571 wins = xcb_query_tree_children(qtr);
11572 no = xcb_query_tree_children_length(qtr);
11573
11574 /* Try to sort windows according to _NET_CLIENT_LIST. */
11575 pr = xcb_get_property_reply(conn, xcb_get_property(conn, 0,
11576 screens[i].root, ewmh[_NET_CLIENT_LIST].atom,
11577 XCB_ATOM_WINDOW, 0, UINT32_MAX), NULL);
11578 if (pr != NULL) {
11579 cwins = xcb_get_property_value(pr);
11580 n = xcb_get_property_value_length(pr) /
11581 sizeof(xcb_atom_t);
11582
11583 for (j = 0; j < n; ++j) {
11584 for (k = j; k < no; ++k) {
11585 if (wins[k] == cwins[j]) {
11586 /* Swap wins j and k. */
11587 wins[k] = wins[j];
11588 wins[j] = cwins[j];
11589 }
11590 }
11591 }
11592
11593 free(pr);
11594 }
11595
11596 /* Manage top-level windows first, then transients. */
11597 /* TODO: allow transients to be managed before leader. */
11598 DNPRINTF(SWM_D_INIT, "grab_windows: grab top-level windows.\n");
11599 for (j = 0; j < no; j++) {
11600 TAILQ_FOREACH(r, &screens[i].rl, entry) {
11601 if (r->id == wins[j]) {
11602 DNPRINTF(SWM_D_INIT, "grab_windows: "
11603 "skip %#x; region input window.\n",
11604 wins[j]);
11605 break;
11606 } else if (r->bar->id == wins[j]) {
11607 DNPRINTF(SWM_D_INIT, "grab_windows: "
11608 "skip %#x; region bar.\n",
11609 wins[j]);
11610 break;
11611 }
11612 }
11613
11614 if (r)
11615 continue;
11616
11617 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
11618 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
11619 &trans, NULL)) {
11620 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
11621 "is transient for %#x.\n", wins[j], trans);
11622 continue;
11623 }
11624
11625 manage_window(wins[j], SWM_STACK_TOP, false);
11626 }
11627
11628 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
11629 for (j = 0; j < no; j++) {
11630 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
11631 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
11632 &trans, NULL))
11633 manage_window(wins[j], SWM_STACK_TOP, false);
11634 }
11635 free(qtr);
11636 }
11637 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
11638 }
11639
11640 void
11641 setup_screens(void)
11642 {
11643 int i, j, k, num_screens;
11644 struct workspace *ws;
11645 uint32_t gcv[1], wa[1];
11646 const xcb_query_extension_reply_t *qep;
11647 xcb_screen_t *screen;
11648 xcb_randr_query_version_cookie_t c;
11649 xcb_randr_query_version_reply_t *r;
11650
11651 num_screens = get_screen_count();
11652 if ((screens = calloc(num_screens, sizeof(struct swm_screen))) == NULL)
11653 err(1, "setup_screens: calloc: failed to allocate memory for "
11654 "screens");
11655
11656 /* Initial RandR setup. */
11657 randr_support = false;
11658 qep = xcb_get_extension_data(conn, &xcb_randr_id);
11659 if (qep->present) {
11660 c = xcb_randr_query_version(conn, 1, 1);
11661 r = xcb_randr_query_version_reply(conn, c, NULL);
11662 if (r) {
11663 if (r->major_version >= 1) {
11664 randr_support = true;
11665 randr_eventbase = qep->first_event;
11666 }
11667 free(r);
11668 }
11669 }
11670
11671 wa[0] = cursors[XC_LEFT_PTR].cid;
11672
11673 /* map physical screens */
11674 for (i = 0; i < num_screens; i++) {
11675 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
11676 screens[i].idx = i;
11677 screens[i].r_focus = NULL;
11678
11679 TAILQ_INIT(&screens[i].rl);
11680 TAILQ_INIT(&screens[i].orl);
11681 if ((screen = get_screen(i)) == NULL)
11682 errx(1, "ERROR: can't get screen %d.", i);
11683 screens[i].root = screen->root;
11684
11685 /* set default colors */
11686 setscreencolor("red", i, SWM_S_COLOR_FOCUS);
11687 setscreencolor("rgb:88/88/88", i, SWM_S_COLOR_UNFOCUS);
11688 setscreencolor("rgb:00/80/80", i, SWM_S_COLOR_BAR_BORDER);
11689 setscreencolor("rgb:00/40/40", i,
11690 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
11691 setscreencolor("black", i, SWM_S_COLOR_BAR);
11692 setscreencolor("rgb:a0/a0/a0", i, SWM_S_COLOR_BAR_FONT);
11693 setscreencolor("red", i, SWM_S_COLOR_FOCUS_MAXIMIZED);
11694 setscreencolor("rgb:88/88/88", i,
11695 SWM_S_COLOR_UNFOCUS_MAXIMIZED);
11696
11697 /* create graphics context on screen */
11698 screens[i].bar_gc = xcb_generate_id(conn);
11699 gcv[0] = 0;
11700 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
11701 XCB_GC_GRAPHICS_EXPOSURES, gcv);
11702
11703 /* set default cursor */
11704 xcb_change_window_attributes(conn, screens[i].root,
11705 XCB_CW_CURSOR, wa);
11706
11707 /* init all workspaces */
11708 /* XXX these should be dynamically allocated too */
11709 for (j = 0; j < SWM_WS_MAX; j++) {
11710 ws = &screens[i].ws[j];
11711 ws->idx = j;
11712 ws->name = NULL;
11713 ws->bar_enabled = true;
11714 ws->focus = NULL;
11715 ws->focus_prev = NULL;
11716 ws->focus_pending = NULL;
11717 ws->r = NULL;
11718 ws->old_r = NULL;
11719 ws->state = SWM_WS_STATE_HIDDEN;
11720 TAILQ_INIT(&ws->stack);
11721 TAILQ_INIT(&ws->winlist);
11722 TAILQ_INIT(&ws->unmanagedlist);
11723
11724 for (k = 0; layouts[k].l_stack != NULL; k++)
11725 if (layouts[k].l_config != NULL)
11726 layouts[k].l_config(ws,
11727 SWM_ARG_ID_STACKINIT);
11728 ws->cur_layout = &layouts[0];
11729 ws->cur_layout->l_string(ws);
11730 }
11731
11732 scan_randr(i);
11733
11734 if (randr_support)
11735 xcb_randr_select_input(conn, screens[i].root,
11736 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
11737 }
11738 }
11739
11740 void
11741 setup_globals(void)
11742 {
11743 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
11744 err(1, "setup_globals: strdup: failed to allocate memory.");
11745
11746 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
11747 err(1, "setup_globals: strdup: failed to allocate memory.");
11748
11749 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
11750 errx(1, "unable to allocate key symbols");
11751
11752 a_state = get_atom_from_string("WM_STATE");
11753 a_prot = get_atom_from_string("WM_PROTOCOLS");
11754 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
11755 a_net_frame_extents = get_atom_from_string("_NET_FRAME_EXTENTS");
11756 a_net_supported = get_atom_from_string("_NET_SUPPORTED");
11757 a_net_wm_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
11758 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
11759 a_utf8_string = get_atom_from_string("UTF8_STRING");
11760 a_swm_ws = get_atom_from_string("_SWM_WS");
11761 }
11762
11763 void
11764 shutdown_cleanup(void)
11765 {
11766 struct swm_region *r;
11767 struct ws_win *w;
11768 struct workspace *ws;
11769 int i, num_screens;
11770
11771 /* disable alarm because the following code may not be interrupted */
11772 alarm(0);
11773 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
11774 err(1, "can't disable alarm");
11775
11776 bar_extra_stop();
11777 unmap_all();
11778
11779 cursors_cleanup();
11780
11781 clear_quirks();
11782 clear_spawns();
11783 clear_bindings();
11784
11785 teardown_ewmh();
11786
11787 num_screens = get_screen_count();
11788 for (i = 0; i < num_screens; ++i) {
11789 int j;
11790
11791 DNPRINTF(SWM_D_FOCUS, "shutdown_cleanup: set_input_focus: "
11792 "%#x, revert-to: root, time: 0\n", screens[i].root);
11793 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
11794 screens[i].root, XCB_CURRENT_TIME);
11795
11796 if (screens[i].bar_gc != XCB_NONE)
11797 xcb_free_gc(conn, screens[i].bar_gc);
11798 if (!bar_font_legacy) {
11799 XftColorFree(display, DefaultVisual(display, i),
11800 DefaultColormap(display, i), &bar_font_color);
11801 XftColorFree(display, DefaultVisual(display, i),
11802 DefaultColormap(display, i), &search_font_color);
11803 }
11804
11805 for (j = 0; j < SWM_S_COLOR_MAX; ++j) {
11806 free(screens[i].c[j].name);
11807 }
11808
11809 /* Free window memory. */
11810 for (j = 0; j < SWM_WS_MAX; ++j) {
11811 ws = &screens[i].ws[j];
11812 free(ws->name);
11813
11814 while ((w = TAILQ_FIRST(&ws->winlist)) != NULL) {
11815 TAILQ_REMOVE(&ws->winlist, w, entry);
11816 free_window(w);
11817 }
11818
11819 while ((w = TAILQ_FIRST(&ws->unmanagedlist)) != NULL) {
11820 TAILQ_REMOVE(&ws->unmanagedlist, w, entry);
11821 free_window(w);
11822 }
11823 }
11824
11825 /* Free region memory. */
11826 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
11827 TAILQ_REMOVE(&screens[i].rl, r, entry);
11828 free(r->bar);
11829 free(r);
11830 }
11831
11832 while ((r = TAILQ_FIRST(&screens[i].orl)) != NULL) {
11833 TAILQ_REMOVE(&screens[i].rl, r, entry);
11834 free(r->bar);
11835 free(r);
11836 }
11837 }
11838 free(screens);
11839
11840 free(bar_format);
11841 free(bar_fonts);
11842 free(clock_format);
11843 free(startup_exception);
11844
11845 if (bar_fs)
11846 XFreeFontSet(display, bar_fs);
11847 if (bar_font)
11848 XftFontClose(display, bar_font);
11849
11850 xcb_key_symbols_free(syms);
11851 xcb_flush(conn);
11852 xcb_aux_sync(conn);
11853 xcb_disconnect(conn);
11854 }
11855
11856 void
11857 event_error(xcb_generic_error_t *e)
11858 {
11859 (void)e;
11860
11861 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
11862 "resource_id: %u, minor_code: %u\n",
11863 xcb_event_get_error_label(e->error_code), e->error_code,
11864 xcb_event_get_request_label(e->major_code), e->major_code,
11865 e->sequence, e->resource_id, e->minor_code);
11866 }
11867
11868 void
11869 event_handle(xcb_generic_event_t *evt)
11870 {
11871 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
11872
11873 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
11874 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
11875 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
11876
11877 switch (type) {
11878 #define EVENT(type, callback) case type: callback((void *)evt); return
11879 EVENT(0, event_error);
11880 EVENT(XCB_BUTTON_PRESS, buttonpress);
11881 EVENT(XCB_BUTTON_RELEASE, buttonrelease);
11882 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
11883 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
11884 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
11885 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
11886 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
11887 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
11888 /*EVENT(XCB_CREATE_NOTIFY, );*/
11889 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
11890 EVENT(XCB_ENTER_NOTIFY, enternotify);
11891 EVENT(XCB_EXPOSE, expose);
11892 EVENT(XCB_FOCUS_IN, focusin);
11893 #ifdef SWM_DEBUG
11894 EVENT(XCB_FOCUS_OUT, focusout);
11895 #endif
11896 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
11897 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
11898 EVENT(XCB_KEY_PRESS, keypress);
11899 EVENT(XCB_KEY_RELEASE, keyrelease);
11900 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
11901 #ifdef SWM_DEBUG
11902 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
11903 #endif
11904 EVENT(XCB_MAP_NOTIFY, mapnotify);
11905 EVENT(XCB_MAP_REQUEST, maprequest);
11906 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
11907 EVENT(XCB_MOTION_NOTIFY, motionnotify);
11908 /*EVENT(XCB_NO_EXPOSURE, );*/
11909 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
11910 EVENT(XCB_REPARENT_NOTIFY, reparentnotify);
11911 /*EVENT(XCB_RESIZE_REQUEST, );*/
11912 /*EVENT(XCB_SELECTION_CLEAR, );*/
11913 /*EVENT(XCB_SELECTION_NOTIFY, );*/
11914 /*EVENT(XCB_SELECTION_REQUEST, );*/
11915 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
11916 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
11917 #undef EVENT
11918 }
11919 if (type - randr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
11920 screenchange((void *)evt);
11921 }
11922
11923 int
11924 main(int argc, char *argv[])
11925 {
11926 struct pollfd pfd[2];
11927 struct sigaction sact;
11928 struct stat sb;
11929 struct passwd *pwd;
11930 struct swm_region *r;
11931 xcb_generic_event_t *evt;
11932 int xfd, i, num_screens, num_readable;
11933 char conf[PATH_MAX], *cfile = NULL;
11934 bool stdin_ready = false, startup = true;
11935
11936 /* suppress unused warning since var is needed */
11937 (void)argc;
11938
11939 #ifdef SWM_DEBUG
11940 time_started = time(NULL);
11941 #endif
11942
11943 start_argv = argv;
11944 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
11945 if (setlocale(LC_CTYPE, "") == NULL || setlocale(LC_TIME, "") == NULL)
11946 warnx("no locale support");
11947
11948 /* handle some signals */
11949 bzero(&sact, sizeof(sact));
11950 sigemptyset(&sact.sa_mask);
11951 sact.sa_flags = 0;
11952 sact.sa_handler = sighdlr;
11953 sigaction(SIGINT, &sact, NULL);
11954 sigaction(SIGQUIT, &sact, NULL);
11955 sigaction(SIGTERM, &sact, NULL);
11956 sigaction(SIGHUP, &sact, NULL);
11957
11958 sact.sa_handler = sighdlr;
11959 sact.sa_flags = SA_NOCLDSTOP;
11960 sigaction(SIGCHLD, &sact, NULL);
11961
11962 if ((display = XOpenDisplay(0)) == NULL)
11963 errx(1, "can not open display");
11964
11965 conn = XGetXCBConnection(display);
11966 if (xcb_connection_has_error(conn))
11967 errx(1, "can not get XCB connection");
11968
11969 XSetEventQueueOwner(display, XCBOwnsEventQueue);
11970
11971 xcb_prefetch_extension_data(conn, &xcb_randr_id);
11972 xfd = xcb_get_file_descriptor(conn);
11973
11974 /* look for local and global conf file */
11975 pwd = getpwuid(getuid());
11976 if (pwd == NULL)
11977 errx(1, "invalid user: %d", getuid());
11978
11979 xcb_grab_server(conn);
11980 xcb_aux_sync(conn);
11981
11982 /* flush all events */
11983 while ((evt = get_next_event(false))) {
11984 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
11985 event_handle(evt);
11986 free(evt);
11987 }
11988
11989 if (enable_wm())
11990 errx(1, "another window manager is currently running");
11991
11992 /* Load Xcursors and/or cursorfont glyph cursors. */
11993 cursors_load();
11994
11995 xcb_aux_sync(conn);
11996
11997 setup_globals();
11998 setup_screens();
11999 setup_ewmh();
12000 setup_keybindings();
12001 setup_btnbindings();
12002 setup_quirks();
12003 setup_spawn();
12004
12005 /* load config */
12006 for (i = 0; ; i++) {
12007 conf[0] = '\0';
12008 switch (i) {
12009 case 0:
12010 /* ~ */
12011 snprintf(conf, sizeof conf, "%s/.%s",
12012 pwd->pw_dir, SWM_CONF_FILE);
12013 break;
12014 case 1:
12015 /* global */
12016 snprintf(conf, sizeof conf, "/etc/%s",
12017 SWM_CONF_FILE);
12018 break;
12019 case 2:
12020 /* ~ compat */
12021 snprintf(conf, sizeof conf, "%s/.%s",
12022 pwd->pw_dir, SWM_CONF_FILE_OLD);
12023 break;
12024 case 3:
12025 /* global compat */
12026 snprintf(conf, sizeof conf, "/etc/%s",
12027 SWM_CONF_FILE_OLD);
12028 break;
12029 default:
12030 goto noconfig;
12031 }
12032
12033 if (strlen(conf) && stat(conf, &sb) != -1)
12034 if (S_ISREG(sb.st_mode)) {
12035 cfile = conf;
12036 break;
12037 }
12038 }
12039 noconfig:
12040
12041 /* load conf (if any) */
12042 if (cfile)
12043 conf_load(cfile, SWM_CONF_DEFAULT);
12044
12045 validate_spawns();
12046
12047 if (getenv("SWM_STARTED") == NULL)
12048 setenv("SWM_STARTED", "YES", 1);
12049
12050 /* setup all bars */
12051 num_screens = get_screen_count();
12052 for (i = 0; i < num_screens; i++)
12053 TAILQ_FOREACH(r, &screens[i].rl, entry)
12054 bar_setup(r);
12055
12056 /* Manage existing windows. */
12057 grab_windows();
12058
12059 grabkeys();
12060 grabbuttons();
12061
12062 /* Stack all regions to trigger mapping. */
12063 for (i = 0; i < num_screens; i++)
12064 TAILQ_FOREACH(r, &screens[i].rl, entry)
12065 stack(r);
12066
12067 xcb_ungrab_server(conn);
12068 xcb_flush(conn);
12069
12070 /* Update state and bar of each newly mapped workspace. */
12071 for (i = 0; i < num_screens; i++)
12072 TAILQ_FOREACH(r, &screens[i].rl, entry) {
12073 r->ws->state = SWM_WS_STATE_MAPPED;
12074 bar_draw(r->bar);
12075 }
12076
12077 memset(&pfd, 0, sizeof(pfd));
12078 pfd[0].fd = xfd;
12079 pfd[0].events = POLLIN;
12080 pfd[1].fd = STDIN_FILENO;
12081 pfd[1].events = POLLIN;
12082
12083 while (running) {
12084 while ((evt = get_next_event(false))) {
12085 if (!running)
12086 goto done;
12087 event_handle(evt);
12088 free(evt);
12089 }
12090
12091 /* If just (re)started, set default focus if needed. */
12092 if (startup) {
12093 startup = false;
12094
12095 if (focus_mode != SWM_FOCUS_FOLLOW) {
12096 r = TAILQ_FIRST(&screens[0].rl);
12097 if (r) {
12098 focus_region(r);
12099 focus_flush();
12100 }
12101 continue;
12102 }
12103 }
12104
12105 if (search_resp)
12106 search_do_resp();
12107
12108 num_readable = poll(pfd, bar_extra ? 2 : 1, 1000);
12109 if (num_readable == -1) {
12110 DNPRINTF(SWM_D_MISC, "poll failed: %s",
12111 strerror(errno));
12112 } else if (num_readable > 0 && bar_extra &&
12113 pfd[1].revents & POLLIN) {
12114 stdin_ready = true;
12115 }
12116
12117 if (restart_wm)
12118 restart(NULL, NULL, NULL);
12119
12120 if (!running)
12121 goto done;
12122
12123 if (stdin_ready) {
12124 stdin_ready = false;
12125 bar_extra_update();
12126 }
12127
12128 /* Need to ensure the bar(s) are always updated. */
12129 for (i = 0; i < num_screens; i++)
12130 TAILQ_FOREACH(r, &screens[i].rl, entry)
12131 bar_draw(r->bar);
12132
12133 xcb_flush(conn);
12134 }
12135 done:
12136 shutdown_cleanup();
12137
12138 return (0);
12139 }