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(Window
);
569 get_property(Window id
, Atom atom
, long count
, Atom type
, unsigned long *nitems
,
570 unsigned long *nbytes
, unsigned char **data
)
573 unsigned long *nbytes_ret
, *nitems_ret
;
574 unsigned long nbytes_tmp
, nitems_tmp
;
577 nbytes_ret
= nbytes
!= NULL
? nbytes
: &nbytes_tmp
;
578 nitems_ret
= nitems
!= NULL
? nitems
: &nitems_tmp
;
580 status
= XGetWindowProperty(display
, id
, atom
, 0L, count
, False
, type
,
581 &real
, &format
, nitems_ret
, nbytes_ret
, data
);
583 if (status
!= Success
)
592 update_iconic(struct ws_win
*win
, int newv
)
599 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
603 XChangeProperty(display
, win
->id
, iprop
, XA_INTEGER
, 32,
604 PropModeReplace
, (unsigned char *)&v
, 1);
606 XDeleteProperty(display
, win
->id
, iprop
);
610 get_iconic(struct ws_win
*win
)
615 unsigned long nitems
, extra
;
616 unsigned char *prop
= NULL
;
618 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
621 status
= XGetWindowProperty(display
, win
->id
, iprop
, 0L, 1L,
622 False
, XA_INTEGER
, &rettype
, &retfmt
, &nitems
, &extra
, &prop
);
623 if (status
!= Success
)
625 if (rettype
!= XA_INTEGER
|| retfmt
!= 32)
629 v
= *((int32_t *)prop
);
643 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
645 for (i
= 0; i
< LENGTH(ewmh
); i
++)
646 ewmh
[i
].atom
= XInternAtom(display
, ewmh
[i
].name
, False
);
648 for (i
= 0; i
< ScreenCount(display
); i
++) {
649 /* Support check window will be created by workaround(). */
651 /* Report supported atoms */
652 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
653 for (j
= 0; j
< LENGTH(ewmh
); j
++)
654 XChangeProperty(display
, screens
[i
].root
,
655 sup_list
, XA_ATOM
, 32,
656 PropModeAppend
, (unsigned char *)&ewmh
[j
].atom
,1);
664 unsigned char *data
= NULL
;
666 Atom sup_check
, sup_list
;
669 sup_check
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
670 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
672 for (i
= 0; i
< ScreenCount(display
); i
++) {
673 /* Get the support check window and destroy it */
674 success
= get_property(screens
[i
].root
, sup_check
, 1, XA_WINDOW
,
679 XDestroyWindow(display
, id
);
680 XDeleteProperty(display
, screens
[i
].root
, sup_check
);
681 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
689 ewmh_autoquirk(struct ws_win
*win
)
692 unsigned long *data
= NULL
, n
;
695 success
= get_property(win
->id
, ewmh
[_NET_WM_WINDOW_TYPE
].atom
, (~0L),
696 XA_ATOM
, &n
, NULL
, (void *)&data
);
703 for (i
= 0; i
< n
; i
++) {
705 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
707 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
708 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
709 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
711 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
714 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
715 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
717 win
->quirks
= SWM_Q_FLOAT
;
725 #define SWM_EWMH_ACTION_COUNT_MAX (6)
726 #define EWMH_F_FULLSCREEN (1<<0)
727 #define EWMH_F_ABOVE (1<<1)
728 #define EWMH_F_HIDDEN (1<<2)
729 #define EWMH_F_SKIP_PAGER (1<<3)
730 #define EWMH_F_SKIP_TASKBAR (1<<4)
731 #define SWM_F_MANUAL (1<<5)
734 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
736 struct swm_geometry rg
;
744 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%lx, "
745 "fullscreen %s\n", win
->id
, YESNO(fs
));
750 store_float_geom(win
, win
->ws
->r
);
754 if (win
->g_floatvalid
) {
755 /* refloat at last floating relative position */
756 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ rg
.x
;
757 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ rg
.y
;
758 WIDTH(win
) = win
->g_float
.w
;
759 HEIGHT(win
) = win
->g_float
.h
;
767 ewmh_update_actions(struct ws_win
*win
)
769 Atom actions
[SWM_EWMH_ACTION_COUNT_MAX
];
775 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
778 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
779 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
782 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
,
783 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)actions
, n
);
786 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
787 #define _NET_WM_STATE_ADD 1 /* add/set property */
788 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
791 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
793 unsigned int mask
= 0;
794 unsigned int changed
= 0;
795 unsigned int orig_flags
;
800 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
801 mask
= EWMH_F_FULLSCREEN
;
802 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
804 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
806 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
807 mask
= EWMH_F_SKIP_PAGER
;
808 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
809 mask
= EWMH_F_SKIP_TASKBAR
;
812 orig_flags
= win
->ewmh_flags
;
815 case _NET_WM_STATE_REMOVE
:
816 win
->ewmh_flags
&= ~mask
;
818 case _NET_WM_STATE_ADD
:
819 win
->ewmh_flags
|= mask
;
821 case _NET_WM_STATE_TOGGLE
:
822 win
->ewmh_flags
^= mask
;
826 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
828 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
830 if (!floating_toggle_win(win
))
831 win
->ewmh_flags
= orig_flags
; /* revert */
832 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
834 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
835 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
837 if (!ewmh_set_win_fullscreen(win
,
838 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
839 win
->ewmh_flags
= orig_flags
; /* revert */
841 XDeleteProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
843 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
844 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
845 XA_ATOM
, 32, PropModeAppend
,
846 (unsigned char *)&ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
, 1);
847 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
848 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
849 XA_ATOM
, 32, PropModeAppend
,
850 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
, 1);
851 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
852 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
853 XA_ATOM
, 32, PropModeAppend
,
854 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
, 1);
855 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
856 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
857 XA_ATOM
, 32, PropModeAppend
,
858 (unsigned char *)&ewmh
[_NET_WM_STATE_ABOVE
].atom
, 1);
859 if (win
->ewmh_flags
& SWM_F_MANUAL
)
860 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
861 XA_ATOM
, 32, PropModeAppend
,
862 (unsigned char *)&ewmh
[_SWM_WM_STATE_MANUAL
].atom
, 1);
866 ewmh_get_win_state(struct ws_win
*win
)
877 win
->ewmh_flags
|= EWMH_F_ABOVE
;
879 win
->ewmh_flags
|= SWM_F_MANUAL
;
881 success
= get_property(win
->id
, ewmh
[_NET_WM_STATE
].atom
,
882 (~0L), XA_ATOM
, &n
, NULL
, (void *)&states
);
887 for (i
= 0; i
< n
; i
++)
888 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
896 geteventname(XEvent
*e
)
908 name
= "ButtonPress";
911 name
= "ButtonRelease";
914 name
= "MotionNotify";
917 name
= "EnterNotify";
920 name
= "LeaveNotify";
929 name
= "KeymapNotify";
935 name
= "GraphicsExpose";
940 case VisibilityNotify
:
941 name
= "VisibilityNotify";
944 name
= "CreateNotify";
947 name
= "DestroyNotify";
950 name
= "UnmapNotify";
959 name
= "ReparentNotify";
961 case ConfigureNotify
:
962 name
= "ConfigureNotify";
964 case ConfigureRequest
:
965 name
= "ConfigureRequest";
968 name
= "GravityNotify";
971 name
= "ResizeRequest";
973 case CirculateNotify
:
974 name
= "CirculateNotify";
976 case CirculateRequest
:
977 name
= "CirculateRequest";
980 name
= "PropertyNotify";
983 name
= "SelectionClear";
985 case SelectionRequest
:
986 name
= "SelectionRequest";
988 case SelectionNotify
:
989 name
= "SelectionNotify";
992 name
= "ColormapNotify";
995 name
= "ClientMessage";
998 name
= "MappingNotify";
1008 xrandr_geteventname(XEvent
*e
)
1012 switch(e
->type
- xrandr_eventbase
) {
1013 case RRScreenChangeNotify
:
1014 name
= "RRScreenChangeNotify";
1024 dumpwins(struct swm_region
*r
, union arg
*args
)
1028 XWindowAttributes wa
;
1030 if (r
->ws
== NULL
) {
1031 warnx("dumpwins: invalid workspace");
1035 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1037 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1038 state
= getstate(win
->id
);
1039 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1040 warnx("window: 0x%lx, failed XGetWindowAttributes",
1042 warnx("window: 0x%lx, map_state: %d, state: %d, "
1043 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1047 warnx("===== unmanaged window list =====");
1048 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1049 state
= getstate(win
->id
);
1050 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1051 warnx("window: 0x%lx, failed XGetWindowAttributes",
1053 warnx("window: 0x%lx, map_state: %d, state: %d, "
1054 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1058 warnx("=================================");
1062 dumpwins(struct swm_region
*r
, union arg
*args
)
1065 #endif /* SWM_DEBUG */
1067 void expose(XEvent
*);
1068 void keypress(XEvent
*);
1069 void buttonpress(XEvent
*);
1070 void configurerequest(XEvent
*);
1071 void configurenotify(XEvent
*);
1072 void destroynotify(XEvent
*);
1073 void enternotify(XEvent
*);
1074 void focusevent(XEvent
*);
1075 void mapnotify(XEvent
*);
1076 void mappingnotify(XEvent
*);
1077 void maprequest(XEvent
*);
1078 void propertynotify(XEvent
*);
1079 void unmapnotify(XEvent
*);
1080 void visibilitynotify(XEvent
*);
1081 void clientmessage(XEvent
*);
1083 void (*handler
[LASTEvent
])(XEvent
*) = {
1085 [KeyPress
] = keypress
,
1086 [ButtonPress
] = buttonpress
,
1087 [ConfigureRequest
] = configurerequest
,
1088 [ConfigureNotify
] = configurenotify
,
1089 [DestroyNotify
] = destroynotify
,
1090 [EnterNotify
] = enternotify
,
1091 [FocusIn
] = focusevent
,
1092 [FocusOut
] = focusevent
,
1093 [MapNotify
] = mapnotify
,
1094 [MappingNotify
] = mappingnotify
,
1095 [MapRequest
] = maprequest
,
1096 [PropertyNotify
] = propertynotify
,
1097 [UnmapNotify
] = unmapnotify
,
1098 [VisibilityNotify
] = visibilitynotify
,
1099 [ClientMessage
] = clientmessage
,
1105 int saved_errno
, status
;
1108 saved_errno
= errno
;
1112 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1117 if (errno
!= ECHILD
)
1118 warn("sighdlr: waitpid");
1119 #endif /* SWM_DEBUG */
1122 if (pid
== searchpid
)
1126 if (WIFEXITED(status
)) {
1127 if (WEXITSTATUS(status
) != 0)
1128 warnx("sighdlr: child exit status: %d",
1129 WEXITSTATUS(status
));
1131 warnx("sighdlr: child is terminated "
1133 #endif /* SWM_DEBUG */
1147 errno
= saved_errno
;
1153 struct pid_e
*p
= NULL
;
1155 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1160 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1169 name_to_color(char *colorname
)
1173 XColor screen_def
, exact_def
;
1174 unsigned long result
= 0;
1175 char cname
[32] = "#";
1177 cmap
= DefaultColormap(display
, screens
[0].idx
);
1178 status
= XAllocNamedColor(display
, cmap
, colorname
,
1179 &screen_def
, &exact_def
);
1181 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1182 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
1186 result
= screen_def
.pixel
;
1188 warnx("color '%s' not found", colorname
);
1194 setscreencolor(char *val
, int i
, int c
)
1196 if (i
> 0 && i
<= ScreenCount(display
)) {
1197 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1198 free(screens
[i
- 1].c
[c
].name
);
1199 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1201 } else if (i
== -1) {
1202 for (i
= 0; i
< ScreenCount(display
); i
++) {
1203 screens
[i
].c
[c
].color
= name_to_color(val
);
1204 free(screens
[i
].c
[c
].name
);
1205 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1209 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1210 i
, ScreenCount(display
));
1214 fancy_stacker(struct workspace
*ws
)
1216 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1217 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1218 snprintf(ws
->stacker
, sizeof ws
->stacker
, "[%d|%d]",
1219 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1220 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1221 snprintf(ws
->stacker
, sizeof ws
->stacker
, "[%d-%d]",
1222 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1226 plain_stacker(struct workspace
*ws
)
1228 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1229 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1230 strlcpy(ws
->stacker
, "[|]", sizeof ws
->stacker
);
1231 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1232 strlcpy(ws
->stacker
, "[-]", sizeof ws
->stacker
);
1236 custom_region(char *val
)
1238 unsigned int sidx
, x
, y
, w
, h
;
1240 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1241 errx(1, "invalid custom region, "
1242 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1243 if (sidx
< 1 || sidx
> ScreenCount(display
))
1244 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1245 sidx
, ScreenCount(display
));
1249 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1251 if (x
> DisplayWidth(display
, sidx
) ||
1252 y
> DisplayHeight(display
, sidx
) ||
1253 w
+ x
> DisplayWidth(display
, sidx
) ||
1254 h
+ y
> DisplayHeight(display
, sidx
)) {
1255 warnx("ignoring region %ux%u+%u+%u - not within screen "
1256 "boundaries (%ux%u)", w
, h
, x
, y
,
1257 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
1261 new_region(&screens
[sidx
], x
, y
, w
, h
);
1265 socket_setnonblock(int fd
)
1269 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1270 err(1, "fcntl F_GETFL");
1271 flags
|= O_NONBLOCK
;
1272 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1273 err(1, "fcntl F_SETFL");
1277 bar_print(struct swm_region
*r
, char *s
)
1281 XRectangle ibox
, lbox
;
1283 XClearWindow(display
, r
->bar_window
);
1286 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1288 switch (bar_justify
) {
1289 case SWM_BAR_JUSTIFY_LEFT
:
1292 case SWM_BAR_JUSTIFY_CENTER
:
1293 x
= (WIDTH(r
) - lbox
.width
) / 2;
1295 case SWM_BAR_JUSTIFY_RIGHT
:
1296 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1300 if (x
< SWM_BAR_OFFSET
)
1303 DRAWSTRING(display
, r
->bar_window
, bar_fs
, r
->s
->bar_gc
,
1304 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1309 bar_extra_stop(void)
1313 bzero(bar_pipe
, sizeof bar_pipe
);
1316 kill(bar_pid
, SIGTERM
);
1319 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1324 bar_class_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1326 int do_class
, do_name
;
1328 XClassHint
*xch
= NULL
;
1330 if ((title_name_enabled
== 1 || title_class_enabled
== 1) &&
1331 cur_focus
!= NULL
) {
1332 if ((xch
= XAllocClassHint()) == NULL
)
1334 status
= XGetClassHint(display
, cur_focus
->id
, xch
);
1335 if (status
== BadWindow
|| status
== BadAlloc
)
1337 do_class
= (title_class_enabled
&& xch
->res_class
!= NULL
);
1338 do_name
= (title_name_enabled
&& xch
->res_name
!= NULL
);
1340 strlcat(s
, xch
->res_class
, sz
);
1341 if (do_class
&& do_name
)
1342 strlcat(s
, ":", sz
);
1344 strlcat(s
, xch
->res_name
, sz
);
1345 strlcat(s
, " ", sz
);
1349 XFree(xch
->res_name
);
1350 XFree(xch
->res_class
);
1356 bar_window_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1358 unsigned char *title
;
1360 if (window_name_enabled
&& cur_focus
!= NULL
) {
1361 title
= get_win_name(cur_focus
->id
);
1362 if (title
!= NULL
) {
1363 DNPRINTF(SWM_D_BAR
, "bar_window_name: title: %s\n",
1366 if (cur_focus
->floating
)
1367 strlcat(s
, "(f) ", sz
);
1368 strlcat(s
, (char *)title
, sz
);
1369 strlcat(s
, " ", sz
);
1375 int urgent
[SWM_WS_MAX
];
1377 bar_urgent(char *s
, ssize_t sz
)
1379 XWMHints
*wmh
= NULL
;
1384 if (urgent_enabled
== 0)
1387 for (i
= 0; i
< SWM_WS_MAX
; i
++)
1390 for (i
= 0; i
< ScreenCount(display
); i
++)
1391 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1392 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1393 wmh
= XGetWMHints(display
, win
->id
);
1397 if (wmh
->flags
& XUrgencyHint
)
1402 strlcat(s
, "* ", sz
);
1403 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
1405 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1407 snprintf(b
, sizeof b
, "- ");
1410 strlcat(s
, "* ", sz
);
1418 struct swm_region
*r
;
1421 char ws
[SWM_BAR_MAX
];
1422 char s
[SWM_BAR_MAX
];
1423 unsigned char cn
[SWM_BAR_MAX
];
1424 char loc
[SWM_BAR_MAX
];
1425 char *b
, *stack
= "";
1427 if (bar_enabled
== 0)
1429 if (bar_extra
&& bar_extra_running
) {
1430 /* ignore short reads; it'll correct itself */
1431 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1432 if (b
&& b
[len
- 1] == '\n') {
1434 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1436 if (b
== NULL
&& errno
!= EAGAIN
) {
1437 warn("bar_update: bar_extra failed");
1441 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1443 if (clock_enabled
== 0)
1444 strlcpy(s
, "", sizeof s
);
1447 localtime_r(&tmt
, &tm
);
1448 len
= strftime(s
, sizeof s
, clock_format
, &tm
);
1450 strlcat(s
, " ", sizeof s
);
1453 for (i
= 0; i
< ScreenCount(display
); i
++) {
1455 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1456 strlcpy((char *)cn
, "", sizeof cn
);
1457 strlcpy(ws
, "", sizeof ws
);
1459 bar_urgent((char *)cn
, sizeof cn
);
1460 bar_class_name((char *)cn
, sizeof cn
,
1462 bar_window_name((char *)cn
, sizeof cn
,
1465 snprintf(ws
, sizeof ws
, "<%s>",
1469 stack
= r
->ws
->stacker
;
1471 snprintf(loc
, sizeof loc
, "%d:%d %s %s %s%s %s "
1472 "%s", x
++, r
->ws
->idx
+ 1, stack
, ws
, s
, cn
,
1473 bar_ext
, bar_vertext
);
1487 bar_toggle(struct swm_region
*r
, union arg
*args
)
1489 struct swm_region
*tmpr
;
1490 int i
, sc
= ScreenCount(display
);
1492 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1495 for (i
= 0; i
< sc
; i
++)
1496 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1497 XUnmapWindow(display
, tmpr
->bar_window
);
1499 for (i
= 0; i
< sc
; i
++)
1500 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1501 XMapRaised(display
, tmpr
->bar_window
);
1503 bar_enabled
= !bar_enabled
;
1506 /* must be after stack */
1513 XSetWindowAttributes wa
;
1514 struct swm_region
*r
;
1517 /* do this here because the conf file is in memory */
1518 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1519 /* launch external status app */
1520 bar_extra_running
= 1;
1521 if (pipe(bar_pipe
) == -1)
1522 err(1, "pipe error");
1523 socket_setnonblock(bar_pipe
[0]);
1524 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1525 if (dup2(bar_pipe
[0], 0) == -1)
1527 if (dup2(bar_pipe
[1], 1) == -1)
1529 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1530 err(1, "could not disable SIGPIPE");
1531 switch (bar_pid
= fork()) {
1533 err(1, "cannot fork");
1537 execvp(bar_argv
[0], bar_argv
);
1538 err(1, "%s external app failed", bar_argv
[0]);
1540 default: /* parent */
1546 bzero(&wa
, sizeof wa
);
1547 for (i
= 0; i
< ScreenCount(display
); i
++)
1548 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1550 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1551 wa
.background_pixel
=
1552 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1553 XChangeWindowAttributes(display
, r
->bar_window
,
1554 CWBackPixel
| CWBorderPixel
, &wa
);
1560 bar_setup(struct swm_region
*r
)
1562 char *default_string
;
1563 char **missing_charsets
;
1564 int num_missing_charsets
= 0;
1568 XFreeFontSet(display
, bar_fs
);
1573 DNPRINTF(SWM_D_BAR
, "bar_setup: loading bar_fonts: %s\n", bar_fonts
);
1575 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
1576 &num_missing_charsets
, &default_string
);
1578 if (num_missing_charsets
> 0) {
1579 warnx("Unable to load charset(s):");
1581 for (i
= 0; i
< num_missing_charsets
; ++i
)
1582 warnx("%s", missing_charsets
[i
]);
1584 XFreeStringList(missing_charsets
);
1586 if (strcmp(default_string
, ""))
1587 warnx("Glyphs from those sets will be replaced "
1588 "by '%s'.", default_string
);
1590 warnx("Glyphs from those sets won't be drawn.");
1594 errx(1, "Error creating font set structure.");
1596 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
1598 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
1599 2 * bar_border_width
;
1605 y
= bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1607 r
->bar_window
= XCreateSimpleWindow(display
,
1608 r
->s
->root
, x
, y
, WIDTH(r
) - 2 * bar_border_width
,
1609 bar_height
- 2 * bar_border_width
,
1610 bar_border_width
, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
1611 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1612 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
1614 XMapRaised(display
, r
->bar_window
);
1615 DNPRINTF(SWM_D_BAR
, "bar_setup: bar_window: 0x%lx\n", r
->bar_window
);
1617 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1618 err(1, "could not install bar_signal");
1623 drain_enter_notify(void)
1628 while (XCheckMaskEvent(display
, EnterWindowMask
, &cne
))
1631 DNPRINTF(SWM_D_MISC
, "drain_enter_notify: drained: %d\n", i
);
1635 set_win_state(struct ws_win
*win
, long state
)
1637 long data
[] = {state
, None
};
1639 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%lx\n", win
->id
);
1644 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1645 (unsigned char *)data
, 2);
1652 unsigned char *p
= NULL
;
1655 if (!get_property(w
, astate
, 2L, astate
, &n
, NULL
, &p
))
1658 result
= *((long *)p
);
1664 version(struct swm_region
*r
, union arg
*args
)
1666 bar_version
= !bar_version
;
1668 snprintf(bar_vertext
, sizeof bar_vertext
,
1669 "Version: %s Build: %s", SCROTWM_VERSION
, buildstr
);
1671 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
1676 client_msg(struct ws_win
*win
, Atom a
)
1678 XClientMessageEvent cm
;
1683 bzero(&cm
, sizeof cm
);
1684 cm
.type
= ClientMessage
;
1685 cm
.window
= win
->id
;
1686 cm
.message_type
= aprot
;
1689 cm
.data
.l
[1] = CurrentTime
;
1690 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
1693 /* synthetic response to a ConfigureRequest when not making a change */
1695 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
1702 /* send notification of unchanged state. */
1703 ce
.type
= ConfigureNotify
;
1706 ce
.width
= WIDTH(win
);
1707 ce
.height
= HEIGHT(win
);
1708 ce
.override_redirect
= False
;
1712 ce
.display
= display
;
1714 ce
.window
= win
->id
;
1715 ce
.border_width
= border_width
;
1719 ce
.display
= ev
->display
;
1720 ce
.event
= ev
->window
;
1721 ce
.window
= ev
->window
;
1723 /* make response appear more WM_SIZE_HINTS-compliant */
1725 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%lx,"
1726 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
1727 "%d x %d\n", win
->id
, win
->sh_mask
, SH_MIN_W(win
),
1728 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
1729 SH_INC_W(win
), SH_INC_H(win
));
1733 /* the hint may be set... to 0! */
1734 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
1735 ce
.width
= SH_MIN_W(win
);
1736 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
1737 ce
.height
= SH_MIN_H(win
);
1742 /* may also be advertized as 0 */
1743 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
1744 ce
.width
= SH_MAX_W(win
);
1745 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
1746 ce
.height
= SH_MAX_H(win
);
1749 /* resize increment. */
1751 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
1752 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
1754 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
1755 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
1759 /* adjust x and y for requested border_width. */
1760 ce
.x
+= border_width
- ev
->border_width
;
1761 ce
.y
+= border_width
- ev
->border_width
;
1762 ce
.border_width
= ev
->border_width
;
1763 ce
.above
= ev
->above
;
1766 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
1767 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
1768 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
1770 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
1774 count_win(struct workspace
*ws
, int count_transient
)
1779 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1780 if (count_transient
== 0 && win
->floating
)
1782 if (count_transient
== 0 && win
->transient
)
1788 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
1794 quit(struct swm_region
*r
, union arg
*args
)
1796 DNPRINTF(SWM_D_MISC
, "quit\n");
1801 unmap_window(struct ws_win
*win
)
1806 /* don't unmap again */
1807 if (getstate(win
->id
) == IconicState
)
1810 set_win_state(win
, IconicState
);
1812 XUnmapWindow(display
, win
->id
);
1813 XSetWindowBorder(display
, win
->id
,
1814 win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1823 for (i
= 0; i
< ScreenCount(display
); i
++)
1824 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1825 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1830 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
1837 event
.display
= display
; /* Ignored, but what the hell */
1838 event
.window
= win
->id
;
1839 event
.root
= win
->s
->root
;
1840 event
.subwindow
= None
;
1841 event
.time
= CurrentTime
;
1846 event
.same_screen
= True
;
1847 event
.keycode
= XKeysymToKeycode(display
, keysym
);
1848 event
.state
= modifiers
;
1850 event
.type
= KeyPress
;
1851 XSendEvent(event
.display
, event
.window
, True
,
1852 KeyPressMask
, (XEvent
*)&event
);
1854 event
.type
= KeyRelease
;
1855 XSendEvent(event
.display
, event
.window
, True
,
1856 KeyPressMask
, (XEvent
*)&event
);
1861 restart(struct swm_region
*r
, union arg
*args
)
1863 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
1865 /* disable alarm because the following code may not be interrupted */
1867 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
1868 err(1, "can't disable alarm");
1873 XCloseDisplay(display
);
1874 execvp(start_argv
[0], start_argv
);
1875 warn("execvp failed");
1880 root_to_region(Window root
)
1882 struct swm_region
*r
= NULL
;
1884 int i
, x
, y
, wx
, wy
;
1887 for (i
= 0; i
< ScreenCount(display
); i
++)
1888 if (screens
[i
].root
== root
)
1891 if (XQueryPointer(display
, screens
[i
].root
,
1892 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
1893 /* choose a region based on pointer location */
1894 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1895 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
1896 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
1901 r
= TAILQ_FIRST(&screens
[i
].rl
);
1907 find_unmanaged_window(Window id
)
1912 for (i
= 0; i
< ScreenCount(display
); i
++)
1913 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1914 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
1922 find_window(Window id
)
1925 Window wrr
, wpr
, *wcr
= NULL
;
1929 for (i
= 0; i
< ScreenCount(display
); i
++)
1930 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1931 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1935 /* if we were looking for the parent return that window instead */
1936 if (XQueryTree(display
, id
, &wrr
, &wpr
, &wcr
, &nc
) == 0)
1941 /* ignore not found and root */
1942 if (wpr
== 0 || wrr
== wpr
)
1945 /* look for parent */
1946 for (i
= 0; i
< ScreenCount(display
); i
++)
1947 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1948 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1956 spawn(int ws_idx
, union arg
*args
, int close_fd
)
1961 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
1964 close(ConnectionNumber(display
));
1966 setenv("LD_PRELOAD", SWM_LIB
, 1);
1968 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
1969 warn("spawn: asprintf SWM_WS");
1972 setenv("_SWM_WS", ret
, 1);
1976 if (asprintf(&ret
, "%d", getpid()) == -1) {
1977 warn("spawn: asprintf _SWM_PID");
1980 setenv("_SWM_PID", ret
, 1);
1984 if (setsid() == -1) {
1985 warn("spawn: setsid");
1991 * close stdin and stdout to prevent interaction between apps
1992 * and the baraction script
1993 * leave stderr open to record errors
1995 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
1996 warn("spawn: open");
1999 dup2(fd
, STDIN_FILENO
);
2000 dup2(fd
, STDOUT_FILENO
);
2005 execvp(args
->argv
[0], args
->argv
);
2007 warn("spawn: execvp");
2012 spawnterm(struct swm_region
*r
, union arg
*args
)
2014 DNPRINTF(SWM_D_MISC
, "spawnterm\n");
2018 setenv("_SWM_XTERM_FONTADJ", "", 1);
2019 spawn(r
->ws
->idx
, args
, 1);
2024 kill_refs(struct ws_win
*win
)
2027 struct swm_region
*r
;
2028 struct workspace
*ws
;
2033 for (i
= 0; i
< ScreenCount(display
); i
++)
2034 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2035 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
2037 if (win
== ws
->focus
)
2039 if (win
== ws
->focus_prev
)
2040 ws
->focus_prev
= NULL
;
2045 validate_win(struct ws_win
*testwin
)
2048 struct workspace
*ws
;
2049 struct swm_region
*r
;
2052 if (testwin
== NULL
)
2055 for (i
= 0; i
< ScreenCount(display
); i
++)
2056 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2057 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
2059 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2067 validate_ws(struct workspace
*testws
)
2069 struct swm_region
*r
;
2070 struct workspace
*ws
;
2073 /* validate all ws */
2074 for (i
= 0; i
< ScreenCount(display
); i
++)
2075 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2076 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
2085 unfocus_win(struct ws_win
*win
)
2090 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%lx\n", WINID(win
));
2094 if (win
->ws
== NULL
)
2097 if (validate_ws(win
->ws
))
2098 return; /* XXX this gets hit with thunderbird, needs fixing */
2100 if (win
->ws
->r
== NULL
)
2103 if (validate_win(win
)) {
2108 if (win
->ws
->focus
== win
) {
2109 win
->ws
->focus
= NULL
;
2110 win
->ws
->focus_prev
= win
;
2113 if (validate_win(win
->ws
->focus
)) {
2114 kill_refs(win
->ws
->focus
);
2115 win
->ws
->focus
= NULL
;
2117 if (validate_win(win
->ws
->focus_prev
)) {
2118 kill_refs(win
->ws
->focus_prev
);
2119 win
->ws
->focus_prev
= NULL
;
2122 /* drain all previous unfocus events */
2123 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
2126 grabbuttons(win
, 0);
2127 XSetWindowBorder(display
, win
->id
,
2128 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2130 XChangeProperty(display
, win
->s
->root
,
2131 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2132 PropModeReplace
, (unsigned char *)&none
,1);
2141 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2143 for (i
= 0; i
< ScreenCount(display
); i
++)
2144 for (j
= 0; j
< SWM_WS_MAX
; j
++)
2145 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2150 focus_win(struct ws_win
*win
)
2155 struct ws_win
*cfw
= NULL
;
2158 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%lx\n", WINID(win
));
2162 if (win
->ws
== NULL
)
2165 if (validate_ws(win
->ws
))
2166 return; /* XXX this gets hit with thunderbird, needs fixing */
2168 if (validate_win(win
)) {
2173 if (validate_win(win
)) {
2178 XGetInputFocus(display
, &cur_focus
, &rr
);
2179 if ((cfw
= find_window(cur_focus
)) != NULL
)
2182 /* use larger hammer since the window was killed somehow */
2183 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2184 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2185 XSetWindowBorder(display
, cfw
->id
,
2186 cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2189 win
->ws
->focus
= win
;
2191 if (win
->ws
->r
!= NULL
) {
2192 /* drain all previous focus events */
2193 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
2197 XSetInputFocus(display
, win
->id
,
2198 RevertToParent
, CurrentTime
);
2199 grabbuttons(win
, 1);
2200 XSetWindowBorder(display
, win
->id
,
2201 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2202 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2203 win
->ws
->always_raise
)
2204 XMapRaised(display
, win
->id
);
2206 XChangeProperty(display
, win
->s
->root
,
2207 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2208 PropModeReplace
, (unsigned char *)&win
->id
,1);
2211 if (window_name_enabled
|| title_class_enabled
|| title_name_enabled
)
2216 switchws(struct swm_region
*r
, union arg
*args
)
2218 int wsid
= args
->id
, unmap_old
= 0;
2219 struct swm_region
*this_r
, *other_r
;
2221 struct workspace
*new_ws
, *old_ws
;
2228 old_ws
= this_r
->ws
;
2229 new_ws
= &this_r
->s
->ws
[wsid
];
2231 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2232 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2234 if (new_ws
== NULL
|| old_ws
== NULL
)
2236 if (new_ws
== old_ws
)
2239 other_r
= new_ws
->r
;
2240 if (other_r
== NULL
) {
2241 /* the other workspace is hidden, hide this one */
2245 /* the other ws is visible in another region, exchange them */
2246 other_r
->ws_prior
= new_ws
;
2247 other_r
->ws
= old_ws
;
2248 old_ws
->r
= other_r
;
2250 this_r
->ws_prior
= old_ws
;
2251 this_r
->ws
= new_ws
;
2254 /* this is needed so that we can click on a window after a restart */
2258 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2259 focus(new_ws
->r
, &a
);
2263 /* unmap old windows */
2265 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2268 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2269 drain_enter_notify();
2273 cyclews(struct swm_region
*r
, union arg
*args
)
2276 struct swm_screen
*s
= r
->s
;
2279 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2280 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2285 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2288 case SWM_ARG_ID_CYCLEWS_UP
:
2289 if (a
.id
< SWM_WS_MAX
- 1)
2294 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2297 case SWM_ARG_ID_CYCLEWS_DOWN
:
2301 a
.id
= SWM_WS_MAX
- 1;
2308 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2310 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2314 } while (a
.id
!= r
->ws
->idx
);
2318 priorws(struct swm_region
*r
, union arg
*args
)
2322 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2323 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2325 if (r
->ws_prior
== NULL
)
2328 a
.id
= r
->ws_prior
->idx
;
2333 cyclescr(struct swm_region
*r
, union arg
*args
)
2335 struct swm_region
*rr
= NULL
;
2339 /* do nothing if we don't have more than one screen */
2340 if (!(ScreenCount(display
) > 1 || outputs
> 1))
2345 case SWM_ARG_ID_CYCLESC_UP
:
2346 rr
= TAILQ_NEXT(r
, entry
);
2348 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2350 case SWM_ARG_ID_CYCLESC_DOWN
:
2351 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2353 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2361 /* move mouse to region */
2363 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2364 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2366 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2369 if (rr
->ws
->focus
) {
2370 /* move to focus window */
2371 x
= X(rr
->ws
->focus
) + 1;
2372 y
= Y(rr
->ws
->focus
) + 1;
2373 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2378 sort_windows(struct ws_win_list
*wl
)
2380 struct ws_win
*win
, *parent
, *nxt
;
2385 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2386 nxt
= TAILQ_NEXT(win
, entry
);
2387 if (win
->transient
) {
2388 parent
= find_window(win
->transient
);
2389 if (parent
== NULL
) {
2390 warnx("not possible bug");
2393 TAILQ_REMOVE(wl
, win
, entry
);
2394 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2401 swapwin(struct swm_region
*r
, union arg
*args
)
2403 struct ws_win
*target
, *source
;
2404 struct ws_win
*cur_focus
;
2405 struct ws_win_list
*wl
;
2408 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2409 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2411 cur_focus
= r
->ws
->focus
;
2412 if (cur_focus
== NULL
)
2416 wl
= &source
->ws
->winlist
;
2419 case SWM_ARG_ID_SWAPPREV
:
2420 if (source
->transient
)
2421 source
= find_window(source
->transient
);
2422 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2423 if (target
&& target
->transient
)
2424 target
= find_window(target
->transient
);
2425 TAILQ_REMOVE(wl
, source
, entry
);
2427 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2429 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2431 case SWM_ARG_ID_SWAPNEXT
:
2432 target
= TAILQ_NEXT(source
, entry
);
2433 /* move the parent and let the sort handle the move */
2434 if (source
->transient
)
2435 source
= find_window(source
->transient
);
2436 TAILQ_REMOVE(wl
, source
, entry
);
2438 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2440 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2442 case SWM_ARG_ID_SWAPMAIN
:
2443 target
= TAILQ_FIRST(wl
);
2444 if (target
== source
) {
2445 if (source
->ws
->focus_prev
!= NULL
&&
2446 source
->ws
->focus_prev
!= target
)
2448 source
= source
->ws
->focus_prev
;
2452 if (target
== NULL
|| source
== NULL
)
2454 source
->ws
->focus_prev
= target
;
2455 TAILQ_REMOVE(wl
, target
, entry
);
2456 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2457 TAILQ_REMOVE(wl
, source
, entry
);
2458 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2460 case SWM_ARG_ID_MOVELAST
:
2461 TAILQ_REMOVE(wl
, source
, entry
);
2462 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2465 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2475 focus_prev(struct ws_win
*win
)
2477 struct ws_win
*winfocus
= NULL
, *winlostfocus
= NULL
;
2478 struct ws_win
*cur_focus
= NULL
;
2479 struct ws_win_list
*wl
= NULL
;
2480 struct workspace
*ws
= NULL
;
2482 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%lx\n", WINID(win
));
2484 if (!(win
&& win
->ws
))
2489 cur_focus
= ws
->focus
;
2490 winlostfocus
= cur_focus
;
2492 /* pickle, just focus on whatever */
2493 if (cur_focus
== NULL
) {
2494 /* use prev_focus if valid */
2495 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2496 find_window(WINID(ws
->focus_prev
)))
2497 winfocus
= ws
->focus_prev
;
2498 if (winfocus
== NULL
)
2499 winfocus
= TAILQ_FIRST(wl
);
2503 /* if transient focus on parent */
2504 if (cur_focus
->transient
) {
2505 winfocus
= find_window(cur_focus
->transient
);
2509 /* if in max_stack try harder */
2510 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2511 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2512 if (cur_focus
!= ws
->focus_prev
)
2513 winfocus
= ws
->focus_prev
;
2514 else if (cur_focus
!= ws
->focus
)
2515 winfocus
= ws
->focus
;
2517 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2522 if (cur_focus
== win
)
2523 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2524 if (winfocus
== NULL
)
2525 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2526 if (winfocus
== NULL
|| winfocus
== win
)
2527 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2529 if (winfocus
== winlostfocus
|| winfocus
== NULL
) {
2530 /* update the bar so that title/class/name will be cleared. */
2531 if (window_name_enabled
|| title_name_enabled
||
2532 title_class_enabled
)
2537 focus_magic(winfocus
);
2541 focus(struct swm_region
*r
, union arg
*args
)
2543 struct ws_win
*winfocus
= NULL
, *winlostfocus
= NULL
, *head
;
2544 struct ws_win
*cur_focus
= NULL
;
2545 struct ws_win_list
*wl
= NULL
;
2546 struct workspace
*ws
= NULL
;
2552 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2554 /* treat FOCUS_CUR special */
2555 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2556 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2557 winfocus
= r
->ws
->focus
;
2558 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2559 winfocus
= r
->ws
->focus_prev
;
2561 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2562 if (winfocus
->iconic
== 0)
2565 focus_magic(winfocus
);
2569 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2573 if (TAILQ_EMPTY(wl
))
2575 /* make sure there is at least one uniconified window */
2577 TAILQ_FOREACH(winfocus
, wl
, entry
)
2578 if (winfocus
->iconic
== 0) {
2585 winlostfocus
= cur_focus
;
2588 case SWM_ARG_ID_FOCUSPREV
:
2589 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2591 head
= TAILQ_LAST(wl
, ws_win_list
);
2593 if (WINID(winfocus
) == cur_focus
->transient
) {
2594 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2596 head
= TAILQ_LAST(wl
, ws_win_list
);
2601 if (winfocus
&& winfocus
->iconic
) {
2602 while (winfocus
!= cur_focus
) {
2603 if (winfocus
== NULL
)
2604 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2605 if (winfocus
->iconic
== 0)
2607 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
2613 case SWM_ARG_ID_FOCUSNEXT
:
2614 head
= TAILQ_NEXT(cur_focus
, entry
);
2616 head
= TAILQ_FIRST(wl
);
2620 if (winfocus
&& winfocus
->iconic
) {
2621 while (winfocus
!= cur_focus
) {
2622 if (winfocus
== NULL
)
2623 winfocus
= TAILQ_FIRST(wl
);
2624 if (winfocus
->iconic
== 0)
2626 winfocus
= TAILQ_NEXT(winfocus
, entry
);
2631 case SWM_ARG_ID_FOCUSMAIN
:
2632 winfocus
= TAILQ_FIRST(wl
);
2633 if (winfocus
== cur_focus
)
2634 winfocus
= cur_focus
->ws
->focus_prev
;
2640 if (winfocus
== winlostfocus
|| winfocus
== NULL
) {
2641 /* update the bar so that title/class/name will be cleared. */
2642 if (window_name_enabled
|| title_name_enabled
||
2643 title_class_enabled
)
2649 focus_magic(winfocus
);
2653 cycle_layout(struct swm_region
*r
, union arg
*args
)
2655 struct workspace
*ws
= r
->ws
;
2658 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2661 if (ws
->cur_layout
->l_stack
== NULL
)
2662 ws
->cur_layout
= &layouts
[0];
2665 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2666 drain_enter_notify();
2667 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2673 stack_config(struct swm_region
*r
, union arg
*args
)
2675 struct workspace
*ws
= r
->ws
;
2677 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
2680 if (ws
->cur_layout
->l_config
!= NULL
)
2681 ws
->cur_layout
->l_config(ws
, args
->id
);
2683 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
2690 struct swm_geometry g
;
2691 struct swm_region
*r
;
2697 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
2699 for (i
= 0; i
< ScreenCount(display
); i
++) {
2703 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2704 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
2705 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
2707 /* start with screen geometry, adjust for bar */
2709 g
.w
-= 2 * border_width
;
2710 g
.h
-= 2 * border_width
;
2716 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
2717 r
->ws
->cur_layout
->l_string(r
->ws
);
2718 /* save r so we can track region changes */
2725 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2726 drain_enter_notify();
2728 DNPRINTF(SWM_D_STACK
, "stack: end\n");
2732 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
2734 /* retain window geom and region geom */
2735 win
->g_float
= win
->g
;
2736 win
->rg_float
= r
->g
;
2737 win
->g_floatvalid
= 1;
2741 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
2749 bzero(&wc
, sizeof wc
);
2750 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
2753 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2754 * geom on ws switches or return from max mode
2756 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&& win
->g_floatvalid
2757 && !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
2759 * use stored g and rg to set relative position and size
2760 * as in old region or before max stack mode
2762 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ X(r
);
2763 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ Y(r
);
2764 WIDTH(win
) = win
->g_float
.w
;
2765 HEIGHT(win
) = win
->g_float
.h
;
2766 win
->g_floatvalid
= 0;
2769 win
->floatmaxed
= 0;
2771 if ((win
->quirks
& SWM_Q_FULLSCREEN
) && (WIDTH(win
) >= WIDTH(r
)) &&
2772 (HEIGHT(win
) >= HEIGHT(r
)))
2773 wc
.border_width
= 0;
2775 wc
.border_width
= border_width
;
2776 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
2777 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
2778 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
2783 * floaters and transients are auto-centred unless moved
2786 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - wc
.border_width
;
2787 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - wc
.border_width
;
2790 /* win can be outside r if new r smaller than old r */
2791 /* Ensure top left corner inside r (move probs otherwise) */
2792 if (X(win
) < X(r
) - wc
.border_width
)
2793 X(win
) = X(r
) - wc
.border_width
;
2794 if (X(win
) > X(r
) + WIDTH(r
) - 1)
2795 X(win
) = (WIDTH(win
) > WIDTH(r
)) ? X(r
) :
2796 (X(r
) + WIDTH(r
) - WIDTH(win
) - 2 * wc
.border_width
);
2797 if (Y(win
) < Y(r
) - wc
.border_width
)
2798 Y(win
) = Y(r
) - wc
.border_width
;
2799 if (Y(win
) > Y(r
) + HEIGHT(r
) - 1)
2800 Y(win
) = (HEIGHT(win
) > HEIGHT(r
)) ? Y(r
) :
2801 (Y(r
) + HEIGHT(r
) - HEIGHT(win
) - 2 * wc
.border_width
);
2805 wc
.width
= WIDTH(win
);
2806 wc
.height
= HEIGHT(win
);
2809 * Retain floater and transient geometry for correct positioning
2810 * when ws changes region
2812 if (!(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
2813 store_float_geom(win
, r
);
2815 DNPRINTF(SWM_D_MISC
, "stack_floater: window: %lu, (x,y) w x h: (%d,%d) "
2816 "%d x %d\n", win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
2818 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2822 * Send keystrokes to terminal to decrease/increase the font size as the
2823 * window size changes.
2826 adjust_font(struct ws_win
*win
)
2828 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
2829 win
->floating
|| win
->transient
)
2832 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
2833 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
2834 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
2835 win
->font_size_boundary
[win
->font_steps
] =
2836 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
2839 win
->last_inc
= win
->sh
.width_inc
;
2840 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
2841 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
2842 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
2845 win
->last_inc
= win
->sh
.width_inc
;
2846 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
2850 #define SWAPXY(g) do { \
2852 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2853 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2856 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
2859 XWindowAttributes wa
;
2860 struct swm_geometry win_g
, r_g
= *g
;
2861 struct ws_win
*win
, *fs_win
= 0;
2862 int i
, j
, s
, stacks
;
2863 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
2864 int hrh
, extra
= 0, h_slice
, last_h
= 0;
2865 int split
, colno
, winno
, mwin
, msize
, mscale
;
2866 int remain
, missing
, v_slice
, reconfigure
;
2869 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
2870 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
2872 winno
= count_win(ws
, 0);
2873 if (winno
== 0 && count_win(ws
, 1) == 0)
2876 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2877 if (win
->transient
== 0 && win
->floating
== 0
2878 && win
->iconic
== 0)
2885 w_inc
= win
->sh
.width_inc
;
2886 w_base
= win
->sh
.base_width
;
2887 mwin
= ws
->l_state
.horizontal_mwin
;
2888 mscale
= ws
->l_state
.horizontal_msize
;
2889 stacks
= ws
->l_state
.horizontal_stacks
;
2892 w_inc
= win
->sh
.height_inc
;
2893 w_base
= win
->sh
.base_height
;
2894 mwin
= ws
->l_state
.vertical_mwin
;
2895 mscale
= ws
->l_state
.vertical_msize
;
2896 stacks
= ws
->l_state
.vertical_stacks
;
2900 if (stacks
> winno
- mwin
)
2901 stacks
= winno
- mwin
;
2905 h_slice
= r_g
.h
/ SWM_H_SLICE
;
2906 if (mwin
&& winno
> mwin
) {
2907 v_slice
= r_g
.w
/ SWM_V_SLICE
;
2911 win_g
.w
= v_slice
* mscale
;
2913 if (w_inc
> 1 && w_inc
< v_slice
) {
2914 /* adjust for window's requested size increment */
2915 remain
= (win_g
.w
- w_base
) % w_inc
;
2916 missing
= w_inc
- remain
;
2923 win_g
.x
+= r_g
.w
- msize
;
2926 colno
= split
= winno
/ stacks
;
2927 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
2928 2 * border_width
) / stacks
);
2930 hrh
= r_g
.h
/ colno
;
2931 extra
= r_g
.h
- (colno
* hrh
);
2932 win_g
.h
= hrh
- 2 * border_width
;
2934 /* stack all the tiled windows */
2935 i
= j
= 0, s
= stacks
;
2936 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2937 if (win
->transient
!= 0 || win
->floating
!= 0)
2939 if (win
->iconic
!= 0)
2942 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
2947 if (split
&& i
== split
) {
2948 colno
= (winno
- mwin
) / stacks
;
2949 if (s
<= (winno
- mwin
) % stacks
)
2951 split
= split
+ colno
;
2952 hrh
= (r_g
.h
/ colno
);
2953 extra
= r_g
.h
- (colno
* hrh
);
2957 win_g
.x
+= win_g
.w
+ 2 * border_width
;
2958 win_g
.w
= (r_g
.w
- msize
-
2959 (stacks
* 2 * border_width
)) / stacks
;
2961 win_g
.w
+= (r_g
.w
- msize
-
2962 (stacks
* 2 * border_width
)) % stacks
;
2966 win_g
.h
= hrh
- 2 * border_width
;
2968 h_inc
= win
->sh
.width_inc
;
2969 h_base
= win
->sh
.base_width
;
2971 h_inc
= win
->sh
.height_inc
;
2972 h_base
= win
->sh
.base_height
;
2974 if (j
== colno
- 1) {
2975 win_g
.h
= hrh
+ extra
;
2976 } else if (h_inc
> 1 && h_inc
< h_slice
) {
2977 /* adjust for window's requested size increment */
2978 remain
= (win_g
.h
- h_base
) % h_inc
;
2979 missing
= h_inc
- remain
;
2981 if (missing
<= extra
|| j
== 0) {
2993 win_g
.y
+= last_h
+ 2 * border_width
;
2995 bzero(&wc
, sizeof wc
);
2996 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
2997 wc
.border_width
= 0;
2998 win_g
.w
+= 2 * border_width
;
2999 win_g
.h
+= 2 * border_width
;
3001 wc
.border_width
= border_width
;
3004 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3005 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3007 X(win
) = wc
.x
= win_g
.y
;
3008 Y(win
) = wc
.y
= win_g
.x
;
3009 WIDTH(win
) = wc
.width
= win_g
.h
;
3010 HEIGHT(win
) = wc
.height
= win_g
.w
;
3013 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3014 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3016 X(win
) = wc
.x
= win_g
.x
;
3017 Y(win
) = wc
.y
= win_g
.y
;
3018 WIDTH(win
) = wc
.width
= win_g
.w
;
3019 HEIGHT(win
) = wc
.height
= win_g
.h
;
3024 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
3025 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3028 if (XGetWindowAttributes(display
, win
->id
, &wa
))
3029 if (wa
.map_state
== IsUnmapped
)
3030 XMapRaised(display
, win
->id
);
3038 /* now, stack all the floaters and transients */
3039 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3040 if (win
->transient
== 0 && win
->floating
== 0)
3042 if (win
->iconic
== 1)
3044 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3049 stack_floater(win
, ws
->r
);
3050 XMapRaised(display
, win
->id
);
3054 stack_floater(fs_win
, ws
->r
);
3055 XMapRaised(display
, fs_win
->id
);
3060 vertical_config(struct workspace
*ws
, int id
)
3062 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3066 case SWM_ARG_ID_STACKRESET
:
3067 case SWM_ARG_ID_STACKINIT
:
3068 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3069 ws
->l_state
.vertical_mwin
= 1;
3070 ws
->l_state
.vertical_stacks
= 1;
3072 case SWM_ARG_ID_MASTERSHRINK
:
3073 if (ws
->l_state
.vertical_msize
> 1)
3074 ws
->l_state
.vertical_msize
--;
3076 case SWM_ARG_ID_MASTERGROW
:
3077 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3078 ws
->l_state
.vertical_msize
++;
3080 case SWM_ARG_ID_MASTERADD
:
3081 ws
->l_state
.vertical_mwin
++;
3083 case SWM_ARG_ID_MASTERDEL
:
3084 if (ws
->l_state
.vertical_mwin
> 0)
3085 ws
->l_state
.vertical_mwin
--;
3087 case SWM_ARG_ID_STACKINC
:
3088 ws
->l_state
.vertical_stacks
++;
3090 case SWM_ARG_ID_STACKDEC
:
3091 if (ws
->l_state
.vertical_stacks
> 1)
3092 ws
->l_state
.vertical_stacks
--;
3100 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3102 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3104 stack_master(ws
, g
, 0, 0);
3108 horizontal_config(struct workspace
*ws
, int id
)
3110 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3113 case SWM_ARG_ID_STACKRESET
:
3114 case SWM_ARG_ID_STACKINIT
:
3115 ws
->l_state
.horizontal_mwin
= 1;
3116 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3117 ws
->l_state
.horizontal_stacks
= 1;
3119 case SWM_ARG_ID_MASTERSHRINK
:
3120 if (ws
->l_state
.horizontal_msize
> 1)
3121 ws
->l_state
.horizontal_msize
--;
3123 case SWM_ARG_ID_MASTERGROW
:
3124 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3125 ws
->l_state
.horizontal_msize
++;
3127 case SWM_ARG_ID_MASTERADD
:
3128 ws
->l_state
.horizontal_mwin
++;
3130 case SWM_ARG_ID_MASTERDEL
:
3131 if (ws
->l_state
.horizontal_mwin
> 0)
3132 ws
->l_state
.horizontal_mwin
--;
3134 case SWM_ARG_ID_STACKINC
:
3135 ws
->l_state
.horizontal_stacks
++;
3137 case SWM_ARG_ID_STACKDEC
:
3138 if (ws
->l_state
.horizontal_stacks
> 1)
3139 ws
->l_state
.horizontal_stacks
--;
3147 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3149 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3151 stack_master(ws
, g
, 1, 0);
3154 /* fullscreen view */
3156 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3159 struct swm_geometry gg
= *g
;
3160 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3164 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3169 winno
= count_win(ws
, 0);
3170 if (winno
== 0 && count_win(ws
, 1) == 0)
3173 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3174 if (win
->transient
) {
3176 parent
= find_window(win
->transient
);
3180 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3182 * retain geometry for retrieval on exit from
3185 store_float_geom(win
, ws
->r
);
3186 win
->floatmaxed
= 1;
3189 /* only reconfigure if necessary */
3190 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3191 HEIGHT(win
) != gg
.h
) {
3192 bzero(&wc
, sizeof wc
);
3193 X(win
) = wc
.x
= gg
.x
;
3194 Y(win
) = wc
.y
= gg
.y
;
3196 wc
.border_width
= border_width
;
3197 WIDTH(win
) = wc
.width
= gg
.w
;
3198 HEIGHT(win
) = wc
.height
= gg
.h
;
3200 wc
.border_width
= 0;
3201 WIDTH(win
) = wc
.width
= gg
.w
+ 2 * border_width
;
3202 HEIGHT(win
) = wc
.height
= gg
.h
+
3205 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
3206 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3208 /* unmap only if we don't have multi screen */
3209 if (win
!= ws
->focus
)
3210 if (!(ScreenCount(display
) > 1 || outputs
> 1))
3214 /* put the last transient on top */
3217 XMapRaised(display
, parent
->id
);
3218 stack_floater(wintrans
, ws
->r
);
3219 focus_magic(wintrans
);
3224 send_to_ws(struct swm_region
*r
, union arg
*args
)
3226 int wsid
= args
->id
;
3227 struct ws_win
*win
= NULL
, *parent
;
3228 struct workspace
*ws
, *nws
;
3229 Atom ws_idx_atom
= 0;
3230 unsigned char ws_idx_str
[SWM_PROPLEN
];
3233 if (r
&& r
->ws
&& r
->ws
->focus
)
3239 if (win
->ws
->idx
== wsid
)
3242 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%lx\n", win
->id
);
3245 nws
= &win
->s
->ws
[wsid
];
3247 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3249 if (win
->transient
) {
3250 parent
= find_window(win
->transient
);
3252 unmap_window(parent
);
3253 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3254 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3259 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3260 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3261 if (TAILQ_EMPTY(&ws
->winlist
))
3262 r
->ws
->focus
= NULL
;
3265 /* Try to update the window's workspace property */
3266 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
3268 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3270 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3272 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
3273 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
3280 pressbutton(struct swm_region
*r
, union arg
*args
)
3282 XTestFakeButtonEvent(display
, args
->id
, True
, CurrentTime
);
3283 XTestFakeButtonEvent(display
, args
->id
, False
, CurrentTime
);
3287 raise_toggle(struct swm_region
*r
, union arg
*args
)
3289 if (r
&& r
->ws
== NULL
)
3292 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3294 /* bring floaters back to top */
3295 if (r
->ws
->always_raise
== 0)
3300 iconify(struct swm_region
*r
, union arg
*args
)
3304 if (r
->ws
->focus
== NULL
)
3306 unmap_window(r
->ws
->focus
);
3307 update_iconic(r
->ws
->focus
, 1);
3309 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3310 drain_enter_notify();
3311 r
->ws
->focus
= NULL
;
3312 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3317 get_win_name(Window win
)
3319 unsigned char *prop
= NULL
;
3320 unsigned long nbytes
, nitems
;
3322 /* try _NET_WM_NAME first */
3323 if (get_property(win
, a_netwmname
, 0L, a_utf8_string
, NULL
, &nbytes
,
3326 if (get_property(win
, a_netwmname
, nbytes
, a_utf8_string
,
3327 &nitems
, NULL
, &prop
))
3331 /* fallback to WM_NAME */
3332 if (!get_property(win
, a_wmname
, 0L, a_string
, NULL
, &nbytes
, &prop
))
3335 if (get_property(win
, a_wmname
, nbytes
, a_string
, &nitems
, NULL
, &prop
))
3342 uniconify(struct swm_region
*r
, union arg
*args
)
3346 unsigned char *name
;
3349 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3351 if (r
&& r
->ws
== NULL
)
3354 /* make sure we have anything to uniconify */
3355 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3356 if (win
->ws
== NULL
)
3357 continue; /* should never happen */
3358 if (win
->iconic
== 0)
3366 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3368 spawn_select(r
, args
, "search", &searchpid
);
3370 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3373 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3374 if (win
->ws
== NULL
)
3375 continue; /* should never happen */
3376 if (win
->iconic
== 0)
3379 name
= get_win_name(win
->id
);
3382 fprintf(lfile
, "%s.%lu\n", name
, win
->id
);
3390 name_workspace(struct swm_region
*r
, union arg
*args
)
3394 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3400 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3402 spawn_select(r
, args
, "name_workspace", &searchpid
);
3404 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3407 fprintf(lfile
, "%s", "");
3412 search_workspace(struct swm_region
*r
, union arg
*args
)
3415 struct workspace
*ws
;
3418 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3424 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3426 spawn_select(r
, args
, "search", &searchpid
);
3428 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3431 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
3435 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3436 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3443 search_win_cleanup(void)
3445 struct search_window
*sw
= NULL
;
3447 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3448 XDestroyWindow(display
, sw
->indicator
);
3449 XFreeGC(display
, sw
->gc
);
3450 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3456 search_win(struct swm_region
*r
, union arg
*args
)
3458 struct ws_win
*win
= NULL
;
3459 struct search_window
*sw
= NULL
;
3466 XRectangle ibox
, lbox
;
3468 DNPRINTF(SWM_D_MISC
, "search_win\n");
3471 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3473 spawn_select(r
, args
, "search", &searchpid
);
3475 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3478 TAILQ_INIT(&search_wl
);
3481 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3482 if (win
->iconic
== 1)
3485 sw
= calloc(1, sizeof(struct search_window
));
3487 warn("search_win: calloc");
3489 search_win_cleanup();
3495 snprintf(s
, sizeof s
, "%d", i
);
3498 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
3500 w
= XCreateSimpleWindow(display
,
3501 win
->id
, 0, 0,lbox
.width
+ 4,
3502 bar_fs_extents
->max_logical_extent
.height
, 1,
3503 r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
,
3504 r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
3507 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3509 sw
->gc
= XCreateGC(display
, w
, 0, &gcv
);
3510 XMapRaised(display
, w
);
3511 XSetForeground(display
, sw
->gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
3513 DRAWSTRING(display
, w
, bar_fs
, sw
->gc
, 2,
3514 (bar_fs_extents
->max_logical_extent
.height
-
3515 lbox
.height
) / 2 - lbox
.y
, s
, len
);
3517 fprintf(lfile
, "%d\n", i
);
3525 search_resp_uniconify(char *resp
, unsigned long len
)
3527 unsigned char *name
;
3531 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3533 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3534 if (win
->iconic
== 0)
3536 name
= get_win_name(win
->id
);
3539 if (asprintf(&s
, "%s.%lu", name
, win
->id
) == -1) {
3544 if (strncmp(s
, resp
, len
) == 0) {
3545 /* XXX this should be a callback to generalize */
3546 update_iconic(win
, 0);
3555 search_resp_name_workspace(char *resp
, unsigned long len
)
3557 struct workspace
*ws
;
3559 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3561 if (search_r
->ws
== NULL
)
3566 free(search_r
->ws
->name
);
3567 search_r
->ws
->name
= NULL
;
3571 ws
->name
= strdup(resp
);
3572 if (ws
->name
== NULL
) {
3573 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
3574 "strdup: %s", strerror(errno
));
3581 search_resp_search_workspace(char *resp
, unsigned long len
)
3588 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
3592 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
3599 ws_idx
= strtonum(q
, 1, SWM_WS_MAX
, &errstr
);
3601 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
3608 switchws(search_r
, &a
);
3612 search_resp_search_window(char *resp
, unsigned long len
)
3617 struct search_window
*sw
;
3619 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
3623 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
3628 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
3630 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
3637 TAILQ_FOREACH(sw
, &search_wl
, entry
)
3638 if (idx
== sw
->idx
) {
3644 #define MAX_RESP_LEN 1024
3647 search_do_resp(void)
3653 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
3658 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
3659 warn("search: calloc");
3663 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
3665 warn("search: read error");
3668 resp
[rbytes
] = '\0';
3671 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3672 * newline, so work around that by sanitizing the resp now.
3674 resp
[strcspn(resp
, "\n")] = '\0';
3677 switch (search_resp_action
) {
3678 case SWM_SEARCH_UNICONIFY
:
3679 search_resp_uniconify(resp
, len
);
3681 case SWM_SEARCH_NAME_WORKSPACE
:
3682 search_resp_name_workspace(resp
, len
);
3684 case SWM_SEARCH_SEARCH_WORKSPACE
:
3685 search_resp_search_workspace(resp
, len
);
3687 case SWM_SEARCH_SEARCH_WINDOW
:
3688 search_resp_search_window(resp
, len
);
3693 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
3694 search_win_cleanup();
3696 search_resp_action
= SWM_SEARCH_NONE
;
3697 close(select_resp_pipe
[0]);
3702 wkill(struct swm_region
*r
, union arg
*args
)
3704 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
3706 if (r
->ws
->focus
== NULL
)
3709 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
3710 XKillClient(display
, r
->ws
->focus
->id
);
3712 if (r
->ws
->focus
->can_delete
)
3713 client_msg(r
->ws
->focus
, adelete
);
3718 floating_toggle_win(struct ws_win
*win
)
3720 struct swm_region
*r
;
3730 /* reject floating toggles in max stack mode */
3731 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3734 if (win
->floating
) {
3735 if (!win
->floatmaxed
) {
3736 /* retain position for refloat */
3737 store_float_geom(win
, r
);
3741 if (win
->g_floatvalid
) {
3742 /* refloat at last floating relative position */
3743 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ X(r
);
3744 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ Y(r
);
3745 WIDTH(win
) = win
->g_float
.w
;
3746 HEIGHT(win
) = win
->g_float
.h
;
3751 ewmh_update_actions(win
);
3757 floating_toggle(struct swm_region
*r
, union arg
*args
)
3759 struct ws_win
*win
= r
->ws
->focus
;
3765 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
3766 _NET_WM_STATE_TOGGLE
);
3769 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3770 drain_enter_notify();
3772 if (win
== win
->ws
->focus
) {
3773 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3774 focus(win
->ws
->r
, &a
);
3779 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
3781 if (X(win
) + WIDTH(win
) > X(r
) + WIDTH(r
) - border_width
) {
3783 WIDTH(win
) = X(r
) + WIDTH(r
) - X(win
) - border_width
;
3785 X(win
) = X(r
) + WIDTH(r
) - WIDTH(win
) - border_width
;
3788 if (X(win
) < X(r
) - border_width
) {
3790 WIDTH(win
) -= X(r
) - X(win
) - border_width
;
3792 X(win
) = X(r
) - border_width
;
3795 if (Y(win
) + HEIGHT(win
) > Y(r
) + HEIGHT(r
) - border_width
) {
3797 HEIGHT(win
) = Y(r
) + HEIGHT(r
) - Y(win
) - border_width
;
3799 Y(win
) = Y(r
) + HEIGHT(r
) - HEIGHT(win
) - border_width
;
3802 if (Y(win
) < Y(r
) - border_width
) {
3804 HEIGHT(win
) -= Y(r
) - Y(win
) - border_width
;
3806 Y(win
) = Y(r
) - border_width
;
3811 if (HEIGHT(win
) < 1)
3816 update_window(struct ws_win
*win
)
3821 bzero(&wc
, sizeof wc
);
3822 mask
= CWBorderWidth
| CWWidth
| CWHeight
| CWX
| CWY
;
3823 wc
.border_width
= border_width
;
3826 wc
.width
= WIDTH(win
);
3827 wc
.height
= HEIGHT(win
);
3829 DNPRINTF(SWM_D_MISC
, "update_window: window: 0x%lx, (x,y) w x h: "
3830 "(%d,%d) %d x %d\n", win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
3832 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3835 #define SWM_RESIZE_STEPS (50)
3838 resize(struct ws_win
*win
, union arg
*args
)
3842 struct swm_region
*r
= NULL
;
3843 int resize_step
= 0;
3847 struct swm_geometry g
;
3848 int top
= 0, left
= 0;
3851 unsigned int shape
; /* cursor style */
3857 DNPRINTF(SWM_D_MOUSE
, "resize: window: 0x%lx, floating: %s, "
3858 "transient: 0x%lx\n", win
->id
, YESNO(win
->floating
),
3861 if (!(win
->transient
!= 0 || win
->floating
!= 0))
3864 /* reject resizes in max mode for floaters (transient ok) */
3865 if (win
->floatmaxed
)
3869 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
3875 case SWM_ARG_ID_WIDTHSHRINK
:
3876 WIDTH(win
) -= SWM_RESIZE_STEPS
;
3879 case SWM_ARG_ID_WIDTHGROW
:
3880 WIDTH(win
) += SWM_RESIZE_STEPS
;
3883 case SWM_ARG_ID_HEIGHTSHRINK
:
3884 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
3887 case SWM_ARG_ID_HEIGHTGROW
:
3888 HEIGHT(win
) += SWM_RESIZE_STEPS
;
3895 constrain_window(win
, r
, 1);
3897 store_float_geom(win
,r
);
3901 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3902 drain_enter_notify();
3904 /* get cursor offset from window root */
3905 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
3910 if (wx
< WIDTH(win
) / 2)
3913 if (wy
< HEIGHT(win
) / 2)
3916 if (args
->id
== SWM_ARG_ID_CENTER
)
3919 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
3921 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
3923 cursor
= XCreateFontCursor(display
, shape
);
3925 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
3926 GrabModeAsync
, None
, cursor
, CurrentTime
) != GrabSuccess
) {
3927 XFreeCursor(display
, cursor
);
3932 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
3933 SubstructureRedirectMask
, &ev
);
3935 case ConfigureRequest
:
3938 handler
[ev
.type
](&ev
);
3941 /* cursor offset/delta from start of the operation */
3942 dx
= ev
.xmotion
.x_root
- x
;
3943 dy
= ev
.xmotion
.y_root
- y
;
3949 if (args
->id
== SWM_ARG_ID_CENTER
) {
3950 if (g
.h
/ 2 + dy
< 1)
3954 HEIGHT(win
) = g
.h
+ 2 * dy
;
3962 HEIGHT(win
) = g
.h
+ dy
;
3969 if (args
->id
== SWM_ARG_ID_CENTER
) {
3970 if (g
.w
/ 2 + dx
< 1)
3974 WIDTH(win
) = g
.w
+ 2 * dx
;
3982 WIDTH(win
) = g
.w
+ dx
;
3985 constrain_window(win
, r
, 1);
3987 /* not free, don't sync more than 120 times / second */
3988 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
3989 time
= ev
.xmotion
.time
;
3990 XSync(display
, False
);
3995 } while (ev
.type
!= ButtonRelease
);
3997 XSync(display
, False
);
4000 store_float_geom(win
,r
);
4002 XUngrabPointer(display
, CurrentTime
);
4003 XFreeCursor(display
, cursor
);
4006 drain_enter_notify();
4010 resize_step(struct swm_region
*r
, union arg
*args
)
4012 struct ws_win
*win
= NULL
;
4014 if (r
&& r
->ws
&& r
->ws
->focus
)
4022 #define SWM_MOVE_STEPS (50)
4025 move(struct ws_win
*win
, union arg
*args
)
4030 struct swm_region
*r
= NULL
;
4040 DNPRINTF(SWM_D_MOUSE
, "move: window: 0x%lx, floating: %s, transient: "
4041 "0x%lx\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4043 /* in max_stack mode should only move transients */
4044 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4048 if (win
->floating
== 0 && !win
->transient
) {
4049 store_float_geom(win
,r
);
4050 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4053 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4060 case SWM_ARG_ID_MOVELEFT
:
4061 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4064 case SWM_ARG_ID_MOVERIGHT
:
4065 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4068 case SWM_ARG_ID_MOVEUP
:
4069 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4072 case SWM_ARG_ID_MOVEDOWN
:
4073 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4080 constrain_window(win
, r
, 0);
4082 store_float_geom(win
, r
);
4086 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4087 GrabModeAsync
, None
, XCreateFontCursor(display
, XC_fleur
),
4088 CurrentTime
) != GrabSuccess
)
4091 /* get cursor offset from window root */
4092 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4096 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4097 SubstructureRedirectMask
, &ev
);
4099 case ConfigureRequest
:
4102 handler
[ev
.type
](&ev
);
4105 X(win
) = ev
.xmotion
.x_root
- wx
- border_width
;
4106 Y(win
) = ev
.xmotion
.y_root
- wy
- border_width
;
4108 constrain_window(win
, r
, 0);
4110 /* not free, don't sync more than 120 times / second */
4111 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4112 time
= ev
.xmotion
.time
;
4113 XSync(display
, False
);
4118 } while (ev
.type
!= ButtonRelease
);
4120 XSync(display
, False
);
4123 store_float_geom(win
,r
);
4124 XUngrabPointer(display
, CurrentTime
);
4127 drain_enter_notify();
4131 move_step(struct swm_region
*r
, union arg
*args
)
4133 struct ws_win
*win
= NULL
;
4135 if (r
&& r
->ws
&& r
->ws
->focus
)
4140 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4147 /* user/key callable function IDs */
4217 kf_search_workspace
,
4219 kf_dumpwins
, /* MUST BE LAST */
4223 /* key definitions */
4225 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4230 legacyfunc(struct swm_region
*r
, union arg
*args
)
4235 char name
[SWM_FUNCNAME_LEN
];
4236 void (*func
)(struct swm_region
*r
, union arg
*);
4238 } keyfuncs
[kf_invalid
+ 1] = {
4239 /* name function argument */
4240 { "cycle_layout", cycle_layout
, {0} },
4241 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4242 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4243 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4244 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4245 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4246 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4247 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4248 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4249 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4250 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4251 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4252 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4253 { "spawn_term", spawnterm
, {.argv
= spawn_term
} },
4254 { "spawn_menu", legacyfunc
, {0} },
4255 { "quit", quit
, {0} },
4256 { "restart", restart
, {0} },
4257 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4258 { "ws_1", switchws
, {.id
= 0} },
4259 { "ws_2", switchws
, {.id
= 1} },
4260 { "ws_3", switchws
, {.id
= 2} },
4261 { "ws_4", switchws
, {.id
= 3} },
4262 { "ws_5", switchws
, {.id
= 4} },
4263 { "ws_6", switchws
, {.id
= 5} },
4264 { "ws_7", switchws
, {.id
= 6} },
4265 { "ws_8", switchws
, {.id
= 7} },
4266 { "ws_9", switchws
, {.id
= 8} },
4267 { "ws_10", switchws
, {.id
= 9} },
4268 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4269 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4270 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4271 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4272 { "ws_prior", priorws
, {0} },
4273 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4274 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4275 { "mvws_1", send_to_ws
, {.id
= 0} },
4276 { "mvws_2", send_to_ws
, {.id
= 1} },
4277 { "mvws_3", send_to_ws
, {.id
= 2} },
4278 { "mvws_4", send_to_ws
, {.id
= 3} },
4279 { "mvws_5", send_to_ws
, {.id
= 4} },
4280 { "mvws_6", send_to_ws
, {.id
= 5} },
4281 { "mvws_7", send_to_ws
, {.id
= 6} },
4282 { "mvws_8", send_to_ws
, {.id
= 7} },
4283 { "mvws_9", send_to_ws
, {.id
= 8} },
4284 { "mvws_10", send_to_ws
, {.id
= 9} },
4285 { "bar_toggle", bar_toggle
, {0} },
4286 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4287 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4288 { "screenshot_all", legacyfunc
, {0} },
4289 { "screenshot_wind", legacyfunc
, {0} },
4290 { "float_toggle", floating_toggle
,{0} },
4291 { "version", version
, {0} },
4292 { "spawn_lock", legacyfunc
, {0} },
4293 { "spawn_initscr", legacyfunc
, {0} },
4294 { "spawn_custom", dummykeyfunc
, {0} },
4295 { "iconify", iconify
, {0} },
4296 { "uniconify", uniconify
, {0} },
4297 { "raise_toggle", raise_toggle
, {0} },
4298 { "button2", pressbutton
, {2} },
4299 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4300 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4301 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4302 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4303 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4304 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4305 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4306 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4307 { "name_workspace", name_workspace
, {0} },
4308 { "search_workspace", search_workspace
, {0} },
4309 { "search_win", search_win
, {0} },
4310 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4311 { "invalid key func", NULL
, {0} },
4314 TAILQ_ENTRY(key
) entry
;
4317 enum keyfuncid funcid
;
4320 TAILQ_HEAD(key_list
, key
);
4321 struct key_list keys
= TAILQ_HEAD_INITIALIZER(keys
);
4324 enum { client_click
, root_click
};
4326 unsigned int action
;
4328 unsigned int button
;
4329 void (*func
)(struct ws_win
*, union arg
*);
4332 /* action key mouse button func args */
4333 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4334 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4335 { client_click
, MODKEY
, Button1
, move
, {0} },
4339 update_modkey(unsigned int mod
)
4345 TAILQ_FOREACH(kp
, &keys
, entry
)
4346 if (kp
->mod
& ShiftMask
)
4347 kp
->mod
= mod
| ShiftMask
;
4351 for (i
= 0; i
< LENGTH(buttons
); i
++)
4352 if (buttons
[i
].mask
& ShiftMask
)
4353 buttons
[i
].mask
= mod
| ShiftMask
;
4355 buttons
[i
].mask
= mod
;
4365 int spawns_size
= 0, spawns_length
= 0;
4366 struct spawn_prog
*spawns
= NULL
;
4369 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4372 struct spawn_prog
*prog
= NULL
;
4374 char *ap
, **real_args
;
4376 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4379 for (i
= 0; i
< spawns_length
; i
++) {
4380 if (!strcasecmp(spawn_name
, spawns
[i
].name
))
4384 warnx("spawn_custom: program %s not found", spawn_name
);
4388 /* make room for expanded args */
4389 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4390 err(1, "spawn_custom: calloc real_args");
4392 /* expand spawn_args into real_args */
4393 for (i
= 0; i
< prog
->argc
; i
++) {
4395 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4396 if (!strcasecmp(ap
, "$bar_border")) {
4398 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4400 err(1, "spawn_custom border color");
4401 } else if (!strcasecmp(ap
, "$bar_color")) {
4403 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4405 err(1, "spawn_custom bar color");
4406 } else if (!strcasecmp(ap
, "$bar_font")) {
4407 if ((real_args
[i
] = strdup(bar_fonts
))
4409 err(1, "spawn_custom bar fonts");
4410 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4412 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4414 err(1, "spawn_custom color font");
4415 } else if (!strcasecmp(ap
, "$color_focus")) {
4417 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4419 err(1, "spawn_custom color focus");
4420 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4422 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4424 err(1, "spawn_custom color unfocus");
4426 /* no match --> copy as is */
4427 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4428 err(1, "spawn_custom strdup(ap)");
4430 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4435 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4436 for (i
= 0; i
< prog
->argc
; i
++)
4437 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4438 DNPRINTF(SWM_D_SPAWN
, "\n");
4440 *ret_args
= real_args
;
4441 return (prog
->argc
);
4445 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4451 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4455 spawn(r
->ws
->idx
, &a
, 1);
4457 for (i
= 0; i
< spawn_argc
; i
++)
4463 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4469 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4473 if (pipe(select_list_pipe
) == -1)
4474 err(1, "pipe error");
4475 if (pipe(select_resp_pipe
) == -1)
4476 err(1, "pipe error");
4478 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4479 err(1, "could not disable SIGPIPE");
4480 switch (*pid
= fork()) {
4482 err(1, "cannot fork");
4485 if (dup2(select_list_pipe
[0], 0) == -1)
4487 if (dup2(select_resp_pipe
[1], 1) == -1)
4489 close(select_list_pipe
[1]);
4490 close(select_resp_pipe
[0]);
4491 spawn(r
->ws
->idx
, &a
, 0);
4493 default: /* parent */
4494 close(select_list_pipe
[0]);
4495 close(select_resp_pipe
[1]);
4499 for (i
= 0; i
< spawn_argc
; i
++)
4505 setspawn(struct spawn_prog
*prog
)
4509 if (prog
== NULL
|| prog
->name
== NULL
)
4513 for (i
= 0; i
< spawns_length
; i
++) {
4514 if (!strcmp(spawns
[i
].name
, prog
->name
)) {
4516 if (prog
->argv
== NULL
) {
4518 DNPRINTF(SWM_D_SPAWN
,
4519 "setspawn: delete #%d: %s\n",
4521 free(spawns
[i
].name
);
4522 for (j
= 0; j
< spawns
[i
].argc
; j
++)
4523 free(spawns
[i
].argv
[j
]);
4524 free(spawns
[i
].argv
);
4525 j
= spawns_length
- 1;
4527 spawns
[i
] = spawns
[j
];
4532 DNPRINTF(SWM_D_SPAWN
,
4533 "setspawn: replace #%d: %s\n",
4535 free(spawns
[i
].name
);
4536 for (j
= 0; j
< spawns
[i
].argc
; j
++)
4537 free(spawns
[i
].argv
[j
]);
4538 free(spawns
[i
].argv
);
4541 /* found case handled */
4547 if (prog
->argv
== NULL
) {
4548 warnx("error: setspawn: cannot find program: %s", prog
->name
);
4553 /* not found: add */
4554 if (spawns_size
== 0 || spawns
== NULL
) {
4556 DNPRINTF(SWM_D_SPAWN
, "setspawn: init list %d\n", spawns_size
);
4557 spawns
= malloc((size_t)spawns_size
*
4558 sizeof(struct spawn_prog
));
4559 if (spawns
== NULL
) {
4560 warn("setspawn: malloc failed");
4563 } else if (spawns_length
== spawns_size
) {
4565 DNPRINTF(SWM_D_SPAWN
, "setspawn: grow list %d\n", spawns_size
);
4566 spawns
= realloc(spawns
, (size_t)spawns_size
*
4567 sizeof(struct spawn_prog
));
4568 if (spawns
== NULL
) {
4569 warn("setspawn: realloc failed");
4574 if (spawns_length
< spawns_size
) {
4575 DNPRINTF(SWM_D_SPAWN
, "setspawn: add #%d %s\n",
4576 spawns_length
, prog
->name
);
4577 i
= spawns_length
++;
4580 warnx("spawns array problem?");
4581 if (spawns
== NULL
) {
4582 warnx("spawns array is NULL!");
4590 setconfspawn(char *selector
, char *value
, int flags
)
4592 struct spawn_prog
*prog
;
4593 char *vp
, *cp
, *word
;
4595 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
4596 if ((prog
= calloc(1, sizeof *prog
)) == NULL
)
4597 err(1, "setconfspawn: calloc prog");
4598 prog
->name
= strdup(selector
);
4599 if (prog
->name
== NULL
)
4600 err(1, "setconfspawn prog->name");
4601 if ((cp
= vp
= strdup(value
)) == NULL
)
4602 err(1, "setconfspawn: strdup(value) ");
4603 while ((word
= strsep(&cp
, " \t")) != NULL
) {
4604 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: arg [%s]\n", word
);
4606 cp
+= (long)strspn(cp
, " \t");
4607 if (strlen(word
) > 0) {
4609 if ((prog
->argv
= realloc(prog
->argv
,
4610 prog
->argc
* sizeof(char *))) == NULL
)
4611 err(1, "setconfspawn: realloc");
4612 if ((prog
->argv
[prog
->argc
- 1] = strdup(word
)) == NULL
)
4613 err(1, "setconfspawn: strdup");
4620 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
4627 setconfspawn("term", "xterm", 0);
4628 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4629 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4630 setconfspawn("lock", "xlock", 0);
4631 setconfspawn("initscr", "initscreen.sh", 0);
4632 setconfspawn("menu", "dmenu_run"
4635 " -nf $bar_font_color"
4637 " -sf $bar_color", 0);
4638 setconfspawn("search", "dmenu"
4642 " -nf $bar_font_color"
4644 " -sf $bar_color", 0);
4645 setconfspawn("name_workspace", "dmenu"
4649 " -nf $bar_font_color"
4651 " -sf $bar_color", 0);
4655 #define SWM_MODNAME_SIZE 32
4656 #define SWM_KEY_WS "\n+ \t"
4658 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
4662 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
4663 if (mod
== NULL
|| ks
== NULL
) {
4664 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
4667 if (keystr
== NULL
|| strlen(keystr
) == 0) {
4668 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
4674 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
4675 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
4677 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
4678 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
4680 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
4682 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
4684 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
4686 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
4688 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
4690 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
4691 *mod
|= ControlMask
;
4693 *ks
= XStringToKeysym(name
);
4694 XConvertCase(*ks
, ks
, &uks
);
4695 if (ks
== NoSymbol
) {
4697 "parsekeys: invalid key %s\n",
4703 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
4708 strdupsafe(char *str
)
4713 return (strdup(str
));
4717 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
4721 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
4722 keyfuncs
[kfid
].name
, spawn_name
);
4724 if ((kp
= malloc(sizeof *kp
)) == NULL
)
4725 err(1, "key_insert: malloc");
4730 kp
->spawn_name
= strdupsafe(spawn_name
);
4731 TAILQ_INSERT_TAIL(&keys
, kp
, entry
);
4733 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
4737 key_remove(struct key
*kp
)
4739 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
4741 TAILQ_REMOVE(&keys
, kp
, entry
);
4742 free(kp
->spawn_name
);
4745 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
4749 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
4752 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
4756 key_insert(mod
, ks
, kfid
, spawn_name
);
4758 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
4762 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
4767 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
4768 keyfuncs
[kfid
].name
, spawn_name
);
4770 TAILQ_FOREACH(kp
, &keys
, entry
) {
4771 if (kp
->mod
== mod
&& kp
->keysym
== ks
) {
4772 if (kfid
== kf_invalid
)
4775 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
4776 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4780 if (kfid
== kf_invalid
) {
4781 warnx("error: setkeybinding: cannot find mod/key combination");
4782 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4786 key_insert(mod
, ks
, kfid
, spawn_name
);
4787 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4791 setconfbinding(char *selector
, char *value
, int flags
)
4793 enum keyfuncid kfid
;
4797 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
4798 if (selector
== NULL
) {
4799 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
4800 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4802 setkeybinding(mod
, ks
, kfid
, NULL
);
4807 /* search by key function name */
4808 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
4809 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
4810 SWM_FUNCNAME_LEN
) == 0) {
4811 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4813 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4814 setkeybinding(mod
, ks
, kfid
, NULL
);
4820 /* search by custom spawn name */
4821 for (i
= 0; i
< spawns_length
; i
++) {
4822 if (strcasecmp(selector
, spawns
[i
].name
) == 0) {
4823 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4825 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4826 setkeybinding(mod
, ks
, kf_spawn_custom
,
4833 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
4840 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
4841 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
4842 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
4843 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
4844 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
4845 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
4846 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
4847 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
4848 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
4849 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
4850 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
4851 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
4852 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
4853 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_term
, NULL
);
4854 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
4855 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
4856 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
4857 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
4858 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
4859 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
4860 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
4861 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
4862 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
4863 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
4864 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
4865 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
4866 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
4867 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
4868 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
4869 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
4870 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
4871 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
4872 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
4873 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
4874 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
4875 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
4876 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
4877 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
4878 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
4879 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
4880 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
4881 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
4882 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
4883 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
4884 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
4885 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
4886 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
4887 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
4888 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
4889 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
4890 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
4891 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
4892 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
4893 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
4894 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
4895 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
4896 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
4897 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
4898 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
4899 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
4900 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
4901 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
4902 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
4903 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
4904 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
4905 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
4906 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
4907 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
4908 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
4909 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
4910 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
4912 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
4919 struct key
*kp_loop
, *kp_next
;
4921 kp_loop
= TAILQ_FIRST(&keys
);
4922 while (kp_loop
!= NULL
) {
4923 kp_next
= TAILQ_NEXT(kp_loop
, entry
);
4924 key_remove(kp_loop
);
4930 setkeymapping(char *selector
, char *value
, int flags
)
4932 char keymapping_file
[PATH_MAX
];
4933 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
4934 if (value
[0] == '~')
4935 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
4936 pwd
->pw_dir
, &value
[1]);
4938 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
4940 /* load new key bindings; if it fails, revert to default bindings */
4941 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
4945 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
4950 updatenumlockmask(void)
4953 XModifierKeymap
*modmap
;
4955 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
4957 modmap
= XGetModifierMapping(display
);
4958 for (i
= 0; i
< 8; i
++)
4959 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
4960 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
4961 == XKeysymToKeycode(display
, XK_Num_Lock
))
4962 numlockmask
= (1 << i
);
4964 XFreeModifiermap(modmap
);
4972 unsigned int modifiers
[] =
4973 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
4976 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
4977 updatenumlockmask();
4979 for (k
= 0; k
< ScreenCount(display
); k
++) {
4980 if (TAILQ_EMPTY(&screens
[k
].rl
))
4982 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
4983 TAILQ_FOREACH(kp
, &keys
, entry
) {
4984 if ((code
= XKeysymToKeycode(display
, kp
->keysym
)))
4985 for (j
= 0; j
< LENGTH(modifiers
); j
++)
4986 XGrabKey(display
, code
,
4987 kp
->mod
| modifiers
[j
],
4988 screens
[k
].root
, True
,
4989 GrabModeAsync
, GrabModeAsync
);
4995 grabbuttons(struct ws_win
*win
, int focused
)
4998 unsigned int modifiers
[] =
4999 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5001 updatenumlockmask();
5002 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
5004 for (i
= 0; i
< LENGTH(buttons
); i
++)
5005 if (buttons
[i
].action
== client_click
)
5006 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5007 XGrabButton(display
, buttons
[i
].button
,
5008 buttons
[i
].mask
| modifiers
[j
],
5009 win
->id
, False
, BUTTONMASK
,
5010 GrabModeAsync
, GrabModeSync
, None
,
5013 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
5014 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
5017 const char *quirkname
[] = {
5018 "NONE", /* config string for "no value" */
5027 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5028 #define SWM_Q_WS "\n|+ \t"
5030 parsequirks(char *qstr
, unsigned long *quirk
)
5040 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5042 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5043 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5044 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5045 DNPRINTF(SWM_D_QUIRK
,
5046 "parsequirks: %s\n", name
);
5051 *quirk
|= 1 << (i
-1);
5055 if (i
>= LENGTH(quirkname
)) {
5056 DNPRINTF(SWM_D_QUIRK
,
5057 "parsequirks: invalid quirk [%s]\n", name
);
5065 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5069 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5072 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5073 err(1, "quirk_insert: malloc");
5074 if ((qp
->class = strdup(class)) == NULL
)
5075 err(1, "quirk_insert: strdup");
5076 if ((qp
->name
= strdup(name
)) == NULL
)
5077 err(1, "quirk_insert: strdup");
5080 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5082 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5086 quirk_remove(struct quirk
*qp
)
5088 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5089 qp
->name
, qp
->quirk
);
5091 TAILQ_REMOVE(&quirks
, qp
, entry
);
5096 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5100 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5101 unsigned long quirk
)
5103 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5104 qp
->name
, qp
->quirk
);
5107 quirk_insert(class, name
, quirk
);
5109 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5113 setquirk(const char *class, const char *name
, unsigned long quirk
)
5117 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5120 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5121 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5125 quirk_replace(qp
, class, name
, quirk
);
5126 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5131 warnx("error: setquirk: cannot find class/name combination");
5135 quirk_insert(class, name
, quirk
);
5136 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5140 setconfquirk(char *selector
, char *value
, int flags
)
5142 char *cp
, *class, *name
;
5144 unsigned long quirks
;
5145 if (selector
== NULL
)
5147 if ((cp
= strchr(selector
, ':')) == NULL
)
5152 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5153 setquirk(class, name
, quirks
);
5160 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5161 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5162 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5163 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5164 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5165 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5166 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5167 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5168 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5169 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5170 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5171 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5172 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5175 /* conf file stuff */
5176 #define SWM_CONF_FILE "scrotwm.conf"
5178 enum { SWM_S_BAR_DELAY
, SWM_S_BAR_ENABLED
, SWM_S_BAR_BORDER_WIDTH
,
5179 SWM_S_STACK_ENABLED
, SWM_S_CLOCK_ENABLED
, SWM_S_CLOCK_FORMAT
,
5180 SWM_S_CYCLE_EMPTY
, SWM_S_CYCLE_VISIBLE
, SWM_S_SS_ENABLED
,
5181 SWM_S_TERM_WIDTH
, SWM_S_TITLE_CLASS_ENABLED
,
5182 SWM_S_TITLE_NAME_ENABLED
, SWM_S_WINDOW_NAME_ENABLED
, SWM_S_URGENT_ENABLED
,
5183 SWM_S_FOCUS_MODE
, SWM_S_DISABLE_BORDER
, SWM_S_BORDER_WIDTH
,
5184 SWM_S_BAR_FONT
, SWM_S_BAR_ACTION
, SWM_S_SPAWN_TERM
,
5185 SWM_S_SS_APP
, SWM_S_DIALOG_RATIO
, SWM_S_BAR_AT_BOTTOM
,
5186 SWM_S_VERBOSE_LAYOUT
, SWM_S_BAR_JUSTIFY
5190 setconfvalue(char *selector
, char *value
, int flags
)
5196 case SWM_S_BAR_DELAY
:
5197 bar_delay
= atoi(value
);
5199 case SWM_S_BAR_ENABLED
:
5200 bar_enabled
= atoi(value
);
5202 case SWM_S_BAR_BORDER_WIDTH
:
5203 bar_border_width
= atoi(value
);
5205 case SWM_S_BAR_AT_BOTTOM
:
5206 bar_at_bottom
= atoi(value
);
5208 case SWM_S_BAR_JUSTIFY
:
5209 if (!strcmp(value
, "left"))
5210 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5211 else if (!strcmp(value
, "center"))
5212 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5213 else if (!strcmp(value
, "right"))
5214 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5216 errx(1, "invalid bar_justify");
5218 case SWM_S_STACK_ENABLED
:
5219 stack_enabled
= atoi(value
);
5221 case SWM_S_CLOCK_ENABLED
:
5222 clock_enabled
= atoi(value
);
5224 case SWM_S_CLOCK_FORMAT
:
5225 #ifndef SWM_DENY_CLOCK_FORMAT
5227 if ((clock_format
= strdup(value
)) == NULL
)
5228 err(1, "setconfvalue: clock_format");
5231 case SWM_S_CYCLE_EMPTY
:
5232 cycle_empty
= atoi(value
);
5234 case SWM_S_CYCLE_VISIBLE
:
5235 cycle_visible
= atoi(value
);
5237 case SWM_S_SS_ENABLED
:
5238 ss_enabled
= atoi(value
);
5240 case SWM_S_TERM_WIDTH
:
5241 term_width
= atoi(value
);
5243 case SWM_S_TITLE_CLASS_ENABLED
:
5244 title_class_enabled
= atoi(value
);
5246 case SWM_S_WINDOW_NAME_ENABLED
:
5247 window_name_enabled
= atoi(value
);
5249 case SWM_S_TITLE_NAME_ENABLED
:
5250 title_name_enabled
= atoi(value
);
5252 case SWM_S_URGENT_ENABLED
:
5253 urgent_enabled
= atoi(value
);
5255 case SWM_S_FOCUS_MODE
:
5256 if (!strcmp(value
, "default"))
5257 focus_mode
= SWM_FOCUS_DEFAULT
;
5258 else if (!strcmp(value
, "follow_cursor"))
5259 focus_mode
= SWM_FOCUS_FOLLOW
;
5260 else if (!strcmp(value
, "synergy"))
5261 focus_mode
= SWM_FOCUS_SYNERGY
;
5263 errx(1, "focus_mode");
5265 case SWM_S_DISABLE_BORDER
:
5266 disable_border
= atoi(value
);
5268 case SWM_S_BORDER_WIDTH
:
5269 border_width
= atoi(value
);
5271 case SWM_S_BAR_FONT
:
5273 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5274 err(1, "setconfvalue: asprintf: failed to allocate "
5275 "memory for bar_fonts.");
5279 case SWM_S_BAR_ACTION
:
5281 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5282 err(1, "setconfvalue: bar_action");
5284 case SWM_S_SPAWN_TERM
:
5285 free(spawn_term
[0]);
5286 if ((spawn_term
[0] = strdup(value
)) == NULL
)
5287 err(1, "setconfvalue: spawn_term");
5291 case SWM_S_DIALOG_RATIO
:
5292 dialog_ratio
= atof(value
);
5293 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5296 case SWM_S_VERBOSE_LAYOUT
:
5297 verbose_layout
= atoi(value
);
5298 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5300 layouts
[i
].l_string
= fancy_stacker
;
5302 layouts
[i
].l_string
= plain_stacker
;
5312 setconfmodkey(char *selector
, char *value
, int flags
)
5314 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5315 update_modkey(Mod1Mask
);
5316 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5317 update_modkey(Mod2Mask
);
5318 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5319 update_modkey(Mod3Mask
);
5320 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5321 update_modkey(Mod4Mask
);
5328 setconfcolor(char *selector
, char *value
, int flags
)
5330 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5335 setconfregion(char *selector
, char *value
, int flags
)
5337 custom_region(value
);
5342 setautorun(char *selector
, char *value
, int flags
)
5352 if (getenv("SWM_STARTED"))
5356 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5357 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5359 if (ws_id
< 0 || ws_id
>= SWM_WS_MAX
)
5360 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5363 * This is a little intricate
5365 * If the pid already exists we simply reuse it because it means it was
5366 * used before AND not claimed by manage_window. We get away with
5367 * altering it in the parent after INSERT because this can not be a race
5370 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5373 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5375 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5376 err(1, "setautorun: realloc");
5377 a
.argv
[argc
- 1] = ap
;
5380 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5381 err(1, "setautorun: realloc");
5382 a
.argv
[argc
] = NULL
;
5384 if ((pid
= fork()) == 0) {
5385 spawn(ws_id
, &a
, 1);
5394 p
= calloc(1, sizeof *p
);
5397 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
5407 setlayout(char *selector
, char *value
, int flags
)
5409 int ws_id
, i
, x
, mg
, ma
, si
, raise
;
5410 int st
= SWM_V_STACK
;
5412 struct workspace
*ws
;
5414 if (getenv("SWM_STARTED"))
5418 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
5419 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
5420 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5421 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5424 if (ws_id
< 0 || ws_id
>= SWM_WS_MAX
)
5425 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
5427 if (!strcasecmp(s
, "vertical"))
5429 else if (!strcasecmp(s
, "horizontal"))
5431 else if (!strcasecmp(s
, "fullscreen"))
5434 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5435 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5438 for (i
= 0; i
< ScreenCount(display
); i
++) {
5439 ws
= (struct workspace
*)&screens
[i
].ws
;
5440 ws
[ws_id
].cur_layout
= &layouts
[st
];
5442 ws
[ws_id
].always_raise
= raise
;
5443 if (st
== SWM_MAX_STACK
)
5447 for (x
= 0; x
< abs(mg
); x
++) {
5448 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5449 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
5450 SWM_ARG_ID_MASTERSHRINK
);
5454 for (x
= 0; x
< abs(ma
); x
++) {
5455 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5456 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
5457 SWM_ARG_ID_MASTERDEL
);
5461 for (x
= 0; x
< abs(si
); x
++) {
5462 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5463 si
>= 0 ? SWM_ARG_ID_STACKINC
:
5464 SWM_ARG_ID_STACKDEC
);
5472 /* config options */
5473 struct config_option
{
5475 int (*func
)(char*, char*, int);
5478 struct config_option configopt
[] = {
5479 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
5480 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
5481 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
5482 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
5483 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
5484 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
5485 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
5486 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
5487 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
5488 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
5489 { "keyboard_mapping", setkeymapping
, 0 },
5490 { "bind", setconfbinding
, 0 },
5491 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
5492 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
5493 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
5494 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
5495 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
5496 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
5497 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
5498 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
5499 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
5500 { "modkey", setconfmodkey
, 0 },
5501 { "program", setconfspawn
, 0 },
5502 { "quirk", setconfquirk
, 0 },
5503 { "region", setconfregion
, 0 },
5504 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
5505 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
5506 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
5507 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
5508 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
5509 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
5510 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
5511 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
5512 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
5513 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
5514 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
5515 { "autorun", setautorun
, 0 },
5516 { "layout", setlayout
, 0 },
5521 conf_load(char *filename
, int keymapping
)
5524 char *line
, *cp
, *optsub
, *optval
;
5525 size_t linelen
, lineno
= 0;
5526 int wordlen
, i
, optind
;
5527 struct config_option
*opt
;
5529 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
5531 if (filename
== NULL
) {
5532 warnx("conf_load: no filename");
5535 if ((config
= fopen(filename
, "r")) == NULL
) {
5536 warn("conf_load: fopen: %s", filename
);
5540 while (!feof(config
)) {
5541 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
5544 err(1, "%s", filename
);
5549 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
5550 if (cp
[0] == '\0') {
5555 /* get config option */
5556 wordlen
= strcspn(cp
, "=[ \t\n");
5558 warnx("%s: line %zd: no option found",
5563 for (i
= 0; i
< LENGTH(configopt
); i
++) {
5564 opt
= &configopt
[i
];
5565 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
5566 strlen(opt
->optname
) == wordlen
) {
5572 warnx("%s: line %zd: unknown option %.*s",
5573 filename
, lineno
, wordlen
, cp
);
5576 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
5577 warnx("%s: line %zd: invalid option %.*s",
5578 filename
, lineno
, wordlen
, cp
);
5582 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
5583 /* get [selector] if any */
5587 wordlen
= strcspn(cp
, "]");
5590 warnx("%s: line %zd: syntax error",
5595 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
5597 warnx("%s: line %zd: unable to allocate"
5598 "memory for selector", filename
,
5604 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
5606 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
5608 optval
= strdup(cp
);
5609 /* call function to deal with it all */
5610 if (configopt
[optind
].func(optsub
, optval
,
5611 configopt
[optind
].funcflags
) != 0)
5612 errx(1, "%s: line %zd: invalid data for %s",
5613 filename
, lineno
, configopt
[optind
].optname
);
5620 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
5627 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
5633 set_child_transient(struct ws_win
*win
, Window
*trans
)
5635 struct ws_win
*parent
, *w
;
5636 XWMHints
*wmh
= NULL
;
5637 struct swm_region
*r
;
5638 struct workspace
*ws
;
5640 parent
= find_window(win
->transient
);
5642 parent
->child_trans
= win
;
5644 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
5645 " for 0x%lx trans 0x%lx\n", win
->id
, win
->transient
);
5647 if (win
->hints
== NULL
) {
5648 warnx("no hints for 0x%lx", win
->id
);
5652 r
= root_to_region(win
->wa
.root
);
5654 /* parent doen't exist in our window list */
5655 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5659 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
5660 warnx("can't get hints for 0x%lx", w
->id
);
5664 if (win
->hints
->window_group
!= wmh
->window_group
)
5667 w
->child_trans
= win
;
5668 win
->transient
= w
->id
;
5670 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
5671 "transient to 0x%lx\n", win
->transient
);
5681 window_get_pid(Window win
)
5683 Atom actual_type_return
;
5684 int actual_format_return
= 0;
5685 unsigned long nitems_return
= 0;
5686 unsigned long bytes_after_return
= 0;
5690 unsigned char *prop
= NULL
;
5692 if (XGetWindowProperty(display
, win
,
5693 XInternAtom(display
, "_NET_WM_PID", False
), 0, 1, False
,
5694 XA_CARDINAL
, &actual_type_return
, &actual_format_return
,
5695 &nitems_return
, &bytes_after_return
,
5696 (unsigned char**)(void*)&pid
) != Success
)
5698 if (actual_type_return
!= XA_CARDINAL
)
5709 if (XGetWindowProperty(display
, win
,
5710 XInternAtom(display
, "_SWM_PID", False
), 0, SWM_PROPLEN
, False
,
5711 XA_STRING
, &actual_type_return
, &actual_format_return
,
5712 &nitems_return
, &bytes_after_return
, &prop
) != Success
)
5714 if (actual_type_return
!= XA_STRING
)
5719 ret
= strtonum((const char *)prop
, 0, UINT_MAX
, &errstr
);
5720 /* ignore error because strtonum returns 0 anyway */
5727 manage_window(Window id
)
5730 struct workspace
*ws
;
5731 struct ws_win
*win
, *ww
;
5732 int format
, i
, ws_idx
, n
, border_me
= 0;
5733 unsigned long nitems
, bytes
;
5734 Atom ws_idx_atom
= 0, type
;
5735 Atom
*prot
= NULL
, *pp
;
5736 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
5737 struct swm_region
*r
;
5744 if ((win
= find_window(id
)) != NULL
)
5745 return (win
); /* already being managed */
5747 /* see if we are on the unmanaged list */
5748 if ((win
= find_unmanaged_window(id
)) != NULL
) {
5749 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
5750 "window: 0x%lx\n", win
->id
);
5751 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5752 if (win
->transient
) {
5753 set_child_transient(win
, &trans
);
5754 } if (trans
&& (ww
= find_window(trans
)))
5755 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
5757 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
5758 ewmh_update_actions(win
);
5762 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
5763 err(1, "manage_window: calloc: failed to allocate memory for "
5768 /* see if we need to override the workspace */
5769 p
= find_pid(window_get_pid(id
));
5771 /* Get all the window data in one shot */
5772 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
5774 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
5775 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
5777 XGetWindowAttributes(display
, id
, &win
->wa
);
5778 XGetWMNormalHints(display
, id
, &win
->sh
, &win
->sh_mask
);
5779 win
->hints
= XGetWMHints(display
, id
);
5780 XGetTransientForHint(display
, id
, &trans
);
5782 win
->transient
= trans
;
5783 set_child_transient(win
, &trans
);
5784 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, "
5785 "transient: 0x%lx\n", win
->id
, win
->transient
);
5788 /* get supported protocols */
5789 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
5790 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
5791 if (*pp
== takefocus
)
5792 win
->take_focus
= 1;
5794 win
->can_delete
= 1;
5800 win
->iconic
= get_iconic(win
);
5803 * Figure out where to put the window. If it was previously assigned to
5804 * a workspace (either by spawn() or manually moving), and isn't
5805 * transient, * put it in the same workspace
5807 r
= root_to_region(win
->wa
.root
);
5809 ws
= &r
->s
->ws
[p
->ws
];
5810 TAILQ_REMOVE(&pidlist
, p
, entry
);
5813 } else if (prop
&& win
->transient
== 0) {
5814 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
5815 ws_idx
= strtonum((const char *)prop
, 0, 9, &errstr
);
5817 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
5820 ws
= &r
->s
->ws
[ws_idx
];
5823 /* this should launch transients in the same ws as parent */
5825 if ((ww
= find_window(trans
)) != NULL
)
5831 warnx("manage_window: fix this "
5837 /* set up the window layout */
5840 win
->s
= r
->s
; /* this never changes */
5841 if (trans
&& (ww
= find_window(trans
)))
5842 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
5844 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
5846 WIDTH(win
) = win
->wa
.width
;
5847 HEIGHT(win
) = win
->wa
.height
;
5850 win
->g_floatvalid
= 0;
5851 win
->floatmaxed
= 0;
5852 win
->ewmh_flags
= 0;
5854 /* Set window properties so we can remember this after reincarnation */
5855 if (ws_idx_atom
&& prop
== NULL
&&
5856 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
5858 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
5860 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
5861 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
5866 ewmh_autoquirk(win
);
5868 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
5869 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
5870 win
->ch
.res_class
, win
->ch
.res_name
);
5872 /* java is retarded so treat it special */
5873 if (strstr(win
->ch
.res_name
, "sun-awt")) {
5878 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5879 if (!strcmp(win
->ch
.res_class
, qp
->class) &&
5880 !strcmp(win
->ch
.res_name
, qp
->name
)) {
5881 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
5882 "class: %s, name: %s\n", win
->ch
.res_class
,
5884 if (qp
->quirk
& SWM_Q_FLOAT
) {
5888 win
->quirks
= qp
->quirk
;
5893 /* alter window position if quirky */
5894 if (win
->quirks
& SWM_Q_ANYWHERE
) {
5895 win
->manual
= 1; /* don't center the quirky windows */
5896 bzero(&wc
, sizeof wc
);
5898 if (bar_enabled
&& Y(win
) < bar_height
) {
5899 Y(win
) = wc
.y
= bar_height
;
5902 if (WIDTH(win
) + X(win
) > WIDTH(r
)) {
5903 X(win
) = wc
.x
= WIDTH(r
) - WIDTH(win
) - 2;
5909 /* Reset font sizes (the bruteforce way; no default keybinding). */
5910 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
5911 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
5912 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
5913 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
5914 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
5917 ewmh_get_win_state(win
);
5918 ewmh_update_actions(win
);
5919 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
5923 bzero(&wc
, sizeof wc
);
5924 wc
.border_width
= border_width
;
5925 mask
= CWBorderWidth
;
5926 XConfigureWindow(display
, win
->id
, mask
, &wc
);
5929 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
5930 PropertyChangeMask
| StructureNotifyMask
);
5932 /* floaters need to be mapped if they are in the current workspace */
5933 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
5934 XMapRaised(display
, win
->id
);
5940 free_window(struct ws_win
*win
)
5942 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%lx\n", win
->id
);
5947 /* needed for restart wm */
5948 set_win_state(win
, WithdrawnState
);
5950 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5952 if (win
->ch
.res_class
)
5953 XFree(win
->ch
.res_class
);
5954 if (win
->ch
.res_name
)
5955 XFree(win
->ch
.res_name
);
5960 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
5966 unmanage_window(struct ws_win
*win
)
5968 struct ws_win
*parent
;
5973 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%lx\n", win
->id
);
5975 if (win
->transient
) {
5976 parent
= find_window(win
->transient
);
5978 parent
->child_trans
= NULL
;
5981 /* focus on root just in case */
5982 XSetInputFocus(display
, PointerRoot
, PointerRoot
, CurrentTime
);
5991 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
5992 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
5998 focus_magic(struct ws_win
*win
)
6000 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%lx\n", WINID(win
));
6005 if (win
->child_trans
) {
6006 /* win = parent & has a transient so focus on that */
6008 focus_win(win
->child_trans
);
6009 if (win
->child_trans
->take_focus
)
6010 client_msg(win
, takefocus
);
6012 /* make sure transient hasn't dissapeared */
6013 if (validate_win(win
->child_trans
) == 0) {
6014 focus_win(win
->child_trans
);
6015 if (win
->child_trans
->take_focus
)
6016 client_msg(win
->child_trans
, takefocus
);
6018 win
->child_trans
= NULL
;
6020 if (win
->take_focus
)
6021 client_msg(win
, takefocus
);
6027 if (win
->take_focus
)
6028 client_msg(win
, takefocus
);
6035 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%lx\n", e
->xexpose
.window
);
6042 XKeyEvent
*ev
= &e
->xkey
;
6045 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
6046 TAILQ_FOREACH(kp
, &keys
, entry
)
6047 if (keysym
== kp
->keysym
6048 && CLEANMASK(kp
->mod
) == CLEANMASK(ev
->state
)
6049 && keyfuncs
[kp
->funcid
].func
) {
6050 if (kp
->funcid
== kf_spawn_custom
)
6052 root_to_region(ev
->root
),
6053 &(keyfuncs
[kp
->funcid
].args
),
6057 keyfuncs
[kp
->funcid
].func(
6058 root_to_region(ev
->root
),
6059 &(keyfuncs
[kp
->funcid
].args
)
6065 buttonpress(XEvent
*e
)
6069 XButtonPressedEvent
*ev
= &e
->xbutton
;
6071 if ((win
= find_window(ev
->window
)) == NULL
)
6075 action
= client_click
;
6077 for (i
= 0; i
< LENGTH(buttons
); i
++)
6078 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6079 buttons
[i
].button
== ev
->button
&&
6080 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
6081 buttons
[i
].func(win
, &buttons
[i
].args
);
6085 configurerequest(XEvent
*e
)
6087 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
6092 if ((win
= find_window(ev
->window
)) == NULL
)
6093 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6096 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%lx, new: %s\n",
6097 ev
->window
, YESNO(new));
6100 bzero(&wc
, sizeof wc
);
6103 wc
.width
= ev
->width
;
6104 wc
.height
= ev
->height
;
6105 wc
.border_width
= ev
->border_width
;
6106 wc
.sibling
= ev
->above
;
6107 wc
.stack_mode
= ev
->detail
;
6108 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
6110 config_win(win
, ev
);
6115 configurenotify(XEvent
*e
)
6119 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%lx\n",
6120 e
->xconfigure
.window
);
6122 win
= find_window(e
->xconfigure
.window
);
6124 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &win
->sh_mask
);
6128 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6129 drain_enter_notify();
6134 destroynotify(XEvent
*e
)
6137 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
6139 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%lx\n", ev
->window
);
6141 if ((win
= find_window(ev
->window
)) == NULL
) {
6142 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6148 /* make sure we focus on something */
6151 unmanage_window(win
);
6153 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6154 drain_enter_notify();
6159 enternotify(XEvent
*e
)
6161 XCrossingEvent
*ev
= &e
->xcrossing
;
6166 Window focus_return
;
6167 int revert_to_return
;
6169 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%lx, mode: %d, detail: "
6170 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6171 "state: %d\n", ev
->window
, ev
->mode
, ev
->detail
, ev
->root
,
6172 ev
->subwindow
, YESNO(ev
->same_screen
), YESNO(ev
->focus
), ev
->state
);
6174 if (ev
->mode
!= NotifyNormal
) {
6175 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6180 switch (focus_mode
) {
6181 case SWM_FOCUS_DEFAULT
:
6183 case SWM_FOCUS_FOLLOW
:
6185 case SWM_FOCUS_SYNERGY
:
6188 * all these checks need to be in this order because the
6189 * XCheckTypedWindowEvent relies on weeding out the previous events
6191 * making this code an option would enable a follow mouse for focus
6196 * state is set when we are switching workspaces and focus is set when
6197 * the window or a subwindow already has focus (occurs during restart).
6199 * Only honor the focus flag if last_focus_event is not FocusOut,
6200 * this allows scrotwm to continue to control focus when another
6201 * program is also playing with it.
6203 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6204 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6209 * happens when a window is created or destroyed and the border
6210 * crosses the mouse pointer and when switching ws
6212 * we need the subwindow test to see if we came from root in order
6213 * to give focus to floaters
6215 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6216 ev
->subwindow
== 0) {
6217 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6221 /* this window already has focus */
6222 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6223 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6227 /* this window is being deleted or moved to another ws */
6228 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6230 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6231 XPutBackEvent(display
, &cne
);
6235 if ((win
= find_window(ev
->window
)) == NULL
) {
6236 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6241 * In fullstack kill all enters unless they come from a different ws
6242 * (i.e. another region) or focus has been grabbed externally.
6244 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6245 last_focus_event
!= FocusOut
) {
6246 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6247 if ((w
= find_window(focus_return
)) == NULL
||
6249 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6257 if ((win
= find_window(ev
->window
)) == NULL
) {
6258 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6263 * if we have more enternotifies let them handle it in due time
6265 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
6266 DNPRINTF(SWM_D_EVENT
,
6267 "ignoring enternotify: got more enternotify\n");
6268 XPutBackEvent(display
, &cne
);
6275 /* lets us use one switch statement for arbitrary mode/detail combinations */
6276 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6279 focusevent(XEvent
*e
)
6283 u_int32_t mode_detail
;
6284 XFocusChangeEvent
*ev
= &e
->xfocus
;
6286 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: 0x%lx mode: %d "
6287 "detail: %d\n", ev
->type
== FocusIn
? "entering" : "leaving",
6288 ev
->window
, ev
->mode
, ev
->detail
);
6290 if (last_focus_event
== ev
->type
) {
6291 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
6295 last_focus_event
= ev
->type
;
6296 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
6298 switch (mode_detail
) {
6299 /* synergy client focus operations */
6300 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
6301 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
6303 /* synergy server focus operations */
6304 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
6306 /* Entering applications like rdesktop that mangle the pointer */
6307 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
6309 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
6310 XSetWindowBorder(display
, win
->id
,
6311 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
6312 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
6315 warnx("ignoring focusevent");
6316 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
6323 mapnotify(XEvent
*e
)
6326 XMapEvent
*ev
= &e
->xmap
;
6328 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%lx\n", ev
->window
);
6330 win
= manage_window(ev
->window
);
6332 set_win_state(win
, NormalState
);
6336 mappingnotify(XEvent
*e
)
6338 XMappingEvent
*ev
= &e
->xmapping
;
6340 XRefreshKeyboardMapping(ev
);
6341 if (ev
->request
== MappingKeyboard
)
6346 maprequest(XEvent
*e
)
6349 struct swm_region
*r
;
6350 XWindowAttributes wa
;
6351 XMapRequestEvent
*ev
= &e
->xmaprequest
;
6353 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%lx\n",
6354 e
->xmaprequest
.window
);
6356 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
6358 if (wa
.override_redirect
)
6361 win
= manage_window(e
->xmaprequest
.window
);
6363 return; /* can't happen */
6367 /* make new win focused */
6368 r
= root_to_region(win
->wa
.root
);
6369 if (win
->ws
== r
->ws
)
6374 propertynotify(XEvent
*e
)
6377 XPropertyEvent
*ev
= &e
->xproperty
;
6380 name
= XGetAtomName(display
, ev
->atom
);
6381 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%lx, atom: %s\n",
6386 win
= find_window(ev
->window
);
6390 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
6391 update_iconic(win
, 0);
6392 XMapRaised(display
, win
->id
);
6400 case XA_WM_NORMAL_HINTS
:
6402 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
6403 warnx("normal hints: flag 0x%x", win
->sh
.flags
);
6404 if (win
->sh
.flags
& PMinSize
) {
6405 WIDTH(win
) = win
->sh
.min_width
;
6406 HEIGHT(win
) = win
->sh
.min_height
;
6407 warnx("min %d %d", WIDTH(win
), HEIGHT(win
));
6409 XMoveResizeWindow(display
, win
->id
,
6410 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
));
6413 if (title_name_enabled
|| title_class_enabled
)
6417 if (window_name_enabled
)
6426 unmapnotify(XEvent
*e
)
6430 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%lx\n", e
->xunmap
.window
);
6432 /* determine if we need to help unmanage this window */
6433 win
= find_window(e
->xunmap
.window
);
6437 if (getstate(e
->xunmap
.window
) == NormalState
) {
6438 unmanage_window(win
);
6441 /* giant hack for apps that don't destroy transient windows */
6442 /* eat a bunch of events to prevent remanaging the window */
6444 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6445 EnterWindowMask
, &cne
))
6447 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6448 StructureNotifyMask
, &cne
))
6450 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6451 SubstructureNotifyMask
, &cne
))
6453 /* resend unmap because we ated it */
6454 XUnmapWindow(display
, e
->xunmap
.window
);
6457 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6458 drain_enter_notify();
6462 visibilitynotify(XEvent
*e
)
6465 struct swm_region
*r
;
6467 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%lx\n",
6468 e
->xvisibility
.window
);
6469 if (e
->xvisibility
.state
== VisibilityUnobscured
)
6470 for (i
= 0; i
< ScreenCount(display
); i
++)
6471 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6472 if (e
->xvisibility
.window
== r
->bar_window
)
6477 clientmessage(XEvent
*e
)
6479 XClientMessageEvent
*ev
;
6484 win
= find_window(ev
->window
);
6488 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx, type: %ld\n",
6489 ev
->window
, ev
->message_type
);
6491 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
6492 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
6495 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
6496 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
6497 if (win
->can_delete
)
6498 client_msg(win
, adelete
);
6500 XKillClient(display
, win
->id
);
6502 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
6503 DNPRINTF(SWM_D_EVENT
,
6504 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
6505 if (win
->floating
) {
6506 if (ev
->data
.l
[0] & (1<<8)) /* x */
6507 X(win
) = ev
->data
.l
[1];
6508 if (ev
->data
.l
[0] & (1<<9)) /* y */
6509 Y(win
) = ev
->data
.l
[2];
6510 if (ev
->data
.l
[0] & (1<<10)) /* width */
6511 WIDTH(win
) = ev
->data
.l
[3];
6512 if (ev
->data
.l
[0] & (1<<11)) /* height */
6513 HEIGHT(win
) = ev
->data
.l
[4];
6518 /* TODO: Change stack sizes */
6519 /* notify no change was made. */
6520 config_win(win
, NULL
);
6523 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
6524 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
6525 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
6527 ewmh_update_win_state(win
, ev
->data
.l
[2],
6535 xerror_start(Display
*d
, XErrorEvent
*ee
)
6542 xerror(Display
*d
, XErrorEvent
*ee
)
6544 /* warnx("error: %p %p", display, ee); */
6552 xerrorxlib
= XSetErrorHandler(xerror_start
);
6554 /* this causes an error if some other window manager is running */
6555 XSelectInput(display
, DefaultRootWindow(display
),
6556 SubstructureRedirectMask
);
6557 XSync(display
, False
);
6561 XSetErrorHandler(xerror
);
6562 XSync(display
, False
);
6567 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
6569 struct swm_region
*r
, *n
;
6570 struct workspace
*ws
= NULL
;
6573 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
6574 s
->idx
, w
, h
, x
, y
);
6576 /* remove any conflicting regions */
6577 n
= TAILQ_FIRST(&s
->rl
);
6580 n
= TAILQ_NEXT(r
, entry
);
6581 if (X(r
) < (x
+ w
) &&
6582 (X(r
) + WIDTH(r
)) > x
&&
6584 (Y(r
) + HEIGHT(r
)) > y
) {
6585 if (r
->ws
->r
!= NULL
)
6586 r
->ws
->old_r
= r
->ws
->r
;
6588 XDestroyWindow(display
, r
->bar_window
);
6589 TAILQ_REMOVE(&s
->rl
, r
, entry
);
6590 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
6594 /* search old regions for one to reuse */
6596 /* size + location match */
6597 TAILQ_FOREACH(r
, &s
->orl
, entry
)
6598 if (X(r
) == x
&& Y(r
) == y
&&
6599 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
6603 TAILQ_FOREACH(r
, &s
->orl
, entry
)
6604 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
6608 TAILQ_REMOVE(&s
->orl
, r
, entry
);
6609 /* try to use old region's workspace */
6610 if (r
->ws
->r
== NULL
)
6613 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
6614 err(1, "new_region: calloc: failed to allocate memory "
6617 /* if we don't have a workspace already, find one */
6619 for (i
= 0; i
< SWM_WS_MAX
; i
++)
6620 if (s
->ws
[i
].r
== NULL
) {
6627 errx(1, "new_region: no free workspaces");
6638 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
6644 #ifdef SWM_XRR_HAS_CRTC
6646 XRRScreenResources
*sr
;
6649 #endif /* SWM_XRR_HAS_CRTC */
6650 struct swm_region
*r
;
6653 if (i
>= ScreenCount(display
))
6654 errx(1, "scan_xrandr: invalid screen");
6656 /* remove any old regions */
6657 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
6658 r
->ws
->old_r
= r
->ws
->r
= NULL
;
6659 XDestroyWindow(display
, r
->bar_window
);
6660 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
6661 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
6665 /* map virtual screens onto physical screens */
6666 #ifdef SWM_XRR_HAS_CRTC
6667 if (xrandr_support
) {
6668 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
6670 new_region(&screens
[i
], 0, 0,
6671 DisplayWidth(display
, i
),
6672 DisplayHeight(display
, i
));
6676 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
6677 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
6678 if (ci
->noutput
== 0)
6681 if (ci
!= NULL
&& ci
->mode
== None
)
6682 new_region(&screens
[i
], 0, 0,
6683 DisplayWidth(display
, i
),
6684 DisplayHeight(display
, i
));
6686 new_region(&screens
[i
],
6687 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
6690 XRRFreeCrtcInfo(ci
);
6691 XRRFreeScreenResources(sr
);
6693 #endif /* SWM_XRR_HAS_CRTC */
6695 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
6696 DisplayHeight(display
, i
));
6701 screenchange(XEvent
*e
) {
6702 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
6703 struct swm_region
*r
;
6706 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%lx\n", xe
->root
);
6708 if (!XRRUpdateConfiguration(e
))
6711 /* silly event doesn't include the screen index */
6712 for (i
= 0; i
< ScreenCount(display
); i
++)
6713 if (screens
[i
].root
== xe
->root
)
6715 if (i
>= ScreenCount(display
))
6716 errx(1, "screenchange: screen not found");
6718 /* brute force for now, just re-enumerate the regions */
6721 /* add bars to all regions */
6722 for (i
= 0; i
< ScreenCount(display
); i
++)
6723 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6726 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6727 drain_enter_notify();
6733 Window d1
, d2
, *wins
= NULL
;
6734 XWindowAttributes wa
;
6739 for (i
= 0; i
< ScreenCount(display
); i
++) {
6740 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
6743 /* attach windows to a region */
6744 /* normal windows */
6745 for (j
= 0; j
< no
; j
++) {
6746 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6747 wa
.override_redirect
||
6748 XGetTransientForHint(display
, wins
[j
], &d1
))
6751 state
= getstate(wins
[j
]);
6752 manage
= state
== IconicState
;
6753 if (wa
.map_state
== IsViewable
|| manage
)
6754 manage_window(wins
[j
]);
6756 /* transient windows */
6757 for (j
= 0; j
< no
; j
++) {
6758 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6759 wa
.override_redirect
)
6762 state
= getstate(wins
[j
]);
6763 manage
= state
== IconicState
;
6764 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
6766 manage_window(wins
[j
]);
6779 int errorbase
, major
, minor
;
6780 struct workspace
*ws
;
6783 if ((screens
= calloc(ScreenCount(display
),
6784 sizeof(struct swm_screen
))) == NULL
)
6785 err(1, "setup_screens: calloc: failed to allocate memory for "
6788 /* initial Xrandr setup */
6789 xrandr_support
= XRRQueryExtension(display
,
6790 &xrandr_eventbase
, &errorbase
);
6792 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
6795 /* map physical screens */
6796 for (i
= 0; i
< ScreenCount(display
); i
++) {
6797 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
6799 TAILQ_INIT(&screens
[i
].rl
);
6800 TAILQ_INIT(&screens
[i
].orl
);
6801 screens
[i
].root
= RootWindow(display
, i
);
6803 /* set default colors */
6804 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
6805 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
6806 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
6807 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
6808 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
6810 /* create graphics context on screen with default font color */
6811 screens
[i
].bar_gc
= XCreateGC(display
, screens
[i
].root
, 0,
6814 XSetForeground(display
, screens
[i
].bar_gc
,
6815 screens
[i
].c
[SWM_S_COLOR_BAR_FONT
].color
);
6817 /* set default cursor */
6818 XDefineCursor(display
, screens
[i
].root
,
6819 XCreateFontCursor(display
, XC_left_ptr
));
6821 /* init all workspaces */
6822 /* XXX these should be dynamically allocated too */
6823 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
6824 ws
= &screens
[i
].ws
[j
];
6830 TAILQ_INIT(&ws
->winlist
);
6831 TAILQ_INIT(&ws
->unmanagedlist
);
6833 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
6834 if (layouts
[k
].l_config
!= NULL
)
6835 layouts
[k
].l_config(ws
,
6836 SWM_ARG_ID_STACKINIT
);
6837 ws
->cur_layout
= &layouts
[0];
6838 ws
->cur_layout
->l_string(ws
);
6844 XRRSelectInput(display
, screens
[i
].root
,
6845 RRScreenChangeNotifyMask
);
6852 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
6853 err(1, "setup_globals: strdup: failed to allocate memory.");
6855 if ((spawn_term
[0] = strdup("xterm")) == NULL
)
6856 err(1, "setup_globals: strdup: failed to allocate memory.");
6858 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
6859 err(1, "setup_globals: strdup: failed to allocate memory.");
6866 Atom netwmcheck
, netwmname
, utf8_string
;
6869 /* work around sun jdk bugs, code from wmname */
6870 netwmcheck
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
6871 netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
6872 utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6873 for (i
= 0; i
< ScreenCount(display
); i
++) {
6874 root
= screens
[i
].root
;
6875 win
= XCreateSimpleWindow(display
,root
, 0, 0, 1, 1, 0,
6876 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
,
6877 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
);
6879 XChangeProperty(display
, root
, netwmcheck
, XA_WINDOW
, 32,
6880 PropModeReplace
, (unsigned char *)&win
,1);
6881 XChangeProperty(display
, win
, netwmcheck
, XA_WINDOW
, 32,
6882 PropModeReplace
, (unsigned char *)&win
,1);
6883 XChangeProperty(display
, win
, netwmname
, utf8_string
, 8,
6884 PropModeReplace
, (unsigned char*)"LG3D", strlen("LG3D"));
6889 main(int argc
, char *argv
[])
6891 struct swm_region
*r
, *rr
;
6892 struct ws_win
*winfocus
= NULL
;
6895 char conf
[PATH_MAX
], *cfile
= NULL
;
6900 struct sigaction sact
;
6903 warnx("Welcome to scrotwm V%s Build: %s", SCROTWM_VERSION
, buildstr
);
6904 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
6906 warnx("no locale support");
6908 if (!X_HAVE_UTF8_STRING
)
6909 warnx("no UTF-8 support");
6911 if (!(display
= XOpenDisplay(0)))
6912 errx(1, "can not open display");
6915 errx(1, "other wm running");
6917 /* handle some signals */
6918 bzero(&sact
, sizeof(sact
));
6919 sigemptyset(&sact
.sa_mask
);
6921 sact
.sa_handler
= sighdlr
;
6922 sigaction(SIGINT
, &sact
, NULL
);
6923 sigaction(SIGQUIT
, &sact
, NULL
);
6924 sigaction(SIGTERM
, &sact
, NULL
);
6925 sigaction(SIGHUP
, &sact
, NULL
);
6927 sact
.sa_handler
= sighdlr
;
6928 sact
.sa_flags
= SA_NOCLDSTOP
;
6929 sigaction(SIGCHLD
, &sact
, NULL
);
6931 astate
= XInternAtom(display
, "WM_STATE", False
);
6932 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
6933 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
6934 takefocus
= XInternAtom(display
, "WM_TAKE_FOCUS", False
);
6935 a_wmname
= XInternAtom(display
, "WM_NAME", False
);
6936 a_netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
6937 a_utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6938 a_string
= XInternAtom(display
, "STRING", False
);
6939 a_swm_iconic
= XInternAtom(display
, "_SWM_ICONIC", False
);
6941 /* look for local and global conf file */
6942 pwd
= getpwuid(getuid());
6944 errx(1, "invalid user: %d", getuid());
6953 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
6954 if (stat(conf
, &sb
) != -1) {
6955 if (S_ISREG(sb
.st_mode
))
6958 /* try global conf file */
6959 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
6960 if (!stat(conf
, &sb
))
6961 if (S_ISREG(sb
.st_mode
))
6965 /* load conf (if any) and refresh font color in bar graphics contexts */
6966 if (cfile
&& conf_load(cfile
, SWM_CONF_DEFAULT
) == 0)
6967 for (i
= 0; i
< ScreenCount(display
); ++i
)
6968 XSetForeground(display
, screens
[i
].bar_gc
,
6969 screens
[i
].c
[SWM_S_COLOR_BAR_FONT
].color
);
6972 /* set some values to work around bad programs */
6974 /* grab existing windows (before we build the bars) */
6977 if (getenv("SWM_STARTED") == NULL
)
6978 setenv("SWM_STARTED", "YES", 1);
6980 /* setup all bars */
6981 for (i
= 0; i
< ScreenCount(display
); i
++)
6982 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
6983 if (winfocus
== NULL
)
6984 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
6992 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6993 drain_enter_notify();
6995 xfd
= ConnectionNumber(display
);
6997 while (XPending(display
)) {
6998 XNextEvent(display
, &e
);
7001 if (e
.type
< LASTEvent
) {
7002 DNPRINTF(SWM_D_EVENTQ
,"XEvent: handled: %s, "
7003 "window: 0x%lx, type: %s (%d), %d remaining"
7004 "\n", YESNO(handler
[e
.type
]),
7005 e
.xany
.window
, geteventname(&e
),
7006 e
.type
, QLength(display
));
7008 if (handler
[e
.type
])
7009 handler
[e
.type
](&e
);
7011 DNPRINTF(SWM_D_EVENTQ
, "XRandr Event: window: "
7012 "0x%lx, type: %s (%d)\n", e
.xany
.window
,
7013 xrandr_geteventname(&e
), e
.type
);
7015 switch (e
.type
- xrandr_eventbase
) {
7016 case RRScreenChangeNotify
:
7025 /* if we are being restarted go focus on first window */
7027 rr
= winfocus
->ws
->r
;
7029 /* not a visible window */
7033 /* move pointer to first screen if multi screen */
7034 if (ScreenCount(display
) > 1 || outputs
> 1)
7035 XWarpPointer(display
, None
, rr
->s
[0].root
,
7037 Y(rr
) + (bar_enabled
? bar_height
: 0));
7039 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7049 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7051 DNPRINTF(SWM_D_MISC
, "select failed");
7052 if (restart_wm
== 1)
7053 restart(NULL
, NULL
);
7054 if (search_resp
== 1)
7067 for (i
= 0; i
< ScreenCount(display
); ++i
)
7068 if (screens
[i
].bar_gc
!= NULL
)
7069 XFreeGC(display
, screens
[i
].bar_gc
);
7071 XFreeFontSet(display
, bar_fs
);
7072 XCloseDisplay(display
);