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