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 <xcb/randr.h>
95 #include <xcb/xcb_icccm.h>
96 #include <xcb/xcb_keysyms.h>
97 #include <X11/Xproto.h>
98 #include <X11/Xutil.h>
99 #include <X11/extensions/Xrandr.h>
100 #include <X11/extensions/XTest.h>
108 #ifdef SPECTRWM_BUILDSTR
109 static const char *buildstr
= SPECTRWM_BUILDSTR
;
111 static const char *buildstr
= SPECTRWM_VERSION
;
115 # error XRandR versions less than 1.0 are not supported
120 #define SWM_XRR_HAS_CRTC
124 #if defined(__OpenBSD__)
125 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
126 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
127 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
128 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
129 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
130 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
131 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
132 #define xcb_icccm_get_wm_name xcb_get_wm_name
133 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
134 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
135 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
138 /*#define SWM_DEBUG*/
140 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
141 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
142 #define SWM_D_MISC 0x0001
143 #define SWM_D_EVENT 0x0002
144 #define SWM_D_WS 0x0004
145 #define SWM_D_FOCUS 0x0008
146 #define SWM_D_MOVE 0x0010
147 #define SWM_D_STACK 0x0020
148 #define SWM_D_MOUSE 0x0040
149 #define SWM_D_PROP 0x0080
150 #define SWM_D_CLASS 0x0100
151 #define SWM_D_KEY 0x0200
152 #define SWM_D_QUIRK 0x0400
153 #define SWM_D_SPAWN 0x0800
154 #define SWM_D_EVENTQ 0x1000
155 #define SWM_D_CONF 0x2000
156 #define SWM_D_BAR 0x4000
158 u_int32_t swm_debug
= 0
176 #define DPRINTF(x...)
177 #define DNPRINTF(n,x...)
180 #define LENGTH(x) (sizeof x / sizeof x[0])
181 #define MODKEY Mod1Mask
182 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
183 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
184 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
185 #define SWM_PROPLEN (16)
186 #define SWM_FUNCNAME_LEN (32)
187 #define SWM_KEYS_LEN (255)
188 #define SWM_QUIRK_LEN (64)
189 #define X(r) (r)->g.x
190 #define Y(r) (r)->g.y
191 #define WIDTH(r) (r)->g.w
192 #define HEIGHT(r) (r)->g.h
193 #define BORDER(w) (w->bordered ? border_width : 0)
194 #define MAX_X(r) ((r)->g.x + (r)->g.w)
195 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
196 #define SH_MIN(w) (w)->sh_mask & PMinSize
197 #define SH_MIN_W(w) (w)->sh.min_width
198 #define SH_MIN_H(w) (w)->sh.min_height
199 #define SH_MAX(w) (w)->sh_mask & PMaxSize
200 #define SH_MAX_W(w) (w)->sh.max_width
201 #define SH_MAX_H(w) (w)->sh.max_height
202 #define SH_INC(w) (w)->sh_mask & PResizeInc
203 #define SH_INC_W(w) (w)->sh.width_inc
204 #define SH_INC_H(w) (w)->sh.height_inc
205 #define SWM_MAX_FONT_STEPS (3)
206 #define WINID(w) ((w) ? (w)->id : 0)
207 #define YESNO(x) ((x) ? "yes" : "no")
209 #define SWM_FOCUS_DEFAULT (0)
210 #define SWM_FOCUS_SYNERGY (1)
211 #define SWM_FOCUS_FOLLOW (2)
213 #define SWM_CONF_DEFAULT (0)
214 #define SWM_CONF_KEYMAPPING (1)
217 #define SWM_LIB "/usr/local/lib/libswmhack.so"
224 xcb_atom_t takefocus
;
226 xcb_atom_t a_netwmname
;
227 xcb_atom_t a_utf8_string
;
229 xcb_atom_t a_swm_iconic
;
230 volatile sig_atomic_t running
= 1;
231 volatile sig_atomic_t restart_wm
= 0;
233 int last_focus_event
= FocusOut
;
234 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
238 int xrandr_eventbase
;
239 unsigned int numlockmask
= 0;
241 xcb_connection_t
*conn
;
244 int cycle_visible
= 0;
246 int font_adjusted
= 0;
247 unsigned int mod_key
= MODKEY
;
250 struct swm_region
*search_r
;
251 int select_list_pipe
[2];
252 int select_resp_pipe
[2];
254 volatile sig_atomic_t search_resp
;
255 int search_resp_action
;
257 struct search_window
{
258 TAILQ_ENTRY(search_window
) entry
;
262 xcb_window_t indicator
;
264 TAILQ_HEAD(search_winlist
, search_window
);
265 struct search_winlist search_wl
;
270 SWM_SEARCH_UNICONIFY
,
271 SWM_SEARCH_NAME_WORKSPACE
,
272 SWM_SEARCH_SEARCH_WORKSPACE
,
273 SWM_SEARCH_SEARCH_WINDOW
276 #define SWM_STACK_TOP (0)
277 #define SWM_STACK_BOTTOM (1)
278 #define SWM_STACK_ABOVE (2)
279 #define SWM_STACK_BELOW (3)
282 double dialog_ratio
= 0.6;
284 #define SWM_BAR_MAX (256)
285 #define SWM_BAR_JUSTIFY_LEFT (0)
286 #define SWM_BAR_JUSTIFY_CENTER (1)
287 #define SWM_BAR_JUSTIFY_RIGHT (2)
288 #define SWM_BAR_OFFSET (4)
289 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
290 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
291 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
292 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
293 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
295 char *bar_argv
[] = { NULL
, NULL
};
297 unsigned char bar_ext
[SWM_BAR_MAX
];
298 char bar_vertext
[SWM_BAR_MAX
];
300 sig_atomic_t bar_alarm
= 0;
303 int bar_border_width
= 1;
304 int bar_at_bottom
= 0;
306 int bar_extra_running
= 0;
309 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
310 char *bar_format
= NULL
;
311 int stack_enabled
= 1;
312 int clock_enabled
= 1;
313 int urgent_enabled
= 0;
314 char *clock_format
= NULL
;
315 int title_name_enabled
= 0;
316 int title_class_enabled
= 0;
317 int window_name_enabled
= 0;
318 int focus_mode
= SWM_FOCUS_DEFAULT
;
319 int focus_close
= SWM_STACK_BELOW
;
320 int focus_close_wrap
= 1;
321 int focus_default
= SWM_STACK_TOP
;
322 int spawn_position
= SWM_STACK_TOP
;
323 int disable_border
= 0;
324 int border_width
= 1;
325 int verbose_layout
= 0;
328 XFontSetExtents
*bar_fs_extents
;
332 #define SWM_MENU_FN (2)
333 #define SWM_MENU_NB (4)
334 #define SWM_MENU_NF (6)
335 #define SWM_MENU_SB (8)
336 #define SWM_MENU_SF (10)
338 /* layout manager data */
339 struct swm_geometry
{
352 struct swm_geometry g
;
355 /* virtual "screens" */
357 TAILQ_ENTRY(swm_region
) entry
;
358 struct swm_geometry g
;
359 struct workspace
*ws
; /* current workspace on this region */
360 struct workspace
*ws_prior
; /* prior workspace on this region */
361 struct swm_screen
*s
; /* screen idx */
364 TAILQ_HEAD(swm_region_list
, swm_region
);
367 TAILQ_ENTRY(ws_win
) entry
;
369 xcb_window_t transient
;
370 struct ws_win
*child_trans
; /* transient child window */
371 struct swm_geometry g
; /* current geometry */
372 struct swm_geometry g_float
; /* region coordinates */
373 int g_floatvalid
; /* g_float geometry validity */
374 int floatmaxed
; /* whether maxed by max_stack */
379 unsigned int ewmh_flags
;
380 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
386 unsigned long quirks
;
387 struct workspace
*ws
; /* always valid */
388 struct swm_screen
*s
; /* always valid, never changes */
389 XWindowAttributes wa
;
395 TAILQ_HEAD(ws_win_list
, ws_win
);
399 TAILQ_ENTRY(pid_e
) entry
;
403 TAILQ_HEAD(pid_list
, pid_e
);
404 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
406 /* layout handlers */
408 void vertical_config(struct workspace
*, int);
409 void vertical_stack(struct workspace
*, struct swm_geometry
*);
410 void horizontal_config(struct workspace
*, int);
411 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
412 void max_stack(struct workspace
*, struct swm_geometry
*);
413 void plain_stacker(struct workspace
*);
414 void fancy_stacker(struct workspace
*);
416 struct ws_win
*find_window(xcb_window_t
);
418 void grabbuttons(struct ws_win
*, int);
419 void new_region(struct swm_screen
*, int, int, int, int);
420 void unmanage_window(struct ws_win
*);
421 uint16_t getstate(xcb_window_t
);
423 int conf_load(char *, int);
426 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
427 void (*l_config
)(struct workspace
*, int);
429 #define SWM_L_FOCUSPREV (1<<0)
430 #define SWM_L_MAPONFOCUS (1<<1)
431 void (*l_string
)(struct workspace
*);
433 /* stack, configure */
434 { vertical_stack
, vertical_config
, 0, plain_stacker
},
435 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
437 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
438 { NULL
, NULL
, 0, NULL
},
441 /* position of max_stack mode in the layouts array, index into layouts! */
442 #define SWM_V_STACK (0)
443 #define SWM_H_STACK (1)
444 #define SWM_MAX_STACK (2)
446 #define SWM_H_SLICE (32)
447 #define SWM_V_SLICE (32)
449 /* define work spaces */
451 int idx
; /* workspace index */
452 char *name
; /* workspace name */
453 int always_raise
; /* raise windows on focus */
454 struct layout
*cur_layout
; /* current layout handlers */
455 struct ws_win
*focus
; /* may be NULL */
456 struct ws_win
*focus_prev
; /* may be NULL */
457 struct swm_region
*r
; /* may be NULL */
458 struct swm_region
*old_r
; /* may be NULL */
459 struct ws_win_list winlist
; /* list of windows in ws */
460 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
461 char stacker
[10]; /* display stacker and layout */
465 int horizontal_msize
;
467 int horizontal_stacks
;
478 SWM_S_COLOR_BAR_BORDER
,
479 SWM_S_COLOR_BAR_FONT
,
485 /* physical screen mapping */
486 #define SWM_WS_MAX (22) /* hard limit */
487 int workspace_limit
= 10; /* soft limit */
490 int idx
; /* screen index */
491 struct swm_region_list rl
; /* list of regions on this screen */
492 struct swm_region_list orl
; /* list of old regions */
494 struct workspace ws
[SWM_WS_MAX
];
500 } c
[SWM_S_COLOR_MAX
];
502 xcb_gcontext_t bar_gc
;
504 struct swm_screen
*screens
;
506 /* args to functions */
509 #define SWM_ARG_ID_FOCUSNEXT (0)
510 #define SWM_ARG_ID_FOCUSPREV (1)
511 #define SWM_ARG_ID_FOCUSMAIN (2)
512 #define SWM_ARG_ID_FOCUSCUR (4)
513 #define SWM_ARG_ID_SWAPNEXT (10)
514 #define SWM_ARG_ID_SWAPPREV (11)
515 #define SWM_ARG_ID_SWAPMAIN (12)
516 #define SWM_ARG_ID_MOVELAST (13)
517 #define SWM_ARG_ID_MASTERSHRINK (20)
518 #define SWM_ARG_ID_MASTERGROW (21)
519 #define SWM_ARG_ID_MASTERADD (22)
520 #define SWM_ARG_ID_MASTERDEL (23)
521 #define SWM_ARG_ID_FLIPLAYOUT (24)
522 #define SWM_ARG_ID_STACKRESET (30)
523 #define SWM_ARG_ID_STACKINIT (31)
524 #define SWM_ARG_ID_CYCLEWS_UP (40)
525 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
526 #define SWM_ARG_ID_CYCLESC_UP (42)
527 #define SWM_ARG_ID_CYCLESC_DOWN (43)
528 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
529 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
530 #define SWM_ARG_ID_STACKINC (50)
531 #define SWM_ARG_ID_STACKDEC (51)
532 #define SWM_ARG_ID_SS_ALL (60)
533 #define SWM_ARG_ID_SS_WINDOW (61)
534 #define SWM_ARG_ID_DONTCENTER (70)
535 #define SWM_ARG_ID_CENTER (71)
536 #define SWM_ARG_ID_KILLWINDOW (80)
537 #define SWM_ARG_ID_DELETEWINDOW (81)
538 #define SWM_ARG_ID_WIDTHGROW (90)
539 #define SWM_ARG_ID_WIDTHSHRINK (91)
540 #define SWM_ARG_ID_HEIGHTGROW (92)
541 #define SWM_ARG_ID_HEIGHTSHRINK (93)
542 #define SWM_ARG_ID_MOVEUP (100)
543 #define SWM_ARG_ID_MOVEDOWN (101)
544 #define SWM_ARG_ID_MOVELEFT (102)
545 #define SWM_ARG_ID_MOVERIGHT (103)
549 void focus(struct swm_region
*, union arg
*);
550 void focus_magic(struct ws_win
*);
554 TAILQ_ENTRY(quirk
) entry
;
558 #define SWM_Q_FLOAT (1<<0) /* float this window */
559 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
560 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
561 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
562 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
563 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
565 TAILQ_HEAD(quirk_list
, quirk
);
566 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
569 * Supported EWMH hints should be added to
570 * both the enum and the ewmh array
575 _NET_MOVERESIZE_WINDOW
,
576 _NET_WM_ACTION_CLOSE
,
577 _NET_WM_ACTION_FULLSCREEN
,
579 _NET_WM_ACTION_RESIZE
,
580 _NET_WM_ALLOWED_ACTIONS
,
583 _NET_WM_STATE_FULLSCREEN
,
584 _NET_WM_STATE_HIDDEN
,
585 _NET_WM_STATE_MAXIMIZED_HORZ
,
586 _NET_WM_STATE_MAXIMIZED_VERT
,
587 _NET_WM_STATE_SKIP_PAGER
,
588 _NET_WM_STATE_SKIP_TASKBAR
,
590 _NET_WM_WINDOW_TYPE_DIALOG
,
591 _NET_WM_WINDOW_TYPE_DOCK
,
592 _NET_WM_WINDOW_TYPE_NORMAL
,
593 _NET_WM_WINDOW_TYPE_SPLASH
,
594 _NET_WM_WINDOW_TYPE_TOOLBAR
,
595 _NET_WM_WINDOW_TYPE_UTILITY
,
596 _SWM_WM_STATE_MANUAL
,
603 } ewmh
[SWM_EWMH_HINT_MAX
] = {
604 /* must be in same order as in the enum */
605 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
606 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
607 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
608 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
609 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
610 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
611 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
612 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
613 {"_NET_WM_STATE", XCB_ATOM_NONE
},
614 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
615 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
616 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
617 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
618 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
619 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
620 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
621 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
622 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
623 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
624 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
625 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
626 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
627 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
628 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
631 void store_float_geom(struct ws_win
*, struct swm_region
*);
632 int floating_toggle_win(struct ws_win
*);
633 void constrain_window(struct ws_win
*, struct swm_region
*, int);
634 void update_window(struct ws_win
*);
635 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
636 char *get_win_name(xcb_window_t
);
637 xcb_atom_t
get_atom_from_string(const char *);
638 void map_window_raised(xcb_window_t
);
640 xcb_screen_t
*get_screen(int);
643 get_screen(int screen
)
645 xcb_screen_iterator_t i
;
647 i
= xcb_setup_roots_iterator(xcb_get_setup(conn
));
648 for (; i
.rem
; --screen
, xcb_screen_next(&i
))
658 xcb_get_input_focus_cookie_t c
;
659 xcb_get_input_focus_reply_t
*r
;
662 c
= xcb_get_input_focus(conn
);
664 r
= xcb_get_input_focus_reply(conn
, c
, NULL
);
670 map_window_raised(xcb_window_t win
)
672 uint32_t val
= XCB_STACK_MODE_ABOVE
;
674 xcb_configure_window(conn
, win
,
675 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
677 xcb_map_window(conn
, win
);
682 get_atom_from_string(const char *str
)
684 xcb_intern_atom_cookie_t c
;
685 xcb_intern_atom_reply_t
*r
;
688 c
= xcb_intern_atom(conn
, False
, strlen(str
), str
);
689 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
697 return (XCB_ATOM_NONE
);
701 update_iconic(struct ws_win
*win
, int newv
)
708 iprop
= get_atom_from_string("_SWM_ICONIC");
709 if (iprop
== XCB_ATOM_NONE
)
713 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
714 iprop
, XCB_ATOM_INTEGER
, 32, 1, &v
);
716 xcb_delete_property(conn
, win
->id
, iprop
);
720 get_iconic(struct ws_win
*win
)
722 int32_t v
= 0, *vtmp
;
724 xcb_get_property_cookie_t pc
;
725 xcb_get_property_reply_t
*pr
= NULL
;
727 iprop
= get_atom_from_string("_SWM_ICONIC");
728 if (iprop
== XCB_ATOM_NONE
)
731 pc
= xcb_get_property(conn
, False
, win
->id
, iprop
, XCB_ATOM_INTEGER
,
733 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
736 if (pr
->type
!= XCB_ATOM_INTEGER
|| pr
->format
!= 32)
738 vtmp
= xcb_get_property_value(pr
);
750 int i
, j
, num_screens
;
752 sup_list
= get_atom_from_string("_NET_SUPPORTED");
754 for (i
= 0; i
< LENGTH(ewmh
); i
++)
755 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
757 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
758 for (i
= 0; i
< num_screens
; i
++) {
759 /* Support check window will be created by workaround(). */
761 /* Report supported atoms */
762 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
763 for (j
= 0; j
< LENGTH(ewmh
); j
++)
764 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
765 screens
[i
].root
, sup_list
, XCB_ATOM_ATOM
, 32, 1,
774 xcb_atom_t sup_check
, sup_list
;
776 xcb_get_property_cookie_t pc
;
777 xcb_get_property_reply_t
*pr
;
779 sup_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
780 sup_list
= get_atom_from_string("_NET_SUPPORTED");
781 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
783 for (i
= 0; i
< num_screens
; i
++) {
784 /* Get the support check window and destroy it */
785 pc
= xcb_get_property(conn
, False
, screens
[i
].root
, sup_check
,
786 XCB_ATOM_WINDOW
, 0, 1);
787 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
789 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
791 xcb_destroy_window(conn
, id
);
792 xcb_delete_property(conn
, screens
[i
].root
, sup_check
);
793 xcb_delete_property(conn
, screens
[i
].root
, sup_list
);
801 ewmh_autoquirk(struct ws_win
*win
)
807 xcb_get_property_cookie_t c
;
808 xcb_get_property_reply_t
*r
;
810 c
= xcb_get_property(conn
, False
, win
->id
,
811 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, (~0L));
812 r
= xcb_get_property_reply(conn
, c
, NULL
);
815 n
= xcb_get_property_value_length(r
);
817 for (i
= 0; i
< n
; i
++) {
818 type
= *((xcb_atom_t
*)xcb_get_property_value(r
));
819 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
821 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
822 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
823 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
825 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
828 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
829 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
831 win
->quirks
= SWM_Q_FLOAT
;
838 #define SWM_EWMH_ACTION_COUNT_MAX (6)
839 #define EWMH_F_FULLSCREEN (1<<0)
840 #define EWMH_F_ABOVE (1<<1)
841 #define EWMH_F_HIDDEN (1<<2)
842 #define EWMH_F_SKIP_PAGER (1<<3)
843 #define EWMH_F_SKIP_TASKBAR (1<<4)
844 #define SWM_F_MANUAL (1<<5)
847 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
855 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%x, "
856 "fullscreen %s\n", win
->id
, YESNO(fs
));
859 if (!win
->g_floatvalid
)
860 store_float_geom(win
, win
->ws
->r
);
862 win
->g
= win
->ws
->r
->g
;
865 if (win
->g_floatvalid
) {
866 /* refloat at last floating relative position */
867 win
->g
= win
->g_float
;
868 X(win
) += X(win
->ws
->r
);
869 Y(win
) += Y(win
->ws
->r
);
877 ewmh_update_actions(struct ws_win
*win
)
879 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
885 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
888 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
889 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
892 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
893 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1,
897 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
898 #define _NET_WM_STATE_ADD 1 /* add/set property */
899 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
902 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
904 unsigned int mask
= 0;
905 unsigned int changed
= 0;
906 unsigned int orig_flags
;
911 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
912 mask
= EWMH_F_FULLSCREEN
;
913 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
915 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
917 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
918 mask
= EWMH_F_SKIP_PAGER
;
919 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
920 mask
= EWMH_F_SKIP_TASKBAR
;
923 orig_flags
= win
->ewmh_flags
;
926 case _NET_WM_STATE_REMOVE
:
927 win
->ewmh_flags
&= ~mask
;
929 case _NET_WM_STATE_ADD
:
930 win
->ewmh_flags
|= mask
;
932 case _NET_WM_STATE_TOGGLE
:
933 win
->ewmh_flags
^= mask
;
937 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
939 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
941 if (!floating_toggle_win(win
))
942 win
->ewmh_flags
= orig_flags
; /* revert */
943 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
945 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
946 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
948 if (!ewmh_set_win_fullscreen(win
,
949 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
950 win
->ewmh_flags
= orig_flags
; /* revert */
952 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
954 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
955 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
956 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
957 &ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
);
958 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
959 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
960 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
961 &ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
);
962 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
963 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
964 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
965 &ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
);
966 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
967 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
968 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
969 &ewmh
[_NET_WM_STATE_ABOVE
].atom
);
970 if (win
->ewmh_flags
& SWM_F_MANUAL
)
971 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, win
->id
,
972 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, 1,
973 &ewmh
[_SWM_WM_STATE_MANUAL
].atom
);
977 ewmh_get_win_state(struct ws_win
*win
)
980 xcb_get_property_cookie_t c
;
981 xcb_get_property_reply_t
*r
;
989 win
->ewmh_flags
|= EWMH_F_ABOVE
;
991 win
->ewmh_flags
|= SWM_F_MANUAL
;
993 c
= xcb_get_property(conn
, False
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
994 XCB_ATOM_ATOM
, 0, (~0L));
995 r
= xcb_get_property_reply(conn
, c
, NULL
);
999 states
= xcb_get_property_value(r
);
1000 n
= xcb_get_property_value_length(r
);
1004 for (i
= 0; i
< n
; i
++)
1005 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1011 geteventname(XEvent
*e
)
1020 name
= "KeyRelease";
1023 name
= "ButtonPress";
1026 name
= "ButtonRelease";
1029 name
= "MotionNotify";
1032 name
= "EnterNotify";
1035 name
= "LeaveNotify";
1044 name
= "KeymapNotify";
1049 case GraphicsExpose
:
1050 name
= "GraphicsExpose";
1055 case VisibilityNotify
:
1056 name
= "VisibilityNotify";
1059 name
= "CreateNotify";
1062 name
= "DestroyNotify";
1065 name
= "UnmapNotify";
1071 name
= "MapRequest";
1073 case ReparentNotify
:
1074 name
= "ReparentNotify";
1076 case ConfigureNotify
:
1077 name
= "ConfigureNotify";
1079 case ConfigureRequest
:
1080 name
= "ConfigureRequest";
1083 name
= "GravityNotify";
1086 name
= "ResizeRequest";
1088 case CirculateNotify
:
1089 name
= "CirculateNotify";
1091 case CirculateRequest
:
1092 name
= "CirculateRequest";
1094 case PropertyNotify
:
1095 name
= "PropertyNotify";
1097 case SelectionClear
:
1098 name
= "SelectionClear";
1100 case SelectionRequest
:
1101 name
= "SelectionRequest";
1103 case SelectionNotify
:
1104 name
= "SelectionNotify";
1106 case ColormapNotify
:
1107 name
= "ColormapNotify";
1110 name
= "ClientMessage";
1113 name
= "MappingNotify";
1123 xrandr_geteventname(XEvent
*e
)
1127 switch(e
->type
- xrandr_eventbase
) {
1128 case RRScreenChangeNotify
:
1129 name
= "RRScreenChangeNotify";
1139 dumpwins(struct swm_region
*r
, union arg
*args
)
1143 xcb_get_window_attributes_cookie_t c
;
1144 xcb_get_window_attributes_reply_t
*wa
;
1146 if (r
->ws
== NULL
) {
1147 warnx("dumpwins: invalid workspace");
1151 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1152 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1153 state
= getstate(win
->id
);
1154 c
= xcb_get_window_attributes(conn
, win
->id
);
1155 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1157 warnx("window: 0x%x, map_state: %d, state: %u, "
1158 "transient: 0x%x", win
->id
, wa
->map_state
,
1159 state
, win
->transient
);
1162 warnx("window: 0x%x, failed xcb_get_window_attributes",
1166 warnx("===== unmanaged window list =====");
1167 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1168 state
= getstate(win
->id
);
1169 c
= xcb_get_window_attributes(conn
, win
->id
);
1170 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1172 warnx("window: 0x%x, map_state: %d, state: %u, "
1173 "transient: 0x%x", win
->id
, wa
->map_state
,
1174 state
, win
->transient
);
1177 warnx("window: 0x%x, failed xcb_get_window_attributes",
1181 warnx("=================================");
1185 dumpwins(struct swm_region
*r
, union arg
*args
)
1188 #endif /* SWM_DEBUG */
1190 void expose(XEvent
*);
1191 void keypress(XEvent
*);
1192 void buttonpress(XEvent
*);
1193 void configurerequest(XEvent
*);
1194 void configurenotify(XEvent
*);
1195 void destroynotify(XEvent
*);
1196 void enternotify(XEvent
*);
1197 void focusevent(XEvent
*);
1198 void mapnotify(XEvent
*);
1199 void mappingnotify(XEvent
*);
1200 void maprequest(XEvent
*);
1201 void propertynotify(XEvent
*);
1202 void unmapnotify(XEvent
*);
1203 void visibilitynotify(XEvent
*);
1204 void clientmessage(XEvent
*);
1206 void (*handler
[LASTEvent
])(XEvent
*) = {
1208 [KeyPress
] = keypress
,
1209 [ButtonPress
] = buttonpress
,
1210 [ConfigureRequest
] = configurerequest
,
1211 [ConfigureNotify
] = configurenotify
,
1212 [DestroyNotify
] = destroynotify
,
1213 [EnterNotify
] = enternotify
,
1214 [FocusIn
] = focusevent
,
1215 [FocusOut
] = focusevent
,
1216 [MapNotify
] = mapnotify
,
1217 [MappingNotify
] = mappingnotify
,
1218 [MapRequest
] = maprequest
,
1219 [PropertyNotify
] = propertynotify
,
1220 [UnmapNotify
] = unmapnotify
,
1221 [VisibilityNotify
] = visibilitynotify
,
1222 [ClientMessage
] = clientmessage
,
1228 int saved_errno
, status
;
1231 saved_errno
= errno
;
1235 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1240 if (errno
!= ECHILD
)
1241 warn("sighdlr: waitpid");
1242 #endif /* SWM_DEBUG */
1245 if (pid
== searchpid
)
1249 if (WIFEXITED(status
)) {
1250 if (WEXITSTATUS(status
) != 0)
1251 warnx("sighdlr: child exit status: %d",
1252 WEXITSTATUS(status
));
1254 warnx("sighdlr: child is terminated "
1256 #endif /* SWM_DEBUG */
1270 errno
= saved_errno
;
1276 struct pid_e
*p
= NULL
;
1278 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1283 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1292 name_to_color(const char *colorname
)
1294 uint32_t result
= 0;
1295 char cname
[32] = "#";
1296 xcb_screen_t
*screen
;
1297 xcb_colormap_t cmap
;
1298 xcb_alloc_named_color_cookie_t c
;
1299 xcb_alloc_named_color_reply_t
*r
;
1301 /* XXX - does not support rgb:/RR/GG/BB
1302 * will need to use xcb_alloc_color
1304 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
1305 cmap
= screen
->default_colormap
;
1307 c
= xcb_alloc_named_color(conn
, cmap
, strlen(colorname
), colorname
);
1308 r
= xcb_alloc_named_color_reply(conn
, c
, NULL
);
1310 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1311 c
= xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1313 r
= xcb_alloc_named_color_reply(conn
, c
, NULL
);
1319 warnx("color '%s' not found", colorname
);
1325 setscreencolor(char *val
, int i
, int c
)
1329 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1330 if (i
> 0 && i
<= num_screens
) {
1331 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1332 free(screens
[i
- 1].c
[c
].name
);
1333 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1335 } else if (i
== -1) {
1336 for (i
= 0; i
< num_screens
; i
++) {
1337 screens
[i
].c
[c
].color
= name_to_color(val
);
1338 free(screens
[i
].c
[c
].name
);
1339 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1343 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1348 fancy_stacker(struct workspace
*ws
)
1350 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1351 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1352 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1353 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1354 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1355 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1356 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1357 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1358 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1362 plain_stacker(struct workspace
*ws
)
1364 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1365 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1366 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1367 sizeof ws
->stacker
);
1368 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1369 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1370 sizeof ws
->stacker
);
1374 custom_region(char *val
)
1376 unsigned int sidx
, x
, y
, w
, h
;
1378 xcb_screen_t
*screen
;
1380 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1381 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1382 errx(1, "invalid custom region, "
1383 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1384 if (sidx
< 1 || sidx
> num_screens
)
1385 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1389 screen
= get_screen(sidx
);
1391 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1393 if (x
> screen
->width_in_pixels
||
1394 y
> screen
->height_in_pixels
||
1395 w
+ x
> screen
->width_in_pixels
||
1396 h
+ y
> screen
->height_in_pixels
) {
1397 warnx("ignoring region %ux%u+%u+%u - not within screen "
1398 "boundaries (%ux%u)", w
, h
, x
, y
,
1399 screen
->width_in_pixels
, screen
->height_in_pixels
);
1403 new_region(&screens
[sidx
], x
, y
, w
, h
);
1407 socket_setnonblock(int fd
)
1411 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1412 err(1, "fcntl F_GETFL");
1413 flags
|= O_NONBLOCK
;
1414 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1415 err(1, "fcntl F_SETFL");
1419 bar_print(struct swm_region
*r
, const char *s
)
1423 xcb_rectangle_t rect
;
1425 XRectangle ibox
, lbox
;
1428 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1430 switch (bar_justify
) {
1431 case SWM_BAR_JUSTIFY_LEFT
:
1434 case SWM_BAR_JUSTIFY_CENTER
:
1435 x
= (WIDTH(r
) - lbox
.width
) / 2;
1437 case SWM_BAR_JUSTIFY_RIGHT
:
1438 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1442 if (x
< SWM_BAR_OFFSET
)
1447 rect
.width
= WIDTH(r
->bar
);
1448 rect
.height
= HEIGHT(r
->bar
);
1450 /* clear back buffer */
1451 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
1452 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1453 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
,
1454 sizeof(rect
), &rect
);
1456 /* draw back buffer */
1457 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
;
1458 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1459 xcb_image_text_8(conn
, len
, r
->bar
->buffer
, r
->s
->bar_gc
, x
,
1460 (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1464 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
1465 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
1469 bar_extra_stop(void)
1473 bzero(bar_pipe
, sizeof bar_pipe
);
1476 kill(bar_pid
, SIGTERM
);
1479 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1484 bar_class_name(char *s
, size_t sz
, struct swm_region
*r
)
1486 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1488 if (r
->ws
->focus
->ch
.res_class
!= NULL
)
1489 strlcat(s
, r
->ws
->focus
->ch
.res_class
, sz
);
1493 bar_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1495 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1497 if (r
->ws
->focus
->ch
.res_name
!= NULL
)
1498 strlcat(s
, r
->ws
->focus
->ch
.res_name
, sz
);
1502 bar_class_title_name(char *s
, size_t sz
, struct swm_region
*r
)
1504 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1507 bar_class_name(s
, sz
, r
);
1508 strlcat(s
, ":", sz
);
1509 bar_title_name(s
, sz
, r
);
1513 bar_window_float(char *s
, size_t sz
, struct swm_region
*r
)
1515 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1517 if (r
->ws
->focus
->floating
)
1518 strlcat(s
, "(f)", sz
);
1522 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
1526 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
1528 if ((title
= get_win_name(r
->ws
->focus
->id
)) == NULL
)
1531 strlcat(s
, title
, sz
);
1535 int urgent
[SWM_WS_MAX
];
1537 bar_urgent(char *s
, size_t sz
)
1540 int i
, j
, num_screens
;
1542 xcb_get_property_cookie_t c
;
1543 xcb_icccm_wm_hints_t hints
;
1545 for (i
= 0; i
< workspace_limit
; i
++)
1548 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1549 for (i
= 0; i
< num_screens
; i
++)
1550 for (j
= 0; j
< workspace_limit
; j
++)
1551 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1552 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
1553 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
1556 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
1560 for (i
= 0; i
< workspace_limit
; i
++) {
1562 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1564 snprintf(b
, sizeof b
, "- ");
1570 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
1572 if (r
== NULL
|| r
->ws
== NULL
)
1574 if (r
->ws
->name
!= NULL
)
1575 strlcat(s
, r
->ws
->name
, sz
);
1578 /* build the default bar format according to the defined enabled options */
1580 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
1582 /* if format provided, just copy the buffers */
1583 if (bar_format
!= NULL
) {
1584 strlcpy(fmtnew
, fmtexp
, sz
);
1588 /* reset the output buffer */
1591 strlcat(fmtnew
, "+N:+I ", sz
);
1593 strlcat(fmtnew
, "+S", sz
);
1594 strlcat(fmtnew
, " ", sz
);
1596 /* only show the workspace name if there's actually one */
1597 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
1598 strlcat(fmtnew
, "<+D>", sz
);
1599 strlcat(fmtnew
, "+3<", sz
);
1601 if (clock_enabled
) {
1602 strlcat(fmtnew
, fmtexp
, sz
);
1603 strlcat(fmtnew
, "+4<", sz
);
1606 /* bar_urgent already adds the space before the last asterisk */
1608 strlcat(fmtnew
, "* +U*+4<", sz
);
1610 if (title_class_enabled
) {
1611 strlcat(fmtnew
, "+C", sz
);
1612 if (title_name_enabled
== 0)
1613 strlcat(fmtnew
, "+4<", sz
);
1616 /* checks needed by the colon and floating strlcat(3) calls below */
1617 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
1618 if (title_name_enabled
) {
1619 if (title_class_enabled
)
1620 strlcat(fmtnew
, ":", sz
);
1621 strlcat(fmtnew
, "+T+4<", sz
);
1623 if (window_name_enabled
) {
1624 if (r
->ws
->focus
->floating
)
1625 strlcat(fmtnew
, "+F ", sz
);
1626 strlcat(fmtnew
, "+64W ", sz
);
1630 /* finally add the action script output and the version */
1631 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
1635 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
1637 /* special case; no limit given, pad one space, instead */
1638 if (*limit
== sz
- 1)
1640 snprintf(tmp
, sz
, "%*s", *limit
, " ");
1643 /* replaces the bar format character sequences (like in tmux(1)) */
1645 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
1649 char tmp
[SWM_BAR_MAX
];
1653 /* reset strlcat(3) buffer */
1656 /* get number, if any */
1659 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
1660 limit
= sizeof tmp
- 1;
1661 if (limit
<= 0 || limit
>= sizeof tmp
)
1662 limit
= sizeof tmp
- 1;
1664 /* there is nothing to replace (ie EOL) */
1671 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
1674 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
1677 bar_class_name(tmp
, sizeof tmp
, r
);
1680 bar_workspace_name(tmp
, sizeof tmp
, r
);
1683 bar_window_float(tmp
, sizeof tmp
, r
);
1686 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
1689 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
1692 bar_class_title_name(tmp
, sizeof tmp
, r
);
1695 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
1698 bar_title_name(tmp
, sizeof tmp
, r
);
1701 bar_urgent(tmp
, sizeof tmp
);
1704 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
1707 bar_window_name(tmp
, sizeof tmp
, r
);
1710 /* unknown character sequence; copy as-is */
1711 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
1719 while (limit
-- > 0) {
1720 if (*offrep
>= sz
- 1)
1722 fmtrep
[(*offrep
)++] = *ptr
++;
1730 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
1735 while (*fmt
!= '\0') {
1737 /* skip ordinary characters */
1740 fmtrep
[off
++] = *fmt
++;
1744 /* character sequence found; replace it */
1745 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
1754 bar_fmt_expand(char *fmtexp
, size_t sz
)
1761 /* start by grabbing the current time and date */
1763 localtime_r(&tmt
, &tm
);
1765 /* figure out what to expand */
1766 if (bar_format
!= NULL
)
1768 else if (bar_format
== NULL
&& clock_enabled
)
1770 /* if nothing to expand bail out */
1776 /* copy as-is, just in case the format shouldn't be expanded below */
1777 strlcpy(fmtexp
, fmt
, sz
);
1778 /* finally pass the string through strftime(3) */
1779 #ifndef SWM_DENY_CLOCK_FORMAT
1780 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
1781 warnx("format too long");
1789 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
1790 char fmtrep
[SWM_BAR_MAX
];
1792 struct swm_region
*r
;
1794 /* expand the format by first passing it through strftime(3) */
1795 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
1797 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1798 for (i
= 0; i
< num_screens
; i
++) {
1799 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1802 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
1803 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
1804 bar_print(r
, fmtrep
);
1815 if (bar_enabled
== 0)
1817 if (bar_extra
&& bar_extra_running
) {
1818 /* ignore short reads; it'll correct itself */
1819 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1820 if (b
&& b
[len
- 1] == '\n') {
1822 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1824 if (b
== NULL
&& errno
!= EAGAIN
) {
1825 warn("bar_update: bar_extra failed");
1829 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1842 bar_toggle(struct swm_region
*r
, union arg
*args
)
1844 struct swm_region
*tmpr
;
1847 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1849 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1851 for (i
= 0; i
< num_screens
; i
++)
1852 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1854 xcb_unmap_window(conn
, tmpr
->bar
->id
);
1856 for (i
= 0; i
< num_screens
; i
++)
1857 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1859 map_window_raised(tmpr
->bar
->id
);
1862 bar_enabled
= !bar_enabled
;
1865 /* must be after stack */
1872 struct swm_region
*r
;
1876 /* do this here because the conf file is in memory */
1877 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1878 /* launch external status app */
1879 bar_extra_running
= 1;
1880 if (pipe(bar_pipe
) == -1)
1881 err(1, "pipe error");
1882 socket_setnonblock(bar_pipe
[0]);
1883 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1884 if (dup2(bar_pipe
[0], 0) == -1)
1886 if (dup2(bar_pipe
[1], 1) == -1)
1888 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1889 err(1, "could not disable SIGPIPE");
1890 switch (bar_pid
= fork()) {
1892 err(1, "cannot fork");
1896 execvp(bar_argv
[0], bar_argv
);
1897 err(1, "%s external app failed", bar_argv
[0]);
1899 default: /* parent */
1905 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
1906 for (i
= 0; i
< num_screens
; i
++)
1907 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1910 wa
[0] = screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1911 wa
[1] = screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1912 xcb_change_window_attributes(conn
, r
->bar
->id
,
1913 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
1919 bar_setup(struct swm_region
*r
)
1921 char *default_string
;
1922 char **missing_charsets
;
1923 int num_missing_charsets
= 0;
1925 xcb_screen_t
*screen
= get_screen(r
->s
->idx
);
1929 XFreeFontSet(display
, bar_fs
);
1933 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
1934 err(1, "bar_setup: calloc: failed to allocate memory.");
1936 DNPRINTF(SWM_D_BAR
, "bar_setup: loading bar_fonts: %s\n", bar_fonts
);
1938 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
1939 &num_missing_charsets
, &default_string
);
1941 if (num_missing_charsets
> 0) {
1942 warnx("Unable to load charset(s):");
1944 for (i
= 0; i
< num_missing_charsets
; ++i
)
1945 warnx("%s", missing_charsets
[i
]);
1947 XFreeStringList(missing_charsets
);
1949 if (strcmp(default_string
, ""))
1950 warnx("Glyphs from those sets will be replaced "
1951 "by '%s'.", default_string
);
1953 warnx("Glyphs from those sets won't be drawn.");
1957 errx(1, "Error creating font set structure.");
1959 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
1961 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
1962 2 * bar_border_width
;
1968 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1969 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
1970 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
1972 r
->bar
->id
= xcb_generate_id(conn
);
1973 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
1974 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
;
1975 xcb_create_window(conn
, screen
->root_depth
, r
->bar
->id
, r
->s
->root
,
1976 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
1977 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1978 screen
->root_visual
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
,
1981 r
->bar
->buffer
= xcb_generate_id(conn
);
1982 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
1983 WIDTH(r
->bar
), HEIGHT(r
->bar
));
1985 xcb_randr_select_input(conn
, r
->bar
->id
,
1986 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
1989 map_window_raised(r
->bar
->id
);
1991 DNPRINTF(SWM_D_BAR
, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
1992 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
1995 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1996 err(1, "could not install bar_signal");
2001 bar_cleanup(struct swm_region
*r
)
2005 xcb_destroy_window(conn
, r
->bar
->id
);
2006 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2012 drain_enter_notify(void)
2016 while (xcb_poll_for_event(conn
))
2019 DNPRINTF(SWM_D_EVENT
, "drain_enter_notify: drained: %d\n", i
);
2023 set_win_state(struct ws_win
*win
, uint16_t state
)
2025 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2027 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%x\n", win
->id
);
2032 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, astate
,
2033 astate
, 32, 2, data
);
2037 getstate(xcb_window_t w
)
2039 uint16_t result
= 0;
2040 xcb_get_property_cookie_t c
;
2041 xcb_get_property_reply_t
*r
;
2043 c
= xcb_get_property(conn
, False
, w
, astate
, astate
, 0L, 2L);
2044 r
= xcb_get_property_reply(conn
, c
, NULL
);
2047 result
= *((uint16_t *)xcb_get_property_value(r
));
2055 version(struct swm_region
*r
, union arg
*args
)
2057 bar_version
= !bar_version
;
2059 snprintf(bar_vertext
, sizeof bar_vertext
,
2060 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2062 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2067 client_msg(struct ws_win
*win
, xcb_atom_t a
)
2069 xcb_client_message_event_t ev
;
2074 bzero(&ev
, sizeof ev
);
2075 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2076 ev
.window
= win
->id
;
2079 ev
.data
.data32
[0] = a
;
2080 ev
.data
.data32
[1] = XCB_CURRENT_TIME
;
2082 xcb_send_event(conn
, False
, win
->id
,
2083 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2087 /* synthetic response to a ConfigureRequest when not making a change */
2089 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
2091 xcb_configure_notify_event_t ce
;
2096 /* send notification of unchanged state. */
2097 bzero(&ce
, sizeof(ce
));
2098 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2101 ce
.width
= WIDTH(win
);
2102 ce
.height
= HEIGHT(win
);
2103 ce
.override_redirect
= False
;
2108 ce
.window
= win
->id
;
2109 ce
.border_width
= BORDER(win
);
2110 ce
.above_sibling
= XCB_WINDOW_NONE
;
2113 ce
.event
= ev
->window
;
2114 ce
.window
= ev
->window
;
2116 /* make response appear more WM_SIZE_HINTS-compliant */
2118 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%x,"
2119 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
2120 "%d x %d\n", win
->id
, win
->sh_mask
, SH_MIN_W(win
),
2121 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2122 SH_INC_W(win
), SH_INC_H(win
));
2126 /* the hint may be set... to 0! */
2127 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2128 ce
.width
= SH_MIN_W(win
);
2129 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2130 ce
.height
= SH_MIN_H(win
);
2135 /* may also be advertized as 0 */
2136 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2137 ce
.width
= SH_MAX_W(win
);
2138 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2139 ce
.height
= SH_MAX_H(win
);
2142 /* resize increment. */
2144 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2145 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2147 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2148 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2152 /* adjust x and y for requested border_width. */
2153 ce
.x
+= BORDER(win
) - ev
->border_width
;
2154 ce
.y
+= BORDER(win
) - ev
->border_width
;
2155 ce
.border_width
= ev
->border_width
;
2156 ce
.above_sibling
= ev
->above
;
2159 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2160 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2161 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2163 xcb_send_event(conn
, False
, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2169 count_win(struct workspace
*ws
, int count_transient
)
2174 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2175 if (count_transient
== 0 && win
->floating
)
2177 if (count_transient
== 0 && win
->transient
)
2183 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2189 quit(struct swm_region
*r
, union arg
*args
)
2191 DNPRINTF(SWM_D_MISC
, "quit\n");
2196 unmap_window(struct ws_win
*win
)
2201 /* don't unmap again */
2202 if (getstate(win
->id
) == XCB_ICCCM_WM_STATE_ICONIC
)
2205 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
2207 xcb_unmap_window(conn
, win
->id
);
2208 xcb_change_window_attributes(conn
, win
->id
,
2209 XCB_CW_BORDER_PIXEL
, &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2216 int i
, j
, num_screens
;
2218 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2219 for (i
= 0; i
< num_screens
; i
++)
2220 for (j
= 0; j
< workspace_limit
; j
++)
2221 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2226 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
2228 xcb_key_press_event_t event
;
2229 xcb_key_symbols_t
*syms
;
2230 xcb_keycode_t
*keycode
;
2235 syms
= xcb_key_symbols_alloc(conn
);
2236 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
2238 event
.event
= win
->id
;
2239 event
.root
= win
->s
->root
;
2240 event
.child
= XCB_WINDOW_NONE
;
2241 event
.time
= XCB_CURRENT_TIME
;
2242 event
.event_x
= X(win
);
2243 event
.event_y
= Y(win
);
2246 event
.same_screen
= True
;
2247 event
.detail
= *keycode
;
2248 event
.state
= modifiers
;
2250 event
.response_type
= XCB_KEY_PRESS
;
2251 xcb_send_event(conn
, True
, win
->id
,
2252 XCB_EVENT_MASK_KEY_PRESS
, (char *)&event
);
2254 event
.response_type
= XCB_KEY_RELEASE
;
2255 xcb_send_event(conn
, True
, win
->id
,
2256 XCB_EVENT_MASK_KEY_RELEASE
, (char *)&event
);
2259 xcb_key_symbols_free(syms
);
2263 restart(struct swm_region
*r
, union arg
*args
)
2265 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
2267 /* disable alarm because the following code may not be interrupted */
2269 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
2270 err(1, "can't disable alarm");
2275 XCloseDisplay(display
);
2276 execvp(start_argv
[0], start_argv
);
2277 warn("execvp failed");
2282 root_to_region(xcb_window_t root
)
2284 struct swm_region
*r
= NULL
;
2286 xcb_query_pointer_reply_t
*qpr
;
2288 DNPRINTF(SWM_D_MISC
, "root_to_region: window: 0x%x\n", root
);
2290 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2291 for (i
= 0; i
< num_screens
; i
++)
2292 if (screens
[i
].root
== root
)
2295 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
2296 screens
[i
].root
), NULL
);
2299 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: (%d,%d)\n",
2300 qpr
->root_x
, qpr
->root_y
);
2301 /* choose a region based on pointer location */
2302 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2303 if (X(r
) <= qpr
->root_x
&& qpr
->root_x
< MAX_X(r
) &&
2304 Y(r
) <= qpr
->root_y
&& qpr
->root_y
< MAX_Y(r
))
2310 r
= TAILQ_FIRST(&screens
[i
].rl
);
2316 find_unmanaged_window(xcb_window_t id
)
2319 int i
, j
, num_screens
;
2321 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2322 for (i
= 0; i
< num_screens
; i
++)
2323 for (j
= 0; j
< workspace_limit
; j
++)
2324 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
2332 find_window(xcb_window_t id
)
2335 int i
, j
, num_screens
;
2336 xcb_query_tree_cookie_t c
;
2337 xcb_query_tree_reply_t
*r
;
2339 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2340 for (i
= 0; i
< num_screens
; i
++)
2341 for (j
= 0; j
< workspace_limit
; j
++)
2342 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2346 c
= xcb_query_tree(conn
, id
);
2347 r
= xcb_query_tree_reply(conn
, c
, NULL
);
2351 /* if we were looking for the parent return that window instead */
2352 if (r
->parent
== 0 || r
->root
== r
->parent
)
2355 /* look for parent */
2356 for (i
= 0; i
< num_screens
; i
++)
2357 for (j
= 0; j
< workspace_limit
; j
++)
2358 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2359 if (r
->parent
== win
->id
) {
2369 spawn(int ws_idx
, union arg
*args
, int close_fd
)
2374 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
2377 close(xcb_get_file_descriptor(conn
));
2379 setenv("LD_PRELOAD", SWM_LIB
, 1);
2381 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
2382 warn("spawn: asprintf SWM_WS");
2385 setenv("_SWM_WS", ret
, 1);
2389 if (asprintf(&ret
, "%d", getpid()) == -1) {
2390 warn("spawn: asprintf _SWM_PID");
2393 setenv("_SWM_PID", ret
, 1);
2397 if (setsid() == -1) {
2398 warn("spawn: setsid");
2404 * close stdin and stdout to prevent interaction between apps
2405 * and the baraction script
2406 * leave stderr open to record errors
2408 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2409 warn("spawn: open");
2412 dup2(fd
, STDIN_FILENO
);
2413 dup2(fd
, STDOUT_FILENO
);
2418 execvp(args
->argv
[0], args
->argv
);
2420 warn("spawn: execvp");
2425 kill_refs(struct ws_win
*win
)
2427 int i
, x
, num_screens
;
2428 struct swm_region
*r
;
2429 struct workspace
*ws
;
2434 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2435 for (i
= 0; i
< num_screens
; i
++)
2436 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2437 for (x
= 0; x
< workspace_limit
; x
++) {
2439 if (win
== ws
->focus
)
2441 if (win
== ws
->focus_prev
)
2442 ws
->focus_prev
= NULL
;
2447 validate_win(struct ws_win
*testwin
)
2450 struct workspace
*ws
;
2451 struct swm_region
*r
;
2452 int i
, x
, num_screens
;
2454 if (testwin
== NULL
)
2457 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2458 for (i
= 0; i
< num_screens
; i
++)
2459 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2460 for (x
= 0; x
< workspace_limit
; x
++) {
2462 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2470 validate_ws(struct workspace
*testws
)
2472 struct swm_region
*r
;
2473 struct workspace
*ws
;
2474 int i
, x
, num_screens
;
2476 /* validate all ws */
2477 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2478 for (i
= 0; i
< num_screens
; i
++)
2479 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2480 for (x
= 0; x
< workspace_limit
; x
++) {
2489 unfocus_win(struct ws_win
*win
)
2492 xcb_window_t none
= XCB_WINDOW_NONE
;
2494 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%x\n", WINID(win
));
2498 if (win
->ws
== NULL
)
2501 if (validate_ws(win
->ws
))
2502 return; /* XXX this gets hit with thunderbird, needs fixing */
2504 if (win
->ws
->r
== NULL
)
2507 if (validate_win(win
)) {
2512 if (win
->ws
->focus
== win
) {
2513 win
->ws
->focus
= NULL
;
2514 win
->ws
->focus_prev
= win
;
2517 if (validate_win(win
->ws
->focus
)) {
2518 kill_refs(win
->ws
->focus
);
2519 win
->ws
->focus
= NULL
;
2521 if (validate_win(win
->ws
->focus_prev
)) {
2522 kill_refs(win
->ws
->focus_prev
);
2523 win
->ws
->focus_prev
= NULL
;
2526 /* drain all previous unfocus events */
2527 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
2530 grabbuttons(win
, 0);
2531 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
2532 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2533 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2534 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2542 int i
, j
, num_screens
;
2544 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2546 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2547 for (i
= 0; i
< num_screens
; i
++)
2548 for (j
= 0; j
< workspace_limit
; j
++)
2549 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2554 focus_win(struct ws_win
*win
)
2557 struct ws_win
*cfw
= NULL
;
2558 xcb_get_input_focus_cookie_t c
;
2559 xcb_get_input_focus_reply_t
*r
;
2560 xcb_window_t cur_focus
= XCB_WINDOW_NONE
;
2562 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%x\n", WINID(win
));
2566 if (win
->ws
== NULL
)
2569 if (validate_ws(win
->ws
))
2570 return; /* XXX this gets hit with thunderbird, needs fixing */
2572 if (validate_win(win
)) {
2577 if (validate_win(win
)) {
2582 c
= xcb_get_input_focus(conn
);
2583 r
= xcb_get_input_focus_reply(conn
, c
, NULL
);
2585 cur_focus
= r
->focus
;
2588 if ((cfw
= find_window(cur_focus
)) != NULL
)
2591 /* use larger hammer since the window was killed somehow */
2592 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2593 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2594 xcb_change_window_attributes(conn
, cfw
->id
,
2595 XCB_CW_BORDER_PIXEL
,
2596 &cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2599 win
->ws
->focus
= win
;
2601 if (win
->ws
->r
!= NULL
) {
2602 /* drain all previous focus events */
2603 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
2607 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
2608 win
->id
, XCB_CURRENT_TIME
);
2609 grabbuttons(win
, 1);
2610 xcb_change_window_attributes(conn
, win
->id
,
2611 XCB_CW_BORDER_PIXEL
,
2612 &win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2613 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2614 win
->ws
->always_raise
)
2615 map_window_raised(win
->id
);
2617 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
2618 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
2626 switchws(struct swm_region
*r
, union arg
*args
)
2628 int wsid
= args
->id
, unmap_old
= 0;
2629 struct swm_region
*this_r
, *other_r
;
2631 struct workspace
*new_ws
, *old_ws
;
2637 if (wsid
>= workspace_limit
)
2641 old_ws
= this_r
->ws
;
2642 new_ws
= &this_r
->s
->ws
[wsid
];
2644 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2645 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2647 if (new_ws
== NULL
|| old_ws
== NULL
)
2649 if (new_ws
== old_ws
)
2652 other_r
= new_ws
->r
;
2653 if (other_r
== NULL
) {
2654 /* the other workspace is hidden, hide this one */
2658 /* the other ws is visible in another region, exchange them */
2659 other_r
->ws_prior
= new_ws
;
2660 other_r
->ws
= old_ws
;
2661 old_ws
->r
= other_r
;
2663 this_r
->ws_prior
= old_ws
;
2664 this_r
->ws
= new_ws
;
2667 /* this is needed so that we can click on a window after a restart */
2671 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2672 focus(new_ws
->r
, &a
);
2674 /* unmap old windows */
2676 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2679 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2680 drain_enter_notify();
2684 cyclews(struct swm_region
*r
, union arg
*args
)
2687 struct swm_screen
*s
= r
->s
;
2690 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2691 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2696 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2699 case SWM_ARG_ID_CYCLEWS_UP
:
2700 if (a
.id
< workspace_limit
- 1)
2705 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2708 case SWM_ARG_ID_CYCLEWS_DOWN
:
2712 a
.id
= workspace_limit
- 1;
2719 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2721 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2725 } while (a
.id
!= r
->ws
->idx
);
2729 priorws(struct swm_region
*r
, union arg
*args
)
2733 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2734 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2736 if (r
->ws_prior
== NULL
)
2739 a
.id
= r
->ws_prior
->idx
;
2744 cyclescr(struct swm_region
*r
, union arg
*args
)
2746 struct swm_region
*rr
= NULL
;
2748 int i
, x
, y
, num_screens
;
2750 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
2751 /* do nothing if we don't have more than one screen */
2752 if (!(num_screens
> 1 || outputs
> 1))
2757 case SWM_ARG_ID_CYCLESC_UP
:
2758 rr
= TAILQ_NEXT(r
, entry
);
2760 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2762 case SWM_ARG_ID_CYCLESC_DOWN
:
2763 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2765 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2773 /* move mouse to region */
2775 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2776 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0, 0,
2779 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2782 if (rr
->ws
->focus
) {
2783 /* move to focus window */
2784 x
= X(rr
->ws
->focus
) + 1;
2785 y
= Y(rr
->ws
->focus
) + 1;
2786 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
, rr
->s
[i
].root
, 0, 0, 0,
2792 sort_windows(struct ws_win_list
*wl
)
2794 struct ws_win
*win
, *parent
, *nxt
;
2799 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2800 nxt
= TAILQ_NEXT(win
, entry
);
2801 if (win
->transient
) {
2802 parent
= find_window(win
->transient
);
2803 if (parent
== NULL
) {
2804 warnx("not possible bug");
2807 TAILQ_REMOVE(wl
, win
, entry
);
2808 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2815 swapwin(struct swm_region
*r
, union arg
*args
)
2817 struct ws_win
*target
, *source
;
2818 struct ws_win
*cur_focus
;
2819 struct ws_win_list
*wl
;
2822 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2823 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2825 cur_focus
= r
->ws
->focus
;
2826 if (cur_focus
== NULL
)
2830 wl
= &source
->ws
->winlist
;
2833 case SWM_ARG_ID_SWAPPREV
:
2834 if (source
->transient
)
2835 source
= find_window(source
->transient
);
2836 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2837 if (target
&& target
->transient
)
2838 target
= find_window(target
->transient
);
2839 TAILQ_REMOVE(wl
, source
, entry
);
2841 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2843 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2845 case SWM_ARG_ID_SWAPNEXT
:
2846 target
= TAILQ_NEXT(source
, entry
);
2847 /* move the parent and let the sort handle the move */
2848 if (source
->transient
)
2849 source
= find_window(source
->transient
);
2850 TAILQ_REMOVE(wl
, source
, entry
);
2852 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2854 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2856 case SWM_ARG_ID_SWAPMAIN
:
2857 target
= TAILQ_FIRST(wl
);
2858 if (target
== source
) {
2859 if (source
->ws
->focus_prev
!= NULL
&&
2860 source
->ws
->focus_prev
!= target
)
2861 source
= source
->ws
->focus_prev
;
2865 if (target
== NULL
|| source
== NULL
)
2867 source
->ws
->focus_prev
= target
;
2868 TAILQ_REMOVE(wl
, target
, entry
);
2869 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2870 TAILQ_REMOVE(wl
, source
, entry
);
2871 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2873 case SWM_ARG_ID_MOVELAST
:
2874 TAILQ_REMOVE(wl
, source
, entry
);
2875 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2878 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2888 focus_prev(struct ws_win
*win
)
2890 struct ws_win
*winfocus
= NULL
;
2891 struct ws_win
*cur_focus
= NULL
;
2892 struct ws_win_list
*wl
= NULL
;
2893 struct workspace
*ws
= NULL
;
2895 if (!(win
&& win
->ws
))
2900 cur_focus
= ws
->focus
;
2902 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%x, cur_focus: 0x%x\n",
2903 WINID(win
), WINID(cur_focus
));
2905 /* pickle, just focus on whatever */
2906 if (cur_focus
== NULL
) {
2907 /* use prev_focus if valid */
2908 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2909 find_window(WINID(ws
->focus_prev
)))
2910 winfocus
= ws
->focus_prev
;
2914 /* if transient focus on parent */
2915 if (cur_focus
->transient
) {
2916 winfocus
= find_window(cur_focus
->transient
);
2920 /* if in max_stack try harder */
2921 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2922 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2923 if (cur_focus
!= ws
->focus_prev
)
2924 winfocus
= ws
->focus_prev
;
2926 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2931 DNPRINTF(SWM_D_FOCUS
, "focus_prev: focus_close: %d\n", focus_close
);
2933 if (winfocus
== NULL
|| winfocus
== win
) {
2934 switch (focus_close
) {
2935 case SWM_STACK_BOTTOM
:
2936 winfocus
= TAILQ_FIRST(wl
);
2939 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2941 case SWM_STACK_ABOVE
:
2942 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
2943 if (focus_close_wrap
)
2944 winfocus
= TAILQ_FIRST(wl
);
2946 winfocus
= TAILQ_PREV(cur_focus
,
2947 ws_win_list
, entry
);
2950 case SWM_STACK_BELOW
:
2951 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
2953 if (focus_close_wrap
)
2954 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2956 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2962 if (winfocus
== NULL
) {
2963 if (focus_default
== SWM_STACK_TOP
)
2964 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2966 winfocus
= TAILQ_FIRST(wl
);
2970 focus_magic(winfocus
);
2974 focus(struct swm_region
*r
, union arg
*args
)
2976 struct ws_win
*winfocus
= NULL
, *head
;
2977 struct ws_win
*cur_focus
= NULL
;
2978 struct ws_win_list
*wl
= NULL
;
2979 struct workspace
*ws
= NULL
;
2985 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2987 /* treat FOCUS_CUR special */
2988 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2989 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2990 winfocus
= r
->ws
->focus
;
2991 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2992 winfocus
= r
->ws
->focus_prev
;
2994 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2995 if (winfocus
->iconic
== 0)
2998 focus_magic(winfocus
);
3002 if ((cur_focus
= r
->ws
->focus
) == NULL
)
3006 if (TAILQ_EMPTY(wl
))
3008 /* make sure there is at least one uniconified window */
3010 TAILQ_FOREACH(winfocus
, wl
, entry
)
3011 if (winfocus
->iconic
== 0) {
3019 case SWM_ARG_ID_FOCUSPREV
:
3020 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
3022 head
= TAILQ_LAST(wl
, ws_win_list
);
3024 if (WINID(winfocus
) == cur_focus
->transient
) {
3025 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
3027 head
= TAILQ_LAST(wl
, ws_win_list
);
3032 if (winfocus
&& winfocus
->iconic
) {
3033 while (winfocus
!= cur_focus
) {
3034 if (winfocus
== NULL
)
3035 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
3036 if (winfocus
->iconic
== 0)
3038 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
3044 case SWM_ARG_ID_FOCUSNEXT
:
3045 head
= TAILQ_NEXT(cur_focus
, entry
);
3047 head
= TAILQ_FIRST(wl
);
3051 if (winfocus
&& winfocus
->iconic
) {
3052 while (winfocus
!= cur_focus
) {
3053 if (winfocus
== NULL
)
3054 winfocus
= TAILQ_FIRST(wl
);
3055 if (winfocus
->iconic
== 0)
3057 winfocus
= TAILQ_NEXT(winfocus
, entry
);
3062 case SWM_ARG_ID_FOCUSMAIN
:
3063 winfocus
= TAILQ_FIRST(wl
);
3064 if (winfocus
== cur_focus
)
3065 winfocus
= cur_focus
->ws
->focus_prev
;
3072 focus_magic(winfocus
);
3076 cycle_layout(struct swm_region
*r
, union arg
*args
)
3078 struct workspace
*ws
= r
->ws
;
3081 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
3084 if (ws
->cur_layout
->l_stack
== NULL
)
3085 ws
->cur_layout
= &layouts
[0];
3088 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3089 drain_enter_notify();
3090 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3095 stack_config(struct swm_region
*r
, union arg
*args
)
3097 struct workspace
*ws
= r
->ws
;
3099 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
3102 if (ws
->cur_layout
->l_config
!= NULL
)
3103 ws
->cur_layout
->l_config(ws
, args
->id
);
3105 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
3112 struct swm_geometry g
;
3113 struct swm_region
*r
;
3119 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
3121 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3122 for (i
= 0; i
< num_screens
; i
++) {
3126 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
3127 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
3128 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
3130 /* start with screen geometry, adjust for bar */
3132 g
.w
-= 2 * border_width
;
3133 g
.h
-= 2 * border_width
;
3139 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
3140 r
->ws
->cur_layout
->l_string(r
->ws
);
3141 /* save r so we can track region changes */
3148 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3149 drain_enter_notify();
3151 DNPRINTF(SWM_D_STACK
, "stack: end\n");
3155 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
3157 /* retain window geom and region geom */
3158 win
->g_float
= win
->g
;
3159 win
->g_float
.x
-= X(r
);
3160 win
->g_float
.y
-= Y(r
);
3161 win
->g_floatvalid
= 1;
3162 DNPRINTF(SWM_D_MISC
, "store_float_geom: window: 0x%x, g: (%d,%d)"
3163 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
3164 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
3165 win
->g_float
.w
, win
->g_float
.h
);
3169 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
3174 DNPRINTF(SWM_D_MISC
, "stack_floater: window: 0x%x\n", win
->id
);
3177 * to allow windows to change their size (e.g. mplayer fs) only retrieve
3178 * geom on ws switches or return from max mode
3180 if (win
->g_floatvalid
&& (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&&
3181 !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
)))) {
3182 /* refloat at last floating relative position */
3183 win
->g
= win
->g_float
;
3188 win
->floatmaxed
= 0;
3191 * if set to fullscreen mode, configure window to maximum size.
3193 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3194 if (!win
->g_floatvalid
)
3195 store_float_geom(win
, win
->ws
->r
);
3197 win
->g
= win
->ws
->r
->g
;
3201 * remove border on fullscreen floater when in fullscreen mode or when
3202 * the quirk is present.
3204 if ((win
->ewmh_flags
& EWMH_F_FULLSCREEN
) ||
3205 ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
3206 (WIDTH(win
) >= WIDTH(r
)) && (HEIGHT(win
) >= HEIGHT(r
)))) {
3207 if (win
->bordered
) {
3209 X(win
) += border_width
;
3210 Y(win
) += border_width
;
3212 } else if (!win
->bordered
) {
3214 X(win
) -= border_width
;
3215 Y(win
) -= border_width
;
3218 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
3219 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
3220 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
3225 * floaters and transients are auto-centred unless moved
3228 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - BORDER(win
);
3229 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - BORDER(win
);
3232 /* keep window within region bounds */
3233 constrain_window(win
, r
, 0);
3239 * Send keystrokes to terminal to decrease/increase the font size as the
3240 * window size changes.
3243 adjust_font(struct ws_win
*win
)
3245 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
3246 win
->floating
|| win
->transient
)
3249 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
3250 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
3251 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
3252 win
->font_size_boundary
[win
->font_steps
] =
3253 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
3256 win
->last_inc
= win
->sh
.width_inc
;
3257 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
3258 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
3259 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
3262 win
->last_inc
= win
->sh
.width_inc
;
3263 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
3267 #define SWAPXY(g) do { \
3269 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3270 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3273 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
3275 XWindowAttributes wa
;
3276 struct swm_geometry win_g
, r_g
= *g
;
3277 struct ws_win
*win
, *fs_win
= NULL
;
3278 int i
, j
, s
, stacks
;
3279 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
3280 int hrh
, extra
= 0, h_slice
, last_h
= 0;
3281 int split
, colno
, winno
, mwin
, msize
, mscale
;
3282 int remain
, missing
, v_slice
, reconfigure
;
3285 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
3286 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
3288 winno
= count_win(ws
, 0);
3289 if (winno
== 0 && count_win(ws
, 1) == 0)
3292 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3293 if (win
->transient
== 0 && win
->floating
== 0
3294 && win
->iconic
== 0)
3301 w_inc
= win
->sh
.width_inc
;
3302 w_base
= win
->sh
.base_width
;
3303 mwin
= ws
->l_state
.horizontal_mwin
;
3304 mscale
= ws
->l_state
.horizontal_msize
;
3305 stacks
= ws
->l_state
.horizontal_stacks
;
3308 w_inc
= win
->sh
.height_inc
;
3309 w_base
= win
->sh
.base_height
;
3310 mwin
= ws
->l_state
.vertical_mwin
;
3311 mscale
= ws
->l_state
.vertical_msize
;
3312 stacks
= ws
->l_state
.vertical_stacks
;
3316 if (stacks
> winno
- mwin
)
3317 stacks
= winno
- mwin
;
3321 h_slice
= r_g
.h
/ SWM_H_SLICE
;
3322 if (mwin
&& winno
> mwin
) {
3323 v_slice
= r_g
.w
/ SWM_V_SLICE
;
3327 win_g
.w
= v_slice
* mscale
;
3329 if (w_inc
> 1 && w_inc
< v_slice
) {
3330 /* adjust for window's requested size increment */
3331 remain
= (win_g
.w
- w_base
) % w_inc
;
3337 win_g
.x
+= r_g
.w
- msize
;
3340 colno
= split
= winno
/ stacks
;
3341 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
3342 2 * border_width
) / stacks
);
3344 hrh
= r_g
.h
/ colno
;
3345 extra
= r_g
.h
- (colno
* hrh
);
3346 win_g
.h
= hrh
- 2 * border_width
;
3348 /* stack all the tiled windows */
3349 i
= j
= 0, s
= stacks
;
3350 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3351 if (win
->transient
!= 0 || win
->floating
!= 0)
3353 if (win
->iconic
!= 0)
3356 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3361 if (split
&& i
== split
) {
3362 colno
= (winno
- mwin
) / stacks
;
3363 if (s
<= (winno
- mwin
) % stacks
)
3365 split
= split
+ colno
;
3366 hrh
= (r_g
.h
/ colno
);
3367 extra
= r_g
.h
- (colno
* hrh
);
3371 win_g
.x
+= win_g
.w
+ 2 * border_width
;
3372 win_g
.w
= (r_g
.w
- msize
-
3373 (stacks
* 2 * border_width
)) / stacks
;
3375 win_g
.w
+= (r_g
.w
- msize
-
3376 (stacks
* 2 * border_width
)) % stacks
;
3380 win_g
.h
= hrh
- 2 * border_width
;
3382 h_inc
= win
->sh
.width_inc
;
3383 h_base
= win
->sh
.base_width
;
3385 h_inc
= win
->sh
.height_inc
;
3386 h_base
= win
->sh
.base_height
;
3388 if (j
== colno
- 1) {
3389 win_g
.h
= hrh
+ extra
;
3390 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3391 /* adjust for window's requested size increment */
3392 remain
= (win_g
.h
- h_base
) % h_inc
;
3393 missing
= h_inc
- remain
;
3395 if (missing
<= extra
|| j
== 0) {
3407 win_g
.y
+= last_h
+ 2 * border_width
;
3409 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3411 win_g
.w
+= 2 * border_width
;
3412 win_g
.h
+= 2 * border_width
;
3417 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3418 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3422 WIDTH(win
) = win_g
.h
;
3423 HEIGHT(win
) = win_g
.w
;
3426 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3427 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3431 WIDTH(win
) = win_g
.w
;
3432 HEIGHT(win
) = win_g
.h
;
3436 if (bordered
!= win
->bordered
) {
3438 win
->bordered
= bordered
;
3446 if (XGetWindowAttributes(display
, win
->id
, &wa
))
3447 if (wa
.map_state
== IsUnmapped
)
3448 map_window_raised(win
->id
);
3456 /* now, stack all the floaters and transients */
3457 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3458 if (win
->transient
== 0 && win
->floating
== 0)
3460 if (win
->iconic
== 1)
3462 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3467 stack_floater(win
, ws
->r
);
3468 map_window_raised(win
->id
);
3472 stack_floater(fs_win
, ws
->r
);
3473 map_window_raised(fs_win
->id
);
3478 vertical_config(struct workspace
*ws
, int id
)
3480 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3484 case SWM_ARG_ID_STACKRESET
:
3485 case SWM_ARG_ID_STACKINIT
:
3486 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3487 ws
->l_state
.vertical_mwin
= 1;
3488 ws
->l_state
.vertical_stacks
= 1;
3490 case SWM_ARG_ID_MASTERSHRINK
:
3491 if (ws
->l_state
.vertical_msize
> 1)
3492 ws
->l_state
.vertical_msize
--;
3494 case SWM_ARG_ID_MASTERGROW
:
3495 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3496 ws
->l_state
.vertical_msize
++;
3498 case SWM_ARG_ID_MASTERADD
:
3499 ws
->l_state
.vertical_mwin
++;
3501 case SWM_ARG_ID_MASTERDEL
:
3502 if (ws
->l_state
.vertical_mwin
> 0)
3503 ws
->l_state
.vertical_mwin
--;
3505 case SWM_ARG_ID_STACKINC
:
3506 ws
->l_state
.vertical_stacks
++;
3508 case SWM_ARG_ID_STACKDEC
:
3509 if (ws
->l_state
.vertical_stacks
> 1)
3510 ws
->l_state
.vertical_stacks
--;
3512 case SWM_ARG_ID_FLIPLAYOUT
:
3513 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3521 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3523 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3525 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3529 horizontal_config(struct workspace
*ws
, int id
)
3531 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3534 case SWM_ARG_ID_STACKRESET
:
3535 case SWM_ARG_ID_STACKINIT
:
3536 ws
->l_state
.horizontal_mwin
= 1;
3537 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3538 ws
->l_state
.horizontal_stacks
= 1;
3540 case SWM_ARG_ID_MASTERSHRINK
:
3541 if (ws
->l_state
.horizontal_msize
> 1)
3542 ws
->l_state
.horizontal_msize
--;
3544 case SWM_ARG_ID_MASTERGROW
:
3545 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3546 ws
->l_state
.horizontal_msize
++;
3548 case SWM_ARG_ID_MASTERADD
:
3549 ws
->l_state
.horizontal_mwin
++;
3551 case SWM_ARG_ID_MASTERDEL
:
3552 if (ws
->l_state
.horizontal_mwin
> 0)
3553 ws
->l_state
.horizontal_mwin
--;
3555 case SWM_ARG_ID_STACKINC
:
3556 ws
->l_state
.horizontal_stacks
++;
3558 case SWM_ARG_ID_STACKDEC
:
3559 if (ws
->l_state
.horizontal_stacks
> 1)
3560 ws
->l_state
.horizontal_stacks
--;
3562 case SWM_ARG_ID_FLIPLAYOUT
:
3563 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3571 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3573 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3575 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3578 /* fullscreen view */
3580 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3582 struct swm_geometry gg
= *g
;
3583 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3584 int winno
, num_screens
;
3586 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3591 winno
= count_win(ws
, 0);
3592 if (winno
== 0 && count_win(ws
, 1) == 0)
3595 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
3596 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3597 if (win
->transient
) {
3599 parent
= find_window(win
->transient
);
3603 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3605 * retain geometry for retrieval on exit from
3608 store_float_geom(win
, ws
->r
);
3609 win
->floatmaxed
= 1;
3612 /* only reconfigure if necessary */
3613 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3614 HEIGHT(win
) != gg
.h
) {
3620 WIDTH(win
) += 2 * border_width
;
3621 HEIGHT(win
) += 2 * border_width
;
3626 /* unmap only if we don't have multi screen */
3627 if (win
!= ws
->focus
)
3628 if (!(num_screens
> 1 || outputs
> 1))
3632 /* put the last transient on top */
3635 map_window_raised(parent
->id
);
3636 stack_floater(wintrans
, ws
->r
);
3637 focus_magic(wintrans
);
3642 send_to_ws(struct swm_region
*r
, union arg
*args
)
3644 int wsid
= args
->id
;
3645 struct ws_win
*win
= NULL
, *parent
;
3646 struct workspace
*ws
, *nws
;
3647 xcb_atom_t ws_idx_atom
= XCB_ATOM_NONE
;
3648 char ws_idx_str
[SWM_PROPLEN
];
3651 if (wsid
>= workspace_limit
)
3654 if (r
&& r
->ws
&& r
->ws
->focus
)
3660 if (win
->ws
->idx
== wsid
)
3663 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%x\n", win
->id
);
3666 nws
= &win
->s
->ws
[wsid
];
3668 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3670 if (win
->transient
) {
3671 parent
= find_window(win
->transient
);
3673 unmap_window(parent
);
3674 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3675 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3680 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3681 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3682 if (TAILQ_EMPTY(&ws
->winlist
))
3683 r
->ws
->focus
= NULL
;
3686 /* Try to update the window's workspace property */
3687 ws_idx_atom
= get_atom_from_string("_SWM_WS");
3689 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3691 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3693 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
3694 ws_idx_atom
, XCB_ATOM_STRING
, 8, strlen(ws_idx_str
),
3703 pressbutton(struct swm_region
*r
, union arg
*args
)
3705 XTestFakeButtonEvent(display
, args
->id
, True
, CurrentTime
);
3706 XTestFakeButtonEvent(display
, args
->id
, False
, CurrentTime
);
3710 raise_toggle(struct swm_region
*r
, union arg
*args
)
3712 if (r
== NULL
|| r
->ws
== NULL
)
3715 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3717 /* bring floaters back to top */
3718 if (r
->ws
->always_raise
== 0)
3723 iconify(struct swm_region
*r
, union arg
*args
)
3727 if (r
->ws
->focus
== NULL
)
3729 unmap_window(r
->ws
->focus
);
3730 update_iconic(r
->ws
->focus
, 1);
3732 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3733 drain_enter_notify();
3734 r
->ws
->focus
= NULL
;
3735 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3740 get_win_name(xcb_window_t win
)
3743 xcb_get_property_cookie_t c
;
3744 xcb_get_text_property_reply_t r
;
3746 c
= xcb_icccm_get_wm_name(conn
, win
);
3747 if (xcb_icccm_get_wm_name_reply(conn
, c
, &r
, NULL
)) {
3748 name
= malloc(r
.name_len
+ 1);
3750 xcb_get_text_property_reply_wipe(&r
);
3753 memcpy(name
, r
.name
, r
.name_len
);
3754 name
[r
.name_len
] = '\0';
3757 xcb_get_text_property_reply_wipe(&r
);
3763 uniconify(struct swm_region
*r
, union arg
*args
)
3770 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3772 if (r
== NULL
|| r
->ws
== NULL
)
3775 /* make sure we have anything to uniconify */
3776 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3777 if (win
->ws
== NULL
)
3778 continue; /* should never happen */
3779 if (win
->iconic
== 0)
3787 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3789 spawn_select(r
, args
, "search", &searchpid
);
3791 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3794 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3795 if (win
->ws
== NULL
)
3796 continue; /* should never happen */
3797 if (win
->iconic
== 0)
3800 name
= get_win_name(win
->id
);
3803 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
3811 name_workspace(struct swm_region
*r
, union arg
*args
)
3815 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3821 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3823 spawn_select(r
, args
, "name_workspace", &searchpid
);
3825 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3828 fprintf(lfile
, "%s", "");
3833 search_workspace(struct swm_region
*r
, union arg
*args
)
3836 struct workspace
*ws
;
3839 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3845 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3847 spawn_select(r
, args
, "search", &searchpid
);
3849 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3852 for (i
= 0; i
< workspace_limit
; i
++) {
3856 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3857 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3864 search_win_cleanup(void)
3866 struct search_window
*sw
= NULL
;
3868 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3869 xcb_destroy_window(conn
, sw
->indicator
);
3870 xcb_free_gc(conn
, sw
->gc
);
3871 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3877 search_win(struct swm_region
*r
, union arg
*args
)
3879 struct ws_win
*win
= NULL
;
3880 struct search_window
*sw
= NULL
;
3882 uint32_t gcv
[1], wa
[2];
3887 XRectangle ibox
, lbox
;
3888 xcb_screen_t
*screen
;
3889 DNPRINTF(SWM_D_MISC
, "search_win\n");
3892 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3894 spawn_select(r
, args
, "search", &searchpid
);
3896 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3899 TAILQ_INIT(&search_wl
);
3902 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3903 if (win
->iconic
== 1)
3906 sw
= calloc(1, sizeof(struct search_window
));
3908 warn("search_win: calloc");
3910 search_win_cleanup();
3915 screen
= get_screen(sw
->idx
);
3917 snprintf(s
, sizeof s
, "%d", i
);
3920 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
3922 w
= xcb_generate_id(conn
);
3923 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].color
;
3924 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
;
3925 xcb_create_window(conn
, screen
->root_depth
, w
, win
->id
,
3926 0, 0, lbox
.width
+ 4,
3927 bar_fs_extents
->max_logical_extent
.height
, 1,
3928 XCB_WINDOW_CLASS_INPUT_OUTPUT
, screen
->root_visual
,
3929 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
3932 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3934 sw
->gc
= xcb_generate_id(conn
);
3935 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].color
;
3936 xcb_create_gc(conn
, sw
->gc
, w
, XCB_GC_FOREGROUND
, gcv
);
3937 map_window_raised(w
);
3939 xcb_image_text_8(conn
, len
, w
, sw
->gc
, 2,
3940 (bar_fs_extents
->max_logical_extent
.height
-
3941 lbox
.height
) / 2 - lbox
.y
, s
);
3943 fprintf(lfile
, "%d\n", i
);
3951 search_resp_uniconify(char *resp
, unsigned long len
)
3957 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3959 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3960 if (win
->iconic
== 0)
3962 name
= get_win_name(win
->id
);
3965 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
3970 if (strncmp(s
, resp
, len
) == 0) {
3971 /* XXX this should be a callback to generalize */
3972 update_iconic(win
, 0);
3981 search_resp_name_workspace(char *resp
, unsigned long len
)
3983 struct workspace
*ws
;
3985 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3987 if (search_r
->ws
== NULL
)
3992 free(search_r
->ws
->name
);
3993 search_r
->ws
->name
= NULL
;
3997 ws
->name
= strdup(resp
);
3998 if (ws
->name
== NULL
) {
3999 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
4000 "strdup: %s", strerror(errno
));
4007 search_resp_search_workspace(char *resp
, unsigned long len
)
4014 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
4018 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
4025 ws_idx
= strtonum(q
, 1, workspace_limit
, &errstr
);
4027 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
4034 switchws(search_r
, &a
);
4038 search_resp_search_window(char *resp
, unsigned long len
)
4043 struct search_window
*sw
;
4045 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
4049 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
4054 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
4056 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
4063 TAILQ_FOREACH(sw
, &search_wl
, entry
)
4064 if (idx
== sw
->idx
) {
4070 #define MAX_RESP_LEN 1024
4073 search_do_resp(void)
4079 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
4084 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
4085 warn("search: calloc");
4089 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
4091 warn("search: read error");
4094 resp
[rbytes
] = '\0';
4097 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
4098 * newline, so work around that by sanitizing the resp now.
4100 resp
[strcspn(resp
, "\n")] = '\0';
4103 switch (search_resp_action
) {
4104 case SWM_SEARCH_UNICONIFY
:
4105 search_resp_uniconify(resp
, len
);
4107 case SWM_SEARCH_NAME_WORKSPACE
:
4108 search_resp_name_workspace(resp
, len
);
4110 case SWM_SEARCH_SEARCH_WORKSPACE
:
4111 search_resp_search_workspace(resp
, len
);
4113 case SWM_SEARCH_SEARCH_WINDOW
:
4114 search_resp_search_window(resp
, len
);
4119 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
4120 search_win_cleanup();
4122 search_resp_action
= SWM_SEARCH_NONE
;
4123 close(select_resp_pipe
[0]);
4128 wkill(struct swm_region
*r
, union arg
*args
)
4130 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
4132 if (r
->ws
->focus
== NULL
)
4135 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
4136 xcb_kill_client(conn
, r
->ws
->focus
->id
);
4138 if (r
->ws
->focus
->can_delete
)
4139 client_msg(r
->ws
->focus
, adelete
);
4144 floating_toggle_win(struct ws_win
*win
)
4146 struct swm_region
*r
;
4156 /* reject floating toggles in max stack mode */
4157 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4160 if (win
->floating
) {
4161 if (!win
->floatmaxed
) {
4162 /* retain position for refloat */
4163 store_float_geom(win
, r
);
4167 if (win
->g_floatvalid
) {
4168 /* refloat at last floating relative position */
4169 X(win
) = win
->g_float
.x
+ X(r
);
4170 Y(win
) = win
->g_float
.y
+ Y(r
);
4171 WIDTH(win
) = win
->g_float
.w
;
4172 HEIGHT(win
) = win
->g_float
.h
;
4177 ewmh_update_actions(win
);
4183 floating_toggle(struct swm_region
*r
, union arg
*args
)
4185 struct ws_win
*win
= r
->ws
->focus
;
4191 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4192 _NET_WM_STATE_TOGGLE
);
4195 if (focus_mode
== SWM_FOCUS_DEFAULT
)
4196 drain_enter_notify();
4198 if (win
== win
->ws
->focus
) {
4199 a
.id
= SWM_ARG_ID_FOCUSCUR
;
4200 focus(win
->ws
->r
, &a
);
4205 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
4207 if (MAX_X(win
) + BORDER(win
) > MAX_X(r
)) {
4209 WIDTH(win
) = MAX_X(r
) - X(win
) - BORDER(win
);
4211 X(win
) = MAX_X(r
)- WIDTH(win
) - BORDER(win
);
4214 if (X(win
) + BORDER(win
) < X(r
)) {
4216 WIDTH(win
) -= X(r
) - X(win
) - BORDER(win
);
4218 X(win
) = X(r
) - BORDER(win
);
4221 if (MAX_Y(win
) + BORDER(win
) > MAX_Y(r
)) {
4223 HEIGHT(win
) = MAX_Y(r
) - Y(win
) - BORDER(win
);
4225 Y(win
) = MAX_Y(r
) - HEIGHT(win
) - BORDER(win
);
4228 if (Y(win
) + BORDER(win
) < Y(r
)) {
4230 HEIGHT(win
) -= Y(r
) - Y(win
) - BORDER(win
);
4232 Y(win
) = Y(r
) - BORDER(win
);
4238 if (HEIGHT(win
) < 1)
4244 update_window(struct ws_win
*win
)
4249 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
4250 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
4251 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
4255 wc
[3] = HEIGHT(win
);
4256 wc
[4] = BORDER(win
);
4258 DNPRINTF(SWM_D_EVENT
, "update_window: window: 0x%x, (x,y) w x h: "
4259 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
4260 wc
[3], YESNO(win
->bordered
));
4262 xcb_configure_window(conn
, win
->id
, mask
, wc
);
4265 #define SWM_RESIZE_STEPS (50)
4268 resize(struct ws_win
*win
, union arg
*args
)
4272 struct swm_region
*r
= NULL
;
4273 int resize_step
= 0;
4274 struct swm_geometry g
;
4275 int top
= 0, left
= 0;
4278 unsigned int shape
; /* cursor style */
4279 xcb_grab_pointer_reply_t
*gpr
;
4280 xcb_query_pointer_reply_t
*xpr
;
4286 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4289 DNPRINTF(SWM_D_MOUSE
, "resize: window: 0x%x, floating: %s, "
4290 "transient: 0x%x\n", win
->id
, YESNO(win
->floating
),
4293 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4296 /* reject resizes in max mode for floaters (transient ok) */
4297 if (win
->floatmaxed
)
4301 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4307 case SWM_ARG_ID_WIDTHSHRINK
:
4308 WIDTH(win
) -= SWM_RESIZE_STEPS
;
4311 case SWM_ARG_ID_WIDTHGROW
:
4312 WIDTH(win
) += SWM_RESIZE_STEPS
;
4315 case SWM_ARG_ID_HEIGHTSHRINK
:
4316 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
4319 case SWM_ARG_ID_HEIGHTGROW
:
4320 HEIGHT(win
) += SWM_RESIZE_STEPS
;
4327 constrain_window(win
, r
, 1);
4329 store_float_geom(win
,r
);
4333 if (focus_mode
== SWM_FOCUS_DEFAULT
)
4334 drain_enter_notify();
4336 /* get cursor offset from window root */
4337 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4344 if (xpr
->win_x
< WIDTH(win
) / 2)
4347 if (xpr
->win_y
< HEIGHT(win
) / 2)
4350 if (args
->id
== SWM_ARG_ID_CENTER
)
4353 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
4355 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
4357 cursor
= XCreateFontCursor(display
, shape
);
4359 gpr
= xcb_grab_pointer_reply(conn
,
4360 xcb_grab_pointer(conn
, False
, win
->id
, MOUSEMASK
,
4361 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
4362 cursor
, XCB_CURRENT_TIME
),
4365 XFreeCursor(display
, cursor
);
4371 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4372 SubstructureRedirectMask
, &ev
);
4374 case ConfigureRequest
:
4377 handler
[ev
.type
](&ev
);
4380 /* cursor offset/delta from start of the operation */
4381 dx
= ev
.xmotion
.x_root
- xpr
->root_x
;
4382 dy
= ev
.xmotion
.y_root
- xpr
->root_y
;
4388 if (args
->id
== SWM_ARG_ID_CENTER
) {
4389 if (g
.h
/ 2 + dy
< 1)
4393 HEIGHT(win
) = g
.h
+ 2 * dy
;
4401 HEIGHT(win
) = g
.h
+ dy
;
4408 if (args
->id
== SWM_ARG_ID_CENTER
) {
4409 if (g
.w
/ 2 + dx
< 1)
4413 WIDTH(win
) = g
.w
+ 2 * dx
;
4421 WIDTH(win
) = g
.w
+ dx
;
4424 constrain_window(win
, r
, 1);
4426 /* not free, don't sync more than 120 times / second */
4427 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4428 time
= ev
.xmotion
.time
;
4434 } while (ev
.type
!= ButtonRelease
);
4439 store_float_geom(win
,r
);
4441 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4442 XFreeCursor(display
, cursor
);
4446 drain_enter_notify();
4450 resize_step(struct swm_region
*r
, union arg
*args
)
4452 struct ws_win
*win
= NULL
;
4454 if (r
&& r
->ws
&& r
->ws
->focus
)
4462 #define SWM_MOVE_STEPS (50)
4465 move(struct ws_win
*win
, union arg
*args
)
4470 struct swm_region
*r
= NULL
;
4471 xcb_grab_pointer_reply_t
*gpr
;
4472 xcb_query_pointer_reply_t
*qpr
;
4478 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
4481 DNPRINTF(SWM_D_MOUSE
, "move: window: 0x%x, floating: %s, transient: "
4482 "0x%x\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4484 /* in max_stack mode should only move transients */
4485 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4489 if (win
->floating
== 0 && !win
->transient
) {
4490 store_float_geom(win
, r
);
4491 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4494 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4501 case SWM_ARG_ID_MOVELEFT
:
4502 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4505 case SWM_ARG_ID_MOVERIGHT
:
4506 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4509 case SWM_ARG_ID_MOVEUP
:
4510 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4513 case SWM_ARG_ID_MOVEDOWN
:
4514 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4521 constrain_window(win
, r
, 0);
4523 store_float_geom(win
, r
);
4527 gpr
= xcb_grab_pointer_reply(conn
,
4528 xcb_grab_pointer(conn
, False
, win
->id
, MOUSEMASK
,
4529 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
4530 XCB_WINDOW_NONE
, XCreateFontCursor(display
, XC_fleur
),
4536 /* get cursor offset from window root */
4537 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
4542 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4543 SubstructureRedirectMask
, &ev
);
4545 case ConfigureRequest
:
4548 handler
[ev
.type
](&ev
);
4551 X(win
) = ev
.xmotion
.x_root
- qpr
->win_x
- border_width
;
4552 Y(win
) = ev
.xmotion
.y_root
- qpr
->win_y
- border_width
;
4554 constrain_window(win
, r
, 0);
4556 /* not free, don't sync more than 120 times / second */
4557 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4558 time
= ev
.xmotion
.time
;
4564 } while (ev
.type
!= ButtonRelease
);
4569 store_float_geom(win
, r
);
4570 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
4574 drain_enter_notify();
4578 move_step(struct swm_region
*r
, union arg
*args
)
4580 struct ws_win
*win
= NULL
;
4582 if (r
&& r
->ws
&& r
->ws
->focus
)
4587 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4594 /* user/key callable function IDs */
4683 kf_search_workspace
,
4685 kf_dumpwins
, /* MUST BE LAST */
4689 /* key definitions */
4691 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4696 char name
[SWM_FUNCNAME_LEN
];
4697 void (*func
)(struct swm_region
*r
, union arg
*);
4699 } keyfuncs
[kf_invalid
+ 1] = {
4700 /* name function argument */
4701 { "cycle_layout", cycle_layout
, {0} },
4702 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4703 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4704 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4705 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4706 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4707 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4708 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4709 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4710 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4711 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4712 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4713 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4714 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4715 { "quit", quit
, {0} },
4716 { "restart", restart
, {0} },
4717 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4718 { "ws_1", switchws
, {.id
= 0} },
4719 { "ws_2", switchws
, {.id
= 1} },
4720 { "ws_3", switchws
, {.id
= 2} },
4721 { "ws_4", switchws
, {.id
= 3} },
4722 { "ws_5", switchws
, {.id
= 4} },
4723 { "ws_6", switchws
, {.id
= 5} },
4724 { "ws_7", switchws
, {.id
= 6} },
4725 { "ws_8", switchws
, {.id
= 7} },
4726 { "ws_9", switchws
, {.id
= 8} },
4727 { "ws_10", switchws
, {.id
= 9} },
4728 { "ws_11", switchws
, {.id
= 10} },
4729 { "ws_12", switchws
, {.id
= 11} },
4730 { "ws_13", switchws
, {.id
= 12} },
4731 { "ws_14", switchws
, {.id
= 13} },
4732 { "ws_15", switchws
, {.id
= 14} },
4733 { "ws_16", switchws
, {.id
= 15} },
4734 { "ws_17", switchws
, {.id
= 16} },
4735 { "ws_18", switchws
, {.id
= 17} },
4736 { "ws_19", switchws
, {.id
= 18} },
4737 { "ws_20", switchws
, {.id
= 19} },
4738 { "ws_21", switchws
, {.id
= 20} },
4739 { "ws_22", switchws
, {.id
= 21} },
4740 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4741 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4742 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4743 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4744 { "ws_prior", priorws
, {0} },
4745 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4746 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4747 { "mvws_1", send_to_ws
, {.id
= 0} },
4748 { "mvws_2", send_to_ws
, {.id
= 1} },
4749 { "mvws_3", send_to_ws
, {.id
= 2} },
4750 { "mvws_4", send_to_ws
, {.id
= 3} },
4751 { "mvws_5", send_to_ws
, {.id
= 4} },
4752 { "mvws_6", send_to_ws
, {.id
= 5} },
4753 { "mvws_7", send_to_ws
, {.id
= 6} },
4754 { "mvws_8", send_to_ws
, {.id
= 7} },
4755 { "mvws_9", send_to_ws
, {.id
= 8} },
4756 { "mvws_10", send_to_ws
, {.id
= 9} },
4757 { "mvws_11", send_to_ws
, {.id
= 10} },
4758 { "mvws_12", send_to_ws
, {.id
= 11} },
4759 { "mvws_13", send_to_ws
, {.id
= 12} },
4760 { "mvws_14", send_to_ws
, {.id
= 13} },
4761 { "mvws_15", send_to_ws
, {.id
= 14} },
4762 { "mvws_16", send_to_ws
, {.id
= 15} },
4763 { "mvws_17", send_to_ws
, {.id
= 16} },
4764 { "mvws_18", send_to_ws
, {.id
= 17} },
4765 { "mvws_19", send_to_ws
, {.id
= 18} },
4766 { "mvws_20", send_to_ws
, {.id
= 19} },
4767 { "mvws_21", send_to_ws
, {.id
= 20} },
4768 { "mvws_22", send_to_ws
, {.id
= 21} },
4769 { "bar_toggle", bar_toggle
, {0} },
4770 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4771 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4772 { "float_toggle", floating_toggle
,{0} },
4773 { "version", version
, {0} },
4774 { "spawn_custom", dummykeyfunc
, {0} },
4775 { "iconify", iconify
, {0} },
4776 { "uniconify", uniconify
, {0} },
4777 { "raise_toggle", raise_toggle
, {0} },
4778 { "button2", pressbutton
, {2} },
4779 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4780 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4781 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4782 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4783 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4784 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4785 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4786 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4787 { "name_workspace", name_workspace
, {0} },
4788 { "search_workspace", search_workspace
, {0} },
4789 { "search_win", search_win
, {0} },
4790 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4791 { "invalid key func", NULL
, {0} },
4794 RB_ENTRY(key
) entry
;
4797 enum keyfuncid funcid
;
4800 RB_HEAD(key_tree
, key
);
4803 key_cmp(struct key
*kp1
, struct key
*kp2
)
4805 if (kp1
->keysym
< kp2
->keysym
)
4807 if (kp1
->keysym
> kp2
->keysym
)
4810 if (kp1
->mod
< kp2
->mod
)
4812 if (kp1
->mod
> kp2
->mod
)
4818 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
4819 struct key_tree keys
;
4822 enum { client_click
, root_click
};
4824 unsigned int action
;
4826 unsigned int button
;
4827 void (*func
)(struct ws_win
*, union arg
*);
4830 /* action key mouse button func args */
4831 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4832 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4833 { client_click
, MODKEY
, Button1
, move
, {0} },
4837 update_modkey(unsigned int mod
)
4843 RB_FOREACH(kp
, key_tree
, &keys
)
4844 if (kp
->mod
& ShiftMask
)
4845 kp
->mod
= mod
| ShiftMask
;
4849 for (i
= 0; i
< LENGTH(buttons
); i
++)
4850 if (buttons
[i
].mask
& ShiftMask
)
4851 buttons
[i
].mask
= mod
| ShiftMask
;
4853 buttons
[i
].mask
= mod
;
4858 TAILQ_ENTRY(spawn_prog
) entry
;
4863 TAILQ_HEAD(spawn_list
, spawn_prog
);
4864 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4867 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4870 struct spawn_prog
*prog
= NULL
;
4872 char *ap
, **real_args
;
4874 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4877 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4878 if (!strcasecmp(spawn_name
, prog
->name
))
4882 warnx("spawn_custom: program %s not found", spawn_name
);
4886 /* make room for expanded args */
4887 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4888 err(1, "spawn_custom: calloc real_args");
4890 /* expand spawn_args into real_args */
4891 for (i
= 0; i
< prog
->argc
; i
++) {
4893 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4894 if (!strcasecmp(ap
, "$bar_border")) {
4896 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4898 err(1, "spawn_custom border color");
4899 } else if (!strcasecmp(ap
, "$bar_color")) {
4901 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4903 err(1, "spawn_custom bar color");
4904 } else if (!strcasecmp(ap
, "$bar_font")) {
4905 if ((real_args
[i
] = strdup(bar_fonts
))
4907 err(1, "spawn_custom bar fonts");
4908 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4910 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4912 err(1, "spawn_custom color font");
4913 } else if (!strcasecmp(ap
, "$color_focus")) {
4915 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4917 err(1, "spawn_custom color focus");
4918 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4920 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4922 err(1, "spawn_custom color unfocus");
4924 /* no match --> copy as is */
4925 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4926 err(1, "spawn_custom strdup(ap)");
4928 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4933 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4934 for (i
= 0; i
< prog
->argc
; i
++)
4935 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4936 DNPRINTF(SWM_D_SPAWN
, "\n");
4938 *ret_args
= real_args
;
4939 return (prog
->argc
);
4943 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4949 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4953 spawn(r
->ws
->idx
, &a
, 1);
4955 for (i
= 0; i
< spawn_argc
; i
++)
4961 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4967 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4971 if (pipe(select_list_pipe
) == -1)
4972 err(1, "pipe error");
4973 if (pipe(select_resp_pipe
) == -1)
4974 err(1, "pipe error");
4976 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4977 err(1, "could not disable SIGPIPE");
4978 switch (*pid
= fork()) {
4980 err(1, "cannot fork");
4983 if (dup2(select_list_pipe
[0], 0) == -1)
4985 if (dup2(select_resp_pipe
[1], 1) == -1)
4987 close(select_list_pipe
[1]);
4988 close(select_resp_pipe
[0]);
4989 spawn(r
->ws
->idx
, &a
, 0);
4991 default: /* parent */
4992 close(select_list_pipe
[0]);
4993 close(select_resp_pipe
[1]);
4997 for (i
= 0; i
< spawn_argc
; i
++)
5003 spawn_insert(char *name
, char *args
)
5005 char *arg
, *cp
, *ptr
;
5006 struct spawn_prog
*sp
;
5008 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
5010 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
5011 err(1, "spawn_insert: malloc");
5012 if ((sp
->name
= strdup(name
)) == NULL
)
5013 err(1, "spawn_insert: strdup");
5015 /* convert the arguments to an argument list */
5016 if ((ptr
= cp
= strdup(args
)) == NULL
)
5017 err(1, "spawn_insert: strdup");
5018 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
5019 /* empty field; skip it */
5024 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
5025 sizeof *sp
->argv
)) == NULL
)
5026 err(1, "spawn_insert: realloc");
5027 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
5028 err(1, "spawn_insert: strdup");
5032 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
5033 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
5037 spawn_remove(struct spawn_prog
*sp
)
5041 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
5043 TAILQ_REMOVE(&spawns
, sp
, entry
);
5044 for (i
= 0; i
< sp
->argc
; i
++)
5050 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
5054 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
5056 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
5059 spawn_insert(name
, args
);
5061 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
5065 setspawn(char *name
, char *args
)
5067 struct spawn_prog
*sp
;
5069 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
5074 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5075 if (!strcmp(sp
->name
, name
)) {
5079 spawn_replace(sp
, name
, args
);
5080 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5084 if (*args
== '\0') {
5085 warnx("error: setspawn: cannot find program: %s", name
);
5089 spawn_insert(name
, args
);
5090 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
5094 setconfspawn(char *selector
, char *value
, int flags
)
5096 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
5098 setspawn(selector
, value
);
5100 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
5107 setconfspawn("term", "xterm", 0);
5108 setconfspawn("spawn_term", "xterm", 0);
5109 setconfspawn("screenshot_all", "screenshot.sh full", 0);
5110 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
5111 setconfspawn("lock", "xlock", 0);
5112 setconfspawn("initscr", "initscreen.sh", 0);
5113 setconfspawn("menu", "dmenu_run"
5116 " -nf $bar_font_color"
5118 " -sf $bar_color", 0);
5119 setconfspawn("search", "dmenu"
5123 " -nf $bar_font_color"
5125 " -sf $bar_color", 0);
5126 setconfspawn("name_workspace", "dmenu"
5130 " -nf $bar_font_color"
5132 " -sf $bar_color", 0);
5136 #define SWM_MODNAME_SIZE 32
5137 #define SWM_KEY_WS "\n+ \t"
5139 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
5143 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
5144 if (mod
== NULL
|| ks
== NULL
) {
5145 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
5148 if (keystr
== NULL
|| strlen(keystr
) == 0) {
5149 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
5155 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
5156 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
5158 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
5159 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
5161 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
5163 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
5165 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
5167 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
5169 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
5171 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
5172 *mod
|= ControlMask
;
5174 *ks
= XStringToKeysym(name
);
5175 XConvertCase(*ks
, ks
, &uks
);
5176 if (ks
== NoSymbol
) {
5178 "parsekeys: invalid key %s\n",
5184 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
5189 strdupsafe(char *str
)
5194 return (strdup(str
));
5198 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
5202 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
5203 keyfuncs
[kfid
].name
, spawn_name
);
5205 if ((kp
= malloc(sizeof *kp
)) == NULL
)
5206 err(1, "key_insert: malloc");
5211 kp
->spawn_name
= strdupsafe(spawn_name
);
5212 RB_INSERT(key_tree
, &keys
, kp
);
5214 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
5218 key_lookup(unsigned int mod
, KeySym ks
)
5225 return (RB_FIND(key_tree
, &keys
, &kp
));
5229 key_remove(struct key
*kp
)
5231 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
5233 RB_REMOVE(key_tree
, &keys
, kp
);
5234 free(kp
->spawn_name
);
5237 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
5241 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5244 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
5248 key_insert(mod
, ks
, kfid
, spawn_name
);
5250 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
5254 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
5259 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
5260 keyfuncs
[kfid
].name
, spawn_name
);
5262 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
5263 if (kfid
== kf_invalid
)
5266 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
5267 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5270 if (kfid
== kf_invalid
) {
5271 warnx("error: setkeybinding: cannot find mod/key combination");
5272 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5276 key_insert(mod
, ks
, kfid
, spawn_name
);
5277 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
5281 setconfbinding(char *selector
, char *value
, int flags
)
5283 enum keyfuncid kfid
;
5286 struct spawn_prog
*sp
;
5287 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
5288 if (selector
== NULL
) {
5289 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
5290 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5292 setkeybinding(mod
, ks
, kfid
, NULL
);
5297 /* search by key function name */
5298 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
5299 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
5300 SWM_FUNCNAME_LEN
) == 0) {
5301 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5303 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5304 setkeybinding(mod
, ks
, kfid
, NULL
);
5310 /* search by custom spawn name */
5311 TAILQ_FOREACH(sp
, &spawns
, entry
) {
5312 if (strcasecmp(selector
, sp
->name
) == 0) {
5313 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
5315 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
5316 setkeybinding(mod
, ks
, kf_spawn_custom
,
5323 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
5330 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
5331 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, kf_flip_layout
, NULL
);
5332 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
5333 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
5334 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
5335 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
5336 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
5337 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
5338 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
5339 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
5340 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
5341 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
5342 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
5343 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
5344 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_custom
,"term");
5345 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
5346 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
5347 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
5348 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
5349 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
5350 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
5351 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
5352 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
5353 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
5354 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
5355 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
5356 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
5357 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
5358 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
5359 setkeybinding(MODKEY
, XK_F1
, kf_ws_11
, NULL
);
5360 setkeybinding(MODKEY
, XK_F2
, kf_ws_12
, NULL
);
5361 setkeybinding(MODKEY
, XK_F3
, kf_ws_13
, NULL
);
5362 setkeybinding(MODKEY
, XK_F4
, kf_ws_14
, NULL
);
5363 setkeybinding(MODKEY
, XK_F5
, kf_ws_15
, NULL
);
5364 setkeybinding(MODKEY
, XK_F6
, kf_ws_16
, NULL
);
5365 setkeybinding(MODKEY
, XK_F7
, kf_ws_17
, NULL
);
5366 setkeybinding(MODKEY
, XK_F8
, kf_ws_18
, NULL
);
5367 setkeybinding(MODKEY
, XK_F9
, kf_ws_19
, NULL
);
5368 setkeybinding(MODKEY
, XK_F10
, kf_ws_20
, NULL
);
5369 setkeybinding(MODKEY
, XK_F11
, kf_ws_21
, NULL
);
5370 setkeybinding(MODKEY
, XK_F12
, kf_ws_22
, NULL
);
5371 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
5372 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
5373 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
5374 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
5375 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
5376 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
5377 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
5378 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
5379 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
5380 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
5381 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
5382 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
5383 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
5384 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
5385 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
5386 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
5387 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
5388 setkeybinding(MODKEY
|ShiftMask
, XK_F1
, kf_mvws_11
, NULL
);
5389 setkeybinding(MODKEY
|ShiftMask
, XK_F2
, kf_mvws_12
, NULL
);
5390 setkeybinding(MODKEY
|ShiftMask
, XK_F3
, kf_mvws_13
, NULL
);
5391 setkeybinding(MODKEY
|ShiftMask
, XK_F4
, kf_mvws_14
, NULL
);
5392 setkeybinding(MODKEY
|ShiftMask
, XK_F5
, kf_mvws_15
, NULL
);
5393 setkeybinding(MODKEY
|ShiftMask
, XK_F6
, kf_mvws_16
, NULL
);
5394 setkeybinding(MODKEY
|ShiftMask
, XK_F7
, kf_mvws_17
, NULL
);
5395 setkeybinding(MODKEY
|ShiftMask
, XK_F8
, kf_mvws_18
, NULL
);
5396 setkeybinding(MODKEY
|ShiftMask
, XK_F9
, kf_mvws_19
, NULL
);
5397 setkeybinding(MODKEY
|ShiftMask
, XK_F10
, kf_mvws_20
, NULL
);
5398 setkeybinding(MODKEY
|ShiftMask
, XK_F11
, kf_mvws_21
, NULL
);
5399 setkeybinding(MODKEY
|ShiftMask
, XK_F12
, kf_mvws_22
, NULL
);
5400 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
5401 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
5402 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
5403 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
5404 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
5405 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
5406 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
5407 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
5408 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
5409 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
5410 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
5411 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
5412 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
5413 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
5414 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
5415 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
5416 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
5417 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
5418 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
5419 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
5420 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
5421 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
5422 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
5423 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
5424 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
5425 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
5427 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
5436 while (RB_EMPTY(&keys
) == 0) {
5437 kp
= RB_ROOT(&keys
);
5443 setkeymapping(char *selector
, char *value
, int flags
)
5445 char keymapping_file
[PATH_MAX
];
5446 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5447 if (value
[0] == '~')
5448 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5449 pwd
->pw_dir
, &value
[1]);
5451 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5453 /* load new key bindings; if it fails, revert to default bindings */
5454 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5458 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5463 updatenumlockmask(void)
5466 XModifierKeymap
*modmap
;
5468 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
5470 modmap
= XGetModifierMapping(display
);
5471 for (i
= 0; i
< 8; i
++)
5472 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
5473 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
5474 == XKeysymToKeycode(display
, XK_Num_Lock
))
5475 numlockmask
= (1 << i
);
5477 XFreeModifiermap(modmap
);
5486 unsigned int modifiers
[] =
5487 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
5490 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5491 updatenumlockmask();
5493 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
5494 for (k
= 0; k
< num_screens
; k
++) {
5495 if (TAILQ_EMPTY(&screens
[k
].rl
))
5497 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
5499 RB_FOREACH(kp
, key_tree
, &keys
) {
5500 if ((code
= XKeysymToKeycode(display
, kp
->keysym
)))
5501 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5502 XGrabKey(display
, code
,
5503 kp
->mod
| modifiers
[j
],
5504 screens
[k
].root
, True
,
5505 GrabModeAsync
, GrabModeAsync
);
5511 grabbuttons(struct ws_win
*win
, int focused
)
5514 unsigned int modifiers
[] =
5515 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5517 updatenumlockmask();
5518 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, win
->id
,
5519 XCB_BUTTON_MASK_ANY
);
5521 for (i
= 0; i
< LENGTH(buttons
); i
++)
5522 if (buttons
[i
].action
== client_click
)
5523 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5524 xcb_grab_button(conn
, False
, win
->id
,
5526 XCB_GRAB_MODE_ASYNC
,
5533 xcb_grab_button(conn
, False
, win
->id
, BUTTONMASK
,
5534 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_SYNC
,
5535 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_ANY
,
5536 XCB_BUTTON_MASK_ANY
);
5539 const char *quirkname
[] = {
5540 "NONE", /* config string for "no value" */
5549 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5550 #define SWM_Q_WS "\n|+ \t"
5552 parsequirks(char *qstr
, unsigned long *quirk
)
5562 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5564 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5565 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5566 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5567 DNPRINTF(SWM_D_QUIRK
,
5568 "parsequirks: %s\n", name
);
5573 *quirk
|= 1 << (i
-1);
5577 if (i
>= LENGTH(quirkname
)) {
5578 DNPRINTF(SWM_D_QUIRK
,
5579 "parsequirks: invalid quirk [%s]\n", name
);
5587 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5591 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5594 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5595 err(1, "quirk_insert: malloc");
5596 if ((qp
->class = strdup(class)) == NULL
)
5597 err(1, "quirk_insert: strdup");
5598 if ((qp
->name
= strdup(name
)) == NULL
)
5599 err(1, "quirk_insert: strdup");
5602 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5604 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5608 quirk_remove(struct quirk
*qp
)
5610 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5611 qp
->name
, qp
->quirk
);
5613 TAILQ_REMOVE(&quirks
, qp
, entry
);
5618 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5622 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5623 unsigned long quirk
)
5625 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5626 qp
->name
, qp
->quirk
);
5629 quirk_insert(class, name
, quirk
);
5631 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5635 setquirk(const char *class, const char *name
, unsigned long quirk
)
5639 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5642 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5643 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5647 quirk_replace(qp
, class, name
, quirk
);
5648 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5653 warnx("error: setquirk: cannot find class/name combination");
5657 quirk_insert(class, name
, quirk
);
5658 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5662 setconfquirk(char *selector
, char *value
, int flags
)
5664 char *cp
, *class, *name
;
5666 unsigned long quirks
;
5667 if (selector
== NULL
)
5669 if ((cp
= strchr(selector
, ':')) == NULL
)
5674 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5675 setquirk(class, name
, quirks
);
5682 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5683 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5684 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5685 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5686 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5687 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5688 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5689 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5690 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5691 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5692 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5693 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5694 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5697 /* conf file stuff */
5698 #define SWM_CONF_FILE "spectrwm.conf"
5699 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5703 SWM_S_BAR_AT_BOTTOM
,
5704 SWM_S_BAR_BORDER_WIDTH
,
5711 SWM_S_CLOCK_ENABLED
,
5714 SWM_S_CYCLE_VISIBLE
,
5716 SWM_S_DISABLE_BORDER
,
5718 SWM_S_FOCUS_CLOSE_WRAP
,
5719 SWM_S_FOCUS_DEFAULT
,
5725 SWM_S_STACK_ENABLED
,
5727 SWM_S_TITLE_CLASS_ENABLED
,
5728 SWM_S_TITLE_NAME_ENABLED
,
5729 SWM_S_URGENT_ENABLED
,
5730 SWM_S_VERBOSE_LAYOUT
,
5731 SWM_S_WINDOW_NAME_ENABLED
,
5732 SWM_S_WORKSPACE_LIMIT
5736 setconfvalue(char *selector
, char *value
, int flags
)
5742 case SWM_S_BAR_ACTION
:
5744 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5745 err(1, "setconfvalue: bar_action");
5747 case SWM_S_BAR_AT_BOTTOM
:
5748 bar_at_bottom
= atoi(value
);
5750 case SWM_S_BAR_BORDER_WIDTH
:
5751 bar_border_width
= atoi(value
);
5752 if (bar_border_width
< 0)
5753 bar_border_width
= 0;
5755 case SWM_S_BAR_DELAY
:
5756 bar_delay
= atoi(value
);
5758 case SWM_S_BAR_ENABLED
:
5759 bar_enabled
= atoi(value
);
5761 case SWM_S_BAR_FONT
:
5763 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5764 err(1, "setconfvalue: asprintf: failed to allocate "
5765 "memory for bar_fonts.");
5769 case SWM_S_BAR_FORMAT
:
5771 if ((bar_format
= strdup(value
)) == NULL
)
5772 err(1, "setconfvalue: bar_format");
5774 case SWM_S_BAR_JUSTIFY
:
5775 if (!strcmp(value
, "left"))
5776 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5777 else if (!strcmp(value
, "center"))
5778 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5779 else if (!strcmp(value
, "right"))
5780 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5782 errx(1, "invalid bar_justify");
5784 case SWM_S_BORDER_WIDTH
:
5785 border_width
= atoi(value
);
5786 if (border_width
< 0)
5789 case SWM_S_CLOCK_ENABLED
:
5790 clock_enabled
= atoi(value
);
5792 case SWM_S_CLOCK_FORMAT
:
5793 #ifndef SWM_DENY_CLOCK_FORMAT
5795 if ((clock_format
= strdup(value
)) == NULL
)
5796 err(1, "setconfvalue: clock_format");
5799 case SWM_S_CYCLE_EMPTY
:
5800 cycle_empty
= atoi(value
);
5802 case SWM_S_CYCLE_VISIBLE
:
5803 cycle_visible
= atoi(value
);
5805 case SWM_S_DIALOG_RATIO
:
5806 dialog_ratio
= atof(value
);
5807 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5810 case SWM_S_DISABLE_BORDER
:
5811 disable_border
= atoi(value
);
5813 case SWM_S_FOCUS_CLOSE
:
5814 if (!strcmp(value
, "first"))
5815 focus_close
= SWM_STACK_BOTTOM
;
5816 else if (!strcmp(value
, "last"))
5817 focus_close
= SWM_STACK_TOP
;
5818 else if (!strcmp(value
, "next"))
5819 focus_close
= SWM_STACK_ABOVE
;
5820 else if (!strcmp(value
, "previous"))
5821 focus_close
= SWM_STACK_BELOW
;
5823 errx(1, "focus_close");
5825 case SWM_S_FOCUS_CLOSE_WRAP
:
5826 focus_close_wrap
= atoi(value
);
5828 case SWM_S_FOCUS_DEFAULT
:
5829 if (!strcmp(value
, "last"))
5830 focus_default
= SWM_STACK_TOP
;
5831 else if (!strcmp(value
, "first"))
5832 focus_default
= SWM_STACK_BOTTOM
;
5834 errx(1, "focus_default");
5836 case SWM_S_FOCUS_MODE
:
5837 if (!strcmp(value
, "default"))
5838 focus_mode
= SWM_FOCUS_DEFAULT
;
5839 else if (!strcmp(value
, "follow_cursor"))
5840 focus_mode
= SWM_FOCUS_FOLLOW
;
5841 else if (!strcmp(value
, "synergy"))
5842 focus_mode
= SWM_FOCUS_SYNERGY
;
5844 errx(1, "focus_mode");
5846 case SWM_S_SPAWN_ORDER
:
5847 if (!strcmp(value
, "first"))
5848 spawn_position
= SWM_STACK_BOTTOM
;
5849 else if (!strcmp(value
, "last"))
5850 spawn_position
= SWM_STACK_TOP
;
5851 else if (!strcmp(value
, "next"))
5852 spawn_position
= SWM_STACK_ABOVE
;
5853 else if (!strcmp(value
, "previous"))
5854 spawn_position
= SWM_STACK_BELOW
;
5856 errx(1, "spawn_position");
5858 case SWM_S_SPAWN_TERM
:
5859 setconfspawn("term", value
, 0);
5860 setconfspawn("spawn_term", value
, 0);
5864 case SWM_S_SS_ENABLED
:
5865 ss_enabled
= atoi(value
);
5867 case SWM_S_STACK_ENABLED
:
5868 stack_enabled
= atoi(value
);
5870 case SWM_S_TERM_WIDTH
:
5871 term_width
= atoi(value
);
5875 case SWM_S_TITLE_CLASS_ENABLED
:
5876 title_class_enabled
= atoi(value
);
5878 case SWM_S_TITLE_NAME_ENABLED
:
5879 title_name_enabled
= atoi(value
);
5881 case SWM_S_URGENT_ENABLED
:
5882 urgent_enabled
= atoi(value
);
5884 case SWM_S_VERBOSE_LAYOUT
:
5885 verbose_layout
= atoi(value
);
5886 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5888 layouts
[i
].l_string
= fancy_stacker
;
5890 layouts
[i
].l_string
= plain_stacker
;
5893 case SWM_S_WINDOW_NAME_ENABLED
:
5894 window_name_enabled
= atoi(value
);
5896 case SWM_S_WORKSPACE_LIMIT
:
5897 workspace_limit
= atoi(value
);
5898 if (workspace_limit
> SWM_WS_MAX
)
5899 workspace_limit
= SWM_WS_MAX
;
5900 else if (workspace_limit
< 1)
5901 workspace_limit
= 1;
5910 setconfmodkey(char *selector
, char *value
, int flags
)
5912 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5913 update_modkey(Mod1Mask
);
5914 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5915 update_modkey(Mod2Mask
);
5916 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5917 update_modkey(Mod3Mask
);
5918 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5919 update_modkey(Mod4Mask
);
5926 setconfcolor(char *selector
, char *value
, int flags
)
5928 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5933 setconfregion(char *selector
, char *value
, int flags
)
5935 custom_region(value
);
5940 setautorun(char *selector
, char *value
, int flags
)
5950 if (getenv("SWM_STARTED"))
5954 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5955 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5957 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5958 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5961 * This is a little intricate
5963 * If the pid already exists we simply reuse it because it means it was
5964 * used before AND not claimed by manage_window. We get away with
5965 * altering it in the parent after INSERT because this can not be a race
5968 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5971 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5973 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5974 err(1, "setautorun: realloc");
5975 a
.argv
[argc
- 1] = ap
;
5978 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5979 err(1, "setautorun: realloc");
5980 a
.argv
[argc
] = NULL
;
5982 if ((pid
= fork()) == 0) {
5983 spawn(ws_id
, &a
, 1);
5992 p
= calloc(1, sizeof *p
);
5995 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
6005 setlayout(char *selector
, char *value
, int flags
)
6007 int ws_id
, i
, x
, mg
, ma
, si
, raise
, f
= 0;
6008 int st
= SWM_V_STACK
, num_screens
;
6010 struct workspace
*ws
;
6012 if (getenv("SWM_STARTED"))
6016 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
6017 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
6018 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6019 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6022 if (ws_id
< 0 || ws_id
>= workspace_limit
)
6023 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
6025 if (!strcasecmp(s
, "vertical"))
6027 else if (!strcasecmp(s
, "vertical_flip")) {
6030 } else if (!strcasecmp(s
, "horizontal"))
6032 else if (!strcasecmp(s
, "horizontal_flip")) {
6035 } else if (!strcasecmp(s
, "fullscreen"))
6038 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
6039 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
6042 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
6043 for (i
= 0; i
< num_screens
; i
++) {
6044 ws
= (struct workspace
*)&screens
[i
].ws
;
6045 ws
[ws_id
].cur_layout
= &layouts
[st
];
6047 ws
[ws_id
].always_raise
= raise
;
6048 if (st
== SWM_MAX_STACK
)
6052 for (x
= 0; x
< abs(mg
); x
++) {
6053 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6054 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
6055 SWM_ARG_ID_MASTERSHRINK
);
6059 for (x
= 0; x
< abs(ma
); x
++) {
6060 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6061 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
6062 SWM_ARG_ID_MASTERDEL
);
6066 for (x
= 0; x
< abs(si
); x
++) {
6067 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6068 si
>= 0 ? SWM_ARG_ID_STACKINC
:
6069 SWM_ARG_ID_STACKDEC
);
6074 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
6075 SWM_ARG_ID_FLIPLAYOUT
);
6083 /* config options */
6084 struct config_option
{
6086 int (*func
)(char*, char*, int);
6089 struct config_option configopt
[] = {
6090 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
6091 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
6092 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
6093 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
6094 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
6095 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
6096 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
6097 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
6098 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
6099 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
6100 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
6101 { "keyboard_mapping", setkeymapping
, 0 },
6102 { "bind", setconfbinding
, 0 },
6103 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
6104 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
6105 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
6106 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
6107 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
6108 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
6109 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
6110 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
6111 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
6112 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
6113 { "modkey", setconfmodkey
, 0 },
6114 { "program", setconfspawn
, 0 },
6115 { "quirk", setconfquirk
, 0 },
6116 { "region", setconfregion
, 0 },
6117 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
6118 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
6119 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
6120 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
6121 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
6122 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
6123 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
6124 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
6125 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
6126 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
6127 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
6128 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
6129 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
6130 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
6131 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
6132 { "autorun", setautorun
, 0 },
6133 { "layout", setlayout
, 0 },
6138 conf_load(char *filename
, int keymapping
)
6141 char *line
, *cp
, *optsub
, *optval
;
6142 size_t linelen
, lineno
= 0;
6143 int wordlen
, i
, optind
;
6144 struct config_option
*opt
;
6146 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
6148 if (filename
== NULL
) {
6149 warnx("conf_load: no filename");
6152 if ((config
= fopen(filename
, "r")) == NULL
) {
6153 warn("conf_load: fopen: %s", filename
);
6157 while (!feof(config
)) {
6158 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
6161 err(1, "%s", filename
);
6166 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6167 if (cp
[0] == '\0') {
6172 /* get config option */
6173 wordlen
= strcspn(cp
, "=[ \t\n");
6175 warnx("%s: line %zd: no option found",
6180 for (i
= 0; i
< LENGTH(configopt
); i
++) {
6181 opt
= &configopt
[i
];
6182 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
6183 strlen(opt
->optname
) == wordlen
) {
6189 warnx("%s: line %zd: unknown option %.*s",
6190 filename
, lineno
, wordlen
, cp
);
6193 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
6194 warnx("%s: line %zd: invalid option %.*s",
6195 filename
, lineno
, wordlen
, cp
);
6199 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
6200 /* get [selector] if any */
6204 wordlen
= strcspn(cp
, "]");
6207 warnx("%s: line %zd: syntax error",
6212 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
6214 warnx("%s: line %zd: unable to allocate"
6215 "memory for selector", filename
,
6221 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
6223 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
6225 optval
= strdup(cp
);
6226 /* call function to deal with it all */
6227 if (configopt
[optind
].func(optsub
, optval
,
6228 configopt
[optind
].funcflags
) != 0)
6229 errx(1, "%s: line %zd: invalid data for %s",
6230 filename
, lineno
, configopt
[optind
].optname
);
6237 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
6244 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
6250 set_child_transient(struct ws_win
*win
, Window
*trans
)
6252 struct ws_win
*parent
, *w
;
6253 XWMHints
*wmh
= NULL
;
6254 struct swm_region
*r
;
6255 struct workspace
*ws
;
6257 parent
= find_window(win
->transient
);
6259 parent
->child_trans
= win
;
6261 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
6262 " for 0x%x trans 0x%x\n", win
->id
, win
->transient
);
6264 if (win
->hints
== NULL
) {
6265 warnx("no hints for 0x%x", win
->id
);
6269 r
= root_to_region(win
->wa
.root
);
6271 /* parent doen't exist in our window list */
6272 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
6276 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
6277 warnx("can't get hints for 0x%x", w
->id
);
6281 if (win
->hints
->window_group
!= wmh
->window_group
)
6284 w
->child_trans
= win
;
6285 win
->transient
= w
->id
;
6287 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
6288 "transient to 0x%x\n", win
->transient
);
6298 window_get_pid(xcb_window_t win
)
6303 xcb_get_property_cookie_t pc
;
6304 xcb_get_property_reply_t
*pr
;
6306 apid
= get_atom_from_string("_NET_WM_PID");
6307 if (apid
== XCB_ATOM_NONE
)
6310 pc
= xcb_get_property(conn
, False
, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
6311 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6314 if (pr
->type
!= XCB_ATOM_CARDINAL
)
6317 ret
= *(long *)xcb_get_property_value(pr
);
6323 apid
= get_atom_from_string("_SWM_PID");
6324 pc
= xcb_get_property(conn
, False
, win
, apid
, XCB_ATOM_STRING
,
6326 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
6329 if (pr
->type
!= XCB_ATOM_STRING
)
6332 ret
= strtonum(xcb_get_property_value(pr
), 0, UINT_MAX
, &errstr
);
6339 manage_window(xcb_window_t id
)
6342 struct workspace
*ws
;
6343 struct ws_win
*win
, *ww
;
6344 int format
, i
, ws_idx
, n
, border_me
= 0;
6345 unsigned long nitems
, bytes
;
6346 Atom ws_idx_atom
= 0, type
;
6347 Atom
*prot
= NULL
, *pp
;
6348 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
6349 struct swm_region
*r
;
6353 uint32_t event_mask
;
6355 if ((win
= find_window(id
)) != NULL
)
6356 return (win
); /* already being managed */
6358 /* see if we are on the unmanaged list */
6359 if ((win
= find_unmanaged_window(id
)) != NULL
) {
6360 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
6361 "window: 0x%x\n", win
->id
);
6362 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6364 set_child_transient(win
, &trans
);
6366 if (trans
&& (ww
= find_window(trans
)))
6367 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
6368 else if ((ww
= win
->ws
->focus
) &&
6369 spawn_position
== SWM_STACK_ABOVE
)
6370 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
,
6372 else if (ww
&& spawn_position
== SWM_STACK_BELOW
)
6373 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6374 else switch (spawn_position
) {
6377 case SWM_STACK_ABOVE
:
6378 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6380 case SWM_STACK_BOTTOM
:
6381 case SWM_STACK_BELOW
:
6382 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6385 ewmh_update_actions(win
);
6389 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
6390 err(1, "manage_window: calloc: failed to allocate memory for "
6396 /* see if we need to override the workspace */
6397 p
= find_pid(window_get_pid(id
));
6399 /* Get all the window data in one shot */
6400 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
6402 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
6403 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
6405 XGetWindowAttributes(display
, id
, &win
->wa
);
6406 XGetWMNormalHints(display
, id
, &win
->sh
, &win
->sh_mask
);
6407 win
->hints
= XGetWMHints(display
, id
);
6408 XGetTransientForHint(display
, id
, &trans
);
6410 win
->transient
= trans
;
6411 set_child_transient(win
, &trans
);
6412 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%x, "
6413 "transient: 0x%x\n", win
->id
, win
->transient
);
6416 /* get supported protocols */
6417 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
6418 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
6419 if (*pp
== takefocus
)
6420 win
->take_focus
= 1;
6422 win
->can_delete
= 1;
6428 win
->iconic
= get_iconic(win
);
6431 * Figure out where to put the window. If it was previously assigned to
6432 * a workspace (either by spawn() or manually moving), and isn't
6433 * transient, * put it in the same workspace
6435 r
= root_to_region(win
->wa
.root
);
6437 ws
= &r
->s
->ws
[p
->ws
];
6438 TAILQ_REMOVE(&pidlist
, p
, entry
);
6441 } else if (prop
&& win
->transient
== 0) {
6442 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
6443 ws_idx
= strtonum((const char *)prop
, 0, workspace_limit
- 1,
6446 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
6449 ws
= &r
->s
->ws
[ws_idx
];
6452 /* this should launch transients in the same ws as parent */
6454 if ((ww
= find_window(trans
)) != NULL
)
6460 warnx("manage_window: fix this "
6466 /* set up the window layout */
6469 win
->s
= r
->s
; /* this never changes */
6470 if (trans
&& (ww
= find_window(trans
)))
6471 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
6472 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
6473 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
6475 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
6476 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
6477 else switch (spawn_position
) {
6480 case SWM_STACK_ABOVE
:
6481 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
6483 case SWM_STACK_BOTTOM
:
6484 case SWM_STACK_BELOW
:
6485 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
6488 /* ignore window border if there is one. */
6489 WIDTH(win
) = win
->wa
.width
;
6490 HEIGHT(win
) = win
->wa
.height
;
6491 X(win
) = win
->wa
.x
+ win
->wa
.border_width
;
6492 Y(win
) = win
->wa
.y
+ win
->wa
.border_width
;
6494 win
->g_floatvalid
= 0;
6495 win
->floatmaxed
= 0;
6496 win
->ewmh_flags
= 0;
6498 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%x, (x,y) w x h: "
6499 "(%d,%d) %d x %d, ws: %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
6500 HEIGHT(win
), ws
->idx
);
6502 constrain_window(win
, r
, 0);
6504 /* Set window properties so we can remember this after reincarnation */
6505 if (ws_idx_atom
&& prop
== NULL
&&
6506 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
6508 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6510 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
6511 PropModeReplace
, ws_idx_str
, strlen((char *)ws_idx_str
));
6516 ewmh_autoquirk(win
);
6518 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
6519 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6520 win
->ch
.res_class
, win
->ch
.res_name
);
6522 /* java is retarded so treat it special */
6523 if (strstr(win
->ch
.res_name
, "sun-awt")) {
6528 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6529 if (!strcmp(win
->ch
.res_class
, qp
->class) &&
6530 !strcmp(win
->ch
.res_name
, qp
->name
)) {
6531 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
6532 "class: %s, name: %s\n", win
->ch
.res_class
,
6534 if (qp
->quirk
& SWM_Q_FLOAT
) {
6538 win
->quirks
= qp
->quirk
;
6543 /* alter window position if quirky */
6544 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6545 win
->manual
= 1; /* don't center the quirky windows */
6546 if (bar_enabled
&& Y(win
) < bar_height
)
6547 Y(win
) = bar_height
;
6548 if (WIDTH(win
) + X(win
) > WIDTH(r
))
6549 X(win
) = WIDTH(r
) - WIDTH(win
) - 2;
6553 /* Reset font sizes (the bruteforce way; no default keybinding). */
6554 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6555 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6556 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
6557 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6558 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
6561 ewmh_get_win_state(win
);
6562 ewmh_update_actions(win
);
6563 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
6568 X(win
) -= border_width
;
6569 Y(win
) -= border_width
;
6573 event_mask
= XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
|
6574 XCB_EVENT_MASK_PROPERTY_CHANGE
|
6575 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
6577 xcb_change_window_attributes(conn
, id
, XCB_CW_EVENT_MASK
, &event_mask
);
6579 /* floaters need to be mapped if they are in the current workspace */
6580 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
6581 map_window_raised(win
->id
);
6587 free_window(struct ws_win
*win
)
6589 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%x\n", win
->id
);
6594 /* needed for restart wm */
6595 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
6597 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6599 if (win
->ch
.res_class
)
6600 XFree(win
->ch
.res_class
);
6601 if (win
->ch
.res_name
)
6602 XFree(win
->ch
.res_name
);
6607 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6613 unmanage_window(struct ws_win
*win
)
6615 struct ws_win
*parent
;
6616 xcb_screen_t
*screen
;
6621 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%x\n", win
->id
);
6623 if (win
->transient
) {
6624 parent
= find_window(win
->transient
);
6626 parent
->child_trans
= NULL
;
6629 /* focus on root just in case */
6630 screen
= xcb_setup_roots_iterator(xcb_get_setup(conn
)).data
;
6631 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
6632 screen
->root
, XCB_CURRENT_TIME
);
6641 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
6642 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6646 focus_magic(struct ws_win
*win
)
6648 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%x\n", WINID(win
));
6651 /* if there are no windows clear the status-bar */
6656 if (win
->child_trans
) {
6657 /* win = parent & has a transient so focus on that */
6659 focus_win(win
->child_trans
);
6660 if (win
->child_trans
->take_focus
)
6661 client_msg(win
, takefocus
);
6663 /* make sure transient hasn't disappeared */
6664 if (validate_win(win
->child_trans
) == 0) {
6665 focus_win(win
->child_trans
);
6666 if (win
->child_trans
->take_focus
)
6667 client_msg(win
->child_trans
, takefocus
);
6669 win
->child_trans
= NULL
;
6671 if (win
->take_focus
)
6672 client_msg(win
, takefocus
);
6678 if (win
->take_focus
)
6679 client_msg(win
, takefocus
);
6686 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%lx\n", e
->xexpose
.window
);
6693 XKeyEvent
*ev
= &e
->xkey
;
6695 struct swm_region
*r
;
6697 keysym
= XkbKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0, 0);
6698 if ((kp
= key_lookup(CLEANMASK(ev
->state
), keysym
)) == NULL
)
6700 if (keyfuncs
[kp
->funcid
].func
== NULL
)
6703 r
= root_to_region(ev
->root
);
6704 if (kp
->funcid
== kf_spawn_custom
)
6705 spawn_custom(r
, &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
6707 keyfuncs
[kp
->funcid
].func(r
, &(keyfuncs
[kp
->funcid
].args
));
6711 buttonpress(XEvent
*e
)
6715 XButtonPressedEvent
*ev
= &e
->xbutton
;
6717 if ((win
= find_window(ev
->window
)) == NULL
)
6721 action
= client_click
;
6723 for (i
= 0; i
< LENGTH(buttons
); i
++)
6724 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6725 buttons
[i
].button
== ev
->button
&&
6726 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
6727 buttons
[i
].func(win
, &buttons
[i
].args
);
6731 configurerequest(XEvent
*e
)
6733 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
6738 if ((win
= find_window(ev
->window
)) == NULL
)
6739 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6743 bzero(&wc
, sizeof wc
);
6746 wc
.width
= ev
->width
;
6747 wc
.height
= ev
->height
;
6748 wc
.border_width
= ev
->border_width
;
6749 wc
.sibling
= ev
->above
;
6750 wc
.stack_mode
= ev
->detail
;
6752 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: 0x%lx, "
6753 "new: %s, (x,y) w x h: (%d,%d) %d x %d\n", ev
->window
,
6754 YESNO(new), wc
.x
, wc
.y
, wc
.width
, wc
.height
);
6756 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
6757 } else if ((!win
->manual
|| win
->quirks
& SWM_Q_ANYWHERE
) &&
6758 !(win
->sh_mask
& EWMH_F_FULLSCREEN
)) {
6759 win
->g_float
.x
= ev
->x
- X(win
->ws
->r
);
6760 win
->g_float
.y
= ev
->y
- Y(win
->ws
->r
);
6761 win
->g_float
.w
= ev
->width
;
6762 win
->g_float
.h
= ev
->height
;
6763 win
->g_floatvalid
= 1;
6765 if (win
->floating
) {
6766 win
->g
= win
->g_float
;
6767 win
->g
.x
+= X(win
->ws
->r
);
6768 win
->g
.y
+= Y(win
->ws
->r
);
6771 config_win(win
, ev
);
6774 config_win(win
, ev
);
6779 configurenotify(XEvent
*e
)
6783 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%lx\n",
6784 e
->xconfigure
.window
);
6786 win
= find_window(e
->xconfigure
.window
);
6788 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &win
->sh_mask
);
6792 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6793 drain_enter_notify();
6798 destroynotify(XEvent
*e
)
6801 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
6803 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%lx\n", ev
->window
);
6805 if ((win
= find_window(ev
->window
)) == NULL
) {
6806 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6812 /* make sure we focus on something */
6815 unmanage_window(win
);
6817 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6818 drain_enter_notify();
6823 enternotify(XEvent
*e
)
6825 XCrossingEvent
*ev
= &e
->xcrossing
;
6830 Window focus_return
;
6831 int revert_to_return
;
6833 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%lx, mode: %d, detail: "
6834 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6835 "state: %d\n", ev
->window
, ev
->mode
, ev
->detail
, ev
->root
,
6836 ev
->subwindow
, YESNO(ev
->same_screen
), YESNO(ev
->focus
), ev
->state
);
6838 if (ev
->mode
!= NotifyNormal
) {
6839 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6844 switch (focus_mode
) {
6845 case SWM_FOCUS_DEFAULT
:
6847 case SWM_FOCUS_FOLLOW
:
6849 case SWM_FOCUS_SYNERGY
:
6852 * all these checks need to be in this order because the
6853 * XCheckTypedWindowEvent relies on weeding out the previous events
6855 * making this code an option would enable a follow mouse for focus
6860 * state is set when we are switching workspaces and focus is set when
6861 * the window or a subwindow already has focus (occurs during restart).
6863 * Only honor the focus flag if last_focus_event is not FocusOut,
6864 * this allows spectrwm to continue to control focus when another
6865 * program is also playing with it.
6867 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6868 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6873 * happens when a window is created or destroyed and the border
6874 * crosses the mouse pointer and when switching ws
6876 * we need the subwindow test to see if we came from root in order
6877 * to give focus to floaters
6879 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6880 ev
->subwindow
== 0) {
6881 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6885 /* this window already has focus */
6886 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6887 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6891 /* this window is being deleted or moved to another ws */
6892 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6894 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6895 XPutBackEvent(display
, &cne
);
6899 if ((win
= find_window(ev
->window
)) == NULL
) {
6900 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6905 * In fullstack kill all enters unless they come from a different ws
6906 * (i.e. another region) or focus has been grabbed externally.
6908 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6909 last_focus_event
!= FocusOut
) {
6910 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6911 if ((w
= find_window(focus_return
)) == NULL
||
6913 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6921 if ((win
= find_window(ev
->window
)) == NULL
) {
6922 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6927 * if we have more enternotifies let them handle it in due time
6929 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
6930 DNPRINTF(SWM_D_EVENT
,
6931 "ignoring enternotify: got more enternotify\n");
6932 XPutBackEvent(display
, &cne
);
6939 /* lets us use one switch statement for arbitrary mode/detail combinations */
6940 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6943 focusevent(XEvent
*e
)
6947 u_int32_t mode_detail
;
6948 XFocusChangeEvent
*ev
= &e
->xfocus
;
6950 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: 0x%lx mode: %d "
6951 "detail: %d\n", ev
->type
== FocusIn
? "entering" : "leaving",
6952 ev
->window
, ev
->mode
, ev
->detail
);
6954 if (last_focus_event
== ev
->type
) {
6955 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
6959 last_focus_event
= ev
->type
;
6960 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
6962 switch (mode_detail
) {
6963 /* synergy client focus operations */
6964 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
6965 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
6967 /* synergy server focus operations */
6968 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
6970 /* Entering applications like rdesktop that mangle the pointer */
6971 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
6973 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
6974 XSetWindowBorder(display
, win
->id
,
6975 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
6976 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
6979 warnx("ignoring focusevent");
6980 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
6987 mapnotify(XEvent
*e
)
6990 XMapEvent
*ev
= &e
->xmap
;
6992 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%lx\n", ev
->window
);
6994 win
= manage_window(ev
->window
);
6996 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
6999 * focus_win can only set input focus on a mapped window.
7000 * make sure the window really has focus since it is just being mapped.
7002 if (win
->ws
->focus
== win
)
7007 mappingnotify(XEvent
*e
)
7009 XMappingEvent
*ev
= &e
->xmapping
;
7011 XRefreshKeyboardMapping(ev
);
7012 if (ev
->request
== MappingKeyboard
)
7017 maprequest(XEvent
*e
)
7020 struct swm_region
*r
;
7021 XWindowAttributes wa
;
7022 XMapRequestEvent
*ev
= &e
->xmaprequest
;
7024 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%lx\n",
7025 e
->xmaprequest
.window
);
7027 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
7029 if (wa
.override_redirect
)
7032 win
= manage_window(e
->xmaprequest
.window
);
7034 return; /* can't happen */
7038 /* make new win focused */
7039 r
= root_to_region(win
->wa
.root
);
7040 if (win
->ws
== r
->ws
)
7045 propertynotify(XEvent
*e
)
7048 XPropertyEvent
*ev
= &e
->xproperty
;
7051 name
= XGetAtomName(display
, ev
->atom
);
7052 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%lx, atom: %s\n",
7057 win
= find_window(ev
->window
);
7061 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
7062 update_iconic(win
, 0);
7063 map_window_raised(win
->id
);
7071 case XA_WM_NORMAL_HINTS
:
7073 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
7074 warnx("normal hints: flag 0x%x", win
->sh
.flags
);
7075 if (win
->sh
.flags
& PMinSize
) {
7076 WIDTH(win
) = win
->sh
.min_width
;
7077 HEIGHT(win
) = win
->sh
.min_height
;
7078 warnx("min %d %d", WIDTH(win
), HEIGHT(win
));
7080 XMoveResizeWindow(display
, win
->id
,
7081 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
));
7093 unmapnotify(XEvent
*e
)
7097 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%lx\n", e
->xunmap
.window
);
7099 /* determine if we need to help unmanage this window */
7100 win
= find_window(e
->xunmap
.window
);
7104 if (getstate(e
->xunmap
.window
) == XCB_ICCCM_WM_STATE_NORMAL
) {
7105 unmanage_window(win
);
7108 /* giant hack for apps that don't destroy transient windows */
7109 /* eat a bunch of events to prevent remanaging the window */
7111 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
7112 EnterWindowMask
, &cne
))
7114 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
7115 StructureNotifyMask
, &cne
))
7117 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
7118 SubstructureNotifyMask
, &cne
))
7120 /* resend unmap because we ated it */
7121 XUnmapWindow(display
, e
->xunmap
.window
);
7124 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7125 drain_enter_notify();
7129 visibilitynotify(XEvent
*e
)
7132 struct swm_region
*r
;
7134 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%lx\n",
7135 e
->xvisibility
.window
);
7137 if (e
->xvisibility
.state
== VisibilityUnobscured
) {
7138 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7139 for (i
= 0; i
< num_screens
; i
++)
7140 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7141 if (e
->xvisibility
.window
== WINID(r
->bar
))
7147 clientmessage(XEvent
*e
)
7149 XClientMessageEvent
*ev
;
7154 win
= find_window(ev
->window
);
7156 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7157 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
7158 "unmanaged window.\n");
7159 e
->xmaprequest
.window
= ev
->window
;
7165 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx, type: %ld\n",
7166 ev
->window
, ev
->message_type
);
7168 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
7169 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
7172 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
7173 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
7174 if (win
->can_delete
)
7175 client_msg(win
, adelete
);
7177 xcb_kill_client(conn
, win
->id
);
7179 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
7180 DNPRINTF(SWM_D_EVENT
,
7181 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
7182 if (win
->floating
) {
7183 if (ev
->data
.l
[0] & (1<<8)) /* x */
7184 X(win
) = ev
->data
.l
[1];
7185 if (ev
->data
.l
[0] & (1<<9)) /* y */
7186 Y(win
) = ev
->data
.l
[2];
7187 if (ev
->data
.l
[0] & (1<<10)) /* width */
7188 WIDTH(win
) = ev
->data
.l
[3];
7189 if (ev
->data
.l
[0] & (1<<11)) /* height */
7190 HEIGHT(win
) = ev
->data
.l
[4];
7195 /* TODO: Change stack sizes */
7196 /* notify no change was made. */
7197 config_win(win
, NULL
);
7200 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
7201 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
7202 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
7204 ewmh_update_win_state(win
, ev
->data
.l
[2],
7212 xerror_start(Display
*d
, XErrorEvent
*ee
)
7219 xerror(Display
*d
, XErrorEvent
*ee
)
7221 /* warnx("error: %p %p", display, ee); */
7229 xerrorxlib
= XSetErrorHandler(xerror_start
);
7231 /* this causes an error if some other window manager is running */
7232 XSelectInput(display
, DefaultRootWindow(display
),
7233 SubstructureRedirectMask
);
7238 XSetErrorHandler(xerror
);
7244 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
7246 struct swm_region
*r
, *n
;
7247 struct workspace
*ws
= NULL
;
7250 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
7251 s
->idx
, w
, h
, x
, y
);
7253 /* remove any conflicting regions */
7254 n
= TAILQ_FIRST(&s
->rl
);
7257 n
= TAILQ_NEXT(r
, entry
);
7258 if (X(r
) < (x
+ w
) &&
7259 (X(r
) + WIDTH(r
)) > x
&&
7261 (Y(r
) + HEIGHT(r
)) > y
) {
7262 if (r
->ws
->r
!= NULL
)
7263 r
->ws
->old_r
= r
->ws
->r
;
7266 TAILQ_REMOVE(&s
->rl
, r
, entry
);
7267 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
7271 /* search old regions for one to reuse */
7273 /* size + location match */
7274 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7275 if (X(r
) == x
&& Y(r
) == y
&&
7276 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7280 TAILQ_FOREACH(r
, &s
->orl
, entry
)
7281 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
7285 TAILQ_REMOVE(&s
->orl
, r
, entry
);
7286 /* try to use old region's workspace */
7287 if (r
->ws
->r
== NULL
)
7290 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
7291 err(1, "new_region: calloc: failed to allocate memory "
7294 /* if we don't have a workspace already, find one */
7296 for (i
= 0; i
< workspace_limit
; i
++)
7297 if (s
->ws
[i
].r
== NULL
) {
7304 errx(1, "new_region: no free workspaces");
7315 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
7321 #ifdef SWM_XRR_HAS_CRTC
7324 #endif /* SWM_XRR_HAS_CRTC */
7325 struct swm_region
*r
;
7327 xcb_randr_get_screen_resources_current_cookie_t src
;
7328 xcb_randr_get_screen_resources_current_reply_t
*srr
;
7329 xcb_randr_get_crtc_info_cookie_t cic
;
7330 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
7331 xcb_randr_crtc_t
*crtc
;
7332 xcb_screen_t
*screen
= get_screen(i
);
7334 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7335 if (i
>= num_screens
)
7336 errx(1, "scan_xrandr: invalid screen");
7338 /* remove any old regions */
7339 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
7340 r
->ws
->old_r
= r
->ws
->r
= NULL
;
7342 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
7343 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
7347 /* map virtual screens onto physical screens */
7348 #ifdef SWM_XRR_HAS_CRTC
7349 if (xrandr_support
) {
7350 src
= xcb_randr_get_screen_resources_current(conn
,
7352 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
7355 new_region(&screens
[i
], 0, 0,
7356 screen
->width_in_pixels
,
7357 screen
->height_in_pixels
);
7360 ncrtc
= srr
->num_crtcs
;
7361 for (c
= 0; c
< ncrtc
; c
++) {
7362 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
7363 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
7365 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
7368 if (cir
->num_outputs
== 0) {
7374 new_region(&screens
[i
], 0, 0,
7375 screen
->width_in_pixels
,
7376 screen
->height_in_pixels
);
7378 new_region(&screens
[i
],
7379 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
7384 #endif /* SWM_XRR_HAS_CRTC */
7386 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
7387 screen
->height_in_pixels
);
7392 screenchange(XEvent
*e
)
7394 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
7395 struct swm_region
*r
;
7398 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%lx\n", xe
->root
);
7400 if (!XRRUpdateConfiguration(e
))
7403 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7404 /* silly event doesn't include the screen index */
7405 for (i
= 0; i
< num_screens
; i
++)
7406 if (screens
[i
].root
== xe
->root
)
7408 if (i
>= num_screens
)
7409 errx(1, "screenchange: screen not found");
7411 /* brute force for now, just re-enumerate the regions */
7414 /* add bars to all regions */
7415 for (i
= 0; i
< num_screens
; i
++)
7416 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
7419 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7420 drain_enter_notify();
7426 xcb_window_t
*wins
= NULL
;
7428 int i
, j
, num_screens
;
7429 uint16_t state
, manage
;
7431 xcb_query_tree_cookie_t qtc
;
7432 xcb_query_tree_reply_t
*qtr
;
7433 xcb_get_window_attributes_cookie_t c
;
7434 xcb_get_window_attributes_reply_t
*r
;
7435 xcb_get_property_cookie_t pc
;
7437 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7438 for (i
= 0; i
< num_screens
; i
++) {
7439 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
7440 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
7443 wins
= xcb_query_tree_children(qtr
);
7444 no
= xcb_query_tree_children_length(qtr
);
7445 /* attach windows to a region */
7446 /* normal windows */
7447 for (j
= 0; j
< no
; j
++) {
7448 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7449 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7452 if (r
->override_redirect
) {
7457 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7458 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
, &wins
[j
],
7464 state
= getstate(wins
[j
]);
7465 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7466 if (r
->map_state
== XCB_MAP_STATE_VIEWABLE
|| manage
)
7467 manage_window(wins
[j
]);
7470 /* transient windows */
7471 for (j
= 0; j
< no
; j
++) {
7472 c
= xcb_get_window_attributes(conn
, wins
[j
]);
7473 r
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
7476 if (r
->override_redirect
) {
7482 state
= getstate(wins
[j
]);
7483 manage
= state
== XCB_ICCCM_WM_STATE_ICONIC
;
7484 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
7485 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
, &wins
[j
],
7487 manage_window(wins
[j
]);
7496 int i
, j
, k
, num_screens
;
7497 struct workspace
*ws
;
7499 const xcb_query_extension_reply_t
*qep
;
7500 xcb_randr_query_version_cookie_t c
;
7501 xcb_randr_query_version_reply_t
*r
;
7503 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7504 if ((screens
= calloc(num_screens
,
7505 sizeof(struct swm_screen
))) == NULL
)
7506 err(1, "setup_screens: calloc: failed to allocate memory for "
7509 /* initial Xrandr setup */
7510 xrandr_support
= False
;
7511 c
= xcb_randr_query_version(conn
, True
, True
);
7512 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
7514 if (r
->major_version
>= 1)
7515 xrandr_support
= True
;
7518 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
7519 xrandr_eventbase
= qep
->first_event
;
7521 /* map physical screens */
7522 for (i
= 0; i
< num_screens
; i
++) {
7523 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
7525 TAILQ_INIT(&screens
[i
].rl
);
7526 TAILQ_INIT(&screens
[i
].orl
);
7527 screens
[i
].root
= RootWindow(display
, i
);
7529 /* set default colors */
7530 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
7531 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
7532 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
7533 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
7534 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
7536 /* create graphics context on screen */
7537 screens
[i
].bar_gc
= xcb_generate_id(conn
);
7539 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
7540 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
7542 /* set default cursor */
7543 XDefineCursor(display
, screens
[i
].root
,
7544 XCreateFontCursor(display
, XC_left_ptr
));
7546 /* init all workspaces */
7547 /* XXX these should be dynamically allocated too */
7548 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
7549 ws
= &screens
[i
].ws
[j
];
7555 TAILQ_INIT(&ws
->winlist
);
7556 TAILQ_INIT(&ws
->unmanagedlist
);
7558 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
7559 if (layouts
[k
].l_config
!= NULL
)
7560 layouts
[k
].l_config(ws
,
7561 SWM_ARG_ID_STACKINIT
);
7562 ws
->cur_layout
= &layouts
[0];
7563 ws
->cur_layout
->l_string(ws
);
7569 xcb_randr_select_input(conn
, screens
[i
].root
,
7570 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
7577 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
7578 err(1, "setup_globals: strdup: failed to allocate memory.");
7580 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
7581 err(1, "setup_globals: strdup: failed to allocate memory.");
7588 xcb_atom_t netwmcheck
, netwmname
, utf8_string
;
7589 xcb_window_t root
, win
;
7590 xcb_screen_t
*screen
;
7593 /* work around sun jdk bugs, code from wmname */
7594 netwmcheck
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
7595 netwmname
= get_atom_from_string("_NET_WM_NAME");
7596 utf8_string
= get_atom_from_string("UTF8_STRING");
7598 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7599 for (i
= 0; i
< num_screens
; i
++) {
7600 root
= screens
[i
].root
;
7601 screen
= get_screen(i
);
7603 win
= xcb_generate_id(conn
);
7604 wa
[0] = screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
;
7605 wa
[1] = screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
;
7606 xcb_create_window(conn
, screen
->root_depth
, win
, 0,
7607 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
7608 screen
->root_visual
,
7609 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
, wa
);
7611 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
7612 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7613 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7614 netwmcheck
, XCB_ATOM_WINDOW
, 32, 1, &win
);
7615 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
7616 netwmname
, utf8_string
, 8, strlen("LG3D"), "LG3D");
7621 main(int argc
, char *argv
[])
7623 struct swm_region
*r
, *rr
;
7624 struct ws_win
*winfocus
= NULL
;
7627 char conf
[PATH_MAX
], *cfile
= NULL
;
7630 int xfd
, i
, num_screens
;
7632 struct sigaction sact
;
7635 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
7636 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
7638 warnx("no locale support");
7640 if (!X_HAVE_UTF8_STRING
)
7641 warnx("no UTF-8 support");
7643 if (!(display
= XOpenDisplay(0)))
7644 errx(1, "can not open display");
7646 if (!(conn
= XGetXCBConnection(display
)))
7647 errx(1, "can not get XCB connection");
7650 errx(1, "other wm running");
7652 /* handle some signals */
7653 bzero(&sact
, sizeof(sact
));
7654 sigemptyset(&sact
.sa_mask
);
7656 sact
.sa_handler
= sighdlr
;
7657 sigaction(SIGINT
, &sact
, NULL
);
7658 sigaction(SIGQUIT
, &sact
, NULL
);
7659 sigaction(SIGTERM
, &sact
, NULL
);
7660 sigaction(SIGHUP
, &sact
, NULL
);
7662 sact
.sa_handler
= sighdlr
;
7663 sact
.sa_flags
= SA_NOCLDSTOP
;
7664 sigaction(SIGCHLD
, &sact
, NULL
);
7666 astate
= get_atom_from_string("WM_STATE");
7667 aprot
= get_atom_from_string("WM_PROTOCOLS");
7668 adelete
= get_atom_from_string("WM_DELETE_WINDOW");
7669 takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
7670 a_wmname
= get_atom_from_string("WM_NAME");
7671 a_netwmname
= get_atom_from_string("_NET_WM_NAME");
7672 a_utf8_string
= get_atom_from_string("UTF8_STRING");
7673 a_string
= get_atom_from_string("STRING");
7674 a_swm_iconic
= get_atom_from_string("_SWM_ICONIC");
7676 /* look for local and global conf file */
7677 pwd
= getpwuid(getuid());
7679 errx(1, "invalid user: %d", getuid());
7688 for (i
= 0; ; i
++) {
7693 snprintf(conf
, sizeof conf
, "%s/.%s",
7694 pwd
->pw_dir
, SWM_CONF_FILE
);
7698 snprintf(conf
, sizeof conf
, "/etc/%s",
7703 snprintf(conf
, sizeof conf
, "%s/.%s",
7704 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
7708 snprintf(conf
, sizeof conf
, "/etc/%s",
7715 if (strlen(conf
) && stat(conf
, &sb
) != -1)
7716 if (S_ISREG(sb
.st_mode
)) {
7723 /* load conf (if any) */
7725 conf_load(cfile
, SWM_CONF_DEFAULT
);
7728 /* set some values to work around bad programs */
7730 /* grab existing windows (before we build the bars) */
7733 if (getenv("SWM_STARTED") == NULL
)
7734 setenv("SWM_STARTED", "YES", 1);
7736 /* setup all bars */
7737 num_screens
= xcb_setup_roots_length(xcb_get_setup(conn
));
7738 for (i
= 0; i
< num_screens
; i
++)
7739 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
7740 if (winfocus
== NULL
)
7741 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
7749 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7750 drain_enter_notify();
7752 xfd
= xcb_get_file_descriptor(conn
);
7754 while (XPending(display
)) {
7755 XNextEvent(display
, &e
);
7758 if (e
.type
< LASTEvent
) {
7759 DNPRINTF(SWM_D_EVENTQ
,"XEvent: handled: %s, "
7760 "window: 0x%lx, type: %s (%d), %d remaining"
7761 "\n", YESNO(handler
[e
.type
]),
7762 e
.xany
.window
, geteventname(&e
),
7763 e
.type
, QLength(display
));
7765 if (handler
[e
.type
])
7766 handler
[e
.type
](&e
);
7768 DNPRINTF(SWM_D_EVENTQ
, "XRandr Event: window: "
7769 "0x%lx, type: %s (%d)\n", e
.xany
.window
,
7770 xrandr_geteventname(&e
), e
.type
);
7772 switch (e
.type
- xrandr_eventbase
) {
7773 case XCB_RANDR_SCREEN_CHANGE_NOTIFY
:
7782 /* if we are being restarted go focus on first window */
7784 rr
= winfocus
->ws
->r
;
7786 /* not a visible window */
7790 /* move pointer to first screen if multi screen */
7791 if (num_screens
> 1 || outputs
> 1)
7792 xcb_warp_pointer(conn
, XCB_WINDOW_NONE
,
7793 rr
->s
[0].root
, 0, 0, 0, 0, X(rr
),
7794 Y(rr
) + (bar_enabled
? bar_height
: 0));
7796 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7806 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7808 DNPRINTF(SWM_D_MISC
, "select failed");
7809 if (restart_wm
== 1)
7810 restart(NULL
, NULL
);
7811 if (search_resp
== 1)
7824 for (i
= 0; i
< num_screens
; ++i
)
7825 if (screens
[i
].bar_gc
!= 0)
7826 xcb_free_gc(conn
, screens
[i
].bar_gc
);
7827 XFreeFontSet(display
, bar_fs
);
7828 xcb_disconnect(conn
);
7829 XCloseDisplay(display
);