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>
94 #include <X11/Xproto.h>
95 #include <X11/Xutil.h>
96 #include <X11/extensions/Xrandr.h>
97 #include <X11/extensions/XTest.h>
105 #ifdef SPECTRWM_BUILDSTR
106 static const char *buildstr
= SPECTRWM_BUILDSTR
;
108 static const char *buildstr
= SPECTRWM_VERSION
;
112 # error XRandR versions less than 1.0 are not supported
117 #define SWM_XRR_HAS_CRTC
121 /*#define SWM_DEBUG*/
123 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
124 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
125 #define SWM_D_MISC 0x0001
126 #define SWM_D_EVENT 0x0002
127 #define SWM_D_WS 0x0004
128 #define SWM_D_FOCUS 0x0008
129 #define SWM_D_MOVE 0x0010
130 #define SWM_D_STACK 0x0020
131 #define SWM_D_MOUSE 0x0040
132 #define SWM_D_PROP 0x0080
133 #define SWM_D_CLASS 0x0100
134 #define SWM_D_KEY 0x0200
135 #define SWM_D_QUIRK 0x0400
136 #define SWM_D_SPAWN 0x0800
137 #define SWM_D_EVENTQ 0x1000
138 #define SWM_D_CONF 0x2000
139 #define SWM_D_BAR 0x4000
141 u_int32_t swm_debug
= 0
159 #define DPRINTF(x...)
160 #define DNPRINTF(n,x...)
163 #define LENGTH(x) (sizeof x / sizeof x[0])
164 #define MODKEY Mod1Mask
165 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
166 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
167 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
168 #define SWM_PROPLEN (16)
169 #define SWM_FUNCNAME_LEN (32)
170 #define SWM_KEYS_LEN (255)
171 #define SWM_QUIRK_LEN (64)
172 #define X(r) (r)->g.x
173 #define Y(r) (r)->g.y
174 #define WIDTH(r) (r)->g.w
175 #define HEIGHT(r) (r)->g.h
176 #define SH_MIN(w) (w)->sh_mask & PMinSize
177 #define SH_MIN_W(w) (w)->sh.min_width
178 #define SH_MIN_H(w) (w)->sh.min_height
179 #define SH_MAX(w) (w)->sh_mask & PMaxSize
180 #define SH_MAX_W(w) (w)->sh.max_width
181 #define SH_MAX_H(w) (w)->sh.max_height
182 #define SH_INC(w) (w)->sh_mask & PResizeInc
183 #define SH_INC_W(w) (w)->sh.width_inc
184 #define SH_INC_H(w) (w)->sh.height_inc
185 #define SWM_MAX_FONT_STEPS (3)
186 #define WINID(w) ((w) ? (w)->id : 0)
187 #define YESNO(x) ((x) ? "yes" : "no")
189 #define SWM_FOCUS_DEFAULT (0)
190 #define SWM_FOCUS_SYNERGY (1)
191 #define SWM_FOCUS_FOLLOW (2)
193 #define SWM_CONF_DEFAULT (0)
194 #define SWM_CONF_KEYMAPPING (1)
197 #define SWM_LIB "/usr/local/lib/libswmhack.so"
210 volatile sig_atomic_t running
= 1;
211 volatile sig_atomic_t restart_wm
= 0;
213 int last_focus_event
= FocusOut
;
214 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
218 int xrandr_eventbase
;
219 unsigned int numlockmask
= 0;
223 int cycle_visible
= 0;
225 int font_adjusted
= 0;
226 unsigned int mod_key
= MODKEY
;
229 struct swm_region
*search_r
;
230 int select_list_pipe
[2];
231 int select_resp_pipe
[2];
233 volatile sig_atomic_t search_resp
;
234 int search_resp_action
;
236 struct search_window
{
237 TAILQ_ENTRY(search_window
) entry
;
243 TAILQ_HEAD(search_winlist
, search_window
);
244 struct search_winlist search_wl
;
249 SWM_SEARCH_UNICONIFY
,
250 SWM_SEARCH_NAME_WORKSPACE
,
251 SWM_SEARCH_SEARCH_WORKSPACE
,
252 SWM_SEARCH_SEARCH_WINDOW
255 #define SWM_STACK_TOP (0)
256 #define SWM_STACK_BOTTOM (1)
257 #define SWM_STACK_ABOVE (2)
258 #define SWM_STACK_BELOW (3)
261 double dialog_ratio
= 0.6;
263 #define SWM_BAR_MAX (256)
264 #define SWM_BAR_JUSTIFY_LEFT (0)
265 #define SWM_BAR_JUSTIFY_CENTER (1)
266 #define SWM_BAR_JUSTIFY_RIGHT (2)
267 #define SWM_BAR_OFFSET (4)
268 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
269 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
270 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
271 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
272 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
274 #ifdef X_HAVE_UTF8_STRING
275 #define DRAWSTRING(x...) Xutf8DrawString(x)
277 #define DRAWSTRING(x...) XmbDrawString(x)
280 char *bar_argv
[] = { NULL
, NULL
};
282 unsigned char bar_ext
[SWM_BAR_MAX
];
283 char bar_vertext
[SWM_BAR_MAX
];
285 sig_atomic_t bar_alarm
= 0;
288 int bar_border_width
= 1;
289 int bar_at_bottom
= 0;
291 int bar_extra_running
= 0;
294 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
295 int stack_enabled
= 1;
296 int clock_enabled
= 1;
297 int urgent_enabled
= 0;
298 char *clock_format
= NULL
;
299 int title_name_enabled
= 0;
300 int title_class_enabled
= 0;
301 int window_name_enabled
= 0;
302 int focus_mode
= SWM_FOCUS_DEFAULT
;
303 int focus_close
= SWM_STACK_BELOW
;
304 int focus_close_wrap
= 1;
305 int focus_default
= SWM_STACK_TOP
;
306 int spawn_position
= SWM_STACK_TOP
;
307 int disable_border
= 0;
308 int border_width
= 1;
309 int verbose_layout
= 0;
312 XFontSetExtents
*bar_fs_extents
;
314 char *spawn_term
[] = { NULL
, NULL
}; /* XXX fully dynamic */
317 #define SWM_MENU_FN (2)
318 #define SWM_MENU_NB (4)
319 #define SWM_MENU_NF (6)
320 #define SWM_MENU_SB (8)
321 #define SWM_MENU_SF (10)
323 /* layout manager data */
324 struct swm_geometry
{
334 /* virtual "screens" */
336 TAILQ_ENTRY(swm_region
) entry
;
337 struct swm_geometry g
;
338 struct workspace
*ws
; /* current workspace on this region */
339 struct workspace
*ws_prior
; /* prior workspace on this region */
340 struct swm_screen
*s
; /* screen idx */
343 TAILQ_HEAD(swm_region_list
, swm_region
);
346 TAILQ_ENTRY(ws_win
) entry
;
349 struct ws_win
*child_trans
; /* transient child window */
350 struct swm_geometry g
; /* current geometry */
351 struct swm_geometry g_float
; /* geometry when floating */
352 struct swm_geometry rg_float
; /* region geom when floating */
353 int g_floatvalid
; /* g_float geometry validity */
354 int floatmaxed
; /* whether maxed by max_stack */
358 unsigned int ewmh_flags
;
359 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
365 unsigned long quirks
;
366 struct workspace
*ws
; /* always valid */
367 struct swm_screen
*s
; /* always valid, never changes */
368 XWindowAttributes wa
;
374 TAILQ_HEAD(ws_win_list
, ws_win
);
378 TAILQ_ENTRY(pid_e
) entry
;
382 TAILQ_HEAD(pid_list
, pid_e
);
383 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
385 /* layout handlers */
387 void vertical_config(struct workspace
*, int);
388 void vertical_stack(struct workspace
*, struct swm_geometry
*);
389 void horizontal_config(struct workspace
*, int);
390 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
391 void max_stack(struct workspace
*, struct swm_geometry
*);
392 void plain_stacker(struct workspace
*);
393 void fancy_stacker(struct workspace
*);
395 struct ws_win
*find_window(Window
);
397 void grabbuttons(struct ws_win
*, int);
398 void new_region(struct swm_screen
*, int, int, int, int);
399 void unmanage_window(struct ws_win
*);
400 long getstate(Window
);
402 int conf_load(char *, int);
405 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
406 void (*l_config
)(struct workspace
*, int);
408 #define SWM_L_FOCUSPREV (1<<0)
409 #define SWM_L_MAPONFOCUS (1<<1)
410 void (*l_string
)(struct workspace
*);
412 /* stack, configure */
413 { vertical_stack
, vertical_config
, 0, plain_stacker
},
414 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
416 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
417 { NULL
, NULL
, 0, NULL
},
420 /* position of max_stack mode in the layouts array, index into layouts! */
421 #define SWM_V_STACK (0)
422 #define SWM_H_STACK (1)
423 #define SWM_MAX_STACK (2)
425 #define SWM_H_SLICE (32)
426 #define SWM_V_SLICE (32)
428 /* define work spaces */
430 int idx
; /* workspace index */
431 char *name
; /* workspace name */
432 int always_raise
; /* raise windows on focus */
433 struct layout
*cur_layout
; /* current layout handlers */
434 struct ws_win
*focus
; /* may be NULL */
435 struct ws_win
*focus_prev
; /* may be NULL */
436 struct swm_region
*r
; /* may be NULL */
437 struct swm_region
*old_r
; /* may be NULL */
438 struct ws_win_list winlist
; /* list of windows in ws */
439 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
440 char stacker
[10]; /* display stacker and layout */
444 int horizontal_msize
;
446 int horizontal_stacks
;
455 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
456 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
458 /* physical screen mapping */
459 #define SWM_WS_MAX (22) /* hard limit */
460 int workspace_limit
= 10; /* soft limit */
463 int idx
; /* screen index */
464 struct swm_region_list rl
; /* list of regions on this screen */
465 struct swm_region_list orl
; /* list of old regions */
467 struct workspace ws
[SWM_WS_MAX
];
473 } c
[SWM_S_COLOR_MAX
];
477 struct swm_screen
*screens
;
479 /* args to functions */
482 #define SWM_ARG_ID_FOCUSNEXT (0)
483 #define SWM_ARG_ID_FOCUSPREV (1)
484 #define SWM_ARG_ID_FOCUSMAIN (2)
485 #define SWM_ARG_ID_FOCUSCUR (4)
486 #define SWM_ARG_ID_SWAPNEXT (10)
487 #define SWM_ARG_ID_SWAPPREV (11)
488 #define SWM_ARG_ID_SWAPMAIN (12)
489 #define SWM_ARG_ID_MOVELAST (13)
490 #define SWM_ARG_ID_MASTERSHRINK (20)
491 #define SWM_ARG_ID_MASTERGROW (21)
492 #define SWM_ARG_ID_MASTERADD (22)
493 #define SWM_ARG_ID_MASTERDEL (23)
494 #define SWM_ARG_ID_FLIPLAYOUT (24)
495 #define SWM_ARG_ID_STACKRESET (30)
496 #define SWM_ARG_ID_STACKINIT (31)
497 #define SWM_ARG_ID_CYCLEWS_UP (40)
498 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
499 #define SWM_ARG_ID_CYCLESC_UP (42)
500 #define SWM_ARG_ID_CYCLESC_DOWN (43)
501 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
502 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
503 #define SWM_ARG_ID_STACKINC (50)
504 #define SWM_ARG_ID_STACKDEC (51)
505 #define SWM_ARG_ID_SS_ALL (60)
506 #define SWM_ARG_ID_SS_WINDOW (61)
507 #define SWM_ARG_ID_DONTCENTER (70)
508 #define SWM_ARG_ID_CENTER (71)
509 #define SWM_ARG_ID_KILLWINDOW (80)
510 #define SWM_ARG_ID_DELETEWINDOW (81)
511 #define SWM_ARG_ID_WIDTHGROW (90)
512 #define SWM_ARG_ID_WIDTHSHRINK (91)
513 #define SWM_ARG_ID_HEIGHTGROW (92)
514 #define SWM_ARG_ID_HEIGHTSHRINK (93)
515 #define SWM_ARG_ID_MOVEUP (100)
516 #define SWM_ARG_ID_MOVEDOWN (101)
517 #define SWM_ARG_ID_MOVELEFT (102)
518 #define SWM_ARG_ID_MOVERIGHT (103)
522 void focus(struct swm_region
*, union arg
*);
523 void focus_magic(struct ws_win
*);
527 TAILQ_ENTRY(quirk
) entry
;
531 #define SWM_Q_FLOAT (1<<0) /* float this window */
532 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
533 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
534 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
535 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
536 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
538 TAILQ_HEAD(quirk_list
, quirk
);
539 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
542 * Supported EWMH hints should be added to
543 * both the enum and the ewmh array
545 enum { _NET_ACTIVE_WINDOW
, _NET_MOVERESIZE_WINDOW
, _NET_CLOSE_WINDOW
,
546 _NET_WM_WINDOW_TYPE
, _NET_WM_WINDOW_TYPE_DOCK
,
547 _NET_WM_WINDOW_TYPE_TOOLBAR
, _NET_WM_WINDOW_TYPE_UTILITY
,
548 _NET_WM_WINDOW_TYPE_SPLASH
, _NET_WM_WINDOW_TYPE_DIALOG
,
549 _NET_WM_WINDOW_TYPE_NORMAL
, _NET_WM_STATE
,
550 _NET_WM_STATE_MAXIMIZED_HORZ
, _NET_WM_STATE_MAXIMIZED_VERT
,
551 _NET_WM_STATE_SKIP_TASKBAR
, _NET_WM_STATE_SKIP_PAGER
,
552 _NET_WM_STATE_HIDDEN
, _NET_WM_STATE_ABOVE
, _SWM_WM_STATE_MANUAL
,
553 _NET_WM_STATE_FULLSCREEN
, _NET_WM_ALLOWED_ACTIONS
, _NET_WM_ACTION_MOVE
,
554 _NET_WM_ACTION_RESIZE
, _NET_WM_ACTION_FULLSCREEN
, _NET_WM_ACTION_CLOSE
,
560 } ewmh
[SWM_EWMH_HINT_MAX
] = {
561 /* must be in same order as in the enum */
562 {"_NET_ACTIVE_WINDOW", None
},
563 {"_NET_MOVERESIZE_WINDOW", None
},
564 {"_NET_CLOSE_WINDOW", None
},
565 {"_NET_WM_WINDOW_TYPE", None
},
566 {"_NET_WM_WINDOW_TYPE_DOCK", None
},
567 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None
},
568 {"_NET_WM_WINDOW_TYPE_UTILITY", None
},
569 {"_NET_WM_WINDOW_TYPE_SPLASH", None
},
570 {"_NET_WM_WINDOW_TYPE_DIALOG", None
},
571 {"_NET_WM_WINDOW_TYPE_NORMAL", None
},
572 {"_NET_WM_STATE", None
},
573 {"_NET_WM_STATE_MAXIMIZED_HORZ", None
},
574 {"_NET_WM_STATE_MAXIMIZED_VERT", None
},
575 {"_NET_WM_STATE_SKIP_TASKBAR", None
},
576 {"_NET_WM_STATE_SKIP_PAGER", None
},
577 {"_NET_WM_STATE_HIDDEN", None
},
578 {"_NET_WM_STATE_ABOVE", None
},
579 {"_SWM_WM_STATE_MANUAL", None
},
580 {"_NET_WM_STATE_FULLSCREEN", None
},
581 {"_NET_WM_ALLOWED_ACTIONS", None
},
582 {"_NET_WM_ACTION_MOVE", None
},
583 {"_NET_WM_ACTION_RESIZE", None
},
584 {"_NET_WM_ACTION_FULLSCREEN", None
},
585 {"_NET_WM_ACTION_CLOSE", None
},
588 void store_float_geom(struct ws_win
*, struct swm_region
*);
589 int floating_toggle_win(struct ws_win
*);
590 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
591 unsigned char *get_win_name(Window
);
594 get_property(Window id
, Atom atom
, long count
, Atom type
, unsigned long *nitems
,
595 unsigned long *nbytes
, unsigned char **data
)
598 unsigned long *nbytes_ret
, *nitems_ret
;
599 unsigned long nbytes_tmp
, nitems_tmp
;
602 nbytes_ret
= nbytes
!= NULL
? nbytes
: &nbytes_tmp
;
603 nitems_ret
= nitems
!= NULL
? nitems
: &nitems_tmp
;
605 status
= XGetWindowProperty(display
, id
, atom
, 0L, count
, False
, type
,
606 &real
, &format
, nitems_ret
, nbytes_ret
, data
);
608 if (status
!= Success
)
617 update_iconic(struct ws_win
*win
, int newv
)
624 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
628 XChangeProperty(display
, win
->id
, iprop
, XA_INTEGER
, 32,
629 PropModeReplace
, (unsigned char *)&v
, 1);
631 XDeleteProperty(display
, win
->id
, iprop
);
635 get_iconic(struct ws_win
*win
)
640 unsigned long nitems
, extra
;
641 unsigned char *prop
= NULL
;
643 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
646 status
= XGetWindowProperty(display
, win
->id
, iprop
, 0L, 1L,
647 False
, XA_INTEGER
, &rettype
, &retfmt
, &nitems
, &extra
, &prop
);
648 if (status
!= Success
)
650 if (rettype
!= XA_INTEGER
|| retfmt
!= 32)
654 v
= *((int32_t *)prop
);
668 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
670 for (i
= 0; i
< LENGTH(ewmh
); i
++)
671 ewmh
[i
].atom
= XInternAtom(display
, ewmh
[i
].name
, False
);
673 for (i
= 0; i
< ScreenCount(display
); i
++) {
674 /* Support check window will be created by workaround(). */
676 /* Report supported atoms */
677 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
678 for (j
= 0; j
< LENGTH(ewmh
); j
++)
679 XChangeProperty(display
, screens
[i
].root
,
680 sup_list
, XA_ATOM
, 32,
681 PropModeAppend
, (unsigned char *)&ewmh
[j
].atom
, 1);
689 unsigned char *data
= NULL
;
691 Atom sup_check
, sup_list
;
694 sup_check
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
695 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
697 for (i
= 0; i
< ScreenCount(display
); i
++) {
698 /* Get the support check window and destroy it */
699 success
= get_property(screens
[i
].root
, sup_check
, 1, XA_WINDOW
,
704 XDestroyWindow(display
, id
);
705 XDeleteProperty(display
, screens
[i
].root
, sup_check
);
706 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
714 ewmh_autoquirk(struct ws_win
*win
)
717 unsigned long *data
= NULL
, n
;
720 success
= get_property(win
->id
, ewmh
[_NET_WM_WINDOW_TYPE
].atom
, (~0L),
721 XA_ATOM
, &n
, NULL
, (void *)&data
);
728 for (i
= 0; i
< n
; i
++) {
730 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
732 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
733 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
734 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
736 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
739 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
740 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
742 win
->quirks
= SWM_Q_FLOAT
;
750 #define SWM_EWMH_ACTION_COUNT_MAX (6)
751 #define EWMH_F_FULLSCREEN (1<<0)
752 #define EWMH_F_ABOVE (1<<1)
753 #define EWMH_F_HIDDEN (1<<2)
754 #define EWMH_F_SKIP_PAGER (1<<3)
755 #define EWMH_F_SKIP_TASKBAR (1<<4)
756 #define SWM_F_MANUAL (1<<5)
759 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
761 struct swm_geometry rg
;
769 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%lx, "
770 "fullscreen %s\n", win
->id
, YESNO(fs
));
775 store_float_geom(win
, win
->ws
->r
);
779 if (win
->g_floatvalid
) {
780 /* refloat at last floating relative position */
781 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ rg
.x
;
782 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ rg
.y
;
783 WIDTH(win
) = win
->g_float
.w
;
784 HEIGHT(win
) = win
->g_float
.h
;
792 ewmh_update_actions(struct ws_win
*win
)
794 Atom actions
[SWM_EWMH_ACTION_COUNT_MAX
];
800 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
803 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
804 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
807 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
,
808 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)actions
, n
);
811 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
812 #define _NET_WM_STATE_ADD 1 /* add/set property */
813 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
816 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
818 unsigned int mask
= 0;
819 unsigned int changed
= 0;
820 unsigned int orig_flags
;
825 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
826 mask
= EWMH_F_FULLSCREEN
;
827 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
829 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
831 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
832 mask
= EWMH_F_SKIP_PAGER
;
833 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
834 mask
= EWMH_F_SKIP_TASKBAR
;
837 orig_flags
= win
->ewmh_flags
;
840 case _NET_WM_STATE_REMOVE
:
841 win
->ewmh_flags
&= ~mask
;
843 case _NET_WM_STATE_ADD
:
844 win
->ewmh_flags
|= mask
;
846 case _NET_WM_STATE_TOGGLE
:
847 win
->ewmh_flags
^= mask
;
851 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
853 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
855 if (!floating_toggle_win(win
))
856 win
->ewmh_flags
= orig_flags
; /* revert */
857 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
859 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
860 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
862 if (!ewmh_set_win_fullscreen(win
,
863 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
864 win
->ewmh_flags
= orig_flags
; /* revert */
866 XDeleteProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
868 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
869 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
870 XA_ATOM
, 32, PropModeAppend
,
871 (unsigned char *)&ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
, 1);
872 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
873 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
874 XA_ATOM
, 32, PropModeAppend
,
875 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
, 1);
876 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
877 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
878 XA_ATOM
, 32, PropModeAppend
,
879 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
, 1);
880 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
881 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
882 XA_ATOM
, 32, PropModeAppend
,
883 (unsigned char *)&ewmh
[_NET_WM_STATE_ABOVE
].atom
, 1);
884 if (win
->ewmh_flags
& SWM_F_MANUAL
)
885 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
886 XA_ATOM
, 32, PropModeAppend
,
887 (unsigned char *)&ewmh
[_SWM_WM_STATE_MANUAL
].atom
, 1);
891 ewmh_get_win_state(struct ws_win
*win
)
902 win
->ewmh_flags
|= EWMH_F_ABOVE
;
904 win
->ewmh_flags
|= SWM_F_MANUAL
;
906 success
= get_property(win
->id
, ewmh
[_NET_WM_STATE
].atom
,
907 (~0L), XA_ATOM
, &n
, NULL
, (void *)&states
);
912 for (i
= 0; i
< n
; i
++)
913 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
921 geteventname(XEvent
*e
)
933 name
= "ButtonPress";
936 name
= "ButtonRelease";
939 name
= "MotionNotify";
942 name
= "EnterNotify";
945 name
= "LeaveNotify";
954 name
= "KeymapNotify";
960 name
= "GraphicsExpose";
965 case VisibilityNotify
:
966 name
= "VisibilityNotify";
969 name
= "CreateNotify";
972 name
= "DestroyNotify";
975 name
= "UnmapNotify";
984 name
= "ReparentNotify";
986 case ConfigureNotify
:
987 name
= "ConfigureNotify";
989 case ConfigureRequest
:
990 name
= "ConfigureRequest";
993 name
= "GravityNotify";
996 name
= "ResizeRequest";
998 case CirculateNotify
:
999 name
= "CirculateNotify";
1001 case CirculateRequest
:
1002 name
= "CirculateRequest";
1004 case PropertyNotify
:
1005 name
= "PropertyNotify";
1007 case SelectionClear
:
1008 name
= "SelectionClear";
1010 case SelectionRequest
:
1011 name
= "SelectionRequest";
1013 case SelectionNotify
:
1014 name
= "SelectionNotify";
1016 case ColormapNotify
:
1017 name
= "ColormapNotify";
1020 name
= "ClientMessage";
1023 name
= "MappingNotify";
1033 xrandr_geteventname(XEvent
*e
)
1037 switch(e
->type
- xrandr_eventbase
) {
1038 case RRScreenChangeNotify
:
1039 name
= "RRScreenChangeNotify";
1049 dumpwins(struct swm_region
*r
, union arg
*args
)
1053 XWindowAttributes wa
;
1055 if (r
->ws
== NULL
) {
1056 warnx("dumpwins: invalid workspace");
1060 warnx("=== managed window list ws %02d ===", r
->ws
->idx
);
1062 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1063 state
= getstate(win
->id
);
1064 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1065 warnx("window: 0x%lx, failed XGetWindowAttributes",
1067 warnx("window: 0x%lx, map_state: %d, state: %d, "
1068 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1072 warnx("===== unmanaged window list =====");
1073 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1074 state
= getstate(win
->id
);
1075 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1076 warnx("window: 0x%lx, failed XGetWindowAttributes",
1078 warnx("window: 0x%lx, map_state: %d, state: %d, "
1079 "transient: 0x%lx", win
->id
, wa
.map_state
, state
,
1083 warnx("=================================");
1087 dumpwins(struct swm_region
*r
, union arg
*args
)
1090 #endif /* SWM_DEBUG */
1092 void expose(XEvent
*);
1093 void keypress(XEvent
*);
1094 void buttonpress(XEvent
*);
1095 void configurerequest(XEvent
*);
1096 void configurenotify(XEvent
*);
1097 void destroynotify(XEvent
*);
1098 void enternotify(XEvent
*);
1099 void focusevent(XEvent
*);
1100 void mapnotify(XEvent
*);
1101 void mappingnotify(XEvent
*);
1102 void maprequest(XEvent
*);
1103 void propertynotify(XEvent
*);
1104 void unmapnotify(XEvent
*);
1105 void visibilitynotify(XEvent
*);
1106 void clientmessage(XEvent
*);
1108 void (*handler
[LASTEvent
])(XEvent
*) = {
1110 [KeyPress
] = keypress
,
1111 [ButtonPress
] = buttonpress
,
1112 [ConfigureRequest
] = configurerequest
,
1113 [ConfigureNotify
] = configurenotify
,
1114 [DestroyNotify
] = destroynotify
,
1115 [EnterNotify
] = enternotify
,
1116 [FocusIn
] = focusevent
,
1117 [FocusOut
] = focusevent
,
1118 [MapNotify
] = mapnotify
,
1119 [MappingNotify
] = mappingnotify
,
1120 [MapRequest
] = maprequest
,
1121 [PropertyNotify
] = propertynotify
,
1122 [UnmapNotify
] = unmapnotify
,
1123 [VisibilityNotify
] = visibilitynotify
,
1124 [ClientMessage
] = clientmessage
,
1130 int saved_errno
, status
;
1133 saved_errno
= errno
;
1137 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1142 if (errno
!= ECHILD
)
1143 warn("sighdlr: waitpid");
1144 #endif /* SWM_DEBUG */
1147 if (pid
== searchpid
)
1151 if (WIFEXITED(status
)) {
1152 if (WEXITSTATUS(status
) != 0)
1153 warnx("sighdlr: child exit status: %d",
1154 WEXITSTATUS(status
));
1156 warnx("sighdlr: child is terminated "
1158 #endif /* SWM_DEBUG */
1172 errno
= saved_errno
;
1178 struct pid_e
*p
= NULL
;
1180 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1185 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1194 name_to_color(char *colorname
)
1198 XColor screen_def
, exact_def
;
1199 unsigned long result
= 0;
1200 char cname
[32] = "#";
1202 cmap
= DefaultColormap(display
, screens
[0].idx
);
1203 status
= XAllocNamedColor(display
, cmap
, colorname
,
1204 &screen_def
, &exact_def
);
1206 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1207 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
1211 result
= screen_def
.pixel
;
1213 warnx("color '%s' not found", colorname
);
1219 setscreencolor(char *val
, int i
, int c
)
1221 if (i
> 0 && i
<= ScreenCount(display
)) {
1222 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1223 free(screens
[i
- 1].c
[c
].name
);
1224 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1226 } else if (i
== -1) {
1227 for (i
= 0; i
< ScreenCount(display
); i
++) {
1228 screens
[i
].c
[c
].color
= name_to_color(val
);
1229 free(screens
[i
].c
[c
].name
);
1230 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1234 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1235 i
, ScreenCount(display
));
1239 fancy_stacker(struct workspace
*ws
)
1241 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1242 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1243 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1244 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1245 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1246 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1247 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1248 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1249 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1253 plain_stacker(struct workspace
*ws
)
1255 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1256 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1257 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1258 sizeof ws
->stacker
);
1259 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1260 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1261 sizeof ws
->stacker
);
1265 custom_region(char *val
)
1267 unsigned int sidx
, x
, y
, w
, h
;
1269 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1270 errx(1, "invalid custom region, "
1271 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1272 if (sidx
< 1 || sidx
> ScreenCount(display
))
1273 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1274 sidx
, ScreenCount(display
));
1278 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1280 if (x
> DisplayWidth(display
, sidx
) ||
1281 y
> DisplayHeight(display
, sidx
) ||
1282 w
+ x
> DisplayWidth(display
, sidx
) ||
1283 h
+ y
> DisplayHeight(display
, sidx
)) {
1284 warnx("ignoring region %ux%u+%u+%u - not within screen "
1285 "boundaries (%ux%u)", w
, h
, x
, y
,
1286 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
1290 new_region(&screens
[sidx
], x
, y
, w
, h
);
1294 socket_setnonblock(int fd
)
1298 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1299 err(1, "fcntl F_GETFL");
1300 flags
|= O_NONBLOCK
;
1301 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1302 err(1, "fcntl F_SETFL");
1306 bar_print(struct swm_region
*r
, char *s
)
1310 XRectangle ibox
, lbox
;
1312 XClearWindow(display
, r
->bar_window
);
1315 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1317 switch (bar_justify
) {
1318 case SWM_BAR_JUSTIFY_LEFT
:
1321 case SWM_BAR_JUSTIFY_CENTER
:
1322 x
= (WIDTH(r
) - lbox
.width
) / 2;
1324 case SWM_BAR_JUSTIFY_RIGHT
:
1325 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1329 if (x
< SWM_BAR_OFFSET
)
1332 DRAWSTRING(display
, r
->bar_window
, bar_fs
, r
->s
->bar_gc
,
1333 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1338 bar_extra_stop(void)
1342 bzero(bar_pipe
, sizeof bar_pipe
);
1345 kill(bar_pid
, SIGTERM
);
1348 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1353 bar_class_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1355 int do_class
, do_name
;
1358 if (title_name_enabled
== 0 && title_class_enabled
== 0)
1360 if (cur_focus
== NULL
)
1363 ch
= &cur_focus
->ch
;
1364 do_class
= (title_class_enabled
&& ch
->res_class
!= NULL
);
1365 do_name
= (title_name_enabled
&& ch
->res_name
!= NULL
);
1368 strlcat(s
, ch
->res_class
, sz
);
1369 if (do_class
&& do_name
)
1370 strlcat(s
, ":", sz
);
1372 strlcat(s
, ch
->res_name
, sz
);
1373 strlcat(s
, " ", sz
);
1377 bar_window_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1379 unsigned char *title
;
1381 if (window_name_enabled
&& cur_focus
!= NULL
) {
1382 title
= get_win_name(cur_focus
->id
);
1383 if (title
!= NULL
) {
1384 DNPRINTF(SWM_D_BAR
, "bar_window_name: title: %s\n",
1387 if (cur_focus
->floating
)
1388 strlcat(s
, "(f) ", sz
);
1389 strlcat(s
, (char *)title
, sz
);
1390 strlcat(s
, " ", sz
);
1396 int urgent
[SWM_WS_MAX
];
1398 bar_urgent(char *s
, ssize_t sz
)
1400 XWMHints
*wmh
= NULL
;
1405 if (urgent_enabled
== 0)
1408 for (i
= 0; i
< workspace_limit
; i
++)
1411 for (i
= 0; i
< ScreenCount(display
); i
++)
1412 for (j
= 0; j
< workspace_limit
; j
++)
1413 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1414 wmh
= XGetWMHints(display
, win
->id
);
1418 if (wmh
->flags
& XUrgencyHint
)
1423 strlcat(s
, "* ", sz
);
1424 for (i
= 0; i
< workspace_limit
; i
++) {
1426 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1428 snprintf(b
, sizeof b
, "- ");
1431 strlcat(s
, "* ", sz
);
1439 struct swm_region
*r
;
1442 char ws
[SWM_BAR_MAX
];
1443 char s
[SWM_BAR_MAX
];
1444 unsigned char cn
[SWM_BAR_MAX
];
1445 char loc
[SWM_BAR_MAX
];
1446 char *b
, *stack
= "";
1448 if (bar_enabled
== 0)
1450 if (bar_extra
&& bar_extra_running
) {
1451 /* ignore short reads; it'll correct itself */
1452 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1453 if (b
&& b
[len
- 1] == '\n') {
1455 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1457 if (b
== NULL
&& errno
!= EAGAIN
) {
1458 warn("bar_update: bar_extra failed");
1462 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1464 if (clock_enabled
== 0)
1465 strlcpy(s
, "", sizeof s
);
1468 localtime_r(&tmt
, &tm
);
1469 len
= strftime(s
, sizeof s
, clock_format
, &tm
);
1471 strlcat(s
, " ", sizeof s
);
1474 for (i
= 0; i
< ScreenCount(display
); i
++) {
1476 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1477 strlcpy((char *)cn
, "", sizeof cn
);
1478 strlcpy(ws
, "", sizeof ws
);
1480 bar_urgent((char *)cn
, sizeof cn
);
1481 bar_class_name((char *)cn
, sizeof cn
,
1483 bar_window_name((char *)cn
, sizeof cn
,
1486 snprintf(ws
, sizeof ws
, "<%s>",
1489 stack
= r
->ws
->stacker
;
1491 snprintf(loc
, sizeof loc
,
1492 "%d:%d %s %s %s%s %s %s",
1493 x
++, r
->ws
->idx
+ 1, stack
, ws
, s
, cn
,
1494 bar_ext
, bar_vertext
);
1509 bar_toggle(struct swm_region
*r
, union arg
*args
)
1511 struct swm_region
*tmpr
;
1512 int i
, sc
= ScreenCount(display
);
1514 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1517 for (i
= 0; i
< sc
; i
++)
1518 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1519 XUnmapWindow(display
, tmpr
->bar_window
);
1521 for (i
= 0; i
< sc
; i
++)
1522 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1523 XMapRaised(display
, tmpr
->bar_window
);
1525 bar_enabled
= !bar_enabled
;
1528 /* must be after stack */
1535 XSetWindowAttributes wa
;
1536 struct swm_region
*r
;
1539 /* do this here because the conf file is in memory */
1540 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1541 /* launch external status app */
1542 bar_extra_running
= 1;
1543 if (pipe(bar_pipe
) == -1)
1544 err(1, "pipe error");
1545 socket_setnonblock(bar_pipe
[0]);
1546 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1547 if (dup2(bar_pipe
[0], 0) == -1)
1549 if (dup2(bar_pipe
[1], 1) == -1)
1551 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1552 err(1, "could not disable SIGPIPE");
1553 switch (bar_pid
= fork()) {
1555 err(1, "cannot fork");
1559 execvp(bar_argv
[0], bar_argv
);
1560 err(1, "%s external app failed", bar_argv
[0]);
1562 default: /* parent */
1568 bzero(&wa
, sizeof wa
);
1569 for (i
= 0; i
< ScreenCount(display
); i
++)
1570 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1572 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1573 wa
.background_pixel
=
1574 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1575 XChangeWindowAttributes(display
, r
->bar_window
,
1576 CWBackPixel
| CWBorderPixel
, &wa
);
1582 bar_setup(struct swm_region
*r
)
1584 char *default_string
;
1585 char **missing_charsets
;
1586 int num_missing_charsets
= 0;
1590 XFreeFontSet(display
, bar_fs
);
1595 DNPRINTF(SWM_D_BAR
, "bar_setup: loading bar_fonts: %s\n", bar_fonts
);
1597 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
1598 &num_missing_charsets
, &default_string
);
1600 if (num_missing_charsets
> 0) {
1601 warnx("Unable to load charset(s):");
1603 for (i
= 0; i
< num_missing_charsets
; ++i
)
1604 warnx("%s", missing_charsets
[i
]);
1606 XFreeStringList(missing_charsets
);
1608 if (strcmp(default_string
, ""))
1609 warnx("Glyphs from those sets will be replaced "
1610 "by '%s'.", default_string
);
1612 warnx("Glyphs from those sets won't be drawn.");
1616 errx(1, "Error creating font set structure.");
1618 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
1620 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
1621 2 * bar_border_width
;
1627 y
= bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1629 r
->bar_window
= XCreateSimpleWindow(display
,
1630 r
->s
->root
, x
, y
, WIDTH(r
) - 2 * bar_border_width
,
1631 bar_height
- 2 * bar_border_width
,
1632 bar_border_width
, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
1633 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1634 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
1636 XMapRaised(display
, r
->bar_window
);
1637 DNPRINTF(SWM_D_BAR
, "bar_setup: bar_window: 0x%lx\n", r
->bar_window
);
1639 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1640 err(1, "could not install bar_signal");
1645 drain_enter_notify(void)
1650 while (XCheckMaskEvent(display
, EnterWindowMask
, &cne
))
1653 DNPRINTF(SWM_D_MISC
, "drain_enter_notify: drained: %d\n", i
);
1657 set_win_state(struct ws_win
*win
, long state
)
1659 long data
[] = {state
, None
};
1661 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%lx\n", win
->id
);
1666 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1667 (unsigned char *)data
, 2);
1674 unsigned char *p
= NULL
;
1677 if (!get_property(w
, astate
, 2L, astate
, &n
, NULL
, &p
))
1680 result
= *((long *)p
);
1686 version(struct swm_region
*r
, union arg
*args
)
1688 bar_version
= !bar_version
;
1690 snprintf(bar_vertext
, sizeof bar_vertext
,
1691 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
1693 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
1698 client_msg(struct ws_win
*win
, Atom a
)
1700 XClientMessageEvent cm
;
1705 bzero(&cm
, sizeof cm
);
1706 cm
.type
= ClientMessage
;
1707 cm
.window
= win
->id
;
1708 cm
.message_type
= aprot
;
1711 cm
.data
.l
[1] = CurrentTime
;
1712 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
1715 /* synthetic response to a ConfigureRequest when not making a change */
1717 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
1724 /* send notification of unchanged state. */
1725 ce
.type
= ConfigureNotify
;
1728 ce
.width
= WIDTH(win
);
1729 ce
.height
= HEIGHT(win
);
1730 ce
.override_redirect
= False
;
1734 ce
.display
= display
;
1736 ce
.window
= win
->id
;
1737 ce
.border_width
= border_width
;
1741 ce
.display
= ev
->display
;
1742 ce
.event
= ev
->window
;
1743 ce
.window
= ev
->window
;
1745 /* make response appear more WM_SIZE_HINTS-compliant */
1747 DNPRINTF(SWM_D_MISC
, "config_win: hints: window: 0x%lx,"
1748 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
1749 "%d x %d\n", win
->id
, win
->sh_mask
, SH_MIN_W(win
),
1750 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
1751 SH_INC_W(win
), SH_INC_H(win
));
1755 /* the hint may be set... to 0! */
1756 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
1757 ce
.width
= SH_MIN_W(win
);
1758 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
1759 ce
.height
= SH_MIN_H(win
);
1764 /* may also be advertized as 0 */
1765 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
1766 ce
.width
= SH_MAX_W(win
);
1767 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
1768 ce
.height
= SH_MAX_H(win
);
1771 /* resize increment. */
1773 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
1774 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
1776 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
1777 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
1781 /* adjust x and y for requested border_width. */
1782 ce
.x
+= border_width
- ev
->border_width
;
1783 ce
.y
+= border_width
- ev
->border_width
;
1784 ce
.border_width
= ev
->border_width
;
1785 ce
.above
= ev
->above
;
1788 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
1789 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
1790 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
1792 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
1796 count_win(struct workspace
*ws
, int count_transient
)
1801 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1802 if (count_transient
== 0 && win
->floating
)
1804 if (count_transient
== 0 && win
->transient
)
1810 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
1816 quit(struct swm_region
*r
, union arg
*args
)
1818 DNPRINTF(SWM_D_MISC
, "quit\n");
1823 unmap_window(struct ws_win
*win
)
1828 /* don't unmap again */
1829 if (getstate(win
->id
) == IconicState
)
1832 set_win_state(win
, IconicState
);
1834 XUnmapWindow(display
, win
->id
);
1835 XSetWindowBorder(display
, win
->id
,
1836 win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1845 for (i
= 0; i
< ScreenCount(display
); i
++)
1846 for (j
= 0; j
< workspace_limit
; j
++)
1847 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1852 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
1859 event
.display
= display
; /* Ignored, but what the hell */
1860 event
.window
= win
->id
;
1861 event
.root
= win
->s
->root
;
1862 event
.subwindow
= None
;
1863 event
.time
= CurrentTime
;
1868 event
.same_screen
= True
;
1869 event
.keycode
= XKeysymToKeycode(display
, keysym
);
1870 event
.state
= modifiers
;
1872 event
.type
= KeyPress
;
1873 XSendEvent(event
.display
, event
.window
, True
,
1874 KeyPressMask
, (XEvent
*)&event
);
1876 event
.type
= KeyRelease
;
1877 XSendEvent(event
.display
, event
.window
, True
,
1878 KeyPressMask
, (XEvent
*)&event
);
1883 restart(struct swm_region
*r
, union arg
*args
)
1885 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
1887 /* disable alarm because the following code may not be interrupted */
1889 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
1890 err(1, "can't disable alarm");
1895 XCloseDisplay(display
);
1896 execvp(start_argv
[0], start_argv
);
1897 warn("execvp failed");
1902 root_to_region(Window root
)
1904 struct swm_region
*r
= NULL
;
1906 int i
, x
, y
, wx
, wy
;
1909 for (i
= 0; i
< ScreenCount(display
); i
++)
1910 if (screens
[i
].root
== root
)
1913 if (XQueryPointer(display
, screens
[i
].root
,
1914 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
1915 /* choose a region based on pointer location */
1916 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1917 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
1918 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
1923 r
= TAILQ_FIRST(&screens
[i
].rl
);
1929 find_unmanaged_window(Window id
)
1934 for (i
= 0; i
< ScreenCount(display
); i
++)
1935 for (j
= 0; j
< workspace_limit
; j
++)
1936 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
1944 find_window(Window id
)
1947 Window wrr
, wpr
, *wcr
= NULL
;
1951 for (i
= 0; i
< ScreenCount(display
); i
++)
1952 for (j
= 0; j
< workspace_limit
; j
++)
1953 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1957 /* if we were looking for the parent return that window instead */
1958 if (XQueryTree(display
, id
, &wrr
, &wpr
, &wcr
, &nc
) == 0)
1963 /* ignore not found and root */
1964 if (wpr
== 0 || wrr
== wpr
)
1967 /* look for parent */
1968 for (i
= 0; i
< ScreenCount(display
); i
++)
1969 for (j
= 0; j
< workspace_limit
; j
++)
1970 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1978 spawn(int ws_idx
, union arg
*args
, int close_fd
)
1983 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
1986 close(ConnectionNumber(display
));
1988 setenv("LD_PRELOAD", SWM_LIB
, 1);
1990 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
1991 warn("spawn: asprintf SWM_WS");
1994 setenv("_SWM_WS", ret
, 1);
1998 if (asprintf(&ret
, "%d", getpid()) == -1) {
1999 warn("spawn: asprintf _SWM_PID");
2002 setenv("_SWM_PID", ret
, 1);
2006 if (setsid() == -1) {
2007 warn("spawn: setsid");
2013 * close stdin and stdout to prevent interaction between apps
2014 * and the baraction script
2015 * leave stderr open to record errors
2017 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
2018 warn("spawn: open");
2021 dup2(fd
, STDIN_FILENO
);
2022 dup2(fd
, STDOUT_FILENO
);
2027 execvp(args
->argv
[0], args
->argv
);
2029 warn("spawn: execvp");
2034 spawnterm(struct swm_region
*r
, union arg
*args
)
2036 DNPRINTF(SWM_D_MISC
, "spawnterm\n");
2040 setenv("_SWM_XTERM_FONTADJ", "", 1);
2041 spawn(r
->ws
->idx
, args
, 1);
2046 kill_refs(struct ws_win
*win
)
2049 struct swm_region
*r
;
2050 struct workspace
*ws
;
2055 for (i
= 0; i
< ScreenCount(display
); i
++)
2056 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2057 for (x
= 0; x
< workspace_limit
; x
++) {
2059 if (win
== ws
->focus
)
2061 if (win
== ws
->focus_prev
)
2062 ws
->focus_prev
= NULL
;
2067 validate_win(struct ws_win
*testwin
)
2070 struct workspace
*ws
;
2071 struct swm_region
*r
;
2074 if (testwin
== NULL
)
2077 for (i
= 0; i
< ScreenCount(display
); i
++)
2078 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2079 for (x
= 0; x
< workspace_limit
; x
++) {
2081 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2089 validate_ws(struct workspace
*testws
)
2091 struct swm_region
*r
;
2092 struct workspace
*ws
;
2095 /* validate all ws */
2096 for (i
= 0; i
< ScreenCount(display
); i
++)
2097 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2098 for (x
= 0; x
< workspace_limit
; x
++) {
2107 unfocus_win(struct ws_win
*win
)
2112 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%lx\n", WINID(win
));
2116 if (win
->ws
== NULL
)
2119 if (validate_ws(win
->ws
))
2120 return; /* XXX this gets hit with thunderbird, needs fixing */
2122 if (win
->ws
->r
== NULL
)
2125 if (validate_win(win
)) {
2130 if (win
->ws
->focus
== win
) {
2131 win
->ws
->focus
= NULL
;
2132 win
->ws
->focus_prev
= win
;
2135 if (validate_win(win
->ws
->focus
)) {
2136 kill_refs(win
->ws
->focus
);
2137 win
->ws
->focus
= NULL
;
2139 if (validate_win(win
->ws
->focus_prev
)) {
2140 kill_refs(win
->ws
->focus_prev
);
2141 win
->ws
->focus_prev
= NULL
;
2144 /* drain all previous unfocus events */
2145 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
2148 grabbuttons(win
, 0);
2149 XSetWindowBorder(display
, win
->id
,
2150 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2152 XChangeProperty(display
, win
->s
->root
,
2153 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2154 PropModeReplace
, (unsigned char *)&none
, 1);
2163 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2165 for (i
= 0; i
< ScreenCount(display
); i
++)
2166 for (j
= 0; j
< workspace_limit
; j
++)
2167 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2172 focus_win(struct ws_win
*win
)
2177 struct ws_win
*cfw
= NULL
;
2180 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%lx\n", WINID(win
));
2184 if (win
->ws
== NULL
)
2187 if (validate_ws(win
->ws
))
2188 return; /* XXX this gets hit with thunderbird, needs fixing */
2190 if (validate_win(win
)) {
2195 if (validate_win(win
)) {
2200 XGetInputFocus(display
, &cur_focus
, &rr
);
2201 if ((cfw
= find_window(cur_focus
)) != NULL
)
2204 /* use larger hammer since the window was killed somehow */
2205 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2206 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2207 XSetWindowBorder(display
, cfw
->id
,
2208 cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2211 win
->ws
->focus
= win
;
2213 if (win
->ws
->r
!= NULL
) {
2214 /* drain all previous focus events */
2215 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
2219 XSetInputFocus(display
, win
->id
,
2220 RevertToParent
, CurrentTime
);
2221 grabbuttons(win
, 1);
2222 XSetWindowBorder(display
, win
->id
,
2223 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2224 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2225 win
->ws
->always_raise
)
2226 XMapRaised(display
, win
->id
);
2228 XChangeProperty(display
, win
->s
->root
,
2229 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2230 PropModeReplace
, (unsigned char *)&win
->id
, 1);
2237 switchws(struct swm_region
*r
, union arg
*args
)
2239 int wsid
= args
->id
, unmap_old
= 0;
2240 struct swm_region
*this_r
, *other_r
;
2242 struct workspace
*new_ws
, *old_ws
;
2248 if (wsid
>= workspace_limit
)
2252 old_ws
= this_r
->ws
;
2253 new_ws
= &this_r
->s
->ws
[wsid
];
2255 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2256 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2258 if (new_ws
== NULL
|| old_ws
== NULL
)
2260 if (new_ws
== old_ws
)
2263 other_r
= new_ws
->r
;
2264 if (other_r
== NULL
) {
2265 /* the other workspace is hidden, hide this one */
2269 /* the other ws is visible in another region, exchange them */
2270 other_r
->ws_prior
= new_ws
;
2271 other_r
->ws
= old_ws
;
2272 old_ws
->r
= other_r
;
2274 this_r
->ws_prior
= old_ws
;
2275 this_r
->ws
= new_ws
;
2278 /* this is needed so that we can click on a window after a restart */
2282 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2283 focus(new_ws
->r
, &a
);
2285 /* unmap old windows */
2287 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2290 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2291 drain_enter_notify();
2295 cyclews(struct swm_region
*r
, union arg
*args
)
2298 struct swm_screen
*s
= r
->s
;
2301 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2302 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2307 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2310 case SWM_ARG_ID_CYCLEWS_UP
:
2311 if (a
.id
< workspace_limit
- 1)
2316 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2319 case SWM_ARG_ID_CYCLEWS_DOWN
:
2323 a
.id
= workspace_limit
- 1;
2330 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2332 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2336 } while (a
.id
!= r
->ws
->idx
);
2340 priorws(struct swm_region
*r
, union arg
*args
)
2344 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2345 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2347 if (r
->ws_prior
== NULL
)
2350 a
.id
= r
->ws_prior
->idx
;
2355 cyclescr(struct swm_region
*r
, union arg
*args
)
2357 struct swm_region
*rr
= NULL
;
2361 /* do nothing if we don't have more than one screen */
2362 if (!(ScreenCount(display
) > 1 || outputs
> 1))
2367 case SWM_ARG_ID_CYCLESC_UP
:
2368 rr
= TAILQ_NEXT(r
, entry
);
2370 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2372 case SWM_ARG_ID_CYCLESC_DOWN
:
2373 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2375 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2383 /* move mouse to region */
2385 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2386 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2388 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2391 if (rr
->ws
->focus
) {
2392 /* move to focus window */
2393 x
= X(rr
->ws
->focus
) + 1;
2394 y
= Y(rr
->ws
->focus
) + 1;
2395 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2400 sort_windows(struct ws_win_list
*wl
)
2402 struct ws_win
*win
, *parent
, *nxt
;
2407 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2408 nxt
= TAILQ_NEXT(win
, entry
);
2409 if (win
->transient
) {
2410 parent
= find_window(win
->transient
);
2411 if (parent
== NULL
) {
2412 warnx("not possible bug");
2415 TAILQ_REMOVE(wl
, win
, entry
);
2416 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2423 swapwin(struct swm_region
*r
, union arg
*args
)
2425 struct ws_win
*target
, *source
;
2426 struct ws_win
*cur_focus
;
2427 struct ws_win_list
*wl
;
2430 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2431 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2433 cur_focus
= r
->ws
->focus
;
2434 if (cur_focus
== NULL
)
2438 wl
= &source
->ws
->winlist
;
2441 case SWM_ARG_ID_SWAPPREV
:
2442 if (source
->transient
)
2443 source
= find_window(source
->transient
);
2444 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2445 if (target
&& target
->transient
)
2446 target
= find_window(target
->transient
);
2447 TAILQ_REMOVE(wl
, source
, entry
);
2449 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2451 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2453 case SWM_ARG_ID_SWAPNEXT
:
2454 target
= TAILQ_NEXT(source
, entry
);
2455 /* move the parent and let the sort handle the move */
2456 if (source
->transient
)
2457 source
= find_window(source
->transient
);
2458 TAILQ_REMOVE(wl
, source
, entry
);
2460 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2462 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2464 case SWM_ARG_ID_SWAPMAIN
:
2465 target
= TAILQ_FIRST(wl
);
2466 if (target
== source
) {
2467 if (source
->ws
->focus_prev
!= NULL
&&
2468 source
->ws
->focus_prev
!= target
)
2469 source
= source
->ws
->focus_prev
;
2473 if (target
== NULL
|| source
== NULL
)
2475 source
->ws
->focus_prev
= target
;
2476 TAILQ_REMOVE(wl
, target
, entry
);
2477 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2478 TAILQ_REMOVE(wl
, source
, entry
);
2479 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2481 case SWM_ARG_ID_MOVELAST
:
2482 TAILQ_REMOVE(wl
, source
, entry
);
2483 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2486 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2496 focus_prev(struct ws_win
*win
)
2498 struct ws_win
*winfocus
= NULL
;
2499 struct ws_win
*cur_focus
= NULL
;
2500 struct ws_win_list
*wl
= NULL
;
2501 struct workspace
*ws
= NULL
;
2503 if (!(win
&& win
->ws
))
2508 cur_focus
= ws
->focus
;
2510 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%lx, cur_focus: 0x%lx\n",
2511 WINID(win
), WINID(cur_focus
));
2513 /* pickle, just focus on whatever */
2514 if (cur_focus
== NULL
) {
2515 /* use prev_focus if valid */
2516 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2517 find_window(WINID(ws
->focus_prev
)))
2518 winfocus
= ws
->focus_prev
;
2522 /* if transient focus on parent */
2523 if (cur_focus
->transient
) {
2524 winfocus
= find_window(cur_focus
->transient
);
2528 /* if in max_stack try harder */
2529 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2530 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2531 if (cur_focus
!= ws
->focus_prev
)
2532 winfocus
= ws
->focus_prev
;
2534 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2539 DNPRINTF(SWM_D_FOCUS
, "focus_prev: focus_close: %d\n", focus_close
);
2541 if (winfocus
== NULL
|| winfocus
== win
) {
2542 switch (focus_close
) {
2543 case SWM_STACK_BOTTOM
:
2544 winfocus
= TAILQ_FIRST(wl
);
2547 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2549 case SWM_STACK_ABOVE
:
2550 if ((winfocus
= TAILQ_NEXT(cur_focus
, entry
)) == NULL
) {
2551 if (focus_close_wrap
)
2552 winfocus
= TAILQ_FIRST(wl
);
2554 winfocus
= TAILQ_PREV(cur_focus
,
2555 ws_win_list
, entry
);
2558 case SWM_STACK_BELOW
:
2559 if ((winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
,
2561 if (focus_close_wrap
)
2562 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2564 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2570 if (winfocus
== NULL
) {
2571 if (focus_default
== SWM_STACK_TOP
)
2572 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2574 winfocus
= TAILQ_FIRST(wl
);
2578 focus_magic(winfocus
);
2582 focus(struct swm_region
*r
, union arg
*args
)
2584 struct ws_win
*winfocus
= NULL
, *head
;
2585 struct ws_win
*cur_focus
= NULL
;
2586 struct ws_win_list
*wl
= NULL
;
2587 struct workspace
*ws
= NULL
;
2593 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2595 /* treat FOCUS_CUR special */
2596 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2597 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2598 winfocus
= r
->ws
->focus
;
2599 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2600 winfocus
= r
->ws
->focus_prev
;
2602 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2603 if (winfocus
->iconic
== 0)
2606 focus_magic(winfocus
);
2610 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2614 if (TAILQ_EMPTY(wl
))
2616 /* make sure there is at least one uniconified window */
2618 TAILQ_FOREACH(winfocus
, wl
, entry
)
2619 if (winfocus
->iconic
== 0) {
2627 case SWM_ARG_ID_FOCUSPREV
:
2628 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2630 head
= TAILQ_LAST(wl
, ws_win_list
);
2632 if (WINID(winfocus
) == cur_focus
->transient
) {
2633 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2635 head
= TAILQ_LAST(wl
, ws_win_list
);
2640 if (winfocus
&& winfocus
->iconic
) {
2641 while (winfocus
!= cur_focus
) {
2642 if (winfocus
== NULL
)
2643 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2644 if (winfocus
->iconic
== 0)
2646 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
2652 case SWM_ARG_ID_FOCUSNEXT
:
2653 head
= TAILQ_NEXT(cur_focus
, entry
);
2655 head
= TAILQ_FIRST(wl
);
2659 if (winfocus
&& winfocus
->iconic
) {
2660 while (winfocus
!= cur_focus
) {
2661 if (winfocus
== NULL
)
2662 winfocus
= TAILQ_FIRST(wl
);
2663 if (winfocus
->iconic
== 0)
2665 winfocus
= TAILQ_NEXT(winfocus
, entry
);
2670 case SWM_ARG_ID_FOCUSMAIN
:
2671 winfocus
= TAILQ_FIRST(wl
);
2672 if (winfocus
== cur_focus
)
2673 winfocus
= cur_focus
->ws
->focus_prev
;
2680 focus_magic(winfocus
);
2684 cycle_layout(struct swm_region
*r
, union arg
*args
)
2686 struct workspace
*ws
= r
->ws
;
2689 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2692 if (ws
->cur_layout
->l_stack
== NULL
)
2693 ws
->cur_layout
= &layouts
[0];
2696 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2697 drain_enter_notify();
2698 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2703 stack_config(struct swm_region
*r
, union arg
*args
)
2705 struct workspace
*ws
= r
->ws
;
2707 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
2710 if (ws
->cur_layout
->l_config
!= NULL
)
2711 ws
->cur_layout
->l_config(ws
, args
->id
);
2713 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
2720 struct swm_geometry g
;
2721 struct swm_region
*r
;
2727 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
2729 for (i
= 0; i
< ScreenCount(display
); i
++) {
2733 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2734 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
2735 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
2737 /* start with screen geometry, adjust for bar */
2739 g
.w
-= 2 * border_width
;
2740 g
.h
-= 2 * border_width
;
2746 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
2747 r
->ws
->cur_layout
->l_string(r
->ws
);
2748 /* save r so we can track region changes */
2755 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2756 drain_enter_notify();
2758 DNPRINTF(SWM_D_STACK
, "stack: end\n");
2762 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
2764 /* retain window geom and region geom */
2765 win
->g_float
= win
->g
;
2766 win
->rg_float
= r
->g
;
2767 win
->g_floatvalid
= 1;
2771 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
2779 bzero(&wc
, sizeof wc
);
2780 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
2783 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2784 * geom on ws switches or return from max mode
2786 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&& win
->g_floatvalid
2787 && !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
2789 * use stored g and rg to set relative position and size
2790 * as in old region or before max stack mode
2792 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ X(r
);
2793 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ Y(r
);
2794 WIDTH(win
) = win
->g_float
.w
;
2795 HEIGHT(win
) = win
->g_float
.h
;
2796 win
->g_floatvalid
= 0;
2799 win
->floatmaxed
= 0;
2801 if ((win
->quirks
& SWM_Q_FULLSCREEN
) && (WIDTH(win
) >= WIDTH(r
)) &&
2802 (HEIGHT(win
) >= HEIGHT(r
)))
2803 wc
.border_width
= 0;
2805 wc
.border_width
= border_width
;
2806 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
2807 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
2808 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
2813 * floaters and transients are auto-centred unless moved
2816 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - wc
.border_width
;
2817 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - wc
.border_width
;
2820 /* win can be outside r if new r smaller than old r */
2821 /* Ensure top left corner inside r (move probs otherwise) */
2822 if (X(win
) < X(r
) - wc
.border_width
)
2823 X(win
) = X(r
) - wc
.border_width
;
2824 if (X(win
) > X(r
) + WIDTH(r
) - 1)
2825 X(win
) = (WIDTH(win
) > WIDTH(r
)) ? X(r
) :
2826 (X(r
) + WIDTH(r
) - WIDTH(win
) - 2 * wc
.border_width
);
2827 if (Y(win
) < Y(r
) - wc
.border_width
)
2828 Y(win
) = Y(r
) - wc
.border_width
;
2829 if (Y(win
) > Y(r
) + HEIGHT(r
) - 1)
2830 Y(win
) = (HEIGHT(win
) > HEIGHT(r
)) ? Y(r
) :
2831 (Y(r
) + HEIGHT(r
) - HEIGHT(win
) - 2 * wc
.border_width
);
2835 wc
.width
= WIDTH(win
);
2836 wc
.height
= HEIGHT(win
);
2839 * Retain floater and transient geometry for correct positioning
2840 * when ws changes region
2842 if (!(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
2843 store_float_geom(win
, r
);
2845 DNPRINTF(SWM_D_MISC
, "stack_floater: window: %lu, (x,y) w x h: (%d,%d) "
2846 "%d x %d\n", win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
2848 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2852 * Send keystrokes to terminal to decrease/increase the font size as the
2853 * window size changes.
2856 adjust_font(struct ws_win
*win
)
2858 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
2859 win
->floating
|| win
->transient
)
2862 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
2863 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
2864 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
2865 win
->font_size_boundary
[win
->font_steps
] =
2866 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
2869 win
->last_inc
= win
->sh
.width_inc
;
2870 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
2871 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
2872 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
2875 win
->last_inc
= win
->sh
.width_inc
;
2876 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
2880 #define SWAPXY(g) do { \
2882 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2883 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2886 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
2889 XWindowAttributes wa
;
2890 struct swm_geometry win_g
, r_g
= *g
;
2891 struct ws_win
*win
, *fs_win
= 0;
2892 int i
, j
, s
, stacks
;
2893 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
2894 int hrh
, extra
= 0, h_slice
, last_h
= 0;
2895 int split
, colno
, winno
, mwin
, msize
, mscale
;
2896 int remain
, missing
, v_slice
, reconfigure
;
2899 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
2900 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
2902 winno
= count_win(ws
, 0);
2903 if (winno
== 0 && count_win(ws
, 1) == 0)
2906 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2907 if (win
->transient
== 0 && win
->floating
== 0
2908 && win
->iconic
== 0)
2915 w_inc
= win
->sh
.width_inc
;
2916 w_base
= win
->sh
.base_width
;
2917 mwin
= ws
->l_state
.horizontal_mwin
;
2918 mscale
= ws
->l_state
.horizontal_msize
;
2919 stacks
= ws
->l_state
.horizontal_stacks
;
2922 w_inc
= win
->sh
.height_inc
;
2923 w_base
= win
->sh
.base_height
;
2924 mwin
= ws
->l_state
.vertical_mwin
;
2925 mscale
= ws
->l_state
.vertical_msize
;
2926 stacks
= ws
->l_state
.vertical_stacks
;
2930 if (stacks
> winno
- mwin
)
2931 stacks
= winno
- mwin
;
2935 h_slice
= r_g
.h
/ SWM_H_SLICE
;
2936 if (mwin
&& winno
> mwin
) {
2937 v_slice
= r_g
.w
/ SWM_V_SLICE
;
2941 win_g
.w
= v_slice
* mscale
;
2943 if (w_inc
> 1 && w_inc
< v_slice
) {
2944 /* adjust for window's requested size increment */
2945 remain
= (win_g
.w
- w_base
) % w_inc
;
2951 win_g
.x
+= r_g
.w
- msize
;
2954 colno
= split
= winno
/ stacks
;
2955 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
2956 2 * border_width
) / stacks
);
2958 hrh
= r_g
.h
/ colno
;
2959 extra
= r_g
.h
- (colno
* hrh
);
2960 win_g
.h
= hrh
- 2 * border_width
;
2962 /* stack all the tiled windows */
2963 i
= j
= 0, s
= stacks
;
2964 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2965 if (win
->transient
!= 0 || win
->floating
!= 0)
2967 if (win
->iconic
!= 0)
2970 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
2975 if (split
&& i
== split
) {
2976 colno
= (winno
- mwin
) / stacks
;
2977 if (s
<= (winno
- mwin
) % stacks
)
2979 split
= split
+ colno
;
2980 hrh
= (r_g
.h
/ colno
);
2981 extra
= r_g
.h
- (colno
* hrh
);
2985 win_g
.x
+= win_g
.w
+ 2 * border_width
;
2986 win_g
.w
= (r_g
.w
- msize
-
2987 (stacks
* 2 * border_width
)) / stacks
;
2989 win_g
.w
+= (r_g
.w
- msize
-
2990 (stacks
* 2 * border_width
)) % stacks
;
2994 win_g
.h
= hrh
- 2 * border_width
;
2996 h_inc
= win
->sh
.width_inc
;
2997 h_base
= win
->sh
.base_width
;
2999 h_inc
= win
->sh
.height_inc
;
3000 h_base
= win
->sh
.base_height
;
3002 if (j
== colno
- 1) {
3003 win_g
.h
= hrh
+ extra
;
3004 } else if (h_inc
> 1 && h_inc
< h_slice
) {
3005 /* adjust for window's requested size increment */
3006 remain
= (win_g
.h
- h_base
) % h_inc
;
3007 missing
= h_inc
- remain
;
3009 if (missing
<= extra
|| j
== 0) {
3021 win_g
.y
+= last_h
+ 2 * border_width
;
3023 bzero(&wc
, sizeof wc
);
3024 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
3025 wc
.border_width
= 0;
3026 win_g
.w
+= 2 * border_width
;
3027 win_g
.h
+= 2 * border_width
;
3029 wc
.border_width
= border_width
;
3032 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
3033 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
3035 X(win
) = wc
.x
= win_g
.y
;
3036 Y(win
) = wc
.y
= win_g
.x
;
3037 WIDTH(win
) = wc
.width
= win_g
.h
;
3038 HEIGHT(win
) = wc
.height
= win_g
.w
;
3041 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
3042 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
3044 X(win
) = wc
.x
= win_g
.x
;
3045 Y(win
) = wc
.y
= win_g
.y
;
3046 WIDTH(win
) = wc
.width
= win_g
.w
;
3047 HEIGHT(win
) = wc
.height
= win_g
.h
;
3052 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
3053 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3056 if (XGetWindowAttributes(display
, win
->id
, &wa
))
3057 if (wa
.map_state
== IsUnmapped
)
3058 XMapRaised(display
, win
->id
);
3066 /* now, stack all the floaters and transients */
3067 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3068 if (win
->transient
== 0 && win
->floating
== 0)
3070 if (win
->iconic
== 1)
3072 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3077 stack_floater(win
, ws
->r
);
3078 XMapRaised(display
, win
->id
);
3082 stack_floater(fs_win
, ws
->r
);
3083 XMapRaised(display
, fs_win
->id
);
3088 vertical_config(struct workspace
*ws
, int id
)
3090 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3094 case SWM_ARG_ID_STACKRESET
:
3095 case SWM_ARG_ID_STACKINIT
:
3096 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3097 ws
->l_state
.vertical_mwin
= 1;
3098 ws
->l_state
.vertical_stacks
= 1;
3100 case SWM_ARG_ID_MASTERSHRINK
:
3101 if (ws
->l_state
.vertical_msize
> 1)
3102 ws
->l_state
.vertical_msize
--;
3104 case SWM_ARG_ID_MASTERGROW
:
3105 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3106 ws
->l_state
.vertical_msize
++;
3108 case SWM_ARG_ID_MASTERADD
:
3109 ws
->l_state
.vertical_mwin
++;
3111 case SWM_ARG_ID_MASTERDEL
:
3112 if (ws
->l_state
.vertical_mwin
> 0)
3113 ws
->l_state
.vertical_mwin
--;
3115 case SWM_ARG_ID_STACKINC
:
3116 ws
->l_state
.vertical_stacks
++;
3118 case SWM_ARG_ID_STACKDEC
:
3119 if (ws
->l_state
.vertical_stacks
> 1)
3120 ws
->l_state
.vertical_stacks
--;
3122 case SWM_ARG_ID_FLIPLAYOUT
:
3123 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
3131 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3133 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3135 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
3139 horizontal_config(struct workspace
*ws
, int id
)
3141 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3144 case SWM_ARG_ID_STACKRESET
:
3145 case SWM_ARG_ID_STACKINIT
:
3146 ws
->l_state
.horizontal_mwin
= 1;
3147 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3148 ws
->l_state
.horizontal_stacks
= 1;
3150 case SWM_ARG_ID_MASTERSHRINK
:
3151 if (ws
->l_state
.horizontal_msize
> 1)
3152 ws
->l_state
.horizontal_msize
--;
3154 case SWM_ARG_ID_MASTERGROW
:
3155 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3156 ws
->l_state
.horizontal_msize
++;
3158 case SWM_ARG_ID_MASTERADD
:
3159 ws
->l_state
.horizontal_mwin
++;
3161 case SWM_ARG_ID_MASTERDEL
:
3162 if (ws
->l_state
.horizontal_mwin
> 0)
3163 ws
->l_state
.horizontal_mwin
--;
3165 case SWM_ARG_ID_STACKINC
:
3166 ws
->l_state
.horizontal_stacks
++;
3168 case SWM_ARG_ID_STACKDEC
:
3169 if (ws
->l_state
.horizontal_stacks
> 1)
3170 ws
->l_state
.horizontal_stacks
--;
3172 case SWM_ARG_ID_FLIPLAYOUT
:
3173 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
3181 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3183 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3185 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
3188 /* fullscreen view */
3190 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3193 struct swm_geometry gg
= *g
;
3194 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3198 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3203 winno
= count_win(ws
, 0);
3204 if (winno
== 0 && count_win(ws
, 1) == 0)
3207 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3208 if (win
->transient
) {
3210 parent
= find_window(win
->transient
);
3214 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3216 * retain geometry for retrieval on exit from
3219 store_float_geom(win
, ws
->r
);
3220 win
->floatmaxed
= 1;
3223 /* only reconfigure if necessary */
3224 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3225 HEIGHT(win
) != gg
.h
) {
3226 bzero(&wc
, sizeof wc
);
3227 X(win
) = wc
.x
= gg
.x
;
3228 Y(win
) = wc
.y
= gg
.y
;
3230 wc
.border_width
= border_width
;
3231 WIDTH(win
) = wc
.width
= gg
.w
;
3232 HEIGHT(win
) = wc
.height
= gg
.h
;
3234 wc
.border_width
= 0;
3235 WIDTH(win
) = wc
.width
= gg
.w
+ 2 * border_width
;
3236 HEIGHT(win
) = wc
.height
= gg
.h
+
3239 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
3240 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3242 /* unmap only if we don't have multi screen */
3243 if (win
!= ws
->focus
)
3244 if (!(ScreenCount(display
) > 1 || outputs
> 1))
3248 /* put the last transient on top */
3251 XMapRaised(display
, parent
->id
);
3252 stack_floater(wintrans
, ws
->r
);
3253 focus_magic(wintrans
);
3258 send_to_ws(struct swm_region
*r
, union arg
*args
)
3260 int wsid
= args
->id
;
3261 struct ws_win
*win
= NULL
, *parent
;
3262 struct workspace
*ws
, *nws
;
3263 Atom ws_idx_atom
= 0;
3264 unsigned char ws_idx_str
[SWM_PROPLEN
];
3267 if (wsid
>= workspace_limit
)
3270 if (r
&& r
->ws
&& r
->ws
->focus
)
3276 if (win
->ws
->idx
== wsid
)
3279 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%lx\n", win
->id
);
3282 nws
= &win
->s
->ws
[wsid
];
3284 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3286 if (win
->transient
) {
3287 parent
= find_window(win
->transient
);
3289 unmap_window(parent
);
3290 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3291 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3296 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3297 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3298 if (TAILQ_EMPTY(&ws
->winlist
))
3299 r
->ws
->focus
= NULL
;
3302 /* Try to update the window's workspace property */
3303 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
3305 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3307 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3309 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
3310 PropModeReplace
, ws_idx_str
, strlen((char *)ws_idx_str
));
3318 pressbutton(struct swm_region
*r
, union arg
*args
)
3320 XTestFakeButtonEvent(display
, args
->id
, True
, CurrentTime
);
3321 XTestFakeButtonEvent(display
, args
->id
, False
, CurrentTime
);
3325 raise_toggle(struct swm_region
*r
, union arg
*args
)
3327 if (r
== NULL
|| r
->ws
== NULL
)
3330 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3332 /* bring floaters back to top */
3333 if (r
->ws
->always_raise
== 0)
3338 iconify(struct swm_region
*r
, union arg
*args
)
3342 if (r
->ws
->focus
== NULL
)
3344 unmap_window(r
->ws
->focus
);
3345 update_iconic(r
->ws
->focus
, 1);
3347 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3348 drain_enter_notify();
3349 r
->ws
->focus
= NULL
;
3350 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3355 get_win_name(Window win
)
3357 unsigned char *prop
= NULL
;
3358 unsigned long nbytes
, nitems
;
3360 /* try _NET_WM_NAME first */
3361 if (get_property(win
, a_netwmname
, 0L, a_utf8_string
, NULL
, &nbytes
,
3364 if (get_property(win
, a_netwmname
, nbytes
, a_utf8_string
,
3365 &nitems
, NULL
, &prop
))
3369 /* fallback to WM_NAME */
3370 if (!get_property(win
, a_wmname
, 0L, a_string
, NULL
, &nbytes
, &prop
))
3373 if (get_property(win
, a_wmname
, nbytes
, a_string
, &nitems
, NULL
, &prop
))
3380 uniconify(struct swm_region
*r
, union arg
*args
)
3384 unsigned char *name
;
3387 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3389 if (r
== NULL
|| r
->ws
== NULL
)
3392 /* make sure we have anything to uniconify */
3393 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3394 if (win
->ws
== NULL
)
3395 continue; /* should never happen */
3396 if (win
->iconic
== 0)
3404 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3406 spawn_select(r
, args
, "search", &searchpid
);
3408 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3411 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3412 if (win
->ws
== NULL
)
3413 continue; /* should never happen */
3414 if (win
->iconic
== 0)
3417 name
= get_win_name(win
->id
);
3420 fprintf(lfile
, "%s.%lu\n", name
, win
->id
);
3428 name_workspace(struct swm_region
*r
, union arg
*args
)
3432 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3438 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3440 spawn_select(r
, args
, "name_workspace", &searchpid
);
3442 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3445 fprintf(lfile
, "%s", "");
3450 search_workspace(struct swm_region
*r
, union arg
*args
)
3453 struct workspace
*ws
;
3456 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3462 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3464 spawn_select(r
, args
, "search", &searchpid
);
3466 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3469 for (i
= 0; i
< workspace_limit
; i
++) {
3473 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3474 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3481 search_win_cleanup(void)
3483 struct search_window
*sw
= NULL
;
3485 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3486 XDestroyWindow(display
, sw
->indicator
);
3487 XFreeGC(display
, sw
->gc
);
3488 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3494 search_win(struct swm_region
*r
, union arg
*args
)
3496 struct ws_win
*win
= NULL
;
3497 struct search_window
*sw
= NULL
;
3504 XRectangle ibox
, lbox
;
3506 DNPRINTF(SWM_D_MISC
, "search_win\n");
3509 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3511 spawn_select(r
, args
, "search", &searchpid
);
3513 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3516 TAILQ_INIT(&search_wl
);
3519 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3520 if (win
->iconic
== 1)
3523 sw
= calloc(1, sizeof(struct search_window
));
3525 warn("search_win: calloc");
3527 search_win_cleanup();
3533 snprintf(s
, sizeof s
, "%d", i
);
3536 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
3538 w
= XCreateSimpleWindow(display
,
3539 win
->id
, 0, 0,lbox
.width
+ 4,
3540 bar_fs_extents
->max_logical_extent
.height
, 1,
3541 r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
,
3542 r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
3545 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3547 sw
->gc
= XCreateGC(display
, w
, 0, &gcv
);
3548 XMapRaised(display
, w
);
3549 XSetForeground(display
, sw
->gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
3551 DRAWSTRING(display
, w
, bar_fs
, sw
->gc
, 2,
3552 (bar_fs_extents
->max_logical_extent
.height
-
3553 lbox
.height
) / 2 - lbox
.y
, s
, len
);
3555 fprintf(lfile
, "%d\n", i
);
3563 search_resp_uniconify(char *resp
, unsigned long len
)
3565 unsigned char *name
;
3569 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3571 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3572 if (win
->iconic
== 0)
3574 name
= get_win_name(win
->id
);
3577 if (asprintf(&s
, "%s.%lu", name
, win
->id
) == -1) {
3582 if (strncmp(s
, resp
, len
) == 0) {
3583 /* XXX this should be a callback to generalize */
3584 update_iconic(win
, 0);
3593 search_resp_name_workspace(char *resp
, unsigned long len
)
3595 struct workspace
*ws
;
3597 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3599 if (search_r
->ws
== NULL
)
3604 free(search_r
->ws
->name
);
3605 search_r
->ws
->name
= NULL
;
3609 ws
->name
= strdup(resp
);
3610 if (ws
->name
== NULL
) {
3611 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
3612 "strdup: %s", strerror(errno
));
3619 search_resp_search_workspace(char *resp
, unsigned long len
)
3626 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
3630 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
3637 ws_idx
= strtonum(q
, 1, workspace_limit
, &errstr
);
3639 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
3646 switchws(search_r
, &a
);
3650 search_resp_search_window(char *resp
, unsigned long len
)
3655 struct search_window
*sw
;
3657 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
3661 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
3666 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
3668 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
3675 TAILQ_FOREACH(sw
, &search_wl
, entry
)
3676 if (idx
== sw
->idx
) {
3682 #define MAX_RESP_LEN 1024
3685 search_do_resp(void)
3691 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
3696 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
3697 warn("search: calloc");
3701 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
3703 warn("search: read error");
3706 resp
[rbytes
] = '\0';
3709 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3710 * newline, so work around that by sanitizing the resp now.
3712 resp
[strcspn(resp
, "\n")] = '\0';
3715 switch (search_resp_action
) {
3716 case SWM_SEARCH_UNICONIFY
:
3717 search_resp_uniconify(resp
, len
);
3719 case SWM_SEARCH_NAME_WORKSPACE
:
3720 search_resp_name_workspace(resp
, len
);
3722 case SWM_SEARCH_SEARCH_WORKSPACE
:
3723 search_resp_search_workspace(resp
, len
);
3725 case SWM_SEARCH_SEARCH_WINDOW
:
3726 search_resp_search_window(resp
, len
);
3731 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
3732 search_win_cleanup();
3734 search_resp_action
= SWM_SEARCH_NONE
;
3735 close(select_resp_pipe
[0]);
3740 wkill(struct swm_region
*r
, union arg
*args
)
3742 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
3744 if (r
->ws
->focus
== NULL
)
3747 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
3748 XKillClient(display
, r
->ws
->focus
->id
);
3750 if (r
->ws
->focus
->can_delete
)
3751 client_msg(r
->ws
->focus
, adelete
);
3756 floating_toggle_win(struct ws_win
*win
)
3758 struct swm_region
*r
;
3768 /* reject floating toggles in max stack mode */
3769 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3772 if (win
->floating
) {
3773 if (!win
->floatmaxed
) {
3774 /* retain position for refloat */
3775 store_float_geom(win
, r
);
3779 if (win
->g_floatvalid
) {
3780 /* refloat at last floating relative position */
3781 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ X(r
);
3782 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ Y(r
);
3783 WIDTH(win
) = win
->g_float
.w
;
3784 HEIGHT(win
) = win
->g_float
.h
;
3789 ewmh_update_actions(win
);
3795 floating_toggle(struct swm_region
*r
, union arg
*args
)
3797 struct ws_win
*win
= r
->ws
->focus
;
3803 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
3804 _NET_WM_STATE_TOGGLE
);
3807 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3808 drain_enter_notify();
3810 if (win
== win
->ws
->focus
) {
3811 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3812 focus(win
->ws
->r
, &a
);
3817 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
3819 if (X(win
) + WIDTH(win
) > X(r
) + WIDTH(r
) - border_width
) {
3821 WIDTH(win
) = X(r
) + WIDTH(r
) - X(win
) - border_width
;
3823 X(win
) = X(r
) + WIDTH(r
) - WIDTH(win
) - border_width
;
3826 if (X(win
) < X(r
) - border_width
) {
3828 WIDTH(win
) -= X(r
) - X(win
) - border_width
;
3830 X(win
) = X(r
) - border_width
;
3833 if (Y(win
) + HEIGHT(win
) > Y(r
) + HEIGHT(r
) - border_width
) {
3835 HEIGHT(win
) = Y(r
) + HEIGHT(r
) - Y(win
) - border_width
;
3837 Y(win
) = Y(r
) + HEIGHT(r
) - HEIGHT(win
) - border_width
;
3840 if (Y(win
) < Y(r
) - border_width
) {
3842 HEIGHT(win
) -= Y(r
) - Y(win
) - border_width
;
3844 Y(win
) = Y(r
) - border_width
;
3849 if (HEIGHT(win
) < 1)
3854 update_window(struct ws_win
*win
)
3859 bzero(&wc
, sizeof wc
);
3860 mask
= CWBorderWidth
| CWWidth
| CWHeight
| CWX
| CWY
;
3861 wc
.border_width
= border_width
;
3864 wc
.width
= WIDTH(win
);
3865 wc
.height
= HEIGHT(win
);
3867 DNPRINTF(SWM_D_MISC
, "update_window: window: 0x%lx, (x,y) w x h: "
3868 "(%d,%d) %d x %d\n", win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
3870 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3873 #define SWM_RESIZE_STEPS (50)
3876 resize(struct ws_win
*win
, union arg
*args
)
3880 struct swm_region
*r
= NULL
;
3881 int resize_step
= 0;
3885 struct swm_geometry g
;
3886 int top
= 0, left
= 0;
3889 unsigned int shape
; /* cursor style */
3895 DNPRINTF(SWM_D_MOUSE
, "resize: window: 0x%lx, floating: %s, "
3896 "transient: 0x%lx\n", win
->id
, YESNO(win
->floating
),
3899 if (!(win
->transient
!= 0 || win
->floating
!= 0))
3902 /* reject resizes in max mode for floaters (transient ok) */
3903 if (win
->floatmaxed
)
3907 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
3913 case SWM_ARG_ID_WIDTHSHRINK
:
3914 WIDTH(win
) -= SWM_RESIZE_STEPS
;
3917 case SWM_ARG_ID_WIDTHGROW
:
3918 WIDTH(win
) += SWM_RESIZE_STEPS
;
3921 case SWM_ARG_ID_HEIGHTSHRINK
:
3922 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
3925 case SWM_ARG_ID_HEIGHTGROW
:
3926 HEIGHT(win
) += SWM_RESIZE_STEPS
;
3933 constrain_window(win
, r
, 1);
3935 store_float_geom(win
,r
);
3939 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3940 drain_enter_notify();
3942 /* get cursor offset from window root */
3943 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
3948 if (wx
< WIDTH(win
) / 2)
3951 if (wy
< HEIGHT(win
) / 2)
3954 if (args
->id
== SWM_ARG_ID_CENTER
)
3957 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
3959 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
3961 cursor
= XCreateFontCursor(display
, shape
);
3963 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
3964 GrabModeAsync
, None
, cursor
, CurrentTime
) != GrabSuccess
) {
3965 XFreeCursor(display
, cursor
);
3970 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
3971 SubstructureRedirectMask
, &ev
);
3973 case ConfigureRequest
:
3976 handler
[ev
.type
](&ev
);
3979 /* cursor offset/delta from start of the operation */
3980 dx
= ev
.xmotion
.x_root
- x
;
3981 dy
= ev
.xmotion
.y_root
- y
;
3987 if (args
->id
== SWM_ARG_ID_CENTER
) {
3988 if (g
.h
/ 2 + dy
< 1)
3992 HEIGHT(win
) = g
.h
+ 2 * dy
;
4000 HEIGHT(win
) = g
.h
+ dy
;
4007 if (args
->id
== SWM_ARG_ID_CENTER
) {
4008 if (g
.w
/ 2 + dx
< 1)
4012 WIDTH(win
) = g
.w
+ 2 * dx
;
4020 WIDTH(win
) = g
.w
+ dx
;
4023 constrain_window(win
, r
, 1);
4025 /* not free, don't sync more than 120 times / second */
4026 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4027 time
= ev
.xmotion
.time
;
4028 XSync(display
, False
);
4033 } while (ev
.type
!= ButtonRelease
);
4035 XSync(display
, False
);
4038 store_float_geom(win
,r
);
4040 XUngrabPointer(display
, CurrentTime
);
4041 XFreeCursor(display
, cursor
);
4044 drain_enter_notify();
4048 resize_step(struct swm_region
*r
, union arg
*args
)
4050 struct ws_win
*win
= NULL
;
4052 if (r
&& r
->ws
&& r
->ws
->focus
)
4060 #define SWM_MOVE_STEPS (50)
4063 move(struct ws_win
*win
, union arg
*args
)
4068 struct swm_region
*r
= NULL
;
4078 DNPRINTF(SWM_D_MOUSE
, "move: window: 0x%lx, floating: %s, transient: "
4079 "0x%lx\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4081 /* in max_stack mode should only move transients */
4082 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4086 if (win
->floating
== 0 && !win
->transient
) {
4087 store_float_geom(win
,r
);
4088 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4091 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4098 case SWM_ARG_ID_MOVELEFT
:
4099 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4102 case SWM_ARG_ID_MOVERIGHT
:
4103 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4106 case SWM_ARG_ID_MOVEUP
:
4107 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4110 case SWM_ARG_ID_MOVEDOWN
:
4111 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4118 constrain_window(win
, r
, 0);
4120 store_float_geom(win
, r
);
4124 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4125 GrabModeAsync
, None
, XCreateFontCursor(display
, XC_fleur
),
4126 CurrentTime
) != GrabSuccess
)
4129 /* get cursor offset from window root */
4130 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4134 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4135 SubstructureRedirectMask
, &ev
);
4137 case ConfigureRequest
:
4140 handler
[ev
.type
](&ev
);
4143 X(win
) = ev
.xmotion
.x_root
- wx
- border_width
;
4144 Y(win
) = ev
.xmotion
.y_root
- wy
- border_width
;
4146 constrain_window(win
, r
, 0);
4148 /* not free, don't sync more than 120 times / second */
4149 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4150 time
= ev
.xmotion
.time
;
4151 XSync(display
, False
);
4156 } while (ev
.type
!= ButtonRelease
);
4158 XSync(display
, False
);
4161 store_float_geom(win
,r
);
4162 XUngrabPointer(display
, CurrentTime
);
4165 drain_enter_notify();
4169 move_step(struct swm_region
*r
, union arg
*args
)
4171 struct ws_win
*win
= NULL
;
4173 if (r
&& r
->ws
&& r
->ws
->focus
)
4178 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4185 /* user/key callable function IDs */
4280 kf_search_workspace
,
4282 kf_dumpwins
, /* MUST BE LAST */
4286 /* key definitions */
4288 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4293 legacyfunc(struct swm_region
*r
, union arg
*args
)
4298 char name
[SWM_FUNCNAME_LEN
];
4299 void (*func
)(struct swm_region
*r
, union arg
*);
4301 } keyfuncs
[kf_invalid
+ 1] = {
4302 /* name function argument */
4303 { "cycle_layout", cycle_layout
, {0} },
4304 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
4305 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4306 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4307 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4308 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4309 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4310 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4311 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4312 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4313 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4314 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4315 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4316 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4317 { "spawn_term", spawnterm
, {.argv
= spawn_term
} },
4318 { "spawn_menu", legacyfunc
, {0} },
4319 { "quit", quit
, {0} },
4320 { "restart", restart
, {0} },
4321 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4322 { "ws_1", switchws
, {.id
= 0} },
4323 { "ws_2", switchws
, {.id
= 1} },
4324 { "ws_3", switchws
, {.id
= 2} },
4325 { "ws_4", switchws
, {.id
= 3} },
4326 { "ws_5", switchws
, {.id
= 4} },
4327 { "ws_6", switchws
, {.id
= 5} },
4328 { "ws_7", switchws
, {.id
= 6} },
4329 { "ws_8", switchws
, {.id
= 7} },
4330 { "ws_9", switchws
, {.id
= 8} },
4331 { "ws_10", switchws
, {.id
= 9} },
4332 { "ws_11", switchws
, {.id
= 10} },
4333 { "ws_12", switchws
, {.id
= 11} },
4334 { "ws_13", switchws
, {.id
= 12} },
4335 { "ws_14", switchws
, {.id
= 13} },
4336 { "ws_15", switchws
, {.id
= 14} },
4337 { "ws_16", switchws
, {.id
= 15} },
4338 { "ws_17", switchws
, {.id
= 16} },
4339 { "ws_18", switchws
, {.id
= 17} },
4340 { "ws_19", switchws
, {.id
= 18} },
4341 { "ws_20", switchws
, {.id
= 19} },
4342 { "ws_21", switchws
, {.id
= 20} },
4343 { "ws_22", switchws
, {.id
= 21} },
4344 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4345 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4346 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4347 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4348 { "ws_prior", priorws
, {0} },
4349 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4350 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4351 { "mvws_1", send_to_ws
, {.id
= 0} },
4352 { "mvws_2", send_to_ws
, {.id
= 1} },
4353 { "mvws_3", send_to_ws
, {.id
= 2} },
4354 { "mvws_4", send_to_ws
, {.id
= 3} },
4355 { "mvws_5", send_to_ws
, {.id
= 4} },
4356 { "mvws_6", send_to_ws
, {.id
= 5} },
4357 { "mvws_7", send_to_ws
, {.id
= 6} },
4358 { "mvws_8", send_to_ws
, {.id
= 7} },
4359 { "mvws_9", send_to_ws
, {.id
= 8} },
4360 { "mvws_10", send_to_ws
, {.id
= 9} },
4361 { "mvws_11", send_to_ws
, {.id
= 10} },
4362 { "mvws_12", send_to_ws
, {.id
= 11} },
4363 { "mvws_13", send_to_ws
, {.id
= 12} },
4364 { "mvws_14", send_to_ws
, {.id
= 13} },
4365 { "mvws_15", send_to_ws
, {.id
= 14} },
4366 { "mvws_16", send_to_ws
, {.id
= 15} },
4367 { "mvws_17", send_to_ws
, {.id
= 16} },
4368 { "mvws_18", send_to_ws
, {.id
= 17} },
4369 { "mvws_19", send_to_ws
, {.id
= 18} },
4370 { "mvws_20", send_to_ws
, {.id
= 19} },
4371 { "mvws_21", send_to_ws
, {.id
= 20} },
4372 { "mvws_22", send_to_ws
, {.id
= 21} },
4373 { "bar_toggle", bar_toggle
, {0} },
4374 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4375 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4376 { "screenshot_all", legacyfunc
, {0} },
4377 { "screenshot_wind", legacyfunc
, {0} },
4378 { "float_toggle", floating_toggle
,{0} },
4379 { "version", version
, {0} },
4380 { "spawn_lock", legacyfunc
, {0} },
4381 { "spawn_initscr", legacyfunc
, {0} },
4382 { "spawn_custom", dummykeyfunc
, {0} },
4383 { "iconify", iconify
, {0} },
4384 { "uniconify", uniconify
, {0} },
4385 { "raise_toggle", raise_toggle
, {0} },
4386 { "button2", pressbutton
, {2} },
4387 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4388 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4389 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4390 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4391 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4392 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4393 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4394 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4395 { "name_workspace", name_workspace
, {0} },
4396 { "search_workspace", search_workspace
, {0} },
4397 { "search_win", search_win
, {0} },
4398 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4399 { "invalid key func", NULL
, {0} },
4402 RB_ENTRY(key
) entry
;
4405 enum keyfuncid funcid
;
4408 RB_HEAD(key_list
, key
);
4411 key_cmp(struct key
*kp1
, struct key
*kp2
)
4413 if (kp1
->keysym
< kp2
->keysym
)
4415 if (kp1
->keysym
> kp2
->keysym
)
4418 if (kp1
->mod
< kp2
->mod
)
4420 if (kp1
->mod
> kp2
->mod
)
4426 RB_GENERATE_STATIC(key_list
, key
, entry
, key_cmp
);
4427 struct key_list keys
;
4430 enum { client_click
, root_click
};
4432 unsigned int action
;
4434 unsigned int button
;
4435 void (*func
)(struct ws_win
*, union arg
*);
4438 /* action key mouse button func args */
4439 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4440 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4441 { client_click
, MODKEY
, Button1
, move
, {0} },
4445 update_modkey(unsigned int mod
)
4451 RB_FOREACH(kp
, key_list
, &keys
)
4452 if (kp
->mod
& ShiftMask
)
4453 kp
->mod
= mod
| ShiftMask
;
4457 for (i
= 0; i
< LENGTH(buttons
); i
++)
4458 if (buttons
[i
].mask
& ShiftMask
)
4459 buttons
[i
].mask
= mod
| ShiftMask
;
4461 buttons
[i
].mask
= mod
;
4466 TAILQ_ENTRY(spawn_prog
) entry
;
4471 TAILQ_HEAD(spawn_list
, spawn_prog
);
4472 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
4475 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4478 struct spawn_prog
*prog
= NULL
;
4480 char *ap
, **real_args
;
4482 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4485 TAILQ_FOREACH(prog
, &spawns
, entry
) {
4486 if (!strcasecmp(spawn_name
, prog
->name
))
4490 warnx("spawn_custom: program %s not found", spawn_name
);
4494 /* make room for expanded args */
4495 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4496 err(1, "spawn_custom: calloc real_args");
4498 /* expand spawn_args into real_args */
4499 for (i
= 0; i
< prog
->argc
; i
++) {
4501 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4502 if (!strcasecmp(ap
, "$bar_border")) {
4504 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4506 err(1, "spawn_custom border color");
4507 } else if (!strcasecmp(ap
, "$bar_color")) {
4509 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4511 err(1, "spawn_custom bar color");
4512 } else if (!strcasecmp(ap
, "$bar_font")) {
4513 if ((real_args
[i
] = strdup(bar_fonts
))
4515 err(1, "spawn_custom bar fonts");
4516 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4518 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4520 err(1, "spawn_custom color font");
4521 } else if (!strcasecmp(ap
, "$color_focus")) {
4523 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4525 err(1, "spawn_custom color focus");
4526 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4528 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4530 err(1, "spawn_custom color unfocus");
4532 /* no match --> copy as is */
4533 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4534 err(1, "spawn_custom strdup(ap)");
4536 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4541 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4542 for (i
= 0; i
< prog
->argc
; i
++)
4543 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4544 DNPRINTF(SWM_D_SPAWN
, "\n");
4546 *ret_args
= real_args
;
4547 return (prog
->argc
);
4551 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4557 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4561 spawn(r
->ws
->idx
, &a
, 1);
4563 for (i
= 0; i
< spawn_argc
; i
++)
4569 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4575 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4579 if (pipe(select_list_pipe
) == -1)
4580 err(1, "pipe error");
4581 if (pipe(select_resp_pipe
) == -1)
4582 err(1, "pipe error");
4584 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4585 err(1, "could not disable SIGPIPE");
4586 switch (*pid
= fork()) {
4588 err(1, "cannot fork");
4591 if (dup2(select_list_pipe
[0], 0) == -1)
4593 if (dup2(select_resp_pipe
[1], 1) == -1)
4595 close(select_list_pipe
[1]);
4596 close(select_resp_pipe
[0]);
4597 spawn(r
->ws
->idx
, &a
, 0);
4599 default: /* parent */
4600 close(select_list_pipe
[0]);
4601 close(select_resp_pipe
[1]);
4605 for (i
= 0; i
< spawn_argc
; i
++)
4611 spawn_insert(char *name
, char *args
)
4613 char *arg
, *cp
, *ptr
;
4614 struct spawn_prog
*sp
;
4616 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s\n", name
);
4618 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
4619 err(1, "spawn_insert: malloc");
4620 if ((sp
->name
= strdup(name
)) == NULL
)
4621 err(1, "spawn_insert: strdup");
4623 /* convert the arguments to an argument list */
4624 if ((ptr
= cp
= strdup(args
)) == NULL
)
4625 err(1, "spawn_insert: strdup");
4626 while ((arg
= strsep(&ptr
, " \t")) != NULL
) {
4627 /* empty field; skip it */
4632 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
4633 sizeof *sp
->argv
)) == NULL
)
4634 err(1, "spawn_insert: realloc");
4635 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
4636 err(1, "spawn_insert: strdup");
4640 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
4641 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
4645 spawn_remove(struct spawn_prog
*sp
)
4649 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
4651 TAILQ_REMOVE(&spawns
, sp
, entry
);
4652 for (i
= 0; i
< sp
->argc
; i
++)
4658 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
4662 spawn_replace(struct spawn_prog
*sp
, char *name
, char *args
)
4664 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: %s [%s]\n", sp
->name
, name
);
4667 spawn_insert(name
, args
);
4669 DNPRINTF(SWM_D_SPAWN
, "spawn_replace: leave\n");
4673 setspawn(char *name
, char *args
)
4675 struct spawn_prog
*sp
;
4677 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
4682 TAILQ_FOREACH(sp
, &spawns
, entry
) {
4683 if (!strcmp(sp
->name
, name
)) {
4687 spawn_replace(sp
, name
, args
);
4688 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
4692 if (*args
== '\0') {
4693 warnx("error: setspawn: cannot find program: %s", name
);
4697 spawn_insert(name
, args
);
4698 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
4702 setconfspawn(char *selector
, char *value
, int flags
)
4704 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
4706 setspawn(selector
, value
);
4708 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
4715 setconfspawn("term", "xterm", 0);
4716 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4717 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4718 setconfspawn("lock", "xlock", 0);
4719 setconfspawn("initscr", "initscreen.sh", 0);
4720 setconfspawn("menu", "dmenu_run"
4723 " -nf $bar_font_color"
4725 " -sf $bar_color", 0);
4726 setconfspawn("search", "dmenu"
4730 " -nf $bar_font_color"
4732 " -sf $bar_color", 0);
4733 setconfspawn("name_workspace", "dmenu"
4737 " -nf $bar_font_color"
4739 " -sf $bar_color", 0);
4743 #define SWM_MODNAME_SIZE 32
4744 #define SWM_KEY_WS "\n+ \t"
4746 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
4750 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
4751 if (mod
== NULL
|| ks
== NULL
) {
4752 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
4755 if (keystr
== NULL
|| strlen(keystr
) == 0) {
4756 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
4762 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
4763 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
4765 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
4766 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
4768 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
4770 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
4772 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
4774 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
4776 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
4778 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
4779 *mod
|= ControlMask
;
4781 *ks
= XStringToKeysym(name
);
4782 XConvertCase(*ks
, ks
, &uks
);
4783 if (ks
== NoSymbol
) {
4785 "parsekeys: invalid key %s\n",
4791 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
4796 strdupsafe(char *str
)
4801 return (strdup(str
));
4805 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
4809 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
4810 keyfuncs
[kfid
].name
, spawn_name
);
4812 if ((kp
= malloc(sizeof *kp
)) == NULL
)
4813 err(1, "key_insert: malloc");
4818 kp
->spawn_name
= strdupsafe(spawn_name
);
4819 RB_INSERT(key_list
, &keys
, kp
);
4821 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
4825 key_lookup(unsigned int mod
, KeySym ks
)
4832 return (RB_FIND(key_list
, &keys
, &kp
));
4836 key_remove(struct key
*kp
)
4838 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
4840 RB_REMOVE(key_list
, &keys
, kp
);
4841 free(kp
->spawn_name
);
4844 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
4848 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
4851 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
4855 key_insert(mod
, ks
, kfid
, spawn_name
);
4857 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
4861 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
4866 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
4867 keyfuncs
[kfid
].name
, spawn_name
);
4869 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
4870 if (kfid
== kf_invalid
)
4873 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
4874 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4877 if (kfid
== kf_invalid
) {
4878 warnx("error: setkeybinding: cannot find mod/key combination");
4879 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4883 key_insert(mod
, ks
, kfid
, spawn_name
);
4884 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4888 setconfbinding(char *selector
, char *value
, int flags
)
4890 enum keyfuncid kfid
;
4893 struct spawn_prog
*sp
;
4894 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
4895 if (selector
== NULL
) {
4896 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
4897 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4899 setkeybinding(mod
, ks
, kfid
, NULL
);
4904 /* search by key function name */
4905 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
4906 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
4907 SWM_FUNCNAME_LEN
) == 0) {
4908 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4910 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4911 setkeybinding(mod
, ks
, kfid
, NULL
);
4917 /* search by custom spawn name */
4918 TAILQ_FOREACH(sp
, &spawns
, entry
) {
4919 if (strcasecmp(selector
, sp
->name
) == 0) {
4920 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4922 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4923 setkeybinding(mod
, ks
, kf_spawn_custom
,
4930 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
4937 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
4938 setkeybinding(MODKEY
|ShiftMask
, XK_backslash
, kf_flip_layout
, NULL
);
4939 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
4940 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
4941 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
4942 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
4943 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
4944 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
4945 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
4946 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
4947 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
4948 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
4949 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
4950 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
4951 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_term
, NULL
);
4952 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
4953 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
4954 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
4955 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
4956 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
4957 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
4958 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
4959 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
4960 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
4961 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
4962 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
4963 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
4964 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
4965 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
4966 setkeybinding(MODKEY
, XK_F1
, kf_ws_11
, NULL
);
4967 setkeybinding(MODKEY
, XK_F2
, kf_ws_12
, NULL
);
4968 setkeybinding(MODKEY
, XK_F3
, kf_ws_13
, NULL
);
4969 setkeybinding(MODKEY
, XK_F4
, kf_ws_14
, NULL
);
4970 setkeybinding(MODKEY
, XK_F5
, kf_ws_15
, NULL
);
4971 setkeybinding(MODKEY
, XK_F6
, kf_ws_16
, NULL
);
4972 setkeybinding(MODKEY
, XK_F7
, kf_ws_17
, NULL
);
4973 setkeybinding(MODKEY
, XK_F8
, kf_ws_18
, NULL
);
4974 setkeybinding(MODKEY
, XK_F9
, kf_ws_19
, NULL
);
4975 setkeybinding(MODKEY
, XK_F10
, kf_ws_20
, NULL
);
4976 setkeybinding(MODKEY
, XK_F11
, kf_ws_21
, NULL
);
4977 setkeybinding(MODKEY
, XK_F12
, kf_ws_22
, NULL
);
4978 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
4979 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
4980 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
4981 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
4982 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
4983 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
4984 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
4985 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
4986 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
4987 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
4988 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
4989 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
4990 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
4991 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
4992 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
4993 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
4994 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
4995 setkeybinding(MODKEY
|ShiftMask
, XK_F1
, kf_mvws_11
, NULL
);
4996 setkeybinding(MODKEY
|ShiftMask
, XK_F2
, kf_mvws_12
, NULL
);
4997 setkeybinding(MODKEY
|ShiftMask
, XK_F3
, kf_mvws_13
, NULL
);
4998 setkeybinding(MODKEY
|ShiftMask
, XK_F4
, kf_mvws_14
, NULL
);
4999 setkeybinding(MODKEY
|ShiftMask
, XK_F5
, kf_mvws_15
, NULL
);
5000 setkeybinding(MODKEY
|ShiftMask
, XK_F6
, kf_mvws_16
, NULL
);
5001 setkeybinding(MODKEY
|ShiftMask
, XK_F7
, kf_mvws_17
, NULL
);
5002 setkeybinding(MODKEY
|ShiftMask
, XK_F8
, kf_mvws_18
, NULL
);
5003 setkeybinding(MODKEY
|ShiftMask
, XK_F9
, kf_mvws_19
, NULL
);
5004 setkeybinding(MODKEY
|ShiftMask
, XK_F10
, kf_mvws_20
, NULL
);
5005 setkeybinding(MODKEY
|ShiftMask
, XK_F11
, kf_mvws_21
, NULL
);
5006 setkeybinding(MODKEY
|ShiftMask
, XK_F12
, kf_mvws_22
, NULL
);
5007 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
5008 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
5009 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
5010 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
5011 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
5012 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
5013 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
5014 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
5015 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
5016 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
5017 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
5018 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
5019 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
5020 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
5021 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
5022 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
5023 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
5024 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
5025 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
5026 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
5027 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
5028 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
5029 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
5030 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
5031 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
5032 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
5034 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
5043 while (RB_EMPTY(&keys
) == 0) {
5044 kp
= RB_ROOT(&keys
);
5050 setkeymapping(char *selector
, char *value
, int flags
)
5052 char keymapping_file
[PATH_MAX
];
5053 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
5054 if (value
[0] == '~')
5055 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
5056 pwd
->pw_dir
, &value
[1]);
5058 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
5060 /* load new key bindings; if it fails, revert to default bindings */
5061 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
5065 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
5070 updatenumlockmask(void)
5073 XModifierKeymap
*modmap
;
5075 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
5077 modmap
= XGetModifierMapping(display
);
5078 for (i
= 0; i
< 8; i
++)
5079 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
5080 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
5081 == XKeysymToKeycode(display
, XK_Num_Lock
))
5082 numlockmask
= (1 << i
);
5084 XFreeModifiermap(modmap
);
5092 unsigned int modifiers
[] =
5093 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
5096 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
5097 updatenumlockmask();
5099 for (k
= 0; k
< ScreenCount(display
); k
++) {
5100 if (TAILQ_EMPTY(&screens
[k
].rl
))
5102 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
5103 RB_FOREACH(kp
, key_list
, &keys
) {
5104 if ((code
= XKeysymToKeycode(display
, kp
->keysym
)))
5105 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5106 XGrabKey(display
, code
,
5107 kp
->mod
| modifiers
[j
],
5108 screens
[k
].root
, True
,
5109 GrabModeAsync
, GrabModeAsync
);
5115 grabbuttons(struct ws_win
*win
, int focused
)
5118 unsigned int modifiers
[] =
5119 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
5121 updatenumlockmask();
5122 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
5124 for (i
= 0; i
< LENGTH(buttons
); i
++)
5125 if (buttons
[i
].action
== client_click
)
5126 for (j
= 0; j
< LENGTH(modifiers
); j
++)
5127 XGrabButton(display
, buttons
[i
].button
,
5128 buttons
[i
].mask
| modifiers
[j
],
5129 win
->id
, False
, BUTTONMASK
,
5130 GrabModeAsync
, GrabModeSync
, None
,
5133 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
5134 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
5137 const char *quirkname
[] = {
5138 "NONE", /* config string for "no value" */
5147 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5148 #define SWM_Q_WS "\n|+ \t"
5150 parsequirks(char *qstr
, unsigned long *quirk
)
5160 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5162 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5163 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5164 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5165 DNPRINTF(SWM_D_QUIRK
,
5166 "parsequirks: %s\n", name
);
5171 *quirk
|= 1 << (i
-1);
5175 if (i
>= LENGTH(quirkname
)) {
5176 DNPRINTF(SWM_D_QUIRK
,
5177 "parsequirks: invalid quirk [%s]\n", name
);
5185 quirk_insert(const char *class, const char *name
, unsigned long quirk
)
5189 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: %s:%s [%lu]\n", class, name
,
5192 if ((qp
= malloc(sizeof *qp
)) == NULL
)
5193 err(1, "quirk_insert: malloc");
5194 if ((qp
->class = strdup(class)) == NULL
)
5195 err(1, "quirk_insert: strdup");
5196 if ((qp
->name
= strdup(name
)) == NULL
)
5197 err(1, "quirk_insert: strdup");
5200 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
5202 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
5206 quirk_remove(struct quirk
*qp
)
5208 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
5209 qp
->name
, qp
->quirk
);
5211 TAILQ_REMOVE(&quirks
, qp
, entry
);
5216 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
5220 quirk_replace(struct quirk
*qp
, const char *class, const char *name
,
5221 unsigned long quirk
)
5223 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s [%lu]\n", qp
->class,
5224 qp
->name
, qp
->quirk
);
5227 quirk_insert(class, name
, quirk
);
5229 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
5233 setquirk(const char *class, const char *name
, unsigned long quirk
)
5237 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s [%lu]\n", class, name
,
5240 TAILQ_FOREACH(qp
, &quirks
, entry
) {
5241 if (!strcmp(qp
->class, class) && !strcmp(qp
->name
, name
)) {
5245 quirk_replace(qp
, class, name
, quirk
);
5246 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5251 warnx("error: setquirk: cannot find class/name combination");
5255 quirk_insert(class, name
, quirk
);
5256 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
5260 setconfquirk(char *selector
, char *value
, int flags
)
5262 char *cp
, *class, *name
;
5264 unsigned long quirks
;
5265 if (selector
== NULL
)
5267 if ((cp
= strchr(selector
, ':')) == NULL
)
5272 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5273 setquirk(class, name
, quirks
);
5280 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5281 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5282 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5283 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5284 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5285 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5286 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5287 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5288 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5289 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5290 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5291 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5292 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5295 /* conf file stuff */
5296 #define SWM_CONF_FILE "spectrwm.conf"
5297 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5299 enum { SWM_S_BAR_DELAY
, SWM_S_BAR_ENABLED
, SWM_S_BAR_BORDER_WIDTH
,
5300 SWM_S_STACK_ENABLED
, SWM_S_CLOCK_ENABLED
, SWM_S_CLOCK_FORMAT
,
5301 SWM_S_CYCLE_EMPTY
, SWM_S_CYCLE_VISIBLE
, SWM_S_WORKSPACE_LIMIT
,
5302 SWM_S_SS_ENABLED
, SWM_S_TERM_WIDTH
, SWM_S_TITLE_CLASS_ENABLED
,
5303 SWM_S_TITLE_NAME_ENABLED
, SWM_S_WINDOW_NAME_ENABLED
,
5304 SWM_S_URGENT_ENABLED
, SWM_S_FOCUS_MODE
, SWM_S_FOCUS_CLOSE
,
5305 SWM_S_FOCUS_CLOSE_WRAP
, SWM_S_FOCUS_DEFAULT
, SWM_S_SPAWN_ORDER
,
5306 SWM_S_DISABLE_BORDER
, SWM_S_BORDER_WIDTH
, SWM_S_BAR_FONT
,
5307 SWM_S_BAR_ACTION
, SWM_S_SPAWN_TERM
, SWM_S_SS_APP
, SWM_S_DIALOG_RATIO
,
5308 SWM_S_BAR_AT_BOTTOM
, SWM_S_VERBOSE_LAYOUT
, SWM_S_BAR_JUSTIFY
5312 setconfvalue(char *selector
, char *value
, int flags
)
5318 case SWM_S_BAR_DELAY
:
5319 bar_delay
= atoi(value
);
5321 case SWM_S_BAR_ENABLED
:
5322 bar_enabled
= atoi(value
);
5324 case SWM_S_BAR_BORDER_WIDTH
:
5325 bar_border_width
= atoi(value
);
5327 case SWM_S_BAR_AT_BOTTOM
:
5328 bar_at_bottom
= atoi(value
);
5330 case SWM_S_BAR_JUSTIFY
:
5331 if (!strcmp(value
, "left"))
5332 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5333 else if (!strcmp(value
, "center"))
5334 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5335 else if (!strcmp(value
, "right"))
5336 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5338 errx(1, "invalid bar_justify");
5340 case SWM_S_STACK_ENABLED
:
5341 stack_enabled
= atoi(value
);
5343 case SWM_S_CLOCK_ENABLED
:
5344 clock_enabled
= atoi(value
);
5346 case SWM_S_CLOCK_FORMAT
:
5347 #ifndef SWM_DENY_CLOCK_FORMAT
5349 if ((clock_format
= strdup(value
)) == NULL
)
5350 err(1, "setconfvalue: clock_format");
5353 case SWM_S_CYCLE_EMPTY
:
5354 cycle_empty
= atoi(value
);
5356 case SWM_S_CYCLE_VISIBLE
:
5357 cycle_visible
= atoi(value
);
5359 case SWM_S_WORKSPACE_LIMIT
:
5360 workspace_limit
= atoi(value
);
5361 if (workspace_limit
> SWM_WS_MAX
)
5362 workspace_limit
= SWM_WS_MAX
;
5363 else if (workspace_limit
< 1)
5364 workspace_limit
= 1;
5366 case SWM_S_SS_ENABLED
:
5367 ss_enabled
= atoi(value
);
5369 case SWM_S_TERM_WIDTH
:
5370 term_width
= atoi(value
);
5372 case SWM_S_TITLE_CLASS_ENABLED
:
5373 title_class_enabled
= atoi(value
);
5375 case SWM_S_WINDOW_NAME_ENABLED
:
5376 window_name_enabled
= atoi(value
);
5378 case SWM_S_TITLE_NAME_ENABLED
:
5379 title_name_enabled
= atoi(value
);
5381 case SWM_S_URGENT_ENABLED
:
5382 urgent_enabled
= atoi(value
);
5384 case SWM_S_FOCUS_MODE
:
5385 if (!strcmp(value
, "default"))
5386 focus_mode
= SWM_FOCUS_DEFAULT
;
5387 else if (!strcmp(value
, "follow_cursor"))
5388 focus_mode
= SWM_FOCUS_FOLLOW
;
5389 else if (!strcmp(value
, "synergy"))
5390 focus_mode
= SWM_FOCUS_SYNERGY
;
5392 errx(1, "focus_mode");
5394 case SWM_S_FOCUS_CLOSE
:
5395 if (!strcmp(value
, "first"))
5396 focus_close
= SWM_STACK_BOTTOM
;
5397 else if (!strcmp(value
, "last"))
5398 focus_close
= SWM_STACK_TOP
;
5399 else if (!strcmp(value
, "next"))
5400 focus_close
= SWM_STACK_ABOVE
;
5401 else if (!strcmp(value
, "previous"))
5402 focus_close
= SWM_STACK_BELOW
;
5404 errx(1, "focus_close");
5406 case SWM_S_FOCUS_CLOSE_WRAP
:
5407 focus_close_wrap
= atoi(value
);
5409 case SWM_S_FOCUS_DEFAULT
:
5410 if (!strcmp(value
, "last"))
5411 focus_default
= SWM_STACK_TOP
;
5412 else if (!strcmp(value
, "first"))
5413 focus_default
= SWM_STACK_BOTTOM
;
5415 errx(1, "focus_default");
5417 case SWM_S_SPAWN_ORDER
:
5418 if (!strcmp(value
, "first"))
5419 spawn_position
= SWM_STACK_BOTTOM
;
5420 else if (!strcmp(value
, "last"))
5421 spawn_position
= SWM_STACK_TOP
;
5422 else if (!strcmp(value
, "next"))
5423 spawn_position
= SWM_STACK_ABOVE
;
5424 else if (!strcmp(value
, "previous"))
5425 spawn_position
= SWM_STACK_BELOW
;
5427 errx(1, "spawn_position");
5429 case SWM_S_DISABLE_BORDER
:
5430 disable_border
= atoi(value
);
5432 case SWM_S_BORDER_WIDTH
:
5433 border_width
= atoi(value
);
5435 case SWM_S_BAR_FONT
:
5437 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5438 err(1, "setconfvalue: asprintf: failed to allocate "
5439 "memory for bar_fonts.");
5443 case SWM_S_BAR_ACTION
:
5445 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5446 err(1, "setconfvalue: bar_action");
5448 case SWM_S_SPAWN_TERM
:
5449 free(spawn_term
[0]);
5450 if ((spawn_term
[0] = strdup(value
)) == NULL
)
5451 err(1, "setconfvalue: spawn_term");
5455 case SWM_S_DIALOG_RATIO
:
5456 dialog_ratio
= atof(value
);
5457 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5460 case SWM_S_VERBOSE_LAYOUT
:
5461 verbose_layout
= atoi(value
);
5462 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5464 layouts
[i
].l_string
= fancy_stacker
;
5466 layouts
[i
].l_string
= plain_stacker
;
5476 setconfmodkey(char *selector
, char *value
, int flags
)
5478 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5479 update_modkey(Mod1Mask
);
5480 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5481 update_modkey(Mod2Mask
);
5482 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5483 update_modkey(Mod3Mask
);
5484 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5485 update_modkey(Mod4Mask
);
5492 setconfcolor(char *selector
, char *value
, int flags
)
5494 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5499 setconfregion(char *selector
, char *value
, int flags
)
5501 custom_region(value
);
5506 setautorun(char *selector
, char *value
, int flags
)
5516 if (getenv("SWM_STARTED"))
5520 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5521 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5523 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5524 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5527 * This is a little intricate
5529 * If the pid already exists we simply reuse it because it means it was
5530 * used before AND not claimed by manage_window. We get away with
5531 * altering it in the parent after INSERT because this can not be a race
5534 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5537 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5539 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5540 err(1, "setautorun: realloc");
5541 a
.argv
[argc
- 1] = ap
;
5544 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5545 err(1, "setautorun: realloc");
5546 a
.argv
[argc
] = NULL
;
5548 if ((pid
= fork()) == 0) {
5549 spawn(ws_id
, &a
, 1);
5558 p
= calloc(1, sizeof *p
);
5561 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
5571 setlayout(char *selector
, char *value
, int flags
)
5573 int ws_id
, i
, x
, mg
, ma
, si
, raise
;
5574 int st
= SWM_V_STACK
;
5576 struct workspace
*ws
;
5578 if (getenv("SWM_STARTED"))
5582 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
5583 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
5584 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5585 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5588 if (ws_id
< 0 || ws_id
>= workspace_limit
)
5589 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
5591 if (!strcasecmp(s
, "vertical"))
5593 else if (!strcasecmp(s
, "horizontal"))
5595 else if (!strcasecmp(s
, "fullscreen"))
5598 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5599 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5602 for (i
= 0; i
< ScreenCount(display
); i
++) {
5603 ws
= (struct workspace
*)&screens
[i
].ws
;
5604 ws
[ws_id
].cur_layout
= &layouts
[st
];
5606 ws
[ws_id
].always_raise
= raise
;
5607 if (st
== SWM_MAX_STACK
)
5611 for (x
= 0; x
< abs(mg
); x
++) {
5612 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5613 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
5614 SWM_ARG_ID_MASTERSHRINK
);
5618 for (x
= 0; x
< abs(ma
); x
++) {
5619 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5620 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
5621 SWM_ARG_ID_MASTERDEL
);
5625 for (x
= 0; x
< abs(si
); x
++) {
5626 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5627 si
>= 0 ? SWM_ARG_ID_STACKINC
:
5628 SWM_ARG_ID_STACKDEC
);
5636 /* config options */
5637 struct config_option
{
5639 int (*func
)(char*, char*, int);
5642 struct config_option configopt
[] = {
5643 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
5644 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
5645 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
5646 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
5647 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
5648 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
5649 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
5650 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
5651 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
5652 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
5653 { "keyboard_mapping", setkeymapping
, 0 },
5654 { "bind", setconfbinding
, 0 },
5655 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
5656 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
5657 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
5658 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
5659 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
5660 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
5661 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
5662 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
5663 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
5664 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
5665 { "modkey", setconfmodkey
, 0 },
5666 { "program", setconfspawn
, 0 },
5667 { "quirk", setconfquirk
, 0 },
5668 { "region", setconfregion
, 0 },
5669 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
5670 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
5671 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
5672 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
5673 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
5674 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
5675 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
5676 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
5677 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
5678 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
5679 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
5680 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
5681 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
5682 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
5683 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
5684 { "autorun", setautorun
, 0 },
5685 { "layout", setlayout
, 0 },
5690 conf_load(char *filename
, int keymapping
)
5693 char *line
, *cp
, *optsub
, *optval
;
5694 size_t linelen
, lineno
= 0;
5695 int wordlen
, i
, optind
;
5696 struct config_option
*opt
;
5698 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
5700 if (filename
== NULL
) {
5701 warnx("conf_load: no filename");
5704 if ((config
= fopen(filename
, "r")) == NULL
) {
5705 warn("conf_load: fopen: %s", filename
);
5709 while (!feof(config
)) {
5710 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
5713 err(1, "%s", filename
);
5718 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
5719 if (cp
[0] == '\0') {
5724 /* get config option */
5725 wordlen
= strcspn(cp
, "=[ \t\n");
5727 warnx("%s: line %zd: no option found",
5732 for (i
= 0; i
< LENGTH(configopt
); i
++) {
5733 opt
= &configopt
[i
];
5734 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
5735 strlen(opt
->optname
) == wordlen
) {
5741 warnx("%s: line %zd: unknown option %.*s",
5742 filename
, lineno
, wordlen
, cp
);
5745 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
5746 warnx("%s: line %zd: invalid option %.*s",
5747 filename
, lineno
, wordlen
, cp
);
5751 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
5752 /* get [selector] if any */
5756 wordlen
= strcspn(cp
, "]");
5759 warnx("%s: line %zd: syntax error",
5764 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
5766 warnx("%s: line %zd: unable to allocate"
5767 "memory for selector", filename
,
5773 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
5775 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
5777 optval
= strdup(cp
);
5778 /* call function to deal with it all */
5779 if (configopt
[optind
].func(optsub
, optval
,
5780 configopt
[optind
].funcflags
) != 0)
5781 errx(1, "%s: line %zd: invalid data for %s",
5782 filename
, lineno
, configopt
[optind
].optname
);
5789 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
5796 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
5802 set_child_transient(struct ws_win
*win
, Window
*trans
)
5804 struct ws_win
*parent
, *w
;
5805 XWMHints
*wmh
= NULL
;
5806 struct swm_region
*r
;
5807 struct workspace
*ws
;
5809 parent
= find_window(win
->transient
);
5811 parent
->child_trans
= win
;
5813 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
5814 " for 0x%lx trans 0x%lx\n", win
->id
, win
->transient
);
5816 if (win
->hints
== NULL
) {
5817 warnx("no hints for 0x%lx", win
->id
);
5821 r
= root_to_region(win
->wa
.root
);
5823 /* parent doen't exist in our window list */
5824 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5828 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
5829 warnx("can't get hints for 0x%lx", w
->id
);
5833 if (win
->hints
->window_group
!= wmh
->window_group
)
5836 w
->child_trans
= win
;
5837 win
->transient
= w
->id
;
5839 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
5840 "transient to 0x%lx\n", win
->transient
);
5850 window_get_pid(Window win
)
5852 Atom actual_type_return
;
5853 int actual_format_return
= 0;
5854 unsigned long nitems_return
= 0;
5855 unsigned long bytes_after_return
= 0;
5859 unsigned char *prop
= NULL
;
5861 if (XGetWindowProperty(display
, win
,
5862 XInternAtom(display
, "_NET_WM_PID", False
), 0, 1, False
,
5863 XA_CARDINAL
, &actual_type_return
, &actual_format_return
,
5864 &nitems_return
, &bytes_after_return
,
5865 (unsigned char**)(void*)&pid
) != Success
)
5867 if (actual_type_return
!= XA_CARDINAL
)
5878 if (XGetWindowProperty(display
, win
,
5879 XInternAtom(display
, "_SWM_PID", False
), 0, SWM_PROPLEN
, False
,
5880 XA_STRING
, &actual_type_return
, &actual_format_return
,
5881 &nitems_return
, &bytes_after_return
, &prop
) != Success
)
5883 if (actual_type_return
!= XA_STRING
)
5888 ret
= strtonum((const char *)prop
, 0, UINT_MAX
, &errstr
);
5889 /* ignore error because strtonum returns 0 anyway */
5896 manage_window(Window id
)
5899 struct workspace
*ws
;
5900 struct ws_win
*win
, *ww
;
5901 int format
, i
, ws_idx
, n
, border_me
= 0;
5902 unsigned long nitems
, bytes
;
5903 Atom ws_idx_atom
= 0, type
;
5904 Atom
*prot
= NULL
, *pp
;
5905 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
5906 struct swm_region
*r
;
5913 if ((win
= find_window(id
)) != NULL
)
5914 return (win
); /* already being managed */
5916 /* see if we are on the unmanaged list */
5917 if ((win
= find_unmanaged_window(id
)) != NULL
) {
5918 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
5919 "window: 0x%lx\n", win
->id
);
5920 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5922 set_child_transient(win
, &trans
);
5924 if (trans
&& (ww
= find_window(trans
)))
5925 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
5926 else if ((ww
= win
->ws
->focus
) &&
5927 spawn_position
== SWM_STACK_ABOVE
)
5928 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
, entry
);
5929 else if (ww
&& spawn_position
== SWM_STACK_BELOW
)
5930 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
, entry
);
5931 else switch (spawn_position
) {
5934 case SWM_STACK_ABOVE
:
5935 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
5937 case SWM_STACK_BOTTOM
:
5938 case SWM_STACK_BELOW
:
5939 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
5942 ewmh_update_actions(win
);
5946 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
5947 err(1, "manage_window: calloc: failed to allocate memory for "
5952 /* see if we need to override the workspace */
5953 p
= find_pid(window_get_pid(id
));
5955 /* Get all the window data in one shot */
5956 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
5958 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
5959 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
5961 XGetWindowAttributes(display
, id
, &win
->wa
);
5962 XGetWMNormalHints(display
, id
, &win
->sh
, &win
->sh_mask
);
5963 win
->hints
= XGetWMHints(display
, id
);
5964 XGetTransientForHint(display
, id
, &trans
);
5966 win
->transient
= trans
;
5967 set_child_transient(win
, &trans
);
5968 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, "
5969 "transient: 0x%lx\n", win
->id
, win
->transient
);
5972 /* get supported protocols */
5973 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
5974 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
5975 if (*pp
== takefocus
)
5976 win
->take_focus
= 1;
5978 win
->can_delete
= 1;
5984 win
->iconic
= get_iconic(win
);
5987 * Figure out where to put the window. If it was previously assigned to
5988 * a workspace (either by spawn() or manually moving), and isn't
5989 * transient, * put it in the same workspace
5991 r
= root_to_region(win
->wa
.root
);
5993 ws
= &r
->s
->ws
[p
->ws
];
5994 TAILQ_REMOVE(&pidlist
, p
, entry
);
5997 } else if (prop
&& win
->transient
== 0) {
5998 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
5999 ws_idx
= strtonum((const char *)prop
, 0, workspace_limit
- 1,
6002 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
6005 ws
= &r
->s
->ws
[ws_idx
];
6008 /* this should launch transients in the same ws as parent */
6010 if ((ww
= find_window(trans
)) != NULL
)
6016 warnx("manage_window: fix this "
6022 /* set up the window layout */
6025 win
->s
= r
->s
; /* this never changes */
6026 if (trans
&& (ww
= find_window(trans
)))
6027 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
6028 else if (spawn_position
== SWM_STACK_ABOVE
&& win
->ws
->focus
)
6029 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
, entry
);
6031 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
6033 WIDTH(win
) = win
->wa
.width
;
6034 HEIGHT(win
) = win
->wa
.height
;
6037 win
->g_floatvalid
= 0;
6038 win
->floatmaxed
= 0;
6039 win
->ewmh_flags
= 0;
6041 /* Set window properties so we can remember this after reincarnation */
6042 if (ws_idx_atom
&& prop
== NULL
&&
6043 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
6045 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
6047 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
6048 PropModeReplace
, ws_idx_str
, strlen((char *)ws_idx_str
));
6053 ewmh_autoquirk(win
);
6055 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
6056 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
6057 win
->ch
.res_class
, win
->ch
.res_name
);
6059 /* java is retarded so treat it special */
6060 if (strstr(win
->ch
.res_name
, "sun-awt")) {
6065 TAILQ_FOREACH(qp
, &quirks
, entry
) {
6066 if (!strcmp(win
->ch
.res_class
, qp
->class) &&
6067 !strcmp(win
->ch
.res_name
, qp
->name
)) {
6068 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
6069 "class: %s, name: %s\n", win
->ch
.res_class
,
6071 if (qp
->quirk
& SWM_Q_FLOAT
) {
6075 win
->quirks
= qp
->quirk
;
6080 /* alter window position if quirky */
6081 if (win
->quirks
& SWM_Q_ANYWHERE
) {
6082 win
->manual
= 1; /* don't center the quirky windows */
6083 bzero(&wc
, sizeof wc
);
6085 if (bar_enabled
&& Y(win
) < bar_height
) {
6086 Y(win
) = wc
.y
= bar_height
;
6089 if (WIDTH(win
) + X(win
) > WIDTH(r
)) {
6090 X(win
) = wc
.x
= WIDTH(r
) - WIDTH(win
) - 2;
6096 /* Reset font sizes (the bruteforce way; no default keybinding). */
6097 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
6098 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6099 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
6100 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
6101 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
6104 ewmh_get_win_state(win
);
6105 ewmh_update_actions(win
);
6106 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
6110 bzero(&wc
, sizeof wc
);
6111 wc
.border_width
= border_width
;
6112 mask
|= CWBorderWidth
;
6113 XConfigureWindow(display
, win
->id
, mask
, &wc
);
6116 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
6117 PropertyChangeMask
| StructureNotifyMask
);
6119 /* floaters need to be mapped if they are in the current workspace */
6120 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
6121 XMapRaised(display
, win
->id
);
6127 free_window(struct ws_win
*win
)
6129 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%lx\n", win
->id
);
6134 /* needed for restart wm */
6135 set_win_state(win
, WithdrawnState
);
6137 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
6139 if (win
->ch
.res_class
)
6140 XFree(win
->ch
.res_class
);
6141 if (win
->ch
.res_name
)
6142 XFree(win
->ch
.res_name
);
6147 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
6153 unmanage_window(struct ws_win
*win
)
6155 struct ws_win
*parent
;
6160 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%lx\n", win
->id
);
6162 if (win
->transient
) {
6163 parent
= find_window(win
->transient
);
6165 parent
->child_trans
= NULL
;
6168 /* focus on root just in case */
6169 XSetInputFocus(display
, PointerRoot
, PointerRoot
, CurrentTime
);
6178 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
6179 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
6183 focus_magic(struct ws_win
*win
)
6185 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%lx\n", WINID(win
));
6188 /* if there are no windows clear the status-bar */
6193 if (win
->child_trans
) {
6194 /* win = parent & has a transient so focus on that */
6196 focus_win(win
->child_trans
);
6197 if (win
->child_trans
->take_focus
)
6198 client_msg(win
, takefocus
);
6200 /* make sure transient hasn't disappeared */
6201 if (validate_win(win
->child_trans
) == 0) {
6202 focus_win(win
->child_trans
);
6203 if (win
->child_trans
->take_focus
)
6204 client_msg(win
->child_trans
, takefocus
);
6206 win
->child_trans
= NULL
;
6208 if (win
->take_focus
)
6209 client_msg(win
, takefocus
);
6215 if (win
->take_focus
)
6216 client_msg(win
, takefocus
);
6223 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%lx\n", e
->xexpose
.window
);
6230 XKeyEvent
*ev
= &e
->xkey
;
6232 struct swm_region
*r
;
6234 keysym
= XkbKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0, 0);
6235 if ((kp
= key_lookup(CLEANMASK(ev
->state
), keysym
)) == NULL
)
6237 if (keyfuncs
[kp
->funcid
].func
== NULL
)
6240 r
= root_to_region(ev
->root
);
6241 if (kp
->funcid
== kf_spawn_custom
)
6242 spawn_custom(r
, &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
6244 keyfuncs
[kp
->funcid
].func(r
, &(keyfuncs
[kp
->funcid
].args
));
6248 buttonpress(XEvent
*e
)
6252 XButtonPressedEvent
*ev
= &e
->xbutton
;
6254 if ((win
= find_window(ev
->window
)) == NULL
)
6258 action
= client_click
;
6260 for (i
= 0; i
< LENGTH(buttons
); i
++)
6261 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6262 buttons
[i
].button
== ev
->button
&&
6263 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
6264 buttons
[i
].func(win
, &buttons
[i
].args
);
6268 configurerequest(XEvent
*e
)
6270 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
6275 if ((win
= find_window(ev
->window
)) == NULL
)
6276 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6279 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%lx, new: %s\n",
6280 ev
->window
, YESNO(new));
6283 bzero(&wc
, sizeof wc
);
6286 wc
.width
= ev
->width
;
6287 wc
.height
= ev
->height
;
6288 wc
.border_width
= ev
->border_width
;
6289 wc
.sibling
= ev
->above
;
6290 wc
.stack_mode
= ev
->detail
;
6291 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
6293 config_win(win
, ev
);
6297 configurenotify(XEvent
*e
)
6301 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%lx\n",
6302 e
->xconfigure
.window
);
6304 win
= find_window(e
->xconfigure
.window
);
6306 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &win
->sh_mask
);
6310 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6311 drain_enter_notify();
6316 destroynotify(XEvent
*e
)
6319 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
6321 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%lx\n", ev
->window
);
6323 if ((win
= find_window(ev
->window
)) == NULL
) {
6324 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6330 /* make sure we focus on something */
6333 unmanage_window(win
);
6335 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6336 drain_enter_notify();
6341 enternotify(XEvent
*e
)
6343 XCrossingEvent
*ev
= &e
->xcrossing
;
6348 Window focus_return
;
6349 int revert_to_return
;
6351 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%lx, mode: %d, detail: "
6352 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6353 "state: %d\n", ev
->window
, ev
->mode
, ev
->detail
, ev
->root
,
6354 ev
->subwindow
, YESNO(ev
->same_screen
), YESNO(ev
->focus
), ev
->state
);
6356 if (ev
->mode
!= NotifyNormal
) {
6357 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6362 switch (focus_mode
) {
6363 case SWM_FOCUS_DEFAULT
:
6365 case SWM_FOCUS_FOLLOW
:
6367 case SWM_FOCUS_SYNERGY
:
6370 * all these checks need to be in this order because the
6371 * XCheckTypedWindowEvent relies on weeding out the previous events
6373 * making this code an option would enable a follow mouse for focus
6378 * state is set when we are switching workspaces and focus is set when
6379 * the window or a subwindow already has focus (occurs during restart).
6381 * Only honor the focus flag if last_focus_event is not FocusOut,
6382 * this allows spectrwm to continue to control focus when another
6383 * program is also playing with it.
6385 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6386 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6391 * happens when a window is created or destroyed and the border
6392 * crosses the mouse pointer and when switching ws
6394 * we need the subwindow test to see if we came from root in order
6395 * to give focus to floaters
6397 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6398 ev
->subwindow
== 0) {
6399 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6403 /* this window already has focus */
6404 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6405 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6409 /* this window is being deleted or moved to another ws */
6410 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6412 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6413 XPutBackEvent(display
, &cne
);
6417 if ((win
= find_window(ev
->window
)) == NULL
) {
6418 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6423 * In fullstack kill all enters unless they come from a different ws
6424 * (i.e. another region) or focus has been grabbed externally.
6426 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6427 last_focus_event
!= FocusOut
) {
6428 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6429 if ((w
= find_window(focus_return
)) == NULL
||
6431 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6439 if ((win
= find_window(ev
->window
)) == NULL
) {
6440 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6445 * if we have more enternotifies let them handle it in due time
6447 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
6448 DNPRINTF(SWM_D_EVENT
,
6449 "ignoring enternotify: got more enternotify\n");
6450 XPutBackEvent(display
, &cne
);
6457 /* lets us use one switch statement for arbitrary mode/detail combinations */
6458 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6461 focusevent(XEvent
*e
)
6465 u_int32_t mode_detail
;
6466 XFocusChangeEvent
*ev
= &e
->xfocus
;
6468 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: 0x%lx mode: %d "
6469 "detail: %d\n", ev
->type
== FocusIn
? "entering" : "leaving",
6470 ev
->window
, ev
->mode
, ev
->detail
);
6472 if (last_focus_event
== ev
->type
) {
6473 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
6477 last_focus_event
= ev
->type
;
6478 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
6480 switch (mode_detail
) {
6481 /* synergy client focus operations */
6482 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
6483 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
6485 /* synergy server focus operations */
6486 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
6488 /* Entering applications like rdesktop that mangle the pointer */
6489 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
6491 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
6492 XSetWindowBorder(display
, win
->id
,
6493 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
6494 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
6497 warnx("ignoring focusevent");
6498 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
6505 mapnotify(XEvent
*e
)
6508 XMapEvent
*ev
= &e
->xmap
;
6510 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%lx\n", ev
->window
);
6512 win
= manage_window(ev
->window
);
6514 set_win_state(win
, NormalState
);
6517 * focus_win can only set input focus on a mapped window.
6518 * make sure the window really has focus since it is just being mapped.
6520 if (win
->ws
->focus
== win
)
6525 mappingnotify(XEvent
*e
)
6527 XMappingEvent
*ev
= &e
->xmapping
;
6529 XRefreshKeyboardMapping(ev
);
6530 if (ev
->request
== MappingKeyboard
)
6535 maprequest(XEvent
*e
)
6538 struct swm_region
*r
;
6539 XWindowAttributes wa
;
6540 XMapRequestEvent
*ev
= &e
->xmaprequest
;
6542 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%lx\n",
6543 e
->xmaprequest
.window
);
6545 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
6547 if (wa
.override_redirect
)
6550 win
= manage_window(e
->xmaprequest
.window
);
6552 return; /* can't happen */
6556 /* make new win focused */
6557 r
= root_to_region(win
->wa
.root
);
6558 if (win
->ws
== r
->ws
)
6563 propertynotify(XEvent
*e
)
6566 XPropertyEvent
*ev
= &e
->xproperty
;
6569 name
= XGetAtomName(display
, ev
->atom
);
6570 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%lx, atom: %s\n",
6575 win
= find_window(ev
->window
);
6579 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
6580 update_iconic(win
, 0);
6581 XMapRaised(display
, win
->id
);
6589 case XA_WM_NORMAL_HINTS
:
6591 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
6592 warnx("normal hints: flag 0x%x", win
->sh
.flags
);
6593 if (win
->sh
.flags
& PMinSize
) {
6594 WIDTH(win
) = win
->sh
.min_width
;
6595 HEIGHT(win
) = win
->sh
.min_height
;
6596 warnx("min %d %d", WIDTH(win
), HEIGHT(win
));
6598 XMoveResizeWindow(display
, win
->id
,
6599 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
));
6611 unmapnotify(XEvent
*e
)
6615 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%lx\n", e
->xunmap
.window
);
6617 /* determine if we need to help unmanage this window */
6618 win
= find_window(e
->xunmap
.window
);
6622 if (getstate(e
->xunmap
.window
) == NormalState
) {
6623 unmanage_window(win
);
6626 /* giant hack for apps that don't destroy transient windows */
6627 /* eat a bunch of events to prevent remanaging the window */
6629 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6630 EnterWindowMask
, &cne
))
6632 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6633 StructureNotifyMask
, &cne
))
6635 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6636 SubstructureNotifyMask
, &cne
))
6638 /* resend unmap because we ated it */
6639 XUnmapWindow(display
, e
->xunmap
.window
);
6642 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6643 drain_enter_notify();
6647 visibilitynotify(XEvent
*e
)
6650 struct swm_region
*r
;
6652 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%lx\n",
6653 e
->xvisibility
.window
);
6654 if (e
->xvisibility
.state
== VisibilityUnobscured
)
6655 for (i
= 0; i
< ScreenCount(display
); i
++)
6656 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6657 if (e
->xvisibility
.window
== r
->bar_window
)
6662 clientmessage(XEvent
*e
)
6664 XClientMessageEvent
*ev
;
6669 win
= find_window(ev
->window
);
6673 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx, type: %ld\n",
6674 ev
->window
, ev
->message_type
);
6676 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
6677 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
6680 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
6681 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
6682 if (win
->can_delete
)
6683 client_msg(win
, adelete
);
6685 XKillClient(display
, win
->id
);
6687 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
6688 DNPRINTF(SWM_D_EVENT
,
6689 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
6690 if (win
->floating
) {
6691 if (ev
->data
.l
[0] & (1<<8)) /* x */
6692 X(win
) = ev
->data
.l
[1];
6693 if (ev
->data
.l
[0] & (1<<9)) /* y */
6694 Y(win
) = ev
->data
.l
[2];
6695 if (ev
->data
.l
[0] & (1<<10)) /* width */
6696 WIDTH(win
) = ev
->data
.l
[3];
6697 if (ev
->data
.l
[0] & (1<<11)) /* height */
6698 HEIGHT(win
) = ev
->data
.l
[4];
6703 /* TODO: Change stack sizes */
6704 /* notify no change was made. */
6705 config_win(win
, NULL
);
6708 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
6709 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
6710 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
6712 ewmh_update_win_state(win
, ev
->data
.l
[2],
6720 xerror_start(Display
*d
, XErrorEvent
*ee
)
6727 xerror(Display
*d
, XErrorEvent
*ee
)
6729 /* warnx("error: %p %p", display, ee); */
6737 xerrorxlib
= XSetErrorHandler(xerror_start
);
6739 /* this causes an error if some other window manager is running */
6740 XSelectInput(display
, DefaultRootWindow(display
),
6741 SubstructureRedirectMask
);
6742 XSync(display
, False
);
6746 XSetErrorHandler(xerror
);
6747 XSync(display
, False
);
6752 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
6754 struct swm_region
*r
, *n
;
6755 struct workspace
*ws
= NULL
;
6758 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
6759 s
->idx
, w
, h
, x
, y
);
6761 /* remove any conflicting regions */
6762 n
= TAILQ_FIRST(&s
->rl
);
6765 n
= TAILQ_NEXT(r
, entry
);
6766 if (X(r
) < (x
+ w
) &&
6767 (X(r
) + WIDTH(r
)) > x
&&
6769 (Y(r
) + HEIGHT(r
)) > y
) {
6770 if (r
->ws
->r
!= NULL
)
6771 r
->ws
->old_r
= r
->ws
->r
;
6773 XDestroyWindow(display
, r
->bar_window
);
6774 TAILQ_REMOVE(&s
->rl
, r
, entry
);
6775 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
6779 /* search old regions for one to reuse */
6781 /* size + location match */
6782 TAILQ_FOREACH(r
, &s
->orl
, entry
)
6783 if (X(r
) == x
&& Y(r
) == y
&&
6784 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
6788 TAILQ_FOREACH(r
, &s
->orl
, entry
)
6789 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
6793 TAILQ_REMOVE(&s
->orl
, r
, entry
);
6794 /* try to use old region's workspace */
6795 if (r
->ws
->r
== NULL
)
6798 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
6799 err(1, "new_region: calloc: failed to allocate memory "
6802 /* if we don't have a workspace already, find one */
6804 for (i
= 0; i
< workspace_limit
; i
++)
6805 if (s
->ws
[i
].r
== NULL
) {
6812 errx(1, "new_region: no free workspaces");
6823 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
6829 #ifdef SWM_XRR_HAS_CRTC
6831 XRRScreenResources
*sr
;
6834 #endif /* SWM_XRR_HAS_CRTC */
6835 struct swm_region
*r
;
6838 if (i
>= ScreenCount(display
))
6839 errx(1, "scan_xrandr: invalid screen");
6841 /* remove any old regions */
6842 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
6843 r
->ws
->old_r
= r
->ws
->r
= NULL
;
6844 XDestroyWindow(display
, r
->bar_window
);
6845 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
6846 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
6850 /* map virtual screens onto physical screens */
6851 #ifdef SWM_XRR_HAS_CRTC
6852 if (xrandr_support
) {
6853 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
6855 new_region(&screens
[i
], 0, 0,
6856 DisplayWidth(display
, i
),
6857 DisplayHeight(display
, i
));
6861 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
6862 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
6863 if (ci
->noutput
== 0)
6866 if (ci
!= NULL
&& ci
->mode
== None
)
6867 new_region(&screens
[i
], 0, 0,
6868 DisplayWidth(display
, i
),
6869 DisplayHeight(display
, i
));
6871 new_region(&screens
[i
],
6872 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
6875 XRRFreeCrtcInfo(ci
);
6876 XRRFreeScreenResources(sr
);
6878 #endif /* SWM_XRR_HAS_CRTC */
6880 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
6881 DisplayHeight(display
, i
));
6886 screenchange(XEvent
*e
) {
6887 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
6888 struct swm_region
*r
;
6891 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%lx\n", xe
->root
);
6893 if (!XRRUpdateConfiguration(e
))
6896 /* silly event doesn't include the screen index */
6897 for (i
= 0; i
< ScreenCount(display
); i
++)
6898 if (screens
[i
].root
== xe
->root
)
6900 if (i
>= ScreenCount(display
))
6901 errx(1, "screenchange: screen not found");
6903 /* brute force for now, just re-enumerate the regions */
6906 /* add bars to all regions */
6907 for (i
= 0; i
< ScreenCount(display
); i
++)
6908 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6911 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6912 drain_enter_notify();
6918 Window d1
, d2
, *wins
= NULL
;
6919 XWindowAttributes wa
;
6924 for (i
= 0; i
< ScreenCount(display
); i
++) {
6925 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
6928 /* attach windows to a region */
6929 /* normal windows */
6930 for (j
= 0; j
< no
; j
++) {
6931 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6932 wa
.override_redirect
||
6933 XGetTransientForHint(display
, wins
[j
], &d1
))
6936 state
= getstate(wins
[j
]);
6937 manage
= state
== IconicState
;
6938 if (wa
.map_state
== IsViewable
|| manage
)
6939 manage_window(wins
[j
]);
6941 /* transient windows */
6942 for (j
= 0; j
< no
; j
++) {
6943 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6944 wa
.override_redirect
)
6947 state
= getstate(wins
[j
]);
6948 manage
= state
== IconicState
;
6949 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
6951 manage_window(wins
[j
]);
6964 int errorbase
, major
, minor
;
6965 struct workspace
*ws
;
6968 if ((screens
= calloc(ScreenCount(display
),
6969 sizeof(struct swm_screen
))) == NULL
)
6970 err(1, "setup_screens: calloc: failed to allocate memory for "
6973 /* initial Xrandr setup */
6974 xrandr_support
= XRRQueryExtension(display
,
6975 &xrandr_eventbase
, &errorbase
);
6977 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
6980 /* map physical screens */
6981 for (i
= 0; i
< ScreenCount(display
); i
++) {
6982 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
6984 TAILQ_INIT(&screens
[i
].rl
);
6985 TAILQ_INIT(&screens
[i
].orl
);
6986 screens
[i
].root
= RootWindow(display
, i
);
6988 /* set default colors */
6989 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
6990 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
6991 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
6992 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
6993 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
6995 /* create graphics context on screen with default font color */
6996 screens
[i
].bar_gc
= XCreateGC(display
, screens
[i
].root
, 0,
6999 XSetForeground(display
, screens
[i
].bar_gc
,
7000 screens
[i
].c
[SWM_S_COLOR_BAR_FONT
].color
);
7002 /* set default cursor */
7003 XDefineCursor(display
, screens
[i
].root
,
7004 XCreateFontCursor(display
, XC_left_ptr
));
7006 /* init all workspaces */
7007 /* XXX these should be dynamically allocated too */
7008 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
7009 ws
= &screens
[i
].ws
[j
];
7015 TAILQ_INIT(&ws
->winlist
);
7016 TAILQ_INIT(&ws
->unmanagedlist
);
7018 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
7019 if (layouts
[k
].l_config
!= NULL
)
7020 layouts
[k
].l_config(ws
,
7021 SWM_ARG_ID_STACKINIT
);
7022 ws
->cur_layout
= &layouts
[0];
7023 ws
->cur_layout
->l_string(ws
);
7029 XRRSelectInput(display
, screens
[i
].root
,
7030 RRScreenChangeNotifyMask
);
7037 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
7038 err(1, "setup_globals: strdup: failed to allocate memory.");
7040 if ((spawn_term
[0] = strdup("xterm")) == NULL
)
7041 err(1, "setup_globals: strdup: failed to allocate memory.");
7043 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
7044 err(1, "setup_globals: strdup: failed to allocate memory.");
7051 Atom netwmcheck
, netwmname
, utf8_string
;
7054 /* work around sun jdk bugs, code from wmname */
7055 netwmcheck
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
7056 netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
7057 utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
7058 for (i
= 0; i
< ScreenCount(display
); i
++) {
7059 root
= screens
[i
].root
;
7060 win
= XCreateSimpleWindow(display
,root
, 0, 0, 1, 1, 0,
7061 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
,
7062 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
);
7064 XChangeProperty(display
, root
, netwmcheck
, XA_WINDOW
, 32,
7065 PropModeReplace
, (unsigned char *)&win
, 1);
7066 XChangeProperty(display
, win
, netwmcheck
, XA_WINDOW
, 32,
7067 PropModeReplace
, (unsigned char *)&win
, 1);
7068 XChangeProperty(display
, win
, netwmname
, utf8_string
, 8,
7069 PropModeReplace
, (unsigned char*)"LG3D", strlen("LG3D"));
7074 main(int argc
, char *argv
[])
7076 struct swm_region
*r
, *rr
;
7077 struct ws_win
*winfocus
= NULL
;
7080 char conf
[PATH_MAX
], *cfile
= NULL
;
7085 struct sigaction sact
;
7088 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
7089 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
7091 warnx("no locale support");
7093 if (!X_HAVE_UTF8_STRING
)
7094 warnx("no UTF-8 support");
7096 if (!(display
= XOpenDisplay(0)))
7097 errx(1, "can not open display");
7100 errx(1, "other wm running");
7102 /* handle some signals */
7103 bzero(&sact
, sizeof(sact
));
7104 sigemptyset(&sact
.sa_mask
);
7106 sact
.sa_handler
= sighdlr
;
7107 sigaction(SIGINT
, &sact
, NULL
);
7108 sigaction(SIGQUIT
, &sact
, NULL
);
7109 sigaction(SIGTERM
, &sact
, NULL
);
7110 sigaction(SIGHUP
, &sact
, NULL
);
7112 sact
.sa_handler
= sighdlr
;
7113 sact
.sa_flags
= SA_NOCLDSTOP
;
7114 sigaction(SIGCHLD
, &sact
, NULL
);
7116 astate
= XInternAtom(display
, "WM_STATE", False
);
7117 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
7118 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
7119 takefocus
= XInternAtom(display
, "WM_TAKE_FOCUS", False
);
7120 a_wmname
= XInternAtom(display
, "WM_NAME", False
);
7121 a_netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
7122 a_utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
7123 a_string
= XInternAtom(display
, "STRING", False
);
7124 a_swm_iconic
= XInternAtom(display
, "_SWM_ICONIC", False
);
7126 /* look for local and global conf file */
7127 pwd
= getpwuid(getuid());
7129 errx(1, "invalid user: %d", getuid());
7138 for (i
= 0; ; i
++) {
7143 snprintf(conf
, sizeof conf
, "%s/.%s",
7144 pwd
->pw_dir
, SWM_CONF_FILE
);
7148 snprintf(conf
, sizeof conf
, "/etc/%s",
7153 snprintf(conf
, sizeof conf
, "%s/.%s",
7154 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
7158 snprintf(conf
, sizeof conf
, "/etc/%s",
7165 if (strlen(conf
) && stat(conf
, &sb
) != -1)
7166 if (S_ISREG(sb
.st_mode
)) {
7173 /* load conf (if any) and refresh font color in bar graphics contexts */
7174 if (cfile
&& conf_load(cfile
, SWM_CONF_DEFAULT
) == 0)
7175 for (i
= 0; i
< ScreenCount(display
); ++i
)
7176 XSetForeground(display
, screens
[i
].bar_gc
,
7177 screens
[i
].c
[SWM_S_COLOR_BAR_FONT
].color
);
7180 /* set some values to work around bad programs */
7182 /* grab existing windows (before we build the bars) */
7185 if (getenv("SWM_STARTED") == NULL
)
7186 setenv("SWM_STARTED", "YES", 1);
7188 /* setup all bars */
7189 for (i
= 0; i
< ScreenCount(display
); i
++)
7190 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
7191 if (winfocus
== NULL
)
7192 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
7200 if (focus_mode
== SWM_FOCUS_DEFAULT
)
7201 drain_enter_notify();
7203 xfd
= ConnectionNumber(display
);
7205 while (XPending(display
)) {
7206 XNextEvent(display
, &e
);
7209 if (e
.type
< LASTEvent
) {
7210 DNPRINTF(SWM_D_EVENTQ
,"XEvent: handled: %s, "
7211 "window: 0x%lx, type: %s (%d), %d remaining"
7212 "\n", YESNO(handler
[e
.type
]),
7213 e
.xany
.window
, geteventname(&e
),
7214 e
.type
, QLength(display
));
7216 if (handler
[e
.type
])
7217 handler
[e
.type
](&e
);
7219 DNPRINTF(SWM_D_EVENTQ
, "XRandr Event: window: "
7220 "0x%lx, type: %s (%d)\n", e
.xany
.window
,
7221 xrandr_geteventname(&e
), e
.type
);
7223 switch (e
.type
- xrandr_eventbase
) {
7224 case RRScreenChangeNotify
:
7233 /* if we are being restarted go focus on first window */
7235 rr
= winfocus
->ws
->r
;
7237 /* not a visible window */
7241 /* move pointer to first screen if multi screen */
7242 if (ScreenCount(display
) > 1 || outputs
> 1)
7243 XWarpPointer(display
, None
, rr
->s
[0].root
,
7245 Y(rr
) + (bar_enabled
? bar_height
: 0));
7247 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7257 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7259 DNPRINTF(SWM_D_MISC
, "select failed");
7260 if (restart_wm
== 1)
7261 restart(NULL
, NULL
);
7262 if (search_resp
== 1)
7275 for (i
= 0; i
< ScreenCount(display
); ++i
)
7276 if (screens
[i
].bar_gc
!= NULL
)
7277 XFreeGC(display
, screens
[i
].bar_gc
);
7279 XFreeFontSet(display
, bar_fs
);
7280 XCloseDisplay(display
);