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