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>
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.
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.
25 * Much code and ideas taken from dwm under the following license:
26 * MIT/X Consortium License
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>
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:
45 * The above copyright notice and this permission notice shall be included in
46 * all copies or substantial portions of the Software.
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.
58 #include <sys/types.h>
62 #include <sys/queue.h>
63 #include <sys/param.h>
64 #include <sys/select.h>
65 #if defined(__linux__)
67 #elif defined(__OpenBSD__)
69 #elif defined(__FreeBSD__)
90 #include <X11/cursorfont.h>
91 #include <X11/extensions/Xrandr.h>
92 #include <X11/Xft/Xft.h>
93 #include <X11/Xlib-xcb.h>
94 #include <xcb/xcb_atom.h>
95 #include <xcb/xcb_aux.h>
96 #include <xcb/xcb_event.h>
97 #include <xcb/xcb_icccm.h>
98 #include <xcb/xcb_keysyms.h>
99 #include <xcb/xtest.h>
100 #include <xcb/randr.h>
108 #ifdef SPECTRWM_BUILDSTR
109 static const char *buildstr
= SPECTRWM_BUILDSTR
;
111 static const char *buildstr
= SPECTRWM_VERSION
;
115 # error XRandR versions less than 1.0 are not supported
120 #define SWM_XRR_HAS_CRTC
124 #if defined(__OpenBSD__)
125 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
126 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
127 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
128 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
129 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
130 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
131 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
132 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
133 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
134 #define xcb_icccm_get_wm_class xcb_get_wm_class
135 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
136 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
137 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
138 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
139 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
140 #define xcb_icccm_get_wm_name xcb_get_wm_name
141 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
142 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
143 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
144 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
145 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
146 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
147 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
148 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
149 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
150 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
155 #define DPRINTF(x...) do { \
157 fprintf(stderr, x); \
159 #define DNPRINTF(n,x...) do { \
160 if (swm_debug & n) { \
161 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
162 fprintf(stderr, x); \
165 #define SWM_D_MISC 0x0001
166 #define SWM_D_EVENT 0x0002
167 #define SWM_D_WS 0x0004
168 #define SWM_D_FOCUS 0x0008
169 #define SWM_D_MOVE 0x0010
170 #define SWM_D_STACK 0x0020
171 #define SWM_D_MOUSE 0x0040
172 #define SWM_D_PROP 0x0080
173 #define SWM_D_CLASS 0x0100
174 #define SWM_D_KEY 0x0200
175 #define SWM_D_QUIRK 0x0400
176 #define SWM_D_SPAWN 0x0800
177 #define SWM_D_EVENTQ 0x1000
178 #define SWM_D_CONF 0x2000
179 #define SWM_D_BAR 0x4000
180 #define SWM_D_INIT 0x8000
182 u_int32_t swm_debug
= 0
201 #define DPRINTF(x...)
202 #define DNPRINTF(n,x...)
205 /* convert 8-bit to 16-bit */
206 #define RGB_8_TO_16(col) ((col) << 8) + (col)
208 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
209 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
210 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
211 xrc.blue = RGB_8_TO_16((px) & 0xff); \
214 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
215 #define MODKEY Mod1Mask
216 #define CLEANMASK(mask) ((mask) & ~(numlockmask | LockMask))
217 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
218 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
219 #define SWM_PROPLEN (16)
220 #define SWM_FUNCNAME_LEN (32)
221 #define SWM_KEYS_LEN (255)
222 #define SWM_QUIRK_LEN (64)
223 #define X(r) (r)->g.x
224 #define Y(r) (r)->g.y
225 #define WIDTH(r) (r)->g.w
226 #define HEIGHT(r) (r)->g.h
227 #define BORDER(w) ((w)->bordered ? border_width : 0)
228 #define MAX_X(r) ((r)->g.x + (r)->g.w)
229 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
230 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
231 #define SH_MIN_W(w) (w)->sh.min_width
232 #define SH_MIN_H(w) (w)->sh.min_height
233 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
234 #define SH_MAX_W(w) (w)->sh.max_width
235 #define SH_MAX_H(w) (w)->sh.max_height
236 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
237 #define SH_INC_W(w) (w)->sh.width_inc
238 #define SH_INC_H(w) (w)->sh.height_inc
239 #define SWM_MAX_FONT_STEPS (3)
240 #define WINID(w) ((w) ? (w)->id : 0)
241 #define YESNO(x) ((x) ? "yes" : "no")
243 #define SWM_FOCUS_DEFAULT (0)
244 #define SWM_FOCUS_SYNERGY (1)
245 #define SWM_FOCUS_FOLLOW (2)
247 #define SWM_CONF_DEFAULT (0)
248 #define SWM_CONF_KEYMAPPING (1)
251 #define SWM_LIB "/usr/local/lib/libswmhack.so"
258 xcb_atom_t a_takefocus
;
260 xcb_atom_t a_netwmname
;
261 xcb_atom_t a_utf8_string
;
263 xcb_atom_t a_swm_iconic
;
265 volatile sig_atomic_t running
= 1;
266 volatile sig_atomic_t restart_wm
= 0;
268 /*int last_focus_event = FocusOut;*/
272 int xrandr_eventbase
;
273 unsigned int numlockmask
= 0;
276 xcb_connection_t
*conn
;
277 xcb_key_symbols_t
*syms
;
280 int cycle_visible
= 0;
282 int font_adjusted
= 0;
283 unsigned int mod_key
= MODKEY
;
286 struct swm_region
*search_r
;
287 int select_list_pipe
[2];
288 int select_resp_pipe
[2];
290 volatile sig_atomic_t search_resp
;
291 int search_resp_action
;
293 struct search_window
{
294 TAILQ_ENTRY(search_window
) entry
;
298 xcb_window_t indicator
;
300 TAILQ_HEAD(search_winlist
, search_window
);
301 struct search_winlist search_wl
;
306 SWM_SEARCH_UNICONIFY
,
307 SWM_SEARCH_NAME_WORKSPACE
,
308 SWM_SEARCH_SEARCH_WORKSPACE
,
309 SWM_SEARCH_SEARCH_WINDOW
312 #define SWM_STACK_TOP (0)
313 #define SWM_STACK_BOTTOM (1)
314 #define SWM_STACK_ABOVE (2)
315 #define SWM_STACK_BELOW (3)
318 double dialog_ratio
= 0.6;
320 #define SWM_BAR_MAX (256)
321 #define SWM_BAR_JUSTIFY_LEFT (0)
322 #define SWM_BAR_JUSTIFY_CENTER (1)
323 #define SWM_BAR_JUSTIFY_RIGHT (2)
324 #define SWM_BAR_OFFSET (4)
325 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
326 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
327 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
328 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
329 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
331 char *bar_argv
[] = { NULL
, NULL
};
333 unsigned char bar_ext
[SWM_BAR_MAX
];
334 char bar_vertext
[SWM_BAR_MAX
];
336 sig_atomic_t bar_alarm
= 0;
339 int bar_border_width
= 1;
340 int bar_at_bottom
= 0;
342 int bar_extra_running
= 0;
345 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
346 char *bar_format
= NULL
;
347 int stack_enabled
= 1;
348 int clock_enabled
= 1;
349 int urgent_enabled
= 0;
350 char *clock_format
= NULL
;
351 int title_name_enabled
= 0;
352 int title_class_enabled
= 0;
353 int window_name_enabled
= 0;
354 int focus_mode
= SWM_FOCUS_DEFAULT
;
355 int focus_close
= SWM_STACK_BELOW
;
356 int focus_close_wrap
= 1;
357 int focus_default
= SWM_STACK_TOP
;
358 int spawn_position
= SWM_STACK_TOP
;
359 int disable_border
= 0;
360 int border_width
= 1;
361 int verbose_layout
= 0;
366 XftColor bar_font_color
;
369 /* layout manager data */
370 struct swm_geometry
{
383 struct swm_geometry g
;
386 /* virtual "screens" */
388 TAILQ_ENTRY(swm_region
) entry
;
389 struct swm_geometry g
;
390 struct workspace
*ws
; /* current workspace on this region */
391 struct workspace
*ws_prior
; /* prior workspace on this region */
392 struct swm_screen
*s
; /* screen idx */
395 TAILQ_HEAD(swm_region_list
, swm_region
);
398 TAILQ_ENTRY(ws_win
) entry
;
400 xcb_window_t transient
;
401 struct ws_win
*child_trans
; /* transient child window */
402 struct swm_geometry g
; /* current geometry */
403 struct swm_geometry g_float
; /* region coordinates */
404 int g_floatvalid
; /* g_float geometry validity */
405 int floatmaxed
; /* whether maxed by max_stack */
410 unsigned int ewmh_flags
;
411 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
417 unsigned long quirks
;
418 struct workspace
*ws
; /* always valid */
419 struct swm_screen
*s
; /* always valid, never changes */
420 xcb_get_geometry_reply_t
*wa
;
422 xcb_icccm_get_wm_class_reply_t ch
;
423 xcb_icccm_wm_hints_t hints
;
425 TAILQ_HEAD(ws_win_list
, ws_win
);
429 TAILQ_ENTRY(pid_e
) entry
;
433 TAILQ_HEAD(pid_list
, pid_e
);
434 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
436 /* layout handlers */
438 void vertical_config(struct workspace
*, int);
439 void vertical_stack(struct workspace
*, struct swm_geometry
*);
440 void horizontal_config(struct workspace
*, int);
441 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
442 void max_stack(struct workspace
*, struct swm_geometry
*);
443 void plain_stacker(struct workspace
*);
444 void fancy_stacker(struct workspace
*);
447 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
448 void (*l_config
)(struct workspace
*, int);
450 #define SWM_L_FOCUSPREV (1<<0)
451 #define SWM_L_MAPONFOCUS (1<<1)
452 void (*l_string
)(struct workspace
*);
454 /* stack, configure */
455 { vertical_stack
, vertical_config
, 0, plain_stacker
},
456 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
458 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
459 { NULL
, NULL
, 0, NULL
},
462 /* position of max_stack mode in the layouts array, index into layouts! */
463 #define SWM_V_STACK (0)
464 #define SWM_H_STACK (1)
465 #define SWM_MAX_STACK (2)
467 #define SWM_H_SLICE (32)
468 #define SWM_V_SLICE (32)
470 /* define work spaces */
472 int idx
; /* workspace index */
473 char *name
; /* workspace name */
474 int always_raise
; /* raise windows on focus */
475 struct layout
*cur_layout
; /* current layout handlers */
476 struct ws_win
*focus
; /* may be NULL */
477 struct ws_win
*focus_prev
; /* may be NULL */
478 struct swm_region
*r
; /* may be NULL */
479 struct swm_region
*old_r
; /* may be NULL */
480 struct ws_win_list winlist
; /* list of windows in ws */
481 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
482 char stacker
[10]; /* display stacker and layout */
486 int horizontal_msize
;
488 int horizontal_stacks
;
499 SWM_S_COLOR_BAR_BORDER
,
500 SWM_S_COLOR_BAR_FONT
,
506 /* physical screen mapping */
507 #define SWM_WS_MAX (22) /* hard limit */
508 int workspace_limit
= 10; /* soft limit */
511 int idx
; /* screen index */
512 struct swm_region_list rl
; /* list of regions on this screen */
513 struct swm_region_list orl
; /* list of old regions */
515 struct workspace ws
[SWM_WS_MAX
];
521 } c
[SWM_S_COLOR_MAX
];
523 xcb_gcontext_t bar_gc
;
525 struct swm_screen
*screens
;
527 /* args to functions */
530 #define SWM_ARG_ID_FOCUSNEXT (0)
531 #define SWM_ARG_ID_FOCUSPREV (1)
532 #define SWM_ARG_ID_FOCUSMAIN (2)
533 #define SWM_ARG_ID_FOCUSCUR (4)
534 #define SWM_ARG_ID_SWAPNEXT (10)
535 #define SWM_ARG_ID_SWAPPREV (11)
536 #define SWM_ARG_ID_SWAPMAIN (12)
537 #define SWM_ARG_ID_MOVELAST (13)
538 #define SWM_ARG_ID_MASTERSHRINK (20)
539 #define SWM_ARG_ID_MASTERGROW (21)
540 #define SWM_ARG_ID_MASTERADD (22)
541 #define SWM_ARG_ID_MASTERDEL (23)
542 #define SWM_ARG_ID_FLIPLAYOUT (24)
543 #define SWM_ARG_ID_STACKRESET (30)
544 #define SWM_ARG_ID_STACKINIT (31)
545 #define SWM_ARG_ID_CYCLEWS_UP (40)
546 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
547 #define SWM_ARG_ID_CYCLESC_UP (42)
548 #define SWM_ARG_ID_CYCLESC_DOWN (43)
549 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
550 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
551 #define SWM_ARG_ID_STACKINC (50)
552 #define SWM_ARG_ID_STACKDEC (51)
553 #define SWM_ARG_ID_SS_ALL (60)
554 #define SWM_ARG_ID_SS_WINDOW (61)
555 #define SWM_ARG_ID_DONTCENTER (70)
556 #define SWM_ARG_ID_CENTER (71)
557 #define SWM_ARG_ID_KILLWINDOW (80)
558 #define SWM_ARG_ID_DELETEWINDOW (81)
559 #define SWM_ARG_ID_WIDTHGROW (90)
560 #define SWM_ARG_ID_WIDTHSHRINK (91)
561 #define SWM_ARG_ID_HEIGHTGROW (92)
562 #define SWM_ARG_ID_HEIGHTSHRINK (93)
563 #define SWM_ARG_ID_MOVEUP (100)
564 #define SWM_ARG_ID_MOVEDOWN (101)
565 #define SWM_ARG_ID_MOVELEFT (102)
566 #define SWM_ARG_ID_MOVERIGHT (103)
572 TAILQ_ENTRY(quirk
) entry
;
576 #define SWM_Q_FLOAT (1<<0) /* float this window */
577 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
578 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
579 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
580 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
581 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
583 TAILQ_HEAD(quirk_list
, quirk
);
584 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
587 * Supported EWMH hints should be added to
588 * both the enum and the ewmh array
593 _NET_MOVERESIZE_WINDOW
,
594 _NET_WM_ACTION_CLOSE
,
595 _NET_WM_ACTION_FULLSCREEN
,
597 _NET_WM_ACTION_RESIZE
,
598 _NET_WM_ALLOWED_ACTIONS
,
601 _NET_WM_STATE_FULLSCREEN
,
602 _NET_WM_STATE_HIDDEN
,
603 _NET_WM_STATE_MAXIMIZED_HORZ
,
604 _NET_WM_STATE_MAXIMIZED_VERT
,
605 _NET_WM_STATE_SKIP_PAGER
,
606 _NET_WM_STATE_SKIP_TASKBAR
,
608 _NET_WM_WINDOW_TYPE_DIALOG
,
609 _NET_WM_WINDOW_TYPE_DOCK
,
610 _NET_WM_WINDOW_TYPE_NORMAL
,
611 _NET_WM_WINDOW_TYPE_SPLASH
,
612 _NET_WM_WINDOW_TYPE_TOOLBAR
,
613 _NET_WM_WINDOW_TYPE_UTILITY
,
614 _SWM_WM_STATE_MANUAL
,
621 } ewmh
[SWM_EWMH_HINT_MAX
] = {
622 /* must be in same order as in the enum */
623 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
624 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
625 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
626 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
627 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
628 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
629 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
630 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
631 {"_NET_WM_STATE", XCB_ATOM_NONE
},
632 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
633 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
634 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
635 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
636 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
637 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
638 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
639 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
640 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
641 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
642 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
643 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
644 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
645 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
646 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
649 /* function prototypes */
650 xcb_char2b_t
*char2b(const char *);
651 int conf_load(char *, int);
652 void constrain_window(struct ws_win
*, struct swm_region
*, int);
654 void event_handle(xcb_generic_event_t
*);
655 struct ws_win
*find_window(xcb_window_t
);
656 int floating_toggle_win(struct ws_win
*);
657 void focus(struct swm_region
*, union arg
*);
658 void focus_magic(struct ws_win
*);
659 xcb_atom_t
get_atom_from_string(const char *);
660 xcb_screen_t
*get_screen(int);
661 char *get_win_name(xcb_window_t
);
662 uint16_t getstate(xcb_window_t
);
663 void grabbuttons(struct ws_win
*, int);
664 void map_window_raised(xcb_window_t
);
665 void new_region(struct swm_screen
*, int, int, int, int);
666 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
667 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
668 void store_float_geom(struct ws_win
*, struct swm_region
*);
669 void unmanage_window(struct ws_win
*);
670 void update_window(struct ws_win
*);
672 char *get_atom_name(xcb_atom_t
);
675 /* function definitions */
677 char2b(const char *str
)
683 s
= malloc(len
* sizeof(xcb_char2b_t
));
687 for (i
= 0; i
< len
; i
++) {
696 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
698 unsigned int tmpr
, tmpg
, tmpb
;
700 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
703 *rr
= RGB_8_TO_16(tmpr
);
704 *gg
= RGB_8_TO_16(tmpg
);
705 *bb
= RGB_8_TO_16(tmpb
);
711 get_screen(int screen
)
713 xcb_screen_iterator_t i
;
715 i
= xcb_setup_roots_iterator(xcb_get_setup(conn
));
716 for (; i
.rem
; --screen
, xcb_screen_next(&i
))
726 xcb_get_input_focus_cookie_t c
;
727 xcb_get_input_focus_reply_t
*r
;
730 c
= xcb_get_input_focus(conn
);
732 r
= xcb_get_input_focus_reply(conn
, c
, NULL
);
738 map_window_raised(xcb_window_t win
)
740 uint32_t val
= XCB_STACK_MODE_ABOVE
;
742 xcb_configure_window(conn
, win
,
743 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
745 xcb_map_window(conn
, win
);
749 get_atom_from_string(const char *str
)
751 xcb_intern_atom_cookie_t c
;
752 xcb_intern_atom_reply_t
*r
;
755 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
756 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
764 return (XCB_ATOM_NONE
);
768 update_iconic(struct ws_win
*win
, int newv
)
775 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
776 a_swm_iconic
, XCB_ATOM_INTEGER
, 32, 1, &v
);
778 xcb_delete_property(conn
, win
->id
, a_swm_iconic
);
782 get_iconic(struct ws_win
*win
)
785 xcb_get_property_reply_t
*pr
= NULL
;
788 pr
= xcb_get_property_reply(conn
,
789 xcb_get_property(conn
, 0, win
->id
, a_swm_iconic
,
790 XCB_ATOM_INTEGER
, 0, 1), NULL
);
793 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
795 v
= *((int32_t *)xcb_get_property_value(pr
));
806 int i
, j
, num_screens
;
808 sup_list
= get_atom_from_string("_NET_SUPPORTED");
810 for (i
= 0; i
< LENGTH(ewmh
); i
++)
811 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
813 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
814 for (i
= 0; i
< num_screens
; i
++) {
815 /* Support check window will be created by workaround(). */
817 /* Report supported atoms */
818 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
819 for (j
= 0; j
< LENGTH(ewmh
); j
++)
820 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
821 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
830 xcb_atom_t sup_check
, sup_list
;
832 xcb_get_property_cookie_t pc
;
833 xcb_get_property_reply_t
*pr
;
835 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
836 sup_list
= get_atom_from_string("_NET_SUPPORTED");
837 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
839 for (i
= 0; i
< num_screens
; i
++) {
840 /* Get the support check window and destroy it */
841 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, sup_check
,
842 XCB_ATOM_WINDOW
, 0, 1);
843 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
845 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
847 xcb_destroy_window(conn
, id
);
848 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
849 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
857 ewmh_autoquirk(struct ws_win
*win
)
861 xcb_get_property_cookie_t c
;
862 xcb_get_property_reply_t
*r
;
864 c
= xcb_get_property(conn
, 0, win
->id
,
865 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
866 r
= xcb_get_property_reply(conn
, c
, NULL
);
870 n
= xcb_get_property_value_length(r
);
871 type
= xcb_get_property_value(r
);
873 for (i
= 0; i
< n
; i
++) {
874 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
876 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
877 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
878 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
880 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
883 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
884 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
886 win
->quirks
= SWM_Q_FLOAT
;
893 #define SWM_EWMH_ACTION_COUNT_MAX (6)
894 #define EWMH_F_FULLSCREEN (1<<0)
895 #define EWMH_F_ABOVE (1<<1)
896 #define EWMH_F_HIDDEN (1<<2)
897 #define EWMH_F_SKIP_PAGER (1<<3)
898 #define EWMH_F_SKIP_TASKBAR (1<<4)
899 #define SWM_F_MANUAL (1<<5)
902 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
910 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
911 "fullscreen %s\n", win
->id
, YESNO(fs
));
914 if (!win
->g_floatvalid
)
915 store_float_geom(win
, win
->ws
->r
);
917 win
->g
= win
->ws
->r
->g
;
920 if (win
->g_floatvalid
) {
921 /* refloat at last floating relative position */
922 win
->g
= win
->g_float
;
923 X(win
) += X(win
->ws
->r
);
924 Y(win
) += Y(win
->ws
->r
);
932 ewmh_update_actions(struct ws_win
*win
)
934 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
940 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
943 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
944 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
947 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
948 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
951 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
952 #define _NET_WM_STATE_ADD 1 /* add/set property */
953 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
956 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
958 unsigned int mask
= 0;
959 unsigned int changed
= 0;
960 unsigned int orig_flags
;
965 DNPRINTF(SWM_D_PROP
, "ewmh_update_win_state: window: 0x%x, state: %ld, "
966 "action: %ld\n", win
->id
, state
, action
);
968 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
969 mask
= EWMH_F_FULLSCREEN
;
970 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
972 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
974 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
975 mask
= EWMH_F_SKIP_PAGER
;
976 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
977 mask
= EWMH_F_SKIP_TASKBAR
;
980 orig_flags
= win
->ewmh_flags
;
983 case _NET_WM_STATE_REMOVE
:
984 win
->ewmh_flags
&= ~mask
;
986 case _NET_WM_STATE_ADD
:
987 win
->ewmh_flags
|= mask
;
989 case _NET_WM_STATE_TOGGLE
:
990 win
->ewmh_flags
^= mask
;
994 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
996 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
998 if (!floating_toggle_win(win
))
999 win
->ewmh_flags
= orig_flags
; /* revert */
1000 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1002 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
1003 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1005 if (!ewmh_set_win_fullscreen(win
,
1006 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
1007 win
->ewmh_flags
= orig_flags
; /* revert */
1009 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1011 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
1012 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1013 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1014 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
1015 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1016 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1017 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1018 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1019 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1020 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1021 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1022 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1023 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1024 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1025 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1026 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1027 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1028 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1029 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1030 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1034 ewmh_get_win_state(struct ws_win
*win
)
1037 xcb_get_property_cookie_t c
;
1038 xcb_get_property_reply_t
*r
;
1044 win
->ewmh_flags
= 0;
1046 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1048 win
->ewmh_flags
|= SWM_F_MANUAL
;
1050 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1051 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1052 r
= xcb_get_property_reply(conn
, c
, NULL
);
1056 states
= xcb_get_property_value(r
);
1057 n
= xcb_get_property_value_length(r
);
1059 for (i
= 0; i
< n
; i
++)
1060 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1068 dumpwins(struct swm_region
*r
, union arg
*args
)
1072 xcb_get_window_attributes_cookie_t c
;
1073 xcb_get_window_attributes_reply_t
*wa
;
1075 /* suppress unused warning since var is needed */
1078 if (r
->ws
== NULL
) {
1079 warnx("dumpwins: invalid workspace");
1083 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1084 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1085 state
= getstate(win
->id
);
1086 c
= xcb_get_window_attributes(conn
, win
->id
);
1087 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1089 warnx("window: 0x%x, map_state: %d, state: %u, "
1090 "transient: 0x%x", win
->id
, wa
->map_state
,
1091 state
, win
->transient
);
1094 warnx("window: 0x%x, failed xcb_get_window_attributes",
1098 warnx("===== unmanaged window list =====");
1099 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1100 state
= getstate(win
->id
);
1101 c
= xcb_get_window_attributes(conn
, win
->id
);
1102 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1104 warnx("window: 0x%x, map_state: %d, state: %u, "
1105 "transient: 0x%x", win
->id
, wa
->map_state
,
1106 state
, win
->transient
);
1109 warnx("window: 0x%x, failed xcb_get_window_attributes",
1113 warnx("=================================");
1117 dumpwins(struct swm_region
*r
, union arg
*args
)
1120 #endif /* SWM_DEBUG */
1122 void event_error(xcb_generic_error_t
*);
1123 void expose(xcb_expose_event_t
*);
1124 void keypress(xcb_key_press_event_t
*);
1125 void buttonpress(xcb_button_press_event_t
*);
1126 void configurerequest(xcb_configure_request_event_t
*);
1127 void configurenotify(xcb_configure_notify_event_t
*);
1128 void destroynotify(xcb_destroy_notify_event_t
*);
1129 void enternotify(xcb_enter_notify_event_t
*);
1130 void mapnotify(xcb_map_notify_event_t
*);
1131 void mappingnotify(xcb_mapping_notify_event_t
*);
1132 void maprequest(xcb_map_request_event_t
*);
1133 void propertynotify(xcb_property_notify_event_t
*);
1134 void unmapnotify(xcb_unmap_notify_event_t
*);
1135 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1136 void clientmessage(xcb_client_message_event_t
*);
1137 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1142 int saved_errno
, status
;
1145 saved_errno
= errno
;
1149 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1154 if (errno
!= ECHILD
)
1155 warn("sighdlr: waitpid");
1156 #endif /* SWM_DEBUG */
1159 if (pid
== searchpid
)
1163 if (WIFEXITED(status
)) {
1164 if (WEXITSTATUS(status
) != 0)
1165 warnx("sighdlr: child exit status: %d",
1166 WEXITSTATUS(status
));
1168 warnx("sighdlr: child is terminated "
1170 #endif /* SWM_DEBUG */
1184 errno
= saved_errno
;
1190 struct pid_e
*p
= NULL
;
1192 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1197 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1206 name_to_pixel(const char *colorname
)
1208 uint32_t result
= 0;
1209 char cname
[32] = "#";
1210 xcb_screen_t
*screen
;
1211 xcb_colormap_t cmap
;
1212 xcb_alloc_color_reply_t
*cr
;
1213 xcb_alloc_named_color_reply_t
*nr
;
1214 uint16_t rr
, gg
, bb
;
1216 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1217 cmap
= screen
->default_colormap
;
1219 /* color is in format rgb://rr/gg/bb */
1220 if (strncmp(colorname
, "rgb:", 4) == 0) {
1221 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1222 warnx("could not parse rgb %s", colorname
);
1224 cr
= xcb_alloc_color_reply(conn
,
1225 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1231 warnx("color '%s' not found", colorname
);
1234 nr
= xcb_alloc_named_color_reply(conn
,
1235 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1238 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1239 nr
= xcb_alloc_named_color_reply(conn
,
1240 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1247 warnx("color '%s' not found", colorname
);
1254 setscreencolor(char *val
, int i
, int c
)
1258 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1259 if (i
> 0 && i
<= num_screens
) {
1260 screens
[i
- 1].c
[c
].pixel
= name_to_pixel(val
);
1261 free(screens
[i
- 1].c
[c
].name
);
1262 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1264 } else if (i
== -1) {
1265 for (i
= 0; i
< num_screens
; i
++) {
1266 screens
[i
].c
[c
].pixel
= name_to_pixel(val
);
1267 free(screens
[i
].c
[c
].name
);
1268 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1272 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1277 fancy_stacker(struct workspace
*ws
)
1279 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1280 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1281 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1282 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1283 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1284 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1285 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1286 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1287 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1291 plain_stacker(struct workspace
*ws
)
1293 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1294 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1295 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1296 sizeof ws
->stacker
);
1297 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1298 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1299 sizeof ws
->stacker
);
1303 custom_region(char *val
)
1305 unsigned int x
, y
, w
, h
;
1306 int sidx
, num_screens
;
1307 xcb_screen_t
*screen
;
1309 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1310 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1311 errx(1, "invalid custom region, "
1312 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1313 if (sidx
< 1 || sidx
> num_screens
)
1314 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1318 screen
= get_screen(sidx
);
1320 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1322 if (x
> screen
->width_in_pixels
||
1323 y
> screen
->height_in_pixels
||
1324 w
+ x
> screen
->width_in_pixels
||
1325 h
+ y
> screen
->height_in_pixels
) {
1326 warnx("ignoring region %ux%u+%u+%u - not within screen "
1327 "boundaries (%ux%u)", w
, h
, x
, y
,
1328 screen
->width_in_pixels
, screen
->height_in_pixels
);
1332 new_region(&screens
[sidx
], x
, y
, w
, h
);
1336 socket_setnonblock(int fd
)
1340 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1341 err(1, "fcntl F_GETFL");
1342 flags
|= O_NONBLOCK
;
1343 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1344 err(1, "fcntl F_SETFL");
1348 bar_print(struct swm_region
*r
, const char *s
)
1351 xcb_rectangle_t rect
;
1359 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
1361 switch (bar_justify
) {
1362 case SWM_BAR_JUSTIFY_LEFT
:
1365 case SWM_BAR_JUSTIFY_CENTER
:
1366 x
= (WIDTH(r
) - info
.width
) / 2;
1368 case SWM_BAR_JUSTIFY_RIGHT
:
1369 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
1373 if (x
< SWM_BAR_OFFSET
)
1378 rect
.width
= WIDTH(r
->bar
);
1379 rect
.height
= HEIGHT(r
->bar
);
1381 /* clear back buffer */
1382 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1383 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1384 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
,
1385 sizeof(rect
), &rect
);
1387 /* draw back buffer */
1388 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1389 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_BACKGROUND
, gcv
);
1390 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
;
1391 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1393 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
1394 DefaultVisual(display
, r
->s
->idx
),
1395 DefaultColormap(display
, r
->s
->idx
));
1397 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
1398 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
1401 XftDrawDestroy(draw
);
1404 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1405 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1409 bar_extra_stop(void)
1413 bzero(bar_pipe
, sizeof bar_pipe
);
1416 kill(bar_pid
, SIGTERM
);
1419 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1424 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1426 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1428 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1429 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1433 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1435 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1437 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1438 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1442 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1444 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1447 bar_class_name(s
, sz
, r
);
1448 strlcat(s
, ":", sz
);
1449 bar_title_name(s
, sz
, r
);
1453 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1455 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1457 if (r
->ws
->focus
->floating
)
1458 strlcat(s
, "(f)", sz
);
1462 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1466 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1468 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1471 strlcat(s
, title
, sz
);
1475 int urgent
[SWM_WS_MAX
];
1477 bar_urgent(char *s
, size_t sz
)
1480 int i
, j
, num_screens
;
1482 xcb_get_property_cookie_t c
;
1483 xcb_icccm_wm_hints_t hints
;
1485 for (i
= 0; i
< workspace_limit
; i
++)
1488 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1489 for (i
= 0; i
< num_screens
; i
++)
1490 for (j
= 0; j
< workspace_limit
; j
++)
1491 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1492 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
1493 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
1496 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
1500 for (i
= 0; i
< workspace_limit
; i
++) {
1502 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1504 snprintf(b
, sizeof b
, "- ");
1510 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1512 if (r
== NULL
|| r
->ws
== NULL
)
1514 if (r
->ws
->name
!= NULL
)
1515 strlcat(s
, r
->ws
->name
, sz
);
1518 /* build the default bar format according to the defined enabled options */
1520 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1522 /* if format provided, just copy the buffers */
1523 if (bar_format
!= NULL
) {
1524 strlcpy(fmtnew
, fmtexp
, sz
);
1528 /* reset the output buffer */
1531 strlcat(fmtnew
, "+N:+I ", sz
);
1533 strlcat(fmtnew
, "+S", sz
);
1534 strlcat(fmtnew
, " ", sz
);
1536 /* only show the workspace name if there's actually one */
1537 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1538 strlcat(fmtnew
, "<+D>", sz
);
1539 strlcat(fmtnew
, "+3<", sz
);
1541 if (clock_enabled
) {
1542 strlcat(fmtnew
, fmtexp
, sz
);
1543 strlcat(fmtnew
, "+4<", sz
);
1546 /* bar_urgent already adds the space before the last asterisk */
1548 strlcat(fmtnew
, "* +U*+4<", sz
);
1550 if (title_class_enabled
) {
1551 strlcat(fmtnew
, "+C", sz
);
1552 if (title_name_enabled
== 0)
1553 strlcat(fmtnew
, "+4<", sz
);
1556 /* checks needed by the colon and floating strlcat(3) calls below */
1557 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1558 if (title_name_enabled
) {
1559 if (title_class_enabled
)
1560 strlcat(fmtnew
, ":", sz
);
1561 strlcat(fmtnew
, "+T+4<", sz
);
1563 if (window_name_enabled
) {
1564 if (r
->ws
->focus
->floating
)
1565 strlcat(fmtnew
, "+F ", sz
);
1566 strlcat(fmtnew
, "+64W ", sz
);
1570 /* finally add the action script output and the version */
1571 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1575 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1577 /* special case; no limit given, pad one space, instead */
1578 if (*limit
== (int)sz
- 1)
1580 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1583 /* replaces the bar format character sequences (like in tmux(1)) */
1585 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1589 char tmp
[SWM_BAR_MAX
];
1593 /* reset strlcat(3) buffer */
1596 /* get number, if any */
1599 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1600 limit
= sizeof tmp
- 1;
1601 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
1602 limit
= sizeof tmp
- 1;
1604 /* there is nothing to replace (ie EOL) */
1611 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1614 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1617 bar_class_name(tmp
, sizeof tmp
, r
);
1620 bar_workspace_name(tmp
, sizeof tmp
, r
);
1623 bar_window_float(tmp
, sizeof tmp
, r
);
1626 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1629 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1632 bar_class_title_name(tmp
, sizeof tmp
, r
);
1635 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1638 bar_title_name(tmp
, sizeof tmp
, r
);
1641 bar_urgent(tmp
, sizeof tmp
);
1644 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1647 bar_window_name(tmp
, sizeof tmp
, r
);
1650 /* unknown character sequence; copy as-is */
1651 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1657 if ((int)len
< limit
)
1659 while (limit
-- > 0) {
1660 if (*offrep
>= sz
- 1)
1662 fmtrep
[(*offrep
)++] = *ptr
++;
1670 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1675 while (*fmt
!= '\0') {
1677 /* skip ordinary characters */
1680 fmtrep
[off
++] = *fmt
++;
1684 /* character sequence found; replace it */
1685 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1694 bar_fmt_expand(char *fmtexp
, size_t sz
)
1701 /* start by grabbing the current time and date */
1703 localtime_r(&tmt
, &tm
);
1705 /* figure out what to expand */
1706 if (bar_format
!= NULL
)
1708 else if (bar_format
== NULL
&& clock_enabled
)
1710 /* if nothing to expand bail out */
1716 /* copy as-is, just in case the format shouldn't be expanded below */
1717 strlcpy(fmtexp
, fmt
, sz
);
1718 /* finally pass the string through strftime(3) */
1719 #ifndef SWM_DENY_CLOCK_FORMAT
1720 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1721 warnx("format too long");
1729 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1730 char fmtrep
[SWM_BAR_MAX
];
1732 struct swm_region
*r
;
1734 /* expand the format by first passing it through strftime(3) */
1735 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1737 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1738 for (i
= 0; i
< num_screens
; i
++) {
1739 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1742 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1743 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1744 bar_print(r
, fmtrep
);
1755 if (bar_enabled
== 0)
1757 if (bar_extra
&& bar_extra_running
) {
1758 /* ignore short reads; it'll correct itself */
1759 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1760 if (b
&& b
[len
- 1] == '\n') {
1762 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1764 if (b
== NULL
&& errno
!= EAGAIN
) {
1765 warn("bar_update: bar_extra failed");
1769 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1778 /* suppress unused warning since var is needed */
1785 bar_toggle(struct swm_region
*r
, union arg
*args
)
1787 struct swm_region
*tmpr
;
1790 /* suppress unused warnings since vars are needed */
1794 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1796 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1798 for (i
= 0; i
< num_screens
; i
++)
1799 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1801 xcb_unmap_window(conn
, tmpr
->bar
->id
);
1803 for (i
= 0; i
< num_screens
; i
++)
1804 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1806 map_window_raised(tmpr
->bar
->id
);
1809 bar_enabled
= !bar_enabled
;
1812 /* must be after stack */
1821 struct swm_region
*r
;
1825 /* do this here because the conf file is in memory */
1826 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1827 /* launch external status app */
1828 bar_extra_running
= 1;
1829 if (pipe(bar_pipe
) == -1)
1830 err(1, "pipe error");
1831 socket_setnonblock(bar_pipe
[0]);
1832 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1833 if (dup2(bar_pipe
[0], 0) == -1)
1835 if (dup2(bar_pipe
[1], 1) == -1)
1837 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1838 err(1, "could not disable SIGPIPE");
1839 switch (bar_pid
= fork()) {
1841 err(1, "cannot fork");
1845 execvp(bar_argv
[0], bar_argv
);
1846 err(1, "%s external app failed", bar_argv
[0]);
1848 default: /* parent */
1854 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1855 for (i
= 0; i
< num_screens
; i
++)
1856 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1859 wa
[0] = screens
[i
].c
[SWM_S_COLOR_BAR
].pixel
;
1860 wa
[1] = screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1861 xcb_change_window_attributes(conn
, r
->bar
->id
,
1862 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
1868 bar_setup(struct swm_region
*r
)
1870 char *font
, *fontpos
;
1872 xcb_screen_t
*screen
= get_screen(r
->s
->idx
);
1879 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
1880 err(1, "bar_setup: calloc: failed to allocate memory.");
1882 if (bar_font
== NULL
) {
1883 while ((font
= strsep(&bar_fonts
, ",")) != NULL
) {
1887 DNPRINTF(SWM_D_INIT
, "bar_setup: try font %s\n", font
);
1891 while ((fontpos
= index(fontpos
, '-'))) {
1897 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
1900 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
1903 warnx("unable to load font %s", font
);
1906 DNPRINTF(SWM_D_INIT
, "successfully opened "
1913 if (bar_font
== NULL
)
1914 errx(1, "unable to open a font");
1916 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
1921 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
1923 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
1924 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
1925 warn("unable to allocate Xft color");
1928 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1929 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
1930 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
1932 r
->bar
->id
= xcb_generate_id(conn
);
1933 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1934 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1935 wa
[2] = XCB_EVENT_MASK_EXPOSURE
;
1937 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
1938 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
1939 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1940 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
1941 | XCB_CW_EVENT_MASK
, wa
);
1943 r
->bar
->buffer
= xcb_generate_id(conn
);
1944 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
1945 WIDTH(r
->bar
), HEIGHT(r
->bar
));
1947 xcb_randr_select_input(conn
, r
->bar
->id
,
1948 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
1951 map_window_raised(r
->bar
->id
);
1953 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
1954 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
1957 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1958 err(1, "could not install bar_signal");
1963 bar_cleanup(struct swm_region
*r
)
1967 xcb_destroy_window(conn
, r
->bar
->id
);
1968 xcb_free_pixmap(conn
, r
->bar
->buffer
);
1974 set_win_state(struct ws_win
*win
, uint16_t state
)
1976 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
1978 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x\n", win
->id
);
1983 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
1984 a_state
, 32, 2, data
);
1988 getstate(xcb_window_t w
)
1990 uint16_t result
= 0;
1991 xcb_get_property_cookie_t c
;
1992 xcb_get_property_reply_t
*r
;
1994 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
1995 r
= xcb_get_property_reply(conn
, c
, NULL
);
1998 result
= *((uint16_t *)xcb_get_property_value(r
));
2002 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
2008 version(struct swm_region
*r
, union arg
*args
)
2010 /* suppress unused warnings since vars are needed */
2014 bar_version
= !bar_version
;
2016 snprintf(bar_vertext
, sizeof bar_vertext
,
2017 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2019 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2026 client_msg(struct ws_win
*win
, xcb_atom_t a
)
2028 xcb_client_message_event_t ev
;
2033 bzero(&ev
, sizeof ev
);
2034 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2035 ev
.window
= win
->id
;
2038 ev
.data
.data32
[0] = a
;
2039 ev
.data
.data32
[1] = XCB_CURRENT_TIME
;
2041 xcb_send_event(conn
, 0, win
->id
,
2042 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2045 /* synthetic response to a ConfigureRequest when not making a change */
2047 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2049 xcb_configure_notify_event_t ce
;
2054 /* send notification of unchanged state. */
2055 bzero(&ce
, sizeof(ce
));
2056 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2059 ce
.width
= WIDTH(win
);
2060 ce
.height
= HEIGHT(win
);
2061 ce
.override_redirect
= 0;
2066 ce
.window
= win
->id
;
2067 ce
.border_width
= BORDER(win
);
2068 ce
.above_sibling
= XCB_WINDOW_NONE
;
2071 ce
.event
= ev
->window
;
2072 ce
.window
= ev
->window
;
2074 /* make response appear more WM_SIZE_HINTS-compliant */
2076 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2077 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2078 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2079 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2080 SH_INC_W(win
), SH_INC_H(win
));
2084 /* the hint may be set... to 0! */
2085 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2086 ce
.width
= SH_MIN_W(win
);
2087 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2088 ce
.height
= SH_MIN_H(win
);
2093 /* may also be advertized as 0 */
2094 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2095 ce
.width
= SH_MAX_W(win
);
2096 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2097 ce
.height
= SH_MAX_H(win
);
2100 /* resize increment. */
2102 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2103 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2105 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2106 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2110 /* adjust x and y for requested border_width. */
2111 ce
.x
+= BORDER(win
) - ev
->border_width
;
2112 ce
.y
+= BORDER(win
) - ev
->border_width
;
2113 ce
.border_width
= ev
->border_width
;
2114 ce
.above_sibling
= ev
->sibling
;
2117 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2118 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2119 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2121 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2126 count_win(struct workspace
*ws
, int count_transient
)
2131 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2132 if (count_transient
== 0 && win
->floating
)
2134 if (count_transient
== 0 && win
->transient
)
2140 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2146 quit(struct swm_region
*r
, union arg
*args
)
2148 /* suppress unused warnings since vars are needed */
2152 DNPRINTF(SWM_D_MISC
, "quit\n");
2157 unmap_window(struct ws_win
*win
)
2159 DNPRINTF(SWM_D_EVENT
, "unmap_window: window: 0x%x\n", win
->id
);
2164 /* don't unmap again */
2165 if (getstate(win
->id
) == XCB_ICCCM_WM_STATE_ICONIC
)
2168 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2170 xcb_unmap_window(conn
, win
->id
);
2171 xcb_change_window_attributes(conn
, win
->id
,
2172 XCB_CW_BORDER_PIXEL
, &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
2179 int i
, j
, num_screens
;
2181 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2182 for (i
= 0; i
< num_screens
; i
++)
2183 for (j
= 0; j
< workspace_limit
; j
++)
2184 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2189 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2191 xcb_key_press_event_t event
;
2192 xcb_keycode_t
*keycode
;
2197 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2199 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2202 bzero(&event
, sizeof(event
));
2203 event
.event
= win
->id
;
2204 event
.root
= win
->s
->root
;
2205 event
.child
= XCB_WINDOW_NONE
;
2206 event
.time
= XCB_CURRENT_TIME
;
2207 event
.event_x
= X(win
);
2208 event
.event_y
= Y(win
);
2211 event
.same_screen
= 1;
2212 event
.detail
= *keycode
;
2213 event
.state
= modifiers
;
2215 event
.response_type
= XCB_KEY_PRESS
;
2216 xcb_send_event(conn
, 1, win
->id
,
2217 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
2219 event
.response_type
= XCB_KEY_RELEASE
;
2220 xcb_send_event(conn
, 1, win
->id
,
2221 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
2225 restart(struct swm_region
*r
, union arg
*args
)
2227 /* suppress unused warning since var is needed */
2230 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2232 /* disable alarm because the following code may not be interrupted */
2234 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
2235 err(1, "can't disable alarm");
2241 XftFontClose(display
, bar_font
);
2242 XftColorFree(display
, DefaultVisual(display
, r
->s
->idx
),
2243 DefaultColormap(display
, r
->s
->idx
), &bar_font_color
);
2244 xcb_key_symbols_free(syms
);
2246 xcb_disconnect(conn
);
2248 execvp(start_argv
[0], start_argv
);
2249 warn("execvp failed");
2254 root_to_region(xcb_window_t root
)
2256 struct swm_region
*r
= NULL
;
2258 xcb_query_pointer_reply_t
*qpr
;
2260 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2262 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2263 for (i
= 0; i
< num_screens
; i
++)
2264 if (screens
[i
].root
== root
)
2267 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2268 screens
[i
].root
), NULL
);
2271 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: (%d,%d)\n",
2272 qpr
->root_x
, qpr
->root_y
);
2273 /* choose a region based on pointer location */
2274 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2275 if (X(r
) <= qpr
->root_x
&& qpr
->root_x
< MAX_X(r
) &&
2276 Y(r
) <= qpr
->root_y
&& qpr
->root_y
< MAX_Y(r
))
2282 r
= TAILQ_FIRST(&screens
[i
].rl
);
2288 find_unmanaged_window(xcb_window_t id
)
2291 int i
, j
, num_screens
;
2293 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2294 for (i
= 0; i
< num_screens
; i
++)
2295 for (j
= 0; j
< workspace_limit
; j
++)
2296 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2304 find_window(xcb_window_t id
)
2307 int i
, j
, num_screens
;
2308 xcb_query_tree_reply_t
*r
;
2310 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2311 for (i
= 0; i
< num_screens
; i
++)
2312 for (j
= 0; j
< workspace_limit
; j
++)
2313 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2317 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
2321 /* if we were looking for the parent return that window instead */
2322 if (r
->parent
== 0 || r
->root
== r
->parent
)
2325 /* look for parent */
2326 for (i
= 0; i
< num_screens
; i
++)
2327 for (j
= 0; j
< workspace_limit
; j
++)
2328 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2329 if (r
->parent
== win
->id
) {
2339 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2344 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2346 close(xcb_get_file_descriptor(conn
));
2348 setenv("LD_PRELOAD", SWM_LIB
, 1);
2350 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2351 warn("spawn: asprintf SWM_WS");
2354 setenv("_SWM_WS", ret
, 1);
2358 if (asprintf(&ret
, "%d", getpid()) == -1) {
2359 warn("spawn: asprintf _SWM_PID");
2362 setenv("_SWM_PID", ret
, 1);
2366 if (setsid() == -1) {
2367 warn("spawn: setsid");
2373 * close stdin and stdout to prevent interaction between apps
2374 * and the baraction script
2375 * leave stderr open to record errors
2377 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2378 warn("spawn: open");
2381 dup2(fd
, STDIN_FILENO
);
2382 dup2(fd
, STDOUT_FILENO
);
2387 execvp(args
->argv
[0], args
->argv
);
2389 warn("spawn: execvp");
2394 kill_refs(struct ws_win
*win
)
2396 int i
, x
, num_screens
;
2397 struct swm_region
*r
;
2398 struct workspace
*ws
;
2403 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2404 for (i
= 0; i
< num_screens
; i
++)
2405 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2406 for (x
= 0; x
< workspace_limit
; x
++) {
2408 if (win
== ws
->focus
)
2410 if (win
== ws
->focus_prev
)
2411 ws
->focus_prev
= NULL
;
2416 validate_win(struct ws_win
*testwin
)
2419 struct workspace
*ws
;
2420 struct swm_region
*r
;
2421 int i
, x
, num_screens
;
2423 if (testwin
== NULL
)
2426 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2427 for (i
= 0; i
< num_screens
; i
++)
2428 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2429 for (x
= 0; x
< workspace_limit
; x
++) {
2431 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2439 validate_ws(struct workspace
*testws
)
2441 struct swm_region
*r
;
2442 struct workspace
*ws
;
2443 int i
, x
, num_screens
;
2445 /* validate all ws */
2446 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2447 for (i
= 0; i
< num_screens
; i
++)
2448 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2449 for (x
= 0; x
< workspace_limit
; x
++) {
2458 unfocus_win(struct ws_win
*win
)
2460 xcb_window_t none
= XCB_WINDOW_NONE
;
2462 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
2466 if (win
->ws
== NULL
)
2469 if (validate_ws(win
->ws
))
2470 return; /* XXX this gets hit with thunderbird, needs fixing */
2472 if (win
->ws
->r
== NULL
)
2475 if (validate_win(win
)) {
2480 if (win
->ws
->focus
== win
) {
2481 win
->ws
->focus
= NULL
;
2482 win
->ws
->focus_prev
= win
;
2485 if (validate_win(win
->ws
->focus
)) {
2486 kill_refs(win
->ws
->focus
);
2487 win
->ws
->focus
= NULL
;
2489 if (validate_win(win
->ws
->focus_prev
)) {
2490 kill_refs(win
->ws
->focus_prev
);
2491 win
->ws
->focus_prev
= NULL
;
2494 grabbuttons(win
, 0);
2495 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2496 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
2497 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2498 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
2505 int i
, j
, num_screens
;
2507 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2509 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2510 for (i
= 0; i
< num_screens
; i
++)
2511 for (j
= 0; j
< workspace_limit
; j
++)
2512 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2517 focus_win(struct ws_win
*win
)
2519 struct ws_win
*cfw
= NULL
;
2520 xcb_get_input_focus_reply_t
*r
;
2521 xcb_window_t cur_focus
= XCB_WINDOW_NONE
;
2523 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
2527 if (win
->ws
== NULL
)
2530 if (validate_ws(win
->ws
))
2531 return; /* XXX this gets hit with thunderbird, needs fixing */
2533 if (validate_win(win
)) {
2538 if (validate_win(win
)) {
2543 r
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
2545 cur_focus
= r
->focus
;
2548 if ((cfw
= find_window(cur_focus
)) != NULL
)
2552 /* use larger hammer since the window was killed somehow */
2553 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2554 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2555 xcb_change_window_attributes(conn
, cfw
->id
,
2556 XCB_CW_BORDER_PIXEL
,
2557 &cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
2561 win
->ws
->focus
= win
;
2563 if (win
->ws
->r
!= NULL
) {
2565 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
2566 win
->id
, XCB_CURRENT_TIME
);
2567 grabbuttons(win
, 1);
2568 xcb_change_window_attributes(conn
, win
->id
,
2569 XCB_CW_BORDER_PIXEL
,
2570 &win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
);
2571 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2572 win
->ws
->always_raise
)
2573 map_window_raised(win
->id
);
2575 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2576 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2584 event_drain(uint8_t rt
)
2586 xcb_generic_event_t
*evt
;
2589 while ((evt
= xcb_poll_for_event(conn
))) {
2590 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
2598 switchws(struct swm_region
*r
, union arg
*args
)
2600 int wsid
= args
->id
, unmap_old
= 0;
2601 struct swm_region
*this_r
, *other_r
;
2603 struct workspace
*new_ws
, *old_ws
;
2609 if (wsid
>= workspace_limit
)
2613 old_ws
= this_r
->ws
;
2614 new_ws
= &this_r
->s
->ws
[wsid
];
2616 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2617 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2619 if (new_ws
== NULL
|| old_ws
== NULL
)
2621 if (new_ws
== old_ws
)
2624 other_r
= new_ws
->r
;
2625 if (other_r
== NULL
) {
2626 /* the other workspace is hidden, hide this one */
2630 /* the other ws is visible in another region, exchange them */
2631 other_r
->ws_prior
= new_ws
;
2632 other_r
->ws
= old_ws
;
2633 old_ws
->r
= other_r
;
2635 this_r
->ws_prior
= old_ws
;
2636 this_r
->ws
= new_ws
;
2639 /* this is needed so that we can click on a window after a restart */
2643 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2644 focus(new_ws
->r
, &a
);
2646 /* unmap old windows */
2648 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2651 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2652 event_drain(XCB_ENTER_NOTIFY
);
2656 DNPRINTF(SWM_D_WS
, "switchws: done\n");
2660 cyclews(struct swm_region
*r
, union arg
*args
)
2663 struct swm_screen
*s
= r
->s
;
2666 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2667 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2672 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2675 case SWM_ARG_ID_CYCLEWS_UP
:
2676 if (a
.id
< workspace_limit
- 1)
2681 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2684 case SWM_ARG_ID_CYCLEWS_DOWN
:
2688 a
.id
= workspace_limit
- 1;
2695 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2697 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2701 } while (a
.id
!= r
->ws
->idx
);
2705 priorws(struct swm_region
*r
, union arg
*args
)
2709 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2710 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2712 if (r
->ws_prior
== NULL
)
2715 a
.id
= r
->ws_prior
->idx
;
2720 cyclescr(struct swm_region
*r
, union arg
*args
)
2722 struct swm_region
*rr
= NULL
;
2724 int i
, x
, y
, num_screens
;
2726 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2727 /* do nothing if we don't have more than one screen */
2728 if (!(num_screens
> 1 || outputs
> 1))
2733 case SWM_ARG_ID_CYCLESC_UP
:
2734 rr
= TAILQ_NEXT(r
, entry
);
2736 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2738 case SWM_ARG_ID_CYCLESC_DOWN
:
2739 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2741 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2749 /* move mouse to region */
2751 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2752 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0, 0,
2755 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2758 if (rr
->ws
->focus
) {
2759 /* move to focus window */
2760 x
= X(rr
->ws
->focus
) + 1;
2761 y
= Y(rr
->ws
->focus
) + 1;
2762 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0,
2768 sort_windows(struct ws_win_list
*wl
)
2770 struct ws_win
*win
, *parent
, *nxt
;
2775 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2776 nxt
= TAILQ_NEXT(win
, entry
);
2777 if (win
->transient
) {
2778 parent
= find_window(win
->transient
);
2779 if (parent
== NULL
) {
2780 warnx("not possible bug");
2783 TAILQ_REMOVE(wl
, win
, entry
);
2784 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2791 swapwin(struct swm_region
*r
, union arg
*args
)
2793 struct ws_win
*target
, *source
;
2794 struct ws_win
*cur_focus
;
2795 struct ws_win_list
*wl
;
2798 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2799 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2801 cur_focus
= r
->ws
->focus
;
2802 if (cur_focus
== NULL
)
2806 wl
= &source
->ws
->winlist
;
2809 case SWM_ARG_ID_SWAPPREV
:
2810 if (source
->transient
)
2811 source
= find_window(source
->transient
);
2812 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2813 if (target
&& target
->transient
)
2814 target
= find_window(target
->transient
);
2815 TAILQ_REMOVE(wl
, source
, entry
);
2817 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2819 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2821 case SWM_ARG_ID_SWAPNEXT
:
2822 target
= TAILQ_NEXT(source
, entry
);
2823 /* move the parent and let the sort handle the move */
2824 if (source
->transient
)
2825 source
= find_window(source
->transient
);
2826 TAILQ_REMOVE(wl
, source
, entry
);
2828 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2830 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2832 case SWM_ARG_ID_SWAPMAIN
:
2833 target
= TAILQ_FIRST(wl
);
2834 if (target
== source
) {
2835 if (source
->ws
->focus_prev
!= NULL
&&
2836 source
->ws
->focus_prev
!= target
)
2837 source
= source
->ws
->focus_prev
;
2841 if (target
== NULL
|| source
== NULL
)
2843 source
->ws
->focus_prev
= target
;
2844 TAILQ_REMOVE(wl
, target
, entry
);
2845 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2846 TAILQ_REMOVE(wl
, source
, entry
);
2847 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2849 case SWM_ARG_ID_MOVELAST
:
2850 TAILQ_REMOVE(wl
, source
, entry
);
2851 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2854 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2866 focus_prev(struct ws_win
*win
)
2868 struct ws_win
*winfocus
= NULL
;
2869 struct ws_win
*cur_focus
= NULL
;
2870 struct ws_win_list
*wl
= NULL
;
2871 struct workspace
*ws
= NULL
;
2873 if (!(win
&& win
->ws
))
2878 cur_focus
= ws
->focus
;
2880 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2881 WINID(win
), WINID(cur_focus
));
2883 /* pickle, just focus on whatever */
2884 if (cur_focus
== NULL
) {
2885 /* use prev_focus if valid */
2886 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2887 find_window(WINID(ws
->focus_prev
)))
2888 winfocus
= ws
->focus_prev
;
2892 /* if transient focus on parent */
2893 if (cur_focus
->transient
) {
2894 winfocus
= find_window(cur_focus
->transient
);
2898 /* if in max_stack try harder */
2899 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2900 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2901 if (cur_focus
!= ws
->focus_prev
)
2902 winfocus
= ws
->focus_prev
;
2904 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2909 DNPRINTF(SWM_D_FOCUS
, "focus_prev: focus_close: %d\n", focus_close
);
2911 if (winfocus
== NULL
|| winfocus
== win
) {
2912 switch (focus_close
) {
2913 case SWM_STACK_BOTTOM
:
2914 winfocus
= TAILQ_FIRST(wl
);
2917 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2919 case SWM_STACK_ABOVE
:
2920 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
2921 if (focus_close_wrap
)
2922 winfocus
= TAILQ_FIRST(wl
);
2924 winfocus
= TAILQ_PREV(cur_focus
,
2925 ws_win_list
, entry
);
2928 case SWM_STACK_BELOW
:
2929 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
2931 if (focus_close_wrap
)
2932 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2934 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2940 if (winfocus
== NULL
) {
2941 if (focus_default
== SWM_STACK_TOP
)
2942 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2944 winfocus
= TAILQ_FIRST(wl
);
2948 focus_magic(winfocus
);
2952 focus(struct swm_region
*r
, union arg
*args
)
2954 struct ws_win
*winfocus
= NULL
, *head
;
2955 struct ws_win
*cur_focus
= NULL
;
2956 struct ws_win_list
*wl
= NULL
;
2957 struct workspace
*ws
= NULL
;
2963 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2965 /* treat FOCUS_CUR special */
2966 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2967 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2968 winfocus
= r
->ws
->focus
;
2969 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2970 winfocus
= r
->ws
->focus_prev
;
2972 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2973 if (winfocus
->iconic
== 0)
2976 focus_magic(winfocus
);
2980 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2984 if (TAILQ_EMPTY(wl
))
2986 /* make sure there is at least one uniconified window */
2988 TAILQ_FOREACH(winfocus
, wl
, entry
)
2989 if (winfocus
->iconic
== 0) {
2997 case SWM_ARG_ID_FOCUSPREV
:
2998 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3000 head
= TAILQ_LAST(wl
, ws_win_list
);
3002 if (WINID(winfocus
) == cur_focus
->transient
) {
3003 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
3005 head
= TAILQ_LAST(wl
, ws_win_list
);
3010 if (winfocus
&& winfocus
->iconic
) {
3011 while (winfocus
!= cur_focus
) {
3012 if (winfocus
== NULL
)
3013 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3014 if (winfocus
->iconic
== 0)
3016 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3022 case SWM_ARG_ID_FOCUSNEXT
:
3023 head
= TAILQ_NEXT(cur_focus
, entry
);
3025 head
= TAILQ_FIRST(wl
);
3029 if (winfocus
&& winfocus
->iconic
) {
3030 while (winfocus
!= cur_focus
) {
3031 if (winfocus
== NULL
)
3032 winfocus
= TAILQ_FIRST(wl
);
3033 if (winfocus
->iconic
== 0)
3035 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3040 case SWM_ARG_ID_FOCUSMAIN
:
3041 winfocus
= TAILQ_FIRST(wl
);
3042 if (winfocus
== cur_focus
)
3043 winfocus
= cur_focus
->ws
->focus_prev
;
3050 focus_magic(winfocus
);
3056 cycle_layout(struct swm_region
*r
, union arg
*args
)
3058 struct workspace
*ws
= r
->ws
;
3061 /* suppress unused warning since var is needed */
3064 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3067 if (ws
->cur_layout
->l_stack
== NULL
)
3068 ws
->cur_layout
= &layouts
[0];
3073 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3074 event_drain(XCB_ENTER_NOTIFY
);
3076 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3081 stack_config(struct swm_region
*r
, union arg
*args
)
3083 struct workspace
*ws
= r
->ws
;
3085 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3088 if (ws
->cur_layout
->l_config
!= NULL
)
3089 ws
->cur_layout
->l_config(ws
, args
->id
);
3091 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3098 struct swm_geometry g
;
3099 struct swm_region
*r
;
3105 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3107 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3108 for (i
= 0; i
< num_screens
; i
++) {
3112 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3113 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3114 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3116 /* start with screen geometry, adjust for bar */
3118 g
.w
-= 2 * border_width
;
3119 g
.h
-= 2 * border_width
;
3125 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3126 r
->ws
->cur_layout
->l_string(r
->ws
);
3127 /* save r so we can track region changes */
3134 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3135 event_drain(XCB_ENTER_NOTIFY
);
3137 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3141 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3143 /* retain window geom and region geom */
3144 win
->g_float
= win
->g
;
3145 win
->g_float
.x
-= X(r
);
3146 win
->g_float
.y
-= Y(r
);
3147 win
->g_floatvalid
= 1;
3148 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
3149 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3150 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3151 win
->g_float
.w
, win
->g_float
.h
);
3155 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3160 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
3163 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3164 * geom on ws switches or return from max mode
3166 if (win
->g_floatvalid
&& (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3167 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)))) {
3168 /* refloat at last floating relative position */
3169 win
->g
= win
->g_float
;
3174 win
->floatmaxed
= 0;
3177 * if set to fullscreen mode, configure window to maximum size.
3179 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3180 if (!win
->g_floatvalid
)
3181 store_float_geom(win
, win
->ws
->r
);
3183 win
->g
= win
->ws
->r
->g
;
3187 * remove border on fullscreen floater when in fullscreen mode or when
3188 * the quirk is present.
3190 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3191 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3192 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3193 if (win
->bordered
) {
3195 X(win
) += border_width
;
3196 Y(win
) += border_width
;
3198 } else if (!win
->bordered
) {
3200 X(win
) -= border_width
;
3201 Y(win
) -= border_width
;
3204 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3205 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3206 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3211 * floaters and transients are auto-centred unless moved
3214 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3215 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3218 /* keep window within region bounds */
3219 constrain_window(win
, r
, 0);
3225 * Send keystrokes to terminal to decrease/increase the font size as the
3226 * window size changes.
3229 adjust_font(struct ws_win
*win
)
3231 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3232 win
->floating
|| win
->transient
)
3235 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3236 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3237 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3238 win
->font_size_boundary
[win
->font_steps
] =
3239 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3242 win
->last_inc
= win
->sh
.width_inc
;
3243 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
3244 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3245 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3248 win
->last_inc
= win
->sh
.width_inc
;
3249 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
3253 #define SWAPXY(g) do { \
3255 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3256 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3259 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3261 struct swm_geometry win_g
, r_g
= *g
;
3262 struct ws_win
*win
, *fs_win
= NULL
;
3263 int i
, j
, s
, stacks
;
3264 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3265 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3266 int split
, colno
, winno
, mwin
, msize
, mscale
;
3267 int remain
, missing
, v_slice
, reconfigure
;
3269 xcb_get_window_attributes_reply_t
*war
;
3271 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3272 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3274 winno
= count_win(ws
, 0);
3275 if (winno
== 0 && count_win(ws
, 1) == 0)
3278 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3279 if (win
->transient
== 0 && win
->floating
== 0
3280 && win
->iconic
== 0)
3287 w_inc
= win
->sh
.width_inc
;
3288 w_base
= win
->sh
.base_width
;
3289 mwin
= ws
->l_state
.horizontal_mwin
;
3290 mscale
= ws
->l_state
.horizontal_msize
;
3291 stacks
= ws
->l_state
.horizontal_stacks
;
3294 w_inc
= win
->sh
.height_inc
;
3295 w_base
= win
->sh
.base_height
;
3296 mwin
= ws
->l_state
.vertical_mwin
;
3297 mscale
= ws
->l_state
.vertical_msize
;
3298 stacks
= ws
->l_state
.vertical_stacks
;
3302 if (stacks
> winno
- mwin
)
3303 stacks
= winno
- mwin
;
3307 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3308 if (mwin
&& winno
> mwin
) {
3309 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3313 win_g
.w
= v_slice
* mscale
;
3315 if (w_inc
> 1 && w_inc
< v_slice
) {
3316 /* adjust for window's requested size increment */
3317 remain
= (win_g
.w
- w_base
) % w_inc
;
3323 win_g
.x
+= r_g
.w
- msize
;
3326 colno
= split
= winno
/ stacks
;
3327 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3328 2 * border_width
) / stacks
);
3330 hrh
= r_g
.h
/ colno
;
3331 extra
= r_g
.h
- (colno
* hrh
);
3332 win_g
.h
= hrh
- 2 * border_width
;
3334 /* stack all the tiled windows */
3335 i
= j
= 0, s
= stacks
;
3336 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3337 if (win
->transient
!= 0 || win
->floating
!= 0)
3339 if (win
->iconic
!= 0)
3342 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3347 if (split
&& i
== split
) {
3348 colno
= (winno
- mwin
) / stacks
;
3349 if (s
<= (winno
- mwin
) % stacks
)
3351 split
= split
+ colno
;
3352 hrh
= (r_g
.h
/ colno
);
3353 extra
= r_g
.h
- (colno
* hrh
);
3357 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3358 win_g
.w
= (r_g
.w
- msize
-
3359 (stacks
* 2 * border_width
)) / stacks
;
3361 win_g
.w
+= (r_g
.w
- msize
-
3362 (stacks
* 2 * border_width
)) % stacks
;
3366 win_g
.h
= hrh
- 2 * border_width
;
3368 h_inc
= win
->sh
.width_inc
;
3369 h_base
= win
->sh
.base_width
;
3371 h_inc
= win
->sh
.height_inc
;
3372 h_base
= win
->sh
.base_height
;
3374 if (j
== colno
- 1) {
3375 win_g
.h
= hrh
+ extra
;
3376 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3377 /* adjust for window's requested size increment */
3378 remain
= (win_g
.h
- h_base
) % h_inc
;
3379 missing
= h_inc
- remain
;
3381 if (missing
<= extra
|| j
== 0) {
3393 win_g
.y
+= last_h
+ 2 * border_width
;
3395 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3397 win_g
.w
+= 2 * border_width
;
3398 win_g
.h
+= 2 * border_width
;
3403 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3404 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3408 WIDTH(win
) = win_g
.h
;
3409 HEIGHT(win
) = win_g
.w
;
3412 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3413 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3417 WIDTH(win
) = win_g
.w
;
3418 HEIGHT(win
) = win_g
.h
;
3422 if (bordered
!= win
->bordered
) {
3424 win
->bordered
= bordered
;
3432 war
= xcb_get_window_attributes_reply(conn
,
3433 xcb_get_window_attributes(conn
, win
->id
),
3436 if (war
->map_state
== XCB_MAP_STATE_UNMAPPED
)
3437 map_window_raised(win
->id
);
3447 /* now, stack all the floaters and transients */
3448 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3449 if (win
->transient
== 0 && win
->floating
== 0)
3451 if (win
->iconic
== 1)
3453 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3458 stack_floater(win
, ws
->r
);
3459 map_window_raised(win
->id
);
3463 stack_floater(fs_win
, ws
->r
);
3464 map_window_raised(fs_win
->id
);
3469 vertical_config(struct workspace
*ws
, int id
)
3471 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3475 case SWM_ARG_ID_STACKRESET
:
3476 case SWM_ARG_ID_STACKINIT
:
3477 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3478 ws
->l_state
.vertical_mwin
= 1;
3479 ws
->l_state
.vertical_stacks
= 1;
3481 case SWM_ARG_ID_MASTERSHRINK
:
3482 if (ws
->l_state
.vertical_msize
> 1)
3483 ws
->l_state
.vertical_msize
--;
3485 case SWM_ARG_ID_MASTERGROW
:
3486 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3487 ws
->l_state
.vertical_msize
++;
3489 case SWM_ARG_ID_MASTERADD
:
3490 ws
->l_state
.vertical_mwin
++;
3492 case SWM_ARG_ID_MASTERDEL
:
3493 if (ws
->l_state
.vertical_mwin
> 0)
3494 ws
->l_state
.vertical_mwin
--;
3496 case SWM_ARG_ID_STACKINC
:
3497 ws
->l_state
.vertical_stacks
++;
3499 case SWM_ARG_ID_STACKDEC
:
3500 if (ws
->l_state
.vertical_stacks
> 1)
3501 ws
->l_state
.vertical_stacks
--;
3503 case SWM_ARG_ID_FLIPLAYOUT
:
3504 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3512 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3514 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3516 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3520 horizontal_config(struct workspace
*ws
, int id
)
3522 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3525 case SWM_ARG_ID_STACKRESET
:
3526 case SWM_ARG_ID_STACKINIT
:
3527 ws
->l_state
.horizontal_mwin
= 1;
3528 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3529 ws
->l_state
.horizontal_stacks
= 1;
3531 case SWM_ARG_ID_MASTERSHRINK
:
3532 if (ws
->l_state
.horizontal_msize
> 1)
3533 ws
->l_state
.horizontal_msize
--;
3535 case SWM_ARG_ID_MASTERGROW
:
3536 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3537 ws
->l_state
.horizontal_msize
++;
3539 case SWM_ARG_ID_MASTERADD
:
3540 ws
->l_state
.horizontal_mwin
++;
3542 case SWM_ARG_ID_MASTERDEL
:
3543 if (ws
->l_state
.horizontal_mwin
> 0)
3544 ws
->l_state
.horizontal_mwin
--;
3546 case SWM_ARG_ID_STACKINC
:
3547 ws
->l_state
.horizontal_stacks
++;
3549 case SWM_ARG_ID_STACKDEC
:
3550 if (ws
->l_state
.horizontal_stacks
> 1)
3551 ws
->l_state
.horizontal_stacks
--;
3553 case SWM_ARG_ID_FLIPLAYOUT
:
3554 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3562 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3564 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3566 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3569 /* fullscreen view */
3571 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3573 struct swm_geometry gg
= *g
;
3574 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3575 int winno
, num_screens
;
3577 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3582 winno
= count_win(ws
, 0);
3583 if (winno
== 0 && count_win(ws
, 1) == 0)
3586 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3587 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3588 if (win
->transient
) {
3590 parent
= find_window(win
->transient
);
3594 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3596 * retain geometry for retrieval on exit from
3599 store_float_geom(win
, ws
->r
);
3600 win
->floatmaxed
= 1;
3603 /* only reconfigure if necessary */
3604 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3605 HEIGHT(win
) != gg
.h
) {
3611 WIDTH(win
) += 2 * border_width
;
3612 HEIGHT(win
) += 2 * border_width
;
3617 /* unmap only if we don't have multi screen */
3618 if (win
!= ws
->focus
)
3619 if (!(num_screens
> 1 || outputs
> 1))
3623 /* put the last transient on top */
3626 map_window_raised(parent
->id
);
3627 stack_floater(wintrans
, ws
->r
);
3628 focus_magic(wintrans
);
3633 send_to_ws(struct swm_region
*r
, union arg
*args
)
3635 int wsid
= args
->id
;
3636 struct ws_win
*win
= NULL
, *parent
;
3637 struct workspace
*ws
, *nws
;
3638 char ws_idx_str
[SWM_PROPLEN
];
3641 if (wsid
>= workspace_limit
)
3644 if (r
&& r
->ws
&& r
->ws
->focus
)
3650 if (win
->ws
->idx
== wsid
)
3653 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%x\n", win
->id
);
3656 nws
= &win
->s
->ws
[wsid
];
3658 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3660 if (win
->transient
) {
3661 parent
= find_window(win
->transient
);
3663 unmap_window(parent
);
3664 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3665 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3670 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3671 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3672 if (TAILQ_EMPTY(&ws
->winlist
))
3673 r
->ws
->focus
= NULL
;
3676 /* Try to update the window's workspace property */
3677 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
3678 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3680 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
3681 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
3692 pressbutton(struct swm_region
*r
, union arg
*args
)
3694 /* suppress unused warning since var is needed */
3697 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
3698 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3699 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
3700 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3704 raise_toggle(struct swm_region
*r
, union arg
*args
)
3706 /* suppress unused warning since var is needed */
3709 if (r
== NULL
|| r
->ws
== NULL
)
3712 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3714 /* bring floaters back to top */
3715 if (r
->ws
->always_raise
== 0)
3722 iconify(struct swm_region
*r
, union arg
*args
)
3726 /* suppress unused warning since var is needed */
3729 if (r
->ws
->focus
== NULL
)
3731 unmap_window(r
->ws
->focus
);
3732 update_iconic(r
->ws
->focus
, 1);
3735 r
->ws
->focus
= NULL
;
3736 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3743 get_win_name(xcb_window_t win
)
3746 xcb_get_property_cookie_t c
;
3747 xcb_icccm_get_text_property_reply_t r
;
3749 c
= xcb_icccm_get_wm_name(conn
, win
);
3750 if (xcb_icccm_get_wm_name_reply(conn
, c
, &r
, NULL
)) {
3751 if (r
.name_len
> 0) {
3752 name
= malloc(r
.name_len
+ 1);
3754 xcb_icccm_get_text_property_reply_wipe(&r
);
3757 memcpy(name
, r
.name
, r
.name_len
);
3758 name
[r
.name_len
] = '\0';
3760 xcb_icccm_get_text_property_reply_wipe(&r
);
3767 uniconify(struct swm_region
*r
, union arg
*args
)
3774 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3776 if (r
== NULL
|| r
->ws
== NULL
)
3779 /* make sure we have anything to uniconify */
3780 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3781 if (win
->ws
== NULL
)
3782 continue; /* should never happen */
3783 if (win
->iconic
== 0)
3791 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3793 spawn_select(r
, args
, "search", &searchpid
);
3795 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3798 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3799 if (win
->ws
== NULL
)
3800 continue; /* should never happen */
3801 if (win
->iconic
== 0)
3804 name
= get_win_name(win
->id
);
3807 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
3817 name_workspace(struct swm_region
*r
, union arg
*args
)
3821 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3827 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3829 spawn_select(r
, args
, "name_workspace", &searchpid
);
3831 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3834 fprintf(lfile
, "%s", "");
3839 search_workspace(struct swm_region
*r
, union arg
*args
)
3842 struct workspace
*ws
;
3845 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3851 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3853 spawn_select(r
, args
, "search", &searchpid
);
3855 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3858 for (i
= 0; i
< workspace_limit
; i
++) {
3862 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3863 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3870 search_win_cleanup(void)
3872 struct search_window
*sw
= NULL
;
3874 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3875 xcb_destroy_window(conn
, sw
->indicator
);
3876 xcb_free_gc(conn
, sw
->gc
);
3877 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3883 search_win(struct swm_region
*r
, union arg
*args
)
3885 struct ws_win
*win
= NULL
;
3886 struct search_window
*sw
= NULL
;
3888 uint32_t gcv
[3], wa
[2];
3895 DNPRINTF(SWM_D_MISC
, "search_win\n");
3898 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3900 spawn_select(r
, args
, "search", &searchpid
);
3902 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3905 TAILQ_INIT(&search_wl
);
3908 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3909 if (win
->iconic
== 1)
3912 sw
= calloc(1, sizeof(struct search_window
));
3914 warn("search_win: calloc");
3916 search_win_cleanup();
3922 snprintf(s
, sizeof s
, "%d", i
);
3925 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
3927 w
= xcb_generate_id(conn
);
3928 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
3929 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
3930 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
3931 info
.width
+ 4, bar_font
->height
+ 4,
3932 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
3933 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
3936 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3938 sw
->gc
= xcb_generate_id(conn
);
3939 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
3940 gcv
[1] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
3942 xcb_create_gc(conn
, sw
->gc
, w
, XCB_GC_FOREGROUND
|
3943 XCB_GC_BACKGROUND
| XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
3944 map_window_raised(w
);
3946 xcb_image_text_8(conn
, len
, w
, sw
->gc
, 2, bar_font
->height
, s
);
3948 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
3950 fprintf(lfile
, "%d\n", i
);
3960 search_resp_uniconify(char *resp
, unsigned long len
)
3966 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3968 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3969 if (win
->iconic
== 0)
3971 name
= get_win_name(win
->id
);
3974 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
3979 if (strncmp(s
, resp
, len
) == 0) {
3980 /* XXX this should be a callback to generalize */
3981 update_iconic(win
, 0);
3990 search_resp_name_workspace(char *resp
, unsigned long len
)
3992 struct workspace
*ws
;
3994 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3996 if (search_r
->ws
== NULL
)
4001 free(search_r
->ws
->name
);
4002 search_r
->ws
->name
= NULL
;
4006 ws
->name
= strdup(resp
);
4007 if (ws
->name
== NULL
) {
4008 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
4009 "strdup: %s", strerror(errno
));
4016 search_resp_search_workspace(char *resp
)
4023 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
4027 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
4034 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
4036 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
4043 switchws(search_r
, &a
);
4047 search_resp_search_window(char *resp
)
4052 struct search_window
*sw
;
4054 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
4058 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
4063 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
4065 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
4072 TAILQ_FOREACH(sw
, &search_wl
, entry
)
4073 if (idx
== sw
->idx
) {
4079 #define MAX_RESP_LEN 1024
4082 search_do_resp(void)
4088 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
4093 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
4094 warn("search: calloc");
4098 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
4100 warn("search: read error");
4103 resp
[rbytes
] = '\0';
4106 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4107 * newline, so work around that by sanitizing the resp now.
4109 resp
[strcspn(resp
, "\n")] = '\0';
4112 switch (search_resp_action
) {
4113 case SWM_SEARCH_UNICONIFY
:
4114 search_resp_uniconify(resp
, len
);
4116 case SWM_SEARCH_NAME_WORKSPACE
:
4117 search_resp_name_workspace(resp
, len
);
4119 case SWM_SEARCH_SEARCH_WORKSPACE
:
4120 search_resp_search_workspace(resp
);
4122 case SWM_SEARCH_SEARCH_WINDOW
:
4123 search_resp_search_window(resp
);
4128 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4129 search_win_cleanup();
4131 search_resp_action
= SWM_SEARCH_NONE
;
4132 close(select_resp_pipe
[0]);
4137 wkill(struct swm_region
*r
, union arg
*args
)
4139 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4141 if (r
->ws
->focus
== NULL
)
4144 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4145 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4147 if (r
->ws
->focus
->can_delete
)
4148 client_msg(r
->ws
->focus
, a_delete
);
4155 floating_toggle_win(struct ws_win
*win
)
4157 struct swm_region
*r
;
4167 /* reject floating toggles in max stack mode */
4168 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4171 if (win
->floating
) {
4172 if (!win
->floatmaxed
) {
4173 /* retain position for refloat */
4174 store_float_geom(win
, r
);
4178 if (win
->g_floatvalid
) {
4179 /* refloat at last floating relative position */
4180 X(win
) = win
->g_float
.x
+ X(r
);
4181 Y(win
) = win
->g_float
.y
+ Y(r
);
4182 WIDTH(win
) = win
->g_float
.w
;
4183 HEIGHT(win
) = win
->g_float
.h
;
4188 ewmh_update_actions(win
);
4194 floating_toggle(struct swm_region
*r
, union arg
*args
)
4196 struct ws_win
*win
= r
->ws
->focus
;
4199 /* suppress unused warning since var is needed */
4205 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4206 _NET_WM_STATE_TOGGLE
);
4210 if (win
== win
->ws
->focus
) {
4211 a
.id
= SWM_ARG_ID_FOCUSCUR
;
4212 focus(win
->ws
->r
, &a
);
4216 event_drain(XCB_ENTER_NOTIFY
);
4220 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4222 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4224 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4226 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4229 if (X(win
) + BORDER(win
) < X(r
)) {
4231 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4233 X(win
) = X(r
) - BORDER(win
);
4236 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4238 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4240 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4243 if (Y(win
) + BORDER(win
) < Y(r
)) {
4245 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4247 Y(win
) = Y(r
) - BORDER(win
);
4253 if (HEIGHT(win
) < 1)
4259 update_window(struct ws_win
*win
)
4264 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
4265 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
4266 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
4270 wc
[3] = HEIGHT(win
);
4271 wc
[4] = BORDER(win
);
4273 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
4274 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
4275 wc
[3], YESNO(win
->bordered
));
4277 xcb_configure_window(conn
, win
->id
, mask
, wc
);
4280 #define SWM_RESIZE_STEPS (50)
4283 resize(struct ws_win
*win
, union arg
*args
)
4285 xcb_timestamp_t timestamp
= 0;
4286 struct swm_region
*r
= NULL
;
4287 int resize_step
= 0;
4288 struct swm_geometry g
;
4289 int top
= 0, left
= 0, resizing
;
4291 unsigned int shape
; /* cursor style */
4292 xcb_cursor_t cursor
;
4293 xcb_font_t cursor_font
;
4294 xcb_grab_pointer_cookie_t gpc
;
4295 xcb_grab_pointer_reply_t
*gpr
;
4296 xcb_query_pointer_reply_t
*xpr
;
4297 xcb_generic_event_t
*evt
;
4298 xcb_motion_notify_event_t
*mne
;
4304 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4307 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
4308 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
4311 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4314 /* reject resizes in max mode for floaters (transient ok) */
4315 if (win
->floatmaxed
)
4319 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4325 case SWM_ARG_ID_WIDTHSHRINK
:
4326 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4329 case SWM_ARG_ID_WIDTHGROW
:
4330 WIDTH(win
) += SWM_RESIZE_STEPS
;
4333 case SWM_ARG_ID_HEIGHTSHRINK
:
4334 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4337 case SWM_ARG_ID_HEIGHTGROW
:
4338 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4345 constrain_window(win
, r
, 1);
4347 store_float_geom(win
,r
);
4351 /* get cursor offset from window root */
4352 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4359 if (xpr
->win_x
< WIDTH(win
) / 2)
4362 if (xpr
->win_y
< HEIGHT(win
) / 2)
4365 if (args
->id
== SWM_ARG_ID_CENTER
)
4368 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
4370 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
4372 cursor_font
= xcb_generate_id(conn
);
4373 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4375 cursor
= xcb_generate_id(conn
);
4376 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4377 shape
, shape
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4379 gpc
= xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
4380 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
4382 gpr
= xcb_grab_pointer_reply(conn
, gpc
, NULL
);
4384 xcb_free_cursor(conn
, cursor
);
4385 xcb_close_font(conn
, cursor_font
);
4392 while ((evt
= xcb_wait_for_event(conn
)) && resizing
) {
4393 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4394 case XCB_BUTTON_RELEASE
:
4395 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
4398 case XCB_MOTION_NOTIFY
:
4399 mne
= (xcb_motion_notify_event_t
*)evt
;
4400 /* cursor offset/delta from start of the operation */
4401 dx
= mne
->root_x
- xpr
->root_x
;
4402 dy
= mne
->root_y
- xpr
->root_y
;
4408 if (args
->id
== SWM_ARG_ID_CENTER
) {
4409 if (g
.h
/ 2 + dy
< 1)
4413 HEIGHT(win
) = g
.h
+ 2 * dy
;
4421 HEIGHT(win
) = g
.h
+ dy
;
4428 if (args
->id
== SWM_ARG_ID_CENTER
) {
4429 if (g
.w
/ 2 + dx
< 1)
4433 WIDTH(win
) = g
.w
+ 2 * dx
;
4441 WIDTH(win
) = g
.w
+ dx
;
4444 constrain_window(win
, r
, 1);
4446 /* not free, don't sync more than 120 times / second */
4447 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4448 timestamp
= mne
->time
;
4463 store_float_geom(win
,r
);
4465 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4466 xcb_free_cursor(conn
, cursor
);
4467 xcb_close_font(conn
, cursor_font
);
4470 DNPRINTF(SWM_D_EVENT
, "resize: done\n");
4474 resize_step(struct swm_region
*r
, union arg
*args
)
4476 struct ws_win
*win
= NULL
;
4478 if (r
&& r
->ws
&& r
->ws
->focus
)
4486 #define SWM_MOVE_STEPS (50)
4489 move(struct ws_win
*win
, union arg
*args
)
4491 xcb_timestamp_t timestamp
= 0;
4492 int move_step
= 0, moving
;
4493 struct swm_region
*r
= NULL
;
4494 xcb_font_t cursor_font
;
4495 xcb_cursor_t cursor
;
4496 xcb_grab_pointer_cookie_t gpc
;
4497 xcb_grab_pointer_reply_t
*gpr
;
4498 xcb_query_pointer_reply_t
*qpr
;
4499 xcb_generic_event_t
*evt
;
4500 xcb_motion_notify_event_t
*mne
;
4506 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4509 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
4510 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4512 /* in max_stack mode should only move transients */
4513 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4517 if (win
->floating
== 0 && !win
->transient
) {
4518 store_float_geom(win
, r
);
4519 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4522 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4529 case SWM_ARG_ID_MOVELEFT
:
4530 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4533 case SWM_ARG_ID_MOVERIGHT
:
4534 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4537 case SWM_ARG_ID_MOVEUP
:
4538 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4541 case SWM_ARG_ID_MOVEDOWN
:
4542 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4549 constrain_window(win
, r
, 0);
4551 store_float_geom(win
, r
);
4555 cursor_font
= xcb_generate_id(conn
);
4556 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4558 cursor
= xcb_generate_id(conn
);
4559 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4560 XC_fleur
, XC_fleur
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4562 gpc
= xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
4563 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
4564 XCB_WINDOW_NONE
, cursor
, XCB_CURRENT_TIME
);
4565 gpr
= xcb_grab_pointer_reply(conn
, gpc
, NULL
);
4567 xcb_free_cursor(conn
, cursor
);
4568 xcb_close_font(conn
, cursor_font
);
4572 /* get cursor offset from window root */
4573 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4576 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4577 xcb_free_cursor(conn
, cursor
);
4578 xcb_close_font(conn
, cursor_font
);
4584 while ((evt
= xcb_wait_for_event(conn
)) && moving
) {
4585 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4586 case XCB_BUTTON_RELEASE
:
4587 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
4590 case XCB_MOTION_NOTIFY
:
4591 mne
= (xcb_motion_notify_event_t
*)evt
;
4592 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
4593 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
4595 constrain_window(win
, r
, 0);
4597 /* not free, don't sync more than 120 times / second */
4598 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4599 timestamp
= mne
->time
;
4614 store_float_geom(win
, r
);
4616 xcb_free_cursor(conn
, cursor
);
4617 xcb_close_font(conn
, cursor_font
);
4618 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4619 DNPRINTF(SWM_D_EVENT
, "move: done\n");
4623 move_step(struct swm_region
*r
, union arg
*args
)
4625 struct ws_win
*win
= NULL
;
4627 if (r
&& r
->ws
&& r
->ws
->focus
)
4632 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4639 /* user/key callable function IDs */
4689 KF_SEARCH_WORKSPACE
,
4730 KF_DUMPWINS
, /* MUST BE LAST */
4734 /* key definitions */
4736 char name
[SWM_FUNCNAME_LEN
];
4737 void (*func
)(struct swm_region
*r
, union arg
*);
4739 } keyfuncs
[KF_INVALID
+ 1] = {
4740 /* name function argument */
4741 { "bar_toggle", bar_toggle
, {0} },
4742 { "button2", pressbutton
, {2} },
4743 { "cycle_layout", cycle_layout
, {0} },
4744 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4745 { "float_toggle", floating_toggle
,{0} },
4746 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4747 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4748 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4749 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4750 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4751 { "iconify", iconify
, {0} },
4752 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4753 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4754 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4755 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4756 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4757 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4758 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4759 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4760 { "mvws_1", send_to_ws
, {.id
= 0} },
4761 { "mvws_2", send_to_ws
, {.id
= 1} },
4762 { "mvws_3", send_to_ws
, {.id
= 2} },
4763 { "mvws_4", send_to_ws
, {.id
= 3} },
4764 { "mvws_5", send_to_ws
, {.id
= 4} },
4765 { "mvws_6", send_to_ws
, {.id
= 5} },
4766 { "mvws_7", send_to_ws
, {.id
= 6} },
4767 { "mvws_8", send_to_ws
, {.id
= 7} },
4768 { "mvws_9", send_to_ws
, {.id
= 8} },
4769 { "mvws_10", send_to_ws
, {.id
= 9} },
4770 { "mvws_11", send_to_ws
, {.id
= 10} },
4771 { "mvws_12", send_to_ws
, {.id
= 11} },
4772 { "mvws_13", send_to_ws
, {.id
= 12} },
4773 { "mvws_14", send_to_ws
, {.id
= 13} },
4774 { "mvws_15", send_to_ws
, {.id
= 14} },
4775 { "mvws_16", send_to_ws
, {.id
= 15} },
4776 { "mvws_17", send_to_ws
, {.id
= 16} },
4777 { "mvws_18", send_to_ws
, {.id
= 17} },
4778 { "mvws_19", send_to_ws
, {.id
= 18} },
4779 { "mvws_20", send_to_ws
, {.id
= 19} },
4780 { "mvws_21", send_to_ws
, {.id
= 20} },
4781 { "mvws_22", send_to_ws
, {.id
= 21} },
4782 { "name_workspace", name_workspace
, {0} },
4783 { "quit", quit
, {0} },
4784 { "raise_toggle", raise_toggle
, {0} },
4785 { "restart", restart
, {0} },
4786 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4787 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4788 { "search_win", search_win
, {0} },
4789 { "search_workspace", search_workspace
, {0} },
4790 { "spawn_custom", NULL
, {0} },
4791 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4792 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4793 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4794 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4795 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4796 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4797 { "uniconify", uniconify
, {0} },
4798 { "version", version
, {0} },
4799 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4800 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4801 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4802 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4803 { "ws_1", switchws
, {.id
= 0} },
4804 { "ws_2", switchws
, {.id
= 1} },
4805 { "ws_3", switchws
, {.id
= 2} },
4806 { "ws_4", switchws
, {.id
= 3} },
4807 { "ws_5", switchws
, {.id
= 4} },
4808 { "ws_6", switchws
, {.id
= 5} },
4809 { "ws_7", switchws
, {.id
= 6} },
4810 { "ws_8", switchws
, {.id
= 7} },
4811 { "ws_9", switchws
, {.id
= 8} },
4812 { "ws_10", switchws
, {.id
= 9} },
4813 { "ws_11", switchws
, {.id
= 10} },
4814 { "ws_12", switchws
, {.id
= 11} },
4815 { "ws_13", switchws
, {.id
= 12} },
4816 { "ws_14", switchws
, {.id
= 13} },
4817 { "ws_15", switchws
, {.id
= 14} },
4818 { "ws_16", switchws
, {.id
= 15} },
4819 { "ws_17", switchws
, {.id
= 16} },
4820 { "ws_18", switchws
, {.id
= 17} },
4821 { "ws_19", switchws
, {.id
= 18} },
4822 { "ws_20", switchws
, {.id
= 19} },
4823 { "ws_21", switchws
, {.id
= 20} },
4824 { "ws_22", switchws
, {.id
= 21} },
4825 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4826 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4827 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4828 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4829 { "ws_prior", priorws
, {0} },
4830 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4831 { "invalid key func", NULL
, {0} },
4834 RB_ENTRY(key
) entry
;
4837 enum keyfuncid funcid
;
4840 RB_HEAD(key_tree
, key
);
4843 key_cmp(struct key
*kp1
, struct key
*kp2
)
4845 if (kp1
->keysym
< kp2
->keysym
)
4847 if (kp1
->keysym
> kp2
->keysym
)
4850 if (kp1
->mod
< kp2
->mod
)
4852 if (kp1
->mod
> kp2
->mod
)
4858 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
4859 struct key_tree keys
;
4862 enum { client_click
, root_click
};
4864 unsigned int action
;
4866 unsigned int button
;
4867 void (*func
)(struct ws_win
*, union arg
*);
4870 /* action key mouse button func args */
4871 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4872 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4873 { client_click
, MODKEY
, Button1
, move
, {0} },
4877 update_modkey(unsigned int mod
)
4883 RB_FOREACH(kp
, key_tree
, &keys
)
4884 if (kp
->mod
& ShiftMask
)
4885 kp
->mod
= mod
| ShiftMask
;
4889 for (i
= 0; i
< LENGTH(buttons
); i
++)
4890 if (buttons
[i
].mask
& ShiftMask
)
4891 buttons
[i
].mask
= mod
| ShiftMask
;
4893 buttons
[i
].mask
= mod
;
4898 TAILQ_ENTRY(spawn_prog
) entry
;
4903 TAILQ_HEAD(spawn_list
, spawn_prog
);
4904 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4907 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4910 struct spawn_prog
*prog
= NULL
;
4912 char *ap
, **real_args
;
4914 /* suppress unused warning since var is needed */
4917 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4920 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4921 if (!strcasecmp(spawn_name
, prog
->name
))
4925 warnx("spawn_custom: program %s not found", spawn_name
);
4929 /* make room for expanded args */
4930 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4931 err(1, "spawn_custom: calloc real_args");
4933 /* expand spawn_args into real_args */
4934 for (i
= 0; i
< prog
->argc
; i
++) {
4936 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4937 if (!strcasecmp(ap
, "$bar_border")) {
4939 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4941 err(1, "spawn_custom border color");
4942 } else if (!strcasecmp(ap
, "$bar_color")) {
4944 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4946 err(1, "spawn_custom bar color");
4947 } else if (!strcasecmp(ap
, "$bar_font")) {
4948 if ((real_args
[i
] = strdup(bar_fonts
))
4950 err(1, "spawn_custom bar fonts");
4951 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4953 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4955 err(1, "spawn_custom color font");
4956 } else if (!strcasecmp(ap
, "$color_focus")) {
4958 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4960 err(1, "spawn_custom color focus");
4961 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4963 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4965 err(1, "spawn_custom color unfocus");
4967 /* no match --> copy as is */
4968 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4969 err(1, "spawn_custom strdup(ap)");
4971 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4976 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4977 for (i
= 0; i
< prog
->argc
; i
++)
4978 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4979 DNPRINTF(SWM_D_SPAWN
, "\n");
4981 *ret_args
= real_args
;
4982 return (prog
->argc
);
4986 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4992 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4996 spawn(r
->ws
->idx
, &a
, 1);
4998 for (i
= 0; i
< spawn_argc
; i
++)
5004 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
5010 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5014 if (pipe(select_list_pipe
) == -1)
5015 err(1, "pipe error");
5016 if (pipe(select_resp_pipe
) == -1)
5017 err(1, "pipe error");
5019 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
5020 err(1, "could not disable SIGPIPE");
5021 switch (*pid
= fork()) {
5023 err(1, "cannot fork");
5026 if (dup2(select_list_pipe
[0], 0) == -1)
5028 if (dup2(select_resp_pipe
[1], 1) == -1)
5030 close(select_list_pipe
[1]);
5031 close(select_resp_pipe
[0]);
5032 spawn(r
->ws
->idx
, &a
, 0);
5034 default: /* parent */
5035 close(select_list_pipe
[0]);
5036 close(select_resp_pipe
[1]);
5040 for (i
= 0; i
< spawn_argc
; i
++)
5046 spawn_insert(char *name
, char *args
)
5048 char *arg
, *cp
, *ptr
;
5049 struct spawn_prog
*sp
;
5051 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
5053 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
5054 err(1, "spawn_insert: calloc");
5055 if ((sp
->name
= strdup(name
)) == NULL
)
5056 err(1, "spawn_insert: strdup");
5058 /* convert the arguments to an argument list */
5059 if ((ptr
= cp
= strdup(args
)) == NULL
)
5060 err(1, "spawn_insert: strdup");
5061 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
5062 /* empty field; skip it */
5067 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
5068 sizeof *sp
->argv
)) == NULL
)
5069 err(1, "spawn_insert: realloc");
5070 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
5071 err(1, "spawn_insert: strdup");
5075 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
5076 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
5080 spawn_remove(struct spawn_prog
*sp
)
5084 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
5086 TAILQ_REMOVE(&spawns
, sp
, entry
);
5087 for (i
= 0; i
< sp
->argc
; i
++)
5093 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
5097 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
5099 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
5102 spawn_insert(name
, args
);
5104 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
5108 setspawn(char *name
, char *args
)
5110 struct spawn_prog
*sp
;
5112 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5117 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5118 if (!strcmp(sp
->name
, name
)) {
5122 spawn_replace(sp
, name
, args
);
5123 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5127 if (*args
== '\0') {
5128 warnx("error: setspawn: cannot find program: %s", name
);
5132 spawn_insert(name
, args
);
5133 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5137 setconfspawn(char *selector
, char *value
, int flags
)
5139 /* suppress unused warning since var is needed */
5142 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
5144 setspawn(selector
, value
);
5146 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
5153 setconfspawn("term", "xterm", 0);
5154 setconfspawn("spawn_term", "xterm", 0);
5155 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5156 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5157 setconfspawn("lock", "xlock", 0);
5158 setconfspawn("initscr", "initscreen.sh", 0);
5159 setconfspawn("menu", "dmenu_run"
5162 " -nf $bar_font_color"
5164 " -sf $bar_color", 0);
5165 setconfspawn("search", "dmenu"
5169 " -nf $bar_font_color"
5171 " -sf $bar_color", 0);
5172 setconfspawn("name_workspace", "dmenu"
5176 " -nf $bar_font_color"
5178 " -sf $bar_color", 0);
5182 #define SWM_MODNAME_SIZE 32
5183 #define SWM_KEY_WS "\n+ \t"
5185 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5189 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5190 if (mod
== NULL
|| ks
== NULL
) {
5191 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5194 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5195 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5199 *ks
= XCB_NO_SYMBOL
;
5201 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5202 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5204 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5205 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5207 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5209 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5211 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5213 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5215 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5217 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5218 *mod
|= ControlMask
;
5220 *ks
= XStringToKeysym(name
);
5221 XConvertCase(*ks
, ks
, &uks
);
5222 if (ks
== XCB_NO_SYMBOL
) {
5224 "parsekeys: invalid key %s\n",
5230 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5235 strdupsafe(char *str
)
5240 return (strdup(str
));
5244 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5248 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5249 keyfuncs
[kfid
].name
, spawn_name
);
5251 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5252 err(1, "key_insert: malloc");
5257 kp
->spawn_name
= strdupsafe(spawn_name
);
5258 RB_INSERT(key_tree
, &keys
, kp
);
5260 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5264 key_lookup(unsigned int mod
, KeySym ks
)
5271 return (RB_FIND(key_tree
, &keys
, &kp
));
5275 key_remove(struct key
*kp
)
5277 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5279 RB_REMOVE(key_tree
, &keys
, kp
);
5280 free(kp
->spawn_name
);
5283 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5287 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5290 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5294 key_insert(mod
, ks
, kfid
, spawn_name
);
5296 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5300 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5305 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5306 keyfuncs
[kfid
].name
, spawn_name
);
5308 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5309 if (kfid
== KF_INVALID
)
5312 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5313 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5316 if (kfid
== KF_INVALID
) {
5317 warnx("error: setkeybinding: cannot find mod/key combination");
5318 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5322 key_insert(mod
, ks
, kfid
, spawn_name
);
5323 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5327 setconfbinding(char *selector
, char *value
, int flags
)
5329 enum keyfuncid kfid
;
5332 struct spawn_prog
*sp
;
5334 /* suppress unused warning since var is needed */
5337 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5338 if (selector
== NULL
) {
5339 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5340 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5342 setkeybinding(mod
, ks
, kfid
, NULL
);
5347 /* search by key function name */
5348 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
5349 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5350 SWM_FUNCNAME_LEN
) == 0) {
5351 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5353 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5354 setkeybinding(mod
, ks
, kfid
, NULL
);
5360 /* search by custom spawn name */
5361 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5362 if (strcasecmp(selector
, sp
->name
) == 0) {
5363 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5365 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5366 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
5373 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5380 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
5381 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
5382 setkeybinding(MODKEY
|ShiftMask
, XK_space
, KF_STACK_RESET
, NULL
);
5383 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
5384 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
5385 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
5386 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
5387 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, KF_STACK_INC
, NULL
);
5388 setkeybinding(MODKEY
|ShiftMask
, XK_period
, KF_STACK_DEC
, NULL
);
5389 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
5390 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
5391 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
5392 setkeybinding(MODKEY
|ShiftMask
, XK_j
, KF_SWAP_NEXT
, NULL
);
5393 setkeybinding(MODKEY
|ShiftMask
, XK_k
, KF_SWAP_PREV
, NULL
);
5394 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
5395 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
5396 setkeybinding(MODKEY
|ShiftMask
, XK_q
, KF_QUIT
, NULL
);
5397 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
5398 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
5399 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
5400 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
5401 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
5402 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
5403 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
5404 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
5405 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
5406 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
5407 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
5408 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
5409 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
5410 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
5411 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
5412 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
5413 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
5414 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
5415 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
5416 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
5417 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
5418 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
5419 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
5420 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
5421 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
5422 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
5423 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
5424 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
5425 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
5426 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, KF_SCREEN_NEXT
, NULL
);
5427 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, KF_SCREEN_PREV
, NULL
);
5428 setkeybinding(MODKEY
|ShiftMask
, XK_1
, KF_MVWS_1
, NULL
);
5429 setkeybinding(MODKEY
|ShiftMask
, XK_2
, KF_MVWS_2
, NULL
);
5430 setkeybinding(MODKEY
|ShiftMask
, XK_3
, KF_MVWS_3
, NULL
);
5431 setkeybinding(MODKEY
|ShiftMask
, XK_4
, KF_MVWS_4
, NULL
);
5432 setkeybinding(MODKEY
|ShiftMask
, XK_5
, KF_MVWS_5
, NULL
);
5433 setkeybinding(MODKEY
|ShiftMask
, XK_6
, KF_MVWS_6
, NULL
);
5434 setkeybinding(MODKEY
|ShiftMask
, XK_7
, KF_MVWS_7
, NULL
);
5435 setkeybinding(MODKEY
|ShiftMask
, XK_8
, KF_MVWS_8
, NULL
);
5436 setkeybinding(MODKEY
|ShiftMask
, XK_9
, KF_MVWS_9
, NULL
);
5437 setkeybinding(MODKEY
|ShiftMask
, XK_0
, KF_MVWS_10
, NULL
);
5438 setkeybinding(MODKEY
|ShiftMask
, XK_F1
, KF_MVWS_11
, NULL
);
5439 setkeybinding(MODKEY
|ShiftMask
, XK_F2
, KF_MVWS_12
, NULL
);
5440 setkeybinding(MODKEY
|ShiftMask
, XK_F3
, KF_MVWS_13
, NULL
);
5441 setkeybinding(MODKEY
|ShiftMask
, XK_F4
, KF_MVWS_14
, NULL
);
5442 setkeybinding(MODKEY
|ShiftMask
, XK_F5
, KF_MVWS_15
, NULL
);
5443 setkeybinding(MODKEY
|ShiftMask
, XK_F6
, KF_MVWS_16
, NULL
);
5444 setkeybinding(MODKEY
|ShiftMask
, XK_F7
, KF_MVWS_17
, NULL
);
5445 setkeybinding(MODKEY
|ShiftMask
, XK_F8
, KF_MVWS_18
, NULL
);
5446 setkeybinding(MODKEY
|ShiftMask
, XK_F9
, KF_MVWS_19
, NULL
);
5447 setkeybinding(MODKEY
|ShiftMask
, XK_F10
, KF_MVWS_20
, NULL
);
5448 setkeybinding(MODKEY
|ShiftMask
, XK_F11
, KF_MVWS_21
, NULL
);
5449 setkeybinding(MODKEY
|ShiftMask
, XK_F12
, KF_MVWS_22
, NULL
);
5450 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
5451 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
5452 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
5453 setkeybinding(MODKEY
|ShiftMask
, XK_x
, KF_WIND_KILL
, NULL
);
5454 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
5455 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
5456 setkeybinding(MODKEY
|ShiftMask
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
5457 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
5458 setkeybinding(MODKEY
|ShiftMask
, XK_v
, KF_VERSION
, NULL
);
5459 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
5460 setkeybinding(MODKEY
|ShiftMask
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
5461 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
5462 setkeybinding(MODKEY
|ShiftMask
, XK_w
, KF_UNICONIFY
, NULL
);
5463 setkeybinding(MODKEY
|ShiftMask
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
5464 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
5465 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
5466 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
5467 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
5468 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
5469 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
5470 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
5471 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
5472 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
5473 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
5474 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
5475 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
5477 setkeybinding(MODKEY
|ShiftMask
, XK_d
, KF_DUMPWINS
, NULL
);
5486 while (RB_EMPTY(&keys
) == 0) {
5487 kp
= RB_ROOT(&keys
);
5493 setkeymapping(char *selector
, char *value
, int flags
)
5495 char keymapping_file
[PATH_MAX
];
5497 /* suppress unused warnings since vars are needed */
5501 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5502 if (value
[0] == '~')
5503 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5504 pwd
->pw_dir
, &value
[1]);
5506 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5508 /* load new key bindings; if it fails, revert to default bindings */
5509 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5513 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5518 updatenumlockmask(void)
5521 xcb_get_modifier_mapping_reply_t
*modmap_r
;
5522 xcb_keycode_t
*modmap
, kc
;
5524 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
5527 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
5528 xcb_get_modifier_mapping(conn
),
5531 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
5532 for (i
= 0; i
< 8; i
++) {
5533 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
5534 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
5537 if (kc
== *((xcb_keycode_t
*)xcb_key_symbols_get_keycode(syms
,
5539 numlockmask
= (1 << i
);
5551 xcb_keycode_t
*code
;
5552 unsigned int modifiers
[] =
5553 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
5556 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5557 updatenumlockmask();
5559 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
5560 for (k
= 0; k
< num_screens
; k
++) {
5561 if (TAILQ_EMPTY(&screens
[k
].rl
))
5563 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
5565 RB_FOREACH(kp
, key_tree
, &keys
) {
5566 if ((code
= xcb_key_symbols_get_keycode(syms
,
5568 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5569 xcb_grab_key(conn
, 1,
5571 kp
->mod
| modifiers
[j
],
5572 *code
, XCB_GRAB_MODE_ASYNC
,
5573 XCB_GRAB_MODE_ASYNC
);
5579 grabbuttons(struct ws_win
*win
, int focused
)
5582 unsigned int modifiers
[] =
5583 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5585 updatenumlockmask();
5586 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, win
->id
,
5587 XCB_BUTTON_MASK_ANY
);
5589 for (i
= 0; i
< LENGTH(buttons
); i
++)
5590 if (buttons
[i
].action
== client_click
)
5591 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5592 xcb_grab_button(conn
, 0, win
->id
,
5594 XCB_GRAB_MODE_ASYNC
,
5601 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
5602 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_SYNC
, XCB_WINDOW_NONE
,
5603 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_ANY
, XCB_BUTTON_MASK_ANY
);
5606 const char *quirkname
[] = {
5607 "NONE", /* config string for "no value" */
5616 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5617 #define SWM_Q_WS "\n|+ \t"
5619 parsequirks(char *qstr
, unsigned long *quirk
)
5629 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5631 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5632 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5633 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5634 DNPRINTF(SWM_D_QUIRK
,
5635 "parsequirks: %s\n", name
);
5640 *quirk
|= 1 << (i
-1);
5644 if (i
>= LENGTH(quirkname
)) {
5645 DNPRINTF(SWM_D_QUIRK
,
5646 "parsequirks: invalid quirk [%s]\n", name
);
5654 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5658 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5661 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5662 err(1, "quirk_insert: malloc");
5663 if ((qp
->class = strdup(class)) == NULL
)
5664 err(1, "quirk_insert: strdup");
5665 if ((qp
->name
= strdup(name
)) == NULL
)
5666 err(1, "quirk_insert: strdup");
5669 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5671 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5675 quirk_remove(struct quirk
*qp
)
5677 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5678 qp
->name
, qp
->quirk
);
5680 TAILQ_REMOVE(&quirks
, qp
, entry
);
5685 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5689 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5690 unsigned long quirk
)
5692 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5693 qp
->name
, qp
->quirk
);
5696 quirk_insert(class, name
, quirk
);
5698 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5702 setquirk(const char *class, const char *name
, unsigned long quirk
)
5706 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5709 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5710 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5714 quirk_replace(qp
, class, name
, quirk
);
5715 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5720 warnx("error: setquirk: cannot find class/name combination");
5724 quirk_insert(class, name
, quirk
);
5725 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5729 setconfquirk(char *selector
, char *value
, int flags
)
5731 char *cp
, *class, *name
;
5733 unsigned long quirks
;
5735 /* suppress unused warning since var is needed */
5738 if (selector
== NULL
)
5740 if ((cp
= strchr(selector
, ':')) == NULL
)
5745 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5746 setquirk(class, name
, quirks
);
5753 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5754 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5755 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5756 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5757 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5758 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5759 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5760 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5761 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5762 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5763 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5764 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5765 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5768 /* conf file stuff */
5769 #define SWM_CONF_FILE "spectrwm.conf"
5770 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5774 SWM_S_BAR_AT_BOTTOM
,
5775 SWM_S_BAR_BORDER_WIDTH
,
5782 SWM_S_CLOCK_ENABLED
,
5785 SWM_S_CYCLE_VISIBLE
,
5787 SWM_S_DISABLE_BORDER
,
5789 SWM_S_FOCUS_CLOSE_WRAP
,
5790 SWM_S_FOCUS_DEFAULT
,
5796 SWM_S_STACK_ENABLED
,
5798 SWM_S_TITLE_CLASS_ENABLED
,
5799 SWM_S_TITLE_NAME_ENABLED
,
5800 SWM_S_URGENT_ENABLED
,
5801 SWM_S_VERBOSE_LAYOUT
,
5802 SWM_S_WINDOW_NAME_ENABLED
,
5803 SWM_S_WORKSPACE_LIMIT
5807 setconfvalue(char *selector
, char *value
, int flags
)
5812 /* suppress unused warning since var is needed */
5816 case SWM_S_BAR_ACTION
:
5818 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5819 err(1, "setconfvalue: bar_action");
5821 case SWM_S_BAR_AT_BOTTOM
:
5822 bar_at_bottom
= atoi(value
);
5824 case SWM_S_BAR_BORDER_WIDTH
:
5825 bar_border_width
= atoi(value
);
5826 if (bar_border_width
< 0)
5827 bar_border_width
= 0;
5829 case SWM_S_BAR_DELAY
:
5830 bar_delay
= atoi(value
);
5832 case SWM_S_BAR_ENABLED
:
5833 bar_enabled
= atoi(value
);
5835 case SWM_S_BAR_FONT
:
5837 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5838 err(1, "setconfvalue: asprintf: failed to allocate "
5839 "memory for bar_fonts.");
5842 case SWM_S_BAR_FORMAT
:
5844 if ((bar_format
= strdup(value
)) == NULL
)
5845 err(1, "setconfvalue: bar_format");
5847 case SWM_S_BAR_JUSTIFY
:
5848 if (!strcmp(value
, "left"))
5849 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5850 else if (!strcmp(value
, "center"))
5851 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5852 else if (!strcmp(value
, "right"))
5853 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5855 errx(1, "invalid bar_justify");
5857 case SWM_S_BORDER_WIDTH
:
5858 border_width
= atoi(value
);
5859 if (border_width
< 0)
5862 case SWM_S_CLOCK_ENABLED
:
5863 clock_enabled
= atoi(value
);
5865 case SWM_S_CLOCK_FORMAT
:
5866 #ifndef SWM_DENY_CLOCK_FORMAT
5868 if ((clock_format
= strdup(value
)) == NULL
)
5869 err(1, "setconfvalue: clock_format");
5872 case SWM_S_CYCLE_EMPTY
:
5873 cycle_empty
= atoi(value
);
5875 case SWM_S_CYCLE_VISIBLE
:
5876 cycle_visible
= atoi(value
);
5878 case SWM_S_DIALOG_RATIO
:
5879 dialog_ratio
= atof(value
);
5880 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5883 case SWM_S_DISABLE_BORDER
:
5884 disable_border
= atoi(value
);
5886 case SWM_S_FOCUS_CLOSE
:
5887 if (!strcmp(value
, "first"))
5888 focus_close
= SWM_STACK_BOTTOM
;
5889 else if (!strcmp(value
, "last"))
5890 focus_close
= SWM_STACK_TOP
;
5891 else if (!strcmp(value
, "next"))
5892 focus_close
= SWM_STACK_ABOVE
;
5893 else if (!strcmp(value
, "previous"))
5894 focus_close
= SWM_STACK_BELOW
;
5896 errx(1, "focus_close");
5898 case SWM_S_FOCUS_CLOSE_WRAP
:
5899 focus_close_wrap
= atoi(value
);
5901 case SWM_S_FOCUS_DEFAULT
:
5902 if (!strcmp(value
, "last"))
5903 focus_default
= SWM_STACK_TOP
;
5904 else if (!strcmp(value
, "first"))
5905 focus_default
= SWM_STACK_BOTTOM
;
5907 errx(1, "focus_default");
5909 case SWM_S_FOCUS_MODE
:
5910 if (!strcmp(value
, "default"))
5911 focus_mode
= SWM_FOCUS_DEFAULT
;
5912 else if (!strcmp(value
, "follow_cursor"))
5913 focus_mode
= SWM_FOCUS_FOLLOW
;
5914 else if (!strcmp(value
, "synergy"))
5915 focus_mode
= SWM_FOCUS_SYNERGY
;
5917 errx(1, "focus_mode");
5919 case SWM_S_SPAWN_ORDER
:
5920 if (!strcmp(value
, "first"))
5921 spawn_position
= SWM_STACK_BOTTOM
;
5922 else if (!strcmp(value
, "last"))
5923 spawn_position
= SWM_STACK_TOP
;
5924 else if (!strcmp(value
, "next"))
5925 spawn_position
= SWM_STACK_ABOVE
;
5926 else if (!strcmp(value
, "previous"))
5927 spawn_position
= SWM_STACK_BELOW
;
5929 errx(1, "spawn_position");
5931 case SWM_S_SPAWN_TERM
:
5932 setconfspawn("term", value
, 0);
5933 setconfspawn("spawn_term", value
, 0);
5937 case SWM_S_SS_ENABLED
:
5938 ss_enabled
= atoi(value
);
5940 case SWM_S_STACK_ENABLED
:
5941 stack_enabled
= atoi(value
);
5943 case SWM_S_TERM_WIDTH
:
5944 term_width
= atoi(value
);
5948 case SWM_S_TITLE_CLASS_ENABLED
:
5949 title_class_enabled
= atoi(value
);
5951 case SWM_S_TITLE_NAME_ENABLED
:
5952 title_name_enabled
= atoi(value
);
5954 case SWM_S_URGENT_ENABLED
:
5955 urgent_enabled
= atoi(value
);
5957 case SWM_S_VERBOSE_LAYOUT
:
5958 verbose_layout
= atoi(value
);
5959 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5961 layouts
[i
].l_string
= fancy_stacker
;
5963 layouts
[i
].l_string
= plain_stacker
;
5966 case SWM_S_WINDOW_NAME_ENABLED
:
5967 window_name_enabled
= atoi(value
);
5969 case SWM_S_WORKSPACE_LIMIT
:
5970 workspace_limit
= atoi(value
);
5971 if (workspace_limit
> SWM_WS_MAX
)
5972 workspace_limit
= SWM_WS_MAX
;
5973 else if (workspace_limit
< 1)
5974 workspace_limit
= 1;
5983 setconfmodkey(char *selector
, char *value
, int flags
)
5985 /* suppress unused warnings since vars are needed */
5989 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5990 update_modkey(Mod1Mask
);
5991 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5992 update_modkey(Mod2Mask
);
5993 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5994 update_modkey(Mod3Mask
);
5995 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5996 update_modkey(Mod4Mask
);
6003 setconfcolor(char *selector
, char *value
, int flags
)
6005 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
6010 setconfregion(char *selector
, char *value
, int flags
)
6012 /* suppress unused warnings since vars are needed */
6016 custom_region(value
);
6021 setautorun(char *selector
, char *value
, int flags
)
6031 /* suppress unused warnings since vars are needed */
6035 if (getenv("SWM_STARTED"))
6039 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
6040 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6042 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6043 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
6046 * This is a little intricate
6048 * If the pid already exists we simply reuse it because it means it was
6049 * used before AND not claimed by manage_window. We get away with
6050 * altering it in the parent after INSERT because this can not be a race
6053 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
6056 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
6058 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
6059 err(1, "setautorun: realloc");
6060 a
.argv
[argc
- 1] = ap
;
6063 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
6064 err(1, "setautorun: realloc");
6065 a
.argv
[argc
] = NULL
;
6067 if ((pid
= fork()) == 0) {
6068 spawn(ws_id
, &a
, 1);
6077 p
= calloc(1, sizeof *p
);
6080 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
6090 setlayout(char *selector
, char *value
, int flags
)
6092 int ws_id
, i
, x
, mg
, ma
, si
, raise
, f
= 0;
6093 int st
= SWM_V_STACK
, num_screens
;
6095 struct workspace
*ws
;
6097 /* suppress unused warnings since vars are needed */
6101 if (getenv("SWM_STARTED"))
6105 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
6106 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
6107 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6108 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6111 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6112 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
6114 if (!strcasecmp(s
, "vertical"))
6116 else if (!strcasecmp(s
, "vertical_flip")) {
6119 } else if (!strcasecmp(s
, "horizontal"))
6121 else if (!strcasecmp(s
, "horizontal_flip")) {
6124 } else if (!strcasecmp(s
, "fullscreen"))
6127 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6128 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6131 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6132 for (i
= 0; i
< num_screens
; i
++) {
6133 ws
= (struct workspace
*)&screens
[i
].ws
;
6134 ws
[ws_id
].cur_layout
= &layouts
[st
];
6136 ws
[ws_id
].always_raise
= raise
;
6137 if (st
== SWM_MAX_STACK
)
6141 for (x
= 0; x
< abs(mg
); x
++) {
6142 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6143 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
6144 SWM_ARG_ID_MASTERSHRINK
);
6148 for (x
= 0; x
< abs(ma
); x
++) {
6149 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6150 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
6151 SWM_ARG_ID_MASTERDEL
);
6155 for (x
= 0; x
< abs(si
); x
++) {
6156 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6157 si
>= 0 ? SWM_ARG_ID_STACKINC
:
6158 SWM_ARG_ID_STACKDEC
);
6163 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6164 SWM_ARG_ID_FLIPLAYOUT
);
6172 /* config options */
6173 struct config_option
{
6175 int (*func
)(char*, char*, int);
6178 struct config_option configopt
[] = {
6179 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
6180 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
6181 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
6182 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
6183 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
6184 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
6185 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
6186 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
6187 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
6188 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
6189 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
6190 { "keyboard_mapping", setkeymapping
, 0 },
6191 { "bind", setconfbinding
, 0 },
6192 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
6193 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
6194 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
6195 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
6196 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
6197 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
6198 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
6199 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
6200 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
6201 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
6202 { "modkey", setconfmodkey
, 0 },
6203 { "program", setconfspawn
, 0 },
6204 { "quirk", setconfquirk
, 0 },
6205 { "region", setconfregion
, 0 },
6206 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
6207 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
6208 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
6209 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
6210 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
6211 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
6212 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6213 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6214 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6215 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6216 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6217 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6218 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6219 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6220 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6221 { "autorun", setautorun
, 0 },
6222 { "layout", setlayout
, 0 },
6227 conf_load(char *filename
, int keymapping
)
6230 char *line
, *cp
, *optsub
, *optval
;
6231 size_t linelen
, lineno
= 0;
6232 int wordlen
, i
, optind
;
6233 struct config_option
*opt
;
6235 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6237 if (filename
== NULL
) {
6238 warnx("conf_load: no filename");
6241 if ((config
= fopen(filename
, "r")) == NULL
) {
6242 warn("conf_load: fopen: %s", filename
);
6246 while (!feof(config
)) {
6247 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6250 err(1, "%s", filename
);
6255 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6256 if (cp
[0] == '\0') {
6261 /* get config option */
6262 wordlen
= strcspn(cp
, "=[ \t\n");
6264 warnx("%s: line %zd: no option found",
6269 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6270 opt
= &configopt
[i
];
6271 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6272 (int)strlen(opt
->optname
) == wordlen
) {
6278 warnx("%s: line %zd: unknown option %.*s",
6279 filename
, lineno
, wordlen
, cp
);
6282 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
6283 warnx("%s: line %zd: invalid option %.*s",
6284 filename
, lineno
, wordlen
, cp
);
6288 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6289 /* get [selector] if any */
6293 wordlen
= strcspn(cp
, "]");
6296 warnx("%s: line %zd: syntax error",
6301 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6303 warnx("%s: line %zd: unable to allocate"
6304 "memory for selector", filename
,
6310 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6312 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6314 optval
= strdup(cp
);
6315 /* call function to deal with it all */
6316 if (configopt
[optind
].func(optsub
, optval
,
6317 configopt
[optind
].funcflags
) != 0)
6318 errx(1, "%s: line %zd: invalid data for %s",
6319 filename
, lineno
, configopt
[optind
].optname
);
6326 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6333 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6339 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
6341 struct ws_win
*parent
, *w
;
6342 struct swm_region
*r
;
6343 struct workspace
*ws
;
6344 xcb_icccm_wm_hints_t wmh
;
6346 parent
= find_window(win
->transient
);
6348 parent
->child_trans
= win
;
6350 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6351 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
6353 r
= root_to_region(win
->wa
->root
);
6355 /* parent doen't exist in our window list */
6356 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6357 if (xcb_icccm_get_wm_hints_reply(conn
,
6358 xcb_icccm_get_wm_hints(conn
, w
->id
),
6360 warnx("can't get hints for 0x%x", w
->id
);
6364 if (win
->hints
.window_group
!= wmh
.window_group
)
6367 w
->child_trans
= win
;
6368 win
->transient
= w
->id
;
6370 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
6371 "transient to 0x%x\n", win
->transient
);
6378 window_get_pid(xcb_window_t win
)
6383 xcb_get_property_cookie_t pc
;
6384 xcb_get_property_reply_t
*pr
;
6386 apid
= get_atom_from_string("_NET_WM_PID");
6387 if (apid
== XCB_ATOM_NONE
)
6390 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
6391 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6394 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
6399 ret
= *((pid_t
*)xcb_get_property_value(pr
));
6405 apid
= get_atom_from_string("_SWM_PID");
6406 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
6408 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6411 if (pr
->type
!= XCB_ATOM_STRING
) {
6416 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
6423 get_ws_idx(xcb_window_t id
)
6429 xcb_get_property_reply_t
*gpr
;
6431 gpr
= xcb_get_property_reply(conn
,
6432 xcb_get_property(conn
, 0, id
, a_swm_ws
,
6433 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
6436 proplen
= xcb_get_property_value_length(gpr
);
6438 prop
= malloc(proplen
+ 1);
6441 xcb_get_property_value(gpr
),
6443 prop
[proplen
] = '\0';
6450 DNPRINTF(SWM_D_PROP
, "get_ws_idx: _SWM_WS: %s\n", prop
);
6451 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
6453 DNPRINTF(SWM_D_PROP
, "get_ws_idx: window: #%s: %s",
6462 manage_window(xcb_window_t id
)
6464 xcb_window_t trans
= XCB_WINDOW_NONE
;
6465 struct workspace
*ws
;
6466 struct ws_win
*win
, *ww
;
6467 int ws_idx
, border_me
= 0;
6468 char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
6469 struct swm_region
*r
;
6472 uint32_t event_mask
, i
;
6473 xcb_icccm_get_wm_protocols_reply_t wpr
;
6475 if ((win
= find_window(id
)) != NULL
)
6476 return (win
); /* already being managed */
6478 /* see if we are on the unmanaged list */
6479 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6480 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
6481 "window: 0x%x\n", win
->id
);
6482 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6484 set_child_transient(win
, &trans
);
6486 if (trans
&& (ww
= find_window(trans
)))
6487 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
6488 else if ((ww
= win
->ws
->focus
) &&
6489 spawn_position
== SWM_STACK_ABOVE
)
6490 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
,
6492 else if (ww
&& spawn_position
== SWM_STACK_BELOW
)
6493 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6494 else switch (spawn_position
) {
6497 case SWM_STACK_ABOVE
:
6498 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6500 case SWM_STACK_BOTTOM
:
6501 case SWM_STACK_BELOW
:
6502 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6505 ewmh_update_actions(win
);
6509 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6510 err(1, "manage_window: calloc: failed to allocate memory for "
6516 /* see if we need to override the workspace */
6517 p
= find_pid(window_get_pid(id
));
6519 /* Get all the window data in one shot */
6520 ws_idx
= get_ws_idx(id
);
6522 win
->wa
= xcb_get_geometry_reply(conn
,
6523 xcb_get_geometry(conn
, id
),
6525 xcb_icccm_get_wm_normal_hints_reply(conn
,
6526 xcb_icccm_get_wm_normal_hints(conn
, id
),
6528 xcb_icccm_get_wm_hints_reply(conn
,
6529 xcb_icccm_get_wm_hints(conn
, id
),
6531 xcb_icccm_get_wm_transient_for_reply(conn
,
6532 xcb_icccm_get_wm_transient_for(conn
, id
),
6535 win
->transient
= trans
;
6536 set_child_transient(win
, &trans
);
6537 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%x, "
6538 "transient: 0x%x\n", win
->id
, win
->transient
);
6541 /* get supported protocols */
6542 if (xcb_icccm_get_wm_protocols_reply(conn
,
6543 xcb_icccm_get_wm_protocols(conn
, id
, a_prot
),
6545 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
6546 if (wpr
.atoms
[i
] == a_takefocus
)
6547 win
->take_focus
= 1;
6548 if (wpr
.atoms
[i
] == a_delete
)
6549 win
->can_delete
= 1;
6551 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
6554 win
->iconic
= get_iconic(win
);
6557 * Figure out where to put the window. If it was previously assigned to
6558 * a workspace (either by spawn() or manually moving), and isn't
6559 * transient, * put it in the same workspace
6561 r
= root_to_region(win
->wa
->root
);
6563 ws
= &r
->s
->ws
[p
->ws
];
6564 TAILQ_REMOVE(&pidlist
, p
, entry
);
6567 } else if (ws_idx
!= -1 && win
->transient
== 0) {
6568 ws
= &r
->s
->ws
[ws_idx
];
6571 /* this should launch transients in the same ws as parent */
6573 if ((ww
= find_window(trans
)) != NULL
)
6579 warnx("manage_window: fix this "
6585 /* set up the window layout */
6588 win
->s
= r
->s
; /* this never changes */
6589 if (trans
&& (ww
= find_window(trans
)))
6590 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
6591 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
6592 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
6594 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
6595 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6596 else switch (spawn_position
) {
6599 case SWM_STACK_ABOVE
:
6600 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6602 case SWM_STACK_BOTTOM
:
6603 case SWM_STACK_BELOW
:
6604 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6607 /* ignore window border if there is one. */
6608 WIDTH(win
) = win
->wa
->width
;
6609 HEIGHT(win
) = win
->wa
->height
;
6610 X(win
) = win
->wa
->x
+ win
->wa
->border_width
;
6611 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
;
6613 win
->g_floatvalid
= 0;
6614 win
->floatmaxed
= 0;
6615 win
->ewmh_flags
= 0;
6617 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%x, (x,y) w x h: "
6618 "(%d,%d) %d x %d, ws: %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
6619 HEIGHT(win
), ws
->idx
);
6621 constrain_window(win
, r
, 0);
6623 /* Set window properties so we can remember this after reincarnation */
6624 if (prop
== NULL
&& snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
6626 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6628 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
6629 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
6635 ewmh_autoquirk(win
);
6637 if (xcb_icccm_get_wm_class_reply(conn
,
6638 xcb_icccm_get_wm_class(conn
, win
->id
),
6640 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6641 win
->ch
.class_name
, win
->ch
.instance_name
);
6643 /* java is retarded so treat it special */
6644 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
6649 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6650 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
6651 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
6652 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
6653 "class: %s, name: %s\n", win
->ch
.class_name
,
6654 win
->ch
.instance_name
);
6655 if (qp
->quirk
& SWM_Q_FLOAT
) {
6659 win
->quirks
= qp
->quirk
;
6664 /* alter window position if quirky */
6665 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6666 win
->manual
= 1; /* don't center the quirky windows */
6667 if (bar_enabled
&& Y(win
) < bar_height
)
6668 Y(win
) = bar_height
;
6669 if (WIDTH(win
) + X(win
) > WIDTH(r
))
6670 X(win
) = WIDTH(r
) - WIDTH(win
) - 2;
6674 /* Reset font sizes (the bruteforce way; no default keybinding). */
6675 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6676 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6677 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
6678 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6679 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
6682 ewmh_get_win_state(win
);
6683 ewmh_update_actions(win
);
6684 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
6689 X(win
) -= border_width
;
6690 Y(win
) -= border_width
;
6694 event_mask
= XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
|
6695 XCB_EVENT_MASK_PROPERTY_CHANGE
| XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
6697 xcb_change_window_attributes(conn
, id
, XCB_CW_EVENT_MASK
, &event_mask
);
6699 /* floaters need to be mapped if they are in the current workspace */
6700 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
6701 map_window_raised(win
->id
);
6707 free_window(struct ws_win
*win
)
6709 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
6714 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6719 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
6724 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6727 DNPRINTF(SWM_D_MISC
, "free_window: done\n");
6731 unmanage_window(struct ws_win
*win
)
6733 struct ws_win
*parent
;
6734 xcb_screen_t
*screen
;
6739 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
6741 if (win
->transient
) {
6742 parent
= find_window(win
->transient
);
6744 parent
->child_trans
= NULL
;
6747 /* focus on root just in case */
6748 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
6749 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
6750 screen
->root
, XCB_CURRENT_TIME
);
6754 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
6755 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6759 focus_magic(struct ws_win
*win
)
6761 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%x\n", WINID(win
));
6764 /* if there are no windows clear the status-bar */
6769 if (win
->child_trans
) {
6770 /* win = parent & has a transient so focus on that */
6772 focus_win(win
->child_trans
);
6773 if (win
->child_trans
->take_focus
)
6774 client_msg(win
, a_takefocus
);
6776 /* make sure transient hasn't disappeared */
6777 if (validate_win(win
->child_trans
) == 0) {
6778 focus_win(win
->child_trans
);
6779 if (win
->child_trans
->take_focus
)
6780 client_msg(win
->child_trans
,
6783 win
->child_trans
= NULL
;
6785 if (win
->take_focus
)
6786 client_msg(win
, a_takefocus
);
6792 if (win
->take_focus
)
6793 client_msg(win
, a_takefocus
);
6798 expose(xcb_expose_event_t
*e
)
6801 struct swm_region
*r
;
6803 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
6805 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6806 for (i
= 0; i
< num_screens
; i
++)
6807 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6808 if (e
->window
== WINID(r
->bar
))
6815 keypress(xcb_key_press_event_t
*e
)
6817 xcb_keysym_t keysym
;
6820 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
6822 DNPRINTF(SWM_D_EVENT
, "keypress: %u %u\n", e
->detail
, keysym
);
6824 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
6827 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
6828 spawn_custom(root_to_region(e
->root
),
6829 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
6830 else if (keyfuncs
[kp
->funcid
].func
)
6831 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
),
6832 &(keyfuncs
[kp
->funcid
].args
));
6836 buttonpress(xcb_button_press_event_t
*e
)
6840 unsigned int action
;
6842 DNPRINTF(SWM_D_EVENT
, "buttonpress: window 0x%x, detail: %u\n",
6843 e
->event
, e
->detail
);
6845 if ((win
= find_window(e
->event
)) == NULL
)
6849 action
= client_click
;
6851 for (i
= 0; i
< LENGTH(buttons
); i
++)
6852 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6853 buttons
[i
].button
== e
->detail
&&
6854 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
))
6855 buttons
[i
].func(win
, &buttons
[i
].args
);
6862 print_win_geom(xcb_window_t w
)
6864 xcb_get_geometry_reply_t
*wa
;
6866 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
6869 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: 0x%x\n",
6874 DNPRINTF(SWM_D_MISC
, "print_win_geom: window: 0x%x, root: 0x%x, "
6875 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
6876 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
6884 configurerequest(xcb_configure_request_event_t
*e
)
6889 uint32_t wc
[7] = {0};
6891 if ((win
= find_window(e
->window
)) == NULL
)
6892 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
6895 print_win_geom(e
->window
);
6898 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: 0x%x, "
6899 "value_mask: 0x%x", e
->window
, e
->value_mask
);
6900 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
6901 mask
|= XCB_CONFIG_WINDOW_X
;
6903 DPRINTF(", X: %d", e
->x
);
6905 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
6906 mask
|= XCB_CONFIG_WINDOW_Y
;
6908 DPRINTF(", Y: %d", e
->y
);
6910 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
6911 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
6913 DPRINTF(", W: %u", e
->width
);
6915 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
6916 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
6917 wc
[i
++] = e
->height
;
6918 DPRINTF(", H: %u", e
->height
);
6920 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
6921 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6922 wc
[i
++] = e
->border_width
;
6923 DPRINTF(", Border: %u", e
->border_width
);
6925 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
6926 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
6927 wc
[i
++] = e
->sibling
;
6928 DPRINTF(", Sibling: 0x%x", e
->sibling
);
6930 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
6931 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
6932 wc
[i
++] = e
->stack_mode
;
6933 DPRINTF(", StackMode: %u", e
->stack_mode
);
6937 xcb_configure_window(conn
, e
->window
, mask
, wc
);
6939 DPRINTF(", Sent: %s\n", YESNO((mask
!= 0)));
6940 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
6941 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
6942 win
->g_float
.x
= e
->x
- X(win
->ws
->r
);
6943 win
->g_float
.y
= e
->y
- Y(win
->ws
->r
);
6944 win
->g_float
.w
= e
->width
;
6945 win
->g_float
.h
= e
->height
;
6946 win
->g_floatvalid
= 1;
6948 if (win
->floating
) {
6949 win
->g
= win
->g_float
;
6950 win
->g
.x
+= X(win
->ws
->r
);
6951 win
->g
.y
+= Y(win
->ws
->r
);
6964 configurenotify(xcb_configure_notify_event_t
*e
)
6968 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%x\n",
6971 win
= find_window(e
->window
);
6973 xcb_icccm_get_wm_normal_hints_reply(conn
,
6974 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
6983 destroynotify(xcb_destroy_notify_event_t
*e
)
6987 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
6989 if ((win
= find_window(e
->window
)) == NULL
) {
6990 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
6996 /* make sure we focus on something */
6999 unmanage_window(win
);
7005 enternotify(xcb_enter_notify_event_t
*e
)
7008 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%x, mode: %d, detail: "
7009 "%d, root: 0x%x, subwindow: 0x%x, same_screen_focus: %s, "
7010 "state: %d\n", e
->event
, e
->mode
, e
->detail
, e
->root
,
7011 e
->child
, YESNO(e
->same_screen_focus
), e
->state
);
7013 if (e
->mode
!= XCB_NOTIFY_MODE_NORMAL
) {
7014 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
7019 switch (focus_mode
) {
7020 case SWM_FOCUS_DEFAULT
:
7022 case SWM_FOCUS_FOLLOW
:
7024 case SWM_FOCUS_SYNERGY
:
7028 if ((win
= find_window(e
->event
)) == NULL
) {
7029 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
7038 /* lets us use one switch statement for arbitrary mode/detail combinations */
7039 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7042 mapnotify(xcb_map_notify_event_t
*e
)
7046 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
7048 win
= manage_window(e
->window
);
7050 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
7053 * focus_win can only set input focus on a mapped window.
7054 * make sure the window really has focus since it is just being mapped.
7056 if (win
->ws
->focus
== win
)
7061 mappingnotify(xcb_mapping_notify_event_t
*e
)
7063 xcb_refresh_keyboard_mapping(syms
, e
);
7065 if (e
->request
== XCB_MAPPING_KEYBOARD
)
7070 maprequest(xcb_map_request_event_t
*e
)
7073 struct swm_region
*r
;
7074 xcb_get_window_attributes_reply_t
*war
;
7076 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%x\n",
7079 war
= xcb_get_window_attributes_reply(conn
,
7080 xcb_get_window_attributes(conn
, e
->window
),
7084 if (war
->override_redirect
) {
7090 win
= manage_window(e
->window
);
7092 return; /* can't happen */
7097 /* make new win focused */
7098 r
= root_to_region(win
->wa
->root
);
7099 if (win
->ws
== r
->ws
)
7105 get_atom_name(xcb_atom_t atom
)
7109 xcb_get_atom_name_reply_t
*r
;
7111 r
= xcb_get_atom_name_reply(conn
,
7112 xcb_get_atom_name(conn
, atom
),
7115 len
= xcb_get_atom_name_name_length(r
);
7117 name
= malloc(len
+ 1);
7119 memcpy(name
, xcb_get_atom_name_name(r
), len
);
7133 propertynotify(xcb_property_notify_event_t
*e
)
7139 name
= get_atom_name(e
->atom
);
7140 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%x, atom: %s(%u)\n",
7141 e
->window
, name
, e
->atom
);
7145 win
= find_window(e
->window
);
7149 if (e
->state
== XCB_PROPERTY_DELETE
&& e
->atom
== a_swm_iconic
) {
7150 update_iconic(win
, 0);
7151 map_window_raised(win
->id
);
7158 case XCB_ATOM_WM_CLASS
:
7159 case XCB_ATOM_WM_NAME
:
7168 unmapnotify(xcb_unmap_notify_event_t
*e
)
7172 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
7174 /* determine if we need to help unmanage this window */
7175 win
= find_window(e
->window
);
7179 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
) {
7180 unmanage_window(win
);
7183 /* resend unmap because we ated it */
7184 xcb_unmap_window(conn
, e
->window
);
7191 visibilitynotify(xcb_visibility_notify_event_t
*e
)
7193 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%x\n",
7199 clientmessage(xcb_client_message_event_t
*e
)
7202 xcb_map_request_event_t mre
;
7206 name
= get_atom_name(e
->type
);
7207 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7208 e
->window
, name
, e
->type
);
7211 win
= find_window(e
->window
);
7214 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7215 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
7216 "unmanaged window.\n");
7217 mre
.window
= e
->window
;
7223 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7224 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7227 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7228 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7229 if (win
->can_delete
)
7230 client_msg(win
, a_delete
);
7232 xcb_kill_client(conn
, win
->id
);
7234 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7235 DNPRINTF(SWM_D_EVENT
,
7236 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7237 if (win
->floating
) {
7238 if (e
->data
.data32
[0] & (1<<8)) /* x */
7239 X(win
) = e
->data
.data32
[1];
7240 if (e
->data
.data32
[0] & (1<<9)) /* y */
7241 Y(win
) = e
->data
.data32
[2];
7242 if (e
->data
.data32
[0] & (1<<10)) /* width */
7243 WIDTH(win
) = e
->data
.data32
[3];
7244 if (e
->data
.data32
[0] & (1<<11)) /* height */
7245 HEIGHT(win
) = e
->data
.data32
[4];
7250 /* TODO: Change stack sizes */
7251 /* notify no change was made. */
7252 config_win(win
, NULL
);
7255 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
7256 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
7257 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
7258 if (e
->data
.data32
[2])
7259 ewmh_update_win_state(win
, e
->data
.data32
[2],
7272 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
;
7274 xcb_void_cookie_t wac
;
7275 xcb_generic_error_t
*error
;
7277 /* this causes an error if some other window manager is running */
7278 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7279 for (i
= 0; i
< num_screens
; i
++) {
7281 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: 0x%x\n",
7283 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
7284 XCB_CW_EVENT_MASK
, &val
);
7285 if ((error
= xcb_request_check(conn
, wac
))) {
7286 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
7297 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
7299 struct swm_region
*r
, *n
;
7300 struct workspace
*ws
= NULL
;
7303 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
7304 s
->idx
, w
, h
, x
, y
);
7306 /* remove any conflicting regions */
7307 n
= TAILQ_FIRST(&s
->rl
);
7310 n
= TAILQ_NEXT(r
, entry
);
7311 if (X(r
) < (x
+ w
) &&
7312 (X(r
) + WIDTH(r
)) > x
&&
7314 (Y(r
) + HEIGHT(r
)) > y
) {
7315 if (r
->ws
->r
!= NULL
)
7316 r
->ws
->old_r
= r
->ws
->r
;
7319 TAILQ_REMOVE(&s
->rl
, r
, entry
);
7320 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
7324 /* search old regions for one to reuse */
7326 /* size + location match */
7327 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7328 if (X(r
) == x
&& Y(r
) == y
&&
7329 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7333 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7334 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7338 TAILQ_REMOVE(&s
->orl
, r
, entry
);
7339 /* try to use old region's workspace */
7340 if (r
->ws
->r
== NULL
)
7343 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
7344 err(1, "new_region: calloc: failed to allocate memory "
7347 /* if we don't have a workspace already, find one */
7349 for (i
= 0; i
< workspace_limit
; i
++)
7350 if (s
->ws
[i
].r
== NULL
) {
7357 errx(1, "new_region: no free workspaces");
7368 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
7374 #ifdef SWM_XRR_HAS_CRTC
7377 #endif /* SWM_XRR_HAS_CRTC */
7378 struct swm_region
*r
;
7380 xcb_randr_get_screen_resources_current_cookie_t src
;
7381 xcb_randr_get_screen_resources_current_reply_t
*srr
;
7382 xcb_randr_get_crtc_info_cookie_t cic
;
7383 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
7384 xcb_randr_crtc_t
*crtc
;
7385 xcb_screen_t
*screen
= get_screen(i
);
7387 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7388 if (i
>= num_screens
)
7389 errx(1, "scan_xrandr: invalid screen");
7391 /* remove any old regions */
7392 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
7393 r
->ws
->old_r
= r
->ws
->r
= NULL
;
7395 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
7396 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
7400 /* map virtual screens onto physical screens */
7401 #ifdef SWM_XRR_HAS_CRTC
7402 if (xrandr_support
) {
7403 src
= xcb_randr_get_screen_resources_current(conn
,
7405 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
7408 new_region(&screens
[i
], 0, 0,
7409 screen
->width_in_pixels
,
7410 screen
->height_in_pixels
);
7413 ncrtc
= srr
->num_crtcs
;
7414 for (c
= 0; c
< ncrtc
; c
++) {
7415 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
7416 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
7418 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
7421 if (cir
->num_outputs
== 0) {
7427 new_region(&screens
[i
], 0, 0,
7428 screen
->width_in_pixels
,
7429 screen
->height_in_pixels
);
7431 new_region(&screens
[i
],
7432 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
7437 #endif /* SWM_XRR_HAS_CRTC */
7439 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
7440 screen
->height_in_pixels
);
7445 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
7447 struct swm_region
*r
;
7450 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
7452 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7453 /* silly event doesn't include the screen index */
7454 for (i
= 0; i
< num_screens
; i
++)
7455 if (screens
[i
].root
== e
->root
)
7457 if (i
>= num_screens
)
7458 errx(1, "screenchange: screen not found");
7460 /* brute force for now, just re-enumerate the regions */
7464 print_win_geom(e
->root
);
7466 /* add bars to all regions */
7467 for (i
= 0; i
< num_screens
; i
++)
7468 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7476 xcb_window_t
*wins
= NULL
;
7478 int i
, j
, num_screens
;
7479 uint16_t state
, manage
;
7481 xcb_query_tree_cookie_t qtc
;
7482 xcb_query_tree_reply_t
*qtr
;
7483 xcb_get_window_attributes_cookie_t c
;
7484 xcb_get_window_attributes_reply_t
*r
;
7485 xcb_get_property_cookie_t pc
;
7487 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
7488 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7489 for (i
= 0; i
< num_screens
; i
++) {
7490 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
7491 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
7494 wins
= xcb_query_tree_children(qtr
);
7495 no
= xcb_query_tree_children_length(qtr
);
7496 /* attach windows to a region */
7497 /* normal windows */
7498 for (j
= 0; j
< no
; j
++) {
7499 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7500 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7503 if (r
->override_redirect
) {
7508 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7509 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7515 state
= getstate(wins
[j
]);
7516 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7517 if (r
->map_state
== XCB_MAP_STATE_VIEWABLE
|| manage
)
7518 manage_window(wins
[j
]);
7521 /* transient windows */
7522 for (j
= 0; j
< no
; j
++) {
7523 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7524 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7527 if (r
->override_redirect
) {
7533 state
= getstate(wins
[j
]);
7534 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7535 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7536 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7537 &wins
[j
], NULL
) && manage
)
7538 manage_window(wins
[j
]);
7542 DNPRINTF(SWM_D_INIT
, "grab_windows: done\n");
7548 int i
, j
, k
, num_screens
;
7549 struct workspace
*ws
;
7550 uint32_t gcv
[1], wa
[1];
7551 const xcb_query_extension_reply_t
*qep
;
7552 xcb_cursor_t cursor
;
7553 xcb_font_t cursor_font
;
7554 xcb_randr_query_version_cookie_t c
;
7555 xcb_randr_query_version_reply_t
*r
;
7557 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7558 if ((screens
= calloc(num_screens
,
7559 sizeof(struct swm_screen
))) == NULL
)
7560 err(1, "setup_screens: calloc: failed to allocate memory for "
7563 /* initial Xrandr setup */
7565 c
= xcb_randr_query_version(conn
, 1, 1);
7566 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
7568 if (r
->major_version
>= 1)
7572 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
7573 xrandr_eventbase
= qep
->first_event
;
7575 cursor_font
= xcb_generate_id(conn
);
7576 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
7578 cursor
= xcb_generate_id(conn
);
7579 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
7580 XC_left_ptr
, XC_left_ptr
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7583 /* map physical screens */
7584 for (i
= 0; i
< num_screens
; i
++) {
7585 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
7587 TAILQ_INIT(&screens
[i
].rl
);
7588 TAILQ_INIT(&screens
[i
].orl
);
7589 screens
[i
].root
= get_screen(i
)->root
;
7591 /* set default colors */
7592 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
7593 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
7594 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
7595 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
7596 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
7598 /* create graphics context on screen */
7599 screens
[i
].bar_gc
= xcb_generate_id(conn
);
7601 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
7602 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
7604 /* set default cursor */
7605 xcb_change_window_attributes(conn
, screens
[i
].root
,
7608 /* init all workspaces */
7609 /* XXX these should be dynamically allocated too */
7610 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
7611 ws
= &screens
[i
].ws
[j
];
7617 TAILQ_INIT(&ws
->winlist
);
7618 TAILQ_INIT(&ws
->unmanagedlist
);
7620 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
7621 if (layouts
[k
].l_config
!= NULL
)
7622 layouts
[k
].l_config(ws
,
7623 SWM_ARG_ID_STACKINIT
);
7624 ws
->cur_layout
= &layouts
[0];
7625 ws
->cur_layout
->l_string(ws
);
7631 xcb_randr_select_input(conn
, screens
[i
].root
,
7632 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
7634 xcb_free_cursor(conn
, cursor
);
7635 xcb_close_font(conn
, cursor_font
);
7641 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
7642 err(1, "setup_globals: strdup: failed to allocate memory.");
7644 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
7645 err(1, "setup_globals: strdup: failed to allocate memory.");
7647 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
7648 errx(1, "unable to allocate key symbols");
7650 a_state
= get_atom_from_string("WM_STATE");
7651 a_prot
= get_atom_from_string("WM_PROTOCOLS");
7652 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
7653 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
7654 a_wmname
= get_atom_from_string("WM_NAME");
7655 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
7656 a_utf8_string
= get_atom_from_string("UTF8_STRING");
7657 a_string
= get_atom_from_string("STRING");
7658 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
7659 a_swm_ws
= get_atom_from_string("_SWM_WS");
7666 xcb_atom_t netwmcheck
;
7667 xcb_window_t root
, win
;
7670 /* work around sun jdk bugs, code from wmname */
7671 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7673 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7674 for (i
= 0; i
< num_screens
; i
++) {
7675 root
= screens
[i
].root
;
7677 win
= xcb_generate_id(conn
);
7678 wa
[0] = screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].pixel
;
7679 wa
[1] = screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].pixel
;
7680 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, 0, 0, 0, 1,
7681 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
7682 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
7684 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
7685 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7686 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7687 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7688 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7689 a_netwmname
, a_utf8_string
, 8, strlen("LG3D"), "LG3D");
7694 event_error(xcb_generic_error_t
*e
)
7696 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
7697 "resource_id: %u, minor_code: %u\n",
7698 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
7699 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
7700 e
->sequence
, e
->resource_id
, e
->minor_code
);
7704 event_handle(xcb_generic_event_t
*evt
)
7706 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
7708 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d)\n",
7709 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
7710 XCB_EVENT_RESPONSE_TYPE(evt
));
7713 #define EVENT(type, callback) case type: callback((void *)evt); return
7714 EVENT(0, event_error
);
7715 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
7716 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7717 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
7718 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
7719 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
7720 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
7721 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
7722 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
7723 /*EVENT(XCB_CREATE_NOTIFY, );*/
7724 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
7725 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
7726 EVENT(XCB_EXPOSE
, expose
);
7727 /*EVENT(XCB_FOCUS_IN, );*/
7728 /*EVENT(XCB_FOCUS_OUT, );*/
7729 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
7730 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
7731 EVENT(XCB_KEY_PRESS
, keypress
);
7732 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7733 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
7734 /*EVENT(XCB_LEAVE_NOTIFY, );*/
7735 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
7736 EVENT(XCB_MAP_REQUEST
, maprequest
);
7737 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
7738 /*EVENT(XCB_MOTION_NOTIFY, );*/
7739 /*EVENT(XCB_NO_EXPOSURE, );*/
7740 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
7741 /*EVENT(XCB_REPARENT_NOTIFY, );*/
7742 /*EVENT(XCB_RESIZE_REQUEST, );*/
7743 /*EVENT(XCB_SELECTION_CLEAR, );*/
7744 /*EVENT(XCB_SELECTION_NOTIFY, );*/
7745 /*EVENT(XCB_SELECTION_REQUEST, );*/
7746 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
7747 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
7750 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
7751 screenchange((void *)evt
);
7755 main(int argc
, char *argv
[])
7757 struct swm_region
*r
, *rr
;
7758 struct ws_win
*winfocus
= NULL
;
7760 char conf
[PATH_MAX
], *cfile
= NULL
;
7762 int xfd
, i
, num_screens
;
7763 struct sigaction sact
;
7764 xcb_generic_event_t
*evt
;
7768 /* suppress unused warning since var is needed */
7771 time_started
= time(NULL
);
7774 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
7775 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, ""))
7776 warnx("no locale support");
7778 /* handle some signals */
7779 bzero(&sact
, sizeof(sact
));
7780 sigemptyset(&sact
.sa_mask
);
7782 sact
.sa_handler
= sighdlr
;
7783 sigaction(SIGINT
, &sact
, NULL
);
7784 sigaction(SIGQUIT
, &sact
, NULL
);
7785 sigaction(SIGTERM
, &sact
, NULL
);
7786 sigaction(SIGHUP
, &sact
, NULL
);
7788 sact
.sa_handler
= sighdlr
;
7789 sact
.sa_flags
= SA_NOCLDSTOP
;
7790 sigaction(SIGCHLD
, &sact
, NULL
);
7792 if (!(display
= XOpenDisplay(0)))
7793 errx(1, "can not open display");
7795 conn
= XGetXCBConnection(display
);
7796 if (xcb_connection_has_error(conn
))
7797 errx(1, "can not get XCB connection");
7799 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
7801 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
7802 xfd
= xcb_get_file_descriptor(conn
);
7804 /* look for local and global conf file */
7805 pwd
= getpwuid(getuid());
7807 errx(1, "invalid user: %d", getuid());
7809 xcb_grab_server(conn
);
7812 /* flush all events */
7813 while ((evt
= xcb_poll_for_event(conn
))) {
7814 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
7819 if (enable_wm() != 0)
7820 errx(1, "another window manager is currently running");
7831 for (i
= 0; ; i
++) {
7836 snprintf(conf
, sizeof conf
, "%s/.%s",
7837 pwd
->pw_dir
, SWM_CONF_FILE
);
7841 snprintf(conf
, sizeof conf
, "/etc/%s",
7846 snprintf(conf
, sizeof conf
, "%s/.%s",
7847 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
7851 snprintf(conf
, sizeof conf
, "/etc/%s",
7858 if (strlen(conf
) && stat(conf
, &sb
) != -1)
7859 if (S_ISREG(sb
.st_mode
)) {
7866 /* load conf (if any) */
7868 conf_load(cfile
, SWM_CONF_DEFAULT
);
7871 /* set some values to work around bad programs */
7873 /* grab existing windows (before we build the bars) */
7876 if (getenv("SWM_STARTED") == NULL
)
7877 setenv("SWM_STARTED", "YES", 1);
7879 /* setup all bars */
7880 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7881 for (i
= 0; i
< num_screens
; i
++)
7882 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
7883 if (winfocus
== NULL
)
7884 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
7893 xcb_ungrab_server(conn
);
7897 while ((evt
= xcb_poll_for_event(conn
))) {
7904 /* if we are being restarted go focus on first window */
7906 rr
= winfocus
->ws
->r
;
7908 /* not a visible window */
7912 /* move pointer to first screen if multi screen */
7913 if (num_screens
> 1 || outputs
> 1)
7914 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
,
7915 rr
->s
[0].root
, 0, 0, 0, 0, X(rr
),
7916 Y(rr
) + (bar_enabled
? bar_height
: 0));
7918 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7928 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7930 DNPRINTF(SWM_D_MISC
, "select failed");
7931 if (restart_wm
== 1)
7932 restart(NULL
, NULL
);
7933 if (search_resp
== 1)
7946 for (i
= 0; i
< num_screens
; ++i
)
7947 if (screens
[i
].bar_gc
!= 0)
7948 xcb_free_gc(conn
, screens
[i
].bar_gc
);
7950 XftFontClose(display
, bar_font
);
7951 XftColorFree(display
, DefaultVisual(display
, 0),
7952 DefaultColormap(display
, 0), &bar_font_color
);
7953 xcb_key_symbols_free(syms
);
7955 xcb_disconnect(conn
);