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/Xatom.h>
93 #include <X11/Xproto.h>
94 #include <X11/Xutil.h>
95 #include <X11/extensions/Xrandr.h>
96 #include <X11/extensions/XTest.h>
104 #ifdef SPECTRWM_BUILDSTR
105 static const char *buildstr
= SPECTRWM_BUILDSTR
;
107 static const char *buildstr
= SPECTRWM_VERSION
;
111 # error XRandR versions less than 1.0 are not supported
116 #define SWM_XRR_HAS_CRTC
120 /*#define SWM_DEBUG*/
122 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
123 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
124 #define SWM_D_MISC 0x0001
125 #define SWM_D_EVENT 0x0002
126 #define SWM_D_WS 0x0004
127 #define SWM_D_FOCUS 0x0008
128 #define SWM_D_MOVE 0x0010
129 #define SWM_D_STACK 0x0020
130 #define SWM_D_MOUSE 0x0040
131 #define SWM_D_PROP 0x0080
132 #define SWM_D_CLASS 0x0100
133 #define SWM_D_KEY 0x0200
134 #define SWM_D_QUIRK 0x0400
135 #define SWM_D_SPAWN 0x0800
136 #define SWM_D_EVENTQ 0x1000
137 #define SWM_D_CONF 0x2000
138 #define SWM_D_BAR 0x4000
140 u_int32_t swm_debug
= 0
158 #define DPRINTF(x...)
159 #define DNPRINTF(n,x...)
162 #define LENGTH(x) (sizeof x / sizeof x[0])
163 #define MODKEY Mod1Mask
164 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
165 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
166 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
167 #define SWM_PROPLEN (16)
168 #define SWM_FUNCNAME_LEN (32)
169 #define SWM_KEYS_LEN (255)
170 #define SWM_QUIRK_LEN (64)
171 #define X(r) (r)->g.x
172 #define Y(r) (r)->g.y
173 #define WIDTH(r) (r)->g.w
174 #define HEIGHT(r) (r)->g.h
175 #define SH_MIN(w) (w)->sh_mask & PMinSize
176 #define SH_MIN_W(w) (w)->sh.min_width
177 #define SH_MIN_H(w) (w)->sh.min_height
178 #define SH_MAX(w) (w)->sh_mask & PMaxSize
179 #define SH_MAX_W(w) (w)->sh.max_width
180 #define SH_MAX_H(w) (w)->sh.max_height
181 #define SH_INC(w) (w)->sh_mask & PResizeInc
182 #define SH_INC_W(w) (w)->sh.width_inc
183 #define SH_INC_H(w) (w)->sh.height_inc
184 #define SWM_MAX_FONT_STEPS (3)
185 #define WINID(w) ((w) ? (w)->id : 0)
186 #define YESNO(x) ((x) ? "yes" : "no")
188 #define SWM_FOCUS_DEFAULT (0)
189 #define SWM_FOCUS_SYNERGY (1)
190 #define SWM_FOCUS_FOLLOW (2)
192 #define SWM_CONF_DEFAULT (0)
193 #define SWM_CONF_KEYMAPPING (1)
196 #define SWM_LIB "/usr/local/lib/libswmhack.so"
209 volatile sig_atomic_t running
= 1;
210 volatile sig_atomic_t restart_wm
= 0;
212 int last_focus_event
= FocusOut
;
213 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
217 int xrandr_eventbase
;
218 unsigned int numlockmask
= 0;
222 int cycle_visible
= 0;
224 int font_adjusted
= 0;
225 unsigned int mod_key
= MODKEY
;
228 struct swm_region
*search_r
;
229 int select_list_pipe
[2];
230 int select_resp_pipe
[2];
232 volatile sig_atomic_t search_resp
;
233 int search_resp_action
;
235 struct search_window
{
236 TAILQ_ENTRY(search_window
) entry
;
242 TAILQ_HEAD(search_winlist
, search_window
);
244 struct search_winlist search_wl
;
249 SWM_SEARCH_UNICONIFY
,
250 SWM_SEARCH_NAME_WORKSPACE
,
251 SWM_SEARCH_SEARCH_WORKSPACE
,
252 SWM_SEARCH_SEARCH_WINDOW
256 double dialog_ratio
= 0.6;
258 #define SWM_BAR_MAX (256)
259 #define SWM_BAR_JUSTIFY_LEFT (0)
260 #define SWM_BAR_JUSTIFY_CENTER (1)
261 #define SWM_BAR_JUSTIFY_RIGHT (2)
262 #define SWM_BAR_OFFSET (4)
263 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
264 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
265 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
266 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"
268 #ifdef X_HAVE_UTF8_STRING
269 #define DRAWSTRING(x...) Xutf8DrawString(x)
271 #define DRAWSTRING(x...) XmbDrawString(x)
274 char *bar_argv
[] = { NULL
, NULL
};
276 unsigned char bar_ext
[SWM_BAR_MAX
];
277 char bar_vertext
[SWM_BAR_MAX
];
279 sig_atomic_t bar_alarm
= 0;
282 int bar_border_width
= 1;
283 int bar_at_bottom
= 0;
285 int bar_extra_running
= 0;
288 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
289 int stack_enabled
= 1;
290 int clock_enabled
= 1;
291 int urgent_enabled
= 0;
292 char *clock_format
= NULL
;
293 int title_name_enabled
= 0;
294 int title_class_enabled
= 0;
295 int window_name_enabled
= 0;
296 int focus_mode
= SWM_FOCUS_DEFAULT
;
297 int disable_border
= 0;
298 int border_width
= 1;
299 int verbose_layout
= 0;
302 XFontSetExtents
*bar_fs_extents
;
304 char *spawn_term
[] = { NULL
, NULL
}; /* XXX fully dynamic */
307 #define SWM_MENU_FN (2)
308 #define SWM_MENU_NB (4)
309 #define SWM_MENU_NF (6)
310 #define SWM_MENU_SB (8)
311 #define SWM_MENU_SF (10)
313 /* layout manager data */
314 struct swm_geometry
{
324 /* virtual "screens" */
326 TAILQ_ENTRY(swm_region
) entry
;
327 struct swm_geometry g
;
328 struct workspace
*ws
; /* current workspace on this region */
329 struct workspace
*ws_prior
; /* prior workspace on this region */
330 struct swm_screen
*s
; /* screen idx */
333 TAILQ_HEAD(swm_region_list
, swm_region
);
336 TAILQ_ENTRY(ws_win
) entry
;
339 struct ws_win
*child_trans
; /* transient child window */
340 struct swm_geometry g
; /* current geometry */
341 struct swm_geometry g_float
; /* geometry when floating */
342 struct swm_geometry rg_float
; /* region geom when floating */
343 int g_floatvalid
; /* g_float geometry validity */
344 int floatmaxed
; /* whether maxed by max_stack */
348 unsigned int ewmh_flags
;
349 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
355 unsigned long quirks
;
356 struct workspace
*ws
; /* always valid */
357 struct swm_screen
*s
; /* always valid, never changes */
358 XWindowAttributes wa
;
364 TAILQ_HEAD(ws_win_list
, ws_win
);
368 TAILQ_ENTRY(pid_e
) entry
;
372 TAILQ_HEAD(pid_list
, pid_e
);
373 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
375 /* layout handlers */
377 void vertical_config(struct workspace
*, int);
378 void vertical_stack(struct workspace
*, struct swm_geometry
*);
379 void horizontal_config(struct workspace
*, int);
380 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
381 void max_stack(struct workspace
*, struct swm_geometry
*);
382 void plain_stacker(struct workspace
*);
383 void fancy_stacker(struct workspace
*);
385 struct ws_win
*find_window(Window
);
387 void grabbuttons(struct ws_win
*, int);
388 void new_region(struct swm_screen
*, int, int, int, int);
389 void unmanage_window(struct ws_win
*);
390 long getstate(Window
);
392 int conf_load(char *, int);
395 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
396 void (*l_config
)(struct workspace
*, int);
398 #define SWM_L_FOCUSPREV (1<<0)
399 #define SWM_L_MAPONFOCUS (1<<1)
400 void (*l_string
)(struct workspace
*);
402 /* stack, configure */
403 { vertical_stack
, vertical_config
, 0, plain_stacker
},
404 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
406 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
407 { NULL
, NULL
, 0, NULL
},
410 /* position of max_stack mode in the layouts array, index into layouts! */
411 #define SWM_V_STACK (0)
412 #define SWM_H_STACK (1)
413 #define SWM_MAX_STACK (2)
415 #define SWM_H_SLICE (32)
416 #define SWM_V_SLICE (32)
418 /* define work spaces */
420 int idx
; /* workspace index */
421 char *name
; /* workspace name */
422 int always_raise
; /* raise windows on focus */
423 struct layout
*cur_layout
; /* current layout handlers */
424 struct ws_win
*focus
; /* may be NULL */
425 struct ws_win
*focus_prev
; /* may be NULL */
426 struct swm_region
*r
; /* may be NULL */
427 struct swm_region
*old_r
; /* may be NULL */
428 struct ws_win_list winlist
; /* list of windows in ws */
429 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
430 char stacker
[10]; /* display stacker and layout */
434 int horizontal_msize
;
436 int horizontal_stacks
;
445 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
446 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
448 /* physical screen mapping */
449 #define SWM_WS_MAX (10)
451 int idx
; /* screen index */
452 struct swm_region_list rl
; /* list of regions on this screen */
453 struct swm_region_list orl
; /* list of old regions */
455 struct workspace ws
[SWM_WS_MAX
];
461 } c
[SWM_S_COLOR_MAX
];
465 struct swm_screen
*screens
;
468 /* args to functions */
471 #define SWM_ARG_ID_FOCUSNEXT (0)
472 #define SWM_ARG_ID_FOCUSPREV (1)
473 #define SWM_ARG_ID_FOCUSMAIN (2)
474 #define SWM_ARG_ID_FOCUSCUR (4)
475 #define SWM_ARG_ID_SWAPNEXT (10)
476 #define SWM_ARG_ID_SWAPPREV (11)
477 #define SWM_ARG_ID_SWAPMAIN (12)
478 #define SWM_ARG_ID_MOVELAST (13)
479 #define SWM_ARG_ID_MASTERSHRINK (20)
480 #define SWM_ARG_ID_MASTERGROW (21)
481 #define SWM_ARG_ID_MASTERADD (22)
482 #define SWM_ARG_ID_MASTERDEL (23)
483 #define SWM_ARG_ID_FLIPLAYOUT (24)
484 #define SWM_ARG_ID_STACKRESET (30)
485 #define SWM_ARG_ID_STACKINIT (31)
486 #define SWM_ARG_ID_CYCLEWS_UP (40)
487 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
488 #define SWM_ARG_ID_CYCLESC_UP (42)
489 #define SWM_ARG_ID_CYCLESC_DOWN (43)
490 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
491 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
492 #define SWM_ARG_ID_STACKINC (50)
493 #define SWM_ARG_ID_STACKDEC (51)
494 #define SWM_ARG_ID_SS_ALL (60)
495 #define SWM_ARG_ID_SS_WINDOW (61)
496 #define SWM_ARG_ID_DONTCENTER (70)
497 #define SWM_ARG_ID_CENTER (71)
498 #define SWM_ARG_ID_KILLWINDOW (80)
499 #define SWM_ARG_ID_DELETEWINDOW (81)
500 #define SWM_ARG_ID_WIDTHGROW (90)
501 #define SWM_ARG_ID_WIDTHSHRINK (91)
502 #define SWM_ARG_ID_HEIGHTGROW (92)
503 #define SWM_ARG_ID_HEIGHTSHRINK (93)
504 #define SWM_ARG_ID_MOVEUP (100)
505 #define SWM_ARG_ID_MOVEDOWN (101)
506 #define SWM_ARG_ID_MOVELEFT (102)
507 #define SWM_ARG_ID_MOVERIGHT (103)
511 void focus(struct swm_region
*, union arg
*);
512 void focus_magic(struct ws_win
*);
516 TAILQ_ENTRY(quirk
) entry
;
520 #define SWM_Q_FLOAT (1<<0) /* float this window */
521 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
522 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
523 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
524 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
525 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
527 TAILQ_HEAD(quirk_list
, quirk
);
528 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
531 * Supported EWMH hints should be added to
532 * both the enum and the ewmh array
534 enum { _NET_ACTIVE_WINDOW
, _NET_MOVERESIZE_WINDOW
, _NET_CLOSE_WINDOW
,
535 _NET_WM_WINDOW_TYPE
, _NET_WM_WINDOW_TYPE_DOCK
,
536 _NET_WM_WINDOW_TYPE_TOOLBAR
, _NET_WM_WINDOW_TYPE_UTILITY
,
537 _NET_WM_WINDOW_TYPE_SPLASH
, _NET_WM_WINDOW_TYPE_DIALOG
,
538 _NET_WM_WINDOW_TYPE_NORMAL
, _NET_WM_STATE
,
539 _NET_WM_STATE_MAXIMIZED_HORZ
, _NET_WM_STATE_MAXIMIZED_VERT
,
540 _NET_WM_STATE_SKIP_TASKBAR
, _NET_WM_STATE_SKIP_PAGER
,
541 _NET_WM_STATE_HIDDEN
, _NET_WM_STATE_ABOVE
, _SWM_WM_STATE_MANUAL
,
542 _NET_WM_STATE_FULLSCREEN
, _NET_WM_ALLOWED_ACTIONS
, _NET_WM_ACTION_MOVE
,
543 _NET_WM_ACTION_RESIZE
, _NET_WM_ACTION_FULLSCREEN
, _NET_WM_ACTION_CLOSE
,
549 } ewmh
[SWM_EWMH_HINT_MAX
] = {
550 /* must be in same order as in the enum */
551 {"_NET_ACTIVE_WINDOW", None
},
552 {"_NET_MOVERESIZE_WINDOW", None
},
553 {"_NET_CLOSE_WINDOW", None
},
554 {"_NET_WM_WINDOW_TYPE", None
},
555 {"_NET_WM_WINDOW_TYPE_DOCK", None
},
556 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None
},
557 {"_NET_WM_WINDOW_TYPE_UTILITY", None
},
558 {"_NET_WM_WINDOW_TYPE_SPLASH", None
},
559 {"_NET_WM_WINDOW_TYPE_DIALOG", None
},
560 {"_NET_WM_WINDOW_TYPE_NORMAL", None
},
561 {"_NET_WM_STATE", None
},
562 {"_NET_WM_STATE_MAXIMIZED_HORZ", None
},
563 {"_NET_WM_STATE_MAXIMIZED_VERT", None
},
564 {"_NET_WM_STATE_SKIP_TASKBAR", None
},
565 {"_NET_WM_STATE_SKIP_PAGER", None
},
566 {"_NET_WM_STATE_HIDDEN", None
},
567 {"_NET_WM_STATE_ABOVE", None
},
568 {"_SWM_WM_STATE_MANUAL", None
},
569 {"_NET_WM_STATE_FULLSCREEN", None
},
570 {"_NET_WM_ALLOWED_ACTIONS", None
},
571 {"_NET_WM_ACTION_MOVE", None
},
572 {"_NET_WM_ACTION_RESIZE", None
},
573 {"_NET_WM_ACTION_FULLSCREEN", None
},
574 {"_NET_WM_ACTION_CLOSE", None
},
577 void store_float_geom(struct ws_win
*, struct swm_region
*);
578 int floating_toggle_win(struct ws_win
*);
579 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
580 unsigned char *get_win_name(Window
);
583 get_property(Window id
, Atom atom
, long count
, Atom type
, unsigned long *nitems
,
584 unsigned long *nbytes
, unsigned char **data
)
587 unsigned long *nbytes_ret
, *nitems_ret
;
588 unsigned long nbytes_tmp
, nitems_tmp
;
591 nbytes_ret
= nbytes
!= NULL
? nbytes
: &nbytes_tmp
;
592 nitems_ret
= nitems
!= NULL
? nitems
: &nitems_tmp
;
594 status
= XGetWindowProperty(display
, id
, atom
, 0L, count
, False
, type
,
595 &real
, &format
, nitems_ret
, nbytes_ret
, data
);
597 if (status
!= Success
)
606 update_iconic(struct ws_win
*win
, int newv
)
613 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
617 XChangeProperty(display
, win
->id
, iprop
, XA_INTEGER
, 32,
618 PropModeReplace
, (unsigned char *)&v
, 1);
620 XDeleteProperty(display
, win
->id
, iprop
);
624 get_iconic(struct ws_win
*win
)
629 unsigned long nitems
, extra
;
630 unsigned char *prop
= NULL
;
632 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
635 status
= XGetWindowProperty(display
, win
->id
, iprop
, 0L, 1L,
636 False
, XA_INTEGER
, &rettype
, &retfmt
, &nitems
, &extra
, &prop
);
637 if (status
!= Success
)
639 if (rettype
!= XA_INTEGER
|| retfmt
!= 32)
643 v
= *((int32_t *)prop
);
657 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
659 for (i
= 0; i
< LENGTH(ewmh
); i
++)
660 ewmh
[i
].atom
= XInternAtom(display
, ewmh
[i
].name
, False
);
662 for (i
= 0; i
< ScreenCount(display
); i
++) {
663 /* Support check window will be created by workaround(). */
665 /* Report supported atoms */
666 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
667 for (j
= 0; j
< LENGTH(ewmh
); j
++)
668 XChangeProperty(display
, screens
[i
].root
,
669 sup_list
, XA_ATOM
, 32,
670 PropModeAppend
, (unsigned char *)&ewmh
[j
].atom
,1);
678 unsigned char *data
= NULL
;
680 Atom sup_check
, sup_list
;
683 sup_check
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
684 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
686 for (i
= 0; i
< ScreenCount(display
); i
++) {
687 /* Get the support check window and destroy it */
688 success
= get_property(screens
[i
].root
, sup_check
, 1, XA_WINDOW
,
693 XDestroyWindow(display
, id
);
694 XDeleteProperty(display
, screens
[i
].root
, sup_check
);
695 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
703 ewmh_autoquirk(struct ws_win
*win
)
706 unsigned long *data
= NULL
, n
;
709 success
= get_property(win
->id
, ewmh
[_NET_WM_WINDOW_TYPE
].atom
, (~0L),
710 XA_ATOM
, &n
, NULL
, (void *)&data
);
717 for (i
= 0; i
< n
; i
++) {
719 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
721 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
722 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
723 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
725 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
728 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
729 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
731 win
->quirks
= SWM_Q_FLOAT
;
739 #define SWM_EWMH_ACTION_COUNT_MAX (6)
740 #define EWMH_F_FULLSCREEN (1<<0)
741 #define EWMH_F_ABOVE (1<<1)
742 #define EWMH_F_HIDDEN (1<<2)
743 #define EWMH_F_SKIP_PAGER (1<<3)
744 #define EWMH_F_SKIP_TASKBAR (1<<4)
745 #define SWM_F_MANUAL (1<<5)
748 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
750 struct swm_geometry rg
;
758 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%lx, "
759 "fullscreen %s\n", win
->id
, YESNO(fs
));
764 store_float_geom(win
, win
->ws
->r
);
768 if (win
->g_floatvalid
) {
769 /* refloat at last floating relative position */
770 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ rg
.x
;
771 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ rg
.y
;
772 WIDTH(win
) = win
->g_float
.w
;
773 HEIGHT(win
) = win
->g_float
.h
;
781 ewmh_update_actions(struct ws_win
*win
)
783 Atom actions
[SWM_EWMH_ACTION_COUNT_MAX
];
789 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
792 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
793 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
796 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
,
797 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)actions
, n
);
800 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
801 #define _NET_WM_STATE_ADD 1 /* add/set property */
802 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
805 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
807 unsigned int mask
= 0;
808 unsigned int changed
= 0;
809 unsigned int orig_flags
;
814 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
815 mask
= EWMH_F_FULLSCREEN
;
816 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
818 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
820 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
821 mask
= EWMH_F_SKIP_PAGER
;
822 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
823 mask
= EWMH_F_SKIP_TASKBAR
;
826 orig_flags
= win
->ewmh_flags
;
829 case _NET_WM_STATE_REMOVE
:
830 win
->ewmh_flags
&= ~mask
;
832 case _NET_WM_STATE_ADD
:
833 win
->ewmh_flags
|= mask
;
835 case _NET_WM_STATE_TOGGLE
:
836 win
->ewmh_flags
^= mask
;
840 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
842 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
844 if (!floating_toggle_win(win
))
845 win
->ewmh_flags
= orig_flags
; /* revert */
846 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
848 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
849 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
851 if (!ewmh_set_win_fullscreen(win
,
852 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
853 win
->ewmh_flags
= orig_flags
; /* revert */
855 XDeleteProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
857 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
858 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
859 XA_ATOM
, 32, PropModeAppend
,
860 (unsigned char *)&ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
, 1);
861 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
862 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
863 XA_ATOM
, 32, PropModeAppend
,
864 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
, 1);
865 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
866 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
867 XA_ATOM
, 32, PropModeAppend
,
868 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
, 1);
869 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
870 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
871 XA_ATOM
, 32, PropModeAppend
,
872 (unsigned char *)&ewmh
[_NET_WM_STATE_ABOVE
].atom
, 1);
873 if (win
->ewmh_flags
& SWM_F_MANUAL
)
874 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
875 XA_ATOM
, 32, PropModeAppend
,
876 (unsigned char *)&ewmh
[_SWM_WM_STATE_MANUAL
].atom
, 1);
880 ewmh_get_win_state(struct ws_win
*win
)
891 win
->ewmh_flags
|= EWMH_F_ABOVE
;
893 win
->ewmh_flags
|= SWM_F_MANUAL
;
895 success
= get_property(win
->id
, ewmh
[_NET_WM_STATE
].atom
,
896 (~0L), XA_ATOM
, &n
, NULL
, (void *)&states
);
901 for (i
= 0; i
< n
; i
++)
902 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
910 geteventname(XEvent
*e
)
922 name
= "ButtonPress";
925 name
= "ButtonRelease";
928 name
= "MotionNotify";
931 name
= "EnterNotify";
934 name
= "LeaveNotify";
943 name
= "KeymapNotify";
949 name
= "GraphicsExpose";
954 case VisibilityNotify
:
955 name
= "VisibilityNotify";
958 name
= "CreateNotify";
961 name
= "DestroyNotify";
964 name
= "UnmapNotify";
973 name
= "ReparentNotify";
975 case ConfigureNotify
:
976 name
= "ConfigureNotify";
978 case ConfigureRequest
:
979 name
= "ConfigureRequest";
982 name
= "GravityNotify";
985 name
= "ResizeRequest";
987 case CirculateNotify
:
988 name
= "CirculateNotify";
990 case CirculateRequest
:
991 name
= "CirculateRequest";
994 name
= "PropertyNotify";
997 name
= "SelectionClear";
999 case SelectionRequest
:
1000 name
= "SelectionRequest";
1002 case SelectionNotify
:
1003 name
= "SelectionNotify";
1005 case ColormapNotify
:
1006 name
= "ColormapNotify";
1009 name
= "ClientMessage";
1012 name
= "MappingNotify";
1022 xrandr_geteventname(XEvent
*e
)
1026 switch(e
->type
- xrandr_eventbase
) {
1027 case RRScreenChangeNotify
:
1028 name
= "RRScreenChangeNotify";
1038 dumpwins(struct swm_region
*r
, union arg
*args
)
1042 XWindowAttributes wa
;
1044 if (r
->ws
== NULL
) {
1045 warnx("dumpwins: invalid workspace");
1049 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1051 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1052 state
= getstate(win
->id
);
1053 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1054 warnx("window: 0x%lx, failed XGetWindowAttributes",
1056 warnx("window: 0x%lx, map_state: %d, state: %d, "
1057 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1061 warnx("===== unmanaged window list =====");
1062 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1063 state
= getstate(win
->id
);
1064 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1065 warnx("window: 0x%lx, failed XGetWindowAttributes",
1067 warnx("window: 0x%lx, map_state: %d, state: %d, "
1068 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1072 warnx("=================================");
1076 dumpwins(struct swm_region
*r
, union arg
*args
)
1079 #endif /* SWM_DEBUG */
1081 void expose(XEvent
*);
1082 void keypress(XEvent
*);
1083 void buttonpress(XEvent
*);
1084 void configurerequest(XEvent
*);
1085 void configurenotify(XEvent
*);
1086 void destroynotify(XEvent
*);
1087 void enternotify(XEvent
*);
1088 void focusevent(XEvent
*);
1089 void mapnotify(XEvent
*);
1090 void mappingnotify(XEvent
*);
1091 void maprequest(XEvent
*);
1092 void propertynotify(XEvent
*);
1093 void unmapnotify(XEvent
*);
1094 void visibilitynotify(XEvent
*);
1095 void clientmessage(XEvent
*);
1097 void (*handler
[LASTEvent
])(XEvent
*) = {
1099 [KeyPress
] = keypress
,
1100 [ButtonPress
] = buttonpress
,
1101 [ConfigureRequest
] = configurerequest
,
1102 [ConfigureNotify
] = configurenotify
,
1103 [DestroyNotify
] = destroynotify
,
1104 [EnterNotify
] = enternotify
,
1105 [FocusIn
] = focusevent
,
1106 [FocusOut
] = focusevent
,
1107 [MapNotify
] = mapnotify
,
1108 [MappingNotify
] = mappingnotify
,
1109 [MapRequest
] = maprequest
,
1110 [PropertyNotify
] = propertynotify
,
1111 [UnmapNotify
] = unmapnotify
,
1112 [VisibilityNotify
] = visibilitynotify
,
1113 [ClientMessage
] = clientmessage
,
1119 int saved_errno
, status
;
1122 saved_errno
= errno
;
1126 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1131 if (errno
!= ECHILD
)
1132 warn("sighdlr: waitpid");
1133 #endif /* SWM_DEBUG */
1136 if (pid
== searchpid
)
1140 if (WIFEXITED(status
)) {
1141 if (WEXITSTATUS(status
) != 0)
1142 warnx("sighdlr: child exit status: %d",
1143 WEXITSTATUS(status
));
1145 warnx("sighdlr: child is terminated "
1147 #endif /* SWM_DEBUG */
1161 errno
= saved_errno
;
1167 struct pid_e
*p
= NULL
;
1169 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1174 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1183 name_to_color(char *colorname
)
1187 XColor screen_def
, exact_def
;
1188 unsigned long result
= 0;
1189 char cname
[32] = "#";
1191 cmap
= DefaultColormap(display
, screens
[0].idx
);
1192 status
= XAllocNamedColor(display
, cmap
, colorname
,
1193 &screen_def
, &exact_def
);
1195 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1196 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
1200 result
= screen_def
.pixel
;
1202 warnx("color '%s' not found", colorname
);
1208 setscreencolor(char *val
, int i
, int c
)
1210 if (i
> 0 && i
<= ScreenCount(display
)) {
1211 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1212 free(screens
[i
- 1].c
[c
].name
);
1213 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1215 } else if (i
== -1) {
1216 for (i
= 0; i
< ScreenCount(display
); i
++) {
1217 screens
[i
].c
[c
].color
= name_to_color(val
);
1218 free(screens
[i
].c
[c
].name
);
1219 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1223 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1224 i
, ScreenCount(display
));
1228 fancy_stacker(struct workspace
*ws
)
1230 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1231 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1232 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1233 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1234 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1235 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1236 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1237 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1238 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1242 plain_stacker(struct workspace
*ws
)
1244 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1245 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1246 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1247 sizeof ws
->stacker
);
1248 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1249 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1250 sizeof ws
->stacker
);
1254 custom_region(char *val
)
1256 unsigned int sidx
, x
, y
, w
, h
;
1258 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1259 errx(1, "invalid custom region, "
1260 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1261 if (sidx
< 1 || sidx
> ScreenCount(display
))
1262 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1263 sidx
, ScreenCount(display
));
1267 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1269 if (x
> DisplayWidth(display
, sidx
) ||
1270 y
> DisplayHeight(display
, sidx
) ||
1271 w
+ x
> DisplayWidth(display
, sidx
) ||
1272 h
+ y
> DisplayHeight(display
, sidx
)) {
1273 warnx("ignoring region %ux%u+%u+%u - not within screen "
1274 "boundaries (%ux%u)", w
, h
, x
, y
,
1275 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
1279 new_region(&screens
[sidx
], x
, y
, w
, h
);
1283 socket_setnonblock(int fd
)
1287 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1288 err(1, "fcntl F_GETFL");
1289 flags
|= O_NONBLOCK
;
1290 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1291 err(1, "fcntl F_SETFL");
1295 bar_print(struct swm_region
*r
, char *s
)
1299 XRectangle ibox
, lbox
;
1301 XClearWindow(display
, r
->bar_window
);
1304 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1306 switch (bar_justify
) {
1307 case SWM_BAR_JUSTIFY_LEFT
:
1310 case SWM_BAR_JUSTIFY_CENTER
:
1311 x
= (WIDTH(r
) - lbox
.width
) / 2;
1313 case SWM_BAR_JUSTIFY_RIGHT
:
1314 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1318 if (x
< SWM_BAR_OFFSET
)
1321 DRAWSTRING(display
, r
->bar_window
, bar_fs
, r
->s
->bar_gc
,
1322 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1327 bar_extra_stop(void)
1331 bzero(bar_pipe
, sizeof bar_pipe
);
1334 kill(bar_pid
, SIGTERM
);
1337 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1342 bar_class_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1344 int do_class
, do_name
;
1346 XClassHint
*xch
= NULL
;
1348 if ((title_name_enabled
== 1 || title_class_enabled
== 1) &&
1349 cur_focus
!= NULL
) {
1350 if ((xch
= XAllocClassHint()) == NULL
)
1352 status
= XGetClassHint(display
, cur_focus
->id
, xch
);
1353 if (status
== BadWindow
|| status
== BadAlloc
)
1355 do_class
= (title_class_enabled
&& xch
->res_class
!= NULL
);
1356 do_name
= (title_name_enabled
&& xch
->res_name
!= NULL
);
1358 strlcat(s
, xch
->res_class
, sz
);
1359 if (do_class
&& do_name
)
1360 strlcat(s
, ":", sz
);
1362 strlcat(s
, xch
->res_name
, sz
);
1363 strlcat(s
, " ", sz
);
1367 XFree(xch
->res_name
);
1368 XFree(xch
->res_class
);
1374 bar_window_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1376 unsigned char *title
;
1378 if (window_name_enabled
&& cur_focus
!= NULL
) {
1379 title
= get_win_name(cur_focus
->id
);
1380 if (title
!= NULL
) {
1381 DNPRINTF(SWM_D_BAR
, "bar_window_name: title: %s\n",
1384 if (cur_focus
->floating
)
1385 strlcat(s
, "(f) ", sz
);
1386 strlcat(s
, (char *)title
, sz
);
1387 strlcat(s
, " ", sz
);
1393 int urgent
[SWM_WS_MAX
];
1395 bar_urgent(char *s
, ssize_t sz
)
1397 XWMHints
*wmh
= NULL
;
1402 if (urgent_enabled
== 0)
1405 for (i
= 0; i
< SWM_WS_MAX
; i
++)
1408 for (i
= 0; i
< ScreenCount(display
); i
++)
1409 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1410 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1411 wmh
= XGetWMHints(display
, win
->id
);
1415 if (wmh
->flags
& XUrgencyHint
)
1420 strlcat(s
, "* ", sz
);
1421 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
1423 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1425 snprintf(b
, sizeof b
, "- ");
1428 strlcat(s
, "* ", sz
);
1436 struct swm_region
*r
;
1439 char ws
[SWM_BAR_MAX
];
1440 char s
[SWM_BAR_MAX
];
1441 unsigned char cn
[SWM_BAR_MAX
];
1442 char loc
[SWM_BAR_MAX
];
1443 char *b
, *stack
= "";
1445 if (bar_enabled
== 0)
1447 if (bar_extra
&& bar_extra_running
) {
1448 /* ignore short reads; it'll correct itself */
1449 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1450 if (b
&& b
[len
- 1] == '\n') {
1452 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1454 if (b
== NULL
&& errno
!= EAGAIN
) {
1455 warn("bar_update: bar_extra failed");
1459 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1461 if (clock_enabled
== 0)
1462 strlcpy(s
, "", sizeof s
);
1465 localtime_r(&tmt
, &tm
);
1466 len
= strftime(s
, sizeof s
, clock_format
, &tm
);
1468 strlcat(s
, " ", sizeof s
);
1471 for (i
= 0; i
< ScreenCount(display
); i
++) {
1473 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1474 strlcpy((char *)cn
, "", sizeof cn
);
1475 strlcpy(ws
, "", sizeof ws
);
1477 bar_urgent((char *)cn
, sizeof cn
);
1478 bar_class_name((char *)cn
, sizeof cn
,
1480 bar_window_name((char *)cn
, sizeof cn
,
1483 snprintf(ws
, sizeof ws
, "<%s>",
1487 stack
= r
->ws
->stacker
;
1489 snprintf(loc
, sizeof loc
, "%d:%d %s %s %s%s %s "
1490 "%s", x
++, r
->ws
->idx
+ 1, stack
, ws
, s
, cn
,
1491 bar_ext
, bar_vertext
);
1499 bar_check_opts(void)
1501 if (title_class_enabled
|| title_name_enabled
|| window_name_enabled
)
1512 bar_toggle(struct swm_region
*r
, union arg
*args
)
1514 struct swm_region
*tmpr
;
1515 int i
, sc
= ScreenCount(display
);
1517 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1520 for (i
= 0; i
< sc
; i
++)
1521 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1522 XUnmapWindow(display
, tmpr
->bar_window
);
1524 for (i
= 0; i
< sc
; i
++)
1525 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1526 XMapRaised(display
, tmpr
->bar_window
);
1528 bar_enabled
= !bar_enabled
;
1531 /* must be after stack */
1538 XSetWindowAttributes wa
;
1539 struct swm_region
*r
;
1542 /* do this here because the conf file is in memory */
1543 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1544 /* launch external status app */
1545 bar_extra_running
= 1;
1546 if (pipe(bar_pipe
) == -1)
1547 err(1, "pipe error");
1548 socket_setnonblock(bar_pipe
[0]);
1549 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1550 if (dup2(bar_pipe
[0], 0) == -1)
1552 if (dup2(bar_pipe
[1], 1) == -1)
1554 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1555 err(1, "could not disable SIGPIPE");
1556 switch (bar_pid
= fork()) {
1558 err(1, "cannot fork");
1562 execvp(bar_argv
[0], bar_argv
);
1563 err(1, "%s external app failed", bar_argv
[0]);
1565 default: /* parent */
1571 bzero(&wa
, sizeof wa
);
1572 for (i
= 0; i
< ScreenCount(display
); i
++)
1573 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1575 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1576 wa
.background_pixel
=
1577 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1578 XChangeWindowAttributes(display
, r
->bar_window
,
1579 CWBackPixel
| CWBorderPixel
, &wa
);
1585 bar_setup(struct swm_region
*r
)
1587 char *default_string
;
1588 char **missing_charsets
;
1589 int num_missing_charsets
= 0;
1593 XFreeFontSet(display
, bar_fs
);
1598 DNPRINTF(SWM_D_BAR
, "bar_setup: loading bar_fonts: %s\n", bar_fonts
);
1600 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
1601 &num_missing_charsets
, &default_string
);
1603 if (num_missing_charsets
> 0) {
1604 warnx("Unable to load charset(s):");
1606 for (i
= 0; i
< num_missing_charsets
; ++i
)
1607 warnx("%s", missing_charsets
[i
]);
1609 XFreeStringList(missing_charsets
);
1611 if (strcmp(default_string
, ""))
1612 warnx("Glyphs from those sets will be replaced "
1613 "by '%s'.", default_string
);
1615 warnx("Glyphs from those sets won't be drawn.");
1619 errx(1, "Error creating font set structure.");
1621 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
1623 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
1624 2 * bar_border_width
;
1630 y
= bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1632 r
->bar_window
= XCreateSimpleWindow(display
,
1633 r
->s
->root
, x
, y
, WIDTH(r
) - 2 * bar_border_width
,
1634 bar_height
- 2 * bar_border_width
,
1635 bar_border_width
, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
1636 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1637 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
1639 XMapRaised(display
, r
->bar_window
);
1640 DNPRINTF(SWM_D_BAR
, "bar_setup: bar_window: 0x%lx\n", r
->bar_window
);
1642 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1643 err(1, "could not install bar_signal");
1648 drain_enter_notify(void)
1653 while (XCheckMaskEvent(display
, EnterWindowMask
, &cne
))
1656 DNPRINTF(SWM_D_MISC
, "drain_enter_notify: drained: %d\n", i
);
1660 set_win_state(struct ws_win
*win
, long state
)
1662 long data
[] = {state
, None
};
1664 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%lx\n", win
->id
);
1669 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1670 (unsigned char *)data
, 2);
1677 unsigned char *p
= NULL
;
1680 if (!get_property(w
, astate
, 2L, astate
, &n
, NULL
, &p
))
1683 result
= *((long *)p
);
1689 version(struct swm_region
*r
, union arg
*args
)
1691 bar_version
= !bar_version
;
1693 snprintf(bar_vertext
, sizeof bar_vertext
,
1694 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
1696 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
1701 client_msg(struct ws_win
*win
, Atom a
)
1703 XClientMessageEvent cm
;
1708 bzero(&cm
, sizeof cm
);
1709 cm
.type
= ClientMessage
;
1710 cm
.window
= win
->id
;
1711 cm
.message_type
= aprot
;
1714 cm
.data
.l
[1] = CurrentTime
;
1715 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
1718 /* synthetic response to a ConfigureRequest when not making a change */
1720 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
1727 /* send notification of unchanged state. */
1728 ce
.type
= ConfigureNotify
;
1731 ce
.width
= WIDTH(win
);
1732 ce
.height
= HEIGHT(win
);
1733 ce
.override_redirect
= False
;
1737 ce
.display
= display
;
1739 ce
.window
= win
->id
;
1740 ce
.border_width
= border_width
;
1744 ce
.display
= ev
->display
;
1745 ce
.event
= ev
->window
;
1746 ce
.window
= ev
->window
;
1748 /* make response appear more WM_SIZE_HINTS-compliant */
1750 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%lx,"
1751 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
1752 "%d x %d\n", win
->id
, win
->sh_mask
, SH_MIN_W(win
),
1753 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
1754 SH_INC_W(win
), SH_INC_H(win
));
1758 /* the hint may be set... to 0! */
1759 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
1760 ce
.width
= SH_MIN_W(win
);
1761 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
1762 ce
.height
= SH_MIN_H(win
);
1767 /* may also be advertized as 0 */
1768 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
1769 ce
.width
= SH_MAX_W(win
);
1770 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
1771 ce
.height
= SH_MAX_H(win
);
1774 /* resize increment. */
1776 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
1777 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
1779 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
1780 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
1784 /* adjust x and y for requested border_width. */
1785 ce
.x
+= border_width
- ev
->border_width
;
1786 ce
.y
+= border_width
- ev
->border_width
;
1787 ce
.border_width
= ev
->border_width
;
1788 ce
.above
= ev
->above
;
1791 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
1792 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
1793 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
1795 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
1799 count_win(struct workspace
*ws
, int count_transient
)
1804 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1805 if (count_transient
== 0 && win
->floating
)
1807 if (count_transient
== 0 && win
->transient
)
1813 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
1819 quit(struct swm_region
*r
, union arg
*args
)
1821 DNPRINTF(SWM_D_MISC
, "quit\n");
1826 unmap_window(struct ws_win
*win
)
1831 /* don't unmap again */
1832 if (getstate(win
->id
) == IconicState
)
1835 set_win_state(win
, IconicState
);
1837 XUnmapWindow(display
, win
->id
);
1838 XSetWindowBorder(display
, win
->id
,
1839 win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1848 for (i
= 0; i
< ScreenCount(display
); i
++)
1849 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1850 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1855 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
1862 event
.display
= display
; /* Ignored, but what the hell */
1863 event
.window
= win
->id
;
1864 event
.root
= win
->s
->root
;
1865 event
.subwindow
= None
;
1866 event
.time
= CurrentTime
;
1871 event
.same_screen
= True
;
1872 event
.keycode
= XKeysymToKeycode(display
, keysym
);
1873 event
.state
= modifiers
;
1875 event
.type
= KeyPress
;
1876 XSendEvent(event
.display
, event
.window
, True
,
1877 KeyPressMask
, (XEvent
*)&event
);
1879 event
.type
= KeyRelease
;
1880 XSendEvent(event
.display
, event
.window
, True
,
1881 KeyPressMask
, (XEvent
*)&event
);
1886 restart(struct swm_region
*r
, union arg
*args
)
1888 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
1890 /* disable alarm because the following code may not be interrupted */
1892 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
1893 err(1, "can't disable alarm");
1898 XCloseDisplay(display
);
1899 execvp(start_argv
[0], start_argv
);
1900 warn("execvp failed");
1905 root_to_region(Window root
)
1907 struct swm_region
*r
= NULL
;
1909 int i
, x
, y
, wx
, wy
;
1912 for (i
= 0; i
< ScreenCount(display
); i
++)
1913 if (screens
[i
].root
== root
)
1916 if (XQueryPointer(display
, screens
[i
].root
,
1917 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
1918 /* choose a region based on pointer location */
1919 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1920 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
1921 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
1926 r
= TAILQ_FIRST(&screens
[i
].rl
);
1932 find_unmanaged_window(Window id
)
1937 for (i
= 0; i
< ScreenCount(display
); i
++)
1938 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1939 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
1947 find_window(Window id
)
1950 Window wrr
, wpr
, *wcr
= NULL
;
1954 for (i
= 0; i
< ScreenCount(display
); i
++)
1955 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1956 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1960 /* if we were looking for the parent return that window instead */
1961 if (XQueryTree(display
, id
, &wrr
, &wpr
, &wcr
, &nc
) == 0)
1966 /* ignore not found and root */
1967 if (wpr
== 0 || wrr
== wpr
)
1970 /* look for parent */
1971 for (i
= 0; i
< ScreenCount(display
); i
++)
1972 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1973 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1981 spawn(int ws_idx
, union arg
*args
, int close_fd
)
1986 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
1989 close(ConnectionNumber(display
));
1991 setenv("LD_PRELOAD", SWM_LIB
, 1);
1993 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
1994 warn("spawn: asprintf SWM_WS");
1997 setenv("_SWM_WS", ret
, 1);
2001 if (asprintf(&ret
, "%d", getpid()) == -1) {
2002 warn("spawn: asprintf _SWM_PID");
2005 setenv("_SWM_PID", ret
, 1);
2009 if (setsid() == -1) {
2010 warn("spawn: setsid");
2016 * close stdin and stdout to prevent interaction between apps
2017 * and the baraction script
2018 * leave stderr open to record errors
2020 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2021 warn("spawn: open");
2024 dup2(fd
, STDIN_FILENO
);
2025 dup2(fd
, STDOUT_FILENO
);
2030 execvp(args
->argv
[0], args
->argv
);
2032 warn("spawn: execvp");
2037 spawnterm(struct swm_region
*r
, union arg
*args
)
2039 DNPRINTF(SWM_D_MISC
, "spawnterm\n");
2043 setenv("_SWM_XTERM_FONTADJ", "", 1);
2044 spawn(r
->ws
->idx
, args
, 1);
2049 kill_refs(struct ws_win
*win
)
2052 struct swm_region
*r
;
2053 struct workspace
*ws
;
2058 for (i
= 0; i
< ScreenCount(display
); i
++)
2059 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2060 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
2062 if (win
== ws
->focus
)
2064 if (win
== ws
->focus_prev
)
2065 ws
->focus_prev
= NULL
;
2070 validate_win(struct ws_win
*testwin
)
2073 struct workspace
*ws
;
2074 struct swm_region
*r
;
2077 if (testwin
== NULL
)
2080 for (i
= 0; i
< ScreenCount(display
); i
++)
2081 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2082 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
2084 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2092 validate_ws(struct workspace
*testws
)
2094 struct swm_region
*r
;
2095 struct workspace
*ws
;
2098 /* validate all ws */
2099 for (i
= 0; i
< ScreenCount(display
); i
++)
2100 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2101 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
2110 unfocus_win(struct ws_win
*win
)
2115 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%lx\n", WINID(win
));
2119 if (win
->ws
== NULL
)
2122 if (validate_ws(win
->ws
))
2123 return; /* XXX this gets hit with thunderbird, needs fixing */
2125 if (win
->ws
->r
== NULL
)
2128 if (validate_win(win
)) {
2133 if (win
->ws
->focus
== win
) {
2134 win
->ws
->focus
= NULL
;
2135 win
->ws
->focus_prev
= win
;
2138 if (validate_win(win
->ws
->focus
)) {
2139 kill_refs(win
->ws
->focus
);
2140 win
->ws
->focus
= NULL
;
2142 if (validate_win(win
->ws
->focus_prev
)) {
2143 kill_refs(win
->ws
->focus_prev
);
2144 win
->ws
->focus_prev
= NULL
;
2147 /* drain all previous unfocus events */
2148 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
2151 grabbuttons(win
, 0);
2152 XSetWindowBorder(display
, win
->id
,
2153 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2155 XChangeProperty(display
, win
->s
->root
,
2156 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2157 PropModeReplace
, (unsigned char *)&none
,1);
2166 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2168 for (i
= 0; i
< ScreenCount(display
); i
++)
2169 for (j
= 0; j
< SWM_WS_MAX
; j
++)
2170 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2175 focus_win(struct ws_win
*win
)
2180 struct ws_win
*cfw
= NULL
;
2183 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%lx\n", WINID(win
));
2187 if (win
->ws
== NULL
)
2190 if (validate_ws(win
->ws
))
2191 return; /* XXX this gets hit with thunderbird, needs fixing */
2193 if (validate_win(win
)) {
2198 if (validate_win(win
)) {
2203 XGetInputFocus(display
, &cur_focus
, &rr
);
2204 if ((cfw
= find_window(cur_focus
)) != NULL
)
2207 /* use larger hammer since the window was killed somehow */
2208 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2209 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2210 XSetWindowBorder(display
, cfw
->id
,
2211 cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2214 win
->ws
->focus
= win
;
2216 if (win
->ws
->r
!= NULL
) {
2217 /* drain all previous focus events */
2218 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
2222 XSetInputFocus(display
, win
->id
,
2223 RevertToParent
, CurrentTime
);
2224 grabbuttons(win
, 1);
2225 XSetWindowBorder(display
, win
->id
,
2226 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2227 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2228 win
->ws
->always_raise
)
2229 XMapRaised(display
, win
->id
);
2231 XChangeProperty(display
, win
->s
->root
,
2232 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2233 PropModeReplace
, (unsigned char *)&win
->id
,1);
2240 switchws(struct swm_region
*r
, union arg
*args
)
2242 int wsid
= args
->id
, unmap_old
= 0;
2243 struct swm_region
*this_r
, *other_r
;
2245 struct workspace
*new_ws
, *old_ws
;
2252 old_ws
= this_r
->ws
;
2253 new_ws
= &this_r
->s
->ws
[wsid
];
2255 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2256 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2258 if (new_ws
== NULL
|| old_ws
== NULL
)
2260 if (new_ws
== old_ws
)
2263 other_r
= new_ws
->r
;
2264 if (other_r
== NULL
) {
2265 /* the other workspace is hidden, hide this one */
2269 /* the other ws is visible in another region, exchange them */
2270 other_r
->ws_prior
= new_ws
;
2271 other_r
->ws
= old_ws
;
2272 old_ws
->r
= other_r
;
2274 this_r
->ws_prior
= old_ws
;
2275 this_r
->ws
= new_ws
;
2278 /* this is needed so that we can click on a window after a restart */
2282 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2283 focus(new_ws
->r
, &a
);
2287 /* unmap old windows */
2289 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2292 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2293 drain_enter_notify();
2297 cyclews(struct swm_region
*r
, union arg
*args
)
2300 struct swm_screen
*s
= r
->s
;
2303 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2304 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2309 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2312 case SWM_ARG_ID_CYCLEWS_UP
:
2313 if (a
.id
< SWM_WS_MAX
- 1)
2318 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2321 case SWM_ARG_ID_CYCLEWS_DOWN
:
2325 a
.id
= SWM_WS_MAX
- 1;
2332 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2334 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2338 } while (a
.id
!= r
->ws
->idx
);
2342 priorws(struct swm_region
*r
, union arg
*args
)
2346 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2347 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2349 if (r
->ws_prior
== NULL
)
2352 a
.id
= r
->ws_prior
->idx
;
2357 cyclescr(struct swm_region
*r
, union arg
*args
)
2359 struct swm_region
*rr
= NULL
;
2363 /* do nothing if we don't have more than one screen */
2364 if (!(ScreenCount(display
) > 1 || outputs
> 1))
2369 case SWM_ARG_ID_CYCLESC_UP
:
2370 rr
= TAILQ_NEXT(r
, entry
);
2372 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2374 case SWM_ARG_ID_CYCLESC_DOWN
:
2375 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2377 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2385 /* move mouse to region */
2387 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2388 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2390 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2393 if (rr
->ws
->focus
) {
2394 /* move to focus window */
2395 x
= X(rr
->ws
->focus
) + 1;
2396 y
= Y(rr
->ws
->focus
) + 1;
2397 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2402 sort_windows(struct ws_win_list
*wl
)
2404 struct ws_win
*win
, *parent
, *nxt
;
2409 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2410 nxt
= TAILQ_NEXT(win
, entry
);
2411 if (win
->transient
) {
2412 parent
= find_window(win
->transient
);
2413 if (parent
== NULL
) {
2414 warnx("not possible bug");
2417 TAILQ_REMOVE(wl
, win
, entry
);
2418 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2425 swapwin(struct swm_region
*r
, union arg
*args
)
2427 struct ws_win
*target
, *source
;
2428 struct ws_win
*cur_focus
;
2429 struct ws_win_list
*wl
;
2432 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2433 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2435 cur_focus
= r
->ws
->focus
;
2436 if (cur_focus
== NULL
)
2440 wl
= &source
->ws
->winlist
;
2443 case SWM_ARG_ID_SWAPPREV
:
2444 if (source
->transient
)
2445 source
= find_window(source
->transient
);
2446 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2447 if (target
&& target
->transient
)
2448 target
= find_window(target
->transient
);
2449 TAILQ_REMOVE(wl
, source
, entry
);
2451 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2453 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2455 case SWM_ARG_ID_SWAPNEXT
:
2456 target
= TAILQ_NEXT(source
, entry
);
2457 /* move the parent and let the sort handle the move */
2458 if (source
->transient
)
2459 source
= find_window(source
->transient
);
2460 TAILQ_REMOVE(wl
, source
, entry
);
2462 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2464 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2466 case SWM_ARG_ID_SWAPMAIN
:
2467 target
= TAILQ_FIRST(wl
);
2468 if (target
== source
) {
2469 if (source
->ws
->focus_prev
!= NULL
&&
2470 source
->ws
->focus_prev
!= target
)
2472 source
= source
->ws
->focus_prev
;
2476 if (target
== NULL
|| source
== NULL
)
2478 source
->ws
->focus_prev
= target
;
2479 TAILQ_REMOVE(wl
, target
, entry
);
2480 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2481 TAILQ_REMOVE(wl
, source
, entry
);
2482 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2484 case SWM_ARG_ID_MOVELAST
:
2485 TAILQ_REMOVE(wl
, source
, entry
);
2486 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2489 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2499 focus_prev(struct ws_win
*win
)
2501 struct ws_win
*winfocus
= NULL
;
2502 struct ws_win
*cur_focus
= NULL
;
2503 struct ws_win_list
*wl
= NULL
;
2504 struct workspace
*ws
= NULL
;
2506 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%lx\n", WINID(win
));
2508 if (!(win
&& win
->ws
))
2513 cur_focus
= ws
->focus
;
2515 /* pickle, just focus on whatever */
2516 if (cur_focus
== NULL
) {
2517 /* use prev_focus if valid */
2518 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2519 find_window(WINID(ws
->focus_prev
)))
2520 winfocus
= ws
->focus_prev
;
2521 if (winfocus
== NULL
)
2522 winfocus
= TAILQ_FIRST(wl
);
2526 /* if transient focus on parent */
2527 if (cur_focus
->transient
) {
2528 winfocus
= find_window(cur_focus
->transient
);
2532 /* if in max_stack try harder */
2533 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2534 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2535 if (cur_focus
!= ws
->focus_prev
)
2536 winfocus
= ws
->focus_prev
;
2537 else if (cur_focus
!= ws
->focus
)
2538 winfocus
= ws
->focus
;
2540 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2545 if (cur_focus
== win
)
2546 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2547 if (winfocus
== NULL
)
2548 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2549 if (winfocus
== NULL
|| winfocus
== win
)
2550 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2553 focus_magic(winfocus
);
2557 focus(struct swm_region
*r
, union arg
*args
)
2559 struct ws_win
*winfocus
= NULL
, *head
;
2560 struct ws_win
*cur_focus
= NULL
;
2561 struct ws_win_list
*wl
= NULL
;
2562 struct workspace
*ws
= NULL
;
2568 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2570 /* treat FOCUS_CUR special */
2571 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2572 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2573 winfocus
= r
->ws
->focus
;
2574 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2575 winfocus
= r
->ws
->focus_prev
;
2577 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2578 if (winfocus
->iconic
== 0)
2581 focus_magic(winfocus
);
2585 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2589 if (TAILQ_EMPTY(wl
))
2591 /* make sure there is at least one uniconified window */
2593 TAILQ_FOREACH(winfocus
, wl
, entry
)
2594 if (winfocus
->iconic
== 0) {
2602 case SWM_ARG_ID_FOCUSPREV
:
2603 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2605 head
= TAILQ_LAST(wl
, ws_win_list
);
2607 if (WINID(winfocus
) == cur_focus
->transient
) {
2608 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2610 head
= TAILQ_LAST(wl
, ws_win_list
);
2615 if (winfocus
&& winfocus
->iconic
) {
2616 while (winfocus
!= cur_focus
) {
2617 if (winfocus
== NULL
)
2618 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2619 if (winfocus
->iconic
== 0)
2621 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
2627 case SWM_ARG_ID_FOCUSNEXT
:
2628 head
= TAILQ_NEXT(cur_focus
, entry
);
2630 head
= TAILQ_FIRST(wl
);
2634 if (winfocus
&& winfocus
->iconic
) {
2635 while (winfocus
!= cur_focus
) {
2636 if (winfocus
== NULL
)
2637 winfocus
= TAILQ_FIRST(wl
);
2638 if (winfocus
->iconic
== 0)
2640 winfocus
= TAILQ_NEXT(winfocus
, entry
);
2645 case SWM_ARG_ID_FOCUSMAIN
:
2646 winfocus
= TAILQ_FIRST(wl
);
2647 if (winfocus
== cur_focus
)
2648 winfocus
= cur_focus
->ws
->focus_prev
;
2655 focus_magic(winfocus
);
2659 cycle_layout(struct swm_region
*r
, union arg
*args
)
2661 struct workspace
*ws
= r
->ws
;
2664 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2667 if (ws
->cur_layout
->l_stack
== NULL
)
2668 ws
->cur_layout
= &layouts
[0];
2671 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2672 drain_enter_notify();
2673 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2679 stack_config(struct swm_region
*r
, union arg
*args
)
2681 struct workspace
*ws
= r
->ws
;
2683 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
2686 if (ws
->cur_layout
->l_config
!= NULL
)
2687 ws
->cur_layout
->l_config(ws
, args
->id
);
2689 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
2696 struct swm_geometry g
;
2697 struct swm_region
*r
;
2703 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
2705 for (i
= 0; i
< ScreenCount(display
); i
++) {
2709 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2710 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
2711 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
2713 /* start with screen geometry, adjust for bar */
2715 g
.w
-= 2 * border_width
;
2716 g
.h
-= 2 * border_width
;
2722 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
2723 r
->ws
->cur_layout
->l_string(r
->ws
);
2724 /* save r so we can track region changes */
2731 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2732 drain_enter_notify();
2734 DNPRINTF(SWM_D_STACK
, "stack: end\n");
2738 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
2740 /* retain window geom and region geom */
2741 win
->g_float
= win
->g
;
2742 win
->rg_float
= r
->g
;
2743 win
->g_floatvalid
= 1;
2747 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
2755 bzero(&wc
, sizeof wc
);
2756 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
2759 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2760 * geom on ws switches or return from max mode
2762 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&& win
->g_floatvalid
2763 && !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
2765 * use stored g and rg to set relative position and size
2766 * as in old region or before max stack mode
2768 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ X(r
);
2769 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ Y(r
);
2770 WIDTH(win
) = win
->g_float
.w
;
2771 HEIGHT(win
) = win
->g_float
.h
;
2772 win
->g_floatvalid
= 0;
2775 win
->floatmaxed
= 0;
2777 if ((win
->quirks
& SWM_Q_FULLSCREEN
) && (WIDTH(win
) >= WIDTH(r
)) &&
2778 (HEIGHT(win
) >= HEIGHT(r
)))
2779 wc
.border_width
= 0;
2781 wc
.border_width
= border_width
;
2782 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
2783 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
2784 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
2789 * floaters and transients are auto-centred unless moved
2792 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - wc
.border_width
;
2793 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - wc
.border_width
;
2796 /* win can be outside r if new r smaller than old r */
2797 /* Ensure top left corner inside r (move probs otherwise) */
2798 if (X(win
) < X(r
) - wc
.border_width
)
2799 X(win
) = X(r
) - wc
.border_width
;
2800 if (X(win
) > X(r
) + WIDTH(r
) - 1)
2801 X(win
) = (WIDTH(win
) > WIDTH(r
)) ? X(r
) :
2802 (X(r
) + WIDTH(r
) - WIDTH(win
) - 2 * wc
.border_width
);
2803 if (Y(win
) < Y(r
) - wc
.border_width
)
2804 Y(win
) = Y(r
) - wc
.border_width
;
2805 if (Y(win
) > Y(r
) + HEIGHT(r
) - 1)
2806 Y(win
) = (HEIGHT(win
) > HEIGHT(r
)) ? Y(r
) :
2807 (Y(r
) + HEIGHT(r
) - HEIGHT(win
) - 2 * wc
.border_width
);
2811 wc
.width
= WIDTH(win
);
2812 wc
.height
= HEIGHT(win
);
2815 * Retain floater and transient geometry for correct positioning
2816 * when ws changes region
2818 if (!(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
2819 store_float_geom(win
, r
);
2821 DNPRINTF(SWM_D_MISC
, "stack_floater: window: %lu, (x,y) w x h: (%d,%d) "
2822 "%d x %d\n", win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
2824 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2828 * Send keystrokes to terminal to decrease/increase the font size as the
2829 * window size changes.
2832 adjust_font(struct ws_win
*win
)
2834 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
2835 win
->floating
|| win
->transient
)
2838 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
2839 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
2840 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
2841 win
->font_size_boundary
[win
->font_steps
] =
2842 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
2845 win
->last_inc
= win
->sh
.width_inc
;
2846 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
2847 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
2848 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
2851 win
->last_inc
= win
->sh
.width_inc
;
2852 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
2856 #define SWAPXY(g) do { \
2858 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2859 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2862 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
2865 XWindowAttributes wa
;
2866 struct swm_geometry win_g
, r_g
= *g
;
2867 struct ws_win
*win
, *fs_win
= 0;
2868 int i
, j
, s
, stacks
;
2869 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
2870 int hrh
, extra
= 0, h_slice
, last_h
= 0;
2871 int split
, colno
, winno
, mwin
, msize
, mscale
;
2872 int remain
, missing
, v_slice
, reconfigure
;
2875 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
2876 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
2878 winno
= count_win(ws
, 0);
2879 if (winno
== 0 && count_win(ws
, 1) == 0)
2882 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2883 if (win
->transient
== 0 && win
->floating
== 0
2884 && win
->iconic
== 0)
2891 w_inc
= win
->sh
.width_inc
;
2892 w_base
= win
->sh
.base_width
;
2893 mwin
= ws
->l_state
.horizontal_mwin
;
2894 mscale
= ws
->l_state
.horizontal_msize
;
2895 stacks
= ws
->l_state
.horizontal_stacks
;
2898 w_inc
= win
->sh
.height_inc
;
2899 w_base
= win
->sh
.base_height
;
2900 mwin
= ws
->l_state
.vertical_mwin
;
2901 mscale
= ws
->l_state
.vertical_msize
;
2902 stacks
= ws
->l_state
.vertical_stacks
;
2906 if (stacks
> winno
- mwin
)
2907 stacks
= winno
- mwin
;
2911 h_slice
= r_g
.h
/ SWM_H_SLICE
;
2912 if (mwin
&& winno
> mwin
) {
2913 v_slice
= r_g
.w
/ SWM_V_SLICE
;
2917 win_g
.w
= v_slice
* mscale
;
2919 if (w_inc
> 1 && w_inc
< v_slice
) {
2920 /* adjust for window's requested size increment */
2921 remain
= (win_g
.w
- w_base
) % w_inc
;
2922 missing
= w_inc
- remain
;
2929 win_g
.x
+= r_g
.w
- msize
;
2932 colno
= split
= winno
/ stacks
;
2933 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
2934 2 * border_width
) / stacks
);
2936 hrh
= r_g
.h
/ colno
;
2937 extra
= r_g
.h
- (colno
* hrh
);
2938 win_g
.h
= hrh
- 2 * border_width
;
2940 /* stack all the tiled windows */
2941 i
= j
= 0, s
= stacks
;
2942 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2943 if (win
->transient
!= 0 || win
->floating
!= 0)
2945 if (win
->iconic
!= 0)
2948 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
2953 if (split
&& i
== split
) {
2954 colno
= (winno
- mwin
) / stacks
;
2955 if (s
<= (winno
- mwin
) % stacks
)
2957 split
= split
+ colno
;
2958 hrh
= (r_g
.h
/ colno
);
2959 extra
= r_g
.h
- (colno
* hrh
);
2963 win_g
.x
+= win_g
.w
+ 2 * border_width
;
2964 win_g
.w
= (r_g
.w
- msize
-
2965 (stacks
* 2 * border_width
)) / stacks
;
2967 win_g
.w
+= (r_g
.w
- msize
-
2968 (stacks
* 2 * border_width
)) % stacks
;
2972 win_g
.h
= hrh
- 2 * border_width
;
2974 h_inc
= win
->sh
.width_inc
;
2975 h_base
= win
->sh
.base_width
;
2977 h_inc
= win
->sh
.height_inc
;
2978 h_base
= win
->sh
.base_height
;
2980 if (j
== colno
- 1) {
2981 win_g
.h
= hrh
+ extra
;
2982 } else if (h_inc
> 1 && h_inc
< h_slice
) {
2983 /* adjust for window's requested size increment */
2984 remain
= (win_g
.h
- h_base
) % h_inc
;
2985 missing
= h_inc
- remain
;
2987 if (missing
<= extra
|| j
== 0) {
2999 win_g
.y
+= last_h
+ 2 * border_width
;
3001 bzero(&wc
, sizeof wc
);
3002 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3003 wc
.border_width
= 0;
3004 win_g
.w
+= 2 * border_width
;
3005 win_g
.h
+= 2 * border_width
;
3007 wc
.border_width
= border_width
;
3010 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3011 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3013 X(win
) = wc
.x
= win_g
.y
;
3014 Y(win
) = wc
.y
= win_g
.x
;
3015 WIDTH(win
) = wc
.width
= win_g
.h
;
3016 HEIGHT(win
) = wc
.height
= win_g
.w
;
3019 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3020 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3022 X(win
) = wc
.x
= win_g
.x
;
3023 Y(win
) = wc
.y
= win_g
.y
;
3024 WIDTH(win
) = wc
.width
= win_g
.w
;
3025 HEIGHT(win
) = wc
.height
= win_g
.h
;
3030 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
3031 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3034 if (XGetWindowAttributes(display
, win
->id
, &wa
))
3035 if (wa
.map_state
== IsUnmapped
)
3036 XMapRaised(display
, win
->id
);
3044 /* now, stack all the floaters and transients */
3045 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3046 if (win
->transient
== 0 && win
->floating
== 0)
3048 if (win
->iconic
== 1)
3050 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3055 stack_floater(win
, ws
->r
);
3056 XMapRaised(display
, win
->id
);
3060 stack_floater(fs_win
, ws
->r
);
3061 XMapRaised(display
, fs_win
->id
);
3066 vertical_config(struct workspace
*ws
, int id
)
3068 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3072 case SWM_ARG_ID_STACKRESET
:
3073 case SWM_ARG_ID_STACKINIT
:
3074 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3075 ws
->l_state
.vertical_mwin
= 1;
3076 ws
->l_state
.vertical_stacks
= 1;
3078 case SWM_ARG_ID_MASTERSHRINK
:
3079 if (ws
->l_state
.vertical_msize
> 1)
3080 ws
->l_state
.vertical_msize
--;
3082 case SWM_ARG_ID_MASTERGROW
:
3083 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3084 ws
->l_state
.vertical_msize
++;
3086 case SWM_ARG_ID_MASTERADD
:
3087 ws
->l_state
.vertical_mwin
++;
3089 case SWM_ARG_ID_MASTERDEL
:
3090 if (ws
->l_state
.vertical_mwin
> 0)
3091 ws
->l_state
.vertical_mwin
--;
3093 case SWM_ARG_ID_STACKINC
:
3094 ws
->l_state
.vertical_stacks
++;
3096 case SWM_ARG_ID_STACKDEC
:
3097 if (ws
->l_state
.vertical_stacks
> 1)
3098 ws
->l_state
.vertical_stacks
--;
3100 case SWM_ARG_ID_FLIPLAYOUT
:
3101 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3109 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3111 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3113 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3117 horizontal_config(struct workspace
*ws
, int id
)
3119 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3122 case SWM_ARG_ID_STACKRESET
:
3123 case SWM_ARG_ID_STACKINIT
:
3124 ws
->l_state
.horizontal_mwin
= 1;
3125 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3126 ws
->l_state
.horizontal_stacks
= 1;
3128 case SWM_ARG_ID_MASTERSHRINK
:
3129 if (ws
->l_state
.horizontal_msize
> 1)
3130 ws
->l_state
.horizontal_msize
--;
3132 case SWM_ARG_ID_MASTERGROW
:
3133 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3134 ws
->l_state
.horizontal_msize
++;
3136 case SWM_ARG_ID_MASTERADD
:
3137 ws
->l_state
.horizontal_mwin
++;
3139 case SWM_ARG_ID_MASTERDEL
:
3140 if (ws
->l_state
.horizontal_mwin
> 0)
3141 ws
->l_state
.horizontal_mwin
--;
3143 case SWM_ARG_ID_STACKINC
:
3144 ws
->l_state
.horizontal_stacks
++;
3146 case SWM_ARG_ID_STACKDEC
:
3147 if (ws
->l_state
.horizontal_stacks
> 1)
3148 ws
->l_state
.horizontal_stacks
--;
3150 case SWM_ARG_ID_FLIPLAYOUT
:
3151 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3159 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3161 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3163 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3166 /* fullscreen view */
3168 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3171 struct swm_geometry gg
= *g
;
3172 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3176 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3181 winno
= count_win(ws
, 0);
3182 if (winno
== 0 && count_win(ws
, 1) == 0)
3185 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3186 if (win
->transient
) {
3188 parent
= find_window(win
->transient
);
3192 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3194 * retain geometry for retrieval on exit from
3197 store_float_geom(win
, ws
->r
);
3198 win
->floatmaxed
= 1;
3201 /* only reconfigure if necessary */
3202 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3203 HEIGHT(win
) != gg
.h
) {
3204 bzero(&wc
, sizeof wc
);
3205 X(win
) = wc
.x
= gg
.x
;
3206 Y(win
) = wc
.y
= gg
.y
;
3208 wc
.border_width
= border_width
;
3209 WIDTH(win
) = wc
.width
= gg
.w
;
3210 HEIGHT(win
) = wc
.height
= gg
.h
;
3212 wc
.border_width
= 0;
3213 WIDTH(win
) = wc
.width
= gg
.w
+ 2 * border_width
;
3214 HEIGHT(win
) = wc
.height
= gg
.h
+
3217 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
3218 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3220 /* unmap only if we don't have multi screen */
3221 if (win
!= ws
->focus
)
3222 if (!(ScreenCount(display
) > 1 || outputs
> 1))
3226 /* put the last transient on top */
3229 XMapRaised(display
, parent
->id
);
3230 stack_floater(wintrans
, ws
->r
);
3231 focus_magic(wintrans
);
3236 send_to_ws(struct swm_region
*r
, union arg
*args
)
3238 int wsid
= args
->id
;
3239 struct ws_win
*win
= NULL
, *parent
;
3240 struct workspace
*ws
, *nws
;
3241 Atom ws_idx_atom
= 0;
3242 unsigned char ws_idx_str
[SWM_PROPLEN
];
3245 if (r
&& r
->ws
&& r
->ws
->focus
)
3251 if (win
->ws
->idx
== wsid
)
3254 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%lx\n", win
->id
);
3257 nws
= &win
->s
->ws
[wsid
];
3259 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3261 if (win
->transient
) {
3262 parent
= find_window(win
->transient
);
3264 unmap_window(parent
);
3265 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3266 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3271 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3272 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3273 if (TAILQ_EMPTY(&ws
->winlist
))
3274 r
->ws
->focus
= NULL
;
3277 /* Try to update the window's workspace property */
3278 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
3280 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3282 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3284 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
3285 PropModeReplace
, ws_idx_str
, strlen((char *)ws_idx_str
));
3292 pressbutton(struct swm_region
*r
, union arg
*args
)
3294 XTestFakeButtonEvent(display
, args
->id
, True
, CurrentTime
);
3295 XTestFakeButtonEvent(display
, args
->id
, False
, CurrentTime
);
3299 raise_toggle(struct swm_region
*r
, union arg
*args
)
3301 if (r
&& r
->ws
== NULL
)
3304 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3306 /* bring floaters back to top */
3307 if (r
->ws
->always_raise
== 0)
3312 iconify(struct swm_region
*r
, union arg
*args
)
3316 if (r
->ws
->focus
== NULL
)
3318 unmap_window(r
->ws
->focus
);
3319 update_iconic(r
->ws
->focus
, 1);
3321 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3322 drain_enter_notify();
3323 r
->ws
->focus
= NULL
;
3324 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3329 get_win_name(Window win
)
3331 unsigned char *prop
= NULL
;
3332 unsigned long nbytes
, nitems
;
3334 /* try _NET_WM_NAME first */
3335 if (get_property(win
, a_netwmname
, 0L, a_utf8_string
, NULL
, &nbytes
,
3338 if (get_property(win
, a_netwmname
, nbytes
, a_utf8_string
,
3339 &nitems
, NULL
, &prop
))
3343 /* fallback to WM_NAME */
3344 if (!get_property(win
, a_wmname
, 0L, a_string
, NULL
, &nbytes
, &prop
))
3347 if (get_property(win
, a_wmname
, nbytes
, a_string
, &nitems
, NULL
, &prop
))
3354 uniconify(struct swm_region
*r
, union arg
*args
)
3358 unsigned char *name
;
3361 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3363 if (r
&& r
->ws
== NULL
)
3366 /* make sure we have anything to uniconify */
3367 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3368 if (win
->ws
== NULL
)
3369 continue; /* should never happen */
3370 if (win
->iconic
== 0)
3378 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3380 spawn_select(r
, args
, "search", &searchpid
);
3382 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3385 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3386 if (win
->ws
== NULL
)
3387 continue; /* should never happen */
3388 if (win
->iconic
== 0)
3391 name
= get_win_name(win
->id
);
3394 fprintf(lfile
, "%s.%lu\n", name
, win
->id
);
3402 name_workspace(struct swm_region
*r
, union arg
*args
)
3406 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3412 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3414 spawn_select(r
, args
, "name_workspace", &searchpid
);
3416 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3419 fprintf(lfile
, "%s", "");
3424 search_workspace(struct swm_region
*r
, union arg
*args
)
3427 struct workspace
*ws
;
3430 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3436 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3438 spawn_select(r
, args
, "search", &searchpid
);
3440 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3443 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
3447 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3448 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3455 search_win_cleanup(void)
3457 struct search_window
*sw
= NULL
;
3459 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3460 XDestroyWindow(display
, sw
->indicator
);
3461 XFreeGC(display
, sw
->gc
);
3462 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3468 search_win(struct swm_region
*r
, union arg
*args
)
3470 struct ws_win
*win
= NULL
;
3471 struct search_window
*sw
= NULL
;
3478 XRectangle ibox
, lbox
;
3480 DNPRINTF(SWM_D_MISC
, "search_win\n");
3483 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3485 spawn_select(r
, args
, "search", &searchpid
);
3487 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3490 TAILQ_INIT(&search_wl
);
3493 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3494 if (win
->iconic
== 1)
3497 sw
= calloc(1, sizeof(struct search_window
));
3499 warn("search_win: calloc");
3501 search_win_cleanup();
3507 snprintf(s
, sizeof s
, "%d", i
);
3510 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
3512 w
= XCreateSimpleWindow(display
,
3513 win
->id
, 0, 0,lbox
.width
+ 4,
3514 bar_fs_extents
->max_logical_extent
.height
, 1,
3515 r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
,
3516 r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
3519 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3521 sw
->gc
= XCreateGC(display
, w
, 0, &gcv
);
3522 XMapRaised(display
, w
);
3523 XSetForeground(display
, sw
->gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
3525 DRAWSTRING(display
, w
, bar_fs
, sw
->gc
, 2,
3526 (bar_fs_extents
->max_logical_extent
.height
-
3527 lbox
.height
) / 2 - lbox
.y
, s
, len
);
3529 fprintf(lfile
, "%d\n", i
);
3537 search_resp_uniconify(char *resp
, unsigned long len
)
3539 unsigned char *name
;
3543 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3545 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3546 if (win
->iconic
== 0)
3548 name
= get_win_name(win
->id
);
3551 if (asprintf(&s
, "%s.%lu", name
, win
->id
) == -1) {
3556 if (strncmp(s
, resp
, len
) == 0) {
3557 /* XXX this should be a callback to generalize */
3558 update_iconic(win
, 0);
3567 search_resp_name_workspace(char *resp
, unsigned long len
)
3569 struct workspace
*ws
;
3571 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3573 if (search_r
->ws
== NULL
)
3578 free(search_r
->ws
->name
);
3579 search_r
->ws
->name
= NULL
;
3583 ws
->name
= strdup(resp
);
3584 if (ws
->name
== NULL
) {
3585 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
3586 "strdup: %s", strerror(errno
));
3593 search_resp_search_workspace(char *resp
, unsigned long len
)
3600 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
3604 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
3611 ws_idx
= strtonum(q
, 1, SWM_WS_MAX
, &errstr
);
3613 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
3620 switchws(search_r
, &a
);
3624 search_resp_search_window(char *resp
, unsigned long len
)
3629 struct search_window
*sw
;
3631 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
3635 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
3640 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
3642 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
3649 TAILQ_FOREACH(sw
, &search_wl
, entry
)
3650 if (idx
== sw
->idx
) {
3656 #define MAX_RESP_LEN 1024
3659 search_do_resp(void)
3665 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
3670 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
3671 warn("search: calloc");
3675 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
3677 warn("search: read error");
3680 resp
[rbytes
] = '\0';
3683 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3684 * newline, so work around that by sanitizing the resp now.
3686 resp
[strcspn(resp
, "\n")] = '\0';
3689 switch (search_resp_action
) {
3690 case SWM_SEARCH_UNICONIFY
:
3691 search_resp_uniconify(resp
, len
);
3693 case SWM_SEARCH_NAME_WORKSPACE
:
3694 search_resp_name_workspace(resp
, len
);
3696 case SWM_SEARCH_SEARCH_WORKSPACE
:
3697 search_resp_search_workspace(resp
, len
);
3699 case SWM_SEARCH_SEARCH_WINDOW
:
3700 search_resp_search_window(resp
, len
);
3705 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
3706 search_win_cleanup();
3708 search_resp_action
= SWM_SEARCH_NONE
;
3709 close(select_resp_pipe
[0]);
3714 wkill(struct swm_region
*r
, union arg
*args
)
3716 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
3718 if (r
->ws
->focus
== NULL
)
3721 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
3722 XKillClient(display
, r
->ws
->focus
->id
);
3724 if (r
->ws
->focus
->can_delete
)
3725 client_msg(r
->ws
->focus
, adelete
);
3730 floating_toggle_win(struct ws_win
*win
)
3732 struct swm_region
*r
;
3742 /* reject floating toggles in max stack mode */
3743 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3746 if (win
->floating
) {
3747 if (!win
->floatmaxed
) {
3748 /* retain position for refloat */
3749 store_float_geom(win
, r
);
3753 if (win
->g_floatvalid
) {
3754 /* refloat at last floating relative position */
3755 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ X(r
);
3756 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ Y(r
);
3757 WIDTH(win
) = win
->g_float
.w
;
3758 HEIGHT(win
) = win
->g_float
.h
;
3763 ewmh_update_actions(win
);
3769 floating_toggle(struct swm_region
*r
, union arg
*args
)
3771 struct ws_win
*win
= r
->ws
->focus
;
3777 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
3778 _NET_WM_STATE_TOGGLE
);
3781 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3782 drain_enter_notify();
3784 if (win
== win
->ws
->focus
) {
3785 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3786 focus(win
->ws
->r
, &a
);
3791 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
3793 if (X(win
) + WIDTH(win
) > X(r
) + WIDTH(r
) - border_width
) {
3795 WIDTH(win
) = X(r
) + WIDTH(r
) - X(win
) - border_width
;
3797 X(win
) = X(r
) + WIDTH(r
) - WIDTH(win
) - border_width
;
3800 if (X(win
) < X(r
) - border_width
) {
3802 WIDTH(win
) -= X(r
) - X(win
) - border_width
;
3804 X(win
) = X(r
) - border_width
;
3807 if (Y(win
) + HEIGHT(win
) > Y(r
) + HEIGHT(r
) - border_width
) {
3809 HEIGHT(win
) = Y(r
) + HEIGHT(r
) - Y(win
) - border_width
;
3811 Y(win
) = Y(r
) + HEIGHT(r
) - HEIGHT(win
) - border_width
;
3814 if (Y(win
) < Y(r
) - border_width
) {
3816 HEIGHT(win
) -= Y(r
) - Y(win
) - border_width
;
3818 Y(win
) = Y(r
) - border_width
;
3823 if (HEIGHT(win
) < 1)
3828 update_window(struct ws_win
*win
)
3833 bzero(&wc
, sizeof wc
);
3834 mask
= CWBorderWidth
| CWWidth
| CWHeight
| CWX
| CWY
;
3835 wc
.border_width
= border_width
;
3838 wc
.width
= WIDTH(win
);
3839 wc
.height
= HEIGHT(win
);
3841 DNPRINTF(SWM_D_MISC
, "update_window: window: 0x%lx, (x,y) w x h: "
3842 "(%d,%d) %d x %d\n", win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
3844 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3847 #define SWM_RESIZE_STEPS (50)
3850 resize(struct ws_win
*win
, union arg
*args
)
3854 struct swm_region
*r
= NULL
;
3855 int resize_step
= 0;
3859 struct swm_geometry g
;
3860 int top
= 0, left
= 0;
3863 unsigned int shape
; /* cursor style */
3869 DNPRINTF(SWM_D_MOUSE
, "resize: window: 0x%lx, floating: %s, "
3870 "transient: 0x%lx\n", win
->id
, YESNO(win
->floating
),
3873 if (!(win
->transient
!= 0 || win
->floating
!= 0))
3876 /* reject resizes in max mode for floaters (transient ok) */
3877 if (win
->floatmaxed
)
3881 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
3887 case SWM_ARG_ID_WIDTHSHRINK
:
3888 WIDTH(win
) -= SWM_RESIZE_STEPS
;
3891 case SWM_ARG_ID_WIDTHGROW
:
3892 WIDTH(win
) += SWM_RESIZE_STEPS
;
3895 case SWM_ARG_ID_HEIGHTSHRINK
:
3896 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
3899 case SWM_ARG_ID_HEIGHTGROW
:
3900 HEIGHT(win
) += SWM_RESIZE_STEPS
;
3907 constrain_window(win
, r
, 1);
3909 store_float_geom(win
,r
);
3913 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3914 drain_enter_notify();
3916 /* get cursor offset from window root */
3917 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
3922 if (wx
< WIDTH(win
) / 2)
3925 if (wy
< HEIGHT(win
) / 2)
3928 if (args
->id
== SWM_ARG_ID_CENTER
)
3931 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
3933 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
3935 cursor
= XCreateFontCursor(display
, shape
);
3937 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
3938 GrabModeAsync
, None
, cursor
, CurrentTime
) != GrabSuccess
) {
3939 XFreeCursor(display
, cursor
);
3944 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
3945 SubstructureRedirectMask
, &ev
);
3947 case ConfigureRequest
:
3950 handler
[ev
.type
](&ev
);
3953 /* cursor offset/delta from start of the operation */
3954 dx
= ev
.xmotion
.x_root
- x
;
3955 dy
= ev
.xmotion
.y_root
- y
;
3961 if (args
->id
== SWM_ARG_ID_CENTER
) {
3962 if (g
.h
/ 2 + dy
< 1)
3966 HEIGHT(win
) = g
.h
+ 2 * dy
;
3974 HEIGHT(win
) = g
.h
+ dy
;
3981 if (args
->id
== SWM_ARG_ID_CENTER
) {
3982 if (g
.w
/ 2 + dx
< 1)
3986 WIDTH(win
) = g
.w
+ 2 * dx
;
3994 WIDTH(win
) = g
.w
+ dx
;
3997 constrain_window(win
, r
, 1);
3999 /* not free, don't sync more than 120 times / second */
4000 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4001 time
= ev
.xmotion
.time
;
4002 XSync(display
, False
);
4007 } while (ev
.type
!= ButtonRelease
);
4009 XSync(display
, False
);
4012 store_float_geom(win
,r
);
4014 XUngrabPointer(display
, CurrentTime
);
4015 XFreeCursor(display
, cursor
);
4018 drain_enter_notify();
4022 resize_step(struct swm_region
*r
, union arg
*args
)
4024 struct ws_win
*win
= NULL
;
4026 if (r
&& r
->ws
&& r
->ws
->focus
)
4034 #define SWM_MOVE_STEPS (50)
4037 move(struct ws_win
*win
, union arg
*args
)
4042 struct swm_region
*r
= NULL
;
4052 DNPRINTF(SWM_D_MOUSE
, "move: window: 0x%lx, floating: %s, transient: "
4053 "0x%lx\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4055 /* in max_stack mode should only move transients */
4056 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4060 if (win
->floating
== 0 && !win
->transient
) {
4061 store_float_geom(win
,r
);
4062 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4065 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4072 case SWM_ARG_ID_MOVELEFT
:
4073 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4076 case SWM_ARG_ID_MOVERIGHT
:
4077 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4080 case SWM_ARG_ID_MOVEUP
:
4081 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4084 case SWM_ARG_ID_MOVEDOWN
:
4085 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4092 constrain_window(win
, r
, 0);
4094 store_float_geom(win
, r
);
4098 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4099 GrabModeAsync
, None
, XCreateFontCursor(display
, XC_fleur
),
4100 CurrentTime
) != GrabSuccess
)
4103 /* get cursor offset from window root */
4104 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4108 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4109 SubstructureRedirectMask
, &ev
);
4111 case ConfigureRequest
:
4114 handler
[ev
.type
](&ev
);
4117 X(win
) = ev
.xmotion
.x_root
- wx
- border_width
;
4118 Y(win
) = ev
.xmotion
.y_root
- wy
- border_width
;
4120 constrain_window(win
, r
, 0);
4122 /* not free, don't sync more than 120 times / second */
4123 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4124 time
= ev
.xmotion
.time
;
4125 XSync(display
, False
);
4130 } while (ev
.type
!= ButtonRelease
);
4132 XSync(display
, False
);
4135 store_float_geom(win
,r
);
4136 XUngrabPointer(display
, CurrentTime
);
4139 drain_enter_notify();
4143 move_step(struct swm_region
*r
, union arg
*args
)
4145 struct ws_win
*win
= NULL
;
4147 if (r
&& r
->ws
&& r
->ws
->focus
)
4152 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4159 /* user/key callable function IDs */
4230 kf_search_workspace
,
4232 kf_dumpwins
, /* MUST BE LAST */
4236 /* key definitions */
4238 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4243 legacyfunc(struct swm_region
*r
, union arg
*args
)
4248 char name
[SWM_FUNCNAME_LEN
];
4249 void (*func
)(struct swm_region
*r
, union arg
*);
4251 } keyfuncs
[kf_invalid
+ 1] = {
4252 /* name function argument */
4253 { "cycle_layout", cycle_layout
, {0} },
4254 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4255 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4256 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4257 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4258 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4259 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4260 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4261 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4262 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4263 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4264 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4265 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4266 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4267 { "spawn_term", spawnterm
, {.argv
= spawn_term
} },
4268 { "spawn_menu", legacyfunc
, {0} },
4269 { "quit", quit
, {0} },
4270 { "restart", restart
, {0} },
4271 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4272 { "ws_1", switchws
, {.id
= 0} },
4273 { "ws_2", switchws
, {.id
= 1} },
4274 { "ws_3", switchws
, {.id
= 2} },
4275 { "ws_4", switchws
, {.id
= 3} },
4276 { "ws_5", switchws
, {.id
= 4} },
4277 { "ws_6", switchws
, {.id
= 5} },
4278 { "ws_7", switchws
, {.id
= 6} },
4279 { "ws_8", switchws
, {.id
= 7} },
4280 { "ws_9", switchws
, {.id
= 8} },
4281 { "ws_10", switchws
, {.id
= 9} },
4282 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4283 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4284 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4285 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4286 { "ws_prior", priorws
, {0} },
4287 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4288 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4289 { "mvws_1", send_to_ws
, {.id
= 0} },
4290 { "mvws_2", send_to_ws
, {.id
= 1} },
4291 { "mvws_3", send_to_ws
, {.id
= 2} },
4292 { "mvws_4", send_to_ws
, {.id
= 3} },
4293 { "mvws_5", send_to_ws
, {.id
= 4} },
4294 { "mvws_6", send_to_ws
, {.id
= 5} },
4295 { "mvws_7", send_to_ws
, {.id
= 6} },
4296 { "mvws_8", send_to_ws
, {.id
= 7} },
4297 { "mvws_9", send_to_ws
, {.id
= 8} },
4298 { "mvws_10", send_to_ws
, {.id
= 9} },
4299 { "bar_toggle", bar_toggle
, {0} },
4300 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4301 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4302 { "screenshot_all", legacyfunc
, {0} },
4303 { "screenshot_wind", legacyfunc
, {0} },
4304 { "float_toggle", floating_toggle
,{0} },
4305 { "version", version
, {0} },
4306 { "spawn_lock", legacyfunc
, {0} },
4307 { "spawn_initscr", legacyfunc
, {0} },
4308 { "spawn_custom", dummykeyfunc
, {0} },
4309 { "iconify", iconify
, {0} },
4310 { "uniconify", uniconify
, {0} },
4311 { "raise_toggle", raise_toggle
, {0} },
4312 { "button2", pressbutton
, {2} },
4313 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4314 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4315 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4316 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4317 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4318 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4319 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4320 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4321 { "name_workspace", name_workspace
, {0} },
4322 { "search_workspace", search_workspace
, {0} },
4323 { "search_win", search_win
, {0} },
4324 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4325 { "invalid key func", NULL
, {0} },
4328 RB_ENTRY(key
) entry
;
4331 enum keyfuncid funcid
;
4334 RB_HEAD(key_list
, key
);
4337 key_cmp(struct key
*kp1
, struct key
*kp2
)
4339 if (kp1
->keysym
< kp2
->keysym
)
4341 if (kp1
->keysym
> kp2
->keysym
)
4344 if (kp1
->mod
< kp2
->mod
)
4346 if (kp1
->mod
> kp2
->mod
)
4352 RB_GENERATE_STATIC(key_list
, key
, entry
, key_cmp
);
4353 struct key_list keys
;
4356 enum { client_click
, root_click
};
4358 unsigned int action
;
4360 unsigned int button
;
4361 void (*func
)(struct ws_win
*, union arg
*);
4364 /* action key mouse button func args */
4365 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4366 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4367 { client_click
, MODKEY
, Button1
, move
, {0} },
4371 update_modkey(unsigned int mod
)
4377 RB_FOREACH(kp
, key_list
, &keys
)
4378 if (kp
->mod
& ShiftMask
)
4379 kp
->mod
= mod
| ShiftMask
;
4383 for (i
= 0; i
< LENGTH(buttons
); i
++)
4384 if (buttons
[i
].mask
& ShiftMask
)
4385 buttons
[i
].mask
= mod
| ShiftMask
;
4387 buttons
[i
].mask
= mod
;
4392 TAILQ_ENTRY(spawn_prog
) entry
;
4397 TAILQ_HEAD(spawn_list
, spawn_prog
);
4398 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4401 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4404 struct spawn_prog
*prog
= NULL
;
4406 char *ap
, **real_args
;
4408 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4411 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4412 if (!strcasecmp(spawn_name
, prog
->name
))
4416 warnx("spawn_custom: program %s not found", spawn_name
);
4420 /* make room for expanded args */
4421 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4422 err(1, "spawn_custom: calloc real_args");
4424 /* expand spawn_args into real_args */
4425 for (i
= 0; i
< prog
->argc
; i
++) {
4427 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4428 if (!strcasecmp(ap
, "$bar_border")) {
4430 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4432 err(1, "spawn_custom border color");
4433 } else if (!strcasecmp(ap
, "$bar_color")) {
4435 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4437 err(1, "spawn_custom bar color");
4438 } else if (!strcasecmp(ap
, "$bar_font")) {
4439 if ((real_args
[i
] = strdup(bar_fonts
))
4441 err(1, "spawn_custom bar fonts");
4442 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4444 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4446 err(1, "spawn_custom color font");
4447 } else if (!strcasecmp(ap
, "$color_focus")) {
4449 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4451 err(1, "spawn_custom color focus");
4452 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4454 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4456 err(1, "spawn_custom color unfocus");
4458 /* no match --> copy as is */
4459 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4460 err(1, "spawn_custom strdup(ap)");
4462 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4467 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4468 for (i
= 0; i
< prog
->argc
; i
++)
4469 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4470 DNPRINTF(SWM_D_SPAWN
, "\n");
4472 *ret_args
= real_args
;
4473 return (prog
->argc
);
4477 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4483 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4487 spawn(r
->ws
->idx
, &a
, 1);
4489 for (i
= 0; i
< spawn_argc
; i
++)
4495 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4501 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4505 if (pipe(select_list_pipe
) == -1)
4506 err(1, "pipe error");
4507 if (pipe(select_resp_pipe
) == -1)
4508 err(1, "pipe error");
4510 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4511 err(1, "could not disable SIGPIPE");
4512 switch (*pid
= fork()) {
4514 err(1, "cannot fork");
4517 if (dup2(select_list_pipe
[0], 0) == -1)
4519 if (dup2(select_resp_pipe
[1], 1) == -1)
4521 close(select_list_pipe
[1]);
4522 close(select_resp_pipe
[0]);
4523 spawn(r
->ws
->idx
, &a
, 0);
4525 default: /* parent */
4526 close(select_list_pipe
[0]);
4527 close(select_resp_pipe
[1]);
4531 for (i
= 0; i
< spawn_argc
; i
++)
4537 spawn_insert(char *name
, char *args
)
4539 char *arg
, *cp
, *ptr
;
4540 struct spawn_prog
*sp
;
4542 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
4544 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
4545 err(1, "spawn_insert: malloc");
4546 if ((sp
->name
= strdup(name
)) == NULL
)
4547 err(1, "spawn_insert: strdup");
4549 /* convert the arguments to an argument list */
4550 if ((ptr
= cp
= strdup(args
)) == NULL
)
4551 err(1, "spawn_insert: strdup");
4552 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
4553 /* empty field; skip it */
4558 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
4559 sizeof *sp
->argv
)) == NULL
)
4560 err(1, "spawn_insert: realloc");
4561 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
4562 err(1, "spawn_insert: strdup");
4566 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
4567 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
4571 spawn_remove(struct spawn_prog
*sp
)
4575 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
4577 TAILQ_REMOVE(&spawns
, sp
, entry
);
4578 for (i
= 0; i
< sp
->argc
; i
++)
4584 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
4588 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
4590 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
4593 spawn_insert(name
, args
);
4595 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
4599 setspawn(char *name
, char *args
)
4601 struct spawn_prog
*sp
;
4603 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
4608 TAILQ_FOREACH(sp
, &spawns
, entry
) {
4609 if (!strcmp(sp
->name
, name
)) {
4613 spawn_replace(sp
, name
, args
);
4614 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
4618 if (*args
== '\0') {
4619 warnx("error: setspawn: cannot find program: %s", name
);
4623 spawn_insert(name
, args
);
4624 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
4628 setconfspawn(char *selector
, char *value
, int flags
)
4630 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
4632 setspawn(selector
, value
);
4634 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
4641 setconfspawn("term", "xterm", 0);
4642 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4643 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4644 setconfspawn("lock", "xlock", 0);
4645 setconfspawn("initscr", "initscreen.sh", 0);
4646 setconfspawn("menu", "dmenu_run"
4649 " -nf $bar_font_color"
4651 " -sf $bar_color", 0);
4652 setconfspawn("search", "dmenu"
4656 " -nf $bar_font_color"
4658 " -sf $bar_color", 0);
4659 setconfspawn("name_workspace", "dmenu"
4663 " -nf $bar_font_color"
4665 " -sf $bar_color", 0);
4669 #define SWM_MODNAME_SIZE 32
4670 #define SWM_KEY_WS "\n+ \t"
4672 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
4676 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
4677 if (mod
== NULL
|| ks
== NULL
) {
4678 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
4681 if (keystr
== NULL
|| strlen(keystr
) == 0) {
4682 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
4688 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
4689 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
4691 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
4692 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
4694 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
4696 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
4698 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
4700 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
4702 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
4704 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
4705 *mod
|= ControlMask
;
4707 *ks
= XStringToKeysym(name
);
4708 XConvertCase(*ks
, ks
, &uks
);
4709 if (ks
== NoSymbol
) {
4711 "parsekeys: invalid key %s\n",
4717 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
4722 strdupsafe(char *str
)
4727 return (strdup(str
));
4731 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
4735 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
4736 keyfuncs
[kfid
].name
, spawn_name
);
4738 if ((kp
= malloc(sizeof *kp
)) == NULL
)
4739 err(1, "key_insert: malloc");
4744 kp
->spawn_name
= strdupsafe(spawn_name
);
4745 RB_INSERT(key_list
, &keys
, kp
);
4747 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
4751 key_lookup(unsigned int mod
, KeySym ks
)
4758 return (RB_FIND(key_list
, &keys
, &kp
));
4762 key_remove(struct key
*kp
)
4764 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
4766 RB_REMOVE(key_list
, &keys
, kp
);
4767 free(kp
->spawn_name
);
4770 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
4774 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
4777 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
4781 key_insert(mod
, ks
, kfid
, spawn_name
);
4783 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
4787 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
4792 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
4793 keyfuncs
[kfid
].name
, spawn_name
);
4795 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
4796 if (kfid
== kf_invalid
)
4799 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
4800 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4803 if (kfid
== kf_invalid
) {
4804 warnx("error: setkeybinding: cannot find mod/key combination");
4805 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4809 key_insert(mod
, ks
, kfid
, spawn_name
);
4810 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4814 setconfbinding(char *selector
, char *value
, int flags
)
4816 enum keyfuncid kfid
;
4819 struct spawn_prog
*sp
;
4820 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
4821 if (selector
== NULL
) {
4822 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
4823 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4825 setkeybinding(mod
, ks
, kfid
, NULL
);
4830 /* search by key function name */
4831 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
4832 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
4833 SWM_FUNCNAME_LEN
) == 0) {
4834 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4836 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4837 setkeybinding(mod
, ks
, kfid
, NULL
);
4843 /* search by custom spawn name */
4844 TAILQ_FOREACH(sp
, &spawns
, entry
) {
4845 if (strcasecmp(selector
, sp
->name
) == 0) {
4846 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4848 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4849 setkeybinding(mod
, ks
, kf_spawn_custom
,
4856 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
4863 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
4864 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, kf_flip_layout
, NULL
);
4865 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
4866 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
4867 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
4868 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
4869 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
4870 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
4871 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
4872 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
4873 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
4874 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
4875 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
4876 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
4877 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_term
, NULL
);
4878 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
4879 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
4880 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
4881 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
4882 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
4883 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
4884 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
4885 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
4886 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
4887 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
4888 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
4889 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
4890 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
4891 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
4892 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
4893 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
4894 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
4895 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
4896 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
4897 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
4898 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
4899 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
4900 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
4901 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
4902 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
4903 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
4904 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
4905 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
4906 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
4907 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
4908 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
4909 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
4910 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
4911 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
4912 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
4913 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
4914 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
4915 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
4916 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
4917 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
4918 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
4919 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
4920 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
4921 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
4922 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
4923 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
4924 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
4925 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
4926 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
4927 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
4928 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
4929 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
4930 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
4931 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
4932 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
4933 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
4934 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
4936 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
4945 while (RB_EMPTY(&keys
) == 0) {
4946 kp
= RB_ROOT(&keys
);
4952 setkeymapping(char *selector
, char *value
, int flags
)
4954 char keymapping_file
[PATH_MAX
];
4955 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
4956 if (value
[0] == '~')
4957 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
4958 pwd
->pw_dir
, &value
[1]);
4960 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
4962 /* load new key bindings; if it fails, revert to default bindings */
4963 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
4967 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
4972 updatenumlockmask(void)
4975 XModifierKeymap
*modmap
;
4977 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
4979 modmap
= XGetModifierMapping(display
);
4980 for (i
= 0; i
< 8; i
++)
4981 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
4982 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
4983 == XKeysymToKeycode(display
, XK_Num_Lock
))
4984 numlockmask
= (1 << i
);
4986 XFreeModifiermap(modmap
);
4994 unsigned int modifiers
[] =
4995 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
4998 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
4999 updatenumlockmask();
5001 for (k
= 0; k
< ScreenCount(display
); k
++) {
5002 if (TAILQ_EMPTY(&screens
[k
].rl
))
5004 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
5005 RB_FOREACH(kp
, key_list
, &keys
) {
5006 if ((code
= XKeysymToKeycode(display
, kp
->keysym
)))
5007 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5008 XGrabKey(display
, code
,
5009 kp
->mod
| modifiers
[j
],
5010 screens
[k
].root
, True
,
5011 GrabModeAsync
, GrabModeAsync
);
5017 grabbuttons(struct ws_win
*win
, int focused
)
5020 unsigned int modifiers
[] =
5021 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5023 updatenumlockmask();
5024 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
5026 for (i
= 0; i
< LENGTH(buttons
); i
++)
5027 if (buttons
[i
].action
== client_click
)
5028 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5029 XGrabButton(display
, buttons
[i
].button
,
5030 buttons
[i
].mask
| modifiers
[j
],
5031 win
->id
, False
, BUTTONMASK
,
5032 GrabModeAsync
, GrabModeSync
, None
,
5035 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
5036 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
5039 const char *quirkname
[] = {
5040 "NONE", /* config string for "no value" */
5049 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5050 #define SWM_Q_WS "\n|+ \t"
5052 parsequirks(char *qstr
, unsigned long *quirk
)
5062 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5064 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5065 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5066 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5067 DNPRINTF(SWM_D_QUIRK
,
5068 "parsequirks: %s\n", name
);
5073 *quirk
|= 1 << (i
-1);
5077 if (i
>= LENGTH(quirkname
)) {
5078 DNPRINTF(SWM_D_QUIRK
,
5079 "parsequirks: invalid quirk [%s]\n", name
);
5087 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5091 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5094 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5095 err(1, "quirk_insert: malloc");
5096 if ((qp
->class = strdup(class)) == NULL
)
5097 err(1, "quirk_insert: strdup");
5098 if ((qp
->name
= strdup(name
)) == NULL
)
5099 err(1, "quirk_insert: strdup");
5102 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5104 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5108 quirk_remove(struct quirk
*qp
)
5110 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5111 qp
->name
, qp
->quirk
);
5113 TAILQ_REMOVE(&quirks
, qp
, entry
);
5118 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5122 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5123 unsigned long quirk
)
5125 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5126 qp
->name
, qp
->quirk
);
5129 quirk_insert(class, name
, quirk
);
5131 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5135 setquirk(const char *class, const char *name
, unsigned long quirk
)
5139 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5142 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5143 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5147 quirk_replace(qp
, class, name
, quirk
);
5148 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5153 warnx("error: setquirk: cannot find class/name combination");
5157 quirk_insert(class, name
, quirk
);
5158 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5162 setconfquirk(char *selector
, char *value
, int flags
)
5164 char *cp
, *class, *name
;
5166 unsigned long quirks
;
5167 if (selector
== NULL
)
5169 if ((cp
= strchr(selector
, ':')) == NULL
)
5174 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5175 setquirk(class, name
, quirks
);
5182 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5183 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5184 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5185 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5186 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5187 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5188 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5189 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5190 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5191 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5192 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5193 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5194 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5197 /* conf file stuff */
5198 #define SWM_CONF_FILE "spectrwm.conf"
5199 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5201 enum { SWM_S_BAR_DELAY
, SWM_S_BAR_ENABLED
, SWM_S_BAR_BORDER_WIDTH
,
5202 SWM_S_STACK_ENABLED
, SWM_S_CLOCK_ENABLED
, SWM_S_CLOCK_FORMAT
,
5203 SWM_S_CYCLE_EMPTY
, SWM_S_CYCLE_VISIBLE
, SWM_S_SS_ENABLED
,
5204 SWM_S_TERM_WIDTH
, SWM_S_TITLE_CLASS_ENABLED
,
5205 SWM_S_TITLE_NAME_ENABLED
, SWM_S_WINDOW_NAME_ENABLED
, SWM_S_URGENT_ENABLED
,
5206 SWM_S_FOCUS_MODE
, SWM_S_DISABLE_BORDER
, SWM_S_BORDER_WIDTH
,
5207 SWM_S_BAR_FONT
, SWM_S_BAR_ACTION
, SWM_S_SPAWN_TERM
,
5208 SWM_S_SS_APP
, SWM_S_DIALOG_RATIO
, SWM_S_BAR_AT_BOTTOM
,
5209 SWM_S_VERBOSE_LAYOUT
, SWM_S_BAR_JUSTIFY
5213 setconfvalue(char *selector
, char *value
, int flags
)
5219 case SWM_S_BAR_DELAY
:
5220 bar_delay
= atoi(value
);
5222 case SWM_S_BAR_ENABLED
:
5223 bar_enabled
= atoi(value
);
5225 case SWM_S_BAR_BORDER_WIDTH
:
5226 bar_border_width
= atoi(value
);
5228 case SWM_S_BAR_AT_BOTTOM
:
5229 bar_at_bottom
= atoi(value
);
5231 case SWM_S_BAR_JUSTIFY
:
5232 if (!strcmp(value
, "left"))
5233 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5234 else if (!strcmp(value
, "center"))
5235 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5236 else if (!strcmp(value
, "right"))
5237 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5239 errx(1, "invalid bar_justify");
5241 case SWM_S_STACK_ENABLED
:
5242 stack_enabled
= atoi(value
);
5244 case SWM_S_CLOCK_ENABLED
:
5245 clock_enabled
= atoi(value
);
5247 case SWM_S_CLOCK_FORMAT
:
5248 #ifndef SWM_DENY_CLOCK_FORMAT
5250 if ((clock_format
= strdup(value
)) == NULL
)
5251 err(1, "setconfvalue: clock_format");
5254 case SWM_S_CYCLE_EMPTY
:
5255 cycle_empty
= atoi(value
);
5257 case SWM_S_CYCLE_VISIBLE
:
5258 cycle_visible
= atoi(value
);
5260 case SWM_S_SS_ENABLED
:
5261 ss_enabled
= atoi(value
);
5263 case SWM_S_TERM_WIDTH
:
5264 term_width
= atoi(value
);
5266 case SWM_S_TITLE_CLASS_ENABLED
:
5267 title_class_enabled
= atoi(value
);
5269 case SWM_S_WINDOW_NAME_ENABLED
:
5270 window_name_enabled
= atoi(value
);
5272 case SWM_S_TITLE_NAME_ENABLED
:
5273 title_name_enabled
= atoi(value
);
5275 case SWM_S_URGENT_ENABLED
:
5276 urgent_enabled
= atoi(value
);
5278 case SWM_S_FOCUS_MODE
:
5279 if (!strcmp(value
, "default"))
5280 focus_mode
= SWM_FOCUS_DEFAULT
;
5281 else if (!strcmp(value
, "follow_cursor"))
5282 focus_mode
= SWM_FOCUS_FOLLOW
;
5283 else if (!strcmp(value
, "synergy"))
5284 focus_mode
= SWM_FOCUS_SYNERGY
;
5286 errx(1, "focus_mode");
5288 case SWM_S_DISABLE_BORDER
:
5289 disable_border
= atoi(value
);
5291 case SWM_S_BORDER_WIDTH
:
5292 border_width
= atoi(value
);
5294 case SWM_S_BAR_FONT
:
5296 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5297 err(1, "setconfvalue: asprintf: failed to allocate "
5298 "memory for bar_fonts.");
5302 case SWM_S_BAR_ACTION
:
5304 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5305 err(1, "setconfvalue: bar_action");
5307 case SWM_S_SPAWN_TERM
:
5308 free(spawn_term
[0]);
5309 if ((spawn_term
[0] = strdup(value
)) == NULL
)
5310 err(1, "setconfvalue: spawn_term");
5314 case SWM_S_DIALOG_RATIO
:
5315 dialog_ratio
= atof(value
);
5316 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5319 case SWM_S_VERBOSE_LAYOUT
:
5320 verbose_layout
= atoi(value
);
5321 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5323 layouts
[i
].l_string
= fancy_stacker
;
5325 layouts
[i
].l_string
= plain_stacker
;
5335 setconfmodkey(char *selector
, char *value
, int flags
)
5337 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5338 update_modkey(Mod1Mask
);
5339 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5340 update_modkey(Mod2Mask
);
5341 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5342 update_modkey(Mod3Mask
);
5343 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5344 update_modkey(Mod4Mask
);
5351 setconfcolor(char *selector
, char *value
, int flags
)
5353 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5358 setconfregion(char *selector
, char *value
, int flags
)
5360 custom_region(value
);
5365 setautorun(char *selector
, char *value
, int flags
)
5375 if (getenv("SWM_STARTED"))
5379 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5380 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5382 if (ws_id
< 0 || ws_id
>= SWM_WS_MAX
)
5383 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5386 * This is a little intricate
5388 * If the pid already exists we simply reuse it because it means it was
5389 * used before AND not claimed by manage_window. We get away with
5390 * altering it in the parent after INSERT because this can not be a race
5393 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5396 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5398 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5399 err(1, "setautorun: realloc");
5400 a
.argv
[argc
- 1] = ap
;
5403 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5404 err(1, "setautorun: realloc");
5405 a
.argv
[argc
] = NULL
;
5407 if ((pid
= fork()) == 0) {
5408 spawn(ws_id
, &a
, 1);
5417 p
= calloc(1, sizeof *p
);
5420 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
5430 setlayout(char *selector
, char *value
, int flags
)
5432 int ws_id
, i
, x
, mg
, ma
, si
, raise
;
5433 int st
= SWM_V_STACK
;
5435 struct workspace
*ws
;
5437 if (getenv("SWM_STARTED"))
5441 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
5442 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
5443 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5444 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5447 if (ws_id
< 0 || ws_id
>= SWM_WS_MAX
)
5448 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
5450 if (!strcasecmp(s
, "vertical"))
5452 else if (!strcasecmp(s
, "horizontal"))
5454 else if (!strcasecmp(s
, "fullscreen"))
5457 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5458 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5461 for (i
= 0; i
< ScreenCount(display
); i
++) {
5462 ws
= (struct workspace
*)&screens
[i
].ws
;
5463 ws
[ws_id
].cur_layout
= &layouts
[st
];
5465 ws
[ws_id
].always_raise
= raise
;
5466 if (st
== SWM_MAX_STACK
)
5470 for (x
= 0; x
< abs(mg
); x
++) {
5471 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5472 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
5473 SWM_ARG_ID_MASTERSHRINK
);
5477 for (x
= 0; x
< abs(ma
); x
++) {
5478 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5479 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
5480 SWM_ARG_ID_MASTERDEL
);
5484 for (x
= 0; x
< abs(si
); x
++) {
5485 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5486 si
>= 0 ? SWM_ARG_ID_STACKINC
:
5487 SWM_ARG_ID_STACKDEC
);
5495 /* config options */
5496 struct config_option
{
5498 int (*func
)(char*, char*, int);
5501 struct config_option configopt
[] = {
5502 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
5503 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
5504 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
5505 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
5506 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
5507 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
5508 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
5509 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
5510 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
5511 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
5512 { "keyboard_mapping", setkeymapping
, 0 },
5513 { "bind", setconfbinding
, 0 },
5514 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
5515 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
5516 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
5517 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
5518 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
5519 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
5520 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
5521 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
5522 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
5523 { "modkey", setconfmodkey
, 0 },
5524 { "program", setconfspawn
, 0 },
5525 { "quirk", setconfquirk
, 0 },
5526 { "region", setconfregion
, 0 },
5527 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
5528 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
5529 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
5530 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
5531 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
5532 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
5533 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
5534 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
5535 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
5536 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
5537 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
5538 { "autorun", setautorun
, 0 },
5539 { "layout", setlayout
, 0 },
5544 conf_load(char *filename
, int keymapping
)
5547 char *line
, *cp
, *optsub
, *optval
;
5548 size_t linelen
, lineno
= 0;
5549 int wordlen
, i
, optind
;
5550 struct config_option
*opt
;
5552 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
5554 if (filename
== NULL
) {
5555 warnx("conf_load: no filename");
5558 if ((config
= fopen(filename
, "r")) == NULL
) {
5559 warn("conf_load: fopen: %s", filename
);
5563 while (!feof(config
)) {
5564 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
5567 err(1, "%s", filename
);
5572 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
5573 if (cp
[0] == '\0') {
5578 /* get config option */
5579 wordlen
= strcspn(cp
, "=[ \t\n");
5581 warnx("%s: line %zd: no option found",
5586 for (i
= 0; i
< LENGTH(configopt
); i
++) {
5587 opt
= &configopt
[i
];
5588 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
5589 strlen(opt
->optname
) == wordlen
) {
5595 warnx("%s: line %zd: unknown option %.*s",
5596 filename
, lineno
, wordlen
, cp
);
5599 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
5600 warnx("%s: line %zd: invalid option %.*s",
5601 filename
, lineno
, wordlen
, cp
);
5605 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
5606 /* get [selector] if any */
5610 wordlen
= strcspn(cp
, "]");
5613 warnx("%s: line %zd: syntax error",
5618 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
5620 warnx("%s: line %zd: unable to allocate"
5621 "memory for selector", filename
,
5627 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
5629 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
5631 optval
= strdup(cp
);
5632 /* call function to deal with it all */
5633 if (configopt
[optind
].func(optsub
, optval
,
5634 configopt
[optind
].funcflags
) != 0)
5635 errx(1, "%s: line %zd: invalid data for %s",
5636 filename
, lineno
, configopt
[optind
].optname
);
5643 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
5650 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
5656 set_child_transient(struct ws_win
*win
, Window
*trans
)
5658 struct ws_win
*parent
, *w
;
5659 XWMHints
*wmh
= NULL
;
5660 struct swm_region
*r
;
5661 struct workspace
*ws
;
5663 parent
= find_window(win
->transient
);
5665 parent
->child_trans
= win
;
5667 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
5668 " for 0x%lx trans 0x%lx\n", win
->id
, win
->transient
);
5670 if (win
->hints
== NULL
) {
5671 warnx("no hints for 0x%lx", win
->id
);
5675 r
= root_to_region(win
->wa
.root
);
5677 /* parent doen't exist in our window list */
5678 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5682 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
5683 warnx("can't get hints for 0x%lx", w
->id
);
5687 if (win
->hints
->window_group
!= wmh
->window_group
)
5690 w
->child_trans
= win
;
5691 win
->transient
= w
->id
;
5693 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
5694 "transient to 0x%lx\n", win
->transient
);
5704 window_get_pid(Window win
)
5706 Atom actual_type_return
;
5707 int actual_format_return
= 0;
5708 unsigned long nitems_return
= 0;
5709 unsigned long bytes_after_return
= 0;
5713 unsigned char *prop
= NULL
;
5715 if (XGetWindowProperty(display
, win
,
5716 XInternAtom(display
, "_NET_WM_PID", False
), 0, 1, False
,
5717 XA_CARDINAL
, &actual_type_return
, &actual_format_return
,
5718 &nitems_return
, &bytes_after_return
,
5719 (unsigned char**)(void*)&pid
) != Success
)
5721 if (actual_type_return
!= XA_CARDINAL
)
5732 if (XGetWindowProperty(display
, win
,
5733 XInternAtom(display
, "_SWM_PID", False
), 0, SWM_PROPLEN
, False
,
5734 XA_STRING
, &actual_type_return
, &actual_format_return
,
5735 &nitems_return
, &bytes_after_return
, &prop
) != Success
)
5737 if (actual_type_return
!= XA_STRING
)
5742 ret
= strtonum((const char *)prop
, 0, UINT_MAX
, &errstr
);
5743 /* ignore error because strtonum returns 0 anyway */
5750 manage_window(Window id
)
5753 struct workspace
*ws
;
5754 struct ws_win
*win
, *ww
;
5755 int format
, i
, ws_idx
, n
, border_me
= 0;
5756 unsigned long nitems
, bytes
;
5757 Atom ws_idx_atom
= 0, type
;
5758 Atom
*prot
= NULL
, *pp
;
5759 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
5760 struct swm_region
*r
;
5767 if ((win
= find_window(id
)) != NULL
)
5768 return (win
); /* already being managed */
5770 /* see if we are on the unmanaged list */
5771 if ((win
= find_unmanaged_window(id
)) != NULL
) {
5772 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
5773 "window: 0x%lx\n", win
->id
);
5774 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5775 if (win
->transient
) {
5776 set_child_transient(win
, &trans
);
5777 } if (trans
&& (ww
= find_window(trans
)))
5778 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
5780 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
5781 ewmh_update_actions(win
);
5785 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
5786 err(1, "manage_window: calloc: failed to allocate memory for "
5791 /* see if we need to override the workspace */
5792 p
= find_pid(window_get_pid(id
));
5794 /* Get all the window data in one shot */
5795 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
5797 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
5798 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
5800 XGetWindowAttributes(display
, id
, &win
->wa
);
5801 XGetWMNormalHints(display
, id
, &win
->sh
, &win
->sh_mask
);
5802 win
->hints
= XGetWMHints(display
, id
);
5803 XGetTransientForHint(display
, id
, &trans
);
5805 win
->transient
= trans
;
5806 set_child_transient(win
, &trans
);
5807 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, "
5808 "transient: 0x%lx\n", win
->id
, win
->transient
);
5811 /* get supported protocols */
5812 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
5813 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
5814 if (*pp
== takefocus
)
5815 win
->take_focus
= 1;
5817 win
->can_delete
= 1;
5823 win
->iconic
= get_iconic(win
);
5826 * Figure out where to put the window. If it was previously assigned to
5827 * a workspace (either by spawn() or manually moving), and isn't
5828 * transient, * put it in the same workspace
5830 r
= root_to_region(win
->wa
.root
);
5832 ws
= &r
->s
->ws
[p
->ws
];
5833 TAILQ_REMOVE(&pidlist
, p
, entry
);
5836 } else if (prop
&& win
->transient
== 0) {
5837 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
5838 ws_idx
= strtonum((const char *)prop
, 0, 9, &errstr
);
5840 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
5843 ws
= &r
->s
->ws
[ws_idx
];
5846 /* this should launch transients in the same ws as parent */
5848 if ((ww
= find_window(trans
)) != NULL
)
5854 warnx("manage_window: fix this "
5860 /* set up the window layout */
5863 win
->s
= r
->s
; /* this never changes */
5864 if (trans
&& (ww
= find_window(trans
)))
5865 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
5867 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
5869 WIDTH(win
) = win
->wa
.width
;
5870 HEIGHT(win
) = win
->wa
.height
;
5873 win
->g_floatvalid
= 0;
5874 win
->floatmaxed
= 0;
5875 win
->ewmh_flags
= 0;
5877 /* Set window properties so we can remember this after reincarnation */
5878 if (ws_idx_atom
&& prop
== NULL
&&
5879 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
5881 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
5883 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
5884 PropModeReplace
, ws_idx_str
, strlen((char *)ws_idx_str
));
5889 ewmh_autoquirk(win
);
5891 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
5892 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
5893 win
->ch
.res_class
, win
->ch
.res_name
);
5895 /* java is retarded so treat it special */
5896 if (strstr(win
->ch
.res_name
, "sun-awt")) {
5901 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5902 if (!strcmp(win
->ch
.res_class
, qp
->class) &&
5903 !strcmp(win
->ch
.res_name
, qp
->name
)) {
5904 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
5905 "class: %s, name: %s\n", win
->ch
.res_class
,
5907 if (qp
->quirk
& SWM_Q_FLOAT
) {
5911 win
->quirks
= qp
->quirk
;
5916 /* alter window position if quirky */
5917 if (win
->quirks
& SWM_Q_ANYWHERE
) {
5918 win
->manual
= 1; /* don't center the quirky windows */
5919 bzero(&wc
, sizeof wc
);
5921 if (bar_enabled
&& Y(win
) < bar_height
) {
5922 Y(win
) = wc
.y
= bar_height
;
5925 if (WIDTH(win
) + X(win
) > WIDTH(r
)) {
5926 X(win
) = wc
.x
= WIDTH(r
) - WIDTH(win
) - 2;
5932 /* Reset font sizes (the bruteforce way; no default keybinding). */
5933 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
5934 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
5935 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
5936 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
5937 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
5940 ewmh_get_win_state(win
);
5941 ewmh_update_actions(win
);
5942 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
5946 bzero(&wc
, sizeof wc
);
5947 wc
.border_width
= border_width
;
5948 mask
= CWBorderWidth
;
5949 XConfigureWindow(display
, win
->id
, mask
, &wc
);
5952 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
5953 PropertyChangeMask
| StructureNotifyMask
);
5955 /* floaters need to be mapped if they are in the current workspace */
5956 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
5957 XMapRaised(display
, win
->id
);
5963 free_window(struct ws_win
*win
)
5965 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%lx\n", win
->id
);
5970 /* needed for restart wm */
5971 set_win_state(win
, WithdrawnState
);
5973 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5975 if (win
->ch
.res_class
)
5976 XFree(win
->ch
.res_class
);
5977 if (win
->ch
.res_name
)
5978 XFree(win
->ch
.res_name
);
5983 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
5989 unmanage_window(struct ws_win
*win
)
5991 struct ws_win
*parent
;
5996 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%lx\n", win
->id
);
5998 if (win
->transient
) {
5999 parent
= find_window(win
->transient
);
6001 parent
->child_trans
= NULL
;
6004 /* focus on root just in case */
6005 XSetInputFocus(display
, PointerRoot
, PointerRoot
, CurrentTime
);
6014 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
6015 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6021 focus_magic(struct ws_win
*win
)
6023 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%lx\n", WINID(win
));
6026 /* if there are no windows clear the status-bar */
6031 if (win
->child_trans
) {
6032 /* win = parent & has a transient so focus on that */
6034 focus_win(win
->child_trans
);
6035 if (win
->child_trans
->take_focus
)
6036 client_msg(win
, takefocus
);
6038 /* make sure transient hasn't disappeared */
6039 if (validate_win(win
->child_trans
) == 0) {
6040 focus_win(win
->child_trans
);
6041 if (win
->child_trans
->take_focus
)
6042 client_msg(win
->child_trans
, takefocus
);
6044 win
->child_trans
= NULL
;
6046 if (win
->take_focus
)
6047 client_msg(win
, takefocus
);
6053 if (win
->take_focus
)
6054 client_msg(win
, takefocus
);
6061 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%lx\n", e
->xexpose
.window
);
6068 XKeyEvent
*ev
= &e
->xkey
;
6070 struct swm_region
*r
;
6072 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
6073 if ((kp
= key_lookup(CLEANMASK(ev
->state
), keysym
)) == NULL
)
6075 if (keyfuncs
[kp
->funcid
].func
== NULL
)
6078 r
= root_to_region(ev
->root
);
6079 if (kp
->funcid
== kf_spawn_custom
)
6080 spawn_custom(r
, &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
6082 keyfuncs
[kp
->funcid
].func(r
, &(keyfuncs
[kp
->funcid
].args
));
6086 buttonpress(XEvent
*e
)
6090 XButtonPressedEvent
*ev
= &e
->xbutton
;
6092 if ((win
= find_window(ev
->window
)) == NULL
)
6096 action
= client_click
;
6098 for (i
= 0; i
< LENGTH(buttons
); i
++)
6099 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6100 buttons
[i
].button
== ev
->button
&&
6101 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
6102 buttons
[i
].func(win
, &buttons
[i
].args
);
6106 configurerequest(XEvent
*e
)
6108 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
6113 if ((win
= find_window(ev
->window
)) == NULL
)
6114 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6117 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%lx, new: %s\n",
6118 ev
->window
, YESNO(new));
6121 bzero(&wc
, sizeof wc
);
6124 wc
.width
= ev
->width
;
6125 wc
.height
= ev
->height
;
6126 wc
.border_width
= ev
->border_width
;
6127 wc
.sibling
= ev
->above
;
6128 wc
.stack_mode
= ev
->detail
;
6129 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
6131 config_win(win
, ev
);
6136 configurenotify(XEvent
*e
)
6140 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%lx\n",
6141 e
->xconfigure
.window
);
6143 win
= find_window(e
->xconfigure
.window
);
6145 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &win
->sh_mask
);
6149 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6150 drain_enter_notify();
6155 destroynotify(XEvent
*e
)
6158 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
6160 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%lx\n", ev
->window
);
6162 if ((win
= find_window(ev
->window
)) == NULL
) {
6163 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6169 /* make sure we focus on something */
6172 unmanage_window(win
);
6174 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6175 drain_enter_notify();
6180 enternotify(XEvent
*e
)
6182 XCrossingEvent
*ev
= &e
->xcrossing
;
6187 Window focus_return
;
6188 int revert_to_return
;
6190 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%lx, mode: %d, detail: "
6191 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6192 "state: %d\n", ev
->window
, ev
->mode
, ev
->detail
, ev
->root
,
6193 ev
->subwindow
, YESNO(ev
->same_screen
), YESNO(ev
->focus
), ev
->state
);
6195 if (ev
->mode
!= NotifyNormal
) {
6196 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6201 switch (focus_mode
) {
6202 case SWM_FOCUS_DEFAULT
:
6204 case SWM_FOCUS_FOLLOW
:
6206 case SWM_FOCUS_SYNERGY
:
6209 * all these checks need to be in this order because the
6210 * XCheckTypedWindowEvent relies on weeding out the previous events
6212 * making this code an option would enable a follow mouse for focus
6217 * state is set when we are switching workspaces and focus is set when
6218 * the window or a subwindow already has focus (occurs during restart).
6220 * Only honor the focus flag if last_focus_event is not FocusOut,
6221 * this allows spectrwm to continue to control focus when another
6222 * program is also playing with it.
6224 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6225 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6230 * happens when a window is created or destroyed and the border
6231 * crosses the mouse pointer and when switching ws
6233 * we need the subwindow test to see if we came from root in order
6234 * to give focus to floaters
6236 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6237 ev
->subwindow
== 0) {
6238 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6242 /* this window already has focus */
6243 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6244 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6248 /* this window is being deleted or moved to another ws */
6249 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6251 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6252 XPutBackEvent(display
, &cne
);
6256 if ((win
= find_window(ev
->window
)) == NULL
) {
6257 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6262 * In fullstack kill all enters unless they come from a different ws
6263 * (i.e. another region) or focus has been grabbed externally.
6265 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6266 last_focus_event
!= FocusOut
) {
6267 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6268 if ((w
= find_window(focus_return
)) == NULL
||
6270 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6278 if ((win
= find_window(ev
->window
)) == NULL
) {
6279 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6284 * if we have more enternotifies let them handle it in due time
6286 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
6287 DNPRINTF(SWM_D_EVENT
,
6288 "ignoring enternotify: got more enternotify\n");
6289 XPutBackEvent(display
, &cne
);
6296 /* lets us use one switch statement for arbitrary mode/detail combinations */
6297 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6300 focusevent(XEvent
*e
)
6304 u_int32_t mode_detail
;
6305 XFocusChangeEvent
*ev
= &e
->xfocus
;
6307 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: 0x%lx mode: %d "
6308 "detail: %d\n", ev
->type
== FocusIn
? "entering" : "leaving",
6309 ev
->window
, ev
->mode
, ev
->detail
);
6311 if (last_focus_event
== ev
->type
) {
6312 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
6316 last_focus_event
= ev
->type
;
6317 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
6319 switch (mode_detail
) {
6320 /* synergy client focus operations */
6321 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
6322 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
6324 /* synergy server focus operations */
6325 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
6327 /* Entering applications like rdesktop that mangle the pointer */
6328 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
6330 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
6331 XSetWindowBorder(display
, win
->id
,
6332 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
6333 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
6336 warnx("ignoring focusevent");
6337 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
6344 mapnotify(XEvent
*e
)
6347 XMapEvent
*ev
= &e
->xmap
;
6349 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%lx\n", ev
->window
);
6351 win
= manage_window(ev
->window
);
6353 set_win_state(win
, NormalState
);
6357 mappingnotify(XEvent
*e
)
6359 XMappingEvent
*ev
= &e
->xmapping
;
6361 XRefreshKeyboardMapping(ev
);
6362 if (ev
->request
== MappingKeyboard
)
6367 maprequest(XEvent
*e
)
6370 struct swm_region
*r
;
6371 XWindowAttributes wa
;
6372 XMapRequestEvent
*ev
= &e
->xmaprequest
;
6374 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%lx\n",
6375 e
->xmaprequest
.window
);
6377 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
6379 if (wa
.override_redirect
)
6382 win
= manage_window(e
->xmaprequest
.window
);
6384 return; /* can't happen */
6388 /* make new win focused */
6389 r
= root_to_region(win
->wa
.root
);
6390 if (win
->ws
== r
->ws
)
6395 propertynotify(XEvent
*e
)
6398 XPropertyEvent
*ev
= &e
->xproperty
;
6401 name
= XGetAtomName(display
, ev
->atom
);
6402 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%lx, atom: %s\n",
6407 win
= find_window(ev
->window
);
6411 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
6412 update_iconic(win
, 0);
6413 XMapRaised(display
, win
->id
);
6421 case XA_WM_NORMAL_HINTS
:
6423 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
6424 warnx("normal hints: flag 0x%x", win
->sh
.flags
);
6425 if (win
->sh
.flags
& PMinSize
) {
6426 WIDTH(win
) = win
->sh
.min_width
;
6427 HEIGHT(win
) = win
->sh
.min_height
;
6428 warnx("min %d %d", WIDTH(win
), HEIGHT(win
));
6430 XMoveResizeWindow(display
, win
->id
,
6431 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
));
6434 if (title_name_enabled
|| title_class_enabled
)
6438 if (window_name_enabled
)
6447 unmapnotify(XEvent
*e
)
6451 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%lx\n", e
->xunmap
.window
);
6453 /* determine if we need to help unmanage this window */
6454 win
= find_window(e
->xunmap
.window
);
6458 if (getstate(e
->xunmap
.window
) == NormalState
) {
6459 unmanage_window(win
);
6462 /* giant hack for apps that don't destroy transient windows */
6463 /* eat a bunch of events to prevent remanaging the window */
6465 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6466 EnterWindowMask
, &cne
))
6468 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6469 StructureNotifyMask
, &cne
))
6471 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6472 SubstructureNotifyMask
, &cne
))
6474 /* resend unmap because we ated it */
6475 XUnmapWindow(display
, e
->xunmap
.window
);
6478 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6479 drain_enter_notify();
6483 visibilitynotify(XEvent
*e
)
6486 struct swm_region
*r
;
6488 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%lx\n",
6489 e
->xvisibility
.window
);
6490 if (e
->xvisibility
.state
== VisibilityUnobscured
)
6491 for (i
= 0; i
< ScreenCount(display
); i
++)
6492 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6493 if (e
->xvisibility
.window
== r
->bar_window
)
6498 clientmessage(XEvent
*e
)
6500 XClientMessageEvent
*ev
;
6505 win
= find_window(ev
->window
);
6509 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx, type: %ld\n",
6510 ev
->window
, ev
->message_type
);
6512 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
6513 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
6516 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
6517 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
6518 if (win
->can_delete
)
6519 client_msg(win
, adelete
);
6521 XKillClient(display
, win
->id
);
6523 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
6524 DNPRINTF(SWM_D_EVENT
,
6525 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
6526 if (win
->floating
) {
6527 if (ev
->data
.l
[0] & (1<<8)) /* x */
6528 X(win
) = ev
->data
.l
[1];
6529 if (ev
->data
.l
[0] & (1<<9)) /* y */
6530 Y(win
) = ev
->data
.l
[2];
6531 if (ev
->data
.l
[0] & (1<<10)) /* width */
6532 WIDTH(win
) = ev
->data
.l
[3];
6533 if (ev
->data
.l
[0] & (1<<11)) /* height */
6534 HEIGHT(win
) = ev
->data
.l
[4];
6539 /* TODO: Change stack sizes */
6540 /* notify no change was made. */
6541 config_win(win
, NULL
);
6544 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
6545 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
6546 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
6548 ewmh_update_win_state(win
, ev
->data
.l
[2],
6556 xerror_start(Display
*d
, XErrorEvent
*ee
)
6563 xerror(Display
*d
, XErrorEvent
*ee
)
6565 /* warnx("error: %p %p", display, ee); */
6573 xerrorxlib
= XSetErrorHandler(xerror_start
);
6575 /* this causes an error if some other window manager is running */
6576 XSelectInput(display
, DefaultRootWindow(display
),
6577 SubstructureRedirectMask
);
6578 XSync(display
, False
);
6582 XSetErrorHandler(xerror
);
6583 XSync(display
, False
);
6588 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
6590 struct swm_region
*r
, *n
;
6591 struct workspace
*ws
= NULL
;
6594 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
6595 s
->idx
, w
, h
, x
, y
);
6597 /* remove any conflicting regions */
6598 n
= TAILQ_FIRST(&s
->rl
);
6601 n
= TAILQ_NEXT(r
, entry
);
6602 if (X(r
) < (x
+ w
) &&
6603 (X(r
) + WIDTH(r
)) > x
&&
6605 (Y(r
) + HEIGHT(r
)) > y
) {
6606 if (r
->ws
->r
!= NULL
)
6607 r
->ws
->old_r
= r
->ws
->r
;
6609 XDestroyWindow(display
, r
->bar_window
);
6610 TAILQ_REMOVE(&s
->rl
, r
, entry
);
6611 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
6615 /* search old regions for one to reuse */
6617 /* size + location match */
6618 TAILQ_FOREACH(r
, &s
->orl
, entry
)
6619 if (X(r
) == x
&& Y(r
) == y
&&
6620 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
6624 TAILQ_FOREACH(r
, &s
->orl
, entry
)
6625 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
6629 TAILQ_REMOVE(&s
->orl
, r
, entry
);
6630 /* try to use old region's workspace */
6631 if (r
->ws
->r
== NULL
)
6634 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
6635 err(1, "new_region: calloc: failed to allocate memory "
6638 /* if we don't have a workspace already, find one */
6640 for (i
= 0; i
< SWM_WS_MAX
; i
++)
6641 if (s
->ws
[i
].r
== NULL
) {
6648 errx(1, "new_region: no free workspaces");
6659 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
6665 #ifdef SWM_XRR_HAS_CRTC
6667 XRRScreenResources
*sr
;
6670 #endif /* SWM_XRR_HAS_CRTC */
6671 struct swm_region
*r
;
6674 if (i
>= ScreenCount(display
))
6675 errx(1, "scan_xrandr: invalid screen");
6677 /* remove any old regions */
6678 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
6679 r
->ws
->old_r
= r
->ws
->r
= NULL
;
6680 XDestroyWindow(display
, r
->bar_window
);
6681 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
6682 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
6686 /* map virtual screens onto physical screens */
6687 #ifdef SWM_XRR_HAS_CRTC
6688 if (xrandr_support
) {
6689 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
6691 new_region(&screens
[i
], 0, 0,
6692 DisplayWidth(display
, i
),
6693 DisplayHeight(display
, i
));
6697 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
6698 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
6699 if (ci
->noutput
== 0)
6702 if (ci
!= NULL
&& ci
->mode
== None
)
6703 new_region(&screens
[i
], 0, 0,
6704 DisplayWidth(display
, i
),
6705 DisplayHeight(display
, i
));
6707 new_region(&screens
[i
],
6708 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
6711 XRRFreeCrtcInfo(ci
);
6712 XRRFreeScreenResources(sr
);
6714 #endif /* SWM_XRR_HAS_CRTC */
6716 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
6717 DisplayHeight(display
, i
));
6722 screenchange(XEvent
*e
) {
6723 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
6724 struct swm_region
*r
;
6727 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%lx\n", xe
->root
);
6729 if (!XRRUpdateConfiguration(e
))
6732 /* silly event doesn't include the screen index */
6733 for (i
= 0; i
< ScreenCount(display
); i
++)
6734 if (screens
[i
].root
== xe
->root
)
6736 if (i
>= ScreenCount(display
))
6737 errx(1, "screenchange: screen not found");
6739 /* brute force for now, just re-enumerate the regions */
6742 /* add bars to all regions */
6743 for (i
= 0; i
< ScreenCount(display
); i
++)
6744 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6747 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6748 drain_enter_notify();
6754 Window d1
, d2
, *wins
= NULL
;
6755 XWindowAttributes wa
;
6760 for (i
= 0; i
< ScreenCount(display
); i
++) {
6761 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
6764 /* attach windows to a region */
6765 /* normal windows */
6766 for (j
= 0; j
< no
; j
++) {
6767 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6768 wa
.override_redirect
||
6769 XGetTransientForHint(display
, wins
[j
], &d1
))
6772 state
= getstate(wins
[j
]);
6773 manage
= state
== IconicState
;
6774 if (wa
.map_state
== IsViewable
|| manage
)
6775 manage_window(wins
[j
]);
6777 /* transient windows */
6778 for (j
= 0; j
< no
; j
++) {
6779 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6780 wa
.override_redirect
)
6783 state
= getstate(wins
[j
]);
6784 manage
= state
== IconicState
;
6785 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
6787 manage_window(wins
[j
]);
6800 int errorbase
, major
, minor
;
6801 struct workspace
*ws
;
6804 if ((screens
= calloc(ScreenCount(display
),
6805 sizeof(struct swm_screen
))) == NULL
)
6806 err(1, "setup_screens: calloc: failed to allocate memory for "
6809 /* initial Xrandr setup */
6810 xrandr_support
= XRRQueryExtension(display
,
6811 &xrandr_eventbase
, &errorbase
);
6813 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
6816 /* map physical screens */
6817 for (i
= 0; i
< ScreenCount(display
); i
++) {
6818 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
6820 TAILQ_INIT(&screens
[i
].rl
);
6821 TAILQ_INIT(&screens
[i
].orl
);
6822 screens
[i
].root
= RootWindow(display
, i
);
6824 /* set default colors */
6825 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
6826 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
6827 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
6828 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
6829 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
6831 /* create graphics context on screen with default font color */
6832 screens
[i
].bar_gc
= XCreateGC(display
, screens
[i
].root
, 0,
6835 XSetForeground(display
, screens
[i
].bar_gc
,
6836 screens
[i
].c
[SWM_S_COLOR_BAR_FONT
].color
);
6838 /* set default cursor */
6839 XDefineCursor(display
, screens
[i
].root
,
6840 XCreateFontCursor(display
, XC_left_ptr
));
6842 /* init all workspaces */
6843 /* XXX these should be dynamically allocated too */
6844 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
6845 ws
= &screens
[i
].ws
[j
];
6851 TAILQ_INIT(&ws
->winlist
);
6852 TAILQ_INIT(&ws
->unmanagedlist
);
6854 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
6855 if (layouts
[k
].l_config
!= NULL
)
6856 layouts
[k
].l_config(ws
,
6857 SWM_ARG_ID_STACKINIT
);
6858 ws
->cur_layout
= &layouts
[0];
6859 ws
->cur_layout
->l_string(ws
);
6865 XRRSelectInput(display
, screens
[i
].root
,
6866 RRScreenChangeNotifyMask
);
6873 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
6874 err(1, "setup_globals: strdup: failed to allocate memory.");
6876 if ((spawn_term
[0] = strdup("xterm")) == NULL
)
6877 err(1, "setup_globals: strdup: failed to allocate memory.");
6879 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
6880 err(1, "setup_globals: strdup: failed to allocate memory.");
6887 Atom netwmcheck
, netwmname
, utf8_string
;
6890 /* work around sun jdk bugs, code from wmname */
6891 netwmcheck
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
6892 netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
6893 utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6894 for (i
= 0; i
< ScreenCount(display
); i
++) {
6895 root
= screens
[i
].root
;
6896 win
= XCreateSimpleWindow(display
,root
, 0, 0, 1, 1, 0,
6897 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
,
6898 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
);
6900 XChangeProperty(display
, root
, netwmcheck
, XA_WINDOW
, 32,
6901 PropModeReplace
, (unsigned char *)&win
,1);
6902 XChangeProperty(display
, win
, netwmcheck
, XA_WINDOW
, 32,
6903 PropModeReplace
, (unsigned char *)&win
,1);
6904 XChangeProperty(display
, win
, netwmname
, utf8_string
, 8,
6905 PropModeReplace
, (unsigned char*)"LG3D", strlen("LG3D"));
6910 main(int argc
, char *argv
[])
6912 struct swm_region
*r
, *rr
;
6913 struct ws_win
*winfocus
= NULL
;
6916 char conf
[PATH_MAX
], *cfile
= NULL
;
6921 struct sigaction sact
;
6924 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
6925 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
6927 warnx("no locale support");
6929 if (!X_HAVE_UTF8_STRING
)
6930 warnx("no UTF-8 support");
6932 if (!(display
= XOpenDisplay(0)))
6933 errx(1, "can not open display");
6936 errx(1, "other wm running");
6938 /* handle some signals */
6939 bzero(&sact
, sizeof(sact
));
6940 sigemptyset(&sact
.sa_mask
);
6942 sact
.sa_handler
= sighdlr
;
6943 sigaction(SIGINT
, &sact
, NULL
);
6944 sigaction(SIGQUIT
, &sact
, NULL
);
6945 sigaction(SIGTERM
, &sact
, NULL
);
6946 sigaction(SIGHUP
, &sact
, NULL
);
6948 sact
.sa_handler
= sighdlr
;
6949 sact
.sa_flags
= SA_NOCLDSTOP
;
6950 sigaction(SIGCHLD
, &sact
, NULL
);
6952 astate
= XInternAtom(display
, "WM_STATE", False
);
6953 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
6954 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
6955 takefocus
= XInternAtom(display
, "WM_TAKE_FOCUS", False
);
6956 a_wmname
= XInternAtom(display
, "WM_NAME", False
);
6957 a_netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
6958 a_utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6959 a_string
= XInternAtom(display
, "STRING", False
);
6960 a_swm_iconic
= XInternAtom(display
, "_SWM_ICONIC", False
);
6962 /* look for local and global conf file */
6963 pwd
= getpwuid(getuid());
6965 errx(1, "invalid user: %d", getuid());
6974 for (i
= 0; ; i
++) {
6979 snprintf(conf
, sizeof conf
, "%s/.%s",
6980 pwd
->pw_dir
, SWM_CONF_FILE
);
6984 snprintf(conf
, sizeof conf
, "/etc/%s",
6989 snprintf(conf
, sizeof conf
, "%s/.%s",
6990 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
6994 snprintf(conf
, sizeof conf
, "/etc/%s",
7001 if (strlen(conf
) && stat(conf
, &sb
) != -1)
7002 if (S_ISREG(sb
.st_mode
)) {
7009 /* load conf (if any) and refresh font color in bar graphics contexts */
7010 if (cfile
&& conf_load(cfile
, SWM_CONF_DEFAULT
) == 0)
7011 for (i
= 0; i
< ScreenCount(display
); ++i
)
7012 XSetForeground(display
, screens
[i
].bar_gc
,
7013 screens
[i
].c
[SWM_S_COLOR_BAR_FONT
].color
);
7016 /* set some values to work around bad programs */
7018 /* grab existing windows (before we build the bars) */
7021 if (getenv("SWM_STARTED") == NULL
)
7022 setenv("SWM_STARTED", "YES", 1);
7024 /* setup all bars */
7025 for (i
= 0; i
< ScreenCount(display
); i
++)
7026 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
7027 if (winfocus
== NULL
)
7028 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
7036 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7037 drain_enter_notify();
7039 xfd
= ConnectionNumber(display
);
7041 while (XPending(display
)) {
7042 XNextEvent(display
, &e
);
7045 if (e
.type
< LASTEvent
) {
7046 DNPRINTF(SWM_D_EVENTQ
,"XEvent: handled: %s, "
7047 "window: 0x%lx, type: %s (%d), %d remaining"
7048 "\n", YESNO(handler
[e
.type
]),
7049 e
.xany
.window
, geteventname(&e
),
7050 e
.type
, QLength(display
));
7052 if (handler
[e
.type
])
7053 handler
[e
.type
](&e
);
7055 DNPRINTF(SWM_D_EVENTQ
, "XRandr Event: window: "
7056 "0x%lx, type: %s (%d)\n", e
.xany
.window
,
7057 xrandr_geteventname(&e
), e
.type
);
7059 switch (e
.type
- xrandr_eventbase
) {
7060 case RRScreenChangeNotify
:
7069 /* if we are being restarted go focus on first window */
7071 rr
= winfocus
->ws
->r
;
7073 /* not a visible window */
7077 /* move pointer to first screen if multi screen */
7078 if (ScreenCount(display
) > 1 || outputs
> 1)
7079 XWarpPointer(display
, None
, rr
->s
[0].root
,
7081 Y(rr
) + (bar_enabled
? bar_height
: 0));
7083 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7093 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7095 DNPRINTF(SWM_D_MISC
, "select failed");
7096 if (restart_wm
== 1)
7097 restart(NULL
, NULL
);
7098 if (search_resp
== 1)
7111 for (i
= 0; i
< ScreenCount(display
); ++i
)
7112 if (screens
[i
].bar_gc
!= NULL
)
7113 XFreeGC(display
, screens
[i
].bar_gc
);
7115 XFreeFontSet(display
, bar_fs
);
7116 XCloseDisplay(display
);