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 int conf_load(char *, int);
651 void constrain_window(struct ws_win
*, struct swm_region
*, int);
653 void event_handle(xcb_generic_event_t
*);
654 struct ws_win
*find_window(xcb_window_t
);
655 int floating_toggle_win(struct ws_win
*);
656 void focus(struct swm_region
*, union arg
*);
657 void focus_magic(struct ws_win
*);
658 xcb_atom_t
get_atom_from_string(const char *);
659 xcb_screen_t
*get_screen(int);
660 char *get_win_name(xcb_window_t
);
661 uint32_t getstate(xcb_window_t
);
662 void grabbuttons(struct ws_win
*, int);
663 void map_window_raised(xcb_window_t
);
664 void new_region(struct swm_screen
*, int, int, int, int);
665 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
666 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
667 void store_float_geom(struct ws_win
*, struct swm_region
*);
668 void unmanage_window(struct ws_win
*);
669 void update_window(struct ws_win
*);
671 char *get_atom_name(xcb_atom_t
);
675 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
677 unsigned int tmpr
, tmpg
, tmpb
;
679 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
682 *rr
= RGB_8_TO_16(tmpr
);
683 *gg
= RGB_8_TO_16(tmpg
);
684 *bb
= RGB_8_TO_16(tmpb
);
690 get_screen(int screen
)
692 xcb_screen_iterator_t i
;
694 i
= xcb_setup_roots_iterator(xcb_get_setup(conn
));
695 for (; i
.rem
; --screen
, xcb_screen_next(&i
))
705 xcb_get_input_focus_cookie_t c
;
706 xcb_get_input_focus_reply_t
*r
;
709 c
= xcb_get_input_focus(conn
);
711 r
= xcb_get_input_focus_reply(conn
, c
, NULL
);
717 map_window_raised(xcb_window_t win
)
719 uint32_t val
= XCB_STACK_MODE_ABOVE
;
721 xcb_configure_window(conn
, win
,
722 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
724 xcb_map_window(conn
, win
);
728 get_atom_from_string(const char *str
)
730 xcb_intern_atom_cookie_t c
;
731 xcb_intern_atom_reply_t
*r
;
734 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
735 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
743 return (XCB_ATOM_NONE
);
747 update_iconic(struct ws_win
*win
, int newv
)
754 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
755 a_swm_iconic
, XCB_ATOM_INTEGER
, 32, 1, &v
);
757 xcb_delete_property(conn
, win
->id
, a_swm_iconic
);
761 get_iconic(struct ws_win
*win
)
764 xcb_get_property_reply_t
*pr
= NULL
;
767 pr
= xcb_get_property_reply(conn
,
768 xcb_get_property(conn
, 0, win
->id
, a_swm_iconic
,
769 XCB_ATOM_INTEGER
, 0, 1), NULL
);
772 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
774 v
= *((int32_t *)xcb_get_property_value(pr
));
785 int i
, j
, num_screens
;
787 sup_list
= get_atom_from_string("_NET_SUPPORTED");
789 for (i
= 0; i
< LENGTH(ewmh
); i
++)
790 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
792 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
793 for (i
= 0; i
< num_screens
; i
++) {
794 /* Support check window will be created by workaround(). */
796 /* Report supported atoms */
797 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
798 for (j
= 0; j
< LENGTH(ewmh
); j
++)
799 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
800 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
809 xcb_atom_t sup_check
, sup_list
;
811 xcb_get_property_cookie_t pc
;
812 xcb_get_property_reply_t
*pr
;
814 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
815 sup_list
= get_atom_from_string("_NET_SUPPORTED");
816 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
818 for (i
= 0; i
< num_screens
; i
++) {
819 /* Get the support check window and destroy it */
820 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, sup_check
,
821 XCB_ATOM_WINDOW
, 0, 1);
822 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
825 if (pr
->format
== sup_check
) {
826 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
828 xcb_destroy_window(conn
, id
);
829 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
830 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
838 ewmh_autoquirk(struct ws_win
*win
)
842 xcb_get_property_cookie_t c
;
843 xcb_get_property_reply_t
*r
;
845 c
= xcb_get_property(conn
, 0, win
->id
,
846 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
847 r
= xcb_get_property_reply(conn
, c
, NULL
);
851 n
= xcb_get_property_value_length(r
);
852 type
= xcb_get_property_value(r
);
854 for (i
= 0; i
< n
; i
++) {
855 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
857 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
858 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
859 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
861 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
864 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
865 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
867 win
->quirks
= SWM_Q_FLOAT
;
874 #define SWM_EWMH_ACTION_COUNT_MAX (6)
875 #define EWMH_F_FULLSCREEN (1<<0)
876 #define EWMH_F_ABOVE (1<<1)
877 #define EWMH_F_HIDDEN (1<<2)
878 #define EWMH_F_SKIP_PAGER (1<<3)
879 #define EWMH_F_SKIP_TASKBAR (1<<4)
880 #define SWM_F_MANUAL (1<<5)
883 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
891 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
892 "fullscreen %s\n", win
->id
, YESNO(fs
));
895 if (!win
->g_floatvalid
)
896 store_float_geom(win
, win
->ws
->r
);
898 win
->g
= win
->ws
->r
->g
;
901 if (win
->g_floatvalid
) {
902 /* refloat at last floating relative position */
903 win
->g
= win
->g_float
;
904 X(win
) += X(win
->ws
->r
);
905 Y(win
) += Y(win
->ws
->r
);
913 ewmh_update_actions(struct ws_win
*win
)
915 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
921 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
924 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
925 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
928 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
929 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
932 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
933 #define _NET_WM_STATE_ADD 1 /* add/set property */
934 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
937 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
939 unsigned int mask
= 0;
940 unsigned int changed
= 0;
941 unsigned int orig_flags
;
946 DNPRINTF(SWM_D_PROP
, "ewmh_update_win_state: window: 0x%x, state: %ld, "
947 "action: %ld\n", win
->id
, state
, action
);
949 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
950 mask
= EWMH_F_FULLSCREEN
;
951 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
953 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
955 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
956 mask
= EWMH_F_SKIP_PAGER
;
957 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
958 mask
= EWMH_F_SKIP_TASKBAR
;
961 orig_flags
= win
->ewmh_flags
;
964 case _NET_WM_STATE_REMOVE
:
965 win
->ewmh_flags
&= ~mask
;
967 case _NET_WM_STATE_ADD
:
968 win
->ewmh_flags
|= mask
;
970 case _NET_WM_STATE_TOGGLE
:
971 win
->ewmh_flags
^= mask
;
975 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
977 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
979 if (!floating_toggle_win(win
))
980 win
->ewmh_flags
= orig_flags
; /* revert */
981 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
983 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
984 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
986 if (!ewmh_set_win_fullscreen(win
,
987 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
988 win
->ewmh_flags
= orig_flags
; /* revert */
990 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
992 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
993 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
994 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
995 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
996 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
997 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
998 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
999 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1000 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1001 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1002 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1003 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1004 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1005 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1006 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1007 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1008 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1009 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1010 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1011 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1015 ewmh_get_win_state(struct ws_win
*win
)
1018 xcb_get_property_cookie_t c
;
1019 xcb_get_property_reply_t
*r
;
1025 win
->ewmh_flags
= 0;
1027 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1029 win
->ewmh_flags
|= SWM_F_MANUAL
;
1031 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1032 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1033 r
= xcb_get_property_reply(conn
, c
, NULL
);
1037 states
= xcb_get_property_value(r
);
1038 n
= xcb_get_property_value_length(r
);
1040 for (i
= 0; i
< n
; i
++)
1041 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1049 dumpwins(struct swm_region
*r
, union arg
*args
)
1053 xcb_get_window_attributes_cookie_t c
;
1054 xcb_get_window_attributes_reply_t
*wa
;
1056 /* suppress unused warning since var is needed */
1059 if (r
->ws
== NULL
) {
1060 warnx("dumpwins: invalid workspace");
1064 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1065 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1066 state
= getstate(win
->id
);
1067 c
= xcb_get_window_attributes(conn
, win
->id
);
1068 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1070 warnx("window: 0x%x, map_state: %d, state: %u, "
1071 "transient: 0x%x", win
->id
, wa
->map_state
,
1072 state
, win
->transient
);
1075 warnx("window: 0x%x, failed xcb_get_window_attributes",
1079 warnx("===== unmanaged window list =====");
1080 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1081 state
= getstate(win
->id
);
1082 c
= xcb_get_window_attributes(conn
, win
->id
);
1083 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1085 warnx("window: 0x%x, map_state: %d, state: %u, "
1086 "transient: 0x%x", win
->id
, wa
->map_state
,
1087 state
, win
->transient
);
1090 warnx("window: 0x%x, failed xcb_get_window_attributes",
1094 warnx("=================================");
1098 dumpwins(struct swm_region
*r
, union arg
*args
)
1101 #endif /* SWM_DEBUG */
1103 void event_error(xcb_generic_error_t
*);
1104 void expose(xcb_expose_event_t
*);
1105 void keypress(xcb_key_press_event_t
*);
1106 void buttonpress(xcb_button_press_event_t
*);
1107 void configurerequest(xcb_configure_request_event_t
*);
1108 void configurenotify(xcb_configure_notify_event_t
*);
1109 void destroynotify(xcb_destroy_notify_event_t
*);
1110 void enternotify(xcb_enter_notify_event_t
*);
1111 void mapnotify(xcb_map_notify_event_t
*);
1112 void mappingnotify(xcb_mapping_notify_event_t
*);
1113 void maprequest(xcb_map_request_event_t
*);
1114 void propertynotify(xcb_property_notify_event_t
*);
1115 void unmapnotify(xcb_unmap_notify_event_t
*);
1116 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1117 void clientmessage(xcb_client_message_event_t
*);
1118 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1123 int saved_errno
, status
;
1126 saved_errno
= errno
;
1130 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1135 if (errno
!= ECHILD
)
1136 warn("sighdlr: waitpid");
1137 #endif /* SWM_DEBUG */
1140 if (pid
== searchpid
)
1144 if (WIFEXITED(status
)) {
1145 if (WEXITSTATUS(status
) != 0)
1146 warnx("sighdlr: child exit status: %d",
1147 WEXITSTATUS(status
));
1149 warnx("sighdlr: child is terminated "
1151 #endif /* SWM_DEBUG */
1165 errno
= saved_errno
;
1171 struct pid_e
*p
= NULL
;
1173 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1178 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1187 name_to_pixel(const char *colorname
)
1189 uint32_t result
= 0;
1190 char cname
[32] = "#";
1191 xcb_screen_t
*screen
;
1192 xcb_colormap_t cmap
;
1193 xcb_alloc_color_reply_t
*cr
;
1194 xcb_alloc_named_color_reply_t
*nr
;
1195 uint16_t rr
, gg
, bb
;
1197 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1198 cmap
= screen
->default_colormap
;
1200 /* color is in format rgb://rr/gg/bb */
1201 if (strncmp(colorname
, "rgb:", 4) == 0) {
1202 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1203 warnx("could not parse rgb %s", colorname
);
1205 cr
= xcb_alloc_color_reply(conn
,
1206 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1212 warnx("color '%s' not found", colorname
);
1215 nr
= xcb_alloc_named_color_reply(conn
,
1216 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1219 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1220 nr
= xcb_alloc_named_color_reply(conn
,
1221 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1228 warnx("color '%s' not found", colorname
);
1235 setscreencolor(char *val
, int i
, int c
)
1239 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1240 if (i
> 0 && i
<= num_screens
) {
1241 screens
[i
- 1].c
[c
].pixel
= name_to_pixel(val
);
1242 free(screens
[i
- 1].c
[c
].name
);
1243 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1245 } else if (i
== -1) {
1246 for (i
= 0; i
< num_screens
; i
++) {
1247 screens
[i
].c
[c
].pixel
= name_to_pixel(val
);
1248 free(screens
[i
].c
[c
].name
);
1249 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1253 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1258 fancy_stacker(struct workspace
*ws
)
1260 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1261 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1262 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1263 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1264 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1265 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1266 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1267 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1268 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1272 plain_stacker(struct workspace
*ws
)
1274 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1275 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1276 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1277 sizeof ws
->stacker
);
1278 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1279 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1280 sizeof ws
->stacker
);
1284 custom_region(char *val
)
1286 unsigned int x
, y
, w
, h
;
1287 int sidx
, num_screens
;
1288 xcb_screen_t
*screen
;
1290 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1291 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1292 errx(1, "invalid custom region, "
1293 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1294 if (sidx
< 1 || sidx
> num_screens
)
1295 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1299 screen
= get_screen(sidx
);
1301 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1303 if (x
> screen
->width_in_pixels
||
1304 y
> screen
->height_in_pixels
||
1305 w
+ x
> screen
->width_in_pixels
||
1306 h
+ y
> screen
->height_in_pixels
) {
1307 warnx("ignoring region %ux%u+%u+%u - not within screen "
1308 "boundaries (%ux%u)", w
, h
, x
, y
,
1309 screen
->width_in_pixels
, screen
->height_in_pixels
);
1313 new_region(&screens
[sidx
], x
, y
, w
, h
);
1317 socket_setnonblock(int fd
)
1321 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1322 err(1, "fcntl F_GETFL");
1323 flags
|= O_NONBLOCK
;
1324 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1325 err(1, "fcntl F_SETFL");
1329 bar_print(struct swm_region
*r
, const char *s
)
1332 xcb_rectangle_t rect
;
1340 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
1342 switch (bar_justify
) {
1343 case SWM_BAR_JUSTIFY_LEFT
:
1346 case SWM_BAR_JUSTIFY_CENTER
:
1347 x
= (WIDTH(r
) - info
.width
) / 2;
1349 case SWM_BAR_JUSTIFY_RIGHT
:
1350 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
1354 if (x
< SWM_BAR_OFFSET
)
1359 rect
.width
= WIDTH(r
->bar
);
1360 rect
.height
= HEIGHT(r
->bar
);
1362 /* clear back buffer */
1363 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1364 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1365 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
,
1366 sizeof(rect
), &rect
);
1368 /* draw back buffer */
1369 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1370 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_BACKGROUND
, gcv
);
1371 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
;
1372 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1374 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
1375 DefaultVisual(display
, r
->s
->idx
),
1376 DefaultColormap(display
, r
->s
->idx
));
1378 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
1379 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
1382 XftDrawDestroy(draw
);
1385 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1386 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1390 bar_extra_stop(void)
1394 bzero(bar_pipe
, sizeof bar_pipe
);
1397 kill(bar_pid
, SIGTERM
);
1400 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1405 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1407 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1409 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1410 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1414 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1416 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1418 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1419 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1423 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1425 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1428 bar_class_name(s
, sz
, r
);
1429 strlcat(s
, ":", sz
);
1430 bar_title_name(s
, sz
, r
);
1434 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1436 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1438 if (r
->ws
->focus
->floating
)
1439 strlcat(s
, "(f)", sz
);
1443 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1447 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1449 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1452 strlcat(s
, title
, sz
);
1456 int urgent
[SWM_WS_MAX
];
1458 bar_urgent(char *s
, size_t sz
)
1461 int i
, j
, num_screens
;
1463 xcb_get_property_cookie_t c
;
1464 xcb_icccm_wm_hints_t hints
;
1466 for (i
= 0; i
< workspace_limit
; i
++)
1469 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1470 for (i
= 0; i
< num_screens
; i
++)
1471 for (j
= 0; j
< workspace_limit
; j
++)
1472 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1473 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
1474 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
1477 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
1481 for (i
= 0; i
< workspace_limit
; i
++) {
1483 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1485 snprintf(b
, sizeof b
, "- ");
1491 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1493 if (r
== NULL
|| r
->ws
== NULL
)
1495 if (r
->ws
->name
!= NULL
)
1496 strlcat(s
, r
->ws
->name
, sz
);
1499 /* build the default bar format according to the defined enabled options */
1501 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1503 /* if format provided, just copy the buffers */
1504 if (bar_format
!= NULL
) {
1505 strlcpy(fmtnew
, fmtexp
, sz
);
1509 /* reset the output buffer */
1512 strlcat(fmtnew
, "+N:+I ", sz
);
1514 strlcat(fmtnew
, "+S", sz
);
1515 strlcat(fmtnew
, " ", sz
);
1517 /* only show the workspace name if there's actually one */
1518 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1519 strlcat(fmtnew
, "<+D>", sz
);
1520 strlcat(fmtnew
, "+3<", sz
);
1522 if (clock_enabled
) {
1523 strlcat(fmtnew
, fmtexp
, sz
);
1524 strlcat(fmtnew
, "+4<", sz
);
1527 /* bar_urgent already adds the space before the last asterisk */
1529 strlcat(fmtnew
, "* +U*+4<", sz
);
1531 if (title_class_enabled
) {
1532 strlcat(fmtnew
, "+C", sz
);
1533 if (title_name_enabled
== 0)
1534 strlcat(fmtnew
, "+4<", sz
);
1537 /* checks needed by the colon and floating strlcat(3) calls below */
1538 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1539 if (title_name_enabled
) {
1540 if (title_class_enabled
)
1541 strlcat(fmtnew
, ":", sz
);
1542 strlcat(fmtnew
, "+T+4<", sz
);
1544 if (window_name_enabled
) {
1545 if (r
->ws
->focus
->floating
)
1546 strlcat(fmtnew
, "+F ", sz
);
1547 strlcat(fmtnew
, "+64W ", sz
);
1551 /* finally add the action script output and the version */
1552 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1556 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1558 /* special case; no limit given, pad one space, instead */
1559 if (*limit
== (int)sz
- 1)
1561 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1564 /* replaces the bar format character sequences (like in tmux(1)) */
1566 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1570 char tmp
[SWM_BAR_MAX
];
1574 /* reset strlcat(3) buffer */
1577 /* get number, if any */
1580 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1581 limit
= sizeof tmp
- 1;
1582 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
1583 limit
= sizeof tmp
- 1;
1585 /* there is nothing to replace (ie EOL) */
1592 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1595 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1598 bar_class_name(tmp
, sizeof tmp
, r
);
1601 bar_workspace_name(tmp
, sizeof tmp
, r
);
1604 bar_window_float(tmp
, sizeof tmp
, r
);
1607 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1610 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1613 bar_class_title_name(tmp
, sizeof tmp
, r
);
1616 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1619 bar_title_name(tmp
, sizeof tmp
, r
);
1622 bar_urgent(tmp
, sizeof tmp
);
1625 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1628 bar_window_name(tmp
, sizeof tmp
, r
);
1631 /* unknown character sequence; copy as-is */
1632 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1638 if ((int)len
< limit
)
1640 while (limit
-- > 0) {
1641 if (*offrep
>= sz
- 1)
1643 fmtrep
[(*offrep
)++] = *ptr
++;
1651 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1656 while (*fmt
!= '\0') {
1658 /* skip ordinary characters */
1661 fmtrep
[off
++] = *fmt
++;
1665 /* character sequence found; replace it */
1666 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1675 bar_fmt_expand(char *fmtexp
, size_t sz
)
1682 /* start by grabbing the current time and date */
1684 localtime_r(&tmt
, &tm
);
1686 /* figure out what to expand */
1687 if (bar_format
!= NULL
)
1689 else if (bar_format
== NULL
&& clock_enabled
)
1691 /* if nothing to expand bail out */
1697 /* copy as-is, just in case the format shouldn't be expanded below */
1698 strlcpy(fmtexp
, fmt
, sz
);
1699 /* finally pass the string through strftime(3) */
1700 #ifndef SWM_DENY_CLOCK_FORMAT
1701 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1702 warnx("format too long");
1710 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1711 char fmtrep
[SWM_BAR_MAX
];
1713 struct swm_region
*r
;
1715 /* expand the format by first passing it through strftime(3) */
1716 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1718 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1719 for (i
= 0; i
< num_screens
; i
++) {
1720 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1723 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1724 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1725 bar_print(r
, fmtrep
);
1736 if (bar_enabled
== 0)
1738 if (bar_extra
&& bar_extra_running
) {
1739 /* ignore short reads; it'll correct itself */
1740 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1741 if (b
&& b
[len
- 1] == '\n') {
1743 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1745 if (b
== NULL
&& errno
!= EAGAIN
) {
1746 warn("bar_update: bar_extra failed");
1750 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1759 /* suppress unused warning since var is needed */
1766 bar_toggle(struct swm_region
*r
, union arg
*args
)
1768 struct swm_region
*tmpr
;
1771 /* suppress unused warnings since vars are needed */
1775 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1777 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1779 for (i
= 0; i
< num_screens
; i
++)
1780 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1782 xcb_unmap_window(conn
, tmpr
->bar
->id
);
1784 for (i
= 0; i
< num_screens
; i
++)
1785 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1787 map_window_raised(tmpr
->bar
->id
);
1790 bar_enabled
= !bar_enabled
;
1793 /* must be after stack */
1802 struct swm_region
*r
;
1806 /* do this here because the conf file is in memory */
1807 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1808 /* launch external status app */
1809 bar_extra_running
= 1;
1810 if (pipe(bar_pipe
) == -1)
1811 err(1, "pipe error");
1812 socket_setnonblock(bar_pipe
[0]);
1813 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1814 if (dup2(bar_pipe
[0], 0) == -1)
1816 if (dup2(bar_pipe
[1], 1) == -1)
1818 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1819 err(1, "could not disable SIGPIPE");
1820 switch (bar_pid
= fork()) {
1822 err(1, "cannot fork");
1826 execvp(bar_argv
[0], bar_argv
);
1827 err(1, "%s external app failed", bar_argv
[0]);
1829 default: /* parent */
1835 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1836 for (i
= 0; i
< num_screens
; i
++)
1837 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1840 wa
[0] = screens
[i
].c
[SWM_S_COLOR_BAR
].pixel
;
1841 wa
[1] = screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1842 xcb_change_window_attributes(conn
, r
->bar
->id
,
1843 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
1849 bar_setup(struct swm_region
*r
)
1851 char *font
, *fontpos
;
1853 xcb_screen_t
*screen
= get_screen(r
->s
->idx
);
1860 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
1861 err(1, "bar_setup: calloc: failed to allocate memory.");
1863 if (bar_font
== NULL
) {
1864 while ((font
= strsep(&bar_fonts
, ",")) != NULL
) {
1868 DNPRINTF(SWM_D_INIT
, "bar_setup: try font %s\n", font
);
1872 while ((fontpos
= index(fontpos
, '-'))) {
1878 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
1881 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
1884 warnx("unable to load font %s", font
);
1887 DNPRINTF(SWM_D_INIT
, "successfully opened "
1894 if (bar_font
== NULL
)
1895 errx(1, "unable to open a font");
1897 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
1902 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
1904 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
1905 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
1906 warn("unable to allocate Xft color");
1909 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1910 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
1911 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
1913 r
->bar
->id
= xcb_generate_id(conn
);
1914 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1915 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1916 wa
[2] = XCB_EVENT_MASK_EXPOSURE
;
1918 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
1919 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
1920 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1921 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
1922 | XCB_CW_EVENT_MASK
, wa
);
1924 r
->bar
->buffer
= xcb_generate_id(conn
);
1925 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
1926 WIDTH(r
->bar
), HEIGHT(r
->bar
));
1928 xcb_randr_select_input(conn
, r
->bar
->id
,
1929 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
1932 map_window_raised(r
->bar
->id
);
1934 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
1935 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
1938 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1939 err(1, "could not install bar_signal");
1944 bar_cleanup(struct swm_region
*r
)
1948 xcb_destroy_window(conn
, r
->bar
->id
);
1949 xcb_free_pixmap(conn
, r
->bar
->buffer
);
1955 set_win_state(struct ws_win
*win
, uint16_t state
)
1957 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
1959 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x\n", win
->id
);
1964 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
1965 a_state
, 32, 2, data
);
1969 getstate(xcb_window_t w
)
1971 uint32_t result
= 0;
1972 xcb_get_property_cookie_t c
;
1973 xcb_get_property_reply_t
*r
;
1975 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
1976 r
= xcb_get_property_reply(conn
, c
, NULL
);
1979 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
1980 result
= *((uint32_t *)xcb_get_property_value(r
));
1984 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
1990 version(struct swm_region
*r
, union arg
*args
)
1992 /* suppress unused warnings since vars are needed */
1996 bar_version
= !bar_version
;
1998 snprintf(bar_vertext
, sizeof bar_vertext
,
1999 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2001 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2008 client_msg(struct ws_win
*win
, xcb_atom_t a
)
2010 xcb_client_message_event_t ev
;
2015 bzero(&ev
, sizeof ev
);
2016 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2017 ev
.window
= win
->id
;
2020 ev
.data
.data32
[0] = a
;
2021 ev
.data
.data32
[1] = XCB_CURRENT_TIME
;
2023 xcb_send_event(conn
, 0, win
->id
,
2024 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2027 /* synthetic response to a ConfigureRequest when not making a change */
2029 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2031 xcb_configure_notify_event_t ce
;
2036 /* send notification of unchanged state. */
2037 bzero(&ce
, sizeof(ce
));
2038 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2041 ce
.width
= WIDTH(win
);
2042 ce
.height
= HEIGHT(win
);
2043 ce
.override_redirect
= 0;
2048 ce
.window
= win
->id
;
2049 ce
.border_width
= BORDER(win
);
2050 ce
.above_sibling
= XCB_WINDOW_NONE
;
2053 ce
.event
= ev
->window
;
2054 ce
.window
= ev
->window
;
2056 /* make response appear more WM_SIZE_HINTS-compliant */
2058 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2059 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2060 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2061 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2062 SH_INC_W(win
), SH_INC_H(win
));
2066 /* the hint may be set... to 0! */
2067 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2068 ce
.width
= SH_MIN_W(win
);
2069 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2070 ce
.height
= SH_MIN_H(win
);
2075 /* may also be advertized as 0 */
2076 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2077 ce
.width
= SH_MAX_W(win
);
2078 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2079 ce
.height
= SH_MAX_H(win
);
2082 /* resize increment. */
2084 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2085 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2087 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2088 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2092 /* adjust x and y for requested border_width. */
2093 ce
.x
+= BORDER(win
) - ev
->border_width
;
2094 ce
.y
+= BORDER(win
) - ev
->border_width
;
2095 ce
.border_width
= ev
->border_width
;
2096 ce
.above_sibling
= ev
->sibling
;
2099 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2100 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2101 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2103 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2108 count_win(struct workspace
*ws
, int count_transient
)
2113 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2114 if (count_transient
== 0 && win
->floating
)
2116 if (count_transient
== 0 && win
->transient
)
2122 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2128 quit(struct swm_region
*r
, union arg
*args
)
2130 /* suppress unused warnings since vars are needed */
2134 DNPRINTF(SWM_D_MISC
, "quit\n");
2139 unmap_window(struct ws_win
*win
)
2141 DNPRINTF(SWM_D_EVENT
, "unmap_window: window: 0x%x\n", win
->id
);
2146 /* don't unmap again */
2147 if (getstate(win
->id
) == XCB_ICCCM_WM_STATE_ICONIC
)
2150 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2152 xcb_unmap_window(conn
, win
->id
);
2153 xcb_change_window_attributes(conn
, win
->id
,
2154 XCB_CW_BORDER_PIXEL
, &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
2161 int i
, j
, num_screens
;
2163 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2164 for (i
= 0; i
< num_screens
; i
++)
2165 for (j
= 0; j
< workspace_limit
; j
++)
2166 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2171 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2173 xcb_key_press_event_t event
;
2174 xcb_keycode_t
*keycode
;
2179 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2181 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2184 bzero(&event
, sizeof(event
));
2185 event
.event
= win
->id
;
2186 event
.root
= win
->s
->root
;
2187 event
.child
= XCB_WINDOW_NONE
;
2188 event
.time
= XCB_CURRENT_TIME
;
2189 event
.event_x
= X(win
);
2190 event
.event_y
= Y(win
);
2193 event
.same_screen
= 1;
2194 event
.detail
= *keycode
;
2195 event
.state
= modifiers
;
2197 event
.response_type
= XCB_KEY_PRESS
;
2198 xcb_send_event(conn
, 1, win
->id
,
2199 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
2201 event
.response_type
= XCB_KEY_RELEASE
;
2202 xcb_send_event(conn
, 1, win
->id
,
2203 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
2209 restart(struct swm_region
*r
, union arg
*args
)
2211 /* suppress unused warning since var is needed */
2214 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2216 /* disable alarm because the following code may not be interrupted */
2218 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
2219 err(1, "can't disable alarm");
2225 XftFontClose(display
, bar_font
);
2226 XftColorFree(display
, DefaultVisual(display
, r
->s
->idx
),
2227 DefaultColormap(display
, r
->s
->idx
), &bar_font_color
);
2228 xcb_key_symbols_free(syms
);
2230 xcb_disconnect(conn
);
2232 execvp(start_argv
[0], start_argv
);
2233 warn("execvp failed");
2238 root_to_region(xcb_window_t root
)
2240 struct swm_region
*r
= NULL
;
2242 xcb_query_pointer_reply_t
*qpr
;
2244 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2246 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2247 for (i
= 0; i
< num_screens
; i
++)
2248 if (screens
[i
].root
== root
)
2251 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2252 screens
[i
].root
), NULL
);
2255 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: (%d,%d)\n",
2256 qpr
->root_x
, qpr
->root_y
);
2257 /* choose a region based on pointer location */
2258 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2259 if (X(r
) <= qpr
->root_x
&& qpr
->root_x
< MAX_X(r
) &&
2260 Y(r
) <= qpr
->root_y
&& qpr
->root_y
< MAX_Y(r
))
2266 r
= TAILQ_FIRST(&screens
[i
].rl
);
2272 find_unmanaged_window(xcb_window_t id
)
2275 int i
, j
, num_screens
;
2277 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2278 for (i
= 0; i
< num_screens
; i
++)
2279 for (j
= 0; j
< workspace_limit
; j
++)
2280 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2288 find_window(xcb_window_t id
)
2291 int i
, j
, num_screens
;
2292 xcb_query_tree_reply_t
*r
;
2294 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2295 for (i
= 0; i
< num_screens
; i
++)
2296 for (j
= 0; j
< workspace_limit
; j
++)
2297 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2301 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
2305 /* if we were looking for the parent return that window instead */
2306 if (r
->parent
== 0 || r
->root
== r
->parent
) {
2311 /* look for parent */
2312 for (i
= 0; i
< num_screens
; i
++)
2313 for (j
= 0; j
< workspace_limit
; j
++)
2314 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2315 if (r
->parent
== win
->id
) {
2325 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2330 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2332 close(xcb_get_file_descriptor(conn
));
2334 setenv("LD_PRELOAD", SWM_LIB
, 1);
2336 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2337 warn("spawn: asprintf SWM_WS");
2340 setenv("_SWM_WS", ret
, 1);
2344 if (asprintf(&ret
, "%d", getpid()) == -1) {
2345 warn("spawn: asprintf _SWM_PID");
2348 setenv("_SWM_PID", ret
, 1);
2352 if (setsid() == -1) {
2353 warn("spawn: setsid");
2359 * close stdin and stdout to prevent interaction between apps
2360 * and the baraction script
2361 * leave stderr open to record errors
2363 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2364 warn("spawn: open");
2367 dup2(fd
, STDIN_FILENO
);
2368 dup2(fd
, STDOUT_FILENO
);
2373 execvp(args
->argv
[0], args
->argv
);
2375 warn("spawn: execvp");
2380 kill_refs(struct ws_win
*win
)
2382 int i
, x
, num_screens
;
2383 struct swm_region
*r
;
2384 struct workspace
*ws
;
2389 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2390 for (i
= 0; i
< num_screens
; i
++)
2391 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2392 for (x
= 0; x
< workspace_limit
; x
++) {
2394 if (win
== ws
->focus
)
2396 if (win
== ws
->focus_prev
)
2397 ws
->focus_prev
= NULL
;
2402 validate_win(struct ws_win
*testwin
)
2405 struct workspace
*ws
;
2406 struct swm_region
*r
;
2407 int i
, x
, num_screens
;
2409 if (testwin
== NULL
)
2412 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2413 for (i
= 0; i
< num_screens
; i
++)
2414 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2415 for (x
= 0; x
< workspace_limit
; x
++) {
2417 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2425 validate_ws(struct workspace
*testws
)
2427 struct swm_region
*r
;
2428 struct workspace
*ws
;
2429 int i
, x
, num_screens
;
2431 /* validate all ws */
2432 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2433 for (i
= 0; i
< num_screens
; i
++)
2434 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2435 for (x
= 0; x
< workspace_limit
; x
++) {
2444 unfocus_win(struct ws_win
*win
)
2446 xcb_window_t none
= XCB_WINDOW_NONE
;
2448 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
2452 if (win
->ws
== NULL
)
2455 if (validate_ws(win
->ws
))
2456 return; /* XXX this gets hit with thunderbird, needs fixing */
2458 if (win
->ws
->r
== NULL
)
2461 if (validate_win(win
)) {
2466 if (win
->ws
->focus
== win
) {
2467 win
->ws
->focus
= NULL
;
2468 win
->ws
->focus_prev
= win
;
2471 if (validate_win(win
->ws
->focus
)) {
2472 kill_refs(win
->ws
->focus
);
2473 win
->ws
->focus
= NULL
;
2475 if (validate_win(win
->ws
->focus_prev
)) {
2476 kill_refs(win
->ws
->focus_prev
);
2477 win
->ws
->focus_prev
= NULL
;
2480 grabbuttons(win
, 0);
2481 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2482 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
2483 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2484 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
2491 int i
, j
, num_screens
;
2493 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2495 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2496 for (i
= 0; i
< num_screens
; i
++)
2497 for (j
= 0; j
< workspace_limit
; j
++)
2498 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2503 focus_win(struct ws_win
*win
)
2505 struct ws_win
*cfw
= NULL
;
2506 xcb_get_input_focus_reply_t
*r
;
2507 xcb_window_t cur_focus
= XCB_WINDOW_NONE
;
2509 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
2513 if (win
->ws
== NULL
)
2516 if (validate_ws(win
->ws
))
2517 return; /* XXX this gets hit with thunderbird, needs fixing */
2519 if (validate_win(win
)) {
2524 if (validate_win(win
)) {
2529 r
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
2531 cur_focus
= r
->focus
;
2534 if ((cfw
= find_window(cur_focus
)) != NULL
)
2538 /* use larger hammer since the window was killed somehow */
2539 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2540 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2541 xcb_change_window_attributes(conn
, cfw
->id
,
2542 XCB_CW_BORDER_PIXEL
,
2543 &cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
2547 win
->ws
->focus
= win
;
2549 if (win
->ws
->r
!= NULL
) {
2551 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
2552 win
->id
, XCB_CURRENT_TIME
);
2553 grabbuttons(win
, 1);
2554 xcb_change_window_attributes(conn
, win
->id
,
2555 XCB_CW_BORDER_PIXEL
,
2556 &win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
);
2557 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2558 win
->ws
->always_raise
)
2559 map_window_raised(win
->id
);
2561 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2562 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2570 event_drain(uint8_t rt
)
2572 xcb_generic_event_t
*evt
;
2575 while ((evt
= xcb_poll_for_event(conn
))) {
2576 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
2584 switchws(struct swm_region
*r
, union arg
*args
)
2586 int wsid
= args
->id
, unmap_old
= 0;
2587 struct swm_region
*this_r
, *other_r
;
2589 struct workspace
*new_ws
, *old_ws
;
2595 if (wsid
>= workspace_limit
)
2599 old_ws
= this_r
->ws
;
2600 new_ws
= &this_r
->s
->ws
[wsid
];
2602 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2603 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2605 if (new_ws
== NULL
|| old_ws
== NULL
)
2607 if (new_ws
== old_ws
)
2610 other_r
= new_ws
->r
;
2611 if (other_r
== NULL
) {
2612 /* the other workspace is hidden, hide this one */
2616 /* the other ws is visible in another region, exchange them */
2617 other_r
->ws_prior
= new_ws
;
2618 other_r
->ws
= old_ws
;
2619 old_ws
->r
= other_r
;
2621 this_r
->ws_prior
= old_ws
;
2622 this_r
->ws
= new_ws
;
2625 /* this is needed so that we can click on a window after a restart */
2629 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2630 focus(new_ws
->r
, &a
);
2632 /* unmap old windows */
2634 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2637 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2638 event_drain(XCB_ENTER_NOTIFY
);
2642 DNPRINTF(SWM_D_WS
, "switchws: done\n");
2646 cyclews(struct swm_region
*r
, union arg
*args
)
2649 struct swm_screen
*s
= r
->s
;
2652 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2653 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2658 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2661 case SWM_ARG_ID_CYCLEWS_UP
:
2662 if (a
.id
< workspace_limit
- 1)
2667 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2670 case SWM_ARG_ID_CYCLEWS_DOWN
:
2674 a
.id
= workspace_limit
- 1;
2681 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2683 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2687 } while (a
.id
!= r
->ws
->idx
);
2691 priorws(struct swm_region
*r
, union arg
*args
)
2695 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2696 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2698 if (r
->ws_prior
== NULL
)
2701 a
.id
= r
->ws_prior
->idx
;
2706 cyclescr(struct swm_region
*r
, union arg
*args
)
2708 struct swm_region
*rr
= NULL
;
2710 int i
, x
, y
, num_screens
;
2712 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2713 /* do nothing if we don't have more than one screen */
2714 if (!(num_screens
> 1 || outputs
> 1))
2719 case SWM_ARG_ID_CYCLESC_UP
:
2720 rr
= TAILQ_NEXT(r
, entry
);
2722 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2724 case SWM_ARG_ID_CYCLESC_DOWN
:
2725 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2727 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2735 /* move mouse to region */
2737 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2738 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0, 0,
2741 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2744 if (rr
->ws
->focus
) {
2745 /* move to focus window */
2746 x
= X(rr
->ws
->focus
) + 1;
2747 y
= Y(rr
->ws
->focus
) + 1;
2748 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0,
2754 sort_windows(struct ws_win_list
*wl
)
2756 struct ws_win
*win
, *parent
, *nxt
;
2761 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2762 nxt
= TAILQ_NEXT(win
, entry
);
2763 if (win
->transient
) {
2764 parent
= find_window(win
->transient
);
2765 if (parent
== NULL
) {
2766 warnx("not possible bug");
2769 TAILQ_REMOVE(wl
, win
, entry
);
2770 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2777 swapwin(struct swm_region
*r
, union arg
*args
)
2779 struct ws_win
*target
, *source
;
2780 struct ws_win
*cur_focus
;
2781 struct ws_win_list
*wl
;
2784 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2785 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2787 cur_focus
= r
->ws
->focus
;
2788 if (cur_focus
== NULL
)
2792 wl
= &source
->ws
->winlist
;
2795 case SWM_ARG_ID_SWAPPREV
:
2796 if (source
->transient
)
2797 source
= find_window(source
->transient
);
2798 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2799 if (target
&& target
->transient
)
2800 target
= find_window(target
->transient
);
2801 TAILQ_REMOVE(wl
, source
, entry
);
2803 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2805 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2807 case SWM_ARG_ID_SWAPNEXT
:
2808 target
= TAILQ_NEXT(source
, entry
);
2809 /* move the parent and let the sort handle the move */
2810 if (source
->transient
)
2811 source
= find_window(source
->transient
);
2812 TAILQ_REMOVE(wl
, source
, entry
);
2814 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2816 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2818 case SWM_ARG_ID_SWAPMAIN
:
2819 target
= TAILQ_FIRST(wl
);
2820 if (target
== source
) {
2821 if (source
->ws
->focus_prev
!= NULL
&&
2822 source
->ws
->focus_prev
!= target
)
2823 source
= source
->ws
->focus_prev
;
2827 if (target
== NULL
|| source
== NULL
)
2829 source
->ws
->focus_prev
= target
;
2830 TAILQ_REMOVE(wl
, target
, entry
);
2831 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2832 TAILQ_REMOVE(wl
, source
, entry
);
2833 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2835 case SWM_ARG_ID_MOVELAST
:
2836 TAILQ_REMOVE(wl
, source
, entry
);
2837 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2840 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2852 focus_prev(struct ws_win
*win
)
2854 struct ws_win
*winfocus
= NULL
;
2855 struct ws_win
*cur_focus
= NULL
;
2856 struct ws_win_list
*wl
= NULL
;
2857 struct workspace
*ws
= NULL
;
2859 if (!(win
&& win
->ws
))
2864 cur_focus
= ws
->focus
;
2866 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2867 WINID(win
), WINID(cur_focus
));
2869 /* pickle, just focus on whatever */
2870 if (cur_focus
== NULL
) {
2871 /* use prev_focus if valid */
2872 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2873 find_window(WINID(ws
->focus_prev
)))
2874 winfocus
= ws
->focus_prev
;
2878 /* if transient focus on parent */
2879 if (cur_focus
->transient
) {
2880 winfocus
= find_window(cur_focus
->transient
);
2884 /* if in max_stack try harder */
2885 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2886 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2887 if (cur_focus
!= ws
->focus_prev
)
2888 winfocus
= ws
->focus_prev
;
2890 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2895 DNPRINTF(SWM_D_FOCUS
, "focus_prev: focus_close: %d\n", focus_close
);
2897 if (winfocus
== NULL
|| winfocus
== win
) {
2898 switch (focus_close
) {
2899 case SWM_STACK_BOTTOM
:
2900 winfocus
= TAILQ_FIRST(wl
);
2903 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2905 case SWM_STACK_ABOVE
:
2906 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
2907 if (focus_close_wrap
)
2908 winfocus
= TAILQ_FIRST(wl
);
2910 winfocus
= TAILQ_PREV(cur_focus
,
2911 ws_win_list
, entry
);
2914 case SWM_STACK_BELOW
:
2915 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
2917 if (focus_close_wrap
)
2918 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2920 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2926 if (winfocus
== NULL
) {
2927 if (focus_default
== SWM_STACK_TOP
)
2928 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2930 winfocus
= TAILQ_FIRST(wl
);
2934 focus_magic(winfocus
);
2938 focus(struct swm_region
*r
, union arg
*args
)
2940 struct ws_win
*winfocus
= NULL
, *head
;
2941 struct ws_win
*cur_focus
= NULL
;
2942 struct ws_win_list
*wl
= NULL
;
2943 struct workspace
*ws
= NULL
;
2949 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2951 /* treat FOCUS_CUR special */
2952 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2953 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2954 winfocus
= r
->ws
->focus
;
2955 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2956 winfocus
= r
->ws
->focus_prev
;
2958 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2959 if (winfocus
->iconic
== 0)
2962 focus_magic(winfocus
);
2966 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2970 if (TAILQ_EMPTY(wl
))
2972 /* make sure there is at least one uniconified window */
2974 TAILQ_FOREACH(winfocus
, wl
, entry
)
2975 if (winfocus
->iconic
== 0) {
2983 case SWM_ARG_ID_FOCUSPREV
:
2984 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2986 head
= TAILQ_LAST(wl
, ws_win_list
);
2988 if (WINID(winfocus
) == cur_focus
->transient
) {
2989 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2991 head
= TAILQ_LAST(wl
, ws_win_list
);
2996 if (winfocus
&& winfocus
->iconic
) {
2997 while (winfocus
!= cur_focus
) {
2998 if (winfocus
== NULL
)
2999 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3000 if (winfocus
->iconic
== 0)
3002 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3008 case SWM_ARG_ID_FOCUSNEXT
:
3009 head
= TAILQ_NEXT(cur_focus
, entry
);
3011 head
= TAILQ_FIRST(wl
);
3015 if (winfocus
&& winfocus
->iconic
) {
3016 while (winfocus
!= cur_focus
) {
3017 if (winfocus
== NULL
)
3018 winfocus
= TAILQ_FIRST(wl
);
3019 if (winfocus
->iconic
== 0)
3021 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3026 case SWM_ARG_ID_FOCUSMAIN
:
3027 winfocus
= TAILQ_FIRST(wl
);
3028 if (winfocus
== cur_focus
)
3029 winfocus
= cur_focus
->ws
->focus_prev
;
3036 focus_magic(winfocus
);
3042 cycle_layout(struct swm_region
*r
, union arg
*args
)
3044 struct workspace
*ws
= r
->ws
;
3047 /* suppress unused warning since var is needed */
3050 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3053 if (ws
->cur_layout
->l_stack
== NULL
)
3054 ws
->cur_layout
= &layouts
[0];
3059 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3060 event_drain(XCB_ENTER_NOTIFY
);
3062 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3067 stack_config(struct swm_region
*r
, union arg
*args
)
3069 struct workspace
*ws
= r
->ws
;
3071 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3074 if (ws
->cur_layout
->l_config
!= NULL
)
3075 ws
->cur_layout
->l_config(ws
, args
->id
);
3077 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3084 struct swm_geometry g
;
3085 struct swm_region
*r
;
3091 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3093 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3094 for (i
= 0; i
< num_screens
; i
++) {
3098 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3099 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3100 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3102 /* start with screen geometry, adjust for bar */
3104 g
.w
-= 2 * border_width
;
3105 g
.h
-= 2 * border_width
;
3111 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3112 r
->ws
->cur_layout
->l_string(r
->ws
);
3113 /* save r so we can track region changes */
3120 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3121 event_drain(XCB_ENTER_NOTIFY
);
3123 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3127 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3129 /* retain window geom and region geom */
3130 win
->g_float
= win
->g
;
3131 win
->g_float
.x
-= X(r
);
3132 win
->g_float
.y
-= Y(r
);
3133 win
->g_floatvalid
= 1;
3134 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
3135 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3136 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3137 win
->g_float
.w
, win
->g_float
.h
);
3141 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3146 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
3149 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3150 * geom on ws switches or return from max mode
3152 if (win
->g_floatvalid
&& (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3153 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)))) {
3154 /* refloat at last floating relative position */
3155 win
->g
= win
->g_float
;
3160 win
->floatmaxed
= 0;
3163 * if set to fullscreen mode, configure window to maximum size.
3165 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3166 if (!win
->g_floatvalid
)
3167 store_float_geom(win
, win
->ws
->r
);
3169 win
->g
= win
->ws
->r
->g
;
3173 * remove border on fullscreen floater when in fullscreen mode or when
3174 * the quirk is present.
3176 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3177 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3178 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3179 if (win
->bordered
) {
3181 X(win
) += border_width
;
3182 Y(win
) += border_width
;
3184 } else if (!win
->bordered
) {
3186 X(win
) -= border_width
;
3187 Y(win
) -= border_width
;
3190 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3191 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3192 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3197 * floaters and transients are auto-centred unless moved
3200 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3201 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3204 /* keep window within region bounds */
3205 constrain_window(win
, r
, 0);
3211 * Send keystrokes to terminal to decrease/increase the font size as the
3212 * window size changes.
3215 adjust_font(struct ws_win
*win
)
3217 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3218 win
->floating
|| win
->transient
)
3221 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3222 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3223 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3224 win
->font_size_boundary
[win
->font_steps
] =
3225 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3228 win
->last_inc
= win
->sh
.width_inc
;
3229 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
3230 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3231 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3234 win
->last_inc
= win
->sh
.width_inc
;
3235 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
3239 #define SWAPXY(g) do { \
3241 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3242 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3245 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3247 struct swm_geometry win_g
, r_g
= *g
;
3248 struct ws_win
*win
, *fs_win
= NULL
;
3249 int i
, j
, s
, stacks
;
3250 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3251 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3252 int split
, colno
, winno
, mwin
, msize
, mscale
;
3253 int remain
, missing
, v_slice
, reconfigure
;
3255 xcb_get_window_attributes_reply_t
*war
;
3257 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3258 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3260 winno
= count_win(ws
, 0);
3261 if (winno
== 0 && count_win(ws
, 1) == 0)
3264 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3265 if (win
->transient
== 0 && win
->floating
== 0
3266 && win
->iconic
== 0)
3273 w_inc
= win
->sh
.width_inc
;
3274 w_base
= win
->sh
.base_width
;
3275 mwin
= ws
->l_state
.horizontal_mwin
;
3276 mscale
= ws
->l_state
.horizontal_msize
;
3277 stacks
= ws
->l_state
.horizontal_stacks
;
3280 w_inc
= win
->sh
.height_inc
;
3281 w_base
= win
->sh
.base_height
;
3282 mwin
= ws
->l_state
.vertical_mwin
;
3283 mscale
= ws
->l_state
.vertical_msize
;
3284 stacks
= ws
->l_state
.vertical_stacks
;
3288 if (stacks
> winno
- mwin
)
3289 stacks
= winno
- mwin
;
3293 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3294 if (mwin
&& winno
> mwin
) {
3295 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3299 win_g
.w
= v_slice
* mscale
;
3301 if (w_inc
> 1 && w_inc
< v_slice
) {
3302 /* adjust for window's requested size increment */
3303 remain
= (win_g
.w
- w_base
) % w_inc
;
3309 win_g
.x
+= r_g
.w
- msize
;
3312 colno
= split
= winno
/ stacks
;
3313 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3314 2 * border_width
) / stacks
);
3316 hrh
= r_g
.h
/ colno
;
3317 extra
= r_g
.h
- (colno
* hrh
);
3318 win_g
.h
= hrh
- 2 * border_width
;
3320 /* stack all the tiled windows */
3321 i
= j
= 0, s
= stacks
;
3322 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3323 if (win
->transient
!= 0 || win
->floating
!= 0)
3325 if (win
->iconic
!= 0)
3328 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3333 if (split
&& i
== split
) {
3334 colno
= (winno
- mwin
) / stacks
;
3335 if (s
<= (winno
- mwin
) % stacks
)
3337 split
= split
+ colno
;
3338 hrh
= (r_g
.h
/ colno
);
3339 extra
= r_g
.h
- (colno
* hrh
);
3343 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3344 win_g
.w
= (r_g
.w
- msize
-
3345 (stacks
* 2 * border_width
)) / stacks
;
3347 win_g
.w
+= (r_g
.w
- msize
-
3348 (stacks
* 2 * border_width
)) % stacks
;
3352 win_g
.h
= hrh
- 2 * border_width
;
3354 h_inc
= win
->sh
.width_inc
;
3355 h_base
= win
->sh
.base_width
;
3357 h_inc
= win
->sh
.height_inc
;
3358 h_base
= win
->sh
.base_height
;
3360 if (j
== colno
- 1) {
3361 win_g
.h
= hrh
+ extra
;
3362 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3363 /* adjust for window's requested size increment */
3364 remain
= (win_g
.h
- h_base
) % h_inc
;
3365 missing
= h_inc
- remain
;
3367 if (missing
<= extra
|| j
== 0) {
3379 win_g
.y
+= last_h
+ 2 * border_width
;
3381 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3383 win_g
.w
+= 2 * border_width
;
3384 win_g
.h
+= 2 * border_width
;
3389 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3390 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3394 WIDTH(win
) = win_g
.h
;
3395 HEIGHT(win
) = win_g
.w
;
3398 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3399 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3403 WIDTH(win
) = win_g
.w
;
3404 HEIGHT(win
) = win_g
.h
;
3408 if (bordered
!= win
->bordered
) {
3410 win
->bordered
= bordered
;
3418 war
= xcb_get_window_attributes_reply(conn
,
3419 xcb_get_window_attributes(conn
, win
->id
),
3422 if (war
->map_state
== XCB_MAP_STATE_UNMAPPED
)
3423 map_window_raised(win
->id
);
3433 /* now, stack all the floaters and transients */
3434 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3435 if (win
->transient
== 0 && win
->floating
== 0)
3437 if (win
->iconic
== 1)
3439 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3444 stack_floater(win
, ws
->r
);
3445 map_window_raised(win
->id
);
3449 stack_floater(fs_win
, ws
->r
);
3450 map_window_raised(fs_win
->id
);
3455 vertical_config(struct workspace
*ws
, int id
)
3457 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3461 case SWM_ARG_ID_STACKRESET
:
3462 case SWM_ARG_ID_STACKINIT
:
3463 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3464 ws
->l_state
.vertical_mwin
= 1;
3465 ws
->l_state
.vertical_stacks
= 1;
3467 case SWM_ARG_ID_MASTERSHRINK
:
3468 if (ws
->l_state
.vertical_msize
> 1)
3469 ws
->l_state
.vertical_msize
--;
3471 case SWM_ARG_ID_MASTERGROW
:
3472 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3473 ws
->l_state
.vertical_msize
++;
3475 case SWM_ARG_ID_MASTERADD
:
3476 ws
->l_state
.vertical_mwin
++;
3478 case SWM_ARG_ID_MASTERDEL
:
3479 if (ws
->l_state
.vertical_mwin
> 0)
3480 ws
->l_state
.vertical_mwin
--;
3482 case SWM_ARG_ID_STACKINC
:
3483 ws
->l_state
.vertical_stacks
++;
3485 case SWM_ARG_ID_STACKDEC
:
3486 if (ws
->l_state
.vertical_stacks
> 1)
3487 ws
->l_state
.vertical_stacks
--;
3489 case SWM_ARG_ID_FLIPLAYOUT
:
3490 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3498 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3500 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3502 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3506 horizontal_config(struct workspace
*ws
, int id
)
3508 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3511 case SWM_ARG_ID_STACKRESET
:
3512 case SWM_ARG_ID_STACKINIT
:
3513 ws
->l_state
.horizontal_mwin
= 1;
3514 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3515 ws
->l_state
.horizontal_stacks
= 1;
3517 case SWM_ARG_ID_MASTERSHRINK
:
3518 if (ws
->l_state
.horizontal_msize
> 1)
3519 ws
->l_state
.horizontal_msize
--;
3521 case SWM_ARG_ID_MASTERGROW
:
3522 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3523 ws
->l_state
.horizontal_msize
++;
3525 case SWM_ARG_ID_MASTERADD
:
3526 ws
->l_state
.horizontal_mwin
++;
3528 case SWM_ARG_ID_MASTERDEL
:
3529 if (ws
->l_state
.horizontal_mwin
> 0)
3530 ws
->l_state
.horizontal_mwin
--;
3532 case SWM_ARG_ID_STACKINC
:
3533 ws
->l_state
.horizontal_stacks
++;
3535 case SWM_ARG_ID_STACKDEC
:
3536 if (ws
->l_state
.horizontal_stacks
> 1)
3537 ws
->l_state
.horizontal_stacks
--;
3539 case SWM_ARG_ID_FLIPLAYOUT
:
3540 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3548 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3550 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3552 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3555 /* fullscreen view */
3557 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3559 struct swm_geometry gg
= *g
;
3560 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3561 int winno
, num_screens
;
3563 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3568 winno
= count_win(ws
, 0);
3569 if (winno
== 0 && count_win(ws
, 1) == 0)
3572 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3573 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3574 if (win
->transient
) {
3576 parent
= find_window(win
->transient
);
3580 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3582 * retain geometry for retrieval on exit from
3585 store_float_geom(win
, ws
->r
);
3586 win
->floatmaxed
= 1;
3589 /* only reconfigure if necessary */
3590 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3591 HEIGHT(win
) != gg
.h
) {
3597 WIDTH(win
) += 2 * border_width
;
3598 HEIGHT(win
) += 2 * border_width
;
3603 /* unmap only if we don't have multi screen */
3604 if (win
!= ws
->focus
)
3605 if (!(num_screens
> 1 || outputs
> 1))
3609 /* put the last transient on top */
3612 map_window_raised(parent
->id
);
3613 stack_floater(wintrans
, ws
->r
);
3614 focus_magic(wintrans
);
3619 send_to_ws(struct swm_region
*r
, union arg
*args
)
3621 int wsid
= args
->id
;
3622 struct ws_win
*win
= NULL
, *parent
;
3623 struct workspace
*ws
, *nws
;
3624 char ws_idx_str
[SWM_PROPLEN
];
3627 if (wsid
>= workspace_limit
)
3630 if (r
&& r
->ws
&& r
->ws
->focus
)
3636 if (win
->ws
->idx
== wsid
)
3639 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%x\n", win
->id
);
3642 nws
= &win
->s
->ws
[wsid
];
3644 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3646 if (win
->transient
) {
3647 parent
= find_window(win
->transient
);
3649 unmap_window(parent
);
3650 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3651 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3656 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3657 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3658 if (TAILQ_EMPTY(&ws
->winlist
))
3659 r
->ws
->focus
= NULL
;
3662 /* Try to update the window's workspace property */
3663 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
3664 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3666 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
3667 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
3678 pressbutton(struct swm_region
*r
, union arg
*args
)
3680 /* suppress unused warning since var is needed */
3683 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
3684 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3685 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
3686 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3690 raise_toggle(struct swm_region
*r
, union arg
*args
)
3692 /* suppress unused warning since var is needed */
3695 if (r
== NULL
|| r
->ws
== NULL
)
3698 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3700 /* bring floaters back to top */
3701 if (r
->ws
->always_raise
== 0)
3708 iconify(struct swm_region
*r
, union arg
*args
)
3712 /* suppress unused warning since var is needed */
3715 if (r
->ws
->focus
== NULL
)
3717 unmap_window(r
->ws
->focus
);
3718 update_iconic(r
->ws
->focus
, 1);
3721 r
->ws
->focus
= NULL
;
3722 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3729 get_win_name(xcb_window_t win
)
3732 xcb_get_property_cookie_t c
;
3733 xcb_icccm_get_text_property_reply_t r
;
3735 c
= xcb_icccm_get_wm_name(conn
, win
);
3736 if (xcb_icccm_get_wm_name_reply(conn
, c
, &r
, NULL
)) {
3737 if (r
.name_len
> 0) {
3738 name
= malloc(r
.name_len
+ 1);
3740 memcpy(name
, r
.name
, r
.name_len
);
3741 name
[r
.name_len
] = '\0';
3744 xcb_icccm_get_text_property_reply_wipe(&r
);
3751 uniconify(struct swm_region
*r
, union arg
*args
)
3758 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3760 if (r
== NULL
|| r
->ws
== NULL
)
3763 /* make sure we have anything to uniconify */
3764 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3765 if (win
->ws
== NULL
)
3766 continue; /* should never happen */
3767 if (win
->iconic
== 0)
3775 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3777 spawn_select(r
, args
, "search", &searchpid
);
3779 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3782 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3783 if (win
->ws
== NULL
)
3784 continue; /* should never happen */
3785 if (win
->iconic
== 0)
3788 name
= get_win_name(win
->id
);
3791 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
3801 name_workspace(struct swm_region
*r
, union arg
*args
)
3805 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3811 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3813 spawn_select(r
, args
, "name_workspace", &searchpid
);
3815 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3818 fprintf(lfile
, "%s", "");
3823 search_workspace(struct swm_region
*r
, union arg
*args
)
3826 struct workspace
*ws
;
3829 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3835 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3837 spawn_select(r
, args
, "search", &searchpid
);
3839 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3842 for (i
= 0; i
< workspace_limit
; i
++) {
3846 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3847 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3854 search_win_cleanup(void)
3856 struct search_window
*sw
= NULL
;
3858 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3859 xcb_destroy_window(conn
, sw
->indicator
);
3860 xcb_free_gc(conn
, sw
->gc
);
3861 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3867 search_win(struct swm_region
*r
, union arg
*args
)
3869 struct ws_win
*win
= NULL
;
3870 struct search_window
*sw
= NULL
;
3872 uint32_t gcv
[3], wa
[2];
3880 DNPRINTF(SWM_D_MISC
, "search_win\n");
3883 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3885 spawn_select(r
, args
, "search", &searchpid
);
3887 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3890 TAILQ_INIT(&search_wl
);
3893 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3894 if (win
->iconic
== 1)
3897 sw
= calloc(1, sizeof(struct search_window
));
3899 warn("search_win: calloc");
3901 search_win_cleanup();
3907 snprintf(s
, sizeof s
, "%d", i
);
3910 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
3912 w
= xcb_generate_id(conn
);
3913 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
3914 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
3915 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
3916 info
.width
+ 4, bar_font
->height
+ 4,
3917 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
3918 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
3921 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3923 sw
->gc
= xcb_generate_id(conn
);
3924 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
3925 gcv
[1] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
3927 xcb_create_gc(conn
, sw
->gc
, w
, XCB_GC_FOREGROUND
|
3928 XCB_GC_BACKGROUND
| XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
3929 map_window_raised(w
);
3931 draw
= XftDrawCreate(display
, w
,
3932 DefaultVisual(display
, r
->s
->idx
),
3933 DefaultColormap(display
, r
->s
->idx
));
3935 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
3936 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
3939 XftDrawDestroy(draw
);
3941 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
3943 fprintf(lfile
, "%d\n", i
);
3953 search_resp_uniconify(char *resp
, unsigned long len
)
3959 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3961 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3962 if (win
->iconic
== 0)
3964 name
= get_win_name(win
->id
);
3967 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
3972 if (strncmp(s
, resp
, len
) == 0) {
3973 /* XXX this should be a callback to generalize */
3974 update_iconic(win
, 0);
3983 search_resp_name_workspace(char *resp
, unsigned long len
)
3985 struct workspace
*ws
;
3987 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3989 if (search_r
->ws
== NULL
)
3994 free(search_r
->ws
->name
);
3995 search_r
->ws
->name
= NULL
;
3999 ws
->name
= strdup(resp
);
4000 if (ws
->name
== NULL
) {
4001 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
4002 "strdup: %s", strerror(errno
));
4009 search_resp_search_workspace(char *resp
)
4016 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
4020 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
4027 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
4029 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
4036 switchws(search_r
, &a
);
4040 search_resp_search_window(char *resp
)
4045 struct search_window
*sw
;
4047 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
4051 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
4056 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
4058 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
4065 TAILQ_FOREACH(sw
, &search_wl
, entry
)
4066 if (idx
== sw
->idx
) {
4072 #define MAX_RESP_LEN 1024
4075 search_do_resp(void)
4081 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
4086 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
4087 warn("search: calloc");
4091 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
4093 warn("search: read error");
4096 resp
[rbytes
] = '\0';
4099 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4100 * newline, so work around that by sanitizing the resp now.
4102 resp
[strcspn(resp
, "\n")] = '\0';
4105 switch (search_resp_action
) {
4106 case SWM_SEARCH_UNICONIFY
:
4107 search_resp_uniconify(resp
, len
);
4109 case SWM_SEARCH_NAME_WORKSPACE
:
4110 search_resp_name_workspace(resp
, len
);
4112 case SWM_SEARCH_SEARCH_WORKSPACE
:
4113 search_resp_search_workspace(resp
);
4115 case SWM_SEARCH_SEARCH_WINDOW
:
4116 search_resp_search_window(resp
);
4121 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4122 search_win_cleanup();
4124 search_resp_action
= SWM_SEARCH_NONE
;
4125 close(select_resp_pipe
[0]);
4130 wkill(struct swm_region
*r
, union arg
*args
)
4132 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4134 if (r
->ws
->focus
== NULL
)
4137 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4138 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4140 if (r
->ws
->focus
->can_delete
)
4141 client_msg(r
->ws
->focus
, a_delete
);
4148 floating_toggle_win(struct ws_win
*win
)
4150 struct swm_region
*r
;
4160 /* reject floating toggles in max stack mode */
4161 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4164 if (win
->floating
) {
4165 if (!win
->floatmaxed
) {
4166 /* retain position for refloat */
4167 store_float_geom(win
, r
);
4171 if (win
->g_floatvalid
) {
4172 /* refloat at last floating relative position */
4173 X(win
) = win
->g_float
.x
+ X(r
);
4174 Y(win
) = win
->g_float
.y
+ Y(r
);
4175 WIDTH(win
) = win
->g_float
.w
;
4176 HEIGHT(win
) = win
->g_float
.h
;
4181 ewmh_update_actions(win
);
4187 floating_toggle(struct swm_region
*r
, union arg
*args
)
4189 struct ws_win
*win
= r
->ws
->focus
;
4192 /* suppress unused warning since var is needed */
4198 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4199 _NET_WM_STATE_TOGGLE
);
4203 if (win
== win
->ws
->focus
) {
4204 a
.id
= SWM_ARG_ID_FOCUSCUR
;
4205 focus(win
->ws
->r
, &a
);
4209 event_drain(XCB_ENTER_NOTIFY
);
4213 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4215 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4217 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4219 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4222 if (X(win
) + BORDER(win
) < X(r
)) {
4224 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4226 X(win
) = X(r
) - BORDER(win
);
4229 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4231 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4233 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4236 if (Y(win
) + BORDER(win
) < Y(r
)) {
4238 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4240 Y(win
) = Y(r
) - BORDER(win
);
4246 if (HEIGHT(win
) < 1)
4252 update_window(struct ws_win
*win
)
4257 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
4258 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
4259 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
4263 wc
[3] = HEIGHT(win
);
4264 wc
[4] = BORDER(win
);
4266 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
4267 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
4268 wc
[3], YESNO(win
->bordered
));
4270 xcb_configure_window(conn
, win
->id
, mask
, wc
);
4273 #define SWM_RESIZE_STEPS (50)
4276 resize(struct ws_win
*win
, union arg
*args
)
4278 xcb_timestamp_t timestamp
= 0;
4279 struct swm_region
*r
= NULL
;
4280 int resize_step
= 0;
4281 struct swm_geometry g
;
4282 int top
= 0, left
= 0, resizing
;
4284 unsigned int shape
; /* cursor style */
4285 xcb_cursor_t cursor
;
4286 xcb_font_t cursor_font
;
4287 xcb_grab_pointer_cookie_t gpc
;
4288 xcb_grab_pointer_reply_t
*gpr
;
4289 xcb_query_pointer_reply_t
*xpr
;
4290 xcb_generic_event_t
*evt
;
4291 xcb_motion_notify_event_t
*mne
;
4297 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4300 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
4301 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
4304 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4307 /* reject resizes in max mode for floaters (transient ok) */
4308 if (win
->floatmaxed
)
4312 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4318 case SWM_ARG_ID_WIDTHSHRINK
:
4319 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4322 case SWM_ARG_ID_WIDTHGROW
:
4323 WIDTH(win
) += SWM_RESIZE_STEPS
;
4326 case SWM_ARG_ID_HEIGHTSHRINK
:
4327 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4330 case SWM_ARG_ID_HEIGHTGROW
:
4331 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4338 constrain_window(win
, r
, 1);
4340 store_float_geom(win
,r
);
4344 /* get cursor offset from window root */
4345 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4352 if (xpr
->win_x
< WIDTH(win
) / 2)
4355 if (xpr
->win_y
< HEIGHT(win
) / 2)
4358 if (args
->id
== SWM_ARG_ID_CENTER
)
4361 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
4363 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
4365 cursor_font
= xcb_generate_id(conn
);
4366 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4368 cursor
= xcb_generate_id(conn
);
4369 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4370 shape
, shape
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4372 gpc
= xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
4373 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
4375 gpr
= xcb_grab_pointer_reply(conn
, gpc
, NULL
);
4377 xcb_free_cursor(conn
, cursor
);
4378 xcb_close_font(conn
, cursor_font
);
4385 while ((evt
= xcb_wait_for_event(conn
)) && resizing
) {
4386 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4387 case XCB_BUTTON_RELEASE
:
4388 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
4391 case XCB_MOTION_NOTIFY
:
4392 mne
= (xcb_motion_notify_event_t
*)evt
;
4393 /* cursor offset/delta from start of the operation */
4394 dx
= mne
->root_x
- xpr
->root_x
;
4395 dy
= mne
->root_y
- xpr
->root_y
;
4401 if (args
->id
== SWM_ARG_ID_CENTER
) {
4402 if (g
.h
/ 2 + dy
< 1)
4406 HEIGHT(win
) = g
.h
+ 2 * dy
;
4414 HEIGHT(win
) = g
.h
+ dy
;
4421 if (args
->id
== SWM_ARG_ID_CENTER
) {
4422 if (g
.w
/ 2 + dx
< 1)
4426 WIDTH(win
) = g
.w
+ 2 * dx
;
4434 WIDTH(win
) = g
.w
+ dx
;
4437 constrain_window(win
, r
, 1);
4439 /* not free, don't sync more than 120 times / second */
4440 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4441 timestamp
= mne
->time
;
4456 store_float_geom(win
,r
);
4458 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4459 xcb_free_cursor(conn
, cursor
);
4460 xcb_close_font(conn
, cursor_font
);
4463 DNPRINTF(SWM_D_EVENT
, "resize: done\n");
4467 resize_step(struct swm_region
*r
, union arg
*args
)
4469 struct ws_win
*win
= NULL
;
4471 if (r
&& r
->ws
&& r
->ws
->focus
)
4479 #define SWM_MOVE_STEPS (50)
4482 move(struct ws_win
*win
, union arg
*args
)
4484 xcb_timestamp_t timestamp
= 0;
4485 int move_step
= 0, moving
;
4486 struct swm_region
*r
= NULL
;
4487 xcb_font_t cursor_font
;
4488 xcb_cursor_t cursor
;
4489 xcb_grab_pointer_cookie_t gpc
;
4490 xcb_grab_pointer_reply_t
*gpr
;
4491 xcb_query_pointer_reply_t
*qpr
;
4492 xcb_generic_event_t
*evt
;
4493 xcb_motion_notify_event_t
*mne
;
4499 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4502 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
4503 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4505 /* in max_stack mode should only move transients */
4506 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4510 if (win
->floating
== 0 && !win
->transient
) {
4511 store_float_geom(win
, r
);
4512 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4515 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4522 case SWM_ARG_ID_MOVELEFT
:
4523 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4526 case SWM_ARG_ID_MOVERIGHT
:
4527 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4530 case SWM_ARG_ID_MOVEUP
:
4531 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4534 case SWM_ARG_ID_MOVEDOWN
:
4535 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4542 constrain_window(win
, r
, 0);
4544 store_float_geom(win
, r
);
4548 cursor_font
= xcb_generate_id(conn
);
4549 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4551 cursor
= xcb_generate_id(conn
);
4552 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4553 XC_fleur
, XC_fleur
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4555 gpc
= xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
4556 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
4557 XCB_WINDOW_NONE
, cursor
, XCB_CURRENT_TIME
);
4558 gpr
= xcb_grab_pointer_reply(conn
, gpc
, NULL
);
4560 xcb_free_cursor(conn
, cursor
);
4561 xcb_close_font(conn
, cursor_font
);
4566 /* get cursor offset from window root */
4567 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4570 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4571 xcb_free_cursor(conn
, cursor
);
4572 xcb_close_font(conn
, cursor_font
);
4578 while ((evt
= xcb_wait_for_event(conn
)) && moving
) {
4579 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4580 case XCB_BUTTON_RELEASE
:
4581 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
4584 case XCB_MOTION_NOTIFY
:
4585 mne
= (xcb_motion_notify_event_t
*)evt
;
4586 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
4587 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
4589 constrain_window(win
, r
, 0);
4591 /* not free, don't sync more than 120 times / second */
4592 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4593 timestamp
= mne
->time
;
4608 store_float_geom(win
, r
);
4610 xcb_free_cursor(conn
, cursor
);
4611 xcb_close_font(conn
, cursor_font
);
4612 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4613 DNPRINTF(SWM_D_EVENT
, "move: done\n");
4617 move_step(struct swm_region
*r
, union arg
*args
)
4619 struct ws_win
*win
= NULL
;
4621 if (r
&& r
->ws
&& r
->ws
->focus
)
4626 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4633 /* user/key callable function IDs */
4683 KF_SEARCH_WORKSPACE
,
4724 KF_DUMPWINS
, /* MUST BE LAST */
4728 /* key definitions */
4730 char name
[SWM_FUNCNAME_LEN
];
4731 void (*func
)(struct swm_region
*r
, union arg
*);
4733 } keyfuncs
[KF_INVALID
+ 1] = {
4734 /* name function argument */
4735 { "bar_toggle", bar_toggle
, {0} },
4736 { "button2", pressbutton
, {2} },
4737 { "cycle_layout", cycle_layout
, {0} },
4738 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4739 { "float_toggle", floating_toggle
,{0} },
4740 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4741 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4742 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4743 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4744 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4745 { "iconify", iconify
, {0} },
4746 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4747 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4748 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4749 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4750 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4751 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4752 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4753 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4754 { "mvws_1", send_to_ws
, {.id
= 0} },
4755 { "mvws_2", send_to_ws
, {.id
= 1} },
4756 { "mvws_3", send_to_ws
, {.id
= 2} },
4757 { "mvws_4", send_to_ws
, {.id
= 3} },
4758 { "mvws_5", send_to_ws
, {.id
= 4} },
4759 { "mvws_6", send_to_ws
, {.id
= 5} },
4760 { "mvws_7", send_to_ws
, {.id
= 6} },
4761 { "mvws_8", send_to_ws
, {.id
= 7} },
4762 { "mvws_9", send_to_ws
, {.id
= 8} },
4763 { "mvws_10", send_to_ws
, {.id
= 9} },
4764 { "mvws_11", send_to_ws
, {.id
= 10} },
4765 { "mvws_12", send_to_ws
, {.id
= 11} },
4766 { "mvws_13", send_to_ws
, {.id
= 12} },
4767 { "mvws_14", send_to_ws
, {.id
= 13} },
4768 { "mvws_15", send_to_ws
, {.id
= 14} },
4769 { "mvws_16", send_to_ws
, {.id
= 15} },
4770 { "mvws_17", send_to_ws
, {.id
= 16} },
4771 { "mvws_18", send_to_ws
, {.id
= 17} },
4772 { "mvws_19", send_to_ws
, {.id
= 18} },
4773 { "mvws_20", send_to_ws
, {.id
= 19} },
4774 { "mvws_21", send_to_ws
, {.id
= 20} },
4775 { "mvws_22", send_to_ws
, {.id
= 21} },
4776 { "name_workspace", name_workspace
, {0} },
4777 { "quit", quit
, {0} },
4778 { "raise_toggle", raise_toggle
, {0} },
4779 { "restart", restart
, {0} },
4780 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4781 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4782 { "search_win", search_win
, {0} },
4783 { "search_workspace", search_workspace
, {0} },
4784 { "spawn_custom", NULL
, {0} },
4785 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4786 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4787 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4788 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4789 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4790 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4791 { "uniconify", uniconify
, {0} },
4792 { "version", version
, {0} },
4793 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4794 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4795 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4796 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4797 { "ws_1", switchws
, {.id
= 0} },
4798 { "ws_2", switchws
, {.id
= 1} },
4799 { "ws_3", switchws
, {.id
= 2} },
4800 { "ws_4", switchws
, {.id
= 3} },
4801 { "ws_5", switchws
, {.id
= 4} },
4802 { "ws_6", switchws
, {.id
= 5} },
4803 { "ws_7", switchws
, {.id
= 6} },
4804 { "ws_8", switchws
, {.id
= 7} },
4805 { "ws_9", switchws
, {.id
= 8} },
4806 { "ws_10", switchws
, {.id
= 9} },
4807 { "ws_11", switchws
, {.id
= 10} },
4808 { "ws_12", switchws
, {.id
= 11} },
4809 { "ws_13", switchws
, {.id
= 12} },
4810 { "ws_14", switchws
, {.id
= 13} },
4811 { "ws_15", switchws
, {.id
= 14} },
4812 { "ws_16", switchws
, {.id
= 15} },
4813 { "ws_17", switchws
, {.id
= 16} },
4814 { "ws_18", switchws
, {.id
= 17} },
4815 { "ws_19", switchws
, {.id
= 18} },
4816 { "ws_20", switchws
, {.id
= 19} },
4817 { "ws_21", switchws
, {.id
= 20} },
4818 { "ws_22", switchws
, {.id
= 21} },
4819 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4820 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4821 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4822 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4823 { "ws_prior", priorws
, {0} },
4824 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4825 { "invalid key func", NULL
, {0} },
4828 RB_ENTRY(key
) entry
;
4831 enum keyfuncid funcid
;
4834 RB_HEAD(key_tree
, key
);
4837 key_cmp(struct key
*kp1
, struct key
*kp2
)
4839 if (kp1
->keysym
< kp2
->keysym
)
4841 if (kp1
->keysym
> kp2
->keysym
)
4844 if (kp1
->mod
< kp2
->mod
)
4846 if (kp1
->mod
> kp2
->mod
)
4852 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
4853 struct key_tree keys
;
4856 enum { client_click
, root_click
};
4858 unsigned int action
;
4860 unsigned int button
;
4861 void (*func
)(struct ws_win
*, union arg
*);
4864 /* action key mouse button func args */
4865 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4866 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4867 { client_click
, MODKEY
, Button1
, move
, {0} },
4871 update_modkey(unsigned int mod
)
4877 RB_FOREACH(kp
, key_tree
, &keys
)
4878 if (kp
->mod
& ShiftMask
)
4879 kp
->mod
= mod
| ShiftMask
;
4883 for (i
= 0; i
< LENGTH(buttons
); i
++)
4884 if (buttons
[i
].mask
& ShiftMask
)
4885 buttons
[i
].mask
= mod
| ShiftMask
;
4887 buttons
[i
].mask
= mod
;
4892 TAILQ_ENTRY(spawn_prog
) entry
;
4897 TAILQ_HEAD(spawn_list
, spawn_prog
);
4898 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4901 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4904 struct spawn_prog
*prog
= NULL
;
4906 char *ap
, **real_args
;
4908 /* suppress unused warning since var is needed */
4911 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4914 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4915 if (!strcasecmp(spawn_name
, prog
->name
))
4919 warnx("spawn_custom: program %s not found", spawn_name
);
4923 /* make room for expanded args */
4924 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4925 err(1, "spawn_custom: calloc real_args");
4927 /* expand spawn_args into real_args */
4928 for (i
= 0; i
< prog
->argc
; i
++) {
4930 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4931 if (!strcasecmp(ap
, "$bar_border")) {
4933 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4935 err(1, "spawn_custom border color");
4936 } else if (!strcasecmp(ap
, "$bar_color")) {
4938 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4940 err(1, "spawn_custom bar color");
4941 } else if (!strcasecmp(ap
, "$bar_font")) {
4942 if ((real_args
[i
] = strdup(bar_fonts
))
4944 err(1, "spawn_custom bar fonts");
4945 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4947 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4949 err(1, "spawn_custom color font");
4950 } else if (!strcasecmp(ap
, "$color_focus")) {
4952 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4954 err(1, "spawn_custom color focus");
4955 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4957 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4959 err(1, "spawn_custom color unfocus");
4961 /* no match --> copy as is */
4962 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4963 err(1, "spawn_custom strdup(ap)");
4965 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4970 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4971 for (i
= 0; i
< prog
->argc
; i
++)
4972 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4973 DNPRINTF(SWM_D_SPAWN
, "\n");
4975 *ret_args
= real_args
;
4976 return (prog
->argc
);
4980 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4986 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4990 spawn(r
->ws
->idx
, &a
, 1);
4992 for (i
= 0; i
< spawn_argc
; i
++)
4998 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
5004 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5008 if (pipe(select_list_pipe
) == -1)
5009 err(1, "pipe error");
5010 if (pipe(select_resp_pipe
) == -1)
5011 err(1, "pipe error");
5013 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
5014 err(1, "could not disable SIGPIPE");
5015 switch (*pid
= fork()) {
5017 err(1, "cannot fork");
5020 if (dup2(select_list_pipe
[0], 0) == -1)
5022 if (dup2(select_resp_pipe
[1], 1) == -1)
5024 close(select_list_pipe
[1]);
5025 close(select_resp_pipe
[0]);
5026 spawn(r
->ws
->idx
, &a
, 0);
5028 default: /* parent */
5029 close(select_list_pipe
[0]);
5030 close(select_resp_pipe
[1]);
5034 for (i
= 0; i
< spawn_argc
; i
++)
5040 spawn_insert(char *name
, char *args
)
5042 char *arg
, *cp
, *ptr
;
5043 struct spawn_prog
*sp
;
5045 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
5047 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
5048 err(1, "spawn_insert: calloc");
5049 if ((sp
->name
= strdup(name
)) == NULL
)
5050 err(1, "spawn_insert: strdup");
5052 /* convert the arguments to an argument list */
5053 if ((ptr
= cp
= strdup(args
)) == NULL
)
5054 err(1, "spawn_insert: strdup");
5055 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
5056 /* empty field; skip it */
5061 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
5062 sizeof *sp
->argv
)) == NULL
)
5063 err(1, "spawn_insert: realloc");
5064 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
5065 err(1, "spawn_insert: strdup");
5069 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
5070 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
5074 spawn_remove(struct spawn_prog
*sp
)
5078 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
5080 TAILQ_REMOVE(&spawns
, sp
, entry
);
5081 for (i
= 0; i
< sp
->argc
; i
++)
5087 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
5091 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
5093 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
5096 spawn_insert(name
, args
);
5098 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
5102 setspawn(char *name
, char *args
)
5104 struct spawn_prog
*sp
;
5106 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5111 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5112 if (!strcmp(sp
->name
, name
)) {
5116 spawn_replace(sp
, name
, args
);
5117 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5121 if (*args
== '\0') {
5122 warnx("error: setspawn: cannot find program: %s", name
);
5126 spawn_insert(name
, args
);
5127 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5131 setconfspawn(char *selector
, char *value
, int flags
)
5133 /* suppress unused warning since var is needed */
5136 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
5138 setspawn(selector
, value
);
5140 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
5147 setconfspawn("term", "xterm", 0);
5148 setconfspawn("spawn_term", "xterm", 0);
5149 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5150 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5151 setconfspawn("lock", "xlock", 0);
5152 setconfspawn("initscr", "initscreen.sh", 0);
5153 setconfspawn("menu", "dmenu_run"
5156 " -nf $bar_font_color"
5158 " -sf $bar_color", 0);
5159 setconfspawn("search", "dmenu"
5163 " -nf $bar_font_color"
5165 " -sf $bar_color", 0);
5166 setconfspawn("name_workspace", "dmenu"
5170 " -nf $bar_font_color"
5172 " -sf $bar_color", 0);
5176 #define SWM_MODNAME_SIZE 32
5177 #define SWM_KEY_WS "\n+ \t"
5179 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5183 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5184 if (mod
== NULL
|| ks
== NULL
) {
5185 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5188 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5189 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5193 *ks
= XCB_NO_SYMBOL
;
5195 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5196 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5198 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5199 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5201 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5203 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5205 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5207 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5209 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5211 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5212 *mod
|= ControlMask
;
5214 *ks
= XStringToKeysym(name
);
5215 XConvertCase(*ks
, ks
, &uks
);
5216 if (ks
== XCB_NO_SYMBOL
) {
5218 "parsekeys: invalid key %s\n",
5224 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5229 strdupsafe(char *str
)
5234 return (strdup(str
));
5238 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5242 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5243 keyfuncs
[kfid
].name
, spawn_name
);
5245 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5246 err(1, "key_insert: malloc");
5251 kp
->spawn_name
= strdupsafe(spawn_name
);
5252 RB_INSERT(key_tree
, &keys
, kp
);
5254 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5258 key_lookup(unsigned int mod
, KeySym ks
)
5265 return (RB_FIND(key_tree
, &keys
, &kp
));
5269 key_remove(struct key
*kp
)
5271 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5273 RB_REMOVE(key_tree
, &keys
, kp
);
5274 free(kp
->spawn_name
);
5277 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5281 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5284 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5288 key_insert(mod
, ks
, kfid
, spawn_name
);
5290 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5294 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5299 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5300 keyfuncs
[kfid
].name
, spawn_name
);
5302 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5303 if (kfid
== KF_INVALID
)
5306 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5307 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5310 if (kfid
== KF_INVALID
) {
5311 warnx("error: setkeybinding: cannot find mod/key combination");
5312 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5316 key_insert(mod
, ks
, kfid
, spawn_name
);
5317 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5321 setconfbinding(char *selector
, char *value
, int flags
)
5323 enum keyfuncid kfid
;
5326 struct spawn_prog
*sp
;
5328 /* suppress unused warning since var is needed */
5331 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5332 if (selector
== NULL
) {
5333 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5334 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5336 setkeybinding(mod
, ks
, kfid
, NULL
);
5341 /* search by key function name */
5342 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
5343 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5344 SWM_FUNCNAME_LEN
) == 0) {
5345 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5347 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5348 setkeybinding(mod
, ks
, kfid
, NULL
);
5354 /* search by custom spawn name */
5355 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5356 if (strcasecmp(selector
, sp
->name
) == 0) {
5357 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5359 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5360 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
5367 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5374 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
5375 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
5376 setkeybinding(MODKEY
|ShiftMask
, XK_space
, KF_STACK_RESET
, NULL
);
5377 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
5378 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
5379 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
5380 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
5381 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, KF_STACK_INC
, NULL
);
5382 setkeybinding(MODKEY
|ShiftMask
, XK_period
, KF_STACK_DEC
, NULL
);
5383 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
5384 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
5385 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
5386 setkeybinding(MODKEY
|ShiftMask
, XK_j
, KF_SWAP_NEXT
, NULL
);
5387 setkeybinding(MODKEY
|ShiftMask
, XK_k
, KF_SWAP_PREV
, NULL
);
5388 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
5389 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
5390 setkeybinding(MODKEY
|ShiftMask
, XK_q
, KF_QUIT
, NULL
);
5391 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
5392 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
5393 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
5394 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
5395 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
5396 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
5397 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
5398 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
5399 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
5400 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
5401 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
5402 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
5403 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
5404 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
5405 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
5406 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
5407 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
5408 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
5409 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
5410 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
5411 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
5412 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
5413 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
5414 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
5415 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
5416 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
5417 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
5418 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
5419 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
5420 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, KF_SCREEN_NEXT
, NULL
);
5421 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, KF_SCREEN_PREV
, NULL
);
5422 setkeybinding(MODKEY
|ShiftMask
, XK_1
, KF_MVWS_1
, NULL
);
5423 setkeybinding(MODKEY
|ShiftMask
, XK_2
, KF_MVWS_2
, NULL
);
5424 setkeybinding(MODKEY
|ShiftMask
, XK_3
, KF_MVWS_3
, NULL
);
5425 setkeybinding(MODKEY
|ShiftMask
, XK_4
, KF_MVWS_4
, NULL
);
5426 setkeybinding(MODKEY
|ShiftMask
, XK_5
, KF_MVWS_5
, NULL
);
5427 setkeybinding(MODKEY
|ShiftMask
, XK_6
, KF_MVWS_6
, NULL
);
5428 setkeybinding(MODKEY
|ShiftMask
, XK_7
, KF_MVWS_7
, NULL
);
5429 setkeybinding(MODKEY
|ShiftMask
, XK_8
, KF_MVWS_8
, NULL
);
5430 setkeybinding(MODKEY
|ShiftMask
, XK_9
, KF_MVWS_9
, NULL
);
5431 setkeybinding(MODKEY
|ShiftMask
, XK_0
, KF_MVWS_10
, NULL
);
5432 setkeybinding(MODKEY
|ShiftMask
, XK_F1
, KF_MVWS_11
, NULL
);
5433 setkeybinding(MODKEY
|ShiftMask
, XK_F2
, KF_MVWS_12
, NULL
);
5434 setkeybinding(MODKEY
|ShiftMask
, XK_F3
, KF_MVWS_13
, NULL
);
5435 setkeybinding(MODKEY
|ShiftMask
, XK_F4
, KF_MVWS_14
, NULL
);
5436 setkeybinding(MODKEY
|ShiftMask
, XK_F5
, KF_MVWS_15
, NULL
);
5437 setkeybinding(MODKEY
|ShiftMask
, XK_F6
, KF_MVWS_16
, NULL
);
5438 setkeybinding(MODKEY
|ShiftMask
, XK_F7
, KF_MVWS_17
, NULL
);
5439 setkeybinding(MODKEY
|ShiftMask
, XK_F8
, KF_MVWS_18
, NULL
);
5440 setkeybinding(MODKEY
|ShiftMask
, XK_F9
, KF_MVWS_19
, NULL
);
5441 setkeybinding(MODKEY
|ShiftMask
, XK_F10
, KF_MVWS_20
, NULL
);
5442 setkeybinding(MODKEY
|ShiftMask
, XK_F11
, KF_MVWS_21
, NULL
);
5443 setkeybinding(MODKEY
|ShiftMask
, XK_F12
, KF_MVWS_22
, NULL
);
5444 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
5445 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
5446 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
5447 setkeybinding(MODKEY
|ShiftMask
, XK_x
, KF_WIND_KILL
, NULL
);
5448 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
5449 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
5450 setkeybinding(MODKEY
|ShiftMask
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
5451 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
5452 setkeybinding(MODKEY
|ShiftMask
, XK_v
, KF_VERSION
, NULL
);
5453 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
5454 setkeybinding(MODKEY
|ShiftMask
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
5455 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
5456 setkeybinding(MODKEY
|ShiftMask
, XK_w
, KF_UNICONIFY
, NULL
);
5457 setkeybinding(MODKEY
|ShiftMask
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
5458 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
5459 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
5460 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
5461 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
5462 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
5463 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
5464 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
5465 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
5466 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
5467 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
5468 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
5469 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
5471 setkeybinding(MODKEY
|ShiftMask
, XK_d
, KF_DUMPWINS
, NULL
);
5480 while (RB_EMPTY(&keys
) == 0) {
5481 kp
= RB_ROOT(&keys
);
5487 setkeymapping(char *selector
, char *value
, int flags
)
5489 char keymapping_file
[PATH_MAX
];
5491 /* suppress unused warnings since vars are needed */
5495 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5496 if (value
[0] == '~')
5497 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5498 pwd
->pw_dir
, &value
[1]);
5500 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5502 /* load new key bindings; if it fails, revert to default bindings */
5503 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5507 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5512 updatenumlockmask(void)
5515 xcb_get_modifier_mapping_reply_t
*modmap_r
;
5516 xcb_keycode_t
*modmap
, kc
, *keycode
;
5518 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
5521 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
5522 xcb_get_modifier_mapping(conn
),
5525 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
5526 for (i
= 0; i
< 8; i
++) {
5527 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
5528 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
5530 keycode
= xcb_key_symbols_get_keycode(syms
,
5533 numlockmask
= (1 << i
);
5546 xcb_keycode_t
*code
;
5547 unsigned int modifiers
[] =
5548 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
5551 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5552 updatenumlockmask();
5554 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
5555 for (k
= 0; k
< num_screens
; k
++) {
5556 if (TAILQ_EMPTY(&screens
[k
].rl
))
5558 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
5560 RB_FOREACH(kp
, key_tree
, &keys
) {
5561 if ((code
= xcb_key_symbols_get_keycode(syms
,
5563 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5564 xcb_grab_key(conn
, 1,
5566 kp
->mod
| modifiers
[j
],
5567 *code
, XCB_GRAB_MODE_ASYNC
,
5568 XCB_GRAB_MODE_ASYNC
);
5575 grabbuttons(struct ws_win
*win
, int focused
)
5578 unsigned int modifiers
[] =
5579 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5581 updatenumlockmask();
5582 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, win
->id
,
5583 XCB_BUTTON_MASK_ANY
);
5585 for (i
= 0; i
< LENGTH(buttons
); i
++)
5586 if (buttons
[i
].action
== client_click
)
5587 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5588 xcb_grab_button(conn
, 0, win
->id
,
5590 XCB_GRAB_MODE_ASYNC
,
5597 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
5598 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_SYNC
, XCB_WINDOW_NONE
,
5599 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_ANY
, XCB_BUTTON_MASK_ANY
);
5602 const char *quirkname
[] = {
5603 "NONE", /* config string for "no value" */
5612 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5613 #define SWM_Q_WS "\n|+ \t"
5615 parsequirks(char *qstr
, unsigned long *quirk
)
5625 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5627 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5628 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5629 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5630 DNPRINTF(SWM_D_QUIRK
,
5631 "parsequirks: %s\n", name
);
5636 *quirk
|= 1 << (i
-1);
5640 if (i
>= LENGTH(quirkname
)) {
5641 DNPRINTF(SWM_D_QUIRK
,
5642 "parsequirks: invalid quirk [%s]\n", name
);
5650 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5654 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5657 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5658 err(1, "quirk_insert: malloc");
5659 if ((qp
->class = strdup(class)) == NULL
)
5660 err(1, "quirk_insert: strdup");
5661 if ((qp
->name
= strdup(name
)) == NULL
)
5662 err(1, "quirk_insert: strdup");
5665 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5667 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5671 quirk_remove(struct quirk
*qp
)
5673 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5674 qp
->name
, qp
->quirk
);
5676 TAILQ_REMOVE(&quirks
, qp
, entry
);
5681 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5685 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5686 unsigned long quirk
)
5688 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5689 qp
->name
, qp
->quirk
);
5692 quirk_insert(class, name
, quirk
);
5694 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5698 setquirk(const char *class, const char *name
, unsigned long quirk
)
5702 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5705 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5706 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5710 quirk_replace(qp
, class, name
, quirk
);
5711 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5716 warnx("error: setquirk: cannot find class/name combination");
5720 quirk_insert(class, name
, quirk
);
5721 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5725 setconfquirk(char *selector
, char *value
, int flags
)
5727 char *cp
, *class, *name
;
5729 unsigned long quirks
;
5731 /* suppress unused warning since var is needed */
5734 if (selector
== NULL
)
5736 if ((cp
= strchr(selector
, ':')) == NULL
)
5741 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5742 setquirk(class, name
, quirks
);
5749 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5750 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5751 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5752 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5753 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5754 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5755 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5756 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5757 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5758 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5759 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5760 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5761 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5764 /* conf file stuff */
5765 #define SWM_CONF_FILE "spectrwm.conf"
5766 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5770 SWM_S_BAR_AT_BOTTOM
,
5771 SWM_S_BAR_BORDER_WIDTH
,
5778 SWM_S_CLOCK_ENABLED
,
5781 SWM_S_CYCLE_VISIBLE
,
5783 SWM_S_DISABLE_BORDER
,
5785 SWM_S_FOCUS_CLOSE_WRAP
,
5786 SWM_S_FOCUS_DEFAULT
,
5792 SWM_S_STACK_ENABLED
,
5794 SWM_S_TITLE_CLASS_ENABLED
,
5795 SWM_S_TITLE_NAME_ENABLED
,
5796 SWM_S_URGENT_ENABLED
,
5797 SWM_S_VERBOSE_LAYOUT
,
5798 SWM_S_WINDOW_NAME_ENABLED
,
5799 SWM_S_WORKSPACE_LIMIT
5803 setconfvalue(char *selector
, char *value
, int flags
)
5808 /* suppress unused warning since var is needed */
5812 case SWM_S_BAR_ACTION
:
5814 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5815 err(1, "setconfvalue: bar_action");
5817 case SWM_S_BAR_AT_BOTTOM
:
5818 bar_at_bottom
= atoi(value
);
5820 case SWM_S_BAR_BORDER_WIDTH
:
5821 bar_border_width
= atoi(value
);
5822 if (bar_border_width
< 0)
5823 bar_border_width
= 0;
5825 case SWM_S_BAR_DELAY
:
5826 bar_delay
= atoi(value
);
5828 case SWM_S_BAR_ENABLED
:
5829 bar_enabled
= atoi(value
);
5831 case SWM_S_BAR_FONT
:
5833 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5834 err(1, "setconfvalue: asprintf: failed to allocate "
5835 "memory for bar_fonts.");
5838 case SWM_S_BAR_FORMAT
:
5840 if ((bar_format
= strdup(value
)) == NULL
)
5841 err(1, "setconfvalue: bar_format");
5843 case SWM_S_BAR_JUSTIFY
:
5844 if (!strcmp(value
, "left"))
5845 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5846 else if (!strcmp(value
, "center"))
5847 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5848 else if (!strcmp(value
, "right"))
5849 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5851 errx(1, "invalid bar_justify");
5853 case SWM_S_BORDER_WIDTH
:
5854 border_width
= atoi(value
);
5855 if (border_width
< 0)
5858 case SWM_S_CLOCK_ENABLED
:
5859 clock_enabled
= atoi(value
);
5861 case SWM_S_CLOCK_FORMAT
:
5862 #ifndef SWM_DENY_CLOCK_FORMAT
5864 if ((clock_format
= strdup(value
)) == NULL
)
5865 err(1, "setconfvalue: clock_format");
5868 case SWM_S_CYCLE_EMPTY
:
5869 cycle_empty
= atoi(value
);
5871 case SWM_S_CYCLE_VISIBLE
:
5872 cycle_visible
= atoi(value
);
5874 case SWM_S_DIALOG_RATIO
:
5875 dialog_ratio
= atof(value
);
5876 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5879 case SWM_S_DISABLE_BORDER
:
5880 disable_border
= atoi(value
);
5882 case SWM_S_FOCUS_CLOSE
:
5883 if (!strcmp(value
, "first"))
5884 focus_close
= SWM_STACK_BOTTOM
;
5885 else if (!strcmp(value
, "last"))
5886 focus_close
= SWM_STACK_TOP
;
5887 else if (!strcmp(value
, "next"))
5888 focus_close
= SWM_STACK_ABOVE
;
5889 else if (!strcmp(value
, "previous"))
5890 focus_close
= SWM_STACK_BELOW
;
5892 errx(1, "focus_close");
5894 case SWM_S_FOCUS_CLOSE_WRAP
:
5895 focus_close_wrap
= atoi(value
);
5897 case SWM_S_FOCUS_DEFAULT
:
5898 if (!strcmp(value
, "last"))
5899 focus_default
= SWM_STACK_TOP
;
5900 else if (!strcmp(value
, "first"))
5901 focus_default
= SWM_STACK_BOTTOM
;
5903 errx(1, "focus_default");
5905 case SWM_S_FOCUS_MODE
:
5906 if (!strcmp(value
, "default"))
5907 focus_mode
= SWM_FOCUS_DEFAULT
;
5908 else if (!strcmp(value
, "follow_cursor"))
5909 focus_mode
= SWM_FOCUS_FOLLOW
;
5910 else if (!strcmp(value
, "synergy"))
5911 focus_mode
= SWM_FOCUS_SYNERGY
;
5913 errx(1, "focus_mode");
5915 case SWM_S_SPAWN_ORDER
:
5916 if (!strcmp(value
, "first"))
5917 spawn_position
= SWM_STACK_BOTTOM
;
5918 else if (!strcmp(value
, "last"))
5919 spawn_position
= SWM_STACK_TOP
;
5920 else if (!strcmp(value
, "next"))
5921 spawn_position
= SWM_STACK_ABOVE
;
5922 else if (!strcmp(value
, "previous"))
5923 spawn_position
= SWM_STACK_BELOW
;
5925 errx(1, "spawn_position");
5927 case SWM_S_SPAWN_TERM
:
5928 setconfspawn("term", value
, 0);
5929 setconfspawn("spawn_term", value
, 0);
5933 case SWM_S_SS_ENABLED
:
5934 ss_enabled
= atoi(value
);
5936 case SWM_S_STACK_ENABLED
:
5937 stack_enabled
= atoi(value
);
5939 case SWM_S_TERM_WIDTH
:
5940 term_width
= atoi(value
);
5944 case SWM_S_TITLE_CLASS_ENABLED
:
5945 title_class_enabled
= atoi(value
);
5947 case SWM_S_TITLE_NAME_ENABLED
:
5948 title_name_enabled
= atoi(value
);
5950 case SWM_S_URGENT_ENABLED
:
5951 urgent_enabled
= atoi(value
);
5953 case SWM_S_VERBOSE_LAYOUT
:
5954 verbose_layout
= atoi(value
);
5955 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5957 layouts
[i
].l_string
= fancy_stacker
;
5959 layouts
[i
].l_string
= plain_stacker
;
5962 case SWM_S_WINDOW_NAME_ENABLED
:
5963 window_name_enabled
= atoi(value
);
5965 case SWM_S_WORKSPACE_LIMIT
:
5966 workspace_limit
= atoi(value
);
5967 if (workspace_limit
> SWM_WS_MAX
)
5968 workspace_limit
= SWM_WS_MAX
;
5969 else if (workspace_limit
< 1)
5970 workspace_limit
= 1;
5979 setconfmodkey(char *selector
, char *value
, int flags
)
5981 /* suppress unused warnings since vars are needed */
5985 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5986 update_modkey(Mod1Mask
);
5987 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5988 update_modkey(Mod2Mask
);
5989 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5990 update_modkey(Mod3Mask
);
5991 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5992 update_modkey(Mod4Mask
);
5999 setconfcolor(char *selector
, char *value
, int flags
)
6001 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
6006 setconfregion(char *selector
, char *value
, int flags
)
6008 /* suppress unused warnings since vars are needed */
6012 custom_region(value
);
6017 setautorun(char *selector
, char *value
, int flags
)
6027 /* suppress unused warnings since vars are needed */
6031 if (getenv("SWM_STARTED"))
6035 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
6036 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6038 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6039 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
6042 * This is a little intricate
6044 * If the pid already exists we simply reuse it because it means it was
6045 * used before AND not claimed by manage_window. We get away with
6046 * altering it in the parent after INSERT because this can not be a race
6049 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
6052 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
6054 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
6055 err(1, "setautorun: realloc");
6056 a
.argv
[argc
- 1] = ap
;
6059 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
6060 err(1, "setautorun: realloc");
6061 a
.argv
[argc
] = NULL
;
6063 if ((pid
= fork()) == 0) {
6064 spawn(ws_id
, &a
, 1);
6073 p
= calloc(1, sizeof *p
);
6076 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
6086 setlayout(char *selector
, char *value
, int flags
)
6088 int ws_id
, i
, x
, mg
, ma
, si
, raise
, f
= 0;
6089 int st
= SWM_V_STACK
, num_screens
;
6091 struct workspace
*ws
;
6093 /* suppress unused warnings since vars are needed */
6097 if (getenv("SWM_STARTED"))
6101 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
6102 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
6103 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6104 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6107 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6108 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
6110 if (!strcasecmp(s
, "vertical"))
6112 else if (!strcasecmp(s
, "vertical_flip")) {
6115 } else if (!strcasecmp(s
, "horizontal"))
6117 else if (!strcasecmp(s
, "horizontal_flip")) {
6120 } else if (!strcasecmp(s
, "fullscreen"))
6123 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6124 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6127 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6128 for (i
= 0; i
< num_screens
; i
++) {
6129 ws
= (struct workspace
*)&screens
[i
].ws
;
6130 ws
[ws_id
].cur_layout
= &layouts
[st
];
6132 ws
[ws_id
].always_raise
= raise
;
6133 if (st
== SWM_MAX_STACK
)
6137 for (x
= 0; x
< abs(mg
); x
++) {
6138 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6139 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
6140 SWM_ARG_ID_MASTERSHRINK
);
6144 for (x
= 0; x
< abs(ma
); x
++) {
6145 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6146 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
6147 SWM_ARG_ID_MASTERDEL
);
6151 for (x
= 0; x
< abs(si
); x
++) {
6152 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6153 si
>= 0 ? SWM_ARG_ID_STACKINC
:
6154 SWM_ARG_ID_STACKDEC
);
6159 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6160 SWM_ARG_ID_FLIPLAYOUT
);
6168 /* config options */
6169 struct config_option
{
6171 int (*func
)(char*, char*, int);
6174 struct config_option configopt
[] = {
6175 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
6176 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
6177 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
6178 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
6179 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
6180 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
6181 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
6182 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
6183 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
6184 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
6185 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
6186 { "keyboard_mapping", setkeymapping
, 0 },
6187 { "bind", setconfbinding
, 0 },
6188 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
6189 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
6190 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
6191 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
6192 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
6193 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
6194 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
6195 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
6196 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
6197 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
6198 { "modkey", setconfmodkey
, 0 },
6199 { "program", setconfspawn
, 0 },
6200 { "quirk", setconfquirk
, 0 },
6201 { "region", setconfregion
, 0 },
6202 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
6203 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
6204 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
6205 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
6206 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
6207 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
6208 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6209 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6210 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6211 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6212 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6213 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6214 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6215 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6216 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6217 { "autorun", setautorun
, 0 },
6218 { "layout", setlayout
, 0 },
6223 conf_load(char *filename
, int keymapping
)
6226 char *line
, *cp
, *optsub
, *optval
;
6227 size_t linelen
, lineno
= 0;
6228 int wordlen
, i
, optind
;
6229 struct config_option
*opt
;
6231 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6233 if (filename
== NULL
) {
6234 warnx("conf_load: no filename");
6237 if ((config
= fopen(filename
, "r")) == NULL
) {
6238 warn("conf_load: fopen: %s", filename
);
6242 while (!feof(config
)) {
6243 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6246 err(1, "%s", filename
);
6251 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6252 if (cp
[0] == '\0') {
6257 /* get config option */
6258 wordlen
= strcspn(cp
, "=[ \t\n");
6260 warnx("%s: line %zd: no option found",
6265 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6266 opt
= &configopt
[i
];
6267 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6268 (int)strlen(opt
->optname
) == wordlen
) {
6274 warnx("%s: line %zd: unknown option %.*s",
6275 filename
, lineno
, wordlen
, cp
);
6278 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
6279 warnx("%s: line %zd: invalid option %.*s",
6280 filename
, lineno
, wordlen
, cp
);
6284 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6285 /* get [selector] if any */
6289 wordlen
= strcspn(cp
, "]");
6292 warnx("%s: line %zd: syntax error",
6297 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6299 warnx("%s: line %zd: unable to allocate"
6300 "memory for selector", filename
,
6306 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6308 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6310 optval
= strdup(cp
);
6311 /* call function to deal with it all */
6312 if (configopt
[optind
].func(optsub
, optval
,
6313 configopt
[optind
].funcflags
) != 0)
6314 errx(1, "%s: line %zd: invalid data for %s",
6315 filename
, lineno
, configopt
[optind
].optname
);
6322 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6329 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6335 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
6337 struct ws_win
*parent
, *w
;
6338 struct swm_region
*r
;
6339 struct workspace
*ws
;
6340 xcb_icccm_wm_hints_t wmh
;
6342 parent
= find_window(win
->transient
);
6344 parent
->child_trans
= win
;
6346 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6347 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
6349 r
= root_to_region(win
->wa
->root
);
6351 /* parent doen't exist in our window list */
6352 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6353 if (xcb_icccm_get_wm_hints_reply(conn
,
6354 xcb_icccm_get_wm_hints(conn
, w
->id
),
6356 warnx("can't get hints for 0x%x", w
->id
);
6360 if (win
->hints
.window_group
!= wmh
.window_group
)
6363 w
->child_trans
= win
;
6364 win
->transient
= w
->id
;
6366 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
6367 "transient to 0x%x\n", win
->transient
);
6374 window_get_pid(xcb_window_t win
)
6379 xcb_get_property_cookie_t pc
;
6380 xcb_get_property_reply_t
*pr
;
6382 apid
= get_atom_from_string("_NET_WM_PID");
6383 if (apid
== XCB_ATOM_NONE
)
6386 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
6387 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6390 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
6395 if (pr
->type
== apid
&& pr
->format
== 32)
6396 ret
= *((pid_t
*)xcb_get_property_value(pr
));
6402 apid
= get_atom_from_string("_SWM_PID");
6403 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
6405 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6408 if (pr
->type
!= apid
) {
6413 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
6420 get_ws_idx(xcb_window_t id
)
6426 xcb_get_property_reply_t
*gpr
;
6428 gpr
= xcb_get_property_reply(conn
,
6429 xcb_get_property(conn
, 0, id
, a_swm_ws
,
6430 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
6435 proplen
= xcb_get_property_value_length(gpr
);
6437 prop
= malloc(proplen
+ 1);
6440 xcb_get_property_value(gpr
),
6442 prop
[proplen
] = '\0';
6449 DNPRINTF(SWM_D_PROP
, "get_ws_idx: _SWM_WS: %s\n", prop
);
6450 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
6452 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
;
6474 xcb_icccm_get_wm_class_reply_t tmpch
;
6476 if ((win
= find_window(id
)) != NULL
)
6477 return (win
); /* already being managed */
6479 /* see if we are on the unmanaged list */
6480 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6481 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
6482 "window: 0x%x\n", win
->id
);
6483 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6485 set_child_transient(win
, &trans
);
6487 if (trans
&& (ww
= find_window(trans
)))
6488 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
6489 else if ((ww
= win
->ws
->focus
) &&
6490 spawn_position
== SWM_STACK_ABOVE
)
6491 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
,
6493 else if (ww
&& spawn_position
== SWM_STACK_BELOW
)
6494 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6495 else switch (spawn_position
) {
6498 case SWM_STACK_ABOVE
:
6499 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6501 case SWM_STACK_BOTTOM
:
6502 case SWM_STACK_BELOW
:
6503 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6506 ewmh_update_actions(win
);
6510 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6511 err(1, "manage_window: calloc: failed to allocate memory for "
6517 /* see if we need to override the workspace */
6518 p
= find_pid(window_get_pid(id
));
6520 /* Get all the window data in one shot */
6521 ws_idx
= get_ws_idx(id
);
6523 win
->wa
= xcb_get_geometry_reply(conn
,
6524 xcb_get_geometry(conn
, id
),
6526 xcb_icccm_get_wm_normal_hints_reply(conn
,
6527 xcb_icccm_get_wm_normal_hints(conn
, id
),
6529 xcb_icccm_get_wm_hints_reply(conn
,
6530 xcb_icccm_get_wm_hints(conn
, id
),
6532 xcb_icccm_get_wm_transient_for_reply(conn
,
6533 xcb_icccm_get_wm_transient_for(conn
, id
),
6536 win
->transient
= trans
;
6537 set_child_transient(win
, &trans
);
6538 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%x, "
6539 "transient: 0x%x\n", win
->id
, win
->transient
);
6542 /* get supported protocols */
6543 if (xcb_icccm_get_wm_protocols_reply(conn
,
6544 xcb_icccm_get_wm_protocols(conn
, id
, a_prot
),
6546 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
6547 if (wpr
.atoms
[i
] == a_takefocus
)
6548 win
->take_focus
= 1;
6549 if (wpr
.atoms
[i
] == a_delete
)
6550 win
->can_delete
= 1;
6552 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
6555 win
->iconic
= get_iconic(win
);
6558 * Figure out where to put the window. If it was previously assigned to
6559 * a workspace (either by spawn() or manually moving), and isn't
6560 * transient, * put it in the same workspace
6562 r
= root_to_region(win
->wa
->root
);
6564 ws
= &r
->s
->ws
[p
->ws
];
6565 TAILQ_REMOVE(&pidlist
, p
, entry
);
6568 } else if (ws_idx
!= -1 && win
->transient
== 0) {
6569 ws
= &r
->s
->ws
[ws_idx
];
6572 /* this should launch transients in the same ws as parent */
6574 if ((ww
= find_window(trans
)) != NULL
)
6580 warnx("manage_window: fix this "
6586 /* set up the window layout */
6589 win
->s
= r
->s
; /* this never changes */
6590 if (trans
&& (ww
= find_window(trans
)))
6591 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
6592 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
6593 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
6595 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
6596 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6597 else switch (spawn_position
) {
6600 case SWM_STACK_ABOVE
:
6601 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6603 case SWM_STACK_BOTTOM
:
6604 case SWM_STACK_BELOW
:
6605 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6608 /* ignore window border if there is one. */
6609 WIDTH(win
) = win
->wa
->width
;
6610 HEIGHT(win
) = win
->wa
->height
;
6611 X(win
) = win
->wa
->x
+ win
->wa
->border_width
;
6612 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
;
6614 win
->g_floatvalid
= 0;
6615 win
->floatmaxed
= 0;
6616 win
->ewmh_flags
= 0;
6618 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%x, (x,y) w x h: "
6619 "(%d,%d) %d x %d, ws: %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
6620 HEIGHT(win
), ws
->idx
);
6622 constrain_window(win
, r
, 0);
6624 /* Set window properties so we can remember this after reincarnation */
6625 if (prop
== NULL
&& snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
6627 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6629 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
6630 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
6636 ewmh_autoquirk(win
);
6638 if (xcb_icccm_get_wm_class_reply(conn
,
6639 xcb_icccm_get_wm_class(conn
, win
->id
),
6641 win
->ch
.class_name
= tmpch
.class_name
;
6642 win
->ch
.instance_name
= tmpch
.instance_name
;
6644 xcb_get_wm_class_reply_wipe(&tmpch
);
6646 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6647 win
->ch
.class_name
, win
->ch
.instance_name
);
6649 /* java is retarded so treat it special */
6650 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
6655 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6656 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
6657 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
6658 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
6659 "class: %s, name: %s\n", win
->ch
.class_name
,
6660 win
->ch
.instance_name
);
6661 if (qp
->quirk
& SWM_Q_FLOAT
) {
6665 win
->quirks
= qp
->quirk
;
6670 /* alter window position if quirky */
6671 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6672 win
->manual
= 1; /* don't center the quirky windows */
6673 if (bar_enabled
&& Y(win
) < bar_height
)
6674 Y(win
) = bar_height
;
6675 if (WIDTH(win
) + X(win
) > WIDTH(r
))
6676 X(win
) = WIDTH(r
) - WIDTH(win
) - 2;
6680 /* Reset font sizes (the bruteforce way; no default keybinding). */
6681 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6682 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6683 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
6684 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6685 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
6688 ewmh_get_win_state(win
);
6689 ewmh_update_actions(win
);
6690 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
6695 X(win
) -= border_width
;
6696 Y(win
) -= border_width
;
6700 event_mask
= XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
|
6701 XCB_EVENT_MASK_PROPERTY_CHANGE
| XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
6703 xcb_change_window_attributes(conn
, id
, XCB_CW_EVENT_MASK
, &event_mask
);
6705 /* floaters need to be mapped if they are in the current workspace */
6706 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
6707 map_window_raised(win
->id
);
6713 free_window(struct ws_win
*win
)
6715 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
6720 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6728 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6731 DNPRINTF(SWM_D_MISC
, "free_window: done\n");
6735 unmanage_window(struct ws_win
*win
)
6737 struct ws_win
*parent
;
6738 xcb_screen_t
*screen
;
6743 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
6745 if (win
->transient
) {
6746 parent
= find_window(win
->transient
);
6748 parent
->child_trans
= NULL
;
6751 /* focus on root just in case */
6752 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
6753 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
6754 screen
->root
, XCB_CURRENT_TIME
);
6758 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
6759 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6763 focus_magic(struct ws_win
*win
)
6765 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%x\n", WINID(win
));
6768 /* if there are no windows clear the status-bar */
6773 if (win
->child_trans
) {
6774 /* win = parent & has a transient so focus on that */
6776 focus_win(win
->child_trans
);
6777 if (win
->child_trans
->take_focus
)
6778 client_msg(win
, a_takefocus
);
6780 /* make sure transient hasn't disappeared */
6781 if (validate_win(win
->child_trans
) == 0) {
6782 focus_win(win
->child_trans
);
6783 if (win
->child_trans
->take_focus
)
6784 client_msg(win
->child_trans
,
6787 win
->child_trans
= NULL
;
6789 if (win
->take_focus
)
6790 client_msg(win
, a_takefocus
);
6796 if (win
->take_focus
)
6797 client_msg(win
, a_takefocus
);
6802 expose(xcb_expose_event_t
*e
)
6805 struct swm_region
*r
;
6807 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
6809 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6810 for (i
= 0; i
< num_screens
; i
++)
6811 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6812 if (e
->window
== WINID(r
->bar
))
6819 keypress(xcb_key_press_event_t
*e
)
6821 xcb_keysym_t keysym
;
6824 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
6826 DNPRINTF(SWM_D_EVENT
, "keypress: %u %u\n", e
->detail
, keysym
);
6828 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
6831 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
6832 spawn_custom(root_to_region(e
->root
),
6833 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
6834 else if (keyfuncs
[kp
->funcid
].func
)
6835 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
),
6836 &(keyfuncs
[kp
->funcid
].args
));
6840 buttonpress(xcb_button_press_event_t
*e
)
6844 unsigned int action
;
6846 DNPRINTF(SWM_D_EVENT
, "buttonpress: window 0x%x, detail: %u\n",
6847 e
->event
, e
->detail
);
6849 if ((win
= find_window(e
->event
)) == NULL
)
6853 action
= client_click
;
6855 for (i
= 0; i
< LENGTH(buttons
); i
++)
6856 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6857 buttons
[i
].button
== e
->detail
&&
6858 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
))
6859 buttons
[i
].func(win
, &buttons
[i
].args
);
6866 print_win_geom(xcb_window_t w
)
6868 xcb_get_geometry_reply_t
*wa
;
6870 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
6873 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: 0x%x\n",
6878 DNPRINTF(SWM_D_MISC
, "print_win_geom: window: 0x%x, root: 0x%x, "
6879 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
6880 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
6888 configurerequest(xcb_configure_request_event_t
*e
)
6893 uint32_t wc
[7] = {0};
6895 if ((win
= find_window(e
->window
)) == NULL
)
6896 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
6899 print_win_geom(e
->window
);
6902 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: 0x%x, "
6903 "value_mask: 0x%x", e
->window
, e
->value_mask
);
6904 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
6905 mask
|= XCB_CONFIG_WINDOW_X
;
6907 DPRINTF(", X: %d", e
->x
);
6909 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
6910 mask
|= XCB_CONFIG_WINDOW_Y
;
6912 DPRINTF(", Y: %d", e
->y
);
6914 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
6915 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
6917 DPRINTF(", W: %u", e
->width
);
6919 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
6920 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
6921 wc
[i
++] = e
->height
;
6922 DPRINTF(", H: %u", e
->height
);
6924 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
6925 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6926 wc
[i
++] = e
->border_width
;
6927 DPRINTF(", Border: %u", e
->border_width
);
6929 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
6930 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
6931 wc
[i
++] = e
->sibling
;
6932 DPRINTF(", Sibling: 0x%x", e
->sibling
);
6934 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
6935 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
6936 wc
[i
++] = e
->stack_mode
;
6937 DPRINTF(", StackMode: %u", e
->stack_mode
);
6941 xcb_configure_window(conn
, e
->window
, mask
, wc
);
6943 DPRINTF(", Sent: %s\n", YESNO((mask
!= 0)));
6944 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
6945 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
6946 win
->g_float
.x
= e
->x
- X(win
->ws
->r
);
6947 win
->g_float
.y
= e
->y
- Y(win
->ws
->r
);
6948 win
->g_float
.w
= e
->width
;
6949 win
->g_float
.h
= e
->height
;
6950 win
->g_floatvalid
= 1;
6952 if (win
->floating
) {
6953 win
->g
= win
->g_float
;
6954 win
->g
.x
+= X(win
->ws
->r
);
6955 win
->g
.y
+= Y(win
->ws
->r
);
6968 configurenotify(xcb_configure_notify_event_t
*e
)
6972 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%x\n",
6975 win
= find_window(e
->window
);
6977 xcb_icccm_get_wm_normal_hints_reply(conn
,
6978 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
6987 destroynotify(xcb_destroy_notify_event_t
*e
)
6991 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
6993 if ((win
= find_window(e
->window
)) == NULL
) {
6994 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7000 /* make sure we focus on something */
7003 unmanage_window(win
);
7009 enternotify(xcb_enter_notify_event_t
*e
)
7012 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%x, mode: %d, detail: "
7013 "%d, root: 0x%x, subwindow: 0x%x, same_screen_focus: %s, "
7014 "state: %d\n", e
->event
, e
->mode
, e
->detail
, e
->root
,
7015 e
->child
, YESNO(e
->same_screen_focus
), e
->state
);
7017 if (e
->mode
!= XCB_NOTIFY_MODE_NORMAL
) {
7018 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
7023 switch (focus_mode
) {
7024 case SWM_FOCUS_DEFAULT
:
7026 case SWM_FOCUS_FOLLOW
:
7028 case SWM_FOCUS_SYNERGY
:
7032 if ((win
= find_window(e
->event
)) == NULL
) {
7033 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
7042 /* lets us use one switch statement for arbitrary mode/detail combinations */
7043 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7046 mapnotify(xcb_map_notify_event_t
*e
)
7050 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
7052 win
= manage_window(e
->window
);
7054 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
7057 * focus_win can only set input focus on a mapped window.
7058 * make sure the window really has focus since it is just being mapped.
7060 if (win
->ws
->focus
== win
)
7065 mappingnotify(xcb_mapping_notify_event_t
*e
)
7067 xcb_refresh_keyboard_mapping(syms
, e
);
7069 if (e
->request
== XCB_MAPPING_KEYBOARD
)
7074 maprequest(xcb_map_request_event_t
*e
)
7077 struct swm_region
*r
;
7078 xcb_get_window_attributes_reply_t
*war
;
7080 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%x\n",
7083 war
= xcb_get_window_attributes_reply(conn
,
7084 xcb_get_window_attributes(conn
, e
->window
),
7088 if (war
->override_redirect
) {
7094 win
= manage_window(e
->window
);
7096 return; /* can't happen */
7101 /* make new win focused */
7102 r
= root_to_region(win
->wa
->root
);
7103 if (win
->ws
== r
->ws
)
7109 get_atom_name(xcb_atom_t atom
)
7113 xcb_get_atom_name_reply_t
*r
;
7115 r
= xcb_get_atom_name_reply(conn
,
7116 xcb_get_atom_name(conn
, atom
),
7119 len
= xcb_get_atom_name_name_length(r
);
7121 name
= malloc(len
+ 1);
7123 memcpy(name
, xcb_get_atom_name_name(r
), len
);
7135 propertynotify(xcb_property_notify_event_t
*e
)
7141 name
= get_atom_name(e
->atom
);
7142 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%x, atom: %s(%u)\n",
7143 e
->window
, name
, e
->atom
);
7147 win
= find_window(e
->window
);
7151 if (e
->state
== XCB_PROPERTY_DELETE
&& e
->atom
== a_swm_iconic
) {
7152 update_iconic(win
, 0);
7153 map_window_raised(win
->id
);
7160 case XCB_ATOM_WM_CLASS
:
7161 case XCB_ATOM_WM_NAME
:
7170 unmapnotify(xcb_unmap_notify_event_t
*e
)
7174 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
7176 /* determine if we need to help unmanage this window */
7177 win
= find_window(e
->window
);
7181 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
) {
7182 unmanage_window(win
);
7185 /* resend unmap because we ated it */
7186 xcb_unmap_window(conn
, e
->window
);
7193 visibilitynotify(xcb_visibility_notify_event_t
*e
)
7195 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%x\n",
7201 clientmessage(xcb_client_message_event_t
*e
)
7204 xcb_map_request_event_t mre
;
7208 name
= get_atom_name(e
->type
);
7209 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7210 e
->window
, name
, e
->type
);
7213 win
= find_window(e
->window
);
7216 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7217 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
7218 "unmanaged window.\n");
7219 mre
.window
= e
->window
;
7225 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7226 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7229 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7230 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7231 if (win
->can_delete
)
7232 client_msg(win
, a_delete
);
7234 xcb_kill_client(conn
, win
->id
);
7236 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7237 DNPRINTF(SWM_D_EVENT
,
7238 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7239 if (win
->floating
) {
7240 if (e
->data
.data32
[0] & (1<<8)) /* x */
7241 X(win
) = e
->data
.data32
[1];
7242 if (e
->data
.data32
[0] & (1<<9)) /* y */
7243 Y(win
) = e
->data
.data32
[2];
7244 if (e
->data
.data32
[0] & (1<<10)) /* width */
7245 WIDTH(win
) = e
->data
.data32
[3];
7246 if (e
->data
.data32
[0] & (1<<11)) /* height */
7247 HEIGHT(win
) = e
->data
.data32
[4];
7252 /* TODO: Change stack sizes */
7253 /* notify no change was made. */
7254 config_win(win
, NULL
);
7257 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
7258 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
7259 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
7260 if (e
->data
.data32
[2])
7261 ewmh_update_win_state(win
, e
->data
.data32
[2],
7274 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
;
7276 xcb_void_cookie_t wac
;
7277 xcb_generic_error_t
*error
;
7279 /* this causes an error if some other window manager is running */
7280 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7281 for (i
= 0; i
< num_screens
; i
++) {
7283 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: 0x%x\n",
7285 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
7286 XCB_CW_EVENT_MASK
, &val
);
7287 if ((error
= xcb_request_check(conn
, wac
))) {
7288 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
7299 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
7301 struct swm_region
*r
, *n
;
7302 struct workspace
*ws
= NULL
;
7305 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
7306 s
->idx
, w
, h
, x
, y
);
7308 /* remove any conflicting regions */
7309 n
= TAILQ_FIRST(&s
->rl
);
7312 n
= TAILQ_NEXT(r
, entry
);
7313 if (X(r
) < (x
+ w
) &&
7314 (X(r
) + WIDTH(r
)) > x
&&
7316 (Y(r
) + HEIGHT(r
)) > y
) {
7317 if (r
->ws
->r
!= NULL
)
7318 r
->ws
->old_r
= r
->ws
->r
;
7321 TAILQ_REMOVE(&s
->rl
, r
, entry
);
7322 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
7326 /* search old regions for one to reuse */
7328 /* size + location match */
7329 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7330 if (X(r
) == x
&& Y(r
) == y
&&
7331 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7335 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7336 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7340 TAILQ_REMOVE(&s
->orl
, r
, entry
);
7341 /* try to use old region's workspace */
7342 if (r
->ws
->r
== NULL
)
7345 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
7346 err(1, "new_region: calloc: failed to allocate memory "
7349 /* if we don't have a workspace already, find one */
7351 for (i
= 0; i
< workspace_limit
; i
++)
7352 if (s
->ws
[i
].r
== NULL
) {
7359 errx(1, "new_region: no free workspaces");
7370 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
7376 #ifdef SWM_XRR_HAS_CRTC
7379 #endif /* SWM_XRR_HAS_CRTC */
7380 struct swm_region
*r
;
7382 xcb_randr_get_screen_resources_current_cookie_t src
;
7383 xcb_randr_get_screen_resources_current_reply_t
*srr
;
7384 xcb_randr_get_crtc_info_cookie_t cic
;
7385 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
7386 xcb_randr_crtc_t
*crtc
;
7387 xcb_screen_t
*screen
= get_screen(i
);
7389 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7390 if (i
>= num_screens
)
7391 errx(1, "scan_xrandr: invalid screen");
7393 /* remove any old regions */
7394 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
7395 r
->ws
->old_r
= r
->ws
->r
= NULL
;
7397 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
7398 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
7402 /* map virtual screens onto physical screens */
7403 #ifdef SWM_XRR_HAS_CRTC
7404 if (xrandr_support
) {
7405 src
= xcb_randr_get_screen_resources_current(conn
,
7407 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
7410 new_region(&screens
[i
], 0, 0,
7411 screen
->width_in_pixels
,
7412 screen
->height_in_pixels
);
7415 ncrtc
= srr
->num_crtcs
;
7417 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
7418 for (c
= 0; c
< ncrtc
; c
++) {
7419 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
7421 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
7424 if (cir
->num_outputs
== 0) {
7430 new_region(&screens
[i
], 0, 0,
7431 screen
->width_in_pixels
,
7432 screen
->height_in_pixels
);
7434 new_region(&screens
[i
],
7435 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
7440 #endif /* SWM_XRR_HAS_CRTC */
7442 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
7443 screen
->height_in_pixels
);
7448 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
7450 struct swm_region
*r
;
7453 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
7455 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7456 /* silly event doesn't include the screen index */
7457 for (i
= 0; i
< num_screens
; i
++)
7458 if (screens
[i
].root
== e
->root
)
7460 if (i
>= num_screens
)
7461 errx(1, "screenchange: screen not found");
7463 /* brute force for now, just re-enumerate the regions */
7467 print_win_geom(e
->root
);
7469 /* add bars to all regions */
7470 for (i
= 0; i
< num_screens
; i
++)
7471 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7479 xcb_window_t
*wins
= NULL
;
7481 int i
, j
, num_screens
;
7482 uint16_t state
, manage
;
7484 xcb_query_tree_cookie_t qtc
;
7485 xcb_query_tree_reply_t
*qtr
;
7486 xcb_get_window_attributes_cookie_t c
;
7487 xcb_get_window_attributes_reply_t
*r
;
7488 xcb_get_property_cookie_t pc
;
7490 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
7491 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7492 for (i
= 0; i
< num_screens
; i
++) {
7493 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
7494 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
7497 wins
= xcb_query_tree_children(qtr
);
7498 no
= xcb_query_tree_children_length(qtr
);
7499 /* attach windows to a region */
7500 /* normal windows */
7501 for (j
= 0; j
< no
; j
++) {
7502 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7503 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7506 if (r
->override_redirect
) {
7511 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7512 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7518 state
= getstate(wins
[j
]);
7519 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7520 if (r
->map_state
== XCB_MAP_STATE_VIEWABLE
|| manage
)
7521 manage_window(wins
[j
]);
7524 /* transient windows */
7525 for (j
= 0; j
< no
; j
++) {
7526 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7527 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7530 if (r
->override_redirect
) {
7536 state
= getstate(wins
[j
]);
7537 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7538 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7539 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7540 &wins
[j
], NULL
) && manage
)
7541 manage_window(wins
[j
]);
7545 DNPRINTF(SWM_D_INIT
, "grab_windows: done\n");
7551 int i
, j
, k
, num_screens
;
7552 struct workspace
*ws
;
7553 uint32_t gcv
[1], wa
[1];
7554 const xcb_query_extension_reply_t
*qep
;
7555 xcb_cursor_t cursor
;
7556 xcb_font_t cursor_font
;
7557 xcb_randr_query_version_cookie_t c
;
7558 xcb_randr_query_version_reply_t
*r
;
7560 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7561 if ((screens
= calloc(num_screens
,
7562 sizeof(struct swm_screen
))) == NULL
)
7563 err(1, "setup_screens: calloc: failed to allocate memory for "
7566 /* initial Xrandr setup */
7568 c
= xcb_randr_query_version(conn
, 1, 1);
7569 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
7571 if (r
->major_version
>= 1)
7575 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
7576 xrandr_eventbase
= qep
->first_event
;
7578 cursor_font
= xcb_generate_id(conn
);
7579 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
7581 cursor
= xcb_generate_id(conn
);
7582 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
7583 XC_left_ptr
, XC_left_ptr
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7586 /* map physical screens */
7587 for (i
= 0; i
< num_screens
; i
++) {
7588 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
7590 TAILQ_INIT(&screens
[i
].rl
);
7591 TAILQ_INIT(&screens
[i
].orl
);
7592 screens
[i
].root
= get_screen(i
)->root
;
7594 /* set default colors */
7595 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
7596 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
7597 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
7598 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
7599 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
7601 /* create graphics context on screen */
7602 screens
[i
].bar_gc
= xcb_generate_id(conn
);
7604 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
7605 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
7607 /* set default cursor */
7608 xcb_change_window_attributes(conn
, screens
[i
].root
,
7611 /* init all workspaces */
7612 /* XXX these should be dynamically allocated too */
7613 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
7614 ws
= &screens
[i
].ws
[j
];
7620 TAILQ_INIT(&ws
->winlist
);
7621 TAILQ_INIT(&ws
->unmanagedlist
);
7623 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
7624 if (layouts
[k
].l_config
!= NULL
)
7625 layouts
[k
].l_config(ws
,
7626 SWM_ARG_ID_STACKINIT
);
7627 ws
->cur_layout
= &layouts
[0];
7628 ws
->cur_layout
->l_string(ws
);
7634 xcb_randr_select_input(conn
, screens
[i
].root
,
7635 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
7637 xcb_free_cursor(conn
, cursor
);
7638 xcb_close_font(conn
, cursor_font
);
7644 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
7645 err(1, "setup_globals: strdup: failed to allocate memory.");
7647 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
7648 err(1, "setup_globals: strdup: failed to allocate memory.");
7650 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
7651 errx(1, "unable to allocate key symbols");
7653 a_state
= get_atom_from_string("WM_STATE");
7654 a_prot
= get_atom_from_string("WM_PROTOCOLS");
7655 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
7656 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
7657 a_wmname
= get_atom_from_string("WM_NAME");
7658 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
7659 a_utf8_string
= get_atom_from_string("UTF8_STRING");
7660 a_string
= get_atom_from_string("STRING");
7661 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
7662 a_swm_ws
= get_atom_from_string("_SWM_WS");
7669 xcb_atom_t netwmcheck
;
7670 xcb_window_t root
, win
;
7673 /* work around sun jdk bugs, code from wmname */
7674 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7676 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7677 for (i
= 0; i
< num_screens
; i
++) {
7678 root
= screens
[i
].root
;
7680 win
= xcb_generate_id(conn
);
7681 wa
[0] = screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].pixel
;
7682 wa
[1] = screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].pixel
;
7683 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, 0, 0, 0, 1,
7684 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
7685 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
7687 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
7688 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7689 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7690 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7691 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7692 a_netwmname
, a_utf8_string
, 8, strlen("LG3D"), "LG3D");
7697 event_error(xcb_generic_error_t
*e
)
7699 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
7700 "resource_id: %u, minor_code: %u\n",
7701 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
7702 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
7703 e
->sequence
, e
->resource_id
, e
->minor_code
);
7707 event_handle(xcb_generic_event_t
*evt
)
7709 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
7711 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d)\n",
7712 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
7713 XCB_EVENT_RESPONSE_TYPE(evt
));
7716 #define EVENT(type, callback) case type: callback((void *)evt); return
7717 EVENT(0, event_error
);
7718 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
7719 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7720 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
7721 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
7722 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
7723 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
7724 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
7725 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
7726 /*EVENT(XCB_CREATE_NOTIFY, );*/
7727 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
7728 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
7729 EVENT(XCB_EXPOSE
, expose
);
7730 /*EVENT(XCB_FOCUS_IN, );*/
7731 /*EVENT(XCB_FOCUS_OUT, );*/
7732 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
7733 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
7734 EVENT(XCB_KEY_PRESS
, keypress
);
7735 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7736 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
7737 /*EVENT(XCB_LEAVE_NOTIFY, );*/
7738 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
7739 EVENT(XCB_MAP_REQUEST
, maprequest
);
7740 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
7741 /*EVENT(XCB_MOTION_NOTIFY, );*/
7742 /*EVENT(XCB_NO_EXPOSURE, );*/
7743 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
7744 /*EVENT(XCB_REPARENT_NOTIFY, );*/
7745 /*EVENT(XCB_RESIZE_REQUEST, );*/
7746 /*EVENT(XCB_SELECTION_CLEAR, );*/
7747 /*EVENT(XCB_SELECTION_NOTIFY, );*/
7748 /*EVENT(XCB_SELECTION_REQUEST, );*/
7749 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
7750 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
7753 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
7754 screenchange((void *)evt
);
7758 main(int argc
, char *argv
[])
7760 struct swm_region
*r
, *rr
;
7761 struct ws_win
*winfocus
= NULL
;
7763 char conf
[PATH_MAX
], *cfile
= NULL
;
7765 int xfd
, i
, num_screens
;
7766 struct sigaction sact
;
7767 xcb_generic_event_t
*evt
;
7771 /* suppress unused warning since var is needed */
7774 time_started
= time(NULL
);
7777 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
7778 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, ""))
7779 warnx("no locale support");
7781 /* handle some signals */
7782 bzero(&sact
, sizeof(sact
));
7783 sigemptyset(&sact
.sa_mask
);
7785 sact
.sa_handler
= sighdlr
;
7786 sigaction(SIGINT
, &sact
, NULL
);
7787 sigaction(SIGQUIT
, &sact
, NULL
);
7788 sigaction(SIGTERM
, &sact
, NULL
);
7789 sigaction(SIGHUP
, &sact
, NULL
);
7791 sact
.sa_handler
= sighdlr
;
7792 sact
.sa_flags
= SA_NOCLDSTOP
;
7793 sigaction(SIGCHLD
, &sact
, NULL
);
7795 if (!(display
= XOpenDisplay(0)))
7796 errx(1, "can not open display");
7798 conn
= XGetXCBConnection(display
);
7799 if (xcb_connection_has_error(conn
))
7800 errx(1, "can not get XCB connection");
7802 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
7804 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
7805 xfd
= xcb_get_file_descriptor(conn
);
7807 /* look for local and global conf file */
7808 pwd
= getpwuid(getuid());
7810 errx(1, "invalid user: %d", getuid());
7812 xcb_grab_server(conn
);
7815 /* flush all events */
7816 while ((evt
= xcb_poll_for_event(conn
))) {
7817 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
7822 if (enable_wm() != 0)
7823 errx(1, "another window manager is currently running");
7834 for (i
= 0; ; i
++) {
7839 snprintf(conf
, sizeof conf
, "%s/.%s",
7840 pwd
->pw_dir
, SWM_CONF_FILE
);
7844 snprintf(conf
, sizeof conf
, "/etc/%s",
7849 snprintf(conf
, sizeof conf
, "%s/.%s",
7850 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
7854 snprintf(conf
, sizeof conf
, "/etc/%s",
7861 if (strlen(conf
) && stat(conf
, &sb
) != -1)
7862 if (S_ISREG(sb
.st_mode
)) {
7869 /* load conf (if any) */
7871 conf_load(cfile
, SWM_CONF_DEFAULT
);
7874 /* set some values to work around bad programs */
7876 /* grab existing windows (before we build the bars) */
7879 if (getenv("SWM_STARTED") == NULL
)
7880 setenv("SWM_STARTED", "YES", 1);
7882 /* setup all bars */
7883 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7884 for (i
= 0; i
< num_screens
; i
++)
7885 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
7886 if (winfocus
== NULL
)
7887 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
7896 xcb_ungrab_server(conn
);
7900 while ((evt
= xcb_poll_for_event(conn
))) {
7907 /* if we are being restarted go focus on first window */
7909 rr
= winfocus
->ws
->r
;
7911 /* not a visible window */
7915 /* move pointer to first screen if multi screen */
7916 if (num_screens
> 1 || outputs
> 1)
7917 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
,
7918 rr
->s
[0].root
, 0, 0, 0, 0, X(rr
),
7919 Y(rr
) + (bar_enabled
? bar_height
: 0));
7921 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7931 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7933 DNPRINTF(SWM_D_MISC
, "select failed");
7934 if (restart_wm
== 1)
7935 restart(NULL
, NULL
);
7936 if (search_resp
== 1)
7949 for (i
= 0; i
< num_screens
; ++i
)
7950 if (screens
[i
].bar_gc
!= 0)
7951 xcb_free_gc(conn
, screens
[i
].bar_gc
);
7953 XftFontClose(display
, bar_font
);
7954 XftColorFree(display
, DefaultVisual(display
, 0),
7955 DefaultColormap(display
, 0), &bar_font_color
);
7956 xcb_key_symbols_free(syms
);
7958 xcb_disconnect(conn
);