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