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