2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Much code and ideas taken from dwm under the following license:
24 * MIT/X Consortium License
26 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
27 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
28 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
29 * 2007 Premysl Hruby <dfenze at gmail dot com>
30 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
31 * 2007 Christof Musik <christof at sendfax dot de>
32 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
33 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
34 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
36 * Permission is hereby granted, free of charge, to any person obtaining a
37 * copy of this software and associated documentation files (the "Software"),
38 * to deal in the Software without restriction, including without limitation
39 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
40 * and/or sell copies of the Software, and to permit persons to whom the
41 * Software is furnished to do so, subject to the following conditions:
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
49 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
51 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
52 * DEALINGS IN THE SOFTWARE.
70 #include <sys/types.h>
74 #include <sys/queue.h>
75 #include <sys/param.h>
76 #include <sys/select.h>
78 #include <X11/cursorfont.h>
79 #include <X11/keysym.h>
80 #include <X11/Xatom.h>
82 #include <X11/Xproto.h>
83 #include <X11/Xutil.h>
84 #include <X11/extensions/Xrandr.h>
85 #include <X11/extensions/XTest.h>
93 #ifdef SCROTWM_BUILDSTR
94 static const char *buildstr
= SCROTWM_BUILDSTR
;
96 static const char *buildstr
= SCROTWM_VERSION
;
100 # error XRandR versions less than 1.0 are not supported
105 #define SWM_XRR_HAS_CRTC
109 /*#define SWM_DEBUG*/
111 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
112 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
113 #define SWM_D_MISC 0x0001
114 #define SWM_D_EVENT 0x0002
115 #define SWM_D_WS 0x0004
116 #define SWM_D_FOCUS 0x0008
117 #define SWM_D_MOVE 0x0010
118 #define SWM_D_STACK 0x0020
119 #define SWM_D_MOUSE 0x0040
120 #define SWM_D_PROP 0x0080
121 #define SWM_D_CLASS 0x0100
122 #define SWM_D_KEY 0x0200
123 #define SWM_D_QUIRK 0x0400
124 #define SWM_D_SPAWN 0x0800
125 #define SWM_D_EVENTQ 0x1000
126 #define SWM_D_CONF 0x2000
128 u_int32_t swm_debug
= 0
145 #define DPRINTF(x...)
146 #define DNPRINTF(n,x...)
149 #define LENGTH(x) (sizeof x / sizeof x[0])
150 #define MODKEY Mod1Mask
151 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
152 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
153 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
154 #define SWM_PROPLEN (16)
155 #define SWM_FUNCNAME_LEN (32)
156 #define SWM_KEYS_LEN (255)
157 #define SWM_QUIRK_LEN (64)
158 #define X(r) (r)->g.x
159 #define Y(r) (r)->g.y
160 #define WIDTH(r) (r)->g.w
161 #define HEIGHT(r) (r)->g.h
162 #define SWM_MAX_FONT_STEPS (3)
163 #define WINID(w) (w ? w->id : 0)
165 #define SWM_FOCUS_DEFAULT (0)
166 #define SWM_FOCUS_SYNERGY (1)
167 #define SWM_FOCUS_FOLLOW (2)
169 #define SWM_CONF_DEFAULT (0)
170 #define SWM_CONF_KEYMAPPING (1)
173 #define SWM_LIB "/usr/local/lib/libswmhack.so"
186 volatile sig_atomic_t running
= 1;
187 volatile sig_atomic_t restart_wm
= 0;
189 int last_focus_event
= FocusOut
;
190 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
194 int xrandr_eventbase
;
195 unsigned int numlockmask
= 0;
199 int cycle_visible
= 0;
201 int font_adjusted
= 0;
202 unsigned int mod_key
= MODKEY
;
205 struct swm_region
*search_r
;
206 int select_list_pipe
[2];
207 int select_resp_pipe
[2];
209 volatile sig_atomic_t search_resp
;
210 int search_resp_action
;
212 struct search_window
{
213 TAILQ_ENTRY(search_window
) entry
;
218 TAILQ_HEAD(search_winlist
, search_window
);
220 struct search_winlist search_wl
;
225 SWM_SEARCH_UNICONIFY
,
226 SWM_SEARCH_NAME_WORKSPACE
,
227 SWM_SEARCH_SEARCH_WORKSPACE
,
228 SWM_SEARCH_SEARCH_WINDOW
232 double dialog_ratio
= .6;
234 #define SWM_BAR_MAX (256)
235 #define SWM_BAR_JUSTIFY_LEFT (0)
236 #define SWM_BAR_JUSTIFY_CENTER (1)
237 #define SWM_BAR_JUSTIFY_RIGHT (2)
238 #define SWM_BAR_OFFSET (4)
239 char *bar_argv
[] = { NULL
, NULL
};
241 char bar_ext
[SWM_BAR_MAX
];
242 char bar_vertext
[SWM_BAR_MAX
];
244 sig_atomic_t bar_alarm
= 0;
247 int bar_border_width
= 1;
248 int bar_at_bottom
= 0;
250 int bar_extra_running
= 0;
253 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
254 int stack_enabled
= 1;
255 int clock_enabled
= 1;
256 int urgent_enabled
= 0;
257 char *clock_format
= NULL
;
258 int title_name_enabled
= 0;
259 int title_class_enabled
= 0;
260 int window_name_enabled
= 0;
261 int focus_mode
= SWM_FOCUS_DEFAULT
;
262 int disable_border
= 0;
263 int border_width
= 1;
264 int verbose_layout
= 0;
270 char *bar_fonts
[] = { NULL
, NULL
, NULL
, NULL
};/* XXX Make fully dynamic */
271 char *spawn_term
[] = { NULL
, NULL
}; /* XXX Make fully dynamic */
274 #define SWM_MENU_FN (2)
275 #define SWM_MENU_NB (4)
276 #define SWM_MENU_NF (6)
277 #define SWM_MENU_SB (8)
278 #define SWM_MENU_SF (10)
280 /* layout manager data */
281 struct swm_geometry
{
291 /* virtual "screens" */
293 TAILQ_ENTRY(swm_region
) entry
;
294 struct swm_geometry g
;
295 struct workspace
*ws
; /* current workspace on this region */
296 struct workspace
*ws_prior
; /* prior workspace on this region */
297 struct swm_screen
*s
; /* screen idx */
300 TAILQ_HEAD(swm_region_list
, swm_region
);
303 TAILQ_ENTRY(ws_win
) entry
;
306 struct ws_win
*child_trans
; /* transient child window */
307 struct swm_geometry g
; /* current geometry */
308 struct swm_geometry g_float
; /* geometry when floating */
309 struct swm_geometry rg_float
; /* region geom when floating */
310 int g_floatvalid
; /* flag: geometry in g_float is valid */
311 int floatmaxed
; /* flag: floater was maxed in max_stack */
315 unsigned int ewmh_flags
;
316 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
322 unsigned long quirks
;
323 struct workspace
*ws
; /* always valid */
324 struct swm_screen
*s
; /* always valid, never changes */
325 XWindowAttributes wa
;
330 TAILQ_HEAD(ws_win_list
, ws_win
);
334 TAILQ_ENTRY(pid_e
) entry
;
338 TAILQ_HEAD(pid_list
, pid_e
);
339 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
341 /* layout handlers */
343 void vertical_config(struct workspace
*, int);
344 void vertical_stack(struct workspace
*, struct swm_geometry
*);
345 void horizontal_config(struct workspace
*, int);
346 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
347 void max_stack(struct workspace
*, struct swm_geometry
*);
348 void plain_stacker(struct workspace
*);
349 void fancy_stacker(struct workspace
*);
351 struct ws_win
*find_window(Window
);
353 void grabbuttons(struct ws_win
*, int);
354 void new_region(struct swm_screen
*, int, int, int, int);
355 void unmanage_window(struct ws_win
*);
356 long getstate(Window
);
358 int conf_load(char *, int);
361 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
362 void (*l_config
)(struct workspace
*, int);
364 #define SWM_L_FOCUSPREV (1<<0)
365 #define SWM_L_MAPONFOCUS (1<<1)
366 void (*l_string
)(struct workspace
*);
368 /* stack, configure */
369 { vertical_stack
, vertical_config
, 0, plain_stacker
},
370 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
372 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
373 { NULL
, NULL
, 0, NULL
},
376 /* position of max_stack mode in the layouts array, index into layouts! */
377 #define SWM_V_STACK (0)
378 #define SWM_H_STACK (1)
379 #define SWM_MAX_STACK (2)
381 #define SWM_H_SLICE (32)
382 #define SWM_V_SLICE (32)
384 /* define work spaces */
386 int idx
; /* workspace index */
387 char *name
; /* workspace name */
388 int always_raise
; /* raise windows on focus */
389 struct layout
*cur_layout
; /* current layout handlers */
390 struct ws_win
*focus
; /* may be NULL */
391 struct ws_win
*focus_prev
; /* may be NULL */
392 struct swm_region
*r
; /* may be NULL */
393 struct swm_region
*old_r
; /* may be NULL */
394 struct ws_win_list winlist
; /* list of windows in ws */
395 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
396 char stacker
[10]; /* display stacker and layout */
400 int horizontal_msize
;
402 int horizontal_stacks
;
409 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
410 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
412 /* physical screen mapping */
413 #define SWM_WS_MAX (10)
415 int idx
; /* screen index */
416 struct swm_region_list rl
; /* list of regions on this screen */
417 struct swm_region_list orl
; /* list of old regions */
419 struct workspace ws
[SWM_WS_MAX
];
425 } c
[SWM_S_COLOR_MAX
];
427 struct swm_screen
*screens
;
430 /* args to functions */
433 #define SWM_ARG_ID_FOCUSNEXT (0)
434 #define SWM_ARG_ID_FOCUSPREV (1)
435 #define SWM_ARG_ID_FOCUSMAIN (2)
436 #define SWM_ARG_ID_FOCUSCUR (4)
437 #define SWM_ARG_ID_SWAPNEXT (10)
438 #define SWM_ARG_ID_SWAPPREV (11)
439 #define SWM_ARG_ID_SWAPMAIN (12)
440 #define SWM_ARG_ID_MOVELAST (13)
441 #define SWM_ARG_ID_MASTERSHRINK (20)
442 #define SWM_ARG_ID_MASTERGROW (21)
443 #define SWM_ARG_ID_MASTERADD (22)
444 #define SWM_ARG_ID_MASTERDEL (23)
445 #define SWM_ARG_ID_STACKRESET (30)
446 #define SWM_ARG_ID_STACKINIT (31)
447 #define SWM_ARG_ID_CYCLEWS_UP (40)
448 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
449 #define SWM_ARG_ID_CYCLESC_UP (42)
450 #define SWM_ARG_ID_CYCLESC_DOWN (43)
451 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
452 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
453 #define SWM_ARG_ID_STACKINC (50)
454 #define SWM_ARG_ID_STACKDEC (51)
455 #define SWM_ARG_ID_SS_ALL (60)
456 #define SWM_ARG_ID_SS_WINDOW (61)
457 #define SWM_ARG_ID_DONTCENTER (70)
458 #define SWM_ARG_ID_CENTER (71)
459 #define SWM_ARG_ID_KILLWINDOW (80)
460 #define SWM_ARG_ID_DELETEWINDOW (81)
461 #define SWM_ARG_ID_WIDTHGROW (90)
462 #define SWM_ARG_ID_WIDTHSHRINK (91)
463 #define SWM_ARG_ID_HEIGHTGROW (92)
464 #define SWM_ARG_ID_HEIGHTSHRINK (93)
465 #define SWM_ARG_ID_MOVEUP (100)
466 #define SWM_ARG_ID_MOVEDOWN (101)
467 #define SWM_ARG_ID_MOVELEFT (102)
468 #define SWM_ARG_ID_MOVERIGHT (103)
472 void focus(struct swm_region
*, union arg
*);
473 void focus_magic(struct ws_win
*);
480 #define SWM_Q_FLOAT (1<<0) /* float this window */
481 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
482 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
483 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
484 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
485 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
487 int quirks_size
= 0, quirks_length
= 0;
488 struct quirk
*quirks
= NULL
;
491 * Supported EWMH hints should be added to
492 * both the enum and the ewmh array
494 enum { _NET_ACTIVE_WINDOW
, _NET_MOVERESIZE_WINDOW
, _NET_CLOSE_WINDOW
,
495 _NET_WM_WINDOW_TYPE
, _NET_WM_WINDOW_TYPE_DOCK
,
496 _NET_WM_WINDOW_TYPE_TOOLBAR
, _NET_WM_WINDOW_TYPE_UTILITY
,
497 _NET_WM_WINDOW_TYPE_SPLASH
, _NET_WM_WINDOW_TYPE_DIALOG
,
498 _NET_WM_WINDOW_TYPE_NORMAL
, _NET_WM_STATE
,
499 _NET_WM_STATE_MAXIMIZED_HORZ
, _NET_WM_STATE_MAXIMIZED_VERT
,
500 _NET_WM_STATE_SKIP_TASKBAR
, _NET_WM_STATE_SKIP_PAGER
,
501 _NET_WM_STATE_HIDDEN
, _NET_WM_STATE_ABOVE
, _SWM_WM_STATE_MANUAL
,
502 _NET_WM_STATE_FULLSCREEN
, _NET_WM_ALLOWED_ACTIONS
, _NET_WM_ACTION_MOVE
,
503 _NET_WM_ACTION_RESIZE
, _NET_WM_ACTION_FULLSCREEN
, _NET_WM_ACTION_CLOSE
,
509 } ewmh
[SWM_EWMH_HINT_MAX
] = {
510 /* must be in same order as in the enum */
511 {"_NET_ACTIVE_WINDOW", None
},
512 {"_NET_MOVERESIZE_WINDOW", None
},
513 {"_NET_CLOSE_WINDOW", None
},
514 {"_NET_WM_WINDOW_TYPE", None
},
515 {"_NET_WM_WINDOW_TYPE_DOCK", None
},
516 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None
},
517 {"_NET_WM_WINDOW_TYPE_UTILITY", None
},
518 {"_NET_WM_WINDOW_TYPE_SPLASH", None
},
519 {"_NET_WM_WINDOW_TYPE_DIALOG", None
},
520 {"_NET_WM_WINDOW_TYPE_NORMAL", None
},
521 {"_NET_WM_STATE", None
},
522 {"_NET_WM_STATE_MAXIMIZED_HORZ", None
},
523 {"_NET_WM_STATE_MAXIMIZED_VERT", None
},
524 {"_NET_WM_STATE_SKIP_TASKBAR", None
},
525 {"_NET_WM_STATE_SKIP_PAGER", None
},
526 {"_NET_WM_STATE_HIDDEN", None
},
527 {"_NET_WM_STATE_ABOVE", None
},
528 {"_SWM_WM_STATE_MANUAL", None
},
529 {"_NET_WM_STATE_FULLSCREEN", None
},
530 {"_NET_WM_ALLOWED_ACTIONS", None
},
531 {"_NET_WM_ACTION_MOVE", None
},
532 {"_NET_WM_ACTION_RESIZE", None
},
533 {"_NET_WM_ACTION_FULLSCREEN", None
},
534 {"_NET_WM_ACTION_CLOSE", None
},
537 void store_float_geom(struct ws_win
*win
, struct swm_region
*r
);
538 int floating_toggle_win(struct ws_win
*win
);
539 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
542 get_property(Window id
, Atom atom
, long count
, Atom type
,
543 unsigned long *n
, unsigned char **data
)
546 unsigned long tmp
, extra
;
547 unsigned long *nitems
;
550 nitems
= n
!= NULL
? n
: &tmp
;
551 status
= XGetWindowProperty(display
, id
, atom
, 0L, count
, False
, type
,
552 &real
, &format
, nitems
, &extra
, data
);
554 if (status
!= Success
)
563 update_iconic(struct ws_win
*win
, int newv
)
570 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
574 XChangeProperty(display
, win
->id
, iprop
, XA_INTEGER
, 32,
575 PropModeReplace
, (unsigned char *)&v
, 1);
577 XDeleteProperty(display
, win
->id
, iprop
);
581 get_iconic(struct ws_win
*win
)
586 unsigned long nitems
, extra
;
587 unsigned char *prop
= NULL
;
589 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
592 status
= XGetWindowProperty(display
, win
->id
, iprop
, 0L, 1L,
593 False
, XA_INTEGER
, &rettype
, &retfmt
, &nitems
, &extra
, &prop
);
594 if (status
!= Success
)
596 if (rettype
!= XA_INTEGER
|| retfmt
!= 32)
600 v
= *((int32_t *)prop
);
614 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
616 for (i
= 0; i
< LENGTH(ewmh
); i
++)
617 ewmh
[i
].atom
= XInternAtom(display
, ewmh
[i
].name
, False
);
619 for (i
= 0; i
< ScreenCount(display
); i
++) {
620 /* Support check window will be created by workaround(). */
622 /* Report supported atoms */
623 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
624 for (j
= 0; j
< LENGTH(ewmh
); j
++)
625 XChangeProperty(display
, screens
[i
].root
,
626 sup_list
, XA_ATOM
, 32,
627 PropModeAppend
, (unsigned char *)&ewmh
[j
].atom
,1);
635 unsigned char *data
= NULL
;
637 Atom sup_check
, sup_list
;
640 sup_check
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
641 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
643 for (i
= 0; i
< ScreenCount(display
); i
++) {
644 /* Get the support check window and destroy it */
645 success
= get_property(screens
[i
].root
, sup_check
, 1, XA_WINDOW
,
650 XDestroyWindow(display
, id
);
651 XDeleteProperty(display
, screens
[i
].root
, sup_check
);
652 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
660 ewmh_autoquirk(struct ws_win
*win
)
663 unsigned long *data
= NULL
, n
;
666 success
= get_property(win
->id
, ewmh
[_NET_WM_WINDOW_TYPE
].atom
, (~0L),
667 XA_ATOM
, &n
, (void *)&data
);
674 for (i
= 0; i
< n
; i
++) {
676 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
678 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
679 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
680 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
682 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
685 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
686 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
688 win
->quirks
= SWM_Q_FLOAT
;
696 #define SWM_EWMH_ACTION_COUNT_MAX (6)
697 #define EWMH_F_FULLSCREEN (1<<0)
698 #define EWMH_F_ABOVE (1<<1)
699 #define EWMH_F_HIDDEN (1<<2)
700 #define EWMH_F_SKIP_PAGER (1<<3)
701 #define EWMH_F_SKIP_TASKBAR (1<<4)
702 #define SWM_F_MANUAL (1<<5)
705 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
707 struct swm_geometry rg
;
715 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
721 store_float_geom(win
, win
->ws
->r
);
728 if (win
->g_floatvalid
) {
729 /* refloat at last floating relative position */
730 win
->g
.x
= win
->g_float
.x
- win
->rg_float
.x
+ rg
.x
;
731 win
->g
.y
= win
->g_float
.y
- win
->rg_float
.y
+ rg
.y
;
732 win
->g
.w
= win
->g_float
.w
;
733 win
->g
.h
= win
->g_float
.h
;
741 ewmh_update_actions(struct ws_win
*win
)
743 Atom actions
[SWM_EWMH_ACTION_COUNT_MAX
];
749 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
752 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
753 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
756 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
,
757 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)actions
, n
);
760 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
761 #define _NET_WM_STATE_ADD 1 /* add/set property */
762 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
765 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
767 unsigned int mask
= 0;
768 unsigned int changed
= 0;
769 unsigned int orig_flags
;
774 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
775 mask
= EWMH_F_FULLSCREEN
;
776 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
778 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
780 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
781 mask
= EWMH_F_SKIP_PAGER
;
782 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
783 mask
= EWMH_F_SKIP_TASKBAR
;
786 orig_flags
= win
->ewmh_flags
;
789 case _NET_WM_STATE_REMOVE
:
790 win
->ewmh_flags
&= ~mask
;
792 case _NET_WM_STATE_ADD
:
793 win
->ewmh_flags
|= mask
;
795 case _NET_WM_STATE_TOGGLE
:
796 win
->ewmh_flags
^= mask
;
800 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
802 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
804 if (!floating_toggle_win(win
))
805 win
->ewmh_flags
= orig_flags
; /* revert */
806 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
808 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
809 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
811 if (!ewmh_set_win_fullscreen(win
,
812 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
813 win
->ewmh_flags
= orig_flags
; /* revert */
815 XDeleteProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
817 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
818 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
819 XA_ATOM
, 32, PropModeAppend
,
820 (unsigned char *)&ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
, 1);
821 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
822 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
823 XA_ATOM
, 32, PropModeAppend
,
824 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
, 1);
825 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
826 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
827 XA_ATOM
, 32, PropModeAppend
,
828 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
, 1);
829 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
830 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
831 XA_ATOM
, 32, PropModeAppend
,
832 (unsigned char *)&ewmh
[_NET_WM_STATE_ABOVE
].atom
, 1);
833 if (win
->ewmh_flags
& SWM_F_MANUAL
)
834 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
835 XA_ATOM
, 32, PropModeAppend
,
836 (unsigned char *)&ewmh
[_SWM_WM_STATE_MANUAL
].atom
, 1);
840 ewmh_get_win_state(struct ws_win
*win
)
851 win
->ewmh_flags
|= EWMH_F_ABOVE
;
853 win
->ewmh_flags
|= SWM_F_MANUAL
;
855 success
= get_property(win
->id
, ewmh
[_NET_WM_STATE
].atom
,
856 (~0L), XA_ATOM
, &n
, (void *)&states
);
861 for (i
= 0; i
< n
; i
++)
862 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
882 name
= "ButtonPress";
885 name
= "ButtonRelease";
888 name
= "MotionNotify";
891 name
= "EnterNotify";
894 name
= "LeaveNotify";
903 name
= "KeymapNotify";
909 name
= "GraphicsExpose";
914 case VisibilityNotify
:
915 name
= "VisibilityNotify";
918 name
= "CreateNotify";
921 name
= "DestroyNotify";
924 name
= "UnmapNotify";
933 name
= "ReparentNotify";
935 case ConfigureNotify
:
936 name
= "ConfigureNotify";
938 case ConfigureRequest
:
939 name
= "ConfigureRequest";
942 name
= "GravityNotify";
945 name
= "ResizeRequest";
947 case CirculateNotify
:
948 name
= "CirculateNotify";
950 case CirculateRequest
:
951 name
= "CirculateRequest";
954 name
= "PropertyNotify";
957 name
= "SelectionClear";
959 case SelectionRequest
:
960 name
= "SelectionRequest";
962 case SelectionNotify
:
963 name
= "SelectionNotify";
966 name
= "ColormapNotify";
969 name
= "ClientMessage";
972 name
= "MappingNotify";
977 DNPRINTF(SWM_D_EVENTQ
,"window: %lu event: %s (%d), %d "
979 e
->xany
.window
, name
, e
->type
, QLength(display
));
981 DNPRINTF(SWM_D_EVENTQ
, "window: %lu unknown event %d, %d "
983 e
->xany
.window
, e
->type
, QLength(display
));
987 dumpwins(struct swm_region
*r
, union arg
*args
)
991 XWindowAttributes wa
;
994 fprintf(stderr
, "invalid workspace\n");
998 fprintf(stderr
, "=== managed window list ws %02d ===\n", r
->ws
->idx
);
1000 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1001 state
= getstate(win
->id
);
1002 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1003 fprintf(stderr
, "window: %lu failed "
1004 "XGetWindowAttributes\n", win
->id
);
1005 fprintf(stderr
, "window: %lu map_state: %d state: %d "
1007 win
->id
, wa
.map_state
, state
, win
->transient
);
1010 fprintf(stderr
, "===== unmanaged window list =====\n");
1011 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1012 state
= getstate(win
->id
);
1013 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1014 fprintf(stderr
, "window: %lu failed "
1015 "XGetWindowAttributes\n", win
->id
);
1016 fprintf(stderr
, "window: %lu map_state: %d state: %d "
1018 win
->id
, wa
.map_state
, state
, win
->transient
);
1021 fprintf(stderr
, "=================================\n");
1024 #define dumpevent(e)
1026 dumpwins(struct swm_region
*r
, union arg
*args
)
1029 #endif /* SWM_DEBUG */
1031 void expose(XEvent
*);
1032 void keypress(XEvent
*);
1033 void buttonpress(XEvent
*);
1034 void configurerequest(XEvent
*);
1035 void configurenotify(XEvent
*);
1036 void destroynotify(XEvent
*);
1037 void enternotify(XEvent
*);
1038 void focusevent(XEvent
*);
1039 void mapnotify(XEvent
*);
1040 void mappingnotify(XEvent
*);
1041 void maprequest(XEvent
*);
1042 void propertynotify(XEvent
*);
1043 void unmapnotify(XEvent
*);
1044 void visibilitynotify(XEvent
*);
1045 void clientmessage(XEvent
*);
1047 void (*handler
[LASTEvent
])(XEvent
*) = {
1049 [KeyPress
] = keypress
,
1050 [ButtonPress
] = buttonpress
,
1051 [ConfigureRequest
] = configurerequest
,
1052 [ConfigureNotify
] = configurenotify
,
1053 [DestroyNotify
] = destroynotify
,
1054 [EnterNotify
] = enternotify
,
1055 [FocusIn
] = focusevent
,
1056 [FocusOut
] = focusevent
,
1057 [MapNotify
] = mapnotify
,
1058 [MappingNotify
] = mappingnotify
,
1059 [MapRequest
] = maprequest
,
1060 [PropertyNotify
] = propertynotify
,
1061 [UnmapNotify
] = unmapnotify
,
1062 [VisibilityNotify
] = visibilitynotify
,
1063 [ClientMessage
] = clientmessage
,
1069 int saved_errno
, status
;
1072 saved_errno
= errno
;
1076 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1081 if (errno
!= ECHILD
)
1082 warn("sighdlr: waitpid");
1083 #endif /* SWM_DEBUG */
1086 if (pid
== searchpid
)
1090 if (WIFEXITED(status
)) {
1091 if (WEXITSTATUS(status
) != 0)
1092 warnx("sighdlr: child exit status: %d",
1093 WEXITSTATUS(status
));
1095 warnx("sighdlr: child is terminated "
1097 #endif /* SWM_DEBUG */
1111 errno
= saved_errno
;
1117 struct pid_e
*p
= NULL
;
1119 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1124 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1133 name_to_color(char *colorname
)
1137 XColor screen_def
, exact_def
;
1138 unsigned long result
= 0;
1139 char cname
[32] = "#";
1141 cmap
= DefaultColormap(display
, screens
[0].idx
);
1142 status
= XAllocNamedColor(display
, cmap
, colorname
,
1143 &screen_def
, &exact_def
);
1145 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1146 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
1150 result
= screen_def
.pixel
;
1152 fprintf(stderr
, "color '%s' not found.\n", colorname
);
1158 setscreencolor(char *val
, int i
, int c
)
1160 if (i
> 0 && i
<= ScreenCount(display
)) {
1161 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1162 free(screens
[i
- 1].c
[c
].name
);
1163 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1165 } else if (i
== -1) {
1166 for (i
= 0; i
< ScreenCount(display
); i
++) {
1167 screens
[i
].c
[c
].color
= name_to_color(val
);
1168 free(screens
[i
].c
[c
].name
);
1169 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1173 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1174 i
, ScreenCount(display
));
1178 fancy_stacker(struct workspace
*ws
)
1180 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1181 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1182 snprintf(ws
->stacker
, sizeof ws
->stacker
, "[%d|%d]",
1183 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1184 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1185 snprintf(ws
->stacker
, sizeof ws
->stacker
, "[%d-%d]",
1186 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1190 plain_stacker(struct workspace
*ws
)
1192 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1193 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1194 strlcpy(ws
->stacker
, "[|]", sizeof ws
->stacker
);
1195 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1196 strlcpy(ws
->stacker
, "[-]", sizeof ws
->stacker
);
1200 custom_region(char *val
)
1202 unsigned int sidx
, x
, y
, w
, h
;
1204 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1205 errx(1, "invalid custom region, "
1206 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1207 if (sidx
< 1 || sidx
> ScreenCount(display
))
1208 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1209 sidx
, ScreenCount(display
));
1213 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1215 if (x
> DisplayWidth(display
, sidx
) ||
1216 y
> DisplayHeight(display
, sidx
) ||
1217 w
+ x
> DisplayWidth(display
, sidx
) ||
1218 h
+ y
> DisplayHeight(display
, sidx
)) {
1219 fprintf(stderr
, "ignoring region %ux%u+%u+%u "
1220 "- not within screen boundaries "
1221 "(%ux%u)\n", w
, h
, x
, y
,
1222 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
1226 new_region(&screens
[sidx
], x
, y
, w
, h
);
1230 socket_setnonblock(int fd
)
1234 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1235 err(1, "fcntl F_GETFL");
1236 flags
|= O_NONBLOCK
;
1237 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1238 err(1, "fcntl F_SETFL");
1242 bar_print(struct swm_region
*r
, char *s
)
1244 int textwidth
, x
= 0;
1247 XClearWindow(display
, r
->bar_window
);
1248 XSetForeground(display
, bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
1251 textwidth
= XTextWidth(bar_fs
, s
, len
);
1253 switch (bar_justify
) {
1254 case SWM_BAR_JUSTIFY_LEFT
:
1257 case SWM_BAR_JUSTIFY_CENTER
:
1258 x
= (WIDTH(r
) - textwidth
) / 2;
1260 case SWM_BAR_JUSTIFY_RIGHT
:
1261 x
= WIDTH(r
) - textwidth
- SWM_BAR_OFFSET
;
1265 if (x
< SWM_BAR_OFFSET
)
1268 XDrawString(display
, r
->bar_window
, bar_gc
, x
, bar_fs
->ascent
, s
, len
);
1272 bar_extra_stop(void)
1276 bzero(bar_pipe
, sizeof bar_pipe
);
1279 kill(bar_pid
, SIGTERM
);
1282 strlcpy(bar_ext
, "", sizeof bar_ext
);
1287 bar_class_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1289 int do_class
, do_name
;
1291 XClassHint
*xch
= NULL
;
1293 if ((title_name_enabled
== 1 || title_class_enabled
== 1) &&
1294 cur_focus
!= NULL
) {
1295 if ((xch
= XAllocClassHint()) == NULL
)
1297 status
= XGetClassHint(display
, cur_focus
->id
, xch
);
1298 if (status
== BadWindow
|| status
== BadAlloc
)
1300 do_class
= (title_class_enabled
&& xch
->res_class
!= NULL
);
1301 do_name
= (title_name_enabled
&& xch
->res_name
!= NULL
);
1303 strlcat(s
, xch
->res_class
, sz
);
1304 if (do_class
&& do_name
)
1305 strlcat(s
, ":", sz
);
1307 strlcat(s
, xch
->res_name
, sz
);
1308 strlcat(s
, " ", sz
);
1316 bar_window_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1320 if (window_name_enabled
&& cur_focus
!= NULL
) {
1321 XFetchName(display
, cur_focus
->id
, &title
);
1323 if (cur_focus
->floating
)
1324 strlcat(s
, "(f) ", sz
);
1325 strlcat(s
, title
, sz
);
1326 strlcat(s
, " ", sz
);
1332 int urgent
[SWM_WS_MAX
];
1334 bar_urgent(char *s
, ssize_t sz
)
1336 XWMHints
*wmh
= NULL
;
1341 if (urgent_enabled
== 0)
1344 for (i
= 0; i
< SWM_WS_MAX
; i
++)
1347 for (i
= 0; i
< ScreenCount(display
); i
++)
1348 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1349 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1350 wmh
= XGetWMHints(display
, win
->id
);
1354 if (wmh
->flags
& XUrgencyHint
)
1359 strlcat(s
, "* ", sz
);
1360 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
1362 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1364 snprintf(b
, sizeof b
, "- ");
1367 strlcat(s
, "* ", sz
);
1375 struct swm_region
*r
;
1378 char ws
[SWM_BAR_MAX
];
1379 char s
[SWM_BAR_MAX
];
1380 char cn
[SWM_BAR_MAX
];
1381 char loc
[SWM_BAR_MAX
];
1382 char *b
, *stack
= "";
1384 if (bar_enabled
== 0)
1386 if (bar_extra
&& bar_extra_running
) {
1387 /* ignore short reads; it'll correct itself */
1388 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1389 if (b
&& b
[len
- 1] == '\n') {
1391 strlcpy(bar_ext
, b
, sizeof bar_ext
);
1393 if (b
== NULL
&& errno
!= EAGAIN
) {
1394 fprintf(stderr
, "bar_extra failed: errno: %d %s\n",
1395 errno
, strerror(errno
));
1399 strlcpy(bar_ext
, "", sizeof bar_ext
);
1401 if (clock_enabled
== 0)
1402 strlcpy(s
, "", sizeof s
);
1405 localtime_r(&tmt
, &tm
);
1406 strftime(s
, sizeof s
, clock_format
, &tm
);
1407 strlcat(s
, " ", sizeof s
);
1410 for (i
= 0; i
< ScreenCount(display
); i
++) {
1412 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1413 strlcpy(cn
, "", sizeof cn
);
1414 strlcpy(ws
, "", sizeof ws
);
1416 bar_urgent(cn
, sizeof cn
);
1417 bar_class_name(cn
, sizeof cn
, r
->ws
->focus
);
1418 bar_window_name(cn
, sizeof cn
, r
->ws
->focus
);
1420 snprintf(ws
, sizeof ws
, "<%s>", r
->ws
->name
);
1423 stack
= r
->ws
->stacker
;
1425 snprintf(loc
, sizeof loc
, "%d:%d %s %s %s%s %s %s",
1426 x
++, r
->ws
->idx
+ 1, stack
, ws
, s
, cn
, bar_ext
,
1441 bar_toggle(struct swm_region
*r
, union arg
*args
)
1443 struct swm_region
*tmpr
;
1444 int i
, sc
= ScreenCount(display
);
1446 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
1449 for (i
= 0; i
< sc
; i
++)
1450 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1451 XUnmapWindow(display
, tmpr
->bar_window
);
1453 for (i
= 0; i
< sc
; i
++)
1454 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1455 XMapRaised(display
, tmpr
->bar_window
);
1457 bar_enabled
= !bar_enabled
;
1460 /* must be after stack */
1467 XSetWindowAttributes wa
;
1468 struct swm_region
*r
;
1471 /* do this here because the conf file is in memory */
1472 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1473 /* launch external status app */
1474 bar_extra_running
= 1;
1475 if (pipe(bar_pipe
) == -1)
1476 err(1, "pipe error");
1477 socket_setnonblock(bar_pipe
[0]);
1478 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1479 if (dup2(bar_pipe
[0], 0) == -1)
1481 if (dup2(bar_pipe
[1], 1) == -1)
1483 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1484 err(1, "could not disable SIGPIPE");
1485 switch (bar_pid
= fork()) {
1487 err(1, "cannot fork");
1491 execvp(bar_argv
[0], bar_argv
);
1492 err(1, "%s external app failed", bar_argv
[0]);
1494 default: /* parent */
1500 bzero(&wa
, sizeof wa
);
1501 for (i
= 0; i
< ScreenCount(display
); i
++)
1502 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1504 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1505 wa
.background_pixel
=
1506 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1507 XChangeWindowAttributes(display
, r
->bar_window
,
1508 CWBackPixel
| CWBorderPixel
, &wa
);
1514 bar_setup(struct swm_region
*r
)
1519 XFreeFont(display
, bar_fs
);
1523 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
1524 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
1530 if (bar_fonts
[i
] == NULL
)
1531 errx(1, "couldn't load font");
1533 errx(1, "couldn't create font structure");
1535 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 1 +
1536 2 * bar_border_width
;
1538 y
= bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1540 r
->bar_window
= XCreateSimpleWindow(display
,
1541 r
->s
->root
, x
, y
, WIDTH(r
) - 2 * bar_border_width
,
1542 bar_height
- 2 * bar_border_width
,
1543 bar_border_width
, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
1544 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1545 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
1546 XSetFont(display
, bar_gc
, bar_fs
->fid
);
1547 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
1549 XMapRaised(display
, r
->bar_window
);
1550 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
1552 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1553 err(1, "could not install bar_signal");
1558 drain_enter_notify(void)
1563 while (XCheckMaskEvent(display
, EnterWindowMask
, &cne
))
1566 DNPRINTF(SWM_D_MISC
, "drain_enter_notify: drained %d\n", i
);
1570 set_win_state(struct ws_win
*win
, long state
)
1572 long data
[] = {state
, None
};
1574 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
1579 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1580 (unsigned char *)data
, 2);
1587 unsigned char *p
= NULL
;
1590 if (!get_property(w
, astate
, 2L, astate
, &n
, &p
))
1593 result
= *((long *)p
);
1599 version(struct swm_region
*r
, union arg
*args
)
1601 bar_version
= !bar_version
;
1603 snprintf(bar_vertext
, sizeof bar_vertext
, "Version: %s Build: %s",
1604 SCROTWM_VERSION
, buildstr
);
1606 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
1611 client_msg(struct ws_win
*win
, Atom a
)
1613 XClientMessageEvent cm
;
1618 bzero(&cm
, sizeof cm
);
1619 cm
.type
= ClientMessage
;
1620 cm
.window
= win
->id
;
1621 cm
.message_type
= aprot
;
1624 cm
.data
.l
[1] = CurrentTime
;
1625 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
1629 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
1637 DNPRINTF(SWM_D_MISC
,
1638 "config_win: win %lu x %d y %d w %d h %d\n",
1639 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
1641 ce
.type
= ConfigureNotify
;
1642 ce
.display
= display
;
1644 ce
.window
= win
->id
;
1647 ce
.width
= win
->g
.w
;
1648 ce
.height
= win
->g
.h
;
1649 ce
.border_width
= border_width
;
1651 ce
.override_redirect
= False
;
1653 DNPRINTF(SWM_D_MISC
,
1654 "config_win: ev win %lu x %d y %d w %d h %d\n",
1655 ev
->window
, ev
->x
, ev
->y
, ev
->width
, ev
->height
);
1656 ce
.type
= ConfigureNotify
;
1657 ce
.display
= ev
->display
;
1658 ce
.event
= ev
->window
;
1659 ce
.window
= ev
->window
;
1662 ce
.width
= ev
->width
;
1663 ce
.height
= ev
->height
;
1664 ce
.border_width
= ev
->border_width
;
1665 ce
.above
= ev
->above
;
1666 ce
.override_redirect
= False
;
1669 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
1673 count_win(struct workspace
*ws
, int count_transient
)
1678 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1679 if (count_transient
== 0 && win
->floating
)
1681 if (count_transient
== 0 && win
->transient
)
1687 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
1693 quit(struct swm_region
*r
, union arg
*args
)
1695 DNPRINTF(SWM_D_MISC
, "quit\n");
1700 unmap_window(struct ws_win
*win
)
1705 /* don't unmap again */
1706 if (getstate(win
->id
) == IconicState
)
1709 set_win_state(win
, IconicState
);
1711 XUnmapWindow(display
, win
->id
);
1712 XSetWindowBorder(display
, win
->id
,
1713 win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1722 for (i
= 0; i
< ScreenCount(display
); i
++)
1723 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1724 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1729 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
1736 event
.display
= display
; /* Ignored, but what the hell */
1737 event
.window
= win
->id
;
1738 event
.root
= win
->s
->root
;
1739 event
.subwindow
= None
;
1740 event
.time
= CurrentTime
;
1745 event
.same_screen
= True
;
1746 event
.keycode
= XKeysymToKeycode(display
, keysym
);
1747 event
.state
= modifiers
;
1749 event
.type
= KeyPress
;
1750 XSendEvent(event
.display
, event
.window
, True
,
1751 KeyPressMask
, (XEvent
*)&event
);
1753 event
.type
= KeyRelease
;
1754 XSendEvent(event
.display
, event
.window
, True
,
1755 KeyPressMask
, (XEvent
*)&event
);
1760 restart(struct swm_region
*r
, union arg
*args
)
1762 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
1764 /* disable alarm because the following code may not be interrupted */
1766 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
1767 err(1, "can't disable alarm");
1772 XCloseDisplay(display
);
1773 execvp(start_argv
[0], start_argv
);
1774 fprintf(stderr
, "execvp failed\n");
1780 root_to_region(Window root
)
1782 struct swm_region
*r
= NULL
;
1784 int i
, x
, y
, wx
, wy
;
1787 for (i
= 0; i
< ScreenCount(display
); i
++)
1788 if (screens
[i
].root
== root
)
1791 if (XQueryPointer(display
, screens
[i
].root
,
1792 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
1793 /* choose a region based on pointer location */
1794 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1795 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
1796 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
1801 r
= TAILQ_FIRST(&screens
[i
].rl
);
1807 find_unmanaged_window(Window id
)
1812 for (i
= 0; i
< ScreenCount(display
); i
++)
1813 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1814 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
1822 find_window(Window id
)
1825 Window wrr
, wpr
, *wcr
= NULL
;
1829 for (i
= 0; i
< ScreenCount(display
); i
++)
1830 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1831 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1835 /* if we were looking for the parent return that window instead */
1836 if (XQueryTree(display
, id
, &wrr
, &wpr
, &wcr
, &nc
) == 0)
1841 /* ignore not found and root */
1842 if (wpr
== 0 || wrr
== wpr
)
1845 /* look for parent */
1846 for (i
= 0; i
< ScreenCount(display
); i
++)
1847 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1848 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1856 spawn(int ws_idx
, union arg
*args
, int close_fd
)
1861 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
1864 close(ConnectionNumber(display
));
1866 setenv("LD_PRELOAD", SWM_LIB
, 1);
1868 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
1872 setenv("_SWM_WS", ret
, 1);
1876 if (asprintf(&ret
, "%d", getpid()) == -1) {
1880 setenv("_SWM_PID", ret
, 1);
1884 if (setsid() == -1) {
1891 * close stdin and stdout to prevent interaction between apps
1892 * and the baraction script
1893 * leave stderr open to record errors
1895 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
1899 dup2(fd
, STDIN_FILENO
);
1900 dup2(fd
, STDOUT_FILENO
);
1905 execvp(args
->argv
[0], args
->argv
);
1912 spawnterm(struct swm_region
*r
, union arg
*args
)
1914 DNPRINTF(SWM_D_MISC
, "spawnterm\n");
1918 setenv("_SWM_XTERM_FONTADJ", "", 1);
1919 spawn(r
->ws
->idx
, args
, 1);
1924 kill_refs(struct ws_win
*win
)
1927 struct swm_region
*r
;
1928 struct workspace
*ws
;
1933 for (i
= 0; i
< ScreenCount(display
); i
++)
1934 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1935 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
1937 if (win
== ws
->focus
)
1939 if (win
== ws
->focus_prev
)
1940 ws
->focus_prev
= NULL
;
1945 validate_win(struct ws_win
*testwin
)
1948 struct workspace
*ws
;
1949 struct swm_region
*r
;
1952 if (testwin
== NULL
)
1955 for (i
= 0; i
< ScreenCount(display
); i
++)
1956 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1957 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
1959 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
1967 validate_ws(struct workspace
*testws
)
1969 struct swm_region
*r
;
1970 struct workspace
*ws
;
1973 /* validate all ws */
1974 for (i
= 0; i
< ScreenCount(display
); i
++)
1975 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1976 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
1985 unfocus_win(struct ws_win
*win
)
1990 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: id: %lu\n", WINID(win
));
1994 if (win
->ws
== NULL
)
1997 if (validate_ws(win
->ws
))
1998 return; /* XXX this gets hit with thunderbird, needs fixing */
2000 if (win
->ws
->r
== NULL
)
2003 if (validate_win(win
)) {
2008 if (win
->ws
->focus
== win
) {
2009 win
->ws
->focus
= NULL
;
2010 win
->ws
->focus_prev
= win
;
2013 if (validate_win(win
->ws
->focus
)) {
2014 kill_refs(win
->ws
->focus
);
2015 win
->ws
->focus
= NULL
;
2017 if (validate_win(win
->ws
->focus_prev
)) {
2018 kill_refs(win
->ws
->focus_prev
);
2019 win
->ws
->focus_prev
= NULL
;
2022 /* drain all previous unfocus events */
2023 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
2026 grabbuttons(win
, 0);
2027 XSetWindowBorder(display
, win
->id
,
2028 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2030 XChangeProperty(display
, win
->s
->root
,
2031 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2032 PropModeReplace
, (unsigned char *)&none
,1);
2041 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2043 for (i
= 0; i
< ScreenCount(display
); i
++)
2044 for (j
= 0; j
< SWM_WS_MAX
; j
++)
2045 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2050 focus_win(struct ws_win
*win
)
2055 struct ws_win
*cfw
= NULL
;
2058 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
? win
->id
: 0);
2062 if (win
->ws
== NULL
)
2065 if (validate_ws(win
->ws
))
2066 return; /* XXX this gets hit with thunderbird, needs fixing */
2068 if (validate_win(win
)) {
2073 if (validate_win(win
)) {
2078 XGetInputFocus(display
, &cur_focus
, &rr
);
2079 if ((cfw
= find_window(cur_focus
)) != NULL
)
2082 /* use larger hammer since the window was killed somehow */
2083 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2084 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2085 XSetWindowBorder(display
, cfw
->id
,
2086 cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2089 win
->ws
->focus
= win
;
2091 if (win
->ws
->r
!= NULL
) {
2092 /* drain all previous focus events */
2093 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
2097 XSetInputFocus(display
, win
->id
,
2098 RevertToParent
, CurrentTime
);
2099 grabbuttons(win
, 1);
2100 XSetWindowBorder(display
, win
->id
,
2101 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2102 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2103 win
->ws
->always_raise
)
2104 XMapRaised(display
, win
->id
);
2106 XChangeProperty(display
, win
->s
->root
,
2107 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2108 PropModeReplace
, (unsigned char *)&win
->id
,1);
2111 if (window_name_enabled
)
2116 switchws(struct swm_region
*r
, union arg
*args
)
2118 int wsid
= args
->id
, unmap_old
= 0;
2119 struct swm_region
*this_r
, *other_r
;
2121 struct workspace
*new_ws
, *old_ws
;
2128 old_ws
= this_r
->ws
;
2129 new_ws
= &this_r
->s
->ws
[wsid
];
2131 DNPRINTF(SWM_D_WS
, "switchws screen[%d]:%dx%d+%d+%d: "
2132 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
2135 if (new_ws
== NULL
|| old_ws
== NULL
)
2137 if (new_ws
== old_ws
)
2140 other_r
= new_ws
->r
;
2141 if (other_r
== NULL
) {
2142 /* the other workspace is hidden, hide this one */
2146 /* the other ws is visible in another region, exchange them */
2147 other_r
->ws_prior
= new_ws
;
2148 other_r
->ws
= old_ws
;
2149 old_ws
->r
= other_r
;
2151 this_r
->ws_prior
= old_ws
;
2152 this_r
->ws
= new_ws
;
2155 /* this is needed so that we can click on a window after a restart */
2159 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2160 focus(new_ws
->r
, &a
);
2164 /* unmap old windows */
2166 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2169 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2170 drain_enter_notify();
2174 cyclews(struct swm_region
*r
, union arg
*args
)
2177 struct swm_screen
*s
= r
->s
;
2180 DNPRINTF(SWM_D_WS
, "cyclews id %d "
2181 "in screen[%d]:%dx%d+%d+%d ws %d\n", args
->id
,
2182 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2187 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2190 case SWM_ARG_ID_CYCLEWS_UP
:
2191 if (a
.id
< SWM_WS_MAX
- 1)
2196 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2199 case SWM_ARG_ID_CYCLEWS_DOWN
:
2203 a
.id
= SWM_WS_MAX
- 1;
2210 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2212 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2216 } while (a
.id
!= r
->ws
->idx
);
2220 priorws(struct swm_region
*r
, union arg
*args
)
2224 DNPRINTF(SWM_D_WS
, "priorws id %d "
2225 "in screen[%d]:%dx%d+%d+%d ws %d\n", args
->id
,
2226 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2228 if (r
->ws_prior
== NULL
)
2231 a
.id
= r
->ws_prior
->idx
;
2236 cyclescr(struct swm_region
*r
, union arg
*args
)
2238 struct swm_region
*rr
= NULL
;
2242 /* do nothing if we don't have more than one screen */
2243 if (!(ScreenCount(display
) > 1 || outputs
> 1))
2248 case SWM_ARG_ID_CYCLESC_UP
:
2249 rr
= TAILQ_NEXT(r
, entry
);
2251 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2253 case SWM_ARG_ID_CYCLESC_DOWN
:
2254 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2256 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2264 /* move mouse to region */
2266 y
= rr
->g
.y
+ 1 + (bar_enabled
? bar_height
: 0);
2267 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2269 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2272 if (rr
->ws
->focus
) {
2273 /* move to focus window */
2274 x
= rr
->ws
->focus
->g
.x
+ 1;
2275 y
= rr
->ws
->focus
->g
.y
+ 1;
2276 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2281 sort_windows(struct ws_win_list
*wl
)
2283 struct ws_win
*win
, *parent
, *nxt
;
2288 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2289 nxt
= TAILQ_NEXT(win
, entry
);
2290 if (win
->transient
) {
2291 parent
= find_window(win
->transient
);
2292 if (parent
== NULL
) {
2293 fprintf(stderr
, "not possible bug\n");
2296 TAILQ_REMOVE(wl
, win
, entry
);
2297 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2304 swapwin(struct swm_region
*r
, union arg
*args
)
2306 struct ws_win
*target
, *source
;
2307 struct ws_win
*cur_focus
;
2308 struct ws_win_list
*wl
;
2311 DNPRINTF(SWM_D_WS
, "swapwin id %d "
2312 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
2313 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2315 cur_focus
= r
->ws
->focus
;
2316 if (cur_focus
== NULL
)
2320 wl
= &source
->ws
->winlist
;
2323 case SWM_ARG_ID_SWAPPREV
:
2324 if (source
->transient
)
2325 source
= find_window(source
->transient
);
2326 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2327 if (target
&& target
->transient
)
2328 target
= find_window(target
->transient
);
2329 TAILQ_REMOVE(wl
, source
, entry
);
2331 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2333 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2335 case SWM_ARG_ID_SWAPNEXT
:
2336 target
= TAILQ_NEXT(source
, entry
);
2337 /* move the parent and let the sort handle the move */
2338 if (source
->transient
)
2339 source
= find_window(source
->transient
);
2340 TAILQ_REMOVE(wl
, source
, entry
);
2342 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2344 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2346 case SWM_ARG_ID_SWAPMAIN
:
2347 target
= TAILQ_FIRST(wl
);
2348 if (target
== source
) {
2349 if (source
->ws
->focus_prev
!= NULL
&&
2350 source
->ws
->focus_prev
!= target
)
2352 source
= source
->ws
->focus_prev
;
2356 if (target
== NULL
|| source
== NULL
)
2358 source
->ws
->focus_prev
= target
;
2359 TAILQ_REMOVE(wl
, target
, entry
);
2360 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2361 TAILQ_REMOVE(wl
, source
, entry
);
2362 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2364 case SWM_ARG_ID_MOVELAST
:
2365 TAILQ_REMOVE(wl
, source
, entry
);
2366 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2369 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
2379 focus_prev(struct ws_win
*win
)
2381 struct ws_win
*winfocus
= NULL
, *winlostfocus
= NULL
;
2382 struct ws_win
*cur_focus
= NULL
;
2383 struct ws_win_list
*wl
= NULL
;
2384 struct workspace
*ws
= NULL
;
2386 DNPRINTF(SWM_D_FOCUS
, "focus_prev: id %lu\n", WINID(win
));
2388 if (!(win
&& win
->ws
))
2393 cur_focus
= ws
->focus
;
2394 winlostfocus
= cur_focus
;
2396 /* pickle, just focus on whatever */
2397 if (cur_focus
== NULL
) {
2398 /* use prev_focus if valid */
2399 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2400 find_window(WINID(ws
->focus_prev
)))
2401 winfocus
= ws
->focus_prev
;
2402 if (winfocus
== NULL
)
2403 winfocus
= TAILQ_FIRST(wl
);
2407 /* if transient focus on parent */
2408 if (cur_focus
->transient
) {
2409 winfocus
= find_window(cur_focus
->transient
);
2413 /* if in max_stack try harder */
2414 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2415 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2416 if (cur_focus
!= ws
->focus_prev
)
2417 winfocus
= ws
->focus_prev
;
2418 else if (cur_focus
!= ws
->focus
)
2419 winfocus
= ws
->focus
;
2421 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2426 if (cur_focus
== win
)
2427 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2428 if (winfocus
== NULL
)
2429 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2430 if (winfocus
== NULL
|| winfocus
== win
)
2431 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2433 if (winfocus
== winlostfocus
|| winfocus
== NULL
) {
2434 /* update the bar so that title/class/name will be cleared. */
2435 if (window_name_enabled
|| title_name_enabled
||
2436 title_class_enabled
)
2442 focus_magic(winfocus
);
2446 focus(struct swm_region
*r
, union arg
*args
)
2448 struct ws_win
*winfocus
= NULL
, *winlostfocus
= NULL
, *head
;
2449 struct ws_win
*cur_focus
= NULL
;
2450 struct ws_win_list
*wl
= NULL
;
2451 struct workspace
*ws
= NULL
;
2457 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
2459 /* treat FOCUS_CUR special */
2460 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2461 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2462 winfocus
= r
->ws
->focus
;
2463 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2464 winfocus
= r
->ws
->focus_prev
;
2466 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2467 if (winfocus
->iconic
== 0)
2470 focus_magic(winfocus
);
2474 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2478 if (TAILQ_EMPTY(wl
))
2480 /* make sure there is at least one uniconified window */
2482 TAILQ_FOREACH(winfocus
, wl
, entry
)
2483 if (winfocus
->iconic
== 0) {
2490 winlostfocus
= cur_focus
;
2493 case SWM_ARG_ID_FOCUSPREV
:
2494 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2496 head
= TAILQ_LAST(wl
, ws_win_list
);
2498 if (WINID(winfocus
) == cur_focus
->transient
) {
2499 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2501 head
= TAILQ_LAST(wl
, ws_win_list
);
2506 if (winfocus
&& winfocus
->iconic
) {
2507 while (winfocus
!= cur_focus
) {
2508 if (winfocus
== NULL
)
2509 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2510 if (winfocus
->iconic
== 0)
2512 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2517 case SWM_ARG_ID_FOCUSNEXT
:
2518 head
= TAILQ_NEXT(cur_focus
, entry
);
2520 head
= TAILQ_FIRST(wl
);
2524 if (winfocus
&& winfocus
->iconic
) {
2525 while (winfocus
!= cur_focus
) {
2526 if (winfocus
== NULL
)
2527 winfocus
= TAILQ_FIRST(wl
);
2528 if (winfocus
->iconic
== 0)
2530 winfocus
= TAILQ_NEXT(winfocus
, entry
);
2535 case SWM_ARG_ID_FOCUSMAIN
:
2536 winfocus
= TAILQ_FIRST(wl
);
2537 if (winfocus
== cur_focus
)
2538 winfocus
= cur_focus
->ws
->focus_prev
;
2544 if (winfocus
== winlostfocus
|| winfocus
== NULL
) {
2545 /* update the bar so that title/class/name will be cleared. */
2546 if (window_name_enabled
|| title_name_enabled
||
2547 title_class_enabled
)
2553 focus_magic(winfocus
);
2557 cycle_layout(struct swm_region
*r
, union arg
*args
)
2559 struct workspace
*ws
= r
->ws
;
2562 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2565 if (ws
->cur_layout
->l_stack
== NULL
)
2566 ws
->cur_layout
= &layouts
[0];
2569 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2570 drain_enter_notify();
2571 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2577 stack_config(struct swm_region
*r
, union arg
*args
)
2579 struct workspace
*ws
= r
->ws
;
2581 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
2584 if (ws
->cur_layout
->l_config
!= NULL
)
2585 ws
->cur_layout
->l_config(ws
, args
->id
);
2587 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
2594 struct swm_geometry g
;
2595 struct swm_region
*r
;
2601 DNPRINTF(SWM_D_STACK
, "stack\n");
2603 for (i
= 0; i
< ScreenCount(display
); i
++) {
2607 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2608 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
2609 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
2611 /* start with screen geometry, adjust for bar */
2613 g
.w
-= 2 * border_width
;
2614 g
.h
-= 2 * border_width
;
2620 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
2621 r
->ws
->cur_layout
->l_string(r
->ws
);
2622 /* save r so we can track region changes */
2629 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2630 drain_enter_notify();
2634 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
2636 /* retain window geom and region geom */
2637 win
->g_float
.x
= win
->g
.x
;
2638 win
->g_float
.y
= win
->g
.y
;
2639 win
->g_float
.w
= win
->g
.w
;
2640 win
->g_float
.h
= win
->g
.h
;
2641 win
->rg_float
.x
= r
->g
.x
;
2642 win
->rg_float
.y
= r
->g
.y
;
2643 win
->rg_float
.w
= r
->g
.w
;
2644 win
->rg_float
.h
= r
->g
.h
;
2645 win
->g_floatvalid
= 1;
2649 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
2657 bzero(&wc
, sizeof wc
);
2658 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
2661 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2662 * geom on ws switches or return from max mode
2665 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&& win
->g_floatvalid
2666 && !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
2668 * use stored g and rg to set relative position and size
2669 * as in old region or before max stack mode
2671 win
->g
.x
= win
->g_float
.x
- win
->rg_float
.x
+ r
->g
.x
;
2672 win
->g
.y
= win
->g_float
.y
- win
->rg_float
.y
+ r
->g
.y
;
2673 win
->g
.w
= win
->g_float
.w
;
2674 win
->g
.h
= win
->g_float
.h
;
2675 win
->g_floatvalid
= 0;
2678 win
->floatmaxed
= 0;
2680 if ((win
->quirks
& SWM_Q_FULLSCREEN
) && (win
->g
.w
>= WIDTH(r
)) &&
2681 (win
->g
.h
>= HEIGHT(r
)))
2682 wc
.border_width
= 0;
2684 wc
.border_width
= border_width
;
2685 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
2686 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
2687 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
2692 * floaters and transients are auto-centred unless moved
2695 win
->g
.x
= r
->g
.x
+ (WIDTH(r
) - win
->g
.w
) /
2696 2 - wc
.border_width
;
2697 win
->g
.y
= r
->g
.y
+ (HEIGHT(r
) - win
->g
.h
) /
2698 2 - wc
.border_width
;
2701 /* win can be outside r if new r smaller than old r */
2702 /* Ensure top left corner inside r (move probs otherwise) */
2703 if (win
->g
.x
< r
->g
.x
- wc
.border_width
)
2704 win
->g
.x
= r
->g
.x
- wc
.border_width
;
2705 if (win
->g
.x
> r
->g
.x
+ r
->g
.w
- 1)
2706 win
->g
.x
= (win
->g
.w
> r
->g
.w
) ? r
->g
.x
:
2707 (r
->g
.x
+ r
->g
.w
- win
->g
.w
- 2 * wc
.border_width
);
2708 if (win
->g
.y
< r
->g
.y
- wc
.border_width
)
2709 win
->g
.y
= r
->g
.y
- wc
.border_width
;
2710 if (win
->g
.y
> r
->g
.y
+ r
->g
.h
- 1)
2711 win
->g
.y
= (win
->g
.h
> r
->g
.h
) ? r
->g
.y
:
2712 (r
->g
.y
+ r
->g
.h
- win
->g
.h
- 2 * wc
.border_width
);
2716 wc
.width
= win
->g
.w
;
2717 wc
.height
= win
->g
.h
;
2720 * Retain floater and transient geometry for correct positioning
2721 * when ws changes region
2723 if (!(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
2724 store_float_geom(win
, r
);
2726 DNPRINTF(SWM_D_MISC
, "stack_floater: win %lu x %d y %d w %d h %d\n",
2727 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
2729 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2733 * Send keystrokes to terminal to decrease/increase the font size as the
2734 * window size changes.
2737 adjust_font(struct ws_win
*win
)
2739 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
2740 win
->floating
|| win
->transient
)
2743 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
2744 win
->g
.w
/ win
->sh
.width_inc
< term_width
&&
2745 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
2746 win
->font_size_boundary
[win
->font_steps
] =
2747 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
2750 win
->last_inc
= win
->sh
.width_inc
;
2751 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
2752 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
2753 win
->g
.w
> win
->font_size_boundary
[win
->font_steps
- 1]) {
2756 win
->last_inc
= win
->sh
.width_inc
;
2757 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
2761 #define SWAPXY(g) do { \
2763 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2764 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2767 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
2770 XWindowAttributes wa
;
2771 struct swm_geometry win_g
, r_g
= *g
;
2772 struct ws_win
*win
, *fs_win
= 0;
2773 int i
, j
, s
, stacks
;
2774 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
2775 int hrh
, extra
= 0, h_slice
, last_h
= 0;
2776 int split
, colno
, winno
, mwin
, msize
, mscale
;
2777 int remain
, missing
, v_slice
, reconfigure
;
2780 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d\n rot=%s flip=%s",
2781 ws
->idx
, rot
? "yes" : "no", flip
? "yes" : "no");
2783 winno
= count_win(ws
, 0);
2784 if (winno
== 0 && count_win(ws
, 1) == 0)
2787 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2788 if (win
->transient
== 0 && win
->floating
== 0
2789 && win
->iconic
== 0)
2796 w_inc
= win
->sh
.width_inc
;
2797 w_base
= win
->sh
.base_width
;
2798 mwin
= ws
->l_state
.horizontal_mwin
;
2799 mscale
= ws
->l_state
.horizontal_msize
;
2800 stacks
= ws
->l_state
.horizontal_stacks
;
2803 w_inc
= win
->sh
.height_inc
;
2804 w_base
= win
->sh
.base_height
;
2805 mwin
= ws
->l_state
.vertical_mwin
;
2806 mscale
= ws
->l_state
.vertical_msize
;
2807 stacks
= ws
->l_state
.vertical_stacks
;
2811 if (stacks
> winno
- mwin
)
2812 stacks
= winno
- mwin
;
2816 h_slice
= r_g
.h
/ SWM_H_SLICE
;
2817 if (mwin
&& winno
> mwin
) {
2818 v_slice
= r_g
.w
/ SWM_V_SLICE
;
2822 win_g
.w
= v_slice
* mscale
;
2824 if (w_inc
> 1 && w_inc
< v_slice
) {
2825 /* adjust for window's requested size increment */
2826 remain
= (win_g
.w
- w_base
) % w_inc
;
2827 missing
= w_inc
- remain
;
2834 win_g
.x
+= r_g
.w
- msize
;
2837 colno
= split
= winno
/ stacks
;
2838 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
2839 2 * border_width
) / stacks
);
2841 hrh
= r_g
.h
/ colno
;
2842 extra
= r_g
.h
- (colno
* hrh
);
2843 win_g
.h
= hrh
- 2 * border_width
;
2845 /* stack all the tiled windows */
2846 i
= j
= 0, s
= stacks
;
2847 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2848 if (win
->transient
!= 0 || win
->floating
!= 0)
2850 if (win
->iconic
!= 0)
2853 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
2858 if (split
&& i
== split
) {
2859 colno
= (winno
- mwin
) / stacks
;
2860 if (s
<= (winno
- mwin
) % stacks
)
2862 split
= split
+ colno
;
2863 hrh
= (r_g
.h
/ colno
);
2864 extra
= r_g
.h
- (colno
* hrh
);
2868 win_g
.x
+= win_g
.w
+ 2 * border_width
;
2869 win_g
.w
= (r_g
.w
- msize
-
2870 (stacks
* 2 * border_width
)) / stacks
;
2872 win_g
.w
+= (r_g
.w
- msize
-
2873 (stacks
* 2 * border_width
)) % stacks
;
2877 win_g
.h
= hrh
- 2 * border_width
;
2879 h_inc
= win
->sh
.width_inc
;
2880 h_base
= win
->sh
.base_width
;
2882 h_inc
= win
->sh
.height_inc
;
2883 h_base
= win
->sh
.base_height
;
2885 if (j
== colno
- 1) {
2886 win_g
.h
= hrh
+ extra
;
2887 } else if (h_inc
> 1 && h_inc
< h_slice
) {
2888 /* adjust for window's requested size increment */
2889 remain
= (win_g
.h
- h_base
) % h_inc
;
2890 missing
= h_inc
- remain
;
2892 if (missing
<= extra
|| j
== 0) {
2904 win_g
.y
+= last_h
+ 2 * border_width
;
2906 bzero(&wc
, sizeof wc
);
2907 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
2908 wc
.border_width
= 0;
2909 win_g
.w
+= 2 * border_width
;
2910 win_g
.h
+= 2 * border_width
;
2912 wc
.border_width
= border_width
;
2915 if (win
->g
.x
!= win_g
.y
|| win
->g
.y
!= win_g
.x
||
2916 win
->g
.w
!= win_g
.h
|| win
->g
.h
!= win_g
.w
) {
2918 win
->g
.x
= wc
.x
= win_g
.y
;
2919 win
->g
.y
= wc
.y
= win_g
.x
;
2920 win
->g
.w
= wc
.width
= win_g
.h
;
2921 win
->g
.h
= wc
.height
= win_g
.w
;
2924 if (win
->g
.x
!= win_g
.x
|| win
->g
.y
!= win_g
.y
||
2925 win
->g
.w
!= win_g
.w
|| win
->g
.h
!= win_g
.h
) {
2927 win
->g
.x
= wc
.x
= win_g
.x
;
2928 win
->g
.y
= wc
.y
= win_g
.y
;
2929 win
->g
.w
= wc
.width
= win_g
.w
;
2930 win
->g
.h
= wc
.height
= win_g
.h
;
2935 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
2936 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2939 if (XGetWindowAttributes(display
, win
->id
, &wa
))
2940 if (wa
.map_state
== IsUnmapped
)
2941 XMapRaised(display
, win
->id
);
2949 /* now, stack all the floaters and transients */
2950 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2951 if (win
->transient
== 0 && win
->floating
== 0)
2953 if (win
->iconic
== 1)
2955 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
2960 stack_floater(win
, ws
->r
);
2961 XMapRaised(display
, win
->id
);
2965 stack_floater(fs_win
, ws
->r
);
2966 XMapRaised(display
, fs_win
->id
);
2971 vertical_config(struct workspace
*ws
, int id
)
2973 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
2976 case SWM_ARG_ID_STACKRESET
:
2977 case SWM_ARG_ID_STACKINIT
:
2978 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
2979 ws
->l_state
.vertical_mwin
= 1;
2980 ws
->l_state
.vertical_stacks
= 1;
2982 case SWM_ARG_ID_MASTERSHRINK
:
2983 if (ws
->l_state
.vertical_msize
> 1)
2984 ws
->l_state
.vertical_msize
--;
2986 case SWM_ARG_ID_MASTERGROW
:
2987 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
2988 ws
->l_state
.vertical_msize
++;
2990 case SWM_ARG_ID_MASTERADD
:
2991 ws
->l_state
.vertical_mwin
++;
2993 case SWM_ARG_ID_MASTERDEL
:
2994 if (ws
->l_state
.vertical_mwin
> 0)
2995 ws
->l_state
.vertical_mwin
--;
2997 case SWM_ARG_ID_STACKINC
:
2998 ws
->l_state
.vertical_stacks
++;
3000 case SWM_ARG_ID_STACKDEC
:
3001 if (ws
->l_state
.vertical_stacks
> 1)
3002 ws
->l_state
.vertical_stacks
--;
3010 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3012 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3014 stack_master(ws
, g
, 0, 0);
3018 horizontal_config(struct workspace
*ws
, int id
)
3020 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3023 case SWM_ARG_ID_STACKRESET
:
3024 case SWM_ARG_ID_STACKINIT
:
3025 ws
->l_state
.horizontal_mwin
= 1;
3026 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3027 ws
->l_state
.horizontal_stacks
= 1;
3029 case SWM_ARG_ID_MASTERSHRINK
:
3030 if (ws
->l_state
.horizontal_msize
> 1)
3031 ws
->l_state
.horizontal_msize
--;
3033 case SWM_ARG_ID_MASTERGROW
:
3034 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3035 ws
->l_state
.horizontal_msize
++;
3037 case SWM_ARG_ID_MASTERADD
:
3038 ws
->l_state
.horizontal_mwin
++;
3040 case SWM_ARG_ID_MASTERDEL
:
3041 if (ws
->l_state
.horizontal_mwin
> 0)
3042 ws
->l_state
.horizontal_mwin
--;
3044 case SWM_ARG_ID_STACKINC
:
3045 ws
->l_state
.horizontal_stacks
++;
3047 case SWM_ARG_ID_STACKDEC
:
3048 if (ws
->l_state
.horizontal_stacks
> 1)
3049 ws
->l_state
.horizontal_stacks
--;
3057 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3059 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3061 stack_master(ws
, g
, 1, 0);
3064 /* fullscreen view */
3066 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3069 struct swm_geometry gg
= *g
;
3070 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3074 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3079 winno
= count_win(ws
, 0);
3080 if (winno
== 0 && count_win(ws
, 1) == 0)
3083 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3084 if (win
->transient
) {
3086 parent
= find_window(win
->transient
);
3090 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3092 * retain geometry for retrieval on exit from
3095 store_float_geom(win
, ws
->r
);
3096 win
->floatmaxed
= 1;
3099 /* only reconfigure if necessary */
3100 if (win
->g
.x
!= gg
.x
|| win
->g
.y
!= gg
.y
|| win
->g
.w
!= gg
.w
||
3102 bzero(&wc
, sizeof wc
);
3103 win
->g
.x
= wc
.x
= gg
.x
;
3104 win
->g
.y
= wc
.y
= gg
.y
;
3106 wc
.border_width
= border_width
;
3107 win
->g
.w
= wc
.width
= gg
.w
;
3108 win
->g
.h
= wc
.height
= gg
.h
;
3110 wc
.border_width
= 0;
3111 win
->g
.w
= wc
.width
= gg
.w
+ 2 * border_width
;
3112 win
->g
.h
= wc
.height
= gg
.h
+ 2 * border_width
;
3114 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
3115 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3117 /* unmap only if we don't have multi screen */
3118 if (win
!= ws
->focus
)
3119 if (!(ScreenCount(display
) > 1 || outputs
> 1))
3123 /* put the last transient on top */
3126 XMapRaised(display
, parent
->id
);
3127 stack_floater(wintrans
, ws
->r
);
3128 focus_magic(wintrans
);
3133 send_to_ws(struct swm_region
*r
, union arg
*args
)
3135 int wsid
= args
->id
;
3136 struct ws_win
*win
= NULL
, *parent
;
3137 struct workspace
*ws
, *nws
;
3138 Atom ws_idx_atom
= 0;
3139 unsigned char ws_idx_str
[SWM_PROPLEN
];
3142 if (r
&& r
->ws
&& r
->ws
->focus
)
3148 if (win
->ws
->idx
== wsid
)
3151 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
3154 nws
= &win
->s
->ws
[wsid
];
3156 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3158 if (win
->transient
) {
3159 parent
= find_window(win
->transient
);
3161 unmap_window(parent
);
3162 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3163 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3168 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3169 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3170 if (TAILQ_EMPTY(&ws
->winlist
))
3171 r
->ws
->focus
= NULL
;
3174 /* Try to update the window's workspace property */
3175 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
3177 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3179 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
3181 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
3182 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
3189 pressbutton(struct swm_region
*r
, union arg
*args
)
3191 XTestFakeButtonEvent(display
, args
->id
, True
, CurrentTime
);
3192 XTestFakeButtonEvent(display
, args
->id
, False
, CurrentTime
);
3196 raise_toggle(struct swm_region
*r
, union arg
*args
)
3198 if (r
&& r
->ws
== NULL
)
3201 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3203 /* bring floaters back to top */
3204 if (r
->ws
->always_raise
== 0)
3209 iconify(struct swm_region
*r
, union arg
*args
)
3213 if (r
->ws
->focus
== NULL
)
3215 unmap_window(r
->ws
->focus
);
3216 update_iconic(r
->ws
->focus
, 1);
3218 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3219 drain_enter_notify();
3220 r
->ws
->focus
= NULL
;
3221 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3226 get_win_name(Display
*dpy
, Window win
, Atom wname
, Atom stype
,
3227 unsigned long *slen
)
3230 unsigned long nitems
, nbytes
, nextra
;
3231 unsigned char *prop
= NULL
;
3234 status
= XGetWindowProperty(dpy
, win
, wname
, 0L, 0L, False
, stype
,
3235 &rettype
, &retfmt
, &nitems
, &nbytes
, &prop
);
3236 if (status
!= Success
)
3240 status
= XGetWindowProperty(dpy
, win
, wname
, 0L, nbytes
, False
,
3241 stype
, &rettype
, &retfmt
, &nitems
, &nextra
, &prop
);
3242 if (status
!= Success
) {
3246 if (rettype
!= stype
) {
3255 uniconify(struct swm_region
*r
, union arg
*args
)
3259 unsigned char *name
;
3263 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3265 if (r
&& r
->ws
== NULL
)
3268 /* make sure we have anything to uniconify */
3269 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3270 if (win
->ws
== NULL
)
3271 continue; /* should never happen */
3272 if (win
->iconic
== 0)
3280 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3282 spawn_select(r
, args
, "search", &searchpid
);
3284 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3287 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3288 if (win
->ws
== NULL
)
3289 continue; /* should never happen */
3290 if (win
->iconic
== 0)
3293 name
= get_win_name(display
, win
->id
, a_netwmname
, a_utf8_string
,
3297 fprintf(lfile
, "%s.%lu\n", name
, win
->id
);
3305 name_workspace(struct swm_region
*r
, union arg
*args
)
3309 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3315 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3317 spawn_select(r
, args
, "name_workspace", &searchpid
);
3319 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3322 fprintf(lfile
, "%s", "");
3327 search_workspace(struct swm_region
*r
, union arg
*args
)
3330 struct workspace
*ws
;
3333 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3339 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3341 spawn_select(r
, args
, "search", &searchpid
);
3343 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3346 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
3350 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3351 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3358 search_win_cleanup(void)
3360 struct search_window
*sw
= NULL
;
3362 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3363 XDestroyWindow(display
, sw
->indicator
);
3364 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3370 search_win(struct swm_region
*r
, union arg
*args
)
3372 struct ws_win
*win
= NULL
;
3373 struct search_window
*sw
= NULL
;
3383 DNPRINTF(SWM_D_MISC
, "search_win\n");
3386 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3388 spawn_select(r
, args
, "search", &searchpid
);
3390 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3393 TAILQ_INIT(&search_wl
);
3396 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3397 if (win
->iconic
== 1)
3400 sw
= calloc(1, sizeof(struct search_window
));
3402 fprintf(stderr
, "search_win: calloc: %s", strerror(errno
));
3404 search_win_cleanup();
3410 snprintf(s
, sizeof s
, "%d", i
);
3412 textwidth
= XTextWidth(bar_fs
, s
, len
);
3414 w
= XCreateSimpleWindow(display
,
3415 win
->id
, 0, 0, textwidth
+ 12,
3416 bar_fs
->ascent
+ bar_fs
->descent
+ 4, 1,
3417 r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
,
3418 r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
3421 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3423 gc
= XCreateGC(display
, w
, 0, &gcv
);
3424 XSetFont(display
, gc
, bar_fs
->fid
);
3425 XMapRaised(display
, w
);
3426 XSetForeground(display
, gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
3428 XDrawString(display
, w
, gc
, 6, bar_fs
->ascent
+ 2, s
, len
);
3430 fprintf(lfile
, "%d\n", i
);
3438 search_resp_uniconify(char *resp
, unsigned long len
)
3440 unsigned char *name
;
3444 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp %s\n", resp
);
3446 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3447 if (win
->iconic
== 0)
3449 name
= get_win_name(display
, win
->id
, a_netwmname
, a_utf8_string
, &len
);
3452 if (asprintf(&s
, "%s.%lu", name
, win
->id
) == -1) {
3457 if (strncmp(s
, resp
, len
) == 0) {
3458 /* XXX this should be a callback to generalize */
3459 update_iconic(win
, 0);
3468 search_resp_name_workspace(char *resp
, unsigned long len
)
3470 struct workspace
*ws
;
3472 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp %s\n", resp
);
3474 if (search_r
->ws
== NULL
)
3479 free(search_r
->ws
->name
);
3480 search_r
->ws
->name
= NULL
;
3484 ws
->name
= strdup(resp
);
3485 if (ws
->name
== NULL
) {
3486 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: strdup: %s",
3494 search_resp_search_workspace(char *resp
, unsigned long len
)
3501 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp %s\n", resp
);
3505 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
3512 ws_idx
= strtonum(q
, 1, SWM_WS_MAX
, &errstr
);
3514 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
3521 switchws(search_r
, &a
);
3525 search_resp_search_window(char *resp
, unsigned long len
)
3530 struct search_window
*sw
;
3532 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp %s\n", resp
);
3536 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
3541 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
3543 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
3550 TAILQ_FOREACH(sw
, &search_wl
, entry
)
3551 if (idx
== sw
->idx
) {
3557 #define MAX_RESP_LEN 1024
3560 search_do_resp(void)
3566 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
3571 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
3572 fprintf(stderr
, "search: calloc\n");
3576 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
3578 fprintf(stderr
, "search: read error: %s\n", strerror(errno
));
3581 resp
[rbytes
] = '\0';
3584 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3585 * newline, so work around that by sanitizing the resp now.
3587 resp
[strcspn(resp
, "\n")] = '\0';
3590 switch (search_resp_action
) {
3591 case SWM_SEARCH_UNICONIFY
:
3592 search_resp_uniconify(resp
, len
);
3594 case SWM_SEARCH_NAME_WORKSPACE
:
3595 search_resp_name_workspace(resp
, len
);
3597 case SWM_SEARCH_SEARCH_WORKSPACE
:
3598 search_resp_search_workspace(resp
, len
);
3600 case SWM_SEARCH_SEARCH_WINDOW
:
3601 search_resp_search_window(resp
, len
);
3606 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
3607 search_win_cleanup();
3609 search_resp_action
= SWM_SEARCH_NONE
;
3610 close(select_resp_pipe
[0]);
3615 wkill(struct swm_region
*r
, union arg
*args
)
3617 DNPRINTF(SWM_D_MISC
, "wkill %d\n", args
->id
);
3619 if (r
->ws
->focus
== NULL
)
3622 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
3623 XKillClient(display
, r
->ws
->focus
->id
);
3625 if (r
->ws
->focus
->can_delete
)
3626 client_msg(r
->ws
->focus
, adelete
);
3631 floating_toggle_win(struct ws_win
*win
)
3633 struct swm_region
*r
;
3643 /* reject floating toggles in max stack mode */
3644 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3647 if (win
->floating
) {
3648 if (!win
->floatmaxed
) {
3649 /* retain position for refloat */
3650 store_float_geom(win
, r
);
3654 if (win
->g_floatvalid
) {
3655 /* refloat at last floating relative position */
3656 win
->g
.x
= win
->g_float
.x
- win
->rg_float
.x
+ r
->g
.x
;
3657 win
->g
.y
= win
->g_float
.y
- win
->rg_float
.y
+ r
->g
.y
;
3658 win
->g
.w
= win
->g_float
.w
;
3659 win
->g
.h
= win
->g_float
.h
;
3664 ewmh_update_actions(win
);
3670 floating_toggle(struct swm_region
*r
, union arg
*args
)
3672 struct ws_win
*win
= r
->ws
->focus
;
3678 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
3679 _NET_WM_STATE_TOGGLE
);
3682 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3683 drain_enter_notify();
3685 if (win
== win
->ws
->focus
) {
3686 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3687 focus(win
->ws
->r
, &a
);
3692 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
3694 if (win
->g
.x
+ win
->g
.w
> r
->g
.x
+ r
->g
.w
- border_width
) {
3696 win
->g
.w
= r
->g
.x
+ r
->g
.w
- win
->g
.x
- border_width
;
3698 win
->g
.x
= r
->g
.x
+ r
->g
.w
- win
->g
.w
- border_width
;
3701 if (win
->g
.x
< r
->g
.x
- border_width
) {
3703 win
->g
.w
-= r
->g
.x
- win
->g
.x
- border_width
;
3705 win
->g
.x
= r
->g
.x
- border_width
;
3708 if (win
->g
.y
+ win
->g
.h
> r
->g
.y
+ r
->g
.h
- border_width
) {
3710 win
->g
.h
= r
->g
.y
+ r
->g
.h
- win
->g
.y
- border_width
;
3712 win
->g
.y
= r
->g
.y
+ r
->g
.h
- win
->g
.h
- border_width
;
3715 if (win
->g
.y
< r
->g
.y
- border_width
) {
3717 win
->g
.h
-= r
->g
.y
- win
->g
.y
- border_width
;
3719 win
->g
.y
= r
->g
.y
- border_width
;
3729 update_window(struct ws_win
*win
)
3734 bzero(&wc
, sizeof wc
);
3735 mask
= CWBorderWidth
| CWWidth
| CWHeight
| CWX
| CWY
;
3736 wc
.border_width
= border_width
;
3739 wc
.width
= win
->g
.w
;
3740 wc
.height
= win
->g
.h
;
3742 DNPRINTF(SWM_D_STACK
, "update_window: win %lu x %d y %d w %d h %d\n",
3743 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
3745 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3748 #define SWM_RESIZE_STEPS (50)
3751 resize(struct ws_win
*win
, union arg
*args
)
3755 struct swm_region
*r
= NULL
;
3756 int resize_step
= 0;
3760 struct swm_geometry g
;
3761 int top
= 0, left
= 0;
3764 unsigned int shape
; /* cursor style */
3770 DNPRINTF(SWM_D_MOUSE
, "resize: win %lu floating %d trans %lu\n",
3771 win
->id
, win
->floating
, win
->transient
);
3773 if (!(win
->transient
!= 0 || win
->floating
!= 0))
3776 /* reject resizes in max mode for floaters (transient ok) */
3777 if (win
->floatmaxed
)
3781 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
3787 case SWM_ARG_ID_WIDTHSHRINK
:
3788 win
->g
.w
-= SWM_RESIZE_STEPS
;
3791 case SWM_ARG_ID_WIDTHGROW
:
3792 win
->g
.w
+= SWM_RESIZE_STEPS
;
3795 case SWM_ARG_ID_HEIGHTSHRINK
:
3796 win
->g
.h
-= SWM_RESIZE_STEPS
;
3799 case SWM_ARG_ID_HEIGHTGROW
:
3800 win
->g
.h
+= SWM_RESIZE_STEPS
;
3807 constrain_window(win
, r
, 1);
3809 store_float_geom(win
,r
);
3813 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3814 drain_enter_notify();
3816 /* get cursor offset from window root */
3817 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
3822 if (wx
< win
->g
.w
/ 2)
3825 if (wy
< win
->g
.h
/ 2)
3828 if (args
->id
== SWM_ARG_ID_CENTER
)
3831 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
3833 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
3835 cursor
= XCreateFontCursor(display
, shape
);
3837 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
3838 GrabModeAsync
, None
, cursor
, CurrentTime
) != GrabSuccess
) {
3839 XFreeCursor(display
, cursor
);
3844 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
3845 SubstructureRedirectMask
, &ev
);
3847 case ConfigureRequest
:
3850 handler
[ev
.type
](&ev
);
3853 /* cursor offset/delta from start of the operation */
3854 dx
= ev
.xmotion
.x_root
- x
;
3855 dy
= ev
.xmotion
.y_root
- y
;
3861 if (args
->id
== SWM_ARG_ID_CENTER
) {
3862 if (g
.h
/ 2 + dy
< 1)
3865 win
->g
.y
= g
.y
- dy
;
3866 win
->g
.h
= g
.h
+ 2 * dy
;
3872 win
->g
.y
= g
.y
- dy
;
3874 win
->g
.h
= g
.h
+ dy
;
3881 if (args
->id
== SWM_ARG_ID_CENTER
) {
3882 if (g
.w
/ 2 + dx
< 1)
3885 win
->g
.x
= g
.x
- dx
;
3886 win
->g
.w
= g
.w
+ 2 * dx
;
3892 win
->g
.x
= g
.x
- dx
;
3894 win
->g
.w
= g
.w
+ dx
;
3897 constrain_window(win
, r
, 1);
3899 /* not free, don't sync more than 120 times / second */
3900 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
3901 time
= ev
.xmotion
.time
;
3902 XSync(display
, False
);
3907 } while (ev
.type
!= ButtonRelease
);
3909 XSync(display
, False
);
3912 store_float_geom(win
,r
);
3914 XUngrabPointer(display
, CurrentTime
);
3915 XFreeCursor(display
, cursor
);
3918 drain_enter_notify();
3922 resize_step(struct swm_region
*r
, union arg
*args
)
3924 struct ws_win
*win
= NULL
;
3926 if (r
&& r
->ws
&& r
->ws
->focus
)
3934 #define SWM_MOVE_STEPS (50)
3937 move(struct ws_win
*win
, union arg
*args
)
3942 struct swm_region
*r
= NULL
;
3952 DNPRINTF(SWM_D_MOUSE
, "move: win %lu floating %d trans %lu\n",
3953 win
->id
, win
->floating
, win
->transient
);
3955 /* in max_stack mode should only move transients */
3956 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
3960 if (win
->floating
== 0 && !win
->transient
) {
3961 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
3964 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
3971 case SWM_ARG_ID_MOVELEFT
:
3972 win
->g
.x
-= (SWM_MOVE_STEPS
- border_width
);
3975 case SWM_ARG_ID_MOVERIGHT
:
3976 win
->g
.x
+= (SWM_MOVE_STEPS
- border_width
);
3979 case SWM_ARG_ID_MOVEUP
:
3980 win
->g
.y
-= (SWM_MOVE_STEPS
- border_width
);
3983 case SWM_ARG_ID_MOVEDOWN
:
3984 win
->g
.y
+= (SWM_MOVE_STEPS
- border_width
);
3991 constrain_window(win
, r
, 0);
3993 store_float_geom(win
,r
);
3997 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
3998 GrabModeAsync
, None
, XCreateFontCursor(display
, XC_fleur
),
3999 CurrentTime
) != GrabSuccess
)
4002 /* get cursor offset from window root */
4003 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4007 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4008 SubstructureRedirectMask
, &ev
);
4010 case ConfigureRequest
:
4013 handler
[ev
.type
](&ev
);
4016 win
->g
.x
= ev
.xmotion
.x_root
- wx
- border_width
;
4017 win
->g
.y
= ev
.xmotion
.y_root
- wy
- border_width
;
4019 constrain_window(win
, r
, 0);
4021 /* not free, don't sync more than 120 times / second */
4022 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4023 time
= ev
.xmotion
.time
;
4024 XSync(display
, False
);
4029 } while (ev
.type
!= ButtonRelease
);
4031 XSync(display
, False
);
4034 store_float_geom(win
,r
);
4035 XUngrabPointer(display
, CurrentTime
);
4038 drain_enter_notify();
4042 move_step(struct swm_region
*r
, union arg
*args
)
4044 struct ws_win
*win
= NULL
;
4046 if (r
&& r
->ws
&& r
->ws
->focus
)
4051 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4058 /* user/key callable function IDs */
4128 kf_search_workspace
,
4130 kf_dumpwins
, /* MUST BE LAST */
4134 /* key definitions */
4136 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4141 legacyfunc(struct swm_region
*r
, union arg
*args
)
4146 char name
[SWM_FUNCNAME_LEN
];
4147 void (*func
)(struct swm_region
*r
, union arg
*);
4149 } keyfuncs
[kf_invalid
+ 1] = {
4150 /* name function argument */
4151 { "cycle_layout", cycle_layout
, {0} },
4152 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4153 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4154 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4155 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4156 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4157 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4158 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4159 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4160 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4161 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4162 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4163 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4164 { "spawn_term", spawnterm
, {.argv
= spawn_term
} },
4165 { "spawn_menu", legacyfunc
, {0} },
4166 { "quit", quit
, {0} },
4167 { "restart", restart
, {0} },
4168 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4169 { "ws_1", switchws
, {.id
= 0} },
4170 { "ws_2", switchws
, {.id
= 1} },
4171 { "ws_3", switchws
, {.id
= 2} },
4172 { "ws_4", switchws
, {.id
= 3} },
4173 { "ws_5", switchws
, {.id
= 4} },
4174 { "ws_6", switchws
, {.id
= 5} },
4175 { "ws_7", switchws
, {.id
= 6} },
4176 { "ws_8", switchws
, {.id
= 7} },
4177 { "ws_9", switchws
, {.id
= 8} },
4178 { "ws_10", switchws
, {.id
= 9} },
4179 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4180 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4181 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4182 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4183 { "ws_prior", priorws
, {0} },
4184 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4185 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4186 { "mvws_1", send_to_ws
, {.id
= 0} },
4187 { "mvws_2", send_to_ws
, {.id
= 1} },
4188 { "mvws_3", send_to_ws
, {.id
= 2} },
4189 { "mvws_4", send_to_ws
, {.id
= 3} },
4190 { "mvws_5", send_to_ws
, {.id
= 4} },
4191 { "mvws_6", send_to_ws
, {.id
= 5} },
4192 { "mvws_7", send_to_ws
, {.id
= 6} },
4193 { "mvws_8", send_to_ws
, {.id
= 7} },
4194 { "mvws_9", send_to_ws
, {.id
= 8} },
4195 { "mvws_10", send_to_ws
, {.id
= 9} },
4196 { "bar_toggle", bar_toggle
, {0} },
4197 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4198 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4199 { "screenshot_all", legacyfunc
, {0} },
4200 { "screenshot_wind", legacyfunc
, {0} },
4201 { "float_toggle", floating_toggle
,{0} },
4202 { "version", version
, {0} },
4203 { "spawn_lock", legacyfunc
, {0} },
4204 { "spawn_initscr", legacyfunc
, {0} },
4205 { "spawn_custom", dummykeyfunc
, {0} },
4206 { "iconify", iconify
, {0} },
4207 { "uniconify", uniconify
, {0} },
4208 { "raise_toggle", raise_toggle
, {0} },
4209 { "button2", pressbutton
, {2} },
4210 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4211 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4212 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4213 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4214 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4215 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4216 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4217 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4218 { "name_workspace", name_workspace
, {0} },
4219 { "search_workspace", search_workspace
, {0} },
4220 { "search_win", search_win
, {0} },
4221 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4222 { "invalid key func", NULL
, {0} },
4227 enum keyfuncid funcid
;
4230 int keys_size
= 0, keys_length
= 0;
4231 struct key
*keys
= NULL
;
4234 enum { client_click
, root_click
};
4236 unsigned int action
;
4238 unsigned int button
;
4239 void (*func
)(struct ws_win
*, union arg
*);
4242 /* action key mouse button func args */
4243 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4244 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4245 { client_click
, MODKEY
, Button1
, move
, {0} },
4249 update_modkey(unsigned int mod
)
4254 for (i
= 0; i
< keys_length
; i
++)
4255 if (keys
[i
].mod
& ShiftMask
)
4256 keys
[i
].mod
= mod
| ShiftMask
;
4260 for (i
= 0; i
< LENGTH(buttons
); i
++)
4261 if (buttons
[i
].mask
& ShiftMask
)
4262 buttons
[i
].mask
= mod
| ShiftMask
;
4264 buttons
[i
].mask
= mod
;
4274 int spawns_size
= 0, spawns_length
= 0;
4275 struct spawn_prog
*spawns
= NULL
;
4278 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4281 struct spawn_prog
*prog
= NULL
;
4283 char *ap
, **real_args
;
4285 DNPRINTF(SWM_D_SPAWN
, "spawn_expand %s\n", spawn_name
);
4288 for (i
= 0; i
< spawns_length
; i
++) {
4289 if (!strcasecmp(spawn_name
, spawns
[i
].name
))
4293 fprintf(stderr
, "spawn_custom: program %s not found\n",
4298 /* make room for expanded args */
4299 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4300 err(1, "spawn_custom: calloc real_args");
4302 /* expand spawn_args into real_args */
4303 for (i
= 0; i
< prog
->argc
; i
++) {
4305 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg = %s\n", ap
);
4306 if (!strcasecmp(ap
, "$bar_border")) {
4308 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4310 err(1, "spawn_custom border color");
4311 } else if (!strcasecmp(ap
, "$bar_color")) {
4313 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4315 err(1, "spawn_custom bar color");
4316 } else if (!strcasecmp(ap
, "$bar_font")) {
4317 if ((real_args
[i
] = strdup(bar_fonts
[bar_fidx
]))
4319 err(1, "spawn_custom bar fonts");
4320 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4322 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4324 err(1, "spawn_custom color font");
4325 } else if (!strcasecmp(ap
, "$color_focus")) {
4327 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4329 err(1, "spawn_custom color focus");
4330 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4332 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4334 err(1, "spawn_custom color unfocus");
4336 /* no match --> copy as is */
4337 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4338 err(1, "spawn_custom strdup(ap)");
4340 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg = %s\n",
4345 if ((swm_debug
& SWM_D_SPAWN
) != 0) {
4346 fprintf(stderr
, "spawn_custom: result = ");
4347 for (i
= 0; i
< prog
->argc
; i
++)
4348 fprintf(stderr
, "\"%s\" ", real_args
[i
]);
4349 fprintf(stderr
, "\n");
4352 *ret_args
= real_args
;
4353 return (prog
->argc
);
4357 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4363 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4367 spawn(r
->ws
->idx
, &a
, 1);
4369 for (i
= 0; i
< spawn_argc
; i
++)
4375 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4381 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4385 if (pipe(select_list_pipe
) == -1)
4386 err(1, "pipe error");
4387 if (pipe(select_resp_pipe
) == -1)
4388 err(1, "pipe error");
4390 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4391 err(1, "could not disable SIGPIPE");
4392 switch (*pid
= fork()) {
4394 err(1, "cannot fork");
4397 if (dup2(select_list_pipe
[0], 0) == -1)
4399 if (dup2(select_resp_pipe
[1], 1) == -1)
4401 close(select_list_pipe
[1]);
4402 close(select_resp_pipe
[0]);
4403 spawn(r
->ws
->idx
, &a
, 0);
4405 default: /* parent */
4406 close(select_list_pipe
[0]);
4407 close(select_resp_pipe
[1]);
4411 for (i
= 0; i
< spawn_argc
; i
++)
4417 setspawn(struct spawn_prog
*prog
)
4421 if (prog
== NULL
|| prog
->name
== NULL
)
4425 for (i
= 0; i
< spawns_length
; i
++) {
4426 if (!strcmp(spawns
[i
].name
, prog
->name
)) {
4428 if (prog
->argv
== NULL
) {
4430 DNPRINTF(SWM_D_SPAWN
,
4431 "setspawn: delete #%d %s\n",
4433 free(spawns
[i
].name
);
4434 for (j
= 0; j
< spawns
[i
].argc
; j
++)
4435 free(spawns
[i
].argv
[j
]);
4436 free(spawns
[i
].argv
);
4437 j
= spawns_length
- 1;
4439 spawns
[i
] = spawns
[j
];
4444 DNPRINTF(SWM_D_SPAWN
,
4445 "setspawn: replace #%d %s\n",
4447 free(spawns
[i
].name
);
4448 for (j
= 0; j
< spawns
[i
].argc
; j
++)
4449 free(spawns
[i
].argv
[j
]);
4450 free(spawns
[i
].argv
);
4453 /* found case handled */
4459 if (prog
->argv
== NULL
) {
4461 "error: setspawn: cannot find program %s", prog
->name
);
4466 /* not found: add */
4467 if (spawns_size
== 0 || spawns
== NULL
) {
4469 DNPRINTF(SWM_D_SPAWN
, "setspawn: init list %d\n", spawns_size
);
4470 spawns
= malloc((size_t)spawns_size
*
4471 sizeof(struct spawn_prog
));
4472 if (spawns
== NULL
) {
4473 fprintf(stderr
, "setspawn: malloc failed\n");
4477 } else if (spawns_length
== spawns_size
) {
4479 DNPRINTF(SWM_D_SPAWN
, "setspawn: grow list %d\n", spawns_size
);
4480 spawns
= realloc(spawns
, (size_t)spawns_size
*
4481 sizeof(struct spawn_prog
));
4482 if (spawns
== NULL
) {
4483 fprintf(stderr
, "setspawn: realloc failed\n");
4489 if (spawns_length
< spawns_size
) {
4490 DNPRINTF(SWM_D_SPAWN
, "setspawn: add #%d %s\n",
4491 spawns_length
, prog
->name
);
4492 i
= spawns_length
++;
4495 fprintf(stderr
, "spawns array problem?\n");
4496 if (spawns
== NULL
) {
4497 fprintf(stderr
, "spawns array is NULL!\n");
4505 setconfspawn(char *selector
, char *value
, int flags
)
4507 struct spawn_prog
*prog
;
4508 char *vp
, *cp
, *word
;
4510 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
4511 if ((prog
= calloc(1, sizeof *prog
)) == NULL
)
4512 err(1, "setconfspawn: calloc prog");
4513 prog
->name
= strdup(selector
);
4514 if (prog
->name
== NULL
)
4515 err(1, "setconfspawn prog->name");
4516 if ((cp
= vp
= strdup(value
)) == NULL
)
4517 err(1, "setconfspawn: strdup(value) ");
4518 while ((word
= strsep(&cp
, " \t")) != NULL
) {
4519 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: arg [%s]\n", word
);
4521 cp
+= (long)strspn(cp
, " \t");
4522 if (strlen(word
) > 0) {
4524 if ((prog
->argv
= realloc(prog
->argv
,
4525 prog
->argc
* sizeof(char *))) == NULL
)
4526 err(1, "setconfspawn: realloc");
4527 if ((prog
->argv
[prog
->argc
- 1] = strdup(word
)) == NULL
)
4528 err(1, "setconfspawn: strdup");
4535 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
4542 setconfspawn("term", "xterm", 0);
4543 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4544 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4545 setconfspawn("lock", "xlock", 0);
4546 setconfspawn("initscr", "initscreen.sh", 0);
4547 setconfspawn("menu", "dmenu_run"
4550 " -nf $bar_font_color"
4552 " -sf $bar_color", 0);
4553 setconfspawn("search", "dmenu"
4557 " -nf $bar_font_color"
4559 " -sf $bar_color", 0);
4560 setconfspawn("name_workspace", "dmenu"
4564 " -nf $bar_font_color"
4566 " -sf $bar_color", 0);
4570 #define SWM_MODNAME_SIZE 32
4571 #define SWM_KEY_WS "\n+ \t"
4573 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
4577 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
4578 if (mod
== NULL
|| ks
== NULL
) {
4579 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
4582 if (keystr
== NULL
|| strlen(keystr
) == 0) {
4583 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
4589 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
4590 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
4592 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
4593 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
4595 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
4597 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
4599 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
4601 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
4603 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
4605 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
4606 *mod
|= ControlMask
;
4608 *ks
= XStringToKeysym(name
);
4609 XConvertCase(*ks
, ks
, &uks
);
4610 if (ks
== NoSymbol
) {
4612 "parsekeys: invalid key %s\n",
4618 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
4623 strdupsafe(char *str
)
4628 return (strdup(str
));
4632 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
4636 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
4637 keyfuncs
[kfid
].name
, spawn_name
);
4639 for (i
= 0; i
< keys_length
; i
++) {
4640 if (keys
[i
].mod
== mod
&& keys
[i
].keysym
== ks
) {
4641 if (kfid
== kf_invalid
) {
4644 "setkeybinding: delete #%d %s\n",
4645 i
, keyfuncs
[keys
[i
].funcid
].name
);
4646 free(keys
[i
].spawn_name
);
4647 j
= keys_length
- 1;
4651 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4654 /* found: replace */
4656 "setkeybinding: replace #%d %s %s\n",
4657 i
, keyfuncs
[keys
[i
].funcid
].name
,
4659 free(keys
[i
].spawn_name
);
4661 keys
[i
].keysym
= ks
;
4662 keys
[i
].funcid
= kfid
;
4663 keys
[i
].spawn_name
= strdupsafe(spawn_name
);
4664 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4669 if (kfid
== kf_invalid
) {
4671 "error: setkeybinding: cannot find mod/key combination");
4672 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4675 /* not found: add */
4676 if (keys_size
== 0 || keys
== NULL
) {
4678 DNPRINTF(SWM_D_KEY
, "setkeybinding: init list %d\n", keys_size
);
4679 keys
= malloc((size_t)keys_size
* sizeof(struct key
));
4681 fprintf(stderr
, "malloc failed\n");
4685 } else if (keys_length
== keys_size
) {
4687 DNPRINTF(SWM_D_KEY
, "setkeybinding: grow list %d\n", keys_size
);
4688 keys
= realloc(keys
, (size_t)keys_size
* sizeof(struct key
));
4690 fprintf(stderr
, "realloc failed\n");
4695 if (keys_length
< keys_size
) {
4697 DNPRINTF(SWM_D_KEY
, "setkeybinding: add #%d %s %s\n",
4698 j
, keyfuncs
[kfid
].name
, spawn_name
);
4700 keys
[j
].keysym
= ks
;
4701 keys
[j
].funcid
= kfid
;
4702 keys
[j
].spawn_name
= strdupsafe(spawn_name
);
4704 fprintf(stderr
, "keys array problem?\n");
4706 fprintf(stderr
, "keys array problem\n");
4710 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4714 setconfbinding(char *selector
, char *value
, int flags
)
4716 enum keyfuncid kfid
;
4720 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
4721 if (selector
== NULL
) {
4722 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
4723 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4725 setkeybinding(mod
, ks
, kfid
, NULL
);
4730 /* search by key function name */
4731 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
4732 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
4733 SWM_FUNCNAME_LEN
) == 0) {
4734 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4736 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4737 setkeybinding(mod
, ks
, kfid
, NULL
);
4743 /* search by custom spawn name */
4744 for (i
= 0; i
< spawns_length
; i
++) {
4745 if (strcasecmp(selector
, spawns
[i
].name
) == 0) {
4746 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4748 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4749 setkeybinding(mod
, ks
, kf_spawn_custom
,
4756 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
4763 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
4764 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
4765 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
4766 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
4767 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
4768 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
4769 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
4770 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
4771 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
4772 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
4773 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
4774 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
4775 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
4776 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_term
, NULL
);
4777 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
4778 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
4779 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
4780 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
4781 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
4782 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
4783 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
4784 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
4785 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
4786 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
4787 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
4788 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
4789 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
4790 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
4791 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
4792 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
4793 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
4794 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
4795 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
4796 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
4797 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
4798 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
4799 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
4800 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
4801 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
4802 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
4803 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
4804 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
4805 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
4806 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
4807 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
4808 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
4809 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
4810 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
4811 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
4812 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
4813 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
4814 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
4815 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
4816 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
4817 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
4818 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
4819 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
4820 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
4821 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
4822 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
4823 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
4824 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
4825 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
4826 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
4827 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
4828 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
4829 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
4830 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
4831 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
4832 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
4833 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
4835 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
4844 /* clear all key bindings, if any */
4845 for (i
= 0; i
< keys_length
; i
++)
4846 free(keys
[i
].spawn_name
);
4851 setkeymapping(char *selector
, char *value
, int flags
)
4853 char keymapping_file
[PATH_MAX
];
4854 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
4855 if (value
[0] == '~')
4856 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
4857 pwd
->pw_dir
, &value
[1]);
4859 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
4861 /* load new key bindings; if it fails, revert to default bindings */
4862 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
4866 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
4871 updatenumlockmask(void)
4874 XModifierKeymap
*modmap
;
4876 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
4878 modmap
= XGetModifierMapping(display
);
4879 for (i
= 0; i
< 8; i
++)
4880 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
4881 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
4882 == XKeysymToKeycode(display
, XK_Num_Lock
))
4883 numlockmask
= (1 << i
);
4885 XFreeModifiermap(modmap
);
4891 unsigned int i
, j
, k
;
4893 unsigned int modifiers
[] =
4894 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
4896 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
4897 updatenumlockmask();
4899 for (k
= 0; k
< ScreenCount(display
); k
++) {
4900 if (TAILQ_EMPTY(&screens
[k
].rl
))
4902 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
4903 for (i
= 0; i
< keys_length
; i
++) {
4904 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
4905 for (j
= 0; j
< LENGTH(modifiers
); j
++)
4906 XGrabKey(display
, code
,
4907 keys
[i
].mod
| modifiers
[j
],
4908 screens
[k
].root
, True
,
4909 GrabModeAsync
, GrabModeAsync
);
4915 grabbuttons(struct ws_win
*win
, int focused
)
4918 unsigned int modifiers
[] =
4919 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
4921 updatenumlockmask();
4922 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
4924 for (i
= 0; i
< LENGTH(buttons
); i
++)
4925 if (buttons
[i
].action
== client_click
)
4926 for (j
= 0; j
< LENGTH(modifiers
); j
++)
4927 XGrabButton(display
, buttons
[i
].button
,
4928 buttons
[i
].mask
| modifiers
[j
],
4929 win
->id
, False
, BUTTONMASK
,
4930 GrabModeAsync
, GrabModeSync
, None
,
4933 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
4934 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
4937 const char *quirkname
[] = {
4938 "NONE", /* config string for "no value" */
4947 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4948 #define SWM_Q_WS "\n|+ \t"
4950 parsequirks(char *qstr
, unsigned long *quirk
)
4960 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
4962 cp
+= (long)strspn(cp
, SWM_Q_WS
);
4963 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
4964 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
4965 DNPRINTF(SWM_D_QUIRK
,
4966 "parsequirks: %s\n", name
);
4971 *quirk
|= 1 << (i
-1);
4975 if (i
>= LENGTH(quirkname
)) {
4976 DNPRINTF(SWM_D_QUIRK
,
4977 "parsequirks: invalid quirk [%s]\n", name
);
4985 setquirk(const char *class, const char *name
, const int quirk
)
4990 for (i
= 0; i
< quirks_length
; i
++) {
4991 if (!strcmp(quirks
[i
].class, class) &&
4992 !strcmp(quirks
[i
].name
, name
)) {
4995 DNPRINTF(SWM_D_QUIRK
,
4996 "setquirk: delete #%d %s:%s\n",
4997 i
, quirks
[i
].class, quirks
[i
].name
);
4998 free(quirks
[i
].class);
4999 free(quirks
[i
].name
);
5000 j
= quirks_length
- 1;
5002 quirks
[i
] = quirks
[j
];
5006 /* found: replace */
5007 DNPRINTF(SWM_D_QUIRK
,
5008 "setquirk: replace #%d %s:%s\n",
5009 i
, quirks
[i
].class, quirks
[i
].name
);
5010 free(quirks
[i
].class);
5011 free(quirks
[i
].name
);
5012 quirks
[i
].class = strdup(class);
5013 quirks
[i
].name
= strdup(name
);
5014 quirks
[i
].quirk
= quirk
;
5021 "error: setquirk: cannot find class/name combination");
5024 /* not found: add */
5025 if (quirks_size
== 0 || quirks
== NULL
) {
5027 DNPRINTF(SWM_D_QUIRK
, "setquirk: init list %d\n", quirks_size
);
5028 quirks
= malloc((size_t)quirks_size
* sizeof(struct quirk
));
5029 if (quirks
== NULL
) {
5030 fprintf(stderr
, "setquirk: malloc failed\n");
5034 } else if (quirks_length
== quirks_size
) {
5036 DNPRINTF(SWM_D_QUIRK
, "setquirk: grow list %d\n", quirks_size
);
5037 quirks
= realloc(quirks
,
5038 (size_t)quirks_size
* sizeof(struct quirk
));
5039 if (quirks
== NULL
) {
5040 fprintf(stderr
, "setquirk: realloc failed\n");
5045 if (quirks_length
< quirks_size
) {
5046 DNPRINTF(SWM_D_QUIRK
, "setquirk: add %d\n", quirks_length
);
5047 j
= quirks_length
++;
5048 quirks
[j
].class = strdup(class);
5049 quirks
[j
].name
= strdup(name
);
5050 quirks
[j
].quirk
= quirk
;
5052 fprintf(stderr
, "quirks array problem?\n");
5053 if (quirks
== NULL
) {
5054 fprintf(stderr
, "quirks array problem!\n");
5061 setconfquirk(char *selector
, char *value
, int flags
)
5063 char *cp
, *class, *name
;
5065 unsigned long quirks
;
5066 if (selector
== NULL
)
5068 if ((cp
= strchr(selector
, ':')) == NULL
)
5073 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5074 setquirk(class, name
, quirks
);
5081 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5082 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5083 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5084 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5085 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5086 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5087 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5088 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5089 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5090 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5091 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5092 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5093 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5096 /* conf file stuff */
5097 #define SWM_CONF_FILE "scrotwm.conf"
5099 enum { SWM_S_BAR_DELAY
, SWM_S_BAR_ENABLED
, SWM_S_BAR_BORDER_WIDTH
,
5100 SWM_S_STACK_ENABLED
, SWM_S_CLOCK_ENABLED
, SWM_S_CLOCK_FORMAT
,
5101 SWM_S_CYCLE_EMPTY
, SWM_S_CYCLE_VISIBLE
, SWM_S_SS_ENABLED
,
5102 SWM_S_TERM_WIDTH
, SWM_S_TITLE_CLASS_ENABLED
,
5103 SWM_S_TITLE_NAME_ENABLED
, SWM_S_WINDOW_NAME_ENABLED
, SWM_S_URGENT_ENABLED
,
5104 SWM_S_FOCUS_MODE
, SWM_S_DISABLE_BORDER
, SWM_S_BORDER_WIDTH
,
5105 SWM_S_BAR_FONT
, SWM_S_BAR_ACTION
, SWM_S_SPAWN_TERM
,
5106 SWM_S_SS_APP
, SWM_S_DIALOG_RATIO
, SWM_S_BAR_AT_BOTTOM
,
5107 SWM_S_VERBOSE_LAYOUT
, SWM_S_BAR_JUSTIFY
5111 setconfvalue(char *selector
, char *value
, int flags
)
5116 case SWM_S_BAR_DELAY
:
5117 bar_delay
= atoi(value
);
5119 case SWM_S_BAR_ENABLED
:
5120 bar_enabled
= atoi(value
);
5122 case SWM_S_BAR_BORDER_WIDTH
:
5123 bar_border_width
= atoi(value
);
5125 case SWM_S_BAR_AT_BOTTOM
:
5126 bar_at_bottom
= atoi(value
);
5128 case SWM_S_BAR_JUSTIFY
:
5129 if (!strcmp(value
, "left"))
5130 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5131 else if (!strcmp(value
, "center"))
5132 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5133 else if (!strcmp(value
, "right"))
5134 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5136 errx(1, "invalid bar_justify");
5138 case SWM_S_STACK_ENABLED
:
5139 stack_enabled
= atoi(value
);
5141 case SWM_S_CLOCK_ENABLED
:
5142 clock_enabled
= atoi(value
);
5144 case SWM_S_CLOCK_FORMAT
:
5145 #ifndef SWM_DENY_CLOCK_FORMAT
5147 if ((clock_format
= strdup(value
)) == NULL
)
5148 err(1, "setconfvalue: clock_format");
5151 case SWM_S_CYCLE_EMPTY
:
5152 cycle_empty
= atoi(value
);
5154 case SWM_S_CYCLE_VISIBLE
:
5155 cycle_visible
= atoi(value
);
5157 case SWM_S_SS_ENABLED
:
5158 ss_enabled
= atoi(value
);
5160 case SWM_S_TERM_WIDTH
:
5161 term_width
= atoi(value
);
5163 case SWM_S_TITLE_CLASS_ENABLED
:
5164 title_class_enabled
= atoi(value
);
5166 case SWM_S_WINDOW_NAME_ENABLED
:
5167 window_name_enabled
= atoi(value
);
5169 case SWM_S_TITLE_NAME_ENABLED
:
5170 title_name_enabled
= atoi(value
);
5172 case SWM_S_URGENT_ENABLED
:
5173 urgent_enabled
= atoi(value
);
5175 case SWM_S_FOCUS_MODE
:
5176 if (!strcmp(value
, "default"))
5177 focus_mode
= SWM_FOCUS_DEFAULT
;
5178 else if (!strcmp(value
, "follow_cursor"))
5179 focus_mode
= SWM_FOCUS_FOLLOW
;
5180 else if (!strcmp(value
, "synergy"))
5181 focus_mode
= SWM_FOCUS_SYNERGY
;
5183 errx(1, "focus_mode");
5185 case SWM_S_DISABLE_BORDER
:
5186 disable_border
= atoi(value
);
5188 case SWM_S_BORDER_WIDTH
:
5189 border_width
= atoi(value
);
5191 case SWM_S_BAR_FONT
:
5193 if ((bar_fonts
[0] = strdup(value
)) == NULL
)
5194 err(1, "setconfvalue: bar_font");
5196 case SWM_S_BAR_ACTION
:
5198 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5199 err(1, "setconfvalue: bar_action");
5201 case SWM_S_SPAWN_TERM
:
5202 free(spawn_term
[0]);
5203 if ((spawn_term
[0] = strdup(value
)) == NULL
)
5204 err(1, "setconfvalue: spawn_term");
5208 case SWM_S_DIALOG_RATIO
:
5209 dialog_ratio
= atof(value
);
5210 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5213 case SWM_S_VERBOSE_LAYOUT
:
5214 verbose_layout
= atoi(value
);
5215 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5217 layouts
[i
].l_string
= fancy_stacker
;
5219 layouts
[i
].l_string
= plain_stacker
;
5229 setconfmodkey(char *selector
, char *value
, int flags
)
5231 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5232 update_modkey(Mod1Mask
);
5233 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5234 update_modkey(Mod2Mask
);
5235 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5236 update_modkey(Mod3Mask
);
5237 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5238 update_modkey(Mod4Mask
);
5245 setconfcolor(char *selector
, char *value
, int flags
)
5247 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5252 setconfregion(char *selector
, char *value
, int flags
)
5254 custom_region(value
);
5259 setautorun(char *selector
, char *value
, int flags
)
5269 if (getenv("SWM_STARTED"))
5273 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5274 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5276 if (ws_id
< 0 || ws_id
>= SWM_WS_MAX
)
5277 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5280 * This is a little intricate
5282 * If the pid already exists we simply reuse it because it means it was
5283 * used before AND not claimed by manage_window. We get away with
5284 * altering it in the parent after INSERT because this can not be a race
5287 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5290 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5292 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5293 err(1, "setautorun: realloc");
5294 a
.argv
[argc
- 1] = ap
;
5297 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5298 err(1, "setautorun: realloc");
5299 a
.argv
[argc
] = NULL
;
5301 if ((pid
= fork()) == 0) {
5302 spawn(ws_id
, &a
, 1);
5311 p
= calloc(1, sizeof *p
);
5314 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
5324 setlayout(char *selector
, char *value
, int flags
)
5326 int ws_id
, i
, x
, mg
, ma
, si
, raise
;
5327 int st
= SWM_V_STACK
;
5329 struct workspace
*ws
;
5331 if (getenv("SWM_STARTED"))
5335 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
5336 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
5337 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5338 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5341 if (ws_id
< 0 || ws_id
>= SWM_WS_MAX
)
5342 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
5344 if (!strcasecmp(s
, "vertical"))
5346 else if (!strcasecmp(s
, "horizontal"))
5348 else if (!strcasecmp(s
, "fullscreen"))
5351 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5352 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5355 for (i
= 0; i
< ScreenCount(display
); i
++) {
5356 ws
= (struct workspace
*)&screens
[i
].ws
;
5357 ws
[ws_id
].cur_layout
= &layouts
[st
];
5359 ws
[ws_id
].always_raise
= raise
;
5360 if (st
== SWM_MAX_STACK
)
5364 for (x
= 0; x
< abs(mg
); x
++) {
5365 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5366 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
5367 SWM_ARG_ID_MASTERSHRINK
);
5371 for (x
= 0; x
< abs(ma
); x
++) {
5372 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5373 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
5374 SWM_ARG_ID_MASTERDEL
);
5378 for (x
= 0; x
< abs(si
); x
++) {
5379 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5380 si
>= 0 ? SWM_ARG_ID_STACKINC
:
5381 SWM_ARG_ID_STACKDEC
);
5389 /* config options */
5390 struct config_option
{
5392 int (*func
)(char*, char*, int);
5395 struct config_option configopt
[] = {
5396 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
5397 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
5398 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
5399 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
5400 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
5401 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
5402 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
5403 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
5404 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
5405 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
5406 { "keyboard_mapping", setkeymapping
, 0 },
5407 { "bind", setconfbinding
, 0 },
5408 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
5409 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
5410 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
5411 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
5412 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
5413 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
5414 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
5415 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
5416 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
5417 { "modkey", setconfmodkey
, 0 },
5418 { "program", setconfspawn
, 0 },
5419 { "quirk", setconfquirk
, 0 },
5420 { "region", setconfregion
, 0 },
5421 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
5422 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
5423 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
5424 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
5425 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
5426 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
5427 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
5428 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
5429 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
5430 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
5431 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
5432 { "autorun", setautorun
, 0 },
5433 { "layout", setlayout
, 0 },
5438 conf_load(char *filename
, int keymapping
)
5441 char *line
, *cp
, *optsub
, *optval
;
5442 size_t linelen
, lineno
= 0;
5443 int wordlen
, i
, optind
;
5444 struct config_option
*opt
;
5446 DNPRINTF(SWM_D_CONF
, "conf_load begin\n");
5448 if (filename
== NULL
) {
5449 fprintf(stderr
, "conf_load: no filename\n");
5452 if ((config
= fopen(filename
, "r")) == NULL
) {
5453 warn("conf_load: fopen: %s", filename
);
5457 while (!feof(config
)) {
5458 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
5461 err(1, "%s", filename
);
5466 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
5467 if (cp
[0] == '\0') {
5472 /* get config option */
5473 wordlen
= strcspn(cp
, "=[ \t\n");
5475 warnx("%s: line %zd: no option found",
5480 for (i
= 0; i
< LENGTH(configopt
); i
++) {
5481 opt
= &configopt
[i
];
5482 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
5483 strlen(opt
->optname
) == wordlen
) {
5489 warnx("%s: line %zd: unknown option %.*s",
5490 filename
, lineno
, wordlen
, cp
);
5493 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
5494 warnx("%s: line %zd: invalid option %.*s",
5495 filename
, lineno
, wordlen
, cp
);
5499 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
5500 /* get [selector] if any */
5504 wordlen
= strcspn(cp
, "]");
5507 warnx("%s: line %zd: syntax error",
5512 if(asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
5514 warnx("%s: line %zd: unable to allocate"
5515 "memory for selector", filename
,
5521 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
5523 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
5525 optval
= strdup(cp
);
5526 /* call function to deal with it all */
5527 if (configopt
[optind
].func(optsub
, optval
,
5528 configopt
[optind
].funcflags
) != 0) {
5529 fprintf(stderr
, "%s line %zd: %s\n",
5530 filename
, lineno
, line
);
5531 errx(1, "%s: line %zd: invalid data for %s",
5532 filename
, lineno
, configopt
[optind
].optname
);
5540 DNPRINTF(SWM_D_CONF
, "conf_load end\n");
5552 set_child_transient(struct ws_win
*win
, Window
*trans
)
5554 struct ws_win
*parent
, *w
;
5555 XWMHints
*wmh
= NULL
;
5556 struct swm_region
*r
;
5557 struct workspace
*ws
;
5559 parent
= find_window(win
->transient
);
5561 parent
->child_trans
= win
;
5563 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
5564 " for %lu trans %lu\n", win
->id
, win
->transient
);
5566 if (win
->hints
== NULL
) {
5567 fprintf(stderr
, "no hints for %lu\n", win
->id
);
5571 r
= root_to_region(win
->wa
.root
);
5573 /* parent doen't exist in our window list */
5574 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5578 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
5579 fprintf(stderr
, "can't get hints for %lu\n",
5584 if (win
->hints
->window_group
!= wmh
->window_group
)
5587 w
->child_trans
= win
;
5588 win
->transient
= w
->id
;
5590 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
5591 "transient to %lu\n", win
->transient
);
5601 window_get_pid(Window win
)
5603 Atom actual_type_return
;
5604 int actual_format_return
= 0;
5605 unsigned long nitems_return
= 0;
5606 unsigned long bytes_after_return
= 0;
5610 unsigned char *prop
= NULL
;
5612 if (XGetWindowProperty(display
, win
,
5613 XInternAtom(display
, "_NET_WM_PID", False
), 0, 1, False
,
5614 XA_CARDINAL
, &actual_type_return
, &actual_format_return
,
5615 &nitems_return
, &bytes_after_return
,
5616 (unsigned char**)(void*)&pid
) != Success
)
5618 if (actual_type_return
!= XA_CARDINAL
)
5629 if (XGetWindowProperty(display
, win
,
5630 XInternAtom(display
, "_SWM_PID", False
), 0, SWM_PROPLEN
, False
,
5631 XA_STRING
, &actual_type_return
, &actual_format_return
,
5632 &nitems_return
, &bytes_after_return
, &prop
) != Success
)
5634 if (actual_type_return
!= XA_STRING
)
5639 ret
= strtonum((const char *)prop
, 0, UINT_MAX
, &errstr
);
5640 /* ignore error because strtonum returns 0 anyway */
5647 manage_window(Window id
)
5650 struct workspace
*ws
;
5651 struct ws_win
*win
, *ww
;
5652 int format
, i
, ws_idx
, n
, border_me
= 0;
5653 unsigned long nitems
, bytes
;
5654 Atom ws_idx_atom
= 0, type
;
5655 Atom
*prot
= NULL
, *pp
;
5656 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
5657 struct swm_region
*r
;
5663 if ((win
= find_window(id
)) != NULL
)
5664 return (win
); /* already being managed */
5666 /* see if we are on the unmanaged list */
5667 if ((win
= find_unmanaged_window(id
)) != NULL
) {
5668 DNPRINTF(SWM_D_MISC
, "manage previously unmanaged window "
5670 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5671 if (win
->transient
) {
5672 set_child_transient(win
, &trans
);
5673 } if (trans
&& (ww
= find_window(trans
)))
5674 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
5676 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
5677 ewmh_update_actions(win
);
5681 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
5682 err(1, "calloc: failed to allocate memory for new window");
5686 /* see if we need to override the workspace */
5687 p
= find_pid(window_get_pid(id
));
5689 /* Get all the window data in one shot */
5690 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
5692 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
5693 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
5695 XGetWindowAttributes(display
, id
, &win
->wa
);
5696 XGetWMNormalHints(display
, id
, &win
->sh
, &mask
);
5697 win
->hints
= XGetWMHints(display
, id
);
5698 XGetTransientForHint(display
, id
, &trans
);
5700 win
->transient
= trans
;
5701 set_child_transient(win
, &trans
);
5702 DNPRINTF(SWM_D_MISC
, "manage_window: win %lu transient %lu\n",
5703 win
->id
, win
->transient
);
5706 /* get supported protocols */
5707 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
5708 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
5709 if (*pp
== takefocus
)
5710 win
->take_focus
= 1;
5712 win
->can_delete
= 1;
5718 win
->iconic
= get_iconic(win
);
5721 * Figure out where to put the window. If it was previously assigned to
5722 * a workspace (either by spawn() or manually moving), and isn't
5723 * transient, * put it in the same workspace
5725 r
= root_to_region(win
->wa
.root
);
5727 ws
= &r
->s
->ws
[p
->ws
];
5728 TAILQ_REMOVE(&pidlist
, p
, entry
);
5731 } else if (prop
&& win
->transient
== 0) {
5732 DNPRINTF(SWM_D_PROP
, "got property _SWM_WS=%s\n", prop
);
5733 ws_idx
= strtonum((const char *)prop
, 0, 9, &errstr
);
5735 DNPRINTF(SWM_D_EVENT
, "window idx is %s: %s",
5738 ws
= &r
->s
->ws
[ws_idx
];
5741 /* this should launch transients in the same ws as parent */
5743 if ((ww
= find_window(trans
)) != NULL
)
5750 "fix this bug mcbride\n");
5755 /* set up the window layout */
5758 win
->s
= r
->s
; /* this never changes */
5759 if (trans
&& (ww
= find_window(trans
)))
5760 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
5762 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
5764 win
->g
.w
= win
->wa
.width
;
5765 win
->g
.h
= win
->wa
.height
;
5766 win
->g
.x
= win
->wa
.x
;
5767 win
->g
.y
= win
->wa
.y
;
5768 win
->g_floatvalid
= 0;
5769 win
->floatmaxed
= 0;
5770 win
->ewmh_flags
= 0;
5772 /* Set window properties so we can remember this after reincarnation */
5773 if (ws_idx_atom
&& prop
== NULL
&&
5774 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
5776 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
5778 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
5779 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
5784 ewmh_autoquirk(win
);
5786 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
5787 DNPRINTF(SWM_D_CLASS
, "class: %s name: %s\n",
5788 win
->ch
.res_class
, win
->ch
.res_name
);
5790 /* java is retarded so treat it special */
5791 if (strstr(win
->ch
.res_name
, "sun-awt")) {
5796 for (i
= 0; i
< quirks_length
; i
++){
5797 if (!strcmp(win
->ch
.res_class
, quirks
[i
].class) &&
5798 !strcmp(win
->ch
.res_name
, quirks
[i
].name
)) {
5799 DNPRINTF(SWM_D_CLASS
, "found: %s name: %s\n",
5800 win
->ch
.res_class
, win
->ch
.res_name
);
5801 if (quirks
[i
].quirk
& SWM_Q_FLOAT
) {
5805 win
->quirks
= quirks
[i
].quirk
;
5810 /* alter window position if quirky */
5811 if (win
->quirks
& SWM_Q_ANYWHERE
) {
5812 win
->manual
= 1; /* don't center the quirky windows */
5813 bzero(&wc
, sizeof wc
);
5815 if (bar_enabled
&& win
->g
.y
< bar_height
) {
5816 win
->g
.y
= wc
.y
= bar_height
;
5819 if (win
->g
.w
+ win
->g
.x
> WIDTH(r
)) {
5820 win
->g
.x
= wc
.x
= WIDTH(r
) - win
->g
.w
- 2;
5826 /* Reset font sizes (the bruteforce way; no default keybinding). */
5827 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
5828 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
5829 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
5830 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
5831 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
5834 ewmh_get_win_state(win
);
5835 ewmh_update_actions(win
);
5836 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
5840 bzero(&wc
, sizeof wc
);
5841 wc
.border_width
= border_width
;
5842 mask
= CWBorderWidth
;
5843 XConfigureWindow(display
, win
->id
, mask
, &wc
);
5846 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
5847 PropertyChangeMask
| StructureNotifyMask
);
5849 /* floaters need to be mapped if they are in the current workspace */
5850 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
5851 XMapRaised(display
, win
->id
);
5857 free_window(struct ws_win
*win
)
5859 DNPRINTF(SWM_D_MISC
, "free_window: %lu\n", win
->id
);
5864 /* needed for restart wm */
5865 set_win_state(win
, WithdrawnState
);
5867 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5869 if (win
->ch
.res_class
)
5870 XFree(win
->ch
.res_class
);
5871 if (win
->ch
.res_name
)
5872 XFree(win
->ch
.res_name
);
5877 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
5883 unmanage_window(struct ws_win
*win
)
5885 struct ws_win
*parent
;
5890 DNPRINTF(SWM_D_MISC
, "unmanage_window: %lu\n", win
->id
);
5892 if (win
->transient
) {
5893 parent
= find_window(win
->transient
);
5895 parent
->child_trans
= NULL
;
5898 /* focus on root just in case */
5899 XSetInputFocus(display
, PointerRoot
, PointerRoot
, CurrentTime
);
5903 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
5904 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
5910 focus_magic(struct ws_win
*win
)
5912 DNPRINTF(SWM_D_FOCUS
, "focus_magic: %lu\n", WINID(win
));
5917 if (win
->child_trans
) {
5918 /* win = parent & has a transient so focus on that */
5920 focus_win(win
->child_trans
);
5921 if (win
->child_trans
->take_focus
)
5922 client_msg(win
, takefocus
);
5924 /* make sure transient hasn't dissapeared */
5925 if (validate_win(win
->child_trans
) == 0) {
5926 focus_win(win
->child_trans
);
5927 if (win
->child_trans
->take_focus
)
5928 client_msg(win
->child_trans
, takefocus
);
5930 win
->child_trans
= NULL
;
5932 if (win
->take_focus
)
5933 client_msg(win
, takefocus
);
5939 if (win
->take_focus
)
5940 client_msg(win
, takefocus
);
5947 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
5955 XKeyEvent
*ev
= &e
->xkey
;
5957 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
5959 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
5960 for (i
= 0; i
< keys_length
; i
++)
5961 if (keysym
== keys
[i
].keysym
5962 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
5963 && keyfuncs
[keys
[i
].funcid
].func
) {
5964 if (keys
[i
].funcid
== kf_spawn_custom
)
5966 root_to_region(ev
->root
),
5967 &(keyfuncs
[keys
[i
].funcid
].args
),
5971 keyfuncs
[keys
[i
].funcid
].func(
5972 root_to_region(ev
->root
),
5973 &(keyfuncs
[keys
[i
].funcid
].args
)
5979 buttonpress(XEvent
*e
)
5983 XButtonPressedEvent
*ev
= &e
->xbutton
;
5985 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
5987 if ((win
= find_window(ev
->window
)) == NULL
)
5991 action
= client_click
;
5993 for (i
= 0; i
< LENGTH(buttons
); i
++)
5994 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
5995 buttons
[i
].button
== ev
->button
&&
5996 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
5997 buttons
[i
].func(win
, &buttons
[i
].args
);
6001 configurerequest(XEvent
*e
)
6003 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
6008 if ((win
= find_window(ev
->window
)) == NULL
)
6009 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6013 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
6015 bzero(&wc
, sizeof wc
);
6018 wc
.width
= ev
->width
;
6019 wc
.height
= ev
->height
;
6020 wc
.border_width
= ev
->border_width
;
6021 wc
.sibling
= ev
->above
;
6022 wc
.stack_mode
= ev
->detail
;
6023 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
6025 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
6027 config_win(win
, ev
);
6032 configurenotify(XEvent
*e
)
6037 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
6038 e
->xconfigure
.window
);
6040 win
= find_window(e
->xconfigure
.window
);
6042 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
6046 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6047 drain_enter_notify();
6052 destroynotify(XEvent
*e
)
6055 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
6057 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
6059 if ((win
= find_window(ev
->window
)) == NULL
) {
6060 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6066 /* make sure we focus on something */
6069 unmanage_window(win
);
6071 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6072 drain_enter_notify();
6077 enternotify(XEvent
*e
)
6079 XCrossingEvent
*ev
= &e
->xcrossing
;
6084 Window focus_return
;
6085 int revert_to_return
;
6087 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: %lu mode %d detail %d root "
6088 "%lu subwindow %lu same_screen %d focus %d state %d\n",
6089 ev
->window
, ev
->mode
, ev
->detail
, ev
->root
, ev
->subwindow
,
6090 ev
->same_screen
, ev
->focus
, ev
->state
);
6092 if (ev
->mode
!= NotifyNormal
) {
6093 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: generated by cursor grab.\n");
6097 switch (focus_mode
) {
6098 case SWM_FOCUS_DEFAULT
:
6100 case SWM_FOCUS_FOLLOW
:
6102 case SWM_FOCUS_SYNERGY
:
6105 * all these checks need to be in this order because the
6106 * XCheckTypedWindowEvent relies on weeding out the previous events
6108 * making this code an option would enable a follow mouse for focus
6113 * state is set when we are switching workspaces and focus is set when
6114 * the window or a subwindow already has focus (occurs during restart).
6116 * Only honor the focus flag if last_focus_event is not FocusOut,
6117 * this allows scrotwm to continue to control focus when another
6118 * program is also playing with it.
6120 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6121 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6126 * happens when a window is created or destroyed and the border
6127 * crosses the mouse pointer and when switching ws
6129 * we need the subwindow test to see if we came from root in order
6130 * to give focus to floaters
6132 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6133 ev
->subwindow
== 0) {
6134 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6138 /* this window already has focus */
6139 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6140 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6144 /* this window is being deleted or moved to another ws */
6145 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6147 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6148 XPutBackEvent(display
, &cne
);
6152 if ((win
= find_window(ev
->window
)) == NULL
) {
6153 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6158 * In fullstack kill all enters unless they come from a different ws
6159 * (i.e. another region) or focus has been grabbed externally.
6161 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6162 last_focus_event
!= FocusOut
) {
6163 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6164 if ((w
= find_window(focus_return
)) == NULL
||
6166 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6174 if ((win
= find_window(ev
->window
)) == NULL
) {
6175 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6180 * if we have more enternotifies let them handle it in due time
6182 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
6183 DNPRINTF(SWM_D_EVENT
,
6184 "ignoring enternotify: got more enternotify\n");
6185 XPutBackEvent(display
, &cne
);
6192 /* lets us use one switch statement for arbitrary mode/detail combinations */
6193 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6196 focusevent(XEvent
*e
)
6200 u_int32_t mode_detail
;
6201 XFocusChangeEvent
*ev
= &e
->xfocus
;
6203 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: %lu mode %d detail %d\n",
6204 ev
->type
== FocusIn
? "entering" : "leaving",
6205 ev
->window
, ev
->mode
, ev
->detail
);
6207 if (last_focus_event
== ev
->type
) {
6208 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
6212 last_focus_event
= ev
->type
;
6213 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
6215 switch (mode_detail
) {
6216 /* synergy client focus operations */
6217 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
6218 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
6220 /* synergy server focus operations */
6221 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
6223 /* Entering applications like rdesktop that mangle the pointer */
6224 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
6226 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
6227 XSetWindowBorder(display
, win
->id
,
6228 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
6229 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
6232 fprintf(stderr
, "ignoring focusevent\n");
6233 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
6240 mapnotify(XEvent
*e
)
6243 XMapEvent
*ev
= &e
->xmap
;
6245 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: %lu\n", ev
->window
);
6247 win
= manage_window(ev
->window
);
6249 set_win_state(win
, NormalState
);
6253 mappingnotify(XEvent
*e
)
6255 XMappingEvent
*ev
= &e
->xmapping
;
6257 XRefreshKeyboardMapping(ev
);
6258 if (ev
->request
== MappingKeyboard
)
6263 maprequest(XEvent
*e
)
6266 struct swm_region
*r
;
6267 XWindowAttributes wa
;
6268 XMapRequestEvent
*ev
= &e
->xmaprequest
;
6270 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
6271 e
->xmaprequest
.window
);
6273 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
6275 if (wa
.override_redirect
)
6278 win
= manage_window(e
->xmaprequest
.window
);
6280 return; /* can't happen */
6284 /* make new win focused */
6285 r
= root_to_region(win
->wa
.root
);
6286 if (win
->ws
== r
->ws
)
6291 propertynotify(XEvent
*e
)
6294 XPropertyEvent
*ev
= &e
->xproperty
;
6296 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu atom:%s\n",
6297 ev
->window
, XGetAtomName(display
, ev
->atom
));
6299 win
= find_window(ev
->window
);
6303 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
6304 update_iconic(win
, 0);
6305 XMapRaised(display
, win
->id
);
6313 case XA_WM_NORMAL_HINTS
:
6315 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
6316 fprintf(stderr
, "normal hints: flag 0x%x\n", win
->sh
.flags
);
6317 if (win
->sh
.flags
& PMinSize
) {
6318 win
->g
.w
= win
->sh
.min_width
;
6319 win
->g
.h
= win
->sh
.min_height
;
6320 fprintf(stderr
, "min %d %d\n", win
->g
.w
, win
->g
.h
);
6322 XMoveResizeWindow(display
, win
->id
,
6323 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
6326 if (title_name_enabled
|| title_class_enabled
)
6330 if (window_name_enabled
)
6339 unmapnotify(XEvent
*e
)
6343 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
6345 /* determine if we need to help unmanage this window */
6346 win
= find_window(e
->xunmap
.window
);
6350 if (getstate(e
->xunmap
.window
) == NormalState
) {
6351 unmanage_window(win
);
6354 /* giant hack for apps that don't destroy transient windows */
6355 /* eat a bunch of events to prevent remanaging the window */
6357 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6358 EnterWindowMask
, &cne
))
6360 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6361 StructureNotifyMask
, &cne
))
6363 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6364 SubstructureNotifyMask
, &cne
))
6366 /* resend unmap because we ated it */
6367 XUnmapWindow(display
, e
->xunmap
.window
);
6370 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6371 drain_enter_notify();
6375 visibilitynotify(XEvent
*e
)
6378 struct swm_region
*r
;
6380 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
6381 e
->xvisibility
.window
);
6382 if (e
->xvisibility
.state
== VisibilityUnobscured
)
6383 for (i
= 0; i
< ScreenCount(display
); i
++)
6384 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6385 if (e
->xvisibility
.window
== r
->bar_window
)
6390 clientmessage(XEvent
*e
)
6392 XClientMessageEvent
*ev
;
6397 win
= find_window(ev
->window
);
6401 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx type: %ld \n",
6402 ev
->window
, ev
->message_type
);
6404 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
6405 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW \n");
6408 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
6409 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW \n");
6410 if (win
->can_delete
)
6411 client_msg(win
, adelete
);
6413 XKillClient(display
, win
->id
);
6415 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
6416 DNPRINTF(SWM_D_EVENT
,
6417 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
6418 if (win
->floating
) {
6419 if (ev
->data
.l
[0] & (1<<8)) /* x */
6420 win
->g
.x
= ev
->data
.l
[1];
6421 if (ev
->data
.l
[0] & (1<<9)) /* y */
6422 win
->g
.y
= ev
->data
.l
[2];
6423 if (ev
->data
.l
[0] & (1<<10)) /* width */
6424 win
->g
.w
= ev
->data
.l
[3];
6425 if (ev
->data
.l
[0] & (1<<11)) /* height */
6426 win
->g
.h
= ev
->data
.l
[4];
6429 /* TODO: Change stack sizes */
6431 config_win(win
, NULL
);
6433 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
6434 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE \n");
6435 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
6437 ewmh_update_win_state(win
, ev
->data
.l
[2],
6445 xerror_start(Display
*d
, XErrorEvent
*ee
)
6452 xerror(Display
*d
, XErrorEvent
*ee
)
6454 /* fprintf(stderr, "error: %p %p\n", display, ee); */
6462 xerrorxlib
= XSetErrorHandler(xerror_start
);
6464 /* this causes an error if some other window manager is running */
6465 XSelectInput(display
, DefaultRootWindow(display
),
6466 SubstructureRedirectMask
);
6467 XSync(display
, False
);
6471 XSetErrorHandler(xerror
);
6472 XSync(display
, False
);
6477 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
6479 struct swm_region
*r
, *n
;
6480 struct workspace
*ws
= NULL
;
6483 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
6484 s
->idx
, w
, h
, x
, y
);
6486 /* remove any conflicting regions */
6487 n
= TAILQ_FIRST(&s
->rl
);
6490 n
= TAILQ_NEXT(r
, entry
);
6491 if (X(r
) < (x
+ w
) &&
6492 (X(r
) + WIDTH(r
)) > x
&&
6494 (Y(r
) + HEIGHT(r
)) > y
) {
6495 if (r
->ws
->r
!= NULL
)
6496 r
->ws
->old_r
= r
->ws
->r
;
6498 XDestroyWindow(display
, r
->bar_window
);
6499 TAILQ_REMOVE(&s
->rl
, r
, entry
);
6500 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
6504 /* search old regions for one to reuse */
6506 /* size + location match */
6507 TAILQ_FOREACH(r
, &s
->orl
, entry
)
6508 if (X(r
) == x
&& Y(r
) == y
&&
6509 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
6513 TAILQ_FOREACH(r
, &s
->orl
, entry
)
6514 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
6518 TAILQ_REMOVE(&s
->orl
, r
, entry
);
6519 /* try to use old region's workspace */
6520 if (r
->ws
->r
== NULL
)
6523 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
6524 err(1, "calloc: failed to allocate memory for screen");
6526 /* if we don't have a workspace already, find one */
6528 for (i
= 0; i
< SWM_WS_MAX
; i
++)
6529 if (s
->ws
[i
].r
== NULL
) {
6536 errx(1, "no free workspaces");
6547 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
6553 #ifdef SWM_XRR_HAS_CRTC
6555 XRRScreenResources
*sr
;
6558 #endif /* SWM_XRR_HAS_CRTC */
6559 struct swm_region
*r
;
6562 if (i
>= ScreenCount(display
))
6563 errx(1, "invalid screen");
6565 /* remove any old regions */
6566 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
6567 r
->ws
->old_r
= r
->ws
->r
= NULL
;
6568 XDestroyWindow(display
, r
->bar_window
);
6569 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
6570 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
6574 /* map virtual screens onto physical screens */
6575 #ifdef SWM_XRR_HAS_CRTC
6576 if (xrandr_support
) {
6577 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
6579 new_region(&screens
[i
], 0, 0,
6580 DisplayWidth(display
, i
),
6581 DisplayHeight(display
, i
));
6585 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
6586 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
6587 if (ci
->noutput
== 0)
6590 if (ci
!= NULL
&& ci
->mode
== None
)
6591 new_region(&screens
[i
], 0, 0,
6592 DisplayWidth(display
, i
),
6593 DisplayHeight(display
, i
));
6595 new_region(&screens
[i
],
6596 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
6599 XRRFreeCrtcInfo(ci
);
6600 XRRFreeScreenResources(sr
);
6602 #endif /* SWM_XRR_HAS_CRTC */
6604 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
6605 DisplayHeight(display
, i
));
6610 screenchange(XEvent
*e
) {
6611 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
6612 struct swm_region
*r
;
6615 DNPRINTF(SWM_D_EVENT
, "screenchange: %lu\n", xe
->root
);
6617 if (!XRRUpdateConfiguration(e
))
6620 /* silly event doesn't include the screen index */
6621 for (i
= 0; i
< ScreenCount(display
); i
++)
6622 if (screens
[i
].root
== xe
->root
)
6624 if (i
>= ScreenCount(display
))
6625 errx(1, "screenchange: screen not found");
6627 /* brute force for now, just re-enumerate the regions */
6630 /* add bars to all regions */
6631 for (i
= 0; i
< ScreenCount(display
); i
++)
6632 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6635 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6636 drain_enter_notify();
6642 Window d1
, d2
, *wins
= NULL
;
6643 XWindowAttributes wa
;
6648 for (i
= 0; i
< ScreenCount(display
); i
++) {
6649 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
6652 /* attach windows to a region */
6653 /* normal windows */
6654 for (j
= 0; j
< no
; j
++) {
6655 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6656 wa
.override_redirect
||
6657 XGetTransientForHint(display
, wins
[j
], &d1
))
6660 state
= getstate(wins
[j
]);
6661 manage
= state
== IconicState
;
6662 if (wa
.map_state
== IsViewable
|| manage
)
6663 manage_window(wins
[j
]);
6665 /* transient windows */
6666 for (j
= 0; j
< no
; j
++) {
6667 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6668 wa
.override_redirect
)
6671 state
= getstate(wins
[j
]);
6672 manage
= state
== IconicState
;
6673 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
6675 manage_window(wins
[j
]);
6688 int errorbase
, major
, minor
;
6689 struct workspace
*ws
;
6691 if ((screens
= calloc(ScreenCount(display
),
6692 sizeof(struct swm_screen
))) == NULL
)
6693 err(1, "calloc: screens");
6695 /* initial Xrandr setup */
6696 xrandr_support
= XRRQueryExtension(display
,
6697 &xrandr_eventbase
, &errorbase
);
6699 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
6702 /* map physical screens */
6703 for (i
= 0; i
< ScreenCount(display
); i
++) {
6704 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
6706 TAILQ_INIT(&screens
[i
].rl
);
6707 TAILQ_INIT(&screens
[i
].orl
);
6708 screens
[i
].root
= RootWindow(display
, i
);
6710 /* set default colors */
6711 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
6712 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
6713 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
6714 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
6715 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
6717 /* set default cursor */
6718 XDefineCursor(display
, screens
[i
].root
,
6719 XCreateFontCursor(display
, XC_left_ptr
));
6721 /* init all workspaces */
6722 /* XXX these should be dynamically allocated too */
6723 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
6724 ws
= &screens
[i
].ws
[j
];
6730 TAILQ_INIT(&ws
->winlist
);
6731 TAILQ_INIT(&ws
->unmanagedlist
);
6733 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
6734 if (layouts
[k
].l_config
!= NULL
)
6735 layouts
[k
].l_config(ws
,
6736 SWM_ARG_ID_STACKINIT
);
6737 ws
->cur_layout
= &layouts
[0];
6738 ws
->cur_layout
->l_string(ws
);
6744 XRRSelectInput(display
, screens
[i
].root
,
6745 RRScreenChangeNotifyMask
);
6752 if ((bar_fonts
[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
6754 err(1, "setup_globals: strdup");
6755 if ((bar_fonts
[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
6757 err(1, "setup_globals: strdup");
6758 if ((bar_fonts
[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
6760 err(1, "setup_globals: strdup");
6761 if ((spawn_term
[0] = strdup("xterm")) == NULL
)
6762 err(1, "setup_globals: strdup");
6763 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
6771 Atom netwmcheck
, netwmname
, utf8_string
;
6774 /* work around sun jdk bugs, code from wmname */
6775 netwmcheck
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
6776 netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
6777 utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6778 for (i
= 0; i
< ScreenCount(display
); i
++) {
6779 root
= screens
[i
].root
;
6780 win
= XCreateSimpleWindow(display
,root
, 0, 0, 1, 1, 0,
6781 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
,
6782 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
);
6784 XChangeProperty(display
, root
, netwmcheck
, XA_WINDOW
, 32,
6785 PropModeReplace
, (unsigned char *)&win
,1);
6786 XChangeProperty(display
, win
, netwmcheck
, XA_WINDOW
, 32,
6787 PropModeReplace
, (unsigned char *)&win
,1);
6788 XChangeProperty(display
, win
, netwmname
, utf8_string
, 8,
6789 PropModeReplace
, (unsigned char*)"LG3D", strlen("LG3D"));
6794 main(int argc
, char *argv
[])
6796 struct swm_region
*r
, *rr
;
6797 struct ws_win
*winfocus
= NULL
;
6800 char conf
[PATH_MAX
], *cfile
= NULL
;
6805 struct sigaction sact
;
6808 fprintf(stderr
, "Welcome to scrotwm V%s Build: %s\n",
6809 SCROTWM_VERSION
, buildstr
);
6810 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
6812 warnx("no locale support");
6814 if (!(display
= XOpenDisplay(0)))
6815 errx(1, "can not open display");
6818 errx(1, "other wm running");
6820 /* handle some signals */
6821 bzero(&sact
, sizeof(sact
));
6822 sigemptyset(&sact
.sa_mask
);
6824 sact
.sa_handler
= sighdlr
;
6825 sigaction(SIGINT
, &sact
, NULL
);
6826 sigaction(SIGQUIT
, &sact
, NULL
);
6827 sigaction(SIGTERM
, &sact
, NULL
);
6828 sigaction(SIGHUP
, &sact
, NULL
);
6830 sact
.sa_handler
= sighdlr
;
6831 sact
.sa_flags
= SA_NOCLDSTOP
;
6832 sigaction(SIGCHLD
, &sact
, NULL
);
6834 astate
= XInternAtom(display
, "WM_STATE", False
);
6835 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
6836 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
6837 takefocus
= XInternAtom(display
, "WM_TAKE_FOCUS", False
);
6838 a_wmname
= XInternAtom(display
, "WM_NAME", False
);
6839 a_netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
6840 a_utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6841 a_string
= XInternAtom(display
, "STRING", False
);
6842 a_swm_iconic
= XInternAtom(display
, "_SWM_ICONIC", False
);
6844 /* look for local and global conf file */
6845 pwd
= getpwuid(getuid());
6847 errx(1, "invalid user %d", getuid());
6856 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
6857 if (stat(conf
, &sb
) != -1) {
6858 if (S_ISREG(sb
.st_mode
))
6861 /* try global conf file */
6862 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
6863 if (!stat(conf
, &sb
))
6864 if (S_ISREG(sb
.st_mode
))
6868 conf_load(cfile
, SWM_CONF_DEFAULT
);
6871 /* set some values to work around bad programs */
6873 /* grab existing windows (before we build the bars) */
6876 if (getenv("SWM_STARTED") == NULL
)
6877 setenv("SWM_STARTED", "YES", 1);
6879 /* setup all bars */
6880 for (i
= 0; i
< ScreenCount(display
); i
++)
6881 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
6882 if (winfocus
== NULL
)
6883 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
6891 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6892 drain_enter_notify();
6894 xfd
= ConnectionNumber(display
);
6896 while (XPending(display
)) {
6897 XNextEvent(display
, &e
);
6900 if (e
.type
< LASTEvent
) {
6902 if (handler
[e
.type
])
6903 handler
[e
.type
](&e
);
6905 DNPRINTF(SWM_D_EVENT
,
6906 "win: %lu unknown event: %d\n",
6907 e
.xany
.window
, e
.type
);
6909 switch (e
.type
- xrandr_eventbase
) {
6910 case RRScreenChangeNotify
:
6914 DNPRINTF(SWM_D_EVENT
,
6915 "win: %lu unknown xrandr event: "
6916 "%d\n", e
.xany
.window
, e
.type
);
6922 /* if we are being restarted go focus on first window */
6924 rr
= winfocus
->ws
->r
;
6926 /* not a visible window */
6930 /* move pointer to first screen if multi screen */
6931 if (ScreenCount(display
) > 1 || outputs
> 1)
6932 XWarpPointer(display
, None
, rr
->s
[0].root
,
6933 0, 0, 0, 0, rr
->g
.x
,
6934 rr
->g
.y
+ (bar_enabled
? bar_height
: 0));
6936 a
.id
= SWM_ARG_ID_FOCUSCUR
;
6946 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
6948 DNPRINTF(SWM_D_MISC
, "select failed");
6949 if (restart_wm
== 1)
6950 restart(NULL
, NULL
);
6951 if (search_resp
== 1)
6963 XFreeGC(display
, bar_gc
);
6964 XCloseDisplay(display
);