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