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