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