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
, Atom a
)
2028 XClientMessageEvent cm
;
2033 bzero(&cm
, sizeof cm
);
2034 cm
.type
= ClientMessage
;
2035 cm
.window
= win
->id
;
2036 cm
.message_type
= aprot
;
2039 cm
.data
.l
[1] = CurrentTime
;
2040 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
2043 /* synthetic response to a ConfigureRequest when not making a change */
2045 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
2052 /* send notification of unchanged state. */
2053 ce
.type
= ConfigureNotify
;
2056 ce
.width
= WIDTH(win
);
2057 ce
.height
= HEIGHT(win
);
2058 ce
.override_redirect
= False
;
2062 ce
.display
= display
;
2064 ce
.window
= win
->id
;
2065 ce
.border_width
= BORDER(win
);
2069 ce
.display
= ev
->display
;
2070 ce
.event
= ev
->window
;
2071 ce
.window
= ev
->window
;
2073 /* make response appear more WM_SIZE_HINTS-compliant */
2075 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%lx,"
2076 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
2077 "%d x %d\n", win
->id
, win
->sh_mask
, SH_MIN_W(win
),
2078 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2079 SH_INC_W(win
), SH_INC_H(win
));
2083 /* the hint may be set... to 0! */
2084 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2085 ce
.width
= SH_MIN_W(win
);
2086 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2087 ce
.height
= SH_MIN_H(win
);
2092 /* may also be advertized as 0 */
2093 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2094 ce
.width
= SH_MAX_W(win
);
2095 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2096 ce
.height
= SH_MAX_H(win
);
2099 /* resize increment. */
2101 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2102 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2104 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2105 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2109 /* adjust x and y for requested border_width. */
2110 ce
.x
+= BORDER(win
) - ev
->border_width
;
2111 ce
.y
+= BORDER(win
) - ev
->border_width
;
2112 ce
.border_width
= ev
->border_width
;
2113 ce
.above
= ev
->above
;
2116 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
2117 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2118 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2120 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
2124 count_win(struct workspace
*ws
, int count_transient
)
2129 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2130 if (count_transient
== 0 && win
->floating
)
2132 if (count_transient
== 0 && win
->transient
)
2138 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2144 quit(struct swm_region
*r
, union arg
*args
)
2146 DNPRINTF(SWM_D_MISC
, "quit\n");
2151 unmap_window(struct ws_win
*win
)
2156 /* don't unmap again */
2157 if (getstate(win
->id
) == XCB_WM_STATE_ICONIC
)
2160 set_win_state(win
, XCB_WM_STATE_ICONIC
);
2162 xcb_unmap_window(conn
, win
->id
);
2163 XSetWindowBorder(display
, win
->id
,
2164 win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2171 int i
, j
, num_screens
;
2173 for (i
= 0; i
< num_screens
; i
++)
2174 for (j
= 0; j
< workspace_limit
; j
++)
2175 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2180 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
2187 event
.display
= display
; /* Ignored, but what the hell */
2188 event
.window
= win
->id
;
2189 event
.root
= win
->s
->root
;
2190 event
.subwindow
= None
;
2191 event
.time
= CurrentTime
;
2196 event
.same_screen
= True
;
2197 event
.keycode
= XKeysymToKeycode(display
, keysym
);
2198 event
.state
= modifiers
;
2200 event
.type
= KeyPress
;
2201 XSendEvent(event
.display
, event
.window
, True
,
2202 KeyPressMask
, (XEvent
*)&event
);
2204 event
.type
= KeyRelease
;
2205 XSendEvent(event
.display
, event
.window
, True
,
2206 KeyPressMask
, (XEvent
*)&event
);
2211 restart(struct swm_region
*r
, union arg
*args
)
2213 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2215 /* disable alarm because the following code may not be interrupted */
2217 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
2218 err(1, "can't disable alarm");
2223 XCloseDisplay(display
);
2224 execvp(start_argv
[0], start_argv
);
2225 warn("execvp failed");
2230 root_to_region(Window root
)
2232 struct swm_region
*r
= NULL
;
2234 int i
, x
, y
, wx
, wy
, num_screens
;
2237 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%lx\n", root
);
2239 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2240 for (i
= 0; i
< num_screens
; i
++)
2241 if (screens
[i
].root
== root
)
2244 if (XQueryPointer(display
, screens
[i
].root
,
2245 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
2246 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: (%d,%d)\n",
2248 /* choose a region based on pointer location */
2249 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2250 if (X(r
) <= x
&& x
< MAX_X(r
) &&
2251 Y(r
) <= y
&& y
< MAX_Y(r
))
2256 r
= TAILQ_FIRST(&screens
[i
].rl
);
2262 find_unmanaged_window(xcb_window_t id
)
2265 int i
, j
, num_screens
;
2267 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2268 for (i
= 0; i
< num_screens
; i
++)
2269 for (j
= 0; j
< workspace_limit
; j
++)
2270 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2278 find_window(xcb_window_t id
)
2281 int i
, j
, num_screens
;
2282 xcb_query_tree_cookie_t c
;
2283 xcb_query_tree_reply_t
*r
;
2285 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2286 for (i
= 0; i
< num_screens
; i
++)
2287 for (j
= 0; j
< workspace_limit
; j
++)
2288 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2292 c
= xcb_query_tree(conn
, id
);
2293 r
= xcb_query_tree_reply(conn
, c
, NULL
);
2297 /* if we were looking for the parent return that window instead */
2298 if (r
->parent
== 0 || r
->root
== r
->parent
)
2301 /* look for parent */
2302 for (i
= 0; i
< num_screens
; i
++)
2303 for (j
= 0; j
< workspace_limit
; j
++)
2304 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2305 if (r
->parent
== win
->id
) {
2315 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2320 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2323 close(xcb_get_file_descriptor(conn
));
2325 setenv("LD_PRELOAD", SWM_LIB
, 1);
2327 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2328 warn("spawn: asprintf SWM_WS");
2331 setenv("_SWM_WS", ret
, 1);
2335 if (asprintf(&ret
, "%d", getpid()) == -1) {
2336 warn("spawn: asprintf _SWM_PID");
2339 setenv("_SWM_PID", ret
, 1);
2343 if (setsid() == -1) {
2344 warn("spawn: setsid");
2350 * close stdin and stdout to prevent interaction between apps
2351 * and the baraction script
2352 * leave stderr open to record errors
2354 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2355 warn("spawn: open");
2358 dup2(fd
, STDIN_FILENO
);
2359 dup2(fd
, STDOUT_FILENO
);
2364 execvp(args
->argv
[0], args
->argv
);
2366 warn("spawn: execvp");
2371 kill_refs(struct ws_win
*win
)
2373 int i
, x
, num_screens
;
2374 struct swm_region
*r
;
2375 struct workspace
*ws
;
2380 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2381 for (i
= 0; i
< num_screens
; i
++)
2382 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2383 for (x
= 0; x
< workspace_limit
; x
++) {
2385 if (win
== ws
->focus
)
2387 if (win
== ws
->focus_prev
)
2388 ws
->focus_prev
= NULL
;
2393 validate_win(struct ws_win
*testwin
)
2396 struct workspace
*ws
;
2397 struct swm_region
*r
;
2398 int i
, x
, num_screens
;
2400 if (testwin
== NULL
)
2403 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2404 for (i
= 0; i
< num_screens
; i
++)
2405 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2406 for (x
= 0; x
< workspace_limit
; x
++) {
2408 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2416 validate_ws(struct workspace
*testws
)
2418 struct swm_region
*r
;
2419 struct workspace
*ws
;
2420 int i
, x
, num_screens
;
2422 /* validate all ws */
2423 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2424 for (i
= 0; i
< num_screens
; i
++)
2425 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2426 for (x
= 0; x
< workspace_limit
; x
++) {
2435 unfocus_win(struct ws_win
*win
)
2438 xcb_window_t none
= XCB_WINDOW_NONE
;
2440 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%lx\n", WINID(win
));
2444 if (win
->ws
== NULL
)
2447 if (validate_ws(win
->ws
))
2448 return; /* XXX this gets hit with thunderbird, needs fixing */
2450 if (win
->ws
->r
== NULL
)
2453 if (validate_win(win
)) {
2458 if (win
->ws
->focus
== win
) {
2459 win
->ws
->focus
= NULL
;
2460 win
->ws
->focus_prev
= win
;
2463 if (validate_win(win
->ws
->focus
)) {
2464 kill_refs(win
->ws
->focus
);
2465 win
->ws
->focus
= NULL
;
2467 if (validate_win(win
->ws
->focus_prev
)) {
2468 kill_refs(win
->ws
->focus_prev
);
2469 win
->ws
->focus_prev
= NULL
;
2472 /* drain all previous unfocus events */
2473 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
2476 grabbuttons(win
, 0);
2477 XSetWindowBorder(display
, win
->id
,
2478 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2480 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2481 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2489 int i
, j
, num_screens
;
2491 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2493 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2494 for (i
= 0; i
< num_screens
; i
++)
2495 for (j
= 0; j
< workspace_limit
; j
++)
2496 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2501 focus_win(struct ws_win
*win
)
2504 struct ws_win
*cfw
= NULL
;
2505 xcb_get_input_focus_cookie_t c
;
2506 xcb_get_input_focus_reply_t
*r
;
2507 xcb_window_t cur_focus
= XCB_WINDOW_NONE
;
2509 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%lx\n", WINID(win
));
2513 if (win
->ws
== NULL
)
2516 if (validate_ws(win
->ws
))
2517 return; /* XXX this gets hit with thunderbird, needs fixing */
2519 if (validate_win(win
)) {
2524 if (validate_win(win
)) {
2529 c
= xcb_get_input_focus(conn
);
2530 r
= xcb_get_input_focus_reply(conn
, c
, NULL
);
2532 cur_focus
= r
->focus
;
2535 if ((cfw
= find_window(cur_focus
)) != NULL
)
2538 /* use larger hammer since the window was killed somehow */
2539 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2540 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2541 XSetWindowBorder(display
, cfw
->id
,
2542 cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2545 win
->ws
->focus
= win
;
2547 if (win
->ws
->r
!= NULL
) {
2548 /* drain all previous focus events */
2549 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
2553 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
2554 win
->id
, XCB_CURRENT_TIME
);
2555 grabbuttons(win
, 1);
2556 XSetWindowBorder(display
, win
->id
,
2557 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2558 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2559 win
->ws
->always_raise
)
2560 map_window_raised(win
->id
);
2562 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2563 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2571 switchws(struct swm_region
*r
, union arg
*args
)
2573 int wsid
= args
->id
, unmap_old
= 0;
2574 struct swm_region
*this_r
, *other_r
;
2576 struct workspace
*new_ws
, *old_ws
;
2582 if (wsid
>= workspace_limit
)
2586 old_ws
= this_r
->ws
;
2587 new_ws
= &this_r
->s
->ws
[wsid
];
2589 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2590 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2592 if (new_ws
== NULL
|| old_ws
== NULL
)
2594 if (new_ws
== old_ws
)
2597 other_r
= new_ws
->r
;
2598 if (other_r
== NULL
) {
2599 /* the other workspace is hidden, hide this one */
2603 /* the other ws is visible in another region, exchange them */
2604 other_r
->ws_prior
= new_ws
;
2605 other_r
->ws
= old_ws
;
2606 old_ws
->r
= other_r
;
2608 this_r
->ws_prior
= old_ws
;
2609 this_r
->ws
= new_ws
;
2612 /* this is needed so that we can click on a window after a restart */
2616 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2617 focus(new_ws
->r
, &a
);
2619 /* unmap old windows */
2621 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2624 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2625 drain_enter_notify();
2629 cyclews(struct swm_region
*r
, union arg
*args
)
2632 struct swm_screen
*s
= r
->s
;
2635 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2636 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2641 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2644 case SWM_ARG_ID_CYCLEWS_UP
:
2645 if (a
.id
< workspace_limit
- 1)
2650 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2653 case SWM_ARG_ID_CYCLEWS_DOWN
:
2657 a
.id
= workspace_limit
- 1;
2664 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2666 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2670 } while (a
.id
!= r
->ws
->idx
);
2674 priorws(struct swm_region
*r
, union arg
*args
)
2678 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2679 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2681 if (r
->ws_prior
== NULL
)
2684 a
.id
= r
->ws_prior
->idx
;
2689 cyclescr(struct swm_region
*r
, union arg
*args
)
2691 struct swm_region
*rr
= NULL
;
2693 int i
, x
, y
, num_screens
;
2695 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2696 /* do nothing if we don't have more than one screen */
2697 if (!(num_screens
> 1 || outputs
> 1))
2702 case SWM_ARG_ID_CYCLESC_UP
:
2703 rr
= TAILQ_NEXT(r
, entry
);
2705 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2707 case SWM_ARG_ID_CYCLESC_DOWN
:
2708 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2710 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2718 /* move mouse to region */
2720 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2721 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2723 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2726 if (rr
->ws
->focus
) {
2727 /* move to focus window */
2728 x
= X(rr
->ws
->focus
) + 1;
2729 y
= Y(rr
->ws
->focus
) + 1;
2730 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2735 sort_windows(struct ws_win_list
*wl
)
2737 struct ws_win
*win
, *parent
, *nxt
;
2742 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2743 nxt
= TAILQ_NEXT(win
, entry
);
2744 if (win
->transient
) {
2745 parent
= find_window(win
->transient
);
2746 if (parent
== NULL
) {
2747 warnx("not possible bug");
2750 TAILQ_REMOVE(wl
, win
, entry
);
2751 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2758 swapwin(struct swm_region
*r
, union arg
*args
)
2760 struct ws_win
*target
, *source
;
2761 struct ws_win
*cur_focus
;
2762 struct ws_win_list
*wl
;
2765 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2766 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2768 cur_focus
= r
->ws
->focus
;
2769 if (cur_focus
== NULL
)
2773 wl
= &source
->ws
->winlist
;
2776 case SWM_ARG_ID_SWAPPREV
:
2777 if (source
->transient
)
2778 source
= find_window(source
->transient
);
2779 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2780 if (target
&& target
->transient
)
2781 target
= find_window(target
->transient
);
2782 TAILQ_REMOVE(wl
, source
, entry
);
2784 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2786 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2788 case SWM_ARG_ID_SWAPNEXT
:
2789 target
= TAILQ_NEXT(source
, entry
);
2790 /* move the parent and let the sort handle the move */
2791 if (source
->transient
)
2792 source
= find_window(source
->transient
);
2793 TAILQ_REMOVE(wl
, source
, entry
);
2795 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2797 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2799 case SWM_ARG_ID_SWAPMAIN
:
2800 target
= TAILQ_FIRST(wl
);
2801 if (target
== source
) {
2802 if (source
->ws
->focus_prev
!= NULL
&&
2803 source
->ws
->focus_prev
!= target
)
2804 source
= source
->ws
->focus_prev
;
2808 if (target
== NULL
|| source
== NULL
)
2810 source
->ws
->focus_prev
= target
;
2811 TAILQ_REMOVE(wl
, target
, entry
);
2812 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2813 TAILQ_REMOVE(wl
, source
, entry
);
2814 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2816 case SWM_ARG_ID_MOVELAST
:
2817 TAILQ_REMOVE(wl
, source
, entry
);
2818 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2821 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2831 focus_prev(struct ws_win
*win
)
2833 struct ws_win
*winfocus
= NULL
;
2834 struct ws_win
*cur_focus
= NULL
;
2835 struct ws_win_list
*wl
= NULL
;
2836 struct workspace
*ws
= NULL
;
2838 if (!(win
&& win
->ws
))
2843 cur_focus
= ws
->focus
;
2845 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%lx, cur_focus: 0x%lx\n",
2846 WINID(win
), WINID(cur_focus
));
2848 /* pickle, just focus on whatever */
2849 if (cur_focus
== NULL
) {
2850 /* use prev_focus if valid */
2851 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2852 find_window(WINID(ws
->focus_prev
)))
2853 winfocus
= ws
->focus_prev
;
2857 /* if transient focus on parent */
2858 if (cur_focus
->transient
) {
2859 winfocus
= find_window(cur_focus
->transient
);
2863 /* if in max_stack try harder */
2864 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2865 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2866 if (cur_focus
!= ws
->focus_prev
)
2867 winfocus
= ws
->focus_prev
;
2869 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2874 DNPRINTF(SWM_D_FOCUS
, "focus_prev: focus_close: %d\n", focus_close
);
2876 if (winfocus
== NULL
|| winfocus
== win
) {
2877 switch (focus_close
) {
2878 case SWM_STACK_BOTTOM
:
2879 winfocus
= TAILQ_FIRST(wl
);
2882 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2884 case SWM_STACK_ABOVE
:
2885 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
2886 if (focus_close_wrap
)
2887 winfocus
= TAILQ_FIRST(wl
);
2889 winfocus
= TAILQ_PREV(cur_focus
,
2890 ws_win_list
, entry
);
2893 case SWM_STACK_BELOW
:
2894 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
2896 if (focus_close_wrap
)
2897 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2899 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2905 if (winfocus
== NULL
) {
2906 if (focus_default
== SWM_STACK_TOP
)
2907 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2909 winfocus
= TAILQ_FIRST(wl
);
2913 focus_magic(winfocus
);
2917 focus(struct swm_region
*r
, union arg
*args
)
2919 struct ws_win
*winfocus
= NULL
, *head
;
2920 struct ws_win
*cur_focus
= NULL
;
2921 struct ws_win_list
*wl
= NULL
;
2922 struct workspace
*ws
= NULL
;
2928 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2930 /* treat FOCUS_CUR special */
2931 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2932 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2933 winfocus
= r
->ws
->focus
;
2934 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2935 winfocus
= r
->ws
->focus_prev
;
2937 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2938 if (winfocus
->iconic
== 0)
2941 focus_magic(winfocus
);
2945 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2949 if (TAILQ_EMPTY(wl
))
2951 /* make sure there is at least one uniconified window */
2953 TAILQ_FOREACH(winfocus
, wl
, entry
)
2954 if (winfocus
->iconic
== 0) {
2962 case SWM_ARG_ID_FOCUSPREV
:
2963 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2965 head
= TAILQ_LAST(wl
, ws_win_list
);
2967 if (WINID(winfocus
) == cur_focus
->transient
) {
2968 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2970 head
= TAILQ_LAST(wl
, ws_win_list
);
2975 if (winfocus
&& winfocus
->iconic
) {
2976 while (winfocus
!= cur_focus
) {
2977 if (winfocus
== NULL
)
2978 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2979 if (winfocus
->iconic
== 0)
2981 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
2987 case SWM_ARG_ID_FOCUSNEXT
:
2988 head
= TAILQ_NEXT(cur_focus
, entry
);
2990 head
= TAILQ_FIRST(wl
);
2994 if (winfocus
&& winfocus
->iconic
) {
2995 while (winfocus
!= cur_focus
) {
2996 if (winfocus
== NULL
)
2997 winfocus
= TAILQ_FIRST(wl
);
2998 if (winfocus
->iconic
== 0)
3000 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3005 case SWM_ARG_ID_FOCUSMAIN
:
3006 winfocus
= TAILQ_FIRST(wl
);
3007 if (winfocus
== cur_focus
)
3008 winfocus
= cur_focus
->ws
->focus_prev
;
3015 focus_magic(winfocus
);
3019 cycle_layout(struct swm_region
*r
, union arg
*args
)
3021 struct workspace
*ws
= r
->ws
;
3024 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3027 if (ws
->cur_layout
->l_stack
== NULL
)
3028 ws
->cur_layout
= &layouts
[0];
3031 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3032 drain_enter_notify();
3033 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3038 stack_config(struct swm_region
*r
, union arg
*args
)
3040 struct workspace
*ws
= r
->ws
;
3042 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3045 if (ws
->cur_layout
->l_config
!= NULL
)
3046 ws
->cur_layout
->l_config(ws
, args
->id
);
3048 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3055 struct swm_geometry g
;
3056 struct swm_region
*r
;
3062 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3064 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3065 for (i
= 0; i
< num_screens
; i
++) {
3069 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3070 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3071 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3073 /* start with screen geometry, adjust for bar */
3075 g
.w
-= 2 * border_width
;
3076 g
.h
-= 2 * border_width
;
3082 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3083 r
->ws
->cur_layout
->l_string(r
->ws
);
3084 /* save r so we can track region changes */
3091 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3092 drain_enter_notify();
3094 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3098 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3100 /* retain window geom and region geom */
3101 win
->g_float
= win
->g
;
3102 win
->g_float
.x
-= X(r
);
3103 win
->g_float
.y
-= Y(r
);
3104 win
->g_floatvalid
= 1;
3105 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%lx, g: (%d,%d)"
3106 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3107 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3108 win
->g_float
.w
, win
->g_float
.h
);
3112 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3117 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%lx\n", win
->id
);
3120 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3121 * geom on ws switches or return from max mode
3123 if (win
->g_floatvalid
&& (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3124 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)))) {
3125 /* refloat at last floating relative position */
3126 win
->g
= win
->g_float
;
3131 win
->floatmaxed
= 0;
3134 * if set to fullscreen mode, configure window to maximum size.
3136 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3137 if (!win
->g_floatvalid
)
3138 store_float_geom(win
, win
->ws
->r
);
3140 win
->g
= win
->ws
->r
->g
;
3144 * remove border on fullscreen floater when in fullscreen mode or when
3145 * the quirk is present.
3147 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3148 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3149 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3150 if (win
->bordered
) {
3152 X(win
) += border_width
;
3153 Y(win
) += border_width
;
3155 } else if (!win
->bordered
) {
3157 X(win
) -= border_width
;
3158 Y(win
) -= border_width
;
3161 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3162 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3163 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3168 * floaters and transients are auto-centred unless moved
3171 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3172 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3175 /* keep window within region bounds */
3176 constrain_window(win
, r
, 0);
3182 * Send keystrokes to terminal to decrease/increase the font size as the
3183 * window size changes.
3186 adjust_font(struct ws_win
*win
)
3188 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3189 win
->floating
|| win
->transient
)
3192 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3193 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3194 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3195 win
->font_size_boundary
[win
->font_steps
] =
3196 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3199 win
->last_inc
= win
->sh
.width_inc
;
3200 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
3201 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3202 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3205 win
->last_inc
= win
->sh
.width_inc
;
3206 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
3210 #define SWAPXY(g) do { \
3212 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3213 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3216 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3218 XWindowAttributes wa
;
3219 struct swm_geometry win_g
, r_g
= *g
;
3220 struct ws_win
*win
, *fs_win
= NULL
;
3221 int i
, j
, s
, stacks
;
3222 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3223 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3224 int split
, colno
, winno
, mwin
, msize
, mscale
;
3225 int remain
, missing
, v_slice
, reconfigure
;
3228 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3229 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3231 winno
= count_win(ws
, 0);
3232 if (winno
== 0 && count_win(ws
, 1) == 0)
3235 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3236 if (win
->transient
== 0 && win
->floating
== 0
3237 && win
->iconic
== 0)
3244 w_inc
= win
->sh
.width_inc
;
3245 w_base
= win
->sh
.base_width
;
3246 mwin
= ws
->l_state
.horizontal_mwin
;
3247 mscale
= ws
->l_state
.horizontal_msize
;
3248 stacks
= ws
->l_state
.horizontal_stacks
;
3251 w_inc
= win
->sh
.height_inc
;
3252 w_base
= win
->sh
.base_height
;
3253 mwin
= ws
->l_state
.vertical_mwin
;
3254 mscale
= ws
->l_state
.vertical_msize
;
3255 stacks
= ws
->l_state
.vertical_stacks
;
3259 if (stacks
> winno
- mwin
)
3260 stacks
= winno
- mwin
;
3264 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3265 if (mwin
&& winno
> mwin
) {
3266 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3270 win_g
.w
= v_slice
* mscale
;
3272 if (w_inc
> 1 && w_inc
< v_slice
) {
3273 /* adjust for window's requested size increment */
3274 remain
= (win_g
.w
- w_base
) % w_inc
;
3280 win_g
.x
+= r_g
.w
- msize
;
3283 colno
= split
= winno
/ stacks
;
3284 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3285 2 * border_width
) / stacks
);
3287 hrh
= r_g
.h
/ colno
;
3288 extra
= r_g
.h
- (colno
* hrh
);
3289 win_g
.h
= hrh
- 2 * border_width
;
3291 /* stack all the tiled windows */
3292 i
= j
= 0, s
= stacks
;
3293 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3294 if (win
->transient
!= 0 || win
->floating
!= 0)
3296 if (win
->iconic
!= 0)
3299 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3304 if (split
&& i
== split
) {
3305 colno
= (winno
- mwin
) / stacks
;
3306 if (s
<= (winno
- mwin
) % stacks
)
3308 split
= split
+ colno
;
3309 hrh
= (r_g
.h
/ colno
);
3310 extra
= r_g
.h
- (colno
* hrh
);
3314 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3315 win_g
.w
= (r_g
.w
- msize
-
3316 (stacks
* 2 * border_width
)) / stacks
;
3318 win_g
.w
+= (r_g
.w
- msize
-
3319 (stacks
* 2 * border_width
)) % stacks
;
3323 win_g
.h
= hrh
- 2 * border_width
;
3325 h_inc
= win
->sh
.width_inc
;
3326 h_base
= win
->sh
.base_width
;
3328 h_inc
= win
->sh
.height_inc
;
3329 h_base
= win
->sh
.base_height
;
3331 if (j
== colno
- 1) {
3332 win_g
.h
= hrh
+ extra
;
3333 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3334 /* adjust for window's requested size increment */
3335 remain
= (win_g
.h
- h_base
) % h_inc
;
3336 missing
= h_inc
- remain
;
3338 if (missing
<= extra
|| j
== 0) {
3350 win_g
.y
+= last_h
+ 2 * border_width
;
3352 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3354 win_g
.w
+= 2 * border_width
;
3355 win_g
.h
+= 2 * border_width
;
3360 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3361 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3365 WIDTH(win
) = win_g
.h
;
3366 HEIGHT(win
) = win_g
.w
;
3369 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3370 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3374 WIDTH(win
) = win_g
.w
;
3375 HEIGHT(win
) = win_g
.h
;
3379 if (bordered
!= win
->bordered
) {
3381 win
->bordered
= bordered
;
3389 if (XGetWindowAttributes(display
, win
->id
, &wa
))
3390 if (wa
.map_state
== IsUnmapped
)
3391 map_window_raised(win
->id
);
3399 /* now, stack all the floaters and transients */
3400 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3401 if (win
->transient
== 0 && win
->floating
== 0)
3403 if (win
->iconic
== 1)
3405 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3410 stack_floater(win
, ws
->r
);
3411 map_window_raised(win
->id
);
3415 stack_floater(fs_win
, ws
->r
);
3416 map_window_raised(fs_win
->id
);
3421 vertical_config(struct workspace
*ws
, int id
)
3423 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3427 case SWM_ARG_ID_STACKRESET
:
3428 case SWM_ARG_ID_STACKINIT
:
3429 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3430 ws
->l_state
.vertical_mwin
= 1;
3431 ws
->l_state
.vertical_stacks
= 1;
3433 case SWM_ARG_ID_MASTERSHRINK
:
3434 if (ws
->l_state
.vertical_msize
> 1)
3435 ws
->l_state
.vertical_msize
--;
3437 case SWM_ARG_ID_MASTERGROW
:
3438 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3439 ws
->l_state
.vertical_msize
++;
3441 case SWM_ARG_ID_MASTERADD
:
3442 ws
->l_state
.vertical_mwin
++;
3444 case SWM_ARG_ID_MASTERDEL
:
3445 if (ws
->l_state
.vertical_mwin
> 0)
3446 ws
->l_state
.vertical_mwin
--;
3448 case SWM_ARG_ID_STACKINC
:
3449 ws
->l_state
.vertical_stacks
++;
3451 case SWM_ARG_ID_STACKDEC
:
3452 if (ws
->l_state
.vertical_stacks
> 1)
3453 ws
->l_state
.vertical_stacks
--;
3455 case SWM_ARG_ID_FLIPLAYOUT
:
3456 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3464 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3466 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3468 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3472 horizontal_config(struct workspace
*ws
, int id
)
3474 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3477 case SWM_ARG_ID_STACKRESET
:
3478 case SWM_ARG_ID_STACKINIT
:
3479 ws
->l_state
.horizontal_mwin
= 1;
3480 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3481 ws
->l_state
.horizontal_stacks
= 1;
3483 case SWM_ARG_ID_MASTERSHRINK
:
3484 if (ws
->l_state
.horizontal_msize
> 1)
3485 ws
->l_state
.horizontal_msize
--;
3487 case SWM_ARG_ID_MASTERGROW
:
3488 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3489 ws
->l_state
.horizontal_msize
++;
3491 case SWM_ARG_ID_MASTERADD
:
3492 ws
->l_state
.horizontal_mwin
++;
3494 case SWM_ARG_ID_MASTERDEL
:
3495 if (ws
->l_state
.horizontal_mwin
> 0)
3496 ws
->l_state
.horizontal_mwin
--;
3498 case SWM_ARG_ID_STACKINC
:
3499 ws
->l_state
.horizontal_stacks
++;
3501 case SWM_ARG_ID_STACKDEC
:
3502 if (ws
->l_state
.horizontal_stacks
> 1)
3503 ws
->l_state
.horizontal_stacks
--;
3505 case SWM_ARG_ID_FLIPLAYOUT
:
3506 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3514 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3516 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3518 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3521 /* fullscreen view */
3523 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3525 struct swm_geometry gg
= *g
;
3526 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3527 int winno
, num_screens
;
3529 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3534 winno
= count_win(ws
, 0);
3535 if (winno
== 0 && count_win(ws
, 1) == 0)
3538 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3539 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3540 if (win
->transient
) {
3542 parent
= find_window(win
->transient
);
3546 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3548 * retain geometry for retrieval on exit from
3551 store_float_geom(win
, ws
->r
);
3552 win
->floatmaxed
= 1;
3555 /* only reconfigure if necessary */
3556 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3557 HEIGHT(win
) != gg
.h
) {
3563 WIDTH(win
) += 2 * border_width
;
3564 HEIGHT(win
) += 2 * border_width
;
3569 /* unmap only if we don't have multi screen */
3570 if (win
!= ws
->focus
)
3571 if (!(num_screens
> 1 || outputs
> 1))
3575 /* put the last transient on top */
3578 map_window_raised(parent
->id
);
3579 stack_floater(wintrans
, ws
->r
);
3580 focus_magic(wintrans
);
3585 send_to_ws(struct swm_region
*r
, union arg
*args
)
3587 int wsid
= args
->id
;
3588 struct ws_win
*win
= NULL
, *parent
;
3589 struct workspace
*ws
, *nws
;
3590 Atom ws_idx_atom
= 0;
3591 unsigned char ws_idx_str
[SWM_PROPLEN
];
3594 if (wsid
>= workspace_limit
)
3597 if (r
&& r
->ws
&& r
->ws
->focus
)
3603 if (win
->ws
->idx
== wsid
)
3606 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%lx\n", win
->id
);
3609 nws
= &win
->s
->ws
[wsid
];
3611 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3613 if (win
->transient
) {
3614 parent
= find_window(win
->transient
);
3616 unmap_window(parent
);
3617 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3618 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3623 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3624 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3625 if (TAILQ_EMPTY(&ws
->winlist
))
3626 r
->ws
->focus
= NULL
;
3629 /* Try to update the window's workspace property */
3630 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
3632 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3634 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3636 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
3637 PropModeReplace
, ws_idx_str
, strlen((char *)ws_idx_str
));
3645 pressbutton(struct swm_region
*r
, union arg
*args
)
3647 XTestFakeButtonEvent(display
, args
->id
, True
, CurrentTime
);
3648 XTestFakeButtonEvent(display
, args
->id
, False
, CurrentTime
);
3652 raise_toggle(struct swm_region
*r
, union arg
*args
)
3654 if (r
== NULL
|| r
->ws
== NULL
)
3657 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3659 /* bring floaters back to top */
3660 if (r
->ws
->always_raise
== 0)
3665 iconify(struct swm_region
*r
, union arg
*args
)
3669 if (r
->ws
->focus
== NULL
)
3671 unmap_window(r
->ws
->focus
);
3672 update_iconic(r
->ws
->focus
, 1);
3674 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3675 drain_enter_notify();
3676 r
->ws
->focus
= NULL
;
3677 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3682 get_win_name(xcb_window_t win
)
3684 unsigned char *prop
= NULL
;
3685 unsigned long nbytes
, nitems
;
3687 /* try _NET_WM_NAME first */
3688 if (get_property(win
, a_netwmname
, 0L, a_utf8_string
, NULL
, &nbytes
,
3691 if (get_property(win
, a_netwmname
, nbytes
, a_utf8_string
,
3692 &nitems
, NULL
, &prop
))
3696 /* fallback to WM_NAME */
3697 if (!get_property(win
, a_wmname
, 0L, a_string
, NULL
, &nbytes
, &prop
))
3700 if (get_property(win
, a_wmname
, nbytes
, a_string
, &nitems
, NULL
, &prop
))
3707 uniconify(struct swm_region
*r
, union arg
*args
)
3711 unsigned char *name
;
3714 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3716 if (r
== NULL
|| r
->ws
== NULL
)
3719 /* make sure we have anything to uniconify */
3720 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3721 if (win
->ws
== NULL
)
3722 continue; /* should never happen */
3723 if (win
->iconic
== 0)
3731 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3733 spawn_select(r
, args
, "search", &searchpid
);
3735 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3738 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3739 if (win
->ws
== NULL
)
3740 continue; /* should never happen */
3741 if (win
->iconic
== 0)
3744 name
= get_win_name(win
->id
);
3747 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
3755 name_workspace(struct swm_region
*r
, union arg
*args
)
3759 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3765 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3767 spawn_select(r
, args
, "name_workspace", &searchpid
);
3769 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3772 fprintf(lfile
, "%s", "");
3777 search_workspace(struct swm_region
*r
, union arg
*args
)
3780 struct workspace
*ws
;
3783 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3789 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3791 spawn_select(r
, args
, "search", &searchpid
);
3793 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3796 for (i
= 0; i
< workspace_limit
; i
++) {
3800 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3801 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3808 search_win_cleanup(void)
3810 struct search_window
*sw
= NULL
;
3812 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3813 xcb_destroy_window(conn
, sw
->indicator
);
3814 XFreeGC(display
, sw
->gc
);
3815 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3821 search_win(struct swm_region
*r
, union arg
*args
)
3823 struct ws_win
*win
= NULL
;
3824 struct search_window
*sw
= NULL
;
3831 XRectangle ibox
, lbox
;
3833 DNPRINTF(SWM_D_MISC
, "search_win\n");
3836 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3838 spawn_select(r
, args
, "search", &searchpid
);
3840 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3843 TAILQ_INIT(&search_wl
);
3846 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3847 if (win
->iconic
== 1)
3850 sw
= calloc(1, sizeof(struct search_window
));
3852 warn("search_win: calloc");
3854 search_win_cleanup();
3860 snprintf(s
, sizeof s
, "%d", i
);
3863 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
3865 w
= XCreateSimpleWindow(display
,
3866 win
->id
, 0, 0,lbox
.width
+ 4,
3867 bar_fs_extents
->max_logical_extent
.height
, 1,
3868 r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
,
3869 r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
3872 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3874 sw
->gc
= XCreateGC(display
, w
, 0, &gcv
);
3875 map_window_raised(w
);
3876 XSetForeground(display
, sw
->gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
3878 DRAWSTRING(display
, w
, bar_fs
, sw
->gc
, 2,
3879 (bar_fs_extents
->max_logical_extent
.height
-
3880 lbox
.height
) / 2 - lbox
.y
, s
, len
);
3882 fprintf(lfile
, "%d\n", i
);
3890 search_resp_uniconify(char *resp
, unsigned long len
)
3892 unsigned char *name
;
3896 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3898 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3899 if (win
->iconic
== 0)
3901 name
= get_win_name(win
->id
);
3904 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
3909 if (strncmp(s
, resp
, len
) == 0) {
3910 /* XXX this should be a callback to generalize */
3911 update_iconic(win
, 0);
3920 search_resp_name_workspace(char *resp
, unsigned long len
)
3922 struct workspace
*ws
;
3924 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3926 if (search_r
->ws
== NULL
)
3931 free(search_r
->ws
->name
);
3932 search_r
->ws
->name
= NULL
;
3936 ws
->name
= strdup(resp
);
3937 if (ws
->name
== NULL
) {
3938 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
3939 "strdup: %s", strerror(errno
));
3946 search_resp_search_workspace(char *resp
, unsigned long len
)
3953 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
3957 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
3964 ws_idx
= strtonum(q
, 1, workspace_limit
, &errstr
);
3966 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
3973 switchws(search_r
, &a
);
3977 search_resp_search_window(char *resp
, unsigned long len
)
3982 struct search_window
*sw
;
3984 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
3988 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
3993 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
3995 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
4002 TAILQ_FOREACH(sw
, &search_wl
, entry
)
4003 if (idx
== sw
->idx
) {
4009 #define MAX_RESP_LEN 1024
4012 search_do_resp(void)
4018 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
4023 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
4024 warn("search: calloc");
4028 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
4030 warn("search: read error");
4033 resp
[rbytes
] = '\0';
4036 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4037 * newline, so work around that by sanitizing the resp now.
4039 resp
[strcspn(resp
, "\n")] = '\0';
4042 switch (search_resp_action
) {
4043 case SWM_SEARCH_UNICONIFY
:
4044 search_resp_uniconify(resp
, len
);
4046 case SWM_SEARCH_NAME_WORKSPACE
:
4047 search_resp_name_workspace(resp
, len
);
4049 case SWM_SEARCH_SEARCH_WORKSPACE
:
4050 search_resp_search_workspace(resp
, len
);
4052 case SWM_SEARCH_SEARCH_WINDOW
:
4053 search_resp_search_window(resp
, len
);
4058 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4059 search_win_cleanup();
4061 search_resp_action
= SWM_SEARCH_NONE
;
4062 close(select_resp_pipe
[0]);
4067 wkill(struct swm_region
*r
, union arg
*args
)
4069 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4071 if (r
->ws
->focus
== NULL
)
4074 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4075 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4077 if (r
->ws
->focus
->can_delete
)
4078 client_msg(r
->ws
->focus
, adelete
);
4083 floating_toggle_win(struct ws_win
*win
)
4085 struct swm_region
*r
;
4095 /* reject floating toggles in max stack mode */
4096 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4099 if (win
->floating
) {
4100 if (!win
->floatmaxed
) {
4101 /* retain position for refloat */
4102 store_float_geom(win
, r
);
4106 if (win
->g_floatvalid
) {
4107 /* refloat at last floating relative position */
4108 X(win
) = win
->g_float
.x
+ X(r
);
4109 Y(win
) = win
->g_float
.y
+ Y(r
);
4110 WIDTH(win
) = win
->g_float
.w
;
4111 HEIGHT(win
) = win
->g_float
.h
;
4116 ewmh_update_actions(win
);
4122 floating_toggle(struct swm_region
*r
, union arg
*args
)
4124 struct ws_win
*win
= r
->ws
->focus
;
4130 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4131 _NET_WM_STATE_TOGGLE
);
4134 if (focus_mode
== SWM_FOCUS_DEFAULT
)
4135 drain_enter_notify();
4137 if (win
== win
->ws
->focus
) {
4138 a
.id
= SWM_ARG_ID_FOCUSCUR
;
4139 focus(win
->ws
->r
, &a
);
4144 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4146 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4148 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4150 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4153 if (X(win
) + BORDER(win
) < X(r
)) {
4155 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4157 X(win
) = X(r
) - BORDER(win
);
4160 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4162 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4164 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4167 if (Y(win
) + BORDER(win
) < Y(r
)) {
4169 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4171 Y(win
) = Y(r
) - BORDER(win
);
4177 if (HEIGHT(win
) < 1)
4183 update_window(struct ws_win
*win
)
4188 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
4189 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
4190 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
4194 wc
[3] = HEIGHT(win
);
4195 wc
[4] = BORDER(win
);
4197 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
4198 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
4199 wc
[3], YESNO(win
->bordered
));
4201 xcb_configure_window(conn
, win
->id
, mask
, wc
);
4204 #define SWM_RESIZE_STEPS (50)
4207 resize(struct ws_win
*win
, union arg
*args
)
4211 struct swm_region
*r
= NULL
;
4212 int resize_step
= 0;
4216 struct swm_geometry g
;
4217 int top
= 0, left
= 0;
4220 unsigned int shape
; /* cursor style */
4226 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4229 DNPRINTF(SWM_D_MOUSE
, "resize: window: 0x%lx, floating: %s, "
4230 "transient: 0x%lx\n", win
->id
, YESNO(win
->floating
),
4233 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4236 /* reject resizes in max mode for floaters (transient ok) */
4237 if (win
->floatmaxed
)
4241 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4247 case SWM_ARG_ID_WIDTHSHRINK
:
4248 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4251 case SWM_ARG_ID_WIDTHGROW
:
4252 WIDTH(win
) += SWM_RESIZE_STEPS
;
4255 case SWM_ARG_ID_HEIGHTSHRINK
:
4256 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4259 case SWM_ARG_ID_HEIGHTGROW
:
4260 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4267 constrain_window(win
, r
, 1);
4269 store_float_geom(win
,r
);
4273 if (focus_mode
== SWM_FOCUS_DEFAULT
)
4274 drain_enter_notify();
4276 /* get cursor offset from window root */
4277 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4282 if (wx
< WIDTH(win
) / 2)
4285 if (wy
< HEIGHT(win
) / 2)
4288 if (args
->id
== SWM_ARG_ID_CENTER
)
4291 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
4293 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
4295 cursor
= XCreateFontCursor(display
, shape
);
4297 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4298 GrabModeAsync
, None
, cursor
, CurrentTime
) != GrabSuccess
) {
4299 XFreeCursor(display
, cursor
);
4304 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4305 SubstructureRedirectMask
, &ev
);
4307 case ConfigureRequest
:
4310 handler
[ev
.type
](&ev
);
4313 /* cursor offset/delta from start of the operation */
4314 dx
= ev
.xmotion
.x_root
- x
;
4315 dy
= ev
.xmotion
.y_root
- y
;
4321 if (args
->id
== SWM_ARG_ID_CENTER
) {
4322 if (g
.h
/ 2 + dy
< 1)
4326 HEIGHT(win
) = g
.h
+ 2 * dy
;
4334 HEIGHT(win
) = g
.h
+ dy
;
4341 if (args
->id
== SWM_ARG_ID_CENTER
) {
4342 if (g
.w
/ 2 + dx
< 1)
4346 WIDTH(win
) = g
.w
+ 2 * dx
;
4354 WIDTH(win
) = g
.w
+ dx
;
4357 constrain_window(win
, r
, 1);
4359 /* not free, don't sync more than 120 times / second */
4360 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4361 time
= ev
.xmotion
.time
;
4362 XSync(display
, False
);
4367 } while (ev
.type
!= ButtonRelease
);
4369 XSync(display
, False
);
4372 store_float_geom(win
,r
);
4374 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4375 XFreeCursor(display
, cursor
);
4378 drain_enter_notify();
4382 resize_step(struct swm_region
*r
, union arg
*args
)
4384 struct ws_win
*win
= NULL
;
4386 if (r
&& r
->ws
&& r
->ws
->focus
)
4394 #define SWM_MOVE_STEPS (50)
4397 move(struct ws_win
*win
, union arg
*args
)
4402 struct swm_region
*r
= NULL
;
4412 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4415 DNPRINTF(SWM_D_MOUSE
, "move: window: 0x%lx, floating: %s, transient: "
4416 "0x%lx\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4418 /* in max_stack mode should only move transients */
4419 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4423 if (win
->floating
== 0 && !win
->transient
) {
4424 store_float_geom(win
, r
);
4425 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4428 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4435 case SWM_ARG_ID_MOVELEFT
:
4436 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4439 case SWM_ARG_ID_MOVERIGHT
:
4440 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4443 case SWM_ARG_ID_MOVEUP
:
4444 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4447 case SWM_ARG_ID_MOVEDOWN
:
4448 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4455 constrain_window(win
, r
, 0);
4457 store_float_geom(win
, r
);
4461 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4462 GrabModeAsync
, None
, XCreateFontCursor(display
, XC_fleur
),
4463 CurrentTime
) != GrabSuccess
)
4466 /* get cursor offset from window root */
4467 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4471 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4472 SubstructureRedirectMask
, &ev
);
4474 case ConfigureRequest
:
4477 handler
[ev
.type
](&ev
);
4480 X(win
) = ev
.xmotion
.x_root
- wx
- border_width
;
4481 Y(win
) = ev
.xmotion
.y_root
- wy
- border_width
;
4483 constrain_window(win
, r
, 0);
4485 /* not free, don't sync more than 120 times / second */
4486 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4487 time
= ev
.xmotion
.time
;
4488 XSync(display
, False
);
4493 } while (ev
.type
!= ButtonRelease
);
4495 XSync(display
, False
);
4498 store_float_geom(win
, r
);
4499 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4502 drain_enter_notify();
4506 move_step(struct swm_region
*r
, union arg
*args
)
4508 struct ws_win
*win
= NULL
;
4510 if (r
&& r
->ws
&& r
->ws
->focus
)
4515 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4522 /* user/key callable function IDs */
4611 kf_search_workspace
,
4613 kf_dumpwins
, /* MUST BE LAST */
4617 /* key definitions */
4619 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4624 char name
[SWM_FUNCNAME_LEN
];
4625 void (*func
)(struct swm_region
*r
, union arg
*);
4627 } keyfuncs
[kf_invalid
+ 1] = {
4628 /* name function argument */
4629 { "cycle_layout", cycle_layout
, {0} },
4630 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4631 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4632 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4633 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4634 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4635 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4636 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4637 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4638 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4639 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4640 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4641 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4642 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4643 { "quit", quit
, {0} },
4644 { "restart", restart
, {0} },
4645 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4646 { "ws_1", switchws
, {.id
= 0} },
4647 { "ws_2", switchws
, {.id
= 1} },
4648 { "ws_3", switchws
, {.id
= 2} },
4649 { "ws_4", switchws
, {.id
= 3} },
4650 { "ws_5", switchws
, {.id
= 4} },
4651 { "ws_6", switchws
, {.id
= 5} },
4652 { "ws_7", switchws
, {.id
= 6} },
4653 { "ws_8", switchws
, {.id
= 7} },
4654 { "ws_9", switchws
, {.id
= 8} },
4655 { "ws_10", switchws
, {.id
= 9} },
4656 { "ws_11", switchws
, {.id
= 10} },
4657 { "ws_12", switchws
, {.id
= 11} },
4658 { "ws_13", switchws
, {.id
= 12} },
4659 { "ws_14", switchws
, {.id
= 13} },
4660 { "ws_15", switchws
, {.id
= 14} },
4661 { "ws_16", switchws
, {.id
= 15} },
4662 { "ws_17", switchws
, {.id
= 16} },
4663 { "ws_18", switchws
, {.id
= 17} },
4664 { "ws_19", switchws
, {.id
= 18} },
4665 { "ws_20", switchws
, {.id
= 19} },
4666 { "ws_21", switchws
, {.id
= 20} },
4667 { "ws_22", switchws
, {.id
= 21} },
4668 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4669 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4670 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4671 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4672 { "ws_prior", priorws
, {0} },
4673 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4674 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4675 { "mvws_1", send_to_ws
, {.id
= 0} },
4676 { "mvws_2", send_to_ws
, {.id
= 1} },
4677 { "mvws_3", send_to_ws
, {.id
= 2} },
4678 { "mvws_4", send_to_ws
, {.id
= 3} },
4679 { "mvws_5", send_to_ws
, {.id
= 4} },
4680 { "mvws_6", send_to_ws
, {.id
= 5} },
4681 { "mvws_7", send_to_ws
, {.id
= 6} },
4682 { "mvws_8", send_to_ws
, {.id
= 7} },
4683 { "mvws_9", send_to_ws
, {.id
= 8} },
4684 { "mvws_10", send_to_ws
, {.id
= 9} },
4685 { "mvws_11", send_to_ws
, {.id
= 10} },
4686 { "mvws_12", send_to_ws
, {.id
= 11} },
4687 { "mvws_13", send_to_ws
, {.id
= 12} },
4688 { "mvws_14", send_to_ws
, {.id
= 13} },
4689 { "mvws_15", send_to_ws
, {.id
= 14} },
4690 { "mvws_16", send_to_ws
, {.id
= 15} },
4691 { "mvws_17", send_to_ws
, {.id
= 16} },
4692 { "mvws_18", send_to_ws
, {.id
= 17} },
4693 { "mvws_19", send_to_ws
, {.id
= 18} },
4694 { "mvws_20", send_to_ws
, {.id
= 19} },
4695 { "mvws_21", send_to_ws
, {.id
= 20} },
4696 { "mvws_22", send_to_ws
, {.id
= 21} },
4697 { "bar_toggle", bar_toggle
, {0} },
4698 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4699 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4700 { "float_toggle", floating_toggle
,{0} },
4701 { "version", version
, {0} },
4702 { "spawn_custom", dummykeyfunc
, {0} },
4703 { "iconify", iconify
, {0} },
4704 { "uniconify", uniconify
, {0} },
4705 { "raise_toggle", raise_toggle
, {0} },
4706 { "button2", pressbutton
, {2} },
4707 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4708 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4709 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4710 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4711 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4712 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4713 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4714 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4715 { "name_workspace", name_workspace
, {0} },
4716 { "search_workspace", search_workspace
, {0} },
4717 { "search_win", search_win
, {0} },
4718 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4719 { "invalid key func", NULL
, {0} },
4722 RB_ENTRY(key
) entry
;
4725 enum keyfuncid funcid
;
4728 RB_HEAD(key_tree
, key
);
4731 key_cmp(struct key
*kp1
, struct key
*kp2
)
4733 if (kp1
->keysym
< kp2
->keysym
)
4735 if (kp1
->keysym
> kp2
->keysym
)
4738 if (kp1
->mod
< kp2
->mod
)
4740 if (kp1
->mod
> kp2
->mod
)
4746 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
4747 struct key_tree keys
;
4750 enum { client_click
, root_click
};
4752 unsigned int action
;
4754 unsigned int button
;
4755 void (*func
)(struct ws_win
*, union arg
*);
4758 /* action key mouse button func args */
4759 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4760 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4761 { client_click
, MODKEY
, Button1
, move
, {0} },
4765 update_modkey(unsigned int mod
)
4771 RB_FOREACH(kp
, key_tree
, &keys
)
4772 if (kp
->mod
& ShiftMask
)
4773 kp
->mod
= mod
| ShiftMask
;
4777 for (i
= 0; i
< LENGTH(buttons
); i
++)
4778 if (buttons
[i
].mask
& ShiftMask
)
4779 buttons
[i
].mask
= mod
| ShiftMask
;
4781 buttons
[i
].mask
= mod
;
4786 TAILQ_ENTRY(spawn_prog
) entry
;
4791 TAILQ_HEAD(spawn_list
, spawn_prog
);
4792 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4795 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4798 struct spawn_prog
*prog
= NULL
;
4800 char *ap
, **real_args
;
4802 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4805 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4806 if (!strcasecmp(spawn_name
, prog
->name
))
4810 warnx("spawn_custom: program %s not found", spawn_name
);
4814 /* make room for expanded args */
4815 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4816 err(1, "spawn_custom: calloc real_args");
4818 /* expand spawn_args into real_args */
4819 for (i
= 0; i
< prog
->argc
; i
++) {
4821 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4822 if (!strcasecmp(ap
, "$bar_border")) {
4824 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4826 err(1, "spawn_custom border color");
4827 } else if (!strcasecmp(ap
, "$bar_color")) {
4829 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4831 err(1, "spawn_custom bar color");
4832 } else if (!strcasecmp(ap
, "$bar_font")) {
4833 if ((real_args
[i
] = strdup(bar_fonts
))
4835 err(1, "spawn_custom bar fonts");
4836 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4838 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4840 err(1, "spawn_custom color font");
4841 } else if (!strcasecmp(ap
, "$color_focus")) {
4843 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4845 err(1, "spawn_custom color focus");
4846 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4848 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4850 err(1, "spawn_custom color unfocus");
4852 /* no match --> copy as is */
4853 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4854 err(1, "spawn_custom strdup(ap)");
4856 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4861 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4862 for (i
= 0; i
< prog
->argc
; i
++)
4863 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4864 DNPRINTF(SWM_D_SPAWN
, "\n");
4866 *ret_args
= real_args
;
4867 return (prog
->argc
);
4871 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4877 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4881 spawn(r
->ws
->idx
, &a
, 1);
4883 for (i
= 0; i
< spawn_argc
; i
++)
4889 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4895 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4899 if (pipe(select_list_pipe
) == -1)
4900 err(1, "pipe error");
4901 if (pipe(select_resp_pipe
) == -1)
4902 err(1, "pipe error");
4904 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4905 err(1, "could not disable SIGPIPE");
4906 switch (*pid
= fork()) {
4908 err(1, "cannot fork");
4911 if (dup2(select_list_pipe
[0], 0) == -1)
4913 if (dup2(select_resp_pipe
[1], 1) == -1)
4915 close(select_list_pipe
[1]);
4916 close(select_resp_pipe
[0]);
4917 spawn(r
->ws
->idx
, &a
, 0);
4919 default: /* parent */
4920 close(select_list_pipe
[0]);
4921 close(select_resp_pipe
[1]);
4925 for (i
= 0; i
< spawn_argc
; i
++)
4931 spawn_insert(char *name
, char *args
)
4933 char *arg
, *cp
, *ptr
;
4934 struct spawn_prog
*sp
;
4936 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
4938 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
4939 err(1, "spawn_insert: malloc");
4940 if ((sp
->name
= strdup(name
)) == NULL
)
4941 err(1, "spawn_insert: strdup");
4943 /* convert the arguments to an argument list */
4944 if ((ptr
= cp
= strdup(args
)) == NULL
)
4945 err(1, "spawn_insert: strdup");
4946 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
4947 /* empty field; skip it */
4952 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
4953 sizeof *sp
->argv
)) == NULL
)
4954 err(1, "spawn_insert: realloc");
4955 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
4956 err(1, "spawn_insert: strdup");
4960 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
4961 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
4965 spawn_remove(struct spawn_prog
*sp
)
4969 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
4971 TAILQ_REMOVE(&spawns
, sp
, entry
);
4972 for (i
= 0; i
< sp
->argc
; i
++)
4978 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
4982 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
4984 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
4987 spawn_insert(name
, args
);
4989 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
4993 setspawn(char *name
, char *args
)
4995 struct spawn_prog
*sp
;
4997 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5002 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5003 if (!strcmp(sp
->name
, name
)) {
5007 spawn_replace(sp
, name
, args
);
5008 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5012 if (*args
== '\0') {
5013 warnx("error: setspawn: cannot find program: %s", name
);
5017 spawn_insert(name
, args
);
5018 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5022 setconfspawn(char *selector
, char *value
, int flags
)
5024 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
5026 setspawn(selector
, value
);
5028 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
5035 setconfspawn("term", "xterm", 0);
5036 setconfspawn("spawn_term", "xterm", 0);
5037 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5038 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5039 setconfspawn("lock", "xlock", 0);
5040 setconfspawn("initscr", "initscreen.sh", 0);
5041 setconfspawn("menu", "dmenu_run"
5044 " -nf $bar_font_color"
5046 " -sf $bar_color", 0);
5047 setconfspawn("search", "dmenu"
5051 " -nf $bar_font_color"
5053 " -sf $bar_color", 0);
5054 setconfspawn("name_workspace", "dmenu"
5058 " -nf $bar_font_color"
5060 " -sf $bar_color", 0);
5064 #define SWM_MODNAME_SIZE 32
5065 #define SWM_KEY_WS "\n+ \t"
5067 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5071 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5072 if (mod
== NULL
|| ks
== NULL
) {
5073 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5076 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5077 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5083 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5084 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5086 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5087 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5089 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5091 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5093 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5095 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5097 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5099 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5100 *mod
|= ControlMask
;
5102 *ks
= XStringToKeysym(name
);
5103 XConvertCase(*ks
, ks
, &uks
);
5104 if (ks
== NoSymbol
) {
5106 "parsekeys: invalid key %s\n",
5112 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5117 strdupsafe(char *str
)
5122 return (strdup(str
));
5126 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5130 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5131 keyfuncs
[kfid
].name
, spawn_name
);
5133 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5134 err(1, "key_insert: malloc");
5139 kp
->spawn_name
= strdupsafe(spawn_name
);
5140 RB_INSERT(key_tree
, &keys
, kp
);
5142 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5146 key_lookup(unsigned int mod
, KeySym ks
)
5153 return (RB_FIND(key_tree
, &keys
, &kp
));
5157 key_remove(struct key
*kp
)
5159 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5161 RB_REMOVE(key_tree
, &keys
, kp
);
5162 free(kp
->spawn_name
);
5165 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5169 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5172 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5176 key_insert(mod
, ks
, kfid
, spawn_name
);
5178 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5182 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5187 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5188 keyfuncs
[kfid
].name
, spawn_name
);
5190 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5191 if (kfid
== kf_invalid
)
5194 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5195 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5198 if (kfid
== kf_invalid
) {
5199 warnx("error: setkeybinding: cannot find mod/key combination");
5200 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5204 key_insert(mod
, ks
, kfid
, spawn_name
);
5205 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5209 setconfbinding(char *selector
, char *value
, int flags
)
5211 enum keyfuncid kfid
;
5214 struct spawn_prog
*sp
;
5215 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5216 if (selector
== NULL
) {
5217 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5218 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5220 setkeybinding(mod
, ks
, kfid
, NULL
);
5225 /* search by key function name */
5226 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
5227 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5228 SWM_FUNCNAME_LEN
) == 0) {
5229 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5231 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5232 setkeybinding(mod
, ks
, kfid
, NULL
);
5238 /* search by custom spawn name */
5239 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5240 if (strcasecmp(selector
, sp
->name
) == 0) {
5241 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5243 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5244 setkeybinding(mod
, ks
, kf_spawn_custom
,
5251 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5258 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
5259 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, kf_flip_layout
, NULL
);
5260 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
5261 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
5262 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
5263 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
5264 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
5265 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
5266 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
5267 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
5268 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
5269 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
5270 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
5271 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
5272 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_custom
,"term");
5273 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
5274 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
5275 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
5276 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
5277 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
5278 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
5279 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
5280 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
5281 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
5282 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
5283 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
5284 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
5285 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
5286 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
5287 setkeybinding(MODKEY
, XK_F1
, kf_ws_11
, NULL
);
5288 setkeybinding(MODKEY
, XK_F2
, kf_ws_12
, NULL
);
5289 setkeybinding(MODKEY
, XK_F3
, kf_ws_13
, NULL
);
5290 setkeybinding(MODKEY
, XK_F4
, kf_ws_14
, NULL
);
5291 setkeybinding(MODKEY
, XK_F5
, kf_ws_15
, NULL
);
5292 setkeybinding(MODKEY
, XK_F6
, kf_ws_16
, NULL
);
5293 setkeybinding(MODKEY
, XK_F7
, kf_ws_17
, NULL
);
5294 setkeybinding(MODKEY
, XK_F8
, kf_ws_18
, NULL
);
5295 setkeybinding(MODKEY
, XK_F9
, kf_ws_19
, NULL
);
5296 setkeybinding(MODKEY
, XK_F10
, kf_ws_20
, NULL
);
5297 setkeybinding(MODKEY
, XK_F11
, kf_ws_21
, NULL
);
5298 setkeybinding(MODKEY
, XK_F12
, kf_ws_22
, NULL
);
5299 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
5300 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
5301 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
5302 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
5303 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
5304 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
5305 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
5306 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
5307 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
5308 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
5309 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
5310 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
5311 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
5312 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
5313 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
5314 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
5315 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
5316 setkeybinding(MODKEY
|ShiftMask
, XK_F1
, kf_mvws_11
, NULL
);
5317 setkeybinding(MODKEY
|ShiftMask
, XK_F2
, kf_mvws_12
, NULL
);
5318 setkeybinding(MODKEY
|ShiftMask
, XK_F3
, kf_mvws_13
, NULL
);
5319 setkeybinding(MODKEY
|ShiftMask
, XK_F4
, kf_mvws_14
, NULL
);
5320 setkeybinding(MODKEY
|ShiftMask
, XK_F5
, kf_mvws_15
, NULL
);
5321 setkeybinding(MODKEY
|ShiftMask
, XK_F6
, kf_mvws_16
, NULL
);
5322 setkeybinding(MODKEY
|ShiftMask
, XK_F7
, kf_mvws_17
, NULL
);
5323 setkeybinding(MODKEY
|ShiftMask
, XK_F8
, kf_mvws_18
, NULL
);
5324 setkeybinding(MODKEY
|ShiftMask
, XK_F9
, kf_mvws_19
, NULL
);
5325 setkeybinding(MODKEY
|ShiftMask
, XK_F10
, kf_mvws_20
, NULL
);
5326 setkeybinding(MODKEY
|ShiftMask
, XK_F11
, kf_mvws_21
, NULL
);
5327 setkeybinding(MODKEY
|ShiftMask
, XK_F12
, kf_mvws_22
, NULL
);
5328 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
5329 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
5330 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
5331 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
5332 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
5333 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
5334 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
5335 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
5336 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
5337 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
5338 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
5339 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
5340 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
5341 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
5342 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
5343 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
5344 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
5345 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
5346 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
5347 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
5348 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
5349 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
5350 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
5351 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
5352 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
5353 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
5355 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
5364 while (RB_EMPTY(&keys
) == 0) {
5365 kp
= RB_ROOT(&keys
);
5371 setkeymapping(char *selector
, char *value
, int flags
)
5373 char keymapping_file
[PATH_MAX
];
5374 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5375 if (value
[0] == '~')
5376 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5377 pwd
->pw_dir
, &value
[1]);
5379 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5381 /* load new key bindings; if it fails, revert to default bindings */
5382 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5386 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5391 updatenumlockmask(void)
5394 XModifierKeymap
*modmap
;
5396 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
5398 modmap
= XGetModifierMapping(display
);
5399 for (i
= 0; i
< 8; i
++)
5400 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
5401 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
5402 == XKeysymToKeycode(display
, XK_Num_Lock
))
5403 numlockmask
= (1 << i
);
5405 XFreeModifiermap(modmap
);
5414 unsigned int modifiers
[] =
5415 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
5418 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5419 updatenumlockmask();
5421 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
5422 for (k
= 0; k
< num_screens
; k
++) {
5423 if (TAILQ_EMPTY(&screens
[k
].rl
))
5425 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
5427 RB_FOREACH(kp
, key_tree
, &keys
) {
5428 if ((code
= XKeysymToKeycode(display
, kp
->keysym
)))
5429 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5430 XGrabKey(display
, code
,
5431 kp
->mod
| modifiers
[j
],
5432 screens
[k
].root
, True
,
5433 GrabModeAsync
, GrabModeAsync
);
5439 grabbuttons(struct ws_win
*win
, int focused
)
5442 unsigned int modifiers
[] =
5443 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5445 updatenumlockmask();
5446 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, win
->id
,
5447 XCB_BUTTON_MASK_ANY
);
5449 for (i
= 0; i
< LENGTH(buttons
); i
++)
5450 if (buttons
[i
].action
== client_click
)
5451 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5452 xcb_grab_button(conn
, False
, win
->id
,
5454 XCB_GRAB_MODE_ASYNC
,
5461 xcb_grab_button(conn
, False
, win
->id
, BUTTONMASK
,
5462 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_SYNC
,
5463 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_ANY
,
5464 XCB_BUTTON_MASK_ANY
);
5467 const char *quirkname
[] = {
5468 "NONE", /* config string for "no value" */
5477 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5478 #define SWM_Q_WS "\n|+ \t"
5480 parsequirks(char *qstr
, unsigned long *quirk
)
5490 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5492 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5493 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5494 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5495 DNPRINTF(SWM_D_QUIRK
,
5496 "parsequirks: %s\n", name
);
5501 *quirk
|= 1 << (i
-1);
5505 if (i
>= LENGTH(quirkname
)) {
5506 DNPRINTF(SWM_D_QUIRK
,
5507 "parsequirks: invalid quirk [%s]\n", name
);
5515 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5519 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5522 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5523 err(1, "quirk_insert: malloc");
5524 if ((qp
->class = strdup(class)) == NULL
)
5525 err(1, "quirk_insert: strdup");
5526 if ((qp
->name
= strdup(name
)) == NULL
)
5527 err(1, "quirk_insert: strdup");
5530 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5532 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5536 quirk_remove(struct quirk
*qp
)
5538 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5539 qp
->name
, qp
->quirk
);
5541 TAILQ_REMOVE(&quirks
, qp
, entry
);
5546 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5550 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5551 unsigned long quirk
)
5553 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5554 qp
->name
, qp
->quirk
);
5557 quirk_insert(class, name
, quirk
);
5559 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5563 setquirk(const char *class, const char *name
, unsigned long quirk
)
5567 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5570 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5571 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5575 quirk_replace(qp
, class, name
, quirk
);
5576 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5581 warnx("error: setquirk: cannot find class/name combination");
5585 quirk_insert(class, name
, quirk
);
5586 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5590 setconfquirk(char *selector
, char *value
, int flags
)
5592 char *cp
, *class, *name
;
5594 unsigned long quirks
;
5595 if (selector
== NULL
)
5597 if ((cp
= strchr(selector
, ':')) == NULL
)
5602 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5603 setquirk(class, name
, quirks
);
5610 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5611 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5612 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5613 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5614 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5615 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5616 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5617 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5618 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5619 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5620 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5621 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5622 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5625 /* conf file stuff */
5626 #define SWM_CONF_FILE "spectrwm.conf"
5627 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5631 SWM_S_BAR_AT_BOTTOM
,
5632 SWM_S_BAR_BORDER_WIDTH
,
5639 SWM_S_CLOCK_ENABLED
,
5642 SWM_S_CYCLE_VISIBLE
,
5644 SWM_S_DISABLE_BORDER
,
5646 SWM_S_FOCUS_CLOSE_WRAP
,
5647 SWM_S_FOCUS_DEFAULT
,
5653 SWM_S_STACK_ENABLED
,
5655 SWM_S_TITLE_CLASS_ENABLED
,
5656 SWM_S_TITLE_NAME_ENABLED
,
5657 SWM_S_URGENT_ENABLED
,
5658 SWM_S_VERBOSE_LAYOUT
,
5659 SWM_S_WINDOW_NAME_ENABLED
,
5660 SWM_S_WORKSPACE_LIMIT
5664 setconfvalue(char *selector
, char *value
, int flags
)
5670 case SWM_S_BAR_ACTION
:
5672 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5673 err(1, "setconfvalue: bar_action");
5675 case SWM_S_BAR_AT_BOTTOM
:
5676 bar_at_bottom
= atoi(value
);
5678 case SWM_S_BAR_BORDER_WIDTH
:
5679 bar_border_width
= atoi(value
);
5680 if (bar_border_width
< 0)
5681 bar_border_width
= 0;
5683 case SWM_S_BAR_DELAY
:
5684 bar_delay
= atoi(value
);
5686 case SWM_S_BAR_ENABLED
:
5687 bar_enabled
= atoi(value
);
5689 case SWM_S_BAR_FONT
:
5691 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5692 err(1, "setconfvalue: asprintf: failed to allocate "
5693 "memory for bar_fonts.");
5697 case SWM_S_BAR_FORMAT
:
5699 if ((bar_format
= strdup(value
)) == NULL
)
5700 err(1, "setconfvalue: bar_format");
5702 case SWM_S_BAR_JUSTIFY
:
5703 if (!strcmp(value
, "left"))
5704 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5705 else if (!strcmp(value
, "center"))
5706 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5707 else if (!strcmp(value
, "right"))
5708 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5710 errx(1, "invalid bar_justify");
5712 case SWM_S_BORDER_WIDTH
:
5713 border_width
= atoi(value
);
5714 if (border_width
< 0)
5717 case SWM_S_CLOCK_ENABLED
:
5718 clock_enabled
= atoi(value
);
5720 case SWM_S_CLOCK_FORMAT
:
5721 #ifndef SWM_DENY_CLOCK_FORMAT
5723 if ((clock_format
= strdup(value
)) == NULL
)
5724 err(1, "setconfvalue: clock_format");
5727 case SWM_S_CYCLE_EMPTY
:
5728 cycle_empty
= atoi(value
);
5730 case SWM_S_CYCLE_VISIBLE
:
5731 cycle_visible
= atoi(value
);
5733 case SWM_S_DIALOG_RATIO
:
5734 dialog_ratio
= atof(value
);
5735 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5738 case SWM_S_DISABLE_BORDER
:
5739 disable_border
= atoi(value
);
5741 case SWM_S_FOCUS_CLOSE
:
5742 if (!strcmp(value
, "first"))
5743 focus_close
= SWM_STACK_BOTTOM
;
5744 else if (!strcmp(value
, "last"))
5745 focus_close
= SWM_STACK_TOP
;
5746 else if (!strcmp(value
, "next"))
5747 focus_close
= SWM_STACK_ABOVE
;
5748 else if (!strcmp(value
, "previous"))
5749 focus_close
= SWM_STACK_BELOW
;
5751 errx(1, "focus_close");
5753 case SWM_S_FOCUS_CLOSE_WRAP
:
5754 focus_close_wrap
= atoi(value
);
5756 case SWM_S_FOCUS_DEFAULT
:
5757 if (!strcmp(value
, "last"))
5758 focus_default
= SWM_STACK_TOP
;
5759 else if (!strcmp(value
, "first"))
5760 focus_default
= SWM_STACK_BOTTOM
;
5762 errx(1, "focus_default");
5764 case SWM_S_FOCUS_MODE
:
5765 if (!strcmp(value
, "default"))
5766 focus_mode
= SWM_FOCUS_DEFAULT
;
5767 else if (!strcmp(value
, "follow_cursor"))
5768 focus_mode
= SWM_FOCUS_FOLLOW
;
5769 else if (!strcmp(value
, "synergy"))
5770 focus_mode
= SWM_FOCUS_SYNERGY
;
5772 errx(1, "focus_mode");
5774 case SWM_S_SPAWN_ORDER
:
5775 if (!strcmp(value
, "first"))
5776 spawn_position
= SWM_STACK_BOTTOM
;
5777 else if (!strcmp(value
, "last"))
5778 spawn_position
= SWM_STACK_TOP
;
5779 else if (!strcmp(value
, "next"))
5780 spawn_position
= SWM_STACK_ABOVE
;
5781 else if (!strcmp(value
, "previous"))
5782 spawn_position
= SWM_STACK_BELOW
;
5784 errx(1, "spawn_position");
5786 case SWM_S_SPAWN_TERM
:
5787 setconfspawn("term", value
, 0);
5788 setconfspawn("spawn_term", value
, 0);
5792 case SWM_S_SS_ENABLED
:
5793 ss_enabled
= atoi(value
);
5795 case SWM_S_STACK_ENABLED
:
5796 stack_enabled
= atoi(value
);
5798 case SWM_S_TERM_WIDTH
:
5799 term_width
= atoi(value
);
5803 case SWM_S_TITLE_CLASS_ENABLED
:
5804 title_class_enabled
= atoi(value
);
5806 case SWM_S_TITLE_NAME_ENABLED
:
5807 title_name_enabled
= atoi(value
);
5809 case SWM_S_URGENT_ENABLED
:
5810 urgent_enabled
= atoi(value
);
5812 case SWM_S_VERBOSE_LAYOUT
:
5813 verbose_layout
= atoi(value
);
5814 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5816 layouts
[i
].l_string
= fancy_stacker
;
5818 layouts
[i
].l_string
= plain_stacker
;
5821 case SWM_S_WINDOW_NAME_ENABLED
:
5822 window_name_enabled
= atoi(value
);
5824 case SWM_S_WORKSPACE_LIMIT
:
5825 workspace_limit
= atoi(value
);
5826 if (workspace_limit
> SWM_WS_MAX
)
5827 workspace_limit
= SWM_WS_MAX
;
5828 else if (workspace_limit
< 1)
5829 workspace_limit
= 1;
5838 setconfmodkey(char *selector
, char *value
, int flags
)
5840 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5841 update_modkey(Mod1Mask
);
5842 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5843 update_modkey(Mod2Mask
);
5844 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5845 update_modkey(Mod3Mask
);
5846 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5847 update_modkey(Mod4Mask
);
5854 setconfcolor(char *selector
, char *value
, int flags
)
5856 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5861 setconfregion(char *selector
, char *value
, int flags
)
5863 custom_region(value
);
5868 setautorun(char *selector
, char *value
, int flags
)
5878 if (getenv("SWM_STARTED"))
5882 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5883 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5885 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5886 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5889 * This is a little intricate
5891 * If the pid already exists we simply reuse it because it means it was
5892 * used before AND not claimed by manage_window. We get away with
5893 * altering it in the parent after INSERT because this can not be a race
5896 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5899 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5901 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5902 err(1, "setautorun: realloc");
5903 a
.argv
[argc
- 1] = ap
;
5906 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5907 err(1, "setautorun: realloc");
5908 a
.argv
[argc
] = NULL
;
5910 if ((pid
= fork()) == 0) {
5911 spawn(ws_id
, &a
, 1);
5920 p
= calloc(1, sizeof *p
);
5923 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
5933 setlayout(char *selector
, char *value
, int flags
)
5935 int ws_id
, i
, x
, mg
, ma
, si
, raise
, f
= 0;
5936 int st
= SWM_V_STACK
, num_screens
;
5938 struct workspace
*ws
;
5940 if (getenv("SWM_STARTED"))
5944 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
5945 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
5946 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5947 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5950 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5951 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
5953 if (!strcasecmp(s
, "vertical"))
5955 else if (!strcasecmp(s
, "vertical_flip")) {
5958 } else if (!strcasecmp(s
, "horizontal"))
5960 else if (!strcasecmp(s
, "horizontal_flip")) {
5963 } else if (!strcasecmp(s
, "fullscreen"))
5966 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5967 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5970 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
5971 for (i
= 0; i
< num_screens
; i
++) {
5972 ws
= (struct workspace
*)&screens
[i
].ws
;
5973 ws
[ws_id
].cur_layout
= &layouts
[st
];
5975 ws
[ws_id
].always_raise
= raise
;
5976 if (st
== SWM_MAX_STACK
)
5980 for (x
= 0; x
< abs(mg
); x
++) {
5981 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5982 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
5983 SWM_ARG_ID_MASTERSHRINK
);
5987 for (x
= 0; x
< abs(ma
); x
++) {
5988 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5989 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
5990 SWM_ARG_ID_MASTERDEL
);
5994 for (x
= 0; x
< abs(si
); x
++) {
5995 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5996 si
>= 0 ? SWM_ARG_ID_STACKINC
:
5997 SWM_ARG_ID_STACKDEC
);
6002 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6003 SWM_ARG_ID_FLIPLAYOUT
);
6011 /* config options */
6012 struct config_option
{
6014 int (*func
)(char*, char*, int);
6017 struct config_option configopt
[] = {
6018 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
6019 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
6020 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
6021 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
6022 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
6023 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
6024 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
6025 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
6026 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
6027 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
6028 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
6029 { "keyboard_mapping", setkeymapping
, 0 },
6030 { "bind", setconfbinding
, 0 },
6031 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
6032 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
6033 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
6034 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
6035 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
6036 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
6037 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
6038 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
6039 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
6040 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
6041 { "modkey", setconfmodkey
, 0 },
6042 { "program", setconfspawn
, 0 },
6043 { "quirk", setconfquirk
, 0 },
6044 { "region", setconfregion
, 0 },
6045 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
6046 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
6047 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
6048 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
6049 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
6050 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
6051 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6052 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6053 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6054 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6055 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6056 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6057 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6058 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6059 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6060 { "autorun", setautorun
, 0 },
6061 { "layout", setlayout
, 0 },
6066 conf_load(char *filename
, int keymapping
)
6069 char *line
, *cp
, *optsub
, *optval
;
6070 size_t linelen
, lineno
= 0;
6071 int wordlen
, i
, optind
;
6072 struct config_option
*opt
;
6074 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6076 if (filename
== NULL
) {
6077 warnx("conf_load: no filename");
6080 if ((config
= fopen(filename
, "r")) == NULL
) {
6081 warn("conf_load: fopen: %s", filename
);
6085 while (!feof(config
)) {
6086 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6089 err(1, "%s", filename
);
6094 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6095 if (cp
[0] == '\0') {
6100 /* get config option */
6101 wordlen
= strcspn(cp
, "=[ \t\n");
6103 warnx("%s: line %zd: no option found",
6108 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6109 opt
= &configopt
[i
];
6110 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6111 strlen(opt
->optname
) == wordlen
) {
6117 warnx("%s: line %zd: unknown option %.*s",
6118 filename
, lineno
, wordlen
, cp
);
6121 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
6122 warnx("%s: line %zd: invalid option %.*s",
6123 filename
, lineno
, wordlen
, cp
);
6127 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6128 /* get [selector] if any */
6132 wordlen
= strcspn(cp
, "]");
6135 warnx("%s: line %zd: syntax error",
6140 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6142 warnx("%s: line %zd: unable to allocate"
6143 "memory for selector", filename
,
6149 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6151 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6153 optval
= strdup(cp
);
6154 /* call function to deal with it all */
6155 if (configopt
[optind
].func(optsub
, optval
,
6156 configopt
[optind
].funcflags
) != 0)
6157 errx(1, "%s: line %zd: invalid data for %s",
6158 filename
, lineno
, configopt
[optind
].optname
);
6165 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6172 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6178 set_child_transient(struct ws_win
*win
, Window
*trans
)
6180 struct ws_win
*parent
, *w
;
6181 XWMHints
*wmh
= NULL
;
6182 struct swm_region
*r
;
6183 struct workspace
*ws
;
6185 parent
= find_window(win
->transient
);
6187 parent
->child_trans
= win
;
6189 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6190 " for 0x%lx trans 0x%lx\n", win
->id
, win
->transient
);
6192 if (win
->hints
== NULL
) {
6193 warnx("no hints for 0x%x", win
->id
);
6197 r
= root_to_region(win
->wa
.root
);
6199 /* parent doen't exist in our window list */
6200 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6204 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
6205 warnx("can't get hints for 0x%x", w
->id
);
6209 if (win
->hints
->window_group
!= wmh
->window_group
)
6212 w
->child_trans
= win
;
6213 win
->transient
= w
->id
;
6215 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
6216 "transient to 0x%x\n", win
->transient
);
6226 window_get_pid(xcb_window_t win
)
6231 xcb_get_property_cookie_t pc
;
6232 xcb_get_property_reply_t
*pr
;
6234 apid
= get_atom_from_string("_NET_WM_PID");
6235 if (apid
== XCB_ATOM_NONE
)
6238 pc
= xcb_get_property(conn
, False
, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
6239 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6242 if (pr
->type
!= XCB_ATOM_CARDINAL
)
6245 ret
= *(long *)xcb_get_property_value(pr
);
6251 apid
= get_atom_from_string("_SWM_PID");
6252 pc
= xcb_get_property(conn
, False
, win
, apid
, XCB_ATOM_STRING
,
6254 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6257 if (pr
->type
!= XCB_ATOM_STRING
)
6260 ret
= strtonum(xcb_get_property_value(pr
), 0, UINT_MAX
, &errstr
);
6267 manage_window(xcb_window_t id
)
6270 struct workspace
*ws
;
6271 struct ws_win
*win
, *ww
;
6272 int format
, i
, ws_idx
, n
, border_me
= 0;
6273 unsigned long nitems
, bytes
;
6274 Atom ws_idx_atom
= 0, type
;
6275 Atom
*prot
= NULL
, *pp
;
6276 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
6277 struct swm_region
*r
;
6282 if ((win
= find_window(id
)) != NULL
)
6283 return (win
); /* already being managed */
6285 /* see if we are on the unmanaged list */
6286 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6287 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
6288 "window: 0x%lx\n", win
->id
);
6289 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6291 set_child_transient(win
, &trans
);
6293 if (trans
&& (ww
= find_window(trans
)))
6294 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
6295 else if ((ww
= win
->ws
->focus
) &&
6296 spawn_position
== SWM_STACK_ABOVE
)
6297 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
,
6299 else if (ww
&& spawn_position
== SWM_STACK_BELOW
)
6300 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6301 else switch (spawn_position
) {
6304 case SWM_STACK_ABOVE
:
6305 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6307 case SWM_STACK_BOTTOM
:
6308 case SWM_STACK_BELOW
:
6309 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6312 ewmh_update_actions(win
);
6316 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6317 err(1, "manage_window: calloc: failed to allocate memory for "
6323 /* see if we need to override the workspace */
6324 p
= find_pid(window_get_pid(id
));
6326 /* Get all the window data in one shot */
6327 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
6329 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
6330 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
6332 XGetWindowAttributes(display
, id
, &win
->wa
);
6333 XGetWMNormalHints(display
, id
, &win
->sh
, &win
->sh_mask
);
6334 win
->hints
= XGetWMHints(display
, id
);
6335 XGetTransientForHint(display
, id
, &trans
);
6337 win
->transient
= trans
;
6338 set_child_transient(win
, &trans
);
6339 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, "
6340 "transient: 0x%lx\n", win
->id
, win
->transient
);
6343 /* get supported protocols */
6344 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
6345 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
6346 if (*pp
== takefocus
)
6347 win
->take_focus
= 1;
6349 win
->can_delete
= 1;
6355 win
->iconic
= get_iconic(win
);
6358 * Figure out where to put the window. If it was previously assigned to
6359 * a workspace (either by spawn() or manually moving), and isn't
6360 * transient, * put it in the same workspace
6362 r
= root_to_region(win
->wa
.root
);
6364 ws
= &r
->s
->ws
[p
->ws
];
6365 TAILQ_REMOVE(&pidlist
, p
, entry
);
6368 } else if (prop
&& win
->transient
== 0) {
6369 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
6370 ws_idx
= strtonum((const char *)prop
, 0, workspace_limit
- 1,
6373 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
6376 ws
= &r
->s
->ws
[ws_idx
];
6379 /* this should launch transients in the same ws as parent */
6381 if ((ww
= find_window(trans
)) != NULL
)
6387 warnx("manage_window: fix this "
6393 /* set up the window layout */
6396 win
->s
= r
->s
; /* this never changes */
6397 if (trans
&& (ww
= find_window(trans
)))
6398 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
6399 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
6400 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
6402 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
6403 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6404 else switch (spawn_position
) {
6407 case SWM_STACK_ABOVE
:
6408 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6410 case SWM_STACK_BOTTOM
:
6411 case SWM_STACK_BELOW
:
6412 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6415 /* ignore window border if there is one. */
6416 WIDTH(win
) = win
->wa
.width
;
6417 HEIGHT(win
) = win
->wa
.height
;
6418 X(win
) = win
->wa
.x
+ win
->wa
.border_width
;
6419 Y(win
) = win
->wa
.y
+ win
->wa
.border_width
;
6421 win
->g_floatvalid
= 0;
6422 win
->floatmaxed
= 0;
6423 win
->ewmh_flags
= 0;
6425 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, (x,y) w x h: "
6426 "(%d,%d) %d x %d, ws: %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
6427 HEIGHT(win
), ws
->idx
);
6429 constrain_window(win
, r
, 0);
6431 /* Set window properties so we can remember this after reincarnation */
6432 if (ws_idx_atom
&& prop
== NULL
&&
6433 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
6435 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6437 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
6438 PropModeReplace
, ws_idx_str
, strlen((char *)ws_idx_str
));
6443 ewmh_autoquirk(win
);
6445 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
6446 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6447 win
->ch
.res_class
, win
->ch
.res_name
);
6449 /* java is retarded so treat it special */
6450 if (strstr(win
->ch
.res_name
, "sun-awt")) {
6455 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6456 if (!strcmp(win
->ch
.res_class
, qp
->class) &&
6457 !strcmp(win
->ch
.res_name
, qp
->name
)) {
6458 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
6459 "class: %s, name: %s\n", win
->ch
.res_class
,
6461 if (qp
->quirk
& SWM_Q_FLOAT
) {
6465 win
->quirks
= qp
->quirk
;
6470 /* alter window position if quirky */
6471 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6472 win
->manual
= 1; /* don't center the quirky windows */
6473 if (bar_enabled
&& Y(win
) < bar_height
)
6474 Y(win
) = bar_height
;
6475 if (WIDTH(win
) + X(win
) > WIDTH(r
))
6476 X(win
) = WIDTH(r
) - WIDTH(win
) - 2;
6480 /* Reset font sizes (the bruteforce way; no default keybinding). */
6481 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6482 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6483 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
6484 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6485 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
6488 ewmh_get_win_state(win
);
6489 ewmh_update_actions(win
);
6490 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
6495 X(win
) -= border_width
;
6496 Y(win
) -= border_width
;
6500 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
6501 PropertyChangeMask
| StructureNotifyMask
);
6503 /* floaters need to be mapped if they are in the current workspace */
6504 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
6505 map_window_raised(win
->id
);
6511 free_window(struct ws_win
*win
)
6513 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%lx\n", win
->id
);
6518 /* needed for restart wm */
6519 set_win_state(win
, XCB_WM_STATE_WITHDRAWN
);
6521 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6523 if (win
->ch
.res_class
)
6524 XFree(win
->ch
.res_class
);
6525 if (win
->ch
.res_name
)
6526 XFree(win
->ch
.res_name
);
6531 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6537 unmanage_window(struct ws_win
*win
)
6539 struct ws_win
*parent
;
6544 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%lx\n", win
->id
);
6546 if (win
->transient
) {
6547 parent
= find_window(win
->transient
);
6549 parent
->child_trans
= NULL
;
6552 /* focus on root just in case */
6553 XSetInputFocus(display
, PointerRoot
, PointerRoot
, CurrentTime
);
6562 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
6563 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6567 focus_magic(struct ws_win
*win
)
6569 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%lx\n", WINID(win
));
6572 /* if there are no windows clear the status-bar */
6577 if (win
->child_trans
) {
6578 /* win = parent & has a transient so focus on that */
6580 focus_win(win
->child_trans
);
6581 if (win
->child_trans
->take_focus
)
6582 client_msg(win
, takefocus
);
6584 /* make sure transient hasn't disappeared */
6585 if (validate_win(win
->child_trans
) == 0) {
6586 focus_win(win
->child_trans
);
6587 if (win
->child_trans
->take_focus
)
6588 client_msg(win
->child_trans
, takefocus
);
6590 win
->child_trans
= NULL
;
6592 if (win
->take_focus
)
6593 client_msg(win
, takefocus
);
6599 if (win
->take_focus
)
6600 client_msg(win
, takefocus
);
6607 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%lx\n", e
->xexpose
.window
);
6614 XKeyEvent
*ev
= &e
->xkey
;
6616 struct swm_region
*r
;
6618 keysym
= XkbKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0, 0);
6619 if ((kp
= key_lookup(CLEANMASK(ev
->state
), keysym
)) == NULL
)
6621 if (keyfuncs
[kp
->funcid
].func
== NULL
)
6624 r
= root_to_region(ev
->root
);
6625 if (kp
->funcid
== kf_spawn_custom
)
6626 spawn_custom(r
, &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
6628 keyfuncs
[kp
->funcid
].func(r
, &(keyfuncs
[kp
->funcid
].args
));
6632 buttonpress(XEvent
*e
)
6636 XButtonPressedEvent
*ev
= &e
->xbutton
;
6638 if ((win
= find_window(ev
->window
)) == NULL
)
6642 action
= client_click
;
6644 for (i
= 0; i
< LENGTH(buttons
); i
++)
6645 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6646 buttons
[i
].button
== ev
->button
&&
6647 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
6648 buttons
[i
].func(win
, &buttons
[i
].args
);
6652 configurerequest(XEvent
*e
)
6654 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
6659 if ((win
= find_window(ev
->window
)) == NULL
)
6660 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6664 bzero(&wc
, sizeof wc
);
6667 wc
.width
= ev
->width
;
6668 wc
.height
= ev
->height
;
6669 wc
.border_width
= ev
->border_width
;
6670 wc
.sibling
= ev
->above
;
6671 wc
.stack_mode
= ev
->detail
;
6673 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: 0x%lx, "
6674 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", ev
->window
,
6675 YESNO(new), wc
.x
, wc
.y
, wc
.width
, wc
.height
);
6677 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
6678 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
6679 !(win
->sh_mask
& EWMH_F_FULLSCREEN
)) {
6680 win
->g_float
.x
= ev
->x
- X(win
->ws
->r
);
6681 win
->g_float
.y
= ev
->y
- Y(win
->ws
->r
);
6682 win
->g_float
.w
= ev
->width
;
6683 win
->g_float
.h
= ev
->height
;
6684 win
->g_floatvalid
= 1;
6686 if (win
->floating
) {
6687 win
->g
= win
->g_float
;
6688 win
->g
.x
+= X(win
->ws
->r
);
6689 win
->g
.y
+= Y(win
->ws
->r
);
6692 config_win(win
, ev
);
6695 config_win(win
, ev
);
6700 configurenotify(XEvent
*e
)
6704 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%lx\n",
6705 e
->xconfigure
.window
);
6707 win
= find_window(e
->xconfigure
.window
);
6709 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &win
->sh_mask
);
6713 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6714 drain_enter_notify();
6719 destroynotify(XEvent
*e
)
6722 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
6724 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%lx\n", ev
->window
);
6726 if ((win
= find_window(ev
->window
)) == NULL
) {
6727 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6733 /* make sure we focus on something */
6736 unmanage_window(win
);
6738 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6739 drain_enter_notify();
6744 enternotify(XEvent
*e
)
6746 XCrossingEvent
*ev
= &e
->xcrossing
;
6751 Window focus_return
;
6752 int revert_to_return
;
6754 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%lx, mode: %d, detail: "
6755 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6756 "state: %d\n", ev
->window
, ev
->mode
, ev
->detail
, ev
->root
,
6757 ev
->subwindow
, YESNO(ev
->same_screen
), YESNO(ev
->focus
), ev
->state
);
6759 if (ev
->mode
!= NotifyNormal
) {
6760 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6765 switch (focus_mode
) {
6766 case SWM_FOCUS_DEFAULT
:
6768 case SWM_FOCUS_FOLLOW
:
6770 case SWM_FOCUS_SYNERGY
:
6773 * all these checks need to be in this order because the
6774 * XCheckTypedWindowEvent relies on weeding out the previous events
6776 * making this code an option would enable a follow mouse for focus
6781 * state is set when we are switching workspaces and focus is set when
6782 * the window or a subwindow already has focus (occurs during restart).
6784 * Only honor the focus flag if last_focus_event is not FocusOut,
6785 * this allows spectrwm to continue to control focus when another
6786 * program is also playing with it.
6788 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6789 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6794 * happens when a window is created or destroyed and the border
6795 * crosses the mouse pointer and when switching ws
6797 * we need the subwindow test to see if we came from root in order
6798 * to give focus to floaters
6800 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6801 ev
->subwindow
== 0) {
6802 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6806 /* this window already has focus */
6807 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6808 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6812 /* this window is being deleted or moved to another ws */
6813 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6815 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6816 XPutBackEvent(display
, &cne
);
6820 if ((win
= find_window(ev
->window
)) == NULL
) {
6821 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6826 * In fullstack kill all enters unless they come from a different ws
6827 * (i.e. another region) or focus has been grabbed externally.
6829 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6830 last_focus_event
!= FocusOut
) {
6831 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6832 if ((w
= find_window(focus_return
)) == NULL
||
6834 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6842 if ((win
= find_window(ev
->window
)) == NULL
) {
6843 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6848 * if we have more enternotifies let them handle it in due time
6850 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
6851 DNPRINTF(SWM_D_EVENT
,
6852 "ignoring enternotify: got more enternotify\n");
6853 XPutBackEvent(display
, &cne
);
6860 /* lets us use one switch statement for arbitrary mode/detail combinations */
6861 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6864 focusevent(XEvent
*e
)
6868 u_int32_t mode_detail
;
6869 XFocusChangeEvent
*ev
= &e
->xfocus
;
6871 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: 0x%lx mode: %d "
6872 "detail: %d\n", ev
->type
== FocusIn
? "entering" : "leaving",
6873 ev
->window
, ev
->mode
, ev
->detail
);
6875 if (last_focus_event
== ev
->type
) {
6876 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
6880 last_focus_event
= ev
->type
;
6881 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
6883 switch (mode_detail
) {
6884 /* synergy client focus operations */
6885 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
6886 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
6888 /* synergy server focus operations */
6889 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
6891 /* Entering applications like rdesktop that mangle the pointer */
6892 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
6894 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
6895 XSetWindowBorder(display
, win
->id
,
6896 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
6897 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
6900 warnx("ignoring focusevent");
6901 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
6908 mapnotify(XEvent
*e
)
6911 XMapEvent
*ev
= &e
->xmap
;
6913 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%lx\n", ev
->window
);
6915 win
= manage_window(ev
->window
);
6917 set_win_state(win
, XCB_WM_STATE_NORMAL
);
6920 * focus_win can only set input focus on a mapped window.
6921 * make sure the window really has focus since it is just being mapped.
6923 if (win
->ws
->focus
== win
)
6928 mappingnotify(XEvent
*e
)
6930 XMappingEvent
*ev
= &e
->xmapping
;
6932 XRefreshKeyboardMapping(ev
);
6933 if (ev
->request
== MappingKeyboard
)
6938 maprequest(XEvent
*e
)
6941 struct swm_region
*r
;
6942 XWindowAttributes wa
;
6943 XMapRequestEvent
*ev
= &e
->xmaprequest
;
6945 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%lx\n",
6946 e
->xmaprequest
.window
);
6948 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
6950 if (wa
.override_redirect
)
6953 win
= manage_window(e
->xmaprequest
.window
);
6955 return; /* can't happen */
6959 /* make new win focused */
6960 r
= root_to_region(win
->wa
.root
);
6961 if (win
->ws
== r
->ws
)
6966 propertynotify(XEvent
*e
)
6969 XPropertyEvent
*ev
= &e
->xproperty
;
6972 name
= XGetAtomName(display
, ev
->atom
);
6973 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%lx, atom: %s\n",
6978 win
= find_window(ev
->window
);
6982 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
6983 update_iconic(win
, 0);
6984 map_window_raised(win
->id
);
6992 case XA_WM_NORMAL_HINTS
:
6994 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
6995 warnx("normal hints: flag 0x%x", win
->sh
.flags
);
6996 if (win
->sh
.flags
& PMinSize
) {
6997 WIDTH(win
) = win
->sh
.min_width
;
6998 HEIGHT(win
) = win
->sh
.min_height
;
6999 warnx("min %d %d", WIDTH(win
), HEIGHT(win
));
7001 XMoveResizeWindow(display
, win
->id
,
7002 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
));
7014 unmapnotify(XEvent
*e
)
7018 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%lx\n", e
->xunmap
.window
);
7020 /* determine if we need to help unmanage this window */
7021 win
= find_window(e
->xunmap
.window
);
7025 if (getstate(e
->xunmap
.window
) == XCB_WM_STATE_NORMAL
) {
7026 unmanage_window(win
);
7029 /* giant hack for apps that don't destroy transient windows */
7030 /* eat a bunch of events to prevent remanaging the window */
7032 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
7033 EnterWindowMask
, &cne
))
7035 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
7036 StructureNotifyMask
, &cne
))
7038 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
7039 SubstructureNotifyMask
, &cne
))
7041 /* resend unmap because we ated it */
7042 XUnmapWindow(display
, e
->xunmap
.window
);
7045 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7046 drain_enter_notify();
7050 visibilitynotify(XEvent
*e
)
7053 struct swm_region
*r
;
7055 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%lx\n",
7056 e
->xvisibility
.window
);
7058 if (e
->xvisibility
.state
== VisibilityUnobscured
) {
7059 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7060 for (i
= 0; i
< num_screens
; i
++)
7061 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7062 if (e
->xvisibility
.window
== WINID(r
->bar
))
7068 clientmessage(XEvent
*e
)
7070 XClientMessageEvent
*ev
;
7075 win
= find_window(ev
->window
);
7077 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7078 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
7079 "unmanaged window.\n");
7080 e
->xmaprequest
.window
= ev
->window
;
7086 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx, type: %ld\n",
7087 ev
->window
, ev
->message_type
);
7089 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7090 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7093 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7094 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7095 if (win
->can_delete
)
7096 client_msg(win
, adelete
);
7098 xcb_kill_client(conn
, win
->id
);
7100 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7101 DNPRINTF(SWM_D_EVENT
,
7102 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7103 if (win
->floating
) {
7104 if (ev
->data
.l
[0] & (1<<8)) /* x */
7105 X(win
) = ev
->data
.l
[1];
7106 if (ev
->data
.l
[0] & (1<<9)) /* y */
7107 Y(win
) = ev
->data
.l
[2];
7108 if (ev
->data
.l
[0] & (1<<10)) /* width */
7109 WIDTH(win
) = ev
->data
.l
[3];
7110 if (ev
->data
.l
[0] & (1<<11)) /* height */
7111 HEIGHT(win
) = ev
->data
.l
[4];
7116 /* TODO: Change stack sizes */
7117 /* notify no change was made. */
7118 config_win(win
, NULL
);
7121 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
7122 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
7123 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
7125 ewmh_update_win_state(win
, ev
->data
.l
[2],
7133 xerror_start(Display
*d
, XErrorEvent
*ee
)
7140 xerror(Display
*d
, XErrorEvent
*ee
)
7142 /* warnx("error: %p %p", display, ee); */
7150 xerrorxlib
= XSetErrorHandler(xerror_start
);
7152 /* this causes an error if some other window manager is running */
7153 XSelectInput(display
, DefaultRootWindow(display
),
7154 SubstructureRedirectMask
);
7155 XSync(display
, False
);
7159 XSetErrorHandler(xerror
);
7160 XSync(display
, False
);
7165 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
7167 struct swm_region
*r
, *n
;
7168 struct workspace
*ws
= NULL
;
7171 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
7172 s
->idx
, w
, h
, x
, y
);
7174 /* remove any conflicting regions */
7175 n
= TAILQ_FIRST(&s
->rl
);
7178 n
= TAILQ_NEXT(r
, entry
);
7179 if (X(r
) < (x
+ w
) &&
7180 (X(r
) + WIDTH(r
)) > x
&&
7182 (Y(r
) + HEIGHT(r
)) > y
) {
7183 if (r
->ws
->r
!= NULL
)
7184 r
->ws
->old_r
= r
->ws
->r
;
7187 TAILQ_REMOVE(&s
->rl
, r
, entry
);
7188 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
7192 /* search old regions for one to reuse */
7194 /* size + location match */
7195 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7196 if (X(r
) == x
&& Y(r
) == y
&&
7197 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7201 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7202 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7206 TAILQ_REMOVE(&s
->orl
, r
, entry
);
7207 /* try to use old region's workspace */
7208 if (r
->ws
->r
== NULL
)
7211 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
7212 err(1, "new_region: calloc: failed to allocate memory "
7215 /* if we don't have a workspace already, find one */
7217 for (i
= 0; i
< workspace_limit
; i
++)
7218 if (s
->ws
[i
].r
== NULL
) {
7225 errx(1, "new_region: no free workspaces");
7236 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
7242 #ifdef SWM_XRR_HAS_CRTC
7245 #endif /* SWM_XRR_HAS_CRTC */
7246 struct swm_region
*r
;
7248 xcb_randr_get_screen_resources_cookie_t src
;
7249 xcb_randr_get_screen_resources_reply_t
*srr
;
7250 xcb_randr_get_crtc_info_cookie_t cic
;
7251 xcb_randr_get_crtc_info_reply_t
*cir
;
7252 xcb_randr_crtc_t
*crtc
;
7254 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7255 if (i
>= num_screens
)
7256 errx(1, "scan_xrandr: invalid screen");
7258 /* remove any old regions */
7259 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
7260 r
->ws
->old_r
= r
->ws
->r
= NULL
;
7262 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
7263 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
7267 /* map virtual screens onto physical screens */
7268 #ifdef SWM_XRR_HAS_CRTC
7269 if (xrandr_support
) {
7270 src
= xcb_randr_get_screen_resources(conn
, screens
[i
].root
);
7271 srr
= xcb_randr_get_screen_resources_reply(conn
, src
, NULL
);
7273 new_region(&screens
[i
], 0, 0,
7274 DisplayWidth(display
, i
),
7275 DisplayHeight(display
, i
));
7277 ncrtc
= srr
->num_crtcs
;
7278 for (c
= 0; c
< ncrtc
; c
++) {
7279 crtc
= xcb_randr_get_screen_resources_crtcs(srr
);
7280 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
7282 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
7283 if (cir
&& cir
->num_outputs
== 0)
7286 if (cir
== NULL
|| cir
->mode
== 0)
7287 new_region(&screens
[i
], 0, 0,
7288 DisplayWidth(display
, i
),
7289 DisplayHeight(display
, i
));
7291 new_region(&screens
[i
],
7292 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
7299 #endif /* SWM_XRR_HAS_CRTC */
7301 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
7302 DisplayHeight(display
, i
));
7307 screenchange(XEvent
*e
)
7309 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
7310 struct swm_region
*r
;
7313 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%lx\n", xe
->root
);
7315 if (!XRRUpdateConfiguration(e
))
7318 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7319 /* silly event doesn't include the screen index */
7320 for (i
= 0; i
< num_screens
; i
++)
7321 if (screens
[i
].root
== xe
->root
)
7323 if (i
>= num_screens
)
7324 errx(1, "screenchange: screen not found");
7326 /* brute force for now, just re-enumerate the regions */
7329 /* add bars to all regions */
7330 for (i
= 0; i
< num_screens
; i
++)
7331 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7334 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7335 drain_enter_notify();
7341 xcb_window_t
*wins
= NULL
;
7343 int i
, j
, num_screens
;
7344 uint16_t state
, manage
;
7346 xcb_query_tree_cookie_t qtc
;
7347 xcb_query_tree_reply_t
*qtr
;
7348 xcb_get_window_attributes_cookie_t c
;
7349 xcb_get_window_attributes_reply_t
*r
;
7350 xcb_get_property_cookie_t pc
;
7352 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7353 for (i
= 0; i
< num_screens
; i
++) {
7354 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
7355 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
7358 wins
= xcb_query_tree_children(qtr
);
7359 no
= xcb_query_tree_children_length(qtr
);
7360 /* attach windows to a region */
7361 /* normal windows */
7362 for (j
= 0; j
< no
; j
++) {
7363 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7364 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7367 if (r
->override_redirect
) {
7372 pc
= xcb_get_wm_transient_for(conn
, wins
[j
]);
7373 if (xcb_get_wm_transient_for_reply(conn
, pc
, &wins
[j
],
7379 state
= getstate(wins
[j
]);
7380 manage
= state
== XCB_WM_STATE_ICONIC
;
7381 if (r
->map_state
== XCB_MAP_STATE_VIEWABLE
|| manage
)
7382 manage_window(wins
[j
]);
7385 /* transient windows */
7386 for (j
= 0; j
< no
; j
++) {
7387 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7388 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7391 if (r
->override_redirect
) {
7397 state
= getstate(wins
[j
]);
7398 manage
= state
== XCB_WM_STATE_ICONIC
;
7399 pc
= xcb_get_wm_transient_for(conn
, wins
[j
]);
7400 if (xcb_get_wm_transient_for_reply(conn
, pc
, &wins
[j
],
7402 manage_window(wins
[j
]);
7411 int i
, j
, k
, num_screens
;
7413 struct workspace
*ws
;
7416 xcb_randr_query_version_cookie_t c
;
7417 xcb_randr_query_version_reply_t
*r
;
7419 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7420 if ((screens
= calloc(num_screens
,
7421 sizeof(struct swm_screen
))) == NULL
)
7422 err(1, "setup_screens: calloc: failed to allocate memory for "
7425 /* initial Xrandr setup */
7426 xrandr_support
= XRRQueryExtension(display
,
7427 &xrandr_eventbase
, &errorbase
);
7428 if (xrandr_support
) {
7429 c
= xcb_randr_query_version(conn
, True
, False
);
7430 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
7432 if (r
->major_version
< 1)
7439 /* map physical screens */
7440 for (i
= 0; i
< num_screens
; i
++) {
7441 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
7443 TAILQ_INIT(&screens
[i
].rl
);
7444 TAILQ_INIT(&screens
[i
].orl
);
7445 screens
[i
].root
= RootWindow(display
, i
);
7447 /* set default colors */
7448 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
7449 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
7450 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
7451 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
7452 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
7454 /* create graphics context on screen */
7455 gcv
.graphics_exposures
= 0;
7456 screens
[i
].bar_gc
= XCreateGC(display
, screens
[i
].root
,
7457 GCGraphicsExposures
, &gcv
);
7459 /* set default cursor */
7460 XDefineCursor(display
, screens
[i
].root
,
7461 XCreateFontCursor(display
, XC_left_ptr
));
7463 /* init all workspaces */
7464 /* XXX these should be dynamically allocated too */
7465 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
7466 ws
= &screens
[i
].ws
[j
];
7472 TAILQ_INIT(&ws
->winlist
);
7473 TAILQ_INIT(&ws
->unmanagedlist
);
7475 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
7476 if (layouts
[k
].l_config
!= NULL
)
7477 layouts
[k
].l_config(ws
,
7478 SWM_ARG_ID_STACKINIT
);
7479 ws
->cur_layout
= &layouts
[0];
7480 ws
->cur_layout
->l_string(ws
);
7486 xcb_randr_select_input(conn
, screens
[i
].root
,
7487 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
7494 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
7495 err(1, "setup_globals: strdup: failed to allocate memory.");
7497 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
7498 err(1, "setup_globals: strdup: failed to allocate memory.");
7505 xcb_atom_t netwmcheck
, netwmname
, utf8_string
;
7506 xcb_window_t root
, win
;
7508 /* work around sun jdk bugs, code from wmname */
7509 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7510 netwmname
= get_atom_from_string("_NET_WM_NAME");
7511 utf8_string
= get_atom_from_string("UTF8_STRING");
7513 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7514 for (i
= 0; i
< num_screens
; i
++) {
7515 root
= screens
[i
].root
;
7516 win
= XCreateSimpleWindow(display
,root
, 0, 0, 1, 1, 0,
7517 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
,
7518 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
);
7520 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
7521 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7522 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7523 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7524 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7525 netwmcheck
, utf8_string
, 8, strlen("LG3D"), "LG3D");
7530 main(int argc
, char *argv
[])
7532 struct swm_region
*r
, *rr
;
7533 struct ws_win
*winfocus
= NULL
;
7536 char conf
[PATH_MAX
], *cfile
= NULL
;
7539 int xfd
, i
, num_screens
;
7541 struct sigaction sact
;
7542 xcb_generic_event_t
*evt
;
7545 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
7546 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
7548 warnx("no locale support");
7550 if (!X_HAVE_UTF8_STRING
)
7551 warnx("no UTF-8 support");
7553 if (!(display
= XOpenDisplay(0)))
7554 errx(1, "can not open display");
7556 if (!(conn
= XGetXCBConnection(display
)))
7557 errx(1, "can not get XCB connection");
7560 errx(1, "other wm running");
7562 /* handle some signals */
7563 bzero(&sact
, sizeof(sact
));
7564 sigemptyset(&sact
.sa_mask
);
7566 sact
.sa_handler
= sighdlr
;
7567 sigaction(SIGINT
, &sact
, NULL
);
7568 sigaction(SIGQUIT
, &sact
, NULL
);
7569 sigaction(SIGTERM
, &sact
, NULL
);
7570 sigaction(SIGHUP
, &sact
, NULL
);
7572 sact
.sa_handler
= sighdlr
;
7573 sact
.sa_flags
= SA_NOCLDSTOP
;
7574 sigaction(SIGCHLD
, &sact
, NULL
);
7576 astate
= get_atom_from_string("WM_STATE");
7577 aprot
= get_atom_from_string("WM_PROTOCOLS");
7578 adelete
= get_atom_from_string("WM_DELETE_WINDOW");
7579 takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
7580 a_wmname
= get_atom_from_string("WM_NAME");
7581 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
7582 a_utf8_string
= get_atom_from_string("UTF8_STRING");
7583 a_string
= get_atom_from_string("STRING");
7584 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
7586 /* look for local and global conf file */
7587 pwd
= getpwuid(getuid());
7589 errx(1, "invalid user: %d", getuid());
7598 for (i
= 0; ; i
++) {
7603 snprintf(conf
, sizeof conf
, "%s/.%s",
7604 pwd
->pw_dir
, SWM_CONF_FILE
);
7608 snprintf(conf
, sizeof conf
, "/etc/%s",
7613 snprintf(conf
, sizeof conf
, "%s/.%s",
7614 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
7618 snprintf(conf
, sizeof conf
, "/etc/%s",
7625 if (strlen(conf
) && stat(conf
, &sb
) != -1)
7626 if (S_ISREG(sb
.st_mode
)) {
7633 /* load conf (if any) */
7635 conf_load(cfile
, SWM_CONF_DEFAULT
);
7638 /* set some values to work around bad programs */
7640 /* grab existing windows (before we build the bars) */
7643 if (getenv("SWM_STARTED") == NULL
)
7644 setenv("SWM_STARTED", "YES", 1);
7646 /* setup all bars */
7647 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7648 for (i
= 0; i
< num_screens
; i
++)
7649 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
7650 if (winfocus
== NULL
)
7651 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
7659 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7660 drain_enter_notify();
7662 xfd
= xcb_get_file_descriptor(conn
);
7664 while ((evt
= xcb_poll_for_event(conn
)) == 0) {
7665 XNextEvent(display
, &e
);
7668 if (e
.type
< LASTEvent
) {
7669 DNPRINTF(SWM_D_EVENTQ
,"XEvent: handled: %s, "
7670 "window: 0x%lx, type: %s (%d), %d remaining"
7671 "\n", YESNO(handler
[e
.type
]),
7672 e
.xany
.window
, geteventname(&e
),
7673 e
.type
, QLength(display
));
7675 if (handler
[e
.type
])
7676 handler
[e
.type
](&e
);
7678 DNPRINTF(SWM_D_EVENTQ
, "XRandr Event: window: "
7679 "0x%lx, type: %s (%d)\n", e
.xany
.window
,
7680 xrandr_geteventname(&e
), e
.type
);
7682 switch (e
.type
- xrandr_eventbase
) {
7683 case RRScreenChangeNotify
:
7692 /* if we are being restarted go focus on first window */
7694 rr
= winfocus
->ws
->r
;
7696 /* not a visible window */
7700 /* move pointer to first screen if multi screen */
7701 if (num_screens
> 1 || outputs
> 1)
7702 XWarpPointer(display
, None
, rr
->s
[0].root
,
7704 Y(rr
) + (bar_enabled
? bar_height
: 0));
7706 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7716 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7718 DNPRINTF(SWM_D_MISC
, "select failed");
7719 if (restart_wm
== 1)
7720 restart(NULL
, NULL
);
7721 if (search_resp
== 1)
7734 for (i
= 0; i
< num_screens
; ++i
)
7735 if (screens
[i
].bar_gc
!= NULL
)
7736 XFreeGC(display
, screens
[i
].bar_gc
);
7737 XFreeFontSet(display
, bar_fs
);
7738 xcb_disconnect(conn
);
7739 XCloseDisplay(display
);