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