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>
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;
226 int cycle_visible
= 0;
228 int font_adjusted
= 0;
229 unsigned int mod_key
= MODKEY
;
232 struct swm_region
*search_r
;
233 int select_list_pipe
[2];
234 int select_resp_pipe
[2];
236 volatile sig_atomic_t search_resp
;
237 int search_resp_action
;
239 struct search_window
{
240 TAILQ_ENTRY(search_window
) entry
;
246 TAILQ_HEAD(search_winlist
, search_window
);
247 struct search_winlist search_wl
;
252 SWM_SEARCH_UNICONIFY
,
253 SWM_SEARCH_NAME_WORKSPACE
,
254 SWM_SEARCH_SEARCH_WORKSPACE
,
255 SWM_SEARCH_SEARCH_WINDOW
258 #define SWM_STACK_TOP (0)
259 #define SWM_STACK_BOTTOM (1)
260 #define SWM_STACK_ABOVE (2)
261 #define SWM_STACK_BELOW (3)
264 double dialog_ratio
= 0.6;
266 #define SWM_BAR_MAX (256)
267 #define SWM_BAR_JUSTIFY_LEFT (0)
268 #define SWM_BAR_JUSTIFY_CENTER (1)
269 #define SWM_BAR_JUSTIFY_RIGHT (2)
270 #define SWM_BAR_OFFSET (4)
271 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
272 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
273 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
274 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
275 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
277 #ifdef X_HAVE_UTF8_STRING
278 #define DRAWSTRING(x...) Xutf8DrawString(x)
280 #define DRAWSTRING(x...) XmbDrawString(x)
283 char *bar_argv
[] = { NULL
, NULL
};
285 unsigned char bar_ext
[SWM_BAR_MAX
];
286 char bar_vertext
[SWM_BAR_MAX
];
288 sig_atomic_t bar_alarm
= 0;
291 int bar_border_width
= 1;
292 int bar_at_bottom
= 0;
294 int bar_extra_running
= 0;
297 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
298 char *bar_format
= NULL
;
299 int stack_enabled
= 1;
300 int clock_enabled
= 1;
301 int urgent_enabled
= 0;
302 char *clock_format
= NULL
;
303 int title_name_enabled
= 0;
304 int title_class_enabled
= 0;
305 int window_name_enabled
= 0;
306 int focus_mode
= SWM_FOCUS_DEFAULT
;
307 int focus_close
= SWM_STACK_BELOW
;
308 int focus_close_wrap
= 1;
309 int focus_default
= SWM_STACK_TOP
;
310 int spawn_position
= SWM_STACK_TOP
;
311 int disable_border
= 0;
312 int border_width
= 1;
313 int verbose_layout
= 0;
316 XFontSetExtents
*bar_fs_extents
;
320 #define SWM_MENU_FN (2)
321 #define SWM_MENU_NB (4)
322 #define SWM_MENU_NF (6)
323 #define SWM_MENU_SB (8)
324 #define SWM_MENU_SF (10)
326 /* layout manager data */
327 struct swm_geometry
{
340 struct swm_geometry g
;
343 /* virtual "screens" */
345 TAILQ_ENTRY(swm_region
) entry
;
346 struct swm_geometry g
;
347 struct workspace
*ws
; /* current workspace on this region */
348 struct workspace
*ws_prior
; /* prior workspace on this region */
349 struct swm_screen
*s
; /* screen idx */
352 TAILQ_HEAD(swm_region_list
, swm_region
);
355 TAILQ_ENTRY(ws_win
) entry
;
358 struct ws_win
*child_trans
; /* transient child window */
359 struct swm_geometry g
; /* current geometry */
360 struct swm_geometry g_float
; /* region coordinates */
361 int g_floatvalid
; /* g_float geometry validity */
362 int floatmaxed
; /* whether maxed by max_stack */
367 unsigned int ewmh_flags
;
368 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
374 unsigned long quirks
;
375 struct workspace
*ws
; /* always valid */
376 struct swm_screen
*s
; /* always valid, never changes */
377 XWindowAttributes wa
;
383 TAILQ_HEAD(ws_win_list
, ws_win
);
387 TAILQ_ENTRY(pid_e
) entry
;
391 TAILQ_HEAD(pid_list
, pid_e
);
392 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
394 /* layout handlers */
396 void vertical_config(struct workspace
*, int);
397 void vertical_stack(struct workspace
*, struct swm_geometry
*);
398 void horizontal_config(struct workspace
*, int);
399 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
400 void max_stack(struct workspace
*, struct swm_geometry
*);
401 void plain_stacker(struct workspace
*);
402 void fancy_stacker(struct workspace
*);
404 struct ws_win
*find_window(Window
);
406 void grabbuttons(struct ws_win
*, int);
407 void new_region(struct swm_screen
*, int, int, int, int);
408 void unmanage_window(struct ws_win
*);
409 long getstate(Window
);
411 int conf_load(char *, int);
414 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
415 void (*l_config
)(struct workspace
*, int);
417 #define SWM_L_FOCUSPREV (1<<0)
418 #define SWM_L_MAPONFOCUS (1<<1)
419 void (*l_string
)(struct workspace
*);
421 /* stack, configure */
422 { vertical_stack
, vertical_config
, 0, plain_stacker
},
423 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
425 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
426 { NULL
, NULL
, 0, NULL
},
429 /* position of max_stack mode in the layouts array, index into layouts! */
430 #define SWM_V_STACK (0)
431 #define SWM_H_STACK (1)
432 #define SWM_MAX_STACK (2)
434 #define SWM_H_SLICE (32)
435 #define SWM_V_SLICE (32)
437 /* define work spaces */
439 int idx
; /* workspace index */
440 char *name
; /* workspace name */
441 int always_raise
; /* raise windows on focus */
442 struct layout
*cur_layout
; /* current layout handlers */
443 struct ws_win
*focus
; /* may be NULL */
444 struct ws_win
*focus_prev
; /* may be NULL */
445 struct swm_region
*r
; /* may be NULL */
446 struct swm_region
*old_r
; /* may be NULL */
447 struct ws_win_list winlist
; /* list of windows in ws */
448 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
449 char stacker
[10]; /* display stacker and layout */
453 int horizontal_msize
;
455 int horizontal_stacks
;
466 SWM_S_COLOR_BAR_BORDER
,
467 SWM_S_COLOR_BAR_FONT
,
473 /* physical screen mapping */
474 #define SWM_WS_MAX (22) /* hard limit */
475 int workspace_limit
= 10; /* soft limit */
478 int idx
; /* screen index */
479 struct swm_region_list rl
; /* list of regions on this screen */
480 struct swm_region_list orl
; /* list of old regions */
482 struct workspace ws
[SWM_WS_MAX
];
488 } c
[SWM_S_COLOR_MAX
];
492 struct swm_screen
*screens
;
494 /* args to functions */
497 #define SWM_ARG_ID_FOCUSNEXT (0)
498 #define SWM_ARG_ID_FOCUSPREV (1)
499 #define SWM_ARG_ID_FOCUSMAIN (2)
500 #define SWM_ARG_ID_FOCUSCUR (4)
501 #define SWM_ARG_ID_SWAPNEXT (10)
502 #define SWM_ARG_ID_SWAPPREV (11)
503 #define SWM_ARG_ID_SWAPMAIN (12)
504 #define SWM_ARG_ID_MOVELAST (13)
505 #define SWM_ARG_ID_MASTERSHRINK (20)
506 #define SWM_ARG_ID_MASTERGROW (21)
507 #define SWM_ARG_ID_MASTERADD (22)
508 #define SWM_ARG_ID_MASTERDEL (23)
509 #define SWM_ARG_ID_FLIPLAYOUT (24)
510 #define SWM_ARG_ID_STACKRESET (30)
511 #define SWM_ARG_ID_STACKINIT (31)
512 #define SWM_ARG_ID_CYCLEWS_UP (40)
513 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
514 #define SWM_ARG_ID_CYCLESC_UP (42)
515 #define SWM_ARG_ID_CYCLESC_DOWN (43)
516 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
517 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
518 #define SWM_ARG_ID_STACKINC (50)
519 #define SWM_ARG_ID_STACKDEC (51)
520 #define SWM_ARG_ID_SS_ALL (60)
521 #define SWM_ARG_ID_SS_WINDOW (61)
522 #define SWM_ARG_ID_DONTCENTER (70)
523 #define SWM_ARG_ID_CENTER (71)
524 #define SWM_ARG_ID_KILLWINDOW (80)
525 #define SWM_ARG_ID_DELETEWINDOW (81)
526 #define SWM_ARG_ID_WIDTHGROW (90)
527 #define SWM_ARG_ID_WIDTHSHRINK (91)
528 #define SWM_ARG_ID_HEIGHTGROW (92)
529 #define SWM_ARG_ID_HEIGHTSHRINK (93)
530 #define SWM_ARG_ID_MOVEUP (100)
531 #define SWM_ARG_ID_MOVEDOWN (101)
532 #define SWM_ARG_ID_MOVELEFT (102)
533 #define SWM_ARG_ID_MOVERIGHT (103)
537 void focus(struct swm_region
*, union arg
*);
538 void focus_magic(struct ws_win
*);
542 TAILQ_ENTRY(quirk
) entry
;
546 #define SWM_Q_FLOAT (1<<0) /* float this window */
547 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
548 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
549 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
550 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
551 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
553 TAILQ_HEAD(quirk_list
, quirk
);
554 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
557 * Supported EWMH hints should be added to
558 * both the enum and the ewmh array
563 _NET_MOVERESIZE_WINDOW
,
564 _NET_WM_ACTION_CLOSE
,
565 _NET_WM_ACTION_FULLSCREEN
,
567 _NET_WM_ACTION_RESIZE
,
568 _NET_WM_ALLOWED_ACTIONS
,
571 _NET_WM_STATE_FULLSCREEN
,
572 _NET_WM_STATE_HIDDEN
,
573 _NET_WM_STATE_MAXIMIZED_HORZ
,
574 _NET_WM_STATE_MAXIMIZED_VERT
,
575 _NET_WM_STATE_SKIP_PAGER
,
576 _NET_WM_STATE_SKIP_TASKBAR
,
578 _NET_WM_WINDOW_TYPE_DIALOG
,
579 _NET_WM_WINDOW_TYPE_DOCK
,
580 _NET_WM_WINDOW_TYPE_NORMAL
,
581 _NET_WM_WINDOW_TYPE_SPLASH
,
582 _NET_WM_WINDOW_TYPE_TOOLBAR
,
583 _NET_WM_WINDOW_TYPE_UTILITY
,
584 _SWM_WM_STATE_MANUAL
,
591 } ewmh
[SWM_EWMH_HINT_MAX
] = {
592 /* must be in same order as in the enum */
593 {"_NET_ACTIVE_WINDOW", None
},
594 {"_NET_CLOSE_WINDOW", None
},
595 {"_NET_MOVERESIZE_WINDOW", None
},
596 {"_NET_WM_ACTION_CLOSE", None
},
597 {"_NET_WM_ACTION_FULLSCREEN", None
},
598 {"_NET_WM_ACTION_MOVE", None
},
599 {"_NET_WM_ACTION_RESIZE", None
},
600 {"_NET_WM_ALLOWED_ACTIONS", None
},
601 {"_NET_WM_STATE", None
},
602 {"_NET_WM_STATE_ABOVE", None
},
603 {"_NET_WM_STATE_FULLSCREEN", None
},
604 {"_NET_WM_STATE_HIDDEN", None
},
605 {"_NET_WM_STATE_MAXIMIZED_HORZ", None
},
606 {"_NET_WM_STATE_MAXIMIZED_VERT", None
},
607 {"_NET_WM_STATE_SKIP_PAGER", None
},
608 {"_NET_WM_STATE_SKIP_TASKBAR", None
},
609 {"_NET_WM_WINDOW_TYPE", None
},
610 {"_NET_WM_WINDOW_TYPE_DIALOG", None
},
611 {"_NET_WM_WINDOW_TYPE_DOCK", None
},
612 {"_NET_WM_WINDOW_TYPE_NORMAL", None
},
613 {"_NET_WM_WINDOW_TYPE_SPLASH", None
},
614 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None
},
615 {"_NET_WM_WINDOW_TYPE_UTILITY", None
},
616 {"_SWM_WM_STATE_MANUAL", None
},
619 void store_float_geom(struct ws_win
*, struct swm_region
*);
620 int floating_toggle_win(struct ws_win
*);
621 void constrain_window(struct ws_win
*, struct swm_region
*, int);
622 void update_window(struct ws_win
*);
623 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
624 unsigned char *get_win_name(Window
);
627 get_property(Window id
, Atom atom
, long count
, Atom type
, unsigned long *nitems
,
628 unsigned long *nbytes
, unsigned char **data
)
631 unsigned long *nbytes_ret
, *nitems_ret
;
632 unsigned long nbytes_tmp
, nitems_tmp
;
635 nbytes_ret
= nbytes
!= NULL
? nbytes
: &nbytes_tmp
;
636 nitems_ret
= nitems
!= NULL
? nitems
: &nitems_tmp
;
638 status
= XGetWindowProperty(display
, id
, atom
, 0L, count
, False
, type
,
639 &real
, &format
, nitems_ret
, nbytes_ret
, data
);
641 if (status
!= Success
)
650 update_iconic(struct ws_win
*win
, int newv
)
657 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
661 XChangeProperty(display
, win
->id
, iprop
, XA_INTEGER
, 32,
662 PropModeReplace
, (unsigned char *)&v
, 1);
664 XDeleteProperty(display
, win
->id
, iprop
);
668 get_iconic(struct ws_win
*win
)
673 unsigned long nitems
, extra
;
674 unsigned char *prop
= NULL
;
676 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
679 status
= XGetWindowProperty(display
, win
->id
, iprop
, 0L, 1L,
680 False
, XA_INTEGER
, &rettype
, &retfmt
, &nitems
, &extra
, &prop
);
681 if (status
!= Success
)
683 if (rettype
!= XA_INTEGER
|| retfmt
!= 32)
687 v
= *((int32_t *)prop
);
701 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
703 for (i
= 0; i
< LENGTH(ewmh
); i
++)
704 ewmh
[i
].atom
= XInternAtom(display
, ewmh
[i
].name
, False
);
706 for (i
= 0; i
< ScreenCount(display
); i
++) {
707 /* Support check window will be created by workaround(). */
709 /* Report supported atoms */
710 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
711 for (j
= 0; j
< LENGTH(ewmh
); j
++)
712 XChangeProperty(display
, screens
[i
].root
,
713 sup_list
, XA_ATOM
, 32,
714 PropModeAppend
, (unsigned char *)&ewmh
[j
].atom
, 1);
722 unsigned char *data
= NULL
;
724 Atom sup_check
, sup_list
;
727 sup_check
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
728 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
730 for (i
= 0; i
< ScreenCount(display
); i
++) {
731 /* Get the support check window and destroy it */
732 success
= get_property(screens
[i
].root
, sup_check
, 1, XA_WINDOW
,
737 XDestroyWindow(display
, id
);
738 XDeleteProperty(display
, screens
[i
].root
, sup_check
);
739 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
747 ewmh_autoquirk(struct ws_win
*win
)
750 unsigned long *data
= NULL
, n
;
753 success
= get_property(win
->id
, ewmh
[_NET_WM_WINDOW_TYPE
].atom
, (~0L),
754 XA_ATOM
, &n
, NULL
, (void *)&data
);
761 for (i
= 0; i
< n
; i
++) {
763 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
765 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
766 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
767 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
769 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
772 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
773 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
775 win
->quirks
= SWM_Q_FLOAT
;
783 #define SWM_EWMH_ACTION_COUNT_MAX (6)
784 #define EWMH_F_FULLSCREEN (1<<0)
785 #define EWMH_F_ABOVE (1<<1)
786 #define EWMH_F_HIDDEN (1<<2)
787 #define EWMH_F_SKIP_PAGER (1<<3)
788 #define EWMH_F_SKIP_TASKBAR (1<<4)
789 #define SWM_F_MANUAL (1<<5)
792 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
800 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%lx, "
801 "fullscreen %s\n", win
->id
, YESNO(fs
));
804 if (!win
->g_floatvalid
)
805 store_float_geom(win
, win
->ws
->r
);
807 win
->g
= win
->ws
->r
->g
;
810 if (win
->g_floatvalid
) {
811 /* refloat at last floating relative position */
812 win
->g
= win
->g_float
;
813 X(win
) += X(win
->ws
->r
);
814 Y(win
) += Y(win
->ws
->r
);
822 ewmh_update_actions(struct ws_win
*win
)
824 Atom actions
[SWM_EWMH_ACTION_COUNT_MAX
];
830 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
833 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
834 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
837 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
,
838 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)actions
, n
);
841 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
842 #define _NET_WM_STATE_ADD 1 /* add/set property */
843 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
846 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
848 unsigned int mask
= 0;
849 unsigned int changed
= 0;
850 unsigned int orig_flags
;
855 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
856 mask
= EWMH_F_FULLSCREEN
;
857 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
859 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
861 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
862 mask
= EWMH_F_SKIP_PAGER
;
863 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
864 mask
= EWMH_F_SKIP_TASKBAR
;
867 orig_flags
= win
->ewmh_flags
;
870 case _NET_WM_STATE_REMOVE
:
871 win
->ewmh_flags
&= ~mask
;
873 case _NET_WM_STATE_ADD
:
874 win
->ewmh_flags
|= mask
;
876 case _NET_WM_STATE_TOGGLE
:
877 win
->ewmh_flags
^= mask
;
881 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
883 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
885 if (!floating_toggle_win(win
))
886 win
->ewmh_flags
= orig_flags
; /* revert */
887 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
889 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
890 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
892 if (!ewmh_set_win_fullscreen(win
,
893 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
894 win
->ewmh_flags
= orig_flags
; /* revert */
896 XDeleteProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
898 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
899 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
900 XA_ATOM
, 32, PropModeAppend
,
901 (unsigned char *)&ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
, 1);
902 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
903 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
904 XA_ATOM
, 32, PropModeAppend
,
905 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
, 1);
906 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
907 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
908 XA_ATOM
, 32, PropModeAppend
,
909 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
, 1);
910 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
911 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
912 XA_ATOM
, 32, PropModeAppend
,
913 (unsigned char *)&ewmh
[_NET_WM_STATE_ABOVE
].atom
, 1);
914 if (win
->ewmh_flags
& SWM_F_MANUAL
)
915 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
916 XA_ATOM
, 32, PropModeAppend
,
917 (unsigned char *)&ewmh
[_SWM_WM_STATE_MANUAL
].atom
, 1);
921 ewmh_get_win_state(struct ws_win
*win
)
932 win
->ewmh_flags
|= EWMH_F_ABOVE
;
934 win
->ewmh_flags
|= SWM_F_MANUAL
;
936 success
= get_property(win
->id
, ewmh
[_NET_WM_STATE
].atom
,
937 (~0L), XA_ATOM
, &n
, NULL
, (void *)&states
);
942 for (i
= 0; i
< n
; i
++)
943 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
951 geteventname(XEvent
*e
)
963 name
= "ButtonPress";
966 name
= "ButtonRelease";
969 name
= "MotionNotify";
972 name
= "EnterNotify";
975 name
= "LeaveNotify";
984 name
= "KeymapNotify";
990 name
= "GraphicsExpose";
995 case VisibilityNotify
:
996 name
= "VisibilityNotify";
999 name
= "CreateNotify";
1002 name
= "DestroyNotify";
1005 name
= "UnmapNotify";
1011 name
= "MapRequest";
1013 case ReparentNotify
:
1014 name
= "ReparentNotify";
1016 case ConfigureNotify
:
1017 name
= "ConfigureNotify";
1019 case ConfigureRequest
:
1020 name
= "ConfigureRequest";
1023 name
= "GravityNotify";
1026 name
= "ResizeRequest";
1028 case CirculateNotify
:
1029 name
= "CirculateNotify";
1031 case CirculateRequest
:
1032 name
= "CirculateRequest";
1034 case PropertyNotify
:
1035 name
= "PropertyNotify";
1037 case SelectionClear
:
1038 name
= "SelectionClear";
1040 case SelectionRequest
:
1041 name
= "SelectionRequest";
1043 case SelectionNotify
:
1044 name
= "SelectionNotify";
1046 case ColormapNotify
:
1047 name
= "ColormapNotify";
1050 name
= "ClientMessage";
1053 name
= "MappingNotify";
1063 xrandr_geteventname(XEvent
*e
)
1067 switch(e
->type
- xrandr_eventbase
) {
1068 case RRScreenChangeNotify
:
1069 name
= "RRScreenChangeNotify";
1079 dumpwins(struct swm_region
*r
, union arg
*args
)
1083 XWindowAttributes wa
;
1085 if (r
->ws
== NULL
) {
1086 warnx("dumpwins: invalid workspace");
1090 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1092 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1093 state
= getstate(win
->id
);
1094 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1095 warnx("window: 0x%lx, failed XGetWindowAttributes",
1097 warnx("window: 0x%lx, map_state: %d, state: %d, "
1098 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1102 warnx("===== unmanaged window list =====");
1103 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1104 state
= getstate(win
->id
);
1105 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1106 warnx("window: 0x%lx, failed XGetWindowAttributes",
1108 warnx("window: 0x%lx, map_state: %d, state: %d, "
1109 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1113 warnx("=================================");
1117 dumpwins(struct swm_region
*r
, union arg
*args
)
1120 #endif /* SWM_DEBUG */
1122 void expose(XEvent
*);
1123 void keypress(XEvent
*);
1124 void buttonpress(XEvent
*);
1125 void configurerequest(XEvent
*);
1126 void configurenotify(XEvent
*);
1127 void destroynotify(XEvent
*);
1128 void enternotify(XEvent
*);
1129 void focusevent(XEvent
*);
1130 void mapnotify(XEvent
*);
1131 void mappingnotify(XEvent
*);
1132 void maprequest(XEvent
*);
1133 void propertynotify(XEvent
*);
1134 void unmapnotify(XEvent
*);
1135 void visibilitynotify(XEvent
*);
1136 void clientmessage(XEvent
*);
1138 void (*handler
[LASTEvent
])(XEvent
*) = {
1140 [KeyPress
] = keypress
,
1141 [ButtonPress
] = buttonpress
,
1142 [ConfigureRequest
] = configurerequest
,
1143 [ConfigureNotify
] = configurenotify
,
1144 [DestroyNotify
] = destroynotify
,
1145 [EnterNotify
] = enternotify
,
1146 [FocusIn
] = focusevent
,
1147 [FocusOut
] = focusevent
,
1148 [MapNotify
] = mapnotify
,
1149 [MappingNotify
] = mappingnotify
,
1150 [MapRequest
] = maprequest
,
1151 [PropertyNotify
] = propertynotify
,
1152 [UnmapNotify
] = unmapnotify
,
1153 [VisibilityNotify
] = visibilitynotify
,
1154 [ClientMessage
] = clientmessage
,
1160 int saved_errno
, status
;
1163 saved_errno
= errno
;
1167 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1172 if (errno
!= ECHILD
)
1173 warn("sighdlr: waitpid");
1174 #endif /* SWM_DEBUG */
1177 if (pid
== searchpid
)
1181 if (WIFEXITED(status
)) {
1182 if (WEXITSTATUS(status
) != 0)
1183 warnx("sighdlr: child exit status: %d",
1184 WEXITSTATUS(status
));
1186 warnx("sighdlr: child is terminated "
1188 #endif /* SWM_DEBUG */
1202 errno
= saved_errno
;
1208 struct pid_e
*p
= NULL
;
1210 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1215 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1224 name_to_color(char *colorname
)
1228 XColor screen_def
, exact_def
;
1229 unsigned long result
= 0;
1230 char cname
[32] = "#";
1232 cmap
= DefaultColormap(display
, screens
[0].idx
);
1233 status
= XAllocNamedColor(display
, cmap
, colorname
,
1234 &screen_def
, &exact_def
);
1236 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1237 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
1241 result
= screen_def
.pixel
;
1243 warnx("color '%s' not found", colorname
);
1249 setscreencolor(char *val
, int i
, int c
)
1251 if (i
> 0 && i
<= ScreenCount(display
)) {
1252 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1253 free(screens
[i
- 1].c
[c
].name
);
1254 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1256 } else if (i
== -1) {
1257 for (i
= 0; i
< ScreenCount(display
); i
++) {
1258 screens
[i
].c
[c
].color
= name_to_color(val
);
1259 free(screens
[i
].c
[c
].name
);
1260 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1264 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1265 i
, ScreenCount(display
));
1269 fancy_stacker(struct workspace
*ws
)
1271 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1272 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1273 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1274 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1275 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1276 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1277 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1278 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1279 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1283 plain_stacker(struct workspace
*ws
)
1285 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1286 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1287 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1288 sizeof ws
->stacker
);
1289 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1290 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1291 sizeof ws
->stacker
);
1295 custom_region(char *val
)
1297 unsigned int sidx
, x
, y
, w
, h
;
1299 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1300 errx(1, "invalid custom region, "
1301 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1302 if (sidx
< 1 || sidx
> ScreenCount(display
))
1303 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1304 sidx
, ScreenCount(display
));
1308 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1310 if (x
> DisplayWidth(display
, sidx
) ||
1311 y
> DisplayHeight(display
, sidx
) ||
1312 w
+ x
> DisplayWidth(display
, sidx
) ||
1313 h
+ y
> DisplayHeight(display
, sidx
)) {
1314 warnx("ignoring region %ux%u+%u+%u - not within screen "
1315 "boundaries (%ux%u)", w
, h
, x
, y
,
1316 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
1320 new_region(&screens
[sidx
], x
, y
, w
, h
);
1324 socket_setnonblock(int fd
)
1328 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1329 err(1, "fcntl F_GETFL");
1330 flags
|= O_NONBLOCK
;
1331 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1332 err(1, "fcntl F_SETFL");
1336 bar_print(struct swm_region
*r
, const char *s
)
1340 XRectangle ibox
, lbox
;
1343 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1345 switch (bar_justify
) {
1346 case SWM_BAR_JUSTIFY_LEFT
:
1349 case SWM_BAR_JUSTIFY_CENTER
:
1350 x
= (WIDTH(r
) - lbox
.width
) / 2;
1352 case SWM_BAR_JUSTIFY_RIGHT
:
1353 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1357 if (x
< SWM_BAR_OFFSET
)
1360 /* clear back buffer */
1361 XSetForeground(display
, r
->s
->bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1362 XFillRectangle(display
, r
->bar
->buffer
, r
->s
->bar_gc
, 0, 0,
1363 WIDTH(r
->bar
), HEIGHT(r
->bar
));
1365 /* draw back buffer */
1366 XSetForeground(display
, r
->s
->bar_gc
,
1367 r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
1368 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, r
->s
->bar_gc
,
1369 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1373 XCopyArea(display
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1374 WIDTH(r
->bar
), HEIGHT(r
->bar
), 0, 0);
1378 bar_extra_stop(void)
1382 bzero(bar_pipe
, sizeof bar_pipe
);
1385 kill(bar_pid
, SIGTERM
);
1388 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1393 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1395 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1397 if (r
->ws
->focus
->ch
.res_class
!= NULL
)
1398 strlcat(s
, r
->ws
->focus
->ch
.res_class
, sz
);
1402 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1404 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1406 if (r
->ws
->focus
->ch
.res_name
!= NULL
)
1407 strlcat(s
, r
->ws
->focus
->ch
.res_name
, sz
);
1411 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1413 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1416 bar_class_name(s
, sz
, r
);
1417 strlcat(s
, ":", sz
);
1418 bar_title_name(s
, sz
, r
);
1422 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1424 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1426 if (r
->ws
->focus
->floating
)
1427 strlcat(s
, "(f)", sz
);
1431 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1433 unsigned char *title
;
1435 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1437 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1440 strlcat(s
, (char *)title
, sz
);
1444 int urgent
[SWM_WS_MAX
];
1446 bar_urgent(char *s
, size_t sz
)
1448 XWMHints
*wmh
= NULL
;
1453 for (i
= 0; i
< workspace_limit
; i
++)
1456 for (i
= 0; i
< ScreenCount(display
); i
++)
1457 for (j
= 0; j
< workspace_limit
; j
++)
1458 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1459 wmh
= XGetWMHints(display
, win
->id
);
1463 if (wmh
->flags
& XUrgencyHint
)
1468 for (i
= 0; i
< workspace_limit
; i
++) {
1470 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1472 snprintf(b
, sizeof b
, "- ");
1478 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1480 if (r
== NULL
|| r
->ws
== NULL
)
1482 if (r
->ws
->name
!= NULL
)
1483 strlcat(s
, r
->ws
->name
, sz
);
1486 /* build the default bar format according to the defined enabled options */
1488 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1490 /* if format provided, just copy the buffers */
1491 if (bar_format
!= NULL
) {
1492 strlcpy(fmtnew
, fmtexp
, sz
);
1496 /* reset the output buffer */
1499 strlcat(fmtnew
, "+N:+I ", sz
);
1501 strlcat(fmtnew
, "+S", sz
);
1502 strlcat(fmtnew
, " ", sz
);
1504 /* only show the workspace name if there's actually one */
1505 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1506 strlcat(fmtnew
, "<+D>", sz
);
1507 strlcat(fmtnew
, "+3<", sz
);
1509 if (clock_enabled
) {
1510 strlcat(fmtnew
, fmtexp
, sz
);
1511 strlcat(fmtnew
, "+4<", sz
);
1514 /* bar_urgent already adds the space before the last asterisk */
1516 strlcat(fmtnew
, "* +U*+4<", sz
);
1518 if (title_class_enabled
) {
1519 strlcat(fmtnew
, "+C", sz
);
1520 if (title_name_enabled
== 0)
1521 strlcat(fmtnew
, "+4<", sz
);
1524 /* checks needed by the colon and floating strlcat(3) calls below */
1525 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1526 if (title_name_enabled
) {
1527 if (title_class_enabled
)
1528 strlcat(fmtnew
, ":", sz
);
1529 strlcat(fmtnew
, "+T+4<", sz
);
1531 if (window_name_enabled
) {
1532 if (r
->ws
->focus
->floating
)
1533 strlcat(fmtnew
, "+F ", sz
);
1534 strlcat(fmtnew
, "+64W ", sz
);
1538 /* finally add the action script output and the version */
1539 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1543 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1545 /* special case; no limit given, pad one space, instead */
1546 if (*limit
== sz
- 1)
1548 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1551 /* replaces the bar format character sequences (like in tmux(1)) */
1553 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1557 char tmp
[SWM_BAR_MAX
];
1561 /* reset strlcat(3) buffer */
1564 /* get number, if any */
1567 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1568 limit
= sizeof tmp
- 1;
1569 if (limit
<= 0 || limit
>= sizeof tmp
)
1570 limit
= sizeof tmp
- 1;
1572 /* there is nothing to replace (ie EOL) */
1579 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1582 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1585 bar_class_name(tmp
, sizeof tmp
, r
);
1588 bar_workspace_name(tmp
, sizeof tmp
, r
);
1591 bar_window_float(tmp
, sizeof tmp
, r
);
1594 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1597 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1600 bar_class_title_name(tmp
, sizeof tmp
, r
);
1603 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1606 bar_title_name(tmp
, sizeof tmp
, r
);
1609 bar_urgent(tmp
, sizeof tmp
);
1612 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1615 bar_window_name(tmp
, sizeof tmp
, r
);
1618 /* unknown character sequence; copy as-is */
1619 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1627 while (limit
-- > 0) {
1628 if (*offrep
>= sz
- 1)
1630 fmtrep
[(*offrep
)++] = *ptr
++;
1638 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1643 while (*fmt
!= '\0') {
1645 /* skip ordinary characters */
1648 fmtrep
[off
++] = *fmt
++;
1652 /* character sequence found; replace it */
1653 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1662 bar_fmt_expand(char *fmtexp
, size_t sz
)
1669 /* start by grabbing the current time and date */
1671 localtime_r(&tmt
, &tm
);
1673 /* figure out what to expand */
1674 if (bar_format
!= NULL
)
1676 else if (bar_format
== NULL
&& clock_enabled
)
1678 /* if nothing to expand bail out */
1684 /* copy as-is, just in case the format shouldn't be expanded below */
1685 strlcpy(fmtexp
, fmt
, sz
);
1686 /* finally pass the string through strftime(3) */
1687 #ifndef SWM_DENY_CLOCK_FORMAT
1688 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1689 warnx("format too long");
1697 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1698 char fmtrep
[SWM_BAR_MAX
];
1700 struct swm_region
*r
;
1702 /* expand the format by first passing it through strftime(3) */
1703 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1705 for (i
= 0; i
< ScreenCount(display
); i
++) {
1706 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1709 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1710 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1711 bar_print(r
, fmtrep
);
1722 if (bar_enabled
== 0)
1724 if (bar_extra
&& bar_extra_running
) {
1725 /* ignore short reads; it'll correct itself */
1726 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1727 if (b
&& b
[len
- 1] == '\n') {
1729 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1731 if (b
== NULL
&& errno
!= EAGAIN
) {
1732 warn("bar_update: bar_extra failed");
1736 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1749 bar_toggle(struct swm_region
*r
, union arg
*args
)
1751 struct swm_region
*tmpr
;
1752 int i
, sc
= ScreenCount(display
);
1754 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1757 for (i
= 0; i
< sc
; i
++)
1758 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1760 XUnmapWindow(display
, tmpr
->bar
->id
);
1762 for (i
= 0; i
< sc
; i
++)
1763 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1765 XMapRaised(display
, tmpr
->bar
->id
);
1768 bar_enabled
= !bar_enabled
;
1771 /* must be after stack */
1778 XSetWindowAttributes wa
;
1779 struct swm_region
*r
;
1782 /* do this here because the conf file is in memory */
1783 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1784 /* launch external status app */
1785 bar_extra_running
= 1;
1786 if (pipe(bar_pipe
) == -1)
1787 err(1, "pipe error");
1788 socket_setnonblock(bar_pipe
[0]);
1789 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1790 if (dup2(bar_pipe
[0], 0) == -1)
1792 if (dup2(bar_pipe
[1], 1) == -1)
1794 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1795 err(1, "could not disable SIGPIPE");
1796 switch (bar_pid
= fork()) {
1798 err(1, "cannot fork");
1802 execvp(bar_argv
[0], bar_argv
);
1803 err(1, "%s external app failed", bar_argv
[0]);
1805 default: /* parent */
1811 bzero(&wa
, sizeof wa
);
1812 for (i
= 0; i
< ScreenCount(display
); i
++)
1813 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1817 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1818 wa
.background_pixel
=
1819 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1820 XChangeWindowAttributes(display
, r
->bar
->id
,
1821 CWBackPixel
| CWBorderPixel
, &wa
);
1827 bar_setup(struct swm_region
*r
)
1829 char *default_string
;
1830 char **missing_charsets
;
1831 int num_missing_charsets
= 0;
1835 XFreeFontSet(display
, bar_fs
);
1839 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
1840 err(1, "bar_setup: calloc: failed to allocate memory.");
1842 DNPRINTF(SWM_D_BAR
, "bar_setup: loading bar_fonts: %s\n", bar_fonts
);
1844 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
1845 &num_missing_charsets
, &default_string
);
1847 if (num_missing_charsets
> 0) {
1848 warnx("Unable to load charset(s):");
1850 for (i
= 0; i
< num_missing_charsets
; ++i
)
1851 warnx("%s", missing_charsets
[i
]);
1853 XFreeStringList(missing_charsets
);
1855 if (strcmp(default_string
, ""))
1856 warnx("Glyphs from those sets will be replaced "
1857 "by '%s'.", default_string
);
1859 warnx("Glyphs from those sets won't be drawn.");
1863 errx(1, "Error creating font set structure.");
1865 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
1867 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
1868 2 * bar_border_width
;
1874 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1875 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
1876 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
1878 r
->bar
->id
= XCreateSimpleWindow(display
,
1879 r
->s
->root
, X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
1880 bar_border_width
, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
1881 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1883 r
->bar
->buffer
= XCreatePixmap(display
, r
->bar
->id
, WIDTH(r
->bar
),
1884 HEIGHT(r
->bar
), DefaultDepth(display
, r
->s
->idx
));
1886 XSelectInput(display
, r
->bar
->id
, VisibilityChangeMask
);
1889 XMapRaised(display
, r
->bar
->id
);
1891 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%lx, (x,y) w x h: (%d,%d) "
1892 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
1895 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1896 err(1, "could not install bar_signal");
1901 bar_cleanup(struct swm_region
*r
)
1905 XDestroyWindow(display
, r
->bar
->id
);
1906 XFreePixmap(display
, r
->bar
->buffer
);
1912 drain_enter_notify(void)
1917 while (XCheckMaskEvent(display
, EnterWindowMask
, &cne
))
1920 DNPRINTF(SWM_D_EVENT
, "drain_enter_notify: drained: %d\n", i
);
1924 set_win_state(struct ws_win
*win
, long state
)
1926 long data
[] = {state
, None
};
1928 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%lx\n", win
->id
);
1933 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1934 (unsigned char *)data
, 2);
1941 unsigned char *p
= NULL
;
1944 if (!get_property(w
, astate
, 2L, astate
, &n
, NULL
, &p
))
1947 result
= *((long *)p
);
1953 version(struct swm_region
*r
, union arg
*args
)
1955 bar_version
= !bar_version
;
1957 snprintf(bar_vertext
, sizeof bar_vertext
,
1958 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
1960 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
1965 client_msg(struct ws_win
*win
, Atom a
)
1967 XClientMessageEvent cm
;
1972 bzero(&cm
, sizeof cm
);
1973 cm
.type
= ClientMessage
;
1974 cm
.window
= win
->id
;
1975 cm
.message_type
= aprot
;
1978 cm
.data
.l
[1] = CurrentTime
;
1979 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
1982 /* synthetic response to a ConfigureRequest when not making a change */
1984 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
1991 /* send notification of unchanged state. */
1992 ce
.type
= ConfigureNotify
;
1995 ce
.width
= WIDTH(win
);
1996 ce
.height
= HEIGHT(win
);
1997 ce
.override_redirect
= False
;
2001 ce
.display
= display
;
2003 ce
.window
= win
->id
;
2004 ce
.border_width
= BORDER(win
);
2008 ce
.display
= ev
->display
;
2009 ce
.event
= ev
->window
;
2010 ce
.window
= ev
->window
;
2012 /* make response appear more WM_SIZE_HINTS-compliant */
2014 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%lx,"
2015 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
2016 "%d x %d\n", win
->id
, win
->sh_mask
, SH_MIN_W(win
),
2017 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2018 SH_INC_W(win
), SH_INC_H(win
));
2022 /* the hint may be set... to 0! */
2023 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2024 ce
.width
= SH_MIN_W(win
);
2025 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2026 ce
.height
= SH_MIN_H(win
);
2031 /* may also be advertized as 0 */
2032 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2033 ce
.width
= SH_MAX_W(win
);
2034 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2035 ce
.height
= SH_MAX_H(win
);
2038 /* resize increment. */
2040 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2041 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2043 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2044 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2048 /* adjust x and y for requested border_width. */
2049 ce
.x
+= BORDER(win
) - ev
->border_width
;
2050 ce
.y
+= BORDER(win
) - ev
->border_width
;
2051 ce
.border_width
= ev
->border_width
;
2052 ce
.above
= ev
->above
;
2055 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
2056 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2057 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2059 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
2063 count_win(struct workspace
*ws
, int count_transient
)
2068 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2069 if (count_transient
== 0 && win
->floating
)
2071 if (count_transient
== 0 && win
->transient
)
2077 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2083 quit(struct swm_region
*r
, union arg
*args
)
2085 DNPRINTF(SWM_D_MISC
, "quit\n");
2090 unmap_window(struct ws_win
*win
)
2095 /* don't unmap again */
2096 if (getstate(win
->id
) == IconicState
)
2099 set_win_state(win
, IconicState
);
2101 XUnmapWindow(display
, win
->id
);
2102 XSetWindowBorder(display
, win
->id
,
2103 win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2112 for (i
= 0; i
< ScreenCount(display
); i
++)
2113 for (j
= 0; j
< workspace_limit
; j
++)
2114 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2119 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
2126 event
.display
= display
; /* Ignored, but what the hell */
2127 event
.window
= win
->id
;
2128 event
.root
= win
->s
->root
;
2129 event
.subwindow
= None
;
2130 event
.time
= CurrentTime
;
2135 event
.same_screen
= True
;
2136 event
.keycode
= XKeysymToKeycode(display
, keysym
);
2137 event
.state
= modifiers
;
2139 event
.type
= KeyPress
;
2140 XSendEvent(event
.display
, event
.window
, True
,
2141 KeyPressMask
, (XEvent
*)&event
);
2143 event
.type
= KeyRelease
;
2144 XSendEvent(event
.display
, event
.window
, True
,
2145 KeyPressMask
, (XEvent
*)&event
);
2150 restart(struct swm_region
*r
, union arg
*args
)
2152 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2154 /* disable alarm because the following code may not be interrupted */
2156 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
2157 err(1, "can't disable alarm");
2162 XCloseDisplay(display
);
2163 execvp(start_argv
[0], start_argv
);
2164 warn("execvp failed");
2169 root_to_region(Window root
)
2171 struct swm_region
*r
= NULL
;
2173 int i
, x
, y
, wx
, wy
;
2176 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%lx\n", root
);
2178 for (i
= 0; i
< ScreenCount(display
); i
++)
2179 if (screens
[i
].root
== root
)
2182 if (XQueryPointer(display
, screens
[i
].root
,
2183 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
2184 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: (%d,%d)\n",
2186 /* choose a region based on pointer location */
2187 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2188 if (X(r
) <= x
&& x
< MAX_X(r
) &&
2189 Y(r
) <= y
&& y
< MAX_Y(r
))
2194 r
= TAILQ_FIRST(&screens
[i
].rl
);
2200 find_unmanaged_window(Window id
)
2205 for (i
= 0; i
< ScreenCount(display
); i
++)
2206 for (j
= 0; j
< workspace_limit
; j
++)
2207 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2215 find_window(Window id
)
2218 Window wrr
, wpr
, *wcr
= NULL
;
2222 for (i
= 0; i
< ScreenCount(display
); i
++)
2223 for (j
= 0; j
< workspace_limit
; j
++)
2224 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2228 /* if we were looking for the parent return that window instead */
2229 if (XQueryTree(display
, id
, &wrr
, &wpr
, &wcr
, &nc
) == 0)
2234 /* ignore not found and root */
2235 if (wpr
== 0 || wrr
== wpr
)
2238 /* look for parent */
2239 for (i
= 0; i
< ScreenCount(display
); i
++)
2240 for (j
= 0; j
< workspace_limit
; j
++)
2241 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2249 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2254 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2257 close(ConnectionNumber(display
));
2259 setenv("LD_PRELOAD", SWM_LIB
, 1);
2261 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2262 warn("spawn: asprintf SWM_WS");
2265 setenv("_SWM_WS", ret
, 1);
2269 if (asprintf(&ret
, "%d", getpid()) == -1) {
2270 warn("spawn: asprintf _SWM_PID");
2273 setenv("_SWM_PID", ret
, 1);
2277 if (setsid() == -1) {
2278 warn("spawn: setsid");
2284 * close stdin and stdout to prevent interaction between apps
2285 * and the baraction script
2286 * leave stderr open to record errors
2288 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2289 warn("spawn: open");
2292 dup2(fd
, STDIN_FILENO
);
2293 dup2(fd
, STDOUT_FILENO
);
2298 execvp(args
->argv
[0], args
->argv
);
2300 warn("spawn: execvp");
2305 kill_refs(struct ws_win
*win
)
2308 struct swm_region
*r
;
2309 struct workspace
*ws
;
2314 for (i
= 0; i
< ScreenCount(display
); i
++)
2315 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2316 for (x
= 0; x
< workspace_limit
; x
++) {
2318 if (win
== ws
->focus
)
2320 if (win
== ws
->focus_prev
)
2321 ws
->focus_prev
= NULL
;
2326 validate_win(struct ws_win
*testwin
)
2329 struct workspace
*ws
;
2330 struct swm_region
*r
;
2333 if (testwin
== NULL
)
2336 for (i
= 0; i
< ScreenCount(display
); i
++)
2337 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2338 for (x
= 0; x
< workspace_limit
; x
++) {
2340 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2348 validate_ws(struct workspace
*testws
)
2350 struct swm_region
*r
;
2351 struct workspace
*ws
;
2354 /* validate all ws */
2355 for (i
= 0; i
< ScreenCount(display
); i
++)
2356 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2357 for (x
= 0; x
< workspace_limit
; x
++) {
2366 unfocus_win(struct ws_win
*win
)
2371 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%lx\n", WINID(win
));
2375 if (win
->ws
== NULL
)
2378 if (validate_ws(win
->ws
))
2379 return; /* XXX this gets hit with thunderbird, needs fixing */
2381 if (win
->ws
->r
== NULL
)
2384 if (validate_win(win
)) {
2389 if (win
->ws
->focus
== win
) {
2390 win
->ws
->focus
= NULL
;
2391 win
->ws
->focus_prev
= win
;
2394 if (validate_win(win
->ws
->focus
)) {
2395 kill_refs(win
->ws
->focus
);
2396 win
->ws
->focus
= NULL
;
2398 if (validate_win(win
->ws
->focus_prev
)) {
2399 kill_refs(win
->ws
->focus_prev
);
2400 win
->ws
->focus_prev
= NULL
;
2403 /* drain all previous unfocus events */
2404 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
2407 grabbuttons(win
, 0);
2408 XSetWindowBorder(display
, win
->id
,
2409 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2411 XChangeProperty(display
, win
->s
->root
,
2412 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2413 PropModeReplace
, (unsigned char *)&none
, 1);
2422 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2424 for (i
= 0; i
< ScreenCount(display
); i
++)
2425 for (j
= 0; j
< workspace_limit
; j
++)
2426 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2431 focus_win(struct ws_win
*win
)
2436 struct ws_win
*cfw
= NULL
;
2439 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%lx\n", WINID(win
));
2443 if (win
->ws
== NULL
)
2446 if (validate_ws(win
->ws
))
2447 return; /* XXX this gets hit with thunderbird, needs fixing */
2449 if (validate_win(win
)) {
2454 if (validate_win(win
)) {
2459 XGetInputFocus(display
, &cur_focus
, &rr
);
2460 if ((cfw
= find_window(cur_focus
)) != NULL
)
2463 /* use larger hammer since the window was killed somehow */
2464 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2465 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2466 XSetWindowBorder(display
, cfw
->id
,
2467 cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2470 win
->ws
->focus
= win
;
2472 if (win
->ws
->r
!= NULL
) {
2473 /* drain all previous focus events */
2474 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
2478 XSetInputFocus(display
, win
->id
,
2479 RevertToParent
, CurrentTime
);
2480 grabbuttons(win
, 1);
2481 XSetWindowBorder(display
, win
->id
,
2482 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2483 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2484 win
->ws
->always_raise
)
2485 XMapRaised(display
, win
->id
);
2487 XChangeProperty(display
, win
->s
->root
,
2488 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2489 PropModeReplace
, (unsigned char *)&win
->id
, 1);
2496 switchws(struct swm_region
*r
, union arg
*args
)
2498 int wsid
= args
->id
, unmap_old
= 0;
2499 struct swm_region
*this_r
, *other_r
;
2501 struct workspace
*new_ws
, *old_ws
;
2507 if (wsid
>= workspace_limit
)
2511 old_ws
= this_r
->ws
;
2512 new_ws
= &this_r
->s
->ws
[wsid
];
2514 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2515 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2517 if (new_ws
== NULL
|| old_ws
== NULL
)
2519 if (new_ws
== old_ws
)
2522 other_r
= new_ws
->r
;
2523 if (other_r
== NULL
) {
2524 /* the other workspace is hidden, hide this one */
2528 /* the other ws is visible in another region, exchange them */
2529 other_r
->ws_prior
= new_ws
;
2530 other_r
->ws
= old_ws
;
2531 old_ws
->r
= other_r
;
2533 this_r
->ws_prior
= old_ws
;
2534 this_r
->ws
= new_ws
;
2537 /* this is needed so that we can click on a window after a restart */
2541 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2542 focus(new_ws
->r
, &a
);
2544 /* unmap old windows */
2546 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2549 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2550 drain_enter_notify();
2554 cyclews(struct swm_region
*r
, union arg
*args
)
2557 struct swm_screen
*s
= r
->s
;
2560 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2561 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2566 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2569 case SWM_ARG_ID_CYCLEWS_UP
:
2570 if (a
.id
< workspace_limit
- 1)
2575 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2578 case SWM_ARG_ID_CYCLEWS_DOWN
:
2582 a
.id
= workspace_limit
- 1;
2589 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2591 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2595 } while (a
.id
!= r
->ws
->idx
);
2599 priorws(struct swm_region
*r
, union arg
*args
)
2603 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2604 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2606 if (r
->ws_prior
== NULL
)
2609 a
.id
= r
->ws_prior
->idx
;
2614 cyclescr(struct swm_region
*r
, union arg
*args
)
2616 struct swm_region
*rr
= NULL
;
2620 /* do nothing if we don't have more than one screen */
2621 if (!(ScreenCount(display
) > 1 || outputs
> 1))
2626 case SWM_ARG_ID_CYCLESC_UP
:
2627 rr
= TAILQ_NEXT(r
, entry
);
2629 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2631 case SWM_ARG_ID_CYCLESC_DOWN
:
2632 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2634 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2642 /* move mouse to region */
2644 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2645 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2647 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2650 if (rr
->ws
->focus
) {
2651 /* move to focus window */
2652 x
= X(rr
->ws
->focus
) + 1;
2653 y
= Y(rr
->ws
->focus
) + 1;
2654 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2659 sort_windows(struct ws_win_list
*wl
)
2661 struct ws_win
*win
, *parent
, *nxt
;
2666 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2667 nxt
= TAILQ_NEXT(win
, entry
);
2668 if (win
->transient
) {
2669 parent
= find_window(win
->transient
);
2670 if (parent
== NULL
) {
2671 warnx("not possible bug");
2674 TAILQ_REMOVE(wl
, win
, entry
);
2675 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2682 swapwin(struct swm_region
*r
, union arg
*args
)
2684 struct ws_win
*target
, *source
;
2685 struct ws_win
*cur_focus
;
2686 struct ws_win_list
*wl
;
2689 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2690 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2692 cur_focus
= r
->ws
->focus
;
2693 if (cur_focus
== NULL
)
2697 wl
= &source
->ws
->winlist
;
2700 case SWM_ARG_ID_SWAPPREV
:
2701 if (source
->transient
)
2702 source
= find_window(source
->transient
);
2703 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2704 if (target
&& target
->transient
)
2705 target
= find_window(target
->transient
);
2706 TAILQ_REMOVE(wl
, source
, entry
);
2708 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2710 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2712 case SWM_ARG_ID_SWAPNEXT
:
2713 target
= TAILQ_NEXT(source
, entry
);
2714 /* move the parent and let the sort handle the move */
2715 if (source
->transient
)
2716 source
= find_window(source
->transient
);
2717 TAILQ_REMOVE(wl
, source
, entry
);
2719 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2721 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2723 case SWM_ARG_ID_SWAPMAIN
:
2724 target
= TAILQ_FIRST(wl
);
2725 if (target
== source
) {
2726 if (source
->ws
->focus_prev
!= NULL
&&
2727 source
->ws
->focus_prev
!= target
)
2728 source
= source
->ws
->focus_prev
;
2732 if (target
== NULL
|| source
== NULL
)
2734 source
->ws
->focus_prev
= target
;
2735 TAILQ_REMOVE(wl
, target
, entry
);
2736 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2737 TAILQ_REMOVE(wl
, source
, entry
);
2738 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2740 case SWM_ARG_ID_MOVELAST
:
2741 TAILQ_REMOVE(wl
, source
, entry
);
2742 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2745 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2755 focus_prev(struct ws_win
*win
)
2757 struct ws_win
*winfocus
= NULL
;
2758 struct ws_win
*cur_focus
= NULL
;
2759 struct ws_win_list
*wl
= NULL
;
2760 struct workspace
*ws
= NULL
;
2762 if (!(win
&& win
->ws
))
2767 cur_focus
= ws
->focus
;
2769 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%lx, cur_focus: 0x%lx\n",
2770 WINID(win
), WINID(cur_focus
));
2772 /* pickle, just focus on whatever */
2773 if (cur_focus
== NULL
) {
2774 /* use prev_focus if valid */
2775 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2776 find_window(WINID(ws
->focus_prev
)))
2777 winfocus
= ws
->focus_prev
;
2781 /* if transient focus on parent */
2782 if (cur_focus
->transient
) {
2783 winfocus
= find_window(cur_focus
->transient
);
2787 /* if in max_stack try harder */
2788 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2789 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2790 if (cur_focus
!= ws
->focus_prev
)
2791 winfocus
= ws
->focus_prev
;
2793 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2798 DNPRINTF(SWM_D_FOCUS
, "focus_prev: focus_close: %d\n", focus_close
);
2800 if (winfocus
== NULL
|| winfocus
== win
) {
2801 switch (focus_close
) {
2802 case SWM_STACK_BOTTOM
:
2803 winfocus
= TAILQ_FIRST(wl
);
2806 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2808 case SWM_STACK_ABOVE
:
2809 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
2810 if (focus_close_wrap
)
2811 winfocus
= TAILQ_FIRST(wl
);
2813 winfocus
= TAILQ_PREV(cur_focus
,
2814 ws_win_list
, entry
);
2817 case SWM_STACK_BELOW
:
2818 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
2820 if (focus_close_wrap
)
2821 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2823 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2829 if (winfocus
== NULL
) {
2830 if (focus_default
== SWM_STACK_TOP
)
2831 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2833 winfocus
= TAILQ_FIRST(wl
);
2837 focus_magic(winfocus
);
2841 focus(struct swm_region
*r
, union arg
*args
)
2843 struct ws_win
*winfocus
= NULL
, *head
;
2844 struct ws_win
*cur_focus
= NULL
;
2845 struct ws_win_list
*wl
= NULL
;
2846 struct workspace
*ws
= NULL
;
2852 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2854 /* treat FOCUS_CUR special */
2855 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2856 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2857 winfocus
= r
->ws
->focus
;
2858 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2859 winfocus
= r
->ws
->focus_prev
;
2861 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2862 if (winfocus
->iconic
== 0)
2865 focus_magic(winfocus
);
2869 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2873 if (TAILQ_EMPTY(wl
))
2875 /* make sure there is at least one uniconified window */
2877 TAILQ_FOREACH(winfocus
, wl
, entry
)
2878 if (winfocus
->iconic
== 0) {
2886 case SWM_ARG_ID_FOCUSPREV
:
2887 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2889 head
= TAILQ_LAST(wl
, ws_win_list
);
2891 if (WINID(winfocus
) == cur_focus
->transient
) {
2892 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2894 head
= TAILQ_LAST(wl
, ws_win_list
);
2899 if (winfocus
&& winfocus
->iconic
) {
2900 while (winfocus
!= cur_focus
) {
2901 if (winfocus
== NULL
)
2902 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2903 if (winfocus
->iconic
== 0)
2905 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
2911 case SWM_ARG_ID_FOCUSNEXT
:
2912 head
= TAILQ_NEXT(cur_focus
, entry
);
2914 head
= TAILQ_FIRST(wl
);
2918 if (winfocus
&& winfocus
->iconic
) {
2919 while (winfocus
!= cur_focus
) {
2920 if (winfocus
== NULL
)
2921 winfocus
= TAILQ_FIRST(wl
);
2922 if (winfocus
->iconic
== 0)
2924 winfocus
= TAILQ_NEXT(winfocus
, entry
);
2929 case SWM_ARG_ID_FOCUSMAIN
:
2930 winfocus
= TAILQ_FIRST(wl
);
2931 if (winfocus
== cur_focus
)
2932 winfocus
= cur_focus
->ws
->focus_prev
;
2939 focus_magic(winfocus
);
2943 cycle_layout(struct swm_region
*r
, union arg
*args
)
2945 struct workspace
*ws
= r
->ws
;
2948 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2951 if (ws
->cur_layout
->l_stack
== NULL
)
2952 ws
->cur_layout
= &layouts
[0];
2955 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2956 drain_enter_notify();
2957 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2962 stack_config(struct swm_region
*r
, union arg
*args
)
2964 struct workspace
*ws
= r
->ws
;
2966 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
2969 if (ws
->cur_layout
->l_config
!= NULL
)
2970 ws
->cur_layout
->l_config(ws
, args
->id
);
2972 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
2979 struct swm_geometry g
;
2980 struct swm_region
*r
;
2986 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
2988 for (i
= 0; i
< ScreenCount(display
); i
++) {
2992 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2993 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
2994 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
2996 /* start with screen geometry, adjust for bar */
2998 g
.w
-= 2 * border_width
;
2999 g
.h
-= 2 * border_width
;
3005 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3006 r
->ws
->cur_layout
->l_string(r
->ws
);
3007 /* save r so we can track region changes */
3014 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3015 drain_enter_notify();
3017 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3021 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3023 /* retain window geom and region geom */
3024 win
->g_float
= win
->g
;
3025 win
->g_float
.x
-= X(r
);
3026 win
->g_float
.y
-= Y(r
);
3027 win
->g_floatvalid
= 1;
3028 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%lx, g: (%d,%d)"
3029 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3030 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3031 win
->g_float
.w
, win
->g_float
.h
);
3035 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3040 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%lx\n", win
->id
);
3043 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3044 * geom on ws switches or return from max mode
3046 if (win
->g_floatvalid
&& (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3047 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)))) {
3048 /* refloat at last floating relative position */
3049 win
->g
= win
->g_float
;
3054 win
->floatmaxed
= 0;
3057 * if set to fullscreen mode, configure window to maximum size.
3059 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3060 if (!win
->g_floatvalid
)
3061 store_float_geom(win
, win
->ws
->r
);
3063 win
->g
= win
->ws
->r
->g
;
3067 * remove border on fullscreen floater when in fullscreen mode or when
3068 * the quirk is present.
3070 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3071 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3072 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3073 if (win
->bordered
) {
3075 X(win
) += border_width
;
3076 Y(win
) += border_width
;
3078 } else if (!win
->bordered
) {
3080 X(win
) -= border_width
;
3081 Y(win
) -= border_width
;
3084 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3085 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3086 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3091 * floaters and transients are auto-centred unless moved
3094 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3095 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3098 /* keep window within region bounds */
3099 constrain_window(win
, r
, 0);
3105 * Send keystrokes to terminal to decrease/increase the font size as the
3106 * window size changes.
3109 adjust_font(struct ws_win
*win
)
3111 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3112 win
->floating
|| win
->transient
)
3115 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3116 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3117 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3118 win
->font_size_boundary
[win
->font_steps
] =
3119 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3122 win
->last_inc
= win
->sh
.width_inc
;
3123 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
3124 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3125 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3128 win
->last_inc
= win
->sh
.width_inc
;
3129 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
3133 #define SWAPXY(g) do { \
3135 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3136 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3139 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3141 XWindowAttributes wa
;
3142 struct swm_geometry win_g
, r_g
= *g
;
3143 struct ws_win
*win
, *fs_win
= NULL
;
3144 int i
, j
, s
, stacks
;
3145 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3146 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3147 int split
, colno
, winno
, mwin
, msize
, mscale
;
3148 int remain
, missing
, v_slice
, reconfigure
;
3151 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3152 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3154 winno
= count_win(ws
, 0);
3155 if (winno
== 0 && count_win(ws
, 1) == 0)
3158 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3159 if (win
->transient
== 0 && win
->floating
== 0
3160 && win
->iconic
== 0)
3167 w_inc
= win
->sh
.width_inc
;
3168 w_base
= win
->sh
.base_width
;
3169 mwin
= ws
->l_state
.horizontal_mwin
;
3170 mscale
= ws
->l_state
.horizontal_msize
;
3171 stacks
= ws
->l_state
.horizontal_stacks
;
3174 w_inc
= win
->sh
.height_inc
;
3175 w_base
= win
->sh
.base_height
;
3176 mwin
= ws
->l_state
.vertical_mwin
;
3177 mscale
= ws
->l_state
.vertical_msize
;
3178 stacks
= ws
->l_state
.vertical_stacks
;
3182 if (stacks
> winno
- mwin
)
3183 stacks
= winno
- mwin
;
3187 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3188 if (mwin
&& winno
> mwin
) {
3189 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3193 win_g
.w
= v_slice
* mscale
;
3195 if (w_inc
> 1 && w_inc
< v_slice
) {
3196 /* adjust for window's requested size increment */
3197 remain
= (win_g
.w
- w_base
) % w_inc
;
3203 win_g
.x
+= r_g
.w
- msize
;
3206 colno
= split
= winno
/ stacks
;
3207 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3208 2 * border_width
) / stacks
);
3210 hrh
= r_g
.h
/ colno
;
3211 extra
= r_g
.h
- (colno
* hrh
);
3212 win_g
.h
= hrh
- 2 * border_width
;
3214 /* stack all the tiled windows */
3215 i
= j
= 0, s
= stacks
;
3216 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3217 if (win
->transient
!= 0 || win
->floating
!= 0)
3219 if (win
->iconic
!= 0)
3222 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3227 if (split
&& i
== split
) {
3228 colno
= (winno
- mwin
) / stacks
;
3229 if (s
<= (winno
- mwin
) % stacks
)
3231 split
= split
+ colno
;
3232 hrh
= (r_g
.h
/ colno
);
3233 extra
= r_g
.h
- (colno
* hrh
);
3237 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3238 win_g
.w
= (r_g
.w
- msize
-
3239 (stacks
* 2 * border_width
)) / stacks
;
3241 win_g
.w
+= (r_g
.w
- msize
-
3242 (stacks
* 2 * border_width
)) % stacks
;
3246 win_g
.h
= hrh
- 2 * border_width
;
3248 h_inc
= win
->sh
.width_inc
;
3249 h_base
= win
->sh
.base_width
;
3251 h_inc
= win
->sh
.height_inc
;
3252 h_base
= win
->sh
.base_height
;
3254 if (j
== colno
- 1) {
3255 win_g
.h
= hrh
+ extra
;
3256 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3257 /* adjust for window's requested size increment */
3258 remain
= (win_g
.h
- h_base
) % h_inc
;
3259 missing
= h_inc
- remain
;
3261 if (missing
<= extra
|| j
== 0) {
3273 win_g
.y
+= last_h
+ 2 * border_width
;
3275 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3277 win_g
.w
+= 2 * border_width
;
3278 win_g
.h
+= 2 * border_width
;
3283 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3284 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3288 WIDTH(win
) = win_g
.h
;
3289 HEIGHT(win
) = win_g
.w
;
3292 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3293 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3297 WIDTH(win
) = win_g
.w
;
3298 HEIGHT(win
) = win_g
.h
;
3302 if (bordered
!= win
->bordered
) {
3304 win
->bordered
= bordered
;
3312 if (XGetWindowAttributes(display
, win
->id
, &wa
))
3313 if (wa
.map_state
== IsUnmapped
)
3314 XMapRaised(display
, win
->id
);
3322 /* now, stack all the floaters and transients */
3323 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3324 if (win
->transient
== 0 && win
->floating
== 0)
3326 if (win
->iconic
== 1)
3328 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3333 stack_floater(win
, ws
->r
);
3334 XMapRaised(display
, win
->id
);
3338 stack_floater(fs_win
, ws
->r
);
3339 XMapRaised(display
, fs_win
->id
);
3344 vertical_config(struct workspace
*ws
, int id
)
3346 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3350 case SWM_ARG_ID_STACKRESET
:
3351 case SWM_ARG_ID_STACKINIT
:
3352 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3353 ws
->l_state
.vertical_mwin
= 1;
3354 ws
->l_state
.vertical_stacks
= 1;
3356 case SWM_ARG_ID_MASTERSHRINK
:
3357 if (ws
->l_state
.vertical_msize
> 1)
3358 ws
->l_state
.vertical_msize
--;
3360 case SWM_ARG_ID_MASTERGROW
:
3361 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3362 ws
->l_state
.vertical_msize
++;
3364 case SWM_ARG_ID_MASTERADD
:
3365 ws
->l_state
.vertical_mwin
++;
3367 case SWM_ARG_ID_MASTERDEL
:
3368 if (ws
->l_state
.vertical_mwin
> 0)
3369 ws
->l_state
.vertical_mwin
--;
3371 case SWM_ARG_ID_STACKINC
:
3372 ws
->l_state
.vertical_stacks
++;
3374 case SWM_ARG_ID_STACKDEC
:
3375 if (ws
->l_state
.vertical_stacks
> 1)
3376 ws
->l_state
.vertical_stacks
--;
3378 case SWM_ARG_ID_FLIPLAYOUT
:
3379 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3387 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3389 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3391 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3395 horizontal_config(struct workspace
*ws
, int id
)
3397 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3400 case SWM_ARG_ID_STACKRESET
:
3401 case SWM_ARG_ID_STACKINIT
:
3402 ws
->l_state
.horizontal_mwin
= 1;
3403 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3404 ws
->l_state
.horizontal_stacks
= 1;
3406 case SWM_ARG_ID_MASTERSHRINK
:
3407 if (ws
->l_state
.horizontal_msize
> 1)
3408 ws
->l_state
.horizontal_msize
--;
3410 case SWM_ARG_ID_MASTERGROW
:
3411 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3412 ws
->l_state
.horizontal_msize
++;
3414 case SWM_ARG_ID_MASTERADD
:
3415 ws
->l_state
.horizontal_mwin
++;
3417 case SWM_ARG_ID_MASTERDEL
:
3418 if (ws
->l_state
.horizontal_mwin
> 0)
3419 ws
->l_state
.horizontal_mwin
--;
3421 case SWM_ARG_ID_STACKINC
:
3422 ws
->l_state
.horizontal_stacks
++;
3424 case SWM_ARG_ID_STACKDEC
:
3425 if (ws
->l_state
.horizontal_stacks
> 1)
3426 ws
->l_state
.horizontal_stacks
--;
3428 case SWM_ARG_ID_FLIPLAYOUT
:
3429 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3437 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3439 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3441 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3444 /* fullscreen view */
3446 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3448 struct swm_geometry gg
= *g
;
3449 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3452 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3457 winno
= count_win(ws
, 0);
3458 if (winno
== 0 && count_win(ws
, 1) == 0)
3461 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3462 if (win
->transient
) {
3464 parent
= find_window(win
->transient
);
3468 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3470 * retain geometry for retrieval on exit from
3473 store_float_geom(win
, ws
->r
);
3474 win
->floatmaxed
= 1;
3477 /* only reconfigure if necessary */
3478 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3479 HEIGHT(win
) != gg
.h
) {
3485 WIDTH(win
) += 2 * border_width
;
3486 HEIGHT(win
) += 2 * border_width
;
3491 /* unmap only if we don't have multi screen */
3492 if (win
!= ws
->focus
)
3493 if (!(ScreenCount(display
) > 1 || outputs
> 1))
3497 /* put the last transient on top */
3500 XMapRaised(display
, parent
->id
);
3501 stack_floater(wintrans
, ws
->r
);
3502 focus_magic(wintrans
);
3507 send_to_ws(struct swm_region
*r
, union arg
*args
)
3509 int wsid
= args
->id
;
3510 struct ws_win
*win
= NULL
, *parent
;
3511 struct workspace
*ws
, *nws
;
3512 Atom ws_idx_atom
= 0;
3513 unsigned char ws_idx_str
[SWM_PROPLEN
];
3516 if (wsid
>= workspace_limit
)
3519 if (r
&& r
->ws
&& r
->ws
->focus
)
3525 if (win
->ws
->idx
== wsid
)
3528 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%lx\n", win
->id
);
3531 nws
= &win
->s
->ws
[wsid
];
3533 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3535 if (win
->transient
) {
3536 parent
= find_window(win
->transient
);
3538 unmap_window(parent
);
3539 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3540 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3545 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3546 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3547 if (TAILQ_EMPTY(&ws
->winlist
))
3548 r
->ws
->focus
= NULL
;
3551 /* Try to update the window's workspace property */
3552 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
3554 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3556 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3558 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
3559 PropModeReplace
, ws_idx_str
, strlen((char *)ws_idx_str
));
3567 pressbutton(struct swm_region
*r
, union arg
*args
)
3569 XTestFakeButtonEvent(display
, args
->id
, True
, CurrentTime
);
3570 XTestFakeButtonEvent(display
, args
->id
, False
, CurrentTime
);
3574 raise_toggle(struct swm_region
*r
, union arg
*args
)
3576 if (r
== NULL
|| r
->ws
== NULL
)
3579 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3581 /* bring floaters back to top */
3582 if (r
->ws
->always_raise
== 0)
3587 iconify(struct swm_region
*r
, union arg
*args
)
3591 if (r
->ws
->focus
== NULL
)
3593 unmap_window(r
->ws
->focus
);
3594 update_iconic(r
->ws
->focus
, 1);
3596 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3597 drain_enter_notify();
3598 r
->ws
->focus
= NULL
;
3599 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3604 get_win_name(Window win
)
3606 unsigned char *prop
= NULL
;
3607 unsigned long nbytes
, nitems
;
3609 /* try _NET_WM_NAME first */
3610 if (get_property(win
, a_netwmname
, 0L, a_utf8_string
, NULL
, &nbytes
,
3613 if (get_property(win
, a_netwmname
, nbytes
, a_utf8_string
,
3614 &nitems
, NULL
, &prop
))
3618 /* fallback to WM_NAME */
3619 if (!get_property(win
, a_wmname
, 0L, a_string
, NULL
, &nbytes
, &prop
))
3622 if (get_property(win
, a_wmname
, nbytes
, a_string
, &nitems
, NULL
, &prop
))
3629 uniconify(struct swm_region
*r
, union arg
*args
)
3633 unsigned char *name
;
3636 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3638 if (r
== NULL
|| r
->ws
== NULL
)
3641 /* make sure we have anything to uniconify */
3642 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3643 if (win
->ws
== NULL
)
3644 continue; /* should never happen */
3645 if (win
->iconic
== 0)
3653 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3655 spawn_select(r
, args
, "search", &searchpid
);
3657 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3660 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3661 if (win
->ws
== NULL
)
3662 continue; /* should never happen */
3663 if (win
->iconic
== 0)
3666 name
= get_win_name(win
->id
);
3669 fprintf(lfile
, "%s.%lu\n", name
, win
->id
);
3677 name_workspace(struct swm_region
*r
, union arg
*args
)
3681 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3687 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3689 spawn_select(r
, args
, "name_workspace", &searchpid
);
3691 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3694 fprintf(lfile
, "%s", "");
3699 search_workspace(struct swm_region
*r
, union arg
*args
)
3702 struct workspace
*ws
;
3705 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3711 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3713 spawn_select(r
, args
, "search", &searchpid
);
3715 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3718 for (i
= 0; i
< workspace_limit
; i
++) {
3722 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3723 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3730 search_win_cleanup(void)
3732 struct search_window
*sw
= NULL
;
3734 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3735 XDestroyWindow(display
, sw
->indicator
);
3736 XFreeGC(display
, sw
->gc
);
3737 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3743 search_win(struct swm_region
*r
, union arg
*args
)
3745 struct ws_win
*win
= NULL
;
3746 struct search_window
*sw
= NULL
;
3753 XRectangle ibox
, lbox
;
3755 DNPRINTF(SWM_D_MISC
, "search_win\n");
3758 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3760 spawn_select(r
, args
, "search", &searchpid
);
3762 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3765 TAILQ_INIT(&search_wl
);
3768 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3769 if (win
->iconic
== 1)
3772 sw
= calloc(1, sizeof(struct search_window
));
3774 warn("search_win: calloc");
3776 search_win_cleanup();
3782 snprintf(s
, sizeof s
, "%d", i
);
3785 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
3787 w
= XCreateSimpleWindow(display
,
3788 win
->id
, 0, 0,lbox
.width
+ 4,
3789 bar_fs_extents
->max_logical_extent
.height
, 1,
3790 r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
,
3791 r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
3794 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3796 sw
->gc
= XCreateGC(display
, w
, 0, &gcv
);
3797 XMapRaised(display
, w
);
3798 XSetForeground(display
, sw
->gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
3800 DRAWSTRING(display
, w
, bar_fs
, sw
->gc
, 2,
3801 (bar_fs_extents
->max_logical_extent
.height
-
3802 lbox
.height
) / 2 - lbox
.y
, s
, len
);
3804 fprintf(lfile
, "%d\n", i
);
3812 search_resp_uniconify(char *resp
, unsigned long len
)
3814 unsigned char *name
;
3818 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3820 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3821 if (win
->iconic
== 0)
3823 name
= get_win_name(win
->id
);
3826 if (asprintf(&s
, "%s.%lu", name
, win
->id
) == -1) {
3831 if (strncmp(s
, resp
, len
) == 0) {
3832 /* XXX this should be a callback to generalize */
3833 update_iconic(win
, 0);
3842 search_resp_name_workspace(char *resp
, unsigned long len
)
3844 struct workspace
*ws
;
3846 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3848 if (search_r
->ws
== NULL
)
3853 free(search_r
->ws
->name
);
3854 search_r
->ws
->name
= NULL
;
3858 ws
->name
= strdup(resp
);
3859 if (ws
->name
== NULL
) {
3860 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
3861 "strdup: %s", strerror(errno
));
3868 search_resp_search_workspace(char *resp
, unsigned long len
)
3875 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
3879 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
3886 ws_idx
= strtonum(q
, 1, workspace_limit
, &errstr
);
3888 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
3895 switchws(search_r
, &a
);
3899 search_resp_search_window(char *resp
, unsigned long len
)
3904 struct search_window
*sw
;
3906 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
3910 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
3915 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
3917 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
3924 TAILQ_FOREACH(sw
, &search_wl
, entry
)
3925 if (idx
== sw
->idx
) {
3931 #define MAX_RESP_LEN 1024
3934 search_do_resp(void)
3940 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
3945 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
3946 warn("search: calloc");
3950 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
3952 warn("search: read error");
3955 resp
[rbytes
] = '\0';
3958 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3959 * newline, so work around that by sanitizing the resp now.
3961 resp
[strcspn(resp
, "\n")] = '\0';
3964 switch (search_resp_action
) {
3965 case SWM_SEARCH_UNICONIFY
:
3966 search_resp_uniconify(resp
, len
);
3968 case SWM_SEARCH_NAME_WORKSPACE
:
3969 search_resp_name_workspace(resp
, len
);
3971 case SWM_SEARCH_SEARCH_WORKSPACE
:
3972 search_resp_search_workspace(resp
, len
);
3974 case SWM_SEARCH_SEARCH_WINDOW
:
3975 search_resp_search_window(resp
, len
);
3980 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
3981 search_win_cleanup();
3983 search_resp_action
= SWM_SEARCH_NONE
;
3984 close(select_resp_pipe
[0]);
3989 wkill(struct swm_region
*r
, union arg
*args
)
3991 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
3993 if (r
->ws
->focus
== NULL
)
3996 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
3997 XKillClient(display
, r
->ws
->focus
->id
);
3999 if (r
->ws
->focus
->can_delete
)
4000 client_msg(r
->ws
->focus
, adelete
);
4005 floating_toggle_win(struct ws_win
*win
)
4007 struct swm_region
*r
;
4017 /* reject floating toggles in max stack mode */
4018 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4021 if (win
->floating
) {
4022 if (!win
->floatmaxed
) {
4023 /* retain position for refloat */
4024 store_float_geom(win
, r
);
4028 if (win
->g_floatvalid
) {
4029 /* refloat at last floating relative position */
4030 X(win
) = win
->g_float
.x
+ X(r
);
4031 Y(win
) = win
->g_float
.y
+ Y(r
);
4032 WIDTH(win
) = win
->g_float
.w
;
4033 HEIGHT(win
) = win
->g_float
.h
;
4038 ewmh_update_actions(win
);
4044 floating_toggle(struct swm_region
*r
, union arg
*args
)
4046 struct ws_win
*win
= r
->ws
->focus
;
4052 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4053 _NET_WM_STATE_TOGGLE
);
4056 if (focus_mode
== SWM_FOCUS_DEFAULT
)
4057 drain_enter_notify();
4059 if (win
== win
->ws
->focus
) {
4060 a
.id
= SWM_ARG_ID_FOCUSCUR
;
4061 focus(win
->ws
->r
, &a
);
4066 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4068 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4070 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4072 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4075 if (X(win
) + BORDER(win
) < X(r
)) {
4077 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4079 X(win
) = X(r
) - BORDER(win
);
4082 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4084 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4086 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4089 if (Y(win
) + BORDER(win
) < Y(r
)) {
4091 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4093 Y(win
) = Y(r
) - BORDER(win
);
4099 if (HEIGHT(win
) < 1)
4105 update_window(struct ws_win
*win
)
4110 bzero(&wc
, sizeof wc
);
4111 mask
= CWBorderWidth
| CWWidth
| CWHeight
| CWX
| CWY
;
4113 wc
.border_width
= BORDER(win
);
4116 wc
.width
= WIDTH(win
);
4117 wc
.height
= HEIGHT(win
);
4119 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%lx, (x,y) w x h: "
4120 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
.x
, wc
.y
, wc
.width
,
4121 wc
.height
, YESNO(win
->bordered
));
4123 XConfigureWindow(display
, win
->id
, mask
, &wc
);
4126 #define SWM_RESIZE_STEPS (50)
4129 resize(struct ws_win
*win
, union arg
*args
)
4133 struct swm_region
*r
= NULL
;
4134 int resize_step
= 0;
4138 struct swm_geometry g
;
4139 int top
= 0, left
= 0;
4142 unsigned int shape
; /* cursor style */
4148 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4151 DNPRINTF(SWM_D_MOUSE
, "resize: window: 0x%lx, floating: %s, "
4152 "transient: 0x%lx\n", win
->id
, YESNO(win
->floating
),
4155 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4158 /* reject resizes in max mode for floaters (transient ok) */
4159 if (win
->floatmaxed
)
4163 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4169 case SWM_ARG_ID_WIDTHSHRINK
:
4170 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4173 case SWM_ARG_ID_WIDTHGROW
:
4174 WIDTH(win
) += SWM_RESIZE_STEPS
;
4177 case SWM_ARG_ID_HEIGHTSHRINK
:
4178 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4181 case SWM_ARG_ID_HEIGHTGROW
:
4182 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4189 constrain_window(win
, r
, 1);
4191 store_float_geom(win
,r
);
4195 if (focus_mode
== SWM_FOCUS_DEFAULT
)
4196 drain_enter_notify();
4198 /* get cursor offset from window root */
4199 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4204 if (wx
< WIDTH(win
) / 2)
4207 if (wy
< HEIGHT(win
) / 2)
4210 if (args
->id
== SWM_ARG_ID_CENTER
)
4213 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
4215 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
4217 cursor
= XCreateFontCursor(display
, shape
);
4219 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4220 GrabModeAsync
, None
, cursor
, CurrentTime
) != GrabSuccess
) {
4221 XFreeCursor(display
, cursor
);
4226 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4227 SubstructureRedirectMask
, &ev
);
4229 case ConfigureRequest
:
4232 handler
[ev
.type
](&ev
);
4235 /* cursor offset/delta from start of the operation */
4236 dx
= ev
.xmotion
.x_root
- x
;
4237 dy
= ev
.xmotion
.y_root
- y
;
4243 if (args
->id
== SWM_ARG_ID_CENTER
) {
4244 if (g
.h
/ 2 + dy
< 1)
4248 HEIGHT(win
) = g
.h
+ 2 * dy
;
4256 HEIGHT(win
) = g
.h
+ dy
;
4263 if (args
->id
== SWM_ARG_ID_CENTER
) {
4264 if (g
.w
/ 2 + dx
< 1)
4268 WIDTH(win
) = g
.w
+ 2 * dx
;
4276 WIDTH(win
) = g
.w
+ dx
;
4279 constrain_window(win
, r
, 1);
4281 /* not free, don't sync more than 120 times / second */
4282 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4283 time
= ev
.xmotion
.time
;
4284 XSync(display
, False
);
4289 } while (ev
.type
!= ButtonRelease
);
4291 XSync(display
, False
);
4294 store_float_geom(win
,r
);
4296 XUngrabPointer(display
, CurrentTime
);
4297 XFreeCursor(display
, cursor
);
4300 drain_enter_notify();
4304 resize_step(struct swm_region
*r
, union arg
*args
)
4306 struct ws_win
*win
= NULL
;
4308 if (r
&& r
->ws
&& r
->ws
->focus
)
4316 #define SWM_MOVE_STEPS (50)
4319 move(struct ws_win
*win
, union arg
*args
)
4324 struct swm_region
*r
= NULL
;
4334 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4337 DNPRINTF(SWM_D_MOUSE
, "move: window: 0x%lx, floating: %s, transient: "
4338 "0x%lx\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4340 /* in max_stack mode should only move transients */
4341 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4345 if (win
->floating
== 0 && !win
->transient
) {
4346 store_float_geom(win
, r
);
4347 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4350 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4357 case SWM_ARG_ID_MOVELEFT
:
4358 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4361 case SWM_ARG_ID_MOVERIGHT
:
4362 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4365 case SWM_ARG_ID_MOVEUP
:
4366 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4369 case SWM_ARG_ID_MOVEDOWN
:
4370 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4377 constrain_window(win
, r
, 0);
4379 store_float_geom(win
, r
);
4383 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4384 GrabModeAsync
, None
, XCreateFontCursor(display
, XC_fleur
),
4385 CurrentTime
) != GrabSuccess
)
4388 /* get cursor offset from window root */
4389 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4393 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4394 SubstructureRedirectMask
, &ev
);
4396 case ConfigureRequest
:
4399 handler
[ev
.type
](&ev
);
4402 X(win
) = ev
.xmotion
.x_root
- wx
- border_width
;
4403 Y(win
) = ev
.xmotion
.y_root
- wy
- border_width
;
4405 constrain_window(win
, r
, 0);
4407 /* not free, don't sync more than 120 times / second */
4408 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4409 time
= ev
.xmotion
.time
;
4410 XSync(display
, False
);
4415 } while (ev
.type
!= ButtonRelease
);
4417 XSync(display
, False
);
4420 store_float_geom(win
, r
);
4421 XUngrabPointer(display
, CurrentTime
);
4424 drain_enter_notify();
4428 move_step(struct swm_region
*r
, union arg
*args
)
4430 struct ws_win
*win
= NULL
;
4432 if (r
&& r
->ws
&& r
->ws
->focus
)
4437 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4444 /* user/key callable function IDs */
4533 kf_search_workspace
,
4535 kf_dumpwins
, /* MUST BE LAST */
4539 /* key definitions */
4541 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4546 char name
[SWM_FUNCNAME_LEN
];
4547 void (*func
)(struct swm_region
*r
, union arg
*);
4549 } keyfuncs
[kf_invalid
+ 1] = {
4550 /* name function argument */
4551 { "cycle_layout", cycle_layout
, {0} },
4552 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4553 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4554 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4555 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4556 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4557 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4558 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4559 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4560 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4561 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4562 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4563 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4564 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4565 { "quit", quit
, {0} },
4566 { "restart", restart
, {0} },
4567 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4568 { "ws_1", switchws
, {.id
= 0} },
4569 { "ws_2", switchws
, {.id
= 1} },
4570 { "ws_3", switchws
, {.id
= 2} },
4571 { "ws_4", switchws
, {.id
= 3} },
4572 { "ws_5", switchws
, {.id
= 4} },
4573 { "ws_6", switchws
, {.id
= 5} },
4574 { "ws_7", switchws
, {.id
= 6} },
4575 { "ws_8", switchws
, {.id
= 7} },
4576 { "ws_9", switchws
, {.id
= 8} },
4577 { "ws_10", switchws
, {.id
= 9} },
4578 { "ws_11", switchws
, {.id
= 10} },
4579 { "ws_12", switchws
, {.id
= 11} },
4580 { "ws_13", switchws
, {.id
= 12} },
4581 { "ws_14", switchws
, {.id
= 13} },
4582 { "ws_15", switchws
, {.id
= 14} },
4583 { "ws_16", switchws
, {.id
= 15} },
4584 { "ws_17", switchws
, {.id
= 16} },
4585 { "ws_18", switchws
, {.id
= 17} },
4586 { "ws_19", switchws
, {.id
= 18} },
4587 { "ws_20", switchws
, {.id
= 19} },
4588 { "ws_21", switchws
, {.id
= 20} },
4589 { "ws_22", switchws
, {.id
= 21} },
4590 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4591 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4592 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4593 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4594 { "ws_prior", priorws
, {0} },
4595 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4596 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4597 { "mvws_1", send_to_ws
, {.id
= 0} },
4598 { "mvws_2", send_to_ws
, {.id
= 1} },
4599 { "mvws_3", send_to_ws
, {.id
= 2} },
4600 { "mvws_4", send_to_ws
, {.id
= 3} },
4601 { "mvws_5", send_to_ws
, {.id
= 4} },
4602 { "mvws_6", send_to_ws
, {.id
= 5} },
4603 { "mvws_7", send_to_ws
, {.id
= 6} },
4604 { "mvws_8", send_to_ws
, {.id
= 7} },
4605 { "mvws_9", send_to_ws
, {.id
= 8} },
4606 { "mvws_10", send_to_ws
, {.id
= 9} },
4607 { "mvws_11", send_to_ws
, {.id
= 10} },
4608 { "mvws_12", send_to_ws
, {.id
= 11} },
4609 { "mvws_13", send_to_ws
, {.id
= 12} },
4610 { "mvws_14", send_to_ws
, {.id
= 13} },
4611 { "mvws_15", send_to_ws
, {.id
= 14} },
4612 { "mvws_16", send_to_ws
, {.id
= 15} },
4613 { "mvws_17", send_to_ws
, {.id
= 16} },
4614 { "mvws_18", send_to_ws
, {.id
= 17} },
4615 { "mvws_19", send_to_ws
, {.id
= 18} },
4616 { "mvws_20", send_to_ws
, {.id
= 19} },
4617 { "mvws_21", send_to_ws
, {.id
= 20} },
4618 { "mvws_22", send_to_ws
, {.id
= 21} },
4619 { "bar_toggle", bar_toggle
, {0} },
4620 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4621 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4622 { "float_toggle", floating_toggle
,{0} },
4623 { "version", version
, {0} },
4624 { "spawn_custom", dummykeyfunc
, {0} },
4625 { "iconify", iconify
, {0} },
4626 { "uniconify", uniconify
, {0} },
4627 { "raise_toggle", raise_toggle
, {0} },
4628 { "button2", pressbutton
, {2} },
4629 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4630 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4631 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4632 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4633 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4634 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4635 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4636 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4637 { "name_workspace", name_workspace
, {0} },
4638 { "search_workspace", search_workspace
, {0} },
4639 { "search_win", search_win
, {0} },
4640 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4641 { "invalid key func", NULL
, {0} },
4644 RB_ENTRY(key
) entry
;
4647 enum keyfuncid funcid
;
4650 RB_HEAD(key_tree
, key
);
4653 key_cmp(struct key
*kp1
, struct key
*kp2
)
4655 if (kp1
->keysym
< kp2
->keysym
)
4657 if (kp1
->keysym
> kp2
->keysym
)
4660 if (kp1
->mod
< kp2
->mod
)
4662 if (kp1
->mod
> kp2
->mod
)
4668 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
4669 struct key_tree keys
;
4672 enum { client_click
, root_click
};
4674 unsigned int action
;
4676 unsigned int button
;
4677 void (*func
)(struct ws_win
*, union arg
*);
4680 /* action key mouse button func args */
4681 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4682 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4683 { client_click
, MODKEY
, Button1
, move
, {0} },
4687 update_modkey(unsigned int mod
)
4693 RB_FOREACH(kp
, key_tree
, &keys
)
4694 if (kp
->mod
& ShiftMask
)
4695 kp
->mod
= mod
| ShiftMask
;
4699 for (i
= 0; i
< LENGTH(buttons
); i
++)
4700 if (buttons
[i
].mask
& ShiftMask
)
4701 buttons
[i
].mask
= mod
| ShiftMask
;
4703 buttons
[i
].mask
= mod
;
4708 TAILQ_ENTRY(spawn_prog
) entry
;
4713 TAILQ_HEAD(spawn_list
, spawn_prog
);
4714 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4717 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4720 struct spawn_prog
*prog
= NULL
;
4722 char *ap
, **real_args
;
4724 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4727 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4728 if (!strcasecmp(spawn_name
, prog
->name
))
4732 warnx("spawn_custom: program %s not found", spawn_name
);
4736 /* make room for expanded args */
4737 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4738 err(1, "spawn_custom: calloc real_args");
4740 /* expand spawn_args into real_args */
4741 for (i
= 0; i
< prog
->argc
; i
++) {
4743 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4744 if (!strcasecmp(ap
, "$bar_border")) {
4746 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4748 err(1, "spawn_custom border color");
4749 } else if (!strcasecmp(ap
, "$bar_color")) {
4751 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4753 err(1, "spawn_custom bar color");
4754 } else if (!strcasecmp(ap
, "$bar_font")) {
4755 if ((real_args
[i
] = strdup(bar_fonts
))
4757 err(1, "spawn_custom bar fonts");
4758 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4760 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4762 err(1, "spawn_custom color font");
4763 } else if (!strcasecmp(ap
, "$color_focus")) {
4765 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4767 err(1, "spawn_custom color focus");
4768 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4770 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4772 err(1, "spawn_custom color unfocus");
4774 /* no match --> copy as is */
4775 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4776 err(1, "spawn_custom strdup(ap)");
4778 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4783 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4784 for (i
= 0; i
< prog
->argc
; i
++)
4785 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4786 DNPRINTF(SWM_D_SPAWN
, "\n");
4788 *ret_args
= real_args
;
4789 return (prog
->argc
);
4793 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4799 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4803 spawn(r
->ws
->idx
, &a
, 1);
4805 for (i
= 0; i
< spawn_argc
; i
++)
4811 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4817 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4821 if (pipe(select_list_pipe
) == -1)
4822 err(1, "pipe error");
4823 if (pipe(select_resp_pipe
) == -1)
4824 err(1, "pipe error");
4826 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4827 err(1, "could not disable SIGPIPE");
4828 switch (*pid
= fork()) {
4830 err(1, "cannot fork");
4833 if (dup2(select_list_pipe
[0], 0) == -1)
4835 if (dup2(select_resp_pipe
[1], 1) == -1)
4837 close(select_list_pipe
[1]);
4838 close(select_resp_pipe
[0]);
4839 spawn(r
->ws
->idx
, &a
, 0);
4841 default: /* parent */
4842 close(select_list_pipe
[0]);
4843 close(select_resp_pipe
[1]);
4847 for (i
= 0; i
< spawn_argc
; i
++)
4853 spawn_insert(char *name
, char *args
)
4855 char *arg
, *cp
, *ptr
;
4856 struct spawn_prog
*sp
;
4858 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
4860 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
4861 err(1, "spawn_insert: malloc");
4862 if ((sp
->name
= strdup(name
)) == NULL
)
4863 err(1, "spawn_insert: strdup");
4865 /* convert the arguments to an argument list */
4866 if ((ptr
= cp
= strdup(args
)) == NULL
)
4867 err(1, "spawn_insert: strdup");
4868 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
4869 /* empty field; skip it */
4874 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
4875 sizeof *sp
->argv
)) == NULL
)
4876 err(1, "spawn_insert: realloc");
4877 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
4878 err(1, "spawn_insert: strdup");
4882 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
4883 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
4887 spawn_remove(struct spawn_prog
*sp
)
4891 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
4893 TAILQ_REMOVE(&spawns
, sp
, entry
);
4894 for (i
= 0; i
< sp
->argc
; i
++)
4900 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
4904 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
4906 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
4909 spawn_insert(name
, args
);
4911 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
4915 setspawn(char *name
, char *args
)
4917 struct spawn_prog
*sp
;
4919 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
4924 TAILQ_FOREACH(sp
, &spawns
, entry
) {
4925 if (!strcmp(sp
->name
, name
)) {
4929 spawn_replace(sp
, name
, args
);
4930 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
4934 if (*args
== '\0') {
4935 warnx("error: setspawn: cannot find program: %s", name
);
4939 spawn_insert(name
, args
);
4940 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
4944 setconfspawn(char *selector
, char *value
, int flags
)
4946 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
4948 setspawn(selector
, value
);
4950 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
4957 setconfspawn("term", "xterm", 0);
4958 setconfspawn("spawn_term", "xterm", 0);
4959 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4960 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4961 setconfspawn("lock", "xlock", 0);
4962 setconfspawn("initscr", "initscreen.sh", 0);
4963 setconfspawn("menu", "dmenu_run"
4966 " -nf $bar_font_color"
4968 " -sf $bar_color", 0);
4969 setconfspawn("search", "dmenu"
4973 " -nf $bar_font_color"
4975 " -sf $bar_color", 0);
4976 setconfspawn("name_workspace", "dmenu"
4980 " -nf $bar_font_color"
4982 " -sf $bar_color", 0);
4986 #define SWM_MODNAME_SIZE 32
4987 #define SWM_KEY_WS "\n+ \t"
4989 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
4993 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
4994 if (mod
== NULL
|| ks
== NULL
) {
4995 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
4998 if (keystr
== NULL
|| strlen(keystr
) == 0) {
4999 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5005 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5006 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5008 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5009 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5011 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5013 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5015 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5017 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5019 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5021 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5022 *mod
|= ControlMask
;
5024 *ks
= XStringToKeysym(name
);
5025 XConvertCase(*ks
, ks
, &uks
);
5026 if (ks
== NoSymbol
) {
5028 "parsekeys: invalid key %s\n",
5034 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5039 strdupsafe(char *str
)
5044 return (strdup(str
));
5048 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5052 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5053 keyfuncs
[kfid
].name
, spawn_name
);
5055 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5056 err(1, "key_insert: malloc");
5061 kp
->spawn_name
= strdupsafe(spawn_name
);
5062 RB_INSERT(key_tree
, &keys
, kp
);
5064 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5068 key_lookup(unsigned int mod
, KeySym ks
)
5075 return (RB_FIND(key_tree
, &keys
, &kp
));
5079 key_remove(struct key
*kp
)
5081 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5083 RB_REMOVE(key_tree
, &keys
, kp
);
5084 free(kp
->spawn_name
);
5087 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5091 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5094 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5098 key_insert(mod
, ks
, kfid
, spawn_name
);
5100 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5104 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5109 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5110 keyfuncs
[kfid
].name
, spawn_name
);
5112 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5113 if (kfid
== kf_invalid
)
5116 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5117 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5120 if (kfid
== kf_invalid
) {
5121 warnx("error: setkeybinding: cannot find mod/key combination");
5122 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5126 key_insert(mod
, ks
, kfid
, spawn_name
);
5127 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5131 setconfbinding(char *selector
, char *value
, int flags
)
5133 enum keyfuncid kfid
;
5136 struct spawn_prog
*sp
;
5137 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5138 if (selector
== NULL
) {
5139 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5140 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5142 setkeybinding(mod
, ks
, kfid
, NULL
);
5147 /* search by key function name */
5148 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
5149 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5150 SWM_FUNCNAME_LEN
) == 0) {
5151 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5153 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5154 setkeybinding(mod
, ks
, kfid
, NULL
);
5160 /* search by custom spawn name */
5161 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5162 if (strcasecmp(selector
, sp
->name
) == 0) {
5163 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5165 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5166 setkeybinding(mod
, ks
, kf_spawn_custom
,
5173 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5180 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
5181 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, kf_flip_layout
, NULL
);
5182 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
5183 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
5184 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
5185 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
5186 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
5187 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
5188 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
5189 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
5190 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
5191 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
5192 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
5193 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
5194 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_custom
,"term");
5195 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
5196 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
5197 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
5198 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
5199 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
5200 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
5201 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
5202 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
5203 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
5204 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
5205 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
5206 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
5207 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
5208 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
5209 setkeybinding(MODKEY
, XK_F1
, kf_ws_11
, NULL
);
5210 setkeybinding(MODKEY
, XK_F2
, kf_ws_12
, NULL
);
5211 setkeybinding(MODKEY
, XK_F3
, kf_ws_13
, NULL
);
5212 setkeybinding(MODKEY
, XK_F4
, kf_ws_14
, NULL
);
5213 setkeybinding(MODKEY
, XK_F5
, kf_ws_15
, NULL
);
5214 setkeybinding(MODKEY
, XK_F6
, kf_ws_16
, NULL
);
5215 setkeybinding(MODKEY
, XK_F7
, kf_ws_17
, NULL
);
5216 setkeybinding(MODKEY
, XK_F8
, kf_ws_18
, NULL
);
5217 setkeybinding(MODKEY
, XK_F9
, kf_ws_19
, NULL
);
5218 setkeybinding(MODKEY
, XK_F10
, kf_ws_20
, NULL
);
5219 setkeybinding(MODKEY
, XK_F11
, kf_ws_21
, NULL
);
5220 setkeybinding(MODKEY
, XK_F12
, kf_ws_22
, NULL
);
5221 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
5222 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
5223 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
5224 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
5225 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
5226 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
5227 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
5228 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
5229 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
5230 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
5231 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
5232 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
5233 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
5234 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
5235 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
5236 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
5237 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
5238 setkeybinding(MODKEY
|ShiftMask
, XK_F1
, kf_mvws_11
, NULL
);
5239 setkeybinding(MODKEY
|ShiftMask
, XK_F2
, kf_mvws_12
, NULL
);
5240 setkeybinding(MODKEY
|ShiftMask
, XK_F3
, kf_mvws_13
, NULL
);
5241 setkeybinding(MODKEY
|ShiftMask
, XK_F4
, kf_mvws_14
, NULL
);
5242 setkeybinding(MODKEY
|ShiftMask
, XK_F5
, kf_mvws_15
, NULL
);
5243 setkeybinding(MODKEY
|ShiftMask
, XK_F6
, kf_mvws_16
, NULL
);
5244 setkeybinding(MODKEY
|ShiftMask
, XK_F7
, kf_mvws_17
, NULL
);
5245 setkeybinding(MODKEY
|ShiftMask
, XK_F8
, kf_mvws_18
, NULL
);
5246 setkeybinding(MODKEY
|ShiftMask
, XK_F9
, kf_mvws_19
, NULL
);
5247 setkeybinding(MODKEY
|ShiftMask
, XK_F10
, kf_mvws_20
, NULL
);
5248 setkeybinding(MODKEY
|ShiftMask
, XK_F11
, kf_mvws_21
, NULL
);
5249 setkeybinding(MODKEY
|ShiftMask
, XK_F12
, kf_mvws_22
, NULL
);
5250 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
5251 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
5252 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
5253 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
5254 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
5255 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
5256 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
5257 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
5258 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
5259 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
5260 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
5261 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
5262 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
5263 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
5264 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
5265 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
5266 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
5267 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
5268 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
5269 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
5270 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
5271 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
5272 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
5273 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
5274 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
5275 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
5277 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
5286 while (RB_EMPTY(&keys
) == 0) {
5287 kp
= RB_ROOT(&keys
);
5293 setkeymapping(char *selector
, char *value
, int flags
)
5295 char keymapping_file
[PATH_MAX
];
5296 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5297 if (value
[0] == '~')
5298 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5299 pwd
->pw_dir
, &value
[1]);
5301 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5303 /* load new key bindings; if it fails, revert to default bindings */
5304 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5308 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5313 updatenumlockmask(void)
5316 XModifierKeymap
*modmap
;
5318 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
5320 modmap
= XGetModifierMapping(display
);
5321 for (i
= 0; i
< 8; i
++)
5322 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
5323 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
5324 == XKeysymToKeycode(display
, XK_Num_Lock
))
5325 numlockmask
= (1 << i
);
5327 XFreeModifiermap(modmap
);
5335 unsigned int modifiers
[] =
5336 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
5339 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5340 updatenumlockmask();
5342 for (k
= 0; k
< ScreenCount(display
); k
++) {
5343 if (TAILQ_EMPTY(&screens
[k
].rl
))
5345 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
5346 RB_FOREACH(kp
, key_tree
, &keys
) {
5347 if ((code
= XKeysymToKeycode(display
, kp
->keysym
)))
5348 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5349 XGrabKey(display
, code
,
5350 kp
->mod
| modifiers
[j
],
5351 screens
[k
].root
, True
,
5352 GrabModeAsync
, GrabModeAsync
);
5358 grabbuttons(struct ws_win
*win
, int focused
)
5361 unsigned int modifiers
[] =
5362 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5364 updatenumlockmask();
5365 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
5367 for (i
= 0; i
< LENGTH(buttons
); i
++)
5368 if (buttons
[i
].action
== client_click
)
5369 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5370 XGrabButton(display
, buttons
[i
].button
,
5371 buttons
[i
].mask
| modifiers
[j
],
5372 win
->id
, False
, BUTTONMASK
,
5373 GrabModeAsync
, GrabModeSync
, None
,
5376 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
5377 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
5380 const char *quirkname
[] = {
5381 "NONE", /* config string for "no value" */
5390 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5391 #define SWM_Q_WS "\n|+ \t"
5393 parsequirks(char *qstr
, unsigned long *quirk
)
5403 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5405 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5406 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5407 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5408 DNPRINTF(SWM_D_QUIRK
,
5409 "parsequirks: %s\n", name
);
5414 *quirk
|= 1 << (i
-1);
5418 if (i
>= LENGTH(quirkname
)) {
5419 DNPRINTF(SWM_D_QUIRK
,
5420 "parsequirks: invalid quirk [%s]\n", name
);
5428 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5432 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5435 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5436 err(1, "quirk_insert: malloc");
5437 if ((qp
->class = strdup(class)) == NULL
)
5438 err(1, "quirk_insert: strdup");
5439 if ((qp
->name
= strdup(name
)) == NULL
)
5440 err(1, "quirk_insert: strdup");
5443 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5445 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5449 quirk_remove(struct quirk
*qp
)
5451 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5452 qp
->name
, qp
->quirk
);
5454 TAILQ_REMOVE(&quirks
, qp
, entry
);
5459 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5463 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5464 unsigned long quirk
)
5466 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5467 qp
->name
, qp
->quirk
);
5470 quirk_insert(class, name
, quirk
);
5472 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5476 setquirk(const char *class, const char *name
, unsigned long quirk
)
5480 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5483 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5484 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5488 quirk_replace(qp
, class, name
, quirk
);
5489 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5494 warnx("error: setquirk: cannot find class/name combination");
5498 quirk_insert(class, name
, quirk
);
5499 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5503 setconfquirk(char *selector
, char *value
, int flags
)
5505 char *cp
, *class, *name
;
5507 unsigned long quirks
;
5508 if (selector
== NULL
)
5510 if ((cp
= strchr(selector
, ':')) == NULL
)
5515 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5516 setquirk(class, name
, quirks
);
5523 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5524 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5525 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5526 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5527 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5528 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5529 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5530 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5531 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5532 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5533 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5534 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5535 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5538 /* conf file stuff */
5539 #define SWM_CONF_FILE "spectrwm.conf"
5540 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5544 SWM_S_BAR_AT_BOTTOM
,
5545 SWM_S_BAR_BORDER_WIDTH
,
5552 SWM_S_CLOCK_ENABLED
,
5555 SWM_S_CYCLE_VISIBLE
,
5557 SWM_S_DISABLE_BORDER
,
5559 SWM_S_FOCUS_CLOSE_WRAP
,
5560 SWM_S_FOCUS_DEFAULT
,
5566 SWM_S_STACK_ENABLED
,
5568 SWM_S_TITLE_CLASS_ENABLED
,
5569 SWM_S_TITLE_NAME_ENABLED
,
5570 SWM_S_URGENT_ENABLED
,
5571 SWM_S_VERBOSE_LAYOUT
,
5572 SWM_S_WINDOW_NAME_ENABLED
,
5573 SWM_S_WORKSPACE_LIMIT
5577 setconfvalue(char *selector
, char *value
, int flags
)
5583 case SWM_S_BAR_ACTION
:
5585 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5586 err(1, "setconfvalue: bar_action");
5588 case SWM_S_BAR_AT_BOTTOM
:
5589 bar_at_bottom
= atoi(value
);
5591 case SWM_S_BAR_BORDER_WIDTH
:
5592 bar_border_width
= atoi(value
);
5593 if (bar_border_width
< 0)
5594 bar_border_width
= 0;
5596 case SWM_S_BAR_DELAY
:
5597 bar_delay
= atoi(value
);
5599 case SWM_S_BAR_ENABLED
:
5600 bar_enabled
= atoi(value
);
5602 case SWM_S_BAR_FONT
:
5604 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5605 err(1, "setconfvalue: asprintf: failed to allocate "
5606 "memory for bar_fonts.");
5610 case SWM_S_BAR_FORMAT
:
5612 if ((bar_format
= strdup(value
)) == NULL
)
5613 err(1, "setconfvalue: bar_format");
5615 case SWM_S_BAR_JUSTIFY
:
5616 if (!strcmp(value
, "left"))
5617 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5618 else if (!strcmp(value
, "center"))
5619 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5620 else if (!strcmp(value
, "right"))
5621 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5623 errx(1, "invalid bar_justify");
5625 case SWM_S_BORDER_WIDTH
:
5626 border_width
= atoi(value
);
5627 if (border_width
< 0)
5630 case SWM_S_CLOCK_ENABLED
:
5631 clock_enabled
= atoi(value
);
5633 case SWM_S_CLOCK_FORMAT
:
5634 #ifndef SWM_DENY_CLOCK_FORMAT
5636 if ((clock_format
= strdup(value
)) == NULL
)
5637 err(1, "setconfvalue: clock_format");
5640 case SWM_S_CYCLE_EMPTY
:
5641 cycle_empty
= atoi(value
);
5643 case SWM_S_CYCLE_VISIBLE
:
5644 cycle_visible
= atoi(value
);
5646 case SWM_S_DIALOG_RATIO
:
5647 dialog_ratio
= atof(value
);
5648 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5651 case SWM_S_DISABLE_BORDER
:
5652 disable_border
= atoi(value
);
5654 case SWM_S_FOCUS_CLOSE
:
5655 if (!strcmp(value
, "first"))
5656 focus_close
= SWM_STACK_BOTTOM
;
5657 else if (!strcmp(value
, "last"))
5658 focus_close
= SWM_STACK_TOP
;
5659 else if (!strcmp(value
, "next"))
5660 focus_close
= SWM_STACK_ABOVE
;
5661 else if (!strcmp(value
, "previous"))
5662 focus_close
= SWM_STACK_BELOW
;
5664 errx(1, "focus_close");
5666 case SWM_S_FOCUS_CLOSE_WRAP
:
5667 focus_close_wrap
= atoi(value
);
5669 case SWM_S_FOCUS_DEFAULT
:
5670 if (!strcmp(value
, "last"))
5671 focus_default
= SWM_STACK_TOP
;
5672 else if (!strcmp(value
, "first"))
5673 focus_default
= SWM_STACK_BOTTOM
;
5675 errx(1, "focus_default");
5677 case SWM_S_FOCUS_MODE
:
5678 if (!strcmp(value
, "default"))
5679 focus_mode
= SWM_FOCUS_DEFAULT
;
5680 else if (!strcmp(value
, "follow_cursor"))
5681 focus_mode
= SWM_FOCUS_FOLLOW
;
5682 else if (!strcmp(value
, "synergy"))
5683 focus_mode
= SWM_FOCUS_SYNERGY
;
5685 errx(1, "focus_mode");
5687 case SWM_S_SPAWN_ORDER
:
5688 if (!strcmp(value
, "first"))
5689 spawn_position
= SWM_STACK_BOTTOM
;
5690 else if (!strcmp(value
, "last"))
5691 spawn_position
= SWM_STACK_TOP
;
5692 else if (!strcmp(value
, "next"))
5693 spawn_position
= SWM_STACK_ABOVE
;
5694 else if (!strcmp(value
, "previous"))
5695 spawn_position
= SWM_STACK_BELOW
;
5697 errx(1, "spawn_position");
5699 case SWM_S_SPAWN_TERM
:
5700 setconfspawn("term", value
, 0);
5701 setconfspawn("spawn_term", value
, 0);
5705 case SWM_S_SS_ENABLED
:
5706 ss_enabled
= atoi(value
);
5708 case SWM_S_STACK_ENABLED
:
5709 stack_enabled
= atoi(value
);
5711 case SWM_S_TERM_WIDTH
:
5712 term_width
= atoi(value
);
5716 case SWM_S_TITLE_CLASS_ENABLED
:
5717 title_class_enabled
= atoi(value
);
5719 case SWM_S_TITLE_NAME_ENABLED
:
5720 title_name_enabled
= atoi(value
);
5722 case SWM_S_URGENT_ENABLED
:
5723 urgent_enabled
= atoi(value
);
5725 case SWM_S_VERBOSE_LAYOUT
:
5726 verbose_layout
= atoi(value
);
5727 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5729 layouts
[i
].l_string
= fancy_stacker
;
5731 layouts
[i
].l_string
= plain_stacker
;
5734 case SWM_S_WINDOW_NAME_ENABLED
:
5735 window_name_enabled
= atoi(value
);
5737 case SWM_S_WORKSPACE_LIMIT
:
5738 workspace_limit
= atoi(value
);
5739 if (workspace_limit
> SWM_WS_MAX
)
5740 workspace_limit
= SWM_WS_MAX
;
5741 else if (workspace_limit
< 1)
5742 workspace_limit
= 1;
5751 setconfmodkey(char *selector
, char *value
, int flags
)
5753 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5754 update_modkey(Mod1Mask
);
5755 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5756 update_modkey(Mod2Mask
);
5757 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5758 update_modkey(Mod3Mask
);
5759 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5760 update_modkey(Mod4Mask
);
5767 setconfcolor(char *selector
, char *value
, int flags
)
5769 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5774 setconfregion(char *selector
, char *value
, int flags
)
5776 custom_region(value
);
5781 setautorun(char *selector
, char *value
, int flags
)
5791 if (getenv("SWM_STARTED"))
5795 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5796 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5798 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5799 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5802 * This is a little intricate
5804 * If the pid already exists we simply reuse it because it means it was
5805 * used before AND not claimed by manage_window. We get away with
5806 * altering it in the parent after INSERT because this can not be a race
5809 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5812 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5814 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5815 err(1, "setautorun: realloc");
5816 a
.argv
[argc
- 1] = ap
;
5819 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5820 err(1, "setautorun: realloc");
5821 a
.argv
[argc
] = NULL
;
5823 if ((pid
= fork()) == 0) {
5824 spawn(ws_id
, &a
, 1);
5833 p
= calloc(1, sizeof *p
);
5836 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
5846 setlayout(char *selector
, char *value
, int flags
)
5848 int ws_id
, i
, x
, mg
, ma
, si
, raise
, f
= 0;
5849 int st
= SWM_V_STACK
;
5851 struct workspace
*ws
;
5853 if (getenv("SWM_STARTED"))
5857 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
5858 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
5859 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5860 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5863 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5864 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
5866 if (!strcasecmp(s
, "vertical"))
5868 else if (!strcasecmp(s
, "vertical_flip")) {
5871 } else if (!strcasecmp(s
, "horizontal"))
5873 else if (!strcasecmp(s
, "horizontal_flip")) {
5876 } else if (!strcasecmp(s
, "fullscreen"))
5879 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5880 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5883 for (i
= 0; i
< ScreenCount(display
); i
++) {
5884 ws
= (struct workspace
*)&screens
[i
].ws
;
5885 ws
[ws_id
].cur_layout
= &layouts
[st
];
5887 ws
[ws_id
].always_raise
= raise
;
5888 if (st
== SWM_MAX_STACK
)
5892 for (x
= 0; x
< abs(mg
); x
++) {
5893 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5894 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
5895 SWM_ARG_ID_MASTERSHRINK
);
5899 for (x
= 0; x
< abs(ma
); x
++) {
5900 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5901 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
5902 SWM_ARG_ID_MASTERDEL
);
5906 for (x
= 0; x
< abs(si
); x
++) {
5907 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5908 si
>= 0 ? SWM_ARG_ID_STACKINC
:
5909 SWM_ARG_ID_STACKDEC
);
5914 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5915 SWM_ARG_ID_FLIPLAYOUT
);
5923 /* config options */
5924 struct config_option
{
5926 int (*func
)(char*, char*, int);
5929 struct config_option configopt
[] = {
5930 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
5931 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
5932 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
5933 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
5934 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
5935 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
5936 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
5937 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
5938 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
5939 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
5940 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
5941 { "keyboard_mapping", setkeymapping
, 0 },
5942 { "bind", setconfbinding
, 0 },
5943 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
5944 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
5945 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
5946 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
5947 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
5948 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
5949 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
5950 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
5951 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
5952 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
5953 { "modkey", setconfmodkey
, 0 },
5954 { "program", setconfspawn
, 0 },
5955 { "quirk", setconfquirk
, 0 },
5956 { "region", setconfregion
, 0 },
5957 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
5958 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
5959 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
5960 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
5961 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
5962 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
5963 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
5964 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
5965 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
5966 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
5967 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
5968 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
5969 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
5970 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
5971 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
5972 { "autorun", setautorun
, 0 },
5973 { "layout", setlayout
, 0 },
5978 conf_load(char *filename
, int keymapping
)
5981 char *line
, *cp
, *optsub
, *optval
;
5982 size_t linelen
, lineno
= 0;
5983 int wordlen
, i
, optind
;
5984 struct config_option
*opt
;
5986 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
5988 if (filename
== NULL
) {
5989 warnx("conf_load: no filename");
5992 if ((config
= fopen(filename
, "r")) == NULL
) {
5993 warn("conf_load: fopen: %s", filename
);
5997 while (!feof(config
)) {
5998 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6001 err(1, "%s", filename
);
6006 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6007 if (cp
[0] == '\0') {
6012 /* get config option */
6013 wordlen
= strcspn(cp
, "=[ \t\n");
6015 warnx("%s: line %zd: no option found",
6020 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6021 opt
= &configopt
[i
];
6022 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6023 strlen(opt
->optname
) == wordlen
) {
6029 warnx("%s: line %zd: unknown option %.*s",
6030 filename
, lineno
, wordlen
, cp
);
6033 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
6034 warnx("%s: line %zd: invalid option %.*s",
6035 filename
, lineno
, wordlen
, cp
);
6039 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6040 /* get [selector] if any */
6044 wordlen
= strcspn(cp
, "]");
6047 warnx("%s: line %zd: syntax error",
6052 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6054 warnx("%s: line %zd: unable to allocate"
6055 "memory for selector", filename
,
6061 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6063 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6065 optval
= strdup(cp
);
6066 /* call function to deal with it all */
6067 if (configopt
[optind
].func(optsub
, optval
,
6068 configopt
[optind
].funcflags
) != 0)
6069 errx(1, "%s: line %zd: invalid data for %s",
6070 filename
, lineno
, configopt
[optind
].optname
);
6077 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6084 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6090 set_child_transient(struct ws_win
*win
, Window
*trans
)
6092 struct ws_win
*parent
, *w
;
6093 XWMHints
*wmh
= NULL
;
6094 struct swm_region
*r
;
6095 struct workspace
*ws
;
6097 parent
= find_window(win
->transient
);
6099 parent
->child_trans
= win
;
6101 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6102 " for 0x%lx trans 0x%lx\n", win
->id
, win
->transient
);
6104 if (win
->hints
== NULL
) {
6105 warnx("no hints for 0x%lx", win
->id
);
6109 r
= root_to_region(win
->wa
.root
);
6111 /* parent doen't exist in our window list */
6112 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6116 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
6117 warnx("can't get hints for 0x%lx", w
->id
);
6121 if (win
->hints
->window_group
!= wmh
->window_group
)
6124 w
->child_trans
= win
;
6125 win
->transient
= w
->id
;
6127 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
6128 "transient to 0x%lx\n", win
->transient
);
6138 window_get_pid(Window win
)
6140 Atom actual_type_return
;
6141 int actual_format_return
= 0;
6142 unsigned long nitems_return
= 0;
6143 unsigned long bytes_after_return
= 0;
6147 unsigned char *prop
= NULL
;
6149 if (XGetWindowProperty(display
, win
,
6150 XInternAtom(display
, "_NET_WM_PID", False
), 0, 1, False
,
6151 XA_CARDINAL
, &actual_type_return
, &actual_format_return
,
6152 &nitems_return
, &bytes_after_return
,
6153 (unsigned char**)(void*)&pid
) != Success
)
6155 if (actual_type_return
!= XA_CARDINAL
)
6166 if (XGetWindowProperty(display
, win
,
6167 XInternAtom(display
, "_SWM_PID", False
), 0, SWM_PROPLEN
, False
,
6168 XA_STRING
, &actual_type_return
, &actual_format_return
,
6169 &nitems_return
, &bytes_after_return
, &prop
) != Success
)
6171 if (actual_type_return
!= XA_STRING
)
6176 ret
= strtonum((const char *)prop
, 0, UINT_MAX
, &errstr
);
6177 /* ignore error because strtonum returns 0 anyway */
6184 manage_window(Window id
)
6187 struct workspace
*ws
;
6188 struct ws_win
*win
, *ww
;
6189 int format
, i
, ws_idx
, n
, border_me
= 0;
6190 unsigned long nitems
, bytes
;
6191 Atom ws_idx_atom
= 0, type
;
6192 Atom
*prot
= NULL
, *pp
;
6193 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
6194 struct swm_region
*r
;
6199 if ((win
= find_window(id
)) != NULL
)
6200 return (win
); /* already being managed */
6202 /* see if we are on the unmanaged list */
6203 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6204 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
6205 "window: 0x%lx\n", win
->id
);
6206 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6208 set_child_transient(win
, &trans
);
6210 if (trans
&& (ww
= find_window(trans
)))
6211 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
6212 else if ((ww
= win
->ws
->focus
) &&
6213 spawn_position
== SWM_STACK_ABOVE
)
6214 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
,
6216 else if (ww
&& spawn_position
== SWM_STACK_BELOW
)
6217 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6218 else switch (spawn_position
) {
6221 case SWM_STACK_ABOVE
:
6222 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6224 case SWM_STACK_BOTTOM
:
6225 case SWM_STACK_BELOW
:
6226 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6229 ewmh_update_actions(win
);
6233 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6234 err(1, "manage_window: calloc: failed to allocate memory for "
6240 /* see if we need to override the workspace */
6241 p
= find_pid(window_get_pid(id
));
6243 /* Get all the window data in one shot */
6244 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
6246 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
6247 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
6249 XGetWindowAttributes(display
, id
, &win
->wa
);
6250 XGetWMNormalHints(display
, id
, &win
->sh
, &win
->sh_mask
);
6251 win
->hints
= XGetWMHints(display
, id
);
6252 XGetTransientForHint(display
, id
, &trans
);
6254 win
->transient
= trans
;
6255 set_child_transient(win
, &trans
);
6256 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, "
6257 "transient: 0x%lx\n", win
->id
, win
->transient
);
6260 /* get supported protocols */
6261 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
6262 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
6263 if (*pp
== takefocus
)
6264 win
->take_focus
= 1;
6266 win
->can_delete
= 1;
6272 win
->iconic
= get_iconic(win
);
6275 * Figure out where to put the window. If it was previously assigned to
6276 * a workspace (either by spawn() or manually moving), and isn't
6277 * transient, * put it in the same workspace
6279 r
= root_to_region(win
->wa
.root
);
6281 ws
= &r
->s
->ws
[p
->ws
];
6282 TAILQ_REMOVE(&pidlist
, p
, entry
);
6285 } else if (prop
&& win
->transient
== 0) {
6286 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
6287 ws_idx
= strtonum((const char *)prop
, 0, workspace_limit
- 1,
6290 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
6293 ws
= &r
->s
->ws
[ws_idx
];
6296 /* this should launch transients in the same ws as parent */
6298 if ((ww
= find_window(trans
)) != NULL
)
6304 warnx("manage_window: fix this "
6310 /* set up the window layout */
6313 win
->s
= r
->s
; /* this never changes */
6314 if (trans
&& (ww
= find_window(trans
)))
6315 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
6316 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
6317 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
6319 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
6320 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6321 else switch (spawn_position
) {
6324 case SWM_STACK_ABOVE
:
6325 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6327 case SWM_STACK_BOTTOM
:
6328 case SWM_STACK_BELOW
:
6329 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6332 /* ignore window border if there is one. */
6333 WIDTH(win
) = win
->wa
.width
;
6334 HEIGHT(win
) = win
->wa
.height
;
6335 X(win
) = win
->wa
.x
+ win
->wa
.border_width
;
6336 Y(win
) = win
->wa
.y
+ win
->wa
.border_width
;
6338 win
->g_floatvalid
= 0;
6339 win
->floatmaxed
= 0;
6340 win
->ewmh_flags
= 0;
6342 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, (x,y) w x h: "
6343 "(%d,%d) %d x %d, ws: %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
6344 HEIGHT(win
), ws
->idx
);
6346 constrain_window(win
, r
, 0);
6348 /* Set window properties so we can remember this after reincarnation */
6349 if (ws_idx_atom
&& prop
== NULL
&&
6350 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
6352 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6354 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
6355 PropModeReplace
, ws_idx_str
, strlen((char *)ws_idx_str
));
6360 ewmh_autoquirk(win
);
6362 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
6363 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6364 win
->ch
.res_class
, win
->ch
.res_name
);
6366 /* java is retarded so treat it special */
6367 if (strstr(win
->ch
.res_name
, "sun-awt")) {
6372 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6373 if (!strcmp(win
->ch
.res_class
, qp
->class) &&
6374 !strcmp(win
->ch
.res_name
, qp
->name
)) {
6375 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
6376 "class: %s, name: %s\n", win
->ch
.res_class
,
6378 if (qp
->quirk
& SWM_Q_FLOAT
) {
6382 win
->quirks
= qp
->quirk
;
6387 /* alter window position if quirky */
6388 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6389 win
->manual
= 1; /* don't center the quirky windows */
6390 if (bar_enabled
&& Y(win
) < bar_height
)
6391 Y(win
) = bar_height
;
6392 if (WIDTH(win
) + X(win
) > WIDTH(r
))
6393 X(win
) = WIDTH(r
) - WIDTH(win
) - 2;
6397 /* Reset font sizes (the bruteforce way; no default keybinding). */
6398 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6399 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6400 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
6401 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6402 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
6405 ewmh_get_win_state(win
);
6406 ewmh_update_actions(win
);
6407 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
6412 X(win
) -= border_width
;
6413 Y(win
) -= border_width
;
6417 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
6418 PropertyChangeMask
| StructureNotifyMask
);
6420 /* floaters need to be mapped if they are in the current workspace */
6421 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
6422 XMapRaised(display
, win
->id
);
6428 free_window(struct ws_win
*win
)
6430 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%lx\n", win
->id
);
6435 /* needed for restart wm */
6436 set_win_state(win
, WithdrawnState
);
6438 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6440 if (win
->ch
.res_class
)
6441 XFree(win
->ch
.res_class
);
6442 if (win
->ch
.res_name
)
6443 XFree(win
->ch
.res_name
);
6448 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6454 unmanage_window(struct ws_win
*win
)
6456 struct ws_win
*parent
;
6461 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%lx\n", win
->id
);
6463 if (win
->transient
) {
6464 parent
= find_window(win
->transient
);
6466 parent
->child_trans
= NULL
;
6469 /* focus on root just in case */
6470 XSetInputFocus(display
, PointerRoot
, PointerRoot
, CurrentTime
);
6479 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
6480 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6484 focus_magic(struct ws_win
*win
)
6486 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%lx\n", WINID(win
));
6489 /* if there are no windows clear the status-bar */
6494 if (win
->child_trans
) {
6495 /* win = parent & has a transient so focus on that */
6497 focus_win(win
->child_trans
);
6498 if (win
->child_trans
->take_focus
)
6499 client_msg(win
, takefocus
);
6501 /* make sure transient hasn't disappeared */
6502 if (validate_win(win
->child_trans
) == 0) {
6503 focus_win(win
->child_trans
);
6504 if (win
->child_trans
->take_focus
)
6505 client_msg(win
->child_trans
, takefocus
);
6507 win
->child_trans
= NULL
;
6509 if (win
->take_focus
)
6510 client_msg(win
, takefocus
);
6516 if (win
->take_focus
)
6517 client_msg(win
, takefocus
);
6524 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%lx\n", e
->xexpose
.window
);
6531 XKeyEvent
*ev
= &e
->xkey
;
6533 struct swm_region
*r
;
6535 keysym
= XkbKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0, 0);
6536 if ((kp
= key_lookup(CLEANMASK(ev
->state
), keysym
)) == NULL
)
6538 if (keyfuncs
[kp
->funcid
].func
== NULL
)
6541 r
= root_to_region(ev
->root
);
6542 if (kp
->funcid
== kf_spawn_custom
)
6543 spawn_custom(r
, &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
6545 keyfuncs
[kp
->funcid
].func(r
, &(keyfuncs
[kp
->funcid
].args
));
6549 buttonpress(XEvent
*e
)
6553 XButtonPressedEvent
*ev
= &e
->xbutton
;
6555 if ((win
= find_window(ev
->window
)) == NULL
)
6559 action
= client_click
;
6561 for (i
= 0; i
< LENGTH(buttons
); i
++)
6562 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6563 buttons
[i
].button
== ev
->button
&&
6564 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
6565 buttons
[i
].func(win
, &buttons
[i
].args
);
6569 configurerequest(XEvent
*e
)
6571 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
6576 if ((win
= find_window(ev
->window
)) == NULL
)
6577 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6581 bzero(&wc
, sizeof wc
);
6584 wc
.width
= ev
->width
;
6585 wc
.height
= ev
->height
;
6586 wc
.border_width
= ev
->border_width
;
6587 wc
.sibling
= ev
->above
;
6588 wc
.stack_mode
= ev
->detail
;
6590 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: 0x%lx, "
6591 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", ev
->window
,
6592 YESNO(new), wc
.x
, wc
.y
, wc
.width
, wc
.height
);
6594 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
6595 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
6596 !(win
->sh_mask
& EWMH_F_FULLSCREEN
)) {
6597 win
->g_float
.x
= ev
->x
- X(win
->ws
->r
);
6598 win
->g_float
.y
= ev
->y
- Y(win
->ws
->r
);
6599 win
->g_float
.w
= ev
->width
;
6600 win
->g_float
.h
= ev
->height
;
6601 win
->g_floatvalid
= 1;
6603 if (win
->floating
) {
6604 win
->g
= win
->g_float
;
6605 win
->g
.x
+= X(win
->ws
->r
);
6606 win
->g
.y
+= Y(win
->ws
->r
);
6609 config_win(win
, ev
);
6612 config_win(win
, ev
);
6617 configurenotify(XEvent
*e
)
6621 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%lx\n",
6622 e
->xconfigure
.window
);
6624 win
= find_window(e
->xconfigure
.window
);
6626 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &win
->sh_mask
);
6630 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6631 drain_enter_notify();
6636 destroynotify(XEvent
*e
)
6639 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
6641 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%lx\n", ev
->window
);
6643 if ((win
= find_window(ev
->window
)) == NULL
) {
6644 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6650 /* make sure we focus on something */
6653 unmanage_window(win
);
6655 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6656 drain_enter_notify();
6661 enternotify(XEvent
*e
)
6663 XCrossingEvent
*ev
= &e
->xcrossing
;
6668 Window focus_return
;
6669 int revert_to_return
;
6671 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%lx, mode: %d, detail: "
6672 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6673 "state: %d\n", ev
->window
, ev
->mode
, ev
->detail
, ev
->root
,
6674 ev
->subwindow
, YESNO(ev
->same_screen
), YESNO(ev
->focus
), ev
->state
);
6676 if (ev
->mode
!= NotifyNormal
) {
6677 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6682 switch (focus_mode
) {
6683 case SWM_FOCUS_DEFAULT
:
6685 case SWM_FOCUS_FOLLOW
:
6687 case SWM_FOCUS_SYNERGY
:
6690 * all these checks need to be in this order because the
6691 * XCheckTypedWindowEvent relies on weeding out the previous events
6693 * making this code an option would enable a follow mouse for focus
6698 * state is set when we are switching workspaces and focus is set when
6699 * the window or a subwindow already has focus (occurs during restart).
6701 * Only honor the focus flag if last_focus_event is not FocusOut,
6702 * this allows spectrwm to continue to control focus when another
6703 * program is also playing with it.
6705 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6706 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6711 * happens when a window is created or destroyed and the border
6712 * crosses the mouse pointer and when switching ws
6714 * we need the subwindow test to see if we came from root in order
6715 * to give focus to floaters
6717 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6718 ev
->subwindow
== 0) {
6719 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6723 /* this window already has focus */
6724 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6725 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6729 /* this window is being deleted or moved to another ws */
6730 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6732 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6733 XPutBackEvent(display
, &cne
);
6737 if ((win
= find_window(ev
->window
)) == NULL
) {
6738 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6743 * In fullstack kill all enters unless they come from a different ws
6744 * (i.e. another region) or focus has been grabbed externally.
6746 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6747 last_focus_event
!= FocusOut
) {
6748 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6749 if ((w
= find_window(focus_return
)) == NULL
||
6751 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6759 if ((win
= find_window(ev
->window
)) == NULL
) {
6760 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6765 * if we have more enternotifies let them handle it in due time
6767 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
6768 DNPRINTF(SWM_D_EVENT
,
6769 "ignoring enternotify: got more enternotify\n");
6770 XPutBackEvent(display
, &cne
);
6777 /* lets us use one switch statement for arbitrary mode/detail combinations */
6778 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6781 focusevent(XEvent
*e
)
6785 u_int32_t mode_detail
;
6786 XFocusChangeEvent
*ev
= &e
->xfocus
;
6788 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: 0x%lx mode: %d "
6789 "detail: %d\n", ev
->type
== FocusIn
? "entering" : "leaving",
6790 ev
->window
, ev
->mode
, ev
->detail
);
6792 if (last_focus_event
== ev
->type
) {
6793 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
6797 last_focus_event
= ev
->type
;
6798 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
6800 switch (mode_detail
) {
6801 /* synergy client focus operations */
6802 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
6803 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
6805 /* synergy server focus operations */
6806 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
6808 /* Entering applications like rdesktop that mangle the pointer */
6809 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
6811 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
6812 XSetWindowBorder(display
, win
->id
,
6813 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
6814 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
6817 warnx("ignoring focusevent");
6818 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
6825 mapnotify(XEvent
*e
)
6828 XMapEvent
*ev
= &e
->xmap
;
6830 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%lx\n", ev
->window
);
6832 win
= manage_window(ev
->window
);
6834 set_win_state(win
, NormalState
);
6837 * focus_win can only set input focus on a mapped window.
6838 * make sure the window really has focus since it is just being mapped.
6840 if (win
->ws
->focus
== win
)
6845 mappingnotify(XEvent
*e
)
6847 XMappingEvent
*ev
= &e
->xmapping
;
6849 XRefreshKeyboardMapping(ev
);
6850 if (ev
->request
== MappingKeyboard
)
6855 maprequest(XEvent
*e
)
6858 struct swm_region
*r
;
6859 XWindowAttributes wa
;
6860 XMapRequestEvent
*ev
= &e
->xmaprequest
;
6862 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%lx\n",
6863 e
->xmaprequest
.window
);
6865 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
6867 if (wa
.override_redirect
)
6870 win
= manage_window(e
->xmaprequest
.window
);
6872 return; /* can't happen */
6876 /* make new win focused */
6877 r
= root_to_region(win
->wa
.root
);
6878 if (win
->ws
== r
->ws
)
6883 propertynotify(XEvent
*e
)
6886 XPropertyEvent
*ev
= &e
->xproperty
;
6889 name
= XGetAtomName(display
, ev
->atom
);
6890 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%lx, atom: %s\n",
6895 win
= find_window(ev
->window
);
6899 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
6900 update_iconic(win
, 0);
6901 XMapRaised(display
, win
->id
);
6909 case XA_WM_NORMAL_HINTS
:
6911 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
6912 warnx("normal hints: flag 0x%x", win
->sh
.flags
);
6913 if (win
->sh
.flags
& PMinSize
) {
6914 WIDTH(win
) = win
->sh
.min_width
;
6915 HEIGHT(win
) = win
->sh
.min_height
;
6916 warnx("min %d %d", WIDTH(win
), HEIGHT(win
));
6918 XMoveResizeWindow(display
, win
->id
,
6919 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
));
6931 unmapnotify(XEvent
*e
)
6935 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%lx\n", e
->xunmap
.window
);
6937 /* determine if we need to help unmanage this window */
6938 win
= find_window(e
->xunmap
.window
);
6942 if (getstate(e
->xunmap
.window
) == NormalState
) {
6943 unmanage_window(win
);
6946 /* giant hack for apps that don't destroy transient windows */
6947 /* eat a bunch of events to prevent remanaging the window */
6949 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6950 EnterWindowMask
, &cne
))
6952 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6953 StructureNotifyMask
, &cne
))
6955 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6956 SubstructureNotifyMask
, &cne
))
6958 /* resend unmap because we ated it */
6959 XUnmapWindow(display
, e
->xunmap
.window
);
6962 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6963 drain_enter_notify();
6967 visibilitynotify(XEvent
*e
)
6970 struct swm_region
*r
;
6972 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%lx\n",
6973 e
->xvisibility
.window
);
6974 if (e
->xvisibility
.state
== VisibilityUnobscured
)
6975 for (i
= 0; i
< ScreenCount(display
); i
++)
6976 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6977 if (e
->xvisibility
.window
== WINID(r
->bar
))
6982 clientmessage(XEvent
*e
)
6984 XClientMessageEvent
*ev
;
6989 win
= find_window(ev
->window
);
6991 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
6992 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
6993 "unmanaged window.\n");
6994 e
->xmaprequest
.window
= ev
->window
;
7000 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx, type: %ld\n",
7001 ev
->window
, ev
->message_type
);
7003 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7004 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7007 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7008 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7009 if (win
->can_delete
)
7010 client_msg(win
, adelete
);
7012 XKillClient(display
, win
->id
);
7014 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7015 DNPRINTF(SWM_D_EVENT
,
7016 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7017 if (win
->floating
) {
7018 if (ev
->data
.l
[0] & (1<<8)) /* x */
7019 X(win
) = ev
->data
.l
[1];
7020 if (ev
->data
.l
[0] & (1<<9)) /* y */
7021 Y(win
) = ev
->data
.l
[2];
7022 if (ev
->data
.l
[0] & (1<<10)) /* width */
7023 WIDTH(win
) = ev
->data
.l
[3];
7024 if (ev
->data
.l
[0] & (1<<11)) /* height */
7025 HEIGHT(win
) = ev
->data
.l
[4];
7030 /* TODO: Change stack sizes */
7031 /* notify no change was made. */
7032 config_win(win
, NULL
);
7035 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
7036 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
7037 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
7039 ewmh_update_win_state(win
, ev
->data
.l
[2],
7047 xerror_start(Display
*d
, XErrorEvent
*ee
)
7054 xerror(Display
*d
, XErrorEvent
*ee
)
7056 /* warnx("error: %p %p", display, ee); */
7064 xerrorxlib
= XSetErrorHandler(xerror_start
);
7066 /* this causes an error if some other window manager is running */
7067 XSelectInput(display
, DefaultRootWindow(display
),
7068 SubstructureRedirectMask
);
7069 XSync(display
, False
);
7073 XSetErrorHandler(xerror
);
7074 XSync(display
, False
);
7079 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
7081 struct swm_region
*r
, *n
;
7082 struct workspace
*ws
= NULL
;
7085 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
7086 s
->idx
, w
, h
, x
, y
);
7088 /* remove any conflicting regions */
7089 n
= TAILQ_FIRST(&s
->rl
);
7092 n
= TAILQ_NEXT(r
, entry
);
7093 if (X(r
) < (x
+ w
) &&
7094 (X(r
) + WIDTH(r
)) > x
&&
7096 (Y(r
) + HEIGHT(r
)) > y
) {
7097 if (r
->ws
->r
!= NULL
)
7098 r
->ws
->old_r
= r
->ws
->r
;
7101 TAILQ_REMOVE(&s
->rl
, r
, entry
);
7102 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
7106 /* search old regions for one to reuse */
7108 /* size + location match */
7109 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7110 if (X(r
) == x
&& Y(r
) == y
&&
7111 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7115 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7116 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7120 TAILQ_REMOVE(&s
->orl
, r
, entry
);
7121 /* try to use old region's workspace */
7122 if (r
->ws
->r
== NULL
)
7125 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
7126 err(1, "new_region: calloc: failed to allocate memory "
7129 /* if we don't have a workspace already, find one */
7131 for (i
= 0; i
< workspace_limit
; i
++)
7132 if (s
->ws
[i
].r
== NULL
) {
7139 errx(1, "new_region: no free workspaces");
7150 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
7156 #ifdef SWM_XRR_HAS_CRTC
7158 XRRScreenResources
*sr
;
7161 #endif /* SWM_XRR_HAS_CRTC */
7162 struct swm_region
*r
;
7165 if (i
>= ScreenCount(display
))
7166 errx(1, "scan_xrandr: invalid screen");
7168 /* remove any old regions */
7169 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
7170 r
->ws
->old_r
= r
->ws
->r
= NULL
;
7172 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
7173 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
7177 /* map virtual screens onto physical screens */
7178 #ifdef SWM_XRR_HAS_CRTC
7179 if (xrandr_support
) {
7180 sr
= XRRGetScreenResourcesCurrent(display
, screens
[i
].root
);
7182 new_region(&screens
[i
], 0, 0,
7183 DisplayWidth(display
, i
),
7184 DisplayHeight(display
, i
));
7188 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
7189 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
7190 if (ci
->noutput
== 0)
7193 if (ci
!= NULL
&& ci
->mode
== None
)
7194 new_region(&screens
[i
], 0, 0,
7195 DisplayWidth(display
, i
),
7196 DisplayHeight(display
, i
));
7198 new_region(&screens
[i
],
7199 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
7202 XRRFreeCrtcInfo(ci
);
7203 XRRFreeScreenResources(sr
);
7205 #endif /* SWM_XRR_HAS_CRTC */
7207 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
7208 DisplayHeight(display
, i
));
7213 screenchange(XEvent
*e
) {
7214 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
7215 struct swm_region
*r
;
7218 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%lx\n", xe
->root
);
7220 if (!XRRUpdateConfiguration(e
))
7223 /* silly event doesn't include the screen index */
7224 for (i
= 0; i
< ScreenCount(display
); i
++)
7225 if (screens
[i
].root
== xe
->root
)
7227 if (i
>= ScreenCount(display
))
7228 errx(1, "screenchange: screen not found");
7230 /* brute force for now, just re-enumerate the regions */
7233 /* add bars to all regions */
7234 for (i
= 0; i
< ScreenCount(display
); i
++)
7235 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7238 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7239 drain_enter_notify();
7245 Window d1
, d2
, *wins
= NULL
;
7246 XWindowAttributes wa
;
7251 for (i
= 0; i
< ScreenCount(display
); i
++) {
7252 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
7255 /* attach windows to a region */
7256 /* normal windows */
7257 for (j
= 0; j
< no
; j
++) {
7258 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
7259 wa
.override_redirect
||
7260 XGetTransientForHint(display
, wins
[j
], &d1
))
7263 state
= getstate(wins
[j
]);
7264 manage
= state
== IconicState
;
7265 if (wa
.map_state
== IsViewable
|| manage
)
7266 manage_window(wins
[j
]);
7268 /* transient windows */
7269 for (j
= 0; j
< no
; j
++) {
7270 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
7271 wa
.override_redirect
)
7274 state
= getstate(wins
[j
]);
7275 manage
= state
== IconicState
;
7276 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
7278 manage_window(wins
[j
]);
7291 int errorbase
, major
, minor
;
7292 struct workspace
*ws
;
7295 if ((screens
= calloc(ScreenCount(display
),
7296 sizeof(struct swm_screen
))) == NULL
)
7297 err(1, "setup_screens: calloc: failed to allocate memory for "
7300 /* initial Xrandr setup */
7301 xrandr_support
= XRRQueryExtension(display
,
7302 &xrandr_eventbase
, &errorbase
);
7304 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
7307 /* map physical screens */
7308 for (i
= 0; i
< ScreenCount(display
); i
++) {
7309 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
7311 TAILQ_INIT(&screens
[i
].rl
);
7312 TAILQ_INIT(&screens
[i
].orl
);
7313 screens
[i
].root
= RootWindow(display
, i
);
7315 /* set default colors */
7316 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
7317 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
7318 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
7319 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
7320 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
7322 /* create graphics context on screen */
7323 gcv
.graphics_exposures
= 0;
7324 screens
[i
].bar_gc
= XCreateGC(display
, screens
[i
].root
,
7325 GCGraphicsExposures
, &gcv
);
7327 /* set default cursor */
7328 XDefineCursor(display
, screens
[i
].root
,
7329 XCreateFontCursor(display
, XC_left_ptr
));
7331 /* init all workspaces */
7332 /* XXX these should be dynamically allocated too */
7333 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
7334 ws
= &screens
[i
].ws
[j
];
7340 TAILQ_INIT(&ws
->winlist
);
7341 TAILQ_INIT(&ws
->unmanagedlist
);
7343 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
7344 if (layouts
[k
].l_config
!= NULL
)
7345 layouts
[k
].l_config(ws
,
7346 SWM_ARG_ID_STACKINIT
);
7347 ws
->cur_layout
= &layouts
[0];
7348 ws
->cur_layout
->l_string(ws
);
7354 XRRSelectInput(display
, screens
[i
].root
,
7355 RRScreenChangeNotifyMask
);
7362 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
7363 err(1, "setup_globals: strdup: failed to allocate memory.");
7365 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
7366 err(1, "setup_globals: strdup: failed to allocate memory.");
7373 Atom netwmcheck
, netwmname
, utf8_string
;
7376 /* work around sun jdk bugs, code from wmname */
7377 netwmcheck
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
7378 netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
7379 utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
7380 for (i
= 0; i
< ScreenCount(display
); i
++) {
7381 root
= screens
[i
].root
;
7382 win
= XCreateSimpleWindow(display
,root
, 0, 0, 1, 1, 0,
7383 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
,
7384 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
);
7386 XChangeProperty(display
, root
, netwmcheck
, XA_WINDOW
, 32,
7387 PropModeReplace
, (unsigned char *)&win
, 1);
7388 XChangeProperty(display
, win
, netwmcheck
, XA_WINDOW
, 32,
7389 PropModeReplace
, (unsigned char *)&win
, 1);
7390 XChangeProperty(display
, win
, netwmname
, utf8_string
, 8,
7391 PropModeReplace
, (unsigned char*)"LG3D", strlen("LG3D"));
7396 main(int argc
, char *argv
[])
7398 struct swm_region
*r
, *rr
;
7399 struct ws_win
*winfocus
= NULL
;
7402 char conf
[PATH_MAX
], *cfile
= NULL
;
7407 struct sigaction sact
;
7410 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
7411 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
7413 warnx("no locale support");
7415 if (!X_HAVE_UTF8_STRING
)
7416 warnx("no UTF-8 support");
7418 if (!(display
= XOpenDisplay(0)))
7419 errx(1, "can not open display");
7422 errx(1, "other wm running");
7424 /* handle some signals */
7425 bzero(&sact
, sizeof(sact
));
7426 sigemptyset(&sact
.sa_mask
);
7428 sact
.sa_handler
= sighdlr
;
7429 sigaction(SIGINT
, &sact
, NULL
);
7430 sigaction(SIGQUIT
, &sact
, NULL
);
7431 sigaction(SIGTERM
, &sact
, NULL
);
7432 sigaction(SIGHUP
, &sact
, NULL
);
7434 sact
.sa_handler
= sighdlr
;
7435 sact
.sa_flags
= SA_NOCLDSTOP
;
7436 sigaction(SIGCHLD
, &sact
, NULL
);
7438 astate
= XInternAtom(display
, "WM_STATE", False
);
7439 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
7440 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
7441 takefocus
= XInternAtom(display
, "WM_TAKE_FOCUS", False
);
7442 a_wmname
= XInternAtom(display
, "WM_NAME", False
);
7443 a_netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
7444 a_utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
7445 a_string
= XInternAtom(display
, "STRING", False
);
7446 a_swm_iconic
= XInternAtom(display
, "_SWM_ICONIC", False
);
7448 /* look for local and global conf file */
7449 pwd
= getpwuid(getuid());
7451 errx(1, "invalid user: %d", getuid());
7460 for (i
= 0; ; i
++) {
7465 snprintf(conf
, sizeof conf
, "%s/.%s",
7466 pwd
->pw_dir
, SWM_CONF_FILE
);
7470 snprintf(conf
, sizeof conf
, "/etc/%s",
7475 snprintf(conf
, sizeof conf
, "%s/.%s",
7476 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
7480 snprintf(conf
, sizeof conf
, "/etc/%s",
7487 if (strlen(conf
) && stat(conf
, &sb
) != -1)
7488 if (S_ISREG(sb
.st_mode
)) {
7495 /* load conf (if any) */
7497 conf_load(cfile
, SWM_CONF_DEFAULT
);
7500 /* set some values to work around bad programs */
7502 /* grab existing windows (before we build the bars) */
7505 if (getenv("SWM_STARTED") == NULL
)
7506 setenv("SWM_STARTED", "YES", 1);
7508 /* setup all bars */
7509 for (i
= 0; i
< ScreenCount(display
); i
++)
7510 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
7511 if (winfocus
== NULL
)
7512 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
7520 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7521 drain_enter_notify();
7523 xfd
= ConnectionNumber(display
);
7525 while (XPending(display
)) {
7526 XNextEvent(display
, &e
);
7529 if (e
.type
< LASTEvent
) {
7530 DNPRINTF(SWM_D_EVENTQ
,"XEvent: handled: %s, "
7531 "window: 0x%lx, type: %s (%d), %d remaining"
7532 "\n", YESNO(handler
[e
.type
]),
7533 e
.xany
.window
, geteventname(&e
),
7534 e
.type
, QLength(display
));
7536 if (handler
[e
.type
])
7537 handler
[e
.type
](&e
);
7539 DNPRINTF(SWM_D_EVENTQ
, "XRandr Event: window: "
7540 "0x%lx, type: %s (%d)\n", e
.xany
.window
,
7541 xrandr_geteventname(&e
), e
.type
);
7543 switch (e
.type
- xrandr_eventbase
) {
7544 case RRScreenChangeNotify
:
7553 /* if we are being restarted go focus on first window */
7555 rr
= winfocus
->ws
->r
;
7557 /* not a visible window */
7561 /* move pointer to first screen if multi screen */
7562 if (ScreenCount(display
) > 1 || outputs
> 1)
7563 XWarpPointer(display
, None
, rr
->s
[0].root
,
7565 Y(rr
) + (bar_enabled
? bar_height
: 0));
7567 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7577 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7579 DNPRINTF(SWM_D_MISC
, "select failed");
7580 if (restart_wm
== 1)
7581 restart(NULL
, NULL
);
7582 if (search_resp
== 1)
7595 for (i
= 0; i
< ScreenCount(display
); ++i
)
7596 if (screens
[i
].bar_gc
!= NULL
)
7597 XFreeGC(display
, screens
[i
].bar_gc
);
7599 XFreeFontSet(display
, bar_fs
);
7600 XCloseDisplay(display
);