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