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