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