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