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/Xlib-xcb.h>
93 #include <xcb/xcb_atom.h>
94 #include <xcb/xcb_aux.h>
95 #include <xcb/xcb_event.h>
96 #include <xcb/xcb_icccm.h>
97 #include <xcb/xcb_keysyms.h>
98 #include <xcb/xtest.h>
99 #include <xcb/randr.h>
107 #ifdef SPECTRWM_BUILDSTR
108 static const char *buildstr
= SPECTRWM_BUILDSTR
;
110 static const char *buildstr
= SPECTRWM_VERSION
;
114 # error XRandR versions less than 1.0 are not supported
119 #define SWM_XRR_HAS_CRTC
123 #if defined(__OpenBSD__)
124 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
125 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
126 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
127 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
128 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
129 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
130 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
131 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
132 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
133 #define xcb_icccm_get_wm_class xcb_get_wm_class
134 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
135 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
136 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
137 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
138 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
139 #define xcb_icccm_get_wm_name xcb_get_wm_name
140 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
141 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
142 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
143 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
144 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
145 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
146 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
147 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
148 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
149 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
154 #define DPRINTF(x...) do { \
156 fprintf(stderr, x); \
158 #define DNPRINTF(n,x...) do { \
159 if (swm_debug & n) { \
160 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
161 fprintf(stderr, x); \
164 #define SWM_D_MISC 0x0001
165 #define SWM_D_EVENT 0x0002
166 #define SWM_D_WS 0x0004
167 #define SWM_D_FOCUS 0x0008
168 #define SWM_D_MOVE 0x0010
169 #define SWM_D_STACK 0x0020
170 #define SWM_D_MOUSE 0x0040
171 #define SWM_D_PROP 0x0080
172 #define SWM_D_CLASS 0x0100
173 #define SWM_D_KEY 0x0200
174 #define SWM_D_QUIRK 0x0400
175 #define SWM_D_SPAWN 0x0800
176 #define SWM_D_EVENTQ 0x1000
177 #define SWM_D_CONF 0x2000
178 #define SWM_D_BAR 0x4000
179 #define SWM_D_INIT 0x8000
181 u_int32_t swm_debug
= 0
200 #define DPRINTF(x...)
201 #define DNPRINTF(n,x...)
204 #define LENGTH(x) (int)(sizeof x / sizeof x[0])
205 #define MODKEY Mod1Mask
206 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
207 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
208 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
209 #define SWM_PROPLEN (16)
210 #define SWM_FUNCNAME_LEN (32)
211 #define SWM_KEYS_LEN (255)
212 #define SWM_QUIRK_LEN (64)
213 #define X(r) (r)->g.x
214 #define Y(r) (r)->g.y
215 #define WIDTH(r) (r)->g.w
216 #define HEIGHT(r) (r)->g.h
217 #define BORDER(w) (w->bordered ? border_width : 0)
218 #define MAX_X(r) ((r)->g.x + (r)->g.w)
219 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
220 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
221 #define SH_MIN_W(w) (w)->sh.min_width
222 #define SH_MIN_H(w) (w)->sh.min_height
223 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
224 #define SH_MAX_W(w) (w)->sh.max_width
225 #define SH_MAX_H(w) (w)->sh.max_height
226 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
227 #define SH_INC_W(w) (w)->sh.width_inc
228 #define SH_INC_H(w) (w)->sh.height_inc
229 #define SWM_MAX_FONT_STEPS (3)
230 #define WINID(w) ((w) ? (w)->id : 0)
231 #define YESNO(x) ((x) ? "yes" : "no")
233 #define SWM_FOCUS_DEFAULT (0)
234 #define SWM_FOCUS_SYNERGY (1)
235 #define SWM_FOCUS_FOLLOW (2)
237 #define SWM_CONF_DEFAULT (0)
238 #define SWM_CONF_KEYMAPPING (1)
241 #define SWM_LIB "/usr/local/lib/libswmhack.so"
248 xcb_atom_t a_takefocus
;
250 xcb_atom_t a_netwmname
;
251 xcb_atom_t a_utf8_string
;
253 xcb_atom_t a_swm_iconic
;
254 volatile sig_atomic_t running
= 1;
255 volatile sig_atomic_t restart_wm
= 0;
257 /*int last_focus_event = FocusOut;*/
261 int xrandr_eventbase
;
262 unsigned int numlockmask
= 0;
265 xcb_connection_t
*conn
;
266 xcb_key_symbols_t
*syms
;
269 int cycle_visible
= 0;
271 int font_adjusted
= 0;
272 unsigned int mod_key
= MODKEY
;
275 struct swm_region
*search_r
;
276 int select_list_pipe
[2];
277 int select_resp_pipe
[2];
279 volatile sig_atomic_t search_resp
;
280 int search_resp_action
;
282 struct search_window
{
283 TAILQ_ENTRY(search_window
) entry
;
287 xcb_window_t indicator
;
289 TAILQ_HEAD(search_winlist
, search_window
);
290 struct search_winlist search_wl
;
295 SWM_SEARCH_UNICONIFY
,
296 SWM_SEARCH_NAME_WORKSPACE
,
297 SWM_SEARCH_SEARCH_WORKSPACE
,
298 SWM_SEARCH_SEARCH_WINDOW
301 #define SWM_STACK_TOP (0)
302 #define SWM_STACK_BOTTOM (1)
303 #define SWM_STACK_ABOVE (2)
304 #define SWM_STACK_BELOW (3)
307 double dialog_ratio
= 0.6;
309 #define SWM_BAR_MAX (256)
310 #define SWM_BAR_JUSTIFY_LEFT (0)
311 #define SWM_BAR_JUSTIFY_CENTER (1)
312 #define SWM_BAR_JUSTIFY_RIGHT (2)
313 #define SWM_BAR_OFFSET (4)
314 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
315 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
316 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
317 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
318 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
320 char *bar_argv
[] = { NULL
, NULL
};
322 unsigned char bar_ext
[SWM_BAR_MAX
];
323 char bar_vertext
[SWM_BAR_MAX
];
325 sig_atomic_t bar_alarm
= 0;
328 int bar_border_width
= 1;
329 int bar_at_bottom
= 0;
331 int bar_extra_running
= 0;
334 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
335 char *bar_format
= NULL
;
336 int stack_enabled
= 1;
337 int clock_enabled
= 1;
338 int urgent_enabled
= 0;
339 char *clock_format
= NULL
;
340 int title_name_enabled
= 0;
341 int title_class_enabled
= 0;
342 int window_name_enabled
= 0;
343 int focus_mode
= SWM_FOCUS_DEFAULT
;
344 int focus_close
= SWM_STACK_BELOW
;
345 int focus_close_wrap
= 1;
346 int focus_default
= SWM_STACK_TOP
;
347 int spawn_position
= SWM_STACK_TOP
;
348 int disable_border
= 0;
349 int border_width
= 1;
350 int verbose_layout
= 0;
354 int32_t bar_fs_height
;
358 /* layout manager data */
359 struct swm_geometry
{
372 struct swm_geometry g
;
375 /* virtual "screens" */
377 TAILQ_ENTRY(swm_region
) entry
;
378 struct swm_geometry g
;
379 struct workspace
*ws
; /* current workspace on this region */
380 struct workspace
*ws_prior
; /* prior workspace on this region */
381 struct swm_screen
*s
; /* screen idx */
384 TAILQ_HEAD(swm_region_list
, swm_region
);
387 TAILQ_ENTRY(ws_win
) entry
;
389 xcb_window_t transient
;
390 struct ws_win
*child_trans
; /* transient child window */
391 struct swm_geometry g
; /* current geometry */
392 struct swm_geometry g_float
; /* region coordinates */
393 int g_floatvalid
; /* g_float geometry validity */
394 int floatmaxed
; /* whether maxed by max_stack */
399 unsigned int ewmh_flags
;
400 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
406 unsigned long quirks
;
407 struct workspace
*ws
; /* always valid */
408 struct swm_screen
*s
; /* always valid, never changes */
409 xcb_get_geometry_reply_t
*wa
;
411 xcb_icccm_get_wm_class_reply_t ch
;
412 xcb_icccm_wm_hints_t hints
;
414 TAILQ_HEAD(ws_win_list
, ws_win
);
418 TAILQ_ENTRY(pid_e
) entry
;
422 TAILQ_HEAD(pid_list
, pid_e
);
423 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
425 /* layout handlers */
427 void vertical_config(struct workspace
*, int);
428 void vertical_stack(struct workspace
*, struct swm_geometry
*);
429 void horizontal_config(struct workspace
*, int);
430 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
431 void max_stack(struct workspace
*, struct swm_geometry
*);
432 void plain_stacker(struct workspace
*);
433 void fancy_stacker(struct workspace
*);
436 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
437 void (*l_config
)(struct workspace
*, int);
439 #define SWM_L_FOCUSPREV (1<<0)
440 #define SWM_L_MAPONFOCUS (1<<1)
441 void (*l_string
)(struct workspace
*);
443 /* stack, configure */
444 { vertical_stack
, vertical_config
, 0, plain_stacker
},
445 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
447 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
448 { NULL
, NULL
, 0, NULL
},
451 /* position of max_stack mode in the layouts array, index into layouts! */
452 #define SWM_V_STACK (0)
453 #define SWM_H_STACK (1)
454 #define SWM_MAX_STACK (2)
456 #define SWM_H_SLICE (32)
457 #define SWM_V_SLICE (32)
459 /* define work spaces */
461 int idx
; /* workspace index */
462 char *name
; /* workspace name */
463 int always_raise
; /* raise windows on focus */
464 struct layout
*cur_layout
; /* current layout handlers */
465 struct ws_win
*focus
; /* may be NULL */
466 struct ws_win
*focus_prev
; /* may be NULL */
467 struct swm_region
*r
; /* may be NULL */
468 struct swm_region
*old_r
; /* may be NULL */
469 struct ws_win_list winlist
; /* list of windows in ws */
470 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
471 char stacker
[10]; /* display stacker and layout */
475 int horizontal_msize
;
477 int horizontal_stacks
;
488 SWM_S_COLOR_BAR_BORDER
,
489 SWM_S_COLOR_BAR_FONT
,
495 /* physical screen mapping */
496 #define SWM_WS_MAX (22) /* hard limit */
497 int workspace_limit
= 10; /* soft limit */
500 int idx
; /* screen index */
501 struct swm_region_list rl
; /* list of regions on this screen */
502 struct swm_region_list orl
; /* list of old regions */
504 struct workspace ws
[SWM_WS_MAX
];
510 } c
[SWM_S_COLOR_MAX
];
512 xcb_gcontext_t bar_gc
;
514 struct swm_screen
*screens
;
516 /* args to functions */
519 #define SWM_ARG_ID_FOCUSNEXT (0)
520 #define SWM_ARG_ID_FOCUSPREV (1)
521 #define SWM_ARG_ID_FOCUSMAIN (2)
522 #define SWM_ARG_ID_FOCUSCUR (4)
523 #define SWM_ARG_ID_SWAPNEXT (10)
524 #define SWM_ARG_ID_SWAPPREV (11)
525 #define SWM_ARG_ID_SWAPMAIN (12)
526 #define SWM_ARG_ID_MOVELAST (13)
527 #define SWM_ARG_ID_MASTERSHRINK (20)
528 #define SWM_ARG_ID_MASTERGROW (21)
529 #define SWM_ARG_ID_MASTERADD (22)
530 #define SWM_ARG_ID_MASTERDEL (23)
531 #define SWM_ARG_ID_FLIPLAYOUT (24)
532 #define SWM_ARG_ID_STACKRESET (30)
533 #define SWM_ARG_ID_STACKINIT (31)
534 #define SWM_ARG_ID_CYCLEWS_UP (40)
535 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
536 #define SWM_ARG_ID_CYCLESC_UP (42)
537 #define SWM_ARG_ID_CYCLESC_DOWN (43)
538 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
539 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
540 #define SWM_ARG_ID_STACKINC (50)
541 #define SWM_ARG_ID_STACKDEC (51)
542 #define SWM_ARG_ID_SS_ALL (60)
543 #define SWM_ARG_ID_SS_WINDOW (61)
544 #define SWM_ARG_ID_DONTCENTER (70)
545 #define SWM_ARG_ID_CENTER (71)
546 #define SWM_ARG_ID_KILLWINDOW (80)
547 #define SWM_ARG_ID_DELETEWINDOW (81)
548 #define SWM_ARG_ID_WIDTHGROW (90)
549 #define SWM_ARG_ID_WIDTHSHRINK (91)
550 #define SWM_ARG_ID_HEIGHTGROW (92)
551 #define SWM_ARG_ID_HEIGHTSHRINK (93)
552 #define SWM_ARG_ID_MOVEUP (100)
553 #define SWM_ARG_ID_MOVEDOWN (101)
554 #define SWM_ARG_ID_MOVELEFT (102)
555 #define SWM_ARG_ID_MOVERIGHT (103)
561 TAILQ_ENTRY(quirk
) entry
;
565 #define SWM_Q_FLOAT (1<<0) /* float this window */
566 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
567 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
568 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
569 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
570 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
572 TAILQ_HEAD(quirk_list
, quirk
);
573 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
576 * Supported EWMH hints should be added to
577 * both the enum and the ewmh array
582 _NET_MOVERESIZE_WINDOW
,
583 _NET_WM_ACTION_CLOSE
,
584 _NET_WM_ACTION_FULLSCREEN
,
586 _NET_WM_ACTION_RESIZE
,
587 _NET_WM_ALLOWED_ACTIONS
,
590 _NET_WM_STATE_FULLSCREEN
,
591 _NET_WM_STATE_HIDDEN
,
592 _NET_WM_STATE_MAXIMIZED_HORZ
,
593 _NET_WM_STATE_MAXIMIZED_VERT
,
594 _NET_WM_STATE_SKIP_PAGER
,
595 _NET_WM_STATE_SKIP_TASKBAR
,
597 _NET_WM_WINDOW_TYPE_DIALOG
,
598 _NET_WM_WINDOW_TYPE_DOCK
,
599 _NET_WM_WINDOW_TYPE_NORMAL
,
600 _NET_WM_WINDOW_TYPE_SPLASH
,
601 _NET_WM_WINDOW_TYPE_TOOLBAR
,
602 _NET_WM_WINDOW_TYPE_UTILITY
,
603 _SWM_WM_STATE_MANUAL
,
610 } ewmh
[SWM_EWMH_HINT_MAX
] = {
611 /* must be in same order as in the enum */
612 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
613 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
614 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
615 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
616 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
617 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
618 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
619 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
620 {"_NET_WM_STATE", XCB_ATOM_NONE
},
621 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
622 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
623 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
624 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
625 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
626 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
627 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
628 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
629 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
630 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
631 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
632 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
633 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
634 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
635 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
638 /* function prototypes */
639 xcb_char2b_t
*char2b(const char *);
640 int conf_load(char *, int);
641 void constrain_window(struct ws_win
*, struct swm_region
*, int);
643 void event_handle(xcb_generic_event_t
*);
644 struct ws_win
*find_window(xcb_window_t
);
645 int floating_toggle_win(struct ws_win
*);
646 void focus(struct swm_region
*, union arg
*);
647 void focus_magic(struct ws_win
*);
648 xcb_atom_t
get_atom_from_string(const char *);
649 xcb_screen_t
*get_screen(int);
650 char *get_win_name(xcb_window_t
);
651 uint16_t getstate(xcb_window_t
);
652 void grabbuttons(struct ws_win
*, int);
653 void map_window_raised(xcb_window_t
);
654 void new_region(struct swm_screen
*, int, int, int, int);
655 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
656 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
657 void store_float_geom(struct ws_win
*, struct swm_region
*);
658 void unmanage_window(struct ws_win
*);
659 void update_window(struct ws_win
*);
661 /* function definitions */
663 char2b(const char *str
)
669 s
= malloc(len
* sizeof(xcb_char2b_t
));
673 for (i
= 0; i
< len
; i
++) {
682 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
684 unsigned int tmpr
, tmpg
, tmpb
;
686 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
697 get_screen(int screen
)
699 xcb_screen_iterator_t i
;
701 i
= xcb_setup_roots_iterator(xcb_get_setup(conn
));
702 for (; i
.rem
; --screen
, xcb_screen_next(&i
))
712 xcb_get_input_focus_cookie_t c
;
713 xcb_get_input_focus_reply_t
*r
;
716 c
= xcb_get_input_focus(conn
);
718 r
= xcb_get_input_focus_reply(conn
, c
, NULL
);
724 map_window_raised(xcb_window_t win
)
726 uint32_t val
= XCB_STACK_MODE_ABOVE
;
728 xcb_configure_window(conn
, win
,
729 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
731 xcb_map_window(conn
, win
);
735 get_atom_from_string(const char *str
)
737 xcb_intern_atom_cookie_t c
;
738 xcb_intern_atom_reply_t
*r
;
741 c
= xcb_intern_atom(conn
, False
, strlen(str
), str
);
742 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
750 return (XCB_ATOM_NONE
);
754 update_iconic(struct ws_win
*win
, int newv
)
761 iprop
= get_atom_from_string("_SWM_ICONIC");
762 if (iprop
== XCB_ATOM_NONE
)
766 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
767 iprop
, XCB_ATOM_INTEGER
, 32, 1, &v
);
769 xcb_delete_property(conn
, win
->id
, iprop
);
773 get_iconic(struct ws_win
*win
)
777 xcb_get_property_reply_t
*pr
= NULL
;
779 iprop
= get_atom_from_string("_SWM_ICONIC");
780 if (iprop
== XCB_ATOM_NONE
)
783 pr
= xcb_get_property_reply(conn
,
784 xcb_get_property(conn
, False
, win
->id
, iprop
, XCB_ATOM_INTEGER
,
788 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
790 v
= *((int32_t *)xcb_get_property_value(pr
));
801 int i
, j
, num_screens
;
803 sup_list
= get_atom_from_string("_NET_SUPPORTED");
805 for (i
= 0; i
< LENGTH(ewmh
); i
++)
806 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
808 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
809 for (i
= 0; i
< num_screens
; i
++) {
810 /* Support check window will be created by workaround(). */
812 /* Report supported atoms */
813 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
814 for (j
= 0; j
< LENGTH(ewmh
); j
++)
815 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
816 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
825 xcb_atom_t sup_check
, sup_list
;
827 xcb_get_property_cookie_t pc
;
828 xcb_get_property_reply_t
*pr
;
830 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
831 sup_list
= get_atom_from_string("_NET_SUPPORTED");
832 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
834 for (i
= 0; i
< num_screens
; i
++) {
835 /* Get the support check window and destroy it */
836 pc
= xcb_get_property(conn
, False
, screens
[i
].root
, sup_check
,
837 XCB_ATOM_WINDOW
, 0, 1);
838 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
840 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
842 xcb_destroy_window(conn
, id
);
843 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
844 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
852 ewmh_autoquirk(struct ws_win
*win
)
856 xcb_get_property_cookie_t c
;
857 xcb_get_property_reply_t
*r
;
859 c
= xcb_get_property(conn
, False
, win
->id
,
860 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
861 r
= xcb_get_property_reply(conn
, c
, NULL
);
865 n
= xcb_get_property_value_length(r
);
866 type
= xcb_get_property_value(r
);
868 for (i
= 0; i
< n
; i
++) {
869 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
871 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
872 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
873 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
875 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
878 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
879 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
881 win
->quirks
= SWM_Q_FLOAT
;
888 #define SWM_EWMH_ACTION_COUNT_MAX (6)
889 #define EWMH_F_FULLSCREEN (1<<0)
890 #define EWMH_F_ABOVE (1<<1)
891 #define EWMH_F_HIDDEN (1<<2)
892 #define EWMH_F_SKIP_PAGER (1<<3)
893 #define EWMH_F_SKIP_TASKBAR (1<<4)
894 #define SWM_F_MANUAL (1<<5)
897 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
905 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
906 "fullscreen %s\n", win
->id
, YESNO(fs
));
909 if (!win
->g_floatvalid
)
910 store_float_geom(win
, win
->ws
->r
);
912 win
->g
= win
->ws
->r
->g
;
915 if (win
->g_floatvalid
) {
916 /* refloat at last floating relative position */
917 win
->g
= win
->g_float
;
918 X(win
) += X(win
->ws
->r
);
919 Y(win
) += Y(win
->ws
->r
);
927 ewmh_update_actions(struct ws_win
*win
)
929 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
935 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
938 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
939 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
942 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
943 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
946 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
947 #define _NET_WM_STATE_ADD 1 /* add/set property */
948 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
951 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
953 unsigned int mask
= 0;
954 unsigned int changed
= 0;
955 unsigned int orig_flags
;
960 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
961 mask
= EWMH_F_FULLSCREEN
;
962 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
964 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
966 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
967 mask
= EWMH_F_SKIP_PAGER
;
968 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
969 mask
= EWMH_F_SKIP_TASKBAR
;
972 orig_flags
= win
->ewmh_flags
;
975 case _NET_WM_STATE_REMOVE
:
976 win
->ewmh_flags
&= ~mask
;
978 case _NET_WM_STATE_ADD
:
979 win
->ewmh_flags
|= mask
;
981 case _NET_WM_STATE_TOGGLE
:
982 win
->ewmh_flags
^= mask
;
986 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
988 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
990 if (!floating_toggle_win(win
))
991 win
->ewmh_flags
= orig_flags
; /* revert */
992 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
994 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
995 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
997 if (!ewmh_set_win_fullscreen(win
,
998 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
999 win
->ewmh_flags
= orig_flags
; /* revert */
1001 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1003 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
1004 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1005 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1006 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
1007 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1008 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1009 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1010 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1011 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1012 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1013 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1014 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1015 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1016 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1017 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1018 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1019 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1020 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1021 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1022 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1026 ewmh_get_win_state(struct ws_win
*win
)
1029 xcb_get_property_cookie_t c
;
1030 xcb_get_property_reply_t
*r
;
1036 win
->ewmh_flags
= 0;
1038 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1040 win
->ewmh_flags
|= SWM_F_MANUAL
;
1042 c
= xcb_get_property(conn
, False
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1043 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1044 r
= xcb_get_property_reply(conn
, c
, NULL
);
1048 states
= xcb_get_property_value(r
);
1049 n
= xcb_get_property_value_length(r
);
1051 for (i
= 0; i
< n
; i
++)
1052 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1060 dumpwins(struct swm_region
*r
, union arg
*args
)
1064 xcb_get_window_attributes_cookie_t c
;
1065 xcb_get_window_attributes_reply_t
*wa
;
1067 if (r
->ws
== NULL
) {
1068 warnx("dumpwins: invalid workspace");
1072 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1073 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1074 state
= getstate(win
->id
);
1075 c
= xcb_get_window_attributes(conn
, win
->id
);
1076 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1078 warnx("window: 0x%x, map_state: %d, state: %u, "
1079 "transient: 0x%x", win
->id
, wa
->map_state
,
1080 state
, win
->transient
);
1083 warnx("window: 0x%x, failed xcb_get_window_attributes",
1087 warnx("===== unmanaged window list =====");
1088 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1089 state
= getstate(win
->id
);
1090 c
= xcb_get_window_attributes(conn
, win
->id
);
1091 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1093 warnx("window: 0x%x, map_state: %d, state: %u, "
1094 "transient: 0x%x", win
->id
, wa
->map_state
,
1095 state
, win
->transient
);
1098 warnx("window: 0x%x, failed xcb_get_window_attributes",
1102 warnx("=================================");
1106 dumpwins(struct swm_region
*r
, union arg
*args
)
1109 #endif /* SWM_DEBUG */
1111 void event_error(xcb_generic_error_t
*);
1112 void expose(xcb_expose_event_t
*);
1113 void keypress(xcb_key_press_event_t
*);
1114 void buttonpress(xcb_button_press_event_t
*);
1115 void configurerequest(xcb_configure_request_event_t
*);
1116 void configurenotify(xcb_configure_notify_event_t
*);
1117 void destroynotify(xcb_destroy_notify_event_t
*);
1118 void enternotify(xcb_enter_notify_event_t
*);
1119 void mapnotify(xcb_map_notify_event_t
*);
1120 void mappingnotify(xcb_mapping_notify_event_t
*);
1121 void maprequest(xcb_map_request_event_t
*);
1122 void propertynotify(xcb_property_notify_event_t
*);
1123 void unmapnotify(xcb_unmap_notify_event_t
*);
1124 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
1125 void clientmessage(xcb_client_message_event_t
*);
1126 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1131 int saved_errno
, status
;
1134 saved_errno
= errno
;
1138 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1143 if (errno
!= ECHILD
)
1144 warn("sighdlr: waitpid");
1145 #endif /* SWM_DEBUG */
1148 if (pid
== searchpid
)
1152 if (WIFEXITED(status
)) {
1153 if (WEXITSTATUS(status
) != 0)
1154 warnx("sighdlr: child exit status: %d",
1155 WEXITSTATUS(status
));
1157 warnx("sighdlr: child is terminated "
1159 #endif /* SWM_DEBUG */
1173 errno
= saved_errno
;
1179 struct pid_e
*p
= NULL
;
1181 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1186 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1195 name_to_color(const char *colorname
)
1197 uint32_t result
= 0;
1198 char cname
[32] = "#";
1199 xcb_screen_t
*screen
;
1200 xcb_colormap_t cmap
;
1201 xcb_alloc_color_reply_t
*cr
;
1202 xcb_alloc_named_color_reply_t
*nr
;
1203 uint16_t rr
, gg
, bb
;
1205 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1206 cmap
= screen
->default_colormap
;
1208 /* color is in format rgb://rr/gg/bb */
1209 if (strncmp(colorname
, "rgb:", 4) == 0) {
1210 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1211 warnx("could not parse rgb %s", colorname
);
1213 cr
= xcb_alloc_color_reply(conn
,
1214 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1220 warnx("color '%s' not found", colorname
);
1223 nr
= xcb_alloc_named_color_reply(conn
,
1224 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1227 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1228 nr
= xcb_alloc_named_color_reply(conn
,
1229 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1236 warnx("color '%s' not found", colorname
);
1243 setscreencolor(char *val
, int i
, int c
)
1247 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1248 if (i
> 0 && i
<= num_screens
) {
1249 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1250 free(screens
[i
- 1].c
[c
].name
);
1251 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1253 } else if (i
== -1) {
1254 for (i
= 0; i
< num_screens
; i
++) {
1255 screens
[i
].c
[c
].color
= name_to_color(val
);
1256 free(screens
[i
].c
[c
].name
);
1257 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1261 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1266 fancy_stacker(struct workspace
*ws
)
1268 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1269 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1270 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1271 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1272 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1273 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1274 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1275 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1276 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1280 plain_stacker(struct workspace
*ws
)
1282 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1283 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1284 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1285 sizeof ws
->stacker
);
1286 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1287 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1288 sizeof ws
->stacker
);
1292 custom_region(char *val
)
1294 unsigned int x
, y
, w
, h
;
1295 int sidx
, num_screens
;
1296 xcb_screen_t
*screen
;
1298 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1299 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1300 errx(1, "invalid custom region, "
1301 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1302 if (sidx
< 1 || sidx
> num_screens
)
1303 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1307 screen
= get_screen(sidx
);
1309 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1311 if (x
> screen
->width_in_pixels
||
1312 y
> screen
->height_in_pixels
||
1313 w
+ x
> screen
->width_in_pixels
||
1314 h
+ y
> screen
->height_in_pixels
) {
1315 warnx("ignoring region %ux%u+%u+%u - not within screen "
1316 "boundaries (%ux%u)", w
, h
, x
, y
,
1317 screen
->width_in_pixels
, screen
->height_in_pixels
);
1321 new_region(&screens
[sidx
], x
, y
, w
, h
);
1325 socket_setnonblock(int fd
)
1329 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1330 err(1, "fcntl F_GETFL");
1331 flags
|= O_NONBLOCK
;
1332 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1333 err(1, "fcntl F_SETFL");
1337 bar_print(struct swm_region
*r
, const char *s
)
1340 xcb_rectangle_t rect
;
1343 xcb_query_text_extents_reply_t
*ter
;
1350 ter
= xcb_query_text_extents_reply(conn
,
1351 xcb_query_text_extents(conn
, bar_fs
, len
, c2b
),
1357 width
= ter
->overall_width
;
1362 switch (bar_justify
) {
1363 case SWM_BAR_JUSTIFY_LEFT
:
1366 case SWM_BAR_JUSTIFY_CENTER
:
1367 x
= (WIDTH(r
) - width
) / 2;
1369 case SWM_BAR_JUSTIFY_RIGHT
:
1370 x
= WIDTH(r
) - width
- SWM_BAR_OFFSET
;
1374 if (x
< SWM_BAR_OFFSET
)
1379 rect
.width
= WIDTH(r
->bar
);
1380 rect
.height
= HEIGHT(r
->bar
);
1382 /* clear back buffer */
1383 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
1384 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1385 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
,
1386 sizeof(rect
), &rect
);
1388 /* draw back buffer */
1389 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
1390 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_BACKGROUND
, gcv
);
1391 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
;
1392 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1394 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FONT
, gcv
);
1396 xcb_image_text_8(conn
, len
, r
->bar
->buffer
, r
->s
->bar_gc
, x
,
1400 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1401 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1405 bar_extra_stop(void)
1409 bzero(bar_pipe
, sizeof bar_pipe
);
1412 kill(bar_pid
, SIGTERM
);
1415 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1420 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1422 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1424 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1425 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1429 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1431 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1433 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1434 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1438 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1440 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1443 bar_class_name(s
, sz
, r
);
1444 strlcat(s
, ":", sz
);
1445 bar_title_name(s
, sz
, r
);
1449 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1451 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1453 if (r
->ws
->focus
->floating
)
1454 strlcat(s
, "(f)", sz
);
1458 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1462 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1464 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1467 strlcat(s
, title
, sz
);
1471 int urgent
[SWM_WS_MAX
];
1473 bar_urgent(char *s
, size_t sz
)
1476 int i
, j
, num_screens
;
1478 xcb_get_property_cookie_t c
;
1479 xcb_icccm_wm_hints_t hints
;
1481 for (i
= 0; i
< workspace_limit
; i
++)
1484 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1485 for (i
= 0; i
< num_screens
; i
++)
1486 for (j
= 0; j
< workspace_limit
; j
++)
1487 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1488 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
1489 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
1492 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
1496 for (i
= 0; i
< workspace_limit
; i
++) {
1498 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1500 snprintf(b
, sizeof b
, "- ");
1506 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1508 if (r
== NULL
|| r
->ws
== NULL
)
1510 if (r
->ws
->name
!= NULL
)
1511 strlcat(s
, r
->ws
->name
, sz
);
1514 /* build the default bar format according to the defined enabled options */
1516 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1518 /* if format provided, just copy the buffers */
1519 if (bar_format
!= NULL
) {
1520 strlcpy(fmtnew
, fmtexp
, sz
);
1524 /* reset the output buffer */
1527 strlcat(fmtnew
, "+N:+I ", sz
);
1529 strlcat(fmtnew
, "+S", sz
);
1530 strlcat(fmtnew
, " ", sz
);
1532 /* only show the workspace name if there's actually one */
1533 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1534 strlcat(fmtnew
, "<+D>", sz
);
1535 strlcat(fmtnew
, "+3<", sz
);
1537 if (clock_enabled
) {
1538 strlcat(fmtnew
, fmtexp
, sz
);
1539 strlcat(fmtnew
, "+4<", sz
);
1542 /* bar_urgent already adds the space before the last asterisk */
1544 strlcat(fmtnew
, "* +U*+4<", sz
);
1546 if (title_class_enabled
) {
1547 strlcat(fmtnew
, "+C", sz
);
1548 if (title_name_enabled
== 0)
1549 strlcat(fmtnew
, "+4<", sz
);
1552 /* checks needed by the colon and floating strlcat(3) calls below */
1553 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1554 if (title_name_enabled
) {
1555 if (title_class_enabled
)
1556 strlcat(fmtnew
, ":", sz
);
1557 strlcat(fmtnew
, "+T+4<", sz
);
1559 if (window_name_enabled
) {
1560 if (r
->ws
->focus
->floating
)
1561 strlcat(fmtnew
, "+F ", sz
);
1562 strlcat(fmtnew
, "+64W ", sz
);
1566 /* finally add the action script output and the version */
1567 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1571 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1573 /* special case; no limit given, pad one space, instead */
1574 if (*limit
== (int)sz
- 1)
1576 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1579 /* replaces the bar format character sequences (like in tmux(1)) */
1581 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1585 char tmp
[SWM_BAR_MAX
];
1589 /* reset strlcat(3) buffer */
1592 /* get number, if any */
1595 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1596 limit
= sizeof tmp
- 1;
1597 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
1598 limit
= sizeof tmp
- 1;
1600 /* there is nothing to replace (ie EOL) */
1607 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1610 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1613 bar_class_name(tmp
, sizeof tmp
, r
);
1616 bar_workspace_name(tmp
, sizeof tmp
, r
);
1619 bar_window_float(tmp
, sizeof tmp
, r
);
1622 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1625 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1628 bar_class_title_name(tmp
, sizeof tmp
, r
);
1631 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1634 bar_title_name(tmp
, sizeof tmp
, r
);
1637 bar_urgent(tmp
, sizeof tmp
);
1640 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1643 bar_window_name(tmp
, sizeof tmp
, r
);
1646 /* unknown character sequence; copy as-is */
1647 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1653 if ((int)len
< limit
)
1655 while (limit
-- > 0) {
1656 if (*offrep
>= sz
- 1)
1658 fmtrep
[(*offrep
)++] = *ptr
++;
1666 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1671 while (*fmt
!= '\0') {
1673 /* skip ordinary characters */
1676 fmtrep
[off
++] = *fmt
++;
1680 /* character sequence found; replace it */
1681 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1690 bar_fmt_expand(char *fmtexp
, size_t sz
)
1697 /* start by grabbing the current time and date */
1699 localtime_r(&tmt
, &tm
);
1701 /* figure out what to expand */
1702 if (bar_format
!= NULL
)
1704 else if (bar_format
== NULL
&& clock_enabled
)
1706 /* if nothing to expand bail out */
1712 /* copy as-is, just in case the format shouldn't be expanded below */
1713 strlcpy(fmtexp
, fmt
, sz
);
1714 /* finally pass the string through strftime(3) */
1715 #ifndef SWM_DENY_CLOCK_FORMAT
1716 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1717 warnx("format too long");
1725 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1726 char fmtrep
[SWM_BAR_MAX
];
1728 struct swm_region
*r
;
1730 /* expand the format by first passing it through strftime(3) */
1731 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1733 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1734 for (i
= 0; i
< num_screens
; i
++) {
1735 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1738 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1739 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1740 bar_print(r
, fmtrep
);
1751 if (bar_enabled
== 0)
1753 if (bar_extra
&& bar_extra_running
) {
1754 /* ignore short reads; it'll correct itself */
1755 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1756 if (b
&& b
[len
- 1] == '\n') {
1758 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1760 if (b
== NULL
&& errno
!= EAGAIN
) {
1761 warn("bar_update: bar_extra failed");
1765 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1778 bar_toggle(struct swm_region
*r
, union arg
*args
)
1780 struct swm_region
*tmpr
;
1783 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1785 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1787 for (i
= 0; i
< num_screens
; i
++)
1788 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1790 xcb_unmap_window(conn
, tmpr
->bar
->id
);
1792 for (i
= 0; i
< num_screens
; i
++)
1793 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1795 map_window_raised(tmpr
->bar
->id
);
1798 bar_enabled
= !bar_enabled
;
1801 /* must be after stack */
1810 struct swm_region
*r
;
1814 /* do this here because the conf file is in memory */
1815 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1816 /* launch external status app */
1817 bar_extra_running
= 1;
1818 if (pipe(bar_pipe
) == -1)
1819 err(1, "pipe error");
1820 socket_setnonblock(bar_pipe
[0]);
1821 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1822 if (dup2(bar_pipe
[0], 0) == -1)
1824 if (dup2(bar_pipe
[1], 1) == -1)
1826 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1827 err(1, "could not disable SIGPIPE");
1828 switch (bar_pid
= fork()) {
1830 err(1, "cannot fork");
1834 execvp(bar_argv
[0], bar_argv
);
1835 err(1, "%s external app failed", bar_argv
[0]);
1837 default: /* parent */
1843 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1844 for (i
= 0; i
< num_screens
; i
++)
1845 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1848 wa
[0] = screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1849 wa
[1] = screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1850 xcb_change_window_attributes(conn
, r
->bar
->id
,
1851 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
1857 bar_setup(struct swm_region
*r
)
1860 xcb_screen_t
*screen
= get_screen(r
->s
->idx
);
1862 xcb_generic_error_t
*error
;
1863 xcb_void_cookie_t voc
;
1864 xcb_query_font_reply_t
*bar_fs_info
;
1867 xcb_close_font(conn
, bar_fs
);
1871 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
1872 err(1, "bar_setup: calloc: failed to allocate memory.");
1874 bar_fs
= xcb_generate_id(conn
);
1875 while ((bar_font
= strsep(&bar_fonts
, " ,")) != NULL
) {
1876 if (*bar_font
== '\0')
1879 DNPRINTF(SWM_D_INIT
, "bar_setup: try font %s\n", bar_font
);
1880 voc
= xcb_open_font_checked(conn
, bar_fs
, strlen(bar_font
),
1883 if ((error
= xcb_request_check(conn
, voc
))) {
1884 DNPRINTF(SWM_D_INIT
,
1885 "bar_setup: unable to open font: %s\n",
1888 warnx("unable to load font %s", bar_font
);
1890 DNPRINTF(SWM_D_INIT
,
1891 "bar_setup: successfully opened font: %s\n",
1897 bar_fs_info
= xcb_query_font_reply(conn
,
1898 xcb_query_font(conn
, bar_fs
),
1901 errx(1, "unable to get font information for font %s\n",
1903 bar_fs_height
= bar_fs_info
->font_ascent
+ bar_fs_info
->font_descent
;
1906 bar_height
= bar_fs_height
+ 4 * bar_border_width
;
1912 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1913 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
1914 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
1916 r
->bar
->id
= xcb_generate_id(conn
);
1917 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
1918 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
;
1919 wa
[2] = XCB_EVENT_MASK_EXPOSURE
;
1920 DNPRINTF(SWM_D_BAR
, "bar_setup: create_window: (x,y) w x h: (%d,%d) "
1921 "%d x %d\n", X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
));
1922 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
1923 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
1924 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1925 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
1926 | XCB_CW_EVENT_MASK
, wa
);
1928 r
->bar
->buffer
= xcb_generate_id(conn
);
1929 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
1930 WIDTH(r
->bar
), HEIGHT(r
->bar
));
1932 xcb_randr_select_input(conn
, r
->bar
->id
,
1933 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
1936 map_window_raised(r
->bar
->id
);
1938 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
1939 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
1942 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1943 err(1, "could not install bar_signal");
1948 bar_cleanup(struct swm_region
*r
)
1952 xcb_destroy_window(conn
, r
->bar
->id
);
1953 xcb_free_pixmap(conn
, r
->bar
->buffer
);
1959 set_win_state(struct ws_win
*win
, uint16_t state
)
1961 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
1963 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x\n", win
->id
);
1968 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
1969 a_state
, 32, 2, data
);
1973 getstate(xcb_window_t w
)
1975 uint16_t result
= 0;
1976 xcb_get_property_cookie_t c
;
1977 xcb_get_property_reply_t
*r
;
1979 c
= xcb_get_property(conn
, False
, w
, a_state
, a_state
, 0L, 2L);
1980 r
= xcb_get_property_reply(conn
, c
, NULL
);
1983 result
= *((uint16_t *)xcb_get_property_value(r
));
1987 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
1993 version(struct swm_region
*r
, union arg
*args
)
1995 bar_version
= !bar_version
;
1997 snprintf(bar_vertext
, sizeof bar_vertext
,
1998 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2000 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2007 client_msg(struct ws_win
*win
, xcb_atom_t a
)
2009 xcb_client_message_event_t ev
;
2014 bzero(&ev
, sizeof ev
);
2015 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2016 ev
.window
= win
->id
;
2019 ev
.data
.data32
[0] = a
;
2020 ev
.data
.data32
[1] = XCB_CURRENT_TIME
;
2022 xcb_send_event(conn
, False
, win
->id
,
2023 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2026 /* synthetic response to a ConfigureRequest when not making a change */
2028 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2030 xcb_configure_notify_event_t ce
;
2035 /* send notification of unchanged state. */
2036 bzero(&ce
, sizeof(ce
));
2037 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2040 ce
.width
= WIDTH(win
);
2041 ce
.height
= HEIGHT(win
);
2042 ce
.override_redirect
= False
;
2047 ce
.window
= win
->id
;
2048 ce
.border_width
= BORDER(win
);
2049 ce
.above_sibling
= XCB_WINDOW_NONE
;
2052 ce
.event
= ev
->window
;
2053 ce
.window
= ev
->window
;
2055 /* make response appear more WM_SIZE_HINTS-compliant */
2057 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2058 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2059 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2060 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2061 SH_INC_W(win
), SH_INC_H(win
));
2065 /* the hint may be set... to 0! */
2066 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2067 ce
.width
= SH_MIN_W(win
);
2068 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2069 ce
.height
= SH_MIN_H(win
);
2074 /* may also be advertized as 0 */
2075 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2076 ce
.width
= SH_MAX_W(win
);
2077 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2078 ce
.height
= SH_MAX_H(win
);
2081 /* resize increment. */
2083 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2084 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2086 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2087 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2091 /* adjust x and y for requested border_width. */
2092 ce
.x
+= BORDER(win
) - ev
->border_width
;
2093 ce
.y
+= BORDER(win
) - ev
->border_width
;
2094 ce
.border_width
= ev
->border_width
;
2095 ce
.above_sibling
= ev
->sibling
;
2098 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2099 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2100 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2102 xcb_send_event(conn
, False
, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2107 count_win(struct workspace
*ws
, int count_transient
)
2112 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2113 if (count_transient
== 0 && win
->floating
)
2115 if (count_transient
== 0 && win
->transient
)
2121 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2127 quit(struct swm_region
*r
, union arg
*args
)
2129 DNPRINTF(SWM_D_MISC
, "quit\n");
2134 unmap_window(struct ws_win
*win
)
2139 /* don't unmap again */
2140 if (getstate(win
->id
) == XCB_ICCCM_WM_STATE_ICONIC
)
2143 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2145 xcb_unmap_window(conn
, win
->id
);
2146 xcb_change_window_attributes(conn
, win
->id
,
2147 XCB_CW_BORDER_PIXEL
, &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2154 int i
, j
, num_screens
;
2156 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2157 for (i
= 0; i
< num_screens
; i
++)
2158 for (j
= 0; j
< workspace_limit
; j
++)
2159 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2164 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2166 xcb_key_press_event_t event
;
2167 xcb_keycode_t
*keycode
;
2172 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2174 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2177 bzero(&event
, sizeof(event
));
2178 event
.event
= win
->id
;
2179 event
.root
= win
->s
->root
;
2180 event
.child
= XCB_WINDOW_NONE
;
2181 event
.time
= XCB_CURRENT_TIME
;
2182 event
.event_x
= X(win
);
2183 event
.event_y
= Y(win
);
2186 event
.same_screen
= True
;
2187 event
.detail
= *keycode
;
2188 event
.state
= modifiers
;
2190 event
.response_type
= XCB_KEY_PRESS
;
2191 xcb_send_event(conn
, True
, win
->id
,
2192 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
2194 event
.response_type
= XCB_KEY_RELEASE
;
2195 xcb_send_event(conn
, True
, win
->id
,
2196 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
2200 restart(struct swm_region
*r
, union arg
*args
)
2202 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2204 /* disable alarm because the following code may not be interrupted */
2206 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
2207 err(1, "can't disable alarm");
2213 xcb_key_symbols_free(syms
);
2215 xcb_disconnect(conn
);
2217 execvp(start_argv
[0], start_argv
);
2218 warn("execvp failed");
2223 root_to_region(xcb_window_t root
)
2225 struct swm_region
*r
= NULL
;
2227 xcb_query_pointer_reply_t
*qpr
;
2229 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2231 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2232 for (i
= 0; i
< num_screens
; i
++)
2233 if (screens
[i
].root
== root
)
2236 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2237 screens
[i
].root
), NULL
);
2240 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: (%d,%d)\n",
2241 qpr
->root_x
, qpr
->root_y
);
2242 /* choose a region based on pointer location */
2243 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2244 if (X(r
) <= qpr
->root_x
&& qpr
->root_x
< MAX_X(r
) &&
2245 Y(r
) <= qpr
->root_y
&& qpr
->root_y
< MAX_Y(r
))
2251 r
= TAILQ_FIRST(&screens
[i
].rl
);
2257 find_unmanaged_window(xcb_window_t id
)
2260 int i
, j
, num_screens
;
2262 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2263 for (i
= 0; i
< num_screens
; i
++)
2264 for (j
= 0; j
< workspace_limit
; j
++)
2265 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2273 find_window(xcb_window_t id
)
2276 int i
, j
, num_screens
;
2277 xcb_query_tree_reply_t
*r
;
2279 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2280 for (i
= 0; i
< num_screens
; i
++)
2281 for (j
= 0; j
< workspace_limit
; j
++)
2282 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2286 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
2290 /* if we were looking for the parent return that window instead */
2291 if (r
->parent
== 0 || r
->root
== r
->parent
)
2294 /* look for parent */
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
)
2298 if (r
->parent
== win
->id
) {
2308 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2313 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2315 close(xcb_get_file_descriptor(conn
));
2317 setenv("LD_PRELOAD", SWM_LIB
, 1);
2319 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2320 warn("spawn: asprintf SWM_WS");
2323 setenv("_SWM_WS", ret
, 1);
2327 if (asprintf(&ret
, "%d", getpid()) == -1) {
2328 warn("spawn: asprintf _SWM_PID");
2331 setenv("_SWM_PID", ret
, 1);
2335 if (setsid() == -1) {
2336 warn("spawn: setsid");
2342 * close stdin and stdout to prevent interaction between apps
2343 * and the baraction script
2344 * leave stderr open to record errors
2346 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2347 warn("spawn: open");
2350 dup2(fd
, STDIN_FILENO
);
2351 dup2(fd
, STDOUT_FILENO
);
2356 execvp(args
->argv
[0], args
->argv
);
2358 warn("spawn: execvp");
2363 kill_refs(struct ws_win
*win
)
2365 int i
, x
, num_screens
;
2366 struct swm_region
*r
;
2367 struct workspace
*ws
;
2372 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2373 for (i
= 0; i
< num_screens
; i
++)
2374 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2375 for (x
= 0; x
< workspace_limit
; x
++) {
2377 if (win
== ws
->focus
)
2379 if (win
== ws
->focus_prev
)
2380 ws
->focus_prev
= NULL
;
2385 validate_win(struct ws_win
*testwin
)
2388 struct workspace
*ws
;
2389 struct swm_region
*r
;
2390 int i
, x
, num_screens
;
2392 if (testwin
== NULL
)
2395 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2396 for (i
= 0; i
< num_screens
; i
++)
2397 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2398 for (x
= 0; x
< workspace_limit
; x
++) {
2400 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2408 validate_ws(struct workspace
*testws
)
2410 struct swm_region
*r
;
2411 struct workspace
*ws
;
2412 int i
, x
, num_screens
;
2414 /* validate all ws */
2415 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2416 for (i
= 0; i
< num_screens
; i
++)
2417 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2418 for (x
= 0; x
< workspace_limit
; x
++) {
2427 unfocus_win(struct ws_win
*win
)
2429 xcb_window_t none
= XCB_WINDOW_NONE
;
2431 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
2435 if (win
->ws
== NULL
)
2438 if (validate_ws(win
->ws
))
2439 return; /* XXX this gets hit with thunderbird, needs fixing */
2441 if (win
->ws
->r
== NULL
)
2444 if (validate_win(win
)) {
2449 if (win
->ws
->focus
== win
) {
2450 win
->ws
->focus
= NULL
;
2451 win
->ws
->focus_prev
= win
;
2454 if (validate_win(win
->ws
->focus
)) {
2455 kill_refs(win
->ws
->focus
);
2456 win
->ws
->focus
= NULL
;
2458 if (validate_win(win
->ws
->focus_prev
)) {
2459 kill_refs(win
->ws
->focus_prev
);
2460 win
->ws
->focus_prev
= NULL
;
2463 grabbuttons(win
, 0);
2464 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2465 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2466 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2467 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
2474 int i
, j
, num_screens
;
2476 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2478 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2479 for (i
= 0; i
< num_screens
; i
++)
2480 for (j
= 0; j
< workspace_limit
; j
++)
2481 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2486 focus_win(struct ws_win
*win
)
2488 struct ws_win
*cfw
= NULL
;
2489 xcb_get_input_focus_reply_t
*r
;
2490 xcb_window_t cur_focus
= XCB_WINDOW_NONE
;
2492 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
2496 if (win
->ws
== NULL
)
2499 if (validate_ws(win
->ws
))
2500 return; /* XXX this gets hit with thunderbird, needs fixing */
2502 if (validate_win(win
)) {
2507 if (validate_win(win
)) {
2512 r
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
2514 cur_focus
= r
->focus
;
2517 if ((cfw
= find_window(cur_focus
)) != NULL
)
2520 /* use larger hammer since the window was killed somehow */
2521 /* TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2522 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2523 xcb_change_window_attributes(conn, cfw->id,
2524 XCB_CW_BORDER_PIXEL,
2525 &cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);*/
2528 win
->ws
->focus
= win
;
2530 if (win
->ws
->r
!= NULL
) {
2532 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
2533 win
->id
, XCB_CURRENT_TIME
);
2534 grabbuttons(win
, 1);
2535 xcb_change_window_attributes(conn
, win
->id
,
2536 XCB_CW_BORDER_PIXEL
,
2537 &win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2538 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2539 win
->ws
->always_raise
)
2540 map_window_raised(win
->id
);
2542 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2543 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2551 event_drain(uint8_t rt
)
2553 xcb_generic_event_t
*evt
;
2556 while ((evt
= xcb_poll_for_event(conn
))) {
2557 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
2565 switchws(struct swm_region
*r
, union arg
*args
)
2567 int wsid
= args
->id
, unmap_old
= 0;
2568 struct swm_region
*this_r
, *other_r
;
2570 struct workspace
*new_ws
, *old_ws
;
2576 if (wsid
>= workspace_limit
)
2580 old_ws
= this_r
->ws
;
2581 new_ws
= &this_r
->s
->ws
[wsid
];
2583 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2584 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2586 if (new_ws
== NULL
|| old_ws
== NULL
)
2588 if (new_ws
== old_ws
)
2591 other_r
= new_ws
->r
;
2592 if (other_r
== NULL
) {
2593 /* the other workspace is hidden, hide this one */
2597 /* the other ws is visible in another region, exchange them */
2598 other_r
->ws_prior
= new_ws
;
2599 other_r
->ws
= old_ws
;
2600 old_ws
->r
= other_r
;
2602 this_r
->ws_prior
= old_ws
;
2603 this_r
->ws
= new_ws
;
2606 /* this is needed so that we can click on a window after a restart */
2610 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2611 focus(new_ws
->r
, &a
);
2613 /* unmap old windows */
2615 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2618 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2619 event_drain(XCB_ENTER_NOTIFY
);
2625 cyclews(struct swm_region
*r
, union arg
*args
)
2628 struct swm_screen
*s
= r
->s
;
2631 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2632 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2637 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2640 case SWM_ARG_ID_CYCLEWS_UP
:
2641 if (a
.id
< workspace_limit
- 1)
2646 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2649 case SWM_ARG_ID_CYCLEWS_DOWN
:
2653 a
.id
= workspace_limit
- 1;
2660 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2662 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2666 } while (a
.id
!= r
->ws
->idx
);
2670 priorws(struct swm_region
*r
, union arg
*args
)
2674 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2675 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2677 if (r
->ws_prior
== NULL
)
2680 a
.id
= r
->ws_prior
->idx
;
2685 cyclescr(struct swm_region
*r
, union arg
*args
)
2687 struct swm_region
*rr
= NULL
;
2689 int i
, x
, y
, num_screens
;
2691 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2692 /* do nothing if we don't have more than one screen */
2693 if (!(num_screens
> 1 || outputs
> 1))
2698 case SWM_ARG_ID_CYCLESC_UP
:
2699 rr
= TAILQ_NEXT(r
, entry
);
2701 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2703 case SWM_ARG_ID_CYCLESC_DOWN
:
2704 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2706 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2714 /* move mouse to region */
2716 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2717 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0, 0,
2720 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2723 if (rr
->ws
->focus
) {
2724 /* move to focus window */
2725 x
= X(rr
->ws
->focus
) + 1;
2726 y
= Y(rr
->ws
->focus
) + 1;
2727 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0,
2733 sort_windows(struct ws_win_list
*wl
)
2735 struct ws_win
*win
, *parent
, *nxt
;
2740 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2741 nxt
= TAILQ_NEXT(win
, entry
);
2742 if (win
->transient
) {
2743 parent
= find_window(win
->transient
);
2744 if (parent
== NULL
) {
2745 warnx("not possible bug");
2748 TAILQ_REMOVE(wl
, win
, entry
);
2749 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2756 swapwin(struct swm_region
*r
, union arg
*args
)
2758 struct ws_win
*target
, *source
;
2759 struct ws_win
*cur_focus
;
2760 struct ws_win_list
*wl
;
2763 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2764 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2766 cur_focus
= r
->ws
->focus
;
2767 if (cur_focus
== NULL
)
2771 wl
= &source
->ws
->winlist
;
2774 case SWM_ARG_ID_SWAPPREV
:
2775 if (source
->transient
)
2776 source
= find_window(source
->transient
);
2777 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2778 if (target
&& target
->transient
)
2779 target
= find_window(target
->transient
);
2780 TAILQ_REMOVE(wl
, source
, entry
);
2782 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2784 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2786 case SWM_ARG_ID_SWAPNEXT
:
2787 target
= TAILQ_NEXT(source
, entry
);
2788 /* move the parent and let the sort handle the move */
2789 if (source
->transient
)
2790 source
= find_window(source
->transient
);
2791 TAILQ_REMOVE(wl
, source
, entry
);
2793 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2795 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2797 case SWM_ARG_ID_SWAPMAIN
:
2798 target
= TAILQ_FIRST(wl
);
2799 if (target
== source
) {
2800 if (source
->ws
->focus_prev
!= NULL
&&
2801 source
->ws
->focus_prev
!= target
)
2802 source
= source
->ws
->focus_prev
;
2806 if (target
== NULL
|| source
== NULL
)
2808 source
->ws
->focus_prev
= target
;
2809 TAILQ_REMOVE(wl
, target
, entry
);
2810 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2811 TAILQ_REMOVE(wl
, source
, entry
);
2812 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2814 case SWM_ARG_ID_MOVELAST
:
2815 TAILQ_REMOVE(wl
, source
, entry
);
2816 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2819 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2831 focus_prev(struct ws_win
*win
)
2833 struct ws_win
*winfocus
= NULL
;
2834 struct ws_win
*cur_focus
= NULL
;
2835 struct ws_win_list
*wl
= NULL
;
2836 struct workspace
*ws
= NULL
;
2838 if (!(win
&& win
->ws
))
2843 cur_focus
= ws
->focus
;
2845 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2846 WINID(win
), WINID(cur_focus
));
2848 /* pickle, just focus on whatever */
2849 if (cur_focus
== NULL
) {
2850 /* use prev_focus if valid */
2851 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2852 find_window(WINID(ws
->focus_prev
)))
2853 winfocus
= ws
->focus_prev
;
2857 /* if transient focus on parent */
2858 if (cur_focus
->transient
) {
2859 winfocus
= find_window(cur_focus
->transient
);
2863 /* if in max_stack try harder */
2864 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2865 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2866 if (cur_focus
!= ws
->focus_prev
)
2867 winfocus
= ws
->focus_prev
;
2869 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2874 DNPRINTF(SWM_D_FOCUS
, "focus_prev: focus_close: %d\n", focus_close
);
2876 if (winfocus
== NULL
|| winfocus
== win
) {
2877 switch (focus_close
) {
2878 case SWM_STACK_BOTTOM
:
2879 winfocus
= TAILQ_FIRST(wl
);
2882 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2884 case SWM_STACK_ABOVE
:
2885 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
2886 if (focus_close_wrap
)
2887 winfocus
= TAILQ_FIRST(wl
);
2889 winfocus
= TAILQ_PREV(cur_focus
,
2890 ws_win_list
, entry
);
2893 case SWM_STACK_BELOW
:
2894 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
2896 if (focus_close_wrap
)
2897 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2899 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2905 if (winfocus
== NULL
) {
2906 if (focus_default
== SWM_STACK_TOP
)
2907 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2909 winfocus
= TAILQ_FIRST(wl
);
2913 focus_magic(winfocus
);
2917 focus(struct swm_region
*r
, union arg
*args
)
2919 struct ws_win
*winfocus
= NULL
, *head
;
2920 struct ws_win
*cur_focus
= NULL
;
2921 struct ws_win_list
*wl
= NULL
;
2922 struct workspace
*ws
= NULL
;
2928 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2930 /* treat FOCUS_CUR special */
2931 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2932 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2933 winfocus
= r
->ws
->focus
;
2934 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2935 winfocus
= r
->ws
->focus_prev
;
2937 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2938 if (winfocus
->iconic
== 0)
2941 focus_magic(winfocus
);
2945 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2949 if (TAILQ_EMPTY(wl
))
2951 /* make sure there is at least one uniconified window */
2953 TAILQ_FOREACH(winfocus
, wl
, entry
)
2954 if (winfocus
->iconic
== 0) {
2962 case SWM_ARG_ID_FOCUSPREV
:
2963 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2965 head
= TAILQ_LAST(wl
, ws_win_list
);
2967 if (WINID(winfocus
) == cur_focus
->transient
) {
2968 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2970 head
= TAILQ_LAST(wl
, ws_win_list
);
2975 if (winfocus
&& winfocus
->iconic
) {
2976 while (winfocus
!= cur_focus
) {
2977 if (winfocus
== NULL
)
2978 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2979 if (winfocus
->iconic
== 0)
2981 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
2987 case SWM_ARG_ID_FOCUSNEXT
:
2988 head
= TAILQ_NEXT(cur_focus
, entry
);
2990 head
= TAILQ_FIRST(wl
);
2994 if (winfocus
&& winfocus
->iconic
) {
2995 while (winfocus
!= cur_focus
) {
2996 if (winfocus
== NULL
)
2997 winfocus
= TAILQ_FIRST(wl
);
2998 if (winfocus
->iconic
== 0)
3000 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3005 case SWM_ARG_ID_FOCUSMAIN
:
3006 winfocus
= TAILQ_FIRST(wl
);
3007 if (winfocus
== cur_focus
)
3008 winfocus
= cur_focus
->ws
->focus_prev
;
3015 focus_magic(winfocus
);
3021 cycle_layout(struct swm_region
*r
, union arg
*args
)
3023 struct workspace
*ws
= r
->ws
;
3026 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3029 if (ws
->cur_layout
->l_stack
== NULL
)
3030 ws
->cur_layout
= &layouts
[0];
3035 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3036 event_drain(XCB_ENTER_NOTIFY
);
3038 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3043 stack_config(struct swm_region
*r
, union arg
*args
)
3045 struct workspace
*ws
= r
->ws
;
3047 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3050 if (ws
->cur_layout
->l_config
!= NULL
)
3051 ws
->cur_layout
->l_config(ws
, args
->id
);
3053 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3060 struct swm_geometry g
;
3061 struct swm_region
*r
;
3067 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3069 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3070 for (i
= 0; i
< num_screens
; i
++) {
3074 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3075 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3076 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3078 /* start with screen geometry, adjust for bar */
3080 g
.w
-= 2 * border_width
;
3081 g
.h
-= 2 * border_width
;
3087 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3088 r
->ws
->cur_layout
->l_string(r
->ws
);
3089 /* save r so we can track region changes */
3096 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3097 event_drain(XCB_ENTER_NOTIFY
);
3099 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3103 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3105 /* retain window geom and region geom */
3106 win
->g_float
= win
->g
;
3107 win
->g_float
.x
-= X(r
);
3108 win
->g_float
.y
-= Y(r
);
3109 win
->g_floatvalid
= 1;
3110 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
3111 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3112 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3113 win
->g_float
.w
, win
->g_float
.h
);
3117 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3122 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
3125 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3126 * geom on ws switches or return from max mode
3128 if (win
->g_floatvalid
&& (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3129 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)))) {
3130 /* refloat at last floating relative position */
3131 win
->g
= win
->g_float
;
3136 win
->floatmaxed
= 0;
3139 * if set to fullscreen mode, configure window to maximum size.
3141 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3142 if (!win
->g_floatvalid
)
3143 store_float_geom(win
, win
->ws
->r
);
3145 win
->g
= win
->ws
->r
->g
;
3149 * remove border on fullscreen floater when in fullscreen mode or when
3150 * the quirk is present.
3152 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3153 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3154 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3155 if (win
->bordered
) {
3157 X(win
) += border_width
;
3158 Y(win
) += border_width
;
3160 } else if (!win
->bordered
) {
3162 X(win
) -= border_width
;
3163 Y(win
) -= border_width
;
3166 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3167 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3168 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3173 * floaters and transients are auto-centred unless moved
3176 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3177 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3180 /* keep window within region bounds */
3181 constrain_window(win
, r
, 0);
3187 * Send keystrokes to terminal to decrease/increase the font size as the
3188 * window size changes.
3191 adjust_font(struct ws_win
*win
)
3193 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3194 win
->floating
|| win
->transient
)
3197 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3198 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3199 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3200 win
->font_size_boundary
[win
->font_steps
] =
3201 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3204 win
->last_inc
= win
->sh
.width_inc
;
3205 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
3206 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3207 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3210 win
->last_inc
= win
->sh
.width_inc
;
3211 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
3215 #define SWAPXY(g) do { \
3217 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3218 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3221 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3223 struct swm_geometry win_g
, r_g
= *g
;
3224 struct ws_win
*win
, *fs_win
= NULL
;
3225 int i
, j
, s
, stacks
;
3226 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3227 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3228 int split
, colno
, winno
, mwin
, msize
, mscale
;
3229 int remain
, missing
, v_slice
, reconfigure
;
3231 xcb_get_window_attributes_reply_t
*war
;
3233 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3234 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3236 winno
= count_win(ws
, 0);
3237 if (winno
== 0 && count_win(ws
, 1) == 0)
3240 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3241 if (win
->transient
== 0 && win
->floating
== 0
3242 && win
->iconic
== 0)
3249 w_inc
= win
->sh
.width_inc
;
3250 w_base
= win
->sh
.base_width
;
3251 mwin
= ws
->l_state
.horizontal_mwin
;
3252 mscale
= ws
->l_state
.horizontal_msize
;
3253 stacks
= ws
->l_state
.horizontal_stacks
;
3256 w_inc
= win
->sh
.height_inc
;
3257 w_base
= win
->sh
.base_height
;
3258 mwin
= ws
->l_state
.vertical_mwin
;
3259 mscale
= ws
->l_state
.vertical_msize
;
3260 stacks
= ws
->l_state
.vertical_stacks
;
3264 if (stacks
> winno
- mwin
)
3265 stacks
= winno
- mwin
;
3269 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3270 if (mwin
&& winno
> mwin
) {
3271 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3275 win_g
.w
= v_slice
* mscale
;
3277 if (w_inc
> 1 && w_inc
< v_slice
) {
3278 /* adjust for window's requested size increment */
3279 remain
= (win_g
.w
- w_base
) % w_inc
;
3285 win_g
.x
+= r_g
.w
- msize
;
3288 colno
= split
= winno
/ stacks
;
3289 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3290 2 * border_width
) / stacks
);
3292 hrh
= r_g
.h
/ colno
;
3293 extra
= r_g
.h
- (colno
* hrh
);
3294 win_g
.h
= hrh
- 2 * border_width
;
3296 /* stack all the tiled windows */
3297 i
= j
= 0, s
= stacks
;
3298 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3299 if (win
->transient
!= 0 || win
->floating
!= 0)
3301 if (win
->iconic
!= 0)
3304 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3309 if (split
&& i
== split
) {
3310 colno
= (winno
- mwin
) / stacks
;
3311 if (s
<= (winno
- mwin
) % stacks
)
3313 split
= split
+ colno
;
3314 hrh
= (r_g
.h
/ colno
);
3315 extra
= r_g
.h
- (colno
* hrh
);
3319 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3320 win_g
.w
= (r_g
.w
- msize
-
3321 (stacks
* 2 * border_width
)) / stacks
;
3323 win_g
.w
+= (r_g
.w
- msize
-
3324 (stacks
* 2 * border_width
)) % stacks
;
3328 win_g
.h
= hrh
- 2 * border_width
;
3330 h_inc
= win
->sh
.width_inc
;
3331 h_base
= win
->sh
.base_width
;
3333 h_inc
= win
->sh
.height_inc
;
3334 h_base
= win
->sh
.base_height
;
3336 if (j
== colno
- 1) {
3337 win_g
.h
= hrh
+ extra
;
3338 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3339 /* adjust for window's requested size increment */
3340 remain
= (win_g
.h
- h_base
) % h_inc
;
3341 missing
= h_inc
- remain
;
3343 if (missing
<= extra
|| j
== 0) {
3355 win_g
.y
+= last_h
+ 2 * border_width
;
3357 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3359 win_g
.w
+= 2 * border_width
;
3360 win_g
.h
+= 2 * border_width
;
3365 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3366 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3370 WIDTH(win
) = win_g
.h
;
3371 HEIGHT(win
) = win_g
.w
;
3374 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3375 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3379 WIDTH(win
) = win_g
.w
;
3380 HEIGHT(win
) = win_g
.h
;
3384 if (bordered
!= win
->bordered
) {
3386 win
->bordered
= bordered
;
3394 war
= xcb_get_window_attributes_reply(conn
,
3395 xcb_get_window_attributes(conn
, win
->id
),
3398 if (war
->map_state
== XCB_MAP_STATE_UNMAPPED
)
3399 map_window_raised(win
->id
);
3409 /* now, stack all the floaters and transients */
3410 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3411 if (win
->transient
== 0 && win
->floating
== 0)
3413 if (win
->iconic
== 1)
3415 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3420 stack_floater(win
, ws
->r
);
3421 map_window_raised(win
->id
);
3425 stack_floater(fs_win
, ws
->r
);
3426 map_window_raised(fs_win
->id
);
3431 vertical_config(struct workspace
*ws
, int id
)
3433 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3437 case SWM_ARG_ID_STACKRESET
:
3438 case SWM_ARG_ID_STACKINIT
:
3439 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3440 ws
->l_state
.vertical_mwin
= 1;
3441 ws
->l_state
.vertical_stacks
= 1;
3443 case SWM_ARG_ID_MASTERSHRINK
:
3444 if (ws
->l_state
.vertical_msize
> 1)
3445 ws
->l_state
.vertical_msize
--;
3447 case SWM_ARG_ID_MASTERGROW
:
3448 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3449 ws
->l_state
.vertical_msize
++;
3451 case SWM_ARG_ID_MASTERADD
:
3452 ws
->l_state
.vertical_mwin
++;
3454 case SWM_ARG_ID_MASTERDEL
:
3455 if (ws
->l_state
.vertical_mwin
> 0)
3456 ws
->l_state
.vertical_mwin
--;
3458 case SWM_ARG_ID_STACKINC
:
3459 ws
->l_state
.vertical_stacks
++;
3461 case SWM_ARG_ID_STACKDEC
:
3462 if (ws
->l_state
.vertical_stacks
> 1)
3463 ws
->l_state
.vertical_stacks
--;
3465 case SWM_ARG_ID_FLIPLAYOUT
:
3466 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3474 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3476 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3478 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3482 horizontal_config(struct workspace
*ws
, int id
)
3484 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3487 case SWM_ARG_ID_STACKRESET
:
3488 case SWM_ARG_ID_STACKINIT
:
3489 ws
->l_state
.horizontal_mwin
= 1;
3490 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3491 ws
->l_state
.horizontal_stacks
= 1;
3493 case SWM_ARG_ID_MASTERSHRINK
:
3494 if (ws
->l_state
.horizontal_msize
> 1)
3495 ws
->l_state
.horizontal_msize
--;
3497 case SWM_ARG_ID_MASTERGROW
:
3498 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3499 ws
->l_state
.horizontal_msize
++;
3501 case SWM_ARG_ID_MASTERADD
:
3502 ws
->l_state
.horizontal_mwin
++;
3504 case SWM_ARG_ID_MASTERDEL
:
3505 if (ws
->l_state
.horizontal_mwin
> 0)
3506 ws
->l_state
.horizontal_mwin
--;
3508 case SWM_ARG_ID_STACKINC
:
3509 ws
->l_state
.horizontal_stacks
++;
3511 case SWM_ARG_ID_STACKDEC
:
3512 if (ws
->l_state
.horizontal_stacks
> 1)
3513 ws
->l_state
.horizontal_stacks
--;
3515 case SWM_ARG_ID_FLIPLAYOUT
:
3516 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3524 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3526 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3528 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3531 /* fullscreen view */
3533 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3535 struct swm_geometry gg
= *g
;
3536 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3537 int winno
, num_screens
;
3539 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3544 winno
= count_win(ws
, 0);
3545 if (winno
== 0 && count_win(ws
, 1) == 0)
3548 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3549 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3550 if (win
->transient
) {
3552 parent
= find_window(win
->transient
);
3556 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3558 * retain geometry for retrieval on exit from
3561 store_float_geom(win
, ws
->r
);
3562 win
->floatmaxed
= 1;
3565 /* only reconfigure if necessary */
3566 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3567 HEIGHT(win
) != gg
.h
) {
3573 WIDTH(win
) += 2 * border_width
;
3574 HEIGHT(win
) += 2 * border_width
;
3579 /* unmap only if we don't have multi screen */
3580 if (win
!= ws
->focus
)
3581 if (!(num_screens
> 1 || outputs
> 1))
3585 /* put the last transient on top */
3588 map_window_raised(parent
->id
);
3589 stack_floater(wintrans
, ws
->r
);
3590 focus_magic(wintrans
);
3595 send_to_ws(struct swm_region
*r
, union arg
*args
)
3597 int wsid
= args
->id
;
3598 struct ws_win
*win
= NULL
, *parent
;
3599 struct workspace
*ws
, *nws
;
3600 xcb_atom_t ws_idx_atom
= XCB_ATOM_NONE
;
3601 char ws_idx_str
[SWM_PROPLEN
];
3604 if (wsid
>= workspace_limit
)
3607 if (r
&& r
->ws
&& r
->ws
->focus
)
3613 if (win
->ws
->idx
== wsid
)
3616 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%x\n", win
->id
);
3619 nws
= &win
->s
->ws
[wsid
];
3621 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3623 if (win
->transient
) {
3624 parent
= find_window(win
->transient
);
3626 unmap_window(parent
);
3627 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3628 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3633 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3634 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3635 if (TAILQ_EMPTY(&ws
->winlist
))
3636 r
->ws
->focus
= NULL
;
3639 /* Try to update the window's workspace property */
3640 ws_idx_atom
= get_atom_from_string("_SWM_WS");
3642 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3644 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3646 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
3647 ws_idx_atom
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
3658 pressbutton(struct swm_region
*r
, union arg
*args
)
3660 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
3661 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3662 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
3663 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3667 raise_toggle(struct swm_region
*r
, union arg
*args
)
3669 if (r
== NULL
|| r
->ws
== NULL
)
3672 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3674 /* bring floaters back to top */
3675 if (r
->ws
->always_raise
== 0)
3682 iconify(struct swm_region
*r
, union arg
*args
)
3686 if (r
->ws
->focus
== NULL
)
3688 unmap_window(r
->ws
->focus
);
3689 update_iconic(r
->ws
->focus
, 1);
3692 r
->ws
->focus
= NULL
;
3693 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3700 get_win_name(xcb_window_t win
)
3703 xcb_get_property_cookie_t c
;
3704 xcb_icccm_get_text_property_reply_t r
;
3706 c
= xcb_icccm_get_wm_name(conn
, win
);
3707 if (xcb_icccm_get_wm_name_reply(conn
, c
, &r
, NULL
)) {
3708 if (r
.name_len
> 0) {
3709 name
= malloc(r
.name_len
+ 1);
3711 xcb_icccm_get_text_property_reply_wipe(&r
);
3714 memcpy(name
, r
.name
, r
.name_len
);
3715 name
[r
.name_len
] = '\0';
3717 xcb_icccm_get_text_property_reply_wipe(&r
);
3724 uniconify(struct swm_region
*r
, union arg
*args
)
3731 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3733 if (r
== NULL
|| r
->ws
== NULL
)
3736 /* make sure we have anything to uniconify */
3737 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3738 if (win
->ws
== NULL
)
3739 continue; /* should never happen */
3740 if (win
->iconic
== 0)
3748 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3750 spawn_select(r
, args
, "search", &searchpid
);
3752 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3755 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3756 if (win
->ws
== NULL
)
3757 continue; /* should never happen */
3758 if (win
->iconic
== 0)
3761 name
= get_win_name(win
->id
);
3764 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
3774 name_workspace(struct swm_region
*r
, union arg
*args
)
3778 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3784 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3786 spawn_select(r
, args
, "name_workspace", &searchpid
);
3788 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3791 fprintf(lfile
, "%s", "");
3796 search_workspace(struct swm_region
*r
, union arg
*args
)
3799 struct workspace
*ws
;
3802 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3808 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3810 spawn_select(r
, args
, "search", &searchpid
);
3812 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3815 for (i
= 0; i
< workspace_limit
; i
++) {
3819 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3820 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3827 search_win_cleanup(void)
3829 struct search_window
*sw
= NULL
;
3831 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3832 xcb_destroy_window(conn
, sw
->indicator
);
3833 xcb_free_gc(conn
, sw
->gc
);
3834 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3840 search_win(struct swm_region
*r
, union arg
*args
)
3842 struct ws_win
*win
= NULL
;
3843 struct search_window
*sw
= NULL
;
3845 uint32_t gcv
[4], wa
[2];
3851 xcb_query_text_extents_reply_t
*ter
;
3853 DNPRINTF(SWM_D_MISC
, "search_win\n");
3856 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3858 spawn_select(r
, args
, "search", &searchpid
);
3860 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3863 TAILQ_INIT(&search_wl
);
3866 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3867 if (win
->iconic
== 1)
3870 sw
= calloc(1, sizeof(struct search_window
));
3872 warn("search_win: calloc");
3874 search_win_cleanup();
3880 snprintf(s
, sizeof s
, "%d", i
);
3885 warn("search_win: char2b malloc");
3888 search_win_cleanup();
3891 ter
= xcb_query_text_extents_reply(conn
,
3892 xcb_query_text_extents(conn
, bar_fs
,
3896 warn("search_win: query text failed");
3900 search_win_cleanup();
3903 width
= ter
->overall_width
;
3907 w
= xcb_generate_id(conn
);
3908 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].color
;
3909 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
;
3910 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
3911 width
+ 4, bar_fs_height
+ 4,
3912 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
3913 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
3916 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3918 sw
->gc
= xcb_generate_id(conn
);
3919 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
3920 gcv
[1] = r
->s
->c
[SWM_S_COLOR_FOCUS
].color
;
3923 xcb_create_gc(conn
, sw
->gc
, w
, XCB_GC_FOREGROUND
|
3924 XCB_GC_BACKGROUND
| XCB_GC_FONT
| XCB_GC_GRAPHICS_EXPOSURES
,
3926 map_window_raised(w
);
3928 xcb_image_text_8(conn
, len
, w
, sw
->gc
, 2, bar_fs_height
, s
);
3930 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
3932 fprintf(lfile
, "%d\n", i
);
3942 search_resp_uniconify(char *resp
, unsigned long len
)
3948 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3950 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3951 if (win
->iconic
== 0)
3953 name
= get_win_name(win
->id
);
3956 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
3961 if (strncmp(s
, resp
, len
) == 0) {
3962 /* XXX this should be a callback to generalize */
3963 update_iconic(win
, 0);
3972 search_resp_name_workspace(char *resp
, unsigned long len
)
3974 struct workspace
*ws
;
3976 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3978 if (search_r
->ws
== NULL
)
3983 free(search_r
->ws
->name
);
3984 search_r
->ws
->name
= NULL
;
3988 ws
->name
= strdup(resp
);
3989 if (ws
->name
== NULL
) {
3990 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
3991 "strdup: %s", strerror(errno
));
3998 search_resp_search_workspace(char *resp
, unsigned long len
)
4005 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
4009 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
4016 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
4018 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
4025 switchws(search_r
, &a
);
4029 search_resp_search_window(char *resp
, unsigned long len
)
4034 struct search_window
*sw
;
4036 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
4040 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
4045 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
4047 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
4054 TAILQ_FOREACH(sw
, &search_wl
, entry
)
4055 if (idx
== sw
->idx
) {
4061 #define MAX_RESP_LEN 1024
4064 search_do_resp(void)
4070 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
4075 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
4076 warn("search: calloc");
4080 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
4082 warn("search: read error");
4085 resp
[rbytes
] = '\0';
4088 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4089 * newline, so work around that by sanitizing the resp now.
4091 resp
[strcspn(resp
, "\n")] = '\0';
4094 switch (search_resp_action
) {
4095 case SWM_SEARCH_UNICONIFY
:
4096 search_resp_uniconify(resp
, len
);
4098 case SWM_SEARCH_NAME_WORKSPACE
:
4099 search_resp_name_workspace(resp
, len
);
4101 case SWM_SEARCH_SEARCH_WORKSPACE
:
4102 search_resp_search_workspace(resp
, len
);
4104 case SWM_SEARCH_SEARCH_WINDOW
:
4105 search_resp_search_window(resp
, len
);
4110 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4111 search_win_cleanup();
4113 search_resp_action
= SWM_SEARCH_NONE
;
4114 close(select_resp_pipe
[0]);
4119 wkill(struct swm_region
*r
, union arg
*args
)
4121 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4123 if (r
->ws
->focus
== NULL
)
4126 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4127 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4129 if (r
->ws
->focus
->can_delete
)
4130 client_msg(r
->ws
->focus
, a_delete
);
4137 floating_toggle_win(struct ws_win
*win
)
4139 struct swm_region
*r
;
4149 /* reject floating toggles in max stack mode */
4150 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4153 if (win
->floating
) {
4154 if (!win
->floatmaxed
) {
4155 /* retain position for refloat */
4156 store_float_geom(win
, r
);
4160 if (win
->g_floatvalid
) {
4161 /* refloat at last floating relative position */
4162 X(win
) = win
->g_float
.x
+ X(r
);
4163 Y(win
) = win
->g_float
.y
+ Y(r
);
4164 WIDTH(win
) = win
->g_float
.w
;
4165 HEIGHT(win
) = win
->g_float
.h
;
4170 ewmh_update_actions(win
);
4176 floating_toggle(struct swm_region
*r
, union arg
*args
)
4178 struct ws_win
*win
= r
->ws
->focus
;
4184 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4185 _NET_WM_STATE_TOGGLE
);
4189 if (win
== win
->ws
->focus
) {
4190 a
.id
= SWM_ARG_ID_FOCUSCUR
;
4191 focus(win
->ws
->r
, &a
);
4195 event_drain(XCB_ENTER_NOTIFY
);
4199 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4201 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4203 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4205 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4208 if (X(win
) + BORDER(win
) < X(r
)) {
4210 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4212 X(win
) = X(r
) - BORDER(win
);
4215 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4217 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4219 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4222 if (Y(win
) + BORDER(win
) < Y(r
)) {
4224 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4226 Y(win
) = Y(r
) - BORDER(win
);
4232 if (HEIGHT(win
) < 1)
4238 update_window(struct ws_win
*win
)
4243 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
4244 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
4245 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
4249 wc
[3] = HEIGHT(win
);
4250 wc
[4] = BORDER(win
);
4252 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
4253 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
4254 wc
[3], YESNO(win
->bordered
));
4256 xcb_configure_window(conn
, win
->id
, mask
, wc
);
4259 #define SWM_RESIZE_STEPS (50)
4262 resize(struct ws_win
*win
, union arg
*args
)
4264 xcb_timestamp_t timestamp
= 0;
4265 struct swm_region
*r
= NULL
;
4266 int resize_step
= 0;
4267 struct swm_geometry g
;
4268 int top
= 0, left
= 0, resizing
;
4270 unsigned int shape
; /* cursor style */
4271 xcb_cursor_t cursor
;
4272 xcb_font_t cursor_font
;
4273 xcb_grab_pointer_cookie_t gpc
;
4274 xcb_grab_pointer_reply_t
*gpr
;
4275 xcb_query_pointer_reply_t
*xpr
;
4276 xcb_generic_event_t
*evt
;
4277 xcb_motion_notify_event_t
*mne
;
4283 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4286 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
4287 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
4290 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4293 /* reject resizes in max mode for floaters (transient ok) */
4294 if (win
->floatmaxed
)
4298 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4304 case SWM_ARG_ID_WIDTHSHRINK
:
4305 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4308 case SWM_ARG_ID_WIDTHGROW
:
4309 WIDTH(win
) += SWM_RESIZE_STEPS
;
4312 case SWM_ARG_ID_HEIGHTSHRINK
:
4313 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4316 case SWM_ARG_ID_HEIGHTGROW
:
4317 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4324 constrain_window(win
, r
, 1);
4326 store_float_geom(win
,r
);
4330 /* get cursor offset from window root */
4331 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4338 if (xpr
->win_x
< WIDTH(win
) / 2)
4341 if (xpr
->win_y
< HEIGHT(win
) / 2)
4344 if (args
->id
== SWM_ARG_ID_CENTER
)
4347 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
4349 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
4351 cursor_font
= xcb_generate_id(conn
);
4352 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4354 cursor
= xcb_generate_id(conn
);
4355 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4356 shape
, shape
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4358 gpc
= xcb_grab_pointer(conn
, False
, win
->id
, MOUSEMASK
,
4359 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
4361 gpr
= xcb_grab_pointer_reply(conn
, gpc
, NULL
);
4363 xcb_free_cursor(conn
, cursor
);
4364 xcb_close_font(conn
, cursor_font
);
4371 while ((evt
= xcb_wait_for_event(conn
)) && resizing
) {
4372 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4373 case XCB_BUTTON_RELEASE
:
4374 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
4377 case XCB_MOTION_NOTIFY
:
4378 mne
= (xcb_motion_notify_event_t
*)evt
;
4379 /* cursor offset/delta from start of the operation */
4380 dx
= mne
->root_x
- xpr
->root_x
;
4381 dy
= mne
->root_y
- xpr
->root_y
;
4387 if (args
->id
== SWM_ARG_ID_CENTER
) {
4388 if (g
.h
/ 2 + dy
< 1)
4392 HEIGHT(win
) = g
.h
+ 2 * dy
;
4400 HEIGHT(win
) = g
.h
+ dy
;
4407 if (args
->id
== SWM_ARG_ID_CENTER
) {
4408 if (g
.w
/ 2 + dx
< 1)
4412 WIDTH(win
) = g
.w
+ 2 * dx
;
4420 WIDTH(win
) = g
.w
+ dx
;
4423 constrain_window(win
, r
, 1);
4425 /* not free, don't sync more than 120 times / second */
4426 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4427 timestamp
= mne
->time
;
4442 store_float_geom(win
,r
);
4444 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4445 xcb_free_cursor(conn
, cursor
);
4446 xcb_close_font(conn
, cursor_font
);
4449 DNPRINTF(SWM_D_EVENT
, "resize: done\n");
4453 resize_step(struct swm_region
*r
, union arg
*args
)
4455 struct ws_win
*win
= NULL
;
4457 if (r
&& r
->ws
&& r
->ws
->focus
)
4465 #define SWM_MOVE_STEPS (50)
4468 move(struct ws_win
*win
, union arg
*args
)
4470 xcb_timestamp_t timestamp
= 0;
4471 int move_step
= 0, moving
;
4472 struct swm_region
*r
= NULL
;
4473 xcb_font_t cursor_font
;
4474 xcb_cursor_t cursor
;
4475 xcb_grab_pointer_cookie_t gpc
;
4476 xcb_grab_pointer_reply_t
*gpr
;
4477 xcb_query_pointer_reply_t
*qpr
;
4478 xcb_generic_event_t
*evt
;
4479 xcb_motion_notify_event_t
*mne
;
4485 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4488 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
4489 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4491 /* in max_stack mode should only move transients */
4492 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4496 if (win
->floating
== 0 && !win
->transient
) {
4497 store_float_geom(win
, r
);
4498 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4501 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4508 case SWM_ARG_ID_MOVELEFT
:
4509 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4512 case SWM_ARG_ID_MOVERIGHT
:
4513 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4516 case SWM_ARG_ID_MOVEUP
:
4517 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4520 case SWM_ARG_ID_MOVEDOWN
:
4521 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4528 constrain_window(win
, r
, 0);
4530 store_float_geom(win
, r
);
4534 cursor_font
= xcb_generate_id(conn
);
4535 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4537 cursor
= xcb_generate_id(conn
);
4538 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4539 XC_fleur
, XC_fleur
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4541 gpc
= xcb_grab_pointer(conn
, False
, win
->id
, MOUSEMASK
,
4542 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
4543 XCB_WINDOW_NONE
, cursor
, XCB_CURRENT_TIME
);
4544 gpr
= xcb_grab_pointer_reply(conn
, gpc
, NULL
);
4546 xcb_free_cursor(conn
, cursor
);
4547 xcb_close_font(conn
, cursor_font
);
4551 /* get cursor offset from window root */
4552 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4555 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4556 xcb_free_cursor(conn
, cursor
);
4557 xcb_close_font(conn
, cursor_font
);
4563 while ((evt
= xcb_wait_for_event(conn
)) && moving
) {
4564 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4565 case XCB_BUTTON_RELEASE
:
4566 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
4569 case XCB_MOTION_NOTIFY
:
4570 mne
= (xcb_motion_notify_event_t
*)evt
;
4571 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
4572 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
4574 constrain_window(win
, r
, 0);
4576 /* not free, don't sync more than 120 times / second */
4577 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4578 timestamp
= mne
->time
;
4593 store_float_geom(win
, r
);
4595 xcb_free_cursor(conn
, cursor
);
4596 xcb_close_font(conn
, cursor_font
);
4597 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4598 DNPRINTF(SWM_D_EVENT
, "move: done\n");
4602 move_step(struct swm_region
*r
, union arg
*args
)
4604 struct ws_win
*win
= NULL
;
4606 if (r
&& r
->ws
&& r
->ws
->focus
)
4611 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4618 /* user/key callable function IDs */
4668 KF_SEARCH_WORKSPACE
,
4709 KF_DUMPWINS
, /* MUST BE LAST */
4713 /* key definitions */
4715 char name
[SWM_FUNCNAME_LEN
];
4716 void (*func
)(struct swm_region
*r
, union arg
*);
4718 } keyfuncs
[KF_INVALID
+ 1] = {
4719 /* name function argument */
4720 { "bar_toggle", bar_toggle
, {0} },
4721 { "button2", pressbutton
, {2} },
4722 { "cycle_layout", cycle_layout
, {0} },
4723 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4724 { "float_toggle", floating_toggle
,{0} },
4725 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4726 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4727 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4728 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4729 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4730 { "iconify", iconify
, {0} },
4731 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4732 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4733 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4734 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4735 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4736 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4737 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4738 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4739 { "mvws_1", send_to_ws
, {.id
= 0} },
4740 { "mvws_2", send_to_ws
, {.id
= 1} },
4741 { "mvws_3", send_to_ws
, {.id
= 2} },
4742 { "mvws_4", send_to_ws
, {.id
= 3} },
4743 { "mvws_5", send_to_ws
, {.id
= 4} },
4744 { "mvws_6", send_to_ws
, {.id
= 5} },
4745 { "mvws_7", send_to_ws
, {.id
= 6} },
4746 { "mvws_8", send_to_ws
, {.id
= 7} },
4747 { "mvws_9", send_to_ws
, {.id
= 8} },
4748 { "mvws_10", send_to_ws
, {.id
= 9} },
4749 { "mvws_11", send_to_ws
, {.id
= 10} },
4750 { "mvws_12", send_to_ws
, {.id
= 11} },
4751 { "mvws_13", send_to_ws
, {.id
= 12} },
4752 { "mvws_14", send_to_ws
, {.id
= 13} },
4753 { "mvws_15", send_to_ws
, {.id
= 14} },
4754 { "mvws_16", send_to_ws
, {.id
= 15} },
4755 { "mvws_17", send_to_ws
, {.id
= 16} },
4756 { "mvws_18", send_to_ws
, {.id
= 17} },
4757 { "mvws_19", send_to_ws
, {.id
= 18} },
4758 { "mvws_20", send_to_ws
, {.id
= 19} },
4759 { "mvws_21", send_to_ws
, {.id
= 20} },
4760 { "mvws_22", send_to_ws
, {.id
= 21} },
4761 { "name_workspace", name_workspace
, {0} },
4762 { "quit", quit
, {0} },
4763 { "raise_toggle", raise_toggle
, {0} },
4764 { "restart", restart
, {0} },
4765 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4766 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4767 { "search_win", search_win
, {0} },
4768 { "search_workspace", search_workspace
, {0} },
4769 { "spawn_custom", NULL
, {0} },
4770 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4771 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4772 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4773 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4774 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4775 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4776 { "uniconify", uniconify
, {0} },
4777 { "version", version
, {0} },
4778 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4779 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4780 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4781 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4782 { "ws_1", switchws
, {.id
= 0} },
4783 { "ws_2", switchws
, {.id
= 1} },
4784 { "ws_3", switchws
, {.id
= 2} },
4785 { "ws_4", switchws
, {.id
= 3} },
4786 { "ws_5", switchws
, {.id
= 4} },
4787 { "ws_6", switchws
, {.id
= 5} },
4788 { "ws_7", switchws
, {.id
= 6} },
4789 { "ws_8", switchws
, {.id
= 7} },
4790 { "ws_9", switchws
, {.id
= 8} },
4791 { "ws_10", switchws
, {.id
= 9} },
4792 { "ws_11", switchws
, {.id
= 10} },
4793 { "ws_12", switchws
, {.id
= 11} },
4794 { "ws_13", switchws
, {.id
= 12} },
4795 { "ws_14", switchws
, {.id
= 13} },
4796 { "ws_15", switchws
, {.id
= 14} },
4797 { "ws_16", switchws
, {.id
= 15} },
4798 { "ws_17", switchws
, {.id
= 16} },
4799 { "ws_18", switchws
, {.id
= 17} },
4800 { "ws_19", switchws
, {.id
= 18} },
4801 { "ws_20", switchws
, {.id
= 19} },
4802 { "ws_21", switchws
, {.id
= 20} },
4803 { "ws_22", switchws
, {.id
= 21} },
4804 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4805 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4806 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4807 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4808 { "ws_prior", priorws
, {0} },
4809 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4810 { "invalid key func", NULL
, {0} },
4813 RB_ENTRY(key
) entry
;
4816 enum keyfuncid funcid
;
4819 RB_HEAD(key_tree
, key
);
4822 key_cmp(struct key
*kp1
, struct key
*kp2
)
4824 if (kp1
->keysym
< kp2
->keysym
)
4826 if (kp1
->keysym
> kp2
->keysym
)
4829 if (kp1
->mod
< kp2
->mod
)
4831 if (kp1
->mod
> kp2
->mod
)
4837 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
4838 struct key_tree keys
;
4841 enum { client_click
, root_click
};
4843 unsigned int action
;
4845 unsigned int button
;
4846 void (*func
)(struct ws_win
*, union arg
*);
4849 /* action key mouse button func args */
4850 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4851 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4852 { client_click
, MODKEY
, Button1
, move
, {0} },
4856 update_modkey(unsigned int mod
)
4862 RB_FOREACH(kp
, key_tree
, &keys
)
4863 if (kp
->mod
& ShiftMask
)
4864 kp
->mod
= mod
| ShiftMask
;
4868 for (i
= 0; i
< LENGTH(buttons
); i
++)
4869 if (buttons
[i
].mask
& ShiftMask
)
4870 buttons
[i
].mask
= mod
| ShiftMask
;
4872 buttons
[i
].mask
= mod
;
4877 TAILQ_ENTRY(spawn_prog
) entry
;
4882 TAILQ_HEAD(spawn_list
, spawn_prog
);
4883 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4886 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4889 struct spawn_prog
*prog
= NULL
;
4891 char *ap
, **real_args
;
4893 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4896 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4897 if (!strcasecmp(spawn_name
, prog
->name
))
4901 warnx("spawn_custom: program %s not found", spawn_name
);
4905 /* make room for expanded args */
4906 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4907 err(1, "spawn_custom: calloc real_args");
4909 /* expand spawn_args into real_args */
4910 for (i
= 0; i
< prog
->argc
; i
++) {
4912 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4913 if (!strcasecmp(ap
, "$bar_border")) {
4915 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4917 err(1, "spawn_custom border color");
4918 } else if (!strcasecmp(ap
, "$bar_color")) {
4920 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4922 err(1, "spawn_custom bar color");
4923 } else if (!strcasecmp(ap
, "$bar_font")) {
4924 if ((real_args
[i
] = strdup(bar_fonts
))
4926 err(1, "spawn_custom bar fonts");
4927 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4929 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4931 err(1, "spawn_custom color font");
4932 } else if (!strcasecmp(ap
, "$color_focus")) {
4934 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4936 err(1, "spawn_custom color focus");
4937 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4939 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4941 err(1, "spawn_custom color unfocus");
4943 /* no match --> copy as is */
4944 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4945 err(1, "spawn_custom strdup(ap)");
4947 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4952 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4953 for (i
= 0; i
< prog
->argc
; i
++)
4954 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4955 DNPRINTF(SWM_D_SPAWN
, "\n");
4957 *ret_args
= real_args
;
4958 return (prog
->argc
);
4962 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4968 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4972 spawn(r
->ws
->idx
, &a
, 1);
4974 for (i
= 0; i
< spawn_argc
; i
++)
4980 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4986 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4990 if (pipe(select_list_pipe
) == -1)
4991 err(1, "pipe error");
4992 if (pipe(select_resp_pipe
) == -1)
4993 err(1, "pipe error");
4995 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4996 err(1, "could not disable SIGPIPE");
4997 switch (*pid
= fork()) {
4999 err(1, "cannot fork");
5002 if (dup2(select_list_pipe
[0], 0) == -1)
5004 if (dup2(select_resp_pipe
[1], 1) == -1)
5006 close(select_list_pipe
[1]);
5007 close(select_resp_pipe
[0]);
5008 spawn(r
->ws
->idx
, &a
, 0);
5010 default: /* parent */
5011 close(select_list_pipe
[0]);
5012 close(select_resp_pipe
[1]);
5016 for (i
= 0; i
< spawn_argc
; i
++)
5022 spawn_insert(char *name
, char *args
)
5024 char *arg
, *cp
, *ptr
;
5025 struct spawn_prog
*sp
;
5027 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
5029 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
5030 err(1, "spawn_insert: calloc");
5031 if ((sp
->name
= strdup(name
)) == NULL
)
5032 err(1, "spawn_insert: strdup");
5034 /* convert the arguments to an argument list */
5035 if ((ptr
= cp
= strdup(args
)) == NULL
)
5036 err(1, "spawn_insert: strdup");
5037 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
5038 /* empty field; skip it */
5043 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
5044 sizeof *sp
->argv
)) == NULL
)
5045 err(1, "spawn_insert: realloc");
5046 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
5047 err(1, "spawn_insert: strdup");
5051 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
5052 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
5056 spawn_remove(struct spawn_prog
*sp
)
5060 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
5062 TAILQ_REMOVE(&spawns
, sp
, entry
);
5063 for (i
= 0; i
< sp
->argc
; i
++)
5069 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
5073 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
5075 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
5078 spawn_insert(name
, args
);
5080 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
5084 setspawn(char *name
, char *args
)
5086 struct spawn_prog
*sp
;
5088 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5093 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5094 if (!strcmp(sp
->name
, name
)) {
5098 spawn_replace(sp
, name
, args
);
5099 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5103 if (*args
== '\0') {
5104 warnx("error: setspawn: cannot find program: %s", name
);
5108 spawn_insert(name
, args
);
5109 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5113 setconfspawn(char *selector
, char *value
, int flags
)
5115 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
5117 setspawn(selector
, value
);
5119 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
5126 setconfspawn("term", "xterm", 0);
5127 setconfspawn("spawn_term", "xterm", 0);
5128 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5129 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5130 setconfspawn("lock", "xlock", 0);
5131 setconfspawn("initscr", "initscreen.sh", 0);
5132 setconfspawn("menu", "dmenu_run"
5135 " -nf $bar_font_color"
5137 " -sf $bar_color", 0);
5138 setconfspawn("search", "dmenu"
5142 " -nf $bar_font_color"
5144 " -sf $bar_color", 0);
5145 setconfspawn("name_workspace", "dmenu"
5149 " -nf $bar_font_color"
5151 " -sf $bar_color", 0);
5155 #define SWM_MODNAME_SIZE 32
5156 #define SWM_KEY_WS "\n+ \t"
5158 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5162 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5163 if (mod
== NULL
|| ks
== NULL
) {
5164 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5167 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5168 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5172 *ks
= XCB_NO_SYMBOL
;
5174 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5175 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5177 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5178 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5180 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5182 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5184 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5186 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5188 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5190 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5191 *mod
|= ControlMask
;
5193 *ks
= XStringToKeysym(name
);
5194 XConvertCase(*ks
, ks
, &uks
);
5195 if (ks
== XCB_NO_SYMBOL
) {
5197 "parsekeys: invalid key %s\n",
5203 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5208 strdupsafe(char *str
)
5213 return (strdup(str
));
5217 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5221 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5222 keyfuncs
[kfid
].name
, spawn_name
);
5224 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5225 err(1, "key_insert: malloc");
5230 kp
->spawn_name
= strdupsafe(spawn_name
);
5231 RB_INSERT(key_tree
, &keys
, kp
);
5233 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5237 key_lookup(unsigned int mod
, KeySym ks
)
5244 return (RB_FIND(key_tree
, &keys
, &kp
));
5248 key_remove(struct key
*kp
)
5250 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5252 RB_REMOVE(key_tree
, &keys
, kp
);
5253 free(kp
->spawn_name
);
5256 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5260 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5263 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5267 key_insert(mod
, ks
, kfid
, spawn_name
);
5269 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5273 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5278 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5279 keyfuncs
[kfid
].name
, spawn_name
);
5281 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5282 if (kfid
== KF_INVALID
)
5285 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5286 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5289 if (kfid
== KF_INVALID
) {
5290 warnx("error: setkeybinding: cannot find mod/key combination");
5291 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5295 key_insert(mod
, ks
, kfid
, spawn_name
);
5296 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5300 setconfbinding(char *selector
, char *value
, int flags
)
5302 enum keyfuncid kfid
;
5305 struct spawn_prog
*sp
;
5306 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5307 if (selector
== NULL
) {
5308 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5309 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5311 setkeybinding(mod
, ks
, kfid
, NULL
);
5316 /* search by key function name */
5317 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
5318 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5319 SWM_FUNCNAME_LEN
) == 0) {
5320 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5322 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5323 setkeybinding(mod
, ks
, kfid
, NULL
);
5329 /* search by custom spawn name */
5330 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5331 if (strcasecmp(selector
, sp
->name
) == 0) {
5332 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5334 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5335 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
5342 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5349 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
5350 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
5351 setkeybinding(MODKEY
|ShiftMask
, XK_space
, KF_STACK_RESET
, NULL
);
5352 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
5353 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
5354 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
5355 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
5356 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, KF_STACK_INC
, NULL
);
5357 setkeybinding(MODKEY
|ShiftMask
, XK_period
, KF_STACK_DEC
, NULL
);
5358 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
5359 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
5360 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
5361 setkeybinding(MODKEY
|ShiftMask
, XK_j
, KF_SWAP_NEXT
, NULL
);
5362 setkeybinding(MODKEY
|ShiftMask
, XK_k
, KF_SWAP_PREV
, NULL
);
5363 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
5364 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
5365 setkeybinding(MODKEY
|ShiftMask
, XK_q
, KF_QUIT
, NULL
);
5366 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
5367 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
5368 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
5369 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
5370 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
5371 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
5372 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
5373 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
5374 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
5375 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
5376 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
5377 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
5378 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
5379 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
5380 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
5381 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
5382 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
5383 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
5384 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
5385 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
5386 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
5387 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
5388 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
5389 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
5390 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
5391 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
5392 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
5393 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
5394 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
5395 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, KF_SCREEN_NEXT
, NULL
);
5396 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, KF_SCREEN_PREV
, NULL
);
5397 setkeybinding(MODKEY
|ShiftMask
, XK_1
, KF_MVWS_1
, NULL
);
5398 setkeybinding(MODKEY
|ShiftMask
, XK_2
, KF_MVWS_2
, NULL
);
5399 setkeybinding(MODKEY
|ShiftMask
, XK_3
, KF_MVWS_3
, NULL
);
5400 setkeybinding(MODKEY
|ShiftMask
, XK_4
, KF_MVWS_4
, NULL
);
5401 setkeybinding(MODKEY
|ShiftMask
, XK_5
, KF_MVWS_5
, NULL
);
5402 setkeybinding(MODKEY
|ShiftMask
, XK_6
, KF_MVWS_6
, NULL
);
5403 setkeybinding(MODKEY
|ShiftMask
, XK_7
, KF_MVWS_7
, NULL
);
5404 setkeybinding(MODKEY
|ShiftMask
, XK_8
, KF_MVWS_8
, NULL
);
5405 setkeybinding(MODKEY
|ShiftMask
, XK_9
, KF_MVWS_9
, NULL
);
5406 setkeybinding(MODKEY
|ShiftMask
, XK_0
, KF_MVWS_10
, NULL
);
5407 setkeybinding(MODKEY
|ShiftMask
, XK_F1
, KF_MVWS_11
, NULL
);
5408 setkeybinding(MODKEY
|ShiftMask
, XK_F2
, KF_MVWS_12
, NULL
);
5409 setkeybinding(MODKEY
|ShiftMask
, XK_F3
, KF_MVWS_13
, NULL
);
5410 setkeybinding(MODKEY
|ShiftMask
, XK_F4
, KF_MVWS_14
, NULL
);
5411 setkeybinding(MODKEY
|ShiftMask
, XK_F5
, KF_MVWS_15
, NULL
);
5412 setkeybinding(MODKEY
|ShiftMask
, XK_F6
, KF_MVWS_16
, NULL
);
5413 setkeybinding(MODKEY
|ShiftMask
, XK_F7
, KF_MVWS_17
, NULL
);
5414 setkeybinding(MODKEY
|ShiftMask
, XK_F8
, KF_MVWS_18
, NULL
);
5415 setkeybinding(MODKEY
|ShiftMask
, XK_F9
, KF_MVWS_19
, NULL
);
5416 setkeybinding(MODKEY
|ShiftMask
, XK_F10
, KF_MVWS_20
, NULL
);
5417 setkeybinding(MODKEY
|ShiftMask
, XK_F11
, KF_MVWS_21
, NULL
);
5418 setkeybinding(MODKEY
|ShiftMask
, XK_F12
, KF_MVWS_22
, NULL
);
5419 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
5420 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
5421 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
5422 setkeybinding(MODKEY
|ShiftMask
, XK_x
, KF_WIND_KILL
, NULL
);
5423 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
5424 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
5425 setkeybinding(MODKEY
|ShiftMask
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
5426 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
5427 setkeybinding(MODKEY
|ShiftMask
, XK_v
, KF_VERSION
, NULL
);
5428 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
5429 setkeybinding(MODKEY
|ShiftMask
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
5430 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
5431 setkeybinding(MODKEY
|ShiftMask
, XK_w
, KF_UNICONIFY
, NULL
);
5432 setkeybinding(MODKEY
|ShiftMask
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
5433 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
5434 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
5435 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
5436 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
5437 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
5438 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
5439 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
5440 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
5441 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
5442 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
5443 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
5444 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
5446 setkeybinding(MODKEY
|ShiftMask
, XK_d
, KF_DUMPWINS
, NULL
);
5455 while (RB_EMPTY(&keys
) == 0) {
5456 kp
= RB_ROOT(&keys
);
5462 setkeymapping(char *selector
, char *value
, int flags
)
5464 char keymapping_file
[PATH_MAX
];
5465 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5466 if (value
[0] == '~')
5467 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5468 pwd
->pw_dir
, &value
[1]);
5470 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5472 /* load new key bindings; if it fails, revert to default bindings */
5473 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5477 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5482 updatenumlockmask(void)
5485 xcb_get_modifier_mapping_reply_t
*modmap_r
;
5486 xcb_keycode_t
*modmap
, kc
;
5488 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
5491 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
5492 xcb_get_modifier_mapping(conn
),
5495 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
5496 for (i
= 0; i
< 8; i
++) {
5497 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
5498 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
5501 if (kc
== *((xcb_keycode_t
*)xcb_key_symbols_get_keycode(syms
,
5503 numlockmask
= (1 << i
);
5515 xcb_keycode_t
*code
;
5516 unsigned int modifiers
[] =
5517 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
5520 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5521 updatenumlockmask();
5523 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
5524 for (k
= 0; k
< num_screens
; k
++) {
5525 if (TAILQ_EMPTY(&screens
[k
].rl
))
5527 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
5529 RB_FOREACH(kp
, key_tree
, &keys
) {
5530 if ((code
= xcb_key_symbols_get_keycode(syms
,
5532 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5533 xcb_grab_key(conn
, True
,
5535 kp
->mod
| modifiers
[j
],
5536 *code
, XCB_GRAB_MODE_ASYNC
,
5537 XCB_GRAB_MODE_ASYNC
);
5543 grabbuttons(struct ws_win
*win
, int focused
)
5546 unsigned int modifiers
[] =
5547 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5549 updatenumlockmask();
5550 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, win
->id
,
5551 XCB_BUTTON_MASK_ANY
);
5553 for (i
= 0; i
< LENGTH(buttons
); i
++)
5554 if (buttons
[i
].action
== client_click
)
5555 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5556 xcb_grab_button(conn
, False
, win
->id
,
5558 XCB_GRAB_MODE_ASYNC
,
5565 xcb_grab_button(conn
, False
, win
->id
, BUTTONMASK
,
5566 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_SYNC
, XCB_WINDOW_NONE
,
5567 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_ANY
, XCB_BUTTON_MASK_ANY
);
5570 const char *quirkname
[] = {
5571 "NONE", /* config string for "no value" */
5580 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5581 #define SWM_Q_WS "\n|+ \t"
5583 parsequirks(char *qstr
, unsigned long *quirk
)
5593 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5595 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5596 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5597 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5598 DNPRINTF(SWM_D_QUIRK
,
5599 "parsequirks: %s\n", name
);
5604 *quirk
|= 1 << (i
-1);
5608 if (i
>= LENGTH(quirkname
)) {
5609 DNPRINTF(SWM_D_QUIRK
,
5610 "parsequirks: invalid quirk [%s]\n", name
);
5618 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5622 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5625 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5626 err(1, "quirk_insert: malloc");
5627 if ((qp
->class = strdup(class)) == NULL
)
5628 err(1, "quirk_insert: strdup");
5629 if ((qp
->name
= strdup(name
)) == NULL
)
5630 err(1, "quirk_insert: strdup");
5633 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5635 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5639 quirk_remove(struct quirk
*qp
)
5641 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5642 qp
->name
, qp
->quirk
);
5644 TAILQ_REMOVE(&quirks
, qp
, entry
);
5649 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5653 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5654 unsigned long quirk
)
5656 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5657 qp
->name
, qp
->quirk
);
5660 quirk_insert(class, name
, quirk
);
5662 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5666 setquirk(const char *class, const char *name
, unsigned long quirk
)
5670 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5673 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5674 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5678 quirk_replace(qp
, class, name
, quirk
);
5679 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5684 warnx("error: setquirk: cannot find class/name combination");
5688 quirk_insert(class, name
, quirk
);
5689 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5693 setconfquirk(char *selector
, char *value
, int flags
)
5695 char *cp
, *class, *name
;
5697 unsigned long quirks
;
5698 if (selector
== NULL
)
5700 if ((cp
= strchr(selector
, ':')) == NULL
)
5705 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5706 setquirk(class, name
, quirks
);
5713 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5714 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5715 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5716 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5717 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5718 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5719 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5720 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5721 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5722 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5723 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5724 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5725 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5728 /* conf file stuff */
5729 #define SWM_CONF_FILE "spectrwm.conf"
5730 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5734 SWM_S_BAR_AT_BOTTOM
,
5735 SWM_S_BAR_BORDER_WIDTH
,
5742 SWM_S_CLOCK_ENABLED
,
5745 SWM_S_CYCLE_VISIBLE
,
5747 SWM_S_DISABLE_BORDER
,
5749 SWM_S_FOCUS_CLOSE_WRAP
,
5750 SWM_S_FOCUS_DEFAULT
,
5756 SWM_S_STACK_ENABLED
,
5758 SWM_S_TITLE_CLASS_ENABLED
,
5759 SWM_S_TITLE_NAME_ENABLED
,
5760 SWM_S_URGENT_ENABLED
,
5761 SWM_S_VERBOSE_LAYOUT
,
5762 SWM_S_WINDOW_NAME_ENABLED
,
5763 SWM_S_WORKSPACE_LIMIT
5767 setconfvalue(char *selector
, char *value
, int flags
)
5773 case SWM_S_BAR_ACTION
:
5775 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5776 err(1, "setconfvalue: bar_action");
5778 case SWM_S_BAR_AT_BOTTOM
:
5779 bar_at_bottom
= atoi(value
);
5781 case SWM_S_BAR_BORDER_WIDTH
:
5782 bar_border_width
= atoi(value
);
5783 if (bar_border_width
< 0)
5784 bar_border_width
= 0;
5786 case SWM_S_BAR_DELAY
:
5787 bar_delay
= atoi(value
);
5789 case SWM_S_BAR_ENABLED
:
5790 bar_enabled
= atoi(value
);
5792 case SWM_S_BAR_FONT
:
5794 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5795 err(1, "setconfvalue: asprintf: failed to allocate "
5796 "memory for bar_fonts.");
5799 case SWM_S_BAR_FORMAT
:
5801 if ((bar_format
= strdup(value
)) == NULL
)
5802 err(1, "setconfvalue: bar_format");
5804 case SWM_S_BAR_JUSTIFY
:
5805 if (!strcmp(value
, "left"))
5806 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5807 else if (!strcmp(value
, "center"))
5808 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5809 else if (!strcmp(value
, "right"))
5810 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5812 errx(1, "invalid bar_justify");
5814 case SWM_S_BORDER_WIDTH
:
5815 border_width
= atoi(value
);
5816 if (border_width
< 0)
5819 case SWM_S_CLOCK_ENABLED
:
5820 clock_enabled
= atoi(value
);
5822 case SWM_S_CLOCK_FORMAT
:
5823 #ifndef SWM_DENY_CLOCK_FORMAT
5825 if ((clock_format
= strdup(value
)) == NULL
)
5826 err(1, "setconfvalue: clock_format");
5829 case SWM_S_CYCLE_EMPTY
:
5830 cycle_empty
= atoi(value
);
5832 case SWM_S_CYCLE_VISIBLE
:
5833 cycle_visible
= atoi(value
);
5835 case SWM_S_DIALOG_RATIO
:
5836 dialog_ratio
= atof(value
);
5837 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5840 case SWM_S_DISABLE_BORDER
:
5841 disable_border
= atoi(value
);
5843 case SWM_S_FOCUS_CLOSE
:
5844 if (!strcmp(value
, "first"))
5845 focus_close
= SWM_STACK_BOTTOM
;
5846 else if (!strcmp(value
, "last"))
5847 focus_close
= SWM_STACK_TOP
;
5848 else if (!strcmp(value
, "next"))
5849 focus_close
= SWM_STACK_ABOVE
;
5850 else if (!strcmp(value
, "previous"))
5851 focus_close
= SWM_STACK_BELOW
;
5853 errx(1, "focus_close");
5855 case SWM_S_FOCUS_CLOSE_WRAP
:
5856 focus_close_wrap
= atoi(value
);
5858 case SWM_S_FOCUS_DEFAULT
:
5859 if (!strcmp(value
, "last"))
5860 focus_default
= SWM_STACK_TOP
;
5861 else if (!strcmp(value
, "first"))
5862 focus_default
= SWM_STACK_BOTTOM
;
5864 errx(1, "focus_default");
5866 case SWM_S_FOCUS_MODE
:
5867 if (!strcmp(value
, "default"))
5868 focus_mode
= SWM_FOCUS_DEFAULT
;
5869 else if (!strcmp(value
, "follow_cursor"))
5870 focus_mode
= SWM_FOCUS_FOLLOW
;
5871 else if (!strcmp(value
, "synergy"))
5872 focus_mode
= SWM_FOCUS_SYNERGY
;
5874 errx(1, "focus_mode");
5876 case SWM_S_SPAWN_ORDER
:
5877 if (!strcmp(value
, "first"))
5878 spawn_position
= SWM_STACK_BOTTOM
;
5879 else if (!strcmp(value
, "last"))
5880 spawn_position
= SWM_STACK_TOP
;
5881 else if (!strcmp(value
, "next"))
5882 spawn_position
= SWM_STACK_ABOVE
;
5883 else if (!strcmp(value
, "previous"))
5884 spawn_position
= SWM_STACK_BELOW
;
5886 errx(1, "spawn_position");
5888 case SWM_S_SPAWN_TERM
:
5889 setconfspawn("term", value
, 0);
5890 setconfspawn("spawn_term", value
, 0);
5894 case SWM_S_SS_ENABLED
:
5895 ss_enabled
= atoi(value
);
5897 case SWM_S_STACK_ENABLED
:
5898 stack_enabled
= atoi(value
);
5900 case SWM_S_TERM_WIDTH
:
5901 term_width
= atoi(value
);
5905 case SWM_S_TITLE_CLASS_ENABLED
:
5906 title_class_enabled
= atoi(value
);
5908 case SWM_S_TITLE_NAME_ENABLED
:
5909 title_name_enabled
= atoi(value
);
5911 case SWM_S_URGENT_ENABLED
:
5912 urgent_enabled
= atoi(value
);
5914 case SWM_S_VERBOSE_LAYOUT
:
5915 verbose_layout
= atoi(value
);
5916 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5918 layouts
[i
].l_string
= fancy_stacker
;
5920 layouts
[i
].l_string
= plain_stacker
;
5923 case SWM_S_WINDOW_NAME_ENABLED
:
5924 window_name_enabled
= atoi(value
);
5926 case SWM_S_WORKSPACE_LIMIT
:
5927 workspace_limit
= atoi(value
);
5928 if (workspace_limit
> SWM_WS_MAX
)
5929 workspace_limit
= SWM_WS_MAX
;
5930 else if (workspace_limit
< 1)
5931 workspace_limit
= 1;
5940 setconfmodkey(char *selector
, char *value
, int flags
)
5942 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5943 update_modkey(Mod1Mask
);
5944 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5945 update_modkey(Mod2Mask
);
5946 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5947 update_modkey(Mod3Mask
);
5948 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5949 update_modkey(Mod4Mask
);
5956 setconfcolor(char *selector
, char *value
, int flags
)
5958 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5963 setconfregion(char *selector
, char *value
, int flags
)
5965 custom_region(value
);
5970 setautorun(char *selector
, char *value
, int flags
)
5980 if (getenv("SWM_STARTED"))
5984 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5985 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5987 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5988 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5991 * This is a little intricate
5993 * If the pid already exists we simply reuse it because it means it was
5994 * used before AND not claimed by manage_window. We get away with
5995 * altering it in the parent after INSERT because this can not be a race
5998 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
6001 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
6003 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
6004 err(1, "setautorun: realloc");
6005 a
.argv
[argc
- 1] = ap
;
6008 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
6009 err(1, "setautorun: realloc");
6010 a
.argv
[argc
] = NULL
;
6012 if ((pid
= fork()) == 0) {
6013 spawn(ws_id
, &a
, 1);
6022 p
= calloc(1, sizeof *p
);
6025 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
6035 setlayout(char *selector
, char *value
, int flags
)
6037 int ws_id
, i
, x
, mg
, ma
, si
, raise
, f
= 0;
6038 int st
= SWM_V_STACK
, num_screens
;
6040 struct workspace
*ws
;
6042 if (getenv("SWM_STARTED"))
6046 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
6047 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
6048 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6049 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6052 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6053 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
6055 if (!strcasecmp(s
, "vertical"))
6057 else if (!strcasecmp(s
, "vertical_flip")) {
6060 } else if (!strcasecmp(s
, "horizontal"))
6062 else if (!strcasecmp(s
, "horizontal_flip")) {
6065 } else if (!strcasecmp(s
, "fullscreen"))
6068 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6069 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6072 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6073 for (i
= 0; i
< num_screens
; i
++) {
6074 ws
= (struct workspace
*)&screens
[i
].ws
;
6075 ws
[ws_id
].cur_layout
= &layouts
[st
];
6077 ws
[ws_id
].always_raise
= raise
;
6078 if (st
== SWM_MAX_STACK
)
6082 for (x
= 0; x
< abs(mg
); x
++) {
6083 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6084 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
6085 SWM_ARG_ID_MASTERSHRINK
);
6089 for (x
= 0; x
< abs(ma
); x
++) {
6090 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6091 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
6092 SWM_ARG_ID_MASTERDEL
);
6096 for (x
= 0; x
< abs(si
); x
++) {
6097 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6098 si
>= 0 ? SWM_ARG_ID_STACKINC
:
6099 SWM_ARG_ID_STACKDEC
);
6104 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6105 SWM_ARG_ID_FLIPLAYOUT
);
6113 /* config options */
6114 struct config_option
{
6116 int (*func
)(char*, char*, int);
6119 struct config_option configopt
[] = {
6120 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
6121 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
6122 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
6123 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
6124 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
6125 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
6126 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
6127 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
6128 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
6129 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
6130 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
6131 { "keyboard_mapping", setkeymapping
, 0 },
6132 { "bind", setconfbinding
, 0 },
6133 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
6134 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
6135 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
6136 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
6137 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
6138 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
6139 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
6140 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
6141 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
6142 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
6143 { "modkey", setconfmodkey
, 0 },
6144 { "program", setconfspawn
, 0 },
6145 { "quirk", setconfquirk
, 0 },
6146 { "region", setconfregion
, 0 },
6147 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
6148 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
6149 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
6150 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
6151 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
6152 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
6153 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6154 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6155 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6156 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6157 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6158 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6159 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6160 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6161 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6162 { "autorun", setautorun
, 0 },
6163 { "layout", setlayout
, 0 },
6168 conf_load(char *filename
, int keymapping
)
6171 char *line
, *cp
, *optsub
, *optval
;
6172 size_t linelen
, lineno
= 0;
6173 int wordlen
, i
, optind
;
6174 struct config_option
*opt
;
6176 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6178 if (filename
== NULL
) {
6179 warnx("conf_load: no filename");
6182 if ((config
= fopen(filename
, "r")) == NULL
) {
6183 warn("conf_load: fopen: %s", filename
);
6187 while (!feof(config
)) {
6188 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6191 err(1, "%s", filename
);
6196 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6197 if (cp
[0] == '\0') {
6202 /* get config option */
6203 wordlen
= strcspn(cp
, "=[ \t\n");
6205 warnx("%s: line %zd: no option found",
6210 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6211 opt
= &configopt
[i
];
6212 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6213 (int)strlen(opt
->optname
) == wordlen
) {
6219 warnx("%s: line %zd: unknown option %.*s",
6220 filename
, lineno
, wordlen
, cp
);
6223 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
6224 warnx("%s: line %zd: invalid option %.*s",
6225 filename
, lineno
, wordlen
, cp
);
6229 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6230 /* get [selector] if any */
6234 wordlen
= strcspn(cp
, "]");
6237 warnx("%s: line %zd: syntax error",
6242 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6244 warnx("%s: line %zd: unable to allocate"
6245 "memory for selector", filename
,
6251 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6253 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6255 optval
= strdup(cp
);
6256 /* call function to deal with it all */
6257 if (configopt
[optind
].func(optsub
, optval
,
6258 configopt
[optind
].funcflags
) != 0)
6259 errx(1, "%s: line %zd: invalid data for %s",
6260 filename
, lineno
, configopt
[optind
].optname
);
6267 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6274 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6280 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
6282 struct ws_win
*parent
, *w
;
6283 struct swm_region
*r
;
6284 struct workspace
*ws
;
6285 xcb_icccm_wm_hints_t wmh
;
6287 parent
= find_window(win
->transient
);
6289 parent
->child_trans
= win
;
6291 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6292 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
6294 r
= root_to_region(win
->wa
->root
);
6296 /* parent doen't exist in our window list */
6297 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6298 if (xcb_icccm_get_wm_hints_reply(conn
,
6299 xcb_icccm_get_wm_hints(conn
, w
->id
),
6301 warnx("can't get hints for 0x%x", w
->id
);
6305 if (win
->hints
.window_group
!= wmh
.window_group
)
6308 w
->child_trans
= win
;
6309 win
->transient
= w
->id
;
6311 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
6312 "transient to 0x%x\n", win
->transient
);
6319 window_get_pid(xcb_window_t win
)
6324 xcb_get_property_cookie_t pc
;
6325 xcb_get_property_reply_t
*pr
;
6327 apid
= get_atom_from_string("_NET_WM_PID");
6328 if (apid
== XCB_ATOM_NONE
)
6331 pc
= xcb_get_property(conn
, False
, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
6332 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6335 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
6340 ret
= *((pid_t
*)xcb_get_property_value(pr
));
6346 apid
= get_atom_from_string("_SWM_PID");
6347 pc
= xcb_get_property(conn
, False
, win
, apid
, XCB_ATOM_STRING
,
6349 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6352 if (pr
->type
!= XCB_ATOM_STRING
) {
6357 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
6364 manage_window(xcb_window_t id
)
6366 xcb_window_t trans
= XCB_WINDOW_NONE
;
6367 struct workspace
*ws
;
6368 struct ws_win
*win
, *ww
;
6369 int ws_idx
, border_me
= 0;
6370 xcb_atom_t ws_idx_atom
= XCB_ATOM_NONE
;
6371 char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
6373 struct swm_region
*r
;
6377 uint32_t event_mask
, i
;
6379 xcb_get_property_reply_t
*gpr
;
6380 xcb_icccm_get_wm_protocols_reply_t wpr
;
6382 if ((win
= find_window(id
)) != NULL
)
6383 return (win
); /* already being managed */
6385 /* see if we are on the unmanaged list */
6386 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6387 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
6388 "window: 0x%x\n", win
->id
);
6389 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6391 set_child_transient(win
, &trans
);
6393 if (trans
&& (ww
= find_window(trans
)))
6394 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
6395 else if ((ww
= win
->ws
->focus
) &&
6396 spawn_position
== SWM_STACK_ABOVE
)
6397 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
,
6399 else if (ww
&& spawn_position
== SWM_STACK_BELOW
)
6400 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6401 else switch (spawn_position
) {
6404 case SWM_STACK_ABOVE
:
6405 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6407 case SWM_STACK_BOTTOM
:
6408 case SWM_STACK_BELOW
:
6409 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6412 ewmh_update_actions(win
);
6416 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6417 err(1, "manage_window: calloc: failed to allocate memory for "
6423 /* see if we need to override the workspace */
6424 p
= find_pid(window_get_pid(id
));
6426 /* Get all the window data in one shot */
6427 ws_idx_atom
= get_atom_from_string("_SWM_WS");
6429 gpr
= xcb_get_property_reply(conn
,
6430 xcb_get_property(conn
, False
, id
, ws_idx_atom
,
6431 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
6434 proplen
= xcb_get_property_value_length(gpr
);
6436 prop
= malloc(proplen
+ 1);
6439 xcb_get_property_value(gpr
),
6441 prop
[proplen
] = '\0';
6447 win
->wa
= xcb_get_geometry_reply(conn
,
6448 xcb_get_geometry(conn
, id
),
6450 xcb_icccm_get_wm_normal_hints_reply(conn
,
6451 xcb_icccm_get_wm_normal_hints(conn
, id
),
6453 xcb_icccm_get_wm_hints_reply(conn
,
6454 xcb_icccm_get_wm_hints(conn
, id
),
6456 xcb_icccm_get_wm_transient_for_reply(conn
,
6457 xcb_icccm_get_wm_transient_for(conn
, id
),
6460 win
->transient
= trans
;
6461 set_child_transient(win
, &trans
);
6462 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%x, "
6463 "transient: 0x%x\n", win
->id
, win
->transient
);
6466 prot
= get_atom_from_string("WM_PROTOCOLS");
6468 /* get supported protocols */
6469 if (xcb_icccm_get_wm_protocols_reply(conn
,
6470 xcb_icccm_get_wm_protocols(conn
, id
, prot
),
6472 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
6473 if (wpr
.atoms
[i
] == a_takefocus
)
6474 win
->take_focus
= 1;
6475 if (wpr
.atoms
[i
] == a_delete
)
6476 win
->can_delete
= 1;
6478 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
6481 win
->iconic
= get_iconic(win
);
6484 * Figure out where to put the window. If it was previously assigned to
6485 * a workspace (either by spawn() or manually moving), and isn't
6486 * transient, * put it in the same workspace
6488 r
= root_to_region(win
->wa
->root
);
6490 ws
= &r
->s
->ws
[p
->ws
];
6491 TAILQ_REMOVE(&pidlist
, p
, entry
);
6494 } else if (prop
&& win
->transient
== 0) {
6495 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
6496 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1,
6499 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
6502 ws
= &r
->s
->ws
[ws_idx
];
6505 /* this should launch transients in the same ws as parent */
6507 if ((ww
= find_window(trans
)) != NULL
)
6513 warnx("manage_window: fix this "
6519 /* set up the window layout */
6522 win
->s
= r
->s
; /* this never changes */
6523 if (trans
&& (ww
= find_window(trans
)))
6524 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
6525 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
6526 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
6528 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
6529 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6530 else switch (spawn_position
) {
6533 case SWM_STACK_ABOVE
:
6534 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6536 case SWM_STACK_BOTTOM
:
6537 case SWM_STACK_BELOW
:
6538 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6541 /* ignore window border if there is one. */
6542 WIDTH(win
) = win
->wa
->width
;
6543 HEIGHT(win
) = win
->wa
->height
;
6544 X(win
) = win
->wa
->x
+ win
->wa
->border_width
;
6545 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
;
6547 win
->g_floatvalid
= 0;
6548 win
->floatmaxed
= 0;
6549 win
->ewmh_flags
= 0;
6551 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%x, (x,y) w x h: "
6552 "(%d,%d) %d x %d, ws: %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
6553 HEIGHT(win
), ws
->idx
);
6555 constrain_window(win
, r
, 0);
6557 /* Set window properties so we can remember this after reincarnation */
6558 if (ws_idx_atom
&& prop
== NULL
&&
6559 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
6561 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6563 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
6564 ws_idx_atom
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
6570 ewmh_autoquirk(win
);
6572 if (xcb_icccm_get_wm_class_reply(conn
,
6573 xcb_icccm_get_wm_class(conn
, win
->id
),
6575 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6576 win
->ch
.class_name
, win
->ch
.instance_name
);
6578 /* java is retarded so treat it special */
6579 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
6584 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6585 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
6586 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
6587 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
6588 "class: %s, name: %s\n", win
->ch
.class_name
,
6589 win
->ch
.instance_name
);
6590 if (qp
->quirk
& SWM_Q_FLOAT
) {
6594 win
->quirks
= qp
->quirk
;
6599 /* alter window position if quirky */
6600 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6601 win
->manual
= 1; /* don't center the quirky windows */
6602 if (bar_enabled
&& Y(win
) < bar_height
)
6603 Y(win
) = bar_height
;
6604 if (WIDTH(win
) + X(win
) > WIDTH(r
))
6605 X(win
) = WIDTH(r
) - WIDTH(win
) - 2;
6609 /* Reset font sizes (the bruteforce way; no default keybinding). */
6610 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6611 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6612 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
6613 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6614 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
6617 ewmh_get_win_state(win
);
6618 ewmh_update_actions(win
);
6619 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
6624 X(win
) -= border_width
;
6625 Y(win
) -= border_width
;
6629 event_mask
= XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
|
6630 XCB_EVENT_MASK_PROPERTY_CHANGE
| XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
6632 xcb_change_window_attributes(conn
, id
, XCB_CW_EVENT_MASK
, &event_mask
);
6634 /* floaters need to be mapped if they are in the current workspace */
6635 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
6636 map_window_raised(win
->id
);
6642 free_window(struct ws_win
*win
)
6644 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
6649 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6654 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
6659 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6662 DNPRINTF(SWM_D_MISC
, "free_window: done\n");
6666 unmanage_window(struct ws_win
*win
)
6668 struct ws_win
*parent
;
6669 xcb_screen_t
*screen
;
6674 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
6676 if (win
->transient
) {
6677 parent
= find_window(win
->transient
);
6679 parent
->child_trans
= NULL
;
6682 /* focus on root just in case */
6683 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
6684 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
6685 screen
->root
, XCB_CURRENT_TIME
);
6689 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
6690 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6694 focus_magic(struct ws_win
*win
)
6696 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%x\n", WINID(win
));
6699 /* if there are no windows clear the status-bar */
6704 if (win
->child_trans
) {
6705 /* win = parent & has a transient so focus on that */
6707 focus_win(win
->child_trans
);
6708 if (win
->child_trans
->take_focus
)
6709 client_msg(win
, a_takefocus
);
6711 /* make sure transient hasn't disappeared */
6712 if (validate_win(win
->child_trans
) == 0) {
6713 focus_win(win
->child_trans
);
6714 if (win
->child_trans
->take_focus
)
6715 client_msg(win
->child_trans
,
6718 win
->child_trans
= NULL
;
6720 if (win
->take_focus
)
6721 client_msg(win
, a_takefocus
);
6727 if (win
->take_focus
)
6728 client_msg(win
, a_takefocus
);
6733 expose(xcb_expose_event_t
*e
)
6736 struct swm_region
*r
;
6738 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
6740 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6741 for (i
= 0; i
< num_screens
; i
++)
6742 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6743 if (e
->window
== WINID(r
->bar
))
6750 keypress(xcb_key_press_event_t
*e
)
6752 xcb_keysym_t keysym
;
6755 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
6757 DNPRINTF(SWM_D_EVENT
, "keypress: %u %u\n", e
->detail
, keysym
);
6759 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
6762 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
6763 spawn_custom(root_to_region(e
->root
),
6764 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
6765 else if (keyfuncs
[kp
->funcid
].func
)
6766 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
),
6767 &(keyfuncs
[kp
->funcid
].args
));
6771 buttonpress(xcb_button_press_event_t
*e
)
6775 unsigned int action
;
6777 DNPRINTF(SWM_D_EVENT
, "buttonpress: window 0x%x, detail: %u\n",
6778 e
->event
, e
->detail
);
6780 if ((win
= find_window(e
->event
)) == NULL
)
6784 action
= client_click
;
6786 for (i
= 0; i
< LENGTH(buttons
); i
++)
6787 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6788 buttons
[i
].button
== e
->detail
&&
6789 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
))
6790 buttons
[i
].func(win
, &buttons
[i
].args
);
6797 print_win_geom(xcb_window_t w
)
6799 xcb_get_geometry_reply_t
*wa
;
6802 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
6804 DNPRINTF(SWM_D_MISC
, "print_win_geom: window: 0x%x, root: 0x%x, "
6805 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
6806 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
6814 configurerequest(xcb_configure_request_event_t
*e
)
6819 uint32_t wc
[7] = {0};
6821 if ((win
= find_window(e
->window
)) == NULL
)
6822 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
6825 print_win_geom(e
->window
);
6828 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: 0x%x, "
6829 "value_mask: 0x%x", e
->window
, e
->value_mask
);
6830 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
6831 mask
|= XCB_CONFIG_WINDOW_X
;
6833 DPRINTF(", X: %d", e
->x
);
6835 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
6836 mask
|= XCB_CONFIG_WINDOW_Y
;
6838 DPRINTF(", Y: %d", e
->y
);
6840 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
6841 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
6843 DPRINTF(", W: %u", e
->width
);
6845 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
6846 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
6847 wc
[i
++] = e
->height
;
6848 DPRINTF(", H: %u", e
->height
);
6850 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
6851 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6852 wc
[i
++] = e
->border_width
;
6853 DPRINTF(", Border: %u", e
->border_width
);
6855 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
6856 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
6857 wc
[i
++] = e
->sibling
;
6858 DPRINTF(", Sibling: 0x%x", e
->sibling
);
6860 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
6861 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
6862 wc
[i
++] = e
->stack_mode
;
6863 DPRINTF(", StackMode: %u", e
->stack_mode
);
6867 xcb_configure_window(conn
, e
->window
, mask
, wc
);
6869 DPRINTF(", Sent: %s\n", YESNO((mask
!= 0)));
6870 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
6871 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
6872 win
->g_float
.x
= e
->x
- X(win
->ws
->r
);
6873 win
->g_float
.y
= e
->y
- Y(win
->ws
->r
);
6874 win
->g_float
.w
= e
->width
;
6875 win
->g_float
.h
= e
->height
;
6876 win
->g_floatvalid
= 1;
6878 if (win
->floating
) {
6879 win
->g
= win
->g_float
;
6880 win
->g
.x
+= X(win
->ws
->r
);
6881 win
->g
.y
+= Y(win
->ws
->r
);
6894 configurenotify(xcb_configure_notify_event_t
*e
)
6898 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%x\n",
6901 win
= find_window(e
->window
);
6903 xcb_icccm_get_wm_normal_hints_reply(conn
,
6904 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
6913 destroynotify(xcb_destroy_notify_event_t
*e
)
6917 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
6919 if ((win
= find_window(e
->window
)) == NULL
) {
6920 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
6926 /* make sure we focus on something */
6929 unmanage_window(win
);
6935 enternotify(xcb_enter_notify_event_t
*e
)
6938 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%x, mode: %d, detail: "
6939 "%d, root: 0x%x, subwindow: 0x%x, same_screen_focus: %s, "
6940 "state: %d\n", e
->event
, e
->mode
, e
->detail
, e
->root
,
6941 e
->child
, YESNO(e
->same_screen_focus
), e
->state
);
6943 if (e
->mode
!= XCB_NOTIFY_MODE_NORMAL
) {
6944 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6949 switch (focus_mode
) {
6950 case SWM_FOCUS_DEFAULT
:
6952 case SWM_FOCUS_FOLLOW
:
6954 case SWM_FOCUS_SYNERGY
:
6958 if ((win
= find_window(e
->event
)) == NULL
) {
6959 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6968 /* lets us use one switch statement for arbitrary mode/detail combinations */
6969 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6972 mapnotify(xcb_map_notify_event_t
*e
)
6976 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
6978 win
= manage_window(e
->window
);
6980 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
6983 * focus_win can only set input focus on a mapped window.
6984 * make sure the window really has focus since it is just being mapped.
6986 if (win
->ws
->focus
== win
)
6991 mappingnotify(xcb_mapping_notify_event_t
*e
)
6993 xcb_refresh_keyboard_mapping(syms
, e
);
6995 if (e
->request
== XCB_MAPPING_KEYBOARD
)
7000 maprequest(xcb_map_request_event_t
*e
)
7003 struct swm_region
*r
;
7004 xcb_get_window_attributes_reply_t
*war
;
7006 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%x\n",
7009 war
= xcb_get_window_attributes_reply(conn
,
7010 xcb_get_window_attributes(conn
, e
->window
),
7014 if (war
->override_redirect
) {
7020 win
= manage_window(e
->window
);
7022 return; /* can't happen */
7027 /* make new win focused */
7028 r
= root_to_region(win
->wa
->root
);
7029 if (win
->ws
== r
->ws
)
7035 get_atom_name(xcb_atom_t atom
)
7039 xcb_get_atom_name_reply_t
*r
;
7040 xcb_map_request_event_t mre
;
7042 r
= xcb_get_atom_name_reply(conn
,
7043 xcb_get_atom_name(conn
, atom
),
7046 len
= xcb_get_atom_name_name_length(r
);
7048 name
= malloc(len
+ 1);
7050 memcpy(name
, xcb_get_atom_name_name(r
), len
);
7064 propertynotify(xcb_property_notify_event_t
*e
)
7070 name
= get_atom_name(e
->atom
);
7071 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%x, atom: %s(%u)\n",
7072 e
->window
, name
, e
->atom
);
7076 win
= find_window(e
->window
);
7080 if (e
->state
== XCB_PROPERTY_DELETE
&& e
->atom
== a_swm_iconic
) {
7081 update_iconic(win
, 0);
7082 map_window_raised(win
->id
);
7089 case XCB_ATOM_WM_CLASS
:
7090 case XCB_ATOM_WM_NAME
:
7099 unmapnotify(xcb_unmap_notify_event_t
*e
)
7103 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
7105 /* determine if we need to help unmanage this window */
7106 win
= find_window(e
->window
);
7110 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
) {
7111 unmanage_window(win
);
7114 /* resend unmap because we ated it */
7115 xcb_unmap_window(conn
, e
->window
);
7121 visibilitynotify(xcb_visibility_notify_event_t *e)
7123 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%x\n",
7128 clientmessage(xcb_client_message_event_t
*e
)
7131 xcb_map_request_event_t mre
;
7135 name
= get_atom_name(e
->type
);
7136 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7137 e
->window
, name
, e
->type
);
7140 win
= find_window(e
->window
);
7143 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7144 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
7145 "unmanaged window.\n");
7146 mre
.window
= e
->window
;
7152 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7153 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7156 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7157 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7158 if (win
->can_delete
)
7159 client_msg(win
, a_delete
);
7161 xcb_kill_client(conn
, win
->id
);
7163 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7164 DNPRINTF(SWM_D_EVENT
,
7165 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7166 if (win
->floating
) {
7167 if (e
->data
.data32
[0] & (1<<8)) /* x */
7168 X(win
) = e
->data
.data32
[1];
7169 if (e
->data
.data32
[0] & (1<<9)) /* y */
7170 Y(win
) = e
->data
.data32
[2];
7171 if (e
->data
.data32
[0] & (1<<10)) /* width */
7172 WIDTH(win
) = e
->data
.data32
[3];
7173 if (e
->data
.data32
[0] & (1<<11)) /* height */
7174 HEIGHT(win
) = e
->data
.data32
[4];
7179 /* TODO: Change stack sizes */
7180 /* notify no change was made. */
7181 config_win(win
, NULL
);
7184 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
7185 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
7186 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
7187 if (e
->data
.data32
[2])
7188 ewmh_update_win_state(win
, e
->data
.data32
[2],
7201 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
;
7203 xcb_void_cookie_t wac
;
7204 xcb_generic_error_t
*error
;
7206 /* this causes an error if some other window manager is running */
7207 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7208 for (i
= 0; i
< num_screens
; i
++) {
7210 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: 0x%x\n",
7212 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
7213 XCB_CW_EVENT_MASK
, &val
);
7214 if ((error
= xcb_request_check(conn
, wac
))) {
7215 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
7226 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
7228 struct swm_region
*r
, *n
;
7229 struct workspace
*ws
= NULL
;
7232 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
7233 s
->idx
, w
, h
, x
, y
);
7235 /* remove any conflicting regions */
7236 n
= TAILQ_FIRST(&s
->rl
);
7239 n
= TAILQ_NEXT(r
, entry
);
7240 if (X(r
) < (x
+ w
) &&
7241 (X(r
) + WIDTH(r
)) > x
&&
7243 (Y(r
) + HEIGHT(r
)) > y
) {
7244 if (r
->ws
->r
!= NULL
)
7245 r
->ws
->old_r
= r
->ws
->r
;
7248 TAILQ_REMOVE(&s
->rl
, r
, entry
);
7249 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
7253 /* search old regions for one to reuse */
7255 /* size + location match */
7256 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7257 if (X(r
) == x
&& Y(r
) == y
&&
7258 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7262 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7263 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7267 TAILQ_REMOVE(&s
->orl
, r
, entry
);
7268 /* try to use old region's workspace */
7269 if (r
->ws
->r
== NULL
)
7272 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
7273 err(1, "new_region: calloc: failed to allocate memory "
7276 /* if we don't have a workspace already, find one */
7278 for (i
= 0; i
< workspace_limit
; i
++)
7279 if (s
->ws
[i
].r
== NULL
) {
7286 errx(1, "new_region: no free workspaces");
7297 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
7303 #ifdef SWM_XRR_HAS_CRTC
7306 #endif /* SWM_XRR_HAS_CRTC */
7307 struct swm_region
*r
;
7309 xcb_randr_get_screen_resources_current_cookie_t src
;
7310 xcb_randr_get_screen_resources_current_reply_t
*srr
;
7311 xcb_randr_get_crtc_info_cookie_t cic
;
7312 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
7313 xcb_randr_crtc_t
*crtc
;
7314 xcb_screen_t
*screen
= get_screen(i
);
7316 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7317 if (i
>= num_screens
)
7318 errx(1, "scan_xrandr: invalid screen");
7320 /* remove any old regions */
7321 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
7322 r
->ws
->old_r
= r
->ws
->r
= NULL
;
7324 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
7325 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
7329 /* map virtual screens onto physical screens */
7330 #ifdef SWM_XRR_HAS_CRTC
7331 if (xrandr_support
) {
7332 src
= xcb_randr_get_screen_resources_current(conn
,
7334 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
7337 new_region(&screens
[i
], 0, 0,
7338 screen
->width_in_pixels
,
7339 screen
->height_in_pixels
);
7342 ncrtc
= srr
->num_crtcs
;
7343 for (c
= 0; c
< ncrtc
; c
++) {
7344 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
7345 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
7347 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
7350 if (cir
->num_outputs
== 0) {
7356 new_region(&screens
[i
], 0, 0,
7357 screen
->width_in_pixels
,
7358 screen
->height_in_pixels
);
7360 new_region(&screens
[i
],
7361 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
7366 #endif /* SWM_XRR_HAS_CRTC */
7368 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
7369 screen
->height_in_pixels
);
7374 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
7376 struct swm_region
*r
;
7379 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
7381 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7382 /* silly event doesn't include the screen index */
7383 for (i
= 0; i
< num_screens
; i
++)
7384 if (screens
[i
].root
== e
->root
)
7386 if (i
>= num_screens
)
7387 errx(1, "screenchange: screen not found");
7389 /* brute force for now, just re-enumerate the regions */
7393 print_win_geom(e
->root
);
7395 /* add bars to all regions */
7396 for (i
= 0; i
< num_screens
; i
++)
7397 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7405 xcb_window_t
*wins
= NULL
;
7407 int i
, j
, num_screens
;
7408 uint16_t state
, manage
;
7410 xcb_query_tree_cookie_t qtc
;
7411 xcb_query_tree_reply_t
*qtr
;
7412 xcb_get_window_attributes_cookie_t c
;
7413 xcb_get_window_attributes_reply_t
*r
;
7414 xcb_get_property_cookie_t pc
;
7416 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7417 for (i
= 0; i
< num_screens
; i
++) {
7418 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
7419 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
7422 wins
= xcb_query_tree_children(qtr
);
7423 no
= xcb_query_tree_children_length(qtr
);
7424 /* attach windows to a region */
7425 /* normal windows */
7426 for (j
= 0; j
< no
; j
++) {
7427 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7428 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7431 if (r
->override_redirect
) {
7436 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7437 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7443 state
= getstate(wins
[j
]);
7444 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7445 if (r
->map_state
== XCB_MAP_STATE_VIEWABLE
|| manage
)
7446 manage_window(wins
[j
]);
7449 /* transient windows */
7450 for (j
= 0; j
< no
; j
++) {
7451 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7452 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7455 if (r
->override_redirect
) {
7461 state
= getstate(wins
[j
]);
7462 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7463 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7464 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7465 &wins
[j
], NULL
) && manage
)
7466 manage_window(wins
[j
]);
7475 int i
, j
, k
, num_screens
;
7476 struct workspace
*ws
;
7477 uint32_t gcv
[1], wa
[1];
7478 const xcb_query_extension_reply_t
*qep
;
7479 xcb_cursor_t cursor
;
7480 xcb_font_t cursor_font
;
7481 xcb_randr_query_version_cookie_t c
;
7482 xcb_randr_query_version_reply_t
*r
;
7484 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7485 if ((screens
= calloc(num_screens
,
7486 sizeof(struct swm_screen
))) == NULL
)
7487 err(1, "setup_screens: calloc: failed to allocate memory for "
7490 /* initial Xrandr setup */
7491 xrandr_support
= False
;
7492 c
= xcb_randr_query_version(conn
, True
, True
);
7493 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
7495 if (r
->major_version
>= 1)
7496 xrandr_support
= True
;
7499 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
7500 xrandr_eventbase
= qep
->first_event
;
7502 cursor_font
= xcb_generate_id(conn
);
7503 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
7505 cursor
= xcb_generate_id(conn
);
7506 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
7507 XC_left_ptr
, XC_left_ptr
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7510 /* map physical screens */
7511 for (i
= 0; i
< num_screens
; i
++) {
7512 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
7514 TAILQ_INIT(&screens
[i
].rl
);
7515 TAILQ_INIT(&screens
[i
].orl
);
7516 screens
[i
].root
= get_screen(i
)->root
;
7518 /* set default colors */
7519 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
7520 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
7521 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
7522 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
7523 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
7525 /* create graphics context on screen */
7526 screens
[i
].bar_gc
= xcb_generate_id(conn
);
7528 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
7529 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
7531 /* set default cursor */
7532 xcb_change_window_attributes(conn
, screens
[i
].root
,
7535 /* init all workspaces */
7536 /* XXX these should be dynamically allocated too */
7537 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
7538 ws
= &screens
[i
].ws
[j
];
7544 TAILQ_INIT(&ws
->winlist
);
7545 TAILQ_INIT(&ws
->unmanagedlist
);
7547 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
7548 if (layouts
[k
].l_config
!= NULL
)
7549 layouts
[k
].l_config(ws
,
7550 SWM_ARG_ID_STACKINIT
);
7551 ws
->cur_layout
= &layouts
[0];
7552 ws
->cur_layout
->l_string(ws
);
7558 xcb_randr_select_input(conn
, screens
[i
].root
,
7559 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
7561 xcb_free_cursor(conn
, cursor
);
7562 xcb_close_font(conn
, cursor_font
);
7568 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
7569 err(1, "setup_globals: strdup: failed to allocate memory.");
7571 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
7572 err(1, "setup_globals: strdup: failed to allocate memory.");
7574 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
7575 errx(1, "unable to allocate key symbols");
7577 a_state
= get_atom_from_string("WM_STATE");
7578 a_prot
= get_atom_from_string("WM_PROTOCOLS");
7579 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
7580 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
7581 a_wmname
= get_atom_from_string("WM_NAME");
7582 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
7583 a_utf8_string
= get_atom_from_string("UTF8_STRING");
7584 a_string
= get_atom_from_string("STRING");
7585 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
7592 xcb_atom_t netwmcheck
, netwmname
, utf8_string
;
7593 xcb_window_t root
, win
;
7596 /* work around sun jdk bugs, code from wmname */
7597 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7598 netwmname
= get_atom_from_string("_NET_WM_NAME");
7599 utf8_string
= get_atom_from_string("UTF8_STRING");
7601 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7602 for (i
= 0; i
< num_screens
; i
++) {
7603 root
= screens
[i
].root
;
7605 win
= xcb_generate_id(conn
);
7606 wa
[0] = screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
;
7607 wa
[1] = screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
;
7608 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, 0, 0, 0, 1,
7609 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
7610 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
7612 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
7613 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7614 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7615 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7616 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7617 netwmname
, utf8_string
, 8, strlen("LG3D"), "LG3D");
7622 event_error(xcb_generic_error_t
*e
)
7624 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
7625 "resource_id: %u, minor_code: %u\n",
7626 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
7627 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
7628 e
->sequence
, e
->resource_id
, e
->minor_code
);
7632 event_handle(xcb_generic_event_t
*evt
)
7634 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
7636 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d)\n",
7637 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
7638 XCB_EVENT_RESPONSE_TYPE(evt
));
7641 #define EVENT(type, callback) case type: callback((void *)evt); return
7642 EVENT(0, event_error
);
7643 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
7644 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7645 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
7646 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
7647 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
7648 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
7649 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
7650 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
7651 /*EVENT(XCB_CREATE_NOTIFY, );*/
7652 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
7653 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
7654 EVENT(XCB_EXPOSE
, expose
);
7655 /*EVENT(XCB_FOCUS_IN, );*/
7656 /*EVENT(XCB_FOCUS_OUT, );*/
7657 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
7658 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
7659 EVENT(XCB_KEY_PRESS
, keypress
);
7660 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7661 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
7662 /*EVENT(XCB_LEAVE_NOTIFY, );*/
7663 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
7664 EVENT(XCB_MAP_REQUEST
, maprequest
);
7665 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
7666 /*EVENT(XCB_MOTION_NOTIFY, );*/
7667 /*EVENT(XCB_NO_EXPOSURE, );*/
7668 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
7669 /*EVENT(XCB_REPARENT_NOTIFY, );*/
7670 /*EVENT(XCB_RESIZE_REQUEST, );*/
7671 /*EVENT(XCB_SELECTION_CLEAR, );*/
7672 /*EVENT(XCB_SELECTION_NOTIFY, );*/
7673 /*EVENT(XCB_SELECTION_REQUEST, );*/
7674 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
7675 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
7678 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
7679 screenchange((void *)evt
);
7683 main(int argc
, char *argv
[])
7685 struct swm_region
*r
, *rr
;
7686 struct ws_win
*winfocus
= NULL
;
7688 char conf
[PATH_MAX
], *cfile
= NULL
;
7690 int xfd
, i
, num_screens
;
7691 struct sigaction sact
;
7692 xcb_generic_event_t
*evt
;
7696 time_started
= time(NULL
);
7699 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
7700 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, ""))
7701 warnx("no locale support");
7703 /* handle some signals */
7704 bzero(&sact
, sizeof(sact
));
7705 sigemptyset(&sact
.sa_mask
);
7707 sact
.sa_handler
= sighdlr
;
7708 sigaction(SIGINT
, &sact
, NULL
);
7709 sigaction(SIGQUIT
, &sact
, NULL
);
7710 sigaction(SIGTERM
, &sact
, NULL
);
7711 sigaction(SIGHUP
, &sact
, NULL
);
7713 sact
.sa_handler
= sighdlr
;
7714 sact
.sa_flags
= SA_NOCLDSTOP
;
7715 sigaction(SIGCHLD
, &sact
, NULL
);
7717 if (!(display
= XOpenDisplay(0)))
7718 errx(1, "can not open display");
7720 conn
= XGetXCBConnection(display
);
7721 if (xcb_connection_has_error(conn
))
7722 errx(1, "can not get XCB connection");
7724 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
7725 xfd
= xcb_get_file_descriptor(conn
);
7727 /* look for local and global conf file */
7728 pwd
= getpwuid(getuid());
7730 errx(1, "invalid user: %d", getuid());
7732 xcb_grab_server(conn
);
7735 /* flush all events */
7736 while ((evt
= xcb_poll_for_event(conn
))) {
7737 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
7742 if (enable_wm() != 0)
7743 errx(1, "another window manager is currently running");
7754 for (i
= 0; ; i
++) {
7759 snprintf(conf
, sizeof conf
, "%s/.%s",
7760 pwd
->pw_dir
, SWM_CONF_FILE
);
7764 snprintf(conf
, sizeof conf
, "/etc/%s",
7769 snprintf(conf
, sizeof conf
, "%s/.%s",
7770 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
7774 snprintf(conf
, sizeof conf
, "/etc/%s",
7781 if (strlen(conf
) && stat(conf
, &sb
) != -1)
7782 if (S_ISREG(sb
.st_mode
)) {
7789 /* load conf (if any) */
7791 conf_load(cfile
, SWM_CONF_DEFAULT
);
7794 /* set some values to work around bad programs */
7796 /* grab existing windows (before we build the bars) */
7799 if (getenv("SWM_STARTED") == NULL
)
7800 setenv("SWM_STARTED", "YES", 1);
7802 /* setup all bars */
7803 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7804 for (i
= 0; i
< num_screens
; i
++)
7805 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
7806 if (winfocus
== NULL
)
7807 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
7816 xcb_ungrab_server(conn
);
7820 while ((evt
= xcb_poll_for_event(conn
))) {
7827 /* if we are being restarted go focus on first window */
7829 rr
= winfocus
->ws
->r
;
7831 /* not a visible window */
7835 /* move pointer to first screen if multi screen */
7836 if (num_screens
> 1 || outputs
> 1)
7837 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
,
7838 rr
->s
[0].root
, 0, 0, 0, 0, X(rr
),
7839 Y(rr
) + (bar_enabled
? bar_height
: 0));
7841 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7851 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7853 DNPRINTF(SWM_D_MISC
, "select failed");
7854 if (restart_wm
== 1)
7855 restart(NULL
, NULL
);
7856 if (search_resp
== 1)
7869 for (i
= 0; i
< num_screens
; ++i
)
7870 if (screens
[i
].bar_gc
!= 0)
7871 xcb_free_gc(conn
, screens
[i
].bar_gc
);
7873 XFreeFontSet(display
, bar_fs
);
7875 xcb_key_symbols_free(syms
);
7877 xcb_disconnect(conn
);