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