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