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