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