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