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