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>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Much code and ideas taken from dwm under the following license:
24 * MIT/X Consortium License
26 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
27 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
28 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
29 * 2007 Premysl Hruby <dfenze at gmail dot com>
30 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
31 * 2007 Christof Musik <christof at sendfax dot de>
32 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
33 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
34 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
36 * Permission is hereby granted, free of charge, to any person obtaining a
37 * copy of this software and associated documentation files (the "Software"),
38 * to deal in the Software without restriction, including without limitation
39 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
40 * and/or sell copies of the Software, and to permit persons to whom the
41 * Software is furnished to do so, subject to the following conditions:
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
49 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
51 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
52 * DEALINGS IN THE SOFTWARE.
70 #include <sys/types.h>
74 #include <sys/queue.h>
75 #include <sys/param.h>
76 #include <sys/select.h>
78 #include <X11/cursorfont.h>
79 #include <X11/keysym.h>
80 #include <X11/Xatom.h>
82 #include <X11/Xproto.h>
83 #include <X11/Xutil.h>
84 #include <X11/extensions/Xrandr.h>
85 #include <X11/extensions/XTest.h>
93 #ifdef SCROTWM_BUILDSTR
94 static const char *buildstr
= SCROTWM_BUILDSTR
;
96 static const char *buildstr
= SCROTWM_VERSION
;
100 # error XRandR versions less than 1.0 are not supported
105 #define SWM_XRR_HAS_CRTC
109 /*#define SWM_DEBUG*/
111 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
112 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
113 #define SWM_D_MISC 0x0001
114 #define SWM_D_EVENT 0x0002
115 #define SWM_D_WS 0x0004
116 #define SWM_D_FOCUS 0x0008
117 #define SWM_D_MOVE 0x0010
118 #define SWM_D_STACK 0x0020
119 #define SWM_D_MOUSE 0x0040
120 #define SWM_D_PROP 0x0080
121 #define SWM_D_CLASS 0x0100
122 #define SWM_D_KEY 0x0200
123 #define SWM_D_QUIRK 0x0400
124 #define SWM_D_SPAWN 0x0800
125 #define SWM_D_EVENTQ 0x1000
126 #define SWM_D_CONF 0x2000
127 #define SWM_D_BAR 0x4000
129 u_int32_t swm_debug
= 0
147 #define DPRINTF(x...)
148 #define DNPRINTF(n,x...)
151 #define LENGTH(x) (sizeof x / sizeof x[0])
152 #define MODKEY Mod1Mask
153 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
154 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
155 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
156 #define SWM_PROPLEN (16)
157 #define SWM_FUNCNAME_LEN (32)
158 #define SWM_KEYS_LEN (255)
159 #define SWM_QUIRK_LEN (64)
160 #define X(r) (r)->g.x
161 #define Y(r) (r)->g.y
162 #define WIDTH(r) (r)->g.w
163 #define HEIGHT(r) (r)->g.h
164 #define SH_MIN(w) (w)->sh_mask & PMinSize
165 #define SH_MIN_W(w) (w)->sh.min_width
166 #define SH_MIN_H(w) (w)->sh.min_height
167 #define SH_MAX(w) (w)->sh_mask & PMaxSize
168 #define SH_MAX_W(w) (w)->sh.max_width
169 #define SH_MAX_H(w) (w)->sh.max_height
170 #define SH_INC(w) (w)->sh_mask & PResizeInc
171 #define SH_INC_W(w) (w)->sh.width_inc
172 #define SH_INC_H(w) (w)->sh.height_inc
173 #define SWM_MAX_FONT_STEPS (3)
174 #define WINID(w) ((w) ? (w)->id : 0)
175 #define YESNO(x) ((x) ? "yes" : "no")
177 #define SWM_FOCUS_DEFAULT (0)
178 #define SWM_FOCUS_SYNERGY (1)
179 #define SWM_FOCUS_FOLLOW (2)
181 #define SWM_CONF_DEFAULT (0)
182 #define SWM_CONF_KEYMAPPING (1)
185 #define SWM_LIB "/usr/local/lib/libswmhack.so"
198 volatile sig_atomic_t running
= 1;
199 volatile sig_atomic_t restart_wm
= 0;
201 int last_focus_event
= FocusOut
;
202 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
206 int xrandr_eventbase
;
207 unsigned int numlockmask
= 0;
211 int cycle_visible
= 0;
213 int font_adjusted
= 0;
214 unsigned int mod_key
= MODKEY
;
217 struct swm_region
*search_r
;
218 int select_list_pipe
[2];
219 int select_resp_pipe
[2];
221 volatile sig_atomic_t search_resp
;
222 int search_resp_action
;
224 struct search_window
{
225 TAILQ_ENTRY(search_window
) entry
;
231 TAILQ_HEAD(search_winlist
, search_window
);
233 struct search_winlist search_wl
;
238 SWM_SEARCH_UNICONIFY
,
239 SWM_SEARCH_NAME_WORKSPACE
,
240 SWM_SEARCH_SEARCH_WORKSPACE
,
241 SWM_SEARCH_SEARCH_WINDOW
245 double dialog_ratio
= 0.6;
247 #define SWM_BAR_MAX (256)
248 #define SWM_BAR_JUSTIFY_LEFT (0)
249 #define SWM_BAR_JUSTIFY_CENTER (1)
250 #define SWM_BAR_JUSTIFY_RIGHT (2)
251 #define SWM_BAR_OFFSET (4)
252 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
253 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
254 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
255 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"
257 #ifdef X_HAVE_UTF8_STRING
258 #define DRAWSTRING(x...) Xutf8DrawString(x)
260 #define DRAWSTRING(x...) XmbDrawString(x)
263 char *bar_argv
[] = { NULL
, NULL
};
265 unsigned char bar_ext
[SWM_BAR_MAX
];
266 char bar_vertext
[SWM_BAR_MAX
];
268 sig_atomic_t bar_alarm
= 0;
271 int bar_border_width
= 1;
272 int bar_at_bottom
= 0;
274 int bar_extra_running
= 0;
277 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
278 int stack_enabled
= 1;
279 int clock_enabled
= 1;
280 int urgent_enabled
= 0;
281 char *clock_format
= NULL
;
282 int title_name_enabled
= 0;
283 int title_class_enabled
= 0;
284 int window_name_enabled
= 0;
285 int focus_mode
= SWM_FOCUS_DEFAULT
;
286 int disable_border
= 0;
287 int border_width
= 1;
288 int verbose_layout
= 0;
291 XFontSetExtents
*bar_fs_extents
;
293 char *spawn_term
[] = { NULL
, NULL
}; /* XXX fully dynamic */
296 #define SWM_MENU_FN (2)
297 #define SWM_MENU_NB (4)
298 #define SWM_MENU_NF (6)
299 #define SWM_MENU_SB (8)
300 #define SWM_MENU_SF (10)
302 /* layout manager data */
303 struct swm_geometry
{
313 /* virtual "screens" */
315 TAILQ_ENTRY(swm_region
) entry
;
316 struct swm_geometry g
;
317 struct workspace
*ws
; /* current workspace on this region */
318 struct workspace
*ws_prior
; /* prior workspace on this region */
319 struct swm_screen
*s
; /* screen idx */
322 TAILQ_HEAD(swm_region_list
, swm_region
);
325 TAILQ_ENTRY(ws_win
) entry
;
328 struct ws_win
*child_trans
; /* transient child window */
329 struct swm_geometry g
; /* current geometry */
330 struct swm_geometry g_float
; /* geometry when floating */
331 struct swm_geometry rg_float
; /* region geom when floating */
332 int g_floatvalid
; /* g_float geometry validity */
333 int floatmaxed
; /* whether maxed by max_stack */
337 unsigned int ewmh_flags
;
338 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
344 unsigned long quirks
;
345 struct workspace
*ws
; /* always valid */
346 struct swm_screen
*s
; /* always valid, never changes */
347 XWindowAttributes wa
;
353 TAILQ_HEAD(ws_win_list
, ws_win
);
357 TAILQ_ENTRY(pid_e
) entry
;
361 TAILQ_HEAD(pid_list
, pid_e
);
362 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
364 /* layout handlers */
366 void vertical_config(struct workspace
*, int);
367 void vertical_stack(struct workspace
*, struct swm_geometry
*);
368 void horizontal_config(struct workspace
*, int);
369 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
370 void max_stack(struct workspace
*, struct swm_geometry
*);
371 void plain_stacker(struct workspace
*);
372 void fancy_stacker(struct workspace
*);
374 struct ws_win
*find_window(Window
);
376 void grabbuttons(struct ws_win
*, int);
377 void new_region(struct swm_screen
*, int, int, int, int);
378 void unmanage_window(struct ws_win
*);
379 long getstate(Window
);
381 int conf_load(char *, int);
384 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
385 void (*l_config
)(struct workspace
*, int);
387 #define SWM_L_FOCUSPREV (1<<0)
388 #define SWM_L_MAPONFOCUS (1<<1)
389 void (*l_string
)(struct workspace
*);
391 /* stack, configure */
392 { vertical_stack
, vertical_config
, 0, plain_stacker
},
393 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
395 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
396 { NULL
, NULL
, 0, NULL
},
399 /* position of max_stack mode in the layouts array, index into layouts! */
400 #define SWM_V_STACK (0)
401 #define SWM_H_STACK (1)
402 #define SWM_MAX_STACK (2)
404 #define SWM_H_SLICE (32)
405 #define SWM_V_SLICE (32)
407 /* define work spaces */
409 int idx
; /* workspace index */
410 char *name
; /* workspace name */
411 int always_raise
; /* raise windows on focus */
412 struct layout
*cur_layout
; /* current layout handlers */
413 struct ws_win
*focus
; /* may be NULL */
414 struct ws_win
*focus_prev
; /* may be NULL */
415 struct swm_region
*r
; /* may be NULL */
416 struct swm_region
*old_r
; /* may be NULL */
417 struct ws_win_list winlist
; /* list of windows in ws */
418 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
419 char stacker
[10]; /* display stacker and layout */
423 int horizontal_msize
;
425 int horizontal_stacks
;
434 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
435 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
437 /* physical screen mapping */
438 #define SWM_WS_MAX (10)
440 int idx
; /* screen index */
441 struct swm_region_list rl
; /* list of regions on this screen */
442 struct swm_region_list orl
; /* list of old regions */
444 struct workspace ws
[SWM_WS_MAX
];
450 } c
[SWM_S_COLOR_MAX
];
454 struct swm_screen
*screens
;
457 /* args to functions */
460 #define SWM_ARG_ID_FOCUSNEXT (0)
461 #define SWM_ARG_ID_FOCUSPREV (1)
462 #define SWM_ARG_ID_FOCUSMAIN (2)
463 #define SWM_ARG_ID_FOCUSCUR (4)
464 #define SWM_ARG_ID_SWAPNEXT (10)
465 #define SWM_ARG_ID_SWAPPREV (11)
466 #define SWM_ARG_ID_SWAPMAIN (12)
467 #define SWM_ARG_ID_MOVELAST (13)
468 #define SWM_ARG_ID_MASTERSHRINK (20)
469 #define SWM_ARG_ID_MASTERGROW (21)
470 #define SWM_ARG_ID_MASTERADD (22)
471 #define SWM_ARG_ID_MASTERDEL (23)
472 #define SWM_ARG_ID_FLIPLAYOUT (24)
473 #define SWM_ARG_ID_STACKRESET (30)
474 #define SWM_ARG_ID_STACKINIT (31)
475 #define SWM_ARG_ID_CYCLEWS_UP (40)
476 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
477 #define SWM_ARG_ID_CYCLESC_UP (42)
478 #define SWM_ARG_ID_CYCLESC_DOWN (43)
479 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
480 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
481 #define SWM_ARG_ID_STACKINC (50)
482 #define SWM_ARG_ID_STACKDEC (51)
483 #define SWM_ARG_ID_SS_ALL (60)
484 #define SWM_ARG_ID_SS_WINDOW (61)
485 #define SWM_ARG_ID_DONTCENTER (70)
486 #define SWM_ARG_ID_CENTER (71)
487 #define SWM_ARG_ID_KILLWINDOW (80)
488 #define SWM_ARG_ID_DELETEWINDOW (81)
489 #define SWM_ARG_ID_WIDTHGROW (90)
490 #define SWM_ARG_ID_WIDTHSHRINK (91)
491 #define SWM_ARG_ID_HEIGHTGROW (92)
492 #define SWM_ARG_ID_HEIGHTSHRINK (93)
493 #define SWM_ARG_ID_MOVEUP (100)
494 #define SWM_ARG_ID_MOVEDOWN (101)
495 #define SWM_ARG_ID_MOVELEFT (102)
496 #define SWM_ARG_ID_MOVERIGHT (103)
500 void focus(struct swm_region
*, union arg
*);
501 void focus_magic(struct ws_win
*);
505 TAILQ_ENTRY(quirk
) entry
;
509 #define SWM_Q_FLOAT (1<<0) /* float this window */
510 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
511 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
512 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
513 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
514 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
516 TAILQ_HEAD(quirk_list
, quirk
);
517 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
520 * Supported EWMH hints should be added to
521 * both the enum and the ewmh array
523 enum { _NET_ACTIVE_WINDOW
, _NET_MOVERESIZE_WINDOW
, _NET_CLOSE_WINDOW
,
524 _NET_WM_WINDOW_TYPE
, _NET_WM_WINDOW_TYPE_DOCK
,
525 _NET_WM_WINDOW_TYPE_TOOLBAR
, _NET_WM_WINDOW_TYPE_UTILITY
,
526 _NET_WM_WINDOW_TYPE_SPLASH
, _NET_WM_WINDOW_TYPE_DIALOG
,
527 _NET_WM_WINDOW_TYPE_NORMAL
, _NET_WM_STATE
,
528 _NET_WM_STATE_MAXIMIZED_HORZ
, _NET_WM_STATE_MAXIMIZED_VERT
,
529 _NET_WM_STATE_SKIP_TASKBAR
, _NET_WM_STATE_SKIP_PAGER
,
530 _NET_WM_STATE_HIDDEN
, _NET_WM_STATE_ABOVE
, _SWM_WM_STATE_MANUAL
,
531 _NET_WM_STATE_FULLSCREEN
, _NET_WM_ALLOWED_ACTIONS
, _NET_WM_ACTION_MOVE
,
532 _NET_WM_ACTION_RESIZE
, _NET_WM_ACTION_FULLSCREEN
, _NET_WM_ACTION_CLOSE
,
538 } ewmh
[SWM_EWMH_HINT_MAX
] = {
539 /* must be in same order as in the enum */
540 {"_NET_ACTIVE_WINDOW", None
},
541 {"_NET_MOVERESIZE_WINDOW", None
},
542 {"_NET_CLOSE_WINDOW", None
},
543 {"_NET_WM_WINDOW_TYPE", None
},
544 {"_NET_WM_WINDOW_TYPE_DOCK", None
},
545 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None
},
546 {"_NET_WM_WINDOW_TYPE_UTILITY", None
},
547 {"_NET_WM_WINDOW_TYPE_SPLASH", None
},
548 {"_NET_WM_WINDOW_TYPE_DIALOG", None
},
549 {"_NET_WM_WINDOW_TYPE_NORMAL", None
},
550 {"_NET_WM_STATE", None
},
551 {"_NET_WM_STATE_MAXIMIZED_HORZ", None
},
552 {"_NET_WM_STATE_MAXIMIZED_VERT", None
},
553 {"_NET_WM_STATE_SKIP_TASKBAR", None
},
554 {"_NET_WM_STATE_SKIP_PAGER", None
},
555 {"_NET_WM_STATE_HIDDEN", None
},
556 {"_NET_WM_STATE_ABOVE", None
},
557 {"_SWM_WM_STATE_MANUAL", None
},
558 {"_NET_WM_STATE_FULLSCREEN", None
},
559 {"_NET_WM_ALLOWED_ACTIONS", None
},
560 {"_NET_WM_ACTION_MOVE", None
},
561 {"_NET_WM_ACTION_RESIZE", None
},
562 {"_NET_WM_ACTION_FULLSCREEN", None
},
563 {"_NET_WM_ACTION_CLOSE", None
},
566 void store_float_geom(struct ws_win
*, struct swm_region
*);
567 int floating_toggle_win(struct ws_win
*);
568 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
569 unsigned char *get_win_name(Window
);
572 get_property(Window id
, Atom atom
, long count
, Atom type
, unsigned long *nitems
,
573 unsigned long *nbytes
, unsigned char **data
)
576 unsigned long *nbytes_ret
, *nitems_ret
;
577 unsigned long nbytes_tmp
, nitems_tmp
;
580 nbytes_ret
= nbytes
!= NULL
? nbytes
: &nbytes_tmp
;
581 nitems_ret
= nitems
!= NULL
? nitems
: &nitems_tmp
;
583 status
= XGetWindowProperty(display
, id
, atom
, 0L, count
, False
, type
,
584 &real
, &format
, nitems_ret
, nbytes_ret
, data
);
586 if (status
!= Success
)
595 update_iconic(struct ws_win
*win
, int newv
)
602 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
606 XChangeProperty(display
, win
->id
, iprop
, XA_INTEGER
, 32,
607 PropModeReplace
, (unsigned char *)&v
, 1);
609 XDeleteProperty(display
, win
->id
, iprop
);
613 get_iconic(struct ws_win
*win
)
618 unsigned long nitems
, extra
;
619 unsigned char *prop
= NULL
;
621 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
624 status
= XGetWindowProperty(display
, win
->id
, iprop
, 0L, 1L,
625 False
, XA_INTEGER
, &rettype
, &retfmt
, &nitems
, &extra
, &prop
);
626 if (status
!= Success
)
628 if (rettype
!= XA_INTEGER
|| retfmt
!= 32)
632 v
= *((int32_t *)prop
);
646 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
648 for (i
= 0; i
< LENGTH(ewmh
); i
++)
649 ewmh
[i
].atom
= XInternAtom(display
, ewmh
[i
].name
, False
);
651 for (i
= 0; i
< ScreenCount(display
); i
++) {
652 /* Support check window will be created by workaround(). */
654 /* Report supported atoms */
655 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
656 for (j
= 0; j
< LENGTH(ewmh
); j
++)
657 XChangeProperty(display
, screens
[i
].root
,
658 sup_list
, XA_ATOM
, 32,
659 PropModeAppend
, (unsigned char *)&ewmh
[j
].atom
,1);
667 unsigned char *data
= NULL
;
669 Atom sup_check
, sup_list
;
672 sup_check
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
673 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
675 for (i
= 0; i
< ScreenCount(display
); i
++) {
676 /* Get the support check window and destroy it */
677 success
= get_property(screens
[i
].root
, sup_check
, 1, XA_WINDOW
,
682 XDestroyWindow(display
, id
);
683 XDeleteProperty(display
, screens
[i
].root
, sup_check
);
684 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
692 ewmh_autoquirk(struct ws_win
*win
)
695 unsigned long *data
= NULL
, n
;
698 success
= get_property(win
->id
, ewmh
[_NET_WM_WINDOW_TYPE
].atom
, (~0L),
699 XA_ATOM
, &n
, NULL
, (void *)&data
);
706 for (i
= 0; i
< n
; i
++) {
708 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
710 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
711 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
712 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
714 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
717 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
718 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
720 win
->quirks
= SWM_Q_FLOAT
;
728 #define SWM_EWMH_ACTION_COUNT_MAX (6)
729 #define EWMH_F_FULLSCREEN (1<<0)
730 #define EWMH_F_ABOVE (1<<1)
731 #define EWMH_F_HIDDEN (1<<2)
732 #define EWMH_F_SKIP_PAGER (1<<3)
733 #define EWMH_F_SKIP_TASKBAR (1<<4)
734 #define SWM_F_MANUAL (1<<5)
737 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
739 struct swm_geometry rg
;
747 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%lx, "
748 "fullscreen %s\n", win
->id
, YESNO(fs
));
753 store_float_geom(win
, win
->ws
->r
);
757 if (win
->g_floatvalid
) {
758 /* refloat at last floating relative position */
759 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ rg
.x
;
760 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ rg
.y
;
761 WIDTH(win
) = win
->g_float
.w
;
762 HEIGHT(win
) = win
->g_float
.h
;
770 ewmh_update_actions(struct ws_win
*win
)
772 Atom actions
[SWM_EWMH_ACTION_COUNT_MAX
];
778 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
781 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
782 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
785 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
,
786 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)actions
, n
);
789 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
790 #define _NET_WM_STATE_ADD 1 /* add/set property */
791 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
794 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
796 unsigned int mask
= 0;
797 unsigned int changed
= 0;
798 unsigned int orig_flags
;
803 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
804 mask
= EWMH_F_FULLSCREEN
;
805 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
807 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
809 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
810 mask
= EWMH_F_SKIP_PAGER
;
811 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
812 mask
= EWMH_F_SKIP_TASKBAR
;
815 orig_flags
= win
->ewmh_flags
;
818 case _NET_WM_STATE_REMOVE
:
819 win
->ewmh_flags
&= ~mask
;
821 case _NET_WM_STATE_ADD
:
822 win
->ewmh_flags
|= mask
;
824 case _NET_WM_STATE_TOGGLE
:
825 win
->ewmh_flags
^= mask
;
829 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
831 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
833 if (!floating_toggle_win(win
))
834 win
->ewmh_flags
= orig_flags
; /* revert */
835 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
837 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
838 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
840 if (!ewmh_set_win_fullscreen(win
,
841 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
842 win
->ewmh_flags
= orig_flags
; /* revert */
844 XDeleteProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
846 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
847 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
848 XA_ATOM
, 32, PropModeAppend
,
849 (unsigned char *)&ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
, 1);
850 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
851 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
852 XA_ATOM
, 32, PropModeAppend
,
853 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
, 1);
854 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
855 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
856 XA_ATOM
, 32, PropModeAppend
,
857 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
, 1);
858 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
859 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
860 XA_ATOM
, 32, PropModeAppend
,
861 (unsigned char *)&ewmh
[_NET_WM_STATE_ABOVE
].atom
, 1);
862 if (win
->ewmh_flags
& SWM_F_MANUAL
)
863 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
864 XA_ATOM
, 32, PropModeAppend
,
865 (unsigned char *)&ewmh
[_SWM_WM_STATE_MANUAL
].atom
, 1);
869 ewmh_get_win_state(struct ws_win
*win
)
880 win
->ewmh_flags
|= EWMH_F_ABOVE
;
882 win
->ewmh_flags
|= SWM_F_MANUAL
;
884 success
= get_property(win
->id
, ewmh
[_NET_WM_STATE
].atom
,
885 (~0L), XA_ATOM
, &n
, NULL
, (void *)&states
);
890 for (i
= 0; i
< n
; i
++)
891 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
899 geteventname(XEvent
*e
)
911 name
= "ButtonPress";
914 name
= "ButtonRelease";
917 name
= "MotionNotify";
920 name
= "EnterNotify";
923 name
= "LeaveNotify";
932 name
= "KeymapNotify";
938 name
= "GraphicsExpose";
943 case VisibilityNotify
:
944 name
= "VisibilityNotify";
947 name
= "CreateNotify";
950 name
= "DestroyNotify";
953 name
= "UnmapNotify";
962 name
= "ReparentNotify";
964 case ConfigureNotify
:
965 name
= "ConfigureNotify";
967 case ConfigureRequest
:
968 name
= "ConfigureRequest";
971 name
= "GravityNotify";
974 name
= "ResizeRequest";
976 case CirculateNotify
:
977 name
= "CirculateNotify";
979 case CirculateRequest
:
980 name
= "CirculateRequest";
983 name
= "PropertyNotify";
986 name
= "SelectionClear";
988 case SelectionRequest
:
989 name
= "SelectionRequest";
991 case SelectionNotify
:
992 name
= "SelectionNotify";
995 name
= "ColormapNotify";
998 name
= "ClientMessage";
1001 name
= "MappingNotify";
1011 xrandr_geteventname(XEvent
*e
)
1015 switch(e
->type
- xrandr_eventbase
) {
1016 case RRScreenChangeNotify
:
1017 name
= "RRScreenChangeNotify";
1027 dumpwins(struct swm_region
*r
, union arg
*args
)
1031 XWindowAttributes wa
;
1033 if (r
->ws
== NULL
) {
1034 warnx("dumpwins: invalid workspace");
1038 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1040 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1041 state
= getstate(win
->id
);
1042 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1043 warnx("window: 0x%lx, failed XGetWindowAttributes",
1045 warnx("window: 0x%lx, map_state: %d, state: %d, "
1046 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1050 warnx("===== unmanaged window list =====");
1051 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, 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("=================================");
1065 dumpwins(struct swm_region
*r
, union arg
*args
)
1068 #endif /* SWM_DEBUG */
1070 void expose(XEvent
*);
1071 void keypress(XEvent
*);
1072 void buttonpress(XEvent
*);
1073 void configurerequest(XEvent
*);
1074 void configurenotify(XEvent
*);
1075 void destroynotify(XEvent
*);
1076 void enternotify(XEvent
*);
1077 void focusevent(XEvent
*);
1078 void mapnotify(XEvent
*);
1079 void mappingnotify(XEvent
*);
1080 void maprequest(XEvent
*);
1081 void propertynotify(XEvent
*);
1082 void unmapnotify(XEvent
*);
1083 void visibilitynotify(XEvent
*);
1084 void clientmessage(XEvent
*);
1086 void (*handler
[LASTEvent
])(XEvent
*) = {
1088 [KeyPress
] = keypress
,
1089 [ButtonPress
] = buttonpress
,
1090 [ConfigureRequest
] = configurerequest
,
1091 [ConfigureNotify
] = configurenotify
,
1092 [DestroyNotify
] = destroynotify
,
1093 [EnterNotify
] = enternotify
,
1094 [FocusIn
] = focusevent
,
1095 [FocusOut
] = focusevent
,
1096 [MapNotify
] = mapnotify
,
1097 [MappingNotify
] = mappingnotify
,
1098 [MapRequest
] = maprequest
,
1099 [PropertyNotify
] = propertynotify
,
1100 [UnmapNotify
] = unmapnotify
,
1101 [VisibilityNotify
] = visibilitynotify
,
1102 [ClientMessage
] = clientmessage
,
1108 int saved_errno
, status
;
1111 saved_errno
= errno
;
1115 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1120 if (errno
!= ECHILD
)
1121 warn("sighdlr: waitpid");
1122 #endif /* SWM_DEBUG */
1125 if (pid
== searchpid
)
1129 if (WIFEXITED(status
)) {
1130 if (WEXITSTATUS(status
) != 0)
1131 warnx("sighdlr: child exit status: %d",
1132 WEXITSTATUS(status
));
1134 warnx("sighdlr: child is terminated "
1136 #endif /* SWM_DEBUG */
1150 errno
= saved_errno
;
1156 struct pid_e
*p
= NULL
;
1158 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1163 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1172 name_to_color(char *colorname
)
1176 XColor screen_def
, exact_def
;
1177 unsigned long result
= 0;
1178 char cname
[32] = "#";
1180 cmap
= DefaultColormap(display
, screens
[0].idx
);
1181 status
= XAllocNamedColor(display
, cmap
, colorname
,
1182 &screen_def
, &exact_def
);
1184 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1185 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
1189 result
= screen_def
.pixel
;
1191 warnx("color '%s' not found", colorname
);
1197 setscreencolor(char *val
, int i
, int c
)
1199 if (i
> 0 && i
<= ScreenCount(display
)) {
1200 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1201 free(screens
[i
- 1].c
[c
].name
);
1202 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1204 } else if (i
== -1) {
1205 for (i
= 0; i
< ScreenCount(display
); i
++) {
1206 screens
[i
].c
[c
].color
= name_to_color(val
);
1207 free(screens
[i
].c
[c
].name
);
1208 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1212 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1213 i
, ScreenCount(display
));
1217 fancy_stacker(struct workspace
*ws
)
1219 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1220 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1221 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1222 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1223 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1224 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1225 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1226 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1227 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1231 plain_stacker(struct workspace
*ws
)
1233 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1234 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1235 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1236 sizeof ws
->stacker
);
1237 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1238 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1239 sizeof ws
->stacker
);
1243 custom_region(char *val
)
1245 unsigned int sidx
, x
, y
, w
, h
;
1247 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1248 errx(1, "invalid custom region, "
1249 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1250 if (sidx
< 1 || sidx
> ScreenCount(display
))
1251 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1252 sidx
, ScreenCount(display
));
1256 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1258 if (x
> DisplayWidth(display
, sidx
) ||
1259 y
> DisplayHeight(display
, sidx
) ||
1260 w
+ x
> DisplayWidth(display
, sidx
) ||
1261 h
+ y
> DisplayHeight(display
, sidx
)) {
1262 warnx("ignoring region %ux%u+%u+%u - not within screen "
1263 "boundaries (%ux%u)", w
, h
, x
, y
,
1264 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
1268 new_region(&screens
[sidx
], x
, y
, w
, h
);
1272 socket_setnonblock(int fd
)
1276 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1277 err(1, "fcntl F_GETFL");
1278 flags
|= O_NONBLOCK
;
1279 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1280 err(1, "fcntl F_SETFL");
1284 bar_print(struct swm_region
*r
, char *s
)
1288 XRectangle ibox
, lbox
;
1290 XClearWindow(display
, r
->bar_window
);
1293 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1295 switch (bar_justify
) {
1296 case SWM_BAR_JUSTIFY_LEFT
:
1299 case SWM_BAR_JUSTIFY_CENTER
:
1300 x
= (WIDTH(r
) - lbox
.width
) / 2;
1302 case SWM_BAR_JUSTIFY_RIGHT
:
1303 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1307 if (x
< SWM_BAR_OFFSET
)
1310 DRAWSTRING(display
, r
->bar_window
, bar_fs
, r
->s
->bar_gc
,
1311 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1316 bar_extra_stop(void)
1320 bzero(bar_pipe
, sizeof bar_pipe
);
1323 kill(bar_pid
, SIGTERM
);
1326 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1331 bar_class_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1333 int do_class
, do_name
;
1335 XClassHint
*xch
= NULL
;
1337 if ((title_name_enabled
== 1 || title_class_enabled
== 1) &&
1338 cur_focus
!= NULL
) {
1339 if ((xch
= XAllocClassHint()) == NULL
)
1341 status
= XGetClassHint(display
, cur_focus
->id
, xch
);
1342 if (status
== BadWindow
|| status
== BadAlloc
)
1344 do_class
= (title_class_enabled
&& xch
->res_class
!= NULL
);
1345 do_name
= (title_name_enabled
&& xch
->res_name
!= NULL
);
1347 strlcat(s
, xch
->res_class
, sz
);
1348 if (do_class
&& do_name
)
1349 strlcat(s
, ":", sz
);
1351 strlcat(s
, xch
->res_name
, sz
);
1352 strlcat(s
, " ", sz
);
1356 XFree(xch
->res_name
);
1357 XFree(xch
->res_class
);
1363 bar_window_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1365 unsigned char *title
;
1367 if (window_name_enabled
&& cur_focus
!= NULL
) {
1368 title
= get_win_name(cur_focus
->id
);
1369 if (title
!= NULL
) {
1370 DNPRINTF(SWM_D_BAR
, "bar_window_name: title: %s\n",
1373 if (cur_focus
->floating
)
1374 strlcat(s
, "(f) ", sz
);
1375 strlcat(s
, (char *)title
, sz
);
1376 strlcat(s
, " ", sz
);
1382 int urgent
[SWM_WS_MAX
];
1384 bar_urgent(char *s
, ssize_t sz
)
1386 XWMHints
*wmh
= NULL
;
1391 if (urgent_enabled
== 0)
1394 for (i
= 0; i
< SWM_WS_MAX
; i
++)
1397 for (i
= 0; i
< ScreenCount(display
); i
++)
1398 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1399 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1400 wmh
= XGetWMHints(display
, win
->id
);
1404 if (wmh
->flags
& XUrgencyHint
)
1409 strlcat(s
, "* ", sz
);
1410 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
1412 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1414 snprintf(b
, sizeof b
, "- ");
1417 strlcat(s
, "* ", sz
);
1425 struct swm_region
*r
;
1428 char ws
[SWM_BAR_MAX
];
1429 char s
[SWM_BAR_MAX
];
1430 unsigned char cn
[SWM_BAR_MAX
];
1431 char loc
[SWM_BAR_MAX
];
1432 char *b
, *stack
= "";
1434 if (bar_enabled
== 0)
1436 if (bar_extra
&& bar_extra_running
) {
1437 /* ignore short reads; it'll correct itself */
1438 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1439 if (b
&& b
[len
- 1] == '\n') {
1441 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1443 if (b
== NULL
&& errno
!= EAGAIN
) {
1444 warn("bar_update: bar_extra failed");
1448 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1450 if (clock_enabled
== 0)
1451 strlcpy(s
, "", sizeof s
);
1454 localtime_r(&tmt
, &tm
);
1455 len
= strftime(s
, sizeof s
, clock_format
, &tm
);
1457 strlcat(s
, " ", sizeof s
);
1460 for (i
= 0; i
< ScreenCount(display
); i
++) {
1462 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1463 strlcpy((char *)cn
, "", sizeof cn
);
1464 strlcpy(ws
, "", sizeof ws
);
1466 bar_urgent((char *)cn
, sizeof cn
);
1467 bar_class_name((char *)cn
, sizeof cn
,
1469 bar_window_name((char *)cn
, sizeof cn
,
1472 snprintf(ws
, sizeof ws
, "<%s>",
1476 stack
= r
->ws
->stacker
;
1478 snprintf(loc
, sizeof loc
, "%d:%d %s %s %s%s %s "
1479 "%s", x
++, r
->ws
->idx
+ 1, stack
, ws
, s
, cn
,
1480 bar_ext
, bar_vertext
);
1488 bar_check_opts(void)
1490 if (title_class_enabled
|| title_name_enabled
|| window_name_enabled
)
1501 bar_toggle(struct swm_region
*r
, union arg
*args
)
1503 struct swm_region
*tmpr
;
1504 int i
, sc
= ScreenCount(display
);
1506 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1509 for (i
= 0; i
< sc
; i
++)
1510 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1511 XUnmapWindow(display
, tmpr
->bar_window
);
1513 for (i
= 0; i
< sc
; i
++)
1514 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1515 XMapRaised(display
, tmpr
->bar_window
);
1517 bar_enabled
= !bar_enabled
;
1520 /* must be after stack */
1527 XSetWindowAttributes wa
;
1528 struct swm_region
*r
;
1531 /* do this here because the conf file is in memory */
1532 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1533 /* launch external status app */
1534 bar_extra_running
= 1;
1535 if (pipe(bar_pipe
) == -1)
1536 err(1, "pipe error");
1537 socket_setnonblock(bar_pipe
[0]);
1538 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1539 if (dup2(bar_pipe
[0], 0) == -1)
1541 if (dup2(bar_pipe
[1], 1) == -1)
1543 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1544 err(1, "could not disable SIGPIPE");
1545 switch (bar_pid
= fork()) {
1547 err(1, "cannot fork");
1551 execvp(bar_argv
[0], bar_argv
);
1552 err(1, "%s external app failed", bar_argv
[0]);
1554 default: /* parent */
1560 bzero(&wa
, sizeof wa
);
1561 for (i
= 0; i
< ScreenCount(display
); i
++)
1562 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1564 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1565 wa
.background_pixel
=
1566 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1567 XChangeWindowAttributes(display
, r
->bar_window
,
1568 CWBackPixel
| CWBorderPixel
, &wa
);
1574 bar_setup(struct swm_region
*r
)
1576 char *default_string
;
1577 char **missing_charsets
;
1578 int num_missing_charsets
= 0;
1582 XFreeFontSet(display
, bar_fs
);
1587 DNPRINTF(SWM_D_BAR
, "bar_setup: loading bar_fonts: %s\n", bar_fonts
);
1589 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
1590 &num_missing_charsets
, &default_string
);
1592 if (num_missing_charsets
> 0) {
1593 warnx("Unable to load charset(s):");
1595 for (i
= 0; i
< num_missing_charsets
; ++i
)
1596 warnx("%s", missing_charsets
[i
]);
1598 XFreeStringList(missing_charsets
);
1600 if (strcmp(default_string
, ""))
1601 warnx("Glyphs from those sets will be replaced "
1602 "by '%s'.", default_string
);
1604 warnx("Glyphs from those sets won't be drawn.");
1608 errx(1, "Error creating font set structure.");
1610 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
1612 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
1613 2 * bar_border_width
;
1619 y
= bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1621 r
->bar_window
= XCreateSimpleWindow(display
,
1622 r
->s
->root
, x
, y
, WIDTH(r
) - 2 * bar_border_width
,
1623 bar_height
- 2 * bar_border_width
,
1624 bar_border_width
, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
1625 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1626 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
1628 XMapRaised(display
, r
->bar_window
);
1629 DNPRINTF(SWM_D_BAR
, "bar_setup: bar_window: 0x%lx\n", r
->bar_window
);
1631 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1632 err(1, "could not install bar_signal");
1637 drain_enter_notify(void)
1642 while (XCheckMaskEvent(display
, EnterWindowMask
, &cne
))
1645 DNPRINTF(SWM_D_MISC
, "drain_enter_notify: drained: %d\n", i
);
1649 set_win_state(struct ws_win
*win
, long state
)
1651 long data
[] = {state
, None
};
1653 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%lx\n", win
->id
);
1658 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1659 (unsigned char *)data
, 2);
1666 unsigned char *p
= NULL
;
1669 if (!get_property(w
, astate
, 2L, astate
, &n
, NULL
, &p
))
1672 result
= *((long *)p
);
1678 version(struct swm_region
*r
, union arg
*args
)
1680 bar_version
= !bar_version
;
1682 snprintf(bar_vertext
, sizeof bar_vertext
,
1683 "Version: %s Build: %s", SCROTWM_VERSION
, buildstr
);
1685 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
1690 client_msg(struct ws_win
*win
, Atom a
)
1692 XClientMessageEvent cm
;
1697 bzero(&cm
, sizeof cm
);
1698 cm
.type
= ClientMessage
;
1699 cm
.window
= win
->id
;
1700 cm
.message_type
= aprot
;
1703 cm
.data
.l
[1] = CurrentTime
;
1704 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
1707 /* synthetic response to a ConfigureRequest when not making a change */
1709 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
1716 /* send notification of unchanged state. */
1717 ce
.type
= ConfigureNotify
;
1720 ce
.width
= WIDTH(win
);
1721 ce
.height
= HEIGHT(win
);
1722 ce
.override_redirect
= False
;
1726 ce
.display
= display
;
1728 ce
.window
= win
->id
;
1729 ce
.border_width
= border_width
;
1733 ce
.display
= ev
->display
;
1734 ce
.event
= ev
->window
;
1735 ce
.window
= ev
->window
;
1737 /* make response appear more WM_SIZE_HINTS-compliant */
1739 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%lx,"
1740 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
1741 "%d x %d\n", win
->id
, win
->sh_mask
, SH_MIN_W(win
),
1742 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
1743 SH_INC_W(win
), SH_INC_H(win
));
1747 /* the hint may be set... to 0! */
1748 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
1749 ce
.width
= SH_MIN_W(win
);
1750 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
1751 ce
.height
= SH_MIN_H(win
);
1756 /* may also be advertized as 0 */
1757 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
1758 ce
.width
= SH_MAX_W(win
);
1759 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
1760 ce
.height
= SH_MAX_H(win
);
1763 /* resize increment. */
1765 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
1766 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
1768 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
1769 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
1773 /* adjust x and y for requested border_width. */
1774 ce
.x
+= border_width
- ev
->border_width
;
1775 ce
.y
+= border_width
- ev
->border_width
;
1776 ce
.border_width
= ev
->border_width
;
1777 ce
.above
= ev
->above
;
1780 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
1781 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
1782 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
1784 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
1788 count_win(struct workspace
*ws
, int count_transient
)
1793 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1794 if (count_transient
== 0 && win
->floating
)
1796 if (count_transient
== 0 && win
->transient
)
1802 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
1808 quit(struct swm_region
*r
, union arg
*args
)
1810 DNPRINTF(SWM_D_MISC
, "quit\n");
1815 unmap_window(struct ws_win
*win
)
1820 /* don't unmap again */
1821 if (getstate(win
->id
) == IconicState
)
1824 set_win_state(win
, IconicState
);
1826 XUnmapWindow(display
, win
->id
);
1827 XSetWindowBorder(display
, win
->id
,
1828 win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1837 for (i
= 0; i
< ScreenCount(display
); i
++)
1838 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1839 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1844 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
1851 event
.display
= display
; /* Ignored, but what the hell */
1852 event
.window
= win
->id
;
1853 event
.root
= win
->s
->root
;
1854 event
.subwindow
= None
;
1855 event
.time
= CurrentTime
;
1860 event
.same_screen
= True
;
1861 event
.keycode
= XKeysymToKeycode(display
, keysym
);
1862 event
.state
= modifiers
;
1864 event
.type
= KeyPress
;
1865 XSendEvent(event
.display
, event
.window
, True
,
1866 KeyPressMask
, (XEvent
*)&event
);
1868 event
.type
= KeyRelease
;
1869 XSendEvent(event
.display
, event
.window
, True
,
1870 KeyPressMask
, (XEvent
*)&event
);
1875 restart(struct swm_region
*r
, union arg
*args
)
1877 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
1879 /* disable alarm because the following code may not be interrupted */
1881 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
1882 err(1, "can't disable alarm");
1887 XCloseDisplay(display
);
1888 execvp(start_argv
[0], start_argv
);
1889 warn("execvp failed");
1894 root_to_region(Window root
)
1896 struct swm_region
*r
= NULL
;
1898 int i
, x
, y
, wx
, wy
;
1901 for (i
= 0; i
< ScreenCount(display
); i
++)
1902 if (screens
[i
].root
== root
)
1905 if (XQueryPointer(display
, screens
[i
].root
,
1906 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
1907 /* choose a region based on pointer location */
1908 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1909 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
1910 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
1915 r
= TAILQ_FIRST(&screens
[i
].rl
);
1921 find_unmanaged_window(Window id
)
1926 for (i
= 0; i
< ScreenCount(display
); i
++)
1927 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1928 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
1936 find_window(Window id
)
1939 Window wrr
, wpr
, *wcr
= NULL
;
1943 for (i
= 0; i
< ScreenCount(display
); i
++)
1944 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1945 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1949 /* if we were looking for the parent return that window instead */
1950 if (XQueryTree(display
, id
, &wrr
, &wpr
, &wcr
, &nc
) == 0)
1955 /* ignore not found and root */
1956 if (wpr
== 0 || wrr
== wpr
)
1959 /* look for parent */
1960 for (i
= 0; i
< ScreenCount(display
); i
++)
1961 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1962 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1970 spawn(int ws_idx
, union arg
*args
, int close_fd
)
1975 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
1978 close(ConnectionNumber(display
));
1980 setenv("LD_PRELOAD", SWM_LIB
, 1);
1982 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
1983 warn("spawn: asprintf SWM_WS");
1986 setenv("_SWM_WS", ret
, 1);
1990 if (asprintf(&ret
, "%d", getpid()) == -1) {
1991 warn("spawn: asprintf _SWM_PID");
1994 setenv("_SWM_PID", ret
, 1);
1998 if (setsid() == -1) {
1999 warn("spawn: setsid");
2005 * close stdin and stdout to prevent interaction between apps
2006 * and the baraction script
2007 * leave stderr open to record errors
2009 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2010 warn("spawn: open");
2013 dup2(fd
, STDIN_FILENO
);
2014 dup2(fd
, STDOUT_FILENO
);
2019 execvp(args
->argv
[0], args
->argv
);
2021 warn("spawn: execvp");
2026 spawnterm(struct swm_region
*r
, union arg
*args
)
2028 DNPRINTF(SWM_D_MISC
, "spawnterm\n");
2032 setenv("_SWM_XTERM_FONTADJ", "", 1);
2033 spawn(r
->ws
->idx
, args
, 1);
2038 kill_refs(struct ws_win
*win
)
2041 struct swm_region
*r
;
2042 struct workspace
*ws
;
2047 for (i
= 0; i
< ScreenCount(display
); i
++)
2048 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2049 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
2051 if (win
== ws
->focus
)
2053 if (win
== ws
->focus_prev
)
2054 ws
->focus_prev
= NULL
;
2059 validate_win(struct ws_win
*testwin
)
2062 struct workspace
*ws
;
2063 struct swm_region
*r
;
2066 if (testwin
== NULL
)
2069 for (i
= 0; i
< ScreenCount(display
); i
++)
2070 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2071 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
2073 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2081 validate_ws(struct workspace
*testws
)
2083 struct swm_region
*r
;
2084 struct workspace
*ws
;
2087 /* validate all ws */
2088 for (i
= 0; i
< ScreenCount(display
); i
++)
2089 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2090 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
2099 unfocus_win(struct ws_win
*win
)
2104 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%lx\n", WINID(win
));
2108 if (win
->ws
== NULL
)
2111 if (validate_ws(win
->ws
))
2112 return; /* XXX this gets hit with thunderbird, needs fixing */
2114 if (win
->ws
->r
== NULL
)
2117 if (validate_win(win
)) {
2122 if (win
->ws
->focus
== win
) {
2123 win
->ws
->focus
= NULL
;
2124 win
->ws
->focus_prev
= win
;
2127 if (validate_win(win
->ws
->focus
)) {
2128 kill_refs(win
->ws
->focus
);
2129 win
->ws
->focus
= NULL
;
2131 if (validate_win(win
->ws
->focus_prev
)) {
2132 kill_refs(win
->ws
->focus_prev
);
2133 win
->ws
->focus_prev
= NULL
;
2136 /* drain all previous unfocus events */
2137 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
2140 grabbuttons(win
, 0);
2141 XSetWindowBorder(display
, win
->id
,
2142 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2144 XChangeProperty(display
, win
->s
->root
,
2145 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2146 PropModeReplace
, (unsigned char *)&none
,1);
2155 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2157 for (i
= 0; i
< ScreenCount(display
); i
++)
2158 for (j
= 0; j
< SWM_WS_MAX
; j
++)
2159 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2164 focus_win(struct ws_win
*win
)
2169 struct ws_win
*cfw
= NULL
;
2172 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%lx\n", WINID(win
));
2176 if (win
->ws
== NULL
)
2179 if (validate_ws(win
->ws
))
2180 return; /* XXX this gets hit with thunderbird, needs fixing */
2182 if (validate_win(win
)) {
2187 if (validate_win(win
)) {
2192 XGetInputFocus(display
, &cur_focus
, &rr
);
2193 if ((cfw
= find_window(cur_focus
)) != NULL
)
2196 /* use larger hammer since the window was killed somehow */
2197 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2198 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2199 XSetWindowBorder(display
, cfw
->id
,
2200 cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2203 win
->ws
->focus
= win
;
2205 if (win
->ws
->r
!= NULL
) {
2206 /* drain all previous focus events */
2207 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
2211 XSetInputFocus(display
, win
->id
,
2212 RevertToParent
, CurrentTime
);
2213 grabbuttons(win
, 1);
2214 XSetWindowBorder(display
, win
->id
,
2215 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2216 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2217 win
->ws
->always_raise
)
2218 XMapRaised(display
, win
->id
);
2220 XChangeProperty(display
, win
->s
->root
,
2221 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2222 PropModeReplace
, (unsigned char *)&win
->id
,1);
2229 switchws(struct swm_region
*r
, union arg
*args
)
2231 int wsid
= args
->id
, unmap_old
= 0;
2232 struct swm_region
*this_r
, *other_r
;
2234 struct workspace
*new_ws
, *old_ws
;
2241 old_ws
= this_r
->ws
;
2242 new_ws
= &this_r
->s
->ws
[wsid
];
2244 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2245 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2247 if (new_ws
== NULL
|| old_ws
== NULL
)
2249 if (new_ws
== old_ws
)
2252 other_r
= new_ws
->r
;
2253 if (other_r
== NULL
) {
2254 /* the other workspace is hidden, hide this one */
2258 /* the other ws is visible in another region, exchange them */
2259 other_r
->ws_prior
= new_ws
;
2260 other_r
->ws
= old_ws
;
2261 old_ws
->r
= other_r
;
2263 this_r
->ws_prior
= old_ws
;
2264 this_r
->ws
= new_ws
;
2267 /* this is needed so that we can click on a window after a restart */
2271 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2272 focus(new_ws
->r
, &a
);
2276 /* unmap old windows */
2278 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2281 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2282 drain_enter_notify();
2286 cyclews(struct swm_region
*r
, union arg
*args
)
2289 struct swm_screen
*s
= r
->s
;
2292 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2293 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2298 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2301 case SWM_ARG_ID_CYCLEWS_UP
:
2302 if (a
.id
< SWM_WS_MAX
- 1)
2307 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2310 case SWM_ARG_ID_CYCLEWS_DOWN
:
2314 a
.id
= SWM_WS_MAX
- 1;
2321 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2323 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2327 } while (a
.id
!= r
->ws
->idx
);
2331 priorws(struct swm_region
*r
, union arg
*args
)
2335 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2336 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2338 if (r
->ws_prior
== NULL
)
2341 a
.id
= r
->ws_prior
->idx
;
2346 cyclescr(struct swm_region
*r
, union arg
*args
)
2348 struct swm_region
*rr
= NULL
;
2352 /* do nothing if we don't have more than one screen */
2353 if (!(ScreenCount(display
) > 1 || outputs
> 1))
2358 case SWM_ARG_ID_CYCLESC_UP
:
2359 rr
= TAILQ_NEXT(r
, entry
);
2361 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2363 case SWM_ARG_ID_CYCLESC_DOWN
:
2364 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2366 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2374 /* move mouse to region */
2376 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2377 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2379 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2382 if (rr
->ws
->focus
) {
2383 /* move to focus window */
2384 x
= X(rr
->ws
->focus
) + 1;
2385 y
= Y(rr
->ws
->focus
) + 1;
2386 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2391 sort_windows(struct ws_win_list
*wl
)
2393 struct ws_win
*win
, *parent
, *nxt
;
2398 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2399 nxt
= TAILQ_NEXT(win
, entry
);
2400 if (win
->transient
) {
2401 parent
= find_window(win
->transient
);
2402 if (parent
== NULL
) {
2403 warnx("not possible bug");
2406 TAILQ_REMOVE(wl
, win
, entry
);
2407 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2414 swapwin(struct swm_region
*r
, union arg
*args
)
2416 struct ws_win
*target
, *source
;
2417 struct ws_win
*cur_focus
;
2418 struct ws_win_list
*wl
;
2421 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2422 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2424 cur_focus
= r
->ws
->focus
;
2425 if (cur_focus
== NULL
)
2429 wl
= &source
->ws
->winlist
;
2432 case SWM_ARG_ID_SWAPPREV
:
2433 if (source
->transient
)
2434 source
= find_window(source
->transient
);
2435 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2436 if (target
&& target
->transient
)
2437 target
= find_window(target
->transient
);
2438 TAILQ_REMOVE(wl
, source
, entry
);
2440 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2442 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2444 case SWM_ARG_ID_SWAPNEXT
:
2445 target
= TAILQ_NEXT(source
, entry
);
2446 /* move the parent and let the sort handle the move */
2447 if (source
->transient
)
2448 source
= find_window(source
->transient
);
2449 TAILQ_REMOVE(wl
, source
, entry
);
2451 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2453 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2455 case SWM_ARG_ID_SWAPMAIN
:
2456 target
= TAILQ_FIRST(wl
);
2457 if (target
== source
) {
2458 if (source
->ws
->focus_prev
!= NULL
&&
2459 source
->ws
->focus_prev
!= target
)
2461 source
= source
->ws
->focus_prev
;
2465 if (target
== NULL
|| source
== NULL
)
2467 source
->ws
->focus_prev
= target
;
2468 TAILQ_REMOVE(wl
, target
, entry
);
2469 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2470 TAILQ_REMOVE(wl
, source
, entry
);
2471 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2473 case SWM_ARG_ID_MOVELAST
:
2474 TAILQ_REMOVE(wl
, source
, entry
);
2475 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2478 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2488 focus_prev(struct ws_win
*win
)
2490 struct ws_win
*winfocus
= NULL
;
2491 struct ws_win
*cur_focus
= NULL
;
2492 struct ws_win_list
*wl
= NULL
;
2493 struct workspace
*ws
= NULL
;
2495 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%lx\n", WINID(win
));
2497 if (!(win
&& win
->ws
))
2502 cur_focus
= ws
->focus
;
2504 /* pickle, just focus on whatever */
2505 if (cur_focus
== NULL
) {
2506 /* use prev_focus if valid */
2507 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2508 find_window(WINID(ws
->focus_prev
)))
2509 winfocus
= ws
->focus_prev
;
2510 if (winfocus
== NULL
)
2511 winfocus
= TAILQ_FIRST(wl
);
2515 /* if transient focus on parent */
2516 if (cur_focus
->transient
) {
2517 winfocus
= find_window(cur_focus
->transient
);
2521 /* if in max_stack try harder */
2522 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2523 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2524 if (cur_focus
!= ws
->focus_prev
)
2525 winfocus
= ws
->focus_prev
;
2526 else if (cur_focus
!= ws
->focus
)
2527 winfocus
= ws
->focus
;
2529 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2534 if (cur_focus
== win
)
2535 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2536 if (winfocus
== NULL
)
2537 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2538 if (winfocus
== NULL
|| winfocus
== win
)
2539 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2542 focus_magic(winfocus
);
2546 focus(struct swm_region
*r
, union arg
*args
)
2548 struct ws_win
*winfocus
= NULL
, *head
;
2549 struct ws_win
*cur_focus
= NULL
;
2550 struct ws_win_list
*wl
= NULL
;
2551 struct workspace
*ws
= NULL
;
2557 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2559 /* treat FOCUS_CUR special */
2560 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2561 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2562 winfocus
= r
->ws
->focus
;
2563 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2564 winfocus
= r
->ws
->focus_prev
;
2566 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2567 if (winfocus
->iconic
== 0)
2570 focus_magic(winfocus
);
2574 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2578 if (TAILQ_EMPTY(wl
))
2580 /* make sure there is at least one uniconified window */
2582 TAILQ_FOREACH(winfocus
, wl
, entry
)
2583 if (winfocus
->iconic
== 0) {
2591 case SWM_ARG_ID_FOCUSPREV
:
2592 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2594 head
= TAILQ_LAST(wl
, ws_win_list
);
2596 if (WINID(winfocus
) == cur_focus
->transient
) {
2597 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2599 head
= TAILQ_LAST(wl
, ws_win_list
);
2604 if (winfocus
&& winfocus
->iconic
) {
2605 while (winfocus
!= cur_focus
) {
2606 if (winfocus
== NULL
)
2607 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2608 if (winfocus
->iconic
== 0)
2610 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
2616 case SWM_ARG_ID_FOCUSNEXT
:
2617 head
= TAILQ_NEXT(cur_focus
, entry
);
2619 head
= TAILQ_FIRST(wl
);
2623 if (winfocus
&& winfocus
->iconic
) {
2624 while (winfocus
!= cur_focus
) {
2625 if (winfocus
== NULL
)
2626 winfocus
= TAILQ_FIRST(wl
);
2627 if (winfocus
->iconic
== 0)
2629 winfocus
= TAILQ_NEXT(winfocus
, entry
);
2634 case SWM_ARG_ID_FOCUSMAIN
:
2635 winfocus
= TAILQ_FIRST(wl
);
2636 if (winfocus
== cur_focus
)
2637 winfocus
= cur_focus
->ws
->focus_prev
;
2644 focus_magic(winfocus
);
2648 cycle_layout(struct swm_region
*r
, union arg
*args
)
2650 struct workspace
*ws
= r
->ws
;
2653 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2656 if (ws
->cur_layout
->l_stack
== NULL
)
2657 ws
->cur_layout
= &layouts
[0];
2660 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2661 drain_enter_notify();
2662 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2668 stack_config(struct swm_region
*r
, union arg
*args
)
2670 struct workspace
*ws
= r
->ws
;
2672 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
2675 if (ws
->cur_layout
->l_config
!= NULL
)
2676 ws
->cur_layout
->l_config(ws
, args
->id
);
2678 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
2685 struct swm_geometry g
;
2686 struct swm_region
*r
;
2692 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
2694 for (i
= 0; i
< ScreenCount(display
); i
++) {
2698 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2699 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
2700 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
2702 /* start with screen geometry, adjust for bar */
2704 g
.w
-= 2 * border_width
;
2705 g
.h
-= 2 * border_width
;
2711 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
2712 r
->ws
->cur_layout
->l_string(r
->ws
);
2713 /* save r so we can track region changes */
2720 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2721 drain_enter_notify();
2723 DNPRINTF(SWM_D_STACK
, "stack: end\n");
2727 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
2729 /* retain window geom and region geom */
2730 win
->g_float
= win
->g
;
2731 win
->rg_float
= r
->g
;
2732 win
->g_floatvalid
= 1;
2736 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
2744 bzero(&wc
, sizeof wc
);
2745 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
2748 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2749 * geom on ws switches or return from max mode
2751 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&& win
->g_floatvalid
2752 && !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
2754 * use stored g and rg to set relative position and size
2755 * as in old region or before max stack mode
2757 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ X(r
);
2758 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ Y(r
);
2759 WIDTH(win
) = win
->g_float
.w
;
2760 HEIGHT(win
) = win
->g_float
.h
;
2761 win
->g_floatvalid
= 0;
2764 win
->floatmaxed
= 0;
2766 if ((win
->quirks
& SWM_Q_FULLSCREEN
) && (WIDTH(win
) >= WIDTH(r
)) &&
2767 (HEIGHT(win
) >= HEIGHT(r
)))
2768 wc
.border_width
= 0;
2770 wc
.border_width
= border_width
;
2771 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
2772 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
2773 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
2778 * floaters and transients are auto-centred unless moved
2781 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - wc
.border_width
;
2782 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - wc
.border_width
;
2785 /* win can be outside r if new r smaller than old r */
2786 /* Ensure top left corner inside r (move probs otherwise) */
2787 if (X(win
) < X(r
) - wc
.border_width
)
2788 X(win
) = X(r
) - wc
.border_width
;
2789 if (X(win
) > X(r
) + WIDTH(r
) - 1)
2790 X(win
) = (WIDTH(win
) > WIDTH(r
)) ? X(r
) :
2791 (X(r
) + WIDTH(r
) - WIDTH(win
) - 2 * wc
.border_width
);
2792 if (Y(win
) < Y(r
) - wc
.border_width
)
2793 Y(win
) = Y(r
) - wc
.border_width
;
2794 if (Y(win
) > Y(r
) + HEIGHT(r
) - 1)
2795 Y(win
) = (HEIGHT(win
) > HEIGHT(r
)) ? Y(r
) :
2796 (Y(r
) + HEIGHT(r
) - HEIGHT(win
) - 2 * wc
.border_width
);
2800 wc
.width
= WIDTH(win
);
2801 wc
.height
= HEIGHT(win
);
2804 * Retain floater and transient geometry for correct positioning
2805 * when ws changes region
2807 if (!(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
2808 store_float_geom(win
, r
);
2810 DNPRINTF(SWM_D_MISC
, "stack_floater: window: %lu, (x,y) w x h: (%d,%d) "
2811 "%d x %d\n", win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
2813 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2817 * Send keystrokes to terminal to decrease/increase the font size as the
2818 * window size changes.
2821 adjust_font(struct ws_win
*win
)
2823 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
2824 win
->floating
|| win
->transient
)
2827 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
2828 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
2829 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
2830 win
->font_size_boundary
[win
->font_steps
] =
2831 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
2834 win
->last_inc
= win
->sh
.width_inc
;
2835 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
2836 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
2837 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
2840 win
->last_inc
= win
->sh
.width_inc
;
2841 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
2845 #define SWAPXY(g) do { \
2847 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2848 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2851 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
2854 XWindowAttributes wa
;
2855 struct swm_geometry win_g
, r_g
= *g
;
2856 struct ws_win
*win
, *fs_win
= 0;
2857 int i
, j
, s
, stacks
;
2858 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
2859 int hrh
, extra
= 0, h_slice
, last_h
= 0;
2860 int split
, colno
, winno
, mwin
, msize
, mscale
;
2861 int remain
, missing
, v_slice
, reconfigure
;
2864 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
2865 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
2867 winno
= count_win(ws
, 0);
2868 if (winno
== 0 && count_win(ws
, 1) == 0)
2871 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2872 if (win
->transient
== 0 && win
->floating
== 0
2873 && win
->iconic
== 0)
2880 w_inc
= win
->sh
.width_inc
;
2881 w_base
= win
->sh
.base_width
;
2882 mwin
= ws
->l_state
.horizontal_mwin
;
2883 mscale
= ws
->l_state
.horizontal_msize
;
2884 stacks
= ws
->l_state
.horizontal_stacks
;
2887 w_inc
= win
->sh
.height_inc
;
2888 w_base
= win
->sh
.base_height
;
2889 mwin
= ws
->l_state
.vertical_mwin
;
2890 mscale
= ws
->l_state
.vertical_msize
;
2891 stacks
= ws
->l_state
.vertical_stacks
;
2895 if (stacks
> winno
- mwin
)
2896 stacks
= winno
- mwin
;
2900 h_slice
= r_g
.h
/ SWM_H_SLICE
;
2901 if (mwin
&& winno
> mwin
) {
2902 v_slice
= r_g
.w
/ SWM_V_SLICE
;
2906 win_g
.w
= v_slice
* mscale
;
2908 if (w_inc
> 1 && w_inc
< v_slice
) {
2909 /* adjust for window's requested size increment */
2910 remain
= (win_g
.w
- w_base
) % w_inc
;
2911 missing
= w_inc
- remain
;
2918 win_g
.x
+= r_g
.w
- msize
;
2921 colno
= split
= winno
/ stacks
;
2922 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
2923 2 * border_width
) / stacks
);
2925 hrh
= r_g
.h
/ colno
;
2926 extra
= r_g
.h
- (colno
* hrh
);
2927 win_g
.h
= hrh
- 2 * border_width
;
2929 /* stack all the tiled windows */
2930 i
= j
= 0, s
= stacks
;
2931 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2932 if (win
->transient
!= 0 || win
->floating
!= 0)
2934 if (win
->iconic
!= 0)
2937 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
2942 if (split
&& i
== split
) {
2943 colno
= (winno
- mwin
) / stacks
;
2944 if (s
<= (winno
- mwin
) % stacks
)
2946 split
= split
+ colno
;
2947 hrh
= (r_g
.h
/ colno
);
2948 extra
= r_g
.h
- (colno
* hrh
);
2952 win_g
.x
+= win_g
.w
+ 2 * border_width
;
2953 win_g
.w
= (r_g
.w
- msize
-
2954 (stacks
* 2 * border_width
)) / stacks
;
2956 win_g
.w
+= (r_g
.w
- msize
-
2957 (stacks
* 2 * border_width
)) % stacks
;
2961 win_g
.h
= hrh
- 2 * border_width
;
2963 h_inc
= win
->sh
.width_inc
;
2964 h_base
= win
->sh
.base_width
;
2966 h_inc
= win
->sh
.height_inc
;
2967 h_base
= win
->sh
.base_height
;
2969 if (j
== colno
- 1) {
2970 win_g
.h
= hrh
+ extra
;
2971 } else if (h_inc
> 1 && h_inc
< h_slice
) {
2972 /* adjust for window's requested size increment */
2973 remain
= (win_g
.h
- h_base
) % h_inc
;
2974 missing
= h_inc
- remain
;
2976 if (missing
<= extra
|| j
== 0) {
2988 win_g
.y
+= last_h
+ 2 * border_width
;
2990 bzero(&wc
, sizeof wc
);
2991 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
2992 wc
.border_width
= 0;
2993 win_g
.w
+= 2 * border_width
;
2994 win_g
.h
+= 2 * border_width
;
2996 wc
.border_width
= border_width
;
2999 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3000 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3002 X(win
) = wc
.x
= win_g
.y
;
3003 Y(win
) = wc
.y
= win_g
.x
;
3004 WIDTH(win
) = wc
.width
= win_g
.h
;
3005 HEIGHT(win
) = wc
.height
= win_g
.w
;
3008 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3009 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3011 X(win
) = wc
.x
= win_g
.x
;
3012 Y(win
) = wc
.y
= win_g
.y
;
3013 WIDTH(win
) = wc
.width
= win_g
.w
;
3014 HEIGHT(win
) = wc
.height
= win_g
.h
;
3019 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
3020 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3023 if (XGetWindowAttributes(display
, win
->id
, &wa
))
3024 if (wa
.map_state
== IsUnmapped
)
3025 XMapRaised(display
, win
->id
);
3033 /* now, stack all the floaters and transients */
3034 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3035 if (win
->transient
== 0 && win
->floating
== 0)
3037 if (win
->iconic
== 1)
3039 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3044 stack_floater(win
, ws
->r
);
3045 XMapRaised(display
, win
->id
);
3049 stack_floater(fs_win
, ws
->r
);
3050 XMapRaised(display
, fs_win
->id
);
3055 vertical_config(struct workspace
*ws
, int id
)
3057 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3061 case SWM_ARG_ID_STACKRESET
:
3062 case SWM_ARG_ID_STACKINIT
:
3063 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3064 ws
->l_state
.vertical_mwin
= 1;
3065 ws
->l_state
.vertical_stacks
= 1;
3067 case SWM_ARG_ID_MASTERSHRINK
:
3068 if (ws
->l_state
.vertical_msize
> 1)
3069 ws
->l_state
.vertical_msize
--;
3071 case SWM_ARG_ID_MASTERGROW
:
3072 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3073 ws
->l_state
.vertical_msize
++;
3075 case SWM_ARG_ID_MASTERADD
:
3076 ws
->l_state
.vertical_mwin
++;
3078 case SWM_ARG_ID_MASTERDEL
:
3079 if (ws
->l_state
.vertical_mwin
> 0)
3080 ws
->l_state
.vertical_mwin
--;
3082 case SWM_ARG_ID_STACKINC
:
3083 ws
->l_state
.vertical_stacks
++;
3085 case SWM_ARG_ID_STACKDEC
:
3086 if (ws
->l_state
.vertical_stacks
> 1)
3087 ws
->l_state
.vertical_stacks
--;
3089 case SWM_ARG_ID_FLIPLAYOUT
:
3090 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3098 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3100 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3102 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3106 horizontal_config(struct workspace
*ws
, int id
)
3108 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3111 case SWM_ARG_ID_STACKRESET
:
3112 case SWM_ARG_ID_STACKINIT
:
3113 ws
->l_state
.horizontal_mwin
= 1;
3114 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3115 ws
->l_state
.horizontal_stacks
= 1;
3117 case SWM_ARG_ID_MASTERSHRINK
:
3118 if (ws
->l_state
.horizontal_msize
> 1)
3119 ws
->l_state
.horizontal_msize
--;
3121 case SWM_ARG_ID_MASTERGROW
:
3122 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3123 ws
->l_state
.horizontal_msize
++;
3125 case SWM_ARG_ID_MASTERADD
:
3126 ws
->l_state
.horizontal_mwin
++;
3128 case SWM_ARG_ID_MASTERDEL
:
3129 if (ws
->l_state
.horizontal_mwin
> 0)
3130 ws
->l_state
.horizontal_mwin
--;
3132 case SWM_ARG_ID_STACKINC
:
3133 ws
->l_state
.horizontal_stacks
++;
3135 case SWM_ARG_ID_STACKDEC
:
3136 if (ws
->l_state
.horizontal_stacks
> 1)
3137 ws
->l_state
.horizontal_stacks
--;
3139 case SWM_ARG_ID_FLIPLAYOUT
:
3140 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3148 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3150 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3152 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3155 /* fullscreen view */
3157 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3160 struct swm_geometry gg
= *g
;
3161 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3165 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3170 winno
= count_win(ws
, 0);
3171 if (winno
== 0 && count_win(ws
, 1) == 0)
3174 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3175 if (win
->transient
) {
3177 parent
= find_window(win
->transient
);
3181 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3183 * retain geometry for retrieval on exit from
3186 store_float_geom(win
, ws
->r
);
3187 win
->floatmaxed
= 1;
3190 /* only reconfigure if necessary */
3191 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3192 HEIGHT(win
) != gg
.h
) {
3193 bzero(&wc
, sizeof wc
);
3194 X(win
) = wc
.x
= gg
.x
;
3195 Y(win
) = wc
.y
= gg
.y
;
3197 wc
.border_width
= border_width
;
3198 WIDTH(win
) = wc
.width
= gg
.w
;
3199 HEIGHT(win
) = wc
.height
= gg
.h
;
3201 wc
.border_width
= 0;
3202 WIDTH(win
) = wc
.width
= gg
.w
+ 2 * border_width
;
3203 HEIGHT(win
) = wc
.height
= gg
.h
+
3206 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
3207 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3209 /* unmap only if we don't have multi screen */
3210 if (win
!= ws
->focus
)
3211 if (!(ScreenCount(display
) > 1 || outputs
> 1))
3215 /* put the last transient on top */
3218 XMapRaised(display
, parent
->id
);
3219 stack_floater(wintrans
, ws
->r
);
3220 focus_magic(wintrans
);
3225 send_to_ws(struct swm_region
*r
, union arg
*args
)
3227 int wsid
= args
->id
;
3228 struct ws_win
*win
= NULL
, *parent
;
3229 struct workspace
*ws
, *nws
;
3230 Atom ws_idx_atom
= 0;
3231 unsigned char ws_idx_str
[SWM_PROPLEN
];
3234 if (r
&& r
->ws
&& r
->ws
->focus
)
3240 if (win
->ws
->idx
== wsid
)
3243 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%lx\n", win
->id
);
3246 nws
= &win
->s
->ws
[wsid
];
3248 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3250 if (win
->transient
) {
3251 parent
= find_window(win
->transient
);
3253 unmap_window(parent
);
3254 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3255 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3260 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3261 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3262 if (TAILQ_EMPTY(&ws
->winlist
))
3263 r
->ws
->focus
= NULL
;
3266 /* Try to update the window's workspace property */
3267 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
3269 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3271 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3273 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
3274 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
3281 pressbutton(struct swm_region
*r
, union arg
*args
)
3283 XTestFakeButtonEvent(display
, args
->id
, True
, CurrentTime
);
3284 XTestFakeButtonEvent(display
, args
->id
, False
, CurrentTime
);
3288 raise_toggle(struct swm_region
*r
, union arg
*args
)
3290 if (r
&& r
->ws
== NULL
)
3293 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3295 /* bring floaters back to top */
3296 if (r
->ws
->always_raise
== 0)
3301 iconify(struct swm_region
*r
, union arg
*args
)
3305 if (r
->ws
->focus
== NULL
)
3307 unmap_window(r
->ws
->focus
);
3308 update_iconic(r
->ws
->focus
, 1);
3310 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3311 drain_enter_notify();
3312 r
->ws
->focus
= NULL
;
3313 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3318 get_win_name(Window win
)
3320 unsigned char *prop
= NULL
;
3321 unsigned long nbytes
, nitems
;
3323 /* try _NET_WM_NAME first */
3324 if (get_property(win
, a_netwmname
, 0L, a_utf8_string
, NULL
, &nbytes
,
3327 if (get_property(win
, a_netwmname
, nbytes
, a_utf8_string
,
3328 &nitems
, NULL
, &prop
))
3332 /* fallback to WM_NAME */
3333 if (!get_property(win
, a_wmname
, 0L, a_string
, NULL
, &nbytes
, &prop
))
3336 if (get_property(win
, a_wmname
, nbytes
, a_string
, &nitems
, NULL
, &prop
))
3343 uniconify(struct swm_region
*r
, union arg
*args
)
3347 unsigned char *name
;
3350 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3352 if (r
&& r
->ws
== NULL
)
3355 /* make sure we have anything to uniconify */
3356 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3357 if (win
->ws
== NULL
)
3358 continue; /* should never happen */
3359 if (win
->iconic
== 0)
3367 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3369 spawn_select(r
, args
, "search", &searchpid
);
3371 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3374 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3375 if (win
->ws
== NULL
)
3376 continue; /* should never happen */
3377 if (win
->iconic
== 0)
3380 name
= get_win_name(win
->id
);
3383 fprintf(lfile
, "%s.%lu\n", name
, win
->id
);
3391 name_workspace(struct swm_region
*r
, union arg
*args
)
3395 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3401 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3403 spawn_select(r
, args
, "name_workspace", &searchpid
);
3405 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3408 fprintf(lfile
, "%s", "");
3413 search_workspace(struct swm_region
*r
, union arg
*args
)
3416 struct workspace
*ws
;
3419 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3425 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3427 spawn_select(r
, args
, "search", &searchpid
);
3429 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3432 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
3436 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3437 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3444 search_win_cleanup(void)
3446 struct search_window
*sw
= NULL
;
3448 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3449 XDestroyWindow(display
, sw
->indicator
);
3450 XFreeGC(display
, sw
->gc
);
3451 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3457 search_win(struct swm_region
*r
, union arg
*args
)
3459 struct ws_win
*win
= NULL
;
3460 struct search_window
*sw
= NULL
;
3467 XRectangle ibox
, lbox
;
3469 DNPRINTF(SWM_D_MISC
, "search_win\n");
3472 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3474 spawn_select(r
, args
, "search", &searchpid
);
3476 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3479 TAILQ_INIT(&search_wl
);
3482 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3483 if (win
->iconic
== 1)
3486 sw
= calloc(1, sizeof(struct search_window
));
3488 warn("search_win: calloc");
3490 search_win_cleanup();
3496 snprintf(s
, sizeof s
, "%d", i
);
3499 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
3501 w
= XCreateSimpleWindow(display
,
3502 win
->id
, 0, 0,lbox
.width
+ 4,
3503 bar_fs_extents
->max_logical_extent
.height
, 1,
3504 r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
,
3505 r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
3508 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3510 sw
->gc
= XCreateGC(display
, w
, 0, &gcv
);
3511 XMapRaised(display
, w
);
3512 XSetForeground(display
, sw
->gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
3514 DRAWSTRING(display
, w
, bar_fs
, sw
->gc
, 2,
3515 (bar_fs_extents
->max_logical_extent
.height
-
3516 lbox
.height
) / 2 - lbox
.y
, s
, len
);
3518 fprintf(lfile
, "%d\n", i
);
3526 search_resp_uniconify(char *resp
, unsigned long len
)
3528 unsigned char *name
;
3532 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3534 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3535 if (win
->iconic
== 0)
3537 name
= get_win_name(win
->id
);
3540 if (asprintf(&s
, "%s.%lu", name
, win
->id
) == -1) {
3545 if (strncmp(s
, resp
, len
) == 0) {
3546 /* XXX this should be a callback to generalize */
3547 update_iconic(win
, 0);
3556 search_resp_name_workspace(char *resp
, unsigned long len
)
3558 struct workspace
*ws
;
3560 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3562 if (search_r
->ws
== NULL
)
3567 free(search_r
->ws
->name
);
3568 search_r
->ws
->name
= NULL
;
3572 ws
->name
= strdup(resp
);
3573 if (ws
->name
== NULL
) {
3574 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
3575 "strdup: %s", strerror(errno
));
3582 search_resp_search_workspace(char *resp
, unsigned long len
)
3589 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
3593 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
3600 ws_idx
= strtonum(q
, 1, SWM_WS_MAX
, &errstr
);
3602 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
3609 switchws(search_r
, &a
);
3613 search_resp_search_window(char *resp
, unsigned long len
)
3618 struct search_window
*sw
;
3620 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
3624 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
3629 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
3631 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
3638 TAILQ_FOREACH(sw
, &search_wl
, entry
)
3639 if (idx
== sw
->idx
) {
3645 #define MAX_RESP_LEN 1024
3648 search_do_resp(void)
3654 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
3659 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
3660 warn("search: calloc");
3664 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
3666 warn("search: read error");
3669 resp
[rbytes
] = '\0';
3672 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3673 * newline, so work around that by sanitizing the resp now.
3675 resp
[strcspn(resp
, "\n")] = '\0';
3678 switch (search_resp_action
) {
3679 case SWM_SEARCH_UNICONIFY
:
3680 search_resp_uniconify(resp
, len
);
3682 case SWM_SEARCH_NAME_WORKSPACE
:
3683 search_resp_name_workspace(resp
, len
);
3685 case SWM_SEARCH_SEARCH_WORKSPACE
:
3686 search_resp_search_workspace(resp
, len
);
3688 case SWM_SEARCH_SEARCH_WINDOW
:
3689 search_resp_search_window(resp
, len
);
3694 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
3695 search_win_cleanup();
3697 search_resp_action
= SWM_SEARCH_NONE
;
3698 close(select_resp_pipe
[0]);
3703 wkill(struct swm_region
*r
, union arg
*args
)
3705 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
3707 if (r
->ws
->focus
== NULL
)
3710 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
3711 XKillClient(display
, r
->ws
->focus
->id
);
3713 if (r
->ws
->focus
->can_delete
)
3714 client_msg(r
->ws
->focus
, adelete
);
3719 floating_toggle_win(struct ws_win
*win
)
3721 struct swm_region
*r
;
3731 /* reject floating toggles in max stack mode */
3732 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3735 if (win
->floating
) {
3736 if (!win
->floatmaxed
) {
3737 /* retain position for refloat */
3738 store_float_geom(win
, r
);
3742 if (win
->g_floatvalid
) {
3743 /* refloat at last floating relative position */
3744 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ X(r
);
3745 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ Y(r
);
3746 WIDTH(win
) = win
->g_float
.w
;
3747 HEIGHT(win
) = win
->g_float
.h
;
3752 ewmh_update_actions(win
);
3758 floating_toggle(struct swm_region
*r
, union arg
*args
)
3760 struct ws_win
*win
= r
->ws
->focus
;
3766 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
3767 _NET_WM_STATE_TOGGLE
);
3770 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3771 drain_enter_notify();
3773 if (win
== win
->ws
->focus
) {
3774 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3775 focus(win
->ws
->r
, &a
);
3780 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
3782 if (X(win
) + WIDTH(win
) > X(r
) + WIDTH(r
) - border_width
) {
3784 WIDTH(win
) = X(r
) + WIDTH(r
) - X(win
) - border_width
;
3786 X(win
) = X(r
) + WIDTH(r
) - WIDTH(win
) - border_width
;
3789 if (X(win
) < X(r
) - border_width
) {
3791 WIDTH(win
) -= X(r
) - X(win
) - border_width
;
3793 X(win
) = X(r
) - border_width
;
3796 if (Y(win
) + HEIGHT(win
) > Y(r
) + HEIGHT(r
) - border_width
) {
3798 HEIGHT(win
) = Y(r
) + HEIGHT(r
) - Y(win
) - border_width
;
3800 Y(win
) = Y(r
) + HEIGHT(r
) - HEIGHT(win
) - border_width
;
3803 if (Y(win
) < Y(r
) - border_width
) {
3805 HEIGHT(win
) -= Y(r
) - Y(win
) - border_width
;
3807 Y(win
) = Y(r
) - border_width
;
3812 if (HEIGHT(win
) < 1)
3817 update_window(struct ws_win
*win
)
3822 bzero(&wc
, sizeof wc
);
3823 mask
= CWBorderWidth
| CWWidth
| CWHeight
| CWX
| CWY
;
3824 wc
.border_width
= border_width
;
3827 wc
.width
= WIDTH(win
);
3828 wc
.height
= HEIGHT(win
);
3830 DNPRINTF(SWM_D_MISC
, "update_window: window: 0x%lx, (x,y) w x h: "
3831 "(%d,%d) %d x %d\n", win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
3833 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3836 #define SWM_RESIZE_STEPS (50)
3839 resize(struct ws_win
*win
, union arg
*args
)
3843 struct swm_region
*r
= NULL
;
3844 int resize_step
= 0;
3848 struct swm_geometry g
;
3849 int top
= 0, left
= 0;
3852 unsigned int shape
; /* cursor style */
3858 DNPRINTF(SWM_D_MOUSE
, "resize: window: 0x%lx, floating: %s, "
3859 "transient: 0x%lx\n", win
->id
, YESNO(win
->floating
),
3862 if (!(win
->transient
!= 0 || win
->floating
!= 0))
3865 /* reject resizes in max mode for floaters (transient ok) */
3866 if (win
->floatmaxed
)
3870 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
3876 case SWM_ARG_ID_WIDTHSHRINK
:
3877 WIDTH(win
) -= SWM_RESIZE_STEPS
;
3880 case SWM_ARG_ID_WIDTHGROW
:
3881 WIDTH(win
) += SWM_RESIZE_STEPS
;
3884 case SWM_ARG_ID_HEIGHTSHRINK
:
3885 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
3888 case SWM_ARG_ID_HEIGHTGROW
:
3889 HEIGHT(win
) += SWM_RESIZE_STEPS
;
3896 constrain_window(win
, r
, 1);
3898 store_float_geom(win
,r
);
3902 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3903 drain_enter_notify();
3905 /* get cursor offset from window root */
3906 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
3911 if (wx
< WIDTH(win
) / 2)
3914 if (wy
< HEIGHT(win
) / 2)
3917 if (args
->id
== SWM_ARG_ID_CENTER
)
3920 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
3922 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
3924 cursor
= XCreateFontCursor(display
, shape
);
3926 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
3927 GrabModeAsync
, None
, cursor
, CurrentTime
) != GrabSuccess
) {
3928 XFreeCursor(display
, cursor
);
3933 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
3934 SubstructureRedirectMask
, &ev
);
3936 case ConfigureRequest
:
3939 handler
[ev
.type
](&ev
);
3942 /* cursor offset/delta from start of the operation */
3943 dx
= ev
.xmotion
.x_root
- x
;
3944 dy
= ev
.xmotion
.y_root
- y
;
3950 if (args
->id
== SWM_ARG_ID_CENTER
) {
3951 if (g
.h
/ 2 + dy
< 1)
3955 HEIGHT(win
) = g
.h
+ 2 * dy
;
3963 HEIGHT(win
) = g
.h
+ dy
;
3970 if (args
->id
== SWM_ARG_ID_CENTER
) {
3971 if (g
.w
/ 2 + dx
< 1)
3975 WIDTH(win
) = g
.w
+ 2 * dx
;
3983 WIDTH(win
) = g
.w
+ dx
;
3986 constrain_window(win
, r
, 1);
3988 /* not free, don't sync more than 120 times / second */
3989 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
3990 time
= ev
.xmotion
.time
;
3991 XSync(display
, False
);
3996 } while (ev
.type
!= ButtonRelease
);
3998 XSync(display
, False
);
4001 store_float_geom(win
,r
);
4003 XUngrabPointer(display
, CurrentTime
);
4004 XFreeCursor(display
, cursor
);
4007 drain_enter_notify();
4011 resize_step(struct swm_region
*r
, union arg
*args
)
4013 struct ws_win
*win
= NULL
;
4015 if (r
&& r
->ws
&& r
->ws
->focus
)
4023 #define SWM_MOVE_STEPS (50)
4026 move(struct ws_win
*win
, union arg
*args
)
4031 struct swm_region
*r
= NULL
;
4041 DNPRINTF(SWM_D_MOUSE
, "move: window: 0x%lx, floating: %s, transient: "
4042 "0x%lx\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4044 /* in max_stack mode should only move transients */
4045 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4049 if (win
->floating
== 0 && !win
->transient
) {
4050 store_float_geom(win
,r
);
4051 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4054 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4061 case SWM_ARG_ID_MOVELEFT
:
4062 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4065 case SWM_ARG_ID_MOVERIGHT
:
4066 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4069 case SWM_ARG_ID_MOVEUP
:
4070 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4073 case SWM_ARG_ID_MOVEDOWN
:
4074 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4081 constrain_window(win
, r
, 0);
4083 store_float_geom(win
, r
);
4087 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4088 GrabModeAsync
, None
, XCreateFontCursor(display
, XC_fleur
),
4089 CurrentTime
) != GrabSuccess
)
4092 /* get cursor offset from window root */
4093 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4097 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4098 SubstructureRedirectMask
, &ev
);
4100 case ConfigureRequest
:
4103 handler
[ev
.type
](&ev
);
4106 X(win
) = ev
.xmotion
.x_root
- wx
- border_width
;
4107 Y(win
) = ev
.xmotion
.y_root
- wy
- border_width
;
4109 constrain_window(win
, r
, 0);
4111 /* not free, don't sync more than 120 times / second */
4112 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4113 time
= ev
.xmotion
.time
;
4114 XSync(display
, False
);
4119 } while (ev
.type
!= ButtonRelease
);
4121 XSync(display
, False
);
4124 store_float_geom(win
,r
);
4125 XUngrabPointer(display
, CurrentTime
);
4128 drain_enter_notify();
4132 move_step(struct swm_region
*r
, union arg
*args
)
4134 struct ws_win
*win
= NULL
;
4136 if (r
&& r
->ws
&& r
->ws
->focus
)
4141 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4148 /* user/key callable function IDs */
4219 kf_search_workspace
,
4221 kf_dumpwins
, /* MUST BE LAST */
4225 /* key definitions */
4227 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4232 legacyfunc(struct swm_region
*r
, union arg
*args
)
4237 char name
[SWM_FUNCNAME_LEN
];
4238 void (*func
)(struct swm_region
*r
, union arg
*);
4240 } keyfuncs
[kf_invalid
+ 1] = {
4241 /* name function argument */
4242 { "cycle_layout", cycle_layout
, {0} },
4243 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4244 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4245 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4246 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4247 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4248 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4249 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4250 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4251 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4252 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4253 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4254 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4255 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4256 { "spawn_term", spawnterm
, {.argv
= spawn_term
} },
4257 { "spawn_menu", legacyfunc
, {0} },
4258 { "quit", quit
, {0} },
4259 { "restart", restart
, {0} },
4260 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4261 { "ws_1", switchws
, {.id
= 0} },
4262 { "ws_2", switchws
, {.id
= 1} },
4263 { "ws_3", switchws
, {.id
= 2} },
4264 { "ws_4", switchws
, {.id
= 3} },
4265 { "ws_5", switchws
, {.id
= 4} },
4266 { "ws_6", switchws
, {.id
= 5} },
4267 { "ws_7", switchws
, {.id
= 6} },
4268 { "ws_8", switchws
, {.id
= 7} },
4269 { "ws_9", switchws
, {.id
= 8} },
4270 { "ws_10", switchws
, {.id
= 9} },
4271 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4272 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4273 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4274 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4275 { "ws_prior", priorws
, {0} },
4276 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4277 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4278 { "mvws_1", send_to_ws
, {.id
= 0} },
4279 { "mvws_2", send_to_ws
, {.id
= 1} },
4280 { "mvws_3", send_to_ws
, {.id
= 2} },
4281 { "mvws_4", send_to_ws
, {.id
= 3} },
4282 { "mvws_5", send_to_ws
, {.id
= 4} },
4283 { "mvws_6", send_to_ws
, {.id
= 5} },
4284 { "mvws_7", send_to_ws
, {.id
= 6} },
4285 { "mvws_8", send_to_ws
, {.id
= 7} },
4286 { "mvws_9", send_to_ws
, {.id
= 8} },
4287 { "mvws_10", send_to_ws
, {.id
= 9} },
4288 { "bar_toggle", bar_toggle
, {0} },
4289 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4290 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4291 { "screenshot_all", legacyfunc
, {0} },
4292 { "screenshot_wind", legacyfunc
, {0} },
4293 { "float_toggle", floating_toggle
,{0} },
4294 { "version", version
, {0} },
4295 { "spawn_lock", legacyfunc
, {0} },
4296 { "spawn_initscr", legacyfunc
, {0} },
4297 { "spawn_custom", dummykeyfunc
, {0} },
4298 { "iconify", iconify
, {0} },
4299 { "uniconify", uniconify
, {0} },
4300 { "raise_toggle", raise_toggle
, {0} },
4301 { "button2", pressbutton
, {2} },
4302 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4303 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4304 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4305 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4306 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4307 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4308 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4309 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4310 { "name_workspace", name_workspace
, {0} },
4311 { "search_workspace", search_workspace
, {0} },
4312 { "search_win", search_win
, {0} },
4313 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4314 { "invalid key func", NULL
, {0} },
4317 TAILQ_ENTRY(key
) entry
;
4320 enum keyfuncid funcid
;
4323 TAILQ_HEAD(key_list
, key
);
4324 struct key_list keys
= TAILQ_HEAD_INITIALIZER(keys
);
4327 enum { client_click
, root_click
};
4329 unsigned int action
;
4331 unsigned int button
;
4332 void (*func
)(struct ws_win
*, union arg
*);
4335 /* action key mouse button func args */
4336 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4337 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4338 { client_click
, MODKEY
, Button1
, move
, {0} },
4342 update_modkey(unsigned int mod
)
4348 TAILQ_FOREACH(kp
, &keys
, entry
)
4349 if (kp
->mod
& ShiftMask
)
4350 kp
->mod
= mod
| ShiftMask
;
4354 for (i
= 0; i
< LENGTH(buttons
); i
++)
4355 if (buttons
[i
].mask
& ShiftMask
)
4356 buttons
[i
].mask
= mod
| ShiftMask
;
4358 buttons
[i
].mask
= mod
;
4363 TAILQ_ENTRY(spawn_prog
) entry
;
4368 TAILQ_HEAD(spawn_list
, spawn_prog
);
4369 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4372 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4375 struct spawn_prog
*prog
= NULL
;
4377 char *ap
, **real_args
;
4379 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4382 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4383 if (!strcasecmp(spawn_name
, prog
->name
))
4387 warnx("spawn_custom: program %s not found", spawn_name
);
4391 /* make room for expanded args */
4392 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4393 err(1, "spawn_custom: calloc real_args");
4395 /* expand spawn_args into real_args */
4396 for (i
= 0; i
< prog
->argc
; i
++) {
4398 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4399 if (!strcasecmp(ap
, "$bar_border")) {
4401 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4403 err(1, "spawn_custom border color");
4404 } else if (!strcasecmp(ap
, "$bar_color")) {
4406 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4408 err(1, "spawn_custom bar color");
4409 } else if (!strcasecmp(ap
, "$bar_font")) {
4410 if ((real_args
[i
] = strdup(bar_fonts
))
4412 err(1, "spawn_custom bar fonts");
4413 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4415 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4417 err(1, "spawn_custom color font");
4418 } else if (!strcasecmp(ap
, "$color_focus")) {
4420 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4422 err(1, "spawn_custom color focus");
4423 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4425 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4427 err(1, "spawn_custom color unfocus");
4429 /* no match --> copy as is */
4430 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4431 err(1, "spawn_custom strdup(ap)");
4433 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4438 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4439 for (i
= 0; i
< prog
->argc
; i
++)
4440 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4441 DNPRINTF(SWM_D_SPAWN
, "\n");
4443 *ret_args
= real_args
;
4444 return (prog
->argc
);
4448 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4454 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4458 spawn(r
->ws
->idx
, &a
, 1);
4460 for (i
= 0; i
< spawn_argc
; i
++)
4466 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4472 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4476 if (pipe(select_list_pipe
) == -1)
4477 err(1, "pipe error");
4478 if (pipe(select_resp_pipe
) == -1)
4479 err(1, "pipe error");
4481 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4482 err(1, "could not disable SIGPIPE");
4483 switch (*pid
= fork()) {
4485 err(1, "cannot fork");
4488 if (dup2(select_list_pipe
[0], 0) == -1)
4490 if (dup2(select_resp_pipe
[1], 1) == -1)
4492 close(select_list_pipe
[1]);
4493 close(select_resp_pipe
[0]);
4494 spawn(r
->ws
->idx
, &a
, 0);
4496 default: /* parent */
4497 close(select_list_pipe
[0]);
4498 close(select_resp_pipe
[1]);
4502 for (i
= 0; i
< spawn_argc
; i
++)
4508 spawn_insert(char *name
, char *args
)
4510 char *arg
, *cp
, *ptr
;
4511 struct spawn_prog
*sp
;
4513 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
4515 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
4516 err(1, "spawn_insert: malloc");
4517 if ((sp
->name
= strdup(name
)) == NULL
)
4518 err(1, "spawn_insert: strdup");
4520 /* convert the arguments to an argument list */
4521 if ((ptr
= cp
= strdup(args
)) == NULL
)
4522 err(1, "spawn_insert: strdup");
4523 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
4524 /* empty field; skip it */
4529 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
4530 sizeof *sp
->argv
)) == NULL
)
4531 err(1, "spawn_insert: realloc");
4532 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
4533 err(1, "spawn_insert: strdup");
4537 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
4538 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
4542 spawn_remove(struct spawn_prog
*sp
)
4546 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
4548 TAILQ_REMOVE(&spawns
, sp
, entry
);
4549 for (i
= 0; i
< sp
->argc
; i
++)
4555 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
4559 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
4561 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
4564 spawn_insert(name
, args
);
4566 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
4570 setspawn(char *name
, char *args
)
4572 struct spawn_prog
*sp
;
4574 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
4579 TAILQ_FOREACH(sp
, &spawns
, entry
) {
4580 if (!strcmp(sp
->name
, name
)) {
4584 spawn_replace(sp
, name
, args
);
4585 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
4589 if (*args
== '\0') {
4590 warnx("error: setspawn: cannot find program: %s", name
);
4594 spawn_insert(name
, args
);
4595 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
4599 setconfspawn(char *selector
, char *value
, int flags
)
4601 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
4603 setspawn(selector
, value
);
4605 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
4612 setconfspawn("term", "xterm", 0);
4613 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4614 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4615 setconfspawn("lock", "xlock", 0);
4616 setconfspawn("initscr", "initscreen.sh", 0);
4617 setconfspawn("menu", "dmenu_run"
4620 " -nf $bar_font_color"
4622 " -sf $bar_color", 0);
4623 setconfspawn("search", "dmenu"
4627 " -nf $bar_font_color"
4629 " -sf $bar_color", 0);
4630 setconfspawn("name_workspace", "dmenu"
4634 " -nf $bar_font_color"
4636 " -sf $bar_color", 0);
4640 #define SWM_MODNAME_SIZE 32
4641 #define SWM_KEY_WS "\n+ \t"
4643 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
4647 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
4648 if (mod
== NULL
|| ks
== NULL
) {
4649 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
4652 if (keystr
== NULL
|| strlen(keystr
) == 0) {
4653 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
4659 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
4660 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
4662 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
4663 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
4665 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
4667 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
4669 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
4671 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
4673 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
4675 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
4676 *mod
|= ControlMask
;
4678 *ks
= XStringToKeysym(name
);
4679 XConvertCase(*ks
, ks
, &uks
);
4680 if (ks
== NoSymbol
) {
4682 "parsekeys: invalid key %s\n",
4688 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
4693 strdupsafe(char *str
)
4698 return (strdup(str
));
4702 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
4706 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
4707 keyfuncs
[kfid
].name
, spawn_name
);
4709 if ((kp
= malloc(sizeof *kp
)) == NULL
)
4710 err(1, "key_insert: malloc");
4715 kp
->spawn_name
= strdupsafe(spawn_name
);
4716 TAILQ_INSERT_TAIL(&keys
, kp
, entry
);
4718 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
4722 key_remove(struct key
*kp
)
4724 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
4726 TAILQ_REMOVE(&keys
, kp
, entry
);
4727 free(kp
->spawn_name
);
4730 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
4734 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
4737 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
4741 key_insert(mod
, ks
, kfid
, spawn_name
);
4743 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
4747 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
4752 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
4753 keyfuncs
[kfid
].name
, spawn_name
);
4755 TAILQ_FOREACH(kp
, &keys
, entry
) {
4756 if (kp
->mod
== mod
&& kp
->keysym
== ks
) {
4757 if (kfid
== kf_invalid
)
4760 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
4761 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4765 if (kfid
== kf_invalid
) {
4766 warnx("error: setkeybinding: cannot find mod/key combination");
4767 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4771 key_insert(mod
, ks
, kfid
, spawn_name
);
4772 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4776 setconfbinding(char *selector
, char *value
, int flags
)
4778 enum keyfuncid kfid
;
4781 struct spawn_prog
*sp
;
4782 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
4783 if (selector
== NULL
) {
4784 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
4785 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4787 setkeybinding(mod
, ks
, kfid
, NULL
);
4792 /* search by key function name */
4793 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
4794 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
4795 SWM_FUNCNAME_LEN
) == 0) {
4796 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4798 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4799 setkeybinding(mod
, ks
, kfid
, NULL
);
4805 /* search by custom spawn name */
4806 TAILQ_FOREACH(sp
, &spawns
, entry
) {
4807 if (strcasecmp(selector
, sp
->name
) == 0) {
4808 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4810 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4811 setkeybinding(mod
, ks
, kf_spawn_custom
,
4818 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
4825 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
4826 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, kf_flip_layout
, NULL
);
4827 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
4828 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
4829 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
4830 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
4831 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
4832 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
4833 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
4834 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
4835 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
4836 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
4837 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
4838 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
4839 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_term
, NULL
);
4840 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
4841 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
4842 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
4843 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
4844 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
4845 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
4846 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
4847 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
4848 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
4849 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
4850 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
4851 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
4852 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
4853 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
4854 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
4855 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
4856 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
4857 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
4858 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
4859 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
4860 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
4861 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
4862 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
4863 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
4864 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
4865 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
4866 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
4867 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
4868 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
4869 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
4870 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
4871 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
4872 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
4873 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
4874 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
4875 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
4876 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
4877 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
4878 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
4879 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
4880 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
4881 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
4882 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
4883 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
4884 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
4885 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
4886 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
4887 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
4888 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
4889 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
4890 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
4891 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
4892 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
4893 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
4894 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
4895 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
4896 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
4898 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
4905 struct key
*kp_loop
, *kp_next
;
4907 kp_loop
= TAILQ_FIRST(&keys
);
4908 while (kp_loop
!= NULL
) {
4909 kp_next
= TAILQ_NEXT(kp_loop
, entry
);
4910 key_remove(kp_loop
);
4916 setkeymapping(char *selector
, char *value
, int flags
)
4918 char keymapping_file
[PATH_MAX
];
4919 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
4920 if (value
[0] == '~')
4921 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
4922 pwd
->pw_dir
, &value
[1]);
4924 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
4926 /* load new key bindings; if it fails, revert to default bindings */
4927 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
4931 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
4936 updatenumlockmask(void)
4939 XModifierKeymap
*modmap
;
4941 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
4943 modmap
= XGetModifierMapping(display
);
4944 for (i
= 0; i
< 8; i
++)
4945 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
4946 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
4947 == XKeysymToKeycode(display
, XK_Num_Lock
))
4948 numlockmask
= (1 << i
);
4950 XFreeModifiermap(modmap
);
4958 unsigned int modifiers
[] =
4959 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
4962 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
4963 updatenumlockmask();
4965 for (k
= 0; k
< ScreenCount(display
); k
++) {
4966 if (TAILQ_EMPTY(&screens
[k
].rl
))
4968 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
4969 TAILQ_FOREACH(kp
, &keys
, entry
) {
4970 if ((code
= XKeysymToKeycode(display
, kp
->keysym
)))
4971 for (j
= 0; j
< LENGTH(modifiers
); j
++)
4972 XGrabKey(display
, code
,
4973 kp
->mod
| modifiers
[j
],
4974 screens
[k
].root
, True
,
4975 GrabModeAsync
, GrabModeAsync
);
4981 grabbuttons(struct ws_win
*win
, int focused
)
4984 unsigned int modifiers
[] =
4985 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
4987 updatenumlockmask();
4988 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
4990 for (i
= 0; i
< LENGTH(buttons
); i
++)
4991 if (buttons
[i
].action
== client_click
)
4992 for (j
= 0; j
< LENGTH(modifiers
); j
++)
4993 XGrabButton(display
, buttons
[i
].button
,
4994 buttons
[i
].mask
| modifiers
[j
],
4995 win
->id
, False
, BUTTONMASK
,
4996 GrabModeAsync
, GrabModeSync
, None
,
4999 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
5000 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
5003 const char *quirkname
[] = {
5004 "NONE", /* config string for "no value" */
5013 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5014 #define SWM_Q_WS "\n|+ \t"
5016 parsequirks(char *qstr
, unsigned long *quirk
)
5026 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5028 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5029 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5030 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5031 DNPRINTF(SWM_D_QUIRK
,
5032 "parsequirks: %s\n", name
);
5037 *quirk
|= 1 << (i
-1);
5041 if (i
>= LENGTH(quirkname
)) {
5042 DNPRINTF(SWM_D_QUIRK
,
5043 "parsequirks: invalid quirk [%s]\n", name
);
5051 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5055 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5058 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5059 err(1, "quirk_insert: malloc");
5060 if ((qp
->class = strdup(class)) == NULL
)
5061 err(1, "quirk_insert: strdup");
5062 if ((qp
->name
= strdup(name
)) == NULL
)
5063 err(1, "quirk_insert: strdup");
5066 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5068 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5072 quirk_remove(struct quirk
*qp
)
5074 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5075 qp
->name
, qp
->quirk
);
5077 TAILQ_REMOVE(&quirks
, qp
, entry
);
5082 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5086 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5087 unsigned long quirk
)
5089 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5090 qp
->name
, qp
->quirk
);
5093 quirk_insert(class, name
, quirk
);
5095 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5099 setquirk(const char *class, const char *name
, unsigned long quirk
)
5103 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5106 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5107 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5111 quirk_replace(qp
, class, name
, quirk
);
5112 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5117 warnx("error: setquirk: cannot find class/name combination");
5121 quirk_insert(class, name
, quirk
);
5122 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5126 setconfquirk(char *selector
, char *value
, int flags
)
5128 char *cp
, *class, *name
;
5130 unsigned long quirks
;
5131 if (selector
== NULL
)
5133 if ((cp
= strchr(selector
, ':')) == NULL
)
5138 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5139 setquirk(class, name
, quirks
);
5146 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5147 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5148 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5149 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5150 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5151 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5152 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5153 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5154 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5155 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5156 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5157 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5158 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5161 /* conf file stuff */
5162 #define SWM_CONF_FILE "scrotwm.conf"
5164 enum { SWM_S_BAR_DELAY
, SWM_S_BAR_ENABLED
, SWM_S_BAR_BORDER_WIDTH
,
5165 SWM_S_STACK_ENABLED
, SWM_S_CLOCK_ENABLED
, SWM_S_CLOCK_FORMAT
,
5166 SWM_S_CYCLE_EMPTY
, SWM_S_CYCLE_VISIBLE
, SWM_S_SS_ENABLED
,
5167 SWM_S_TERM_WIDTH
, SWM_S_TITLE_CLASS_ENABLED
,
5168 SWM_S_TITLE_NAME_ENABLED
, SWM_S_WINDOW_NAME_ENABLED
, SWM_S_URGENT_ENABLED
,
5169 SWM_S_FOCUS_MODE
, SWM_S_DISABLE_BORDER
, SWM_S_BORDER_WIDTH
,
5170 SWM_S_BAR_FONT
, SWM_S_BAR_ACTION
, SWM_S_SPAWN_TERM
,
5171 SWM_S_SS_APP
, SWM_S_DIALOG_RATIO
, SWM_S_BAR_AT_BOTTOM
,
5172 SWM_S_VERBOSE_LAYOUT
, SWM_S_BAR_JUSTIFY
5176 setconfvalue(char *selector
, char *value
, int flags
)
5182 case SWM_S_BAR_DELAY
:
5183 bar_delay
= atoi(value
);
5185 case SWM_S_BAR_ENABLED
:
5186 bar_enabled
= atoi(value
);
5188 case SWM_S_BAR_BORDER_WIDTH
:
5189 bar_border_width
= atoi(value
);
5191 case SWM_S_BAR_AT_BOTTOM
:
5192 bar_at_bottom
= atoi(value
);
5194 case SWM_S_BAR_JUSTIFY
:
5195 if (!strcmp(value
, "left"))
5196 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5197 else if (!strcmp(value
, "center"))
5198 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5199 else if (!strcmp(value
, "right"))
5200 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5202 errx(1, "invalid bar_justify");
5204 case SWM_S_STACK_ENABLED
:
5205 stack_enabled
= atoi(value
);
5207 case SWM_S_CLOCK_ENABLED
:
5208 clock_enabled
= atoi(value
);
5210 case SWM_S_CLOCK_FORMAT
:
5211 #ifndef SWM_DENY_CLOCK_FORMAT
5213 if ((clock_format
= strdup(value
)) == NULL
)
5214 err(1, "setconfvalue: clock_format");
5217 case SWM_S_CYCLE_EMPTY
:
5218 cycle_empty
= atoi(value
);
5220 case SWM_S_CYCLE_VISIBLE
:
5221 cycle_visible
= atoi(value
);
5223 case SWM_S_SS_ENABLED
:
5224 ss_enabled
= atoi(value
);
5226 case SWM_S_TERM_WIDTH
:
5227 term_width
= atoi(value
);
5229 case SWM_S_TITLE_CLASS_ENABLED
:
5230 title_class_enabled
= atoi(value
);
5232 case SWM_S_WINDOW_NAME_ENABLED
:
5233 window_name_enabled
= atoi(value
);
5235 case SWM_S_TITLE_NAME_ENABLED
:
5236 title_name_enabled
= atoi(value
);
5238 case SWM_S_URGENT_ENABLED
:
5239 urgent_enabled
= atoi(value
);
5241 case SWM_S_FOCUS_MODE
:
5242 if (!strcmp(value
, "default"))
5243 focus_mode
= SWM_FOCUS_DEFAULT
;
5244 else if (!strcmp(value
, "follow_cursor"))
5245 focus_mode
= SWM_FOCUS_FOLLOW
;
5246 else if (!strcmp(value
, "synergy"))
5247 focus_mode
= SWM_FOCUS_SYNERGY
;
5249 errx(1, "focus_mode");
5251 case SWM_S_DISABLE_BORDER
:
5252 disable_border
= atoi(value
);
5254 case SWM_S_BORDER_WIDTH
:
5255 border_width
= atoi(value
);
5257 case SWM_S_BAR_FONT
:
5259 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5260 err(1, "setconfvalue: asprintf: failed to allocate "
5261 "memory for bar_fonts.");
5265 case SWM_S_BAR_ACTION
:
5267 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5268 err(1, "setconfvalue: bar_action");
5270 case SWM_S_SPAWN_TERM
:
5271 free(spawn_term
[0]);
5272 if ((spawn_term
[0] = strdup(value
)) == NULL
)
5273 err(1, "setconfvalue: spawn_term");
5277 case SWM_S_DIALOG_RATIO
:
5278 dialog_ratio
= atof(value
);
5279 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5282 case SWM_S_VERBOSE_LAYOUT
:
5283 verbose_layout
= atoi(value
);
5284 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5286 layouts
[i
].l_string
= fancy_stacker
;
5288 layouts
[i
].l_string
= plain_stacker
;
5298 setconfmodkey(char *selector
, char *value
, int flags
)
5300 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5301 update_modkey(Mod1Mask
);
5302 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5303 update_modkey(Mod2Mask
);
5304 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5305 update_modkey(Mod3Mask
);
5306 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5307 update_modkey(Mod4Mask
);
5314 setconfcolor(char *selector
, char *value
, int flags
)
5316 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5321 setconfregion(char *selector
, char *value
, int flags
)
5323 custom_region(value
);
5328 setautorun(char *selector
, char *value
, int flags
)
5338 if (getenv("SWM_STARTED"))
5342 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5343 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5345 if (ws_id
< 0 || ws_id
>= SWM_WS_MAX
)
5346 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5349 * This is a little intricate
5351 * If the pid already exists we simply reuse it because it means it was
5352 * used before AND not claimed by manage_window. We get away with
5353 * altering it in the parent after INSERT because this can not be a race
5356 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5359 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5361 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5362 err(1, "setautorun: realloc");
5363 a
.argv
[argc
- 1] = ap
;
5366 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5367 err(1, "setautorun: realloc");
5368 a
.argv
[argc
] = NULL
;
5370 if ((pid
= fork()) == 0) {
5371 spawn(ws_id
, &a
, 1);
5380 p
= calloc(1, sizeof *p
);
5383 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
5393 setlayout(char *selector
, char *value
, int flags
)
5395 int ws_id
, i
, x
, mg
, ma
, si
, raise
;
5396 int st
= SWM_V_STACK
;
5398 struct workspace
*ws
;
5400 if (getenv("SWM_STARTED"))
5404 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
5405 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
5406 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5407 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5410 if (ws_id
< 0 || ws_id
>= SWM_WS_MAX
)
5411 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
5413 if (!strcasecmp(s
, "vertical"))
5415 else if (!strcasecmp(s
, "horizontal"))
5417 else if (!strcasecmp(s
, "fullscreen"))
5420 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5421 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5424 for (i
= 0; i
< ScreenCount(display
); i
++) {
5425 ws
= (struct workspace
*)&screens
[i
].ws
;
5426 ws
[ws_id
].cur_layout
= &layouts
[st
];
5428 ws
[ws_id
].always_raise
= raise
;
5429 if (st
== SWM_MAX_STACK
)
5433 for (x
= 0; x
< abs(mg
); x
++) {
5434 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5435 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
5436 SWM_ARG_ID_MASTERSHRINK
);
5440 for (x
= 0; x
< abs(ma
); x
++) {
5441 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5442 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
5443 SWM_ARG_ID_MASTERDEL
);
5447 for (x
= 0; x
< abs(si
); x
++) {
5448 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5449 si
>= 0 ? SWM_ARG_ID_STACKINC
:
5450 SWM_ARG_ID_STACKDEC
);
5458 /* config options */
5459 struct config_option
{
5461 int (*func
)(char*, char*, int);
5464 struct config_option configopt
[] = {
5465 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
5466 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
5467 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
5468 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
5469 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
5470 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
5471 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
5472 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
5473 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
5474 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
5475 { "keyboard_mapping", setkeymapping
, 0 },
5476 { "bind", setconfbinding
, 0 },
5477 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
5478 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
5479 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
5480 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
5481 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
5482 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
5483 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
5484 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
5485 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
5486 { "modkey", setconfmodkey
, 0 },
5487 { "program", setconfspawn
, 0 },
5488 { "quirk", setconfquirk
, 0 },
5489 { "region", setconfregion
, 0 },
5490 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
5491 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
5492 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
5493 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
5494 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
5495 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
5496 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
5497 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
5498 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
5499 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
5500 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
5501 { "autorun", setautorun
, 0 },
5502 { "layout", setlayout
, 0 },
5507 conf_load(char *filename
, int keymapping
)
5510 char *line
, *cp
, *optsub
, *optval
;
5511 size_t linelen
, lineno
= 0;
5512 int wordlen
, i
, optind
;
5513 struct config_option
*opt
;
5515 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
5517 if (filename
== NULL
) {
5518 warnx("conf_load: no filename");
5521 if ((config
= fopen(filename
, "r")) == NULL
) {
5522 warn("conf_load: fopen: %s", filename
);
5526 while (!feof(config
)) {
5527 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
5530 err(1, "%s", filename
);
5535 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
5536 if (cp
[0] == '\0') {
5541 /* get config option */
5542 wordlen
= strcspn(cp
, "=[ \t\n");
5544 warnx("%s: line %zd: no option found",
5549 for (i
= 0; i
< LENGTH(configopt
); i
++) {
5550 opt
= &configopt
[i
];
5551 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
5552 strlen(opt
->optname
) == wordlen
) {
5558 warnx("%s: line %zd: unknown option %.*s",
5559 filename
, lineno
, wordlen
, cp
);
5562 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
5563 warnx("%s: line %zd: invalid option %.*s",
5564 filename
, lineno
, wordlen
, cp
);
5568 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
5569 /* get [selector] if any */
5573 wordlen
= strcspn(cp
, "]");
5576 warnx("%s: line %zd: syntax error",
5581 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
5583 warnx("%s: line %zd: unable to allocate"
5584 "memory for selector", filename
,
5590 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
5592 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
5594 optval
= strdup(cp
);
5595 /* call function to deal with it all */
5596 if (configopt
[optind
].func(optsub
, optval
,
5597 configopt
[optind
].funcflags
) != 0)
5598 errx(1, "%s: line %zd: invalid data for %s",
5599 filename
, lineno
, configopt
[optind
].optname
);
5606 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
5613 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
5619 set_child_transient(struct ws_win
*win
, Window
*trans
)
5621 struct ws_win
*parent
, *w
;
5622 XWMHints
*wmh
= NULL
;
5623 struct swm_region
*r
;
5624 struct workspace
*ws
;
5626 parent
= find_window(win
->transient
);
5628 parent
->child_trans
= win
;
5630 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
5631 " for 0x%lx trans 0x%lx\n", win
->id
, win
->transient
);
5633 if (win
->hints
== NULL
) {
5634 warnx("no hints for 0x%lx", win
->id
);
5638 r
= root_to_region(win
->wa
.root
);
5640 /* parent doen't exist in our window list */
5641 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5645 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
5646 warnx("can't get hints for 0x%lx", w
->id
);
5650 if (win
->hints
->window_group
!= wmh
->window_group
)
5653 w
->child_trans
= win
;
5654 win
->transient
= w
->id
;
5656 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
5657 "transient to 0x%lx\n", win
->transient
);
5667 window_get_pid(Window win
)
5669 Atom actual_type_return
;
5670 int actual_format_return
= 0;
5671 unsigned long nitems_return
= 0;
5672 unsigned long bytes_after_return
= 0;
5676 unsigned char *prop
= NULL
;
5678 if (XGetWindowProperty(display
, win
,
5679 XInternAtom(display
, "_NET_WM_PID", False
), 0, 1, False
,
5680 XA_CARDINAL
, &actual_type_return
, &actual_format_return
,
5681 &nitems_return
, &bytes_after_return
,
5682 (unsigned char**)(void*)&pid
) != Success
)
5684 if (actual_type_return
!= XA_CARDINAL
)
5695 if (XGetWindowProperty(display
, win
,
5696 XInternAtom(display
, "_SWM_PID", False
), 0, SWM_PROPLEN
, False
,
5697 XA_STRING
, &actual_type_return
, &actual_format_return
,
5698 &nitems_return
, &bytes_after_return
, &prop
) != Success
)
5700 if (actual_type_return
!= XA_STRING
)
5705 ret
= strtonum((const char *)prop
, 0, UINT_MAX
, &errstr
);
5706 /* ignore error because strtonum returns 0 anyway */
5713 manage_window(Window id
)
5716 struct workspace
*ws
;
5717 struct ws_win
*win
, *ww
;
5718 int format
, i
, ws_idx
, n
, border_me
= 0;
5719 unsigned long nitems
, bytes
;
5720 Atom ws_idx_atom
= 0, type
;
5721 Atom
*prot
= NULL
, *pp
;
5722 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
5723 struct swm_region
*r
;
5730 if ((win
= find_window(id
)) != NULL
)
5731 return (win
); /* already being managed */
5733 /* see if we are on the unmanaged list */
5734 if ((win
= find_unmanaged_window(id
)) != NULL
) {
5735 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
5736 "window: 0x%lx\n", win
->id
);
5737 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5738 if (win
->transient
) {
5739 set_child_transient(win
, &trans
);
5740 } if (trans
&& (ww
= find_window(trans
)))
5741 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
5743 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
5744 ewmh_update_actions(win
);
5748 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
5749 err(1, "manage_window: calloc: failed to allocate memory for "
5754 /* see if we need to override the workspace */
5755 p
= find_pid(window_get_pid(id
));
5757 /* Get all the window data in one shot */
5758 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
5760 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
5761 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
5763 XGetWindowAttributes(display
, id
, &win
->wa
);
5764 XGetWMNormalHints(display
, id
, &win
->sh
, &win
->sh_mask
);
5765 win
->hints
= XGetWMHints(display
, id
);
5766 XGetTransientForHint(display
, id
, &trans
);
5768 win
->transient
= trans
;
5769 set_child_transient(win
, &trans
);
5770 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, "
5771 "transient: 0x%lx\n", win
->id
, win
->transient
);
5774 /* get supported protocols */
5775 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
5776 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
5777 if (*pp
== takefocus
)
5778 win
->take_focus
= 1;
5780 win
->can_delete
= 1;
5786 win
->iconic
= get_iconic(win
);
5789 * Figure out where to put the window. If it was previously assigned to
5790 * a workspace (either by spawn() or manually moving), and isn't
5791 * transient, * put it in the same workspace
5793 r
= root_to_region(win
->wa
.root
);
5795 ws
= &r
->s
->ws
[p
->ws
];
5796 TAILQ_REMOVE(&pidlist
, p
, entry
);
5799 } else if (prop
&& win
->transient
== 0) {
5800 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
5801 ws_idx
= strtonum((const char *)prop
, 0, 9, &errstr
);
5803 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
5806 ws
= &r
->s
->ws
[ws_idx
];
5809 /* this should launch transients in the same ws as parent */
5811 if ((ww
= find_window(trans
)) != NULL
)
5817 warnx("manage_window: fix this "
5823 /* set up the window layout */
5826 win
->s
= r
->s
; /* this never changes */
5827 if (trans
&& (ww
= find_window(trans
)))
5828 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
5830 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
5832 WIDTH(win
) = win
->wa
.width
;
5833 HEIGHT(win
) = win
->wa
.height
;
5836 win
->g_floatvalid
= 0;
5837 win
->floatmaxed
= 0;
5838 win
->ewmh_flags
= 0;
5840 /* Set window properties so we can remember this after reincarnation */
5841 if (ws_idx_atom
&& prop
== NULL
&&
5842 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
5844 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
5846 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
5847 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
5852 ewmh_autoquirk(win
);
5854 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
5855 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
5856 win
->ch
.res_class
, win
->ch
.res_name
);
5858 /* java is retarded so treat it special */
5859 if (strstr(win
->ch
.res_name
, "sun-awt")) {
5864 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5865 if (!strcmp(win
->ch
.res_class
, qp
->class) &&
5866 !strcmp(win
->ch
.res_name
, qp
->name
)) {
5867 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
5868 "class: %s, name: %s\n", win
->ch
.res_class
,
5870 if (qp
->quirk
& SWM_Q_FLOAT
) {
5874 win
->quirks
= qp
->quirk
;
5879 /* alter window position if quirky */
5880 if (win
->quirks
& SWM_Q_ANYWHERE
) {
5881 win
->manual
= 1; /* don't center the quirky windows */
5882 bzero(&wc
, sizeof wc
);
5884 if (bar_enabled
&& Y(win
) < bar_height
) {
5885 Y(win
) = wc
.y
= bar_height
;
5888 if (WIDTH(win
) + X(win
) > WIDTH(r
)) {
5889 X(win
) = wc
.x
= WIDTH(r
) - WIDTH(win
) - 2;
5895 /* Reset font sizes (the bruteforce way; no default keybinding). */
5896 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
5897 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
5898 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
5899 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
5900 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
5903 ewmh_get_win_state(win
);
5904 ewmh_update_actions(win
);
5905 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
5909 bzero(&wc
, sizeof wc
);
5910 wc
.border_width
= border_width
;
5911 mask
= CWBorderWidth
;
5912 XConfigureWindow(display
, win
->id
, mask
, &wc
);
5915 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
5916 PropertyChangeMask
| StructureNotifyMask
);
5918 /* floaters need to be mapped if they are in the current workspace */
5919 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
5920 XMapRaised(display
, win
->id
);
5926 free_window(struct ws_win
*win
)
5928 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%lx\n", win
->id
);
5933 /* needed for restart wm */
5934 set_win_state(win
, WithdrawnState
);
5936 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5938 if (win
->ch
.res_class
)
5939 XFree(win
->ch
.res_class
);
5940 if (win
->ch
.res_name
)
5941 XFree(win
->ch
.res_name
);
5946 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
5952 unmanage_window(struct ws_win
*win
)
5954 struct ws_win
*parent
;
5959 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%lx\n", win
->id
);
5961 if (win
->transient
) {
5962 parent
= find_window(win
->transient
);
5964 parent
->child_trans
= NULL
;
5967 /* focus on root just in case */
5968 XSetInputFocus(display
, PointerRoot
, PointerRoot
, CurrentTime
);
5977 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
5978 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
5984 focus_magic(struct ws_win
*win
)
5986 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%lx\n", WINID(win
));
5989 /* if there are no windows clear the status-bar */
5994 if (win
->child_trans
) {
5995 /* win = parent & has a transient so focus on that */
5997 focus_win(win
->child_trans
);
5998 if (win
->child_trans
->take_focus
)
5999 client_msg(win
, takefocus
);
6001 /* make sure transient hasn't disappeared */
6002 if (validate_win(win
->child_trans
) == 0) {
6003 focus_win(win
->child_trans
);
6004 if (win
->child_trans
->take_focus
)
6005 client_msg(win
->child_trans
, takefocus
);
6007 win
->child_trans
= NULL
;
6009 if (win
->take_focus
)
6010 client_msg(win
, takefocus
);
6016 if (win
->take_focus
)
6017 client_msg(win
, takefocus
);
6024 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%lx\n", e
->xexpose
.window
);
6031 XKeyEvent
*ev
= &e
->xkey
;
6034 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
6035 TAILQ_FOREACH(kp
, &keys
, entry
)
6036 if (keysym
== kp
->keysym
6037 && CLEANMASK(kp
->mod
) == CLEANMASK(ev
->state
)
6038 && keyfuncs
[kp
->funcid
].func
) {
6039 if (kp
->funcid
== kf_spawn_custom
)
6041 root_to_region(ev
->root
),
6042 &(keyfuncs
[kp
->funcid
].args
),
6046 keyfuncs
[kp
->funcid
].func(
6047 root_to_region(ev
->root
),
6048 &(keyfuncs
[kp
->funcid
].args
)
6054 buttonpress(XEvent
*e
)
6058 XButtonPressedEvent
*ev
= &e
->xbutton
;
6060 if ((win
= find_window(ev
->window
)) == NULL
)
6064 action
= client_click
;
6066 for (i
= 0; i
< LENGTH(buttons
); i
++)
6067 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6068 buttons
[i
].button
== ev
->button
&&
6069 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
6070 buttons
[i
].func(win
, &buttons
[i
].args
);
6074 configurerequest(XEvent
*e
)
6076 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
6081 if ((win
= find_window(ev
->window
)) == NULL
)
6082 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6085 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%lx, new: %s\n",
6086 ev
->window
, YESNO(new));
6089 bzero(&wc
, sizeof wc
);
6092 wc
.width
= ev
->width
;
6093 wc
.height
= ev
->height
;
6094 wc
.border_width
= ev
->border_width
;
6095 wc
.sibling
= ev
->above
;
6096 wc
.stack_mode
= ev
->detail
;
6097 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
6099 config_win(win
, ev
);
6104 configurenotify(XEvent
*e
)
6108 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%lx\n",
6109 e
->xconfigure
.window
);
6111 win
= find_window(e
->xconfigure
.window
);
6113 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &win
->sh_mask
);
6117 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6118 drain_enter_notify();
6123 destroynotify(XEvent
*e
)
6126 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
6128 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%lx\n", ev
->window
);
6130 if ((win
= find_window(ev
->window
)) == NULL
) {
6131 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6137 /* make sure we focus on something */
6140 unmanage_window(win
);
6142 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6143 drain_enter_notify();
6148 enternotify(XEvent
*e
)
6150 XCrossingEvent
*ev
= &e
->xcrossing
;
6155 Window focus_return
;
6156 int revert_to_return
;
6158 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%lx, mode: %d, detail: "
6159 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6160 "state: %d\n", ev
->window
, ev
->mode
, ev
->detail
, ev
->root
,
6161 ev
->subwindow
, YESNO(ev
->same_screen
), YESNO(ev
->focus
), ev
->state
);
6163 if (ev
->mode
!= NotifyNormal
) {
6164 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6169 switch (focus_mode
) {
6170 case SWM_FOCUS_DEFAULT
:
6172 case SWM_FOCUS_FOLLOW
:
6174 case SWM_FOCUS_SYNERGY
:
6177 * all these checks need to be in this order because the
6178 * XCheckTypedWindowEvent relies on weeding out the previous events
6180 * making this code an option would enable a follow mouse for focus
6185 * state is set when we are switching workspaces and focus is set when
6186 * the window or a subwindow already has focus (occurs during restart).
6188 * Only honor the focus flag if last_focus_event is not FocusOut,
6189 * this allows scrotwm to continue to control focus when another
6190 * program is also playing with it.
6192 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6193 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6198 * happens when a window is created or destroyed and the border
6199 * crosses the mouse pointer and when switching ws
6201 * we need the subwindow test to see if we came from root in order
6202 * to give focus to floaters
6204 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6205 ev
->subwindow
== 0) {
6206 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6210 /* this window already has focus */
6211 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6212 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6216 /* this window is being deleted or moved to another ws */
6217 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6219 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6220 XPutBackEvent(display
, &cne
);
6224 if ((win
= find_window(ev
->window
)) == NULL
) {
6225 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6230 * In fullstack kill all enters unless they come from a different ws
6231 * (i.e. another region) or focus has been grabbed externally.
6233 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6234 last_focus_event
!= FocusOut
) {
6235 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6236 if ((w
= find_window(focus_return
)) == NULL
||
6238 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6246 if ((win
= find_window(ev
->window
)) == NULL
) {
6247 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6252 * if we have more enternotifies let them handle it in due time
6254 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
6255 DNPRINTF(SWM_D_EVENT
,
6256 "ignoring enternotify: got more enternotify\n");
6257 XPutBackEvent(display
, &cne
);
6264 /* lets us use one switch statement for arbitrary mode/detail combinations */
6265 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6268 focusevent(XEvent
*e
)
6272 u_int32_t mode_detail
;
6273 XFocusChangeEvent
*ev
= &e
->xfocus
;
6275 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: 0x%lx mode: %d "
6276 "detail: %d\n", ev
->type
== FocusIn
? "entering" : "leaving",
6277 ev
->window
, ev
->mode
, ev
->detail
);
6279 if (last_focus_event
== ev
->type
) {
6280 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
6284 last_focus_event
= ev
->type
;
6285 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
6287 switch (mode_detail
) {
6288 /* synergy client focus operations */
6289 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
6290 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
6292 /* synergy server focus operations */
6293 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
6295 /* Entering applications like rdesktop that mangle the pointer */
6296 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
6298 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
6299 XSetWindowBorder(display
, win
->id
,
6300 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
6301 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
6304 warnx("ignoring focusevent");
6305 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
6312 mapnotify(XEvent
*e
)
6315 XMapEvent
*ev
= &e
->xmap
;
6317 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%lx\n", ev
->window
);
6319 win
= manage_window(ev
->window
);
6321 set_win_state(win
, NormalState
);
6325 mappingnotify(XEvent
*e
)
6327 XMappingEvent
*ev
= &e
->xmapping
;
6329 XRefreshKeyboardMapping(ev
);
6330 if (ev
->request
== MappingKeyboard
)
6335 maprequest(XEvent
*e
)
6338 struct swm_region
*r
;
6339 XWindowAttributes wa
;
6340 XMapRequestEvent
*ev
= &e
->xmaprequest
;
6342 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%lx\n",
6343 e
->xmaprequest
.window
);
6345 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
6347 if (wa
.override_redirect
)
6350 win
= manage_window(e
->xmaprequest
.window
);
6352 return; /* can't happen */
6356 /* make new win focused */
6357 r
= root_to_region(win
->wa
.root
);
6358 if (win
->ws
== r
->ws
)
6363 propertynotify(XEvent
*e
)
6366 XPropertyEvent
*ev
= &e
->xproperty
;
6369 name
= XGetAtomName(display
, ev
->atom
);
6370 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%lx, atom: %s\n",
6375 win
= find_window(ev
->window
);
6379 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
6380 update_iconic(win
, 0);
6381 XMapRaised(display
, win
->id
);
6389 case XA_WM_NORMAL_HINTS
:
6391 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
6392 warnx("normal hints: flag 0x%x", win
->sh
.flags
);
6393 if (win
->sh
.flags
& PMinSize
) {
6394 WIDTH(win
) = win
->sh
.min_width
;
6395 HEIGHT(win
) = win
->sh
.min_height
;
6396 warnx("min %d %d", WIDTH(win
), HEIGHT(win
));
6398 XMoveResizeWindow(display
, win
->id
,
6399 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
));
6402 if (title_name_enabled
|| title_class_enabled
)
6406 if (window_name_enabled
)
6415 unmapnotify(XEvent
*e
)
6419 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%lx\n", e
->xunmap
.window
);
6421 /* determine if we need to help unmanage this window */
6422 win
= find_window(e
->xunmap
.window
);
6426 if (getstate(e
->xunmap
.window
) == NormalState
) {
6427 unmanage_window(win
);
6430 /* giant hack for apps that don't destroy transient windows */
6431 /* eat a bunch of events to prevent remanaging the window */
6433 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6434 EnterWindowMask
, &cne
))
6436 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6437 StructureNotifyMask
, &cne
))
6439 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6440 SubstructureNotifyMask
, &cne
))
6442 /* resend unmap because we ated it */
6443 XUnmapWindow(display
, e
->xunmap
.window
);
6446 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6447 drain_enter_notify();
6451 visibilitynotify(XEvent
*e
)
6454 struct swm_region
*r
;
6456 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%lx\n",
6457 e
->xvisibility
.window
);
6458 if (e
->xvisibility
.state
== VisibilityUnobscured
)
6459 for (i
= 0; i
< ScreenCount(display
); i
++)
6460 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6461 if (e
->xvisibility
.window
== r
->bar_window
)
6466 clientmessage(XEvent
*e
)
6468 XClientMessageEvent
*ev
;
6473 win
= find_window(ev
->window
);
6477 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx, type: %ld\n",
6478 ev
->window
, ev
->message_type
);
6480 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
6481 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
6484 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
6485 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
6486 if (win
->can_delete
)
6487 client_msg(win
, adelete
);
6489 XKillClient(display
, win
->id
);
6491 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
6492 DNPRINTF(SWM_D_EVENT
,
6493 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
6494 if (win
->floating
) {
6495 if (ev
->data
.l
[0] & (1<<8)) /* x */
6496 X(win
) = ev
->data
.l
[1];
6497 if (ev
->data
.l
[0] & (1<<9)) /* y */
6498 Y(win
) = ev
->data
.l
[2];
6499 if (ev
->data
.l
[0] & (1<<10)) /* width */
6500 WIDTH(win
) = ev
->data
.l
[3];
6501 if (ev
->data
.l
[0] & (1<<11)) /* height */
6502 HEIGHT(win
) = ev
->data
.l
[4];
6507 /* TODO: Change stack sizes */
6508 /* notify no change was made. */
6509 config_win(win
, NULL
);
6512 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
6513 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
6514 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
6516 ewmh_update_win_state(win
, ev
->data
.l
[2],
6524 xerror_start(Display
*d
, XErrorEvent
*ee
)
6531 xerror(Display
*d
, XErrorEvent
*ee
)
6533 /* warnx("error: %p %p", display, ee); */
6541 xerrorxlib
= XSetErrorHandler(xerror_start
);
6543 /* this causes an error if some other window manager is running */
6544 XSelectInput(display
, DefaultRootWindow(display
),
6545 SubstructureRedirectMask
);
6546 XSync(display
, False
);
6550 XSetErrorHandler(xerror
);
6551 XSync(display
, False
);
6556 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
6558 struct swm_region
*r
, *n
;
6559 struct workspace
*ws
= NULL
;
6562 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
6563 s
->idx
, w
, h
, x
, y
);
6565 /* remove any conflicting regions */
6566 n
= TAILQ_FIRST(&s
->rl
);
6569 n
= TAILQ_NEXT(r
, entry
);
6570 if (X(r
) < (x
+ w
) &&
6571 (X(r
) + WIDTH(r
)) > x
&&
6573 (Y(r
) + HEIGHT(r
)) > y
) {
6574 if (r
->ws
->r
!= NULL
)
6575 r
->ws
->old_r
= r
->ws
->r
;
6577 XDestroyWindow(display
, r
->bar_window
);
6578 TAILQ_REMOVE(&s
->rl
, r
, entry
);
6579 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
6583 /* search old regions for one to reuse */
6585 /* size + location match */
6586 TAILQ_FOREACH(r
, &s
->orl
, entry
)
6587 if (X(r
) == x
&& Y(r
) == y
&&
6588 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
6592 TAILQ_FOREACH(r
, &s
->orl
, entry
)
6593 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
6597 TAILQ_REMOVE(&s
->orl
, r
, entry
);
6598 /* try to use old region's workspace */
6599 if (r
->ws
->r
== NULL
)
6602 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
6603 err(1, "new_region: calloc: failed to allocate memory "
6606 /* if we don't have a workspace already, find one */
6608 for (i
= 0; i
< SWM_WS_MAX
; i
++)
6609 if (s
->ws
[i
].r
== NULL
) {
6616 errx(1, "new_region: no free workspaces");
6627 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
6633 #ifdef SWM_XRR_HAS_CRTC
6635 XRRScreenResources
*sr
;
6638 #endif /* SWM_XRR_HAS_CRTC */
6639 struct swm_region
*r
;
6642 if (i
>= ScreenCount(display
))
6643 errx(1, "scan_xrandr: invalid screen");
6645 /* remove any old regions */
6646 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
6647 r
->ws
->old_r
= r
->ws
->r
= NULL
;
6648 XDestroyWindow(display
, r
->bar_window
);
6649 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
6650 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
6654 /* map virtual screens onto physical screens */
6655 #ifdef SWM_XRR_HAS_CRTC
6656 if (xrandr_support
) {
6657 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
6659 new_region(&screens
[i
], 0, 0,
6660 DisplayWidth(display
, i
),
6661 DisplayHeight(display
, i
));
6665 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
6666 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
6667 if (ci
->noutput
== 0)
6670 if (ci
!= NULL
&& ci
->mode
== None
)
6671 new_region(&screens
[i
], 0, 0,
6672 DisplayWidth(display
, i
),
6673 DisplayHeight(display
, i
));
6675 new_region(&screens
[i
],
6676 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
6679 XRRFreeCrtcInfo(ci
);
6680 XRRFreeScreenResources(sr
);
6682 #endif /* SWM_XRR_HAS_CRTC */
6684 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
6685 DisplayHeight(display
, i
));
6690 screenchange(XEvent
*e
) {
6691 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
6692 struct swm_region
*r
;
6695 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%lx\n", xe
->root
);
6697 if (!XRRUpdateConfiguration(e
))
6700 /* silly event doesn't include the screen index */
6701 for (i
= 0; i
< ScreenCount(display
); i
++)
6702 if (screens
[i
].root
== xe
->root
)
6704 if (i
>= ScreenCount(display
))
6705 errx(1, "screenchange: screen not found");
6707 /* brute force for now, just re-enumerate the regions */
6710 /* add bars to all regions */
6711 for (i
= 0; i
< ScreenCount(display
); i
++)
6712 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6715 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6716 drain_enter_notify();
6722 Window d1
, d2
, *wins
= NULL
;
6723 XWindowAttributes wa
;
6728 for (i
= 0; i
< ScreenCount(display
); i
++) {
6729 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
6732 /* attach windows to a region */
6733 /* normal windows */
6734 for (j
= 0; j
< no
; j
++) {
6735 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6736 wa
.override_redirect
||
6737 XGetTransientForHint(display
, wins
[j
], &d1
))
6740 state
= getstate(wins
[j
]);
6741 manage
= state
== IconicState
;
6742 if (wa
.map_state
== IsViewable
|| manage
)
6743 manage_window(wins
[j
]);
6745 /* transient windows */
6746 for (j
= 0; j
< no
; j
++) {
6747 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6748 wa
.override_redirect
)
6751 state
= getstate(wins
[j
]);
6752 manage
= state
== IconicState
;
6753 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
6755 manage_window(wins
[j
]);
6768 int errorbase
, major
, minor
;
6769 struct workspace
*ws
;
6772 if ((screens
= calloc(ScreenCount(display
),
6773 sizeof(struct swm_screen
))) == NULL
)
6774 err(1, "setup_screens: calloc: failed to allocate memory for "
6777 /* initial Xrandr setup */
6778 xrandr_support
= XRRQueryExtension(display
,
6779 &xrandr_eventbase
, &errorbase
);
6781 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
6784 /* map physical screens */
6785 for (i
= 0; i
< ScreenCount(display
); i
++) {
6786 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
6788 TAILQ_INIT(&screens
[i
].rl
);
6789 TAILQ_INIT(&screens
[i
].orl
);
6790 screens
[i
].root
= RootWindow(display
, i
);
6792 /* set default colors */
6793 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
6794 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
6795 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
6796 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
6797 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
6799 /* create graphics context on screen with default font color */
6800 screens
[i
].bar_gc
= XCreateGC(display
, screens
[i
].root
, 0,
6803 XSetForeground(display
, screens
[i
].bar_gc
,
6804 screens
[i
].c
[SWM_S_COLOR_BAR_FONT
].color
);
6806 /* set default cursor */
6807 XDefineCursor(display
, screens
[i
].root
,
6808 XCreateFontCursor(display
, XC_left_ptr
));
6810 /* init all workspaces */
6811 /* XXX these should be dynamically allocated too */
6812 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
6813 ws
= &screens
[i
].ws
[j
];
6819 TAILQ_INIT(&ws
->winlist
);
6820 TAILQ_INIT(&ws
->unmanagedlist
);
6822 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
6823 if (layouts
[k
].l_config
!= NULL
)
6824 layouts
[k
].l_config(ws
,
6825 SWM_ARG_ID_STACKINIT
);
6826 ws
->cur_layout
= &layouts
[0];
6827 ws
->cur_layout
->l_string(ws
);
6833 XRRSelectInput(display
, screens
[i
].root
,
6834 RRScreenChangeNotifyMask
);
6841 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
6842 err(1, "setup_globals: strdup: failed to allocate memory.");
6844 if ((spawn_term
[0] = strdup("xterm")) == NULL
)
6845 err(1, "setup_globals: strdup: failed to allocate memory.");
6847 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
6848 err(1, "setup_globals: strdup: failed to allocate memory.");
6855 Atom netwmcheck
, netwmname
, utf8_string
;
6858 /* work around sun jdk bugs, code from wmname */
6859 netwmcheck
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
6860 netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
6861 utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6862 for (i
= 0; i
< ScreenCount(display
); i
++) {
6863 root
= screens
[i
].root
;
6864 win
= XCreateSimpleWindow(display
,root
, 0, 0, 1, 1, 0,
6865 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
,
6866 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
);
6868 XChangeProperty(display
, root
, netwmcheck
, XA_WINDOW
, 32,
6869 PropModeReplace
, (unsigned char *)&win
,1);
6870 XChangeProperty(display
, win
, netwmcheck
, XA_WINDOW
, 32,
6871 PropModeReplace
, (unsigned char *)&win
,1);
6872 XChangeProperty(display
, win
, netwmname
, utf8_string
, 8,
6873 PropModeReplace
, (unsigned char*)"LG3D", strlen("LG3D"));
6878 main(int argc
, char *argv
[])
6880 struct swm_region
*r
, *rr
;
6881 struct ws_win
*winfocus
= NULL
;
6884 char conf
[PATH_MAX
], *cfile
= NULL
;
6889 struct sigaction sact
;
6892 warnx("Welcome to scrotwm V%s Build: %s", SCROTWM_VERSION
, buildstr
);
6893 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
6895 warnx("no locale support");
6897 if (!X_HAVE_UTF8_STRING
)
6898 warnx("no UTF-8 support");
6900 if (!(display
= XOpenDisplay(0)))
6901 errx(1, "can not open display");
6904 errx(1, "other wm running");
6906 /* handle some signals */
6907 bzero(&sact
, sizeof(sact
));
6908 sigemptyset(&sact
.sa_mask
);
6910 sact
.sa_handler
= sighdlr
;
6911 sigaction(SIGINT
, &sact
, NULL
);
6912 sigaction(SIGQUIT
, &sact
, NULL
);
6913 sigaction(SIGTERM
, &sact
, NULL
);
6914 sigaction(SIGHUP
, &sact
, NULL
);
6916 sact
.sa_handler
= sighdlr
;
6917 sact
.sa_flags
= SA_NOCLDSTOP
;
6918 sigaction(SIGCHLD
, &sact
, NULL
);
6920 astate
= XInternAtom(display
, "WM_STATE", False
);
6921 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
6922 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
6923 takefocus
= XInternAtom(display
, "WM_TAKE_FOCUS", False
);
6924 a_wmname
= XInternAtom(display
, "WM_NAME", False
);
6925 a_netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
6926 a_utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6927 a_string
= XInternAtom(display
, "STRING", False
);
6928 a_swm_iconic
= XInternAtom(display
, "_SWM_ICONIC", False
);
6930 /* look for local and global conf file */
6931 pwd
= getpwuid(getuid());
6933 errx(1, "invalid user: %d", getuid());
6942 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
6943 if (stat(conf
, &sb
) != -1) {
6944 if (S_ISREG(sb
.st_mode
))
6947 /* try global conf file */
6948 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
6949 if (!stat(conf
, &sb
))
6950 if (S_ISREG(sb
.st_mode
))
6954 /* load conf (if any) and refresh font color in bar graphics contexts */
6955 if (cfile
&& conf_load(cfile
, SWM_CONF_DEFAULT
) == 0)
6956 for (i
= 0; i
< ScreenCount(display
); ++i
)
6957 XSetForeground(display
, screens
[i
].bar_gc
,
6958 screens
[i
].c
[SWM_S_COLOR_BAR_FONT
].color
);
6961 /* set some values to work around bad programs */
6963 /* grab existing windows (before we build the bars) */
6966 if (getenv("SWM_STARTED") == NULL
)
6967 setenv("SWM_STARTED", "YES", 1);
6969 /* setup all bars */
6970 for (i
= 0; i
< ScreenCount(display
); i
++)
6971 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
6972 if (winfocus
== NULL
)
6973 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
6981 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6982 drain_enter_notify();
6984 xfd
= ConnectionNumber(display
);
6986 while (XPending(display
)) {
6987 XNextEvent(display
, &e
);
6990 if (e
.type
< LASTEvent
) {
6991 DNPRINTF(SWM_D_EVENTQ
,"XEvent: handled: %s, "
6992 "window: 0x%lx, type: %s (%d), %d remaining"
6993 "\n", YESNO(handler
[e
.type
]),
6994 e
.xany
.window
, geteventname(&e
),
6995 e
.type
, QLength(display
));
6997 if (handler
[e
.type
])
6998 handler
[e
.type
](&e
);
7000 DNPRINTF(SWM_D_EVENTQ
, "XRandr Event: window: "
7001 "0x%lx, type: %s (%d)\n", e
.xany
.window
,
7002 xrandr_geteventname(&e
), e
.type
);
7004 switch (e
.type
- xrandr_eventbase
) {
7005 case RRScreenChangeNotify
:
7014 /* if we are being restarted go focus on first window */
7016 rr
= winfocus
->ws
->r
;
7018 /* not a visible window */
7022 /* move pointer to first screen if multi screen */
7023 if (ScreenCount(display
) > 1 || outputs
> 1)
7024 XWarpPointer(display
, None
, rr
->s
[0].root
,
7026 Y(rr
) + (bar_enabled
? bar_height
: 0));
7028 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7038 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7040 DNPRINTF(SWM_D_MISC
, "select failed");
7041 if (restart_wm
== 1)
7042 restart(NULL
, NULL
);
7043 if (search_resp
== 1)
7056 for (i
= 0; i
< ScreenCount(display
); ++i
)
7057 if (screens
[i
].bar_gc
!= NULL
)
7058 XFreeGC(display
, screens
[i
].bar_gc
);
7060 XFreeFontSet(display
, bar_fs
);
7061 XCloseDisplay(display
);