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 <X11/Xproto.h>
95 #include <X11/Xutil.h>
96 #include <X11/extensions/Xrandr.h>
97 #include <X11/extensions/XTest.h>
105 #ifdef SPECTRWM_BUILDSTR
106 static const char *buildstr
= SPECTRWM_BUILDSTR
;
108 static const char *buildstr
= SPECTRWM_VERSION
;
112 # error XRandR versions less than 1.0 are not supported
117 #define SWM_XRR_HAS_CRTC
121 /*#define SWM_DEBUG*/
123 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
124 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
125 #define SWM_D_MISC 0x0001
126 #define SWM_D_EVENT 0x0002
127 #define SWM_D_WS 0x0004
128 #define SWM_D_FOCUS 0x0008
129 #define SWM_D_MOVE 0x0010
130 #define SWM_D_STACK 0x0020
131 #define SWM_D_MOUSE 0x0040
132 #define SWM_D_PROP 0x0080
133 #define SWM_D_CLASS 0x0100
134 #define SWM_D_KEY 0x0200
135 #define SWM_D_QUIRK 0x0400
136 #define SWM_D_SPAWN 0x0800
137 #define SWM_D_EVENTQ 0x1000
138 #define SWM_D_CONF 0x2000
139 #define SWM_D_BAR 0x4000
141 u_int32_t swm_debug
= 0
159 #define DPRINTF(x...)
160 #define DNPRINTF(n,x...)
163 #define LENGTH(x) (sizeof x / sizeof x[0])
164 #define MODKEY Mod1Mask
165 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
166 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
167 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
168 #define SWM_PROPLEN (16)
169 #define SWM_FUNCNAME_LEN (32)
170 #define SWM_KEYS_LEN (255)
171 #define SWM_QUIRK_LEN (64)
172 #define X(r) (r)->g.x
173 #define Y(r) (r)->g.y
174 #define WIDTH(r) (r)->g.w
175 #define HEIGHT(r) (r)->g.h
176 #define BORDER(w) (w->bordered ? border_width : 0)
177 #define MAX_X(r) ((r)->g.x + (r)->g.w)
178 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
179 #define SH_MIN(w) (w)->sh_mask & PMinSize
180 #define SH_MIN_W(w) (w)->sh.min_width
181 #define SH_MIN_H(w) (w)->sh.min_height
182 #define SH_MAX(w) (w)->sh_mask & PMaxSize
183 #define SH_MAX_W(w) (w)->sh.max_width
184 #define SH_MAX_H(w) (w)->sh.max_height
185 #define SH_INC(w) (w)->sh_mask & PResizeInc
186 #define SH_INC_W(w) (w)->sh.width_inc
187 #define SH_INC_H(w) (w)->sh.height_inc
188 #define SWM_MAX_FONT_STEPS (3)
189 #define WINID(w) ((w) ? (w)->id : 0)
190 #define YESNO(x) ((x) ? "yes" : "no")
192 #define SWM_FOCUS_DEFAULT (0)
193 #define SWM_FOCUS_SYNERGY (1)
194 #define SWM_FOCUS_FOLLOW (2)
196 #define SWM_CONF_DEFAULT (0)
197 #define SWM_CONF_KEYMAPPING (1)
200 #define SWM_LIB "/usr/local/lib/libswmhack.so"
213 volatile sig_atomic_t running
= 1;
214 volatile sig_atomic_t restart_wm
= 0;
216 int last_focus_event
= FocusOut
;
217 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
221 int xrandr_eventbase
;
222 unsigned int numlockmask
= 0;
224 xcb_connection_t
*conn
;
227 int cycle_visible
= 0;
229 int font_adjusted
= 0;
230 unsigned int mod_key
= MODKEY
;
233 struct swm_region
*search_r
;
234 int select_list_pipe
[2];
235 int select_resp_pipe
[2];
237 volatile sig_atomic_t search_resp
;
238 int search_resp_action
;
240 struct search_window
{
241 TAILQ_ENTRY(search_window
) entry
;
247 TAILQ_HEAD(search_winlist
, search_window
);
248 struct search_winlist search_wl
;
253 SWM_SEARCH_UNICONIFY
,
254 SWM_SEARCH_NAME_WORKSPACE
,
255 SWM_SEARCH_SEARCH_WORKSPACE
,
256 SWM_SEARCH_SEARCH_WINDOW
259 #define SWM_STACK_TOP (0)
260 #define SWM_STACK_BOTTOM (1)
261 #define SWM_STACK_ABOVE (2)
262 #define SWM_STACK_BELOW (3)
265 double dialog_ratio
= 0.6;
267 #define SWM_BAR_MAX (256)
268 #define SWM_BAR_JUSTIFY_LEFT (0)
269 #define SWM_BAR_JUSTIFY_CENTER (1)
270 #define SWM_BAR_JUSTIFY_RIGHT (2)
271 #define SWM_BAR_OFFSET (4)
272 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
273 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
274 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
275 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
276 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
278 #ifdef X_HAVE_UTF8_STRING
279 #define DRAWSTRING(x...) Xutf8DrawString(x)
281 #define DRAWSTRING(x...) XmbDrawString(x)
284 char *bar_argv
[] = { NULL
, NULL
};
286 unsigned char bar_ext
[SWM_BAR_MAX
];
287 char bar_vertext
[SWM_BAR_MAX
];
289 sig_atomic_t bar_alarm
= 0;
292 int bar_border_width
= 1;
293 int bar_at_bottom
= 0;
295 int bar_extra_running
= 0;
298 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
299 char *bar_format
= NULL
;
300 int stack_enabled
= 1;
301 int clock_enabled
= 1;
302 int urgent_enabled
= 0;
303 char *clock_format
= NULL
;
304 int title_name_enabled
= 0;
305 int title_class_enabled
= 0;
306 int window_name_enabled
= 0;
307 int focus_mode
= SWM_FOCUS_DEFAULT
;
308 int focus_close
= SWM_STACK_BELOW
;
309 int focus_close_wrap
= 1;
310 int focus_default
= SWM_STACK_TOP
;
311 int spawn_position
= SWM_STACK_TOP
;
312 int disable_border
= 0;
313 int border_width
= 1;
314 int verbose_layout
= 0;
317 XFontSetExtents
*bar_fs_extents
;
321 #define SWM_MENU_FN (2)
322 #define SWM_MENU_NB (4)
323 #define SWM_MENU_NF (6)
324 #define SWM_MENU_SB (8)
325 #define SWM_MENU_SF (10)
327 /* layout manager data */
328 struct swm_geometry
{
341 struct swm_geometry g
;
344 /* virtual "screens" */
346 TAILQ_ENTRY(swm_region
) entry
;
347 struct swm_geometry g
;
348 struct workspace
*ws
; /* current workspace on this region */
349 struct workspace
*ws_prior
; /* prior workspace on this region */
350 struct swm_screen
*s
; /* screen idx */
353 TAILQ_HEAD(swm_region_list
, swm_region
);
356 TAILQ_ENTRY(ws_win
) entry
;
359 struct ws_win
*child_trans
; /* transient child window */
360 struct swm_geometry g
; /* current geometry */
361 struct swm_geometry g_float
; /* region coordinates */
362 int g_floatvalid
; /* g_float geometry validity */
363 int floatmaxed
; /* whether maxed by max_stack */
368 unsigned int ewmh_flags
;
369 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
375 unsigned long quirks
;
376 struct workspace
*ws
; /* always valid */
377 struct swm_screen
*s
; /* always valid, never changes */
378 XWindowAttributes wa
;
384 TAILQ_HEAD(ws_win_list
, ws_win
);
388 TAILQ_ENTRY(pid_e
) entry
;
392 TAILQ_HEAD(pid_list
, pid_e
);
393 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
395 /* layout handlers */
397 void vertical_config(struct workspace
*, int);
398 void vertical_stack(struct workspace
*, struct swm_geometry
*);
399 void horizontal_config(struct workspace
*, int);
400 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
401 void max_stack(struct workspace
*, struct swm_geometry
*);
402 void plain_stacker(struct workspace
*);
403 void fancy_stacker(struct workspace
*);
405 struct ws_win
*find_window(Window
);
407 void grabbuttons(struct ws_win
*, int);
408 void new_region(struct swm_screen
*, int, int, int, int);
409 void unmanage_window(struct ws_win
*);
410 long getstate(Window
);
412 int conf_load(char *, int);
415 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
416 void (*l_config
)(struct workspace
*, int);
418 #define SWM_L_FOCUSPREV (1<<0)
419 #define SWM_L_MAPONFOCUS (1<<1)
420 void (*l_string
)(struct workspace
*);
422 /* stack, configure */
423 { vertical_stack
, vertical_config
, 0, plain_stacker
},
424 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
426 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
427 { NULL
, NULL
, 0, NULL
},
430 /* position of max_stack mode in the layouts array, index into layouts! */
431 #define SWM_V_STACK (0)
432 #define SWM_H_STACK (1)
433 #define SWM_MAX_STACK (2)
435 #define SWM_H_SLICE (32)
436 #define SWM_V_SLICE (32)
438 /* define work spaces */
440 int idx
; /* workspace index */
441 char *name
; /* workspace name */
442 int always_raise
; /* raise windows on focus */
443 struct layout
*cur_layout
; /* current layout handlers */
444 struct ws_win
*focus
; /* may be NULL */
445 struct ws_win
*focus_prev
; /* may be NULL */
446 struct swm_region
*r
; /* may be NULL */
447 struct swm_region
*old_r
; /* may be NULL */
448 struct ws_win_list winlist
; /* list of windows in ws */
449 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
450 char stacker
[10]; /* display stacker and layout */
454 int horizontal_msize
;
456 int horizontal_stacks
;
467 SWM_S_COLOR_BAR_BORDER
,
468 SWM_S_COLOR_BAR_FONT
,
474 /* physical screen mapping */
475 #define SWM_WS_MAX (22) /* hard limit */
476 int workspace_limit
= 10; /* soft limit */
479 int idx
; /* screen index */
480 struct swm_region_list rl
; /* list of regions on this screen */
481 struct swm_region_list orl
; /* list of old regions */
483 struct workspace ws
[SWM_WS_MAX
];
489 } c
[SWM_S_COLOR_MAX
];
493 struct swm_screen
*screens
;
495 /* args to functions */
498 #define SWM_ARG_ID_FOCUSNEXT (0)
499 #define SWM_ARG_ID_FOCUSPREV (1)
500 #define SWM_ARG_ID_FOCUSMAIN (2)
501 #define SWM_ARG_ID_FOCUSCUR (4)
502 #define SWM_ARG_ID_SWAPNEXT (10)
503 #define SWM_ARG_ID_SWAPPREV (11)
504 #define SWM_ARG_ID_SWAPMAIN (12)
505 #define SWM_ARG_ID_MOVELAST (13)
506 #define SWM_ARG_ID_MASTERSHRINK (20)
507 #define SWM_ARG_ID_MASTERGROW (21)
508 #define SWM_ARG_ID_MASTERADD (22)
509 #define SWM_ARG_ID_MASTERDEL (23)
510 #define SWM_ARG_ID_FLIPLAYOUT (24)
511 #define SWM_ARG_ID_STACKRESET (30)
512 #define SWM_ARG_ID_STACKINIT (31)
513 #define SWM_ARG_ID_CYCLEWS_UP (40)
514 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
515 #define SWM_ARG_ID_CYCLESC_UP (42)
516 #define SWM_ARG_ID_CYCLESC_DOWN (43)
517 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
518 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
519 #define SWM_ARG_ID_STACKINC (50)
520 #define SWM_ARG_ID_STACKDEC (51)
521 #define SWM_ARG_ID_SS_ALL (60)
522 #define SWM_ARG_ID_SS_WINDOW (61)
523 #define SWM_ARG_ID_DONTCENTER (70)
524 #define SWM_ARG_ID_CENTER (71)
525 #define SWM_ARG_ID_KILLWINDOW (80)
526 #define SWM_ARG_ID_DELETEWINDOW (81)
527 #define SWM_ARG_ID_WIDTHGROW (90)
528 #define SWM_ARG_ID_WIDTHSHRINK (91)
529 #define SWM_ARG_ID_HEIGHTGROW (92)
530 #define SWM_ARG_ID_HEIGHTSHRINK (93)
531 #define SWM_ARG_ID_MOVEUP (100)
532 #define SWM_ARG_ID_MOVEDOWN (101)
533 #define SWM_ARG_ID_MOVELEFT (102)
534 #define SWM_ARG_ID_MOVERIGHT (103)
538 void focus(struct swm_region
*, union arg
*);
539 void focus_magic(struct ws_win
*);
543 TAILQ_ENTRY(quirk
) entry
;
547 #define SWM_Q_FLOAT (1<<0) /* float this window */
548 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
549 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
550 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
551 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
552 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
554 TAILQ_HEAD(quirk_list
, quirk
);
555 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
558 * Supported EWMH hints should be added to
559 * both the enum and the ewmh array
564 _NET_MOVERESIZE_WINDOW
,
565 _NET_WM_ACTION_CLOSE
,
566 _NET_WM_ACTION_FULLSCREEN
,
568 _NET_WM_ACTION_RESIZE
,
569 _NET_WM_ALLOWED_ACTIONS
,
572 _NET_WM_STATE_FULLSCREEN
,
573 _NET_WM_STATE_HIDDEN
,
574 _NET_WM_STATE_MAXIMIZED_HORZ
,
575 _NET_WM_STATE_MAXIMIZED_VERT
,
576 _NET_WM_STATE_SKIP_PAGER
,
577 _NET_WM_STATE_SKIP_TASKBAR
,
579 _NET_WM_WINDOW_TYPE_DIALOG
,
580 _NET_WM_WINDOW_TYPE_DOCK
,
581 _NET_WM_WINDOW_TYPE_NORMAL
,
582 _NET_WM_WINDOW_TYPE_SPLASH
,
583 _NET_WM_WINDOW_TYPE_TOOLBAR
,
584 _NET_WM_WINDOW_TYPE_UTILITY
,
585 _SWM_WM_STATE_MANUAL
,
592 } ewmh
[SWM_EWMH_HINT_MAX
] = {
593 /* must be in same order as in the enum */
594 {"_NET_ACTIVE_WINDOW", None
},
595 {"_NET_CLOSE_WINDOW", None
},
596 {"_NET_MOVERESIZE_WINDOW", None
},
597 {"_NET_WM_ACTION_CLOSE", None
},
598 {"_NET_WM_ACTION_FULLSCREEN", None
},
599 {"_NET_WM_ACTION_MOVE", None
},
600 {"_NET_WM_ACTION_RESIZE", None
},
601 {"_NET_WM_ALLOWED_ACTIONS", None
},
602 {"_NET_WM_STATE", None
},
603 {"_NET_WM_STATE_ABOVE", None
},
604 {"_NET_WM_STATE_FULLSCREEN", None
},
605 {"_NET_WM_STATE_HIDDEN", None
},
606 {"_NET_WM_STATE_MAXIMIZED_HORZ", None
},
607 {"_NET_WM_STATE_MAXIMIZED_VERT", None
},
608 {"_NET_WM_STATE_SKIP_PAGER", None
},
609 {"_NET_WM_STATE_SKIP_TASKBAR", None
},
610 {"_NET_WM_WINDOW_TYPE", None
},
611 {"_NET_WM_WINDOW_TYPE_DIALOG", None
},
612 {"_NET_WM_WINDOW_TYPE_DOCK", None
},
613 {"_NET_WM_WINDOW_TYPE_NORMAL", None
},
614 {"_NET_WM_WINDOW_TYPE_SPLASH", None
},
615 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None
},
616 {"_NET_WM_WINDOW_TYPE_UTILITY", None
},
617 {"_SWM_WM_STATE_MANUAL", None
},
620 void store_float_geom(struct ws_win
*, struct swm_region
*);
621 int floating_toggle_win(struct ws_win
*);
622 void constrain_window(struct ws_win
*, struct swm_region
*, int);
623 void update_window(struct ws_win
*);
624 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
625 unsigned char *get_win_name(Window
);
628 get_property(Window id
, Atom atom
, long count
, Atom type
, unsigned long *nitems
,
629 unsigned long *nbytes
, unsigned char **data
)
632 unsigned long *nbytes_ret
, *nitems_ret
;
633 unsigned long nbytes_tmp
, nitems_tmp
;
636 nbytes_ret
= nbytes
!= NULL
? nbytes
: &nbytes_tmp
;
637 nitems_ret
= nitems
!= NULL
? nitems
: &nitems_tmp
;
639 status
= XGetWindowProperty(display
, id
, atom
, 0L, count
, False
, type
,
640 &real
, &format
, nitems_ret
, nbytes_ret
, data
);
642 if (status
!= Success
)
651 update_iconic(struct ws_win
*win
, int newv
)
655 xcb_intern_atom_cookie_t c
;
656 xcb_intern_atom_reply_t
*r
;
660 c
= xcb_intern_atom(conn
, False
, strlen("_SWM_ICONIC"), "_SWM_ICONIC");
661 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
669 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
670 iprop
, XCB_ATOM_INTEGER
, 32, 1, &v
);
672 xcb_delete_property(conn
, win
->id
, iprop
);
676 get_iconic(struct ws_win
*win
)
681 unsigned long nitems
, extra
;
682 unsigned char *prop
= NULL
;
684 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
687 status
= XGetWindowProperty(display
, win
->id
, iprop
, 0L, 1L,
688 False
, XA_INTEGER
, &rettype
, &retfmt
, &nitems
, &extra
, &prop
);
689 if (status
!= Success
)
691 if (rettype
!= XA_INTEGER
|| retfmt
!= 32)
695 v
= *((int32_t *)prop
);
707 xcb_intern_atom_cookie_t c
;
708 xcb_intern_atom_reply_t
*r
;
709 int i
, j
, num_screens
;
711 c
= xcb_intern_atom(conn
, False
, strlen("_NET_SUPPORTED"),
713 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
719 for (i
= 0; i
< LENGTH(ewmh
); i
++) {
720 c
= xcb_intern_atom(conn
, False
, strlen(ewmh
[i
].name
),
722 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
724 ewmh
[i
].atom
= r
->atom
;
729 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
730 for (i
= 0; i
< num_screens
; i
++) {
731 /* Support check window will be created by workaround(). */
733 /* Report supported atoms */
734 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
735 for (j
= 0; j
< LENGTH(ewmh
); j
++)
736 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
737 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
746 unsigned char *data
= NULL
;
748 Atom sup_check
, sup_list
;
751 sup_check
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
752 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
754 for (i
= 0; i
< ScreenCount(display
); i
++) {
755 /* Get the support check window and destroy it */
756 success
= get_property(screens
[i
].root
, sup_check
, 1, XA_WINDOW
,
761 XDestroyWindow(display
, id
);
762 XDeleteProperty(display
, screens
[i
].root
, sup_check
);
763 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
771 ewmh_autoquirk(struct ws_win
*win
)
774 unsigned long *data
= NULL
, n
;
777 success
= get_property(win
->id
, ewmh
[_NET_WM_WINDOW_TYPE
].atom
, (~0L),
778 XA_ATOM
, &n
, NULL
, (void *)&data
);
785 for (i
= 0; i
< n
; i
++) {
787 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
789 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
790 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
791 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
793 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
796 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
797 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
799 win
->quirks
= SWM_Q_FLOAT
;
807 #define SWM_EWMH_ACTION_COUNT_MAX (6)
808 #define EWMH_F_FULLSCREEN (1<<0)
809 #define EWMH_F_ABOVE (1<<1)
810 #define EWMH_F_HIDDEN (1<<2)
811 #define EWMH_F_SKIP_PAGER (1<<3)
812 #define EWMH_F_SKIP_TASKBAR (1<<4)
813 #define SWM_F_MANUAL (1<<5)
816 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
824 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%lx, "
825 "fullscreen %s\n", win
->id
, YESNO(fs
));
828 if (!win
->g_floatvalid
)
829 store_float_geom(win
, win
->ws
->r
);
831 win
->g
= win
->ws
->r
->g
;
834 if (win
->g_floatvalid
) {
835 /* refloat at last floating relative position */
836 win
->g
= win
->g_float
;
837 X(win
) += X(win
->ws
->r
);
838 Y(win
) += Y(win
->ws
->r
);
846 ewmh_update_actions(struct ws_win
*win
)
848 Atom actions
[SWM_EWMH_ACTION_COUNT_MAX
];
854 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
857 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
858 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
861 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
,
862 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)actions
, n
);
865 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
866 #define _NET_WM_STATE_ADD 1 /* add/set property */
867 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
870 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
872 unsigned int mask
= 0;
873 unsigned int changed
= 0;
874 unsigned int orig_flags
;
879 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
880 mask
= EWMH_F_FULLSCREEN
;
881 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
883 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
885 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
886 mask
= EWMH_F_SKIP_PAGER
;
887 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
888 mask
= EWMH_F_SKIP_TASKBAR
;
891 orig_flags
= win
->ewmh_flags
;
894 case _NET_WM_STATE_REMOVE
:
895 win
->ewmh_flags
&= ~mask
;
897 case _NET_WM_STATE_ADD
:
898 win
->ewmh_flags
|= mask
;
900 case _NET_WM_STATE_TOGGLE
:
901 win
->ewmh_flags
^= mask
;
905 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
907 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
909 if (!floating_toggle_win(win
))
910 win
->ewmh_flags
= orig_flags
; /* revert */
911 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
913 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
914 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
916 if (!ewmh_set_win_fullscreen(win
,
917 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
918 win
->ewmh_flags
= orig_flags
; /* revert */
920 XDeleteProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
922 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
923 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
924 XA_ATOM
, 32, PropModeAppend
,
925 (unsigned char *)&ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
, 1);
926 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
927 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
928 XA_ATOM
, 32, PropModeAppend
,
929 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
, 1);
930 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
931 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
932 XA_ATOM
, 32, PropModeAppend
,
933 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
, 1);
934 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
935 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
936 XA_ATOM
, 32, PropModeAppend
,
937 (unsigned char *)&ewmh
[_NET_WM_STATE_ABOVE
].atom
, 1);
938 if (win
->ewmh_flags
& SWM_F_MANUAL
)
939 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
940 XA_ATOM
, 32, PropModeAppend
,
941 (unsigned char *)&ewmh
[_SWM_WM_STATE_MANUAL
].atom
, 1);
945 ewmh_get_win_state(struct ws_win
*win
)
956 win
->ewmh_flags
|= EWMH_F_ABOVE
;
958 win
->ewmh_flags
|= SWM_F_MANUAL
;
960 success
= get_property(win
->id
, ewmh
[_NET_WM_STATE
].atom
,
961 (~0L), XA_ATOM
, &n
, NULL
, (void *)&states
);
966 for (i
= 0; i
< n
; i
++)
967 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
975 geteventname(XEvent
*e
)
987 name
= "ButtonPress";
990 name
= "ButtonRelease";
993 name
= "MotionNotify";
996 name
= "EnterNotify";
999 name
= "LeaveNotify";
1008 name
= "KeymapNotify";
1013 case GraphicsExpose
:
1014 name
= "GraphicsExpose";
1019 case VisibilityNotify
:
1020 name
= "VisibilityNotify";
1023 name
= "CreateNotify";
1026 name
= "DestroyNotify";
1029 name
= "UnmapNotify";
1035 name
= "MapRequest";
1037 case ReparentNotify
:
1038 name
= "ReparentNotify";
1040 case ConfigureNotify
:
1041 name
= "ConfigureNotify";
1043 case ConfigureRequest
:
1044 name
= "ConfigureRequest";
1047 name
= "GravityNotify";
1050 name
= "ResizeRequest";
1052 case CirculateNotify
:
1053 name
= "CirculateNotify";
1055 case CirculateRequest
:
1056 name
= "CirculateRequest";
1058 case PropertyNotify
:
1059 name
= "PropertyNotify";
1061 case SelectionClear
:
1062 name
= "SelectionClear";
1064 case SelectionRequest
:
1065 name
= "SelectionRequest";
1067 case SelectionNotify
:
1068 name
= "SelectionNotify";
1070 case ColormapNotify
:
1071 name
= "ColormapNotify";
1074 name
= "ClientMessage";
1077 name
= "MappingNotify";
1087 xrandr_geteventname(XEvent
*e
)
1091 switch(e
->type
- xrandr_eventbase
) {
1092 case RRScreenChangeNotify
:
1093 name
= "RRScreenChangeNotify";
1103 dumpwins(struct swm_region
*r
, union arg
*args
)
1107 XWindowAttributes wa
;
1109 if (r
->ws
== NULL
) {
1110 warnx("dumpwins: invalid workspace");
1114 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1116 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1117 state
= getstate(win
->id
);
1118 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1119 warnx("window: 0x%lx, failed XGetWindowAttributes",
1121 warnx("window: 0x%lx, map_state: %d, state: %d, "
1122 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1126 warnx("===== unmanaged window list =====");
1127 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1128 state
= getstate(win
->id
);
1129 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1130 warnx("window: 0x%lx, failed XGetWindowAttributes",
1132 warnx("window: 0x%lx, map_state: %d, state: %d, "
1133 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1137 warnx("=================================");
1141 dumpwins(struct swm_region
*r
, union arg
*args
)
1144 #endif /* SWM_DEBUG */
1146 void expose(XEvent
*);
1147 void keypress(XEvent
*);
1148 void buttonpress(XEvent
*);
1149 void configurerequest(XEvent
*);
1150 void configurenotify(XEvent
*);
1151 void destroynotify(XEvent
*);
1152 void enternotify(XEvent
*);
1153 void focusevent(XEvent
*);
1154 void mapnotify(XEvent
*);
1155 void mappingnotify(XEvent
*);
1156 void maprequest(XEvent
*);
1157 void propertynotify(XEvent
*);
1158 void unmapnotify(XEvent
*);
1159 void visibilitynotify(XEvent
*);
1160 void clientmessage(XEvent
*);
1162 void (*handler
[LASTEvent
])(XEvent
*) = {
1164 [KeyPress
] = keypress
,
1165 [ButtonPress
] = buttonpress
,
1166 [ConfigureRequest
] = configurerequest
,
1167 [ConfigureNotify
] = configurenotify
,
1168 [DestroyNotify
] = destroynotify
,
1169 [EnterNotify
] = enternotify
,
1170 [FocusIn
] = focusevent
,
1171 [FocusOut
] = focusevent
,
1172 [MapNotify
] = mapnotify
,
1173 [MappingNotify
] = mappingnotify
,
1174 [MapRequest
] = maprequest
,
1175 [PropertyNotify
] = propertynotify
,
1176 [UnmapNotify
] = unmapnotify
,
1177 [VisibilityNotify
] = visibilitynotify
,
1178 [ClientMessage
] = clientmessage
,
1184 int saved_errno
, status
;
1187 saved_errno
= errno
;
1191 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1196 if (errno
!= ECHILD
)
1197 warn("sighdlr: waitpid");
1198 #endif /* SWM_DEBUG */
1201 if (pid
== searchpid
)
1205 if (WIFEXITED(status
)) {
1206 if (WEXITSTATUS(status
) != 0)
1207 warnx("sighdlr: child exit status: %d",
1208 WEXITSTATUS(status
));
1210 warnx("sighdlr: child is terminated "
1212 #endif /* SWM_DEBUG */
1226 errno
= saved_errno
;
1232 struct pid_e
*p
= NULL
;
1234 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1239 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1248 name_to_color(char *colorname
)
1252 XColor screen_def
, exact_def
;
1253 unsigned long result
= 0;
1254 char cname
[32] = "#";
1256 cmap
= DefaultColormap(display
, screens
[0].idx
);
1257 status
= XAllocNamedColor(display
, cmap
, colorname
,
1258 &screen_def
, &exact_def
);
1260 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1261 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
1265 result
= screen_def
.pixel
;
1267 warnx("color '%s' not found", colorname
);
1273 setscreencolor(char *val
, int i
, int c
)
1277 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1278 if (i
> 0 && i
<= num_screens
) {
1279 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1280 free(screens
[i
- 1].c
[c
].name
);
1281 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1283 } else if (i
== -1) {
1284 for (i
= 0; i
< num_screens
; i
++) {
1285 screens
[i
].c
[c
].color
= name_to_color(val
);
1286 free(screens
[i
].c
[c
].name
);
1287 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1291 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1296 fancy_stacker(struct workspace
*ws
)
1298 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1299 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1300 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1301 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1302 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1303 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1304 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1305 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1306 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1310 plain_stacker(struct workspace
*ws
)
1312 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1313 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1314 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1315 sizeof ws
->stacker
);
1316 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1317 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1318 sizeof ws
->stacker
);
1322 custom_region(char *val
)
1324 unsigned int sidx
, x
, y
, w
, h
;
1326 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1327 errx(1, "invalid custom region, "
1328 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1329 if (sidx
< 1 || sidx
> ScreenCount(display
))
1330 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1331 sidx
, ScreenCount(display
));
1335 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1337 if (x
> DisplayWidth(display
, sidx
) ||
1338 y
> DisplayHeight(display
, sidx
) ||
1339 w
+ x
> DisplayWidth(display
, sidx
) ||
1340 h
+ y
> DisplayHeight(display
, sidx
)) {
1341 warnx("ignoring region %ux%u+%u+%u - not within screen "
1342 "boundaries (%ux%u)", w
, h
, x
, y
,
1343 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
1347 new_region(&screens
[sidx
], x
, y
, w
, h
);
1351 socket_setnonblock(int fd
)
1355 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1356 err(1, "fcntl F_GETFL");
1357 flags
|= O_NONBLOCK
;
1358 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1359 err(1, "fcntl F_SETFL");
1363 bar_print(struct swm_region
*r
, const char *s
)
1367 XRectangle ibox
, lbox
;
1370 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1372 switch (bar_justify
) {
1373 case SWM_BAR_JUSTIFY_LEFT
:
1376 case SWM_BAR_JUSTIFY_CENTER
:
1377 x
= (WIDTH(r
) - lbox
.width
) / 2;
1379 case SWM_BAR_JUSTIFY_RIGHT
:
1380 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1384 if (x
< SWM_BAR_OFFSET
)
1387 /* clear back buffer */
1388 XSetForeground(display
, r
->s
->bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1389 XFillRectangle(display
, r
->bar
->buffer
, r
->s
->bar_gc
, 0, 0,
1390 WIDTH(r
->bar
), HEIGHT(r
->bar
));
1392 /* draw back buffer */
1393 XSetForeground(display
, r
->s
->bar_gc
,
1394 r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
1395 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, r
->s
->bar_gc
,
1396 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1400 XCopyArea(display
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1401 WIDTH(r
->bar
), HEIGHT(r
->bar
), 0, 0);
1405 bar_extra_stop(void)
1409 bzero(bar_pipe
, sizeof bar_pipe
);
1412 kill(bar_pid
, SIGTERM
);
1415 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1420 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1422 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1424 if (r
->ws
->focus
->ch
.res_class
!= NULL
)
1425 strlcat(s
, r
->ws
->focus
->ch
.res_class
, sz
);
1429 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1431 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1433 if (r
->ws
->focus
->ch
.res_name
!= NULL
)
1434 strlcat(s
, r
->ws
->focus
->ch
.res_name
, sz
);
1438 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1440 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1443 bar_class_name(s
, sz
, r
);
1444 strlcat(s
, ":", sz
);
1445 bar_title_name(s
, sz
, r
);
1449 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1451 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1453 if (r
->ws
->focus
->floating
)
1454 strlcat(s
, "(f)", sz
);
1458 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1460 unsigned char *title
;
1462 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1464 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1467 strlcat(s
, (char *)title
, sz
);
1471 int urgent
[SWM_WS_MAX
];
1473 bar_urgent(char *s
, size_t sz
)
1475 XWMHints
*wmh
= NULL
;
1480 for (i
= 0; i
< workspace_limit
; i
++)
1483 for (i
= 0; i
< ScreenCount(display
); i
++)
1484 for (j
= 0; j
< workspace_limit
; j
++)
1485 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1486 wmh
= XGetWMHints(display
, win
->id
);
1490 if (wmh
->flags
& XUrgencyHint
)
1495 for (i
= 0; i
< workspace_limit
; i
++) {
1497 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1499 snprintf(b
, sizeof b
, "- ");
1505 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1507 if (r
== NULL
|| r
->ws
== NULL
)
1509 if (r
->ws
->name
!= NULL
)
1510 strlcat(s
, r
->ws
->name
, sz
);
1513 /* build the default bar format according to the defined enabled options */
1515 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1517 /* if format provided, just copy the buffers */
1518 if (bar_format
!= NULL
) {
1519 strlcpy(fmtnew
, fmtexp
, sz
);
1523 /* reset the output buffer */
1526 strlcat(fmtnew
, "+N:+I ", sz
);
1528 strlcat(fmtnew
, "+S", sz
);
1529 strlcat(fmtnew
, " ", sz
);
1531 /* only show the workspace name if there's actually one */
1532 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1533 strlcat(fmtnew
, "<+D>", sz
);
1534 strlcat(fmtnew
, "+3<", sz
);
1536 if (clock_enabled
) {
1537 strlcat(fmtnew
, fmtexp
, sz
);
1538 strlcat(fmtnew
, "+4<", sz
);
1541 /* bar_urgent already adds the space before the last asterisk */
1543 strlcat(fmtnew
, "* +U*+4<", sz
);
1545 if (title_class_enabled
) {
1546 strlcat(fmtnew
, "+C", sz
);
1547 if (title_name_enabled
== 0)
1548 strlcat(fmtnew
, "+4<", sz
);
1551 /* checks needed by the colon and floating strlcat(3) calls below */
1552 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1553 if (title_name_enabled
) {
1554 if (title_class_enabled
)
1555 strlcat(fmtnew
, ":", sz
);
1556 strlcat(fmtnew
, "+T+4<", sz
);
1558 if (window_name_enabled
) {
1559 if (r
->ws
->focus
->floating
)
1560 strlcat(fmtnew
, "+F ", sz
);
1561 strlcat(fmtnew
, "+64W ", sz
);
1565 /* finally add the action script output and the version */
1566 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1570 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1572 /* special case; no limit given, pad one space, instead */
1573 if (*limit
== sz
- 1)
1575 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1578 /* replaces the bar format character sequences (like in tmux(1)) */
1580 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1584 char tmp
[SWM_BAR_MAX
];
1588 /* reset strlcat(3) buffer */
1591 /* get number, if any */
1594 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1595 limit
= sizeof tmp
- 1;
1596 if (limit
<= 0 || limit
>= sizeof tmp
)
1597 limit
= sizeof tmp
- 1;
1599 /* there is nothing to replace (ie EOL) */
1606 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1609 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1612 bar_class_name(tmp
, sizeof tmp
, r
);
1615 bar_workspace_name(tmp
, sizeof tmp
, r
);
1618 bar_window_float(tmp
, sizeof tmp
, r
);
1621 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1624 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1627 bar_class_title_name(tmp
, sizeof tmp
, r
);
1630 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1633 bar_title_name(tmp
, sizeof tmp
, r
);
1636 bar_urgent(tmp
, sizeof tmp
);
1639 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1642 bar_window_name(tmp
, sizeof tmp
, r
);
1645 /* unknown character sequence; copy as-is */
1646 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1654 while (limit
-- > 0) {
1655 if (*offrep
>= sz
- 1)
1657 fmtrep
[(*offrep
)++] = *ptr
++;
1665 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1670 while (*fmt
!= '\0') {
1672 /* skip ordinary characters */
1675 fmtrep
[off
++] = *fmt
++;
1679 /* character sequence found; replace it */
1680 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1689 bar_fmt_expand(char *fmtexp
, size_t sz
)
1696 /* start by grabbing the current time and date */
1698 localtime_r(&tmt
, &tm
);
1700 /* figure out what to expand */
1701 if (bar_format
!= NULL
)
1703 else if (bar_format
== NULL
&& clock_enabled
)
1705 /* if nothing to expand bail out */
1711 /* copy as-is, just in case the format shouldn't be expanded below */
1712 strlcpy(fmtexp
, fmt
, sz
);
1713 /* finally pass the string through strftime(3) */
1714 #ifndef SWM_DENY_CLOCK_FORMAT
1715 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1716 warnx("format too long");
1724 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1725 char fmtrep
[SWM_BAR_MAX
];
1727 struct swm_region
*r
;
1729 /* expand the format by first passing it through strftime(3) */
1730 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1732 for (i
= 0; i
< ScreenCount(display
); i
++) {
1733 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1736 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1737 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1738 bar_print(r
, fmtrep
);
1749 if (bar_enabled
== 0)
1751 if (bar_extra
&& bar_extra_running
) {
1752 /* ignore short reads; it'll correct itself */
1753 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1754 if (b
&& b
[len
- 1] == '\n') {
1756 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1758 if (b
== NULL
&& errno
!= EAGAIN
) {
1759 warn("bar_update: bar_extra failed");
1763 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1776 bar_toggle(struct swm_region
*r
, union arg
*args
)
1778 struct swm_region
*tmpr
;
1779 int i
, sc
= ScreenCount(display
);
1781 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1784 for (i
= 0; i
< sc
; i
++)
1785 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1787 XUnmapWindow(display
, tmpr
->bar
->id
);
1789 for (i
= 0; i
< sc
; i
++)
1790 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1792 XMapRaised(display
, tmpr
->bar
->id
);
1795 bar_enabled
= !bar_enabled
;
1798 /* must be after stack */
1805 XSetWindowAttributes wa
;
1806 struct swm_region
*r
;
1809 /* do this here because the conf file is in memory */
1810 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1811 /* launch external status app */
1812 bar_extra_running
= 1;
1813 if (pipe(bar_pipe
) == -1)
1814 err(1, "pipe error");
1815 socket_setnonblock(bar_pipe
[0]);
1816 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1817 if (dup2(bar_pipe
[0], 0) == -1)
1819 if (dup2(bar_pipe
[1], 1) == -1)
1821 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1822 err(1, "could not disable SIGPIPE");
1823 switch (bar_pid
= fork()) {
1825 err(1, "cannot fork");
1829 execvp(bar_argv
[0], bar_argv
);
1830 err(1, "%s external app failed", bar_argv
[0]);
1832 default: /* parent */
1838 bzero(&wa
, sizeof wa
);
1839 for (i
= 0; i
< ScreenCount(display
); i
++)
1840 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1844 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1845 wa
.background_pixel
=
1846 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1847 XChangeWindowAttributes(display
, r
->bar
->id
,
1848 CWBackPixel
| CWBorderPixel
, &wa
);
1854 bar_setup(struct swm_region
*r
)
1856 char *default_string
;
1857 char **missing_charsets
;
1858 int num_missing_charsets
= 0;
1862 XFreeFontSet(display
, bar_fs
);
1866 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
1867 err(1, "bar_setup: calloc: failed to allocate memory.");
1869 DNPRINTF(SWM_D_BAR
, "bar_setup: loading bar_fonts: %s\n", bar_fonts
);
1871 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
1872 &num_missing_charsets
, &default_string
);
1874 if (num_missing_charsets
> 0) {
1875 warnx("Unable to load charset(s):");
1877 for (i
= 0; i
< num_missing_charsets
; ++i
)
1878 warnx("%s", missing_charsets
[i
]);
1880 XFreeStringList(missing_charsets
);
1882 if (strcmp(default_string
, ""))
1883 warnx("Glyphs from those sets will be replaced "
1884 "by '%s'.", default_string
);
1886 warnx("Glyphs from those sets won't be drawn.");
1890 errx(1, "Error creating font set structure.");
1892 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
1894 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
1895 2 * bar_border_width
;
1901 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1902 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
1903 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
1905 r
->bar
->id
= XCreateSimpleWindow(display
,
1906 r
->s
->root
, X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
1907 bar_border_width
, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
1908 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1910 r
->bar
->buffer
= XCreatePixmap(display
, r
->bar
->id
, WIDTH(r
->bar
),
1911 HEIGHT(r
->bar
), DefaultDepth(display
, r
->s
->idx
));
1913 XSelectInput(display
, r
->bar
->id
, VisibilityChangeMask
);
1916 XMapRaised(display
, r
->bar
->id
);
1918 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%lx, (x,y) w x h: (%d,%d) "
1919 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
1922 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1923 err(1, "could not install bar_signal");
1928 bar_cleanup(struct swm_region
*r
)
1932 XDestroyWindow(display
, r
->bar
->id
);
1933 XFreePixmap(display
, r
->bar
->buffer
);
1939 drain_enter_notify(void)
1944 while (XCheckMaskEvent(display
, EnterWindowMask
, &cne
))
1947 DNPRINTF(SWM_D_EVENT
, "drain_enter_notify: drained: %d\n", i
);
1951 set_win_state(struct ws_win
*win
, long state
)
1953 long data
[] = {state
, None
};
1955 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%lx\n", win
->id
);
1960 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1961 (unsigned char *)data
, 2);
1968 unsigned char *p
= NULL
;
1971 if (!get_property(w
, astate
, 2L, astate
, &n
, NULL
, &p
))
1974 result
= *((long *)p
);
1980 version(struct swm_region
*r
, union arg
*args
)
1982 bar_version
= !bar_version
;
1984 snprintf(bar_vertext
, sizeof bar_vertext
,
1985 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
1987 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
1992 client_msg(struct ws_win
*win
, Atom a
)
1994 XClientMessageEvent cm
;
1999 bzero(&cm
, sizeof cm
);
2000 cm
.type
= ClientMessage
;
2001 cm
.window
= win
->id
;
2002 cm
.message_type
= aprot
;
2005 cm
.data
.l
[1] = CurrentTime
;
2006 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
2009 /* synthetic response to a ConfigureRequest when not making a change */
2011 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
2018 /* send notification of unchanged state. */
2019 ce
.type
= ConfigureNotify
;
2022 ce
.width
= WIDTH(win
);
2023 ce
.height
= HEIGHT(win
);
2024 ce
.override_redirect
= False
;
2028 ce
.display
= display
;
2030 ce
.window
= win
->id
;
2031 ce
.border_width
= BORDER(win
);
2035 ce
.display
= ev
->display
;
2036 ce
.event
= ev
->window
;
2037 ce
.window
= ev
->window
;
2039 /* make response appear more WM_SIZE_HINTS-compliant */
2041 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%lx,"
2042 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
2043 "%d x %d\n", win
->id
, win
->sh_mask
, SH_MIN_W(win
),
2044 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2045 SH_INC_W(win
), SH_INC_H(win
));
2049 /* the hint may be set... to 0! */
2050 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2051 ce
.width
= SH_MIN_W(win
);
2052 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2053 ce
.height
= SH_MIN_H(win
);
2058 /* may also be advertized as 0 */
2059 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2060 ce
.width
= SH_MAX_W(win
);
2061 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2062 ce
.height
= SH_MAX_H(win
);
2065 /* resize increment. */
2067 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2068 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2070 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2071 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2075 /* adjust x and y for requested border_width. */
2076 ce
.x
+= BORDER(win
) - ev
->border_width
;
2077 ce
.y
+= BORDER(win
) - ev
->border_width
;
2078 ce
.border_width
= ev
->border_width
;
2079 ce
.above
= ev
->above
;
2082 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
2083 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2084 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2086 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
2090 count_win(struct workspace
*ws
, int count_transient
)
2095 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2096 if (count_transient
== 0 && win
->floating
)
2098 if (count_transient
== 0 && win
->transient
)
2104 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2110 quit(struct swm_region
*r
, union arg
*args
)
2112 DNPRINTF(SWM_D_MISC
, "quit\n");
2117 unmap_window(struct ws_win
*win
)
2122 /* don't unmap again */
2123 if (getstate(win
->id
) == IconicState
)
2126 set_win_state(win
, IconicState
);
2128 XUnmapWindow(display
, win
->id
);
2129 XSetWindowBorder(display
, win
->id
,
2130 win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2139 for (i
= 0; i
< ScreenCount(display
); i
++)
2140 for (j
= 0; j
< workspace_limit
; j
++)
2141 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2146 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
2153 event
.display
= display
; /* Ignored, but what the hell */
2154 event
.window
= win
->id
;
2155 event
.root
= win
->s
->root
;
2156 event
.subwindow
= None
;
2157 event
.time
= CurrentTime
;
2162 event
.same_screen
= True
;
2163 event
.keycode
= XKeysymToKeycode(display
, keysym
);
2164 event
.state
= modifiers
;
2166 event
.type
= KeyPress
;
2167 XSendEvent(event
.display
, event
.window
, True
,
2168 KeyPressMask
, (XEvent
*)&event
);
2170 event
.type
= KeyRelease
;
2171 XSendEvent(event
.display
, event
.window
, True
,
2172 KeyPressMask
, (XEvent
*)&event
);
2177 restart(struct swm_region
*r
, union arg
*args
)
2179 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2181 /* disable alarm because the following code may not be interrupted */
2183 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
2184 err(1, "can't disable alarm");
2189 xcb_disconnect(conn
);
2190 XCloseDisplay(display
);
2191 execvp(start_argv
[0], start_argv
);
2192 warn("execvp failed");
2197 root_to_region(Window root
)
2199 struct swm_region
*r
= NULL
;
2201 int i
, x
, y
, wx
, wy
;
2204 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%lx\n", root
);
2206 for (i
= 0; i
< ScreenCount(display
); i
++)
2207 if (screens
[i
].root
== root
)
2210 if (XQueryPointer(display
, screens
[i
].root
,
2211 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
2212 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: (%d,%d)\n",
2214 /* choose a region based on pointer location */
2215 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2216 if (X(r
) <= x
&& x
< MAX_X(r
) &&
2217 Y(r
) <= y
&& y
< MAX_Y(r
))
2222 r
= TAILQ_FIRST(&screens
[i
].rl
);
2228 find_unmanaged_window(Window id
)
2233 for (i
= 0; i
< ScreenCount(display
); i
++)
2234 for (j
= 0; j
< workspace_limit
; j
++)
2235 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2243 find_window(Window id
)
2246 Window wrr
, wpr
, *wcr
= NULL
;
2250 for (i
= 0; i
< ScreenCount(display
); i
++)
2251 for (j
= 0; j
< workspace_limit
; j
++)
2252 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2256 /* if we were looking for the parent return that window instead */
2257 if (XQueryTree(display
, id
, &wrr
, &wpr
, &wcr
, &nc
) == 0)
2262 /* ignore not found and root */
2263 if (wpr
== 0 || wrr
== wpr
)
2266 /* look for parent */
2267 for (i
= 0; i
< ScreenCount(display
); i
++)
2268 for (j
= 0; j
< workspace_limit
; j
++)
2269 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2277 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2282 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2285 close(ConnectionNumber(display
));
2287 setenv("LD_PRELOAD", SWM_LIB
, 1);
2289 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2290 warn("spawn: asprintf SWM_WS");
2293 setenv("_SWM_WS", ret
, 1);
2297 if (asprintf(&ret
, "%d", getpid()) == -1) {
2298 warn("spawn: asprintf _SWM_PID");
2301 setenv("_SWM_PID", ret
, 1);
2305 if (setsid() == -1) {
2306 warn("spawn: setsid");
2312 * close stdin and stdout to prevent interaction between apps
2313 * and the baraction script
2314 * leave stderr open to record errors
2316 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2317 warn("spawn: open");
2320 dup2(fd
, STDIN_FILENO
);
2321 dup2(fd
, STDOUT_FILENO
);
2326 execvp(args
->argv
[0], args
->argv
);
2328 warn("spawn: execvp");
2333 kill_refs(struct ws_win
*win
)
2336 struct swm_region
*r
;
2337 struct workspace
*ws
;
2342 for (i
= 0; i
< ScreenCount(display
); i
++)
2343 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2344 for (x
= 0; x
< workspace_limit
; x
++) {
2346 if (win
== ws
->focus
)
2348 if (win
== ws
->focus_prev
)
2349 ws
->focus_prev
= NULL
;
2354 validate_win(struct ws_win
*testwin
)
2357 struct workspace
*ws
;
2358 struct swm_region
*r
;
2361 if (testwin
== NULL
)
2364 for (i
= 0; i
< ScreenCount(display
); i
++)
2365 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2366 for (x
= 0; x
< workspace_limit
; x
++) {
2368 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2376 validate_ws(struct workspace
*testws
)
2378 struct swm_region
*r
;
2379 struct workspace
*ws
;
2382 /* validate all ws */
2383 for (i
= 0; i
< ScreenCount(display
); i
++)
2384 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2385 for (x
= 0; x
< workspace_limit
; x
++) {
2394 unfocus_win(struct ws_win
*win
)
2399 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%lx\n", WINID(win
));
2403 if (win
->ws
== NULL
)
2406 if (validate_ws(win
->ws
))
2407 return; /* XXX this gets hit with thunderbird, needs fixing */
2409 if (win
->ws
->r
== NULL
)
2412 if (validate_win(win
)) {
2417 if (win
->ws
->focus
== win
) {
2418 win
->ws
->focus
= NULL
;
2419 win
->ws
->focus_prev
= win
;
2422 if (validate_win(win
->ws
->focus
)) {
2423 kill_refs(win
->ws
->focus
);
2424 win
->ws
->focus
= NULL
;
2426 if (validate_win(win
->ws
->focus_prev
)) {
2427 kill_refs(win
->ws
->focus_prev
);
2428 win
->ws
->focus_prev
= NULL
;
2431 /* drain all previous unfocus events */
2432 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
2435 grabbuttons(win
, 0);
2436 XSetWindowBorder(display
, win
->id
,
2437 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2439 XChangeProperty(display
, win
->s
->root
,
2440 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2441 PropModeReplace
, (unsigned char *)&none
, 1);
2450 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2452 for (i
= 0; i
< ScreenCount(display
); i
++)
2453 for (j
= 0; j
< workspace_limit
; j
++)
2454 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2459 focus_win(struct ws_win
*win
)
2464 struct ws_win
*cfw
= NULL
;
2467 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%lx\n", WINID(win
));
2471 if (win
->ws
== NULL
)
2474 if (validate_ws(win
->ws
))
2475 return; /* XXX this gets hit with thunderbird, needs fixing */
2477 if (validate_win(win
)) {
2482 if (validate_win(win
)) {
2487 XGetInputFocus(display
, &cur_focus
, &rr
);
2488 if ((cfw
= find_window(cur_focus
)) != NULL
)
2491 /* use larger hammer since the window was killed somehow */
2492 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2493 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2494 XSetWindowBorder(display
, cfw
->id
,
2495 cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2498 win
->ws
->focus
= win
;
2500 if (win
->ws
->r
!= NULL
) {
2501 /* drain all previous focus events */
2502 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
2506 XSetInputFocus(display
, win
->id
,
2507 RevertToParent
, CurrentTime
);
2508 grabbuttons(win
, 1);
2509 XSetWindowBorder(display
, win
->id
,
2510 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2511 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2512 win
->ws
->always_raise
)
2513 XMapRaised(display
, win
->id
);
2515 XChangeProperty(display
, win
->s
->root
,
2516 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2517 PropModeReplace
, (unsigned char *)&win
->id
, 1);
2524 switchws(struct swm_region
*r
, union arg
*args
)
2526 int wsid
= args
->id
, unmap_old
= 0;
2527 struct swm_region
*this_r
, *other_r
;
2529 struct workspace
*new_ws
, *old_ws
;
2535 if (wsid
>= workspace_limit
)
2539 old_ws
= this_r
->ws
;
2540 new_ws
= &this_r
->s
->ws
[wsid
];
2542 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2543 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2545 if (new_ws
== NULL
|| old_ws
== NULL
)
2547 if (new_ws
== old_ws
)
2550 other_r
= new_ws
->r
;
2551 if (other_r
== NULL
) {
2552 /* the other workspace is hidden, hide this one */
2556 /* the other ws is visible in another region, exchange them */
2557 other_r
->ws_prior
= new_ws
;
2558 other_r
->ws
= old_ws
;
2559 old_ws
->r
= other_r
;
2561 this_r
->ws_prior
= old_ws
;
2562 this_r
->ws
= new_ws
;
2565 /* this is needed so that we can click on a window after a restart */
2569 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2570 focus(new_ws
->r
, &a
);
2572 /* unmap old windows */
2574 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2577 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2578 drain_enter_notify();
2582 cyclews(struct swm_region
*r
, union arg
*args
)
2585 struct swm_screen
*s
= r
->s
;
2588 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2589 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2594 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2597 case SWM_ARG_ID_CYCLEWS_UP
:
2598 if (a
.id
< workspace_limit
- 1)
2603 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2606 case SWM_ARG_ID_CYCLEWS_DOWN
:
2610 a
.id
= workspace_limit
- 1;
2617 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2619 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2623 } while (a
.id
!= r
->ws
->idx
);
2627 priorws(struct swm_region
*r
, union arg
*args
)
2631 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2632 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2634 if (r
->ws_prior
== NULL
)
2637 a
.id
= r
->ws_prior
->idx
;
2642 cyclescr(struct swm_region
*r
, union arg
*args
)
2644 struct swm_region
*rr
= NULL
;
2648 /* do nothing if we don't have more than one screen */
2649 if (!(ScreenCount(display
) > 1 || outputs
> 1))
2654 case SWM_ARG_ID_CYCLESC_UP
:
2655 rr
= TAILQ_NEXT(r
, entry
);
2657 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2659 case SWM_ARG_ID_CYCLESC_DOWN
:
2660 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2662 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2670 /* move mouse to region */
2672 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2673 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2675 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2678 if (rr
->ws
->focus
) {
2679 /* move to focus window */
2680 x
= X(rr
->ws
->focus
) + 1;
2681 y
= Y(rr
->ws
->focus
) + 1;
2682 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2687 sort_windows(struct ws_win_list
*wl
)
2689 struct ws_win
*win
, *parent
, *nxt
;
2694 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2695 nxt
= TAILQ_NEXT(win
, entry
);
2696 if (win
->transient
) {
2697 parent
= find_window(win
->transient
);
2698 if (parent
== NULL
) {
2699 warnx("not possible bug");
2702 TAILQ_REMOVE(wl
, win
, entry
);
2703 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2710 swapwin(struct swm_region
*r
, union arg
*args
)
2712 struct ws_win
*target
, *source
;
2713 struct ws_win
*cur_focus
;
2714 struct ws_win_list
*wl
;
2717 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2718 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2720 cur_focus
= r
->ws
->focus
;
2721 if (cur_focus
== NULL
)
2725 wl
= &source
->ws
->winlist
;
2728 case SWM_ARG_ID_SWAPPREV
:
2729 if (source
->transient
)
2730 source
= find_window(source
->transient
);
2731 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2732 if (target
&& target
->transient
)
2733 target
= find_window(target
->transient
);
2734 TAILQ_REMOVE(wl
, source
, entry
);
2736 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2738 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2740 case SWM_ARG_ID_SWAPNEXT
:
2741 target
= TAILQ_NEXT(source
, entry
);
2742 /* move the parent and let the sort handle the move */
2743 if (source
->transient
)
2744 source
= find_window(source
->transient
);
2745 TAILQ_REMOVE(wl
, source
, entry
);
2747 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2749 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2751 case SWM_ARG_ID_SWAPMAIN
:
2752 target
= TAILQ_FIRST(wl
);
2753 if (target
== source
) {
2754 if (source
->ws
->focus_prev
!= NULL
&&
2755 source
->ws
->focus_prev
!= target
)
2756 source
= source
->ws
->focus_prev
;
2760 if (target
== NULL
|| source
== NULL
)
2762 source
->ws
->focus_prev
= target
;
2763 TAILQ_REMOVE(wl
, target
, entry
);
2764 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2765 TAILQ_REMOVE(wl
, source
, entry
);
2766 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2768 case SWM_ARG_ID_MOVELAST
:
2769 TAILQ_REMOVE(wl
, source
, entry
);
2770 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2773 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2783 focus_prev(struct ws_win
*win
)
2785 struct ws_win
*winfocus
= NULL
;
2786 struct ws_win
*cur_focus
= NULL
;
2787 struct ws_win_list
*wl
= NULL
;
2788 struct workspace
*ws
= NULL
;
2790 if (!(win
&& win
->ws
))
2795 cur_focus
= ws
->focus
;
2797 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%lx, cur_focus: 0x%lx\n",
2798 WINID(win
), WINID(cur_focus
));
2800 /* pickle, just focus on whatever */
2801 if (cur_focus
== NULL
) {
2802 /* use prev_focus if valid */
2803 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2804 find_window(WINID(ws
->focus_prev
)))
2805 winfocus
= ws
->focus_prev
;
2809 /* if transient focus on parent */
2810 if (cur_focus
->transient
) {
2811 winfocus
= find_window(cur_focus
->transient
);
2815 /* if in max_stack try harder */
2816 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2817 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2818 if (cur_focus
!= ws
->focus_prev
)
2819 winfocus
= ws
->focus_prev
;
2821 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2826 DNPRINTF(SWM_D_FOCUS
, "focus_prev: focus_close: %d\n", focus_close
);
2828 if (winfocus
== NULL
|| winfocus
== win
) {
2829 switch (focus_close
) {
2830 case SWM_STACK_BOTTOM
:
2831 winfocus
= TAILQ_FIRST(wl
);
2834 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2836 case SWM_STACK_ABOVE
:
2837 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
2838 if (focus_close_wrap
)
2839 winfocus
= TAILQ_FIRST(wl
);
2841 winfocus
= TAILQ_PREV(cur_focus
,
2842 ws_win_list
, entry
);
2845 case SWM_STACK_BELOW
:
2846 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
2848 if (focus_close_wrap
)
2849 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2851 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2857 if (winfocus
== NULL
) {
2858 if (focus_default
== SWM_STACK_TOP
)
2859 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2861 winfocus
= TAILQ_FIRST(wl
);
2865 focus_magic(winfocus
);
2869 focus(struct swm_region
*r
, union arg
*args
)
2871 struct ws_win
*winfocus
= NULL
, *head
;
2872 struct ws_win
*cur_focus
= NULL
;
2873 struct ws_win_list
*wl
= NULL
;
2874 struct workspace
*ws
= NULL
;
2880 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2882 /* treat FOCUS_CUR special */
2883 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2884 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2885 winfocus
= r
->ws
->focus
;
2886 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2887 winfocus
= r
->ws
->focus_prev
;
2889 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2890 if (winfocus
->iconic
== 0)
2893 focus_magic(winfocus
);
2897 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2901 if (TAILQ_EMPTY(wl
))
2903 /* make sure there is at least one uniconified window */
2905 TAILQ_FOREACH(winfocus
, wl
, entry
)
2906 if (winfocus
->iconic
== 0) {
2914 case SWM_ARG_ID_FOCUSPREV
:
2915 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2917 head
= TAILQ_LAST(wl
, ws_win_list
);
2919 if (WINID(winfocus
) == cur_focus
->transient
) {
2920 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2922 head
= TAILQ_LAST(wl
, ws_win_list
);
2927 if (winfocus
&& winfocus
->iconic
) {
2928 while (winfocus
!= cur_focus
) {
2929 if (winfocus
== NULL
)
2930 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2931 if (winfocus
->iconic
== 0)
2933 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
2939 case SWM_ARG_ID_FOCUSNEXT
:
2940 head
= TAILQ_NEXT(cur_focus
, entry
);
2942 head
= TAILQ_FIRST(wl
);
2946 if (winfocus
&& winfocus
->iconic
) {
2947 while (winfocus
!= cur_focus
) {
2948 if (winfocus
== NULL
)
2949 winfocus
= TAILQ_FIRST(wl
);
2950 if (winfocus
->iconic
== 0)
2952 winfocus
= TAILQ_NEXT(winfocus
, entry
);
2957 case SWM_ARG_ID_FOCUSMAIN
:
2958 winfocus
= TAILQ_FIRST(wl
);
2959 if (winfocus
== cur_focus
)
2960 winfocus
= cur_focus
->ws
->focus_prev
;
2967 focus_magic(winfocus
);
2971 cycle_layout(struct swm_region
*r
, union arg
*args
)
2973 struct workspace
*ws
= r
->ws
;
2976 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2979 if (ws
->cur_layout
->l_stack
== NULL
)
2980 ws
->cur_layout
= &layouts
[0];
2983 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2984 drain_enter_notify();
2985 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2990 stack_config(struct swm_region
*r
, union arg
*args
)
2992 struct workspace
*ws
= r
->ws
;
2994 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
2997 if (ws
->cur_layout
->l_config
!= NULL
)
2998 ws
->cur_layout
->l_config(ws
, args
->id
);
3000 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3007 struct swm_geometry g
;
3008 struct swm_region
*r
;
3014 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3016 for (i
= 0; i
< ScreenCount(display
); i
++) {
3020 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3021 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3022 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3024 /* start with screen geometry, adjust for bar */
3026 g
.w
-= 2 * border_width
;
3027 g
.h
-= 2 * border_width
;
3033 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3034 r
->ws
->cur_layout
->l_string(r
->ws
);
3035 /* save r so we can track region changes */
3042 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3043 drain_enter_notify();
3045 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3049 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3051 /* retain window geom and region geom */
3052 win
->g_float
= win
->g
;
3053 win
->g_float
.x
-= X(r
);
3054 win
->g_float
.y
-= Y(r
);
3055 win
->g_floatvalid
= 1;
3056 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%lx, g: (%d,%d)"
3057 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3058 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3059 win
->g_float
.w
, win
->g_float
.h
);
3063 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3068 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%lx\n", win
->id
);
3071 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3072 * geom on ws switches or return from max mode
3074 if (win
->g_floatvalid
&& (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3075 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)))) {
3076 /* refloat at last floating relative position */
3077 win
->g
= win
->g_float
;
3082 win
->floatmaxed
= 0;
3085 * if set to fullscreen mode, configure window to maximum size.
3087 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3088 if (!win
->g_floatvalid
)
3089 store_float_geom(win
, win
->ws
->r
);
3091 win
->g
= win
->ws
->r
->g
;
3095 * remove border on fullscreen floater when in fullscreen mode or when
3096 * the quirk is present.
3098 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3099 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3100 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3101 if (win
->bordered
) {
3103 X(win
) += border_width
;
3104 Y(win
) += border_width
;
3106 } else if (!win
->bordered
) {
3108 X(win
) -= border_width
;
3109 Y(win
) -= border_width
;
3112 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3113 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3114 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3119 * floaters and transients are auto-centred unless moved
3122 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3123 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3126 /* keep window within region bounds */
3127 constrain_window(win
, r
, 0);
3133 * Send keystrokes to terminal to decrease/increase the font size as the
3134 * window size changes.
3137 adjust_font(struct ws_win
*win
)
3139 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3140 win
->floating
|| win
->transient
)
3143 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3144 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3145 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3146 win
->font_size_boundary
[win
->font_steps
] =
3147 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3150 win
->last_inc
= win
->sh
.width_inc
;
3151 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
3152 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3153 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3156 win
->last_inc
= win
->sh
.width_inc
;
3157 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
3161 #define SWAPXY(g) do { \
3163 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3164 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3167 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3169 XWindowAttributes wa
;
3170 struct swm_geometry win_g
, r_g
= *g
;
3171 struct ws_win
*win
, *fs_win
= NULL
;
3172 int i
, j
, s
, stacks
;
3173 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3174 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3175 int split
, colno
, winno
, mwin
, msize
, mscale
;
3176 int remain
, missing
, v_slice
, reconfigure
;
3179 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3180 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3182 winno
= count_win(ws
, 0);
3183 if (winno
== 0 && count_win(ws
, 1) == 0)
3186 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3187 if (win
->transient
== 0 && win
->floating
== 0
3188 && win
->iconic
== 0)
3195 w_inc
= win
->sh
.width_inc
;
3196 w_base
= win
->sh
.base_width
;
3197 mwin
= ws
->l_state
.horizontal_mwin
;
3198 mscale
= ws
->l_state
.horizontal_msize
;
3199 stacks
= ws
->l_state
.horizontal_stacks
;
3202 w_inc
= win
->sh
.height_inc
;
3203 w_base
= win
->sh
.base_height
;
3204 mwin
= ws
->l_state
.vertical_mwin
;
3205 mscale
= ws
->l_state
.vertical_msize
;
3206 stacks
= ws
->l_state
.vertical_stacks
;
3210 if (stacks
> winno
- mwin
)
3211 stacks
= winno
- mwin
;
3215 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3216 if (mwin
&& winno
> mwin
) {
3217 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3221 win_g
.w
= v_slice
* mscale
;
3223 if (w_inc
> 1 && w_inc
< v_slice
) {
3224 /* adjust for window's requested size increment */
3225 remain
= (win_g
.w
- w_base
) % w_inc
;
3231 win_g
.x
+= r_g
.w
- msize
;
3234 colno
= split
= winno
/ stacks
;
3235 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3236 2 * border_width
) / stacks
);
3238 hrh
= r_g
.h
/ colno
;
3239 extra
= r_g
.h
- (colno
* hrh
);
3240 win_g
.h
= hrh
- 2 * border_width
;
3242 /* stack all the tiled windows */
3243 i
= j
= 0, s
= stacks
;
3244 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3245 if (win
->transient
!= 0 || win
->floating
!= 0)
3247 if (win
->iconic
!= 0)
3250 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3255 if (split
&& i
== split
) {
3256 colno
= (winno
- mwin
) / stacks
;
3257 if (s
<= (winno
- mwin
) % stacks
)
3259 split
= split
+ colno
;
3260 hrh
= (r_g
.h
/ colno
);
3261 extra
= r_g
.h
- (colno
* hrh
);
3265 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3266 win_g
.w
= (r_g
.w
- msize
-
3267 (stacks
* 2 * border_width
)) / stacks
;
3269 win_g
.w
+= (r_g
.w
- msize
-
3270 (stacks
* 2 * border_width
)) % stacks
;
3274 win_g
.h
= hrh
- 2 * border_width
;
3276 h_inc
= win
->sh
.width_inc
;
3277 h_base
= win
->sh
.base_width
;
3279 h_inc
= win
->sh
.height_inc
;
3280 h_base
= win
->sh
.base_height
;
3282 if (j
== colno
- 1) {
3283 win_g
.h
= hrh
+ extra
;
3284 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3285 /* adjust for window's requested size increment */
3286 remain
= (win_g
.h
- h_base
) % h_inc
;
3287 missing
= h_inc
- remain
;
3289 if (missing
<= extra
|| j
== 0) {
3301 win_g
.y
+= last_h
+ 2 * border_width
;
3303 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3305 win_g
.w
+= 2 * border_width
;
3306 win_g
.h
+= 2 * border_width
;
3311 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3312 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3316 WIDTH(win
) = win_g
.h
;
3317 HEIGHT(win
) = win_g
.w
;
3320 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3321 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3325 WIDTH(win
) = win_g
.w
;
3326 HEIGHT(win
) = win_g
.h
;
3330 if (bordered
!= win
->bordered
) {
3332 win
->bordered
= bordered
;
3340 if (XGetWindowAttributes(display
, win
->id
, &wa
))
3341 if (wa
.map_state
== IsUnmapped
)
3342 XMapRaised(display
, win
->id
);
3350 /* now, stack all the floaters and transients */
3351 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3352 if (win
->transient
== 0 && win
->floating
== 0)
3354 if (win
->iconic
== 1)
3356 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3361 stack_floater(win
, ws
->r
);
3362 XMapRaised(display
, win
->id
);
3366 stack_floater(fs_win
, ws
->r
);
3367 XMapRaised(display
, fs_win
->id
);
3372 vertical_config(struct workspace
*ws
, int id
)
3374 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3378 case SWM_ARG_ID_STACKRESET
:
3379 case SWM_ARG_ID_STACKINIT
:
3380 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3381 ws
->l_state
.vertical_mwin
= 1;
3382 ws
->l_state
.vertical_stacks
= 1;
3384 case SWM_ARG_ID_MASTERSHRINK
:
3385 if (ws
->l_state
.vertical_msize
> 1)
3386 ws
->l_state
.vertical_msize
--;
3388 case SWM_ARG_ID_MASTERGROW
:
3389 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3390 ws
->l_state
.vertical_msize
++;
3392 case SWM_ARG_ID_MASTERADD
:
3393 ws
->l_state
.vertical_mwin
++;
3395 case SWM_ARG_ID_MASTERDEL
:
3396 if (ws
->l_state
.vertical_mwin
> 0)
3397 ws
->l_state
.vertical_mwin
--;
3399 case SWM_ARG_ID_STACKINC
:
3400 ws
->l_state
.vertical_stacks
++;
3402 case SWM_ARG_ID_STACKDEC
:
3403 if (ws
->l_state
.vertical_stacks
> 1)
3404 ws
->l_state
.vertical_stacks
--;
3406 case SWM_ARG_ID_FLIPLAYOUT
:
3407 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3415 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3417 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3419 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3423 horizontal_config(struct workspace
*ws
, int id
)
3425 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3428 case SWM_ARG_ID_STACKRESET
:
3429 case SWM_ARG_ID_STACKINIT
:
3430 ws
->l_state
.horizontal_mwin
= 1;
3431 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3432 ws
->l_state
.horizontal_stacks
= 1;
3434 case SWM_ARG_ID_MASTERSHRINK
:
3435 if (ws
->l_state
.horizontal_msize
> 1)
3436 ws
->l_state
.horizontal_msize
--;
3438 case SWM_ARG_ID_MASTERGROW
:
3439 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3440 ws
->l_state
.horizontal_msize
++;
3442 case SWM_ARG_ID_MASTERADD
:
3443 ws
->l_state
.horizontal_mwin
++;
3445 case SWM_ARG_ID_MASTERDEL
:
3446 if (ws
->l_state
.horizontal_mwin
> 0)
3447 ws
->l_state
.horizontal_mwin
--;
3449 case SWM_ARG_ID_STACKINC
:
3450 ws
->l_state
.horizontal_stacks
++;
3452 case SWM_ARG_ID_STACKDEC
:
3453 if (ws
->l_state
.horizontal_stacks
> 1)
3454 ws
->l_state
.horizontal_stacks
--;
3456 case SWM_ARG_ID_FLIPLAYOUT
:
3457 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3465 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3467 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3469 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3472 /* fullscreen view */
3474 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3476 struct swm_geometry gg
= *g
;
3477 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3480 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3485 winno
= count_win(ws
, 0);
3486 if (winno
== 0 && count_win(ws
, 1) == 0)
3489 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3490 if (win
->transient
) {
3492 parent
= find_window(win
->transient
);
3496 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3498 * retain geometry for retrieval on exit from
3501 store_float_geom(win
, ws
->r
);
3502 win
->floatmaxed
= 1;
3505 /* only reconfigure if necessary */
3506 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3507 HEIGHT(win
) != gg
.h
) {
3513 WIDTH(win
) += 2 * border_width
;
3514 HEIGHT(win
) += 2 * border_width
;
3519 /* unmap only if we don't have multi screen */
3520 if (win
!= ws
->focus
)
3521 if (!(ScreenCount(display
) > 1 || outputs
> 1))
3525 /* put the last transient on top */
3528 XMapRaised(display
, parent
->id
);
3529 stack_floater(wintrans
, ws
->r
);
3530 focus_magic(wintrans
);
3535 send_to_ws(struct swm_region
*r
, union arg
*args
)
3537 int wsid
= args
->id
;
3538 struct ws_win
*win
= NULL
, *parent
;
3539 struct workspace
*ws
, *nws
;
3540 Atom ws_idx_atom
= 0;
3541 unsigned char ws_idx_str
[SWM_PROPLEN
];
3544 if (wsid
>= workspace_limit
)
3547 if (r
&& r
->ws
&& r
->ws
->focus
)
3553 if (win
->ws
->idx
== wsid
)
3556 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%lx\n", win
->id
);
3559 nws
= &win
->s
->ws
[wsid
];
3561 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3563 if (win
->transient
) {
3564 parent
= find_window(win
->transient
);
3566 unmap_window(parent
);
3567 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3568 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3573 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3574 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3575 if (TAILQ_EMPTY(&ws
->winlist
))
3576 r
->ws
->focus
= NULL
;
3579 /* Try to update the window's workspace property */
3580 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
3582 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3584 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3586 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
3587 PropModeReplace
, ws_idx_str
, strlen((char *)ws_idx_str
));
3595 pressbutton(struct swm_region
*r
, union arg
*args
)
3597 XTestFakeButtonEvent(display
, args
->id
, True
, CurrentTime
);
3598 XTestFakeButtonEvent(display
, args
->id
, False
, CurrentTime
);
3602 raise_toggle(struct swm_region
*r
, union arg
*args
)
3604 if (r
== NULL
|| r
->ws
== NULL
)
3607 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3609 /* bring floaters back to top */
3610 if (r
->ws
->always_raise
== 0)
3615 iconify(struct swm_region
*r
, union arg
*args
)
3619 if (r
->ws
->focus
== NULL
)
3621 unmap_window(r
->ws
->focus
);
3622 update_iconic(r
->ws
->focus
, 1);
3624 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3625 drain_enter_notify();
3626 r
->ws
->focus
= NULL
;
3627 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3632 get_win_name(Window win
)
3634 unsigned char *prop
= NULL
;
3635 unsigned long nbytes
, nitems
;
3637 /* try _NET_WM_NAME first */
3638 if (get_property(win
, a_netwmname
, 0L, a_utf8_string
, NULL
, &nbytes
,
3641 if (get_property(win
, a_netwmname
, nbytes
, a_utf8_string
,
3642 &nitems
, NULL
, &prop
))
3646 /* fallback to WM_NAME */
3647 if (!get_property(win
, a_wmname
, 0L, a_string
, NULL
, &nbytes
, &prop
))
3650 if (get_property(win
, a_wmname
, nbytes
, a_string
, &nitems
, NULL
, &prop
))
3657 uniconify(struct swm_region
*r
, union arg
*args
)
3661 unsigned char *name
;
3664 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3666 if (r
== NULL
|| r
->ws
== NULL
)
3669 /* make sure we have anything to uniconify */
3670 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3671 if (win
->ws
== NULL
)
3672 continue; /* should never happen */
3673 if (win
->iconic
== 0)
3681 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3683 spawn_select(r
, args
, "search", &searchpid
);
3685 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3688 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3689 if (win
->ws
== NULL
)
3690 continue; /* should never happen */
3691 if (win
->iconic
== 0)
3694 name
= get_win_name(win
->id
);
3697 fprintf(lfile
, "%s.%lu\n", name
, win
->id
);
3705 name_workspace(struct swm_region
*r
, union arg
*args
)
3709 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3715 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3717 spawn_select(r
, args
, "name_workspace", &searchpid
);
3719 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3722 fprintf(lfile
, "%s", "");
3727 search_workspace(struct swm_region
*r
, union arg
*args
)
3730 struct workspace
*ws
;
3733 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3739 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3741 spawn_select(r
, args
, "search", &searchpid
);
3743 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3746 for (i
= 0; i
< workspace_limit
; i
++) {
3750 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3751 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3758 search_win_cleanup(void)
3760 struct search_window
*sw
= NULL
;
3762 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3763 XDestroyWindow(display
, sw
->indicator
);
3764 XFreeGC(display
, sw
->gc
);
3765 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3771 search_win(struct swm_region
*r
, union arg
*args
)
3773 struct ws_win
*win
= NULL
;
3774 struct search_window
*sw
= NULL
;
3781 XRectangle ibox
, lbox
;
3783 DNPRINTF(SWM_D_MISC
, "search_win\n");
3786 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3788 spawn_select(r
, args
, "search", &searchpid
);
3790 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3793 TAILQ_INIT(&search_wl
);
3796 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3797 if (win
->iconic
== 1)
3800 sw
= calloc(1, sizeof(struct search_window
));
3802 warn("search_win: calloc");
3804 search_win_cleanup();
3810 snprintf(s
, sizeof s
, "%d", i
);
3813 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
3815 w
= XCreateSimpleWindow(display
,
3816 win
->id
, 0, 0,lbox
.width
+ 4,
3817 bar_fs_extents
->max_logical_extent
.height
, 1,
3818 r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
,
3819 r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
3822 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3824 sw
->gc
= XCreateGC(display
, w
, 0, &gcv
);
3825 XMapRaised(display
, w
);
3826 XSetForeground(display
, sw
->gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
3828 DRAWSTRING(display
, w
, bar_fs
, sw
->gc
, 2,
3829 (bar_fs_extents
->max_logical_extent
.height
-
3830 lbox
.height
) / 2 - lbox
.y
, s
, len
);
3832 fprintf(lfile
, "%d\n", i
);
3840 search_resp_uniconify(char *resp
, unsigned long len
)
3842 unsigned char *name
;
3846 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3848 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3849 if (win
->iconic
== 0)
3851 name
= get_win_name(win
->id
);
3854 if (asprintf(&s
, "%s.%lu", name
, win
->id
) == -1) {
3859 if (strncmp(s
, resp
, len
) == 0) {
3860 /* XXX this should be a callback to generalize */
3861 update_iconic(win
, 0);
3870 search_resp_name_workspace(char *resp
, unsigned long len
)
3872 struct workspace
*ws
;
3874 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3876 if (search_r
->ws
== NULL
)
3881 free(search_r
->ws
->name
);
3882 search_r
->ws
->name
= NULL
;
3886 ws
->name
= strdup(resp
);
3887 if (ws
->name
== NULL
) {
3888 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
3889 "strdup: %s", strerror(errno
));
3896 search_resp_search_workspace(char *resp
, unsigned long len
)
3903 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
3907 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
3914 ws_idx
= strtonum(q
, 1, workspace_limit
, &errstr
);
3916 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
3923 switchws(search_r
, &a
);
3927 search_resp_search_window(char *resp
, unsigned long len
)
3932 struct search_window
*sw
;
3934 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
3938 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
3943 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
3945 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
3952 TAILQ_FOREACH(sw
, &search_wl
, entry
)
3953 if (idx
== sw
->idx
) {
3959 #define MAX_RESP_LEN 1024
3962 search_do_resp(void)
3968 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
3973 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
3974 warn("search: calloc");
3978 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
3980 warn("search: read error");
3983 resp
[rbytes
] = '\0';
3986 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3987 * newline, so work around that by sanitizing the resp now.
3989 resp
[strcspn(resp
, "\n")] = '\0';
3992 switch (search_resp_action
) {
3993 case SWM_SEARCH_UNICONIFY
:
3994 search_resp_uniconify(resp
, len
);
3996 case SWM_SEARCH_NAME_WORKSPACE
:
3997 search_resp_name_workspace(resp
, len
);
3999 case SWM_SEARCH_SEARCH_WORKSPACE
:
4000 search_resp_search_workspace(resp
, len
);
4002 case SWM_SEARCH_SEARCH_WINDOW
:
4003 search_resp_search_window(resp
, len
);
4008 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4009 search_win_cleanup();
4011 search_resp_action
= SWM_SEARCH_NONE
;
4012 close(select_resp_pipe
[0]);
4017 wkill(struct swm_region
*r
, union arg
*args
)
4019 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4021 if (r
->ws
->focus
== NULL
)
4024 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4025 XKillClient(display
, r
->ws
->focus
->id
);
4027 if (r
->ws
->focus
->can_delete
)
4028 client_msg(r
->ws
->focus
, adelete
);
4033 floating_toggle_win(struct ws_win
*win
)
4035 struct swm_region
*r
;
4045 /* reject floating toggles in max stack mode */
4046 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4049 if (win
->floating
) {
4050 if (!win
->floatmaxed
) {
4051 /* retain position for refloat */
4052 store_float_geom(win
, r
);
4056 if (win
->g_floatvalid
) {
4057 /* refloat at last floating relative position */
4058 X(win
) = win
->g_float
.x
+ X(r
);
4059 Y(win
) = win
->g_float
.y
+ Y(r
);
4060 WIDTH(win
) = win
->g_float
.w
;
4061 HEIGHT(win
) = win
->g_float
.h
;
4066 ewmh_update_actions(win
);
4072 floating_toggle(struct swm_region
*r
, union arg
*args
)
4074 struct ws_win
*win
= r
->ws
->focus
;
4080 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4081 _NET_WM_STATE_TOGGLE
);
4084 if (focus_mode
== SWM_FOCUS_DEFAULT
)
4085 drain_enter_notify();
4087 if (win
== win
->ws
->focus
) {
4088 a
.id
= SWM_ARG_ID_FOCUSCUR
;
4089 focus(win
->ws
->r
, &a
);
4094 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4096 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4098 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4100 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4103 if (X(win
) + BORDER(win
) < X(r
)) {
4105 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4107 X(win
) = X(r
) - BORDER(win
);
4110 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4112 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4114 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4117 if (Y(win
) + BORDER(win
) < Y(r
)) {
4119 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4121 Y(win
) = Y(r
) - BORDER(win
);
4127 if (HEIGHT(win
) < 1)
4133 update_window(struct ws_win
*win
)
4138 bzero(&wc
, sizeof wc
);
4139 mask
= CWBorderWidth
| CWWidth
| CWHeight
| CWX
| CWY
;
4141 wc
.border_width
= BORDER(win
);
4144 wc
.width
= WIDTH(win
);
4145 wc
.height
= HEIGHT(win
);
4147 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%lx, (x,y) w x h: "
4148 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
.x
, wc
.y
, wc
.width
,
4149 wc
.height
, YESNO(win
->bordered
));
4151 XConfigureWindow(display
, win
->id
, mask
, &wc
);
4154 #define SWM_RESIZE_STEPS (50)
4157 resize(struct ws_win
*win
, union arg
*args
)
4161 struct swm_region
*r
= NULL
;
4162 int resize_step
= 0;
4166 struct swm_geometry g
;
4167 int top
= 0, left
= 0;
4170 unsigned int shape
; /* cursor style */
4176 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4179 DNPRINTF(SWM_D_MOUSE
, "resize: window: 0x%lx, floating: %s, "
4180 "transient: 0x%lx\n", win
->id
, YESNO(win
->floating
),
4183 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4186 /* reject resizes in max mode for floaters (transient ok) */
4187 if (win
->floatmaxed
)
4191 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4197 case SWM_ARG_ID_WIDTHSHRINK
:
4198 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4201 case SWM_ARG_ID_WIDTHGROW
:
4202 WIDTH(win
) += SWM_RESIZE_STEPS
;
4205 case SWM_ARG_ID_HEIGHTSHRINK
:
4206 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4209 case SWM_ARG_ID_HEIGHTGROW
:
4210 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4217 constrain_window(win
, r
, 1);
4219 store_float_geom(win
,r
);
4223 if (focus_mode
== SWM_FOCUS_DEFAULT
)
4224 drain_enter_notify();
4226 /* get cursor offset from window root */
4227 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4232 if (wx
< WIDTH(win
) / 2)
4235 if (wy
< HEIGHT(win
) / 2)
4238 if (args
->id
== SWM_ARG_ID_CENTER
)
4241 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
4243 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
4245 cursor
= XCreateFontCursor(display
, shape
);
4247 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4248 GrabModeAsync
, None
, cursor
, CurrentTime
) != GrabSuccess
) {
4249 XFreeCursor(display
, cursor
);
4254 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4255 SubstructureRedirectMask
, &ev
);
4257 case ConfigureRequest
:
4260 handler
[ev
.type
](&ev
);
4263 /* cursor offset/delta from start of the operation */
4264 dx
= ev
.xmotion
.x_root
- x
;
4265 dy
= ev
.xmotion
.y_root
- y
;
4271 if (args
->id
== SWM_ARG_ID_CENTER
) {
4272 if (g
.h
/ 2 + dy
< 1)
4276 HEIGHT(win
) = g
.h
+ 2 * dy
;
4284 HEIGHT(win
) = g
.h
+ dy
;
4291 if (args
->id
== SWM_ARG_ID_CENTER
) {
4292 if (g
.w
/ 2 + dx
< 1)
4296 WIDTH(win
) = g
.w
+ 2 * dx
;
4304 WIDTH(win
) = g
.w
+ dx
;
4307 constrain_window(win
, r
, 1);
4309 /* not free, don't sync more than 120 times / second */
4310 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4311 time
= ev
.xmotion
.time
;
4312 XSync(display
, False
);
4317 } while (ev
.type
!= ButtonRelease
);
4319 XSync(display
, False
);
4322 store_float_geom(win
,r
);
4324 XUngrabPointer(display
, CurrentTime
);
4325 XFreeCursor(display
, cursor
);
4328 drain_enter_notify();
4332 resize_step(struct swm_region
*r
, union arg
*args
)
4334 struct ws_win
*win
= NULL
;
4336 if (r
&& r
->ws
&& r
->ws
->focus
)
4344 #define SWM_MOVE_STEPS (50)
4347 move(struct ws_win
*win
, union arg
*args
)
4352 struct swm_region
*r
= NULL
;
4362 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4365 DNPRINTF(SWM_D_MOUSE
, "move: window: 0x%lx, floating: %s, transient: "
4366 "0x%lx\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4368 /* in max_stack mode should only move transients */
4369 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4373 if (win
->floating
== 0 && !win
->transient
) {
4374 store_float_geom(win
, r
);
4375 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4378 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4385 case SWM_ARG_ID_MOVELEFT
:
4386 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4389 case SWM_ARG_ID_MOVERIGHT
:
4390 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4393 case SWM_ARG_ID_MOVEUP
:
4394 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4397 case SWM_ARG_ID_MOVEDOWN
:
4398 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4405 constrain_window(win
, r
, 0);
4407 store_float_geom(win
, r
);
4411 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4412 GrabModeAsync
, None
, XCreateFontCursor(display
, XC_fleur
),
4413 CurrentTime
) != GrabSuccess
)
4416 /* get cursor offset from window root */
4417 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4421 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4422 SubstructureRedirectMask
, &ev
);
4424 case ConfigureRequest
:
4427 handler
[ev
.type
](&ev
);
4430 X(win
) = ev
.xmotion
.x_root
- wx
- border_width
;
4431 Y(win
) = ev
.xmotion
.y_root
- wy
- border_width
;
4433 constrain_window(win
, r
, 0);
4435 /* not free, don't sync more than 120 times / second */
4436 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4437 time
= ev
.xmotion
.time
;
4438 XSync(display
, False
);
4443 } while (ev
.type
!= ButtonRelease
);
4445 XSync(display
, False
);
4448 store_float_geom(win
, r
);
4449 XUngrabPointer(display
, CurrentTime
);
4452 drain_enter_notify();
4456 move_step(struct swm_region
*r
, union arg
*args
)
4458 struct ws_win
*win
= NULL
;
4460 if (r
&& r
->ws
&& r
->ws
->focus
)
4465 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4472 /* user/key callable function IDs */
4561 kf_search_workspace
,
4563 kf_dumpwins
, /* MUST BE LAST */
4567 /* key definitions */
4569 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4574 char name
[SWM_FUNCNAME_LEN
];
4575 void (*func
)(struct swm_region
*r
, union arg
*);
4577 } keyfuncs
[kf_invalid
+ 1] = {
4578 /* name function argument */
4579 { "cycle_layout", cycle_layout
, {0} },
4580 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4581 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4582 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4583 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4584 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4585 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4586 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4587 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4588 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4589 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4590 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4591 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4592 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4593 { "quit", quit
, {0} },
4594 { "restart", restart
, {0} },
4595 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4596 { "ws_1", switchws
, {.id
= 0} },
4597 { "ws_2", switchws
, {.id
= 1} },
4598 { "ws_3", switchws
, {.id
= 2} },
4599 { "ws_4", switchws
, {.id
= 3} },
4600 { "ws_5", switchws
, {.id
= 4} },
4601 { "ws_6", switchws
, {.id
= 5} },
4602 { "ws_7", switchws
, {.id
= 6} },
4603 { "ws_8", switchws
, {.id
= 7} },
4604 { "ws_9", switchws
, {.id
= 8} },
4605 { "ws_10", switchws
, {.id
= 9} },
4606 { "ws_11", switchws
, {.id
= 10} },
4607 { "ws_12", switchws
, {.id
= 11} },
4608 { "ws_13", switchws
, {.id
= 12} },
4609 { "ws_14", switchws
, {.id
= 13} },
4610 { "ws_15", switchws
, {.id
= 14} },
4611 { "ws_16", switchws
, {.id
= 15} },
4612 { "ws_17", switchws
, {.id
= 16} },
4613 { "ws_18", switchws
, {.id
= 17} },
4614 { "ws_19", switchws
, {.id
= 18} },
4615 { "ws_20", switchws
, {.id
= 19} },
4616 { "ws_21", switchws
, {.id
= 20} },
4617 { "ws_22", switchws
, {.id
= 21} },
4618 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4619 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4620 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4621 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4622 { "ws_prior", priorws
, {0} },
4623 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4624 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4625 { "mvws_1", send_to_ws
, {.id
= 0} },
4626 { "mvws_2", send_to_ws
, {.id
= 1} },
4627 { "mvws_3", send_to_ws
, {.id
= 2} },
4628 { "mvws_4", send_to_ws
, {.id
= 3} },
4629 { "mvws_5", send_to_ws
, {.id
= 4} },
4630 { "mvws_6", send_to_ws
, {.id
= 5} },
4631 { "mvws_7", send_to_ws
, {.id
= 6} },
4632 { "mvws_8", send_to_ws
, {.id
= 7} },
4633 { "mvws_9", send_to_ws
, {.id
= 8} },
4634 { "mvws_10", send_to_ws
, {.id
= 9} },
4635 { "mvws_11", send_to_ws
, {.id
= 10} },
4636 { "mvws_12", send_to_ws
, {.id
= 11} },
4637 { "mvws_13", send_to_ws
, {.id
= 12} },
4638 { "mvws_14", send_to_ws
, {.id
= 13} },
4639 { "mvws_15", send_to_ws
, {.id
= 14} },
4640 { "mvws_16", send_to_ws
, {.id
= 15} },
4641 { "mvws_17", send_to_ws
, {.id
= 16} },
4642 { "mvws_18", send_to_ws
, {.id
= 17} },
4643 { "mvws_19", send_to_ws
, {.id
= 18} },
4644 { "mvws_20", send_to_ws
, {.id
= 19} },
4645 { "mvws_21", send_to_ws
, {.id
= 20} },
4646 { "mvws_22", send_to_ws
, {.id
= 21} },
4647 { "bar_toggle", bar_toggle
, {0} },
4648 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4649 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4650 { "float_toggle", floating_toggle
,{0} },
4651 { "version", version
, {0} },
4652 { "spawn_custom", dummykeyfunc
, {0} },
4653 { "iconify", iconify
, {0} },
4654 { "uniconify", uniconify
, {0} },
4655 { "raise_toggle", raise_toggle
, {0} },
4656 { "button2", pressbutton
, {2} },
4657 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4658 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4659 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4660 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4661 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4662 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4663 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4664 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4665 { "name_workspace", name_workspace
, {0} },
4666 { "search_workspace", search_workspace
, {0} },
4667 { "search_win", search_win
, {0} },
4668 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4669 { "invalid key func", NULL
, {0} },
4672 RB_ENTRY(key
) entry
;
4675 enum keyfuncid funcid
;
4678 RB_HEAD(key_tree
, key
);
4681 key_cmp(struct key
*kp1
, struct key
*kp2
)
4683 if (kp1
->keysym
< kp2
->keysym
)
4685 if (kp1
->keysym
> kp2
->keysym
)
4688 if (kp1
->mod
< kp2
->mod
)
4690 if (kp1
->mod
> kp2
->mod
)
4696 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
4697 struct key_tree keys
;
4700 enum { client_click
, root_click
};
4702 unsigned int action
;
4704 unsigned int button
;
4705 void (*func
)(struct ws_win
*, union arg
*);
4708 /* action key mouse button func args */
4709 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4710 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4711 { client_click
, MODKEY
, Button1
, move
, {0} },
4715 update_modkey(unsigned int mod
)
4721 RB_FOREACH(kp
, key_tree
, &keys
)
4722 if (kp
->mod
& ShiftMask
)
4723 kp
->mod
= mod
| ShiftMask
;
4727 for (i
= 0; i
< LENGTH(buttons
); i
++)
4728 if (buttons
[i
].mask
& ShiftMask
)
4729 buttons
[i
].mask
= mod
| ShiftMask
;
4731 buttons
[i
].mask
= mod
;
4736 TAILQ_ENTRY(spawn_prog
) entry
;
4741 TAILQ_HEAD(spawn_list
, spawn_prog
);
4742 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4745 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4748 struct spawn_prog
*prog
= NULL
;
4750 char *ap
, **real_args
;
4752 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4755 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4756 if (!strcasecmp(spawn_name
, prog
->name
))
4760 warnx("spawn_custom: program %s not found", spawn_name
);
4764 /* make room for expanded args */
4765 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4766 err(1, "spawn_custom: calloc real_args");
4768 /* expand spawn_args into real_args */
4769 for (i
= 0; i
< prog
->argc
; i
++) {
4771 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4772 if (!strcasecmp(ap
, "$bar_border")) {
4774 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4776 err(1, "spawn_custom border color");
4777 } else if (!strcasecmp(ap
, "$bar_color")) {
4779 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4781 err(1, "spawn_custom bar color");
4782 } else if (!strcasecmp(ap
, "$bar_font")) {
4783 if ((real_args
[i
] = strdup(bar_fonts
))
4785 err(1, "spawn_custom bar fonts");
4786 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4788 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4790 err(1, "spawn_custom color font");
4791 } else if (!strcasecmp(ap
, "$color_focus")) {
4793 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4795 err(1, "spawn_custom color focus");
4796 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4798 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4800 err(1, "spawn_custom color unfocus");
4802 /* no match --> copy as is */
4803 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4804 err(1, "spawn_custom strdup(ap)");
4806 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4811 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4812 for (i
= 0; i
< prog
->argc
; i
++)
4813 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4814 DNPRINTF(SWM_D_SPAWN
, "\n");
4816 *ret_args
= real_args
;
4817 return (prog
->argc
);
4821 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4827 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4831 spawn(r
->ws
->idx
, &a
, 1);
4833 for (i
= 0; i
< spawn_argc
; i
++)
4839 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4845 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4849 if (pipe(select_list_pipe
) == -1)
4850 err(1, "pipe error");
4851 if (pipe(select_resp_pipe
) == -1)
4852 err(1, "pipe error");
4854 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4855 err(1, "could not disable SIGPIPE");
4856 switch (*pid
= fork()) {
4858 err(1, "cannot fork");
4861 if (dup2(select_list_pipe
[0], 0) == -1)
4863 if (dup2(select_resp_pipe
[1], 1) == -1)
4865 close(select_list_pipe
[1]);
4866 close(select_resp_pipe
[0]);
4867 spawn(r
->ws
->idx
, &a
, 0);
4869 default: /* parent */
4870 close(select_list_pipe
[0]);
4871 close(select_resp_pipe
[1]);
4875 for (i
= 0; i
< spawn_argc
; i
++)
4881 spawn_insert(char *name
, char *args
)
4883 char *arg
, *cp
, *ptr
;
4884 struct spawn_prog
*sp
;
4886 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
4888 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
4889 err(1, "spawn_insert: malloc");
4890 if ((sp
->name
= strdup(name
)) == NULL
)
4891 err(1, "spawn_insert: strdup");
4893 /* convert the arguments to an argument list */
4894 if ((ptr
= cp
= strdup(args
)) == NULL
)
4895 err(1, "spawn_insert: strdup");
4896 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
4897 /* empty field; skip it */
4902 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
4903 sizeof *sp
->argv
)) == NULL
)
4904 err(1, "spawn_insert: realloc");
4905 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
4906 err(1, "spawn_insert: strdup");
4910 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
4911 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
4915 spawn_remove(struct spawn_prog
*sp
)
4919 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
4921 TAILQ_REMOVE(&spawns
, sp
, entry
);
4922 for (i
= 0; i
< sp
->argc
; i
++)
4928 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
4932 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
4934 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
4937 spawn_insert(name
, args
);
4939 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
4943 setspawn(char *name
, char *args
)
4945 struct spawn_prog
*sp
;
4947 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
4952 TAILQ_FOREACH(sp
, &spawns
, entry
) {
4953 if (!strcmp(sp
->name
, name
)) {
4957 spawn_replace(sp
, name
, args
);
4958 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
4962 if (*args
== '\0') {
4963 warnx("error: setspawn: cannot find program: %s", name
);
4967 spawn_insert(name
, args
);
4968 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
4972 setconfspawn(char *selector
, char *value
, int flags
)
4974 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
4976 setspawn(selector
, value
);
4978 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
4985 setconfspawn("term", "xterm", 0);
4986 setconfspawn("spawn_term", "xterm", 0);
4987 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4988 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4989 setconfspawn("lock", "xlock", 0);
4990 setconfspawn("initscr", "initscreen.sh", 0);
4991 setconfspawn("menu", "dmenu_run"
4994 " -nf $bar_font_color"
4996 " -sf $bar_color", 0);
4997 setconfspawn("search", "dmenu"
5001 " -nf $bar_font_color"
5003 " -sf $bar_color", 0);
5004 setconfspawn("name_workspace", "dmenu"
5008 " -nf $bar_font_color"
5010 " -sf $bar_color", 0);
5014 #define SWM_MODNAME_SIZE 32
5015 #define SWM_KEY_WS "\n+ \t"
5017 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5021 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5022 if (mod
== NULL
|| ks
== NULL
) {
5023 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5026 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5027 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5033 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5034 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5036 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5037 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5039 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5041 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5043 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5045 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5047 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5049 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5050 *mod
|= ControlMask
;
5052 *ks
= XStringToKeysym(name
);
5053 XConvertCase(*ks
, ks
, &uks
);
5054 if (ks
== NoSymbol
) {
5056 "parsekeys: invalid key %s\n",
5062 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5067 strdupsafe(char *str
)
5072 return (strdup(str
));
5076 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5080 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5081 keyfuncs
[kfid
].name
, spawn_name
);
5083 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5084 err(1, "key_insert: malloc");
5089 kp
->spawn_name
= strdupsafe(spawn_name
);
5090 RB_INSERT(key_tree
, &keys
, kp
);
5092 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5096 key_lookup(unsigned int mod
, KeySym ks
)
5103 return (RB_FIND(key_tree
, &keys
, &kp
));
5107 key_remove(struct key
*kp
)
5109 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5111 RB_REMOVE(key_tree
, &keys
, kp
);
5112 free(kp
->spawn_name
);
5115 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5119 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5122 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5126 key_insert(mod
, ks
, kfid
, spawn_name
);
5128 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5132 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5137 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5138 keyfuncs
[kfid
].name
, spawn_name
);
5140 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5141 if (kfid
== kf_invalid
)
5144 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5145 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5148 if (kfid
== kf_invalid
) {
5149 warnx("error: setkeybinding: cannot find mod/key combination");
5150 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5154 key_insert(mod
, ks
, kfid
, spawn_name
);
5155 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5159 setconfbinding(char *selector
, char *value
, int flags
)
5161 enum keyfuncid kfid
;
5164 struct spawn_prog
*sp
;
5165 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5166 if (selector
== NULL
) {
5167 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5168 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5170 setkeybinding(mod
, ks
, kfid
, NULL
);
5175 /* search by key function name */
5176 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
5177 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5178 SWM_FUNCNAME_LEN
) == 0) {
5179 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5181 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5182 setkeybinding(mod
, ks
, kfid
, NULL
);
5188 /* search by custom spawn name */
5189 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5190 if (strcasecmp(selector
, sp
->name
) == 0) {
5191 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5193 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5194 setkeybinding(mod
, ks
, kf_spawn_custom
,
5201 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5208 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
5209 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, kf_flip_layout
, NULL
);
5210 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
5211 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
5212 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
5213 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
5214 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
5215 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
5216 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
5217 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
5218 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
5219 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
5220 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
5221 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
5222 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_custom
,"term");
5223 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
5224 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
5225 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
5226 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
5227 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
5228 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
5229 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
5230 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
5231 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
5232 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
5233 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
5234 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
5235 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
5236 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
5237 setkeybinding(MODKEY
, XK_F1
, kf_ws_11
, NULL
);
5238 setkeybinding(MODKEY
, XK_F2
, kf_ws_12
, NULL
);
5239 setkeybinding(MODKEY
, XK_F3
, kf_ws_13
, NULL
);
5240 setkeybinding(MODKEY
, XK_F4
, kf_ws_14
, NULL
);
5241 setkeybinding(MODKEY
, XK_F5
, kf_ws_15
, NULL
);
5242 setkeybinding(MODKEY
, XK_F6
, kf_ws_16
, NULL
);
5243 setkeybinding(MODKEY
, XK_F7
, kf_ws_17
, NULL
);
5244 setkeybinding(MODKEY
, XK_F8
, kf_ws_18
, NULL
);
5245 setkeybinding(MODKEY
, XK_F9
, kf_ws_19
, NULL
);
5246 setkeybinding(MODKEY
, XK_F10
, kf_ws_20
, NULL
);
5247 setkeybinding(MODKEY
, XK_F11
, kf_ws_21
, NULL
);
5248 setkeybinding(MODKEY
, XK_F12
, kf_ws_22
, NULL
);
5249 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
5250 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
5251 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
5252 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
5253 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
5254 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
5255 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
5256 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
5257 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
5258 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
5259 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
5260 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
5261 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
5262 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
5263 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
5264 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
5265 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
5266 setkeybinding(MODKEY
|ShiftMask
, XK_F1
, kf_mvws_11
, NULL
);
5267 setkeybinding(MODKEY
|ShiftMask
, XK_F2
, kf_mvws_12
, NULL
);
5268 setkeybinding(MODKEY
|ShiftMask
, XK_F3
, kf_mvws_13
, NULL
);
5269 setkeybinding(MODKEY
|ShiftMask
, XK_F4
, kf_mvws_14
, NULL
);
5270 setkeybinding(MODKEY
|ShiftMask
, XK_F5
, kf_mvws_15
, NULL
);
5271 setkeybinding(MODKEY
|ShiftMask
, XK_F6
, kf_mvws_16
, NULL
);
5272 setkeybinding(MODKEY
|ShiftMask
, XK_F7
, kf_mvws_17
, NULL
);
5273 setkeybinding(MODKEY
|ShiftMask
, XK_F8
, kf_mvws_18
, NULL
);
5274 setkeybinding(MODKEY
|ShiftMask
, XK_F9
, kf_mvws_19
, NULL
);
5275 setkeybinding(MODKEY
|ShiftMask
, XK_F10
, kf_mvws_20
, NULL
);
5276 setkeybinding(MODKEY
|ShiftMask
, XK_F11
, kf_mvws_21
, NULL
);
5277 setkeybinding(MODKEY
|ShiftMask
, XK_F12
, kf_mvws_22
, NULL
);
5278 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
5279 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
5280 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
5281 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
5282 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
5283 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
5284 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
5285 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
5286 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
5287 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
5288 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
5289 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
5290 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
5291 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
5292 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
5293 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
5294 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
5295 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
5296 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
5297 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
5298 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
5299 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
5300 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
5301 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
5302 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
5303 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
5305 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
5314 while (RB_EMPTY(&keys
) == 0) {
5315 kp
= RB_ROOT(&keys
);
5321 setkeymapping(char *selector
, char *value
, int flags
)
5323 char keymapping_file
[PATH_MAX
];
5324 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5325 if (value
[0] == '~')
5326 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5327 pwd
->pw_dir
, &value
[1]);
5329 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5331 /* load new key bindings; if it fails, revert to default bindings */
5332 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5336 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5341 updatenumlockmask(void)
5344 XModifierKeymap
*modmap
;
5346 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
5348 modmap
= XGetModifierMapping(display
);
5349 for (i
= 0; i
< 8; i
++)
5350 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
5351 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
5352 == XKeysymToKeycode(display
, XK_Num_Lock
))
5353 numlockmask
= (1 << i
);
5355 XFreeModifiermap(modmap
);
5363 unsigned int modifiers
[] =
5364 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
5367 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5368 updatenumlockmask();
5370 for (k
= 0; k
< ScreenCount(display
); k
++) {
5371 if (TAILQ_EMPTY(&screens
[k
].rl
))
5373 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
5374 RB_FOREACH(kp
, key_tree
, &keys
) {
5375 if ((code
= XKeysymToKeycode(display
, kp
->keysym
)))
5376 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5377 XGrabKey(display
, code
,
5378 kp
->mod
| modifiers
[j
],
5379 screens
[k
].root
, True
,
5380 GrabModeAsync
, GrabModeAsync
);
5386 grabbuttons(struct ws_win
*win
, int focused
)
5389 unsigned int modifiers
[] =
5390 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5392 updatenumlockmask();
5393 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
5395 for (i
= 0; i
< LENGTH(buttons
); i
++)
5396 if (buttons
[i
].action
== client_click
)
5397 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5398 XGrabButton(display
, buttons
[i
].button
,
5399 buttons
[i
].mask
| modifiers
[j
],
5400 win
->id
, False
, BUTTONMASK
,
5401 GrabModeAsync
, GrabModeSync
, None
,
5404 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
5405 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
5408 const char *quirkname
[] = {
5409 "NONE", /* config string for "no value" */
5418 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5419 #define SWM_Q_WS "\n|+ \t"
5421 parsequirks(char *qstr
, unsigned long *quirk
)
5431 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5433 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5434 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5435 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5436 DNPRINTF(SWM_D_QUIRK
,
5437 "parsequirks: %s\n", name
);
5442 *quirk
|= 1 << (i
-1);
5446 if (i
>= LENGTH(quirkname
)) {
5447 DNPRINTF(SWM_D_QUIRK
,
5448 "parsequirks: invalid quirk [%s]\n", name
);
5456 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5460 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5463 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5464 err(1, "quirk_insert: malloc");
5465 if ((qp
->class = strdup(class)) == NULL
)
5466 err(1, "quirk_insert: strdup");
5467 if ((qp
->name
= strdup(name
)) == NULL
)
5468 err(1, "quirk_insert: strdup");
5471 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5473 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5477 quirk_remove(struct quirk
*qp
)
5479 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5480 qp
->name
, qp
->quirk
);
5482 TAILQ_REMOVE(&quirks
, qp
, entry
);
5487 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5491 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5492 unsigned long quirk
)
5494 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5495 qp
->name
, qp
->quirk
);
5498 quirk_insert(class, name
, quirk
);
5500 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5504 setquirk(const char *class, const char *name
, unsigned long quirk
)
5508 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5511 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5512 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5516 quirk_replace(qp
, class, name
, quirk
);
5517 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5522 warnx("error: setquirk: cannot find class/name combination");
5526 quirk_insert(class, name
, quirk
);
5527 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5531 setconfquirk(char *selector
, char *value
, int flags
)
5533 char *cp
, *class, *name
;
5535 unsigned long quirks
;
5536 if (selector
== NULL
)
5538 if ((cp
= strchr(selector
, ':')) == NULL
)
5543 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5544 setquirk(class, name
, quirks
);
5551 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5552 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5553 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5554 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5555 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5556 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5557 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5558 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5559 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5560 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5561 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5562 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5563 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5566 /* conf file stuff */
5567 #define SWM_CONF_FILE "spectrwm.conf"
5568 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5572 SWM_S_BAR_AT_BOTTOM
,
5573 SWM_S_BAR_BORDER_WIDTH
,
5580 SWM_S_CLOCK_ENABLED
,
5583 SWM_S_CYCLE_VISIBLE
,
5585 SWM_S_DISABLE_BORDER
,
5587 SWM_S_FOCUS_CLOSE_WRAP
,
5588 SWM_S_FOCUS_DEFAULT
,
5594 SWM_S_STACK_ENABLED
,
5596 SWM_S_TITLE_CLASS_ENABLED
,
5597 SWM_S_TITLE_NAME_ENABLED
,
5598 SWM_S_URGENT_ENABLED
,
5599 SWM_S_VERBOSE_LAYOUT
,
5600 SWM_S_WINDOW_NAME_ENABLED
,
5601 SWM_S_WORKSPACE_LIMIT
5605 setconfvalue(char *selector
, char *value
, int flags
)
5611 case SWM_S_BAR_ACTION
:
5613 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5614 err(1, "setconfvalue: bar_action");
5616 case SWM_S_BAR_AT_BOTTOM
:
5617 bar_at_bottom
= atoi(value
);
5619 case SWM_S_BAR_BORDER_WIDTH
:
5620 bar_border_width
= atoi(value
);
5621 if (bar_border_width
< 0)
5622 bar_border_width
= 0;
5624 case SWM_S_BAR_DELAY
:
5625 bar_delay
= atoi(value
);
5627 case SWM_S_BAR_ENABLED
:
5628 bar_enabled
= atoi(value
);
5630 case SWM_S_BAR_FONT
:
5632 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5633 err(1, "setconfvalue: asprintf: failed to allocate "
5634 "memory for bar_fonts.");
5638 case SWM_S_BAR_FORMAT
:
5640 if ((bar_format
= strdup(value
)) == NULL
)
5641 err(1, "setconfvalue: bar_format");
5643 case SWM_S_BAR_JUSTIFY
:
5644 if (!strcmp(value
, "left"))
5645 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5646 else if (!strcmp(value
, "center"))
5647 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5648 else if (!strcmp(value
, "right"))
5649 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5651 errx(1, "invalid bar_justify");
5653 case SWM_S_BORDER_WIDTH
:
5654 border_width
= atoi(value
);
5655 if (border_width
< 0)
5658 case SWM_S_CLOCK_ENABLED
:
5659 clock_enabled
= atoi(value
);
5661 case SWM_S_CLOCK_FORMAT
:
5662 #ifndef SWM_DENY_CLOCK_FORMAT
5664 if ((clock_format
= strdup(value
)) == NULL
)
5665 err(1, "setconfvalue: clock_format");
5668 case SWM_S_CYCLE_EMPTY
:
5669 cycle_empty
= atoi(value
);
5671 case SWM_S_CYCLE_VISIBLE
:
5672 cycle_visible
= atoi(value
);
5674 case SWM_S_DIALOG_RATIO
:
5675 dialog_ratio
= atof(value
);
5676 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5679 case SWM_S_DISABLE_BORDER
:
5680 disable_border
= atoi(value
);
5682 case SWM_S_FOCUS_CLOSE
:
5683 if (!strcmp(value
, "first"))
5684 focus_close
= SWM_STACK_BOTTOM
;
5685 else if (!strcmp(value
, "last"))
5686 focus_close
= SWM_STACK_TOP
;
5687 else if (!strcmp(value
, "next"))
5688 focus_close
= SWM_STACK_ABOVE
;
5689 else if (!strcmp(value
, "previous"))
5690 focus_close
= SWM_STACK_BELOW
;
5692 errx(1, "focus_close");
5694 case SWM_S_FOCUS_CLOSE_WRAP
:
5695 focus_close_wrap
= atoi(value
);
5697 case SWM_S_FOCUS_DEFAULT
:
5698 if (!strcmp(value
, "last"))
5699 focus_default
= SWM_STACK_TOP
;
5700 else if (!strcmp(value
, "first"))
5701 focus_default
= SWM_STACK_BOTTOM
;
5703 errx(1, "focus_default");
5705 case SWM_S_FOCUS_MODE
:
5706 if (!strcmp(value
, "default"))
5707 focus_mode
= SWM_FOCUS_DEFAULT
;
5708 else if (!strcmp(value
, "follow_cursor"))
5709 focus_mode
= SWM_FOCUS_FOLLOW
;
5710 else if (!strcmp(value
, "synergy"))
5711 focus_mode
= SWM_FOCUS_SYNERGY
;
5713 errx(1, "focus_mode");
5715 case SWM_S_SPAWN_ORDER
:
5716 if (!strcmp(value
, "first"))
5717 spawn_position
= SWM_STACK_BOTTOM
;
5718 else if (!strcmp(value
, "last"))
5719 spawn_position
= SWM_STACK_TOP
;
5720 else if (!strcmp(value
, "next"))
5721 spawn_position
= SWM_STACK_ABOVE
;
5722 else if (!strcmp(value
, "previous"))
5723 spawn_position
= SWM_STACK_BELOW
;
5725 errx(1, "spawn_position");
5727 case SWM_S_SPAWN_TERM
:
5728 setconfspawn("term", value
, 0);
5729 setconfspawn("spawn_term", value
, 0);
5733 case SWM_S_SS_ENABLED
:
5734 ss_enabled
= atoi(value
);
5736 case SWM_S_STACK_ENABLED
:
5737 stack_enabled
= atoi(value
);
5739 case SWM_S_TERM_WIDTH
:
5740 term_width
= atoi(value
);
5744 case SWM_S_TITLE_CLASS_ENABLED
:
5745 title_class_enabled
= atoi(value
);
5747 case SWM_S_TITLE_NAME_ENABLED
:
5748 title_name_enabled
= atoi(value
);
5750 case SWM_S_URGENT_ENABLED
:
5751 urgent_enabled
= atoi(value
);
5753 case SWM_S_VERBOSE_LAYOUT
:
5754 verbose_layout
= atoi(value
);
5755 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5757 layouts
[i
].l_string
= fancy_stacker
;
5759 layouts
[i
].l_string
= plain_stacker
;
5762 case SWM_S_WINDOW_NAME_ENABLED
:
5763 window_name_enabled
= atoi(value
);
5765 case SWM_S_WORKSPACE_LIMIT
:
5766 workspace_limit
= atoi(value
);
5767 if (workspace_limit
> SWM_WS_MAX
)
5768 workspace_limit
= SWM_WS_MAX
;
5769 else if (workspace_limit
< 1)
5770 workspace_limit
= 1;
5779 setconfmodkey(char *selector
, char *value
, int flags
)
5781 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5782 update_modkey(Mod1Mask
);
5783 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5784 update_modkey(Mod2Mask
);
5785 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5786 update_modkey(Mod3Mask
);
5787 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5788 update_modkey(Mod4Mask
);
5795 setconfcolor(char *selector
, char *value
, int flags
)
5797 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5802 setconfregion(char *selector
, char *value
, int flags
)
5804 custom_region(value
);
5809 setautorun(char *selector
, char *value
, int flags
)
5819 if (getenv("SWM_STARTED"))
5823 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5824 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5826 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5827 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5830 * This is a little intricate
5832 * If the pid already exists we simply reuse it because it means it was
5833 * used before AND not claimed by manage_window. We get away with
5834 * altering it in the parent after INSERT because this can not be a race
5837 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5840 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5842 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5843 err(1, "setautorun: realloc");
5844 a
.argv
[argc
- 1] = ap
;
5847 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5848 err(1, "setautorun: realloc");
5849 a
.argv
[argc
] = NULL
;
5851 if ((pid
= fork()) == 0) {
5852 spawn(ws_id
, &a
, 1);
5861 p
= calloc(1, sizeof *p
);
5864 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
5874 setlayout(char *selector
, char *value
, int flags
)
5876 int ws_id
, i
, x
, mg
, ma
, si
, raise
, f
= 0;
5877 int st
= SWM_V_STACK
;
5879 struct workspace
*ws
;
5881 if (getenv("SWM_STARTED"))
5885 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
5886 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
5887 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5888 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5891 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5892 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
5894 if (!strcasecmp(s
, "vertical"))
5896 else if (!strcasecmp(s
, "vertical_flip")) {
5899 } else if (!strcasecmp(s
, "horizontal"))
5901 else if (!strcasecmp(s
, "horizontal_flip")) {
5904 } else if (!strcasecmp(s
, "fullscreen"))
5907 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5908 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5911 for (i
= 0; i
< ScreenCount(display
); i
++) {
5912 ws
= (struct workspace
*)&screens
[i
].ws
;
5913 ws
[ws_id
].cur_layout
= &layouts
[st
];
5915 ws
[ws_id
].always_raise
= raise
;
5916 if (st
== SWM_MAX_STACK
)
5920 for (x
= 0; x
< abs(mg
); x
++) {
5921 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5922 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
5923 SWM_ARG_ID_MASTERSHRINK
);
5927 for (x
= 0; x
< abs(ma
); x
++) {
5928 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5929 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
5930 SWM_ARG_ID_MASTERDEL
);
5934 for (x
= 0; x
< abs(si
); x
++) {
5935 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5936 si
>= 0 ? SWM_ARG_ID_STACKINC
:
5937 SWM_ARG_ID_STACKDEC
);
5942 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5943 SWM_ARG_ID_FLIPLAYOUT
);
5951 /* config options */
5952 struct config_option
{
5954 int (*func
)(char*, char*, int);
5957 struct config_option configopt
[] = {
5958 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
5959 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
5960 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
5961 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
5962 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
5963 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
5964 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
5965 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
5966 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
5967 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
5968 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
5969 { "keyboard_mapping", setkeymapping
, 0 },
5970 { "bind", setconfbinding
, 0 },
5971 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
5972 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
5973 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
5974 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
5975 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
5976 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
5977 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
5978 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
5979 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
5980 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
5981 { "modkey", setconfmodkey
, 0 },
5982 { "program", setconfspawn
, 0 },
5983 { "quirk", setconfquirk
, 0 },
5984 { "region", setconfregion
, 0 },
5985 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
5986 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
5987 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
5988 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
5989 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
5990 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
5991 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
5992 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
5993 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
5994 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
5995 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
5996 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
5997 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
5998 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
5999 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6000 { "autorun", setautorun
, 0 },
6001 { "layout", setlayout
, 0 },
6006 conf_load(char *filename
, int keymapping
)
6009 char *line
, *cp
, *optsub
, *optval
;
6010 size_t linelen
, lineno
= 0;
6011 int wordlen
, i
, optind
;
6012 struct config_option
*opt
;
6014 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6016 if (filename
== NULL
) {
6017 warnx("conf_load: no filename");
6020 if ((config
= fopen(filename
, "r")) == NULL
) {
6021 warn("conf_load: fopen: %s", filename
);
6025 while (!feof(config
)) {
6026 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6029 err(1, "%s", filename
);
6034 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6035 if (cp
[0] == '\0') {
6040 /* get config option */
6041 wordlen
= strcspn(cp
, "=[ \t\n");
6043 warnx("%s: line %zd: no option found",
6048 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6049 opt
= &configopt
[i
];
6050 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6051 strlen(opt
->optname
) == wordlen
) {
6057 warnx("%s: line %zd: unknown option %.*s",
6058 filename
, lineno
, wordlen
, cp
);
6061 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
6062 warnx("%s: line %zd: invalid option %.*s",
6063 filename
, lineno
, wordlen
, cp
);
6067 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6068 /* get [selector] if any */
6072 wordlen
= strcspn(cp
, "]");
6075 warnx("%s: line %zd: syntax error",
6080 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6082 warnx("%s: line %zd: unable to allocate"
6083 "memory for selector", filename
,
6089 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6091 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6093 optval
= strdup(cp
);
6094 /* call function to deal with it all */
6095 if (configopt
[optind
].func(optsub
, optval
,
6096 configopt
[optind
].funcflags
) != 0)
6097 errx(1, "%s: line %zd: invalid data for %s",
6098 filename
, lineno
, configopt
[optind
].optname
);
6105 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6112 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6118 set_child_transient(struct ws_win
*win
, Window
*trans
)
6120 struct ws_win
*parent
, *w
;
6121 XWMHints
*wmh
= NULL
;
6122 struct swm_region
*r
;
6123 struct workspace
*ws
;
6125 parent
= find_window(win
->transient
);
6127 parent
->child_trans
= win
;
6129 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6130 " for 0x%lx trans 0x%lx\n", win
->id
, win
->transient
);
6132 if (win
->hints
== NULL
) {
6133 warnx("no hints for 0x%lx", win
->id
);
6137 r
= root_to_region(win
->wa
.root
);
6139 /* parent doen't exist in our window list */
6140 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6144 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
6145 warnx("can't get hints for 0x%lx", w
->id
);
6149 if (win
->hints
->window_group
!= wmh
->window_group
)
6152 w
->child_trans
= win
;
6153 win
->transient
= w
->id
;
6155 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
6156 "transient to 0x%lx\n", win
->transient
);
6166 window_get_pid(Window win
)
6168 Atom actual_type_return
;
6169 int actual_format_return
= 0;
6170 unsigned long nitems_return
= 0;
6171 unsigned long bytes_after_return
= 0;
6175 unsigned char *prop
= NULL
;
6177 if (XGetWindowProperty(display
, win
,
6178 XInternAtom(display
, "_NET_WM_PID", False
), 0, 1, False
,
6179 XA_CARDINAL
, &actual_type_return
, &actual_format_return
,
6180 &nitems_return
, &bytes_after_return
,
6181 (unsigned char**)(void*)&pid
) != Success
)
6183 if (actual_type_return
!= XA_CARDINAL
)
6194 if (XGetWindowProperty(display
, win
,
6195 XInternAtom(display
, "_SWM_PID", False
), 0, SWM_PROPLEN
, False
,
6196 XA_STRING
, &actual_type_return
, &actual_format_return
,
6197 &nitems_return
, &bytes_after_return
, &prop
) != Success
)
6199 if (actual_type_return
!= XA_STRING
)
6204 ret
= strtonum((const char *)prop
, 0, UINT_MAX
, &errstr
);
6205 /* ignore error because strtonum returns 0 anyway */
6212 manage_window(Window id
)
6215 struct workspace
*ws
;
6216 struct ws_win
*win
, *ww
;
6217 int format
, i
, ws_idx
, n
, border_me
= 0;
6218 unsigned long nitems
, bytes
;
6219 Atom ws_idx_atom
= 0, type
;
6220 Atom
*prot
= NULL
, *pp
;
6221 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
6222 struct swm_region
*r
;
6227 if ((win
= find_window(id
)) != NULL
)
6228 return (win
); /* already being managed */
6230 /* see if we are on the unmanaged list */
6231 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6232 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
6233 "window: 0x%lx\n", win
->id
);
6234 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6236 set_child_transient(win
, &trans
);
6238 if (trans
&& (ww
= find_window(trans
)))
6239 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
6240 else if ((ww
= win
->ws
->focus
) &&
6241 spawn_position
== SWM_STACK_ABOVE
)
6242 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
,
6244 else if (ww
&& spawn_position
== SWM_STACK_BELOW
)
6245 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6246 else switch (spawn_position
) {
6249 case SWM_STACK_ABOVE
:
6250 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6252 case SWM_STACK_BOTTOM
:
6253 case SWM_STACK_BELOW
:
6254 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6257 ewmh_update_actions(win
);
6261 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6262 err(1, "manage_window: calloc: failed to allocate memory for "
6268 /* see if we need to override the workspace */
6269 p
= find_pid(window_get_pid(id
));
6271 /* Get all the window data in one shot */
6272 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
6274 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
6275 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
6277 XGetWindowAttributes(display
, id
, &win
->wa
);
6278 XGetWMNormalHints(display
, id
, &win
->sh
, &win
->sh_mask
);
6279 win
->hints
= XGetWMHints(display
, id
);
6280 XGetTransientForHint(display
, id
, &trans
);
6282 win
->transient
= trans
;
6283 set_child_transient(win
, &trans
);
6284 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, "
6285 "transient: 0x%lx\n", win
->id
, win
->transient
);
6288 /* get supported protocols */
6289 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
6290 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
6291 if (*pp
== takefocus
)
6292 win
->take_focus
= 1;
6294 win
->can_delete
= 1;
6300 win
->iconic
= get_iconic(win
);
6303 * Figure out where to put the window. If it was previously assigned to
6304 * a workspace (either by spawn() or manually moving), and isn't
6305 * transient, * put it in the same workspace
6307 r
= root_to_region(win
->wa
.root
);
6309 ws
= &r
->s
->ws
[p
->ws
];
6310 TAILQ_REMOVE(&pidlist
, p
, entry
);
6313 } else if (prop
&& win
->transient
== 0) {
6314 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
6315 ws_idx
= strtonum((const char *)prop
, 0, workspace_limit
- 1,
6318 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
6321 ws
= &r
->s
->ws
[ws_idx
];
6324 /* this should launch transients in the same ws as parent */
6326 if ((ww
= find_window(trans
)) != NULL
)
6332 warnx("manage_window: fix this "
6338 /* set up the window layout */
6341 win
->s
= r
->s
; /* this never changes */
6342 if (trans
&& (ww
= find_window(trans
)))
6343 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
6344 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
6345 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
6347 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
6348 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6349 else switch (spawn_position
) {
6352 case SWM_STACK_ABOVE
:
6353 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6355 case SWM_STACK_BOTTOM
:
6356 case SWM_STACK_BELOW
:
6357 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6360 /* ignore window border if there is one. */
6361 WIDTH(win
) = win
->wa
.width
;
6362 HEIGHT(win
) = win
->wa
.height
;
6363 X(win
) = win
->wa
.x
+ win
->wa
.border_width
;
6364 Y(win
) = win
->wa
.y
+ win
->wa
.border_width
;
6366 win
->g_floatvalid
= 0;
6367 win
->floatmaxed
= 0;
6368 win
->ewmh_flags
= 0;
6370 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, (x,y) w x h: "
6371 "(%d,%d) %d x %d, ws: %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
6372 HEIGHT(win
), ws
->idx
);
6374 constrain_window(win
, r
, 0);
6376 /* Set window properties so we can remember this after reincarnation */
6377 if (ws_idx_atom
&& prop
== NULL
&&
6378 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
6380 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6382 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
6383 PropModeReplace
, ws_idx_str
, strlen((char *)ws_idx_str
));
6388 ewmh_autoquirk(win
);
6390 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
6391 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6392 win
->ch
.res_class
, win
->ch
.res_name
);
6394 /* java is retarded so treat it special */
6395 if (strstr(win
->ch
.res_name
, "sun-awt")) {
6400 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6401 if (!strcmp(win
->ch
.res_class
, qp
->class) &&
6402 !strcmp(win
->ch
.res_name
, qp
->name
)) {
6403 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
6404 "class: %s, name: %s\n", win
->ch
.res_class
,
6406 if (qp
->quirk
& SWM_Q_FLOAT
) {
6410 win
->quirks
= qp
->quirk
;
6415 /* alter window position if quirky */
6416 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6417 win
->manual
= 1; /* don't center the quirky windows */
6418 if (bar_enabled
&& Y(win
) < bar_height
)
6419 Y(win
) = bar_height
;
6420 if (WIDTH(win
) + X(win
) > WIDTH(r
))
6421 X(win
) = WIDTH(r
) - WIDTH(win
) - 2;
6425 /* Reset font sizes (the bruteforce way; no default keybinding). */
6426 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6427 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6428 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
6429 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6430 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
6433 ewmh_get_win_state(win
);
6434 ewmh_update_actions(win
);
6435 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
6440 X(win
) -= border_width
;
6441 Y(win
) -= border_width
;
6445 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
6446 PropertyChangeMask
| StructureNotifyMask
);
6448 /* floaters need to be mapped if they are in the current workspace */
6449 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
6450 XMapRaised(display
, win
->id
);
6456 free_window(struct ws_win
*win
)
6458 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%lx\n", win
->id
);
6463 /* needed for restart wm */
6464 set_win_state(win
, WithdrawnState
);
6466 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6468 if (win
->ch
.res_class
)
6469 XFree(win
->ch
.res_class
);
6470 if (win
->ch
.res_name
)
6471 XFree(win
->ch
.res_name
);
6476 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6482 unmanage_window(struct ws_win
*win
)
6484 struct ws_win
*parent
;
6489 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%lx\n", win
->id
);
6491 if (win
->transient
) {
6492 parent
= find_window(win
->transient
);
6494 parent
->child_trans
= NULL
;
6497 /* focus on root just in case */
6498 XSetInputFocus(display
, PointerRoot
, PointerRoot
, CurrentTime
);
6507 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
6508 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6512 focus_magic(struct ws_win
*win
)
6514 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%lx\n", WINID(win
));
6517 /* if there are no windows clear the status-bar */
6522 if (win
->child_trans
) {
6523 /* win = parent & has a transient so focus on that */
6525 focus_win(win
->child_trans
);
6526 if (win
->child_trans
->take_focus
)
6527 client_msg(win
, takefocus
);
6529 /* make sure transient hasn't disappeared */
6530 if (validate_win(win
->child_trans
) == 0) {
6531 focus_win(win
->child_trans
);
6532 if (win
->child_trans
->take_focus
)
6533 client_msg(win
->child_trans
, takefocus
);
6535 win
->child_trans
= NULL
;
6537 if (win
->take_focus
)
6538 client_msg(win
, takefocus
);
6544 if (win
->take_focus
)
6545 client_msg(win
, takefocus
);
6552 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%lx\n", e
->xexpose
.window
);
6559 XKeyEvent
*ev
= &e
->xkey
;
6561 struct swm_region
*r
;
6563 keysym
= XkbKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0, 0);
6564 if ((kp
= key_lookup(CLEANMASK(ev
->state
), keysym
)) == NULL
)
6566 if (keyfuncs
[kp
->funcid
].func
== NULL
)
6569 r
= root_to_region(ev
->root
);
6570 if (kp
->funcid
== kf_spawn_custom
)
6571 spawn_custom(r
, &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
6573 keyfuncs
[kp
->funcid
].func(r
, &(keyfuncs
[kp
->funcid
].args
));
6577 buttonpress(XEvent
*e
)
6581 XButtonPressedEvent
*ev
= &e
->xbutton
;
6583 if ((win
= find_window(ev
->window
)) == NULL
)
6587 action
= client_click
;
6589 for (i
= 0; i
< LENGTH(buttons
); i
++)
6590 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6591 buttons
[i
].button
== ev
->button
&&
6592 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
6593 buttons
[i
].func(win
, &buttons
[i
].args
);
6597 configurerequest(XEvent
*e
)
6599 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
6604 if ((win
= find_window(ev
->window
)) == NULL
)
6605 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6609 bzero(&wc
, sizeof wc
);
6612 wc
.width
= ev
->width
;
6613 wc
.height
= ev
->height
;
6614 wc
.border_width
= ev
->border_width
;
6615 wc
.sibling
= ev
->above
;
6616 wc
.stack_mode
= ev
->detail
;
6618 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: 0x%lx, "
6619 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", ev
->window
,
6620 YESNO(new), wc
.x
, wc
.y
, wc
.width
, wc
.height
);
6622 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
6623 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
6624 !(win
->sh_mask
& EWMH_F_FULLSCREEN
)) {
6625 win
->g_float
.x
= ev
->x
- X(win
->ws
->r
);
6626 win
->g_float
.y
= ev
->y
- Y(win
->ws
->r
);
6627 win
->g_float
.w
= ev
->width
;
6628 win
->g_float
.h
= ev
->height
;
6629 win
->g_floatvalid
= 1;
6631 if (win
->floating
) {
6632 win
->g
= win
->g_float
;
6633 win
->g
.x
+= X(win
->ws
->r
);
6634 win
->g
.y
+= Y(win
->ws
->r
);
6637 config_win(win
, ev
);
6640 config_win(win
, ev
);
6645 configurenotify(XEvent
*e
)
6649 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%lx\n",
6650 e
->xconfigure
.window
);
6652 win
= find_window(e
->xconfigure
.window
);
6654 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &win
->sh_mask
);
6658 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6659 drain_enter_notify();
6664 destroynotify(XEvent
*e
)
6667 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
6669 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%lx\n", ev
->window
);
6671 if ((win
= find_window(ev
->window
)) == NULL
) {
6672 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6678 /* make sure we focus on something */
6681 unmanage_window(win
);
6683 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6684 drain_enter_notify();
6689 enternotify(XEvent
*e
)
6691 XCrossingEvent
*ev
= &e
->xcrossing
;
6696 Window focus_return
;
6697 int revert_to_return
;
6699 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%lx, mode: %d, detail: "
6700 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6701 "state: %d\n", ev
->window
, ev
->mode
, ev
->detail
, ev
->root
,
6702 ev
->subwindow
, YESNO(ev
->same_screen
), YESNO(ev
->focus
), ev
->state
);
6704 if (ev
->mode
!= NotifyNormal
) {
6705 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6710 switch (focus_mode
) {
6711 case SWM_FOCUS_DEFAULT
:
6713 case SWM_FOCUS_FOLLOW
:
6715 case SWM_FOCUS_SYNERGY
:
6718 * all these checks need to be in this order because the
6719 * XCheckTypedWindowEvent relies on weeding out the previous events
6721 * making this code an option would enable a follow mouse for focus
6726 * state is set when we are switching workspaces and focus is set when
6727 * the window or a subwindow already has focus (occurs during restart).
6729 * Only honor the focus flag if last_focus_event is not FocusOut,
6730 * this allows spectrwm to continue to control focus when another
6731 * program is also playing with it.
6733 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6734 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6739 * happens when a window is created or destroyed and the border
6740 * crosses the mouse pointer and when switching ws
6742 * we need the subwindow test to see if we came from root in order
6743 * to give focus to floaters
6745 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6746 ev
->subwindow
== 0) {
6747 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6751 /* this window already has focus */
6752 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6753 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6757 /* this window is being deleted or moved to another ws */
6758 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6760 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6761 XPutBackEvent(display
, &cne
);
6765 if ((win
= find_window(ev
->window
)) == NULL
) {
6766 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6771 * In fullstack kill all enters unless they come from a different ws
6772 * (i.e. another region) or focus has been grabbed externally.
6774 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6775 last_focus_event
!= FocusOut
) {
6776 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6777 if ((w
= find_window(focus_return
)) == NULL
||
6779 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6787 if ((win
= find_window(ev
->window
)) == NULL
) {
6788 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6793 * if we have more enternotifies let them handle it in due time
6795 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
6796 DNPRINTF(SWM_D_EVENT
,
6797 "ignoring enternotify: got more enternotify\n");
6798 XPutBackEvent(display
, &cne
);
6805 /* lets us use one switch statement for arbitrary mode/detail combinations */
6806 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6809 focusevent(XEvent
*e
)
6813 u_int32_t mode_detail
;
6814 XFocusChangeEvent
*ev
= &e
->xfocus
;
6816 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: 0x%lx mode: %d "
6817 "detail: %d\n", ev
->type
== FocusIn
? "entering" : "leaving",
6818 ev
->window
, ev
->mode
, ev
->detail
);
6820 if (last_focus_event
== ev
->type
) {
6821 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
6825 last_focus_event
= ev
->type
;
6826 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
6828 switch (mode_detail
) {
6829 /* synergy client focus operations */
6830 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
6831 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
6833 /* synergy server focus operations */
6834 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
6836 /* Entering applications like rdesktop that mangle the pointer */
6837 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
6839 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
6840 XSetWindowBorder(display
, win
->id
,
6841 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
6842 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
6845 warnx("ignoring focusevent");
6846 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
6853 mapnotify(XEvent
*e
)
6856 XMapEvent
*ev
= &e
->xmap
;
6858 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%lx\n", ev
->window
);
6860 win
= manage_window(ev
->window
);
6862 set_win_state(win
, NormalState
);
6865 * focus_win can only set input focus on a mapped window.
6866 * make sure the window really has focus since it is just being mapped.
6868 if (win
->ws
->focus
== win
)
6873 mappingnotify(XEvent
*e
)
6875 XMappingEvent
*ev
= &e
->xmapping
;
6877 XRefreshKeyboardMapping(ev
);
6878 if (ev
->request
== MappingKeyboard
)
6883 maprequest(XEvent
*e
)
6886 struct swm_region
*r
;
6887 XWindowAttributes wa
;
6888 XMapRequestEvent
*ev
= &e
->xmaprequest
;
6890 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%lx\n",
6891 e
->xmaprequest
.window
);
6893 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
6895 if (wa
.override_redirect
)
6898 win
= manage_window(e
->xmaprequest
.window
);
6900 return; /* can't happen */
6904 /* make new win focused */
6905 r
= root_to_region(win
->wa
.root
);
6906 if (win
->ws
== r
->ws
)
6911 propertynotify(XEvent
*e
)
6914 XPropertyEvent
*ev
= &e
->xproperty
;
6917 name
= XGetAtomName(display
, ev
->atom
);
6918 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%lx, atom: %s\n",
6923 win
= find_window(ev
->window
);
6927 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
6928 update_iconic(win
, 0);
6929 XMapRaised(display
, win
->id
);
6937 case XA_WM_NORMAL_HINTS
:
6939 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
6940 warnx("normal hints: flag 0x%x", win
->sh
.flags
);
6941 if (win
->sh
.flags
& PMinSize
) {
6942 WIDTH(win
) = win
->sh
.min_width
;
6943 HEIGHT(win
) = win
->sh
.min_height
;
6944 warnx("min %d %d", WIDTH(win
), HEIGHT(win
));
6946 XMoveResizeWindow(display
, win
->id
,
6947 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
));
6959 unmapnotify(XEvent
*e
)
6963 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%lx\n", e
->xunmap
.window
);
6965 /* determine if we need to help unmanage this window */
6966 win
= find_window(e
->xunmap
.window
);
6970 if (getstate(e
->xunmap
.window
) == NormalState
) {
6971 unmanage_window(win
);
6974 /* giant hack for apps that don't destroy transient windows */
6975 /* eat a bunch of events to prevent remanaging the window */
6977 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6978 EnterWindowMask
, &cne
))
6980 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6981 StructureNotifyMask
, &cne
))
6983 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6984 SubstructureNotifyMask
, &cne
))
6986 /* resend unmap because we ated it */
6987 XUnmapWindow(display
, e
->xunmap
.window
);
6990 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6991 drain_enter_notify();
6995 visibilitynotify(XEvent
*e
)
6998 struct swm_region
*r
;
7000 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%lx\n",
7001 e
->xvisibility
.window
);
7002 if (e
->xvisibility
.state
== VisibilityUnobscured
)
7003 for (i
= 0; i
< ScreenCount(display
); i
++)
7004 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7005 if (e
->xvisibility
.window
== WINID(r
->bar
))
7010 clientmessage(XEvent
*e
)
7012 XClientMessageEvent
*ev
;
7017 win
= find_window(ev
->window
);
7019 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7020 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
7021 "unmanaged window.\n");
7022 e
->xmaprequest
.window
= ev
->window
;
7028 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx, type: %ld\n",
7029 ev
->window
, ev
->message_type
);
7031 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7032 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7035 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7036 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7037 if (win
->can_delete
)
7038 client_msg(win
, adelete
);
7040 XKillClient(display
, win
->id
);
7042 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7043 DNPRINTF(SWM_D_EVENT
,
7044 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7045 if (win
->floating
) {
7046 if (ev
->data
.l
[0] & (1<<8)) /* x */
7047 X(win
) = ev
->data
.l
[1];
7048 if (ev
->data
.l
[0] & (1<<9)) /* y */
7049 Y(win
) = ev
->data
.l
[2];
7050 if (ev
->data
.l
[0] & (1<<10)) /* width */
7051 WIDTH(win
) = ev
->data
.l
[3];
7052 if (ev
->data
.l
[0] & (1<<11)) /* height */
7053 HEIGHT(win
) = ev
->data
.l
[4];
7058 /* TODO: Change stack sizes */
7059 /* notify no change was made. */
7060 config_win(win
, NULL
);
7063 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
7064 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
7065 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
7067 ewmh_update_win_state(win
, ev
->data
.l
[2],
7075 xerror_start(Display
*d
, XErrorEvent
*ee
)
7082 xerror(Display
*d
, XErrorEvent
*ee
)
7084 /* warnx("error: %p %p", display, ee); */
7092 xerrorxlib
= XSetErrorHandler(xerror_start
);
7094 /* this causes an error if some other window manager is running */
7095 XSelectInput(display
, DefaultRootWindow(display
),
7096 SubstructureRedirectMask
);
7097 XSync(display
, False
);
7101 XSetErrorHandler(xerror
);
7102 XSync(display
, False
);
7107 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
7109 struct swm_region
*r
, *n
;
7110 struct workspace
*ws
= NULL
;
7113 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
7114 s
->idx
, w
, h
, x
, y
);
7116 /* remove any conflicting regions */
7117 n
= TAILQ_FIRST(&s
->rl
);
7120 n
= TAILQ_NEXT(r
, entry
);
7121 if (X(r
) < (x
+ w
) &&
7122 (X(r
) + WIDTH(r
)) > x
&&
7124 (Y(r
) + HEIGHT(r
)) > y
) {
7125 if (r
->ws
->r
!= NULL
)
7126 r
->ws
->old_r
= r
->ws
->r
;
7129 TAILQ_REMOVE(&s
->rl
, r
, entry
);
7130 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
7134 /* search old regions for one to reuse */
7136 /* size + location match */
7137 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7138 if (X(r
) == x
&& Y(r
) == y
&&
7139 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7143 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7144 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7148 TAILQ_REMOVE(&s
->orl
, r
, entry
);
7149 /* try to use old region's workspace */
7150 if (r
->ws
->r
== NULL
)
7153 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
7154 err(1, "new_region: calloc: failed to allocate memory "
7157 /* if we don't have a workspace already, find one */
7159 for (i
= 0; i
< workspace_limit
; i
++)
7160 if (s
->ws
[i
].r
== NULL
) {
7167 errx(1, "new_region: no free workspaces");
7178 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
7184 #ifdef SWM_XRR_HAS_CRTC
7186 XRRScreenResources
*sr
;
7189 #endif /* SWM_XRR_HAS_CRTC */
7190 struct swm_region
*r
;
7193 if (i
>= ScreenCount(display
))
7194 errx(1, "scan_xrandr: invalid screen");
7196 /* remove any old regions */
7197 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
7198 r
->ws
->old_r
= r
->ws
->r
= NULL
;
7200 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
7201 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
7205 /* map virtual screens onto physical screens */
7206 #ifdef SWM_XRR_HAS_CRTC
7207 if (xrandr_support
) {
7208 sr
= XRRGetScreenResourcesCurrent(display
, screens
[i
].root
);
7210 new_region(&screens
[i
], 0, 0,
7211 DisplayWidth(display
, i
),
7212 DisplayHeight(display
, i
));
7216 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
7217 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
7218 if (ci
->noutput
== 0)
7221 if (ci
!= NULL
&& ci
->mode
== None
)
7222 new_region(&screens
[i
], 0, 0,
7223 DisplayWidth(display
, i
),
7224 DisplayHeight(display
, i
));
7226 new_region(&screens
[i
],
7227 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
7230 XRRFreeCrtcInfo(ci
);
7231 XRRFreeScreenResources(sr
);
7233 #endif /* SWM_XRR_HAS_CRTC */
7235 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
7236 DisplayHeight(display
, i
));
7241 screenchange(XEvent
*e
) {
7242 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
7243 struct swm_region
*r
;
7246 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%lx\n", xe
->root
);
7248 if (!XRRUpdateConfiguration(e
))
7251 /* silly event doesn't include the screen index */
7252 for (i
= 0; i
< ScreenCount(display
); i
++)
7253 if (screens
[i
].root
== xe
->root
)
7255 if (i
>= ScreenCount(display
))
7256 errx(1, "screenchange: screen not found");
7258 /* brute force for now, just re-enumerate the regions */
7261 /* add bars to all regions */
7262 for (i
= 0; i
< ScreenCount(display
); i
++)
7263 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7266 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7267 drain_enter_notify();
7273 Window d1
, d2
, *wins
= NULL
;
7274 XWindowAttributes wa
;
7279 for (i
= 0; i
< ScreenCount(display
); i
++) {
7280 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
7283 /* attach windows to a region */
7284 /* normal windows */
7285 for (j
= 0; j
< no
; j
++) {
7286 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
7287 wa
.override_redirect
||
7288 XGetTransientForHint(display
, wins
[j
], &d1
))
7291 state
= getstate(wins
[j
]);
7292 manage
= state
== IconicState
;
7293 if (wa
.map_state
== IsViewable
|| manage
)
7294 manage_window(wins
[j
]);
7296 /* transient windows */
7297 for (j
= 0; j
< no
; j
++) {
7298 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
7299 wa
.override_redirect
)
7302 state
= getstate(wins
[j
]);
7303 manage
= state
== IconicState
;
7304 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
7306 manage_window(wins
[j
]);
7318 int i
, j
, k
, num_screens
;
7319 int errorbase
, major
, minor
;
7320 struct workspace
*ws
;
7323 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7324 if ((screens
= calloc(num_screens
,
7325 sizeof(struct swm_screen
))) == NULL
)
7326 err(1, "setup_screens: calloc: failed to allocate memory for "
7329 /* initial Xrandr setup */
7330 xrandr_support
= XRRQueryExtension(display
,
7331 &xrandr_eventbase
, &errorbase
);
7333 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
7336 /* map physical screens */
7337 for (i
= 0; i
< num_screens
; i
++) {
7338 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
7340 TAILQ_INIT(&screens
[i
].rl
);
7341 TAILQ_INIT(&screens
[i
].orl
);
7342 screens
[i
].root
= RootWindow(display
, i
);
7344 /* set default colors */
7345 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
7346 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
7347 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
7348 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
7349 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
7351 /* create graphics context on screen */
7352 gcv
.graphics_exposures
= 0;
7353 screens
[i
].bar_gc
= XCreateGC(display
, screens
[i
].root
,
7354 GCGraphicsExposures
, &gcv
);
7356 /* set default cursor */
7357 XDefineCursor(display
, screens
[i
].root
,
7358 XCreateFontCursor(display
, XC_left_ptr
));
7360 /* init all workspaces */
7361 /* XXX these should be dynamically allocated too */
7362 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
7363 ws
= &screens
[i
].ws
[j
];
7369 TAILQ_INIT(&ws
->winlist
);
7370 TAILQ_INIT(&ws
->unmanagedlist
);
7372 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
7373 if (layouts
[k
].l_config
!= NULL
)
7374 layouts
[k
].l_config(ws
,
7375 SWM_ARG_ID_STACKINIT
);
7376 ws
->cur_layout
= &layouts
[0];
7377 ws
->cur_layout
->l_string(ws
);
7383 XRRSelectInput(display
, screens
[i
].root
,
7384 RRScreenChangeNotifyMask
);
7391 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
7392 err(1, "setup_globals: strdup: failed to allocate memory.");
7394 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
7395 err(1, "setup_globals: strdup: failed to allocate memory.");
7402 Atom netwmcheck
, netwmname
, utf8_string
;
7405 /* work around sun jdk bugs, code from wmname */
7406 netwmcheck
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
7407 netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
7408 utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
7409 for (i
= 0; i
< ScreenCount(display
); i
++) {
7410 root
= screens
[i
].root
;
7411 win
= XCreateSimpleWindow(display
,root
, 0, 0, 1, 1, 0,
7412 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
,
7413 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
);
7415 XChangeProperty(display
, root
, netwmcheck
, XA_WINDOW
, 32,
7416 PropModeReplace
, (unsigned char *)&win
, 1);
7417 XChangeProperty(display
, win
, netwmcheck
, XA_WINDOW
, 32,
7418 PropModeReplace
, (unsigned char *)&win
, 1);
7419 XChangeProperty(display
, win
, netwmname
, utf8_string
, 8,
7420 PropModeReplace
, (unsigned char*)"LG3D", strlen("LG3D"));
7425 main(int argc
, char *argv
[])
7427 struct swm_region
*r
, *rr
;
7428 struct ws_win
*winfocus
= NULL
;
7431 char conf
[PATH_MAX
], *cfile
= NULL
;
7436 struct sigaction sact
;
7439 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
7440 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
7442 warnx("no locale support");
7444 if (!X_HAVE_UTF8_STRING
)
7445 warnx("no UTF-8 support");
7447 if (!(display
= XOpenDisplay(0)))
7448 errx(1, "can not open display");
7450 if (!(conn
= XGetXCBConnection(display
)))
7451 errx(1, "can not get XCB connection");
7454 errx(1, "other wm running");
7456 /* handle some signals */
7457 bzero(&sact
, sizeof(sact
));
7458 sigemptyset(&sact
.sa_mask
);
7460 sact
.sa_handler
= sighdlr
;
7461 sigaction(SIGINT
, &sact
, NULL
);
7462 sigaction(SIGQUIT
, &sact
, NULL
);
7463 sigaction(SIGTERM
, &sact
, NULL
);
7464 sigaction(SIGHUP
, &sact
, NULL
);
7466 sact
.sa_handler
= sighdlr
;
7467 sact
.sa_flags
= SA_NOCLDSTOP
;
7468 sigaction(SIGCHLD
, &sact
, NULL
);
7470 astate
= XInternAtom(display
, "WM_STATE", False
);
7471 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
7472 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
7473 takefocus
= XInternAtom(display
, "WM_TAKE_FOCUS", False
);
7474 a_wmname
= XInternAtom(display
, "WM_NAME", False
);
7475 a_netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
7476 a_utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
7477 a_string
= XInternAtom(display
, "STRING", False
);
7478 a_swm_iconic
= XInternAtom(display
, "_SWM_ICONIC", False
);
7480 /* look for local and global conf file */
7481 pwd
= getpwuid(getuid());
7483 errx(1, "invalid user: %d", getuid());
7492 for (i
= 0; ; i
++) {
7497 snprintf(conf
, sizeof conf
, "%s/.%s",
7498 pwd
->pw_dir
, SWM_CONF_FILE
);
7502 snprintf(conf
, sizeof conf
, "/etc/%s",
7507 snprintf(conf
, sizeof conf
, "%s/.%s",
7508 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
7512 snprintf(conf
, sizeof conf
, "/etc/%s",
7519 if (strlen(conf
) && stat(conf
, &sb
) != -1)
7520 if (S_ISREG(sb
.st_mode
)) {
7527 /* load conf (if any) */
7529 conf_load(cfile
, SWM_CONF_DEFAULT
);
7532 /* set some values to work around bad programs */
7534 /* grab existing windows (before we build the bars) */
7537 if (getenv("SWM_STARTED") == NULL
)
7538 setenv("SWM_STARTED", "YES", 1);
7540 /* setup all bars */
7541 for (i
= 0; i
< ScreenCount(display
); i
++)
7542 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
7543 if (winfocus
== NULL
)
7544 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
7552 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7553 drain_enter_notify();
7555 xfd
= ConnectionNumber(display
);
7557 while (XPending(display
)) {
7558 XNextEvent(display
, &e
);
7561 if (e
.type
< LASTEvent
) {
7562 DNPRINTF(SWM_D_EVENTQ
,"XEvent: handled: %s, "
7563 "window: 0x%lx, type: %s (%d), %d remaining"
7564 "\n", YESNO(handler
[e
.type
]),
7565 e
.xany
.window
, geteventname(&e
),
7566 e
.type
, QLength(display
));
7568 if (handler
[e
.type
])
7569 handler
[e
.type
](&e
);
7571 DNPRINTF(SWM_D_EVENTQ
, "XRandr Event: window: "
7572 "0x%lx, type: %s (%d)\n", e
.xany
.window
,
7573 xrandr_geteventname(&e
), e
.type
);
7575 switch (e
.type
- xrandr_eventbase
) {
7576 case RRScreenChangeNotify
:
7585 /* if we are being restarted go focus on first window */
7587 rr
= winfocus
->ws
->r
;
7589 /* not a visible window */
7593 /* move pointer to first screen if multi screen */
7594 if (ScreenCount(display
) > 1 || outputs
> 1)
7595 XWarpPointer(display
, None
, rr
->s
[0].root
,
7597 Y(rr
) + (bar_enabled
? bar_height
: 0));
7599 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7609 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7611 DNPRINTF(SWM_D_MISC
, "select failed");
7612 if (restart_wm
== 1)
7613 restart(NULL
, NULL
);
7614 if (search_resp
== 1)
7627 for (i
= 0; i
< ScreenCount(display
); ++i
)
7628 if (screens
[i
].bar_gc
!= NULL
)
7629 XFreeGC(display
, screens
[i
].bar_gc
);
7631 XFreeFontSet(display
, bar_fs
);
7632 xcb_disconnect(conn
);
7633 XCloseDisplay(display
);