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
= "$scrotwm$";
56 #define SWM_VERSION "0.9.30"
73 #include <sys/types.h>
77 #include <sys/queue.h>
78 #include <sys/param.h>
79 #include <sys/select.h>
81 #include <X11/cursorfont.h>
82 #include <X11/keysym.h>
83 #include <X11/Xatom.h>
85 #include <X11/Xproto.h>
86 #include <X11/Xutil.h>
87 #include <X11/extensions/Xrandr.h>
94 # error XRandR versions less than 1.0 are not supported
99 #define SWM_XRR_HAS_CRTC
103 /*#define SWM_DEBUG*/
105 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
106 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
107 #define SWM_D_MISC 0x0001
108 #define SWM_D_EVENT 0x0002
109 #define SWM_D_WS 0x0004
110 #define SWM_D_FOCUS 0x0008
111 #define SWM_D_MOVE 0x0010
112 #define SWM_D_STACK 0x0020
113 #define SWM_D_MOUSE 0x0040
114 #define SWM_D_PROP 0x0080
115 #define SWM_D_CLASS 0x0100
116 #define SWM_D_KEY 0x0200
117 #define SWM_D_QUIRK 0x0400
118 #define SWM_D_SPAWN 0x0800
119 #define SWM_D_EVENTQ 0x1000
120 #define SWM_D_CONF 0x2000
122 u_int32_t swm_debug
= 0
139 #define DPRINTF(x...)
140 #define DNPRINTF(n,x...)
143 #define LENGTH(x) (sizeof x / sizeof x[0])
144 #define MODKEY Mod1Mask
145 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
146 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
147 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
148 #define SWM_PROPLEN (16)
149 #define SWM_FUNCNAME_LEN (32)
150 #define SWM_KEYS_LEN (255)
151 #define SWM_QUIRK_LEN (64)
152 #define X(r) (r)->g.x
153 #define Y(r) (r)->g.y
154 #define WIDTH(r) (r)->g.w
155 #define HEIGHT(r) (r)->g.h
156 #define SWM_MAX_FONT_STEPS (3)
157 #define WINID(w) (w ? w->id : 0)
159 #define SWM_FOCUS_DEFAULT (0)
160 #define SWM_FOCUS_SYNERGY (1)
161 #define SWM_FOCUS_FOLLOW (2)
164 #define SWM_LIB "/usr/local/lib/libswmhack.so"
176 volatile sig_atomic_t running
= 1;
177 volatile sig_atomic_t restart_wm
= 0;
179 int last_focus_event
= FocusOut
;
180 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
184 int xrandr_eventbase
;
185 unsigned int numlockmask
= 0;
189 int cycle_visible
= 0;
191 int font_adjusted
= 0;
192 unsigned int mod_key
= MODKEY
;
195 struct swm_region
*search_r
;
196 int select_list_pipe
[2];
197 int select_resp_pipe
[2];
199 volatile sig_atomic_t search_resp
;
202 double dialog_ratio
= .6;
204 #define SWM_BAR_MAX (256)
205 char *bar_argv
[] = { NULL
, NULL
};
207 char bar_ext
[SWM_BAR_MAX
];
208 char bar_vertext
[SWM_BAR_MAX
];
210 sig_atomic_t bar_alarm
= 0;
213 int bar_border_width
= 1;
214 int bar_at_bottom
= 0;
216 int bar_extra_running
= 0;
219 int stack_enabled
= 1;
220 int clock_enabled
= 1;
221 char *clock_format
= NULL
;
222 int title_name_enabled
= 0;
223 int title_class_enabled
= 0;
224 int window_name_enabled
= 0;
225 int focus_mode
= SWM_FOCUS_DEFAULT
;
226 int disable_border
= 0;
227 int border_width
= 1;
233 char *bar_fonts
[] = { NULL
, NULL
, NULL
, NULL
};/* XXX Make fully dynamic */
234 char *spawn_term
[] = { NULL
, NULL
}; /* XXX Make fully dynamic */
236 #define SWM_MENU_FN (2)
237 #define SWM_MENU_NB (4)
238 #define SWM_MENU_NF (6)
239 #define SWM_MENU_SB (8)
240 #define SWM_MENU_SF (10)
242 /* layout manager data */
243 struct swm_geometry
{
253 /* virtual "screens" */
255 TAILQ_ENTRY(swm_region
) entry
;
256 struct swm_geometry g
;
257 struct workspace
*ws
; /* current workspace on this region */
258 struct workspace
*ws_prior
; /* prior workspace on this region */
259 struct swm_screen
*s
; /* screen idx */
262 TAILQ_HEAD(swm_region_list
, swm_region
);
265 TAILQ_ENTRY(ws_win
) entry
;
268 struct ws_win
*child_trans
; /* transient child window */
269 struct swm_geometry g
; /* current geometry */
270 struct swm_geometry g_float
; /* geometry when floating */
271 struct swm_geometry rg_float
; /* region geom when floating */
272 int g_floatvalid
; /* flag: geometry in g_float is valid */
273 int floatmaxed
; /* flag: floater was maxed in max_stack */
277 unsigned int ewmh_flags
;
278 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
284 unsigned long quirks
;
285 struct workspace
*ws
; /* always valid */
286 struct swm_screen
*s
; /* always valid, never changes */
287 XWindowAttributes wa
;
292 TAILQ_HEAD(ws_win_list
, ws_win
);
294 /* layout handlers */
296 void vertical_config(struct workspace
*, int);
297 void vertical_stack(struct workspace
*, struct swm_geometry
*);
298 void horizontal_config(struct workspace
*, int);
299 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
300 void max_stack(struct workspace
*, struct swm_geometry
*);
302 struct ws_win
*find_window(Window
);
304 void grabbuttons(struct ws_win
*, int);
305 void new_region(struct swm_screen
*, int, int, int, int);
306 void unmanage_window(struct ws_win
*);
307 long getstate(Window
);
310 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
311 void (*l_config
)(struct workspace
*, int);
313 #define SWM_L_FOCUSPREV (1<<0)
314 #define SWM_L_MAPONFOCUS (1<<1)
317 /* stack, configure */
318 { vertical_stack
, vertical_config
, 0, "[|]" },
319 { horizontal_stack
, horizontal_config
, 0, "[-]" },
321 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, "[ ]"},
322 { NULL
, NULL
, 0, NULL
},
325 /* position of max_stack mode in the layouts array */
326 #define SWM_MAX_STACK 2
328 #define SWM_H_SLICE (32)
329 #define SWM_V_SLICE (32)
331 /* define work spaces */
333 int idx
; /* workspace index */
334 struct layout
*cur_layout
; /* current layout handlers */
335 struct ws_win
*focus
; /* may be NULL */
336 struct ws_win
*focus_prev
; /* may be NULL */
337 struct swm_region
*r
; /* may be NULL */
338 struct swm_region
*old_r
; /* may be NULL */
339 struct ws_win_list winlist
; /* list of windows in ws */
340 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
344 int horizontal_msize
;
346 int horizontal_stacks
;
353 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
354 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
356 /* physical screen mapping */
357 #define SWM_WS_MAX (10)
359 int idx
; /* screen index */
360 struct swm_region_list rl
; /* list of regions on this screen */
361 struct swm_region_list orl
; /* list of old regions */
363 struct workspace ws
[SWM_WS_MAX
];
369 } c
[SWM_S_COLOR_MAX
];
371 struct swm_screen
*screens
;
374 /* args to functions */
377 #define SWM_ARG_ID_FOCUSNEXT (0)
378 #define SWM_ARG_ID_FOCUSPREV (1)
379 #define SWM_ARG_ID_FOCUSMAIN (2)
380 #define SWM_ARG_ID_FOCUSCUR (4)
381 #define SWM_ARG_ID_SWAPNEXT (10)
382 #define SWM_ARG_ID_SWAPPREV (11)
383 #define SWM_ARG_ID_SWAPMAIN (12)
384 #define SWM_ARG_ID_MOVELAST (13)
385 #define SWM_ARG_ID_MASTERSHRINK (20)
386 #define SWM_ARG_ID_MASTERGROW (21)
387 #define SWM_ARG_ID_MASTERADD (22)
388 #define SWM_ARG_ID_MASTERDEL (23)
389 #define SWM_ARG_ID_STACKRESET (30)
390 #define SWM_ARG_ID_STACKINIT (31)
391 #define SWM_ARG_ID_CYCLEWS_UP (40)
392 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
393 #define SWM_ARG_ID_CYCLESC_UP (42)
394 #define SWM_ARG_ID_CYCLESC_DOWN (43)
395 #define SWM_ARG_ID_STACKINC (50)
396 #define SWM_ARG_ID_STACKDEC (51)
397 #define SWM_ARG_ID_SS_ALL (60)
398 #define SWM_ARG_ID_SS_WINDOW (61)
399 #define SWM_ARG_ID_DONTCENTER (70)
400 #define SWM_ARG_ID_CENTER (71)
401 #define SWM_ARG_ID_KILLWINDOW (80)
402 #define SWM_ARG_ID_DELETEWINDOW (81)
406 void focus(struct swm_region
*, union arg
*);
407 void focus_magic(struct ws_win
*);
414 #define SWM_Q_FLOAT (1<<0) /* float this window */
415 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
416 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
417 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
418 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
420 int quirks_size
= 0, quirks_length
= 0;
421 struct quirk
*quirks
= NULL
;
424 * Supported EWMH hints should be added to
425 * both the enum and the ewmh array
427 enum { _NET_ACTIVE_WINDOW
, _NET_MOVERESIZE_WINDOW
, _NET_CLOSE_WINDOW
,
428 _NET_WM_WINDOW_TYPE
, _NET_WM_WINDOW_TYPE_DOCK
,
429 _NET_WM_WINDOW_TYPE_TOOLBAR
, _NET_WM_WINDOW_TYPE_UTILITY
,
430 _NET_WM_WINDOW_TYPE_SPLASH
, _NET_WM_WINDOW_TYPE_DIALOG
,
431 _NET_WM_WINDOW_TYPE_NORMAL
, _NET_WM_STATE
,
432 _NET_WM_STATE_MAXIMIZED_HORZ
, _NET_WM_STATE_MAXIMIZED_VERT
,
433 _NET_WM_STATE_SKIP_TASKBAR
, _NET_WM_STATE_SKIP_PAGER
,
434 _NET_WM_STATE_HIDDEN
, _NET_WM_STATE_ABOVE
, _SWM_WM_STATE_MANUAL
,
435 _NET_WM_STATE_FULLSCREEN
, _NET_WM_ALLOWED_ACTIONS
, _NET_WM_ACTION_MOVE
,
436 _NET_WM_ACTION_RESIZE
, _NET_WM_ACTION_FULLSCREEN
, _NET_WM_ACTION_CLOSE
,
442 } ewmh
[SWM_EWMH_HINT_MAX
] = {
443 /* must be in same order as in the enum */
444 {"_NET_ACTIVE_WINDOW", None
},
445 {"_NET_MOVERESIZE_WINDOW", None
},
446 {"_NET_CLOSE_WINDOW", None
},
447 {"_NET_WM_WINDOW_TYPE", None
},
448 {"_NET_WM_WINDOW_TYPE_DOCK", None
},
449 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None
},
450 {"_NET_WM_WINDOW_TYPE_UTILITY", None
},
451 {"_NET_WM_WINDOW_TYPE_SPLASH", None
},
452 {"_NET_WM_WINDOW_TYPE_DIALOG", None
},
453 {"_NET_WM_WINDOW_TYPE_NORMAL", None
},
454 {"_NET_WM_STATE", None
},
455 {"_NET_WM_STATE_MAXIMIZED_HORZ", None
},
456 {"_NET_WM_STATE_MAXIMIZED_VERT", None
},
457 {"_NET_WM_STATE_SKIP_TASKBAR", None
},
458 {"_NET_WM_STATE_SKIP_PAGER", None
},
459 {"_NET_WM_STATE_HIDDEN", None
},
460 {"_NET_WM_STATE_ABOVE", None
},
461 {"_SWM_WM_STATE_MANUAL", None
},
462 {"_NET_WM_STATE_FULLSCREEN", None
},
463 {"_NET_WM_ALLOWED_ACTIONS", None
},
464 {"_NET_WM_ACTION_MOVE", None
},
465 {"_NET_WM_ACTION_RESIZE", None
},
466 {"_NET_WM_ACTION_FULLSCREEN", None
},
467 {"_NET_WM_ACTION_CLOSE", None
},
470 void store_float_geom(struct ws_win
*win
, struct swm_region
*r
);
471 int floating_toggle_win(struct ws_win
*win
);
472 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
475 get_property(Window id
, Atom atom
, long count
, Atom type
,
476 unsigned long *n
, unsigned char **data
)
479 unsigned long tmp
, extra
;
480 unsigned long *nitems
;
483 nitems
= n
!= NULL
? n
: &tmp
;
484 status
= XGetWindowProperty(display
, id
, atom
, 0L, count
, False
, type
,
485 &real
, &format
, nitems
, &extra
, data
);
487 if (status
!= Success
)
496 update_iconic(struct ws_win
*win
, int newv
)
503 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
507 XChangeProperty(display
, win
->id
, iprop
, XA_INTEGER
, 32,
508 PropModeReplace
, (unsigned char *)&v
, 1);
510 XDeleteProperty(display
, win
->id
, iprop
);
514 get_iconic(struct ws_win
*win
)
519 unsigned long nitems
, extra
;
520 unsigned char *prop
= NULL
;
522 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
525 status
= XGetWindowProperty(display
, win
->id
, iprop
, 0L, 1L,
526 False
, XA_INTEGER
, &rettype
, &retfmt
, &nitems
, &extra
, &prop
);
527 if (status
!= Success
)
529 if (rettype
!= XA_INTEGER
|| retfmt
!= 32)
533 v
= *((int32_t *)prop
);
547 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
549 for (i
= 0; i
< LENGTH(ewmh
); i
++)
550 ewmh
[i
].atom
= XInternAtom(display
, ewmh
[i
].name
, False
);
552 for (i
= 0; i
< ScreenCount(display
); i
++) {
553 /* Support check window will be created by workaround(). */
555 /* Report supported atoms */
556 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
557 for (j
= 0; j
< LENGTH(ewmh
); j
++)
558 XChangeProperty(display
, screens
[i
].root
, sup_list
, XA_ATOM
, 32,
559 PropModeAppend
, (unsigned char *)&ewmh
[j
].atom
,1);
567 unsigned char *data
= NULL
;
569 Atom sup_check
, sup_list
;
572 sup_check
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
573 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
575 for (i
= 0; i
< ScreenCount(display
); i
++) {
576 /* Get the support check window and destroy it */
577 success
= get_property(screens
[i
].root
, sup_check
, 1, XA_WINDOW
,
582 XDestroyWindow(display
, id
);
583 XDeleteProperty(display
, screens
[i
].root
, sup_check
);
584 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
592 ewmh_autoquirk(struct ws_win
*win
)
595 unsigned long *data
= NULL
;
599 success
= get_property(win
->id
, ewmh
[_NET_WM_WINDOW_TYPE
].atom
, (~0L),
600 XA_ATOM
, &n
, (unsigned char **)&data
);
607 for (i
= 0; i
< n
; i
++) {
609 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
611 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
612 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
613 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
615 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
618 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
619 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
621 win
->quirks
= SWM_Q_FLOAT
;
629 #define SWM_EWMH_ACTION_COUNT_MAX (6)
630 #define EWMH_F_FULLSCREEN (1<<0)
631 #define EWMH_F_ABOVE (1<<1)
632 #define EWMH_F_HIDDEN (1<<2)
633 #define EWMH_F_SKIP_PAGER (1<<3)
634 #define EWMH_F_SKIP_TASKBAR (1<<4)
635 #define SWM_F_MANUAL (1<<5)
638 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
640 struct swm_geometry rg
;
648 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
654 store_float_geom(win
, win
->ws
->r
);
661 if (win
->g_floatvalid
) {
662 /* refloat at last floating relative position */
663 win
->g
.x
= win
->g_float
.x
- win
->rg_float
.x
+ rg
.x
;
664 win
->g
.y
= win
->g_float
.y
- win
->rg_float
.y
+ rg
.y
;
665 win
->g
.w
= win
->g_float
.w
;
666 win
->g
.h
= win
->g_float
.h
;
674 ewmh_update_actions(struct ws_win
*win
)
676 Atom actions
[SWM_EWMH_ACTION_COUNT_MAX
];
682 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
685 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
686 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
689 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
,
690 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)actions
, n
);
693 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
694 #define _NET_WM_STATE_ADD 1 /* add/set property */
695 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
698 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
700 unsigned int mask
= 0;
701 unsigned int changed
= 0;
702 unsigned int orig_flags
;
707 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
708 mask
= EWMH_F_FULLSCREEN
;
709 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
711 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
713 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
714 mask
= EWMH_F_SKIP_PAGER
;
715 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
716 mask
= EWMH_F_SKIP_TASKBAR
;
719 orig_flags
= win
->ewmh_flags
;
722 case _NET_WM_STATE_REMOVE
:
723 win
->ewmh_flags
&= ~mask
;
725 case _NET_WM_STATE_ADD
:
726 win
->ewmh_flags
|= mask
;
728 case _NET_WM_STATE_TOGGLE
:
729 win
->ewmh_flags
^= mask
;
733 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
735 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
737 if (!floating_toggle_win(win
))
738 win
->ewmh_flags
= orig_flags
; /* revert */
739 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
741 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
742 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
744 if (!ewmh_set_win_fullscreen(win
, win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
745 win
->ewmh_flags
= orig_flags
; /* revert */
747 XDeleteProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
749 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
750 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
751 XA_ATOM
, 32, PropModeAppend
,
752 (unsigned char *)&ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
, 1);
753 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
754 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
755 XA_ATOM
, 32, PropModeAppend
,
756 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
, 1);
757 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
758 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
759 XA_ATOM
, 32, PropModeAppend
,
760 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
, 1);
761 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
762 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
763 XA_ATOM
, 32, PropModeAppend
,
764 (unsigned char *)&ewmh
[_NET_WM_STATE_ABOVE
].atom
, 1);
765 if (win
->ewmh_flags
& SWM_F_MANUAL
)
766 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
767 XA_ATOM
, 32, PropModeAppend
,
768 (unsigned char *)&ewmh
[_SWM_WM_STATE_MANUAL
].atom
, 1);
772 ewmh_get_win_state(struct ws_win
*win
)
783 win
->ewmh_flags
|= EWMH_F_ABOVE
;
785 win
->ewmh_flags
|= SWM_F_MANUAL
;
787 success
= get_property(win
->id
, ewmh
[_NET_WM_STATE
].atom
, (~0L), XA_ATOM
,
788 &n
, (unsigned char **)&states
);
793 for (i
= 0; i
< n
; i
++)
794 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
814 name
= "ButtonPress";
817 name
= "ButtonRelease";
820 name
= "MotionNotify";
823 name
= "EnterNotify";
826 name
= "LeaveNotify";
835 name
= "KeymapNotify";
841 name
= "GraphicsExpose";
846 case VisibilityNotify
:
847 name
= "VisibilityNotify";
850 name
= "CreateNotify";
853 name
= "DestroyNotify";
856 name
= "UnmapNotify";
865 name
= "ReparentNotify";
867 case ConfigureNotify
:
868 name
= "ConfigureNotify";
870 case ConfigureRequest
:
871 name
= "ConfigureRequest";
874 name
= "GravityNotify";
877 name
= "ResizeRequest";
879 case CirculateNotify
:
880 name
= "CirculateNotify";
882 case CirculateRequest
:
883 name
= "CirculateRequest";
886 name
= "PropertyNotify";
889 name
= "SelectionClear";
891 case SelectionRequest
:
892 name
= "SelectionRequest";
894 case SelectionNotify
:
895 name
= "SelectionNotify";
898 name
= "ColormapNotify";
901 name
= "ClientMessage";
904 name
= "MappingNotify";
909 DNPRINTF(SWM_D_EVENTQ
,"window: %lu event: %s (%d), %d "
911 e
->xany
.window
, name
, e
->type
, QLength(display
));
913 DNPRINTF(SWM_D_EVENTQ
, "window: %lu unknown event %d, %d "
915 e
->xany
.window
, e
->type
, QLength(display
));
919 dumpwins(struct swm_region
*r
, union arg
*args
)
923 XWindowAttributes wa
;
926 fprintf(stderr
, "invalid workspace\n");
930 fprintf(stderr
, "=== managed window list ws %02d ===\n", r
->ws
->idx
);
932 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
933 state
= getstate(win
->id
);
934 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
935 fprintf(stderr
, "window: %lu failed "
936 "XGetWindowAttributes\n", win
->id
);
937 fprintf(stderr
, "window: %lu map_state: %d state: %d "
939 win
->id
, wa
.map_state
, state
, win
->transient
);
942 fprintf(stderr
, "===== unmanaged window list =====\n");
943 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
944 state
= getstate(win
->id
);
945 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
946 fprintf(stderr
, "window: %lu failed "
947 "XGetWindowAttributes\n", win
->id
);
948 fprintf(stderr
, "window: %lu map_state: %d state: %d "
950 win
->id
, wa
.map_state
, state
, win
->transient
);
953 fprintf(stderr
, "=================================\n");
958 dumpwins(struct swm_region
*r
, union arg
*args
)
961 #endif /* SWM_DEBUG */
963 void expose(XEvent
*);
964 void keypress(XEvent
*);
965 void buttonpress(XEvent
*);
966 void configurerequest(XEvent
*);
967 void configurenotify(XEvent
*);
968 void destroynotify(XEvent
*);
969 void enternotify(XEvent
*);
970 void focusevent(XEvent
*);
971 void mapnotify(XEvent
*);
972 void mappingnotify(XEvent
*);
973 void maprequest(XEvent
*);
974 void propertynotify(XEvent
*);
975 void unmapnotify(XEvent
*);
976 void visibilitynotify(XEvent
*);
977 void clientmessage(XEvent
*);
979 void (*handler
[LASTEvent
])(XEvent
*) = {
981 [KeyPress
] = keypress
,
982 [ButtonPress
] = buttonpress
,
983 [ConfigureRequest
] = configurerequest
,
984 [ConfigureNotify
] = configurenotify
,
985 [DestroyNotify
] = destroynotify
,
986 [EnterNotify
] = enternotify
,
987 [FocusIn
] = focusevent
,
988 [FocusOut
] = focusevent
,
989 [MapNotify
] = mapnotify
,
990 [MappingNotify
] = mappingnotify
,
991 [MapRequest
] = maprequest
,
992 [PropertyNotify
] = propertynotify
,
993 [UnmapNotify
] = unmapnotify
,
994 [VisibilityNotify
] = visibilitynotify
,
995 [ClientMessage
] = clientmessage
,
1001 int saved_errno
, status
;
1004 saved_errno
= errno
;
1008 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1013 if (errno
!= ECHILD
)
1014 warn("sighdlr: waitpid");
1015 #endif /* SWM_DEBUG */
1018 if (pid
== searchpid
)
1022 if (WIFEXITED(status
)) {
1023 if (WEXITSTATUS(status
) != 0)
1024 warnx("sighdlr: child exit status: %d",
1025 WEXITSTATUS(status
));
1027 warnx("sighdlr: child is terminated "
1029 #endif /* SWM_DEBUG */
1043 errno
= saved_errno
;
1047 name_to_color(char *colorname
)
1051 XColor screen_def
, exact_def
;
1052 unsigned long result
= 0;
1053 char cname
[32] = "#";
1055 cmap
= DefaultColormap(display
, screens
[0].idx
);
1056 status
= XAllocNamedColor(display
, cmap
, colorname
,
1057 &screen_def
, &exact_def
);
1059 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1060 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
1064 result
= screen_def
.pixel
;
1066 fprintf(stderr
, "color '%s' not found.\n", colorname
);
1072 setscreencolor(char *val
, int i
, int c
)
1074 if (i
> 0 && i
<= ScreenCount(display
)) {
1075 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1076 free(screens
[i
- 1].c
[c
].name
);
1077 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1079 } else if (i
== -1) {
1080 for (i
= 0; i
< ScreenCount(display
); i
++) {
1081 screens
[i
].c
[c
].color
= name_to_color(val
);
1082 free(screens
[i
].c
[c
].name
);
1083 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1087 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1088 i
, ScreenCount(display
));
1092 custom_region(char *val
)
1094 unsigned int sidx
, x
, y
, w
, h
;
1096 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1097 errx(1, "invalid custom region, "
1098 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1099 if (sidx
< 1 || sidx
> ScreenCount(display
))
1100 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1101 sidx
, ScreenCount(display
));
1105 errx(1, "region %ux%u+%u+%u too small\n", w
, h
, x
, y
);
1107 if (x
< 0 || x
> DisplayWidth(display
, sidx
) ||
1108 y
< 0 || y
> DisplayHeight(display
, sidx
) ||
1109 w
+ x
> DisplayWidth(display
, sidx
) ||
1110 h
+ y
> DisplayHeight(display
, sidx
)) {
1111 fprintf(stderr
, "ignoring region %ux%u+%u+%u - not within screen boundaries "
1112 "(%ux%u)\n", w
, h
, x
, y
,
1113 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
1117 new_region(&screens
[sidx
], x
, y
, w
, h
);
1121 socket_setnonblock(int fd
)
1125 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1126 err(1, "fcntl F_GETFL");
1127 flags
|= O_NONBLOCK
;
1128 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1129 err(1, "fcntl F_SETFL");
1133 bar_print(struct swm_region
*r
, char *s
)
1135 XClearWindow(display
, r
->bar_window
);
1136 XSetForeground(display
, bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
1137 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
, s
,
1142 bar_extra_stop(void)
1146 bzero(bar_pipe
, sizeof bar_pipe
);
1149 kill(bar_pid
, SIGTERM
);
1152 strlcpy(bar_ext
, "", sizeof bar_ext
);
1157 bar_class_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1159 int do_class
, do_name
;
1161 XClassHint
*xch
= NULL
;
1163 if ((title_name_enabled
== 1 || title_class_enabled
== 1) &&
1164 cur_focus
!= NULL
) {
1165 if ((xch
= XAllocClassHint()) == NULL
)
1167 status
= XGetClassHint(display
, cur_focus
->id
, xch
);
1168 if (status
== BadWindow
|| status
== BadAlloc
)
1170 do_class
= (title_class_enabled
&& xch
->res_class
!= NULL
);
1171 do_name
= (title_name_enabled
&& xch
->res_name
!= NULL
);
1173 strlcat(s
, xch
->res_class
, sz
);
1174 if (do_class
&& do_name
)
1175 strlcat(s
, ":", sz
);
1177 strlcat(s
, xch
->res_name
, sz
);
1178 strlcat(s
, " ", sz
);
1186 bar_window_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1190 if (window_name_enabled
&& cur_focus
!= NULL
) {
1191 XFetchName(display
, cur_focus
->id
, &title
);
1193 if (cur_focus
->floating
)
1194 strlcat(s
, "(f) ", sz
);
1195 strlcat(s
, title
, sz
);
1196 strlcat(s
, " ", sz
);
1207 struct swm_region
*r
;
1210 char s
[SWM_BAR_MAX
];
1211 char cn
[SWM_BAR_MAX
];
1212 char loc
[SWM_BAR_MAX
];
1216 if (bar_enabled
== 0)
1218 if (bar_extra
&& bar_extra_running
) {
1219 /* ignore short reads; it'll correct itself */
1220 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1221 if (b
&& b
[len
- 1] == '\n') {
1223 strlcpy(bar_ext
, b
, sizeof bar_ext
);
1225 if (b
== NULL
&& errno
!= EAGAIN
) {
1226 fprintf(stderr
, "bar_extra failed: errno: %d %s\n",
1227 errno
, strerror(errno
));
1231 strlcpy(bar_ext
, "", sizeof bar_ext
);
1233 if (clock_enabled
== 0)
1234 strlcpy(s
, "", sizeof s
);
1237 localtime_r(&tmt
, &tm
);
1238 strftime(s
, sizeof s
, clock_format
, &tm
);
1239 strlcat(s
, " ", sizeof s
);
1242 for (i
= 0; i
< ScreenCount(display
); i
++) {
1244 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1245 strlcpy(cn
, "", sizeof cn
);
1247 bar_class_name(cn
, sizeof cn
, r
->ws
->focus
);
1248 bar_window_name(cn
, sizeof cn
, r
->ws
->focus
);
1252 stack
= r
->ws
->cur_layout
->name
;
1254 snprintf(loc
, sizeof loc
, "%d:%d %s %s%s %s %s",
1255 x
++, r
->ws
->idx
+ 1, stack
, s
, cn
, bar_ext
,
1270 bar_toggle(struct swm_region
*r
, union arg
*args
)
1272 struct swm_region
*tmpr
;
1273 int i
, sc
= ScreenCount(display
);
1275 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
1278 for (i
= 0; i
< sc
; i
++)
1279 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1280 XUnmapWindow(display
, tmpr
->bar_window
);
1282 for (i
= 0; i
< sc
; i
++)
1283 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1284 XMapRaised(display
, tmpr
->bar_window
);
1286 bar_enabled
= !bar_enabled
;
1289 /* must be after stack */
1296 XSetWindowAttributes wa
;
1297 struct swm_region
*r
;
1300 /* do this here because the conf file is in memory */
1301 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1302 /* launch external status app */
1303 bar_extra_running
= 1;
1304 if (pipe(bar_pipe
) == -1)
1305 err(1, "pipe error");
1306 socket_setnonblock(bar_pipe
[0]);
1307 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1308 if (dup2(bar_pipe
[0], 0) == -1)
1310 if (dup2(bar_pipe
[1], 1) == -1)
1312 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1313 err(1, "could not disable SIGPIPE");
1314 switch (bar_pid
= fork()) {
1316 err(1, "cannot fork");
1320 execvp(bar_argv
[0], bar_argv
);
1321 err(1, "%s external app failed", bar_argv
[0]);
1323 default: /* parent */
1329 bzero(&wa
, sizeof wa
);
1330 for (i
= 0; i
< ScreenCount(display
); i
++)
1331 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1333 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1334 wa
.background_pixel
=
1335 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1336 XChangeWindowAttributes(display
, r
->bar_window
,
1337 CWBackPixel
| CWBorderPixel
, &wa
);
1343 bar_setup(struct swm_region
*r
)
1348 XFreeFont(display
, bar_fs
);
1352 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
1353 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
1359 if (bar_fonts
[i
] == NULL
)
1360 errx(1, "couldn't load font");
1362 errx(1, "couldn't create font structure");
1364 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 1 + 2 * bar_border_width
;
1366 y
= bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1368 r
->bar_window
= XCreateSimpleWindow(display
,
1369 r
->s
->root
, x
, y
, WIDTH(r
) - 2 * bar_border_width
, bar_height
- 2 * bar_border_width
,
1370 bar_border_width
, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
1371 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1372 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
1373 XSetFont(display
, bar_gc
, bar_fs
->fid
);
1374 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
1376 XMapRaised(display
, r
->bar_window
);
1377 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
1379 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1380 err(1, "could not install bar_signal");
1385 drain_enter_notify(void)
1390 while (XCheckMaskEvent(display
, EnterWindowMask
, &cne
))
1393 DNPRINTF(SWM_D_MISC
, "drain_enter_notify: drained %d\n", i
);
1397 set_win_state(struct ws_win
*win
, long state
)
1399 long data
[] = {state
, None
};
1401 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
1406 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1407 (unsigned char *)data
, 2);
1414 unsigned char *p
= NULL
;
1417 if (!get_property(w
, astate
, 2L, astate
, &n
, &p
))
1420 result
= *((long *)p
);
1426 version(struct swm_region
*r
, union arg
*args
)
1428 bar_version
= !bar_version
;
1430 snprintf(bar_vertext
, sizeof bar_vertext
, "Version: %s CVS: %s",
1431 SWM_VERSION
, cvstag
);
1433 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
1438 client_msg(struct ws_win
*win
, Atom a
)
1440 XClientMessageEvent cm
;
1445 bzero(&cm
, sizeof cm
);
1446 cm
.type
= ClientMessage
;
1447 cm
.window
= win
->id
;
1448 cm
.message_type
= aprot
;
1451 cm
.data
.l
[1] = CurrentTime
;
1452 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
1456 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
1464 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
1465 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
1467 ce
.type
= ConfigureNotify
;
1468 ce
.display
= display
;
1470 ce
.window
= win
->id
;
1473 ce
.width
= win
->g
.w
;
1474 ce
.height
= win
->g
.h
;
1475 ce
.border_width
= border_width
;
1477 ce
.override_redirect
= False
;
1479 DNPRINTF(SWM_D_MISC
, "config_win: ev win %lu x %d y %d w %d h %d\n",
1480 ev
->window
, ev
->x
, ev
->y
, ev
->width
, ev
->height
);
1481 ce
.type
= ConfigureNotify
;
1482 ce
.display
= ev
->display
;
1483 ce
.event
= ev
->window
;
1484 ce
.window
= ev
->window
;
1487 ce
.width
= ev
->width
;
1488 ce
.height
= ev
->height
;
1489 ce
.border_width
= ev
->border_width
;
1490 ce
.above
= ev
->above
;
1491 ce
.override_redirect
= False
;
1494 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
1498 count_win(struct workspace
*ws
, int count_transient
)
1503 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1504 if (count_transient
== 0 && win
->floating
)
1506 if (count_transient
== 0 && win
->transient
)
1512 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
1518 quit(struct swm_region
*r
, union arg
*args
)
1520 DNPRINTF(SWM_D_MISC
, "quit\n");
1525 unmap_window(struct ws_win
*win
)
1530 /* don't unmap again */
1531 if (getstate(win
->id
) == IconicState
)
1534 set_win_state(win
, IconicState
);
1536 XUnmapWindow(display
, win
->id
);
1537 XSetWindowBorder(display
, win
->id
,
1538 win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1547 for (i
= 0; i
< ScreenCount(display
); i
++)
1548 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1549 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1554 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
1561 event
.display
= display
; /* Ignored, but what the hell */
1562 event
.window
= win
->id
;
1563 event
.root
= win
->s
->root
;
1564 event
.subwindow
= None
;
1565 event
.time
= CurrentTime
;
1570 event
.same_screen
= True
;
1571 event
.keycode
= XKeysymToKeycode(display
, keysym
);
1572 event
.state
= modifiers
;
1574 event
.type
= KeyPress
;
1575 XSendEvent(event
.display
, event
.window
, True
,
1576 KeyPressMask
, (XEvent
*)&event
);
1578 event
.type
= KeyRelease
;
1579 XSendEvent(event
.display
, event
.window
, True
,
1580 KeyPressMask
, (XEvent
*)&event
);
1585 restart(struct swm_region
*r
, union arg
*args
)
1587 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
1589 /* disable alarm because the following code may not be interrupted */
1591 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
1592 errx(1, "can't disable alarm");
1597 XCloseDisplay(display
);
1598 execvp(start_argv
[0], start_argv
);
1599 fprintf(stderr
, "execvp failed\n");
1605 root_to_region(Window root
)
1607 struct swm_region
*r
= NULL
;
1609 int i
, x
, y
, wx
, wy
;
1612 for (i
= 0; i
< ScreenCount(display
); i
++)
1613 if (screens
[i
].root
== root
)
1616 if (XQueryPointer(display
, screens
[i
].root
,
1617 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
1618 /* choose a region based on pointer location */
1619 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1620 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
1621 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
1626 r
= TAILQ_FIRST(&screens
[i
].rl
);
1632 find_unmanaged_window(Window id
)
1637 for (i
= 0; i
< ScreenCount(display
); i
++)
1638 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1639 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
1647 find_window(Window id
)
1650 Window wrr
, wpr
, *wcr
= NULL
;
1654 for (i
= 0; i
< ScreenCount(display
); i
++)
1655 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1656 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1660 /* if we were looking for the parent return that window instead */
1661 if (XQueryTree(display
, id
, &wrr
, &wpr
, &wcr
, &nc
) == 0)
1666 /* ignore not found and root */
1667 if (wpr
== 0 || wrr
== wpr
)
1670 /* look for parent */
1671 for (i
= 0; i
< ScreenCount(display
); i
++)
1672 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1673 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1681 spawn(struct swm_region
*r
, union arg
*args
, int close_fd
)
1686 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
1689 close(ConnectionNumber(display
));
1691 setenv("LD_PRELOAD", SWM_LIB
, 1);
1693 if (asprintf(&ret
, "%d", r
->ws
->idx
) == -1) {
1697 setenv("_SWM_WS", ret
, 1);
1701 if (asprintf(&ret
, "%d", getpid()) == -1) {
1705 setenv("_SWM_PID", ret
, 1);
1709 if (setsid() == -1) {
1716 * close stdin and stdout to prevent interaction between apps
1717 * and the baraction script
1718 * leave stderr open to record errors
1720 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
1724 dup2(fd
, STDIN_FILENO
);
1725 dup2(fd
, STDOUT_FILENO
);
1730 execvp(args
->argv
[0], args
->argv
);
1737 spawnterm(struct swm_region
*r
, union arg
*args
)
1739 DNPRINTF(SWM_D_MISC
, "spawnterm\n");
1742 setenv("_SWM_XTERM_FONTADJ", "", 1);
1748 kill_refs(struct ws_win
*win
)
1751 struct swm_region
*r
;
1752 struct workspace
*ws
;
1757 for (i
= 0; i
< ScreenCount(display
); i
++)
1758 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1759 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
1761 if (win
== ws
->focus
)
1763 if (win
== ws
->focus_prev
)
1764 ws
->focus_prev
= NULL
;
1769 validate_win(struct ws_win
*testwin
)
1772 struct workspace
*ws
;
1773 struct swm_region
*r
;
1774 int i
, x
, foundit
= 0;
1776 if (testwin
== NULL
)
1779 for (i
= 0, foundit
= 0; i
< ScreenCount(display
); i
++)
1780 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1781 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
1783 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
1791 validate_ws(struct workspace
*testws
)
1793 struct swm_region
*r
;
1794 struct workspace
*ws
;
1797 /* validate all ws */
1798 for (i
= 0, foundit
= 0; i
< ScreenCount(display
); i
++)
1799 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1800 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
1809 unfocus_win(struct ws_win
*win
)
1814 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: id: %lu\n", WINID(win
));
1818 if (win
->ws
== NULL
)
1821 if (validate_ws(win
->ws
))
1822 return; /* XXX this gets hit with thunderbird, needs fixing */
1824 if (win
->ws
->r
== NULL
)
1827 if (validate_win(win
)) {
1832 if (win
->ws
->focus
== win
) {
1833 win
->ws
->focus
= NULL
;
1834 win
->ws
->focus_prev
= win
;
1837 if (validate_win(win
->ws
->focus
)) {
1838 kill_refs(win
->ws
->focus
);
1839 win
->ws
->focus
= NULL
;
1841 if (validate_win(win
->ws
->focus_prev
)) {
1842 kill_refs(win
->ws
->focus_prev
);
1843 win
->ws
->focus_prev
= NULL
;
1846 /* drain all previous unfocus events */
1847 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
1850 grabbuttons(win
, 0);
1851 XSetWindowBorder(display
, win
->id
,
1852 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1854 XChangeProperty(display
, win
->s
->root
,
1855 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
1856 PropModeReplace
, (unsigned char *)&none
,1);
1865 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
1867 for (i
= 0; i
< ScreenCount(display
); i
++)
1868 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1869 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1874 focus_win(struct ws_win
*win
)
1879 struct ws_win
*cfw
= NULL
;
1882 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
? win
->id
: 0);
1886 if (win
->ws
== NULL
)
1889 if (validate_ws(win
->ws
))
1890 return; /* XXX this gets hit with thunderbird, needs fixing */
1892 if (validate_win(win
)) {
1897 if (validate_win(win
)) {
1902 XGetInputFocus(display
, &cur_focus
, &rr
);
1903 if ((cfw
= find_window(cur_focus
)) != NULL
)
1906 win
->ws
->focus
= win
;
1908 if (win
->ws
->r
!= NULL
) {
1909 /* drain all previous focus events */
1910 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
1914 XSetInputFocus(display
, win
->id
,
1915 RevertToParent
, CurrentTime
);
1916 XMapRaised(display
, win
->id
);
1917 grabbuttons(win
, 1);
1918 XSetWindowBorder(display
, win
->id
,
1919 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
1920 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
)
1921 XMapRaised(display
, win
->id
);
1923 XChangeProperty(display
, win
->s
->root
,
1924 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
1925 PropModeReplace
, (unsigned char *)&win
->id
,1);
1928 if (window_name_enabled
)
1933 switchws(struct swm_region
*r
, union arg
*args
)
1935 int wsid
= args
->id
, unmap_old
= 0;
1936 struct swm_region
*this_r
, *other_r
;
1938 struct workspace
*new_ws
, *old_ws
;
1945 old_ws
= this_r
->ws
;
1946 new_ws
= &this_r
->s
->ws
[wsid
];
1948 DNPRINTF(SWM_D_WS
, "switchws screen[%d]:%dx%d+%d+%d: "
1949 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
1952 if (new_ws
== NULL
|| old_ws
== NULL
)
1954 if (new_ws
== old_ws
)
1957 other_r
= new_ws
->r
;
1958 if (other_r
== NULL
) {
1959 /* the other workspace is hidden, hide this one */
1963 /* the other ws is visible in another region, exchange them */
1964 other_r
->ws_prior
= new_ws
;
1965 other_r
->ws
= old_ws
;
1966 old_ws
->r
= other_r
;
1968 this_r
->ws_prior
= old_ws
;
1969 this_r
->ws
= new_ws
;
1973 a
.id
= SWM_ARG_ID_FOCUSCUR
;
1974 focus(new_ws
->r
, &a
);
1978 /* unmap old windows */
1980 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
1983 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1984 drain_enter_notify();
1988 cyclews(struct swm_region
*r
, union arg
*args
)
1991 struct swm_screen
*s
= r
->s
;
1993 DNPRINTF(SWM_D_WS
, "cyclews id %d "
1994 "in screen[%d]:%dx%d+%d+%d ws %d\n", args
->id
,
1995 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2000 case SWM_ARG_ID_CYCLEWS_UP
:
2001 if (a
.id
< SWM_WS_MAX
- 1)
2006 case SWM_ARG_ID_CYCLEWS_DOWN
:
2010 a
.id
= SWM_WS_MAX
- 1;
2016 if (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
))
2018 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2022 } while (a
.id
!= r
->ws
->idx
);
2026 priorws(struct swm_region
*r
, union arg
*args
)
2030 DNPRINTF(SWM_D_WS
, "priorws id %d "
2031 "in screen[%d]:%dx%d+%d+%d ws %d\n", args
->id
,
2032 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2034 if (r
->ws_prior
== NULL
)
2037 a
.id
= r
->ws_prior
->idx
;
2042 cyclescr(struct swm_region
*r
, union arg
*args
)
2044 struct swm_region
*rr
= NULL
;
2048 /* do nothing if we don't have more than one screen */
2049 if (!(ScreenCount(display
) > 1 || outputs
> 1))
2054 case SWM_ARG_ID_CYCLESC_UP
:
2055 rr
= TAILQ_NEXT(r
, entry
);
2057 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2059 case SWM_ARG_ID_CYCLESC_DOWN
:
2060 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2062 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2070 /* move mouse to region */
2072 y
= rr
->g
.y
+ 1 + (bar_enabled
? bar_height
: 0);
2073 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2075 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2078 if (rr
->ws
->focus
) {
2079 /* move to focus window */
2080 x
= rr
->ws
->focus
->g
.x
+ 1;
2081 y
= rr
->ws
->focus
->g
.y
+ 1;
2082 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2087 sort_windows(struct ws_win_list
*wl
)
2089 struct ws_win
*win
, *parent
, *nxt
;
2094 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2095 nxt
= TAILQ_NEXT(win
, entry
);
2096 if (win
->transient
) {
2097 parent
= find_window(win
->transient
);
2098 if (parent
== NULL
) {
2099 fprintf(stderr
, "not possible bug\n");
2102 TAILQ_REMOVE(wl
, win
, entry
);
2103 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2110 swapwin(struct swm_region
*r
, union arg
*args
)
2112 struct ws_win
*target
, *source
;
2113 struct ws_win
*cur_focus
;
2114 struct ws_win_list
*wl
;
2117 DNPRINTF(SWM_D_WS
, "swapwin id %d "
2118 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
2119 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2121 cur_focus
= r
->ws
->focus
;
2122 if (cur_focus
== NULL
)
2126 wl
= &source
->ws
->winlist
;
2129 case SWM_ARG_ID_SWAPPREV
:
2130 if (source
->transient
)
2131 source
= find_window(source
->transient
);
2132 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2133 if (target
&& target
->transient
)
2134 target
= find_window(target
->transient
);
2135 TAILQ_REMOVE(wl
, source
, entry
);
2137 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2139 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2141 case SWM_ARG_ID_SWAPNEXT
:
2142 target
= TAILQ_NEXT(source
, entry
);
2143 /* move the parent and let the sort handle the move */
2144 if (source
->transient
)
2145 source
= find_window(source
->transient
);
2146 TAILQ_REMOVE(wl
, source
, entry
);
2148 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2150 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2152 case SWM_ARG_ID_SWAPMAIN
:
2153 target
= TAILQ_FIRST(wl
);
2154 if (target
== source
) {
2155 if (source
->ws
->focus_prev
!= NULL
&&
2156 source
->ws
->focus_prev
!= target
)
2158 source
= source
->ws
->focus_prev
;
2162 if (target
== NULL
|| source
== NULL
)
2164 source
->ws
->focus_prev
= target
;
2165 TAILQ_REMOVE(wl
, target
, entry
);
2166 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2167 TAILQ_REMOVE(wl
, source
, entry
);
2168 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2170 case SWM_ARG_ID_MOVELAST
:
2171 TAILQ_REMOVE(wl
, source
, entry
);
2172 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2175 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
2185 focus_prev(struct ws_win
*win
)
2187 struct ws_win
*winfocus
= NULL
, *winlostfocus
= NULL
;
2188 struct ws_win
*cur_focus
= NULL
;
2189 struct ws_win_list
*wl
= NULL
;
2190 struct workspace
*ws
= NULL
;
2192 DNPRINTF(SWM_D_FOCUS
, "focus_prev: id %lu\n", WINID(win
));
2194 if (!(win
&& win
->ws
))
2199 cur_focus
= ws
->focus
;
2200 winlostfocus
= cur_focus
;
2202 /* pickle, just focus on whatever */
2203 if (cur_focus
== NULL
) {
2204 /* use prev_focus if valid */
2205 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2206 find_window(WINID(ws
->focus_prev
)))
2207 winfocus
= ws
->focus_prev
;
2208 if (winfocus
== NULL
)
2209 winfocus
= TAILQ_FIRST(wl
);
2213 /* if transient focus on parent */
2214 if (cur_focus
->transient
) {
2215 winfocus
= find_window(cur_focus
->transient
);
2219 /* if in max_stack try harder */
2220 if (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
) {
2221 if (cur_focus
!= ws
->focus_prev
)
2222 winfocus
= ws
->focus_prev
;
2223 else if (cur_focus
!= ws
->focus
)
2224 winfocus
= ws
->focus
;
2226 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2231 if (cur_focus
== win
)
2232 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2233 if (winfocus
== NULL
)
2234 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2235 if (winfocus
== NULL
|| winfocus
== win
)
2236 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2238 if (winfocus
== winlostfocus
|| winfocus
== NULL
)
2241 focus_magic(winfocus
);
2245 focus(struct swm_region
*r
, union arg
*args
)
2247 struct ws_win
*winfocus
= NULL
, *winlostfocus
= NULL
, *head
;
2248 struct ws_win
*cur_focus
= NULL
;
2249 struct ws_win_list
*wl
= NULL
;
2250 struct workspace
*ws
= NULL
;
2255 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
2257 /* treat FOCUS_CUR special */
2258 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2259 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2260 winfocus
= r
->ws
->focus
;
2261 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2262 winfocus
= r
->ws
->focus_prev
;
2264 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2265 if (winfocus
->iconic
== 0)
2268 focus_magic(winfocus
);
2272 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2277 winlostfocus
= cur_focus
;
2280 case SWM_ARG_ID_FOCUSPREV
:
2281 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2283 head
= TAILQ_LAST(wl
, ws_win_list
);
2285 if (WINID(winfocus
) == cur_focus
->transient
) {
2286 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2288 head
= TAILQ_LAST(wl
, ws_win_list
);
2293 if (winfocus
&& winfocus
->iconic
) {
2294 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
2295 if (winfocus
->iconic
== 0)
2300 case SWM_ARG_ID_FOCUSNEXT
:
2301 head
= TAILQ_NEXT(cur_focus
, entry
);
2303 head
= TAILQ_FIRST(wl
);
2307 if (winfocus
&& winfocus
->iconic
) {
2308 TAILQ_FOREACH(winfocus
, wl
, entry
)
2309 if (winfocus
->iconic
== 0)
2314 case SWM_ARG_ID_FOCUSMAIN
:
2315 winfocus
= TAILQ_FIRST(wl
);
2316 if (winfocus
== cur_focus
)
2317 winfocus
= cur_focus
->ws
->focus_prev
;
2323 if (winfocus
== winlostfocus
|| winfocus
== NULL
)
2326 focus_magic(winfocus
);
2330 cycle_layout(struct swm_region
*r
, union arg
*args
)
2332 struct workspace
*ws
= r
->ws
;
2333 struct ws_win
*winfocus
;
2336 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2338 winfocus
= ws
->focus
;
2341 if (ws
->cur_layout
->l_stack
== NULL
)
2342 ws
->cur_layout
= &layouts
[0];
2345 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2346 drain_enter_notify();
2347 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2353 stack_config(struct swm_region
*r
, union arg
*args
)
2355 struct workspace
*ws
= r
->ws
;
2357 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
2360 if (ws
->cur_layout
->l_config
!= NULL
)
2361 ws
->cur_layout
->l_config(ws
, args
->id
);
2363 if (args
->id
!= SWM_ARG_ID_STACKINIT
);
2369 struct swm_geometry g
;
2370 struct swm_region
*r
;
2373 DNPRINTF(SWM_D_STACK
, "stack\n");
2375 for (i
= 0; i
< ScreenCount(display
); i
++) {
2377 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2378 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
2379 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
2381 /* start with screen geometry, adjust for bar */
2383 g
.w
-= 2 * border_width
;
2384 g
.h
-= 2 * border_width
;
2390 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
2391 /* save r so we can track region changes */
2398 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2399 drain_enter_notify();
2403 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
2405 /* retain window geom and region geom */
2406 win
->g_float
.x
= win
->g
.x
;
2407 win
->g_float
.y
= win
->g
.y
;
2408 win
->g_float
.w
= win
->g
.w
;
2409 win
->g_float
.h
= win
->g
.h
;
2410 win
->rg_float
.x
= r
->g
.x
;
2411 win
->rg_float
.y
= r
->g
.y
;
2412 win
->rg_float
.w
= r
->g
.w
;
2413 win
->rg_float
.h
= r
->g
.h
;
2414 win
->g_floatvalid
= 1;
2418 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
2426 bzero(&wc
, sizeof wc
);
2427 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
2430 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2431 * geom on ws switches or return from max mode
2434 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&& win
->g_floatvalid
2435 && !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
2437 * use stored g and rg to set relative position and size
2438 * as in old region or before max stack mode
2440 win
->g
.x
= win
->g_float
.x
- win
->rg_float
.x
+ r
->g
.x
;
2441 win
->g
.y
= win
->g_float
.y
- win
->rg_float
.y
+ r
->g
.y
;
2442 win
->g
.w
= win
->g_float
.w
;
2443 win
->g
.h
= win
->g_float
.h
;
2444 win
->g_floatvalid
= 0;
2447 win
->floatmaxed
= 0;
2449 if ((win
->quirks
& SWM_Q_FULLSCREEN
) && (win
->g
.w
>= WIDTH(r
)) &&
2450 (win
->g
.h
>= HEIGHT(r
)))
2451 wc
.border_width
= 0;
2453 wc
.border_width
= border_width
;
2454 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
2455 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
2456 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
2461 * floaters and transients are auto-centred unless moved
2464 win
->g
.x
= r
->g
.x
+ (WIDTH(r
) - win
->g
.w
) / 2 - wc
.border_width
;
2465 win
->g
.y
= r
->g
.y
+ (HEIGHT(r
) - win
->g
.h
) / 2 - wc
.border_width
;
2468 /* win can be outside r if new r smaller than old r */
2469 /* Ensure top left corner inside r (move probs otherwise) */
2470 if (win
->g
.x
< r
->g
.x
- wc
.border_width
)
2471 win
->g
.x
= r
->g
.x
- wc
.border_width
;
2472 if (win
->g
.x
> r
->g
.x
+ r
->g
.w
- 1)
2473 win
->g
.x
= (win
->g
.w
> r
->g
.w
) ? r
->g
.x
:
2474 (r
->g
.x
+ r
->g
.w
- win
->g
.w
- 2 * wc
.border_width
);
2475 if (win
->g
.y
< r
->g
.y
- wc
.border_width
)
2476 win
->g
.y
= r
->g
.y
- wc
.border_width
;
2477 if (win
->g
.y
> r
->g
.y
+ r
->g
.h
- 1)
2478 win
->g
.y
= (win
->g
.h
> r
->g
.h
) ? r
->g
.y
:
2479 (r
->g
.y
+ r
->g
.h
- win
->g
.h
- 2 * wc
.border_width
);
2483 wc
.width
= win
->g
.w
;
2484 wc
.height
= win
->g
.h
;
2487 * Retain floater and transient geometry for correct positioning
2488 * when ws changes region
2490 if (!(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
2491 store_float_geom(win
, r
);
2493 DNPRINTF(SWM_D_MISC
, "stack_floater: win %lu x %d y %d w %d h %d\n",
2494 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
2496 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2500 * Send keystrokes to terminal to decrease/increase the font size as the
2501 * window size changes.
2504 adjust_font(struct ws_win
*win
)
2506 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
2507 win
->floating
|| win
->transient
)
2510 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
2511 win
->g
.w
/ win
->sh
.width_inc
< term_width
&&
2512 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
2513 win
->font_size_boundary
[win
->font_steps
] =
2514 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
2517 win
->last_inc
= win
->sh
.width_inc
;
2518 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
2519 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
2520 win
->g
.w
> win
->font_size_boundary
[win
->font_steps
- 1]) {
2523 win
->last_inc
= win
->sh
.width_inc
;
2524 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
2528 #define SWAPXY(g) do { \
2530 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2531 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2534 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
2537 XWindowAttributes wa
;
2538 struct swm_geometry win_g
, r_g
= *g
;
2539 struct ws_win
*win
, *fs_win
= 0;
2540 int i
, j
, s
, stacks
;
2541 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
2542 int hrh
, extra
= 0, h_slice
, last_h
= 0;
2543 int split
, colno
, winno
, mwin
, msize
, mscale
;
2544 int remain
, missing
, v_slice
, reconfigure
;
2547 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d\n rot=%s flip=%s",
2548 ws
->idx
, rot
? "yes" : "no", flip
? "yes" : "no");
2550 winno
= count_win(ws
, 0);
2551 if (winno
== 0 && count_win(ws
, 1) == 0)
2554 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2555 if (win
->transient
== 0 && win
->floating
== 0 && win
->iconic
== 0)
2562 w_inc
= win
->sh
.width_inc
;
2563 w_base
= win
->sh
.base_width
;
2564 mwin
= ws
->l_state
.horizontal_mwin
;
2565 mscale
= ws
->l_state
.horizontal_msize
;
2566 stacks
= ws
->l_state
.horizontal_stacks
;
2569 w_inc
= win
->sh
.height_inc
;
2570 w_base
= win
->sh
.base_height
;
2571 mwin
= ws
->l_state
.vertical_mwin
;
2572 mscale
= ws
->l_state
.vertical_msize
;
2573 stacks
= ws
->l_state
.vertical_stacks
;
2577 if (stacks
> winno
- mwin
)
2578 stacks
= winno
- mwin
;
2582 h_slice
= r_g
.h
/ SWM_H_SLICE
;
2583 if (mwin
&& winno
> mwin
) {
2584 v_slice
= r_g
.w
/ SWM_V_SLICE
;
2588 win_g
.w
= v_slice
* mscale
;
2590 if (w_inc
> 1 && w_inc
< v_slice
) {
2591 /* adjust for window's requested size increment */
2592 remain
= (win_g
.w
- w_base
) % w_inc
;
2593 missing
= w_inc
- remain
;
2600 win_g
.x
+= r_g
.w
- msize
;
2603 colno
= split
= winno
/ stacks
;
2604 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) + 2 * border_width
) / stacks
);
2606 hrh
= r_g
.h
/ colno
;
2607 extra
= r_g
.h
- (colno
* hrh
);
2608 win_g
.h
= hrh
- 2 * border_width
;
2610 /* stack all the tiled windows */
2611 i
= j
= 0, s
= stacks
;
2612 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2613 if (win
->transient
!= 0 || win
->floating
!= 0)
2615 if (win
->iconic
!= 0)
2618 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
2623 if (split
&& i
== split
) {
2624 colno
= (winno
- mwin
) / stacks
;
2625 if (s
<= (winno
- mwin
) % stacks
)
2627 split
= split
+ colno
;
2628 hrh
= (r_g
.h
/ colno
);
2629 extra
= r_g
.h
- (colno
* hrh
);
2633 win_g
.x
+= win_g
.w
+ 2 * border_width
;
2634 win_g
.w
= (r_g
.w
- msize
- (stacks
* 2 * border_width
)) / stacks
;
2636 win_g
.w
+= (r_g
.w
- msize
- (stacks
* 2 * border_width
)) %
2641 win_g
.h
= hrh
- 2 * border_width
;
2643 h_inc
= win
->sh
.width_inc
;
2644 h_base
= win
->sh
.base_width
;
2646 h_inc
= win
->sh
.height_inc
;
2647 h_base
= win
->sh
.base_height
;
2649 if (j
== colno
- 1) {
2650 win_g
.h
= hrh
+ extra
;
2651 } else if (h_inc
> 1 && h_inc
< h_slice
) {
2652 /* adjust for window's requested size increment */
2653 remain
= (win_g
.h
- h_base
) % h_inc
;
2654 missing
= h_inc
- remain
;
2656 if (missing
<= extra
|| j
== 0) {
2668 win_g
.y
+= last_h
+ 2 * border_width
;
2670 bzero(&wc
, sizeof wc
);
2671 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
2672 wc
.border_width
= 0;
2673 win_g
.w
+= 2 * border_width
;
2674 win_g
.h
+= 2 * border_width
;
2676 wc
.border_width
= border_width
;
2679 if (win
->g
.x
!= win_g
.y
|| win
->g
.y
!= win_g
.x
||
2680 win
->g
.w
!= win_g
.h
|| win
->g
.h
!= win_g
.w
) {
2682 win
->g
.x
= wc
.x
= win_g
.y
;
2683 win
->g
.y
= wc
.y
= win_g
.x
;
2684 win
->g
.w
= wc
.width
= win_g
.h
;
2685 win
->g
.h
= wc
.height
= win_g
.w
;
2688 if (win
->g
.x
!= win_g
.x
|| win
->g
.y
!= win_g
.y
||
2689 win
->g
.w
!= win_g
.w
|| win
->g
.h
!= win_g
.h
) {
2691 win
->g
.x
= wc
.x
= win_g
.x
;
2692 win
->g
.y
= wc
.y
= win_g
.y
;
2693 win
->g
.w
= wc
.width
= win_g
.w
;
2694 win
->g
.h
= wc
.height
= win_g
.h
;
2699 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
2700 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2703 if (XGetWindowAttributes(display
, win
->id
, &wa
))
2704 if (wa
.map_state
== IsUnmapped
)
2705 XMapRaised(display
, win
->id
);
2713 /* now, stack all the floaters and transients */
2714 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2715 if (win
->transient
== 0 && win
->floating
== 0)
2717 if (win
->iconic
== 1)
2719 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
2724 stack_floater(win
, ws
->r
);
2725 XMapRaised(display
, win
->id
);
2729 stack_floater(fs_win
, ws
->r
);
2730 XMapRaised(display
, fs_win
->id
);
2735 vertical_config(struct workspace
*ws
, int id
)
2737 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
2740 case SWM_ARG_ID_STACKRESET
:
2741 case SWM_ARG_ID_STACKINIT
:
2742 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
2743 ws
->l_state
.vertical_mwin
= 1;
2744 ws
->l_state
.vertical_stacks
= 1;
2746 case SWM_ARG_ID_MASTERSHRINK
:
2747 if (ws
->l_state
.vertical_msize
> 1)
2748 ws
->l_state
.vertical_msize
--;
2750 case SWM_ARG_ID_MASTERGROW
:
2751 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
2752 ws
->l_state
.vertical_msize
++;
2754 case SWM_ARG_ID_MASTERADD
:
2755 ws
->l_state
.vertical_mwin
++;
2757 case SWM_ARG_ID_MASTERDEL
:
2758 if (ws
->l_state
.vertical_mwin
> 0)
2759 ws
->l_state
.vertical_mwin
--;
2761 case SWM_ARG_ID_STACKINC
:
2762 ws
->l_state
.vertical_stacks
++;
2764 case SWM_ARG_ID_STACKDEC
:
2765 if (ws
->l_state
.vertical_stacks
> 1)
2766 ws
->l_state
.vertical_stacks
--;
2774 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
2776 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
2778 stack_master(ws
, g
, 0, 0);
2782 horizontal_config(struct workspace
*ws
, int id
)
2784 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
2787 case SWM_ARG_ID_STACKRESET
:
2788 case SWM_ARG_ID_STACKINIT
:
2789 ws
->l_state
.horizontal_mwin
= 1;
2790 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
2791 ws
->l_state
.horizontal_stacks
= 1;
2793 case SWM_ARG_ID_MASTERSHRINK
:
2794 if (ws
->l_state
.horizontal_msize
> 1)
2795 ws
->l_state
.horizontal_msize
--;
2797 case SWM_ARG_ID_MASTERGROW
:
2798 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
2799 ws
->l_state
.horizontal_msize
++;
2801 case SWM_ARG_ID_MASTERADD
:
2802 ws
->l_state
.horizontal_mwin
++;
2804 case SWM_ARG_ID_MASTERDEL
:
2805 if (ws
->l_state
.horizontal_mwin
> 0)
2806 ws
->l_state
.horizontal_mwin
--;
2808 case SWM_ARG_ID_STACKINC
:
2809 ws
->l_state
.horizontal_stacks
++;
2811 case SWM_ARG_ID_STACKDEC
:
2812 if (ws
->l_state
.horizontal_stacks
> 1)
2813 ws
->l_state
.horizontal_stacks
--;
2821 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
2823 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
2825 stack_master(ws
, g
, 1, 0);
2828 /* fullscreen view */
2830 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
2833 struct swm_geometry gg
= *g
;
2834 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
2838 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
2843 winno
= count_win(ws
, 0);
2844 if (winno
== 0 && count_win(ws
, 1) == 0)
2847 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2848 if (win
->transient
) {
2850 parent
= find_window(win
->transient
);
2854 if (win
->floating
&& win
->floatmaxed
== 0 ) {
2856 * retain geometry for retrieval on exit from
2859 store_float_geom(win
, ws
->r
);
2860 win
->floatmaxed
= 1;
2863 /* only reconfigure if necessary */
2864 if (win
->g
.x
!= gg
.x
|| win
->g
.y
!= gg
.y
|| win
->g
.w
!= gg
.w
||
2866 bzero(&wc
, sizeof wc
);
2867 win
->g
.x
= wc
.x
= gg
.x
;
2868 win
->g
.y
= wc
.y
= gg
.y
;
2870 wc
.border_width
= border_width
;
2871 win
->g
.w
= wc
.width
= gg
.w
;
2872 win
->g
.h
= wc
.height
= gg
.h
;
2874 wc
.border_width
= 0;
2875 win
->g
.w
= wc
.width
= gg
.w
+ 2 * border_width
;
2876 win
->g
.h
= wc
.height
= gg
.h
+ 2 * border_width
;
2878 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
2879 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2881 /* unmap only if we don't have multi screen */
2882 if (win
!= ws
->focus
)
2883 if (!(ScreenCount(display
) > 1 || outputs
> 1))
2887 /* put the last transient on top */
2890 XMapRaised(display
, parent
->id
);
2891 stack_floater(wintrans
, ws
->r
);
2892 focus_magic(wintrans
);
2897 send_to_ws(struct swm_region
*r
, union arg
*args
)
2899 int wsid
= args
->id
;
2900 struct ws_win
*win
= NULL
, *parent
;
2901 struct workspace
*ws
, *nws
;
2902 Atom ws_idx_atom
= 0;
2903 unsigned char ws_idx_str
[SWM_PROPLEN
];
2912 if (win
->ws
->idx
== wsid
)
2915 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
2918 nws
= &win
->s
->ws
[wsid
];
2920 a
.id
= SWM_ARG_ID_FOCUSPREV
;
2922 if (win
->transient
) {
2923 parent
= find_window(win
->transient
);
2925 unmap_window(parent
);
2926 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
2927 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
2932 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
2933 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
2936 /* Try to update the window's workspace property */
2937 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
2939 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
2940 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
2942 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
2943 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
2950 iconify(struct swm_region
*r
, union arg
*args
)
2954 if (r
->ws
->focus
== NULL
)
2956 unmap_window(r
->ws
->focus
);
2957 update_iconic(r
->ws
->focus
, 1);
2959 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2960 drain_enter_notify();
2961 r
->ws
->focus
= NULL
;
2962 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2967 get_win_name(Display
*dpy
, Window win
, Atom wname
, Atom stype
,
2968 unsigned long *slen
)
2971 unsigned long nitems
, nbytes
, nextra
;
2972 unsigned char *prop
= NULL
;
2975 status
= XGetWindowProperty(dpy
, win
, wname
, 0L, 0L, False
, stype
,
2976 &rettype
, &retfmt
, &nitems
, &nbytes
, &prop
);
2977 if (status
!= Success
)
2981 status
= XGetWindowProperty(dpy
, win
, wname
, 0L, nbytes
, False
,
2982 stype
, &rettype
, &retfmt
, &nitems
, &nextra
, &prop
);
2983 if (status
!= Success
) {
2987 if (rettype
!= stype
) {
2996 uniconify(struct swm_region
*r
, union arg
*args
)
3004 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3006 if (r
&& r
->ws
== NULL
)
3009 /* make sure we have anything to uniconify */
3010 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3011 if (win
->ws
== NULL
)
3012 continue; /* should never happen */
3013 if (win
->iconic
== 0)
3022 spawn_select(r
, args
, "uniconify", &searchpid
);
3024 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3027 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3028 if (win
->ws
== NULL
)
3029 continue; /* should never happen */
3030 if (win
->iconic
== 0)
3033 name
= get_win_name(display
, win
->id
, a_wmname
, a_string
,
3037 fprintf(lfile
, "%s.%lu\n", name
, win
->id
);
3044 #define MAX_RESP_LEN 1024
3047 search_do_resp(void)
3051 char *name
, *resp
, *s
;
3054 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
3059 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
3060 fprintf(stderr
, "search: calloc\n");
3064 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
3066 fprintf(stderr
, "search: read error: %s\n", strerror(errno
));
3069 resp
[rbytes
] = '\0';
3072 DNPRINTF(SWM_D_MISC
, "search_do_resp: resp %s\n", resp
);
3073 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3074 if (win
->iconic
== 0)
3076 name
= get_win_name(display
, win
->id
, a_wmname
, a_string
, &len
);
3079 if (asprintf(&s
, "%s.%lu", name
, win
->id
) == -1) {
3084 if (strncmp(s
, resp
, len
) == 0) {
3085 /* XXX this should be a callback to generalize */
3086 update_iconic(win
, 0);
3097 wkill(struct swm_region
*r
, union arg
*args
)
3099 DNPRINTF(SWM_D_MISC
, "wkill %d\n", args
->id
);
3101 if (r
->ws
->focus
== NULL
)
3104 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
3105 XKillClient(display
, r
->ws
->focus
->id
);
3107 if (r
->ws
->focus
->can_delete
)
3108 client_msg(r
->ws
->focus
, adelete
);
3113 floating_toggle_win(struct ws_win
*win
)
3115 struct swm_region
*r
;
3125 /* reject floating toggles in max stack mode */
3126 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3129 if (win
->floating
) {
3130 if (!win
->floatmaxed
) {
3131 /* retain position for refloat */
3132 store_float_geom(win
, r
);
3136 if (win
->g_floatvalid
) {
3137 /* refloat at last floating relative position */
3138 win
->g
.x
= win
->g_float
.x
- win
->rg_float
.x
+ r
->g
.x
;
3139 win
->g
.y
= win
->g_float
.y
- win
->rg_float
.y
+ r
->g
.y
;
3140 win
->g
.w
= win
->g_float
.w
;
3141 win
->g
.h
= win
->g_float
.h
;
3146 ewmh_update_actions(win
);
3152 floating_toggle(struct swm_region
*r
, union arg
*args
)
3154 struct ws_win
*win
= r
->ws
->focus
;
3160 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
3161 _NET_WM_STATE_TOGGLE
);
3164 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3165 drain_enter_notify();
3167 if (win
== win
->ws
->focus
) {
3168 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3169 focus(win
->ws
->r
, &a
);
3174 resize_window(struct ws_win
*win
, int center
)
3178 struct swm_region
*r
;
3180 r
= root_to_region(win
->wa
.root
);
3181 bzero(&wc
, sizeof wc
);
3182 mask
= CWBorderWidth
| CWWidth
| CWHeight
;
3183 wc
.border_width
= border_width
;
3184 wc
.width
= win
->g
.w
;
3185 wc
.height
= win
->g
.h
;
3186 if (center
== SWM_ARG_ID_CENTER
) {
3187 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2 - border_width
;
3188 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2 - border_width
;
3192 DNPRINTF(SWM_D_STACK
, "resize_window: win %lu x %d y %d w %d h %d\n",
3193 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
3195 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3199 resize(struct ws_win
*win
, union arg
*args
)
3203 struct swm_region
*r
= win
->ws
->r
;
3208 DNPRINTF(SWM_D_MOUSE
, "resize: win %lu floating %d trans %lu\n",
3209 win
->id
, win
->floating
, win
->transient
);
3211 if (!(win
->transient
!= 0 || win
->floating
!= 0))
3214 /* reject resizes in max mode for floaters (transient ok) */
3215 if (win
->floatmaxed
)
3219 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
3221 /* raise the window = move to last in window list */
3222 a
.id
= SWM_ARG_ID_MOVELAST
;
3225 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3226 drain_enter_notify();
3228 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
3229 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
3232 /* place pointer at bottom left corner or nearest point inside r */
3233 if ( win
->g
.x
+ win
->g
.w
< r
->g
.x
+ r
->g
.w
- 1)
3234 relx
= win
->g
.w
- 1;
3236 relx
= r
->g
.x
+ r
->g
.w
- win
->g
.x
- 1;
3238 if ( win
->g
.y
+ win
->g
.h
< r
->g
.y
+ r
->g
.h
- 1)
3239 rely
= win
->g
.h
- 1;
3241 rely
= r
->g
.y
+ r
->g
.h
- win
->g
.y
- 1;
3243 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, relx
, rely
);
3245 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
3246 SubstructureRedirectMask
, &ev
);
3248 case ConfigureRequest
:
3251 handler
[ev
.type
](&ev
);
3254 /* do not allow resize outside of region */
3255 if ( ev
.xmotion
.x_root
< r
->g
.x
||
3256 ev
.xmotion
.x_root
> r
->g
.x
+ r
->g
.w
- 1 ||
3257 ev
.xmotion
.y_root
< r
->g
.y
||
3258 ev
.xmotion
.y_root
> r
->g
.y
+ r
->g
.h
- 1)
3261 if (ev
.xmotion
.x
<= 1)
3263 if (ev
.xmotion
.y
<= 1)
3265 win
->g
.w
= ev
.xmotion
.x
+ 1;
3266 win
->g
.h
= ev
.xmotion
.y
+ 1;
3268 /* not free, don't sync more than 120 times / second */
3269 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
3270 time
= ev
.xmotion
.time
;
3271 XSync(display
, False
);
3272 resize_window(win
, args
->id
);
3276 } while (ev
.type
!= ButtonRelease
);
3278 XSync(display
, False
);
3279 resize_window(win
, args
->id
);
3281 store_float_geom(win
,r
);
3283 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
- 1,
3285 XUngrabPointer(display
, CurrentTime
);
3288 drain_enter_notify();
3292 move_window(struct ws_win
*win
)
3296 struct swm_region
*r
;
3298 r
= root_to_region(win
->wa
.root
);
3299 bzero(&wc
, sizeof wc
);
3303 wc
.border_width
= border_width
;
3305 DNPRINTF(SWM_D_STACK
, "move_window: win %lu x %d y %d w %d h %d\n",
3306 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
3308 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3312 move(struct ws_win
*win
, union arg
*args
)
3316 struct swm_region
*r
= win
->ws
->r
;
3319 DNPRINTF(SWM_D_MOUSE
, "move: win %lu floating %d trans %lu\n",
3320 win
->id
, win
->floating
, win
->transient
);
3322 /* in max_stack mode should only move transients */
3323 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
3327 if (win
->floating
== 0 && !win
->transient
) {
3329 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
3332 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
3335 /* raise the window = move to last in window list */
3336 a
.id
= SWM_ARG_ID_MOVELAST
;
3340 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
3341 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
3343 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
3345 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
3346 SubstructureRedirectMask
, &ev
);
3348 case ConfigureRequest
:
3351 handler
[ev
.type
](&ev
);
3354 /* don't allow to move window origin out of region */
3355 if ( ev
.xmotion
.x_root
< r
->g
.x
||
3356 ev
.xmotion
.x_root
> r
->g
.x
+ r
->g
.w
- 1 ||
3357 ev
.xmotion
.y_root
< r
->g
.y
||
3358 ev
.xmotion
.y_root
> r
->g
.y
+ r
->g
.h
- 1)
3361 win
->g
.x
= ev
.xmotion
.x_root
- border_width
;
3362 win
->g
.y
= ev
.xmotion
.y_root
- border_width
;
3364 /* not free, don't sync more than 120 times / second */
3365 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
3366 time
= ev
.xmotion
.time
;
3367 XSync(display
, False
);
3372 } while (ev
.type
!= ButtonRelease
);
3374 XSync(display
, False
);
3377 store_float_geom(win
,r
);
3378 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
3379 XUngrabPointer(display
, CurrentTime
);
3382 drain_enter_notify();
3385 /* user/key callable function IDs */
3442 kf_dumpwins
, /* MUST BE LAST */
3446 /* key definitions */
3448 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
3453 legacyfunc(struct swm_region
*r
, union arg
*args
)
3458 char name
[SWM_FUNCNAME_LEN
];
3459 void (*func
)(struct swm_region
*r
, union arg
*);
3461 } keyfuncs
[kf_invalid
+ 1] = {
3462 /* name function argument */
3463 { "cycle_layout", cycle_layout
, {0} },
3464 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
3465 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
3466 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
3467 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
3468 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
3469 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
3470 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
3471 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
3472 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
3473 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
3474 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
3475 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
3476 { "spawn_term", spawnterm
, {.argv
= spawn_term
} },
3477 { "spawn_menu", legacyfunc
, {0} },
3478 { "quit", quit
, {0} },
3479 { "restart", restart
, {0} },
3480 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
3481 { "ws_1", switchws
, {.id
= 0} },
3482 { "ws_2", switchws
, {.id
= 1} },
3483 { "ws_3", switchws
, {.id
= 2} },
3484 { "ws_4", switchws
, {.id
= 3} },
3485 { "ws_5", switchws
, {.id
= 4} },
3486 { "ws_6", switchws
, {.id
= 5} },
3487 { "ws_7", switchws
, {.id
= 6} },
3488 { "ws_8", switchws
, {.id
= 7} },
3489 { "ws_9", switchws
, {.id
= 8} },
3490 { "ws_10", switchws
, {.id
= 9} },
3491 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
3492 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
3493 { "ws_prior", priorws
, {0} },
3494 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
3495 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
3496 { "mvws_1", send_to_ws
, {.id
= 0} },
3497 { "mvws_2", send_to_ws
, {.id
= 1} },
3498 { "mvws_3", send_to_ws
, {.id
= 2} },
3499 { "mvws_4", send_to_ws
, {.id
= 3} },
3500 { "mvws_5", send_to_ws
, {.id
= 4} },
3501 { "mvws_6", send_to_ws
, {.id
= 5} },
3502 { "mvws_7", send_to_ws
, {.id
= 6} },
3503 { "mvws_8", send_to_ws
, {.id
= 7} },
3504 { "mvws_9", send_to_ws
, {.id
= 8} },
3505 { "mvws_10", send_to_ws
, {.id
= 9} },
3506 { "bar_toggle", bar_toggle
, {0} },
3507 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
3508 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
3509 { "screenshot_all", legacyfunc
, {0} },
3510 { "screenshot_wind", legacyfunc
, {0} },
3511 { "float_toggle", floating_toggle
,{0} },
3512 { "version", version
, {0} },
3513 { "spawn_lock", legacyfunc
, {0} },
3514 { "spawn_initscr", legacyfunc
, {0} },
3515 { "spawn_custom", dummykeyfunc
, {0} },
3516 { "iconify", iconify
, {0} },
3517 { "uniconify", uniconify
, {0} },
3518 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
3519 { "invalid key func", NULL
, {0} },
3524 enum keyfuncid funcid
;
3527 int keys_size
= 0, keys_length
= 0;
3528 struct key
*keys
= NULL
;
3531 enum { client_click
, root_click
};
3533 unsigned int action
;
3535 unsigned int button
;
3536 void (*func
)(struct ws_win
*, union arg
*);
3539 /* action key mouse button func args */
3540 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
3541 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
3542 { client_click
, MODKEY
, Button1
, move
, {0} },
3546 update_modkey(unsigned int mod
)
3551 for (i
= 0; i
< keys_length
; i
++)
3552 if (keys
[i
].mod
& ShiftMask
)
3553 keys
[i
].mod
= mod
| ShiftMask
;
3557 for (i
= 0; i
< LENGTH(buttons
); i
++)
3558 if (buttons
[i
].mask
& ShiftMask
)
3559 buttons
[i
].mask
= mod
| ShiftMask
;
3561 buttons
[i
].mask
= mod
;
3571 int spawns_size
= 0, spawns_length
= 0;
3572 struct spawn_prog
*spawns
= NULL
;
3575 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
3578 struct spawn_prog
*prog
= NULL
;
3580 char *ap
, **real_args
;
3582 DNPRINTF(SWM_D_SPAWN
, "spawn_expand %s\n", spawn_name
);
3585 for (i
= 0; i
< spawns_length
; i
++) {
3586 if (!strcasecmp(spawn_name
, spawns
[i
].name
))
3590 fprintf(stderr
, "spawn_custom: program %s not found\n",
3595 /* make room for expanded args */
3596 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
3597 err(1, "spawn_custom: calloc real_args");
3599 /* expand spawn_args into real_args */
3600 for (i
= 0; i
< prog
->argc
; i
++) {
3602 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg = %s\n", ap
);
3603 if (!strcasecmp(ap
, "$bar_border")) {
3605 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
3607 err(1, "spawn_custom border color");
3608 } else if (!strcasecmp(ap
, "$bar_color")) {
3610 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
3612 err(1, "spawn_custom bar color");
3613 } else if (!strcasecmp(ap
, "$bar_font")) {
3614 if ((real_args
[i
] = strdup(bar_fonts
[bar_fidx
]))
3616 err(1, "spawn_custom bar fonts");
3617 } else if (!strcasecmp(ap
, "$bar_font_color")) {
3619 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
3621 err(1, "spawn_custom color font");
3622 } else if (!strcasecmp(ap
, "$color_focus")) {
3624 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
3626 err(1, "spawn_custom color focus");
3627 } else if (!strcasecmp(ap
, "$color_unfocus")) {
3629 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
3631 err(1, "spawn_custom color unfocus");
3633 /* no match --> copy as is */
3634 if ((real_args
[i
] = strdup(ap
)) == NULL
)
3635 err(1, "spawn_custom strdup(ap)");
3637 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg = %s\n",
3642 if ((swm_debug
& SWM_D_SPAWN
) != 0) {
3643 fprintf(stderr
, "spawn_custom: result = ");
3644 for (i
= 0; i
< prog
->argc
; i
++)
3645 fprintf(stderr
, "\"%s\" ", real_args
[i
]);
3646 fprintf(stderr
, "\n");
3649 *ret_args
= real_args
;
3650 return (prog
->argc
);
3654 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
3660 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
3666 for (i
= 0; i
< spawn_argc
; i
++)
3672 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
3678 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
3682 if (pipe(select_list_pipe
) == -1)
3683 err(1, "pipe error");
3684 if (pipe(select_resp_pipe
) == -1)
3685 err(1, "pipe error");
3687 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
3688 err(1, "could not disable SIGPIPE");
3689 switch (*pid
= fork()) {
3691 err(1, "cannot fork");
3694 if (dup2(select_list_pipe
[0], 0) == -1)
3696 if (dup2(select_resp_pipe
[1], 1) == -1)
3698 close(select_list_pipe
[1]);
3699 close(select_resp_pipe
[0]);
3702 default: /* parent */
3703 close(select_list_pipe
[0]);
3704 close(select_resp_pipe
[1]);
3708 for (i
= 0; i
< spawn_argc
; i
++)
3714 setspawn(struct spawn_prog
*prog
)
3718 if (prog
== NULL
|| prog
->name
== NULL
)
3722 for (i
= 0; i
< spawns_length
; i
++) {
3723 if (!strcmp(spawns
[i
].name
, prog
->name
)) {
3725 if (prog
->argv
== NULL
) {
3727 DNPRINTF(SWM_D_SPAWN
,
3728 "setspawn: delete #%d %s\n",
3730 free(spawns
[i
].name
);
3731 for (j
= 0; j
< spawns
[i
].argc
; j
++)
3732 free(spawns
[i
].argv
[j
]);
3733 free(spawns
[i
].argv
);
3734 j
= spawns_length
- 1;
3736 spawns
[i
] = spawns
[j
];
3741 DNPRINTF(SWM_D_SPAWN
,
3742 "setspawn: replace #%d %s\n",
3744 free(spawns
[i
].name
);
3745 for (j
= 0; j
< spawns
[i
].argc
; j
++)
3746 free(spawns
[i
].argv
[j
]);
3747 free(spawns
[i
].argv
);
3750 /* found case handled */
3756 if (prog
->argv
== NULL
) {
3758 "error: setspawn: cannot find program %s", prog
->name
);
3763 /* not found: add */
3764 if (spawns_size
== 0 || spawns
== NULL
) {
3766 DNPRINTF(SWM_D_SPAWN
, "setspawn: init list %d\n", spawns_size
);
3767 spawns
= malloc((size_t)spawns_size
*
3768 sizeof(struct spawn_prog
));
3769 if (spawns
== NULL
) {
3770 fprintf(stderr
, "setspawn: malloc failed\n");
3774 } else if (spawns_length
== spawns_size
) {
3776 DNPRINTF(SWM_D_SPAWN
, "setspawn: grow list %d\n", spawns_size
);
3777 spawns
= realloc(spawns
, (size_t)spawns_size
*
3778 sizeof(struct spawn_prog
));
3779 if (spawns
== NULL
) {
3780 fprintf(stderr
, "setspawn: realloc failed\n");
3786 if (spawns_length
< spawns_size
) {
3787 DNPRINTF(SWM_D_SPAWN
, "setspawn: add #%d %s\n",
3788 spawns_length
, prog
->name
);
3789 i
= spawns_length
++;
3792 fprintf(stderr
, "spawns array problem?\n");
3793 if (spawns
== NULL
) {
3794 fprintf(stderr
, "spawns array is NULL!\n");
3802 setconfspawn(char *selector
, char *value
, int flags
)
3804 struct spawn_prog
*prog
;
3805 char *vp
, *cp
, *word
;
3807 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
3808 if ((prog
= calloc(1, sizeof *prog
)) == NULL
)
3809 err(1, "setconfspawn: calloc prog");
3810 prog
->name
= strdup(selector
);
3811 if (prog
->name
== NULL
)
3812 err(1, "setconfspawn prog->name");
3813 if ((cp
= vp
= strdup(value
)) == NULL
)
3814 err(1, "setconfspawn: strdup(value) ");
3815 while ((word
= strsep(&cp
, " \t")) != NULL
) {
3816 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: arg [%s]\n", word
);
3818 cp
+= (long)strspn(cp
, " \t");
3819 if (strlen(word
) > 0) {
3821 prog
->argv
= realloc(prog
->argv
,
3822 prog
->argc
* sizeof(char *));
3823 if ((prog
->argv
[prog
->argc
- 1] = strdup(word
)) == NULL
)
3824 err(1, "setconfspawn: strdup");
3831 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
3838 setconfspawn("term", "xterm", 0);
3839 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3840 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3841 setconfspawn("lock", "xlock", 0);
3842 setconfspawn("initscr", "initscreen.sh", 0);
3843 setconfspawn("menu", "dmenu_run"
3846 " -nf $bar_font_color"
3848 " -sf $bar_color", 0);
3849 setconfspawn("uniconify", "dmenu"
3853 " -nf $bar_font_color"
3855 " -sf $bar_color", 0);
3859 #define SWM_MODNAME_SIZE 32
3860 #define SWM_KEY_WS "\n+ \t"
3862 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
3866 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
3867 if (mod
== NULL
|| ks
== NULL
) {
3868 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
3871 if (keystr
== NULL
|| strlen(keystr
) == 0) {
3872 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
3878 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
3879 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
3881 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
3882 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
3884 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
3886 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
3888 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
3890 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
3892 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
3894 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
3895 *mod
|= ControlMask
;
3897 *ks
= XStringToKeysym(name
);
3898 XConvertCase(*ks
, ks
, &uks
);
3899 if (ks
== NoSymbol
) {
3901 "parsekeys: invalid key %s\n",
3907 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
3912 strdupsafe(char *str
)
3917 return (strdup(str
));
3921 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
3924 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
3925 keyfuncs
[kfid
].name
, spawn_name
);
3927 for (i
= 0; i
< keys_length
; i
++) {
3928 if (keys
[i
].mod
== mod
&& keys
[i
].keysym
== ks
) {
3929 if (kfid
== kf_invalid
) {
3932 "setkeybinding: delete #%d %s\n",
3933 i
, keyfuncs
[keys
[i
].funcid
].name
);
3934 free(keys
[i
].spawn_name
);
3935 j
= keys_length
- 1;
3939 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
3942 /* found: replace */
3944 "setkeybinding: replace #%d %s %s\n",
3945 i
, keyfuncs
[keys
[i
].funcid
].name
,
3947 free(keys
[i
].spawn_name
);
3949 keys
[i
].keysym
= ks
;
3950 keys
[i
].funcid
= kfid
;
3951 keys
[i
].spawn_name
= strdupsafe(spawn_name
);
3952 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
3957 if (kfid
== kf_invalid
) {
3959 "error: setkeybinding: cannot find mod/key combination");
3960 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
3963 /* not found: add */
3964 if (keys_size
== 0 || keys
== NULL
) {
3966 DNPRINTF(SWM_D_KEY
, "setkeybinding: init list %d\n", keys_size
);
3967 keys
= malloc((size_t)keys_size
* sizeof(struct key
));
3969 fprintf(stderr
, "malloc failed\n");
3973 } else if (keys_length
== keys_size
) {
3975 DNPRINTF(SWM_D_KEY
, "setkeybinding: grow list %d\n", keys_size
);
3976 keys
= realloc(keys
, (size_t)keys_size
* sizeof(struct key
));
3978 fprintf(stderr
, "realloc failed\n");
3983 if (keys_length
< keys_size
) {
3985 DNPRINTF(SWM_D_KEY
, "setkeybinding: add #%d %s %s\n",
3986 j
, keyfuncs
[kfid
].name
, spawn_name
);
3988 keys
[j
].keysym
= ks
;
3989 keys
[j
].funcid
= kfid
;
3990 keys
[j
].spawn_name
= strdupsafe(spawn_name
);
3992 fprintf(stderr
, "keys array problem?\n");
3994 fprintf(stderr
, "keys array problem\n");
3998 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4002 setconfbinding(char *selector
, char *value
, int flags
)
4004 enum keyfuncid kfid
;
4008 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
4009 if (selector
== NULL
) {
4010 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
4011 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4013 setkeybinding(mod
, ks
, kfid
, NULL
);
4018 /* search by key function name */
4019 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
4020 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
4021 SWM_FUNCNAME_LEN
) == 0) {
4022 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4024 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4025 setkeybinding(mod
, ks
, kfid
, NULL
);
4031 /* search by custom spawn name */
4032 for (i
= 0; i
< spawns_length
; i
++) {
4033 if (strcasecmp(selector
, spawns
[i
].name
) == 0) {
4034 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4036 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4037 setkeybinding(mod
, ks
, kf_spawn_custom
,
4044 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
4051 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
4052 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
4053 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
4054 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
4055 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
4056 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
4057 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
4058 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
4059 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
4060 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
4061 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
4062 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
4063 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
4064 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_term
, NULL
);
4065 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
, "menu");
4066 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
4067 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
4068 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
4069 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
4070 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
4071 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
4072 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
4073 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
4074 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
4075 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
4076 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
4077 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
4078 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
4079 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
4080 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
4081 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
4082 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
4083 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
4084 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
4085 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
4086 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
4087 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
4088 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
4089 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
4090 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
4091 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
4092 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
4093 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
4094 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
4095 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
4096 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
4097 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
4098 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
4099 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
, "screenshot_all");
4100 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
, "screenshot_wind");
4101 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
4102 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
4103 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
, "lock");
4104 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
, "initscr");
4105 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
4106 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
4108 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
4113 updatenumlockmask(void)
4116 XModifierKeymap
*modmap
;
4118 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
4120 modmap
= XGetModifierMapping(display
);
4121 for (i
= 0; i
< 8; i
++)
4122 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
4123 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
4124 == XKeysymToKeycode(display
, XK_Num_Lock
))
4125 numlockmask
= (1 << i
);
4127 XFreeModifiermap(modmap
);
4133 unsigned int i
, j
, k
;
4135 unsigned int modifiers
[] =
4136 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
4138 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
4139 updatenumlockmask();
4141 for (k
= 0; k
< ScreenCount(display
); k
++) {
4142 if (TAILQ_EMPTY(&screens
[k
].rl
))
4144 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
4145 for (i
= 0; i
< keys_length
; i
++) {
4146 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
4147 for (j
= 0; j
< LENGTH(modifiers
); j
++)
4148 XGrabKey(display
, code
,
4149 keys
[i
].mod
| modifiers
[j
],
4150 screens
[k
].root
, True
,
4151 GrabModeAsync
, GrabModeAsync
);
4157 grabbuttons(struct ws_win
*win
, int focused
)
4160 unsigned int modifiers
[] =
4161 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
4163 updatenumlockmask();
4164 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
4166 for (i
= 0; i
< LENGTH(buttons
); i
++)
4167 if (buttons
[i
].action
== client_click
)
4168 for (j
= 0; j
< LENGTH(modifiers
); j
++)
4169 XGrabButton(display
, buttons
[i
].button
,
4170 buttons
[i
].mask
| modifiers
[j
],
4171 win
->id
, False
, BUTTONMASK
,
4172 GrabModeAsync
, GrabModeSync
, None
,
4175 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
4176 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
4179 const char *quirkname
[] = {
4180 "NONE", /* config string for "no value" */
4188 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4189 #define SWM_Q_WS "\n|+ \t"
4191 parsequirks(char *qstr
, unsigned long *quirk
)
4201 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
4203 cp
+= (long)strspn(cp
, SWM_Q_WS
);
4204 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
4205 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
4206 DNPRINTF(SWM_D_QUIRK
, "parsequirks: %s\n", name
);
4211 *quirk
|= 1 << (i
-1);
4215 if (i
>= LENGTH(quirkname
)) {
4216 DNPRINTF(SWM_D_QUIRK
,
4217 "parsequirks: invalid quirk [%s]\n", name
);
4225 setquirk(const char *class, const char *name
, const int quirk
)
4230 for (i
= 0; i
< quirks_length
; i
++) {
4231 if (!strcmp(quirks
[i
].class, class) &&
4232 !strcmp(quirks
[i
].name
, name
)) {
4235 DNPRINTF(SWM_D_QUIRK
,
4236 "setquirk: delete #%d %s:%s\n",
4237 i
, quirks
[i
].class, quirks
[i
].name
);
4238 free(quirks
[i
].class);
4239 free(quirks
[i
].name
);
4240 j
= quirks_length
- 1;
4242 quirks
[i
] = quirks
[j
];
4246 /* found: replace */
4247 DNPRINTF(SWM_D_QUIRK
,
4248 "setquirk: replace #%d %s:%s\n",
4249 i
, quirks
[i
].class, quirks
[i
].name
);
4250 free(quirks
[i
].class);
4251 free(quirks
[i
].name
);
4252 quirks
[i
].class = strdup(class);
4253 quirks
[i
].name
= strdup(name
);
4254 quirks
[i
].quirk
= quirk
;
4261 "error: setquirk: cannot find class/name combination");
4264 /* not found: add */
4265 if (quirks_size
== 0 || quirks
== NULL
) {
4267 DNPRINTF(SWM_D_QUIRK
, "setquirk: init list %d\n", quirks_size
);
4268 quirks
= malloc((size_t)quirks_size
* sizeof(struct quirk
));
4270 fprintf(stderr
, "setquirk: malloc failed\n");
4274 } else if (quirks_length
== quirks_size
) {
4276 DNPRINTF(SWM_D_QUIRK
, "setquirk: grow list %d\n", quirks_size
);
4277 quirks
= realloc(quirks
, (size_t)quirks_size
* sizeof(struct quirk
));
4279 fprintf(stderr
, "setquirk: realloc failed\n");
4284 if (quirks_length
< quirks_size
) {
4285 DNPRINTF(SWM_D_QUIRK
, "setquirk: add %d\n", quirks_length
);
4286 j
= quirks_length
++;
4287 quirks
[j
].class = strdup(class);
4288 quirks
[j
].name
= strdup(name
);
4289 quirks
[j
].quirk
= quirk
;
4291 fprintf(stderr
, "quirks array problem?\n");
4293 fprintf(stderr
, "quirks array problem!\n");
4300 setconfquirk(char *selector
, char *value
, int flags
)
4302 char *cp
, *class, *name
;
4304 unsigned long quirks
;
4305 if (selector
== NULL
)
4307 if ((cp
= strchr(selector
, ':')) == NULL
)
4312 if ((retval
= parsequirks(value
, &quirks
)) == 0)
4313 setquirk(class, name
, quirks
);
4320 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
4321 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
4322 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
4323 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
4324 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
4325 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
4326 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
4327 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
4328 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
4329 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
4330 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
4331 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
4332 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
4335 /* conf file stuff */
4336 #define SWM_CONF_FILE "scrotwm.conf"
4338 enum { SWM_S_BAR_DELAY
, SWM_S_BAR_ENABLED
, SWM_S_BAR_BORDER_WIDTH
, SWM_S_STACK_ENABLED
,
4339 SWM_S_CLOCK_ENABLED
, SWM_S_CLOCK_FORMAT
, SWM_S_CYCLE_EMPTY
,
4340 SWM_S_CYCLE_VISIBLE
, SWM_S_SS_ENABLED
, SWM_S_TERM_WIDTH
,
4341 SWM_S_TITLE_CLASS_ENABLED
, SWM_S_TITLE_NAME_ENABLED
, SWM_S_WINDOW_NAME_ENABLED
,
4342 SWM_S_FOCUS_MODE
, SWM_S_DISABLE_BORDER
, SWM_S_BORDER_WIDTH
, SWM_S_BAR_FONT
,
4343 SWM_S_BAR_ACTION
, SWM_S_SPAWN_TERM
, SWM_S_SS_APP
, SWM_S_DIALOG_RATIO
,
4348 setconfvalue(char *selector
, char *value
, int flags
)
4351 case SWM_S_BAR_DELAY
:
4352 bar_delay
= atoi(value
);
4354 case SWM_S_BAR_ENABLED
:
4355 bar_enabled
= atoi(value
);
4357 case SWM_S_BAR_BORDER_WIDTH
:
4358 bar_border_width
= atoi(value
);
4360 case SWM_S_BAR_AT_BOTTOM
:
4361 bar_at_bottom
= atoi(value
);
4363 case SWM_S_STACK_ENABLED
:
4364 stack_enabled
= atoi(value
);
4366 case SWM_S_CLOCK_ENABLED
:
4367 clock_enabled
= atoi(value
);
4369 case SWM_S_CLOCK_FORMAT
:
4370 #ifndef SWM_DENY_CLOCK_FORMAT
4372 if ((clock_format
= strdup(value
)) == NULL
)
4373 err(1, "setconfvalue: clock_format");
4376 case SWM_S_CYCLE_EMPTY
:
4377 cycle_empty
= atoi(value
);
4379 case SWM_S_CYCLE_VISIBLE
:
4380 cycle_visible
= atoi(value
);
4382 case SWM_S_SS_ENABLED
:
4383 ss_enabled
= atoi(value
);
4385 case SWM_S_TERM_WIDTH
:
4386 term_width
= atoi(value
);
4388 case SWM_S_TITLE_CLASS_ENABLED
:
4389 title_class_enabled
= atoi(value
);
4391 case SWM_S_WINDOW_NAME_ENABLED
:
4392 window_name_enabled
= atoi(value
);
4394 case SWM_S_TITLE_NAME_ENABLED
:
4395 title_name_enabled
= atoi(value
);
4397 case SWM_S_FOCUS_MODE
:
4398 if (!strcmp(value
, "default"))
4399 focus_mode
= SWM_FOCUS_DEFAULT
;
4400 else if (!strcmp(value
, "follow_cursor"))
4401 focus_mode
= SWM_FOCUS_FOLLOW
;
4402 else if (!strcmp(value
, "synergy"))
4403 focus_mode
= SWM_FOCUS_SYNERGY
;
4405 err(1, "focus_mode");
4407 case SWM_S_DISABLE_BORDER
:
4408 disable_border
= atoi(value
);
4410 case SWM_S_BORDER_WIDTH
:
4411 border_width
= atoi(value
);
4413 case SWM_S_BAR_FONT
:
4415 if ((bar_fonts
[0] = strdup(value
)) == NULL
)
4416 err(1, "setconfvalue: bar_font");
4418 case SWM_S_BAR_ACTION
:
4420 if ((bar_argv
[0] = strdup(value
)) == NULL
)
4421 err(1, "setconfvalue: bar_action");
4423 case SWM_S_SPAWN_TERM
:
4424 free(spawn_term
[0]);
4425 if ((spawn_term
[0] = strdup(value
)) == NULL
)
4426 err(1, "setconfvalue: spawn_term");
4430 case SWM_S_DIALOG_RATIO
:
4431 dialog_ratio
= atof(value
);
4432 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
4442 setconfmodkey(char *selector
, char *value
, int flags
)
4444 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
4445 update_modkey(Mod1Mask
);
4446 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
4447 update_modkey(Mod2Mask
);
4448 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
4449 update_modkey(Mod3Mask
);
4450 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
4451 update_modkey(Mod4Mask
);
4458 setconfcolor(char *selector
, char *value
, int flags
)
4460 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
4465 setconfregion(char *selector
, char *value
, int flags
)
4467 custom_region(value
);
4471 /* config options */
4472 struct config_option
{
4474 int (*func
)(char*, char*, int);
4477 struct config_option configopt
[] = {
4478 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
4479 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
4480 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
4481 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
4482 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
4483 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
4484 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
4485 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
4486 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
4487 { "bind", setconfbinding
, 0 },
4488 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
4489 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
4490 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
4491 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
4492 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
4493 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
4494 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
4495 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
4496 { "modkey", setconfmodkey
, 0 },
4497 { "program", setconfspawn
, 0 },
4498 { "quirk", setconfquirk
, 0 },
4499 { "region", setconfregion
, 0 },
4500 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
4501 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
4502 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
4503 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
4504 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
4505 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
4506 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
4507 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
4508 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
4509 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
4514 conf_load(char *filename
)
4517 char *line
, *cp
, *optsub
, *optval
;
4518 size_t linelen
, lineno
= 0;
4519 int wordlen
, i
, optind
;
4520 struct config_option
*opt
;
4522 DNPRINTF(SWM_D_CONF
, "conf_load begin\n");
4524 if (filename
== NULL
) {
4525 fprintf(stderr
, "conf_load: no filename\n");
4528 if ((config
= fopen(filename
, "r")) == NULL
) {
4529 warn("conf_load: fopen");
4533 while (!feof(config
)) {
4534 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
4537 err(1, "%s", filename
);
4542 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
4543 if (cp
[0] == '\0') {
4548 /* get config option */
4549 wordlen
= strcspn(cp
, "=[ \t\n");
4551 warnx("%s: line %zd: no option found",
4556 for (i
= 0; i
< LENGTH(configopt
); i
++) {
4557 opt
= &configopt
[i
];
4558 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
4559 strlen(opt
->optname
) == wordlen
) {
4565 warnx("%s: line %zd: unknown option %.*s",
4566 filename
, lineno
, wordlen
, cp
);
4570 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
4571 /* get [selector] if any */
4575 wordlen
= strcspn(cp
, "]");
4578 warnx("%s: line %zd: syntax error",
4582 asprintf(&optsub
, "%.*s", wordlen
, cp
);
4585 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
4587 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
4589 optval
= strdup(cp
);
4590 /* call function to deal with it all */
4591 if (configopt
[optind
].func(optsub
, optval
,
4592 configopt
[optind
].funcflags
) != 0) {
4593 fprintf(stderr
, "%s line %zd: %s\n",
4594 filename
, lineno
, line
);
4595 errx(1, "%s: line %zd: invalid data for %s",
4596 filename
, lineno
, configopt
[optind
].optname
);
4604 DNPRINTF(SWM_D_CONF
, "conf_load end\n");
4610 set_child_transient(struct ws_win
*win
, Window
*trans
)
4612 struct ws_win
*parent
, *w
;
4613 XWMHints
*wmh
= NULL
;
4614 struct swm_region
*r
;
4615 struct workspace
*ws
;
4617 parent
= find_window(win
->transient
);
4619 parent
->child_trans
= win
;
4621 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
4622 " for %lu trans %lu\n", win
->id
, win
->transient
);
4624 if (win
->hints
== NULL
) {
4625 fprintf(stderr
, "no hints for %lu\n", win
->id
);
4629 r
= root_to_region(win
->wa
.root
);
4631 /* parent doen't exist in our window list */
4632 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
4636 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
4637 fprintf(stderr
, "can't get hints for %lu\n",
4642 if (win
->hints
->window_group
!= wmh
->window_group
)
4645 w
->child_trans
= win
;
4646 win
->transient
= w
->id
;
4648 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
4649 "transient to %lu\n", win
->transient
);
4659 manage_window(Window id
)
4662 struct workspace
*ws
;
4663 struct ws_win
*win
, *ww
;
4664 int format
, i
, ws_idx
, n
, border_me
= 0;
4665 unsigned long nitems
, bytes
;
4666 Atom ws_idx_atom
= 0, type
;
4667 Atom
*prot
= NULL
, *pp
;
4668 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
4669 struct swm_region
*r
;
4674 if ((win
= find_window(id
)) != NULL
)
4675 return (win
); /* already being managed */
4677 /* see if we are on the unmanaged list */
4678 if ((win
= find_unmanaged_window(id
)) != NULL
) {
4679 DNPRINTF(SWM_D_MISC
, "manage previously unmanaged window "
4681 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
4682 if (win
->transient
) {
4683 set_child_transient(win
, &trans
);
4684 } if (trans
&& (ww
= find_window(trans
)))
4685 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
4687 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
4688 ewmh_update_actions(win
);
4692 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
4693 errx(1, "calloc: failed to allocate memory for new window");
4697 /* Get all the window data in one shot */
4698 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
4700 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
4701 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
4702 XGetWindowAttributes(display
, id
, &win
->wa
);
4703 XGetWMNormalHints(display
, id
, &win
->sh
, &mask
);
4704 win
->hints
= XGetWMHints(display
, id
);
4705 XGetTransientForHint(display
, id
, &trans
);
4707 win
->transient
= trans
;
4708 set_child_transient(win
, &trans
);
4709 DNPRINTF(SWM_D_MISC
, "manage_window: win %lu transient %lu\n",
4710 win
->id
, win
->transient
);
4713 /* get supported protocols */
4714 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
4715 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
4716 if (*pp
== takefocus
)
4717 win
->take_focus
= 1;
4719 win
->can_delete
= 1;
4725 win
->iconic
= get_iconic(win
);
4728 * Figure out where to put the window. If it was previously assigned to
4729 * a workspace (either by spawn() or manually moving), and isn't
4730 * transient, * put it in the same workspace
4732 r
= root_to_region(win
->wa
.root
);
4733 if (prop
&& win
->transient
== 0) {
4734 DNPRINTF(SWM_D_PROP
, "got property _SWM_WS=%s\n", prop
);
4735 ws_idx
= strtonum(prop
, 0, 9, &errstr
);
4737 DNPRINTF(SWM_D_EVENT
, "window idx is %s: %s",
4740 ws
= &r
->s
->ws
[ws_idx
];
4743 /* this should launch transients in the same ws as parent */
4745 if ((ww
= find_window(trans
)) != NULL
)
4752 "fix this bug mcbride\n");
4757 /* set up the window layout */
4760 win
->s
= r
->s
; /* this never changes */
4761 if (trans
&& (ww
= find_window(trans
)))
4762 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
4764 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
4766 win
->g
.w
= win
->wa
.width
;
4767 win
->g
.h
= win
->wa
.height
;
4768 win
->g
.x
= win
->wa
.x
;
4769 win
->g
.y
= win
->wa
.y
;
4770 win
->g_floatvalid
= 0;
4771 win
->floatmaxed
= 0;
4772 win
->ewmh_flags
= 0;
4774 /* Set window properties so we can remember this after reincarnation */
4775 if (ws_idx_atom
&& prop
== NULL
&&
4776 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) < SWM_PROPLEN
) {
4777 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
4779 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
4780 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
4785 ewmh_autoquirk(win
);
4787 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
4788 DNPRINTF(SWM_D_CLASS
, "class: %s name: %s\n",
4789 win
->ch
.res_class
, win
->ch
.res_name
);
4791 /* java is retarded so treat it special */
4792 if (strstr(win
->ch
.res_name
, "sun-awt")) {
4797 for (i
= 0; i
< quirks_length
; i
++){
4798 if (!strcmp(win
->ch
.res_class
, quirks
[i
].class) &&
4799 !strcmp(win
->ch
.res_name
, quirks
[i
].name
)) {
4800 DNPRINTF(SWM_D_CLASS
, "found: %s name: %s\n",
4801 win
->ch
.res_class
, win
->ch
.res_name
);
4802 if (quirks
[i
].quirk
& SWM_Q_FLOAT
) {
4806 win
->quirks
= quirks
[i
].quirk
;
4811 /* alter window position if quirky */
4812 if (win
->quirks
& SWM_Q_ANYWHERE
) {
4813 win
->manual
= 1; /* don't center the quirky windows */
4814 bzero(&wc
, sizeof wc
);
4816 if (bar_enabled
&& win
->g
.y
< bar_height
) {
4817 win
->g
.y
= wc
.y
= bar_height
;
4820 if (win
->g
.w
+ win
->g
.x
> WIDTH(r
)) {
4821 win
->g
.x
= wc
.x
= WIDTH(r
) - win
->g
.w
- 2;
4827 /* Reset font sizes (the bruteforce way; no default keybinding). */
4828 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
4829 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
4830 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
4831 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
4832 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
4835 ewmh_get_win_state(win
);
4836 ewmh_update_actions(win
);
4837 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
4841 bzero(&wc
, sizeof wc
);
4842 wc
.border_width
= border_width
;
4843 mask
= CWBorderWidth
;
4844 XConfigureWindow(display
, win
->id
, mask
, &wc
);
4847 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
4848 PropertyChangeMask
| StructureNotifyMask
);
4850 /* floaters need to be mapped if they are in the current workspace */
4851 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
4852 XMapRaised(display
, win
->id
);
4858 free_window(struct ws_win
*win
)
4860 DNPRINTF(SWM_D_MISC
, "free_window: %lu\n", win
->id
);
4865 /* needed for restart wm */
4866 set_win_state(win
, WithdrawnState
);
4868 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
4870 if (win
->ch
.res_class
)
4871 XFree(win
->ch
.res_class
);
4872 if (win
->ch
.res_name
)
4873 XFree(win
->ch
.res_name
);
4878 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
4884 unmanage_window(struct ws_win
*win
)
4886 struct ws_win
*parent
;
4891 DNPRINTF(SWM_D_MISC
, "unmanage_window: %lu\n", win
->id
);
4893 if (win
->transient
) {
4894 parent
= find_window(win
->transient
);
4896 parent
->child_trans
= NULL
;
4899 /* focus on root just in case */
4900 XSetInputFocus(display
, PointerRoot
, PointerRoot
, CurrentTime
);
4904 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
4905 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
4911 focus_magic(struct ws_win
*win
)
4913 DNPRINTF(SWM_D_FOCUS
, "focus_magic: %lu\n", WINID(win
));
4918 if (win
->child_trans
) {
4919 /* win = parent & has a transient so focus on that */
4921 focus_win(win
->child_trans
);
4922 if (win
->child_trans
->take_focus
)
4923 client_msg(win
, takefocus
);
4925 /* make sure transient hasn't dissapeared */
4926 if (validate_win(win
->child_trans
) == 0) {
4927 focus_win(win
->child_trans
);
4928 if (win
->child_trans
->take_focus
)
4929 client_msg(win
->child_trans
, takefocus
);
4931 win
->child_trans
= NULL
;
4933 if (win
->take_focus
)
4934 client_msg(win
, takefocus
);
4940 if (win
->take_focus
)
4941 client_msg(win
, takefocus
);
4948 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
4956 XKeyEvent
*ev
= &e
->xkey
;
4958 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
4960 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
4961 for (i
= 0; i
< keys_length
; i
++)
4962 if (keysym
== keys
[i
].keysym
4963 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
4964 && keyfuncs
[keys
[i
].funcid
].func
) {
4965 if (keys
[i
].funcid
== kf_spawn_custom
)
4967 root_to_region(ev
->root
),
4968 &(keyfuncs
[keys
[i
].funcid
].args
),
4972 keyfuncs
[keys
[i
].funcid
].func(
4973 root_to_region(ev
->root
),
4974 &(keyfuncs
[keys
[i
].funcid
].args
)
4980 buttonpress(XEvent
*e
)
4984 XButtonPressedEvent
*ev
= &e
->xbutton
;
4986 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
4988 action
= root_click
;
4989 if ((win
= find_window(ev
->window
)) == NULL
)
4993 action
= client_click
;
4995 for (i
= 0; i
< LENGTH(buttons
); i
++)
4996 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
4997 buttons
[i
].button
== ev
->button
&&
4998 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
4999 buttons
[i
].func(win
, &buttons
[i
].args
);
5003 configurerequest(XEvent
*e
)
5005 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
5010 if ((win
= find_window(ev
->window
)) == NULL
)
5011 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
5015 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
5017 bzero(&wc
, sizeof wc
);
5020 wc
.width
= ev
->width
;
5021 wc
.height
= ev
->height
;
5022 wc
.border_width
= ev
->border_width
;
5023 wc
.sibling
= ev
->above
;
5024 wc
.stack_mode
= ev
->detail
;
5025 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
5027 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
5029 config_win(win
, ev
);
5034 configurenotify(XEvent
*e
)
5039 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
5040 e
->xconfigure
.window
);
5042 win
= find_window(e
->xconfigure
.window
);
5044 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
5048 if (focus_mode
== SWM_FOCUS_DEFAULT
)
5049 drain_enter_notify();
5054 destroynotify(XEvent
*e
)
5057 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
5059 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
5061 if ((win
= find_window(ev
->window
)) == NULL
) {
5062 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
5068 /* make sure we focus on something */
5071 unmanage_window(win
);
5073 if (focus_mode
== SWM_FOCUS_DEFAULT
)
5074 drain_enter_notify();
5079 enternotify(XEvent
*e
)
5081 XCrossingEvent
*ev
= &e
->xcrossing
;
5086 Window focus_return
;
5087 int revert_to_return
;
5089 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: %lu mode %d detail %d root "
5090 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5091 ev
->window
, ev
->mode
, ev
->detail
, ev
->root
, ev
->subwindow
,
5092 ev
->same_screen
, ev
->focus
, ev
->state
);
5094 switch (focus_mode
) {
5095 case SWM_FOCUS_DEFAULT
:
5097 case SWM_FOCUS_FOLLOW
:
5099 case SWM_FOCUS_SYNERGY
:
5102 * all these checks need to be in this order because the
5103 * XCheckTypedWindowEvent relies on weeding out the previous events
5105 * making this code an option would enable a follow mouse for focus
5110 * state is set when we are switching workspaces and focus is set when
5111 * the window or a subwindow already has focus (occurs during restart).
5113 * Only honor the focus flag if last_focus_event is not FocusOut,
5114 * this allows scrotwm to continue to control focus when another
5115 * program is also playing with it.
5117 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
5118 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
5123 * happens when a window is created or destroyed and the border
5124 * crosses the mouse pointer and when switching ws
5126 * we need the subwindow test to see if we came from root in order
5127 * to give focus to floaters
5129 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
5130 ev
->subwindow
== 0) {
5131 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
5135 /* this window already has focus */
5136 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
5137 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
5141 /* this window is being deleted or moved to another ws */
5142 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
5144 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
5145 XPutBackEvent(display
, &cne
);
5149 if ((win
= find_window(ev
->window
)) == NULL
) {
5150 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
5155 * In fullstack kill all enters unless they come from a different ws
5156 * (i.e. another region) or focus has been grabbed externally.
5158 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
5159 last_focus_event
!= FocusOut
) {
5160 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
5161 if ((w
= find_window(focus_return
)) == NULL
||
5163 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
5171 if ((win
= find_window(ev
->window
)) == NULL
) {
5172 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
5177 * if we have more enternotifies let them handle it in due time
5179 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
5180 DNPRINTF(SWM_D_EVENT
,
5181 "ignoring enternotify: got more enternotify\n");
5182 XPutBackEvent(display
, &cne
);
5189 /* lets us use one switch statement for arbitrary mode/detail combinations */
5190 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
5193 focusevent(XEvent
*e
)
5197 u_int32_t mode_detail
;
5198 XFocusChangeEvent
*ev
= &e
->xfocus
;
5200 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: %lu mode %d detail %d\n",
5201 ev
->type
== FocusIn
? "entering" : "leaving",
5202 ev
->window
, ev
->mode
, ev
->detail
);
5204 if (last_focus_event
== ev
->type
) {
5205 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
5209 last_focus_event
= ev
->type
;
5210 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
5212 switch (mode_detail
) {
5213 /* synergy client focus operations */
5214 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
5215 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
5217 /* synergy server focus operations */
5218 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
5220 /* Entering applications like rdesktop that mangle the pointer */
5221 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
5223 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
5224 XSetWindowBorder(display
, win
->id
,
5225 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
5226 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
5229 fprintf(stderr
, "ignoring focusevent\n");
5230 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
5237 mapnotify(XEvent
*e
)
5240 XMapEvent
*ev
= &e
->xmap
;
5242 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: %lu\n", ev
->window
);
5244 win
= manage_window(ev
->window
);
5246 set_win_state(win
, NormalState
);
5250 mappingnotify(XEvent
*e
)
5252 XMappingEvent
*ev
= &e
->xmapping
;
5254 XRefreshKeyboardMapping(ev
);
5255 if (ev
->request
== MappingKeyboard
)
5260 maprequest(XEvent
*e
)
5263 struct swm_region
*r
;
5264 XWindowAttributes wa
;
5265 XMapRequestEvent
*ev
= &e
->xmaprequest
;
5267 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
5268 e
->xmaprequest
.window
);
5270 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
5272 if (wa
.override_redirect
)
5275 win
= manage_window(e
->xmaprequest
.window
);
5277 return; /* can't happen */
5281 /* make new win focused */
5282 r
= root_to_region(win
->wa
.root
);
5283 if (win
->ws
== r
->ws
)
5288 propertynotify(XEvent
*e
)
5291 XPropertyEvent
*ev
= &e
->xproperty
;
5293 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
5296 win
= find_window(ev
->window
);
5300 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
5301 update_iconic(win
, 0);
5302 XMapRaised(display
, win
->id
);
5309 case XA_WM_NORMAL_HINTS
:
5312 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
5313 fprintf(stderr
, "normal hints: flag 0x%x\n", win
->sh
.flags
);
5314 if (win
->sh
.flags
& PMinSize
) {
5315 win
->g
.w
= win
->sh
.min_width
;
5316 win
->g
.h
= win
->sh
.min_height
;
5317 fprintf(stderr
, "min %d %d\n", win
->g
.w
, win
->g
.h
);
5319 XMoveResizeWindow(display
, win
->id
,
5320 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
5322 if (window_name_enabled
)
5331 unmapnotify(XEvent
*e
)
5335 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
5337 /* determine if we need to help unmanage this window */
5338 win
= find_window(e
->xunmap
.window
);
5342 if (getstate(e
->xunmap
.window
) == NormalState
) {
5343 unmanage_window(win
);
5346 /* giant hack for apps that don't destroy transient windows */
5347 /* eat a bunch of events to prevent remanaging the window */
5349 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
5350 EnterWindowMask
, &cne
))
5352 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
5353 StructureNotifyMask
, &cne
))
5355 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
5356 SubstructureNotifyMask
, &cne
))
5358 /* resend unmap because we ated it */
5359 XUnmapWindow(display
, e
->xunmap
.window
);
5364 visibilitynotify(XEvent
*e
)
5367 struct swm_region
*r
;
5369 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
5370 e
->xvisibility
.window
);
5371 if (e
->xvisibility
.state
== VisibilityUnobscured
)
5372 for (i
= 0; i
< ScreenCount(display
); i
++)
5373 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
5374 if (e
->xvisibility
.window
== r
->bar_window
)
5379 clientmessage(XEvent
*e
)
5381 XClientMessageEvent
*ev
;
5386 win
= find_window(ev
->window
);
5390 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx type: %ld \n",
5391 ev
->window
, ev
->message_type
);
5393 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
5394 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW \n");
5397 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
5398 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW \n");
5399 if (win
->can_delete
)
5400 client_msg(win
, adelete
);
5402 XKillClient(display
, win
->id
);
5404 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
5405 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_MOVERESIZE_WINDOW \n");
5406 if (win
->floating
) {
5407 if (ev
->data
.l
[0] & (1<<8)) /* x */
5408 win
->g
.x
= ev
->data
.l
[1];
5409 if (ev
->data
.l
[0] & (1<<9)) /* y */
5410 win
->g
.y
= ev
->data
.l
[2];
5411 if (ev
->data
.l
[0] & (1<<10)) /* width */
5412 win
->g
.w
= ev
->data
.l
[3];
5413 if (ev
->data
.l
[0] & (1<<11)) /* height */
5414 win
->g
.h
= ev
->data
.l
[4];
5417 /* TODO: Change stack sizes */
5419 config_win(win
, NULL
);
5421 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
5422 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE \n");
5423 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
5425 ewmh_update_win_state(win
, ev
->data
.l
[2], ev
->data
.l
[0]);
5432 xerror_start(Display
*d
, XErrorEvent
*ee
)
5439 xerror(Display
*d
, XErrorEvent
*ee
)
5441 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5449 xerrorxlib
= XSetErrorHandler(xerror_start
);
5451 /* this causes an error if some other window manager is running */
5452 XSelectInput(display
, DefaultRootWindow(display
),
5453 SubstructureRedirectMask
);
5454 XSync(display
, False
);
5458 XSetErrorHandler(xerror
);
5459 XSync(display
, False
);
5464 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
5466 struct swm_region
*r
, *n
;
5467 struct workspace
*ws
= NULL
;
5470 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
5471 s
->idx
, w
, h
, x
, y
);
5473 /* remove any conflicting regions */
5474 n
= TAILQ_FIRST(&s
->rl
);
5477 n
= TAILQ_NEXT(r
, entry
);
5478 if (X(r
) < (x
+ w
) &&
5479 (X(r
) + WIDTH(r
)) > x
&&
5481 (Y(r
) + HEIGHT(r
)) > y
) {
5482 if (r
->ws
->r
!= NULL
)
5483 r
->ws
->old_r
= r
->ws
->r
;
5485 XDestroyWindow(display
, r
->bar_window
);
5486 TAILQ_REMOVE(&s
->rl
, r
, entry
);
5487 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
5491 /* search old regions for one to reuse */
5493 /* size + location match */
5494 TAILQ_FOREACH(r
, &s
->orl
, entry
)
5495 if (X(r
) == x
&& Y(r
) == y
&&
5496 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
5500 TAILQ_FOREACH(r
, &s
->orl
, entry
)
5501 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
5505 TAILQ_REMOVE(&s
->orl
, r
, entry
);
5506 /* try to use old region's workspace */
5507 if (r
->ws
->r
== NULL
)
5510 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
5511 errx(1, "calloc: failed to allocate memory for screen");
5513 /* if we don't have a workspace already, find one */
5515 for (i
= 0; i
< SWM_WS_MAX
; i
++)
5516 if (s
->ws
[i
].r
== NULL
) {
5523 errx(1, "no free workspaces\n");
5534 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
5540 #ifdef SWM_XRR_HAS_CRTC
5542 XRRScreenResources
*sr
;
5545 #endif /* SWM_XRR_HAS_CRTC */
5546 struct swm_region
*r
;
5549 if (i
>= ScreenCount(display
))
5550 errx(1, "invalid screen");
5552 /* remove any old regions */
5553 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
5554 r
->ws
->old_r
= r
->ws
->r
= NULL
;
5555 XDestroyWindow(display
, r
->bar_window
);
5556 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
5557 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
5561 /* map virtual screens onto physical screens */
5562 #ifdef SWM_XRR_HAS_CRTC
5563 if (xrandr_support
) {
5564 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
5566 new_region(&screens
[i
], 0, 0,
5567 DisplayWidth(display
, i
),
5568 DisplayHeight(display
, i
));
5572 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
5573 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
5574 if (ci
->noutput
== 0)
5577 if (ci
!= NULL
&& ci
->mode
== None
)
5578 new_region(&screens
[i
], 0, 0,
5579 DisplayWidth(display
, i
),
5580 DisplayHeight(display
, i
));
5582 new_region(&screens
[i
],
5583 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
5586 XRRFreeCrtcInfo(ci
);
5587 XRRFreeScreenResources(sr
);
5589 #endif /* SWM_XRR_HAS_CRTC */
5591 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
5592 DisplayHeight(display
, i
));
5597 screenchange(XEvent
*e
) {
5598 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
5599 struct swm_region
*r
;
5602 DNPRINTF(SWM_D_EVENT
, "screenchange: %lu\n", xe
->root
);
5604 if (!XRRUpdateConfiguration(e
))
5607 /* silly event doesn't include the screen index */
5608 for (i
= 0; i
< ScreenCount(display
); i
++)
5609 if (screens
[i
].root
== xe
->root
)
5611 if (i
>= ScreenCount(display
))
5612 errx(1, "screenchange: screen not found\n");
5614 /* brute force for now, just re-enumerate the regions */
5617 /* add bars to all regions */
5618 for (i
= 0; i
< ScreenCount(display
); i
++)
5619 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
5622 if (focus_mode
== SWM_FOCUS_DEFAULT
)
5623 drain_enter_notify();
5629 Window d1
, d2
, *wins
= NULL
;
5630 XWindowAttributes wa
;
5635 for (i
= 0; i
< ScreenCount(display
); i
++) {
5636 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
5639 /* attach windows to a region */
5640 /* normal windows */
5641 for (j
= 0; j
< no
; j
++) {
5642 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
5643 wa
.override_redirect
||
5644 XGetTransientForHint(display
, wins
[j
], &d1
))
5647 state
= getstate(wins
[j
]);
5648 manage
= state
== IconicState
;
5649 if (wa
.map_state
== IsViewable
|| manage
)
5650 manage_window(wins
[j
]);
5652 /* transient windows */
5653 for (j
= 0; j
< no
; j
++) {
5654 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
5655 wa
.override_redirect
)
5658 state
= getstate(wins
[j
]);
5659 manage
= state
== IconicState
;
5660 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
5662 manage_window(wins
[j
]);
5675 int errorbase
, major
, minor
;
5676 struct workspace
*ws
;
5679 if ((screens
= calloc(ScreenCount(display
),
5680 sizeof(struct swm_screen
))) == NULL
)
5681 errx(1, "calloc: screens");
5683 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
5685 /* initial Xrandr setup */
5686 xrandr_support
= XRRQueryExtension(display
,
5687 &xrandr_eventbase
, &errorbase
);
5689 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
5692 /* map physical screens */
5693 for (i
= 0; i
< ScreenCount(display
); i
++) {
5694 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
5696 TAILQ_INIT(&screens
[i
].rl
);
5697 TAILQ_INIT(&screens
[i
].orl
);
5698 screens
[i
].root
= RootWindow(display
, i
);
5700 /* set default colors */
5701 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
5702 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
5703 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
5704 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
5705 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
5707 /* set default cursor */
5708 XDefineCursor(display
, screens
[i
].root
,
5709 XCreateFontCursor(display
, XC_left_ptr
));
5711 /* init all workspaces */
5712 /* XXX these should be dynamically allocated too */
5713 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
5714 ws
= &screens
[i
].ws
[j
];
5719 TAILQ_INIT(&ws
->winlist
);
5720 TAILQ_INIT(&ws
->unmanagedlist
);
5722 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
5723 if (layouts
[k
].l_config
!= NULL
)
5724 layouts
[k
].l_config(ws
,
5725 SWM_ARG_ID_STACKINIT
);
5726 ws
->cur_layout
= &layouts
[0];
5732 XRRSelectInput(display
, screens
[i
].root
,
5733 RRScreenChangeNotifyMask
);
5740 if ((bar_fonts
[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
5742 err(1, "setup_globals: strdup");
5743 if ((bar_fonts
[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
5745 err(1, "setup_globals: strdup");
5746 if ((bar_fonts
[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
5748 err(1, "setup_globals: strdup");
5749 if ((spawn_term
[0] = strdup("xterm")) == NULL
)
5750 err(1, "setup_globals: strdup");
5751 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
5759 Atom netwmcheck
, netwmname
, utf8_string
;
5762 /* work around sun jdk bugs, code from wmname */
5763 netwmcheck
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
5764 netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
5765 utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
5766 for (i
= 0; i
< ScreenCount(display
); i
++) {
5767 root
= screens
[i
].root
;
5768 win
= XCreateSimpleWindow(display
,root
, 0, 0, 1, 1, 0,
5769 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
,
5770 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
);
5772 XChangeProperty(display
, root
, netwmcheck
, XA_WINDOW
, 32,
5773 PropModeReplace
, (unsigned char *)&win
,1);
5774 XChangeProperty(display
, win
, netwmcheck
, XA_WINDOW
, 32,
5775 PropModeReplace
, (unsigned char *)&win
,1);
5776 XChangeProperty(display
, win
, netwmname
, utf8_string
, 8,
5777 PropModeReplace
, (unsigned char*)"LG3D", strlen("LG3D"));
5782 main(int argc
, char *argv
[])
5785 struct swm_region
*r
, *rr
;
5786 struct ws_win
*winfocus
= NULL
;
5789 char conf
[PATH_MAX
], *cfile
= NULL
;
5794 struct sigaction sact
;
5797 fprintf(stderr
, "Welcome to scrotwm V%s cvs tag: %s\n",
5798 SWM_VERSION
, cvstag
);
5799 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
5800 warnx("no locale support");
5802 if (!(display
= XOpenDisplay(0)))
5803 errx(1, "can not open display");
5806 errx(1, "other wm running");
5808 /* handle some signals */
5809 bzero(&sact
, sizeof(sact
));
5810 sigemptyset(&sact
.sa_mask
);
5812 sact
.sa_handler
= sighdlr
;
5813 sigaction(SIGINT
, &sact
, NULL
);
5814 sigaction(SIGQUIT
, &sact
, NULL
);
5815 sigaction(SIGTERM
, &sact
, NULL
);
5816 sigaction(SIGHUP
, &sact
, NULL
);
5818 sact
.sa_handler
= sighdlr
;
5819 sact
.sa_flags
= SA_NOCLDSTOP
;
5820 sigaction(SIGCHLD
, &sact
, NULL
);
5822 astate
= XInternAtom(display
, "WM_STATE", False
);
5823 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
5824 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
5825 takefocus
= XInternAtom(display
, "WM_TAKE_FOCUS", False
);
5826 a_wmname
= XInternAtom(display
, "WM_NAME", False
);
5827 a_utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
5828 a_string
= XInternAtom(display
, "STRING", False
);
5829 a_swm_iconic
= XInternAtom(display
, "_SWM_ICONIC", False
);
5831 /* look for local and global conf file */
5832 pwd
= getpwuid(getuid());
5834 errx(1, "invalid user %d", getuid());
5843 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
5844 if (stat(conf
, &sb
) != -1) {
5845 if (S_ISREG(sb
.st_mode
))
5848 /* try global conf file */
5849 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
5850 if (!stat(conf
, &sb
))
5851 if (S_ISREG(sb
.st_mode
))
5858 /* set some values to work around bad programs */
5861 /* grab existing windows (before we build the bars) */
5864 /* setup all bars */
5865 for (i
= 0; i
< ScreenCount(display
); i
++)
5866 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
5867 if (winfocus
== NULL
)
5868 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
5876 if (focus_mode
== SWM_FOCUS_DEFAULT
)
5877 drain_enter_notify();
5879 xfd
= ConnectionNumber(display
);
5881 while (XPending(display
)) {
5882 XNextEvent(display
, &e
);
5885 if (e
.type
< LASTEvent
) {
5887 if (handler
[e
.type
])
5888 handler
[e
.type
](&e
);
5890 DNPRINTF(SWM_D_EVENT
,
5891 "win: %lu unknown event: %d\n",
5892 e
.xany
.window
, e
.type
);
5894 switch (e
.type
- xrandr_eventbase
) {
5895 case RRScreenChangeNotify
:
5899 DNPRINTF(SWM_D_EVENT
,
5900 "win: %lu unknown xrandr event: "
5901 "%d\n", e
.xany
.window
, e
.type
);
5907 /* if we are being restarted go focus on first window */
5909 rr
= winfocus
->ws
->r
;
5911 /* not a visible window */
5915 /* move pointer to first screen if multi screen */
5916 if (ScreenCount(display
) > 1 || outputs
> 1)
5917 XWarpPointer(display
, None
, rr
->s
[0].root
,
5918 0, 0, 0, 0, rr
->g
.x
,
5919 rr
->g
.y
+ (bar_enabled
? bar_height
: 0));
5921 a
.id
= SWM_ARG_ID_FOCUSCUR
;
5931 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
5933 DNPRINTF(SWM_D_MISC
, "select failed");
5934 if (restart_wm
== 1)
5935 restart(NULL
, NULL
);
5936 if (search_resp
== 1)
5948 XFreeGC(display
, bar_gc
);
5949 XCloseDisplay(display
);