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