2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
12 * Permission to use, copy, modify, and distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 * Much code and ideas taken from dwm under the following license:
26 * MIT/X Consortium License
28 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
29 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
30 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
31 * 2007 Premysl Hruby <dfenze at gmail dot com>
32 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
33 * 2007 Christof Musik <christof at sendfax dot de>
34 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
35 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
36 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
38 * Permission is hereby granted, free of charge, to any person obtaining a
39 * copy of this software and associated documentation files (the "Software"),
40 * to deal in the Software without restriction, including without limitation
41 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
42 * and/or sell copies of the Software, and to permit persons to whom the
43 * Software is furnished to do so, subject to the following conditions:
45 * The above copyright notice and this permission notice shall be included in
46 * all copies or substantial portions of the Software.
48 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
51 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
53 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
54 * DEALINGS IN THE SOFTWARE.
72 #include <sys/types.h>
76 #include <sys/queue.h>
77 #include <sys/param.h>
78 #include <sys/select.h>
79 #if defined(__linux__)
81 #elif defined(__OpenBSD__)
83 #elif defined(__FreeBSD__)
89 #include <X11/cursorfont.h>
90 #include <X11/keysym.h>
91 #include <X11/XKBlib.h>
92 #include <X11/Xatom.h>
93 #include <X11/Xlib-xcb.h>
94 #include <xcb/randr.h>
95 #include <xcb/xcb_icccm.h>
96 #include <X11/Xproto.h>
97 #include <X11/Xutil.h>
98 #include <X11/extensions/Xrandr.h>
99 #include <X11/extensions/XTest.h>
107 #ifdef SPECTRWM_BUILDSTR
108 static const char *buildstr
= SPECTRWM_BUILDSTR
;
110 static const char *buildstr
= SPECTRWM_VERSION
;
114 # error XRandR versions less than 1.0 are not supported
119 #define SWM_XRR_HAS_CRTC
123 /*#define SWM_DEBUG*/
125 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
126 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
127 #define SWM_D_MISC 0x0001
128 #define SWM_D_EVENT 0x0002
129 #define SWM_D_WS 0x0004
130 #define SWM_D_FOCUS 0x0008
131 #define SWM_D_MOVE 0x0010
132 #define SWM_D_STACK 0x0020
133 #define SWM_D_MOUSE 0x0040
134 #define SWM_D_PROP 0x0080
135 #define SWM_D_CLASS 0x0100
136 #define SWM_D_KEY 0x0200
137 #define SWM_D_QUIRK 0x0400
138 #define SWM_D_SPAWN 0x0800
139 #define SWM_D_EVENTQ 0x1000
140 #define SWM_D_CONF 0x2000
141 #define SWM_D_BAR 0x4000
143 u_int32_t swm_debug
= 0
161 #define DPRINTF(x...)
162 #define DNPRINTF(n,x...)
165 #define LENGTH(x) (sizeof x / sizeof x[0])
166 #define MODKEY Mod1Mask
167 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
168 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
169 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
170 #define SWM_PROPLEN (16)
171 #define SWM_FUNCNAME_LEN (32)
172 #define SWM_KEYS_LEN (255)
173 #define SWM_QUIRK_LEN (64)
174 #define X(r) (r)->g.x
175 #define Y(r) (r)->g.y
176 #define WIDTH(r) (r)->g.w
177 #define HEIGHT(r) (r)->g.h
178 #define BORDER(w) (w->bordered ? border_width : 0)
179 #define MAX_X(r) ((r)->g.x + (r)->g.w)
180 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
181 #define SH_MIN(w) (w)->sh_mask & PMinSize
182 #define SH_MIN_W(w) (w)->sh.min_width
183 #define SH_MIN_H(w) (w)->sh.min_height
184 #define SH_MAX(w) (w)->sh_mask & PMaxSize
185 #define SH_MAX_W(w) (w)->sh.max_width
186 #define SH_MAX_H(w) (w)->sh.max_height
187 #define SH_INC(w) (w)->sh_mask & PResizeInc
188 #define SH_INC_W(w) (w)->sh.width_inc
189 #define SH_INC_H(w) (w)->sh.height_inc
190 #define SWM_MAX_FONT_STEPS (3)
191 #define WINID(w) ((w) ? (w)->id : 0)
192 #define YESNO(x) ((x) ? "yes" : "no")
194 #define SWM_FOCUS_DEFAULT (0)
195 #define SWM_FOCUS_SYNERGY (1)
196 #define SWM_FOCUS_FOLLOW (2)
198 #define SWM_CONF_DEFAULT (0)
199 #define SWM_CONF_KEYMAPPING (1)
202 #define SWM_LIB "/usr/local/lib/libswmhack.so"
209 xcb_atom_t takefocus
;
211 xcb_atom_t a_netwmname
;
212 xcb_atom_t a_utf8_string
;
214 xcb_atom_t a_swm_iconic
;
215 volatile sig_atomic_t running
= 1;
216 volatile sig_atomic_t restart_wm
= 0;
218 int last_focus_event
= FocusOut
;
219 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
223 int xrandr_eventbase
;
224 unsigned int numlockmask
= 0;
226 xcb_connection_t
*conn
;
229 int cycle_visible
= 0;
231 int font_adjusted
= 0;
232 unsigned int mod_key
= MODKEY
;
235 struct swm_region
*search_r
;
236 int select_list_pipe
[2];
237 int select_resp_pipe
[2];
239 volatile sig_atomic_t search_resp
;
240 int search_resp_action
;
242 struct search_window
{
243 TAILQ_ENTRY(search_window
) entry
;
247 xcb_window_t indicator
;
249 TAILQ_HEAD(search_winlist
, search_window
);
250 struct search_winlist search_wl
;
255 SWM_SEARCH_UNICONIFY
,
256 SWM_SEARCH_NAME_WORKSPACE
,
257 SWM_SEARCH_SEARCH_WORKSPACE
,
258 SWM_SEARCH_SEARCH_WINDOW
261 #define SWM_STACK_TOP (0)
262 #define SWM_STACK_BOTTOM (1)
263 #define SWM_STACK_ABOVE (2)
264 #define SWM_STACK_BELOW (3)
267 double dialog_ratio
= 0.6;
269 #define SWM_BAR_MAX (256)
270 #define SWM_BAR_JUSTIFY_LEFT (0)
271 #define SWM_BAR_JUSTIFY_CENTER (1)
272 #define SWM_BAR_JUSTIFY_RIGHT (2)
273 #define SWM_BAR_OFFSET (4)
274 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
275 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
276 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
277 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
278 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
280 #ifdef X_HAVE_UTF8_STRING
281 #define DRAWSTRING(x...) Xutf8DrawString(x)
283 #define DRAWSTRING(x...) XmbDrawString(x)
286 char *bar_argv
[] = { NULL
, NULL
};
288 unsigned char bar_ext
[SWM_BAR_MAX
];
289 char bar_vertext
[SWM_BAR_MAX
];
291 sig_atomic_t bar_alarm
= 0;
294 int bar_border_width
= 1;
295 int bar_at_bottom
= 0;
297 int bar_extra_running
= 0;
300 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
301 char *bar_format
= NULL
;
302 int stack_enabled
= 1;
303 int clock_enabled
= 1;
304 int urgent_enabled
= 0;
305 char *clock_format
= NULL
;
306 int title_name_enabled
= 0;
307 int title_class_enabled
= 0;
308 int window_name_enabled
= 0;
309 int focus_mode
= SWM_FOCUS_DEFAULT
;
310 int focus_close
= SWM_STACK_BELOW
;
311 int focus_close_wrap
= 1;
312 int focus_default
= SWM_STACK_TOP
;
313 int spawn_position
= SWM_STACK_TOP
;
314 int disable_border
= 0;
315 int border_width
= 1;
316 int verbose_layout
= 0;
319 XFontSetExtents
*bar_fs_extents
;
323 #define SWM_MENU_FN (2)
324 #define SWM_MENU_NB (4)
325 #define SWM_MENU_NF (6)
326 #define SWM_MENU_SB (8)
327 #define SWM_MENU_SF (10)
329 /* layout manager data */
330 struct swm_geometry
{
343 struct swm_geometry g
;
346 /* virtual "screens" */
348 TAILQ_ENTRY(swm_region
) entry
;
349 struct swm_geometry g
;
350 struct workspace
*ws
; /* current workspace on this region */
351 struct workspace
*ws_prior
; /* prior workspace on this region */
352 struct swm_screen
*s
; /* screen idx */
355 TAILQ_HEAD(swm_region_list
, swm_region
);
358 TAILQ_ENTRY(ws_win
) entry
;
360 xcb_window_t transient
;
361 struct ws_win
*child_trans
; /* transient child window */
362 struct swm_geometry g
; /* current geometry */
363 struct swm_geometry g_float
; /* region coordinates */
364 int g_floatvalid
; /* g_float geometry validity */
365 int floatmaxed
; /* whether maxed by max_stack */
370 unsigned int ewmh_flags
;
371 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
377 unsigned long quirks
;
378 struct workspace
*ws
; /* always valid */
379 struct swm_screen
*s
; /* always valid, never changes */
380 XWindowAttributes wa
;
386 TAILQ_HEAD(ws_win_list
, ws_win
);
390 TAILQ_ENTRY(pid_e
) entry
;
394 TAILQ_HEAD(pid_list
, pid_e
);
395 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
397 /* layout handlers */
399 void vertical_config(struct workspace
*, int);
400 void vertical_stack(struct workspace
*, struct swm_geometry
*);
401 void horizontal_config(struct workspace
*, int);
402 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
403 void max_stack(struct workspace
*, struct swm_geometry
*);
404 void plain_stacker(struct workspace
*);
405 void fancy_stacker(struct workspace
*);
407 struct ws_win
*find_window(xcb_window_t
);
409 void grabbuttons(struct ws_win
*, int);
410 void new_region(struct swm_screen
*, int, int, int, int);
411 void unmanage_window(struct ws_win
*);
412 uint16_t getstate(xcb_window_t
);
414 int conf_load(char *, int);
417 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
418 void (*l_config
)(struct workspace
*, int);
420 #define SWM_L_FOCUSPREV (1<<0)
421 #define SWM_L_MAPONFOCUS (1<<1)
422 void (*l_string
)(struct workspace
*);
424 /* stack, configure */
425 { vertical_stack
, vertical_config
, 0, plain_stacker
},
426 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
428 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
429 { NULL
, NULL
, 0, NULL
},
432 /* position of max_stack mode in the layouts array, index into layouts! */
433 #define SWM_V_STACK (0)
434 #define SWM_H_STACK (1)
435 #define SWM_MAX_STACK (2)
437 #define SWM_H_SLICE (32)
438 #define SWM_V_SLICE (32)
440 /* define work spaces */
442 int idx
; /* workspace index */
443 char *name
; /* workspace name */
444 int always_raise
; /* raise windows on focus */
445 struct layout
*cur_layout
; /* current layout handlers */
446 struct ws_win
*focus
; /* may be NULL */
447 struct ws_win
*focus_prev
; /* may be NULL */
448 struct swm_region
*r
; /* may be NULL */
449 struct swm_region
*old_r
; /* may be NULL */
450 struct ws_win_list winlist
; /* list of windows in ws */
451 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
452 char stacker
[10]; /* display stacker and layout */
456 int horizontal_msize
;
458 int horizontal_stacks
;
469 SWM_S_COLOR_BAR_BORDER
,
470 SWM_S_COLOR_BAR_FONT
,
476 /* physical screen mapping */
477 #define SWM_WS_MAX (22) /* hard limit */
478 int workspace_limit
= 10; /* soft limit */
481 int idx
; /* screen index */
482 struct swm_region_list rl
; /* list of regions on this screen */
483 struct swm_region_list orl
; /* list of old regions */
485 struct workspace ws
[SWM_WS_MAX
];
491 } c
[SWM_S_COLOR_MAX
];
495 struct swm_screen
*screens
;
497 /* args to functions */
500 #define SWM_ARG_ID_FOCUSNEXT (0)
501 #define SWM_ARG_ID_FOCUSPREV (1)
502 #define SWM_ARG_ID_FOCUSMAIN (2)
503 #define SWM_ARG_ID_FOCUSCUR (4)
504 #define SWM_ARG_ID_SWAPNEXT (10)
505 #define SWM_ARG_ID_SWAPPREV (11)
506 #define SWM_ARG_ID_SWAPMAIN (12)
507 #define SWM_ARG_ID_MOVELAST (13)
508 #define SWM_ARG_ID_MASTERSHRINK (20)
509 #define SWM_ARG_ID_MASTERGROW (21)
510 #define SWM_ARG_ID_MASTERADD (22)
511 #define SWM_ARG_ID_MASTERDEL (23)
512 #define SWM_ARG_ID_FLIPLAYOUT (24)
513 #define SWM_ARG_ID_STACKRESET (30)
514 #define SWM_ARG_ID_STACKINIT (31)
515 #define SWM_ARG_ID_CYCLEWS_UP (40)
516 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
517 #define SWM_ARG_ID_CYCLESC_UP (42)
518 #define SWM_ARG_ID_CYCLESC_DOWN (43)
519 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
520 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
521 #define SWM_ARG_ID_STACKINC (50)
522 #define SWM_ARG_ID_STACKDEC (51)
523 #define SWM_ARG_ID_SS_ALL (60)
524 #define SWM_ARG_ID_SS_WINDOW (61)
525 #define SWM_ARG_ID_DONTCENTER (70)
526 #define SWM_ARG_ID_CENTER (71)
527 #define SWM_ARG_ID_KILLWINDOW (80)
528 #define SWM_ARG_ID_DELETEWINDOW (81)
529 #define SWM_ARG_ID_WIDTHGROW (90)
530 #define SWM_ARG_ID_WIDTHSHRINK (91)
531 #define SWM_ARG_ID_HEIGHTGROW (92)
532 #define SWM_ARG_ID_HEIGHTSHRINK (93)
533 #define SWM_ARG_ID_MOVEUP (100)
534 #define SWM_ARG_ID_MOVEDOWN (101)
535 #define SWM_ARG_ID_MOVELEFT (102)
536 #define SWM_ARG_ID_MOVERIGHT (103)
540 void focus(struct swm_region
*, union arg
*);
541 void focus_magic(struct ws_win
*);
545 TAILQ_ENTRY(quirk
) entry
;
549 #define SWM_Q_FLOAT (1<<0) /* float this window */
550 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
551 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
552 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
553 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
554 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
556 TAILQ_HEAD(quirk_list
, quirk
);
557 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
560 * Supported EWMH hints should be added to
561 * both the enum and the ewmh array
566 _NET_MOVERESIZE_WINDOW
,
567 _NET_WM_ACTION_CLOSE
,
568 _NET_WM_ACTION_FULLSCREEN
,
570 _NET_WM_ACTION_RESIZE
,
571 _NET_WM_ALLOWED_ACTIONS
,
574 _NET_WM_STATE_FULLSCREEN
,
575 _NET_WM_STATE_HIDDEN
,
576 _NET_WM_STATE_MAXIMIZED_HORZ
,
577 _NET_WM_STATE_MAXIMIZED_VERT
,
578 _NET_WM_STATE_SKIP_PAGER
,
579 _NET_WM_STATE_SKIP_TASKBAR
,
581 _NET_WM_WINDOW_TYPE_DIALOG
,
582 _NET_WM_WINDOW_TYPE_DOCK
,
583 _NET_WM_WINDOW_TYPE_NORMAL
,
584 _NET_WM_WINDOW_TYPE_SPLASH
,
585 _NET_WM_WINDOW_TYPE_TOOLBAR
,
586 _NET_WM_WINDOW_TYPE_UTILITY
,
587 _SWM_WM_STATE_MANUAL
,
594 } ewmh
[SWM_EWMH_HINT_MAX
] = {
595 /* must be in same order as in the enum */
596 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
597 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
598 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
599 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
600 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
601 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
602 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
603 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
604 {"_NET_WM_STATE", XCB_ATOM_NONE
},
605 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
606 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
607 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
608 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
609 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
610 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
611 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
612 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
613 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
614 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
615 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
616 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
617 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
618 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
619 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
622 void store_float_geom(struct ws_win
*, struct swm_region
*);
623 int floating_toggle_win(struct ws_win
*);
624 void constrain_window(struct ws_win
*, struct swm_region
*, int);
625 void update_window(struct ws_win
*);
626 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
627 unsigned char *get_win_name(xcb_window_t
);
628 xcb_atom_t
get_atom_from_string(const char *);
629 void map_window_raised(xcb_window_t
);
632 map_window_raised(xcb_window_t win
)
634 uint32_t val
= XCB_STACK_MODE_ABOVE
;
636 xcb_configure_window(conn
, win
,
637 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
639 xcb_map_window(conn
, win
);
643 get_atom_from_string(const char *str
)
645 xcb_intern_atom_cookie_t c
;
646 xcb_intern_atom_reply_t
*r
;
649 c
= xcb_intern_atom(conn
, False
, strlen(str
), str
);
650 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
658 return (XCB_ATOM_NONE
);
662 get_property(Window id
, Atom atom
, long count
, Atom type
, unsigned long *nitems
,
663 unsigned long *nbytes
, unsigned char **data
)
666 unsigned long *nbytes_ret
, *nitems_ret
;
667 unsigned long nbytes_tmp
, nitems_tmp
;
670 nbytes_ret
= nbytes
!= NULL
? nbytes
: &nbytes_tmp
;
671 nitems_ret
= nitems
!= NULL
? nitems
: &nitems_tmp
;
673 status
= XGetWindowProperty(display
, id
, atom
, 0L, count
, False
, type
,
674 &real
, &format
, nitems_ret
, nbytes_ret
, data
);
676 if (status
!= Success
)
685 update_iconic(struct ws_win
*win
, int newv
)
692 iprop
= get_atom_from_string("_SWM_ICONIC");
693 if (iprop
== XCB_ATOM_NONE
)
697 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
698 iprop
, XCB_ATOM_INTEGER
, 32, 1, &v
);
700 xcb_delete_property(conn
, win
->id
, iprop
);
704 get_iconic(struct ws_win
*win
)
706 int32_t v
= 0, *vtmp
;
708 xcb_get_property_cookie_t pc
;
709 xcb_get_property_reply_t
*pr
;
711 iprop
= get_atom_from_string("_SWM_ICONIC");
712 if (iprop
== XCB_ATOM_NONE
)
715 pc
= xcb_get_property(conn
, False
, win
->id
, iprop
, XCB_ATOM_INTEGER
,
717 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
720 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
722 vtmp
= xcb_get_property_value(pr
);
734 int i
, j
, num_screens
;
736 sup_list
= get_atom_from_string("_NET_SUPPORTED");
738 for (i
= 0; i
< LENGTH(ewmh
); i
++)
739 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
741 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
742 for (i
= 0; i
< num_screens
; i
++) {
743 /* Support check window will be created by workaround(). */
745 /* Report supported atoms */
746 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
747 for (j
= 0; j
< LENGTH(ewmh
); j
++)
748 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
749 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
758 xcb_atom_t sup_check
, sup_list
;
760 xcb_get_property_cookie_t pc
;
761 xcb_get_property_reply_t
*pr
;
763 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
764 sup_list
= get_atom_from_string("_NET_SUPPORTED");
765 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
767 for (i
= 0; i
< num_screens
; i
++) {
768 /* Get the support check window and destroy it */
769 pc
= xcb_get_property(conn
, False
, screens
[i
].root
, sup_check
,
770 XCB_ATOM_WINDOW
, 0, 1);
771 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
773 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
775 xcb_destroy_window(conn
, id
);
776 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
777 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
785 ewmh_autoquirk(struct ws_win
*win
)
791 xcb_get_property_cookie_t c
;
792 xcb_get_property_reply_t
*r
;
794 c
= xcb_get_property(conn
, False
, win
->id
,
795 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, (~0L));
796 r
= xcb_get_property_reply(conn
, c
, NULL
);
799 n
= xcb_get_property_value_length(r
);
801 for (i
= 0; i
< n
; i
++) {
802 type
= *((xcb_atom_t
*)xcb_get_property_value(r
));
803 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
805 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
806 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
807 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
809 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
812 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
813 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
815 win
->quirks
= SWM_Q_FLOAT
;
822 #define SWM_EWMH_ACTION_COUNT_MAX (6)
823 #define EWMH_F_FULLSCREEN (1<<0)
824 #define EWMH_F_ABOVE (1<<1)
825 #define EWMH_F_HIDDEN (1<<2)
826 #define EWMH_F_SKIP_PAGER (1<<3)
827 #define EWMH_F_SKIP_TASKBAR (1<<4)
828 #define SWM_F_MANUAL (1<<5)
831 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
839 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%lx, "
840 "fullscreen %s\n", win
->id
, YESNO(fs
));
843 if (!win
->g_floatvalid
)
844 store_float_geom(win
, win
->ws
->r
);
846 win
->g
= win
->ws
->r
->g
;
849 if (win
->g_floatvalid
) {
850 /* refloat at last floating relative position */
851 win
->g
= win
->g_float
;
852 X(win
) += X(win
->ws
->r
);
853 Y(win
) += Y(win
->ws
->r
);
861 ewmh_update_actions(struct ws_win
*win
)
863 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
869 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
872 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
873 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
876 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
877 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1,
881 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
882 #define _NET_WM_STATE_ADD 1 /* add/set property */
883 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
886 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
888 unsigned int mask
= 0;
889 unsigned int changed
= 0;
890 unsigned int orig_flags
;
895 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
896 mask
= EWMH_F_FULLSCREEN
;
897 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
899 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
901 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
902 mask
= EWMH_F_SKIP_PAGER
;
903 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
904 mask
= EWMH_F_SKIP_TASKBAR
;
907 orig_flags
= win
->ewmh_flags
;
910 case _NET_WM_STATE_REMOVE
:
911 win
->ewmh_flags
&= ~mask
;
913 case _NET_WM_STATE_ADD
:
914 win
->ewmh_flags
|= mask
;
916 case _NET_WM_STATE_TOGGLE
:
917 win
->ewmh_flags
^= mask
;
921 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
923 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
925 if (!floating_toggle_win(win
))
926 win
->ewmh_flags
= orig_flags
; /* revert */
927 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
929 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
930 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
932 if (!ewmh_set_win_fullscreen(win
,
933 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
934 win
->ewmh_flags
= orig_flags
; /* revert */
936 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
938 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
939 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
940 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
941 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
942 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
943 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
944 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
945 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
946 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
947 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
948 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
949 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
950 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
951 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
952 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
953 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
954 if (win
->ewmh_flags
& SWM_F_MANUAL
)
955 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
956 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
957 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
961 ewmh_get_win_state(struct ws_win
*win
)
964 xcb_get_property_cookie_t c
;
965 xcb_get_property_reply_t
*r
;
973 win
->ewmh_flags
|= EWMH_F_ABOVE
;
975 win
->ewmh_flags
|= SWM_F_MANUAL
;
977 c
= xcb_get_property(conn
, False
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
978 XCB_ATOM_ATOM
, 0, (~0L));
979 r
= xcb_get_property_reply(conn
, c
, NULL
);
983 states
= xcb_get_property_value(r
);
984 n
= xcb_get_property_value_length(r
);
988 for (i
= 0; i
< n
; i
++)
989 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
995 geteventname(XEvent
*e
)
1004 name
= "KeyRelease";
1007 name
= "ButtonPress";
1010 name
= "ButtonRelease";
1013 name
= "MotionNotify";
1016 name
= "EnterNotify";
1019 name
= "LeaveNotify";
1028 name
= "KeymapNotify";
1033 case GraphicsExpose
:
1034 name
= "GraphicsExpose";
1039 case VisibilityNotify
:
1040 name
= "VisibilityNotify";
1043 name
= "CreateNotify";
1046 name
= "DestroyNotify";
1049 name
= "UnmapNotify";
1055 name
= "MapRequest";
1057 case ReparentNotify
:
1058 name
= "ReparentNotify";
1060 case ConfigureNotify
:
1061 name
= "ConfigureNotify";
1063 case ConfigureRequest
:
1064 name
= "ConfigureRequest";
1067 name
= "GravityNotify";
1070 name
= "ResizeRequest";
1072 case CirculateNotify
:
1073 name
= "CirculateNotify";
1075 case CirculateRequest
:
1076 name
= "CirculateRequest";
1078 case PropertyNotify
:
1079 name
= "PropertyNotify";
1081 case SelectionClear
:
1082 name
= "SelectionClear";
1084 case SelectionRequest
:
1085 name
= "SelectionRequest";
1087 case SelectionNotify
:
1088 name
= "SelectionNotify";
1090 case ColormapNotify
:
1091 name
= "ColormapNotify";
1094 name
= "ClientMessage";
1097 name
= "MappingNotify";
1107 xrandr_geteventname(XEvent
*e
)
1111 switch(e
->type
- xrandr_eventbase
) {
1112 case RRScreenChangeNotify
:
1113 name
= "RRScreenChangeNotify";
1123 dumpwins(struct swm_region
*r
, union arg
*args
)
1127 XWindowAttributes wa
;
1129 if (r
->ws
== NULL
) {
1130 warnx("dumpwins: invalid workspace");
1134 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1136 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1137 state
= getstate(win
->id
);
1138 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1139 warnx("window: 0x%lx, failed XGetWindowAttributes",
1141 warnx("window: 0x%lx, map_state: %d, state: %u, "
1142 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1146 warnx("===== unmanaged window list =====");
1147 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1148 state
= getstate(win
->id
);
1149 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1150 warnx("window: 0x%lx, failed XGetWindowAttributes",
1152 warnx("window: 0x%lx, map_state: %d, state: %u, "
1153 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1157 warnx("=================================");
1161 dumpwins(struct swm_region
*r
, union arg
*args
)
1164 #endif /* SWM_DEBUG */
1166 void expose(XEvent
*);
1167 void keypress(XEvent
*);
1168 void buttonpress(XEvent
*);
1169 void configurerequest(XEvent
*);
1170 void configurenotify(XEvent
*);
1171 void destroynotify(XEvent
*);
1172 void enternotify(XEvent
*);
1173 void focusevent(XEvent
*);
1174 void mapnotify(XEvent
*);
1175 void mappingnotify(XEvent
*);
1176 void maprequest(XEvent
*);
1177 void propertynotify(XEvent
*);
1178 void unmapnotify(XEvent
*);
1179 void visibilitynotify(XEvent
*);
1180 void clientmessage(XEvent
*);
1182 void (*handler
[LASTEvent
])(XEvent
*) = {
1184 [KeyPress
] = keypress
,
1185 [ButtonPress
] = buttonpress
,
1186 [ConfigureRequest
] = configurerequest
,
1187 [ConfigureNotify
] = configurenotify
,
1188 [DestroyNotify
] = destroynotify
,
1189 [EnterNotify
] = enternotify
,
1190 [FocusIn
] = focusevent
,
1191 [FocusOut
] = focusevent
,
1192 [MapNotify
] = mapnotify
,
1193 [MappingNotify
] = mappingnotify
,
1194 [MapRequest
] = maprequest
,
1195 [PropertyNotify
] = propertynotify
,
1196 [UnmapNotify
] = unmapnotify
,
1197 [VisibilityNotify
] = visibilitynotify
,
1198 [ClientMessage
] = clientmessage
,
1204 int saved_errno
, status
;
1207 saved_errno
= errno
;
1211 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1216 if (errno
!= ECHILD
)
1217 warn("sighdlr: waitpid");
1218 #endif /* SWM_DEBUG */
1221 if (pid
== searchpid
)
1225 if (WIFEXITED(status
)) {
1226 if (WEXITSTATUS(status
) != 0)
1227 warnx("sighdlr: child exit status: %d",
1228 WEXITSTATUS(status
));
1230 warnx("sighdlr: child is terminated "
1232 #endif /* SWM_DEBUG */
1246 errno
= saved_errno
;
1252 struct pid_e
*p
= NULL
;
1254 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1259 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1268 name_to_color(const char *colorname
)
1270 uint32_t result
= 0;
1271 char cname
[32] = "#";
1272 xcb_screen_t
*screen
;
1273 xcb_colormap_t cmap
;
1274 xcb_alloc_named_color_cookie_t c
;
1275 xcb_alloc_named_color_reply_t
*r
;
1277 /* XXX - does not support rgb:/RR/GG/BB
1278 * will need to use xcb_alloc_color
1280 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1281 cmap
= screen
->default_colormap
;
1283 c
= xcb_alloc_named_color(conn
, cmap
, strlen(colorname
), colorname
);
1284 r
= xcb_alloc_named_color_reply(conn
, c
, NULL
);
1286 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1287 c
= xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1289 r
= xcb_alloc_named_color_reply(conn
, c
, NULL
);
1295 warnx("color '%s' not found", colorname
);
1301 setscreencolor(char *val
, int i
, int c
)
1305 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1306 if (i
> 0 && i
<= num_screens
) {
1307 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1308 free(screens
[i
- 1].c
[c
].name
);
1309 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1311 } else if (i
== -1) {
1312 for (i
= 0; i
< num_screens
; i
++) {
1313 screens
[i
].c
[c
].color
= name_to_color(val
);
1314 free(screens
[i
].c
[c
].name
);
1315 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1319 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1324 fancy_stacker(struct workspace
*ws
)
1326 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1327 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1328 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1329 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1330 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1331 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1332 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1333 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1334 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1338 plain_stacker(struct workspace
*ws
)
1340 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1341 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1342 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1343 sizeof ws
->stacker
);
1344 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1345 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1346 sizeof ws
->stacker
);
1350 custom_region(char *val
)
1352 unsigned int sidx
, x
, y
, w
, h
;
1355 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1356 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1357 errx(1, "invalid custom region, "
1358 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1359 if (sidx
< 1 || sidx
> num_screens
)
1360 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1365 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1367 if (x
> DisplayWidth(display
, sidx
) ||
1368 y
> DisplayHeight(display
, sidx
) ||
1369 w
+ x
> DisplayWidth(display
, sidx
) ||
1370 h
+ y
> DisplayHeight(display
, sidx
)) {
1371 warnx("ignoring region %ux%u+%u+%u - not within screen "
1372 "boundaries (%ux%u)", w
, h
, x
, y
,
1373 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
1377 new_region(&screens
[sidx
], x
, y
, w
, h
);
1381 socket_setnonblock(int fd
)
1385 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1386 err(1, "fcntl F_GETFL");
1387 flags
|= O_NONBLOCK
;
1388 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1389 err(1, "fcntl F_SETFL");
1393 bar_print(struct swm_region
*r
, const char *s
)
1397 XRectangle ibox
, lbox
;
1400 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1402 switch (bar_justify
) {
1403 case SWM_BAR_JUSTIFY_LEFT
:
1406 case SWM_BAR_JUSTIFY_CENTER
:
1407 x
= (WIDTH(r
) - lbox
.width
) / 2;
1409 case SWM_BAR_JUSTIFY_RIGHT
:
1410 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1414 if (x
< SWM_BAR_OFFSET
)
1417 /* clear back buffer */
1418 XSetForeground(display
, r
->s
->bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1419 XFillRectangle(display
, r
->bar
->buffer
, r
->s
->bar_gc
, 0, 0,
1420 WIDTH(r
->bar
), HEIGHT(r
->bar
));
1422 /* draw back buffer */
1423 XSetForeground(display
, r
->s
->bar_gc
,
1424 r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
1425 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, r
->s
->bar_gc
,
1426 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1430 XCopyArea(display
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1431 WIDTH(r
->bar
), HEIGHT(r
->bar
), 0, 0);
1435 bar_extra_stop(void)
1439 bzero(bar_pipe
, sizeof bar_pipe
);
1442 kill(bar_pid
, SIGTERM
);
1445 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1450 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1452 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1454 if (r
->ws
->focus
->ch
.res_class
!= NULL
)
1455 strlcat(s
, r
->ws
->focus
->ch
.res_class
, sz
);
1459 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1461 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1463 if (r
->ws
->focus
->ch
.res_name
!= NULL
)
1464 strlcat(s
, r
->ws
->focus
->ch
.res_name
, sz
);
1468 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1470 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1473 bar_class_name(s
, sz
, r
);
1474 strlcat(s
, ":", sz
);
1475 bar_title_name(s
, sz
, r
);
1479 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1481 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1483 if (r
->ws
->focus
->floating
)
1484 strlcat(s
, "(f)", sz
);
1488 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1490 unsigned char *title
;
1492 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1494 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1497 strlcat(s
, (char *)title
, sz
);
1501 int urgent
[SWM_WS_MAX
];
1503 bar_urgent(char *s
, size_t sz
)
1505 XWMHints
*wmh
= NULL
;
1507 int i
, j
, num_screens
;
1510 for (i
= 0; i
< workspace_limit
; i
++)
1513 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1514 for (i
= 0; i
< num_screens
; i
++)
1515 for (j
= 0; j
< workspace_limit
; j
++)
1516 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1517 wmh
= XGetWMHints(display
, win
->id
);
1521 if (wmh
->flags
& XUrgencyHint
)
1526 for (i
= 0; i
< workspace_limit
; i
++) {
1528 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1530 snprintf(b
, sizeof b
, "- ");
1536 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1538 if (r
== NULL
|| r
->ws
== NULL
)
1540 if (r
->ws
->name
!= NULL
)
1541 strlcat(s
, r
->ws
->name
, sz
);
1544 /* build the default bar format according to the defined enabled options */
1546 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1548 /* if format provided, just copy the buffers */
1549 if (bar_format
!= NULL
) {
1550 strlcpy(fmtnew
, fmtexp
, sz
);
1554 /* reset the output buffer */
1557 strlcat(fmtnew
, "+N:+I ", sz
);
1559 strlcat(fmtnew
, "+S", sz
);
1560 strlcat(fmtnew
, " ", sz
);
1562 /* only show the workspace name if there's actually one */
1563 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1564 strlcat(fmtnew
, "<+D>", sz
);
1565 strlcat(fmtnew
, "+3<", sz
);
1567 if (clock_enabled
) {
1568 strlcat(fmtnew
, fmtexp
, sz
);
1569 strlcat(fmtnew
, "+4<", sz
);
1572 /* bar_urgent already adds the space before the last asterisk */
1574 strlcat(fmtnew
, "* +U*+4<", sz
);
1576 if (title_class_enabled
) {
1577 strlcat(fmtnew
, "+C", sz
);
1578 if (title_name_enabled
== 0)
1579 strlcat(fmtnew
, "+4<", sz
);
1582 /* checks needed by the colon and floating strlcat(3) calls below */
1583 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1584 if (title_name_enabled
) {
1585 if (title_class_enabled
)
1586 strlcat(fmtnew
, ":", sz
);
1587 strlcat(fmtnew
, "+T+4<", sz
);
1589 if (window_name_enabled
) {
1590 if (r
->ws
->focus
->floating
)
1591 strlcat(fmtnew
, "+F ", sz
);
1592 strlcat(fmtnew
, "+64W ", sz
);
1596 /* finally add the action script output and the version */
1597 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1601 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1603 /* special case; no limit given, pad one space, instead */
1604 if (*limit
== sz
- 1)
1606 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1609 /* replaces the bar format character sequences (like in tmux(1)) */
1611 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1615 char tmp
[SWM_BAR_MAX
];
1619 /* reset strlcat(3) buffer */
1622 /* get number, if any */
1625 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1626 limit
= sizeof tmp
- 1;
1627 if (limit
<= 0 || limit
>= sizeof tmp
)
1628 limit
= sizeof tmp
- 1;
1630 /* there is nothing to replace (ie EOL) */
1637 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1640 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1643 bar_class_name(tmp
, sizeof tmp
, r
);
1646 bar_workspace_name(tmp
, sizeof tmp
, r
);
1649 bar_window_float(tmp
, sizeof tmp
, r
);
1652 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1655 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1658 bar_class_title_name(tmp
, sizeof tmp
, r
);
1661 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1664 bar_title_name(tmp
, sizeof tmp
, r
);
1667 bar_urgent(tmp
, sizeof tmp
);
1670 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1673 bar_window_name(tmp
, sizeof tmp
, r
);
1676 /* unknown character sequence; copy as-is */
1677 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1685 while (limit
-- > 0) {
1686 if (*offrep
>= sz
- 1)
1688 fmtrep
[(*offrep
)++] = *ptr
++;
1696 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1701 while (*fmt
!= '\0') {
1703 /* skip ordinary characters */
1706 fmtrep
[off
++] = *fmt
++;
1710 /* character sequence found; replace it */
1711 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1720 bar_fmt_expand(char *fmtexp
, size_t sz
)
1727 /* start by grabbing the current time and date */
1729 localtime_r(&tmt
, &tm
);
1731 /* figure out what to expand */
1732 if (bar_format
!= NULL
)
1734 else if (bar_format
== NULL
&& clock_enabled
)
1736 /* if nothing to expand bail out */
1742 /* copy as-is, just in case the format shouldn't be expanded below */
1743 strlcpy(fmtexp
, fmt
, sz
);
1744 /* finally pass the string through strftime(3) */
1745 #ifndef SWM_DENY_CLOCK_FORMAT
1746 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1747 warnx("format too long");
1755 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1756 char fmtrep
[SWM_BAR_MAX
];
1758 struct swm_region
*r
;
1760 /* expand the format by first passing it through strftime(3) */
1761 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1763 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1764 for (i
= 0; i
< num_screens
; i
++) {
1765 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1768 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1769 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1770 bar_print(r
, fmtrep
);
1781 if (bar_enabled
== 0)
1783 if (bar_extra
&& bar_extra_running
) {
1784 /* ignore short reads; it'll correct itself */
1785 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1786 if (b
&& b
[len
- 1] == '\n') {
1788 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1790 if (b
== NULL
&& errno
!= EAGAIN
) {
1791 warn("bar_update: bar_extra failed");
1795 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1808 bar_toggle(struct swm_region
*r
, union arg
*args
)
1810 struct swm_region
*tmpr
;
1813 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1815 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1817 for (i
= 0; i
< num_screens
; i
++)
1818 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1820 xcb_unmap_window(conn
, tmpr
->bar
->id
);
1822 for (i
= 0; i
< num_screens
; i
++)
1823 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1825 map_window_raised(tmpr
->bar
->id
);
1828 bar_enabled
= !bar_enabled
;
1831 /* must be after stack */
1838 struct swm_region
*r
;
1842 /* do this here because the conf file is in memory */
1843 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1844 /* launch external status app */
1845 bar_extra_running
= 1;
1846 if (pipe(bar_pipe
) == -1)
1847 err(1, "pipe error");
1848 socket_setnonblock(bar_pipe
[0]);
1849 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1850 if (dup2(bar_pipe
[0], 0) == -1)
1852 if (dup2(bar_pipe
[1], 1) == -1)
1854 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1855 err(1, "could not disable SIGPIPE");
1856 switch (bar_pid
= fork()) {
1858 err(1, "cannot fork");
1862 execvp(bar_argv
[0], bar_argv
);
1863 err(1, "%s external app failed", bar_argv
[0]);
1865 default: /* parent */
1871 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1872 for (i
= 0; i
< num_screens
; i
++)
1873 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1876 wa
[0] = screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1877 wa
[1] = screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1878 xcb_change_window_attributes(conn
, r
->bar
->id
,
1879 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
1885 bar_setup(struct swm_region
*r
)
1887 char *default_string
;
1888 char **missing_charsets
;
1889 int num_missing_charsets
= 0;
1893 XFreeFontSet(display
, bar_fs
);
1897 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
1898 err(1, "bar_setup: calloc: failed to allocate memory.");
1900 DNPRINTF(SWM_D_BAR
, "bar_setup: loading bar_fonts: %s\n", bar_fonts
);
1902 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
1903 &num_missing_charsets
, &default_string
);
1905 if (num_missing_charsets
> 0) {
1906 warnx("Unable to load charset(s):");
1908 for (i
= 0; i
< num_missing_charsets
; ++i
)
1909 warnx("%s", missing_charsets
[i
]);
1911 XFreeStringList(missing_charsets
);
1913 if (strcmp(default_string
, ""))
1914 warnx("Glyphs from those sets will be replaced "
1915 "by '%s'.", default_string
);
1917 warnx("Glyphs from those sets won't be drawn.");
1921 errx(1, "Error creating font set structure.");
1923 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
1925 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
1926 2 * bar_border_width
;
1932 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1933 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
1934 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
1936 r
->bar
->id
= XCreateSimpleWindow(display
,
1937 r
->s
->root
, X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
1938 bar_border_width
, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
1939 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1941 r
->bar
->buffer
= XCreatePixmap(display
, r
->bar
->id
, WIDTH(r
->bar
),
1942 HEIGHT(r
->bar
), DefaultDepth(display
, r
->s
->idx
));
1944 xcb_randr_select_input(conn
, r
->bar
->id
,
1945 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
1948 map_window_raised(r
->bar
->id
);
1950 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%lx, (x,y) w x h: (%d,%d) "
1951 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
1954 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1955 err(1, "could not install bar_signal");
1960 bar_cleanup(struct swm_region
*r
)
1964 xcb_destroy_window(conn
, r
->bar
->id
);
1965 xcb_free_pixmap(conn
, r
->bar
->buffer
);
1971 drain_enter_notify(void)
1975 while (xcb_poll_for_event(conn
))
1978 DNPRINTF(SWM_D_EVENT
, "drain_enter_notify: drained: %d\n", i
);
1982 set_win_state(struct ws_win
*win
, uint16_t state
)
1984 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
1986 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x\n", win
->id
);
1991 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, astate
,
1992 astate
, 32, 2, data
);
1996 getstate(xcb_window_t w
)
1998 uint16_t result
= 0;
1999 xcb_get_property_cookie_t c
;
2000 xcb_get_property_reply_t
*r
;
2002 c
= xcb_get_property(conn
, False
, w
, astate
, astate
, 0L, 2L);
2003 r
= xcb_get_property_reply(conn
, c
, NULL
);
2006 result
= *((uint16_t *)xcb_get_property_value(r
));
2014 version(struct swm_region
*r
, union arg
*args
)
2016 bar_version
= !bar_version
;
2018 snprintf(bar_vertext
, sizeof bar_vertext
,
2019 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2021 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2026 client_msg(struct ws_win
*win
, xcb_atom_t a
)
2028 xcb_client_message_event_t ev
;
2033 bzero(&ev
, sizeof ev
);
2034 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2035 ev
.window
= win
->id
;
2038 ev
.data
.data32
[0] = a
;
2039 ev
.data
.data32
[1] = XCB_CURRENT_TIME
;
2041 xcb_send_event(conn
, False
, win
->id
,
2042 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2045 /* synthetic response to a ConfigureRequest when not making a change */
2047 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
2054 /* send notification of unchanged state. */
2055 ce
.type
= ConfigureNotify
;
2058 ce
.width
= WIDTH(win
);
2059 ce
.height
= HEIGHT(win
);
2060 ce
.override_redirect
= False
;
2064 ce
.display
= display
;
2066 ce
.window
= win
->id
;
2067 ce
.border_width
= BORDER(win
);
2071 ce
.display
= ev
->display
;
2072 ce
.event
= ev
->window
;
2073 ce
.window
= ev
->window
;
2075 /* make response appear more WM_SIZE_HINTS-compliant */
2077 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%lx,"
2078 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
2079 "%d x %d\n", win
->id
, win
->sh_mask
, SH_MIN_W(win
),
2080 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2081 SH_INC_W(win
), SH_INC_H(win
));
2085 /* the hint may be set... to 0! */
2086 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2087 ce
.width
= SH_MIN_W(win
);
2088 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2089 ce
.height
= SH_MIN_H(win
);
2094 /* may also be advertized as 0 */
2095 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2096 ce
.width
= SH_MAX_W(win
);
2097 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2098 ce
.height
= SH_MAX_H(win
);
2101 /* resize increment. */
2103 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2104 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2106 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2107 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2111 /* adjust x and y for requested border_width. */
2112 ce
.x
+= BORDER(win
) - ev
->border_width
;
2113 ce
.y
+= BORDER(win
) - ev
->border_width
;
2114 ce
.border_width
= ev
->border_width
;
2115 ce
.above
= ev
->above
;
2118 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
2119 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2120 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2122 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
2126 count_win(struct workspace
*ws
, int count_transient
)
2131 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2132 if (count_transient
== 0 && win
->floating
)
2134 if (count_transient
== 0 && win
->transient
)
2140 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2146 quit(struct swm_region
*r
, union arg
*args
)
2148 DNPRINTF(SWM_D_MISC
, "quit\n");
2153 unmap_window(struct ws_win
*win
)
2158 /* don't unmap again */
2159 if (getstate(win
->id
) == XCB_WM_STATE_ICONIC
)
2162 set_win_state(win
, XCB_WM_STATE_ICONIC
);
2164 xcb_unmap_window(conn
, win
->id
);
2165 XSetWindowBorder(display
, win
->id
,
2166 win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2173 int i
, j
, num_screens
;
2175 for (i
= 0; i
< num_screens
; i
++)
2176 for (j
= 0; j
< workspace_limit
; j
++)
2177 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2182 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
2189 event
.display
= display
; /* Ignored, but what the hell */
2190 event
.window
= win
->id
;
2191 event
.root
= win
->s
->root
;
2192 event
.subwindow
= None
;
2193 event
.time
= CurrentTime
;
2198 event
.same_screen
= True
;
2199 event
.keycode
= XKeysymToKeycode(display
, keysym
);
2200 event
.state
= modifiers
;
2202 event
.type
= KeyPress
;
2203 XSendEvent(event
.display
, event
.window
, True
,
2204 KeyPressMask
, (XEvent
*)&event
);
2206 event
.type
= KeyRelease
;
2207 XSendEvent(event
.display
, event
.window
, True
,
2208 KeyPressMask
, (XEvent
*)&event
);
2213 restart(struct swm_region
*r
, union arg
*args
)
2215 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2217 /* disable alarm because the following code may not be interrupted */
2219 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
2220 err(1, "can't disable alarm");
2225 XCloseDisplay(display
);
2226 execvp(start_argv
[0], start_argv
);
2227 warn("execvp failed");
2232 root_to_region(xcb_window_t root
)
2234 struct swm_region
*r
= NULL
;
2236 xcb_query_pointer_cookie_t qpc
;
2237 xcb_query_pointer_reply_t
*qpr
;
2239 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2241 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2242 for (i
= 0; i
< num_screens
; i
++)
2243 if (screens
[i
].root
== root
)
2246 qpc
= xcb_query_pointer(conn
, screens
[i
].root
);
2247 qpr
= xcb_query_pointer_reply(conn
, qpc
, NULL
);
2250 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: (%d,%d)\n",
2251 qpr
->root_x
, qpr
->root_y
);
2252 /* choose a region based on pointer location */
2253 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2254 if (X(r
) <= qpr
->root_x
&& qpr
->root_x
< MAX_X(r
) &&
2255 Y(r
) <= qpr
->root_y
&& qpr
->root_y
< MAX_Y(r
))
2261 r
= TAILQ_FIRST(&screens
[i
].rl
);
2267 find_unmanaged_window(xcb_window_t id
)
2270 int i
, j
, num_screens
;
2272 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2273 for (i
= 0; i
< num_screens
; i
++)
2274 for (j
= 0; j
< workspace_limit
; j
++)
2275 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2283 find_window(xcb_window_t id
)
2286 int i
, j
, num_screens
;
2287 xcb_query_tree_cookie_t c
;
2288 xcb_query_tree_reply_t
*r
;
2290 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2291 for (i
= 0; i
< num_screens
; i
++)
2292 for (j
= 0; j
< workspace_limit
; j
++)
2293 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2297 c
= xcb_query_tree(conn
, id
);
2298 r
= xcb_query_tree_reply(conn
, c
, NULL
);
2302 /* if we were looking for the parent return that window instead */
2303 if (r
->parent
== 0 || r
->root
== r
->parent
)
2306 /* look for parent */
2307 for (i
= 0; i
< num_screens
; i
++)
2308 for (j
= 0; j
< workspace_limit
; j
++)
2309 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2310 if (r
->parent
== win
->id
) {
2320 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2325 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2328 close(xcb_get_file_descriptor(conn
));
2330 setenv("LD_PRELOAD", SWM_LIB
, 1);
2332 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2333 warn("spawn: asprintf SWM_WS");
2336 setenv("_SWM_WS", ret
, 1);
2340 if (asprintf(&ret
, "%d", getpid()) == -1) {
2341 warn("spawn: asprintf _SWM_PID");
2344 setenv("_SWM_PID", ret
, 1);
2348 if (setsid() == -1) {
2349 warn("spawn: setsid");
2355 * close stdin and stdout to prevent interaction between apps
2356 * and the baraction script
2357 * leave stderr open to record errors
2359 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2360 warn("spawn: open");
2363 dup2(fd
, STDIN_FILENO
);
2364 dup2(fd
, STDOUT_FILENO
);
2369 execvp(args
->argv
[0], args
->argv
);
2371 warn("spawn: execvp");
2376 kill_refs(struct ws_win
*win
)
2378 int i
, x
, num_screens
;
2379 struct swm_region
*r
;
2380 struct workspace
*ws
;
2385 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2386 for (i
= 0; i
< num_screens
; i
++)
2387 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2388 for (x
= 0; x
< workspace_limit
; x
++) {
2390 if (win
== ws
->focus
)
2392 if (win
== ws
->focus_prev
)
2393 ws
->focus_prev
= NULL
;
2398 validate_win(struct ws_win
*testwin
)
2401 struct workspace
*ws
;
2402 struct swm_region
*r
;
2403 int i
, x
, num_screens
;
2405 if (testwin
== NULL
)
2408 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2409 for (i
= 0; i
< num_screens
; i
++)
2410 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2411 for (x
= 0; x
< workspace_limit
; x
++) {
2413 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2421 validate_ws(struct workspace
*testws
)
2423 struct swm_region
*r
;
2424 struct workspace
*ws
;
2425 int i
, x
, num_screens
;
2427 /* validate all ws */
2428 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2429 for (i
= 0; i
< num_screens
; i
++)
2430 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2431 for (x
= 0; x
< workspace_limit
; x
++) {
2440 unfocus_win(struct ws_win
*win
)
2443 xcb_window_t none
= XCB_WINDOW_NONE
;
2445 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%lx\n", WINID(win
));
2449 if (win
->ws
== NULL
)
2452 if (validate_ws(win
->ws
))
2453 return; /* XXX this gets hit with thunderbird, needs fixing */
2455 if (win
->ws
->r
== NULL
)
2458 if (validate_win(win
)) {
2463 if (win
->ws
->focus
== win
) {
2464 win
->ws
->focus
= NULL
;
2465 win
->ws
->focus_prev
= win
;
2468 if (validate_win(win
->ws
->focus
)) {
2469 kill_refs(win
->ws
->focus
);
2470 win
->ws
->focus
= NULL
;
2472 if (validate_win(win
->ws
->focus_prev
)) {
2473 kill_refs(win
->ws
->focus_prev
);
2474 win
->ws
->focus_prev
= NULL
;
2477 /* drain all previous unfocus events */
2478 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
2481 grabbuttons(win
, 0);
2482 XSetWindowBorder(display
, win
->id
,
2483 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2485 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2486 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2494 int i
, j
, num_screens
;
2496 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2498 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2499 for (i
= 0; i
< num_screens
; i
++)
2500 for (j
= 0; j
< workspace_limit
; j
++)
2501 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2506 focus_win(struct ws_win
*win
)
2509 struct ws_win
*cfw
= NULL
;
2510 xcb_get_input_focus_cookie_t c
;
2511 xcb_get_input_focus_reply_t
*r
;
2512 xcb_window_t cur_focus
= XCB_WINDOW_NONE
;
2514 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%lx\n", WINID(win
));
2518 if (win
->ws
== NULL
)
2521 if (validate_ws(win
->ws
))
2522 return; /* XXX this gets hit with thunderbird, needs fixing */
2524 if (validate_win(win
)) {
2529 if (validate_win(win
)) {
2534 c
= xcb_get_input_focus(conn
);
2535 r
= xcb_get_input_focus_reply(conn
, c
, NULL
);
2537 cur_focus
= r
->focus
;
2540 if ((cfw
= find_window(cur_focus
)) != NULL
)
2543 /* use larger hammer since the window was killed somehow */
2544 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2545 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2546 XSetWindowBorder(display
, cfw
->id
,
2547 cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2550 win
->ws
->focus
= win
;
2552 if (win
->ws
->r
!= NULL
) {
2553 /* drain all previous focus events */
2554 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
2558 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
2559 win
->id
, XCB_CURRENT_TIME
);
2560 grabbuttons(win
, 1);
2561 XSetWindowBorder(display
, win
->id
,
2562 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2563 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2564 win
->ws
->always_raise
)
2565 map_window_raised(win
->id
);
2567 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2568 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2576 switchws(struct swm_region
*r
, union arg
*args
)
2578 int wsid
= args
->id
, unmap_old
= 0;
2579 struct swm_region
*this_r
, *other_r
;
2581 struct workspace
*new_ws
, *old_ws
;
2587 if (wsid
>= workspace_limit
)
2591 old_ws
= this_r
->ws
;
2592 new_ws
= &this_r
->s
->ws
[wsid
];
2594 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2595 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2597 if (new_ws
== NULL
|| old_ws
== NULL
)
2599 if (new_ws
== old_ws
)
2602 other_r
= new_ws
->r
;
2603 if (other_r
== NULL
) {
2604 /* the other workspace is hidden, hide this one */
2608 /* the other ws is visible in another region, exchange them */
2609 other_r
->ws_prior
= new_ws
;
2610 other_r
->ws
= old_ws
;
2611 old_ws
->r
= other_r
;
2613 this_r
->ws_prior
= old_ws
;
2614 this_r
->ws
= new_ws
;
2617 /* this is needed so that we can click on a window after a restart */
2621 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2622 focus(new_ws
->r
, &a
);
2624 /* unmap old windows */
2626 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2629 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2630 drain_enter_notify();
2634 cyclews(struct swm_region
*r
, union arg
*args
)
2637 struct swm_screen
*s
= r
->s
;
2640 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2641 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2646 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2649 case SWM_ARG_ID_CYCLEWS_UP
:
2650 if (a
.id
< workspace_limit
- 1)
2655 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2658 case SWM_ARG_ID_CYCLEWS_DOWN
:
2662 a
.id
= workspace_limit
- 1;
2669 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2671 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2675 } while (a
.id
!= r
->ws
->idx
);
2679 priorws(struct swm_region
*r
, union arg
*args
)
2683 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2684 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2686 if (r
->ws_prior
== NULL
)
2689 a
.id
= r
->ws_prior
->idx
;
2694 cyclescr(struct swm_region
*r
, union arg
*args
)
2696 struct swm_region
*rr
= NULL
;
2698 int i
, x
, y
, num_screens
;
2700 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2701 /* do nothing if we don't have more than one screen */
2702 if (!(num_screens
> 1 || outputs
> 1))
2707 case SWM_ARG_ID_CYCLESC_UP
:
2708 rr
= TAILQ_NEXT(r
, entry
);
2710 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2712 case SWM_ARG_ID_CYCLESC_DOWN
:
2713 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2715 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2723 /* move mouse to region */
2725 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2726 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2728 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2731 if (rr
->ws
->focus
) {
2732 /* move to focus window */
2733 x
= X(rr
->ws
->focus
) + 1;
2734 y
= Y(rr
->ws
->focus
) + 1;
2735 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2740 sort_windows(struct ws_win_list
*wl
)
2742 struct ws_win
*win
, *parent
, *nxt
;
2747 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2748 nxt
= TAILQ_NEXT(win
, entry
);
2749 if (win
->transient
) {
2750 parent
= find_window(win
->transient
);
2751 if (parent
== NULL
) {
2752 warnx("not possible bug");
2755 TAILQ_REMOVE(wl
, win
, entry
);
2756 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2763 swapwin(struct swm_region
*r
, union arg
*args
)
2765 struct ws_win
*target
, *source
;
2766 struct ws_win
*cur_focus
;
2767 struct ws_win_list
*wl
;
2770 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2771 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2773 cur_focus
= r
->ws
->focus
;
2774 if (cur_focus
== NULL
)
2778 wl
= &source
->ws
->winlist
;
2781 case SWM_ARG_ID_SWAPPREV
:
2782 if (source
->transient
)
2783 source
= find_window(source
->transient
);
2784 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2785 if (target
&& target
->transient
)
2786 target
= find_window(target
->transient
);
2787 TAILQ_REMOVE(wl
, source
, entry
);
2789 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2791 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2793 case SWM_ARG_ID_SWAPNEXT
:
2794 target
= TAILQ_NEXT(source
, entry
);
2795 /* move the parent and let the sort handle the move */
2796 if (source
->transient
)
2797 source
= find_window(source
->transient
);
2798 TAILQ_REMOVE(wl
, source
, entry
);
2800 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2802 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2804 case SWM_ARG_ID_SWAPMAIN
:
2805 target
= TAILQ_FIRST(wl
);
2806 if (target
== source
) {
2807 if (source
->ws
->focus_prev
!= NULL
&&
2808 source
->ws
->focus_prev
!= target
)
2809 source
= source
->ws
->focus_prev
;
2813 if (target
== NULL
|| source
== NULL
)
2815 source
->ws
->focus_prev
= target
;
2816 TAILQ_REMOVE(wl
, target
, entry
);
2817 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2818 TAILQ_REMOVE(wl
, source
, entry
);
2819 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2821 case SWM_ARG_ID_MOVELAST
:
2822 TAILQ_REMOVE(wl
, source
, entry
);
2823 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2826 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2836 focus_prev(struct ws_win
*win
)
2838 struct ws_win
*winfocus
= NULL
;
2839 struct ws_win
*cur_focus
= NULL
;
2840 struct ws_win_list
*wl
= NULL
;
2841 struct workspace
*ws
= NULL
;
2843 if (!(win
&& win
->ws
))
2848 cur_focus
= ws
->focus
;
2850 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%lx, cur_focus: 0x%lx\n",
2851 WINID(win
), WINID(cur_focus
));
2853 /* pickle, just focus on whatever */
2854 if (cur_focus
== NULL
) {
2855 /* use prev_focus if valid */
2856 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2857 find_window(WINID(ws
->focus_prev
)))
2858 winfocus
= ws
->focus_prev
;
2862 /* if transient focus on parent */
2863 if (cur_focus
->transient
) {
2864 winfocus
= find_window(cur_focus
->transient
);
2868 /* if in max_stack try harder */
2869 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2870 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2871 if (cur_focus
!= ws
->focus_prev
)
2872 winfocus
= ws
->focus_prev
;
2874 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2879 DNPRINTF(SWM_D_FOCUS
, "focus_prev: focus_close: %d\n", focus_close
);
2881 if (winfocus
== NULL
|| winfocus
== win
) {
2882 switch (focus_close
) {
2883 case SWM_STACK_BOTTOM
:
2884 winfocus
= TAILQ_FIRST(wl
);
2887 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2889 case SWM_STACK_ABOVE
:
2890 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
2891 if (focus_close_wrap
)
2892 winfocus
= TAILQ_FIRST(wl
);
2894 winfocus
= TAILQ_PREV(cur_focus
,
2895 ws_win_list
, entry
);
2898 case SWM_STACK_BELOW
:
2899 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
2901 if (focus_close_wrap
)
2902 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2904 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2910 if (winfocus
== NULL
) {
2911 if (focus_default
== SWM_STACK_TOP
)
2912 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2914 winfocus
= TAILQ_FIRST(wl
);
2918 focus_magic(winfocus
);
2922 focus(struct swm_region
*r
, union arg
*args
)
2924 struct ws_win
*winfocus
= NULL
, *head
;
2925 struct ws_win
*cur_focus
= NULL
;
2926 struct ws_win_list
*wl
= NULL
;
2927 struct workspace
*ws
= NULL
;
2933 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2935 /* treat FOCUS_CUR special */
2936 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2937 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2938 winfocus
= r
->ws
->focus
;
2939 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2940 winfocus
= r
->ws
->focus_prev
;
2942 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2943 if (winfocus
->iconic
== 0)
2946 focus_magic(winfocus
);
2950 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2954 if (TAILQ_EMPTY(wl
))
2956 /* make sure there is at least one uniconified window */
2958 TAILQ_FOREACH(winfocus
, wl
, entry
)
2959 if (winfocus
->iconic
== 0) {
2967 case SWM_ARG_ID_FOCUSPREV
:
2968 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2970 head
= TAILQ_LAST(wl
, ws_win_list
);
2972 if (WINID(winfocus
) == cur_focus
->transient
) {
2973 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2975 head
= TAILQ_LAST(wl
, ws_win_list
);
2980 if (winfocus
&& winfocus
->iconic
) {
2981 while (winfocus
!= cur_focus
) {
2982 if (winfocus
== NULL
)
2983 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2984 if (winfocus
->iconic
== 0)
2986 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
2992 case SWM_ARG_ID_FOCUSNEXT
:
2993 head
= TAILQ_NEXT(cur_focus
, entry
);
2995 head
= TAILQ_FIRST(wl
);
2999 if (winfocus
&& winfocus
->iconic
) {
3000 while (winfocus
!= cur_focus
) {
3001 if (winfocus
== NULL
)
3002 winfocus
= TAILQ_FIRST(wl
);
3003 if (winfocus
->iconic
== 0)
3005 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3010 case SWM_ARG_ID_FOCUSMAIN
:
3011 winfocus
= TAILQ_FIRST(wl
);
3012 if (winfocus
== cur_focus
)
3013 winfocus
= cur_focus
->ws
->focus_prev
;
3020 focus_magic(winfocus
);
3024 cycle_layout(struct swm_region
*r
, union arg
*args
)
3026 struct workspace
*ws
= r
->ws
;
3029 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3032 if (ws
->cur_layout
->l_stack
== NULL
)
3033 ws
->cur_layout
= &layouts
[0];
3036 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3037 drain_enter_notify();
3038 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3043 stack_config(struct swm_region
*r
, union arg
*args
)
3045 struct workspace
*ws
= r
->ws
;
3047 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3050 if (ws
->cur_layout
->l_config
!= NULL
)
3051 ws
->cur_layout
->l_config(ws
, args
->id
);
3053 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3060 struct swm_geometry g
;
3061 struct swm_region
*r
;
3067 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3069 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3070 for (i
= 0; i
< num_screens
; i
++) {
3074 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3075 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3076 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3078 /* start with screen geometry, adjust for bar */
3080 g
.w
-= 2 * border_width
;
3081 g
.h
-= 2 * border_width
;
3087 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3088 r
->ws
->cur_layout
->l_string(r
->ws
);
3089 /* save r so we can track region changes */
3096 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3097 drain_enter_notify();
3099 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3103 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3105 /* retain window geom and region geom */
3106 win
->g_float
= win
->g
;
3107 win
->g_float
.x
-= X(r
);
3108 win
->g_float
.y
-= Y(r
);
3109 win
->g_floatvalid
= 1;
3110 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%lx, g: (%d,%d)"
3111 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3112 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3113 win
->g_float
.w
, win
->g_float
.h
);
3117 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3122 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%lx\n", win
->id
);
3125 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3126 * geom on ws switches or return from max mode
3128 if (win
->g_floatvalid
&& (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3129 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)))) {
3130 /* refloat at last floating relative position */
3131 win
->g
= win
->g_float
;
3136 win
->floatmaxed
= 0;
3139 * if set to fullscreen mode, configure window to maximum size.
3141 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3142 if (!win
->g_floatvalid
)
3143 store_float_geom(win
, win
->ws
->r
);
3145 win
->g
= win
->ws
->r
->g
;
3149 * remove border on fullscreen floater when in fullscreen mode or when
3150 * the quirk is present.
3152 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3153 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3154 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3155 if (win
->bordered
) {
3157 X(win
) += border_width
;
3158 Y(win
) += border_width
;
3160 } else if (!win
->bordered
) {
3162 X(win
) -= border_width
;
3163 Y(win
) -= border_width
;
3166 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3167 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3168 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3173 * floaters and transients are auto-centred unless moved
3176 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3177 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3180 /* keep window within region bounds */
3181 constrain_window(win
, r
, 0);
3187 * Send keystrokes to terminal to decrease/increase the font size as the
3188 * window size changes.
3191 adjust_font(struct ws_win
*win
)
3193 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3194 win
->floating
|| win
->transient
)
3197 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3198 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3199 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3200 win
->font_size_boundary
[win
->font_steps
] =
3201 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3204 win
->last_inc
= win
->sh
.width_inc
;
3205 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
3206 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3207 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3210 win
->last_inc
= win
->sh
.width_inc
;
3211 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
3215 #define SWAPXY(g) do { \
3217 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3218 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3221 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3223 XWindowAttributes wa
;
3224 struct swm_geometry win_g
, r_g
= *g
;
3225 struct ws_win
*win
, *fs_win
= NULL
;
3226 int i
, j
, s
, stacks
;
3227 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3228 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3229 int split
, colno
, winno
, mwin
, msize
, mscale
;
3230 int remain
, missing
, v_slice
, reconfigure
;
3233 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3234 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3236 winno
= count_win(ws
, 0);
3237 if (winno
== 0 && count_win(ws
, 1) == 0)
3240 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3241 if (win
->transient
== 0 && win
->floating
== 0
3242 && win
->iconic
== 0)
3249 w_inc
= win
->sh
.width_inc
;
3250 w_base
= win
->sh
.base_width
;
3251 mwin
= ws
->l_state
.horizontal_mwin
;
3252 mscale
= ws
->l_state
.horizontal_msize
;
3253 stacks
= ws
->l_state
.horizontal_stacks
;
3256 w_inc
= win
->sh
.height_inc
;
3257 w_base
= win
->sh
.base_height
;
3258 mwin
= ws
->l_state
.vertical_mwin
;
3259 mscale
= ws
->l_state
.vertical_msize
;
3260 stacks
= ws
->l_state
.vertical_stacks
;
3264 if (stacks
> winno
- mwin
)
3265 stacks
= winno
- mwin
;
3269 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3270 if (mwin
&& winno
> mwin
) {
3271 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3275 win_g
.w
= v_slice
* mscale
;
3277 if (w_inc
> 1 && w_inc
< v_slice
) {
3278 /* adjust for window's requested size increment */
3279 remain
= (win_g
.w
- w_base
) % w_inc
;
3285 win_g
.x
+= r_g
.w
- msize
;
3288 colno
= split
= winno
/ stacks
;
3289 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3290 2 * border_width
) / stacks
);
3292 hrh
= r_g
.h
/ colno
;
3293 extra
= r_g
.h
- (colno
* hrh
);
3294 win_g
.h
= hrh
- 2 * border_width
;
3296 /* stack all the tiled windows */
3297 i
= j
= 0, s
= stacks
;
3298 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3299 if (win
->transient
!= 0 || win
->floating
!= 0)
3301 if (win
->iconic
!= 0)
3304 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3309 if (split
&& i
== split
) {
3310 colno
= (winno
- mwin
) / stacks
;
3311 if (s
<= (winno
- mwin
) % stacks
)
3313 split
= split
+ colno
;
3314 hrh
= (r_g
.h
/ colno
);
3315 extra
= r_g
.h
- (colno
* hrh
);
3319 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3320 win_g
.w
= (r_g
.w
- msize
-
3321 (stacks
* 2 * border_width
)) / stacks
;
3323 win_g
.w
+= (r_g
.w
- msize
-
3324 (stacks
* 2 * border_width
)) % stacks
;
3328 win_g
.h
= hrh
- 2 * border_width
;
3330 h_inc
= win
->sh
.width_inc
;
3331 h_base
= win
->sh
.base_width
;
3333 h_inc
= win
->sh
.height_inc
;
3334 h_base
= win
->sh
.base_height
;
3336 if (j
== colno
- 1) {
3337 win_g
.h
= hrh
+ extra
;
3338 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3339 /* adjust for window's requested size increment */
3340 remain
= (win_g
.h
- h_base
) % h_inc
;
3341 missing
= h_inc
- remain
;
3343 if (missing
<= extra
|| j
== 0) {
3355 win_g
.y
+= last_h
+ 2 * border_width
;
3357 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3359 win_g
.w
+= 2 * border_width
;
3360 win_g
.h
+= 2 * border_width
;
3365 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3366 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3370 WIDTH(win
) = win_g
.h
;
3371 HEIGHT(win
) = win_g
.w
;
3374 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3375 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3379 WIDTH(win
) = win_g
.w
;
3380 HEIGHT(win
) = win_g
.h
;
3384 if (bordered
!= win
->bordered
) {
3386 win
->bordered
= bordered
;
3394 if (XGetWindowAttributes(display
, win
->id
, &wa
))
3395 if (wa
.map_state
== IsUnmapped
)
3396 map_window_raised(win
->id
);
3404 /* now, stack all the floaters and transients */
3405 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3406 if (win
->transient
== 0 && win
->floating
== 0)
3408 if (win
->iconic
== 1)
3410 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3415 stack_floater(win
, ws
->r
);
3416 map_window_raised(win
->id
);
3420 stack_floater(fs_win
, ws
->r
);
3421 map_window_raised(fs_win
->id
);
3426 vertical_config(struct workspace
*ws
, int id
)
3428 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3432 case SWM_ARG_ID_STACKRESET
:
3433 case SWM_ARG_ID_STACKINIT
:
3434 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3435 ws
->l_state
.vertical_mwin
= 1;
3436 ws
->l_state
.vertical_stacks
= 1;
3438 case SWM_ARG_ID_MASTERSHRINK
:
3439 if (ws
->l_state
.vertical_msize
> 1)
3440 ws
->l_state
.vertical_msize
--;
3442 case SWM_ARG_ID_MASTERGROW
:
3443 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3444 ws
->l_state
.vertical_msize
++;
3446 case SWM_ARG_ID_MASTERADD
:
3447 ws
->l_state
.vertical_mwin
++;
3449 case SWM_ARG_ID_MASTERDEL
:
3450 if (ws
->l_state
.vertical_mwin
> 0)
3451 ws
->l_state
.vertical_mwin
--;
3453 case SWM_ARG_ID_STACKINC
:
3454 ws
->l_state
.vertical_stacks
++;
3456 case SWM_ARG_ID_STACKDEC
:
3457 if (ws
->l_state
.vertical_stacks
> 1)
3458 ws
->l_state
.vertical_stacks
--;
3460 case SWM_ARG_ID_FLIPLAYOUT
:
3461 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3469 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3471 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3473 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3477 horizontal_config(struct workspace
*ws
, int id
)
3479 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3482 case SWM_ARG_ID_STACKRESET
:
3483 case SWM_ARG_ID_STACKINIT
:
3484 ws
->l_state
.horizontal_mwin
= 1;
3485 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3486 ws
->l_state
.horizontal_stacks
= 1;
3488 case SWM_ARG_ID_MASTERSHRINK
:
3489 if (ws
->l_state
.horizontal_msize
> 1)
3490 ws
->l_state
.horizontal_msize
--;
3492 case SWM_ARG_ID_MASTERGROW
:
3493 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3494 ws
->l_state
.horizontal_msize
++;
3496 case SWM_ARG_ID_MASTERADD
:
3497 ws
->l_state
.horizontal_mwin
++;
3499 case SWM_ARG_ID_MASTERDEL
:
3500 if (ws
->l_state
.horizontal_mwin
> 0)
3501 ws
->l_state
.horizontal_mwin
--;
3503 case SWM_ARG_ID_STACKINC
:
3504 ws
->l_state
.horizontal_stacks
++;
3506 case SWM_ARG_ID_STACKDEC
:
3507 if (ws
->l_state
.horizontal_stacks
> 1)
3508 ws
->l_state
.horizontal_stacks
--;
3510 case SWM_ARG_ID_FLIPLAYOUT
:
3511 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3519 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3521 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3523 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3526 /* fullscreen view */
3528 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3530 struct swm_geometry gg
= *g
;
3531 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3532 int winno
, num_screens
;
3534 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3539 winno
= count_win(ws
, 0);
3540 if (winno
== 0 && count_win(ws
, 1) == 0)
3543 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3544 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3545 if (win
->transient
) {
3547 parent
= find_window(win
->transient
);
3551 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3553 * retain geometry for retrieval on exit from
3556 store_float_geom(win
, ws
->r
);
3557 win
->floatmaxed
= 1;
3560 /* only reconfigure if necessary */
3561 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3562 HEIGHT(win
) != gg
.h
) {
3568 WIDTH(win
) += 2 * border_width
;
3569 HEIGHT(win
) += 2 * border_width
;
3574 /* unmap only if we don't have multi screen */
3575 if (win
!= ws
->focus
)
3576 if (!(num_screens
> 1 || outputs
> 1))
3580 /* put the last transient on top */
3583 map_window_raised(parent
->id
);
3584 stack_floater(wintrans
, ws
->r
);
3585 focus_magic(wintrans
);
3590 send_to_ws(struct swm_region
*r
, union arg
*args
)
3592 int wsid
= args
->id
;
3593 struct ws_win
*win
= NULL
, *parent
;
3594 struct workspace
*ws
, *nws
;
3595 xcb_atom_t ws_idx_atom
= XCB_ATOM_NONE
;
3596 char ws_idx_str
[SWM_PROPLEN
];
3599 if (wsid
>= workspace_limit
)
3602 if (r
&& r
->ws
&& r
->ws
->focus
)
3608 if (win
->ws
->idx
== wsid
)
3611 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%lx\n", win
->id
);
3614 nws
= &win
->s
->ws
[wsid
];
3616 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3618 if (win
->transient
) {
3619 parent
= find_window(win
->transient
);
3621 unmap_window(parent
);
3622 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3623 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3628 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3629 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3630 if (TAILQ_EMPTY(&ws
->winlist
))
3631 r
->ws
->focus
= NULL
;
3634 /* Try to update the window's workspace property */
3635 ws_idx_atom
= get_atom_from_string("_SWM_WS");
3637 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3639 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3641 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
3642 ws_idx_atom
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
3651 pressbutton(struct swm_region
*r
, union arg
*args
)
3653 XTestFakeButtonEvent(display
, args
->id
, True
, CurrentTime
);
3654 XTestFakeButtonEvent(display
, args
->id
, False
, CurrentTime
);
3658 raise_toggle(struct swm_region
*r
, union arg
*args
)
3660 if (r
== NULL
|| r
->ws
== NULL
)
3663 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3665 /* bring floaters back to top */
3666 if (r
->ws
->always_raise
== 0)
3671 iconify(struct swm_region
*r
, union arg
*args
)
3675 if (r
->ws
->focus
== NULL
)
3677 unmap_window(r
->ws
->focus
);
3678 update_iconic(r
->ws
->focus
, 1);
3680 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3681 drain_enter_notify();
3682 r
->ws
->focus
= NULL
;
3683 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3688 get_win_name(xcb_window_t win
)
3690 unsigned char *prop
= NULL
;
3691 unsigned long nbytes
, nitems
;
3693 /* try _NET_WM_NAME first */
3694 if (get_property(win
, a_netwmname
, 0L, a_utf8_string
, NULL
, &nbytes
,
3697 if (get_property(win
, a_netwmname
, nbytes
, a_utf8_string
,
3698 &nitems
, NULL
, &prop
))
3702 /* fallback to WM_NAME */
3703 if (!get_property(win
, a_wmname
, 0L, a_string
, NULL
, &nbytes
, &prop
))
3706 if (get_property(win
, a_wmname
, nbytes
, a_string
, &nitems
, NULL
, &prop
))
3713 uniconify(struct swm_region
*r
, union arg
*args
)
3717 unsigned char *name
;
3720 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3722 if (r
== NULL
|| r
->ws
== NULL
)
3725 /* make sure we have anything to uniconify */
3726 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3727 if (win
->ws
== NULL
)
3728 continue; /* should never happen */
3729 if (win
->iconic
== 0)
3737 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3739 spawn_select(r
, args
, "search", &searchpid
);
3741 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3744 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3745 if (win
->ws
== NULL
)
3746 continue; /* should never happen */
3747 if (win
->iconic
== 0)
3750 name
= get_win_name(win
->id
);
3753 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
3761 name_workspace(struct swm_region
*r
, union arg
*args
)
3765 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3771 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3773 spawn_select(r
, args
, "name_workspace", &searchpid
);
3775 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3778 fprintf(lfile
, "%s", "");
3783 search_workspace(struct swm_region
*r
, union arg
*args
)
3786 struct workspace
*ws
;
3789 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3795 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3797 spawn_select(r
, args
, "search", &searchpid
);
3799 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3802 for (i
= 0; i
< workspace_limit
; i
++) {
3806 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3807 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3814 search_win_cleanup(void)
3816 struct search_window
*sw
= NULL
;
3818 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3819 xcb_destroy_window(conn
, sw
->indicator
);
3820 XFreeGC(display
, sw
->gc
);
3821 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3827 search_win(struct swm_region
*r
, union arg
*args
)
3829 struct ws_win
*win
= NULL
;
3830 struct search_window
*sw
= NULL
;
3837 XRectangle ibox
, lbox
;
3839 DNPRINTF(SWM_D_MISC
, "search_win\n");
3842 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3844 spawn_select(r
, args
, "search", &searchpid
);
3846 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3849 TAILQ_INIT(&search_wl
);
3852 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3853 if (win
->iconic
== 1)
3856 sw
= calloc(1, sizeof(struct search_window
));
3858 warn("search_win: calloc");
3860 search_win_cleanup();
3866 snprintf(s
, sizeof s
, "%d", i
);
3869 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
3871 w
= XCreateSimpleWindow(display
,
3872 win
->id
, 0, 0,lbox
.width
+ 4,
3873 bar_fs_extents
->max_logical_extent
.height
, 1,
3874 r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
,
3875 r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
3878 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3880 sw
->gc
= XCreateGC(display
, w
, 0, &gcv
);
3881 map_window_raised(w
);
3882 XSetForeground(display
, sw
->gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
3884 DRAWSTRING(display
, w
, bar_fs
, sw
->gc
, 2,
3885 (bar_fs_extents
->max_logical_extent
.height
-
3886 lbox
.height
) / 2 - lbox
.y
, s
, len
);
3888 fprintf(lfile
, "%d\n", i
);
3896 search_resp_uniconify(char *resp
, unsigned long len
)
3898 unsigned char *name
;
3902 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3904 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3905 if (win
->iconic
== 0)
3907 name
= get_win_name(win
->id
);
3910 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
3915 if (strncmp(s
, resp
, len
) == 0) {
3916 /* XXX this should be a callback to generalize */
3917 update_iconic(win
, 0);
3926 search_resp_name_workspace(char *resp
, unsigned long len
)
3928 struct workspace
*ws
;
3930 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3932 if (search_r
->ws
== NULL
)
3937 free(search_r
->ws
->name
);
3938 search_r
->ws
->name
= NULL
;
3942 ws
->name
= strdup(resp
);
3943 if (ws
->name
== NULL
) {
3944 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
3945 "strdup: %s", strerror(errno
));
3952 search_resp_search_workspace(char *resp
, unsigned long len
)
3959 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
3963 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
3970 ws_idx
= strtonum(q
, 1, workspace_limit
, &errstr
);
3972 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
3979 switchws(search_r
, &a
);
3983 search_resp_search_window(char *resp
, unsigned long len
)
3988 struct search_window
*sw
;
3990 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
3994 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
3999 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
4001 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
4008 TAILQ_FOREACH(sw
, &search_wl
, entry
)
4009 if (idx
== sw
->idx
) {
4015 #define MAX_RESP_LEN 1024
4018 search_do_resp(void)
4024 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
4029 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
4030 warn("search: calloc");
4034 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
4036 warn("search: read error");
4039 resp
[rbytes
] = '\0';
4042 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4043 * newline, so work around that by sanitizing the resp now.
4045 resp
[strcspn(resp
, "\n")] = '\0';
4048 switch (search_resp_action
) {
4049 case SWM_SEARCH_UNICONIFY
:
4050 search_resp_uniconify(resp
, len
);
4052 case SWM_SEARCH_NAME_WORKSPACE
:
4053 search_resp_name_workspace(resp
, len
);
4055 case SWM_SEARCH_SEARCH_WORKSPACE
:
4056 search_resp_search_workspace(resp
, len
);
4058 case SWM_SEARCH_SEARCH_WINDOW
:
4059 search_resp_search_window(resp
, len
);
4064 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4065 search_win_cleanup();
4067 search_resp_action
= SWM_SEARCH_NONE
;
4068 close(select_resp_pipe
[0]);
4073 wkill(struct swm_region
*r
, union arg
*args
)
4075 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4077 if (r
->ws
->focus
== NULL
)
4080 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4081 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4083 if (r
->ws
->focus
->can_delete
)
4084 client_msg(r
->ws
->focus
, adelete
);
4089 floating_toggle_win(struct ws_win
*win
)
4091 struct swm_region
*r
;
4101 /* reject floating toggles in max stack mode */
4102 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4105 if (win
->floating
) {
4106 if (!win
->floatmaxed
) {
4107 /* retain position for refloat */
4108 store_float_geom(win
, r
);
4112 if (win
->g_floatvalid
) {
4113 /* refloat at last floating relative position */
4114 X(win
) = win
->g_float
.x
+ X(r
);
4115 Y(win
) = win
->g_float
.y
+ Y(r
);
4116 WIDTH(win
) = win
->g_float
.w
;
4117 HEIGHT(win
) = win
->g_float
.h
;
4122 ewmh_update_actions(win
);
4128 floating_toggle(struct swm_region
*r
, union arg
*args
)
4130 struct ws_win
*win
= r
->ws
->focus
;
4136 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4137 _NET_WM_STATE_TOGGLE
);
4140 if (focus_mode
== SWM_FOCUS_DEFAULT
)
4141 drain_enter_notify();
4143 if (win
== win
->ws
->focus
) {
4144 a
.id
= SWM_ARG_ID_FOCUSCUR
;
4145 focus(win
->ws
->r
, &a
);
4150 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4152 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4154 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4156 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4159 if (X(win
) + BORDER(win
) < X(r
)) {
4161 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4163 X(win
) = X(r
) - BORDER(win
);
4166 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4168 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4170 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4173 if (Y(win
) + BORDER(win
) < Y(r
)) {
4175 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4177 Y(win
) = Y(r
) - BORDER(win
);
4183 if (HEIGHT(win
) < 1)
4189 update_window(struct ws_win
*win
)
4194 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
4195 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
4196 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
4200 wc
[3] = HEIGHT(win
);
4201 wc
[4] = BORDER(win
);
4203 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
4204 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
4205 wc
[3], YESNO(win
->bordered
));
4207 xcb_configure_window(conn
, win
->id
, mask
, wc
);
4210 #define SWM_RESIZE_STEPS (50)
4213 resize(struct ws_win
*win
, union arg
*args
)
4217 struct swm_region
*r
= NULL
;
4218 int resize_step
= 0;
4222 struct swm_geometry g
;
4223 int top
= 0, left
= 0;
4226 unsigned int shape
; /* cursor style */
4232 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4235 DNPRINTF(SWM_D_MOUSE
, "resize: window: 0x%lx, floating: %s, "
4236 "transient: 0x%lx\n", win
->id
, YESNO(win
->floating
),
4239 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4242 /* reject resizes in max mode for floaters (transient ok) */
4243 if (win
->floatmaxed
)
4247 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4253 case SWM_ARG_ID_WIDTHSHRINK
:
4254 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4257 case SWM_ARG_ID_WIDTHGROW
:
4258 WIDTH(win
) += SWM_RESIZE_STEPS
;
4261 case SWM_ARG_ID_HEIGHTSHRINK
:
4262 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4265 case SWM_ARG_ID_HEIGHTGROW
:
4266 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4273 constrain_window(win
, r
, 1);
4275 store_float_geom(win
,r
);
4279 if (focus_mode
== SWM_FOCUS_DEFAULT
)
4280 drain_enter_notify();
4282 /* get cursor offset from window root */
4283 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4288 if (wx
< WIDTH(win
) / 2)
4291 if (wy
< HEIGHT(win
) / 2)
4294 if (args
->id
== SWM_ARG_ID_CENTER
)
4297 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
4299 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
4301 cursor
= XCreateFontCursor(display
, shape
);
4303 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4304 GrabModeAsync
, None
, cursor
, CurrentTime
) != GrabSuccess
) {
4305 XFreeCursor(display
, cursor
);
4310 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4311 SubstructureRedirectMask
, &ev
);
4313 case ConfigureRequest
:
4316 handler
[ev
.type
](&ev
);
4319 /* cursor offset/delta from start of the operation */
4320 dx
= ev
.xmotion
.x_root
- x
;
4321 dy
= ev
.xmotion
.y_root
- y
;
4327 if (args
->id
== SWM_ARG_ID_CENTER
) {
4328 if (g
.h
/ 2 + dy
< 1)
4332 HEIGHT(win
) = g
.h
+ 2 * dy
;
4340 HEIGHT(win
) = g
.h
+ dy
;
4347 if (args
->id
== SWM_ARG_ID_CENTER
) {
4348 if (g
.w
/ 2 + dx
< 1)
4352 WIDTH(win
) = g
.w
+ 2 * dx
;
4360 WIDTH(win
) = g
.w
+ dx
;
4363 constrain_window(win
, r
, 1);
4365 /* not free, don't sync more than 120 times / second */
4366 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4367 time
= ev
.xmotion
.time
;
4368 XSync(display
, False
);
4373 } while (ev
.type
!= ButtonRelease
);
4375 XSync(display
, False
);
4378 store_float_geom(win
,r
);
4380 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4381 XFreeCursor(display
, cursor
);
4384 drain_enter_notify();
4388 resize_step(struct swm_region
*r
, union arg
*args
)
4390 struct ws_win
*win
= NULL
;
4392 if (r
&& r
->ws
&& r
->ws
->focus
)
4400 #define SWM_MOVE_STEPS (50)
4403 move(struct ws_win
*win
, union arg
*args
)
4408 struct swm_region
*r
= NULL
;
4418 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4421 DNPRINTF(SWM_D_MOUSE
, "move: window: 0x%lx, floating: %s, transient: "
4422 "0x%lx\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4424 /* in max_stack mode should only move transients */
4425 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4429 if (win
->floating
== 0 && !win
->transient
) {
4430 store_float_geom(win
, r
);
4431 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4434 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4441 case SWM_ARG_ID_MOVELEFT
:
4442 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4445 case SWM_ARG_ID_MOVERIGHT
:
4446 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4449 case SWM_ARG_ID_MOVEUP
:
4450 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4453 case SWM_ARG_ID_MOVEDOWN
:
4454 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4461 constrain_window(win
, r
, 0);
4463 store_float_geom(win
, r
);
4467 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4468 GrabModeAsync
, None
, XCreateFontCursor(display
, XC_fleur
),
4469 CurrentTime
) != GrabSuccess
)
4472 /* get cursor offset from window root */
4473 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4477 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4478 SubstructureRedirectMask
, &ev
);
4480 case ConfigureRequest
:
4483 handler
[ev
.type
](&ev
);
4486 X(win
) = ev
.xmotion
.x_root
- wx
- border_width
;
4487 Y(win
) = ev
.xmotion
.y_root
- wy
- border_width
;
4489 constrain_window(win
, r
, 0);
4491 /* not free, don't sync more than 120 times / second */
4492 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4493 time
= ev
.xmotion
.time
;
4494 XSync(display
, False
);
4499 } while (ev
.type
!= ButtonRelease
);
4501 XSync(display
, False
);
4504 store_float_geom(win
, r
);
4505 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4508 drain_enter_notify();
4512 move_step(struct swm_region
*r
, union arg
*args
)
4514 struct ws_win
*win
= NULL
;
4516 if (r
&& r
->ws
&& r
->ws
->focus
)
4521 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4528 /* user/key callable function IDs */
4617 kf_search_workspace
,
4619 kf_dumpwins
, /* MUST BE LAST */
4623 /* key definitions */
4625 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4630 char name
[SWM_FUNCNAME_LEN
];
4631 void (*func
)(struct swm_region
*r
, union arg
*);
4633 } keyfuncs
[kf_invalid
+ 1] = {
4634 /* name function argument */
4635 { "cycle_layout", cycle_layout
, {0} },
4636 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4637 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4638 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4639 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4640 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4641 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4642 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4643 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4644 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4645 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4646 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4647 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4648 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4649 { "quit", quit
, {0} },
4650 { "restart", restart
, {0} },
4651 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4652 { "ws_1", switchws
, {.id
= 0} },
4653 { "ws_2", switchws
, {.id
= 1} },
4654 { "ws_3", switchws
, {.id
= 2} },
4655 { "ws_4", switchws
, {.id
= 3} },
4656 { "ws_5", switchws
, {.id
= 4} },
4657 { "ws_6", switchws
, {.id
= 5} },
4658 { "ws_7", switchws
, {.id
= 6} },
4659 { "ws_8", switchws
, {.id
= 7} },
4660 { "ws_9", switchws
, {.id
= 8} },
4661 { "ws_10", switchws
, {.id
= 9} },
4662 { "ws_11", switchws
, {.id
= 10} },
4663 { "ws_12", switchws
, {.id
= 11} },
4664 { "ws_13", switchws
, {.id
= 12} },
4665 { "ws_14", switchws
, {.id
= 13} },
4666 { "ws_15", switchws
, {.id
= 14} },
4667 { "ws_16", switchws
, {.id
= 15} },
4668 { "ws_17", switchws
, {.id
= 16} },
4669 { "ws_18", switchws
, {.id
= 17} },
4670 { "ws_19", switchws
, {.id
= 18} },
4671 { "ws_20", switchws
, {.id
= 19} },
4672 { "ws_21", switchws
, {.id
= 20} },
4673 { "ws_22", switchws
, {.id
= 21} },
4674 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4675 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4676 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4677 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4678 { "ws_prior", priorws
, {0} },
4679 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4680 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4681 { "mvws_1", send_to_ws
, {.id
= 0} },
4682 { "mvws_2", send_to_ws
, {.id
= 1} },
4683 { "mvws_3", send_to_ws
, {.id
= 2} },
4684 { "mvws_4", send_to_ws
, {.id
= 3} },
4685 { "mvws_5", send_to_ws
, {.id
= 4} },
4686 { "mvws_6", send_to_ws
, {.id
= 5} },
4687 { "mvws_7", send_to_ws
, {.id
= 6} },
4688 { "mvws_8", send_to_ws
, {.id
= 7} },
4689 { "mvws_9", send_to_ws
, {.id
= 8} },
4690 { "mvws_10", send_to_ws
, {.id
= 9} },
4691 { "mvws_11", send_to_ws
, {.id
= 10} },
4692 { "mvws_12", send_to_ws
, {.id
= 11} },
4693 { "mvws_13", send_to_ws
, {.id
= 12} },
4694 { "mvws_14", send_to_ws
, {.id
= 13} },
4695 { "mvws_15", send_to_ws
, {.id
= 14} },
4696 { "mvws_16", send_to_ws
, {.id
= 15} },
4697 { "mvws_17", send_to_ws
, {.id
= 16} },
4698 { "mvws_18", send_to_ws
, {.id
= 17} },
4699 { "mvws_19", send_to_ws
, {.id
= 18} },
4700 { "mvws_20", send_to_ws
, {.id
= 19} },
4701 { "mvws_21", send_to_ws
, {.id
= 20} },
4702 { "mvws_22", send_to_ws
, {.id
= 21} },
4703 { "bar_toggle", bar_toggle
, {0} },
4704 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4705 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4706 { "float_toggle", floating_toggle
,{0} },
4707 { "version", version
, {0} },
4708 { "spawn_custom", dummykeyfunc
, {0} },
4709 { "iconify", iconify
, {0} },
4710 { "uniconify", uniconify
, {0} },
4711 { "raise_toggle", raise_toggle
, {0} },
4712 { "button2", pressbutton
, {2} },
4713 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4714 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4715 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4716 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4717 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4718 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4719 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4720 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4721 { "name_workspace", name_workspace
, {0} },
4722 { "search_workspace", search_workspace
, {0} },
4723 { "search_win", search_win
, {0} },
4724 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4725 { "invalid key func", NULL
, {0} },
4728 RB_ENTRY(key
) entry
;
4731 enum keyfuncid funcid
;
4734 RB_HEAD(key_tree
, key
);
4737 key_cmp(struct key
*kp1
, struct key
*kp2
)
4739 if (kp1
->keysym
< kp2
->keysym
)
4741 if (kp1
->keysym
> kp2
->keysym
)
4744 if (kp1
->mod
< kp2
->mod
)
4746 if (kp1
->mod
> kp2
->mod
)
4752 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
4753 struct key_tree keys
;
4756 enum { client_click
, root_click
};
4758 unsigned int action
;
4760 unsigned int button
;
4761 void (*func
)(struct ws_win
*, union arg
*);
4764 /* action key mouse button func args */
4765 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4766 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4767 { client_click
, MODKEY
, Button1
, move
, {0} },
4771 update_modkey(unsigned int mod
)
4777 RB_FOREACH(kp
, key_tree
, &keys
)
4778 if (kp
->mod
& ShiftMask
)
4779 kp
->mod
= mod
| ShiftMask
;
4783 for (i
= 0; i
< LENGTH(buttons
); i
++)
4784 if (buttons
[i
].mask
& ShiftMask
)
4785 buttons
[i
].mask
= mod
| ShiftMask
;
4787 buttons
[i
].mask
= mod
;
4792 TAILQ_ENTRY(spawn_prog
) entry
;
4797 TAILQ_HEAD(spawn_list
, spawn_prog
);
4798 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4801 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4804 struct spawn_prog
*prog
= NULL
;
4806 char *ap
, **real_args
;
4808 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4811 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4812 if (!strcasecmp(spawn_name
, prog
->name
))
4816 warnx("spawn_custom: program %s not found", spawn_name
);
4820 /* make room for expanded args */
4821 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4822 err(1, "spawn_custom: calloc real_args");
4824 /* expand spawn_args into real_args */
4825 for (i
= 0; i
< prog
->argc
; i
++) {
4827 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4828 if (!strcasecmp(ap
, "$bar_border")) {
4830 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4832 err(1, "spawn_custom border color");
4833 } else if (!strcasecmp(ap
, "$bar_color")) {
4835 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4837 err(1, "spawn_custom bar color");
4838 } else if (!strcasecmp(ap
, "$bar_font")) {
4839 if ((real_args
[i
] = strdup(bar_fonts
))
4841 err(1, "spawn_custom bar fonts");
4842 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4844 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4846 err(1, "spawn_custom color font");
4847 } else if (!strcasecmp(ap
, "$color_focus")) {
4849 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4851 err(1, "spawn_custom color focus");
4852 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4854 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4856 err(1, "spawn_custom color unfocus");
4858 /* no match --> copy as is */
4859 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4860 err(1, "spawn_custom strdup(ap)");
4862 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4867 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4868 for (i
= 0; i
< prog
->argc
; i
++)
4869 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4870 DNPRINTF(SWM_D_SPAWN
, "\n");
4872 *ret_args
= real_args
;
4873 return (prog
->argc
);
4877 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4883 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4887 spawn(r
->ws
->idx
, &a
, 1);
4889 for (i
= 0; i
< spawn_argc
; i
++)
4895 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4901 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4905 if (pipe(select_list_pipe
) == -1)
4906 err(1, "pipe error");
4907 if (pipe(select_resp_pipe
) == -1)
4908 err(1, "pipe error");
4910 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4911 err(1, "could not disable SIGPIPE");
4912 switch (*pid
= fork()) {
4914 err(1, "cannot fork");
4917 if (dup2(select_list_pipe
[0], 0) == -1)
4919 if (dup2(select_resp_pipe
[1], 1) == -1)
4921 close(select_list_pipe
[1]);
4922 close(select_resp_pipe
[0]);
4923 spawn(r
->ws
->idx
, &a
, 0);
4925 default: /* parent */
4926 close(select_list_pipe
[0]);
4927 close(select_resp_pipe
[1]);
4931 for (i
= 0; i
< spawn_argc
; i
++)
4937 spawn_insert(char *name
, char *args
)
4939 char *arg
, *cp
, *ptr
;
4940 struct spawn_prog
*sp
;
4942 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
4944 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
4945 err(1, "spawn_insert: malloc");
4946 if ((sp
->name
= strdup(name
)) == NULL
)
4947 err(1, "spawn_insert: strdup");
4949 /* convert the arguments to an argument list */
4950 if ((ptr
= cp
= strdup(args
)) == NULL
)
4951 err(1, "spawn_insert: strdup");
4952 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
4953 /* empty field; skip it */
4958 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
4959 sizeof *sp
->argv
)) == NULL
)
4960 err(1, "spawn_insert: realloc");
4961 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
4962 err(1, "spawn_insert: strdup");
4966 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
4967 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
4971 spawn_remove(struct spawn_prog
*sp
)
4975 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
4977 TAILQ_REMOVE(&spawns
, sp
, entry
);
4978 for (i
= 0; i
< sp
->argc
; i
++)
4984 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
4988 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
4990 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
4993 spawn_insert(name
, args
);
4995 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
4999 setspawn(char *name
, char *args
)
5001 struct spawn_prog
*sp
;
5003 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5008 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5009 if (!strcmp(sp
->name
, name
)) {
5013 spawn_replace(sp
, name
, args
);
5014 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5018 if (*args
== '\0') {
5019 warnx("error: setspawn: cannot find program: %s", name
);
5023 spawn_insert(name
, args
);
5024 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5028 setconfspawn(char *selector
, char *value
, int flags
)
5030 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
5032 setspawn(selector
, value
);
5034 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
5041 setconfspawn("term", "xterm", 0);
5042 setconfspawn("spawn_term", "xterm", 0);
5043 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5044 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5045 setconfspawn("lock", "xlock", 0);
5046 setconfspawn("initscr", "initscreen.sh", 0);
5047 setconfspawn("menu", "dmenu_run"
5050 " -nf $bar_font_color"
5052 " -sf $bar_color", 0);
5053 setconfspawn("search", "dmenu"
5057 " -nf $bar_font_color"
5059 " -sf $bar_color", 0);
5060 setconfspawn("name_workspace", "dmenu"
5064 " -nf $bar_font_color"
5066 " -sf $bar_color", 0);
5070 #define SWM_MODNAME_SIZE 32
5071 #define SWM_KEY_WS "\n+ \t"
5073 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5077 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5078 if (mod
== NULL
|| ks
== NULL
) {
5079 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5082 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5083 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5089 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5090 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5092 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5093 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5095 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5097 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5099 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5101 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5103 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5105 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5106 *mod
|= ControlMask
;
5108 *ks
= XStringToKeysym(name
);
5109 XConvertCase(*ks
, ks
, &uks
);
5110 if (ks
== NoSymbol
) {
5112 "parsekeys: invalid key %s\n",
5118 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5123 strdupsafe(char *str
)
5128 return (strdup(str
));
5132 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5136 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5137 keyfuncs
[kfid
].name
, spawn_name
);
5139 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5140 err(1, "key_insert: malloc");
5145 kp
->spawn_name
= strdupsafe(spawn_name
);
5146 RB_INSERT(key_tree
, &keys
, kp
);
5148 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5152 key_lookup(unsigned int mod
, KeySym ks
)
5159 return (RB_FIND(key_tree
, &keys
, &kp
));
5163 key_remove(struct key
*kp
)
5165 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5167 RB_REMOVE(key_tree
, &keys
, kp
);
5168 free(kp
->spawn_name
);
5171 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5175 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5178 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5182 key_insert(mod
, ks
, kfid
, spawn_name
);
5184 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5188 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5193 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5194 keyfuncs
[kfid
].name
, spawn_name
);
5196 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5197 if (kfid
== kf_invalid
)
5200 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5201 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5204 if (kfid
== kf_invalid
) {
5205 warnx("error: setkeybinding: cannot find mod/key combination");
5206 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5210 key_insert(mod
, ks
, kfid
, spawn_name
);
5211 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5215 setconfbinding(char *selector
, char *value
, int flags
)
5217 enum keyfuncid kfid
;
5220 struct spawn_prog
*sp
;
5221 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5222 if (selector
== NULL
) {
5223 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5224 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5226 setkeybinding(mod
, ks
, kfid
, NULL
);
5231 /* search by key function name */
5232 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
5233 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5234 SWM_FUNCNAME_LEN
) == 0) {
5235 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5237 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5238 setkeybinding(mod
, ks
, kfid
, NULL
);
5244 /* search by custom spawn name */
5245 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5246 if (strcasecmp(selector
, sp
->name
) == 0) {
5247 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5249 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5250 setkeybinding(mod
, ks
, kf_spawn_custom
,
5257 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5264 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
5265 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, kf_flip_layout
, NULL
);
5266 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
5267 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
5268 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
5269 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
5270 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
5271 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
5272 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
5273 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
5274 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
5275 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
5276 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
5277 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
5278 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_custom
,"term");
5279 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
5280 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
5281 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
5282 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
5283 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
5284 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
5285 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
5286 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
5287 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
5288 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
5289 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
5290 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
5291 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
5292 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
5293 setkeybinding(MODKEY
, XK_F1
, kf_ws_11
, NULL
);
5294 setkeybinding(MODKEY
, XK_F2
, kf_ws_12
, NULL
);
5295 setkeybinding(MODKEY
, XK_F3
, kf_ws_13
, NULL
);
5296 setkeybinding(MODKEY
, XK_F4
, kf_ws_14
, NULL
);
5297 setkeybinding(MODKEY
, XK_F5
, kf_ws_15
, NULL
);
5298 setkeybinding(MODKEY
, XK_F6
, kf_ws_16
, NULL
);
5299 setkeybinding(MODKEY
, XK_F7
, kf_ws_17
, NULL
);
5300 setkeybinding(MODKEY
, XK_F8
, kf_ws_18
, NULL
);
5301 setkeybinding(MODKEY
, XK_F9
, kf_ws_19
, NULL
);
5302 setkeybinding(MODKEY
, XK_F10
, kf_ws_20
, NULL
);
5303 setkeybinding(MODKEY
, XK_F11
, kf_ws_21
, NULL
);
5304 setkeybinding(MODKEY
, XK_F12
, kf_ws_22
, NULL
);
5305 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
5306 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
5307 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
5308 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
5309 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
5310 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
5311 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
5312 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
5313 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
5314 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
5315 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
5316 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
5317 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
5318 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
5319 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
5320 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
5321 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
5322 setkeybinding(MODKEY
|ShiftMask
, XK_F1
, kf_mvws_11
, NULL
);
5323 setkeybinding(MODKEY
|ShiftMask
, XK_F2
, kf_mvws_12
, NULL
);
5324 setkeybinding(MODKEY
|ShiftMask
, XK_F3
, kf_mvws_13
, NULL
);
5325 setkeybinding(MODKEY
|ShiftMask
, XK_F4
, kf_mvws_14
, NULL
);
5326 setkeybinding(MODKEY
|ShiftMask
, XK_F5
, kf_mvws_15
, NULL
);
5327 setkeybinding(MODKEY
|ShiftMask
, XK_F6
, kf_mvws_16
, NULL
);
5328 setkeybinding(MODKEY
|ShiftMask
, XK_F7
, kf_mvws_17
, NULL
);
5329 setkeybinding(MODKEY
|ShiftMask
, XK_F8
, kf_mvws_18
, NULL
);
5330 setkeybinding(MODKEY
|ShiftMask
, XK_F9
, kf_mvws_19
, NULL
);
5331 setkeybinding(MODKEY
|ShiftMask
, XK_F10
, kf_mvws_20
, NULL
);
5332 setkeybinding(MODKEY
|ShiftMask
, XK_F11
, kf_mvws_21
, NULL
);
5333 setkeybinding(MODKEY
|ShiftMask
, XK_F12
, kf_mvws_22
, NULL
);
5334 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
5335 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
5336 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
5337 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
5338 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
5339 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
5340 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
5341 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
5342 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
5343 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
5344 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
5345 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
5346 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
5347 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
5348 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
5349 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
5350 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
5351 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
5352 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
5353 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
5354 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
5355 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
5356 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
5357 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
5358 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
5359 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
5361 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
5370 while (RB_EMPTY(&keys
) == 0) {
5371 kp
= RB_ROOT(&keys
);
5377 setkeymapping(char *selector
, char *value
, int flags
)
5379 char keymapping_file
[PATH_MAX
];
5380 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5381 if (value
[0] == '~')
5382 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5383 pwd
->pw_dir
, &value
[1]);
5385 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5387 /* load new key bindings; if it fails, revert to default bindings */
5388 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5392 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5397 updatenumlockmask(void)
5400 XModifierKeymap
*modmap
;
5402 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
5404 modmap
= XGetModifierMapping(display
);
5405 for (i
= 0; i
< 8; i
++)
5406 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
5407 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
5408 == XKeysymToKeycode(display
, XK_Num_Lock
))
5409 numlockmask
= (1 << i
);
5411 XFreeModifiermap(modmap
);
5420 unsigned int modifiers
[] =
5421 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
5424 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5425 updatenumlockmask();
5427 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
5428 for (k
= 0; k
< num_screens
; k
++) {
5429 if (TAILQ_EMPTY(&screens
[k
].rl
))
5431 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
5433 RB_FOREACH(kp
, key_tree
, &keys
) {
5434 if ((code
= XKeysymToKeycode(display
, kp
->keysym
)))
5435 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5436 XGrabKey(display
, code
,
5437 kp
->mod
| modifiers
[j
],
5438 screens
[k
].root
, True
,
5439 GrabModeAsync
, GrabModeAsync
);
5445 grabbuttons(struct ws_win
*win
, int focused
)
5448 unsigned int modifiers
[] =
5449 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5451 updatenumlockmask();
5452 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, win
->id
,
5453 XCB_BUTTON_MASK_ANY
);
5455 for (i
= 0; i
< LENGTH(buttons
); i
++)
5456 if (buttons
[i
].action
== client_click
)
5457 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5458 xcb_grab_button(conn
, False
, win
->id
,
5460 XCB_GRAB_MODE_ASYNC
,
5467 xcb_grab_button(conn
, False
, win
->id
, BUTTONMASK
,
5468 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_SYNC
,
5469 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_ANY
,
5470 XCB_BUTTON_MASK_ANY
);
5473 const char *quirkname
[] = {
5474 "NONE", /* config string for "no value" */
5483 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5484 #define SWM_Q_WS "\n|+ \t"
5486 parsequirks(char *qstr
, unsigned long *quirk
)
5496 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5498 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5499 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5500 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5501 DNPRINTF(SWM_D_QUIRK
,
5502 "parsequirks: %s\n", name
);
5507 *quirk
|= 1 << (i
-1);
5511 if (i
>= LENGTH(quirkname
)) {
5512 DNPRINTF(SWM_D_QUIRK
,
5513 "parsequirks: invalid quirk [%s]\n", name
);
5521 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5525 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5528 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5529 err(1, "quirk_insert: malloc");
5530 if ((qp
->class = strdup(class)) == NULL
)
5531 err(1, "quirk_insert: strdup");
5532 if ((qp
->name
= strdup(name
)) == NULL
)
5533 err(1, "quirk_insert: strdup");
5536 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5538 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5542 quirk_remove(struct quirk
*qp
)
5544 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5545 qp
->name
, qp
->quirk
);
5547 TAILQ_REMOVE(&quirks
, qp
, entry
);
5552 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5556 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5557 unsigned long quirk
)
5559 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5560 qp
->name
, qp
->quirk
);
5563 quirk_insert(class, name
, quirk
);
5565 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5569 setquirk(const char *class, const char *name
, unsigned long quirk
)
5573 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5576 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5577 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5581 quirk_replace(qp
, class, name
, quirk
);
5582 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5587 warnx("error: setquirk: cannot find class/name combination");
5591 quirk_insert(class, name
, quirk
);
5592 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5596 setconfquirk(char *selector
, char *value
, int flags
)
5598 char *cp
, *class, *name
;
5600 unsigned long quirks
;
5601 if (selector
== NULL
)
5603 if ((cp
= strchr(selector
, ':')) == NULL
)
5608 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5609 setquirk(class, name
, quirks
);
5616 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5617 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5618 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5619 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5620 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5621 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5622 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5623 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5624 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5625 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5626 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5627 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5628 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5631 /* conf file stuff */
5632 #define SWM_CONF_FILE "spectrwm.conf"
5633 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5637 SWM_S_BAR_AT_BOTTOM
,
5638 SWM_S_BAR_BORDER_WIDTH
,
5645 SWM_S_CLOCK_ENABLED
,
5648 SWM_S_CYCLE_VISIBLE
,
5650 SWM_S_DISABLE_BORDER
,
5652 SWM_S_FOCUS_CLOSE_WRAP
,
5653 SWM_S_FOCUS_DEFAULT
,
5659 SWM_S_STACK_ENABLED
,
5661 SWM_S_TITLE_CLASS_ENABLED
,
5662 SWM_S_TITLE_NAME_ENABLED
,
5663 SWM_S_URGENT_ENABLED
,
5664 SWM_S_VERBOSE_LAYOUT
,
5665 SWM_S_WINDOW_NAME_ENABLED
,
5666 SWM_S_WORKSPACE_LIMIT
5670 setconfvalue(char *selector
, char *value
, int flags
)
5676 case SWM_S_BAR_ACTION
:
5678 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5679 err(1, "setconfvalue: bar_action");
5681 case SWM_S_BAR_AT_BOTTOM
:
5682 bar_at_bottom
= atoi(value
);
5684 case SWM_S_BAR_BORDER_WIDTH
:
5685 bar_border_width
= atoi(value
);
5686 if (bar_border_width
< 0)
5687 bar_border_width
= 0;
5689 case SWM_S_BAR_DELAY
:
5690 bar_delay
= atoi(value
);
5692 case SWM_S_BAR_ENABLED
:
5693 bar_enabled
= atoi(value
);
5695 case SWM_S_BAR_FONT
:
5697 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5698 err(1, "setconfvalue: asprintf: failed to allocate "
5699 "memory for bar_fonts.");
5703 case SWM_S_BAR_FORMAT
:
5705 if ((bar_format
= strdup(value
)) == NULL
)
5706 err(1, "setconfvalue: bar_format");
5708 case SWM_S_BAR_JUSTIFY
:
5709 if (!strcmp(value
, "left"))
5710 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5711 else if (!strcmp(value
, "center"))
5712 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5713 else if (!strcmp(value
, "right"))
5714 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5716 errx(1, "invalid bar_justify");
5718 case SWM_S_BORDER_WIDTH
:
5719 border_width
= atoi(value
);
5720 if (border_width
< 0)
5723 case SWM_S_CLOCK_ENABLED
:
5724 clock_enabled
= atoi(value
);
5726 case SWM_S_CLOCK_FORMAT
:
5727 #ifndef SWM_DENY_CLOCK_FORMAT
5729 if ((clock_format
= strdup(value
)) == NULL
)
5730 err(1, "setconfvalue: clock_format");
5733 case SWM_S_CYCLE_EMPTY
:
5734 cycle_empty
= atoi(value
);
5736 case SWM_S_CYCLE_VISIBLE
:
5737 cycle_visible
= atoi(value
);
5739 case SWM_S_DIALOG_RATIO
:
5740 dialog_ratio
= atof(value
);
5741 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5744 case SWM_S_DISABLE_BORDER
:
5745 disable_border
= atoi(value
);
5747 case SWM_S_FOCUS_CLOSE
:
5748 if (!strcmp(value
, "first"))
5749 focus_close
= SWM_STACK_BOTTOM
;
5750 else if (!strcmp(value
, "last"))
5751 focus_close
= SWM_STACK_TOP
;
5752 else if (!strcmp(value
, "next"))
5753 focus_close
= SWM_STACK_ABOVE
;
5754 else if (!strcmp(value
, "previous"))
5755 focus_close
= SWM_STACK_BELOW
;
5757 errx(1, "focus_close");
5759 case SWM_S_FOCUS_CLOSE_WRAP
:
5760 focus_close_wrap
= atoi(value
);
5762 case SWM_S_FOCUS_DEFAULT
:
5763 if (!strcmp(value
, "last"))
5764 focus_default
= SWM_STACK_TOP
;
5765 else if (!strcmp(value
, "first"))
5766 focus_default
= SWM_STACK_BOTTOM
;
5768 errx(1, "focus_default");
5770 case SWM_S_FOCUS_MODE
:
5771 if (!strcmp(value
, "default"))
5772 focus_mode
= SWM_FOCUS_DEFAULT
;
5773 else if (!strcmp(value
, "follow_cursor"))
5774 focus_mode
= SWM_FOCUS_FOLLOW
;
5775 else if (!strcmp(value
, "synergy"))
5776 focus_mode
= SWM_FOCUS_SYNERGY
;
5778 errx(1, "focus_mode");
5780 case SWM_S_SPAWN_ORDER
:
5781 if (!strcmp(value
, "first"))
5782 spawn_position
= SWM_STACK_BOTTOM
;
5783 else if (!strcmp(value
, "last"))
5784 spawn_position
= SWM_STACK_TOP
;
5785 else if (!strcmp(value
, "next"))
5786 spawn_position
= SWM_STACK_ABOVE
;
5787 else if (!strcmp(value
, "previous"))
5788 spawn_position
= SWM_STACK_BELOW
;
5790 errx(1, "spawn_position");
5792 case SWM_S_SPAWN_TERM
:
5793 setconfspawn("term", value
, 0);
5794 setconfspawn("spawn_term", value
, 0);
5798 case SWM_S_SS_ENABLED
:
5799 ss_enabled
= atoi(value
);
5801 case SWM_S_STACK_ENABLED
:
5802 stack_enabled
= atoi(value
);
5804 case SWM_S_TERM_WIDTH
:
5805 term_width
= atoi(value
);
5809 case SWM_S_TITLE_CLASS_ENABLED
:
5810 title_class_enabled
= atoi(value
);
5812 case SWM_S_TITLE_NAME_ENABLED
:
5813 title_name_enabled
= atoi(value
);
5815 case SWM_S_URGENT_ENABLED
:
5816 urgent_enabled
= atoi(value
);
5818 case SWM_S_VERBOSE_LAYOUT
:
5819 verbose_layout
= atoi(value
);
5820 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5822 layouts
[i
].l_string
= fancy_stacker
;
5824 layouts
[i
].l_string
= plain_stacker
;
5827 case SWM_S_WINDOW_NAME_ENABLED
:
5828 window_name_enabled
= atoi(value
);
5830 case SWM_S_WORKSPACE_LIMIT
:
5831 workspace_limit
= atoi(value
);
5832 if (workspace_limit
> SWM_WS_MAX
)
5833 workspace_limit
= SWM_WS_MAX
;
5834 else if (workspace_limit
< 1)
5835 workspace_limit
= 1;
5844 setconfmodkey(char *selector
, char *value
, int flags
)
5846 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5847 update_modkey(Mod1Mask
);
5848 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5849 update_modkey(Mod2Mask
);
5850 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5851 update_modkey(Mod3Mask
);
5852 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5853 update_modkey(Mod4Mask
);
5860 setconfcolor(char *selector
, char *value
, int flags
)
5862 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5867 setconfregion(char *selector
, char *value
, int flags
)
5869 custom_region(value
);
5874 setautorun(char *selector
, char *value
, int flags
)
5884 if (getenv("SWM_STARTED"))
5888 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5889 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5891 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5892 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5895 * This is a little intricate
5897 * If the pid already exists we simply reuse it because it means it was
5898 * used before AND not claimed by manage_window. We get away with
5899 * altering it in the parent after INSERT because this can not be a race
5902 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5905 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5907 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5908 err(1, "setautorun: realloc");
5909 a
.argv
[argc
- 1] = ap
;
5912 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5913 err(1, "setautorun: realloc");
5914 a
.argv
[argc
] = NULL
;
5916 if ((pid
= fork()) == 0) {
5917 spawn(ws_id
, &a
, 1);
5926 p
= calloc(1, sizeof *p
);
5929 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
5939 setlayout(char *selector
, char *value
, int flags
)
5941 int ws_id
, i
, x
, mg
, ma
, si
, raise
, f
= 0;
5942 int st
= SWM_V_STACK
, num_screens
;
5944 struct workspace
*ws
;
5946 if (getenv("SWM_STARTED"))
5950 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
5951 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
5952 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5953 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5956 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5957 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
5959 if (!strcasecmp(s
, "vertical"))
5961 else if (!strcasecmp(s
, "vertical_flip")) {
5964 } else if (!strcasecmp(s
, "horizontal"))
5966 else if (!strcasecmp(s
, "horizontal_flip")) {
5969 } else if (!strcasecmp(s
, "fullscreen"))
5972 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5973 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5976 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
5977 for (i
= 0; i
< num_screens
; i
++) {
5978 ws
= (struct workspace
*)&screens
[i
].ws
;
5979 ws
[ws_id
].cur_layout
= &layouts
[st
];
5981 ws
[ws_id
].always_raise
= raise
;
5982 if (st
== SWM_MAX_STACK
)
5986 for (x
= 0; x
< abs(mg
); x
++) {
5987 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5988 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
5989 SWM_ARG_ID_MASTERSHRINK
);
5993 for (x
= 0; x
< abs(ma
); x
++) {
5994 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5995 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
5996 SWM_ARG_ID_MASTERDEL
);
6000 for (x
= 0; x
< abs(si
); x
++) {
6001 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6002 si
>= 0 ? SWM_ARG_ID_STACKINC
:
6003 SWM_ARG_ID_STACKDEC
);
6008 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6009 SWM_ARG_ID_FLIPLAYOUT
);
6017 /* config options */
6018 struct config_option
{
6020 int (*func
)(char*, char*, int);
6023 struct config_option configopt
[] = {
6024 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
6025 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
6026 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
6027 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
6028 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
6029 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
6030 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
6031 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
6032 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
6033 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
6034 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
6035 { "keyboard_mapping", setkeymapping
, 0 },
6036 { "bind", setconfbinding
, 0 },
6037 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
6038 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
6039 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
6040 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
6041 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
6042 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
6043 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
6044 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
6045 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
6046 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
6047 { "modkey", setconfmodkey
, 0 },
6048 { "program", setconfspawn
, 0 },
6049 { "quirk", setconfquirk
, 0 },
6050 { "region", setconfregion
, 0 },
6051 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
6052 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
6053 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
6054 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
6055 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
6056 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
6057 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6058 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6059 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6060 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6061 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6062 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6063 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6064 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6065 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6066 { "autorun", setautorun
, 0 },
6067 { "layout", setlayout
, 0 },
6072 conf_load(char *filename
, int keymapping
)
6075 char *line
, *cp
, *optsub
, *optval
;
6076 size_t linelen
, lineno
= 0;
6077 int wordlen
, i
, optind
;
6078 struct config_option
*opt
;
6080 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6082 if (filename
== NULL
) {
6083 warnx("conf_load: no filename");
6086 if ((config
= fopen(filename
, "r")) == NULL
) {
6087 warn("conf_load: fopen: %s", filename
);
6091 while (!feof(config
)) {
6092 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6095 err(1, "%s", filename
);
6100 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6101 if (cp
[0] == '\0') {
6106 /* get config option */
6107 wordlen
= strcspn(cp
, "=[ \t\n");
6109 warnx("%s: line %zd: no option found",
6114 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6115 opt
= &configopt
[i
];
6116 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6117 strlen(opt
->optname
) == wordlen
) {
6123 warnx("%s: line %zd: unknown option %.*s",
6124 filename
, lineno
, wordlen
, cp
);
6127 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
6128 warnx("%s: line %zd: invalid option %.*s",
6129 filename
, lineno
, wordlen
, cp
);
6133 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6134 /* get [selector] if any */
6138 wordlen
= strcspn(cp
, "]");
6141 warnx("%s: line %zd: syntax error",
6146 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6148 warnx("%s: line %zd: unable to allocate"
6149 "memory for selector", filename
,
6155 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6157 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6159 optval
= strdup(cp
);
6160 /* call function to deal with it all */
6161 if (configopt
[optind
].func(optsub
, optval
,
6162 configopt
[optind
].funcflags
) != 0)
6163 errx(1, "%s: line %zd: invalid data for %s",
6164 filename
, lineno
, configopt
[optind
].optname
);
6171 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6178 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6184 set_child_transient(struct ws_win
*win
, Window
*trans
)
6186 struct ws_win
*parent
, *w
;
6187 XWMHints
*wmh
= NULL
;
6188 struct swm_region
*r
;
6189 struct workspace
*ws
;
6191 parent
= find_window(win
->transient
);
6193 parent
->child_trans
= win
;
6195 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6196 " for 0x%lx trans 0x%lx\n", win
->id
, win
->transient
);
6198 if (win
->hints
== NULL
) {
6199 warnx("no hints for 0x%x", win
->id
);
6203 r
= root_to_region(win
->wa
.root
);
6205 /* parent doen't exist in our window list */
6206 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6210 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
6211 warnx("can't get hints for 0x%x", w
->id
);
6215 if (win
->hints
->window_group
!= wmh
->window_group
)
6218 w
->child_trans
= win
;
6219 win
->transient
= w
->id
;
6221 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
6222 "transient to 0x%x\n", win
->transient
);
6232 window_get_pid(xcb_window_t win
)
6237 xcb_get_property_cookie_t pc
;
6238 xcb_get_property_reply_t
*pr
;
6240 apid
= get_atom_from_string("_NET_WM_PID");
6241 if (apid
== XCB_ATOM_NONE
)
6244 pc
= xcb_get_property(conn
, False
, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
6245 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6248 if (pr
->type
!= XCB_ATOM_CARDINAL
)
6251 ret
= *(long *)xcb_get_property_value(pr
);
6257 apid
= get_atom_from_string("_SWM_PID");
6258 pc
= xcb_get_property(conn
, False
, win
, apid
, XCB_ATOM_STRING
,
6260 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6263 if (pr
->type
!= XCB_ATOM_STRING
)
6266 ret
= strtonum(xcb_get_property_value(pr
), 0, UINT_MAX
, &errstr
);
6273 manage_window(xcb_window_t id
)
6276 struct workspace
*ws
;
6277 struct ws_win
*win
, *ww
;
6278 int format
, i
, ws_idx
, n
, border_me
= 0;
6279 unsigned long nitems
, bytes
;
6280 Atom ws_idx_atom
= 0, type
;
6281 Atom
*prot
= NULL
, *pp
;
6282 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
6283 struct swm_region
*r
;
6288 if ((win
= find_window(id
)) != NULL
)
6289 return (win
); /* already being managed */
6291 /* see if we are on the unmanaged list */
6292 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6293 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
6294 "window: 0x%lx\n", win
->id
);
6295 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6297 set_child_transient(win
, &trans
);
6299 if (trans
&& (ww
= find_window(trans
)))
6300 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
6301 else if ((ww
= win
->ws
->focus
) &&
6302 spawn_position
== SWM_STACK_ABOVE
)
6303 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
,
6305 else if (ww
&& spawn_position
== SWM_STACK_BELOW
)
6306 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6307 else switch (spawn_position
) {
6310 case SWM_STACK_ABOVE
:
6311 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6313 case SWM_STACK_BOTTOM
:
6314 case SWM_STACK_BELOW
:
6315 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6318 ewmh_update_actions(win
);
6322 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6323 err(1, "manage_window: calloc: failed to allocate memory for "
6329 /* see if we need to override the workspace */
6330 p
= find_pid(window_get_pid(id
));
6332 /* Get all the window data in one shot */
6333 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
6335 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
6336 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
6338 XGetWindowAttributes(display
, id
, &win
->wa
);
6339 XGetWMNormalHints(display
, id
, &win
->sh
, &win
->sh_mask
);
6340 win
->hints
= XGetWMHints(display
, id
);
6341 XGetTransientForHint(display
, id
, &trans
);
6343 win
->transient
= trans
;
6344 set_child_transient(win
, &trans
);
6345 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, "
6346 "transient: 0x%lx\n", win
->id
, win
->transient
);
6349 /* get supported protocols */
6350 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
6351 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
6352 if (*pp
== takefocus
)
6353 win
->take_focus
= 1;
6355 win
->can_delete
= 1;
6361 win
->iconic
= get_iconic(win
);
6364 * Figure out where to put the window. If it was previously assigned to
6365 * a workspace (either by spawn() or manually moving), and isn't
6366 * transient, * put it in the same workspace
6368 r
= root_to_region(win
->wa
.root
);
6370 ws
= &r
->s
->ws
[p
->ws
];
6371 TAILQ_REMOVE(&pidlist
, p
, entry
);
6374 } else if (prop
&& win
->transient
== 0) {
6375 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
6376 ws_idx
= strtonum((const char *)prop
, 0, workspace_limit
- 1,
6379 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
6382 ws
= &r
->s
->ws
[ws_idx
];
6385 /* this should launch transients in the same ws as parent */
6387 if ((ww
= find_window(trans
)) != NULL
)
6393 warnx("manage_window: fix this "
6399 /* set up the window layout */
6402 win
->s
= r
->s
; /* this never changes */
6403 if (trans
&& (ww
= find_window(trans
)))
6404 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
6405 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
6406 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
6408 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
6409 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6410 else switch (spawn_position
) {
6413 case SWM_STACK_ABOVE
:
6414 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6416 case SWM_STACK_BOTTOM
:
6417 case SWM_STACK_BELOW
:
6418 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6421 /* ignore window border if there is one. */
6422 WIDTH(win
) = win
->wa
.width
;
6423 HEIGHT(win
) = win
->wa
.height
;
6424 X(win
) = win
->wa
.x
+ win
->wa
.border_width
;
6425 Y(win
) = win
->wa
.y
+ win
->wa
.border_width
;
6427 win
->g_floatvalid
= 0;
6428 win
->floatmaxed
= 0;
6429 win
->ewmh_flags
= 0;
6431 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, (x,y) w x h: "
6432 "(%d,%d) %d x %d, ws: %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
6433 HEIGHT(win
), ws
->idx
);
6435 constrain_window(win
, r
, 0);
6437 /* Set window properties so we can remember this after reincarnation */
6438 if (ws_idx_atom
&& prop
== NULL
&&
6439 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
6441 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6443 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
6444 PropModeReplace
, ws_idx_str
, strlen((char *)ws_idx_str
));
6449 ewmh_autoquirk(win
);
6451 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
6452 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6453 win
->ch
.res_class
, win
->ch
.res_name
);
6455 /* java is retarded so treat it special */
6456 if (strstr(win
->ch
.res_name
, "sun-awt")) {
6461 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6462 if (!strcmp(win
->ch
.res_class
, qp
->class) &&
6463 !strcmp(win
->ch
.res_name
, qp
->name
)) {
6464 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
6465 "class: %s, name: %s\n", win
->ch
.res_class
,
6467 if (qp
->quirk
& SWM_Q_FLOAT
) {
6471 win
->quirks
= qp
->quirk
;
6476 /* alter window position if quirky */
6477 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6478 win
->manual
= 1; /* don't center the quirky windows */
6479 if (bar_enabled
&& Y(win
) < bar_height
)
6480 Y(win
) = bar_height
;
6481 if (WIDTH(win
) + X(win
) > WIDTH(r
))
6482 X(win
) = WIDTH(r
) - WIDTH(win
) - 2;
6486 /* Reset font sizes (the bruteforce way; no default keybinding). */
6487 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6488 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6489 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
6490 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6491 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
6494 ewmh_get_win_state(win
);
6495 ewmh_update_actions(win
);
6496 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
6501 X(win
) -= border_width
;
6502 Y(win
) -= border_width
;
6506 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
6507 PropertyChangeMask
| StructureNotifyMask
);
6509 /* floaters need to be mapped if they are in the current workspace */
6510 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
6511 map_window_raised(win
->id
);
6517 free_window(struct ws_win
*win
)
6519 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%lx\n", win
->id
);
6524 /* needed for restart wm */
6525 set_win_state(win
, XCB_WM_STATE_WITHDRAWN
);
6527 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6529 if (win
->ch
.res_class
)
6530 XFree(win
->ch
.res_class
);
6531 if (win
->ch
.res_name
)
6532 XFree(win
->ch
.res_name
);
6537 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6543 unmanage_window(struct ws_win
*win
)
6545 struct ws_win
*parent
;
6550 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%lx\n", win
->id
);
6552 if (win
->transient
) {
6553 parent
= find_window(win
->transient
);
6555 parent
->child_trans
= NULL
;
6558 /* focus on root just in case */
6559 XSetInputFocus(display
, PointerRoot
, PointerRoot
, CurrentTime
);
6568 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
6569 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6573 focus_magic(struct ws_win
*win
)
6575 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%lx\n", WINID(win
));
6578 /* if there are no windows clear the status-bar */
6583 if (win
->child_trans
) {
6584 /* win = parent & has a transient so focus on that */
6586 focus_win(win
->child_trans
);
6587 if (win
->child_trans
->take_focus
)
6588 client_msg(win
, takefocus
);
6590 /* make sure transient hasn't disappeared */
6591 if (validate_win(win
->child_trans
) == 0) {
6592 focus_win(win
->child_trans
);
6593 if (win
->child_trans
->take_focus
)
6594 client_msg(win
->child_trans
, takefocus
);
6596 win
->child_trans
= NULL
;
6598 if (win
->take_focus
)
6599 client_msg(win
, takefocus
);
6605 if (win
->take_focus
)
6606 client_msg(win
, takefocus
);
6613 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%lx\n", e
->xexpose
.window
);
6620 XKeyEvent
*ev
= &e
->xkey
;
6622 struct swm_region
*r
;
6624 keysym
= XkbKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0, 0);
6625 if ((kp
= key_lookup(CLEANMASK(ev
->state
), keysym
)) == NULL
)
6627 if (keyfuncs
[kp
->funcid
].func
== NULL
)
6630 r
= root_to_region(ev
->root
);
6631 if (kp
->funcid
== kf_spawn_custom
)
6632 spawn_custom(r
, &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
6634 keyfuncs
[kp
->funcid
].func(r
, &(keyfuncs
[kp
->funcid
].args
));
6638 buttonpress(XEvent
*e
)
6642 XButtonPressedEvent
*ev
= &e
->xbutton
;
6644 if ((win
= find_window(ev
->window
)) == NULL
)
6648 action
= client_click
;
6650 for (i
= 0; i
< LENGTH(buttons
); i
++)
6651 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6652 buttons
[i
].button
== ev
->button
&&
6653 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
6654 buttons
[i
].func(win
, &buttons
[i
].args
);
6658 configurerequest(XEvent
*e
)
6660 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
6665 if ((win
= find_window(ev
->window
)) == NULL
)
6666 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6670 bzero(&wc
, sizeof wc
);
6673 wc
.width
= ev
->width
;
6674 wc
.height
= ev
->height
;
6675 wc
.border_width
= ev
->border_width
;
6676 wc
.sibling
= ev
->above
;
6677 wc
.stack_mode
= ev
->detail
;
6679 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: 0x%lx, "
6680 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", ev
->window
,
6681 YESNO(new), wc
.x
, wc
.y
, wc
.width
, wc
.height
);
6683 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
6684 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
6685 !(win
->sh_mask
& EWMH_F_FULLSCREEN
)) {
6686 win
->g_float
.x
= ev
->x
- X(win
->ws
->r
);
6687 win
->g_float
.y
= ev
->y
- Y(win
->ws
->r
);
6688 win
->g_float
.w
= ev
->width
;
6689 win
->g_float
.h
= ev
->height
;
6690 win
->g_floatvalid
= 1;
6692 if (win
->floating
) {
6693 win
->g
= win
->g_float
;
6694 win
->g
.x
+= X(win
->ws
->r
);
6695 win
->g
.y
+= Y(win
->ws
->r
);
6698 config_win(win
, ev
);
6701 config_win(win
, ev
);
6706 configurenotify(XEvent
*e
)
6710 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%lx\n",
6711 e
->xconfigure
.window
);
6713 win
= find_window(e
->xconfigure
.window
);
6715 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &win
->sh_mask
);
6719 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6720 drain_enter_notify();
6725 destroynotify(XEvent
*e
)
6728 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
6730 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%lx\n", ev
->window
);
6732 if ((win
= find_window(ev
->window
)) == NULL
) {
6733 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6739 /* make sure we focus on something */
6742 unmanage_window(win
);
6744 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6745 drain_enter_notify();
6750 enternotify(XEvent
*e
)
6752 XCrossingEvent
*ev
= &e
->xcrossing
;
6757 Window focus_return
;
6758 int revert_to_return
;
6760 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%lx, mode: %d, detail: "
6761 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6762 "state: %d\n", ev
->window
, ev
->mode
, ev
->detail
, ev
->root
,
6763 ev
->subwindow
, YESNO(ev
->same_screen
), YESNO(ev
->focus
), ev
->state
);
6765 if (ev
->mode
!= NotifyNormal
) {
6766 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6771 switch (focus_mode
) {
6772 case SWM_FOCUS_DEFAULT
:
6774 case SWM_FOCUS_FOLLOW
:
6776 case SWM_FOCUS_SYNERGY
:
6779 * all these checks need to be in this order because the
6780 * XCheckTypedWindowEvent relies on weeding out the previous events
6782 * making this code an option would enable a follow mouse for focus
6787 * state is set when we are switching workspaces and focus is set when
6788 * the window or a subwindow already has focus (occurs during restart).
6790 * Only honor the focus flag if last_focus_event is not FocusOut,
6791 * this allows spectrwm to continue to control focus when another
6792 * program is also playing with it.
6794 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6795 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6800 * happens when a window is created or destroyed and the border
6801 * crosses the mouse pointer and when switching ws
6803 * we need the subwindow test to see if we came from root in order
6804 * to give focus to floaters
6806 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6807 ev
->subwindow
== 0) {
6808 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6812 /* this window already has focus */
6813 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6814 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6818 /* this window is being deleted or moved to another ws */
6819 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6821 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6822 XPutBackEvent(display
, &cne
);
6826 if ((win
= find_window(ev
->window
)) == NULL
) {
6827 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6832 * In fullstack kill all enters unless they come from a different ws
6833 * (i.e. another region) or focus has been grabbed externally.
6835 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6836 last_focus_event
!= FocusOut
) {
6837 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6838 if ((w
= find_window(focus_return
)) == NULL
||
6840 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6848 if ((win
= find_window(ev
->window
)) == NULL
) {
6849 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6854 * if we have more enternotifies let them handle it in due time
6856 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
6857 DNPRINTF(SWM_D_EVENT
,
6858 "ignoring enternotify: got more enternotify\n");
6859 XPutBackEvent(display
, &cne
);
6866 /* lets us use one switch statement for arbitrary mode/detail combinations */
6867 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6870 focusevent(XEvent
*e
)
6874 u_int32_t mode_detail
;
6875 XFocusChangeEvent
*ev
= &e
->xfocus
;
6877 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: 0x%lx mode: %d "
6878 "detail: %d\n", ev
->type
== FocusIn
? "entering" : "leaving",
6879 ev
->window
, ev
->mode
, ev
->detail
);
6881 if (last_focus_event
== ev
->type
) {
6882 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
6886 last_focus_event
= ev
->type
;
6887 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
6889 switch (mode_detail
) {
6890 /* synergy client focus operations */
6891 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
6892 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
6894 /* synergy server focus operations */
6895 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
6897 /* Entering applications like rdesktop that mangle the pointer */
6898 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
6900 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
6901 XSetWindowBorder(display
, win
->id
,
6902 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
6903 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
6906 warnx("ignoring focusevent");
6907 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
6914 mapnotify(XEvent
*e
)
6917 XMapEvent
*ev
= &e
->xmap
;
6919 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%lx\n", ev
->window
);
6921 win
= manage_window(ev
->window
);
6923 set_win_state(win
, XCB_WM_STATE_NORMAL
);
6926 * focus_win can only set input focus on a mapped window.
6927 * make sure the window really has focus since it is just being mapped.
6929 if (win
->ws
->focus
== win
)
6934 mappingnotify(XEvent
*e
)
6936 XMappingEvent
*ev
= &e
->xmapping
;
6938 XRefreshKeyboardMapping(ev
);
6939 if (ev
->request
== MappingKeyboard
)
6944 maprequest(XEvent
*e
)
6947 struct swm_region
*r
;
6948 XWindowAttributes wa
;
6949 XMapRequestEvent
*ev
= &e
->xmaprequest
;
6951 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%lx\n",
6952 e
->xmaprequest
.window
);
6954 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
6956 if (wa
.override_redirect
)
6959 win
= manage_window(e
->xmaprequest
.window
);
6961 return; /* can't happen */
6965 /* make new win focused */
6966 r
= root_to_region(win
->wa
.root
);
6967 if (win
->ws
== r
->ws
)
6972 propertynotify(XEvent
*e
)
6975 XPropertyEvent
*ev
= &e
->xproperty
;
6978 name
= XGetAtomName(display
, ev
->atom
);
6979 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%lx, atom: %s\n",
6984 win
= find_window(ev
->window
);
6988 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
6989 update_iconic(win
, 0);
6990 map_window_raised(win
->id
);
6998 case XA_WM_NORMAL_HINTS
:
7000 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
7001 warnx("normal hints: flag 0x%x", win
->sh
.flags
);
7002 if (win
->sh
.flags
& PMinSize
) {
7003 WIDTH(win
) = win
->sh
.min_width
;
7004 HEIGHT(win
) = win
->sh
.min_height
;
7005 warnx("min %d %d", WIDTH(win
), HEIGHT(win
));
7007 XMoveResizeWindow(display
, win
->id
,
7008 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
));
7020 unmapnotify(XEvent
*e
)
7024 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%lx\n", e
->xunmap
.window
);
7026 /* determine if we need to help unmanage this window */
7027 win
= find_window(e
->xunmap
.window
);
7031 if (getstate(e
->xunmap
.window
) == XCB_WM_STATE_NORMAL
) {
7032 unmanage_window(win
);
7035 /* giant hack for apps that don't destroy transient windows */
7036 /* eat a bunch of events to prevent remanaging the window */
7038 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
7039 EnterWindowMask
, &cne
))
7041 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
7042 StructureNotifyMask
, &cne
))
7044 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
7045 SubstructureNotifyMask
, &cne
))
7047 /* resend unmap because we ated it */
7048 XUnmapWindow(display
, e
->xunmap
.window
);
7051 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7052 drain_enter_notify();
7056 visibilitynotify(XEvent
*e
)
7059 struct swm_region
*r
;
7061 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%lx\n",
7062 e
->xvisibility
.window
);
7064 if (e
->xvisibility
.state
== VisibilityUnobscured
) {
7065 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7066 for (i
= 0; i
< num_screens
; i
++)
7067 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7068 if (e
->xvisibility
.window
== WINID(r
->bar
))
7074 clientmessage(XEvent
*e
)
7076 XClientMessageEvent
*ev
;
7081 win
= find_window(ev
->window
);
7083 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7084 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
7085 "unmanaged window.\n");
7086 e
->xmaprequest
.window
= ev
->window
;
7092 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx, type: %ld\n",
7093 ev
->window
, ev
->message_type
);
7095 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7096 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7099 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7100 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7101 if (win
->can_delete
)
7102 client_msg(win
, adelete
);
7104 xcb_kill_client(conn
, win
->id
);
7106 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7107 DNPRINTF(SWM_D_EVENT
,
7108 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7109 if (win
->floating
) {
7110 if (ev
->data
.l
[0] & (1<<8)) /* x */
7111 X(win
) = ev
->data
.l
[1];
7112 if (ev
->data
.l
[0] & (1<<9)) /* y */
7113 Y(win
) = ev
->data
.l
[2];
7114 if (ev
->data
.l
[0] & (1<<10)) /* width */
7115 WIDTH(win
) = ev
->data
.l
[3];
7116 if (ev
->data
.l
[0] & (1<<11)) /* height */
7117 HEIGHT(win
) = ev
->data
.l
[4];
7122 /* TODO: Change stack sizes */
7123 /* notify no change was made. */
7124 config_win(win
, NULL
);
7127 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
7128 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
7129 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
7131 ewmh_update_win_state(win
, ev
->data
.l
[2],
7139 xerror_start(Display
*d
, XErrorEvent
*ee
)
7146 xerror(Display
*d
, XErrorEvent
*ee
)
7148 /* warnx("error: %p %p", display, ee); */
7156 xerrorxlib
= XSetErrorHandler(xerror_start
);
7158 /* this causes an error if some other window manager is running */
7159 XSelectInput(display
, DefaultRootWindow(display
),
7160 SubstructureRedirectMask
);
7161 XSync(display
, False
);
7165 XSetErrorHandler(xerror
);
7166 XSync(display
, False
);
7171 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
7173 struct swm_region
*r
, *n
;
7174 struct workspace
*ws
= NULL
;
7177 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
7178 s
->idx
, w
, h
, x
, y
);
7180 /* remove any conflicting regions */
7181 n
= TAILQ_FIRST(&s
->rl
);
7184 n
= TAILQ_NEXT(r
, entry
);
7185 if (X(r
) < (x
+ w
) &&
7186 (X(r
) + WIDTH(r
)) > x
&&
7188 (Y(r
) + HEIGHT(r
)) > y
) {
7189 if (r
->ws
->r
!= NULL
)
7190 r
->ws
->old_r
= r
->ws
->r
;
7193 TAILQ_REMOVE(&s
->rl
, r
, entry
);
7194 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
7198 /* search old regions for one to reuse */
7200 /* size + location match */
7201 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7202 if (X(r
) == x
&& Y(r
) == y
&&
7203 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7207 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7208 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7212 TAILQ_REMOVE(&s
->orl
, r
, entry
);
7213 /* try to use old region's workspace */
7214 if (r
->ws
->r
== NULL
)
7217 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
7218 err(1, "new_region: calloc: failed to allocate memory "
7221 /* if we don't have a workspace already, find one */
7223 for (i
= 0; i
< workspace_limit
; i
++)
7224 if (s
->ws
[i
].r
== NULL
) {
7231 errx(1, "new_region: no free workspaces");
7242 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
7248 #ifdef SWM_XRR_HAS_CRTC
7251 #endif /* SWM_XRR_HAS_CRTC */
7252 struct swm_region
*r
;
7254 xcb_randr_get_screen_resources_cookie_t src
;
7255 xcb_randr_get_screen_resources_reply_t
*srr
;
7256 xcb_randr_get_crtc_info_cookie_t cic
;
7257 xcb_randr_get_crtc_info_reply_t
*cir
;
7258 xcb_randr_crtc_t
*crtc
;
7260 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7261 if (i
>= num_screens
)
7262 errx(1, "scan_xrandr: invalid screen");
7264 /* remove any old regions */
7265 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
7266 r
->ws
->old_r
= r
->ws
->r
= NULL
;
7268 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
7269 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
7273 /* map virtual screens onto physical screens */
7274 #ifdef SWM_XRR_HAS_CRTC
7275 if (xrandr_support
) {
7276 src
= xcb_randr_get_screen_resources(conn
, screens
[i
].root
);
7277 srr
= xcb_randr_get_screen_resources_reply(conn
, src
, NULL
);
7279 new_region(&screens
[i
], 0, 0,
7280 DisplayWidth(display
, i
),
7281 DisplayHeight(display
, i
));
7283 ncrtc
= srr
->num_crtcs
;
7284 for (c
= 0; c
< ncrtc
; c
++) {
7285 crtc
= xcb_randr_get_screen_resources_crtcs(srr
);
7286 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
7288 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
7289 if (cir
&& cir
->num_outputs
== 0)
7292 if (cir
== NULL
|| cir
->mode
== 0)
7293 new_region(&screens
[i
], 0, 0,
7294 DisplayWidth(display
, i
),
7295 DisplayHeight(display
, i
));
7297 new_region(&screens
[i
],
7298 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
7305 #endif /* SWM_XRR_HAS_CRTC */
7307 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
7308 DisplayHeight(display
, i
));
7313 screenchange(XEvent
*e
)
7315 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
7316 struct swm_region
*r
;
7319 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%lx\n", xe
->root
);
7321 if (!XRRUpdateConfiguration(e
))
7324 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7325 /* silly event doesn't include the screen index */
7326 for (i
= 0; i
< num_screens
; i
++)
7327 if (screens
[i
].root
== xe
->root
)
7329 if (i
>= num_screens
)
7330 errx(1, "screenchange: screen not found");
7332 /* brute force for now, just re-enumerate the regions */
7335 /* add bars to all regions */
7336 for (i
= 0; i
< num_screens
; i
++)
7337 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7340 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7341 drain_enter_notify();
7347 xcb_window_t
*wins
= NULL
;
7349 int i
, j
, num_screens
;
7350 uint16_t state
, manage
;
7352 xcb_query_tree_cookie_t qtc
;
7353 xcb_query_tree_reply_t
*qtr
;
7354 xcb_get_window_attributes_cookie_t c
;
7355 xcb_get_window_attributes_reply_t
*r
;
7356 xcb_get_property_cookie_t pc
;
7358 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7359 for (i
= 0; i
< num_screens
; i
++) {
7360 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
7361 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
7364 wins
= xcb_query_tree_children(qtr
);
7365 no
= xcb_query_tree_children_length(qtr
);
7366 /* attach windows to a region */
7367 /* normal windows */
7368 for (j
= 0; j
< no
; j
++) {
7369 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7370 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7373 if (r
->override_redirect
) {
7378 pc
= xcb_get_wm_transient_for(conn
, wins
[j
]);
7379 if (xcb_get_wm_transient_for_reply(conn
, pc
, &wins
[j
],
7385 state
= getstate(wins
[j
]);
7386 manage
= state
== XCB_WM_STATE_ICONIC
;
7387 if (r
->map_state
== XCB_MAP_STATE_VIEWABLE
|| manage
)
7388 manage_window(wins
[j
]);
7391 /* transient windows */
7392 for (j
= 0; j
< no
; j
++) {
7393 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7394 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7397 if (r
->override_redirect
) {
7403 state
= getstate(wins
[j
]);
7404 manage
= state
== XCB_WM_STATE_ICONIC
;
7405 pc
= xcb_get_wm_transient_for(conn
, wins
[j
]);
7406 if (xcb_get_wm_transient_for_reply(conn
, pc
, &wins
[j
],
7408 manage_window(wins
[j
]);
7417 int i
, j
, k
, num_screens
;
7419 struct workspace
*ws
;
7422 xcb_randr_query_version_cookie_t c
;
7423 xcb_randr_query_version_reply_t
*r
;
7425 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7426 if ((screens
= calloc(num_screens
,
7427 sizeof(struct swm_screen
))) == NULL
)
7428 err(1, "setup_screens: calloc: failed to allocate memory for "
7431 /* initial Xrandr setup */
7432 xrandr_support
= XRRQueryExtension(display
,
7433 &xrandr_eventbase
, &errorbase
);
7434 if (xrandr_support
) {
7435 c
= xcb_randr_query_version(conn
, True
, False
);
7436 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
7438 if (r
->major_version
< 1)
7445 /* map physical screens */
7446 for (i
= 0; i
< num_screens
; i
++) {
7447 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
7449 TAILQ_INIT(&screens
[i
].rl
);
7450 TAILQ_INIT(&screens
[i
].orl
);
7451 screens
[i
].root
= RootWindow(display
, i
);
7453 /* set default colors */
7454 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
7455 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
7456 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
7457 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
7458 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
7460 /* create graphics context on screen */
7461 gcv
.graphics_exposures
= 0;
7462 screens
[i
].bar_gc
= XCreateGC(display
, screens
[i
].root
,
7463 GCGraphicsExposures
, &gcv
);
7465 /* set default cursor */
7466 XDefineCursor(display
, screens
[i
].root
,
7467 XCreateFontCursor(display
, XC_left_ptr
));
7469 /* init all workspaces */
7470 /* XXX these should be dynamically allocated too */
7471 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
7472 ws
= &screens
[i
].ws
[j
];
7478 TAILQ_INIT(&ws
->winlist
);
7479 TAILQ_INIT(&ws
->unmanagedlist
);
7481 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
7482 if (layouts
[k
].l_config
!= NULL
)
7483 layouts
[k
].l_config(ws
,
7484 SWM_ARG_ID_STACKINIT
);
7485 ws
->cur_layout
= &layouts
[0];
7486 ws
->cur_layout
->l_string(ws
);
7492 xcb_randr_select_input(conn
, screens
[i
].root
,
7493 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
7500 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
7501 err(1, "setup_globals: strdup: failed to allocate memory.");
7503 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
7504 err(1, "setup_globals: strdup: failed to allocate memory.");
7511 xcb_atom_t netwmcheck
, netwmname
, utf8_string
;
7512 xcb_window_t root
, win
;
7514 /* work around sun jdk bugs, code from wmname */
7515 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7516 netwmname
= get_atom_from_string("_NET_WM_NAME");
7517 utf8_string
= get_atom_from_string("UTF8_STRING");
7519 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7520 for (i
= 0; i
< num_screens
; i
++) {
7521 root
= screens
[i
].root
;
7522 win
= XCreateSimpleWindow(display
,root
, 0, 0, 1, 1, 0,
7523 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
,
7524 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
);
7526 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
7527 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7528 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7529 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7530 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7531 netwmcheck
, utf8_string
, 8, strlen("LG3D"), "LG3D");
7536 main(int argc
, char *argv
[])
7538 struct swm_region
*r
, *rr
;
7539 struct ws_win
*winfocus
= NULL
;
7542 char conf
[PATH_MAX
], *cfile
= NULL
;
7545 int xfd
, i
, num_screens
;
7547 struct sigaction sact
;
7548 xcb_generic_event_t
*evt
;
7551 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
7552 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
7554 warnx("no locale support");
7556 if (!X_HAVE_UTF8_STRING
)
7557 warnx("no UTF-8 support");
7559 if (!(display
= XOpenDisplay(0)))
7560 errx(1, "can not open display");
7562 if (!(conn
= XGetXCBConnection(display
)))
7563 errx(1, "can not get XCB connection");
7566 errx(1, "other wm running");
7568 /* handle some signals */
7569 bzero(&sact
, sizeof(sact
));
7570 sigemptyset(&sact
.sa_mask
);
7572 sact
.sa_handler
= sighdlr
;
7573 sigaction(SIGINT
, &sact
, NULL
);
7574 sigaction(SIGQUIT
, &sact
, NULL
);
7575 sigaction(SIGTERM
, &sact
, NULL
);
7576 sigaction(SIGHUP
, &sact
, NULL
);
7578 sact
.sa_handler
= sighdlr
;
7579 sact
.sa_flags
= SA_NOCLDSTOP
;
7580 sigaction(SIGCHLD
, &sact
, NULL
);
7582 astate
= get_atom_from_string("WM_STATE");
7583 aprot
= get_atom_from_string("WM_PROTOCOLS");
7584 adelete
= get_atom_from_string("WM_DELETE_WINDOW");
7585 takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
7586 a_wmname
= get_atom_from_string("WM_NAME");
7587 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
7588 a_utf8_string
= get_atom_from_string("UTF8_STRING");
7589 a_string
= get_atom_from_string("STRING");
7590 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
7592 /* look for local and global conf file */
7593 pwd
= getpwuid(getuid());
7595 errx(1, "invalid user: %d", getuid());
7604 for (i
= 0; ; i
++) {
7609 snprintf(conf
, sizeof conf
, "%s/.%s",
7610 pwd
->pw_dir
, SWM_CONF_FILE
);
7614 snprintf(conf
, sizeof conf
, "/etc/%s",
7619 snprintf(conf
, sizeof conf
, "%s/.%s",
7620 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
7624 snprintf(conf
, sizeof conf
, "/etc/%s",
7631 if (strlen(conf
) && stat(conf
, &sb
) != -1)
7632 if (S_ISREG(sb
.st_mode
)) {
7639 /* load conf (if any) */
7641 conf_load(cfile
, SWM_CONF_DEFAULT
);
7644 /* set some values to work around bad programs */
7646 /* grab existing windows (before we build the bars) */
7649 if (getenv("SWM_STARTED") == NULL
)
7650 setenv("SWM_STARTED", "YES", 1);
7652 /* setup all bars */
7653 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7654 for (i
= 0; i
< num_screens
; i
++)
7655 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
7656 if (winfocus
== NULL
)
7657 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
7665 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7666 drain_enter_notify();
7668 xfd
= xcb_get_file_descriptor(conn
);
7670 while ((evt
= xcb_poll_for_event(conn
)) == 0) {
7671 XNextEvent(display
, &e
);
7674 if (e
.type
< LASTEvent
) {
7675 DNPRINTF(SWM_D_EVENTQ
,"XEvent: handled: %s, "
7676 "window: 0x%lx, type: %s (%d), %d remaining"
7677 "\n", YESNO(handler
[e
.type
]),
7678 e
.xany
.window
, geteventname(&e
),
7679 e
.type
, QLength(display
));
7681 if (handler
[e
.type
])
7682 handler
[e
.type
](&e
);
7684 DNPRINTF(SWM_D_EVENTQ
, "XRandr Event: window: "
7685 "0x%lx, type: %s (%d)\n", e
.xany
.window
,
7686 xrandr_geteventname(&e
), e
.type
);
7688 switch (e
.type
- xrandr_eventbase
) {
7689 case RRScreenChangeNotify
:
7698 /* if we are being restarted go focus on first window */
7700 rr
= winfocus
->ws
->r
;
7702 /* not a visible window */
7706 /* move pointer to first screen if multi screen */
7707 if (num_screens
> 1 || outputs
> 1)
7708 XWarpPointer(display
, None
, rr
->s
[0].root
,
7710 Y(rr
) + (bar_enabled
? bar_height
: 0));
7712 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7722 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7724 DNPRINTF(SWM_D_MISC
, "select failed");
7725 if (restart_wm
== 1)
7726 restart(NULL
, NULL
);
7727 if (search_resp
== 1)
7740 for (i
= 0; i
< num_screens
; ++i
)
7741 if (screens
[i
].bar_gc
!= NULL
)
7742 XFreeGC(display
, screens
[i
].bar_gc
);
7743 XFreeFontSet(display
, bar_fs
);
7744 xcb_disconnect(conn
);
7745 XCloseDisplay(display
);