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