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