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