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