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