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