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