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