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