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