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