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