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