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