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