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