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