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