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