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