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