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