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