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