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