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