2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Much code and ideas taken from dwm under the following license:
24 * MIT/X Consortium License
26 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
27 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
28 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
29 * 2007 Premysl Hruby <dfenze at gmail dot com>
30 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
31 * 2007 Christof Musik <christof at sendfax dot de>
32 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
33 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
34 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
36 * Permission is hereby granted, free of charge, to any person obtaining a
37 * copy of this software and associated documentation files (the "Software"),
38 * to deal in the Software without restriction, including without limitation
39 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
40 * and/or sell copies of the Software, and to permit persons to whom the
41 * Software is furnished to do so, subject to the following conditions:
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
49 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
51 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
52 * DEALINGS IN THE SOFTWARE.
70 #include <sys/types.h>
74 #include <sys/queue.h>
75 #include <sys/param.h>
76 #include <sys/select.h>
78 #include <X11/cursorfont.h>
79 #include <X11/keysym.h>
80 #include <X11/Xatom.h>
82 #include <X11/Xproto.h>
83 #include <X11/Xutil.h>
84 #include <X11/extensions/Xrandr.h>
85 #include <X11/extensions/XTest.h>
93 #ifdef SCROTWM_BUILDSTR
94 static const char *buildstr
= SCROTWM_BUILDSTR
;
96 static const char *buildstr
= SCROTWM_VERSION
;
100 # error XRandR versions less than 1.0 are not supported
105 #define SWM_XRR_HAS_CRTC
109 /*#define SWM_DEBUG*/
111 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
112 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
113 #define SWM_D_MISC 0x0001
114 #define SWM_D_EVENT 0x0002
115 #define SWM_D_WS 0x0004
116 #define SWM_D_FOCUS 0x0008
117 #define SWM_D_MOVE 0x0010
118 #define SWM_D_STACK 0x0020
119 #define SWM_D_MOUSE 0x0040
120 #define SWM_D_PROP 0x0080
121 #define SWM_D_CLASS 0x0100
122 #define SWM_D_KEY 0x0200
123 #define SWM_D_QUIRK 0x0400
124 #define SWM_D_SPAWN 0x0800
125 #define SWM_D_EVENTQ 0x1000
126 #define SWM_D_CONF 0x2000
127 #define SWM_D_BAR 0x4000
129 u_int32_t swm_debug
= 0
147 #define DPRINTF(x...)
148 #define DNPRINTF(n,x...)
151 #define LENGTH(x) (sizeof x / sizeof x[0])
152 #define MODKEY Mod1Mask
153 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
154 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
155 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
156 #define SWM_PROPLEN (16)
157 #define SWM_FUNCNAME_LEN (32)
158 #define SWM_KEYS_LEN (255)
159 #define SWM_QUIRK_LEN (64)
160 #define X(r) (r)->g.x
161 #define Y(r) (r)->g.y
162 #define WIDTH(r) (r)->g.w
163 #define HEIGHT(r) (r)->g.h
164 #define SWM_MAX_FONT_STEPS (3)
165 #define WINID(w) (w ? w->id : 0)
166 #define YESNO(x) (x ? "yes" : "no")
168 #define SWM_FOCUS_DEFAULT (0)
169 #define SWM_FOCUS_SYNERGY (1)
170 #define SWM_FOCUS_FOLLOW (2)
172 #define SWM_CONF_DEFAULT (0)
173 #define SWM_CONF_KEYMAPPING (1)
176 #define SWM_LIB "/usr/local/lib/libswmhack.so"
189 volatile sig_atomic_t running
= 1;
190 volatile sig_atomic_t restart_wm
= 0;
192 int last_focus_event
= FocusOut
;
193 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
197 int xrandr_eventbase
;
198 unsigned int numlockmask
= 0;
202 int cycle_visible
= 0;
204 int font_adjusted
= 0;
205 unsigned int mod_key
= MODKEY
;
208 struct swm_region
*search_r
;
209 int select_list_pipe
[2];
210 int select_resp_pipe
[2];
212 volatile sig_atomic_t search_resp
;
213 int search_resp_action
;
215 struct search_window
{
216 TAILQ_ENTRY(search_window
) entry
;
222 TAILQ_HEAD(search_winlist
, search_window
);
224 struct search_winlist search_wl
;
229 SWM_SEARCH_UNICONIFY
,
230 SWM_SEARCH_NAME_WORKSPACE
,
231 SWM_SEARCH_SEARCH_WORKSPACE
,
232 SWM_SEARCH_SEARCH_WINDOW
236 double dialog_ratio
= 0.6;
238 #define SWM_BAR_MAX (256)
239 #define SWM_BAR_JUSTIFY_LEFT (0)
240 #define SWM_BAR_JUSTIFY_CENTER (1)
241 #define SWM_BAR_JUSTIFY_RIGHT (2)
242 #define SWM_BAR_OFFSET (4)
243 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
244 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
245 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
246 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"
248 #ifdef X_HAVE_UTF8_STRING
249 #define DRAWSTRING(x...) Xutf8DrawString(x)
251 #define DRAWSTRING(x...) XmbDrawString(x)
254 char *bar_argv
[] = { NULL
, NULL
};
256 unsigned char bar_ext
[SWM_BAR_MAX
];
257 char bar_vertext
[SWM_BAR_MAX
];
259 sig_atomic_t bar_alarm
= 0;
262 int bar_border_width
= 1;
263 int bar_at_bottom
= 0;
265 int bar_extra_running
= 0;
268 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
269 int stack_enabled
= 1;
270 int clock_enabled
= 1;
271 int urgent_enabled
= 0;
272 char *clock_format
= NULL
;
273 int title_name_enabled
= 0;
274 int title_class_enabled
= 0;
275 int window_name_enabled
= 0;
276 int focus_mode
= SWM_FOCUS_DEFAULT
;
277 int disable_border
= 0;
278 int border_width
= 1;
279 int verbose_layout
= 0;
282 XFontSetExtents
*bar_fs_extents
;
284 char *spawn_term
[] = { NULL
, NULL
}; /* XXX fully dynamic */
287 #define SWM_MENU_FN (2)
288 #define SWM_MENU_NB (4)
289 #define SWM_MENU_NF (6)
290 #define SWM_MENU_SB (8)
291 #define SWM_MENU_SF (10)
293 /* layout manager data */
294 struct swm_geometry
{
304 /* virtual "screens" */
306 TAILQ_ENTRY(swm_region
) entry
;
307 struct swm_geometry g
;
308 struct workspace
*ws
; /* current workspace on this region */
309 struct workspace
*ws_prior
; /* prior workspace on this region */
310 struct swm_screen
*s
; /* screen idx */
313 TAILQ_HEAD(swm_region_list
, swm_region
);
316 TAILQ_ENTRY(ws_win
) entry
;
319 struct ws_win
*child_trans
; /* transient child window */
320 struct swm_geometry g
; /* current geometry */
321 struct swm_geometry g_float
; /* geometry when floating */
322 struct swm_geometry rg_float
; /* region geom when floating */
323 int g_floatvalid
; /* g_float geometry validity */
324 int floatmaxed
; /* whether maxed by max_stack */
328 unsigned int ewmh_flags
;
329 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
335 unsigned long quirks
;
336 struct workspace
*ws
; /* always valid */
337 struct swm_screen
*s
; /* always valid, never changes */
338 XWindowAttributes wa
;
343 TAILQ_HEAD(ws_win_list
, ws_win
);
347 TAILQ_ENTRY(pid_e
) entry
;
351 TAILQ_HEAD(pid_list
, pid_e
);
352 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
354 /* layout handlers */
356 void vertical_config(struct workspace
*, int);
357 void vertical_stack(struct workspace
*, struct swm_geometry
*);
358 void horizontal_config(struct workspace
*, int);
359 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
360 void max_stack(struct workspace
*, struct swm_geometry
*);
361 void plain_stacker(struct workspace
*);
362 void fancy_stacker(struct workspace
*);
364 struct ws_win
*find_window(Window
);
366 void grabbuttons(struct ws_win
*, int);
367 void new_region(struct swm_screen
*, int, int, int, int);
368 void unmanage_window(struct ws_win
*);
369 long getstate(Window
);
371 int conf_load(char *, int);
374 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
375 void (*l_config
)(struct workspace
*, int);
377 #define SWM_L_FOCUSPREV (1<<0)
378 #define SWM_L_MAPONFOCUS (1<<1)
379 void (*l_string
)(struct workspace
*);
381 /* stack, configure */
382 { vertical_stack
, vertical_config
, 0, plain_stacker
},
383 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
385 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
386 { NULL
, NULL
, 0, NULL
},
389 /* position of max_stack mode in the layouts array, index into layouts! */
390 #define SWM_V_STACK (0)
391 #define SWM_H_STACK (1)
392 #define SWM_MAX_STACK (2)
394 #define SWM_H_SLICE (32)
395 #define SWM_V_SLICE (32)
397 /* define work spaces */
399 int idx
; /* workspace index */
400 char *name
; /* workspace name */
401 int always_raise
; /* raise windows on focus */
402 struct layout
*cur_layout
; /* current layout handlers */
403 struct ws_win
*focus
; /* may be NULL */
404 struct ws_win
*focus_prev
; /* may be NULL */
405 struct swm_region
*r
; /* may be NULL */
406 struct swm_region
*old_r
; /* may be NULL */
407 struct ws_win_list winlist
; /* list of windows in ws */
408 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
409 char stacker
[10]; /* display stacker and layout */
413 int horizontal_msize
;
415 int horizontal_stacks
;
422 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
423 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
425 /* physical screen mapping */
426 #define SWM_WS_MAX (10)
428 int idx
; /* screen index */
429 struct swm_region_list rl
; /* list of regions on this screen */
430 struct swm_region_list orl
; /* list of old regions */
432 struct workspace ws
[SWM_WS_MAX
];
438 } c
[SWM_S_COLOR_MAX
];
442 struct swm_screen
*screens
;
445 /* args to functions */
448 #define SWM_ARG_ID_FOCUSNEXT (0)
449 #define SWM_ARG_ID_FOCUSPREV (1)
450 #define SWM_ARG_ID_FOCUSMAIN (2)
451 #define SWM_ARG_ID_FOCUSCUR (4)
452 #define SWM_ARG_ID_SWAPNEXT (10)
453 #define SWM_ARG_ID_SWAPPREV (11)
454 #define SWM_ARG_ID_SWAPMAIN (12)
455 #define SWM_ARG_ID_MOVELAST (13)
456 #define SWM_ARG_ID_MASTERSHRINK (20)
457 #define SWM_ARG_ID_MASTERGROW (21)
458 #define SWM_ARG_ID_MASTERADD (22)
459 #define SWM_ARG_ID_MASTERDEL (23)
460 #define SWM_ARG_ID_STACKRESET (30)
461 #define SWM_ARG_ID_STACKINIT (31)
462 #define SWM_ARG_ID_CYCLEWS_UP (40)
463 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
464 #define SWM_ARG_ID_CYCLESC_UP (42)
465 #define SWM_ARG_ID_CYCLESC_DOWN (43)
466 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
467 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
468 #define SWM_ARG_ID_STACKINC (50)
469 #define SWM_ARG_ID_STACKDEC (51)
470 #define SWM_ARG_ID_SS_ALL (60)
471 #define SWM_ARG_ID_SS_WINDOW (61)
472 #define SWM_ARG_ID_DONTCENTER (70)
473 #define SWM_ARG_ID_CENTER (71)
474 #define SWM_ARG_ID_KILLWINDOW (80)
475 #define SWM_ARG_ID_DELETEWINDOW (81)
476 #define SWM_ARG_ID_WIDTHGROW (90)
477 #define SWM_ARG_ID_WIDTHSHRINK (91)
478 #define SWM_ARG_ID_HEIGHTGROW (92)
479 #define SWM_ARG_ID_HEIGHTSHRINK (93)
480 #define SWM_ARG_ID_MOVEUP (100)
481 #define SWM_ARG_ID_MOVEDOWN (101)
482 #define SWM_ARG_ID_MOVELEFT (102)
483 #define SWM_ARG_ID_MOVERIGHT (103)
487 void focus(struct swm_region
*, union arg
*);
488 void focus_magic(struct ws_win
*);
495 #define SWM_Q_FLOAT (1<<0) /* float this window */
496 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
497 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
498 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
499 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
500 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
502 int quirks_size
= 0, quirks_length
= 0;
503 struct quirk
*quirks
= NULL
;
506 * Supported EWMH hints should be added to
507 * both the enum and the ewmh array
509 enum { _NET_ACTIVE_WINDOW
, _NET_MOVERESIZE_WINDOW
, _NET_CLOSE_WINDOW
,
510 _NET_WM_WINDOW_TYPE
, _NET_WM_WINDOW_TYPE_DOCK
,
511 _NET_WM_WINDOW_TYPE_TOOLBAR
, _NET_WM_WINDOW_TYPE_UTILITY
,
512 _NET_WM_WINDOW_TYPE_SPLASH
, _NET_WM_WINDOW_TYPE_DIALOG
,
513 _NET_WM_WINDOW_TYPE_NORMAL
, _NET_WM_STATE
,
514 _NET_WM_STATE_MAXIMIZED_HORZ
, _NET_WM_STATE_MAXIMIZED_VERT
,
515 _NET_WM_STATE_SKIP_TASKBAR
, _NET_WM_STATE_SKIP_PAGER
,
516 _NET_WM_STATE_HIDDEN
, _NET_WM_STATE_ABOVE
, _SWM_WM_STATE_MANUAL
,
517 _NET_WM_STATE_FULLSCREEN
, _NET_WM_ALLOWED_ACTIONS
, _NET_WM_ACTION_MOVE
,
518 _NET_WM_ACTION_RESIZE
, _NET_WM_ACTION_FULLSCREEN
, _NET_WM_ACTION_CLOSE
,
524 } ewmh
[SWM_EWMH_HINT_MAX
] = {
525 /* must be in same order as in the enum */
526 {"_NET_ACTIVE_WINDOW", None
},
527 {"_NET_MOVERESIZE_WINDOW", None
},
528 {"_NET_CLOSE_WINDOW", None
},
529 {"_NET_WM_WINDOW_TYPE", None
},
530 {"_NET_WM_WINDOW_TYPE_DOCK", None
},
531 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None
},
532 {"_NET_WM_WINDOW_TYPE_UTILITY", None
},
533 {"_NET_WM_WINDOW_TYPE_SPLASH", None
},
534 {"_NET_WM_WINDOW_TYPE_DIALOG", None
},
535 {"_NET_WM_WINDOW_TYPE_NORMAL", None
},
536 {"_NET_WM_STATE", None
},
537 {"_NET_WM_STATE_MAXIMIZED_HORZ", None
},
538 {"_NET_WM_STATE_MAXIMIZED_VERT", None
},
539 {"_NET_WM_STATE_SKIP_TASKBAR", None
},
540 {"_NET_WM_STATE_SKIP_PAGER", None
},
541 {"_NET_WM_STATE_HIDDEN", None
},
542 {"_NET_WM_STATE_ABOVE", None
},
543 {"_SWM_WM_STATE_MANUAL", None
},
544 {"_NET_WM_STATE_FULLSCREEN", None
},
545 {"_NET_WM_ALLOWED_ACTIONS", None
},
546 {"_NET_WM_ACTION_MOVE", None
},
547 {"_NET_WM_ACTION_RESIZE", None
},
548 {"_NET_WM_ACTION_FULLSCREEN", None
},
549 {"_NET_WM_ACTION_CLOSE", None
},
552 void store_float_geom(struct ws_win
*, struct swm_region
*);
553 int floating_toggle_win(struct ws_win
*);
554 void spawn_select(struct swm_region
*, union arg
*, char *, int *);
555 unsigned char *get_win_name(Display
*, Window
, Atom
, Atom
, unsigned long *);
558 get_property(Window id
, Atom atom
, long count
, Atom type
,
559 unsigned long *n
, unsigned char **data
)
562 unsigned long tmp
, extra
;
563 unsigned long *nitems
;
566 nitems
= n
!= NULL
? n
: &tmp
;
567 status
= XGetWindowProperty(display
, id
, atom
, 0L, count
, False
, type
,
568 &real
, &format
, nitems
, &extra
, data
);
570 if (status
!= Success
)
579 update_iconic(struct ws_win
*win
, int newv
)
586 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
590 XChangeProperty(display
, win
->id
, iprop
, XA_INTEGER
, 32,
591 PropModeReplace
, (unsigned char *)&v
, 1);
593 XDeleteProperty(display
, win
->id
, iprop
);
597 get_iconic(struct ws_win
*win
)
602 unsigned long nitems
, extra
;
603 unsigned char *prop
= NULL
;
605 iprop
= XInternAtom(display
, "_SWM_ICONIC", False
);
608 status
= XGetWindowProperty(display
, win
->id
, iprop
, 0L, 1L,
609 False
, XA_INTEGER
, &rettype
, &retfmt
, &nitems
, &extra
, &prop
);
610 if (status
!= Success
)
612 if (rettype
!= XA_INTEGER
|| retfmt
!= 32)
616 v
= *((int32_t *)prop
);
630 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
632 for (i
= 0; i
< LENGTH(ewmh
); i
++)
633 ewmh
[i
].atom
= XInternAtom(display
, ewmh
[i
].name
, False
);
635 for (i
= 0; i
< ScreenCount(display
); i
++) {
636 /* Support check window will be created by workaround(). */
638 /* Report supported atoms */
639 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
640 for (j
= 0; j
< LENGTH(ewmh
); j
++)
641 XChangeProperty(display
, screens
[i
].root
,
642 sup_list
, XA_ATOM
, 32,
643 PropModeAppend
, (unsigned char *)&ewmh
[j
].atom
,1);
651 unsigned char *data
= NULL
;
653 Atom sup_check
, sup_list
;
656 sup_check
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
657 sup_list
= XInternAtom(display
, "_NET_SUPPORTED", False
);
659 for (i
= 0; i
< ScreenCount(display
); i
++) {
660 /* Get the support check window and destroy it */
661 success
= get_property(screens
[i
].root
, sup_check
, 1, XA_WINDOW
,
666 XDestroyWindow(display
, id
);
667 XDeleteProperty(display
, screens
[i
].root
, sup_check
);
668 XDeleteProperty(display
, screens
[i
].root
, sup_list
);
676 ewmh_autoquirk(struct ws_win
*win
)
679 unsigned long *data
= NULL
, n
;
682 success
= get_property(win
->id
, ewmh
[_NET_WM_WINDOW_TYPE
].atom
, (~0L),
683 XA_ATOM
, &n
, (void *)&data
);
690 for (i
= 0; i
< n
; i
++) {
692 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
694 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
695 type
== ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
696 type
== ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
698 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
701 if (type
== ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
702 type
== ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
704 win
->quirks
= SWM_Q_FLOAT
;
712 #define SWM_EWMH_ACTION_COUNT_MAX (6)
713 #define EWMH_F_FULLSCREEN (1<<0)
714 #define EWMH_F_ABOVE (1<<1)
715 #define EWMH_F_HIDDEN (1<<2)
716 #define EWMH_F_SKIP_PAGER (1<<3)
717 #define EWMH_F_SKIP_TASKBAR (1<<4)
718 #define SWM_F_MANUAL (1<<5)
721 ewmh_set_win_fullscreen(struct ws_win
*win
, int fs
)
723 struct swm_geometry rg
;
731 DNPRINTF(SWM_D_MISC
, "ewmh_set_win_fullscreen: window: 0x%lx, "
732 "fullscreen %s\n", win
->id
, YESNO(fs
));
737 store_float_geom(win
, win
->ws
->r
);
741 if (win
->g_floatvalid
) {
742 /* refloat at last floating relative position */
743 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ rg
.x
;
744 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ rg
.y
;
745 WIDTH(win
) = win
->g_float
.w
;
746 HEIGHT(win
) = win
->g_float
.h
;
754 ewmh_update_actions(struct ws_win
*win
)
756 Atom actions
[SWM_EWMH_ACTION_COUNT_MAX
];
762 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
765 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
766 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
769 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
,
770 XA_ATOM
, 32, PropModeReplace
, (unsigned char *)actions
, n
);
773 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
774 #define _NET_WM_STATE_ADD 1 /* add/set property */
775 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
778 ewmh_update_win_state(struct ws_win
*win
, long state
, long action
)
780 unsigned int mask
= 0;
781 unsigned int changed
= 0;
782 unsigned int orig_flags
;
787 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
788 mask
= EWMH_F_FULLSCREEN
;
789 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
791 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
793 if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
794 mask
= EWMH_F_SKIP_PAGER
;
795 if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
796 mask
= EWMH_F_SKIP_TASKBAR
;
799 orig_flags
= win
->ewmh_flags
;
802 case _NET_WM_STATE_REMOVE
:
803 win
->ewmh_flags
&= ~mask
;
805 case _NET_WM_STATE_ADD
:
806 win
->ewmh_flags
|= mask
;
808 case _NET_WM_STATE_TOGGLE
:
809 win
->ewmh_flags
^= mask
;
813 changed
= (win
->ewmh_flags
& mask
) ^ (orig_flags
& mask
) ? 1 : 0;
815 if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
817 if (!floating_toggle_win(win
))
818 win
->ewmh_flags
= orig_flags
; /* revert */
819 if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
821 win
->manual
= (win
->ewmh_flags
& SWM_F_MANUAL
) != 0;
822 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
824 if (!ewmh_set_win_fullscreen(win
,
825 win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
826 win
->ewmh_flags
= orig_flags
; /* revert */
828 XDeleteProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
830 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
)
831 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
832 XA_ATOM
, 32, PropModeAppend
,
833 (unsigned char *)&ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
, 1);
834 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
835 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
836 XA_ATOM
, 32, PropModeAppend
,
837 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
, 1);
838 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
839 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
840 XA_ATOM
, 32, PropModeAppend
,
841 (unsigned char *)&ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
, 1);
842 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
843 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
844 XA_ATOM
, 32, PropModeAppend
,
845 (unsigned char *)&ewmh
[_NET_WM_STATE_ABOVE
].atom
, 1);
846 if (win
->ewmh_flags
& SWM_F_MANUAL
)
847 XChangeProperty(display
, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
848 XA_ATOM
, 32, PropModeAppend
,
849 (unsigned char *)&ewmh
[_SWM_WM_STATE_MANUAL
].atom
, 1);
853 ewmh_get_win_state(struct ws_win
*win
)
864 win
->ewmh_flags
|= EWMH_F_ABOVE
;
866 win
->ewmh_flags
|= SWM_F_MANUAL
;
868 success
= get_property(win
->id
, ewmh
[_NET_WM_STATE
].atom
,
869 (~0L), XA_ATOM
, &n
, (void *)&states
);
874 for (i
= 0; i
< n
; i
++)
875 ewmh_update_win_state(win
, states
[i
], _NET_WM_STATE_ADD
);
883 geteventname(XEvent
*e
)
895 name
= "ButtonPress";
898 name
= "ButtonRelease";
901 name
= "MotionNotify";
904 name
= "EnterNotify";
907 name
= "LeaveNotify";
916 name
= "KeymapNotify";
922 name
= "GraphicsExpose";
927 case VisibilityNotify
:
928 name
= "VisibilityNotify";
931 name
= "CreateNotify";
934 name
= "DestroyNotify";
937 name
= "UnmapNotify";
946 name
= "ReparentNotify";
948 case ConfigureNotify
:
949 name
= "ConfigureNotify";
951 case ConfigureRequest
:
952 name
= "ConfigureRequest";
955 name
= "GravityNotify";
958 name
= "ResizeRequest";
960 case CirculateNotify
:
961 name
= "CirculateNotify";
963 case CirculateRequest
:
964 name
= "CirculateRequest";
967 name
= "PropertyNotify";
970 name
= "SelectionClear";
972 case SelectionRequest
:
973 name
= "SelectionRequest";
975 case SelectionNotify
:
976 name
= "SelectionNotify";
979 name
= "ColormapNotify";
982 name
= "ClientMessage";
985 name
= "MappingNotify";
995 xrandr_geteventname(XEvent
*e
)
999 switch(e
->type
- xrandr_eventbase
) {
1000 case RRScreenChangeNotify
:
1001 name
= "RRScreenChangeNotify";
1011 dumpwins(struct swm_region
*r
, union arg
*args
)
1015 XWindowAttributes wa
;
1017 if (r
->ws
== NULL
) {
1018 fprintf(stderr
, "dumpwins: invalid workspace\n");
1022 fprintf(stderr
, "=== managed window list ws %02d ===\n", r
->ws
->idx
);
1024 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1025 state
= getstate(win
->id
);
1026 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1027 fprintf(stderr
, "window: 0x%lx, failed "
1028 "XGetWindowAttributes\n", win
->id
);
1029 fprintf(stderr
, "window: 0x%lx, map_state: %d, state: %d, "
1030 "transient: 0x%lx\n",
1031 win
->id
, wa
.map_state
, state
, win
->transient
);
1034 fprintf(stderr
, "===== unmanaged window list =====\n");
1035 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1036 state
= getstate(win
->id
);
1037 if (!XGetWindowAttributes(display
, win
->id
, &wa
))
1038 fprintf(stderr
, "window: 0x%lx, failed "
1039 "XGetWindowAttributes\n", win
->id
);
1040 fprintf(stderr
, "window: 0x%lx, map_state: %d, state: %d, "
1041 "transient: 0x%lx\n",
1042 win
->id
, wa
.map_state
, state
, win
->transient
);
1045 fprintf(stderr
, "=================================\n");
1049 dumpwins(struct swm_region
*r
, union arg
*args
)
1052 #endif /* SWM_DEBUG */
1054 void expose(XEvent
*);
1055 void keypress(XEvent
*);
1056 void buttonpress(XEvent
*);
1057 void configurerequest(XEvent
*);
1058 void configurenotify(XEvent
*);
1059 void destroynotify(XEvent
*);
1060 void enternotify(XEvent
*);
1061 void focusevent(XEvent
*);
1062 void mapnotify(XEvent
*);
1063 void mappingnotify(XEvent
*);
1064 void maprequest(XEvent
*);
1065 void propertynotify(XEvent
*);
1066 void unmapnotify(XEvent
*);
1067 void visibilitynotify(XEvent
*);
1068 void clientmessage(XEvent
*);
1070 void (*handler
[LASTEvent
])(XEvent
*) = {
1072 [KeyPress
] = keypress
,
1073 [ButtonPress
] = buttonpress
,
1074 [ConfigureRequest
] = configurerequest
,
1075 [ConfigureNotify
] = configurenotify
,
1076 [DestroyNotify
] = destroynotify
,
1077 [EnterNotify
] = enternotify
,
1078 [FocusIn
] = focusevent
,
1079 [FocusOut
] = focusevent
,
1080 [MapNotify
] = mapnotify
,
1081 [MappingNotify
] = mappingnotify
,
1082 [MapRequest
] = maprequest
,
1083 [PropertyNotify
] = propertynotify
,
1084 [UnmapNotify
] = unmapnotify
,
1085 [VisibilityNotify
] = visibilitynotify
,
1086 [ClientMessage
] = clientmessage
,
1092 int saved_errno
, status
;
1095 saved_errno
= errno
;
1099 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1104 if (errno
!= ECHILD
)
1105 warn("sighdlr: waitpid");
1106 #endif /* SWM_DEBUG */
1109 if (pid
== searchpid
)
1113 if (WIFEXITED(status
)) {
1114 if (WEXITSTATUS(status
) != 0)
1115 warnx("sighdlr: child exit status: %d",
1116 WEXITSTATUS(status
));
1118 warnx("sighdlr: child is terminated "
1120 #endif /* SWM_DEBUG */
1134 errno
= saved_errno
;
1140 struct pid_e
*p
= NULL
;
1142 DNPRINTF(SWM_D_MISC
, "find_pid: %lu\n", pid
);
1147 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1156 name_to_color(char *colorname
)
1160 XColor screen_def
, exact_def
;
1161 unsigned long result
= 0;
1162 char cname
[32] = "#";
1164 cmap
= DefaultColormap(display
, screens
[0].idx
);
1165 status
= XAllocNamedColor(display
, cmap
, colorname
,
1166 &screen_def
, &exact_def
);
1168 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1169 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
1173 result
= screen_def
.pixel
;
1175 fprintf(stderr
, "color '%s' not found.\n", colorname
);
1181 setscreencolor(char *val
, int i
, int c
)
1183 if (i
> 0 && i
<= ScreenCount(display
)) {
1184 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
1185 free(screens
[i
- 1].c
[c
].name
);
1186 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1188 } else if (i
== -1) {
1189 for (i
= 0; i
< ScreenCount(display
); i
++) {
1190 screens
[i
].c
[c
].color
= name_to_color(val
);
1191 free(screens
[i
].c
[c
].name
);
1192 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1196 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1197 i
, ScreenCount(display
));
1201 fancy_stacker(struct workspace
*ws
)
1203 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1204 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1205 snprintf(ws
->stacker
, sizeof ws
->stacker
, "[%d|%d]",
1206 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1207 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1208 snprintf(ws
->stacker
, sizeof ws
->stacker
, "[%d-%d]",
1209 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1213 plain_stacker(struct workspace
*ws
)
1215 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1216 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1217 strlcpy(ws
->stacker
, "[|]", sizeof ws
->stacker
);
1218 if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1219 strlcpy(ws
->stacker
, "[-]", sizeof ws
->stacker
);
1223 custom_region(char *val
)
1225 unsigned int sidx
, x
, y
, w
, h
;
1227 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1228 errx(1, "invalid custom region, "
1229 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1230 if (sidx
< 1 || sidx
> ScreenCount(display
))
1231 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1232 sidx
, ScreenCount(display
));
1236 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1238 if (x
> DisplayWidth(display
, sidx
) ||
1239 y
> DisplayHeight(display
, sidx
) ||
1240 w
+ x
> DisplayWidth(display
, sidx
) ||
1241 h
+ y
> DisplayHeight(display
, sidx
)) {
1242 fprintf(stderr
, "ignoring region %ux%u+%u+%u "
1243 "- not within screen boundaries "
1244 "(%ux%u)\n", w
, h
, x
, y
,
1245 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
1249 new_region(&screens
[sidx
], x
, y
, w
, h
);
1253 socket_setnonblock(int fd
)
1257 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1258 err(1, "fcntl F_GETFL");
1259 flags
|= O_NONBLOCK
;
1260 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1261 err(1, "fcntl F_SETFL");
1265 bar_print(struct swm_region
*r
, char *s
)
1269 XRectangle ibox
, lbox
;
1271 XClearWindow(display
, r
->bar_window
);
1274 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1276 switch (bar_justify
) {
1277 case SWM_BAR_JUSTIFY_LEFT
:
1280 case SWM_BAR_JUSTIFY_CENTER
:
1281 x
= (WIDTH(r
) - lbox
.width
) / 2;
1283 case SWM_BAR_JUSTIFY_RIGHT
:
1284 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1288 if (x
< SWM_BAR_OFFSET
)
1291 DRAWSTRING(display
, r
->bar_window
, bar_fs
, r
->s
->bar_gc
,
1292 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1297 bar_extra_stop(void)
1301 bzero(bar_pipe
, sizeof bar_pipe
);
1304 kill(bar_pid
, SIGTERM
);
1307 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1312 bar_class_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1314 int do_class
, do_name
;
1316 XClassHint
*xch
= NULL
;
1318 if ((title_name_enabled
== 1 || title_class_enabled
== 1) &&
1319 cur_focus
!= NULL
) {
1320 if ((xch
= XAllocClassHint()) == NULL
)
1322 status
= XGetClassHint(display
, cur_focus
->id
, xch
);
1323 if (status
== BadWindow
|| status
== BadAlloc
)
1325 do_class
= (title_class_enabled
&& xch
->res_class
!= NULL
);
1326 do_name
= (title_name_enabled
&& xch
->res_name
!= NULL
);
1328 strlcat(s
, xch
->res_class
, sz
);
1329 if (do_class
&& do_name
)
1330 strlcat(s
, ":", sz
);
1332 strlcat(s
, xch
->res_name
, sz
);
1333 strlcat(s
, " ", sz
);
1337 XFree(xch
->res_name
);
1338 XFree(xch
->res_class
);
1344 bar_window_name(char *s
, ssize_t sz
, struct ws_win
*cur_focus
)
1346 unsigned char *title
;
1349 if (window_name_enabled
&& cur_focus
!= NULL
) {
1350 if ((title
= get_win_name(display
, cur_focus
->id
, a_netwmname
,
1351 a_utf8_string
, &len
)) != NULL
) {
1352 DNPRINTF(SWM_D_BAR
, "bar_window_name: title: %s\n",
1355 if (cur_focus
->floating
)
1356 strlcat(s
, "(f) ", sz
);
1357 strlcat(s
, (char *)title
, sz
);
1358 strlcat(s
, " ", sz
);
1364 int urgent
[SWM_WS_MAX
];
1366 bar_urgent(char *s
, ssize_t sz
)
1368 XWMHints
*wmh
= NULL
;
1373 if (urgent_enabled
== 0)
1376 for (i
= 0; i
< SWM_WS_MAX
; i
++)
1379 for (i
= 0; i
< ScreenCount(display
); i
++)
1380 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1381 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1382 wmh
= XGetWMHints(display
, win
->id
);
1386 if (wmh
->flags
& XUrgencyHint
)
1391 strlcat(s
, "* ", sz
);
1392 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
1394 snprintf(b
, sizeof b
, "%d ", i
+ 1);
1396 snprintf(b
, sizeof b
, "- ");
1399 strlcat(s
, "* ", sz
);
1407 struct swm_region
*r
;
1410 char ws
[SWM_BAR_MAX
];
1411 char s
[SWM_BAR_MAX
];
1412 unsigned char cn
[SWM_BAR_MAX
];
1413 char loc
[SWM_BAR_MAX
];
1414 char *b
, *stack
= "";
1416 if (bar_enabled
== 0)
1418 if (bar_extra
&& bar_extra_running
) {
1419 /* ignore short reads; it'll correct itself */
1420 while ((b
= fgetln(stdin
, &len
)) != NULL
)
1421 if (b
&& b
[len
- 1] == '\n') {
1423 strlcpy((char *)bar_ext
, b
, sizeof bar_ext
);
1425 if (b
== NULL
&& errno
!= EAGAIN
) {
1426 fprintf(stderr
, "bar_update: bar_extra failed: "
1427 "errno: %d %s\n", errno
, strerror(errno
));
1431 strlcpy((char *)bar_ext
, "", sizeof bar_ext
);
1433 if (clock_enabled
== 0)
1434 strlcpy(s
, "", sizeof s
);
1437 localtime_r(&tmt
, &tm
);
1438 strftime(s
, sizeof s
, clock_format
, &tm
);
1439 strlcat(s
, " ", sizeof s
);
1442 for (i
= 0; i
< ScreenCount(display
); i
++) {
1444 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1445 strlcpy((char *)cn
, "", sizeof cn
);
1446 strlcpy(ws
, "", sizeof ws
);
1448 bar_urgent((char *)cn
, sizeof cn
);
1449 bar_class_name((char *)cn
, sizeof cn
,
1451 bar_window_name((char *)cn
, sizeof cn
,
1454 snprintf(ws
, sizeof ws
, "<%s>",
1458 stack
= r
->ws
->stacker
;
1460 snprintf(loc
, sizeof loc
, "%d:%d %s %s %s%s %s "
1461 "%s", x
++, r
->ws
->idx
+ 1, stack
, ws
, s
, cn
,
1462 bar_ext
, bar_vertext
);
1476 bar_toggle(struct swm_region
*r
, union arg
*args
)
1478 struct swm_region
*tmpr
;
1479 int i
, sc
= ScreenCount(display
);
1481 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
1484 for (i
= 0; i
< sc
; i
++)
1485 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1486 XUnmapWindow(display
, tmpr
->bar_window
);
1488 for (i
= 0; i
< sc
; i
++)
1489 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
1490 XMapRaised(display
, tmpr
->bar_window
);
1492 bar_enabled
= !bar_enabled
;
1495 /* must be after stack */
1502 XSetWindowAttributes wa
;
1503 struct swm_region
*r
;
1506 /* do this here because the conf file is in memory */
1507 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
1508 /* launch external status app */
1509 bar_extra_running
= 1;
1510 if (pipe(bar_pipe
) == -1)
1511 err(1, "pipe error");
1512 socket_setnonblock(bar_pipe
[0]);
1513 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
1514 if (dup2(bar_pipe
[0], 0) == -1)
1516 if (dup2(bar_pipe
[1], 1) == -1)
1518 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1519 err(1, "could not disable SIGPIPE");
1520 switch (bar_pid
= fork()) {
1522 err(1, "cannot fork");
1526 execvp(bar_argv
[0], bar_argv
);
1527 err(1, "%s external app failed", bar_argv
[0]);
1529 default: /* parent */
1535 bzero(&wa
, sizeof wa
);
1536 for (i
= 0; i
< ScreenCount(display
); i
++)
1537 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1539 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
1540 wa
.background_pixel
=
1541 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
1542 XChangeWindowAttributes(display
, r
->bar_window
,
1543 CWBackPixel
| CWBorderPixel
, &wa
);
1549 bar_setup(struct swm_region
*r
)
1551 char *default_string
;
1552 char **missing_charsets
;
1553 int num_missing_charsets
= 0;
1557 XFreeFontSet(display
, bar_fs
);
1562 DNPRINTF(SWM_D_BAR
, "bar_setup: loading bar_fonts: %s\n", bar_fonts
);
1564 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
1565 &num_missing_charsets
, &default_string
);
1567 if (num_missing_charsets
> 0) {
1568 warnx("Unable to load charset(s):");
1570 for (i
= 0; i
< num_missing_charsets
; ++i
)
1571 warnx("%s", missing_charsets
[i
]);
1573 XFreeStringList(missing_charsets
);
1575 if (strcmp(default_string
, ""))
1576 warnx("Glyphs from those sets will be replaced "
1577 "by '%s'.", default_string
);
1579 warnx("Glyphs from those sets won't be drawn.");
1583 errx(1, "Error creating font set structure.");
1585 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
1587 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
1588 2 * bar_border_width
;
1594 y
= bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
1596 r
->bar_window
= XCreateSimpleWindow(display
,
1597 r
->s
->root
, x
, y
, WIDTH(r
) - 2 * bar_border_width
,
1598 bar_height
- 2 * bar_border_width
,
1599 bar_border_width
, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
1600 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
1601 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
1603 XMapRaised(display
, r
->bar_window
);
1604 DNPRINTF(SWM_D_BAR
, "bar_setup: bar_window: 0x%lx\n", r
->bar_window
);
1606 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
1607 err(1, "could not install bar_signal");
1612 drain_enter_notify(void)
1617 while (XCheckMaskEvent(display
, EnterWindowMask
, &cne
))
1620 DNPRINTF(SWM_D_MISC
, "drain_enter_notify: drained: %d\n", i
);
1624 set_win_state(struct ws_win
*win
, long state
)
1626 long data
[] = {state
, None
};
1628 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: 0x%lx\n", win
->id
);
1633 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1634 (unsigned char *)data
, 2);
1641 unsigned char *p
= NULL
;
1644 if (!get_property(w
, astate
, 2L, astate
, &n
, &p
))
1647 result
= *((long *)p
);
1653 version(struct swm_region
*r
, union arg
*args
)
1655 bar_version
= !bar_version
;
1657 snprintf(bar_vertext
, sizeof bar_vertext
,
1658 "Version: %s Build: %s", SCROTWM_VERSION
, buildstr
);
1660 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
1665 client_msg(struct ws_win
*win
, Atom a
)
1667 XClientMessageEvent cm
;
1672 bzero(&cm
, sizeof cm
);
1673 cm
.type
= ClientMessage
;
1674 cm
.window
= win
->id
;
1675 cm
.message_type
= aprot
;
1678 cm
.data
.l
[1] = CurrentTime
;
1679 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
1682 /* synthetic response to a ConfigureRequest when not making a change */
1684 config_win(struct ws_win
*win
, XConfigureRequestEvent
*ev
)
1691 /* send notification of unchanged state. */
1692 ce
.type
= ConfigureNotify
;
1695 ce
.width
= WIDTH(win
);
1696 ce
.height
= HEIGHT(win
);
1697 ce
.override_redirect
= False
;
1701 ce
.display
= display
;
1703 ce
.window
= win
->id
;
1704 ce
.border_width
= border_width
;
1708 ce
.display
= ev
->display
;
1709 ce
.event
= ev
->window
;
1710 ce
.window
= ev
->window
;
1711 /* adjust x and y for requested border_width. */
1712 ce
.x
+= border_width
- ev
->border_width
;
1713 ce
.y
+= border_width
- ev
->border_width
;
1714 ce
.border_width
= ev
->border_width
;
1715 ce
.above
= ev
->above
;
1718 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
1719 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
1720 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
1722 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
1726 count_win(struct workspace
*ws
, int count_transient
)
1731 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1732 if (count_transient
== 0 && win
->floating
)
1734 if (count_transient
== 0 && win
->transient
)
1740 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
1746 quit(struct swm_region
*r
, union arg
*args
)
1748 DNPRINTF(SWM_D_MISC
, "quit\n");
1753 unmap_window(struct ws_win
*win
)
1758 /* don't unmap again */
1759 if (getstate(win
->id
) == IconicState
)
1762 set_win_state(win
, IconicState
);
1764 XUnmapWindow(display
, win
->id
);
1765 XSetWindowBorder(display
, win
->id
,
1766 win
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1775 for (i
= 0; i
< ScreenCount(display
); i
++)
1776 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1777 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1782 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
1789 event
.display
= display
; /* Ignored, but what the hell */
1790 event
.window
= win
->id
;
1791 event
.root
= win
->s
->root
;
1792 event
.subwindow
= None
;
1793 event
.time
= CurrentTime
;
1798 event
.same_screen
= True
;
1799 event
.keycode
= XKeysymToKeycode(display
, keysym
);
1800 event
.state
= modifiers
;
1802 event
.type
= KeyPress
;
1803 XSendEvent(event
.display
, event
.window
, True
,
1804 KeyPressMask
, (XEvent
*)&event
);
1806 event
.type
= KeyRelease
;
1807 XSendEvent(event
.display
, event
.window
, True
,
1808 KeyPressMask
, (XEvent
*)&event
);
1813 restart(struct swm_region
*r
, union arg
*args
)
1815 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
1817 /* disable alarm because the following code may not be interrupted */
1819 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
1820 err(1, "can't disable alarm");
1825 XCloseDisplay(display
);
1826 execvp(start_argv
[0], start_argv
);
1827 fprintf(stderr
, "execvp failed\n");
1833 root_to_region(Window root
)
1835 struct swm_region
*r
= NULL
;
1837 int i
, x
, y
, wx
, wy
;
1840 for (i
= 0; i
< ScreenCount(display
); i
++)
1841 if (screens
[i
].root
== root
)
1844 if (XQueryPointer(display
, screens
[i
].root
,
1845 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
1846 /* choose a region based on pointer location */
1847 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1848 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
1849 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
1854 r
= TAILQ_FIRST(&screens
[i
].rl
);
1860 find_unmanaged_window(Window id
)
1865 for (i
= 0; i
< ScreenCount(display
); i
++)
1866 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1867 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
1875 find_window(Window id
)
1878 Window wrr
, wpr
, *wcr
= NULL
;
1882 for (i
= 0; i
< ScreenCount(display
); i
++)
1883 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1884 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1888 /* if we were looking for the parent return that window instead */
1889 if (XQueryTree(display
, id
, &wrr
, &wpr
, &wcr
, &nc
) == 0)
1894 /* ignore not found and root */
1895 if (wpr
== 0 || wrr
== wpr
)
1898 /* look for parent */
1899 for (i
= 0; i
< ScreenCount(display
); i
++)
1900 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1901 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1909 spawn(int ws_idx
, union arg
*args
, int close_fd
)
1914 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
1917 close(ConnectionNumber(display
));
1919 setenv("LD_PRELOAD", SWM_LIB
, 1);
1921 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
1925 setenv("_SWM_WS", ret
, 1);
1929 if (asprintf(&ret
, "%d", getpid()) == -1) {
1933 setenv("_SWM_PID", ret
, 1);
1937 if (setsid() == -1) {
1944 * close stdin and stdout to prevent interaction between apps
1945 * and the baraction script
1946 * leave stderr open to record errors
1948 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
1952 dup2(fd
, STDIN_FILENO
);
1953 dup2(fd
, STDOUT_FILENO
);
1958 execvp(args
->argv
[0], args
->argv
);
1965 spawnterm(struct swm_region
*r
, union arg
*args
)
1967 DNPRINTF(SWM_D_MISC
, "spawnterm\n");
1971 setenv("_SWM_XTERM_FONTADJ", "", 1);
1972 spawn(r
->ws
->idx
, args
, 1);
1977 kill_refs(struct ws_win
*win
)
1980 struct swm_region
*r
;
1981 struct workspace
*ws
;
1986 for (i
= 0; i
< ScreenCount(display
); i
++)
1987 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1988 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
1990 if (win
== ws
->focus
)
1992 if (win
== ws
->focus_prev
)
1993 ws
->focus_prev
= NULL
;
1998 validate_win(struct ws_win
*testwin
)
2001 struct workspace
*ws
;
2002 struct swm_region
*r
;
2005 if (testwin
== NULL
)
2008 for (i
= 0; i
< ScreenCount(display
); i
++)
2009 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2010 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
2012 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2020 validate_ws(struct workspace
*testws
)
2022 struct swm_region
*r
;
2023 struct workspace
*ws
;
2026 /* validate all ws */
2027 for (i
= 0; i
< ScreenCount(display
); i
++)
2028 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2029 for (x
= 0; x
< SWM_WS_MAX
; x
++) {
2038 unfocus_win(struct ws_win
*win
)
2043 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: window: 0x%lx\n", WINID(win
));
2047 if (win
->ws
== NULL
)
2050 if (validate_ws(win
->ws
))
2051 return; /* XXX this gets hit with thunderbird, needs fixing */
2053 if (win
->ws
->r
== NULL
)
2056 if (validate_win(win
)) {
2061 if (win
->ws
->focus
== win
) {
2062 win
->ws
->focus
= NULL
;
2063 win
->ws
->focus_prev
= win
;
2066 if (validate_win(win
->ws
->focus
)) {
2067 kill_refs(win
->ws
->focus
);
2068 win
->ws
->focus
= NULL
;
2070 if (validate_win(win
->ws
->focus_prev
)) {
2071 kill_refs(win
->ws
->focus_prev
);
2072 win
->ws
->focus_prev
= NULL
;
2075 /* drain all previous unfocus events */
2076 while (XCheckTypedEvent(display
, FocusOut
, &cne
) == True
)
2079 grabbuttons(win
, 0);
2080 XSetWindowBorder(display
, win
->id
,
2081 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2083 XChangeProperty(display
, win
->s
->root
,
2084 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2085 PropModeReplace
, (unsigned char *)&none
,1);
2094 DNPRINTF(SWM_D_FOCUS
, "unfocus_all\n");
2096 for (i
= 0; i
< ScreenCount(display
); i
++)
2097 for (j
= 0; j
< SWM_WS_MAX
; j
++)
2098 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2103 focus_win(struct ws_win
*win
)
2108 struct ws_win
*cfw
= NULL
;
2111 DNPRINTF(SWM_D_FOCUS
, "focus_win: window: 0x%lx\n", WINID(win
));
2115 if (win
->ws
== NULL
)
2118 if (validate_ws(win
->ws
))
2119 return; /* XXX this gets hit with thunderbird, needs fixing */
2121 if (validate_win(win
)) {
2126 if (validate_win(win
)) {
2131 XGetInputFocus(display
, &cur_focus
, &rr
);
2132 if ((cfw
= find_window(cur_focus
)) != NULL
)
2135 /* use larger hammer since the window was killed somehow */
2136 TAILQ_FOREACH(cfw
, &win
->ws
->winlist
, entry
)
2137 if (cfw
->ws
&& cfw
->ws
->r
&& cfw
->ws
->r
->s
)
2138 XSetWindowBorder(display
, cfw
->id
,
2139 cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
2142 win
->ws
->focus
= win
;
2144 if (win
->ws
->r
!= NULL
) {
2145 /* drain all previous focus events */
2146 while (XCheckTypedEvent(display
, FocusIn
, &cne
) == True
)
2150 XSetInputFocus(display
, win
->id
,
2151 RevertToParent
, CurrentTime
);
2152 grabbuttons(win
, 1);
2153 XSetWindowBorder(display
, win
->id
,
2154 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
2155 if (win
->ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
2156 win
->ws
->always_raise
)
2157 XMapRaised(display
, win
->id
);
2159 XChangeProperty(display
, win
->s
->root
,
2160 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XA_WINDOW
, 32,
2161 PropModeReplace
, (unsigned char *)&win
->id
,1);
2164 if (window_name_enabled
)
2169 switchws(struct swm_region
*r
, union arg
*args
)
2171 int wsid
= args
->id
, unmap_old
= 0;
2172 struct swm_region
*this_r
, *other_r
;
2174 struct workspace
*new_ws
, *old_ws
;
2181 old_ws
= this_r
->ws
;
2182 new_ws
= &this_r
->s
->ws
[wsid
];
2184 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2185 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
2187 if (new_ws
== NULL
|| old_ws
== NULL
)
2189 if (new_ws
== old_ws
)
2192 other_r
= new_ws
->r
;
2193 if (other_r
== NULL
) {
2194 /* the other workspace is hidden, hide this one */
2198 /* the other ws is visible in another region, exchange them */
2199 other_r
->ws_prior
= new_ws
;
2200 other_r
->ws
= old_ws
;
2201 old_ws
->r
= other_r
;
2203 this_r
->ws_prior
= old_ws
;
2204 this_r
->ws
= new_ws
;
2207 /* this is needed so that we can click on a window after a restart */
2211 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2212 focus(new_ws
->r
, &a
);
2216 /* unmap old windows */
2218 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
2221 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2222 drain_enter_notify();
2226 cyclews(struct swm_region
*r
, union arg
*args
)
2229 struct swm_screen
*s
= r
->s
;
2232 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2233 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2238 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
2241 case SWM_ARG_ID_CYCLEWS_UP
:
2242 if (a
.id
< SWM_WS_MAX
- 1)
2247 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
2250 case SWM_ARG_ID_CYCLEWS_DOWN
:
2254 a
.id
= SWM_WS_MAX
- 1;
2261 (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
2263 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
2267 } while (a
.id
!= r
->ws
->idx
);
2271 priorws(struct swm_region
*r
, union arg
*args
)
2275 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2276 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2278 if (r
->ws_prior
== NULL
)
2281 a
.id
= r
->ws_prior
->idx
;
2286 cyclescr(struct swm_region
*r
, union arg
*args
)
2288 struct swm_region
*rr
= NULL
;
2292 /* do nothing if we don't have more than one screen */
2293 if (!(ScreenCount(display
) > 1 || outputs
> 1))
2298 case SWM_ARG_ID_CYCLESC_UP
:
2299 rr
= TAILQ_NEXT(r
, entry
);
2301 rr
= TAILQ_FIRST(&screens
[i
].rl
);
2303 case SWM_ARG_ID_CYCLESC_DOWN
:
2304 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
2306 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
2314 /* move mouse to region */
2316 y
= Y(rr
) + 1 + (bar_enabled
? bar_height
: 0);
2317 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2319 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2322 if (rr
->ws
->focus
) {
2323 /* move to focus window */
2324 x
= X(rr
->ws
->focus
) + 1;
2325 y
= Y(rr
->ws
->focus
) + 1;
2326 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, x
, y
);
2331 sort_windows(struct ws_win_list
*wl
)
2333 struct ws_win
*win
, *parent
, *nxt
;
2338 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
2339 nxt
= TAILQ_NEXT(win
, entry
);
2340 if (win
->transient
) {
2341 parent
= find_window(win
->transient
);
2342 if (parent
== NULL
) {
2343 fprintf(stderr
, "not possible bug\n");
2346 TAILQ_REMOVE(wl
, win
, entry
);
2347 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
2354 swapwin(struct swm_region
*r
, union arg
*args
)
2356 struct ws_win
*target
, *source
;
2357 struct ws_win
*cur_focus
;
2358 struct ws_win_list
*wl
;
2361 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2362 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
2364 cur_focus
= r
->ws
->focus
;
2365 if (cur_focus
== NULL
)
2369 wl
= &source
->ws
->winlist
;
2372 case SWM_ARG_ID_SWAPPREV
:
2373 if (source
->transient
)
2374 source
= find_window(source
->transient
);
2375 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
2376 if (target
&& target
->transient
)
2377 target
= find_window(target
->transient
);
2378 TAILQ_REMOVE(wl
, source
, entry
);
2380 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2382 TAILQ_INSERT_BEFORE(target
, source
, entry
);
2384 case SWM_ARG_ID_SWAPNEXT
:
2385 target
= TAILQ_NEXT(source
, entry
);
2386 /* move the parent and let the sort handle the move */
2387 if (source
->transient
)
2388 source
= find_window(source
->transient
);
2389 TAILQ_REMOVE(wl
, source
, entry
);
2391 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2393 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
2395 case SWM_ARG_ID_SWAPMAIN
:
2396 target
= TAILQ_FIRST(wl
);
2397 if (target
== source
) {
2398 if (source
->ws
->focus_prev
!= NULL
&&
2399 source
->ws
->focus_prev
!= target
)
2401 source
= source
->ws
->focus_prev
;
2405 if (target
== NULL
|| source
== NULL
)
2407 source
->ws
->focus_prev
= target
;
2408 TAILQ_REMOVE(wl
, target
, entry
);
2409 TAILQ_INSERT_BEFORE(source
, target
, entry
);
2410 TAILQ_REMOVE(wl
, source
, entry
);
2411 TAILQ_INSERT_HEAD(wl
, source
, entry
);
2413 case SWM_ARG_ID_MOVELAST
:
2414 TAILQ_REMOVE(wl
, source
, entry
);
2415 TAILQ_INSERT_TAIL(wl
, source
, entry
);
2418 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
2428 focus_prev(struct ws_win
*win
)
2430 struct ws_win
*winfocus
= NULL
, *winlostfocus
= NULL
;
2431 struct ws_win
*cur_focus
= NULL
;
2432 struct ws_win_list
*wl
= NULL
;
2433 struct workspace
*ws
= NULL
;
2435 DNPRINTF(SWM_D_FOCUS
, "focus_prev: window: 0x%lx\n", WINID(win
));
2437 if (!(win
&& win
->ws
))
2442 cur_focus
= ws
->focus
;
2443 winlostfocus
= cur_focus
;
2445 /* pickle, just focus on whatever */
2446 if (cur_focus
== NULL
) {
2447 /* use prev_focus if valid */
2448 if (ws
->focus_prev
&& ws
->focus_prev
!= cur_focus
&&
2449 find_window(WINID(ws
->focus_prev
)))
2450 winfocus
= ws
->focus_prev
;
2451 if (winfocus
== NULL
)
2452 winfocus
= TAILQ_FIRST(wl
);
2456 /* if transient focus on parent */
2457 if (cur_focus
->transient
) {
2458 winfocus
= find_window(cur_focus
->transient
);
2462 /* if in max_stack try harder */
2463 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
2464 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
2465 if (cur_focus
!= ws
->focus_prev
)
2466 winfocus
= ws
->focus_prev
;
2467 else if (cur_focus
!= ws
->focus
)
2468 winfocus
= ws
->focus
;
2470 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2475 if (cur_focus
== win
)
2476 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2477 if (winfocus
== NULL
)
2478 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2479 if (winfocus
== NULL
|| winfocus
== win
)
2480 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
2482 if (winfocus
== winlostfocus
|| winfocus
== NULL
) {
2483 /* update the bar so that title/class/name will be cleared. */
2484 if (window_name_enabled
|| title_name_enabled
||
2485 title_class_enabled
)
2491 focus_magic(winfocus
);
2495 focus(struct swm_region
*r
, union arg
*args
)
2497 struct ws_win
*winfocus
= NULL
, *winlostfocus
= NULL
, *head
;
2498 struct ws_win
*cur_focus
= NULL
;
2499 struct ws_win_list
*wl
= NULL
;
2500 struct workspace
*ws
= NULL
;
2506 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
2508 /* treat FOCUS_CUR special */
2509 if (args
->id
== SWM_ARG_ID_FOCUSCUR
) {
2510 if (r
->ws
->focus
&& r
->ws
->focus
->iconic
== 0)
2511 winfocus
= r
->ws
->focus
;
2512 else if (r
->ws
->focus_prev
&& r
->ws
->focus_prev
->iconic
== 0)
2513 winfocus
= r
->ws
->focus_prev
;
2515 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
2516 if (winfocus
->iconic
== 0)
2519 focus_magic(winfocus
);
2523 if ((cur_focus
= r
->ws
->focus
) == NULL
)
2527 if (TAILQ_EMPTY(wl
))
2529 /* make sure there is at least one uniconified window */
2531 TAILQ_FOREACH(winfocus
, wl
, entry
)
2532 if (winfocus
->iconic
== 0) {
2539 winlostfocus
= cur_focus
;
2542 case SWM_ARG_ID_FOCUSPREV
:
2543 head
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
2545 head
= TAILQ_LAST(wl
, ws_win_list
);
2547 if (WINID(winfocus
) == cur_focus
->transient
) {
2548 head
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
2550 head
= TAILQ_LAST(wl
, ws_win_list
);
2555 if (winfocus
&& winfocus
->iconic
) {
2556 while (winfocus
!= cur_focus
) {
2557 if (winfocus
== NULL
)
2558 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
2559 if (winfocus
->iconic
== 0)
2561 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
2567 case SWM_ARG_ID_FOCUSNEXT
:
2568 head
= TAILQ_NEXT(cur_focus
, entry
);
2570 head
= TAILQ_FIRST(wl
);
2574 if (winfocus
&& winfocus
->iconic
) {
2575 while (winfocus
!= cur_focus
) {
2576 if (winfocus
== NULL
)
2577 winfocus
= TAILQ_FIRST(wl
);
2578 if (winfocus
->iconic
== 0)
2580 winfocus
= TAILQ_NEXT(winfocus
, entry
);
2585 case SWM_ARG_ID_FOCUSMAIN
:
2586 winfocus
= TAILQ_FIRST(wl
);
2587 if (winfocus
== cur_focus
)
2588 winfocus
= cur_focus
->ws
->focus_prev
;
2594 if (winfocus
== winlostfocus
|| winfocus
== NULL
) {
2595 /* update the bar so that title/class/name will be cleared. */
2596 if (window_name_enabled
|| title_name_enabled
||
2597 title_class_enabled
)
2603 focus_magic(winfocus
);
2607 cycle_layout(struct swm_region
*r
, union arg
*args
)
2609 struct workspace
*ws
= r
->ws
;
2612 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
2615 if (ws
->cur_layout
->l_stack
== NULL
)
2616 ws
->cur_layout
= &layouts
[0];
2619 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2620 drain_enter_notify();
2621 a
.id
= SWM_ARG_ID_FOCUSCUR
;
2627 stack_config(struct swm_region
*r
, union arg
*args
)
2629 struct workspace
*ws
= r
->ws
;
2631 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
2634 if (ws
->cur_layout
->l_config
!= NULL
)
2635 ws
->cur_layout
->l_config(ws
, args
->id
);
2637 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
2644 struct swm_geometry g
;
2645 struct swm_region
*r
;
2651 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
2653 for (i
= 0; i
< ScreenCount(display
); i
++) {
2657 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2658 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d "
2659 "(screen: %d, region: %d)\n", r
->ws
->idx
, i
, j
++);
2661 /* start with screen geometry, adjust for bar */
2663 g
.w
-= 2 * border_width
;
2664 g
.h
-= 2 * border_width
;
2670 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
2671 r
->ws
->cur_layout
->l_string(r
->ws
);
2672 /* save r so we can track region changes */
2679 if (focus_mode
== SWM_FOCUS_DEFAULT
)
2680 drain_enter_notify();
2682 DNPRINTF(SWM_D_STACK
, "stack: end\n");
2686 store_float_geom(struct ws_win
*win
, struct swm_region
*r
)
2688 /* retain window geom and region geom */
2689 win
->g_float
= win
->g
;
2690 win
->rg_float
= r
->g
;
2691 win
->g_floatvalid
= 1;
2695 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
2703 bzero(&wc
, sizeof wc
);
2704 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
2707 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2708 * geom on ws switches or return from max mode
2710 if (win
->floatmaxed
|| (r
!= r
->ws
->old_r
&& win
->g_floatvalid
2711 && !(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))) {
2713 * use stored g and rg to set relative position and size
2714 * as in old region or before max stack mode
2716 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ X(r
);
2717 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ Y(r
);
2718 WIDTH(win
) = win
->g_float
.w
;
2719 HEIGHT(win
) = win
->g_float
.h
;
2720 win
->g_floatvalid
= 0;
2723 win
->floatmaxed
= 0;
2725 if ((win
->quirks
& SWM_Q_FULLSCREEN
) && (WIDTH(win
) >= WIDTH(r
)) &&
2726 (HEIGHT(win
) >= HEIGHT(r
)))
2727 wc
.border_width
= 0;
2729 wc
.border_width
= border_width
;
2730 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
2731 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
2732 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
2737 * floaters and transients are auto-centred unless moved
2740 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 - wc
.border_width
;
2741 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 - wc
.border_width
;
2744 /* win can be outside r if new r smaller than old r */
2745 /* Ensure top left corner inside r (move probs otherwise) */
2746 if (X(win
) < X(r
) - wc
.border_width
)
2747 X(win
) = X(r
) - wc
.border_width
;
2748 if (X(win
) > X(r
) + WIDTH(r
) - 1)
2749 X(win
) = (WIDTH(win
) > WIDTH(r
)) ? X(r
) :
2750 (X(r
) + WIDTH(r
) - WIDTH(win
) - 2 * wc
.border_width
);
2751 if (Y(win
) < Y(r
) - wc
.border_width
)
2752 Y(win
) = Y(r
) - wc
.border_width
;
2753 if (Y(win
) > Y(r
) + HEIGHT(r
) - 1)
2754 Y(win
) = (HEIGHT(win
) > HEIGHT(r
)) ? Y(r
) :
2755 (Y(r
) + HEIGHT(r
) - HEIGHT(win
) - 2 * wc
.border_width
);
2759 wc
.width
= WIDTH(win
);
2760 wc
.height
= HEIGHT(win
);
2763 * Retain floater and transient geometry for correct positioning
2764 * when ws changes region
2766 if (!(win
->ewmh_flags
& EWMH_F_FULLSCREEN
))
2767 store_float_geom(win
, r
);
2769 DNPRINTF(SWM_D_MISC
, "stack_floater: window: %lu, (x,y) w x h: (%d,%d) "
2770 "%d x %d\n", win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
2772 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2776 * Send keystrokes to terminal to decrease/increase the font size as the
2777 * window size changes.
2780 adjust_font(struct ws_win
*win
)
2782 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
2783 win
->floating
|| win
->transient
)
2786 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
2787 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
2788 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
2789 win
->font_size_boundary
[win
->font_steps
] =
2790 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
2793 win
->last_inc
= win
->sh
.width_inc
;
2794 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
2795 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
2796 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
2799 win
->last_inc
= win
->sh
.width_inc
;
2800 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
2804 #define SWAPXY(g) do { \
2806 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2807 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2810 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
2813 XWindowAttributes wa
;
2814 struct swm_geometry win_g
, r_g
= *g
;
2815 struct ws_win
*win
, *fs_win
= 0;
2816 int i
, j
, s
, stacks
;
2817 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
2818 int hrh
, extra
= 0, h_slice
, last_h
= 0;
2819 int split
, colno
, winno
, mwin
, msize
, mscale
;
2820 int remain
, missing
, v_slice
, reconfigure
;
2823 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
2824 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
2826 winno
= count_win(ws
, 0);
2827 if (winno
== 0 && count_win(ws
, 1) == 0)
2830 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
2831 if (win
->transient
== 0 && win
->floating
== 0
2832 && win
->iconic
== 0)
2839 w_inc
= win
->sh
.width_inc
;
2840 w_base
= win
->sh
.base_width
;
2841 mwin
= ws
->l_state
.horizontal_mwin
;
2842 mscale
= ws
->l_state
.horizontal_msize
;
2843 stacks
= ws
->l_state
.horizontal_stacks
;
2846 w_inc
= win
->sh
.height_inc
;
2847 w_base
= win
->sh
.base_height
;
2848 mwin
= ws
->l_state
.vertical_mwin
;
2849 mscale
= ws
->l_state
.vertical_msize
;
2850 stacks
= ws
->l_state
.vertical_stacks
;
2854 if (stacks
> winno
- mwin
)
2855 stacks
= winno
- mwin
;
2859 h_slice
= r_g
.h
/ SWM_H_SLICE
;
2860 if (mwin
&& winno
> mwin
) {
2861 v_slice
= r_g
.w
/ SWM_V_SLICE
;
2865 win_g
.w
= v_slice
* mscale
;
2867 if (w_inc
> 1 && w_inc
< v_slice
) {
2868 /* adjust for window's requested size increment */
2869 remain
= (win_g
.w
- w_base
) % w_inc
;
2870 missing
= w_inc
- remain
;
2877 win_g
.x
+= r_g
.w
- msize
;
2880 colno
= split
= winno
/ stacks
;
2881 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
2882 2 * border_width
) / stacks
);
2884 hrh
= r_g
.h
/ colno
;
2885 extra
= r_g
.h
- (colno
* hrh
);
2886 win_g
.h
= hrh
- 2 * border_width
;
2888 /* stack all the tiled windows */
2889 i
= j
= 0, s
= stacks
;
2890 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2891 if (win
->transient
!= 0 || win
->floating
!= 0)
2893 if (win
->iconic
!= 0)
2896 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
2901 if (split
&& i
== split
) {
2902 colno
= (winno
- mwin
) / stacks
;
2903 if (s
<= (winno
- mwin
) % stacks
)
2905 split
= split
+ colno
;
2906 hrh
= (r_g
.h
/ colno
);
2907 extra
= r_g
.h
- (colno
* hrh
);
2911 win_g
.x
+= win_g
.w
+ 2 * border_width
;
2912 win_g
.w
= (r_g
.w
- msize
-
2913 (stacks
* 2 * border_width
)) / stacks
;
2915 win_g
.w
+= (r_g
.w
- msize
-
2916 (stacks
* 2 * border_width
)) % stacks
;
2920 win_g
.h
= hrh
- 2 * border_width
;
2922 h_inc
= win
->sh
.width_inc
;
2923 h_base
= win
->sh
.base_width
;
2925 h_inc
= win
->sh
.height_inc
;
2926 h_base
= win
->sh
.base_height
;
2928 if (j
== colno
- 1) {
2929 win_g
.h
= hrh
+ extra
;
2930 } else if (h_inc
> 1 && h_inc
< h_slice
) {
2931 /* adjust for window's requested size increment */
2932 remain
= (win_g
.h
- h_base
) % h_inc
;
2933 missing
= h_inc
- remain
;
2935 if (missing
<= extra
|| j
== 0) {
2947 win_g
.y
+= last_h
+ 2 * border_width
;
2949 bzero(&wc
, sizeof wc
);
2950 if (disable_border
&& bar_enabled
== 0 && winno
== 1){
2951 wc
.border_width
= 0;
2952 win_g
.w
+= 2 * border_width
;
2953 win_g
.h
+= 2 * border_width
;
2955 wc
.border_width
= border_width
;
2958 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
2959 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
2961 X(win
) = wc
.x
= win_g
.y
;
2962 Y(win
) = wc
.y
= win_g
.x
;
2963 WIDTH(win
) = wc
.width
= win_g
.h
;
2964 HEIGHT(win
) = wc
.height
= win_g
.w
;
2967 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
2968 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
2970 X(win
) = wc
.x
= win_g
.x
;
2971 Y(win
) = wc
.y
= win_g
.y
;
2972 WIDTH(win
) = wc
.width
= win_g
.w
;
2973 HEIGHT(win
) = wc
.height
= win_g
.h
;
2978 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
2979 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2982 if (XGetWindowAttributes(display
, win
->id
, &wa
))
2983 if (wa
.map_state
== IsUnmapped
)
2984 XMapRaised(display
, win
->id
);
2992 /* now, stack all the floaters and transients */
2993 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2994 if (win
->transient
== 0 && win
->floating
== 0)
2996 if (win
->iconic
== 1)
2998 if (win
->ewmh_flags
& EWMH_F_FULLSCREEN
) {
3003 stack_floater(win
, ws
->r
);
3004 XMapRaised(display
, win
->id
);
3008 stack_floater(fs_win
, ws
->r
);
3009 XMapRaised(display
, fs_win
->id
);
3014 vertical_config(struct workspace
*ws
, int id
)
3016 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
3020 case SWM_ARG_ID_STACKRESET
:
3021 case SWM_ARG_ID_STACKINIT
:
3022 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
3023 ws
->l_state
.vertical_mwin
= 1;
3024 ws
->l_state
.vertical_stacks
= 1;
3026 case SWM_ARG_ID_MASTERSHRINK
:
3027 if (ws
->l_state
.vertical_msize
> 1)
3028 ws
->l_state
.vertical_msize
--;
3030 case SWM_ARG_ID_MASTERGROW
:
3031 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
3032 ws
->l_state
.vertical_msize
++;
3034 case SWM_ARG_ID_MASTERADD
:
3035 ws
->l_state
.vertical_mwin
++;
3037 case SWM_ARG_ID_MASTERDEL
:
3038 if (ws
->l_state
.vertical_mwin
> 0)
3039 ws
->l_state
.vertical_mwin
--;
3041 case SWM_ARG_ID_STACKINC
:
3042 ws
->l_state
.vertical_stacks
++;
3044 case SWM_ARG_ID_STACKDEC
:
3045 if (ws
->l_state
.vertical_stacks
> 1)
3046 ws
->l_state
.vertical_stacks
--;
3054 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3056 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
3058 stack_master(ws
, g
, 0, 0);
3062 horizontal_config(struct workspace
*ws
, int id
)
3064 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
3067 case SWM_ARG_ID_STACKRESET
:
3068 case SWM_ARG_ID_STACKINIT
:
3069 ws
->l_state
.horizontal_mwin
= 1;
3070 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
3071 ws
->l_state
.horizontal_stacks
= 1;
3073 case SWM_ARG_ID_MASTERSHRINK
:
3074 if (ws
->l_state
.horizontal_msize
> 1)
3075 ws
->l_state
.horizontal_msize
--;
3077 case SWM_ARG_ID_MASTERGROW
:
3078 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
3079 ws
->l_state
.horizontal_msize
++;
3081 case SWM_ARG_ID_MASTERADD
:
3082 ws
->l_state
.horizontal_mwin
++;
3084 case SWM_ARG_ID_MASTERDEL
:
3085 if (ws
->l_state
.horizontal_mwin
> 0)
3086 ws
->l_state
.horizontal_mwin
--;
3088 case SWM_ARG_ID_STACKINC
:
3089 ws
->l_state
.horizontal_stacks
++;
3091 case SWM_ARG_ID_STACKDEC
:
3092 if (ws
->l_state
.horizontal_stacks
> 1)
3093 ws
->l_state
.horizontal_stacks
--;
3101 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3103 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
3105 stack_master(ws
, g
, 1, 0);
3108 /* fullscreen view */
3110 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
3113 struct swm_geometry gg
= *g
;
3114 struct ws_win
*win
, *wintrans
= NULL
, *parent
= NULL
;
3118 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
3123 winno
= count_win(ws
, 0);
3124 if (winno
== 0 && count_win(ws
, 1) == 0)
3127 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3128 if (win
->transient
) {
3130 parent
= find_window(win
->transient
);
3134 if (win
->floating
&& win
->floatmaxed
== 0 ) {
3136 * retain geometry for retrieval on exit from
3139 store_float_geom(win
, ws
->r
);
3140 win
->floatmaxed
= 1;
3143 /* only reconfigure if necessary */
3144 if (X(win
) != gg
.x
|| Y(win
) != gg
.y
|| WIDTH(win
) != gg
.w
||
3145 HEIGHT(win
) != gg
.h
) {
3146 bzero(&wc
, sizeof wc
);
3147 X(win
) = wc
.x
= gg
.x
;
3148 Y(win
) = wc
.y
= gg
.y
;
3150 wc
.border_width
= border_width
;
3151 WIDTH(win
) = wc
.width
= gg
.w
;
3152 HEIGHT(win
) = wc
.height
= gg
.h
;
3154 wc
.border_width
= 0;
3155 WIDTH(win
) = wc
.width
= gg
.w
+ 2 * border_width
;
3156 HEIGHT(win
) = wc
.height
= gg
.h
+
3159 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
3160 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3162 /* unmap only if we don't have multi screen */
3163 if (win
!= ws
->focus
)
3164 if (!(ScreenCount(display
) > 1 || outputs
> 1))
3168 /* put the last transient on top */
3171 XMapRaised(display
, parent
->id
);
3172 stack_floater(wintrans
, ws
->r
);
3173 focus_magic(wintrans
);
3178 send_to_ws(struct swm_region
*r
, union arg
*args
)
3180 int wsid
= args
->id
;
3181 struct ws_win
*win
= NULL
, *parent
;
3182 struct workspace
*ws
, *nws
;
3183 Atom ws_idx_atom
= 0;
3184 unsigned char ws_idx_str
[SWM_PROPLEN
];
3187 if (r
&& r
->ws
&& r
->ws
->focus
)
3193 if (win
->ws
->idx
== wsid
)
3196 DNPRINTF(SWM_D_MOVE
, "send_to_ws: window: 0x%lx\n", win
->id
);
3199 nws
= &win
->s
->ws
[wsid
];
3201 a
.id
= SWM_ARG_ID_FOCUSPREV
;
3203 if (win
->transient
) {
3204 parent
= find_window(win
->transient
);
3206 unmap_window(parent
);
3207 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
3208 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
3213 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
3214 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
3215 if (TAILQ_EMPTY(&ws
->winlist
))
3216 r
->ws
->focus
= NULL
;
3219 /* Try to update the window's workspace property */
3220 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
3222 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) <
3224 DNPRINTF(SWM_D_PROP
, "send_to_ws: set property: _SWM_WS: %s\n",
3226 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
3227 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
3234 pressbutton(struct swm_region
*r
, union arg
*args
)
3236 XTestFakeButtonEvent(display
, args
->id
, True
, CurrentTime
);
3237 XTestFakeButtonEvent(display
, args
->id
, False
, CurrentTime
);
3241 raise_toggle(struct swm_region
*r
, union arg
*args
)
3243 if (r
&& r
->ws
== NULL
)
3246 r
->ws
->always_raise
= !r
->ws
->always_raise
;
3248 /* bring floaters back to top */
3249 if (r
->ws
->always_raise
== 0)
3254 iconify(struct swm_region
*r
, union arg
*args
)
3258 if (r
->ws
->focus
== NULL
)
3260 unmap_window(r
->ws
->focus
);
3261 update_iconic(r
->ws
->focus
, 1);
3263 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3264 drain_enter_notify();
3265 r
->ws
->focus
= NULL
;
3266 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3271 get_win_name(Display
*dpy
, Window win
, Atom wname
, Atom stype
,
3272 unsigned long *slen
)
3275 unsigned long nitems
, nbytes
, nextra
;
3276 unsigned char *prop
= NULL
;
3279 status
= XGetWindowProperty(dpy
, win
, wname
, 0L, 0L, False
, stype
,
3280 &rettype
, &retfmt
, &nitems
, &nbytes
, &prop
);
3281 if (status
!= Success
)
3285 status
= XGetWindowProperty(dpy
, win
, wname
, 0L, nbytes
, False
,
3286 stype
, &rettype
, &retfmt
, &nitems
, &nextra
, &prop
);
3287 if (status
!= Success
) {
3291 if (rettype
!= stype
) {
3300 uniconify(struct swm_region
*r
, union arg
*args
)
3304 unsigned char *name
;
3308 DNPRINTF(SWM_D_MISC
, "uniconify\n");
3310 if (r
&& r
->ws
== NULL
)
3313 /* make sure we have anything to uniconify */
3314 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3315 if (win
->ws
== NULL
)
3316 continue; /* should never happen */
3317 if (win
->iconic
== 0)
3325 search_resp_action
= SWM_SEARCH_UNICONIFY
;
3327 spawn_select(r
, args
, "search", &searchpid
);
3329 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3332 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3333 if (win
->ws
== NULL
)
3334 continue; /* should never happen */
3335 if (win
->iconic
== 0)
3338 name
= get_win_name(display
, win
->id
, a_netwmname
,
3339 a_utf8_string
, &len
);
3342 fprintf(lfile
, "%s.%lu\n", name
, win
->id
);
3350 name_workspace(struct swm_region
*r
, union arg
*args
)
3354 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
3360 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
3362 spawn_select(r
, args
, "name_workspace", &searchpid
);
3364 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3367 fprintf(lfile
, "%s", "");
3372 search_workspace(struct swm_region
*r
, union arg
*args
)
3375 struct workspace
*ws
;
3378 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
3384 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
3386 spawn_select(r
, args
, "search", &searchpid
);
3388 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3391 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
3395 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
3396 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
3403 search_win_cleanup(void)
3405 struct search_window
*sw
= NULL
;
3407 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
3408 XDestroyWindow(display
, sw
->indicator
);
3409 XFreeGC(display
, sw
->gc
);
3410 TAILQ_REMOVE(&search_wl
, sw
, entry
);
3416 search_win(struct swm_region
*r
, union arg
*args
)
3418 struct ws_win
*win
= NULL
;
3419 struct search_window
*sw
= NULL
;
3426 XRectangle ibox
, lbox
;
3428 DNPRINTF(SWM_D_MISC
, "search_win\n");
3431 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
3433 spawn_select(r
, args
, "search", &searchpid
);
3435 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
3438 TAILQ_INIT(&search_wl
);
3441 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
3442 if (win
->iconic
== 1)
3445 sw
= calloc(1, sizeof(struct search_window
));
3447 fprintf(stderr
, "search_win: calloc: %s",
3450 search_win_cleanup();
3456 snprintf(s
, sizeof s
, "%d", i
);
3459 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
3461 w
= XCreateSimpleWindow(display
,
3462 win
->id
, 0, 0,lbox
.width
+ 4,
3463 bar_fs_extents
->max_logical_extent
.height
, 1,
3464 r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
,
3465 r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
3468 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
3470 sw
->gc
= XCreateGC(display
, w
, 0, &gcv
);
3471 XMapRaised(display
, w
);
3472 XSetForeground(display
, sw
->gc
, r
->s
->c
[SWM_S_COLOR_BAR
].color
);
3474 DRAWSTRING(display
, w
, bar_fs
, sw
->gc
, 2,
3475 (bar_fs_extents
->max_logical_extent
.height
-
3476 lbox
.height
) / 2 - lbox
.y
, s
, len
);
3478 fprintf(lfile
, "%d\n", i
);
3486 search_resp_uniconify(char *resp
, unsigned long len
)
3488 unsigned char *name
;
3492 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
3494 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
3495 if (win
->iconic
== 0)
3497 name
= get_win_name(display
, win
->id
, a_netwmname
,
3498 a_utf8_string
, &len
);
3501 if (asprintf(&s
, "%s.%lu", name
, win
->id
) == -1) {
3506 if (strncmp(s
, resp
, len
) == 0) {
3507 /* XXX this should be a callback to generalize */
3508 update_iconic(win
, 0);
3517 search_resp_name_workspace(char *resp
, unsigned long len
)
3519 struct workspace
*ws
;
3521 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
3523 if (search_r
->ws
== NULL
)
3528 free(search_r
->ws
->name
);
3529 search_r
->ws
->name
= NULL
;
3533 ws
->name
= strdup(resp
);
3534 if (ws
->name
== NULL
) {
3535 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
3536 "strdup: %s", strerror(errno
));
3543 search_resp_search_workspace(char *resp
, unsigned long len
)
3550 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
3554 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
3561 ws_idx
= strtonum(q
, 1, SWM_WS_MAX
, &errstr
);
3563 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
3570 switchws(search_r
, &a
);
3574 search_resp_search_window(char *resp
, unsigned long len
)
3579 struct search_window
*sw
;
3581 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
3585 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
3590 idx
= strtonum(s
, 1, INT_MAX
, &errstr
);
3592 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
3599 TAILQ_FOREACH(sw
, &search_wl
, entry
)
3600 if (idx
== sw
->idx
) {
3606 #define MAX_RESP_LEN 1024
3609 search_do_resp(void)
3615 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
3620 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
3621 fprintf(stderr
, "search: calloc\n");
3625 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
3627 fprintf(stderr
, "search: read error: %s\n", strerror(errno
));
3630 resp
[rbytes
] = '\0';
3633 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3634 * newline, so work around that by sanitizing the resp now.
3636 resp
[strcspn(resp
, "\n")] = '\0';
3639 switch (search_resp_action
) {
3640 case SWM_SEARCH_UNICONIFY
:
3641 search_resp_uniconify(resp
, len
);
3643 case SWM_SEARCH_NAME_WORKSPACE
:
3644 search_resp_name_workspace(resp
, len
);
3646 case SWM_SEARCH_SEARCH_WORKSPACE
:
3647 search_resp_search_workspace(resp
, len
);
3649 case SWM_SEARCH_SEARCH_WINDOW
:
3650 search_resp_search_window(resp
, len
);
3655 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
3656 search_win_cleanup();
3658 search_resp_action
= SWM_SEARCH_NONE
;
3659 close(select_resp_pipe
[0]);
3664 wkill(struct swm_region
*r
, union arg
*args
)
3666 DNPRINTF(SWM_D_MISC
, "wkill: id: %d\n", args
->id
);
3668 if (r
->ws
->focus
== NULL
)
3671 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
3672 XKillClient(display
, r
->ws
->focus
->id
);
3674 if (r
->ws
->focus
->can_delete
)
3675 client_msg(r
->ws
->focus
, adelete
);
3680 floating_toggle_win(struct ws_win
*win
)
3682 struct swm_region
*r
;
3692 /* reject floating toggles in max stack mode */
3693 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3696 if (win
->floating
) {
3697 if (!win
->floatmaxed
) {
3698 /* retain position for refloat */
3699 store_float_geom(win
, r
);
3703 if (win
->g_floatvalid
) {
3704 /* refloat at last floating relative position */
3705 X(win
) = win
->g_float
.x
- win
->rg_float
.x
+ X(r
);
3706 Y(win
) = win
->g_float
.y
- win
->rg_float
.y
+ Y(r
);
3707 WIDTH(win
) = win
->g_float
.w
;
3708 HEIGHT(win
) = win
->g_float
.h
;
3713 ewmh_update_actions(win
);
3719 floating_toggle(struct swm_region
*r
, union arg
*args
)
3721 struct ws_win
*win
= r
->ws
->focus
;
3727 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
3728 _NET_WM_STATE_TOGGLE
);
3731 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3732 drain_enter_notify();
3734 if (win
== win
->ws
->focus
) {
3735 a
.id
= SWM_ARG_ID_FOCUSCUR
;
3736 focus(win
->ws
->r
, &a
);
3741 constrain_window(struct ws_win
*win
, struct swm_region
*r
, int resizable
)
3743 if (X(win
) + WIDTH(win
) > X(r
) + WIDTH(r
) - border_width
) {
3745 WIDTH(win
) = X(r
) + WIDTH(r
) - X(win
) - border_width
;
3747 X(win
) = X(r
) + WIDTH(r
) - WIDTH(win
) - border_width
;
3750 if (X(win
) < X(r
) - border_width
) {
3752 WIDTH(win
) -= X(r
) - X(win
) - border_width
;
3754 X(win
) = X(r
) - border_width
;
3757 if (Y(win
) + HEIGHT(win
) > Y(r
) + HEIGHT(r
) - border_width
) {
3759 HEIGHT(win
) = Y(r
) + HEIGHT(r
) - Y(win
) - border_width
;
3761 Y(win
) = Y(r
) + HEIGHT(r
) - HEIGHT(win
) - border_width
;
3764 if (Y(win
) < Y(r
) - border_width
) {
3766 HEIGHT(win
) -= Y(r
) - Y(win
) - border_width
;
3768 Y(win
) = Y(r
) - border_width
;
3773 if (HEIGHT(win
) < 1)
3778 update_window(struct ws_win
*win
)
3783 bzero(&wc
, sizeof wc
);
3784 mask
= CWBorderWidth
| CWWidth
| CWHeight
| CWX
| CWY
;
3785 wc
.border_width
= border_width
;
3788 wc
.width
= WIDTH(win
);
3789 wc
.height
= HEIGHT(win
);
3791 DNPRINTF(SWM_D_MISC
, "update_window: window: 0x%lx, (x,y) w x h: "
3792 "(%d,%d) %d x %d\n", win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
3794 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3797 #define SWM_RESIZE_STEPS (50)
3800 resize(struct ws_win
*win
, union arg
*args
)
3804 struct swm_region
*r
= NULL
;
3805 int resize_step
= 0;
3809 struct swm_geometry g
;
3810 int top
= 0, left
= 0;
3813 unsigned int shape
; /* cursor style */
3819 DNPRINTF(SWM_D_MOUSE
, "resize: window: 0x%lx, floating: %s, "
3820 "transient: 0x%lx\n", win
->id
, YESNO(win
->floating
),
3823 if (!(win
->transient
!= 0 || win
->floating
!= 0))
3826 /* reject resizes in max mode for floaters (transient ok) */
3827 if (win
->floatmaxed
)
3831 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
3837 case SWM_ARG_ID_WIDTHSHRINK
:
3838 WIDTH(win
) -= SWM_RESIZE_STEPS
;
3841 case SWM_ARG_ID_WIDTHGROW
:
3842 WIDTH(win
) += SWM_RESIZE_STEPS
;
3845 case SWM_ARG_ID_HEIGHTSHRINK
:
3846 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
3849 case SWM_ARG_ID_HEIGHTGROW
:
3850 HEIGHT(win
) += SWM_RESIZE_STEPS
;
3857 constrain_window(win
, r
, 1);
3859 store_float_geom(win
,r
);
3863 if (focus_mode
== SWM_FOCUS_DEFAULT
)
3864 drain_enter_notify();
3866 /* get cursor offset from window root */
3867 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
3872 if (wx
< WIDTH(win
) / 2)
3875 if (wy
< HEIGHT(win
) / 2)
3878 if (args
->id
== SWM_ARG_ID_CENTER
)
3881 shape
= (left
) ? XC_top_left_corner
: XC_top_right_corner
;
3883 shape
= (left
) ? XC_bottom_left_corner
: XC_bottom_right_corner
;
3885 cursor
= XCreateFontCursor(display
, shape
);
3887 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
3888 GrabModeAsync
, None
, cursor
, CurrentTime
) != GrabSuccess
) {
3889 XFreeCursor(display
, cursor
);
3894 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
3895 SubstructureRedirectMask
, &ev
);
3897 case ConfigureRequest
:
3900 handler
[ev
.type
](&ev
);
3903 /* cursor offset/delta from start of the operation */
3904 dx
= ev
.xmotion
.x_root
- x
;
3905 dy
= ev
.xmotion
.y_root
- y
;
3911 if (args
->id
== SWM_ARG_ID_CENTER
) {
3912 if (g
.h
/ 2 + dy
< 1)
3916 HEIGHT(win
) = g
.h
+ 2 * dy
;
3924 HEIGHT(win
) = g
.h
+ dy
;
3931 if (args
->id
== SWM_ARG_ID_CENTER
) {
3932 if (g
.w
/ 2 + dx
< 1)
3936 WIDTH(win
) = g
.w
+ 2 * dx
;
3944 WIDTH(win
) = g
.w
+ dx
;
3947 constrain_window(win
, r
, 1);
3949 /* not free, don't sync more than 120 times / second */
3950 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
3951 time
= ev
.xmotion
.time
;
3952 XSync(display
, False
);
3957 } while (ev
.type
!= ButtonRelease
);
3959 XSync(display
, False
);
3962 store_float_geom(win
,r
);
3964 XUngrabPointer(display
, CurrentTime
);
3965 XFreeCursor(display
, cursor
);
3968 drain_enter_notify();
3972 resize_step(struct swm_region
*r
, union arg
*args
)
3974 struct ws_win
*win
= NULL
;
3976 if (r
&& r
->ws
&& r
->ws
->focus
)
3984 #define SWM_MOVE_STEPS (50)
3987 move(struct ws_win
*win
, union arg
*args
)
3992 struct swm_region
*r
= NULL
;
4002 DNPRINTF(SWM_D_MOUSE
, "move: window: 0x%lx, floating: %s, transient: "
4003 "0x%lx\n", win
->id
, YESNO(win
->floating
), win
->transient
);
4005 /* in max_stack mode should only move transients */
4006 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !win
->transient
)
4010 if (win
->floating
== 0 && !win
->transient
) {
4011 store_float_geom(win
,r
);
4012 ewmh_update_win_state(win
, ewmh
[_NET_WM_STATE_ABOVE
].atom
,
4015 ewmh_update_win_state(win
, ewmh
[_SWM_WM_STATE_MANUAL
].atom
,
4022 case SWM_ARG_ID_MOVELEFT
:
4023 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
4026 case SWM_ARG_ID_MOVERIGHT
:
4027 X(win
) += (SWM_MOVE_STEPS
- border_width
);
4030 case SWM_ARG_ID_MOVEUP
:
4031 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
4034 case SWM_ARG_ID_MOVEDOWN
:
4035 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
4042 constrain_window(win
, r
, 0);
4044 store_float_geom(win
, r
);
4048 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
4049 GrabModeAsync
, None
, XCreateFontCursor(display
, XC_fleur
),
4050 CurrentTime
) != GrabSuccess
)
4053 /* get cursor offset from window root */
4054 if (!XQueryPointer(display
, win
->id
, &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
))
4058 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
4059 SubstructureRedirectMask
, &ev
);
4061 case ConfigureRequest
:
4064 handler
[ev
.type
](&ev
);
4067 X(win
) = ev
.xmotion
.x_root
- wx
- border_width
;
4068 Y(win
) = ev
.xmotion
.y_root
- wy
- border_width
;
4070 constrain_window(win
, r
, 0);
4072 /* not free, don't sync more than 120 times / second */
4073 if ((ev
.xmotion
.time
- time
) > (1000 / 120) ) {
4074 time
= ev
.xmotion
.time
;
4075 XSync(display
, False
);
4080 } while (ev
.type
!= ButtonRelease
);
4082 XSync(display
, False
);
4085 store_float_geom(win
,r
);
4086 XUngrabPointer(display
, CurrentTime
);
4089 drain_enter_notify();
4093 move_step(struct swm_region
*r
, union arg
*args
)
4095 struct ws_win
*win
= NULL
;
4097 if (r
&& r
->ws
&& r
->ws
->focus
)
4102 if (!(win
->transient
!= 0 || win
->floating
!= 0))
4109 /* user/key callable function IDs */
4179 kf_search_workspace
,
4181 kf_dumpwins
, /* MUST BE LAST */
4185 /* key definitions */
4187 dummykeyfunc(struct swm_region
*r
, union arg
*args
)
4192 legacyfunc(struct swm_region
*r
, union arg
*args
)
4197 char name
[SWM_FUNCNAME_LEN
];
4198 void (*func
)(struct swm_region
*r
, union arg
*);
4200 } keyfuncs
[kf_invalid
+ 1] = {
4201 /* name function argument */
4202 { "cycle_layout", cycle_layout
, {0} },
4203 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
4204 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
4205 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
4206 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
4207 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
4208 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
4209 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
4210 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
4211 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
4212 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
4213 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
4214 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
4215 { "spawn_term", spawnterm
, {.argv
= spawn_term
} },
4216 { "spawn_menu", legacyfunc
, {0} },
4217 { "quit", quit
, {0} },
4218 { "restart", restart
, {0} },
4219 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
4220 { "ws_1", switchws
, {.id
= 0} },
4221 { "ws_2", switchws
, {.id
= 1} },
4222 { "ws_3", switchws
, {.id
= 2} },
4223 { "ws_4", switchws
, {.id
= 3} },
4224 { "ws_5", switchws
, {.id
= 4} },
4225 { "ws_6", switchws
, {.id
= 5} },
4226 { "ws_7", switchws
, {.id
= 6} },
4227 { "ws_8", switchws
, {.id
= 7} },
4228 { "ws_9", switchws
, {.id
= 8} },
4229 { "ws_10", switchws
, {.id
= 9} },
4230 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
4231 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
4232 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
4233 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
4234 { "ws_prior", priorws
, {0} },
4235 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
4236 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
4237 { "mvws_1", send_to_ws
, {.id
= 0} },
4238 { "mvws_2", send_to_ws
, {.id
= 1} },
4239 { "mvws_3", send_to_ws
, {.id
= 2} },
4240 { "mvws_4", send_to_ws
, {.id
= 3} },
4241 { "mvws_5", send_to_ws
, {.id
= 4} },
4242 { "mvws_6", send_to_ws
, {.id
= 5} },
4243 { "mvws_7", send_to_ws
, {.id
= 6} },
4244 { "mvws_8", send_to_ws
, {.id
= 7} },
4245 { "mvws_9", send_to_ws
, {.id
= 8} },
4246 { "mvws_10", send_to_ws
, {.id
= 9} },
4247 { "bar_toggle", bar_toggle
, {0} },
4248 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
4249 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
4250 { "screenshot_all", legacyfunc
, {0} },
4251 { "screenshot_wind", legacyfunc
, {0} },
4252 { "float_toggle", floating_toggle
,{0} },
4253 { "version", version
, {0} },
4254 { "spawn_lock", legacyfunc
, {0} },
4255 { "spawn_initscr", legacyfunc
, {0} },
4256 { "spawn_custom", dummykeyfunc
, {0} },
4257 { "iconify", iconify
, {0} },
4258 { "uniconify", uniconify
, {0} },
4259 { "raise_toggle", raise_toggle
, {0} },
4260 { "button2", pressbutton
, {2} },
4261 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
4262 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
4263 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
4264 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
4265 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
4266 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
4267 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
4268 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
4269 { "name_workspace", name_workspace
, {0} },
4270 { "search_workspace", search_workspace
, {0} },
4271 { "search_win", search_win
, {0} },
4272 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
4273 { "invalid key func", NULL
, {0} },
4276 TAILQ_ENTRY(key
) entry
;
4279 enum keyfuncid funcid
;
4282 TAILQ_HEAD(key_list
, key
);
4283 struct key_list keys
= TAILQ_HEAD_INITIALIZER(keys
);
4286 enum { client_click
, root_click
};
4288 unsigned int action
;
4290 unsigned int button
;
4291 void (*func
)(struct ws_win
*, union arg
*);
4294 /* action key mouse button func args */
4295 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
4296 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
4297 { client_click
, MODKEY
, Button1
, move
, {0} },
4301 update_modkey(unsigned int mod
)
4307 TAILQ_FOREACH(kp
, &keys
, entry
)
4308 if (kp
->mod
& ShiftMask
)
4309 kp
->mod
= mod
| ShiftMask
;
4313 for (i
= 0; i
< LENGTH(buttons
); i
++)
4314 if (buttons
[i
].mask
& ShiftMask
)
4315 buttons
[i
].mask
= mod
| ShiftMask
;
4317 buttons
[i
].mask
= mod
;
4327 int spawns_size
= 0, spawns_length
= 0;
4328 struct spawn_prog
*spawns
= NULL
;
4331 spawn_expand(struct swm_region
*r
, union arg
*args
, char *spawn_name
,
4334 struct spawn_prog
*prog
= NULL
;
4336 char *ap
, **real_args
;
4338 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
4341 for (i
= 0; i
< spawns_length
; i
++) {
4342 if (!strcasecmp(spawn_name
, spawns
[i
].name
))
4346 fprintf(stderr
, "spawn_custom: program %s not found\n",
4351 /* make room for expanded args */
4352 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
4353 err(1, "spawn_custom: calloc real_args");
4355 /* expand spawn_args into real_args */
4356 for (i
= 0; i
< prog
->argc
; i
++) {
4358 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
4359 if (!strcasecmp(ap
, "$bar_border")) {
4361 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
4363 err(1, "spawn_custom border color");
4364 } else if (!strcasecmp(ap
, "$bar_color")) {
4366 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
4368 err(1, "spawn_custom bar color");
4369 } else if (!strcasecmp(ap
, "$bar_font")) {
4370 if ((real_args
[i
] = strdup(bar_fonts
))
4372 err(1, "spawn_custom bar fonts");
4373 } else if (!strcasecmp(ap
, "$bar_font_color")) {
4375 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
4377 err(1, "spawn_custom color font");
4378 } else if (!strcasecmp(ap
, "$color_focus")) {
4380 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
4382 err(1, "spawn_custom color focus");
4383 } else if (!strcasecmp(ap
, "$color_unfocus")) {
4385 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
4387 err(1, "spawn_custom color unfocus");
4389 /* no match --> copy as is */
4390 if ((real_args
[i
] = strdup(ap
)) == NULL
)
4391 err(1, "spawn_custom strdup(ap)");
4393 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
4398 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
4399 for (i
= 0; i
< prog
->argc
; i
++)
4400 DNPRINTF(SWM_D_SPAWN
, "\"%s\" ", real_args
[i
]);
4401 DNPRINTF(SWM_D_SPAWN
, "\n");
4403 *ret_args
= real_args
;
4404 return (prog
->argc
);
4408 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
4414 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4418 spawn(r
->ws
->idx
, &a
, 1);
4420 for (i
= 0; i
< spawn_argc
; i
++)
4426 spawn_select(struct swm_region
*r
, union arg
*args
, char *spawn_name
, int *pid
)
4432 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
4436 if (pipe(select_list_pipe
) == -1)
4437 err(1, "pipe error");
4438 if (pipe(select_resp_pipe
) == -1)
4439 err(1, "pipe error");
4441 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
4442 err(1, "could not disable SIGPIPE");
4443 switch (*pid
= fork()) {
4445 err(1, "cannot fork");
4448 if (dup2(select_list_pipe
[0], 0) == -1)
4450 if (dup2(select_resp_pipe
[1], 1) == -1)
4452 close(select_list_pipe
[1]);
4453 close(select_resp_pipe
[0]);
4454 spawn(r
->ws
->idx
, &a
, 0);
4456 default: /* parent */
4457 close(select_list_pipe
[0]);
4458 close(select_resp_pipe
[1]);
4462 for (i
= 0; i
< spawn_argc
; i
++)
4468 setspawn(struct spawn_prog
*prog
)
4472 if (prog
== NULL
|| prog
->name
== NULL
)
4476 for (i
= 0; i
< spawns_length
; i
++) {
4477 if (!strcmp(spawns
[i
].name
, prog
->name
)) {
4479 if (prog
->argv
== NULL
) {
4481 DNPRINTF(SWM_D_SPAWN
,
4482 "setspawn: delete #%d: %s\n",
4484 free(spawns
[i
].name
);
4485 for (j
= 0; j
< spawns
[i
].argc
; j
++)
4486 free(spawns
[i
].argv
[j
]);
4487 free(spawns
[i
].argv
);
4488 j
= spawns_length
- 1;
4490 spawns
[i
] = spawns
[j
];
4495 DNPRINTF(SWM_D_SPAWN
,
4496 "setspawn: replace #%d: %s\n",
4498 free(spawns
[i
].name
);
4499 for (j
= 0; j
< spawns
[i
].argc
; j
++)
4500 free(spawns
[i
].argv
[j
]);
4501 free(spawns
[i
].argv
);
4504 /* found case handled */
4510 if (prog
->argv
== NULL
) {
4512 "error: setspawn: cannot find program: %s", prog
->name
);
4517 /* not found: add */
4518 if (spawns_size
== 0 || spawns
== NULL
) {
4520 DNPRINTF(SWM_D_SPAWN
, "setspawn: init list %d\n", spawns_size
);
4521 spawns
= malloc((size_t)spawns_size
*
4522 sizeof(struct spawn_prog
));
4523 if (spawns
== NULL
) {
4524 fprintf(stderr
, "setspawn: malloc failed\n");
4528 } else if (spawns_length
== spawns_size
) {
4530 DNPRINTF(SWM_D_SPAWN
, "setspawn: grow list %d\n", spawns_size
);
4531 spawns
= realloc(spawns
, (size_t)spawns_size
*
4532 sizeof(struct spawn_prog
));
4533 if (spawns
== NULL
) {
4534 fprintf(stderr
, "setspawn: realloc failed\n");
4540 if (spawns_length
< spawns_size
) {
4541 DNPRINTF(SWM_D_SPAWN
, "setspawn: add #%d %s\n",
4542 spawns_length
, prog
->name
);
4543 i
= spawns_length
++;
4546 fprintf(stderr
, "spawns array problem?\n");
4547 if (spawns
== NULL
) {
4548 fprintf(stderr
, "spawns array is NULL!\n");
4556 setconfspawn(char *selector
, char *value
, int flags
)
4558 struct spawn_prog
*prog
;
4559 char *vp
, *cp
, *word
;
4561 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
4562 if ((prog
= calloc(1, sizeof *prog
)) == NULL
)
4563 err(1, "setconfspawn: calloc prog");
4564 prog
->name
= strdup(selector
);
4565 if (prog
->name
== NULL
)
4566 err(1, "setconfspawn prog->name");
4567 if ((cp
= vp
= strdup(value
)) == NULL
)
4568 err(1, "setconfspawn: strdup(value) ");
4569 while ((word
= strsep(&cp
, " \t")) != NULL
) {
4570 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: arg [%s]\n", word
);
4572 cp
+= (long)strspn(cp
, " \t");
4573 if (strlen(word
) > 0) {
4575 if ((prog
->argv
= realloc(prog
->argv
,
4576 prog
->argc
* sizeof(char *))) == NULL
)
4577 err(1, "setconfspawn: realloc");
4578 if ((prog
->argv
[prog
->argc
- 1] = strdup(word
)) == NULL
)
4579 err(1, "setconfspawn: strdup");
4586 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
4593 setconfspawn("term", "xterm", 0);
4594 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4595 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4596 setconfspawn("lock", "xlock", 0);
4597 setconfspawn("initscr", "initscreen.sh", 0);
4598 setconfspawn("menu", "dmenu_run"
4601 " -nf $bar_font_color"
4603 " -sf $bar_color", 0);
4604 setconfspawn("search", "dmenu"
4608 " -nf $bar_font_color"
4610 " -sf $bar_color", 0);
4611 setconfspawn("name_workspace", "dmenu"
4615 " -nf $bar_font_color"
4617 " -sf $bar_color", 0);
4621 #define SWM_MODNAME_SIZE 32
4622 #define SWM_KEY_WS "\n+ \t"
4624 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
4628 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
4629 if (mod
== NULL
|| ks
== NULL
) {
4630 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
4633 if (keystr
== NULL
|| strlen(keystr
) == 0) {
4634 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
4640 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
4641 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
4643 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
4644 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
4646 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
4648 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
4650 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
4652 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
4654 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
4656 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
4657 *mod
|= ControlMask
;
4659 *ks
= XStringToKeysym(name
);
4660 XConvertCase(*ks
, ks
, &uks
);
4661 if (ks
== NoSymbol
) {
4663 "parsekeys: invalid key %s\n",
4669 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
4674 strdupsafe(char *str
)
4679 return (strdup(str
));
4683 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
4687 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
4688 keyfuncs
[kfid
].name
, spawn_name
);
4690 if ((kp
= malloc(sizeof *kp
)) == NULL
)
4691 err(1, "key_insert: malloc");
4696 kp
->spawn_name
= strdupsafe(spawn_name
);
4697 TAILQ_INSERT_TAIL(&keys
, kp
, entry
);
4699 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
4703 key_remove(struct key
*kp
)
4705 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
4707 TAILQ_REMOVE(&keys
, kp
, entry
);
4708 free(kp
->spawn_name
);
4711 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
4715 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
4718 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
4722 key_insert(mod
, ks
, kfid
, spawn_name
);
4724 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
4728 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
4733 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
4734 keyfuncs
[kfid
].name
, spawn_name
);
4736 TAILQ_FOREACH(kp
, &keys
, entry
) {
4737 if (kp
->mod
== mod
&& kp
->keysym
== ks
) {
4738 if (kfid
== kf_invalid
)
4741 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
4742 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4746 if (kfid
== kf_invalid
) {
4747 warnx("error: setkeybinding: cannot find mod/key combination");
4748 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4752 key_insert(mod
, ks
, kfid
, spawn_name
);
4753 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
4757 setconfbinding(char *selector
, char *value
, int flags
)
4759 enum keyfuncid kfid
;
4763 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
4764 if (selector
== NULL
) {
4765 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
4766 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4768 setkeybinding(mod
, ks
, kfid
, NULL
);
4773 /* search by key function name */
4774 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
4775 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
4776 SWM_FUNCNAME_LEN
) == 0) {
4777 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4779 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4780 setkeybinding(mod
, ks
, kfid
, NULL
);
4786 /* search by custom spawn name */
4787 for (i
= 0; i
< spawns_length
; i
++) {
4788 if (strcasecmp(selector
, spawns
[i
].name
) == 0) {
4789 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
4791 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
4792 setkeybinding(mod
, ks
, kf_spawn_custom
,
4799 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
4806 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
4807 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
4808 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
4809 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
4810 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
4811 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
4812 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
4813 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
4814 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
4815 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
4816 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
4817 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
4818 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
4819 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_term
, NULL
);
4820 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
,"menu");
4821 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
4822 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
4823 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
4824 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
4825 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
4826 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
4827 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
4828 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
4829 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
4830 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
4831 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
4832 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
4833 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
4834 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
4835 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
4836 setkeybinding(MODKEY
, XK_Up
, kf_ws_next_all
, NULL
);
4837 setkeybinding(MODKEY
, XK_Down
, kf_ws_prev_all
, NULL
);
4838 setkeybinding(MODKEY
, XK_a
, kf_ws_prior
, NULL
);
4839 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
4840 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
4841 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
4842 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
4843 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
4844 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
4845 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
4846 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
4847 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
4848 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
4849 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
4850 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
4851 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
4852 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
4853 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
4854 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
4855 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
4856 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
,"screenshot_all");
4857 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
,"screenshot_wind");
4858 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
4859 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
4860 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
,"lock");
4861 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
,"initscr");
4862 setkeybinding(MODKEY
, XK_w
, kf_iconify
, NULL
);
4863 setkeybinding(MODKEY
|ShiftMask
, XK_w
, kf_uniconify
, NULL
);
4864 setkeybinding(MODKEY
|ShiftMask
, XK_r
, kf_raise_toggle
,NULL
);
4865 setkeybinding(MODKEY
, XK_v
, kf_button2
, NULL
);
4866 setkeybinding(MODKEY
, XK_equal
, kf_width_grow
, NULL
);
4867 setkeybinding(MODKEY
, XK_minus
, kf_width_shrink
,NULL
);
4868 setkeybinding(MODKEY
|ShiftMask
, XK_equal
, kf_height_grow
, NULL
);
4869 setkeybinding(MODKEY
|ShiftMask
, XK_minus
, kf_height_shrink
,NULL
);
4870 setkeybinding(MODKEY
, XK_bracketleft
, kf_move_left
, NULL
);
4871 setkeybinding(MODKEY
, XK_bracketright
,kf_move_right
, NULL
);
4872 setkeybinding(MODKEY
|ShiftMask
, XK_bracketleft
, kf_move_up
, NULL
);
4873 setkeybinding(MODKEY
|ShiftMask
, XK_bracketright
,kf_move_down
, NULL
);
4874 setkeybinding(MODKEY
|ShiftMask
, XK_slash
, kf_name_workspace
,NULL
);
4875 setkeybinding(MODKEY
, XK_slash
, kf_search_workspace
,NULL
);
4876 setkeybinding(MODKEY
, XK_f
, kf_search_win
, NULL
);
4878 setkeybinding(MODKEY
|ShiftMask
, XK_d
, kf_dumpwins
, NULL
);
4885 struct key
*kp_loop
, *kp_next
;
4887 kp_loop
= TAILQ_FIRST(&keys
);
4888 while (kp_loop
!= NULL
) {
4889 kp_next
= TAILQ_NEXT(kp_loop
, entry
);
4890 key_remove(kp_loop
);
4896 setkeymapping(char *selector
, char *value
, int flags
)
4898 char keymapping_file
[PATH_MAX
];
4899 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
4900 if (value
[0] == '~')
4901 snprintf(keymapping_file
, sizeof keymapping_file
, "%s/%s",
4902 pwd
->pw_dir
, &value
[1]);
4904 strlcpy(keymapping_file
, value
, sizeof keymapping_file
);
4906 /* load new key bindings; if it fails, revert to default bindings */
4907 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
4911 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
4916 updatenumlockmask(void)
4919 XModifierKeymap
*modmap
;
4921 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
4923 modmap
= XGetModifierMapping(display
);
4924 for (i
= 0; i
< 8; i
++)
4925 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
4926 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
4927 == XKeysymToKeycode(display
, XK_Num_Lock
))
4928 numlockmask
= (1 << i
);
4930 XFreeModifiermap(modmap
);
4938 unsigned int modifiers
[] =
4939 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
4942 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
4943 updatenumlockmask();
4945 for (k
= 0; k
< ScreenCount(display
); k
++) {
4946 if (TAILQ_EMPTY(&screens
[k
].rl
))
4948 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
4949 TAILQ_FOREACH(kp
, &keys
, entry
) {
4950 if ((code
= XKeysymToKeycode(display
, kp
->keysym
)))
4951 for (j
= 0; j
< LENGTH(modifiers
); j
++)
4952 XGrabKey(display
, code
,
4953 kp
->mod
| modifiers
[j
],
4954 screens
[k
].root
, True
,
4955 GrabModeAsync
, GrabModeAsync
);
4961 grabbuttons(struct ws_win
*win
, int focused
)
4964 unsigned int modifiers
[] =
4965 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
4967 updatenumlockmask();
4968 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
4970 for (i
= 0; i
< LENGTH(buttons
); i
++)
4971 if (buttons
[i
].action
== client_click
)
4972 for (j
= 0; j
< LENGTH(modifiers
); j
++)
4973 XGrabButton(display
, buttons
[i
].button
,
4974 buttons
[i
].mask
| modifiers
[j
],
4975 win
->id
, False
, BUTTONMASK
,
4976 GrabModeAsync
, GrabModeSync
, None
,
4979 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
4980 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
4983 const char *quirkname
[] = {
4984 "NONE", /* config string for "no value" */
4993 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4994 #define SWM_Q_WS "\n|+ \t"
4996 parsequirks(char *qstr
, unsigned long *quirk
)
5006 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
5008 cp
+= (long)strspn(cp
, SWM_Q_WS
);
5009 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
5010 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
5011 DNPRINTF(SWM_D_QUIRK
,
5012 "parsequirks: %s\n", name
);
5017 *quirk
|= 1 << (i
-1);
5021 if (i
>= LENGTH(quirkname
)) {
5022 DNPRINTF(SWM_D_QUIRK
,
5023 "parsequirks: invalid quirk [%s]\n", name
);
5031 setquirk(const char *class, const char *name
, const int quirk
)
5036 for (i
= 0; i
< quirks_length
; i
++) {
5037 if (!strcmp(quirks
[i
].class, class) &&
5038 !strcmp(quirks
[i
].name
, name
)) {
5041 DNPRINTF(SWM_D_QUIRK
,
5042 "setquirk: delete #%d %s:%s\n",
5043 i
, quirks
[i
].class, quirks
[i
].name
);
5044 free(quirks
[i
].class);
5045 free(quirks
[i
].name
);
5046 j
= quirks_length
- 1;
5048 quirks
[i
] = quirks
[j
];
5052 /* found: replace */
5053 DNPRINTF(SWM_D_QUIRK
,
5054 "setquirk: replace #%d %s:%s\n",
5055 i
, quirks
[i
].class, quirks
[i
].name
);
5056 free(quirks
[i
].class);
5057 free(quirks
[i
].name
);
5058 quirks
[i
].class = strdup(class);
5059 quirks
[i
].name
= strdup(name
);
5060 quirks
[i
].quirk
= quirk
;
5067 "error: setquirk: cannot find class/name combination");
5070 /* not found: add */
5071 if (quirks_size
== 0 || quirks
== NULL
) {
5073 DNPRINTF(SWM_D_QUIRK
, "setquirk: init list %d\n", quirks_size
);
5074 quirks
= malloc((size_t)quirks_size
* sizeof(struct quirk
));
5075 if (quirks
== NULL
) {
5076 fprintf(stderr
, "setquirk: malloc failed\n");
5080 } else if (quirks_length
== quirks_size
) {
5082 DNPRINTF(SWM_D_QUIRK
, "setquirk: grow list %d\n", quirks_size
);
5083 quirks
= realloc(quirks
,
5084 (size_t)quirks_size
* sizeof(struct quirk
));
5085 if (quirks
== NULL
) {
5086 fprintf(stderr
, "setquirk: realloc failed\n");
5091 if (quirks_length
< quirks_size
) {
5092 DNPRINTF(SWM_D_QUIRK
, "setquirk: add %d\n", quirks_length
);
5093 j
= quirks_length
++;
5094 quirks
[j
].class = strdup(class);
5095 quirks
[j
].name
= strdup(name
);
5096 quirks
[j
].quirk
= quirk
;
5098 fprintf(stderr
, "quirks array problem?\n");
5099 if (quirks
== NULL
) {
5100 fprintf(stderr
, "quirks array problem!\n");
5107 setconfquirk(char *selector
, char *value
, int flags
)
5109 char *cp
, *class, *name
;
5111 unsigned long quirks
;
5112 if (selector
== NULL
)
5114 if ((cp
= strchr(selector
, ':')) == NULL
)
5119 if ((retval
= parsequirks(value
, &quirks
)) == 0)
5120 setquirk(class, name
, quirks
);
5127 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
5128 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT
);
5129 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
5130 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
5131 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5132 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
5133 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5134 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5135 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5136 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
5137 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
5138 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
5139 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
5142 /* conf file stuff */
5143 #define SWM_CONF_FILE "scrotwm.conf"
5145 enum { SWM_S_BAR_DELAY
, SWM_S_BAR_ENABLED
, SWM_S_BAR_BORDER_WIDTH
,
5146 SWM_S_STACK_ENABLED
, SWM_S_CLOCK_ENABLED
, SWM_S_CLOCK_FORMAT
,
5147 SWM_S_CYCLE_EMPTY
, SWM_S_CYCLE_VISIBLE
, SWM_S_SS_ENABLED
,
5148 SWM_S_TERM_WIDTH
, SWM_S_TITLE_CLASS_ENABLED
,
5149 SWM_S_TITLE_NAME_ENABLED
, SWM_S_WINDOW_NAME_ENABLED
, SWM_S_URGENT_ENABLED
,
5150 SWM_S_FOCUS_MODE
, SWM_S_DISABLE_BORDER
, SWM_S_BORDER_WIDTH
,
5151 SWM_S_BAR_FONT
, SWM_S_BAR_ACTION
, SWM_S_SPAWN_TERM
,
5152 SWM_S_SS_APP
, SWM_S_DIALOG_RATIO
, SWM_S_BAR_AT_BOTTOM
,
5153 SWM_S_VERBOSE_LAYOUT
, SWM_S_BAR_JUSTIFY
5157 setconfvalue(char *selector
, char *value
, int flags
)
5163 case SWM_S_BAR_DELAY
:
5164 bar_delay
= atoi(value
);
5166 case SWM_S_BAR_ENABLED
:
5167 bar_enabled
= atoi(value
);
5169 case SWM_S_BAR_BORDER_WIDTH
:
5170 bar_border_width
= atoi(value
);
5172 case SWM_S_BAR_AT_BOTTOM
:
5173 bar_at_bottom
= atoi(value
);
5175 case SWM_S_BAR_JUSTIFY
:
5176 if (!strcmp(value
, "left"))
5177 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
5178 else if (!strcmp(value
, "center"))
5179 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
5180 else if (!strcmp(value
, "right"))
5181 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
5183 errx(1, "invalid bar_justify");
5185 case SWM_S_STACK_ENABLED
:
5186 stack_enabled
= atoi(value
);
5188 case SWM_S_CLOCK_ENABLED
:
5189 clock_enabled
= atoi(value
);
5191 case SWM_S_CLOCK_FORMAT
:
5192 #ifndef SWM_DENY_CLOCK_FORMAT
5194 if ((clock_format
= strdup(value
)) == NULL
)
5195 err(1, "setconfvalue: clock_format");
5198 case SWM_S_CYCLE_EMPTY
:
5199 cycle_empty
= atoi(value
);
5201 case SWM_S_CYCLE_VISIBLE
:
5202 cycle_visible
= atoi(value
);
5204 case SWM_S_SS_ENABLED
:
5205 ss_enabled
= atoi(value
);
5207 case SWM_S_TERM_WIDTH
:
5208 term_width
= atoi(value
);
5210 case SWM_S_TITLE_CLASS_ENABLED
:
5211 title_class_enabled
= atoi(value
);
5213 case SWM_S_WINDOW_NAME_ENABLED
:
5214 window_name_enabled
= atoi(value
);
5216 case SWM_S_TITLE_NAME_ENABLED
:
5217 title_name_enabled
= atoi(value
);
5219 case SWM_S_URGENT_ENABLED
:
5220 urgent_enabled
= atoi(value
);
5222 case SWM_S_FOCUS_MODE
:
5223 if (!strcmp(value
, "default"))
5224 focus_mode
= SWM_FOCUS_DEFAULT
;
5225 else if (!strcmp(value
, "follow_cursor"))
5226 focus_mode
= SWM_FOCUS_FOLLOW
;
5227 else if (!strcmp(value
, "synergy"))
5228 focus_mode
= SWM_FOCUS_SYNERGY
;
5230 errx(1, "focus_mode");
5232 case SWM_S_DISABLE_BORDER
:
5233 disable_border
= atoi(value
);
5235 case SWM_S_BORDER_WIDTH
:
5236 border_width
= atoi(value
);
5238 case SWM_S_BAR_FONT
:
5240 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
5241 err(1, "setconfvalue: asprintf: failed to allocate "
5242 "memory for bar_fonts.");
5246 case SWM_S_BAR_ACTION
:
5248 if ((bar_argv
[0] = strdup(value
)) == NULL
)
5249 err(1, "setconfvalue: bar_action");
5251 case SWM_S_SPAWN_TERM
:
5252 free(spawn_term
[0]);
5253 if ((spawn_term
[0] = strdup(value
)) == NULL
)
5254 err(1, "setconfvalue: spawn_term");
5258 case SWM_S_DIALOG_RATIO
:
5259 dialog_ratio
= atof(value
);
5260 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
5263 case SWM_S_VERBOSE_LAYOUT
:
5264 verbose_layout
= atoi(value
);
5265 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
5267 layouts
[i
].l_string
= fancy_stacker
;
5269 layouts
[i
].l_string
= plain_stacker
;
5279 setconfmodkey(char *selector
, char *value
, int flags
)
5281 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
5282 update_modkey(Mod1Mask
);
5283 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
5284 update_modkey(Mod2Mask
);
5285 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
5286 update_modkey(Mod3Mask
);
5287 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
5288 update_modkey(Mod4Mask
);
5295 setconfcolor(char *selector
, char *value
, int flags
)
5297 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
5302 setconfregion(char *selector
, char *value
, int flags
)
5304 custom_region(value
);
5309 setautorun(char *selector
, char *value
, int flags
)
5319 if (getenv("SWM_STARTED"))
5323 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
5324 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5326 if (ws_id
< 0 || ws_id
>= SWM_WS_MAX
)
5327 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
5330 * This is a little intricate
5332 * If the pid already exists we simply reuse it because it means it was
5333 * used before AND not claimed by manage_window. We get away with
5334 * altering it in the parent after INSERT because this can not be a race
5337 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
5340 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
5342 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
5343 err(1, "setautorun: realloc");
5344 a
.argv
[argc
- 1] = ap
;
5347 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
5348 err(1, "setautorun: realloc");
5349 a
.argv
[argc
] = NULL
;
5351 if ((pid
= fork()) == 0) {
5352 spawn(ws_id
, &a
, 1);
5361 p
= calloc(1, sizeof *p
);
5364 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
5374 setlayout(char *selector
, char *value
, int flags
)
5376 int ws_id
, i
, x
, mg
, ma
, si
, raise
;
5377 int st
= SWM_V_STACK
;
5379 struct workspace
*ws
;
5381 if (getenv("SWM_STARTED"))
5385 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
5386 &ws_id
, &mg
, &ma
, &si
, &raise
, s
) != 6)
5387 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5388 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5391 if (ws_id
< 0 || ws_id
>= SWM_WS_MAX
)
5392 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
5394 if (!strcasecmp(s
, "vertical"))
5396 else if (!strcasecmp(s
, "horizontal"))
5398 else if (!strcasecmp(s
, "fullscreen"))
5401 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5402 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5405 for (i
= 0; i
< ScreenCount(display
); i
++) {
5406 ws
= (struct workspace
*)&screens
[i
].ws
;
5407 ws
[ws_id
].cur_layout
= &layouts
[st
];
5409 ws
[ws_id
].always_raise
= raise
;
5410 if (st
== SWM_MAX_STACK
)
5414 for (x
= 0; x
< abs(mg
); x
++) {
5415 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5416 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
5417 SWM_ARG_ID_MASTERSHRINK
);
5421 for (x
= 0; x
< abs(ma
); x
++) {
5422 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5423 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
5424 SWM_ARG_ID_MASTERDEL
);
5428 for (x
= 0; x
< abs(si
); x
++) {
5429 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
5430 si
>= 0 ? SWM_ARG_ID_STACKINC
:
5431 SWM_ARG_ID_STACKDEC
);
5439 /* config options */
5440 struct config_option
{
5442 int (*func
)(char*, char*, int);
5445 struct config_option configopt
[] = {
5446 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
5447 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
5448 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
5449 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
5450 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
5451 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
5452 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
5453 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
5454 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
5455 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
5456 { "keyboard_mapping", setkeymapping
, 0 },
5457 { "bind", setconfbinding
, 0 },
5458 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
5459 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
5460 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
5461 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
5462 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
5463 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
5464 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
5465 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
5466 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
5467 { "modkey", setconfmodkey
, 0 },
5468 { "program", setconfspawn
, 0 },
5469 { "quirk", setconfquirk
, 0 },
5470 { "region", setconfregion
, 0 },
5471 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
5472 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
5473 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
5474 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
5475 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
5476 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
5477 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
5478 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
},
5479 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
5480 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
5481 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
5482 { "autorun", setautorun
, 0 },
5483 { "layout", setlayout
, 0 },
5488 conf_load(char *filename
, int keymapping
)
5491 char *line
, *cp
, *optsub
, *optval
;
5492 size_t linelen
, lineno
= 0;
5493 int wordlen
, i
, optind
;
5494 struct config_option
*opt
;
5496 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
5498 if (filename
== NULL
) {
5499 fprintf(stderr
, "conf_load: no filename\n");
5502 if ((config
= fopen(filename
, "r")) == NULL
) {
5503 warn("conf_load: fopen: %s", filename
);
5507 while (!feof(config
)) {
5508 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
5511 err(1, "%s", filename
);
5516 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
5517 if (cp
[0] == '\0') {
5522 /* get config option */
5523 wordlen
= strcspn(cp
, "=[ \t\n");
5525 warnx("%s: line %zd: no option found",
5530 for (i
= 0; i
< LENGTH(configopt
); i
++) {
5531 opt
= &configopt
[i
];
5532 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
5533 strlen(opt
->optname
) == wordlen
) {
5539 warnx("%s: line %zd: unknown option %.*s",
5540 filename
, lineno
, wordlen
, cp
);
5543 if (keymapping
&& strcmp(opt
->optname
, "bind")) {
5544 warnx("%s: line %zd: invalid option %.*s",
5545 filename
, lineno
, wordlen
, cp
);
5549 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
5550 /* get [selector] if any */
5554 wordlen
= strcspn(cp
, "]");
5557 warnx("%s: line %zd: syntax error",
5562 if (asprintf(&optsub
, "%.*s", wordlen
, cp
) ==
5564 warnx("%s: line %zd: unable to allocate"
5565 "memory for selector", filename
,
5571 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
5573 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
5575 optval
= strdup(cp
);
5576 /* call function to deal with it all */
5577 if (configopt
[optind
].func(optsub
, optval
,
5578 configopt
[optind
].funcflags
) != 0) {
5579 fprintf(stderr
, "%s line %zd: %s\n",
5580 filename
, lineno
, line
);
5581 errx(1, "%s: line %zd: invalid data for %s",
5582 filename
, lineno
, configopt
[optind
].optname
);
5590 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
5597 DNPRINTF(SWM_D_CONF
, "conf_load: end with error.\n");
5603 set_child_transient(struct ws_win
*win
, Window
*trans
)
5605 struct ws_win
*parent
, *w
;
5606 XWMHints
*wmh
= NULL
;
5607 struct swm_region
*r
;
5608 struct workspace
*ws
;
5610 parent
= find_window(win
->transient
);
5612 parent
->child_trans
= win
;
5614 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
5615 " for 0x%lx trans 0x%lx\n", win
->id
, win
->transient
);
5617 if (win
->hints
== NULL
) {
5618 fprintf(stderr
, "no hints for 0x%lx\n", win
->id
);
5622 r
= root_to_region(win
->wa
.root
);
5624 /* parent doen't exist in our window list */
5625 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5629 if ((wmh
= XGetWMHints(display
, w
->id
)) == NULL
) {
5630 fprintf(stderr
, "can't get hints for 0x%lx\n",
5635 if (win
->hints
->window_group
!= wmh
->window_group
)
5638 w
->child_trans
= win
;
5639 win
->transient
= w
->id
;
5641 DNPRINTF(SWM_D_MISC
, "set_child_transient: asjusting "
5642 "transient to 0x%lx\n", win
->transient
);
5652 window_get_pid(Window win
)
5654 Atom actual_type_return
;
5655 int actual_format_return
= 0;
5656 unsigned long nitems_return
= 0;
5657 unsigned long bytes_after_return
= 0;
5661 unsigned char *prop
= NULL
;
5663 if (XGetWindowProperty(display
, win
,
5664 XInternAtom(display
, "_NET_WM_PID", False
), 0, 1, False
,
5665 XA_CARDINAL
, &actual_type_return
, &actual_format_return
,
5666 &nitems_return
, &bytes_after_return
,
5667 (unsigned char**)(void*)&pid
) != Success
)
5669 if (actual_type_return
!= XA_CARDINAL
)
5680 if (XGetWindowProperty(display
, win
,
5681 XInternAtom(display
, "_SWM_PID", False
), 0, SWM_PROPLEN
, False
,
5682 XA_STRING
, &actual_type_return
, &actual_format_return
,
5683 &nitems_return
, &bytes_after_return
, &prop
) != Success
)
5685 if (actual_type_return
!= XA_STRING
)
5690 ret
= strtonum((const char *)prop
, 0, UINT_MAX
, &errstr
);
5691 /* ignore error because strtonum returns 0 anyway */
5698 manage_window(Window id
)
5701 struct workspace
*ws
;
5702 struct ws_win
*win
, *ww
;
5703 int format
, i
, ws_idx
, n
, border_me
= 0;
5704 unsigned long nitems
, bytes
;
5705 Atom ws_idx_atom
= 0, type
;
5706 Atom
*prot
= NULL
, *pp
;
5707 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
5708 struct swm_region
*r
;
5714 if ((win
= find_window(id
)) != NULL
)
5715 return (win
); /* already being managed */
5717 /* see if we are on the unmanaged list */
5718 if ((win
= find_unmanaged_window(id
)) != NULL
) {
5719 DNPRINTF(SWM_D_MISC
, "manage_window: previously unmanaged "
5720 "window: 0x%lx\n", win
->id
);
5721 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5722 if (win
->transient
) {
5723 set_child_transient(win
, &trans
);
5724 } if (trans
&& (ww
= find_window(trans
)))
5725 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
5727 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
5728 ewmh_update_actions(win
);
5732 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
5733 err(1, "manage_window: calloc: failed to allocate memory for "
5738 /* see if we need to override the workspace */
5739 p
= find_pid(window_get_pid(id
));
5741 /* Get all the window data in one shot */
5742 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
5744 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
5745 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
5747 XGetWindowAttributes(display
, id
, &win
->wa
);
5748 XGetWMNormalHints(display
, id
, &win
->sh
, &mask
);
5749 win
->hints
= XGetWMHints(display
, id
);
5750 XGetTransientForHint(display
, id
, &trans
);
5752 win
->transient
= trans
;
5753 set_child_transient(win
, &trans
);
5754 DNPRINTF(SWM_D_MISC
, "manage_window: window: 0x%lx, "
5755 "transient: 0x%lx\n", win
->id
, win
->transient
);
5758 /* get supported protocols */
5759 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
5760 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++) {
5761 if (*pp
== takefocus
)
5762 win
->take_focus
= 1;
5764 win
->can_delete
= 1;
5770 win
->iconic
= get_iconic(win
);
5773 * Figure out where to put the window. If it was previously assigned to
5774 * a workspace (either by spawn() or manually moving), and isn't
5775 * transient, * put it in the same workspace
5777 r
= root_to_region(win
->wa
.root
);
5779 ws
= &r
->s
->ws
[p
->ws
];
5780 TAILQ_REMOVE(&pidlist
, p
, entry
);
5783 } else if (prop
&& win
->transient
== 0) {
5784 DNPRINTF(SWM_D_PROP
, "manage_window: get _SWM_WS: %s\n", prop
);
5785 ws_idx
= strtonum((const char *)prop
, 0, 9, &errstr
);
5787 DNPRINTF(SWM_D_EVENT
, "manage_window: window: #%s: %s",
5790 ws
= &r
->s
->ws
[ws_idx
];
5793 /* this should launch transients in the same ws as parent */
5795 if ((ww
= find_window(trans
)) != NULL
)
5802 "manage_window: fix this "
5808 /* set up the window layout */
5811 win
->s
= r
->s
; /* this never changes */
5812 if (trans
&& (ww
= find_window(trans
)))
5813 TAILQ_INSERT_AFTER(&ws
->winlist
, ww
, win
, entry
);
5815 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
5817 WIDTH(win
) = win
->wa
.width
;
5818 HEIGHT(win
) = win
->wa
.height
;
5821 win
->g_floatvalid
= 0;
5822 win
->floatmaxed
= 0;
5823 win
->ewmh_flags
= 0;
5825 /* Set window properties so we can remember this after reincarnation */
5826 if (ws_idx_atom
&& prop
== NULL
&&
5827 snprintf((char *)ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) <
5829 DNPRINTF(SWM_D_PROP
, "manage_window: set _SWM_WS: %s\n",
5831 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
5832 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
5837 ewmh_autoquirk(win
);
5839 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
5840 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, name: %s\n",
5841 win
->ch
.res_class
, win
->ch
.res_name
);
5843 /* java is retarded so treat it special */
5844 if (strstr(win
->ch
.res_name
, "sun-awt")) {
5849 for (i
= 0; i
< quirks_length
; i
++){
5850 if (!strcmp(win
->ch
.res_class
, quirks
[i
].class) &&
5851 !strcmp(win
->ch
.res_name
, quirks
[i
].name
)) {
5852 DNPRINTF(SWM_D_CLASS
, "manage_window: found: "
5853 "class: %s, name: %s\n", win
->ch
.res_class
,
5855 if (quirks
[i
].quirk
& SWM_Q_FLOAT
) {
5859 win
->quirks
= quirks
[i
].quirk
;
5864 /* alter window position if quirky */
5865 if (win
->quirks
& SWM_Q_ANYWHERE
) {
5866 win
->manual
= 1; /* don't center the quirky windows */
5867 bzero(&wc
, sizeof wc
);
5869 if (bar_enabled
&& Y(win
) < bar_height
) {
5870 Y(win
) = wc
.y
= bar_height
;
5873 if (WIDTH(win
) + X(win
) > WIDTH(r
)) {
5874 X(win
) = wc
.x
= WIDTH(r
) - WIDTH(win
) - 2;
5880 /* Reset font sizes (the bruteforce way; no default keybinding). */
5881 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
5882 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
5883 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
5884 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
5885 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
5888 ewmh_get_win_state(win
);
5889 ewmh_update_actions(win
);
5890 ewmh_update_win_state(win
, None
, _NET_WM_STATE_REMOVE
);
5894 bzero(&wc
, sizeof wc
);
5895 wc
.border_width
= border_width
;
5896 mask
= CWBorderWidth
;
5897 XConfigureWindow(display
, win
->id
, mask
, &wc
);
5900 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
5901 PropertyChangeMask
| StructureNotifyMask
);
5903 /* floaters need to be mapped if they are in the current workspace */
5904 if ((win
->floating
|| win
->transient
) && (ws
->idx
== r
->ws
->idx
))
5905 XMapRaised(display
, win
->id
);
5911 free_window(struct ws_win
*win
)
5913 DNPRINTF(SWM_D_MISC
, "free_window: window: 0x%lx\n", win
->id
);
5918 /* needed for restart wm */
5919 set_win_state(win
, WithdrawnState
);
5921 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
5923 if (win
->ch
.res_class
)
5924 XFree(win
->ch
.res_class
);
5925 if (win
->ch
.res_name
)
5926 XFree(win
->ch
.res_name
);
5931 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
5937 unmanage_window(struct ws_win
*win
)
5939 struct ws_win
*parent
;
5944 DNPRINTF(SWM_D_MISC
, "unmanage_window: window: 0x%lx\n", win
->id
);
5946 if (win
->transient
) {
5947 parent
= find_window(win
->transient
);
5949 parent
->child_trans
= NULL
;
5952 /* focus on root just in case */
5953 XSetInputFocus(display
, PointerRoot
, PointerRoot
, CurrentTime
);
5959 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
5960 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
5966 focus_magic(struct ws_win
*win
)
5968 DNPRINTF(SWM_D_FOCUS
, "focus_magic: window: 0x%lx\n", WINID(win
));
5973 if (win
->child_trans
) {
5974 /* win = parent & has a transient so focus on that */
5976 focus_win(win
->child_trans
);
5977 if (win
->child_trans
->take_focus
)
5978 client_msg(win
, takefocus
);
5980 /* make sure transient hasn't dissapeared */
5981 if (validate_win(win
->child_trans
) == 0) {
5982 focus_win(win
->child_trans
);
5983 if (win
->child_trans
->take_focus
)
5984 client_msg(win
->child_trans
, takefocus
);
5986 win
->child_trans
= NULL
;
5988 if (win
->take_focus
)
5989 client_msg(win
, takefocus
);
5995 if (win
->take_focus
)
5996 client_msg(win
, takefocus
);
6003 DNPRINTF(SWM_D_EVENT
, "expose: window: 0x%lx\n", e
->xexpose
.window
);
6010 XKeyEvent
*ev
= &e
->xkey
;
6013 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
6014 TAILQ_FOREACH(kp
, &keys
, entry
)
6015 if (keysym
== kp
->keysym
6016 && CLEANMASK(kp
->mod
) == CLEANMASK(ev
->state
)
6017 && keyfuncs
[kp
->funcid
].func
) {
6018 if (kp
->funcid
== kf_spawn_custom
)
6020 root_to_region(ev
->root
),
6021 &(keyfuncs
[kp
->funcid
].args
),
6025 keyfuncs
[kp
->funcid
].func(
6026 root_to_region(ev
->root
),
6027 &(keyfuncs
[kp
->funcid
].args
)
6033 buttonpress(XEvent
*e
)
6037 XButtonPressedEvent
*ev
= &e
->xbutton
;
6039 if ((win
= find_window(ev
->window
)) == NULL
)
6043 action
= client_click
;
6045 for (i
= 0; i
< LENGTH(buttons
); i
++)
6046 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
6047 buttons
[i
].button
== ev
->button
&&
6048 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
6049 buttons
[i
].func(win
, &buttons
[i
].args
);
6053 configurerequest(XEvent
*e
)
6055 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
6060 if ((win
= find_window(ev
->window
)) == NULL
)
6061 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6064 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: 0x%lx, new: %s\n",
6065 ev
->window
, YESNO(new));
6068 bzero(&wc
, sizeof wc
);
6071 wc
.width
= ev
->width
;
6072 wc
.height
= ev
->height
;
6073 wc
.border_width
= ev
->border_width
;
6074 wc
.sibling
= ev
->above
;
6075 wc
.stack_mode
= ev
->detail
;
6076 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
6078 config_win(win
, ev
);
6083 configurenotify(XEvent
*e
)
6088 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: 0x%lx\n",
6089 e
->xconfigure
.window
);
6091 win
= find_window(e
->xconfigure
.window
);
6093 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
6097 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6098 drain_enter_notify();
6103 destroynotify(XEvent
*e
)
6106 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
6108 DNPRINTF(SWM_D_EVENT
, "destroynotify: window: 0x%lx\n", ev
->window
);
6110 if ((win
= find_window(ev
->window
)) == NULL
) {
6111 if ((win
= find_unmanaged_window(ev
->window
)) == NULL
)
6117 /* make sure we focus on something */
6120 unmanage_window(win
);
6122 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6123 drain_enter_notify();
6128 enternotify(XEvent
*e
)
6130 XCrossingEvent
*ev
= &e
->xcrossing
;
6135 Window focus_return
;
6136 int revert_to_return
;
6138 DNPRINTF(SWM_D_FOCUS
, "enternotify: window: 0x%lx, mode: %d, detail: "
6139 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6140 "state: %d\n", ev
->window
, ev
->mode
, ev
->detail
, ev
->root
,
6141 ev
->subwindow
, YESNO(ev
->same_screen
), YESNO(ev
->focus
), ev
->state
);
6143 if (ev
->mode
!= NotifyNormal
) {
6144 DNPRINTF(SWM_D_EVENT
, "skip enternotify: generated by "
6149 switch (focus_mode
) {
6150 case SWM_FOCUS_DEFAULT
:
6152 case SWM_FOCUS_FOLLOW
:
6154 case SWM_FOCUS_SYNERGY
:
6157 * all these checks need to be in this order because the
6158 * XCheckTypedWindowEvent relies on weeding out the previous events
6160 * making this code an option would enable a follow mouse for focus
6165 * state is set when we are switching workspaces and focus is set when
6166 * the window or a subwindow already has focus (occurs during restart).
6168 * Only honor the focus flag if last_focus_event is not FocusOut,
6169 * this allows scrotwm to continue to control focus when another
6170 * program is also playing with it.
6172 if (ev
->state
|| (ev
->focus
&& last_focus_event
!= FocusOut
)) {
6173 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: focus\n");
6178 * happens when a window is created or destroyed and the border
6179 * crosses the mouse pointer and when switching ws
6181 * we need the subwindow test to see if we came from root in order
6182 * to give focus to floaters
6184 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyVirtual
&&
6185 ev
->subwindow
== 0) {
6186 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: NotifyVirtual\n");
6190 /* this window already has focus */
6191 if (ev
->mode
== NotifyNormal
&& ev
->detail
== NotifyInferior
) {
6192 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win has focus\n");
6196 /* this window is being deleted or moved to another ws */
6197 if (XCheckTypedWindowEvent(display
, ev
->window
, ConfigureNotify
,
6199 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: configurenotify\n");
6200 XPutBackEvent(display
, &cne
);
6204 if ((win
= find_window(ev
->window
)) == NULL
) {
6205 DNPRINTF(SWM_D_EVENT
, "ignoring enternotify: win == NULL\n");
6210 * In fullstack kill all enters unless they come from a different ws
6211 * (i.e. another region) or focus has been grabbed externally.
6213 if (win
->ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
&&
6214 last_focus_event
!= FocusOut
) {
6215 XGetInputFocus(display
, &focus_return
, &revert_to_return
);
6216 if ((w
= find_window(focus_return
)) == NULL
||
6218 DNPRINTF(SWM_D_EVENT
, "ignoring event: fullstack\n");
6226 if ((win
= find_window(ev
->window
)) == NULL
) {
6227 DNPRINTF(SWM_D_EVENT
, "skip enternotify: window is NULL\n");
6232 * if we have more enternotifies let them handle it in due time
6234 if (XCheckTypedEvent(display
, EnterNotify
, &cne
) == True
) {
6235 DNPRINTF(SWM_D_EVENT
,
6236 "ignoring enternotify: got more enternotify\n");
6237 XPutBackEvent(display
, &cne
);
6244 /* lets us use one switch statement for arbitrary mode/detail combinations */
6245 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6248 focusevent(XEvent
*e
)
6252 u_int32_t mode_detail
;
6253 XFocusChangeEvent
*ev
= &e
->xfocus
;
6255 DNPRINTF(SWM_D_EVENT
, "focusevent: %s window: 0x%lx mode: %d "
6256 "detail: %d\n", ev
->type
== FocusIn
? "entering" : "leaving",
6257 ev
->window
, ev
->mode
, ev
->detail
);
6259 if (last_focus_event
== ev
->type
) {
6260 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent: bad ordering\n");
6264 last_focus_event
= ev
->type
;
6265 mode_detail
= MERGE_MEMBERS(ev
->mode
, ev
->detail
);
6267 switch (mode_detail
) {
6268 /* synergy client focus operations */
6269 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinear
):
6270 case MERGE_MEMBERS(NotifyNormal
, NotifyNonlinearVirtual
):
6272 /* synergy server focus operations */
6273 case MERGE_MEMBERS(NotifyWhileGrabbed
, NotifyNonlinear
):
6275 /* Entering applications like rdesktop that mangle the pointer */
6276 case MERGE_MEMBERS(NotifyNormal
, NotifyPointer
):
6278 if ((win
= find_window(e
->xfocus
.window
)) != NULL
&& win
->ws
->r
)
6279 XSetWindowBorder(display
, win
->id
,
6280 win
->ws
->r
->s
->c
[ev
->type
== FocusIn
?
6281 SWM_S_COLOR_FOCUS
: SWM_S_COLOR_UNFOCUS
].color
);
6284 fprintf(stderr
, "ignoring focusevent\n");
6285 DNPRINTF(SWM_D_FOCUS
, "ignoring focusevent\n");
6292 mapnotify(XEvent
*e
)
6295 XMapEvent
*ev
= &e
->xmap
;
6297 DNPRINTF(SWM_D_EVENT
, "mapnotify: window: 0x%lx\n", ev
->window
);
6299 win
= manage_window(ev
->window
);
6301 set_win_state(win
, NormalState
);
6305 mappingnotify(XEvent
*e
)
6307 XMappingEvent
*ev
= &e
->xmapping
;
6309 XRefreshKeyboardMapping(ev
);
6310 if (ev
->request
== MappingKeyboard
)
6315 maprequest(XEvent
*e
)
6318 struct swm_region
*r
;
6319 XWindowAttributes wa
;
6320 XMapRequestEvent
*ev
= &e
->xmaprequest
;
6322 DNPRINTF(SWM_D_EVENT
, "maprequest: window: 0x%lx\n",
6323 e
->xmaprequest
.window
);
6325 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
6327 if (wa
.override_redirect
)
6330 win
= manage_window(e
->xmaprequest
.window
);
6332 return; /* can't happen */
6336 /* make new win focused */
6337 r
= root_to_region(win
->wa
.root
);
6338 if (win
->ws
== r
->ws
)
6343 propertynotify(XEvent
*e
)
6346 XPropertyEvent
*ev
= &e
->xproperty
;
6349 name
= XGetAtomName(display
, ev
->atom
);
6350 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: 0x%lx, atom: %s\n",
6355 win
= find_window(ev
->window
);
6359 if (ev
->state
== PropertyDelete
&& ev
->atom
== a_swm_iconic
) {
6360 update_iconic(win
, 0);
6361 XMapRaised(display
, win
->id
);
6369 case XA_WM_NORMAL_HINTS
:
6371 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
6372 fprintf(stderr
, "normal hints: flag 0x%x\n", win
->sh
.flags
);
6373 if (win
->sh
.flags
& PMinSize
) {
6374 WIDTH(win
) = win
->sh
.min_width
;
6375 HEIGHT(win
) = win
->sh
.min_height
;
6376 fprintf(stderr
, "min %d %d\n", WIDTH(win
), HEIGHT(win
));
6378 XMoveResizeWindow(display
, win
->id
,
6379 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
));
6382 if (title_name_enabled
|| title_class_enabled
)
6386 if (window_name_enabled
)
6395 unmapnotify(XEvent
*e
)
6399 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: 0x%lx\n", e
->xunmap
.window
);
6401 /* determine if we need to help unmanage this window */
6402 win
= find_window(e
->xunmap
.window
);
6406 if (getstate(e
->xunmap
.window
) == NormalState
) {
6407 unmanage_window(win
);
6410 /* giant hack for apps that don't destroy transient windows */
6411 /* eat a bunch of events to prevent remanaging the window */
6413 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6414 EnterWindowMask
, &cne
))
6416 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6417 StructureNotifyMask
, &cne
))
6419 while (XCheckWindowEvent(display
, e
->xunmap
.window
,
6420 SubstructureNotifyMask
, &cne
))
6422 /* resend unmap because we ated it */
6423 XUnmapWindow(display
, e
->xunmap
.window
);
6426 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6427 drain_enter_notify();
6431 visibilitynotify(XEvent
*e
)
6434 struct swm_region
*r
;
6436 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: 0x%lx\n",
6437 e
->xvisibility
.window
);
6438 if (e
->xvisibility
.state
== VisibilityUnobscured
)
6439 for (i
= 0; i
< ScreenCount(display
); i
++)
6440 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6441 if (e
->xvisibility
.window
== r
->bar_window
)
6446 clientmessage(XEvent
*e
)
6448 XClientMessageEvent
*ev
;
6453 win
= find_window(ev
->window
);
6457 DNPRINTF(SWM_D_EVENT
, "clientmessage: window: 0x%lx, type: %ld\n",
6458 ev
->window
, ev
->message_type
);
6460 if (ev
->message_type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
6461 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
6464 if (ev
->message_type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
6465 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
6466 if (win
->can_delete
)
6467 client_msg(win
, adelete
);
6469 XKillClient(display
, win
->id
);
6471 if (ev
->message_type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
6472 DNPRINTF(SWM_D_EVENT
,
6473 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
6474 if (win
->floating
) {
6475 if (ev
->data
.l
[0] & (1<<8)) /* x */
6476 X(win
) = ev
->data
.l
[1];
6477 if (ev
->data
.l
[0] & (1<<9)) /* y */
6478 Y(win
) = ev
->data
.l
[2];
6479 if (ev
->data
.l
[0] & (1<<10)) /* width */
6480 WIDTH(win
) = ev
->data
.l
[3];
6481 if (ev
->data
.l
[0] & (1<<11)) /* height */
6482 HEIGHT(win
) = ev
->data
.l
[4];
6487 /* TODO: Change stack sizes */
6488 /* notify no change was made. */
6489 config_win(win
, NULL
);
6492 if (ev
->message_type
== ewmh
[_NET_WM_STATE
].atom
) {
6493 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
6494 ewmh_update_win_state(win
, ev
->data
.l
[1], ev
->data
.l
[0]);
6496 ewmh_update_win_state(win
, ev
->data
.l
[2],
6504 xerror_start(Display
*d
, XErrorEvent
*ee
)
6511 xerror(Display
*d
, XErrorEvent
*ee
)
6513 /* fprintf(stderr, "error: %p %p\n", display, ee); */
6521 xerrorxlib
= XSetErrorHandler(xerror_start
);
6523 /* this causes an error if some other window manager is running */
6524 XSelectInput(display
, DefaultRootWindow(display
),
6525 SubstructureRedirectMask
);
6526 XSync(display
, False
);
6530 XSetErrorHandler(xerror
);
6531 XSync(display
, False
);
6536 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
6538 struct swm_region
*r
, *n
;
6539 struct workspace
*ws
= NULL
;
6542 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
6543 s
->idx
, w
, h
, x
, y
);
6545 /* remove any conflicting regions */
6546 n
= TAILQ_FIRST(&s
->rl
);
6549 n
= TAILQ_NEXT(r
, entry
);
6550 if (X(r
) < (x
+ w
) &&
6551 (X(r
) + WIDTH(r
)) > x
&&
6553 (Y(r
) + HEIGHT(r
)) > y
) {
6554 if (r
->ws
->r
!= NULL
)
6555 r
->ws
->old_r
= r
->ws
->r
;
6557 XDestroyWindow(display
, r
->bar_window
);
6558 TAILQ_REMOVE(&s
->rl
, r
, entry
);
6559 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
6563 /* search old regions for one to reuse */
6565 /* size + location match */
6566 TAILQ_FOREACH(r
, &s
->orl
, entry
)
6567 if (X(r
) == x
&& Y(r
) == y
&&
6568 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
6572 TAILQ_FOREACH(r
, &s
->orl
, entry
)
6573 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
6577 TAILQ_REMOVE(&s
->orl
, r
, entry
);
6578 /* try to use old region's workspace */
6579 if (r
->ws
->r
== NULL
)
6582 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
6583 err(1, "new_region: calloc: failed to allocate memory "
6586 /* if we don't have a workspace already, find one */
6588 for (i
= 0; i
< SWM_WS_MAX
; i
++)
6589 if (s
->ws
[i
].r
== NULL
) {
6596 errx(1, "new_region: no free workspaces");
6607 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
6613 #ifdef SWM_XRR_HAS_CRTC
6615 XRRScreenResources
*sr
;
6618 #endif /* SWM_XRR_HAS_CRTC */
6619 struct swm_region
*r
;
6622 if (i
>= ScreenCount(display
))
6623 errx(1, "scan_xrandr: invalid screen");
6625 /* remove any old regions */
6626 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
6627 r
->ws
->old_r
= r
->ws
->r
= NULL
;
6628 XDestroyWindow(display
, r
->bar_window
);
6629 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
6630 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
6634 /* map virtual screens onto physical screens */
6635 #ifdef SWM_XRR_HAS_CRTC
6636 if (xrandr_support
) {
6637 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
6639 new_region(&screens
[i
], 0, 0,
6640 DisplayWidth(display
, i
),
6641 DisplayHeight(display
, i
));
6645 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
6646 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
6647 if (ci
->noutput
== 0)
6650 if (ci
!= NULL
&& ci
->mode
== None
)
6651 new_region(&screens
[i
], 0, 0,
6652 DisplayWidth(display
, i
),
6653 DisplayHeight(display
, i
));
6655 new_region(&screens
[i
],
6656 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
6659 XRRFreeCrtcInfo(ci
);
6660 XRRFreeScreenResources(sr
);
6662 #endif /* SWM_XRR_HAS_CRTC */
6664 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
6665 DisplayHeight(display
, i
));
6670 screenchange(XEvent
*e
) {
6671 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
6672 struct swm_region
*r
;
6675 DNPRINTF(SWM_D_EVENT
, "screenchange: root: 0x%lx\n", xe
->root
);
6677 if (!XRRUpdateConfiguration(e
))
6680 /* silly event doesn't include the screen index */
6681 for (i
= 0; i
< ScreenCount(display
); i
++)
6682 if (screens
[i
].root
== xe
->root
)
6684 if (i
>= ScreenCount(display
))
6685 errx(1, "screenchange: screen not found");
6687 /* brute force for now, just re-enumerate the regions */
6690 /* add bars to all regions */
6691 for (i
= 0; i
< ScreenCount(display
); i
++)
6692 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
6695 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6696 drain_enter_notify();
6702 Window d1
, d2
, *wins
= NULL
;
6703 XWindowAttributes wa
;
6708 for (i
= 0; i
< ScreenCount(display
); i
++) {
6709 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
6712 /* attach windows to a region */
6713 /* normal windows */
6714 for (j
= 0; j
< no
; j
++) {
6715 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6716 wa
.override_redirect
||
6717 XGetTransientForHint(display
, wins
[j
], &d1
))
6720 state
= getstate(wins
[j
]);
6721 manage
= state
== IconicState
;
6722 if (wa
.map_state
== IsViewable
|| manage
)
6723 manage_window(wins
[j
]);
6725 /* transient windows */
6726 for (j
= 0; j
< no
; j
++) {
6727 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
6728 wa
.override_redirect
)
6731 state
= getstate(wins
[j
]);
6732 manage
= state
== IconicState
;
6733 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
6735 manage_window(wins
[j
]);
6748 int errorbase
, major
, minor
;
6749 struct workspace
*ws
;
6752 if ((screens
= calloc(ScreenCount(display
),
6753 sizeof(struct swm_screen
))) == NULL
)
6754 err(1, "setup_screens: calloc: failed to allocate memory for "
6757 /* initial Xrandr setup */
6758 xrandr_support
= XRRQueryExtension(display
,
6759 &xrandr_eventbase
, &errorbase
);
6761 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
6764 /* map physical screens */
6765 for (i
= 0; i
< ScreenCount(display
); i
++) {
6766 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
6768 TAILQ_INIT(&screens
[i
].rl
);
6769 TAILQ_INIT(&screens
[i
].orl
);
6770 screens
[i
].root
= RootWindow(display
, i
);
6772 /* set default colors */
6773 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
6774 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
6775 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
6776 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
6777 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
6779 /* create graphics context on screen with default font color */
6780 screens
[i
].bar_gc
= XCreateGC(display
, screens
[i
].root
, 0,
6783 XSetForeground(display
, screens
[i
].bar_gc
,
6784 screens
[i
].c
[SWM_S_COLOR_BAR_FONT
].color
);
6786 /* set default cursor */
6787 XDefineCursor(display
, screens
[i
].root
,
6788 XCreateFontCursor(display
, XC_left_ptr
));
6790 /* init all workspaces */
6791 /* XXX these should be dynamically allocated too */
6792 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
6793 ws
= &screens
[i
].ws
[j
];
6799 TAILQ_INIT(&ws
->winlist
);
6800 TAILQ_INIT(&ws
->unmanagedlist
);
6802 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
6803 if (layouts
[k
].l_config
!= NULL
)
6804 layouts
[k
].l_config(ws
,
6805 SWM_ARG_ID_STACKINIT
);
6806 ws
->cur_layout
= &layouts
[0];
6807 ws
->cur_layout
->l_string(ws
);
6813 XRRSelectInput(display
, screens
[i
].root
,
6814 RRScreenChangeNotifyMask
);
6821 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
6822 err(1, "setup_globals: strdup: failed to allocate memory.");
6824 if ((spawn_term
[0] = strdup("xterm")) == NULL
)
6825 err(1, "setup_globals: strdup: failed to allocate memory.");
6827 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
6828 err(1, "setup_globals: strdup: failed to allocate memory.");
6835 Atom netwmcheck
, netwmname
, utf8_string
;
6838 /* work around sun jdk bugs, code from wmname */
6839 netwmcheck
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
6840 netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
6841 utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6842 for (i
= 0; i
< ScreenCount(display
); i
++) {
6843 root
= screens
[i
].root
;
6844 win
= XCreateSimpleWindow(display
,root
, 0, 0, 1, 1, 0,
6845 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
,
6846 screens
[i
].c
[SWM_S_COLOR_UNFOCUS
].color
);
6848 XChangeProperty(display
, root
, netwmcheck
, XA_WINDOW
, 32,
6849 PropModeReplace
, (unsigned char *)&win
,1);
6850 XChangeProperty(display
, win
, netwmcheck
, XA_WINDOW
, 32,
6851 PropModeReplace
, (unsigned char *)&win
,1);
6852 XChangeProperty(display
, win
, netwmname
, utf8_string
, 8,
6853 PropModeReplace
, (unsigned char*)"LG3D", strlen("LG3D"));
6858 main(int argc
, char *argv
[])
6860 struct swm_region
*r
, *rr
;
6861 struct ws_win
*winfocus
= NULL
;
6864 char conf
[PATH_MAX
], *cfile
= NULL
;
6869 struct sigaction sact
;
6872 fprintf(stderr
, "Welcome to scrotwm V%s Build: %s\n",
6873 SCROTWM_VERSION
, buildstr
);
6874 if (!setlocale(LC_CTYPE
, "") || !setlocale(LC_TIME
, "") ||
6876 warnx("no locale support");
6878 if (!X_HAVE_UTF8_STRING
)
6879 warnx("no UTF-8 support");
6881 if (!(display
= XOpenDisplay(0)))
6882 errx(1, "can not open display");
6885 errx(1, "other wm running");
6887 /* handle some signals */
6888 bzero(&sact
, sizeof(sact
));
6889 sigemptyset(&sact
.sa_mask
);
6891 sact
.sa_handler
= sighdlr
;
6892 sigaction(SIGINT
, &sact
, NULL
);
6893 sigaction(SIGQUIT
, &sact
, NULL
);
6894 sigaction(SIGTERM
, &sact
, NULL
);
6895 sigaction(SIGHUP
, &sact
, NULL
);
6897 sact
.sa_handler
= sighdlr
;
6898 sact
.sa_flags
= SA_NOCLDSTOP
;
6899 sigaction(SIGCHLD
, &sact
, NULL
);
6901 astate
= XInternAtom(display
, "WM_STATE", False
);
6902 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
6903 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
6904 takefocus
= XInternAtom(display
, "WM_TAKE_FOCUS", False
);
6905 a_wmname
= XInternAtom(display
, "WM_NAME", False
);
6906 a_netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
6907 a_utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
6908 a_string
= XInternAtom(display
, "STRING", False
);
6909 a_swm_iconic
= XInternAtom(display
, "_SWM_ICONIC", False
);
6911 /* look for local and global conf file */
6912 pwd
= getpwuid(getuid());
6914 errx(1, "invalid user: %d", getuid());
6923 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
6924 if (stat(conf
, &sb
) != -1) {
6925 if (S_ISREG(sb
.st_mode
))
6928 /* try global conf file */
6929 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
6930 if (!stat(conf
, &sb
))
6931 if (S_ISREG(sb
.st_mode
))
6935 /* load conf (if any) and refresh font color in bar graphics contexts */
6936 if (cfile
&& conf_load(cfile
, SWM_CONF_DEFAULT
) == 0)
6937 for (i
= 0; i
< ScreenCount(display
); ++i
)
6938 XSetForeground(display
, screens
[i
].bar_gc
,
6939 screens
[i
].c
[SWM_S_COLOR_BAR_FONT
].color
);
6942 /* set some values to work around bad programs */
6944 /* grab existing windows (before we build the bars) */
6947 if (getenv("SWM_STARTED") == NULL
)
6948 setenv("SWM_STARTED", "YES", 1);
6950 /* setup all bars */
6951 for (i
= 0; i
< ScreenCount(display
); i
++)
6952 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
6953 if (winfocus
== NULL
)
6954 winfocus
= TAILQ_FIRST(&r
->ws
->winlist
);
6962 if (focus_mode
== SWM_FOCUS_DEFAULT
)
6963 drain_enter_notify();
6965 xfd
= ConnectionNumber(display
);
6967 while (XPending(display
)) {
6968 XNextEvent(display
, &e
);
6971 if (e
.type
< LASTEvent
) {
6972 DNPRINTF(SWM_D_EVENTQ
,"XEvent: handled: %s, "
6973 "window: 0x%lx, type: %s (%d), %d remaining"
6974 "\n", YESNO(handler
[e
.type
]),
6975 e
.xany
.window
, geteventname(&e
),
6976 e
.type
, QLength(display
));
6978 if (handler
[e
.type
])
6979 handler
[e
.type
](&e
);
6981 DNPRINTF(SWM_D_EVENTQ
, "XRandr Event: window: "
6982 "0x%lx, type: %s (%d)\n", e
.xany
.window
,
6983 xrandr_geteventname(&e
), e
.type
);
6985 switch (e
.type
- xrandr_eventbase
) {
6986 case RRScreenChangeNotify
:
6995 /* if we are being restarted go focus on first window */
6997 rr
= winfocus
->ws
->r
;
6999 /* not a visible window */
7003 /* move pointer to first screen if multi screen */
7004 if (ScreenCount(display
) > 1 || outputs
> 1)
7005 XWarpPointer(display
, None
, rr
->s
[0].root
,
7007 Y(rr
) + (bar_enabled
? bar_height
: 0));
7009 a
.id
= SWM_ARG_ID_FOCUSCUR
;
7019 if (select(xfd
+ 1, &rd
, NULL
, NULL
, &tv
) == -1)
7021 DNPRINTF(SWM_D_MISC
, "select failed");
7022 if (restart_wm
== 1)
7023 restart(NULL
, NULL
);
7024 if (search_resp
== 1)
7037 for (i
= 0; i
< ScreenCount(display
); ++i
)
7038 if (screens
[i
].bar_gc
!= NULL
)
7039 XFreeGC(display
, screens
[i
].bar_gc
);
7041 XFreeFontSet(display
, bar_fs
);
7042 XCloseDisplay(display
);