2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
91 #include <X11/cursorfont.h>
92 #include <X11/extensions/Xrandr.h>
93 #include <X11/Xft/Xft.h>
94 #include <X11/Xlib-xcb.h>
95 #include <xcb/xcb_atom.h>
96 #include <xcb/xcb_aux.h>
97 #include <xcb/xcb_event.h>
98 #include <xcb/xcb_icccm.h>
99 #include <xcb/xcb_keysyms.h>
100 #include <xcb/xtest.h>
101 #include <xcb/randr.h>
109 #ifdef SPECTRWM_BUILDSTR
110 static const char *buildstr
= SPECTRWM_BUILDSTR
;
112 static const char *buildstr
= SPECTRWM_VERSION
;
116 # error XRandR versions less than 1.0 are not supported
121 #define SWM_XRR_HAS_CRTC
125 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
126 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
127 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
128 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
129 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
130 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
131 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
132 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
133 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
134 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
135 #define xcb_icccm_get_wm_class xcb_get_wm_class
136 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
137 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
138 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
139 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
140 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
141 #define xcb_icccm_get_wm_name xcb_get_wm_name
142 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
143 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
144 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
145 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
146 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
147 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
148 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
149 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
150 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
151 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
156 #define DPRINTF(x...) do { \
158 fprintf(stderr, x); \
160 #define DNPRINTF(n,x...) do { \
161 if (swm_debug & n) { \
162 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
163 fprintf(stderr, x); \
166 #define SWM_D_MISC 0x0001
167 #define SWM_D_EVENT 0x0002
168 #define SWM_D_WS 0x0004
169 #define SWM_D_FOCUS 0x0008
170 #define SWM_D_MOVE 0x0010
171 #define SWM_D_STACK 0x0020
172 #define SWM_D_MOUSE 0x0040
173 #define SWM_D_PROP 0x0080
174 #define SWM_D_CLASS 0x0100
175 #define SWM_D_KEY 0x0200
176 #define SWM_D_QUIRK 0x0400
177 #define SWM_D_SPAWN 0x0800
178 #define SWM_D_EVENTQ 0x1000
179 #define SWM_D_CONF 0x2000
180 #define SWM_D_BAR 0x4000
181 #define SWM_D_INIT 0x8000
183 u_int32_t swm_debug
= 0
202 #define DPRINTF(x...)
203 #define DNPRINTF(n,x...)
206 /* convert 8-bit to 16-bit */
207 #define RGB_8_TO_16(col) ((col) << 8) + (col)
209 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
210 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
211 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
212 xrc.blue = RGB_8_TO_16((px) & 0xff); \
215 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
216 #define MODKEY Mod1Mask
217 #define CLEANMASK(mask) ((mask) & ~(numlockmask | LockMask))
218 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
219 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
220 #define SWM_PROPLEN (16)
221 #define SWM_FUNCNAME_LEN (32)
222 #define SWM_KEYS_LEN (255)
223 #define SWM_QUIRK_LEN (64)
224 #define X(r) (r)->g.x
225 #define Y(r) (r)->g.y
226 #define WIDTH(r) (r)->g.w
227 #define HEIGHT(r) (r)->g.h
228 #define BORDER(w) ((w)->bordered ? border_width : 0)
229 #define MAX_X(r) ((r)->g.x + (r)->g.w)
230 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
231 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
232 #define SH_MIN_W(w) (w)->sh.min_width
233 #define SH_MIN_H(w) (w)->sh.min_height
234 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
235 #define SH_MAX_W(w) (w)->sh.max_width
236 #define SH_MAX_H(w) (w)->sh.max_height
237 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
238 #define SH_INC_W(w) (w)->sh.width_inc
239 #define SH_INC_H(w) (w)->sh.height_inc
240 #define SWM_MAX_FONT_STEPS (3)
241 #define WINID(w) ((w) ? (w)->id : 0)
242 #define YESNO(x) ((x) ? "yes" : "no")
244 #define SWM_FOCUS_DEFAULT (0)
245 #define SWM_FOCUS_SYNERGY (1)
246 #define SWM_FOCUS_FOLLOW (2)
248 #define SWM_CONF_DEFAULT (0)
249 #define SWM_CONF_KEYMAPPING (1)
252 #define SWM_LIB "/usr/local/lib/libswmhack.so"
259 xcb_atom_t a_takefocus
;
261 xcb_atom_t a_netwmname
;
262 xcb_atom_t a_utf8_string
;
264 xcb_atom_t a_swm_iconic
;
266 volatile sig_atomic_t running
= 1;
267 volatile sig_atomic_t restart_wm
= 0;
269 /*int last_focus_event = FocusOut;*/
273 int xrandr_eventbase
;
274 unsigned int numlockmask
= 0;
277 xcb_connection_t
*conn
;
278 xcb_key_symbols_t
*syms
;
281 int cycle_visible
= 0;
283 int font_adjusted
= 0;
284 unsigned int mod_key
= MODKEY
;
287 struct swm_region
*search_r
;
288 int select_list_pipe
[2];
289 int select_resp_pipe
[2];
291 volatile sig_atomic_t search_resp
;
292 int search_resp_action
;
294 struct search_window
{
295 TAILQ_ENTRY(search_window
) entry
;
299 xcb_window_t indicator
;
301 TAILQ_HEAD(search_winlist
, search_window
);
302 struct search_winlist search_wl
;
307 SWM_SEARCH_UNICONIFY
,
308 SWM_SEARCH_NAME_WORKSPACE
,
309 SWM_SEARCH_SEARCH_WORKSPACE
,
310 SWM_SEARCH_SEARCH_WINDOW
313 #define SWM_STACK_TOP (0)
314 #define SWM_STACK_BOTTOM (1)
315 #define SWM_STACK_ABOVE (2)
316 #define SWM_STACK_BELOW (3)
319 double dialog_ratio
= 0.6;
321 #define SWM_BAR_MAX (256)
322 #define SWM_BAR_JUSTIFY_LEFT (0)
323 #define SWM_BAR_JUSTIFY_CENTER (1)
324 #define SWM_BAR_JUSTIFY_RIGHT (2)
325 #define SWM_BAR_OFFSET (4)
326 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
327 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
328 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
329 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
330 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
332 char *bar_argv
[] = { NULL
, NULL
};
334 unsigned char bar_ext
[SWM_BAR_MAX
];
335 char bar_vertext
[SWM_BAR_MAX
];
337 sig_atomic_t bar_alarm
= 0;
340 int bar_border_width
= 1;
341 int bar_at_bottom
= 0;
343 int bar_extra_running
= 0;
346 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
347 char *bar_format
= NULL
;
348 int stack_enabled
= 1;
349 int clock_enabled
= 1;
350 int urgent_enabled
= 0;
351 char *clock_format
= NULL
;
352 int title_name_enabled
= 0;
353 int title_class_enabled
= 0;
354 int window_name_enabled
= 0;
355 int focus_mode
= SWM_FOCUS_DEFAULT
;
356 int focus_close
= SWM_STACK_BELOW
;
357 int focus_close_wrap
= 1;
358 int focus_default
= SWM_STACK_TOP
;
359 int spawn_position
= SWM_STACK_TOP
;
360 int disable_border
= 0;
361 int border_width
= 1;
362 int verbose_layout
= 0;
367 XftColor bar_font_color
;
370 /* layout manager data */
371 struct swm_geometry
{
384 struct swm_geometry g
;
387 /* virtual "screens" */
389 TAILQ_ENTRY(swm_region
) entry
;
390 struct swm_geometry g
;
391 struct workspace
*ws
; /* current workspace on this region */
392 struct workspace
*ws_prior
; /* prior workspace on this region */
393 struct swm_screen
*s
; /* screen idx */
396 TAILQ_HEAD(swm_region_list
, swm_region
);
399 TAILQ_ENTRY(ws_win
) entry
;
401 xcb_window_t transient
;
402 struct ws_win
*child_trans
; /* transient child window */
403 struct swm_geometry g
; /* current geometry */
404 struct swm_geometry g_float
; /* region coordinates */
405 int g_floatvalid
; /* g_float geometry validity */
406 int floatmaxed
; /* whether maxed by max_stack */
411 unsigned int ewmh_flags
;
412 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
418 unsigned long quirks
;
419 struct workspace
*ws
; /* always valid */
420 struct swm_screen
*s
; /* always valid, never changes */
421 xcb_get_geometry_reply_t
*wa
;
423 xcb_icccm_get_wm_class_reply_t ch
;
424 xcb_icccm_wm_hints_t hints
;
426 TAILQ_HEAD(ws_win_list
, ws_win
);
430 TAILQ_ENTRY(pid_e
) entry
;
434 TAILQ_HEAD(pid_list
, pid_e
);
435 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
437 /* layout handlers */
439 void vertical_config(struct workspace
*, int);
440 void vertical_stack(struct workspace
*, struct swm_geometry
*);
441 void horizontal_config(struct workspace
*, int);
442 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
443 void max_stack(struct workspace
*, struct swm_geometry
*);
444 void plain_stacker(struct workspace
*);
445 void fancy_stacker(struct workspace
*);
448 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
449 void (*l_config
)(struct workspace
*, int);
451 #define SWM_L_FOCUSPREV (1<<0)
452 #define SWM_L_MAPONFOCUS (1<<1)
453 void (*l_string
)(struct workspace
*);
455 /* stack, configure */
456 { vertical_stack
, vertical_config
, 0, plain_stacker
},
457 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
459 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
460 { NULL
, NULL
, 0, NULL
},
463 /* position of max_stack mode in the layouts array, index into layouts! */
464 #define SWM_V_STACK (0)
465 #define SWM_H_STACK (1)
466 #define SWM_MAX_STACK (2)
468 #define SWM_H_SLICE (32)
469 #define SWM_V_SLICE (32)
471 /* define work spaces */
473 int idx
; /* workspace index */
474 char *name
; /* workspace name */
475 int always_raise
; /* raise windows on focus */
476 struct layout
*cur_layout
; /* current layout handlers */
477 struct ws_win
*focus
; /* may be NULL */
478 struct ws_win
*focus_prev
; /* may be NULL */
479 struct swm_region
*r
; /* may be NULL */
480 struct swm_region
*old_r
; /* may be NULL */
481 struct ws_win_list winlist
; /* list of windows in ws */
482 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
483 char stacker
[10]; /* display stacker and layout */
487 int horizontal_msize
;
489 int horizontal_stacks
;
500 SWM_S_COLOR_BAR_BORDER
,
501 SWM_S_COLOR_BAR_FONT
,
507 /* physical screen mapping */
508 #define SWM_WS_MAX (22) /* hard limit */
509 int workspace_limit
= 10; /* soft limit */
512 int idx
; /* screen index */
513 struct swm_region_list rl
; /* list of regions on this screen */
514 struct swm_region_list orl
; /* list of old regions */
516 struct workspace ws
[SWM_WS_MAX
];
522 } c
[SWM_S_COLOR_MAX
];
524 xcb_gcontext_t bar_gc
;
526 struct swm_screen
*screens
;
528 /* args to functions */
531 #define SWM_ARG_ID_FOCUSNEXT (0)
532 #define SWM_ARG_ID_FOCUSPREV (1)
533 #define SWM_ARG_ID_FOCUSMAIN (2)
534 #define SWM_ARG_ID_FOCUSCUR (4)
535 #define SWM_ARG_ID_SWAPNEXT (10)
536 #define SWM_ARG_ID_SWAPPREV (11)
537 #define SWM_ARG_ID_SWAPMAIN (12)
538 #define SWM_ARG_ID_MOVELAST (13)
539 #define SWM_ARG_ID_MASTERSHRINK (20)
540 #define SWM_ARG_ID_MASTERGROW (21)
541 #define SWM_ARG_ID_MASTERADD (22)
542 #define SWM_ARG_ID_MASTERDEL (23)
543 #define SWM_ARG_ID_FLIPLAYOUT (24)
544 #define SWM_ARG_ID_STACKRESET (30)
545 #define SWM_ARG_ID_STACKINIT (31)
546 #define SWM_ARG_ID_CYCLEWS_UP (40)
547 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
548 #define SWM_ARG_ID_CYCLESC_UP (42)
549 #define SWM_ARG_ID_CYCLESC_DOWN (43)
550 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
551 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
552 #define SWM_ARG_ID_STACKINC (50)
553 #define SWM_ARG_ID_STACKDEC (51)
554 #define SWM_ARG_ID_SS_ALL (60)
555 #define SWM_ARG_ID_SS_WINDOW (61)
556 #define SWM_ARG_ID_DONTCENTER (70)
557 #define SWM_ARG_ID_CENTER (71)
558 #define SWM_ARG_ID_KILLWINDOW (80)
559 #define SWM_ARG_ID_DELETEWINDOW (81)
560 #define SWM_ARG_ID_WIDTHGROW (90)
561 #define SWM_ARG_ID_WIDTHSHRINK (91)
562 #define SWM_ARG_ID_HEIGHTGROW (92)
563 #define SWM_ARG_ID_HEIGHTSHRINK (93)
564 #define SWM_ARG_ID_MOVEUP (100)
565 #define SWM_ARG_ID_MOVEDOWN (101)
566 #define SWM_ARG_ID_MOVELEFT (102)
567 #define SWM_ARG_ID_MOVERIGHT (103)
573 TAILQ_ENTRY(quirk
) entry
;
577 #define SWM_Q_FLOAT (1<<0) /* float this window */
578 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
579 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
580 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
581 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
582 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
584 TAILQ_HEAD(quirk_list
, quirk
);
585 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
588 * Supported EWMH hints should be added to
589 * both the enum and the ewmh array
594 _NET_MOVERESIZE_WINDOW
,
595 _NET_WM_ACTION_CLOSE
,
596 _NET_WM_ACTION_FULLSCREEN
,
598 _NET_WM_ACTION_RESIZE
,
599 _NET_WM_ALLOWED_ACTIONS
,
602 _NET_WM_STATE_FULLSCREEN
,
603 _NET_WM_STATE_HIDDEN
,
604 _NET_WM_STATE_MAXIMIZED_HORZ
,
605 _NET_WM_STATE_MAXIMIZED_VERT
,
606 _NET_WM_STATE_SKIP_PAGER
,
607 _NET_WM_STATE_SKIP_TASKBAR
,
609 _NET_WM_WINDOW_TYPE_DIALOG
,
610 _NET_WM_WINDOW_TYPE_DOCK
,
611 _NET_WM_WINDOW_TYPE_NORMAL
,
612 _NET_WM_WINDOW_TYPE_SPLASH
,
613 _NET_WM_WINDOW_TYPE_TOOLBAR
,
614 _NET_WM_WINDOW_TYPE_UTILITY
,
615 _SWM_WM_STATE_MANUAL
,
622 } ewmh
[SWM_EWMH_HINT_MAX
] = {
623 /* must be in same order as in the enum */
624 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
625 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
626 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
627 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
628 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
629 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
630 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
631 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
632 {"_NET_WM_STATE", XCB_ATOM_NONE
},
633 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
634 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
635 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
636 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
637 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
638 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
639 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
640 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
641 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
642 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
643 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
644 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
645 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
646 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
647 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
650 /* function prototypes */
651 void buttonpress(xcb_button_press_event_t
*);
652 void check_conn(void);
653 void clientmessage(xcb_client_message_event_t
*);
654 int conf_load(char *, int);
655 void configurenotify(xcb_configure_notify_event_t
*);
656 void configurerequest(xcb_configure_request_event_t
*);
657 void constrain_window(struct ws_win
*, struct swm_region
*, int);
658 void destroynotify(xcb_destroy_notify_event_t
*);
660 void enternotify(xcb_enter_notify_event_t
*);
661 void event_error(xcb_generic_error_t
*);
662 void event_handle(xcb_generic_event_t
*);
663 char *expand_tilde(char *);
664 void expose(xcb_expose_event_t
*);
665 struct ws_win
*find_window(xcb_window_t
);
666 int floating_toggle_win(struct ws_win
*);
667 void focus(struct swm_region
*, union arg
*);
668 void focus_magic(struct ws_win
*);
670 void focusin(xcb_focus_in_event_t
*);
672 xcb_atom_t
get_atom_from_string(const char *);
674 char *get_atom_name(xcb_atom_t
);
675 char *get_notify_detail_label(uint8_t);
676 char *get_notify_mode_label(uint8_t);
678 xcb_screen_t
*get_screen(int);
679 char *get_win_name(xcb_window_t
);
680 uint32_t getstate(xcb_window_t
);
681 void grabbuttons(struct ws_win
*, int);
682 void keypress(xcb_key_press_event_t
*);
684 void leavenotify(xcb_leave_notify_event_t
*);
686 void map_window_raised(xcb_window_t
);
687 void mapnotify(xcb_map_notify_event_t
*);
688 void mappingnotify(xcb_mapping_notify_event_t
*);
689 void maprequest(xcb_map_request_event_t
*);
690 void new_region(struct swm_screen
*, int, int, int, int);
691 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
692 void propertynotify(xcb_property_notify_event_t
*);
693 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
694 void screenchange(xcb_randr_screen_change_notify_event_t
*);
695 void store_float_geom(struct ws_win
*, struct swm_region
*);
696 void unmanage_window(struct ws_win
*);
697 void unmapnotify(xcb_unmap_notify_event_t
*);
698 void update_window(struct ws_win
*);
699 /*void visibilitynotify(xcb_visibility_notify_event_t *);*/
702 expand_tilde(char *s
)
711 errx(1, "expand_tilde: NULL string.");
720 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
721 errx(1, "expand_tilde: sysconf");
723 if ((user
= calloc(1, max
+ 1)) == NULL
)
724 errx(1, "expand_tilde: calloc");
726 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
731 pwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
735 if (asprintf(&result
, "%s%s", pwd
->pw_dir
, s
) == -1)
739 errx(1, "expand_tilde: failed to allocate memory.");
745 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
747 unsigned int tmpr
, tmpg
, tmpb
;
749 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
752 *rr
= RGB_8_TO_16(tmpr
);
753 *gg
= RGB_8_TO_16(tmpg
);
754 *bb
= RGB_8_TO_16(tmpb
);
760 get_screen(int screen
)
762 const xcb_setup_t
*r
;
763 xcb_screen_iterator_t iter
;
765 if ((r
= xcb_get_setup(conn
)) == NULL
) {
766 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
770 iter
= xcb_setup_roots_iterator(r
);
771 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
781 xcb_get_input_focus_cookie_t c
;
782 xcb_get_input_focus_reply_t
*r
;
785 c
= xcb_get_input_focus(conn
);
787 r
= xcb_get_input_focus_reply(conn
, c
, NULL
);
793 map_window_raised(xcb_window_t win
)
795 uint32_t val
= XCB_STACK_MODE_ABOVE
;
797 xcb_configure_window(conn
, win
,
798 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
800 xcb_map_window(conn
, win
);
804 get_atom_from_string(const char *str
)
806 xcb_intern_atom_cookie_t c
;
807 xcb_intern_atom_reply_t
*r
;
810 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
811 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
819 return (XCB_ATOM_NONE
);
823 update_iconic(struct ws_win
*win
, int newv
)
830 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
831 a_swm_iconic
, XCB_ATOM_INTEGER
, 32, 1, &v
);
833 xcb_delete_property(conn
, win
->id
, a_swm_iconic
);
837 get_iconic(struct ws_win
*win
)
840 xcb_get_property_reply_t
*pr
= NULL
;
843 pr
= xcb_get_property_reply(conn
,
844 xcb_get_property(conn
, 0, win
->id
, a_swm_iconic
,
845 XCB_ATOM_INTEGER
, 0, 1), NULL
);
848 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
850 v
= *((int32_t *)xcb_get_property_value(pr
));
861 int i
, j
, num_screens
;
863 sup_list
= get_atom_from_string("_NET_SUPPORTED");
865 for (i
= 0; i
< LENGTH(ewmh
); i
++)
866 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
868 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
869 for (i
= 0; i
< num_screens
; i
++) {
870 /* Support check window will be created by workaround(). */
872 /* Report supported atoms */
873 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
874 for (j
= 0; j
< LENGTH(ewmh
); j
++)
875 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
876 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
885 xcb_atom_t sup_check
, sup_list
;
887 xcb_get_property_cookie_t pc
;
888 xcb_get_property_reply_t
*pr
;
890 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
891 sup_list
= get_atom_from_string("_NET_SUPPORTED");
892 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
894 for (i
= 0; i
< num_screens
; i
++) {
895 /* Get the support check window and destroy it */
896 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, sup_check
,
897 XCB_ATOM_WINDOW
, 0, 1);
898 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
901 if (pr
->format
== sup_check
) {
902 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
904 xcb_destroy_window(conn
, id
);
905 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
906 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
913 ewmh_autoquirk(struct ws_win
*win
)
917 xcb_get_property_cookie_t c
;
918 xcb_get_property_reply_t
*r
;
920 c
= xcb_get_property(conn
, 0, win
->id
,
921 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
922 r
= xcb_get_property_reply(conn
, c
, NULL
);
926 n
= xcb_get_property_value_length(r
);
927 type
= xcb_get_property_value(r
);
929 for (i
= 0; i
< n
; i
++) {
930 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
932 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
933 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
934 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
936 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
939 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
940 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
942 win
->quirks
= SWM_Q_FLOAT
;
949 #define SWM_EWMH_ACTION_COUNT_MAX (6)
950 #define EWMH_F_FULLSCREEN (1<<0)
951 #define EWMH_F_ABOVE (1<<1)
952 #define EWMH_F_HIDDEN (1<<2)
953 #define EWMH_F_SKIP_PAGER (1<<3)
954 #define EWMH_F_SKIP_TASKBAR (1<<4)
955 #define SWM_F_MANUAL (1<<5)
958 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
966 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
967 "fullscreen %s\n", win
->id
, YESNO(fs
));
970 if (!win
->g_floatvalid
)
971 store_float_geom(win
, win
->ws
->r
);
973 win
->g
= win
->ws
->r
->g
;
976 if (win
->g_floatvalid
) {
977 /* refloat at last floating relative position */
978 win
->g
= win
->g_float
;
979 X(win
) += X(win
->ws
->r
);
980 Y(win
) += Y(win
->ws
->r
);
988 ewmh_update_actions(struct ws_win
*win
)
990 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
996 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
999 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1000 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1003 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1004 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1007 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1008 #define _NET_WM_STATE_ADD 1 /* add/set property */
1009 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1012 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
1014 unsigned int mask
= 0;
1015 unsigned int changed
= 0;
1016 unsigned int orig_flags
;
1021 DNPRINTF(SWM_D_PROP
, "ewmh_update_win_state: window: 0x%x, state: %ld, "
1022 "action: %ld\n", win
->id
, state
, action
);
1024 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1025 mask
= EWMH_F_FULLSCREEN
;
1026 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1027 mask
= EWMH_F_ABOVE
;
1028 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1029 mask
= SWM_F_MANUAL
;
1030 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1031 mask
= EWMH_F_SKIP_PAGER
;
1032 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1033 mask
= EWMH_F_SKIP_TASKBAR
;
1036 orig_flags
= win
->ewmh_flags
;
1039 case _NET_WM_STATE_REMOVE
:
1040 win
->ewmh_flags
&= ~mask
;
1042 case _NET_WM_STATE_ADD
:
1043 win
->ewmh_flags
|= mask
;
1045 case _NET_WM_STATE_TOGGLE
:
1046 win
->ewmh_flags
^= mask
;
1050 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
1052 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1054 if (!floating_toggle_win(win
))
1055 win
->ewmh_flags
= orig_flags
; /* revert */
1056 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1058 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
1059 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1061 if (!ewmh_set_win_fullscreen(win
,
1062 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
1063 win
->ewmh_flags
= orig_flags
; /* revert */
1065 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1067 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
1068 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1069 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1070 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
1071 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1072 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1073 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1074 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
1075 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1076 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1077 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1078 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
1079 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1080 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1081 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1082 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
1083 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1084 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
1085 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
1086 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
1090 ewmh_get_win_state(struct ws_win
*win
)
1093 xcb_get_property_cookie_t c
;
1094 xcb_get_property_reply_t
*r
;
1100 win
->ewmh_flags
= 0;
1102 win
->ewmh_flags
|= EWMH_F_ABOVE
;
1104 win
->ewmh_flags
|= SWM_F_MANUAL
;
1106 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1107 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1108 r
= xcb_get_property_reply(conn
, c
, NULL
);
1112 states
= xcb_get_property_value(r
);
1113 n
= xcb_get_property_value_length(r
);
1115 for (i
= 0; i
< n
; i
++)
1116 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1124 dumpwins(struct swm_region
*r
, union arg
*args
)
1128 xcb_get_window_attributes_cookie_t c
;
1129 xcb_get_window_attributes_reply_t
*wa
;
1131 /* suppress unused warning since var is needed */
1134 if (r
->ws
== NULL
) {
1135 warnx("dumpwins: invalid workspace");
1139 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1140 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1141 state
= getstate(win
->id
);
1142 c
= xcb_get_window_attributes(conn
, win
->id
);
1143 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1145 warnx("window: 0x%x, map_state: %d, state: %u, "
1146 "transient: 0x%x", win
->id
, wa
->map_state
,
1147 state
, win
->transient
);
1150 warnx("window: 0x%x, failed xcb_get_window_attributes",
1154 warnx("===== unmanaged window list =====");
1155 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1156 state
= getstate(win
->id
);
1157 c
= xcb_get_window_attributes(conn
, win
->id
);
1158 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1160 warnx("window: 0x%x, map_state: %d, state: %u, "
1161 "transient: 0x%x", win
->id
, wa
->map_state
,
1162 state
, win
->transient
);
1165 warnx("window: 0x%x, failed xcb_get_window_attributes",
1169 warnx("=================================");
1173 dumpwins(struct swm_region
*r
, union arg
*args
)
1176 #endif /* SWM_DEBUG */
1181 int saved_errno
, status
;
1184 saved_errno
= errno
;
1188 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1193 if (errno
!= ECHILD
)
1194 warn("sighdlr: waitpid");
1195 #endif /* SWM_DEBUG */
1198 if (pid
== searchpid
)
1202 if (WIFEXITED(status
)) {
1203 if (WEXITSTATUS(status
) != 0)
1204 warnx("sighdlr: child exit status: %d",
1205 WEXITSTATUS(status
));
1207 warnx("sighdlr: child is terminated "
1209 #endif /* SWM_DEBUG */
1223 errno
= saved_errno
;
1229 struct pid_e
*p
= NULL
;
1231 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1236 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1245 name_to_pixel(const char *colorname
)
1247 uint32_t result
= 0;
1248 char cname
[32] = "#";
1249 xcb_screen_t
*screen
;
1250 xcb_colormap_t cmap
;
1251 xcb_alloc_color_reply_t
*cr
;
1252 xcb_alloc_named_color_reply_t
*nr
;
1253 uint16_t rr
, gg
, bb
;
1255 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1256 cmap
= screen
->default_colormap
;
1258 /* color is in format rgb://rr/gg/bb */
1259 if (strncmp(colorname
, "rgb:", 4) == 0) {
1260 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1261 warnx("could not parse rgb %s", colorname
);
1263 cr
= xcb_alloc_color_reply(conn
,
1264 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1270 warnx("color '%s' not found", colorname
);
1273 nr
= xcb_alloc_named_color_reply(conn
,
1274 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1277 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1278 nr
= xcb_alloc_named_color_reply(conn
,
1279 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1286 warnx("color '%s' not found", colorname
);
1293 setscreencolor(char *val
, int i
, int c
)
1297 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1298 if (i
> 0 && i
<= num_screens
) {
1299 screens
[i
- 1].c
[c
].pixel
= name_to_pixel(val
);
1300 free(screens
[i
- 1].c
[c
].name
);
1301 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1303 } else if (i
== -1) {
1304 for (i
= 0; i
< num_screens
; i
++) {
1305 screens
[i
].c
[c
].pixel
= name_to_pixel(val
);
1306 free(screens
[i
].c
[c
].name
);
1307 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1311 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1316 fancy_stacker(struct workspace
*ws
)
1318 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1319 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1320 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1321 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1322 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1323 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1324 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1325 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1326 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1330 plain_stacker(struct workspace
*ws
)
1332 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1333 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1334 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1335 sizeof ws
->stacker
);
1336 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1337 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1338 sizeof ws
->stacker
);
1342 custom_region(char *val
)
1344 unsigned int x
, y
, w
, h
;
1345 int sidx
, num_screens
;
1346 xcb_screen_t
*screen
;
1348 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1349 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1350 errx(1, "invalid custom region, "
1351 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1352 if (sidx
< 1 || sidx
> num_screens
)
1353 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1357 if ((screen
= get_screen(sidx
)) == NULL
)
1358 errx(1, "ERROR: can't get screen %d.", sidx
);
1361 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1363 if (x
> screen
->width_in_pixels
||
1364 y
> screen
->height_in_pixels
||
1365 w
+ x
> screen
->width_in_pixels
||
1366 h
+ y
> screen
->height_in_pixels
) {
1367 warnx("ignoring region %ux%u+%u+%u - not within screen "
1368 "boundaries (%ux%u)", w
, h
, x
, y
,
1369 screen
->width_in_pixels
, screen
->height_in_pixels
);
1373 new_region(&screens
[sidx
], x
, y
, w
, h
);
1377 socket_setnonblock(int fd
)
1381 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1382 err(1, "fcntl F_GETFL");
1383 flags
|= O_NONBLOCK
;
1384 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1385 err(1, "fcntl F_SETFL");
1389 bar_print(struct swm_region
*r
, const char *s
)
1392 xcb_rectangle_t rect
;
1400 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
1402 switch (bar_justify
) {
1403 case SWM_BAR_JUSTIFY_LEFT
:
1406 case SWM_BAR_JUSTIFY_CENTER
:
1407 x
= (WIDTH(r
) - info
.width
) / 2;
1409 case SWM_BAR_JUSTIFY_RIGHT
:
1410 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
1414 if (x
< SWM_BAR_OFFSET
)
1419 rect
.width
= WIDTH(r
->bar
);
1420 rect
.height
= HEIGHT(r
->bar
);
1422 /* clear back buffer */
1423 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1424 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1425 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
,
1426 sizeof(rect
), &rect
);
1428 /* draw back buffer */
1429 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1430 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_BACKGROUND
, gcv
);
1431 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
;
1432 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1434 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
1435 DefaultVisual(display
, r
->s
->idx
),
1436 DefaultColormap(display
, r
->s
->idx
));
1438 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
1439 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
1442 XftDrawDestroy(draw
);
1445 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1446 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1450 bar_extra_stop(void)
1454 bzero(bar_pipe
, sizeof bar_pipe
);
1457 kill(bar_pid
, SIGTERM
);
1460 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1465 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1467 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1469 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
1470 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
1474 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1476 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1478 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
1479 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
1483 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1485 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1488 bar_class_name(s
, sz
, r
);
1489 strlcat(s
, ":", sz
);
1490 bar_title_name(s
, sz
, r
);
1494 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1496 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1498 if (r
->ws
->focus
->floating
)
1499 strlcat(s
, "(f)", sz
);
1503 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1507 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1509 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1512 strlcat(s
, title
, sz
);
1516 int urgent
[SWM_WS_MAX
];
1518 bar_urgent(char *s
, size_t sz
)
1521 int i
, j
, num_screens
;
1523 xcb_get_property_cookie_t c
;
1524 xcb_icccm_wm_hints_t hints
;
1526 for (i
= 0; i
< workspace_limit
; i
++)
1529 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1530 for (i
= 0; i
< num_screens
; i
++)
1531 for (j
= 0; j
< workspace_limit
; j
++)
1532 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1533 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
1534 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
1537 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
1541 for (i
= 0; i
< workspace_limit
; i
++) {
1543 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1545 snprintf(b
, sizeof b
, "- ");
1551 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1553 if (r
== NULL
|| r
->ws
== NULL
)
1555 if (r
->ws
->name
!= NULL
)
1556 strlcat(s
, r
->ws
->name
, sz
);
1559 /* build the default bar format according to the defined enabled options */
1561 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1563 /* if format provided, just copy the buffers */
1564 if (bar_format
!= NULL
) {
1565 strlcpy(fmtnew
, fmtexp
, sz
);
1569 /* reset the output buffer */
1572 strlcat(fmtnew
, "+N:+I ", sz
);
1574 strlcat(fmtnew
, "+S", sz
);
1575 strlcat(fmtnew
, " ", sz
);
1577 /* only show the workspace name if there's actually one */
1578 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1579 strlcat(fmtnew
, "<+D>", sz
);
1580 strlcat(fmtnew
, "+3<", sz
);
1582 if (clock_enabled
) {
1583 strlcat(fmtnew
, fmtexp
, sz
);
1584 strlcat(fmtnew
, "+4<", sz
);
1587 /* bar_urgent already adds the space before the last asterisk */
1589 strlcat(fmtnew
, "* +U*+4<", sz
);
1591 if (title_class_enabled
) {
1592 strlcat(fmtnew
, "+C", sz
);
1593 if (title_name_enabled
== 0)
1594 strlcat(fmtnew
, "+4<", sz
);
1597 /* checks needed by the colon and floating strlcat(3) calls below */
1598 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1599 if (title_name_enabled
) {
1600 if (title_class_enabled
)
1601 strlcat(fmtnew
, ":", sz
);
1602 strlcat(fmtnew
, "+T+4<", sz
);
1604 if (window_name_enabled
) {
1605 if (r
->ws
->focus
->floating
)
1606 strlcat(fmtnew
, "+F ", sz
);
1607 strlcat(fmtnew
, "+64W ", sz
);
1611 /* finally add the action script output and the version */
1612 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1616 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1618 /* special case; no limit given, pad one space, instead */
1619 if (*limit
== (int)sz
- 1)
1621 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1624 /* replaces the bar format character sequences (like in tmux(1)) */
1626 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1630 char tmp
[SWM_BAR_MAX
];
1634 /* reset strlcat(3) buffer */
1637 /* get number, if any */
1640 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1641 limit
= sizeof tmp
- 1;
1642 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
1643 limit
= sizeof tmp
- 1;
1645 /* there is nothing to replace (ie EOL) */
1652 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1655 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1658 bar_class_name(tmp
, sizeof tmp
, r
);
1661 bar_workspace_name(tmp
, sizeof tmp
, r
);
1664 bar_window_float(tmp
, sizeof tmp
, r
);
1667 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1670 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1673 bar_class_title_name(tmp
, sizeof tmp
, r
);
1676 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1679 bar_title_name(tmp
, sizeof tmp
, r
);
1682 bar_urgent(tmp
, sizeof tmp
);
1685 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1688 bar_window_name(tmp
, sizeof tmp
, r
);
1691 /* unknown character sequence; copy as-is */
1692 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1698 if ((int)len
< limit
)
1700 while (limit
-- > 0) {
1701 if (*offrep
>= sz
- 1)
1703 fmtrep
[(*offrep
)++] = *ptr
++;
1711 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1716 while (*fmt
!= '\0') {
1718 /* skip ordinary characters */
1721 fmtrep
[off
++] = *fmt
++;
1725 /* character sequence found; replace it */
1726 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1735 bar_fmt_expand(char *fmtexp
, size_t sz
)
1742 /* start by grabbing the current time and date */
1744 localtime_r(&tmt
, &tm
);
1746 /* figure out what to expand */
1747 if (bar_format
!= NULL
)
1749 else if (bar_format
== NULL
&& clock_enabled
)
1751 /* if nothing to expand bail out */
1757 /* copy as-is, just in case the format shouldn't be expanded below */
1758 strlcpy(fmtexp
, fmt
, sz
);
1759 /* finally pass the string through strftime(3) */
1760 #ifndef SWM_DENY_CLOCK_FORMAT
1761 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1762 warnx("format too long");
1770 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1771 char fmtrep
[SWM_BAR_MAX
];
1773 struct swm_region
*r
;
1775 /* expand the format by first passing it through strftime(3) */
1776 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1778 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1779 for (i
= 0; i
< num_screens
; i
++) {
1780 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1783 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1784 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1785 bar_print(r
, fmtrep
);
1796 if (bar_enabled
== 0)
1798 if (bar_extra
&& bar_extra_running
) {
1799 /* ignore short reads; it'll correct itself */
1800 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1801 if (b
&& b
[len
- 1] == '\n') {
1803 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1805 if (b
== NULL
&& errno
!= EAGAIN
) {
1806 warn("bar_update: bar_extra failed");
1810 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1819 /* suppress unused warning since var is needed */
1826 bar_toggle(struct swm_region
*r
, union arg
*args
)
1828 struct swm_region
*tmpr
;
1831 /* suppress unused warnings since vars are needed */
1835 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1837 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1839 for (i
= 0; i
< num_screens
; i
++)
1840 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1842 xcb_unmap_window(conn
, tmpr
->bar
->id
);
1844 for (i
= 0; i
< num_screens
; i
++)
1845 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1847 map_window_raised(tmpr
->bar
->id
);
1850 bar_enabled
= !bar_enabled
;
1853 /* must be after stack */
1862 struct swm_region
*r
;
1866 /* do this here because the conf file is in memory */
1867 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1868 /* launch external status app */
1869 bar_extra_running
= 1;
1870 if (pipe(bar_pipe
) == -1)
1871 err(1, "pipe error");
1872 socket_setnonblock(bar_pipe
[0]);
1873 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1874 if (dup2(bar_pipe
[0], 0) == -1)
1876 if (dup2(bar_pipe
[1], 1) == -1)
1878 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1879 err(1, "could not disable SIGPIPE");
1880 switch (bar_pid
= fork()) {
1882 err(1, "cannot fork");
1886 execvp(bar_argv
[0], bar_argv
);
1887 err(1, "%s external app failed", bar_argv
[0]);
1889 default: /* parent */
1895 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1896 for (i
= 0; i
< num_screens
; i
++)
1897 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1900 wa
[0] = screens
[i
].c
[SWM_S_COLOR_BAR
].pixel
;
1901 wa
[1] = screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1902 xcb_change_window_attributes(conn
, r
->bar
->id
,
1903 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
1909 bar_setup(struct swm_region
*r
)
1911 char *font
, *fontpos
, *dup
, *search
;
1913 xcb_screen_t
*screen
;
1917 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
1918 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
1923 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
1924 err(1, "bar_setup: calloc: failed to allocate memory.");
1926 if (bar_font
== NULL
) {
1927 if ((dup
= strdup(bar_fonts
)) == NULL
)
1928 errx(1, "insufficient memory.");
1930 while ((font
= strsep(&search
, ",")) != NULL
) {
1934 DNPRINTF(SWM_D_INIT
, "bar_setup: try font %s\n", font
);
1938 while ((fontpos
= index(fontpos
, '-'))) {
1944 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
1947 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
1950 warnx("unable to load font %s", font
);
1953 DNPRINTF(SWM_D_INIT
, "successfully opened "
1961 if (bar_font
== NULL
)
1962 errx(1, "unable to open a font");
1964 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
1969 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
1971 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
1972 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
1973 warn("unable to allocate Xft color");
1976 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1977 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
1978 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
1980 r
->bar
->id
= xcb_generate_id(conn
);
1981 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1982 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1983 wa
[2] = XCB_EVENT_MASK_EXPOSURE
;
1985 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
1986 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
1987 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1988 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
1989 | XCB_CW_EVENT_MASK
, wa
);
1991 r
->bar
->buffer
= xcb_generate_id(conn
);
1992 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
1993 WIDTH(r
->bar
), HEIGHT(r
->bar
));
1995 xcb_randr_select_input(conn
, r
->bar
->id
,
1996 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
1999 map_window_raised(r
->bar
->id
);
2001 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2002 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2005 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
2006 err(1, "could not install bar_signal");
2011 bar_cleanup(struct swm_region
*r
)
2015 xcb_destroy_window(conn
, r
->bar
->id
);
2016 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2022 set_win_state(struct ws_win
*win
, uint16_t state
)
2024 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2026 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x\n", win
->id
);
2031 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2032 a_state
, 32, 2, data
);
2036 getstate(xcb_window_t w
)
2038 uint32_t result
= 0;
2039 xcb_get_property_cookie_t c
;
2040 xcb_get_property_reply_t
*r
;
2042 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2043 r
= xcb_get_property_reply(conn
, c
, NULL
);
2046 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2047 result
= *((uint32_t *)xcb_get_property_value(r
));
2051 DNPRINTF(SWM_D_MISC
, "getstate property: win 0x%x state %u\n", w
,
2057 version(struct swm_region
*r
, union arg
*args
)
2059 /* suppress unused warnings since vars are needed */
2063 bar_version
= !bar_version
;
2065 snprintf(bar_vertext
, sizeof bar_vertext
,
2066 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2068 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2075 client_msg(struct ws_win
*win
, xcb_atom_t a
)
2077 xcb_client_message_event_t ev
;
2082 bzero(&ev
, sizeof ev
);
2083 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2084 ev
.window
= win
->id
;
2087 ev
.data
.data32
[0] = a
;
2088 ev
.data
.data32
[1] = XCB_CURRENT_TIME
;
2090 xcb_send_event(conn
, 0, win
->id
,
2091 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2094 /* synthetic response to a ConfigureRequest when not making a change */
2096 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2098 xcb_configure_notify_event_t ce
;
2103 /* send notification of unchanged state. */
2104 bzero(&ce
, sizeof(ce
));
2105 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2108 ce
.width
= WIDTH(win
);
2109 ce
.height
= HEIGHT(win
);
2110 ce
.override_redirect
= 0;
2115 ce
.window
= win
->id
;
2116 ce
.border_width
= BORDER(win
);
2117 ce
.above_sibling
= XCB_WINDOW_NONE
;
2120 ce
.event
= ev
->window
;
2121 ce
.window
= ev
->window
;
2123 /* make response appear more WM_SIZE_HINTS-compliant */
2125 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2126 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2127 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2128 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2129 SH_INC_W(win
), SH_INC_H(win
));
2133 /* the hint may be set... to 0! */
2134 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2135 ce
.width
= SH_MIN_W(win
);
2136 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2137 ce
.height
= SH_MIN_H(win
);
2142 /* may also be advertized as 0 */
2143 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2144 ce
.width
= SH_MAX_W(win
);
2145 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2146 ce
.height
= SH_MAX_H(win
);
2149 /* resize increment. */
2151 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2152 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2154 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2155 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2159 /* adjust x and y for requested border_width. */
2160 ce
.x
+= BORDER(win
) - ev
->border_width
;
2161 ce
.y
+= BORDER(win
) - ev
->border_width
;
2162 ce
.border_width
= ev
->border_width
;
2163 ce
.above_sibling
= ev
->sibling
;
2166 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2167 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2168 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2170 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2175 count_win(struct workspace
*ws
, int count_transient
)
2180 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2181 if (count_transient
== 0 && win
->floating
)
2183 if (count_transient
== 0 && win
->transient
)
2189 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2195 quit(struct swm_region
*r
, union arg
*args
)
2197 /* suppress unused warnings since vars are needed */
2201 DNPRINTF(SWM_D_MISC
, "quit\n");
2206 unmap_window(struct ws_win
*win
)
2208 DNPRINTF(SWM_D_EVENT
, "unmap_window: window: 0x%x\n", win
->id
);
2213 /* don't unmap again */
2214 if (getstate(win
->id
) == XCB_ICCCM_WM_STATE_ICONIC
)
2217 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2219 xcb_unmap_window(conn
, win
->id
);
2220 xcb_change_window_attributes(conn
, win
->id
,
2221 XCB_CW_BORDER_PIXEL
, &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
2228 int i
, j
, num_screens
;
2230 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2231 for (i
= 0; i
< num_screens
; i
++)
2232 for (j
= 0; j
< workspace_limit
; j
++)
2233 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2238 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2240 xcb_key_press_event_t event
;
2241 xcb_keycode_t
*keycode
;
2246 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2248 DNPRINTF(SWM_D_MISC
, "fake_keypress: win 0x%x keycode %u\n",
2251 bzero(&event
, sizeof(event
));
2252 event
.event
= win
->id
;
2253 event
.root
= win
->s
->root
;
2254 event
.child
= XCB_WINDOW_NONE
;
2255 event
.time
= XCB_CURRENT_TIME
;
2256 event
.event_x
= X(win
);
2257 event
.event_y
= Y(win
);
2260 event
.same_screen
= 1;
2261 event
.detail
= *keycode
;
2262 event
.state
= modifiers
;
2264 event
.response_type
= XCB_KEY_PRESS
;
2265 xcb_send_event(conn
, 1, win
->id
,
2266 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
2268 event
.response_type
= XCB_KEY_RELEASE
;
2269 xcb_send_event(conn
, 1, win
->id
,
2270 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
2276 restart(struct swm_region
*r
, union arg
*args
)
2278 /* suppress unused warning since var is needed */
2281 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2283 /* disable alarm because the following code may not be interrupted */
2285 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
2286 err(1, "can't disable alarm");
2292 XftFontClose(display
, bar_font
);
2293 XftColorFree(display
, DefaultVisual(display
, r
->s
->idx
),
2294 DefaultColormap(display
, r
->s
->idx
), &bar_font_color
);
2295 xcb_key_symbols_free(syms
);
2297 xcb_disconnect(conn
);
2299 execvp(start_argv
[0], start_argv
);
2300 warn("execvp failed");
2305 root_to_region(xcb_window_t root
)
2307 struct swm_region
*r
= NULL
;
2309 xcb_query_pointer_reply_t
*qpr
;
2311 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2313 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2314 for (i
= 0; i
< num_screens
; i
++)
2315 if (screens
[i
].root
== root
)
2318 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2319 screens
[i
].root
), NULL
);
2322 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: (%d,%d)\n",
2323 qpr
->root_x
, qpr
->root_y
);
2324 /* choose a region based on pointer location */
2325 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2326 if (X(r
) <= qpr
->root_x
&& qpr
->root_x
< MAX_X(r
) &&
2327 Y(r
) <= qpr
->root_y
&& qpr
->root_y
< MAX_Y(r
))
2333 r
= TAILQ_FIRST(&screens
[i
].rl
);
2339 find_unmanaged_window(xcb_window_t id
)
2342 int i
, j
, num_screens
;
2344 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2345 for (i
= 0; i
< num_screens
; i
++)
2346 for (j
= 0; j
< workspace_limit
; j
++)
2347 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2355 find_window(xcb_window_t id
)
2358 int i
, j
, num_screens
;
2359 xcb_query_tree_reply_t
*r
;
2361 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2362 for (i
= 0; i
< num_screens
; i
++)
2363 for (j
= 0; j
< workspace_limit
; j
++)
2364 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2368 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
2372 /* if we were looking for the parent return that window instead */
2373 if (r
->parent
== 0 || r
->root
== r
->parent
) {
2378 /* look for parent */
2379 for (i
= 0; i
< num_screens
; i
++)
2380 for (j
= 0; j
< workspace_limit
; j
++)
2381 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2382 if (r
->parent
== win
->id
) {
2392 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2397 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2399 close(xcb_get_file_descriptor(conn
));
2401 setenv("LD_PRELOAD", SWM_LIB
, 1);
2403 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2404 warn("spawn: asprintf SWM_WS");
2407 setenv("_SWM_WS", ret
, 1);
2411 if (asprintf(&ret
, "%d", getpid()) == -1) {
2412 warn("spawn: asprintf _SWM_PID");
2415 setenv("_SWM_PID", ret
, 1);
2419 if (setsid() == -1) {
2420 warn("spawn: setsid");
2426 * close stdin and stdout to prevent interaction between apps
2427 * and the baraction script
2428 * leave stderr open to record errors
2430 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2431 warn("spawn: open");
2434 dup2(fd
, STDIN_FILENO
);
2435 dup2(fd
, STDOUT_FILENO
);
2440 execvp(args
->argv
[0], args
->argv
);
2442 warn("spawn: execvp");
2447 kill_refs(struct ws_win
*win
)
2449 int i
, x
, num_screens
;
2450 struct swm_region
*r
;
2451 struct workspace
*ws
;
2456 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2457 for (i
= 0; i
< num_screens
; i
++)
2458 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2459 for (x
= 0; x
< workspace_limit
; x
++) {
2461 if (win
== ws
->focus
)
2463 if (win
== ws
->focus_prev
)
2464 ws
->focus_prev
= NULL
;
2469 validate_win(struct ws_win
*testwin
)
2472 struct workspace
*ws
;
2473 struct swm_region
*r
;
2474 int i
, x
, num_screens
;
2476 if (testwin
== NULL
)
2479 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2480 for (i
= 0; i
< num_screens
; i
++)
2481 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2482 for (x
= 0; x
< workspace_limit
; x
++) {
2484 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2492 validate_ws(struct workspace
*testws
)
2494 struct swm_region
*r
;
2495 struct workspace
*ws
;
2496 int i
, x
, num_screens
;
2498 /* validate all ws */
2499 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2500 for (i
= 0; i
< num_screens
; i
++)
2501 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2502 for (x
= 0; x
< workspace_limit
; x
++) {
2511 unfocus_win(struct ws_win
*win
)
2513 xcb_window_t none
= XCB_WINDOW_NONE
;
2515 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
2519 if (win
->ws
== NULL
)
2522 if (validate_ws(win
->ws
))
2523 return; /* XXX this gets hit with thunderbird, needs fixing */
2525 if (win
->ws
->r
== NULL
)
2528 if (validate_win(win
)) {
2533 if (win
->ws
->focus
== win
) {
2534 win
->ws
->focus
= NULL
;
2535 win
->ws
->focus_prev
= win
;
2538 if (validate_win(win
->ws
->focus
)) {
2539 kill_refs(win
->ws
->focus
);
2540 win
->ws
->focus
= NULL
;
2542 if (validate_win(win
->ws
->focus_prev
)) {
2543 kill_refs(win
->ws
->focus_prev
);
2544 win
->ws
->focus_prev
= NULL
;
2547 grabbuttons(win
, 0);
2548 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2549 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
2550 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2551 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
2558 int i
, j
, num_screens
;
2560 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2562 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2563 for (i
= 0; i
< num_screens
; i
++)
2564 for (j
= 0; j
< workspace_limit
; j
++)
2565 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2570 focus_win(struct ws_win
*win
)
2572 struct ws_win
*cfw
= NULL
;
2573 xcb_get_input_focus_reply_t
*r
;
2574 xcb_window_t cur_focus
= XCB_WINDOW_NONE
;
2576 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
2580 if (win
->ws
== NULL
)
2583 if (validate_ws(win
->ws
))
2584 return; /* XXX this gets hit with thunderbird, needs fixing */
2586 if (validate_win(win
)) {
2591 if (validate_win(win
)) {
2596 r
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
2598 cur_focus
= r
->focus
;
2601 if ((cfw
= find_window(cur_focus
)) != NULL
)
2605 /* use larger hammer since the window was killed somehow */
2606 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2607 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2608 xcb_change_window_attributes(conn
, cfw
->id
,
2609 XCB_CW_BORDER_PIXEL
,
2610 &cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
2614 win
->ws
->focus
= win
;
2616 if (win
->ws
->r
!= NULL
) {
2618 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
2619 win
->id
, XCB_CURRENT_TIME
);
2620 grabbuttons(win
, 1);
2621 xcb_change_window_attributes(conn
, win
->id
,
2622 XCB_CW_BORDER_PIXEL
,
2623 &win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
);
2624 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2625 win
->ws
->always_raise
)
2626 map_window_raised(win
->id
);
2628 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2629 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2637 event_drain(uint8_t rt
)
2639 xcb_generic_event_t
*evt
;
2642 while ((evt
= xcb_poll_for_event(conn
))) {
2643 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
2651 switchws(struct swm_region
*r
, union arg
*args
)
2653 int wsid
= args
->id
, unmap_old
= 0;
2654 struct swm_region
*this_r
, *other_r
;
2656 struct workspace
*new_ws
, *old_ws
;
2662 if (wsid
>= workspace_limit
)
2666 old_ws
= this_r
->ws
;
2667 new_ws
= &this_r
->s
->ws
[wsid
];
2669 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2670 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2672 if (new_ws
== NULL
|| old_ws
== NULL
)
2674 if (new_ws
== old_ws
)
2677 other_r
= new_ws
->r
;
2678 if (other_r
== NULL
) {
2679 /* the other workspace is hidden, hide this one */
2683 /* the other ws is visible in another region, exchange them */
2684 other_r
->ws_prior
= new_ws
;
2685 other_r
->ws
= old_ws
;
2686 old_ws
->r
= other_r
;
2688 this_r
->ws_prior
= old_ws
;
2689 this_r
->ws
= new_ws
;
2692 /* this is needed so that we can click on a window after a restart */
2696 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2697 focus(new_ws
->r
, &a
);
2699 /* unmap old windows */
2701 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2704 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2705 event_drain(XCB_ENTER_NOTIFY
);
2709 DNPRINTF(SWM_D_WS
, "switchws: done\n");
2713 cyclews(struct swm_region
*r
, union arg
*args
)
2716 struct swm_screen
*s
= r
->s
;
2719 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2720 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2725 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2728 case SWM_ARG_ID_CYCLEWS_UP
:
2729 if (a
.id
< workspace_limit
- 1)
2734 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2737 case SWM_ARG_ID_CYCLEWS_DOWN
:
2741 a
.id
= workspace_limit
- 1;
2748 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2750 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2754 } while (a
.id
!= r
->ws
->idx
);
2758 priorws(struct swm_region
*r
, union arg
*args
)
2762 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2763 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2765 if (r
->ws_prior
== NULL
)
2768 a
.id
= r
->ws_prior
->idx
;
2773 cyclescr(struct swm_region
*r
, union arg
*args
)
2775 struct swm_region
*rr
= NULL
;
2777 int i
, x
, y
, num_screens
;
2779 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2780 /* do nothing if we don't have more than one screen */
2781 if (!(num_screens
> 1 || outputs
> 1))
2786 case SWM_ARG_ID_CYCLESC_UP
:
2787 rr
= TAILQ_NEXT(r
, entry
);
2789 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2791 case SWM_ARG_ID_CYCLESC_DOWN
:
2792 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2794 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2802 /* move mouse to region */
2804 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2805 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0, 0,
2808 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2811 if (rr
->ws
->focus
) {
2812 /* move to focus window */
2813 x
= X(rr
->ws
->focus
) + 1;
2814 y
= Y(rr
->ws
->focus
) + 1;
2815 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0,
2821 sort_windows(struct ws_win_list
*wl
)
2823 struct ws_win
*win
, *parent
, *nxt
;
2828 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2829 nxt
= TAILQ_NEXT(win
, entry
);
2830 if (win
->transient
) {
2831 parent
= find_window(win
->transient
);
2832 if (parent
== NULL
) {
2833 warnx("not possible bug");
2836 TAILQ_REMOVE(wl
, win
, entry
);
2837 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2844 swapwin(struct swm_region
*r
, union arg
*args
)
2846 struct ws_win
*target
, *source
;
2847 struct ws_win
*cur_focus
;
2848 struct ws_win_list
*wl
;
2851 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2852 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2854 cur_focus
= r
->ws
->focus
;
2855 if (cur_focus
== NULL
)
2859 wl
= &source
->ws
->winlist
;
2862 case SWM_ARG_ID_SWAPPREV
:
2863 if (source
->transient
)
2864 source
= find_window(source
->transient
);
2865 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2866 if (target
&& target
->transient
)
2867 target
= find_window(target
->transient
);
2868 TAILQ_REMOVE(wl
, source
, entry
);
2870 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2872 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2874 case SWM_ARG_ID_SWAPNEXT
:
2875 target
= TAILQ_NEXT(source
, entry
);
2876 /* move the parent and let the sort handle the move */
2877 if (source
->transient
)
2878 source
= find_window(source
->transient
);
2879 TAILQ_REMOVE(wl
, source
, entry
);
2881 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2883 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2885 case SWM_ARG_ID_SWAPMAIN
:
2886 target
= TAILQ_FIRST(wl
);
2887 if (target
== source
) {
2888 if (source
->ws
->focus_prev
!= NULL
&&
2889 source
->ws
->focus_prev
!= target
)
2890 source
= source
->ws
->focus_prev
;
2894 if (target
== NULL
|| source
== NULL
)
2896 source
->ws
->focus_prev
= target
;
2897 TAILQ_REMOVE(wl
, target
, entry
);
2898 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2899 TAILQ_REMOVE(wl
, source
, entry
);
2900 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2902 case SWM_ARG_ID_MOVELAST
:
2903 TAILQ_REMOVE(wl
, source
, entry
);
2904 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2907 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2919 focus_prev(struct ws_win
*win
)
2921 struct ws_win
*winfocus
= NULL
;
2922 struct ws_win
*cur_focus
= NULL
;
2923 struct ws_win_list
*wl
= NULL
;
2924 struct workspace
*ws
= NULL
;
2926 if (!(win
&& win
->ws
))
2931 cur_focus
= ws
->focus
;
2933 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2934 WINID(win
), WINID(cur_focus
));
2936 /* pickle, just focus on whatever */
2937 if (cur_focus
== NULL
) {
2938 /* use prev_focus if valid */
2939 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2940 find_window(WINID(ws
->focus_prev
)))
2941 winfocus
= ws
->focus_prev
;
2945 /* if transient focus on parent */
2946 if (cur_focus
->transient
) {
2947 winfocus
= find_window(cur_focus
->transient
);
2951 /* if in max_stack try harder */
2952 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2953 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2954 if (cur_focus
!= ws
->focus_prev
)
2955 winfocus
= ws
->focus_prev
;
2957 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2962 DNPRINTF(SWM_D_FOCUS
, "focus_prev: focus_close: %d\n", focus_close
);
2964 if (winfocus
== NULL
|| winfocus
== win
) {
2965 switch (focus_close
) {
2966 case SWM_STACK_BOTTOM
:
2967 winfocus
= TAILQ_FIRST(wl
);
2970 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2972 case SWM_STACK_ABOVE
:
2973 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
2974 if (focus_close_wrap
)
2975 winfocus
= TAILQ_FIRST(wl
);
2977 winfocus
= TAILQ_PREV(cur_focus
,
2978 ws_win_list
, entry
);
2981 case SWM_STACK_BELOW
:
2982 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
2984 if (focus_close_wrap
)
2985 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2987 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2993 if (winfocus
== NULL
) {
2994 if (focus_default
== SWM_STACK_TOP
)
2995 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2997 winfocus
= TAILQ_FIRST(wl
);
3001 focus_magic(winfocus
);
3005 focus(struct swm_region
*r
, union arg
*args
)
3007 struct ws_win
*winfocus
= NULL
, *head
;
3008 struct ws_win
*cur_focus
= NULL
;
3009 struct ws_win_list
*wl
= NULL
;
3010 struct workspace
*ws
= NULL
;
3016 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
3018 /* treat FOCUS_CUR special */
3019 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
3020 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
3021 winfocus
= r
->ws
->focus
;
3022 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
3023 winfocus
= r
->ws
->focus_prev
;
3025 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
3026 if (winfocus
->iconic
== 0)
3029 focus_magic(winfocus
);
3033 if ((cur_focus
= r
->ws
->focus
) == NULL
)
3037 if (TAILQ_EMPTY(wl
))
3039 /* make sure there is at least one uniconified window */
3041 TAILQ_FOREACH(winfocus
, wl
, entry
)
3042 if (winfocus
->iconic
== 0) {
3050 case SWM_ARG_ID_FOCUSPREV
:
3051 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3053 head
= TAILQ_LAST(wl
, ws_win_list
);
3055 if (WINID(winfocus
) == cur_focus
->transient
) {
3056 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
3058 head
= TAILQ_LAST(wl
, ws_win_list
);
3063 if (winfocus
&& winfocus
->iconic
) {
3064 while (winfocus
!= cur_focus
) {
3065 if (winfocus
== NULL
)
3066 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3067 if (winfocus
->iconic
== 0)
3069 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3075 case SWM_ARG_ID_FOCUSNEXT
:
3076 head
= TAILQ_NEXT(cur_focus
, entry
);
3078 head
= TAILQ_FIRST(wl
);
3082 if (winfocus
&& winfocus
->iconic
) {
3083 while (winfocus
!= cur_focus
) {
3084 if (winfocus
== NULL
)
3085 winfocus
= TAILQ_FIRST(wl
);
3086 if (winfocus
->iconic
== 0)
3088 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3093 case SWM_ARG_ID_FOCUSMAIN
:
3094 winfocus
= TAILQ_FIRST(wl
);
3095 if (winfocus
== cur_focus
)
3096 winfocus
= cur_focus
->ws
->focus_prev
;
3103 focus_magic(winfocus
);
3109 cycle_layout(struct swm_region
*r
, union arg
*args
)
3111 struct workspace
*ws
= r
->ws
;
3114 /* suppress unused warning since var is needed */
3117 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3120 if (ws
->cur_layout
->l_stack
== NULL
)
3121 ws
->cur_layout
= &layouts
[0];
3126 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3127 event_drain(XCB_ENTER_NOTIFY
);
3129 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3134 stack_config(struct swm_region
*r
, union arg
*args
)
3136 struct workspace
*ws
= r
->ws
;
3138 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3141 if (ws
->cur_layout
->l_config
!= NULL
)
3142 ws
->cur_layout
->l_config(ws
, args
->id
);
3144 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3151 struct swm_geometry g
;
3152 struct swm_region
*r
;
3158 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3160 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3161 for (i
= 0; i
< num_screens
; i
++) {
3165 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3166 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3167 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3169 /* start with screen geometry, adjust for bar */
3171 g
.w
-= 2 * border_width
;
3172 g
.h
-= 2 * border_width
;
3178 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3179 r
->ws
->cur_layout
->l_string(r
->ws
);
3180 /* save r so we can track region changes */
3187 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3188 event_drain(XCB_ENTER_NOTIFY
);
3190 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3194 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3196 /* retain window geom and region geom */
3197 win
->g_float
= win
->g
;
3198 win
->g_float
.x
-= X(r
);
3199 win
->g_float
.y
-= Y(r
);
3200 win
->g_floatvalid
= 1;
3201 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
3202 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3203 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3204 win
->g_float
.w
, win
->g_float
.h
);
3208 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3213 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
3216 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3217 * geom on ws switches or return from max mode
3219 if (win
->g_floatvalid
&& (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3220 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)))) {
3221 /* refloat at last floating relative position */
3222 win
->g
= win
->g_float
;
3227 win
->floatmaxed
= 0;
3230 * if set to fullscreen mode, configure window to maximum size.
3232 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3233 if (!win
->g_floatvalid
)
3234 store_float_geom(win
, win
->ws
->r
);
3236 win
->g
= win
->ws
->r
->g
;
3240 * remove border on fullscreen floater when in fullscreen mode or when
3241 * the quirk is present.
3243 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3244 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3245 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3246 if (win
->bordered
) {
3248 X(win
) += border_width
;
3249 Y(win
) += border_width
;
3251 } else if (!win
->bordered
) {
3253 X(win
) -= border_width
;
3254 Y(win
) -= border_width
;
3257 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3258 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3259 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3264 * floaters and transients are auto-centred unless moved
3267 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3268 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3271 /* keep window within region bounds */
3272 constrain_window(win
, r
, 0);
3278 * Send keystrokes to terminal to decrease/increase the font size as the
3279 * window size changes.
3282 adjust_font(struct ws_win
*win
)
3284 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3285 win
->floating
|| win
->transient
)
3288 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3289 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3290 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3291 win
->font_size_boundary
[win
->font_steps
] =
3292 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3295 win
->last_inc
= win
->sh
.width_inc
;
3296 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
3297 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3298 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3301 win
->last_inc
= win
->sh
.width_inc
;
3302 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
3306 #define SWAPXY(g) do { \
3308 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3309 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3312 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3314 struct swm_geometry win_g
, r_g
= *g
;
3315 struct ws_win
*win
, *fs_win
= NULL
;
3316 int i
, j
, s
, stacks
;
3317 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3318 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3319 int split
, colno
, winno
, mwin
, msize
, mscale
;
3320 int remain
, missing
, v_slice
, reconfigure
;
3322 xcb_get_window_attributes_reply_t
*war
;
3324 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3325 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3327 winno
= count_win(ws
, 0);
3328 if (winno
== 0 && count_win(ws
, 1) == 0)
3331 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3332 if (win
->transient
== 0 && win
->floating
== 0
3333 && win
->iconic
== 0)
3340 w_inc
= win
->sh
.width_inc
;
3341 w_base
= win
->sh
.base_width
;
3342 mwin
= ws
->l_state
.horizontal_mwin
;
3343 mscale
= ws
->l_state
.horizontal_msize
;
3344 stacks
= ws
->l_state
.horizontal_stacks
;
3347 w_inc
= win
->sh
.height_inc
;
3348 w_base
= win
->sh
.base_height
;
3349 mwin
= ws
->l_state
.vertical_mwin
;
3350 mscale
= ws
->l_state
.vertical_msize
;
3351 stacks
= ws
->l_state
.vertical_stacks
;
3355 if (stacks
> winno
- mwin
)
3356 stacks
= winno
- mwin
;
3360 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3361 if (mwin
&& winno
> mwin
) {
3362 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3366 win_g
.w
= v_slice
* mscale
;
3368 if (w_inc
> 1 && w_inc
< v_slice
) {
3369 /* adjust for window's requested size increment */
3370 remain
= (win_g
.w
- w_base
) % w_inc
;
3376 win_g
.x
+= r_g
.w
- msize
;
3379 colno
= split
= winno
/ stacks
;
3380 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3381 2 * border_width
) / stacks
);
3383 hrh
= r_g
.h
/ colno
;
3384 extra
= r_g
.h
- (colno
* hrh
);
3385 win_g
.h
= hrh
- 2 * border_width
;
3387 /* stack all the tiled windows */
3388 i
= j
= 0, s
= stacks
;
3389 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3390 if (win
->transient
!= 0 || win
->floating
!= 0)
3392 if (win
->iconic
!= 0)
3395 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3400 if (split
&& i
== split
) {
3401 colno
= (winno
- mwin
) / stacks
;
3402 if (s
<= (winno
- mwin
) % stacks
)
3404 split
= split
+ colno
;
3405 hrh
= (r_g
.h
/ colno
);
3406 extra
= r_g
.h
- (colno
* hrh
);
3410 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3411 win_g
.w
= (r_g
.w
- msize
-
3412 (stacks
* 2 * border_width
)) / stacks
;
3414 win_g
.w
+= (r_g
.w
- msize
-
3415 (stacks
* 2 * border_width
)) % stacks
;
3419 win_g
.h
= hrh
- 2 * border_width
;
3421 h_inc
= win
->sh
.width_inc
;
3422 h_base
= win
->sh
.base_width
;
3424 h_inc
= win
->sh
.height_inc
;
3425 h_base
= win
->sh
.base_height
;
3427 if (j
== colno
- 1) {
3428 win_g
.h
= hrh
+ extra
;
3429 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3430 /* adjust for window's requested size increment */
3431 remain
= (win_g
.h
- h_base
) % h_inc
;
3432 missing
= h_inc
- remain
;
3434 if (missing
<= extra
|| j
== 0) {
3446 win_g
.y
+= last_h
+ 2 * border_width
;
3448 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3450 win_g
.w
+= 2 * border_width
;
3451 win_g
.h
+= 2 * border_width
;
3456 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3457 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3461 WIDTH(win
) = win_g
.h
;
3462 HEIGHT(win
) = win_g
.w
;
3465 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3466 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3470 WIDTH(win
) = win_g
.w
;
3471 HEIGHT(win
) = win_g
.h
;
3475 if (bordered
!= win
->bordered
) {
3477 win
->bordered
= bordered
;
3485 war
= xcb_get_window_attributes_reply(conn
,
3486 xcb_get_window_attributes(conn
, win
->id
),
3489 if (war
->map_state
== XCB_MAP_STATE_UNMAPPED
)
3490 map_window_raised(win
->id
);
3500 /* now, stack all the floaters and transients */
3501 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3502 if (win
->transient
== 0 && win
->floating
== 0)
3504 if (win
->iconic
== 1)
3506 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3511 stack_floater(win
, ws
->r
);
3512 map_window_raised(win
->id
);
3516 stack_floater(fs_win
, ws
->r
);
3517 map_window_raised(fs_win
->id
);
3522 vertical_config(struct workspace
*ws
, int id
)
3524 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3528 case SWM_ARG_ID_STACKRESET
:
3529 case SWM_ARG_ID_STACKINIT
:
3530 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3531 ws
->l_state
.vertical_mwin
= 1;
3532 ws
->l_state
.vertical_stacks
= 1;
3534 case SWM_ARG_ID_MASTERSHRINK
:
3535 if (ws
->l_state
.vertical_msize
> 1)
3536 ws
->l_state
.vertical_msize
--;
3538 case SWM_ARG_ID_MASTERGROW
:
3539 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3540 ws
->l_state
.vertical_msize
++;
3542 case SWM_ARG_ID_MASTERADD
:
3543 ws
->l_state
.vertical_mwin
++;
3545 case SWM_ARG_ID_MASTERDEL
:
3546 if (ws
->l_state
.vertical_mwin
> 0)
3547 ws
->l_state
.vertical_mwin
--;
3549 case SWM_ARG_ID_STACKINC
:
3550 ws
->l_state
.vertical_stacks
++;
3552 case SWM_ARG_ID_STACKDEC
:
3553 if (ws
->l_state
.vertical_stacks
> 1)
3554 ws
->l_state
.vertical_stacks
--;
3556 case SWM_ARG_ID_FLIPLAYOUT
:
3557 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3565 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3567 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3569 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3573 horizontal_config(struct workspace
*ws
, int id
)
3575 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3578 case SWM_ARG_ID_STACKRESET
:
3579 case SWM_ARG_ID_STACKINIT
:
3580 ws
->l_state
.horizontal_mwin
= 1;
3581 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3582 ws
->l_state
.horizontal_stacks
= 1;
3584 case SWM_ARG_ID_MASTERSHRINK
:
3585 if (ws
->l_state
.horizontal_msize
> 1)
3586 ws
->l_state
.horizontal_msize
--;
3588 case SWM_ARG_ID_MASTERGROW
:
3589 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3590 ws
->l_state
.horizontal_msize
++;
3592 case SWM_ARG_ID_MASTERADD
:
3593 ws
->l_state
.horizontal_mwin
++;
3595 case SWM_ARG_ID_MASTERDEL
:
3596 if (ws
->l_state
.horizontal_mwin
> 0)
3597 ws
->l_state
.horizontal_mwin
--;
3599 case SWM_ARG_ID_STACKINC
:
3600 ws
->l_state
.horizontal_stacks
++;
3602 case SWM_ARG_ID_STACKDEC
:
3603 if (ws
->l_state
.horizontal_stacks
> 1)
3604 ws
->l_state
.horizontal_stacks
--;
3606 case SWM_ARG_ID_FLIPLAYOUT
:
3607 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3615 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3617 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3619 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3622 /* fullscreen view */
3624 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3626 struct swm_geometry gg
= *g
;
3627 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3628 int winno
, num_screens
;
3630 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3635 winno
= count_win(ws
, 0);
3636 if (winno
== 0 && count_win(ws
, 1) == 0)
3639 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3640 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3641 if (win
->transient
) {
3643 parent
= find_window(win
->transient
);
3647 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3649 * retain geometry for retrieval on exit from
3652 store_float_geom(win
, ws
->r
);
3653 win
->floatmaxed
= 1;
3656 /* only reconfigure if necessary */
3657 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3658 HEIGHT(win
) != gg
.h
) {
3664 WIDTH(win
) += 2 * border_width
;
3665 HEIGHT(win
) += 2 * border_width
;
3670 /* unmap only if we don't have multi screen */
3671 if (win
!= ws
->focus
)
3672 if (!(num_screens
> 1 || outputs
> 1))
3676 /* put the last transient on top */
3679 map_window_raised(parent
->id
);
3680 stack_floater(wintrans
, ws
->r
);
3681 focus_magic(wintrans
);
3686 send_to_ws(struct swm_region
*r
, union arg
*args
)
3688 int wsid
= args
->id
;
3689 struct ws_win
*win
= NULL
, *parent
;
3690 struct workspace
*ws
, *nws
;
3691 char ws_idx_str
[SWM_PROPLEN
];
3694 if (wsid
>= workspace_limit
)
3697 if (r
&& r
->ws
&& r
->ws
->focus
)
3703 if (win
->ws
->idx
== wsid
)
3706 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%x\n", win
->id
);
3709 nws
= &win
->s
->ws
[wsid
];
3711 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3713 if (win
->transient
) {
3714 parent
= find_window(win
->transient
);
3716 unmap_window(parent
);
3717 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3718 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3723 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3724 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3725 if (TAILQ_EMPTY(&ws
->winlist
))
3726 r
->ws
->focus
= NULL
;
3729 /* Try to update the window's workspace property */
3730 if (snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
3731 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3733 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
3734 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
3745 pressbutton(struct swm_region
*r
, union arg
*args
)
3747 /* suppress unused warning since var is needed */
3750 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
3751 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3752 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
3753 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
3757 raise_toggle(struct swm_region
*r
, union arg
*args
)
3759 /* suppress unused warning since var is needed */
3762 if (r
== NULL
|| r
->ws
== NULL
)
3765 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3767 /* bring floaters back to top */
3768 if (r
->ws
->always_raise
== 0)
3775 iconify(struct swm_region
*r
, union arg
*args
)
3779 /* suppress unused warning since var is needed */
3782 if (r
->ws
->focus
== NULL
)
3784 unmap_window(r
->ws
->focus
);
3785 update_iconic(r
->ws
->focus
, 1);
3788 r
->ws
->focus
= NULL
;
3789 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3796 get_win_name(xcb_window_t win
)
3799 xcb_get_property_cookie_t c
;
3800 xcb_icccm_get_text_property_reply_t r
;
3802 c
= xcb_icccm_get_wm_name(conn
, win
);
3803 if (xcb_icccm_get_wm_name_reply(conn
, c
, &r
, NULL
)) {
3804 if (r
.name_len
> 0) {
3805 name
= malloc(r
.name_len
+ 1);
3807 memcpy(name
, r
.name
, r
.name_len
);
3808 name
[r
.name_len
] = '\0';
3811 xcb_icccm_get_text_property_reply_wipe(&r
);
3818 uniconify(struct swm_region
*r
, union arg
*args
)
3825 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3827 if (r
== NULL
|| r
->ws
== NULL
)
3830 /* make sure we have anything to uniconify */
3831 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3832 if (win
->ws
== NULL
)
3833 continue; /* should never happen */
3834 if (win
->iconic
== 0)
3842 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3844 spawn_select(r
, args
, "search", &searchpid
);
3846 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3849 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3850 if (win
->ws
== NULL
)
3851 continue; /* should never happen */
3852 if (win
->iconic
== 0)
3855 name
= get_win_name(win
->id
);
3858 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
3868 name_workspace(struct swm_region
*r
, union arg
*args
)
3872 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3878 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3880 spawn_select(r
, args
, "name_workspace", &searchpid
);
3882 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3885 fprintf(lfile
, "%s", "");
3890 search_workspace(struct swm_region
*r
, union arg
*args
)
3893 struct workspace
*ws
;
3896 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3902 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3904 spawn_select(r
, args
, "search", &searchpid
);
3906 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3909 for (i
= 0; i
< workspace_limit
; i
++) {
3913 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3914 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3921 search_win_cleanup(void)
3923 struct search_window
*sw
= NULL
;
3925 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3926 xcb_destroy_window(conn
, sw
->indicator
);
3927 xcb_free_gc(conn
, sw
->gc
);
3928 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3934 search_win(struct swm_region
*r
, union arg
*args
)
3936 struct ws_win
*win
= NULL
;
3937 struct search_window
*sw
= NULL
;
3939 uint32_t gcv
[3], wa
[2];
3947 DNPRINTF(SWM_D_MISC
, "search_win\n");
3950 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3952 spawn_select(r
, args
, "search", &searchpid
);
3954 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3957 TAILQ_INIT(&search_wl
);
3960 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3961 if (win
->iconic
== 1)
3964 sw
= calloc(1, sizeof(struct search_window
));
3966 warn("search_win: calloc");
3968 search_win_cleanup();
3974 snprintf(s
, sizeof s
, "%d", i
);
3977 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
3979 w
= xcb_generate_id(conn
);
3980 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
3981 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
3982 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
3983 info
.width
+ 4, bar_font
->height
+ 4,
3984 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
3985 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
3988 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3990 sw
->gc
= xcb_generate_id(conn
);
3991 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
3992 gcv
[1] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
3994 xcb_create_gc(conn
, sw
->gc
, w
, XCB_GC_FOREGROUND
|
3995 XCB_GC_BACKGROUND
| XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
3996 map_window_raised(w
);
3998 draw
= XftDrawCreate(display
, w
,
3999 DefaultVisual(display
, r
->s
->idx
),
4000 DefaultColormap(display
, r
->s
->idx
));
4002 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
4003 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
4006 XftDrawDestroy(draw
);
4008 DNPRINTF(SWM_D_MISC
, "search_win: mapped window: 0x%x\n", w
);
4010 fprintf(lfile
, "%d\n", i
);
4020 search_resp_uniconify(char *resp
, unsigned long len
)
4026 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
4028 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
4029 if (win
->iconic
== 0)
4031 name
= get_win_name(win
->id
);
4034 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
4039 if (strncmp(s
, resp
, len
) == 0) {
4040 /* XXX this should be a callback to generalize */
4041 update_iconic(win
, 0);
4050 search_resp_name_workspace(char *resp
, unsigned long len
)
4052 struct workspace
*ws
;
4054 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
4056 if (search_r
->ws
== NULL
)
4061 free(search_r
->ws
->name
);
4062 search_r
->ws
->name
= NULL
;
4066 ws
->name
= strdup(resp
);
4067 if (ws
->name
== NULL
) {
4068 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
4069 "strdup: %s", strerror(errno
));
4076 search_resp_search_workspace(char *resp
)
4083 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
4087 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
4094 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
4096 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
4103 switchws(search_r
, &a
);
4107 search_resp_search_window(char *resp
)
4112 struct search_window
*sw
;
4114 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
4118 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
4123 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
4125 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
4132 TAILQ_FOREACH(sw
, &search_wl
, entry
)
4133 if (idx
== sw
->idx
) {
4139 #define MAX_RESP_LEN 1024
4142 search_do_resp(void)
4148 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
4153 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
4154 warn("search: calloc");
4158 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
4160 warn("search: read error");
4163 resp
[rbytes
] = '\0';
4166 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4167 * newline, so work around that by sanitizing the resp now.
4169 resp
[strcspn(resp
, "\n")] = '\0';
4172 switch (search_resp_action
) {
4173 case SWM_SEARCH_UNICONIFY
:
4174 search_resp_uniconify(resp
, len
);
4176 case SWM_SEARCH_NAME_WORKSPACE
:
4177 search_resp_name_workspace(resp
, len
);
4179 case SWM_SEARCH_SEARCH_WORKSPACE
:
4180 search_resp_search_workspace(resp
);
4182 case SWM_SEARCH_SEARCH_WINDOW
:
4183 search_resp_search_window(resp
);
4188 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4189 search_win_cleanup();
4191 search_resp_action
= SWM_SEARCH_NONE
;
4192 close(select_resp_pipe
[0]);
4197 wkill(struct swm_region
*r
, union arg
*args
)
4199 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4201 if (r
->ws
->focus
== NULL
)
4204 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4205 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4207 if (r
->ws
->focus
->can_delete
)
4208 client_msg(r
->ws
->focus
, a_delete
);
4215 floating_toggle_win(struct ws_win
*win
)
4217 struct swm_region
*r
;
4227 /* reject floating toggles in max stack mode */
4228 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4231 if (win
->floating
) {
4232 if (!win
->floatmaxed
) {
4233 /* retain position for refloat */
4234 store_float_geom(win
, r
);
4238 if (win
->g_floatvalid
) {
4239 /* refloat at last floating relative position */
4240 X(win
) = win
->g_float
.x
+ X(r
);
4241 Y(win
) = win
->g_float
.y
+ Y(r
);
4242 WIDTH(win
) = win
->g_float
.w
;
4243 HEIGHT(win
) = win
->g_float
.h
;
4248 ewmh_update_actions(win
);
4254 floating_toggle(struct swm_region
*r
, union arg
*args
)
4256 struct ws_win
*win
= r
->ws
->focus
;
4259 /* suppress unused warning since var is needed */
4265 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4266 _NET_WM_STATE_TOGGLE
);
4270 if (win
== win
->ws
->focus
) {
4271 a
.id
= SWM_ARG_ID_FOCUSCUR
;
4272 focus(win
->ws
->r
, &a
);
4276 event_drain(XCB_ENTER_NOTIFY
);
4280 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4282 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4284 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4286 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4289 if (X(win
) + BORDER(win
) < X(r
)) {
4291 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4293 X(win
) = X(r
) - BORDER(win
);
4296 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4298 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4300 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4303 if (Y(win
) + BORDER(win
) < Y(r
)) {
4305 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4307 Y(win
) = Y(r
) - BORDER(win
);
4313 if (HEIGHT(win
) < 1)
4319 update_window(struct ws_win
*win
)
4324 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
4325 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
4326 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
4330 wc
[3] = HEIGHT(win
);
4331 wc
[4] = BORDER(win
);
4333 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
4334 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
4335 wc
[3], YESNO(win
->bordered
));
4337 xcb_configure_window(conn
, win
->id
, mask
, wc
);
4340 #define SWM_RESIZE_STEPS (50)
4343 resize(struct ws_win
*win
, union arg
*args
)
4345 xcb_timestamp_t timestamp
= 0;
4346 struct swm_region
*r
= NULL
;
4347 int resize_step
= 0;
4348 struct swm_geometry g
;
4349 int top
= 0, left
= 0, resizing
;
4351 unsigned int shape
; /* cursor style */
4352 xcb_cursor_t cursor
;
4353 xcb_font_t cursor_font
;
4354 xcb_query_pointer_reply_t
*xpr
;
4355 xcb_generic_event_t
*evt
;
4356 xcb_motion_notify_event_t
*mne
;
4362 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4365 DNPRINTF(SWM_D_EVENT
, "resize: window: 0x%x, floating: %s, "
4366 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
4369 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4372 /* reject resizes in max mode for floaters (transient ok) */
4373 if (win
->floatmaxed
)
4377 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4383 case SWM_ARG_ID_WIDTHSHRINK
:
4384 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4387 case SWM_ARG_ID_WIDTHGROW
:
4388 WIDTH(win
) += SWM_RESIZE_STEPS
;
4391 case SWM_ARG_ID_HEIGHTSHRINK
:
4392 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4395 case SWM_ARG_ID_HEIGHTGROW
:
4396 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4403 constrain_window(win
, r
, 1);
4405 store_float_geom(win
,r
);
4409 /* get cursor offset from window root */
4410 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4417 if (xpr
->win_x
< WIDTH(win
) / 2)
4420 if (xpr
->win_y
< HEIGHT(win
) / 2)
4423 if (args
->id
== SWM_ARG_ID_CENTER
)
4426 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
4428 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
4430 cursor_font
= xcb_generate_id(conn
);
4431 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4433 cursor
= xcb_generate_id(conn
);
4434 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4435 shape
, shape
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4437 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
4438 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
4443 while ((evt
= xcb_wait_for_event(conn
)) && resizing
) {
4444 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4445 case XCB_BUTTON_RELEASE
:
4446 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
4449 case XCB_MOTION_NOTIFY
:
4450 mne
= (xcb_motion_notify_event_t
*)evt
;
4451 /* cursor offset/delta from start of the operation */
4452 dx
= mne
->root_x
- xpr
->root_x
;
4453 dy
= mne
->root_y
- xpr
->root_y
;
4459 if (args
->id
== SWM_ARG_ID_CENTER
) {
4460 if (g
.h
/ 2 + dy
< 1)
4464 HEIGHT(win
) = g
.h
+ 2 * dy
;
4472 HEIGHT(win
) = g
.h
+ dy
;
4479 if (args
->id
== SWM_ARG_ID_CENTER
) {
4480 if (g
.w
/ 2 + dx
< 1)
4484 WIDTH(win
) = g
.w
+ 2 * dx
;
4492 WIDTH(win
) = g
.w
+ dx
;
4495 constrain_window(win
, r
, 1);
4497 /* not free, don't sync more than 120 times / second */
4498 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4499 timestamp
= mne
->time
;
4514 store_float_geom(win
,r
);
4516 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4517 xcb_free_cursor(conn
, cursor
);
4518 xcb_close_font(conn
, cursor_font
);
4520 DNPRINTF(SWM_D_EVENT
, "resize: done\n");
4524 resize_step(struct swm_region
*r
, union arg
*args
)
4526 struct ws_win
*win
= NULL
;
4528 if (r
&& r
->ws
&& r
->ws
->focus
)
4536 #define SWM_MOVE_STEPS (50)
4539 move(struct ws_win
*win
, union arg
*args
)
4541 xcb_timestamp_t timestamp
= 0;
4542 int move_step
= 0, moving
;
4543 struct swm_region
*r
= NULL
;
4544 xcb_font_t cursor_font
;
4545 xcb_cursor_t cursor
;
4546 xcb_query_pointer_reply_t
*qpr
;
4547 xcb_generic_event_t
*evt
;
4548 xcb_motion_notify_event_t
*mne
;
4554 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4557 DNPRINTF(SWM_D_EVENT
, "move: window: 0x%x, floating: %s, transient: "
4558 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4560 /* in max_stack mode should only move transients */
4561 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4565 if (win
->floating
== 0 && !win
->transient
) {
4566 store_float_geom(win
, r
);
4567 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4570 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4577 case SWM_ARG_ID_MOVELEFT
:
4578 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4581 case SWM_ARG_ID_MOVERIGHT
:
4582 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4585 case SWM_ARG_ID_MOVEUP
:
4586 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4589 case SWM_ARG_ID_MOVEDOWN
:
4590 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4597 constrain_window(win
, r
, 0);
4599 store_float_geom(win
, r
);
4603 cursor_font
= xcb_generate_id(conn
);
4604 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
4606 cursor
= xcb_generate_id(conn
);
4607 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
4608 XC_fleur
, XC_fleur
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
4610 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
4611 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
4612 XCB_WINDOW_NONE
, cursor
, XCB_CURRENT_TIME
);
4614 /* get cursor offset from window root */
4615 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4618 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4619 xcb_free_cursor(conn
, cursor
);
4620 xcb_close_font(conn
, cursor_font
);
4626 while ((evt
= xcb_wait_for_event(conn
)) && moving
) {
4627 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
4628 case XCB_BUTTON_RELEASE
:
4629 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
4632 case XCB_MOTION_NOTIFY
:
4633 mne
= (xcb_motion_notify_event_t
*)evt
;
4634 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
4635 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
4637 constrain_window(win
, r
, 0);
4639 /* not free, don't sync more than 120 times / second */
4640 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
4641 timestamp
= mne
->time
;
4656 store_float_geom(win
, r
);
4658 xcb_free_cursor(conn
, cursor
);
4659 xcb_close_font(conn
, cursor_font
);
4660 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4661 DNPRINTF(SWM_D_EVENT
, "move: done\n");
4665 move_step(struct swm_region
*r
, union arg
*args
)
4667 struct ws_win
*win
= NULL
;
4669 if (r
&& r
->ws
&& r
->ws
->focus
)
4674 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4681 /* user/key callable function IDs */
4731 KF_SEARCH_WORKSPACE
,
4772 KF_DUMPWINS
, /* MUST BE LAST */
4776 /* key definitions */
4778 char name
[SWM_FUNCNAME_LEN
];
4779 void (*func
)(struct swm_region
*r
, union arg
*);
4781 } keyfuncs
[KF_INVALID
+ 1] = {
4782 /* name function argument */
4783 { "bar_toggle", bar_toggle
, {0} },
4784 { "button2", pressbutton
, {2} },
4785 { "cycle_layout", cycle_layout
, {0} },
4786 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4787 { "float_toggle", floating_toggle
,{0} },
4788 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4789 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4790 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4791 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4792 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4793 { "iconify", iconify
, {0} },
4794 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4795 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4796 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4797 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4798 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4799 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4800 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4801 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4802 { "mvws_1", send_to_ws
, {.id
= 0} },
4803 { "mvws_2", send_to_ws
, {.id
= 1} },
4804 { "mvws_3", send_to_ws
, {.id
= 2} },
4805 { "mvws_4", send_to_ws
, {.id
= 3} },
4806 { "mvws_5", send_to_ws
, {.id
= 4} },
4807 { "mvws_6", send_to_ws
, {.id
= 5} },
4808 { "mvws_7", send_to_ws
, {.id
= 6} },
4809 { "mvws_8", send_to_ws
, {.id
= 7} },
4810 { "mvws_9", send_to_ws
, {.id
= 8} },
4811 { "mvws_10", send_to_ws
, {.id
= 9} },
4812 { "mvws_11", send_to_ws
, {.id
= 10} },
4813 { "mvws_12", send_to_ws
, {.id
= 11} },
4814 { "mvws_13", send_to_ws
, {.id
= 12} },
4815 { "mvws_14", send_to_ws
, {.id
= 13} },
4816 { "mvws_15", send_to_ws
, {.id
= 14} },
4817 { "mvws_16", send_to_ws
, {.id
= 15} },
4818 { "mvws_17", send_to_ws
, {.id
= 16} },
4819 { "mvws_18", send_to_ws
, {.id
= 17} },
4820 { "mvws_19", send_to_ws
, {.id
= 18} },
4821 { "mvws_20", send_to_ws
, {.id
= 19} },
4822 { "mvws_21", send_to_ws
, {.id
= 20} },
4823 { "mvws_22", send_to_ws
, {.id
= 21} },
4824 { "name_workspace", name_workspace
, {0} },
4825 { "quit", quit
, {0} },
4826 { "raise_toggle", raise_toggle
, {0} },
4827 { "restart", restart
, {0} },
4828 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4829 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4830 { "search_win", search_win
, {0} },
4831 { "search_workspace", search_workspace
, {0} },
4832 { "spawn_custom", NULL
, {0} },
4833 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4834 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4835 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4836 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4837 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4838 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4839 { "uniconify", uniconify
, {0} },
4840 { "version", version
, {0} },
4841 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4842 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4843 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4844 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4845 { "ws_1", switchws
, {.id
= 0} },
4846 { "ws_2", switchws
, {.id
= 1} },
4847 { "ws_3", switchws
, {.id
= 2} },
4848 { "ws_4", switchws
, {.id
= 3} },
4849 { "ws_5", switchws
, {.id
= 4} },
4850 { "ws_6", switchws
, {.id
= 5} },
4851 { "ws_7", switchws
, {.id
= 6} },
4852 { "ws_8", switchws
, {.id
= 7} },
4853 { "ws_9", switchws
, {.id
= 8} },
4854 { "ws_10", switchws
, {.id
= 9} },
4855 { "ws_11", switchws
, {.id
= 10} },
4856 { "ws_12", switchws
, {.id
= 11} },
4857 { "ws_13", switchws
, {.id
= 12} },
4858 { "ws_14", switchws
, {.id
= 13} },
4859 { "ws_15", switchws
, {.id
= 14} },
4860 { "ws_16", switchws
, {.id
= 15} },
4861 { "ws_17", switchws
, {.id
= 16} },
4862 { "ws_18", switchws
, {.id
= 17} },
4863 { "ws_19", switchws
, {.id
= 18} },
4864 { "ws_20", switchws
, {.id
= 19} },
4865 { "ws_21", switchws
, {.id
= 20} },
4866 { "ws_22", switchws
, {.id
= 21} },
4867 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4868 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4869 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4870 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4871 { "ws_prior", priorws
, {0} },
4872 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4873 { "invalid key func", NULL
, {0} },
4876 RB_ENTRY(key
) entry
;
4879 enum keyfuncid funcid
;
4882 RB_HEAD(key_tree
, key
);
4885 key_cmp(struct key
*kp1
, struct key
*kp2
)
4887 if (kp1
->keysym
< kp2
->keysym
)
4889 if (kp1
->keysym
> kp2
->keysym
)
4892 if (kp1
->mod
< kp2
->mod
)
4894 if (kp1
->mod
> kp2
->mod
)
4900 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
4901 struct key_tree keys
;
4904 enum { client_click
, root_click
};
4906 unsigned int action
;
4908 unsigned int button
;
4909 void (*func
)(struct ws_win
*, union arg
*);
4912 /* action key mouse button func args */
4913 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4914 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4915 { client_click
, MODKEY
, Button1
, move
, {0} },
4919 update_modkey(unsigned int mod
)
4925 RB_FOREACH(kp
, key_tree
, &keys
)
4926 if (kp
->mod
& ShiftMask
)
4927 kp
->mod
= mod
| ShiftMask
;
4931 for (i
= 0; i
< LENGTH(buttons
); i
++)
4932 if (buttons
[i
].mask
& ShiftMask
)
4933 buttons
[i
].mask
= mod
| ShiftMask
;
4935 buttons
[i
].mask
= mod
;
4940 TAILQ_ENTRY(spawn_prog
) entry
;
4945 TAILQ_HEAD(spawn_list
, spawn_prog
);
4946 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4949 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4952 struct spawn_prog
*prog
= NULL
;
4954 char *ap
, **real_args
;
4956 /* suppress unused warning since var is needed */
4959 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4962 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4963 if (!strcasecmp(spawn_name
, prog
->name
))
4967 warnx("spawn_custom: program %s not found", spawn_name
);
4971 /* make room for expanded args */
4972 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4973 err(1, "spawn_custom: calloc real_args");
4975 /* expand spawn_args into real_args */
4976 for (i
= 0; i
< prog
->argc
; i
++) {
4978 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4979 if (!strcasecmp(ap
, "$bar_border")) {
4981 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4983 err(1, "spawn_custom border color");
4984 } else if (!strcasecmp(ap
, "$bar_color")) {
4986 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4988 err(1, "spawn_custom bar color");
4989 } else if (!strcasecmp(ap
, "$bar_font")) {
4990 if ((real_args
[i
] = strdup(bar_fonts
))
4992 err(1, "spawn_custom bar fonts");
4993 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4995 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4997 err(1, "spawn_custom color font");
4998 } else if (!strcasecmp(ap
, "$color_focus")) {
5000 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
5002 err(1, "spawn_custom color focus");
5003 } else if (!strcasecmp(ap
, "$color_unfocus")) {
5005 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
5007 err(1, "spawn_custom color unfocus");
5009 /* no match --> copy as is */
5010 if ((real_args
[i
] = strdup(ap
)) == NULL
)
5011 err(1, "spawn_custom strdup(ap)");
5013 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
5018 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
5019 for (i
= 0; i
< prog
->argc
; i
++)
5020 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
5021 DNPRINTF(SWM_D_SPAWN
, "\n");
5023 *ret_args
= real_args
;
5024 return (prog
->argc
);
5028 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
5034 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5038 spawn(r
->ws
->idx
, &a
, 1);
5040 for (i
= 0; i
< spawn_argc
; i
++)
5046 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
5052 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
5056 if (pipe(select_list_pipe
) == -1)
5057 err(1, "pipe error");
5058 if (pipe(select_resp_pipe
) == -1)
5059 err(1, "pipe error");
5061 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
5062 err(1, "could not disable SIGPIPE");
5063 switch (*pid
= fork()) {
5065 err(1, "cannot fork");
5068 if (dup2(select_list_pipe
[0], 0) == -1)
5070 if (dup2(select_resp_pipe
[1], 1) == -1)
5072 close(select_list_pipe
[1]);
5073 close(select_resp_pipe
[0]);
5074 spawn(r
->ws
->idx
, &a
, 0);
5076 default: /* parent */
5077 close(select_list_pipe
[0]);
5078 close(select_resp_pipe
[1]);
5082 for (i
= 0; i
< spawn_argc
; i
++)
5088 spawn_insert(char *name
, char *args
)
5090 char *arg
, *cp
, *ptr
;
5091 struct spawn_prog
*sp
;
5093 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
5095 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
5096 err(1, "spawn_insert: calloc");
5097 if ((sp
->name
= strdup(name
)) == NULL
)
5098 err(1, "spawn_insert: strdup");
5100 /* convert the arguments to an argument list */
5101 if ((ptr
= cp
= strdup(args
)) == NULL
)
5102 err(1, "spawn_insert: strdup");
5103 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
5104 /* empty field; skip it */
5109 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
5110 sizeof *sp
->argv
)) == NULL
)
5111 err(1, "spawn_insert: realloc");
5112 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
5113 err(1, "spawn_insert: strdup");
5117 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
5118 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
5122 spawn_remove(struct spawn_prog
*sp
)
5126 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
5128 TAILQ_REMOVE(&spawns
, sp
, entry
);
5129 for (i
= 0; i
< sp
->argc
; i
++)
5135 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
5139 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
5141 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
5144 spawn_insert(name
, args
);
5146 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
5150 setspawn(char *name
, char *args
)
5152 struct spawn_prog
*sp
;
5154 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5159 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5160 if (!strcmp(sp
->name
, name
)) {
5164 spawn_replace(sp
, name
, args
);
5165 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5169 if (*args
== '\0') {
5170 warnx("error: setspawn: cannot find program: %s", name
);
5174 spawn_insert(name
, args
);
5175 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5179 setconfspawn(char *selector
, char *value
, int flags
)
5183 /* suppress unused warning since var is needed */
5186 args
= expand_tilde(value
);
5188 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
5190 setspawn(selector
, args
);
5193 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
5200 setconfspawn("term", "xterm", 0);
5201 setconfspawn("spawn_term", "xterm", 0);
5202 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5203 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5204 setconfspawn("lock", "xlock", 0);
5205 setconfspawn("initscr", "initscreen.sh", 0);
5206 setconfspawn("menu", "dmenu_run"
5209 " -nf $bar_font_color"
5211 " -sf $bar_color", 0);
5212 setconfspawn("search", "dmenu"
5216 " -nf $bar_font_color"
5218 " -sf $bar_color", 0);
5219 setconfspawn("name_workspace", "dmenu"
5223 " -nf $bar_font_color"
5225 " -sf $bar_color", 0);
5229 #define SWM_MODNAME_SIZE 32
5230 #define SWM_KEY_WS "\n+ \t"
5232 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5236 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5237 if (mod
== NULL
|| ks
== NULL
) {
5238 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5241 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5242 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5246 *ks
= XCB_NO_SYMBOL
;
5248 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5249 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5251 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5252 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5254 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5256 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5258 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5260 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5262 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5264 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5265 *mod
|= ControlMask
;
5267 *ks
= XStringToKeysym(name
);
5268 XConvertCase(*ks
, ks
, &uks
);
5269 if (ks
== XCB_NO_SYMBOL
) {
5271 "parsekeys: invalid key %s\n",
5277 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5282 strdupsafe(char *str
)
5287 return (strdup(str
));
5291 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5295 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5296 keyfuncs
[kfid
].name
, spawn_name
);
5298 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5299 err(1, "key_insert: malloc");
5304 kp
->spawn_name
= strdupsafe(spawn_name
);
5305 RB_INSERT(key_tree
, &keys
, kp
);
5307 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5311 key_lookup(unsigned int mod
, KeySym ks
)
5318 return (RB_FIND(key_tree
, &keys
, &kp
));
5322 key_remove(struct key
*kp
)
5324 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5326 RB_REMOVE(key_tree
, &keys
, kp
);
5327 free(kp
->spawn_name
);
5330 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5334 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5337 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5341 key_insert(mod
, ks
, kfid
, spawn_name
);
5343 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5347 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5352 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5353 keyfuncs
[kfid
].name
, spawn_name
);
5355 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5356 if (kfid
== KF_INVALID
)
5359 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5360 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5363 if (kfid
== KF_INVALID
) {
5364 warnx("error: setkeybinding: cannot find mod/key combination");
5365 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5369 key_insert(mod
, ks
, kfid
, spawn_name
);
5370 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5374 setconfbinding(char *selector
, char *value
, int flags
)
5376 enum keyfuncid kfid
;
5379 struct spawn_prog
*sp
;
5381 /* suppress unused warning since var is needed */
5384 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5385 if (selector
== NULL
) {
5386 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5387 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5389 setkeybinding(mod
, ks
, kfid
, NULL
);
5394 /* search by key function name */
5395 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
5396 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5397 SWM_FUNCNAME_LEN
) == 0) {
5398 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5400 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5401 setkeybinding(mod
, ks
, kfid
, NULL
);
5407 /* search by custom spawn name */
5408 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5409 if (strcasecmp(selector
, sp
->name
) == 0) {
5410 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5412 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5413 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
5420 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5427 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
5428 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
5429 setkeybinding(MODKEY
|ShiftMask
, XK_space
, KF_STACK_RESET
, NULL
);
5430 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
5431 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
5432 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
5433 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
5434 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, KF_STACK_INC
, NULL
);
5435 setkeybinding(MODKEY
|ShiftMask
, XK_period
, KF_STACK_DEC
, NULL
);
5436 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
5437 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
5438 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
5439 setkeybinding(MODKEY
|ShiftMask
, XK_j
, KF_SWAP_NEXT
, NULL
);
5440 setkeybinding(MODKEY
|ShiftMask
, XK_k
, KF_SWAP_PREV
, NULL
);
5441 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
5442 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
5443 setkeybinding(MODKEY
|ShiftMask
, XK_q
, KF_QUIT
, NULL
);
5444 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
5445 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
5446 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
5447 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
5448 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
5449 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
5450 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
5451 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
5452 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
5453 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
5454 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
5455 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
5456 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
5457 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
5458 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
5459 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
5460 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
5461 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
5462 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
5463 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
5464 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
5465 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
5466 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
5467 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
5468 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
5469 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
5470 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
5471 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
5472 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
5473 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, KF_SCREEN_NEXT
, NULL
);
5474 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, KF_SCREEN_PREV
, NULL
);
5475 setkeybinding(MODKEY
|ShiftMask
, XK_1
, KF_MVWS_1
, NULL
);
5476 setkeybinding(MODKEY
|ShiftMask
, XK_2
, KF_MVWS_2
, NULL
);
5477 setkeybinding(MODKEY
|ShiftMask
, XK_3
, KF_MVWS_3
, NULL
);
5478 setkeybinding(MODKEY
|ShiftMask
, XK_4
, KF_MVWS_4
, NULL
);
5479 setkeybinding(MODKEY
|ShiftMask
, XK_5
, KF_MVWS_5
, NULL
);
5480 setkeybinding(MODKEY
|ShiftMask
, XK_6
, KF_MVWS_6
, NULL
);
5481 setkeybinding(MODKEY
|ShiftMask
, XK_7
, KF_MVWS_7
, NULL
);
5482 setkeybinding(MODKEY
|ShiftMask
, XK_8
, KF_MVWS_8
, NULL
);
5483 setkeybinding(MODKEY
|ShiftMask
, XK_9
, KF_MVWS_9
, NULL
);
5484 setkeybinding(MODKEY
|ShiftMask
, XK_0
, KF_MVWS_10
, NULL
);
5485 setkeybinding(MODKEY
|ShiftMask
, XK_F1
, KF_MVWS_11
, NULL
);
5486 setkeybinding(MODKEY
|ShiftMask
, XK_F2
, KF_MVWS_12
, NULL
);
5487 setkeybinding(MODKEY
|ShiftMask
, XK_F3
, KF_MVWS_13
, NULL
);
5488 setkeybinding(MODKEY
|ShiftMask
, XK_F4
, KF_MVWS_14
, NULL
);
5489 setkeybinding(MODKEY
|ShiftMask
, XK_F5
, KF_MVWS_15
, NULL
);
5490 setkeybinding(MODKEY
|ShiftMask
, XK_F6
, KF_MVWS_16
, NULL
);
5491 setkeybinding(MODKEY
|ShiftMask
, XK_F7
, KF_MVWS_17
, NULL
);
5492 setkeybinding(MODKEY
|ShiftMask
, XK_F8
, KF_MVWS_18
, NULL
);
5493 setkeybinding(MODKEY
|ShiftMask
, XK_F9
, KF_MVWS_19
, NULL
);
5494 setkeybinding(MODKEY
|ShiftMask
, XK_F10
, KF_MVWS_20
, NULL
);
5495 setkeybinding(MODKEY
|ShiftMask
, XK_F11
, KF_MVWS_21
, NULL
);
5496 setkeybinding(MODKEY
|ShiftMask
, XK_F12
, KF_MVWS_22
, NULL
);
5497 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
5498 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
5499 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
5500 setkeybinding(MODKEY
|ShiftMask
, XK_x
, KF_WIND_KILL
, NULL
);
5501 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
5502 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
5503 setkeybinding(MODKEY
|ShiftMask
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
5504 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
5505 setkeybinding(MODKEY
|ShiftMask
, XK_v
, KF_VERSION
, NULL
);
5506 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
5507 setkeybinding(MODKEY
|ShiftMask
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
5508 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
5509 setkeybinding(MODKEY
|ShiftMask
, XK_w
, KF_UNICONIFY
, NULL
);
5510 setkeybinding(MODKEY
|ShiftMask
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
5511 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
5512 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
5513 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
5514 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
5515 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
5516 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
5517 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
5518 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
5519 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
5520 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
5521 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
5522 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
5524 setkeybinding(MODKEY
|ShiftMask
, XK_d
, KF_DUMPWINS
, NULL
);
5533 while (RB_EMPTY(&keys
) == 0) {
5534 kp
= RB_ROOT(&keys
);
5540 setkeymapping(char *selector
, char *value
, int flags
)
5542 char keymapping_file
[PATH_MAX
];
5544 /* suppress unused warnings since vars are needed */
5548 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5549 if (value
[0] == '~')
5550 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5551 pwd
->pw_dir
, &value
[1]);
5553 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5555 /* load new key bindings; if it fails, revert to default bindings */
5556 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5560 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5565 updatenumlockmask(void)
5568 xcb_get_modifier_mapping_reply_t
*modmap_r
;
5569 xcb_keycode_t
*modmap
, kc
, *keycode
;
5571 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
5574 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
5575 xcb_get_modifier_mapping(conn
),
5578 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
5579 for (i
= 0; i
< 8; i
++) {
5580 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
5581 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
5583 keycode
= xcb_key_symbols_get_keycode(syms
,
5586 numlockmask
= (1 << i
);
5599 xcb_keycode_t
*code
;
5600 unsigned int modifiers
[] =
5601 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
5604 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5605 updatenumlockmask();
5607 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
5608 for (k
= 0; k
< num_screens
; k
++) {
5609 if (TAILQ_EMPTY(&screens
[k
].rl
))
5611 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
5613 RB_FOREACH(kp
, key_tree
, &keys
) {
5614 if ((code
= xcb_key_symbols_get_keycode(syms
,
5616 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5617 xcb_grab_key(conn
, 1,
5619 kp
->mod
| modifiers
[j
],
5620 *code
, XCB_GRAB_MODE_ASYNC
,
5621 XCB_GRAB_MODE_ASYNC
);
5628 grabbuttons(struct ws_win
*win
, int focused
)
5631 unsigned int modifiers
[] =
5632 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5634 updatenumlockmask();
5635 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, win
->id
,
5636 XCB_BUTTON_MASK_ANY
);
5638 for (i
= 0; i
< LENGTH(buttons
); i
++)
5639 if (buttons
[i
].action
== client_click
)
5640 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5641 xcb_grab_button(conn
, 0, win
->id
,
5643 XCB_GRAB_MODE_ASYNC
,
5650 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
5651 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_SYNC
, XCB_WINDOW_NONE
,
5652 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_ANY
, XCB_BUTTON_MASK_ANY
);
5655 const char *quirkname
[] = {
5656 "NONE", /* config string for "no value" */
5665 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5666 #define SWM_Q_WS "\n|+ \t"
5668 parsequirks(char *qstr
, unsigned long *quirk
)
5678 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5680 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5681 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5682 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5683 DNPRINTF(SWM_D_QUIRK
,
5684 "parsequirks: %s\n", name
);
5689 *quirk
|= 1 << (i
-1);
5693 if (i
>= LENGTH(quirkname
)) {
5694 DNPRINTF(SWM_D_QUIRK
,
5695 "parsequirks: invalid quirk [%s]\n", name
);
5703 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5707 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5710 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5711 err(1, "quirk_insert: malloc");
5712 if ((qp
->class = strdup(class)) == NULL
)
5713 err(1, "quirk_insert: strdup");
5714 if ((qp
->name
= strdup(name
)) == NULL
)
5715 err(1, "quirk_insert: strdup");
5718 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5720 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5724 quirk_remove(struct quirk
*qp
)
5726 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5727 qp
->name
, qp
->quirk
);
5729 TAILQ_REMOVE(&quirks
, qp
, entry
);
5734 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5738 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5739 unsigned long quirk
)
5741 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5742 qp
->name
, qp
->quirk
);
5745 quirk_insert(class, name
, quirk
);
5747 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5751 setquirk(const char *class, const char *name
, unsigned long quirk
)
5755 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5758 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5759 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5763 quirk_replace(qp
, class, name
, quirk
);
5764 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5769 warnx("error: setquirk: cannot find class/name combination");
5773 quirk_insert(class, name
, quirk
);
5774 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5778 setconfquirk(char *selector
, char *value
, int flags
)
5780 char *cp
, *class, *name
;
5782 unsigned long quirks
;
5784 /* suppress unused warning since var is needed */
5787 if (selector
== NULL
)
5789 if ((cp
= strchr(selector
, ':')) == NULL
)
5794 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5795 setquirk(class, name
, quirks
);
5802 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5803 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5804 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5805 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5806 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5807 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5808 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5809 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5810 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5811 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5812 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5813 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5814 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5817 /* conf file stuff */
5818 #define SWM_CONF_FILE "spectrwm.conf"
5819 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5823 SWM_S_BAR_AT_BOTTOM
,
5824 SWM_S_BAR_BORDER_WIDTH
,
5831 SWM_S_CLOCK_ENABLED
,
5834 SWM_S_CYCLE_VISIBLE
,
5836 SWM_S_DISABLE_BORDER
,
5838 SWM_S_FOCUS_CLOSE_WRAP
,
5839 SWM_S_FOCUS_DEFAULT
,
5845 SWM_S_STACK_ENABLED
,
5847 SWM_S_TITLE_CLASS_ENABLED
,
5848 SWM_S_TITLE_NAME_ENABLED
,
5849 SWM_S_URGENT_ENABLED
,
5850 SWM_S_VERBOSE_LAYOUT
,
5851 SWM_S_WINDOW_NAME_ENABLED
,
5852 SWM_S_WORKSPACE_LIMIT
5856 setconfvalue(char *selector
, char *value
, int flags
)
5861 /* suppress unused warning since var is needed */
5865 case SWM_S_BAR_ACTION
:
5867 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
5868 err(1, "setconfvalue: bar_action");
5870 case SWM_S_BAR_AT_BOTTOM
:
5871 bar_at_bottom
= atoi(value
);
5873 case SWM_S_BAR_BORDER_WIDTH
:
5874 bar_border_width
= atoi(value
);
5875 if (bar_border_width
< 0)
5876 bar_border_width
= 0;
5878 case SWM_S_BAR_DELAY
:
5879 bar_delay
= atoi(value
);
5881 case SWM_S_BAR_ENABLED
:
5882 bar_enabled
= atoi(value
);
5884 case SWM_S_BAR_FONT
:
5886 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5887 err(1, "setconfvalue: asprintf: failed to allocate "
5888 "memory for bar_fonts.");
5891 case SWM_S_BAR_FORMAT
:
5893 if ((bar_format
= strdup(value
)) == NULL
)
5894 err(1, "setconfvalue: bar_format");
5896 case SWM_S_BAR_JUSTIFY
:
5897 if (!strcmp(value
, "left"))
5898 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5899 else if (!strcmp(value
, "center"))
5900 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5901 else if (!strcmp(value
, "right"))
5902 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5904 errx(1, "invalid bar_justify");
5906 case SWM_S_BORDER_WIDTH
:
5907 border_width
= atoi(value
);
5908 if (border_width
< 0)
5911 case SWM_S_CLOCK_ENABLED
:
5912 clock_enabled
= atoi(value
);
5914 case SWM_S_CLOCK_FORMAT
:
5915 #ifndef SWM_DENY_CLOCK_FORMAT
5917 if ((clock_format
= strdup(value
)) == NULL
)
5918 err(1, "setconfvalue: clock_format");
5921 case SWM_S_CYCLE_EMPTY
:
5922 cycle_empty
= atoi(value
);
5924 case SWM_S_CYCLE_VISIBLE
:
5925 cycle_visible
= atoi(value
);
5927 case SWM_S_DIALOG_RATIO
:
5928 dialog_ratio
= atof(value
);
5929 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5932 case SWM_S_DISABLE_BORDER
:
5933 disable_border
= atoi(value
);
5935 case SWM_S_FOCUS_CLOSE
:
5936 if (!strcmp(value
, "first"))
5937 focus_close
= SWM_STACK_BOTTOM
;
5938 else if (!strcmp(value
, "last"))
5939 focus_close
= SWM_STACK_TOP
;
5940 else if (!strcmp(value
, "next"))
5941 focus_close
= SWM_STACK_ABOVE
;
5942 else if (!strcmp(value
, "previous"))
5943 focus_close
= SWM_STACK_BELOW
;
5945 errx(1, "focus_close");
5947 case SWM_S_FOCUS_CLOSE_WRAP
:
5948 focus_close_wrap
= atoi(value
);
5950 case SWM_S_FOCUS_DEFAULT
:
5951 if (!strcmp(value
, "last"))
5952 focus_default
= SWM_STACK_TOP
;
5953 else if (!strcmp(value
, "first"))
5954 focus_default
= SWM_STACK_BOTTOM
;
5956 errx(1, "focus_default");
5958 case SWM_S_FOCUS_MODE
:
5959 if (!strcmp(value
, "default"))
5960 focus_mode
= SWM_FOCUS_DEFAULT
;
5961 else if (!strcmp(value
, "follow_cursor"))
5962 focus_mode
= SWM_FOCUS_FOLLOW
;
5963 else if (!strcmp(value
, "synergy"))
5964 focus_mode
= SWM_FOCUS_SYNERGY
;
5966 errx(1, "focus_mode");
5968 case SWM_S_SPAWN_ORDER
:
5969 if (!strcmp(value
, "first"))
5970 spawn_position
= SWM_STACK_BOTTOM
;
5971 else if (!strcmp(value
, "last"))
5972 spawn_position
= SWM_STACK_TOP
;
5973 else if (!strcmp(value
, "next"))
5974 spawn_position
= SWM_STACK_ABOVE
;
5975 else if (!strcmp(value
, "previous"))
5976 spawn_position
= SWM_STACK_BELOW
;
5978 errx(1, "spawn_position");
5980 case SWM_S_SPAWN_TERM
:
5981 setconfspawn("term", value
, 0);
5982 setconfspawn("spawn_term", value
, 0);
5986 case SWM_S_SS_ENABLED
:
5987 ss_enabled
= atoi(value
);
5989 case SWM_S_STACK_ENABLED
:
5990 stack_enabled
= atoi(value
);
5992 case SWM_S_TERM_WIDTH
:
5993 term_width
= atoi(value
);
5997 case SWM_S_TITLE_CLASS_ENABLED
:
5998 title_class_enabled
= atoi(value
);
6000 case SWM_S_TITLE_NAME_ENABLED
:
6001 title_name_enabled
= atoi(value
);
6003 case SWM_S_URGENT_ENABLED
:
6004 urgent_enabled
= atoi(value
);
6006 case SWM_S_VERBOSE_LAYOUT
:
6007 verbose_layout
= atoi(value
);
6008 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
6010 layouts
[i
].l_string
= fancy_stacker
;
6012 layouts
[i
].l_string
= plain_stacker
;
6015 case SWM_S_WINDOW_NAME_ENABLED
:
6016 window_name_enabled
= atoi(value
);
6018 case SWM_S_WORKSPACE_LIMIT
:
6019 workspace_limit
= atoi(value
);
6020 if (workspace_limit
> SWM_WS_MAX
)
6021 workspace_limit
= SWM_WS_MAX
;
6022 else if (workspace_limit
< 1)
6023 workspace_limit
= 1;
6032 setconfmodkey(char *selector
, char *value
, int flags
)
6034 /* suppress unused warnings since vars are needed */
6038 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
6039 update_modkey(Mod1Mask
);
6040 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
6041 update_modkey(Mod2Mask
);
6042 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
6043 update_modkey(Mod3Mask
);
6044 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
6045 update_modkey(Mod4Mask
);
6052 setconfcolor(char *selector
, char *value
, int flags
)
6054 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
6059 setconfregion(char *selector
, char *value
, int flags
)
6061 /* suppress unused warnings since vars are needed */
6065 custom_region(value
);
6070 setautorun(char *selector
, char *value
, int flags
)
6080 /* suppress unused warnings since vars are needed */
6084 if (getenv("SWM_STARTED"))
6088 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
6089 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
6091 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6092 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
6095 * This is a little intricate
6097 * If the pid already exists we simply reuse it because it means it was
6098 * used before AND not claimed by manage_window. We get away with
6099 * altering it in the parent after INSERT because this can not be a race
6102 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
6105 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
6107 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
6108 err(1, "setautorun: realloc");
6109 a
.argv
[argc
- 1] = ap
;
6112 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
6113 err(1, "setautorun: realloc");
6114 a
.argv
[argc
] = NULL
;
6116 if ((pid
= fork()) == 0) {
6117 spawn(ws_id
, &a
, 1);
6126 p
= calloc(1, sizeof *p
);
6129 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
6139 setlayout(char *selector
, char *value
, int flags
)
6141 int ws_id
, i
, x
, mg
, ma
, si
, raise
, f
= 0;
6142 int st
= SWM_V_STACK
, num_screens
;
6144 struct workspace
*ws
;
6146 /* suppress unused warnings since vars are needed */
6150 if (getenv("SWM_STARTED"))
6154 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
6155 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
6156 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6157 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6160 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6161 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
6163 if (!strcasecmp(s
, "vertical"))
6165 else if (!strcasecmp(s
, "vertical_flip")) {
6168 } else if (!strcasecmp(s
, "horizontal"))
6170 else if (!strcasecmp(s
, "horizontal_flip")) {
6173 } else if (!strcasecmp(s
, "fullscreen"))
6176 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6177 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6180 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6181 for (i
= 0; i
< num_screens
; i
++) {
6182 ws
= (struct workspace
*)&screens
[i
].ws
;
6183 ws
[ws_id
].cur_layout
= &layouts
[st
];
6185 ws
[ws_id
].always_raise
= raise
;
6186 if (st
== SWM_MAX_STACK
)
6190 for (x
= 0; x
< abs(mg
); x
++) {
6191 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6192 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
6193 SWM_ARG_ID_MASTERSHRINK
);
6197 for (x
= 0; x
< abs(ma
); x
++) {
6198 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6199 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
6200 SWM_ARG_ID_MASTERDEL
);
6204 for (x
= 0; x
< abs(si
); x
++) {
6205 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6206 si
>= 0 ? SWM_ARG_ID_STACKINC
:
6207 SWM_ARG_ID_STACKDEC
);
6212 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6213 SWM_ARG_ID_FLIPLAYOUT
);
6221 /* config options */
6222 struct config_option
{
6224 int (*func
)(char*, char*, int);
6227 struct config_option configopt
[] = {
6228 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
6229 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
6230 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
6231 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
6232 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
6233 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
6234 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
6235 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
6236 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
6237 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
6238 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
6239 { "keyboard_mapping", setkeymapping
, 0 },
6240 { "bind", setconfbinding
, 0 },
6241 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
6242 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
6243 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
6244 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
6245 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
6246 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
6247 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
6248 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
6249 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
6250 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
6251 { "modkey", setconfmodkey
, 0 },
6252 { "program", setconfspawn
, 0 },
6253 { "quirk", setconfquirk
, 0 },
6254 { "region", setconfregion
, 0 },
6255 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
6256 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
6257 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
6258 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
6259 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
6260 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
6261 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6262 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6263 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6264 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6265 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6266 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6267 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6268 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6269 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6270 { "autorun", setautorun
, 0 },
6271 { "layout", setlayout
, 0 },
6276 conf_load(char *filename
, int keymapping
)
6279 char *line
, *cp
, *optsub
, *optval
;
6280 size_t linelen
, lineno
= 0;
6281 int wordlen
, i
, optind
;
6282 struct config_option
*opt
;
6284 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6286 if (filename
== NULL
) {
6287 warnx("conf_load: no filename");
6290 if ((config
= fopen(filename
, "r")) == NULL
) {
6291 warn("conf_load: fopen: %s", filename
);
6295 while (!feof(config
)) {
6296 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6299 err(1, "%s", filename
);
6304 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6305 if (cp
[0] == '\0') {
6310 /* get config option */
6311 wordlen
= strcspn(cp
, "=[ \t\n");
6313 warnx("%s: line %zd: no option found",
6318 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6319 opt
= &configopt
[i
];
6320 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6321 (int)strlen(opt
->optname
) == wordlen
) {
6327 warnx("%s: line %zd: unknown option %.*s",
6328 filename
, lineno
, wordlen
, cp
);
6331 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
6332 warnx("%s: line %zd: invalid option %.*s",
6333 filename
, lineno
, wordlen
, cp
);
6337 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6338 /* get [selector] if any */
6342 wordlen
= strcspn(cp
, "]");
6345 warnx("%s: line %zd: syntax error",
6350 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6352 warnx("%s: line %zd: unable to allocate"
6353 "memory for selector", filename
,
6359 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6361 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6363 optval
= strdup(cp
);
6364 /* call function to deal with it all */
6365 if (configopt
[optind
].func(optsub
, optval
,
6366 configopt
[optind
].funcflags
) != 0)
6367 errx(1, "%s: line %zd: invalid data for %s",
6368 filename
, lineno
, configopt
[optind
].optname
);
6375 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6382 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6388 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
6390 struct ws_win
*parent
, *w
;
6391 struct swm_region
*r
;
6392 struct workspace
*ws
;
6393 xcb_icccm_wm_hints_t wmh
;
6395 parent
= find_window(win
->transient
);
6397 parent
->child_trans
= win
;
6399 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6400 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
6402 r
= root_to_region(win
->wa
->root
);
6404 /* parent doen't exist in our window list */
6405 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6406 if (xcb_icccm_get_wm_hints_reply(conn
,
6407 xcb_icccm_get_wm_hints(conn
, w
->id
),
6409 warnx("can't get hints for 0x%x", w
->id
);
6413 if (win
->hints
.window_group
!= wmh
.window_group
)
6416 w
->child_trans
= win
;
6417 win
->transient
= w
->id
;
6419 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
6420 "transient to 0x%x\n", win
->transient
);
6427 window_get_pid(xcb_window_t win
)
6432 xcb_get_property_cookie_t pc
;
6433 xcb_get_property_reply_t
*pr
;
6435 apid
= get_atom_from_string("_NET_WM_PID");
6436 if (apid
== XCB_ATOM_NONE
)
6439 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
6440 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6443 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
6448 if (pr
->type
== apid
&& pr
->format
== 32)
6449 ret
= *((pid_t
*)xcb_get_property_value(pr
));
6455 apid
= get_atom_from_string("_SWM_PID");
6456 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
6458 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6461 if (pr
->type
!= apid
) {
6466 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
6473 get_ws_idx(xcb_window_t id
)
6479 xcb_get_property_reply_t
*gpr
;
6481 gpr
= xcb_get_property_reply(conn
,
6482 xcb_get_property(conn
, 0, id
, a_swm_ws
,
6483 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
6488 proplen
= xcb_get_property_value_length(gpr
);
6490 prop
= malloc(proplen
+ 1);
6493 xcb_get_property_value(gpr
),
6495 prop
[proplen
] = '\0';
6502 DNPRINTF(SWM_D_PROP
, "get_ws_idx: _SWM_WS: %s\n", prop
);
6503 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
6505 DNPRINTF(SWM_D_PROP
, "get_ws_idx: window: #%s: %s",
6515 manage_window(xcb_window_t id
)
6517 xcb_window_t trans
= XCB_WINDOW_NONE
;
6518 struct workspace
*ws
;
6519 struct ws_win
*win
, *ww
;
6520 int ws_idx
, border_me
= 0;
6521 char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
6522 struct swm_region
*r
;
6525 uint32_t event_mask
, i
;
6526 xcb_icccm_get_wm_protocols_reply_t wpr
;
6528 if ((win
= find_window(id
)) != NULL
)
6529 return (win
); /* already being managed */
6531 /* see if we are on the unmanaged list */
6532 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6533 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
6534 "window: 0x%x\n", win
->id
);
6535 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6537 set_child_transient(win
, &trans
);
6539 if (trans
&& (ww
= find_window(trans
)))
6540 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
6541 else if ((ww
= win
->ws
->focus
) &&
6542 spawn_position
== SWM_STACK_ABOVE
)
6543 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
,
6545 else if (ww
&& spawn_position
== SWM_STACK_BELOW
)
6546 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6547 else switch (spawn_position
) {
6550 case SWM_STACK_ABOVE
:
6551 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6553 case SWM_STACK_BOTTOM
:
6554 case SWM_STACK_BELOW
:
6555 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6558 ewmh_update_actions(win
);
6562 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6563 err(1, "manage_window: calloc: failed to allocate memory for "
6569 /* see if we need to override the workspace */
6570 p
= find_pid(window_get_pid(id
));
6572 /* Get all the window data in one shot */
6573 ws_idx
= get_ws_idx(id
);
6575 win
->wa
= xcb_get_geometry_reply(conn
,
6576 xcb_get_geometry(conn
, id
),
6578 xcb_icccm_get_wm_normal_hints_reply(conn
,
6579 xcb_icccm_get_wm_normal_hints(conn
, id
),
6581 xcb_icccm_get_wm_hints_reply(conn
,
6582 xcb_icccm_get_wm_hints(conn
, id
),
6584 xcb_icccm_get_wm_transient_for_reply(conn
,
6585 xcb_icccm_get_wm_transient_for(conn
, id
),
6588 win
->transient
= trans
;
6589 set_child_transient(win
, &trans
);
6590 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%x, "
6591 "transient: 0x%x\n", win
->id
, win
->transient
);
6594 /* get supported protocols */
6595 if (xcb_icccm_get_wm_protocols_reply(conn
,
6596 xcb_icccm_get_wm_protocols(conn
, id
, a_prot
),
6598 for (i
= 0; i
< wpr
.atoms_len
; i
++) {
6599 if (wpr
.atoms
[i
] == a_takefocus
)
6600 win
->take_focus
= 1;
6601 if (wpr
.atoms
[i
] == a_delete
)
6602 win
->can_delete
= 1;
6604 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
6607 win
->iconic
= get_iconic(win
);
6610 * Figure out where to put the window. If it was previously assigned to
6611 * a workspace (either by spawn() or manually moving), and isn't
6612 * transient, * put it in the same workspace
6614 r
= root_to_region(win
->wa
->root
);
6616 ws
= &r
->s
->ws
[p
->ws
];
6617 TAILQ_REMOVE(&pidlist
, p
, entry
);
6620 } else if (ws_idx
!= -1 && win
->transient
== 0) {
6621 ws
= &r
->s
->ws
[ws_idx
];
6624 /* this should launch transients in the same ws as parent */
6626 if ((ww
= find_window(trans
)) != NULL
)
6632 warnx("manage_window: fix this "
6638 /* set up the window layout */
6641 win
->s
= r
->s
; /* this never changes */
6642 if (trans
&& (ww
= find_window(trans
)))
6643 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
6644 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
6645 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
6647 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
6648 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6649 else switch (spawn_position
) {
6652 case SWM_STACK_ABOVE
:
6653 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6655 case SWM_STACK_BOTTOM
:
6656 case SWM_STACK_BELOW
:
6657 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6660 /* ignore window border if there is one. */
6661 WIDTH(win
) = win
->wa
->width
;
6662 HEIGHT(win
) = win
->wa
->height
;
6663 X(win
) = win
->wa
->x
+ win
->wa
->border_width
;
6664 Y(win
) = win
->wa
->y
+ win
->wa
->border_width
;
6666 win
->g_floatvalid
= 0;
6667 win
->floatmaxed
= 0;
6668 win
->ewmh_flags
= 0;
6670 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%x, (x,y) w x h: "
6671 "(%d,%d) %d x %d, ws: %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
6672 HEIGHT(win
), ws
->idx
);
6674 constrain_window(win
, r
, 0);
6676 /* Set window properties so we can remember this after reincarnation */
6677 if (prop
== NULL
&& snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
6679 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6681 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
6682 a_swm_ws
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
6688 ewmh_autoquirk(win
);
6690 if (xcb_icccm_get_wm_class_reply(conn
,
6691 xcb_icccm_get_wm_class(conn
, win
->id
),
6693 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6694 win
->ch
.class_name
, win
->ch
.instance_name
);
6696 /* java is retarded so treat it special */
6697 if (strstr(win
->ch
.instance_name
, "sun-awt")) {
6702 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6703 if (!strcmp(win
->ch
.class_name
, qp
->class) &&
6704 !strcmp(win
->ch
.instance_name
, qp
->name
)) {
6705 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
6706 "class: %s, name: %s\n", win
->ch
.class_name
,
6707 win
->ch
.instance_name
);
6708 if (qp
->quirk
& SWM_Q_FLOAT
) {
6712 win
->quirks
= qp
->quirk
;
6717 /* alter window position if quirky */
6718 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6719 win
->manual
= 1; /* don't center the quirky windows */
6720 if (bar_enabled
&& Y(win
) < bar_height
)
6721 Y(win
) = bar_height
;
6722 if (WIDTH(win
) + X(win
) > WIDTH(r
))
6723 X(win
) = WIDTH(r
) - WIDTH(win
) - 2;
6727 /* Reset font sizes (the bruteforce way; no default keybinding). */
6728 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6729 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6730 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
6731 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6732 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
6735 ewmh_get_win_state(win
);
6736 ewmh_update_actions(win
);
6737 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
6742 X(win
) -= border_width
;
6743 Y(win
) -= border_width
;
6747 event_mask
= XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
|
6748 XCB_EVENT_MASK_PROPERTY_CHANGE
| XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
6750 event_mask
|= XCB_EVENT_MASK_LEAVE_WINDOW
;
6753 xcb_change_window_attributes(conn
, id
, XCB_CW_EVENT_MASK
, &event_mask
);
6755 /* floaters need to be mapped if they are in the current workspace */
6756 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
6757 map_window_raised(win
->id
);
6763 free_window(struct ws_win
*win
)
6765 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
6770 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6775 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
6780 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6783 DNPRINTF(SWM_D_MISC
, "free_window: done\n");
6787 unmanage_window(struct ws_win
*win
)
6789 struct ws_win
*parent
;
6790 xcb_screen_t
*screen
;
6795 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
6797 if (win
->transient
) {
6798 parent
= find_window(win
->transient
);
6800 parent
->child_trans
= NULL
;
6803 /* focus on root just in case */
6804 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
6805 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
6806 screen
->root
, XCB_CURRENT_TIME
);
6810 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
6811 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6815 focus_magic(struct ws_win
*win
)
6817 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%x\n", WINID(win
));
6820 /* if there are no windows clear the status-bar */
6825 if (win
->child_trans
) {
6826 /* win = parent & has a transient so focus on that */
6828 focus_win(win
->child_trans
);
6829 if (win
->child_trans
->take_focus
)
6830 client_msg(win
, a_takefocus
);
6832 /* make sure transient hasn't disappeared */
6833 if (validate_win(win
->child_trans
) == 0) {
6834 focus_win(win
->child_trans
);
6835 if (win
->child_trans
->take_focus
)
6836 client_msg(win
->child_trans
,
6839 win
->child_trans
= NULL
;
6841 if (win
->take_focus
)
6842 client_msg(win
, a_takefocus
);
6848 if (win
->take_focus
)
6849 client_msg(win
, a_takefocus
);
6854 expose(xcb_expose_event_t
*e
)
6857 struct swm_region
*r
;
6859 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%x\n", e
->window
);
6861 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6862 for (i
= 0; i
< num_screens
; i
++)
6863 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6864 if (e
->window
== WINID(r
->bar
))
6872 focusin(xcb_focus_in_event_t
*e
)
6874 DNPRINTF(SWM_D_EVENT
, "focusin: window: 0x%x, mode: %s(%u), "
6875 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
6876 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
6881 keypress(xcb_key_press_event_t
*e
)
6883 xcb_keysym_t keysym
;
6886 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
6888 DNPRINTF(SWM_D_EVENT
, "keypress: %u %u\n", e
->detail
, keysym
);
6890 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
6893 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
6894 spawn_custom(root_to_region(e
->root
),
6895 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
6896 else if (keyfuncs
[kp
->funcid
].func
)
6897 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
),
6898 &(keyfuncs
[kp
->funcid
].args
));
6902 buttonpress(xcb_button_press_event_t
*e
)
6906 unsigned int action
;
6908 DNPRINTF(SWM_D_EVENT
, "buttonpress: window 0x%x, detail: %u\n",
6909 e
->event
, e
->detail
);
6911 if ((win
= find_window(e
->event
)) == NULL
)
6915 action
= client_click
;
6917 for (i
= 0; i
< LENGTH(buttons
); i
++)
6918 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6919 buttons
[i
].button
== e
->detail
&&
6920 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
))
6921 buttons
[i
].func(win
, &buttons
[i
].args
);
6928 print_win_geom(xcb_window_t w
)
6930 xcb_get_geometry_reply_t
*wa
;
6932 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
6935 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: 0x%x\n",
6940 DNPRINTF(SWM_D_MISC
, "print_win_geom: window: 0x%x, root: 0x%x, "
6941 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
6942 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
6950 configurerequest(xcb_configure_request_event_t
*e
)
6955 uint32_t wc
[7] = {0};
6957 if ((win
= find_window(e
->window
)) == NULL
)
6958 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
6961 print_win_geom(e
->window
);
6964 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: 0x%x, "
6965 "value_mask: 0x%x", e
->window
, e
->value_mask
);
6966 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
6967 mask
|= XCB_CONFIG_WINDOW_X
;
6969 DPRINTF(", X: %d", e
->x
);
6971 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
6972 mask
|= XCB_CONFIG_WINDOW_Y
;
6974 DPRINTF(", Y: %d", e
->y
);
6976 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
6977 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
6979 DPRINTF(", W: %u", e
->width
);
6981 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
6982 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
6983 wc
[i
++] = e
->height
;
6984 DPRINTF(", H: %u", e
->height
);
6986 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
6987 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6988 wc
[i
++] = e
->border_width
;
6989 DPRINTF(", Border: %u", e
->border_width
);
6991 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
6992 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
6993 wc
[i
++] = e
->sibling
;
6994 DPRINTF(", Sibling: 0x%x", e
->sibling
);
6996 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
6997 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
6998 wc
[i
++] = e
->stack_mode
;
6999 DPRINTF(", StackMode: %u", e
->stack_mode
);
7003 xcb_configure_window(conn
, e
->window
, mask
, wc
);
7005 DPRINTF(", Sent: %s\n", YESNO((mask
!= 0)));
7006 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
7007 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)) {
7008 win
->g_float
.x
= e
->x
- X(win
->ws
->r
);
7009 win
->g_float
.y
= e
->y
- Y(win
->ws
->r
);
7010 win
->g_float
.w
= e
->width
;
7011 win
->g_float
.h
= e
->height
;
7012 win
->g_floatvalid
= 1;
7014 if (win
->floating
) {
7015 win
->g
= win
->g_float
;
7016 win
->g
.x
+= X(win
->ws
->r
);
7017 win
->g
.y
+= Y(win
->ws
->r
);
7030 configurenotify(xcb_configure_notify_event_t
*e
)
7034 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%x\n",
7037 win
= find_window(e
->window
);
7039 xcb_icccm_get_wm_normal_hints_reply(conn
,
7040 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
7049 destroynotify(xcb_destroy_notify_event_t
*e
)
7053 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%x\n", e
->window
);
7055 if ((win
= find_window(e
->window
)) == NULL
) {
7056 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
7062 /* make sure we focus on something */
7065 unmanage_window(win
);
7072 get_notify_detail_label(uint8_t detail
)
7077 case XCB_NOTIFY_DETAIL_ANCESTOR
:
7080 case XCB_NOTIFY_DETAIL_VIRTUAL
:
7083 case XCB_NOTIFY_DETAIL_INFERIOR
:
7086 case XCB_NOTIFY_DETAIL_NONLINEAR
:
7087 label
= "Nonlinear";
7089 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
7090 label
= "NonlinearVirtual";
7092 case XCB_NOTIFY_DETAIL_POINTER
:
7095 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
7096 label
= "PointerRoot";
7098 case XCB_NOTIFY_DETAIL_NONE
:
7109 get_notify_mode_label(uint8_t mode
)
7114 case XCB_NOTIFY_MODE_NORMAL
:
7117 case XCB_NOTIFY_MODE_GRAB
:
7120 case XCB_NOTIFY_MODE_UNGRAB
:
7123 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
7124 label
= "WhileGrabbed";
7135 enternotify(xcb_enter_notify_event_t
*e
)
7138 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%x, mode: %s(%d), "
7139 "detail: %s(%d), root: 0x%x, subwindow: 0x%x, same_screen_focus: "
7140 "%s, state: %d\n", e
->event
, get_notify_mode_label(e
->mode
),
7141 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
, e
->root
,
7142 e
->child
, YESNO(e
->same_screen_focus
), e
->state
);
7144 if (e
->mode
!= XCB_NOTIFY_MODE_NORMAL
) {
7145 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
7150 switch (focus_mode
) {
7151 case SWM_FOCUS_DEFAULT
:
7153 case SWM_FOCUS_FOLLOW
:
7155 case SWM_FOCUS_SYNERGY
:
7159 if ((win
= find_window(e
->event
)) == NULL
) {
7160 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
7171 leavenotify(xcb_leave_notify_event_t
*e
)
7173 DNPRINTF(SWM_D_FOCUS
, "leavenotify: window: 0x%x, mode: %s(%d), "
7174 "detail: %s(%d), root: 0x%x, subwindow: 0x%x, same_screen_focus: "
7175 "%s, state: %d\n", e
->event
, get_notify_mode_label(e
->mode
),
7176 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
, e
->root
,
7177 e
->child
, YESNO(e
->same_screen_focus
), e
->state
);
7181 /* lets us use one switch statement for arbitrary mode/detail combinations */
7182 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
7185 mapnotify(xcb_map_notify_event_t
*e
)
7189 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%x\n", e
->window
);
7191 win
= manage_window(e
->window
);
7193 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
7196 * focus_win can only set input focus on a mapped window.
7197 * make sure the window really has focus since it is just being mapped.
7199 if (win
->ws
->focus
== win
)
7204 mappingnotify(xcb_mapping_notify_event_t
*e
)
7206 xcb_refresh_keyboard_mapping(syms
, e
);
7208 if (e
->request
== XCB_MAPPING_KEYBOARD
)
7213 maprequest(xcb_map_request_event_t
*e
)
7216 struct swm_region
*r
;
7217 xcb_get_window_attributes_reply_t
*war
;
7219 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%x\n",
7222 war
= xcb_get_window_attributes_reply(conn
,
7223 xcb_get_window_attributes(conn
, e
->window
),
7227 if (war
->override_redirect
) {
7233 win
= manage_window(e
->window
);
7235 return; /* can't happen */
7240 /* make new win focused */
7241 r
= root_to_region(win
->wa
->root
);
7242 if (win
->ws
== r
->ws
)
7248 get_atom_name(xcb_atom_t atom
)
7252 xcb_get_atom_name_reply_t
*r
;
7254 r
= xcb_get_atom_name_reply(conn
,
7255 xcb_get_atom_name(conn
, atom
),
7258 len
= xcb_get_atom_name_name_length(r
);
7260 name
= malloc(len
+ 1);
7262 memcpy(name
, xcb_get_atom_name_name(r
), len
);
7274 propertynotify(xcb_property_notify_event_t
*e
)
7280 name
= get_atom_name(e
->atom
);
7281 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%x, atom: %s(%u)\n",
7282 e
->window
, name
, e
->atom
);
7286 win
= find_window(e
->window
);
7290 if (e
->state
== XCB_PROPERTY_DELETE
&& e
->atom
== a_swm_iconic
) {
7291 update_iconic(win
, 0);
7292 map_window_raised(win
->id
);
7299 case XCB_ATOM_WM_CLASS
:
7300 case XCB_ATOM_WM_NAME
:
7309 unmapnotify(xcb_unmap_notify_event_t
*e
)
7313 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%x\n", e
->window
);
7315 /* determine if we need to help unmanage this window */
7316 win
= find_window(e
->window
);
7320 if (getstate(e
->window
) == XCB_ICCCM_WM_STATE_NORMAL
) {
7321 unmanage_window(win
);
7324 /* resend unmap because we ated it */
7325 xcb_unmap_window(conn
, e
->window
);
7332 visibilitynotify(xcb_visibility_notify_event_t
*e
)
7334 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%x\n",
7340 clientmessage(xcb_client_message_event_t
*e
)
7343 xcb_map_request_event_t mre
;
7347 name
= get_atom_name(e
->type
);
7348 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%x, atom: %s(%u)\n",
7349 e
->window
, name
, e
->type
);
7352 win
= find_window(e
->window
);
7355 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7356 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
7357 "unmanaged window.\n");
7358 mre
.window
= e
->window
;
7364 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7365 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7368 if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7369 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7370 if (win
->can_delete
)
7371 client_msg(win
, a_delete
);
7373 xcb_kill_client(conn
, win
->id
);
7375 if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7376 DNPRINTF(SWM_D_EVENT
,
7377 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7378 if (win
->floating
) {
7379 if (e
->data
.data32
[0] & (1<<8)) /* x */
7380 X(win
) = e
->data
.data32
[1];
7381 if (e
->data
.data32
[0] & (1<<9)) /* y */
7382 Y(win
) = e
->data
.data32
[2];
7383 if (e
->data
.data32
[0] & (1<<10)) /* width */
7384 WIDTH(win
) = e
->data
.data32
[3];
7385 if (e
->data
.data32
[0] & (1<<11)) /* height */
7386 HEIGHT(win
) = e
->data
.data32
[4];
7391 /* TODO: Change stack sizes */
7392 /* notify no change was made. */
7393 config_win(win
, NULL
);
7396 if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
7397 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
7398 ewmh_update_win_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
7399 if (e
->data
.data32
[2])
7400 ewmh_update_win_state(win
, e
->data
.data32
[2],
7412 int err
= xcb_connection_has_error(conn
);
7416 case XCB_CONN_ERROR
:
7417 s
= "Socket error, pipe error or other stream error.";
7419 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
7420 s
= "Extension not supported.";
7422 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
7423 s
= "Insufficient memory.";
7425 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
7426 s
= "Request length was exceeded.";
7428 case XCB_CONN_CLOSED_PARSE_ERR
:
7429 s
= "Error parsing display string.";
7432 s
= "Unknown error.";
7436 errx(err
, "X CONNECTION ERROR: %s", s
);
7443 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
;
7445 xcb_void_cookie_t wac
;
7446 xcb_generic_error_t
*error
;
7448 /* this causes an error if some other window manager is running */
7449 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7450 for (i
= 0; i
< num_screens
; i
++) {
7451 if ((sc
= get_screen(i
)) == NULL
)
7452 errx(1, "ERROR: can't get screen %d.", i
);
7453 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: 0x%x\n",
7455 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
7456 XCB_CW_EVENT_MASK
, &val
);
7457 if ((error
= xcb_request_check(conn
, wac
))) {
7458 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
7469 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
7471 struct swm_region
*r
, *n
;
7472 struct workspace
*ws
= NULL
;
7475 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
7476 s
->idx
, w
, h
, x
, y
);
7478 /* remove any conflicting regions */
7479 n
= TAILQ_FIRST(&s
->rl
);
7482 n
= TAILQ_NEXT(r
, entry
);
7483 if (X(r
) < (x
+ w
) &&
7484 (X(r
) + WIDTH(r
)) > x
&&
7486 (Y(r
) + HEIGHT(r
)) > y
) {
7487 if (r
->ws
->r
!= NULL
)
7488 r
->ws
->old_r
= r
->ws
->r
;
7491 TAILQ_REMOVE(&s
->rl
, r
, entry
);
7492 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
7496 /* search old regions for one to reuse */
7498 /* size + location match */
7499 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7500 if (X(r
) == x
&& Y(r
) == y
&&
7501 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7505 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7506 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7510 TAILQ_REMOVE(&s
->orl
, r
, entry
);
7511 /* try to use old region's workspace */
7512 if (r
->ws
->r
== NULL
)
7515 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
7516 err(1, "new_region: calloc: failed to allocate memory "
7519 /* if we don't have a workspace already, find one */
7521 for (i
= 0; i
< workspace_limit
; i
++)
7522 if (s
->ws
[i
].r
== NULL
) {
7529 errx(1, "new_region: no free workspaces");
7540 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
7546 #ifdef SWM_XRR_HAS_CRTC
7549 #endif /* SWM_XRR_HAS_CRTC */
7550 struct swm_region
*r
;
7552 xcb_randr_get_screen_resources_current_cookie_t src
;
7553 xcb_randr_get_screen_resources_current_reply_t
*srr
;
7554 xcb_randr_get_crtc_info_cookie_t cic
;
7555 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
7556 xcb_randr_crtc_t
*crtc
;
7557 xcb_screen_t
*screen
;
7559 if ((screen
= get_screen(i
)) == NULL
)
7560 errx(1, "ERROR: can't get screen %d.", i
);
7562 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7563 if (i
>= num_screens
)
7564 errx(1, "scan_xrandr: invalid screen");
7566 /* remove any old regions */
7567 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
7568 r
->ws
->old_r
= r
->ws
->r
= NULL
;
7570 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
7571 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
7575 /* map virtual screens onto physical screens */
7576 #ifdef SWM_XRR_HAS_CRTC
7577 if (xrandr_support
) {
7578 src
= xcb_randr_get_screen_resources_current(conn
,
7580 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
7583 new_region(&screens
[i
], 0, 0,
7584 screen
->width_in_pixels
,
7585 screen
->height_in_pixels
);
7588 ncrtc
= srr
->num_crtcs
;
7590 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
7591 for (c
= 0; c
< ncrtc
; c
++) {
7592 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
7594 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
7597 if (cir
->num_outputs
== 0) {
7603 new_region(&screens
[i
], 0, 0,
7604 screen
->width_in_pixels
,
7605 screen
->height_in_pixels
);
7607 new_region(&screens
[i
],
7608 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
7613 #endif /* SWM_XRR_HAS_CRTC */
7615 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
7616 screen
->height_in_pixels
);
7621 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
7623 struct swm_region
*r
;
7626 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%x\n", e
->root
);
7628 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7629 /* silly event doesn't include the screen index */
7630 for (i
= 0; i
< num_screens
; i
++)
7631 if (screens
[i
].root
== e
->root
)
7633 if (i
>= num_screens
)
7634 errx(1, "screenchange: screen not found");
7636 /* brute force for now, just re-enumerate the regions */
7640 print_win_geom(e
->root
);
7642 /* add bars to all regions */
7643 for (i
= 0; i
< num_screens
; i
++)
7644 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7652 xcb_window_t
*wins
= NULL
;
7654 int i
, j
, num_screens
;
7655 uint16_t state
, manage
;
7657 xcb_query_tree_cookie_t qtc
;
7658 xcb_query_tree_reply_t
*qtr
;
7659 xcb_get_window_attributes_cookie_t c
;
7660 xcb_get_window_attributes_reply_t
*r
;
7661 xcb_get_property_cookie_t pc
;
7663 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
7664 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7665 for (i
= 0; i
< num_screens
; i
++) {
7666 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
7667 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
7670 wins
= xcb_query_tree_children(qtr
);
7671 no
= xcb_query_tree_children_length(qtr
);
7672 /* attach windows to a region */
7673 /* normal windows */
7674 for (j
= 0; j
< no
; j
++) {
7675 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7676 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7679 if (r
->override_redirect
) {
7684 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7685 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7691 state
= getstate(wins
[j
]);
7692 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7693 if (r
->map_state
== XCB_MAP_STATE_VIEWABLE
|| manage
)
7694 manage_window(wins
[j
]);
7697 /* transient windows */
7698 for (j
= 0; j
< no
; j
++) {
7699 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7700 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7703 if (r
->override_redirect
) {
7709 state
= getstate(wins
[j
]);
7710 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7711 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7712 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
7713 &wins
[j
], NULL
) && manage
)
7714 manage_window(wins
[j
]);
7718 DNPRINTF(SWM_D_INIT
, "grab_windows: done\n");
7724 int i
, j
, k
, num_screens
;
7725 struct workspace
*ws
;
7726 uint32_t gcv
[1], wa
[1];
7727 const xcb_query_extension_reply_t
*qep
;
7728 xcb_screen_t
*screen
;
7729 xcb_cursor_t cursor
;
7730 xcb_font_t cursor_font
;
7731 xcb_randr_query_version_cookie_t c
;
7732 xcb_randr_query_version_reply_t
*r
;
7734 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7735 if ((screens
= calloc(num_screens
,
7736 sizeof(struct swm_screen
))) == NULL
)
7737 err(1, "setup_screens: calloc: failed to allocate memory for "
7740 /* initial Xrandr setup */
7742 c
= xcb_randr_query_version(conn
, 1, 1);
7743 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
7745 if (r
->major_version
>= 1) {
7746 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
7749 xrandr_eventbase
= qep
->first_event
;
7755 cursor_font
= xcb_generate_id(conn
);
7756 xcb_open_font(conn
, cursor_font
, strlen("cursor"), "cursor");
7758 cursor
= xcb_generate_id(conn
);
7759 xcb_create_glyph_cursor(conn
, cursor
, cursor_font
, cursor_font
,
7760 XC_left_ptr
, XC_left_ptr
+ 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
7763 /* map physical screens */
7764 for (i
= 0; i
< num_screens
; i
++) {
7765 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
7767 TAILQ_INIT(&screens
[i
].rl
);
7768 TAILQ_INIT(&screens
[i
].orl
);
7769 if ((screen
= get_screen(i
)) == NULL
)
7770 errx(1, "ERROR: can't get screen %d.", i
);
7771 screens
[i
].root
= screen
->root
;
7773 /* set default colors */
7774 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
7775 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
7776 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
7777 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
7778 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
7780 /* create graphics context on screen */
7781 screens
[i
].bar_gc
= xcb_generate_id(conn
);
7783 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
7784 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
7786 /* set default cursor */
7787 xcb_change_window_attributes(conn
, screens
[i
].root
,
7790 /* init all workspaces */
7791 /* XXX these should be dynamically allocated too */
7792 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
7793 ws
= &screens
[i
].ws
[j
];
7799 TAILQ_INIT(&ws
->winlist
);
7800 TAILQ_INIT(&ws
->unmanagedlist
);
7802 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
7803 if (layouts
[k
].l_config
!= NULL
)
7804 layouts
[k
].l_config(ws
,
7805 SWM_ARG_ID_STACKINIT
);
7806 ws
->cur_layout
= &layouts
[0];
7807 ws
->cur_layout
->l_string(ws
);
7813 xcb_randr_select_input(conn
, screens
[i
].root
,
7814 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
7816 xcb_free_cursor(conn
, cursor
);
7817 xcb_close_font(conn
, cursor_font
);
7823 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
7824 err(1, "setup_globals: strdup: failed to allocate memory.");
7826 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
7827 err(1, "setup_globals: strdup: failed to allocate memory.");
7829 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
7830 errx(1, "unable to allocate key symbols");
7832 a_state
= get_atom_from_string("WM_STATE");
7833 a_prot
= get_atom_from_string("WM_PROTOCOLS");
7834 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
7835 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
7836 a_wmname
= get_atom_from_string("WM_NAME");
7837 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
7838 a_utf8_string
= get_atom_from_string("UTF8_STRING");
7839 a_string
= get_atom_from_string("STRING");
7840 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
7841 a_swm_ws
= get_atom_from_string("_SWM_WS");
7848 xcb_atom_t netwmcheck
;
7849 xcb_window_t root
, win
;
7852 /* work around sun jdk bugs, code from wmname */
7853 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7855 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7856 for (i
= 0; i
< num_screens
; i
++) {
7857 root
= screens
[i
].root
;
7859 win
= xcb_generate_id(conn
);
7860 wa
[0] = screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].pixel
;
7861 wa
[1] = screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].pixel
;
7862 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, 0, 0, 0, 1,
7863 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
7864 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
7866 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
7867 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7868 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7869 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7870 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7871 a_netwmname
, a_utf8_string
, 8, strlen("LG3D"), "LG3D");
7876 event_error(xcb_generic_error_t
*e
)
7878 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
7879 "resource_id: %u, minor_code: %u\n",
7880 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
7881 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
7882 e
->sequence
, e
->resource_id
, e
->minor_code
);
7886 event_handle(xcb_generic_event_t
*evt
)
7888 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
7890 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d)\n",
7891 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
7892 XCB_EVENT_RESPONSE_TYPE(evt
));
7895 #define EVENT(type, callback) case type: callback((void *)evt); return
7896 EVENT(0, event_error
);
7897 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
7898 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
7899 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
7900 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
7901 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
7902 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
7903 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
7904 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
7905 /*EVENT(XCB_CREATE_NOTIFY, );*/
7906 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
7907 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
7908 EVENT(XCB_EXPOSE
, expose
);
7910 EVENT(XCB_FOCUS_IN
, focusin
);
7912 /*EVENT(XCB_FOCUS_OUT, );*/
7913 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
7914 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
7915 EVENT(XCB_KEY_PRESS
, keypress
);
7916 /*EVENT(XCB_KEY_RELEASE, keypress);*/
7917 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
7919 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
7921 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
7922 EVENT(XCB_MAP_REQUEST
, maprequest
);
7923 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
7924 /*EVENT(XCB_MOTION_NOTIFY, );*/
7925 /*EVENT(XCB_NO_EXPOSURE, );*/
7926 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
7927 /*EVENT(XCB_REPARENT_NOTIFY, );*/
7928 /*EVENT(XCB_RESIZE_REQUEST, );*/
7929 /*EVENT(XCB_SELECTION_CLEAR, );*/
7930 /*EVENT(XCB_SELECTION_NOTIFY, );*/
7931 /*EVENT(XCB_SELECTION_REQUEST, );*/
7932 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
7933 /*EVENT(XCB_VISIBILITY_NOTIFY, visibilitynotify);*/
7936 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
7937 screenchange((void *)evt
);
7941 main(int argc
, char *argv
[])
7943 struct swm_region
*r
, *rr
;
7944 struct ws_win
*winfocus
= NULL
;
7946 char conf
[PATH_MAX
], *cfile
= NULL
;
7948 int xfd
, i
, num_screens
;
7949 struct sigaction sact
;
7950 xcb_generic_event_t
*evt
;
7954 /* suppress unused warning since var is needed */
7957 time_started
= time(NULL
);
7960 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
7961 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, ""))
7962 warnx("no locale support");
7964 /* handle some signals */
7965 bzero(&sact
, sizeof(sact
));
7966 sigemptyset(&sact
.sa_mask
);
7968 sact
.sa_handler
= sighdlr
;
7969 sigaction(SIGINT
, &sact
, NULL
);
7970 sigaction(SIGQUIT
, &sact
, NULL
);
7971 sigaction(SIGTERM
, &sact
, NULL
);
7972 sigaction(SIGHUP
, &sact
, NULL
);
7974 sact
.sa_handler
= sighdlr
;
7975 sact
.sa_flags
= SA_NOCLDSTOP
;
7976 sigaction(SIGCHLD
, &sact
, NULL
);
7978 if (!(display
= XOpenDisplay(0)))
7979 errx(1, "can not open display");
7981 conn
= XGetXCBConnection(display
);
7982 if (xcb_connection_has_error(conn
))
7983 errx(1, "can not get XCB connection");
7985 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
7987 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
7988 xfd
= xcb_get_file_descriptor(conn
);
7990 /* look for local and global conf file */
7991 pwd
= getpwuid(getuid());
7993 errx(1, "invalid user: %d", getuid());
7995 xcb_grab_server(conn
);
7998 /* flush all events */
7999 while ((evt
= xcb_poll_for_event(conn
))) {
8000 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
8005 if (enable_wm() != 0)
8006 errx(1, "another window manager is currently running");
8017 for (i
= 0; ; i
++) {
8022 snprintf(conf
, sizeof conf
, "%s/.%s",
8023 pwd
->pw_dir
, SWM_CONF_FILE
);
8027 snprintf(conf
, sizeof conf
, "/etc/%s",
8032 snprintf(conf
, sizeof conf
, "%s/.%s",
8033 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
8037 snprintf(conf
, sizeof conf
, "/etc/%s",
8044 if (strlen(conf
) && stat(conf
, &sb
) != -1)
8045 if (S_ISREG(sb
.st_mode
)) {
8052 /* load conf (if any) */
8054 conf_load(cfile
, SWM_CONF_DEFAULT
);
8057 /* set some values to work around bad programs */
8059 /* grab existing windows (before we build the bars) */
8062 if (getenv("SWM_STARTED") == NULL
)
8063 setenv("SWM_STARTED", "YES", 1);
8065 /* setup all bars */
8066 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
8067 for (i
= 0; i
< num_screens
; i
++)
8068 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
8069 if (winfocus
== NULL
)
8070 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
8079 xcb_ungrab_server(conn
);
8083 while ((evt
= xcb_poll_for_event(conn
))) {
8090 /* if we are being restarted go focus on first window */
8092 rr
= winfocus
->ws
->r
;
8094 /* not a visible window */
8098 /* move pointer to first screen if multi screen */
8099 if (num_screens
> 1 || outputs
> 1)
8100 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
,
8101 rr
->s
[0].root
, 0, 0, 0, 0, X(rr
),
8102 Y(rr
) + (bar_enabled
? bar_height
: 0));
8104 a
.id
= SWM_ARG_ID_FOCUSCUR
;
8114 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
8116 DNPRINTF(SWM_D_MISC
, "select failed");
8117 if (restart_wm
== 1)
8118 restart(NULL
, NULL
);
8119 if (search_resp
== 1)
8132 for (i
= 0; i
< num_screens
; ++i
)
8133 if (screens
[i
].bar_gc
!= 0)
8134 xcb_free_gc(conn
, screens
[i
].bar_gc
);
8136 XftFontClose(display
, bar_font
);
8137 XftColorFree(display
, DefaultVisual(display
, 0),
8138 DefaultColormap(display
, 0), &bar_font_color
);
8139 xcb_key_symbols_free(syms
);
8141 xcb_disconnect(conn
);