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