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) 2010 Tuukka Kataja <stuge@xor.fi>
8 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Much code and ideas taken from dwm under the following license:
24 * MIT/X Consortium License
26 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
27 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
28 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
29 * 2007 Premysl Hruby <dfenze at gmail dot com>
30 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
31 * 2007 Christof Musik <christof at sendfax dot de>
32 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
33 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
34 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
36 * Permission is hereby granted, free of charge, to any person obtaining a
37 * copy of this software and associated documentation files (the "Software"),
38 * to deal in the Software without restriction, including without limitation
39 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
40 * and/or sell copies of the Software, and to permit persons to whom the
41 * Software is furnished to do so, subject to the following conditions:
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
49 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
51 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
52 * DEALINGS IN THE SOFTWARE.
55 static const char *cvstag
=
58 #define SWM_VERSION "0.9.34"
75 #include <sys/types.h>
79 #include <sys/queue.h>
80 #include <sys/param.h>
81 #include <sys/select.h>
83 #include <X11/cursorfont.h>
84 #include <X11/keysym.h>
85 #include <X11/Xatom.h>
87 #include <X11/Xproto.h>
88 #include <X11/Xutil.h>
89 #include <X11/extensions/Xrandr.h>
90 #include <X11/extensions/XTest.h>
97 # error XRandR versions less than 1.0 are not supported
102 #define SWM_XRR_HAS_CRTC
106 /*#define SWM_DEBUG*/
108 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
109 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
110 #define SWM_D_MISC 0x0001
111 #define SWM_D_EVENT 0x0002
112 #define SWM_D_WS 0x0004
113 #define SWM_D_FOCUS 0x0008
114 #define SWM_D_MOVE 0x0010
115 #define SWM_D_STACK 0x0020
116 #define SWM_D_MOUSE 0x0040
117 #define SWM_D_PROP 0x0080
118 #define SWM_D_CLASS 0x0100
119 #define SWM_D_KEY 0x0200
120 #define SWM_D_QUIRK 0x0400
121 #define SWM_D_SPAWN 0x0800
122 #define SWM_D_EVENTQ 0x1000
123 #define SWM_D_CONF 0x2000
125 u_int32_t swm_debug
= 0
142 #define DPRINTF(x...)
143 #define DNPRINTF(n,x...)
146 #define LENGTH(x) (sizeof x / sizeof x[0])
147 #define MODKEY Mod1Mask
148 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
149 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
150 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
151 #define SWM_PROPLEN (16)
152 #define SWM_FUNCNAME_LEN (32)
153 #define SWM_KEYS_LEN (255)
154 #define SWM_QUIRK_LEN (64)
155 #define X(r) (r)->g.x
156 #define Y(r) (r)->g.y
157 #define WIDTH(r) (r)->g.w
158 #define HEIGHT(r) (r)->g.h
159 #define SWM_MAX_FONT_STEPS (3)
160 #define WINID(w) (w ? w->id : 0)
162 #define SWM_FOCUS_DEFAULT (0)
163 #define SWM_FOCUS_SYNERGY (1)
164 #define SWM_FOCUS_FOLLOW (2)
167 #define SWM_LIB "/usr/local/lib/libswmhack.so"
179 volatile sig_atomic_t running
= 1;
180 volatile sig_atomic_t restart_wm
= 0;
182 int last_focus_event
= FocusOut
;
183 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
187 int xrandr_eventbase
;
188 unsigned int numlockmask
= 0;
192 int cycle_visible
= 0;
194 int font_adjusted
= 0;
195 unsigned int mod_key
= MODKEY
;
198 struct swm_region
*search_r
;
199 int select_list_pipe
[2];
200 int select_resp_pipe
[2];
202 volatile sig_atomic_t search_resp
;
205 double dialog_ratio
= .6;
207 #define SWM_BAR_MAX (256)
208 char *bar_argv
[] = { NULL
, NULL
};
210 char bar_ext
[SWM_BAR_MAX
];
211 char bar_vertext
[SWM_BAR_MAX
];
213 sig_atomic_t bar_alarm
= 0;
216 int bar_border_width
= 1;
217 int bar_at_bottom
= 0;
219 int bar_extra_running
= 0;
222 int stack_enabled
= 1;
223 int clock_enabled
= 1;
224 int urgent_enabled
= 0;
225 char *clock_format
= NULL
;
226 int title_name_enabled
= 0;
227 int title_class_enabled
= 0;
228 int window_name_enabled
= 0;
229 int focus_mode
= SWM_FOCUS_DEFAULT
;
230 int disable_border
= 0;
231 int border_width
= 1;
232 int verbose_layout
= 0;
238 char *bar_fonts
[] = { NULL
, NULL
, NULL
, NULL
};/* XXX Make fully dynamic */
239 char *spawn_term
[] = { NULL
, NULL
}; /* XXX Make fully dynamic */
241 #define SWM_MENU_FN (2)
242 #define SWM_MENU_NB (4)
243 #define SWM_MENU_NF (6)
244 #define SWM_MENU_SB (8)
245 #define SWM_MENU_SF (10)
247 /* layout manager data */
248 struct swm_geometry
{
258 /* virtual "screens" */
260 TAILQ_ENTRY(swm_region
) entry
;
261 struct swm_geometry g
;
262 struct workspace
*ws
; /* current workspace on this region */
263 struct workspace
*ws_prior
; /* prior workspace on this region */
264 struct swm_screen
*s
; /* screen idx */
267 TAILQ_HEAD(swm_region_list
, swm_region
);
270 TAILQ_ENTRY(ws_win
) entry
;
273 struct ws_win
*child_trans
; /* transient child window */
274 struct swm_geometry g
; /* current geometry */
275 struct swm_geometry g_float
; /* geometry when floating */
276 struct swm_geometry rg_float
; /* region geom when floating */
277 int g_floatvalid
; /* flag: geometry in g_float is valid */
278 int floatmaxed
; /* flag: floater was maxed in max_stack */
282 unsigned int ewmh_flags
;
283 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
289 unsigned long quirks
;
290 struct workspace
*ws
; /* always valid */
291 struct swm_screen
*s
; /* always valid, never changes */
292 XWindowAttributes wa
;
297 TAILQ_HEAD(ws_win_list
, ws_win
);
301 TAILQ_ENTRY(pid_e
) entry
;
305 TAILQ_HEAD(pid_list
, pid_e
);
306 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
308 /* layout handlers */
310 void vertical_config(struct workspace
*, int);
311 void vertical_stack(struct workspace
*, struct swm_geometry
*);
312 void horizontal_config(struct workspace
*, int);
313 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
314 void max_stack(struct workspace
*, struct swm_geometry
*);
315 void plain_stacker(struct workspace
*);
316 void fancy_stacker(struct workspace
*);
318 struct ws_win
*find_window(Window
);
320 void grabbuttons(struct ws_win
*, int);
321 void new_region(struct swm_screen
*, int, int, int, int);
322 void unmanage_window(struct ws_win
*);
323 long getstate(Window
);
326 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
327 void (*l_config
)(struct workspace
*, int);
329 #define SWM_L_FOCUSPREV (1<<0)
330 #define SWM_L_MAPONFOCUS (1<<1)
331 void (*l_string
)(struct workspace
*);
333 /* stack, configure */
334 { vertical_stack
, vertical_config
, 0, plain_stacker
},
335 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
337 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
338 { NULL
, NULL
, 0, NULL
},
341 /* position of max_stack mode in the layouts array, index into layouts! */
342 #define SWM_V_STACK (0)
343 #define SWM_H_STACK (1)
344 #define SWM_MAX_STACK (2)
346 #define SWM_H_SLICE (32)
347 #define SWM_V_SLICE (32)
349 /* define work spaces */
351 int idx
; /* workspace index */
352 int always_raise
; /* raise windows on focus */
353 struct layout
*cur_layout
; /* current layout handlers */
354 struct ws_win
*focus
; /* may be NULL */
355 struct ws_win
*focus_prev
; /* may be NULL */
356 struct swm_region
*r
; /* may be NULL */
357 struct swm_region
*old_r
; /* may be NULL */
358 struct ws_win_list winlist
; /* list of windows in ws */
359 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
360 char stacker
[10]; /* display stacker and layout */
364 int horizontal_msize
;
366 int horizontal_stacks
;
373 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
374 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
376 /* physical screen mapping */
377 #define SWM_WS_MAX (10)
379 int idx
; /* screen index */
380 struct swm_region_list rl
; /* list of regions on this screen */
381 struct swm_region_list orl
; /* list of old regions */
383 struct workspace ws
[SWM_WS_MAX
];
389 } c
[SWM_S_COLOR_MAX
];
391 struct swm_screen
*screens
;
394 /* args to functions */
397 #define SWM_ARG_ID_FOCUSNEXT (0)
398 #define SWM_ARG_ID_FOCUSPREV (1)
399 #define SWM_ARG_ID_FOCUSMAIN (2)
400 #define SWM_ARG_ID_FOCUSCUR (4)
401 #define SWM_ARG_ID_SWAPNEXT (10)
402 #define SWM_ARG_ID_SWAPPREV (11)
403 #define SWM_ARG_ID_SWAPMAIN (12)
404 #define SWM_ARG_ID_MOVELAST (13)
405 #define SWM_ARG_ID_MASTERSHRINK (20)
406 #define SWM_ARG_ID_MASTERGROW (21)
407 #define SWM_ARG_ID_MASTERADD (22)
408 #define SWM_ARG_ID_MASTERDEL (23)
409 #define SWM_ARG_ID_STACKRESET (30)
410 #define SWM_ARG_ID_STACKINIT (31)
411 #define SWM_ARG_ID_CYCLEWS_UP (40)
412 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
413 #define SWM_ARG_ID_CYCLESC_UP (42)
414 #define SWM_ARG_ID_CYCLESC_DOWN (43)
415 #define SWM_ARG_ID_STACKINC (50)
416 #define SWM_ARG_ID_STACKDEC (51)
417 #define SWM_ARG_ID_SS_ALL (60)
418 #define SWM_ARG_ID_SS_WINDOW (61)
419 #define SWM_ARG_ID_DONTCENTER (70)
420 #define SWM_ARG_ID_CENTER (71)
421 #define SWM_ARG_ID_KILLWINDOW (80)
422 #define SWM_ARG_ID_DELETEWINDOW (81)
423 #define SWM_ARG_ID_WIDTHGROW (90)
424 #define SWM_ARG_ID_WIDTHSHRINK (91)
425 #define SWM_ARG_ID_HEIGHTGROW (92)
426 #define SWM_ARG_ID_HEIGHTSHRINK (93)
427 #define SWM_ARG_ID_MOVEUP (100)
428 #define SWM_ARG_ID_MOVEDOWN (101)
429 #define SWM_ARG_ID_MOVELEFT (102)
430 #define SWM_ARG_ID_MOVERIGHT (103)
434 void focus(struct swm_region
*, union arg
*);
435 void focus_magic(struct ws_win
*);
442 #define SWM_Q_FLOAT (1<<0) /* float this window */
443 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
444 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
445 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
446 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
447 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
449 int quirks_size
= 0, quirks_length
= 0;
450 struct quirk
*quirks
= NULL
;
453 * Supported EWMH hints should be added to
454 * both the enum and the ewmh array
456 enum { _NET_ACTIVE_WINDOW
, _NET_MOVERESIZE_WINDOW
, _NET_CLOSE_WINDOW
,
457 _NET_WM_WINDOW_TYPE
, _NET_WM_WINDOW_TYPE_DOCK
,
458 _NET_WM_WINDOW_TYPE_TOOLBAR
, _NET_WM_WINDOW_TYPE_UTILITY
,
459 _NET_WM_WINDOW_TYPE_SPLASH
, _NET_WM_WINDOW_TYPE_DIALOG
,
460 _NET_WM_WINDOW_TYPE_NORMAL
, _NET_WM_STATE
,
461 _NET_WM_STATE_MAXIMIZED_HORZ
, _NET_WM_STATE_MAXIMIZED_VERT
,
462 _NET_WM_STATE_SKIP_TASKBAR
, _NET_WM_STATE_SKIP_PAGER
,
463 _NET_WM_STATE_HIDDEN
, _NET_WM_STATE_ABOVE
, _SWM_WM_STATE_MANUAL
,
464 _NET_WM_STATE_FULLSCREEN
, _NET_WM_ALLOWED_ACTIONS
, _NET_WM_ACTION_MOVE
,
465 _NET_WM_ACTION_RESIZE
, _NET_WM_ACTION_FULLSCREEN
, _NET_WM_ACTION_CLOSE
,
471 } ewmh
[SWM_EWMH_HINT_MAX
] = {
472 /* must be in same order as in the enum */
473 {"_NET_ACTIVE_WINDOW", None
},
474 {"_NET_MOVERESIZE_WINDOW", None
},
475 {"_NET_CLOSE_WINDOW", None
},
476 {"_NET_WM_WINDOW_TYPE", None
},
477 {"_NET_WM_WINDOW_TYPE_DOCK", None
},
478 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None
},
479 {"_NET_WM_WINDOW_TYPE_UTILITY", None
},
480 {"_NET_WM_WINDOW_TYPE_SPLASH", None
},
481 {"_NET_WM_WINDOW_TYPE_DIALOG", None
},
482 {"_NET_WM_WINDOW_TYPE_NORMAL", None
},
483 {"_NET_WM_STATE", None
},
484 {"_NET_WM_STATE_MAXIMIZED_HORZ", None
},
485 {"_NET_WM_STATE_MAXIMIZED_VERT", None
},
486 {"_NET_WM_STATE_SKIP_TASKBAR", None
},
487 {"_NET_WM_STATE_SKIP_PAGER", None
},
488 {"_NET_WM_STATE_HIDDEN", None
},
489 {"_NET_WM_STATE_ABOVE", None
},
490 {"_SWM_WM_STATE_MANUAL", None
},
491 {"_NET_WM_STATE_FULLSCREEN", None
},
492 {"_NET_WM_ALLOWED_ACTIONS", None
},
493 {"_NET_WM_ACTION_MOVE", None
},
494 {"_NET_WM_ACTION_RESIZE", None
},
495 {"_NET_WM_ACTION_FULLSCREEN", None
},
496 {"_NET_WM_ACTION_CLOSE", None
},
499 void store_float_geom(struct ws_win
*win
, struct swm_region
*r
);
500 int floating_toggle_win(struct ws_win
*win
);
501 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
504 get_property(Window id
, Atom atom
, long count
, Atom type
,
505 unsigned long *n
, unsigned char **data
)
508 unsigned long tmp
, extra
;
509 unsigned long *nitems
;
512 nitems
= n
!= NULL
? n
: &tmp
;
513 status
= XGetWindowProperty(display
, id
, atom
, 0L, count
, False
, type
,
514 &real
, &format
, nitems
, &extra
, data
);
516 if (status
!= Success
)
525 update_iconic(struct ws_win
*win
, int newv
)
532 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
536 XChangeProperty(display
, win
->id
, iprop
, XA_INTEGER
, 32,
537 PropModeReplace
, (unsigned char *)&v
, 1);
539 XDeleteProperty(display
, win
->id
, iprop
);
543 get_iconic(struct ws_win
*win
)
548 unsigned long nitems
, extra
;
549 unsigned char *prop
= NULL
;
551 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
554 status
= XGetWindowProperty(display
, win
->id
, iprop
, 0L, 1L,
555 False
, XA_INTEGER
, &rettype
, &retfmt
, &nitems
, &extra
, &prop
);
556 if (status
!= Success
)
558 if (rettype
!= XA_INTEGER
|| retfmt
!= 32)
562 v
= *((int32_t *)prop
);
576 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
578 for (i
= 0; i
< LENGTH(ewmh
); i
++)
579 ewmh
[i
].atom
= XInternAtom(display
, ewmh
[i
].name
, False
);
581 for (i
= 0; i
< ScreenCount(display
); i
++) {
582 /* Support check window will be created by workaround(). */
584 /* Report supported atoms */
585 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
586 for (j
= 0; j
< LENGTH(ewmh
); j
++)
587 XChangeProperty(display
, screens
[i
].root
,
588 sup_list
, XA_ATOM
, 32,
589 PropModeAppend
, (unsigned char *)&ewmh
[j
].atom
,1);
597 unsigned char *data
= NULL
;
599 Atom sup_check
, sup_list
;
602 sup_check
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
603 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
605 for (i
= 0; i
< ScreenCount(display
); i
++) {
606 /* Get the support check window and destroy it */
607 success
= get_property(screens
[i
].root
, sup_check
, 1, XA_WINDOW
,
612 XDestroyWindow(display
, id
);
613 XDeleteProperty(display
, screens
[i
].root
, sup_check
);
614 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
622 ewmh_autoquirk(struct ws_win
*win
)
625 unsigned long *data
= NULL
, n
;
628 success
= get_property(win
->id
, ewmh
[_NET_WM_WINDOW_TYPE
].atom
, (~0L),
629 XA_ATOM
, &n
, (void *)&data
);
636 for (i
= 0; i
< n
; i
++) {
638 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
640 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
641 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
642 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
644 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
647 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
648 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
650 win
->quirks
= SWM_Q_FLOAT
;
658 #define SWM_EWMH_ACTION_COUNT_MAX (6)
659 #define EWMH_F_FULLSCREEN (1<<0)
660 #define EWMH_F_ABOVE (1<<1)
661 #define EWMH_F_HIDDEN (1<<2)
662 #define EWMH_F_SKIP_PAGER (1<<3)
663 #define EWMH_F_SKIP_TASKBAR (1<<4)
664 #define SWM_F_MANUAL (1<<5)
667 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
669 struct swm_geometry rg
;
677 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
683 store_float_geom(win
, win
->ws
->r
);
690 if (win
->g_floatvalid
) {
691 /* refloat at last floating relative position */
692 win
->g
.x
= win
->g_float
.x
- win
->rg_float
.x
+ rg
.x
;
693 win
->g
.y
= win
->g_float
.y
- win
->rg_float
.y
+ rg
.y
;
694 win
->g
.w
= win
->g_float
.w
;
695 win
->g
.h
= win
->g_float
.h
;
703 ewmh_update_actions(struct ws_win
*win
)
705 Atom actions
[SWM_EWMH_ACTION_COUNT_MAX
];
711 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
714 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
715 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
718 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
,
719 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)actions
, n
);
722 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
723 #define _NET_WM_STATE_ADD 1 /* add/set property */
724 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
727 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
729 unsigned int mask
= 0;
730 unsigned int changed
= 0;
731 unsigned int orig_flags
;
736 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
737 mask
= EWMH_F_FULLSCREEN
;
738 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
740 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
742 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
743 mask
= EWMH_F_SKIP_PAGER
;
744 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
745 mask
= EWMH_F_SKIP_TASKBAR
;
748 orig_flags
= win
->ewmh_flags
;
751 case _NET_WM_STATE_REMOVE
:
752 win
->ewmh_flags
&= ~mask
;
754 case _NET_WM_STATE_ADD
:
755 win
->ewmh_flags
|= mask
;
757 case _NET_WM_STATE_TOGGLE
:
758 win
->ewmh_flags
^= mask
;
762 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
764 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
766 if (!floating_toggle_win(win
))
767 win
->ewmh_flags
= orig_flags
; /* revert */
768 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
770 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
771 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
773 if (!ewmh_set_win_fullscreen(win
,
774 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
775 win
->ewmh_flags
= orig_flags
; /* revert */
777 XDeleteProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
779 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
780 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
781 XA_ATOM
, 32, PropModeAppend
,
782 (unsigned char *)&ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
, 1);
783 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
784 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
785 XA_ATOM
, 32, PropModeAppend
,
786 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
, 1);
787 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
788 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
789 XA_ATOM
, 32, PropModeAppend
,
790 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
, 1);
791 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
792 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
793 XA_ATOM
, 32, PropModeAppend
,
794 (unsigned char *)&ewmh
[_NET_WM_STATE_ABOVE
].atom
, 1);
795 if (win
->ewmh_flags
& SWM_F_MANUAL
)
796 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
797 XA_ATOM
, 32, PropModeAppend
,
798 (unsigned char *)&ewmh
[_SWM_WM_STATE_MANUAL
].atom
, 1);
802 ewmh_get_win_state(struct ws_win
*win
)
813 win
->ewmh_flags
|= EWMH_F_ABOVE
;
815 win
->ewmh_flags
|= SWM_F_MANUAL
;
817 success
= get_property(win
->id
, ewmh
[_NET_WM_STATE
].atom
,
818 (~0L), XA_ATOM
, &n
, (void *)&states
);
823 for (i
= 0; i
< n
; i
++)
824 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
844 name
= "ButtonPress";
847 name
= "ButtonRelease";
850 name
= "MotionNotify";
853 name
= "EnterNotify";
856 name
= "LeaveNotify";
865 name
= "KeymapNotify";
871 name
= "GraphicsExpose";
876 case VisibilityNotify
:
877 name
= "VisibilityNotify";
880 name
= "CreateNotify";
883 name
= "DestroyNotify";
886 name
= "UnmapNotify";
895 name
= "ReparentNotify";
897 case ConfigureNotify
:
898 name
= "ConfigureNotify";
900 case ConfigureRequest
:
901 name
= "ConfigureRequest";
904 name
= "GravityNotify";
907 name
= "ResizeRequest";
909 case CirculateNotify
:
910 name
= "CirculateNotify";
912 case CirculateRequest
:
913 name
= "CirculateRequest";
916 name
= "PropertyNotify";
919 name
= "SelectionClear";
921 case SelectionRequest
:
922 name
= "SelectionRequest";
924 case SelectionNotify
:
925 name
= "SelectionNotify";
928 name
= "ColormapNotify";
931 name
= "ClientMessage";
934 name
= "MappingNotify";
939 DNPRINTF(SWM_D_EVENTQ
,"window: %lu event: %s (%d), %d "
941 e
->xany
.window
, name
, e
->type
, QLength(display
));
943 DNPRINTF(SWM_D_EVENTQ
, "window: %lu unknown event %d, %d "
945 e
->xany
.window
, e
->type
, QLength(display
));
949 dumpwins(struct swm_region
*r
, union arg
*args
)
953 XWindowAttributes wa
;
956 fprintf(stderr
, "invalid workspace\n");
960 fprintf(stderr
, "=== managed window list ws %02d ===\n", r
->ws
->idx
);
962 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
963 state
= getstate(win
->id
);
964 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
965 fprintf(stderr
, "window: %lu failed "
966 "XGetWindowAttributes\n", win
->id
);
967 fprintf(stderr
, "window: %lu map_state: %d state: %d "
969 win
->id
, wa
.map_state
, state
, win
->transient
);
972 fprintf(stderr
, "===== unmanaged window list =====\n");
973 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
974 state
= getstate(win
->id
);
975 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
976 fprintf(stderr
, "window: %lu failed "
977 "XGetWindowAttributes\n", win
->id
);
978 fprintf(stderr
, "window: %lu map_state: %d state: %d "
980 win
->id
, wa
.map_state
, state
, win
->transient
);
983 fprintf(stderr
, "=================================\n");
988 dumpwins(struct swm_region
*r
, union arg
*args
)
991 #endif /* SWM_DEBUG */
993 void expose(XEvent
*);
994 void keypress(XEvent
*);
995 void buttonpress(XEvent
*);
996 void configurerequest(XEvent
*);
997 void configurenotify(XEvent
*);
998 void destroynotify(XEvent
*);
999 void enternotify(XEvent
*);
1000 void focusevent(XEvent
*);
1001 void mapnotify(XEvent
*);
1002 void mappingnotify(XEvent
*);
1003 void maprequest(XEvent
*);
1004 void propertynotify(XEvent
*);
1005 void unmapnotify(XEvent
*);
1006 void visibilitynotify(XEvent
*);
1007 void clientmessage(XEvent
*);
1009 void (*handler
[LASTEvent
])(XEvent
*) = {
1011 [KeyPress
] = keypress
,
1012 [ButtonPress
] = buttonpress
,
1013 [ConfigureRequest
] = configurerequest
,
1014 [ConfigureNotify
] = configurenotify
,
1015 [DestroyNotify
] = destroynotify
,
1016 [EnterNotify
] = enternotify
,
1017 [FocusIn
] = focusevent
,
1018 [FocusOut
] = focusevent
,
1019 [MapNotify
] = mapnotify
,
1020 [MappingNotify
] = mappingnotify
,
1021 [MapRequest
] = maprequest
,
1022 [PropertyNotify
] = propertynotify
,
1023 [UnmapNotify
] = unmapnotify
,
1024 [VisibilityNotify
] = visibilitynotify
,
1025 [ClientMessage
] = clientmessage
,
1031 int saved_errno
, status
;
1034 saved_errno
= errno
;
1038 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1043 if (errno
!= ECHILD
)
1044 warn("sighdlr: waitpid");
1045 #endif /* SWM_DEBUG */
1048 if (pid
== searchpid
)
1052 if (WIFEXITED(status
)) {
1053 if (WEXITSTATUS(status
) != 0)
1054 warnx("sighdlr: child exit status: %d",
1055 WEXITSTATUS(status
));
1057 warnx("sighdlr: child is terminated "
1059 #endif /* SWM_DEBUG */
1073 errno
= saved_errno
;
1079 struct pid_e
*p
= NULL
;
1081 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1086 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1095 name_to_color(char *colorname
)
1099 XColor screen_def
, exact_def
;
1100 unsigned long result
= 0;
1101 char cname
[32] = "#";
1103 cmap
= DefaultColormap(display
, screens
[0].idx
);
1104 status
= XAllocNamedColor(display
, cmap
, colorname
,
1105 &screen_def
, &exact_def
);
1107 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1108 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
1112 result
= screen_def
.pixel
;
1114 fprintf(stderr
, "color '%s' not found.\n", colorname
);
1120 setscreencolor(char *val
, int i
, int c
)
1122 if (i
> 0 && i
<= ScreenCount(display
)) {
1123 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1124 free(screens
[i
- 1].c
[c
].name
);
1125 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1127 } else if (i
== -1) {
1128 for (i
= 0; i
< ScreenCount(display
); i
++) {
1129 screens
[i
].c
[c
].color
= name_to_color(val
);
1130 free(screens
[i
].c
[c
].name
);
1131 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1135 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1136 i
, ScreenCount(display
));
1140 fancy_stacker(struct workspace
*ws
)
1142 strcpy(ws
->stacker
, "[ ]");
1143 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1144 snprintf(ws
->stacker
, sizeof ws
->stacker
, "[%d|%d]",
1145 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1146 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1147 snprintf(ws
->stacker
, sizeof ws
->stacker
, "[%d-%d]",
1148 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1152 plain_stacker(struct workspace
*ws
)
1154 strcpy(ws
->stacker
, "[ ]");
1155 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1156 strcpy(ws
->stacker
, "[|]");
1157 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1158 strcpy(ws
->stacker
, "[-]");
1162 custom_region(char *val
)
1164 unsigned int sidx
, x
, y
, w
, h
;
1166 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1167 errx(1, "invalid custom region, "
1168 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1169 if (sidx
< 1 || sidx
> ScreenCount(display
))
1170 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1171 sidx
, ScreenCount(display
));
1175 errx(1, "region %ux%u+%u+%u too small\n", w
, h
, x
, y
);
1177 if (x
> DisplayWidth(display
, sidx
) ||
1178 y
> DisplayHeight(display
, sidx
) ||
1179 w
+ x
> DisplayWidth(display
, sidx
) ||
1180 h
+ y
> DisplayHeight(display
, sidx
)) {
1181 fprintf(stderr
, "ignoring region %ux%u+%u+%u "
1182 "- not within screen boundaries "
1183 "(%ux%u)\n", w
, h
, x
, y
,
1184 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
1188 new_region(&screens
[sidx
], x
, y
, w
, h
);
1192 socket_setnonblock(int fd
)
1196 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1197 err(1, "fcntl F_GETFL");
1198 flags
|= O_NONBLOCK
;
1199 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1200 err(1, "fcntl F_SETFL");
1204 bar_print(struct swm_region
*r
, char *s
)
1206 XClearWindow(display
, r
->bar_window
);
1207 XSetForeground(display
, bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
1208 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
, s
,
1213 bar_extra_stop(void)
1217 bzero(bar_pipe
, sizeof bar_pipe
);
1220 kill(bar_pid
, SIGTERM
);
1223 strlcpy(bar_ext
, "", sizeof bar_ext
);
1228 bar_class_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1230 int do_class
, do_name
;
1232 XClassHint
*xch
= NULL
;
1234 if ((title_name_enabled
== 1 || title_class_enabled
== 1) &&
1235 cur_focus
!= NULL
) {
1236 if ((xch
= XAllocClassHint()) == NULL
)
1238 status
= XGetClassHint(display
, cur_focus
->id
, xch
);
1239 if (status
== BadWindow
|| status
== BadAlloc
)
1241 do_class
= (title_class_enabled
&& xch
->res_class
!= NULL
);
1242 do_name
= (title_name_enabled
&& xch
->res_name
!= NULL
);
1244 strlcat(s
, xch
->res_class
, sz
);
1245 if (do_class
&& do_name
)
1246 strlcat(s
, ":", sz
);
1248 strlcat(s
, xch
->res_name
, sz
);
1249 strlcat(s
, " ", sz
);
1257 bar_window_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1261 if (window_name_enabled
&& cur_focus
!= NULL
) {
1262 XFetchName(display
, cur_focus
->id
, &title
);
1264 if (cur_focus
->floating
)
1265 strlcat(s
, "(f) ", sz
);
1266 strlcat(s
, title
, sz
);
1267 strlcat(s
, " ", sz
);
1273 int urgent
[SWM_WS_MAX
];
1275 bar_urgent(char *s
, ssize_t sz
)
1277 XWMHints
*wmh
= NULL
;
1282 if (urgent_enabled
== 0)
1285 for (i
= 0; i
< SWM_WS_MAX
; i
++)
1288 for (i
= 0; i
< ScreenCount(display
); i
++)
1289 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1290 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1291 wmh
= XGetWMHints(display
, win
->id
);
1295 if (wmh
->flags
& XUrgencyHint
)
1300 strlcat(s
, "* ", sz
);
1301 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
1303 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1305 snprintf(b
, sizeof b
, "- ");
1308 strlcat(s
, "* ", sz
);
1316 struct swm_region
*r
;
1319 char s
[SWM_BAR_MAX
];
1320 char cn
[SWM_BAR_MAX
];
1321 char loc
[SWM_BAR_MAX
];
1324 if (bar_enabled
== 0)
1326 if (bar_extra
&& bar_extra_running
) {
1327 /* ignore short reads; it'll correct itself */
1328 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1329 if (b
&& b
[len
- 1] == '\n') {
1331 strlcpy(bar_ext
, b
, sizeof bar_ext
);
1333 if (b
== NULL
&& errno
!= EAGAIN
) {
1334 fprintf(stderr
, "bar_extra failed: errno: %d %s\n",
1335 errno
, strerror(errno
));
1339 strlcpy(bar_ext
, "", sizeof bar_ext
);
1341 if (clock_enabled
== 0)
1342 strlcpy(s
, "", sizeof s
);
1345 localtime_r(&tmt
, &tm
);
1346 strftime(s
, sizeof s
, clock_format
, &tm
);
1347 strlcat(s
, " ", sizeof s
);
1350 for (i
= 0; i
< ScreenCount(display
); i
++) {
1352 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1353 strlcpy(cn
, "", sizeof cn
);
1355 bar_urgent(cn
, sizeof cn
);
1356 bar_class_name(cn
, sizeof cn
, r
->ws
->focus
);
1357 bar_window_name(cn
, sizeof cn
, r
->ws
->focus
);
1360 snprintf(loc
, sizeof loc
, "%d:%d %s %s%s %s %s",
1361 x
++, r
->ws
->idx
+ 1, r
->ws
->stacker
, s
, cn
, bar_ext
,
1376 bar_toggle(struct swm_region
*r
, union arg
*args
)
1378 struct swm_region
*tmpr
;
1379 int i
, sc
= ScreenCount(display
);
1381 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
1384 for (i
= 0; i
< sc
; i
++)
1385 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1386 XUnmapWindow(display
, tmpr
->bar_window
);
1388 for (i
= 0; i
< sc
; i
++)
1389 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1390 XMapRaised(display
, tmpr
->bar_window
);
1392 bar_enabled
= !bar_enabled
;
1395 /* must be after stack */
1402 XSetWindowAttributes wa
;
1403 struct swm_region
*r
;
1406 /* do this here because the conf file is in memory */
1407 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1408 /* launch external status app */
1409 bar_extra_running
= 1;
1410 if (pipe(bar_pipe
) == -1)
1411 err(1, "pipe error");
1412 socket_setnonblock(bar_pipe
[0]);
1413 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1414 if (dup2(bar_pipe
[0], 0) == -1)
1416 if (dup2(bar_pipe
[1], 1) == -1)
1418 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1419 err(1, "could not disable SIGPIPE");
1420 switch (bar_pid
= fork()) {
1422 err(1, "cannot fork");
1426 execvp(bar_argv
[0], bar_argv
);
1427 err(1, "%s external app failed", bar_argv
[0]);
1429 default: /* parent */
1435 bzero(&wa
, sizeof wa
);
1436 for (i
= 0; i
< ScreenCount(display
); i
++)
1437 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1439 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1440 wa
.background_pixel
=
1441 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1442 XChangeWindowAttributes(display
, r
->bar_window
,
1443 CWBackPixel
| CWBorderPixel
, &wa
);
1449 bar_setup(struct swm_region
*r
)
1454 XFreeFont(display
, bar_fs
);
1458 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
1459 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
1465 if (bar_fonts
[i
] == NULL
)
1466 errx(1, "couldn't load font");
1468 errx(1, "couldn't create font structure");
1470 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 1 +
1471 2 * bar_border_width
;
1473 y
= bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1475 r
->bar_window
= XCreateSimpleWindow(display
,
1476 r
->s
->root
, x
, y
, WIDTH(r
) - 2 * bar_border_width
,
1477 bar_height
- 2 * bar_border_width
,
1478 bar_border_width
, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
1479 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1480 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
1481 XSetFont(display
, bar_gc
, bar_fs
->fid
);
1482 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
1484 XMapRaised(display
, r
->bar_window
);
1485 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
1487 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1488 err(1, "could not install bar_signal");
1493 drain_enter_notify(void)
1498 while (XCheckMaskEvent(display
, EnterWindowMask
, &cne
))
1501 DNPRINTF(SWM_D_MISC
, "drain_enter_notify: drained %d\n", i
);
1505 set_win_state(struct ws_win
*win
, long state
)
1507 long data
[] = {state
, None
};
1509 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
1514 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1515 (unsigned char *)data
, 2);
1522 unsigned char *p
= NULL
;
1525 if (!get_property(w
, astate
, 2L, astate
, &n
, &p
))
1528 result
= *((long *)p
);
1534 version(struct swm_region
*r
, union arg
*args
)
1536 bar_version
= !bar_version
;
1538 snprintf(bar_vertext
, sizeof bar_vertext
, "Version: %s CVS: %s",
1539 SWM_VERSION
, cvstag
);
1541 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
1546 client_msg(struct ws_win
*win
, Atom a
)
1548 XClientMessageEvent cm
;
1553 bzero(&cm
, sizeof cm
);
1554 cm
.type
= ClientMessage
;
1555 cm
.window
= win
->id
;
1556 cm
.message_type
= aprot
;
1559 cm
.data
.l
[1] = CurrentTime
;
1560 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
1564 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
1572 DNPRINTF(SWM_D_MISC
,
1573 "config_win: win %lu x %d y %d w %d h %d\n",
1574 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
1576 ce
.type
= ConfigureNotify
;
1577 ce
.display
= display
;
1579 ce
.window
= win
->id
;
1582 ce
.width
= win
->g
.w
;
1583 ce
.height
= win
->g
.h
;
1584 ce
.border_width
= border_width
;
1586 ce
.override_redirect
= False
;
1588 DNPRINTF(SWM_D_MISC
,
1589 "config_win: ev win %lu x %d y %d w %d h %d\n",
1590 ev
->window
, ev
->x
, ev
->y
, ev
->width
, ev
->height
);
1591 ce
.type
= ConfigureNotify
;
1592 ce
.display
= ev
->display
;
1593 ce
.event
= ev
->window
;
1594 ce
.window
= ev
->window
;
1597 ce
.width
= ev
->width
;
1598 ce
.height
= ev
->height
;
1599 ce
.border_width
= ev
->border_width
;
1600 ce
.above
= ev
->above
;
1601 ce
.override_redirect
= False
;
1604 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
1608 count_win(struct workspace
*ws
, int count_transient
)
1613 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1614 if (count_transient
== 0 && win
->floating
)
1616 if (count_transient
== 0 && win
->transient
)
1622 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
1628 quit(struct swm_region
*r
, union arg
*args
)
1630 DNPRINTF(SWM_D_MISC
, "quit\n");
1635 unmap_window(struct ws_win
*win
)
1640 /* don't unmap again */
1641 if (getstate(win
->id
) == IconicState
)
1644 set_win_state(win
, IconicState
);
1646 XUnmapWindow(display
, win
->id
);
1647 XSetWindowBorder(display
, win
->id
,
1648 win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1657 for (i
= 0; i
< ScreenCount(display
); i
++)
1658 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1659 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1664 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
1671 event
.display
= display
; /* Ignored, but what the hell */
1672 event
.window
= win
->id
;
1673 event
.root
= win
->s
->root
;
1674 event
.subwindow
= None
;
1675 event
.time
= CurrentTime
;
1680 event
.same_screen
= True
;
1681 event
.keycode
= XKeysymToKeycode(display
, keysym
);
1682 event
.state
= modifiers
;
1684 event
.type
= KeyPress
;
1685 XSendEvent(event
.display
, event
.window
, True
,
1686 KeyPressMask
, (XEvent
*)&event
);
1688 event
.type
= KeyRelease
;
1689 XSendEvent(event
.display
, event
.window
, True
,
1690 KeyPressMask
, (XEvent
*)&event
);
1695 restart(struct swm_region
*r
, union arg
*args
)
1697 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
1699 /* disable alarm because the following code may not be interrupted */
1701 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
1702 errx(1, "can't disable alarm");
1707 XCloseDisplay(display
);
1708 execvp(start_argv
[0], start_argv
);
1709 fprintf(stderr
, "execvp failed\n");
1715 root_to_region(Window root
)
1717 struct swm_region
*r
= NULL
;
1719 int i
, x
, y
, wx
, wy
;
1722 for (i
= 0; i
< ScreenCount(display
); i
++)
1723 if (screens
[i
].root
== root
)
1726 if (XQueryPointer(display
, screens
[i
].root
,
1727 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
1728 /* choose a region based on pointer location */
1729 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1730 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
1731 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
1736 r
= TAILQ_FIRST(&screens
[i
].rl
);
1742 find_unmanaged_window(Window id
)
1747 for (i
= 0; i
< ScreenCount(display
); i
++)
1748 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1749 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
1757 find_window(Window id
)
1760 Window wrr
, wpr
, *wcr
= NULL
;
1764 for (i
= 0; i
< ScreenCount(display
); i
++)
1765 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1766 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1770 /* if we were looking for the parent return that window instead */
1771 if (XQueryTree(display
, id
, &wrr
, &wpr
, &wcr
, &nc
) == 0)
1776 /* ignore not found and root */
1777 if (wpr
== 0 || wrr
== wpr
)
1780 /* look for parent */
1781 for (i
= 0; i
< ScreenCount(display
); i
++)
1782 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1783 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1791 spawn(int ws_idx
, union arg
*args
, int close_fd
)
1796 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
1799 close(ConnectionNumber(display
));
1801 setenv("LD_PRELOAD", SWM_LIB
, 1);
1803 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
1807 setenv("_SWM_WS", ret
, 1);
1811 if (asprintf(&ret
, "%d", getpid()) == -1) {
1815 setenv("_SWM_PID", ret
, 1);
1819 if (setsid() == -1) {
1826 * close stdin and stdout to prevent interaction between apps
1827 * and the baraction script
1828 * leave stderr open to record errors
1830 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
1834 dup2(fd
, STDIN_FILENO
);
1835 dup2(fd
, STDOUT_FILENO
);
1840 execvp(args
->argv
[0], args
->argv
);
1847 spawnterm(struct swm_region
*r
, union arg
*args
)
1849 DNPRINTF(SWM_D_MISC
, "spawnterm\n");
1853 setenv("_SWM_XTERM_FONTADJ", "", 1);
1854 spawn(r
->ws
->idx
, args
, 1);
1859 kill_refs(struct ws_win
*win
)
1862 struct swm_region
*r
;
1863 struct workspace
*ws
;
1868 for (i
= 0; i
< ScreenCount(display
); i
++)
1869 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1870 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
1872 if (win
== ws
->focus
)
1874 if (win
== ws
->focus_prev
)
1875 ws
->focus_prev
= NULL
;
1880 validate_win(struct ws_win
*testwin
)
1883 struct workspace
*ws
;
1884 struct swm_region
*r
;
1885 int i
, x
, foundit
= 0;
1887 if (testwin
== NULL
)
1890 for (i
= 0, foundit
= 0; i
< ScreenCount(display
); i
++)
1891 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1892 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
1894 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
1902 validate_ws(struct workspace
*testws
)
1904 struct swm_region
*r
;
1905 struct workspace
*ws
;
1908 /* validate all ws */
1909 for (i
= 0, foundit
= 0; i
< ScreenCount(display
); i
++)
1910 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1911 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
1920 unfocus_win(struct ws_win
*win
)
1925 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: id: %lu\n", WINID(win
));
1929 if (win
->ws
== NULL
)
1932 if (validate_ws(win
->ws
))
1933 return; /* XXX this gets hit with thunderbird, needs fixing */
1935 if (win
->ws
->r
== NULL
)
1938 if (validate_win(win
)) {
1943 if (win
->ws
->focus
== win
) {
1944 win
->ws
->focus
= NULL
;
1945 win
->ws
->focus_prev
= win
;
1948 if (validate_win(win
->ws
->focus
)) {
1949 kill_refs(win
->ws
->focus
);
1950 win
->ws
->focus
= NULL
;
1952 if (validate_win(win
->ws
->focus_prev
)) {
1953 kill_refs(win
->ws
->focus_prev
);
1954 win
->ws
->focus_prev
= NULL
;
1957 /* drain all previous unfocus events */
1958 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
1961 grabbuttons(win
, 0);
1962 XSetWindowBorder(display
, win
->id
,
1963 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1965 XChangeProperty(display
, win
->s
->root
,
1966 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
1967 PropModeReplace
, (unsigned char *)&none
,1);
1976 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
1978 for (i
= 0; i
< ScreenCount(display
); i
++)
1979 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1980 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1985 focus_win(struct ws_win
*win
)
1990 struct ws_win
*cfw
= NULL
;
1993 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
? win
->id
: 0);
1997 if (win
->ws
== NULL
)
2000 if (validate_ws(win
->ws
))
2001 return; /* XXX this gets hit with thunderbird, needs fixing */
2003 if (validate_win(win
)) {
2008 if (validate_win(win
)) {
2013 XGetInputFocus(display
, &cur_focus
, &rr
);
2014 if ((cfw
= find_window(cur_focus
)) != NULL
)
2017 /* use larger hammer since the window was killed somehow */
2018 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2019 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2020 XSetWindowBorder(display
, cfw
->id
,
2021 cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2024 win
->ws
->focus
= win
;
2026 if (win
->ws
->r
!= NULL
) {
2027 /* drain all previous focus events */
2028 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
2032 XSetInputFocus(display
, win
->id
,
2033 RevertToParent
, CurrentTime
);
2034 grabbuttons(win
, 1);
2035 XSetWindowBorder(display
, win
->id
,
2036 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2037 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2038 win
->ws
->always_raise
)
2039 XMapRaised(display
, win
->id
);
2041 XChangeProperty(display
, win
->s
->root
,
2042 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2043 PropModeReplace
, (unsigned char *)&win
->id
,1);
2046 if (window_name_enabled
)
2051 switchws(struct swm_region
*r
, union arg
*args
)
2053 int wsid
= args
->id
, unmap_old
= 0;
2054 struct swm_region
*this_r
, *other_r
;
2056 struct workspace
*new_ws
, *old_ws
;
2063 old_ws
= this_r
->ws
;
2064 new_ws
= &this_r
->s
->ws
[wsid
];
2066 DNPRINTF(SWM_D_WS
, "switchws screen[%d]:%dx%d+%d+%d: "
2067 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
2070 if (new_ws
== NULL
|| old_ws
== NULL
)
2072 if (new_ws
== old_ws
)
2075 other_r
= new_ws
->r
;
2076 if (other_r
== NULL
) {
2077 /* the other workspace is hidden, hide this one */
2081 /* the other ws is visible in another region, exchange them */
2082 other_r
->ws_prior
= new_ws
;
2083 other_r
->ws
= old_ws
;
2084 old_ws
->r
= other_r
;
2086 this_r
->ws_prior
= old_ws
;
2087 this_r
->ws
= new_ws
;
2090 /* this is needed so that we can click on a window after a restart */
2094 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2095 focus(new_ws
->r
, &a
);
2099 /* unmap old windows */
2101 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2104 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2105 drain_enter_notify();
2109 cyclews(struct swm_region
*r
, union arg
*args
)
2112 struct swm_screen
*s
= r
->s
;
2114 DNPRINTF(SWM_D_WS
, "cyclews id %d "
2115 "in screen[%d]:%dx%d+%d+%d ws %d\n", args
->id
,
2116 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2121 case SWM_ARG_ID_CYCLEWS_UP
:
2122 if (a
.id
< SWM_WS_MAX
- 1)
2127 case SWM_ARG_ID_CYCLEWS_DOWN
:
2131 a
.id
= SWM_WS_MAX
- 1;
2137 if (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
))
2139 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2143 } while (a
.id
!= r
->ws
->idx
);
2147 priorws(struct swm_region
*r
, union arg
*args
)
2151 DNPRINTF(SWM_D_WS
, "priorws id %d "
2152 "in screen[%d]:%dx%d+%d+%d ws %d\n", args
->id
,
2153 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2155 if (r
->ws_prior
== NULL
)
2158 a
.id
= r
->ws_prior
->idx
;
2163 cyclescr(struct swm_region
*r
, union arg
*args
)
2165 struct swm_region
*rr
= NULL
;
2169 /* do nothing if we don't have more than one screen */
2170 if (!(ScreenCount(display
) > 1 || outputs
> 1))
2175 case SWM_ARG_ID_CYCLESC_UP
:
2176 rr
= TAILQ_NEXT(r
, entry
);
2178 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2180 case SWM_ARG_ID_CYCLESC_DOWN
:
2181 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2183 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2191 /* move mouse to region */
2193 y
= rr
->g
.y
+ 1 + (bar_enabled
? bar_height
: 0);
2194 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2196 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2199 if (rr
->ws
->focus
) {
2200 /* move to focus window */
2201 x
= rr
->ws
->focus
->g
.x
+ 1;
2202 y
= rr
->ws
->focus
->g
.y
+ 1;
2203 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2208 sort_windows(struct ws_win_list
*wl
)
2210 struct ws_win
*win
, *parent
, *nxt
;
2215 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2216 nxt
= TAILQ_NEXT(win
, entry
);
2217 if (win
->transient
) {
2218 parent
= find_window(win
->transient
);
2219 if (parent
== NULL
) {
2220 fprintf(stderr
, "not possible bug\n");
2223 TAILQ_REMOVE(wl
, win
, entry
);
2224 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2231 swapwin(struct swm_region
*r
, union arg
*args
)
2233 struct ws_win
*target
, *source
;
2234 struct ws_win
*cur_focus
;
2235 struct ws_win_list
*wl
;
2238 DNPRINTF(SWM_D_WS
, "swapwin id %d "
2239 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
2240 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2242 cur_focus
= r
->ws
->focus
;
2243 if (cur_focus
== NULL
)
2247 wl
= &source
->ws
->winlist
;
2250 case SWM_ARG_ID_SWAPPREV
:
2251 if (source
->transient
)
2252 source
= find_window(source
->transient
);
2253 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2254 if (target
&& target
->transient
)
2255 target
= find_window(target
->transient
);
2256 TAILQ_REMOVE(wl
, source
, entry
);
2258 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2260 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2262 case SWM_ARG_ID_SWAPNEXT
:
2263 target
= TAILQ_NEXT(source
, entry
);
2264 /* move the parent and let the sort handle the move */
2265 if (source
->transient
)
2266 source
= find_window(source
->transient
);
2267 TAILQ_REMOVE(wl
, source
, entry
);
2269 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2271 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2273 case SWM_ARG_ID_SWAPMAIN
:
2274 target
= TAILQ_FIRST(wl
);
2275 if (target
== source
) {
2276 if (source
->ws
->focus_prev
!= NULL
&&
2277 source
->ws
->focus_prev
!= target
)
2279 source
= source
->ws
->focus_prev
;
2283 if (target
== NULL
|| source
== NULL
)
2285 source
->ws
->focus_prev
= target
;
2286 TAILQ_REMOVE(wl
, target
, entry
);
2287 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2288 TAILQ_REMOVE(wl
, source
, entry
);
2289 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2291 case SWM_ARG_ID_MOVELAST
:
2292 TAILQ_REMOVE(wl
, source
, entry
);
2293 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2296 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
2306 focus_prev(struct ws_win
*win
)
2308 struct ws_win
*winfocus
= NULL
, *winlostfocus
= NULL
;
2309 struct ws_win
*cur_focus
= NULL
;
2310 struct ws_win_list
*wl
= NULL
;
2311 struct workspace
*ws
= NULL
;
2313 DNPRINTF(SWM_D_FOCUS
, "focus_prev: id %lu\n", WINID(win
));
2315 if (!(win
&& win
->ws
))
2320 cur_focus
= ws
->focus
;
2321 winlostfocus
= cur_focus
;
2323 /* pickle, just focus on whatever */
2324 if (cur_focus
== NULL
) {
2325 /* use prev_focus if valid */
2326 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2327 find_window(WINID(ws
->focus_prev
)))
2328 winfocus
= ws
->focus_prev
;
2329 if (winfocus
== NULL
)
2330 winfocus
= TAILQ_FIRST(wl
);
2334 /* if transient focus on parent */
2335 if (cur_focus
->transient
) {
2336 winfocus
= find_window(cur_focus
->transient
);
2340 /* if in max_stack try harder */
2341 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2342 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2343 if (cur_focus
!= ws
->focus_prev
)
2344 winfocus
= ws
->focus_prev
;
2345 else if (cur_focus
!= ws
->focus
)
2346 winfocus
= ws
->focus
;
2348 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2353 if (cur_focus
== win
)
2354 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2355 if (winfocus
== NULL
)
2356 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2357 if (winfocus
== NULL
|| winfocus
== win
)
2358 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2360 if (winfocus
== winlostfocus
|| winfocus
== NULL
)
2363 focus_magic(winfocus
);
2367 focus(struct swm_region
*r
, union arg
*args
)
2369 struct ws_win
*winfocus
= NULL
, *winlostfocus
= NULL
, *head
;
2370 struct ws_win
*cur_focus
= NULL
;
2371 struct ws_win_list
*wl
= NULL
;
2372 struct workspace
*ws
= NULL
;
2377 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
2379 /* treat FOCUS_CUR special */
2380 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2381 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2382 winfocus
= r
->ws
->focus
;
2383 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2384 winfocus
= r
->ws
->focus_prev
;
2386 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2387 if (winfocus
->iconic
== 0)
2390 focus_magic(winfocus
);
2394 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2399 winlostfocus
= cur_focus
;
2402 case SWM_ARG_ID_FOCUSPREV
:
2403 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2405 head
= TAILQ_LAST(wl
, ws_win_list
);
2407 if (WINID(winfocus
) == cur_focus
->transient
) {
2408 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2410 head
= TAILQ_LAST(wl
, ws_win_list
);
2415 if (winfocus
&& winfocus
->iconic
) {
2416 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
2417 if (winfocus
->iconic
== 0)
2422 case SWM_ARG_ID_FOCUSNEXT
:
2423 head
= TAILQ_NEXT(cur_focus
, entry
);
2425 head
= TAILQ_FIRST(wl
);
2429 if (winfocus
&& winfocus
->iconic
) {
2430 TAILQ_FOREACH(winfocus
, wl
, entry
)
2431 if (winfocus
->iconic
== 0)
2436 case SWM_ARG_ID_FOCUSMAIN
:
2437 winfocus
= TAILQ_FIRST(wl
);
2438 if (winfocus
== cur_focus
)
2439 winfocus
= cur_focus
->ws
->focus_prev
;
2445 if (winfocus
== winlostfocus
|| winfocus
== NULL
)
2448 focus_magic(winfocus
);
2452 cycle_layout(struct swm_region
*r
, union arg
*args
)
2454 struct workspace
*ws
= r
->ws
;
2455 struct ws_win
*winfocus
;
2458 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2460 winfocus
= ws
->focus
;
2463 if (ws
->cur_layout
->l_stack
== NULL
)
2464 ws
->cur_layout
= &layouts
[0];
2467 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2468 drain_enter_notify();
2469 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2475 stack_config(struct swm_region
*r
, union arg
*args
)
2477 struct workspace
*ws
= r
->ws
;
2479 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
2482 if (ws
->cur_layout
->l_config
!= NULL
)
2483 ws
->cur_layout
->l_config(ws
, args
->id
);
2485 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
2492 struct swm_geometry g
;
2493 struct swm_region
*r
;
2496 DNPRINTF(SWM_D_STACK
, "stack\n");
2498 for (i
= 0; i
< ScreenCount(display
); i
++) {
2500 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2501 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
2502 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
2504 /* start with screen geometry, adjust for bar */
2506 g
.w
-= 2 * border_width
;
2507 g
.h
-= 2 * border_width
;
2513 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
2514 r
->ws
->cur_layout
->l_string(r
->ws
);
2515 /* save r so we can track region changes */
2522 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2523 drain_enter_notify();
2527 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
2529 /* retain window geom and region geom */
2530 win
->g_float
.x
= win
->g
.x
;
2531 win
->g_float
.y
= win
->g
.y
;
2532 win
->g_float
.w
= win
->g
.w
;
2533 win
->g_float
.h
= win
->g
.h
;
2534 win
->rg_float
.x
= r
->g
.x
;
2535 win
->rg_float
.y
= r
->g
.y
;
2536 win
->rg_float
.w
= r
->g
.w
;
2537 win
->rg_float
.h
= r
->g
.h
;
2538 win
->g_floatvalid
= 1;
2542 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
2550 bzero(&wc
, sizeof wc
);
2551 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
2554 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2555 * geom on ws switches or return from max mode
2558 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&& win
->g_floatvalid
2559 && !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
2561 * use stored g and rg to set relative position and size
2562 * as in old region or before max stack mode
2564 win
->g
.x
= win
->g_float
.x
- win
->rg_float
.x
+ r
->g
.x
;
2565 win
->g
.y
= win
->g_float
.y
- win
->rg_float
.y
+ r
->g
.y
;
2566 win
->g
.w
= win
->g_float
.w
;
2567 win
->g
.h
= win
->g_float
.h
;
2568 win
->g_floatvalid
= 0;
2571 win
->floatmaxed
= 0;
2573 if ((win
->quirks
& SWM_Q_FULLSCREEN
) && (win
->g
.w
>= WIDTH(r
)) &&
2574 (win
->g
.h
>= HEIGHT(r
)))
2575 wc
.border_width
= 0;
2577 wc
.border_width
= border_width
;
2578 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
2579 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
2580 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
2585 * floaters and transients are auto-centred unless moved
2588 win
->g
.x
= r
->g
.x
+ (WIDTH(r
) - win
->g
.w
) /
2589 2 - wc
.border_width
;
2590 win
->g
.y
= r
->g
.y
+ (HEIGHT(r
) - win
->g
.h
) /
2591 2 - wc
.border_width
;
2594 /* win can be outside r if new r smaller than old r */
2595 /* Ensure top left corner inside r (move probs otherwise) */
2596 if (win
->g
.x
< r
->g
.x
- wc
.border_width
)
2597 win
->g
.x
= r
->g
.x
- wc
.border_width
;
2598 if (win
->g
.x
> r
->g
.x
+ r
->g
.w
- 1)
2599 win
->g
.x
= (win
->g
.w
> r
->g
.w
) ? r
->g
.x
:
2600 (r
->g
.x
+ r
->g
.w
- win
->g
.w
- 2 * wc
.border_width
);
2601 if (win
->g
.y
< r
->g
.y
- wc
.border_width
)
2602 win
->g
.y
= r
->g
.y
- wc
.border_width
;
2603 if (win
->g
.y
> r
->g
.y
+ r
->g
.h
- 1)
2604 win
->g
.y
= (win
->g
.h
> r
->g
.h
) ? r
->g
.y
:
2605 (r
->g
.y
+ r
->g
.h
- win
->g
.h
- 2 * wc
.border_width
);
2609 wc
.width
= win
->g
.w
;
2610 wc
.height
= win
->g
.h
;
2613 * Retain floater and transient geometry for correct positioning
2614 * when ws changes region
2616 if (!(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
2617 store_float_geom(win
, r
);
2619 DNPRINTF(SWM_D_MISC
, "stack_floater: win %lu x %d y %d w %d h %d\n",
2620 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
2622 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2626 * Send keystrokes to terminal to decrease/increase the font size as the
2627 * window size changes.
2630 adjust_font(struct ws_win
*win
)
2632 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
2633 win
->floating
|| win
->transient
)
2636 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
2637 win
->g
.w
/ win
->sh
.width_inc
< term_width
&&
2638 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
2639 win
->font_size_boundary
[win
->font_steps
] =
2640 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
2643 win
->last_inc
= win
->sh
.width_inc
;
2644 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
2645 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
2646 win
->g
.w
> win
->font_size_boundary
[win
->font_steps
- 1]) {
2649 win
->last_inc
= win
->sh
.width_inc
;
2650 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
2654 #define SWAPXY(g) do { \
2656 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2657 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2660 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
2663 XWindowAttributes wa
;
2664 struct swm_geometry win_g
, r_g
= *g
;
2665 struct ws_win
*win
, *fs_win
= 0;
2666 int i
, j
, s
, stacks
;
2667 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
2668 int hrh
, extra
= 0, h_slice
, last_h
= 0;
2669 int split
, colno
, winno
, mwin
, msize
, mscale
;
2670 int remain
, missing
, v_slice
, reconfigure
;
2673 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d\n rot=%s flip=%s",
2674 ws
->idx
, rot
? "yes" : "no", flip
? "yes" : "no");
2676 winno
= count_win(ws
, 0);
2677 if (winno
== 0 && count_win(ws
, 1) == 0)
2680 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2681 if (win
->transient
== 0 && win
->floating
== 0
2682 && win
->iconic
== 0)
2689 w_inc
= win
->sh
.width_inc
;
2690 w_base
= win
->sh
.base_width
;
2691 mwin
= ws
->l_state
.horizontal_mwin
;
2692 mscale
= ws
->l_state
.horizontal_msize
;
2693 stacks
= ws
->l_state
.horizontal_stacks
;
2696 w_inc
= win
->sh
.height_inc
;
2697 w_base
= win
->sh
.base_height
;
2698 mwin
= ws
->l_state
.vertical_mwin
;
2699 mscale
= ws
->l_state
.vertical_msize
;
2700 stacks
= ws
->l_state
.vertical_stacks
;
2704 if (stacks
> winno
- mwin
)
2705 stacks
= winno
- mwin
;
2709 h_slice
= r_g
.h
/ SWM_H_SLICE
;
2710 if (mwin
&& winno
> mwin
) {
2711 v_slice
= r_g
.w
/ SWM_V_SLICE
;
2715 win_g
.w
= v_slice
* mscale
;
2717 if (w_inc
> 1 && w_inc
< v_slice
) {
2718 /* adjust for window's requested size increment */
2719 remain
= (win_g
.w
- w_base
) % w_inc
;
2720 missing
= w_inc
- remain
;
2727 win_g
.x
+= r_g
.w
- msize
;
2730 colno
= split
= winno
/ stacks
;
2731 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
2732 2 * border_width
) / stacks
);
2734 hrh
= r_g
.h
/ colno
;
2735 extra
= r_g
.h
- (colno
* hrh
);
2736 win_g
.h
= hrh
- 2 * border_width
;
2738 /* stack all the tiled windows */
2739 i
= j
= 0, s
= stacks
;
2740 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2741 if (win
->transient
!= 0 || win
->floating
!= 0)
2743 if (win
->iconic
!= 0)
2746 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
2751 if (split
&& i
== split
) {
2752 colno
= (winno
- mwin
) / stacks
;
2753 if (s
<= (winno
- mwin
) % stacks
)
2755 split
= split
+ colno
;
2756 hrh
= (r_g
.h
/ colno
);
2757 extra
= r_g
.h
- (colno
* hrh
);
2761 win_g
.x
+= win_g
.w
+ 2 * border_width
;
2762 win_g
.w
= (r_g
.w
- msize
-
2763 (stacks
* 2 * border_width
)) / stacks
;
2765 win_g
.w
+= (r_g
.w
- msize
-
2766 (stacks
* 2 * border_width
)) % stacks
;
2770 win_g
.h
= hrh
- 2 * border_width
;
2772 h_inc
= win
->sh
.width_inc
;
2773 h_base
= win
->sh
.base_width
;
2775 h_inc
= win
->sh
.height_inc
;
2776 h_base
= win
->sh
.base_height
;
2778 if (j
== colno
- 1) {
2779 win_g
.h
= hrh
+ extra
;
2780 } else if (h_inc
> 1 && h_inc
< h_slice
) {
2781 /* adjust for window's requested size increment */
2782 remain
= (win_g
.h
- h_base
) % h_inc
;
2783 missing
= h_inc
- remain
;
2785 if (missing
<= extra
|| j
== 0) {
2797 win_g
.y
+= last_h
+ 2 * border_width
;
2799 bzero(&wc
, sizeof wc
);
2800 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
2801 wc
.border_width
= 0;
2802 win_g
.w
+= 2 * border_width
;
2803 win_g
.h
+= 2 * border_width
;
2805 wc
.border_width
= border_width
;
2808 if (win
->g
.x
!= win_g
.y
|| win
->g
.y
!= win_g
.x
||
2809 win
->g
.w
!= win_g
.h
|| win
->g
.h
!= win_g
.w
) {
2811 win
->g
.x
= wc
.x
= win_g
.y
;
2812 win
->g
.y
= wc
.y
= win_g
.x
;
2813 win
->g
.w
= wc
.width
= win_g
.h
;
2814 win
->g
.h
= wc
.height
= win_g
.w
;
2817 if (win
->g
.x
!= win_g
.x
|| win
->g
.y
!= win_g
.y
||
2818 win
->g
.w
!= win_g
.w
|| win
->g
.h
!= win_g
.h
) {
2820 win
->g
.x
= wc
.x
= win_g
.x
;
2821 win
->g
.y
= wc
.y
= win_g
.y
;
2822 win
->g
.w
= wc
.width
= win_g
.w
;
2823 win
->g
.h
= wc
.height
= win_g
.h
;
2828 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
2829 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2832 if (XGetWindowAttributes(display
, win
->id
, &wa
))
2833 if (wa
.map_state
== IsUnmapped
)
2834 XMapRaised(display
, win
->id
);
2842 /* now, stack all the floaters and transients */
2843 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2844 if (win
->transient
== 0 && win
->floating
== 0)
2846 if (win
->iconic
== 1)
2848 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
2853 stack_floater(win
, ws
->r
);
2854 XMapRaised(display
, win
->id
);
2858 stack_floater(fs_win
, ws
->r
);
2859 XMapRaised(display
, fs_win
->id
);
2864 vertical_config(struct workspace
*ws
, int id
)
2866 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
2869 case SWM_ARG_ID_STACKRESET
:
2870 case SWM_ARG_ID_STACKINIT
:
2871 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
2872 ws
->l_state
.vertical_mwin
= 1;
2873 ws
->l_state
.vertical_stacks
= 1;
2875 case SWM_ARG_ID_MASTERSHRINK
:
2876 if (ws
->l_state
.vertical_msize
> 1)
2877 ws
->l_state
.vertical_msize
--;
2879 case SWM_ARG_ID_MASTERGROW
:
2880 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
2881 ws
->l_state
.vertical_msize
++;
2883 case SWM_ARG_ID_MASTERADD
:
2884 ws
->l_state
.vertical_mwin
++;
2886 case SWM_ARG_ID_MASTERDEL
:
2887 if (ws
->l_state
.vertical_mwin
> 0)
2888 ws
->l_state
.vertical_mwin
--;
2890 case SWM_ARG_ID_STACKINC
:
2891 ws
->l_state
.vertical_stacks
++;
2893 case SWM_ARG_ID_STACKDEC
:
2894 if (ws
->l_state
.vertical_stacks
> 1)
2895 ws
->l_state
.vertical_stacks
--;
2903 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
2905 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
2907 stack_master(ws
, g
, 0, 0);
2911 horizontal_config(struct workspace
*ws
, int id
)
2913 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
2916 case SWM_ARG_ID_STACKRESET
:
2917 case SWM_ARG_ID_STACKINIT
:
2918 ws
->l_state
.horizontal_mwin
= 1;
2919 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
2920 ws
->l_state
.horizontal_stacks
= 1;
2922 case SWM_ARG_ID_MASTERSHRINK
:
2923 if (ws
->l_state
.horizontal_msize
> 1)
2924 ws
->l_state
.horizontal_msize
--;
2926 case SWM_ARG_ID_MASTERGROW
:
2927 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
2928 ws
->l_state
.horizontal_msize
++;
2930 case SWM_ARG_ID_MASTERADD
:
2931 ws
->l_state
.horizontal_mwin
++;
2933 case SWM_ARG_ID_MASTERDEL
:
2934 if (ws
->l_state
.horizontal_mwin
> 0)
2935 ws
->l_state
.horizontal_mwin
--;
2937 case SWM_ARG_ID_STACKINC
:
2938 ws
->l_state
.horizontal_stacks
++;
2940 case SWM_ARG_ID_STACKDEC
:
2941 if (ws
->l_state
.horizontal_stacks
> 1)
2942 ws
->l_state
.horizontal_stacks
--;
2950 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
2952 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
2954 stack_master(ws
, g
, 1, 0);
2957 /* fullscreen view */
2959 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
2962 struct swm_geometry gg
= *g
;
2963 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
2967 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
2972 winno
= count_win(ws
, 0);
2973 if (winno
== 0 && count_win(ws
, 1) == 0)
2976 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2977 if (win
->transient
) {
2979 parent
= find_window(win
->transient
);
2983 if (win
->floating
&& win
->floatmaxed
== 0 ) {
2985 * retain geometry for retrieval on exit from
2988 store_float_geom(win
, ws
->r
);
2989 win
->floatmaxed
= 1;
2992 /* only reconfigure if necessary */
2993 if (win
->g
.x
!= gg
.x
|| win
->g
.y
!= gg
.y
|| win
->g
.w
!= gg
.w
||
2995 bzero(&wc
, sizeof wc
);
2996 win
->g
.x
= wc
.x
= gg
.x
;
2997 win
->g
.y
= wc
.y
= gg
.y
;
2999 wc
.border_width
= border_width
;
3000 win
->g
.w
= wc
.width
= gg
.w
;
3001 win
->g
.h
= wc
.height
= gg
.h
;
3003 wc
.border_width
= 0;
3004 win
->g
.w
= wc
.width
= gg
.w
+ 2 * border_width
;
3005 win
->g
.h
= wc
.height
= gg
.h
+ 2 * border_width
;
3007 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
3008 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3010 /* unmap only if we don't have multi screen */
3011 if (win
!= ws
->focus
)
3012 if (!(ScreenCount(display
) > 1 || outputs
> 1))
3016 /* put the last transient on top */
3019 XMapRaised(display
, parent
->id
);
3020 stack_floater(wintrans
, ws
->r
);
3021 focus_magic(wintrans
);
3026 send_to_ws(struct swm_region
*r
, union arg
*args
)
3028 int wsid
= args
->id
;
3029 struct ws_win
*win
= NULL
, *parent
;
3030 struct workspace
*ws
, *nws
;
3031 Atom ws_idx_atom
= 0;
3032 unsigned char ws_idx_str
[SWM_PROPLEN
];
3041 if (win
->ws
->idx
== wsid
)
3044 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
3047 nws
= &win
->s
->ws
[wsid
];
3049 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3051 if (win
->transient
) {
3052 parent
= find_window(win
->transient
);
3054 unmap_window(parent
);
3055 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3056 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3061 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3062 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3065 /* Try to update the window's workspace property */
3066 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
3068 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3070 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
3072 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
3073 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
3080 pressbutton(struct swm_region
*r
, union arg
*args
)
3082 XTestFakeButtonEvent(display
, args
->id
, True
, CurrentTime
);
3083 XTestFakeButtonEvent(display
, args
->id
, False
, CurrentTime
);
3087 raise_toggle(struct swm_region
*r
, union arg
*args
)
3089 if (r
&& r
->ws
== NULL
)
3092 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3094 /* bring floaters back to top */
3095 if (r
->ws
->always_raise
== 0)
3100 iconify(struct swm_region
*r
, union arg
*args
)
3104 if (r
->ws
->focus
== NULL
)
3106 unmap_window(r
->ws
->focus
);
3107 update_iconic(r
->ws
->focus
, 1);
3109 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3110 drain_enter_notify();
3111 r
->ws
->focus
= NULL
;
3112 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3117 get_win_name(Display
*dpy
, Window win
, Atom wname
, Atom stype
,
3118 unsigned long *slen
)
3121 unsigned long nitems
, nbytes
, nextra
;
3122 unsigned char *prop
= NULL
;
3125 status
= XGetWindowProperty(dpy
, win
, wname
, 0L, 0L, False
, stype
,
3126 &rettype
, &retfmt
, &nitems
, &nbytes
, &prop
);
3127 if (status
!= Success
)
3131 status
= XGetWindowProperty(dpy
, win
, wname
, 0L, nbytes
, False
,
3132 stype
, &rettype
, &retfmt
, &nitems
, &nextra
, &prop
);
3133 if (status
!= Success
) {
3137 if (rettype
!= stype
) {
3146 uniconify(struct swm_region
*r
, union arg
*args
)
3150 unsigned char *name
;
3154 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3156 if (r
&& r
->ws
== NULL
)
3159 /* make sure we have anything to uniconify */
3160 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3161 if (win
->ws
== NULL
)
3162 continue; /* should never happen */
3163 if (win
->iconic
== 0)
3172 spawn_select(r
, args
, "uniconify", &searchpid
);
3174 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3177 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3178 if (win
->ws
== NULL
)
3179 continue; /* should never happen */
3180 if (win
->iconic
== 0)
3183 name
= get_win_name(display
, win
->id
, a_wmname
, a_string
,
3187 fprintf(lfile
, "%s.%lu\n", name
, win
->id
);
3194 #define MAX_RESP_LEN 1024
3197 search_do_resp(void)
3201 unsigned char *name
;
3205 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
3210 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
3211 fprintf(stderr
, "search: calloc\n");
3215 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
3217 fprintf(stderr
, "search: read error: %s\n", strerror(errno
));
3220 resp
[rbytes
] = '\0';
3223 DNPRINTF(SWM_D_MISC
, "search_do_resp: resp %s\n", resp
);
3224 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3225 if (win
->iconic
== 0)
3227 name
= get_win_name(display
, win
->id
, a_wmname
, a_string
, &len
);
3230 if (asprintf(&s
, "%s.%lu", name
, win
->id
) == -1) {
3235 if (strncmp(s
, resp
, len
) == 0) {
3236 /* XXX this should be a callback to generalize */
3237 update_iconic(win
, 0);
3248 wkill(struct swm_region
*r
, union arg
*args
)
3250 DNPRINTF(SWM_D_MISC
, "wkill %d\n", args
->id
);
3252 if (r
->ws
->focus
== NULL
)
3255 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
3256 XKillClient(display
, r
->ws
->focus
->id
);
3258 if (r
->ws
->focus
->can_delete
)
3259 client_msg(r
->ws
->focus
, adelete
);
3264 floating_toggle_win(struct ws_win
*win
)
3266 struct swm_region
*r
;
3276 /* reject floating toggles in max stack mode */
3277 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3280 if (win
->floating
) {
3281 if (!win
->floatmaxed
) {
3282 /* retain position for refloat */
3283 store_float_geom(win
, r
);
3287 if (win
->g_floatvalid
) {
3288 /* refloat at last floating relative position */
3289 win
->g
.x
= win
->g_float
.x
- win
->rg_float
.x
+ r
->g
.x
;
3290 win
->g
.y
= win
->g_float
.y
- win
->rg_float
.y
+ r
->g
.y
;
3291 win
->g
.w
= win
->g_float
.w
;
3292 win
->g
.h
= win
->g_float
.h
;
3297 ewmh_update_actions(win
);
3303 floating_toggle(struct swm_region
*r
, union arg
*args
)
3305 struct ws_win
*win
= r
->ws
->focus
;
3311 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
3312 _NET_WM_STATE_TOGGLE
);
3315 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3316 drain_enter_notify();
3318 if (win
== win
->ws
->focus
) {
3319 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3320 focus(win
->ws
->r
, &a
);
3325 resize_window(struct ws_win
*win
, int center
)
3329 struct swm_region
*r
;
3331 r
= root_to_region(win
->wa
.root
);
3332 bzero(&wc
, sizeof wc
);
3333 mask
= CWBorderWidth
| CWWidth
| CWHeight
;
3334 wc
.border_width
= border_width
;
3335 wc
.width
= win
->g
.w
;
3336 wc
.height
= win
->g
.h
;
3337 if (center
== SWM_ARG_ID_CENTER
) {
3338 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2 - border_width
;
3339 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2 - border_width
;
3343 DNPRINTF(SWM_D_STACK
, "resize_window: win %lu x %d y %d w %d h %d\n",
3344 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
3346 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3349 #define SWM_RESIZE_STEPS (50)
3352 resize(struct ws_win
*win
, union arg
*args
)
3356 struct swm_region
*r
= NULL
;
3358 int resize_step
= 0;
3364 DNPRINTF(SWM_D_MOUSE
, "resize: win %lu floating %d trans %lu\n",
3365 win
->id
, win
->floating
, win
->transient
);
3367 if (!(win
->transient
!= 0 || win
->floating
!= 0))
3370 /* reject resizes in max mode for floaters (transient ok) */
3371 if (win
->floatmaxed
)
3375 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
3381 case SWM_ARG_ID_WIDTHSHRINK
:
3382 win
->g
.w
-= SWM_RESIZE_STEPS
;
3385 case SWM_ARG_ID_WIDTHGROW
:
3386 win
->g
.w
+= SWM_RESIZE_STEPS
;
3389 case SWM_ARG_ID_HEIGHTSHRINK
:
3390 win
->g
.h
-= SWM_RESIZE_STEPS
;
3393 case SWM_ARG_ID_HEIGHTGROW
:
3394 win
->g
.h
+= SWM_RESIZE_STEPS
;
3401 resize_window(win
, 0);
3402 store_float_geom(win
,r
);
3406 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3407 drain_enter_notify();
3409 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
3410 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
3413 /* place pointer at bottom left corner or nearest point inside r */
3414 if ( win
->g
.x
+ win
->g
.w
< r
->g
.x
+ r
->g
.w
- 1)
3415 relx
= win
->g
.w
- 1;
3417 relx
= r
->g
.x
+ r
->g
.w
- win
->g
.x
- 1;
3419 if ( win
->g
.y
+ win
->g
.h
< r
->g
.y
+ r
->g
.h
- 1)
3420 rely
= win
->g
.h
- 1;
3422 rely
= r
->g
.y
+ r
->g
.h
- win
->g
.y
- 1;
3424 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, relx
, rely
);
3426 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
3427 SubstructureRedirectMask
, &ev
);
3429 case ConfigureRequest
:
3432 handler
[ev
.type
](&ev
);
3435 /* do not allow resize outside of region */
3436 if ( ev
.xmotion
.x_root
< r
->g
.x
||
3437 ev
.xmotion
.x_root
> r
->g
.x
+ r
->g
.w
- 1 ||
3438 ev
.xmotion
.y_root
< r
->g
.y
||
3439 ev
.xmotion
.y_root
> r
->g
.y
+ r
->g
.h
- 1)
3442 if (ev
.xmotion
.x
<= 1)
3444 if (ev
.xmotion
.y
<= 1)
3446 win
->g
.w
= ev
.xmotion
.x
+ 1;
3447 win
->g
.h
= ev
.xmotion
.y
+ 1;
3449 /* not free, don't sync more than 120 times / second */
3450 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
3451 time
= ev
.xmotion
.time
;
3452 XSync(display
, False
);
3453 resize_window(win
, args
->id
);
3457 } while (ev
.type
!= ButtonRelease
);
3459 XSync(display
, False
);
3460 resize_window(win
, args
->id
);
3462 store_float_geom(win
,r
);
3464 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
- 1,
3466 XUngrabPointer(display
, CurrentTime
);
3469 drain_enter_notify();
3473 resize_step(struct swm_region
*r
, union arg
*args
)
3475 struct ws_win
*win
= NULL
;
3477 if (r
&& r
->ws
&& r
->ws
->focus
)
3487 move_window(struct ws_win
*win
)
3491 struct swm_region
*r
;
3493 r
= root_to_region(win
->wa
.root
);
3494 bzero(&wc
, sizeof wc
);
3498 wc
.border_width
= border_width
;
3500 DNPRINTF(SWM_D_STACK
, "move_window: win %lu x %d y %d w %d h %d\n",
3501 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
3503 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3506 #define SWM_MOVE_STEPS (50)
3509 move(struct ws_win
*win
, union arg
*args
)
3514 struct swm_region
*r
= NULL
;
3520 DNPRINTF(SWM_D_MOUSE
, "move: win %lu floating %d trans %lu\n",
3521 win
->id
, win
->floating
, win
->transient
);
3523 /* in max_stack mode should only move transients */
3524 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
3528 if (win
->floating
== 0 && !win
->transient
) {
3529 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
3532 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
3539 case SWM_ARG_ID_MOVELEFT
:
3540 win
->g
.x
-= (SWM_MOVE_STEPS
- border_width
);
3543 case SWM_ARG_ID_MOVERIGHT
:
3544 win
->g
.x
+= (SWM_MOVE_STEPS
- border_width
);
3547 case SWM_ARG_ID_MOVEUP
:
3548 win
->g
.y
-= (SWM_MOVE_STEPS
- border_width
);
3551 case SWM_ARG_ID_MOVEDOWN
:
3552 win
->g
.y
+= (SWM_MOVE_STEPS
- border_width
);
3560 store_float_geom(win
,r
);
3565 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
3566 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
3568 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
3570 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
3571 SubstructureRedirectMask
, &ev
);
3573 case ConfigureRequest
:
3576 handler
[ev
.type
](&ev
);
3579 /* don't allow to move window origin out of region */
3580 if ( ev
.xmotion
.x_root
< r
->g
.x
||
3581 ev
.xmotion
.x_root
> r
->g
.x
+ r
->g
.w
- 1 ||
3582 ev
.xmotion
.y_root
< r
->g
.y
||
3583 ev
.xmotion
.y_root
> r
->g
.y
+ r
->g
.h
- 1)
3586 win
->g
.x
= ev
.xmotion
.x_root
- border_width
;
3587 win
->g
.y
= ev
.xmotion
.y_root
- border_width
;
3589 /* not free, don't sync more than 120 times / second */
3590 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
3591 time
= ev
.xmotion
.time
;
3592 XSync(display
, False
);
3597 } while (ev
.type
!= ButtonRelease
);
3599 XSync(display
, False
);
3602 store_float_geom(win
,r
);
3603 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
3604 XUngrabPointer(display
, CurrentTime
);
3607 drain_enter_notify();
3611 move_step(struct swm_region
*r
, union arg
*args
)
3613 struct ws_win
*win
= NULL
;
3615 if (r
&& r
->ws
&& r
->ws
->focus
)
3620 if (!(win
->transient
!= 0 || win
->floating
!= 0))
3627 /* user/key callable function IDs */
3694 kf_dumpwins
, /* MUST BE LAST */
3698 /* key definitions */
3700 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
3705 legacyfunc(struct swm_region
*r
, union arg
*args
)
3710 char name
[SWM_FUNCNAME_LEN
];
3711 void (*func
)(struct swm_region
*r
, union arg
*);
3713 } keyfuncs
[kf_invalid
+ 1] = {
3714 /* name function argument */
3715 { "cycle_layout", cycle_layout
, {0} },
3716 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
3717 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
3718 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
3719 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
3720 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
3721 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
3722 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
3723 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
3724 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
3725 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
3726 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
3727 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
3728 { "spawn_term", spawnterm
, {.argv
= spawn_term
} },
3729 { "spawn_menu", legacyfunc
, {0} },
3730 { "quit", quit
, {0} },
3731 { "restart", restart
, {0} },
3732 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
3733 { "ws_1", switchws
, {.id
= 0} },
3734 { "ws_2", switchws
, {.id
= 1} },
3735 { "ws_3", switchws
, {.id
= 2} },
3736 { "ws_4", switchws
, {.id
= 3} },
3737 { "ws_5", switchws
, {.id
= 4} },
3738 { "ws_6", switchws
, {.id
= 5} },
3739 { "ws_7", switchws
, {.id
= 6} },
3740 { "ws_8", switchws
, {.id
= 7} },
3741 { "ws_9", switchws
, {.id
= 8} },
3742 { "ws_10", switchws
, {.id
= 9} },
3743 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
3744 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
3745 { "ws_prior", priorws
, {0} },
3746 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
3747 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
3748 { "mvws_1", send_to_ws
, {.id
= 0} },
3749 { "mvws_2", send_to_ws
, {.id
= 1} },
3750 { "mvws_3", send_to_ws
, {.id
= 2} },
3751 { "mvws_4", send_to_ws
, {.id
= 3} },
3752 { "mvws_5", send_to_ws
, {.id
= 4} },
3753 { "mvws_6", send_to_ws
, {.id
= 5} },
3754 { "mvws_7", send_to_ws
, {.id
= 6} },
3755 { "mvws_8", send_to_ws
, {.id
= 7} },
3756 { "mvws_9", send_to_ws
, {.id
= 8} },
3757 { "mvws_10", send_to_ws
, {.id
= 9} },
3758 { "bar_toggle", bar_toggle
, {0} },
3759 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
3760 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
3761 { "screenshot_all", legacyfunc
, {0} },
3762 { "screenshot_wind", legacyfunc
, {0} },
3763 { "float_toggle", floating_toggle
,{0} },
3764 { "version", version
, {0} },
3765 { "spawn_lock", legacyfunc
, {0} },
3766 { "spawn_initscr", legacyfunc
, {0} },
3767 { "spawn_custom", dummykeyfunc
, {0} },
3768 { "iconify", iconify
, {0} },
3769 { "uniconify", uniconify
, {0} },
3770 { "raise_toggle", raise_toggle
, {0} },
3771 { "button2", pressbutton
, {2} },
3772 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
3773 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
3774 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
3775 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
3776 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
3777 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
3778 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
3779 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
3780 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
3781 { "invalid key func", NULL
, {0} },
3786 enum keyfuncid funcid
;
3789 int keys_size
= 0, keys_length
= 0;
3790 struct key
*keys
= NULL
;
3793 enum { client_click
, root_click
};
3795 unsigned int action
;
3797 unsigned int button
;
3798 void (*func
)(struct ws_win
*, union arg
*);
3801 /* action key mouse button func args */
3802 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
3803 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
3804 { client_click
, MODKEY
, Button1
, move
, {0} },
3808 update_modkey(unsigned int mod
)
3813 for (i
= 0; i
< keys_length
; i
++)
3814 if (keys
[i
].mod
& ShiftMask
)
3815 keys
[i
].mod
= mod
| ShiftMask
;
3819 for (i
= 0; i
< LENGTH(buttons
); i
++)
3820 if (buttons
[i
].mask
& ShiftMask
)
3821 buttons
[i
].mask
= mod
| ShiftMask
;
3823 buttons
[i
].mask
= mod
;
3833 int spawns_size
= 0, spawns_length
= 0;
3834 struct spawn_prog
*spawns
= NULL
;
3837 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
3840 struct spawn_prog
*prog
= NULL
;
3842 char *ap
, **real_args
;
3844 DNPRINTF(SWM_D_SPAWN
, "spawn_expand %s\n", spawn_name
);
3847 for (i
= 0; i
< spawns_length
; i
++) {
3848 if (!strcasecmp(spawn_name
, spawns
[i
].name
))
3852 fprintf(stderr
, "spawn_custom: program %s not found\n",
3857 /* make room for expanded args */
3858 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
3859 err(1, "spawn_custom: calloc real_args");
3861 /* expand spawn_args into real_args */
3862 for (i
= 0; i
< prog
->argc
; i
++) {
3864 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg = %s\n", ap
);
3865 if (!strcasecmp(ap
, "$bar_border")) {
3867 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
3869 err(1, "spawn_custom border color");
3870 } else if (!strcasecmp(ap
, "$bar_color")) {
3872 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
3874 err(1, "spawn_custom bar color");
3875 } else if (!strcasecmp(ap
, "$bar_font")) {
3876 if ((real_args
[i
] = strdup(bar_fonts
[bar_fidx
]))
3878 err(1, "spawn_custom bar fonts");
3879 } else if (!strcasecmp(ap
, "$bar_font_color")) {
3881 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
3883 err(1, "spawn_custom color font");
3884 } else if (!strcasecmp(ap
, "$color_focus")) {
3886 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
3888 err(1, "spawn_custom color focus");
3889 } else if (!strcasecmp(ap
, "$color_unfocus")) {
3891 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
3893 err(1, "spawn_custom color unfocus");
3895 /* no match --> copy as is */
3896 if ((real_args
[i
] = strdup(ap
)) == NULL
)
3897 err(1, "spawn_custom strdup(ap)");
3899 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg = %s\n",
3904 if ((swm_debug
& SWM_D_SPAWN
) != 0) {
3905 fprintf(stderr
, "spawn_custom: result = ");
3906 for (i
= 0; i
< prog
->argc
; i
++)
3907 fprintf(stderr
, "\"%s\" ", real_args
[i
]);
3908 fprintf(stderr
, "\n");
3911 *ret_args
= real_args
;
3912 return (prog
->argc
);
3916 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
3922 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
3926 spawn(r
->ws
->idx
, &a
, 1);
3928 for (i
= 0; i
< spawn_argc
; i
++)
3934 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
3940 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
3944 if (pipe(select_list_pipe
) == -1)
3945 err(1, "pipe error");
3946 if (pipe(select_resp_pipe
) == -1)
3947 err(1, "pipe error");
3949 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
3950 err(1, "could not disable SIGPIPE");
3951 switch (*pid
= fork()) {
3953 err(1, "cannot fork");
3956 if (dup2(select_list_pipe
[0], 0) == -1)
3958 if (dup2(select_resp_pipe
[1], 1) == -1)
3960 close(select_list_pipe
[1]);
3961 close(select_resp_pipe
[0]);
3962 spawn(r
->ws
->idx
, &a
, 0);
3964 default: /* parent */
3965 close(select_list_pipe
[0]);
3966 close(select_resp_pipe
[1]);
3970 for (i
= 0; i
< spawn_argc
; i
++)
3976 setspawn(struct spawn_prog
*prog
)
3980 if (prog
== NULL
|| prog
->name
== NULL
)
3984 for (i
= 0; i
< spawns_length
; i
++) {
3985 if (!strcmp(spawns
[i
].name
, prog
->name
)) {
3987 if (prog
->argv
== NULL
) {
3989 DNPRINTF(SWM_D_SPAWN
,
3990 "setspawn: delete #%d %s\n",
3992 free(spawns
[i
].name
);
3993 for (j
= 0; j
< spawns
[i
].argc
; j
++)
3994 free(spawns
[i
].argv
[j
]);
3995 free(spawns
[i
].argv
);
3996 j
= spawns_length
- 1;
3998 spawns
[i
] = spawns
[j
];
4003 DNPRINTF(SWM_D_SPAWN
,
4004 "setspawn: replace #%d %s\n",
4006 free(spawns
[i
].name
);
4007 for (j
= 0; j
< spawns
[i
].argc
; j
++)
4008 free(spawns
[i
].argv
[j
]);
4009 free(spawns
[i
].argv
);
4012 /* found case handled */
4018 if (prog
->argv
== NULL
) {
4020 "error: setspawn: cannot find program %s", prog
->name
);
4025 /* not found: add */
4026 if (spawns_size
== 0 || spawns
== NULL
) {
4028 DNPRINTF(SWM_D_SPAWN
, "setspawn: init list %d\n", spawns_size
);
4029 spawns
= malloc((size_t)spawns_size
*
4030 sizeof(struct spawn_prog
));
4031 if (spawns
== NULL
) {
4032 fprintf(stderr
, "setspawn: malloc failed\n");
4036 } else if (spawns_length
== spawns_size
) {
4038 DNPRINTF(SWM_D_SPAWN
, "setspawn: grow list %d\n", spawns_size
);
4039 spawns
= realloc(spawns
, (size_t)spawns_size
*
4040 sizeof(struct spawn_prog
));
4041 if (spawns
== NULL
) {
4042 fprintf(stderr
, "setspawn: realloc failed\n");
4048 if (spawns_length
< spawns_size
) {
4049 DNPRINTF(SWM_D_SPAWN
, "setspawn: add #%d %s\n",
4050 spawns_length
, prog
->name
);
4051 i
= spawns_length
++;
4054 fprintf(stderr
, "spawns array problem?\n");
4055 if (spawns
== NULL
) {
4056 fprintf(stderr
, "spawns array is NULL!\n");
4064 setconfspawn(char *selector
, char *value
, int flags
)
4066 struct spawn_prog
*prog
;
4067 char *vp
, *cp
, *word
;
4069 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
4070 if ((prog
= calloc(1, sizeof *prog
)) == NULL
)
4071 err(1, "setconfspawn: calloc prog");
4072 prog
->name
= strdup(selector
);
4073 if (prog
->name
== NULL
)
4074 err(1, "setconfspawn prog->name");
4075 if ((cp
= vp
= strdup(value
)) == NULL
)
4076 err(1, "setconfspawn: strdup(value) ");
4077 while ((word
= strsep(&cp
, " \t")) != NULL
) {
4078 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: arg [%s]\n", word
);
4080 cp
+= (long)strspn(cp
, " \t");
4081 if (strlen(word
) > 0) {
4083 if ((prog
->argv
= realloc(prog
->argv
,
4084 prog
->argc
* sizeof(char *))) == NULL
)
4085 err(1, "setconfspawn: realloc");
4086 if ((prog
->argv
[prog
->argc
- 1] = strdup(word
)) == NULL
)
4087 err(1, "setconfspawn: strdup");
4094 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
4101 setconfspawn("term", "xterm", 0);
4102 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4103 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4104 setconfspawn("lock", "xlock", 0);
4105 setconfspawn("initscr", "initscreen.sh", 0);
4106 setconfspawn("menu", "dmenu_run"
4109 " -nf $bar_font_color"
4111 " -sf $bar_color", 0);
4112 setconfspawn("uniconify", "dmenu"
4116 " -nf $bar_font_color"
4118 " -sf $bar_color", 0);
4122 #define SWM_MODNAME_SIZE 32
4123 #define SWM_KEY_WS "\n+ \t"
4125 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
4129 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
4130 if (mod
== NULL
|| ks
== NULL
) {
4131 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
4134 if (keystr
== NULL
|| strlen(keystr
) == 0) {
4135 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
4141 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
4142 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
4144 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
4145 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
4147 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
4149 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
4151 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
4153 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
4155 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
4157 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
4158 *mod
|= ControlMask
;
4160 *ks
= XStringToKeysym(name
);
4161 XConvertCase(*ks
, ks
, &uks
);
4162 if (ks
== NoSymbol
) {
4164 "parsekeys: invalid key %s\n",
4170 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
4175 strdupsafe(char *str
)
4180 return (strdup(str
));
4184 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
4188 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
4189 keyfuncs
[kfid
].name
, spawn_name
);
4191 for (i
= 0; i
< keys_length
; i
++) {
4192 if (keys
[i
].mod
== mod
&& keys
[i
].keysym
== ks
) {
4193 if (kfid
== kf_invalid
) {
4196 "setkeybinding: delete #%d %s\n",
4197 i
, keyfuncs
[keys
[i
].funcid
].name
);
4198 free(keys
[i
].spawn_name
);
4199 j
= keys_length
- 1;
4203 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4206 /* found: replace */
4208 "setkeybinding: replace #%d %s %s\n",
4209 i
, keyfuncs
[keys
[i
].funcid
].name
,
4211 free(keys
[i
].spawn_name
);
4213 keys
[i
].keysym
= ks
;
4214 keys
[i
].funcid
= kfid
;
4215 keys
[i
].spawn_name
= strdupsafe(spawn_name
);
4216 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4221 if (kfid
== kf_invalid
) {
4223 "error: setkeybinding: cannot find mod/key combination");
4224 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4227 /* not found: add */
4228 if (keys_size
== 0 || keys
== NULL
) {
4230 DNPRINTF(SWM_D_KEY
, "setkeybinding: init list %d\n", keys_size
);
4231 keys
= malloc((size_t)keys_size
* sizeof(struct key
));
4233 fprintf(stderr
, "malloc failed\n");
4237 } else if (keys_length
== keys_size
) {
4239 DNPRINTF(SWM_D_KEY
, "setkeybinding: grow list %d\n", keys_size
);
4240 keys
= realloc(keys
, (size_t)keys_size
* sizeof(struct key
));
4242 fprintf(stderr
, "realloc failed\n");
4247 if (keys_length
< keys_size
) {
4249 DNPRINTF(SWM_D_KEY
, "setkeybinding: add #%d %s %s\n",
4250 j
, keyfuncs
[kfid
].name
, spawn_name
);
4252 keys
[j
].keysym
= ks
;
4253 keys
[j
].funcid
= kfid
;
4254 keys
[j
].spawn_name
= strdupsafe(spawn_name
);
4256 fprintf(stderr
, "keys array problem?\n");
4258 fprintf(stderr
, "keys array problem\n");
4262 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4266 setconfbinding(char *selector
, char *value
, int flags
)
4268 enum keyfuncid kfid
;
4272 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
4273 if (selector
== NULL
) {
4274 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
4275 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4277 setkeybinding(mod
, ks
, kfid
, NULL
);
4282 /* search by key function name */
4283 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
4284 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
4285 SWM_FUNCNAME_LEN
) == 0) {
4286 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4288 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4289 setkeybinding(mod
, ks
, kfid
, NULL
);
4295 /* search by custom spawn name */
4296 for (i
= 0; i
< spawns_length
; i
++) {
4297 if (strcasecmp(selector
, spawns
[i
].name
) == 0) {
4298 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4300 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4301 setkeybinding(mod
, ks
, kf_spawn_custom
,
4308 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
4315 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
4316 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
4317 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
4318 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
4319 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
4320 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
4321 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
4322 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
4323 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
4324 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
4325 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
4326 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
4327 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
4328 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_term
, NULL
);
4329 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
4330 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
4331 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
4332 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
4333 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
4334 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
4335 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
4336 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
4337 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
4338 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
4339 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
4340 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
4341 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
4342 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
4343 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
4344 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
4345 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
4346 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
4347 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
4348 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
4349 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
4350 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
4351 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
4352 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
4353 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
4354 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
4355 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
4356 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
4357 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
4358 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
4359 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
4360 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
4361 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
4362 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
4363 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
4364 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
4365 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
4366 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
4367 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
4368 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
4369 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
4370 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
4371 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
4372 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
4373 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
4374 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
4375 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
4376 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
4377 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
4378 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
4379 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
4380 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
4382 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
4387 updatenumlockmask(void)
4390 XModifierKeymap
*modmap
;
4392 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
4394 modmap
= XGetModifierMapping(display
);
4395 for (i
= 0; i
< 8; i
++)
4396 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
4397 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
4398 == XKeysymToKeycode(display
, XK_Num_Lock
))
4399 numlockmask
= (1 << i
);
4401 XFreeModifiermap(modmap
);
4407 unsigned int i
, j
, k
;
4409 unsigned int modifiers
[] =
4410 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
4412 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
4413 updatenumlockmask();
4415 for (k
= 0; k
< ScreenCount(display
); k
++) {
4416 if (TAILQ_EMPTY(&screens
[k
].rl
))
4418 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
4419 for (i
= 0; i
< keys_length
; i
++) {
4420 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
4421 for (j
= 0; j
< LENGTH(modifiers
); j
++)
4422 XGrabKey(display
, code
,
4423 keys
[i
].mod
| modifiers
[j
],
4424 screens
[k
].root
, True
,
4425 GrabModeAsync
, GrabModeAsync
);
4431 grabbuttons(struct ws_win
*win
, int focused
)
4434 unsigned int modifiers
[] =
4435 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
4437 updatenumlockmask();
4438 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
4440 for (i
= 0; i
< LENGTH(buttons
); i
++)
4441 if (buttons
[i
].action
== client_click
)
4442 for (j
= 0; j
< LENGTH(modifiers
); j
++)
4443 XGrabButton(display
, buttons
[i
].button
,
4444 buttons
[i
].mask
| modifiers
[j
],
4445 win
->id
, False
, BUTTONMASK
,
4446 GrabModeAsync
, GrabModeSync
, None
,
4449 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
4450 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
4453 const char *quirkname
[] = {
4454 "NONE", /* config string for "no value" */
4463 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4464 #define SWM_Q_WS "\n|+ \t"
4466 parsequirks(char *qstr
, unsigned long *quirk
)
4476 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
4478 cp
+= (long)strspn(cp
, SWM_Q_WS
);
4479 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
4480 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
4481 DNPRINTF(SWM_D_QUIRK
,
4482 "parsequirks: %s\n", name
);
4487 *quirk
|= 1 << (i
-1);
4491 if (i
>= LENGTH(quirkname
)) {
4492 DNPRINTF(SWM_D_QUIRK
,
4493 "parsequirks: invalid quirk [%s]\n", name
);
4501 setquirk(const char *class, const char *name
, const int quirk
)
4506 for (i
= 0; i
< quirks_length
; i
++) {
4507 if (!strcmp(quirks
[i
].class, class) &&
4508 !strcmp(quirks
[i
].name
, name
)) {
4511 DNPRINTF(SWM_D_QUIRK
,
4512 "setquirk: delete #%d %s:%s\n",
4513 i
, quirks
[i
].class, quirks
[i
].name
);
4514 free(quirks
[i
].class);
4515 free(quirks
[i
].name
);
4516 j
= quirks_length
- 1;
4518 quirks
[i
] = quirks
[j
];
4522 /* found: replace */
4523 DNPRINTF(SWM_D_QUIRK
,
4524 "setquirk: replace #%d %s:%s\n",
4525 i
, quirks
[i
].class, quirks
[i
].name
);
4526 free(quirks
[i
].class);
4527 free(quirks
[i
].name
);
4528 quirks
[i
].class = strdup(class);
4529 quirks
[i
].name
= strdup(name
);
4530 quirks
[i
].quirk
= quirk
;
4537 "error: setquirk: cannot find class/name combination");
4540 /* not found: add */
4541 if (quirks_size
== 0 || quirks
== NULL
) {
4543 DNPRINTF(SWM_D_QUIRK
, "setquirk: init list %d\n", quirks_size
);
4544 quirks
= malloc((size_t)quirks_size
* sizeof(struct quirk
));
4545 if (quirks
== NULL
) {
4546 fprintf(stderr
, "setquirk: malloc failed\n");
4550 } else if (quirks_length
== quirks_size
) {
4552 DNPRINTF(SWM_D_QUIRK
, "setquirk: grow list %d\n", quirks_size
);
4553 quirks
= realloc(quirks
,
4554 (size_t)quirks_size
* sizeof(struct quirk
));
4555 if (quirks
== NULL
) {
4556 fprintf(stderr
, "setquirk: realloc failed\n");
4561 if (quirks_length
< quirks_size
) {
4562 DNPRINTF(SWM_D_QUIRK
, "setquirk: add %d\n", quirks_length
);
4563 j
= quirks_length
++;
4564 quirks
[j
].class = strdup(class);
4565 quirks
[j
].name
= strdup(name
);
4566 quirks
[j
].quirk
= quirk
;
4568 fprintf(stderr
, "quirks array problem?\n");
4569 if (quirks
== NULL
) {
4570 fprintf(stderr
, "quirks array problem!\n");
4577 setconfquirk(char *selector
, char *value
, int flags
)
4579 char *cp
, *class, *name
;
4581 unsigned long quirks
;
4582 if (selector
== NULL
)
4584 if ((cp
= strchr(selector
, ':')) == NULL
)
4589 if ((retval
= parsequirks(value
, &quirks
)) == 0)
4590 setquirk(class, name
, quirks
);
4597 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
4598 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
4599 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
4600 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
4601 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
4602 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
4603 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
4604 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
4605 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
4606 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
4607 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
4608 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
4609 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
4612 /* conf file stuff */
4613 #define SWM_CONF_FILE "scrotwm.conf"
4615 enum { SWM_S_BAR_DELAY
, SWM_S_BAR_ENABLED
, SWM_S_BAR_BORDER_WIDTH
,
4616 SWM_S_STACK_ENABLED
, SWM_S_CLOCK_ENABLED
, SWM_S_CLOCK_FORMAT
,
4617 SWM_S_CYCLE_EMPTY
, SWM_S_CYCLE_VISIBLE
, SWM_S_SS_ENABLED
,
4618 SWM_S_TERM_WIDTH
, SWM_S_TITLE_CLASS_ENABLED
,
4619 SWM_S_TITLE_NAME_ENABLED
, SWM_S_WINDOW_NAME_ENABLED
, SWM_S_URGENT_ENABLED
,
4620 SWM_S_FOCUS_MODE
, SWM_S_DISABLE_BORDER
, SWM_S_BORDER_WIDTH
,
4621 SWM_S_BAR_FONT
, SWM_S_BAR_ACTION
, SWM_S_SPAWN_TERM
,
4622 SWM_S_SS_APP
, SWM_S_DIALOG_RATIO
, SWM_S_BAR_AT_BOTTOM
,
4623 SWM_S_VERBOSE_LAYOUT
4627 setconfvalue(char *selector
, char *value
, int flags
)
4631 case SWM_S_BAR_DELAY
:
4632 bar_delay
= atoi(value
);
4634 case SWM_S_BAR_ENABLED
:
4635 bar_enabled
= atoi(value
);
4637 case SWM_S_BAR_BORDER_WIDTH
:
4638 bar_border_width
= atoi(value
);
4640 case SWM_S_BAR_AT_BOTTOM
:
4641 bar_at_bottom
= atoi(value
);
4643 case SWM_S_STACK_ENABLED
:
4644 stack_enabled
= atoi(value
);
4646 case SWM_S_CLOCK_ENABLED
:
4647 clock_enabled
= atoi(value
);
4649 case SWM_S_CLOCK_FORMAT
:
4650 #ifndef SWM_DENY_CLOCK_FORMAT
4652 if ((clock_format
= strdup(value
)) == NULL
)
4653 err(1, "setconfvalue: clock_format");
4656 case SWM_S_CYCLE_EMPTY
:
4657 cycle_empty
= atoi(value
);
4659 case SWM_S_CYCLE_VISIBLE
:
4660 cycle_visible
= atoi(value
);
4662 case SWM_S_SS_ENABLED
:
4663 ss_enabled
= atoi(value
);
4665 case SWM_S_TERM_WIDTH
:
4666 term_width
= atoi(value
);
4668 case SWM_S_TITLE_CLASS_ENABLED
:
4669 title_class_enabled
= atoi(value
);
4671 case SWM_S_WINDOW_NAME_ENABLED
:
4672 window_name_enabled
= atoi(value
);
4674 case SWM_S_TITLE_NAME_ENABLED
:
4675 title_name_enabled
= atoi(value
);
4677 case SWM_S_URGENT_ENABLED
:
4678 urgent_enabled
= atoi(value
);
4680 case SWM_S_FOCUS_MODE
:
4681 if (!strcmp(value
, "default"))
4682 focus_mode
= SWM_FOCUS_DEFAULT
;
4683 else if (!strcmp(value
, "follow_cursor"))
4684 focus_mode
= SWM_FOCUS_FOLLOW
;
4685 else if (!strcmp(value
, "synergy"))
4686 focus_mode
= SWM_FOCUS_SYNERGY
;
4688 err(1, "focus_mode");
4690 case SWM_S_DISABLE_BORDER
:
4691 disable_border
= atoi(value
);
4693 case SWM_S_BORDER_WIDTH
:
4694 border_width
= atoi(value
);
4696 case SWM_S_BAR_FONT
:
4698 if ((bar_fonts
[0] = strdup(value
)) == NULL
)
4699 err(1, "setconfvalue: bar_font");
4701 case SWM_S_BAR_ACTION
:
4703 if ((bar_argv
[0] = strdup(value
)) == NULL
)
4704 err(1, "setconfvalue: bar_action");
4706 case SWM_S_SPAWN_TERM
:
4707 free(spawn_term
[0]);
4708 if ((spawn_term
[0] = strdup(value
)) == NULL
)
4709 err(1, "setconfvalue: spawn_term");
4713 case SWM_S_DIALOG_RATIO
:
4714 dialog_ratio
= atof(value
);
4715 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
4718 case SWM_S_VERBOSE_LAYOUT
:
4719 verbose_layout
= atoi(value
);
4720 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
4722 layouts
[i
].l_string
= fancy_stacker
;
4724 layouts
[i
].l_string
= plain_stacker
;
4734 setconfmodkey(char *selector
, char *value
, int flags
)
4736 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
4737 update_modkey(Mod1Mask
);
4738 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
4739 update_modkey(Mod2Mask
);
4740 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
4741 update_modkey(Mod3Mask
);
4742 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
4743 update_modkey(Mod4Mask
);
4750 setconfcolor(char *selector
, char *value
, int flags
)
4752 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
4757 setconfregion(char *selector
, char *value
, int flags
)
4759 custom_region(value
);
4764 setautorun(char *selector
, char *value
, int flags
)
4774 if (getenv("SWM_STARTED"))
4778 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
4779 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'\n");
4781 if (ws_id
< 0 || ws_id
>= SWM_WS_MAX
)
4782 errx(1, "autorun: invalid workspace %d\n", ws_id
+ 1);
4785 * This is a little intricate
4787 * If the pid already exists we simply reuse it because it means it was
4788 * used before AND not claimed by manage_window. We get away with
4789 * altering it in the parent after INSERT because this can not be a race
4792 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
4795 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
4797 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
4798 err(1, "setautorun: realloc");
4799 a
.argv
[argc
- 1] = ap
;
4802 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
4803 err(1, "setautorun: realloc");
4804 a
.argv
[argc
] = NULL
;
4806 if ((pid
= fork()) == 0) {
4807 spawn(ws_id
, &a
, 1);
4816 p
= calloc(1, sizeof *p
);
4819 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
4829 setlayout(char *selector
, char *value
, int flags
)
4831 int ws_id
, i
, x
, mg
, ma
, si
, raise
;
4832 int st
= SWM_V_STACK
;
4834 struct workspace
*ws
;
4836 if (getenv("SWM_STARTED"))
4840 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
4841 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
4842 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4843 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4846 if (ws_id
< 0 || ws_id
>= SWM_WS_MAX
)
4847 errx(1, "layout: invalid workspace %d\n", ws_id
+ 1);
4849 if (!strcasecmp(s
, "vertical"))
4851 else if (!strcasecmp(s
, "horizontal"))
4853 else if (!strcasecmp(s
, "fullscreen"))
4856 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4857 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4860 for (i
= 0; i
< ScreenCount(display
); i
++) {
4861 ws
= (struct workspace
*)&screens
[i
].ws
;
4862 ws
[ws_id
].cur_layout
= &layouts
[st
];
4864 ws
[ws_id
].always_raise
= raise
;
4865 if (st
== SWM_MAX_STACK
)
4869 for (x
= 0; x
< abs(mg
); x
++) {
4870 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
4871 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
4872 SWM_ARG_ID_MASTERSHRINK
);
4876 for (x
= 0; x
< abs(ma
); x
++) {
4877 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
4878 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
4879 SWM_ARG_ID_MASTERDEL
);
4883 for (x
= 0; x
< abs(si
); x
++) {
4884 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
4885 si
>= 0 ? SWM_ARG_ID_STACKINC
:
4886 SWM_ARG_ID_STACKDEC
);
4894 /* config options */
4895 struct config_option
{
4897 int (*func
)(char*, char*, int);
4900 struct config_option configopt
[] = {
4901 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
4902 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
4903 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
4904 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
4905 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
4906 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
4907 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
4908 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
4909 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
4910 { "bind", setconfbinding
, 0 },
4911 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
4912 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
4913 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
4914 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
4915 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
4916 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
4917 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
4918 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
4919 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
4920 { "modkey", setconfmodkey
, 0 },
4921 { "program", setconfspawn
, 0 },
4922 { "quirk", setconfquirk
, 0 },
4923 { "region", setconfregion
, 0 },
4924 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
4925 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
4926 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
4927 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
4928 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
4929 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
4930 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
4931 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
4932 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
4933 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
4934 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
4935 { "autorun", setautorun
, 0 },
4936 { "layout", setlayout
, 0 },
4941 conf_load(char *filename
)
4944 char *line
, *cp
, *optsub
, *optval
;
4945 size_t linelen
, lineno
= 0;
4946 int wordlen
, i
, optind
;
4947 struct config_option
*opt
;
4949 DNPRINTF(SWM_D_CONF
, "conf_load begin\n");
4951 if (filename
== NULL
) {
4952 fprintf(stderr
, "conf_load: no filename\n");
4955 if ((config
= fopen(filename
, "r")) == NULL
) {
4956 warn("conf_load: fopen");
4960 while (!feof(config
)) {
4961 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
4964 err(1, "%s", filename
);
4969 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
4970 if (cp
[0] == '\0') {
4975 /* get config option */
4976 wordlen
= strcspn(cp
, "=[ \t\n");
4978 warnx("%s: line %zd: no option found",
4983 for (i
= 0; i
< LENGTH(configopt
); i
++) {
4984 opt
= &configopt
[i
];
4985 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
4986 strlen(opt
->optname
) == wordlen
) {
4992 warnx("%s: line %zd: unknown option %.*s",
4993 filename
, lineno
, wordlen
, cp
);
4997 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
4998 /* get [selector] if any */
5002 wordlen
= strcspn(cp
, "]");
5005 warnx("%s: line %zd: syntax error",
5009 asprintf(&optsub
, "%.*s", wordlen
, cp
);
5012 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
5014 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
5016 optval
= strdup(cp
);
5017 /* call function to deal with it all */
5018 if (configopt
[optind
].func(optsub
, optval
,
5019 configopt
[optind
].funcflags
) != 0) {
5020 fprintf(stderr
, "%s line %zd: %s\n",
5021 filename
, lineno
, line
);
5022 errx(1, "%s: line %zd: invalid data for %s",
5023 filename
, lineno
, configopt
[optind
].optname
);
5031 DNPRINTF(SWM_D_CONF
, "conf_load end\n");
5037 set_child_transient(struct ws_win
*win
, Window
*trans
)
5039 struct ws_win
*parent
, *w
;
5040 XWMHints
*wmh
= NULL
;
5041 struct swm_region
*r
;
5042 struct workspace
*ws
;
5044 parent
= find_window(win
->transient
);
5046 parent
->child_trans
= win
;
5048 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
5049 " for %lu trans %lu\n", win
->id
, win
->transient
);
5051 if (win
->hints
== NULL
) {
5052 fprintf(stderr
, "no hints for %lu\n", win
->id
);
5056 r
= root_to_region(win
->wa
.root
);
5058 /* parent doen't exist in our window list */
5059 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5063 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
5064 fprintf(stderr
, "can't get hints for %lu\n",
5069 if (win
->hints
->window_group
!= wmh
->window_group
)
5072 w
->child_trans
= win
;
5073 win
->transient
= w
->id
;
5075 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
5076 "transient to %lu\n", win
->transient
);
5086 window_get_pid(Window win
)
5088 Atom actual_type_return
;
5089 int actual_format_return
= 0;
5090 unsigned long nitems_return
= 0;
5091 unsigned long bytes_after_return
= 0;
5095 unsigned char *prop
= NULL
;
5097 if (XGetWindowProperty(display
, win
,
5098 XInternAtom(display
, "_NET_WM_PID", False
), 0, 1, False
,
5099 XA_CARDINAL
, &actual_type_return
, &actual_format_return
,
5100 &nitems_return
, &bytes_after_return
,
5101 (unsigned char**)(void*)&pid
) != Success
)
5103 if (actual_type_return
!= XA_CARDINAL
)
5114 if (XGetWindowProperty(display
, win
,
5115 XInternAtom(display
, "_SWM_PID", False
), 0, SWM_PROPLEN
, False
,
5116 XA_STRING
, &actual_type_return
, &actual_format_return
,
5117 &nitems_return
, &bytes_after_return
, &prop
) != Success
)
5119 if (actual_type_return
!= XA_STRING
)
5124 ret
= strtonum((const char *)prop
, 0, UINT_MAX
, &errstr
);
5125 /* ignore error because strtonum returns 0 anyway */
5132 manage_window(Window id
)
5135 struct workspace
*ws
;
5136 struct ws_win
*win
, *ww
;
5137 int format
, i
, ws_idx
, n
, border_me
= 0;
5138 unsigned long nitems
, bytes
;
5139 Atom ws_idx_atom
= 0, type
;
5140 Atom
*prot
= NULL
, *pp
;
5141 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
5142 struct swm_region
*r
;
5148 if ((win
= find_window(id
)) != NULL
)
5149 return (win
); /* already being managed */
5151 /* see if we are on the unmanaged list */
5152 if ((win
= find_unmanaged_window(id
)) != NULL
) {
5153 DNPRINTF(SWM_D_MISC
, "manage previously unmanaged window "
5155 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5156 if (win
->transient
) {
5157 set_child_transient(win
, &trans
);
5158 } if (trans
&& (ww
= find_window(trans
)))
5159 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
5161 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
5162 ewmh_update_actions(win
);
5166 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
5167 errx(1, "calloc: failed to allocate memory for new window");
5171 /* see if we need to override the workspace */
5172 p
= find_pid(window_get_pid(id
));
5174 /* Get all the window data in one shot */
5175 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
5177 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
5178 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
5180 XGetWindowAttributes(display
, id
, &win
->wa
);
5181 XGetWMNormalHints(display
, id
, &win
->sh
, &mask
);
5182 win
->hints
= XGetWMHints(display
, id
);
5183 XGetTransientForHint(display
, id
, &trans
);
5185 win
->transient
= trans
;
5186 set_child_transient(win
, &trans
);
5187 DNPRINTF(SWM_D_MISC
, "manage_window: win %lu transient %lu\n",
5188 win
->id
, win
->transient
);
5191 /* get supported protocols */
5192 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
5193 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
5194 if (*pp
== takefocus
)
5195 win
->take_focus
= 1;
5197 win
->can_delete
= 1;
5203 win
->iconic
= get_iconic(win
);
5206 * Figure out where to put the window. If it was previously assigned to
5207 * a workspace (either by spawn() or manually moving), and isn't
5208 * transient, * put it in the same workspace
5210 r
= root_to_region(win
->wa
.root
);
5212 ws
= &r
->s
->ws
[p
->ws
];
5213 TAILQ_REMOVE(&pidlist
, p
, entry
);
5216 } else if (prop
&& win
->transient
== 0) {
5217 DNPRINTF(SWM_D_PROP
, "got property _SWM_WS=%s\n", prop
);
5218 ws_idx
= strtonum((const char *)prop
, 0, 9, &errstr
);
5220 DNPRINTF(SWM_D_EVENT
, "window idx is %s: %s",
5223 ws
= &r
->s
->ws
[ws_idx
];
5226 /* this should launch transients in the same ws as parent */
5228 if ((ww
= find_window(trans
)) != NULL
)
5235 "fix this bug mcbride\n");
5240 /* set up the window layout */
5243 win
->s
= r
->s
; /* this never changes */
5244 if (trans
&& (ww
= find_window(trans
)))
5245 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
5247 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
5249 win
->g
.w
= win
->wa
.width
;
5250 win
->g
.h
= win
->wa
.height
;
5251 win
->g
.x
= win
->wa
.x
;
5252 win
->g
.y
= win
->wa
.y
;
5253 win
->g_floatvalid
= 0;
5254 win
->floatmaxed
= 0;
5255 win
->ewmh_flags
= 0;
5257 /* Set window properties so we can remember this after reincarnation */
5258 if (ws_idx_atom
&& prop
== NULL
&&
5259 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
5261 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
5263 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
5264 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
5269 ewmh_autoquirk(win
);
5271 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
5272 DNPRINTF(SWM_D_CLASS
, "class: %s name: %s\n",
5273 win
->ch
.res_class
, win
->ch
.res_name
);
5275 /* java is retarded so treat it special */
5276 if (strstr(win
->ch
.res_name
, "sun-awt")) {
5281 for (i
= 0; i
< quirks_length
; i
++){
5282 if (!strcmp(win
->ch
.res_class
, quirks
[i
].class) &&
5283 !strcmp(win
->ch
.res_name
, quirks
[i
].name
)) {
5284 DNPRINTF(SWM_D_CLASS
, "found: %s name: %s\n",
5285 win
->ch
.res_class
, win
->ch
.res_name
);
5286 if (quirks
[i
].quirk
& SWM_Q_FLOAT
) {
5290 win
->quirks
= quirks
[i
].quirk
;
5295 /* alter window position if quirky */
5296 if (win
->quirks
& SWM_Q_ANYWHERE
) {
5297 win
->manual
= 1; /* don't center the quirky windows */
5298 bzero(&wc
, sizeof wc
);
5300 if (bar_enabled
&& win
->g
.y
< bar_height
) {
5301 win
->g
.y
= wc
.y
= bar_height
;
5304 if (win
->g
.w
+ win
->g
.x
> WIDTH(r
)) {
5305 win
->g
.x
= wc
.x
= WIDTH(r
) - win
->g
.w
- 2;
5311 /* Reset font sizes (the bruteforce way; no default keybinding). */
5312 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
5313 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
5314 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
5315 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
5316 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
5319 ewmh_get_win_state(win
);
5320 ewmh_update_actions(win
);
5321 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
5325 bzero(&wc
, sizeof wc
);
5326 wc
.border_width
= border_width
;
5327 mask
= CWBorderWidth
;
5328 XConfigureWindow(display
, win
->id
, mask
, &wc
);
5331 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
5332 PropertyChangeMask
| StructureNotifyMask
);
5334 /* floaters need to be mapped if they are in the current workspace */
5335 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
5336 XMapRaised(display
, win
->id
);
5342 free_window(struct ws_win
*win
)
5344 DNPRINTF(SWM_D_MISC
, "free_window: %lu\n", win
->id
);
5349 /* needed for restart wm */
5350 set_win_state(win
, WithdrawnState
);
5352 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5354 if (win
->ch
.res_class
)
5355 XFree(win
->ch
.res_class
);
5356 if (win
->ch
.res_name
)
5357 XFree(win
->ch
.res_name
);
5362 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
5368 unmanage_window(struct ws_win
*win
)
5370 struct ws_win
*parent
;
5375 DNPRINTF(SWM_D_MISC
, "unmanage_window: %lu\n", win
->id
);
5377 if (win
->transient
) {
5378 parent
= find_window(win
->transient
);
5380 parent
->child_trans
= NULL
;
5383 /* focus on root just in case */
5384 XSetInputFocus(display
, PointerRoot
, PointerRoot
, CurrentTime
);
5388 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
5389 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
5395 focus_magic(struct ws_win
*win
)
5397 DNPRINTF(SWM_D_FOCUS
, "focus_magic: %lu\n", WINID(win
));
5402 if (win
->child_trans
) {
5403 /* win = parent & has a transient so focus on that */
5405 focus_win(win
->child_trans
);
5406 if (win
->child_trans
->take_focus
)
5407 client_msg(win
, takefocus
);
5409 /* make sure transient hasn't dissapeared */
5410 if (validate_win(win
->child_trans
) == 0) {
5411 focus_win(win
->child_trans
);
5412 if (win
->child_trans
->take_focus
)
5413 client_msg(win
->child_trans
, takefocus
);
5415 win
->child_trans
= NULL
;
5417 if (win
->take_focus
)
5418 client_msg(win
, takefocus
);
5424 if (win
->take_focus
)
5425 client_msg(win
, takefocus
);
5432 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
5440 XKeyEvent
*ev
= &e
->xkey
;
5442 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
5444 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
5445 for (i
= 0; i
< keys_length
; i
++)
5446 if (keysym
== keys
[i
].keysym
5447 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
5448 && keyfuncs
[keys
[i
].funcid
].func
) {
5449 if (keys
[i
].funcid
== kf_spawn_custom
)
5451 root_to_region(ev
->root
),
5452 &(keyfuncs
[keys
[i
].funcid
].args
),
5456 keyfuncs
[keys
[i
].funcid
].func(
5457 root_to_region(ev
->root
),
5458 &(keyfuncs
[keys
[i
].funcid
].args
)
5464 buttonpress(XEvent
*e
)
5468 XButtonPressedEvent
*ev
= &e
->xbutton
;
5470 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
5472 if ((win
= find_window(ev
->window
)) == NULL
)
5476 action
= client_click
;
5478 for (i
= 0; i
< LENGTH(buttons
); i
++)
5479 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
5480 buttons
[i
].button
== ev
->button
&&
5481 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
5482 buttons
[i
].func(win
, &buttons
[i
].args
);
5486 configurerequest(XEvent
*e
)
5488 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
5493 if ((win
= find_window(ev
->window
)) == NULL
)
5494 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
5498 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
5500 bzero(&wc
, sizeof wc
);
5503 wc
.width
= ev
->width
;
5504 wc
.height
= ev
->height
;
5505 wc
.border_width
= ev
->border_width
;
5506 wc
.sibling
= ev
->above
;
5507 wc
.stack_mode
= ev
->detail
;
5508 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
5510 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
5512 config_win(win
, ev
);
5517 configurenotify(XEvent
*e
)
5522 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
5523 e
->xconfigure
.window
);
5525 win
= find_window(e
->xconfigure
.window
);
5527 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
5531 if (focus_mode
== SWM_FOCUS_DEFAULT
)
5532 drain_enter_notify();
5537 destroynotify(XEvent
*e
)
5540 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
5542 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
5544 if ((win
= find_window(ev
->window
)) == NULL
) {
5545 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
5551 /* make sure we focus on something */
5554 unmanage_window(win
);
5556 if (focus_mode
== SWM_FOCUS_DEFAULT
)
5557 drain_enter_notify();
5562 enternotify(XEvent
*e
)
5564 XCrossingEvent
*ev
= &e
->xcrossing
;
5569 Window focus_return
;
5570 int revert_to_return
;
5572 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: %lu mode %d detail %d root "
5573 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5574 ev
->window
, ev
->mode
, ev
->detail
, ev
->root
, ev
->subwindow
,
5575 ev
->same_screen
, ev
->focus
, ev
->state
);
5577 switch (focus_mode
) {
5578 case SWM_FOCUS_DEFAULT
:
5580 case SWM_FOCUS_FOLLOW
:
5582 case SWM_FOCUS_SYNERGY
:
5585 * all these checks need to be in this order because the
5586 * XCheckTypedWindowEvent relies on weeding out the previous events
5588 * making this code an option would enable a follow mouse for focus
5593 * state is set when we are switching workspaces and focus is set when
5594 * the window or a subwindow already has focus (occurs during restart).
5596 * Only honor the focus flag if last_focus_event is not FocusOut,
5597 * this allows scrotwm to continue to control focus when another
5598 * program is also playing with it.
5600 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
5601 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
5606 * happens when a window is created or destroyed and the border
5607 * crosses the mouse pointer and when switching ws
5609 * we need the subwindow test to see if we came from root in order
5610 * to give focus to floaters
5612 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
5613 ev
->subwindow
== 0) {
5614 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
5618 /* this window already has focus */
5619 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
5620 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
5624 /* this window is being deleted or moved to another ws */
5625 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
5627 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
5628 XPutBackEvent(display
, &cne
);
5632 if ((win
= find_window(ev
->window
)) == NULL
) {
5633 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
5638 * In fullstack kill all enters unless they come from a different ws
5639 * (i.e. another region) or focus has been grabbed externally.
5641 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
5642 last_focus_event
!= FocusOut
) {
5643 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
5644 if ((w
= find_window(focus_return
)) == NULL
||
5646 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
5654 if ((win
= find_window(ev
->window
)) == NULL
) {
5655 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
5660 * if we have more enternotifies let them handle it in due time
5662 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
5663 DNPRINTF(SWM_D_EVENT
,
5664 "ignoring enternotify: got more enternotify\n");
5665 XPutBackEvent(display
, &cne
);
5672 /* lets us use one switch statement for arbitrary mode/detail combinations */
5673 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
5676 focusevent(XEvent
*e
)
5680 u_int32_t mode_detail
;
5681 XFocusChangeEvent
*ev
= &e
->xfocus
;
5683 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: %lu mode %d detail %d\n",
5684 ev
->type
== FocusIn
? "entering" : "leaving",
5685 ev
->window
, ev
->mode
, ev
->detail
);
5687 if (last_focus_event
== ev
->type
) {
5688 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
5692 last_focus_event
= ev
->type
;
5693 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
5695 switch (mode_detail
) {
5696 /* synergy client focus operations */
5697 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
5698 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
5700 /* synergy server focus operations */
5701 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
5703 /* Entering applications like rdesktop that mangle the pointer */
5704 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
5706 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
5707 XSetWindowBorder(display
, win
->id
,
5708 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
5709 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
5712 fprintf(stderr
, "ignoring focusevent\n");
5713 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
5720 mapnotify(XEvent
*e
)
5723 XMapEvent
*ev
= &e
->xmap
;
5725 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: %lu\n", ev
->window
);
5727 win
= manage_window(ev
->window
);
5729 set_win_state(win
, NormalState
);
5733 mappingnotify(XEvent
*e
)
5735 XMappingEvent
*ev
= &e
->xmapping
;
5737 XRefreshKeyboardMapping(ev
);
5738 if (ev
->request
== MappingKeyboard
)
5743 maprequest(XEvent
*e
)
5746 struct swm_region
*r
;
5747 XWindowAttributes wa
;
5748 XMapRequestEvent
*ev
= &e
->xmaprequest
;
5750 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
5751 e
->xmaprequest
.window
);
5753 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
5755 if (wa
.override_redirect
)
5758 win
= manage_window(e
->xmaprequest
.window
);
5760 return; /* can't happen */
5764 /* make new win focused */
5765 r
= root_to_region(win
->wa
.root
);
5766 if (win
->ws
== r
->ws
)
5771 propertynotify(XEvent
*e
)
5774 XPropertyEvent
*ev
= &e
->xproperty
;
5776 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
5779 win
= find_window(ev
->window
);
5783 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
5784 update_iconic(win
, 0);
5785 XMapRaised(display
, win
->id
);
5792 case XA_WM_NORMAL_HINTS
:
5795 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
5796 fprintf(stderr
, "normal hints: flag 0x%x\n", win
->sh
.flags
);
5797 if (win
->sh
.flags
& PMinSize
) {
5798 win
->g
.w
= win
->sh
.min_width
;
5799 win
->g
.h
= win
->sh
.min_height
;
5800 fprintf(stderr
, "min %d %d\n", win
->g
.w
, win
->g
.h
);
5802 XMoveResizeWindow(display
, win
->id
,
5803 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
5805 if (window_name_enabled
)
5814 unmapnotify(XEvent
*e
)
5818 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
5820 /* determine if we need to help unmanage this window */
5821 win
= find_window(e
->xunmap
.window
);
5825 if (getstate(e
->xunmap
.window
) == NormalState
) {
5826 unmanage_window(win
);
5829 /* giant hack for apps that don't destroy transient windows */
5830 /* eat a bunch of events to prevent remanaging the window */
5832 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
5833 EnterWindowMask
, &cne
))
5835 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
5836 StructureNotifyMask
, &cne
))
5838 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
5839 SubstructureNotifyMask
, &cne
))
5841 /* resend unmap because we ated it */
5842 XUnmapWindow(display
, e
->xunmap
.window
);
5845 if (focus_mode
== SWM_FOCUS_DEFAULT
)
5846 drain_enter_notify();
5850 visibilitynotify(XEvent
*e
)
5853 struct swm_region
*r
;
5855 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
5856 e
->xvisibility
.window
);
5857 if (e
->xvisibility
.state
== VisibilityUnobscured
)
5858 for (i
= 0; i
< ScreenCount(display
); i
++)
5859 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
5860 if (e
->xvisibility
.window
== r
->bar_window
)
5865 clientmessage(XEvent
*e
)
5867 XClientMessageEvent
*ev
;
5872 win
= find_window(ev
->window
);
5876 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx type: %ld \n",
5877 ev
->window
, ev
->message_type
);
5879 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
5880 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW \n");
5883 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
5884 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW \n");
5885 if (win
->can_delete
)
5886 client_msg(win
, adelete
);
5888 XKillClient(display
, win
->id
);
5890 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
5891 DNPRINTF(SWM_D_EVENT
,
5892 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
5893 if (win
->floating
) {
5894 if (ev
->data
.l
[0] & (1<<8)) /* x */
5895 win
->g
.x
= ev
->data
.l
[1];
5896 if (ev
->data
.l
[0] & (1<<9)) /* y */
5897 win
->g
.y
= ev
->data
.l
[2];
5898 if (ev
->data
.l
[0] & (1<<10)) /* width */
5899 win
->g
.w
= ev
->data
.l
[3];
5900 if (ev
->data
.l
[0] & (1<<11)) /* height */
5901 win
->g
.h
= ev
->data
.l
[4];
5904 /* TODO: Change stack sizes */
5906 config_win(win
, NULL
);
5908 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
5909 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE \n");
5910 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
5912 ewmh_update_win_state(win
, ev
->data
.l
[2],
5920 xerror_start(Display
*d
, XErrorEvent
*ee
)
5927 xerror(Display
*d
, XErrorEvent
*ee
)
5929 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5937 xerrorxlib
= XSetErrorHandler(xerror_start
);
5939 /* this causes an error if some other window manager is running */
5940 XSelectInput(display
, DefaultRootWindow(display
),
5941 SubstructureRedirectMask
);
5942 XSync(display
, False
);
5946 XSetErrorHandler(xerror
);
5947 XSync(display
, False
);
5952 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
5954 struct swm_region
*r
, *n
;
5955 struct workspace
*ws
= NULL
;
5958 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
5959 s
->idx
, w
, h
, x
, y
);
5961 /* remove any conflicting regions */
5962 n
= TAILQ_FIRST(&s
->rl
);
5965 n
= TAILQ_NEXT(r
, entry
);
5966 if (X(r
) < (x
+ w
) &&
5967 (X(r
) + WIDTH(r
)) > x
&&
5969 (Y(r
) + HEIGHT(r
)) > y
) {
5970 if (r
->ws
->r
!= NULL
)
5971 r
->ws
->old_r
= r
->ws
->r
;
5973 XDestroyWindow(display
, r
->bar_window
);
5974 TAILQ_REMOVE(&s
->rl
, r
, entry
);
5975 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
5979 /* search old regions for one to reuse */
5981 /* size + location match */
5982 TAILQ_FOREACH(r
, &s
->orl
, entry
)
5983 if (X(r
) == x
&& Y(r
) == y
&&
5984 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
5988 TAILQ_FOREACH(r
, &s
->orl
, entry
)
5989 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
5993 TAILQ_REMOVE(&s
->orl
, r
, entry
);
5994 /* try to use old region's workspace */
5995 if (r
->ws
->r
== NULL
)
5998 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
5999 errx(1, "calloc: failed to allocate memory for screen");
6001 /* if we don't have a workspace already, find one */
6003 for (i
= 0; i
< SWM_WS_MAX
; i
++)
6004 if (s
->ws
[i
].r
== NULL
) {
6011 errx(1, "no free workspaces\n");
6022 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
6028 #ifdef SWM_XRR_HAS_CRTC
6030 XRRScreenResources
*sr
;
6033 #endif /* SWM_XRR_HAS_CRTC */
6034 struct swm_region
*r
;
6037 if (i
>= ScreenCount(display
))
6038 errx(1, "invalid screen");
6040 /* remove any old regions */
6041 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
6042 r
->ws
->old_r
= r
->ws
->r
= NULL
;
6043 XDestroyWindow(display
, r
->bar_window
);
6044 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
6045 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
6049 /* map virtual screens onto physical screens */
6050 #ifdef SWM_XRR_HAS_CRTC
6051 if (xrandr_support
) {
6052 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
6054 new_region(&screens
[i
], 0, 0,
6055 DisplayWidth(display
, i
),
6056 DisplayHeight(display
, i
));
6060 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
6061 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
6062 if (ci
->noutput
== 0)
6065 if (ci
!= NULL
&& ci
->mode
== None
)
6066 new_region(&screens
[i
], 0, 0,
6067 DisplayWidth(display
, i
),
6068 DisplayHeight(display
, i
));
6070 new_region(&screens
[i
],
6071 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
6074 XRRFreeCrtcInfo(ci
);
6075 XRRFreeScreenResources(sr
);
6077 #endif /* SWM_XRR_HAS_CRTC */
6079 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
6080 DisplayHeight(display
, i
));
6085 screenchange(XEvent
*e
) {
6086 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
6087 struct swm_region
*r
;
6090 DNPRINTF(SWM_D_EVENT
, "screenchange: %lu\n", xe
->root
);
6092 if (!XRRUpdateConfiguration(e
))
6095 /* silly event doesn't include the screen index */
6096 for (i
= 0; i
< ScreenCount(display
); i
++)
6097 if (screens
[i
].root
== xe
->root
)
6099 if (i
>= ScreenCount(display
))
6100 errx(1, "screenchange: screen not found\n");
6102 /* brute force for now, just re-enumerate the regions */
6105 /* add bars to all regions */
6106 for (i
= 0; i
< ScreenCount(display
); i
++)
6107 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6110 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6111 drain_enter_notify();
6117 Window d1
, d2
, *wins
= NULL
;
6118 XWindowAttributes wa
;
6123 for (i
= 0; i
< ScreenCount(display
); i
++) {
6124 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
6127 /* attach windows to a region */
6128 /* normal windows */
6129 for (j
= 0; j
< no
; j
++) {
6130 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6131 wa
.override_redirect
||
6132 XGetTransientForHint(display
, wins
[j
], &d1
))
6135 state
= getstate(wins
[j
]);
6136 manage
= state
== IconicState
;
6137 if (wa
.map_state
== IsViewable
|| manage
)
6138 manage_window(wins
[j
]);
6140 /* transient windows */
6141 for (j
= 0; j
< no
; j
++) {
6142 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6143 wa
.override_redirect
)
6146 state
= getstate(wins
[j
]);
6147 manage
= state
== IconicState
;
6148 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
6150 manage_window(wins
[j
]);
6163 int errorbase
, major
, minor
;
6164 struct workspace
*ws
;
6166 if ((screens
= calloc(ScreenCount(display
),
6167 sizeof(struct swm_screen
))) == NULL
)
6168 errx(1, "calloc: screens");
6170 /* initial Xrandr setup */
6171 xrandr_support
= XRRQueryExtension(display
,
6172 &xrandr_eventbase
, &errorbase
);
6174 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
6177 /* map physical screens */
6178 for (i
= 0; i
< ScreenCount(display
); i
++) {
6179 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
6181 TAILQ_INIT(&screens
[i
].rl
);
6182 TAILQ_INIT(&screens
[i
].orl
);
6183 screens
[i
].root
= RootWindow(display
, i
);
6185 /* set default colors */
6186 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
6187 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
6188 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
6189 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
6190 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
6192 /* set default cursor */
6193 XDefineCursor(display
, screens
[i
].root
,
6194 XCreateFontCursor(display
, XC_left_ptr
));
6196 /* init all workspaces */
6197 /* XXX these should be dynamically allocated too */
6198 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
6199 ws
= &screens
[i
].ws
[j
];
6204 TAILQ_INIT(&ws
->winlist
);
6205 TAILQ_INIT(&ws
->unmanagedlist
);
6207 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
6208 if (layouts
[k
].l_config
!= NULL
)
6209 layouts
[k
].l_config(ws
,
6210 SWM_ARG_ID_STACKINIT
);
6211 ws
->cur_layout
= &layouts
[0];
6212 ws
->cur_layout
->l_string(ws
);
6218 XRRSelectInput(display
, screens
[i
].root
,
6219 RRScreenChangeNotifyMask
);
6226 if ((bar_fonts
[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
6228 err(1, "setup_globals: strdup");
6229 if ((bar_fonts
[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
6231 err(1, "setup_globals: strdup");
6232 if ((bar_fonts
[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
6234 err(1, "setup_globals: strdup");
6235 if ((spawn_term
[0] = strdup("xterm")) == NULL
)
6236 err(1, "setup_globals: strdup");
6237 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
6245 Atom netwmcheck
, netwmname
, utf8_string
;
6248 /* work around sun jdk bugs, code from wmname */
6249 netwmcheck
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
6250 netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
6251 utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6252 for (i
= 0; i
< ScreenCount(display
); i
++) {
6253 root
= screens
[i
].root
;
6254 win
= XCreateSimpleWindow(display
,root
, 0, 0, 1, 1, 0,
6255 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
,
6256 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
);
6258 XChangeProperty(display
, root
, netwmcheck
, XA_WINDOW
, 32,
6259 PropModeReplace
, (unsigned char *)&win
,1);
6260 XChangeProperty(display
, win
, netwmcheck
, XA_WINDOW
, 32,
6261 PropModeReplace
, (unsigned char *)&win
,1);
6262 XChangeProperty(display
, win
, netwmname
, utf8_string
, 8,
6263 PropModeReplace
, (unsigned char*)"LG3D", strlen("LG3D"));
6268 main(int argc
, char *argv
[])
6271 struct swm_region
*r
, *rr
;
6272 struct ws_win
*winfocus
= NULL
;
6275 char conf
[PATH_MAX
], *cfile
= NULL
;
6280 struct sigaction sact
;
6283 fprintf(stderr
, "Welcome to scrotwm V%s cvs tag: %s\n",
6284 SWM_VERSION
, cvstag
);
6285 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
6286 warnx("no locale support");
6288 if (!(display
= XOpenDisplay(0)))
6289 errx(1, "can not open display");
6292 errx(1, "other wm running");
6294 /* handle some signals */
6295 bzero(&sact
, sizeof(sact
));
6296 sigemptyset(&sact
.sa_mask
);
6298 sact
.sa_handler
= sighdlr
;
6299 sigaction(SIGINT
, &sact
, NULL
);
6300 sigaction(SIGQUIT
, &sact
, NULL
);
6301 sigaction(SIGTERM
, &sact
, NULL
);
6302 sigaction(SIGHUP
, &sact
, NULL
);
6304 sact
.sa_handler
= sighdlr
;
6305 sact
.sa_flags
= SA_NOCLDSTOP
;
6306 sigaction(SIGCHLD
, &sact
, NULL
);
6308 astate
= XInternAtom(display
, "WM_STATE", False
);
6309 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
6310 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
6311 takefocus
= XInternAtom(display
, "WM_TAKE_FOCUS", False
);
6312 a_wmname
= XInternAtom(display
, "WM_NAME", False
);
6313 a_utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6314 a_string
= XInternAtom(display
, "STRING", False
);
6315 a_swm_iconic
= XInternAtom(display
, "_SWM_ICONIC", False
);
6317 /* look for local and global conf file */
6318 pwd
= getpwuid(getuid());
6320 errx(1, "invalid user %d", getuid());
6329 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
6330 if (stat(conf
, &sb
) != -1) {
6331 if (S_ISREG(sb
.st_mode
))
6334 /* try global conf file */
6335 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
6336 if (!stat(conf
, &sb
))
6337 if (S_ISREG(sb
.st_mode
))
6344 /* set some values to work around bad programs */
6346 /* grab existing windows (before we build the bars) */
6349 if (getenv("SWM_STARTED") == NULL
)
6350 setenv("SWM_STARTED", "YES", 1);
6352 /* setup all bars */
6353 for (i
= 0; i
< ScreenCount(display
); i
++)
6354 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
6355 if (winfocus
== NULL
)
6356 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
6364 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6365 drain_enter_notify();
6367 xfd
= ConnectionNumber(display
);
6369 while (XPending(display
)) {
6370 XNextEvent(display
, &e
);
6373 if (e
.type
< LASTEvent
) {
6375 if (handler
[e
.type
])
6376 handler
[e
.type
](&e
);
6378 DNPRINTF(SWM_D_EVENT
,
6379 "win: %lu unknown event: %d\n",
6380 e
.xany
.window
, e
.type
);
6382 switch (e
.type
- xrandr_eventbase
) {
6383 case RRScreenChangeNotify
:
6387 DNPRINTF(SWM_D_EVENT
,
6388 "win: %lu unknown xrandr event: "
6389 "%d\n", e
.xany
.window
, e
.type
);
6395 /* if we are being restarted go focus on first window */
6397 rr
= winfocus
->ws
->r
;
6399 /* not a visible window */
6403 /* move pointer to first screen if multi screen */
6404 if (ScreenCount(display
) > 1 || outputs
> 1)
6405 XWarpPointer(display
, None
, rr
->s
[0].root
,
6406 0, 0, 0, 0, rr
->g
.x
,
6407 rr
->g
.y
+ (bar_enabled
? bar_height
: 0));
6409 a
.id
= SWM_ARG_ID_FOCUSCUR
;
6419 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
6421 DNPRINTF(SWM_D_MISC
, "select failed");
6422 if (restart_wm
== 1)
6423 restart(NULL
, NULL
);
6424 if (search_resp
== 1)
6436 XFreeGC(display
, bar_gc
);
6437 XCloseDisplay(display
);