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