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