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