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