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