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