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