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