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