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