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