]> code.delx.au - spectrwm/blob - spectrwm.c
test all spawn things to see if they exist
[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 (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("screenshot_all", "screenshot.sh full", 0);
6056 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
6057 setconfspawn("lock", "xlock", 0);
6058 setconfspawn("initscr", "initscreen.sh", 0);
6059 setconfspawn("menu", "dmenu_run"
6060 " -fn $bar_font"
6061 " -nb $bar_color"
6062 " -nf $bar_font_color"
6063 " -sb $bar_border"
6064 " -sf $bar_color", 0);
6065 setconfspawn("search", "damenu"
6066 " -i"
6067 " -fn $bar_font"
6068 " -nb $bar_color"
6069 " -nf $bar_font_color"
6070 " -sb $bar_border"
6071 " -sf $bar_color", 0);
6072 setconfspawn("name_workspace", "dmenu"
6073 " -p Workspace"
6074 " -fn $bar_font"
6075 " -nb $bar_color"
6076 " -nf $bar_font_color"
6077 " -sb $bar_border"
6078 " -sf $bar_color", 0);
6079 }
6080
6081 /* key bindings */
6082 #define SWM_MODNAME_SIZE 32
6083 #define SWM_KEY_WS "\n+ \t"
6084 int
6085 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
6086 {
6087 char *cp, *name;
6088 KeySym uks;
6089 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
6090 if (mod == NULL || ks == NULL) {
6091 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
6092 return (1);
6093 }
6094 if (keystr == NULL || strlen(keystr) == 0) {
6095 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
6096 return (1);
6097 }
6098 cp = keystr;
6099 *ks = XCB_NO_SYMBOL;
6100 *mod = 0;
6101 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
6102 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
6103 if (cp)
6104 cp += (long)strspn(cp, SWM_KEY_WS);
6105 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
6106 *mod |= currmod;
6107 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
6108 *mod |= XCB_MOD_MASK_1;
6109 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
6110 *mod += XCB_MOD_MASK_2;
6111 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
6112 *mod |= XCB_MOD_MASK_3;
6113 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
6114 *mod |= XCB_MOD_MASK_4;
6115 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
6116 *mod |= XCB_MOD_MASK_SHIFT;
6117 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
6118 *mod |= XCB_MOD_MASK_CONTROL;
6119 else {
6120 *ks = XStringToKeysym(name);
6121 XConvertCase(*ks, ks, &uks);
6122 if (ks == XCB_NO_SYMBOL) {
6123 DNPRINTF(SWM_D_KEY,
6124 "parsekeys: invalid key %s\n",
6125 name);
6126 return (1);
6127 }
6128 }
6129 }
6130 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
6131 return (0);
6132 }
6133
6134 char *
6135 strdupsafe(const char *str)
6136 {
6137 if (str == NULL)
6138 return (NULL);
6139 else
6140 return (strdup(str));
6141 }
6142
6143 void
6144 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6145 const char *spawn_name)
6146 {
6147 struct key *kp;
6148
6149 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
6150 keyfuncs[kfid].name, spawn_name);
6151
6152 if ((kp = malloc(sizeof *kp)) == NULL)
6153 err(1, "key_insert: malloc");
6154
6155 kp->mod = mod;
6156 kp->keysym = ks;
6157 kp->funcid = kfid;
6158 kp->spawn_name = strdupsafe(spawn_name);
6159 RB_INSERT(key_tree, &keys, kp);
6160
6161 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
6162 }
6163
6164 struct key *
6165 key_lookup(unsigned int mod, KeySym ks)
6166 {
6167 struct key kp;
6168
6169 kp.keysym = ks;
6170 kp.mod = mod;
6171
6172 return (RB_FIND(key_tree, &keys, &kp));
6173 }
6174
6175 void
6176 key_remove(struct key *kp)
6177 {
6178 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
6179
6180 RB_REMOVE(key_tree, &keys, kp);
6181 free(kp->spawn_name);
6182 free(kp);
6183
6184 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
6185 }
6186
6187 void
6188 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
6189 const char *spawn_name)
6190 {
6191 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
6192 spawn_name);
6193
6194 key_remove(kp);
6195 key_insert(mod, ks, kfid, spawn_name);
6196
6197 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
6198 }
6199
6200 void
6201 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6202 const char *spawn_name)
6203 {
6204 struct key *kp;
6205
6206 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
6207 keyfuncs[kfid].name, spawn_name);
6208
6209 if ((kp = key_lookup(mod, ks)) != NULL) {
6210 if (kfid == KF_INVALID)
6211 key_remove(kp);
6212 else
6213 key_replace(kp, mod, ks, kfid, spawn_name);
6214 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6215 return;
6216 }
6217 if (kfid == KF_INVALID) {
6218 warnx("bind: Key combination already unbound.");
6219 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6220 return;
6221 }
6222
6223 key_insert(mod, ks, kfid, spawn_name);
6224 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6225 }
6226
6227 int
6228 setconfbinding(char *selector, char *value, int flags)
6229 {
6230 enum keyfuncid kfid;
6231 unsigned int mod;
6232 KeySym ks;
6233 struct spawn_prog *sp;
6234
6235 /* suppress unused warning since var is needed */
6236 (void)flags;
6237
6238 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
6239 if (selector == NULL) {
6240 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
6241 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6242 kfid = KF_INVALID;
6243 setkeybinding(mod, ks, kfid, NULL);
6244 return (0);
6245 } else
6246 return (1);
6247 }
6248 /* search by key function name */
6249 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
6250 if (strncasecmp(selector, keyfuncs[kfid].name,
6251 SWM_FUNCNAME_LEN) == 0) {
6252 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
6253 selector);
6254 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6255 setkeybinding(mod, ks, kfid, NULL);
6256 return (0);
6257 } else
6258 return (1);
6259 }
6260 }
6261 /* search by custom spawn name */
6262 TAILQ_FOREACH(sp, &spawns, entry) {
6263 if (strcasecmp(selector, sp->name) == 0) {
6264 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
6265 selector);
6266 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6267 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
6268 sp->name);
6269 return (0);
6270 } else
6271 return (1);
6272 }
6273 }
6274 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
6275 return (1);
6276 }
6277
6278 void
6279 setup_keys(void)
6280 {
6281 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6282 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
6283 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
6284 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
6285 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
6286 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
6287 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
6288 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
6289 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
6290 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
6291 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
6292 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
6293 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
6294 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
6295 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
6296 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
6297 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
6298 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
6299 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
6300 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
6301 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
6302 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
6303 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
6304 setkeybinding(MODKEY_SHIFT, XK_KP_End, KF_MVRG_1, NULL);
6305 setkeybinding(MODKEY_SHIFT, XK_KP_Down, KF_MVRG_2, NULL);
6306 setkeybinding(MODKEY_SHIFT, XK_KP_Next, KF_MVRG_3, NULL);
6307 setkeybinding(MODKEY_SHIFT, XK_KP_Left, KF_MVRG_4, NULL);
6308 setkeybinding(MODKEY_SHIFT, XK_KP_Begin, KF_MVRG_5, NULL);
6309 setkeybinding(MODKEY_SHIFT, XK_KP_Right, KF_MVRG_6, NULL);
6310 setkeybinding(MODKEY_SHIFT, XK_KP_Home, KF_MVRG_7, NULL);
6311 setkeybinding(MODKEY_SHIFT, XK_KP_Up, KF_MVRG_8, NULL);
6312 setkeybinding(MODKEY_SHIFT, XK_KP_Prior, KF_MVRG_9, NULL);
6313 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
6314 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
6315 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
6316 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
6317 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
6318 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
6319 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
6320 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
6321 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
6322 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
6323 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
6324 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
6325 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
6326 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
6327 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
6328 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
6329 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
6330 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
6331 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
6332 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
6333 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
6334 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
6335 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
6336 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
6337 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
6338 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
6339 setkeybinding(MODKEY, XK_KP_End, KF_RG_1, NULL);
6340 setkeybinding(MODKEY, XK_KP_Down, KF_RG_2, NULL);
6341 setkeybinding(MODKEY, XK_KP_Next, KF_RG_3, NULL);
6342 setkeybinding(MODKEY, XK_KP_Left, KF_RG_4, NULL);
6343 setkeybinding(MODKEY, XK_KP_Begin, KF_RG_5, NULL);
6344 setkeybinding(MODKEY, XK_KP_Right, KF_RG_6, NULL);
6345 setkeybinding(MODKEY, XK_KP_Home, KF_RG_7, NULL);
6346 setkeybinding(MODKEY, XK_KP_Up, KF_RG_8, NULL);
6347 setkeybinding(MODKEY, XK_KP_Prior, KF_RG_9, NULL);
6348 setkeybinding(MODKEY_SHIFT, XK_Right, KF_RG_NEXT, NULL);
6349 setkeybinding(MODKEY_SHIFT, XK_Left, KF_RG_PREV, NULL);
6350 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
6351 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
6352 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
6353 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
6354 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
6355 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
6356 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
6357 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
6358 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
6359 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
6360 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
6361 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
6362 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
6363 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
6364 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
6365 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
6366 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
6367 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
6368 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
6369 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
6370 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
6371 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
6372 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
6373 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
6374 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
6375 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
6376 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
6377 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
6378 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
6379 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
6380 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
6381 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
6382 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
6383 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
6384 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
6385 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
6386 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
6387 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
6388 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
6389 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
6390 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
6391 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
6392 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
6393 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
6394 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
6395 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
6396 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
6397 #ifdef SWM_DEBUG
6398 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
6399 #endif
6400 #undef MODKEY_SHIFT
6401 }
6402
6403 void
6404 clear_keys(void)
6405 {
6406 struct key *kp;
6407
6408 while (RB_EMPTY(&keys) == 0) {
6409 kp = RB_ROOT(&keys);
6410 key_remove(kp);
6411 }
6412 }
6413
6414 int
6415 setkeymapping(char *selector, char *value, int flags)
6416 {
6417 char keymapping_file[PATH_MAX];
6418
6419 /* suppress unused warnings since vars are needed */
6420 (void)selector;
6421 (void)flags;
6422
6423 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
6424 if (value[0] == '~')
6425 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
6426 pwd->pw_dir, &value[1]);
6427 else
6428 strlcpy(keymapping_file, value, sizeof keymapping_file);
6429 clear_keys();
6430 /* load new key bindings; if it fails, revert to default bindings */
6431 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
6432 clear_keys();
6433 setup_keys();
6434 }
6435 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
6436 return (0);
6437 }
6438
6439 void
6440 updatenumlockmask(void)
6441 {
6442 unsigned int i, j;
6443 xcb_get_modifier_mapping_reply_t *modmap_r;
6444 xcb_keycode_t *modmap, kc, *keycode;
6445
6446 numlockmask = 0;
6447
6448 modmap_r = xcb_get_modifier_mapping_reply(conn,
6449 xcb_get_modifier_mapping(conn),
6450 NULL);
6451 if (modmap_r) {
6452 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
6453 for (i = 0; i < 8; i++) {
6454 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
6455 kc = modmap[i * modmap_r->keycodes_per_modifier
6456 + j];
6457 keycode = xcb_key_symbols_get_keycode(syms,
6458 XK_Num_Lock);
6459 if (kc == *keycode)
6460 numlockmask = (1 << i);
6461 free(keycode);
6462 }
6463 }
6464 free(modmap_r);
6465 }
6466 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
6467 }
6468
6469 void
6470 grabkeys(void)
6471 {
6472 struct key *kp;
6473 int num_screens, k, j;
6474 unsigned int modifiers[4];
6475 xcb_keycode_t *code;
6476
6477 DNPRINTF(SWM_D_MISC, "grabkeys\n");
6478 updatenumlockmask();
6479
6480 modifiers[0] = 0;
6481 modifiers[1] = numlockmask;
6482 modifiers[2] = XCB_MOD_MASK_LOCK;
6483 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6484
6485 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6486 for (k = 0; k < num_screens; k++) {
6487 if (TAILQ_EMPTY(&screens[k].rl))
6488 continue;
6489 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
6490 XCB_MOD_MASK_ANY);
6491 RB_FOREACH(kp, key_tree, &keys) {
6492 if ((code = xcb_key_symbols_get_keycode(syms,
6493 kp->keysym))) {
6494 for (j = 0; j < LENGTH(modifiers); j++)
6495 xcb_grab_key(conn, 1,
6496 screens[k].root,
6497 kp->mod | modifiers[j],
6498 *code, XCB_GRAB_MODE_SYNC,
6499 XCB_GRAB_MODE_SYNC);
6500 free(code);
6501 }
6502 }
6503 }
6504 }
6505
6506 void
6507 grabbuttons(struct ws_win *win)
6508 {
6509 unsigned int modifiers[4];
6510 int i, j;
6511
6512 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win 0x%x\n", win->id);
6513 updatenumlockmask();
6514
6515 modifiers[0] = 0;
6516 modifiers[1] = numlockmask;
6517 modifiers[2] = XCB_MOD_MASK_LOCK;
6518 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
6519
6520 for (i = 0; i < LENGTH(buttons); i++)
6521 if (buttons[i].action == client_click)
6522 for (j = 0; j < LENGTH(modifiers); ++j)
6523 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
6524 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
6525 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
6526 buttons[i].button, buttons[i].mask |
6527 modifiers[j]);
6528 }
6529
6530 const char *quirkname[] = {
6531 "NONE", /* config string for "no value" */
6532 "FLOAT",
6533 "TRANSSZ",
6534 "ANYWHERE",
6535 "XTERM_FONTADJ",
6536 "FULLSCREEN",
6537 "FOCUSPREV",
6538 };
6539
6540 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
6541 #define SWM_Q_WS "\n|+ \t"
6542 int
6543 parsequirks(char *qstr, unsigned long *quirk)
6544 {
6545 char *cp, *name;
6546 int i;
6547
6548 if (quirk == NULL)
6549 return (1);
6550
6551 cp = qstr;
6552 *quirk = 0;
6553 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
6554 if (cp)
6555 cp += (long)strspn(cp, SWM_Q_WS);
6556 for (i = 0; i < LENGTH(quirkname); i++) {
6557 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
6558 DNPRINTF(SWM_D_QUIRK,
6559 "parsequirks: %s\n", name);
6560 if (i == 0) {
6561 *quirk = 0;
6562 return (0);
6563 }
6564 *quirk |= 1 << (i-1);
6565 break;
6566 }
6567 }
6568 if (i >= LENGTH(quirkname)) {
6569 DNPRINTF(SWM_D_QUIRK,
6570 "parsequirks: invalid quirk [%s]\n", name);
6571 return (1);
6572 }
6573 }
6574 return (0);
6575 }
6576
6577 void
6578 quirk_insert(const char *class, const char *name, unsigned long quirk)
6579 {
6580 struct quirk *qp;
6581
6582 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
6583 quirk);
6584
6585 if ((qp = malloc(sizeof *qp)) == NULL)
6586 err(1, "quirk_insert: malloc");
6587 if ((qp->class = strdup(class)) == NULL)
6588 err(1, "quirk_insert: strdup");
6589 if ((qp->name = strdup(name)) == NULL)
6590 err(1, "quirk_insert: strdup");
6591
6592 qp->quirk = quirk;
6593 TAILQ_INSERT_TAIL(&quirks, qp, entry);
6594
6595 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
6596 }
6597
6598 void
6599 quirk_remove(struct quirk *qp)
6600 {
6601 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
6602 qp->name, qp->quirk);
6603
6604 TAILQ_REMOVE(&quirks, qp, entry);
6605 free(qp->class);
6606 free(qp->name);
6607 free(qp);
6608
6609 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
6610 }
6611
6612 void
6613 quirk_replace(struct quirk *qp, const char *class, const char *name,
6614 unsigned long quirk)
6615 {
6616 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
6617 qp->name, qp->quirk);
6618
6619 quirk_remove(qp);
6620 quirk_insert(class, name, quirk);
6621
6622 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
6623 }
6624
6625 void
6626 setquirk(const char *class, const char *name, unsigned long quirk)
6627 {
6628 struct quirk *qp;
6629
6630 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
6631 quirk);
6632
6633 /* Remove/replace existing quirk. */
6634 TAILQ_FOREACH(qp, &quirks, entry) {
6635 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
6636 if (!quirk)
6637 quirk_remove(qp);
6638 else
6639 quirk_replace(qp, class, name, quirk);
6640 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6641 return;
6642 }
6643 }
6644
6645 /* Only insert if quirk is not NONE. */
6646 if (quirk)
6647 quirk_insert(class, name, quirk);
6648
6649 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6650 }
6651
6652 int
6653 setconfquirk(char *selector, char *value, int flags)
6654 {
6655 char *cp, *class, *name;
6656 int retval;
6657 unsigned long qrks;
6658
6659 /* suppress unused warning since var is needed */
6660 (void)flags;
6661
6662 if (selector == NULL)
6663 return (0);
6664 if ((cp = strchr(selector, ':')) == NULL)
6665 return (0);
6666 *cp = '\0';
6667 class = selector;
6668 name = cp + 1;
6669 if ((retval = parsequirks(value, &qrks)) == 0)
6670 setquirk(class, name, qrks);
6671 return (retval);
6672 }
6673
6674 void
6675 setup_quirks(void)
6676 {
6677 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
6678 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
6679 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
6680 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
6681 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6682 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
6683 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6684 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6685 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6686 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6687 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
6688 setquirk("pcb", "pcb", SWM_Q_FLOAT);
6689 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
6690 }
6691
6692 /* conf file stuff */
6693 #define SWM_CONF_FILE "spectrwm.conf"
6694 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6695
6696 enum {
6697 SWM_S_BAR_ACTION,
6698 SWM_S_BAR_AT_BOTTOM,
6699 SWM_S_BAR_BORDER_WIDTH,
6700 SWM_S_BAR_DELAY,
6701 SWM_S_BAR_ENABLED,
6702 SWM_S_BAR_ENABLED_WS,
6703 SWM_S_BAR_FONT,
6704 SWM_S_BAR_FORMAT,
6705 SWM_S_BAR_JUSTIFY,
6706 SWM_S_BORDER_WIDTH,
6707 SWM_S_CLOCK_ENABLED,
6708 SWM_S_CLOCK_FORMAT,
6709 SWM_S_CYCLE_EMPTY,
6710 SWM_S_CYCLE_VISIBLE,
6711 SWM_S_DIALOG_RATIO,
6712 SWM_S_DISABLE_BORDER,
6713 SWM_S_FOCUS_CLOSE,
6714 SWM_S_FOCUS_CLOSE_WRAP,
6715 SWM_S_FOCUS_DEFAULT,
6716 SWM_S_FOCUS_MODE,
6717 SWM_S_REGION_PADDING,
6718 SWM_S_SPAWN_ORDER,
6719 SWM_S_SPAWN_TERM,
6720 SWM_S_SS_APP,
6721 SWM_S_SS_ENABLED,
6722 SWM_S_STACK_ENABLED,
6723 SWM_S_TERM_WIDTH,
6724 SWM_S_TILE_GAP,
6725 SWM_S_TITLE_CLASS_ENABLED,
6726 SWM_S_TITLE_NAME_ENABLED,
6727 SWM_S_URGENT_ENABLED,
6728 SWM_S_VERBOSE_LAYOUT,
6729 SWM_S_WINDOW_NAME_ENABLED,
6730 SWM_S_WORKSPACE_LIMIT
6731 };
6732
6733 int
6734 setconfvalue(char *selector, char *value, int flags)
6735 {
6736 struct workspace *ws;
6737 int i, ws_id, num_screens;
6738 char *b;
6739
6740 /* suppress unused warning since var is needed */
6741 (void)selector;
6742
6743 switch (flags) {
6744 case SWM_S_BAR_ACTION:
6745 free(bar_argv[0]);
6746 if ((bar_argv[0] = expand_tilde(value)) == NULL)
6747 err(1, "setconfvalue: bar_action");
6748 break;
6749 case SWM_S_BAR_AT_BOTTOM:
6750 bar_at_bottom = atoi(value);
6751 break;
6752 case SWM_S_BAR_BORDER_WIDTH:
6753 bar_border_width = atoi(value);
6754 if (bar_border_width < 0)
6755 bar_border_width = 0;
6756 break;
6757 case SWM_S_BAR_DELAY:
6758 /* No longer needed; leave to not break old conf files. */
6759 break;
6760 case SWM_S_BAR_ENABLED:
6761 bar_enabled = atoi(value);
6762 break;
6763 case SWM_S_BAR_ENABLED_WS:
6764 ws_id = atoi(selector) - 1;
6765 if (ws_id < 0 || ws_id >= workspace_limit)
6766 errx(1, "setconfvalue: bar_enabled_ws: invalid "
6767 "workspace %d.", ws_id + 1);
6768
6769 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6770 for (i = 0; i < num_screens; i++) {
6771 ws = (struct workspace *)&screens[i].ws;
6772 ws[ws_id].bar_enabled = atoi(value);
6773 }
6774 break;
6775 case SWM_S_BAR_FONT:
6776 b = bar_fonts;
6777 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
6778 err(1, "setconfvalue: asprintf: failed to allocate "
6779 "memory for bar_fonts.");
6780 free(b);
6781
6782 /* If already in xft mode, then we are done. */
6783 if (!bar_font_legacy)
6784 break;
6785
6786 /* If there are any non-XLFD entries, switch to Xft mode. */
6787 while ((b = strsep(&value, ",")) != NULL) {
6788 if (*b == '\0')
6789 continue;
6790 if (!isxlfd(b)) {
6791 bar_font_legacy = 0;
6792 break;
6793 }
6794 }
6795 break;
6796 case SWM_S_BAR_FORMAT:
6797 free(bar_format);
6798 if ((bar_format = strdup(value)) == NULL)
6799 err(1, "setconfvalue: bar_format");
6800 break;
6801 case SWM_S_BAR_JUSTIFY:
6802 if (!strcmp(value, "left"))
6803 bar_justify = SWM_BAR_JUSTIFY_LEFT;
6804 else if (!strcmp(value, "center"))
6805 bar_justify = SWM_BAR_JUSTIFY_CENTER;
6806 else if (!strcmp(value, "right"))
6807 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
6808 else
6809 errx(1, "invalid bar_justify");
6810 break;
6811 case SWM_S_BORDER_WIDTH:
6812 border_width = atoi(value);
6813 if (border_width < 0)
6814 border_width = 0;
6815 break;
6816 case SWM_S_CLOCK_ENABLED:
6817 clock_enabled = atoi(value);
6818 break;
6819 case SWM_S_CLOCK_FORMAT:
6820 #ifndef SWM_DENY_CLOCK_FORMAT
6821 free(clock_format);
6822 if ((clock_format = strdup(value)) == NULL)
6823 err(1, "setconfvalue: clock_format");
6824 #endif
6825 break;
6826 case SWM_S_CYCLE_EMPTY:
6827 cycle_empty = atoi(value);
6828 break;
6829 case SWM_S_CYCLE_VISIBLE:
6830 cycle_visible = atoi(value);
6831 break;
6832 case SWM_S_DIALOG_RATIO:
6833 dialog_ratio = atof(value);
6834 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
6835 dialog_ratio = .6;
6836 break;
6837 case SWM_S_DISABLE_BORDER:
6838 disable_border = atoi(value);
6839 break;
6840 case SWM_S_FOCUS_CLOSE:
6841 if (!strcmp(value, "first"))
6842 focus_close = SWM_STACK_BOTTOM;
6843 else if (!strcmp(value, "last"))
6844 focus_close = SWM_STACK_TOP;
6845 else if (!strcmp(value, "next"))
6846 focus_close = SWM_STACK_ABOVE;
6847 else if (!strcmp(value, "previous"))
6848 focus_close = SWM_STACK_BELOW;
6849 else
6850 errx(1, "focus_close");
6851 break;
6852 case SWM_S_FOCUS_CLOSE_WRAP:
6853 focus_close_wrap = atoi(value);
6854 break;
6855 case SWM_S_FOCUS_DEFAULT:
6856 if (!strcmp(value, "last"))
6857 focus_default = SWM_STACK_TOP;
6858 else if (!strcmp(value, "first"))
6859 focus_default = SWM_STACK_BOTTOM;
6860 else
6861 errx(1, "focus_default");
6862 break;
6863 case SWM_S_FOCUS_MODE:
6864 if (!strcmp(value, "default"))
6865 focus_mode = SWM_FOCUS_DEFAULT;
6866 else if (!strcmp(value, "follow") ||
6867 !strcmp(value, "follow_cursor"))
6868 focus_mode = SWM_FOCUS_FOLLOW;
6869 else if (!strcmp(value, "manual"))
6870 focus_mode = SWM_FOCUS_MANUAL;
6871 else
6872 errx(1, "focus_mode");
6873 break;
6874 case SWM_S_REGION_PADDING:
6875 region_padding = atoi(value);
6876 if (region_padding < 0)
6877 region_padding = 0;
6878 break;
6879 case SWM_S_SPAWN_ORDER:
6880 if (!strcmp(value, "first"))
6881 spawn_position = SWM_STACK_BOTTOM;
6882 else if (!strcmp(value, "last"))
6883 spawn_position = SWM_STACK_TOP;
6884 else if (!strcmp(value, "next"))
6885 spawn_position = SWM_STACK_ABOVE;
6886 else if (!strcmp(value, "previous"))
6887 spawn_position = SWM_STACK_BELOW;
6888 else
6889 errx(1, "spawn_position");
6890 break;
6891 case SWM_S_SPAWN_TERM:
6892 setconfspawn("term", value, 0);
6893 setconfspawn("spawn_term", value, 0);
6894 break;
6895 case SWM_S_SS_APP:
6896 /* No longer needed; leave to not break old conf files. */
6897 break;
6898 case SWM_S_SS_ENABLED:
6899 /* No longer needed; leave to not break old conf files. */
6900 break;
6901 case SWM_S_STACK_ENABLED:
6902 stack_enabled = atoi(value);
6903 break;
6904 case SWM_S_TERM_WIDTH:
6905 term_width = atoi(value);
6906 if (term_width < 0)
6907 term_width = 0;
6908 break;
6909 case SWM_S_TILE_GAP:
6910 tile_gap = atoi(value);
6911 if (tile_gap < 0)
6912 tile_gap = 0;
6913 break;
6914 case SWM_S_TITLE_CLASS_ENABLED:
6915 title_class_enabled = atoi(value);
6916 break;
6917 case SWM_S_TITLE_NAME_ENABLED:
6918 title_name_enabled = atoi(value);
6919 break;
6920 case SWM_S_URGENT_ENABLED:
6921 urgent_enabled = atoi(value);
6922 break;
6923 case SWM_S_VERBOSE_LAYOUT:
6924 verbose_layout = atoi(value);
6925 for (i = 0; layouts[i].l_stack != NULL; i++) {
6926 if (verbose_layout)
6927 layouts[i].l_string = fancy_stacker;
6928 else
6929 layouts[i].l_string = plain_stacker;
6930 }
6931 break;
6932 case SWM_S_WINDOW_NAME_ENABLED:
6933 window_name_enabled = atoi(value);
6934 break;
6935 case SWM_S_WORKSPACE_LIMIT:
6936 workspace_limit = atoi(value);
6937 if (workspace_limit > SWM_WS_MAX)
6938 workspace_limit = SWM_WS_MAX;
6939 else if (workspace_limit < 1)
6940 workspace_limit = 1;
6941 break;
6942 default:
6943 return (1);
6944 }
6945 return (0);
6946 }
6947
6948 int
6949 setconfmodkey(char *selector, char *value, int flags)
6950 {
6951 /* suppress unused warnings since vars are needed */
6952 (void)selector;
6953 (void)flags;
6954
6955 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
6956 update_modkey(XCB_MOD_MASK_1);
6957 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
6958 update_modkey(XCB_MOD_MASK_2);
6959 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
6960 update_modkey(XCB_MOD_MASK_3);
6961 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
6962 update_modkey(XCB_MOD_MASK_4);
6963 else
6964 return (1);
6965 return (0);
6966 }
6967
6968 int
6969 setconfcolor(char *selector, char *value, int flags)
6970 {
6971 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
6972 return (0);
6973 }
6974
6975 int
6976 setconfregion(char *selector, char *value, int flags)
6977 {
6978 /* suppress unused warnings since vars are needed */
6979 (void)selector;
6980 (void)flags;
6981
6982 custom_region(value);
6983 return (0);
6984 }
6985
6986 int
6987 setautorun(char *selector, char *value, int flags)
6988 {
6989 int ws_id;
6990 char s[1024];
6991 char *ap, *sp;
6992 union arg a;
6993 int argc = 0;
6994 pid_t pid;
6995 struct pid_e *p;
6996
6997 /* suppress unused warnings since vars are needed */
6998 (void)selector;
6999 (void)flags;
7000
7001 if (getenv("SWM_STARTED"))
7002 return (0);
7003
7004 bzero(s, sizeof s);
7005 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
7006 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
7007 ws_id--;
7008 if (ws_id < 0 || ws_id >= workspace_limit)
7009 errx(1, "autorun: invalid workspace %d", ws_id + 1);
7010
7011 sp = expand_tilde((char *)&s);
7012
7013 /*
7014 * This is a little intricate
7015 *
7016 * If the pid already exists we simply reuse it because it means it was
7017 * used before AND not claimed by manage_window. We get away with
7018 * altering it in the parent after INSERT because this can not be a race
7019 */
7020 a.argv = NULL;
7021 while ((ap = strsep(&sp, " \t")) != NULL) {
7022 if (*ap == '\0')
7023 continue;
7024 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
7025 argc++;
7026 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
7027 err(1, "setautorun: realloc");
7028 a.argv[argc - 1] = ap;
7029 }
7030 free(sp);
7031
7032 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
7033 err(1, "setautorun: realloc");
7034 a.argv[argc] = NULL;
7035
7036 if ((pid = fork()) == 0) {
7037 spawn(ws_id, &a, 1);
7038 /* NOTREACHED */
7039 _exit(1);
7040 }
7041 free(a.argv);
7042
7043 /* parent */
7044 p = find_pid(pid);
7045 if (p == NULL) {
7046 p = calloc(1, sizeof *p);
7047 if (p == NULL)
7048 return (1);
7049 TAILQ_INSERT_TAIL(&pidlist, p, entry);
7050 }
7051
7052 p->pid = pid;
7053 p->ws = ws_id;
7054
7055 return (0);
7056 }
7057
7058 int
7059 setlayout(char *selector, char *value, int flags)
7060 {
7061 int ws_id, i, x, mg, ma, si, ar, f = 0;
7062 int st = SWM_V_STACK, num_screens;
7063 char s[1024];
7064 struct workspace *ws;
7065
7066 /* suppress unused warnings since vars are needed */
7067 (void)selector;
7068 (void)flags;
7069
7070 if (getenv("SWM_STARTED"))
7071 return (0);
7072
7073 bzero(s, sizeof s);
7074 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
7075 &ws_id, &mg, &ma, &si, &ar, s) != 6)
7076 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7077 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7078 "<type>'");
7079 ws_id--;
7080 if (ws_id < 0 || ws_id >= workspace_limit)
7081 errx(1, "layout: invalid workspace %d", ws_id + 1);
7082
7083 if (!strcasecmp(s, "vertical"))
7084 st = SWM_V_STACK;
7085 else if (!strcasecmp(s, "vertical_flip")) {
7086 st = SWM_V_STACK;
7087 f = 1;
7088 } else if (!strcasecmp(s, "horizontal"))
7089 st = SWM_H_STACK;
7090 else if (!strcasecmp(s, "horizontal_flip")) {
7091 st = SWM_H_STACK;
7092 f = 1;
7093 } else if (!strcasecmp(s, "fullscreen"))
7094 st = SWM_MAX_STACK;
7095 else
7096 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7097 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7098 "<type>'");
7099
7100 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7101 for (i = 0; i < num_screens; i++) {
7102 ws = (struct workspace *)&screens[i].ws;
7103 ws[ws_id].cur_layout = &layouts[st];
7104
7105 ws[ws_id].always_raise = ar;
7106 if (st == SWM_MAX_STACK)
7107 continue;
7108
7109 /* master grow */
7110 for (x = 0; x < abs(mg); x++) {
7111 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7112 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
7113 SWM_ARG_ID_MASTERSHRINK);
7114 stack();
7115 }
7116 /* master add */
7117 for (x = 0; x < abs(ma); x++) {
7118 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7119 ma >= 0 ? SWM_ARG_ID_MASTERADD :
7120 SWM_ARG_ID_MASTERDEL);
7121 stack();
7122 }
7123 /* stack inc */
7124 for (x = 0; x < abs(si); x++) {
7125 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7126 si >= 0 ? SWM_ARG_ID_STACKINC :
7127 SWM_ARG_ID_STACKDEC);
7128 stack();
7129 }
7130 /* Apply flip */
7131 if (f) {
7132 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7133 SWM_ARG_ID_FLIPLAYOUT);
7134 stack();
7135 }
7136 }
7137
7138 focus_flush();
7139
7140 return (0);
7141 }
7142
7143 /* config options */
7144 struct config_option {
7145 char *optname;
7146 int (*func)(char*, char*, int);
7147 int funcflags;
7148 };
7149 struct config_option configopt[] = {
7150 { "autorun", setautorun, 0 },
7151 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
7152 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
7153 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
7154 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
7155 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
7156 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
7157 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
7158 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
7159 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
7160 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
7161 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
7162 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
7163 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
7164 { "bind", setconfbinding, 0 },
7165 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
7166 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
7167 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
7168 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
7169 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
7170 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
7171 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
7172 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
7173 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
7174 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
7175 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
7176 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
7177 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
7178 { "keyboard_mapping", setkeymapping, 0 },
7179 { "layout", setlayout, 0 },
7180 { "modkey", setconfmodkey, 0 },
7181 { "program", setconfspawn, 0 },
7182 { "quirk", setconfquirk, 0 },
7183 { "region", setconfregion, 0 },
7184 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
7185 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
7186 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
7187 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
7188 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
7189 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
7190 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
7191 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
7192 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
7193 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
7194 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
7195 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
7196 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
7197 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
7198 };
7199
7200 void
7201 _add_startup_exception(const char *fmt, va_list ap)
7202 {
7203 if (vasprintf(&startup_exception, fmt, ap) == -1)
7204 warn("%s: asprintf", __func__);
7205 }
7206
7207 void
7208 add_startup_exception(const char *fmt, ...)
7209 {
7210 va_list ap;
7211
7212 nr_exceptions++;
7213
7214 if (startup_exception)
7215 return;
7216
7217 /* force bar to be enabled due to exception */
7218 bar_enabled = 1;
7219
7220 va_start(ap, fmt);
7221 _add_startup_exception(fmt, ap);
7222 va_end(ap);
7223 }
7224
7225 int
7226 conf_load(const char *filename, int keymapping)
7227 {
7228 FILE *config;
7229 char *line = NULL, *cp, *optsub, *optval;
7230 size_t linelen, lineno = 0;
7231 int wordlen, i, optidx;
7232 struct config_option *opt = NULL;
7233
7234 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
7235
7236 if (filename == NULL) {
7237 warnx("conf_load: no filename");
7238 return (1);
7239 }
7240 if ((config = fopen(filename, "r")) == NULL) {
7241 warn("conf_load: fopen: %s", filename);
7242 return (1);
7243 }
7244
7245 while (!feof(config)) {
7246 if (line)
7247 free(line);
7248
7249 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
7250 == NULL) {
7251 if (ferror(config))
7252 err(1, "%s", filename);
7253 else
7254 continue;
7255 }
7256 cp = line;
7257 cp += strspn(cp, " \t\n"); /* eat whitespace */
7258 if (cp[0] == '\0') {
7259 /* empty line */
7260 continue;
7261 }
7262 /* get config option */
7263 wordlen = strcspn(cp, "=[ \t\n");
7264 if (wordlen == 0) {
7265 add_startup_exception("%s: line %zd: no option found",
7266 filename, lineno);
7267 continue;
7268 }
7269 optidx = -1;
7270 for (i = 0; i < LENGTH(configopt); i++) {
7271 opt = &configopt[i];
7272 if (!strncasecmp(cp, opt->optname, wordlen) &&
7273 (int)strlen(opt->optname) == wordlen) {
7274 optidx = i;
7275 break;
7276 }
7277 }
7278 if (optidx == -1) {
7279 add_startup_exception("%s: line %zd: unknown option "
7280 "%.*s", filename, lineno, wordlen, cp);
7281 continue;
7282 }
7283 if (keymapping && opt && strcmp(opt->optname, "bind")) {
7284 add_startup_exception("%s: line %zd: invalid option "
7285 "%.*s", filename, lineno, wordlen, cp);
7286 continue;
7287 }
7288 cp += wordlen;
7289 cp += strspn(cp, " \t\n"); /* eat whitespace */
7290
7291 /* from here on out we call goto invalid to continue */
7292
7293 /* get [selector] if any */
7294 optsub = NULL;
7295 if (*cp == '[') {
7296 cp++;
7297 wordlen = strcspn(cp, "]");
7298 if (*cp != ']') {
7299 if (wordlen == 0) {
7300 add_startup_exception("%s: line %zd: "
7301 "syntax error", filename, lineno);
7302 goto invalid;
7303 }
7304
7305 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
7306 -1) {
7307 add_startup_exception("%s: line %zd: "
7308 "unable to allocatememory for "
7309 "selector", filename, lineno);
7310 goto invalid;
7311 }
7312 }
7313 cp += wordlen;
7314 cp += strspn(cp, "] \t\n"); /* eat trailing */
7315 }
7316 cp += strspn(cp, "= \t\n"); /* eat trailing */
7317 /* get RHS value */
7318 optval = strdup(cp);
7319 /* call function to deal with it all */
7320 if (configopt[optidx].func(optsub, optval,
7321 configopt[optidx].funcflags) != 0) {
7322 add_startup_exception("%s: line %zd: invalid data for "
7323 "%s", filename, lineno, configopt[optidx].optname);
7324 goto invalid;
7325 }
7326 invalid:
7327 if (optval) {
7328 free(optval);
7329 optval = NULL;
7330 }
7331 if (optsub) {
7332 free(optsub);
7333 optsub = NULL;
7334 }
7335 }
7336
7337 if (line)
7338 free(line);
7339 fclose(config);
7340 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
7341
7342 return (0);
7343 }
7344
7345 void
7346 set_child_transient(struct ws_win *win, xcb_window_t *trans)
7347 {
7348 struct ws_win *parent, *w;
7349 struct swm_region *r;
7350 struct workspace *ws;
7351 xcb_icccm_wm_hints_t wmh;
7352
7353 parent = find_window(win->transient);
7354 if (parent)
7355 parent->focus_child = win;
7356 else {
7357 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
7358 " for 0x%x trans 0x%x\n", win->id, win->transient);
7359
7360 r = root_to_region(win->wa->root, SWM_CK_ALL);
7361 ws = r->ws;
7362 /* parent doen't exist in our window list */
7363 TAILQ_FOREACH(w, &ws->winlist, entry) {
7364 if (xcb_icccm_get_wm_hints_reply(conn,
7365 xcb_icccm_get_wm_hints(conn, w->id),
7366 &wmh, NULL) != 1) {
7367 warnx("can't get hints for 0x%x", w->id);
7368 continue;
7369 }
7370
7371 if (win->hints.window_group != wmh.window_group)
7372 continue;
7373
7374 w->focus_child = win;
7375 win->transient = w->id;
7376 *trans = w->id;
7377 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
7378 "transient to 0x%x\n", win->transient);
7379 break;
7380 }
7381 }
7382 }
7383
7384 pid_t
7385 window_get_pid(xcb_window_t win)
7386 {
7387 pid_t ret = 0;
7388 const char *errstr;
7389 xcb_atom_t apid;
7390 xcb_get_property_cookie_t pc;
7391 xcb_get_property_reply_t *pr;
7392
7393 apid = get_atom_from_string("_NET_WM_PID");
7394 if (apid == XCB_ATOM_NONE)
7395 goto tryharder;
7396
7397 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
7398 pr = xcb_get_property_reply(conn, pc, NULL);
7399 if (!pr)
7400 goto tryharder;
7401 if (pr->type != XCB_ATOM_CARDINAL) {
7402 free(pr);
7403 goto tryharder;
7404 }
7405
7406 if (pr->type == apid && pr->format == 32)
7407 ret = *((pid_t *)xcb_get_property_value(pr));
7408 free(pr);
7409
7410 return (ret);
7411
7412 tryharder:
7413 apid = get_atom_from_string("_SWM_PID");
7414 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
7415 0, SWM_PROPLEN);
7416 pr = xcb_get_property_reply(conn, pc, NULL);
7417 if (!pr)
7418 return (0);
7419 if (pr->type != apid) {
7420 free(pr);
7421 return (0);
7422 }
7423
7424 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
7425 free(pr);
7426
7427 return (ret);
7428 }
7429
7430 int
7431 get_ws_idx(xcb_window_t id)
7432 {
7433 int ws_idx = -1;
7434 char *prop = NULL;
7435 size_t proplen;
7436 const char *errstr;
7437 xcb_get_property_reply_t *gpr;
7438
7439 gpr = xcb_get_property_reply(conn,
7440 xcb_get_property(conn, 0, id, a_swm_ws,
7441 XCB_ATOM_STRING, 0, SWM_PROPLEN),
7442 NULL);
7443 if (!gpr)
7444 return (-1);
7445 if (gpr->type) {
7446 proplen = xcb_get_property_value_length(gpr);
7447 if (proplen > 0) {
7448 prop = malloc(proplen + 1);
7449 if (prop) {
7450 memcpy(prop,
7451 xcb_get_property_value(gpr),
7452 proplen);
7453 prop[proplen] = '\0';
7454 }
7455 }
7456 }
7457 free(gpr);
7458
7459 if (prop) {
7460 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
7461 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
7462 if (errstr) {
7463 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
7464 errstr, prop);
7465 }
7466 free(prop);
7467 }
7468
7469 return ws_idx;
7470 }
7471
7472 struct ws_win *
7473 manage_window(xcb_window_t id, uint16_t mapped)
7474 {
7475 xcb_window_t trans = XCB_WINDOW_NONE;
7476 struct ws_win *win, *ww;
7477 int ws_idx;
7478 char ws_idx_str[SWM_PROPLEN];
7479 struct swm_region *r;
7480 struct pid_e *p;
7481 struct quirk *qp;
7482 uint32_t i, wa[2];
7483 xcb_icccm_get_wm_protocols_reply_t wpr;
7484
7485 if ((win = find_window(id)) != NULL) {
7486 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x already "
7487 "managed; skipping.)\n", id);
7488 return (win); /* Already managed. */
7489 }
7490
7491 /* See if window is on the unmanaged list. */
7492 if ((win = find_unmanaged_window(id)) != NULL) {
7493 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x found on "
7494 "unmanaged list.\n", id);
7495 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7496
7497 if (win->transient)
7498 set_child_transient(win, &trans);
7499
7500 goto out;
7501 } else {
7502 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x is new.\n", id);
7503 }
7504
7505 /* Create and initialize ws_win object. */
7506 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
7507 err(1, "manage_window: calloc: failed to allocate memory for "
7508 "new window");
7509
7510 win->id = id;
7511
7512 /* Get window geometry. */
7513 win->wa = xcb_get_geometry_reply(conn,
7514 xcb_get_geometry(conn, win->id),
7515 NULL);
7516
7517 /* Figure out which region the window belongs to. */
7518 r = root_to_region(win->wa->root, SWM_CK_ALL);
7519
7520 /* Ignore window border if there is one. */
7521 WIDTH(win) = win->wa->width;
7522 HEIGHT(win) = win->wa->height;
7523 X(win) = win->wa->x + win->wa->border_width - border_width;
7524 Y(win) = win->wa->y + win->wa->border_width - border_width;
7525 win->bordered = 1;
7526 win->mapped = mapped;
7527 win->floatmaxed = 0;
7528 win->ewmh_flags = 0;
7529 win->s = r->s; /* this never changes */
7530
7531 store_float_geom(win, r);
7532
7533 /* Get WM_SIZE_HINTS. */
7534 xcb_icccm_get_wm_normal_hints_reply(conn,
7535 xcb_icccm_get_wm_normal_hints(conn, win->id),
7536 &win->sh, NULL);
7537
7538 /* Get WM_HINTS. */
7539 xcb_icccm_get_wm_hints_reply(conn,
7540 xcb_icccm_get_wm_hints(conn, win->id),
7541 &win->hints, NULL);
7542
7543 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
7544 xcb_icccm_get_wm_transient_for_reply(conn,
7545 xcb_icccm_get_wm_transient_for(conn, win->id),
7546 &trans, NULL);
7547 if (trans) {
7548 win->transient = trans;
7549 set_child_transient(win, &win->transient);
7550 }
7551
7552 /* Get supported protocols. */
7553 if (xcb_icccm_get_wm_protocols_reply(conn,
7554 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
7555 &wpr, NULL)) {
7556 for (i = 0; i < wpr.atoms_len; i++) {
7557 if (wpr.atoms[i] == a_takefocus)
7558 win->take_focus = 1;
7559 if (wpr.atoms[i] == a_delete)
7560 win->can_delete = 1;
7561 }
7562 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
7563 }
7564
7565 win->iconic = get_swm_iconic(win);
7566
7567 /* Figure out which workspace the window belongs to. */
7568 if ((p = find_pid(window_get_pid(win->id))) != NULL) {
7569 win->ws = &r->s->ws[p->ws];
7570 TAILQ_REMOVE(&pidlist, p, entry);
7571 free(p);
7572 p = NULL;
7573 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
7574 !win->transient) {
7575 /* _SWM_WS is set; use that. */
7576 win->ws = &r->s->ws[ws_idx];
7577 } else if (trans && (ww = find_window(trans)) != NULL) {
7578 /* Launch transients in the same ws as parent. */
7579 win->ws = ww->ws;
7580 } else {
7581 win->ws = r->ws;
7582 }
7583
7584 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
7585 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", win->ws->idx) <
7586 SWM_PROPLEN) {
7587 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
7588 ws_idx_str);
7589 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
7590 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
7591 ws_idx_str);
7592 }
7593
7594 /* Handle EWMH */
7595 ewmh_autoquirk(win);
7596
7597 /* Determine initial quirks. */
7598 if (xcb_icccm_get_wm_class_reply(conn,
7599 xcb_icccm_get_wm_class(conn, win->id),
7600 &win->ch, NULL)) {
7601 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
7602 win->ch.class_name, win->ch.instance_name);
7603
7604 /* java is retarded so treat it special */
7605 if (strstr(win->ch.instance_name, "sun-awt")) {
7606 DNPRINTF(SWM_D_CLASS, "manage_window: java window "
7607 "detected.\n");
7608 win->java = 1;
7609 }
7610
7611 TAILQ_FOREACH(qp, &quirks, entry) {
7612 if (!strcmp(win->ch.class_name, qp->class) &&
7613 !strcmp(win->ch.instance_name, qp->name)) {
7614 DNPRINTF(SWM_D_CLASS, "manage_window: on quirks"
7615 "list; mask: 0x%lx\n", qp->quirk);
7616 if (qp->quirk & SWM_Q_FLOAT)
7617 win->floating = 1;
7618 win->quirks = qp->quirk;
7619 }
7620 }
7621 }
7622
7623 /* Alter window position if quirky */
7624 if (win->quirks & SWM_Q_ANYWHERE)
7625 win->manual = 1;
7626
7627 /* Reset font sizes (the bruteforce way; no default keybinding). */
7628 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
7629 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7630 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
7631 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7632 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
7633 }
7634
7635 /* Make sure window is positioned inside its region, if its active. */
7636 if (win->ws->r) {
7637 constrain_window(win, win->ws->r, 0);
7638 update_window(win);
7639 }
7640
7641 /* Select which X events to monitor and set border pixel color. */
7642 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
7643 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
7644 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
7645 #ifdef SWM_DEBUG
7646 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
7647 #endif
7648
7649 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
7650 XCB_CW_EVENT_MASK, wa);
7651
7652 out:
7653 /* Figure out where to stack the window in the workspace. */
7654 if (trans && (ww = find_window(trans)))
7655 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
7656 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
7657 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
7658 entry);
7659 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
7660 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
7661 else switch (spawn_position) {
7662 default:
7663 case SWM_STACK_TOP:
7664 case SWM_STACK_ABOVE:
7665 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
7666 break;
7667 case SWM_STACK_BOTTOM:
7668 case SWM_STACK_BELOW:
7669 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
7670 }
7671
7672 /* Get initial _NET_WM_STATE */
7673 ewmh_get_win_state(win);
7674 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7675 ewmh_update_actions(win);
7676
7677 grabbuttons(win);
7678
7679 DNPRINTF(SWM_D_MISC, "manage_window: done. window: 0x%x, (x,y) w x h: "
7680 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win->id,
7681 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
7682 YESNO(win->iconic), win->transient);
7683
7684 return (win);
7685 }
7686
7687 void
7688 free_window(struct ws_win *win)
7689 {
7690 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
7691
7692 if (win == NULL)
7693 return;
7694
7695 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7696
7697 if (win->wa)
7698 free(win->wa);
7699
7700 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
7701
7702 kill_refs(win);
7703
7704 /* paint memory */
7705 memset(win, 0xff, sizeof *win); /* XXX kill later */
7706
7707 free(win);
7708 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
7709 }
7710
7711 void
7712 unmanage_window(struct ws_win *win)
7713 {
7714 struct ws_win *parent;
7715
7716 if (win == NULL)
7717 return;
7718
7719 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
7720
7721 if (win->transient) {
7722 parent = find_window(win->transient);
7723 if (parent)
7724 parent->focus_child = NULL;
7725 }
7726
7727 TAILQ_REMOVE(&win->ws->winlist, win, entry);
7728 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
7729 }
7730
7731 void
7732 expose(xcb_expose_event_t *e)
7733 {
7734 int i, num_screens;
7735 struct swm_region *r;
7736
7737 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
7738
7739 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7740 for (i = 0; i < num_screens; i++)
7741 TAILQ_FOREACH(r, &screens[i].rl, entry)
7742 if (e->window == WINID(r->bar))
7743 bar_draw();
7744
7745 xcb_flush(conn);
7746 }
7747
7748 #ifdef SWM_DEBUG
7749 void
7750 focusin(xcb_focus_in_event_t *e)
7751 {
7752 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
7753 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7754 e->mode, get_notify_detail_label(e->detail), e->detail);
7755 }
7756
7757 void
7758 focusout(xcb_focus_out_event_t *e)
7759 {
7760 DNPRINTF(SWM_D_EVENT, "focusout: window: 0x%x, mode: %s(%u), "
7761 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7762 e->mode, get_notify_detail_label(e->detail), e->detail);
7763 }
7764 #endif
7765
7766 void
7767 keypress(xcb_key_press_event_t *e)
7768 {
7769 xcb_keysym_t keysym;
7770 struct key *kp;
7771
7772 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
7773
7774 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): 0x%x (%d,%d), "
7775 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7776 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
7777 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
7778 e->child, e->state, YESNO(e->same_screen));
7779
7780 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
7781 goto out;
7782
7783 last_event_time = e->time;
7784
7785 if (kp->funcid == KF_SPAWN_CUSTOM)
7786 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
7787 &(keyfuncs[kp->funcid].args), kp->spawn_name);
7788 else if (keyfuncs[kp->funcid].func)
7789 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
7790 &(keyfuncs[kp->funcid].args));
7791
7792 out:
7793 /* Unfreeze grab events. */
7794 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD, e->time);
7795 xcb_flush(conn);
7796
7797 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
7798 }
7799
7800 void
7801 buttonpress(xcb_button_press_event_t *e)
7802 {
7803 struct ws_win *win = NULL;
7804 struct swm_region *r, *old_r;
7805 int i;
7806 int handled = 0;
7807
7808 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): 0x%x (%d,%d), "
7809 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7810 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
7811 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
7812 e->state, YESNO(e->same_screen));
7813
7814 if (e->event == e->root) {
7815 if (e->child != 0) {
7816 win = find_window(e->child);
7817 /* Pass ButtonPress to window if it isn't managed. */
7818 if (win == NULL)
7819 goto out;
7820 } else {
7821 /* Focus on empty region */
7822 /* If no windows on region if its empty. */
7823 r = root_to_region(e->root, SWM_CK_POINTER);
7824 if (r == NULL) {
7825 DNPRINTF(SWM_D_EVENT, "buttonpress: "
7826 "NULL region; ignoring.\n");
7827 goto out;
7828 }
7829
7830 if (TAILQ_EMPTY(&r->ws->winlist)) {
7831 old_r = root_to_region(e->root, SWM_CK_FOCUS);
7832 if (old_r && old_r != r)
7833 unfocus_win(old_r->ws->focus);
7834
7835 xcb_set_input_focus(conn,
7836 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
7837
7838 /* Clear bar since empty. */
7839 bar_draw();
7840
7841 handled = 1;
7842 goto out;
7843 }
7844 }
7845 } else {
7846 win = find_window(e->event);
7847 }
7848
7849 if (win == NULL)
7850 goto out;
7851
7852 last_event_time = e->time;
7853
7854 focus_win(get_focus_magic(win));
7855
7856 for (i = 0; i < LENGTH(buttons); i++)
7857 if (client_click == buttons[i].action && buttons[i].func &&
7858 buttons[i].button == e->detail &&
7859 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
7860 buttons[i].func(win, &buttons[i].args);
7861 handled = 1;
7862 }
7863
7864 out:
7865 if (!handled) {
7866 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
7867 /* Replay event to event window */
7868 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
7869 } else {
7870 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
7871 /* Unfreeze grab events. */
7872 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
7873 }
7874
7875 xcb_flush(conn);
7876 }
7877
7878 #ifdef SWM_DEBUG
7879 void
7880 print_win_geom(xcb_window_t w)
7881 {
7882 xcb_get_geometry_reply_t *wa;
7883
7884 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
7885
7886 if (!wa) {
7887 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
7888 w);
7889 return;
7890 }
7891
7892 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
7893 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7894 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
7895 wa->border_width);
7896
7897 free(wa);
7898 }
7899 #endif
7900
7901 #ifdef SWM_DEBUG
7902 char *
7903 get_stack_mode_name(uint8_t mode)
7904 {
7905 char *name;
7906
7907 switch(mode) {
7908 case XCB_STACK_MODE_ABOVE:
7909 name = "Above";
7910 break;
7911 case XCB_STACK_MODE_BELOW:
7912 name = "Below";
7913 break;
7914 case XCB_STACK_MODE_TOP_IF:
7915 name = "TopIf";
7916 break;
7917 case XCB_STACK_MODE_BOTTOM_IF:
7918 name = "BottomIf";
7919 break;
7920 case XCB_STACK_MODE_OPPOSITE:
7921 name = "Opposite";
7922 break;
7923 default:
7924 name = "Unknown";
7925 }
7926
7927 return name;
7928 }
7929 #endif
7930
7931 void
7932 configurerequest(xcb_configure_request_event_t *e)
7933 {
7934 struct ws_win *win;
7935 struct swm_region *r = NULL;
7936 int new = 0, i = 0;
7937 uint16_t mask = 0;
7938 uint32_t wc[7] = {0};
7939
7940 if ((win = find_window(e->window)) == NULL)
7941 if ((win = find_unmanaged_window(e->window)) == NULL)
7942 new = 1;
7943
7944 #ifdef SWM_DEBUG
7945 if (swm_debug & SWM_D_EVENT) {
7946 print_win_geom(e->window);
7947
7948 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%x, "
7949 "parent: 0x%x, new: %s, value_mask: %u { ", e->window,
7950 e->parent, YESNO(new), e->value_mask);
7951 if (e->value_mask & XCB_CONFIG_WINDOW_X)
7952 DPRINTF("X: %d ", e->x);
7953 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
7954 DPRINTF("Y: %d ", e->y);
7955 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
7956 DPRINTF("W: %u ", e->width);
7957 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
7958 DPRINTF("H: %u ", e->height);
7959 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
7960 DPRINTF("Border: %u ", e->border_width);
7961 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
7962 DPRINTF("Sibling: 0x%x ", e->sibling);
7963 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
7964 DPRINTF("StackMode: %s(%u) ",
7965 get_stack_mode_name(e->stack_mode), e->stack_mode);
7966 DPRINTF("}\n");
7967 }
7968 #endif
7969
7970 if (new) {
7971 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7972 mask |= XCB_CONFIG_WINDOW_X;
7973 wc[i++] = e->x;
7974 }
7975 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7976 mask |= XCB_CONFIG_WINDOW_Y;
7977 wc[i++] = e->y;
7978 }
7979 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
7980 mask |= XCB_CONFIG_WINDOW_WIDTH;
7981 wc[i++] = e->width;
7982 }
7983 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
7984 mask |= XCB_CONFIG_WINDOW_HEIGHT;
7985 wc[i++] = e->height;
7986 }
7987 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
7988 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
7989 wc[i++] = e->border_width;
7990 }
7991 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
7992 mask |= XCB_CONFIG_WINDOW_SIBLING;
7993 wc[i++] = e->sibling;
7994 }
7995 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
7996 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
7997 wc[i++] = e->stack_mode;
7998 }
7999
8000 if (mask != 0) {
8001 xcb_configure_window(conn, e->window, mask, wc);
8002 xcb_flush(conn);
8003 }
8004 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
8005 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
8006 if (win->ws->r)
8007 r = win->ws->r;
8008 else if (win->ws->old_r)
8009 r = win->ws->old_r;
8010
8011 /* windows are centered unless ANYWHERE quirk is set. */
8012 if (win->quirks & SWM_Q_ANYWHERE) {
8013 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
8014 win->g_float.x = e->x;
8015 if (r)
8016 win->g_float.x -= X(r);
8017 }
8018
8019 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
8020 win->g_float.y = e->y;
8021 if (r)
8022 win->g_float.y -= Y(r);
8023 }
8024 }
8025
8026 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
8027 win->g_float.w = e->width;
8028
8029 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
8030 win->g_float.h = e->height;
8031
8032 win->g_floatvalid = 1;
8033
8034 if (win->floating && r && (win->transient ||
8035 win->ws->cur_layout != &layouts[SWM_MAX_STACK])) {
8036 WIDTH(win) = win->g_float.w;
8037 HEIGHT(win) = win->g_float.h;
8038
8039 if (r) {
8040 stack_floater(win, r);
8041 focus_flush();
8042 }
8043 } else {
8044 config_win(win, e);
8045 xcb_flush(conn);
8046 }
8047 } else {
8048 config_win(win, e);
8049 xcb_flush(conn);
8050 }
8051
8052 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
8053 }
8054
8055 void
8056 configurenotify(xcb_configure_notify_event_t *e)
8057 {
8058 struct ws_win *win;
8059
8060 DNPRINTF(SWM_D_EVENT, "configurenotify: win 0x%x, event win: 0x%x, "
8061 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
8062 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
8063 e->width, e->height, e->border_width, e->above_sibling,
8064 YESNO(e->override_redirect));
8065
8066 win = find_window(e->window);
8067 if (win) {
8068 xcb_icccm_get_wm_normal_hints_reply(conn,
8069 xcb_icccm_get_wm_normal_hints(conn, win->id),
8070 &win->sh, NULL);
8071 adjust_font(win);
8072 if (font_adjusted) {
8073 stack();
8074 xcb_flush(conn);
8075 }
8076 }
8077 }
8078
8079 void
8080 destroynotify(xcb_destroy_notify_event_t *e)
8081 {
8082 struct ws_win *win;
8083
8084 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
8085
8086 if ((win = find_window(e->window)) == NULL) {
8087 if ((win = find_unmanaged_window(e->window)) == NULL)
8088 return;
8089 free_window(win);
8090 return;
8091 }
8092
8093 if (focus_mode != SWM_FOCUS_FOLLOW) {
8094 /* If we were focused, make sure we focus on something else. */
8095 if (win == win->ws->focus)
8096 win->ws->focus_pending = get_focus_prev(win);
8097 }
8098
8099 unmanage_window(win);
8100 stack();
8101
8102 if (focus_mode != SWM_FOCUS_FOLLOW) {
8103 if (win->ws->focus_pending) {
8104 focus_win(win->ws->focus_pending);
8105 win->ws->focus_pending = NULL;
8106 }
8107 }
8108
8109 free_window(win);
8110
8111 focus_flush();
8112 }
8113
8114 #ifdef SWM_DEBUG
8115 char *
8116 get_notify_detail_label(uint8_t detail)
8117 {
8118 char *label;
8119
8120 switch (detail) {
8121 case XCB_NOTIFY_DETAIL_ANCESTOR:
8122 label = "Ancestor";
8123 break;
8124 case XCB_NOTIFY_DETAIL_VIRTUAL:
8125 label = "Virtual";
8126 break;
8127 case XCB_NOTIFY_DETAIL_INFERIOR:
8128 label = "Inferior";
8129 break;
8130 case XCB_NOTIFY_DETAIL_NONLINEAR:
8131 label = "Nonlinear";
8132 break;
8133 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
8134 label = "NonlinearVirtual";
8135 break;
8136 case XCB_NOTIFY_DETAIL_POINTER:
8137 label = "Pointer";
8138 break;
8139 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
8140 label = "PointerRoot";
8141 break;
8142 case XCB_NOTIFY_DETAIL_NONE:
8143 label = "None";
8144 break;
8145 default:
8146 label = "Unknown";
8147 }
8148
8149 return label;
8150 }
8151
8152 char *
8153 get_notify_mode_label(uint8_t mode)
8154 {
8155 char *label;
8156
8157 switch (mode) {
8158 case XCB_NOTIFY_MODE_NORMAL:
8159 label = "Normal";
8160 break;
8161 case XCB_NOTIFY_MODE_GRAB:
8162 label = "Grab";
8163 break;
8164 case XCB_NOTIFY_MODE_UNGRAB:
8165 label = "Ungrab";
8166 break;
8167 case XCB_NOTIFY_MODE_WHILE_GRABBED:
8168 label = "WhileGrabbed";
8169 break;
8170 default:
8171 label = "Unknown";
8172 }
8173
8174 return label;
8175 }
8176 #endif
8177
8178 void
8179 enternotify(xcb_enter_notify_event_t *e)
8180 {
8181 struct ws_win *win;
8182 struct swm_region *r;
8183
8184 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): 0x%x "
8185 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8186 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8187 e->time, e->event, e->event_x, e->event_y,
8188 get_notify_mode_label(e->mode), e->mode,
8189 get_notify_detail_label(e->detail), e->detail,
8190 e->root, e->root_x, e->root_y, e->child,
8191 YESNO(e->same_screen_focus), e->state);
8192
8193 if (focus_mode == SWM_FOCUS_MANUAL &&
8194 e->mode == XCB_NOTIFY_MODE_NORMAL) {
8195 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
8196 return;
8197 }
8198
8199 last_event_time = e->time;
8200
8201 if ((win = find_window(e->event)) == NULL) {
8202 if (e->event == e->root) {
8203 /* If no windows on pointer region, then focus root. */
8204 r = root_to_region(e->root, SWM_CK_POINTER);
8205 if (r == NULL) {
8206 DNPRINTF(SWM_D_EVENT, "enterntoify: "
8207 "NULL region; ignoring.\n");
8208 return;
8209 }
8210
8211 focus_region(r);
8212 } else {
8213 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
8214 "ignoring\n");
8215 return;
8216 }
8217 } else {
8218 focus_win(get_focus_magic(win));
8219 }
8220
8221 focus_flush();
8222 }
8223
8224 #ifdef SWM_DEBUG
8225 void
8226 leavenotify(xcb_leave_notify_event_t *e)
8227 {
8228 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): 0x%x "
8229 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8230 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8231 e->time, e->event, e->event_x, e->event_y,
8232 get_notify_mode_label(e->mode), e->mode,
8233 get_notify_detail_label(e->detail), e->detail,
8234 e->root, e->root_x, e->root_y, e->child,
8235 YESNO(e->same_screen_focus), e->state);
8236 }
8237 #endif
8238
8239 void
8240 mapnotify(xcb_map_notify_event_t *e)
8241 {
8242 struct ws_win *win;
8243
8244 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
8245
8246 if ((win = find_window(e->window)) == NULL)
8247 win = manage_window(e->window, 1);
8248
8249 win->mapped = 1;
8250 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
8251
8252 if (focus_mode != SWM_FOCUS_FOLLOW) {
8253 if (win->ws->focus_pending == win) {
8254 focus_win(win);
8255 win->ws->focus_pending = NULL;
8256 focus_flush();
8257 }
8258 }
8259
8260 xcb_flush(conn);
8261 }
8262
8263 void
8264 mappingnotify(xcb_mapping_notify_event_t *e)
8265 {
8266 xcb_refresh_keyboard_mapping(syms, e);
8267
8268 if (e->request == XCB_MAPPING_KEYBOARD)
8269 grabkeys();
8270 }
8271
8272 void
8273 maprequest(xcb_map_request_event_t *e)
8274 {
8275 struct ws_win *win;
8276 xcb_get_window_attributes_reply_t *war;
8277
8278 DNPRINTF(SWM_D_EVENT, "maprequest: win 0x%x\n",
8279 e->window);
8280
8281 war = xcb_get_window_attributes_reply(conn,
8282 xcb_get_window_attributes(conn, e->window),
8283 NULL);
8284 if (war == NULL) {
8285 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
8286 goto out;
8287 }
8288
8289 if (war->override_redirect) {
8290 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
8291 "skipping.\n");
8292 goto out;
8293 }
8294
8295 win = manage_window(e->window,
8296 (war->map_state == XCB_MAP_STATE_VIEWABLE));
8297
8298 /* The new window should get focus; prepare. */
8299 if (focus_mode != SWM_FOCUS_FOLLOW)
8300 win->ws->focus_pending = get_focus_magic(win);
8301
8302 /* All windows need to be mapped if they are in the current workspace.*/
8303 if (win->ws->r)
8304 stack();
8305
8306 /* Ignore EnterNotify to handle the mapnotify without interference. */
8307 if (focus_mode == SWM_FOCUS_DEFAULT)
8308 event_drain(XCB_ENTER_NOTIFY);
8309 out:
8310 free(war);
8311 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
8312 }
8313
8314 void
8315 motionnotify(xcb_motion_notify_event_t *e)
8316 {
8317 struct swm_region *r;
8318 int i, num_screens;
8319
8320 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): 0x%x "
8321 "(%d,%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8322 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8323 e->time, e->event, e->event_x, e->event_y,
8324 get_notify_detail_label(e->detail), e->detail,
8325 e->root, e->root_x, e->root_y, e->child,
8326 YESNO(e->same_screen), e->state);
8327
8328 last_event_time = e->time;
8329
8330 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8331 for (i = 0; i < num_screens; i++)
8332 if (screens[i].root == e->root)
8333 break;
8334
8335 TAILQ_FOREACH(r, &screens[i].rl, entry)
8336 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
8337 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
8338 break;
8339
8340 focus_region(r);
8341 }
8342
8343 #ifdef SWM_DEBUG
8344 char *
8345 get_atom_name(xcb_atom_t atom)
8346 {
8347 char *name = NULL;
8348 #if 0
8349 /*
8350 * This should be disabled during most debugging since
8351 * xcb_get_* causes an xcb_flush.
8352 */
8353 size_t len;
8354 xcb_get_atom_name_reply_t *r;
8355
8356 r = xcb_get_atom_name_reply(conn,
8357 xcb_get_atom_name(conn, atom),
8358 NULL);
8359 if (r) {
8360 len = xcb_get_atom_name_name_length(r);
8361 if (len > 0) {
8362 name = malloc(len + 1);
8363 if (name) {
8364 memcpy(name, xcb_get_atom_name_name(r), len);
8365 name[len] = '\0';
8366 }
8367 }
8368 free(r);
8369 }
8370 #else
8371 (void)atom;
8372 #endif
8373 return (name);
8374 }
8375 #endif
8376
8377 void
8378 propertynotify(xcb_property_notify_event_t *e)
8379 {
8380 struct ws_win *win;
8381 #ifdef SWM_DEBUG
8382 char *name;
8383
8384 name = get_atom_name(e->atom);
8385 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u), "
8386 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
8387 e->state);
8388 free(name);
8389 #endif
8390 win = find_window(e->window);
8391 if (win == NULL)
8392 return;
8393
8394 last_event_time = e->time;
8395
8396 if (e->atom == a_swm_iconic) {
8397 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8398 if (focus_mode != SWM_FOCUS_FOLLOW)
8399 win->ws->focus_pending = get_focus_prev(win);
8400
8401 unfocus_win(win);
8402 unmap_window(win);
8403
8404 if (win->ws->r) {
8405 stack();
8406 if (focus_mode != SWM_FOCUS_FOLLOW) {
8407 focus_win(win->ws->focus_pending);
8408 win->ws->focus_pending = NULL;
8409 }
8410 focus_flush();
8411 }
8412 } else if (e->state == XCB_PROPERTY_DELETE) {
8413 /* The window is no longer iconic, restack ws. */
8414 if (focus_mode != SWM_FOCUS_FOLLOW)
8415 win->ws->focus_pending = get_focus_magic(win);
8416
8417 stack();
8418
8419 /* Flush EnterNotify for mapnotify, if needed. */
8420 focus_flush();
8421 }
8422 } else if (e->atom == a_state) {
8423 /* State just changed, make sure it gets focused if mapped. */
8424 if (e->state == XCB_PROPERTY_NEW_VALUE) {
8425 if (focus_mode != SWM_FOCUS_FOLLOW) {
8426 if (win->mapped &&
8427 win->ws->focus_pending == win) {
8428 focus_win(win->ws->focus_pending);
8429 win->ws->focus_pending = NULL;
8430 }
8431 }
8432 }
8433 } else if (e->atom == XCB_ATOM_WM_CLASS ||
8434 e->atom == XCB_ATOM_WM_NAME) {
8435 bar_draw();
8436 }
8437
8438 xcb_flush(conn);
8439 }
8440
8441 void
8442 unmapnotify(xcb_unmap_notify_event_t *e)
8443 {
8444 struct ws_win *win;
8445 struct workspace *ws;
8446
8447 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
8448
8449 /* If we aren't managing the window, then ignore. */
8450 win = find_window(e->window);
8451 if (win == NULL || win->id != e->window)
8452 return;
8453
8454 ws = win->ws;
8455
8456 if (getstate(e->window) != XCB_ICCCM_WM_STATE_ICONIC)
8457 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8458
8459 if (win->mapped) {
8460 /* window unmapped itself */
8461 /* do unmap/unfocus/restack and unmanage */
8462 win->mapped = 0;
8463
8464 /* If win was focused, make sure to focus on something else. */
8465 if (win == ws->focus) {
8466 if (focus_mode != SWM_FOCUS_FOLLOW) {
8467 ws->focus_pending = get_focus_prev(win);
8468 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
8469 "focus_pending: 0x%x\n",
8470 WINID(ws->focus_pending));
8471 }
8472
8473 unfocus_win(win);
8474 }
8475
8476 unmanage_window(win);
8477
8478 if (ws->r)
8479 stack();
8480
8481 if (focus_mode == SWM_FOCUS_FOLLOW) {
8482 if (ws->r)
8483 focus_win(get_pointer_win(ws->r->s->root));
8484 } else {
8485 if (ws->focus_pending) {
8486 focus_win(ws->focus_pending);
8487 ws->focus_pending = NULL;
8488 }
8489 }
8490 }
8491
8492 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL)
8493 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
8494
8495 focus_flush();
8496 }
8497
8498 #if 0
8499 void
8500 visibilitynotify(xcb_visibility_notify_event_t *e)
8501 {
8502 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
8503 e->window);
8504 }
8505 #endif
8506
8507 void
8508 clientmessage(xcb_client_message_event_t *e)
8509 {
8510 struct ws_win *win;
8511 xcb_map_request_event_t mre;
8512 #ifdef SWM_DEBUG
8513 char *name;
8514
8515 name = get_atom_name(e->type);
8516 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
8517 e->window, name, e->type);
8518 free(name);
8519 #endif
8520 win = find_window(e->window);
8521
8522 if (win == NULL) {
8523 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8524 /* Manage the window with maprequest. */
8525 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
8526 "unmanaged window.\n");
8527 mre.window = e->window;
8528 maprequest(&mre);
8529 }
8530 return;
8531 }
8532
8533 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
8534 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
8535 focus_win(win);
8536 }
8537 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
8538 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
8539 if (win->can_delete)
8540 client_msg(win, a_delete, 0);
8541 else
8542 xcb_kill_client(conn, win->id);
8543 }
8544 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
8545 DNPRINTF(SWM_D_EVENT,
8546 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
8547 if (win->floating) {
8548 if (e->data.data32[0] & (1<<8)) /* x */
8549 X(win) = e->data.data32[1];
8550 if (e->data.data32[0] & (1<<9)) /* y */
8551 Y(win) = e->data.data32[2];
8552 if (e->data.data32[0] & (1<<10)) /* width */
8553 WIDTH(win) = e->data.data32[3];
8554 if (e->data.data32[0] & (1<<11)) /* height */
8555 HEIGHT(win) = e->data.data32[4];
8556
8557 update_window(win);
8558 }
8559 else {
8560 /* TODO: Change stack sizes */
8561 /* notify no change was made. */
8562 config_win(win, NULL);
8563 }
8564 }
8565 if (e->type == ewmh[_NET_WM_STATE].atom) {
8566 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
8567 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
8568 if (e->data.data32[2])
8569 ewmh_update_win_state(win, e->data.data32[2],
8570 e->data.data32[0]);
8571
8572 stack();
8573 }
8574
8575 focus_flush();
8576 }
8577
8578 void
8579 check_conn(void)
8580 {
8581 int errcode = xcb_connection_has_error(conn);
8582 #ifdef XCB_CONN_ERROR
8583 char *s;
8584 switch (errcode) {
8585 case XCB_CONN_ERROR:
8586 s = "Socket error, pipe error or other stream error.";
8587 break;
8588 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
8589 s = "Extension not supported.";
8590 break;
8591 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
8592 s = "Insufficient memory.";
8593 break;
8594 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
8595 s = "Request length was exceeded.";
8596 break;
8597 case XCB_CONN_CLOSED_PARSE_ERR:
8598 s = "Error parsing display string.";
8599 break;
8600 default:
8601 s = "Unknown error.";
8602 }
8603 if (errcode)
8604 errx(errcode, "X CONNECTION ERROR: %s", s);
8605 #else
8606 if (errcode)
8607 errx(errcode, "X CONNECTION ERROR");
8608 #endif
8609 }
8610
8611 int
8612 enable_wm(void)
8613 {
8614 int num_screens, i;
8615 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
8616 XCB_EVENT_MASK_ENTER_WINDOW;
8617 xcb_screen_t *sc;
8618 xcb_void_cookie_t wac;
8619 xcb_generic_error_t *error;
8620
8621 /* this causes an error if some other window manager is running */
8622 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8623 for (i = 0; i < num_screens; i++) {
8624 if ((sc = get_screen(i)) == NULL)
8625 errx(1, "ERROR: can't get screen %d.", i);
8626 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
8627 i, sc->root);
8628 wac = xcb_change_window_attributes_checked(conn, sc->root,
8629 XCB_CW_EVENT_MASK, &val);
8630 if ((error = xcb_request_check(conn, wac))) {
8631 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
8632 error->error_code);
8633 free(error);
8634 return 1;
8635 }
8636
8637 /* click to focus on empty region */
8638 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
8639 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
8640 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
8641 }
8642
8643 return 0;
8644 }
8645
8646 void
8647 new_region(struct swm_screen *s, int x, int y, int w, int h)
8648 {
8649 struct swm_region *r, *n;
8650 struct workspace *ws = NULL;
8651 int i;
8652 uint32_t wa[1];
8653
8654 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
8655 s->idx, w, h, x, y);
8656
8657 /* remove any conflicting regions */
8658 n = TAILQ_FIRST(&s->rl);
8659 while (n) {
8660 r = n;
8661 n = TAILQ_NEXT(r, entry);
8662 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
8663 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
8664 if (r->ws->r != NULL)
8665 r->ws->old_r = r->ws->r;
8666 r->ws->r = NULL;
8667 bar_cleanup(r);
8668 xcb_destroy_window(conn, r->id);
8669 TAILQ_REMOVE(&s->rl, r, entry);
8670 TAILQ_INSERT_TAIL(&s->orl, r, entry);
8671 }
8672 }
8673
8674 /* search old regions for one to reuse */
8675
8676 /* size + location match */
8677 TAILQ_FOREACH(r, &s->orl, entry)
8678 if (X(r) == x && Y(r) == y &&
8679 HEIGHT(r) == h && WIDTH(r) == w)
8680 break;
8681
8682 /* size match */
8683 TAILQ_FOREACH(r, &s->orl, entry)
8684 if (HEIGHT(r) == h && WIDTH(r) == w)
8685 break;
8686
8687 if (r != NULL) {
8688 TAILQ_REMOVE(&s->orl, r, entry);
8689 /* try to use old region's workspace */
8690 if (r->ws->r == NULL)
8691 ws = r->ws;
8692 } else
8693 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
8694 err(1, "new_region: calloc: failed to allocate memory "
8695 "for screen");
8696
8697 /* if we don't have a workspace already, find one */
8698 if (ws == NULL) {
8699 for (i = 0; i < workspace_limit; i++)
8700 if (s->ws[i].r == NULL) {
8701 ws = &s->ws[i];
8702 break;
8703 }
8704 }
8705
8706 if (ws == NULL)
8707 errx(1, "new_region: no free workspaces");
8708
8709 X(r) = x;
8710 Y(r) = y;
8711 WIDTH(r) = w;
8712 HEIGHT(r) = h;
8713 r->s = s;
8714 r->ws = ws;
8715 r->ws_prior = NULL;
8716 ws->r = r;
8717 outputs++;
8718 TAILQ_INSERT_TAIL(&s->rl, r, entry);
8719
8720 /* Invisible region window to detect pointer events on empty regions. */
8721 r->id = xcb_generate_id(conn);
8722 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
8723 XCB_EVENT_MASK_POINTER_MOTION_HINT;
8724
8725 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
8726 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
8727 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
8728
8729 xcb_map_window(conn, r->id);
8730 }
8731
8732 void
8733 scan_xrandr(int i)
8734 {
8735 #ifdef SWM_XRR_HAS_CRTC
8736 int c;
8737 int ncrtc = 0;
8738 #endif /* SWM_XRR_HAS_CRTC */
8739 struct swm_region *r;
8740 int num_screens;
8741 xcb_randr_get_screen_resources_current_cookie_t src;
8742 xcb_randr_get_screen_resources_current_reply_t *srr;
8743 xcb_randr_get_crtc_info_cookie_t cic;
8744 xcb_randr_get_crtc_info_reply_t *cir = NULL;
8745 xcb_randr_crtc_t *crtc;
8746 xcb_screen_t *screen;
8747
8748 DNPRINTF(SWM_D_MISC, "scan_xrandr: screen: %d\n", i);
8749
8750 if ((screen = get_screen(i)) == NULL)
8751 errx(1, "ERROR: can't get screen %d.", i);
8752
8753 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8754 if (i >= num_screens)
8755 errx(1, "scan_xrandr: invalid screen");
8756
8757 /* remove any old regions */
8758 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
8759 r->ws->old_r = r->ws->r = NULL;
8760 bar_cleanup(r);
8761 xcb_destroy_window(conn, r->id);
8762 TAILQ_REMOVE(&screens[i].rl, r, entry);
8763 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
8764
8765 if (r->s->r_focus == r)
8766 r->s->r_focus = NULL;
8767 }
8768 outputs = 0;
8769
8770 /* map virtual screens onto physical screens */
8771 #ifdef SWM_XRR_HAS_CRTC
8772 if (xrandr_support) {
8773 src = xcb_randr_get_screen_resources_current(conn,
8774 screens[i].root);
8775 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
8776 NULL);
8777 if (srr == NULL) {
8778 new_region(&screens[i], 0, 0,
8779 screen->width_in_pixels,
8780 screen->height_in_pixels);
8781 goto out;
8782 } else
8783 ncrtc = srr->num_crtcs;
8784
8785 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
8786 for (c = 0; c < ncrtc; c++) {
8787 cic = xcb_randr_get_crtc_info(conn, crtc[c],
8788 XCB_CURRENT_TIME);
8789 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
8790 if (cir == NULL)
8791 continue;
8792 if (cir->num_outputs == 0) {
8793 free(cir);
8794 continue;
8795 }
8796
8797 if (cir->mode == 0)
8798 new_region(&screens[i], 0, 0,
8799 screen->width_in_pixels,
8800 screen->height_in_pixels);
8801 else
8802 new_region(&screens[i],
8803 cir->x, cir->y, cir->width, cir->height);
8804 free(cir);
8805 }
8806 free(srr);
8807 }
8808 #endif /* SWM_XRR_HAS_CRTC */
8809
8810 /* If detection failed, create a single region that spans the screen. */
8811 if (TAILQ_EMPTY(&screens[i].rl))
8812 new_region(&screens[i], 0, 0, screen->width_in_pixels,
8813 screen->height_in_pixels);
8814
8815 out:
8816 DNPRINTF(SWM_D_MISC, "scan_xrandr: done.\n");
8817 }
8818
8819 void
8820 screenchange(xcb_randr_screen_change_notify_event_t *e)
8821 {
8822 struct swm_region *r;
8823 int i, num_screens;
8824
8825 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
8826
8827 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8828 /* silly event doesn't include the screen index */
8829 for (i = 0; i < num_screens; i++)
8830 if (screens[i].root == e->root)
8831 break;
8832 if (i >= num_screens)
8833 errx(1, "screenchange: screen not found");
8834
8835 /* brute force for now, just re-enumerate the regions */
8836 scan_xrandr(i);
8837
8838 #ifdef SWM_DEBUG
8839 print_win_geom(e->root);
8840 #endif
8841 /* add bars to all regions */
8842 for (i = 0; i < num_screens; i++) {
8843 TAILQ_FOREACH(r, &screens[i].rl, entry)
8844 bar_setup(r);
8845
8846 if (screens[0].r_focus == NULL) {
8847 /* Focus on first region. */
8848 r = TAILQ_FIRST(&screens[0].rl);
8849 if (r)
8850 focus_region(r);
8851 }
8852 }
8853
8854 stack();
8855 bar_draw();
8856 focus_flush();
8857 }
8858
8859 void
8860 grab_windows(void)
8861 {
8862 struct swm_region *r = NULL;
8863 xcb_window_t *wins = NULL, trans;
8864 int no;
8865 int i, j, num_screens;
8866 uint16_t state, manage, mapped;
8867
8868 xcb_query_tree_cookie_t qtc;
8869 xcb_query_tree_reply_t *qtr;
8870 xcb_get_window_attributes_cookie_t gac;
8871 xcb_get_window_attributes_reply_t *gar;
8872 xcb_get_property_cookie_t pc;
8873
8874 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
8875 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8876 for (i = 0; i < num_screens; i++) {
8877 qtc = xcb_query_tree(conn, screens[i].root);
8878 qtr = xcb_query_tree_reply(conn, qtc, NULL);
8879 if (!qtr)
8880 continue;
8881 wins = xcb_query_tree_children(qtr);
8882 no = xcb_query_tree_children_length(qtr);
8883 /* attach windows to a region */
8884 /* normal windows */
8885 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
8886 for (j = 0; j < no; j++) {
8887 TAILQ_FOREACH(r, &screens[i].rl, entry) {
8888 if (r->id == wins[j]) {
8889 DNPRINTF(SWM_D_INIT, "grab_windows: "
8890 "skip %#x; region input window.\n",
8891 wins[j]);
8892 break;
8893 }
8894 }
8895
8896 if (r)
8897 continue;
8898
8899 gac = xcb_get_window_attributes(conn, wins[j]);
8900 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
8901 if (!gar) {
8902 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8903 "doesn't exist.\n", wins[j]);
8904 continue;
8905 }
8906
8907 if (gar->override_redirect) {
8908 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8909 "override_redirect set.\n", wins[j]);
8910 free(gar);
8911 continue;
8912 }
8913
8914 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
8915 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
8916 &trans, NULL)) {
8917 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8918 "is transient for %#x.\n", wins[j], trans);
8919 free(gar);
8920 continue;
8921 }
8922
8923 state = getstate(wins[j]);
8924 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
8925 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
8926 if (mapped || manage)
8927 manage_window(wins[j], mapped);
8928 free(gar);
8929 }
8930 /* transient windows */
8931 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
8932 for (j = 0; j < no; j++) {
8933 gac = xcb_get_window_attributes(conn, wins[j]);
8934 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
8935 if (!gar) {
8936 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8937 "doesn't exist.\n", wins[j]);
8938 continue;
8939 }
8940
8941 if (gar->override_redirect) {
8942 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
8943 "override_redirect set.\n", wins[j]);
8944 free(gar);
8945 continue;
8946 }
8947
8948 state = getstate(wins[j]);
8949 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
8950 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
8951 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
8952 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
8953 &trans, NULL) && manage)
8954 manage_window(wins[j], mapped);
8955 free(gar);
8956 }
8957 free(qtr);
8958 }
8959 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
8960 }
8961
8962 void
8963 setup_screens(void)
8964 {
8965 int i, j, k, num_screens;
8966 struct workspace *ws;
8967 uint32_t gcv[1], wa[1];
8968 const xcb_query_extension_reply_t *qep;
8969 xcb_screen_t *screen;
8970 xcb_randr_query_version_cookie_t c;
8971 xcb_randr_query_version_reply_t *r;
8972
8973 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8974 if ((screens = calloc(num_screens,
8975 sizeof(struct swm_screen))) == NULL)
8976 err(1, "setup_screens: calloc: failed to allocate memory for "
8977 "screens");
8978
8979 /* initial Xrandr setup */
8980 xrandr_support = 0;
8981 qep = xcb_get_extension_data(conn, &xcb_randr_id);
8982 if (qep->present) {
8983 c = xcb_randr_query_version(conn, 1, 1);
8984 r = xcb_randr_query_version_reply(conn, c, NULL);
8985 if (r) {
8986 if (r->major_version >= 1) {
8987 xrandr_support = 1;
8988 xrandr_eventbase = qep->first_event;
8989 }
8990 free(r);
8991 }
8992 }
8993
8994 wa[0] = cursors[XC_LEFT_PTR].cid;
8995
8996 /* map physical screens */
8997 for (i = 0; i < num_screens; i++) {
8998 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
8999 screens[i].idx = i;
9000 screens[i].r_focus = NULL;
9001
9002 TAILQ_INIT(&screens[i].rl);
9003 TAILQ_INIT(&screens[i].orl);
9004 if ((screen = get_screen(i)) == NULL)
9005 errx(1, "ERROR: can't get screen %d.", i);
9006 screens[i].root = screen->root;
9007
9008 /* set default colors */
9009 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
9010 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
9011 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
9012 setscreencolor("rgb:00/40/40", i + 1,
9013 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
9014 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
9015 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
9016
9017 /* create graphics context on screen */
9018 screens[i].bar_gc = xcb_generate_id(conn);
9019 gcv[0] = 0;
9020 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
9021 XCB_GC_GRAPHICS_EXPOSURES, gcv);
9022
9023 /* set default cursor */
9024 xcb_change_window_attributes(conn, screens[i].root,
9025 XCB_CW_CURSOR, wa);
9026
9027 /* init all workspaces */
9028 /* XXX these should be dynamically allocated too */
9029 for (j = 0; j < SWM_WS_MAX; j++) {
9030 ws = &screens[i].ws[j];
9031 ws->idx = j;
9032 ws->name = NULL;
9033 ws->bar_enabled = 1;
9034 ws->focus = NULL;
9035 ws->focus_prev = NULL;
9036 ws->focus_pending = NULL;
9037 ws->r = NULL;
9038 ws->old_r = NULL;
9039 TAILQ_INIT(&ws->winlist);
9040 TAILQ_INIT(&ws->unmanagedlist);
9041
9042 for (k = 0; layouts[k].l_stack != NULL; k++)
9043 if (layouts[k].l_config != NULL)
9044 layouts[k].l_config(ws,
9045 SWM_ARG_ID_STACKINIT);
9046 ws->cur_layout = &layouts[0];
9047 ws->cur_layout->l_string(ws);
9048 }
9049
9050 scan_xrandr(i);
9051
9052 if (xrandr_support)
9053 xcb_randr_select_input(conn, screens[i].root,
9054 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
9055 }
9056 }
9057
9058 void
9059 setup_globals(void)
9060 {
9061 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
9062 err(1, "setup_globals: strdup: failed to allocate memory.");
9063
9064 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
9065 err(1, "setup_globals: strdup: failed to allocate memory.");
9066
9067 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
9068 errx(1, "unable to allocate key symbols");
9069
9070 a_state = get_atom_from_string("WM_STATE");
9071 a_prot = get_atom_from_string("WM_PROTOCOLS");
9072 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
9073 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
9074 a_wmname = get_atom_from_string("WM_NAME");
9075 a_netwmname = get_atom_from_string("_NET_WM_NAME");
9076 a_utf8_string = get_atom_from_string("UTF8_STRING");
9077 a_string = get_atom_from_string("STRING");
9078 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
9079 a_swm_ws = get_atom_from_string("_SWM_WS");
9080 }
9081
9082 void
9083 workaround(void)
9084 {
9085 int i, num_screens;
9086 xcb_atom_t netwmcheck;
9087 xcb_window_t root, win;
9088
9089 /* work around sun jdk bugs, code from wmname */
9090 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
9091
9092 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
9093 for (i = 0; i < num_screens; i++) {
9094 root = screens[i].root;
9095
9096 win = xcb_generate_id(conn);
9097 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
9098 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
9099 XCB_COPY_FROM_PARENT, 0, NULL);
9100
9101 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
9102 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9103 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9104 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9105 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9106 a_netwmname, a_utf8_string, 8, strlen("LG3D"), "LG3D");
9107 }
9108 }
9109
9110 void
9111 shutdown_cleanup(void)
9112 {
9113 int i, num_screens;
9114
9115 /* disable alarm because the following code may not be interrupted */
9116 alarm(0);
9117 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
9118 err(1, "can't disable alarm");
9119
9120 bar_extra_stop();
9121 unmap_all();
9122
9123 cursors_cleanup();
9124
9125 teardown_ewmh();
9126
9127 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
9128 for (i = 0; i < num_screens; ++i) {
9129 if (screens[i].bar_gc != 0)
9130 xcb_free_gc(conn, screens[i].bar_gc);
9131 if (!bar_font_legacy)
9132 XftColorFree(display, DefaultVisual(display, i),
9133 DefaultColormap(display, i), &bar_font_color);
9134 }
9135
9136 if (bar_font_legacy)
9137 XFreeFontSet(display, bar_fs);
9138 else {
9139 XftFontClose(display, bar_font);
9140 }
9141
9142 xcb_key_symbols_free(syms);
9143 xcb_flush(conn);
9144 xcb_disconnect(conn);
9145 }
9146
9147 void
9148 event_error(xcb_generic_error_t *e)
9149 {
9150 (void)e;
9151
9152 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
9153 "resource_id: %u, minor_code: %u\n",
9154 xcb_event_get_error_label(e->error_code), e->error_code,
9155 xcb_event_get_request_label(e->major_code), e->major_code,
9156 e->sequence, e->resource_id, e->minor_code);
9157 }
9158
9159 void
9160 event_handle(xcb_generic_event_t *evt)
9161 {
9162 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
9163
9164 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
9165 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
9166 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
9167
9168 switch (type) {
9169 #define EVENT(type, callback) case type: callback((void *)evt); return
9170 EVENT(0, event_error);
9171 EVENT(XCB_BUTTON_PRESS, buttonpress);
9172 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
9173 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
9174 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
9175 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
9176 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
9177 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
9178 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
9179 /*EVENT(XCB_CREATE_NOTIFY, );*/
9180 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
9181 EVENT(XCB_ENTER_NOTIFY, enternotify);
9182 EVENT(XCB_EXPOSE, expose);
9183 #ifdef SWM_DEBUG
9184 EVENT(XCB_FOCUS_IN, focusin);
9185 EVENT(XCB_FOCUS_OUT, focusout);
9186 #endif
9187 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
9188 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
9189 EVENT(XCB_KEY_PRESS, keypress);
9190 /*EVENT(XCB_KEY_RELEASE, keypress);*/
9191 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
9192 #ifdef SWM_DEBUG
9193 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
9194 #endif
9195 EVENT(XCB_MAP_NOTIFY, mapnotify);
9196 EVENT(XCB_MAP_REQUEST, maprequest);
9197 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
9198 EVENT(XCB_MOTION_NOTIFY, motionnotify);
9199 /*EVENT(XCB_NO_EXPOSURE, );*/
9200 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
9201 /*EVENT(XCB_REPARENT_NOTIFY, );*/
9202 /*EVENT(XCB_RESIZE_REQUEST, );*/
9203 /*EVENT(XCB_SELECTION_CLEAR, );*/
9204 /*EVENT(XCB_SELECTION_NOTIFY, );*/
9205 /*EVENT(XCB_SELECTION_REQUEST, );*/
9206 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
9207 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
9208 #undef EVENT
9209 }
9210 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
9211 screenchange((void *)evt);
9212 }
9213
9214 int
9215 main(int argc, char *argv[])
9216 {
9217 struct swm_region *r;
9218 char conf[PATH_MAX], *cfile = NULL;
9219 struct stat sb;
9220 int xfd, i, num_screens, startup = 1;
9221 struct sigaction sact;
9222 xcb_generic_event_t *evt;
9223 struct timeval tv;
9224 fd_set rd;
9225 int rd_max;
9226 int stdin_ready = 0;
9227 int num_readable;
9228
9229 /* suppress unused warning since var is needed */
9230 (void)argc;
9231
9232 time_started = time(NULL);
9233
9234 start_argv = argv;
9235 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
9236 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
9237 warnx("no locale support");
9238
9239 /* handle some signals */
9240 bzero(&sact, sizeof(sact));
9241 sigemptyset(&sact.sa_mask);
9242 sact.sa_flags = 0;
9243 sact.sa_handler = sighdlr;
9244 sigaction(SIGINT, &sact, NULL);
9245 sigaction(SIGQUIT, &sact, NULL);
9246 sigaction(SIGTERM, &sact, NULL);
9247 sigaction(SIGHUP, &sact, NULL);
9248
9249 sact.sa_handler = sighdlr;
9250 sact.sa_flags = SA_NOCLDSTOP;
9251 sigaction(SIGCHLD, &sact, NULL);
9252
9253 if (!(display = XOpenDisplay(0)))
9254 errx(1, "can not open display");
9255
9256 conn = XGetXCBConnection(display);
9257 if (xcb_connection_has_error(conn))
9258 errx(1, "can not get XCB connection");
9259
9260 XSetEventQueueOwner(display, XCBOwnsEventQueue);
9261
9262 xcb_prefetch_extension_data(conn, &xcb_randr_id);
9263 xfd = xcb_get_file_descriptor(conn);
9264
9265 /* look for local and global conf file */
9266 pwd = getpwuid(getuid());
9267 if (pwd == NULL)
9268 errx(1, "invalid user: %d", getuid());
9269
9270 xcb_grab_server(conn);
9271 xcb_aux_sync(conn);
9272
9273 /* flush all events */
9274 while ((evt = xcb_poll_for_event(conn))) {
9275 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
9276 event_handle(evt);
9277 free(evt);
9278 }
9279
9280 if (enable_wm() != 0)
9281 errx(1, "another window manager is currently running");
9282
9283 /* Load Xcursors and/or cursorfont glyph cursors. */
9284 cursors_load();
9285
9286 xcb_aux_sync(conn);
9287
9288 setup_globals();
9289 setup_screens();
9290 setup_keys();
9291 setup_quirks();
9292 setup_spawn();
9293
9294 /* load config */
9295 for (i = 0; ; i++) {
9296 conf[0] = '\0';
9297 switch (i) {
9298 case 0:
9299 /* ~ */
9300 snprintf(conf, sizeof conf, "%s/.%s",
9301 pwd->pw_dir, SWM_CONF_FILE);
9302 break;
9303 case 1:
9304 /* global */
9305 snprintf(conf, sizeof conf, "/etc/%s",
9306 SWM_CONF_FILE);
9307 break;
9308 case 2:
9309 /* ~ compat */
9310 snprintf(conf, sizeof conf, "%s/.%s",
9311 pwd->pw_dir, SWM_CONF_FILE_OLD);
9312 break;
9313 case 3:
9314 /* global compat */
9315 snprintf(conf, sizeof conf, "/etc/%s",
9316 SWM_CONF_FILE_OLD);
9317 break;
9318 default:
9319 goto noconfig;
9320 }
9321
9322 if (strlen(conf) && stat(conf, &sb) != -1)
9323 if (S_ISREG(sb.st_mode)) {
9324 cfile = conf;
9325 break;
9326 }
9327 }
9328 noconfig:
9329
9330 /* load conf (if any) */
9331 if (cfile)
9332 conf_load(cfile, SWM_CONF_DEFAULT);
9333
9334 setup_ewmh();
9335 /* set some values to work around bad programs */
9336 workaround();
9337 /* grab existing windows (before we build the bars) */
9338 grab_windows();
9339
9340 if (getenv("SWM_STARTED") == NULL)
9341 setenv("SWM_STARTED", "YES", 1);
9342
9343 /* setup all bars */
9344 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
9345 for (i = 0; i < num_screens; i++)
9346 TAILQ_FOREACH(r, &screens[i].rl, entry)
9347 bar_setup(r);
9348
9349 grabkeys();
9350 stack();
9351 bar_draw();
9352
9353 xcb_ungrab_server(conn);
9354 xcb_flush(conn);
9355
9356 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
9357
9358 while (running) {
9359 while ((evt = xcb_poll_for_event(conn))) {
9360 if (!running)
9361 goto done;
9362 event_handle(evt);
9363 free(evt);
9364 }
9365
9366 /* If just (re)started, set default focus if needed. */
9367 if (startup) {
9368 startup = 0;
9369
9370 if (focus_mode != SWM_FOCUS_FOLLOW) {
9371 r = TAILQ_FIRST(&screens[0].rl);
9372 if (r) {
9373 focus_region(r);
9374 focus_flush();
9375 }
9376 continue;
9377 }
9378 }
9379
9380 FD_ZERO(&rd);
9381
9382 if (bar_extra)
9383 FD_SET(STDIN_FILENO, &rd);
9384
9385 FD_SET(xfd, &rd);
9386 tv.tv_sec = 1;
9387 tv.tv_usec = 0;
9388 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
9389 if (num_readable == -1 && errno != EINTR) {
9390 DNPRINTF(SWM_D_MISC, "select failed");
9391 } else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd)) {
9392 stdin_ready = 1;
9393 }
9394
9395 if (restart_wm)
9396 restart(NULL, NULL);
9397
9398 if (search_resp)
9399 search_do_resp();
9400
9401 if (!running)
9402 goto done;
9403
9404 if (stdin_ready) {
9405 stdin_ready = 0;
9406 if (bar_extra_update()) {
9407 bar_draw();
9408 xcb_flush(conn);
9409 }
9410 }
9411 }
9412 done:
9413 shutdown_cleanup();
9414
9415 return (0);
9416 }