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