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