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