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