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