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/XKBlib.h>
92 #include <X11/Xatom.h>
93 #include <X11/Xlib-xcb.h>
94 #include <X11/Xproto.h>
95 #include <X11/Xutil.h>
96 #include <X11/extensions/Xrandr.h>
97 #include <X11/extensions/XTest.h>
105 #ifdef SPECTRWM_BUILDSTR
106 static const char *buildstr
= SPECTRWM_BUILDSTR
;
108 static const char *buildstr
= SPECTRWM_VERSION
;
112 # error XRandR versions less than 1.0 are not supported
117 #define SWM_XRR_HAS_CRTC
121 /*#define SWM_DEBUG*/
123 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
124 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
125 #define SWM_D_MISC 0x0001
126 #define SWM_D_EVENT 0x0002
127 #define SWM_D_WS 0x0004
128 #define SWM_D_FOCUS 0x0008
129 #define SWM_D_MOVE 0x0010
130 #define SWM_D_STACK 0x0020
131 #define SWM_D_MOUSE 0x0040
132 #define SWM_D_PROP 0x0080
133 #define SWM_D_CLASS 0x0100
134 #define SWM_D_KEY 0x0200
135 #define SWM_D_QUIRK 0x0400
136 #define SWM_D_SPAWN 0x0800
137 #define SWM_D_EVENTQ 0x1000
138 #define SWM_D_CONF 0x2000
139 #define SWM_D_BAR 0x4000
141 u_int32_t swm_debug
= 0
159 #define DPRINTF(x...)
160 #define DNPRINTF(n,x...)
163 #define LENGTH(x) (sizeof x / sizeof x[0])
164 #define MODKEY Mod1Mask
165 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
166 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
167 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
168 #define SWM_PROPLEN (16)
169 #define SWM_FUNCNAME_LEN (32)
170 #define SWM_KEYS_LEN (255)
171 #define SWM_QUIRK_LEN (64)
172 #define X(r) (r)->g.x
173 #define Y(r) (r)->g.y
174 #define WIDTH(r) (r)->g.w
175 #define HEIGHT(r) (r)->g.h
176 #define BORDER(w) (w->bordered ? border_width : 0)
177 #define MAX_X(r) ((r)->g.x + (r)->g.w)
178 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
179 #define SH_MIN(w) (w)->sh_mask & PMinSize
180 #define SH_MIN_W(w) (w)->sh.min_width
181 #define SH_MIN_H(w) (w)->sh.min_height
182 #define SH_MAX(w) (w)->sh_mask & PMaxSize
183 #define SH_MAX_W(w) (w)->sh.max_width
184 #define SH_MAX_H(w) (w)->sh.max_height
185 #define SH_INC(w) (w)->sh_mask & PResizeInc
186 #define SH_INC_W(w) (w)->sh.width_inc
187 #define SH_INC_H(w) (w)->sh.height_inc
188 #define SWM_MAX_FONT_STEPS (3)
189 #define WINID(w) ((w) ? (w)->id : 0)
190 #define YESNO(x) ((x) ? "yes" : "no")
192 #define SWM_FOCUS_DEFAULT (0)
193 #define SWM_FOCUS_SYNERGY (1)
194 #define SWM_FOCUS_FOLLOW (2)
196 #define SWM_CONF_DEFAULT (0)
197 #define SWM_CONF_KEYMAPPING (1)
200 #define SWM_LIB "/usr/local/lib/libswmhack.so"
213 volatile sig_atomic_t running
= 1;
214 volatile sig_atomic_t restart_wm
= 0;
216 int last_focus_event
= FocusOut
;
217 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
221 int xrandr_eventbase
;
222 unsigned int numlockmask
= 0;
224 xcb_connection_t
*conn
;
227 int cycle_visible
= 0;
229 int font_adjusted
= 0;
230 unsigned int mod_key
= MODKEY
;
233 struct swm_region
*search_r
;
234 int select_list_pipe
[2];
235 int select_resp_pipe
[2];
237 volatile sig_atomic_t search_resp
;
238 int search_resp_action
;
240 struct search_window
{
241 TAILQ_ENTRY(search_window
) entry
;
247 TAILQ_HEAD(search_winlist
, search_window
);
248 struct search_winlist search_wl
;
253 SWM_SEARCH_UNICONIFY
,
254 SWM_SEARCH_NAME_WORKSPACE
,
255 SWM_SEARCH_SEARCH_WORKSPACE
,
256 SWM_SEARCH_SEARCH_WINDOW
259 #define SWM_STACK_TOP (0)
260 #define SWM_STACK_BOTTOM (1)
261 #define SWM_STACK_ABOVE (2)
262 #define SWM_STACK_BELOW (3)
265 double dialog_ratio
= 0.6;
267 #define SWM_BAR_MAX (256)
268 #define SWM_BAR_JUSTIFY_LEFT (0)
269 #define SWM_BAR_JUSTIFY_CENTER (1)
270 #define SWM_BAR_JUSTIFY_RIGHT (2)
271 #define SWM_BAR_OFFSET (4)
272 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
273 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
274 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
275 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
276 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
278 #ifdef X_HAVE_UTF8_STRING
279 #define DRAWSTRING(x...) Xutf8DrawString(x)
281 #define DRAWSTRING(x...) XmbDrawString(x)
284 char *bar_argv
[] = { NULL
, NULL
};
286 unsigned char bar_ext
[SWM_BAR_MAX
];
287 char bar_vertext
[SWM_BAR_MAX
];
289 sig_atomic_t bar_alarm
= 0;
292 int bar_border_width
= 1;
293 int bar_at_bottom
= 0;
295 int bar_extra_running
= 0;
298 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
299 char *bar_format
= NULL
;
300 int stack_enabled
= 1;
301 int clock_enabled
= 1;
302 int urgent_enabled
= 0;
303 char *clock_format
= NULL
;
304 int title_name_enabled
= 0;
305 int title_class_enabled
= 0;
306 int window_name_enabled
= 0;
307 int focus_mode
= SWM_FOCUS_DEFAULT
;
308 int focus_close
= SWM_STACK_BELOW
;
309 int focus_close_wrap
= 1;
310 int focus_default
= SWM_STACK_TOP
;
311 int spawn_position
= SWM_STACK_TOP
;
312 int disable_border
= 0;
313 int border_width
= 1;
314 int verbose_layout
= 0;
317 XFontSetExtents
*bar_fs_extents
;
321 #define SWM_MENU_FN (2)
322 #define SWM_MENU_NB (4)
323 #define SWM_MENU_NF (6)
324 #define SWM_MENU_SB (8)
325 #define SWM_MENU_SF (10)
327 /* layout manager data */
328 struct swm_geometry
{
341 struct swm_geometry g
;
344 /* virtual "screens" */
346 TAILQ_ENTRY(swm_region
) entry
;
347 struct swm_geometry g
;
348 struct workspace
*ws
; /* current workspace on this region */
349 struct workspace
*ws_prior
; /* prior workspace on this region */
350 struct swm_screen
*s
; /* screen idx */
353 TAILQ_HEAD(swm_region_list
, swm_region
);
356 TAILQ_ENTRY(ws_win
) entry
;
359 struct ws_win
*child_trans
; /* transient child window */
360 struct swm_geometry g
; /* current geometry */
361 struct swm_geometry g_float
; /* region coordinates */
362 int g_floatvalid
; /* g_float geometry validity */
363 int floatmaxed
; /* whether maxed by max_stack */
368 unsigned int ewmh_flags
;
369 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
375 unsigned long quirks
;
376 struct workspace
*ws
; /* always valid */
377 struct swm_screen
*s
; /* always valid, never changes */
378 XWindowAttributes wa
;
384 TAILQ_HEAD(ws_win_list
, ws_win
);
388 TAILQ_ENTRY(pid_e
) entry
;
392 TAILQ_HEAD(pid_list
, pid_e
);
393 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
395 /* layout handlers */
397 void vertical_config(struct workspace
*, int);
398 void vertical_stack(struct workspace
*, struct swm_geometry
*);
399 void horizontal_config(struct workspace
*, int);
400 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
401 void max_stack(struct workspace
*, struct swm_geometry
*);
402 void plain_stacker(struct workspace
*);
403 void fancy_stacker(struct workspace
*);
405 struct ws_win
*find_window(Window
);
407 void grabbuttons(struct ws_win
*, int);
408 void new_region(struct swm_screen
*, int, int, int, int);
409 void unmanage_window(struct ws_win
*);
410 long getstate(Window
);
412 int conf_load(char *, int);
415 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
416 void (*l_config
)(struct workspace
*, int);
418 #define SWM_L_FOCUSPREV (1<<0)
419 #define SWM_L_MAPONFOCUS (1<<1)
420 void (*l_string
)(struct workspace
*);
422 /* stack, configure */
423 { vertical_stack
, vertical_config
, 0, plain_stacker
},
424 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
426 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
427 { NULL
, NULL
, 0, NULL
},
430 /* position of max_stack mode in the layouts array, index into layouts! */
431 #define SWM_V_STACK (0)
432 #define SWM_H_STACK (1)
433 #define SWM_MAX_STACK (2)
435 #define SWM_H_SLICE (32)
436 #define SWM_V_SLICE (32)
438 /* define work spaces */
440 int idx
; /* workspace index */
441 char *name
; /* workspace name */
442 int always_raise
; /* raise windows on focus */
443 struct layout
*cur_layout
; /* current layout handlers */
444 struct ws_win
*focus
; /* may be NULL */
445 struct ws_win
*focus_prev
; /* may be NULL */
446 struct swm_region
*r
; /* may be NULL */
447 struct swm_region
*old_r
; /* may be NULL */
448 struct ws_win_list winlist
; /* list of windows in ws */
449 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
450 char stacker
[10]; /* display stacker and layout */
454 int horizontal_msize
;
456 int horizontal_stacks
;
467 SWM_S_COLOR_BAR_BORDER
,
468 SWM_S_COLOR_BAR_FONT
,
474 /* physical screen mapping */
475 #define SWM_WS_MAX (22) /* hard limit */
476 int workspace_limit
= 10; /* soft limit */
479 int idx
; /* screen index */
480 struct swm_region_list rl
; /* list of regions on this screen */
481 struct swm_region_list orl
; /* list of old regions */
483 struct workspace ws
[SWM_WS_MAX
];
489 } c
[SWM_S_COLOR_MAX
];
493 struct swm_screen
*screens
;
495 /* args to functions */
498 #define SWM_ARG_ID_FOCUSNEXT (0)
499 #define SWM_ARG_ID_FOCUSPREV (1)
500 #define SWM_ARG_ID_FOCUSMAIN (2)
501 #define SWM_ARG_ID_FOCUSCUR (4)
502 #define SWM_ARG_ID_SWAPNEXT (10)
503 #define SWM_ARG_ID_SWAPPREV (11)
504 #define SWM_ARG_ID_SWAPMAIN (12)
505 #define SWM_ARG_ID_MOVELAST (13)
506 #define SWM_ARG_ID_MASTERSHRINK (20)
507 #define SWM_ARG_ID_MASTERGROW (21)
508 #define SWM_ARG_ID_MASTERADD (22)
509 #define SWM_ARG_ID_MASTERDEL (23)
510 #define SWM_ARG_ID_FLIPLAYOUT (24)
511 #define SWM_ARG_ID_STACKRESET (30)
512 #define SWM_ARG_ID_STACKINIT (31)
513 #define SWM_ARG_ID_CYCLEWS_UP (40)
514 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
515 #define SWM_ARG_ID_CYCLESC_UP (42)
516 #define SWM_ARG_ID_CYCLESC_DOWN (43)
517 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
518 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
519 #define SWM_ARG_ID_STACKINC (50)
520 #define SWM_ARG_ID_STACKDEC (51)
521 #define SWM_ARG_ID_SS_ALL (60)
522 #define SWM_ARG_ID_SS_WINDOW (61)
523 #define SWM_ARG_ID_DONTCENTER (70)
524 #define SWM_ARG_ID_CENTER (71)
525 #define SWM_ARG_ID_KILLWINDOW (80)
526 #define SWM_ARG_ID_DELETEWINDOW (81)
527 #define SWM_ARG_ID_WIDTHGROW (90)
528 #define SWM_ARG_ID_WIDTHSHRINK (91)
529 #define SWM_ARG_ID_HEIGHTGROW (92)
530 #define SWM_ARG_ID_HEIGHTSHRINK (93)
531 #define SWM_ARG_ID_MOVEUP (100)
532 #define SWM_ARG_ID_MOVEDOWN (101)
533 #define SWM_ARG_ID_MOVELEFT (102)
534 #define SWM_ARG_ID_MOVERIGHT (103)
538 void focus(struct swm_region
*, union arg
*);
539 void focus_magic(struct ws_win
*);
543 TAILQ_ENTRY(quirk
) entry
;
547 #define SWM_Q_FLOAT (1<<0) /* float this window */
548 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
549 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
550 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
551 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
552 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
554 TAILQ_HEAD(quirk_list
, quirk
);
555 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
558 * Supported EWMH hints should be added to
559 * both the enum and the ewmh array
564 _NET_MOVERESIZE_WINDOW
,
565 _NET_WM_ACTION_CLOSE
,
566 _NET_WM_ACTION_FULLSCREEN
,
568 _NET_WM_ACTION_RESIZE
,
569 _NET_WM_ALLOWED_ACTIONS
,
572 _NET_WM_STATE_FULLSCREEN
,
573 _NET_WM_STATE_HIDDEN
,
574 _NET_WM_STATE_MAXIMIZED_HORZ
,
575 _NET_WM_STATE_MAXIMIZED_VERT
,
576 _NET_WM_STATE_SKIP_PAGER
,
577 _NET_WM_STATE_SKIP_TASKBAR
,
579 _NET_WM_WINDOW_TYPE_DIALOG
,
580 _NET_WM_WINDOW_TYPE_DOCK
,
581 _NET_WM_WINDOW_TYPE_NORMAL
,
582 _NET_WM_WINDOW_TYPE_SPLASH
,
583 _NET_WM_WINDOW_TYPE_TOOLBAR
,
584 _NET_WM_WINDOW_TYPE_UTILITY
,
585 _SWM_WM_STATE_MANUAL
,
592 } ewmh
[SWM_EWMH_HINT_MAX
] = {
593 /* must be in same order as in the enum */
594 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
595 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
596 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
597 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
598 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
599 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
600 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
601 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
602 {"_NET_WM_STATE", XCB_ATOM_NONE
},
603 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
604 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
605 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
606 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
607 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
608 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
609 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
610 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
611 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
612 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
613 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
614 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
615 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
616 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
617 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
620 void store_float_geom(struct ws_win
*, struct swm_region
*);
621 int floating_toggle_win(struct ws_win
*);
622 void constrain_window(struct ws_win
*, struct swm_region
*, int);
623 void update_window(struct ws_win
*);
624 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
625 unsigned char *get_win_name(Window
);
628 get_property(Window id
, Atom atom
, long count
, Atom type
, unsigned long *nitems
,
629 unsigned long *nbytes
, unsigned char **data
)
632 unsigned long *nbytes_ret
, *nitems_ret
;
633 unsigned long nbytes_tmp
, nitems_tmp
;
636 nbytes_ret
= nbytes
!= NULL
? nbytes
: &nbytes_tmp
;
637 nitems_ret
= nitems
!= NULL
? nitems
: &nitems_tmp
;
639 status
= XGetWindowProperty(display
, id
, atom
, 0L, count
, False
, type
,
640 &real
, &format
, nitems_ret
, nbytes_ret
, data
);
642 if (status
!= Success
)
651 update_iconic(struct ws_win
*win
, int newv
)
655 xcb_intern_atom_cookie_t c
;
656 xcb_intern_atom_reply_t
*r
;
660 c
= xcb_intern_atom(conn
, False
, strlen("_SWM_ICONIC"), "_SWM_ICONIC");
661 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
669 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
670 iprop
, XCB_ATOM_INTEGER
, 32, 1, &v
);
672 xcb_delete_property(conn
, win
->id
, iprop
);
676 get_iconic(struct ws_win
*win
)
678 int32_t v
= 0, *vtmp
;
680 xcb_intern_atom_cookie_t c
;
681 xcb_intern_atom_reply_t
*r
;
682 xcb_get_property_cookie_t pc
;
683 xcb_get_property_reply_t
*pr
;
685 c
= xcb_intern_atom(conn
, False
, strlen("_SWM_ICONIC"), "_SWM_ICONIC");
686 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
693 pc
= xcb_get_property(conn
, False
, win
->id
, iprop
, XCB_ATOM_INTEGER
,
695 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
698 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
700 vtmp
= xcb_get_property_value(pr
);
712 xcb_intern_atom_cookie_t c
;
713 xcb_intern_atom_reply_t
*r
;
714 int i
, j
, num_screens
;
716 c
= xcb_intern_atom(conn
, False
, strlen("_NET_SUPPORTED"),
718 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
724 for (i
= 0; i
< LENGTH(ewmh
); i
++) {
725 c
= xcb_intern_atom(conn
, False
, strlen(ewmh
[i
].name
),
727 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
729 ewmh
[i
].atom
= r
->atom
;
734 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
735 for (i
= 0; i
< num_screens
; i
++) {
736 /* Support check window will be created by workaround(). */
738 /* Report supported atoms */
739 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
740 for (j
= 0; j
< LENGTH(ewmh
); j
++)
741 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
742 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
751 unsigned char *data
= NULL
;
753 Atom sup_check
, sup_list
;
756 sup_check
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
757 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
759 for (i
= 0; i
< ScreenCount(display
); i
++) {
760 /* Get the support check window and destroy it */
761 success
= get_property(screens
[i
].root
, sup_check
, 1, XA_WINDOW
,
766 XDestroyWindow(display
, id
);
767 XDeleteProperty(display
, screens
[i
].root
, sup_check
);
768 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
776 ewmh_autoquirk(struct ws_win
*win
)
779 unsigned long *data
= NULL
, n
;
782 success
= get_property(win
->id
, ewmh
[_NET_WM_WINDOW_TYPE
].atom
, (~0L),
783 XA_ATOM
, &n
, NULL
, (void *)&data
);
790 for (i
= 0; i
< n
; i
++) {
792 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
794 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
795 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
796 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
798 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
801 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
802 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
804 win
->quirks
= SWM_Q_FLOAT
;
812 #define SWM_EWMH_ACTION_COUNT_MAX (6)
813 #define EWMH_F_FULLSCREEN (1<<0)
814 #define EWMH_F_ABOVE (1<<1)
815 #define EWMH_F_HIDDEN (1<<2)
816 #define EWMH_F_SKIP_PAGER (1<<3)
817 #define EWMH_F_SKIP_TASKBAR (1<<4)
818 #define SWM_F_MANUAL (1<<5)
821 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
829 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%lx, "
830 "fullscreen %s\n", win
->id
, YESNO(fs
));
833 if (!win
->g_floatvalid
)
834 store_float_geom(win
, win
->ws
->r
);
836 win
->g
= win
->ws
->r
->g
;
839 if (win
->g_floatvalid
) {
840 /* refloat at last floating relative position */
841 win
->g
= win
->g_float
;
842 X(win
) += X(win
->ws
->r
);
843 Y(win
) += Y(win
->ws
->r
);
851 ewmh_update_actions(struct ws_win
*win
)
853 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
859 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
862 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
863 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
866 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
867 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1,
871 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
872 #define _NET_WM_STATE_ADD 1 /* add/set property */
873 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
876 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
878 unsigned int mask
= 0;
879 unsigned int changed
= 0;
880 unsigned int orig_flags
;
885 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
886 mask
= EWMH_F_FULLSCREEN
;
887 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
889 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
891 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
892 mask
= EWMH_F_SKIP_PAGER
;
893 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
894 mask
= EWMH_F_SKIP_TASKBAR
;
897 orig_flags
= win
->ewmh_flags
;
900 case _NET_WM_STATE_REMOVE
:
901 win
->ewmh_flags
&= ~mask
;
903 case _NET_WM_STATE_ADD
:
904 win
->ewmh_flags
|= mask
;
906 case _NET_WM_STATE_TOGGLE
:
907 win
->ewmh_flags
^= mask
;
911 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
913 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
915 if (!floating_toggle_win(win
))
916 win
->ewmh_flags
= orig_flags
; /* revert */
917 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
919 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
920 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
922 if (!ewmh_set_win_fullscreen(win
,
923 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
924 win
->ewmh_flags
= orig_flags
; /* revert */
926 XDeleteProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
928 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
929 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
930 XA_ATOM
, 32, PropModeAppend
,
931 (unsigned char *)&ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
, 1);
932 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
933 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
934 XA_ATOM
, 32, PropModeAppend
,
935 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
, 1);
936 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
937 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
938 XA_ATOM
, 32, PropModeAppend
,
939 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
, 1);
940 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
941 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
942 XA_ATOM
, 32, PropModeAppend
,
943 (unsigned char *)&ewmh
[_NET_WM_STATE_ABOVE
].atom
, 1);
944 if (win
->ewmh_flags
& SWM_F_MANUAL
)
945 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
946 XA_ATOM
, 32, PropModeAppend
,
947 (unsigned char *)&ewmh
[_SWM_WM_STATE_MANUAL
].atom
, 1);
951 ewmh_get_win_state(struct ws_win
*win
)
962 win
->ewmh_flags
|= EWMH_F_ABOVE
;
964 win
->ewmh_flags
|= SWM_F_MANUAL
;
966 success
= get_property(win
->id
, ewmh
[_NET_WM_STATE
].atom
,
967 (~0L), XA_ATOM
, &n
, NULL
, (void *)&states
);
972 for (i
= 0; i
< n
; i
++)
973 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
981 geteventname(XEvent
*e
)
993 name
= "ButtonPress";
996 name
= "ButtonRelease";
999 name
= "MotionNotify";
1002 name
= "EnterNotify";
1005 name
= "LeaveNotify";
1014 name
= "KeymapNotify";
1019 case GraphicsExpose
:
1020 name
= "GraphicsExpose";
1025 case VisibilityNotify
:
1026 name
= "VisibilityNotify";
1029 name
= "CreateNotify";
1032 name
= "DestroyNotify";
1035 name
= "UnmapNotify";
1041 name
= "MapRequest";
1043 case ReparentNotify
:
1044 name
= "ReparentNotify";
1046 case ConfigureNotify
:
1047 name
= "ConfigureNotify";
1049 case ConfigureRequest
:
1050 name
= "ConfigureRequest";
1053 name
= "GravityNotify";
1056 name
= "ResizeRequest";
1058 case CirculateNotify
:
1059 name
= "CirculateNotify";
1061 case CirculateRequest
:
1062 name
= "CirculateRequest";
1064 case PropertyNotify
:
1065 name
= "PropertyNotify";
1067 case SelectionClear
:
1068 name
= "SelectionClear";
1070 case SelectionRequest
:
1071 name
= "SelectionRequest";
1073 case SelectionNotify
:
1074 name
= "SelectionNotify";
1076 case ColormapNotify
:
1077 name
= "ColormapNotify";
1080 name
= "ClientMessage";
1083 name
= "MappingNotify";
1093 xrandr_geteventname(XEvent
*e
)
1097 switch(e
->type
- xrandr_eventbase
) {
1098 case RRScreenChangeNotify
:
1099 name
= "RRScreenChangeNotify";
1109 dumpwins(struct swm_region
*r
, union arg
*args
)
1113 XWindowAttributes wa
;
1115 if (r
->ws
== NULL
) {
1116 warnx("dumpwins: invalid workspace");
1120 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1122 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1123 state
= getstate(win
->id
);
1124 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1125 warnx("window: 0x%lx, failed XGetWindowAttributes",
1127 warnx("window: 0x%lx, map_state: %d, state: %d, "
1128 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1132 warnx("===== unmanaged window list =====");
1133 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1134 state
= getstate(win
->id
);
1135 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1136 warnx("window: 0x%lx, failed XGetWindowAttributes",
1138 warnx("window: 0x%lx, map_state: %d, state: %d, "
1139 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1143 warnx("=================================");
1147 dumpwins(struct swm_region
*r
, union arg
*args
)
1150 #endif /* SWM_DEBUG */
1152 void expose(XEvent
*);
1153 void keypress(XEvent
*);
1154 void buttonpress(XEvent
*);
1155 void configurerequest(XEvent
*);
1156 void configurenotify(XEvent
*);
1157 void destroynotify(XEvent
*);
1158 void enternotify(XEvent
*);
1159 void focusevent(XEvent
*);
1160 void mapnotify(XEvent
*);
1161 void mappingnotify(XEvent
*);
1162 void maprequest(XEvent
*);
1163 void propertynotify(XEvent
*);
1164 void unmapnotify(XEvent
*);
1165 void visibilitynotify(XEvent
*);
1166 void clientmessage(XEvent
*);
1168 void (*handler
[LASTEvent
])(XEvent
*) = {
1170 [KeyPress
] = keypress
,
1171 [ButtonPress
] = buttonpress
,
1172 [ConfigureRequest
] = configurerequest
,
1173 [ConfigureNotify
] = configurenotify
,
1174 [DestroyNotify
] = destroynotify
,
1175 [EnterNotify
] = enternotify
,
1176 [FocusIn
] = focusevent
,
1177 [FocusOut
] = focusevent
,
1178 [MapNotify
] = mapnotify
,
1179 [MappingNotify
] = mappingnotify
,
1180 [MapRequest
] = maprequest
,
1181 [PropertyNotify
] = propertynotify
,
1182 [UnmapNotify
] = unmapnotify
,
1183 [VisibilityNotify
] = visibilitynotify
,
1184 [ClientMessage
] = clientmessage
,
1190 int saved_errno
, status
;
1193 saved_errno
= errno
;
1197 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1202 if (errno
!= ECHILD
)
1203 warn("sighdlr: waitpid");
1204 #endif /* SWM_DEBUG */
1207 if (pid
== searchpid
)
1211 if (WIFEXITED(status
)) {
1212 if (WEXITSTATUS(status
) != 0)
1213 warnx("sighdlr: child exit status: %d",
1214 WEXITSTATUS(status
));
1216 warnx("sighdlr: child is terminated "
1218 #endif /* SWM_DEBUG */
1232 errno
= saved_errno
;
1238 struct pid_e
*p
= NULL
;
1240 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1245 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1254 name_to_color(char *colorname
)
1258 XColor screen_def
, exact_def
;
1259 unsigned long result
= 0;
1260 char cname
[32] = "#";
1262 cmap
= DefaultColormap(display
, screens
[0].idx
);
1263 status
= XAllocNamedColor(display
, cmap
, colorname
,
1264 &screen_def
, &exact_def
);
1266 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1267 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
1271 result
= screen_def
.pixel
;
1273 warnx("color '%s' not found", colorname
);
1279 setscreencolor(char *val
, int i
, int c
)
1283 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1284 if (i
> 0 && i
<= num_screens
) {
1285 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1286 free(screens
[i
- 1].c
[c
].name
);
1287 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1289 } else if (i
== -1) {
1290 for (i
= 0; i
< num_screens
; i
++) {
1291 screens
[i
].c
[c
].color
= name_to_color(val
);
1292 free(screens
[i
].c
[c
].name
);
1293 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1297 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1302 fancy_stacker(struct workspace
*ws
)
1304 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1305 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1306 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1307 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1308 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1309 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1310 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1311 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1312 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1316 plain_stacker(struct workspace
*ws
)
1318 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1319 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1320 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1321 sizeof ws
->stacker
);
1322 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1323 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1324 sizeof ws
->stacker
);
1328 custom_region(char *val
)
1330 unsigned int sidx
, x
, y
, w
, h
;
1333 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1334 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1335 errx(1, "invalid custom region, "
1336 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1337 if (sidx
< 1 || sidx
> num_screens
)
1338 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1343 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1345 if (x
> DisplayWidth(display
, sidx
) ||
1346 y
> DisplayHeight(display
, sidx
) ||
1347 w
+ x
> DisplayWidth(display
, sidx
) ||
1348 h
+ y
> DisplayHeight(display
, sidx
)) {
1349 warnx("ignoring region %ux%u+%u+%u - not within screen "
1350 "boundaries (%ux%u)", w
, h
, x
, y
,
1351 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
1355 new_region(&screens
[sidx
], x
, y
, w
, h
);
1359 socket_setnonblock(int fd
)
1363 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1364 err(1, "fcntl F_GETFL");
1365 flags
|= O_NONBLOCK
;
1366 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1367 err(1, "fcntl F_SETFL");
1371 bar_print(struct swm_region
*r
, const char *s
)
1375 XRectangle ibox
, lbox
;
1378 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1380 switch (bar_justify
) {
1381 case SWM_BAR_JUSTIFY_LEFT
:
1384 case SWM_BAR_JUSTIFY_CENTER
:
1385 x
= (WIDTH(r
) - lbox
.width
) / 2;
1387 case SWM_BAR_JUSTIFY_RIGHT
:
1388 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1392 if (x
< SWM_BAR_OFFSET
)
1395 /* clear back buffer */
1396 XSetForeground(display
, r
->s
->bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1397 XFillRectangle(display
, r
->bar
->buffer
, r
->s
->bar_gc
, 0, 0,
1398 WIDTH(r
->bar
), HEIGHT(r
->bar
));
1400 /* draw back buffer */
1401 XSetForeground(display
, r
->s
->bar_gc
,
1402 r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
1403 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, r
->s
->bar_gc
,
1404 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1408 XCopyArea(display
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1409 WIDTH(r
->bar
), HEIGHT(r
->bar
), 0, 0);
1413 bar_extra_stop(void)
1417 bzero(bar_pipe
, sizeof bar_pipe
);
1420 kill(bar_pid
, SIGTERM
);
1423 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1428 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1430 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1432 if (r
->ws
->focus
->ch
.res_class
!= NULL
)
1433 strlcat(s
, r
->ws
->focus
->ch
.res_class
, sz
);
1437 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1439 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1441 if (r
->ws
->focus
->ch
.res_name
!= NULL
)
1442 strlcat(s
, r
->ws
->focus
->ch
.res_name
, sz
);
1446 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1448 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1451 bar_class_name(s
, sz
, r
);
1452 strlcat(s
, ":", sz
);
1453 bar_title_name(s
, sz
, r
);
1457 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1459 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1461 if (r
->ws
->focus
->floating
)
1462 strlcat(s
, "(f)", sz
);
1466 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1468 unsigned char *title
;
1470 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1472 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1475 strlcat(s
, (char *)title
, sz
);
1479 int urgent
[SWM_WS_MAX
];
1481 bar_urgent(char *s
, size_t sz
)
1483 XWMHints
*wmh
= NULL
;
1488 for (i
= 0; i
< workspace_limit
; i
++)
1491 for (i
= 0; i
< ScreenCount(display
); i
++)
1492 for (j
= 0; j
< workspace_limit
; j
++)
1493 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1494 wmh
= XGetWMHints(display
, win
->id
);
1498 if (wmh
->flags
& XUrgencyHint
)
1503 for (i
= 0; i
< workspace_limit
; i
++) {
1505 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1507 snprintf(b
, sizeof b
, "- ");
1513 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1515 if (r
== NULL
|| r
->ws
== NULL
)
1517 if (r
->ws
->name
!= NULL
)
1518 strlcat(s
, r
->ws
->name
, sz
);
1521 /* build the default bar format according to the defined enabled options */
1523 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1525 /* if format provided, just copy the buffers */
1526 if (bar_format
!= NULL
) {
1527 strlcpy(fmtnew
, fmtexp
, sz
);
1531 /* reset the output buffer */
1534 strlcat(fmtnew
, "+N:+I ", sz
);
1536 strlcat(fmtnew
, "+S", sz
);
1537 strlcat(fmtnew
, " ", sz
);
1539 /* only show the workspace name if there's actually one */
1540 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1541 strlcat(fmtnew
, "<+D>", sz
);
1542 strlcat(fmtnew
, "+3<", sz
);
1544 if (clock_enabled
) {
1545 strlcat(fmtnew
, fmtexp
, sz
);
1546 strlcat(fmtnew
, "+4<", sz
);
1549 /* bar_urgent already adds the space before the last asterisk */
1551 strlcat(fmtnew
, "* +U*+4<", sz
);
1553 if (title_class_enabled
) {
1554 strlcat(fmtnew
, "+C", sz
);
1555 if (title_name_enabled
== 0)
1556 strlcat(fmtnew
, "+4<", sz
);
1559 /* checks needed by the colon and floating strlcat(3) calls below */
1560 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1561 if (title_name_enabled
) {
1562 if (title_class_enabled
)
1563 strlcat(fmtnew
, ":", sz
);
1564 strlcat(fmtnew
, "+T+4<", sz
);
1566 if (window_name_enabled
) {
1567 if (r
->ws
->focus
->floating
)
1568 strlcat(fmtnew
, "+F ", sz
);
1569 strlcat(fmtnew
, "+64W ", sz
);
1573 /* finally add the action script output and the version */
1574 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1578 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1580 /* special case; no limit given, pad one space, instead */
1581 if (*limit
== sz
- 1)
1583 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1586 /* replaces the bar format character sequences (like in tmux(1)) */
1588 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1592 char tmp
[SWM_BAR_MAX
];
1596 /* reset strlcat(3) buffer */
1599 /* get number, if any */
1602 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1603 limit
= sizeof tmp
- 1;
1604 if (limit
<= 0 || limit
>= sizeof tmp
)
1605 limit
= sizeof tmp
- 1;
1607 /* there is nothing to replace (ie EOL) */
1614 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1617 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1620 bar_class_name(tmp
, sizeof tmp
, r
);
1623 bar_workspace_name(tmp
, sizeof tmp
, r
);
1626 bar_window_float(tmp
, sizeof tmp
, r
);
1629 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1632 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1635 bar_class_title_name(tmp
, sizeof tmp
, r
);
1638 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1641 bar_title_name(tmp
, sizeof tmp
, r
);
1644 bar_urgent(tmp
, sizeof tmp
);
1647 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1650 bar_window_name(tmp
, sizeof tmp
, r
);
1653 /* unknown character sequence; copy as-is */
1654 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1662 while (limit
-- > 0) {
1663 if (*offrep
>= sz
- 1)
1665 fmtrep
[(*offrep
)++] = *ptr
++;
1673 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1678 while (*fmt
!= '\0') {
1680 /* skip ordinary characters */
1683 fmtrep
[off
++] = *fmt
++;
1687 /* character sequence found; replace it */
1688 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1697 bar_fmt_expand(char *fmtexp
, size_t sz
)
1704 /* start by grabbing the current time and date */
1706 localtime_r(&tmt
, &tm
);
1708 /* figure out what to expand */
1709 if (bar_format
!= NULL
)
1711 else if (bar_format
== NULL
&& clock_enabled
)
1713 /* if nothing to expand bail out */
1719 /* copy as-is, just in case the format shouldn't be expanded below */
1720 strlcpy(fmtexp
, fmt
, sz
);
1721 /* finally pass the string through strftime(3) */
1722 #ifndef SWM_DENY_CLOCK_FORMAT
1723 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1724 warnx("format too long");
1732 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1733 char fmtrep
[SWM_BAR_MAX
];
1735 struct swm_region
*r
;
1737 /* expand the format by first passing it through strftime(3) */
1738 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1740 for (i
= 0; i
< ScreenCount(display
); i
++) {
1741 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1744 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1745 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1746 bar_print(r
, fmtrep
);
1757 if (bar_enabled
== 0)
1759 if (bar_extra
&& bar_extra_running
) {
1760 /* ignore short reads; it'll correct itself */
1761 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1762 if (b
&& b
[len
- 1] == '\n') {
1764 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1766 if (b
== NULL
&& errno
!= EAGAIN
) {
1767 warn("bar_update: bar_extra failed");
1771 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1784 bar_toggle(struct swm_region
*r
, union arg
*args
)
1786 struct swm_region
*tmpr
;
1787 int i
, sc
= ScreenCount(display
);
1789 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1792 for (i
= 0; i
< sc
; i
++)
1793 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1795 XUnmapWindow(display
, tmpr
->bar
->id
);
1797 for (i
= 0; i
< sc
; i
++)
1798 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1800 XMapRaised(display
, tmpr
->bar
->id
);
1803 bar_enabled
= !bar_enabled
;
1806 /* must be after stack */
1813 XSetWindowAttributes wa
;
1814 struct swm_region
*r
;
1817 /* do this here because the conf file is in memory */
1818 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1819 /* launch external status app */
1820 bar_extra_running
= 1;
1821 if (pipe(bar_pipe
) == -1)
1822 err(1, "pipe error");
1823 socket_setnonblock(bar_pipe
[0]);
1824 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1825 if (dup2(bar_pipe
[0], 0) == -1)
1827 if (dup2(bar_pipe
[1], 1) == -1)
1829 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1830 err(1, "could not disable SIGPIPE");
1831 switch (bar_pid
= fork()) {
1833 err(1, "cannot fork");
1837 execvp(bar_argv
[0], bar_argv
);
1838 err(1, "%s external app failed", bar_argv
[0]);
1840 default: /* parent */
1846 bzero(&wa
, sizeof wa
);
1847 for (i
= 0; i
< ScreenCount(display
); i
++)
1848 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1852 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1853 wa
.background_pixel
=
1854 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1855 XChangeWindowAttributes(display
, r
->bar
->id
,
1856 CWBackPixel
| CWBorderPixel
, &wa
);
1862 bar_setup(struct swm_region
*r
)
1864 char *default_string
;
1865 char **missing_charsets
;
1866 int num_missing_charsets
= 0;
1870 XFreeFontSet(display
, bar_fs
);
1874 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
1875 err(1, "bar_setup: calloc: failed to allocate memory.");
1877 DNPRINTF(SWM_D_BAR
, "bar_setup: loading bar_fonts: %s\n", bar_fonts
);
1879 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
1880 &num_missing_charsets
, &default_string
);
1882 if (num_missing_charsets
> 0) {
1883 warnx("Unable to load charset(s):");
1885 for (i
= 0; i
< num_missing_charsets
; ++i
)
1886 warnx("%s", missing_charsets
[i
]);
1888 XFreeStringList(missing_charsets
);
1890 if (strcmp(default_string
, ""))
1891 warnx("Glyphs from those sets will be replaced "
1892 "by '%s'.", default_string
);
1894 warnx("Glyphs from those sets won't be drawn.");
1898 errx(1, "Error creating font set structure.");
1900 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
1902 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
1903 2 * bar_border_width
;
1909 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1910 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
1911 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
1913 r
->bar
->id
= XCreateSimpleWindow(display
,
1914 r
->s
->root
, X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
1915 bar_border_width
, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
1916 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1918 r
->bar
->buffer
= XCreatePixmap(display
, r
->bar
->id
, WIDTH(r
->bar
),
1919 HEIGHT(r
->bar
), DefaultDepth(display
, r
->s
->idx
));
1921 XSelectInput(display
, r
->bar
->id
, VisibilityChangeMask
);
1924 XMapRaised(display
, r
->bar
->id
);
1926 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%lx, (x,y) w x h: (%d,%d) "
1927 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
1930 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1931 err(1, "could not install bar_signal");
1936 bar_cleanup(struct swm_region
*r
)
1940 xcb_destroy_window(conn
, r
->bar
->id
);
1941 xcb_free_pixmap(conn
, r
->bar
->buffer
);
1947 drain_enter_notify(void)
1952 while (XCheckMaskEvent(display
, EnterWindowMask
, &cne
))
1955 DNPRINTF(SWM_D_EVENT
, "drain_enter_notify: drained: %d\n", i
);
1959 set_win_state(struct ws_win
*win
, long state
)
1961 long data
[] = {state
, None
};
1963 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%lx\n", win
->id
);
1968 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1969 (unsigned char *)data
, 2);
1976 unsigned char *p
= NULL
;
1979 if (!get_property(w
, astate
, 2L, astate
, &n
, NULL
, &p
))
1982 result
= *((long *)p
);
1988 version(struct swm_region
*r
, union arg
*args
)
1990 bar_version
= !bar_version
;
1992 snprintf(bar_vertext
, sizeof bar_vertext
,
1993 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
1995 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2000 client_msg(struct ws_win
*win
, Atom a
)
2002 XClientMessageEvent cm
;
2007 bzero(&cm
, sizeof cm
);
2008 cm
.type
= ClientMessage
;
2009 cm
.window
= win
->id
;
2010 cm
.message_type
= aprot
;
2013 cm
.data
.l
[1] = CurrentTime
;
2014 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
2017 /* synthetic response to a ConfigureRequest when not making a change */
2019 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
2026 /* send notification of unchanged state. */
2027 ce
.type
= ConfigureNotify
;
2030 ce
.width
= WIDTH(win
);
2031 ce
.height
= HEIGHT(win
);
2032 ce
.override_redirect
= False
;
2036 ce
.display
= display
;
2038 ce
.window
= win
->id
;
2039 ce
.border_width
= BORDER(win
);
2043 ce
.display
= ev
->display
;
2044 ce
.event
= ev
->window
;
2045 ce
.window
= ev
->window
;
2047 /* make response appear more WM_SIZE_HINTS-compliant */
2049 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%lx,"
2050 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
2051 "%d x %d\n", win
->id
, win
->sh_mask
, SH_MIN_W(win
),
2052 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2053 SH_INC_W(win
), SH_INC_H(win
));
2057 /* the hint may be set... to 0! */
2058 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2059 ce
.width
= SH_MIN_W(win
);
2060 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2061 ce
.height
= SH_MIN_H(win
);
2066 /* may also be advertized as 0 */
2067 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2068 ce
.width
= SH_MAX_W(win
);
2069 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2070 ce
.height
= SH_MAX_H(win
);
2073 /* resize increment. */
2075 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2076 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2078 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2079 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2083 /* adjust x and y for requested border_width. */
2084 ce
.x
+= BORDER(win
) - ev
->border_width
;
2085 ce
.y
+= BORDER(win
) - ev
->border_width
;
2086 ce
.border_width
= ev
->border_width
;
2087 ce
.above
= ev
->above
;
2090 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
2091 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2092 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2094 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
2098 count_win(struct workspace
*ws
, int count_transient
)
2103 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2104 if (count_transient
== 0 && win
->floating
)
2106 if (count_transient
== 0 && win
->transient
)
2112 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2118 quit(struct swm_region
*r
, union arg
*args
)
2120 DNPRINTF(SWM_D_MISC
, "quit\n");
2125 unmap_window(struct ws_win
*win
)
2130 /* don't unmap again */
2131 if (getstate(win
->id
) == IconicState
)
2134 set_win_state(win
, IconicState
);
2136 XUnmapWindow(display
, win
->id
);
2137 XSetWindowBorder(display
, win
->id
,
2138 win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2147 for (i
= 0; i
< ScreenCount(display
); i
++)
2148 for (j
= 0; j
< workspace_limit
; j
++)
2149 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2154 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
2161 event
.display
= display
; /* Ignored, but what the hell */
2162 event
.window
= win
->id
;
2163 event
.root
= win
->s
->root
;
2164 event
.subwindow
= None
;
2165 event
.time
= CurrentTime
;
2170 event
.same_screen
= True
;
2171 event
.keycode
= XKeysymToKeycode(display
, keysym
);
2172 event
.state
= modifiers
;
2174 event
.type
= KeyPress
;
2175 XSendEvent(event
.display
, event
.window
, True
,
2176 KeyPressMask
, (XEvent
*)&event
);
2178 event
.type
= KeyRelease
;
2179 XSendEvent(event
.display
, event
.window
, True
,
2180 KeyPressMask
, (XEvent
*)&event
);
2185 restart(struct swm_region
*r
, union arg
*args
)
2187 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2189 /* disable alarm because the following code may not be interrupted */
2191 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
2192 err(1, "can't disable alarm");
2197 xcb_disconnect(conn
);
2198 XCloseDisplay(display
);
2199 execvp(start_argv
[0], start_argv
);
2200 warn("execvp failed");
2205 root_to_region(Window root
)
2207 struct swm_region
*r
= NULL
;
2209 int i
, x
, y
, wx
, wy
;
2212 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%lx\n", root
);
2214 for (i
= 0; i
< ScreenCount(display
); i
++)
2215 if (screens
[i
].root
== root
)
2218 if (XQueryPointer(display
, screens
[i
].root
,
2219 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
2220 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: (%d,%d)\n",
2222 /* choose a region based on pointer location */
2223 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2224 if (X(r
) <= x
&& x
< MAX_X(r
) &&
2225 Y(r
) <= y
&& y
< MAX_Y(r
))
2230 r
= TAILQ_FIRST(&screens
[i
].rl
);
2236 find_unmanaged_window(Window id
)
2241 for (i
= 0; i
< ScreenCount(display
); i
++)
2242 for (j
= 0; j
< workspace_limit
; j
++)
2243 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2251 find_window(Window id
)
2254 Window wrr
, wpr
, *wcr
= NULL
;
2258 for (i
= 0; i
< ScreenCount(display
); i
++)
2259 for (j
= 0; j
< workspace_limit
; j
++)
2260 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2264 /* if we were looking for the parent return that window instead */
2265 if (XQueryTree(display
, id
, &wrr
, &wpr
, &wcr
, &nc
) == 0)
2270 /* ignore not found and root */
2271 if (wpr
== 0 || wrr
== wpr
)
2274 /* look for parent */
2275 for (i
= 0; i
< ScreenCount(display
); i
++)
2276 for (j
= 0; j
< workspace_limit
; j
++)
2277 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2285 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2290 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2293 close(ConnectionNumber(display
));
2295 setenv("LD_PRELOAD", SWM_LIB
, 1);
2297 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2298 warn("spawn: asprintf SWM_WS");
2301 setenv("_SWM_WS", ret
, 1);
2305 if (asprintf(&ret
, "%d", getpid()) == -1) {
2306 warn("spawn: asprintf _SWM_PID");
2309 setenv("_SWM_PID", ret
, 1);
2313 if (setsid() == -1) {
2314 warn("spawn: setsid");
2320 * close stdin and stdout to prevent interaction between apps
2321 * and the baraction script
2322 * leave stderr open to record errors
2324 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2325 warn("spawn: open");
2328 dup2(fd
, STDIN_FILENO
);
2329 dup2(fd
, STDOUT_FILENO
);
2334 execvp(args
->argv
[0], args
->argv
);
2336 warn("spawn: execvp");
2341 kill_refs(struct ws_win
*win
)
2344 struct swm_region
*r
;
2345 struct workspace
*ws
;
2350 for (i
= 0; i
< ScreenCount(display
); i
++)
2351 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2352 for (x
= 0; x
< workspace_limit
; x
++) {
2354 if (win
== ws
->focus
)
2356 if (win
== ws
->focus_prev
)
2357 ws
->focus_prev
= NULL
;
2362 validate_win(struct ws_win
*testwin
)
2365 struct workspace
*ws
;
2366 struct swm_region
*r
;
2369 if (testwin
== NULL
)
2372 for (i
= 0; i
< ScreenCount(display
); i
++)
2373 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2374 for (x
= 0; x
< workspace_limit
; x
++) {
2376 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2384 validate_ws(struct workspace
*testws
)
2386 struct swm_region
*r
;
2387 struct workspace
*ws
;
2390 /* validate all ws */
2391 for (i
= 0; i
< ScreenCount(display
); i
++)
2392 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2393 for (x
= 0; x
< workspace_limit
; x
++) {
2402 unfocus_win(struct ws_win
*win
)
2407 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%lx\n", WINID(win
));
2411 if (win
->ws
== NULL
)
2414 if (validate_ws(win
->ws
))
2415 return; /* XXX this gets hit with thunderbird, needs fixing */
2417 if (win
->ws
->r
== NULL
)
2420 if (validate_win(win
)) {
2425 if (win
->ws
->focus
== win
) {
2426 win
->ws
->focus
= NULL
;
2427 win
->ws
->focus_prev
= win
;
2430 if (validate_win(win
->ws
->focus
)) {
2431 kill_refs(win
->ws
->focus
);
2432 win
->ws
->focus
= NULL
;
2434 if (validate_win(win
->ws
->focus_prev
)) {
2435 kill_refs(win
->ws
->focus_prev
);
2436 win
->ws
->focus_prev
= NULL
;
2439 /* drain all previous unfocus events */
2440 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
2443 grabbuttons(win
, 0);
2444 XSetWindowBorder(display
, win
->id
,
2445 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2447 XChangeProperty(display
, win
->s
->root
,
2448 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2449 PropModeReplace
, (unsigned char *)&none
, 1);
2458 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2460 for (i
= 0; i
< ScreenCount(display
); i
++)
2461 for (j
= 0; j
< workspace_limit
; j
++)
2462 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2467 focus_win(struct ws_win
*win
)
2472 struct ws_win
*cfw
= NULL
;
2475 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%lx\n", WINID(win
));
2479 if (win
->ws
== NULL
)
2482 if (validate_ws(win
->ws
))
2483 return; /* XXX this gets hit with thunderbird, needs fixing */
2485 if (validate_win(win
)) {
2490 if (validate_win(win
)) {
2495 XGetInputFocus(display
, &cur_focus
, &rr
);
2496 if ((cfw
= find_window(cur_focus
)) != NULL
)
2499 /* use larger hammer since the window was killed somehow */
2500 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2501 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2502 XSetWindowBorder(display
, cfw
->id
,
2503 cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2506 win
->ws
->focus
= win
;
2508 if (win
->ws
->r
!= NULL
) {
2509 /* drain all previous focus events */
2510 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
2514 XSetInputFocus(display
, win
->id
,
2515 RevertToParent
, CurrentTime
);
2516 grabbuttons(win
, 1);
2517 XSetWindowBorder(display
, win
->id
,
2518 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2519 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2520 win
->ws
->always_raise
)
2521 XMapRaised(display
, win
->id
);
2523 XChangeProperty(display
, win
->s
->root
,
2524 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2525 PropModeReplace
, (unsigned char *)&win
->id
, 1);
2532 switchws(struct swm_region
*r
, union arg
*args
)
2534 int wsid
= args
->id
, unmap_old
= 0;
2535 struct swm_region
*this_r
, *other_r
;
2537 struct workspace
*new_ws
, *old_ws
;
2543 if (wsid
>= workspace_limit
)
2547 old_ws
= this_r
->ws
;
2548 new_ws
= &this_r
->s
->ws
[wsid
];
2550 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2551 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2553 if (new_ws
== NULL
|| old_ws
== NULL
)
2555 if (new_ws
== old_ws
)
2558 other_r
= new_ws
->r
;
2559 if (other_r
== NULL
) {
2560 /* the other workspace is hidden, hide this one */
2564 /* the other ws is visible in another region, exchange them */
2565 other_r
->ws_prior
= new_ws
;
2566 other_r
->ws
= old_ws
;
2567 old_ws
->r
= other_r
;
2569 this_r
->ws_prior
= old_ws
;
2570 this_r
->ws
= new_ws
;
2573 /* this is needed so that we can click on a window after a restart */
2577 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2578 focus(new_ws
->r
, &a
);
2580 /* unmap old windows */
2582 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2585 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2586 drain_enter_notify();
2590 cyclews(struct swm_region
*r
, union arg
*args
)
2593 struct swm_screen
*s
= r
->s
;
2596 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2597 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2602 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2605 case SWM_ARG_ID_CYCLEWS_UP
:
2606 if (a
.id
< workspace_limit
- 1)
2611 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2614 case SWM_ARG_ID_CYCLEWS_DOWN
:
2618 a
.id
= workspace_limit
- 1;
2625 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2627 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2631 } while (a
.id
!= r
->ws
->idx
);
2635 priorws(struct swm_region
*r
, union arg
*args
)
2639 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2640 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2642 if (r
->ws_prior
== NULL
)
2645 a
.id
= r
->ws_prior
->idx
;
2650 cyclescr(struct swm_region
*r
, union arg
*args
)
2652 struct swm_region
*rr
= NULL
;
2656 /* do nothing if we don't have more than one screen */
2657 if (!(ScreenCount(display
) > 1 || outputs
> 1))
2662 case SWM_ARG_ID_CYCLESC_UP
:
2663 rr
= TAILQ_NEXT(r
, entry
);
2665 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2667 case SWM_ARG_ID_CYCLESC_DOWN
:
2668 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2670 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2678 /* move mouse to region */
2680 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2681 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2683 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2686 if (rr
->ws
->focus
) {
2687 /* move to focus window */
2688 x
= X(rr
->ws
->focus
) + 1;
2689 y
= Y(rr
->ws
->focus
) + 1;
2690 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2695 sort_windows(struct ws_win_list
*wl
)
2697 struct ws_win
*win
, *parent
, *nxt
;
2702 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2703 nxt
= TAILQ_NEXT(win
, entry
);
2704 if (win
->transient
) {
2705 parent
= find_window(win
->transient
);
2706 if (parent
== NULL
) {
2707 warnx("not possible bug");
2710 TAILQ_REMOVE(wl
, win
, entry
);
2711 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2718 swapwin(struct swm_region
*r
, union arg
*args
)
2720 struct ws_win
*target
, *source
;
2721 struct ws_win
*cur_focus
;
2722 struct ws_win_list
*wl
;
2725 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2726 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2728 cur_focus
= r
->ws
->focus
;
2729 if (cur_focus
== NULL
)
2733 wl
= &source
->ws
->winlist
;
2736 case SWM_ARG_ID_SWAPPREV
:
2737 if (source
->transient
)
2738 source
= find_window(source
->transient
);
2739 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2740 if (target
&& target
->transient
)
2741 target
= find_window(target
->transient
);
2742 TAILQ_REMOVE(wl
, source
, entry
);
2744 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2746 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2748 case SWM_ARG_ID_SWAPNEXT
:
2749 target
= TAILQ_NEXT(source
, entry
);
2750 /* move the parent and let the sort handle the move */
2751 if (source
->transient
)
2752 source
= find_window(source
->transient
);
2753 TAILQ_REMOVE(wl
, source
, entry
);
2755 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2757 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2759 case SWM_ARG_ID_SWAPMAIN
:
2760 target
= TAILQ_FIRST(wl
);
2761 if (target
== source
) {
2762 if (source
->ws
->focus_prev
!= NULL
&&
2763 source
->ws
->focus_prev
!= target
)
2764 source
= source
->ws
->focus_prev
;
2768 if (target
== NULL
|| source
== NULL
)
2770 source
->ws
->focus_prev
= target
;
2771 TAILQ_REMOVE(wl
, target
, entry
);
2772 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2773 TAILQ_REMOVE(wl
, source
, entry
);
2774 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2776 case SWM_ARG_ID_MOVELAST
:
2777 TAILQ_REMOVE(wl
, source
, entry
);
2778 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2781 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2791 focus_prev(struct ws_win
*win
)
2793 struct ws_win
*winfocus
= NULL
;
2794 struct ws_win
*cur_focus
= NULL
;
2795 struct ws_win_list
*wl
= NULL
;
2796 struct workspace
*ws
= NULL
;
2798 if (!(win
&& win
->ws
))
2803 cur_focus
= ws
->focus
;
2805 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%lx, cur_focus: 0x%lx\n",
2806 WINID(win
), WINID(cur_focus
));
2808 /* pickle, just focus on whatever */
2809 if (cur_focus
== NULL
) {
2810 /* use prev_focus if valid */
2811 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2812 find_window(WINID(ws
->focus_prev
)))
2813 winfocus
= ws
->focus_prev
;
2817 /* if transient focus on parent */
2818 if (cur_focus
->transient
) {
2819 winfocus
= find_window(cur_focus
->transient
);
2823 /* if in max_stack try harder */
2824 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2825 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2826 if (cur_focus
!= ws
->focus_prev
)
2827 winfocus
= ws
->focus_prev
;
2829 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2834 DNPRINTF(SWM_D_FOCUS
, "focus_prev: focus_close: %d\n", focus_close
);
2836 if (winfocus
== NULL
|| winfocus
== win
) {
2837 switch (focus_close
) {
2838 case SWM_STACK_BOTTOM
:
2839 winfocus
= TAILQ_FIRST(wl
);
2842 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2844 case SWM_STACK_ABOVE
:
2845 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
2846 if (focus_close_wrap
)
2847 winfocus
= TAILQ_FIRST(wl
);
2849 winfocus
= TAILQ_PREV(cur_focus
,
2850 ws_win_list
, entry
);
2853 case SWM_STACK_BELOW
:
2854 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
2856 if (focus_close_wrap
)
2857 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2859 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2865 if (winfocus
== NULL
) {
2866 if (focus_default
== SWM_STACK_TOP
)
2867 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2869 winfocus
= TAILQ_FIRST(wl
);
2873 focus_magic(winfocus
);
2877 focus(struct swm_region
*r
, union arg
*args
)
2879 struct ws_win
*winfocus
= NULL
, *head
;
2880 struct ws_win
*cur_focus
= NULL
;
2881 struct ws_win_list
*wl
= NULL
;
2882 struct workspace
*ws
= NULL
;
2888 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2890 /* treat FOCUS_CUR special */
2891 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2892 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2893 winfocus
= r
->ws
->focus
;
2894 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2895 winfocus
= r
->ws
->focus_prev
;
2897 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2898 if (winfocus
->iconic
== 0)
2901 focus_magic(winfocus
);
2905 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2909 if (TAILQ_EMPTY(wl
))
2911 /* make sure there is at least one uniconified window */
2913 TAILQ_FOREACH(winfocus
, wl
, entry
)
2914 if (winfocus
->iconic
== 0) {
2922 case SWM_ARG_ID_FOCUSPREV
:
2923 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2925 head
= TAILQ_LAST(wl
, ws_win_list
);
2927 if (WINID(winfocus
) == cur_focus
->transient
) {
2928 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2930 head
= TAILQ_LAST(wl
, ws_win_list
);
2935 if (winfocus
&& winfocus
->iconic
) {
2936 while (winfocus
!= cur_focus
) {
2937 if (winfocus
== NULL
)
2938 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2939 if (winfocus
->iconic
== 0)
2941 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
2947 case SWM_ARG_ID_FOCUSNEXT
:
2948 head
= TAILQ_NEXT(cur_focus
, entry
);
2950 head
= TAILQ_FIRST(wl
);
2954 if (winfocus
&& winfocus
->iconic
) {
2955 while (winfocus
!= cur_focus
) {
2956 if (winfocus
== NULL
)
2957 winfocus
= TAILQ_FIRST(wl
);
2958 if (winfocus
->iconic
== 0)
2960 winfocus
= TAILQ_NEXT(winfocus
, entry
);
2965 case SWM_ARG_ID_FOCUSMAIN
:
2966 winfocus
= TAILQ_FIRST(wl
);
2967 if (winfocus
== cur_focus
)
2968 winfocus
= cur_focus
->ws
->focus_prev
;
2975 focus_magic(winfocus
);
2979 cycle_layout(struct swm_region
*r
, union arg
*args
)
2981 struct workspace
*ws
= r
->ws
;
2984 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2987 if (ws
->cur_layout
->l_stack
== NULL
)
2988 ws
->cur_layout
= &layouts
[0];
2991 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2992 drain_enter_notify();
2993 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2998 stack_config(struct swm_region
*r
, union arg
*args
)
3000 struct workspace
*ws
= r
->ws
;
3002 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3005 if (ws
->cur_layout
->l_config
!= NULL
)
3006 ws
->cur_layout
->l_config(ws
, args
->id
);
3008 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3015 struct swm_geometry g
;
3016 struct swm_region
*r
;
3022 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3024 for (i
= 0; i
< ScreenCount(display
); i
++) {
3028 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3029 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3030 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3032 /* start with screen geometry, adjust for bar */
3034 g
.w
-= 2 * border_width
;
3035 g
.h
-= 2 * border_width
;
3041 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3042 r
->ws
->cur_layout
->l_string(r
->ws
);
3043 /* save r so we can track region changes */
3050 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3051 drain_enter_notify();
3053 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3057 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3059 /* retain window geom and region geom */
3060 win
->g_float
= win
->g
;
3061 win
->g_float
.x
-= X(r
);
3062 win
->g_float
.y
-= Y(r
);
3063 win
->g_floatvalid
= 1;
3064 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%lx, g: (%d,%d)"
3065 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3066 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3067 win
->g_float
.w
, win
->g_float
.h
);
3071 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3076 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%lx\n", win
->id
);
3079 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3080 * geom on ws switches or return from max mode
3082 if (win
->g_floatvalid
&& (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3083 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)))) {
3084 /* refloat at last floating relative position */
3085 win
->g
= win
->g_float
;
3090 win
->floatmaxed
= 0;
3093 * if set to fullscreen mode, configure window to maximum size.
3095 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3096 if (!win
->g_floatvalid
)
3097 store_float_geom(win
, win
->ws
->r
);
3099 win
->g
= win
->ws
->r
->g
;
3103 * remove border on fullscreen floater when in fullscreen mode or when
3104 * the quirk is present.
3106 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3107 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3108 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3109 if (win
->bordered
) {
3111 X(win
) += border_width
;
3112 Y(win
) += border_width
;
3114 } else if (!win
->bordered
) {
3116 X(win
) -= border_width
;
3117 Y(win
) -= border_width
;
3120 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3121 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3122 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3127 * floaters and transients are auto-centred unless moved
3130 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3131 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3134 /* keep window within region bounds */
3135 constrain_window(win
, r
, 0);
3141 * Send keystrokes to terminal to decrease/increase the font size as the
3142 * window size changes.
3145 adjust_font(struct ws_win
*win
)
3147 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3148 win
->floating
|| win
->transient
)
3151 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3152 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3153 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3154 win
->font_size_boundary
[win
->font_steps
] =
3155 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3158 win
->last_inc
= win
->sh
.width_inc
;
3159 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
3160 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3161 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3164 win
->last_inc
= win
->sh
.width_inc
;
3165 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
3169 #define SWAPXY(g) do { \
3171 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3172 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3175 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3177 XWindowAttributes wa
;
3178 struct swm_geometry win_g
, r_g
= *g
;
3179 struct ws_win
*win
, *fs_win
= NULL
;
3180 int i
, j
, s
, stacks
;
3181 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3182 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3183 int split
, colno
, winno
, mwin
, msize
, mscale
;
3184 int remain
, missing
, v_slice
, reconfigure
;
3187 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3188 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3190 winno
= count_win(ws
, 0);
3191 if (winno
== 0 && count_win(ws
, 1) == 0)
3194 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3195 if (win
->transient
== 0 && win
->floating
== 0
3196 && win
->iconic
== 0)
3203 w_inc
= win
->sh
.width_inc
;
3204 w_base
= win
->sh
.base_width
;
3205 mwin
= ws
->l_state
.horizontal_mwin
;
3206 mscale
= ws
->l_state
.horizontal_msize
;
3207 stacks
= ws
->l_state
.horizontal_stacks
;
3210 w_inc
= win
->sh
.height_inc
;
3211 w_base
= win
->sh
.base_height
;
3212 mwin
= ws
->l_state
.vertical_mwin
;
3213 mscale
= ws
->l_state
.vertical_msize
;
3214 stacks
= ws
->l_state
.vertical_stacks
;
3218 if (stacks
> winno
- mwin
)
3219 stacks
= winno
- mwin
;
3223 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3224 if (mwin
&& winno
> mwin
) {
3225 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3229 win_g
.w
= v_slice
* mscale
;
3231 if (w_inc
> 1 && w_inc
< v_slice
) {
3232 /* adjust for window's requested size increment */
3233 remain
= (win_g
.w
- w_base
) % w_inc
;
3239 win_g
.x
+= r_g
.w
- msize
;
3242 colno
= split
= winno
/ stacks
;
3243 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3244 2 * border_width
) / stacks
);
3246 hrh
= r_g
.h
/ colno
;
3247 extra
= r_g
.h
- (colno
* hrh
);
3248 win_g
.h
= hrh
- 2 * border_width
;
3250 /* stack all the tiled windows */
3251 i
= j
= 0, s
= stacks
;
3252 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3253 if (win
->transient
!= 0 || win
->floating
!= 0)
3255 if (win
->iconic
!= 0)
3258 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3263 if (split
&& i
== split
) {
3264 colno
= (winno
- mwin
) / stacks
;
3265 if (s
<= (winno
- mwin
) % stacks
)
3267 split
= split
+ colno
;
3268 hrh
= (r_g
.h
/ colno
);
3269 extra
= r_g
.h
- (colno
* hrh
);
3273 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3274 win_g
.w
= (r_g
.w
- msize
-
3275 (stacks
* 2 * border_width
)) / stacks
;
3277 win_g
.w
+= (r_g
.w
- msize
-
3278 (stacks
* 2 * border_width
)) % stacks
;
3282 win_g
.h
= hrh
- 2 * border_width
;
3284 h_inc
= win
->sh
.width_inc
;
3285 h_base
= win
->sh
.base_width
;
3287 h_inc
= win
->sh
.height_inc
;
3288 h_base
= win
->sh
.base_height
;
3290 if (j
== colno
- 1) {
3291 win_g
.h
= hrh
+ extra
;
3292 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3293 /* adjust for window's requested size increment */
3294 remain
= (win_g
.h
- h_base
) % h_inc
;
3295 missing
= h_inc
- remain
;
3297 if (missing
<= extra
|| j
== 0) {
3309 win_g
.y
+= last_h
+ 2 * border_width
;
3311 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3313 win_g
.w
+= 2 * border_width
;
3314 win_g
.h
+= 2 * border_width
;
3319 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3320 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3324 WIDTH(win
) = win_g
.h
;
3325 HEIGHT(win
) = win_g
.w
;
3328 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3329 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3333 WIDTH(win
) = win_g
.w
;
3334 HEIGHT(win
) = win_g
.h
;
3338 if (bordered
!= win
->bordered
) {
3340 win
->bordered
= bordered
;
3348 if (XGetWindowAttributes(display
, win
->id
, &wa
))
3349 if (wa
.map_state
== IsUnmapped
)
3350 XMapRaised(display
, win
->id
);
3358 /* now, stack all the floaters and transients */
3359 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3360 if (win
->transient
== 0 && win
->floating
== 0)
3362 if (win
->iconic
== 1)
3364 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3369 stack_floater(win
, ws
->r
);
3370 XMapRaised(display
, win
->id
);
3374 stack_floater(fs_win
, ws
->r
);
3375 XMapRaised(display
, fs_win
->id
);
3380 vertical_config(struct workspace
*ws
, int id
)
3382 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3386 case SWM_ARG_ID_STACKRESET
:
3387 case SWM_ARG_ID_STACKINIT
:
3388 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3389 ws
->l_state
.vertical_mwin
= 1;
3390 ws
->l_state
.vertical_stacks
= 1;
3392 case SWM_ARG_ID_MASTERSHRINK
:
3393 if (ws
->l_state
.vertical_msize
> 1)
3394 ws
->l_state
.vertical_msize
--;
3396 case SWM_ARG_ID_MASTERGROW
:
3397 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3398 ws
->l_state
.vertical_msize
++;
3400 case SWM_ARG_ID_MASTERADD
:
3401 ws
->l_state
.vertical_mwin
++;
3403 case SWM_ARG_ID_MASTERDEL
:
3404 if (ws
->l_state
.vertical_mwin
> 0)
3405 ws
->l_state
.vertical_mwin
--;
3407 case SWM_ARG_ID_STACKINC
:
3408 ws
->l_state
.vertical_stacks
++;
3410 case SWM_ARG_ID_STACKDEC
:
3411 if (ws
->l_state
.vertical_stacks
> 1)
3412 ws
->l_state
.vertical_stacks
--;
3414 case SWM_ARG_ID_FLIPLAYOUT
:
3415 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3423 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3425 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3427 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3431 horizontal_config(struct workspace
*ws
, int id
)
3433 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3436 case SWM_ARG_ID_STACKRESET
:
3437 case SWM_ARG_ID_STACKINIT
:
3438 ws
->l_state
.horizontal_mwin
= 1;
3439 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3440 ws
->l_state
.horizontal_stacks
= 1;
3442 case SWM_ARG_ID_MASTERSHRINK
:
3443 if (ws
->l_state
.horizontal_msize
> 1)
3444 ws
->l_state
.horizontal_msize
--;
3446 case SWM_ARG_ID_MASTERGROW
:
3447 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3448 ws
->l_state
.horizontal_msize
++;
3450 case SWM_ARG_ID_MASTERADD
:
3451 ws
->l_state
.horizontal_mwin
++;
3453 case SWM_ARG_ID_MASTERDEL
:
3454 if (ws
->l_state
.horizontal_mwin
> 0)
3455 ws
->l_state
.horizontal_mwin
--;
3457 case SWM_ARG_ID_STACKINC
:
3458 ws
->l_state
.horizontal_stacks
++;
3460 case SWM_ARG_ID_STACKDEC
:
3461 if (ws
->l_state
.horizontal_stacks
> 1)
3462 ws
->l_state
.horizontal_stacks
--;
3464 case SWM_ARG_ID_FLIPLAYOUT
:
3465 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3473 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3475 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3477 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3480 /* fullscreen view */
3482 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3484 struct swm_geometry gg
= *g
;
3485 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3488 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3493 winno
= count_win(ws
, 0);
3494 if (winno
== 0 && count_win(ws
, 1) == 0)
3497 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3498 if (win
->transient
) {
3500 parent
= find_window(win
->transient
);
3504 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3506 * retain geometry for retrieval on exit from
3509 store_float_geom(win
, ws
->r
);
3510 win
->floatmaxed
= 1;
3513 /* only reconfigure if necessary */
3514 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3515 HEIGHT(win
) != gg
.h
) {
3521 WIDTH(win
) += 2 * border_width
;
3522 HEIGHT(win
) += 2 * border_width
;
3527 /* unmap only if we don't have multi screen */
3528 if (win
!= ws
->focus
)
3529 if (!(ScreenCount(display
) > 1 || outputs
> 1))
3533 /* put the last transient on top */
3536 XMapRaised(display
, parent
->id
);
3537 stack_floater(wintrans
, ws
->r
);
3538 focus_magic(wintrans
);
3543 send_to_ws(struct swm_region
*r
, union arg
*args
)
3545 int wsid
= args
->id
;
3546 struct ws_win
*win
= NULL
, *parent
;
3547 struct workspace
*ws
, *nws
;
3548 Atom ws_idx_atom
= 0;
3549 unsigned char ws_idx_str
[SWM_PROPLEN
];
3552 if (wsid
>= workspace_limit
)
3555 if (r
&& r
->ws
&& r
->ws
->focus
)
3561 if (win
->ws
->idx
== wsid
)
3564 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%lx\n", win
->id
);
3567 nws
= &win
->s
->ws
[wsid
];
3569 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3571 if (win
->transient
) {
3572 parent
= find_window(win
->transient
);
3574 unmap_window(parent
);
3575 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3576 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3581 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3582 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3583 if (TAILQ_EMPTY(&ws
->winlist
))
3584 r
->ws
->focus
= NULL
;
3587 /* Try to update the window's workspace property */
3588 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
3590 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3592 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3594 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
3595 PropModeReplace
, ws_idx_str
, strlen((char *)ws_idx_str
));
3603 pressbutton(struct swm_region
*r
, union arg
*args
)
3605 XTestFakeButtonEvent(display
, args
->id
, True
, CurrentTime
);
3606 XTestFakeButtonEvent(display
, args
->id
, False
, CurrentTime
);
3610 raise_toggle(struct swm_region
*r
, union arg
*args
)
3612 if (r
== NULL
|| r
->ws
== NULL
)
3615 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3617 /* bring floaters back to top */
3618 if (r
->ws
->always_raise
== 0)
3623 iconify(struct swm_region
*r
, union arg
*args
)
3627 if (r
->ws
->focus
== NULL
)
3629 unmap_window(r
->ws
->focus
);
3630 update_iconic(r
->ws
->focus
, 1);
3632 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3633 drain_enter_notify();
3634 r
->ws
->focus
= NULL
;
3635 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3640 get_win_name(Window win
)
3642 unsigned char *prop
= NULL
;
3643 unsigned long nbytes
, nitems
;
3645 /* try _NET_WM_NAME first */
3646 if (get_property(win
, a_netwmname
, 0L, a_utf8_string
, NULL
, &nbytes
,
3649 if (get_property(win
, a_netwmname
, nbytes
, a_utf8_string
,
3650 &nitems
, NULL
, &prop
))
3654 /* fallback to WM_NAME */
3655 if (!get_property(win
, a_wmname
, 0L, a_string
, NULL
, &nbytes
, &prop
))
3658 if (get_property(win
, a_wmname
, nbytes
, a_string
, &nitems
, NULL
, &prop
))
3665 uniconify(struct swm_region
*r
, union arg
*args
)
3669 unsigned char *name
;
3672 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3674 if (r
== NULL
|| r
->ws
== NULL
)
3677 /* make sure we have anything to uniconify */
3678 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3679 if (win
->ws
== NULL
)
3680 continue; /* should never happen */
3681 if (win
->iconic
== 0)
3689 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3691 spawn_select(r
, args
, "search", &searchpid
);
3693 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3696 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3697 if (win
->ws
== NULL
)
3698 continue; /* should never happen */
3699 if (win
->iconic
== 0)
3702 name
= get_win_name(win
->id
);
3705 fprintf(lfile
, "%s.%lu\n", name
, win
->id
);
3713 name_workspace(struct swm_region
*r
, union arg
*args
)
3717 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3723 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3725 spawn_select(r
, args
, "name_workspace", &searchpid
);
3727 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3730 fprintf(lfile
, "%s", "");
3735 search_workspace(struct swm_region
*r
, union arg
*args
)
3738 struct workspace
*ws
;
3741 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3747 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3749 spawn_select(r
, args
, "search", &searchpid
);
3751 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3754 for (i
= 0; i
< workspace_limit
; i
++) {
3758 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3759 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3766 search_win_cleanup(void)
3768 struct search_window
*sw
= NULL
;
3770 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3771 XDestroyWindow(display
, sw
->indicator
);
3772 XFreeGC(display
, sw
->gc
);
3773 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3779 search_win(struct swm_region
*r
, union arg
*args
)
3781 struct ws_win
*win
= NULL
;
3782 struct search_window
*sw
= NULL
;
3789 XRectangle ibox
, lbox
;
3791 DNPRINTF(SWM_D_MISC
, "search_win\n");
3794 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3796 spawn_select(r
, args
, "search", &searchpid
);
3798 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3801 TAILQ_INIT(&search_wl
);
3804 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3805 if (win
->iconic
== 1)
3808 sw
= calloc(1, sizeof(struct search_window
));
3810 warn("search_win: calloc");
3812 search_win_cleanup();
3818 snprintf(s
, sizeof s
, "%d", i
);
3821 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
3823 w
= XCreateSimpleWindow(display
,
3824 win
->id
, 0, 0,lbox
.width
+ 4,
3825 bar_fs_extents
->max_logical_extent
.height
, 1,
3826 r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
,
3827 r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
3830 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3832 sw
->gc
= XCreateGC(display
, w
, 0, &gcv
);
3833 XMapRaised(display
, w
);
3834 XSetForeground(display
, sw
->gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
3836 DRAWSTRING(display
, w
, bar_fs
, sw
->gc
, 2,
3837 (bar_fs_extents
->max_logical_extent
.height
-
3838 lbox
.height
) / 2 - lbox
.y
, s
, len
);
3840 fprintf(lfile
, "%d\n", i
);
3848 search_resp_uniconify(char *resp
, unsigned long len
)
3850 unsigned char *name
;
3854 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3856 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3857 if (win
->iconic
== 0)
3859 name
= get_win_name(win
->id
);
3862 if (asprintf(&s
, "%s.%lu", name
, win
->id
) == -1) {
3867 if (strncmp(s
, resp
, len
) == 0) {
3868 /* XXX this should be a callback to generalize */
3869 update_iconic(win
, 0);
3878 search_resp_name_workspace(char *resp
, unsigned long len
)
3880 struct workspace
*ws
;
3882 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3884 if (search_r
->ws
== NULL
)
3889 free(search_r
->ws
->name
);
3890 search_r
->ws
->name
= NULL
;
3894 ws
->name
= strdup(resp
);
3895 if (ws
->name
== NULL
) {
3896 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
3897 "strdup: %s", strerror(errno
));
3904 search_resp_search_workspace(char *resp
, unsigned long len
)
3911 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
3915 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
3922 ws_idx
= strtonum(q
, 1, workspace_limit
, &errstr
);
3924 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
3931 switchws(search_r
, &a
);
3935 search_resp_search_window(char *resp
, unsigned long len
)
3940 struct search_window
*sw
;
3942 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
3946 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
3951 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
3953 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
3960 TAILQ_FOREACH(sw
, &search_wl
, entry
)
3961 if (idx
== sw
->idx
) {
3967 #define MAX_RESP_LEN 1024
3970 search_do_resp(void)
3976 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
3981 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
3982 warn("search: calloc");
3986 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
3988 warn("search: read error");
3991 resp
[rbytes
] = '\0';
3994 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3995 * newline, so work around that by sanitizing the resp now.
3997 resp
[strcspn(resp
, "\n")] = '\0';
4000 switch (search_resp_action
) {
4001 case SWM_SEARCH_UNICONIFY
:
4002 search_resp_uniconify(resp
, len
);
4004 case SWM_SEARCH_NAME_WORKSPACE
:
4005 search_resp_name_workspace(resp
, len
);
4007 case SWM_SEARCH_SEARCH_WORKSPACE
:
4008 search_resp_search_workspace(resp
, len
);
4010 case SWM_SEARCH_SEARCH_WINDOW
:
4011 search_resp_search_window(resp
, len
);
4016 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4017 search_win_cleanup();
4019 search_resp_action
= SWM_SEARCH_NONE
;
4020 close(select_resp_pipe
[0]);
4025 wkill(struct swm_region
*r
, union arg
*args
)
4027 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4029 if (r
->ws
->focus
== NULL
)
4032 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4033 XKillClient(display
, r
->ws
->focus
->id
);
4035 if (r
->ws
->focus
->can_delete
)
4036 client_msg(r
->ws
->focus
, adelete
);
4041 floating_toggle_win(struct ws_win
*win
)
4043 struct swm_region
*r
;
4053 /* reject floating toggles in max stack mode */
4054 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4057 if (win
->floating
) {
4058 if (!win
->floatmaxed
) {
4059 /* retain position for refloat */
4060 store_float_geom(win
, r
);
4064 if (win
->g_floatvalid
) {
4065 /* refloat at last floating relative position */
4066 X(win
) = win
->g_float
.x
+ X(r
);
4067 Y(win
) = win
->g_float
.y
+ Y(r
);
4068 WIDTH(win
) = win
->g_float
.w
;
4069 HEIGHT(win
) = win
->g_float
.h
;
4074 ewmh_update_actions(win
);
4080 floating_toggle(struct swm_region
*r
, union arg
*args
)
4082 struct ws_win
*win
= r
->ws
->focus
;
4088 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4089 _NET_WM_STATE_TOGGLE
);
4092 if (focus_mode
== SWM_FOCUS_DEFAULT
)
4093 drain_enter_notify();
4095 if (win
== win
->ws
->focus
) {
4096 a
.id
= SWM_ARG_ID_FOCUSCUR
;
4097 focus(win
->ws
->r
, &a
);
4102 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4104 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4106 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4108 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4111 if (X(win
) + BORDER(win
) < X(r
)) {
4113 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4115 X(win
) = X(r
) - BORDER(win
);
4118 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4120 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4122 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4125 if (Y(win
) + BORDER(win
) < Y(r
)) {
4127 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4129 Y(win
) = Y(r
) - BORDER(win
);
4135 if (HEIGHT(win
) < 1)
4141 update_window(struct ws_win
*win
)
4146 bzero(&wc
, sizeof wc
);
4147 mask
= CWBorderWidth
| CWWidth
| CWHeight
| CWX
| CWY
;
4149 wc
.border_width
= BORDER(win
);
4152 wc
.width
= WIDTH(win
);
4153 wc
.height
= HEIGHT(win
);
4155 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%lx, (x,y) w x h: "
4156 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
.x
, wc
.y
, wc
.width
,
4157 wc
.height
, YESNO(win
->bordered
));
4159 XConfigureWindow(display
, win
->id
, mask
, &wc
);
4162 #define SWM_RESIZE_STEPS (50)
4165 resize(struct ws_win
*win
, union arg
*args
)
4169 struct swm_region
*r
= NULL
;
4170 int resize_step
= 0;
4174 struct swm_geometry g
;
4175 int top
= 0, left
= 0;
4178 unsigned int shape
; /* cursor style */
4184 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4187 DNPRINTF(SWM_D_MOUSE
, "resize: window: 0x%lx, floating: %s, "
4188 "transient: 0x%lx\n", win
->id
, YESNO(win
->floating
),
4191 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4194 /* reject resizes in max mode for floaters (transient ok) */
4195 if (win
->floatmaxed
)
4199 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4205 case SWM_ARG_ID_WIDTHSHRINK
:
4206 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4209 case SWM_ARG_ID_WIDTHGROW
:
4210 WIDTH(win
) += SWM_RESIZE_STEPS
;
4213 case SWM_ARG_ID_HEIGHTSHRINK
:
4214 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4217 case SWM_ARG_ID_HEIGHTGROW
:
4218 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4225 constrain_window(win
, r
, 1);
4227 store_float_geom(win
,r
);
4231 if (focus_mode
== SWM_FOCUS_DEFAULT
)
4232 drain_enter_notify();
4234 /* get cursor offset from window root */
4235 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4240 if (wx
< WIDTH(win
) / 2)
4243 if (wy
< HEIGHT(win
) / 2)
4246 if (args
->id
== SWM_ARG_ID_CENTER
)
4249 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
4251 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
4253 cursor
= XCreateFontCursor(display
, shape
);
4255 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4256 GrabModeAsync
, None
, cursor
, CurrentTime
) != GrabSuccess
) {
4257 XFreeCursor(display
, cursor
);
4262 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4263 SubstructureRedirectMask
, &ev
);
4265 case ConfigureRequest
:
4268 handler
[ev
.type
](&ev
);
4271 /* cursor offset/delta from start of the operation */
4272 dx
= ev
.xmotion
.x_root
- x
;
4273 dy
= ev
.xmotion
.y_root
- y
;
4279 if (args
->id
== SWM_ARG_ID_CENTER
) {
4280 if (g
.h
/ 2 + dy
< 1)
4284 HEIGHT(win
) = g
.h
+ 2 * dy
;
4292 HEIGHT(win
) = g
.h
+ dy
;
4299 if (args
->id
== SWM_ARG_ID_CENTER
) {
4300 if (g
.w
/ 2 + dx
< 1)
4304 WIDTH(win
) = g
.w
+ 2 * dx
;
4312 WIDTH(win
) = g
.w
+ dx
;
4315 constrain_window(win
, r
, 1);
4317 /* not free, don't sync more than 120 times / second */
4318 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4319 time
= ev
.xmotion
.time
;
4320 XSync(display
, False
);
4325 } while (ev
.type
!= ButtonRelease
);
4327 XSync(display
, False
);
4330 store_float_geom(win
,r
);
4332 XUngrabPointer(display
, CurrentTime
);
4333 XFreeCursor(display
, cursor
);
4336 drain_enter_notify();
4340 resize_step(struct swm_region
*r
, union arg
*args
)
4342 struct ws_win
*win
= NULL
;
4344 if (r
&& r
->ws
&& r
->ws
->focus
)
4352 #define SWM_MOVE_STEPS (50)
4355 move(struct ws_win
*win
, union arg
*args
)
4360 struct swm_region
*r
= NULL
;
4370 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4373 DNPRINTF(SWM_D_MOUSE
, "move: window: 0x%lx, floating: %s, transient: "
4374 "0x%lx\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4376 /* in max_stack mode should only move transients */
4377 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4381 if (win
->floating
== 0 && !win
->transient
) {
4382 store_float_geom(win
, r
);
4383 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4386 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4393 case SWM_ARG_ID_MOVELEFT
:
4394 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4397 case SWM_ARG_ID_MOVERIGHT
:
4398 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4401 case SWM_ARG_ID_MOVEUP
:
4402 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4405 case SWM_ARG_ID_MOVEDOWN
:
4406 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4413 constrain_window(win
, r
, 0);
4415 store_float_geom(win
, r
);
4419 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4420 GrabModeAsync
, None
, XCreateFontCursor(display
, XC_fleur
),
4421 CurrentTime
) != GrabSuccess
)
4424 /* get cursor offset from window root */
4425 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4429 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4430 SubstructureRedirectMask
, &ev
);
4432 case ConfigureRequest
:
4435 handler
[ev
.type
](&ev
);
4438 X(win
) = ev
.xmotion
.x_root
- wx
- border_width
;
4439 Y(win
) = ev
.xmotion
.y_root
- wy
- border_width
;
4441 constrain_window(win
, r
, 0);
4443 /* not free, don't sync more than 120 times / second */
4444 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4445 time
= ev
.xmotion
.time
;
4446 XSync(display
, False
);
4451 } while (ev
.type
!= ButtonRelease
);
4453 XSync(display
, False
);
4456 store_float_geom(win
, r
);
4457 XUngrabPointer(display
, CurrentTime
);
4460 drain_enter_notify();
4464 move_step(struct swm_region
*r
, union arg
*args
)
4466 struct ws_win
*win
= NULL
;
4468 if (r
&& r
->ws
&& r
->ws
->focus
)
4473 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4480 /* user/key callable function IDs */
4569 kf_search_workspace
,
4571 kf_dumpwins
, /* MUST BE LAST */
4575 /* key definitions */
4577 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4582 char name
[SWM_FUNCNAME_LEN
];
4583 void (*func
)(struct swm_region
*r
, union arg
*);
4585 } keyfuncs
[kf_invalid
+ 1] = {
4586 /* name function argument */
4587 { "cycle_layout", cycle_layout
, {0} },
4588 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4589 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4590 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4591 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4592 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4593 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4594 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4595 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4596 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4597 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4598 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4599 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4600 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4601 { "quit", quit
, {0} },
4602 { "restart", restart
, {0} },
4603 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4604 { "ws_1", switchws
, {.id
= 0} },
4605 { "ws_2", switchws
, {.id
= 1} },
4606 { "ws_3", switchws
, {.id
= 2} },
4607 { "ws_4", switchws
, {.id
= 3} },
4608 { "ws_5", switchws
, {.id
= 4} },
4609 { "ws_6", switchws
, {.id
= 5} },
4610 { "ws_7", switchws
, {.id
= 6} },
4611 { "ws_8", switchws
, {.id
= 7} },
4612 { "ws_9", switchws
, {.id
= 8} },
4613 { "ws_10", switchws
, {.id
= 9} },
4614 { "ws_11", switchws
, {.id
= 10} },
4615 { "ws_12", switchws
, {.id
= 11} },
4616 { "ws_13", switchws
, {.id
= 12} },
4617 { "ws_14", switchws
, {.id
= 13} },
4618 { "ws_15", switchws
, {.id
= 14} },
4619 { "ws_16", switchws
, {.id
= 15} },
4620 { "ws_17", switchws
, {.id
= 16} },
4621 { "ws_18", switchws
, {.id
= 17} },
4622 { "ws_19", switchws
, {.id
= 18} },
4623 { "ws_20", switchws
, {.id
= 19} },
4624 { "ws_21", switchws
, {.id
= 20} },
4625 { "ws_22", switchws
, {.id
= 21} },
4626 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4627 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4628 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4629 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4630 { "ws_prior", priorws
, {0} },
4631 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4632 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4633 { "mvws_1", send_to_ws
, {.id
= 0} },
4634 { "mvws_2", send_to_ws
, {.id
= 1} },
4635 { "mvws_3", send_to_ws
, {.id
= 2} },
4636 { "mvws_4", send_to_ws
, {.id
= 3} },
4637 { "mvws_5", send_to_ws
, {.id
= 4} },
4638 { "mvws_6", send_to_ws
, {.id
= 5} },
4639 { "mvws_7", send_to_ws
, {.id
= 6} },
4640 { "mvws_8", send_to_ws
, {.id
= 7} },
4641 { "mvws_9", send_to_ws
, {.id
= 8} },
4642 { "mvws_10", send_to_ws
, {.id
= 9} },
4643 { "mvws_11", send_to_ws
, {.id
= 10} },
4644 { "mvws_12", send_to_ws
, {.id
= 11} },
4645 { "mvws_13", send_to_ws
, {.id
= 12} },
4646 { "mvws_14", send_to_ws
, {.id
= 13} },
4647 { "mvws_15", send_to_ws
, {.id
= 14} },
4648 { "mvws_16", send_to_ws
, {.id
= 15} },
4649 { "mvws_17", send_to_ws
, {.id
= 16} },
4650 { "mvws_18", send_to_ws
, {.id
= 17} },
4651 { "mvws_19", send_to_ws
, {.id
= 18} },
4652 { "mvws_20", send_to_ws
, {.id
= 19} },
4653 { "mvws_21", send_to_ws
, {.id
= 20} },
4654 { "mvws_22", send_to_ws
, {.id
= 21} },
4655 { "bar_toggle", bar_toggle
, {0} },
4656 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4657 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4658 { "float_toggle", floating_toggle
,{0} },
4659 { "version", version
, {0} },
4660 { "spawn_custom", dummykeyfunc
, {0} },
4661 { "iconify", iconify
, {0} },
4662 { "uniconify", uniconify
, {0} },
4663 { "raise_toggle", raise_toggle
, {0} },
4664 { "button2", pressbutton
, {2} },
4665 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4666 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4667 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4668 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4669 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4670 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4671 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4672 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4673 { "name_workspace", name_workspace
, {0} },
4674 { "search_workspace", search_workspace
, {0} },
4675 { "search_win", search_win
, {0} },
4676 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4677 { "invalid key func", NULL
, {0} },
4680 RB_ENTRY(key
) entry
;
4683 enum keyfuncid funcid
;
4686 RB_HEAD(key_tree
, key
);
4689 key_cmp(struct key
*kp1
, struct key
*kp2
)
4691 if (kp1
->keysym
< kp2
->keysym
)
4693 if (kp1
->keysym
> kp2
->keysym
)
4696 if (kp1
->mod
< kp2
->mod
)
4698 if (kp1
->mod
> kp2
->mod
)
4704 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
4705 struct key_tree keys
;
4708 enum { client_click
, root_click
};
4710 unsigned int action
;
4712 unsigned int button
;
4713 void (*func
)(struct ws_win
*, union arg
*);
4716 /* action key mouse button func args */
4717 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4718 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4719 { client_click
, MODKEY
, Button1
, move
, {0} },
4723 update_modkey(unsigned int mod
)
4729 RB_FOREACH(kp
, key_tree
, &keys
)
4730 if (kp
->mod
& ShiftMask
)
4731 kp
->mod
= mod
| ShiftMask
;
4735 for (i
= 0; i
< LENGTH(buttons
); i
++)
4736 if (buttons
[i
].mask
& ShiftMask
)
4737 buttons
[i
].mask
= mod
| ShiftMask
;
4739 buttons
[i
].mask
= mod
;
4744 TAILQ_ENTRY(spawn_prog
) entry
;
4749 TAILQ_HEAD(spawn_list
, spawn_prog
);
4750 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4753 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4756 struct spawn_prog
*prog
= NULL
;
4758 char *ap
, **real_args
;
4760 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4763 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4764 if (!strcasecmp(spawn_name
, prog
->name
))
4768 warnx("spawn_custom: program %s not found", spawn_name
);
4772 /* make room for expanded args */
4773 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4774 err(1, "spawn_custom: calloc real_args");
4776 /* expand spawn_args into real_args */
4777 for (i
= 0; i
< prog
->argc
; i
++) {
4779 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4780 if (!strcasecmp(ap
, "$bar_border")) {
4782 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4784 err(1, "spawn_custom border color");
4785 } else if (!strcasecmp(ap
, "$bar_color")) {
4787 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4789 err(1, "spawn_custom bar color");
4790 } else if (!strcasecmp(ap
, "$bar_font")) {
4791 if ((real_args
[i
] = strdup(bar_fonts
))
4793 err(1, "spawn_custom bar fonts");
4794 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4796 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4798 err(1, "spawn_custom color font");
4799 } else if (!strcasecmp(ap
, "$color_focus")) {
4801 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4803 err(1, "spawn_custom color focus");
4804 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4806 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4808 err(1, "spawn_custom color unfocus");
4810 /* no match --> copy as is */
4811 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4812 err(1, "spawn_custom strdup(ap)");
4814 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4819 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4820 for (i
= 0; i
< prog
->argc
; i
++)
4821 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4822 DNPRINTF(SWM_D_SPAWN
, "\n");
4824 *ret_args
= real_args
;
4825 return (prog
->argc
);
4829 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4835 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4839 spawn(r
->ws
->idx
, &a
, 1);
4841 for (i
= 0; i
< spawn_argc
; i
++)
4847 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4853 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4857 if (pipe(select_list_pipe
) == -1)
4858 err(1, "pipe error");
4859 if (pipe(select_resp_pipe
) == -1)
4860 err(1, "pipe error");
4862 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4863 err(1, "could not disable SIGPIPE");
4864 switch (*pid
= fork()) {
4866 err(1, "cannot fork");
4869 if (dup2(select_list_pipe
[0], 0) == -1)
4871 if (dup2(select_resp_pipe
[1], 1) == -1)
4873 close(select_list_pipe
[1]);
4874 close(select_resp_pipe
[0]);
4875 spawn(r
->ws
->idx
, &a
, 0);
4877 default: /* parent */
4878 close(select_list_pipe
[0]);
4879 close(select_resp_pipe
[1]);
4883 for (i
= 0; i
< spawn_argc
; i
++)
4889 spawn_insert(char *name
, char *args
)
4891 char *arg
, *cp
, *ptr
;
4892 struct spawn_prog
*sp
;
4894 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
4896 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
4897 err(1, "spawn_insert: malloc");
4898 if ((sp
->name
= strdup(name
)) == NULL
)
4899 err(1, "spawn_insert: strdup");
4901 /* convert the arguments to an argument list */
4902 if ((ptr
= cp
= strdup(args
)) == NULL
)
4903 err(1, "spawn_insert: strdup");
4904 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
4905 /* empty field; skip it */
4910 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
4911 sizeof *sp
->argv
)) == NULL
)
4912 err(1, "spawn_insert: realloc");
4913 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
4914 err(1, "spawn_insert: strdup");
4918 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
4919 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
4923 spawn_remove(struct spawn_prog
*sp
)
4927 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
4929 TAILQ_REMOVE(&spawns
, sp
, entry
);
4930 for (i
= 0; i
< sp
->argc
; i
++)
4936 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
4940 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
4942 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
4945 spawn_insert(name
, args
);
4947 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
4951 setspawn(char *name
, char *args
)
4953 struct spawn_prog
*sp
;
4955 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
4960 TAILQ_FOREACH(sp
, &spawns
, entry
) {
4961 if (!strcmp(sp
->name
, name
)) {
4965 spawn_replace(sp
, name
, args
);
4966 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
4970 if (*args
== '\0') {
4971 warnx("error: setspawn: cannot find program: %s", name
);
4975 spawn_insert(name
, args
);
4976 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
4980 setconfspawn(char *selector
, char *value
, int flags
)
4982 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
4984 setspawn(selector
, value
);
4986 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
4993 setconfspawn("term", "xterm", 0);
4994 setconfspawn("spawn_term", "xterm", 0);
4995 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4996 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4997 setconfspawn("lock", "xlock", 0);
4998 setconfspawn("initscr", "initscreen.sh", 0);
4999 setconfspawn("menu", "dmenu_run"
5002 " -nf $bar_font_color"
5004 " -sf $bar_color", 0);
5005 setconfspawn("search", "dmenu"
5009 " -nf $bar_font_color"
5011 " -sf $bar_color", 0);
5012 setconfspawn("name_workspace", "dmenu"
5016 " -nf $bar_font_color"
5018 " -sf $bar_color", 0);
5022 #define SWM_MODNAME_SIZE 32
5023 #define SWM_KEY_WS "\n+ \t"
5025 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5029 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5030 if (mod
== NULL
|| ks
== NULL
) {
5031 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5034 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5035 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5041 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5042 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5044 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5045 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5047 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5049 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5051 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5053 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5055 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5057 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5058 *mod
|= ControlMask
;
5060 *ks
= XStringToKeysym(name
);
5061 XConvertCase(*ks
, ks
, &uks
);
5062 if (ks
== NoSymbol
) {
5064 "parsekeys: invalid key %s\n",
5070 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5075 strdupsafe(char *str
)
5080 return (strdup(str
));
5084 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5088 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5089 keyfuncs
[kfid
].name
, spawn_name
);
5091 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5092 err(1, "key_insert: malloc");
5097 kp
->spawn_name
= strdupsafe(spawn_name
);
5098 RB_INSERT(key_tree
, &keys
, kp
);
5100 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5104 key_lookup(unsigned int mod
, KeySym ks
)
5111 return (RB_FIND(key_tree
, &keys
, &kp
));
5115 key_remove(struct key
*kp
)
5117 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5119 RB_REMOVE(key_tree
, &keys
, kp
);
5120 free(kp
->spawn_name
);
5123 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5127 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5130 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5134 key_insert(mod
, ks
, kfid
, spawn_name
);
5136 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5140 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5145 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5146 keyfuncs
[kfid
].name
, spawn_name
);
5148 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5149 if (kfid
== kf_invalid
)
5152 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5153 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5156 if (kfid
== kf_invalid
) {
5157 warnx("error: setkeybinding: cannot find mod/key combination");
5158 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5162 key_insert(mod
, ks
, kfid
, spawn_name
);
5163 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5167 setconfbinding(char *selector
, char *value
, int flags
)
5169 enum keyfuncid kfid
;
5172 struct spawn_prog
*sp
;
5173 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5174 if (selector
== NULL
) {
5175 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5176 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5178 setkeybinding(mod
, ks
, kfid
, NULL
);
5183 /* search by key function name */
5184 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
5185 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5186 SWM_FUNCNAME_LEN
) == 0) {
5187 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5189 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5190 setkeybinding(mod
, ks
, kfid
, NULL
);
5196 /* search by custom spawn name */
5197 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5198 if (strcasecmp(selector
, sp
->name
) == 0) {
5199 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5201 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5202 setkeybinding(mod
, ks
, kf_spawn_custom
,
5209 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5216 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
5217 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, kf_flip_layout
, NULL
);
5218 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
5219 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
5220 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
5221 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
5222 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
5223 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
5224 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
5225 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
5226 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
5227 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
5228 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
5229 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
5230 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_custom
,"term");
5231 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
5232 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
5233 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
5234 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
5235 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
5236 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
5237 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
5238 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
5239 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
5240 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
5241 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
5242 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
5243 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
5244 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
5245 setkeybinding(MODKEY
, XK_F1
, kf_ws_11
, NULL
);
5246 setkeybinding(MODKEY
, XK_F2
, kf_ws_12
, NULL
);
5247 setkeybinding(MODKEY
, XK_F3
, kf_ws_13
, NULL
);
5248 setkeybinding(MODKEY
, XK_F4
, kf_ws_14
, NULL
);
5249 setkeybinding(MODKEY
, XK_F5
, kf_ws_15
, NULL
);
5250 setkeybinding(MODKEY
, XK_F6
, kf_ws_16
, NULL
);
5251 setkeybinding(MODKEY
, XK_F7
, kf_ws_17
, NULL
);
5252 setkeybinding(MODKEY
, XK_F8
, kf_ws_18
, NULL
);
5253 setkeybinding(MODKEY
, XK_F9
, kf_ws_19
, NULL
);
5254 setkeybinding(MODKEY
, XK_F10
, kf_ws_20
, NULL
);
5255 setkeybinding(MODKEY
, XK_F11
, kf_ws_21
, NULL
);
5256 setkeybinding(MODKEY
, XK_F12
, kf_ws_22
, NULL
);
5257 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
5258 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
5259 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
5260 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
5261 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
5262 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
5263 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
5264 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
5265 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
5266 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
5267 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
5268 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
5269 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
5270 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
5271 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
5272 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
5273 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
5274 setkeybinding(MODKEY
|ShiftMask
, XK_F1
, kf_mvws_11
, NULL
);
5275 setkeybinding(MODKEY
|ShiftMask
, XK_F2
, kf_mvws_12
, NULL
);
5276 setkeybinding(MODKEY
|ShiftMask
, XK_F3
, kf_mvws_13
, NULL
);
5277 setkeybinding(MODKEY
|ShiftMask
, XK_F4
, kf_mvws_14
, NULL
);
5278 setkeybinding(MODKEY
|ShiftMask
, XK_F5
, kf_mvws_15
, NULL
);
5279 setkeybinding(MODKEY
|ShiftMask
, XK_F6
, kf_mvws_16
, NULL
);
5280 setkeybinding(MODKEY
|ShiftMask
, XK_F7
, kf_mvws_17
, NULL
);
5281 setkeybinding(MODKEY
|ShiftMask
, XK_F8
, kf_mvws_18
, NULL
);
5282 setkeybinding(MODKEY
|ShiftMask
, XK_F9
, kf_mvws_19
, NULL
);
5283 setkeybinding(MODKEY
|ShiftMask
, XK_F10
, kf_mvws_20
, NULL
);
5284 setkeybinding(MODKEY
|ShiftMask
, XK_F11
, kf_mvws_21
, NULL
);
5285 setkeybinding(MODKEY
|ShiftMask
, XK_F12
, kf_mvws_22
, NULL
);
5286 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
5287 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
5288 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
5289 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
5290 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
5291 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
5292 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
5293 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
5294 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
5295 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
5296 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
5297 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
5298 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
5299 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
5300 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
5301 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
5302 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
5303 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
5304 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
5305 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
5306 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
5307 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
5308 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
5309 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
5310 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
5311 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
5313 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
5322 while (RB_EMPTY(&keys
) == 0) {
5323 kp
= RB_ROOT(&keys
);
5329 setkeymapping(char *selector
, char *value
, int flags
)
5331 char keymapping_file
[PATH_MAX
];
5332 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5333 if (value
[0] == '~')
5334 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5335 pwd
->pw_dir
, &value
[1]);
5337 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5339 /* load new key bindings; if it fails, revert to default bindings */
5340 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5344 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5349 updatenumlockmask(void)
5352 XModifierKeymap
*modmap
;
5354 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
5356 modmap
= XGetModifierMapping(display
);
5357 for (i
= 0; i
< 8; i
++)
5358 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
5359 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
5360 == XKeysymToKeycode(display
, XK_Num_Lock
))
5361 numlockmask
= (1 << i
);
5363 XFreeModifiermap(modmap
);
5371 unsigned int modifiers
[] =
5372 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
5375 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5376 updatenumlockmask();
5378 for (k
= 0; k
< ScreenCount(display
); k
++) {
5379 if (TAILQ_EMPTY(&screens
[k
].rl
))
5381 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
5382 RB_FOREACH(kp
, key_tree
, &keys
) {
5383 if ((code
= XKeysymToKeycode(display
, kp
->keysym
)))
5384 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5385 XGrabKey(display
, code
,
5386 kp
->mod
| modifiers
[j
],
5387 screens
[k
].root
, True
,
5388 GrabModeAsync
, GrabModeAsync
);
5394 grabbuttons(struct ws_win
*win
, int focused
)
5397 unsigned int modifiers
[] =
5398 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5400 updatenumlockmask();
5401 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
5403 for (i
= 0; i
< LENGTH(buttons
); i
++)
5404 if (buttons
[i
].action
== client_click
)
5405 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5406 XGrabButton(display
, buttons
[i
].button
,
5407 buttons
[i
].mask
| modifiers
[j
],
5408 win
->id
, False
, BUTTONMASK
,
5409 GrabModeAsync
, GrabModeSync
, None
,
5412 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
5413 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
5416 const char *quirkname
[] = {
5417 "NONE", /* config string for "no value" */
5426 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5427 #define SWM_Q_WS "\n|+ \t"
5429 parsequirks(char *qstr
, unsigned long *quirk
)
5439 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5441 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5442 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5443 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5444 DNPRINTF(SWM_D_QUIRK
,
5445 "parsequirks: %s\n", name
);
5450 *quirk
|= 1 << (i
-1);
5454 if (i
>= LENGTH(quirkname
)) {
5455 DNPRINTF(SWM_D_QUIRK
,
5456 "parsequirks: invalid quirk [%s]\n", name
);
5464 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5468 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5471 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5472 err(1, "quirk_insert: malloc");
5473 if ((qp
->class = strdup(class)) == NULL
)
5474 err(1, "quirk_insert: strdup");
5475 if ((qp
->name
= strdup(name
)) == NULL
)
5476 err(1, "quirk_insert: strdup");
5479 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5481 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5485 quirk_remove(struct quirk
*qp
)
5487 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5488 qp
->name
, qp
->quirk
);
5490 TAILQ_REMOVE(&quirks
, qp
, entry
);
5495 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5499 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5500 unsigned long quirk
)
5502 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5503 qp
->name
, qp
->quirk
);
5506 quirk_insert(class, name
, quirk
);
5508 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5512 setquirk(const char *class, const char *name
, unsigned long quirk
)
5516 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5519 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5520 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5524 quirk_replace(qp
, class, name
, quirk
);
5525 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5530 warnx("error: setquirk: cannot find class/name combination");
5534 quirk_insert(class, name
, quirk
);
5535 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5539 setconfquirk(char *selector
, char *value
, int flags
)
5541 char *cp
, *class, *name
;
5543 unsigned long quirks
;
5544 if (selector
== NULL
)
5546 if ((cp
= strchr(selector
, ':')) == NULL
)
5551 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5552 setquirk(class, name
, quirks
);
5559 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5560 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5561 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5562 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5563 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5564 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5565 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5566 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5567 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5568 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5569 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5570 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5571 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5574 /* conf file stuff */
5575 #define SWM_CONF_FILE "spectrwm.conf"
5576 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5580 SWM_S_BAR_AT_BOTTOM
,
5581 SWM_S_BAR_BORDER_WIDTH
,
5588 SWM_S_CLOCK_ENABLED
,
5591 SWM_S_CYCLE_VISIBLE
,
5593 SWM_S_DISABLE_BORDER
,
5595 SWM_S_FOCUS_CLOSE_WRAP
,
5596 SWM_S_FOCUS_DEFAULT
,
5602 SWM_S_STACK_ENABLED
,
5604 SWM_S_TITLE_CLASS_ENABLED
,
5605 SWM_S_TITLE_NAME_ENABLED
,
5606 SWM_S_URGENT_ENABLED
,
5607 SWM_S_VERBOSE_LAYOUT
,
5608 SWM_S_WINDOW_NAME_ENABLED
,
5609 SWM_S_WORKSPACE_LIMIT
5613 setconfvalue(char *selector
, char *value
, int flags
)
5619 case SWM_S_BAR_ACTION
:
5621 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5622 err(1, "setconfvalue: bar_action");
5624 case SWM_S_BAR_AT_BOTTOM
:
5625 bar_at_bottom
= atoi(value
);
5627 case SWM_S_BAR_BORDER_WIDTH
:
5628 bar_border_width
= atoi(value
);
5629 if (bar_border_width
< 0)
5630 bar_border_width
= 0;
5632 case SWM_S_BAR_DELAY
:
5633 bar_delay
= atoi(value
);
5635 case SWM_S_BAR_ENABLED
:
5636 bar_enabled
= atoi(value
);
5638 case SWM_S_BAR_FONT
:
5640 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5641 err(1, "setconfvalue: asprintf: failed to allocate "
5642 "memory for bar_fonts.");
5646 case SWM_S_BAR_FORMAT
:
5648 if ((bar_format
= strdup(value
)) == NULL
)
5649 err(1, "setconfvalue: bar_format");
5651 case SWM_S_BAR_JUSTIFY
:
5652 if (!strcmp(value
, "left"))
5653 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5654 else if (!strcmp(value
, "center"))
5655 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5656 else if (!strcmp(value
, "right"))
5657 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5659 errx(1, "invalid bar_justify");
5661 case SWM_S_BORDER_WIDTH
:
5662 border_width
= atoi(value
);
5663 if (border_width
< 0)
5666 case SWM_S_CLOCK_ENABLED
:
5667 clock_enabled
= atoi(value
);
5669 case SWM_S_CLOCK_FORMAT
:
5670 #ifndef SWM_DENY_CLOCK_FORMAT
5672 if ((clock_format
= strdup(value
)) == NULL
)
5673 err(1, "setconfvalue: clock_format");
5676 case SWM_S_CYCLE_EMPTY
:
5677 cycle_empty
= atoi(value
);
5679 case SWM_S_CYCLE_VISIBLE
:
5680 cycle_visible
= atoi(value
);
5682 case SWM_S_DIALOG_RATIO
:
5683 dialog_ratio
= atof(value
);
5684 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5687 case SWM_S_DISABLE_BORDER
:
5688 disable_border
= atoi(value
);
5690 case SWM_S_FOCUS_CLOSE
:
5691 if (!strcmp(value
, "first"))
5692 focus_close
= SWM_STACK_BOTTOM
;
5693 else if (!strcmp(value
, "last"))
5694 focus_close
= SWM_STACK_TOP
;
5695 else if (!strcmp(value
, "next"))
5696 focus_close
= SWM_STACK_ABOVE
;
5697 else if (!strcmp(value
, "previous"))
5698 focus_close
= SWM_STACK_BELOW
;
5700 errx(1, "focus_close");
5702 case SWM_S_FOCUS_CLOSE_WRAP
:
5703 focus_close_wrap
= atoi(value
);
5705 case SWM_S_FOCUS_DEFAULT
:
5706 if (!strcmp(value
, "last"))
5707 focus_default
= SWM_STACK_TOP
;
5708 else if (!strcmp(value
, "first"))
5709 focus_default
= SWM_STACK_BOTTOM
;
5711 errx(1, "focus_default");
5713 case SWM_S_FOCUS_MODE
:
5714 if (!strcmp(value
, "default"))
5715 focus_mode
= SWM_FOCUS_DEFAULT
;
5716 else if (!strcmp(value
, "follow_cursor"))
5717 focus_mode
= SWM_FOCUS_FOLLOW
;
5718 else if (!strcmp(value
, "synergy"))
5719 focus_mode
= SWM_FOCUS_SYNERGY
;
5721 errx(1, "focus_mode");
5723 case SWM_S_SPAWN_ORDER
:
5724 if (!strcmp(value
, "first"))
5725 spawn_position
= SWM_STACK_BOTTOM
;
5726 else if (!strcmp(value
, "last"))
5727 spawn_position
= SWM_STACK_TOP
;
5728 else if (!strcmp(value
, "next"))
5729 spawn_position
= SWM_STACK_ABOVE
;
5730 else if (!strcmp(value
, "previous"))
5731 spawn_position
= SWM_STACK_BELOW
;
5733 errx(1, "spawn_position");
5735 case SWM_S_SPAWN_TERM
:
5736 setconfspawn("term", value
, 0);
5737 setconfspawn("spawn_term", value
, 0);
5741 case SWM_S_SS_ENABLED
:
5742 ss_enabled
= atoi(value
);
5744 case SWM_S_STACK_ENABLED
:
5745 stack_enabled
= atoi(value
);
5747 case SWM_S_TERM_WIDTH
:
5748 term_width
= atoi(value
);
5752 case SWM_S_TITLE_CLASS_ENABLED
:
5753 title_class_enabled
= atoi(value
);
5755 case SWM_S_TITLE_NAME_ENABLED
:
5756 title_name_enabled
= atoi(value
);
5758 case SWM_S_URGENT_ENABLED
:
5759 urgent_enabled
= atoi(value
);
5761 case SWM_S_VERBOSE_LAYOUT
:
5762 verbose_layout
= atoi(value
);
5763 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5765 layouts
[i
].l_string
= fancy_stacker
;
5767 layouts
[i
].l_string
= plain_stacker
;
5770 case SWM_S_WINDOW_NAME_ENABLED
:
5771 window_name_enabled
= atoi(value
);
5773 case SWM_S_WORKSPACE_LIMIT
:
5774 workspace_limit
= atoi(value
);
5775 if (workspace_limit
> SWM_WS_MAX
)
5776 workspace_limit
= SWM_WS_MAX
;
5777 else if (workspace_limit
< 1)
5778 workspace_limit
= 1;
5787 setconfmodkey(char *selector
, char *value
, int flags
)
5789 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5790 update_modkey(Mod1Mask
);
5791 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5792 update_modkey(Mod2Mask
);
5793 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5794 update_modkey(Mod3Mask
);
5795 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5796 update_modkey(Mod4Mask
);
5803 setconfcolor(char *selector
, char *value
, int flags
)
5805 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5810 setconfregion(char *selector
, char *value
, int flags
)
5812 custom_region(value
);
5817 setautorun(char *selector
, char *value
, int flags
)
5827 if (getenv("SWM_STARTED"))
5831 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5832 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5834 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5835 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5838 * This is a little intricate
5840 * If the pid already exists we simply reuse it because it means it was
5841 * used before AND not claimed by manage_window. We get away with
5842 * altering it in the parent after INSERT because this can not be a race
5845 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5848 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5850 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5851 err(1, "setautorun: realloc");
5852 a
.argv
[argc
- 1] = ap
;
5855 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5856 err(1, "setautorun: realloc");
5857 a
.argv
[argc
] = NULL
;
5859 if ((pid
= fork()) == 0) {
5860 spawn(ws_id
, &a
, 1);
5869 p
= calloc(1, sizeof *p
);
5872 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
5882 setlayout(char *selector
, char *value
, int flags
)
5884 int ws_id
, i
, x
, mg
, ma
, si
, raise
, f
= 0;
5885 int st
= SWM_V_STACK
;
5887 struct workspace
*ws
;
5889 if (getenv("SWM_STARTED"))
5893 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
5894 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
5895 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5896 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5899 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5900 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
5902 if (!strcasecmp(s
, "vertical"))
5904 else if (!strcasecmp(s
, "vertical_flip")) {
5907 } else if (!strcasecmp(s
, "horizontal"))
5909 else if (!strcasecmp(s
, "horizontal_flip")) {
5912 } else if (!strcasecmp(s
, "fullscreen"))
5915 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5916 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5919 for (i
= 0; i
< ScreenCount(display
); i
++) {
5920 ws
= (struct workspace
*)&screens
[i
].ws
;
5921 ws
[ws_id
].cur_layout
= &layouts
[st
];
5923 ws
[ws_id
].always_raise
= raise
;
5924 if (st
== SWM_MAX_STACK
)
5928 for (x
= 0; x
< abs(mg
); x
++) {
5929 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5930 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
5931 SWM_ARG_ID_MASTERSHRINK
);
5935 for (x
= 0; x
< abs(ma
); x
++) {
5936 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5937 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
5938 SWM_ARG_ID_MASTERDEL
);
5942 for (x
= 0; x
< abs(si
); x
++) {
5943 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5944 si
>= 0 ? SWM_ARG_ID_STACKINC
:
5945 SWM_ARG_ID_STACKDEC
);
5950 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5951 SWM_ARG_ID_FLIPLAYOUT
);
5959 /* config options */
5960 struct config_option
{
5962 int (*func
)(char*, char*, int);
5965 struct config_option configopt
[] = {
5966 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
5967 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
5968 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
5969 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
5970 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
5971 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
5972 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
5973 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
5974 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
5975 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
5976 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
5977 { "keyboard_mapping", setkeymapping
, 0 },
5978 { "bind", setconfbinding
, 0 },
5979 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
5980 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
5981 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
5982 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
5983 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
5984 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
5985 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
5986 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
5987 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
5988 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
5989 { "modkey", setconfmodkey
, 0 },
5990 { "program", setconfspawn
, 0 },
5991 { "quirk", setconfquirk
, 0 },
5992 { "region", setconfregion
, 0 },
5993 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
5994 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
5995 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
5996 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
5997 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
5998 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
5999 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6000 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6001 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6002 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6003 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6004 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6005 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6006 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6007 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6008 { "autorun", setautorun
, 0 },
6009 { "layout", setlayout
, 0 },
6014 conf_load(char *filename
, int keymapping
)
6017 char *line
, *cp
, *optsub
, *optval
;
6018 size_t linelen
, lineno
= 0;
6019 int wordlen
, i
, optind
;
6020 struct config_option
*opt
;
6022 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6024 if (filename
== NULL
) {
6025 warnx("conf_load: no filename");
6028 if ((config
= fopen(filename
, "r")) == NULL
) {
6029 warn("conf_load: fopen: %s", filename
);
6033 while (!feof(config
)) {
6034 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6037 err(1, "%s", filename
);
6042 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6043 if (cp
[0] == '\0') {
6048 /* get config option */
6049 wordlen
= strcspn(cp
, "=[ \t\n");
6051 warnx("%s: line %zd: no option found",
6056 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6057 opt
= &configopt
[i
];
6058 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6059 strlen(opt
->optname
) == wordlen
) {
6065 warnx("%s: line %zd: unknown option %.*s",
6066 filename
, lineno
, wordlen
, cp
);
6069 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
6070 warnx("%s: line %zd: invalid option %.*s",
6071 filename
, lineno
, wordlen
, cp
);
6075 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6076 /* get [selector] if any */
6080 wordlen
= strcspn(cp
, "]");
6083 warnx("%s: line %zd: syntax error",
6088 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6090 warnx("%s: line %zd: unable to allocate"
6091 "memory for selector", filename
,
6097 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6099 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6101 optval
= strdup(cp
);
6102 /* call function to deal with it all */
6103 if (configopt
[optind
].func(optsub
, optval
,
6104 configopt
[optind
].funcflags
) != 0)
6105 errx(1, "%s: line %zd: invalid data for %s",
6106 filename
, lineno
, configopt
[optind
].optname
);
6113 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6120 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6126 set_child_transient(struct ws_win
*win
, Window
*trans
)
6128 struct ws_win
*parent
, *w
;
6129 XWMHints
*wmh
= NULL
;
6130 struct swm_region
*r
;
6131 struct workspace
*ws
;
6133 parent
= find_window(win
->transient
);
6135 parent
->child_trans
= win
;
6137 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6138 " for 0x%lx trans 0x%lx\n", win
->id
, win
->transient
);
6140 if (win
->hints
== NULL
) {
6141 warnx("no hints for 0x%lx", win
->id
);
6145 r
= root_to_region(win
->wa
.root
);
6147 /* parent doen't exist in our window list */
6148 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6152 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
6153 warnx("can't get hints for 0x%lx", w
->id
);
6157 if (win
->hints
->window_group
!= wmh
->window_group
)
6160 w
->child_trans
= win
;
6161 win
->transient
= w
->id
;
6163 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
6164 "transient to 0x%lx\n", win
->transient
);
6174 window_get_pid(Window win
)
6179 xcb_intern_atom_cookie_t c
;
6180 xcb_intern_atom_reply_t
*r
;
6181 xcb_get_property_cookie_t pc
;
6182 xcb_get_property_reply_t
*pr
;
6184 c
= xcb_intern_atom(conn
, False
, strlen("_NET_WM_PID"), "_NET_WM_PID");
6185 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
6192 pc
= xcb_get_property(conn
, False
, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
6193 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6196 if (pr
->type
!= XCB_ATOM_CARDINAL
)
6199 ret
= *(long *)xcb_get_property_value(pr
);
6205 c
= xcb_intern_atom(conn
, False
, strlen("_SWM_PID"), "_SWM_PID");
6206 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
6211 pc
= xcb_get_property(conn
, False
, win
, apid
, XCB_ATOM_STRING
,
6213 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6216 if (pr
->type
!= XCB_ATOM_STRING
)
6219 ret
= strtonum(xcb_get_property_value(pr
), 0, UINT_MAX
, &errstr
);
6226 manage_window(Window id
)
6229 struct workspace
*ws
;
6230 struct ws_win
*win
, *ww
;
6231 int format
, i
, ws_idx
, n
, border_me
= 0;
6232 unsigned long nitems
, bytes
;
6233 Atom ws_idx_atom
= 0, type
;
6234 Atom
*prot
= NULL
, *pp
;
6235 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
6236 struct swm_region
*r
;
6241 if ((win
= find_window(id
)) != NULL
)
6242 return (win
); /* already being managed */
6244 /* see if we are on the unmanaged list */
6245 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6246 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
6247 "window: 0x%lx\n", win
->id
);
6248 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6250 set_child_transient(win
, &trans
);
6252 if (trans
&& (ww
= find_window(trans
)))
6253 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
6254 else if ((ww
= win
->ws
->focus
) &&
6255 spawn_position
== SWM_STACK_ABOVE
)
6256 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
,
6258 else if (ww
&& spawn_position
== SWM_STACK_BELOW
)
6259 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6260 else switch (spawn_position
) {
6263 case SWM_STACK_ABOVE
:
6264 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6266 case SWM_STACK_BOTTOM
:
6267 case SWM_STACK_BELOW
:
6268 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6271 ewmh_update_actions(win
);
6275 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6276 err(1, "manage_window: calloc: failed to allocate memory for "
6282 /* see if we need to override the workspace */
6283 p
= find_pid(window_get_pid(id
));
6285 /* Get all the window data in one shot */
6286 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
6288 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
6289 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
6291 XGetWindowAttributes(display
, id
, &win
->wa
);
6292 XGetWMNormalHints(display
, id
, &win
->sh
, &win
->sh_mask
);
6293 win
->hints
= XGetWMHints(display
, id
);
6294 XGetTransientForHint(display
, id
, &trans
);
6296 win
->transient
= trans
;
6297 set_child_transient(win
, &trans
);
6298 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, "
6299 "transient: 0x%lx\n", win
->id
, win
->transient
);
6302 /* get supported protocols */
6303 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
6304 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
6305 if (*pp
== takefocus
)
6306 win
->take_focus
= 1;
6308 win
->can_delete
= 1;
6314 win
->iconic
= get_iconic(win
);
6317 * Figure out where to put the window. If it was previously assigned to
6318 * a workspace (either by spawn() or manually moving), and isn't
6319 * transient, * put it in the same workspace
6321 r
= root_to_region(win
->wa
.root
);
6323 ws
= &r
->s
->ws
[p
->ws
];
6324 TAILQ_REMOVE(&pidlist
, p
, entry
);
6327 } else if (prop
&& win
->transient
== 0) {
6328 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
6329 ws_idx
= strtonum((const char *)prop
, 0, workspace_limit
- 1,
6332 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
6335 ws
= &r
->s
->ws
[ws_idx
];
6338 /* this should launch transients in the same ws as parent */
6340 if ((ww
= find_window(trans
)) != NULL
)
6346 warnx("manage_window: fix this "
6352 /* set up the window layout */
6355 win
->s
= r
->s
; /* this never changes */
6356 if (trans
&& (ww
= find_window(trans
)))
6357 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
6358 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
6359 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
6361 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
6362 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6363 else switch (spawn_position
) {
6366 case SWM_STACK_ABOVE
:
6367 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6369 case SWM_STACK_BOTTOM
:
6370 case SWM_STACK_BELOW
:
6371 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6374 /* ignore window border if there is one. */
6375 WIDTH(win
) = win
->wa
.width
;
6376 HEIGHT(win
) = win
->wa
.height
;
6377 X(win
) = win
->wa
.x
+ win
->wa
.border_width
;
6378 Y(win
) = win
->wa
.y
+ win
->wa
.border_width
;
6380 win
->g_floatvalid
= 0;
6381 win
->floatmaxed
= 0;
6382 win
->ewmh_flags
= 0;
6384 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, (x,y) w x h: "
6385 "(%d,%d) %d x %d, ws: %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
6386 HEIGHT(win
), ws
->idx
);
6388 constrain_window(win
, r
, 0);
6390 /* Set window properties so we can remember this after reincarnation */
6391 if (ws_idx_atom
&& prop
== NULL
&&
6392 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
6394 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6396 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
6397 PropModeReplace
, ws_idx_str
, strlen((char *)ws_idx_str
));
6402 ewmh_autoquirk(win
);
6404 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
6405 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6406 win
->ch
.res_class
, win
->ch
.res_name
);
6408 /* java is retarded so treat it special */
6409 if (strstr(win
->ch
.res_name
, "sun-awt")) {
6414 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6415 if (!strcmp(win
->ch
.res_class
, qp
->class) &&
6416 !strcmp(win
->ch
.res_name
, qp
->name
)) {
6417 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
6418 "class: %s, name: %s\n", win
->ch
.res_class
,
6420 if (qp
->quirk
& SWM_Q_FLOAT
) {
6424 win
->quirks
= qp
->quirk
;
6429 /* alter window position if quirky */
6430 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6431 win
->manual
= 1; /* don't center the quirky windows */
6432 if (bar_enabled
&& Y(win
) < bar_height
)
6433 Y(win
) = bar_height
;
6434 if (WIDTH(win
) + X(win
) > WIDTH(r
))
6435 X(win
) = WIDTH(r
) - WIDTH(win
) - 2;
6439 /* Reset font sizes (the bruteforce way; no default keybinding). */
6440 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6441 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6442 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
6443 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6444 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
6447 ewmh_get_win_state(win
);
6448 ewmh_update_actions(win
);
6449 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
6454 X(win
) -= border_width
;
6455 Y(win
) -= border_width
;
6459 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
6460 PropertyChangeMask
| StructureNotifyMask
);
6462 /* floaters need to be mapped if they are in the current workspace */
6463 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
6464 XMapRaised(display
, win
->id
);
6470 free_window(struct ws_win
*win
)
6472 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%lx\n", win
->id
);
6477 /* needed for restart wm */
6478 set_win_state(win
, WithdrawnState
);
6480 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6482 if (win
->ch
.res_class
)
6483 XFree(win
->ch
.res_class
);
6484 if (win
->ch
.res_name
)
6485 XFree(win
->ch
.res_name
);
6490 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6496 unmanage_window(struct ws_win
*win
)
6498 struct ws_win
*parent
;
6503 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%lx\n", win
->id
);
6505 if (win
->transient
) {
6506 parent
= find_window(win
->transient
);
6508 parent
->child_trans
= NULL
;
6511 /* focus on root just in case */
6512 XSetInputFocus(display
, PointerRoot
, PointerRoot
, CurrentTime
);
6521 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
6522 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6526 focus_magic(struct ws_win
*win
)
6528 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%lx\n", WINID(win
));
6531 /* if there are no windows clear the status-bar */
6536 if (win
->child_trans
) {
6537 /* win = parent & has a transient so focus on that */
6539 focus_win(win
->child_trans
);
6540 if (win
->child_trans
->take_focus
)
6541 client_msg(win
, takefocus
);
6543 /* make sure transient hasn't disappeared */
6544 if (validate_win(win
->child_trans
) == 0) {
6545 focus_win(win
->child_trans
);
6546 if (win
->child_trans
->take_focus
)
6547 client_msg(win
->child_trans
, takefocus
);
6549 win
->child_trans
= NULL
;
6551 if (win
->take_focus
)
6552 client_msg(win
, takefocus
);
6558 if (win
->take_focus
)
6559 client_msg(win
, takefocus
);
6566 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%lx\n", e
->xexpose
.window
);
6573 XKeyEvent
*ev
= &e
->xkey
;
6575 struct swm_region
*r
;
6577 keysym
= XkbKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0, 0);
6578 if ((kp
= key_lookup(CLEANMASK(ev
->state
), keysym
)) == NULL
)
6580 if (keyfuncs
[kp
->funcid
].func
== NULL
)
6583 r
= root_to_region(ev
->root
);
6584 if (kp
->funcid
== kf_spawn_custom
)
6585 spawn_custom(r
, &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
6587 keyfuncs
[kp
->funcid
].func(r
, &(keyfuncs
[kp
->funcid
].args
));
6591 buttonpress(XEvent
*e
)
6595 XButtonPressedEvent
*ev
= &e
->xbutton
;
6597 if ((win
= find_window(ev
->window
)) == NULL
)
6601 action
= client_click
;
6603 for (i
= 0; i
< LENGTH(buttons
); i
++)
6604 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6605 buttons
[i
].button
== ev
->button
&&
6606 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
6607 buttons
[i
].func(win
, &buttons
[i
].args
);
6611 configurerequest(XEvent
*e
)
6613 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
6618 if ((win
= find_window(ev
->window
)) == NULL
)
6619 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6623 bzero(&wc
, sizeof wc
);
6626 wc
.width
= ev
->width
;
6627 wc
.height
= ev
->height
;
6628 wc
.border_width
= ev
->border_width
;
6629 wc
.sibling
= ev
->above
;
6630 wc
.stack_mode
= ev
->detail
;
6632 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: 0x%lx, "
6633 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", ev
->window
,
6634 YESNO(new), wc
.x
, wc
.y
, wc
.width
, wc
.height
);
6636 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
6637 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
6638 !(win
->sh_mask
& EWMH_F_FULLSCREEN
)) {
6639 win
->g_float
.x
= ev
->x
- X(win
->ws
->r
);
6640 win
->g_float
.y
= ev
->y
- Y(win
->ws
->r
);
6641 win
->g_float
.w
= ev
->width
;
6642 win
->g_float
.h
= ev
->height
;
6643 win
->g_floatvalid
= 1;
6645 if (win
->floating
) {
6646 win
->g
= win
->g_float
;
6647 win
->g
.x
+= X(win
->ws
->r
);
6648 win
->g
.y
+= Y(win
->ws
->r
);
6651 config_win(win
, ev
);
6654 config_win(win
, ev
);
6659 configurenotify(XEvent
*e
)
6663 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%lx\n",
6664 e
->xconfigure
.window
);
6666 win
= find_window(e
->xconfigure
.window
);
6668 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &win
->sh_mask
);
6672 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6673 drain_enter_notify();
6678 destroynotify(XEvent
*e
)
6681 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
6683 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%lx\n", ev
->window
);
6685 if ((win
= find_window(ev
->window
)) == NULL
) {
6686 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6692 /* make sure we focus on something */
6695 unmanage_window(win
);
6697 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6698 drain_enter_notify();
6703 enternotify(XEvent
*e
)
6705 XCrossingEvent
*ev
= &e
->xcrossing
;
6710 Window focus_return
;
6711 int revert_to_return
;
6713 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%lx, mode: %d, detail: "
6714 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6715 "state: %d\n", ev
->window
, ev
->mode
, ev
->detail
, ev
->root
,
6716 ev
->subwindow
, YESNO(ev
->same_screen
), YESNO(ev
->focus
), ev
->state
);
6718 if (ev
->mode
!= NotifyNormal
) {
6719 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6724 switch (focus_mode
) {
6725 case SWM_FOCUS_DEFAULT
:
6727 case SWM_FOCUS_FOLLOW
:
6729 case SWM_FOCUS_SYNERGY
:
6732 * all these checks need to be in this order because the
6733 * XCheckTypedWindowEvent relies on weeding out the previous events
6735 * making this code an option would enable a follow mouse for focus
6740 * state is set when we are switching workspaces and focus is set when
6741 * the window or a subwindow already has focus (occurs during restart).
6743 * Only honor the focus flag if last_focus_event is not FocusOut,
6744 * this allows spectrwm to continue to control focus when another
6745 * program is also playing with it.
6747 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6748 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6753 * happens when a window is created or destroyed and the border
6754 * crosses the mouse pointer and when switching ws
6756 * we need the subwindow test to see if we came from root in order
6757 * to give focus to floaters
6759 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6760 ev
->subwindow
== 0) {
6761 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6765 /* this window already has focus */
6766 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6767 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6771 /* this window is being deleted or moved to another ws */
6772 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6774 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6775 XPutBackEvent(display
, &cne
);
6779 if ((win
= find_window(ev
->window
)) == NULL
) {
6780 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6785 * In fullstack kill all enters unless they come from a different ws
6786 * (i.e. another region) or focus has been grabbed externally.
6788 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6789 last_focus_event
!= FocusOut
) {
6790 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6791 if ((w
= find_window(focus_return
)) == NULL
||
6793 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6801 if ((win
= find_window(ev
->window
)) == NULL
) {
6802 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6807 * if we have more enternotifies let them handle it in due time
6809 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
6810 DNPRINTF(SWM_D_EVENT
,
6811 "ignoring enternotify: got more enternotify\n");
6812 XPutBackEvent(display
, &cne
);
6819 /* lets us use one switch statement for arbitrary mode/detail combinations */
6820 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6823 focusevent(XEvent
*e
)
6827 u_int32_t mode_detail
;
6828 XFocusChangeEvent
*ev
= &e
->xfocus
;
6830 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: 0x%lx mode: %d "
6831 "detail: %d\n", ev
->type
== FocusIn
? "entering" : "leaving",
6832 ev
->window
, ev
->mode
, ev
->detail
);
6834 if (last_focus_event
== ev
->type
) {
6835 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
6839 last_focus_event
= ev
->type
;
6840 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
6842 switch (mode_detail
) {
6843 /* synergy client focus operations */
6844 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
6845 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
6847 /* synergy server focus operations */
6848 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
6850 /* Entering applications like rdesktop that mangle the pointer */
6851 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
6853 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
6854 XSetWindowBorder(display
, win
->id
,
6855 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
6856 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
6859 warnx("ignoring focusevent");
6860 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
6867 mapnotify(XEvent
*e
)
6870 XMapEvent
*ev
= &e
->xmap
;
6872 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%lx\n", ev
->window
);
6874 win
= manage_window(ev
->window
);
6876 set_win_state(win
, NormalState
);
6879 * focus_win can only set input focus on a mapped window.
6880 * make sure the window really has focus since it is just being mapped.
6882 if (win
->ws
->focus
== win
)
6887 mappingnotify(XEvent
*e
)
6889 XMappingEvent
*ev
= &e
->xmapping
;
6891 XRefreshKeyboardMapping(ev
);
6892 if (ev
->request
== MappingKeyboard
)
6897 maprequest(XEvent
*e
)
6900 struct swm_region
*r
;
6901 XWindowAttributes wa
;
6902 XMapRequestEvent
*ev
= &e
->xmaprequest
;
6904 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%lx\n",
6905 e
->xmaprequest
.window
);
6907 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
6909 if (wa
.override_redirect
)
6912 win
= manage_window(e
->xmaprequest
.window
);
6914 return; /* can't happen */
6918 /* make new win focused */
6919 r
= root_to_region(win
->wa
.root
);
6920 if (win
->ws
== r
->ws
)
6925 propertynotify(XEvent
*e
)
6928 XPropertyEvent
*ev
= &e
->xproperty
;
6931 name
= XGetAtomName(display
, ev
->atom
);
6932 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%lx, atom: %s\n",
6937 win
= find_window(ev
->window
);
6941 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
6942 update_iconic(win
, 0);
6943 XMapRaised(display
, win
->id
);
6951 case XA_WM_NORMAL_HINTS
:
6953 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
6954 warnx("normal hints: flag 0x%x", win
->sh
.flags
);
6955 if (win
->sh
.flags
& PMinSize
) {
6956 WIDTH(win
) = win
->sh
.min_width
;
6957 HEIGHT(win
) = win
->sh
.min_height
;
6958 warnx("min %d %d", WIDTH(win
), HEIGHT(win
));
6960 XMoveResizeWindow(display
, win
->id
,
6961 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
));
6973 unmapnotify(XEvent
*e
)
6977 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%lx\n", e
->xunmap
.window
);
6979 /* determine if we need to help unmanage this window */
6980 win
= find_window(e
->xunmap
.window
);
6984 if (getstate(e
->xunmap
.window
) == NormalState
) {
6985 unmanage_window(win
);
6988 /* giant hack for apps that don't destroy transient windows */
6989 /* eat a bunch of events to prevent remanaging the window */
6991 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6992 EnterWindowMask
, &cne
))
6994 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6995 StructureNotifyMask
, &cne
))
6997 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6998 SubstructureNotifyMask
, &cne
))
7000 /* resend unmap because we ated it */
7001 XUnmapWindow(display
, e
->xunmap
.window
);
7004 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7005 drain_enter_notify();
7009 visibilitynotify(XEvent
*e
)
7012 struct swm_region
*r
;
7014 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%lx\n",
7015 e
->xvisibility
.window
);
7016 if (e
->xvisibility
.state
== VisibilityUnobscured
)
7017 for (i
= 0; i
< ScreenCount(display
); i
++)
7018 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7019 if (e
->xvisibility
.window
== WINID(r
->bar
))
7024 clientmessage(XEvent
*e
)
7026 XClientMessageEvent
*ev
;
7031 win
= find_window(ev
->window
);
7033 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7034 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
7035 "unmanaged window.\n");
7036 e
->xmaprequest
.window
= ev
->window
;
7042 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx, type: %ld\n",
7043 ev
->window
, ev
->message_type
);
7045 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7046 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7049 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7050 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7051 if (win
->can_delete
)
7052 client_msg(win
, adelete
);
7054 XKillClient(display
, win
->id
);
7056 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7057 DNPRINTF(SWM_D_EVENT
,
7058 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7059 if (win
->floating
) {
7060 if (ev
->data
.l
[0] & (1<<8)) /* x */
7061 X(win
) = ev
->data
.l
[1];
7062 if (ev
->data
.l
[0] & (1<<9)) /* y */
7063 Y(win
) = ev
->data
.l
[2];
7064 if (ev
->data
.l
[0] & (1<<10)) /* width */
7065 WIDTH(win
) = ev
->data
.l
[3];
7066 if (ev
->data
.l
[0] & (1<<11)) /* height */
7067 HEIGHT(win
) = ev
->data
.l
[4];
7072 /* TODO: Change stack sizes */
7073 /* notify no change was made. */
7074 config_win(win
, NULL
);
7077 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
7078 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
7079 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
7081 ewmh_update_win_state(win
, ev
->data
.l
[2],
7089 xerror_start(Display
*d
, XErrorEvent
*ee
)
7096 xerror(Display
*d
, XErrorEvent
*ee
)
7098 /* warnx("error: %p %p", display, ee); */
7106 xerrorxlib
= XSetErrorHandler(xerror_start
);
7108 /* this causes an error if some other window manager is running */
7109 XSelectInput(display
, DefaultRootWindow(display
),
7110 SubstructureRedirectMask
);
7111 XSync(display
, False
);
7115 XSetErrorHandler(xerror
);
7116 XSync(display
, False
);
7121 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
7123 struct swm_region
*r
, *n
;
7124 struct workspace
*ws
= NULL
;
7127 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
7128 s
->idx
, w
, h
, x
, y
);
7130 /* remove any conflicting regions */
7131 n
= TAILQ_FIRST(&s
->rl
);
7134 n
= TAILQ_NEXT(r
, entry
);
7135 if (X(r
) < (x
+ w
) &&
7136 (X(r
) + WIDTH(r
)) > x
&&
7138 (Y(r
) + HEIGHT(r
)) > y
) {
7139 if (r
->ws
->r
!= NULL
)
7140 r
->ws
->old_r
= r
->ws
->r
;
7143 TAILQ_REMOVE(&s
->rl
, r
, entry
);
7144 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
7148 /* search old regions for one to reuse */
7150 /* size + location match */
7151 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7152 if (X(r
) == x
&& Y(r
) == y
&&
7153 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7157 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7158 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7162 TAILQ_REMOVE(&s
->orl
, r
, entry
);
7163 /* try to use old region's workspace */
7164 if (r
->ws
->r
== NULL
)
7167 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
7168 err(1, "new_region: calloc: failed to allocate memory "
7171 /* if we don't have a workspace already, find one */
7173 for (i
= 0; i
< workspace_limit
; i
++)
7174 if (s
->ws
[i
].r
== NULL
) {
7181 errx(1, "new_region: no free workspaces");
7192 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
7198 #ifdef SWM_XRR_HAS_CRTC
7200 XRRScreenResources
*sr
;
7203 #endif /* SWM_XRR_HAS_CRTC */
7204 struct swm_region
*r
;
7207 if (i
>= ScreenCount(display
))
7208 errx(1, "scan_xrandr: invalid screen");
7210 /* remove any old regions */
7211 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
7212 r
->ws
->old_r
= r
->ws
->r
= NULL
;
7214 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
7215 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
7219 /* map virtual screens onto physical screens */
7220 #ifdef SWM_XRR_HAS_CRTC
7221 if (xrandr_support
) {
7222 sr
= XRRGetScreenResourcesCurrent(display
, screens
[i
].root
);
7224 new_region(&screens
[i
], 0, 0,
7225 DisplayWidth(display
, i
),
7226 DisplayHeight(display
, i
));
7230 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
7231 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
7232 if (ci
->noutput
== 0)
7235 if (ci
!= NULL
&& ci
->mode
== None
)
7236 new_region(&screens
[i
], 0, 0,
7237 DisplayWidth(display
, i
),
7238 DisplayHeight(display
, i
));
7240 new_region(&screens
[i
],
7241 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
7244 XRRFreeCrtcInfo(ci
);
7245 XRRFreeScreenResources(sr
);
7247 #endif /* SWM_XRR_HAS_CRTC */
7249 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
7250 DisplayHeight(display
, i
));
7255 screenchange(XEvent
*e
) {
7256 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
7257 struct swm_region
*r
;
7260 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%lx\n", xe
->root
);
7262 if (!XRRUpdateConfiguration(e
))
7265 /* silly event doesn't include the screen index */
7266 for (i
= 0; i
< ScreenCount(display
); i
++)
7267 if (screens
[i
].root
== xe
->root
)
7269 if (i
>= ScreenCount(display
))
7270 errx(1, "screenchange: screen not found");
7272 /* brute force for now, just re-enumerate the regions */
7275 /* add bars to all regions */
7276 for (i
= 0; i
< ScreenCount(display
); i
++)
7277 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7280 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7281 drain_enter_notify();
7287 Window d1
, d2
, *wins
= NULL
;
7288 XWindowAttributes wa
;
7293 for (i
= 0; i
< ScreenCount(display
); i
++) {
7294 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
7297 /* attach windows to a region */
7298 /* normal windows */
7299 for (j
= 0; j
< no
; j
++) {
7300 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
7301 wa
.override_redirect
||
7302 XGetTransientForHint(display
, wins
[j
], &d1
))
7305 state
= getstate(wins
[j
]);
7306 manage
= state
== IconicState
;
7307 if (wa
.map_state
== IsViewable
|| manage
)
7308 manage_window(wins
[j
]);
7310 /* transient windows */
7311 for (j
= 0; j
< no
; j
++) {
7312 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
7313 wa
.override_redirect
)
7316 state
= getstate(wins
[j
]);
7317 manage
= state
== IconicState
;
7318 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
7320 manage_window(wins
[j
]);
7332 int i
, j
, k
, num_screens
;
7333 int errorbase
, major
, minor
;
7334 struct workspace
*ws
;
7337 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7338 if ((screens
= calloc(num_screens
,
7339 sizeof(struct swm_screen
))) == NULL
)
7340 err(1, "setup_screens: calloc: failed to allocate memory for "
7343 /* initial Xrandr setup */
7344 xrandr_support
= XRRQueryExtension(display
,
7345 &xrandr_eventbase
, &errorbase
);
7347 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
7350 /* map physical screens */
7351 for (i
= 0; i
< num_screens
; i
++) {
7352 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
7354 TAILQ_INIT(&screens
[i
].rl
);
7355 TAILQ_INIT(&screens
[i
].orl
);
7356 screens
[i
].root
= RootWindow(display
, i
);
7358 /* set default colors */
7359 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
7360 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
7361 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
7362 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
7363 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
7365 /* create graphics context on screen */
7366 gcv
.graphics_exposures
= 0;
7367 screens
[i
].bar_gc
= XCreateGC(display
, screens
[i
].root
,
7368 GCGraphicsExposures
, &gcv
);
7370 /* set default cursor */
7371 XDefineCursor(display
, screens
[i
].root
,
7372 XCreateFontCursor(display
, XC_left_ptr
));
7374 /* init all workspaces */
7375 /* XXX these should be dynamically allocated too */
7376 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
7377 ws
= &screens
[i
].ws
[j
];
7383 TAILQ_INIT(&ws
->winlist
);
7384 TAILQ_INIT(&ws
->unmanagedlist
);
7386 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
7387 if (layouts
[k
].l_config
!= NULL
)
7388 layouts
[k
].l_config(ws
,
7389 SWM_ARG_ID_STACKINIT
);
7390 ws
->cur_layout
= &layouts
[0];
7391 ws
->cur_layout
->l_string(ws
);
7397 XRRSelectInput(display
, screens
[i
].root
,
7398 RRScreenChangeNotifyMask
);
7405 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
7406 err(1, "setup_globals: strdup: failed to allocate memory.");
7408 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
7409 err(1, "setup_globals: strdup: failed to allocate memory.");
7416 Atom netwmcheck
, netwmname
, utf8_string
;
7419 /* work around sun jdk bugs, code from wmname */
7420 netwmcheck
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
7421 netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
7422 utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
7423 for (i
= 0; i
< ScreenCount(display
); i
++) {
7424 root
= screens
[i
].root
;
7425 win
= XCreateSimpleWindow(display
,root
, 0, 0, 1, 1, 0,
7426 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
,
7427 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
);
7429 XChangeProperty(display
, root
, netwmcheck
, XA_WINDOW
, 32,
7430 PropModeReplace
, (unsigned char *)&win
, 1);
7431 XChangeProperty(display
, win
, netwmcheck
, XA_WINDOW
, 32,
7432 PropModeReplace
, (unsigned char *)&win
, 1);
7433 XChangeProperty(display
, win
, netwmname
, utf8_string
, 8,
7434 PropModeReplace
, (unsigned char*)"LG3D", strlen("LG3D"));
7439 main(int argc
, char *argv
[])
7441 struct swm_region
*r
, *rr
;
7442 struct ws_win
*winfocus
= NULL
;
7445 char conf
[PATH_MAX
], *cfile
= NULL
;
7450 struct sigaction sact
;
7453 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
7454 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
7456 warnx("no locale support");
7458 if (!X_HAVE_UTF8_STRING
)
7459 warnx("no UTF-8 support");
7461 if (!(display
= XOpenDisplay(0)))
7462 errx(1, "can not open display");
7464 if (!(conn
= XGetXCBConnection(display
)))
7465 errx(1, "can not get XCB connection");
7468 errx(1, "other wm running");
7470 /* handle some signals */
7471 bzero(&sact
, sizeof(sact
));
7472 sigemptyset(&sact
.sa_mask
);
7474 sact
.sa_handler
= sighdlr
;
7475 sigaction(SIGINT
, &sact
, NULL
);
7476 sigaction(SIGQUIT
, &sact
, NULL
);
7477 sigaction(SIGTERM
, &sact
, NULL
);
7478 sigaction(SIGHUP
, &sact
, NULL
);
7480 sact
.sa_handler
= sighdlr
;
7481 sact
.sa_flags
= SA_NOCLDSTOP
;
7482 sigaction(SIGCHLD
, &sact
, NULL
);
7484 astate
= XInternAtom(display
, "WM_STATE", False
);
7485 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
7486 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
7487 takefocus
= XInternAtom(display
, "WM_TAKE_FOCUS", False
);
7488 a_wmname
= XInternAtom(display
, "WM_NAME", False
);
7489 a_netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
7490 a_utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
7491 a_string
= XInternAtom(display
, "STRING", False
);
7492 a_swm_iconic
= XInternAtom(display
, "_SWM_ICONIC", False
);
7494 /* look for local and global conf file */
7495 pwd
= getpwuid(getuid());
7497 errx(1, "invalid user: %d", getuid());
7506 for (i
= 0; ; i
++) {
7511 snprintf(conf
, sizeof conf
, "%s/.%s",
7512 pwd
->pw_dir
, SWM_CONF_FILE
);
7516 snprintf(conf
, sizeof conf
, "/etc/%s",
7521 snprintf(conf
, sizeof conf
, "%s/.%s",
7522 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
7526 snprintf(conf
, sizeof conf
, "/etc/%s",
7533 if (strlen(conf
) && stat(conf
, &sb
) != -1)
7534 if (S_ISREG(sb
.st_mode
)) {
7541 /* load conf (if any) */
7543 conf_load(cfile
, SWM_CONF_DEFAULT
);
7546 /* set some values to work around bad programs */
7548 /* grab existing windows (before we build the bars) */
7551 if (getenv("SWM_STARTED") == NULL
)
7552 setenv("SWM_STARTED", "YES", 1);
7554 /* setup all bars */
7555 for (i
= 0; i
< ScreenCount(display
); i
++)
7556 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
7557 if (winfocus
== NULL
)
7558 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
7566 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7567 drain_enter_notify();
7569 xfd
= ConnectionNumber(display
);
7571 while (XPending(display
)) {
7572 XNextEvent(display
, &e
);
7575 if (e
.type
< LASTEvent
) {
7576 DNPRINTF(SWM_D_EVENTQ
,"XEvent: handled: %s, "
7577 "window: 0x%lx, type: %s (%d), %d remaining"
7578 "\n", YESNO(handler
[e
.type
]),
7579 e
.xany
.window
, geteventname(&e
),
7580 e
.type
, QLength(display
));
7582 if (handler
[e
.type
])
7583 handler
[e
.type
](&e
);
7585 DNPRINTF(SWM_D_EVENTQ
, "XRandr Event: window: "
7586 "0x%lx, type: %s (%d)\n", e
.xany
.window
,
7587 xrandr_geteventname(&e
), e
.type
);
7589 switch (e
.type
- xrandr_eventbase
) {
7590 case RRScreenChangeNotify
:
7599 /* if we are being restarted go focus on first window */
7601 rr
= winfocus
->ws
->r
;
7603 /* not a visible window */
7607 /* move pointer to first screen if multi screen */
7608 if (ScreenCount(display
) > 1 || outputs
> 1)
7609 XWarpPointer(display
, None
, rr
->s
[0].root
,
7611 Y(rr
) + (bar_enabled
? bar_height
: 0));
7613 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7623 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7625 DNPRINTF(SWM_D_MISC
, "select failed");
7626 if (restart_wm
== 1)
7627 restart(NULL
, NULL
);
7628 if (search_resp
== 1)
7641 for (i
= 0; i
< ScreenCount(display
); ++i
)
7642 if (screens
[i
].bar_gc
!= NULL
)
7643 XFreeGC(display
, screens
[i
].bar_gc
);
7645 XFreeFontSet(display
, bar_fs
);
7646 xcb_disconnect(conn
);
7647 XCloseDisplay(display
);