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