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