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