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