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