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