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