]> code.delx.au - spectrwm/blob - spectrwm.c
8f7bb663e94e4339c2db4abe787e1b292d0bfd18
[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;
2758 xcb_get_input_focus_reply_t *r;
2759
2760 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
2761
2762 if (win == NULL)
2763 goto out;
2764
2765 if (win->ws == NULL)
2766 goto out;
2767
2768 if (validate_ws(win->ws))
2769 goto out;
2770
2771 if (validate_win(win)) {
2772 kill_refs(win);
2773 goto out;
2774 }
2775
2776 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
2777 if (r) {
2778 cfw = find_window(r->focus);
2779 if (cfw != win)
2780 unfocus_win(cfw);
2781 free(r);
2782 }
2783
2784 if (win->ws->focus != win) {
2785 if (win->ws->focus && win->ws->focus != cfw)
2786 unfocus_win(win->ws->focus);
2787 win->ws->focus = win;
2788 }
2789
2790 if (cfw != win && win->ws->r != NULL) {
2791 /* Set input focus if no input hint, or indicated by hint. */
2792 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
2793 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
2794 win->hints.input))
2795 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2796 win->id, last_event_time);
2797
2798 /* Tell app it can adjust focus to a specific window. */
2799 if (win->take_focus) {
2800 /* java is special; always tell parent */
2801 if (win->transient && win->java)
2802 client_msg(find_window(win->transient),
2803 a_takefocus, last_event_time);
2804 else
2805 client_msg(win, a_takefocus, last_event_time);
2806 }
2807
2808 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
2809 &win->ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
2810
2811 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2812 win->ws->always_raise)
2813 map_window(win);
2814
2815 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
2816 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
2817 &win->id);
2818 }
2819
2820 out:
2821 bar_update();
2822
2823 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
2824 }
2825
2826 /* If a child window should have focus instead, return it. */
2827 struct ws_win *
2828 get_focus_magic(struct ws_win *win)
2829 {
2830 struct ws_win *parent = NULL;
2831 struct ws_win *child = NULL;
2832
2833 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: window: 0x%x\n", WINID(win));
2834 if (win == NULL)
2835 return win;
2836
2837 if (win->transient) {
2838 parent = find_window(win->transient);
2839
2840 /* If parent prefers focus elsewhere, then try to do so. */
2841 if (parent && (child = parent->focus_child)) {
2842 if (validate_win(child) == 0 && child->mapped)
2843 win = child;
2844 else
2845 parent->focus_child = NULL;
2846 }
2847 }
2848
2849 /* If this window prefers focus elsewhere, then try to do so. */
2850 if ((child = win->focus_child)) {
2851 if (validate_win(child) == 0 && child->mapped)
2852 win = child;
2853 else
2854 win->focus_child = NULL;
2855 }
2856
2857 return win;
2858 }
2859
2860 void
2861 event_drain(uint8_t rt)
2862 {
2863 xcb_generic_event_t *evt;
2864
2865 /* ensure all pending requests have been processed before filtering. */
2866 xcb_aux_sync(conn);
2867 while ((evt = xcb_poll_for_event(conn))) {
2868 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
2869 event_handle(evt);
2870
2871 free(evt);
2872 }
2873 }
2874
2875 void
2876 switchws(struct swm_region *r, union arg *args)
2877 {
2878 int wsid = args->id, unmap_old = 0;
2879 struct swm_region *this_r, *other_r;
2880 struct ws_win *win;
2881 struct workspace *new_ws, *old_ws;
2882
2883 if (!(r && r->s))
2884 return;
2885
2886 if (wsid >= workspace_limit)
2887 return;
2888
2889 this_r = r;
2890 old_ws = this_r->ws;
2891 new_ws = &this_r->s->ws[wsid];
2892
2893 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2894 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2895
2896 if (new_ws == NULL || old_ws == NULL)
2897 return;
2898 if (new_ws == old_ws)
2899 return;
2900
2901 unfocus_win(old_ws->focus);
2902
2903 other_r = new_ws->r;
2904 if (other_r == NULL) {
2905 /* the other workspace is hidden, hide this one */
2906 old_ws->r = NULL;
2907 unmap_old = 1;
2908 } else {
2909 /* the other ws is visible in another region, exchange them */
2910 other_r->ws_prior = new_ws;
2911 other_r->ws = old_ws;
2912 old_ws->r = other_r;
2913 }
2914 this_r->ws_prior = old_ws;
2915 this_r->ws = new_ws;
2916 new_ws->r = this_r;
2917
2918 stack();
2919
2920 /* unmap old windows */
2921 if (unmap_old)
2922 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2923 unmap_window(win);
2924
2925 if (focus_mode != SWM_FOCUS_FOLLOW) {
2926 new_ws->focus_pending = get_region_focus(new_ws->r);
2927
2928 /* if workspaces were swapped, then don't wait to set focus */
2929 if (old_ws->r) {
2930 if (new_ws->focus_pending) {
2931 focus_win(new_ws->focus_pending);
2932 } else {
2933 /* Empty region, focus on root. */
2934 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
2935 new_ws->r->s[new_ws->r->s->idx].root,
2936 XCB_CURRENT_TIME);
2937 }
2938 }
2939 }
2940
2941 /* Clear bar if new ws is empty. */
2942 if (new_ws->focus_pending == NULL)
2943 bar_update();
2944
2945 focus_flush();
2946
2947 DNPRINTF(SWM_D_WS, "switchws: done.\n");
2948 }
2949
2950 void
2951 cyclews(struct swm_region *r, union arg *args)
2952 {
2953 union arg a;
2954 struct swm_screen *s = r->s;
2955 int cycle_all = 0;
2956
2957 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2958 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2959
2960 a.id = r->ws->idx;
2961 do {
2962 switch (args->id) {
2963 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2964 cycle_all = 1;
2965 /* FALLTHROUGH */
2966 case SWM_ARG_ID_CYCLEWS_UP:
2967 if (a.id < workspace_limit - 1)
2968 a.id++;
2969 else
2970 a.id = 0;
2971 break;
2972 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2973 cycle_all = 1;
2974 /* FALLTHROUGH */
2975 case SWM_ARG_ID_CYCLEWS_DOWN:
2976 if (a.id > 0)
2977 a.id--;
2978 else
2979 a.id = workspace_limit - 1;
2980 break;
2981 default:
2982 return;
2983 };
2984
2985 if (!cycle_all &&
2986 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2987 continue;
2988 if (!cycle_visible && s->ws[a.id].r != NULL)
2989 continue;
2990
2991 switchws(r, &a);
2992 } while (a.id != r->ws->idx);
2993 }
2994
2995 void
2996 priorws(struct swm_region *r, union arg *args)
2997 {
2998 union arg a;
2999
3000 (void)args;
3001
3002 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3003 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3004
3005 if (r->ws_prior == NULL)
3006 return;
3007
3008 a.id = r->ws_prior->idx;
3009 switchws(r, &a);
3010 }
3011
3012 void
3013 cyclescr(struct swm_region *r, union arg *args)
3014 {
3015 struct ws_win *nfw;
3016 struct swm_region *rr = NULL;
3017 int i, num_screens;
3018
3019 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3020 /* do nothing if we don't have more than one screen */
3021 if (!(num_screens > 1 || outputs > 1))
3022 return;
3023
3024 i = r->s->idx;
3025 switch (args->id) {
3026 case SWM_ARG_ID_CYCLESC_UP:
3027 rr = TAILQ_NEXT(r, entry);
3028 if (rr == NULL)
3029 rr = TAILQ_FIRST(&screens[i].rl);
3030 break;
3031 case SWM_ARG_ID_CYCLESC_DOWN:
3032 rr = TAILQ_PREV(r, swm_region_list, entry);
3033 if (rr == NULL)
3034 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3035 break;
3036 default:
3037 return;
3038 };
3039 if (rr == NULL)
3040 return;
3041
3042 nfw = get_region_focus(rr);
3043 if (nfw) {
3044 focus_win(nfw);
3045 } else {
3046 /* New region is empty; unfocus old region and warp pointer. */
3047 unfocus_win(r->ws->focus);
3048 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3049 rr->s[i].root, XCB_CURRENT_TIME);
3050
3051 /* Clear bar since empty. */
3052 bar_update();
3053 }
3054
3055 focus_flush();
3056 }
3057
3058 void
3059 sort_windows(struct ws_win_list *wl)
3060 {
3061 struct ws_win *win, *parent, *nxt;
3062
3063 if (wl == NULL)
3064 return;
3065
3066 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3067 nxt = TAILQ_NEXT(win, entry);
3068 if (win->transient) {
3069 parent = find_window(win->transient);
3070 if (parent == NULL) {
3071 warnx("not possible bug");
3072 continue;
3073 }
3074 TAILQ_REMOVE(wl, win, entry);
3075 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3076 }
3077 }
3078
3079 }
3080
3081 void
3082 swapwin(struct swm_region *r, union arg *args)
3083 {
3084 struct ws_win *target, *source;
3085 struct ws_win *cur_focus;
3086 struct ws_win_list *wl;
3087
3088 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3089 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3090
3091 cur_focus = r->ws->focus;
3092 if (cur_focus == NULL)
3093 return;
3094
3095 source = cur_focus;
3096 wl = &source->ws->winlist;
3097
3098 switch (args->id) {
3099 case SWM_ARG_ID_SWAPPREV:
3100 if (source->transient)
3101 source = find_window(source->transient);
3102 target = TAILQ_PREV(source, ws_win_list, entry);
3103 if (target && target->transient)
3104 target = find_window(target->transient);
3105 TAILQ_REMOVE(wl, source, entry);
3106 if (target == NULL)
3107 TAILQ_INSERT_TAIL(wl, source, entry);
3108 else
3109 TAILQ_INSERT_BEFORE(target, source, entry);
3110 break;
3111 case SWM_ARG_ID_SWAPNEXT:
3112 target = TAILQ_NEXT(source, entry);
3113 /* move the parent and let the sort handle the move */
3114 if (source->transient)
3115 source = find_window(source->transient);
3116 TAILQ_REMOVE(wl, source, entry);
3117 if (target == NULL)
3118 TAILQ_INSERT_HEAD(wl, source, entry);
3119 else
3120 TAILQ_INSERT_AFTER(wl, target, source, entry);
3121 break;
3122 case SWM_ARG_ID_SWAPMAIN:
3123 target = TAILQ_FIRST(wl);
3124 if (target == source) {
3125 if (source->ws->focus_prev != NULL &&
3126 source->ws->focus_prev != target)
3127 source = source->ws->focus_prev;
3128 else
3129 return;
3130 }
3131 if (target == NULL || source == NULL)
3132 return;
3133 source->ws->focus_prev = target;
3134 TAILQ_REMOVE(wl, target, entry);
3135 TAILQ_INSERT_BEFORE(source, target, entry);
3136 TAILQ_REMOVE(wl, source, entry);
3137 TAILQ_INSERT_HEAD(wl, source, entry);
3138 break;
3139 case SWM_ARG_ID_MOVELAST:
3140 TAILQ_REMOVE(wl, source, entry);
3141 TAILQ_INSERT_TAIL(wl, source, entry);
3142 break;
3143 default:
3144 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
3145 return;
3146 }
3147
3148 sort_windows(wl);
3149
3150 stack();
3151
3152 focus_flush();
3153 }
3154
3155 struct ws_win *
3156 get_focus_prev(struct ws_win *win)
3157 {
3158 struct ws_win *winfocus = NULL;
3159 struct ws_win *cur_focus = NULL;
3160 struct ws_win_list *wl = NULL;
3161 struct workspace *ws = NULL;
3162
3163 if (!(win && win->ws))
3164 return NULL;
3165
3166 ws = win->ws;
3167 wl = &ws->winlist;
3168 cur_focus = ws->focus;
3169
3170 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: window: 0x%x, cur_focus: 0x%x\n",
3171 WINID(win), WINID(cur_focus));
3172
3173 /* pickle, just focus on whatever */
3174 if (cur_focus == NULL) {
3175 /* use prev_focus if valid */
3176 if (ws->focus_prev && ws->focus_prev != cur_focus &&
3177 find_window(WINID(ws->focus_prev)))
3178 winfocus = ws->focus_prev;
3179 goto done;
3180 }
3181
3182 /* if transient focus on parent */
3183 if (cur_focus->transient) {
3184 winfocus = find_window(cur_focus->transient);
3185 goto done;
3186 }
3187
3188 /* if in max_stack try harder */
3189 if ((win->quirks & SWM_Q_FOCUSPREV) ||
3190 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
3191 if (cur_focus != ws->focus_prev)
3192 winfocus = ws->focus_prev;
3193 else
3194 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3195 if (winfocus)
3196 goto done;
3197 }
3198
3199 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
3200
3201 if (winfocus == NULL || winfocus == win) {
3202 switch (focus_close) {
3203 case SWM_STACK_BOTTOM:
3204 winfocus = TAILQ_FIRST(wl);
3205 break;
3206 case SWM_STACK_TOP:
3207 winfocus = TAILQ_LAST(wl, ws_win_list);
3208 break;
3209 case SWM_STACK_ABOVE:
3210 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
3211 if (focus_close_wrap)
3212 winfocus = TAILQ_FIRST(wl);
3213 else
3214 winfocus = TAILQ_PREV(cur_focus,
3215 ws_win_list, entry);
3216 }
3217 break;
3218 case SWM_STACK_BELOW:
3219 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
3220 entry)) == NULL) {
3221 if (focus_close_wrap)
3222 winfocus = TAILQ_LAST(wl, ws_win_list);
3223 else
3224 winfocus = TAILQ_NEXT(cur_focus, entry);
3225 }
3226 break;
3227 }
3228 }
3229 done:
3230 if (winfocus == NULL) {
3231 if (focus_default == SWM_STACK_TOP)
3232 winfocus = TAILQ_LAST(wl, ws_win_list);
3233 else
3234 winfocus = TAILQ_FIRST(wl);
3235 }
3236
3237 kill_refs(win);
3238
3239 return get_focus_magic(winfocus);
3240 }
3241
3242 struct ws_win *
3243 get_region_focus(struct swm_region *r)
3244 {
3245 struct ws_win *winfocus = NULL;
3246
3247 if (!(r && r->ws))
3248 return NULL;
3249
3250 if (r->ws->focus && !r->ws->focus->iconic)
3251 winfocus = r->ws->focus;
3252 else if (r->ws->focus_prev && !r->ws->focus_prev->iconic)
3253 winfocus = r->ws->focus_prev;
3254 else
3255 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
3256 if (!winfocus->iconic)
3257 break;
3258
3259 return get_focus_magic(winfocus);
3260 }
3261
3262 void
3263 focus(struct swm_region *r, union arg *args)
3264 {
3265 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
3266 struct ws_win_list *wl = NULL;
3267 struct workspace *ws = NULL;
3268 int all_iconics;
3269
3270 if (!(r && r->ws))
3271 return;
3272
3273 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
3274
3275 if ((cur_focus = r->ws->focus) == NULL)
3276 return;
3277 ws = r->ws;
3278 wl = &ws->winlist;
3279 if (TAILQ_EMPTY(wl))
3280 return;
3281 /* make sure there is at least one uniconified window */
3282 all_iconics = 1;
3283 TAILQ_FOREACH(winfocus, wl, entry)
3284 if (!winfocus->iconic) {
3285 all_iconics = 0;
3286 break;
3287 }
3288 if (all_iconics)
3289 return;
3290
3291 switch (args->id) {
3292 case SWM_ARG_ID_FOCUSPREV:
3293 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
3294 if (head == NULL)
3295 head = TAILQ_LAST(wl, ws_win_list);
3296 winfocus = head;
3297 if (WINID(winfocus) == cur_focus->transient) {
3298 head = TAILQ_PREV(winfocus, ws_win_list, entry);
3299 if (head == NULL)
3300 head = TAILQ_LAST(wl, ws_win_list);
3301 winfocus = head;
3302 }
3303
3304 /* skip iconics */
3305 if (winfocus && winfocus->iconic) {
3306 while (winfocus != cur_focus) {
3307 if (winfocus == NULL)
3308 winfocus = TAILQ_LAST(wl, ws_win_list);
3309 if (!winfocus->iconic)
3310 break;
3311 winfocus = TAILQ_PREV(winfocus, ws_win_list,
3312 entry);
3313 }
3314 }
3315 break;
3316
3317 case SWM_ARG_ID_FOCUSNEXT:
3318 head = TAILQ_NEXT(cur_focus, entry);
3319 if (head == NULL)
3320 head = TAILQ_FIRST(wl);
3321 winfocus = head;
3322
3323 /* skip iconics */
3324 if (winfocus && winfocus->iconic) {
3325 while (winfocus != cur_focus) {
3326 if (winfocus == NULL)
3327 winfocus = TAILQ_FIRST(wl);
3328 if (!winfocus->iconic)
3329 break;
3330 winfocus = TAILQ_NEXT(winfocus, entry);
3331 }
3332 }
3333 break;
3334
3335 case SWM_ARG_ID_FOCUSMAIN:
3336 winfocus = TAILQ_FIRST(wl);
3337 if (winfocus == cur_focus)
3338 winfocus = cur_focus->ws->focus_prev;
3339 break;
3340
3341 default:
3342 return;
3343 }
3344
3345 focus_win(get_focus_magic(winfocus));
3346
3347 xcb_flush(conn);
3348 }
3349
3350 void
3351 cycle_layout(struct swm_region *r, union arg *args)
3352 {
3353 struct workspace *ws = r->ws;
3354
3355 /* suppress unused warning since var is needed */
3356 (void)args;
3357
3358 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
3359
3360 ws->cur_layout++;
3361 if (ws->cur_layout->l_stack == NULL)
3362 ws->cur_layout = &layouts[0];
3363
3364 stack();
3365 bar_update();
3366
3367 focus_win(get_region_focus(r));
3368
3369 focus_flush();
3370 }
3371
3372 void
3373 stack_config(struct swm_region *r, union arg *args)
3374 {
3375 struct workspace *ws = r->ws;
3376
3377 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
3378 args->id, ws->idx);
3379
3380 if (ws->cur_layout->l_config != NULL)
3381 ws->cur_layout->l_config(ws, args->id);
3382
3383 if (args->id != SWM_ARG_ID_STACKINIT)
3384 stack();
3385 bar_update();
3386
3387 focus_flush();
3388 }
3389
3390 void
3391 stack(void) {
3392 struct swm_geometry g;
3393 struct swm_region *r;
3394 int i, num_screens;
3395 #ifdef SWM_DEBUG
3396 int j;
3397 #endif
3398
3399 DNPRINTF(SWM_D_STACK, "stack: begin\n");
3400
3401 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3402 for (i = 0; i < num_screens; i++) {
3403 #ifdef SWM_DEBUG
3404 j = 0;
3405 #endif
3406 TAILQ_FOREACH(r, &screens[i].rl, entry) {
3407 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
3408 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
3409
3410 /* start with screen geometry, adjust for bar */
3411 g = r->g;
3412 g.w -= 2 * border_width;
3413 g.h -= 2 * border_width;
3414 if (bar_enabled) {
3415 if (!bar_at_bottom)
3416 g.y += bar_height;
3417 g.h -= bar_height;
3418 }
3419 r->ws->cur_layout->l_stack(r->ws, &g);
3420 r->ws->cur_layout->l_string(r->ws);
3421 /* save r so we can track region changes */
3422 r->ws->old_r = r;
3423 }
3424 }
3425 if (font_adjusted)
3426 font_adjusted--;
3427
3428 DNPRINTF(SWM_D_STACK, "stack: end\n");
3429 }
3430
3431 void
3432 store_float_geom(struct ws_win *win, struct swm_region *r)
3433 {
3434 if (win == NULL || r == NULL)
3435 return;
3436
3437 /* retain window geom and region geom */
3438 win->g_float = win->g;
3439 win->g_float.x -= X(r);
3440 win->g_float.y -= Y(r);
3441 win->g_floatvalid = 1;
3442 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
3443 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
3444 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
3445 win->g_float.w, win->g_float.h);
3446 }
3447
3448 void
3449 load_float_geom(struct ws_win *win, struct swm_region *r)
3450 {
3451 if (win == NULL || r == NULL)
3452 return;
3453
3454 if (win->g_floatvalid) {
3455 win->g = win->g_float;
3456 X(win) += X(r);
3457 Y(win) += Y(r);
3458 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g: (%d,%d)"
3459 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
3460 HEIGHT(win));
3461 } else {
3462 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g_float "
3463 "is not set.\n", win->id);
3464 }
3465 }
3466
3467 void
3468 stack_floater(struct ws_win *win, struct swm_region *r)
3469 {
3470 if (win == NULL)
3471 return;
3472
3473 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
3474
3475 /*
3476 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3477 * geom on ws switches or return from max mode
3478 */
3479 if (win->floatmaxed || (r != r->ws->old_r &&
3480 !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
3481 /* update geometry for the new region */
3482 load_float_geom(win, r);
3483 }
3484
3485 win->floatmaxed = 0;
3486
3487 /*
3488 * if set to fullscreen mode, configure window to maximum size.
3489 */
3490 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3491 if (!win->g_floatvalid)
3492 store_float_geom(win, win->ws->r);
3493
3494 win->g = r->g;
3495 }
3496
3497 /*
3498 * remove border on fullscreen floater when in fullscreen mode or when
3499 * the quirk is present.
3500 */
3501 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) ||
3502 ((win->quirks & SWM_Q_FULLSCREEN) &&
3503 (WIDTH(win) >= WIDTH(r)) && (HEIGHT(win) >= HEIGHT(r)))) {
3504 if (win->bordered) {
3505 win->bordered = 0;
3506 X(win) += border_width;
3507 Y(win) += border_width;
3508 }
3509 } else if (!win->bordered) {
3510 win->bordered = 1;
3511 X(win) -= border_width;
3512 Y(win) -= border_width;
3513 }
3514
3515 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3516 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3517 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3518 }
3519
3520 if (!win->manual && !(win->ewmh_flags & EWMH_F_FULLSCREEN) &&
3521 !(win->quirks & SWM_Q_ANYWHERE)) {
3522 /*
3523 * floaters and transients are auto-centred unless moved,
3524 * resized or ANYWHERE quirk is set.
3525 */
3526 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - BORDER(win);
3527 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - BORDER(win);
3528
3529 store_float_geom(win, r);
3530 }
3531
3532 /* keep window within region bounds */
3533 constrain_window(win, r, 0);
3534
3535 update_window(win);
3536 }
3537
3538 /*
3539 * Send keystrokes to terminal to decrease/increase the font size as the
3540 * window size changes.
3541 */
3542 void
3543 adjust_font(struct ws_win *win)
3544 {
3545 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3546 win->floating || win->transient)
3547 return;
3548
3549 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3550 WIDTH(win) / win->sh.width_inc < term_width &&
3551 win->font_steps < SWM_MAX_FONT_STEPS) {
3552 win->font_size_boundary[win->font_steps] =
3553 (win->sh.width_inc * term_width) + win->sh.base_width;
3554 win->font_steps++;
3555 font_adjusted++;
3556 win->last_inc = win->sh.width_inc;
3557 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
3558 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3559 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3560 win->font_steps--;
3561 font_adjusted++;
3562 win->last_inc = win->sh.width_inc;
3563 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
3564 }
3565 }
3566
3567 #define SWAPXY(g) do { \
3568 int tmp; \
3569 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3570 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3571 } while (0)
3572 void
3573 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3574 {
3575 struct swm_geometry win_g, r_g = *g;
3576 struct ws_win *win, *fs_win = NULL;
3577 int i, j, s, stacks;
3578 int w_inc = 1, h_inc, w_base = 1, h_base;
3579 int hrh, extra = 0, h_slice, last_h = 0;
3580 int split, colno, winno, mwin, msize, mscale;
3581 int remain, missing, v_slice, reconfigure = 0;
3582 int bordered = 1;
3583
3584 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3585 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3586
3587 winno = count_win(ws, 0);
3588 if (winno == 0 && count_win(ws, 1) == 0)
3589 return;
3590
3591 TAILQ_FOREACH(win, &ws->winlist, entry)
3592 if (!win->transient && !win->floating && !win->iconic)
3593 break;
3594
3595 if (win == NULL)
3596 goto notiles;
3597
3598 if (rot) {
3599 w_inc = win->sh.width_inc;
3600 w_base = win->sh.base_width;
3601 mwin = ws->l_state.horizontal_mwin;
3602 mscale = ws->l_state.horizontal_msize;
3603 stacks = ws->l_state.horizontal_stacks;
3604 SWAPXY(&r_g);
3605 } else {
3606 w_inc = win->sh.height_inc;
3607 w_base = win->sh.base_height;
3608 mwin = ws->l_state.vertical_mwin;
3609 mscale = ws->l_state.vertical_msize;
3610 stacks = ws->l_state.vertical_stacks;
3611 }
3612 win_g = r_g;
3613
3614 if (stacks > winno - mwin)
3615 stacks = winno - mwin;
3616 if (stacks < 1)
3617 stacks = 1;
3618
3619 h_slice = r_g.h / SWM_H_SLICE;
3620 if (mwin && winno > mwin) {
3621 v_slice = r_g.w / SWM_V_SLICE;
3622
3623 split = mwin;
3624 colno = split;
3625 win_g.w = v_slice * mscale;
3626
3627 if (w_inc > 1 && w_inc < v_slice) {
3628 /* adjust for window's requested size increment */
3629 remain = (win_g.w - w_base) % w_inc;
3630 win_g.w -= remain;
3631 }
3632
3633 msize = win_g.w;
3634 if (flip)
3635 win_g.x += r_g.w - msize;
3636 } else {
3637 msize = -2;
3638 colno = split = winno / stacks;
3639 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3640 2 * border_width) / stacks);
3641 }
3642 hrh = r_g.h / colno;
3643 extra = r_g.h - (colno * hrh);
3644 win_g.h = hrh - 2 * border_width;
3645
3646 /* stack all the tiled windows */
3647 i = j = 0, s = stacks;
3648 TAILQ_FOREACH(win, &ws->winlist, entry) {
3649 if (win->transient || win->floating)
3650 continue;
3651 if (win->iconic)
3652 continue;
3653
3654 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3655 fs_win = win;
3656 continue;
3657 }
3658
3659 if (split && i == split) {
3660 colno = (winno - mwin) / stacks;
3661 if (s <= (winno - mwin) % stacks)
3662 colno++;
3663 split = split + colno;
3664 hrh = (r_g.h / colno);
3665 extra = r_g.h - (colno * hrh);
3666 if (flip)
3667 win_g.x = r_g.x;
3668 else
3669 win_g.x += win_g.w + 2 * border_width;
3670 win_g.w = (r_g.w - msize -
3671 (stacks * 2 * border_width)) / stacks;
3672 if (s == 1)
3673 win_g.w += (r_g.w - msize -
3674 (stacks * 2 * border_width)) % stacks;
3675 s--;
3676 j = 0;
3677 }
3678 win_g.h = hrh - 2 * border_width;
3679 if (rot) {
3680 h_inc = win->sh.width_inc;
3681 h_base = win->sh.base_width;
3682 } else {
3683 h_inc = win->sh.height_inc;
3684 h_base = win->sh.base_height;
3685 }
3686 if (j == colno - 1) {
3687 win_g.h = hrh + extra;
3688 } else if (h_inc > 1 && h_inc < h_slice) {
3689 /* adjust for window's requested size increment */
3690 remain = (win_g.h - h_base) % h_inc;
3691 missing = h_inc - remain;
3692
3693 if (missing <= extra || j == 0) {
3694 extra -= missing;
3695 win_g.h += missing;
3696 } else {
3697 win_g.h -= remain;
3698 extra += remain;
3699 }
3700 }
3701
3702 if (j == 0)
3703 win_g.y = r_g.y;
3704 else
3705 win_g.y += last_h + 2 * border_width;
3706
3707 if (disable_border && !bar_enabled && winno == 1){
3708 bordered = 0;
3709 win_g.w += 2 * border_width;
3710 win_g.h += 2 * border_width;
3711 } else {
3712 bordered = 1;
3713 }
3714 if (rot) {
3715 if (X(win) != win_g.y || Y(win) != win_g.x ||
3716 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3717 reconfigure = 1;
3718 X(win) = win_g.y;
3719 Y(win) = win_g.x;
3720 WIDTH(win) = win_g.h;
3721 HEIGHT(win) = win_g.w;
3722 }
3723 } else {
3724 if (X(win) != win_g.x || Y(win) != win_g.y ||
3725 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3726 reconfigure = 1;
3727 X(win) = win_g.x;
3728 Y(win) = win_g.y;
3729 WIDTH(win) = win_g.w;
3730 HEIGHT(win) = win_g.h;
3731 }
3732 }
3733
3734 if (bordered != win->bordered) {
3735 reconfigure = 1;
3736 win->bordered = bordered;
3737 }
3738
3739 if (reconfigure) {
3740 adjust_font(win);
3741 update_window(win);
3742 }
3743
3744 map_window(win);
3745
3746 last_h = win_g.h;
3747 i++;
3748 j++;
3749 }
3750
3751 notiles:
3752 /* now, stack all the floaters and transients */
3753 TAILQ_FOREACH(win, &ws->winlist, entry) {
3754 if (!win->transient && !win->floating)
3755 continue;
3756 if (win->iconic)
3757 continue;
3758 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3759 fs_win = win;
3760 continue;
3761 }
3762
3763 stack_floater(win, ws->r);
3764 map_window(win);
3765 }
3766
3767 if (fs_win) {
3768 stack_floater(fs_win, ws->r);
3769 map_window(fs_win);
3770 }
3771 }
3772
3773 void
3774 vertical_config(struct workspace *ws, int id)
3775 {
3776 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3777 id, ws->idx);
3778
3779 switch (id) {
3780 case SWM_ARG_ID_STACKRESET:
3781 case SWM_ARG_ID_STACKINIT:
3782 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3783 ws->l_state.vertical_mwin = 1;
3784 ws->l_state.vertical_stacks = 1;
3785 break;
3786 case SWM_ARG_ID_MASTERSHRINK:
3787 if (ws->l_state.vertical_msize > 1)
3788 ws->l_state.vertical_msize--;
3789 break;
3790 case SWM_ARG_ID_MASTERGROW:
3791 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3792 ws->l_state.vertical_msize++;
3793 break;
3794 case SWM_ARG_ID_MASTERADD:
3795 ws->l_state.vertical_mwin++;
3796 break;
3797 case SWM_ARG_ID_MASTERDEL:
3798 if (ws->l_state.vertical_mwin > 0)
3799 ws->l_state.vertical_mwin--;
3800 break;
3801 case SWM_ARG_ID_STACKINC:
3802 ws->l_state.vertical_stacks++;
3803 break;
3804 case SWM_ARG_ID_STACKDEC:
3805 if (ws->l_state.vertical_stacks > 1)
3806 ws->l_state.vertical_stacks--;
3807 break;
3808 case SWM_ARG_ID_FLIPLAYOUT:
3809 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3810 break;
3811 default:
3812 return;
3813 }
3814 }
3815
3816 void
3817 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3818 {
3819 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3820
3821 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3822 }
3823
3824 void
3825 horizontal_config(struct workspace *ws, int id)
3826 {
3827 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3828
3829 switch (id) {
3830 case SWM_ARG_ID_STACKRESET:
3831 case SWM_ARG_ID_STACKINIT:
3832 ws->l_state.horizontal_mwin = 1;
3833 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3834 ws->l_state.horizontal_stacks = 1;
3835 break;
3836 case SWM_ARG_ID_MASTERSHRINK:
3837 if (ws->l_state.horizontal_msize > 1)
3838 ws->l_state.horizontal_msize--;
3839 break;
3840 case SWM_ARG_ID_MASTERGROW:
3841 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3842 ws->l_state.horizontal_msize++;
3843 break;
3844 case SWM_ARG_ID_MASTERADD:
3845 ws->l_state.horizontal_mwin++;
3846 break;
3847 case SWM_ARG_ID_MASTERDEL:
3848 if (ws->l_state.horizontal_mwin > 0)
3849 ws->l_state.horizontal_mwin--;
3850 break;
3851 case SWM_ARG_ID_STACKINC:
3852 ws->l_state.horizontal_stacks++;
3853 break;
3854 case SWM_ARG_ID_STACKDEC:
3855 if (ws->l_state.horizontal_stacks > 1)
3856 ws->l_state.horizontal_stacks--;
3857 break;
3858 case SWM_ARG_ID_FLIPLAYOUT:
3859 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3860 break;
3861 default:
3862 return;
3863 }
3864 }
3865
3866 void
3867 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3868 {
3869 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3870
3871 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3872 }
3873
3874 /* fullscreen view */
3875 void
3876 max_stack(struct workspace *ws, struct swm_geometry *g)
3877 {
3878 struct swm_geometry gg = *g;
3879 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3880 int winno, num_screens;
3881
3882 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3883
3884 if (ws == NULL)
3885 return;
3886
3887 winno = count_win(ws, 0);
3888 if (winno == 0 && count_win(ws, 1) == 0)
3889 return;
3890
3891 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
3892 TAILQ_FOREACH(win, &ws->winlist, entry) {
3893 if (win->transient) {
3894 wintrans = win;
3895 parent = find_window(win->transient);
3896 continue;
3897 }
3898
3899 if (win->floating && !win->floatmaxed ) {
3900 /*
3901 * retain geometry for retrieval on exit from
3902 * max_stack mode
3903 */
3904 store_float_geom(win, ws->r);
3905 win->floatmaxed = 1;
3906 }
3907
3908 /* only reconfigure if necessary */
3909 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3910 HEIGHT(win) != gg.h) {
3911 win->g = gg;
3912 if (bar_enabled){
3913 win->bordered = 1;
3914 } else {
3915 win->bordered = 0;
3916 WIDTH(win) += 2 * border_width;
3917 HEIGHT(win) += 2 * border_width;
3918 }
3919
3920 update_window(win);
3921 }
3922 /* unmap only if we don't have multi screen */
3923 if (win != ws->focus)
3924 if (!(num_screens > 1 || outputs > 1))
3925 unmap_window(win);
3926 }
3927
3928 /* put the last transient on top */
3929 if (wintrans) {
3930 if (parent)
3931 map_window(parent);
3932 stack_floater(wintrans, ws->r);
3933 ws->focus = get_focus_magic(wintrans);
3934 }
3935 }
3936
3937 void
3938 send_to_ws(struct swm_region *r, union arg *args)
3939 {
3940 int wsid = args->id;
3941 struct ws_win *win = NULL, *parent;
3942 struct workspace *ws, *nws, *pws;
3943 char ws_idx_str[SWM_PROPLEN];
3944
3945 if (wsid >= workspace_limit)
3946 return;
3947
3948 if (r && r->ws && r->ws->focus)
3949 win = r->ws->focus;
3950 else
3951 return;
3952
3953 if (win->ws->idx == wsid)
3954 return;
3955
3956 DNPRINTF(SWM_D_MOVE, "send_to_ws: win 0x%x, ws %d -> %d\n", win->id,
3957 win->ws->idx, wsid);
3958
3959 ws = win->ws;
3960 nws = &win->s->ws[wsid];
3961
3962 /* Update the window's workspace property: _SWM_WS */
3963 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
3964 if (focus_mode != SWM_FOCUS_FOLLOW)
3965 ws->focus_pending = get_focus_prev(win);
3966
3967 /* Move the parent if this is a transient window. */
3968 if (win->transient) {
3969 parent = find_window(win->transient);
3970 if (parent) {
3971 pws = parent->ws;
3972 /* Set new focus in parent's ws if needed. */
3973 if (pws->focus == parent) {
3974 if (focus_mode != SWM_FOCUS_FOLLOW)
3975 pws->focus_pending =
3976 get_focus_prev(parent);
3977
3978 unfocus_win(parent);
3979
3980 if (focus_mode != SWM_FOCUS_FOLLOW)
3981 pws->focus = pws->focus_pending;
3982
3983 if (focus_mode != SWM_FOCUS_FOLLOW)
3984 pws->focus_pending = NULL;
3985 }
3986
3987 /* Don't unmap parent if new ws is visible */
3988 if (nws->r == NULL)
3989 unmap_window(parent);
3990
3991 /* Transfer */
3992 TAILQ_REMOVE(&ws->winlist, parent, entry);
3993 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3994 parent->ws = nws;
3995
3996 DNPRINTF(SWM_D_PROP, "send_to_ws: set "
3997 "property: _SWM_WS: %s\n", ws_idx_str);
3998 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
3999 parent->id, a_swm_ws, XCB_ATOM_STRING, 8,
4000 strlen(ws_idx_str), ws_idx_str);
4001 }
4002 }
4003
4004 unfocus_win(win);
4005
4006 /* Don't unmap if new ws is visible */
4007 if (nws->r == NULL)
4008 unmap_window(win);
4009
4010 /* Transfer */
4011 TAILQ_REMOVE(&ws->winlist, win, entry);
4012 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
4013 win->ws = nws;
4014
4015 /* Set focus on new ws. */
4016 unfocus_win(nws->focus);
4017 nws->focus = win;
4018
4019 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
4020 ws_idx_str);
4021 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
4022 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
4023 ws_idx_str);
4024
4025 /* Restack and set new focus. */
4026 stack();
4027
4028 if (focus_mode != SWM_FOCUS_FOLLOW) {
4029 focus_win(ws->focus_pending);
4030 ws->focus_pending = NULL;
4031 }
4032
4033 focus_flush();
4034 }
4035
4036 DNPRINTF(SWM_D_MOVE, "send_to_ws: done.\n");
4037 }
4038
4039 void
4040 pressbutton(struct swm_region *r, union arg *args)
4041 {
4042 /* suppress unused warning since var is needed */
4043 (void)r;
4044
4045 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
4046 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4047 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
4048 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
4049 }
4050
4051 void
4052 raise_toggle(struct swm_region *r, union arg *args)
4053 {
4054 /* suppress unused warning since var is needed */
4055 (void)args;
4056
4057 if (r == NULL || r->ws == NULL)
4058 return;
4059
4060 r->ws->always_raise = !r->ws->always_raise;
4061
4062 /* bring floaters back to top */
4063 if (!r->ws->always_raise)
4064 stack();
4065
4066 focus_flush();
4067 }
4068
4069 void
4070 iconify(struct swm_region *r, union arg *args)
4071 {
4072 /* suppress unused warning since var is needed */
4073 (void)args;
4074
4075 if (r->ws->focus == NULL)
4076 return;
4077
4078 set_swm_iconic(r->ws->focus, 1);
4079
4080 xcb_flush(conn);
4081 }
4082
4083 char *
4084 get_win_name(xcb_window_t win)
4085 {
4086 char *name = NULL;
4087 xcb_get_property_cookie_t c;
4088 xcb_get_property_reply_t *r;
4089
4090 /* First try _NET_WM_NAME for UTF-8. */
4091 c = xcb_get_property(conn, 0, win, a_netwmname,
4092 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4093 r = xcb_get_property_reply(conn, c, NULL);
4094
4095 if (!r || r->type == XCB_NONE) {
4096 free(r);
4097 /* Use WM_NAME instead; no UTF-8. */
4098 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
4099 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
4100 r = xcb_get_property_reply(conn, c, NULL);
4101
4102 if(!r || r->type == XCB_NONE) {
4103 free(r);
4104 return NULL;
4105 }
4106 }
4107
4108 if (r->length > 0)
4109 name = strndup(xcb_get_property_value(r),
4110 xcb_get_property_value_length(r));
4111
4112 free(r);
4113 return name;
4114 }
4115
4116 void
4117 uniconify(struct swm_region *r, union arg *args)
4118 {
4119 struct ws_win *win;
4120 FILE *lfile;
4121 char *name;
4122 int count = 0;
4123
4124 DNPRINTF(SWM_D_MISC, "uniconify\n");
4125
4126 if (r == NULL || r->ws == NULL)
4127 return;
4128
4129 /* make sure we have anything to uniconify */
4130 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4131 if (win->ws == NULL)
4132 continue; /* should never happen */
4133 if (!win->iconic)
4134 continue;
4135 count++;
4136 }
4137 if (count == 0)
4138 return;
4139
4140 search_r = r;
4141 search_resp_action = SWM_SEARCH_UNICONIFY;
4142
4143 spawn_select(r, args, "search", &searchpid);
4144
4145 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4146 return;
4147
4148 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4149 if (win->ws == NULL)
4150 continue; /* should never happen */
4151 if (!win->iconic)
4152 continue;
4153
4154 name = get_win_name(win->id);
4155 if (name == NULL)
4156 continue;
4157 fprintf(lfile, "%s.%u\n", name, win->id);
4158 free(name);
4159 }
4160
4161 fclose(lfile);
4162 }
4163
4164 void
4165 name_workspace(struct swm_region *r, union arg *args)
4166 {
4167 FILE *lfile;
4168
4169 DNPRINTF(SWM_D_MISC, "name_workspace\n");
4170
4171 if (r == NULL)
4172 return;
4173
4174 search_r = r;
4175 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
4176
4177 spawn_select(r, args, "name_workspace", &searchpid);
4178
4179 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4180 return;
4181
4182 fprintf(lfile, "%s", "");
4183 fclose(lfile);
4184 }
4185
4186 void
4187 search_workspace(struct swm_region *r, union arg *args)
4188 {
4189 int i;
4190 struct workspace *ws;
4191 FILE *lfile;
4192
4193 DNPRINTF(SWM_D_MISC, "search_workspace\n");
4194
4195 if (r == NULL)
4196 return;
4197
4198 search_r = r;
4199 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
4200
4201 spawn_select(r, args, "search", &searchpid);
4202
4203 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4204 return;
4205
4206 for (i = 0; i < workspace_limit; i++) {
4207 ws = &r->s->ws[i];
4208 if (ws == NULL)
4209 continue;
4210 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
4211 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
4212 }
4213
4214 fclose(lfile);
4215 }
4216
4217 void
4218 search_win_cleanup(void)
4219 {
4220 struct search_window *sw = NULL;
4221
4222 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
4223 xcb_destroy_window(conn, sw->indicator);
4224 TAILQ_REMOVE(&search_wl, sw, entry);
4225 free(sw);
4226 }
4227 }
4228
4229 void
4230 search_win(struct swm_region *r, union arg *args)
4231 {
4232 struct ws_win *win = NULL;
4233 struct search_window *sw = NULL;
4234 xcb_window_t w;
4235 uint32_t wa[2];
4236 int i, width, height;
4237 char s[8];
4238 FILE *lfile;
4239 size_t len;
4240 XftDraw *draw;
4241 XGlyphInfo info;
4242 GC l_draw;
4243 XGCValues l_gcv;
4244 XRectangle l_ibox, l_lbox;
4245
4246 DNPRINTF(SWM_D_MISC, "search_win\n");
4247
4248 search_r = r;
4249 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
4250
4251 spawn_select(r, args, "search", &searchpid);
4252
4253 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
4254 return;
4255
4256 TAILQ_INIT(&search_wl);
4257
4258 i = 1;
4259 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
4260 if (win->iconic)
4261 continue;
4262
4263 sw = calloc(1, sizeof(struct search_window));
4264 if (sw == NULL) {
4265 warn("search_win: calloc");
4266 fclose(lfile);
4267 search_win_cleanup();
4268 return;
4269 }
4270 sw->idx = i;
4271 sw->win = win;
4272
4273 snprintf(s, sizeof s, "%d", i);
4274 len = strlen(s);
4275
4276 w = xcb_generate_id(conn);
4277 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
4278 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
4279
4280 if (bar_font_legacy) {
4281 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
4282 width = l_lbox.width + 4;
4283 height = bar_fs_extents->max_logical_extent.height + 4;
4284 } else {
4285 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
4286 &info);
4287 width = info.width + 4;
4288 height = bar_font->height + 4;
4289 }
4290
4291 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
4292 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
4293 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
4294 XCB_CW_BORDER_PIXEL, wa);
4295
4296 xcb_map_window(conn, w);
4297
4298 sw->indicator = w;
4299 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
4300
4301 if (bar_font_legacy) {
4302 l_gcv.graphics_exposures = 0;
4303 l_draw = XCreateGC(display, w, 0, &l_gcv);
4304
4305 XSetForeground(display, l_draw,
4306 r->s->c[SWM_S_COLOR_BAR].pixel);
4307
4308 DRAWSTRING(display, w, bar_fs, l_draw, 2,
4309 (bar_fs_extents->max_logical_extent.height -
4310 l_lbox.height) / 2 - l_lbox.y, s, len);
4311
4312 XFreeGC(display, l_draw);
4313 } else {
4314
4315 draw = XftDrawCreate(display, w,
4316 DefaultVisual(display, r->s->idx),
4317 DefaultColormap(display, r->s->idx));
4318
4319 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
4320 (HEIGHT(r->bar) + bar_font->height) / 2 -
4321 bar_font->descent, (FcChar8 *)s, len);
4322
4323 XftDrawDestroy(draw);
4324 }
4325
4326 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
4327
4328 fprintf(lfile, "%d\n", i);
4329 i++;
4330 }
4331
4332 fclose(lfile);
4333
4334 xcb_flush(conn);
4335 }
4336
4337 void
4338 search_resp_uniconify(char *resp, unsigned long len)
4339 {
4340 char *name;
4341 struct ws_win *win;
4342 char *s;
4343
4344 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
4345
4346 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
4347 if (!win->iconic)
4348 continue;
4349 name = get_win_name(win->id);
4350 if (name == NULL)
4351 continue;
4352 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
4353 free(name);
4354 continue;
4355 }
4356 free(name);
4357 if (strncmp(s, resp, len) == 0) {
4358 /* XXX this should be a callback to generalize */
4359 set_swm_iconic(win, 0);
4360 free(s);
4361 break;
4362 }
4363 free(s);
4364 }
4365 }
4366
4367 void
4368 search_resp_name_workspace(char *resp, unsigned long len)
4369 {
4370 struct workspace *ws;
4371
4372 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
4373
4374 if (search_r->ws == NULL)
4375 return;
4376 ws = search_r->ws;
4377
4378 if (ws->name) {
4379 free(search_r->ws->name);
4380 search_r->ws->name = NULL;
4381 }
4382
4383 if (len > 1) {
4384 ws->name = strdup(resp);
4385 if (ws->name == NULL) {
4386 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
4387 "strdup: %s", strerror(errno));
4388 return;
4389 }
4390 }
4391 }
4392
4393 void
4394 search_resp_search_workspace(char *resp)
4395 {
4396 char *p, *q;
4397 int ws_idx;
4398 const char *errstr;
4399 union arg a;
4400
4401 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
4402
4403 q = strdup(resp);
4404 if (!q) {
4405 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
4406 strerror(errno));
4407 return;
4408 }
4409 p = strchr(q, ':');
4410 if (p != NULL)
4411 *p = '\0';
4412 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
4413 if (errstr) {
4414 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
4415 errstr, q);
4416 free(q);
4417 return;
4418 }
4419 free(q);
4420 a.id = ws_idx - 1;
4421 switchws(search_r, &a);
4422 }
4423
4424 void
4425 search_resp_search_window(char *resp)
4426 {
4427 char *s;
4428 int idx;
4429 const char *errstr;
4430 struct search_window *sw;
4431
4432 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
4433
4434 s = strdup(resp);
4435 if (!s) {
4436 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
4437 strerror(errno));
4438 return;
4439 }
4440
4441 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
4442 if (errstr) {
4443 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
4444 errstr, s);
4445 free(s);
4446 return;
4447 }
4448 free(s);
4449
4450 TAILQ_FOREACH(sw, &search_wl, entry)
4451 if (idx == sw->idx) {
4452 focus_win(sw->win);
4453 break;
4454 }
4455 }
4456
4457 #define MAX_RESP_LEN 1024
4458
4459 void
4460 search_do_resp(void)
4461 {
4462 ssize_t rbytes;
4463 char *resp;
4464 unsigned long len;
4465
4466 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
4467
4468 search_resp = 0;
4469 searchpid = 0;
4470
4471 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
4472 warn("search: calloc");
4473 goto done;
4474 }
4475
4476 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
4477 if (rbytes <= 0) {
4478 warn("search: read error");
4479 goto done;
4480 }
4481 resp[rbytes] = '\0';
4482
4483 /* XXX:
4484 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4485 * newline, so work around that by sanitizing the resp now.
4486 */
4487 resp[strcspn(resp, "\n")] = '\0';
4488 len = strlen(resp);
4489
4490 switch (search_resp_action) {
4491 case SWM_SEARCH_UNICONIFY:
4492 search_resp_uniconify(resp, len);
4493 break;
4494 case SWM_SEARCH_NAME_WORKSPACE:
4495 search_resp_name_workspace(resp, len);
4496 break;
4497 case SWM_SEARCH_SEARCH_WORKSPACE:
4498 search_resp_search_workspace(resp);
4499 break;
4500 case SWM_SEARCH_SEARCH_WINDOW:
4501 search_resp_search_window(resp);
4502 break;
4503 }
4504
4505 done:
4506 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
4507 search_win_cleanup();
4508
4509 search_resp_action = SWM_SEARCH_NONE;
4510 close(select_resp_pipe[0]);
4511 free(resp);
4512
4513 xcb_flush(conn);
4514 }
4515
4516 void
4517 wkill(struct swm_region *r, union arg *args)
4518 {
4519 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
4520
4521 if (r->ws->focus == NULL)
4522 return;
4523
4524 if (args->id == SWM_ARG_ID_KILLWINDOW)
4525 xcb_kill_client(conn, r->ws->focus->id);
4526 else
4527 if (r->ws->focus->can_delete)
4528 client_msg(r->ws->focus, a_delete, 0);
4529
4530 focus_flush();
4531 }
4532
4533 int
4534 floating_toggle_win(struct ws_win *win)
4535 {
4536 struct swm_region *r;
4537
4538 if (win == NULL)
4539 return (0);
4540
4541 if (!win->ws->r)
4542 return (0);
4543
4544 r = win->ws->r;
4545
4546 /* reject floating toggles in max stack mode */
4547 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
4548 return (0);
4549
4550 if (win->floating) {
4551 if (!win->floatmaxed) {
4552 /* retain position for refloat */
4553 store_float_geom(win, r);
4554 }
4555 win->floating = 0;
4556 } else {
4557 load_float_geom(win, r);
4558 win->floating = 1;
4559 }
4560
4561 ewmh_update_actions(win);
4562
4563 return (1);
4564 }
4565
4566 void
4567 floating_toggle(struct swm_region *r, union arg *args)
4568 {
4569 struct ws_win *win = r->ws->focus;
4570
4571 /* suppress unused warning since var is needed */
4572 (void)args;
4573
4574 if (win == NULL)
4575 return;
4576
4577 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4578 return;
4579
4580 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4581 _NET_WM_STATE_TOGGLE);
4582
4583 stack();
4584
4585 if (win == win->ws->focus)
4586 focus_win(win);
4587
4588 focus_flush();
4589 }
4590
4591 void
4592 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4593 {
4594 if (MAX_X(win) + BORDER(win) > MAX_X(r)) {
4595 if (resizable)
4596 WIDTH(win) = MAX_X(r) - X(win) - BORDER(win);
4597 else
4598 X(win) = MAX_X(r)- WIDTH(win) - BORDER(win);
4599 }
4600
4601 if (X(win) + BORDER(win) < X(r)) {
4602 if (resizable)
4603 WIDTH(win) -= X(r) - X(win) - BORDER(win);
4604
4605 X(win) = X(r) - BORDER(win);
4606 }
4607
4608 if (MAX_Y(win) + BORDER(win) > MAX_Y(r)) {
4609 if (resizable)
4610 HEIGHT(win) = MAX_Y(r) - Y(win) - BORDER(win);
4611 else
4612 Y(win) = MAX_Y(r) - HEIGHT(win) - BORDER(win);
4613 }
4614
4615 if (Y(win) + BORDER(win) < Y(r)) {
4616 if (resizable)
4617 HEIGHT(win) -= Y(r) - Y(win) - BORDER(win);
4618
4619 Y(win) = Y(r) - BORDER(win);
4620 }
4621
4622 if (resizable) {
4623 if (WIDTH(win) < 1)
4624 WIDTH(win) = 1;
4625 if (HEIGHT(win) < 1)
4626 HEIGHT(win) = 1;
4627 }
4628 }
4629
4630 void
4631 update_window(struct ws_win *win)
4632 {
4633 uint16_t mask;
4634 uint32_t wc[5];
4635
4636 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
4637 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
4638 XCB_CONFIG_WINDOW_BORDER_WIDTH;
4639 wc[0] = X(win);
4640 wc[1] = Y(win);
4641 wc[2] = WIDTH(win);
4642 wc[3] = HEIGHT(win);
4643 wc[4] = BORDER(win);
4644
4645 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
4646 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
4647 wc[3], YESNO(win->bordered));
4648
4649 xcb_configure_window(conn, win->id, mask, wc);
4650 }
4651
4652 #define SWM_RESIZE_STEPS (50)
4653
4654 void
4655 resize(struct ws_win *win, union arg *args)
4656 {
4657 xcb_timestamp_t timestamp = 0;
4658 struct swm_region *r = NULL;
4659 int resize_step = 0;
4660 struct swm_geometry g;
4661 int top = 0, left = 0, resizing;
4662 int dx, dy;
4663 unsigned int shape; /* cursor style */
4664 xcb_cursor_t cursor;
4665 xcb_font_t cursor_font;
4666 xcb_query_pointer_reply_t *xpr;
4667 xcb_generic_event_t *evt;
4668 xcb_motion_notify_event_t *mne;
4669
4670 if (win == NULL)
4671 return;
4672 r = win->ws->r;
4673
4674 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4675 return;
4676
4677 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
4678 "transient: 0x%x\n", win->id, YESNO(win->floating),
4679 win->transient);
4680
4681 if (!win->transient && !win->floating)
4682 return;
4683
4684 /* reject resizes in max mode for floaters (transient ok) */
4685 if (win->floatmaxed)
4686 return;
4687
4688 win->manual = 1;
4689 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4690 _NET_WM_STATE_ADD);
4691
4692 stack();
4693
4694 focus_flush();
4695
4696 switch (args->id) {
4697 case SWM_ARG_ID_WIDTHSHRINK:
4698 WIDTH(win) -= SWM_RESIZE_STEPS;
4699 resize_step = 1;
4700 break;
4701 case SWM_ARG_ID_WIDTHGROW:
4702 WIDTH(win) += SWM_RESIZE_STEPS;
4703 resize_step = 1;
4704 break;
4705 case SWM_ARG_ID_HEIGHTSHRINK:
4706 HEIGHT(win) -= SWM_RESIZE_STEPS;
4707 resize_step = 1;
4708 break;
4709 case SWM_ARG_ID_HEIGHTGROW:
4710 HEIGHT(win) += SWM_RESIZE_STEPS;
4711 resize_step = 1;
4712 break;
4713 default:
4714 break;
4715 }
4716 if (resize_step) {
4717 constrain_window(win, r, 1);
4718 update_window(win);
4719 store_float_geom(win,r);
4720 return;
4721 }
4722
4723 /* get cursor offset from window root */
4724 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4725 NULL);
4726 if (!xpr)
4727 return;
4728
4729 g = win->g;
4730
4731 if (xpr->win_x < WIDTH(win) / 2)
4732 left = 1;
4733
4734 if (xpr->win_y < HEIGHT(win) / 2)
4735 top = 1;
4736
4737 if (args->id == SWM_ARG_ID_CENTER)
4738 shape = XC_sizing;
4739 else if (top)
4740 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4741 else
4742 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4743
4744 cursor_font = xcb_generate_id(conn);
4745 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4746
4747 cursor = xcb_generate_id(conn);
4748 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4749 shape, shape + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4750
4751 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4752 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
4753 XCB_CURRENT_TIME),
4754
4755 xcb_flush(conn);
4756 resizing = 1;
4757 while ((evt = xcb_wait_for_event(conn)) && resizing) {
4758 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4759 case XCB_BUTTON_RELEASE:
4760 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
4761 resizing = 0;
4762 break;
4763 case XCB_MOTION_NOTIFY:
4764 mne = (xcb_motion_notify_event_t *)evt;
4765 /* cursor offset/delta from start of the operation */
4766 dx = mne->root_x - xpr->root_x;
4767 dy = mne->root_y - xpr->root_y;
4768
4769 /* vertical */
4770 if (top)
4771 dy = -dy;
4772
4773 if (args->id == SWM_ARG_ID_CENTER) {
4774 if (g.h / 2 + dy < 1)
4775 dy = 1 - g.h / 2;
4776
4777 Y(win) = g.y - dy;
4778 HEIGHT(win) = g.h + 2 * dy;
4779 } else {
4780 if (g.h + dy < 1)
4781 dy = 1 - g.h;
4782
4783 if (top)
4784 Y(win) = g.y - dy;
4785
4786 HEIGHT(win) = g.h + dy;
4787 }
4788
4789 /* horizontal */
4790 if (left)
4791 dx = -dx;
4792
4793 if (args->id == SWM_ARG_ID_CENTER) {
4794 if (g.w / 2 + dx < 1)
4795 dx = 1 - g.w / 2;
4796
4797 X(win) = g.x - dx;
4798 WIDTH(win) = g.w + 2 * dx;
4799 } else {
4800 if (g.w + dx < 1)
4801 dx = 1 - g.w;
4802
4803 if (left)
4804 X(win) = g.x - dx;
4805
4806 WIDTH(win) = g.w + dx;
4807 }
4808
4809 constrain_window(win, r, 1);
4810
4811 /* not free, don't sync more than 120 times / second */
4812 if ((mne->time - timestamp) > (1000 / 120) ) {
4813 timestamp = mne->time;
4814 update_window(win);
4815 xcb_flush(conn);
4816 }
4817 break;
4818 default:
4819 event_handle(evt);
4820 break;
4821 }
4822 free(evt);
4823 }
4824 if (timestamp) {
4825 update_window(win);
4826 xcb_flush(conn);
4827 }
4828 store_float_geom(win,r);
4829
4830 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4831 xcb_free_cursor(conn, cursor);
4832 xcb_close_font(conn, cursor_font);
4833 free(xpr);
4834 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
4835 }
4836
4837 void
4838 resize_step(struct swm_region *r, union arg *args)
4839 {
4840 struct ws_win *win = NULL;
4841
4842 if (r && r->ws && r->ws->focus)
4843 win = r->ws->focus;
4844 else
4845 return;
4846
4847 resize(win, args);
4848 focus_flush();
4849 }
4850
4851 #define SWM_MOVE_STEPS (50)
4852
4853 void
4854 move(struct ws_win *win, union arg *args)
4855 {
4856 xcb_timestamp_t timestamp = 0;
4857 int move_step = 0, moving;
4858 struct swm_region *r = NULL;
4859 xcb_font_t cursor_font;
4860 xcb_cursor_t cursor;
4861 xcb_query_pointer_reply_t *qpr;
4862 xcb_generic_event_t *evt;
4863 xcb_motion_notify_event_t *mne;
4864
4865 if (win == NULL)
4866 return;
4867 r = win->ws->r;
4868
4869 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
4870 return;
4871
4872 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
4873 "0x%x\n", win->id, YESNO(win->floating), win->transient);
4874
4875 /* in max_stack mode should only move transients */
4876 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4877 return;
4878
4879 win->manual = 1;
4880 if (!win->floating && !win->transient) {
4881 store_float_geom(win, r);
4882 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4883 _NET_WM_STATE_ADD);
4884 }
4885 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4886 _NET_WM_STATE_ADD);
4887
4888 stack();
4889
4890 focus_flush();
4891
4892 move_step = 0;
4893 switch (args->id) {
4894 case SWM_ARG_ID_MOVELEFT:
4895 X(win) -= (SWM_MOVE_STEPS - border_width);
4896 move_step = 1;
4897 break;
4898 case SWM_ARG_ID_MOVERIGHT:
4899 X(win) += (SWM_MOVE_STEPS - border_width);
4900 move_step = 1;
4901 break;
4902 case SWM_ARG_ID_MOVEUP:
4903 Y(win) -= (SWM_MOVE_STEPS - border_width);
4904 move_step = 1;
4905 break;
4906 case SWM_ARG_ID_MOVEDOWN:
4907 Y(win) += (SWM_MOVE_STEPS - border_width);
4908 move_step = 1;
4909 break;
4910 default:
4911 break;
4912 }
4913 if (move_step) {
4914 constrain_window(win, r, 0);
4915 update_window(win);
4916 store_float_geom(win, r);
4917 return;
4918 }
4919
4920 cursor_font = xcb_generate_id(conn);
4921 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
4922
4923 cursor = xcb_generate_id(conn);
4924 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
4925 XC_fleur, XC_fleur + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4926
4927 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
4928 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
4929 XCB_WINDOW_NONE, cursor, XCB_CURRENT_TIME);
4930
4931 /* get cursor offset from window root */
4932 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
4933 NULL);
4934 if (!qpr) {
4935 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4936 xcb_free_cursor(conn, cursor);
4937 xcb_close_font(conn, cursor_font);
4938 return;
4939 }
4940
4941 xcb_flush(conn);
4942 moving = 1;
4943 while ((evt = xcb_wait_for_event(conn)) && moving) {
4944 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
4945 case XCB_BUTTON_RELEASE:
4946 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
4947 moving = 0;
4948 break;
4949 case XCB_MOTION_NOTIFY:
4950 mne = (xcb_motion_notify_event_t *)evt;
4951 X(win) = mne->root_x - qpr->win_x - border_width;
4952 Y(win) = mne->root_y - qpr->win_y - border_width;
4953
4954 constrain_window(win, r, 0);
4955
4956 /* not free, don't sync more than 120 times / second */
4957 if ((mne->time - timestamp) > (1000 / 120) ) {
4958 timestamp = mne->time;
4959 update_window(win);
4960 xcb_flush(conn);
4961 }
4962 break;
4963 default:
4964 event_handle(evt);
4965 break;
4966 }
4967 free(evt);
4968 }
4969 if (timestamp) {
4970 update_window(win);
4971 xcb_flush(conn);
4972 }
4973 store_float_geom(win, r);
4974 free(qpr);
4975 xcb_free_cursor(conn, cursor);
4976 xcb_close_font(conn, cursor_font);
4977 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
4978 DNPRINTF(SWM_D_EVENT, "move: done.\n");
4979 }
4980
4981 void
4982 move_step(struct swm_region *r, union arg *args)
4983 {
4984 struct ws_win *win = NULL;
4985
4986 if (r && r->ws && r->ws->focus)
4987 win = r->ws->focus;
4988 else
4989 return;
4990
4991 if (!win->transient && !win->floating)
4992 return;
4993
4994 move(win, args);
4995 focus_flush();
4996 }
4997
4998 /* user/key callable function IDs */
4999 enum keyfuncid {
5000 KF_BAR_TOGGLE,
5001 KF_BUTTON2,
5002 KF_CYCLE_LAYOUT,
5003 KF_FLIP_LAYOUT,
5004 KF_FLOAT_TOGGLE,
5005 KF_FOCUS_MAIN,
5006 KF_FOCUS_NEXT,
5007 KF_FOCUS_PREV,
5008 KF_HEIGHT_GROW,
5009 KF_HEIGHT_SHRINK,
5010 KF_ICONIFY,
5011 KF_MASTER_SHRINK,
5012 KF_MASTER_GROW,
5013 KF_MASTER_ADD,
5014 KF_MASTER_DEL,
5015 KF_MOVE_DOWN,
5016 KF_MOVE_LEFT,
5017 KF_MOVE_RIGHT,
5018 KF_MOVE_UP,
5019 KF_MVWS_1,
5020 KF_MVWS_2,
5021 KF_MVWS_3,
5022 KF_MVWS_4,
5023 KF_MVWS_5,
5024 KF_MVWS_6,
5025 KF_MVWS_7,
5026 KF_MVWS_8,
5027 KF_MVWS_9,
5028 KF_MVWS_10,
5029 KF_MVWS_11,
5030 KF_MVWS_12,
5031 KF_MVWS_13,
5032 KF_MVWS_14,
5033 KF_MVWS_15,
5034 KF_MVWS_16,
5035 KF_MVWS_17,
5036 KF_MVWS_18,
5037 KF_MVWS_19,
5038 KF_MVWS_20,
5039 KF_MVWS_21,
5040 KF_MVWS_22,
5041 KF_NAME_WORKSPACE,
5042 KF_QUIT,
5043 KF_RAISE_TOGGLE,
5044 KF_RESTART,
5045 KF_SCREEN_NEXT,
5046 KF_SCREEN_PREV,
5047 KF_SEARCH_WIN,
5048 KF_SEARCH_WORKSPACE,
5049 KF_SPAWN_CUSTOM,
5050 KF_STACK_INC,
5051 KF_STACK_DEC,
5052 KF_STACK_RESET,
5053 KF_SWAP_MAIN,
5054 KF_SWAP_NEXT,
5055 KF_SWAP_PREV,
5056 KF_UNICONIFY,
5057 KF_VERSION,
5058 KF_WIDTH_GROW,
5059 KF_WIDTH_SHRINK,
5060 KF_WIND_DEL,
5061 KF_WIND_KILL,
5062 KF_WS_1,
5063 KF_WS_2,
5064 KF_WS_3,
5065 KF_WS_4,
5066 KF_WS_5,
5067 KF_WS_6,
5068 KF_WS_7,
5069 KF_WS_8,
5070 KF_WS_9,
5071 KF_WS_10,
5072 KF_WS_11,
5073 KF_WS_12,
5074 KF_WS_13,
5075 KF_WS_14,
5076 KF_WS_15,
5077 KF_WS_16,
5078 KF_WS_17,
5079 KF_WS_18,
5080 KF_WS_19,
5081 KF_WS_20,
5082 KF_WS_21,
5083 KF_WS_22,
5084 KF_WS_NEXT,
5085 KF_WS_NEXT_ALL,
5086 KF_WS_PREV,
5087 KF_WS_PREV_ALL,
5088 KF_WS_PRIOR,
5089 KF_DUMPWINS, /* MUST BE LAST */
5090 KF_INVALID
5091 };
5092
5093 /* key definitions */
5094 struct keyfunc {
5095 char name[SWM_FUNCNAME_LEN];
5096 void (*func)(struct swm_region *r, union arg *);
5097 union arg args;
5098 } keyfuncs[KF_INVALID + 1] = {
5099 /* name function argument */
5100 { "bar_toggle", bar_toggle, {0} },
5101 { "button2", pressbutton, {2} },
5102 { "cycle_layout", cycle_layout, {0} },
5103 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
5104 { "float_toggle", floating_toggle,{0} },
5105 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
5106 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
5107 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
5108 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
5109 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
5110 { "iconify", iconify, {0} },
5111 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
5112 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
5113 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
5114 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
5115 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
5116 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
5117 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
5118 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
5119 { "mvws_1", send_to_ws, {.id = 0} },
5120 { "mvws_2", send_to_ws, {.id = 1} },
5121 { "mvws_3", send_to_ws, {.id = 2} },
5122 { "mvws_4", send_to_ws, {.id = 3} },
5123 { "mvws_5", send_to_ws, {.id = 4} },
5124 { "mvws_6", send_to_ws, {.id = 5} },
5125 { "mvws_7", send_to_ws, {.id = 6} },
5126 { "mvws_8", send_to_ws, {.id = 7} },
5127 { "mvws_9", send_to_ws, {.id = 8} },
5128 { "mvws_10", send_to_ws, {.id = 9} },
5129 { "mvws_11", send_to_ws, {.id = 10} },
5130 { "mvws_12", send_to_ws, {.id = 11} },
5131 { "mvws_13", send_to_ws, {.id = 12} },
5132 { "mvws_14", send_to_ws, {.id = 13} },
5133 { "mvws_15", send_to_ws, {.id = 14} },
5134 { "mvws_16", send_to_ws, {.id = 15} },
5135 { "mvws_17", send_to_ws, {.id = 16} },
5136 { "mvws_18", send_to_ws, {.id = 17} },
5137 { "mvws_19", send_to_ws, {.id = 18} },
5138 { "mvws_20", send_to_ws, {.id = 19} },
5139 { "mvws_21", send_to_ws, {.id = 20} },
5140 { "mvws_22", send_to_ws, {.id = 21} },
5141 { "name_workspace", name_workspace, {0} },
5142 { "quit", quit, {0} },
5143 { "raise_toggle", raise_toggle, {0} },
5144 { "restart", restart, {0} },
5145 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
5146 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
5147 { "search_win", search_win, {0} },
5148 { "search_workspace", search_workspace, {0} },
5149 { "spawn_custom", NULL, {0} },
5150 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
5151 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
5152 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
5153 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
5154 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
5155 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
5156 { "uniconify", uniconify, {0} },
5157 { "version", version, {0} },
5158 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
5159 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
5160 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
5161 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
5162 { "ws_1", switchws, {.id = 0} },
5163 { "ws_2", switchws, {.id = 1} },
5164 { "ws_3", switchws, {.id = 2} },
5165 { "ws_4", switchws, {.id = 3} },
5166 { "ws_5", switchws, {.id = 4} },
5167 { "ws_6", switchws, {.id = 5} },
5168 { "ws_7", switchws, {.id = 6} },
5169 { "ws_8", switchws, {.id = 7} },
5170 { "ws_9", switchws, {.id = 8} },
5171 { "ws_10", switchws, {.id = 9} },
5172 { "ws_11", switchws, {.id = 10} },
5173 { "ws_12", switchws, {.id = 11} },
5174 { "ws_13", switchws, {.id = 12} },
5175 { "ws_14", switchws, {.id = 13} },
5176 { "ws_15", switchws, {.id = 14} },
5177 { "ws_16", switchws, {.id = 15} },
5178 { "ws_17", switchws, {.id = 16} },
5179 { "ws_18", switchws, {.id = 17} },
5180 { "ws_19", switchws, {.id = 18} },
5181 { "ws_20", switchws, {.id = 19} },
5182 { "ws_21", switchws, {.id = 20} },
5183 { "ws_22", switchws, {.id = 21} },
5184 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
5185 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
5186 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
5187 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
5188 { "ws_prior", priorws, {0} },
5189 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
5190 { "invalid key func", NULL, {0} },
5191 };
5192 struct key {
5193 RB_ENTRY(key) entry;
5194 unsigned int mod;
5195 KeySym keysym;
5196 enum keyfuncid funcid;
5197 char *spawn_name;
5198 };
5199 RB_HEAD(key_tree, key);
5200
5201 int
5202 key_cmp(struct key *kp1, struct key *kp2)
5203 {
5204 if (kp1->keysym < kp2->keysym)
5205 return (-1);
5206 if (kp1->keysym > kp2->keysym)
5207 return (1);
5208
5209 if (kp1->mod < kp2->mod)
5210 return (-1);
5211 if (kp1->mod > kp2->mod)
5212 return (1);
5213
5214 return (0);
5215 }
5216
5217 RB_GENERATE(key_tree, key, entry, key_cmp);
5218 struct key_tree keys;
5219
5220 /* mouse */
5221 enum { client_click, root_click };
5222 struct button {
5223 unsigned int action;
5224 unsigned int mask;
5225 unsigned int button;
5226 void (*func)(struct ws_win *, union arg *);
5227 union arg args;
5228 } buttons[] = {
5229 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5230 /* action key mouse button func args */
5231 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
5232 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
5233 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
5234 #undef MODKEY_SHIFT
5235 };
5236
5237 void
5238 update_modkey(unsigned int mod)
5239 {
5240 int i;
5241 struct key *kp;
5242
5243 mod_key = mod;
5244 RB_FOREACH(kp, key_tree, &keys)
5245 if (kp->mod & XCB_MOD_MASK_SHIFT)
5246 kp->mod = mod | XCB_MOD_MASK_SHIFT;
5247 else
5248 kp->mod = mod;
5249
5250 for (i = 0; i < LENGTH(buttons); i++)
5251 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
5252 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
5253 else
5254 buttons[i].mask = mod;
5255 }
5256
5257 /* spawn */
5258 struct spawn_prog {
5259 TAILQ_ENTRY(spawn_prog) entry;
5260 char *name;
5261 int argc;
5262 char **argv;
5263 };
5264 TAILQ_HEAD(spawn_list, spawn_prog);
5265 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
5266
5267 int
5268 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
5269 char ***ret_args)
5270 {
5271 struct spawn_prog *prog = NULL;
5272 int i;
5273 char *ap, **real_args;
5274
5275 /* suppress unused warning since var is needed */
5276 (void)args;
5277
5278 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
5279
5280 /* find program */
5281 TAILQ_FOREACH(prog, &spawns, entry) {
5282 if (!strcasecmp(spawn_name, prog->name))
5283 break;
5284 }
5285 if (prog == NULL) {
5286 warnx("spawn_custom: program %s not found", spawn_name);
5287 return (-1);
5288 }
5289
5290 /* make room for expanded args */
5291 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
5292 err(1, "spawn_custom: calloc real_args");
5293
5294 /* expand spawn_args into real_args */
5295 for (i = 0; i < prog->argc; i++) {
5296 ap = prog->argv[i];
5297 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
5298 if (!strcasecmp(ap, "$bar_border")) {
5299 if ((real_args[i] =
5300 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
5301 == NULL)
5302 err(1, "spawn_custom border color");
5303 } else if (!strcasecmp(ap, "$bar_color")) {
5304 if ((real_args[i] =
5305 strdup(r->s->c[SWM_S_COLOR_BAR].name))
5306 == NULL)
5307 err(1, "spawn_custom bar color");
5308 } else if (!strcasecmp(ap, "$bar_font")) {
5309 if ((real_args[i] = strdup(bar_fonts))
5310 == NULL)
5311 err(1, "spawn_custom bar fonts");
5312 } else if (!strcasecmp(ap, "$bar_font_color")) {
5313 if ((real_args[i] =
5314 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
5315 == NULL)
5316 err(1, "spawn_custom color font");
5317 } else if (!strcasecmp(ap, "$color_focus")) {
5318 if ((real_args[i] =
5319 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
5320 == NULL)
5321 err(1, "spawn_custom color focus");
5322 } else if (!strcasecmp(ap, "$color_unfocus")) {
5323 if ((real_args[i] =
5324 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
5325 == NULL)
5326 err(1, "spawn_custom color unfocus");
5327 } else {
5328 /* no match --> copy as is */
5329 if ((real_args[i] = strdup(ap)) == NULL)
5330 err(1, "spawn_custom strdup(ap)");
5331 }
5332 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
5333 real_args[i]);
5334 }
5335
5336 #ifdef SWM_DEBUG
5337 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
5338 for (i = 0; i < prog->argc; i++)
5339 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
5340 DNPRINTF(SWM_D_SPAWN, "\n");
5341 #endif
5342 *ret_args = real_args;
5343 return (prog->argc);
5344 }
5345
5346 void
5347 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
5348 {
5349 union arg a;
5350 char **real_args;
5351 int spawn_argc, i;
5352
5353 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5354 return;
5355 a.argv = real_args;
5356 if (fork() == 0)
5357 spawn(r->ws->idx, &a, 1);
5358
5359 for (i = 0; i < spawn_argc; i++)
5360 free(real_args[i]);
5361 free(real_args);
5362 }
5363
5364 void
5365 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
5366 {
5367 union arg a;
5368 char **real_args;
5369 int i, spawn_argc;
5370
5371 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
5372 return;
5373 a.argv = real_args;
5374
5375 if (pipe(select_list_pipe) == -1)
5376 err(1, "pipe error");
5377 if (pipe(select_resp_pipe) == -1)
5378 err(1, "pipe error");
5379
5380 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5381 err(1, "could not disable SIGPIPE");
5382 switch (*pid = fork()) {
5383 case -1:
5384 err(1, "cannot fork");
5385 break;
5386 case 0: /* child */
5387 if (dup2(select_list_pipe[0], 0) == -1)
5388 err(1, "dup2");
5389 if (dup2(select_resp_pipe[1], 1) == -1)
5390 err(1, "dup2");
5391 close(select_list_pipe[1]);
5392 close(select_resp_pipe[0]);
5393 spawn(r->ws->idx, &a, 0);
5394 break;
5395 default: /* parent */
5396 close(select_list_pipe[0]);
5397 close(select_resp_pipe[1]);
5398 break;
5399 }
5400
5401 for (i = 0; i < spawn_argc; i++)
5402 free(real_args[i]);
5403 free(real_args);
5404 }
5405
5406 void
5407 spawn_insert(char *name, char *args)
5408 {
5409 char *arg, *cp, *ptr;
5410 struct spawn_prog *sp;
5411
5412 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
5413
5414 if ((sp = calloc(1, sizeof *sp)) == NULL)
5415 err(1, "spawn_insert: calloc");
5416 if ((sp->name = strdup(name)) == NULL)
5417 err(1, "spawn_insert: strdup");
5418
5419 /* convert the arguments to an argument list */
5420 if ((ptr = cp = strdup(args)) == NULL)
5421 err(1, "spawn_insert: strdup");
5422 while ((arg = strsep(&ptr, " \t")) != NULL) {
5423 /* empty field; skip it */
5424 if (*arg == '\0')
5425 continue;
5426
5427 sp->argc++;
5428 if ((sp->argv = realloc(sp->argv, sp->argc *
5429 sizeof *sp->argv)) == NULL)
5430 err(1, "spawn_insert: realloc");
5431 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
5432 err(1, "spawn_insert: strdup");
5433 }
5434 free(cp);
5435
5436 TAILQ_INSERT_TAIL(&spawns, sp, entry);
5437 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
5438 }
5439
5440 void
5441 spawn_remove(struct spawn_prog *sp)
5442 {
5443 int i;
5444
5445 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
5446
5447 TAILQ_REMOVE(&spawns, sp, entry);
5448 for (i = 0; i < sp->argc; i++)
5449 free(sp->argv[i]);
5450 free(sp->argv);
5451 free(sp->name);
5452 free(sp);
5453
5454 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
5455 }
5456
5457 void
5458 spawn_replace(struct spawn_prog *sp, char *name, char *args)
5459 {
5460 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
5461
5462 spawn_remove(sp);
5463 spawn_insert(name, args);
5464
5465 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
5466 }
5467
5468 void
5469 setspawn(char *name, char *args)
5470 {
5471 struct spawn_prog *sp;
5472
5473 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
5474
5475 if (name == NULL)
5476 return;
5477
5478 TAILQ_FOREACH(sp, &spawns, entry) {
5479 if (!strcmp(sp->name, name)) {
5480 if (*args == '\0')
5481 spawn_remove(sp);
5482 else
5483 spawn_replace(sp, name, args);
5484 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5485 return;
5486 }
5487 }
5488 if (*args == '\0') {
5489 warnx("error: setspawn: cannot find program: %s", name);
5490 return;
5491 }
5492
5493 spawn_insert(name, args);
5494 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
5495 }
5496
5497 int
5498 setconfspawn(char *selector, char *value, int flags)
5499 {
5500 char *args;
5501
5502 /* suppress unused warning since var is needed */
5503 (void)flags;
5504
5505 args = expand_tilde(value);
5506
5507 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
5508
5509 setspawn(selector, args);
5510 free(args);
5511
5512 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
5513 return (0);
5514 }
5515
5516 void
5517 setup_spawn(void)
5518 {
5519 setconfspawn("term", "xterm", 0);
5520 setconfspawn("spawn_term", "xterm", 0);
5521 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5522 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5523 setconfspawn("lock", "xlock", 0);
5524 setconfspawn("initscr", "initscreen.sh", 0);
5525 setconfspawn("menu", "dmenu_run"
5526 " -fn $bar_font"
5527 " -nb $bar_color"
5528 " -nf $bar_font_color"
5529 " -sb $bar_border"
5530 " -sf $bar_color", 0);
5531 setconfspawn("search", "dmenu"
5532 " -i"
5533 " -fn $bar_font"
5534 " -nb $bar_color"
5535 " -nf $bar_font_color"
5536 " -sb $bar_border"
5537 " -sf $bar_color", 0);
5538 setconfspawn("name_workspace", "dmenu"
5539 " -p Workspace"
5540 " -fn $bar_font"
5541 " -nb $bar_color"
5542 " -nf $bar_font_color"
5543 " -sb $bar_border"
5544 " -sf $bar_color", 0);
5545 }
5546
5547 /* key bindings */
5548 #define SWM_MODNAME_SIZE 32
5549 #define SWM_KEY_WS "\n+ \t"
5550 int
5551 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
5552 {
5553 char *cp, *name;
5554 KeySym uks;
5555 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
5556 if (mod == NULL || ks == NULL) {
5557 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
5558 return (1);
5559 }
5560 if (keystr == NULL || strlen(keystr) == 0) {
5561 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
5562 return (1);
5563 }
5564 cp = keystr;
5565 *ks = XCB_NO_SYMBOL;
5566 *mod = 0;
5567 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
5568 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
5569 if (cp)
5570 cp += (long)strspn(cp, SWM_KEY_WS);
5571 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
5572 *mod |= currmod;
5573 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
5574 *mod |= XCB_MOD_MASK_1;
5575 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
5576 *mod += XCB_MOD_MASK_2;
5577 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
5578 *mod |= XCB_MOD_MASK_3;
5579 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
5580 *mod |= XCB_MOD_MASK_4;
5581 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
5582 *mod |= XCB_MOD_MASK_SHIFT;
5583 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
5584 *mod |= XCB_MOD_MASK_CONTROL;
5585 else {
5586 *ks = XStringToKeysym(name);
5587 XConvertCase(*ks, ks, &uks);
5588 if (ks == XCB_NO_SYMBOL) {
5589 DNPRINTF(SWM_D_KEY,
5590 "parsekeys: invalid key %s\n",
5591 name);
5592 return (1);
5593 }
5594 }
5595 }
5596 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
5597 return (0);
5598 }
5599
5600 char *
5601 strdupsafe(char *str)
5602 {
5603 if (str == NULL)
5604 return (NULL);
5605 else
5606 return (strdup(str));
5607 }
5608
5609 void
5610 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5611 {
5612 struct key *kp;
5613
5614 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5615 keyfuncs[kfid].name, spawn_name);
5616
5617 if ((kp = malloc(sizeof *kp)) == NULL)
5618 err(1, "key_insert: malloc");
5619
5620 kp->mod = mod;
5621 kp->keysym = ks;
5622 kp->funcid = kfid;
5623 kp->spawn_name = strdupsafe(spawn_name);
5624 RB_INSERT(key_tree, &keys, kp);
5625
5626 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5627 }
5628
5629 struct key *
5630 key_lookup(unsigned int mod, KeySym ks)
5631 {
5632 struct key kp;
5633
5634 kp.keysym = ks;
5635 kp.mod = mod;
5636
5637 return (RB_FIND(key_tree, &keys, &kp));
5638 }
5639
5640 void
5641 key_remove(struct key *kp)
5642 {
5643 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5644
5645 RB_REMOVE(key_tree, &keys, kp);
5646 free(kp->spawn_name);
5647 free(kp);
5648
5649 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5650 }
5651
5652 void
5653 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5654 char *spawn_name)
5655 {
5656 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5657 spawn_name);
5658
5659 key_remove(kp);
5660 key_insert(mod, ks, kfid, spawn_name);
5661
5662 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5663 }
5664
5665 void
5666 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5667 char *spawn_name)
5668 {
5669 struct key *kp;
5670
5671 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5672 keyfuncs[kfid].name, spawn_name);
5673
5674 if ((kp = key_lookup(mod, ks)) != NULL) {
5675 if (kfid == KF_INVALID)
5676 key_remove(kp);
5677 else
5678 key_replace(kp, mod, ks, kfid, spawn_name);
5679 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5680 return;
5681 }
5682 if (kfid == KF_INVALID) {
5683 warnx("error: setkeybinding: cannot find mod/key combination");
5684 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5685 return;
5686 }
5687
5688 key_insert(mod, ks, kfid, spawn_name);
5689 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5690 }
5691
5692 int
5693 setconfbinding(char *selector, char *value, int flags)
5694 {
5695 enum keyfuncid kfid;
5696 unsigned int mod;
5697 KeySym ks;
5698 struct spawn_prog *sp;
5699
5700 /* suppress unused warning since var is needed */
5701 (void)flags;
5702
5703 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5704 if (selector == NULL) {
5705 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5706 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5707 kfid = KF_INVALID;
5708 setkeybinding(mod, ks, kfid, NULL);
5709 return (0);
5710 } else
5711 return (1);
5712 }
5713 /* search by key function name */
5714 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
5715 if (strncasecmp(selector, keyfuncs[kfid].name,
5716 SWM_FUNCNAME_LEN) == 0) {
5717 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5718 selector);
5719 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5720 setkeybinding(mod, ks, kfid, NULL);
5721 return (0);
5722 } else
5723 return (1);
5724 }
5725 }
5726 /* search by custom spawn name */
5727 TAILQ_FOREACH(sp, &spawns, entry) {
5728 if (strcasecmp(selector, sp->name) == 0) {
5729 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5730 selector);
5731 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5732 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
5733 sp->name);
5734 return (0);
5735 } else
5736 return (1);
5737 }
5738 }
5739 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5740 return (1);
5741 }
5742
5743 void
5744 setup_keys(void)
5745 {
5746 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
5747 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
5748 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
5749 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
5750 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
5751 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
5752 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
5753 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
5754 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
5755 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
5756 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
5757 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
5758 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
5759 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
5760 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
5761 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
5762 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
5763 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
5764 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
5765 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
5766 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
5767 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
5768 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
5769 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
5770 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
5771 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
5772 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
5773 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
5774 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
5775 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
5776 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
5777 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
5778 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
5779 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
5780 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
5781 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
5782 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
5783 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
5784 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
5785 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
5786 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
5787 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
5788 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
5789 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
5790 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
5791 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
5792 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
5793 setkeybinding(MODKEY_SHIFT, XK_Right, KF_SCREEN_NEXT, NULL);
5794 setkeybinding(MODKEY_SHIFT, XK_Left, KF_SCREEN_PREV, NULL);
5795 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
5796 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
5797 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
5798 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
5799 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
5800 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
5801 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
5802 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
5803 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
5804 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
5805 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
5806 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
5807 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
5808 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
5809 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
5810 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
5811 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
5812 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
5813 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
5814 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
5815 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
5816 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
5817 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
5818 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
5819 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
5820 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
5821 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
5822 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
5823 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
5824 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
5825 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
5826 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
5827 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
5828 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
5829 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
5830 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
5831 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
5832 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
5833 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
5834 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
5835 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
5836 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
5837 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
5838 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
5839 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
5840 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
5841 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
5842 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
5843 #ifdef SWM_DEBUG
5844 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
5845 #endif
5846 #undef MODKEY_SHIFT
5847 }
5848
5849 void
5850 clear_keys(void)
5851 {
5852 struct key *kp;
5853
5854 while (RB_EMPTY(&keys) == 0) {
5855 kp = RB_ROOT(&keys);
5856 key_remove(kp);
5857 }
5858 }
5859
5860 int
5861 setkeymapping(char *selector, char *value, int flags)
5862 {
5863 char keymapping_file[PATH_MAX];
5864
5865 /* suppress unused warnings since vars are needed */
5866 (void)selector;
5867 (void)flags;
5868
5869 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5870 if (value[0] == '~')
5871 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5872 pwd->pw_dir, &value[1]);
5873 else
5874 strlcpy(keymapping_file, value, sizeof keymapping_file);
5875 clear_keys();
5876 /* load new key bindings; if it fails, revert to default bindings */
5877 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5878 clear_keys();
5879 setup_keys();
5880 }
5881 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5882 return (0);
5883 }
5884
5885 void
5886 updatenumlockmask(void)
5887 {
5888 unsigned int i, j;
5889 xcb_get_modifier_mapping_reply_t *modmap_r;
5890 xcb_keycode_t *modmap, kc, *keycode;
5891
5892 numlockmask = 0;
5893
5894 modmap_r = xcb_get_modifier_mapping_reply(conn,
5895 xcb_get_modifier_mapping(conn),
5896 NULL);
5897 if (modmap_r) {
5898 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
5899 for (i = 0; i < 8; i++) {
5900 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
5901 kc = modmap[i * modmap_r->keycodes_per_modifier
5902 + j];
5903 keycode = xcb_key_symbols_get_keycode(syms,
5904 XK_Num_Lock);
5905 if (kc == *keycode)
5906 numlockmask = (1 << i);
5907 free(keycode);
5908 }
5909 }
5910 free(modmap_r);
5911 }
5912 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
5913 }
5914
5915 void
5916 grabkeys(void)
5917 {
5918 struct key *kp;
5919 int num_screens, k, j;
5920 unsigned int modifiers[3];
5921 xcb_keycode_t *code;
5922
5923 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5924 updatenumlockmask();
5925
5926 modifiers[0] = 0;
5927 modifiers[1] = numlockmask;
5928 modifiers[2] = numlockmask | XCB_MOD_MASK_LOCK;
5929
5930 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
5931 for (k = 0; k < num_screens; k++) {
5932 if (TAILQ_EMPTY(&screens[k].rl))
5933 continue;
5934 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
5935 XCB_MOD_MASK_ANY);
5936 RB_FOREACH(kp, key_tree, &keys) {
5937 if ((code = xcb_key_symbols_get_keycode(syms,
5938 kp->keysym))) {
5939 for (j = 0; j < LENGTH(modifiers); j++)
5940 xcb_grab_key(conn, 1,
5941 screens[k].root,
5942 kp->mod | modifiers[j],
5943 *code, XCB_GRAB_MODE_SYNC,
5944 XCB_GRAB_MODE_ASYNC);
5945 free(code);
5946 }
5947 }
5948 }
5949 }
5950
5951 void
5952 grabbuttons(struct ws_win *win)
5953 {
5954 int i;
5955
5956 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win 0x%x\n", win->id);
5957
5958 for (i = 0; i < LENGTH(buttons); i++)
5959 if (buttons[i].action == client_click)
5960 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
5961 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
5962 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
5963 buttons[i].button, buttons[i].mask);
5964 }
5965
5966 const char *quirkname[] = {
5967 "NONE", /* config string for "no value" */
5968 "FLOAT",
5969 "TRANSSZ",
5970 "ANYWHERE",
5971 "XTERM_FONTADJ",
5972 "FULLSCREEN",
5973 "FOCUSPREV",
5974 };
5975
5976 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5977 #define SWM_Q_WS "\n|+ \t"
5978 int
5979 parsequirks(char *qstr, unsigned long *quirk)
5980 {
5981 char *cp, *name;
5982 int i;
5983
5984 if (quirk == NULL)
5985 return (1);
5986
5987 cp = qstr;
5988 *quirk = 0;
5989 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5990 if (cp)
5991 cp += (long)strspn(cp, SWM_Q_WS);
5992 for (i = 0; i < LENGTH(quirkname); i++) {
5993 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5994 DNPRINTF(SWM_D_QUIRK,
5995 "parsequirks: %s\n", name);
5996 if (i == 0) {
5997 *quirk = 0;
5998 return (0);
5999 }
6000 *quirk |= 1 << (i-1);
6001 break;
6002 }
6003 }
6004 if (i >= LENGTH(quirkname)) {
6005 DNPRINTF(SWM_D_QUIRK,
6006 "parsequirks: invalid quirk [%s]\n", name);
6007 return (1);
6008 }
6009 }
6010 return (0);
6011 }
6012
6013 void
6014 quirk_insert(const char *class, const char *name, unsigned long quirk)
6015 {
6016 struct quirk *qp;
6017
6018 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
6019 quirk);
6020
6021 if ((qp = malloc(sizeof *qp)) == NULL)
6022 err(1, "quirk_insert: malloc");
6023 if ((qp->class = strdup(class)) == NULL)
6024 err(1, "quirk_insert: strdup");
6025 if ((qp->name = strdup(name)) == NULL)
6026 err(1, "quirk_insert: strdup");
6027
6028 qp->quirk = quirk;
6029 TAILQ_INSERT_TAIL(&quirks, qp, entry);
6030
6031 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
6032 }
6033
6034 void
6035 quirk_remove(struct quirk *qp)
6036 {
6037 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
6038 qp->name, qp->quirk);
6039
6040 TAILQ_REMOVE(&quirks, qp, entry);
6041 free(qp->class);
6042 free(qp->name);
6043 free(qp);
6044
6045 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
6046 }
6047
6048 void
6049 quirk_replace(struct quirk *qp, const char *class, const char *name,
6050 unsigned long quirk)
6051 {
6052 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
6053 qp->name, qp->quirk);
6054
6055 quirk_remove(qp);
6056 quirk_insert(class, name, quirk);
6057
6058 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
6059 }
6060
6061 void
6062 setquirk(const char *class, const char *name, unsigned long quirk)
6063 {
6064 struct quirk *qp;
6065
6066 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
6067 quirk);
6068
6069 TAILQ_FOREACH(qp, &quirks, entry) {
6070 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
6071 if (!quirk)
6072 quirk_remove(qp);
6073 else
6074 quirk_replace(qp, class, name, quirk);
6075 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6076 return;
6077 }
6078 }
6079 if (!quirk) {
6080 warnx("error: setquirk: cannot find class/name combination");
6081 return;
6082 }
6083
6084 quirk_insert(class, name, quirk);
6085 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
6086 }
6087
6088 int
6089 setconfquirk(char *selector, char *value, int flags)
6090 {
6091 char *cp, *class, *name;
6092 int retval;
6093 unsigned long qrks;
6094
6095 /* suppress unused warning since var is needed */
6096 (void)flags;
6097
6098 if (selector == NULL)
6099 return (0);
6100 if ((cp = strchr(selector, ':')) == NULL)
6101 return (0);
6102 *cp = '\0';
6103 class = selector;
6104 name = cp + 1;
6105 if ((retval = parsequirks(value, &qrks)) == 0)
6106 setquirk(class, name, qrks);
6107 return (retval);
6108 }
6109
6110 void
6111 setup_quirks(void)
6112 {
6113 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
6114 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
6115 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
6116 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
6117 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6118 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
6119 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6120 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6121 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6122 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
6123 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
6124 setquirk("pcb", "pcb", SWM_Q_FLOAT);
6125 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
6126 }
6127
6128 /* conf file stuff */
6129 #define SWM_CONF_FILE "spectrwm.conf"
6130 #define SWM_CONF_FILE_OLD "scrotwm.conf"
6131
6132 enum {
6133 SWM_S_BAR_ACTION,
6134 SWM_S_BAR_AT_BOTTOM,
6135 SWM_S_BAR_BORDER_WIDTH,
6136 SWM_S_BAR_DELAY,
6137 SWM_S_BAR_ENABLED,
6138 SWM_S_BAR_FONT,
6139 SWM_S_BAR_FORMAT,
6140 SWM_S_BAR_JUSTIFY,
6141 SWM_S_BORDER_WIDTH,
6142 SWM_S_CLOCK_ENABLED,
6143 SWM_S_CLOCK_FORMAT,
6144 SWM_S_CYCLE_EMPTY,
6145 SWM_S_CYCLE_VISIBLE,
6146 SWM_S_DIALOG_RATIO,
6147 SWM_S_DISABLE_BORDER,
6148 SWM_S_FOCUS_CLOSE,
6149 SWM_S_FOCUS_CLOSE_WRAP,
6150 SWM_S_FOCUS_DEFAULT,
6151 SWM_S_FOCUS_MODE,
6152 SWM_S_SPAWN_ORDER,
6153 SWM_S_SPAWN_TERM,
6154 SWM_S_SS_APP,
6155 SWM_S_SS_ENABLED,
6156 SWM_S_STACK_ENABLED,
6157 SWM_S_TERM_WIDTH,
6158 SWM_S_TITLE_CLASS_ENABLED,
6159 SWM_S_TITLE_NAME_ENABLED,
6160 SWM_S_URGENT_ENABLED,
6161 SWM_S_VERBOSE_LAYOUT,
6162 SWM_S_WINDOW_NAME_ENABLED,
6163 SWM_S_WORKSPACE_LIMIT
6164 };
6165
6166 int
6167 setconfvalue(char *selector, char *value, int flags)
6168 {
6169 int i;
6170 char *b;
6171
6172 /* suppress unused warning since var is needed */
6173 (void)selector;
6174
6175 switch (flags) {
6176 case SWM_S_BAR_ACTION:
6177 free(bar_argv[0]);
6178 if ((bar_argv[0] = expand_tilde(value)) == NULL)
6179 err(1, "setconfvalue: bar_action");
6180 break;
6181 case SWM_S_BAR_AT_BOTTOM:
6182 bar_at_bottom = atoi(value);
6183 break;
6184 case SWM_S_BAR_BORDER_WIDTH:
6185 bar_border_width = atoi(value);
6186 if (bar_border_width < 0)
6187 bar_border_width = 0;
6188 break;
6189 case SWM_S_BAR_DELAY:
6190 bar_delay = atoi(value);
6191 break;
6192 case SWM_S_BAR_ENABLED:
6193 bar_enabled = atoi(value);
6194 break;
6195 case SWM_S_BAR_FONT:
6196 b = bar_fonts;
6197 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
6198 err(1, "setconfvalue: asprintf: failed to allocate "
6199 "memory for bar_fonts.");
6200 free(b);
6201
6202 /* If already in xft mode, then we are done. */
6203 if (!bar_font_legacy)
6204 break;
6205
6206 /* If there are any non-XLFD entries, switch to Xft mode. */
6207 while ((b = strsep(&value, ",")) != NULL) {
6208 if (*b == '\0')
6209 continue;
6210 if (!isxlfd(b)) {
6211 bar_font_legacy = 0;
6212 break;
6213 }
6214 }
6215 break;
6216 case SWM_S_BAR_FORMAT:
6217 free(bar_format);
6218 if ((bar_format = strdup(value)) == NULL)
6219 err(1, "setconfvalue: bar_format");
6220 break;
6221 case SWM_S_BAR_JUSTIFY:
6222 if (!strcmp(value, "left"))
6223 bar_justify = SWM_BAR_JUSTIFY_LEFT;
6224 else if (!strcmp(value, "center"))
6225 bar_justify = SWM_BAR_JUSTIFY_CENTER;
6226 else if (!strcmp(value, "right"))
6227 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
6228 else
6229 errx(1, "invalid bar_justify");
6230 break;
6231 case SWM_S_BORDER_WIDTH:
6232 border_width = atoi(value);
6233 if (border_width < 0)
6234 border_width = 0;
6235 break;
6236 case SWM_S_CLOCK_ENABLED:
6237 clock_enabled = atoi(value);
6238 break;
6239 case SWM_S_CLOCK_FORMAT:
6240 #ifndef SWM_DENY_CLOCK_FORMAT
6241 free(clock_format);
6242 if ((clock_format = strdup(value)) == NULL)
6243 err(1, "setconfvalue: clock_format");
6244 #endif
6245 break;
6246 case SWM_S_CYCLE_EMPTY:
6247 cycle_empty = atoi(value);
6248 break;
6249 case SWM_S_CYCLE_VISIBLE:
6250 cycle_visible = atoi(value);
6251 break;
6252 case SWM_S_DIALOG_RATIO:
6253 dialog_ratio = atof(value);
6254 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
6255 dialog_ratio = .6;
6256 break;
6257 case SWM_S_DISABLE_BORDER:
6258 disable_border = atoi(value);
6259 break;
6260 case SWM_S_FOCUS_CLOSE:
6261 if (!strcmp(value, "first"))
6262 focus_close = SWM_STACK_BOTTOM;
6263 else if (!strcmp(value, "last"))
6264 focus_close = SWM_STACK_TOP;
6265 else if (!strcmp(value, "next"))
6266 focus_close = SWM_STACK_ABOVE;
6267 else if (!strcmp(value, "previous"))
6268 focus_close = SWM_STACK_BELOW;
6269 else
6270 errx(1, "focus_close");
6271 break;
6272 case SWM_S_FOCUS_CLOSE_WRAP:
6273 focus_close_wrap = atoi(value);
6274 break;
6275 case SWM_S_FOCUS_DEFAULT:
6276 if (!strcmp(value, "last"))
6277 focus_default = SWM_STACK_TOP;
6278 else if (!strcmp(value, "first"))
6279 focus_default = SWM_STACK_BOTTOM;
6280 else
6281 errx(1, "focus_default");
6282 break;
6283 case SWM_S_FOCUS_MODE:
6284 if (!strcmp(value, "default"))
6285 focus_mode = SWM_FOCUS_DEFAULT;
6286 else if (!strcmp(value, "follow") ||
6287 !strcmp(value, "follow_cursor"))
6288 focus_mode = SWM_FOCUS_FOLLOW;
6289 else if (!strcmp(value, "manual"))
6290 focus_mode = SWM_FOCUS_MANUAL;
6291 else
6292 errx(1, "focus_mode");
6293 break;
6294 case SWM_S_SPAWN_ORDER:
6295 if (!strcmp(value, "first"))
6296 spawn_position = SWM_STACK_BOTTOM;
6297 else if (!strcmp(value, "last"))
6298 spawn_position = SWM_STACK_TOP;
6299 else if (!strcmp(value, "next"))
6300 spawn_position = SWM_STACK_ABOVE;
6301 else if (!strcmp(value, "previous"))
6302 spawn_position = SWM_STACK_BELOW;
6303 else
6304 errx(1, "spawn_position");
6305 break;
6306 case SWM_S_SPAWN_TERM:
6307 setconfspawn("term", value, 0);
6308 setconfspawn("spawn_term", value, 0);
6309 break;
6310 case SWM_S_SS_APP:
6311 break;
6312 case SWM_S_SS_ENABLED:
6313 ss_enabled = atoi(value);
6314 break;
6315 case SWM_S_STACK_ENABLED:
6316 stack_enabled = atoi(value);
6317 break;
6318 case SWM_S_TERM_WIDTH:
6319 term_width = atoi(value);
6320 if (term_width < 0)
6321 term_width = 0;
6322 break;
6323 case SWM_S_TITLE_CLASS_ENABLED:
6324 title_class_enabled = atoi(value);
6325 break;
6326 case SWM_S_TITLE_NAME_ENABLED:
6327 title_name_enabled = atoi(value);
6328 break;
6329 case SWM_S_URGENT_ENABLED:
6330 urgent_enabled = atoi(value);
6331 break;
6332 case SWM_S_VERBOSE_LAYOUT:
6333 verbose_layout = atoi(value);
6334 for (i = 0; layouts[i].l_stack != NULL; i++) {
6335 if (verbose_layout)
6336 layouts[i].l_string = fancy_stacker;
6337 else
6338 layouts[i].l_string = plain_stacker;
6339 }
6340 break;
6341 case SWM_S_WINDOW_NAME_ENABLED:
6342 window_name_enabled = atoi(value);
6343 break;
6344 case SWM_S_WORKSPACE_LIMIT:
6345 workspace_limit = atoi(value);
6346 if (workspace_limit > SWM_WS_MAX)
6347 workspace_limit = SWM_WS_MAX;
6348 else if (workspace_limit < 1)
6349 workspace_limit = 1;
6350 break;
6351 default:
6352 return (1);
6353 }
6354 return (0);
6355 }
6356
6357 int
6358 setconfmodkey(char *selector, char *value, int flags)
6359 {
6360 /* suppress unused warnings since vars are needed */
6361 (void)selector;
6362 (void)flags;
6363
6364 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
6365 update_modkey(XCB_MOD_MASK_1);
6366 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
6367 update_modkey(XCB_MOD_MASK_2);
6368 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
6369 update_modkey(XCB_MOD_MASK_3);
6370 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
6371 update_modkey(XCB_MOD_MASK_4);
6372 else
6373 return (1);
6374 return (0);
6375 }
6376
6377 int
6378 setconfcolor(char *selector, char *value, int flags)
6379 {
6380 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
6381 return (0);
6382 }
6383
6384 int
6385 setconfregion(char *selector, char *value, int flags)
6386 {
6387 /* suppress unused warnings since vars are needed */
6388 (void)selector;
6389 (void)flags;
6390
6391 custom_region(value);
6392 return (0);
6393 }
6394
6395 int
6396 setautorun(char *selector, char *value, int flags)
6397 {
6398 int ws_id;
6399 char s[1024];
6400 char *ap, *sp = s;
6401 union arg a;
6402 int argc = 0;
6403 pid_t pid;
6404 struct pid_e *p;
6405
6406 /* suppress unused warnings since vars are needed */
6407 (void)selector;
6408 (void)flags;
6409
6410 if (getenv("SWM_STARTED"))
6411 return (0);
6412
6413 bzero(s, sizeof s);
6414 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
6415 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6416 ws_id--;
6417 if (ws_id < 0 || ws_id >= workspace_limit)
6418 errx(1, "autorun: invalid workspace %d", ws_id + 1);
6419
6420 /*
6421 * This is a little intricate
6422 *
6423 * If the pid already exists we simply reuse it because it means it was
6424 * used before AND not claimed by manage_window. We get away with
6425 * altering it in the parent after INSERT because this can not be a race
6426 */
6427 a.argv = NULL;
6428 while ((ap = strsep(&sp, " \t")) != NULL) {
6429 if (*ap == '\0')
6430 continue;
6431 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
6432 argc++;
6433 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
6434 err(1, "setautorun: realloc");
6435 a.argv[argc - 1] = ap;
6436 }
6437
6438 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
6439 err(1, "setautorun: realloc");
6440 a.argv[argc] = NULL;
6441
6442 if ((pid = fork()) == 0) {
6443 spawn(ws_id, &a, 1);
6444 /* NOTREACHED */
6445 _exit(1);
6446 }
6447 free(a.argv);
6448
6449 /* parent */
6450 p = find_pid(pid);
6451 if (p == NULL) {
6452 p = calloc(1, sizeof *p);
6453 if (p == NULL)
6454 return (1);
6455 TAILQ_INSERT_TAIL(&pidlist, p, entry);
6456 }
6457
6458 p->pid = pid;
6459 p->ws = ws_id;
6460
6461 return (0);
6462 }
6463
6464 int
6465 setlayout(char *selector, char *value, int flags)
6466 {
6467 int ws_id, i, x, mg, ma, si, ar, f = 0;
6468 int st = SWM_V_STACK, num_screens;
6469 char s[1024];
6470 struct workspace *ws;
6471
6472 /* suppress unused warnings since vars are needed */
6473 (void)selector;
6474 (void)flags;
6475
6476 if (getenv("SWM_STARTED"))
6477 return (0);
6478
6479 bzero(s, sizeof s);
6480 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
6481 &ws_id, &mg, &ma, &si, &ar, s) != 6)
6482 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6483 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6484 "<type>'");
6485 ws_id--;
6486 if (ws_id < 0 || ws_id >= workspace_limit)
6487 errx(1, "layout: invalid workspace %d", ws_id + 1);
6488
6489 if (!strcasecmp(s, "vertical"))
6490 st = SWM_V_STACK;
6491 else if (!strcasecmp(s, "vertical_flip")) {
6492 st = SWM_V_STACK;
6493 f = 1;
6494 } else if (!strcasecmp(s, "horizontal"))
6495 st = SWM_H_STACK;
6496 else if (!strcasecmp(s, "horizontal_flip")) {
6497 st = SWM_H_STACK;
6498 f = 1;
6499 } else if (!strcasecmp(s, "fullscreen"))
6500 st = SWM_MAX_STACK;
6501 else
6502 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6503 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6504 "<type>'");
6505
6506 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
6507 for (i = 0; i < num_screens; i++) {
6508 ws = (struct workspace *)&screens[i].ws;
6509 ws[ws_id].cur_layout = &layouts[st];
6510
6511 ws[ws_id].always_raise = ar;
6512 if (st == SWM_MAX_STACK)
6513 continue;
6514
6515 /* master grow */
6516 for (x = 0; x < abs(mg); x++) {
6517 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6518 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
6519 SWM_ARG_ID_MASTERSHRINK);
6520 stack();
6521 }
6522 /* master add */
6523 for (x = 0; x < abs(ma); x++) {
6524 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6525 ma >= 0 ? SWM_ARG_ID_MASTERADD :
6526 SWM_ARG_ID_MASTERDEL);
6527 stack();
6528 }
6529 /* stack inc */
6530 for (x = 0; x < abs(si); x++) {
6531 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6532 si >= 0 ? SWM_ARG_ID_STACKINC :
6533 SWM_ARG_ID_STACKDEC);
6534 stack();
6535 }
6536 /* Apply flip */
6537 if (f) {
6538 ws[ws_id].cur_layout->l_config(&ws[ws_id],
6539 SWM_ARG_ID_FLIPLAYOUT);
6540 stack();
6541 }
6542 }
6543
6544 focus_flush();
6545
6546 return (0);
6547 }
6548
6549 /* config options */
6550 struct config_option {
6551 char *optname;
6552 int (*func)(char*, char*, int);
6553 int funcflags;
6554 };
6555 struct config_option configopt[] = {
6556 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
6557 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
6558 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
6559 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
6560 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
6561 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
6562 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
6563 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
6564 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
6565 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
6566 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
6567 { "keyboard_mapping", setkeymapping, 0 },
6568 { "bind", setconfbinding, 0 },
6569 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
6570 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
6571 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
6572 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
6573 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
6574 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
6575 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
6576 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
6577 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
6578 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
6579 { "modkey", setconfmodkey, 0 },
6580 { "program", setconfspawn, 0 },
6581 { "quirk", setconfquirk, 0 },
6582 { "region", setconfregion, 0 },
6583 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
6584 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
6585 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
6586 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
6587 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
6588 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
6589 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
6590 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
6591 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
6592 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
6593 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
6594 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
6595 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
6596 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
6597 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
6598 { "autorun", setautorun, 0 },
6599 { "layout", setlayout, 0 },
6600 };
6601
6602 int
6603 conf_load(char *filename, int keymapping)
6604 {
6605 FILE *config;
6606 char *line, *cp, *optsub, *optval;
6607 size_t linelen, lineno = 0;
6608 int wordlen, i, optidx;
6609 struct config_option *opt = NULL;
6610
6611 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
6612
6613 if (filename == NULL) {
6614 warnx("conf_load: no filename");
6615 return (1);
6616 }
6617 if ((config = fopen(filename, "r")) == NULL) {
6618 warn("conf_load: fopen: %s", filename);
6619 return (1);
6620 }
6621
6622 while (!feof(config)) {
6623 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
6624 == NULL) {
6625 if (ferror(config))
6626 err(1, "%s", filename);
6627 else
6628 continue;
6629 }
6630 cp = line;
6631 cp += strspn(cp, " \t\n"); /* eat whitespace */
6632 if (cp[0] == '\0') {
6633 /* empty line */
6634 free(line);
6635 continue;
6636 }
6637 /* get config option */
6638 wordlen = strcspn(cp, "=[ \t\n");
6639 if (wordlen == 0) {
6640 warnx("%s: line %zd: no option found",
6641 filename, lineno);
6642 goto out;
6643 }
6644 optidx = -1;
6645 for (i = 0; i < LENGTH(configopt); i++) {
6646 opt = &configopt[i];
6647 if (!strncasecmp(cp, opt->optname, wordlen) &&
6648 (int)strlen(opt->optname) == wordlen) {
6649 optidx = i;
6650 break;
6651 }
6652 }
6653 if (optidx == -1) {
6654 warnx("%s: line %zd: unknown option %.*s",
6655 filename, lineno, wordlen, cp);
6656 goto out;
6657 }
6658 if (keymapping && opt && strcmp(opt->optname, "bind")) {
6659 warnx("%s: line %zd: invalid option %.*s",
6660 filename, lineno, wordlen, cp);
6661 goto out;
6662 }
6663 cp += wordlen;
6664 cp += strspn(cp, " \t\n"); /* eat whitespace */
6665 /* get [selector] if any */
6666 optsub = NULL;
6667 if (*cp == '[') {
6668 cp++;
6669 wordlen = strcspn(cp, "]");
6670 if (*cp != ']') {
6671 if (wordlen == 0) {
6672 warnx("%s: line %zd: syntax error",
6673 filename, lineno);
6674 goto out;
6675 }
6676
6677 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
6678 -1) {
6679 warnx("%s: line %zd: unable to allocate"
6680 "memory for selector", filename,
6681 lineno);
6682 goto out;
6683 }
6684 }
6685 cp += wordlen;
6686 cp += strspn(cp, "] \t\n"); /* eat trailing */
6687 }
6688 cp += strspn(cp, "= \t\n"); /* eat trailing */
6689 /* get RHS value */
6690 optval = strdup(cp);
6691 /* call function to deal with it all */
6692 if (configopt[optidx].func(optsub, optval,
6693 configopt[optidx].funcflags) != 0)
6694 errx(1, "%s: line %zd: invalid data for %s",
6695 filename, lineno, configopt[optidx].optname);
6696 free(optval);
6697 free(optsub);
6698 free(line);
6699 }
6700
6701 fclose(config);
6702 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
6703
6704 return (0);
6705
6706 out:
6707 free(line);
6708 fclose(config);
6709 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6710
6711 return (1);
6712 }
6713
6714 void
6715 set_child_transient(struct ws_win *win, xcb_window_t *trans)
6716 {
6717 struct ws_win *parent, *w;
6718 struct swm_region *r;
6719 struct workspace *ws;
6720 xcb_icccm_wm_hints_t wmh;
6721
6722 parent = find_window(win->transient);
6723 if (parent)
6724 parent->focus_child = win;
6725 else {
6726 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6727 " for 0x%x trans 0x%x\n", win->id, win->transient);
6728
6729 r = root_to_region(win->wa->root, SWM_CK_ALL);
6730 ws = r->ws;
6731 /* parent doen't exist in our window list */
6732 TAILQ_FOREACH(w, &ws->winlist, entry) {
6733 if (xcb_icccm_get_wm_hints_reply(conn,
6734 xcb_icccm_get_wm_hints(conn, w->id),
6735 &wmh, NULL) != 1) {
6736 warnx("can't get hints for 0x%x", w->id);
6737 continue;
6738 }
6739
6740 if (win->hints.window_group != wmh.window_group)
6741 continue;
6742
6743 w->focus_child = win;
6744 win->transient = w->id;
6745 *trans = w->id;
6746 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
6747 "transient to 0x%x\n", win->transient);
6748 break;
6749 }
6750 }
6751 }
6752
6753 pid_t
6754 window_get_pid(xcb_window_t win)
6755 {
6756 pid_t ret = 0;
6757 const char *errstr;
6758 xcb_atom_t apid;
6759 xcb_get_property_cookie_t pc;
6760 xcb_get_property_reply_t *pr;
6761
6762 apid = get_atom_from_string("_NET_WM_PID");
6763 if (apid == XCB_ATOM_NONE)
6764 goto tryharder;
6765
6766 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
6767 pr = xcb_get_property_reply(conn, pc, NULL);
6768 if (!pr)
6769 goto tryharder;
6770 if (pr->type != XCB_ATOM_CARDINAL) {
6771 free(pr);
6772 goto tryharder;
6773 }
6774
6775 if (pr->type == apid && pr->format == 32)
6776 ret = *((pid_t *)xcb_get_property_value(pr));
6777 free(pr);
6778
6779 return (ret);
6780
6781 tryharder:
6782 apid = get_atom_from_string("_SWM_PID");
6783 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
6784 0, SWM_PROPLEN);
6785 pr = xcb_get_property_reply(conn, pc, NULL);
6786 if (!pr)
6787 return (0);
6788 if (pr->type != apid) {
6789 free(pr);
6790 return (0);
6791 }
6792
6793 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
6794 free(pr);
6795
6796 return (ret);
6797 }
6798
6799 int
6800 get_ws_idx(xcb_window_t id)
6801 {
6802 int ws_idx = -1;
6803 char *prop = NULL;
6804 size_t proplen;
6805 const char *errstr;
6806 xcb_get_property_reply_t *gpr;
6807
6808 gpr = xcb_get_property_reply(conn,
6809 xcb_get_property(conn, 0, id, a_swm_ws,
6810 XCB_ATOM_STRING, 0, SWM_PROPLEN),
6811 NULL);
6812 if (!gpr)
6813 return (-1);
6814 if (gpr->type) {
6815 proplen = xcb_get_property_value_length(gpr);
6816 if (proplen > 0) {
6817 prop = malloc(proplen + 1);
6818 if (prop) {
6819 memcpy(prop,
6820 xcb_get_property_value(gpr),
6821 proplen);
6822 prop[proplen] = '\0';
6823 }
6824 }
6825 }
6826 free(gpr);
6827
6828 if (prop) {
6829 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
6830 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
6831 if (errstr) {
6832 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
6833 errstr, prop);
6834 }
6835 free(prop);
6836 }
6837
6838 return ws_idx;
6839 }
6840
6841 struct ws_win *
6842 manage_window(xcb_window_t id, uint16_t mapped)
6843 {
6844 xcb_window_t trans = XCB_WINDOW_NONE;
6845 struct ws_win *win, *ww;
6846 int ws_idx;
6847 char ws_idx_str[SWM_PROPLEN];
6848 struct swm_region *r;
6849 struct pid_e *p;
6850 struct quirk *qp;
6851 uint32_t i, wa[2];
6852 xcb_icccm_get_wm_protocols_reply_t wpr;
6853
6854 if ((win = find_window(id)) != NULL) {
6855 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x already "
6856 "managed; skipping.)\n", id);
6857 return (win); /* Already managed. */
6858 }
6859
6860 /* See if window is on the unmanaged list. */
6861 if ((win = find_unmanaged_window(id)) != NULL) {
6862 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x found on "
6863 "unmanaged list.\n", id);
6864 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6865
6866 if (win->transient)
6867 set_child_transient(win, &trans);
6868
6869 goto out;
6870 } else {
6871 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x is new.\n", id);
6872 }
6873
6874 /* Create and initialize ws_win object. */
6875 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6876 err(1, "manage_window: calloc: failed to allocate memory for "
6877 "new window");
6878
6879 win->id = id;
6880
6881 /* Get window geometry. */
6882 win->wa = xcb_get_geometry_reply(conn,
6883 xcb_get_geometry(conn, win->id),
6884 NULL);
6885
6886 /* Figure out which region the window belongs to. */
6887 r = root_to_region(win->wa->root, SWM_CK_ALL);
6888
6889 /* Ignore window border if there is one. */
6890 WIDTH(win) = win->wa->width;
6891 HEIGHT(win) = win->wa->height;
6892 X(win) = win->wa->x + win->wa->border_width - border_width;
6893 Y(win) = win->wa->y + win->wa->border_width - border_width;
6894 win->bordered = 1;
6895 win->mapped = mapped;
6896 win->floatmaxed = 0;
6897 win->ewmh_flags = 0;
6898 win->s = r->s; /* this never changes */
6899
6900 store_float_geom(win, r);
6901
6902 /* Get WM_SIZE_HINTS. */
6903 xcb_icccm_get_wm_normal_hints_reply(conn,
6904 xcb_icccm_get_wm_normal_hints(conn, win->id),
6905 &win->sh, NULL);
6906
6907 /* Get WM_HINTS. */
6908 xcb_icccm_get_wm_hints_reply(conn,
6909 xcb_icccm_get_wm_hints(conn, win->id),
6910 &win->hints, NULL);
6911
6912 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
6913 xcb_icccm_get_wm_transient_for_reply(conn,
6914 xcb_icccm_get_wm_transient_for(conn, win->id),
6915 &trans, NULL);
6916 if (trans) {
6917 win->transient = trans;
6918 set_child_transient(win, &win->transient);
6919 }
6920
6921 /* Get supported protocols. */
6922 if (xcb_icccm_get_wm_protocols_reply(conn,
6923 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
6924 &wpr, NULL)) {
6925 for (i = 0; i < wpr.atoms_len; i++) {
6926 if (wpr.atoms[i] == a_takefocus)
6927 win->take_focus = 1;
6928 if (wpr.atoms[i] == a_delete)
6929 win->can_delete = 1;
6930 }
6931 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
6932 }
6933
6934 win->iconic = get_swm_iconic(win);
6935
6936 /* Figure out which workspace the window belongs to. */
6937 if ((p = find_pid(window_get_pid(win->id))) != NULL) {
6938 win->ws = &r->s->ws[p->ws];
6939 TAILQ_REMOVE(&pidlist, p, entry);
6940 free(p);
6941 p = NULL;
6942 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
6943 !win->transient) {
6944 /* _SWM_WS is set; use that. */
6945 win->ws = &r->s->ws[ws_idx];
6946 } else if (trans && (ww = find_window(trans)) != NULL) {
6947 /* Launch transients in the same ws as parent. */
6948 win->ws = ww->ws;
6949 } else {
6950 win->ws = r->ws;
6951 }
6952
6953 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
6954 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", win->ws->idx) <
6955 SWM_PROPLEN) {
6956 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6957 ws_idx_str);
6958 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
6959 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
6960 ws_idx_str);
6961 }
6962
6963 /* Handle EWMH */
6964 ewmh_autoquirk(win);
6965
6966 /* Determine initial quirks. */
6967 if (xcb_icccm_get_wm_class_reply(conn,
6968 xcb_icccm_get_wm_class(conn, win->id),
6969 &win->ch, NULL)) {
6970 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6971 win->ch.class_name, win->ch.instance_name);
6972
6973 /* java is retarded so treat it special */
6974 if (strstr(win->ch.instance_name, "sun-awt")) {
6975 DNPRINTF(SWM_D_CLASS, "manage_window: java window "
6976 "detected.\n");
6977 win->java = 1;
6978 }
6979
6980 TAILQ_FOREACH(qp, &quirks, entry) {
6981 if (!strcmp(win->ch.class_name, qp->class) &&
6982 !strcmp(win->ch.instance_name, qp->name)) {
6983 DNPRINTF(SWM_D_CLASS, "manage_window: on quirks"
6984 "list; mask: 0x%lx\n", qp->quirk);
6985 if (qp->quirk & SWM_Q_FLOAT)
6986 win->floating = 1;
6987 win->quirks = qp->quirk;
6988 }
6989 }
6990 }
6991
6992 /* Alter window position if quirky */
6993 if (win->quirks & SWM_Q_ANYWHERE)
6994 win->manual = 1;
6995
6996 /* Reset font sizes (the bruteforce way; no default keybinding). */
6997 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6998 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6999 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
7000 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
7001 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
7002 }
7003
7004 /* Make sure window is positioned inside its region, if its active. */
7005 if (win->ws->r) {
7006 constrain_window(win, win->ws->r, 0);
7007 update_window(win);
7008 }
7009
7010 /* Select which X events to monitor and set border pixel color. */
7011 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
7012 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
7013 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
7014 #ifdef SWM_DEBUG
7015 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
7016 #endif
7017
7018 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
7019 XCB_CW_EVENT_MASK, wa);
7020
7021 out:
7022 /* Figure out where to stack the window in the workspace. */
7023 if (trans && (ww = find_window(trans)))
7024 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
7025 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
7026 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
7027 entry);
7028 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
7029 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
7030 else switch (spawn_position) {
7031 default:
7032 case SWM_STACK_TOP:
7033 case SWM_STACK_ABOVE:
7034 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
7035 break;
7036 case SWM_STACK_BOTTOM:
7037 case SWM_STACK_BELOW:
7038 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
7039 }
7040
7041 /* Get initial _NET_WM_STATE */
7042 ewmh_get_win_state(win);
7043 /* Set initial _NET_WM_ALLOWED_ACTIONS */
7044 ewmh_update_actions(win);
7045
7046 grabbuttons(win);
7047
7048 DNPRINTF(SWM_D_MISC, "manage_window: done. window: 0x%x, (x,y) w x h: "
7049 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win->id,
7050 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
7051 YESNO(win->iconic), win->transient);
7052
7053 return (win);
7054 }
7055
7056 void
7057 free_window(struct ws_win *win)
7058 {
7059 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
7060
7061 if (win == NULL)
7062 return;
7063
7064 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
7065
7066 if (win->wa)
7067 free(win->wa);
7068
7069 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
7070
7071 kill_refs(win);
7072
7073 /* paint memory */
7074 memset(win, 0xff, sizeof *win); /* XXX kill later */
7075
7076 free(win);
7077 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
7078 }
7079
7080 void
7081 unmanage_window(struct ws_win *win)
7082 {
7083 struct ws_win *parent;
7084
7085 if (win == NULL)
7086 return;
7087
7088 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
7089
7090 if (win->transient) {
7091 parent = find_window(win->transient);
7092 if (parent)
7093 parent->focus_child = NULL;
7094 }
7095
7096 TAILQ_REMOVE(&win->ws->winlist, win, entry);
7097 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
7098 }
7099
7100 void
7101 expose(xcb_expose_event_t *e)
7102 {
7103 int i, num_screens;
7104 struct swm_region *r;
7105
7106 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
7107
7108 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7109 for (i = 0; i < num_screens; i++)
7110 TAILQ_FOREACH(r, &screens[i].rl, entry)
7111 if (e->window == WINID(r->bar))
7112 bar_update();
7113
7114 xcb_flush(conn);
7115 }
7116
7117 #ifdef SWM_DEBUG
7118 void
7119 focusin(xcb_focus_in_event_t *e)
7120 {
7121 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
7122 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7123 e->mode, get_notify_detail_label(e->detail), e->detail);
7124 }
7125
7126 void
7127 focusout(xcb_focus_out_event_t *e)
7128 {
7129 DNPRINTF(SWM_D_EVENT, "focusout: window: 0x%x, mode: %s(%u), "
7130 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
7131 e->mode, get_notify_detail_label(e->detail), e->detail);
7132 }
7133 #endif
7134
7135 void
7136 keypress(xcb_key_press_event_t *e)
7137 {
7138 xcb_keysym_t keysym;
7139 struct key *kp;
7140
7141 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
7142
7143 DNPRINTF(SWM_D_EVENT, "keypress: %u %u\n", e->detail, keysym);
7144
7145 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
7146 return;
7147
7148 last_event_time = e->time;
7149
7150 if (kp->funcid == KF_SPAWN_CUSTOM)
7151 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
7152 &(keyfuncs[kp->funcid].args), kp->spawn_name);
7153 else if (keyfuncs[kp->funcid].func)
7154 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
7155 &(keyfuncs[kp->funcid].args));
7156 }
7157
7158 void
7159 buttonpress(xcb_button_press_event_t *e)
7160 {
7161 struct ws_win *win = NULL;
7162 struct swm_region *r, *old_r;
7163 int i;
7164 int handled = 0;
7165
7166 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): 0x%x (%d,%d), "
7167 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
7168 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
7169 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
7170 e->state, YESNO(e->same_screen));
7171
7172 if (e->event == e->root) {
7173 if (e->child != 0) {
7174 win = find_window(e->child);
7175 /* Pass ButtonPress to window if it isn't managed. */
7176 if (win == NULL)
7177 goto out;
7178 } else {
7179 /* Focus on empty region */
7180 /* If no windows on region if its empty. */
7181 r = root_to_region(e->root, SWM_CK_POINTER);
7182 if (TAILQ_EMPTY(&r->ws->winlist)) {
7183 old_r = root_to_region(e->root, SWM_CK_FOCUS);
7184 if (old_r && old_r != r)
7185 unfocus_win(old_r->ws->focus);
7186
7187 xcb_set_input_focus(conn,
7188 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
7189
7190 /* Clear bar since empty. */
7191 bar_update();
7192
7193 handled = 1;
7194 goto out;
7195 }
7196 }
7197 } else {
7198 win = find_window(e->event);
7199 }
7200
7201 if (win == NULL)
7202 return;
7203
7204 last_event_time = e->time;
7205
7206 focus_win(get_focus_magic(win));
7207
7208 for (i = 0; i < LENGTH(buttons); i++)
7209 if (client_click == buttons[i].action && buttons[i].func &&
7210 buttons[i].button == e->detail &&
7211 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
7212 buttons[i].func(win, &buttons[i].args);
7213 handled = 1;
7214 }
7215
7216 out:
7217 if (!handled) {
7218 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
7219 /* Replay event to event window */
7220 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
7221 } else {
7222 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
7223 /* Unfreeze grab events. */
7224 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
7225 }
7226
7227 xcb_flush(conn);
7228 }
7229
7230 #ifdef SWM_DEBUG
7231 void
7232 print_win_geom(xcb_window_t w)
7233 {
7234 xcb_get_geometry_reply_t *wa;
7235
7236 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
7237
7238 if (!wa) {
7239 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
7240 w);
7241 return;
7242 }
7243
7244 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
7245 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
7246 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
7247 wa->border_width);
7248
7249 free(wa);
7250 }
7251 #endif
7252
7253 #ifdef SWM_DEBUG
7254 char *
7255 get_stack_mode_name(uint8_t mode)
7256 {
7257 char *name;
7258
7259 switch(mode) {
7260 case XCB_STACK_MODE_ABOVE:
7261 name = "Above";
7262 break;
7263 case XCB_STACK_MODE_BELOW:
7264 name = "Below";
7265 break;
7266 case XCB_STACK_MODE_TOP_IF:
7267 name = "TopIf";
7268 break;
7269 case XCB_STACK_MODE_BOTTOM_IF:
7270 name = "BottomIf";
7271 break;
7272 case XCB_STACK_MODE_OPPOSITE:
7273 name = "Opposite";
7274 break;
7275 default:
7276 name = "Unknown";
7277 }
7278
7279 return name;
7280 }
7281 #endif
7282
7283 void
7284 configurerequest(xcb_configure_request_event_t *e)
7285 {
7286 struct ws_win *win;
7287 struct swm_region *r = NULL;
7288 int new = 0, i = 0;
7289 uint16_t mask = 0;
7290 uint32_t wc[7] = {0};
7291
7292 if ((win = find_window(e->window)) == NULL)
7293 if ((win = find_unmanaged_window(e->window)) == NULL)
7294 new = 1;
7295
7296 #ifdef SWM_DEBUG
7297 if (swm_debug & SWM_D_EVENT) {
7298 print_win_geom(e->window);
7299
7300 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%x, "
7301 "parent: 0x%x, new: %s, value_mask: %u { ", e->window,
7302 e->parent, YESNO(new), e->value_mask);
7303 if (e->value_mask & XCB_CONFIG_WINDOW_X)
7304 DPRINTF("X: %d ", e->x);
7305 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
7306 DPRINTF("Y: %d ", e->y);
7307 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
7308 DPRINTF("W: %u ", e->width);
7309 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
7310 DPRINTF("H: %u ", e->height);
7311 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
7312 DPRINTF("Border: %u ", e->border_width);
7313 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
7314 DPRINTF("Sibling: 0x%x ", e->sibling);
7315 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
7316 DPRINTF("StackMode: %s(%u) ",
7317 get_stack_mode_name(e->stack_mode), e->stack_mode);
7318 DPRINTF("}\n");
7319 }
7320 #endif
7321
7322 if (new) {
7323 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7324 mask |= XCB_CONFIG_WINDOW_X;
7325 wc[i++] = e->x;
7326 }
7327 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7328 mask |= XCB_CONFIG_WINDOW_Y;
7329 wc[i++] = e->y;
7330 }
7331 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
7332 mask |= XCB_CONFIG_WINDOW_WIDTH;
7333 wc[i++] = e->width;
7334 }
7335 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
7336 mask |= XCB_CONFIG_WINDOW_HEIGHT;
7337 wc[i++] = e->height;
7338 }
7339 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
7340 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
7341 wc[i++] = e->border_width;
7342 }
7343 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
7344 mask |= XCB_CONFIG_WINDOW_SIBLING;
7345 wc[i++] = e->sibling;
7346 }
7347 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
7348 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
7349 wc[i++] = e->stack_mode;
7350 }
7351
7352 if (mask != 0) {
7353 xcb_configure_window(conn, e->window, mask, wc);
7354 xcb_flush(conn);
7355 }
7356 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
7357 !(win->ewmh_flags & EWMH_F_FULLSCREEN)) {
7358 if (win->ws->r)
7359 r = win->ws->r;
7360 else if (win->ws->old_r)
7361 r = win->ws->old_r;
7362
7363 /* windows are centered unless ANYWHERE quirk is set. */
7364 if (win->quirks & SWM_Q_ANYWHERE) {
7365 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
7366 win->g_float.x = e->x;
7367 if (r)
7368 win->g_float.x -= X(r);
7369 }
7370
7371 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
7372 win->g_float.y = e->y;
7373 if (r)
7374 win->g_float.y -= Y(r);
7375 }
7376 }
7377
7378 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
7379 win->g_float.w = e->width;
7380
7381 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
7382 win->g_float.h = e->height;
7383
7384 win->g_floatvalid = 1;
7385
7386 if (win->floating && r) {
7387 WIDTH(win) = win->g_float.w;
7388 HEIGHT(win) = win->g_float.h;
7389
7390 stack_floater(win, win->ws->r);
7391
7392 focus_flush();
7393 } else {
7394 config_win(win, e);
7395 xcb_flush(conn);
7396 }
7397 } else {
7398 config_win(win, e);
7399 xcb_flush(conn);
7400 }
7401
7402 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
7403 }
7404
7405 void
7406 configurenotify(xcb_configure_notify_event_t *e)
7407 {
7408 struct ws_win *win;
7409
7410 DNPRINTF(SWM_D_EVENT, "configurenotify: win 0x%x, event win: 0x%x, "
7411 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
7412 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
7413 e->width, e->height, e->border_width, e->above_sibling,
7414 YESNO(e->override_redirect));
7415
7416 win = find_window(e->window);
7417 if (win) {
7418 xcb_icccm_get_wm_normal_hints_reply(conn,
7419 xcb_icccm_get_wm_normal_hints(conn, win->id),
7420 &win->sh, NULL);
7421 adjust_font(win);
7422 if (font_adjusted) {
7423 stack();
7424 xcb_flush(conn);
7425 }
7426 }
7427 }
7428
7429 void
7430 destroynotify(xcb_destroy_notify_event_t *e)
7431 {
7432 struct ws_win *win;
7433
7434 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
7435
7436 if ((win = find_window(e->window)) == NULL) {
7437 if ((win = find_unmanaged_window(e->window)) == NULL)
7438 return;
7439 free_window(win);
7440 return;
7441 }
7442
7443 if (focus_mode != SWM_FOCUS_FOLLOW) {
7444 /* If we were focused, make sure we focus on something else. */
7445 if (win == win->ws->focus)
7446 win->ws->focus_pending = get_focus_prev(win);
7447 }
7448
7449 unmanage_window(win);
7450 stack();
7451
7452 if (focus_mode != SWM_FOCUS_FOLLOW) {
7453 if (win->ws->focus_pending) {
7454 focus_win(win->ws->focus_pending);
7455 win->ws->focus_pending = NULL;
7456 }
7457 }
7458
7459 free_window(win);
7460
7461 focus_flush();
7462 }
7463
7464 #ifdef SWM_DEBUG
7465 char *
7466 get_notify_detail_label(uint8_t detail)
7467 {
7468 char *label;
7469
7470 switch (detail) {
7471 case XCB_NOTIFY_DETAIL_ANCESTOR:
7472 label = "Ancestor";
7473 break;
7474 case XCB_NOTIFY_DETAIL_VIRTUAL:
7475 label = "Virtual";
7476 break;
7477 case XCB_NOTIFY_DETAIL_INFERIOR:
7478 label = "Inferior";
7479 break;
7480 case XCB_NOTIFY_DETAIL_NONLINEAR:
7481 label = "Nonlinear";
7482 break;
7483 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
7484 label = "NonlinearVirtual";
7485 break;
7486 case XCB_NOTIFY_DETAIL_POINTER:
7487 label = "Pointer";
7488 break;
7489 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
7490 label = "PointerRoot";
7491 break;
7492 case XCB_NOTIFY_DETAIL_NONE:
7493 label = "None";
7494 break;
7495 default:
7496 label = "Unknown";
7497 }
7498
7499 return label;
7500 }
7501
7502 char *
7503 get_notify_mode_label(uint8_t mode)
7504 {
7505 char *label;
7506
7507 switch (mode) {
7508 case XCB_NOTIFY_MODE_NORMAL:
7509 label = "Normal";
7510 break;
7511 case XCB_NOTIFY_MODE_GRAB:
7512 label = "Grab";
7513 break;
7514 case XCB_NOTIFY_MODE_UNGRAB:
7515 label = "Ungrab";
7516 break;
7517 case XCB_NOTIFY_MODE_WHILE_GRABBED:
7518 label = "WhileGrabbed";
7519 break;
7520 default:
7521 label = "Unknown";
7522 }
7523
7524 return label;
7525 }
7526 #endif
7527
7528 void
7529 enternotify(xcb_enter_notify_event_t *e)
7530 {
7531 struct ws_win *win;
7532 struct swm_region *old_r, *r;
7533
7534 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): 0x%x "
7535 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7536 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7537 e->time, e->event, e->event_x, e->event_y,
7538 get_notify_mode_label(e->mode), e->mode,
7539 get_notify_detail_label(e->detail), e->detail,
7540 e->root, e->root_x, e->root_y, e->child,
7541 YESNO(e->same_screen_focus), e->state);
7542
7543 if (focus_mode == SWM_FOCUS_MANUAL &&
7544 e->mode == XCB_NOTIFY_MODE_NORMAL) {
7545 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
7546 return;
7547 }
7548
7549 last_event_time = e->time;
7550
7551 if ((win = find_window(e->event)) == NULL) {
7552 if (e->event == e->root) {
7553 /* If no windows on pointer region, then focus root. */
7554 r = root_to_region(e->root, SWM_CK_POINTER);
7555 if (TAILQ_EMPTY(&r->ws->winlist)) {
7556 old_r = root_to_region(e->root, SWM_CK_FOCUS);
7557 if (old_r && old_r != r)
7558 unfocus_win(old_r->ws->focus);
7559
7560 xcb_set_input_focus(conn,
7561 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
7562
7563 /* Clear bar since empty. */
7564 bar_update();
7565
7566 focus_flush();
7567 }
7568 } else {
7569 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
7570 "ignoring\n");
7571 }
7572 return;
7573 }
7574
7575 focus_win(get_focus_magic(win));
7576
7577 xcb_flush(conn);
7578 }
7579
7580 #ifdef SWM_DEBUG
7581 void
7582 leavenotify(xcb_leave_notify_event_t *e)
7583 {
7584 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): 0x%x "
7585 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
7586 "child: 0x%x, same_screen_focus: %s, state: %d\n",
7587 e->time, e->event, e->event_x, e->event_y,
7588 get_notify_mode_label(e->mode), e->mode,
7589 get_notify_detail_label(e->detail), e->detail,
7590 e->root, e->root_x, e->root_y, e->child,
7591 YESNO(e->same_screen_focus), e->state);
7592 }
7593 #endif
7594
7595 /* lets us use one switch statement for arbitrary mode/detail combinations */
7596 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7597
7598 void
7599 mapnotify(xcb_map_notify_event_t *e)
7600 {
7601 struct ws_win *win;
7602
7603 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
7604
7605 if ((win = find_window(e->window)) == NULL)
7606 win = manage_window(e->window, 1);
7607
7608 win->mapped = 1;
7609 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
7610
7611 if (focus_mode != SWM_FOCUS_FOLLOW) {
7612 if (win->ws->focus_pending == win) {
7613 focus_win(win);
7614 win->ws->focus_pending = NULL;
7615 focus_flush();
7616 }
7617 }
7618
7619 xcb_flush(conn);
7620 }
7621
7622 void
7623 mappingnotify(xcb_mapping_notify_event_t *e)
7624 {
7625 xcb_refresh_keyboard_mapping(syms, e);
7626
7627 if (e->request == XCB_MAPPING_KEYBOARD)
7628 grabkeys();
7629 }
7630
7631 void
7632 maprequest(xcb_map_request_event_t *e)
7633 {
7634 struct ws_win *win;
7635 xcb_get_window_attributes_reply_t *war;
7636
7637 DNPRINTF(SWM_D_EVENT, "maprequest: win 0x%x\n",
7638 e->window);
7639
7640 war = xcb_get_window_attributes_reply(conn,
7641 xcb_get_window_attributes(conn, e->window),
7642 NULL);
7643 if (war == NULL) {
7644 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
7645 goto out;
7646 }
7647
7648 if (war->override_redirect) {
7649 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
7650 "skipping.\n");
7651 goto out;
7652 }
7653
7654 win = manage_window(e->window,
7655 (war->map_state == XCB_MAP_STATE_VIEWABLE));
7656
7657 /* All windows need to be mapped if they are in the current workspace.*/
7658 if (win->ws->r)
7659 stack();
7660
7661 /* The new window should get focus. */
7662 if (focus_mode != SWM_FOCUS_FOLLOW)
7663 win->ws->focus_pending = get_focus_magic(win);
7664
7665 /* Ignore EnterNotify to handle the mapnotify without interference. */
7666 if (focus_mode == SWM_FOCUS_DEFAULT)
7667 event_drain(XCB_ENTER_NOTIFY);
7668 out:
7669 free(war);
7670 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
7671 }
7672
7673 #ifdef SWM_DEBUG
7674 char *
7675 get_atom_name(xcb_atom_t atom)
7676 {
7677 char *name = NULL;
7678 #if 0
7679 /*
7680 * This should be disabled during most debugging since
7681 * xcb_get_* causes an xcb_flush.
7682 */
7683 size_t len;
7684 xcb_get_atom_name_reply_t *r;
7685
7686 r = xcb_get_atom_name_reply(conn,
7687 xcb_get_atom_name(conn, atom),
7688 NULL);
7689 if (r) {
7690 len = xcb_get_atom_name_name_length(r);
7691 if (len > 0) {
7692 name = malloc(len + 1);
7693 if (name) {
7694 memcpy(name, xcb_get_atom_name_name(r), len);
7695 name[len] = '\0';
7696 }
7697 }
7698 free(r);
7699 }
7700 #else
7701 (void)atom;
7702 #endif
7703 return (name);
7704 }
7705 #endif
7706
7707 void
7708 propertynotify(xcb_property_notify_event_t *e)
7709 {
7710 struct ws_win *win;
7711 #ifdef SWM_DEBUG
7712 char *name;
7713
7714 name = get_atom_name(e->atom);
7715 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u), "
7716 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
7717 e->state);
7718 free(name);
7719 #endif
7720 win = find_window(e->window);
7721 if (win == NULL)
7722 return;
7723
7724 last_event_time = e->time;
7725
7726 if (e->atom == a_swm_iconic) {
7727 if (e->state == XCB_PROPERTY_NEW_VALUE) {
7728 if (focus_mode != SWM_FOCUS_FOLLOW)
7729 win->ws->focus_pending = get_focus_prev(win);
7730
7731 unfocus_win(win);
7732 unmap_window(win);
7733
7734 if (win->ws->r) {
7735 stack();
7736 if (focus_mode != SWM_FOCUS_FOLLOW) {
7737 focus_win(win->ws->focus_pending);
7738 win->ws->focus_pending = NULL;
7739 }
7740 focus_flush();
7741 }
7742 } else if (e->state == XCB_PROPERTY_DELETE) {
7743 /* The window is no longer iconic, restack ws. */
7744 if (focus_mode != SWM_FOCUS_FOLLOW)
7745 win->ws->focus_pending = get_focus_magic(win);
7746
7747 stack();
7748
7749 /* Flush EnterNotify for mapnotify, if needed. */
7750 focus_flush();
7751 }
7752 } else if (e->atom == a_state) {
7753 /* State just changed, make sure it gets focused if mapped. */
7754 if (e->state == XCB_PROPERTY_NEW_VALUE) {
7755 if (focus_mode != SWM_FOCUS_FOLLOW) {
7756 if (win->mapped &&
7757 win->ws->focus_pending == win) {
7758 focus_win(win->ws->focus_pending);
7759 win->ws->focus_pending = NULL;
7760 }
7761 }
7762 }
7763 } else if (e->atom == XCB_ATOM_WM_CLASS ||
7764 e->atom == XCB_ATOM_WM_NAME) {
7765 bar_update();
7766 }
7767
7768 xcb_flush(conn);
7769 }
7770
7771 void
7772 unmapnotify(xcb_unmap_notify_event_t *e)
7773 {
7774 struct ws_win *win;
7775 struct workspace *ws;
7776
7777 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
7778
7779 /* If we aren't managing the window, then ignore. */
7780 win = find_window(e->window);
7781 if (win == NULL)
7782 return;
7783
7784 ws = win->ws;
7785
7786 if (getstate(e->window) != XCB_ICCCM_WM_STATE_ICONIC)
7787 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
7788
7789 if (win->mapped) {
7790 /* window unmapped itself */
7791 /* do unmap/unfocus/restack and unmanage */
7792 win->mapped = 0;
7793
7794 /* If win was focused, make sure to focus on something else. */
7795 if (win == ws->focus) {
7796 if (focus_mode != SWM_FOCUS_FOLLOW) {
7797 ws->focus_pending = get_focus_prev(win);
7798 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
7799 "focus_pending: 0x%x\n",
7800 WINID(ws->focus_pending));
7801 }
7802
7803 unfocus_win(win);
7804 }
7805
7806 unmanage_window(win);
7807
7808 if (ws->r)
7809 stack();
7810
7811 if (focus_mode == SWM_FOCUS_FOLLOW) {
7812 if (ws->r)
7813 focus_win(get_pointer_win(ws->r->s->root));
7814 } else {
7815 if (ws->focus_pending) {
7816 focus_win(ws->focus_pending);
7817 ws->focus_pending = NULL;
7818 }
7819 }
7820 }
7821
7822 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL)
7823 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
7824
7825 focus_flush();
7826 }
7827
7828 #if 0
7829 void
7830 visibilitynotify(xcb_visibility_notify_event_t *e)
7831 {
7832 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7833 e->window);
7834 }
7835 #endif
7836
7837 void
7838 clientmessage(xcb_client_message_event_t *e)
7839 {
7840 struct ws_win *win;
7841 xcb_map_request_event_t mre;
7842 #ifdef SWM_DEBUG
7843 char *name;
7844
7845 name = get_atom_name(e->type);
7846 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7847 e->window, name, e->type);
7848 free(name);
7849 #endif
7850 win = find_window(e->window);
7851
7852 if (win == NULL) {
7853 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7854 /* Manage the window with maprequest. */
7855 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
7856 "unmanaged window.\n");
7857 mre.window = e->window;
7858 maprequest(&mre);
7859 }
7860 return;
7861 }
7862
7863 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
7864 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
7865 focus_win(win);
7866 }
7867 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
7868 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
7869 if (win->can_delete)
7870 client_msg(win, a_delete, 0);
7871 else
7872 xcb_kill_client(conn, win->id);
7873 }
7874 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
7875 DNPRINTF(SWM_D_EVENT,
7876 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7877 if (win->floating) {
7878 if (e->data.data32[0] & (1<<8)) /* x */
7879 X(win) = e->data.data32[1];
7880 if (e->data.data32[0] & (1<<9)) /* y */
7881 Y(win) = e->data.data32[2];
7882 if (e->data.data32[0] & (1<<10)) /* width */
7883 WIDTH(win) = e->data.data32[3];
7884 if (e->data.data32[0] & (1<<11)) /* height */
7885 HEIGHT(win) = e->data.data32[4];
7886
7887 update_window(win);
7888 }
7889 else {
7890 /* TODO: Change stack sizes */
7891 /* notify no change was made. */
7892 config_win(win, NULL);
7893 }
7894 }
7895 if (e->type == ewmh[_NET_WM_STATE].atom) {
7896 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
7897 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
7898 if (e->data.data32[2])
7899 ewmh_update_win_state(win, e->data.data32[2],
7900 e->data.data32[0]);
7901
7902 stack();
7903 }
7904
7905 focus_flush();
7906 }
7907
7908 void
7909 check_conn(void)
7910 {
7911 int errcode = xcb_connection_has_error(conn);
7912 #ifdef XCB_CONN_ERROR
7913 char *s;
7914 switch (errcode) {
7915 case XCB_CONN_ERROR:
7916 s = "Socket error, pipe error or other stream error.";
7917 break;
7918 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
7919 s = "Extension not supported.";
7920 break;
7921 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
7922 s = "Insufficient memory.";
7923 break;
7924 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
7925 s = "Request length was exceeded.";
7926 break;
7927 case XCB_CONN_CLOSED_PARSE_ERR:
7928 s = "Error parsing display string.";
7929 break;
7930 default:
7931 s = "Unknown error.";
7932 }
7933 if (errcode)
7934 errx(errcode, "X CONNECTION ERROR: %s", s);
7935 #else
7936 if (errcode)
7937 errx(errcode, "X CONNECTION ERROR");
7938 #endif
7939 }
7940
7941 int
7942 enable_wm(void)
7943 {
7944 int num_screens, i;
7945 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
7946 XCB_EVENT_MASK_ENTER_WINDOW;
7947 xcb_screen_t *sc;
7948 xcb_void_cookie_t wac;
7949 xcb_generic_error_t *error;
7950
7951 /* this causes an error if some other window manager is running */
7952 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
7953 for (i = 0; i < num_screens; i++) {
7954 if ((sc = get_screen(i)) == NULL)
7955 errx(1, "ERROR: can't get screen %d.", i);
7956 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
7957 i, sc->root);
7958 wac = xcb_change_window_attributes_checked(conn, sc->root,
7959 XCB_CW_EVENT_MASK, &val);
7960 if ((error = xcb_request_check(conn, wac))) {
7961 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
7962 error->error_code);
7963 free(error);
7964 return 1;
7965 }
7966
7967 /* click to focus on empty region */
7968 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
7969 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
7970 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
7971 }
7972
7973 return 0;
7974 }
7975
7976 void
7977 new_region(struct swm_screen *s, int x, int y, int w, int h)
7978 {
7979 struct swm_region *r, *n;
7980 struct workspace *ws = NULL;
7981 int i;
7982
7983 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
7984 s->idx, w, h, x, y);
7985
7986 /* remove any conflicting regions */
7987 n = TAILQ_FIRST(&s->rl);
7988 while (n) {
7989 r = n;
7990 n = TAILQ_NEXT(r, entry);
7991 if (X(r) < (x + w) &&
7992 (X(r) + WIDTH(r)) > x &&
7993 Y(r) < (y + h) &&
7994 (Y(r) + HEIGHT(r)) > y) {
7995 if (r->ws->r != NULL)
7996 r->ws->old_r = r->ws->r;
7997 r->ws->r = NULL;
7998 bar_cleanup(r);
7999 TAILQ_REMOVE(&s->rl, r, entry);
8000 TAILQ_INSERT_TAIL(&s->orl, r, entry);
8001 }
8002 }
8003
8004 /* search old regions for one to reuse */
8005
8006 /* size + location match */
8007 TAILQ_FOREACH(r, &s->orl, entry)
8008 if (X(r) == x && Y(r) == y &&
8009 HEIGHT(r) == h && WIDTH(r) == w)
8010 break;
8011
8012 /* size match */
8013 TAILQ_FOREACH(r, &s->orl, entry)
8014 if (HEIGHT(r) == h && WIDTH(r) == w)
8015 break;
8016
8017 if (r != NULL) {
8018 TAILQ_REMOVE(&s->orl, r, entry);
8019 /* try to use old region's workspace */
8020 if (r->ws->r == NULL)
8021 ws = r->ws;
8022 } else
8023 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
8024 err(1, "new_region: calloc: failed to allocate memory "
8025 "for screen");
8026
8027 /* if we don't have a workspace already, find one */
8028 if (ws == NULL) {
8029 for (i = 0; i < workspace_limit; i++)
8030 if (s->ws[i].r == NULL) {
8031 ws = &s->ws[i];
8032 break;
8033 }
8034 }
8035
8036 if (ws == NULL)
8037 errx(1, "new_region: no free workspaces");
8038
8039 X(r) = x;
8040 Y(r) = y;
8041 WIDTH(r) = w;
8042 HEIGHT(r) = h;
8043 r->s = s;
8044 r->ws = ws;
8045 r->ws_prior = NULL;
8046 ws->r = r;
8047 outputs++;
8048 TAILQ_INSERT_TAIL(&s->rl, r, entry);
8049 }
8050
8051 void
8052 scan_xrandr(int i)
8053 {
8054 #ifdef SWM_XRR_HAS_CRTC
8055 int c;
8056 int ncrtc = 0;
8057 #endif /* SWM_XRR_HAS_CRTC */
8058 struct swm_region *r;
8059 int num_screens;
8060 xcb_randr_get_screen_resources_current_cookie_t src;
8061 xcb_randr_get_screen_resources_current_reply_t *srr;
8062 xcb_randr_get_crtc_info_cookie_t cic;
8063 xcb_randr_get_crtc_info_reply_t *cir = NULL;
8064 xcb_randr_crtc_t *crtc;
8065 xcb_screen_t *screen;
8066
8067 if ((screen = get_screen(i)) == NULL)
8068 errx(1, "ERROR: can't get screen %d.", i);
8069
8070 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8071 if (i >= num_screens)
8072 errx(1, "scan_xrandr: invalid screen");
8073
8074 /* remove any old regions */
8075 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
8076 r->ws->old_r = r->ws->r = NULL;
8077 bar_cleanup(r);
8078 TAILQ_REMOVE(&screens[i].rl, r, entry);
8079 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
8080 }
8081 outputs = 0;
8082
8083 /* map virtual screens onto physical screens */
8084 #ifdef SWM_XRR_HAS_CRTC
8085 if (xrandr_support) {
8086 src = xcb_randr_get_screen_resources_current(conn,
8087 screens[i].root);
8088 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
8089 NULL);
8090 if (srr == NULL) {
8091 new_region(&screens[i], 0, 0,
8092 screen->width_in_pixels,
8093 screen->height_in_pixels);
8094 return;
8095 } else
8096 ncrtc = srr->num_crtcs;
8097
8098 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
8099 for (c = 0; c < ncrtc; c++) {
8100 cic = xcb_randr_get_crtc_info(conn, crtc[c],
8101 XCB_CURRENT_TIME);
8102 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
8103 if (cir == NULL)
8104 continue;
8105 if (cir->num_outputs == 0) {
8106 free(cir);
8107 continue;
8108 }
8109
8110 if (cir->mode == 0)
8111 new_region(&screens[i], 0, 0,
8112 screen->width_in_pixels,
8113 screen->height_in_pixels);
8114 else
8115 new_region(&screens[i],
8116 cir->x, cir->y, cir->width, cir->height);
8117 free(cir);
8118 }
8119 free(srr);
8120 } else
8121 #endif /* SWM_XRR_HAS_CRTC */
8122 {
8123 new_region(&screens[i], 0, 0, screen->width_in_pixels,
8124 screen->height_in_pixels);
8125 }
8126 }
8127
8128 void
8129 screenchange(xcb_randr_screen_change_notify_event_t *e)
8130 {
8131 struct swm_region *r;
8132 int i, num_screens;
8133
8134 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
8135
8136 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8137 /* silly event doesn't include the screen index */
8138 for (i = 0; i < num_screens; i++)
8139 if (screens[i].root == e->root)
8140 break;
8141 if (i >= num_screens)
8142 errx(1, "screenchange: screen not found");
8143
8144 /* brute force for now, just re-enumerate the regions */
8145 scan_xrandr(i);
8146
8147 #ifdef SWM_DEBUG
8148 print_win_geom(e->root);
8149 #endif
8150 /* add bars to all regions */
8151 for (i = 0; i < num_screens; i++)
8152 TAILQ_FOREACH(r, &screens[i].rl, entry)
8153 bar_setup(r);
8154 stack();
8155 }
8156
8157 void
8158 grab_windows(void)
8159 {
8160 xcb_window_t *wins = NULL;
8161 int no;
8162 int i, j, num_screens;
8163 uint16_t state, manage, mapped;
8164
8165 xcb_query_tree_cookie_t qtc;
8166 xcb_query_tree_reply_t *qtr;
8167 xcb_get_window_attributes_cookie_t c;
8168 xcb_get_window_attributes_reply_t *r;
8169 xcb_get_property_cookie_t pc;
8170
8171 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
8172 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8173 for (i = 0; i < num_screens; i++) {
8174 qtc = xcb_query_tree(conn, screens[i].root);
8175 qtr = xcb_query_tree_reply(conn, qtc, NULL);
8176 if (!qtr)
8177 continue;
8178 wins = xcb_query_tree_children(qtr);
8179 no = xcb_query_tree_children_length(qtr);
8180 /* attach windows to a region */
8181 /* normal windows */
8182 for (j = 0; j < no; j++) {
8183 c = xcb_get_window_attributes(conn, wins[j]);
8184 r = xcb_get_window_attributes_reply(conn, c, NULL);
8185 if (!r)
8186 continue;
8187 if (r->override_redirect) {
8188 free(r);
8189 continue;
8190 }
8191
8192 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
8193 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
8194 &wins[j], NULL)) {
8195 free(r);
8196 continue;
8197 }
8198
8199 state = getstate(wins[j]);
8200 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
8201 mapped = r->map_state != XCB_MAP_STATE_UNMAPPED;
8202 if (mapped || manage)
8203 manage_window(wins[j], mapped);
8204 free(r);
8205 }
8206 /* transient windows */
8207 for (j = 0; j < no; j++) {
8208 c = xcb_get_window_attributes(conn, wins[j]);
8209 r = xcb_get_window_attributes_reply(conn, c, NULL);
8210 if (!r)
8211 continue;
8212 if (r->override_redirect) {
8213 free(r);
8214 continue;
8215 }
8216 free(r);
8217
8218 state = getstate(wins[j]);
8219 manage = state == XCB_ICCCM_WM_STATE_ICONIC;
8220 mapped = r->map_state != XCB_MAP_STATE_UNMAPPED;
8221 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
8222 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
8223 &wins[j], NULL) && manage)
8224 manage_window(wins[j], mapped);
8225 }
8226 free(qtr);
8227 }
8228 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
8229 }
8230
8231 void
8232 setup_screens(void)
8233 {
8234 int i, j, k, num_screens;
8235 struct workspace *ws;
8236 uint32_t gcv[1], wa[1];
8237 const xcb_query_extension_reply_t *qep;
8238 xcb_screen_t *screen;
8239 xcb_cursor_t cursor;
8240 xcb_font_t cursor_font;
8241 xcb_randr_query_version_cookie_t c;
8242 xcb_randr_query_version_reply_t *r;
8243
8244 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8245 if ((screens = calloc(num_screens,
8246 sizeof(struct swm_screen))) == NULL)
8247 err(1, "setup_screens: calloc: failed to allocate memory for "
8248 "screens");
8249
8250 /* initial Xrandr setup */
8251 xrandr_support = 0;
8252 qep = xcb_get_extension_data(conn, &xcb_randr_id);
8253 if (qep->present) {
8254 c = xcb_randr_query_version(conn, 1, 1);
8255 r = xcb_randr_query_version_reply(conn, c, NULL);
8256 if (r) {
8257 if (r->major_version >= 1) {
8258 xrandr_support = 1;
8259 xrandr_eventbase = qep->first_event;
8260 }
8261 free(r);
8262 }
8263 }
8264
8265 cursor_font = xcb_generate_id(conn);
8266 xcb_open_font(conn, cursor_font, strlen("cursor"), "cursor");
8267
8268 cursor = xcb_generate_id(conn);
8269 xcb_create_glyph_cursor(conn, cursor, cursor_font, cursor_font,
8270 XC_left_ptr, XC_left_ptr + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
8271 wa[0] = cursor;
8272
8273 /* map physical screens */
8274 for (i = 0; i < num_screens; i++) {
8275 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
8276 screens[i].idx = i;
8277 TAILQ_INIT(&screens[i].rl);
8278 TAILQ_INIT(&screens[i].orl);
8279 if ((screen = get_screen(i)) == NULL)
8280 errx(1, "ERROR: can't get screen %d.", i);
8281 screens[i].root = screen->root;
8282
8283 /* set default colors */
8284 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
8285 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
8286 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
8287 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
8288 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
8289
8290 /* create graphics context on screen */
8291 screens[i].bar_gc = xcb_generate_id(conn);
8292 gcv[0] = 0;
8293 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
8294 XCB_GC_GRAPHICS_EXPOSURES, gcv);
8295
8296 /* set default cursor */
8297 xcb_change_window_attributes(conn, screens[i].root,
8298 XCB_CW_CURSOR, wa);
8299
8300 /* init all workspaces */
8301 /* XXX these should be dynamically allocated too */
8302 for (j = 0; j < SWM_WS_MAX; j++) {
8303 ws = &screens[i].ws[j];
8304 ws->idx = j;
8305 ws->name = NULL;
8306 ws->focus = NULL;
8307 ws->focus_prev = NULL;
8308 ws->focus_pending = NULL;
8309 ws->r = NULL;
8310 ws->old_r = NULL;
8311 TAILQ_INIT(&ws->winlist);
8312 TAILQ_INIT(&ws->unmanagedlist);
8313
8314 for (k = 0; layouts[k].l_stack != NULL; k++)
8315 if (layouts[k].l_config != NULL)
8316 layouts[k].l_config(ws,
8317 SWM_ARG_ID_STACKINIT);
8318 ws->cur_layout = &layouts[0];
8319 ws->cur_layout->l_string(ws);
8320 }
8321
8322 scan_xrandr(i);
8323
8324 if (xrandr_support)
8325 xcb_randr_select_input(conn, screens[i].root,
8326 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
8327 }
8328 xcb_free_cursor(conn, cursor);
8329 xcb_close_font(conn, cursor_font);
8330 }
8331
8332 void
8333 setup_globals(void)
8334 {
8335 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
8336 err(1, "setup_globals: strdup: failed to allocate memory.");
8337
8338 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
8339 err(1, "setup_globals: strdup: failed to allocate memory.");
8340
8341 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
8342 errx(1, "unable to allocate key symbols");
8343
8344 a_state = get_atom_from_string("WM_STATE");
8345 a_prot = get_atom_from_string("WM_PROTOCOLS");
8346 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
8347 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
8348 a_wmname = get_atom_from_string("WM_NAME");
8349 a_netwmname = get_atom_from_string("_NET_WM_NAME");
8350 a_utf8_string = get_atom_from_string("UTF8_STRING");
8351 a_string = get_atom_from_string("STRING");
8352 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
8353 a_swm_ws = get_atom_from_string("_SWM_WS");
8354 }
8355
8356 void
8357 workaround(void)
8358 {
8359 int i, num_screens;
8360 xcb_atom_t netwmcheck;
8361 xcb_window_t root, win;
8362
8363 /* work around sun jdk bugs, code from wmname */
8364 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
8365
8366 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8367 for (i = 0; i < num_screens; i++) {
8368 root = screens[i].root;
8369
8370 win = xcb_generate_id(conn);
8371 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
8372 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
8373 XCB_COPY_FROM_PARENT, 0, NULL);
8374
8375 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
8376 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
8377 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
8378 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
8379 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
8380 a_netwmname, a_utf8_string, 8, strlen("LG3D"), "LG3D");
8381 }
8382 }
8383
8384 void
8385 shutdown_cleanup(void)
8386 {
8387 int i, num_screens;
8388
8389 /* disable alarm because the following code may not be interrupted */
8390 alarm(0);
8391 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
8392 err(1, "can't disable alarm");
8393
8394 bar_extra_stop();
8395 bar_extra = 1;
8396 unmap_all();
8397
8398 teardown_ewmh();
8399
8400 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8401 for (i = 0; i < num_screens; ++i) {
8402 if (screens[i].bar_gc != 0)
8403 xcb_free_gc(conn, screens[i].bar_gc);
8404 if (!bar_font_legacy)
8405 XftColorFree(display, DefaultVisual(display, i),
8406 DefaultColormap(display, i), &bar_font_color);
8407 }
8408
8409 if (bar_font_legacy)
8410 XFreeFontSet(display, bar_fs);
8411 else {
8412 XftFontClose(display, bar_font);
8413 }
8414
8415 xcb_key_symbols_free(syms);
8416 xcb_flush(conn);
8417 xcb_disconnect(conn);
8418 }
8419
8420 void
8421 event_error(xcb_generic_error_t *e)
8422 {
8423 (void)e;
8424
8425 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
8426 "resource_id: %u, minor_code: %u\n",
8427 xcb_event_get_error_label(e->error_code), e->error_code,
8428 xcb_event_get_request_label(e->major_code), e->major_code,
8429 e->sequence, e->resource_id, e->minor_code);
8430 }
8431
8432 void
8433 event_handle(xcb_generic_event_t *evt)
8434 {
8435 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
8436
8437 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
8438 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
8439 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
8440
8441 switch (type) {
8442 #define EVENT(type, callback) case type: callback((void *)evt); return
8443 EVENT(0, event_error);
8444 EVENT(XCB_BUTTON_PRESS, buttonpress);
8445 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
8446 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
8447 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
8448 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
8449 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
8450 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
8451 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
8452 /*EVENT(XCB_CREATE_NOTIFY, );*/
8453 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
8454 EVENT(XCB_ENTER_NOTIFY, enternotify);
8455 EVENT(XCB_EXPOSE, expose);
8456 #ifdef SWM_DEBUG
8457 EVENT(XCB_FOCUS_IN, focusin);
8458 EVENT(XCB_FOCUS_OUT, focusout);
8459 #endif
8460 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
8461 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
8462 EVENT(XCB_KEY_PRESS, keypress);
8463 /*EVENT(XCB_KEY_RELEASE, keypress);*/
8464 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
8465 #ifdef SWM_DEBUG
8466 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
8467 #endif
8468 EVENT(XCB_MAP_NOTIFY, mapnotify);
8469 EVENT(XCB_MAP_REQUEST, maprequest);
8470 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
8471 /*EVENT(XCB_MOTION_NOTIFY, );*/
8472 /*EVENT(XCB_NO_EXPOSURE, );*/
8473 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
8474 /*EVENT(XCB_REPARENT_NOTIFY, );*/
8475 /*EVENT(XCB_RESIZE_REQUEST, );*/
8476 /*EVENT(XCB_SELECTION_CLEAR, );*/
8477 /*EVENT(XCB_SELECTION_NOTIFY, );*/
8478 /*EVENT(XCB_SELECTION_REQUEST, );*/
8479 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
8480 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
8481 #undef EVENT
8482 }
8483 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
8484 screenchange((void *)evt);
8485 }
8486
8487 int
8488 main(int argc, char *argv[])
8489 {
8490 struct swm_region *r, *rr;
8491 struct ws_win *winfocus = NULL;
8492 char conf[PATH_MAX], *cfile = NULL;
8493 struct stat sb;
8494 int xfd, i, num_screens;
8495 struct sigaction sact;
8496 xcb_generic_event_t *evt;
8497 struct timeval tv;
8498 fd_set rd;
8499
8500 /* suppress unused warning since var is needed */
8501 (void)argc;
8502
8503 time_started = time(NULL);
8504
8505 start_argv = argv;
8506 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
8507 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, ""))
8508 warnx("no locale support");
8509
8510 /* handle some signals */
8511 bzero(&sact, sizeof(sact));
8512 sigemptyset(&sact.sa_mask);
8513 sact.sa_flags = 0;
8514 sact.sa_handler = sighdlr;
8515 sigaction(SIGINT, &sact, NULL);
8516 sigaction(SIGQUIT, &sact, NULL);
8517 sigaction(SIGTERM, &sact, NULL);
8518 sigaction(SIGHUP, &sact, NULL);
8519
8520 sact.sa_handler = sighdlr;
8521 sact.sa_flags = SA_NOCLDSTOP;
8522 sigaction(SIGCHLD, &sact, NULL);
8523
8524 if (!(display = XOpenDisplay(0)))
8525 errx(1, "can not open display");
8526
8527 conn = XGetXCBConnection(display);
8528 if (xcb_connection_has_error(conn))
8529 errx(1, "can not get XCB connection");
8530
8531 XSetEventQueueOwner(display, XCBOwnsEventQueue);
8532
8533 xcb_prefetch_extension_data(conn, &xcb_randr_id);
8534 xfd = xcb_get_file_descriptor(conn);
8535
8536 /* look for local and global conf file */
8537 pwd = getpwuid(getuid());
8538 if (pwd == NULL)
8539 errx(1, "invalid user: %d", getuid());
8540
8541 xcb_grab_server(conn);
8542 xcb_aux_sync(conn);
8543
8544 /* flush all events */
8545 while ((evt = xcb_poll_for_event(conn))) {
8546 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
8547 event_handle(evt);
8548 free(evt);
8549 }
8550
8551 if (enable_wm() != 0)
8552 errx(1, "another window manager is currently running");
8553
8554 xcb_aux_sync(conn);
8555
8556 setup_globals();
8557 setup_screens();
8558 setup_keys();
8559 setup_quirks();
8560 setup_spawn();
8561
8562 /* load config */
8563 for (i = 0; ; i++) {
8564 conf[0] = '\0';
8565 switch (i) {
8566 case 0:
8567 /* ~ */
8568 snprintf(conf, sizeof conf, "%s/.%s",
8569 pwd->pw_dir, SWM_CONF_FILE);
8570 break;
8571 case 1:
8572 /* global */
8573 snprintf(conf, sizeof conf, "/etc/%s",
8574 SWM_CONF_FILE);
8575 break;
8576 case 2:
8577 /* ~ compat */
8578 snprintf(conf, sizeof conf, "%s/.%s",
8579 pwd->pw_dir, SWM_CONF_FILE_OLD);
8580 break;
8581 case 3:
8582 /* global compat */
8583 snprintf(conf, sizeof conf, "/etc/%s",
8584 SWM_CONF_FILE_OLD);
8585 break;
8586 default:
8587 goto noconfig;
8588 }
8589
8590 if (strlen(conf) && stat(conf, &sb) != -1)
8591 if (S_ISREG(sb.st_mode)) {
8592 cfile = conf;
8593 break;
8594 }
8595 }
8596 noconfig:
8597
8598 /* load conf (if any) */
8599 if (cfile)
8600 conf_load(cfile, SWM_CONF_DEFAULT);
8601
8602 setup_ewmh();
8603 /* set some values to work around bad programs */
8604 workaround();
8605 /* grab existing windows (before we build the bars) */
8606 grab_windows();
8607
8608 if (getenv("SWM_STARTED") == NULL)
8609 setenv("SWM_STARTED", "YES", 1);
8610
8611 /* setup all bars */
8612 num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
8613 for (i = 0; i < num_screens; i++)
8614 TAILQ_FOREACH(r, &screens[i].rl, entry) {
8615 if (winfocus == NULL)
8616 winfocus = TAILQ_FIRST(&r->ws->winlist);
8617 bar_setup(r);
8618 }
8619
8620 grabkeys();
8621 stack();
8622
8623 xcb_ungrab_server(conn);
8624 xcb_flush(conn);
8625
8626 while (running) {
8627 while ((evt = xcb_poll_for_event(conn))) {
8628 if (!running)
8629 goto done;
8630 event_handle(evt);
8631 free(evt);
8632 }
8633
8634 /* if we are being restarted go focus on first window */
8635 if (winfocus) {
8636 rr = winfocus->ws->r;
8637 if (rr == NULL) {
8638 /* not a visible window */
8639 winfocus = NULL;
8640 continue;
8641 }
8642
8643 focus_win(get_region_focus(rr));
8644 focus_flush();
8645 winfocus = NULL;
8646 continue;
8647 }
8648
8649 FD_ZERO(&rd);
8650 FD_SET(xfd, &rd);
8651 tv.tv_sec = 1;
8652 tv.tv_usec = 0;
8653 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
8654 if (errno != EINTR) {
8655 DNPRINTF(SWM_D_MISC, "select failed");
8656 }
8657 if (restart_wm)
8658 restart(NULL, NULL);
8659 if (search_resp)
8660 search_do_resp();
8661 if (!running)
8662 goto done;
8663 if (bar_alarm) {
8664 bar_alarm = 0;
8665 bar_update();
8666 }
8667 }
8668 done:
8669 shutdown_cleanup();
8670
8671 return (0);
8672 }