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