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