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