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