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