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