]> code.delx.au - spectrwm/blob - spectrwm.c
Some cleanup.
[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 == NULL)
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 == NULL)
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 == NULL)
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 == NULL)
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 == NULL)
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 == NULL) {
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 == NULL) {
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 if (r) {
2646 if (r->type == a_state && r->format == 32 && r->length == 2)
2647 result = *((uint32_t *)xcb_get_property_value(r));
2648 free(r);
2649 }
2650
2651 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2652 result);
2653 return (result);
2654 }
2655
2656 void
2657 version(struct swm_region *r, union arg *args)
2658 {
2659 /* suppress unused warnings since vars are needed */
2660 (void)r;
2661 (void)args;
2662
2663 bar_version = !bar_version;
2664 if (bar_version)
2665 snprintf(bar_vertext, sizeof bar_vertext,
2666 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2667 else
2668 strlcpy(bar_vertext, "", sizeof bar_vertext);
2669
2670 bar_draw();
2671 xcb_flush(conn);
2672 }
2673
2674 void
2675 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2676 {
2677 xcb_client_message_event_t ev;
2678 #ifdef SWM_DEBUG
2679 char *name;
2680 #endif
2681
2682 if (win == NULL)
2683 return;
2684 #ifdef SWM_DEBUG
2685 name = get_atom_name(a);
2686 DNPRINTF(SWM_D_EVENT, "client_msg: window: 0x%x, atom: %s(%u), "
2687 "time: %#x\n",
2688 win->id, name, a, t);
2689 free(name);
2690 #endif
2691
2692 bzero(&ev, sizeof ev);
2693 ev.response_type = XCB_CLIENT_MESSAGE;
2694 ev.window = win->id;
2695 ev.type = a_prot;
2696 ev.format = 32;
2697 ev.data.data32[0] = a;
2698 ev.data.data32[1] = t;
2699
2700 xcb_send_event(conn, 0, win->id,
2701 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2702 }
2703
2704 /* synthetic response to a ConfigureRequest when not making a change */
2705 void
2706 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2707 {
2708 xcb_configure_notify_event_t ce;
2709
2710 if (win == NULL)
2711 return;
2712
2713 /* send notification of unchanged state. */
2714 bzero(&ce, sizeof(ce));
2715 ce.response_type = XCB_CONFIGURE_NOTIFY;
2716 ce.x = X(win);
2717 ce.y = Y(win);
2718 ce.width = WIDTH(win);
2719 ce.height = HEIGHT(win);
2720 ce.override_redirect = 0;
2721
2722 if (ev == NULL) {
2723 /* EWMH */
2724 ce.event = win->id;
2725 ce.window = win->id;
2726 ce.border_width = BORDER(win);
2727 ce.above_sibling = XCB_WINDOW_NONE;
2728 } else {
2729 /* normal */
2730 ce.event = ev->window;
2731 ce.window = ev->window;
2732
2733 /* make response appear more WM_SIZE_HINTS-compliant */
2734 if (win->sh.flags) {
2735 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2736 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2737 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2738 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2739 SH_INC_W(win), SH_INC_H(win));
2740 }
2741
2742 /* min size */
2743 if (SH_MIN(win)) {
2744 /* the hint may be set... to 0! */
2745 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2746 ce.width = SH_MIN_W(win);
2747 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2748 ce.height = SH_MIN_H(win);
2749 }
2750
2751 /* max size */
2752 if (SH_MAX(win)) {
2753 /* may also be advertized as 0 */
2754 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2755 ce.width = SH_MAX_W(win);
2756 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2757 ce.height = SH_MAX_H(win);
2758 }
2759
2760 /* resize increment. */
2761 if (SH_INC(win)) {
2762 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2763 ce.width -= (ce.width - SH_MIN_W(win)) %
2764 SH_INC_W(win);
2765 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2766 ce.height -= (ce.height - SH_MIN_H(win)) %
2767 SH_INC_H(win);
2768 }
2769
2770 /* adjust x and y for requested border_width. */
2771 ce.x += BORDER(win) - ev->border_width;
2772 ce.y += BORDER(win) - ev->border_width;
2773 ce.border_width = ev->border_width;
2774 ce.above_sibling = ev->sibling;
2775 }
2776
2777 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2778 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2779 ce.y, ce.width, ce.height, ce.border_width);
2780
2781 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2782 (char *)&ce);
2783 }
2784
2785 int
2786 count_win(struct workspace *ws, int count_transient)
2787 {
2788 struct ws_win *win;
2789 int count = 0;
2790
2791 TAILQ_FOREACH(win, &ws->winlist, entry) {
2792 if (!count_transient && win->floating)
2793 continue;
2794 if (!count_transient && win->transient)
2795 continue;
2796 if (win->iconic)
2797 continue;
2798 count++;
2799 }
2800 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2801
2802 return (count);
2803 }
2804
2805 void
2806 quit(struct swm_region *r, union arg *args)
2807 {
2808 /* suppress unused warnings since vars are needed */
2809 (void)r;
2810 (void)args;
2811
2812 DNPRINTF(SWM_D_MISC, "quit\n");
2813 running = 0;
2814 }
2815
2816 void
2817 map_window(struct ws_win *win, xcb_window_t sibling)
2818 {
2819 uint16_t mode = XCB_CONFIG_WINDOW_STACK_MODE;
2820 uint32_t val[2];
2821 int i = 0;
2822
2823 /* If sibling is specified, stack right above it. */
2824 if (sibling != XCB_WINDOW_NONE) {
2825 mode |= XCB_CONFIG_WINDOW_SIBLING;
2826 val[i++] = sibling;
2827 }
2828
2829 val[i] = XCB_STACK_MODE_ABOVE;
2830
2831 if (win == NULL)
2832 return;
2833
2834 DNPRINTF(SWM_D_EVENT, "map_window: win 0x%x, mapped: %s, "
2835 "sibling: 0x%x\n", win->id, YESNO(win->mapped), sibling);
2836
2837 xcb_configure_window(conn, win->id, mode, val);
2838
2839 if (win->mapped)
2840 return;
2841
2842 xcb_map_window(conn, win->id);
2843 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
2844 win->mapped = 1;
2845 }
2846
2847 void
2848 unmap_window(struct ws_win *win)
2849 {
2850 if (win == NULL)
2851 return;
2852
2853 DNPRINTF(SWM_D_EVENT, "unmap_window: win 0x%x, mapped: %s\n", win->id,
2854 YESNO(win->mapped));
2855
2856 if (!win->mapped)
2857 return;
2858
2859 xcb_unmap_window(conn, win->id);
2860 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
2861 win->mapped = 0;
2862 }
2863
2864 void
2865 unmap_all(void)
2866 {
2867 struct ws_win *win;
2868 int i, j, num_screens;
2869
2870 num_screens = get_screen_count();
2871 for (i = 0; i < num_screens; i++)
2872 for (j = 0; j < workspace_limit; j++)
2873 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2874 unmap_window(win);
2875 }
2876
2877 void
2878 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
2879 {
2880 xcb_key_press_event_t event;
2881 xcb_keycode_t *keycode;
2882
2883 if (win == NULL)
2884 return;
2885
2886 keycode = xcb_key_symbols_get_keycode(syms, keysym);
2887
2888 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
2889 win->id, *keycode);
2890
2891 bzero(&event, sizeof(event));
2892 event.event = win->id;
2893 event.root = win->s->root;
2894 event.child = XCB_WINDOW_NONE;
2895 event.time = XCB_CURRENT_TIME;
2896 event.event_x = X(win);
2897 event.event_y = Y(win);
2898 event.root_x = 1;
2899 event.root_y = 1;
2900 event.same_screen = 1;
2901 event.detail = *keycode;
2902 event.state = modifiers;
2903
2904 event.response_type = XCB_KEY_PRESS;
2905 xcb_send_event(conn, 1, win->id,
2906 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
2907
2908 event.response_type = XCB_KEY_RELEASE;
2909 xcb_send_event(conn, 1, win->id,
2910 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
2911
2912 free(keycode);
2913 }
2914
2915 void
2916 restart(struct swm_region *r, union arg *args)
2917 {
2918 /* suppress unused warning since var is needed */
2919 (void)r;
2920 (void)args;
2921
2922 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2923
2924 shutdown_cleanup();
2925
2926 execvp(start_argv[0], start_argv);
2927 warn("execvp failed");
2928 quit(NULL, NULL);
2929 }
2930
2931 struct ws_win *
2932 get_pointer_win(xcb_window_t root)
2933 {
2934 struct ws_win *win = NULL;
2935 xcb_query_pointer_reply_t *r;
2936
2937 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: 0x%x.\n", root);
2938
2939 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
2940 if (r) {
2941 win = find_window(r->child);
2942 if (win) {
2943 DNPRINTF(SWM_D_EVENT, "get_pointer_win: 0x%x.\n",
2944 win->id);
2945 } else {
2946 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
2947 }
2948 free(r);
2949 }
2950
2951 return win;
2952 }
2953
2954 struct swm_region *
2955 root_to_region(xcb_window_t root, int check)
2956 {
2957 struct ws_win *cfw;
2958 struct swm_region *r = NULL;
2959 int i, num_screens;
2960 xcb_query_pointer_reply_t *qpr;
2961 xcb_get_input_focus_reply_t *gifr;
2962
2963 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
2964
2965 num_screens = get_screen_count();
2966 for (i = 0; i < num_screens; i++)
2967 if (screens[i].root == root)
2968 break;
2969
2970 if (check & SWM_CK_REGION)
2971 r = screens[i].r_focus;
2972
2973 if (r == NULL && check & SWM_CK_FOCUS) {
2974 /* Try to find an actively focused window */
2975 gifr = xcb_get_input_focus_reply(conn,
2976 xcb_get_input_focus(conn), NULL);
2977 if (gifr) {
2978 cfw = find_window(gifr->focus);
2979 if (cfw && cfw->ws->r)
2980 r = cfw->ws->r;
2981
2982 free(gifr);
2983 }
2984 }
2985
2986 if (r == NULL && check & SWM_CK_POINTER) {
2987 /* No region with an active focus; try to use pointer. */
2988 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
2989 screens[i].root), NULL);
2990 if (qpr) {
2991 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
2992 "(%d,%d)\n", qpr->root_x, qpr->root_y);
2993 TAILQ_FOREACH(r, &screens[i].rl, entry)
2994 if (X(r) <= qpr->root_x &&
2995 qpr->root_x < MAX_X(r) &&
2996 Y(r) <= qpr->root_y &&
2997 qpr->root_y < MAX_Y(r))
2998 break;
2999 free(qpr);
3000 }
3001 }
3002
3003 /* Last resort. */
3004 if (r == NULL && check & SWM_CK_FALLBACK)
3005 r = TAILQ_FIRST(&screens[i].rl);
3006
3007 DNPRINTF(SWM_D_MISC, "root_to_region: idx: %d\n", get_region_index(r));
3008
3009 return (r);
3010 }
3011
3012 struct ws_win *
3013 find_unmanaged_window(xcb_window_t id)
3014 {
3015 struct ws_win *win;
3016 int i, j, num_screens;
3017
3018 num_screens = get_screen_count();
3019 for (i = 0; i < num_screens; i++)
3020 for (j = 0; j < workspace_limit; j++)
3021 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
3022 entry)
3023 if (id == win->id)
3024 return (win);
3025 return (NULL);
3026 }
3027
3028 struct ws_win *
3029 find_window(xcb_window_t id)
3030 {
3031 struct ws_win *win;
3032 int i, j, num_screens;
3033 xcb_query_tree_reply_t *r;
3034
3035 num_screens = get_screen_count();
3036 for (i = 0; i < num_screens; i++)
3037 for (j = 0; j < workspace_limit; j++)
3038 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3039 if (id == win->id)
3040 return (win);
3041
3042 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
3043 if (r == NULL)
3044 return (NULL);
3045
3046 /* if we were looking for the parent return that window instead */
3047 if (r->parent == 0 || r->root == r->parent) {
3048 free(r);
3049 return (NULL);
3050 }
3051
3052 /* look for parent */
3053 for (i = 0; i < num_screens; i++)
3054 for (j = 0; j < workspace_limit; j++)
3055 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3056 if (r->parent == win->id) {
3057 free(r);
3058 return (win);
3059 }
3060
3061 free(r);
3062 return (NULL);
3063 }
3064
3065 void
3066 spawn(int ws_idx, union arg *args, int close_fd)
3067 {
3068 int fd;
3069 char *ret = NULL;
3070
3071 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
3072
3073 close(xcb_get_file_descriptor(conn));
3074
3075 setenv("LD_PRELOAD", SWM_LIB, 1);
3076
3077 if (asprintf(&ret, "%d", ws_idx) == -1) {
3078 warn("spawn: asprintf SWM_WS");
3079 _exit(1);
3080 }
3081 setenv("_SWM_WS", ret, 1);
3082 free(ret);
3083 ret = NULL;
3084
3085 if (asprintf(&ret, "%d", getpid()) == -1) {
3086 warn("spawn: asprintf _SWM_PID");
3087 _exit(1);
3088 }
3089 setenv("_SWM_PID", ret, 1);
3090 free(ret);
3091 ret = NULL;
3092
3093 if (setsid() == -1) {
3094 warn("spawn: setsid");
3095 _exit(1);
3096 }
3097
3098 if (close_fd) {
3099 /*
3100 * close stdin and stdout to prevent interaction between apps
3101 * and the baraction script
3102 * leave stderr open to record errors
3103 */
3104 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
3105 warn("spawn: open");
3106 _exit(1);
3107 }
3108 dup2(fd, STDIN_FILENO);
3109 dup2(fd, STDOUT_FILENO);
3110 if (fd > 2)
3111 close(fd);
3112 }
3113
3114 execvp(args->argv[0], args->argv);
3115
3116 warn("spawn: execvp");
3117 _exit(1);
3118 }
3119
3120 void
3121 kill_refs(struct ws_win *win)
3122 {
3123 int i, x, num_screens;
3124 struct swm_region *r;
3125 struct workspace *ws;
3126
3127 if (win == NULL)
3128 return;
3129
3130 num_screens = get_screen_count();
3131 for (i = 0; i < num_screens; i++)
3132 TAILQ_FOREACH(r, &screens[i].rl, entry)
3133 for (x = 0; x < workspace_limit; x++) {
3134 ws = &r->s->ws[x];
3135 if (win == ws->focus)
3136 ws->focus = NULL;
3137 if (win == ws->focus_prev)
3138 ws->focus_prev = NULL;
3139 }
3140 }
3141
3142 int
3143 validate_win(struct ws_win *testwin)
3144 {
3145 struct ws_win *win;
3146 struct workspace *ws;
3147 struct swm_region *r;
3148 int i, x, num_screens;
3149
3150 if (testwin == NULL)
3151 return (0);
3152
3153 num_screens = get_screen_count();
3154 for (i = 0; i < num_screens; i++)
3155 TAILQ_FOREACH(r, &screens[i].rl, entry)
3156 for (x = 0; x < workspace_limit; x++) {
3157 ws = &r->s->ws[x];
3158 TAILQ_FOREACH(win, &ws->winlist, entry)
3159 if (win == testwin)
3160 return (0);
3161 }
3162 return (1);
3163 }
3164
3165 int
3166 validate_ws(struct workspace *testws)
3167 {
3168 struct swm_region *r;
3169 struct workspace *ws;
3170 int i, x, num_screens;
3171
3172 /* validate all ws */
3173 num_screens = get_screen_count();
3174 for (i = 0; i < num_screens; i++)
3175 TAILQ_FOREACH(r, &screens[i].rl, entry)
3176 for (x = 0; x < workspace_limit; x++) {
3177 ws = &r->s->ws[x];
3178 if (ws == testws)
3179 return (0);
3180 }
3181 return (1);
3182 }
3183
3184 xcb_window_t
3185 get_sibling(struct ws_win *win, int mode)
3186 {
3187 struct ws_win *w = win;
3188
3189 switch (mode) {
3190 case SWM_STACK_TOP:
3191 TAILQ_FOREACH_REVERSE(w, &w->ws->winlist, ws_win_list, entry)
3192 if (w != win && !w->floating && !w->iconic)
3193 break;
3194 break;
3195 case SWM_STACK_ABOVE:
3196 do {
3197 w = TAILQ_NEXT(w, entry);
3198 } while (w != NULL && (w == win || w->floating || w->iconic));
3199 break;
3200 case SWM_STACK_BELOW:
3201 do {
3202 w = TAILQ_PREV(w, ws_win_list, entry);
3203 } while (w != NULL && (w == win || w->floating || w->iconic));
3204 break;
3205 case SWM_STACK_BOTTOM:
3206 TAILQ_FOREACH(w, &w->ws->winlist, entry)
3207 if (w != win && !w->floating && !w->iconic)
3208 break;
3209 break;
3210 default:
3211 w = NULL;
3212 }
3213
3214 if (w == NULL)
3215 return (win->ws->r->id);
3216 else
3217 return (w->id);
3218 }
3219
3220 void
3221 unfocus_win(struct ws_win *win)
3222 {
3223 xcb_window_t none = XCB_WINDOW_NONE;
3224
3225 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
3226
3227 if (win == NULL)
3228 return;
3229 if (win->ws == NULL) {
3230 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3231 return;
3232 }
3233
3234 if (validate_ws(win->ws)) {
3235 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3236 return;
3237 }
3238
3239 if (win->ws->r == NULL) {
3240 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3241 return;
3242 }
3243
3244 if (validate_win(win)) {
3245 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3246 kill_refs(win);
3247 return;
3248 }
3249
3250 if (win->ws->focus == win) {
3251 if (tile_gap < 0 && !win->floating)
3252 map_window(win, get_sibling(win, SWM_STACK_BELOW));
3253 win->ws->focus = NULL;
3254 win->ws->focus_prev = win;
3255 }
3256
3257 if (validate_win(win->ws->focus)) {
3258 kill_refs(win->ws->focus);
3259 win->ws->focus = NULL;
3260 }
3261
3262 if (validate_win(win->ws->focus_prev)) {
3263 kill_refs(win->ws->focus_prev);
3264 win->ws->focus_prev = NULL;
3265 }
3266
3267 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3268 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3269
3270 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3271 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3272
3273 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3274 }
3275
3276 void
3277 focus_win(struct ws_win *win)
3278 {
3279 struct ws_win *cfw = NULL, *parent = NULL, *w;
3280 struct workspace *ws;
3281 xcb_get_input_focus_reply_t *r;
3282
3283 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
3284
3285 if (win == NULL)
3286 goto out;
3287
3288 if (win->ws == NULL)
3289 goto out;
3290
3291 if (!win->mapped)
3292 goto out;
3293
3294 ws = win->ws;
3295
3296 if (validate_ws(ws))
3297 goto out;
3298
3299 if (validate_win(win)) {
3300 kill_refs(win);
3301 goto out;
3302 }
3303
3304 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3305 if (r) {
3306 cfw = find_window(r->focus);
3307 if (cfw != win)
3308 unfocus_win(cfw);
3309 free(r);
3310 }
3311
3312 if (ws->focus != win) {
3313 if (ws->focus && ws->focus != cfw)
3314 unfocus_win(ws->focus);
3315 ws->focus = win;
3316 }
3317
3318 /* If this window directs focus to a child window, then clear. */
3319 if (win->focus_child)
3320 win->focus_child = NULL;
3321
3322 /* If transient, adjust parent's focus child for focus_magic. */
3323 if (win->transient) {
3324 parent = find_window(win->transient);
3325 if (parent && parent->focus_child != win)
3326 parent->focus_child = win;
3327 }
3328
3329 if (cfw != win && ws->r != NULL) {
3330 /* Set input focus if no input hint, or indicated by hint. */
3331 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
3332 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
3333 win->hints.input))
3334 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3335 win->id, last_event_time);
3336
3337 /* Tell app it can adjust focus to a specific window. */
3338 if (win->take_focus) {
3339 /* java is special; always tell parent */
3340 if (win->transient && win->java)
3341 client_msg(parent, a_takefocus,
3342 last_event_time);
3343 else
3344 client_msg(win, a_takefocus, last_event_time);
3345 }
3346
3347 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3348 &ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
3349
3350 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
3351 ws->always_raise) {
3352 /* If a parent exists, map it first. */
3353 if (parent) {
3354 map_window(parent, XCB_WINDOW_NONE);
3355
3356 /* Map siblings next. */
3357 TAILQ_FOREACH(w, &ws->winlist, entry)
3358 if (w != win && !w->iconic &&
3359 w->transient == parent->id)
3360 map_window(w, XCB_WINDOW_NONE);
3361 }
3362
3363 /* Map focused window. */
3364 map_window(win, XCB_WINDOW_NONE);
3365
3366 /* Finally, map children of focus window. */
3367 TAILQ_FOREACH(w, &ws->winlist, entry)
3368 if (w->transient == win->id && !w->iconic)
3369 map_window(w, XCB_WINDOW_NONE);
3370 } else if (tile_gap < 0 && !win->floating) {
3371 /*
3372 * Windows overlap in the layout.
3373 * Raise focused win above all tiled wins.
3374 */
3375 if (tile_gap < 0 && !win->floating)
3376 map_window(win,
3377 get_sibling(win, SWM_STACK_TOP));
3378 }
3379
3380 set_region(ws->r);
3381
3382 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3383 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3384 &win->id);
3385 }
3386
3387 out:
3388 bar_draw();
3389
3390 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
3391 }
3392
3393 /* If a child window should have focus instead, return it. */
3394 struct ws_win *
3395 get_focus_magic(struct ws_win *win)
3396 {
3397 struct ws_win *parent = NULL;
3398 struct ws_win *child = NULL;
3399
3400 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: window: 0x%x\n", WINID(win));
3401 if (win == NULL)
3402 return win;
3403
3404 if (win->transient) {
3405 parent = find_window(win->transient);
3406
3407 /* If parent prefers focus elsewhere, then try to do so. */
3408 if (parent && (child = parent->focus_child)) {
3409 if (validate_win(child) == 0 && child->mapped)
3410 win = child;
3411 else
3412 parent->focus_child = NULL;
3413 }
3414 }
3415
3416 /* If this window prefers focus elsewhere, then try to do so. */
3417 if ((child = win->focus_child)) {
3418 if (validate_win(child) == 0 && child->mapped)
3419 win = child;
3420 else
3421 win->focus_child = NULL;
3422 }
3423
3424 return win;
3425 }
3426
3427 void
3428 event_drain(uint8_t rt)
3429 {
3430 xcb_generic_event_t *evt;
3431
3432 /* ensure all pending requests have been processed before filtering. */
3433 xcb_aux_sync(conn);
3434 while ((evt = xcb_poll_for_event(conn))) {
3435 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
3436 event_handle(evt);
3437
3438 free(evt);
3439 }
3440 }
3441
3442 void
3443 set_region(struct swm_region *r)
3444 {
3445 struct swm_region *rf;
3446
3447 if (r == NULL)
3448 return;
3449
3450 rf = r->s->r_focus;
3451 /* Unfocus old region bar. */
3452 if (rf) {
3453 if (rf == r)
3454 return;
3455
3456 xcb_change_window_attributes(conn, rf->bar->id,
3457 XCB_CW_BORDER_PIXEL,
3458 &r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel);
3459 }
3460
3461 /* Set region bar border to focus_color. */
3462 xcb_change_window_attributes(conn, r->bar->id,
3463 XCB_CW_BORDER_PIXEL, &r->s->c[SWM_S_COLOR_BAR_BORDER].pixel);
3464
3465 r->s->r_focus = r;
3466 }
3467
3468 void
3469 focus_region(struct swm_region *r)
3470 {
3471 struct ws_win *nfw;
3472 struct swm_region *old_r;
3473
3474 if (r == NULL)
3475 return;
3476
3477 old_r = r->s->r_focus;
3478 set_region(r);
3479
3480 nfw = get_region_focus(r);
3481 if (nfw) {
3482 focus_win(nfw);
3483 } else {
3484 /* New region is empty; need to manually unfocus win. */
3485 if (old_r)
3486 unfocus_win(old_r->ws->focus);
3487
3488 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3489 XCB_CURRENT_TIME);
3490
3491 /* Clear bar since empty. */
3492 bar_draw();
3493 }
3494 }
3495
3496 void
3497 switchws(struct swm_region *r, union arg *args)
3498 {
3499 struct swm_region *this_r, *other_r;
3500 struct ws_win *win;
3501 struct workspace *new_ws, *old_ws;
3502 xcb_window_t none = XCB_WINDOW_NONE;
3503 int wsid = args->id, unmap_old = 0;
3504
3505 if (!(r && r->s))
3506 return;
3507
3508 if (wsid >= workspace_limit)
3509 return;
3510
3511 this_r = r;
3512 old_ws = this_r->ws;
3513 new_ws = &this_r->s->ws[wsid];
3514
3515 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3516 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
3517
3518 if (new_ws == NULL || old_ws == NULL)
3519 return;
3520 if (new_ws == old_ws)
3521 return;
3522
3523 if ((win = old_ws->focus) != NULL) {
3524 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3525 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3526
3527 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3528 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3529 &none);
3530 }
3531
3532 other_r = new_ws->r;
3533 if (other_r == NULL) {
3534 /* the other workspace is hidden, hide this one */
3535 old_ws->r = NULL;
3536 unmap_old = 1;
3537 } else {
3538 /* the other ws is visible in another region, exchange them */
3539 other_r->ws_prior = new_ws;
3540 other_r->ws = old_ws;
3541 old_ws->r = other_r;
3542 }
3543 this_r->ws_prior = old_ws;
3544 this_r->ws = new_ws;
3545 new_ws->r = this_r;
3546
3547 /* Set focus_pending before stacking, if needed. */
3548 if (focus_mode != SWM_FOCUS_FOLLOW && (!new_ws->focus_pending ||
3549 validate_win(new_ws->focus_pending))) {
3550 new_ws->focus_pending = get_region_focus(new_ws->r);
3551 new_ws->focus = new_ws->focus_prev;
3552 new_ws->focus_prev = NULL;
3553 }
3554
3555 stack();
3556
3557 /* unmap old windows */
3558 if (unmap_old)
3559 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3560 unmap_window(win);
3561
3562 /* if workspaces were swapped, then don't wait to set focus */
3563 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3564 if (new_ws->focus_pending) {
3565 focus_win(new_ws->focus_pending);
3566 new_ws->focus_pending = NULL;
3567 }
3568 }
3569
3570 /* Clear bar and set focus on region input win if new ws is empty. */
3571 if (new_ws->focus_pending == NULL && new_ws->focus == NULL) {
3572 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3573 XCB_CURRENT_TIME);
3574 bar_draw();
3575 }
3576
3577 focus_flush();
3578
3579 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3580 }
3581
3582 void
3583 cyclews(struct swm_region *r, union arg *args)
3584 {
3585 union arg a;
3586 struct swm_screen *s = r->s;
3587 int cycle_all = 0;
3588 int move = 0;
3589
3590 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3591 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3592
3593 a.id = r->ws->idx;
3594
3595 do {
3596 switch (args->id) {
3597 case SWM_ARG_ID_CYCLEWS_MOVE_UP:
3598 move = 1;
3599 /* FALLTHROUGH */
3600 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3601 cycle_all = 1;
3602 /* FALLTHROUGH */
3603 case SWM_ARG_ID_CYCLEWS_UP:
3604 a.id = (a.id < workspace_limit - 1) ? a.id + 1 : 0;
3605 break;
3606 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN:
3607 move = 1;
3608 /* FALLTHROUGH */
3609 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3610 cycle_all = 1;
3611 /* FALLTHROUGH */
3612 case SWM_ARG_ID_CYCLEWS_DOWN:
3613 a.id = (a.id > 0) ? a.id - 1 : workspace_limit - 1;
3614 break;
3615 default:
3616 return;
3617 };
3618
3619 if (!cycle_all &&
3620 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3621 continue;
3622 if (!cycle_visible && s->ws[a.id].r != NULL)
3623 continue;
3624
3625 if (move)
3626 send_to_ws(r, &a);
3627
3628 switchws(r, &a);
3629 } while (a.id != r->ws->idx);
3630 }
3631
3632 void
3633 priorws(struct swm_region *r, union arg *args)
3634 {
3635 union arg a;
3636
3637 (void)args;
3638
3639 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3640 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3641
3642 if (r->ws_prior == NULL)
3643 return;
3644
3645 a.id = r->ws_prior->idx;
3646 switchws(r, &a);
3647 }
3648
3649 void
3650 focusrg(struct swm_region *r, union arg *args)
3651 {
3652 int ridx = args->id, i, num_screens;
3653 struct swm_region *rr = NULL;
3654
3655 num_screens = get_screen_count();
3656 /* do nothing if we don't have more than one screen */
3657 if (!(num_screens > 1 || outputs > 1))
3658 return;
3659
3660 DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
3661
3662 rr = TAILQ_FIRST(&r->s->rl);
3663 for (i = 0; i < ridx && rr != NULL; ++i)
3664 rr = TAILQ_NEXT(rr, entry);
3665
3666 if (rr == NULL)
3667 return;
3668
3669 focus_region(rr);
3670 focus_flush();
3671 }
3672
3673 void
3674 cyclerg(struct swm_region *r, union arg *args)
3675 {
3676 struct swm_region *rr = NULL;
3677 int i, num_screens;
3678
3679 num_screens = get_screen_count();
3680 /* do nothing if we don't have more than one screen */
3681 if (!(num_screens > 1 || outputs > 1))
3682 return;
3683
3684 i = r->s->idx;
3685 switch (args->id) {
3686 case SWM_ARG_ID_CYCLERG_UP:
3687 rr = TAILQ_NEXT(r, entry);
3688 if (rr == NULL)
3689 rr = TAILQ_FIRST(&screens[i].rl);
3690 break;
3691 case SWM_ARG_ID_CYCLERG_DOWN:
3692 rr = TAILQ_PREV(r, swm_region_list, entry);
3693 if (rr == NULL)
3694 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3695 break;
3696 default:
3697 return;
3698 };
3699 if (rr == NULL)
3700 return;
3701
3702 focus_region(rr);
3703 focus_flush();
3704 }
3705
3706 void
3707 sort_windows(struct ws_win_list *wl)
3708 {
3709 struct ws_win *win, *parent, *nxt;
3710
3711 if (wl == NULL)
3712 return;
3713
3714 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3715 nxt = TAILQ_NEXT(win, entry);
3716 if (win->transient) {
3717 parent = find_window(win->transient);
3718 if (parent == NULL) {
3719 warnx("not possible bug");
3720 continue;
3721 }
3722 TAILQ_REMOVE(wl, win, entry);
3723 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3724 }
3725 }
3726
3727 }
3728
3729 void
3730 swapwin(struct swm_region *r, union arg *args)
3731 {
3732 struct ws_win *target, *source;
3733 struct ws_win *cur_focus;
3734 struct ws_win_list *wl;
3735
3736 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3737 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3738
3739 cur_focus = r->ws->focus;
3740 if (cur_focus == NULL)
3741 return;
3742
3743 source = cur_focus;
3744 wl = &source->ws->winlist;
3745
3746 switch (args->id) {
3747 case SWM_ARG_ID_SWAPPREV:
3748 if (source->transient)
3749 source = find_window(source->transient);
3750 target = TAILQ_PREV(source, ws_win_list, entry);
3751 if (target && target->transient)
3752 target = find_window(target->transient);
3753 TAILQ_REMOVE(wl, source, entry);
3754 if (target == NULL)
3755 TAILQ_INSERT_TAIL(wl, source, entry);
3756 else
3757 TAILQ_INSERT_BEFORE(target, source, entry);
3758 break;
3759 case SWM_ARG_ID_SWAPNEXT:
3760 target = TAILQ_NEXT(source, entry);
3761 /* move the parent and let the sort handle the move */
3762 if (source->transient)
3763 source = find_window(source->transient);
3764 TAILQ_REMOVE(wl, source, entry);
3765 if (target == NULL)
3766 TAILQ_INSERT_HEAD(wl, source, entry);
3767 else
3768 TAILQ_INSERT_AFTER(wl, target, source, entry);
3769 break;
3770 case SWM_ARG_ID_SWAPMAIN:
3771 target = TAILQ_FIRST(wl);
3772 if (target == source) {
3773 if (source->ws->focus_prev != NULL &&
3774 source->ws->focus_prev != target)
3775 source = source->ws->focus_prev;
3776 else
3777 return;
3778 }
3779 if (target == NULL || source == NULL)
3780 return;
3781 source->ws->focus_prev = target;
3782 TAILQ_REMOVE(wl, target, entry);
3783 TAILQ_INSERT_BEFORE(source, target, entry);
3784 TAILQ_REMOVE(wl, source, entry);
3785 TAILQ_INSERT_HEAD(wl, source, entry);
3786 break;
3787 case SWM_ARG_ID_MOVELAST:
3788 TAILQ_REMOVE(wl, source, entry);
3789 TAILQ_INSERT_TAIL(wl, source, entry);
3790 break;
3791 default:
3792 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
3793 return;
3794 }
3795
3796 sort_windows(wl);
3797
3798 stack();
3799
3800 focus_flush();
3801 }
3802
3803 struct ws_win *
3804 get_focus_prev(struct ws_win *win)
3805 {
3806 struct ws_win *winfocus = NULL;
3807 struct ws_win *cur_focus = NULL;
3808 struct ws_win_list *wl = NULL;
3809 struct workspace *ws = NULL;
3810
3811 if (!(win && win->ws))
3812 return NULL;
3813
3814 ws = win->ws;
3815 wl = &ws->winlist;
3816 cur_focus = ws->focus;
3817
3818 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3819 WINID(win), WINID(cur_focus));
3820
3821 /* pickle, just focus on whatever */
3822 if (cur_focus == NULL) {
3823 /* use prev_focus if valid */
3824 if (ws->focus_prev && ws->focus_prev != cur_focus &&
3825 find_window(WINID(ws->focus_prev)))
3826 winfocus = ws->focus_prev;
3827 goto done;
3828 }
3829
3830 /* if transient focus on parent */
3831 if (cur_focus->transient) {
3832 winfocus = find_window(cur_focus->transient);
3833 goto done;
3834 }
3835
3836 /* if in max_stack try harder */
3837 if ((win->quirks & SWM_Q_FOCUSPREV) ||
3838 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
3839 if (cur_focus != ws->focus_prev)
3840 winfocus = ws->focus_prev;
3841 else
3842 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3843 if (winfocus)
3844 goto done;
3845 }
3846
3847 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
3848
3849 if (winfocus == NULL || winfocus == win) {
3850 switch (focus_close) {
3851 case SWM_STACK_BOTTOM:
3852 TAILQ_FOREACH(winfocus, wl, entry)
3853 if (!winfocus->iconic && winfocus != cur_focus)
3854 break;
3855 break;
3856 case SWM_STACK_TOP:
3857 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3858 if (!winfocus->iconic && winfocus != cur_focus)
3859 break;
3860 break;
3861 case SWM_STACK_ABOVE:
3862 winfocus = TAILQ_NEXT(cur_focus, entry);
3863 while (winfocus && winfocus->iconic)
3864 winfocus = TAILQ_NEXT(winfocus, entry);
3865
3866 if (winfocus == NULL) {
3867 if (focus_close_wrap) {
3868 TAILQ_FOREACH(winfocus, wl, entry)
3869 if (!winfocus->iconic &&
3870 winfocus != cur_focus)
3871 break;
3872 } else {
3873 TAILQ_FOREACH_REVERSE(winfocus, wl,
3874 ws_win_list, entry)
3875 if (!winfocus->iconic &&
3876 winfocus != cur_focus)
3877 break;
3878 }
3879 }
3880 break;
3881 case SWM_STACK_BELOW:
3882 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
3883 while (winfocus && winfocus->iconic)
3884 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3885 entry);
3886
3887 if (winfocus == NULL) {
3888 if (focus_close_wrap) {
3889 TAILQ_FOREACH_REVERSE(winfocus, wl,
3890 ws_win_list, entry)
3891 if (!winfocus->iconic &&
3892 winfocus != cur_focus)
3893 break;
3894 } else {
3895 TAILQ_FOREACH(winfocus, wl, entry)
3896 if (!winfocus->iconic &&
3897 winfocus != cur_focus)
3898 break;
3899 }
3900 }
3901 break;
3902 }
3903 }
3904 done:
3905 if (winfocus == NULL ||
3906 (winfocus && (winfocus->iconic || winfocus == cur_focus))) {
3907 if (focus_default == SWM_STACK_TOP) {
3908 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3909 if (!winfocus->iconic && winfocus != cur_focus)
3910 break;
3911 } else {
3912 TAILQ_FOREACH(winfocus, wl, entry)
3913 if (!winfocus->iconic && winfocus != cur_focus)
3914 break;
3915 }
3916 }
3917
3918 kill_refs(win);
3919
3920 return get_focus_magic(winfocus);
3921 }
3922
3923 struct ws_win *
3924 get_region_focus(struct swm_region *r)
3925 {
3926 struct ws_win *winfocus = NULL;
3927
3928 if (!(r && r->ws))
3929 return NULL;
3930
3931 if (r->ws->focus && !r->ws->focus->iconic)
3932 winfocus = r->ws->focus;
3933 else if (r->ws->focus_prev && !r->ws->focus_prev->iconic)
3934 winfocus = r->ws->focus_prev;
3935 else
3936 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3937 if (!winfocus->iconic)
3938 break;
3939
3940 return get_focus_magic(winfocus);
3941 }
3942
3943 void
3944 focus(struct swm_region *r, union arg *args)
3945 {
3946 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
3947 struct ws_win_list *wl = NULL;
3948 struct workspace *ws = NULL;
3949 union arg a;
3950 int i;
3951 xcb_icccm_wm_hints_t hints;
3952
3953 if (!(r && r->ws))
3954 goto out;
3955
3956 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3957
3958 cur_focus = r->ws->focus;
3959 ws = r->ws;
3960 wl = &ws->winlist;
3961
3962 /* Make sure an uniconified window has focus, if one exists. */
3963 if (cur_focus == NULL) {
3964 cur_focus = TAILQ_FIRST(wl);
3965 while (cur_focus != NULL && cur_focus->iconic)
3966 cur_focus = TAILQ_NEXT(cur_focus, entry);
3967 }
3968
3969 switch (args->id) {
3970 case SWM_ARG_ID_FOCUSPREV:
3971 if (cur_focus == NULL)
3972 goto out;
3973
3974 winfocus = cur_focus;
3975 do {
3976 winfocus = TAILQ_PREV(winfocus, ws_win_list, entry);
3977 if (winfocus == NULL)
3978 winfocus = TAILQ_LAST(wl, ws_win_list);
3979 if (winfocus == cur_focus)
3980 break;
3981 } while (winfocus != NULL &&
3982 (winfocus->iconic || winfocus->id == cur_focus->transient));
3983 break;
3984 case SWM_ARG_ID_FOCUSNEXT:
3985 if (cur_focus == NULL)
3986 goto out;
3987
3988 winfocus = cur_focus;
3989 do {
3990 winfocus = TAILQ_NEXT(winfocus, entry);
3991 if (winfocus == NULL)
3992 winfocus = TAILQ_FIRST(wl);
3993 if (winfocus == cur_focus)
3994 break;
3995 } while (winfocus != NULL &&
3996 (winfocus->iconic || winfocus->id == cur_focus->transient));
3997 break;
3998 case SWM_ARG_ID_FOCUSMAIN:
3999 if (cur_focus == NULL)
4000 goto out;
4001
4002 winfocus = TAILQ_FIRST(wl);
4003 if (winfocus == cur_focus)
4004 winfocus = cur_focus->ws->focus_prev;
4005 break;
4006 case SWM_ARG_ID_FOCUSURGENT:
4007 /* Search forward for the next urgent window. */
4008 winfocus = NULL;
4009 head = cur_focus;
4010
4011 for (i = 0; i <= workspace_limit; ++i) {
4012 if (head == NULL)
4013 head = TAILQ_FIRST(&r->s->ws[(ws->idx + i) %
4014 workspace_limit].winlist);
4015
4016 while (head != NULL &&
4017 (head = TAILQ_NEXT(head, entry)) != NULL) {
4018 if (head == cur_focus) {
4019 winfocus = cur_focus;
4020 break;
4021 }
4022 if (xcb_icccm_get_wm_hints_reply(conn,
4023 xcb_icccm_get_wm_hints(conn, head->id),
4024 &hints, NULL) != 0 &&
4025 xcb_icccm_wm_hints_get_urgency(&hints)) {
4026 winfocus = head;
4027 break;
4028 }
4029 }
4030
4031 if (winfocus != NULL)
4032 break;
4033 }
4034
4035 /* Switch ws if new focus is on a different ws. */
4036 if (winfocus != NULL && winfocus->ws != ws) {
4037 a.id = winfocus->ws->idx;
4038 switchws(r, &a);
4039 }
4040 break;
4041 default:
4042 goto out;
4043 }
4044
4045 focus_win(get_focus_magic(winfocus));
4046 focus_flush();
4047
4048 out:
4049 DNPRINTF(SWM_D_FOCUS, "focus: done\n");
4050 }
4051
4052 void
4053 cycle_layout(struct swm_region *r, union arg *args)
4054 {
4055 struct workspace *ws = r->ws;
4056
4057 /* suppress unused warning since var is needed */
4058 (void)args;
4059
4060 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
4061
4062 ws->cur_layout++;
4063 if (ws->cur_layout->l_stack == NULL)
4064 ws->cur_layout = &layouts[0];
4065
4066 stack();
4067 bar_draw();
4068
4069 focus_win(get_region_focus(r));
4070
4071 focus_flush();
4072 }
4073
4074 void
4075 stack_config(struct swm_region *r, union arg *args)
4076 {
4077 struct workspace *ws = r->ws;
4078
4079 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
4080 args->id, ws->idx);
4081
4082 if (ws->cur_layout->l_config != NULL)
4083 ws->cur_layout->l_config(ws, args->id);
4084
4085 if (args->id != SWM_ARG_ID_STACKINIT)
4086 stack();
4087 bar_draw();
4088
4089 focus_flush();
4090 }
4091
4092 void
4093 stack(void) {
4094 struct swm_geometry g;
4095 struct swm_region *r;
4096 int i, num_screens;
4097 #ifdef SWM_DEBUG
4098 int j;
4099 #endif
4100
4101 DNPRINTF(SWM_D_STACK, "stack: begin\n");
4102
4103 num_screens = get_screen_count();
4104 for (i = 0; i < num_screens; i++) {
4105 #ifdef SWM_DEBUG
4106 j = 0;
4107 #endif
4108 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4109 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
4110 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
4111
4112 /* Adjust stack area for region bar and padding. */
4113 g = r->g;
4114 g.x += region_padding;
4115 g.y += region_padding;
4116 g.w -= 2 * border_width + 2 * region_padding;
4117 g.h -= 2 * border_width + 2 * region_padding;
4118 if (bar_enabled && r->ws->bar_enabled) {
4119 if (!bar_at_bottom)
4120 g.y += bar_height;
4121 g.h -= bar_height;
4122 }
4123 r->ws->cur_layout->l_stack(r->ws, &g);
4124 r->ws->cur_layout->l_string(r->ws);
4125 /* save r so we can track region changes */
4126 r->ws->old_r = r;
4127 }
4128 }
4129 if (font_adjusted)
4130 font_adjusted--;
4131
4132 DNPRINTF(SWM_D_STACK, "stack: end\n");
4133 }
4134
4135 void
4136 store_float_geom(struct ws_win *win)
4137 {
4138 if (win == NULL || win->ws->r == NULL)
4139 return;
4140
4141 /* retain window geom and region geom */
4142 win->g_float = win->g;
4143 win->g_float.x -= X(win->ws->r);
4144 win->g_float.y -= Y(win->ws->r);
4145 win->g_floatvalid = 1;
4146 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
4147 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
4148 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
4149 win->g_float.w, win->g_float.h);
4150 }
4151
4152 void
4153 load_float_geom(struct ws_win *win)
4154 {
4155 if (win == NULL || win->ws->r == NULL)
4156 return;
4157
4158 if (win->g_floatvalid) {
4159 win->g = win->g_float;
4160 X(win) += X(win->ws->r);
4161 Y(win) += Y(win->ws->r);
4162 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g: (%d,%d)"
4163 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
4164 HEIGHT(win));
4165 } else {
4166 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g_float "
4167 "is not set.\n", win->id);
4168 }
4169 }
4170
4171 void
4172 stack_floater(struct ws_win *win, struct swm_region *r)
4173 {
4174 if (win == NULL)
4175 return;
4176
4177 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
4178
4179 /*
4180 * to allow windows to change their size (e.g. mplayer fs) only retrieve
4181 * geom on ws switches or return from max mode
4182 */
4183 if (win->floatmaxed || (r != r->ws->old_r &&
4184 !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
4185 /* update geometry for the new region */
4186 load_float_geom(win);
4187 }
4188
4189 win->floatmaxed = 0;
4190
4191 /*
4192 * if set to fullscreen mode, configure window to maximum size.
4193 */
4194 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4195 if (!win->g_floatvalid)
4196 store_float_geom(win);
4197
4198 win->g = r->g;
4199 }
4200
4201 /*
4202 * remove border on fullscreen floater when in fullscreen mode or when
4203 * the quirk is present.
4204 */
4205 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
4206 ((win->quirks & SWM_Q_FULLSCREEN) &&
4207 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
4208 if (win->bordered) {
4209 win->bordered = 0;
4210 X(win) += border_width;
4211 Y(win) += border_width;
4212 }
4213 } else if (!win->bordered) {
4214 win->bordered = 1;
4215 X(win) -= border_width;
4216 Y(win) -= border_width;
4217 }
4218
4219 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
4220 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
4221 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
4222 }
4223
4224 if (!win->manual && !(win->ewmh_flags & EWMH_F_FULLSCREEN) &&
4225 !(win->quirks & SWM_Q_ANYWHERE)) {
4226 /*
4227 * floaters and transients are auto-centred unless moved,
4228 * resized or ANYWHERE quirk is set.
4229 */
4230 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
4231 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
4232
4233 store_float_geom(win);
4234 }
4235
4236 /* Ensure at least 1 pixel of the window is in the region. */
4237 region_containment(win, r, SWM_CW_ALLSIDES);
4238
4239 update_window(win);
4240 }
4241
4242 /*
4243 * Send keystrokes to terminal to decrease/increase the font size as the
4244 * window size changes.
4245 */
4246 void
4247 adjust_font(struct ws_win *win)
4248 {
4249 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
4250 win->floating || win->transient)
4251 return;
4252
4253 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
4254 WIDTH(win) / win->sh.width_inc < term_width &&
4255 win->font_steps < SWM_MAX_FONT_STEPS) {
4256 win->font_size_boundary[win->font_steps] =
4257 (win->sh.width_inc * term_width) + win->sh.base_width;
4258 win->font_steps++;
4259 font_adjusted++;
4260 win->last_inc = win->sh.width_inc;
4261 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
4262 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
4263 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
4264 win->font_steps--;
4265 font_adjusted++;
4266 win->last_inc = win->sh.width_inc;
4267 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
4268 }
4269 }
4270
4271 #define SWAPXY(g) do { \
4272 int tmp; \
4273 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4274 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4275 } while (0)
4276 void
4277 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
4278 {
4279 struct swm_geometry win_g, r_g = *g;
4280 struct ws_win *win, *fs_win = NULL;
4281 int i, j, s, stacks;
4282 int w_inc = 1, h_inc, w_base = 1, h_base;
4283 int hrh, extra = 0, h_slice, last_h = 0;
4284 int split, colno, winno, mwin, msize, mscale;
4285 int remain, missing, v_slice, reconfigure = 0;
4286 int bordered = 1;
4287
4288 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
4289 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
4290
4291 winno = count_win(ws, 0);
4292 if (winno == 0 && count_win(ws, 1) == 0)
4293 return;
4294
4295 TAILQ_FOREACH(win, &ws->winlist, entry)
4296 if (!win->transient && !win->floating && !win->iconic)
4297 break;
4298
4299 if (win == NULL)
4300 goto notiles;
4301
4302 if (rot) {
4303 w_inc = win->sh.width_inc;
4304 w_base = win->sh.base_width;
4305 mwin = ws->l_state.horizontal_mwin;
4306 mscale = ws->l_state.horizontal_msize;
4307 stacks = ws->l_state.horizontal_stacks;
4308 SWAPXY(&r_g);
4309 } else {
4310 w_inc = win->sh.height_inc;
4311 w_base = win->sh.base_height;
4312 mwin = ws->l_state.vertical_mwin;
4313 mscale = ws->l_state.vertical_msize;
4314 stacks = ws->l_state.vertical_stacks;
4315 }
4316 win_g = r_g;
4317
4318 if (stacks > winno - mwin)
4319 stacks = winno - mwin;
4320 if (stacks < 1)
4321 stacks = 1;
4322
4323 h_slice = r_g.h / SWM_H_SLICE;
4324 if (mwin && winno > mwin) {
4325 v_slice = r_g.w / SWM_V_SLICE;
4326
4327 split = mwin;
4328 colno = split;
4329 win_g.w = v_slice * mscale;
4330
4331 if (w_inc > 1 && w_inc < v_slice) {
4332 /* adjust for window's requested size increment */
4333 remain = (win_g.w - w_base) % w_inc;
4334 win_g.w -= remain;
4335 }
4336
4337 msize = win_g.w;
4338 if (flip)
4339 win_g.x += r_g.w - msize;
4340 } else {
4341 msize = -2;
4342 colno = split = winno / stacks;
4343 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
4344 2 * border_width) / stacks);
4345 }
4346 hrh = r_g.h / colno;
4347 extra = r_g.h - (colno * hrh);
4348 win_g.h = hrh - 2 * border_width;
4349
4350 /* stack all the tiled windows */
4351 i = j = 0, s = stacks;
4352 TAILQ_FOREACH(win, &ws->winlist, entry) {
4353 if (win->transient || win->floating || win->iconic)
4354 continue;
4355
4356 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4357 fs_win = win;
4358 continue;
4359 }
4360
4361 if (split && i == split) {
4362 colno = (winno - mwin) / stacks;
4363 if (s <= (winno - mwin) % stacks)
4364 colno++;
4365 split += colno;
4366 hrh = r_g.h / colno;
4367 extra = r_g.h - (colno * hrh);
4368
4369 if (!flip)
4370 win_g.x += win_g.w + 2 * border_width +
4371 tile_gap;
4372
4373 win_g.w = (r_g.w - msize -
4374 (stacks * (2 * border_width + tile_gap))) / stacks;
4375 if (s == 1)
4376 win_g.w += (r_g.w - msize -
4377 (stacks * (2 * border_width + tile_gap))) %
4378 stacks;
4379
4380 if (flip)
4381 win_g.x -= win_g.w + 2 * border_width +
4382 tile_gap;
4383 s--;
4384 j = 0;
4385 }
4386
4387 win_g.h = hrh - 2 * border_width - tile_gap;
4388
4389 if (rot) {
4390 h_inc = win->sh.width_inc;
4391 h_base = win->sh.base_width;
4392 } else {
4393 h_inc = win->sh.height_inc;
4394 h_base = win->sh.base_height;
4395 }
4396
4397 if (j == colno - 1) {
4398 win_g.h = hrh + extra;
4399 } else if (h_inc > 1 && h_inc < h_slice) {
4400 /* adjust for window's requested size increment */
4401 remain = (win_g.h - h_base) % h_inc;
4402 missing = h_inc - remain;
4403
4404 if (missing <= extra || j == 0) {
4405 extra -= missing;
4406 win_g.h += missing;
4407 } else {
4408 win_g.h -= remain;
4409 extra += remain;
4410 }
4411 }
4412
4413 if (j == 0)
4414 win_g.y = r_g.y;
4415 else
4416 win_g.y += last_h + 2 * border_width + tile_gap;
4417
4418 if (disable_border && !(bar_enabled && ws->bar_enabled) &&
4419 winno == 1){
4420 bordered = 0;
4421 win_g.w += 2 * border_width;
4422 win_g.h += 2 * border_width;
4423 } else {
4424 bordered = 1;
4425 }
4426
4427 if (rot) {
4428 if (X(win) != win_g.y || Y(win) != win_g.x ||
4429 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
4430 reconfigure = 1;
4431 X(win) = win_g.y;
4432 Y(win) = win_g.x;
4433 WIDTH(win) = win_g.h;
4434 HEIGHT(win) = win_g.w;
4435 }
4436 } else {
4437 if (X(win) != win_g.x || Y(win) != win_g.y ||
4438 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
4439 reconfigure = 1;
4440 X(win) = win_g.x;
4441 Y(win) = win_g.y;
4442 WIDTH(win) = win_g.w;
4443 HEIGHT(win) = win_g.h;
4444 }
4445 }
4446
4447 if (bordered != win->bordered) {
4448 reconfigure = 1;
4449 win->bordered = bordered;
4450 }
4451
4452 if (reconfigure) {
4453 adjust_font(win);
4454 update_window(win);
4455 }
4456
4457 map_window(win, get_sibling(win, SWM_STACK_BELOW));
4458
4459 last_h = win_g.h;
4460 i++;
4461 j++;
4462 }
4463
4464 /* Map/raise focused tiled window to top if windows could overlap. */
4465 if (tile_gap < 0 && ws->focus != NULL && !ws->focus->floating)
4466 map_window(ws->focus, get_sibling(ws->focus, SWM_STACK_TOP));
4467
4468 notiles:
4469 /* now, stack all the floaters and transients */
4470 TAILQ_FOREACH(win, &ws->winlist, entry) {
4471 if (!win->transient && !win->floating)
4472 continue;
4473 if (win->iconic)
4474 continue;
4475 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4476 fs_win = win;
4477 continue;
4478 }
4479
4480 stack_floater(win, ws->r);
4481 map_window(win, XCB_WINDOW_NONE);
4482 }
4483
4484 /* Make sure fs_win is stacked last so it's on top. */
4485 if (fs_win) {
4486 stack_floater(fs_win, ws->r);
4487 map_window(fs_win, XCB_WINDOW_NONE);
4488 }
4489 }
4490
4491 void
4492 vertical_config(struct workspace *ws, int id)
4493 {
4494 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
4495 id, ws->idx);
4496
4497 switch (id) {
4498 case SWM_ARG_ID_STACKRESET:
4499 case SWM_ARG_ID_STACKINIT:
4500 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
4501 ws->l_state.vertical_mwin = 1;
4502 ws->l_state.vertical_stacks = 1;
4503 break;
4504 case SWM_ARG_ID_MASTERSHRINK:
4505 if (ws->l_state.vertical_msize > 1)
4506 ws->l_state.vertical_msize--;
4507 break;
4508 case SWM_ARG_ID_MASTERGROW:
4509 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
4510 ws->l_state.vertical_msize++;
4511 break;
4512 case SWM_ARG_ID_MASTERADD:
4513 ws->l_state.vertical_mwin++;
4514 break;
4515 case SWM_ARG_ID_MASTERDEL:
4516 if (ws->l_state.vertical_mwin > 0)
4517 ws->l_state.vertical_mwin--;
4518 break;
4519 case SWM_ARG_ID_STACKINC:
4520 ws->l_state.vertical_stacks++;
4521 break;
4522 case SWM_ARG_ID_STACKDEC:
4523 if (ws->l_state.vertical_stacks > 1)
4524 ws->l_state.vertical_stacks--;
4525 break;
4526 case SWM_ARG_ID_FLIPLAYOUT:
4527 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
4528 break;
4529 default:
4530 return;
4531 }
4532 }
4533
4534 void
4535 vertical_stack(struct workspace *ws, struct swm_geometry *g)
4536 {
4537 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
4538
4539 stack_master(ws, g, 0, ws->l_state.vertical_flip);
4540 }
4541
4542 void
4543 horizontal_config(struct workspace *ws, int id)
4544 {
4545 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
4546
4547 switch (id) {
4548 case SWM_ARG_ID_STACKRESET:
4549 case SWM_ARG_ID_STACKINIT:
4550 ws->l_state.horizontal_mwin = 1;
4551 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
4552 ws->l_state.horizontal_stacks = 1;
4553 break;
4554 case SWM_ARG_ID_MASTERSHRINK:
4555 if (ws->l_state.horizontal_msize > 1)
4556 ws->l_state.horizontal_msize--;
4557 break;
4558 case SWM_ARG_ID_MASTERGROW:
4559 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
4560 ws->l_state.horizontal_msize++;
4561 break;
4562 case SWM_ARG_ID_MASTERADD:
4563 ws->l_state.horizontal_mwin++;
4564 break;
4565 case SWM_ARG_ID_MASTERDEL:
4566 if (ws->l_state.horizontal_mwin > 0)
4567 ws->l_state.horizontal_mwin--;
4568 break;
4569 case SWM_ARG_ID_STACKINC:
4570 ws->l_state.horizontal_stacks++;
4571 break;
4572 case SWM_ARG_ID_STACKDEC:
4573 if (ws->l_state.horizontal_stacks > 1)
4574 ws->l_state.horizontal_stacks--;
4575 break;
4576 case SWM_ARG_ID_FLIPLAYOUT:
4577 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
4578 break;
4579 default:
4580 return;
4581 }
4582 }
4583
4584 void
4585 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
4586 {
4587 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
4588
4589 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
4590 }
4591
4592 /* fullscreen view */
4593 void
4594 max_stack(struct workspace *ws, struct swm_geometry *g)
4595 {
4596 struct swm_geometry gg = *g;
4597 struct ws_win *w, *win = NULL, *parent = NULL;
4598 int winno;
4599
4600 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
4601
4602 if (ws == NULL)
4603 return;
4604
4605 winno = count_win(ws, 0);
4606 if (winno == 0 && count_win(ws, 1) == 0)
4607 return;
4608
4609 /* Figure out which top level window should be visible. */
4610 if (ws->focus_pending)
4611 win = ws->focus_pending;
4612 else if (ws->focus)
4613 win = ws->focus;
4614 else if (ws->focus_prev)
4615 win = ws->focus_prev;
4616 else
4617 win = TAILQ_FIRST(&ws->winlist);
4618
4619 if (win->transient)
4620 parent = find_window(win->transient);
4621
4622 DNPRINTF(SWM_D_STACK, "max_stack: win: 0x%x\n", win->id);
4623
4624 /* maximize all top level windows */
4625 TAILQ_FOREACH(w, &ws->winlist, entry) {
4626 if (w->transient || w->iconic)
4627 continue;
4628
4629 if (!w->mapped && w != win)
4630 map_window(w, XCB_WINDOW_NONE);
4631
4632 if (w->floating && !w->floatmaxed) {
4633 /*
4634 * retain geometry for retrieval on exit from
4635 * max_stack mode
4636 */
4637 store_float_geom(w);
4638 w->floatmaxed = 1;
4639 }
4640
4641 /* only reconfigure if necessary */
4642 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4643 HEIGHT(w) != gg.h) {
4644 w->g = gg;
4645 if (bar_enabled && ws->bar_enabled){
4646 w->bordered = 1;
4647 } else {
4648 w->bordered = 0;
4649 WIDTH(w) += 2 * border_width;
4650 HEIGHT(w) += 2 * border_width;
4651 }
4652
4653 update_window(w);
4654 }
4655 }
4656
4657 /* If a parent exists, map/raise it first. */
4658 if (parent) {
4659 map_window(parent, XCB_WINDOW_NONE);
4660
4661 /* Map siblings next. */
4662 TAILQ_FOREACH(w, &ws->winlist, entry)
4663 if (w != win && !w->iconic &&
4664 w->transient == parent->id) {
4665 stack_floater(w, ws->r);
4666 map_window(w, XCB_WINDOW_NONE);
4667 }
4668 }
4669
4670 /* Map/raise focused window. */
4671 map_window(win, XCB_WINDOW_NONE);
4672
4673 /* Finally, map/raise children of focus window. */
4674 TAILQ_FOREACH(w, &ws->winlist, entry)
4675 if (w->transient == win->id && !w->iconic) {
4676 stack_floater(w, ws->r);
4677 map_window(w, XCB_WINDOW_NONE);
4678 }
4679 }
4680
4681 void
4682 send_to_rg(struct swm_region *r, union arg *args)
4683 {
4684 int ridx = args->id, i, num_screens;
4685 struct swm_region *rr = NULL;
4686 union arg a;
4687
4688 num_screens = get_screen_count();
4689 /* do nothing if we don't have more than one screen */
4690 if (!(num_screens > 1 || outputs > 1))
4691 return;
4692
4693 DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
4694
4695 rr = TAILQ_FIRST(&r->s->rl);
4696 for (i = 0; i < ridx && rr != NULL; ++i)
4697 rr = TAILQ_NEXT(rr, entry);
4698
4699 if (rr == NULL)
4700 return;
4701
4702 a.id = rr->ws->idx;
4703
4704 send_to_ws(r, &a);
4705 }
4706
4707 struct swm_region *
4708 region_under(struct swm_screen *s, int x, int y)
4709 {
4710 struct swm_region *r = NULL;
4711
4712 TAILQ_FOREACH(r, &s->rl, entry) {
4713 DNPRINTF(SWM_D_MISC, "region_under: ws: %d, region g: (%d,%d) "
4714 "%d x %d, coords: (%d,%d)\n", r->ws->idx, X(r), Y(r),
4715 WIDTH(r), HEIGHT(r), x, y);
4716 if (X(r) <= x && x < MAX_X(r))
4717 if (Y(r) <= y && y < MAX_Y(r))
4718 return (r);
4719 }
4720
4721 return (NULL);
4722 }
4723
4724 void
4725 send_to_ws(struct swm_region *r, union arg *args)
4726 {
4727 int wsid = args->id;
4728 struct ws_win *win = NULL;
4729
4730 if (r && r->ws && r->ws->focus)
4731 win = r->ws->focus;
4732 else
4733 return;
4734
4735 DNPRINTF(SWM_D_MOVE, "send_to_ws: win 0x%x, ws %d\n", win->id, wsid);
4736
4737 win_to_ws(win, wsid, 1);
4738
4739 /* Restack and set new focus. */
4740 stack();
4741
4742 if (focus_mode != SWM_FOCUS_FOLLOW) {
4743 if (r->ws->focus_pending) {
4744 focus_win(r->ws->focus_pending);
4745 r->ws->focus_pending = NULL;
4746 } else {
4747 xcb_set_input_focus(conn,
4748 XCB_INPUT_FOCUS_PARENT, r->id,
4749 XCB_CURRENT_TIME);
4750 }
4751 }
4752
4753 focus_flush();
4754 }
4755
4756 void
4757 win_to_ws(struct ws_win *win, int wsid, int unfocus)
4758 {
4759 struct ws_win *parent;
4760 struct workspace *ws, *nws, *pws;
4761 char ws_idx_str[SWM_PROPLEN];
4762
4763 if (wsid >= workspace_limit)
4764 return;
4765
4766 if (win->ws->idx == wsid)
4767 return;
4768
4769 ws = win->ws;
4770 nws = &win->s->ws[wsid];
4771
4772 DNPRINTF(SWM_D_MOVE, "win_to_ws: win 0x%x, ws %d -> %d\n", win->id,
4773 ws->idx, wsid);
4774
4775 /* Update the window's workspace property: _SWM_WS */
4776 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
4777 if (focus_mode != SWM_FOCUS_FOLLOW)
4778 ws->focus_pending = get_focus_prev(win);
4779
4780 /* Move the parent if this is a transient window. */
4781 if (win->transient) {
4782 parent = find_window(win->transient);
4783 if (parent) {
4784 pws = parent->ws;
4785 /* Set new focus in parent's ws if needed. */
4786 if (pws->focus == parent) {
4787 if (focus_mode != SWM_FOCUS_FOLLOW)
4788 pws->focus_pending =
4789 get_focus_prev(parent);
4790
4791 unfocus_win(parent);
4792
4793 if (focus_mode != SWM_FOCUS_FOLLOW) {
4794 pws->focus = pws->focus_pending;
4795 pws->focus_pending = NULL;
4796 }
4797 }
4798
4799 /* Don't unmap parent if new ws is visible */
4800 if (nws->r == NULL)
4801 unmap_window(parent);
4802
4803 /* Transfer */
4804 TAILQ_REMOVE(&ws->winlist, parent, entry);
4805 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
4806 parent->ws = nws;
4807
4808 DNPRINTF(SWM_D_PROP, "win_to_ws: set "
4809 "property: _SWM_WS: %s\n", ws_idx_str);
4810 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
4811 parent->id, a_swm_ws, XCB_ATOM_STRING, 8,
4812 strlen(ws_idx_str), ws_idx_str);
4813 }
4814 }
4815
4816 if (unfocus)
4817 unfocus_win(win);
4818
4819 /* Don't unmap if new ws is visible */
4820 if (nws->r == NULL)
4821 unmap_window(win);
4822
4823 /* Transfer */
4824 TAILQ_REMOVE(&ws->winlist, win, entry);
4825 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
4826 win->ws = nws;
4827
4828 /* Set focus on new ws. */
4829 unfocus_win(nws->focus);
4830 nws->focus = win;
4831
4832 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: _SWM_WS: %s\n",
4833 ws_idx_str);
4834 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
4835 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
4836 ws_idx_str);
4837 }
4838
4839 DNPRINTF(SWM_D_MOVE, "win_to_ws: done.\n");
4840 }
4841
4842 void
4843 pressbutton(struct swm_region *r, union arg *args)
4844 {
4845 /* suppress unused warning since var is needed */
4846 (void)r;
4847
4848 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
4849 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4850 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
4851 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4852 }
4853
4854 void
4855 raise_toggle(struct swm_region *r, union arg *args)
4856 {
4857 /* suppress unused warning since var is needed */
4858 (void)args;
4859
4860 if (r == NULL || r->ws == NULL)
4861 return;
4862
4863 r->ws->always_raise = !r->ws->always_raise;
4864
4865 /* bring floaters back to top */
4866 if (!r->ws->always_raise)
4867 stack();
4868
4869 focus_flush();
4870 }
4871
4872 void
4873 iconify(struct swm_region *r, union arg *args)
4874 {
4875 /* suppress unused warning since var is needed */
4876 (void)args;
4877
4878 if (r->ws->focus == NULL)
4879 return;
4880
4881 set_swm_iconic(r->ws->focus, 1);
4882
4883 xcb_flush(conn);
4884 }
4885
4886 char *
4887 get_win_name(xcb_window_t win)
4888 {
4889 char *name = NULL;
4890 xcb_get_property_cookie_t c;
4891 xcb_get_property_reply_t *r;
4892
4893 /* First try _NET_WM_NAME for UTF-8. */
4894 c = xcb_get_property(conn, 0, win, ewmh[_NET_WM_NAME].atom,
4895 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4896 r = xcb_get_property_reply(conn, c, NULL);
4897 if (r) {
4898 if (r->type == XCB_NONE) {
4899 free(r);
4900 /* Use WM_NAME instead; no UTF-8. */
4901 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
4902 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4903 r = xcb_get_property_reply(conn, c, NULL);
4904 if (r == NULL)
4905 return (NULL);
4906 if (r->type == XCB_NONE) {
4907 free(r);
4908 return (NULL);
4909 }
4910 }
4911 if (r->length > 0)
4912 name = strndup(xcb_get_property_value(r),
4913 xcb_get_property_value_length(r));
4914
4915 free(r);
4916 }
4917
4918 return (name);
4919 }
4920
4921 void
4922 uniconify(struct swm_region *r, union arg *args)
4923 {
4924 struct ws_win *win;
4925 FILE *lfile;
4926 char *name;
4927 int count = 0;
4928
4929 DNPRINTF(SWM_D_MISC, "uniconify\n");
4930
4931 if (r == NULL || r->ws == NULL)
4932 return;
4933
4934 /* make sure we have anything to uniconify */
4935 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4936 if (win->ws == NULL)
4937 continue; /* should never happen */
4938 if (!win->iconic)
4939 continue;
4940 count++;
4941 }
4942 if (count == 0)
4943 return;
4944
4945 search_r = r;
4946 search_resp_action = SWM_SEARCH_UNICONIFY;
4947
4948 spawn_select(r, args, "search", &searchpid);
4949
4950 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4951 return;
4952
4953 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4954 if (win->ws == NULL)
4955 continue; /* should never happen */
4956 if (!win->iconic)
4957 continue;
4958
4959 name = get_win_name(win->id);
4960 if (name == NULL)
4961 continue;
4962 fprintf(lfile, "%s.%u\n", name, win->id);
4963 free(name);
4964 }
4965
4966 fclose(lfile);
4967 }
4968
4969 void
4970 name_workspace(struct swm_region *r, union arg *args)
4971 {
4972 FILE *lfile;
4973
4974 DNPRINTF(SWM_D_MISC, "name_workspace\n");
4975
4976 if (r == NULL)
4977 return;
4978
4979 search_r = r;
4980 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
4981
4982 spawn_select(r, args, "name_workspace", &searchpid);
4983
4984 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4985 return;
4986
4987 fprintf(lfile, "%s", "");
4988 fclose(lfile);
4989 }
4990
4991 void
4992 search_workspace(struct swm_region *r, union arg *args)
4993 {
4994 int i;
4995 struct workspace *ws;
4996 FILE *lfile;
4997
4998 DNPRINTF(SWM_D_MISC, "search_workspace\n");
4999
5000 if (r == NULL)
5001 return;
5002
5003 search_r = r;
5004 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
5005
5006 spawn_select(r, args, "search", &searchpid);
5007
5008 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5009 return;
5010
5011 for (i = 0; i < workspace_limit; i++) {
5012 ws = &r->s->ws[i];
5013 if (ws == NULL)
5014 continue;
5015 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
5016 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
5017 }
5018
5019 fclose(lfile);
5020 }
5021
5022 void
5023 search_win_cleanup(void)
5024 {
5025 struct search_window *sw = NULL;
5026
5027 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
5028 xcb_destroy_window(conn, sw->indicator);
5029 TAILQ_REMOVE(&search_wl, sw, entry);
5030 free(sw);
5031 }
5032 }
5033
5034 void
5035 search_win(struct swm_region *r, union arg *args)
5036 {
5037 struct ws_win *win = NULL;
5038 struct search_window *sw = NULL;
5039 xcb_window_t w;
5040 uint32_t wa[2];
5041 int i, width, height;
5042 char s[8];
5043 FILE *lfile;
5044 size_t len;
5045 XftDraw *draw;
5046 XGlyphInfo info;
5047 GC l_draw;
5048 XGCValues l_gcv;
5049 XRectangle l_ibox, l_lbox;
5050
5051 DNPRINTF(SWM_D_MISC, "search_win\n");
5052
5053 search_r = r;
5054 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
5055
5056 spawn_select(r, args, "search", &searchpid);
5057
5058 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5059 return;
5060
5061 TAILQ_INIT(&search_wl);
5062
5063 i = 1;
5064 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5065 if (win->iconic)
5066 continue;
5067
5068 sw = calloc(1, sizeof(struct search_window));
5069 if (sw == NULL) {
5070 warn("search_win: calloc");
5071 fclose(lfile);
5072 search_win_cleanup();
5073 return;
5074 }
5075 sw->idx = i;
5076 sw->win = win;
5077
5078 snprintf(s, sizeof s, "%d", i);
5079 len = strlen(s);
5080
5081 w = xcb_generate_id(conn);
5082 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
5083 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
5084
5085 if (bar_font_legacy) {
5086 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
5087 width = l_lbox.width + 4;
5088 height = bar_fs_extents->max_logical_extent.height + 4;
5089 } else {
5090 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
5091 &info);
5092 width = info.width + 4;
5093 height = bar_font->height + 4;
5094 }
5095
5096 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
5097 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
5098 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
5099 XCB_CW_BORDER_PIXEL, wa);
5100
5101 xcb_map_window(conn, w);
5102
5103 sw->indicator = w;
5104 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
5105
5106 if (bar_font_legacy) {
5107 l_gcv.graphics_exposures = 0;
5108 l_draw = XCreateGC(display, w, 0, &l_gcv);
5109
5110 XSetForeground(display, l_draw,
5111 r->s->c[SWM_S_COLOR_BAR].pixel);
5112
5113 DRAWSTRING(display, w, bar_fs, l_draw, 2,
5114 (bar_fs_extents->max_logical_extent.height -
5115 l_lbox.height) / 2 - l_lbox.y, s, len);
5116
5117 XFreeGC(display, l_draw);
5118 } else {
5119
5120 draw = XftDrawCreate(display, w,
5121 DefaultVisual(display, r->s->idx),
5122 DefaultColormap(display, r->s->idx));
5123
5124 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
5125 (HEIGHT(r->bar) + bar_font->height) / 2 -
5126 bar_font->descent, (FcChar8 *)s, len);
5127
5128 XftDrawDestroy(draw);
5129 }
5130
5131 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
5132
5133 fprintf(lfile, "%d\n", i);
5134 i++;
5135 }
5136
5137 fclose(lfile);
5138
5139 xcb_flush(conn);
5140 }
5141
5142 void
5143 search_resp_uniconify(const char *resp, size_t len)
5144 {
5145 char *name;
5146 struct ws_win *win;
5147 char *s;
5148
5149 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
5150
5151 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
5152 if (!win->iconic)
5153 continue;
5154 name = get_win_name(win->id);
5155 if (name == NULL)
5156 continue;
5157 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
5158 free(name);
5159 continue;
5160 }
5161 free(name);
5162 if (strncmp(s, resp, len) == 0) {
5163 /* XXX this should be a callback to generalize */
5164 set_swm_iconic(win, 0);
5165 free(s);
5166 break;
5167 }
5168 free(s);
5169 }
5170 }
5171
5172 void
5173 search_resp_name_workspace(const char *resp, size_t len)
5174 {
5175 struct workspace *ws;
5176
5177 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
5178
5179 if (search_r->ws == NULL)
5180 return;
5181 ws = search_r->ws;
5182
5183 if (ws->name) {
5184 free(search_r->ws->name);
5185 search_r->ws->name = NULL;
5186 }
5187
5188 if (len > 1) {
5189 ws->name = strdup(resp);
5190 if (ws->name == NULL) {
5191 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
5192 "strdup: %s", strerror(errno));
5193 return;
5194 }
5195 }
5196 }
5197
5198 void
5199 search_resp_search_workspace(const char *resp)
5200 {
5201 char *p, *q;
5202 int ws_idx;
5203 const char *errstr;
5204 union arg a;
5205
5206 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
5207
5208 q = strdup(resp);
5209 if (q == NULL) {
5210 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
5211 strerror(errno));
5212 return;
5213 }
5214 p = strchr(q, ':');
5215 if (p != NULL)
5216 *p = '\0';
5217 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
5218 if (errstr) {
5219 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
5220 errstr, q);
5221 free(q);
5222 return;
5223 }
5224 free(q);
5225 a.id = ws_idx - 1;
5226 switchws(search_r, &a);
5227 }
5228
5229 void
5230 search_resp_search_window(const char *resp)
5231 {
5232 char *s;
5233 int idx;
5234 const char *errstr;
5235 struct search_window *sw;
5236
5237 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
5238
5239 s = strdup(resp);
5240 if (s == NULL) {
5241 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
5242 strerror(errno));
5243 return;
5244 }
5245
5246 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
5247 if (errstr) {
5248 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
5249 errstr, s);
5250 free(s);
5251 return;
5252 }
5253 free(s);
5254
5255 TAILQ_FOREACH(sw, &search_wl, entry)
5256 if (idx == sw->idx) {
5257 focus_win(sw->win);
5258 break;
5259 }
5260 }
5261
5262 #define MAX_RESP_LEN 1024
5263
5264 void
5265 search_do_resp(void)
5266 {
5267 ssize_t rbytes;
5268 char *resp;
5269 size_t len;
5270
5271 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
5272
5273 search_resp = 0;
5274 searchpid = 0;
5275
5276 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
5277 warn("search: calloc");
5278 goto done;
5279 }
5280
5281 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
5282 if (rbytes <= 0) {
5283 warn("search: read error");
5284 goto done;
5285 }
5286 resp[rbytes] = '\0';
5287
5288 /* XXX:
5289 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5290 * newline, so work around that by sanitizing the resp now.
5291 */
5292 resp[strcspn(resp, "\n")] = '\0';
5293 len = strlen(resp);
5294
5295 switch (search_resp_action) {
5296 case SWM_SEARCH_UNICONIFY:
5297 search_resp_uniconify(resp, len);
5298 break;
5299 case SWM_SEARCH_NAME_WORKSPACE:
5300 search_resp_name_workspace(resp, len);
5301 break;
5302 case SWM_SEARCH_SEARCH_WORKSPACE:
5303 search_resp_search_workspace(resp);
5304 break;
5305 case SWM_SEARCH_SEARCH_WINDOW:
5306 search_resp_search_window(resp);
5307 break;
5308 }
5309
5310 done:
5311 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
5312 search_win_cleanup();
5313
5314 search_resp_action = SWM_SEARCH_NONE;
5315 close(select_resp_pipe[0]);
5316 free(resp);
5317
5318 xcb_flush(conn);
5319 }
5320
5321 void
5322 wkill(struct swm_region *r, union arg *args)
5323 {
5324 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
5325
5326 if (r->ws->focus == NULL)
5327 return;
5328
5329 if (args->id == SWM_ARG_ID_KILLWINDOW)
5330 xcb_kill_client(conn, r->ws->focus->id);
5331 else
5332 if (r->ws->focus->can_delete)
5333 client_msg(r->ws->focus, a_delete, 0);
5334
5335 focus_flush();
5336 }
5337
5338 int
5339 floating_toggle_win(struct ws_win *win)
5340 {
5341 if (win == NULL)
5342 return (0);
5343
5344 if (win->ws->r == NULL)
5345 return (0);
5346
5347 /* reject floating toggles in max stack mode */
5348 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
5349 return (0);
5350
5351 if (win->floating) {
5352 if (!win->floatmaxed) {
5353 /* retain position for refloat */
5354 store_float_geom(win);
5355 }
5356 win->floating = 0;
5357 } else {
5358 load_float_geom(win);
5359 win->floating = 1;
5360 }
5361
5362 ewmh_update_actions(win);
5363
5364 return (1);
5365 }
5366
5367 void
5368 floating_toggle(struct swm_region *r, union arg *args)
5369 {
5370 struct ws_win *win = r->ws->focus;
5371
5372 /* suppress unused warning since var is needed */
5373 (void)args;
5374
5375 if (win == NULL)
5376 return;
5377
5378 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5379 return;
5380
5381 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5382 _NET_WM_STATE_TOGGLE);
5383
5384 stack();
5385
5386 if (win == win->ws->focus)
5387 focus_win(win);
5388
5389 focus_flush();
5390 }
5391
5392 void
5393 region_containment(struct ws_win *win, struct swm_region *r, int opts)
5394 {
5395 struct swm_geometry g = r->g;
5396 int rt, lt, tp, bm, bw;
5397
5398 bw = (opts & SWM_CW_SOFTBOUNDARY) ? boundary_width : 0;
5399
5400 /*
5401 * Perpendicular distance of each side of the window to the respective
5402 * side of the region boundary. Positive values indicate the side of
5403 * the window has passed beyond the region boundary.
5404 */
5405 rt = opts & SWM_CW_RIGHT ? MAX_X(win) + BORDER(win) - MAX_X(r) : bw;
5406 lt = opts & SWM_CW_LEFT ? X(r) - X(win) + BORDER(win) : bw;
5407 bm = opts & SWM_CW_BOTTOM ? MAX_Y(win) + BORDER(win) - MAX_Y(r) : bw;
5408 tp = opts & SWM_CW_TOP ? Y(r) - Y(win) + BORDER(win) : bw;
5409
5410 DNPRINTF(SWM_D_MISC, "region_containment: win 0x%x, rt: %d, lt: %d, "
5411 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win->id, rt,
5412 lt, bm, tp, YESNO(opts & SWM_CW_SOFTBOUNDARY),
5413 YESNO(opts & SWM_CW_HARDBOUNDARY));
5414
5415 /*
5416 * Disable containment if any of the flagged sides went beyond the
5417 * containment boundary, or if containment is disabled.
5418 */
5419 if (!(opts & SWM_CW_HARDBOUNDARY || opts & SWM_CW_SOFTBOUNDARY) ||
5420 (bw != 0 && ((rt > bw) || (lt > bw) || (bm > bw) || (tp > bw)))) {
5421 /* Make sure window has at least 1 pixel in the region */
5422 g.x += 1 - WIDTH(win);
5423 g.y += 1 - HEIGHT(win);
5424 g.w += 2 * WIDTH(win) - 2;
5425 g.h += 2 * HEIGHT(win) - 2;
5426 }
5427
5428 constrain_window(win, &g, &opts);
5429 }
5430
5431 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
5432 void
5433 constrain_window(struct ws_win *win, struct swm_geometry *b, int *opts)
5434 {
5435 DNPRINTF(SWM_D_MISC, "constrain_window: window: 0x%x, (x,y) w x h: "
5436 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
5437 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win->id, X(win),
5438 Y(win), WIDTH(win), HEIGHT(win), b->x, b->y, b->w, b->h,
5439 YESNO(*opts & SWM_CW_RIGHT), YESNO(*opts & SWM_CW_LEFT),
5440 YESNO(*opts & SWM_CW_BOTTOM), YESNO(*opts & SWM_CW_TOP),
5441 YESNO(*opts & SWM_CW_RESIZABLE));
5442
5443 if ((*opts & SWM_CW_RIGHT) && MAX_X(win) + BORDER(win) > b->x + b->w) {
5444 if (*opts & SWM_CW_RESIZABLE)
5445 WIDTH(win) = b->x + b->w - X(win) - BORDER(win);
5446 else
5447 X(win) = b->x + b->w - WIDTH(win) - BORDER(win);
5448 }
5449
5450 if ((*opts & SWM_CW_LEFT) && X(win) + BORDER(win) < b->x) {
5451 if (*opts & SWM_CW_RESIZABLE)
5452 WIDTH(win) -= b->x - X(win) - BORDER(win);
5453
5454 X(win) = b->x - BORDER(win);
5455 }
5456
5457 if ((*opts & SWM_CW_BOTTOM) && MAX_Y(win) + BORDER(win) > b->y + b->h) {
5458 if (*opts & SWM_CW_RESIZABLE)
5459 HEIGHT(win) = b->y + b->h - Y(win) - BORDER(win);
5460 else
5461 Y(win) = b->y + b->h - HEIGHT(win) - BORDER(win);
5462 }
5463
5464 if ((*opts & SWM_CW_TOP) && Y(win) + BORDER(win) < b->y) {
5465 if (*opts & SWM_CW_RESIZABLE)
5466 HEIGHT(win) -= b->y - Y(win) - BORDER(win);
5467
5468 Y(win) = b->y - BORDER(win);
5469 }
5470
5471 if (*opts & SWM_CW_RESIZABLE) {
5472 if (WIDTH(win) < 1)
5473 WIDTH(win) = 1;
5474 if (HEIGHT(win) < 1)
5475 HEIGHT(win) = 1;
5476 }
5477 }
5478
5479 void
5480 update_window(struct ws_win *win)
5481 {
5482 uint16_t mask;
5483 uint32_t wc[5];
5484
5485 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
5486 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
5487 XCB_CONFIG_WINDOW_BORDER_WIDTH;
5488 wc[0] = X(win);
5489 wc[1] = Y(win);
5490 wc[2] = WIDTH(win);
5491 wc[3] = HEIGHT(win);
5492 wc[4] = BORDER(win);
5493
5494 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
5495 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
5496 wc[3], YESNO(win->bordered));
5497
5498 xcb_configure_window(conn, win->id, mask, wc);
5499 }
5500
5501 #define SWM_RESIZE_STEPS (50)
5502
5503 void
5504 resize(struct ws_win *win, union arg *args)
5505 {
5506 xcb_timestamp_t timestamp = 0;
5507 struct swm_region *r = NULL;
5508 int resize_stp = 0;
5509 struct swm_geometry g;
5510 int top = 0, left = 0, resizing;
5511 int dx, dy;
5512 xcb_cursor_t cursor;
5513 xcb_query_pointer_reply_t *xpr = NULL;
5514 xcb_generic_event_t *evt;
5515 xcb_motion_notify_event_t *mne;
5516
5517 if (win == NULL)
5518 return;
5519 r = win->ws->r;
5520
5521 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5522 return;
5523
5524 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
5525 "transient: 0x%x\n", win->id, YESNO(win->floating),
5526 win->transient);
5527
5528 if (!win->transient && !win->floating)
5529 return;
5530
5531 /* reject resizes in max mode for floaters (transient ok) */
5532 if (win->floatmaxed)
5533 return;
5534
5535 win->manual = 1;
5536 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5537 _NET_WM_STATE_ADD);
5538
5539 stack();
5540
5541 focus_flush();
5542
5543 /* It's possible for win to have been freed during focus_flush(). */
5544 if (validate_win(win)) {
5545 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
5546 goto out;
5547 }
5548
5549 switch (args->id) {
5550 case SWM_ARG_ID_WIDTHSHRINK:
5551 WIDTH(win) -= SWM_RESIZE_STEPS;
5552 resize_stp = 1;
5553 break;
5554 case SWM_ARG_ID_WIDTHGROW:
5555 WIDTH(win) += SWM_RESIZE_STEPS;
5556 resize_stp = 1;
5557 break;
5558 case SWM_ARG_ID_HEIGHTSHRINK:
5559 HEIGHT(win) -= SWM_RESIZE_STEPS;
5560 resize_stp = 1;
5561 break;
5562 case SWM_ARG_ID_HEIGHTGROW:
5563 HEIGHT(win) += SWM_RESIZE_STEPS;
5564 resize_stp = 1;
5565 break;
5566 default:
5567 break;
5568 }
5569 if (resize_stp) {
5570 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
5571 SWM_CW_HARDBOUNDARY);
5572 update_window(win);
5573 store_float_geom(win);
5574 return;
5575 }
5576
5577 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
5578 SWM_CW_SOFTBOUNDARY);
5579 update_window(win);
5580
5581 /* get cursor offset from window root */
5582 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5583 NULL);
5584 if (xpr == NULL)
5585 return;
5586
5587 g = win->g;
5588
5589 if (xpr->win_x < WIDTH(win) / 2)
5590 left = 1;
5591
5592 if (xpr->win_y < HEIGHT(win) / 2)
5593 top = 1;
5594
5595 if (args->id == SWM_ARG_ID_CENTER)
5596 cursor = cursors[XC_SIZING].cid;
5597 else if (top)
5598 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
5599 XC_TOP_RIGHT_CORNER].cid;
5600 else
5601 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
5602 XC_BOTTOM_RIGHT_CORNER].cid;
5603
5604 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5605 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
5606 XCB_CURRENT_TIME),
5607
5608 xcb_flush(conn);
5609 resizing = 1;
5610 while (resizing && (evt = xcb_wait_for_event(conn))) {
5611 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5612 case XCB_BUTTON_RELEASE:
5613 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
5614 resizing = 0;
5615 break;
5616 case XCB_MOTION_NOTIFY:
5617 mne = (xcb_motion_notify_event_t *)evt;
5618 /* cursor offset/delta from start of the operation */
5619 dx = mne->root_x - xpr->root_x;
5620 dy = mne->root_y - xpr->root_y;
5621
5622 /* vertical */
5623 if (top)
5624 dy = -dy;
5625
5626 if (args->id == SWM_ARG_ID_CENTER) {
5627 if (g.h / 2 + dy < 1)
5628 dy = 1 - g.h / 2;
5629
5630 Y(win) = g.y - dy;
5631 HEIGHT(win) = g.h + 2 * dy;
5632 } else {
5633 if (g.h + dy < 1)
5634 dy = 1 - g.h;
5635
5636 if (top)
5637 Y(win) = g.y - dy;
5638
5639 HEIGHT(win) = g.h + dy;
5640 }
5641
5642 /* horizontal */
5643 if (left)
5644 dx = -dx;
5645
5646 if (args->id == SWM_ARG_ID_CENTER) {
5647 if (g.w / 2 + dx < 1)
5648 dx = 1 - g.w / 2;
5649
5650 X(win) = g.x - dx;
5651 WIDTH(win) = g.w + 2 * dx;
5652 } else {
5653 if (g.w + dx < 1)
5654 dx = 1 - g.w;
5655
5656 if (left)
5657 X(win) = g.x - dx;
5658
5659 WIDTH(win) = g.w + dx;
5660 }
5661
5662 /* not free, don't sync more than 120 times / second */
5663 if ((mne->time - timestamp) > (1000 / 120) ) {
5664 timestamp = mne->time;
5665 regionize(win, mne->root_x, mne->root_y);
5666 region_containment(win, r, SWM_CW_ALLSIDES |
5667 SWM_CW_RESIZABLE | SWM_CW_HARDBOUNDARY |
5668 SWM_CW_SOFTBOUNDARY);
5669 update_window(win);
5670 xcb_flush(conn);
5671 }
5672 break;
5673 default:
5674 event_handle(evt);
5675
5676 /* It's possible for win to have been freed above. */
5677 if (validate_win(win)) {
5678 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
5679 goto out;
5680 }
5681 break;
5682 }
5683 free(evt);
5684 }
5685 if (timestamp) {
5686 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
5687 SWM_CW_HARDBOUNDARY | SWM_CW_SOFTBOUNDARY);
5688 update_window(win);
5689 xcb_flush(conn);
5690 }
5691 store_float_geom(win);
5692 out:
5693 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5694 free(xpr);
5695 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
5696 }
5697
5698 void
5699 resize_step(struct swm_region *r, union arg *args)
5700 {
5701 struct ws_win *win = NULL;
5702
5703 if (r && r->ws && r->ws->focus)
5704 win = r->ws->focus;
5705 else
5706 return;
5707
5708 resize(win, args);
5709 focus_flush();
5710 }
5711
5712 /* Try to set window region based on supplied coordinates or window center. */
5713 void
5714 regionize(struct ws_win *win, int x, int y)
5715 {
5716 struct swm_region *r = NULL;
5717
5718 r = region_under(win->s, x, y);
5719 if (r == NULL)
5720 r = region_under(win->s, X(win) + WIDTH(win) / 2,
5721 Y(win) + HEIGHT(win) / 2);
5722
5723 if (r && r != win->ws->r) {
5724 win_to_ws(win, r->ws->idx, 0);
5725 set_region(r);
5726 }
5727 }
5728
5729 #define SWM_MOVE_STEPS (50)
5730
5731 void
5732 move(struct ws_win *win, union arg *args)
5733 {
5734 xcb_timestamp_t timestamp = 0;
5735 int move_stp = 0, moving;
5736 xcb_query_pointer_reply_t *qpr = NULL;
5737 xcb_generic_event_t *evt;
5738 xcb_motion_notify_event_t *mne;
5739
5740 if (win == NULL)
5741 return;
5742
5743 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5744 return;
5745
5746 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
5747 "0x%x\n", win->id, YESNO(win->floating), win->transient);
5748
5749 /* in max_stack mode should only move transients */
5750 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
5751 return;
5752
5753 if (!win->manual) {
5754 win->manual = 1;
5755 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5756 _NET_WM_STATE_ADD);
5757 }
5758
5759 /* When a stacked win is moved, float it and restack. */
5760 if (!win->floating && !win->transient) {
5761 store_float_geom(win);
5762 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5763 _NET_WM_STATE_ADD);
5764 stack();
5765 }
5766
5767 focus_flush();
5768
5769 /* It's possible for win to have been freed during focus_flush(). */
5770 if (validate_win(win)) {
5771 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
5772 goto out;
5773 }
5774
5775 move_stp = 0;
5776 switch (args->id) {
5777 case SWM_ARG_ID_MOVELEFT:
5778 X(win) -= (SWM_MOVE_STEPS - border_width);
5779 move_stp = 1;
5780 break;
5781 case SWM_ARG_ID_MOVERIGHT:
5782 X(win) += (SWM_MOVE_STEPS - border_width);
5783 move_stp = 1;
5784 break;
5785 case SWM_ARG_ID_MOVEUP:
5786 Y(win) -= (SWM_MOVE_STEPS - border_width);
5787 move_stp = 1;
5788 break;
5789 case SWM_ARG_ID_MOVEDOWN:
5790 Y(win) += (SWM_MOVE_STEPS - border_width);
5791 move_stp = 1;
5792 break;
5793 default:
5794 break;
5795 }
5796 if (move_stp) {
5797 regionize(win, -1, -1);
5798 region_containment(win, win->ws->r, SWM_CW_ALLSIDES);
5799 update_window(win);
5800 store_float_geom(win);
5801 return;
5802 }
5803
5804 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5805 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
5806 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
5807
5808 /* get cursor offset from window root */
5809 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5810 NULL);
5811 if (qpr == NULL) {
5812 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5813 return;
5814 }
5815
5816 regionize(win, qpr->root_x, qpr->root_y);
5817 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
5818 SWM_CW_SOFTBOUNDARY);
5819 update_window(win);
5820 xcb_flush(conn);
5821 moving = 1;
5822 while (moving && (evt = xcb_wait_for_event(conn))) {
5823 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5824 case XCB_BUTTON_RELEASE:
5825 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
5826 moving = 0;
5827 break;
5828 case XCB_MOTION_NOTIFY:
5829 mne = (xcb_motion_notify_event_t *)evt;
5830 DNPRINTF(SWM_D_EVENT, "motion: root: 0x%x\n", mne->root);
5831 X(win) = mne->root_x - qpr->win_x - border_width;
5832 Y(win) = mne->root_y - qpr->win_y - border_width;
5833
5834 /* not free, don't sync more than 120 times / second */
5835 if ((mne->time - timestamp) > (1000 / 120) ) {
5836 timestamp = mne->time;
5837 regionize(win, mne->root_x, mne->root_y);
5838 region_containment(win, win->ws->r,
5839 SWM_CW_ALLSIDES | SWM_CW_SOFTBOUNDARY);
5840 update_window(win);
5841 xcb_flush(conn);
5842 }
5843 break;
5844 default:
5845 event_handle(evt);
5846
5847 /* It's possible for win to have been freed above. */
5848 if (validate_win(win)) {
5849 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
5850 goto out;
5851 }
5852 break;
5853 }
5854 free(evt);
5855 }
5856 if (timestamp) {
5857 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
5858 SWM_CW_SOFTBOUNDARY);
5859 update_window(win);
5860 xcb_flush(conn);
5861 }
5862 store_float_geom(win);
5863 out:
5864 free(qpr);
5865 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5866 DNPRINTF(SWM_D_EVENT, "move: done.\n");
5867 }
5868
5869 void
5870 move_step(struct swm_region *r, union arg *args)
5871 {
5872 struct ws_win *win = NULL;
5873
5874 if (r && r->ws && r->ws->focus)
5875 win = r->ws->focus;
5876 else
5877 return;
5878
5879 if (!win->transient && !win->floating)
5880 return;
5881
5882 move(win, args);
5883 focus_flush();
5884 }
5885
5886 /* key definitions */
5887 struct keyfunc {
5888 char name[SWM_FUNCNAME_LEN];
5889 void (*func)(struct swm_region *r, union arg *);
5890 union arg args;
5891 } keyfuncs[KF_INVALID + 1] = {
5892 /* name function argument */
5893 { "bar_toggle", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE} },
5894 { "bar_toggle_ws", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
5895 { "button2", pressbutton, {2} },
5896 { "cycle_layout", cycle_layout, {0} },
5897 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
5898 { "float_toggle", floating_toggle,{0} },
5899 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
5900 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
5901 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
5902 { "focus_urgent", focus, {.id = SWM_ARG_ID_FOCUSURGENT} },
5903 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
5904 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
5905 { "iconify", iconify, {0} },
5906 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
5907 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
5908 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
5909 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
5910 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
5911 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
5912 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
5913 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
5914 { "mvrg_1", send_to_rg, {.id = 0} },
5915 { "mvrg_2", send_to_rg, {.id = 1} },
5916 { "mvrg_3", send_to_rg, {.id = 2} },
5917 { "mvrg_4", send_to_rg, {.id = 3} },
5918 { "mvrg_5", send_to_rg, {.id = 4} },
5919 { "mvrg_6", send_to_rg, {.id = 5} },
5920 { "mvrg_7", send_to_rg, {.id = 6} },
5921 { "mvrg_8", send_to_rg, {.id = 7} },
5922 { "mvrg_9", send_to_rg, {.id = 8} },
5923 { "mvws_1", send_to_ws, {.id = 0} },
5924 { "mvws_2", send_to_ws, {.id = 1} },
5925 { "mvws_3", send_to_ws, {.id = 2} },
5926 { "mvws_4", send_to_ws, {.id = 3} },
5927 { "mvws_5", send_to_ws, {.id = 4} },
5928 { "mvws_6", send_to_ws, {.id = 5} },
5929 { "mvws_7", send_to_ws, {.id = 6} },
5930 { "mvws_8", send_to_ws, {.id = 7} },
5931 { "mvws_9", send_to_ws, {.id = 8} },
5932 { "mvws_10", send_to_ws, {.id = 9} },
5933 { "mvws_11", send_to_ws, {.id = 10} },
5934 { "mvws_12", send_to_ws, {.id = 11} },
5935 { "mvws_13", send_to_ws, {.id = 12} },
5936 { "mvws_14", send_to_ws, {.id = 13} },
5937 { "mvws_15", send_to_ws, {.id = 14} },
5938 { "mvws_16", send_to_ws, {.id = 15} },
5939 { "mvws_17", send_to_ws, {.id = 16} },
5940 { "mvws_18", send_to_ws, {.id = 17} },
5941 { "mvws_19", send_to_ws, {.id = 18} },
5942 { "mvws_20", send_to_ws, {.id = 19} },
5943 { "mvws_21", send_to_ws, {.id = 20} },
5944 { "mvws_22", send_to_ws, {.id = 21} },
5945 { "name_workspace", name_workspace, {0} },
5946 { "quit", quit, {0} },
5947 { "raise_toggle", raise_toggle, {0} },
5948 { "restart", restart, {0} },
5949 { "rg_1", focusrg, {.id = 0} },
5950 { "rg_2", focusrg, {.id = 1} },
5951 { "rg_3", focusrg, {.id = 2} },
5952 { "rg_4", focusrg, {.id = 3} },
5953 { "rg_5", focusrg, {.id = 4} },
5954 { "rg_6", focusrg, {.id = 5} },
5955 { "rg_7", focusrg, {.id = 6} },
5956 { "rg_8", focusrg, {.id = 7} },
5957 { "rg_9", focusrg, {.id = 8} },
5958 { "rg_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
5959 { "rg_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
5960 { "screen_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
5961 { "screen_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
5962 { "search_win", search_win, {0} },
5963 { "search_workspace", search_workspace, {0} },
5964 { "spawn_custom", NULL, {0} },
5965 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
5966 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
5967 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
5968 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
5969 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
5970 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
5971 { "uniconify", uniconify, {0} },
5972 { "version", version, {0} },
5973 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
5974 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
5975 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
5976 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
5977 { "ws_1", switchws, {.id = 0} },
5978 { "ws_2", switchws, {.id = 1} },
5979 { "ws_3", switchws, {.id = 2} },
5980 { "ws_4", switchws, {.id = 3} },
5981 { "ws_5", switchws, {.id = 4} },
5982 { "ws_6", switchws, {.id = 5} },
5983 { "ws_7", switchws, {.id = 6} },
5984 { "ws_8", switchws, {.id = 7} },
5985 { "ws_9", switchws, {.id = 8} },
5986 { "ws_10", switchws, {.id = 9} },
5987 { "ws_11", switchws, {.id = 10} },
5988 { "ws_12", switchws, {.id = 11} },
5989 { "ws_13", switchws, {.id = 12} },
5990 { "ws_14", switchws, {.id = 13} },
5991 { "ws_15", switchws, {.id = 14} },
5992 { "ws_16", switchws, {.id = 15} },
5993 { "ws_17", switchws, {.id = 16} },
5994 { "ws_18", switchws, {.id = 17} },
5995 { "ws_19", switchws, {.id = 18} },
5996 { "ws_20", switchws, {.id = 19} },
5997 { "ws_21", switchws, {.id = 20} },
5998 { "ws_22", switchws, {.id = 21} },
5999 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
6000 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
6001 { "ws_next_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_UP} },
6002 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
6003 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
6004 { "ws_prev_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_DOWN} },
6005 { "ws_prior", priorws, {0} },
6006 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
6007 { "invalid key func", NULL, {0} },
6008 };
6009
6010 int
6011 key_cmp(struct key *kp1, struct key *kp2)
6012 {
6013 if (kp1->keysym < kp2->keysym)
6014 return (-1);
6015 if (kp1->keysym > kp2->keysym)
6016 return (1);
6017
6018 if (kp1->mod < kp2->mod)
6019 return (-1);
6020 if (kp1->mod > kp2->mod)
6021 return (1);
6022
6023 return (0);
6024 }
6025
6026 /* mouse */
6027 enum { client_click, root_click };
6028 struct button {
6029 unsigned int action;
6030 unsigned int mask;
6031 unsigned int button;
6032 void (*func)(struct ws_win *, union arg *);
6033 union arg args;
6034 } buttons[] = {
6035 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6036 /* action key mouse button func args */
6037 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
6038 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
6039 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
6040 #undef MODKEY_SHIFT
6041 };
6042
6043 void
6044 update_modkey(unsigned int mod)
6045 {
6046 int i;
6047 struct key *kp;
6048
6049 mod_key = mod;
6050 RB_FOREACH(kp, key_tree, &keys)
6051 if (kp->mod & XCB_MOD_MASK_SHIFT)
6052 kp->mod = mod | XCB_MOD_MASK_SHIFT;
6053 else
6054 kp->mod = mod;
6055
6056 for (i = 0; i < LENGTH(buttons); i++)
6057 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
6058 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
6059 else
6060 buttons[i].mask = mod;
6061 }
6062
6063 int
6064 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
6065 char ***ret_args)
6066 {
6067 struct spawn_prog *prog = NULL;
6068 int i, c;
6069 char *ap, **real_args;
6070
6071 /* suppress unused warning since var is needed */
6072 (void)args;
6073
6074 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
6075
6076 /* find program */
6077 TAILQ_FOREACH(prog, &spawns, entry) {
6078 if (strcasecmp(spawn_name, prog->name) == 0)
6079 break;
6080 }
6081 if (prog == NULL) {
6082 warnx("spawn_custom: program %s not found", spawn_name);
6083 return (-1);
6084 }
6085
6086 /* make room for expanded args */
6087 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
6088 err(1, "spawn_custom: calloc real_args");
6089
6090 /* expand spawn_args into real_args */
6091 for (i = c = 0; i < prog->argc; i++) {
6092 ap = prog->argv[i];
6093 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
6094 if (strcasecmp(ap, "$bar_border") == 0) {
6095 if ((real_args[c] =
6096 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
6097 == NULL)
6098 err(1, "spawn_custom border color");
6099 } else if (strcasecmp(ap, "$bar_color") == 0) {
6100 if ((real_args[c] =
6101 strdup(r->s->c[SWM_S_COLOR_BAR].name))
6102 == NULL)
6103 err(1, "spawn_custom bar color");
6104 } else if (strcasecmp(ap, "$bar_font") == 0) {
6105 if ((real_args[c] = strdup(bar_fonts))
6106 == NULL)
6107 err(1, "spawn_custom bar fonts");
6108 } else if (strcasecmp(ap, "$bar_font_color") == 0) {
6109 if ((real_args[c] =
6110 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
6111 == NULL)
6112 err(1, "spawn_custom color font");
6113 } else if (strcasecmp(ap, "$color_focus") == 0) {
6114 if ((real_args[c] =
6115 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
6116 == NULL)
6117 err(1, "spawn_custom color focus");
6118 } else if (strcasecmp(ap, "$color_unfocus") == 0) {
6119 if ((real_args[c] =
6120 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
6121 == NULL)
6122 err(1, "spawn_custom color unfocus");
6123 } else if (strcasecmp(ap, "$region_index") == 0) {
6124 if (asprintf(&real_args[c], "%d",
6125 get_region_index(r) + 1) < 1)
6126 err(1, "spawn_custom region index");
6127 } else if (strcasecmp(ap, "$workspace_index") == 0) {
6128 if (asprintf(&real_args[c], "%d", r->ws->idx + 1) < 1)
6129 err(1, "spawn_custom workspace index");
6130 } else if (strcasecmp(ap, "$dmenu_bottom") == 0) {
6131 if (!bar_at_bottom)
6132 continue;
6133 if ((real_args[c] = strdup("-b")) == NULL)
6134 err(1, "spawn_custom workspace index");
6135 } else {
6136 /* no match --> copy as is */
6137 if ((real_args[c] = strdup(ap)) == NULL)
6138 err(1, "spawn_custom strdup(ap)");
6139 }
6140 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
6141 real_args[c]);
6142 ++c;
6143 }
6144
6145 #ifdef SWM_DEBUG
6146 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
6147 for (i = 0; i < c; ++i)
6148 DPRINTF("\"%s\" ", real_args[i]);
6149 DPRINTF("\n");
6150 #endif
6151 *ret_args = real_args;
6152 return (c);
6153 }
6154
6155 void
6156 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
6157 {
6158 union arg a;
6159 char **real_args;
6160 int spawn_argc, i;
6161
6162 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6163 return;
6164 a.argv = real_args;
6165 if (fork() == 0)
6166 spawn(r->ws->idx, &a, 1);
6167
6168 for (i = 0; i < spawn_argc; i++)
6169 free(real_args[i]);
6170 free(real_args);
6171 }
6172
6173 void
6174 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
6175 int *pid)
6176 {
6177 union arg a;
6178 char **real_args;
6179 int i, spawn_argc;
6180
6181 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6182 return;
6183 a.argv = real_args;
6184
6185 if (pipe(select_list_pipe) == -1)
6186 err(1, "pipe error");
6187 if (pipe(select_resp_pipe) == -1)
6188 err(1, "pipe error");
6189
6190 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
6191 err(1, "could not disable SIGPIPE");
6192 switch (*pid = fork()) {
6193 case -1:
6194 err(1, "cannot fork");
6195 break;
6196 case 0: /* child */
6197 if (dup2(select_list_pipe[0], STDIN_FILENO) == -1)
6198 err(1, "dup2");
6199 if (dup2(select_resp_pipe[1], STDOUT_FILENO) == -1)
6200 err(1, "dup2");
6201 close(select_list_pipe[1]);
6202 close(select_resp_pipe[0]);
6203 spawn(r->ws->idx, &a, 0);
6204 break;
6205 default: /* parent */
6206 close(select_list_pipe[0]);
6207 close(select_resp_pipe[1]);
6208 break;
6209 }
6210
6211 for (i = 0; i < spawn_argc; i++)
6212 free(real_args[i]);
6213 free(real_args);
6214 }
6215
6216 void
6217 spawn_insert(const char *name, const char *args, int flags)
6218 {
6219 char *arg, *cp, *ptr;
6220 struct spawn_prog *sp;
6221
6222 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
6223
6224 if ((sp = calloc(1, sizeof *sp)) == NULL)
6225 err(1, "spawn_insert: calloc");
6226 if ((sp->name = strdup(name)) == NULL)
6227 err(1, "spawn_insert: strdup");
6228
6229 /* convert the arguments to an argument list */
6230 if ((ptr = cp = strdup(args)) == NULL)
6231 err(1, "spawn_insert: strdup");
6232 while ((arg = strsep(&ptr, " \t")) != NULL) {
6233 /* empty field; skip it */
6234 if (*arg == '\0')
6235 continue;
6236
6237 sp->argc++;
6238 if ((sp->argv = realloc(sp->argv, sp->argc *
6239 sizeof *sp->argv)) == NULL)
6240 err(1, "spawn_insert: realloc");
6241 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
6242 err(1, "spawn_insert: strdup");
6243 }
6244 free(cp);
6245
6246 sp->flags = flags;
6247
6248 TAILQ_INSERT_TAIL(&spawns, sp, entry);
6249 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
6250 }
6251
6252 void
6253 spawn_remove(struct spawn_prog *sp)
6254 {
6255 int i;
6256
6257 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
6258
6259 TAILQ_REMOVE(&spawns, sp, entry);
6260 for (i = 0; i < sp->argc; i++)
6261 free(sp->argv[i]);
6262 free(sp->argv);
6263 free(sp->name);
6264 free(sp);
6265
6266 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
6267 }
6268
6269 struct spawn_prog*
6270 spawn_find(const char *name)
6271 {
6272 struct spawn_prog *sp;
6273
6274 TAILQ_FOREACH(sp, &spawns, entry)
6275 if (strcasecmp(sp->name, name) == 0)
6276 return sp;
6277
6278 return NULL;
6279 }
6280
6281 void
6282 setspawn(const char *name, const char *args, int flags)
6283 {
6284 struct spawn_prog *sp;
6285
6286 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
6287
6288 if (name == NULL)
6289 return;
6290
6291 /* Remove any old spawn under the same name. */
6292 if ((sp = spawn_find(name)) != NULL)
6293 spawn_remove(sp);
6294
6295 if (*args != '\0')
6296 spawn_insert(name, args, flags);
6297 else
6298 warnx("error: setspawn: cannot find program: %s", name);
6299
6300 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
6301 }
6302
6303 int
6304 setconfspawn(char *selector, char *value, int flags)
6305 {
6306 char *args;
6307
6308 args = expand_tilde(value);
6309
6310 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
6311
6312 setspawn(selector, args, flags);
6313 free(args);
6314
6315 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
6316 return (0);
6317 }
6318
6319 void
6320 validate_spawns(void)
6321 {
6322 struct spawn_prog *sp;
6323 char which[PATH_MAX];
6324 size_t i;
6325
6326 struct key *kp;
6327
6328 RB_FOREACH(kp, key_tree, &keys) {
6329 if (kp->funcid != KF_SPAWN_CUSTOM)
6330 continue;
6331
6332 /* find program */
6333 sp = spawn_find(kp->spawn_name);
6334 if (sp == NULL || sp->flags & SWM_SPAWN_OPTIONAL)
6335 continue;
6336
6337 /* verify we have the goods */
6338 snprintf(which, sizeof which, "which %s", sp->argv[0]);
6339 DNPRINTF(SWM_D_CONF, "validate_spawns: which %s\n",
6340 sp->argv[0]);
6341 for (i = strlen("which "); i < strlen(which); i++)
6342 if (which[i] == ' ') {
6343 which[i] = '\0';
6344 break;
6345 }
6346 if (system(which) != 0)
6347 add_startup_exception("could not find %s",
6348 &which[strlen("which ")]);
6349 }
6350 }
6351
6352 void
6353 setup_spawn(void)
6354 {
6355 setconfspawn("lock", "xlock", 0);
6356
6357 setconfspawn("term", "xterm", 0);
6358 setconfspawn("spawn_term", "xterm", 0);
6359
6360 setconfspawn("menu", "dmenu_run"
6361 " $dmenu_bottom"
6362 " -fn $bar_font"
6363 " -nb $bar_color"
6364 " -nf $bar_font_color"
6365 " -sb $bar_border"
6366 " -sf $bar_color", 0);
6367
6368 setconfspawn("search", "dmenu"
6369 " $dmenu_bottom"
6370 " -i"
6371 " -fn $bar_font"
6372 " -nb $bar_color"
6373 " -nf $bar_font_color"
6374 " -sb $bar_border"
6375 " -sf $bar_color", 0);
6376
6377 setconfspawn("name_workspace", "dmenu"
6378 " $dmenu_bottom"
6379 " -p Workspace"
6380 " -fn $bar_font"
6381 " -nb $bar_color"
6382 " -nf $bar_font_color"
6383 " -sb $bar_border"
6384 " -sf $bar_color", 0);
6385
6386 /* These are not verified for existence, even with a binding set. */
6387 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL);
6388 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL);
6389 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL);
6390 }
6391
6392 /* key bindings */
6393 #define SWM_MODNAME_SIZE 32
6394 #define SWM_KEY_WS "\n+ \t"
6395 int
6396 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
6397 {
6398 char *cp, *name;
6399 KeySym uks;
6400 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
6401 if (mod == NULL || ks == NULL) {
6402 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
6403 return (1);
6404 }
6405 if (keystr == NULL || strlen(keystr) == 0) {
6406 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
6407 return (1);
6408 }
6409 cp = keystr;
6410 *ks = XCB_NO_SYMBOL;
6411 *mod = 0;
6412 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
6413 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
6414 if (cp)
6415 cp += (long)strspn(cp, SWM_KEY_WS);
6416 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
6417 *mod |= currmod;
6418 else if (strncasecmp(name, "Mod1", SWM_MODNAME_SIZE) == 0)
6419 *mod |= XCB_MOD_MASK_1;
6420 else if (strncasecmp(name, "Mod2", SWM_MODNAME_SIZE) == 0)
6421 *mod += XCB_MOD_MASK_2;
6422 else if (strncmp(name, "Mod3", SWM_MODNAME_SIZE) == 0)
6423 *mod |= XCB_MOD_MASK_3;
6424 else if (strncmp(name, "Mod4", SWM_MODNAME_SIZE) == 0)
6425 *mod |= XCB_MOD_MASK_4;
6426 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
6427 *mod |= XCB_MOD_MASK_SHIFT;
6428 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
6429 *mod |= XCB_MOD_MASK_CONTROL;
6430 else {
6431 *ks = XStringToKeysym(name);
6432 XConvertCase(*ks, ks, &uks);
6433 if (ks == XCB_NO_SYMBOL) {
6434 DNPRINTF(SWM_D_KEY,
6435 "parsekeys: invalid key %s\n",
6436 name);
6437 return (1);
6438 }
6439 }
6440 }
6441 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
6442 return (0);
6443 }
6444
6445 char *
6446 strdupsafe(const char *str)
6447 {
6448 if (str == NULL)
6449 return (NULL);
6450 else
6451 return (strdup(str));
6452 }
6453
6454 void
6455 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6456 const char *spawn_name)
6457 {
6458 struct key *kp;
6459
6460 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
6461 keyfuncs[kfid].name, spawn_name);
6462
6463 if ((kp = malloc(sizeof *kp)) == NULL)
6464 err(1, "key_insert: malloc");
6465
6466 kp->mod = mod;
6467 kp->keysym = ks;
6468 kp->funcid = kfid;
6469 kp->spawn_name = strdupsafe(spawn_name);
6470 RB_INSERT(key_tree, &keys, kp);
6471
6472 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
6473 }
6474
6475 struct key *
6476 key_lookup(unsigned int mod, KeySym ks)
6477 {
6478 struct key kp;
6479
6480 kp.keysym = ks;
6481 kp.mod = mod;
6482
6483 return (RB_FIND(key_tree, &keys, &kp));
6484 }
6485
6486 void
6487 key_remove(struct key *kp)
6488 {
6489 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
6490
6491 RB_REMOVE(key_tree, &keys, kp);
6492 free(kp->spawn_name);
6493 free(kp);
6494
6495 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
6496 }
6497
6498 void
6499 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
6500 const char *spawn_name)
6501 {
6502 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
6503 spawn_name);
6504
6505 key_remove(kp);
6506 key_insert(mod, ks, kfid, spawn_name);
6507
6508 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
6509 }
6510
6511 void
6512 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6513 const char *spawn_name)
6514 {
6515 struct key *kp;
6516
6517 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
6518 keyfuncs[kfid].name, spawn_name);
6519
6520 if ((kp = key_lookup(mod, ks)) != NULL) {
6521 if (kfid == KF_INVALID)
6522 key_remove(kp);
6523 else
6524 key_replace(kp, mod, ks, kfid, spawn_name);
6525 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6526 return;
6527 }
6528 if (kfid == KF_INVALID) {
6529 warnx("bind: Key combination already unbound.");
6530 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6531 return;
6532 }
6533
6534 key_insert(mod, ks, kfid, spawn_name);
6535 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6536 }
6537
6538 int
6539 setconfbinding(char *selector, char *value, int flags)
6540 {
6541 enum keyfuncid kfid;
6542 unsigned int mod;
6543 KeySym ks;
6544 struct spawn_prog *sp;
6545
6546 /* suppress unused warning since var is needed */
6547 (void)flags;
6548
6549 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
6550 if (selector == NULL) {
6551 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
6552 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6553 kfid = KF_INVALID;
6554 setkeybinding(mod, ks, kfid, NULL);
6555 return (0);
6556 } else
6557 return (1);
6558 }
6559 /* search by key function name */
6560 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
6561 if (strncasecmp(selector, keyfuncs[kfid].name,
6562 SWM_FUNCNAME_LEN) == 0) {
6563 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
6564 "keyfunc\n", selector);
6565 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6566 setkeybinding(mod, ks, kfid, NULL);
6567 return (0);
6568 } else
6569 return (1);
6570 }
6571 }
6572 /* search by custom spawn name */
6573 if ((sp = spawn_find(selector)) != NULL) {
6574 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
6575 "spawn\n", selector);
6576 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6577 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
6578 sp->name);
6579 return (0);
6580 } else
6581 return (1);
6582 }
6583 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
6584 return (1);
6585 }
6586
6587 void
6588 setup_keys(void)
6589 {
6590 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6591 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
6592 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
6593 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
6594 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
6595 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
6596 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
6597 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
6598 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
6599 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
6600 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
6601 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
6602 setkeybinding(MODKEY, XK_u, KF_FOCUS_URGENT,NULL);
6603 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
6604 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
6605 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
6606 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
6607 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
6608 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
6609 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
6610 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
6611 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
6612 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
6613 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
6614 setkeybinding(MODKEY_SHIFT, XK_KP_End, KF_MVRG_1, NULL);
6615 setkeybinding(MODKEY_SHIFT, XK_KP_Down, KF_MVRG_2, NULL);
6616 setkeybinding(MODKEY_SHIFT, XK_KP_Next, KF_MVRG_3, NULL);
6617 setkeybinding(MODKEY_SHIFT, XK_KP_Left, KF_MVRG_4, NULL);
6618 setkeybinding(MODKEY_SHIFT, XK_KP_Begin, KF_MVRG_5, NULL);
6619 setkeybinding(MODKEY_SHIFT, XK_KP_Right, KF_MVRG_6, NULL);
6620 setkeybinding(MODKEY_SHIFT, XK_KP_Home, KF_MVRG_7, NULL);
6621 setkeybinding(MODKEY_SHIFT, XK_KP_Up, KF_MVRG_8, NULL);
6622 setkeybinding(MODKEY_SHIFT, XK_KP_Prior, KF_MVRG_9, NULL);
6623 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
6624 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
6625 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
6626 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
6627 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
6628 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
6629 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
6630 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
6631 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
6632 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
6633 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
6634 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
6635 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
6636 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
6637 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
6638 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
6639 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
6640 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
6641 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
6642 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
6643 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
6644 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
6645 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
6646 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
6647 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
6648 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
6649 setkeybinding(MODKEY, XK_KP_End, KF_RG_1, NULL);
6650 setkeybinding(MODKEY, XK_KP_Down, KF_RG_2, NULL);
6651 setkeybinding(MODKEY, XK_KP_Next, KF_RG_3, NULL);
6652 setkeybinding(MODKEY, XK_KP_Left, KF_RG_4, NULL);
6653 setkeybinding(MODKEY, XK_KP_Begin, KF_RG_5, NULL);
6654 setkeybinding(MODKEY, XK_KP_Right, KF_RG_6, NULL);
6655 setkeybinding(MODKEY, XK_KP_Home, KF_RG_7, NULL);
6656 setkeybinding(MODKEY, XK_KP_Up, KF_RG_8, NULL);
6657 setkeybinding(MODKEY, XK_KP_Prior, KF_RG_9, NULL);
6658 setkeybinding(MODKEY_SHIFT, XK_Right, KF_RG_NEXT, NULL);
6659 setkeybinding(MODKEY_SHIFT, XK_Left, KF_RG_PREV, NULL);
6660 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
6661 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
6662 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
6663 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
6664 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
6665 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
6666 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
6667 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
6668 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
6669 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
6670 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
6671 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
6672 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
6673 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
6674 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
6675 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
6676 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
6677 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
6678 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
6679 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
6680 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
6681 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
6682 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
6683 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
6684 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
6685 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
6686 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
6687 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
6688 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
6689 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
6690 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
6691 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
6692 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
6693 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
6694 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
6695 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
6696 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
6697 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
6698 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
6699 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
6700 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
6701 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
6702 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
6703 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
6704 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
6705 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
6706 setkeybinding(MODKEY_SHIFT, XK_Up, KF_WS_NEXT_MOVE,NULL);
6707 setkeybinding(MODKEY_SHIFT, XK_Down, KF_WS_PREV_MOVE,NULL);
6708 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
6709 #ifdef SWM_DEBUG
6710 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
6711 #endif
6712 #undef MODKEY_SHIFT
6713 }
6714
6715 void
6716 clear_keys(void)
6717 {
6718 struct key *kp;
6719
6720 while (RB_EMPTY(&keys) == 0) {
6721 kp = RB_ROOT(&keys);
6722 key_remove(kp);
6723 }
6724 }
6725
6726 int
6727 setkeymapping(char *selector, char *value, int flags)
6728 {
6729 char *keymapping_file;
6730
6731 /* suppress unused warnings since vars are needed */
6732 (void)selector;
6733 (void)flags;
6734
6735 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
6736
6737 keymapping_file = expand_tilde(value);
6738
6739 clear_keys();
6740 /* load new key bindings; if it fails, revert to default bindings */
6741 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
6742 clear_keys();
6743 setup_keys();
6744 }
6745
6746 free(keymapping_file);
6747
6748 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
6749 return (0);
6750 }
6751
6752 void
6753 updatenumlockmask(void)
6754 {
6755 unsigned int i, j;
6756 xcb_get_modifier_mapping_reply_t *modmap_r;
6757 xcb_keycode_t *modmap, kc, *keycode;
6758
6759 numlockmask = 0;
6760
6761 modmap_r = xcb_get_modifier_mapping_reply(conn,
6762 xcb_get_modifier_mapping(conn),
6763 NULL);
6764 if (modmap_r) {
6765 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
6766 for (i = 0; i < 8; i++) {
6767 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
6768 kc = modmap[i * modmap_r->keycodes_per_modifier
6769 + j];
6770 keycode = xcb_key_symbols_get_keycode(syms,
6771 XK_Num_Lock);
6772 if (kc == *keycode)
6773 numlockmask = (1 << i);
6774 free(keycode);
6775 }
6776 }
6777 free(modmap_r);
6778 }
6779 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
6780 }
6781
6782 void
6783 grabkeys(void)
6784 {
6785 struct key *kp;
6786 int num_screens, k, j;
6787 unsigned int modifiers[4];
6788 xcb_keycode_t *code;
6789
6790 DNPRINTF(SWM_D_MISC, "grabkeys\n");
6791 updatenumlockmask();
6792
6793 modifiers[0] = 0;
6794 modifiers[1] = numlockmask;
6795 modifiers[2] = XCB_MOD_MASK_LOCK;
6796 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6797
6798 num_screens = get_screen_count();
6799 for (k = 0; k < num_screens; k++) {
6800 if (TAILQ_EMPTY(&screens[k].rl))
6801 continue;
6802 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
6803 XCB_MOD_MASK_ANY);
6804 RB_FOREACH(kp, key_tree, &keys) {
6805 /* Skip unused ws binds. */
6806 if ((int)kp->funcid > KF_WS_1 + workspace_limit - 1 &&
6807 kp->funcid <= KF_WS_22)
6808 continue;
6809
6810 /* Skip unused mvws binds. */
6811 if ((int)kp->funcid > KF_MVWS_1 + workspace_limit - 1 &&
6812 kp->funcid <= KF_MVWS_22)
6813 continue;
6814
6815 if ((code = xcb_key_symbols_get_keycode(syms,
6816 kp->keysym))) {
6817 for (j = 0; j < LENGTH(modifiers); j++)
6818 xcb_grab_key(conn, 1,
6819 screens[k].root,
6820 kp->mod | modifiers[j],
6821 *code, XCB_GRAB_MODE_SYNC,
6822 XCB_GRAB_MODE_SYNC);
6823 free(code);
6824 }
6825 }
6826 }
6827 }
6828
6829 void
6830 grabbuttons(struct ws_win *win)
6831 {
6832 unsigned int modifiers[4];
6833 int i, j;
6834
6835 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win 0x%x\n", win->id);
6836 updatenumlockmask();
6837
6838 modifiers[0] = 0;
6839 modifiers[1] = numlockmask;
6840 modifiers[2] = XCB_MOD_MASK_LOCK;
6841 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6842
6843 for (i = 0; i < LENGTH(buttons); i++)
6844 if (buttons[i].action == client_click)
6845 for (j = 0; j < LENGTH(modifiers); ++j)
6846 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
6847 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
6848 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
6849 buttons[i].button, buttons[i].mask |
6850 modifiers[j]);
6851 }
6852
6853 const char *quirkname[] = {
6854 "NONE", /* config string for "no value" */
6855 "FLOAT",
6856 "TRANSSZ",
6857 "ANYWHERE",
6858 "XTERM_FONTADJ",
6859 "FULLSCREEN",
6860 "FOCUSPREV",
6861 "NOFOCUSONMAP",
6862 "FOCUSONMAP_SINGLE",
6863 };
6864
6865 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6866 #define SWM_Q_WS "\n|+ \t"
6867 int
6868 parsequirks(char *qstr, unsigned long *quirk)
6869 {
6870 char *cp, *name;
6871 int i;
6872
6873 if (quirk == NULL)
6874 return (1);
6875
6876 cp = qstr;
6877 *quirk = 0;
6878 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
6879 if (cp)
6880 cp += (long)strspn(cp, SWM_Q_WS);
6881 for (i = 0; i < LENGTH(quirkname); i++) {
6882 if (strncasecmp(name, quirkname[i],
6883 SWM_QUIRK_LEN) == 0) {
6884 DNPRINTF(SWM_D_QUIRK,
6885 "parsequirks: %s\n", name);
6886 if (i == 0) {
6887 *quirk = 0;
6888 return (0);
6889 }
6890 *quirk |= 1 << (i-1);
6891 break;
6892 }
6893 }
6894 if (i >= LENGTH(quirkname)) {
6895 DNPRINTF(SWM_D_QUIRK,
6896 "parsequirks: invalid quirk [%s]\n", name);
6897 return (1);
6898 }
6899 }
6900 return (0);
6901 }
6902
6903 void
6904 quirk_insert(const char *class, const char *name, unsigned long quirk)
6905 {
6906 struct quirk *qp;
6907
6908 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
6909 quirk);
6910
6911 if ((qp = malloc(sizeof *qp)) == NULL)
6912 err(1, "quirk_insert: malloc");
6913 if ((qp->class = strdup(class)) == NULL)
6914 err(1, "quirk_insert: strdup");
6915 if ((qp->name = strdup(name)) == NULL)
6916 err(1, "quirk_insert: strdup");
6917
6918 qp->quirk = quirk;
6919 TAILQ_INSERT_TAIL(&quirks, qp, entry);
6920
6921 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
6922 }
6923
6924 void
6925 quirk_remove(struct quirk *qp)
6926 {
6927 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
6928 qp->name, qp->quirk);
6929
6930 TAILQ_REMOVE(&quirks, qp, entry);
6931 free(qp->class);
6932 free(qp->name);
6933 free(qp);
6934
6935 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
6936 }
6937
6938 void
6939 quirk_replace(struct quirk *qp, const char *class, const char *name,
6940 unsigned long quirk)
6941 {
6942 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
6943 qp->name, qp->quirk);
6944
6945 quirk_remove(qp);
6946 quirk_insert(class, name, quirk);
6947
6948 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
6949 }
6950
6951 void
6952 setquirk(const char *class, const char *name, unsigned long quirk)
6953 {
6954 struct quirk *qp;
6955
6956 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
6957 quirk);
6958
6959 /* Remove/replace existing quirk. */
6960 TAILQ_FOREACH(qp, &quirks, entry) {
6961 if (strcmp(qp->class, class) == 0 &&
6962 strcmp(qp->name, name) == 0) {
6963 if (quirk == 0)
6964 quirk_remove(qp);
6965 else
6966 quirk_replace(qp, class, name, quirk);
6967 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6968 return;
6969 }
6970 }
6971
6972 /* Only insert if quirk is not NONE. */
6973 if (quirk)
6974 quirk_insert(class, name, quirk);
6975
6976 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6977 }
6978
6979 int
6980 setconfquirk(char *selector, char *value, int flags)
6981 {
6982 char *cp, *class, *name;
6983 int retval;
6984 unsigned long qrks;
6985
6986 /* suppress unused warning since var is needed */
6987 (void)flags;
6988
6989 if (selector == NULL)
6990 return (0);
6991 if ((cp = strchr(selector, ':')) == NULL)
6992 return (0);
6993 *cp = '\0';
6994 class = selector;
6995 name = cp + 1;
6996 if ((retval = parsequirks(value, &qrks)) == 0)
6997 setquirk(class, name, qrks);
6998 return (retval);
6999 }
7000
7001 void
7002 setup_quirks(void)
7003 {
7004 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
7005 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
7006 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
7007 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
7008 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7009 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
7010 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7011 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7012 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7013 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7014 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
7015 setquirk("pcb", "pcb", SWM_Q_FLOAT);
7016 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
7017 }
7018
7019 /* conf file stuff */
7020 #define SWM_CONF_FILE "spectrwm.conf"
7021 #define SWM_CONF_FILE_OLD "scrotwm.conf"
7022
7023 enum {
7024 SWM_S_BAR_ACTION,
7025 SWM_S_BAR_AT_BOTTOM,
7026 SWM_S_BAR_BORDER_WIDTH,
7027 SWM_S_BAR_DELAY,
7028 SWM_S_BAR_ENABLED,
7029 SWM_S_BAR_ENABLED_WS,
7030 SWM_S_BAR_FONT,
7031 SWM_S_BAR_FORMAT,
7032 SWM_S_BAR_JUSTIFY,
7033 SWM_S_BORDER_WIDTH,
7034 SWM_S_BOUNDARY_WIDTH,
7035 SWM_S_CLOCK_ENABLED,
7036 SWM_S_CLOCK_FORMAT,
7037 SWM_S_CYCLE_EMPTY,
7038 SWM_S_CYCLE_VISIBLE,
7039 SWM_S_DIALOG_RATIO,
7040 SWM_S_DISABLE_BORDER,
7041 SWM_S_FOCUS_CLOSE,
7042 SWM_S_FOCUS_CLOSE_WRAP,
7043 SWM_S_FOCUS_DEFAULT,
7044 SWM_S_FOCUS_MODE,
7045 SWM_S_REGION_PADDING,
7046 SWM_S_SPAWN_ORDER,
7047 SWM_S_SPAWN_TERM,
7048 SWM_S_SS_APP,
7049 SWM_S_SS_ENABLED,
7050 SWM_S_STACK_ENABLED,
7051 SWM_S_TERM_WIDTH,
7052 SWM_S_TILE_GAP,
7053 SWM_S_TITLE_CLASS_ENABLED,
7054 SWM_S_TITLE_NAME_ENABLED,
7055 SWM_S_URGENT_ENABLED,
7056 SWM_S_VERBOSE_LAYOUT,
7057 SWM_S_WINDOW_NAME_ENABLED,
7058 SWM_S_WORKSPACE_LIMIT
7059 };
7060
7061 int
7062 setconfvalue(char *selector, char *value, int flags)
7063 {
7064 struct workspace *ws;
7065 int i, ws_id, num_screens;
7066 char *b;
7067
7068 /* suppress unused warning since var is needed */
7069 (void)selector;
7070
7071 switch (flags) {
7072 case SWM_S_BAR_ACTION:
7073 free(bar_argv[0]);
7074 if ((bar_argv[0] = expand_tilde(value)) == NULL)
7075 err(1, "setconfvalue: bar_action");
7076 break;
7077 case SWM_S_BAR_AT_BOTTOM:
7078 bar_at_bottom = atoi(value);
7079 break;
7080 case SWM_S_BAR_BORDER_WIDTH:
7081 bar_border_width = atoi(value);
7082 if (bar_border_width < 0)
7083 bar_border_width = 0;
7084 break;
7085 case SWM_S_BAR_DELAY:
7086 /* No longer needed; leave to not break old conf files. */
7087 break;
7088 case SWM_S_BAR_ENABLED:
7089 bar_enabled = atoi(value);
7090 break;
7091 case SWM_S_BAR_ENABLED_WS:
7092 ws_id = atoi(selector) - 1;
7093 if (ws_id < 0 || ws_id >= workspace_limit)
7094 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7095 "workspace %d.", ws_id + 1);
7096
7097 num_screens = get_screen_count();
7098 for (i = 0; i < num_screens; i++) {
7099 ws = (struct workspace *)&screens[i].ws;
7100 ws[ws_id].bar_enabled = atoi(value);
7101 }
7102 break;
7103 case SWM_S_BAR_FONT:
7104 b = bar_fonts;
7105 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
7106 err(1, "setconfvalue: asprintf: failed to allocate "
7107 "memory for bar_fonts.");
7108 free(b);
7109
7110 /* If already in xft mode, then we are done. */
7111 if (!bar_font_legacy)
7112 break;
7113
7114 /* If there are any non-XLFD entries, switch to Xft mode. */
7115 while ((b = strsep(&value, ",")) != NULL) {
7116 if (*b == '\0')
7117 continue;
7118 if (!isxlfd(b)) {
7119 bar_font_legacy = 0;
7120 break;
7121 }
7122 }
7123 break;
7124 case SWM_S_BAR_FORMAT:
7125 free(bar_format);
7126 if ((bar_format = strdup(value)) == NULL)
7127 err(1, "setconfvalue: bar_format");
7128 break;
7129 case SWM_S_BAR_JUSTIFY:
7130 if (strcmp(value, "left") == 0)
7131 bar_justify = SWM_BAR_JUSTIFY_LEFT;
7132 else if (strcmp(value, "center") == 0)
7133 bar_justify = SWM_BAR_JUSTIFY_CENTER;
7134 else if (strcmp(value, "right") == 0)
7135 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
7136 else
7137 errx(1, "invalid bar_justify");
7138 break;
7139 case SWM_S_BORDER_WIDTH:
7140 border_width = atoi(value);
7141 if (border_width < 0)
7142 border_width = 0;
7143 break;
7144 case SWM_S_BOUNDARY_WIDTH:
7145 boundary_width = atoi(value);
7146 if (boundary_width < 0)
7147 boundary_width = 0;
7148 break;
7149 case SWM_S_CLOCK_ENABLED:
7150 clock_enabled = atoi(value);
7151 break;
7152 case SWM_S_CLOCK_FORMAT:
7153 #ifndef SWM_DENY_CLOCK_FORMAT
7154 free(clock_format);
7155 if ((clock_format = strdup(value)) == NULL)
7156 err(1, "setconfvalue: clock_format");
7157 #endif
7158 break;
7159 case SWM_S_CYCLE_EMPTY:
7160 cycle_empty = atoi(value);
7161 break;
7162 case SWM_S_CYCLE_VISIBLE:
7163 cycle_visible = atoi(value);
7164 break;
7165 case SWM_S_DIALOG_RATIO:
7166 dialog_ratio = atof(value);
7167 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
7168 dialog_ratio = .6;
7169 break;
7170 case SWM_S_DISABLE_BORDER:
7171 disable_border = atoi(value);
7172 break;
7173 case SWM_S_FOCUS_CLOSE:
7174 if (strcmp(value, "first") == 0)
7175 focus_close = SWM_STACK_BOTTOM;
7176 else if (strcmp(value, "last") == 0)
7177 focus_close = SWM_STACK_TOP;
7178 else if (strcmp(value, "next") == 0)
7179 focus_close = SWM_STACK_ABOVE;
7180 else if (strcmp(value, "previous") == 0)
7181 focus_close = SWM_STACK_BELOW;
7182 else
7183 errx(1, "focus_close");
7184 break;
7185 case SWM_S_FOCUS_CLOSE_WRAP:
7186 focus_close_wrap = atoi(value);
7187 break;
7188 case SWM_S_FOCUS_DEFAULT:
7189 if (strcmp(value, "last") == 0)
7190 focus_default = SWM_STACK_TOP;
7191 else if (strcmp(value, "first") == 0)
7192 focus_default = SWM_STACK_BOTTOM;
7193 else
7194 errx(1, "focus_default");
7195 break;
7196 case SWM_S_FOCUS_MODE:
7197 if (strcmp(value, "default") == 0)
7198 focus_mode = SWM_FOCUS_DEFAULT;
7199 else if (strcmp(value, "follow") == 0 ||
7200 strcmp(value, "follow_cursor") == 0)
7201 focus_mode = SWM_FOCUS_FOLLOW;
7202 else if (strcmp(value, "manual") == 0)
7203 focus_mode = SWM_FOCUS_MANUAL;
7204 else
7205 errx(1, "focus_mode");
7206 break;
7207 case SWM_S_REGION_PADDING:
7208 region_padding = atoi(value);
7209 if (region_padding < 0)
7210 region_padding = 0;
7211 break;
7212 case SWM_S_SPAWN_ORDER:
7213 if (strcmp(value, "first") == 0)
7214 spawn_position = SWM_STACK_BOTTOM;
7215 else if (strcmp(value, "last") == 0)
7216 spawn_position = SWM_STACK_TOP;
7217 else if (strcmp(value, "next") == 0)
7218 spawn_position = SWM_STACK_ABOVE;
7219 else if (strcmp(value, "previous") == 0)
7220 spawn_position = SWM_STACK_BELOW;
7221 else
7222 errx(1, "spawn_position");
7223 break;
7224 case SWM_S_SPAWN_TERM:
7225 setconfspawn("term", value, 0);
7226 setconfspawn("spawn_term", value, 0);
7227 break;
7228 case SWM_S_SS_APP:
7229 /* No longer needed; leave to not break old conf files. */
7230 break;
7231 case SWM_S_SS_ENABLED:
7232 /* No longer needed; leave to not break old conf files. */
7233 break;
7234 case SWM_S_STACK_ENABLED:
7235 stack_enabled = atoi(value);
7236 break;
7237 case SWM_S_TERM_WIDTH:
7238 term_width = atoi(value);
7239 if (term_width < 0)
7240 term_width = 0;
7241 break;
7242 case SWM_S_TILE_GAP:
7243 tile_gap = atoi(value);
7244 break;
7245 case SWM_S_TITLE_CLASS_ENABLED:
7246 title_class_enabled = atoi(value);
7247 break;
7248 case SWM_S_TITLE_NAME_ENABLED:
7249 title_name_enabled = atoi(value);
7250 break;
7251 case SWM_S_URGENT_ENABLED:
7252 urgent_enabled = atoi(value);
7253 break;
7254 case SWM_S_VERBOSE_LAYOUT:
7255 verbose_layout = atoi(value);
7256 for (i = 0; layouts[i].l_stack != NULL; i++) {
7257 if (verbose_layout)
7258 layouts[i].l_string = fancy_stacker;
7259 else
7260 layouts[i].l_string = plain_stacker;
7261 }
7262 break;
7263 case SWM_S_WINDOW_NAME_ENABLED:
7264 window_name_enabled = atoi(value);
7265 break;
7266 case SWM_S_WORKSPACE_LIMIT:
7267 workspace_limit = atoi(value);
7268 if (workspace_limit > SWM_WS_MAX)
7269 workspace_limit = SWM_WS_MAX;
7270 else if (workspace_limit < 1)
7271 workspace_limit = 1;
7272 break;
7273 default:
7274 return (1);
7275 }
7276 return (0);
7277 }
7278
7279 int
7280 setconfmodkey(char *selector, char *value, int flags)
7281 {
7282 /* suppress unused warnings since vars are needed */
7283 (void)selector;
7284 (void)flags;
7285
7286 if (strncasecmp(value, "Mod1", strlen("Mod1")) == 0)
7287 update_modkey(XCB_MOD_MASK_1);
7288 else if (strncasecmp(value, "Mod2", strlen("Mod2")) == 0)
7289 update_modkey(XCB_MOD_MASK_2);
7290 else if (strncasecmp(value, "Mod3", strlen("Mod3")) == 0)
7291 update_modkey(XCB_MOD_MASK_3);
7292 else if (strncasecmp(value, "Mod4", strlen("Mod4")) == 0)
7293 update_modkey(XCB_MOD_MASK_4);
7294 else
7295 return (1);
7296 return (0);
7297 }
7298
7299 int
7300 setconfcolor(char *selector, char *value, int flags)
7301 {
7302 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
7303 return (0);
7304 }
7305
7306 int
7307 setconfregion(char *selector, char *value, int flags)
7308 {
7309 /* suppress unused warnings since vars are needed */
7310 (void)selector;
7311 (void)flags;
7312
7313 custom_region(value);
7314 return (0);
7315 }
7316
7317 int
7318 setautorun(char *selector, char *value, int flags)
7319 {
7320 int ws_id;
7321 char s[1024];
7322 char *ap, *sp;
7323 union arg a;
7324 int argc = 0;
7325 pid_t pid;
7326 struct pid_e *p;
7327
7328 /* suppress unused warnings since vars are needed */
7329 (void)selector;
7330 (void)flags;
7331
7332 if (getenv("SWM_STARTED"))
7333 return (0);
7334
7335 bzero(s, sizeof s);
7336 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
7337 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
7338 ws_id--;
7339 if (ws_id < 0 || ws_id >= workspace_limit)
7340 errx(1, "autorun: invalid workspace %d", ws_id + 1);
7341
7342 sp = expand_tilde((char *)&s);
7343
7344 /*
7345 * This is a little intricate
7346 *
7347 * If the pid already exists we simply reuse it because it means it was
7348 * used before AND not claimed by manage_window. We get away with
7349 * altering it in the parent after INSERT because this can not be a race
7350 */
7351 a.argv = NULL;
7352 while ((ap = strsep(&sp, " \t")) != NULL) {
7353 if (*ap == '\0')
7354 continue;
7355 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
7356 argc++;
7357 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
7358 err(1, "setautorun: realloc");
7359 a.argv[argc - 1] = ap;
7360 }
7361 free(sp);
7362
7363 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
7364 err(1, "setautorun: realloc");
7365 a.argv[argc] = NULL;
7366
7367 if ((pid = fork()) == 0) {
7368 spawn(ws_id, &a, 1);
7369 /* NOTREACHED */
7370 _exit(1);
7371 }
7372 free(a.argv);
7373
7374 /* parent */
7375 p = find_pid(pid);
7376 if (p == NULL) {
7377 p = calloc(1, sizeof *p);
7378 if (p == NULL)
7379 return (1);
7380 TAILQ_INSERT_TAIL(&pidlist, p, entry);
7381 }
7382
7383 p->pid = pid;
7384 p->ws = ws_id;
7385
7386 return (0);
7387 }
7388
7389 int
7390 setlayout(char *selector, char *value, int flags)
7391 {
7392 int ws_id, i, x, mg, ma, si, ar, f = 0;
7393 int st = SWM_V_STACK, num_screens;
7394 char s[1024];
7395 struct workspace *ws;
7396
7397 /* suppress unused warnings since vars are needed */
7398 (void)selector;
7399 (void)flags;
7400
7401 if (getenv("SWM_STARTED"))
7402 return (0);
7403
7404 bzero(s, sizeof s);
7405 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
7406 &ws_id, &mg, &ma, &si, &ar, s) != 6)
7407 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7408 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7409 "<type>'");
7410 ws_id--;
7411 if (ws_id < 0 || ws_id >= workspace_limit)
7412 errx(1, "layout: invalid workspace %d", ws_id + 1);
7413
7414 if (strcasecmp(s, "vertical") == 0)
7415 st = SWM_V_STACK;
7416 else if (strcasecmp(s, "vertical_flip") == 0) {
7417 st = SWM_V_STACK;
7418 f = 1;
7419 } else if (strcasecmp(s, "horizontal") == 0)
7420 st = SWM_H_STACK;
7421 else if (strcasecmp(s, "horizontal_flip") == 0) {
7422 st = SWM_H_STACK;
7423 f = 1;
7424 } else if (strcasecmp(s, "fullscreen") == 0)
7425 st = SWM_MAX_STACK;
7426 else
7427 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7428 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7429 "<type>'");
7430
7431 num_screens = get_screen_count();
7432 for (i = 0; i < num_screens; i++) {
7433 ws = (struct workspace *)&screens[i].ws;
7434 ws[ws_id].cur_layout = &layouts[st];
7435
7436 ws[ws_id].always_raise = ar;
7437 if (st == SWM_MAX_STACK)
7438 continue;
7439
7440 /* master grow */
7441 for (x = 0; x < abs(mg); x++) {
7442 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7443 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
7444 SWM_ARG_ID_MASTERSHRINK);
7445 stack();
7446 }
7447 /* master add */
7448 for (x = 0; x < abs(ma); x++) {
7449 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7450 ma >= 0 ? SWM_ARG_ID_MASTERADD :
7451 SWM_ARG_ID_MASTERDEL);
7452 stack();
7453 }
7454 /* stack inc */
7455 for (x = 0; x < abs(si); x++) {
7456 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7457 si >= 0 ? SWM_ARG_ID_STACKINC :
7458 SWM_ARG_ID_STACKDEC);
7459 stack();
7460 }
7461 /* Apply flip */
7462 if (f) {
7463 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7464 SWM_ARG_ID_FLIPLAYOUT);
7465 stack();
7466 }
7467 }
7468
7469 focus_flush();
7470
7471 return (0);
7472 }
7473
7474 /* config options */
7475 struct config_option {
7476 char *optname;
7477 int (*func)(char*, char*, int);
7478 int funcflags;
7479 };
7480 struct config_option configopt[] = {
7481 { "autorun", setautorun, 0 },
7482 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
7483 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
7484 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
7485 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
7486 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
7487 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
7488 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
7489 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
7490 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
7491 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
7492 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
7493 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
7494 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
7495 { "bind", setconfbinding, 0 },
7496 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
7497 { "boundary_width", setconfvalue, SWM_S_BOUNDARY_WIDTH },
7498 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
7499 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
7500 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
7501 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
7502 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
7503 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
7504 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
7505 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
7506 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
7507 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
7508 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
7509 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
7510 { "keyboard_mapping", setkeymapping, 0 },
7511 { "layout", setlayout, 0 },
7512 { "modkey", setconfmodkey, 0 },
7513 { "program", setconfspawn, 0 },
7514 { "quirk", setconfquirk, 0 },
7515 { "region", setconfregion, 0 },
7516 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
7517 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
7518 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
7519 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
7520 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
7521 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
7522 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
7523 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
7524 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
7525 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
7526 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
7527 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
7528 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
7529 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
7530 };
7531
7532 void
7533 _add_startup_exception(const char *fmt, va_list ap)
7534 {
7535 if (vasprintf(&startup_exception, fmt, ap) == -1)
7536 warn("%s: asprintf", __func__);
7537 }
7538
7539 void
7540 add_startup_exception(const char *fmt, ...)
7541 {
7542 va_list ap;
7543
7544 nr_exceptions++;
7545
7546 if (startup_exception)
7547 return;
7548
7549 /* force bar to be enabled due to exception */
7550 bar_enabled = 1;
7551
7552 va_start(ap, fmt);
7553 _add_startup_exception(fmt, ap);
7554 va_end(ap);
7555 }
7556
7557 int
7558 conf_load(const char *filename, int keymapping)
7559 {
7560 FILE *config;
7561 char *line = NULL, *cp, *optsub, *optval = NULL;
7562 size_t linelen, lineno = 0;
7563 int wordlen, i, optidx;
7564 struct config_option *opt = NULL;
7565
7566 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
7567
7568 if (filename == NULL) {
7569 warnx("conf_load: no filename");
7570 return (1);
7571 }
7572
7573 DNPRINTF(SWM_D_CONF, "conf_load: open %s\n", filename);
7574
7575 if ((config = fopen(filename, "r")) == NULL) {
7576 warn("conf_load: fopen: %s", filename);
7577 return (1);
7578 }
7579
7580 while (!feof(config)) {
7581 if (line)
7582 free(line);
7583
7584 if ((line = fparseln(config, &linelen, &lineno, NULL,
7585 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT)) == NULL) {
7586 if (ferror(config))
7587 err(1, "%s", filename);
7588 else
7589 continue;
7590 }
7591 cp = line;
7592 cp += strspn(cp, " \t\n"); /* eat whitespace */
7593 if (cp[0] == '\0') {
7594 /* empty line */
7595 continue;
7596 }
7597 /* get config option */
7598 wordlen = strcspn(cp, "=[ \t\n");
7599 if (wordlen == 0) {
7600 add_startup_exception("%s: line %zd: no option found",
7601 filename, lineno);
7602 continue;
7603 }
7604 optidx = -1;
7605 for (i = 0; i < LENGTH(configopt); i++) {
7606 opt = &configopt[i];
7607 if (strncasecmp(cp, opt->optname, wordlen) == 0 &&
7608 (int)strlen(opt->optname) == wordlen) {
7609 optidx = i;
7610 break;
7611 }
7612 }
7613 if (optidx == -1) {
7614 add_startup_exception("%s: line %zd: unknown option "
7615 "%.*s", filename, lineno, wordlen, cp);
7616 continue;
7617 }
7618 if (keymapping && opt && strcmp(opt->optname, "bind")) {
7619 add_startup_exception("%s: line %zd: invalid option "
7620 "%.*s", filename, lineno, wordlen, cp);
7621 continue;
7622 }
7623 cp += wordlen;
7624 cp += strspn(cp, " \t\n"); /* eat whitespace */
7625
7626 /* from here on out we call goto invalid to continue */
7627
7628 /* get [selector] if any */
7629 optsub = NULL;
7630 if (*cp == '[') {
7631 cp++;
7632 wordlen = strcspn(cp, "]");
7633 if (*cp != ']') {
7634 if (wordlen == 0) {
7635 add_startup_exception("%s: line %zd: "
7636 "syntax error", filename, lineno);
7637 goto invalid;
7638 }
7639
7640 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
7641 -1) {
7642 add_startup_exception("%s: line %zd: "
7643 "unable to allocatememory for "
7644 "selector", filename, lineno);
7645 goto invalid;
7646 }
7647 }
7648 cp += wordlen;
7649 cp += strspn(cp, "] \t\n"); /* eat trailing */
7650 }
7651 cp += strspn(cp, "= \t\n"); /* eat trailing */
7652 /* get RHS value */
7653 optval = strdup(cp);
7654 if (strlen(optval) == 0) {
7655 add_startup_exception("%s: line %zd: must supply value "
7656 "to %s", filename, lineno,
7657 configopt[optidx].optname);
7658 goto invalid;
7659 }
7660 /* call function to deal with it all */
7661 if (configopt[optidx].func(optsub, optval,
7662 configopt[optidx].funcflags) != 0) {
7663 add_startup_exception("%s: line %zd: invalid data for "
7664 "%s", filename, lineno, configopt[optidx].optname);
7665 goto invalid;
7666 }
7667 invalid:
7668 if (optval) {
7669 free(optval);
7670 optval = NULL;
7671 }
7672 if (optsub) {
7673 free(optsub);
7674 optsub = NULL;
7675 }
7676 }
7677
7678 if (line)
7679 free(line);
7680 fclose(config);
7681 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
7682
7683 return (0);
7684 }
7685
7686 void
7687 set_child_transient(struct ws_win *win, xcb_window_t *trans)
7688 {
7689 struct ws_win *parent, *w;
7690 struct swm_region *r;
7691 struct workspace *ws;
7692 xcb_icccm_wm_hints_t wmh;
7693
7694 parent = find_window(win->transient);
7695 if (parent)
7696 parent->focus_child = win;
7697 else {
7698 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
7699 " for 0x%x trans 0x%x\n", win->id, win->transient);
7700
7701 r = root_to_region(win->wa->root, SWM_CK_ALL);
7702 ws = r->ws;
7703 /* parent doen't exist in our window list */
7704 TAILQ_FOREACH(w, &ws->winlist, entry) {
7705 if (xcb_icccm_get_wm_hints_reply(conn,
7706 xcb_icccm_get_wm_hints(conn, w->id),
7707 &wmh, NULL) != 1) {
7708 warnx("can't get hints for 0x%x", w->id);
7709 continue;
7710 }
7711
7712 if (win->hints.window_group != wmh.window_group)
7713 continue;
7714
7715 w->focus_child = win;
7716 win->transient = w->id;
7717 *trans = w->id;
7718 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
7719 "transient to 0x%x\n", win->transient);
7720 break;
7721 }
7722 }
7723 }
7724
7725 pid_t
7726 window_get_pid(xcb_window_t win)
7727 {
7728 pid_t ret = 0;
7729 const char *errstr;
7730 xcb_atom_t apid;
7731 xcb_get_property_cookie_t pc;
7732 xcb_get_property_reply_t *pr;
7733
7734 apid = get_atom_from_string("_NET_WM_PID");
7735 if (apid == XCB_ATOM_NONE)
7736 goto tryharder;
7737
7738 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
7739 pr = xcb_get_property_reply(conn, pc, NULL);
7740 if (pr == NULL)
7741 goto tryharder;
7742 if (pr->type != XCB_ATOM_CARDINAL) {
7743 free(pr);
7744 goto tryharder;
7745 }
7746
7747 if (pr->type == apid && pr->format == 32)
7748 ret = *((pid_t *)xcb_get_property_value(pr));
7749 free(pr);
7750
7751 return (ret);
7752
7753 tryharder:
7754 apid = get_atom_from_string("_SWM_PID");
7755 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
7756 0, SWM_PROPLEN);
7757 pr = xcb_get_property_reply(conn, pc, NULL);
7758 if (pr == NULL)
7759 return (0);
7760 if (pr->type != apid) {
7761 free(pr);
7762 return (0);
7763 }
7764
7765 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
7766 free(pr);
7767
7768 return (ret);
7769 }
7770
7771 int
7772 get_ws_idx(xcb_window_t id)
7773 {
7774 int ws_idx = -1;
7775 char *prop = NULL;
7776 size_t proplen;
7777 const char *errstr;
7778 xcb_get_property_reply_t *gpr;
7779
7780 gpr = xcb_get_property_reply(conn,
7781 xcb_get_property(conn, 0, id, a_swm_ws,
7782 XCB_ATOM_STRING, 0, SWM_PROPLEN),
7783 NULL);
7784 if (gpr == NULL)
7785 return (-1);
7786 if (gpr->type) {
7787 proplen = xcb_get_property_value_length(gpr);
7788 if (proplen > 0) {
7789 prop = malloc(proplen + 1);
7790 if (prop) {
7791 memcpy(prop,
7792 xcb_get_property_value(gpr),
7793 proplen);
7794 prop[proplen] = '\0';
7795 }
7796 }
7797 }
7798 free(gpr);
7799
7800 if (prop) {
7801 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
7802 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
7803 if (errstr) {
7804 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
7805 errstr, prop);
7806 }
7807 free(prop);
7808 }
7809
7810 return ws_idx;
7811 }
7812
7813 struct ws_win *
7814 manage_window(xcb_window_t id, uint16_t mapped)
7815 {
7816 xcb_window_t trans = XCB_WINDOW_NONE;
7817 struct ws_win *win, *ww;
7818 int ws_idx;
7819 char ws_idx_str[SWM_PROPLEN];
7820 struct swm_region *r;
7821 struct pid_e *p;
7822 struct quirk *qp;
7823 uint32_t i, wa[2];
7824 xcb_icccm_get_wm_protocols_reply_t wpr;
7825
7826 if ((win = find_window(id)) != NULL) {
7827 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x already "
7828 "managed; skipping.)\n", id);
7829 return (win); /* Already managed. */
7830 }
7831
7832 /* See if window is on the unmanaged list. */
7833 if ((win = find_unmanaged_window(id)) != NULL) {
7834 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x found on "
7835 "unmanaged list.\n", id);
7836 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7837
7838 if (win->transient)
7839 set_child_transient(win, &trans);
7840
7841 goto out;
7842 } else {
7843 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x is new.\n", id);
7844 }
7845
7846 /* Create and initialize ws_win object. */
7847 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
7848 err(1, "manage_window: calloc: failed to allocate memory for "
7849 "new window");
7850
7851 win->id = id;
7852
7853 /* Get window geometry. */
7854 win->wa = xcb_get_geometry_reply(conn,
7855 xcb_get_geometry(conn, win->id),
7856 NULL);
7857
7858 /* Figure out which region the window belongs to. */
7859 r = root_to_region(win->wa->root, SWM_CK_ALL);
7860
7861 /* Ignore window border if there is one. */
7862 WIDTH(win) = win->wa->width;
7863 HEIGHT(win) = win->wa->height;
7864 X(win) = win->wa->x + win->wa->border_width - border_width;
7865 Y(win) = win->wa->y + win->wa->border_width - border_width;
7866 win->bordered = 1;
7867 win->mapped = mapped;
7868 win->floatmaxed = 0;
7869 win->ewmh_flags = 0;
7870 win->s = r->s; /* this never changes */
7871
7872 /* Get WM_SIZE_HINTS. */
7873 xcb_icccm_get_wm_normal_hints_reply(conn,
7874 xcb_icccm_get_wm_normal_hints(conn, win->id),
7875 &win->sh, NULL);
7876
7877 /* Get WM_HINTS. */
7878 xcb_icccm_get_wm_hints_reply(conn,
7879 xcb_icccm_get_wm_hints(conn, win->id),
7880 &win->hints, NULL);
7881
7882 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7883 xcb_icccm_get_wm_transient_for_reply(conn,
7884 xcb_icccm_get_wm_transient_for(conn, win->id),
7885 &trans, NULL);
7886 if (trans) {
7887 win->transient = trans;
7888 set_child_transient(win, &win->transient);
7889 }
7890
7891 /* Get supported protocols. */
7892 if (xcb_icccm_get_wm_protocols_reply(conn,
7893 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
7894 &wpr, NULL)) {
7895 for (i = 0; i < wpr.atoms_len; i++) {
7896 if (wpr.atoms[i] == a_takefocus)
7897 win->take_focus = 1;
7898 if (wpr.atoms[i] == a_delete)
7899 win->can_delete = 1;
7900 }
7901 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
7902 }
7903
7904 win->iconic = get_swm_iconic(win);
7905
7906 /* Figure out which workspace the window belongs to. */
7907 if ((p = find_pid(window_get_pid(win->id))) != NULL) {
7908 win->ws = &r->s->ws[p->ws];
7909 TAILQ_REMOVE(&pidlist, p, entry);
7910 free(p);
7911 p = NULL;
7912 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
7913 !win->transient) {
7914 /* _SWM_WS is set; use that. */
7915 win->ws = &r->s->ws[ws_idx];
7916 } else if (trans && (ww = find_window(trans)) != NULL) {
7917 /* Launch transients in the same ws as parent. */
7918 win->ws = ww->ws;
7919 } else {
7920 win->ws = r->ws;
7921 }
7922
7923 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7924 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", win->ws->idx) <
7925 SWM_PROPLEN) {
7926 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
7927 ws_idx_str);
7928 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
7929 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
7930 ws_idx_str);
7931 }
7932
7933 /* WS must already be set for this to work. */
7934 store_float_geom(win);
7935
7936 /* Handle EWMH */
7937 ewmh_autoquirk(win);
7938
7939 /* Determine initial quirks. */
7940 if (xcb_icccm_get_wm_class_reply(conn,
7941 xcb_icccm_get_wm_class(conn, win->id),
7942 &win->ch, NULL)) {
7943 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
7944 win->ch.class_name, win->ch.instance_name);
7945
7946 /* java is retarded so treat it special */
7947 if (strstr(win->ch.instance_name, "sun-awt")) {
7948 DNPRINTF(SWM_D_CLASS, "manage_window: java window "
7949 "detected.\n");
7950 win->java = 1;
7951 }
7952
7953 TAILQ_FOREACH(qp, &quirks, entry) {
7954 if (strcmp(win->ch.class_name, qp->class) == 0 &&
7955 strcmp(win->ch.instance_name, qp->name) == 0) {
7956 DNPRINTF(SWM_D_CLASS, "manage_window: on quirks"
7957 "list; mask: 0x%lx\n", qp->quirk);
7958 if (qp->quirk & SWM_Q_FLOAT)
7959 win->floating = 1;
7960 win->quirks = qp->quirk;
7961 }
7962 }
7963 }
7964
7965 /* Alter window position if quirky */
7966 if (win->quirks & SWM_Q_ANYWHERE)
7967 win->manual = 1;
7968
7969 /* Reset font sizes (the bruteforce way; no default keybinding). */
7970 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
7971 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7972 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
7973 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7974 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
7975 }
7976
7977 /* Make sure window is positioned inside its region, if its active. */
7978 if (win->ws->r) {
7979 region_containment(win, r, SWM_CW_ALLSIDES |
7980 SWM_CW_HARDBOUNDARY);
7981 update_window(win);
7982 }
7983
7984 /* Select which X events to monitor and set border pixel color. */
7985 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
7986 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
7987 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
7988 #ifdef SWM_DEBUG
7989 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
7990 #endif
7991
7992 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
7993 XCB_CW_EVENT_MASK, wa);
7994
7995 out:
7996 /* Figure out where to stack the window in the workspace. */
7997 if (trans && (ww = find_window(trans)))
7998 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
7999 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
8000 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
8001 entry);
8002 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
8003 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
8004 else switch (spawn_position) {
8005 default:
8006 case SWM_STACK_TOP:
8007 case SWM_STACK_ABOVE:
8008 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
8009 break;
8010 case SWM_STACK_BOTTOM:
8011 case SWM_STACK_BELOW:
8012 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
8013 }
8014
8015 /* Get initial _NET_WM_STATE */
8016 ewmh_get_win_state(win);
8017 /* Set initial _NET_WM_ALLOWED_ACTIONS */
8018 ewmh_update_actions(win);
8019
8020 grabbuttons(win);
8021
8022 DNPRINTF(SWM_D_MISC, "manage_window: done. window: 0x%x, (x,y) w x h: "
8023 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win->id,
8024 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
8025 YESNO(win->iconic), win->transient);
8026
8027 return (win);
8028 }
8029
8030 void
8031 free_window(struct ws_win *win)
8032 {
8033 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
8034
8035 if (win == NULL)
8036 return;
8037
8038 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
8039
8040 if (win->wa)
8041 free(win->wa);
8042
8043 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
8044
8045 kill_refs(win);
8046
8047 /* paint memory */
8048 memset(win, 0xff, sizeof *win); /* XXX kill later */
8049
8050 free(win);
8051 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
8052 }
8053
8054 void
8055 unmanage_window(struct ws_win *win)
8056 {
8057 struct ws_win *parent;
8058
8059 if (win == NULL)
8060 return;
8061
8062 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
8063
8064 if (win->transient) {
8065 parent = find_window(win->transient);
8066 if (parent)
8067 parent->focus_child = NULL;
8068 }
8069
8070 TAILQ_REMOVE(&win->ws->winlist, win, entry);
8071 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
8072 }
8073
8074 void
8075 expose(xcb_expose_event_t *e)
8076 {
8077 int i, num_screens;
8078 struct swm_region *r;
8079
8080 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
8081
8082 num_screens = get_screen_count();
8083 for (i = 0; i < num_screens; i++)
8084 TAILQ_FOREACH(r, &screens[i].rl, entry)
8085 if (e->window == WINID(r->bar))
8086 bar_draw();
8087
8088 xcb_flush(conn);
8089 }
8090
8091 #ifdef SWM_DEBUG
8092 void
8093 focusin(xcb_focus_in_event_t *e)
8094 {
8095 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
8096 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
8097 e->mode, get_notify_detail_label(e->detail), e->detail);
8098 }
8099
8100 void
8101 focusout(xcb_focus_out_event_t *e)
8102 {
8103 DNPRINTF(SWM_D_EVENT, "focusout: window: 0x%x, mode: %s(%u), "
8104 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
8105 e->mode, get_notify_detail_label(e->detail), e->detail);
8106 }
8107 #endif
8108
8109 void
8110 keypress(xcb_key_press_event_t *e)
8111 {
8112 xcb_keysym_t keysym;
8113 struct key *kp;
8114
8115 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
8116
8117 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): 0x%x (%d,%d), "
8118 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
8119 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
8120 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
8121 e->child, e->state, YESNO(e->same_screen));
8122
8123 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
8124 goto out;
8125
8126 last_event_time = e->time;
8127
8128 if (kp->funcid == KF_SPAWN_CUSTOM)
8129 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
8130 &(keyfuncs[kp->funcid].args), kp->spawn_name);
8131 else if (keyfuncs[kp->funcid].func)
8132 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
8133 &(keyfuncs[kp->funcid].args));
8134
8135 out:
8136 /* Unfreeze grab events. */
8137 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD, e->time);
8138 xcb_flush(conn);
8139
8140 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
8141 }
8142
8143 void
8144 buttonpress(xcb_button_press_event_t *e)
8145 {
8146 struct ws_win *win = NULL;
8147 struct swm_region *r, *old_r;
8148 int i;
8149 int handled = 0;
8150
8151 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): 0x%x (%d,%d), "
8152 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
8153 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
8154 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
8155 e->state, YESNO(e->same_screen));
8156
8157 if (e->event == e->root) {
8158 if (e->child != 0) {
8159 win = find_window(e->child);
8160 /* Pass ButtonPress to window if it isn't managed. */
8161 if (win == NULL)
8162 goto out;
8163 } else {
8164 /* Focus on empty region */
8165 /* If no windows on region if its empty. */
8166 r = root_to_region(e->root, SWM_CK_POINTER);
8167 if (r == NULL) {
8168 DNPRINTF(SWM_D_EVENT, "buttonpress: "
8169 "NULL region; ignoring.\n");
8170 goto out;
8171 }
8172
8173 if (TAILQ_EMPTY(&r->ws->winlist)) {
8174 old_r = root_to_region(e->root, SWM_CK_FOCUS);
8175 if (old_r && old_r != r)
8176 unfocus_win(old_r->ws->focus);
8177
8178 xcb_set_input_focus(conn,
8179 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
8180
8181 /* Clear bar since empty. */
8182 bar_draw();
8183
8184 handled = 1;
8185 goto out;
8186 }
8187 }
8188 } else {
8189 win = find_window(e->event);
8190 }
8191
8192 if (win == NULL)
8193 goto out;
8194
8195 last_event_time = e->time;
8196
8197 focus_win(get_focus_magic(win));
8198
8199 for (i = 0; i < LENGTH(buttons); i++)
8200 if (client_click == buttons[i].action && buttons[i].func &&
8201 buttons[i].button == e->detail &&
8202 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
8203 buttons[i].func(win, &buttons[i].args);
8204 handled = 1;
8205 }
8206
8207 out:
8208 if (!handled) {
8209 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
8210 /* Replay event to event window */
8211 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
8212 } else {
8213 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
8214 /* Unfreeze grab events. */
8215 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
8216 }
8217
8218 xcb_flush(conn);
8219 }
8220
8221 #ifdef SWM_DEBUG
8222 void
8223 print_win_geom(xcb_window_t w)
8224 {
8225 xcb_get_geometry_reply_t *wa;
8226
8227 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
8228 if (wa == NULL) {
8229 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
8230 w);
8231 return;
8232 }
8233
8234 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
8235 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
8236 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
8237 wa->border_width);
8238
8239 free(wa);
8240 }
8241 #endif
8242
8243 #ifdef SWM_DEBUG
8244 char *
8245 get_stack_mode_name(uint8_t mode)
8246 {
8247 char *name;
8248
8249 switch(mode) {
8250 case XCB_STACK_MODE_ABOVE:
8251 name = "Above";
8252 break;
8253 case XCB_STACK_MODE_BELOW:
8254 name = "Below";
8255 break;
8256 case XCB_STACK_MODE_TOP_IF:
8257 name = "TopIf";
8258 break;
8259 case XCB_STACK_MODE_BOTTOM_IF:
8260 name = "BottomIf";
8261 break;
8262 case XCB_STACK_MODE_OPPOSITE:
8263 name = "Opposite";
8264 break;
8265 default:
8266 name = "Unknown";
8267 }
8268
8269 return name;
8270 }
8271 #endif
8272
8273 void
8274 configurerequest(xcb_configure_request_event_t *e)
8275 {
8276 struct ws_win *win;
8277 struct swm_region *r = NULL;
8278 int new = 0, i = 0;
8279 uint16_t mask = 0;
8280 uint32_t wc[7] = {0};
8281
8282 if ((win = find_window(e->window)) == NULL)
8283 if ((win = find_unmanaged_window(e->window)) == NULL)
8284 new = 1;
8285
8286 #ifdef SWM_DEBUG
8287 if (swm_debug & SWM_D_EVENT) {
8288 print_win_geom(e->window);
8289
8290 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%x, "
8291 "parent: 0x%x, new: %s, value_mask: %u { ", e->window,
8292 e->parent, YESNO(new), e->value_mask);
8293 if (e->value_mask & XCB_CONFIG_WINDOW_X)
8294 DPRINTF("X: %d ", e->x);
8295 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
8296 DPRINTF("Y: %d ", e->y);
8297 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
8298 DPRINTF("W: %u ", e->width);
8299 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
8300 DPRINTF("H: %u ", e->height);
8301 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
8302 DPRINTF("Border: %u ", e->border_width);
8303 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
8304 DPRINTF("Sibling: 0x%x ", e->sibling);
8305 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
8306 DPRINTF("StackMode: %s(%u) ",
8307 get_stack_mode_name(e->stack_mode), e->stack_mode);
8308 DPRINTF("}\n");
8309 }
8310 #endif
8311
8312 if (new) {
8313 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
8314 mask |= XCB_CONFIG_WINDOW_X;
8315 wc[i++] = e->x;
8316 }
8317 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
8318 mask |= XCB_CONFIG_WINDOW_Y;
8319 wc[i++] = e->y;
8320 }
8321 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
8322 mask |= XCB_CONFIG_WINDOW_WIDTH;
8323 wc[i++] = e->width;
8324 }
8325 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
8326 mask |= XCB_CONFIG_WINDOW_HEIGHT;
8327 wc[i++] = e->height;
8328 }
8329 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
8330 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
8331 wc[i++] = e->border_width;
8332 }
8333 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
8334 mask |= XCB_CONFIG_WINDOW_SIBLING;
8335 wc[i++] = e->sibling;
8336 }
8337 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
8338 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
8339 wc[i++] = e->stack_mode;
8340 }
8341
8342 if (mask != 0) {
8343 xcb_configure_window(conn, e->window, mask, wc);
8344 xcb_flush(conn);
8345 }
8346 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
8347 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
8348 if (win->ws->r)
8349 r = win->ws->r;
8350 else if (win->ws->old_r)
8351 r = win->ws->old_r;
8352
8353 /* windows are centered unless ANYWHERE quirk is set. */
8354 if (win->quirks & SWM_Q_ANYWHERE) {
8355 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
8356 win->g_float.x = e->x;
8357 if (r)
8358 win->g_float.x -= X(r);
8359 }
8360
8361 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
8362 win->g_float.y = e->y;
8363 if (r)
8364 win->g_float.y -= Y(r);
8365 }
8366 }
8367
8368 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
8369 win->g_float.w = e->width;
8370
8371 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
8372 win->g_float.h = e->height;
8373
8374 win->g_floatvalid = 1;
8375
8376 if (win->floating && r && (win->transient ||
8377 win->ws->cur_layout != &layouts[SWM_MAX_STACK])) {
8378 WIDTH(win) = win->g_float.w;
8379 HEIGHT(win) = win->g_float.h;
8380
8381 if (r) {
8382 stack_floater(win, r);
8383 focus_flush();
8384 }
8385 } else {
8386 config_win(win, e);
8387 xcb_flush(conn);
8388 }
8389 } else {
8390 config_win(win, e);
8391 xcb_flush(conn);
8392 }
8393
8394 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
8395 }
8396
8397 void
8398 configurenotify(xcb_configure_notify_event_t *e)
8399 {
8400 struct ws_win *win;
8401
8402 DNPRINTF(SWM_D_EVENT, "configurenotify: win 0x%x, event win: 0x%x, "
8403 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
8404 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
8405 e->width, e->height, e->border_width, e->above_sibling,
8406 YESNO(e->override_redirect));
8407
8408 win = find_window(e->window);
8409 if (win) {
8410 xcb_icccm_get_wm_normal_hints_reply(conn,
8411 xcb_icccm_get_wm_normal_hints(conn, win->id),
8412 &win->sh, NULL);
8413 adjust_font(win);
8414 if (font_adjusted) {
8415 stack();
8416 xcb_flush(conn);
8417 }
8418 }
8419 }
8420
8421 void
8422 destroynotify(xcb_destroy_notify_event_t *e)
8423 {
8424 struct ws_win *win;
8425
8426 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
8427
8428 if ((win = find_window(e->window)) == NULL) {
8429 if ((win = find_unmanaged_window(e->window)) == NULL)
8430 return;
8431 free_window(win);
8432 return;
8433 }
8434
8435 if (focus_mode != SWM_FOCUS_FOLLOW) {
8436 /* If we were focused, make sure we focus on something else. */
8437 if (win == win->ws->focus)
8438 win->ws->focus_pending = get_focus_prev(win);
8439 }
8440
8441 unmanage_window(win);
8442 stack();
8443
8444 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
8445 if (win->ws->focus_pending) {
8446 focus_win(win->ws->focus_pending);
8447 win->ws->focus_pending = NULL;
8448 } else if (win == win->ws->focus) {
8449 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
8450 win->ws->r->id, XCB_CURRENT_TIME);
8451 }
8452 }
8453
8454 free_window(win);
8455
8456 focus_flush();
8457 }
8458
8459 #ifdef SWM_DEBUG
8460 char *
8461 get_notify_detail_label(uint8_t detail)
8462 {
8463 char *label;
8464
8465 switch (detail) {
8466 case XCB_NOTIFY_DETAIL_ANCESTOR:
8467 label = "Ancestor";
8468 break;
8469 case XCB_NOTIFY_DETAIL_VIRTUAL:
8470 label = "Virtual";
8471 break;
8472 case XCB_NOTIFY_DETAIL_INFERIOR:
8473 label = "Inferior";
8474 break;
8475 case XCB_NOTIFY_DETAIL_NONLINEAR:
8476 label = "Nonlinear";
8477 break;
8478 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
8479 label = "NonlinearVirtual";
8480 break;
8481 case XCB_NOTIFY_DETAIL_POINTER:
8482 label = "Pointer";
8483 break;
8484 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
8485 label = "PointerRoot";
8486 break;
8487 case XCB_NOTIFY_DETAIL_NONE:
8488 label = "None";
8489 break;
8490 default:
8491 label = "Unknown";
8492 }
8493
8494 return label;
8495 }
8496
8497 char *
8498 get_notify_mode_label(uint8_t mode)
8499 {
8500 char *label;
8501
8502 switch (mode) {
8503 case XCB_NOTIFY_MODE_NORMAL:
8504 label = "Normal";
8505 break;
8506 case XCB_NOTIFY_MODE_GRAB:
8507 label = "Grab";
8508 break;
8509 case XCB_NOTIFY_MODE_UNGRAB:
8510 label = "Ungrab";
8511 break;
8512 case XCB_NOTIFY_MODE_WHILE_GRABBED:
8513 label = "WhileGrabbed";
8514 break;
8515 default:
8516 label = "Unknown";
8517 }
8518
8519 return label;
8520 }
8521 #endif
8522
8523 void
8524 enternotify(xcb_enter_notify_event_t *e)
8525 {
8526 struct ws_win *win;
8527 struct swm_region *r;
8528
8529 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): 0x%x "
8530 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8531 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8532 e->time, e->event, e->event_x, e->event_y,
8533 get_notify_mode_label(e->mode), e->mode,
8534 get_notify_detail_label(e->detail), e->detail,
8535 e->root, e->root_x, e->root_y, e->child,
8536 YESNO(e->same_screen_focus), e->state);
8537
8538 if (focus_mode == SWM_FOCUS_MANUAL &&
8539 e->mode == XCB_NOTIFY_MODE_NORMAL) {
8540 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
8541 return;
8542 }
8543
8544 last_event_time = e->time;
8545
8546 if ((win = find_window(e->event)) == NULL) {
8547 if (e->event == e->root) {
8548 /* If no windows on pointer region, then focus root. */
8549 r = root_to_region(e->root, SWM_CK_POINTER);
8550 if (r == NULL) {
8551 DNPRINTF(SWM_D_EVENT, "enternotify: "
8552 "NULL region; ignoring.\n");
8553 return;
8554 }
8555
8556 focus_region(r);
8557 } else {
8558 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
8559 "ignoring\n");
8560 return;
8561 }
8562 } else {
8563 focus_win(get_focus_magic(win));
8564 }
8565
8566 xcb_flush(conn);
8567 }
8568
8569 #ifdef SWM_DEBUG
8570 void
8571 leavenotify(xcb_leave_notify_event_t *e)
8572 {
8573 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): 0x%x "
8574 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8575 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8576 e->time, e->event, e->event_x, e->event_y,
8577 get_notify_mode_label(e->mode), e->mode,
8578 get_notify_detail_label(e->detail), e->detail,
8579 e->root, e->root_x, e->root_y, e->child,
8580 YESNO(e->same_screen_focus), e->state);
8581 }
8582 #endif
8583
8584 void
8585 mapnotify(xcb_map_notify_event_t *e)
8586 {
8587 struct ws_win *win;
8588
8589 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
8590
8591 if ((win = find_window(e->window)) == NULL)
8592 win = manage_window(e->window, 1);
8593
8594 win->mapped = 1;
8595 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
8596
8597 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
8598 if (win->ws->focus_pending == win) {
8599 focus_win(win);
8600 win->ws->focus_pending = NULL;
8601 focus_flush();
8602 }
8603 }
8604
8605 xcb_flush(conn);
8606 }
8607
8608 void
8609 mappingnotify(xcb_mapping_notify_event_t *e)
8610 {
8611 xcb_refresh_keyboard_mapping(syms, e);
8612
8613 if (e->request == XCB_MAPPING_KEYBOARD)
8614 grabkeys();
8615 }
8616
8617 void
8618 maprequest(xcb_map_request_event_t *e)
8619 {
8620 struct ws_win *win, *w = NULL;
8621 xcb_get_window_attributes_reply_t *war;
8622
8623 DNPRINTF(SWM_D_EVENT, "maprequest: win 0x%x\n",
8624 e->window);
8625
8626 war = xcb_get_window_attributes_reply(conn,
8627 xcb_get_window_attributes(conn, e->window),
8628 NULL);
8629 if (war == NULL) {
8630 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
8631 goto out;
8632 }
8633
8634 if (war->override_redirect) {
8635 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
8636 "skipping.\n");
8637 goto out;
8638 }
8639
8640 win = manage_window(e->window,
8641 (war->map_state == XCB_MAP_STATE_VIEWABLE));
8642
8643 /* The new window should get focus; prepare. */
8644 if (focus_mode != SWM_FOCUS_FOLLOW &&
8645 !(win->quirks & SWM_Q_NOFOCUSONMAP)) {
8646 if (win->quirks & SWM_Q_FOCUSONMAP_SINGLE) {
8647 /* See if other wins of same type are already mapped. */
8648 TAILQ_FOREACH(w, &win->ws->winlist, entry) {
8649 if (w == win || !w->mapped)
8650 continue;
8651
8652 if (w->ch.class_name &&
8653 win->ch.class_name &&
8654 strcmp(w->ch.class_name,
8655 win->ch.class_name) == 0 &&
8656 w->ch.instance_name &&
8657 win->ch.instance_name &&
8658 strcmp(w->ch.instance_name,
8659 win->ch.instance_name) == 0)
8660 break;
8661 }
8662 }
8663
8664 if (w == NULL)
8665 win->ws->focus_pending = get_focus_magic(win);
8666 }
8667
8668 /* All windows need to be mapped if they are in the current workspace.*/
8669 if (win->ws->r)
8670 stack();
8671
8672 /* Ignore EnterNotify to handle the mapnotify without interference. */
8673 if (focus_mode == SWM_FOCUS_DEFAULT)
8674 event_drain(XCB_ENTER_NOTIFY);
8675 out:
8676 free(war);
8677 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
8678 }
8679
8680 void
8681 motionnotify(xcb_motion_notify_event_t *e)
8682 {
8683 struct swm_region *r;
8684 int i, num_screens;
8685
8686 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): 0x%x "
8687 "(%d,%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8688 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8689 e->time, e->event, e->event_x, e->event_y,
8690 get_notify_detail_label(e->detail), e->detail,
8691 e->root, e->root_x, e->root_y, e->child,
8692 YESNO(e->same_screen), e->state);
8693
8694 last_event_time = e->time;
8695
8696 if (focus_mode == SWM_FOCUS_MANUAL)
8697 return;
8698
8699 num_screens = get_screen_count();
8700 for (i = 0; i < num_screens; i++)
8701 if (screens[i].root == e->root)
8702 break;
8703
8704 TAILQ_FOREACH(r, &screens[i].rl, entry)
8705 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
8706 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
8707 break;
8708
8709 focus_region(r);
8710 }
8711
8712 #ifdef SWM_DEBUG
8713 char *
8714 get_atom_name(xcb_atom_t atom)
8715 {
8716 char *name = NULL;
8717 #if 0
8718 /*
8719 * This should be disabled during most debugging since
8720 * xcb_get_* causes an xcb_flush.
8721 */
8722 size_t len;
8723 xcb_get_atom_name_reply_t *r;
8724
8725 r = xcb_get_atom_name_reply(conn,
8726 xcb_get_atom_name(conn, atom),
8727 NULL);
8728 if (r) {
8729 len = xcb_get_atom_name_name_length(r);
8730 if (len > 0) {
8731 name = malloc(len + 1);
8732 if (name) {
8733 memcpy(name, xcb_get_atom_name_name(r), len);
8734 name[len] = '\0';
8735 }
8736 }
8737 free(r);
8738 }
8739 #else
8740 (void)atom;
8741 #endif
8742 return (name);
8743 }
8744 #endif
8745
8746 void
8747 propertynotify(xcb_property_notify_event_t *e)
8748 {
8749 struct ws_win *win;
8750 struct workspace *ws;
8751 #ifdef SWM_DEBUG
8752 char *name;
8753
8754 name = get_atom_name(e->atom);
8755 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u), "
8756 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
8757 e->state);
8758 free(name);
8759 #endif
8760 win = find_window(e->window);
8761 if (win == NULL)
8762 return;
8763
8764 ws = win->ws;
8765
8766 last_event_time = e->time;
8767
8768 if (e->atom == a_swm_iconic) {
8769 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8770 if (focus_mode != SWM_FOCUS_FOLLOW)
8771 ws->focus_pending = get_focus_prev(win);
8772
8773 unfocus_win(win);
8774 unmap_window(win);
8775
8776 if (ws->r) {
8777 stack();
8778
8779 if (focus_mode != SWM_FOCUS_FOLLOW &&
8780 WS_FOCUSED(ws)) {
8781 if (ws->focus_pending) {
8782 focus_win(ws->focus_pending);
8783 ws->focus_pending = NULL;
8784 } else {
8785 xcb_set_input_focus(conn,
8786 XCB_INPUT_FOCUS_PARENT,
8787 ws->r->id,
8788 XCB_CURRENT_TIME);
8789 }
8790 }
8791
8792 focus_flush();
8793 }
8794 } else if (e->state == XCB_PROPERTY_DELETE) {
8795 /* The window is no longer iconic, restack ws. */
8796 if (focus_mode != SWM_FOCUS_FOLLOW)
8797 ws->focus_pending = get_focus_magic(win);
8798
8799 stack();
8800
8801 /* Flush EnterNotify for mapnotify, if needed. */
8802 focus_flush();
8803 }
8804 } else if (e->atom == a_state) {
8805 /* State just changed, make sure it gets focused if mapped. */
8806 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8807 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(ws)) {
8808 if (win->mapped &&
8809 ws->focus_pending == win) {
8810 focus_win(ws->focus_pending);
8811 ws->focus_pending = NULL;
8812 }
8813 }
8814 }
8815 } else if (e->atom == XCB_ATOM_WM_CLASS ||
8816 e->atom == XCB_ATOM_WM_NAME) {
8817 bar_draw();
8818 }
8819
8820 xcb_flush(conn);
8821 }
8822
8823 void
8824 unmapnotify(xcb_unmap_notify_event_t *e)
8825 {
8826 struct ws_win *win;
8827 struct workspace *ws;
8828
8829 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
8830
8831 /* If we aren't managing the window, then ignore. */
8832 win = find_window(e->window);
8833 if (win == NULL || win->id != e->window)
8834 return;
8835
8836 ws = win->ws;
8837
8838 if (getstate(e->window) != XCB_ICCCM_WM_STATE_ICONIC)
8839 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8840
8841 if (win->mapped) {
8842 /* window unmapped itself */
8843 /* do unmap/unfocus/restack and unmanage */
8844 win->mapped = 0;
8845
8846 /* If win was focused, make sure to focus on something else. */
8847 if (win == ws->focus) {
8848 if (focus_mode != SWM_FOCUS_FOLLOW) {
8849 ws->focus_pending = get_focus_prev(win);
8850 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
8851 "focus_pending: 0x%x\n",
8852 WINID(ws->focus_pending));
8853 }
8854
8855 unfocus_win(win);
8856 }
8857
8858 unmanage_window(win);
8859
8860 if (ws->r)
8861 stack();
8862
8863 if (WS_FOCUSED(ws)) {
8864 if (focus_mode == SWM_FOCUS_FOLLOW) {
8865 focus_win(get_pointer_win(ws->r->s->root));
8866 } else if (ws->focus_pending) {
8867 focus_win(ws->focus_pending);
8868 ws->focus_pending = NULL;
8869 } else if (ws->focus == NULL) {
8870 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
8871 ws->r->id, XCB_CURRENT_TIME);
8872 }
8873 }
8874 }
8875
8876 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL)
8877 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8878
8879 focus_flush();
8880 }
8881
8882 #if 0
8883 void
8884 visibilitynotify(xcb_visibility_notify_event_t *e)
8885 {
8886 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
8887 e->window);
8888 }
8889 #endif
8890
8891 void
8892 clientmessage(xcb_client_message_event_t *e)
8893 {
8894 struct ws_win *win;
8895 xcb_map_request_event_t mre;
8896 #ifdef SWM_DEBUG
8897 char *name;
8898
8899 name = get_atom_name(e->type);
8900 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
8901 e->window, name, e->type);
8902 free(name);
8903 #endif
8904 win = find_window(e->window);
8905
8906 if (win == NULL) {
8907 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8908 /* Manage the window with maprequest. */
8909 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
8910 "unmanaged window.\n");
8911 mre.window = e->window;
8912 maprequest(&mre);
8913 }
8914 return;
8915 }
8916
8917 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8918 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
8919 if (WS_FOCUSED(win->ws))
8920 focus_win(win);
8921 else
8922 win->ws->focus_pending = win;
8923 }
8924 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
8925 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
8926 if (win->can_delete)
8927 client_msg(win, a_delete, 0);
8928 else
8929 xcb_kill_client(conn, win->id);
8930 }
8931 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
8932 DNPRINTF(SWM_D_EVENT,
8933 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
8934 if (win->floating) {
8935 if (e->data.data32[0] & (1<<8)) /* x */
8936 X(win) = e->data.data32[1];
8937 if (e->data.data32[0] & (1<<9)) /* y */
8938 Y(win) = e->data.data32[2];
8939 if (e->data.data32[0] & (1<<10)) /* width */
8940 WIDTH(win) = e->data.data32[3];
8941 if (e->data.data32[0] & (1<<11)) /* height */
8942 HEIGHT(win) = e->data.data32[4];
8943
8944 update_window(win);
8945 }
8946 else {
8947 /* TODO: Change stack sizes */
8948 /* notify no change was made. */
8949 config_win(win, NULL);
8950 }
8951 }
8952 if (e->type == ewmh[_NET_WM_STATE].atom) {
8953 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
8954 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
8955 if (e->data.data32[2])
8956 ewmh_update_win_state(win, e->data.data32[2],
8957 e->data.data32[0]);
8958
8959 stack();
8960 }
8961
8962 focus_flush();
8963 }
8964
8965 void
8966 check_conn(void)
8967 {
8968 int errcode = xcb_connection_has_error(conn);
8969 #ifdef XCB_CONN_ERROR
8970 char *s;
8971 switch (errcode) {
8972 case XCB_CONN_ERROR:
8973 s = "Socket error, pipe error or other stream error.";
8974 break;
8975 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
8976 s = "Extension not supported.";
8977 break;
8978 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
8979 s = "Insufficient memory.";
8980 break;
8981 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
8982 s = "Request length was exceeded.";
8983 break;
8984 case XCB_CONN_CLOSED_PARSE_ERR:
8985 s = "Error parsing display string.";
8986 break;
8987 default:
8988 s = "Unknown error.";
8989 }
8990 if (errcode)
8991 errx(errcode, "X CONNECTION ERROR: %s", s);
8992 #else
8993 if (errcode)
8994 errx(errcode, "X CONNECTION ERROR");
8995 #endif
8996 }
8997
8998 int
8999 enable_wm(void)
9000 {
9001 int num_screens, i;
9002 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
9003 XCB_EVENT_MASK_ENTER_WINDOW;
9004 xcb_screen_t *sc;
9005 xcb_void_cookie_t wac;
9006 xcb_generic_error_t *error;
9007
9008 /* this causes an error if some other window manager is running */
9009 num_screens = get_screen_count();
9010 for (i = 0; i < num_screens; i++) {
9011 if ((sc = get_screen(i)) == NULL)
9012 errx(1, "ERROR: can't get screen %d.", i);
9013 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
9014 i, sc->root);
9015 wac = xcb_change_window_attributes_checked(conn, sc->root,
9016 XCB_CW_EVENT_MASK, &val);
9017 if ((error = xcb_request_check(conn, wac))) {
9018 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
9019 error->error_code);
9020 free(error);
9021 return 1;
9022 }
9023
9024 /* click to focus on empty region */
9025 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
9026 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
9027 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
9028 }
9029
9030 return 0;
9031 }
9032
9033 void
9034 new_region(struct swm_screen *s, int x, int y, int w, int h)
9035 {
9036 struct swm_region *r, *n;
9037 struct workspace *ws = NULL;
9038 int i;
9039 uint32_t wa[1];
9040
9041 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
9042 s->idx, w, h, x, y);
9043
9044 /* remove any conflicting regions */
9045 n = TAILQ_FIRST(&s->rl);
9046 while (n) {
9047 r = n;
9048 n = TAILQ_NEXT(r, entry);
9049 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
9050 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
9051 if (r->ws->r != NULL)
9052 r->ws->old_r = r->ws->r;
9053 r->ws->r = NULL;
9054 bar_cleanup(r);
9055 xcb_destroy_window(conn, r->id);
9056 TAILQ_REMOVE(&s->rl, r, entry);
9057 TAILQ_INSERT_TAIL(&s->orl, r, entry);
9058 }
9059 }
9060
9061 /* search old regions for one to reuse */
9062
9063 /* size + location match */
9064 TAILQ_FOREACH(r, &s->orl, entry)
9065 if (X(r) == x && Y(r) == y &&
9066 HEIGHT(r) == h && WIDTH(r) == w)
9067 break;
9068
9069 /* size match */
9070 TAILQ_FOREACH(r, &s->orl, entry)
9071 if (HEIGHT(r) == h && WIDTH(r) == w)
9072 break;
9073
9074 if (r != NULL) {
9075 TAILQ_REMOVE(&s->orl, r, entry);
9076 /* try to use old region's workspace */
9077 if (r->ws->r == NULL)
9078 ws = r->ws;
9079 } else
9080 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
9081 err(1, "new_region: calloc: failed to allocate memory "
9082 "for screen");
9083
9084 /* if we don't have a workspace already, find one */
9085 if (ws == NULL) {
9086 for (i = 0; i < workspace_limit; i++)
9087 if (s->ws[i].r == NULL) {
9088 ws = &s->ws[i];
9089 break;
9090 }
9091 }
9092
9093 if (ws == NULL)
9094 errx(1, "new_region: no free workspaces");
9095
9096 X(r) = x;
9097 Y(r) = y;
9098 WIDTH(r) = w;
9099 HEIGHT(r) = h;
9100 r->s = s;
9101 r->ws = ws;
9102 r->ws_prior = NULL;
9103 ws->r = r;
9104 outputs++;
9105 TAILQ_INSERT_TAIL(&s->rl, r, entry);
9106
9107 /* Invisible region window to detect pointer events on empty regions. */
9108 r->id = xcb_generate_id(conn);
9109 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
9110 XCB_EVENT_MASK_POINTER_MOTION_HINT;
9111
9112 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
9113 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
9114 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
9115
9116 xcb_map_window(conn, r->id);
9117 }
9118
9119 void
9120 scan_xrandr(int i)
9121 {
9122 #ifdef SWM_XRR_HAS_CRTC
9123 int c;
9124 int ncrtc = 0;
9125 #endif /* SWM_XRR_HAS_CRTC */
9126 struct swm_region *r;
9127 struct ws_win *win;
9128 int num_screens;
9129 xcb_randr_get_screen_resources_current_cookie_t src;
9130 xcb_randr_get_screen_resources_current_reply_t *srr;
9131 xcb_randr_get_crtc_info_cookie_t cic;
9132 xcb_randr_get_crtc_info_reply_t *cir = NULL;
9133 xcb_randr_crtc_t *crtc;
9134 xcb_screen_t *screen;
9135
9136 DNPRINTF(SWM_D_MISC, "scan_xrandr: screen: %d\n", i);
9137
9138 if ((screen = get_screen(i)) == NULL)
9139 errx(1, "ERROR: can't get screen %d.", i);
9140
9141 num_screens = get_screen_count();
9142 if (i >= num_screens)
9143 errx(1, "scan_xrandr: invalid screen");
9144
9145 /* remove any old regions */
9146 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
9147 r->ws->old_r = r->ws->r = NULL;
9148 bar_cleanup(r);
9149 xcb_destroy_window(conn, r->id);
9150 TAILQ_REMOVE(&screens[i].rl, r, entry);
9151 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
9152 }
9153 outputs = 0;
9154
9155 /* map virtual screens onto physical screens */
9156 #ifdef SWM_XRR_HAS_CRTC
9157 if (xrandr_support) {
9158 src = xcb_randr_get_screen_resources_current(conn,
9159 screens[i].root);
9160 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
9161 NULL);
9162 if (srr == NULL) {
9163 new_region(&screens[i], 0, 0,
9164 screen->width_in_pixels,
9165 screen->height_in_pixels);
9166 goto out;
9167 } else
9168 ncrtc = srr->num_crtcs;
9169
9170 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
9171 for (c = 0; c < ncrtc; c++) {
9172 cic = xcb_randr_get_crtc_info(conn, crtc[c],
9173 XCB_CURRENT_TIME);
9174 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
9175 if (cir == NULL)
9176 continue;
9177 if (cir->num_outputs == 0) {
9178 free(cir);
9179 continue;
9180 }
9181
9182 if (cir->mode == 0)
9183 new_region(&screens[i], 0, 0,
9184 screen->width_in_pixels,
9185 screen->height_in_pixels);
9186 else
9187 new_region(&screens[i],
9188 cir->x, cir->y, cir->width, cir->height);
9189 free(cir);
9190 }
9191 free(srr);
9192 }
9193 #endif /* SWM_XRR_HAS_CRTC */
9194
9195 /* If detection failed, create a single region that spans the screen. */
9196 if (TAILQ_EMPTY(&screens[i].rl))
9197 new_region(&screens[i], 0, 0, screen->width_in_pixels,
9198 screen->height_in_pixels);
9199
9200 out:
9201 /* Cleanup unused previously visible workspaces. */
9202 TAILQ_FOREACH(r, &screens[i].orl, entry) {
9203 TAILQ_FOREACH(win, &r->ws->winlist, entry)
9204 unmap_window(win);
9205
9206 /* The screen shouldn't focus on an unused region. */
9207 if (screens[i].r_focus == r)
9208 screens[i].r_focus = NULL;
9209 }
9210
9211 DNPRINTF(SWM_D_MISC, "scan_xrandr: done.\n");
9212 }
9213
9214 void
9215 screenchange(xcb_randr_screen_change_notify_event_t *e)
9216 {
9217 struct swm_region *r;
9218 int i, num_screens;
9219
9220 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
9221
9222 num_screens = get_screen_count();
9223 /* silly event doesn't include the screen index */
9224 for (i = 0; i < num_screens; i++)
9225 if (screens[i].root == e->root)
9226 break;
9227 if (i >= num_screens)
9228 errx(1, "screenchange: screen not found");
9229
9230 /* brute force for now, just re-enumerate the regions */
9231 scan_xrandr(i);
9232
9233 #ifdef SWM_DEBUG
9234 print_win_geom(e->root);
9235 #endif
9236 /* add bars to all regions */
9237 for (i = 0; i < num_screens; i++) {
9238 TAILQ_FOREACH(r, &screens[i].rl, entry)
9239 bar_setup(r);
9240 }
9241
9242 stack();
9243
9244 /* Make sure a region has focus on each screen. */
9245 for (i = 0; i < num_screens; i++) {
9246 if (screens[i].r_focus == NULL) {
9247 r = TAILQ_FIRST(&screens[i].rl);
9248 if (r != NULL)
9249 focus_region(r);
9250 }
9251 }
9252
9253 bar_draw();
9254 focus_flush();
9255 }
9256
9257 void
9258 grab_windows(void)
9259 {
9260 struct swm_region *r = NULL;
9261 xcb_window_t *wins = NULL, trans;
9262 int no;
9263 int i, j, num_screens;
9264 uint16_t state, manage, mapped;
9265
9266 xcb_query_tree_cookie_t qtc;
9267 xcb_query_tree_reply_t *qtr;
9268 xcb_get_window_attributes_cookie_t gac;
9269 xcb_get_window_attributes_reply_t *gar;
9270 xcb_get_property_cookie_t pc;
9271
9272 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
9273 num_screens = get_screen_count();
9274 for (i = 0; i < num_screens; i++) {
9275 qtc = xcb_query_tree(conn, screens[i].root);
9276 qtr = xcb_query_tree_reply(conn, qtc, NULL);
9277 if (qtr == NULL)
9278 continue;
9279 wins = xcb_query_tree_children(qtr);
9280 no = xcb_query_tree_children_length(qtr);
9281 /* attach windows to a region */
9282 /* normal windows */
9283 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
9284 for (j = 0; j < no; j++) {
9285 TAILQ_FOREACH(r, &screens[i].rl, entry) {
9286 if (r->id == wins[j]) {
9287 DNPRINTF(SWM_D_INIT, "grab_windows: "
9288 "skip %#x; region input window.\n",
9289 wins[j]);
9290 break;
9291 }
9292 }
9293
9294 if (r)
9295 continue;
9296
9297 gac = xcb_get_window_attributes(conn, wins[j]);
9298 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
9299 if (gar == NULL) {
9300 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9301 "doesn't exist.\n", wins[j]);
9302 continue;
9303 }
9304
9305 if (gar->override_redirect) {
9306 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9307 "override_redirect set.\n", wins[j]);
9308 free(gar);
9309 continue;
9310 }
9311
9312 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
9313 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
9314 &trans, NULL)) {
9315 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9316 "is transient for %#x.\n", wins[j], trans);
9317 free(gar);
9318 continue;
9319 }
9320
9321 state = getstate(wins[j]);
9322 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
9323 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
9324 if (mapped || manage)
9325 manage_window(wins[j], mapped);
9326 free(gar);
9327 }
9328 /* transient windows */
9329 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
9330 for (j = 0; j < no; j++) {
9331 gac = xcb_get_window_attributes(conn, wins[j]);
9332 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
9333 if (gar == NULL) {
9334 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9335 "doesn't exist.\n", wins[j]);
9336 continue;
9337 }
9338
9339 if (gar->override_redirect) {
9340 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9341 "override_redirect set.\n", wins[j]);
9342 free(gar);
9343 continue;
9344 }
9345
9346 state = getstate(wins[j]);
9347 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
9348 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
9349 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
9350 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
9351 &trans, NULL) && manage)
9352 manage_window(wins[j], mapped);
9353 free(gar);
9354 }
9355 free(qtr);
9356 }
9357 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
9358 }
9359
9360 void
9361 setup_screens(void)
9362 {
9363 int i, j, k, num_screens;
9364 struct workspace *ws;
9365 uint32_t gcv[1], wa[1];
9366 const xcb_query_extension_reply_t *qep;
9367 xcb_screen_t *screen;
9368 xcb_randr_query_version_cookie_t c;
9369 xcb_randr_query_version_reply_t *r;
9370
9371 num_screens = get_screen_count();
9372 if ((screens = calloc(num_screens,
9373 sizeof(struct swm_screen))) == NULL)
9374 err(1, "setup_screens: calloc: failed to allocate memory for "
9375 "screens");
9376
9377 /* initial Xrandr setup */
9378 xrandr_support = 0;
9379 qep = xcb_get_extension_data(conn, &xcb_randr_id);
9380 if (qep->present) {
9381 c = xcb_randr_query_version(conn, 1, 1);
9382 r = xcb_randr_query_version_reply(conn, c, NULL);
9383 if (r) {
9384 if (r->major_version >= 1) {
9385 xrandr_support = 1;
9386 xrandr_eventbase = qep->first_event;
9387 }
9388 free(r);
9389 }
9390 }
9391
9392 wa[0] = cursors[XC_LEFT_PTR].cid;
9393
9394 /* map physical screens */
9395 for (i = 0; i < num_screens; i++) {
9396 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
9397 screens[i].idx = i;
9398 screens[i].r_focus = NULL;
9399
9400 TAILQ_INIT(&screens[i].rl);
9401 TAILQ_INIT(&screens[i].orl);
9402 if ((screen = get_screen(i)) == NULL)
9403 errx(1, "ERROR: can't get screen %d.", i);
9404 screens[i].root = screen->root;
9405
9406 /* set default colors */
9407 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
9408 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
9409 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
9410 setscreencolor("rgb:00/40/40", i + 1,
9411 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
9412 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
9413 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
9414
9415 /* create graphics context on screen */
9416 screens[i].bar_gc = xcb_generate_id(conn);
9417 gcv[0] = 0;
9418 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
9419 XCB_GC_GRAPHICS_EXPOSURES, gcv);
9420
9421 /* set default cursor */
9422 xcb_change_window_attributes(conn, screens[i].root,
9423 XCB_CW_CURSOR, wa);
9424
9425 /* init all workspaces */
9426 /* XXX these should be dynamically allocated too */
9427 for (j = 0; j < SWM_WS_MAX; j++) {
9428 ws = &screens[i].ws[j];
9429 ws->idx = j;
9430 ws->name = NULL;
9431 ws->bar_enabled = 1;
9432 ws->focus = NULL;
9433 ws->focus_prev = NULL;
9434 ws->focus_pending = NULL;
9435 ws->r = NULL;
9436 ws->old_r = NULL;
9437 TAILQ_INIT(&ws->winlist);
9438 TAILQ_INIT(&ws->unmanagedlist);
9439
9440 for (k = 0; layouts[k].l_stack != NULL; k++)
9441 if (layouts[k].l_config != NULL)
9442 layouts[k].l_config(ws,
9443 SWM_ARG_ID_STACKINIT);
9444 ws->cur_layout = &layouts[0];
9445 ws->cur_layout->l_string(ws);
9446 }
9447
9448 scan_xrandr(i);
9449
9450 if (xrandr_support)
9451 xcb_randr_select_input(conn, screens[i].root,
9452 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
9453 }
9454 }
9455
9456 void
9457 setup_globals(void)
9458 {
9459 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
9460 err(1, "setup_globals: strdup: failed to allocate memory.");
9461
9462 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
9463 err(1, "setup_globals: strdup: failed to allocate memory.");
9464
9465 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
9466 errx(1, "unable to allocate key symbols");
9467
9468 a_state = get_atom_from_string("WM_STATE");
9469 a_prot = get_atom_from_string("WM_PROTOCOLS");
9470 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
9471 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
9472 a_utf8_string = get_atom_from_string("UTF8_STRING");
9473 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
9474 a_swm_ws = get_atom_from_string("_SWM_WS");
9475 }
9476
9477 void
9478 workaround(void)
9479 {
9480 int i, num_screens;
9481 xcb_atom_t netwmcheck;
9482 xcb_window_t root, win;
9483
9484 /* work around sun jdk bugs, code from wmname */
9485 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
9486
9487 num_screens = get_screen_count();
9488 for (i = 0; i < num_screens; i++) {
9489 root = screens[i].root;
9490
9491 win = xcb_generate_id(conn);
9492 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
9493 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
9494 XCB_COPY_FROM_PARENT, 0, NULL);
9495
9496 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
9497 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9498 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9499 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9500 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9501 ewmh[_NET_WM_NAME].atom, a_utf8_string, 8, strlen("LG3D"),
9502 "LG3D");
9503 }
9504 }
9505
9506 void
9507 shutdown_cleanup(void)
9508 {
9509 int i, num_screens;
9510
9511 /* disable alarm because the following code may not be interrupted */
9512 alarm(0);
9513 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
9514 err(1, "can't disable alarm");
9515
9516 bar_extra_stop();
9517 unmap_all();
9518
9519 cursors_cleanup();
9520
9521 teardown_ewmh();
9522
9523 num_screens = get_screen_count();
9524 for (i = 0; i < num_screens; ++i) {
9525 if (screens[i].bar_gc != XCB_NONE)
9526 xcb_free_gc(conn, screens[i].bar_gc);
9527 if (!bar_font_legacy)
9528 XftColorFree(display, DefaultVisual(display, i),
9529 DefaultColormap(display, i), &bar_font_color);
9530 }
9531
9532 if (bar_font_legacy)
9533 XFreeFontSet(display, bar_fs);
9534 else {
9535 XftFontClose(display, bar_font);
9536 }
9537
9538 xcb_key_symbols_free(syms);
9539 xcb_flush(conn);
9540 xcb_disconnect(conn);
9541 }
9542
9543 void
9544 event_error(xcb_generic_error_t *e)
9545 {
9546 (void)e;
9547
9548 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
9549 "resource_id: %u, minor_code: %u\n",
9550 xcb_event_get_error_label(e->error_code), e->error_code,
9551 xcb_event_get_request_label(e->major_code), e->major_code,
9552 e->sequence, e->resource_id, e->minor_code);
9553 }
9554
9555 void
9556 event_handle(xcb_generic_event_t *evt)
9557 {
9558 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
9559
9560 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
9561 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
9562 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
9563
9564 switch (type) {
9565 #define EVENT(type, callback) case type: callback((void *)evt); return
9566 EVENT(0, event_error);
9567 EVENT(XCB_BUTTON_PRESS, buttonpress);
9568 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
9569 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
9570 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
9571 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
9572 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
9573 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
9574 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
9575 /*EVENT(XCB_CREATE_NOTIFY, );*/
9576 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
9577 EVENT(XCB_ENTER_NOTIFY, enternotify);
9578 EVENT(XCB_EXPOSE, expose);
9579 #ifdef SWM_DEBUG
9580 EVENT(XCB_FOCUS_IN, focusin);
9581 EVENT(XCB_FOCUS_OUT, focusout);
9582 #endif
9583 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
9584 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
9585 EVENT(XCB_KEY_PRESS, keypress);
9586 /*EVENT(XCB_KEY_RELEASE, keypress);*/
9587 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
9588 #ifdef SWM_DEBUG
9589 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
9590 #endif
9591 EVENT(XCB_MAP_NOTIFY, mapnotify);
9592 EVENT(XCB_MAP_REQUEST, maprequest);
9593 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
9594 EVENT(XCB_MOTION_NOTIFY, motionnotify);
9595 /*EVENT(XCB_NO_EXPOSURE, );*/
9596 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
9597 /*EVENT(XCB_REPARENT_NOTIFY, );*/
9598 /*EVENT(XCB_RESIZE_REQUEST, );*/
9599 /*EVENT(XCB_SELECTION_CLEAR, );*/
9600 /*EVENT(XCB_SELECTION_NOTIFY, );*/
9601 /*EVENT(XCB_SELECTION_REQUEST, );*/
9602 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
9603 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
9604 #undef EVENT
9605 }
9606 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
9607 screenchange((void *)evt);
9608 }
9609
9610 int
9611 main(int argc, char *argv[])
9612 {
9613 struct swm_region *r;
9614 char conf[PATH_MAX], *cfile = NULL;
9615 struct stat sb;
9616 int xfd, i, num_screens, startup = 1;
9617 struct sigaction sact;
9618 xcb_generic_event_t *evt;
9619 struct timeval tv;
9620 fd_set rd;
9621 int rd_max;
9622 int stdin_ready = 0;
9623 int num_readable;
9624
9625 /* suppress unused warning since var is needed */
9626 (void)argc;
9627
9628 time_started = time(NULL);
9629
9630 start_argv = argv;
9631 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
9632 if (setlocale(LC_CTYPE, "") == NULL || setlocale(LC_TIME, "") == NULL)
9633 warnx("no locale support");
9634
9635 /* handle some signals */
9636 bzero(&sact, sizeof(sact));
9637 sigemptyset(&sact.sa_mask);
9638 sact.sa_flags = 0;
9639 sact.sa_handler = sighdlr;
9640 sigaction(SIGINT, &sact, NULL);
9641 sigaction(SIGQUIT, &sact, NULL);
9642 sigaction(SIGTERM, &sact, NULL);
9643 sigaction(SIGHUP, &sact, NULL);
9644
9645 sact.sa_handler = sighdlr;
9646 sact.sa_flags = SA_NOCLDSTOP;
9647 sigaction(SIGCHLD, &sact, NULL);
9648
9649 if ((display = XOpenDisplay(0)) == NULL)
9650 errx(1, "can not open display");
9651
9652 conn = XGetXCBConnection(display);
9653 if (xcb_connection_has_error(conn))
9654 errx(1, "can not get XCB connection");
9655
9656 XSetEventQueueOwner(display, XCBOwnsEventQueue);
9657
9658 xcb_prefetch_extension_data(conn, &xcb_randr_id);
9659 xfd = xcb_get_file_descriptor(conn);
9660
9661 /* look for local and global conf file */
9662 pwd = getpwuid(getuid());
9663 if (pwd == NULL)
9664 errx(1, "invalid user: %d", getuid());
9665
9666 xcb_grab_server(conn);
9667 xcb_aux_sync(conn);
9668
9669 /* flush all events */
9670 while ((evt = xcb_poll_for_event(conn))) {
9671 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
9672 event_handle(evt);
9673 free(evt);
9674 }
9675
9676 if (enable_wm())
9677 errx(1, "another window manager is currently running");
9678
9679 /* Load Xcursors and/or cursorfont glyph cursors. */
9680 cursors_load();
9681
9682 xcb_aux_sync(conn);
9683
9684 setup_globals();
9685 setup_screens();
9686 setup_keys();
9687 setup_quirks();
9688 setup_spawn();
9689
9690 /* load config */
9691 for (i = 0; ; i++) {
9692 conf[0] = '\0';
9693 switch (i) {
9694 case 0:
9695 /* ~ */
9696 snprintf(conf, sizeof conf, "%s/.%s",
9697 pwd->pw_dir, SWM_CONF_FILE);
9698 break;
9699 case 1:
9700 /* global */
9701 snprintf(conf, sizeof conf, "/etc/%s",
9702 SWM_CONF_FILE);
9703 break;
9704 case 2:
9705 /* ~ compat */
9706 snprintf(conf, sizeof conf, "%s/.%s",
9707 pwd->pw_dir, SWM_CONF_FILE_OLD);
9708 break;
9709 case 3:
9710 /* global compat */
9711 snprintf(conf, sizeof conf, "/etc/%s",
9712 SWM_CONF_FILE_OLD);
9713 break;
9714 default:
9715 goto noconfig;
9716 }
9717
9718 if (strlen(conf) && stat(conf, &sb) != -1)
9719 if (S_ISREG(sb.st_mode)) {
9720 cfile = conf;
9721 break;
9722 }
9723 }
9724 noconfig:
9725
9726 /* load conf (if any) */
9727 if (cfile)
9728 conf_load(cfile, SWM_CONF_DEFAULT);
9729
9730 validate_spawns();
9731
9732 setup_ewmh();
9733 /* set some values to work around bad programs */
9734 workaround();
9735 /* grab existing windows (before we build the bars) */
9736 grab_windows();
9737
9738 if (getenv("SWM_STARTED") == NULL)
9739 setenv("SWM_STARTED", "YES", 1);
9740
9741 /* setup all bars */
9742 num_screens = get_screen_count();
9743 for (i = 0; i < num_screens; i++)
9744 TAILQ_FOREACH(r, &screens[i].rl, entry)
9745 bar_setup(r);
9746
9747 grabkeys();
9748 stack();
9749 bar_draw();
9750
9751 xcb_ungrab_server(conn);
9752 xcb_flush(conn);
9753
9754 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
9755
9756 while (running) {
9757 while ((evt = xcb_poll_for_event(conn))) {
9758 if (!running)
9759 goto done;
9760 event_handle(evt);
9761 free(evt);
9762 }
9763
9764 /* If just (re)started, set default focus if needed. */
9765 if (startup) {
9766 startup = 0;
9767
9768 if (focus_mode != SWM_FOCUS_FOLLOW) {
9769 r = TAILQ_FIRST(&screens[0].rl);
9770 if (r) {
9771 focus_region(r);
9772 focus_flush();
9773 }
9774 continue;
9775 }
9776 }
9777
9778 FD_ZERO(&rd);
9779
9780 if (bar_extra)
9781 FD_SET(STDIN_FILENO, &rd);
9782
9783 FD_SET(xfd, &rd);
9784 tv.tv_sec = 1;
9785 tv.tv_usec = 0;
9786 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
9787 if (num_readable == -1 && errno != EINTR) {
9788 DNPRINTF(SWM_D_MISC, "select failed");
9789 } else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd)) {
9790 stdin_ready = 1;
9791 }
9792
9793 if (restart_wm)
9794 restart(NULL, NULL);
9795
9796 if (search_resp)
9797 search_do_resp();
9798
9799 if (!running)
9800 goto done;
9801
9802 if (stdin_ready) {
9803 stdin_ready = 0;
9804 bar_extra_update();
9805 }
9806
9807 bar_draw();
9808 xcb_flush(conn);
9809 }
9810 done:
9811 shutdown_cleanup();
9812
9813 return (0);
9814 }