3 * Copyright (c) 2009-2010-2011 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009-2010-2011 Ryan McBride <mcbride@countersiege.com>
5 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
6 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Much code and ideas taken from dwm under the following license:
23 * MIT/X Consortium License
25 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
26 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
27 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
28 * 2007 Premysl Hruby <dfenze at gmail dot com>
29 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
30 * 2007 Christof Musik <christof at sendfax dot de>
31 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
32 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
33 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
35 * Permission is hereby granted, free of charge, to any person obtaining a
36 * copy of this software and associated documentation files (the "Software"),
37 * to deal in the Software without restriction, including without limitation
38 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
39 * and/or sell copies of the Software, and to permit persons to whom the
40 * Software is furnished to do so, subject to the following conditions:
42 * The above copyright notice and this permission notice shall be included in
43 * all copies or substantial portions of the Software.
45 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
48 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
50 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
51 * DEALINGS IN THE SOFTWARE.
54 static const char *cvstag
=
57 #define SWM_VERSION "0.9.32"
74 #include <sys/types.h>
78 #include <sys/queue.h>
79 #include <sys/param.h>
80 #include <sys/select.h>
82 #include <X11/cursorfont.h>
83 #include <X11/keysym.h>
84 #include <X11/Xatom.h>
86 #include <X11/Xproto.h>
87 #include <X11/Xutil.h>
88 #include <X11/extensions/Xrandr.h>
95 # error XRandR versions less than 1.0 are not supported
100 #define SWM_XRR_HAS_CRTC
104 /*#define SWM_DEBUG*/
106 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
107 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
108 #define SWM_D_MISC 0x0001
109 #define SWM_D_EVENT 0x0002
110 #define SWM_D_WS 0x0004
111 #define SWM_D_FOCUS 0x0008
112 #define SWM_D_MOVE 0x0010
113 #define SWM_D_STACK 0x0020
114 #define SWM_D_MOUSE 0x0040
115 #define SWM_D_PROP 0x0080
116 #define SWM_D_CLASS 0x0100
117 #define SWM_D_KEY 0x0200
118 #define SWM_D_QUIRK 0x0400
119 #define SWM_D_SPAWN 0x0800
120 #define SWM_D_EVENTQ 0x1000
121 #define SWM_D_CONF 0x2000
123 u_int32_t swm_debug
= 0
140 #define DPRINTF(x...)
141 #define DNPRINTF(n,x...)
144 #define LENGTH(x) (sizeof x / sizeof x[0])
145 #define MODKEY Mod1Mask
146 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
147 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
148 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
149 #define SWM_PROPLEN (16)
150 #define SWM_FUNCNAME_LEN (32)
151 #define SWM_KEYS_LEN (255)
152 #define SWM_QUIRK_LEN (64)
153 #define X(r) (r)->g.x
154 #define Y(r) (r)->g.y
155 #define WIDTH(r) (r)->g.w
156 #define HEIGHT(r) (r)->g.h
157 #define SWM_MAX_FONT_STEPS (3)
158 #define WINID(w) (w ? w->id : 0)
160 #define SWM_FOCUS_DEFAULT (0)
161 #define SWM_FOCUS_SYNERGY (1)
162 #define SWM_FOCUS_FOLLOW (2)
165 #define SWM_LIB "/usr/local/lib/libswmhack.so"
177 volatile sig_atomic_t running
= 1;
178 volatile sig_atomic_t restart_wm
= 0;
180 int last_focus_event
= FocusOut
;
181 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
185 int xrandr_eventbase
;
186 unsigned int numlockmask
= 0;
190 int cycle_visible
= 0;
192 int font_adjusted
= 0;
193 unsigned int mod_key
= MODKEY
;
196 struct swm_region
*search_r
;
197 int select_list_pipe
[2];
198 int select_resp_pipe
[2];
200 volatile sig_atomic_t search_resp
;
203 double dialog_ratio
= .6;
205 #define SWM_BAR_MAX (256)
206 char *bar_argv
[] = { NULL
, NULL
};
208 char bar_ext
[SWM_BAR_MAX
];
209 char bar_vertext
[SWM_BAR_MAX
];
211 sig_atomic_t bar_alarm
= 0;
214 int bar_border_width
= 1;
215 int bar_at_bottom
= 0;
217 int bar_extra_running
= 0;
220 int stack_enabled
= 1;
221 int clock_enabled
= 1;
222 char *clock_format
= NULL
;
223 int title_name_enabled
= 0;
224 int title_class_enabled
= 0;
225 int window_name_enabled
= 0;
226 int focus_mode
= SWM_FOCUS_DEFAULT
;
227 int disable_border
= 0;
228 int border_width
= 1;
234 char *bar_fonts
[] = { NULL
, NULL
, NULL
, NULL
};/* XXX Make fully dynamic */
235 char *spawn_term
[] = { NULL
, NULL
}; /* XXX Make fully dynamic */
237 #define SWM_MENU_FN (2)
238 #define SWM_MENU_NB (4)
239 #define SWM_MENU_NF (6)
240 #define SWM_MENU_SB (8)
241 #define SWM_MENU_SF (10)
243 /* layout manager data */
244 struct swm_geometry
{
254 /* virtual "screens" */
256 TAILQ_ENTRY(swm_region
) entry
;
257 struct swm_geometry g
;
258 struct workspace
*ws
; /* current workspace on this region */
259 struct workspace
*ws_prior
; /* prior workspace on this region */
260 struct swm_screen
*s
; /* screen idx */
263 TAILQ_HEAD(swm_region_list
, swm_region
);
266 TAILQ_ENTRY(ws_win
) entry
;
269 struct ws_win
*child_trans
; /* transient child window */
270 struct swm_geometry g
; /* current geometry */
271 struct swm_geometry g_float
; /* geometry when floating */
272 struct swm_geometry rg_float
; /* region geom when floating */
273 int g_floatvalid
; /* flag: geometry in g_float is valid */
274 int floatmaxed
; /* flag: floater was maxed in max_stack */
278 unsigned int ewmh_flags
;
279 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
285 unsigned long quirks
;
286 struct workspace
*ws
; /* always valid */
287 struct swm_screen
*s
; /* always valid, never changes */
288 XWindowAttributes wa
;
293 TAILQ_HEAD(ws_win_list
, ws_win
);
297 TAILQ_ENTRY(pid_e
) entry
;
301 TAILQ_HEAD(pid_list
, pid_e
);
302 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
304 /* layout handlers */
306 void vertical_config(struct workspace
*, int);
307 void vertical_stack(struct workspace
*, struct swm_geometry
*);
308 void horizontal_config(struct workspace
*, int);
309 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
310 void max_stack(struct workspace
*, struct swm_geometry
*);
312 struct ws_win
*find_window(Window
);
314 void grabbuttons(struct ws_win
*, int);
315 void new_region(struct swm_screen
*, int, int, int, int);
316 void unmanage_window(struct ws_win
*);
317 long getstate(Window
);
320 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
321 void (*l_config
)(struct workspace
*, int);
323 #define SWM_L_FOCUSPREV (1<<0)
324 #define SWM_L_MAPONFOCUS (1<<1)
327 /* stack, configure */
328 { vertical_stack
, vertical_config
, 0, "[|]" },
329 { horizontal_stack
, horizontal_config
, 0, "[-]" },
331 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, "[ ]" },
332 { NULL
, NULL
, 0, NULL
},
335 /* position of max_stack mode in the layouts array, index into layouts! */
336 #define SWM_V_STACK (0)
337 #define SWM_H_STACK (1)
338 #define SWM_MAX_STACK (2)
340 #define SWM_H_SLICE (32)
341 #define SWM_V_SLICE (32)
343 /* define work spaces */
345 int idx
; /* workspace index */
346 struct layout
*cur_layout
; /* current layout handlers */
347 struct ws_win
*focus
; /* may be NULL */
348 struct ws_win
*focus_prev
; /* may be NULL */
349 struct swm_region
*r
; /* may be NULL */
350 struct swm_region
*old_r
; /* may be NULL */
351 struct ws_win_list winlist
; /* list of windows in ws */
352 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
356 int horizontal_msize
;
358 int horizontal_stacks
;
365 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
366 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
368 /* physical screen mapping */
369 #define SWM_WS_MAX (10)
371 int idx
; /* screen index */
372 struct swm_region_list rl
; /* list of regions on this screen */
373 struct swm_region_list orl
; /* list of old regions */
375 struct workspace ws
[SWM_WS_MAX
];
381 } c
[SWM_S_COLOR_MAX
];
383 struct swm_screen
*screens
;
386 /* args to functions */
389 #define SWM_ARG_ID_FOCUSNEXT (0)
390 #define SWM_ARG_ID_FOCUSPREV (1)
391 #define SWM_ARG_ID_FOCUSMAIN (2)
392 #define SWM_ARG_ID_FOCUSCUR (4)
393 #define SWM_ARG_ID_SWAPNEXT (10)
394 #define SWM_ARG_ID_SWAPPREV (11)
395 #define SWM_ARG_ID_SWAPMAIN (12)
396 #define SWM_ARG_ID_MOVELAST (13)
397 #define SWM_ARG_ID_MASTERSHRINK (20)
398 #define SWM_ARG_ID_MASTERGROW (21)
399 #define SWM_ARG_ID_MASTERADD (22)
400 #define SWM_ARG_ID_MASTERDEL (23)
401 #define SWM_ARG_ID_STACKRESET (30)
402 #define SWM_ARG_ID_STACKINIT (31)
403 #define SWM_ARG_ID_CYCLEWS_UP (40)
404 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
405 #define SWM_ARG_ID_CYCLESC_UP (42)
406 #define SWM_ARG_ID_CYCLESC_DOWN (43)
407 #define SWM_ARG_ID_STACKINC (50)
408 #define SWM_ARG_ID_STACKDEC (51)
409 #define SWM_ARG_ID_SS_ALL (60)
410 #define SWM_ARG_ID_SS_WINDOW (61)
411 #define SWM_ARG_ID_DONTCENTER (70)
412 #define SWM_ARG_ID_CENTER (71)
413 #define SWM_ARG_ID_KILLWINDOW (80)
414 #define SWM_ARG_ID_DELETEWINDOW (81)
418 void focus(struct swm_region
*, union arg
*);
419 void focus_magic(struct ws_win
*);
426 #define SWM_Q_FLOAT (1<<0) /* float this window */
427 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
428 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
429 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
430 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
431 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
433 int quirks_size
= 0, quirks_length
= 0;
434 struct quirk
*quirks
= NULL
;
437 * Supported EWMH hints should be added to
438 * both the enum and the ewmh array
440 enum { _NET_ACTIVE_WINDOW
, _NET_MOVERESIZE_WINDOW
, _NET_CLOSE_WINDOW
,
441 _NET_WM_WINDOW_TYPE
, _NET_WM_WINDOW_TYPE_DOCK
,
442 _NET_WM_WINDOW_TYPE_TOOLBAR
, _NET_WM_WINDOW_TYPE_UTILITY
,
443 _NET_WM_WINDOW_TYPE_SPLASH
, _NET_WM_WINDOW_TYPE_DIALOG
,
444 _NET_WM_WINDOW_TYPE_NORMAL
, _NET_WM_STATE
,
445 _NET_WM_STATE_MAXIMIZED_HORZ
, _NET_WM_STATE_MAXIMIZED_VERT
,
446 _NET_WM_STATE_SKIP_TASKBAR
, _NET_WM_STATE_SKIP_PAGER
,
447 _NET_WM_STATE_HIDDEN
, _NET_WM_STATE_ABOVE
, _SWM_WM_STATE_MANUAL
,
448 _NET_WM_STATE_FULLSCREEN
, _NET_WM_ALLOWED_ACTIONS
, _NET_WM_ACTION_MOVE
,
449 _NET_WM_ACTION_RESIZE
, _NET_WM_ACTION_FULLSCREEN
, _NET_WM_ACTION_CLOSE
,
455 } ewmh
[SWM_EWMH_HINT_MAX
] = {
456 /* must be in same order as in the enum */
457 {"_NET_ACTIVE_WINDOW", None
},
458 {"_NET_MOVERESIZE_WINDOW", None
},
459 {"_NET_CLOSE_WINDOW", None
},
460 {"_NET_WM_WINDOW_TYPE", None
},
461 {"_NET_WM_WINDOW_TYPE_DOCK", None
},
462 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None
},
463 {"_NET_WM_WINDOW_TYPE_UTILITY", None
},
464 {"_NET_WM_WINDOW_TYPE_SPLASH", None
},
465 {"_NET_WM_WINDOW_TYPE_DIALOG", None
},
466 {"_NET_WM_WINDOW_TYPE_NORMAL", None
},
467 {"_NET_WM_STATE", None
},
468 {"_NET_WM_STATE_MAXIMIZED_HORZ", None
},
469 {"_NET_WM_STATE_MAXIMIZED_VERT", None
},
470 {"_NET_WM_STATE_SKIP_TASKBAR", None
},
471 {"_NET_WM_STATE_SKIP_PAGER", None
},
472 {"_NET_WM_STATE_HIDDEN", None
},
473 {"_NET_WM_STATE_ABOVE", None
},
474 {"_SWM_WM_STATE_MANUAL", None
},
475 {"_NET_WM_STATE_FULLSCREEN", None
},
476 {"_NET_WM_ALLOWED_ACTIONS", None
},
477 {"_NET_WM_ACTION_MOVE", None
},
478 {"_NET_WM_ACTION_RESIZE", None
},
479 {"_NET_WM_ACTION_FULLSCREEN", None
},
480 {"_NET_WM_ACTION_CLOSE", None
},
483 void store_float_geom(struct ws_win
*win
, struct swm_region
*r
);
484 int floating_toggle_win(struct ws_win
*win
);
485 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
488 get_property(Window id
, Atom atom
, long count
, Atom type
,
489 unsigned long *n
, unsigned char **data
)
492 unsigned long tmp
, extra
;
493 unsigned long *nitems
;
496 nitems
= n
!= NULL
? n
: &tmp
;
497 status
= XGetWindowProperty(display
, id
, atom
, 0L, count
, False
, type
,
498 &real
, &format
, nitems
, &extra
, data
);
500 if (status
!= Success
)
509 update_iconic(struct ws_win
*win
, int newv
)
516 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
520 XChangeProperty(display
, win
->id
, iprop
, XA_INTEGER
, 32,
521 PropModeReplace
, (unsigned char *)&v
, 1);
523 XDeleteProperty(display
, win
->id
, iprop
);
527 get_iconic(struct ws_win
*win
)
532 unsigned long nitems
, extra
;
533 unsigned char *prop
= NULL
;
535 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
538 status
= XGetWindowProperty(display
, win
->id
, iprop
, 0L, 1L,
539 False
, XA_INTEGER
, &rettype
, &retfmt
, &nitems
, &extra
, &prop
);
540 if (status
!= Success
)
542 if (rettype
!= XA_INTEGER
|| retfmt
!= 32)
546 v
= *((int32_t *)prop
);
560 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
562 for (i
= 0; i
< LENGTH(ewmh
); i
++)
563 ewmh
[i
].atom
= XInternAtom(display
, ewmh
[i
].name
, False
);
565 for (i
= 0; i
< ScreenCount(display
); i
++) {
566 /* Support check window will be created by workaround(). */
568 /* Report supported atoms */
569 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
570 for (j
= 0; j
< LENGTH(ewmh
); j
++)
571 XChangeProperty(display
, screens
[i
].root
,
572 sup_list
, XA_ATOM
, 32,
573 PropModeAppend
, (unsigned char *)&ewmh
[j
].atom
,1);
581 unsigned char *data
= NULL
;
583 Atom sup_check
, sup_list
;
586 sup_check
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
587 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
589 for (i
= 0; i
< ScreenCount(display
); i
++) {
590 /* Get the support check window and destroy it */
591 success
= get_property(screens
[i
].root
, sup_check
, 1, XA_WINDOW
,
596 XDestroyWindow(display
, id
);
597 XDeleteProperty(display
, screens
[i
].root
, sup_check
);
598 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
606 ewmh_autoquirk(struct ws_win
*win
)
609 unsigned long *data
= NULL
;
613 success
= get_property(win
->id
, ewmh
[_NET_WM_WINDOW_TYPE
].atom
, (~0L),
614 XA_ATOM
, &n
, (unsigned char **)&data
);
621 for (i
= 0; i
< n
; i
++) {
623 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
625 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
626 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
627 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
629 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
632 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
633 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
635 win
->quirks
= SWM_Q_FLOAT
;
643 #define SWM_EWMH_ACTION_COUNT_MAX (6)
644 #define EWMH_F_FULLSCREEN (1<<0)
645 #define EWMH_F_ABOVE (1<<1)
646 #define EWMH_F_HIDDEN (1<<2)
647 #define EWMH_F_SKIP_PAGER (1<<3)
648 #define EWMH_F_SKIP_TASKBAR (1<<4)
649 #define SWM_F_MANUAL (1<<5)
652 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
654 struct swm_geometry rg
;
662 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
668 store_float_geom(win
, win
->ws
->r
);
675 if (win
->g_floatvalid
) {
676 /* refloat at last floating relative position */
677 win
->g
.x
= win
->g_float
.x
- win
->rg_float
.x
+ rg
.x
;
678 win
->g
.y
= win
->g_float
.y
- win
->rg_float
.y
+ rg
.y
;
679 win
->g
.w
= win
->g_float
.w
;
680 win
->g
.h
= win
->g_float
.h
;
688 ewmh_update_actions(struct ws_win
*win
)
690 Atom actions
[SWM_EWMH_ACTION_COUNT_MAX
];
696 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
699 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
700 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
703 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
,
704 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)actions
, n
);
707 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
708 #define _NET_WM_STATE_ADD 1 /* add/set property */
709 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
712 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
714 unsigned int mask
= 0;
715 unsigned int changed
= 0;
716 unsigned int orig_flags
;
721 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
722 mask
= EWMH_F_FULLSCREEN
;
723 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
725 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
727 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
728 mask
= EWMH_F_SKIP_PAGER
;
729 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
730 mask
= EWMH_F_SKIP_TASKBAR
;
733 orig_flags
= win
->ewmh_flags
;
736 case _NET_WM_STATE_REMOVE
:
737 win
->ewmh_flags
&= ~mask
;
739 case _NET_WM_STATE_ADD
:
740 win
->ewmh_flags
|= mask
;
742 case _NET_WM_STATE_TOGGLE
:
743 win
->ewmh_flags
^= mask
;
747 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
749 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
751 if (!floating_toggle_win(win
))
752 win
->ewmh_flags
= orig_flags
; /* revert */
753 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
755 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
756 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
758 if (!ewmh_set_win_fullscreen(win
,
759 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
760 win
->ewmh_flags
= orig_flags
; /* revert */
762 XDeleteProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
764 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
765 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
766 XA_ATOM
, 32, PropModeAppend
,
767 (unsigned char *)&ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
, 1);
768 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
769 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
770 XA_ATOM
, 32, PropModeAppend
,
771 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
, 1);
772 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
773 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
774 XA_ATOM
, 32, PropModeAppend
,
775 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
, 1);
776 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
777 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
778 XA_ATOM
, 32, PropModeAppend
,
779 (unsigned char *)&ewmh
[_NET_WM_STATE_ABOVE
].atom
, 1);
780 if (win
->ewmh_flags
& SWM_F_MANUAL
)
781 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
782 XA_ATOM
, 32, PropModeAppend
,
783 (unsigned char *)&ewmh
[_SWM_WM_STATE_MANUAL
].atom
, 1);
787 ewmh_get_win_state(struct ws_win
*win
)
798 win
->ewmh_flags
|= EWMH_F_ABOVE
;
800 win
->ewmh_flags
|= SWM_F_MANUAL
;
802 success
= get_property(win
->id
, ewmh
[_NET_WM_STATE
].atom
,
803 (~0L), XA_ATOM
, &n
, (unsigned char **)&states
);
808 for (i
= 0; i
< n
; i
++)
809 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
829 name
= "ButtonPress";
832 name
= "ButtonRelease";
835 name
= "MotionNotify";
838 name
= "EnterNotify";
841 name
= "LeaveNotify";
850 name
= "KeymapNotify";
856 name
= "GraphicsExpose";
861 case VisibilityNotify
:
862 name
= "VisibilityNotify";
865 name
= "CreateNotify";
868 name
= "DestroyNotify";
871 name
= "UnmapNotify";
880 name
= "ReparentNotify";
882 case ConfigureNotify
:
883 name
= "ConfigureNotify";
885 case ConfigureRequest
:
886 name
= "ConfigureRequest";
889 name
= "GravityNotify";
892 name
= "ResizeRequest";
894 case CirculateNotify
:
895 name
= "CirculateNotify";
897 case CirculateRequest
:
898 name
= "CirculateRequest";
901 name
= "PropertyNotify";
904 name
= "SelectionClear";
906 case SelectionRequest
:
907 name
= "SelectionRequest";
909 case SelectionNotify
:
910 name
= "SelectionNotify";
913 name
= "ColormapNotify";
916 name
= "ClientMessage";
919 name
= "MappingNotify";
924 DNPRINTF(SWM_D_EVENTQ
,"window: %lu event: %s (%d), %d "
926 e
->xany
.window
, name
, e
->type
, QLength(display
));
928 DNPRINTF(SWM_D_EVENTQ
, "window: %lu unknown event %d, %d "
930 e
->xany
.window
, e
->type
, QLength(display
));
934 dumpwins(struct swm_region
*r
, union arg
*args
)
938 XWindowAttributes wa
;
941 fprintf(stderr
, "invalid workspace\n");
945 fprintf(stderr
, "=== managed window list ws %02d ===\n", r
->ws
->idx
);
947 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
948 state
= getstate(win
->id
);
949 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
950 fprintf(stderr
, "window: %lu failed "
951 "XGetWindowAttributes\n", win
->id
);
952 fprintf(stderr
, "window: %lu map_state: %d state: %d "
954 win
->id
, wa
.map_state
, state
, win
->transient
);
957 fprintf(stderr
, "===== unmanaged window list =====\n");
958 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
959 state
= getstate(win
->id
);
960 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
961 fprintf(stderr
, "window: %lu failed "
962 "XGetWindowAttributes\n", win
->id
);
963 fprintf(stderr
, "window: %lu map_state: %d state: %d "
965 win
->id
, wa
.map_state
, state
, win
->transient
);
968 fprintf(stderr
, "=================================\n");
973 dumpwins(struct swm_region
*r
, union arg
*args
)
976 #endif /* SWM_DEBUG */
978 void expose(XEvent
*);
979 void keypress(XEvent
*);
980 void buttonpress(XEvent
*);
981 void configurerequest(XEvent
*);
982 void configurenotify(XEvent
*);
983 void destroynotify(XEvent
*);
984 void enternotify(XEvent
*);
985 void focusevent(XEvent
*);
986 void mapnotify(XEvent
*);
987 void mappingnotify(XEvent
*);
988 void maprequest(XEvent
*);
989 void propertynotify(XEvent
*);
990 void unmapnotify(XEvent
*);
991 void visibilitynotify(XEvent
*);
992 void clientmessage(XEvent
*);
994 void (*handler
[LASTEvent
])(XEvent
*) = {
996 [KeyPress
] = keypress
,
997 [ButtonPress
] = buttonpress
,
998 [ConfigureRequest
] = configurerequest
,
999 [ConfigureNotify
] = configurenotify
,
1000 [DestroyNotify
] = destroynotify
,
1001 [EnterNotify
] = enternotify
,
1002 [FocusIn
] = focusevent
,
1003 [FocusOut
] = focusevent
,
1004 [MapNotify
] = mapnotify
,
1005 [MappingNotify
] = mappingnotify
,
1006 [MapRequest
] = maprequest
,
1007 [PropertyNotify
] = propertynotify
,
1008 [UnmapNotify
] = unmapnotify
,
1009 [VisibilityNotify
] = visibilitynotify
,
1010 [ClientMessage
] = clientmessage
,
1016 int saved_errno
, status
;
1019 saved_errno
= errno
;
1023 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1028 if (errno
!= ECHILD
)
1029 warn("sighdlr: waitpid");
1030 #endif /* SWM_DEBUG */
1033 if (pid
== searchpid
)
1037 if (WIFEXITED(status
)) {
1038 if (WEXITSTATUS(status
) != 0)
1039 warnx("sighdlr: child exit status: %d",
1040 WEXITSTATUS(status
));
1042 warnx("sighdlr: child is terminated "
1044 #endif /* SWM_DEBUG */
1058 errno
= saved_errno
;
1064 struct pid_e
*p
= NULL
;
1066 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1071 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1080 name_to_color(char *colorname
)
1084 XColor screen_def
, exact_def
;
1085 unsigned long result
= 0;
1086 char cname
[32] = "#";
1088 cmap
= DefaultColormap(display
, screens
[0].idx
);
1089 status
= XAllocNamedColor(display
, cmap
, colorname
,
1090 &screen_def
, &exact_def
);
1092 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1093 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
1097 result
= screen_def
.pixel
;
1099 fprintf(stderr
, "color '%s' not found.\n", colorname
);
1105 setscreencolor(char *val
, int i
, int c
)
1107 if (i
> 0 && i
<= ScreenCount(display
)) {
1108 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1109 free(screens
[i
- 1].c
[c
].name
);
1110 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1112 } else if (i
== -1) {
1113 for (i
= 0; i
< ScreenCount(display
); i
++) {
1114 screens
[i
].c
[c
].color
= name_to_color(val
);
1115 free(screens
[i
].c
[c
].name
);
1116 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1120 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1121 i
, ScreenCount(display
));
1125 custom_region(char *val
)
1127 unsigned int sidx
, x
, y
, w
, h
;
1129 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1130 errx(1, "invalid custom region, "
1131 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1132 if (sidx
< 1 || sidx
> ScreenCount(display
))
1133 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1134 sidx
, ScreenCount(display
));
1138 errx(1, "region %ux%u+%u+%u too small\n", w
, h
, x
, y
);
1140 if (x
< 0 || x
> DisplayWidth(display
, sidx
) ||
1141 y
< 0 || y
> DisplayHeight(display
, sidx
) ||
1142 w
+ x
> DisplayWidth(display
, sidx
) ||
1143 h
+ y
> DisplayHeight(display
, sidx
)) {
1144 fprintf(stderr
, "ignoring region %ux%u+%u+%u "
1145 "- not within screen boundaries "
1146 "(%ux%u)\n", w
, h
, x
, y
,
1147 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
1151 new_region(&screens
[sidx
], x
, y
, w
, h
);
1155 socket_setnonblock(int fd
)
1159 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1160 err(1, "fcntl F_GETFL");
1161 flags
|= O_NONBLOCK
;
1162 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1163 err(1, "fcntl F_SETFL");
1167 bar_print(struct swm_region
*r
, char *s
)
1169 XClearWindow(display
, r
->bar_window
);
1170 XSetForeground(display
, bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
1171 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
, s
,
1176 bar_extra_stop(void)
1180 bzero(bar_pipe
, sizeof bar_pipe
);
1183 kill(bar_pid
, SIGTERM
);
1186 strlcpy(bar_ext
, "", sizeof bar_ext
);
1191 bar_class_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1193 int do_class
, do_name
;
1195 XClassHint
*xch
= NULL
;
1197 if ((title_name_enabled
== 1 || title_class_enabled
== 1) &&
1198 cur_focus
!= NULL
) {
1199 if ((xch
= XAllocClassHint()) == NULL
)
1201 status
= XGetClassHint(display
, cur_focus
->id
, xch
);
1202 if (status
== BadWindow
|| status
== BadAlloc
)
1204 do_class
= (title_class_enabled
&& xch
->res_class
!= NULL
);
1205 do_name
= (title_name_enabled
&& xch
->res_name
!= NULL
);
1207 strlcat(s
, xch
->res_class
, sz
);
1208 if (do_class
&& do_name
)
1209 strlcat(s
, ":", sz
);
1211 strlcat(s
, xch
->res_name
, sz
);
1212 strlcat(s
, " ", sz
);
1220 bar_window_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1224 if (window_name_enabled
&& cur_focus
!= NULL
) {
1225 XFetchName(display
, cur_focus
->id
, &title
);
1227 if (cur_focus
->floating
)
1228 strlcat(s
, "(f) ", sz
);
1229 strlcat(s
, title
, sz
);
1230 strlcat(s
, " ", sz
);
1241 struct swm_region
*r
;
1244 char s
[SWM_BAR_MAX
];
1245 char cn
[SWM_BAR_MAX
];
1246 char loc
[SWM_BAR_MAX
];
1250 if (bar_enabled
== 0)
1252 if (bar_extra
&& bar_extra_running
) {
1253 /* ignore short reads; it'll correct itself */
1254 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1255 if (b
&& b
[len
- 1] == '\n') {
1257 strlcpy(bar_ext
, b
, sizeof bar_ext
);
1259 if (b
== NULL
&& errno
!= EAGAIN
) {
1260 fprintf(stderr
, "bar_extra failed: errno: %d %s\n",
1261 errno
, strerror(errno
));
1265 strlcpy(bar_ext
, "", sizeof bar_ext
);
1267 if (clock_enabled
== 0)
1268 strlcpy(s
, "", sizeof s
);
1271 localtime_r(&tmt
, &tm
);
1272 strftime(s
, sizeof s
, clock_format
, &tm
);
1273 strlcat(s
, " ", sizeof s
);
1276 for (i
= 0; i
< ScreenCount(display
); i
++) {
1278 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1279 strlcpy(cn
, "", sizeof cn
);
1281 bar_class_name(cn
, sizeof cn
, r
->ws
->focus
);
1282 bar_window_name(cn
, sizeof cn
, r
->ws
->focus
);
1286 stack
= r
->ws
->cur_layout
->name
;
1288 snprintf(loc
, sizeof loc
, "%d:%d %s %s%s %s %s",
1289 x
++, r
->ws
->idx
+ 1, stack
, s
, cn
, bar_ext
,
1304 bar_toggle(struct swm_region
*r
, union arg
*args
)
1306 struct swm_region
*tmpr
;
1307 int i
, sc
= ScreenCount(display
);
1309 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
1312 for (i
= 0; i
< sc
; i
++)
1313 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1314 XUnmapWindow(display
, tmpr
->bar_window
);
1316 for (i
= 0; i
< sc
; i
++)
1317 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1318 XMapRaised(display
, tmpr
->bar_window
);
1320 bar_enabled
= !bar_enabled
;
1323 /* must be after stack */
1330 XSetWindowAttributes wa
;
1331 struct swm_region
*r
;
1334 /* do this here because the conf file is in memory */
1335 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1336 /* launch external status app */
1337 bar_extra_running
= 1;
1338 if (pipe(bar_pipe
) == -1)
1339 err(1, "pipe error");
1340 socket_setnonblock(bar_pipe
[0]);
1341 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1342 if (dup2(bar_pipe
[0], 0) == -1)
1344 if (dup2(bar_pipe
[1], 1) == -1)
1346 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1347 err(1, "could not disable SIGPIPE");
1348 switch (bar_pid
= fork()) {
1350 err(1, "cannot fork");
1354 execvp(bar_argv
[0], bar_argv
);
1355 err(1, "%s external app failed", bar_argv
[0]);
1357 default: /* parent */
1363 bzero(&wa
, sizeof wa
);
1364 for (i
= 0; i
< ScreenCount(display
); i
++)
1365 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1367 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1368 wa
.background_pixel
=
1369 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1370 XChangeWindowAttributes(display
, r
->bar_window
,
1371 CWBackPixel
| CWBorderPixel
, &wa
);
1377 bar_setup(struct swm_region
*r
)
1382 XFreeFont(display
, bar_fs
);
1386 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
1387 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
1393 if (bar_fonts
[i
] == NULL
)
1394 errx(1, "couldn't load font");
1396 errx(1, "couldn't create font structure");
1398 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 1 +
1399 2 * bar_border_width
;
1401 y
= bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1403 r
->bar_window
= XCreateSimpleWindow(display
,
1404 r
->s
->root
, x
, y
, WIDTH(r
) - 2 * bar_border_width
,
1405 bar_height
- 2 * bar_border_width
,
1406 bar_border_width
, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
1407 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1408 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
1409 XSetFont(display
, bar_gc
, bar_fs
->fid
);
1410 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
1412 XMapRaised(display
, r
->bar_window
);
1413 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
1415 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1416 err(1, "could not install bar_signal");
1421 drain_enter_notify(void)
1426 while (XCheckMaskEvent(display
, EnterWindowMask
, &cne
))
1429 DNPRINTF(SWM_D_MISC
, "drain_enter_notify: drained %d\n", i
);
1433 set_win_state(struct ws_win
*win
, long state
)
1435 long data
[] = {state
, None
};
1437 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
1442 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1443 (unsigned char *)data
, 2);
1450 unsigned char *p
= NULL
;
1453 if (!get_property(w
, astate
, 2L, astate
, &n
, &p
))
1456 result
= *((long *)p
);
1462 version(struct swm_region
*r
, union arg
*args
)
1464 bar_version
= !bar_version
;
1466 snprintf(bar_vertext
, sizeof bar_vertext
, "Version: %s CVS: %s",
1467 SWM_VERSION
, cvstag
);
1469 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
1474 client_msg(struct ws_win
*win
, Atom a
)
1476 XClientMessageEvent cm
;
1481 bzero(&cm
, sizeof cm
);
1482 cm
.type
= ClientMessage
;
1483 cm
.window
= win
->id
;
1484 cm
.message_type
= aprot
;
1487 cm
.data
.l
[1] = CurrentTime
;
1488 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
1492 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
1500 DNPRINTF(SWM_D_MISC
,
1501 "config_win: win %lu x %d y %d w %d h %d\n",
1502 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
1504 ce
.type
= ConfigureNotify
;
1505 ce
.display
= display
;
1507 ce
.window
= win
->id
;
1510 ce
.width
= win
->g
.w
;
1511 ce
.height
= win
->g
.h
;
1512 ce
.border_width
= border_width
;
1514 ce
.override_redirect
= False
;
1516 DNPRINTF(SWM_D_MISC
,
1517 "config_win: ev win %lu x %d y %d w %d h %d\n",
1518 ev
->window
, ev
->x
, ev
->y
, ev
->width
, ev
->height
);
1519 ce
.type
= ConfigureNotify
;
1520 ce
.display
= ev
->display
;
1521 ce
.event
= ev
->window
;
1522 ce
.window
= ev
->window
;
1525 ce
.width
= ev
->width
;
1526 ce
.height
= ev
->height
;
1527 ce
.border_width
= ev
->border_width
;
1528 ce
.above
= ev
->above
;
1529 ce
.override_redirect
= False
;
1532 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
1536 count_win(struct workspace
*ws
, int count_transient
)
1541 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1542 if (count_transient
== 0 && win
->floating
)
1544 if (count_transient
== 0 && win
->transient
)
1550 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
1556 quit(struct swm_region
*r
, union arg
*args
)
1558 DNPRINTF(SWM_D_MISC
, "quit\n");
1563 unmap_window(struct ws_win
*win
)
1568 /* don't unmap again */
1569 if (getstate(win
->id
) == IconicState
)
1572 set_win_state(win
, IconicState
);
1574 XUnmapWindow(display
, win
->id
);
1575 XSetWindowBorder(display
, win
->id
,
1576 win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1585 for (i
= 0; i
< ScreenCount(display
); i
++)
1586 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1587 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1592 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
1599 event
.display
= display
; /* Ignored, but what the hell */
1600 event
.window
= win
->id
;
1601 event
.root
= win
->s
->root
;
1602 event
.subwindow
= None
;
1603 event
.time
= CurrentTime
;
1608 event
.same_screen
= True
;
1609 event
.keycode
= XKeysymToKeycode(display
, keysym
);
1610 event
.state
= modifiers
;
1612 event
.type
= KeyPress
;
1613 XSendEvent(event
.display
, event
.window
, True
,
1614 KeyPressMask
, (XEvent
*)&event
);
1616 event
.type
= KeyRelease
;
1617 XSendEvent(event
.display
, event
.window
, True
,
1618 KeyPressMask
, (XEvent
*)&event
);
1623 restart(struct swm_region
*r
, union arg
*args
)
1625 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
1627 /* disable alarm because the following code may not be interrupted */
1629 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
1630 errx(1, "can't disable alarm");
1635 XCloseDisplay(display
);
1636 execvp(start_argv
[0], start_argv
);
1637 fprintf(stderr
, "execvp failed\n");
1643 root_to_region(Window root
)
1645 struct swm_region
*r
= NULL
;
1647 int i
, x
, y
, wx
, wy
;
1650 for (i
= 0; i
< ScreenCount(display
); i
++)
1651 if (screens
[i
].root
== root
)
1654 if (XQueryPointer(display
, screens
[i
].root
,
1655 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
1656 /* choose a region based on pointer location */
1657 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1658 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
1659 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
1664 r
= TAILQ_FIRST(&screens
[i
].rl
);
1670 find_unmanaged_window(Window id
)
1675 for (i
= 0; i
< ScreenCount(display
); i
++)
1676 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1677 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
1685 find_window(Window id
)
1688 Window wrr
, wpr
, *wcr
= NULL
;
1692 for (i
= 0; i
< ScreenCount(display
); i
++)
1693 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1694 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1698 /* if we were looking for the parent return that window instead */
1699 if (XQueryTree(display
, id
, &wrr
, &wpr
, &wcr
, &nc
) == 0)
1704 /* ignore not found and root */
1705 if (wpr
== 0 || wrr
== wpr
)
1708 /* look for parent */
1709 for (i
= 0; i
< ScreenCount(display
); i
++)
1710 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1711 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1719 spawn(int ws_idx
, union arg
*args
, int close_fd
)
1724 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
1727 close(ConnectionNumber(display
));
1729 setenv("LD_PRELOAD", SWM_LIB
, 1);
1731 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
1735 setenv("_SWM_WS", ret
, 1);
1739 if (asprintf(&ret
, "%d", getpid()) == -1) {
1743 setenv("_SWM_PID", ret
, 1);
1747 if (setsid() == -1) {
1754 * close stdin and stdout to prevent interaction between apps
1755 * and the baraction script
1756 * leave stderr open to record errors
1758 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
1762 dup2(fd
, STDIN_FILENO
);
1763 dup2(fd
, STDOUT_FILENO
);
1768 execvp(args
->argv
[0], args
->argv
);
1775 spawnterm(struct swm_region
*r
, union arg
*args
)
1777 DNPRINTF(SWM_D_MISC
, "spawnterm\n");
1781 setenv("_SWM_XTERM_FONTADJ", "", 1);
1782 spawn(r
->ws
->idx
, args
, 1);
1787 kill_refs(struct ws_win
*win
)
1790 struct swm_region
*r
;
1791 struct workspace
*ws
;
1796 for (i
= 0; i
< ScreenCount(display
); i
++)
1797 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1798 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
1800 if (win
== ws
->focus
)
1802 if (win
== ws
->focus_prev
)
1803 ws
->focus_prev
= NULL
;
1808 validate_win(struct ws_win
*testwin
)
1811 struct workspace
*ws
;
1812 struct swm_region
*r
;
1813 int i
, x
, foundit
= 0;
1815 if (testwin
== NULL
)
1818 for (i
= 0, foundit
= 0; i
< ScreenCount(display
); i
++)
1819 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1820 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
1822 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
1830 validate_ws(struct workspace
*testws
)
1832 struct swm_region
*r
;
1833 struct workspace
*ws
;
1836 /* validate all ws */
1837 for (i
= 0, foundit
= 0; i
< ScreenCount(display
); i
++)
1838 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1839 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
1848 unfocus_win(struct ws_win
*win
)
1853 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: id: %lu\n", WINID(win
));
1857 if (win
->ws
== NULL
)
1860 if (validate_ws(win
->ws
))
1861 return; /* XXX this gets hit with thunderbird, needs fixing */
1863 if (win
->ws
->r
== NULL
)
1866 if (validate_win(win
)) {
1871 if (win
->ws
->focus
== win
) {
1872 win
->ws
->focus
= NULL
;
1873 win
->ws
->focus_prev
= win
;
1876 if (validate_win(win
->ws
->focus
)) {
1877 kill_refs(win
->ws
->focus
);
1878 win
->ws
->focus
= NULL
;
1880 if (validate_win(win
->ws
->focus_prev
)) {
1881 kill_refs(win
->ws
->focus_prev
);
1882 win
->ws
->focus_prev
= NULL
;
1885 /* drain all previous unfocus events */
1886 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
1889 grabbuttons(win
, 0);
1890 XSetWindowBorder(display
, win
->id
,
1891 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1893 XChangeProperty(display
, win
->s
->root
,
1894 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
1895 PropModeReplace
, (unsigned char *)&none
,1);
1904 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
1906 for (i
= 0; i
< ScreenCount(display
); i
++)
1907 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1908 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1913 focus_win(struct ws_win
*win
)
1918 struct ws_win
*cfw
= NULL
;
1921 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
? win
->id
: 0);
1925 if (win
->ws
== NULL
)
1928 if (validate_ws(win
->ws
))
1929 return; /* XXX this gets hit with thunderbird, needs fixing */
1931 if (validate_win(win
)) {
1936 if (validate_win(win
)) {
1941 XGetInputFocus(display
, &cur_focus
, &rr
);
1942 if ((cfw
= find_window(cur_focus
)) != NULL
)
1945 /* use larger hammer since the window was killed somehow */
1946 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
1947 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
1948 XSetWindowBorder(display
, cfw
->id
,
1949 cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1952 win
->ws
->focus
= win
;
1954 if (win
->ws
->r
!= NULL
) {
1955 /* drain all previous focus events */
1956 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
1960 XSetInputFocus(display
, win
->id
,
1961 RevertToParent
, CurrentTime
);
1962 XMapRaised(display
, win
->id
);
1963 grabbuttons(win
, 1);
1964 XSetWindowBorder(display
, win
->id
,
1965 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
1966 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
)
1967 XMapRaised(display
, win
->id
);
1969 XChangeProperty(display
, win
->s
->root
,
1970 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
1971 PropModeReplace
, (unsigned char *)&win
->id
,1);
1974 if (window_name_enabled
)
1979 switchws(struct swm_region
*r
, union arg
*args
)
1981 int wsid
= args
->id
, unmap_old
= 0;
1982 struct swm_region
*this_r
, *other_r
;
1984 struct workspace
*new_ws
, *old_ws
;
1991 old_ws
= this_r
->ws
;
1992 new_ws
= &this_r
->s
->ws
[wsid
];
1994 DNPRINTF(SWM_D_WS
, "switchws screen[%d]:%dx%d+%d+%d: "
1995 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
1998 if (new_ws
== NULL
|| old_ws
== NULL
)
2000 if (new_ws
== old_ws
)
2003 other_r
= new_ws
->r
;
2004 if (other_r
== NULL
) {
2005 /* the other workspace is hidden, hide this one */
2009 /* the other ws is visible in another region, exchange them */
2010 other_r
->ws_prior
= new_ws
;
2011 other_r
->ws
= old_ws
;
2012 old_ws
->r
= other_r
;
2014 this_r
->ws_prior
= old_ws
;
2015 this_r
->ws
= new_ws
;
2018 /* this is needed so that we can click on a window after a restart */
2022 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2023 focus(new_ws
->r
, &a
);
2027 /* unmap old windows */
2029 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2032 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2033 drain_enter_notify();
2037 cyclews(struct swm_region
*r
, union arg
*args
)
2040 struct swm_screen
*s
= r
->s
;
2042 DNPRINTF(SWM_D_WS
, "cyclews id %d "
2043 "in screen[%d]:%dx%d+%d+%d ws %d\n", args
->id
,
2044 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2049 case SWM_ARG_ID_CYCLEWS_UP
:
2050 if (a
.id
< SWM_WS_MAX
- 1)
2055 case SWM_ARG_ID_CYCLEWS_DOWN
:
2059 a
.id
= SWM_WS_MAX
- 1;
2065 if (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
))
2067 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2071 } while (a
.id
!= r
->ws
->idx
);
2075 priorws(struct swm_region
*r
, union arg
*args
)
2079 DNPRINTF(SWM_D_WS
, "priorws id %d "
2080 "in screen[%d]:%dx%d+%d+%d ws %d\n", args
->id
,
2081 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2083 if (r
->ws_prior
== NULL
)
2086 a
.id
= r
->ws_prior
->idx
;
2091 cyclescr(struct swm_region
*r
, union arg
*args
)
2093 struct swm_region
*rr
= NULL
;
2097 /* do nothing if we don't have more than one screen */
2098 if (!(ScreenCount(display
) > 1 || outputs
> 1))
2103 case SWM_ARG_ID_CYCLESC_UP
:
2104 rr
= TAILQ_NEXT(r
, entry
);
2106 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2108 case SWM_ARG_ID_CYCLESC_DOWN
:
2109 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2111 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2119 /* move mouse to region */
2121 y
= rr
->g
.y
+ 1 + (bar_enabled
? bar_height
: 0);
2122 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2124 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2127 if (rr
->ws
->focus
) {
2128 /* move to focus window */
2129 x
= rr
->ws
->focus
->g
.x
+ 1;
2130 y
= rr
->ws
->focus
->g
.y
+ 1;
2131 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2136 sort_windows(struct ws_win_list
*wl
)
2138 struct ws_win
*win
, *parent
, *nxt
;
2143 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2144 nxt
= TAILQ_NEXT(win
, entry
);
2145 if (win
->transient
) {
2146 parent
= find_window(win
->transient
);
2147 if (parent
== NULL
) {
2148 fprintf(stderr
, "not possible bug\n");
2151 TAILQ_REMOVE(wl
, win
, entry
);
2152 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2159 swapwin(struct swm_region
*r
, union arg
*args
)
2161 struct ws_win
*target
, *source
;
2162 struct ws_win
*cur_focus
;
2163 struct ws_win_list
*wl
;
2166 DNPRINTF(SWM_D_WS
, "swapwin id %d "
2167 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
2168 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2170 cur_focus
= r
->ws
->focus
;
2171 if (cur_focus
== NULL
)
2175 wl
= &source
->ws
->winlist
;
2178 case SWM_ARG_ID_SWAPPREV
:
2179 if (source
->transient
)
2180 source
= find_window(source
->transient
);
2181 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2182 if (target
&& target
->transient
)
2183 target
= find_window(target
->transient
);
2184 TAILQ_REMOVE(wl
, source
, entry
);
2186 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2188 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2190 case SWM_ARG_ID_SWAPNEXT
:
2191 target
= TAILQ_NEXT(source
, entry
);
2192 /* move the parent and let the sort handle the move */
2193 if (source
->transient
)
2194 source
= find_window(source
->transient
);
2195 TAILQ_REMOVE(wl
, source
, entry
);
2197 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2199 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2201 case SWM_ARG_ID_SWAPMAIN
:
2202 target
= TAILQ_FIRST(wl
);
2203 if (target
== source
) {
2204 if (source
->ws
->focus_prev
!= NULL
&&
2205 source
->ws
->focus_prev
!= target
)
2207 source
= source
->ws
->focus_prev
;
2211 if (target
== NULL
|| source
== NULL
)
2213 source
->ws
->focus_prev
= target
;
2214 TAILQ_REMOVE(wl
, target
, entry
);
2215 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2216 TAILQ_REMOVE(wl
, source
, entry
);
2217 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2219 case SWM_ARG_ID_MOVELAST
:
2220 TAILQ_REMOVE(wl
, source
, entry
);
2221 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2224 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
2234 focus_prev(struct ws_win
*win
)
2236 struct ws_win
*winfocus
= NULL
, *winlostfocus
= NULL
;
2237 struct ws_win
*cur_focus
= NULL
;
2238 struct ws_win_list
*wl
= NULL
;
2239 struct workspace
*ws
= NULL
;
2241 DNPRINTF(SWM_D_FOCUS
, "focus_prev: id %lu\n", WINID(win
));
2243 if (!(win
&& win
->ws
))
2248 cur_focus
= ws
->focus
;
2249 winlostfocus
= cur_focus
;
2251 /* pickle, just focus on whatever */
2252 if (cur_focus
== NULL
) {
2253 /* use prev_focus if valid */
2254 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2255 find_window(WINID(ws
->focus_prev
)))
2256 winfocus
= ws
->focus_prev
;
2257 if (winfocus
== NULL
)
2258 winfocus
= TAILQ_FIRST(wl
);
2262 /* if transient focus on parent */
2263 if (cur_focus
->transient
) {
2264 winfocus
= find_window(cur_focus
->transient
);
2268 /* if in max_stack try harder */
2269 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2270 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2271 if (cur_focus
!= ws
->focus_prev
)
2272 winfocus
= ws
->focus_prev
;
2273 else if (cur_focus
!= ws
->focus
)
2274 winfocus
= ws
->focus
;
2276 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2281 if (cur_focus
== win
)
2282 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2283 if (winfocus
== NULL
)
2284 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2285 if (winfocus
== NULL
|| winfocus
== win
)
2286 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2288 if (winfocus
== winlostfocus
|| winfocus
== NULL
)
2291 focus_magic(winfocus
);
2295 focus(struct swm_region
*r
, union arg
*args
)
2297 struct ws_win
*winfocus
= NULL
, *winlostfocus
= NULL
, *head
;
2298 struct ws_win
*cur_focus
= NULL
;
2299 struct ws_win_list
*wl
= NULL
;
2300 struct workspace
*ws
= NULL
;
2305 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
2307 /* treat FOCUS_CUR special */
2308 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2309 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2310 winfocus
= r
->ws
->focus
;
2311 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2312 winfocus
= r
->ws
->focus_prev
;
2314 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2315 if (winfocus
->iconic
== 0)
2318 focus_magic(winfocus
);
2322 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2327 winlostfocus
= cur_focus
;
2330 case SWM_ARG_ID_FOCUSPREV
:
2331 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2333 head
= TAILQ_LAST(wl
, ws_win_list
);
2335 if (WINID(winfocus
) == cur_focus
->transient
) {
2336 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2338 head
= TAILQ_LAST(wl
, ws_win_list
);
2343 if (winfocus
&& winfocus
->iconic
) {
2344 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
2345 if (winfocus
->iconic
== 0)
2350 case SWM_ARG_ID_FOCUSNEXT
:
2351 head
= TAILQ_NEXT(cur_focus
, entry
);
2353 head
= TAILQ_FIRST(wl
);
2357 if (winfocus
&& winfocus
->iconic
) {
2358 TAILQ_FOREACH(winfocus
, wl
, entry
)
2359 if (winfocus
->iconic
== 0)
2364 case SWM_ARG_ID_FOCUSMAIN
:
2365 winfocus
= TAILQ_FIRST(wl
);
2366 if (winfocus
== cur_focus
)
2367 winfocus
= cur_focus
->ws
->focus_prev
;
2373 if (winfocus
== winlostfocus
|| winfocus
== NULL
)
2376 focus_magic(winfocus
);
2380 cycle_layout(struct swm_region
*r
, union arg
*args
)
2382 struct workspace
*ws
= r
->ws
;
2383 struct ws_win
*winfocus
;
2386 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2388 winfocus
= ws
->focus
;
2391 if (ws
->cur_layout
->l_stack
== NULL
)
2392 ws
->cur_layout
= &layouts
[0];
2395 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2396 drain_enter_notify();
2397 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2403 stack_config(struct swm_region
*r
, union arg
*args
)
2405 struct workspace
*ws
= r
->ws
;
2407 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
2410 if (ws
->cur_layout
->l_config
!= NULL
)
2411 ws
->cur_layout
->l_config(ws
, args
->id
);
2413 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
2419 struct swm_geometry g
;
2420 struct swm_region
*r
;
2423 DNPRINTF(SWM_D_STACK
, "stack\n");
2425 for (i
= 0; i
< ScreenCount(display
); i
++) {
2427 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2428 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
2429 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
2431 /* start with screen geometry, adjust for bar */
2433 g
.w
-= 2 * border_width
;
2434 g
.h
-= 2 * border_width
;
2440 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
2441 /* save r so we can track region changes */
2448 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2449 drain_enter_notify();
2453 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
2455 /* retain window geom and region geom */
2456 win
->g_float
.x
= win
->g
.x
;
2457 win
->g_float
.y
= win
->g
.y
;
2458 win
->g_float
.w
= win
->g
.w
;
2459 win
->g_float
.h
= win
->g
.h
;
2460 win
->rg_float
.x
= r
->g
.x
;
2461 win
->rg_float
.y
= r
->g
.y
;
2462 win
->rg_float
.w
= r
->g
.w
;
2463 win
->rg_float
.h
= r
->g
.h
;
2464 win
->g_floatvalid
= 1;
2468 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
2476 bzero(&wc
, sizeof wc
);
2477 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
2480 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2481 * geom on ws switches or return from max mode
2484 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&& win
->g_floatvalid
2485 && !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
2487 * use stored g and rg to set relative position and size
2488 * as in old region or before max stack mode
2490 win
->g
.x
= win
->g_float
.x
- win
->rg_float
.x
+ r
->g
.x
;
2491 win
->g
.y
= win
->g_float
.y
- win
->rg_float
.y
+ r
->g
.y
;
2492 win
->g
.w
= win
->g_float
.w
;
2493 win
->g
.h
= win
->g_float
.h
;
2494 win
->g_floatvalid
= 0;
2497 win
->floatmaxed
= 0;
2499 if ((win
->quirks
& SWM_Q_FULLSCREEN
) && (win
->g
.w
>= WIDTH(r
)) &&
2500 (win
->g
.h
>= HEIGHT(r
)))
2501 wc
.border_width
= 0;
2503 wc
.border_width
= border_width
;
2504 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
2505 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
2506 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
2511 * floaters and transients are auto-centred unless moved
2514 win
->g
.x
= r
->g
.x
+ (WIDTH(r
) - win
->g
.w
) /
2515 2 - wc
.border_width
;
2516 win
->g
.y
= r
->g
.y
+ (HEIGHT(r
) - win
->g
.h
) /
2517 2 - wc
.border_width
;
2520 /* win can be outside r if new r smaller than old r */
2521 /* Ensure top left corner inside r (move probs otherwise) */
2522 if (win
->g
.x
< r
->g
.x
- wc
.border_width
)
2523 win
->g
.x
= r
->g
.x
- wc
.border_width
;
2524 if (win
->g
.x
> r
->g
.x
+ r
->g
.w
- 1)
2525 win
->g
.x
= (win
->g
.w
> r
->g
.w
) ? r
->g
.x
:
2526 (r
->g
.x
+ r
->g
.w
- win
->g
.w
- 2 * wc
.border_width
);
2527 if (win
->g
.y
< r
->g
.y
- wc
.border_width
)
2528 win
->g
.y
= r
->g
.y
- wc
.border_width
;
2529 if (win
->g
.y
> r
->g
.y
+ r
->g
.h
- 1)
2530 win
->g
.y
= (win
->g
.h
> r
->g
.h
) ? r
->g
.y
:
2531 (r
->g
.y
+ r
->g
.h
- win
->g
.h
- 2 * wc
.border_width
);
2535 wc
.width
= win
->g
.w
;
2536 wc
.height
= win
->g
.h
;
2539 * Retain floater and transient geometry for correct positioning
2540 * when ws changes region
2542 if (!(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
2543 store_float_geom(win
, r
);
2545 DNPRINTF(SWM_D_MISC
, "stack_floater: win %lu x %d y %d w %d h %d\n",
2546 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
2548 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2552 * Send keystrokes to terminal to decrease/increase the font size as the
2553 * window size changes.
2556 adjust_font(struct ws_win
*win
)
2558 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
2559 win
->floating
|| win
->transient
)
2562 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
2563 win
->g
.w
/ win
->sh
.width_inc
< term_width
&&
2564 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
2565 win
->font_size_boundary
[win
->font_steps
] =
2566 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
2569 win
->last_inc
= win
->sh
.width_inc
;
2570 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
2571 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
2572 win
->g
.w
> win
->font_size_boundary
[win
->font_steps
- 1]) {
2575 win
->last_inc
= win
->sh
.width_inc
;
2576 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
2580 #define SWAPXY(g) do { \
2582 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2583 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2586 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
2589 XWindowAttributes wa
;
2590 struct swm_geometry win_g
, r_g
= *g
;
2591 struct ws_win
*win
, *fs_win
= 0;
2592 int i
, j
, s
, stacks
;
2593 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
2594 int hrh
, extra
= 0, h_slice
, last_h
= 0;
2595 int split
, colno
, winno
, mwin
, msize
, mscale
;
2596 int remain
, missing
, v_slice
, reconfigure
;
2599 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d\n rot=%s flip=%s",
2600 ws
->idx
, rot
? "yes" : "no", flip
? "yes" : "no");
2602 winno
= count_win(ws
, 0);
2603 if (winno
== 0 && count_win(ws
, 1) == 0)
2606 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2607 if (win
->transient
== 0 && win
->floating
== 0
2608 && win
->iconic
== 0)
2615 w_inc
= win
->sh
.width_inc
;
2616 w_base
= win
->sh
.base_width
;
2617 mwin
= ws
->l_state
.horizontal_mwin
;
2618 mscale
= ws
->l_state
.horizontal_msize
;
2619 stacks
= ws
->l_state
.horizontal_stacks
;
2622 w_inc
= win
->sh
.height_inc
;
2623 w_base
= win
->sh
.base_height
;
2624 mwin
= ws
->l_state
.vertical_mwin
;
2625 mscale
= ws
->l_state
.vertical_msize
;
2626 stacks
= ws
->l_state
.vertical_stacks
;
2630 if (stacks
> winno
- mwin
)
2631 stacks
= winno
- mwin
;
2635 h_slice
= r_g
.h
/ SWM_H_SLICE
;
2636 if (mwin
&& winno
> mwin
) {
2637 v_slice
= r_g
.w
/ SWM_V_SLICE
;
2641 win_g
.w
= v_slice
* mscale
;
2643 if (w_inc
> 1 && w_inc
< v_slice
) {
2644 /* adjust for window's requested size increment */
2645 remain
= (win_g
.w
- w_base
) % w_inc
;
2646 missing
= w_inc
- remain
;
2653 win_g
.x
+= r_g
.w
- msize
;
2656 colno
= split
= winno
/ stacks
;
2657 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
2658 2 * border_width
) / stacks
);
2660 hrh
= r_g
.h
/ colno
;
2661 extra
= r_g
.h
- (colno
* hrh
);
2662 win_g
.h
= hrh
- 2 * border_width
;
2664 /* stack all the tiled windows */
2665 i
= j
= 0, s
= stacks
;
2666 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2667 if (win
->transient
!= 0 || win
->floating
!= 0)
2669 if (win
->iconic
!= 0)
2672 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
2677 if (split
&& i
== split
) {
2678 colno
= (winno
- mwin
) / stacks
;
2679 if (s
<= (winno
- mwin
) % stacks
)
2681 split
= split
+ colno
;
2682 hrh
= (r_g
.h
/ colno
);
2683 extra
= r_g
.h
- (colno
* hrh
);
2687 win_g
.x
+= win_g
.w
+ 2 * border_width
;
2688 win_g
.w
= (r_g
.w
- msize
-
2689 (stacks
* 2 * border_width
)) / stacks
;
2691 win_g
.w
+= (r_g
.w
- msize
-
2692 (stacks
* 2 * border_width
)) % stacks
;
2696 win_g
.h
= hrh
- 2 * border_width
;
2698 h_inc
= win
->sh
.width_inc
;
2699 h_base
= win
->sh
.base_width
;
2701 h_inc
= win
->sh
.height_inc
;
2702 h_base
= win
->sh
.base_height
;
2704 if (j
== colno
- 1) {
2705 win_g
.h
= hrh
+ extra
;
2706 } else if (h_inc
> 1 && h_inc
< h_slice
) {
2707 /* adjust for window's requested size increment */
2708 remain
= (win_g
.h
- h_base
) % h_inc
;
2709 missing
= h_inc
- remain
;
2711 if (missing
<= extra
|| j
== 0) {
2723 win_g
.y
+= last_h
+ 2 * border_width
;
2725 bzero(&wc
, sizeof wc
);
2726 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
2727 wc
.border_width
= 0;
2728 win_g
.w
+= 2 * border_width
;
2729 win_g
.h
+= 2 * border_width
;
2731 wc
.border_width
= border_width
;
2734 if (win
->g
.x
!= win_g
.y
|| win
->g
.y
!= win_g
.x
||
2735 win
->g
.w
!= win_g
.h
|| win
->g
.h
!= win_g
.w
) {
2737 win
->g
.x
= wc
.x
= win_g
.y
;
2738 win
->g
.y
= wc
.y
= win_g
.x
;
2739 win
->g
.w
= wc
.width
= win_g
.h
;
2740 win
->g
.h
= wc
.height
= win_g
.w
;
2743 if (win
->g
.x
!= win_g
.x
|| win
->g
.y
!= win_g
.y
||
2744 win
->g
.w
!= win_g
.w
|| win
->g
.h
!= win_g
.h
) {
2746 win
->g
.x
= wc
.x
= win_g
.x
;
2747 win
->g
.y
= wc
.y
= win_g
.y
;
2748 win
->g
.w
= wc
.width
= win_g
.w
;
2749 win
->g
.h
= wc
.height
= win_g
.h
;
2754 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
2755 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2758 if (XGetWindowAttributes(display
, win
->id
, &wa
))
2759 if (wa
.map_state
== IsUnmapped
)
2760 XMapRaised(display
, win
->id
);
2768 /* now, stack all the floaters and transients */
2769 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2770 if (win
->transient
== 0 && win
->floating
== 0)
2772 if (win
->iconic
== 1)
2774 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
2779 stack_floater(win
, ws
->r
);
2780 XMapRaised(display
, win
->id
);
2784 stack_floater(fs_win
, ws
->r
);
2785 XMapRaised(display
, fs_win
->id
);
2790 vertical_config(struct workspace
*ws
, int id
)
2792 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
2795 case SWM_ARG_ID_STACKRESET
:
2796 case SWM_ARG_ID_STACKINIT
:
2797 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
2798 ws
->l_state
.vertical_mwin
= 1;
2799 ws
->l_state
.vertical_stacks
= 1;
2801 case SWM_ARG_ID_MASTERSHRINK
:
2802 if (ws
->l_state
.vertical_msize
> 1)
2803 ws
->l_state
.vertical_msize
--;
2805 case SWM_ARG_ID_MASTERGROW
:
2806 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
2807 ws
->l_state
.vertical_msize
++;
2809 case SWM_ARG_ID_MASTERADD
:
2810 ws
->l_state
.vertical_mwin
++;
2812 case SWM_ARG_ID_MASTERDEL
:
2813 if (ws
->l_state
.vertical_mwin
> 0)
2814 ws
->l_state
.vertical_mwin
--;
2816 case SWM_ARG_ID_STACKINC
:
2817 ws
->l_state
.vertical_stacks
++;
2819 case SWM_ARG_ID_STACKDEC
:
2820 if (ws
->l_state
.vertical_stacks
> 1)
2821 ws
->l_state
.vertical_stacks
--;
2829 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
2831 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
2833 stack_master(ws
, g
, 0, 0);
2837 horizontal_config(struct workspace
*ws
, int id
)
2839 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
2842 case SWM_ARG_ID_STACKRESET
:
2843 case SWM_ARG_ID_STACKINIT
:
2844 ws
->l_state
.horizontal_mwin
= 1;
2845 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
2846 ws
->l_state
.horizontal_stacks
= 1;
2848 case SWM_ARG_ID_MASTERSHRINK
:
2849 if (ws
->l_state
.horizontal_msize
> 1)
2850 ws
->l_state
.horizontal_msize
--;
2852 case SWM_ARG_ID_MASTERGROW
:
2853 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
2854 ws
->l_state
.horizontal_msize
++;
2856 case SWM_ARG_ID_MASTERADD
:
2857 ws
->l_state
.horizontal_mwin
++;
2859 case SWM_ARG_ID_MASTERDEL
:
2860 if (ws
->l_state
.horizontal_mwin
> 0)
2861 ws
->l_state
.horizontal_mwin
--;
2863 case SWM_ARG_ID_STACKINC
:
2864 ws
->l_state
.horizontal_stacks
++;
2866 case SWM_ARG_ID_STACKDEC
:
2867 if (ws
->l_state
.horizontal_stacks
> 1)
2868 ws
->l_state
.horizontal_stacks
--;
2876 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
2878 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
2880 stack_master(ws
, g
, 1, 0);
2883 /* fullscreen view */
2885 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
2888 struct swm_geometry gg
= *g
;
2889 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
2893 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
2898 winno
= count_win(ws
, 0);
2899 if (winno
== 0 && count_win(ws
, 1) == 0)
2902 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2903 if (win
->transient
) {
2905 parent
= find_window(win
->transient
);
2909 if (win
->floating
&& win
->floatmaxed
== 0 ) {
2911 * retain geometry for retrieval on exit from
2914 store_float_geom(win
, ws
->r
);
2915 win
->floatmaxed
= 1;
2918 /* only reconfigure if necessary */
2919 if (win
->g
.x
!= gg
.x
|| win
->g
.y
!= gg
.y
|| win
->g
.w
!= gg
.w
||
2921 bzero(&wc
, sizeof wc
);
2922 win
->g
.x
= wc
.x
= gg
.x
;
2923 win
->g
.y
= wc
.y
= gg
.y
;
2925 wc
.border_width
= border_width
;
2926 win
->g
.w
= wc
.width
= gg
.w
;
2927 win
->g
.h
= wc
.height
= gg
.h
;
2929 wc
.border_width
= 0;
2930 win
->g
.w
= wc
.width
= gg
.w
+ 2 * border_width
;
2931 win
->g
.h
= wc
.height
= gg
.h
+ 2 * border_width
;
2933 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
2934 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2936 /* unmap only if we don't have multi screen */
2937 if (win
!= ws
->focus
)
2938 if (!(ScreenCount(display
) > 1 || outputs
> 1))
2942 /* put the last transient on top */
2945 XMapRaised(display
, parent
->id
);
2946 stack_floater(wintrans
, ws
->r
);
2947 focus_magic(wintrans
);
2952 send_to_ws(struct swm_region
*r
, union arg
*args
)
2954 int wsid
= args
->id
;
2955 struct ws_win
*win
= NULL
, *parent
;
2956 struct workspace
*ws
, *nws
;
2957 Atom ws_idx_atom
= 0;
2958 unsigned char ws_idx_str
[SWM_PROPLEN
];
2967 if (win
->ws
->idx
== wsid
)
2970 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
2973 nws
= &win
->s
->ws
[wsid
];
2975 a
.id
= SWM_ARG_ID_FOCUSPREV
;
2977 if (win
->transient
) {
2978 parent
= find_window(win
->transient
);
2980 unmap_window(parent
);
2981 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
2982 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
2987 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
2988 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
2991 /* Try to update the window's workspace property */
2992 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
2994 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
2995 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
2997 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
2998 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
3005 iconify(struct swm_region
*r
, union arg
*args
)
3009 if (r
->ws
->focus
== NULL
)
3011 unmap_window(r
->ws
->focus
);
3012 update_iconic(r
->ws
->focus
, 1);
3014 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3015 drain_enter_notify();
3016 r
->ws
->focus
= NULL
;
3017 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3022 get_win_name(Display
*dpy
, Window win
, Atom wname
, Atom stype
,
3023 unsigned long *slen
)
3026 unsigned long nitems
, nbytes
, nextra
;
3027 unsigned char *prop
= NULL
;
3030 status
= XGetWindowProperty(dpy
, win
, wname
, 0L, 0L, False
, stype
,
3031 &rettype
, &retfmt
, &nitems
, &nbytes
, &prop
);
3032 if (status
!= Success
)
3036 status
= XGetWindowProperty(dpy
, win
, wname
, 0L, nbytes
, False
,
3037 stype
, &rettype
, &retfmt
, &nitems
, &nextra
, &prop
);
3038 if (status
!= Success
) {
3042 if (rettype
!= stype
) {
3051 uniconify(struct swm_region
*r
, union arg
*args
)
3059 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3061 if (r
&& r
->ws
== NULL
)
3064 /* make sure we have anything to uniconify */
3065 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3066 if (win
->ws
== NULL
)
3067 continue; /* should never happen */
3068 if (win
->iconic
== 0)
3077 spawn_select(r
, args
, "uniconify", &searchpid
);
3079 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3082 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3083 if (win
->ws
== NULL
)
3084 continue; /* should never happen */
3085 if (win
->iconic
== 0)
3088 name
= get_win_name(display
, win
->id
, a_wmname
, a_string
,
3092 fprintf(lfile
, "%s.%lu\n", name
, win
->id
);
3099 #define MAX_RESP_LEN 1024
3102 search_do_resp(void)
3106 char *name
, *resp
, *s
;
3109 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
3114 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
3115 fprintf(stderr
, "search: calloc\n");
3119 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
3121 fprintf(stderr
, "search: read error: %s\n", strerror(errno
));
3124 resp
[rbytes
] = '\0';
3127 DNPRINTF(SWM_D_MISC
, "search_do_resp: resp %s\n", resp
);
3128 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3129 if (win
->iconic
== 0)
3131 name
= get_win_name(display
, win
->id
, a_wmname
, a_string
, &len
);
3134 if (asprintf(&s
, "%s.%lu", name
, win
->id
) == -1) {
3139 if (strncmp(s
, resp
, len
) == 0) {
3140 /* XXX this should be a callback to generalize */
3141 update_iconic(win
, 0);
3152 wkill(struct swm_region
*r
, union arg
*args
)
3154 DNPRINTF(SWM_D_MISC
, "wkill %d\n", args
->id
);
3156 if (r
->ws
->focus
== NULL
)
3159 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
3160 XKillClient(display
, r
->ws
->focus
->id
);
3162 if (r
->ws
->focus
->can_delete
)
3163 client_msg(r
->ws
->focus
, adelete
);
3168 floating_toggle_win(struct ws_win
*win
)
3170 struct swm_region
*r
;
3180 /* reject floating toggles in max stack mode */
3181 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3184 if (win
->floating
) {
3185 if (!win
->floatmaxed
) {
3186 /* retain position for refloat */
3187 store_float_geom(win
, r
);
3191 if (win
->g_floatvalid
) {
3192 /* refloat at last floating relative position */
3193 win
->g
.x
= win
->g_float
.x
- win
->rg_float
.x
+ r
->g
.x
;
3194 win
->g
.y
= win
->g_float
.y
- win
->rg_float
.y
+ r
->g
.y
;
3195 win
->g
.w
= win
->g_float
.w
;
3196 win
->g
.h
= win
->g_float
.h
;
3201 ewmh_update_actions(win
);
3207 floating_toggle(struct swm_region
*r
, union arg
*args
)
3209 struct ws_win
*win
= r
->ws
->focus
;
3215 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
3216 _NET_WM_STATE_TOGGLE
);
3219 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3220 drain_enter_notify();
3222 if (win
== win
->ws
->focus
) {
3223 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3224 focus(win
->ws
->r
, &a
);
3229 resize_window(struct ws_win
*win
, int center
)
3233 struct swm_region
*r
;
3235 r
= root_to_region(win
->wa
.root
);
3236 bzero(&wc
, sizeof wc
);
3237 mask
= CWBorderWidth
| CWWidth
| CWHeight
;
3238 wc
.border_width
= border_width
;
3239 wc
.width
= win
->g
.w
;
3240 wc
.height
= win
->g
.h
;
3241 if (center
== SWM_ARG_ID_CENTER
) {
3242 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2 - border_width
;
3243 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2 - border_width
;
3247 DNPRINTF(SWM_D_STACK
, "resize_window: win %lu x %d y %d w %d h %d\n",
3248 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
3250 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3254 resize(struct ws_win
*win
, union arg
*args
)
3258 struct swm_region
*r
= win
->ws
->r
;
3262 DNPRINTF(SWM_D_MOUSE
, "resize: win %lu floating %d trans %lu\n",
3263 win
->id
, win
->floating
, win
->transient
);
3265 if (!(win
->transient
!= 0 || win
->floating
!= 0))
3268 /* reject resizes in max mode for floaters (transient ok) */
3269 if (win
->floatmaxed
)
3273 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
3277 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3278 drain_enter_notify();
3280 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
3281 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
3284 /* place pointer at bottom left corner or nearest point inside r */
3285 if ( win
->g
.x
+ win
->g
.w
< r
->g
.x
+ r
->g
.w
- 1)
3286 relx
= win
->g
.w
- 1;
3288 relx
= r
->g
.x
+ r
->g
.w
- win
->g
.x
- 1;
3290 if ( win
->g
.y
+ win
->g
.h
< r
->g
.y
+ r
->g
.h
- 1)
3291 rely
= win
->g
.h
- 1;
3293 rely
= r
->g
.y
+ r
->g
.h
- win
->g
.y
- 1;
3295 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, relx
, rely
);
3297 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
3298 SubstructureRedirectMask
, &ev
);
3300 case ConfigureRequest
:
3303 handler
[ev
.type
](&ev
);
3306 /* do not allow resize outside of region */
3307 if ( ev
.xmotion
.x_root
< r
->g
.x
||
3308 ev
.xmotion
.x_root
> r
->g
.x
+ r
->g
.w
- 1 ||
3309 ev
.xmotion
.y_root
< r
->g
.y
||
3310 ev
.xmotion
.y_root
> r
->g
.y
+ r
->g
.h
- 1)
3313 if (ev
.xmotion
.x
<= 1)
3315 if (ev
.xmotion
.y
<= 1)
3317 win
->g
.w
= ev
.xmotion
.x
+ 1;
3318 win
->g
.h
= ev
.xmotion
.y
+ 1;
3320 /* not free, don't sync more than 120 times / second */
3321 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
3322 time
= ev
.xmotion
.time
;
3323 XSync(display
, False
);
3324 resize_window(win
, args
->id
);
3328 } while (ev
.type
!= ButtonRelease
);
3330 XSync(display
, False
);
3331 resize_window(win
, args
->id
);
3333 store_float_geom(win
,r
);
3335 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
- 1,
3337 XUngrabPointer(display
, CurrentTime
);
3340 drain_enter_notify();
3344 move_window(struct ws_win
*win
)
3348 struct swm_region
*r
;
3350 r
= root_to_region(win
->wa
.root
);
3351 bzero(&wc
, sizeof wc
);
3355 wc
.border_width
= border_width
;
3357 DNPRINTF(SWM_D_STACK
, "move_window: win %lu x %d y %d w %d h %d\n",
3358 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
3360 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3364 move(struct ws_win
*win
, union arg
*args
)
3368 struct swm_region
*r
= win
->ws
->r
;
3370 DNPRINTF(SWM_D_MOUSE
, "move: win %lu floating %d trans %lu\n",
3371 win
->id
, win
->floating
, win
->transient
);
3373 /* in max_stack mode should only move transients */
3374 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
3378 if (win
->floating
== 0 && !win
->transient
) {
3379 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
3382 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
3387 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
3388 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
3390 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
3392 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
3393 SubstructureRedirectMask
, &ev
);
3395 case ConfigureRequest
:
3398 handler
[ev
.type
](&ev
);
3401 /* don't allow to move window origin out of region */
3402 if ( ev
.xmotion
.x_root
< r
->g
.x
||
3403 ev
.xmotion
.x_root
> r
->g
.x
+ r
->g
.w
- 1 ||
3404 ev
.xmotion
.y_root
< r
->g
.y
||
3405 ev
.xmotion
.y_root
> r
->g
.y
+ r
->g
.h
- 1)
3408 win
->g
.x
= ev
.xmotion
.x_root
- border_width
;
3409 win
->g
.y
= ev
.xmotion
.y_root
- border_width
;
3411 /* not free, don't sync more than 120 times / second */
3412 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
3413 time
= ev
.xmotion
.time
;
3414 XSync(display
, False
);
3419 } while (ev
.type
!= ButtonRelease
);
3421 XSync(display
, False
);
3424 store_float_geom(win
,r
);
3425 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
3426 XUngrabPointer(display
, CurrentTime
);
3429 drain_enter_notify();
3432 /* user/key callable function IDs */
3489 kf_dumpwins
, /* MUST BE LAST */
3493 /* key definitions */
3495 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
3500 legacyfunc(struct swm_region
*r
, union arg
*args
)
3505 char name
[SWM_FUNCNAME_LEN
];
3506 void (*func
)(struct swm_region
*r
, union arg
*);
3508 } keyfuncs
[kf_invalid
+ 1] = {
3509 /* name function argument */
3510 { "cycle_layout", cycle_layout
, {0} },
3511 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
3512 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
3513 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
3514 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
3515 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
3516 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
3517 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
3518 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
3519 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
3520 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
3521 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
3522 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
3523 { "spawn_term", spawnterm
, {.argv
= spawn_term
} },
3524 { "spawn_menu", legacyfunc
, {0} },
3525 { "quit", quit
, {0} },
3526 { "restart", restart
, {0} },
3527 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
3528 { "ws_1", switchws
, {.id
= 0} },
3529 { "ws_2", switchws
, {.id
= 1} },
3530 { "ws_3", switchws
, {.id
= 2} },
3531 { "ws_4", switchws
, {.id
= 3} },
3532 { "ws_5", switchws
, {.id
= 4} },
3533 { "ws_6", switchws
, {.id
= 5} },
3534 { "ws_7", switchws
, {.id
= 6} },
3535 { "ws_8", switchws
, {.id
= 7} },
3536 { "ws_9", switchws
, {.id
= 8} },
3537 { "ws_10", switchws
, {.id
= 9} },
3538 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
3539 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
3540 { "ws_prior", priorws
, {0} },
3541 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
3542 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
3543 { "mvws_1", send_to_ws
, {.id
= 0} },
3544 { "mvws_2", send_to_ws
, {.id
= 1} },
3545 { "mvws_3", send_to_ws
, {.id
= 2} },
3546 { "mvws_4", send_to_ws
, {.id
= 3} },
3547 { "mvws_5", send_to_ws
, {.id
= 4} },
3548 { "mvws_6", send_to_ws
, {.id
= 5} },
3549 { "mvws_7", send_to_ws
, {.id
= 6} },
3550 { "mvws_8", send_to_ws
, {.id
= 7} },
3551 { "mvws_9", send_to_ws
, {.id
= 8} },
3552 { "mvws_10", send_to_ws
, {.id
= 9} },
3553 { "bar_toggle", bar_toggle
, {0} },
3554 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
3555 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
3556 { "screenshot_all", legacyfunc
, {0} },
3557 { "screenshot_wind", legacyfunc
, {0} },
3558 { "float_toggle", floating_toggle
,{0} },
3559 { "version", version
, {0} },
3560 { "spawn_lock", legacyfunc
, {0} },
3561 { "spawn_initscr", legacyfunc
, {0} },
3562 { "spawn_custom", dummykeyfunc
, {0} },
3563 { "iconify", iconify
, {0} },
3564 { "uniconify", uniconify
, {0} },
3565 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
3566 { "invalid key func", NULL
, {0} },
3571 enum keyfuncid funcid
;
3574 int keys_size
= 0, keys_length
= 0;
3575 struct key
*keys
= NULL
;
3578 enum { client_click
, root_click
};
3580 unsigned int action
;
3582 unsigned int button
;
3583 void (*func
)(struct ws_win
*, union arg
*);
3586 /* action key mouse button func args */
3587 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
3588 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
3589 { client_click
, MODKEY
, Button1
, move
, {0} },
3593 update_modkey(unsigned int mod
)
3598 for (i
= 0; i
< keys_length
; i
++)
3599 if (keys
[i
].mod
& ShiftMask
)
3600 keys
[i
].mod
= mod
| ShiftMask
;
3604 for (i
= 0; i
< LENGTH(buttons
); i
++)
3605 if (buttons
[i
].mask
& ShiftMask
)
3606 buttons
[i
].mask
= mod
| ShiftMask
;
3608 buttons
[i
].mask
= mod
;
3618 int spawns_size
= 0, spawns_length
= 0;
3619 struct spawn_prog
*spawns
= NULL
;
3622 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
3625 struct spawn_prog
*prog
= NULL
;
3627 char *ap
, **real_args
;
3629 DNPRINTF(SWM_D_SPAWN
, "spawn_expand %s\n", spawn_name
);
3632 for (i
= 0; i
< spawns_length
; i
++) {
3633 if (!strcasecmp(spawn_name
, spawns
[i
].name
))
3637 fprintf(stderr
, "spawn_custom: program %s not found\n",
3642 /* make room for expanded args */
3643 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
3644 err(1, "spawn_custom: calloc real_args");
3646 /* expand spawn_args into real_args */
3647 for (i
= 0; i
< prog
->argc
; i
++) {
3649 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg = %s\n", ap
);
3650 if (!strcasecmp(ap
, "$bar_border")) {
3652 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
3654 err(1, "spawn_custom border color");
3655 } else if (!strcasecmp(ap
, "$bar_color")) {
3657 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
3659 err(1, "spawn_custom bar color");
3660 } else if (!strcasecmp(ap
, "$bar_font")) {
3661 if ((real_args
[i
] = strdup(bar_fonts
[bar_fidx
]))
3663 err(1, "spawn_custom bar fonts");
3664 } else if (!strcasecmp(ap
, "$bar_font_color")) {
3666 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
3668 err(1, "spawn_custom color font");
3669 } else if (!strcasecmp(ap
, "$color_focus")) {
3671 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
3673 err(1, "spawn_custom color focus");
3674 } else if (!strcasecmp(ap
, "$color_unfocus")) {
3676 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
3678 err(1, "spawn_custom color unfocus");
3680 /* no match --> copy as is */
3681 if ((real_args
[i
] = strdup(ap
)) == NULL
)
3682 err(1, "spawn_custom strdup(ap)");
3684 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg = %s\n",
3689 if ((swm_debug
& SWM_D_SPAWN
) != 0) {
3690 fprintf(stderr
, "spawn_custom: result = ");
3691 for (i
= 0; i
< prog
->argc
; i
++)
3692 fprintf(stderr
, "\"%s\" ", real_args
[i
]);
3693 fprintf(stderr
, "\n");
3696 *ret_args
= real_args
;
3697 return (prog
->argc
);
3701 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
3707 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
3711 spawn(r
->ws
->idx
, &a
, 1);
3713 for (i
= 0; i
< spawn_argc
; i
++)
3719 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
3725 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
3729 if (pipe(select_list_pipe
) == -1)
3730 err(1, "pipe error");
3731 if (pipe(select_resp_pipe
) == -1)
3732 err(1, "pipe error");
3734 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
3735 err(1, "could not disable SIGPIPE");
3736 switch (*pid
= fork()) {
3738 err(1, "cannot fork");
3741 if (dup2(select_list_pipe
[0], 0) == -1)
3743 if (dup2(select_resp_pipe
[1], 1) == -1)
3745 close(select_list_pipe
[1]);
3746 close(select_resp_pipe
[0]);
3747 spawn(r
->ws
->idx
, &a
, 0);
3749 default: /* parent */
3750 close(select_list_pipe
[0]);
3751 close(select_resp_pipe
[1]);
3755 for (i
= 0; i
< spawn_argc
; i
++)
3761 setspawn(struct spawn_prog
*prog
)
3765 if (prog
== NULL
|| prog
->name
== NULL
)
3769 for (i
= 0; i
< spawns_length
; i
++) {
3770 if (!strcmp(spawns
[i
].name
, prog
->name
)) {
3772 if (prog
->argv
== NULL
) {
3774 DNPRINTF(SWM_D_SPAWN
,
3775 "setspawn: delete #%d %s\n",
3777 free(spawns
[i
].name
);
3778 for (j
= 0; j
< spawns
[i
].argc
; j
++)
3779 free(spawns
[i
].argv
[j
]);
3780 free(spawns
[i
].argv
);
3781 j
= spawns_length
- 1;
3783 spawns
[i
] = spawns
[j
];
3788 DNPRINTF(SWM_D_SPAWN
,
3789 "setspawn: replace #%d %s\n",
3791 free(spawns
[i
].name
);
3792 for (j
= 0; j
< spawns
[i
].argc
; j
++)
3793 free(spawns
[i
].argv
[j
]);
3794 free(spawns
[i
].argv
);
3797 /* found case handled */
3803 if (prog
->argv
== NULL
) {
3805 "error: setspawn: cannot find program %s", prog
->name
);
3810 /* not found: add */
3811 if (spawns_size
== 0 || spawns
== NULL
) {
3813 DNPRINTF(SWM_D_SPAWN
, "setspawn: init list %d\n", spawns_size
);
3814 spawns
= malloc((size_t)spawns_size
*
3815 sizeof(struct spawn_prog
));
3816 if (spawns
== NULL
) {
3817 fprintf(stderr
, "setspawn: malloc failed\n");
3821 } else if (spawns_length
== spawns_size
) {
3823 DNPRINTF(SWM_D_SPAWN
, "setspawn: grow list %d\n", spawns_size
);
3824 spawns
= realloc(spawns
, (size_t)spawns_size
*
3825 sizeof(struct spawn_prog
));
3826 if (spawns
== NULL
) {
3827 fprintf(stderr
, "setspawn: realloc failed\n");
3833 if (spawns_length
< spawns_size
) {
3834 DNPRINTF(SWM_D_SPAWN
, "setspawn: add #%d %s\n",
3835 spawns_length
, prog
->name
);
3836 i
= spawns_length
++;
3839 fprintf(stderr
, "spawns array problem?\n");
3840 if (spawns
== NULL
) {
3841 fprintf(stderr
, "spawns array is NULL!\n");
3849 setconfspawn(char *selector
, char *value
, int flags
)
3851 struct spawn_prog
*prog
;
3852 char *vp
, *cp
, *word
;
3854 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
3855 if ((prog
= calloc(1, sizeof *prog
)) == NULL
)
3856 err(1, "setconfspawn: calloc prog");
3857 prog
->name
= strdup(selector
);
3858 if (prog
->name
== NULL
)
3859 err(1, "setconfspawn prog->name");
3860 if ((cp
= vp
= strdup(value
)) == NULL
)
3861 err(1, "setconfspawn: strdup(value) ");
3862 while ((word
= strsep(&cp
, " \t")) != NULL
) {
3863 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: arg [%s]\n", word
);
3865 cp
+= (long)strspn(cp
, " \t");
3866 if (strlen(word
) > 0) {
3868 if ((prog
->argv
= realloc(prog
->argv
,
3869 prog
->argc
* sizeof(char *))) == NULL
)
3870 err(1, "setconfspawn: realloc");
3871 if ((prog
->argv
[prog
->argc
- 1] = strdup(word
)) == NULL
)
3872 err(1, "setconfspawn: strdup");
3879 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
3886 setconfspawn("term", "xterm", 0);
3887 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3888 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3889 setconfspawn("lock", "xlock", 0);
3890 setconfspawn("initscr", "initscreen.sh", 0);
3891 setconfspawn("menu", "dmenu_run"
3894 " -nf $bar_font_color"
3896 " -sf $bar_color", 0);
3897 setconfspawn("uniconify", "dmenu"
3901 " -nf $bar_font_color"
3903 " -sf $bar_color", 0);
3907 #define SWM_MODNAME_SIZE 32
3908 #define SWM_KEY_WS "\n+ \t"
3910 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
3914 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
3915 if (mod
== NULL
|| ks
== NULL
) {
3916 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
3919 if (keystr
== NULL
|| strlen(keystr
) == 0) {
3920 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
3926 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
3927 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
3929 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
3930 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
3932 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
3934 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
3936 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
3938 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
3940 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
3942 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
3943 *mod
|= ControlMask
;
3945 *ks
= XStringToKeysym(name
);
3946 XConvertCase(*ks
, ks
, &uks
);
3947 if (ks
== NoSymbol
) {
3949 "parsekeys: invalid key %s\n",
3955 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
3960 strdupsafe(char *str
)
3965 return (strdup(str
));
3969 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
3973 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
3974 keyfuncs
[kfid
].name
, spawn_name
);
3976 for (i
= 0; i
< keys_length
; i
++) {
3977 if (keys
[i
].mod
== mod
&& keys
[i
].keysym
== ks
) {
3978 if (kfid
== kf_invalid
) {
3981 "setkeybinding: delete #%d %s\n",
3982 i
, keyfuncs
[keys
[i
].funcid
].name
);
3983 free(keys
[i
].spawn_name
);
3984 j
= keys_length
- 1;
3988 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
3991 /* found: replace */
3993 "setkeybinding: replace #%d %s %s\n",
3994 i
, keyfuncs
[keys
[i
].funcid
].name
,
3996 free(keys
[i
].spawn_name
);
3998 keys
[i
].keysym
= ks
;
3999 keys
[i
].funcid
= kfid
;
4000 keys
[i
].spawn_name
= strdupsafe(spawn_name
);
4001 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4006 if (kfid
== kf_invalid
) {
4008 "error: setkeybinding: cannot find mod/key combination");
4009 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4012 /* not found: add */
4013 if (keys_size
== 0 || keys
== NULL
) {
4015 DNPRINTF(SWM_D_KEY
, "setkeybinding: init list %d\n", keys_size
);
4016 keys
= malloc((size_t)keys_size
* sizeof(struct key
));
4018 fprintf(stderr
, "malloc failed\n");
4022 } else if (keys_length
== keys_size
) {
4024 DNPRINTF(SWM_D_KEY
, "setkeybinding: grow list %d\n", keys_size
);
4025 keys
= realloc(keys
, (size_t)keys_size
* sizeof(struct key
));
4027 fprintf(stderr
, "realloc failed\n");
4032 if (keys_length
< keys_size
) {
4034 DNPRINTF(SWM_D_KEY
, "setkeybinding: add #%d %s %s\n",
4035 j
, keyfuncs
[kfid
].name
, spawn_name
);
4037 keys
[j
].keysym
= ks
;
4038 keys
[j
].funcid
= kfid
;
4039 keys
[j
].spawn_name
= strdupsafe(spawn_name
);
4041 fprintf(stderr
, "keys array problem?\n");
4043 fprintf(stderr
, "keys array problem\n");
4047 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4051 setconfbinding(char *selector
, char *value
, int flags
)
4053 enum keyfuncid kfid
;
4057 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
4058 if (selector
== NULL
) {
4059 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
4060 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4062 setkeybinding(mod
, ks
, kfid
, NULL
);
4067 /* search by key function name */
4068 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
4069 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
4070 SWM_FUNCNAME_LEN
) == 0) {
4071 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4073 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4074 setkeybinding(mod
, ks
, kfid
, NULL
);
4080 /* search by custom spawn name */
4081 for (i
= 0; i
< spawns_length
; i
++) {
4082 if (strcasecmp(selector
, spawns
[i
].name
) == 0) {
4083 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4085 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4086 setkeybinding(mod
, ks
, kf_spawn_custom
,
4093 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
4100 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
4101 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
4102 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
4103 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
4104 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
4105 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
4106 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
4107 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
4108 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
4109 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
4110 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
4111 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
4112 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
4113 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_term
, NULL
);
4114 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
, "menu");
4115 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
4116 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
4117 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
4118 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
4119 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
4120 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
4121 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
4122 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
4123 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
4124 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
4125 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
4126 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
4127 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
4128 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
4129 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
4130 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
4131 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
4132 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
4133 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
4134 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
4135 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
4136 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
4137 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
4138 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
4139 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
4140 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
4141 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
4142 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
4143 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
4144 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
4145 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
4146 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
4147 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
4148 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
, "screenshot_all");
4149 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
, "screenshot_wind");
4150 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
4151 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
4152 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
, "lock");
4153 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
, "initscr");
4154 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
4155 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
4157 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
4162 updatenumlockmask(void)
4165 XModifierKeymap
*modmap
;
4167 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
4169 modmap
= XGetModifierMapping(display
);
4170 for (i
= 0; i
< 8; i
++)
4171 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
4172 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
4173 == XKeysymToKeycode(display
, XK_Num_Lock
))
4174 numlockmask
= (1 << i
);
4176 XFreeModifiermap(modmap
);
4182 unsigned int i
, j
, k
;
4184 unsigned int modifiers
[] =
4185 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
4187 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
4188 updatenumlockmask();
4190 for (k
= 0; k
< ScreenCount(display
); k
++) {
4191 if (TAILQ_EMPTY(&screens
[k
].rl
))
4193 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
4194 for (i
= 0; i
< keys_length
; i
++) {
4195 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
4196 for (j
= 0; j
< LENGTH(modifiers
); j
++)
4197 XGrabKey(display
, code
,
4198 keys
[i
].mod
| modifiers
[j
],
4199 screens
[k
].root
, True
,
4200 GrabModeAsync
, GrabModeAsync
);
4206 grabbuttons(struct ws_win
*win
, int focused
)
4209 unsigned int modifiers
[] =
4210 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
4212 updatenumlockmask();
4213 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
4215 for (i
= 0; i
< LENGTH(buttons
); i
++)
4216 if (buttons
[i
].action
== client_click
)
4217 for (j
= 0; j
< LENGTH(modifiers
); j
++)
4218 XGrabButton(display
, buttons
[i
].button
,
4219 buttons
[i
].mask
| modifiers
[j
],
4220 win
->id
, False
, BUTTONMASK
,
4221 GrabModeAsync
, GrabModeSync
, None
,
4224 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
4225 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
4228 const char *quirkname
[] = {
4229 "NONE", /* config string for "no value" */
4238 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4239 #define SWM_Q_WS "\n|+ \t"
4241 parsequirks(char *qstr
, unsigned long *quirk
)
4251 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
4253 cp
+= (long)strspn(cp
, SWM_Q_WS
);
4254 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
4255 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
4256 DNPRINTF(SWM_D_QUIRK
,
4257 "parsequirks: %s\n", name
);
4262 *quirk
|= 1 << (i
-1);
4266 if (i
>= LENGTH(quirkname
)) {
4267 DNPRINTF(SWM_D_QUIRK
,
4268 "parsequirks: invalid quirk [%s]\n", name
);
4276 setquirk(const char *class, const char *name
, const int quirk
)
4281 for (i
= 0; i
< quirks_length
; i
++) {
4282 if (!strcmp(quirks
[i
].class, class) &&
4283 !strcmp(quirks
[i
].name
, name
)) {
4286 DNPRINTF(SWM_D_QUIRK
,
4287 "setquirk: delete #%d %s:%s\n",
4288 i
, quirks
[i
].class, quirks
[i
].name
);
4289 free(quirks
[i
].class);
4290 free(quirks
[i
].name
);
4291 j
= quirks_length
- 1;
4293 quirks
[i
] = quirks
[j
];
4297 /* found: replace */
4298 DNPRINTF(SWM_D_QUIRK
,
4299 "setquirk: replace #%d %s:%s\n",
4300 i
, quirks
[i
].class, quirks
[i
].name
);
4301 free(quirks
[i
].class);
4302 free(quirks
[i
].name
);
4303 quirks
[i
].class = strdup(class);
4304 quirks
[i
].name
= strdup(name
);
4305 quirks
[i
].quirk
= quirk
;
4312 "error: setquirk: cannot find class/name combination");
4315 /* not found: add */
4316 if (quirks_size
== 0 || quirks
== NULL
) {
4318 DNPRINTF(SWM_D_QUIRK
, "setquirk: init list %d\n", quirks_size
);
4319 quirks
= malloc((size_t)quirks_size
* sizeof(struct quirk
));
4320 if (quirks
== NULL
) {
4321 fprintf(stderr
, "setquirk: malloc failed\n");
4325 } else if (quirks_length
== quirks_size
) {
4327 DNPRINTF(SWM_D_QUIRK
, "setquirk: grow list %d\n", quirks_size
);
4328 quirks
= realloc(quirks
,
4329 (size_t)quirks_size
* sizeof(struct quirk
));
4330 if (quirks
== NULL
) {
4331 fprintf(stderr
, "setquirk: realloc failed\n");
4336 if (quirks_length
< quirks_size
) {
4337 DNPRINTF(SWM_D_QUIRK
, "setquirk: add %d\n", quirks_length
);
4338 j
= quirks_length
++;
4339 quirks
[j
].class = strdup(class);
4340 quirks
[j
].name
= strdup(name
);
4341 quirks
[j
].quirk
= quirk
;
4343 fprintf(stderr
, "quirks array problem?\n");
4344 if (quirks
== NULL
) {
4345 fprintf(stderr
, "quirks array problem!\n");
4352 setconfquirk(char *selector
, char *value
, int flags
)
4354 char *cp
, *class, *name
;
4356 unsigned long quirks
;
4357 if (selector
== NULL
)
4359 if ((cp
= strchr(selector
, ':')) == NULL
)
4364 if ((retval
= parsequirks(value
, &quirks
)) == 0)
4365 setquirk(class, name
, quirks
);
4372 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
4373 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
4374 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
4375 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
4376 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
4377 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
4378 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
4379 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
4380 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
4381 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
4382 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
4383 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
4384 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
4387 /* conf file stuff */
4388 #define SWM_CONF_FILE "scrotwm.conf"
4390 enum { SWM_S_BAR_DELAY
, SWM_S_BAR_ENABLED
, SWM_S_BAR_BORDER_WIDTH
,
4391 SWM_S_STACK_ENABLED
, SWM_S_CLOCK_ENABLED
, SWM_S_CLOCK_FORMAT
,
4392 SWM_S_CYCLE_EMPTY
, SWM_S_CYCLE_VISIBLE
, SWM_S_SS_ENABLED
,
4393 SWM_S_TERM_WIDTH
, SWM_S_TITLE_CLASS_ENABLED
,
4394 SWM_S_TITLE_NAME_ENABLED
, SWM_S_WINDOW_NAME_ENABLED
,
4395 SWM_S_FOCUS_MODE
, SWM_S_DISABLE_BORDER
, SWM_S_BORDER_WIDTH
,
4396 SWM_S_BAR_FONT
, SWM_S_BAR_ACTION
, SWM_S_SPAWN_TERM
,
4397 SWM_S_SS_APP
, SWM_S_DIALOG_RATIO
, SWM_S_BAR_AT_BOTTOM
4401 setconfvalue(char *selector
, char *value
, int flags
)
4404 case SWM_S_BAR_DELAY
:
4405 bar_delay
= atoi(value
);
4407 case SWM_S_BAR_ENABLED
:
4408 bar_enabled
= atoi(value
);
4410 case SWM_S_BAR_BORDER_WIDTH
:
4411 bar_border_width
= atoi(value
);
4413 case SWM_S_BAR_AT_BOTTOM
:
4414 bar_at_bottom
= atoi(value
);
4416 case SWM_S_STACK_ENABLED
:
4417 stack_enabled
= atoi(value
);
4419 case SWM_S_CLOCK_ENABLED
:
4420 clock_enabled
= atoi(value
);
4422 case SWM_S_CLOCK_FORMAT
:
4423 #ifndef SWM_DENY_CLOCK_FORMAT
4425 if ((clock_format
= strdup(value
)) == NULL
)
4426 err(1, "setconfvalue: clock_format");
4429 case SWM_S_CYCLE_EMPTY
:
4430 cycle_empty
= atoi(value
);
4432 case SWM_S_CYCLE_VISIBLE
:
4433 cycle_visible
= atoi(value
);
4435 case SWM_S_SS_ENABLED
:
4436 ss_enabled
= atoi(value
);
4438 case SWM_S_TERM_WIDTH
:
4439 term_width
= atoi(value
);
4441 case SWM_S_TITLE_CLASS_ENABLED
:
4442 title_class_enabled
= atoi(value
);
4444 case SWM_S_WINDOW_NAME_ENABLED
:
4445 window_name_enabled
= atoi(value
);
4447 case SWM_S_TITLE_NAME_ENABLED
:
4448 title_name_enabled
= atoi(value
);
4450 case SWM_S_FOCUS_MODE
:
4451 if (!strcmp(value
, "default"))
4452 focus_mode
= SWM_FOCUS_DEFAULT
;
4453 else if (!strcmp(value
, "follow_cursor"))
4454 focus_mode
= SWM_FOCUS_FOLLOW
;
4455 else if (!strcmp(value
, "synergy"))
4456 focus_mode
= SWM_FOCUS_SYNERGY
;
4458 err(1, "focus_mode");
4460 case SWM_S_DISABLE_BORDER
:
4461 disable_border
= atoi(value
);
4463 case SWM_S_BORDER_WIDTH
:
4464 border_width
= atoi(value
);
4466 case SWM_S_BAR_FONT
:
4468 if ((bar_fonts
[0] = strdup(value
)) == NULL
)
4469 err(1, "setconfvalue: bar_font");
4471 case SWM_S_BAR_ACTION
:
4473 if ((bar_argv
[0] = strdup(value
)) == NULL
)
4474 err(1, "setconfvalue: bar_action");
4476 case SWM_S_SPAWN_TERM
:
4477 free(spawn_term
[0]);
4478 if ((spawn_term
[0] = strdup(value
)) == NULL
)
4479 err(1, "setconfvalue: spawn_term");
4483 case SWM_S_DIALOG_RATIO
:
4484 dialog_ratio
= atof(value
);
4485 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
4495 setconfmodkey(char *selector
, char *value
, int flags
)
4497 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
4498 update_modkey(Mod1Mask
);
4499 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
4500 update_modkey(Mod2Mask
);
4501 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
4502 update_modkey(Mod3Mask
);
4503 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
4504 update_modkey(Mod4Mask
);
4511 setconfcolor(char *selector
, char *value
, int flags
)
4513 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
4518 setconfregion(char *selector
, char *value
, int flags
)
4520 custom_region(value
);
4525 setautorun(char *selector
, char *value
, int flags
)
4535 if (getenv("SWM_STARTED"))
4539 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
4540 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'\n");
4542 if (ws_id
< 0 || ws_id
>= SWM_WS_MAX
)
4543 errx(1, "autorun: invalid workspace %d\n", ws_id
+ 1);
4546 * This is a little intricate
4548 * If the pid already exists we simply reuse it because it means it was
4549 * used before AND not claimed by manage_window. We get away with
4550 * altering it in the parent after INSERT because this can not be a race
4553 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
4556 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
4558 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
4559 err(1, "setautorun: realloc");
4560 a
.argv
[argc
- 1] = ap
;
4563 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
4564 err(1, "setautorun: realloc");
4565 a
.argv
[argc
] = NULL
;
4567 if ((pid
= fork()) == 0) {
4568 spawn(ws_id
, &a
, 1);
4577 p
= calloc(1, sizeof *p
);
4580 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
4590 setlayout(char *selector
, char *value
, int flags
)
4592 int ws_id
, st
, i
, x
, mg
, ma
, si
;
4594 struct workspace
*ws
;
4596 if (getenv("SWM_STARTED"))
4600 if (sscanf(value
, "ws[%d]:%d:%d:%d:%1023c",
4601 &ws_id
, &mg
, &ma
, &si
, s
) != 5)
4602 errx(1, "invalid layout entry, should be 'ws[<idx>]:<type>'\n");
4604 if (ws_id
< 0 || ws_id
>= SWM_WS_MAX
)
4605 errx(1, "layout: invalid workspace %d\n", ws_id
+ 1);
4607 if (!strcasecmp(s
, "vertical"))
4609 else if (!strcasecmp(s
, "horizontal"))
4611 else if (!strcasecmp(s
, "fullscreen"))
4614 errx(1, "invalid layout entry, should be 'ws[<idx>]:<type>'\n");
4616 for (i
= 0; i
< ScreenCount(display
); i
++) {
4617 ws
= (struct workspace
*)&screens
[i
].ws
;
4618 ws
[ws_id
].cur_layout
= &layouts
[st
];
4619 if (st
== SWM_MAX_STACK
)
4623 for (x
= 0; x
< abs(mg
); x
++) {
4624 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
4625 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
4626 SWM_ARG_ID_MASTERSHRINK
);
4630 for (x
= 0; x
< abs(ma
); x
++) {
4631 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
4632 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
4633 SWM_ARG_ID_MASTERDEL
);
4637 for (x
= 0; x
< abs(si
); x
++) {
4638 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
4639 si
>= 0 ? SWM_ARG_ID_STACKINC
:
4640 SWM_ARG_ID_STACKDEC
);
4648 /* config options */
4649 struct config_option
{
4651 int (*func
)(char*, char*, int);
4654 struct config_option configopt
[] = {
4655 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
4656 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
4657 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
4658 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
4659 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
4660 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
4661 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
4662 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
4663 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
4664 { "bind", setconfbinding
, 0 },
4665 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
4666 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
4667 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
4668 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
4669 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
4670 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
4671 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
4672 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
4673 { "modkey", setconfmodkey
, 0 },
4674 { "program", setconfspawn
, 0 },
4675 { "quirk", setconfquirk
, 0 },
4676 { "region", setconfregion
, 0 },
4677 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
4678 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
4679 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
4680 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
4681 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
4682 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
4683 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
4684 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
4685 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
4686 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
4687 { "autorun", setautorun
, 0 },
4688 { "layout", setlayout
, 0 },
4693 conf_load(char *filename
)
4696 char *line
, *cp
, *optsub
, *optval
;
4697 size_t linelen
, lineno
= 0;
4698 int wordlen
, i
, optind
;
4699 struct config_option
*opt
;
4701 DNPRINTF(SWM_D_CONF
, "conf_load begin\n");
4703 if (filename
== NULL
) {
4704 fprintf(stderr
, "conf_load: no filename\n");
4707 if ((config
= fopen(filename
, "r")) == NULL
) {
4708 warn("conf_load: fopen");
4712 while (!feof(config
)) {
4713 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
4716 err(1, "%s", filename
);
4721 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
4722 if (cp
[0] == '\0') {
4727 /* get config option */
4728 wordlen
= strcspn(cp
, "=[ \t\n");
4730 warnx("%s: line %zd: no option found",
4735 for (i
= 0; i
< LENGTH(configopt
); i
++) {
4736 opt
= &configopt
[i
];
4737 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
4738 strlen(opt
->optname
) == wordlen
) {
4744 warnx("%s: line %zd: unknown option %.*s",
4745 filename
, lineno
, wordlen
, cp
);
4749 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
4750 /* get [selector] if any */
4754 wordlen
= strcspn(cp
, "]");
4757 warnx("%s: line %zd: syntax error",
4761 asprintf(&optsub
, "%.*s", wordlen
, cp
);
4764 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
4766 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
4768 optval
= strdup(cp
);
4769 /* call function to deal with it all */
4770 if (configopt
[optind
].func(optsub
, optval
,
4771 configopt
[optind
].funcflags
) != 0) {
4772 fprintf(stderr
, "%s line %zd: %s\n",
4773 filename
, lineno
, line
);
4774 errx(1, "%s: line %zd: invalid data for %s",
4775 filename
, lineno
, configopt
[optind
].optname
);
4783 DNPRINTF(SWM_D_CONF
, "conf_load end\n");
4789 set_child_transient(struct ws_win
*win
, Window
*trans
)
4791 struct ws_win
*parent
, *w
;
4792 XWMHints
*wmh
= NULL
;
4793 struct swm_region
*r
;
4794 struct workspace
*ws
;
4796 parent
= find_window(win
->transient
);
4798 parent
->child_trans
= win
;
4800 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
4801 " for %lu trans %lu\n", win
->id
, win
->transient
);
4803 if (win
->hints
== NULL
) {
4804 fprintf(stderr
, "no hints for %lu\n", win
->id
);
4808 r
= root_to_region(win
->wa
.root
);
4810 /* parent doen't exist in our window list */
4811 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
4815 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
4816 fprintf(stderr
, "can't get hints for %lu\n",
4821 if (win
->hints
->window_group
!= wmh
->window_group
)
4824 w
->child_trans
= win
;
4825 win
->transient
= w
->id
;
4827 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
4828 "transient to %lu\n", win
->transient
);
4838 window_get_pid(Window win
)
4840 Atom actual_type_return
;
4841 int actual_format_return
= 0;
4842 unsigned long nitems_return
= 0;
4843 unsigned long bytes_after_return
= 0;
4847 unsigned char *prop
= NULL
;
4849 if (XGetWindowProperty(display
, win
,
4850 XInternAtom(display
, "_NET_WM_PID", False
), 0, 1, False
,
4851 XA_CARDINAL
, &actual_type_return
, &actual_format_return
,
4852 &nitems_return
, &bytes_after_return
,
4853 (unsigned char**)(void*)&pid
) != Success
)
4855 if (actual_type_return
!= XA_CARDINAL
)
4866 if (XGetWindowProperty(display
, win
,
4867 XInternAtom(display
, "_SWM_PID", False
), 0, SWM_PROPLEN
, False
,
4868 XA_STRING
, &actual_type_return
, &actual_format_return
,
4869 &nitems_return
, &bytes_after_return
, &prop
) != Success
)
4871 if (actual_type_return
!= XA_STRING
)
4876 ret
= strtonum(prop
, 0, UINT_MAX
, &errstr
);
4877 /* ignore error because strtonum returns 0 anyway */
4884 manage_window(Window id
)
4887 struct workspace
*ws
;
4888 struct ws_win
*win
, *ww
;
4889 int format
, i
, ws_idx
, n
, border_me
= 0;
4890 unsigned long nitems
, bytes
;
4891 Atom ws_idx_atom
= 0, type
;
4892 Atom
*prot
= NULL
, *pp
;
4893 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
4894 struct swm_region
*r
;
4900 if ((win
= find_window(id
)) != NULL
)
4901 return (win
); /* already being managed */
4903 /* see if we are on the unmanaged list */
4904 if ((win
= find_unmanaged_window(id
)) != NULL
) {
4905 DNPRINTF(SWM_D_MISC
, "manage previously unmanaged window "
4907 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
4908 if (win
->transient
) {
4909 set_child_transient(win
, &trans
);
4910 } if (trans
&& (ww
= find_window(trans
)))
4911 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
4913 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
4914 ewmh_update_actions(win
);
4918 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
4919 errx(1, "calloc: failed to allocate memory for new window");
4923 /* see if we need to override the workspace */
4924 p
= find_pid(window_get_pid(id
));
4926 /* Get all the window data in one shot */
4927 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
4929 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
4930 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
4932 XGetWindowAttributes(display
, id
, &win
->wa
);
4933 XGetWMNormalHints(display
, id
, &win
->sh
, &mask
);
4934 win
->hints
= XGetWMHints(display
, id
);
4935 XGetTransientForHint(display
, id
, &trans
);
4937 win
->transient
= trans
;
4938 set_child_transient(win
, &trans
);
4939 DNPRINTF(SWM_D_MISC
, "manage_window: win %lu transient %lu\n",
4940 win
->id
, win
->transient
);
4943 /* get supported protocols */
4944 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
4945 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
4946 if (*pp
== takefocus
)
4947 win
->take_focus
= 1;
4949 win
->can_delete
= 1;
4955 win
->iconic
= get_iconic(win
);
4958 * Figure out where to put the window. If it was previously assigned to
4959 * a workspace (either by spawn() or manually moving), and isn't
4960 * transient, * put it in the same workspace
4962 r
= root_to_region(win
->wa
.root
);
4964 ws
= &r
->s
->ws
[p
->ws
];
4965 TAILQ_REMOVE(&pidlist
, p
, entry
);
4968 } else if (prop
&& win
->transient
== 0) {
4969 DNPRINTF(SWM_D_PROP
, "got property _SWM_WS=%s\n", prop
);
4970 ws_idx
= strtonum(prop
, 0, 9, &errstr
);
4972 DNPRINTF(SWM_D_EVENT
, "window idx is %s: %s",
4975 ws
= &r
->s
->ws
[ws_idx
];
4978 /* this should launch transients in the same ws as parent */
4980 if ((ww
= find_window(trans
)) != NULL
)
4987 "fix this bug mcbride\n");
4992 /* set up the window layout */
4995 win
->s
= r
->s
; /* this never changes */
4996 if (trans
&& (ww
= find_window(trans
)))
4997 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
4999 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
5001 win
->g
.w
= win
->wa
.width
;
5002 win
->g
.h
= win
->wa
.height
;
5003 win
->g
.x
= win
->wa
.x
;
5004 win
->g
.y
= win
->wa
.y
;
5005 win
->g_floatvalid
= 0;
5006 win
->floatmaxed
= 0;
5007 win
->ewmh_flags
= 0;
5009 /* Set window properties so we can remember this after reincarnation */
5010 if (ws_idx_atom
&& prop
== NULL
&&
5011 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) < SWM_PROPLEN
) {
5012 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
5014 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
5015 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
5020 ewmh_autoquirk(win
);
5022 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
5023 DNPRINTF(SWM_D_CLASS
, "class: %s name: %s\n",
5024 win
->ch
.res_class
, win
->ch
.res_name
);
5026 /* java is retarded so treat it special */
5027 if (strstr(win
->ch
.res_name
, "sun-awt")) {
5032 for (i
= 0; i
< quirks_length
; i
++){
5033 if (!strcmp(win
->ch
.res_class
, quirks
[i
].class) &&
5034 !strcmp(win
->ch
.res_name
, quirks
[i
].name
)) {
5035 DNPRINTF(SWM_D_CLASS
, "found: %s name: %s\n",
5036 win
->ch
.res_class
, win
->ch
.res_name
);
5037 if (quirks
[i
].quirk
& SWM_Q_FLOAT
) {
5041 win
->quirks
= quirks
[i
].quirk
;
5046 /* alter window position if quirky */
5047 if (win
->quirks
& SWM_Q_ANYWHERE
) {
5048 win
->manual
= 1; /* don't center the quirky windows */
5049 bzero(&wc
, sizeof wc
);
5051 if (bar_enabled
&& win
->g
.y
< bar_height
) {
5052 win
->g
.y
= wc
.y
= bar_height
;
5055 if (win
->g
.w
+ win
->g
.x
> WIDTH(r
)) {
5056 win
->g
.x
= wc
.x
= WIDTH(r
) - win
->g
.w
- 2;
5062 /* Reset font sizes (the bruteforce way; no default keybinding). */
5063 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
5064 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
5065 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
5066 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
5067 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
5070 ewmh_get_win_state(win
);
5071 ewmh_update_actions(win
);
5072 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
5076 bzero(&wc
, sizeof wc
);
5077 wc
.border_width
= border_width
;
5078 mask
= CWBorderWidth
;
5079 XConfigureWindow(display
, win
->id
, mask
, &wc
);
5082 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
5083 PropertyChangeMask
| StructureNotifyMask
);
5085 /* floaters need to be mapped if they are in the current workspace */
5086 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
5087 XMapRaised(display
, win
->id
);
5093 free_window(struct ws_win
*win
)
5095 DNPRINTF(SWM_D_MISC
, "free_window: %lu\n", win
->id
);
5100 /* needed for restart wm */
5101 set_win_state(win
, WithdrawnState
);
5103 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5105 if (win
->ch
.res_class
)
5106 XFree(win
->ch
.res_class
);
5107 if (win
->ch
.res_name
)
5108 XFree(win
->ch
.res_name
);
5113 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
5119 unmanage_window(struct ws_win
*win
)
5121 struct ws_win
*parent
;
5126 DNPRINTF(SWM_D_MISC
, "unmanage_window: %lu\n", win
->id
);
5128 if (win
->transient
) {
5129 parent
= find_window(win
->transient
);
5131 parent
->child_trans
= NULL
;
5134 /* focus on root just in case */
5135 XSetInputFocus(display
, PointerRoot
, PointerRoot
, CurrentTime
);
5139 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
5140 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
5146 focus_magic(struct ws_win
*win
)
5148 DNPRINTF(SWM_D_FOCUS
, "focus_magic: %lu\n", WINID(win
));
5153 if (win
->child_trans
) {
5154 /* win = parent & has a transient so focus on that */
5156 focus_win(win
->child_trans
);
5157 if (win
->child_trans
->take_focus
)
5158 client_msg(win
, takefocus
);
5160 /* make sure transient hasn't dissapeared */
5161 if (validate_win(win
->child_trans
) == 0) {
5162 focus_win(win
->child_trans
);
5163 if (win
->child_trans
->take_focus
)
5164 client_msg(win
->child_trans
, takefocus
);
5166 win
->child_trans
= NULL
;
5168 if (win
->take_focus
)
5169 client_msg(win
, takefocus
);
5175 if (win
->take_focus
)
5176 client_msg(win
, takefocus
);
5183 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
5191 XKeyEvent
*ev
= &e
->xkey
;
5193 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
5195 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
5196 for (i
= 0; i
< keys_length
; i
++)
5197 if (keysym
== keys
[i
].keysym
5198 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
5199 && keyfuncs
[keys
[i
].funcid
].func
) {
5200 if (keys
[i
].funcid
== kf_spawn_custom
)
5202 root_to_region(ev
->root
),
5203 &(keyfuncs
[keys
[i
].funcid
].args
),
5207 keyfuncs
[keys
[i
].funcid
].func(
5208 root_to_region(ev
->root
),
5209 &(keyfuncs
[keys
[i
].funcid
].args
)
5215 buttonpress(XEvent
*e
)
5219 XButtonPressedEvent
*ev
= &e
->xbutton
;
5221 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
5223 action
= root_click
;
5224 if ((win
= find_window(ev
->window
)) == NULL
)
5228 action
= client_click
;
5230 for (i
= 0; i
< LENGTH(buttons
); i
++)
5231 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
5232 buttons
[i
].button
== ev
->button
&&
5233 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
5234 buttons
[i
].func(win
, &buttons
[i
].args
);
5238 configurerequest(XEvent
*e
)
5240 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
5245 if ((win
= find_window(ev
->window
)) == NULL
)
5246 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
5250 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
5252 bzero(&wc
, sizeof wc
);
5255 wc
.width
= ev
->width
;
5256 wc
.height
= ev
->height
;
5257 wc
.border_width
= ev
->border_width
;
5258 wc
.sibling
= ev
->above
;
5259 wc
.stack_mode
= ev
->detail
;
5260 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
5262 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
5264 config_win(win
, ev
);
5269 configurenotify(XEvent
*e
)
5274 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
5275 e
->xconfigure
.window
);
5277 win
= find_window(e
->xconfigure
.window
);
5279 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
5283 if (focus_mode
== SWM_FOCUS_DEFAULT
)
5284 drain_enter_notify();
5289 destroynotify(XEvent
*e
)
5292 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
5294 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
5296 if ((win
= find_window(ev
->window
)) == NULL
) {
5297 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
5303 /* make sure we focus on something */
5306 unmanage_window(win
);
5308 if (focus_mode
== SWM_FOCUS_DEFAULT
)
5309 drain_enter_notify();
5314 enternotify(XEvent
*e
)
5316 XCrossingEvent
*ev
= &e
->xcrossing
;
5321 Window focus_return
;
5322 int revert_to_return
;
5324 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: %lu mode %d detail %d root "
5325 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5326 ev
->window
, ev
->mode
, ev
->detail
, ev
->root
, ev
->subwindow
,
5327 ev
->same_screen
, ev
->focus
, ev
->state
);
5329 switch (focus_mode
) {
5330 case SWM_FOCUS_DEFAULT
:
5332 case SWM_FOCUS_FOLLOW
:
5334 case SWM_FOCUS_SYNERGY
:
5337 * all these checks need to be in this order because the
5338 * XCheckTypedWindowEvent relies on weeding out the previous events
5340 * making this code an option would enable a follow mouse for focus
5345 * state is set when we are switching workspaces and focus is set when
5346 * the window or a subwindow already has focus (occurs during restart).
5348 * Only honor the focus flag if last_focus_event is not FocusOut,
5349 * this allows scrotwm to continue to control focus when another
5350 * program is also playing with it.
5352 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
5353 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
5358 * happens when a window is created or destroyed and the border
5359 * crosses the mouse pointer and when switching ws
5361 * we need the subwindow test to see if we came from root in order
5362 * to give focus to floaters
5364 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
5365 ev
->subwindow
== 0) {
5366 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
5370 /* this window already has focus */
5371 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
5372 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
5376 /* this window is being deleted or moved to another ws */
5377 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
5379 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
5380 XPutBackEvent(display
, &cne
);
5384 if ((win
= find_window(ev
->window
)) == NULL
) {
5385 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
5390 * In fullstack kill all enters unless they come from a different ws
5391 * (i.e. another region) or focus has been grabbed externally.
5393 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
5394 last_focus_event
!= FocusOut
) {
5395 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
5396 if ((w
= find_window(focus_return
)) == NULL
||
5398 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
5406 if ((win
= find_window(ev
->window
)) == NULL
) {
5407 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
5412 * if we have more enternotifies let them handle it in due time
5414 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
5415 DNPRINTF(SWM_D_EVENT
,
5416 "ignoring enternotify: got more enternotify\n");
5417 XPutBackEvent(display
, &cne
);
5424 /* lets us use one switch statement for arbitrary mode/detail combinations */
5425 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
5428 focusevent(XEvent
*e
)
5432 u_int32_t mode_detail
;
5433 XFocusChangeEvent
*ev
= &e
->xfocus
;
5435 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: %lu mode %d detail %d\n",
5436 ev
->type
== FocusIn
? "entering" : "leaving",
5437 ev
->window
, ev
->mode
, ev
->detail
);
5439 if (last_focus_event
== ev
->type
) {
5440 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
5444 last_focus_event
= ev
->type
;
5445 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
5447 switch (mode_detail
) {
5448 /* synergy client focus operations */
5449 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
5450 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
5452 /* synergy server focus operations */
5453 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
5455 /* Entering applications like rdesktop that mangle the pointer */
5456 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
5458 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
5459 XSetWindowBorder(display
, win
->id
,
5460 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
5461 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
5464 fprintf(stderr
, "ignoring focusevent\n");
5465 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
5472 mapnotify(XEvent
*e
)
5475 XMapEvent
*ev
= &e
->xmap
;
5477 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: %lu\n", ev
->window
);
5479 win
= manage_window(ev
->window
);
5481 set_win_state(win
, NormalState
);
5485 mappingnotify(XEvent
*e
)
5487 XMappingEvent
*ev
= &e
->xmapping
;
5489 XRefreshKeyboardMapping(ev
);
5490 if (ev
->request
== MappingKeyboard
)
5495 maprequest(XEvent
*e
)
5498 struct swm_region
*r
;
5499 XWindowAttributes wa
;
5500 XMapRequestEvent
*ev
= &e
->xmaprequest
;
5502 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
5503 e
->xmaprequest
.window
);
5505 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
5507 if (wa
.override_redirect
)
5510 win
= manage_window(e
->xmaprequest
.window
);
5512 return; /* can't happen */
5516 /* make new win focused */
5517 r
= root_to_region(win
->wa
.root
);
5518 if (win
->ws
== r
->ws
)
5523 propertynotify(XEvent
*e
)
5526 XPropertyEvent
*ev
= &e
->xproperty
;
5528 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
5531 win
= find_window(ev
->window
);
5535 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
5536 update_iconic(win
, 0);
5537 XMapRaised(display
, win
->id
);
5544 case XA_WM_NORMAL_HINTS
:
5547 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
5548 fprintf(stderr
, "normal hints: flag 0x%x\n", win
->sh
.flags
);
5549 if (win
->sh
.flags
& PMinSize
) {
5550 win
->g
.w
= win
->sh
.min_width
;
5551 win
->g
.h
= win
->sh
.min_height
;
5552 fprintf(stderr
, "min %d %d\n", win
->g
.w
, win
->g
.h
);
5554 XMoveResizeWindow(display
, win
->id
,
5555 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
5557 if (window_name_enabled
)
5566 unmapnotify(XEvent
*e
)
5570 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
5572 /* determine if we need to help unmanage this window */
5573 win
= find_window(e
->xunmap
.window
);
5577 if (getstate(e
->xunmap
.window
) == NormalState
) {
5578 unmanage_window(win
);
5581 /* giant hack for apps that don't destroy transient windows */
5582 /* eat a bunch of events to prevent remanaging the window */
5584 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
5585 EnterWindowMask
, &cne
))
5587 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
5588 StructureNotifyMask
, &cne
))
5590 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
5591 SubstructureNotifyMask
, &cne
))
5593 /* resend unmap because we ated it */
5594 XUnmapWindow(display
, e
->xunmap
.window
);
5597 if (focus_mode
== SWM_FOCUS_DEFAULT
)
5598 drain_enter_notify();
5602 visibilitynotify(XEvent
*e
)
5605 struct swm_region
*r
;
5607 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
5608 e
->xvisibility
.window
);
5609 if (e
->xvisibility
.state
== VisibilityUnobscured
)
5610 for (i
= 0; i
< ScreenCount(display
); i
++)
5611 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
5612 if (e
->xvisibility
.window
== r
->bar_window
)
5617 clientmessage(XEvent
*e
)
5619 XClientMessageEvent
*ev
;
5624 win
= find_window(ev
->window
);
5628 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx type: %ld \n",
5629 ev
->window
, ev
->message_type
);
5631 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
5632 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW \n");
5635 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
5636 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW \n");
5637 if (win
->can_delete
)
5638 client_msg(win
, adelete
);
5640 XKillClient(display
, win
->id
);
5642 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
5643 DNPRINTF(SWM_D_EVENT
,
5644 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
5645 if (win
->floating
) {
5646 if (ev
->data
.l
[0] & (1<<8)) /* x */
5647 win
->g
.x
= ev
->data
.l
[1];
5648 if (ev
->data
.l
[0] & (1<<9)) /* y */
5649 win
->g
.y
= ev
->data
.l
[2];
5650 if (ev
->data
.l
[0] & (1<<10)) /* width */
5651 win
->g
.w
= ev
->data
.l
[3];
5652 if (ev
->data
.l
[0] & (1<<11)) /* height */
5653 win
->g
.h
= ev
->data
.l
[4];
5656 /* TODO: Change stack sizes */
5658 config_win(win
, NULL
);
5660 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
5661 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE \n");
5662 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
5664 ewmh_update_win_state(win
, ev
->data
.l
[2],
5672 xerror_start(Display
*d
, XErrorEvent
*ee
)
5679 xerror(Display
*d
, XErrorEvent
*ee
)
5681 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5689 xerrorxlib
= XSetErrorHandler(xerror_start
);
5691 /* this causes an error if some other window manager is running */
5692 XSelectInput(display
, DefaultRootWindow(display
),
5693 SubstructureRedirectMask
);
5694 XSync(display
, False
);
5698 XSetErrorHandler(xerror
);
5699 XSync(display
, False
);
5704 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
5706 struct swm_region
*r
, *n
;
5707 struct workspace
*ws
= NULL
;
5710 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
5711 s
->idx
, w
, h
, x
, y
);
5713 /* remove any conflicting regions */
5714 n
= TAILQ_FIRST(&s
->rl
);
5717 n
= TAILQ_NEXT(r
, entry
);
5718 if (X(r
) < (x
+ w
) &&
5719 (X(r
) + WIDTH(r
)) > x
&&
5721 (Y(r
) + HEIGHT(r
)) > y
) {
5722 if (r
->ws
->r
!= NULL
)
5723 r
->ws
->old_r
= r
->ws
->r
;
5725 XDestroyWindow(display
, r
->bar_window
);
5726 TAILQ_REMOVE(&s
->rl
, r
, entry
);
5727 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
5731 /* search old regions for one to reuse */
5733 /* size + location match */
5734 TAILQ_FOREACH(r
, &s
->orl
, entry
)
5735 if (X(r
) == x
&& Y(r
) == y
&&
5736 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
5740 TAILQ_FOREACH(r
, &s
->orl
, entry
)
5741 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
5745 TAILQ_REMOVE(&s
->orl
, r
, entry
);
5746 /* try to use old region's workspace */
5747 if (r
->ws
->r
== NULL
)
5750 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
5751 errx(1, "calloc: failed to allocate memory for screen");
5753 /* if we don't have a workspace already, find one */
5755 for (i
= 0; i
< SWM_WS_MAX
; i
++)
5756 if (s
->ws
[i
].r
== NULL
) {
5763 errx(1, "no free workspaces\n");
5774 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
5780 #ifdef SWM_XRR_HAS_CRTC
5782 XRRScreenResources
*sr
;
5785 #endif /* SWM_XRR_HAS_CRTC */
5786 struct swm_region
*r
;
5789 if (i
>= ScreenCount(display
))
5790 errx(1, "invalid screen");
5792 /* remove any old regions */
5793 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
5794 r
->ws
->old_r
= r
->ws
->r
= NULL
;
5795 XDestroyWindow(display
, r
->bar_window
);
5796 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
5797 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
5801 /* map virtual screens onto physical screens */
5802 #ifdef SWM_XRR_HAS_CRTC
5803 if (xrandr_support
) {
5804 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
5806 new_region(&screens
[i
], 0, 0,
5807 DisplayWidth(display
, i
),
5808 DisplayHeight(display
, i
));
5812 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
5813 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
5814 if (ci
->noutput
== 0)
5817 if (ci
!= NULL
&& ci
->mode
== None
)
5818 new_region(&screens
[i
], 0, 0,
5819 DisplayWidth(display
, i
),
5820 DisplayHeight(display
, i
));
5822 new_region(&screens
[i
],
5823 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
5826 XRRFreeCrtcInfo(ci
);
5827 XRRFreeScreenResources(sr
);
5829 #endif /* SWM_XRR_HAS_CRTC */
5831 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
5832 DisplayHeight(display
, i
));
5837 screenchange(XEvent
*e
) {
5838 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
5839 struct swm_region
*r
;
5842 DNPRINTF(SWM_D_EVENT
, "screenchange: %lu\n", xe
->root
);
5844 if (!XRRUpdateConfiguration(e
))
5847 /* silly event doesn't include the screen index */
5848 for (i
= 0; i
< ScreenCount(display
); i
++)
5849 if (screens
[i
].root
== xe
->root
)
5851 if (i
>= ScreenCount(display
))
5852 errx(1, "screenchange: screen not found\n");
5854 /* brute force for now, just re-enumerate the regions */
5857 /* add bars to all regions */
5858 for (i
= 0; i
< ScreenCount(display
); i
++)
5859 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
5862 if (focus_mode
== SWM_FOCUS_DEFAULT
)
5863 drain_enter_notify();
5869 Window d1
, d2
, *wins
= NULL
;
5870 XWindowAttributes wa
;
5875 for (i
= 0; i
< ScreenCount(display
); i
++) {
5876 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
5879 /* attach windows to a region */
5880 /* normal windows */
5881 for (j
= 0; j
< no
; j
++) {
5882 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
5883 wa
.override_redirect
||
5884 XGetTransientForHint(display
, wins
[j
], &d1
))
5887 state
= getstate(wins
[j
]);
5888 manage
= state
== IconicState
;
5889 if (wa
.map_state
== IsViewable
|| manage
)
5890 manage_window(wins
[j
]);
5892 /* transient windows */
5893 for (j
= 0; j
< no
; j
++) {
5894 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
5895 wa
.override_redirect
)
5898 state
= getstate(wins
[j
]);
5899 manage
= state
== IconicState
;
5900 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
5902 manage_window(wins
[j
]);
5915 int errorbase
, major
, minor
;
5916 struct workspace
*ws
;
5918 if ((screens
= calloc(ScreenCount(display
),
5919 sizeof(struct swm_screen
))) == NULL
)
5920 errx(1, "calloc: screens");
5922 /* initial Xrandr setup */
5923 xrandr_support
= XRRQueryExtension(display
,
5924 &xrandr_eventbase
, &errorbase
);
5926 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
5929 /* map physical screens */
5930 for (i
= 0; i
< ScreenCount(display
); i
++) {
5931 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
5933 TAILQ_INIT(&screens
[i
].rl
);
5934 TAILQ_INIT(&screens
[i
].orl
);
5935 screens
[i
].root
= RootWindow(display
, i
);
5937 /* set default colors */
5938 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
5939 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
5940 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
5941 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
5942 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
5944 /* set default cursor */
5945 XDefineCursor(display
, screens
[i
].root
,
5946 XCreateFontCursor(display
, XC_left_ptr
));
5948 /* init all workspaces */
5949 /* XXX these should be dynamically allocated too */
5950 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
5951 ws
= &screens
[i
].ws
[j
];
5956 TAILQ_INIT(&ws
->winlist
);
5957 TAILQ_INIT(&ws
->unmanagedlist
);
5959 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
5960 if (layouts
[k
].l_config
!= NULL
)
5961 layouts
[k
].l_config(ws
,
5962 SWM_ARG_ID_STACKINIT
);
5963 ws
->cur_layout
= &layouts
[0];
5969 XRRSelectInput(display
, screens
[i
].root
,
5970 RRScreenChangeNotifyMask
);
5977 if ((bar_fonts
[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
5979 err(1, "setup_globals: strdup");
5980 if ((bar_fonts
[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
5982 err(1, "setup_globals: strdup");
5983 if ((bar_fonts
[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
5985 err(1, "setup_globals: strdup");
5986 if ((spawn_term
[0] = strdup("xterm")) == NULL
)
5987 err(1, "setup_globals: strdup");
5988 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
5996 Atom netwmcheck
, netwmname
, utf8_string
;
5999 /* work around sun jdk bugs, code from wmname */
6000 netwmcheck
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
6001 netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
6002 utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6003 for (i
= 0; i
< ScreenCount(display
); i
++) {
6004 root
= screens
[i
].root
;
6005 win
= XCreateSimpleWindow(display
,root
, 0, 0, 1, 1, 0,
6006 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
,
6007 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
);
6009 XChangeProperty(display
, root
, netwmcheck
, XA_WINDOW
, 32,
6010 PropModeReplace
, (unsigned char *)&win
,1);
6011 XChangeProperty(display
, win
, netwmcheck
, XA_WINDOW
, 32,
6012 PropModeReplace
, (unsigned char *)&win
,1);
6013 XChangeProperty(display
, win
, netwmname
, utf8_string
, 8,
6014 PropModeReplace
, (unsigned char*)"LG3D", strlen("LG3D"));
6019 main(int argc
, char *argv
[])
6022 struct swm_region
*r
, *rr
;
6023 struct ws_win
*winfocus
= NULL
;
6026 char conf
[PATH_MAX
], *cfile
= NULL
;
6031 struct sigaction sact
;
6034 fprintf(stderr
, "Welcome to scrotwm V%s cvs tag: %s\n",
6035 SWM_VERSION
, cvstag
);
6036 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
6037 warnx("no locale support");
6039 if (!(display
= XOpenDisplay(0)))
6040 errx(1, "can not open display");
6043 errx(1, "other wm running");
6045 /* handle some signals */
6046 bzero(&sact
, sizeof(sact
));
6047 sigemptyset(&sact
.sa_mask
);
6049 sact
.sa_handler
= sighdlr
;
6050 sigaction(SIGINT
, &sact
, NULL
);
6051 sigaction(SIGQUIT
, &sact
, NULL
);
6052 sigaction(SIGTERM
, &sact
, NULL
);
6053 sigaction(SIGHUP
, &sact
, NULL
);
6055 sact
.sa_handler
= sighdlr
;
6056 sact
.sa_flags
= SA_NOCLDSTOP
;
6057 sigaction(SIGCHLD
, &sact
, NULL
);
6059 astate
= XInternAtom(display
, "WM_STATE", False
);
6060 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
6061 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
6062 takefocus
= XInternAtom(display
, "WM_TAKE_FOCUS", False
);
6063 a_wmname
= XInternAtom(display
, "WM_NAME", False
);
6064 a_utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6065 a_string
= XInternAtom(display
, "STRING", False
);
6066 a_swm_iconic
= XInternAtom(display
, "_SWM_ICONIC", False
);
6068 /* look for local and global conf file */
6069 pwd
= getpwuid(getuid());
6071 errx(1, "invalid user %d", getuid());
6080 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
6081 if (stat(conf
, &sb
) != -1) {
6082 if (S_ISREG(sb
.st_mode
))
6085 /* try global conf file */
6086 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
6087 if (!stat(conf
, &sb
))
6088 if (S_ISREG(sb
.st_mode
))
6095 /* set some values to work around bad programs */
6098 /* grab existing windows (before we build the bars) */
6101 if (getenv("SWM_STARTED") == NULL
)
6102 setenv("SWM_STARTED", "YES", 1);
6104 /* setup all bars */
6105 for (i
= 0; i
< ScreenCount(display
); i
++)
6106 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
6107 if (winfocus
== NULL
)
6108 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
6116 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6117 drain_enter_notify();
6119 xfd
= ConnectionNumber(display
);
6121 while (XPending(display
)) {
6122 XNextEvent(display
, &e
);
6125 if (e
.type
< LASTEvent
) {
6127 if (handler
[e
.type
])
6128 handler
[e
.type
](&e
);
6130 DNPRINTF(SWM_D_EVENT
,
6131 "win: %lu unknown event: %d\n",
6132 e
.xany
.window
, e
.type
);
6134 switch (e
.type
- xrandr_eventbase
) {
6135 case RRScreenChangeNotify
:
6139 DNPRINTF(SWM_D_EVENT
,
6140 "win: %lu unknown xrandr event: "
6141 "%d\n", e
.xany
.window
, e
.type
);
6147 /* if we are being restarted go focus on first window */
6149 rr
= winfocus
->ws
->r
;
6151 /* not a visible window */
6155 /* move pointer to first screen if multi screen */
6156 if (ScreenCount(display
) > 1 || outputs
> 1)
6157 XWarpPointer(display
, None
, rr
->s
[0].root
,
6158 0, 0, 0, 0, rr
->g
.x
,
6159 rr
->g
.y
+ (bar_enabled
? bar_height
: 0));
6161 a
.id
= SWM_ARG_ID_FOCUSCUR
;
6171 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
6173 DNPRINTF(SWM_D_MISC
, "select failed");
6174 if (restart_wm
== 1)
6175 restart(NULL
, NULL
);
6176 if (search_resp
== 1)
6188 XFreeGC(display
, bar_gc
);
6189 XCloseDisplay(display
);