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