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