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