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>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
12 * Permission to use, copy, modify, and distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 * Much code and ideas taken from dwm under the following license:
26 * MIT/X Consortium License
28 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
29 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
30 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
31 * 2007 Premysl Hruby <dfenze at gmail dot com>
32 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
33 * 2007 Christof Musik <christof at sendfax dot de>
34 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
35 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
36 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
38 * Permission is hereby granted, free of charge, to any person obtaining a
39 * copy of this software and associated documentation files (the "Software"),
40 * to deal in the Software without restriction, including without limitation
41 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
42 * and/or sell copies of the Software, and to permit persons to whom the
43 * Software is furnished to do so, subject to the following conditions:
45 * The above copyright notice and this permission notice shall be included in
46 * all copies or substantial portions of the Software.
48 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
51 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
53 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
54 * DEALINGS IN THE SOFTWARE.
72 #include <sys/types.h>
76 #include <sys/queue.h>
77 #include <sys/param.h>
78 #include <sys/select.h>
79 #if defined(__linux__)
81 #elif defined(__OpenBSD__)
83 #elif defined(__FreeBSD__)
89 #include <X11/cursorfont.h>
90 #include <X11/keysym.h>
91 #include <X11/Xatom.h>
93 #include <X11/Xproto.h>
94 #include <X11/Xutil.h>
95 #include <X11/extensions/Xrandr.h>
96 #include <X11/extensions/XTest.h>
104 #ifdef SPECTRWM_BUILDSTR
105 static const char *buildstr
= SPECTRWM_BUILDSTR
;
107 static const char *buildstr
= SPECTRWM_VERSION
;
111 # error XRandR versions less than 1.0 are not supported
116 #define SWM_XRR_HAS_CRTC
120 /*#define SWM_DEBUG*/
122 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
123 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
124 #define SWM_D_MISC 0x0001
125 #define SWM_D_EVENT 0x0002
126 #define SWM_D_WS 0x0004
127 #define SWM_D_FOCUS 0x0008
128 #define SWM_D_MOVE 0x0010
129 #define SWM_D_STACK 0x0020
130 #define SWM_D_MOUSE 0x0040
131 #define SWM_D_PROP 0x0080
132 #define SWM_D_CLASS 0x0100
133 #define SWM_D_KEY 0x0200
134 #define SWM_D_QUIRK 0x0400
135 #define SWM_D_SPAWN 0x0800
136 #define SWM_D_EVENTQ 0x1000
137 #define SWM_D_CONF 0x2000
138 #define SWM_D_BAR 0x4000
140 u_int32_t swm_debug
= 0
158 #define DPRINTF(x...)
159 #define DNPRINTF(n,x...)
162 #define LENGTH(x) (sizeof x / sizeof x[0])
163 #define MODKEY Mod1Mask
164 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
165 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
166 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
167 #define SWM_PROPLEN (16)
168 #define SWM_FUNCNAME_LEN (32)
169 #define SWM_KEYS_LEN (255)
170 #define SWM_QUIRK_LEN (64)
171 #define X(r) (r)->g.x
172 #define Y(r) (r)->g.y
173 #define WIDTH(r) (r)->g.w
174 #define HEIGHT(r) (r)->g.h
175 #define SH_MIN(w) (w)->sh_mask & PMinSize
176 #define SH_MIN_W(w) (w)->sh.min_width
177 #define SH_MIN_H(w) (w)->sh.min_height
178 #define SH_MAX(w) (w)->sh_mask & PMaxSize
179 #define SH_MAX_W(w) (w)->sh.max_width
180 #define SH_MAX_H(w) (w)->sh.max_height
181 #define SH_INC(w) (w)->sh_mask & PResizeInc
182 #define SH_INC_W(w) (w)->sh.width_inc
183 #define SH_INC_H(w) (w)->sh.height_inc
184 #define SWM_MAX_FONT_STEPS (3)
185 #define WINID(w) ((w) ? (w)->id : 0)
186 #define YESNO(x) ((x) ? "yes" : "no")
188 #define SWM_FOCUS_DEFAULT (0)
189 #define SWM_FOCUS_SYNERGY (1)
190 #define SWM_FOCUS_FOLLOW (2)
192 #define SWM_CONF_DEFAULT (0)
193 #define SWM_CONF_KEYMAPPING (1)
196 #define SWM_LIB "/usr/local/lib/libswmhack.so"
209 volatile sig_atomic_t running
= 1;
210 volatile sig_atomic_t restart_wm
= 0;
212 int last_focus_event
= FocusOut
;
213 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
217 int xrandr_eventbase
;
218 unsigned int numlockmask
= 0;
222 int cycle_visible
= 0;
224 int font_adjusted
= 0;
225 unsigned int mod_key
= MODKEY
;
228 struct swm_region
*search_r
;
229 int select_list_pipe
[2];
230 int select_resp_pipe
[2];
232 volatile sig_atomic_t search_resp
;
233 int search_resp_action
;
235 struct search_window
{
236 TAILQ_ENTRY(search_window
) entry
;
242 TAILQ_HEAD(search_winlist
, search_window
);
244 struct search_winlist search_wl
;
249 SWM_SEARCH_UNICONIFY
,
250 SWM_SEARCH_NAME_WORKSPACE
,
251 SWM_SEARCH_SEARCH_WORKSPACE
,
252 SWM_SEARCH_SEARCH_WINDOW
256 double dialog_ratio
= 0.6;
258 #define SWM_BAR_MAX (256)
259 #define SWM_BAR_JUSTIFY_LEFT (0)
260 #define SWM_BAR_JUSTIFY_CENTER (1)
261 #define SWM_BAR_JUSTIFY_RIGHT (2)
262 #define SWM_BAR_OFFSET (4)
263 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
264 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
265 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
266 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"
268 #ifdef X_HAVE_UTF8_STRING
269 #define DRAWSTRING(x...) Xutf8DrawString(x)
271 #define DRAWSTRING(x...) XmbDrawString(x)
274 char *bar_argv
[] = { NULL
, NULL
};
276 unsigned char bar_ext
[SWM_BAR_MAX
];
277 char bar_vertext
[SWM_BAR_MAX
];
279 sig_atomic_t bar_alarm
= 0;
282 int bar_border_width
= 1;
283 int bar_at_bottom
= 0;
285 int bar_extra_running
= 0;
288 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
289 int stack_enabled
= 1;
290 int clock_enabled
= 1;
291 int urgent_enabled
= 0;
292 char *clock_format
= NULL
;
293 int title_name_enabled
= 0;
294 int title_class_enabled
= 0;
295 int window_name_enabled
= 0;
296 int focus_mode
= SWM_FOCUS_DEFAULT
;
297 int disable_border
= 0;
298 int border_width
= 1;
299 int verbose_layout
= 0;
302 XFontSetExtents
*bar_fs_extents
;
304 char *spawn_term
[] = { NULL
, NULL
}; /* XXX fully dynamic */
307 #define SWM_MENU_FN (2)
308 #define SWM_MENU_NB (4)
309 #define SWM_MENU_NF (6)
310 #define SWM_MENU_SB (8)
311 #define SWM_MENU_SF (10)
313 /* layout manager data */
314 struct swm_geometry
{
324 /* virtual "screens" */
326 TAILQ_ENTRY(swm_region
) entry
;
327 struct swm_geometry g
;
328 struct workspace
*ws
; /* current workspace on this region */
329 struct workspace
*ws_prior
; /* prior workspace on this region */
330 struct swm_screen
*s
; /* screen idx */
333 TAILQ_HEAD(swm_region_list
, swm_region
);
336 TAILQ_ENTRY(ws_win
) entry
;
339 struct ws_win
*child_trans
; /* transient child window */
340 struct swm_geometry g
; /* current geometry */
341 struct swm_geometry g_float
; /* geometry when floating */
342 struct swm_geometry rg_float
; /* region geom when floating */
343 int g_floatvalid
; /* g_float geometry validity */
344 int floatmaxed
; /* whether maxed by max_stack */
348 unsigned int ewmh_flags
;
349 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
355 unsigned long quirks
;
356 struct workspace
*ws
; /* always valid */
357 struct swm_screen
*s
; /* always valid, never changes */
358 XWindowAttributes wa
;
364 TAILQ_HEAD(ws_win_list
, ws_win
);
368 TAILQ_ENTRY(pid_e
) entry
;
372 TAILQ_HEAD(pid_list
, pid_e
);
373 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
375 /* layout handlers */
377 void vertical_config(struct workspace
*, int);
378 void vertical_stack(struct workspace
*, struct swm_geometry
*);
379 void horizontal_config(struct workspace
*, int);
380 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
381 void max_stack(struct workspace
*, struct swm_geometry
*);
382 void plain_stacker(struct workspace
*);
383 void fancy_stacker(struct workspace
*);
385 struct ws_win
*find_window(Window
);
387 void grabbuttons(struct ws_win
*, int);
388 void new_region(struct swm_screen
*, int, int, int, int);
389 void unmanage_window(struct ws_win
*);
390 long getstate(Window
);
392 int conf_load(char *, int);
395 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
396 void (*l_config
)(struct workspace
*, int);
398 #define SWM_L_FOCUSPREV (1<<0)
399 #define SWM_L_MAPONFOCUS (1<<1)
400 void (*l_string
)(struct workspace
*);
402 /* stack, configure */
403 { vertical_stack
, vertical_config
, 0, plain_stacker
},
404 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
406 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
407 { NULL
, NULL
, 0, NULL
},
410 /* position of max_stack mode in the layouts array, index into layouts! */
411 #define SWM_V_STACK (0)
412 #define SWM_H_STACK (1)
413 #define SWM_MAX_STACK (2)
415 #define SWM_H_SLICE (32)
416 #define SWM_V_SLICE (32)
418 /* define work spaces */
420 int idx
; /* workspace index */
421 char *name
; /* workspace name */
422 int always_raise
; /* raise windows on focus */
423 struct layout
*cur_layout
; /* current layout handlers */
424 struct ws_win
*focus
; /* may be NULL */
425 struct ws_win
*focus_prev
; /* may be NULL */
426 struct swm_region
*r
; /* may be NULL */
427 struct swm_region
*old_r
; /* may be NULL */
428 struct ws_win_list winlist
; /* list of windows in ws */
429 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
430 char stacker
[10]; /* display stacker and layout */
434 int horizontal_msize
;
436 int horizontal_stacks
;
445 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
446 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
448 /* physical screen mapping */
449 #define SWM_WS_MAX (22) /* hard limit */
450 int workspace_limit
= 10; /* soft limit */
453 int idx
; /* screen index */
454 struct swm_region_list rl
; /* list of regions on this screen */
455 struct swm_region_list orl
; /* list of old regions */
457 struct workspace ws
[SWM_WS_MAX
];
463 } c
[SWM_S_COLOR_MAX
];
467 struct swm_screen
*screens
;
470 /* args to functions */
473 #define SWM_ARG_ID_FOCUSNEXT (0)
474 #define SWM_ARG_ID_FOCUSPREV (1)
475 #define SWM_ARG_ID_FOCUSMAIN (2)
476 #define SWM_ARG_ID_FOCUSCUR (4)
477 #define SWM_ARG_ID_SWAPNEXT (10)
478 #define SWM_ARG_ID_SWAPPREV (11)
479 #define SWM_ARG_ID_SWAPMAIN (12)
480 #define SWM_ARG_ID_MOVELAST (13)
481 #define SWM_ARG_ID_MASTERSHRINK (20)
482 #define SWM_ARG_ID_MASTERGROW (21)
483 #define SWM_ARG_ID_MASTERADD (22)
484 #define SWM_ARG_ID_MASTERDEL (23)
485 #define SWM_ARG_ID_FLIPLAYOUT (24)
486 #define SWM_ARG_ID_STACKRESET (30)
487 #define SWM_ARG_ID_STACKINIT (31)
488 #define SWM_ARG_ID_CYCLEWS_UP (40)
489 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
490 #define SWM_ARG_ID_CYCLESC_UP (42)
491 #define SWM_ARG_ID_CYCLESC_DOWN (43)
492 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
493 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
494 #define SWM_ARG_ID_STACKINC (50)
495 #define SWM_ARG_ID_STACKDEC (51)
496 #define SWM_ARG_ID_SS_ALL (60)
497 #define SWM_ARG_ID_SS_WINDOW (61)
498 #define SWM_ARG_ID_DONTCENTER (70)
499 #define SWM_ARG_ID_CENTER (71)
500 #define SWM_ARG_ID_KILLWINDOW (80)
501 #define SWM_ARG_ID_DELETEWINDOW (81)
502 #define SWM_ARG_ID_WIDTHGROW (90)
503 #define SWM_ARG_ID_WIDTHSHRINK (91)
504 #define SWM_ARG_ID_HEIGHTGROW (92)
505 #define SWM_ARG_ID_HEIGHTSHRINK (93)
506 #define SWM_ARG_ID_MOVEUP (100)
507 #define SWM_ARG_ID_MOVEDOWN (101)
508 #define SWM_ARG_ID_MOVELEFT (102)
509 #define SWM_ARG_ID_MOVERIGHT (103)
513 void focus(struct swm_region
*, union arg
*);
514 void focus_magic(struct ws_win
*);
518 TAILQ_ENTRY(quirk
) entry
;
522 #define SWM_Q_FLOAT (1<<0) /* float this window */
523 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
524 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
525 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
526 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
527 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
529 TAILQ_HEAD(quirk_list
, quirk
);
530 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
533 * Supported EWMH hints should be added to
534 * both the enum and the ewmh array
536 enum { _NET_ACTIVE_WINDOW
, _NET_MOVERESIZE_WINDOW
, _NET_CLOSE_WINDOW
,
537 _NET_WM_WINDOW_TYPE
, _NET_WM_WINDOW_TYPE_DOCK
,
538 _NET_WM_WINDOW_TYPE_TOOLBAR
, _NET_WM_WINDOW_TYPE_UTILITY
,
539 _NET_WM_WINDOW_TYPE_SPLASH
, _NET_WM_WINDOW_TYPE_DIALOG
,
540 _NET_WM_WINDOW_TYPE_NORMAL
, _NET_WM_STATE
,
541 _NET_WM_STATE_MAXIMIZED_HORZ
, _NET_WM_STATE_MAXIMIZED_VERT
,
542 _NET_WM_STATE_SKIP_TASKBAR
, _NET_WM_STATE_SKIP_PAGER
,
543 _NET_WM_STATE_HIDDEN
, _NET_WM_STATE_ABOVE
, _SWM_WM_STATE_MANUAL
,
544 _NET_WM_STATE_FULLSCREEN
, _NET_WM_ALLOWED_ACTIONS
, _NET_WM_ACTION_MOVE
,
545 _NET_WM_ACTION_RESIZE
, _NET_WM_ACTION_FULLSCREEN
, _NET_WM_ACTION_CLOSE
,
551 } ewmh
[SWM_EWMH_HINT_MAX
] = {
552 /* must be in same order as in the enum */
553 {"_NET_ACTIVE_WINDOW", None
},
554 {"_NET_MOVERESIZE_WINDOW", None
},
555 {"_NET_CLOSE_WINDOW", None
},
556 {"_NET_WM_WINDOW_TYPE", None
},
557 {"_NET_WM_WINDOW_TYPE_DOCK", None
},
558 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None
},
559 {"_NET_WM_WINDOW_TYPE_UTILITY", None
},
560 {"_NET_WM_WINDOW_TYPE_SPLASH", None
},
561 {"_NET_WM_WINDOW_TYPE_DIALOG", None
},
562 {"_NET_WM_WINDOW_TYPE_NORMAL", None
},
563 {"_NET_WM_STATE", None
},
564 {"_NET_WM_STATE_MAXIMIZED_HORZ", None
},
565 {"_NET_WM_STATE_MAXIMIZED_VERT", None
},
566 {"_NET_WM_STATE_SKIP_TASKBAR", None
},
567 {"_NET_WM_STATE_SKIP_PAGER", None
},
568 {"_NET_WM_STATE_HIDDEN", None
},
569 {"_NET_WM_STATE_ABOVE", None
},
570 {"_SWM_WM_STATE_MANUAL", None
},
571 {"_NET_WM_STATE_FULLSCREEN", None
},
572 {"_NET_WM_ALLOWED_ACTIONS", None
},
573 {"_NET_WM_ACTION_MOVE", None
},
574 {"_NET_WM_ACTION_RESIZE", None
},
575 {"_NET_WM_ACTION_FULLSCREEN", None
},
576 {"_NET_WM_ACTION_CLOSE", None
},
579 void store_float_geom(struct ws_win
*, struct swm_region
*);
580 int floating_toggle_win(struct ws_win
*);
581 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
582 unsigned char *get_win_name(Window
);
585 get_property(Window id
, Atom atom
, long count
, Atom type
, unsigned long *nitems
,
586 unsigned long *nbytes
, unsigned char **data
)
589 unsigned long *nbytes_ret
, *nitems_ret
;
590 unsigned long nbytes_tmp
, nitems_tmp
;
593 nbytes_ret
= nbytes
!= NULL
? nbytes
: &nbytes_tmp
;
594 nitems_ret
= nitems
!= NULL
? nitems
: &nitems_tmp
;
596 status
= XGetWindowProperty(display
, id
, atom
, 0L, count
, False
, type
,
597 &real
, &format
, nitems_ret
, nbytes_ret
, data
);
599 if (status
!= Success
)
608 update_iconic(struct ws_win
*win
, int newv
)
615 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
619 XChangeProperty(display
, win
->id
, iprop
, XA_INTEGER
, 32,
620 PropModeReplace
, (unsigned char *)&v
, 1);
622 XDeleteProperty(display
, win
->id
, iprop
);
626 get_iconic(struct ws_win
*win
)
631 unsigned long nitems
, extra
;
632 unsigned char *prop
= NULL
;
634 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
637 status
= XGetWindowProperty(display
, win
->id
, iprop
, 0L, 1L,
638 False
, XA_INTEGER
, &rettype
, &retfmt
, &nitems
, &extra
, &prop
);
639 if (status
!= Success
)
641 if (rettype
!= XA_INTEGER
|| retfmt
!= 32)
645 v
= *((int32_t *)prop
);
659 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
661 for (i
= 0; i
< LENGTH(ewmh
); i
++)
662 ewmh
[i
].atom
= XInternAtom(display
, ewmh
[i
].name
, False
);
664 for (i
= 0; i
< ScreenCount(display
); i
++) {
665 /* Support check window will be created by workaround(). */
667 /* Report supported atoms */
668 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
669 for (j
= 0; j
< LENGTH(ewmh
); j
++)
670 XChangeProperty(display
, screens
[i
].root
,
671 sup_list
, XA_ATOM
, 32,
672 PropModeAppend
, (unsigned char *)&ewmh
[j
].atom
,1);
680 unsigned char *data
= NULL
;
682 Atom sup_check
, sup_list
;
685 sup_check
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
686 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
688 for (i
= 0; i
< ScreenCount(display
); i
++) {
689 /* Get the support check window and destroy it */
690 success
= get_property(screens
[i
].root
, sup_check
, 1, XA_WINDOW
,
695 XDestroyWindow(display
, id
);
696 XDeleteProperty(display
, screens
[i
].root
, sup_check
);
697 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
705 ewmh_autoquirk(struct ws_win
*win
)
708 unsigned long *data
= NULL
, n
;
711 success
= get_property(win
->id
, ewmh
[_NET_WM_WINDOW_TYPE
].atom
, (~0L),
712 XA_ATOM
, &n
, NULL
, (void *)&data
);
719 for (i
= 0; i
< n
; i
++) {
721 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
723 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
724 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
725 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
727 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
730 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
731 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
733 win
->quirks
= SWM_Q_FLOAT
;
741 #define SWM_EWMH_ACTION_COUNT_MAX (6)
742 #define EWMH_F_FULLSCREEN (1<<0)
743 #define EWMH_F_ABOVE (1<<1)
744 #define EWMH_F_HIDDEN (1<<2)
745 #define EWMH_F_SKIP_PAGER (1<<3)
746 #define EWMH_F_SKIP_TASKBAR (1<<4)
747 #define SWM_F_MANUAL (1<<5)
750 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
752 struct swm_geometry rg
;
760 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%lx, "
761 "fullscreen %s\n", win
->id
, YESNO(fs
));
766 store_float_geom(win
, win
->ws
->r
);
770 if (win
->g_floatvalid
) {
771 /* refloat at last floating relative position */
772 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ rg
.x
;
773 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ rg
.y
;
774 WIDTH(win
) = win
->g_float
.w
;
775 HEIGHT(win
) = win
->g_float
.h
;
783 ewmh_update_actions(struct ws_win
*win
)
785 Atom actions
[SWM_EWMH_ACTION_COUNT_MAX
];
791 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
794 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
795 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
798 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
,
799 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)actions
, n
);
802 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
803 #define _NET_WM_STATE_ADD 1 /* add/set property */
804 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
807 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
809 unsigned int mask
= 0;
810 unsigned int changed
= 0;
811 unsigned int orig_flags
;
816 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
817 mask
= EWMH_F_FULLSCREEN
;
818 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
820 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
822 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
823 mask
= EWMH_F_SKIP_PAGER
;
824 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
825 mask
= EWMH_F_SKIP_TASKBAR
;
828 orig_flags
= win
->ewmh_flags
;
831 case _NET_WM_STATE_REMOVE
:
832 win
->ewmh_flags
&= ~mask
;
834 case _NET_WM_STATE_ADD
:
835 win
->ewmh_flags
|= mask
;
837 case _NET_WM_STATE_TOGGLE
:
838 win
->ewmh_flags
^= mask
;
842 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
844 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
846 if (!floating_toggle_win(win
))
847 win
->ewmh_flags
= orig_flags
; /* revert */
848 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
850 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
851 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
853 if (!ewmh_set_win_fullscreen(win
,
854 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
855 win
->ewmh_flags
= orig_flags
; /* revert */
857 XDeleteProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
859 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
860 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
861 XA_ATOM
, 32, PropModeAppend
,
862 (unsigned char *)&ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
, 1);
863 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
864 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
865 XA_ATOM
, 32, PropModeAppend
,
866 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
, 1);
867 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
868 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
869 XA_ATOM
, 32, PropModeAppend
,
870 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
, 1);
871 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
872 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
873 XA_ATOM
, 32, PropModeAppend
,
874 (unsigned char *)&ewmh
[_NET_WM_STATE_ABOVE
].atom
, 1);
875 if (win
->ewmh_flags
& SWM_F_MANUAL
)
876 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
877 XA_ATOM
, 32, PropModeAppend
,
878 (unsigned char *)&ewmh
[_SWM_WM_STATE_MANUAL
].atom
, 1);
882 ewmh_get_win_state(struct ws_win
*win
)
893 win
->ewmh_flags
|= EWMH_F_ABOVE
;
895 win
->ewmh_flags
|= SWM_F_MANUAL
;
897 success
= get_property(win
->id
, ewmh
[_NET_WM_STATE
].atom
,
898 (~0L), XA_ATOM
, &n
, NULL
, (void *)&states
);
903 for (i
= 0; i
< n
; i
++)
904 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
912 geteventname(XEvent
*e
)
924 name
= "ButtonPress";
927 name
= "ButtonRelease";
930 name
= "MotionNotify";
933 name
= "EnterNotify";
936 name
= "LeaveNotify";
945 name
= "KeymapNotify";
951 name
= "GraphicsExpose";
956 case VisibilityNotify
:
957 name
= "VisibilityNotify";
960 name
= "CreateNotify";
963 name
= "DestroyNotify";
966 name
= "UnmapNotify";
975 name
= "ReparentNotify";
977 case ConfigureNotify
:
978 name
= "ConfigureNotify";
980 case ConfigureRequest
:
981 name
= "ConfigureRequest";
984 name
= "GravityNotify";
987 name
= "ResizeRequest";
989 case CirculateNotify
:
990 name
= "CirculateNotify";
992 case CirculateRequest
:
993 name
= "CirculateRequest";
996 name
= "PropertyNotify";
999 name
= "SelectionClear";
1001 case SelectionRequest
:
1002 name
= "SelectionRequest";
1004 case SelectionNotify
:
1005 name
= "SelectionNotify";
1007 case ColormapNotify
:
1008 name
= "ColormapNotify";
1011 name
= "ClientMessage";
1014 name
= "MappingNotify";
1024 xrandr_geteventname(XEvent
*e
)
1028 switch(e
->type
- xrandr_eventbase
) {
1029 case RRScreenChangeNotify
:
1030 name
= "RRScreenChangeNotify";
1040 dumpwins(struct swm_region
*r
, union arg
*args
)
1044 XWindowAttributes wa
;
1046 if (r
->ws
== NULL
) {
1047 warnx("dumpwins: invalid workspace");
1051 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1053 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1054 state
= getstate(win
->id
);
1055 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1056 warnx("window: 0x%lx, failed XGetWindowAttributes",
1058 warnx("window: 0x%lx, map_state: %d, state: %d, "
1059 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1063 warnx("===== unmanaged window list =====");
1064 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1065 state
= getstate(win
->id
);
1066 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1067 warnx("window: 0x%lx, failed XGetWindowAttributes",
1069 warnx("window: 0x%lx, map_state: %d, state: %d, "
1070 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1074 warnx("=================================");
1078 dumpwins(struct swm_region
*r
, union arg
*args
)
1081 #endif /* SWM_DEBUG */
1083 void expose(XEvent
*);
1084 void keypress(XEvent
*);
1085 void buttonpress(XEvent
*);
1086 void configurerequest(XEvent
*);
1087 void configurenotify(XEvent
*);
1088 void destroynotify(XEvent
*);
1089 void enternotify(XEvent
*);
1090 void focusevent(XEvent
*);
1091 void mapnotify(XEvent
*);
1092 void mappingnotify(XEvent
*);
1093 void maprequest(XEvent
*);
1094 void propertynotify(XEvent
*);
1095 void unmapnotify(XEvent
*);
1096 void visibilitynotify(XEvent
*);
1097 void clientmessage(XEvent
*);
1099 void (*handler
[LASTEvent
])(XEvent
*) = {
1101 [KeyPress
] = keypress
,
1102 [ButtonPress
] = buttonpress
,
1103 [ConfigureRequest
] = configurerequest
,
1104 [ConfigureNotify
] = configurenotify
,
1105 [DestroyNotify
] = destroynotify
,
1106 [EnterNotify
] = enternotify
,
1107 [FocusIn
] = focusevent
,
1108 [FocusOut
] = focusevent
,
1109 [MapNotify
] = mapnotify
,
1110 [MappingNotify
] = mappingnotify
,
1111 [MapRequest
] = maprequest
,
1112 [PropertyNotify
] = propertynotify
,
1113 [UnmapNotify
] = unmapnotify
,
1114 [VisibilityNotify
] = visibilitynotify
,
1115 [ClientMessage
] = clientmessage
,
1121 int saved_errno
, status
;
1124 saved_errno
= errno
;
1128 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1133 if (errno
!= ECHILD
)
1134 warn("sighdlr: waitpid");
1135 #endif /* SWM_DEBUG */
1138 if (pid
== searchpid
)
1142 if (WIFEXITED(status
)) {
1143 if (WEXITSTATUS(status
) != 0)
1144 warnx("sighdlr: child exit status: %d",
1145 WEXITSTATUS(status
));
1147 warnx("sighdlr: child is terminated "
1149 #endif /* SWM_DEBUG */
1163 errno
= saved_errno
;
1169 struct pid_e
*p
= NULL
;
1171 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1176 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1185 name_to_color(char *colorname
)
1189 XColor screen_def
, exact_def
;
1190 unsigned long result
= 0;
1191 char cname
[32] = "#";
1193 cmap
= DefaultColormap(display
, screens
[0].idx
);
1194 status
= XAllocNamedColor(display
, cmap
, colorname
,
1195 &screen_def
, &exact_def
);
1197 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1198 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
1202 result
= screen_def
.pixel
;
1204 warnx("color '%s' not found", colorname
);
1210 setscreencolor(char *val
, int i
, int c
)
1212 if (i
> 0 && i
<= ScreenCount(display
)) {
1213 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1214 free(screens
[i
- 1].c
[c
].name
);
1215 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1217 } else if (i
== -1) {
1218 for (i
= 0; i
< ScreenCount(display
); i
++) {
1219 screens
[i
].c
[c
].color
= name_to_color(val
);
1220 free(screens
[i
].c
[c
].name
);
1221 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1225 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1226 i
, ScreenCount(display
));
1230 fancy_stacker(struct workspace
*ws
)
1232 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1233 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1234 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1235 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1236 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1237 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1238 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1239 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1240 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1244 plain_stacker(struct workspace
*ws
)
1246 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1247 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1248 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1249 sizeof ws
->stacker
);
1250 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1251 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1252 sizeof ws
->stacker
);
1256 custom_region(char *val
)
1258 unsigned int sidx
, x
, y
, w
, h
;
1260 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1261 errx(1, "invalid custom region, "
1262 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1263 if (sidx
< 1 || sidx
> ScreenCount(display
))
1264 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1265 sidx
, ScreenCount(display
));
1269 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1271 if (x
> DisplayWidth(display
, sidx
) ||
1272 y
> DisplayHeight(display
, sidx
) ||
1273 w
+ x
> DisplayWidth(display
, sidx
) ||
1274 h
+ y
> DisplayHeight(display
, sidx
)) {
1275 warnx("ignoring region %ux%u+%u+%u - not within screen "
1276 "boundaries (%ux%u)", w
, h
, x
, y
,
1277 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
1281 new_region(&screens
[sidx
], x
, y
, w
, h
);
1285 socket_setnonblock(int fd
)
1289 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1290 err(1, "fcntl F_GETFL");
1291 flags
|= O_NONBLOCK
;
1292 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1293 err(1, "fcntl F_SETFL");
1297 bar_print(struct swm_region
*r
, char *s
)
1301 XRectangle ibox
, lbox
;
1303 XClearWindow(display
, r
->bar_window
);
1306 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1308 switch (bar_justify
) {
1309 case SWM_BAR_JUSTIFY_LEFT
:
1312 case SWM_BAR_JUSTIFY_CENTER
:
1313 x
= (WIDTH(r
) - lbox
.width
) / 2;
1315 case SWM_BAR_JUSTIFY_RIGHT
:
1316 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1320 if (x
< SWM_BAR_OFFSET
)
1323 DRAWSTRING(display
, r
->bar_window
, bar_fs
, r
->s
->bar_gc
,
1324 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1329 bar_extra_stop(void)
1333 bzero(bar_pipe
, sizeof bar_pipe
);
1336 kill(bar_pid
, SIGTERM
);
1339 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1344 bar_class_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1346 int do_class
, do_name
;
1348 XClassHint
*xch
= NULL
;
1350 if ((title_name_enabled
== 1 || title_class_enabled
== 1) &&
1351 cur_focus
!= NULL
) {
1352 if ((xch
= XAllocClassHint()) == NULL
)
1354 status
= XGetClassHint(display
, cur_focus
->id
, xch
);
1355 if (status
== BadWindow
|| status
== BadAlloc
)
1357 do_class
= (title_class_enabled
&& xch
->res_class
!= NULL
);
1358 do_name
= (title_name_enabled
&& xch
->res_name
!= NULL
);
1360 strlcat(s
, xch
->res_class
, sz
);
1361 if (do_class
&& do_name
)
1362 strlcat(s
, ":", sz
);
1364 strlcat(s
, xch
->res_name
, sz
);
1365 strlcat(s
, " ", sz
);
1369 XFree(xch
->res_name
);
1370 XFree(xch
->res_class
);
1376 bar_window_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1378 unsigned char *title
;
1380 if (window_name_enabled
&& cur_focus
!= NULL
) {
1381 title
= get_win_name(cur_focus
->id
);
1382 if (title
!= NULL
) {
1383 DNPRINTF(SWM_D_BAR
, "bar_window_name: title: %s\n",
1386 if (cur_focus
->floating
)
1387 strlcat(s
, "(f) ", sz
);
1388 strlcat(s
, (char *)title
, sz
);
1389 strlcat(s
, " ", sz
);
1395 int urgent
[SWM_WS_MAX
];
1397 bar_urgent(char *s
, ssize_t sz
)
1399 XWMHints
*wmh
= NULL
;
1404 if (urgent_enabled
== 0)
1407 for (i
= 0; i
< workspace_limit
; i
++)
1410 for (i
= 0; i
< ScreenCount(display
); i
++)
1411 for (j
= 0; j
< workspace_limit
; j
++)
1412 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1413 wmh
= XGetWMHints(display
, win
->id
);
1417 if (wmh
->flags
& XUrgencyHint
)
1422 strlcat(s
, "* ", sz
);
1423 for (i
= 0; i
< workspace_limit
; i
++) {
1425 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1427 snprintf(b
, sizeof b
, "- ");
1430 strlcat(s
, "* ", sz
);
1438 struct swm_region
*r
;
1441 char ws
[SWM_BAR_MAX
];
1442 char s
[SWM_BAR_MAX
];
1443 unsigned char cn
[SWM_BAR_MAX
];
1444 char loc
[SWM_BAR_MAX
];
1445 char *b
, *stack
= "";
1447 if (bar_enabled
== 0)
1449 if (bar_extra
&& bar_extra_running
) {
1450 /* ignore short reads; it'll correct itself */
1451 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1452 if (b
&& b
[len
- 1] == '\n') {
1454 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1456 if (b
== NULL
&& errno
!= EAGAIN
) {
1457 warn("bar_update: bar_extra failed");
1461 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1463 if (clock_enabled
== 0)
1464 strlcpy(s
, "", sizeof s
);
1467 localtime_r(&tmt
, &tm
);
1468 len
= strftime(s
, sizeof s
, clock_format
, &tm
);
1470 strlcat(s
, " ", sizeof s
);
1473 for (i
= 0; i
< ScreenCount(display
); i
++) {
1475 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1476 strlcpy((char *)cn
, "", sizeof cn
);
1477 strlcpy(ws
, "", sizeof ws
);
1479 bar_urgent((char *)cn
, sizeof cn
);
1480 bar_class_name((char *)cn
, sizeof cn
,
1482 bar_window_name((char *)cn
, sizeof cn
,
1485 snprintf(ws
, sizeof ws
, "<%s>",
1488 stack
= r
->ws
->stacker
;
1490 snprintf(loc
, sizeof loc
,
1491 "%d:%d %s %s %s%s %s %s",
1492 x
++, r
->ws
->idx
+ 1, stack
, ws
, s
, cn
,
1493 bar_ext
, bar_vertext
);
1502 bar_check_opts(void)
1504 if (title_class_enabled
|| title_name_enabled
|| window_name_enabled
)
1515 bar_toggle(struct swm_region
*r
, union arg
*args
)
1517 struct swm_region
*tmpr
;
1518 int i
, sc
= ScreenCount(display
);
1520 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1523 for (i
= 0; i
< sc
; i
++)
1524 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1525 XUnmapWindow(display
, tmpr
->bar_window
);
1527 for (i
= 0; i
< sc
; i
++)
1528 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1529 XMapRaised(display
, tmpr
->bar_window
);
1531 bar_enabled
= !bar_enabled
;
1534 /* must be after stack */
1541 XSetWindowAttributes wa
;
1542 struct swm_region
*r
;
1545 /* do this here because the conf file is in memory */
1546 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1547 /* launch external status app */
1548 bar_extra_running
= 1;
1549 if (pipe(bar_pipe
) == -1)
1550 err(1, "pipe error");
1551 socket_setnonblock(bar_pipe
[0]);
1552 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1553 if (dup2(bar_pipe
[0], 0) == -1)
1555 if (dup2(bar_pipe
[1], 1) == -1)
1557 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1558 err(1, "could not disable SIGPIPE");
1559 switch (bar_pid
= fork()) {
1561 err(1, "cannot fork");
1565 execvp(bar_argv
[0], bar_argv
);
1566 err(1, "%s external app failed", bar_argv
[0]);
1568 default: /* parent */
1574 bzero(&wa
, sizeof wa
);
1575 for (i
= 0; i
< ScreenCount(display
); i
++)
1576 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1578 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1579 wa
.background_pixel
=
1580 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1581 XChangeWindowAttributes(display
, r
->bar_window
,
1582 CWBackPixel
| CWBorderPixel
, &wa
);
1588 bar_setup(struct swm_region
*r
)
1590 char *default_string
;
1591 char **missing_charsets
;
1592 int num_missing_charsets
= 0;
1596 XFreeFontSet(display
, bar_fs
);
1601 DNPRINTF(SWM_D_BAR
, "bar_setup: loading bar_fonts: %s\n", bar_fonts
);
1603 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
1604 &num_missing_charsets
, &default_string
);
1606 if (num_missing_charsets
> 0) {
1607 warnx("Unable to load charset(s):");
1609 for (i
= 0; i
< num_missing_charsets
; ++i
)
1610 warnx("%s", missing_charsets
[i
]);
1612 XFreeStringList(missing_charsets
);
1614 if (strcmp(default_string
, ""))
1615 warnx("Glyphs from those sets will be replaced "
1616 "by '%s'.", default_string
);
1618 warnx("Glyphs from those sets won't be drawn.");
1622 errx(1, "Error creating font set structure.");
1624 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
1626 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
1627 2 * bar_border_width
;
1633 y
= bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1635 r
->bar_window
= XCreateSimpleWindow(display
,
1636 r
->s
->root
, x
, y
, WIDTH(r
) - 2 * bar_border_width
,
1637 bar_height
- 2 * bar_border_width
,
1638 bar_border_width
, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
1639 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1640 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
1642 XMapRaised(display
, r
->bar_window
);
1643 DNPRINTF(SWM_D_BAR
, "bar_setup: bar_window: 0x%lx\n", r
->bar_window
);
1645 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1646 err(1, "could not install bar_signal");
1651 drain_enter_notify(void)
1656 while (XCheckMaskEvent(display
, EnterWindowMask
, &cne
))
1659 DNPRINTF(SWM_D_MISC
, "drain_enter_notify: drained: %d\n", i
);
1663 set_win_state(struct ws_win
*win
, long state
)
1665 long data
[] = {state
, None
};
1667 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%lx\n", win
->id
);
1672 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1673 (unsigned char *)data
, 2);
1680 unsigned char *p
= NULL
;
1683 if (!get_property(w
, astate
, 2L, astate
, &n
, NULL
, &p
))
1686 result
= *((long *)p
);
1692 version(struct swm_region
*r
, union arg
*args
)
1694 bar_version
= !bar_version
;
1696 snprintf(bar_vertext
, sizeof bar_vertext
,
1697 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
1699 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
1704 client_msg(struct ws_win
*win
, Atom a
)
1706 XClientMessageEvent cm
;
1711 bzero(&cm
, sizeof cm
);
1712 cm
.type
= ClientMessage
;
1713 cm
.window
= win
->id
;
1714 cm
.message_type
= aprot
;
1717 cm
.data
.l
[1] = CurrentTime
;
1718 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
1721 /* synthetic response to a ConfigureRequest when not making a change */
1723 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
1730 /* send notification of unchanged state. */
1731 ce
.type
= ConfigureNotify
;
1734 ce
.width
= WIDTH(win
);
1735 ce
.height
= HEIGHT(win
);
1736 ce
.override_redirect
= False
;
1740 ce
.display
= display
;
1742 ce
.window
= win
->id
;
1743 ce
.border_width
= border_width
;
1747 ce
.display
= ev
->display
;
1748 ce
.event
= ev
->window
;
1749 ce
.window
= ev
->window
;
1751 /* make response appear more WM_SIZE_HINTS-compliant */
1753 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%lx,"
1754 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
1755 "%d x %d\n", win
->id
, win
->sh_mask
, SH_MIN_W(win
),
1756 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
1757 SH_INC_W(win
), SH_INC_H(win
));
1761 /* the hint may be set... to 0! */
1762 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
1763 ce
.width
= SH_MIN_W(win
);
1764 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
1765 ce
.height
= SH_MIN_H(win
);
1770 /* may also be advertized as 0 */
1771 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
1772 ce
.width
= SH_MAX_W(win
);
1773 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
1774 ce
.height
= SH_MAX_H(win
);
1777 /* resize increment. */
1779 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
1780 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
1782 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
1783 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
1787 /* adjust x and y for requested border_width. */
1788 ce
.x
+= border_width
- ev
->border_width
;
1789 ce
.y
+= border_width
- ev
->border_width
;
1790 ce
.border_width
= ev
->border_width
;
1791 ce
.above
= ev
->above
;
1794 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
1795 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
1796 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
1798 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
1802 count_win(struct workspace
*ws
, int count_transient
)
1807 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1808 if (count_transient
== 0 && win
->floating
)
1810 if (count_transient
== 0 && win
->transient
)
1816 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
1822 quit(struct swm_region
*r
, union arg
*args
)
1824 DNPRINTF(SWM_D_MISC
, "quit\n");
1829 unmap_window(struct ws_win
*win
)
1834 /* don't unmap again */
1835 if (getstate(win
->id
) == IconicState
)
1838 set_win_state(win
, IconicState
);
1840 XUnmapWindow(display
, win
->id
);
1841 XSetWindowBorder(display
, win
->id
,
1842 win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1851 for (i
= 0; i
< ScreenCount(display
); i
++)
1852 for (j
= 0; j
< workspace_limit
; j
++)
1853 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1858 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
1865 event
.display
= display
; /* Ignored, but what the hell */
1866 event
.window
= win
->id
;
1867 event
.root
= win
->s
->root
;
1868 event
.subwindow
= None
;
1869 event
.time
= CurrentTime
;
1874 event
.same_screen
= True
;
1875 event
.keycode
= XKeysymToKeycode(display
, keysym
);
1876 event
.state
= modifiers
;
1878 event
.type
= KeyPress
;
1879 XSendEvent(event
.display
, event
.window
, True
,
1880 KeyPressMask
, (XEvent
*)&event
);
1882 event
.type
= KeyRelease
;
1883 XSendEvent(event
.display
, event
.window
, True
,
1884 KeyPressMask
, (XEvent
*)&event
);
1889 restart(struct swm_region
*r
, union arg
*args
)
1891 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
1893 /* disable alarm because the following code may not be interrupted */
1895 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
1896 err(1, "can't disable alarm");
1901 XCloseDisplay(display
);
1902 execvp(start_argv
[0], start_argv
);
1903 warn("execvp failed");
1908 root_to_region(Window root
)
1910 struct swm_region
*r
= NULL
;
1912 int i
, x
, y
, wx
, wy
;
1915 for (i
= 0; i
< ScreenCount(display
); i
++)
1916 if (screens
[i
].root
== root
)
1919 if (XQueryPointer(display
, screens
[i
].root
,
1920 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
1921 /* choose a region based on pointer location */
1922 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1923 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
1924 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
1929 r
= TAILQ_FIRST(&screens
[i
].rl
);
1935 find_unmanaged_window(Window id
)
1940 for (i
= 0; i
< ScreenCount(display
); i
++)
1941 for (j
= 0; j
< workspace_limit
; j
++)
1942 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
1950 find_window(Window id
)
1953 Window wrr
, wpr
, *wcr
= NULL
;
1957 for (i
= 0; i
< ScreenCount(display
); i
++)
1958 for (j
= 0; j
< workspace_limit
; j
++)
1959 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1963 /* if we were looking for the parent return that window instead */
1964 if (XQueryTree(display
, id
, &wrr
, &wpr
, &wcr
, &nc
) == 0)
1969 /* ignore not found and root */
1970 if (wpr
== 0 || wrr
== wpr
)
1973 /* look for parent */
1974 for (i
= 0; i
< ScreenCount(display
); i
++)
1975 for (j
= 0; j
< workspace_limit
; j
++)
1976 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1984 spawn(int ws_idx
, union arg
*args
, int close_fd
)
1989 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
1992 close(ConnectionNumber(display
));
1994 setenv("LD_PRELOAD", SWM_LIB
, 1);
1996 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
1997 warn("spawn: asprintf SWM_WS");
2000 setenv("_SWM_WS", ret
, 1);
2004 if (asprintf(&ret
, "%d", getpid()) == -1) {
2005 warn("spawn: asprintf _SWM_PID");
2008 setenv("_SWM_PID", ret
, 1);
2012 if (setsid() == -1) {
2013 warn("spawn: setsid");
2019 * close stdin and stdout to prevent interaction between apps
2020 * and the baraction script
2021 * leave stderr open to record errors
2023 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2024 warn("spawn: open");
2027 dup2(fd
, STDIN_FILENO
);
2028 dup2(fd
, STDOUT_FILENO
);
2033 execvp(args
->argv
[0], args
->argv
);
2035 warn("spawn: execvp");
2040 spawnterm(struct swm_region
*r
, union arg
*args
)
2042 DNPRINTF(SWM_D_MISC
, "spawnterm\n");
2046 setenv("_SWM_XTERM_FONTADJ", "", 1);
2047 spawn(r
->ws
->idx
, args
, 1);
2052 kill_refs(struct ws_win
*win
)
2055 struct swm_region
*r
;
2056 struct workspace
*ws
;
2061 for (i
= 0; i
< ScreenCount(display
); i
++)
2062 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2063 for (x
= 0; x
< workspace_limit
; x
++) {
2065 if (win
== ws
->focus
)
2067 if (win
== ws
->focus_prev
)
2068 ws
->focus_prev
= NULL
;
2073 validate_win(struct ws_win
*testwin
)
2076 struct workspace
*ws
;
2077 struct swm_region
*r
;
2080 if (testwin
== NULL
)
2083 for (i
= 0; i
< ScreenCount(display
); i
++)
2084 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2085 for (x
= 0; x
< workspace_limit
; x
++) {
2087 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2095 validate_ws(struct workspace
*testws
)
2097 struct swm_region
*r
;
2098 struct workspace
*ws
;
2101 /* validate all ws */
2102 for (i
= 0; i
< ScreenCount(display
); i
++)
2103 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2104 for (x
= 0; x
< workspace_limit
; x
++) {
2113 unfocus_win(struct ws_win
*win
)
2118 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%lx\n", WINID(win
));
2122 if (win
->ws
== NULL
)
2125 if (validate_ws(win
->ws
))
2126 return; /* XXX this gets hit with thunderbird, needs fixing */
2128 if (win
->ws
->r
== NULL
)
2131 if (validate_win(win
)) {
2136 if (win
->ws
->focus
== win
) {
2137 win
->ws
->focus
= NULL
;
2138 win
->ws
->focus_prev
= win
;
2141 if (validate_win(win
->ws
->focus
)) {
2142 kill_refs(win
->ws
->focus
);
2143 win
->ws
->focus
= NULL
;
2145 if (validate_win(win
->ws
->focus_prev
)) {
2146 kill_refs(win
->ws
->focus_prev
);
2147 win
->ws
->focus_prev
= NULL
;
2150 /* drain all previous unfocus events */
2151 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
2154 grabbuttons(win
, 0);
2155 XSetWindowBorder(display
, win
->id
,
2156 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2158 XChangeProperty(display
, win
->s
->root
,
2159 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2160 PropModeReplace
, (unsigned char *)&none
,1);
2169 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2171 for (i
= 0; i
< ScreenCount(display
); i
++)
2172 for (j
= 0; j
< workspace_limit
; j
++)
2173 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2178 focus_win(struct ws_win
*win
)
2183 struct ws_win
*cfw
= NULL
;
2186 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%lx\n", WINID(win
));
2190 if (win
->ws
== NULL
)
2193 if (validate_ws(win
->ws
))
2194 return; /* XXX this gets hit with thunderbird, needs fixing */
2196 if (validate_win(win
)) {
2201 if (validate_win(win
)) {
2206 XGetInputFocus(display
, &cur_focus
, &rr
);
2207 if ((cfw
= find_window(cur_focus
)) != NULL
)
2210 /* use larger hammer since the window was killed somehow */
2211 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2212 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2213 XSetWindowBorder(display
, cfw
->id
,
2214 cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2217 win
->ws
->focus
= win
;
2219 if (win
->ws
->r
!= NULL
) {
2220 /* drain all previous focus events */
2221 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
2225 XSetInputFocus(display
, win
->id
,
2226 RevertToParent
, CurrentTime
);
2227 grabbuttons(win
, 1);
2228 XSetWindowBorder(display
, win
->id
,
2229 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2230 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2231 win
->ws
->always_raise
)
2232 XMapRaised(display
, win
->id
);
2234 XChangeProperty(display
, win
->s
->root
,
2235 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2236 PropModeReplace
, (unsigned char *)&win
->id
,1);
2243 switchws(struct swm_region
*r
, union arg
*args
)
2245 int wsid
= args
->id
, unmap_old
= 0;
2246 struct swm_region
*this_r
, *other_r
;
2248 struct workspace
*new_ws
, *old_ws
;
2254 if (wsid
>= workspace_limit
)
2258 old_ws
= this_r
->ws
;
2259 new_ws
= &this_r
->s
->ws
[wsid
];
2261 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2262 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2264 if (new_ws
== NULL
|| old_ws
== NULL
)
2266 if (new_ws
== old_ws
)
2269 other_r
= new_ws
->r
;
2270 if (other_r
== NULL
) {
2271 /* the other workspace is hidden, hide this one */
2275 /* the other ws is visible in another region, exchange them */
2276 other_r
->ws_prior
= new_ws
;
2277 other_r
->ws
= old_ws
;
2278 old_ws
->r
= other_r
;
2280 this_r
->ws_prior
= old_ws
;
2281 this_r
->ws
= new_ws
;
2284 /* this is needed so that we can click on a window after a restart */
2288 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2289 focus(new_ws
->r
, &a
);
2293 /* unmap old windows */
2295 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2298 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2299 drain_enter_notify();
2303 cyclews(struct swm_region
*r
, union arg
*args
)
2306 struct swm_screen
*s
= r
->s
;
2309 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2310 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2315 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2318 case SWM_ARG_ID_CYCLEWS_UP
:
2319 if (a
.id
< workspace_limit
- 1)
2324 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2327 case SWM_ARG_ID_CYCLEWS_DOWN
:
2331 a
.id
= workspace_limit
- 1;
2338 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2340 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2344 } while (a
.id
!= r
->ws
->idx
);
2348 priorws(struct swm_region
*r
, union arg
*args
)
2352 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2353 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2355 if (r
->ws_prior
== NULL
)
2358 a
.id
= r
->ws_prior
->idx
;
2363 cyclescr(struct swm_region
*r
, union arg
*args
)
2365 struct swm_region
*rr
= NULL
;
2369 /* do nothing if we don't have more than one screen */
2370 if (!(ScreenCount(display
) > 1 || outputs
> 1))
2375 case SWM_ARG_ID_CYCLESC_UP
:
2376 rr
= TAILQ_NEXT(r
, entry
);
2378 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2380 case SWM_ARG_ID_CYCLESC_DOWN
:
2381 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2383 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2391 /* move mouse to region */
2393 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2394 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2396 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2399 if (rr
->ws
->focus
) {
2400 /* move to focus window */
2401 x
= X(rr
->ws
->focus
) + 1;
2402 y
= Y(rr
->ws
->focus
) + 1;
2403 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2408 sort_windows(struct ws_win_list
*wl
)
2410 struct ws_win
*win
, *parent
, *nxt
;
2415 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2416 nxt
= TAILQ_NEXT(win
, entry
);
2417 if (win
->transient
) {
2418 parent
= find_window(win
->transient
);
2419 if (parent
== NULL
) {
2420 warnx("not possible bug");
2423 TAILQ_REMOVE(wl
, win
, entry
);
2424 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2431 swapwin(struct swm_region
*r
, union arg
*args
)
2433 struct ws_win
*target
, *source
;
2434 struct ws_win
*cur_focus
;
2435 struct ws_win_list
*wl
;
2438 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2439 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2441 cur_focus
= r
->ws
->focus
;
2442 if (cur_focus
== NULL
)
2446 wl
= &source
->ws
->winlist
;
2449 case SWM_ARG_ID_SWAPPREV
:
2450 if (source
->transient
)
2451 source
= find_window(source
->transient
);
2452 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2453 if (target
&& target
->transient
)
2454 target
= find_window(target
->transient
);
2455 TAILQ_REMOVE(wl
, source
, entry
);
2457 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2459 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2461 case SWM_ARG_ID_SWAPNEXT
:
2462 target
= TAILQ_NEXT(source
, entry
);
2463 /* move the parent and let the sort handle the move */
2464 if (source
->transient
)
2465 source
= find_window(source
->transient
);
2466 TAILQ_REMOVE(wl
, source
, entry
);
2468 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2470 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2472 case SWM_ARG_ID_SWAPMAIN
:
2473 target
= TAILQ_FIRST(wl
);
2474 if (target
== source
) {
2475 if (source
->ws
->focus_prev
!= NULL
&&
2476 source
->ws
->focus_prev
!= target
)
2478 source
= source
->ws
->focus_prev
;
2482 if (target
== NULL
|| source
== NULL
)
2484 source
->ws
->focus_prev
= target
;
2485 TAILQ_REMOVE(wl
, target
, entry
);
2486 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2487 TAILQ_REMOVE(wl
, source
, entry
);
2488 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2490 case SWM_ARG_ID_MOVELAST
:
2491 TAILQ_REMOVE(wl
, source
, entry
);
2492 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2495 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2505 focus_prev(struct ws_win
*win
)
2507 struct ws_win
*winfocus
= NULL
;
2508 struct ws_win
*cur_focus
= NULL
;
2509 struct ws_win_list
*wl
= NULL
;
2510 struct workspace
*ws
= NULL
;
2512 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%lx\n", WINID(win
));
2514 if (!(win
&& win
->ws
))
2519 cur_focus
= ws
->focus
;
2521 /* pickle, just focus on whatever */
2522 if (cur_focus
== NULL
) {
2523 /* use prev_focus if valid */
2524 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2525 find_window(WINID(ws
->focus_prev
)))
2526 winfocus
= ws
->focus_prev
;
2527 if (winfocus
== NULL
)
2528 winfocus
= TAILQ_FIRST(wl
);
2532 /* if transient focus on parent */
2533 if (cur_focus
->transient
) {
2534 winfocus
= find_window(cur_focus
->transient
);
2538 /* if in max_stack try harder */
2539 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2540 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2541 if (cur_focus
!= ws
->focus_prev
)
2542 winfocus
= ws
->focus_prev
;
2544 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2549 if (cur_focus
== win
)
2550 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2551 if (winfocus
== NULL
)
2552 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2553 if (winfocus
== NULL
|| winfocus
== win
)
2554 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2557 focus_magic(winfocus
);
2561 focus(struct swm_region
*r
, union arg
*args
)
2563 struct ws_win
*winfocus
= NULL
, *head
;
2564 struct ws_win
*cur_focus
= NULL
;
2565 struct ws_win_list
*wl
= NULL
;
2566 struct workspace
*ws
= NULL
;
2572 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2574 /* treat FOCUS_CUR special */
2575 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2576 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2577 winfocus
= r
->ws
->focus
;
2578 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2579 winfocus
= r
->ws
->focus_prev
;
2581 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2582 if (winfocus
->iconic
== 0)
2585 focus_magic(winfocus
);
2589 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2593 if (TAILQ_EMPTY(wl
))
2595 /* make sure there is at least one uniconified window */
2597 TAILQ_FOREACH(winfocus
, wl
, entry
)
2598 if (winfocus
->iconic
== 0) {
2606 case SWM_ARG_ID_FOCUSPREV
:
2607 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2609 head
= TAILQ_LAST(wl
, ws_win_list
);
2611 if (WINID(winfocus
) == cur_focus
->transient
) {
2612 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2614 head
= TAILQ_LAST(wl
, ws_win_list
);
2619 if (winfocus
&& winfocus
->iconic
) {
2620 while (winfocus
!= cur_focus
) {
2621 if (winfocus
== NULL
)
2622 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2623 if (winfocus
->iconic
== 0)
2625 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
2631 case SWM_ARG_ID_FOCUSNEXT
:
2632 head
= TAILQ_NEXT(cur_focus
, entry
);
2634 head
= TAILQ_FIRST(wl
);
2638 if (winfocus
&& winfocus
->iconic
) {
2639 while (winfocus
!= cur_focus
) {
2640 if (winfocus
== NULL
)
2641 winfocus
= TAILQ_FIRST(wl
);
2642 if (winfocus
->iconic
== 0)
2644 winfocus
= TAILQ_NEXT(winfocus
, entry
);
2649 case SWM_ARG_ID_FOCUSMAIN
:
2650 winfocus
= TAILQ_FIRST(wl
);
2651 if (winfocus
== cur_focus
)
2652 winfocus
= cur_focus
->ws
->focus_prev
;
2659 focus_magic(winfocus
);
2663 cycle_layout(struct swm_region
*r
, union arg
*args
)
2665 struct workspace
*ws
= r
->ws
;
2668 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2671 if (ws
->cur_layout
->l_stack
== NULL
)
2672 ws
->cur_layout
= &layouts
[0];
2675 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2676 drain_enter_notify();
2677 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2683 stack_config(struct swm_region
*r
, union arg
*args
)
2685 struct workspace
*ws
= r
->ws
;
2687 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
2690 if (ws
->cur_layout
->l_config
!= NULL
)
2691 ws
->cur_layout
->l_config(ws
, args
->id
);
2693 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
2700 struct swm_geometry g
;
2701 struct swm_region
*r
;
2707 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
2709 for (i
= 0; i
< ScreenCount(display
); i
++) {
2713 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2714 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
2715 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
2717 /* start with screen geometry, adjust for bar */
2719 g
.w
-= 2 * border_width
;
2720 g
.h
-= 2 * border_width
;
2726 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
2727 r
->ws
->cur_layout
->l_string(r
->ws
);
2728 /* save r so we can track region changes */
2735 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2736 drain_enter_notify();
2738 DNPRINTF(SWM_D_STACK
, "stack: end\n");
2742 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
2744 /* retain window geom and region geom */
2745 win
->g_float
= win
->g
;
2746 win
->rg_float
= r
->g
;
2747 win
->g_floatvalid
= 1;
2751 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
2759 bzero(&wc
, sizeof wc
);
2760 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
2763 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2764 * geom on ws switches or return from max mode
2766 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&& win
->g_floatvalid
2767 && !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
2769 * use stored g and rg to set relative position and size
2770 * as in old region or before max stack mode
2772 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ X(r
);
2773 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ Y(r
);
2774 WIDTH(win
) = win
->g_float
.w
;
2775 HEIGHT(win
) = win
->g_float
.h
;
2776 win
->g_floatvalid
= 0;
2779 win
->floatmaxed
= 0;
2781 if ((win
->quirks
& SWM_Q_FULLSCREEN
) && (WIDTH(win
) >= WIDTH(r
)) &&
2782 (HEIGHT(win
) >= HEIGHT(r
)))
2783 wc
.border_width
= 0;
2785 wc
.border_width
= border_width
;
2786 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
2787 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
2788 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
2793 * floaters and transients are auto-centred unless moved
2796 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - wc
.border_width
;
2797 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - wc
.border_width
;
2800 /* win can be outside r if new r smaller than old r */
2801 /* Ensure top left corner inside r (move probs otherwise) */
2802 if (X(win
) < X(r
) - wc
.border_width
)
2803 X(win
) = X(r
) - wc
.border_width
;
2804 if (X(win
) > X(r
) + WIDTH(r
) - 1)
2805 X(win
) = (WIDTH(win
) > WIDTH(r
)) ? X(r
) :
2806 (X(r
) + WIDTH(r
) - WIDTH(win
) - 2 * wc
.border_width
);
2807 if (Y(win
) < Y(r
) - wc
.border_width
)
2808 Y(win
) = Y(r
) - wc
.border_width
;
2809 if (Y(win
) > Y(r
) + HEIGHT(r
) - 1)
2810 Y(win
) = (HEIGHT(win
) > HEIGHT(r
)) ? Y(r
) :
2811 (Y(r
) + HEIGHT(r
) - HEIGHT(win
) - 2 * wc
.border_width
);
2815 wc
.width
= WIDTH(win
);
2816 wc
.height
= HEIGHT(win
);
2819 * Retain floater and transient geometry for correct positioning
2820 * when ws changes region
2822 if (!(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
2823 store_float_geom(win
, r
);
2825 DNPRINTF(SWM_D_MISC
, "stack_floater: window: %lu, (x,y) w x h: (%d,%d) "
2826 "%d x %d\n", win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
2828 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2832 * Send keystrokes to terminal to decrease/increase the font size as the
2833 * window size changes.
2836 adjust_font(struct ws_win
*win
)
2838 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
2839 win
->floating
|| win
->transient
)
2842 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
2843 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
2844 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
2845 win
->font_size_boundary
[win
->font_steps
] =
2846 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
2849 win
->last_inc
= win
->sh
.width_inc
;
2850 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
2851 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
2852 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
2855 win
->last_inc
= win
->sh
.width_inc
;
2856 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
2860 #define SWAPXY(g) do { \
2862 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2863 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2866 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
2869 XWindowAttributes wa
;
2870 struct swm_geometry win_g
, r_g
= *g
;
2871 struct ws_win
*win
, *fs_win
= 0;
2872 int i
, j
, s
, stacks
;
2873 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
2874 int hrh
, extra
= 0, h_slice
, last_h
= 0;
2875 int split
, colno
, winno
, mwin
, msize
, mscale
;
2876 int remain
, missing
, v_slice
, reconfigure
;
2879 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
2880 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
2882 winno
= count_win(ws
, 0);
2883 if (winno
== 0 && count_win(ws
, 1) == 0)
2886 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2887 if (win
->transient
== 0 && win
->floating
== 0
2888 && win
->iconic
== 0)
2895 w_inc
= win
->sh
.width_inc
;
2896 w_base
= win
->sh
.base_width
;
2897 mwin
= ws
->l_state
.horizontal_mwin
;
2898 mscale
= ws
->l_state
.horizontal_msize
;
2899 stacks
= ws
->l_state
.horizontal_stacks
;
2902 w_inc
= win
->sh
.height_inc
;
2903 w_base
= win
->sh
.base_height
;
2904 mwin
= ws
->l_state
.vertical_mwin
;
2905 mscale
= ws
->l_state
.vertical_msize
;
2906 stacks
= ws
->l_state
.vertical_stacks
;
2910 if (stacks
> winno
- mwin
)
2911 stacks
= winno
- mwin
;
2915 h_slice
= r_g
.h
/ SWM_H_SLICE
;
2916 if (mwin
&& winno
> mwin
) {
2917 v_slice
= r_g
.w
/ SWM_V_SLICE
;
2921 win_g
.w
= v_slice
* mscale
;
2923 if (w_inc
> 1 && w_inc
< v_slice
) {
2924 /* adjust for window's requested size increment */
2925 remain
= (win_g
.w
- w_base
) % w_inc
;
2931 win_g
.x
+= r_g
.w
- msize
;
2934 colno
= split
= winno
/ stacks
;
2935 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
2936 2 * border_width
) / stacks
);
2938 hrh
= r_g
.h
/ colno
;
2939 extra
= r_g
.h
- (colno
* hrh
);
2940 win_g
.h
= hrh
- 2 * border_width
;
2942 /* stack all the tiled windows */
2943 i
= j
= 0, s
= stacks
;
2944 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2945 if (win
->transient
!= 0 || win
->floating
!= 0)
2947 if (win
->iconic
!= 0)
2950 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
2955 if (split
&& i
== split
) {
2956 colno
= (winno
- mwin
) / stacks
;
2957 if (s
<= (winno
- mwin
) % stacks
)
2959 split
= split
+ colno
;
2960 hrh
= (r_g
.h
/ colno
);
2961 extra
= r_g
.h
- (colno
* hrh
);
2965 win_g
.x
+= win_g
.w
+ 2 * border_width
;
2966 win_g
.w
= (r_g
.w
- msize
-
2967 (stacks
* 2 * border_width
)) / stacks
;
2969 win_g
.w
+= (r_g
.w
- msize
-
2970 (stacks
* 2 * border_width
)) % stacks
;
2974 win_g
.h
= hrh
- 2 * border_width
;
2976 h_inc
= win
->sh
.width_inc
;
2977 h_base
= win
->sh
.base_width
;
2979 h_inc
= win
->sh
.height_inc
;
2980 h_base
= win
->sh
.base_height
;
2982 if (j
== colno
- 1) {
2983 win_g
.h
= hrh
+ extra
;
2984 } else if (h_inc
> 1 && h_inc
< h_slice
) {
2985 /* adjust for window's requested size increment */
2986 remain
= (win_g
.h
- h_base
) % h_inc
;
2987 missing
= h_inc
- remain
;
2989 if (missing
<= extra
|| j
== 0) {
3001 win_g
.y
+= last_h
+ 2 * border_width
;
3003 bzero(&wc
, sizeof wc
);
3004 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3005 wc
.border_width
= 0;
3006 win_g
.w
+= 2 * border_width
;
3007 win_g
.h
+= 2 * border_width
;
3009 wc
.border_width
= border_width
;
3012 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3013 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3015 X(win
) = wc
.x
= win_g
.y
;
3016 Y(win
) = wc
.y
= win_g
.x
;
3017 WIDTH(win
) = wc
.width
= win_g
.h
;
3018 HEIGHT(win
) = wc
.height
= win_g
.w
;
3021 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3022 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3024 X(win
) = wc
.x
= win_g
.x
;
3025 Y(win
) = wc
.y
= win_g
.y
;
3026 WIDTH(win
) = wc
.width
= win_g
.w
;
3027 HEIGHT(win
) = wc
.height
= win_g
.h
;
3032 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
3033 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3036 if (XGetWindowAttributes(display
, win
->id
, &wa
))
3037 if (wa
.map_state
== IsUnmapped
)
3038 XMapRaised(display
, win
->id
);
3046 /* now, stack all the floaters and transients */
3047 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3048 if (win
->transient
== 0 && win
->floating
== 0)
3050 if (win
->iconic
== 1)
3052 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3057 stack_floater(win
, ws
->r
);
3058 XMapRaised(display
, win
->id
);
3062 stack_floater(fs_win
, ws
->r
);
3063 XMapRaised(display
, fs_win
->id
);
3068 vertical_config(struct workspace
*ws
, int id
)
3070 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3074 case SWM_ARG_ID_STACKRESET
:
3075 case SWM_ARG_ID_STACKINIT
:
3076 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3077 ws
->l_state
.vertical_mwin
= 1;
3078 ws
->l_state
.vertical_stacks
= 1;
3080 case SWM_ARG_ID_MASTERSHRINK
:
3081 if (ws
->l_state
.vertical_msize
> 1)
3082 ws
->l_state
.vertical_msize
--;
3084 case SWM_ARG_ID_MASTERGROW
:
3085 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3086 ws
->l_state
.vertical_msize
++;
3088 case SWM_ARG_ID_MASTERADD
:
3089 ws
->l_state
.vertical_mwin
++;
3091 case SWM_ARG_ID_MASTERDEL
:
3092 if (ws
->l_state
.vertical_mwin
> 0)
3093 ws
->l_state
.vertical_mwin
--;
3095 case SWM_ARG_ID_STACKINC
:
3096 ws
->l_state
.vertical_stacks
++;
3098 case SWM_ARG_ID_STACKDEC
:
3099 if (ws
->l_state
.vertical_stacks
> 1)
3100 ws
->l_state
.vertical_stacks
--;
3102 case SWM_ARG_ID_FLIPLAYOUT
:
3103 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3111 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3113 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3115 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3119 horizontal_config(struct workspace
*ws
, int id
)
3121 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3124 case SWM_ARG_ID_STACKRESET
:
3125 case SWM_ARG_ID_STACKINIT
:
3126 ws
->l_state
.horizontal_mwin
= 1;
3127 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3128 ws
->l_state
.horizontal_stacks
= 1;
3130 case SWM_ARG_ID_MASTERSHRINK
:
3131 if (ws
->l_state
.horizontal_msize
> 1)
3132 ws
->l_state
.horizontal_msize
--;
3134 case SWM_ARG_ID_MASTERGROW
:
3135 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3136 ws
->l_state
.horizontal_msize
++;
3138 case SWM_ARG_ID_MASTERADD
:
3139 ws
->l_state
.horizontal_mwin
++;
3141 case SWM_ARG_ID_MASTERDEL
:
3142 if (ws
->l_state
.horizontal_mwin
> 0)
3143 ws
->l_state
.horizontal_mwin
--;
3145 case SWM_ARG_ID_STACKINC
:
3146 ws
->l_state
.horizontal_stacks
++;
3148 case SWM_ARG_ID_STACKDEC
:
3149 if (ws
->l_state
.horizontal_stacks
> 1)
3150 ws
->l_state
.horizontal_stacks
--;
3152 case SWM_ARG_ID_FLIPLAYOUT
:
3153 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3161 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3163 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3165 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3168 /* fullscreen view */
3170 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3173 struct swm_geometry gg
= *g
;
3174 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3178 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3183 winno
= count_win(ws
, 0);
3184 if (winno
== 0 && count_win(ws
, 1) == 0)
3187 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3188 if (win
->transient
) {
3190 parent
= find_window(win
->transient
);
3194 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3196 * retain geometry for retrieval on exit from
3199 store_float_geom(win
, ws
->r
);
3200 win
->floatmaxed
= 1;
3203 /* only reconfigure if necessary */
3204 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3205 HEIGHT(win
) != gg
.h
) {
3206 bzero(&wc
, sizeof wc
);
3207 X(win
) = wc
.x
= gg
.x
;
3208 Y(win
) = wc
.y
= gg
.y
;
3210 wc
.border_width
= border_width
;
3211 WIDTH(win
) = wc
.width
= gg
.w
;
3212 HEIGHT(win
) = wc
.height
= gg
.h
;
3214 wc
.border_width
= 0;
3215 WIDTH(win
) = wc
.width
= gg
.w
+ 2 * border_width
;
3216 HEIGHT(win
) = wc
.height
= gg
.h
+
3219 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
3220 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3222 /* unmap only if we don't have multi screen */
3223 if (win
!= ws
->focus
)
3224 if (!(ScreenCount(display
) > 1 || outputs
> 1))
3228 /* put the last transient on top */
3231 XMapRaised(display
, parent
->id
);
3232 stack_floater(wintrans
, ws
->r
);
3233 focus_magic(wintrans
);
3238 send_to_ws(struct swm_region
*r
, union arg
*args
)
3240 int wsid
= args
->id
;
3241 struct ws_win
*win
= NULL
, *parent
;
3242 struct workspace
*ws
, *nws
;
3243 Atom ws_idx_atom
= 0;
3244 unsigned char ws_idx_str
[SWM_PROPLEN
];
3247 if (wsid
>= workspace_limit
)
3250 if (r
&& r
->ws
&& r
->ws
->focus
)
3256 if (win
->ws
->idx
== wsid
)
3259 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%lx\n", win
->id
);
3262 nws
= &win
->s
->ws
[wsid
];
3264 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3266 if (win
->transient
) {
3267 parent
= find_window(win
->transient
);
3269 unmap_window(parent
);
3270 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3271 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3276 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3277 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3278 if (TAILQ_EMPTY(&ws
->winlist
))
3279 r
->ws
->focus
= NULL
;
3282 /* Try to update the window's workspace property */
3283 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
3285 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3287 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3289 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
3290 PropModeReplace
, ws_idx_str
, strlen((char *)ws_idx_str
));
3297 pressbutton(struct swm_region
*r
, union arg
*args
)
3299 XTestFakeButtonEvent(display
, args
->id
, True
, CurrentTime
);
3300 XTestFakeButtonEvent(display
, args
->id
, False
, CurrentTime
);
3304 raise_toggle(struct swm_region
*r
, union arg
*args
)
3306 if (r
== NULL
|| r
->ws
== NULL
)
3309 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3311 /* bring floaters back to top */
3312 if (r
->ws
->always_raise
== 0)
3317 iconify(struct swm_region
*r
, union arg
*args
)
3321 if (r
->ws
->focus
== NULL
)
3323 unmap_window(r
->ws
->focus
);
3324 update_iconic(r
->ws
->focus
, 1);
3326 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3327 drain_enter_notify();
3328 r
->ws
->focus
= NULL
;
3329 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3334 get_win_name(Window win
)
3336 unsigned char *prop
= NULL
;
3337 unsigned long nbytes
, nitems
;
3339 /* try _NET_WM_NAME first */
3340 if (get_property(win
, a_netwmname
, 0L, a_utf8_string
, NULL
, &nbytes
,
3343 if (get_property(win
, a_netwmname
, nbytes
, a_utf8_string
,
3344 &nitems
, NULL
, &prop
))
3348 /* fallback to WM_NAME */
3349 if (!get_property(win
, a_wmname
, 0L, a_string
, NULL
, &nbytes
, &prop
))
3352 if (get_property(win
, a_wmname
, nbytes
, a_string
, &nitems
, NULL
, &prop
))
3359 uniconify(struct swm_region
*r
, union arg
*args
)
3363 unsigned char *name
;
3366 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3368 if (r
== NULL
|| r
->ws
== NULL
)
3371 /* make sure we have anything to uniconify */
3372 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3373 if (win
->ws
== NULL
)
3374 continue; /* should never happen */
3375 if (win
->iconic
== 0)
3383 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3385 spawn_select(r
, args
, "search", &searchpid
);
3387 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3390 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3391 if (win
->ws
== NULL
)
3392 continue; /* should never happen */
3393 if (win
->iconic
== 0)
3396 name
= get_win_name(win
->id
);
3399 fprintf(lfile
, "%s.%lu\n", name
, win
->id
);
3407 name_workspace(struct swm_region
*r
, union arg
*args
)
3411 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3417 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3419 spawn_select(r
, args
, "name_workspace", &searchpid
);
3421 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3424 fprintf(lfile
, "%s", "");
3429 search_workspace(struct swm_region
*r
, union arg
*args
)
3432 struct workspace
*ws
;
3435 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3441 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3443 spawn_select(r
, args
, "search", &searchpid
);
3445 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3448 for (i
= 0; i
< workspace_limit
; i
++) {
3452 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3453 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3460 search_win_cleanup(void)
3462 struct search_window
*sw
= NULL
;
3464 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3465 XDestroyWindow(display
, sw
->indicator
);
3466 XFreeGC(display
, sw
->gc
);
3467 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3473 search_win(struct swm_region
*r
, union arg
*args
)
3475 struct ws_win
*win
= NULL
;
3476 struct search_window
*sw
= NULL
;
3483 XRectangle ibox
, lbox
;
3485 DNPRINTF(SWM_D_MISC
, "search_win\n");
3488 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3490 spawn_select(r
, args
, "search", &searchpid
);
3492 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3495 TAILQ_INIT(&search_wl
);
3498 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3499 if (win
->iconic
== 1)
3502 sw
= calloc(1, sizeof(struct search_window
));
3504 warn("search_win: calloc");
3506 search_win_cleanup();
3512 snprintf(s
, sizeof s
, "%d", i
);
3515 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
3517 w
= XCreateSimpleWindow(display
,
3518 win
->id
, 0, 0,lbox
.width
+ 4,
3519 bar_fs_extents
->max_logical_extent
.height
, 1,
3520 r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
,
3521 r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
3524 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3526 sw
->gc
= XCreateGC(display
, w
, 0, &gcv
);
3527 XMapRaised(display
, w
);
3528 XSetForeground(display
, sw
->gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
3530 DRAWSTRING(display
, w
, bar_fs
, sw
->gc
, 2,
3531 (bar_fs_extents
->max_logical_extent
.height
-
3532 lbox
.height
) / 2 - lbox
.y
, s
, len
);
3534 fprintf(lfile
, "%d\n", i
);
3542 search_resp_uniconify(char *resp
, unsigned long len
)
3544 unsigned char *name
;
3548 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3550 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3551 if (win
->iconic
== 0)
3553 name
= get_win_name(win
->id
);
3556 if (asprintf(&s
, "%s.%lu", name
, win
->id
) == -1) {
3561 if (strncmp(s
, resp
, len
) == 0) {
3562 /* XXX this should be a callback to generalize */
3563 update_iconic(win
, 0);
3572 search_resp_name_workspace(char *resp
, unsigned long len
)
3574 struct workspace
*ws
;
3576 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3578 if (search_r
->ws
== NULL
)
3583 free(search_r
->ws
->name
);
3584 search_r
->ws
->name
= NULL
;
3588 ws
->name
= strdup(resp
);
3589 if (ws
->name
== NULL
) {
3590 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
3591 "strdup: %s", strerror(errno
));
3598 search_resp_search_workspace(char *resp
, unsigned long len
)
3605 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
3609 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
3616 ws_idx
= strtonum(q
, 1, workspace_limit
, &errstr
);
3618 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
3625 switchws(search_r
, &a
);
3629 search_resp_search_window(char *resp
, unsigned long len
)
3634 struct search_window
*sw
;
3636 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
3640 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
3645 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
3647 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
3654 TAILQ_FOREACH(sw
, &search_wl
, entry
)
3655 if (idx
== sw
->idx
) {
3661 #define MAX_RESP_LEN 1024
3664 search_do_resp(void)
3670 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
3675 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
3676 warn("search: calloc");
3680 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
3682 warn("search: read error");
3685 resp
[rbytes
] = '\0';
3688 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3689 * newline, so work around that by sanitizing the resp now.
3691 resp
[strcspn(resp
, "\n")] = '\0';
3694 switch (search_resp_action
) {
3695 case SWM_SEARCH_UNICONIFY
:
3696 search_resp_uniconify(resp
, len
);
3698 case SWM_SEARCH_NAME_WORKSPACE
:
3699 search_resp_name_workspace(resp
, len
);
3701 case SWM_SEARCH_SEARCH_WORKSPACE
:
3702 search_resp_search_workspace(resp
, len
);
3704 case SWM_SEARCH_SEARCH_WINDOW
:
3705 search_resp_search_window(resp
, len
);
3710 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
3711 search_win_cleanup();
3713 search_resp_action
= SWM_SEARCH_NONE
;
3714 close(select_resp_pipe
[0]);
3719 wkill(struct swm_region
*r
, union arg
*args
)
3721 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
3723 if (r
->ws
->focus
== NULL
)
3726 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
3727 XKillClient(display
, r
->ws
->focus
->id
);
3729 if (r
->ws
->focus
->can_delete
)
3730 client_msg(r
->ws
->focus
, adelete
);
3735 floating_toggle_win(struct ws_win
*win
)
3737 struct swm_region
*r
;
3747 /* reject floating toggles in max stack mode */
3748 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3751 if (win
->floating
) {
3752 if (!win
->floatmaxed
) {
3753 /* retain position for refloat */
3754 store_float_geom(win
, r
);
3758 if (win
->g_floatvalid
) {
3759 /* refloat at last floating relative position */
3760 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ X(r
);
3761 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ Y(r
);
3762 WIDTH(win
) = win
->g_float
.w
;
3763 HEIGHT(win
) = win
->g_float
.h
;
3768 ewmh_update_actions(win
);
3774 floating_toggle(struct swm_region
*r
, union arg
*args
)
3776 struct ws_win
*win
= r
->ws
->focus
;
3782 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
3783 _NET_WM_STATE_TOGGLE
);
3786 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3787 drain_enter_notify();
3789 if (win
== win
->ws
->focus
) {
3790 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3791 focus(win
->ws
->r
, &a
);
3796 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
3798 if (X(win
) + WIDTH(win
) > X(r
) + WIDTH(r
) - border_width
) {
3800 WIDTH(win
) = X(r
) + WIDTH(r
) - X(win
) - border_width
;
3802 X(win
) = X(r
) + WIDTH(r
) - WIDTH(win
) - border_width
;
3805 if (X(win
) < X(r
) - border_width
) {
3807 WIDTH(win
) -= X(r
) - X(win
) - border_width
;
3809 X(win
) = X(r
) - border_width
;
3812 if (Y(win
) + HEIGHT(win
) > Y(r
) + HEIGHT(r
) - border_width
) {
3814 HEIGHT(win
) = Y(r
) + HEIGHT(r
) - Y(win
) - border_width
;
3816 Y(win
) = Y(r
) + HEIGHT(r
) - HEIGHT(win
) - border_width
;
3819 if (Y(win
) < Y(r
) - border_width
) {
3821 HEIGHT(win
) -= Y(r
) - Y(win
) - border_width
;
3823 Y(win
) = Y(r
) - border_width
;
3828 if (HEIGHT(win
) < 1)
3833 update_window(struct ws_win
*win
)
3838 bzero(&wc
, sizeof wc
);
3839 mask
= CWBorderWidth
| CWWidth
| CWHeight
| CWX
| CWY
;
3840 wc
.border_width
= border_width
;
3843 wc
.width
= WIDTH(win
);
3844 wc
.height
= HEIGHT(win
);
3846 DNPRINTF(SWM_D_MISC
, "update_window: window: 0x%lx, (x,y) w x h: "
3847 "(%d,%d) %d x %d\n", win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
3849 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3852 #define SWM_RESIZE_STEPS (50)
3855 resize(struct ws_win
*win
, union arg
*args
)
3859 struct swm_region
*r
= NULL
;
3860 int resize_step
= 0;
3864 struct swm_geometry g
;
3865 int top
= 0, left
= 0;
3868 unsigned int shape
; /* cursor style */
3874 DNPRINTF(SWM_D_MOUSE
, "resize: window: 0x%lx, floating: %s, "
3875 "transient: 0x%lx\n", win
->id
, YESNO(win
->floating
),
3878 if (!(win
->transient
!= 0 || win
->floating
!= 0))
3881 /* reject resizes in max mode for floaters (transient ok) */
3882 if (win
->floatmaxed
)
3886 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
3892 case SWM_ARG_ID_WIDTHSHRINK
:
3893 WIDTH(win
) -= SWM_RESIZE_STEPS
;
3896 case SWM_ARG_ID_WIDTHGROW
:
3897 WIDTH(win
) += SWM_RESIZE_STEPS
;
3900 case SWM_ARG_ID_HEIGHTSHRINK
:
3901 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
3904 case SWM_ARG_ID_HEIGHTGROW
:
3905 HEIGHT(win
) += SWM_RESIZE_STEPS
;
3912 constrain_window(win
, r
, 1);
3914 store_float_geom(win
,r
);
3918 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3919 drain_enter_notify();
3921 /* get cursor offset from window root */
3922 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
3927 if (wx
< WIDTH(win
) / 2)
3930 if (wy
< HEIGHT(win
) / 2)
3933 if (args
->id
== SWM_ARG_ID_CENTER
)
3936 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
3938 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
3940 cursor
= XCreateFontCursor(display
, shape
);
3942 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
3943 GrabModeAsync
, None
, cursor
, CurrentTime
) != GrabSuccess
) {
3944 XFreeCursor(display
, cursor
);
3949 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
3950 SubstructureRedirectMask
, &ev
);
3952 case ConfigureRequest
:
3955 handler
[ev
.type
](&ev
);
3958 /* cursor offset/delta from start of the operation */
3959 dx
= ev
.xmotion
.x_root
- x
;
3960 dy
= ev
.xmotion
.y_root
- y
;
3966 if (args
->id
== SWM_ARG_ID_CENTER
) {
3967 if (g
.h
/ 2 + dy
< 1)
3971 HEIGHT(win
) = g
.h
+ 2 * dy
;
3979 HEIGHT(win
) = g
.h
+ dy
;
3986 if (args
->id
== SWM_ARG_ID_CENTER
) {
3987 if (g
.w
/ 2 + dx
< 1)
3991 WIDTH(win
) = g
.w
+ 2 * dx
;
3999 WIDTH(win
) = g
.w
+ dx
;
4002 constrain_window(win
, r
, 1);
4004 /* not free, don't sync more than 120 times / second */
4005 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4006 time
= ev
.xmotion
.time
;
4007 XSync(display
, False
);
4012 } while (ev
.type
!= ButtonRelease
);
4014 XSync(display
, False
);
4017 store_float_geom(win
,r
);
4019 XUngrabPointer(display
, CurrentTime
);
4020 XFreeCursor(display
, cursor
);
4023 drain_enter_notify();
4027 resize_step(struct swm_region
*r
, union arg
*args
)
4029 struct ws_win
*win
= NULL
;
4031 if (r
&& r
->ws
&& r
->ws
->focus
)
4039 #define SWM_MOVE_STEPS (50)
4042 move(struct ws_win
*win
, union arg
*args
)
4047 struct swm_region
*r
= NULL
;
4057 DNPRINTF(SWM_D_MOUSE
, "move: window: 0x%lx, floating: %s, transient: "
4058 "0x%lx\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4060 /* in max_stack mode should only move transients */
4061 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4065 if (win
->floating
== 0 && !win
->transient
) {
4066 store_float_geom(win
,r
);
4067 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4070 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4077 case SWM_ARG_ID_MOVELEFT
:
4078 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4081 case SWM_ARG_ID_MOVERIGHT
:
4082 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4085 case SWM_ARG_ID_MOVEUP
:
4086 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4089 case SWM_ARG_ID_MOVEDOWN
:
4090 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4097 constrain_window(win
, r
, 0);
4099 store_float_geom(win
, r
);
4103 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4104 GrabModeAsync
, None
, XCreateFontCursor(display
, XC_fleur
),
4105 CurrentTime
) != GrabSuccess
)
4108 /* get cursor offset from window root */
4109 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4113 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4114 SubstructureRedirectMask
, &ev
);
4116 case ConfigureRequest
:
4119 handler
[ev
.type
](&ev
);
4122 X(win
) = ev
.xmotion
.x_root
- wx
- border_width
;
4123 Y(win
) = ev
.xmotion
.y_root
- wy
- border_width
;
4125 constrain_window(win
, r
, 0);
4127 /* not free, don't sync more than 120 times / second */
4128 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4129 time
= ev
.xmotion
.time
;
4130 XSync(display
, False
);
4135 } while (ev
.type
!= ButtonRelease
);
4137 XSync(display
, False
);
4140 store_float_geom(win
,r
);
4141 XUngrabPointer(display
, CurrentTime
);
4144 drain_enter_notify();
4148 move_step(struct swm_region
*r
, union arg
*args
)
4150 struct ws_win
*win
= NULL
;
4152 if (r
&& r
->ws
&& r
->ws
->focus
)
4157 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4164 /* user/key callable function IDs */
4259 kf_search_workspace
,
4261 kf_dumpwins
, /* MUST BE LAST */
4265 /* key definitions */
4267 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4272 legacyfunc(struct swm_region
*r
, union arg
*args
)
4277 char name
[SWM_FUNCNAME_LEN
];
4278 void (*func
)(struct swm_region
*r
, union arg
*);
4280 } keyfuncs
[kf_invalid
+ 1] = {
4281 /* name function argument */
4282 { "cycle_layout", cycle_layout
, {0} },
4283 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4284 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4285 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4286 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4287 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4288 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4289 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4290 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4291 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4292 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4293 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4294 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4295 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4296 { "spawn_term", spawnterm
, {.argv
= spawn_term
} },
4297 { "spawn_menu", legacyfunc
, {0} },
4298 { "quit", quit
, {0} },
4299 { "restart", restart
, {0} },
4300 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4301 { "ws_1", switchws
, {.id
= 0} },
4302 { "ws_2", switchws
, {.id
= 1} },
4303 { "ws_3", switchws
, {.id
= 2} },
4304 { "ws_4", switchws
, {.id
= 3} },
4305 { "ws_5", switchws
, {.id
= 4} },
4306 { "ws_6", switchws
, {.id
= 5} },
4307 { "ws_7", switchws
, {.id
= 6} },
4308 { "ws_8", switchws
, {.id
= 7} },
4309 { "ws_9", switchws
, {.id
= 8} },
4310 { "ws_10", switchws
, {.id
= 9} },
4311 { "ws_11", switchws
, {.id
= 10} },
4312 { "ws_12", switchws
, {.id
= 11} },
4313 { "ws_13", switchws
, {.id
= 12} },
4314 { "ws_14", switchws
, {.id
= 13} },
4315 { "ws_15", switchws
, {.id
= 14} },
4316 { "ws_16", switchws
, {.id
= 15} },
4317 { "ws_17", switchws
, {.id
= 16} },
4318 { "ws_18", switchws
, {.id
= 17} },
4319 { "ws_19", switchws
, {.id
= 18} },
4320 { "ws_20", switchws
, {.id
= 19} },
4321 { "ws_21", switchws
, {.id
= 20} },
4322 { "ws_22", switchws
, {.id
= 21} },
4323 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4324 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4325 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4326 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4327 { "ws_prior", priorws
, {0} },
4328 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4329 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4330 { "mvws_1", send_to_ws
, {.id
= 0} },
4331 { "mvws_2", send_to_ws
, {.id
= 1} },
4332 { "mvws_3", send_to_ws
, {.id
= 2} },
4333 { "mvws_4", send_to_ws
, {.id
= 3} },
4334 { "mvws_5", send_to_ws
, {.id
= 4} },
4335 { "mvws_6", send_to_ws
, {.id
= 5} },
4336 { "mvws_7", send_to_ws
, {.id
= 6} },
4337 { "mvws_8", send_to_ws
, {.id
= 7} },
4338 { "mvws_9", send_to_ws
, {.id
= 8} },
4339 { "mvws_10", send_to_ws
, {.id
= 9} },
4340 { "mvws_11", send_to_ws
, {.id
= 10} },
4341 { "mvws_12", send_to_ws
, {.id
= 11} },
4342 { "mvws_13", send_to_ws
, {.id
= 12} },
4343 { "mvws_14", send_to_ws
, {.id
= 13} },
4344 { "mvws_15", send_to_ws
, {.id
= 14} },
4345 { "mvws_16", send_to_ws
, {.id
= 15} },
4346 { "mvws_17", send_to_ws
, {.id
= 16} },
4347 { "mvws_18", send_to_ws
, {.id
= 17} },
4348 { "mvws_19", send_to_ws
, {.id
= 18} },
4349 { "mvws_20", send_to_ws
, {.id
= 19} },
4350 { "mvws_21", send_to_ws
, {.id
= 20} },
4351 { "mvws_22", send_to_ws
, {.id
= 21} },
4352 { "bar_toggle", bar_toggle
, {0} },
4353 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4354 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4355 { "screenshot_all", legacyfunc
, {0} },
4356 { "screenshot_wind", legacyfunc
, {0} },
4357 { "float_toggle", floating_toggle
,{0} },
4358 { "version", version
, {0} },
4359 { "spawn_lock", legacyfunc
, {0} },
4360 { "spawn_initscr", legacyfunc
, {0} },
4361 { "spawn_custom", dummykeyfunc
, {0} },
4362 { "iconify", iconify
, {0} },
4363 { "uniconify", uniconify
, {0} },
4364 { "raise_toggle", raise_toggle
, {0} },
4365 { "button2", pressbutton
, {2} },
4366 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4367 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4368 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4369 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4370 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4371 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4372 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4373 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4374 { "name_workspace", name_workspace
, {0} },
4375 { "search_workspace", search_workspace
, {0} },
4376 { "search_win", search_win
, {0} },
4377 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4378 { "invalid key func", NULL
, {0} },
4381 RB_ENTRY(key
) entry
;
4384 enum keyfuncid funcid
;
4387 RB_HEAD(key_list
, key
);
4390 key_cmp(struct key
*kp1
, struct key
*kp2
)
4392 if (kp1
->keysym
< kp2
->keysym
)
4394 if (kp1
->keysym
> kp2
->keysym
)
4397 if (kp1
->mod
< kp2
->mod
)
4399 if (kp1
->mod
> kp2
->mod
)
4405 RB_GENERATE_STATIC(key_list
, key
, entry
, key_cmp
);
4406 struct key_list keys
;
4409 enum { client_click
, root_click
};
4411 unsigned int action
;
4413 unsigned int button
;
4414 void (*func
)(struct ws_win
*, union arg
*);
4417 /* action key mouse button func args */
4418 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4419 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4420 { client_click
, MODKEY
, Button1
, move
, {0} },
4424 update_modkey(unsigned int mod
)
4430 RB_FOREACH(kp
, key_list
, &keys
)
4431 if (kp
->mod
& ShiftMask
)
4432 kp
->mod
= mod
| ShiftMask
;
4436 for (i
= 0; i
< LENGTH(buttons
); i
++)
4437 if (buttons
[i
].mask
& ShiftMask
)
4438 buttons
[i
].mask
= mod
| ShiftMask
;
4440 buttons
[i
].mask
= mod
;
4445 TAILQ_ENTRY(spawn_prog
) entry
;
4450 TAILQ_HEAD(spawn_list
, spawn_prog
);
4451 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4454 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4457 struct spawn_prog
*prog
= NULL
;
4459 char *ap
, **real_args
;
4461 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4464 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4465 if (!strcasecmp(spawn_name
, prog
->name
))
4469 warnx("spawn_custom: program %s not found", spawn_name
);
4473 /* make room for expanded args */
4474 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4475 err(1, "spawn_custom: calloc real_args");
4477 /* expand spawn_args into real_args */
4478 for (i
= 0; i
< prog
->argc
; i
++) {
4480 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4481 if (!strcasecmp(ap
, "$bar_border")) {
4483 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4485 err(1, "spawn_custom border color");
4486 } else if (!strcasecmp(ap
, "$bar_color")) {
4488 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4490 err(1, "spawn_custom bar color");
4491 } else if (!strcasecmp(ap
, "$bar_font")) {
4492 if ((real_args
[i
] = strdup(bar_fonts
))
4494 err(1, "spawn_custom bar fonts");
4495 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4497 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4499 err(1, "spawn_custom color font");
4500 } else if (!strcasecmp(ap
, "$color_focus")) {
4502 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4504 err(1, "spawn_custom color focus");
4505 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4507 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4509 err(1, "spawn_custom color unfocus");
4511 /* no match --> copy as is */
4512 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4513 err(1, "spawn_custom strdup(ap)");
4515 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4520 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4521 for (i
= 0; i
< prog
->argc
; i
++)
4522 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4523 DNPRINTF(SWM_D_SPAWN
, "\n");
4525 *ret_args
= real_args
;
4526 return (prog
->argc
);
4530 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4536 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4540 spawn(r
->ws
->idx
, &a
, 1);
4542 for (i
= 0; i
< spawn_argc
; i
++)
4548 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4554 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4558 if (pipe(select_list_pipe
) == -1)
4559 err(1, "pipe error");
4560 if (pipe(select_resp_pipe
) == -1)
4561 err(1, "pipe error");
4563 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4564 err(1, "could not disable SIGPIPE");
4565 switch (*pid
= fork()) {
4567 err(1, "cannot fork");
4570 if (dup2(select_list_pipe
[0], 0) == -1)
4572 if (dup2(select_resp_pipe
[1], 1) == -1)
4574 close(select_list_pipe
[1]);
4575 close(select_resp_pipe
[0]);
4576 spawn(r
->ws
->idx
, &a
, 0);
4578 default: /* parent */
4579 close(select_list_pipe
[0]);
4580 close(select_resp_pipe
[1]);
4584 for (i
= 0; i
< spawn_argc
; i
++)
4590 spawn_insert(char *name
, char *args
)
4592 char *arg
, *cp
, *ptr
;
4593 struct spawn_prog
*sp
;
4595 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
4597 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
4598 err(1, "spawn_insert: malloc");
4599 if ((sp
->name
= strdup(name
)) == NULL
)
4600 err(1, "spawn_insert: strdup");
4602 /* convert the arguments to an argument list */
4603 if ((ptr
= cp
= strdup(args
)) == NULL
)
4604 err(1, "spawn_insert: strdup");
4605 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
4606 /* empty field; skip it */
4611 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
4612 sizeof *sp
->argv
)) == NULL
)
4613 err(1, "spawn_insert: realloc");
4614 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
4615 err(1, "spawn_insert: strdup");
4619 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
4620 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
4624 spawn_remove(struct spawn_prog
*sp
)
4628 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
4630 TAILQ_REMOVE(&spawns
, sp
, entry
);
4631 for (i
= 0; i
< sp
->argc
; i
++)
4637 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
4641 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
4643 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
4646 spawn_insert(name
, args
);
4648 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
4652 setspawn(char *name
, char *args
)
4654 struct spawn_prog
*sp
;
4656 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
4661 TAILQ_FOREACH(sp
, &spawns
, entry
) {
4662 if (!strcmp(sp
->name
, name
)) {
4666 spawn_replace(sp
, name
, args
);
4667 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
4671 if (*args
== '\0') {
4672 warnx("error: setspawn: cannot find program: %s", name
);
4676 spawn_insert(name
, args
);
4677 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
4681 setconfspawn(char *selector
, char *value
, int flags
)
4683 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
4685 setspawn(selector
, value
);
4687 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
4694 setconfspawn("term", "xterm", 0);
4695 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4696 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4697 setconfspawn("lock", "xlock", 0);
4698 setconfspawn("initscr", "initscreen.sh", 0);
4699 setconfspawn("menu", "dmenu_run"
4702 " -nf $bar_font_color"
4704 " -sf $bar_color", 0);
4705 setconfspawn("search", "dmenu"
4709 " -nf $bar_font_color"
4711 " -sf $bar_color", 0);
4712 setconfspawn("name_workspace", "dmenu"
4716 " -nf $bar_font_color"
4718 " -sf $bar_color", 0);
4722 #define SWM_MODNAME_SIZE 32
4723 #define SWM_KEY_WS "\n+ \t"
4725 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
4729 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
4730 if (mod
== NULL
|| ks
== NULL
) {
4731 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
4734 if (keystr
== NULL
|| strlen(keystr
) == 0) {
4735 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
4741 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
4742 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
4744 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
4745 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
4747 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
4749 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
4751 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
4753 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
4755 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
4757 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
4758 *mod
|= ControlMask
;
4760 *ks
= XStringToKeysym(name
);
4761 XConvertCase(*ks
, ks
, &uks
);
4762 if (ks
== NoSymbol
) {
4764 "parsekeys: invalid key %s\n",
4770 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
4775 strdupsafe(char *str
)
4780 return (strdup(str
));
4784 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
4788 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
4789 keyfuncs
[kfid
].name
, spawn_name
);
4791 if ((kp
= malloc(sizeof *kp
)) == NULL
)
4792 err(1, "key_insert: malloc");
4797 kp
->spawn_name
= strdupsafe(spawn_name
);
4798 RB_INSERT(key_list
, &keys
, kp
);
4800 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
4804 key_lookup(unsigned int mod
, KeySym ks
)
4811 return (RB_FIND(key_list
, &keys
, &kp
));
4815 key_remove(struct key
*kp
)
4817 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
4819 RB_REMOVE(key_list
, &keys
, kp
);
4820 free(kp
->spawn_name
);
4823 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
4827 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
4830 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
4834 key_insert(mod
, ks
, kfid
, spawn_name
);
4836 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
4840 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
4845 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
4846 keyfuncs
[kfid
].name
, spawn_name
);
4848 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
4849 if (kfid
== kf_invalid
)
4852 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
4853 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4856 if (kfid
== kf_invalid
) {
4857 warnx("error: setkeybinding: cannot find mod/key combination");
4858 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4862 key_insert(mod
, ks
, kfid
, spawn_name
);
4863 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4867 setconfbinding(char *selector
, char *value
, int flags
)
4869 enum keyfuncid kfid
;
4872 struct spawn_prog
*sp
;
4873 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
4874 if (selector
== NULL
) {
4875 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
4876 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4878 setkeybinding(mod
, ks
, kfid
, NULL
);
4883 /* search by key function name */
4884 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
4885 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
4886 SWM_FUNCNAME_LEN
) == 0) {
4887 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4889 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4890 setkeybinding(mod
, ks
, kfid
, NULL
);
4896 /* search by custom spawn name */
4897 TAILQ_FOREACH(sp
, &spawns
, entry
) {
4898 if (strcasecmp(selector
, sp
->name
) == 0) {
4899 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4901 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4902 setkeybinding(mod
, ks
, kf_spawn_custom
,
4909 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
4916 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
4917 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, kf_flip_layout
, NULL
);
4918 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
4919 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
4920 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
4921 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
4922 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
4923 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
4924 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
4925 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
4926 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
4927 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
4928 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
4929 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
4930 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_term
, NULL
);
4931 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
4932 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
4933 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
4934 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
4935 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
4936 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
4937 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
4938 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
4939 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
4940 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
4941 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
4942 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
4943 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
4944 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
4945 setkeybinding(MODKEY
, XK_F1
, kf_ws_11
, NULL
);
4946 setkeybinding(MODKEY
, XK_F2
, kf_ws_12
, NULL
);
4947 setkeybinding(MODKEY
, XK_F3
, kf_ws_13
, NULL
);
4948 setkeybinding(MODKEY
, XK_F4
, kf_ws_14
, NULL
);
4949 setkeybinding(MODKEY
, XK_F5
, kf_ws_15
, NULL
);
4950 setkeybinding(MODKEY
, XK_F6
, kf_ws_16
, NULL
);
4951 setkeybinding(MODKEY
, XK_F7
, kf_ws_17
, NULL
);
4952 setkeybinding(MODKEY
, XK_F8
, kf_ws_18
, NULL
);
4953 setkeybinding(MODKEY
, XK_F9
, kf_ws_19
, NULL
);
4954 setkeybinding(MODKEY
, XK_F10
, kf_ws_20
, NULL
);
4955 setkeybinding(MODKEY
, XK_F11
, kf_ws_21
, NULL
);
4956 setkeybinding(MODKEY
, XK_F12
, kf_ws_22
, NULL
);
4957 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
4958 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
4959 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
4960 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
4961 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
4962 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
4963 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
4964 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
4965 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
4966 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
4967 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
4968 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
4969 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
4970 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
4971 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
4972 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
4973 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
4974 setkeybinding(MODKEY
|ShiftMask
, XK_F1
, kf_mvws_11
, NULL
);
4975 setkeybinding(MODKEY
|ShiftMask
, XK_F2
, kf_mvws_12
, NULL
);
4976 setkeybinding(MODKEY
|ShiftMask
, XK_F3
, kf_mvws_13
, NULL
);
4977 setkeybinding(MODKEY
|ShiftMask
, XK_F4
, kf_mvws_14
, NULL
);
4978 setkeybinding(MODKEY
|ShiftMask
, XK_F5
, kf_mvws_15
, NULL
);
4979 setkeybinding(MODKEY
|ShiftMask
, XK_F6
, kf_mvws_16
, NULL
);
4980 setkeybinding(MODKEY
|ShiftMask
, XK_F7
, kf_mvws_17
, NULL
);
4981 setkeybinding(MODKEY
|ShiftMask
, XK_F8
, kf_mvws_18
, NULL
);
4982 setkeybinding(MODKEY
|ShiftMask
, XK_F9
, kf_mvws_19
, NULL
);
4983 setkeybinding(MODKEY
|ShiftMask
, XK_F10
, kf_mvws_20
, NULL
);
4984 setkeybinding(MODKEY
|ShiftMask
, XK_F11
, kf_mvws_21
, NULL
);
4985 setkeybinding(MODKEY
|ShiftMask
, XK_F12
, kf_mvws_22
, NULL
);
4986 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
4987 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
4988 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
4989 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
4990 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
4991 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
4992 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
4993 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
4994 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
4995 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
4996 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
4997 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
4998 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
4999 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
5000 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
5001 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
5002 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
5003 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
5004 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
5005 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
5006 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
5007 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
5008 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
5009 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
5010 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
5011 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
5013 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
5022 while (RB_EMPTY(&keys
) == 0) {
5023 kp
= RB_ROOT(&keys
);
5029 setkeymapping(char *selector
, char *value
, int flags
)
5031 char keymapping_file
[PATH_MAX
];
5032 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5033 if (value
[0] == '~')
5034 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5035 pwd
->pw_dir
, &value
[1]);
5037 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5039 /* load new key bindings; if it fails, revert to default bindings */
5040 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5044 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5049 updatenumlockmask(void)
5052 XModifierKeymap
*modmap
;
5054 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
5056 modmap
= XGetModifierMapping(display
);
5057 for (i
= 0; i
< 8; i
++)
5058 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
5059 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
5060 == XKeysymToKeycode(display
, XK_Num_Lock
))
5061 numlockmask
= (1 << i
);
5063 XFreeModifiermap(modmap
);
5071 unsigned int modifiers
[] =
5072 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
5075 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5076 updatenumlockmask();
5078 for (k
= 0; k
< ScreenCount(display
); k
++) {
5079 if (TAILQ_EMPTY(&screens
[k
].rl
))
5081 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
5082 RB_FOREACH(kp
, key_list
, &keys
) {
5083 if ((code
= XKeysymToKeycode(display
, kp
->keysym
)))
5084 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5085 XGrabKey(display
, code
,
5086 kp
->mod
| modifiers
[j
],
5087 screens
[k
].root
, True
,
5088 GrabModeAsync
, GrabModeAsync
);
5094 grabbuttons(struct ws_win
*win
, int focused
)
5097 unsigned int modifiers
[] =
5098 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5100 updatenumlockmask();
5101 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
5103 for (i
= 0; i
< LENGTH(buttons
); i
++)
5104 if (buttons
[i
].action
== client_click
)
5105 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5106 XGrabButton(display
, buttons
[i
].button
,
5107 buttons
[i
].mask
| modifiers
[j
],
5108 win
->id
, False
, BUTTONMASK
,
5109 GrabModeAsync
, GrabModeSync
, None
,
5112 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
5113 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
5116 const char *quirkname
[] = {
5117 "NONE", /* config string for "no value" */
5126 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5127 #define SWM_Q_WS "\n|+ \t"
5129 parsequirks(char *qstr
, unsigned long *quirk
)
5139 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5141 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5142 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5143 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5144 DNPRINTF(SWM_D_QUIRK
,
5145 "parsequirks: %s\n", name
);
5150 *quirk
|= 1 << (i
-1);
5154 if (i
>= LENGTH(quirkname
)) {
5155 DNPRINTF(SWM_D_QUIRK
,
5156 "parsequirks: invalid quirk [%s]\n", name
);
5164 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5168 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5171 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5172 err(1, "quirk_insert: malloc");
5173 if ((qp
->class = strdup(class)) == NULL
)
5174 err(1, "quirk_insert: strdup");
5175 if ((qp
->name
= strdup(name
)) == NULL
)
5176 err(1, "quirk_insert: strdup");
5179 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5181 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5185 quirk_remove(struct quirk
*qp
)
5187 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5188 qp
->name
, qp
->quirk
);
5190 TAILQ_REMOVE(&quirks
, qp
, entry
);
5195 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5199 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5200 unsigned long quirk
)
5202 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5203 qp
->name
, qp
->quirk
);
5206 quirk_insert(class, name
, quirk
);
5208 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5212 setquirk(const char *class, const char *name
, unsigned long quirk
)
5216 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5219 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5220 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5224 quirk_replace(qp
, class, name
, quirk
);
5225 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5230 warnx("error: setquirk: cannot find class/name combination");
5234 quirk_insert(class, name
, quirk
);
5235 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5239 setconfquirk(char *selector
, char *value
, int flags
)
5241 char *cp
, *class, *name
;
5243 unsigned long quirks
;
5244 if (selector
== NULL
)
5246 if ((cp
= strchr(selector
, ':')) == NULL
)
5251 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5252 setquirk(class, name
, quirks
);
5259 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5260 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5261 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5262 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5263 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5264 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5265 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5266 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5267 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5268 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5269 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5270 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5271 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5274 /* conf file stuff */
5275 #define SWM_CONF_FILE "spectrwm.conf"
5276 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5278 enum { SWM_S_BAR_DELAY
, SWM_S_BAR_ENABLED
, SWM_S_BAR_BORDER_WIDTH
,
5279 SWM_S_STACK_ENABLED
, SWM_S_CLOCK_ENABLED
, SWM_S_CLOCK_FORMAT
,
5280 SWM_S_CYCLE_EMPTY
, SWM_S_CYCLE_VISIBLE
, SWM_S_WORKSPACE_LIMIT
,
5281 SWM_S_SS_ENABLED
, SWM_S_TERM_WIDTH
, SWM_S_TITLE_CLASS_ENABLED
,
5282 SWM_S_TITLE_NAME_ENABLED
, SWM_S_WINDOW_NAME_ENABLED
, SWM_S_URGENT_ENABLED
,
5283 SWM_S_FOCUS_MODE
, SWM_S_DISABLE_BORDER
, SWM_S_BORDER_WIDTH
,
5284 SWM_S_BAR_FONT
, SWM_S_BAR_ACTION
, SWM_S_SPAWN_TERM
,
5285 SWM_S_SS_APP
, SWM_S_DIALOG_RATIO
, SWM_S_BAR_AT_BOTTOM
,
5286 SWM_S_VERBOSE_LAYOUT
, SWM_S_BAR_JUSTIFY
5290 setconfvalue(char *selector
, char *value
, int flags
)
5296 case SWM_S_BAR_DELAY
:
5297 bar_delay
= atoi(value
);
5299 case SWM_S_BAR_ENABLED
:
5300 bar_enabled
= atoi(value
);
5302 case SWM_S_BAR_BORDER_WIDTH
:
5303 bar_border_width
= atoi(value
);
5305 case SWM_S_BAR_AT_BOTTOM
:
5306 bar_at_bottom
= atoi(value
);
5308 case SWM_S_BAR_JUSTIFY
:
5309 if (!strcmp(value
, "left"))
5310 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5311 else if (!strcmp(value
, "center"))
5312 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5313 else if (!strcmp(value
, "right"))
5314 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5316 errx(1, "invalid bar_justify");
5318 case SWM_S_STACK_ENABLED
:
5319 stack_enabled
= atoi(value
);
5321 case SWM_S_CLOCK_ENABLED
:
5322 clock_enabled
= atoi(value
);
5324 case SWM_S_CLOCK_FORMAT
:
5325 #ifndef SWM_DENY_CLOCK_FORMAT
5327 if ((clock_format
= strdup(value
)) == NULL
)
5328 err(1, "setconfvalue: clock_format");
5331 case SWM_S_CYCLE_EMPTY
:
5332 cycle_empty
= atoi(value
);
5334 case SWM_S_CYCLE_VISIBLE
:
5335 cycle_visible
= atoi(value
);
5337 case SWM_S_WORKSPACE_LIMIT
:
5338 workspace_limit
= atoi(value
);
5339 if (workspace_limit
> SWM_WS_MAX
)
5340 workspace_limit
= SWM_WS_MAX
;
5341 else if (workspace_limit
< 1)
5342 workspace_limit
= 1;
5344 case SWM_S_SS_ENABLED
:
5345 ss_enabled
= atoi(value
);
5347 case SWM_S_TERM_WIDTH
:
5348 term_width
= atoi(value
);
5350 case SWM_S_TITLE_CLASS_ENABLED
:
5351 title_class_enabled
= atoi(value
);
5353 case SWM_S_WINDOW_NAME_ENABLED
:
5354 window_name_enabled
= atoi(value
);
5356 case SWM_S_TITLE_NAME_ENABLED
:
5357 title_name_enabled
= atoi(value
);
5359 case SWM_S_URGENT_ENABLED
:
5360 urgent_enabled
= atoi(value
);
5362 case SWM_S_FOCUS_MODE
:
5363 if (!strcmp(value
, "default"))
5364 focus_mode
= SWM_FOCUS_DEFAULT
;
5365 else if (!strcmp(value
, "follow_cursor"))
5366 focus_mode
= SWM_FOCUS_FOLLOW
;
5367 else if (!strcmp(value
, "synergy"))
5368 focus_mode
= SWM_FOCUS_SYNERGY
;
5370 errx(1, "focus_mode");
5372 case SWM_S_DISABLE_BORDER
:
5373 disable_border
= atoi(value
);
5375 case SWM_S_BORDER_WIDTH
:
5376 border_width
= atoi(value
);
5378 case SWM_S_BAR_FONT
:
5380 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5381 err(1, "setconfvalue: asprintf: failed to allocate "
5382 "memory for bar_fonts.");
5386 case SWM_S_BAR_ACTION
:
5388 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5389 err(1, "setconfvalue: bar_action");
5391 case SWM_S_SPAWN_TERM
:
5392 free(spawn_term
[0]);
5393 if ((spawn_term
[0] = strdup(value
)) == NULL
)
5394 err(1, "setconfvalue: spawn_term");
5398 case SWM_S_DIALOG_RATIO
:
5399 dialog_ratio
= atof(value
);
5400 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5403 case SWM_S_VERBOSE_LAYOUT
:
5404 verbose_layout
= atoi(value
);
5405 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5407 layouts
[i
].l_string
= fancy_stacker
;
5409 layouts
[i
].l_string
= plain_stacker
;
5419 setconfmodkey(char *selector
, char *value
, int flags
)
5421 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5422 update_modkey(Mod1Mask
);
5423 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5424 update_modkey(Mod2Mask
);
5425 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5426 update_modkey(Mod3Mask
);
5427 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5428 update_modkey(Mod4Mask
);
5435 setconfcolor(char *selector
, char *value
, int flags
)
5437 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5442 setconfregion(char *selector
, char *value
, int flags
)
5444 custom_region(value
);
5449 setautorun(char *selector
, char *value
, int flags
)
5459 if (getenv("SWM_STARTED"))
5463 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5464 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5466 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5467 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5470 * This is a little intricate
5472 * If the pid already exists we simply reuse it because it means it was
5473 * used before AND not claimed by manage_window. We get away with
5474 * altering it in the parent after INSERT because this can not be a race
5477 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5480 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5482 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5483 err(1, "setautorun: realloc");
5484 a
.argv
[argc
- 1] = ap
;
5487 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5488 err(1, "setautorun: realloc");
5489 a
.argv
[argc
] = NULL
;
5491 if ((pid
= fork()) == 0) {
5492 spawn(ws_id
, &a
, 1);
5501 p
= calloc(1, sizeof *p
);
5504 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
5514 setlayout(char *selector
, char *value
, int flags
)
5516 int ws_id
, i
, x
, mg
, ma
, si
, raise
;
5517 int st
= SWM_V_STACK
;
5519 struct workspace
*ws
;
5521 if (getenv("SWM_STARTED"))
5525 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
5526 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
5527 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5528 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5531 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5532 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
5534 if (!strcasecmp(s
, "vertical"))
5536 else if (!strcasecmp(s
, "horizontal"))
5538 else if (!strcasecmp(s
, "fullscreen"))
5541 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5542 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5545 for (i
= 0; i
< ScreenCount(display
); i
++) {
5546 ws
= (struct workspace
*)&screens
[i
].ws
;
5547 ws
[ws_id
].cur_layout
= &layouts
[st
];
5549 ws
[ws_id
].always_raise
= raise
;
5550 if (st
== SWM_MAX_STACK
)
5554 for (x
= 0; x
< abs(mg
); x
++) {
5555 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5556 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
5557 SWM_ARG_ID_MASTERSHRINK
);
5561 for (x
= 0; x
< abs(ma
); x
++) {
5562 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5563 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
5564 SWM_ARG_ID_MASTERDEL
);
5568 for (x
= 0; x
< abs(si
); x
++) {
5569 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5570 si
>= 0 ? SWM_ARG_ID_STACKINC
:
5571 SWM_ARG_ID_STACKDEC
);
5579 /* config options */
5580 struct config_option
{
5582 int (*func
)(char*, char*, int);
5585 struct config_option configopt
[] = {
5586 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
5587 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
5588 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
5589 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
5590 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
5591 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
5592 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
5593 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
5594 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
5595 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
5596 { "keyboard_mapping", setkeymapping
, 0 },
5597 { "bind", setconfbinding
, 0 },
5598 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
5599 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
5600 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
5601 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
5602 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
5603 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
5604 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
5605 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
5606 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
5607 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
5608 { "modkey", setconfmodkey
, 0 },
5609 { "program", setconfspawn
, 0 },
5610 { "quirk", setconfquirk
, 0 },
5611 { "region", setconfregion
, 0 },
5612 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
5613 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
5614 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
5615 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
5616 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
5617 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
5618 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
5619 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
5620 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
5621 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
5622 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
5623 { "autorun", setautorun
, 0 },
5624 { "layout", setlayout
, 0 },
5629 conf_load(char *filename
, int keymapping
)
5632 char *line
, *cp
, *optsub
, *optval
;
5633 size_t linelen
, lineno
= 0;
5634 int wordlen
, i
, optind
;
5635 struct config_option
*opt
;
5637 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
5639 if (filename
== NULL
) {
5640 warnx("conf_load: no filename");
5643 if ((config
= fopen(filename
, "r")) == NULL
) {
5644 warn("conf_load: fopen: %s", filename
);
5648 while (!feof(config
)) {
5649 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
5652 err(1, "%s", filename
);
5657 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
5658 if (cp
[0] == '\0') {
5663 /* get config option */
5664 wordlen
= strcspn(cp
, "=[ \t\n");
5666 warnx("%s: line %zd: no option found",
5671 for (i
= 0; i
< LENGTH(configopt
); i
++) {
5672 opt
= &configopt
[i
];
5673 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
5674 strlen(opt
->optname
) == wordlen
) {
5680 warnx("%s: line %zd: unknown option %.*s",
5681 filename
, lineno
, wordlen
, cp
);
5684 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
5685 warnx("%s: line %zd: invalid option %.*s",
5686 filename
, lineno
, wordlen
, cp
);
5690 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
5691 /* get [selector] if any */
5695 wordlen
= strcspn(cp
, "]");
5698 warnx("%s: line %zd: syntax error",
5703 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
5705 warnx("%s: line %zd: unable to allocate"
5706 "memory for selector", filename
,
5712 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
5714 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
5716 optval
= strdup(cp
);
5717 /* call function to deal with it all */
5718 if (configopt
[optind
].func(optsub
, optval
,
5719 configopt
[optind
].funcflags
) != 0)
5720 errx(1, "%s: line %zd: invalid data for %s",
5721 filename
, lineno
, configopt
[optind
].optname
);
5728 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
5735 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
5741 set_child_transient(struct ws_win
*win
, Window
*trans
)
5743 struct ws_win
*parent
, *w
;
5744 XWMHints
*wmh
= NULL
;
5745 struct swm_region
*r
;
5746 struct workspace
*ws
;
5748 parent
= find_window(win
->transient
);
5750 parent
->child_trans
= win
;
5752 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
5753 " for 0x%lx trans 0x%lx\n", win
->id
, win
->transient
);
5755 if (win
->hints
== NULL
) {
5756 warnx("no hints for 0x%lx", win
->id
);
5760 r
= root_to_region(win
->wa
.root
);
5762 /* parent doen't exist in our window list */
5763 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5767 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
5768 warnx("can't get hints for 0x%lx", w
->id
);
5772 if (win
->hints
->window_group
!= wmh
->window_group
)
5775 w
->child_trans
= win
;
5776 win
->transient
= w
->id
;
5778 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
5779 "transient to 0x%lx\n", win
->transient
);
5789 window_get_pid(Window win
)
5791 Atom actual_type_return
;
5792 int actual_format_return
= 0;
5793 unsigned long nitems_return
= 0;
5794 unsigned long bytes_after_return
= 0;
5798 unsigned char *prop
= NULL
;
5800 if (XGetWindowProperty(display
, win
,
5801 XInternAtom(display
, "_NET_WM_PID", False
), 0, 1, False
,
5802 XA_CARDINAL
, &actual_type_return
, &actual_format_return
,
5803 &nitems_return
, &bytes_after_return
,
5804 (unsigned char**)(void*)&pid
) != Success
)
5806 if (actual_type_return
!= XA_CARDINAL
)
5817 if (XGetWindowProperty(display
, win
,
5818 XInternAtom(display
, "_SWM_PID", False
), 0, SWM_PROPLEN
, False
,
5819 XA_STRING
, &actual_type_return
, &actual_format_return
,
5820 &nitems_return
, &bytes_after_return
, &prop
) != Success
)
5822 if (actual_type_return
!= XA_STRING
)
5827 ret
= strtonum((const char *)prop
, 0, UINT_MAX
, &errstr
);
5828 /* ignore error because strtonum returns 0 anyway */
5835 manage_window(Window id
)
5838 struct workspace
*ws
;
5839 struct ws_win
*win
, *ww
;
5840 int format
, i
, ws_idx
, n
, border_me
= 0;
5841 unsigned long nitems
, bytes
;
5842 Atom ws_idx_atom
= 0, type
;
5843 Atom
*prot
= NULL
, *pp
;
5844 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
5845 struct swm_region
*r
;
5852 if ((win
= find_window(id
)) != NULL
)
5853 return (win
); /* already being managed */
5855 /* see if we are on the unmanaged list */
5856 if ((win
= find_unmanaged_window(id
)) != NULL
) {
5857 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
5858 "window: 0x%lx\n", win
->id
);
5859 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5860 if (win
->transient
) {
5861 set_child_transient(win
, &trans
);
5862 } if (trans
&& (ww
= find_window(trans
)))
5863 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
5865 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
5866 ewmh_update_actions(win
);
5870 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
5871 err(1, "manage_window: calloc: failed to allocate memory for "
5876 /* see if we need to override the workspace */
5877 p
= find_pid(window_get_pid(id
));
5879 /* Get all the window data in one shot */
5880 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
5882 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
5883 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
5885 XGetWindowAttributes(display
, id
, &win
->wa
);
5886 XGetWMNormalHints(display
, id
, &win
->sh
, &win
->sh_mask
);
5887 win
->hints
= XGetWMHints(display
, id
);
5888 XGetTransientForHint(display
, id
, &trans
);
5890 win
->transient
= trans
;
5891 set_child_transient(win
, &trans
);
5892 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, "
5893 "transient: 0x%lx\n", win
->id
, win
->transient
);
5896 /* get supported protocols */
5897 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
5898 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
5899 if (*pp
== takefocus
)
5900 win
->take_focus
= 1;
5902 win
->can_delete
= 1;
5908 win
->iconic
= get_iconic(win
);
5911 * Figure out where to put the window. If it was previously assigned to
5912 * a workspace (either by spawn() or manually moving), and isn't
5913 * transient, * put it in the same workspace
5915 r
= root_to_region(win
->wa
.root
);
5917 ws
= &r
->s
->ws
[p
->ws
];
5918 TAILQ_REMOVE(&pidlist
, p
, entry
);
5921 } else if (prop
&& win
->transient
== 0) {
5922 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
5923 ws_idx
= strtonum((const char *)prop
, 0, workspace_limit
- 1,
5926 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
5929 ws
= &r
->s
->ws
[ws_idx
];
5932 /* this should launch transients in the same ws as parent */
5934 if ((ww
= find_window(trans
)) != NULL
)
5940 warnx("manage_window: fix this "
5946 /* set up the window layout */
5949 win
->s
= r
->s
; /* this never changes */
5950 if (trans
&& (ww
= find_window(trans
)))
5951 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
5953 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
5955 WIDTH(win
) = win
->wa
.width
;
5956 HEIGHT(win
) = win
->wa
.height
;
5959 win
->g_floatvalid
= 0;
5960 win
->floatmaxed
= 0;
5961 win
->ewmh_flags
= 0;
5963 /* Set window properties so we can remember this after reincarnation */
5964 if (ws_idx_atom
&& prop
== NULL
&&
5965 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
5967 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
5969 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
5970 PropModeReplace
, ws_idx_str
, strlen((char *)ws_idx_str
));
5975 ewmh_autoquirk(win
);
5977 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
5978 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
5979 win
->ch
.res_class
, win
->ch
.res_name
);
5981 /* java is retarded so treat it special */
5982 if (strstr(win
->ch
.res_name
, "sun-awt")) {
5987 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5988 if (!strcmp(win
->ch
.res_class
, qp
->class) &&
5989 !strcmp(win
->ch
.res_name
, qp
->name
)) {
5990 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
5991 "class: %s, name: %s\n", win
->ch
.res_class
,
5993 if (qp
->quirk
& SWM_Q_FLOAT
) {
5997 win
->quirks
= qp
->quirk
;
6002 /* alter window position if quirky */
6003 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6004 win
->manual
= 1; /* don't center the quirky windows */
6005 bzero(&wc
, sizeof wc
);
6007 if (bar_enabled
&& Y(win
) < bar_height
) {
6008 Y(win
) = wc
.y
= bar_height
;
6011 if (WIDTH(win
) + X(win
) > WIDTH(r
)) {
6012 X(win
) = wc
.x
= WIDTH(r
) - WIDTH(win
) - 2;
6018 /* Reset font sizes (the bruteforce way; no default keybinding). */
6019 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6020 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6021 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
6022 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6023 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
6026 ewmh_get_win_state(win
);
6027 ewmh_update_actions(win
);
6028 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
6032 bzero(&wc
, sizeof wc
);
6033 wc
.border_width
= border_width
;
6034 mask
|= CWBorderWidth
;
6035 XConfigureWindow(display
, win
->id
, mask
, &wc
);
6038 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
6039 PropertyChangeMask
| StructureNotifyMask
);
6041 /* floaters need to be mapped if they are in the current workspace */
6042 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
6043 XMapRaised(display
, win
->id
);
6049 free_window(struct ws_win
*win
)
6051 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%lx\n", win
->id
);
6056 /* needed for restart wm */
6057 set_win_state(win
, WithdrawnState
);
6059 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6061 if (win
->ch
.res_class
)
6062 XFree(win
->ch
.res_class
);
6063 if (win
->ch
.res_name
)
6064 XFree(win
->ch
.res_name
);
6069 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6075 unmanage_window(struct ws_win
*win
)
6077 struct ws_win
*parent
;
6082 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%lx\n", win
->id
);
6084 if (win
->transient
) {
6085 parent
= find_window(win
->transient
);
6087 parent
->child_trans
= NULL
;
6090 /* focus on root just in case */
6091 XSetInputFocus(display
, PointerRoot
, PointerRoot
, CurrentTime
);
6100 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
6101 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6107 focus_magic(struct ws_win
*win
)
6109 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%lx\n", WINID(win
));
6112 /* if there are no windows clear the status-bar */
6117 if (win
->child_trans
) {
6118 /* win = parent & has a transient so focus on that */
6120 focus_win(win
->child_trans
);
6121 if (win
->child_trans
->take_focus
)
6122 client_msg(win
, takefocus
);
6124 /* make sure transient hasn't disappeared */
6125 if (validate_win(win
->child_trans
) == 0) {
6126 focus_win(win
->child_trans
);
6127 if (win
->child_trans
->take_focus
)
6128 client_msg(win
->child_trans
, takefocus
);
6130 win
->child_trans
= NULL
;
6132 if (win
->take_focus
)
6133 client_msg(win
, takefocus
);
6139 if (win
->take_focus
)
6140 client_msg(win
, takefocus
);
6147 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%lx\n", e
->xexpose
.window
);
6154 XKeyEvent
*ev
= &e
->xkey
;
6156 struct swm_region
*r
;
6158 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
6159 if ((kp
= key_lookup(CLEANMASK(ev
->state
), keysym
)) == NULL
)
6161 if (keyfuncs
[kp
->funcid
].func
== NULL
)
6164 r
= root_to_region(ev
->root
);
6165 if (kp
->funcid
== kf_spawn_custom
)
6166 spawn_custom(r
, &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
6168 keyfuncs
[kp
->funcid
].func(r
, &(keyfuncs
[kp
->funcid
].args
));
6172 buttonpress(XEvent
*e
)
6176 XButtonPressedEvent
*ev
= &e
->xbutton
;
6178 if ((win
= find_window(ev
->window
)) == NULL
)
6182 action
= client_click
;
6184 for (i
= 0; i
< LENGTH(buttons
); i
++)
6185 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6186 buttons
[i
].button
== ev
->button
&&
6187 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
6188 buttons
[i
].func(win
, &buttons
[i
].args
);
6192 configurerequest(XEvent
*e
)
6194 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
6199 if ((win
= find_window(ev
->window
)) == NULL
)
6200 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6203 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%lx, new: %s\n",
6204 ev
->window
, YESNO(new));
6207 bzero(&wc
, sizeof wc
);
6210 wc
.width
= ev
->width
;
6211 wc
.height
= ev
->height
;
6212 wc
.border_width
= ev
->border_width
;
6213 wc
.sibling
= ev
->above
;
6214 wc
.stack_mode
= ev
->detail
;
6215 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
6217 config_win(win
, ev
);
6222 configurenotify(XEvent
*e
)
6226 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%lx\n",
6227 e
->xconfigure
.window
);
6229 win
= find_window(e
->xconfigure
.window
);
6231 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &win
->sh_mask
);
6235 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6236 drain_enter_notify();
6241 destroynotify(XEvent
*e
)
6244 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
6246 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%lx\n", ev
->window
);
6248 if ((win
= find_window(ev
->window
)) == NULL
) {
6249 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6255 /* make sure we focus on something */
6258 unmanage_window(win
);
6260 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6261 drain_enter_notify();
6266 enternotify(XEvent
*e
)
6268 XCrossingEvent
*ev
= &e
->xcrossing
;
6273 Window focus_return
;
6274 int revert_to_return
;
6276 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%lx, mode: %d, detail: "
6277 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6278 "state: %d\n", ev
->window
, ev
->mode
, ev
->detail
, ev
->root
,
6279 ev
->subwindow
, YESNO(ev
->same_screen
), YESNO(ev
->focus
), ev
->state
);
6281 if (ev
->mode
!= NotifyNormal
) {
6282 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6287 switch (focus_mode
) {
6288 case SWM_FOCUS_DEFAULT
:
6290 case SWM_FOCUS_FOLLOW
:
6292 case SWM_FOCUS_SYNERGY
:
6295 * all these checks need to be in this order because the
6296 * XCheckTypedWindowEvent relies on weeding out the previous events
6298 * making this code an option would enable a follow mouse for focus
6303 * state is set when we are switching workspaces and focus is set when
6304 * the window or a subwindow already has focus (occurs during restart).
6306 * Only honor the focus flag if last_focus_event is not FocusOut,
6307 * this allows spectrwm to continue to control focus when another
6308 * program is also playing with it.
6310 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6311 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6316 * happens when a window is created or destroyed and the border
6317 * crosses the mouse pointer and when switching ws
6319 * we need the subwindow test to see if we came from root in order
6320 * to give focus to floaters
6322 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6323 ev
->subwindow
== 0) {
6324 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6328 /* this window already has focus */
6329 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6330 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6334 /* this window is being deleted or moved to another ws */
6335 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6337 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6338 XPutBackEvent(display
, &cne
);
6342 if ((win
= find_window(ev
->window
)) == NULL
) {
6343 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6348 * In fullstack kill all enters unless they come from a different ws
6349 * (i.e. another region) or focus has been grabbed externally.
6351 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6352 last_focus_event
!= FocusOut
) {
6353 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6354 if ((w
= find_window(focus_return
)) == NULL
||
6356 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6364 if ((win
= find_window(ev
->window
)) == NULL
) {
6365 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6370 * if we have more enternotifies let them handle it in due time
6372 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
6373 DNPRINTF(SWM_D_EVENT
,
6374 "ignoring enternotify: got more enternotify\n");
6375 XPutBackEvent(display
, &cne
);
6382 /* lets us use one switch statement for arbitrary mode/detail combinations */
6383 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6386 focusevent(XEvent
*e
)
6390 u_int32_t mode_detail
;
6391 XFocusChangeEvent
*ev
= &e
->xfocus
;
6393 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: 0x%lx mode: %d "
6394 "detail: %d\n", ev
->type
== FocusIn
? "entering" : "leaving",
6395 ev
->window
, ev
->mode
, ev
->detail
);
6397 if (last_focus_event
== ev
->type
) {
6398 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
6402 last_focus_event
= ev
->type
;
6403 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
6405 switch (mode_detail
) {
6406 /* synergy client focus operations */
6407 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
6408 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
6410 /* synergy server focus operations */
6411 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
6413 /* Entering applications like rdesktop that mangle the pointer */
6414 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
6416 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
6417 XSetWindowBorder(display
, win
->id
,
6418 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
6419 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
6422 warnx("ignoring focusevent");
6423 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
6430 mapnotify(XEvent
*e
)
6433 XMapEvent
*ev
= &e
->xmap
;
6435 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%lx\n", ev
->window
);
6437 win
= manage_window(ev
->window
);
6439 set_win_state(win
, NormalState
);
6443 mappingnotify(XEvent
*e
)
6445 XMappingEvent
*ev
= &e
->xmapping
;
6447 XRefreshKeyboardMapping(ev
);
6448 if (ev
->request
== MappingKeyboard
)
6453 maprequest(XEvent
*e
)
6456 struct swm_region
*r
;
6457 XWindowAttributes wa
;
6458 XMapRequestEvent
*ev
= &e
->xmaprequest
;
6460 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%lx\n",
6461 e
->xmaprequest
.window
);
6463 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
6465 if (wa
.override_redirect
)
6468 win
= manage_window(e
->xmaprequest
.window
);
6470 return; /* can't happen */
6474 /* make new win focused */
6475 r
= root_to_region(win
->wa
.root
);
6476 if (win
->ws
== r
->ws
)
6481 propertynotify(XEvent
*e
)
6484 XPropertyEvent
*ev
= &e
->xproperty
;
6487 name
= XGetAtomName(display
, ev
->atom
);
6488 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%lx, atom: %s\n",
6493 win
= find_window(ev
->window
);
6497 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
6498 update_iconic(win
, 0);
6499 XMapRaised(display
, win
->id
);
6507 case XA_WM_NORMAL_HINTS
:
6509 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
6510 warnx("normal hints: flag 0x%x", win
->sh
.flags
);
6511 if (win
->sh
.flags
& PMinSize
) {
6512 WIDTH(win
) = win
->sh
.min_width
;
6513 HEIGHT(win
) = win
->sh
.min_height
;
6514 warnx("min %d %d", WIDTH(win
), HEIGHT(win
));
6516 XMoveResizeWindow(display
, win
->id
,
6517 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
));
6520 if (title_name_enabled
|| title_class_enabled
)
6524 if (window_name_enabled
)
6533 unmapnotify(XEvent
*e
)
6537 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%lx\n", e
->xunmap
.window
);
6539 /* determine if we need to help unmanage this window */
6540 win
= find_window(e
->xunmap
.window
);
6544 if (getstate(e
->xunmap
.window
) == NormalState
) {
6545 unmanage_window(win
);
6548 /* giant hack for apps that don't destroy transient windows */
6549 /* eat a bunch of events to prevent remanaging the window */
6551 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6552 EnterWindowMask
, &cne
))
6554 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6555 StructureNotifyMask
, &cne
))
6557 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6558 SubstructureNotifyMask
, &cne
))
6560 /* resend unmap because we ated it */
6561 XUnmapWindow(display
, e
->xunmap
.window
);
6564 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6565 drain_enter_notify();
6569 visibilitynotify(XEvent
*e
)
6572 struct swm_region
*r
;
6574 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%lx\n",
6575 e
->xvisibility
.window
);
6576 if (e
->xvisibility
.state
== VisibilityUnobscured
)
6577 for (i
= 0; i
< ScreenCount(display
); i
++)
6578 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6579 if (e
->xvisibility
.window
== r
->bar_window
)
6584 clientmessage(XEvent
*e
)
6586 XClientMessageEvent
*ev
;
6591 win
= find_window(ev
->window
);
6595 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx, type: %ld\n",
6596 ev
->window
, ev
->message_type
);
6598 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
6599 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
6602 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
6603 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
6604 if (win
->can_delete
)
6605 client_msg(win
, adelete
);
6607 XKillClient(display
, win
->id
);
6609 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
6610 DNPRINTF(SWM_D_EVENT
,
6611 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
6612 if (win
->floating
) {
6613 if (ev
->data
.l
[0] & (1<<8)) /* x */
6614 X(win
) = ev
->data
.l
[1];
6615 if (ev
->data
.l
[0] & (1<<9)) /* y */
6616 Y(win
) = ev
->data
.l
[2];
6617 if (ev
->data
.l
[0] & (1<<10)) /* width */
6618 WIDTH(win
) = ev
->data
.l
[3];
6619 if (ev
->data
.l
[0] & (1<<11)) /* height */
6620 HEIGHT(win
) = ev
->data
.l
[4];
6625 /* TODO: Change stack sizes */
6626 /* notify no change was made. */
6627 config_win(win
, NULL
);
6630 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
6631 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
6632 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
6634 ewmh_update_win_state(win
, ev
->data
.l
[2],
6642 xerror_start(Display
*d
, XErrorEvent
*ee
)
6649 xerror(Display
*d
, XErrorEvent
*ee
)
6651 /* warnx("error: %p %p", display, ee); */
6659 xerrorxlib
= XSetErrorHandler(xerror_start
);
6661 /* this causes an error if some other window manager is running */
6662 XSelectInput(display
, DefaultRootWindow(display
),
6663 SubstructureRedirectMask
);
6664 XSync(display
, False
);
6668 XSetErrorHandler(xerror
);
6669 XSync(display
, False
);
6674 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
6676 struct swm_region
*r
, *n
;
6677 struct workspace
*ws
= NULL
;
6680 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
6681 s
->idx
, w
, h
, x
, y
);
6683 /* remove any conflicting regions */
6684 n
= TAILQ_FIRST(&s
->rl
);
6687 n
= TAILQ_NEXT(r
, entry
);
6688 if (X(r
) < (x
+ w
) &&
6689 (X(r
) + WIDTH(r
)) > x
&&
6691 (Y(r
) + HEIGHT(r
)) > y
) {
6692 if (r
->ws
->r
!= NULL
)
6693 r
->ws
->old_r
= r
->ws
->r
;
6695 XDestroyWindow(display
, r
->bar_window
);
6696 TAILQ_REMOVE(&s
->rl
, r
, entry
);
6697 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
6701 /* search old regions for one to reuse */
6703 /* size + location match */
6704 TAILQ_FOREACH(r
, &s
->orl
, entry
)
6705 if (X(r
) == x
&& Y(r
) == y
&&
6706 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
6710 TAILQ_FOREACH(r
, &s
->orl
, entry
)
6711 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
6715 TAILQ_REMOVE(&s
->orl
, r
, entry
);
6716 /* try to use old region's workspace */
6717 if (r
->ws
->r
== NULL
)
6720 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
6721 err(1, "new_region: calloc: failed to allocate memory "
6724 /* if we don't have a workspace already, find one */
6726 for (i
= 0; i
< workspace_limit
; i
++)
6727 if (s
->ws
[i
].r
== NULL
) {
6734 errx(1, "new_region: no free workspaces");
6745 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
6751 #ifdef SWM_XRR_HAS_CRTC
6753 XRRScreenResources
*sr
;
6756 #endif /* SWM_XRR_HAS_CRTC */
6757 struct swm_region
*r
;
6760 if (i
>= ScreenCount(display
))
6761 errx(1, "scan_xrandr: invalid screen");
6763 /* remove any old regions */
6764 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
6765 r
->ws
->old_r
= r
->ws
->r
= NULL
;
6766 XDestroyWindow(display
, r
->bar_window
);
6767 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
6768 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
6772 /* map virtual screens onto physical screens */
6773 #ifdef SWM_XRR_HAS_CRTC
6774 if (xrandr_support
) {
6775 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
6777 new_region(&screens
[i
], 0, 0,
6778 DisplayWidth(display
, i
),
6779 DisplayHeight(display
, i
));
6783 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
6784 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
6785 if (ci
->noutput
== 0)
6788 if (ci
!= NULL
&& ci
->mode
== None
)
6789 new_region(&screens
[i
], 0, 0,
6790 DisplayWidth(display
, i
),
6791 DisplayHeight(display
, i
));
6793 new_region(&screens
[i
],
6794 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
6797 XRRFreeCrtcInfo(ci
);
6798 XRRFreeScreenResources(sr
);
6800 #endif /* SWM_XRR_HAS_CRTC */
6802 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
6803 DisplayHeight(display
, i
));
6808 screenchange(XEvent
*e
) {
6809 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
6810 struct swm_region
*r
;
6813 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%lx\n", xe
->root
);
6815 if (!XRRUpdateConfiguration(e
))
6818 /* silly event doesn't include the screen index */
6819 for (i
= 0; i
< ScreenCount(display
); i
++)
6820 if (screens
[i
].root
== xe
->root
)
6822 if (i
>= ScreenCount(display
))
6823 errx(1, "screenchange: screen not found");
6825 /* brute force for now, just re-enumerate the regions */
6828 /* add bars to all regions */
6829 for (i
= 0; i
< ScreenCount(display
); i
++)
6830 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6833 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6834 drain_enter_notify();
6840 Window d1
, d2
, *wins
= NULL
;
6841 XWindowAttributes wa
;
6846 for (i
= 0; i
< ScreenCount(display
); i
++) {
6847 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
6850 /* attach windows to a region */
6851 /* normal windows */
6852 for (j
= 0; j
< no
; j
++) {
6853 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6854 wa
.override_redirect
||
6855 XGetTransientForHint(display
, wins
[j
], &d1
))
6858 state
= getstate(wins
[j
]);
6859 manage
= state
== IconicState
;
6860 if (wa
.map_state
== IsViewable
|| manage
)
6861 manage_window(wins
[j
]);
6863 /* transient windows */
6864 for (j
= 0; j
< no
; j
++) {
6865 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6866 wa
.override_redirect
)
6869 state
= getstate(wins
[j
]);
6870 manage
= state
== IconicState
;
6871 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
6873 manage_window(wins
[j
]);
6886 int errorbase
, major
, minor
;
6887 struct workspace
*ws
;
6890 if ((screens
= calloc(ScreenCount(display
),
6891 sizeof(struct swm_screen
))) == NULL
)
6892 err(1, "setup_screens: calloc: failed to allocate memory for "
6895 /* initial Xrandr setup */
6896 xrandr_support
= XRRQueryExtension(display
,
6897 &xrandr_eventbase
, &errorbase
);
6899 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
6902 /* map physical screens */
6903 for (i
= 0; i
< ScreenCount(display
); i
++) {
6904 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
6906 TAILQ_INIT(&screens
[i
].rl
);
6907 TAILQ_INIT(&screens
[i
].orl
);
6908 screens
[i
].root
= RootWindow(display
, i
);
6910 /* set default colors */
6911 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
6912 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
6913 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
6914 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
6915 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
6917 /* create graphics context on screen with default font color */
6918 screens
[i
].bar_gc
= XCreateGC(display
, screens
[i
].root
, 0,
6921 XSetForeground(display
, screens
[i
].bar_gc
,
6922 screens
[i
].c
[SWM_S_COLOR_BAR_FONT
].color
);
6924 /* set default cursor */
6925 XDefineCursor(display
, screens
[i
].root
,
6926 XCreateFontCursor(display
, XC_left_ptr
));
6928 /* init all workspaces */
6929 /* XXX these should be dynamically allocated too */
6930 for (j
= 0; j
< workspace_limit
; j
++) {
6931 ws
= &screens
[i
].ws
[j
];
6937 TAILQ_INIT(&ws
->winlist
);
6938 TAILQ_INIT(&ws
->unmanagedlist
);
6940 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
6941 if (layouts
[k
].l_config
!= NULL
)
6942 layouts
[k
].l_config(ws
,
6943 SWM_ARG_ID_STACKINIT
);
6944 ws
->cur_layout
= &layouts
[0];
6945 ws
->cur_layout
->l_string(ws
);
6951 XRRSelectInput(display
, screens
[i
].root
,
6952 RRScreenChangeNotifyMask
);
6959 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
6960 err(1, "setup_globals: strdup: failed to allocate memory.");
6962 if ((spawn_term
[0] = strdup("xterm")) == NULL
)
6963 err(1, "setup_globals: strdup: failed to allocate memory.");
6965 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
6966 err(1, "setup_globals: strdup: failed to allocate memory.");
6973 Atom netwmcheck
, netwmname
, utf8_string
;
6976 /* work around sun jdk bugs, code from wmname */
6977 netwmcheck
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
6978 netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
6979 utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6980 for (i
= 0; i
< ScreenCount(display
); i
++) {
6981 root
= screens
[i
].root
;
6982 win
= XCreateSimpleWindow(display
,root
, 0, 0, 1, 1, 0,
6983 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
,
6984 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
);
6986 XChangeProperty(display
, root
, netwmcheck
, XA_WINDOW
, 32,
6987 PropModeReplace
, (unsigned char *)&win
,1);
6988 XChangeProperty(display
, win
, netwmcheck
, XA_WINDOW
, 32,
6989 PropModeReplace
, (unsigned char *)&win
,1);
6990 XChangeProperty(display
, win
, netwmname
, utf8_string
, 8,
6991 PropModeReplace
, (unsigned char*)"LG3D", strlen("LG3D"));
6996 main(int argc
, char *argv
[])
6998 struct swm_region
*r
, *rr
;
6999 struct ws_win
*winfocus
= NULL
;
7002 char conf
[PATH_MAX
], *cfile
= NULL
;
7007 struct sigaction sact
;
7010 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
7011 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
7013 warnx("no locale support");
7015 if (!X_HAVE_UTF8_STRING
)
7016 warnx("no UTF-8 support");
7018 if (!(display
= XOpenDisplay(0)))
7019 errx(1, "can not open display");
7022 errx(1, "other wm running");
7024 /* handle some signals */
7025 bzero(&sact
, sizeof(sact
));
7026 sigemptyset(&sact
.sa_mask
);
7028 sact
.sa_handler
= sighdlr
;
7029 sigaction(SIGINT
, &sact
, NULL
);
7030 sigaction(SIGQUIT
, &sact
, NULL
);
7031 sigaction(SIGTERM
, &sact
, NULL
);
7032 sigaction(SIGHUP
, &sact
, NULL
);
7034 sact
.sa_handler
= sighdlr
;
7035 sact
.sa_flags
= SA_NOCLDSTOP
;
7036 sigaction(SIGCHLD
, &sact
, NULL
);
7038 astate
= XInternAtom(display
, "WM_STATE", False
);
7039 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
7040 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
7041 takefocus
= XInternAtom(display
, "WM_TAKE_FOCUS", False
);
7042 a_wmname
= XInternAtom(display
, "WM_NAME", False
);
7043 a_netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
7044 a_utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
7045 a_string
= XInternAtom(display
, "STRING", False
);
7046 a_swm_iconic
= XInternAtom(display
, "_SWM_ICONIC", False
);
7048 /* look for local and global conf file */
7049 pwd
= getpwuid(getuid());
7051 errx(1, "invalid user: %d", getuid());
7060 for (i
= 0; ; i
++) {
7065 snprintf(conf
, sizeof conf
, "%s/.%s",
7066 pwd
->pw_dir
, SWM_CONF_FILE
);
7070 snprintf(conf
, sizeof conf
, "/etc/%s",
7075 snprintf(conf
, sizeof conf
, "%s/.%s",
7076 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
7080 snprintf(conf
, sizeof conf
, "/etc/%s",
7087 if (strlen(conf
) && stat(conf
, &sb
) != -1)
7088 if (S_ISREG(sb
.st_mode
)) {
7095 /* load conf (if any) and refresh font color in bar graphics contexts */
7096 if (cfile
&& conf_load(cfile
, SWM_CONF_DEFAULT
) == 0)
7097 for (i
= 0; i
< ScreenCount(display
); ++i
)
7098 XSetForeground(display
, screens
[i
].bar_gc
,
7099 screens
[i
].c
[SWM_S_COLOR_BAR_FONT
].color
);
7102 /* set some values to work around bad programs */
7104 /* grab existing windows (before we build the bars) */
7107 if (getenv("SWM_STARTED") == NULL
)
7108 setenv("SWM_STARTED", "YES", 1);
7110 /* setup all bars */
7111 for (i
= 0; i
< ScreenCount(display
); i
++)
7112 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
7113 if (winfocus
== NULL
)
7114 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
7122 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7123 drain_enter_notify();
7125 xfd
= ConnectionNumber(display
);
7127 while (XPending(display
)) {
7128 XNextEvent(display
, &e
);
7131 if (e
.type
< LASTEvent
) {
7132 DNPRINTF(SWM_D_EVENTQ
,"XEvent: handled: %s, "
7133 "window: 0x%lx, type: %s (%d), %d remaining"
7134 "\n", YESNO(handler
[e
.type
]),
7135 e
.xany
.window
, geteventname(&e
),
7136 e
.type
, QLength(display
));
7138 if (handler
[e
.type
])
7139 handler
[e
.type
](&e
);
7141 DNPRINTF(SWM_D_EVENTQ
, "XRandr Event: window: "
7142 "0x%lx, type: %s (%d)\n", e
.xany
.window
,
7143 xrandr_geteventname(&e
), e
.type
);
7145 switch (e
.type
- xrandr_eventbase
) {
7146 case RRScreenChangeNotify
:
7155 /* if we are being restarted go focus on first window */
7157 rr
= winfocus
->ws
->r
;
7159 /* not a visible window */
7163 /* move pointer to first screen if multi screen */
7164 if (ScreenCount(display
) > 1 || outputs
> 1)
7165 XWarpPointer(display
, None
, rr
->s
[0].root
,
7167 Y(rr
) + (bar_enabled
? bar_height
: 0));
7169 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7179 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7181 DNPRINTF(SWM_D_MISC
, "select failed");
7182 if (restart_wm
== 1)
7183 restart(NULL
, NULL
);
7184 if (search_resp
== 1)
7197 for (i
= 0; i
< ScreenCount(display
); ++i
)
7198 if (screens
[i
].bar_gc
!= NULL
)
7199 XFreeGC(display
, screens
[i
].bar_gc
);
7201 XFreeFontSet(display
, bar_fs
);
7202 XCloseDisplay(display
);