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