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