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