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