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
;
432 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
433 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
435 /* physical screen mapping */
436 #define SWM_WS_MAX (10)
438 int idx
; /* screen index */
439 struct swm_region_list rl
; /* list of regions on this screen */
440 struct swm_region_list orl
; /* list of old regions */
442 struct workspace ws
[SWM_WS_MAX
];
448 } c
[SWM_S_COLOR_MAX
];
452 struct swm_screen
*screens
;
455 /* args to functions */
458 #define SWM_ARG_ID_FOCUSNEXT (0)
459 #define SWM_ARG_ID_FOCUSPREV (1)
460 #define SWM_ARG_ID_FOCUSMAIN (2)
461 #define SWM_ARG_ID_FOCUSCUR (4)
462 #define SWM_ARG_ID_SWAPNEXT (10)
463 #define SWM_ARG_ID_SWAPPREV (11)
464 #define SWM_ARG_ID_SWAPMAIN (12)
465 #define SWM_ARG_ID_MOVELAST (13)
466 #define SWM_ARG_ID_MASTERSHRINK (20)
467 #define SWM_ARG_ID_MASTERGROW (21)
468 #define SWM_ARG_ID_MASTERADD (22)
469 #define SWM_ARG_ID_MASTERDEL (23)
470 #define SWM_ARG_ID_STACKRESET (30)
471 #define SWM_ARG_ID_STACKINIT (31)
472 #define SWM_ARG_ID_CYCLEWS_UP (40)
473 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
474 #define SWM_ARG_ID_CYCLESC_UP (42)
475 #define SWM_ARG_ID_CYCLESC_DOWN (43)
476 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
477 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
478 #define SWM_ARG_ID_STACKINC (50)
479 #define SWM_ARG_ID_STACKDEC (51)
480 #define SWM_ARG_ID_SS_ALL (60)
481 #define SWM_ARG_ID_SS_WINDOW (61)
482 #define SWM_ARG_ID_DONTCENTER (70)
483 #define SWM_ARG_ID_CENTER (71)
484 #define SWM_ARG_ID_KILLWINDOW (80)
485 #define SWM_ARG_ID_DELETEWINDOW (81)
486 #define SWM_ARG_ID_WIDTHGROW (90)
487 #define SWM_ARG_ID_WIDTHSHRINK (91)
488 #define SWM_ARG_ID_HEIGHTGROW (92)
489 #define SWM_ARG_ID_HEIGHTSHRINK (93)
490 #define SWM_ARG_ID_MOVEUP (100)
491 #define SWM_ARG_ID_MOVEDOWN (101)
492 #define SWM_ARG_ID_MOVELEFT (102)
493 #define SWM_ARG_ID_MOVERIGHT (103)
497 void focus(struct swm_region
*, union arg
*);
498 void focus_magic(struct ws_win
*);
502 TAILQ_ENTRY(quirk
) entry
;
506 #define SWM_Q_FLOAT (1<<0) /* float this window */
507 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
508 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
509 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
510 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
511 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
513 TAILQ_HEAD(quirk_list
, quirk
);
514 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
517 * Supported EWMH hints should be added to
518 * both the enum and the ewmh array
520 enum { _NET_ACTIVE_WINDOW
, _NET_MOVERESIZE_WINDOW
, _NET_CLOSE_WINDOW
,
521 _NET_WM_WINDOW_TYPE
, _NET_WM_WINDOW_TYPE_DOCK
,
522 _NET_WM_WINDOW_TYPE_TOOLBAR
, _NET_WM_WINDOW_TYPE_UTILITY
,
523 _NET_WM_WINDOW_TYPE_SPLASH
, _NET_WM_WINDOW_TYPE_DIALOG
,
524 _NET_WM_WINDOW_TYPE_NORMAL
, _NET_WM_STATE
,
525 _NET_WM_STATE_MAXIMIZED_HORZ
, _NET_WM_STATE_MAXIMIZED_VERT
,
526 _NET_WM_STATE_SKIP_TASKBAR
, _NET_WM_STATE_SKIP_PAGER
,
527 _NET_WM_STATE_HIDDEN
, _NET_WM_STATE_ABOVE
, _SWM_WM_STATE_MANUAL
,
528 _NET_WM_STATE_FULLSCREEN
, _NET_WM_ALLOWED_ACTIONS
, _NET_WM_ACTION_MOVE
,
529 _NET_WM_ACTION_RESIZE
, _NET_WM_ACTION_FULLSCREEN
, _NET_WM_ACTION_CLOSE
,
535 } ewmh
[SWM_EWMH_HINT_MAX
] = {
536 /* must be in same order as in the enum */
537 {"_NET_ACTIVE_WINDOW", None
},
538 {"_NET_MOVERESIZE_WINDOW", None
},
539 {"_NET_CLOSE_WINDOW", None
},
540 {"_NET_WM_WINDOW_TYPE", None
},
541 {"_NET_WM_WINDOW_TYPE_DOCK", None
},
542 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None
},
543 {"_NET_WM_WINDOW_TYPE_UTILITY", None
},
544 {"_NET_WM_WINDOW_TYPE_SPLASH", None
},
545 {"_NET_WM_WINDOW_TYPE_DIALOG", None
},
546 {"_NET_WM_WINDOW_TYPE_NORMAL", None
},
547 {"_NET_WM_STATE", None
},
548 {"_NET_WM_STATE_MAXIMIZED_HORZ", None
},
549 {"_NET_WM_STATE_MAXIMIZED_VERT", None
},
550 {"_NET_WM_STATE_SKIP_TASKBAR", None
},
551 {"_NET_WM_STATE_SKIP_PAGER", None
},
552 {"_NET_WM_STATE_HIDDEN", None
},
553 {"_NET_WM_STATE_ABOVE", None
},
554 {"_SWM_WM_STATE_MANUAL", None
},
555 {"_NET_WM_STATE_FULLSCREEN", None
},
556 {"_NET_WM_ALLOWED_ACTIONS", None
},
557 {"_NET_WM_ACTION_MOVE", None
},
558 {"_NET_WM_ACTION_RESIZE", None
},
559 {"_NET_WM_ACTION_FULLSCREEN", None
},
560 {"_NET_WM_ACTION_CLOSE", None
},
563 void store_float_geom(struct ws_win
*, struct swm_region
*);
564 int floating_toggle_win(struct ws_win
*);
565 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
566 unsigned char *get_win_name(Display
*, Window
, Atom
, Atom
, unsigned long *);
569 get_property(Window id
, Atom atom
, long count
, Atom type
,
570 unsigned long *n
, unsigned char **data
)
573 unsigned long tmp
, extra
;
574 unsigned long *nitems
;
577 nitems
= n
!= NULL
? n
: &tmp
;
578 status
= XGetWindowProperty(display
, id
, atom
, 0L, count
, False
, type
,
579 &real
, &format
, nitems
, &extra
, data
);
581 if (status
!= Success
)
590 update_iconic(struct ws_win
*win
, int newv
)
597 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
601 XChangeProperty(display
, win
->id
, iprop
, XA_INTEGER
, 32,
602 PropModeReplace
, (unsigned char *)&v
, 1);
604 XDeleteProperty(display
, win
->id
, iprop
);
608 get_iconic(struct ws_win
*win
)
613 unsigned long nitems
, extra
;
614 unsigned char *prop
= NULL
;
616 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
619 status
= XGetWindowProperty(display
, win
->id
, iprop
, 0L, 1L,
620 False
, XA_INTEGER
, &rettype
, &retfmt
, &nitems
, &extra
, &prop
);
621 if (status
!= Success
)
623 if (rettype
!= XA_INTEGER
|| retfmt
!= 32)
627 v
= *((int32_t *)prop
);
641 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
643 for (i
= 0; i
< LENGTH(ewmh
); i
++)
644 ewmh
[i
].atom
= XInternAtom(display
, ewmh
[i
].name
, False
);
646 for (i
= 0; i
< ScreenCount(display
); i
++) {
647 /* Support check window will be created by workaround(). */
649 /* Report supported atoms */
650 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
651 for (j
= 0; j
< LENGTH(ewmh
); j
++)
652 XChangeProperty(display
, screens
[i
].root
,
653 sup_list
, XA_ATOM
, 32,
654 PropModeAppend
, (unsigned char *)&ewmh
[j
].atom
,1);
662 unsigned char *data
= NULL
;
664 Atom sup_check
, sup_list
;
667 sup_check
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
668 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
670 for (i
= 0; i
< ScreenCount(display
); i
++) {
671 /* Get the support check window and destroy it */
672 success
= get_property(screens
[i
].root
, sup_check
, 1, XA_WINDOW
,
677 XDestroyWindow(display
, id
);
678 XDeleteProperty(display
, screens
[i
].root
, sup_check
);
679 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
687 ewmh_autoquirk(struct ws_win
*win
)
690 unsigned long *data
= NULL
, n
;
693 success
= get_property(win
->id
, ewmh
[_NET_WM_WINDOW_TYPE
].atom
, (~0L),
694 XA_ATOM
, &n
, (void *)&data
);
701 for (i
= 0; i
< n
; i
++) {
703 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
705 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
706 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
707 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
709 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
712 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
713 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
715 win
->quirks
= SWM_Q_FLOAT
;
723 #define SWM_EWMH_ACTION_COUNT_MAX (6)
724 #define EWMH_F_FULLSCREEN (1<<0)
725 #define EWMH_F_ABOVE (1<<1)
726 #define EWMH_F_HIDDEN (1<<2)
727 #define EWMH_F_SKIP_PAGER (1<<3)
728 #define EWMH_F_SKIP_TASKBAR (1<<4)
729 #define SWM_F_MANUAL (1<<5)
732 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
734 struct swm_geometry rg
;
742 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%lx, "
743 "fullscreen %s\n", win
->id
, YESNO(fs
));
748 store_float_geom(win
, win
->ws
->r
);
752 if (win
->g_floatvalid
) {
753 /* refloat at last floating relative position */
754 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ rg
.x
;
755 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ rg
.y
;
756 WIDTH(win
) = win
->g_float
.w
;
757 HEIGHT(win
) = win
->g_float
.h
;
765 ewmh_update_actions(struct ws_win
*win
)
767 Atom actions
[SWM_EWMH_ACTION_COUNT_MAX
];
773 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
776 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
777 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
780 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
,
781 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)actions
, n
);
784 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
785 #define _NET_WM_STATE_ADD 1 /* add/set property */
786 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
789 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
791 unsigned int mask
= 0;
792 unsigned int changed
= 0;
793 unsigned int orig_flags
;
798 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
799 mask
= EWMH_F_FULLSCREEN
;
800 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
802 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
804 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
805 mask
= EWMH_F_SKIP_PAGER
;
806 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
807 mask
= EWMH_F_SKIP_TASKBAR
;
810 orig_flags
= win
->ewmh_flags
;
813 case _NET_WM_STATE_REMOVE
:
814 win
->ewmh_flags
&= ~mask
;
816 case _NET_WM_STATE_ADD
:
817 win
->ewmh_flags
|= mask
;
819 case _NET_WM_STATE_TOGGLE
:
820 win
->ewmh_flags
^= mask
;
824 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
826 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
828 if (!floating_toggle_win(win
))
829 win
->ewmh_flags
= orig_flags
; /* revert */
830 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
832 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
833 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
835 if (!ewmh_set_win_fullscreen(win
,
836 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
837 win
->ewmh_flags
= orig_flags
; /* revert */
839 XDeleteProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
841 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
842 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
843 XA_ATOM
, 32, PropModeAppend
,
844 (unsigned char *)&ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
, 1);
845 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
846 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
847 XA_ATOM
, 32, PropModeAppend
,
848 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
, 1);
849 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
850 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
851 XA_ATOM
, 32, PropModeAppend
,
852 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
, 1);
853 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
854 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
855 XA_ATOM
, 32, PropModeAppend
,
856 (unsigned char *)&ewmh
[_NET_WM_STATE_ABOVE
].atom
, 1);
857 if (win
->ewmh_flags
& SWM_F_MANUAL
)
858 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
859 XA_ATOM
, 32, PropModeAppend
,
860 (unsigned char *)&ewmh
[_SWM_WM_STATE_MANUAL
].atom
, 1);
864 ewmh_get_win_state(struct ws_win
*win
)
875 win
->ewmh_flags
|= EWMH_F_ABOVE
;
877 win
->ewmh_flags
|= SWM_F_MANUAL
;
879 success
= get_property(win
->id
, ewmh
[_NET_WM_STATE
].atom
,
880 (~0L), XA_ATOM
, &n
, (void *)&states
);
885 for (i
= 0; i
< n
; i
++)
886 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
894 geteventname(XEvent
*e
)
906 name
= "ButtonPress";
909 name
= "ButtonRelease";
912 name
= "MotionNotify";
915 name
= "EnterNotify";
918 name
= "LeaveNotify";
927 name
= "KeymapNotify";
933 name
= "GraphicsExpose";
938 case VisibilityNotify
:
939 name
= "VisibilityNotify";
942 name
= "CreateNotify";
945 name
= "DestroyNotify";
948 name
= "UnmapNotify";
957 name
= "ReparentNotify";
959 case ConfigureNotify
:
960 name
= "ConfigureNotify";
962 case ConfigureRequest
:
963 name
= "ConfigureRequest";
966 name
= "GravityNotify";
969 name
= "ResizeRequest";
971 case CirculateNotify
:
972 name
= "CirculateNotify";
974 case CirculateRequest
:
975 name
= "CirculateRequest";
978 name
= "PropertyNotify";
981 name
= "SelectionClear";
983 case SelectionRequest
:
984 name
= "SelectionRequest";
986 case SelectionNotify
:
987 name
= "SelectionNotify";
990 name
= "ColormapNotify";
993 name
= "ClientMessage";
996 name
= "MappingNotify";
1006 xrandr_geteventname(XEvent
*e
)
1010 switch(e
->type
- xrandr_eventbase
) {
1011 case RRScreenChangeNotify
:
1012 name
= "RRScreenChangeNotify";
1022 dumpwins(struct swm_region
*r
, union arg
*args
)
1026 XWindowAttributes wa
;
1028 if (r
->ws
== NULL
) {
1029 warnx("dumpwins: invalid workspace");
1033 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1035 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1036 state
= getstate(win
->id
);
1037 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1038 warnx("window: 0x%lx, failed XGetWindowAttributes",
1040 warnx("window: 0x%lx, map_state: %d, state: %d, "
1041 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1045 warnx("===== unmanaged window list =====");
1046 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1047 state
= getstate(win
->id
);
1048 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1049 warnx("window: 0x%lx, failed XGetWindowAttributes",
1051 warnx("window: 0x%lx, map_state: %d, state: %d, "
1052 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1056 warnx("=================================");
1060 dumpwins(struct swm_region
*r
, union arg
*args
)
1063 #endif /* SWM_DEBUG */
1065 void expose(XEvent
*);
1066 void keypress(XEvent
*);
1067 void buttonpress(XEvent
*);
1068 void configurerequest(XEvent
*);
1069 void configurenotify(XEvent
*);
1070 void destroynotify(XEvent
*);
1071 void enternotify(XEvent
*);
1072 void focusevent(XEvent
*);
1073 void mapnotify(XEvent
*);
1074 void mappingnotify(XEvent
*);
1075 void maprequest(XEvent
*);
1076 void propertynotify(XEvent
*);
1077 void unmapnotify(XEvent
*);
1078 void visibilitynotify(XEvent
*);
1079 void clientmessage(XEvent
*);
1081 void (*handler
[LASTEvent
])(XEvent
*) = {
1083 [KeyPress
] = keypress
,
1084 [ButtonPress
] = buttonpress
,
1085 [ConfigureRequest
] = configurerequest
,
1086 [ConfigureNotify
] = configurenotify
,
1087 [DestroyNotify
] = destroynotify
,
1088 [EnterNotify
] = enternotify
,
1089 [FocusIn
] = focusevent
,
1090 [FocusOut
] = focusevent
,
1091 [MapNotify
] = mapnotify
,
1092 [MappingNotify
] = mappingnotify
,
1093 [MapRequest
] = maprequest
,
1094 [PropertyNotify
] = propertynotify
,
1095 [UnmapNotify
] = unmapnotify
,
1096 [VisibilityNotify
] = visibilitynotify
,
1097 [ClientMessage
] = clientmessage
,
1103 int saved_errno
, status
;
1106 saved_errno
= errno
;
1110 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1115 if (errno
!= ECHILD
)
1116 warn("sighdlr: waitpid");
1117 #endif /* SWM_DEBUG */
1120 if (pid
== searchpid
)
1124 if (WIFEXITED(status
)) {
1125 if (WEXITSTATUS(status
) != 0)
1126 warnx("sighdlr: child exit status: %d",
1127 WEXITSTATUS(status
));
1129 warnx("sighdlr: child is terminated "
1131 #endif /* SWM_DEBUG */
1145 errno
= saved_errno
;
1151 struct pid_e
*p
= NULL
;
1153 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1158 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1167 name_to_color(char *colorname
)
1171 XColor screen_def
, exact_def
;
1172 unsigned long result
= 0;
1173 char cname
[32] = "#";
1175 cmap
= DefaultColormap(display
, screens
[0].idx
);
1176 status
= XAllocNamedColor(display
, cmap
, colorname
,
1177 &screen_def
, &exact_def
);
1179 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1180 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
1184 result
= screen_def
.pixel
;
1186 warnx("color '%s' not found", colorname
);
1192 setscreencolor(char *val
, int i
, int c
)
1194 if (i
> 0 && i
<= ScreenCount(display
)) {
1195 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1196 free(screens
[i
- 1].c
[c
].name
);
1197 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1199 } else if (i
== -1) {
1200 for (i
= 0; i
< ScreenCount(display
); i
++) {
1201 screens
[i
].c
[c
].color
= name_to_color(val
);
1202 free(screens
[i
].c
[c
].name
);
1203 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1207 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1208 i
, ScreenCount(display
));
1212 fancy_stacker(struct workspace
*ws
)
1214 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1215 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1216 snprintf(ws
->stacker
, sizeof ws
->stacker
, "[%d|%d]",
1217 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1218 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1219 snprintf(ws
->stacker
, sizeof ws
->stacker
, "[%d-%d]",
1220 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1224 plain_stacker(struct workspace
*ws
)
1226 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1227 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1228 strlcpy(ws
->stacker
, "[|]", sizeof ws
->stacker
);
1229 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1230 strlcpy(ws
->stacker
, "[-]", sizeof ws
->stacker
);
1234 custom_region(char *val
)
1236 unsigned int sidx
, x
, y
, w
, h
;
1238 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1239 errx(1, "invalid custom region, "
1240 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1241 if (sidx
< 1 || sidx
> ScreenCount(display
))
1242 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1243 sidx
, ScreenCount(display
));
1247 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1249 if (x
> DisplayWidth(display
, sidx
) ||
1250 y
> DisplayHeight(display
, sidx
) ||
1251 w
+ x
> DisplayWidth(display
, sidx
) ||
1252 h
+ y
> DisplayHeight(display
, sidx
)) {
1253 warnx("ignoring region %ux%u+%u+%u - not within screen "
1254 "boundaries (%ux%u)", w
, h
, x
, y
,
1255 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
1259 new_region(&screens
[sidx
], x
, y
, w
, h
);
1263 socket_setnonblock(int fd
)
1267 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1268 err(1, "fcntl F_GETFL");
1269 flags
|= O_NONBLOCK
;
1270 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1271 err(1, "fcntl F_SETFL");
1275 bar_print(struct swm_region
*r
, char *s
)
1279 XRectangle ibox
, lbox
;
1281 XClearWindow(display
, r
->bar_window
);
1284 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1286 switch (bar_justify
) {
1287 case SWM_BAR_JUSTIFY_LEFT
:
1290 case SWM_BAR_JUSTIFY_CENTER
:
1291 x
= (WIDTH(r
) - lbox
.width
) / 2;
1293 case SWM_BAR_JUSTIFY_RIGHT
:
1294 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1298 if (x
< SWM_BAR_OFFSET
)
1301 DRAWSTRING(display
, r
->bar_window
, bar_fs
, r
->s
->bar_gc
,
1302 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1307 bar_extra_stop(void)
1311 bzero(bar_pipe
, sizeof bar_pipe
);
1314 kill(bar_pid
, SIGTERM
);
1317 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1322 bar_class_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1324 int do_class
, do_name
;
1326 XClassHint
*xch
= NULL
;
1328 if ((title_name_enabled
== 1 || title_class_enabled
== 1) &&
1329 cur_focus
!= NULL
) {
1330 if ((xch
= XAllocClassHint()) == NULL
)
1332 status
= XGetClassHint(display
, cur_focus
->id
, xch
);
1333 if (status
== BadWindow
|| status
== BadAlloc
)
1335 do_class
= (title_class_enabled
&& xch
->res_class
!= NULL
);
1336 do_name
= (title_name_enabled
&& xch
->res_name
!= NULL
);
1338 strlcat(s
, xch
->res_class
, sz
);
1339 if (do_class
&& do_name
)
1340 strlcat(s
, ":", sz
);
1342 strlcat(s
, xch
->res_name
, sz
);
1343 strlcat(s
, " ", sz
);
1347 XFree(xch
->res_name
);
1348 XFree(xch
->res_class
);
1354 bar_window_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1356 unsigned char *title
;
1359 if (window_name_enabled
&& cur_focus
!= NULL
) {
1360 if ((title
= get_win_name(display
, cur_focus
->id
, a_netwmname
,
1361 a_utf8_string
, &len
)) != NULL
) {
1362 DNPRINTF(SWM_D_BAR
, "bar_window_name: title: %s\n",
1365 if (cur_focus
->floating
)
1366 strlcat(s
, "(f) ", sz
);
1367 strlcat(s
, (char *)title
, sz
);
1368 strlcat(s
, " ", sz
);
1374 int urgent
[SWM_WS_MAX
];
1376 bar_urgent(char *s
, ssize_t sz
)
1378 XWMHints
*wmh
= NULL
;
1383 if (urgent_enabled
== 0)
1386 for (i
= 0; i
< SWM_WS_MAX
; i
++)
1389 for (i
= 0; i
< ScreenCount(display
); i
++)
1390 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1391 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1392 wmh
= XGetWMHints(display
, win
->id
);
1396 if (wmh
->flags
& XUrgencyHint
)
1401 strlcat(s
, "* ", sz
);
1402 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
1404 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1406 snprintf(b
, sizeof b
, "- ");
1409 strlcat(s
, "* ", sz
);
1417 struct swm_region
*r
;
1420 char ws
[SWM_BAR_MAX
];
1421 char s
[SWM_BAR_MAX
];
1422 unsigned char cn
[SWM_BAR_MAX
];
1423 char loc
[SWM_BAR_MAX
];
1424 char *b
, *stack
= "";
1426 if (bar_enabled
== 0)
1428 if (bar_extra
&& bar_extra_running
) {
1429 /* ignore short reads; it'll correct itself */
1430 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1431 if (b
&& b
[len
- 1] == '\n') {
1433 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1435 if (b
== NULL
&& errno
!= EAGAIN
) {
1436 warn("bar_update: bar_extra failed");
1440 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1442 if (clock_enabled
== 0)
1443 strlcpy(s
, "", sizeof s
);
1446 localtime_r(&tmt
, &tm
);
1447 len
= strftime(s
, sizeof s
, clock_format
, &tm
);
1449 strlcat(s
, " ", sizeof s
);
1452 for (i
= 0; i
< ScreenCount(display
); i
++) {
1454 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1455 strlcpy((char *)cn
, "", sizeof cn
);
1456 strlcpy(ws
, "", sizeof ws
);
1458 bar_urgent((char *)cn
, sizeof cn
);
1459 bar_class_name((char *)cn
, sizeof cn
,
1461 bar_window_name((char *)cn
, sizeof cn
,
1464 snprintf(ws
, sizeof ws
, "<%s>",
1468 stack
= r
->ws
->stacker
;
1470 snprintf(loc
, sizeof loc
, "%d:%d %s %s %s%s %s "
1471 "%s", x
++, r
->ws
->idx
+ 1, stack
, ws
, s
, cn
,
1472 bar_ext
, bar_vertext
);
1486 bar_toggle(struct swm_region
*r
, union arg
*args
)
1488 struct swm_region
*tmpr
;
1489 int i
, sc
= ScreenCount(display
);
1491 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1494 for (i
= 0; i
< sc
; i
++)
1495 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1496 XUnmapWindow(display
, tmpr
->bar_window
);
1498 for (i
= 0; i
< sc
; i
++)
1499 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1500 XMapRaised(display
, tmpr
->bar_window
);
1502 bar_enabled
= !bar_enabled
;
1505 /* must be after stack */
1512 XSetWindowAttributes wa
;
1513 struct swm_region
*r
;
1516 /* do this here because the conf file is in memory */
1517 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1518 /* launch external status app */
1519 bar_extra_running
= 1;
1520 if (pipe(bar_pipe
) == -1)
1521 err(1, "pipe error");
1522 socket_setnonblock(bar_pipe
[0]);
1523 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1524 if (dup2(bar_pipe
[0], 0) == -1)
1526 if (dup2(bar_pipe
[1], 1) == -1)
1528 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1529 err(1, "could not disable SIGPIPE");
1530 switch (bar_pid
= fork()) {
1532 err(1, "cannot fork");
1536 execvp(bar_argv
[0], bar_argv
);
1537 err(1, "%s external app failed", bar_argv
[0]);
1539 default: /* parent */
1545 bzero(&wa
, sizeof wa
);
1546 for (i
= 0; i
< ScreenCount(display
); i
++)
1547 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1549 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1550 wa
.background_pixel
=
1551 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1552 XChangeWindowAttributes(display
, r
->bar_window
,
1553 CWBackPixel
| CWBorderPixel
, &wa
);
1559 bar_setup(struct swm_region
*r
)
1561 char *default_string
;
1562 char **missing_charsets
;
1563 int num_missing_charsets
= 0;
1567 XFreeFontSet(display
, bar_fs
);
1572 DNPRINTF(SWM_D_BAR
, "bar_setup: loading bar_fonts: %s\n", bar_fonts
);
1574 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
1575 &num_missing_charsets
, &default_string
);
1577 if (num_missing_charsets
> 0) {
1578 warnx("Unable to load charset(s):");
1580 for (i
= 0; i
< num_missing_charsets
; ++i
)
1581 warnx("%s", missing_charsets
[i
]);
1583 XFreeStringList(missing_charsets
);
1585 if (strcmp(default_string
, ""))
1586 warnx("Glyphs from those sets will be replaced "
1587 "by '%s'.", default_string
);
1589 warnx("Glyphs from those sets won't be drawn.");
1593 errx(1, "Error creating font set structure.");
1595 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
1597 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
1598 2 * bar_border_width
;
1604 y
= bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1606 r
->bar_window
= XCreateSimpleWindow(display
,
1607 r
->s
->root
, x
, y
, WIDTH(r
) - 2 * bar_border_width
,
1608 bar_height
- 2 * bar_border_width
,
1609 bar_border_width
, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
1610 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1611 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
1613 XMapRaised(display
, r
->bar_window
);
1614 DNPRINTF(SWM_D_BAR
, "bar_setup: bar_window: 0x%lx\n", r
->bar_window
);
1616 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1617 err(1, "could not install bar_signal");
1622 drain_enter_notify(void)
1627 while (XCheckMaskEvent(display
, EnterWindowMask
, &cne
))
1630 DNPRINTF(SWM_D_MISC
, "drain_enter_notify: drained: %d\n", i
);
1634 set_win_state(struct ws_win
*win
, long state
)
1636 long data
[] = {state
, None
};
1638 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%lx\n", win
->id
);
1643 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1644 (unsigned char *)data
, 2);
1651 unsigned char *p
= NULL
;
1654 if (!get_property(w
, astate
, 2L, astate
, &n
, &p
))
1657 result
= *((long *)p
);
1663 version(struct swm_region
*r
, union arg
*args
)
1665 bar_version
= !bar_version
;
1667 snprintf(bar_vertext
, sizeof bar_vertext
,
1668 "Version: %s Build: %s", SCROTWM_VERSION
, buildstr
);
1670 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
1675 client_msg(struct ws_win
*win
, Atom a
)
1677 XClientMessageEvent cm
;
1682 bzero(&cm
, sizeof cm
);
1683 cm
.type
= ClientMessage
;
1684 cm
.window
= win
->id
;
1685 cm
.message_type
= aprot
;
1688 cm
.data
.l
[1] = CurrentTime
;
1689 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
1692 /* synthetic response to a ConfigureRequest when not making a change */
1694 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
1701 /* send notification of unchanged state. */
1702 ce
.type
= ConfigureNotify
;
1705 ce
.width
= WIDTH(win
);
1706 ce
.height
= HEIGHT(win
);
1707 ce
.override_redirect
= False
;
1711 ce
.display
= display
;
1713 ce
.window
= win
->id
;
1714 ce
.border_width
= border_width
;
1718 ce
.display
= ev
->display
;
1719 ce
.event
= ev
->window
;
1720 ce
.window
= ev
->window
;
1722 /* make response appear more WM_SIZE_HINTS-compliant */
1724 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%lx,"
1725 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
1726 "%d x %d\n", win
->id
, win
->sh_mask
, SH_MIN_W(win
),
1727 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
1728 SH_INC_W(win
), SH_INC_H(win
));
1732 /* the hint may be set... to 0! */
1733 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
1734 ce
.width
= SH_MIN_W(win
);
1735 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
1736 ce
.height
= SH_MIN_H(win
);
1741 /* may also be advertized as 0 */
1742 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
1743 ce
.width
= SH_MAX_W(win
);
1744 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
1745 ce
.height
= SH_MAX_H(win
);
1748 /* resize increment. */
1750 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
1751 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
1753 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
1754 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
1758 /* adjust x and y for requested border_width. */
1759 ce
.x
+= border_width
- ev
->border_width
;
1760 ce
.y
+= border_width
- ev
->border_width
;
1761 ce
.border_width
= ev
->border_width
;
1762 ce
.above
= ev
->above
;
1765 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
1766 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
1767 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
1769 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
1773 count_win(struct workspace
*ws
, int count_transient
)
1778 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1779 if (count_transient
== 0 && win
->floating
)
1781 if (count_transient
== 0 && win
->transient
)
1787 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
1793 quit(struct swm_region
*r
, union arg
*args
)
1795 DNPRINTF(SWM_D_MISC
, "quit\n");
1800 unmap_window(struct ws_win
*win
)
1805 /* don't unmap again */
1806 if (getstate(win
->id
) == IconicState
)
1809 set_win_state(win
, IconicState
);
1811 XUnmapWindow(display
, win
->id
);
1812 XSetWindowBorder(display
, win
->id
,
1813 win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1822 for (i
= 0; i
< ScreenCount(display
); i
++)
1823 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1824 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1829 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
1836 event
.display
= display
; /* Ignored, but what the hell */
1837 event
.window
= win
->id
;
1838 event
.root
= win
->s
->root
;
1839 event
.subwindow
= None
;
1840 event
.time
= CurrentTime
;
1845 event
.same_screen
= True
;
1846 event
.keycode
= XKeysymToKeycode(display
, keysym
);
1847 event
.state
= modifiers
;
1849 event
.type
= KeyPress
;
1850 XSendEvent(event
.display
, event
.window
, True
,
1851 KeyPressMask
, (XEvent
*)&event
);
1853 event
.type
= KeyRelease
;
1854 XSendEvent(event
.display
, event
.window
, True
,
1855 KeyPressMask
, (XEvent
*)&event
);
1860 restart(struct swm_region
*r
, union arg
*args
)
1862 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
1864 /* disable alarm because the following code may not be interrupted */
1866 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
1867 err(1, "can't disable alarm");
1872 XCloseDisplay(display
);
1873 execvp(start_argv
[0], start_argv
);
1874 warn("execvp failed");
1879 root_to_region(Window root
)
1881 struct swm_region
*r
= NULL
;
1883 int i
, x
, y
, wx
, wy
;
1886 for (i
= 0; i
< ScreenCount(display
); i
++)
1887 if (screens
[i
].root
== root
)
1890 if (XQueryPointer(display
, screens
[i
].root
,
1891 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
1892 /* choose a region based on pointer location */
1893 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1894 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
1895 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
1900 r
= TAILQ_FIRST(&screens
[i
].rl
);
1906 find_unmanaged_window(Window id
)
1911 for (i
= 0; i
< ScreenCount(display
); i
++)
1912 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1913 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
1921 find_window(Window id
)
1924 Window wrr
, wpr
, *wcr
= NULL
;
1928 for (i
= 0; i
< ScreenCount(display
); i
++)
1929 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1930 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1934 /* if we were looking for the parent return that window instead */
1935 if (XQueryTree(display
, id
, &wrr
, &wpr
, &wcr
, &nc
) == 0)
1940 /* ignore not found and root */
1941 if (wpr
== 0 || wrr
== wpr
)
1944 /* look for parent */
1945 for (i
= 0; i
< ScreenCount(display
); i
++)
1946 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1947 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1955 spawn(int ws_idx
, union arg
*args
, int close_fd
)
1960 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
1963 close(ConnectionNumber(display
));
1965 setenv("LD_PRELOAD", SWM_LIB
, 1);
1967 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
1968 warn("spawn: asprintf SWM_WS");
1971 setenv("_SWM_WS", ret
, 1);
1975 if (asprintf(&ret
, "%d", getpid()) == -1) {
1976 warn("spawn: asprintf _SWM_PID");
1979 setenv("_SWM_PID", ret
, 1);
1983 if (setsid() == -1) {
1984 warn("spawn: setsid");
1990 * close stdin and stdout to prevent interaction between apps
1991 * and the baraction script
1992 * leave stderr open to record errors
1994 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
1995 warn("spawn: open");
1998 dup2(fd
, STDIN_FILENO
);
1999 dup2(fd
, STDOUT_FILENO
);
2004 execvp(args
->argv
[0], args
->argv
);
2006 warn("spawn: execvp");
2011 spawnterm(struct swm_region
*r
, union arg
*args
)
2013 DNPRINTF(SWM_D_MISC
, "spawnterm\n");
2017 setenv("_SWM_XTERM_FONTADJ", "", 1);
2018 spawn(r
->ws
->idx
, args
, 1);
2023 kill_refs(struct ws_win
*win
)
2026 struct swm_region
*r
;
2027 struct workspace
*ws
;
2032 for (i
= 0; i
< ScreenCount(display
); i
++)
2033 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2034 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
2036 if (win
== ws
->focus
)
2038 if (win
== ws
->focus_prev
)
2039 ws
->focus_prev
= NULL
;
2044 validate_win(struct ws_win
*testwin
)
2047 struct workspace
*ws
;
2048 struct swm_region
*r
;
2051 if (testwin
== NULL
)
2054 for (i
= 0; i
< ScreenCount(display
); i
++)
2055 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2056 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
2058 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2066 validate_ws(struct workspace
*testws
)
2068 struct swm_region
*r
;
2069 struct workspace
*ws
;
2072 /* validate all ws */
2073 for (i
= 0; i
< ScreenCount(display
); i
++)
2074 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2075 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
2084 unfocus_win(struct ws_win
*win
)
2089 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%lx\n", WINID(win
));
2093 if (win
->ws
== NULL
)
2096 if (validate_ws(win
->ws
))
2097 return; /* XXX this gets hit with thunderbird, needs fixing */
2099 if (win
->ws
->r
== NULL
)
2102 if (validate_win(win
)) {
2107 if (win
->ws
->focus
== win
) {
2108 win
->ws
->focus
= NULL
;
2109 win
->ws
->focus_prev
= win
;
2112 if (validate_win(win
->ws
->focus
)) {
2113 kill_refs(win
->ws
->focus
);
2114 win
->ws
->focus
= NULL
;
2116 if (validate_win(win
->ws
->focus_prev
)) {
2117 kill_refs(win
->ws
->focus_prev
);
2118 win
->ws
->focus_prev
= NULL
;
2121 /* drain all previous unfocus events */
2122 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
2125 grabbuttons(win
, 0);
2126 XSetWindowBorder(display
, win
->id
,
2127 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2129 XChangeProperty(display
, win
->s
->root
,
2130 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2131 PropModeReplace
, (unsigned char *)&none
,1);
2140 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2142 for (i
= 0; i
< ScreenCount(display
); i
++)
2143 for (j
= 0; j
< SWM_WS_MAX
; j
++)
2144 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2149 focus_win(struct ws_win
*win
)
2154 struct ws_win
*cfw
= NULL
;
2157 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%lx\n", WINID(win
));
2161 if (win
->ws
== NULL
)
2164 if (validate_ws(win
->ws
))
2165 return; /* XXX this gets hit with thunderbird, needs fixing */
2167 if (validate_win(win
)) {
2172 if (validate_win(win
)) {
2177 XGetInputFocus(display
, &cur_focus
, &rr
);
2178 if ((cfw
= find_window(cur_focus
)) != NULL
)
2181 /* use larger hammer since the window was killed somehow */
2182 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2183 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2184 XSetWindowBorder(display
, cfw
->id
,
2185 cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2188 win
->ws
->focus
= win
;
2190 if (win
->ws
->r
!= NULL
) {
2191 /* drain all previous focus events */
2192 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
2196 XSetInputFocus(display
, win
->id
,
2197 RevertToParent
, CurrentTime
);
2198 grabbuttons(win
, 1);
2199 XSetWindowBorder(display
, win
->id
,
2200 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2201 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2202 win
->ws
->always_raise
)
2203 XMapRaised(display
, win
->id
);
2205 XChangeProperty(display
, win
->s
->root
,
2206 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2207 PropModeReplace
, (unsigned char *)&win
->id
,1);
2210 if (window_name_enabled
)
2215 switchws(struct swm_region
*r
, union arg
*args
)
2217 int wsid
= args
->id
, unmap_old
= 0;
2218 struct swm_region
*this_r
, *other_r
;
2220 struct workspace
*new_ws
, *old_ws
;
2227 old_ws
= this_r
->ws
;
2228 new_ws
= &this_r
->s
->ws
[wsid
];
2230 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2231 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2233 if (new_ws
== NULL
|| old_ws
== NULL
)
2235 if (new_ws
== old_ws
)
2238 other_r
= new_ws
->r
;
2239 if (other_r
== NULL
) {
2240 /* the other workspace is hidden, hide this one */
2244 /* the other ws is visible in another region, exchange them */
2245 other_r
->ws_prior
= new_ws
;
2246 other_r
->ws
= old_ws
;
2247 old_ws
->r
= other_r
;
2249 this_r
->ws_prior
= old_ws
;
2250 this_r
->ws
= new_ws
;
2253 /* this is needed so that we can click on a window after a restart */
2257 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2258 focus(new_ws
->r
, &a
);
2262 /* unmap old windows */
2264 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2267 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2268 drain_enter_notify();
2272 cyclews(struct swm_region
*r
, union arg
*args
)
2275 struct swm_screen
*s
= r
->s
;
2278 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2279 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2284 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2287 case SWM_ARG_ID_CYCLEWS_UP
:
2288 if (a
.id
< SWM_WS_MAX
- 1)
2293 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2296 case SWM_ARG_ID_CYCLEWS_DOWN
:
2300 a
.id
= SWM_WS_MAX
- 1;
2307 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2309 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2313 } while (a
.id
!= r
->ws
->idx
);
2317 priorws(struct swm_region
*r
, union arg
*args
)
2321 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2322 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2324 if (r
->ws_prior
== NULL
)
2327 a
.id
= r
->ws_prior
->idx
;
2332 cyclescr(struct swm_region
*r
, union arg
*args
)
2334 struct swm_region
*rr
= NULL
;
2338 /* do nothing if we don't have more than one screen */
2339 if (!(ScreenCount(display
) > 1 || outputs
> 1))
2344 case SWM_ARG_ID_CYCLESC_UP
:
2345 rr
= TAILQ_NEXT(r
, entry
);
2347 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2349 case SWM_ARG_ID_CYCLESC_DOWN
:
2350 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2352 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2360 /* move mouse to region */
2362 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2363 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2365 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2368 if (rr
->ws
->focus
) {
2369 /* move to focus window */
2370 x
= X(rr
->ws
->focus
) + 1;
2371 y
= Y(rr
->ws
->focus
) + 1;
2372 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2377 sort_windows(struct ws_win_list
*wl
)
2379 struct ws_win
*win
, *parent
, *nxt
;
2384 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2385 nxt
= TAILQ_NEXT(win
, entry
);
2386 if (win
->transient
) {
2387 parent
= find_window(win
->transient
);
2388 if (parent
== NULL
) {
2389 warnx("not possible bug");
2392 TAILQ_REMOVE(wl
, win
, entry
);
2393 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2400 swapwin(struct swm_region
*r
, union arg
*args
)
2402 struct ws_win
*target
, *source
;
2403 struct ws_win
*cur_focus
;
2404 struct ws_win_list
*wl
;
2407 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2408 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2410 cur_focus
= r
->ws
->focus
;
2411 if (cur_focus
== NULL
)
2415 wl
= &source
->ws
->winlist
;
2418 case SWM_ARG_ID_SWAPPREV
:
2419 if (source
->transient
)
2420 source
= find_window(source
->transient
);
2421 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2422 if (target
&& target
->transient
)
2423 target
= find_window(target
->transient
);
2424 TAILQ_REMOVE(wl
, source
, entry
);
2426 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2428 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2430 case SWM_ARG_ID_SWAPNEXT
:
2431 target
= TAILQ_NEXT(source
, entry
);
2432 /* move the parent and let the sort handle the move */
2433 if (source
->transient
)
2434 source
= find_window(source
->transient
);
2435 TAILQ_REMOVE(wl
, source
, entry
);
2437 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2439 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2441 case SWM_ARG_ID_SWAPMAIN
:
2442 target
= TAILQ_FIRST(wl
);
2443 if (target
== source
) {
2444 if (source
->ws
->focus_prev
!= NULL
&&
2445 source
->ws
->focus_prev
!= target
)
2447 source
= source
->ws
->focus_prev
;
2451 if (target
== NULL
|| source
== NULL
)
2453 source
->ws
->focus_prev
= target
;
2454 TAILQ_REMOVE(wl
, target
, entry
);
2455 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2456 TAILQ_REMOVE(wl
, source
, entry
);
2457 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2459 case SWM_ARG_ID_MOVELAST
:
2460 TAILQ_REMOVE(wl
, source
, entry
);
2461 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2464 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2474 focus_prev(struct ws_win
*win
)
2476 struct ws_win
*winfocus
= NULL
, *winlostfocus
= NULL
;
2477 struct ws_win
*cur_focus
= NULL
;
2478 struct ws_win_list
*wl
= NULL
;
2479 struct workspace
*ws
= NULL
;
2481 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%lx\n", WINID(win
));
2483 if (!(win
&& win
->ws
))
2488 cur_focus
= ws
->focus
;
2489 winlostfocus
= cur_focus
;
2491 /* pickle, just focus on whatever */
2492 if (cur_focus
== NULL
) {
2493 /* use prev_focus if valid */
2494 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2495 find_window(WINID(ws
->focus_prev
)))
2496 winfocus
= ws
->focus_prev
;
2497 if (winfocus
== NULL
)
2498 winfocus
= TAILQ_FIRST(wl
);
2502 /* if transient focus on parent */
2503 if (cur_focus
->transient
) {
2504 winfocus
= find_window(cur_focus
->transient
);
2508 /* if in max_stack try harder */
2509 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2510 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2511 if (cur_focus
!= ws
->focus_prev
)
2512 winfocus
= ws
->focus_prev
;
2513 else if (cur_focus
!= ws
->focus
)
2514 winfocus
= ws
->focus
;
2516 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2521 if (cur_focus
== win
)
2522 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2523 if (winfocus
== NULL
)
2524 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2525 if (winfocus
== NULL
|| winfocus
== win
)
2526 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2528 if (winfocus
== winlostfocus
|| winfocus
== NULL
) {
2529 /* update the bar so that title/class/name will be cleared. */
2530 if (window_name_enabled
|| title_name_enabled
||
2531 title_class_enabled
)
2536 focus_magic(winfocus
);
2540 focus(struct swm_region
*r
, union arg
*args
)
2542 struct ws_win
*winfocus
= NULL
, *winlostfocus
= NULL
, *head
;
2543 struct ws_win
*cur_focus
= NULL
;
2544 struct ws_win_list
*wl
= NULL
;
2545 struct workspace
*ws
= NULL
;
2551 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2553 /* treat FOCUS_CUR special */
2554 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2555 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2556 winfocus
= r
->ws
->focus
;
2557 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2558 winfocus
= r
->ws
->focus_prev
;
2560 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2561 if (winfocus
->iconic
== 0)
2564 focus_magic(winfocus
);
2568 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2572 if (TAILQ_EMPTY(wl
))
2574 /* make sure there is at least one uniconified window */
2576 TAILQ_FOREACH(winfocus
, wl
, entry
)
2577 if (winfocus
->iconic
== 0) {
2584 winlostfocus
= cur_focus
;
2587 case SWM_ARG_ID_FOCUSPREV
:
2588 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2590 head
= TAILQ_LAST(wl
, ws_win_list
);
2592 if (WINID(winfocus
) == cur_focus
->transient
) {
2593 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2595 head
= TAILQ_LAST(wl
, ws_win_list
);
2600 if (winfocus
&& winfocus
->iconic
) {
2601 while (winfocus
!= cur_focus
) {
2602 if (winfocus
== NULL
)
2603 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2604 if (winfocus
->iconic
== 0)
2606 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
2612 case SWM_ARG_ID_FOCUSNEXT
:
2613 head
= TAILQ_NEXT(cur_focus
, entry
);
2615 head
= TAILQ_FIRST(wl
);
2619 if (winfocus
&& winfocus
->iconic
) {
2620 while (winfocus
!= cur_focus
) {
2621 if (winfocus
== NULL
)
2622 winfocus
= TAILQ_FIRST(wl
);
2623 if (winfocus
->iconic
== 0)
2625 winfocus
= TAILQ_NEXT(winfocus
, entry
);
2630 case SWM_ARG_ID_FOCUSMAIN
:
2631 winfocus
= TAILQ_FIRST(wl
);
2632 if (winfocus
== cur_focus
)
2633 winfocus
= cur_focus
->ws
->focus_prev
;
2639 if (winfocus
== winlostfocus
|| winfocus
== NULL
) {
2640 /* update the bar so that title/class/name will be cleared. */
2641 if (window_name_enabled
|| title_name_enabled
||
2642 title_class_enabled
)
2648 focus_magic(winfocus
);
2652 cycle_layout(struct swm_region
*r
, union arg
*args
)
2654 struct workspace
*ws
= r
->ws
;
2657 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2660 if (ws
->cur_layout
->l_stack
== NULL
)
2661 ws
->cur_layout
= &layouts
[0];
2664 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2665 drain_enter_notify();
2666 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2672 stack_config(struct swm_region
*r
, union arg
*args
)
2674 struct workspace
*ws
= r
->ws
;
2676 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
2679 if (ws
->cur_layout
->l_config
!= NULL
)
2680 ws
->cur_layout
->l_config(ws
, args
->id
);
2682 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
2689 struct swm_geometry g
;
2690 struct swm_region
*r
;
2696 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
2698 for (i
= 0; i
< ScreenCount(display
); i
++) {
2702 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2703 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
2704 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
2706 /* start with screen geometry, adjust for bar */
2708 g
.w
-= 2 * border_width
;
2709 g
.h
-= 2 * border_width
;
2715 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
2716 r
->ws
->cur_layout
->l_string(r
->ws
);
2717 /* save r so we can track region changes */
2724 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2725 drain_enter_notify();
2727 DNPRINTF(SWM_D_STACK
, "stack: end\n");
2731 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
2733 /* retain window geom and region geom */
2734 win
->g_float
= win
->g
;
2735 win
->rg_float
= r
->g
;
2736 win
->g_floatvalid
= 1;
2740 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
2748 bzero(&wc
, sizeof wc
);
2749 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
2752 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2753 * geom on ws switches or return from max mode
2755 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&& win
->g_floatvalid
2756 && !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
2758 * use stored g and rg to set relative position and size
2759 * as in old region or before max stack mode
2761 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ X(r
);
2762 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ Y(r
);
2763 WIDTH(win
) = win
->g_float
.w
;
2764 HEIGHT(win
) = win
->g_float
.h
;
2765 win
->g_floatvalid
= 0;
2768 win
->floatmaxed
= 0;
2770 if ((win
->quirks
& SWM_Q_FULLSCREEN
) && (WIDTH(win
) >= WIDTH(r
)) &&
2771 (HEIGHT(win
) >= HEIGHT(r
)))
2772 wc
.border_width
= 0;
2774 wc
.border_width
= border_width
;
2775 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
2776 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
2777 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
2782 * floaters and transients are auto-centred unless moved
2785 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - wc
.border_width
;
2786 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - wc
.border_width
;
2789 /* win can be outside r if new r smaller than old r */
2790 /* Ensure top left corner inside r (move probs otherwise) */
2791 if (X(win
) < X(r
) - wc
.border_width
)
2792 X(win
) = X(r
) - wc
.border_width
;
2793 if (X(win
) > X(r
) + WIDTH(r
) - 1)
2794 X(win
) = (WIDTH(win
) > WIDTH(r
)) ? X(r
) :
2795 (X(r
) + WIDTH(r
) - WIDTH(win
) - 2 * wc
.border_width
);
2796 if (Y(win
) < Y(r
) - wc
.border_width
)
2797 Y(win
) = Y(r
) - wc
.border_width
;
2798 if (Y(win
) > Y(r
) + HEIGHT(r
) - 1)
2799 Y(win
) = (HEIGHT(win
) > HEIGHT(r
)) ? Y(r
) :
2800 (Y(r
) + HEIGHT(r
) - HEIGHT(win
) - 2 * wc
.border_width
);
2804 wc
.width
= WIDTH(win
);
2805 wc
.height
= HEIGHT(win
);
2808 * Retain floater and transient geometry for correct positioning
2809 * when ws changes region
2811 if (!(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
2812 store_float_geom(win
, r
);
2814 DNPRINTF(SWM_D_MISC
, "stack_floater: window: %lu, (x,y) w x h: (%d,%d) "
2815 "%d x %d\n", win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
2817 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2821 * Send keystrokes to terminal to decrease/increase the font size as the
2822 * window size changes.
2825 adjust_font(struct ws_win
*win
)
2827 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
2828 win
->floating
|| win
->transient
)
2831 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
2832 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
2833 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
2834 win
->font_size_boundary
[win
->font_steps
] =
2835 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
2838 win
->last_inc
= win
->sh
.width_inc
;
2839 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
2840 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
2841 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
2844 win
->last_inc
= win
->sh
.width_inc
;
2845 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
2849 #define SWAPXY(g) do { \
2851 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2852 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2855 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
2858 XWindowAttributes wa
;
2859 struct swm_geometry win_g
, r_g
= *g
;
2860 struct ws_win
*win
, *fs_win
= 0;
2861 int i
, j
, s
, stacks
;
2862 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
2863 int hrh
, extra
= 0, h_slice
, last_h
= 0;
2864 int split
, colno
, winno
, mwin
, msize
, mscale
;
2865 int remain
, missing
, v_slice
, reconfigure
;
2868 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
2869 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
2871 winno
= count_win(ws
, 0);
2872 if (winno
== 0 && count_win(ws
, 1) == 0)
2875 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2876 if (win
->transient
== 0 && win
->floating
== 0
2877 && win
->iconic
== 0)
2884 w_inc
= win
->sh
.width_inc
;
2885 w_base
= win
->sh
.base_width
;
2886 mwin
= ws
->l_state
.horizontal_mwin
;
2887 mscale
= ws
->l_state
.horizontal_msize
;
2888 stacks
= ws
->l_state
.horizontal_stacks
;
2891 w_inc
= win
->sh
.height_inc
;
2892 w_base
= win
->sh
.base_height
;
2893 mwin
= ws
->l_state
.vertical_mwin
;
2894 mscale
= ws
->l_state
.vertical_msize
;
2895 stacks
= ws
->l_state
.vertical_stacks
;
2899 if (stacks
> winno
- mwin
)
2900 stacks
= winno
- mwin
;
2904 h_slice
= r_g
.h
/ SWM_H_SLICE
;
2905 if (mwin
&& winno
> mwin
) {
2906 v_slice
= r_g
.w
/ SWM_V_SLICE
;
2910 win_g
.w
= v_slice
* mscale
;
2912 if (w_inc
> 1 && w_inc
< v_slice
) {
2913 /* adjust for window's requested size increment */
2914 remain
= (win_g
.w
- w_base
) % w_inc
;
2915 missing
= w_inc
- remain
;
2922 win_g
.x
+= r_g
.w
- msize
;
2925 colno
= split
= winno
/ stacks
;
2926 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
2927 2 * border_width
) / stacks
);
2929 hrh
= r_g
.h
/ colno
;
2930 extra
= r_g
.h
- (colno
* hrh
);
2931 win_g
.h
= hrh
- 2 * border_width
;
2933 /* stack all the tiled windows */
2934 i
= j
= 0, s
= stacks
;
2935 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2936 if (win
->transient
!= 0 || win
->floating
!= 0)
2938 if (win
->iconic
!= 0)
2941 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
2946 if (split
&& i
== split
) {
2947 colno
= (winno
- mwin
) / stacks
;
2948 if (s
<= (winno
- mwin
) % stacks
)
2950 split
= split
+ colno
;
2951 hrh
= (r_g
.h
/ colno
);
2952 extra
= r_g
.h
- (colno
* hrh
);
2956 win_g
.x
+= win_g
.w
+ 2 * border_width
;
2957 win_g
.w
= (r_g
.w
- msize
-
2958 (stacks
* 2 * border_width
)) / stacks
;
2960 win_g
.w
+= (r_g
.w
- msize
-
2961 (stacks
* 2 * border_width
)) % stacks
;
2965 win_g
.h
= hrh
- 2 * border_width
;
2967 h_inc
= win
->sh
.width_inc
;
2968 h_base
= win
->sh
.base_width
;
2970 h_inc
= win
->sh
.height_inc
;
2971 h_base
= win
->sh
.base_height
;
2973 if (j
== colno
- 1) {
2974 win_g
.h
= hrh
+ extra
;
2975 } else if (h_inc
> 1 && h_inc
< h_slice
) {
2976 /* adjust for window's requested size increment */
2977 remain
= (win_g
.h
- h_base
) % h_inc
;
2978 missing
= h_inc
- remain
;
2980 if (missing
<= extra
|| j
== 0) {
2992 win_g
.y
+= last_h
+ 2 * border_width
;
2994 bzero(&wc
, sizeof wc
);
2995 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
2996 wc
.border_width
= 0;
2997 win_g
.w
+= 2 * border_width
;
2998 win_g
.h
+= 2 * border_width
;
3000 wc
.border_width
= border_width
;
3003 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3004 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3006 X(win
) = wc
.x
= win_g
.y
;
3007 Y(win
) = wc
.y
= win_g
.x
;
3008 WIDTH(win
) = wc
.width
= win_g
.h
;
3009 HEIGHT(win
) = wc
.height
= win_g
.w
;
3012 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3013 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3015 X(win
) = wc
.x
= win_g
.x
;
3016 Y(win
) = wc
.y
= win_g
.y
;
3017 WIDTH(win
) = wc
.width
= win_g
.w
;
3018 HEIGHT(win
) = wc
.height
= win_g
.h
;
3023 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
3024 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3027 if (XGetWindowAttributes(display
, win
->id
, &wa
))
3028 if (wa
.map_state
== IsUnmapped
)
3029 XMapRaised(display
, win
->id
);
3037 /* now, stack all the floaters and transients */
3038 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3039 if (win
->transient
== 0 && win
->floating
== 0)
3041 if (win
->iconic
== 1)
3043 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3048 stack_floater(win
, ws
->r
);
3049 XMapRaised(display
, win
->id
);
3053 stack_floater(fs_win
, ws
->r
);
3054 XMapRaised(display
, fs_win
->id
);
3059 vertical_config(struct workspace
*ws
, int id
)
3061 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3065 case SWM_ARG_ID_STACKRESET
:
3066 case SWM_ARG_ID_STACKINIT
:
3067 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3068 ws
->l_state
.vertical_mwin
= 1;
3069 ws
->l_state
.vertical_stacks
= 1;
3071 case SWM_ARG_ID_MASTERSHRINK
:
3072 if (ws
->l_state
.vertical_msize
> 1)
3073 ws
->l_state
.vertical_msize
--;
3075 case SWM_ARG_ID_MASTERGROW
:
3076 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3077 ws
->l_state
.vertical_msize
++;
3079 case SWM_ARG_ID_MASTERADD
:
3080 ws
->l_state
.vertical_mwin
++;
3082 case SWM_ARG_ID_MASTERDEL
:
3083 if (ws
->l_state
.vertical_mwin
> 0)
3084 ws
->l_state
.vertical_mwin
--;
3086 case SWM_ARG_ID_STACKINC
:
3087 ws
->l_state
.vertical_stacks
++;
3089 case SWM_ARG_ID_STACKDEC
:
3090 if (ws
->l_state
.vertical_stacks
> 1)
3091 ws
->l_state
.vertical_stacks
--;
3099 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3101 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3103 stack_master(ws
, g
, 0, 0);
3107 horizontal_config(struct workspace
*ws
, int id
)
3109 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3112 case SWM_ARG_ID_STACKRESET
:
3113 case SWM_ARG_ID_STACKINIT
:
3114 ws
->l_state
.horizontal_mwin
= 1;
3115 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3116 ws
->l_state
.horizontal_stacks
= 1;
3118 case SWM_ARG_ID_MASTERSHRINK
:
3119 if (ws
->l_state
.horizontal_msize
> 1)
3120 ws
->l_state
.horizontal_msize
--;
3122 case SWM_ARG_ID_MASTERGROW
:
3123 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3124 ws
->l_state
.horizontal_msize
++;
3126 case SWM_ARG_ID_MASTERADD
:
3127 ws
->l_state
.horizontal_mwin
++;
3129 case SWM_ARG_ID_MASTERDEL
:
3130 if (ws
->l_state
.horizontal_mwin
> 0)
3131 ws
->l_state
.horizontal_mwin
--;
3133 case SWM_ARG_ID_STACKINC
:
3134 ws
->l_state
.horizontal_stacks
++;
3136 case SWM_ARG_ID_STACKDEC
:
3137 if (ws
->l_state
.horizontal_stacks
> 1)
3138 ws
->l_state
.horizontal_stacks
--;
3146 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3148 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3150 stack_master(ws
, g
, 1, 0);
3153 /* fullscreen view */
3155 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3158 struct swm_geometry gg
= *g
;
3159 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3163 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3168 winno
= count_win(ws
, 0);
3169 if (winno
== 0 && count_win(ws
, 1) == 0)
3172 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3173 if (win
->transient
) {
3175 parent
= find_window(win
->transient
);
3179 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3181 * retain geometry for retrieval on exit from
3184 store_float_geom(win
, ws
->r
);
3185 win
->floatmaxed
= 1;
3188 /* only reconfigure if necessary */
3189 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3190 HEIGHT(win
) != gg
.h
) {
3191 bzero(&wc
, sizeof wc
);
3192 X(win
) = wc
.x
= gg
.x
;
3193 Y(win
) = wc
.y
= gg
.y
;
3195 wc
.border_width
= border_width
;
3196 WIDTH(win
) = wc
.width
= gg
.w
;
3197 HEIGHT(win
) = wc
.height
= gg
.h
;
3199 wc
.border_width
= 0;
3200 WIDTH(win
) = wc
.width
= gg
.w
+ 2 * border_width
;
3201 HEIGHT(win
) = wc
.height
= gg
.h
+
3204 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
3205 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3207 /* unmap only if we don't have multi screen */
3208 if (win
!= ws
->focus
)
3209 if (!(ScreenCount(display
) > 1 || outputs
> 1))
3213 /* put the last transient on top */
3216 XMapRaised(display
, parent
->id
);
3217 stack_floater(wintrans
, ws
->r
);
3218 focus_magic(wintrans
);
3223 send_to_ws(struct swm_region
*r
, union arg
*args
)
3225 int wsid
= args
->id
;
3226 struct ws_win
*win
= NULL
, *parent
;
3227 struct workspace
*ws
, *nws
;
3228 Atom ws_idx_atom
= 0;
3229 unsigned char ws_idx_str
[SWM_PROPLEN
];
3232 if (r
&& r
->ws
&& r
->ws
->focus
)
3238 if (win
->ws
->idx
== wsid
)
3241 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%lx\n", win
->id
);
3244 nws
= &win
->s
->ws
[wsid
];
3246 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3248 if (win
->transient
) {
3249 parent
= find_window(win
->transient
);
3251 unmap_window(parent
);
3252 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3253 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3258 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3259 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3260 if (TAILQ_EMPTY(&ws
->winlist
))
3261 r
->ws
->focus
= NULL
;
3264 /* Try to update the window's workspace property */
3265 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
3267 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3269 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3271 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
3272 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
3279 pressbutton(struct swm_region
*r
, union arg
*args
)
3281 XTestFakeButtonEvent(display
, args
->id
, True
, CurrentTime
);
3282 XTestFakeButtonEvent(display
, args
->id
, False
, CurrentTime
);
3286 raise_toggle(struct swm_region
*r
, union arg
*args
)
3288 if (r
&& r
->ws
== NULL
)
3291 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3293 /* bring floaters back to top */
3294 if (r
->ws
->always_raise
== 0)
3299 iconify(struct swm_region
*r
, union arg
*args
)
3303 if (r
->ws
->focus
== NULL
)
3305 unmap_window(r
->ws
->focus
);
3306 update_iconic(r
->ws
->focus
, 1);
3308 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3309 drain_enter_notify();
3310 r
->ws
->focus
= NULL
;
3311 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3316 get_win_name(Display
*dpy
, Window win
, Atom wname
, Atom stype
,
3317 unsigned long *slen
)
3320 unsigned long nitems
, nbytes
, nextra
;
3321 unsigned char *prop
= NULL
;
3324 status
= XGetWindowProperty(dpy
, win
, wname
, 0L, 0L, False
, stype
,
3325 &rettype
, &retfmt
, &nitems
, &nbytes
, &prop
);
3326 if (status
!= Success
)
3330 status
= XGetWindowProperty(dpy
, win
, wname
, 0L, nbytes
, False
,
3331 stype
, &rettype
, &retfmt
, &nitems
, &nextra
, &prop
);
3332 if (status
!= Success
) {
3336 if (rettype
!= stype
) {
3345 uniconify(struct swm_region
*r
, union arg
*args
)
3349 unsigned char *name
;
3353 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3355 if (r
&& r
->ws
== NULL
)
3358 /* make sure we have anything to uniconify */
3359 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3360 if (win
->ws
== NULL
)
3361 continue; /* should never happen */
3362 if (win
->iconic
== 0)
3370 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3372 spawn_select(r
, args
, "search", &searchpid
);
3374 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3377 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3378 if (win
->ws
== NULL
)
3379 continue; /* should never happen */
3380 if (win
->iconic
== 0)
3383 name
= get_win_name(display
, win
->id
, a_netwmname
,
3384 a_utf8_string
, &len
);
3387 fprintf(lfile
, "%s.%lu\n", name
, win
->id
);
3395 name_workspace(struct swm_region
*r
, union arg
*args
)
3399 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3405 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3407 spawn_select(r
, args
, "name_workspace", &searchpid
);
3409 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3412 fprintf(lfile
, "%s", "");
3417 search_workspace(struct swm_region
*r
, union arg
*args
)
3420 struct workspace
*ws
;
3423 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3429 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3431 spawn_select(r
, args
, "search", &searchpid
);
3433 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3436 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
3440 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3441 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3448 search_win_cleanup(void)
3450 struct search_window
*sw
= NULL
;
3452 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3453 XDestroyWindow(display
, sw
->indicator
);
3454 XFreeGC(display
, sw
->gc
);
3455 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3461 search_win(struct swm_region
*r
, union arg
*args
)
3463 struct ws_win
*win
= NULL
;
3464 struct search_window
*sw
= NULL
;
3471 XRectangle ibox
, lbox
;
3473 DNPRINTF(SWM_D_MISC
, "search_win\n");
3476 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3478 spawn_select(r
, args
, "search", &searchpid
);
3480 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3483 TAILQ_INIT(&search_wl
);
3486 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3487 if (win
->iconic
== 1)
3490 sw
= calloc(1, sizeof(struct search_window
));
3492 warn("search_win: calloc");
3494 search_win_cleanup();
3500 snprintf(s
, sizeof s
, "%d", i
);
3503 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
3505 w
= XCreateSimpleWindow(display
,
3506 win
->id
, 0, 0,lbox
.width
+ 4,
3507 bar_fs_extents
->max_logical_extent
.height
, 1,
3508 r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
,
3509 r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
3512 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3514 sw
->gc
= XCreateGC(display
, w
, 0, &gcv
);
3515 XMapRaised(display
, w
);
3516 XSetForeground(display
, sw
->gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
3518 DRAWSTRING(display
, w
, bar_fs
, sw
->gc
, 2,
3519 (bar_fs_extents
->max_logical_extent
.height
-
3520 lbox
.height
) / 2 - lbox
.y
, s
, len
);
3522 fprintf(lfile
, "%d\n", i
);
3530 search_resp_uniconify(char *resp
, unsigned long len
)
3532 unsigned char *name
;
3536 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3538 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3539 if (win
->iconic
== 0)
3541 name
= get_win_name(display
, win
->id
, a_netwmname
,
3542 a_utf8_string
, &len
);
3545 if (asprintf(&s
, "%s.%lu", name
, win
->id
) == -1) {
3550 if (strncmp(s
, resp
, len
) == 0) {
3551 /* XXX this should be a callback to generalize */
3552 update_iconic(win
, 0);
3561 search_resp_name_workspace(char *resp
, unsigned long len
)
3563 struct workspace
*ws
;
3565 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3567 if (search_r
->ws
== NULL
)
3572 free(search_r
->ws
->name
);
3573 search_r
->ws
->name
= NULL
;
3577 ws
->name
= strdup(resp
);
3578 if (ws
->name
== NULL
) {
3579 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
3580 "strdup: %s", strerror(errno
));
3587 search_resp_search_workspace(char *resp
, unsigned long len
)
3594 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
3598 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
3605 ws_idx
= strtonum(q
, 1, SWM_WS_MAX
, &errstr
);
3607 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
3614 switchws(search_r
, &a
);
3618 search_resp_search_window(char *resp
, unsigned long len
)
3623 struct search_window
*sw
;
3625 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
3629 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
3634 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
3636 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
3643 TAILQ_FOREACH(sw
, &search_wl
, entry
)
3644 if (idx
== sw
->idx
) {
3650 #define MAX_RESP_LEN 1024
3653 search_do_resp(void)
3659 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
3664 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
3665 warn("search: calloc");
3669 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
3671 warn("search: read error");
3674 resp
[rbytes
] = '\0';
3677 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3678 * newline, so work around that by sanitizing the resp now.
3680 resp
[strcspn(resp
, "\n")] = '\0';
3683 switch (search_resp_action
) {
3684 case SWM_SEARCH_UNICONIFY
:
3685 search_resp_uniconify(resp
, len
);
3687 case SWM_SEARCH_NAME_WORKSPACE
:
3688 search_resp_name_workspace(resp
, len
);
3690 case SWM_SEARCH_SEARCH_WORKSPACE
:
3691 search_resp_search_workspace(resp
, len
);
3693 case SWM_SEARCH_SEARCH_WINDOW
:
3694 search_resp_search_window(resp
, len
);
3699 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
3700 search_win_cleanup();
3702 search_resp_action
= SWM_SEARCH_NONE
;
3703 close(select_resp_pipe
[0]);
3708 wkill(struct swm_region
*r
, union arg
*args
)
3710 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
3712 if (r
->ws
->focus
== NULL
)
3715 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
3716 XKillClient(display
, r
->ws
->focus
->id
);
3718 if (r
->ws
->focus
->can_delete
)
3719 client_msg(r
->ws
->focus
, adelete
);
3724 floating_toggle_win(struct ws_win
*win
)
3726 struct swm_region
*r
;
3736 /* reject floating toggles in max stack mode */
3737 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3740 if (win
->floating
) {
3741 if (!win
->floatmaxed
) {
3742 /* retain position for refloat */
3743 store_float_geom(win
, r
);
3747 if (win
->g_floatvalid
) {
3748 /* refloat at last floating relative position */
3749 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ X(r
);
3750 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ Y(r
);
3751 WIDTH(win
) = win
->g_float
.w
;
3752 HEIGHT(win
) = win
->g_float
.h
;
3757 ewmh_update_actions(win
);
3763 floating_toggle(struct swm_region
*r
, union arg
*args
)
3765 struct ws_win
*win
= r
->ws
->focus
;
3771 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
3772 _NET_WM_STATE_TOGGLE
);
3775 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3776 drain_enter_notify();
3778 if (win
== win
->ws
->focus
) {
3779 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3780 focus(win
->ws
->r
, &a
);
3785 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
3787 if (X(win
) + WIDTH(win
) > X(r
) + WIDTH(r
) - border_width
) {
3789 WIDTH(win
) = X(r
) + WIDTH(r
) - X(win
) - border_width
;
3791 X(win
) = X(r
) + WIDTH(r
) - WIDTH(win
) - border_width
;
3794 if (X(win
) < X(r
) - border_width
) {
3796 WIDTH(win
) -= X(r
) - X(win
) - border_width
;
3798 X(win
) = X(r
) - border_width
;
3801 if (Y(win
) + HEIGHT(win
) > Y(r
) + HEIGHT(r
) - border_width
) {
3803 HEIGHT(win
) = Y(r
) + HEIGHT(r
) - Y(win
) - border_width
;
3805 Y(win
) = Y(r
) + HEIGHT(r
) - HEIGHT(win
) - border_width
;
3808 if (Y(win
) < Y(r
) - border_width
) {
3810 HEIGHT(win
) -= Y(r
) - Y(win
) - border_width
;
3812 Y(win
) = Y(r
) - border_width
;
3817 if (HEIGHT(win
) < 1)
3822 update_window(struct ws_win
*win
)
3827 bzero(&wc
, sizeof wc
);
3828 mask
= CWBorderWidth
| CWWidth
| CWHeight
| CWX
| CWY
;
3829 wc
.border_width
= border_width
;
3832 wc
.width
= WIDTH(win
);
3833 wc
.height
= HEIGHT(win
);
3835 DNPRINTF(SWM_D_MISC
, "update_window: window: 0x%lx, (x,y) w x h: "
3836 "(%d,%d) %d x %d\n", win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
3838 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3841 #define SWM_RESIZE_STEPS (50)
3844 resize(struct ws_win
*win
, union arg
*args
)
3848 struct swm_region
*r
= NULL
;
3849 int resize_step
= 0;
3853 struct swm_geometry g
;
3854 int top
= 0, left
= 0;
3857 unsigned int shape
; /* cursor style */
3863 DNPRINTF(SWM_D_MOUSE
, "resize: window: 0x%lx, floating: %s, "
3864 "transient: 0x%lx\n", win
->id
, YESNO(win
->floating
),
3867 if (!(win
->transient
!= 0 || win
->floating
!= 0))
3870 /* reject resizes in max mode for floaters (transient ok) */
3871 if (win
->floatmaxed
)
3875 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
3881 case SWM_ARG_ID_WIDTHSHRINK
:
3882 WIDTH(win
) -= SWM_RESIZE_STEPS
;
3885 case SWM_ARG_ID_WIDTHGROW
:
3886 WIDTH(win
) += SWM_RESIZE_STEPS
;
3889 case SWM_ARG_ID_HEIGHTSHRINK
:
3890 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
3893 case SWM_ARG_ID_HEIGHTGROW
:
3894 HEIGHT(win
) += SWM_RESIZE_STEPS
;
3901 constrain_window(win
, r
, 1);
3903 store_float_geom(win
,r
);
3907 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3908 drain_enter_notify();
3910 /* get cursor offset from window root */
3911 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
3916 if (wx
< WIDTH(win
) / 2)
3919 if (wy
< HEIGHT(win
) / 2)
3922 if (args
->id
== SWM_ARG_ID_CENTER
)
3925 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
3927 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
3929 cursor
= XCreateFontCursor(display
, shape
);
3931 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
3932 GrabModeAsync
, None
, cursor
, CurrentTime
) != GrabSuccess
) {
3933 XFreeCursor(display
, cursor
);
3938 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
3939 SubstructureRedirectMask
, &ev
);
3941 case ConfigureRequest
:
3944 handler
[ev
.type
](&ev
);
3947 /* cursor offset/delta from start of the operation */
3948 dx
= ev
.xmotion
.x_root
- x
;
3949 dy
= ev
.xmotion
.y_root
- y
;
3955 if (args
->id
== SWM_ARG_ID_CENTER
) {
3956 if (g
.h
/ 2 + dy
< 1)
3960 HEIGHT(win
) = g
.h
+ 2 * dy
;
3968 HEIGHT(win
) = g
.h
+ dy
;
3975 if (args
->id
== SWM_ARG_ID_CENTER
) {
3976 if (g
.w
/ 2 + dx
< 1)
3980 WIDTH(win
) = g
.w
+ 2 * dx
;
3988 WIDTH(win
) = g
.w
+ dx
;
3991 constrain_window(win
, r
, 1);
3993 /* not free, don't sync more than 120 times / second */
3994 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
3995 time
= ev
.xmotion
.time
;
3996 XSync(display
, False
);
4001 } while (ev
.type
!= ButtonRelease
);
4003 XSync(display
, False
);
4006 store_float_geom(win
,r
);
4008 XUngrabPointer(display
, CurrentTime
);
4009 XFreeCursor(display
, cursor
);
4012 drain_enter_notify();
4016 resize_step(struct swm_region
*r
, union arg
*args
)
4018 struct ws_win
*win
= NULL
;
4020 if (r
&& r
->ws
&& r
->ws
->focus
)
4028 #define SWM_MOVE_STEPS (50)
4031 move(struct ws_win
*win
, union arg
*args
)
4036 struct swm_region
*r
= NULL
;
4046 DNPRINTF(SWM_D_MOUSE
, "move: window: 0x%lx, floating: %s, transient: "
4047 "0x%lx\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4049 /* in max_stack mode should only move transients */
4050 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4054 if (win
->floating
== 0 && !win
->transient
) {
4055 store_float_geom(win
,r
);
4056 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4059 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4066 case SWM_ARG_ID_MOVELEFT
:
4067 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4070 case SWM_ARG_ID_MOVERIGHT
:
4071 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4074 case SWM_ARG_ID_MOVEUP
:
4075 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4078 case SWM_ARG_ID_MOVEDOWN
:
4079 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4086 constrain_window(win
, r
, 0);
4088 store_float_geom(win
, r
);
4092 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4093 GrabModeAsync
, None
, XCreateFontCursor(display
, XC_fleur
),
4094 CurrentTime
) != GrabSuccess
)
4097 /* get cursor offset from window root */
4098 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4102 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4103 SubstructureRedirectMask
, &ev
);
4105 case ConfigureRequest
:
4108 handler
[ev
.type
](&ev
);
4111 X(win
) = ev
.xmotion
.x_root
- wx
- border_width
;
4112 Y(win
) = ev
.xmotion
.y_root
- wy
- border_width
;
4114 constrain_window(win
, r
, 0);
4116 /* not free, don't sync more than 120 times / second */
4117 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4118 time
= ev
.xmotion
.time
;
4119 XSync(display
, False
);
4124 } while (ev
.type
!= ButtonRelease
);
4126 XSync(display
, False
);
4129 store_float_geom(win
,r
);
4130 XUngrabPointer(display
, CurrentTime
);
4133 drain_enter_notify();
4137 move_step(struct swm_region
*r
, union arg
*args
)
4139 struct ws_win
*win
= NULL
;
4141 if (r
&& r
->ws
&& r
->ws
->focus
)
4146 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4153 /* user/key callable function IDs */
4223 kf_search_workspace
,
4225 kf_dumpwins
, /* MUST BE LAST */
4229 /* key definitions */
4231 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4236 legacyfunc(struct swm_region
*r
, union arg
*args
)
4241 char name
[SWM_FUNCNAME_LEN
];
4242 void (*func
)(struct swm_region
*r
, union arg
*);
4244 } keyfuncs
[kf_invalid
+ 1] = {
4245 /* name function argument */
4246 { "cycle_layout", cycle_layout
, {0} },
4247 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4248 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4249 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4250 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4251 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4252 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4253 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4254 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4255 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4256 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4257 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4258 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4259 { "spawn_term", spawnterm
, {.argv
= spawn_term
} },
4260 { "spawn_menu", legacyfunc
, {0} },
4261 { "quit", quit
, {0} },
4262 { "restart", restart
, {0} },
4263 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4264 { "ws_1", switchws
, {.id
= 0} },
4265 { "ws_2", switchws
, {.id
= 1} },
4266 { "ws_3", switchws
, {.id
= 2} },
4267 { "ws_4", switchws
, {.id
= 3} },
4268 { "ws_5", switchws
, {.id
= 4} },
4269 { "ws_6", switchws
, {.id
= 5} },
4270 { "ws_7", switchws
, {.id
= 6} },
4271 { "ws_8", switchws
, {.id
= 7} },
4272 { "ws_9", switchws
, {.id
= 8} },
4273 { "ws_10", switchws
, {.id
= 9} },
4274 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4275 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4276 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4277 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4278 { "ws_prior", priorws
, {0} },
4279 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4280 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4281 { "mvws_1", send_to_ws
, {.id
= 0} },
4282 { "mvws_2", send_to_ws
, {.id
= 1} },
4283 { "mvws_3", send_to_ws
, {.id
= 2} },
4284 { "mvws_4", send_to_ws
, {.id
= 3} },
4285 { "mvws_5", send_to_ws
, {.id
= 4} },
4286 { "mvws_6", send_to_ws
, {.id
= 5} },
4287 { "mvws_7", send_to_ws
, {.id
= 6} },
4288 { "mvws_8", send_to_ws
, {.id
= 7} },
4289 { "mvws_9", send_to_ws
, {.id
= 8} },
4290 { "mvws_10", send_to_ws
, {.id
= 9} },
4291 { "bar_toggle", bar_toggle
, {0} },
4292 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4293 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4294 { "screenshot_all", legacyfunc
, {0} },
4295 { "screenshot_wind", legacyfunc
, {0} },
4296 { "float_toggle", floating_toggle
,{0} },
4297 { "version", version
, {0} },
4298 { "spawn_lock", legacyfunc
, {0} },
4299 { "spawn_initscr", legacyfunc
, {0} },
4300 { "spawn_custom", dummykeyfunc
, {0} },
4301 { "iconify", iconify
, {0} },
4302 { "uniconify", uniconify
, {0} },
4303 { "raise_toggle", raise_toggle
, {0} },
4304 { "button2", pressbutton
, {2} },
4305 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4306 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4307 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4308 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4309 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4310 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4311 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4312 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4313 { "name_workspace", name_workspace
, {0} },
4314 { "search_workspace", search_workspace
, {0} },
4315 { "search_win", search_win
, {0} },
4316 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4317 { "invalid key func", NULL
, {0} },
4320 TAILQ_ENTRY(key
) entry
;
4323 enum keyfuncid funcid
;
4326 TAILQ_HEAD(key_list
, key
);
4327 struct key_list keys
= TAILQ_HEAD_INITIALIZER(keys
);
4330 enum { client_click
, root_click
};
4332 unsigned int action
;
4334 unsigned int button
;
4335 void (*func
)(struct ws_win
*, union arg
*);
4338 /* action key mouse button func args */
4339 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4340 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4341 { client_click
, MODKEY
, Button1
, move
, {0} },
4345 update_modkey(unsigned int mod
)
4351 TAILQ_FOREACH(kp
, &keys
, entry
)
4352 if (kp
->mod
& ShiftMask
)
4353 kp
->mod
= mod
| ShiftMask
;
4357 for (i
= 0; i
< LENGTH(buttons
); i
++)
4358 if (buttons
[i
].mask
& ShiftMask
)
4359 buttons
[i
].mask
= mod
| ShiftMask
;
4361 buttons
[i
].mask
= mod
;
4371 int spawns_size
= 0, spawns_length
= 0;
4372 struct spawn_prog
*spawns
= NULL
;
4375 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4378 struct spawn_prog
*prog
= NULL
;
4380 char *ap
, **real_args
;
4382 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4385 for (i
= 0; i
< spawns_length
; i
++) {
4386 if (!strcasecmp(spawn_name
, spawns
[i
].name
))
4390 warnx("spawn_custom: program %s not found", spawn_name
);
4394 /* make room for expanded args */
4395 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4396 err(1, "spawn_custom: calloc real_args");
4398 /* expand spawn_args into real_args */
4399 for (i
= 0; i
< prog
->argc
; i
++) {
4401 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4402 if (!strcasecmp(ap
, "$bar_border")) {
4404 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4406 err(1, "spawn_custom border color");
4407 } else if (!strcasecmp(ap
, "$bar_color")) {
4409 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4411 err(1, "spawn_custom bar color");
4412 } else if (!strcasecmp(ap
, "$bar_font")) {
4413 if ((real_args
[i
] = strdup(bar_fonts
))
4415 err(1, "spawn_custom bar fonts");
4416 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4418 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4420 err(1, "spawn_custom color font");
4421 } else if (!strcasecmp(ap
, "$color_focus")) {
4423 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4425 err(1, "spawn_custom color focus");
4426 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4428 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4430 err(1, "spawn_custom color unfocus");
4432 /* no match --> copy as is */
4433 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4434 err(1, "spawn_custom strdup(ap)");
4436 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4441 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4442 for (i
= 0; i
< prog
->argc
; i
++)
4443 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4444 DNPRINTF(SWM_D_SPAWN
, "\n");
4446 *ret_args
= real_args
;
4447 return (prog
->argc
);
4451 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4457 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4461 spawn(r
->ws
->idx
, &a
, 1);
4463 for (i
= 0; i
< spawn_argc
; i
++)
4469 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4475 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4479 if (pipe(select_list_pipe
) == -1)
4480 err(1, "pipe error");
4481 if (pipe(select_resp_pipe
) == -1)
4482 err(1, "pipe error");
4484 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4485 err(1, "could not disable SIGPIPE");
4486 switch (*pid
= fork()) {
4488 err(1, "cannot fork");
4491 if (dup2(select_list_pipe
[0], 0) == -1)
4493 if (dup2(select_resp_pipe
[1], 1) == -1)
4495 close(select_list_pipe
[1]);
4496 close(select_resp_pipe
[0]);
4497 spawn(r
->ws
->idx
, &a
, 0);
4499 default: /* parent */
4500 close(select_list_pipe
[0]);
4501 close(select_resp_pipe
[1]);
4505 for (i
= 0; i
< spawn_argc
; i
++)
4511 setspawn(struct spawn_prog
*prog
)
4515 if (prog
== NULL
|| prog
->name
== NULL
)
4519 for (i
= 0; i
< spawns_length
; i
++) {
4520 if (!strcmp(spawns
[i
].name
, prog
->name
)) {
4522 if (prog
->argv
== NULL
) {
4524 DNPRINTF(SWM_D_SPAWN
,
4525 "setspawn: delete #%d: %s\n",
4527 free(spawns
[i
].name
);
4528 for (j
= 0; j
< spawns
[i
].argc
; j
++)
4529 free(spawns
[i
].argv
[j
]);
4530 free(spawns
[i
].argv
);
4531 j
= spawns_length
- 1;
4533 spawns
[i
] = spawns
[j
];
4538 DNPRINTF(SWM_D_SPAWN
,
4539 "setspawn: replace #%d: %s\n",
4541 free(spawns
[i
].name
);
4542 for (j
= 0; j
< spawns
[i
].argc
; j
++)
4543 free(spawns
[i
].argv
[j
]);
4544 free(spawns
[i
].argv
);
4547 /* found case handled */
4553 if (prog
->argv
== NULL
) {
4554 warnx("error: setspawn: cannot find program: %s", prog
->name
);
4559 /* not found: add */
4560 if (spawns_size
== 0 || spawns
== NULL
) {
4562 DNPRINTF(SWM_D_SPAWN
, "setspawn: init list %d\n", spawns_size
);
4563 spawns
= malloc((size_t)spawns_size
*
4564 sizeof(struct spawn_prog
));
4565 if (spawns
== NULL
) {
4566 warn("setspawn: malloc failed");
4569 } else if (spawns_length
== spawns_size
) {
4571 DNPRINTF(SWM_D_SPAWN
, "setspawn: grow list %d\n", spawns_size
);
4572 spawns
= realloc(spawns
, (size_t)spawns_size
*
4573 sizeof(struct spawn_prog
));
4574 if (spawns
== NULL
) {
4575 warn("setspawn: realloc failed");
4580 if (spawns_length
< spawns_size
) {
4581 DNPRINTF(SWM_D_SPAWN
, "setspawn: add #%d %s\n",
4582 spawns_length
, prog
->name
);
4583 i
= spawns_length
++;
4586 warnx("spawns array problem?");
4587 if (spawns
== NULL
) {
4588 warnx("spawns array is NULL!");
4596 setconfspawn(char *selector
, char *value
, int flags
)
4598 struct spawn_prog
*prog
;
4599 char *vp
, *cp
, *word
;
4601 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
4602 if ((prog
= calloc(1, sizeof *prog
)) == NULL
)
4603 err(1, "setconfspawn: calloc prog");
4604 prog
->name
= strdup(selector
);
4605 if (prog
->name
== NULL
)
4606 err(1, "setconfspawn prog->name");
4607 if ((cp
= vp
= strdup(value
)) == NULL
)
4608 err(1, "setconfspawn: strdup(value) ");
4609 while ((word
= strsep(&cp
, " \t")) != NULL
) {
4610 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: arg [%s]\n", word
);
4612 cp
+= (long)strspn(cp
, " \t");
4613 if (strlen(word
) > 0) {
4615 if ((prog
->argv
= realloc(prog
->argv
,
4616 prog
->argc
* sizeof(char *))) == NULL
)
4617 err(1, "setconfspawn: realloc");
4618 if ((prog
->argv
[prog
->argc
- 1] = strdup(word
)) == NULL
)
4619 err(1, "setconfspawn: strdup");
4626 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
4633 setconfspawn("term", "xterm", 0);
4634 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4635 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4636 setconfspawn("lock", "xlock", 0);
4637 setconfspawn("initscr", "initscreen.sh", 0);
4638 setconfspawn("menu", "dmenu_run"
4641 " -nf $bar_font_color"
4643 " -sf $bar_color", 0);
4644 setconfspawn("search", "dmenu"
4648 " -nf $bar_font_color"
4650 " -sf $bar_color", 0);
4651 setconfspawn("name_workspace", "dmenu"
4655 " -nf $bar_font_color"
4657 " -sf $bar_color", 0);
4661 #define SWM_MODNAME_SIZE 32
4662 #define SWM_KEY_WS "\n+ \t"
4664 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
4668 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
4669 if (mod
== NULL
|| ks
== NULL
) {
4670 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
4673 if (keystr
== NULL
|| strlen(keystr
) == 0) {
4674 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
4680 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
4681 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
4683 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
4684 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
4686 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
4688 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
4690 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
4692 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
4694 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
4696 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
4697 *mod
|= ControlMask
;
4699 *ks
= XStringToKeysym(name
);
4700 XConvertCase(*ks
, ks
, &uks
);
4701 if (ks
== NoSymbol
) {
4703 "parsekeys: invalid key %s\n",
4709 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
4714 strdupsafe(char *str
)
4719 return (strdup(str
));
4723 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
4727 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
4728 keyfuncs
[kfid
].name
, spawn_name
);
4730 if ((kp
= malloc(sizeof *kp
)) == NULL
)
4731 err(1, "key_insert: malloc");
4736 kp
->spawn_name
= strdupsafe(spawn_name
);
4737 TAILQ_INSERT_TAIL(&keys
, kp
, entry
);
4739 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
4743 key_remove(struct key
*kp
)
4745 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
4747 TAILQ_REMOVE(&keys
, kp
, entry
);
4748 free(kp
->spawn_name
);
4751 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
4755 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
4758 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
4762 key_insert(mod
, ks
, kfid
, spawn_name
);
4764 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
4768 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
4773 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
4774 keyfuncs
[kfid
].name
, spawn_name
);
4776 TAILQ_FOREACH(kp
, &keys
, entry
) {
4777 if (kp
->mod
== mod
&& kp
->keysym
== ks
) {
4778 if (kfid
== kf_invalid
)
4781 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
4782 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4786 if (kfid
== kf_invalid
) {
4787 warnx("error: setkeybinding: cannot find mod/key combination");
4788 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4792 key_insert(mod
, ks
, kfid
, spawn_name
);
4793 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4797 setconfbinding(char *selector
, char *value
, int flags
)
4799 enum keyfuncid kfid
;
4803 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
4804 if (selector
== NULL
) {
4805 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
4806 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4808 setkeybinding(mod
, ks
, kfid
, NULL
);
4813 /* search by key function name */
4814 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
4815 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
4816 SWM_FUNCNAME_LEN
) == 0) {
4817 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4819 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4820 setkeybinding(mod
, ks
, kfid
, NULL
);
4826 /* search by custom spawn name */
4827 for (i
= 0; i
< spawns_length
; i
++) {
4828 if (strcasecmp(selector
, spawns
[i
].name
) == 0) {
4829 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4831 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4832 setkeybinding(mod
, ks
, kf_spawn_custom
,
4839 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
4846 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
4847 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
4848 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
4849 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
4850 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
4851 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
4852 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
4853 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
4854 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
4855 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
4856 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
4857 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
4858 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
4859 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_term
, NULL
);
4860 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
4861 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
4862 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
4863 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
4864 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
4865 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
4866 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
4867 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
4868 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
4869 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
4870 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
4871 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
4872 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
4873 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
4874 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
4875 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
4876 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
4877 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
4878 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
4879 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
4880 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
4881 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
4882 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
4883 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
4884 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
4885 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
4886 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
4887 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
4888 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
4889 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
4890 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
4891 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
4892 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
4893 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
4894 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
4895 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
4896 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
4897 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
4898 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
4899 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
4900 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
4901 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
4902 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
4903 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
4904 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
4905 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
4906 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
4907 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
4908 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
4909 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
4910 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
4911 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
4912 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
4913 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
4914 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
4915 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
4916 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
4918 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
4925 struct key
*kp_loop
, *kp_next
;
4927 kp_loop
= TAILQ_FIRST(&keys
);
4928 while (kp_loop
!= NULL
) {
4929 kp_next
= TAILQ_NEXT(kp_loop
, entry
);
4930 key_remove(kp_loop
);
4936 setkeymapping(char *selector
, char *value
, int flags
)
4938 char keymapping_file
[PATH_MAX
];
4939 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
4940 if (value
[0] == '~')
4941 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
4942 pwd
->pw_dir
, &value
[1]);
4944 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
4946 /* load new key bindings; if it fails, revert to default bindings */
4947 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
4951 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
4956 updatenumlockmask(void)
4959 XModifierKeymap
*modmap
;
4961 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
4963 modmap
= XGetModifierMapping(display
);
4964 for (i
= 0; i
< 8; i
++)
4965 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
4966 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
4967 == XKeysymToKeycode(display
, XK_Num_Lock
))
4968 numlockmask
= (1 << i
);
4970 XFreeModifiermap(modmap
);
4978 unsigned int modifiers
[] =
4979 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
4982 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
4983 updatenumlockmask();
4985 for (k
= 0; k
< ScreenCount(display
); k
++) {
4986 if (TAILQ_EMPTY(&screens
[k
].rl
))
4988 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
4989 TAILQ_FOREACH(kp
, &keys
, entry
) {
4990 if ((code
= XKeysymToKeycode(display
, kp
->keysym
)))
4991 for (j
= 0; j
< LENGTH(modifiers
); j
++)
4992 XGrabKey(display
, code
,
4993 kp
->mod
| modifiers
[j
],
4994 screens
[k
].root
, True
,
4995 GrabModeAsync
, GrabModeAsync
);
5001 grabbuttons(struct ws_win
*win
, int focused
)
5004 unsigned int modifiers
[] =
5005 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5007 updatenumlockmask();
5008 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
5010 for (i
= 0; i
< LENGTH(buttons
); i
++)
5011 if (buttons
[i
].action
== client_click
)
5012 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5013 XGrabButton(display
, buttons
[i
].button
,
5014 buttons
[i
].mask
| modifiers
[j
],
5015 win
->id
, False
, BUTTONMASK
,
5016 GrabModeAsync
, GrabModeSync
, None
,
5019 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
5020 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
5023 const char *quirkname
[] = {
5024 "NONE", /* config string for "no value" */
5033 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5034 #define SWM_Q_WS "\n|+ \t"
5036 parsequirks(char *qstr
, unsigned long *quirk
)
5046 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5048 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5049 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5050 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5051 DNPRINTF(SWM_D_QUIRK
,
5052 "parsequirks: %s\n", name
);
5057 *quirk
|= 1 << (i
-1);
5061 if (i
>= LENGTH(quirkname
)) {
5062 DNPRINTF(SWM_D_QUIRK
,
5063 "parsequirks: invalid quirk [%s]\n", name
);
5071 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5075 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5078 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5079 err(1, "quirk_insert: malloc");
5080 if ((qp
->class = strdup(class)) == NULL
)
5081 err(1, "quirk_insert: strdup");
5082 if ((qp
->name
= strdup(name
)) == NULL
)
5083 err(1, "quirk_insert: strdup");
5086 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5088 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5092 quirk_remove(struct quirk
*qp
)
5094 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5095 qp
->name
, qp
->quirk
);
5097 TAILQ_REMOVE(&quirks
, qp
, entry
);
5102 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5106 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5107 unsigned long quirk
)
5109 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5110 qp
->name
, qp
->quirk
);
5113 quirk_insert(class, name
, quirk
);
5115 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5119 setquirk(const char *class, const char *name
, unsigned long quirk
)
5123 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5126 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5127 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5131 quirk_replace(qp
, class, name
, quirk
);
5132 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5137 warnx("error: setquirk: cannot find class/name combination");
5141 quirk_insert(class, name
, quirk
);
5142 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5146 setconfquirk(char *selector
, char *value
, int flags
)
5148 char *cp
, *class, *name
;
5150 unsigned long quirks
;
5151 if (selector
== NULL
)
5153 if ((cp
= strchr(selector
, ':')) == NULL
)
5158 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5159 setquirk(class, name
, quirks
);
5166 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5167 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5168 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5169 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5170 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5171 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5172 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5173 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5174 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5175 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5176 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5177 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5178 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5181 /* conf file stuff */
5182 #define SWM_CONF_FILE "scrotwm.conf"
5184 enum { SWM_S_BAR_DELAY
, SWM_S_BAR_ENABLED
, SWM_S_BAR_BORDER_WIDTH
,
5185 SWM_S_STACK_ENABLED
, SWM_S_CLOCK_ENABLED
, SWM_S_CLOCK_FORMAT
,
5186 SWM_S_CYCLE_EMPTY
, SWM_S_CYCLE_VISIBLE
, SWM_S_SS_ENABLED
,
5187 SWM_S_TERM_WIDTH
, SWM_S_TITLE_CLASS_ENABLED
,
5188 SWM_S_TITLE_NAME_ENABLED
, SWM_S_WINDOW_NAME_ENABLED
, SWM_S_URGENT_ENABLED
,
5189 SWM_S_FOCUS_MODE
, SWM_S_DISABLE_BORDER
, SWM_S_BORDER_WIDTH
,
5190 SWM_S_BAR_FONT
, SWM_S_BAR_ACTION
, SWM_S_SPAWN_TERM
,
5191 SWM_S_SS_APP
, SWM_S_DIALOG_RATIO
, SWM_S_BAR_AT_BOTTOM
,
5192 SWM_S_VERBOSE_LAYOUT
, SWM_S_BAR_JUSTIFY
5196 setconfvalue(char *selector
, char *value
, int flags
)
5202 case SWM_S_BAR_DELAY
:
5203 bar_delay
= atoi(value
);
5205 case SWM_S_BAR_ENABLED
:
5206 bar_enabled
= atoi(value
);
5208 case SWM_S_BAR_BORDER_WIDTH
:
5209 bar_border_width
= atoi(value
);
5211 case SWM_S_BAR_AT_BOTTOM
:
5212 bar_at_bottom
= atoi(value
);
5214 case SWM_S_BAR_JUSTIFY
:
5215 if (!strcmp(value
, "left"))
5216 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5217 else if (!strcmp(value
, "center"))
5218 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5219 else if (!strcmp(value
, "right"))
5220 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5222 errx(1, "invalid bar_justify");
5224 case SWM_S_STACK_ENABLED
:
5225 stack_enabled
= atoi(value
);
5227 case SWM_S_CLOCK_ENABLED
:
5228 clock_enabled
= atoi(value
);
5230 case SWM_S_CLOCK_FORMAT
:
5231 #ifndef SWM_DENY_CLOCK_FORMAT
5233 if ((clock_format
= strdup(value
)) == NULL
)
5234 err(1, "setconfvalue: clock_format");
5237 case SWM_S_CYCLE_EMPTY
:
5238 cycle_empty
= atoi(value
);
5240 case SWM_S_CYCLE_VISIBLE
:
5241 cycle_visible
= atoi(value
);
5243 case SWM_S_SS_ENABLED
:
5244 ss_enabled
= atoi(value
);
5246 case SWM_S_TERM_WIDTH
:
5247 term_width
= atoi(value
);
5249 case SWM_S_TITLE_CLASS_ENABLED
:
5250 title_class_enabled
= atoi(value
);
5252 case SWM_S_WINDOW_NAME_ENABLED
:
5253 window_name_enabled
= atoi(value
);
5255 case SWM_S_TITLE_NAME_ENABLED
:
5256 title_name_enabled
= atoi(value
);
5258 case SWM_S_URGENT_ENABLED
:
5259 urgent_enabled
= atoi(value
);
5261 case SWM_S_FOCUS_MODE
:
5262 if (!strcmp(value
, "default"))
5263 focus_mode
= SWM_FOCUS_DEFAULT
;
5264 else if (!strcmp(value
, "follow_cursor"))
5265 focus_mode
= SWM_FOCUS_FOLLOW
;
5266 else if (!strcmp(value
, "synergy"))
5267 focus_mode
= SWM_FOCUS_SYNERGY
;
5269 errx(1, "focus_mode");
5271 case SWM_S_DISABLE_BORDER
:
5272 disable_border
= atoi(value
);
5274 case SWM_S_BORDER_WIDTH
:
5275 border_width
= atoi(value
);
5277 case SWM_S_BAR_FONT
:
5279 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5280 err(1, "setconfvalue: asprintf: failed to allocate "
5281 "memory for bar_fonts.");
5285 case SWM_S_BAR_ACTION
:
5287 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5288 err(1, "setconfvalue: bar_action");
5290 case SWM_S_SPAWN_TERM
:
5291 free(spawn_term
[0]);
5292 if ((spawn_term
[0] = strdup(value
)) == NULL
)
5293 err(1, "setconfvalue: spawn_term");
5297 case SWM_S_DIALOG_RATIO
:
5298 dialog_ratio
= atof(value
);
5299 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5302 case SWM_S_VERBOSE_LAYOUT
:
5303 verbose_layout
= atoi(value
);
5304 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5306 layouts
[i
].l_string
= fancy_stacker
;
5308 layouts
[i
].l_string
= plain_stacker
;
5318 setconfmodkey(char *selector
, char *value
, int flags
)
5320 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5321 update_modkey(Mod1Mask
);
5322 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5323 update_modkey(Mod2Mask
);
5324 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5325 update_modkey(Mod3Mask
);
5326 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5327 update_modkey(Mod4Mask
);
5334 setconfcolor(char *selector
, char *value
, int flags
)
5336 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5341 setconfregion(char *selector
, char *value
, int flags
)
5343 custom_region(value
);
5348 setautorun(char *selector
, char *value
, int flags
)
5358 if (getenv("SWM_STARTED"))
5362 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5363 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5365 if (ws_id
< 0 || ws_id
>= SWM_WS_MAX
)
5366 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5369 * This is a little intricate
5371 * If the pid already exists we simply reuse it because it means it was
5372 * used before AND not claimed by manage_window. We get away with
5373 * altering it in the parent after INSERT because this can not be a race
5376 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5379 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5381 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5382 err(1, "setautorun: realloc");
5383 a
.argv
[argc
- 1] = ap
;
5386 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5387 err(1, "setautorun: realloc");
5388 a
.argv
[argc
] = NULL
;
5390 if ((pid
= fork()) == 0) {
5391 spawn(ws_id
, &a
, 1);
5400 p
= calloc(1, sizeof *p
);
5403 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
5413 setlayout(char *selector
, char *value
, int flags
)
5415 int ws_id
, i
, x
, mg
, ma
, si
, raise
;
5416 int st
= SWM_V_STACK
;
5418 struct workspace
*ws
;
5420 if (getenv("SWM_STARTED"))
5424 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
5425 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
5426 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5427 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5430 if (ws_id
< 0 || ws_id
>= SWM_WS_MAX
)
5431 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
5433 if (!strcasecmp(s
, "vertical"))
5435 else if (!strcasecmp(s
, "horizontal"))
5437 else if (!strcasecmp(s
, "fullscreen"))
5440 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5441 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5444 for (i
= 0; i
< ScreenCount(display
); i
++) {
5445 ws
= (struct workspace
*)&screens
[i
].ws
;
5446 ws
[ws_id
].cur_layout
= &layouts
[st
];
5448 ws
[ws_id
].always_raise
= raise
;
5449 if (st
== SWM_MAX_STACK
)
5453 for (x
= 0; x
< abs(mg
); x
++) {
5454 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5455 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
5456 SWM_ARG_ID_MASTERSHRINK
);
5460 for (x
= 0; x
< abs(ma
); x
++) {
5461 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5462 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
5463 SWM_ARG_ID_MASTERDEL
);
5467 for (x
= 0; x
< abs(si
); x
++) {
5468 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5469 si
>= 0 ? SWM_ARG_ID_STACKINC
:
5470 SWM_ARG_ID_STACKDEC
);
5478 /* config options */
5479 struct config_option
{
5481 int (*func
)(char*, char*, int);
5484 struct config_option configopt
[] = {
5485 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
5486 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
5487 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
5488 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
5489 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
5490 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
5491 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
5492 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
5493 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
5494 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
5495 { "keyboard_mapping", setkeymapping
, 0 },
5496 { "bind", setconfbinding
, 0 },
5497 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
5498 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
5499 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
5500 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
5501 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
5502 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
5503 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
5504 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
5505 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
5506 { "modkey", setconfmodkey
, 0 },
5507 { "program", setconfspawn
, 0 },
5508 { "quirk", setconfquirk
, 0 },
5509 { "region", setconfregion
, 0 },
5510 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
5511 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
5512 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
5513 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
5514 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
5515 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
5516 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
5517 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
5518 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
5519 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
5520 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
5521 { "autorun", setautorun
, 0 },
5522 { "layout", setlayout
, 0 },
5527 conf_load(char *filename
, int keymapping
)
5530 char *line
, *cp
, *optsub
, *optval
;
5531 size_t linelen
, lineno
= 0;
5532 int wordlen
, i
, optind
;
5533 struct config_option
*opt
;
5535 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
5537 if (filename
== NULL
) {
5538 warnx("conf_load: no filename");
5541 if ((config
= fopen(filename
, "r")) == NULL
) {
5542 warn("conf_load: fopen: %s", filename
);
5546 while (!feof(config
)) {
5547 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
5550 err(1, "%s", filename
);
5555 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
5556 if (cp
[0] == '\0') {
5561 /* get config option */
5562 wordlen
= strcspn(cp
, "=[ \t\n");
5564 warnx("%s: line %zd: no option found",
5569 for (i
= 0; i
< LENGTH(configopt
); i
++) {
5570 opt
= &configopt
[i
];
5571 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
5572 strlen(opt
->optname
) == wordlen
) {
5578 warnx("%s: line %zd: unknown option %.*s",
5579 filename
, lineno
, wordlen
, cp
);
5582 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
5583 warnx("%s: line %zd: invalid option %.*s",
5584 filename
, lineno
, wordlen
, cp
);
5588 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
5589 /* get [selector] if any */
5593 wordlen
= strcspn(cp
, "]");
5596 warnx("%s: line %zd: syntax error",
5601 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
5603 warnx("%s: line %zd: unable to allocate"
5604 "memory for selector", filename
,
5610 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
5612 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
5614 optval
= strdup(cp
);
5615 /* call function to deal with it all */
5616 if (configopt
[optind
].func(optsub
, optval
,
5617 configopt
[optind
].funcflags
) != 0)
5618 errx(1, "%s: line %zd: invalid data for %s",
5619 filename
, lineno
, configopt
[optind
].optname
);
5626 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
5633 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
5639 set_child_transient(struct ws_win
*win
, Window
*trans
)
5641 struct ws_win
*parent
, *w
;
5642 XWMHints
*wmh
= NULL
;
5643 struct swm_region
*r
;
5644 struct workspace
*ws
;
5646 parent
= find_window(win
->transient
);
5648 parent
->child_trans
= win
;
5650 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
5651 " for 0x%lx trans 0x%lx\n", win
->id
, win
->transient
);
5653 if (win
->hints
== NULL
) {
5654 warnx("no hints for 0x%lx", win
->id
);
5658 r
= root_to_region(win
->wa
.root
);
5660 /* parent doen't exist in our window list */
5661 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5665 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
5666 warnx("can't get hints for 0x%lx", w
->id
);
5670 if (win
->hints
->window_group
!= wmh
->window_group
)
5673 w
->child_trans
= win
;
5674 win
->transient
= w
->id
;
5676 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
5677 "transient to 0x%lx\n", win
->transient
);
5687 window_get_pid(Window win
)
5689 Atom actual_type_return
;
5690 int actual_format_return
= 0;
5691 unsigned long nitems_return
= 0;
5692 unsigned long bytes_after_return
= 0;
5696 unsigned char *prop
= NULL
;
5698 if (XGetWindowProperty(display
, win
,
5699 XInternAtom(display
, "_NET_WM_PID", False
), 0, 1, False
,
5700 XA_CARDINAL
, &actual_type_return
, &actual_format_return
,
5701 &nitems_return
, &bytes_after_return
,
5702 (unsigned char**)(void*)&pid
) != Success
)
5704 if (actual_type_return
!= XA_CARDINAL
)
5715 if (XGetWindowProperty(display
, win
,
5716 XInternAtom(display
, "_SWM_PID", False
), 0, SWM_PROPLEN
, False
,
5717 XA_STRING
, &actual_type_return
, &actual_format_return
,
5718 &nitems_return
, &bytes_after_return
, &prop
) != Success
)
5720 if (actual_type_return
!= XA_STRING
)
5725 ret
= strtonum((const char *)prop
, 0, UINT_MAX
, &errstr
);
5726 /* ignore error because strtonum returns 0 anyway */
5733 manage_window(Window id
)
5736 struct workspace
*ws
;
5737 struct ws_win
*win
, *ww
;
5738 int format
, i
, ws_idx
, n
, border_me
= 0;
5739 unsigned long nitems
, bytes
;
5740 Atom ws_idx_atom
= 0, type
;
5741 Atom
*prot
= NULL
, *pp
;
5742 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
5743 struct swm_region
*r
;
5750 if ((win
= find_window(id
)) != NULL
)
5751 return (win
); /* already being managed */
5753 /* see if we are on the unmanaged list */
5754 if ((win
= find_unmanaged_window(id
)) != NULL
) {
5755 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
5756 "window: 0x%lx\n", win
->id
);
5757 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5758 if (win
->transient
) {
5759 set_child_transient(win
, &trans
);
5760 } if (trans
&& (ww
= find_window(trans
)))
5761 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
5763 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
5764 ewmh_update_actions(win
);
5768 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
5769 err(1, "manage_window: calloc: failed to allocate memory for "
5774 /* see if we need to override the workspace */
5775 p
= find_pid(window_get_pid(id
));
5777 /* Get all the window data in one shot */
5778 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
5780 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
5781 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
5783 XGetWindowAttributes(display
, id
, &win
->wa
);
5784 XGetWMNormalHints(display
, id
, &win
->sh
, &win
->sh_mask
);
5785 win
->hints
= XGetWMHints(display
, id
);
5786 XGetTransientForHint(display
, id
, &trans
);
5788 win
->transient
= trans
;
5789 set_child_transient(win
, &trans
);
5790 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, "
5791 "transient: 0x%lx\n", win
->id
, win
->transient
);
5794 /* get supported protocols */
5795 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
5796 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
5797 if (*pp
== takefocus
)
5798 win
->take_focus
= 1;
5800 win
->can_delete
= 1;
5806 win
->iconic
= get_iconic(win
);
5809 * Figure out where to put the window. If it was previously assigned to
5810 * a workspace (either by spawn() or manually moving), and isn't
5811 * transient, * put it in the same workspace
5813 r
= root_to_region(win
->wa
.root
);
5815 ws
= &r
->s
->ws
[p
->ws
];
5816 TAILQ_REMOVE(&pidlist
, p
, entry
);
5819 } else if (prop
&& win
->transient
== 0) {
5820 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
5821 ws_idx
= strtonum((const char *)prop
, 0, 9, &errstr
);
5823 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
5826 ws
= &r
->s
->ws
[ws_idx
];
5829 /* this should launch transients in the same ws as parent */
5831 if ((ww
= find_window(trans
)) != NULL
)
5837 warnx("manage_window: fix this "
5843 /* set up the window layout */
5846 win
->s
= r
->s
; /* this never changes */
5847 if (trans
&& (ww
= find_window(trans
)))
5848 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
5850 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
5852 WIDTH(win
) = win
->wa
.width
;
5853 HEIGHT(win
) = win
->wa
.height
;
5856 win
->g_floatvalid
= 0;
5857 win
->floatmaxed
= 0;
5858 win
->ewmh_flags
= 0;
5860 /* Set window properties so we can remember this after reincarnation */
5861 if (ws_idx_atom
&& prop
== NULL
&&
5862 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
5864 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
5866 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
5867 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
5872 ewmh_autoquirk(win
);
5874 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
5875 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
5876 win
->ch
.res_class
, win
->ch
.res_name
);
5878 /* java is retarded so treat it special */
5879 if (strstr(win
->ch
.res_name
, "sun-awt")) {
5884 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5885 if (!strcmp(win
->ch
.res_class
, qp
->class) &&
5886 !strcmp(win
->ch
.res_name
, qp
->name
)) {
5887 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
5888 "class: %s, name: %s\n", win
->ch
.res_class
,
5890 if (qp
->quirk
& SWM_Q_FLOAT
) {
5894 win
->quirks
= qp
->quirk
;
5899 /* alter window position if quirky */
5900 if (win
->quirks
& SWM_Q_ANYWHERE
) {
5901 win
->manual
= 1; /* don't center the quirky windows */
5902 bzero(&wc
, sizeof wc
);
5904 if (bar_enabled
&& Y(win
) < bar_height
) {
5905 Y(win
) = wc
.y
= bar_height
;
5908 if (WIDTH(win
) + X(win
) > WIDTH(r
)) {
5909 X(win
) = wc
.x
= WIDTH(r
) - WIDTH(win
) - 2;
5915 /* Reset font sizes (the bruteforce way; no default keybinding). */
5916 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
5917 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
5918 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
5919 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
5920 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
5923 ewmh_get_win_state(win
);
5924 ewmh_update_actions(win
);
5925 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
5929 bzero(&wc
, sizeof wc
);
5930 wc
.border_width
= border_width
;
5931 mask
= CWBorderWidth
;
5932 XConfigureWindow(display
, win
->id
, mask
, &wc
);
5935 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
5936 PropertyChangeMask
| StructureNotifyMask
);
5938 /* floaters need to be mapped if they are in the current workspace */
5939 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
5940 XMapRaised(display
, win
->id
);
5946 free_window(struct ws_win
*win
)
5948 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%lx\n", win
->id
);
5953 /* needed for restart wm */
5954 set_win_state(win
, WithdrawnState
);
5956 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5958 if (win
->ch
.res_class
)
5959 XFree(win
->ch
.res_class
);
5960 if (win
->ch
.res_name
)
5961 XFree(win
->ch
.res_name
);
5966 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
5972 unmanage_window(struct ws_win
*win
)
5974 struct ws_win
*parent
;
5979 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%lx\n", win
->id
);
5981 if (win
->transient
) {
5982 parent
= find_window(win
->transient
);
5984 parent
->child_trans
= NULL
;
5987 /* focus on root just in case */
5988 XSetInputFocus(display
, PointerRoot
, PointerRoot
, CurrentTime
);
5997 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
5998 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6004 focus_magic(struct ws_win
*win
)
6006 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%lx\n", WINID(win
));
6011 if (win
->child_trans
) {
6012 /* win = parent & has a transient so focus on that */
6014 focus_win(win
->child_trans
);
6015 if (win
->child_trans
->take_focus
)
6016 client_msg(win
, takefocus
);
6018 /* make sure transient hasn't dissapeared */
6019 if (validate_win(win
->child_trans
) == 0) {
6020 focus_win(win
->child_trans
);
6021 if (win
->child_trans
->take_focus
)
6022 client_msg(win
->child_trans
, takefocus
);
6024 win
->child_trans
= NULL
;
6026 if (win
->take_focus
)
6027 client_msg(win
, takefocus
);
6033 if (win
->take_focus
)
6034 client_msg(win
, takefocus
);
6041 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%lx\n", e
->xexpose
.window
);
6048 XKeyEvent
*ev
= &e
->xkey
;
6051 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
6052 TAILQ_FOREACH(kp
, &keys
, entry
)
6053 if (keysym
== kp
->keysym
6054 && CLEANMASK(kp
->mod
) == CLEANMASK(ev
->state
)
6055 && keyfuncs
[kp
->funcid
].func
) {
6056 if (kp
->funcid
== kf_spawn_custom
)
6058 root_to_region(ev
->root
),
6059 &(keyfuncs
[kp
->funcid
].args
),
6063 keyfuncs
[kp
->funcid
].func(
6064 root_to_region(ev
->root
),
6065 &(keyfuncs
[kp
->funcid
].args
)
6071 buttonpress(XEvent
*e
)
6075 XButtonPressedEvent
*ev
= &e
->xbutton
;
6077 if ((win
= find_window(ev
->window
)) == NULL
)
6081 action
= client_click
;
6083 for (i
= 0; i
< LENGTH(buttons
); i
++)
6084 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6085 buttons
[i
].button
== ev
->button
&&
6086 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
6087 buttons
[i
].func(win
, &buttons
[i
].args
);
6091 configurerequest(XEvent
*e
)
6093 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
6098 if ((win
= find_window(ev
->window
)) == NULL
)
6099 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6102 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%lx, new: %s\n",
6103 ev
->window
, YESNO(new));
6106 bzero(&wc
, sizeof wc
);
6109 wc
.width
= ev
->width
;
6110 wc
.height
= ev
->height
;
6111 wc
.border_width
= ev
->border_width
;
6112 wc
.sibling
= ev
->above
;
6113 wc
.stack_mode
= ev
->detail
;
6114 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
6116 config_win(win
, ev
);
6121 configurenotify(XEvent
*e
)
6125 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%lx\n",
6126 e
->xconfigure
.window
);
6128 win
= find_window(e
->xconfigure
.window
);
6130 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &win
->sh_mask
);
6134 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6135 drain_enter_notify();
6140 destroynotify(XEvent
*e
)
6143 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
6145 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%lx\n", ev
->window
);
6147 if ((win
= find_window(ev
->window
)) == NULL
) {
6148 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6154 /* make sure we focus on something */
6157 unmanage_window(win
);
6159 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6160 drain_enter_notify();
6165 enternotify(XEvent
*e
)
6167 XCrossingEvent
*ev
= &e
->xcrossing
;
6172 Window focus_return
;
6173 int revert_to_return
;
6175 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%lx, mode: %d, detail: "
6176 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6177 "state: %d\n", ev
->window
, ev
->mode
, ev
->detail
, ev
->root
,
6178 ev
->subwindow
, YESNO(ev
->same_screen
), YESNO(ev
->focus
), ev
->state
);
6180 if (ev
->mode
!= NotifyNormal
) {
6181 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6186 switch (focus_mode
) {
6187 case SWM_FOCUS_DEFAULT
:
6189 case SWM_FOCUS_FOLLOW
:
6191 case SWM_FOCUS_SYNERGY
:
6194 * all these checks need to be in this order because the
6195 * XCheckTypedWindowEvent relies on weeding out the previous events
6197 * making this code an option would enable a follow mouse for focus
6202 * state is set when we are switching workspaces and focus is set when
6203 * the window or a subwindow already has focus (occurs during restart).
6205 * Only honor the focus flag if last_focus_event is not FocusOut,
6206 * this allows scrotwm to continue to control focus when another
6207 * program is also playing with it.
6209 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6210 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6215 * happens when a window is created or destroyed and the border
6216 * crosses the mouse pointer and when switching ws
6218 * we need the subwindow test to see if we came from root in order
6219 * to give focus to floaters
6221 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6222 ev
->subwindow
== 0) {
6223 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6227 /* this window already has focus */
6228 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6229 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6233 /* this window is being deleted or moved to another ws */
6234 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6236 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6237 XPutBackEvent(display
, &cne
);
6241 if ((win
= find_window(ev
->window
)) == NULL
) {
6242 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6247 * In fullstack kill all enters unless they come from a different ws
6248 * (i.e. another region) or focus has been grabbed externally.
6250 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6251 last_focus_event
!= FocusOut
) {
6252 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6253 if ((w
= find_window(focus_return
)) == NULL
||
6255 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6263 if ((win
= find_window(ev
->window
)) == NULL
) {
6264 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6269 * if we have more enternotifies let them handle it in due time
6271 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
6272 DNPRINTF(SWM_D_EVENT
,
6273 "ignoring enternotify: got more enternotify\n");
6274 XPutBackEvent(display
, &cne
);
6281 /* lets us use one switch statement for arbitrary mode/detail combinations */
6282 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6285 focusevent(XEvent
*e
)
6289 u_int32_t mode_detail
;
6290 XFocusChangeEvent
*ev
= &e
->xfocus
;
6292 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: 0x%lx mode: %d "
6293 "detail: %d\n", ev
->type
== FocusIn
? "entering" : "leaving",
6294 ev
->window
, ev
->mode
, ev
->detail
);
6296 if (last_focus_event
== ev
->type
) {
6297 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
6301 last_focus_event
= ev
->type
;
6302 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
6304 switch (mode_detail
) {
6305 /* synergy client focus operations */
6306 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
6307 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
6309 /* synergy server focus operations */
6310 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
6312 /* Entering applications like rdesktop that mangle the pointer */
6313 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
6315 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
6316 XSetWindowBorder(display
, win
->id
,
6317 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
6318 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
6321 warnx("ignoring focusevent");
6322 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
6329 mapnotify(XEvent
*e
)
6332 XMapEvent
*ev
= &e
->xmap
;
6334 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%lx\n", ev
->window
);
6336 win
= manage_window(ev
->window
);
6338 set_win_state(win
, NormalState
);
6342 mappingnotify(XEvent
*e
)
6344 XMappingEvent
*ev
= &e
->xmapping
;
6346 XRefreshKeyboardMapping(ev
);
6347 if (ev
->request
== MappingKeyboard
)
6352 maprequest(XEvent
*e
)
6355 struct swm_region
*r
;
6356 XWindowAttributes wa
;
6357 XMapRequestEvent
*ev
= &e
->xmaprequest
;
6359 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%lx\n",
6360 e
->xmaprequest
.window
);
6362 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
6364 if (wa
.override_redirect
)
6367 win
= manage_window(e
->xmaprequest
.window
);
6369 return; /* can't happen */
6373 /* make new win focused */
6374 r
= root_to_region(win
->wa
.root
);
6375 if (win
->ws
== r
->ws
)
6380 propertynotify(XEvent
*e
)
6383 XPropertyEvent
*ev
= &e
->xproperty
;
6386 name
= XGetAtomName(display
, ev
->atom
);
6387 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%lx, atom: %s\n",
6392 win
= find_window(ev
->window
);
6396 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
6397 update_iconic(win
, 0);
6398 XMapRaised(display
, win
->id
);
6406 case XA_WM_NORMAL_HINTS
:
6408 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
6409 warnx("normal hints: flag 0x%x", win
->sh
.flags
);
6410 if (win
->sh
.flags
& PMinSize
) {
6411 WIDTH(win
) = win
->sh
.min_width
;
6412 HEIGHT(win
) = win
->sh
.min_height
;
6413 warnx("min %d %d", WIDTH(win
), HEIGHT(win
));
6415 XMoveResizeWindow(display
, win
->id
,
6416 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
));
6419 if (title_name_enabled
|| title_class_enabled
)
6423 if (window_name_enabled
)
6432 unmapnotify(XEvent
*e
)
6436 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%lx\n", e
->xunmap
.window
);
6438 /* determine if we need to help unmanage this window */
6439 win
= find_window(e
->xunmap
.window
);
6443 if (getstate(e
->xunmap
.window
) == NormalState
) {
6444 unmanage_window(win
);
6447 /* giant hack for apps that don't destroy transient windows */
6448 /* eat a bunch of events to prevent remanaging the window */
6450 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6451 EnterWindowMask
, &cne
))
6453 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6454 StructureNotifyMask
, &cne
))
6456 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6457 SubstructureNotifyMask
, &cne
))
6459 /* resend unmap because we ated it */
6460 XUnmapWindow(display
, e
->xunmap
.window
);
6463 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6464 drain_enter_notify();
6468 visibilitynotify(XEvent
*e
)
6471 struct swm_region
*r
;
6473 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%lx\n",
6474 e
->xvisibility
.window
);
6475 if (e
->xvisibility
.state
== VisibilityUnobscured
)
6476 for (i
= 0; i
< ScreenCount(display
); i
++)
6477 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6478 if (e
->xvisibility
.window
== r
->bar_window
)
6483 clientmessage(XEvent
*e
)
6485 XClientMessageEvent
*ev
;
6490 win
= find_window(ev
->window
);
6494 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx, type: %ld\n",
6495 ev
->window
, ev
->message_type
);
6497 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
6498 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
6501 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
6502 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
6503 if (win
->can_delete
)
6504 client_msg(win
, adelete
);
6506 XKillClient(display
, win
->id
);
6508 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
6509 DNPRINTF(SWM_D_EVENT
,
6510 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
6511 if (win
->floating
) {
6512 if (ev
->data
.l
[0] & (1<<8)) /* x */
6513 X(win
) = ev
->data
.l
[1];
6514 if (ev
->data
.l
[0] & (1<<9)) /* y */
6515 Y(win
) = ev
->data
.l
[2];
6516 if (ev
->data
.l
[0] & (1<<10)) /* width */
6517 WIDTH(win
) = ev
->data
.l
[3];
6518 if (ev
->data
.l
[0] & (1<<11)) /* height */
6519 HEIGHT(win
) = ev
->data
.l
[4];
6524 /* TODO: Change stack sizes */
6525 /* notify no change was made. */
6526 config_win(win
, NULL
);
6529 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
6530 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
6531 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
6533 ewmh_update_win_state(win
, ev
->data
.l
[2],
6541 xerror_start(Display
*d
, XErrorEvent
*ee
)
6548 xerror(Display
*d
, XErrorEvent
*ee
)
6550 /* warnx("error: %p %p", display, ee); */
6558 xerrorxlib
= XSetErrorHandler(xerror_start
);
6560 /* this causes an error if some other window manager is running */
6561 XSelectInput(display
, DefaultRootWindow(display
),
6562 SubstructureRedirectMask
);
6563 XSync(display
, False
);
6567 XSetErrorHandler(xerror
);
6568 XSync(display
, False
);
6573 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
6575 struct swm_region
*r
, *n
;
6576 struct workspace
*ws
= NULL
;
6579 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
6580 s
->idx
, w
, h
, x
, y
);
6582 /* remove any conflicting regions */
6583 n
= TAILQ_FIRST(&s
->rl
);
6586 n
= TAILQ_NEXT(r
, entry
);
6587 if (X(r
) < (x
+ w
) &&
6588 (X(r
) + WIDTH(r
)) > x
&&
6590 (Y(r
) + HEIGHT(r
)) > y
) {
6591 if (r
->ws
->r
!= NULL
)
6592 r
->ws
->old_r
= r
->ws
->r
;
6594 XDestroyWindow(display
, r
->bar_window
);
6595 TAILQ_REMOVE(&s
->rl
, r
, entry
);
6596 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
6600 /* search old regions for one to reuse */
6602 /* size + location match */
6603 TAILQ_FOREACH(r
, &s
->orl
, entry
)
6604 if (X(r
) == x
&& Y(r
) == y
&&
6605 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
6609 TAILQ_FOREACH(r
, &s
->orl
, entry
)
6610 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
6614 TAILQ_REMOVE(&s
->orl
, r
, entry
);
6615 /* try to use old region's workspace */
6616 if (r
->ws
->r
== NULL
)
6619 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
6620 err(1, "new_region: calloc: failed to allocate memory "
6623 /* if we don't have a workspace already, find one */
6625 for (i
= 0; i
< SWM_WS_MAX
; i
++)
6626 if (s
->ws
[i
].r
== NULL
) {
6633 errx(1, "new_region: no free workspaces");
6644 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
6650 #ifdef SWM_XRR_HAS_CRTC
6652 XRRScreenResources
*sr
;
6655 #endif /* SWM_XRR_HAS_CRTC */
6656 struct swm_region
*r
;
6659 if (i
>= ScreenCount(display
))
6660 errx(1, "scan_xrandr: invalid screen");
6662 /* remove any old regions */
6663 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
6664 r
->ws
->old_r
= r
->ws
->r
= NULL
;
6665 XDestroyWindow(display
, r
->bar_window
);
6666 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
6667 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
6671 /* map virtual screens onto physical screens */
6672 #ifdef SWM_XRR_HAS_CRTC
6673 if (xrandr_support
) {
6674 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
6676 new_region(&screens
[i
], 0, 0,
6677 DisplayWidth(display
, i
),
6678 DisplayHeight(display
, i
));
6682 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
6683 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
6684 if (ci
->noutput
== 0)
6687 if (ci
!= NULL
&& ci
->mode
== None
)
6688 new_region(&screens
[i
], 0, 0,
6689 DisplayWidth(display
, i
),
6690 DisplayHeight(display
, i
));
6692 new_region(&screens
[i
],
6693 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
6696 XRRFreeCrtcInfo(ci
);
6697 XRRFreeScreenResources(sr
);
6699 #endif /* SWM_XRR_HAS_CRTC */
6701 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
6702 DisplayHeight(display
, i
));
6707 screenchange(XEvent
*e
) {
6708 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
6709 struct swm_region
*r
;
6712 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%lx\n", xe
->root
);
6714 if (!XRRUpdateConfiguration(e
))
6717 /* silly event doesn't include the screen index */
6718 for (i
= 0; i
< ScreenCount(display
); i
++)
6719 if (screens
[i
].root
== xe
->root
)
6721 if (i
>= ScreenCount(display
))
6722 errx(1, "screenchange: screen not found");
6724 /* brute force for now, just re-enumerate the regions */
6727 /* add bars to all regions */
6728 for (i
= 0; i
< ScreenCount(display
); i
++)
6729 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6732 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6733 drain_enter_notify();
6739 Window d1
, d2
, *wins
= NULL
;
6740 XWindowAttributes wa
;
6745 for (i
= 0; i
< ScreenCount(display
); i
++) {
6746 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
6749 /* attach windows to a region */
6750 /* normal windows */
6751 for (j
= 0; j
< no
; j
++) {
6752 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6753 wa
.override_redirect
||
6754 XGetTransientForHint(display
, wins
[j
], &d1
))
6757 state
= getstate(wins
[j
]);
6758 manage
= state
== IconicState
;
6759 if (wa
.map_state
== IsViewable
|| manage
)
6760 manage_window(wins
[j
]);
6762 /* transient windows */
6763 for (j
= 0; j
< no
; j
++) {
6764 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6765 wa
.override_redirect
)
6768 state
= getstate(wins
[j
]);
6769 manage
= state
== IconicState
;
6770 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
6772 manage_window(wins
[j
]);
6785 int errorbase
, major
, minor
;
6786 struct workspace
*ws
;
6789 if ((screens
= calloc(ScreenCount(display
),
6790 sizeof(struct swm_screen
))) == NULL
)
6791 err(1, "setup_screens: calloc: failed to allocate memory for "
6794 /* initial Xrandr setup */
6795 xrandr_support
= XRRQueryExtension(display
,
6796 &xrandr_eventbase
, &errorbase
);
6798 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
6801 /* map physical screens */
6802 for (i
= 0; i
< ScreenCount(display
); i
++) {
6803 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
6805 TAILQ_INIT(&screens
[i
].rl
);
6806 TAILQ_INIT(&screens
[i
].orl
);
6807 screens
[i
].root
= RootWindow(display
, i
);
6809 /* set default colors */
6810 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
6811 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
6812 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
6813 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
6814 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
6816 /* create graphics context on screen with default font color */
6817 screens
[i
].bar_gc
= XCreateGC(display
, screens
[i
].root
, 0,
6820 XSetForeground(display
, screens
[i
].bar_gc
,
6821 screens
[i
].c
[SWM_S_COLOR_BAR_FONT
].color
);
6823 /* set default cursor */
6824 XDefineCursor(display
, screens
[i
].root
,
6825 XCreateFontCursor(display
, XC_left_ptr
));
6827 /* init all workspaces */
6828 /* XXX these should be dynamically allocated too */
6829 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
6830 ws
= &screens
[i
].ws
[j
];
6836 TAILQ_INIT(&ws
->winlist
);
6837 TAILQ_INIT(&ws
->unmanagedlist
);
6839 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
6840 if (layouts
[k
].l_config
!= NULL
)
6841 layouts
[k
].l_config(ws
,
6842 SWM_ARG_ID_STACKINIT
);
6843 ws
->cur_layout
= &layouts
[0];
6844 ws
->cur_layout
->l_string(ws
);
6850 XRRSelectInput(display
, screens
[i
].root
,
6851 RRScreenChangeNotifyMask
);
6858 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
6859 err(1, "setup_globals: strdup: failed to allocate memory.");
6861 if ((spawn_term
[0] = strdup("xterm")) == NULL
)
6862 err(1, "setup_globals: strdup: failed to allocate memory.");
6864 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
6865 err(1, "setup_globals: strdup: failed to allocate memory.");
6872 Atom netwmcheck
, netwmname
, utf8_string
;
6875 /* work around sun jdk bugs, code from wmname */
6876 netwmcheck
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
6877 netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
6878 utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6879 for (i
= 0; i
< ScreenCount(display
); i
++) {
6880 root
= screens
[i
].root
;
6881 win
= XCreateSimpleWindow(display
,root
, 0, 0, 1, 1, 0,
6882 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
,
6883 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
);
6885 XChangeProperty(display
, root
, netwmcheck
, XA_WINDOW
, 32,
6886 PropModeReplace
, (unsigned char *)&win
,1);
6887 XChangeProperty(display
, win
, netwmcheck
, XA_WINDOW
, 32,
6888 PropModeReplace
, (unsigned char *)&win
,1);
6889 XChangeProperty(display
, win
, netwmname
, utf8_string
, 8,
6890 PropModeReplace
, (unsigned char*)"LG3D", strlen("LG3D"));
6895 main(int argc
, char *argv
[])
6897 struct swm_region
*r
, *rr
;
6898 struct ws_win
*winfocus
= NULL
;
6901 char conf
[PATH_MAX
], *cfile
= NULL
;
6906 struct sigaction sact
;
6909 warnx("Welcome to scrotwm V%s Build: %s", SCROTWM_VERSION
, buildstr
);
6910 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
6912 warnx("no locale support");
6914 if (!X_HAVE_UTF8_STRING
)
6915 warnx("no UTF-8 support");
6917 if (!(display
= XOpenDisplay(0)))
6918 errx(1, "can not open display");
6921 errx(1, "other wm running");
6923 /* handle some signals */
6924 bzero(&sact
, sizeof(sact
));
6925 sigemptyset(&sact
.sa_mask
);
6927 sact
.sa_handler
= sighdlr
;
6928 sigaction(SIGINT
, &sact
, NULL
);
6929 sigaction(SIGQUIT
, &sact
, NULL
);
6930 sigaction(SIGTERM
, &sact
, NULL
);
6931 sigaction(SIGHUP
, &sact
, NULL
);
6933 sact
.sa_handler
= sighdlr
;
6934 sact
.sa_flags
= SA_NOCLDSTOP
;
6935 sigaction(SIGCHLD
, &sact
, NULL
);
6937 astate
= XInternAtom(display
, "WM_STATE", False
);
6938 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
6939 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
6940 takefocus
= XInternAtom(display
, "WM_TAKE_FOCUS", False
);
6941 a_wmname
= XInternAtom(display
, "WM_NAME", False
);
6942 a_netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
6943 a_utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6944 a_string
= XInternAtom(display
, "STRING", False
);
6945 a_swm_iconic
= XInternAtom(display
, "_SWM_ICONIC", False
);
6947 /* look for local and global conf file */
6948 pwd
= getpwuid(getuid());
6950 errx(1, "invalid user: %d", getuid());
6959 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
6960 if (stat(conf
, &sb
) != -1) {
6961 if (S_ISREG(sb
.st_mode
))
6964 /* try global conf file */
6965 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
6966 if (!stat(conf
, &sb
))
6967 if (S_ISREG(sb
.st_mode
))
6971 /* load conf (if any) and refresh font color in bar graphics contexts */
6972 if (cfile
&& conf_load(cfile
, SWM_CONF_DEFAULT
) == 0)
6973 for (i
= 0; i
< ScreenCount(display
); ++i
)
6974 XSetForeground(display
, screens
[i
].bar_gc
,
6975 screens
[i
].c
[SWM_S_COLOR_BAR_FONT
].color
);
6978 /* set some values to work around bad programs */
6980 /* grab existing windows (before we build the bars) */
6983 if (getenv("SWM_STARTED") == NULL
)
6984 setenv("SWM_STARTED", "YES", 1);
6986 /* setup all bars */
6987 for (i
= 0; i
< ScreenCount(display
); i
++)
6988 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
6989 if (winfocus
== NULL
)
6990 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
6998 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6999 drain_enter_notify();
7001 xfd
= ConnectionNumber(display
);
7003 while (XPending(display
)) {
7004 XNextEvent(display
, &e
);
7007 if (e
.type
< LASTEvent
) {
7008 DNPRINTF(SWM_D_EVENTQ
,"XEvent: handled: %s, "
7009 "window: 0x%lx, type: %s (%d), %d remaining"
7010 "\n", YESNO(handler
[e
.type
]),
7011 e
.xany
.window
, geteventname(&e
),
7012 e
.type
, QLength(display
));
7014 if (handler
[e
.type
])
7015 handler
[e
.type
](&e
);
7017 DNPRINTF(SWM_D_EVENTQ
, "XRandr Event: window: "
7018 "0x%lx, type: %s (%d)\n", e
.xany
.window
,
7019 xrandr_geteventname(&e
), e
.type
);
7021 switch (e
.type
- xrandr_eventbase
) {
7022 case RRScreenChangeNotify
:
7031 /* if we are being restarted go focus on first window */
7033 rr
= winfocus
->ws
->r
;
7035 /* not a visible window */
7039 /* move pointer to first screen if multi screen */
7040 if (ScreenCount(display
) > 1 || outputs
> 1)
7041 XWarpPointer(display
, None
, rr
->s
[0].root
,
7043 Y(rr
) + (bar_enabled
? bar_height
: 0));
7045 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7055 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7057 DNPRINTF(SWM_D_MISC
, "select failed");
7058 if (restart_wm
== 1)
7059 restart(NULL
, NULL
);
7060 if (search_resp
== 1)
7073 for (i
= 0; i
< ScreenCount(display
); ++i
)
7074 if (screens
[i
].bar_gc
!= NULL
)
7075 XFreeGC(display
, screens
[i
].bar_gc
);
7077 XFreeFontSet(display
, bar_fs
);
7078 XCloseDisplay(display
);