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