]>
code.delx.au - spectrwm/blob - scrotwm.c
3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Much code and ideas taken from dwm under the following license:
20 * MIT/X Consortium License
22 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
23 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
24 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
25 * 2007 Premysl Hruby <dfenze at gmail dot com>
26 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
27 * 2007 Christof Musik <christof at sendfax dot de>
28 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
29 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
30 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, including without limitation
35 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 * and/or sell copies of the Software, and to permit persons to whom the
37 * Software is furnished to do so, subject to the following conditions:
39 * The above copyright notice and this permission notice shall be included in
40 * all copies or substantial portions of the Software.
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
51 #define SWM_VERSION "0.5"
65 #include <sys/types.h>
69 #include <sys/queue.h>
70 #include <sys/param.h>
71 #include <sys/select.h>
73 #include <X11/cursorfont.h>
74 #include <X11/keysym.h>
75 #include <X11/Xatom.h>
77 #include <X11/Xproto.h>
78 #include <X11/Xutil.h>
79 #include <X11/extensions/Xrandr.h>
82 # error XRandR versions less than 1.0 are not supported
87 #define SWM_XRR_HAS_CRTC
91 /* #define SWM_DEBUG */
93 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
94 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
95 #define SWM_D_MISC 0x0001
96 #define SWM_D_EVENT 0x0002
97 #define SWM_D_WS 0x0004
98 #define SWM_D_FOCUS 0x0008
99 #define SWM_D_MOVE 0x0010
100 #define SWM_D_STACK 0x0020
102 u_int32_t swm_debug
= 0
111 #define DPRINTF(x...)
112 #define DNPRINTF(n,x...)
115 #define LENGTH(x) (sizeof x / sizeof x[0])
116 #define MODKEY Mod1Mask
117 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
119 #define X(r) (r)->g.x
120 #define Y(r) (r)->g.y
121 #define WIDTH(r) (r)->g.w
122 #define HEIGHT(r) (r)->g.h
126 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
129 int xrandr_eventbase
;
130 int ignore_enter
= 0;
131 unsigned int numlockmask
= 0;
135 double dialog_ratio
= .6;
137 #define SWM_BAR_MAX (128)
138 sig_atomic_t bar_alarm
= 0;
145 char *bar_fonts
[] = {
146 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
147 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
151 /* terminal + args */
152 char *spawn_term
[] = { "xterm", NULL
};
153 char *spawn_menu
[] = { "dmenu_run", NULL
};
155 /* layout manager data */
156 struct swm_geometry
{
166 /* virtual "screens" */
168 TAILQ_ENTRY(swm_region
) entry
;
169 struct swm_geometry g
;
171 struct workspace
*ws
; /* current workspace on this region */
172 struct swm_screen
*s
; /* screen idx */
174 TAILQ_HEAD(swm_region_list
, swm_region
);
178 TAILQ_ENTRY(ws_win
) entry
;
180 struct swm_geometry g
;
184 struct workspace
*ws
; /* always valid */
185 struct swm_screen
*s
; /* always valid, never changes */
186 XWindowAttributes wa
;
188 TAILQ_HEAD(ws_win_list
, ws_win
);
190 /* layout handlers */
192 void vertical_init(struct workspace
*);
193 void vertical_resize(struct workspace
*, int);
194 void vertical_stack(struct workspace
*, struct swm_geometry
*);
195 void horizontal_init(struct workspace
*);
196 void horizontal_resize(struct workspace
*, int);
197 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
198 void max_init(struct workspace
*);
199 void max_stack(struct workspace
*, struct swm_geometry
*);
202 void (*l_init
)(struct workspace
*); /* init/reset */
203 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
204 void (*l_resize
)(struct workspace
*, int);
206 /* init stack, resize */
207 { vertical_init
, vertical_stack
, vertical_resize
},
208 { horizontal_init
, horizontal_stack
, horizontal_resize
},
209 { NULL
, max_stack
, NULL
},
213 #define SWM_H_SLICE (32)
214 #define SWM_V_SLICE (32)
217 /* define work spaces */
219 int idx
; /* workspace index */
220 int restack
; /* restack on switch */
221 struct layout
*cur_layout
; /* current layout handlers */
222 struct ws_win
*focus
; /* may be NULL */
223 struct swm_region
*r
; /* may be NULL */
224 struct ws_win_list winlist
; /* list of windows in ws */
228 int horizontal_msize
;
233 /* physical screen mapping */
234 #define SWM_WS_MAX (10) /* XXX Too small? */
236 int idx
; /* screen index */
237 struct swm_region_list rl
; /* list of regions on this screen */
240 struct workspace ws
[SWM_WS_MAX
];
243 unsigned long bar_border
;
244 unsigned long bar_color
;
245 unsigned long bar_font_color
;
246 unsigned long color_focus
; /* XXX should this be per ws? */
247 unsigned long color_unfocus
;
248 char bar_text
[SWM_BAR_MAX
];
250 struct swm_screen
*screens
;
252 Window rootclick
= 0;
254 struct ws_win
*cur_focus
= NULL
;
256 /* args to functions */
259 #define SWM_ARG_ID_FOCUSNEXT (0)
260 #define SWM_ARG_ID_FOCUSPREV (1)
261 #define SWM_ARG_ID_FOCUSMAIN (2)
262 #define SWM_ARG_ID_SWAPNEXT (3)
263 #define SWM_ARG_ID_SWAPPREV (4)
264 #define SWM_ARG_ID_SWAPMAIN (5)
265 #define SWM_ARG_ID_MASTERSHRINK (6)
266 #define SWM_ARG_ID_MASTERGROW (7)
271 name_to_color(char *colorname
)
275 XColor screen_def
, exact_def
;
276 unsigned long result
= 0;
277 char cname
[32] = "#";
279 cmap
= DefaultColormap(display
, screens
[0].idx
);
280 status
= XAllocNamedColor(display
, cmap
, colorname
,
281 &screen_def
, &exact_def
);
283 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
284 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
288 result
= screen_def
.pixel
;
290 fprintf(stderr
, "color '%s' not found.\n", colorname
);
295 /* conf file stuff */
296 #define SWM_CONF_WS "\n= \t"
297 #define SWM_CONF_FILE "scrotwm.conf"
299 conf_load(char *filename
)
302 char *line
, *cp
, *var
, *val
;
303 size_t len
, lineno
= 0;
306 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
308 if (filename
== NULL
)
311 if ((config
= fopen(filename
, "r")) == NULL
)
315 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
319 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
325 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
327 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
328 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
331 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
334 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
335 bar_enabled
= atoi(val
);
336 else if (!strncmp(var
, "bar_border",
337 strlen("bar_border")))
338 for (i
= 0; i
< ScreenCount(display
); i
++)
339 screens
[i
].bar_border
= name_to_color(val
);
340 else if (!strncmp(var
, "bar_color",
341 strlen("bar_color")))
342 for (i
= 0; i
< ScreenCount(display
); i
++)
343 screens
[i
].bar_color
= name_to_color(val
);
344 else if (!strncmp(var
, "bar_font_color",
345 strlen("bar_font_color")))
346 for (i
= 0; i
< ScreenCount(display
); i
++)
347 screens
[i
].bar_font_color
= name_to_color(val
);
348 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
349 asprintf(&bar_fonts
[0], "%s", val
);
355 if (!strncmp(var
, "color_focus", strlen("color_focus")))
356 for (i
= 0; i
< ScreenCount(display
); i
++)
357 screens
[i
].color_focus
= name_to_color(val
);
358 else if (!strncmp(var
, "color_unfocus",
359 strlen("color_unfocus")))
360 for (i
= 0; i
< ScreenCount(display
); i
++)
361 screens
[i
].color_unfocus
= name_to_color(val
);
367 if (!strncmp(var
, "dialog_ratio",
368 strlen("dialog_ratio"))) {
369 dialog_ratio
= atof(val
);
370 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
377 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
378 asprintf(&spawn_term
[0], "%s", val
); /* XXX args? */
389 errx(1, "invalid conf file entry: %s=%s", var
, val
);
393 bar_print(struct swm_region
*r
, char *s
, int erase
)
396 XSetForeground(display
, bar_gc
, r
->s
->bar_color
);
397 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
,
398 r
->s
->bar_text
, strlen(r
->s
->bar_text
));
401 strlcpy(r
->s
->bar_text
, s
, sizeof r
->s
->bar_text
);
402 XSetForeground(display
, bar_gc
, r
->s
->bar_font_color
);
403 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
,
404 r
->s
->bar_text
, strlen(r
->s
->bar_text
));
412 struct swm_region
*r
;
416 if (bar_enabled
== 0)
421 localtime_r(&tmt
, &tm
);
422 strftime(s
, sizeof s
, "%a %b %d %R %Z %Y", &tm
);
423 for (i
= 0; i
< ScreenCount(display
); i
++)
424 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
427 XSync(display
, False
);
438 bar_toggle(struct swm_region
*r
, union arg
*args
)
440 struct swm_region
*tmpr
;
443 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
446 for (i
= 0; i
< ScreenCount(display
); i
++)
447 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
448 XUnmapWindow(display
, tmpr
->bar_window
);
450 for (i
= 0; i
< ScreenCount(display
); i
++)
451 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
452 XMapRaised(display
, tmpr
->bar_window
);
454 bar_enabled
= !bar_enabled
;
455 XSync(display
, False
);
456 for (i
= 0; i
< ScreenCount(display
); i
++)
457 for (j
= 0; j
< SWM_WS_MAX
; j
++)
458 screens
[i
].ws
[j
].restack
= 1;
461 /* must be after stack */
462 for (i
= 0; i
< ScreenCount(display
); i
++)
463 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
468 bar_setup(struct swm_region
*r
)
472 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
473 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
477 if (bar_fonts
[i
] == NULL
)
478 errx(1, "couldn't load font");
479 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
481 r
->bar_window
= XCreateSimpleWindow(display
,
482 r
->s
->root
, X(r
), Y(r
), WIDTH(r
) - 2, bar_height
- 2,
483 1, r
->s
->bar_border
, r
->s
->bar_color
);
484 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
485 XSetFont(display
, bar_gc
, bar_fs
->fid
);
486 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
488 XMapRaised(display
, r
->bar_window
);
489 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
491 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
492 err(1, "could not install bar_signal");
497 config_win(struct ws_win
*win
)
501 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
502 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
503 ce
.type
= ConfigureNotify
;
504 ce
.display
= display
;
510 ce
.height
= win
->g
.h
;
511 ce
.border_width
= 1; /* XXX store this! */
513 ce
.override_redirect
= False
;
514 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
518 count_win(struct workspace
*ws
, int count_transient
)
523 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
524 if (count_transient
== 0 && win
->floating
)
526 if (count_transient
== 0 && win
->transient
)
530 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
536 quit(struct swm_region
*r
, union arg
*args
)
538 DNPRINTF(SWM_D_MISC
, "quit\n");
543 restart(struct swm_region
*r
, union arg
*args
)
545 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
547 /* disable alarm because the following code may not be interrupted */
549 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
550 errx(1, "can't disable alarm");
552 XCloseDisplay(display
);
553 execvp(start_argv
[0], start_argv
);
554 fprintf(stderr
, "execvp failed\n");
560 root_to_region(Window root
)
562 struct swm_region
*r
;
567 for (i
= 0; i
< ScreenCount(display
); i
++)
568 if (screens
[i
].root
== root
)
571 if (rootclick
!= root
&& /* if root was just clicked in, use cursor */
572 cur_focus
&& cur_focus
->ws
->r
&& cur_focus
->s
== &screens
[i
])
573 r
= cur_focus
->ws
->r
;
575 if (XQueryPointer(display
, screens
[i
].root
,
576 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) == False
) {
577 r
= TAILQ_FIRST(&screens
[i
].rl
);
579 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
580 if (x
> X(r
) && x
< X(r
) + WIDTH(r
) &&
581 y
> Y(r
) && y
< Y(r
) + HEIGHT(r
))
586 r
= TAILQ_FIRST(&screens
[i
].rl
);
593 find_window(Window id
)
598 for (i
= 0; i
< ScreenCount(display
); i
++)
599 for (j
= 0; j
< SWM_WS_MAX
; j
++)
600 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
607 spawn(struct swm_region
*r
, union arg
*args
)
609 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
611 * The double-fork construct avoids zombie processes and keeps the code
612 * clean from stupid signal handlers.
617 close(ConnectionNumber(display
));
619 execvp(args
->argv
[0], args
->argv
);
620 fprintf(stderr
, "execvp failed\n");
629 unfocus_win(struct ws_win
*win
)
631 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: id: %lu\n", win
->id
);
632 if (win
->ws
->r
&& win
->focus
)
633 XSetWindowBorder(display
, win
->id
,
634 win
->ws
->r
->s
->color_unfocus
);
636 if (win
->ws
->focus
== win
)
637 win
->ws
->focus
= NULL
;
638 if (cur_focus
== win
)
644 focus_win(struct ws_win
*win
)
646 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
->id
);
650 win
->ws
->focus
= win
;
651 if (win
->ws
->r
!= NULL
) {
652 if (cur_focus
&& cur_focus
!= win
)
653 unfocus_win(cur_focus
);
656 XSetWindowBorder(display
, win
->id
,
657 win
->ws
->r
->s
->color_focus
);
659 XSetInputFocus(display
, win
->id
,
660 RevertToPointerRoot
, CurrentTime
);
665 switchws(struct swm_region
*r
, union arg
*args
)
668 struct swm_region
*this_r
, *other_r
;
670 struct workspace
*new_ws
, *old_ws
;
674 new_ws
= &this_r
->s
->ws
[wsid
];
676 DNPRINTF(SWM_D_WS
, "switchws screen %d region %dx%d+%d+%d: "
677 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
680 if (new_ws
== old_ws
)
685 /* if the other workspace is hidden, switch windows */
686 /* map new window first to prevent ugly blinking */
687 TAILQ_FOREACH(win
, &new_ws
->winlist
, entry
)
688 XMapRaised(display
, win
->id
);
690 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
691 XUnmapWindow(display
, win
->id
);
696 other_r
->ws
= old_ws
;
705 focus_win(new_ws
->focus
);
710 swapwin(struct swm_region
*r
, union arg
*args
)
712 struct ws_win
*target
;
713 struct ws_win_list
*wl
;
716 DNPRINTF(SWM_D_WS
, "swapwin id %d "
717 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
718 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
719 if (cur_focus
== NULL
)
722 wl
= &cur_focus
->ws
->winlist
;
725 case SWM_ARG_ID_SWAPPREV
:
726 target
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
727 TAILQ_REMOVE(wl
, cur_focus
, entry
);
729 TAILQ_INSERT_TAIL(wl
, cur_focus
, entry
);
731 TAILQ_INSERT_BEFORE(target
, cur_focus
, entry
);
733 case SWM_ARG_ID_SWAPNEXT
:
734 target
= TAILQ_NEXT(cur_focus
, entry
);
735 TAILQ_REMOVE(wl
, cur_focus
, entry
);
737 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
739 TAILQ_INSERT_AFTER(wl
, target
, cur_focus
, entry
);
741 case SWM_ARG_ID_SWAPMAIN
:
742 target
= TAILQ_FIRST(wl
);
743 if (target
== cur_focus
)
745 TAILQ_REMOVE(wl
, target
, entry
);
746 TAILQ_INSERT_BEFORE(cur_focus
, target
, entry
);
747 TAILQ_REMOVE(wl
, cur_focus
, entry
);
748 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
751 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
760 focus(struct swm_region
*r
, union arg
*args
)
762 struct ws_win
*winfocus
, *winlostfocus
;
763 struct ws_win_list
*wl
;
765 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
766 if (cur_focus
== NULL
)
769 wl
= &cur_focus
->ws
->winlist
;
771 winlostfocus
= cur_focus
;
774 case SWM_ARG_ID_FOCUSPREV
:
775 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
776 if (winfocus
== NULL
)
777 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
780 case SWM_ARG_ID_FOCUSNEXT
:
781 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
782 if (winfocus
== NULL
)
783 winfocus
= TAILQ_FIRST(wl
);
786 case SWM_ARG_ID_FOCUSMAIN
:
787 winfocus
= TAILQ_FIRST(wl
);
794 if (winfocus
== winlostfocus
)
798 XSync(display
, False
);
802 cycle_layout(struct swm_region
*r
, union arg
*args
)
804 struct workspace
*ws
= r
->ws
;
806 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
809 if (ws
->cur_layout
->l_stack
== NULL
)
810 ws
->cur_layout
= &layouts
[0];
817 resize_master(struct swm_region
*r
, union arg
*args
)
819 struct workspace
*ws
= r
->ws
;
821 DNPRINTF(SWM_D_STACK
, "resize_master for workspace %d (id %d\n",
824 if (ws
->cur_layout
->l_resize
!= NULL
)
825 ws
->cur_layout
->l_resize(ws
, args
->id
);
829 stack_reset(struct swm_region
*r
, union arg
*args
)
831 struct workspace
*ws
= r
->ws
;
833 DNPRINTF(SWM_D_STACK
, "stack_reset: ws %d\n", ws
->idx
);
835 if (ws
->cur_layout
->l_init
!= NULL
) {
836 ws
->cur_layout
->l_init(ws
);
843 struct swm_geometry g
;
844 struct swm_region
*r
;
847 DNPRINTF(SWM_D_STACK
, "stack\n");
849 for (i
= 0; i
< ScreenCount(display
); i
++) {
851 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
852 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
853 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
855 /* start with screen geometry, adjust for bar */
865 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
868 XSync(display
, False
);
872 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
877 bzero(&wc
, sizeof wc
);
878 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
880 if (win
->transient
) {
881 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
882 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
885 wc
.height
= win
->g
.h
;
886 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
887 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
889 DNPRINTF(SWM_D_STACK
, "stack_floater: win %lu x %d y %d w %d h %d\n",
890 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
892 XConfigureWindow(display
, win
->id
, mask
, &wc
);
895 int vertical_msize
[SWM_WS_MAX
];
898 vertical_init(struct workspace
*ws
)
900 DNPRINTF(SWM_D_MISC
, "vertical_init: workspace: %d\n", ws
->idx
);
902 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
906 vertical_resize(struct workspace
*ws
, int id
)
908 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
911 case SWM_ARG_ID_MASTERSHRINK
:
912 ws
->l_state
.vertical_msize
--;
913 if (ws
->l_state
.vertical_msize
< 1)
914 ws
->l_state
.vertical_msize
= 1;
916 case SWM_ARG_ID_MASTERGROW
:
917 ws
->l_state
.vertical_msize
++;
918 if (ws
->l_state
.vertical_msize
> SWM_V_SLICE
- 1)
919 ws
->l_state
.vertical_msize
= SWM_V_SLICE
- 1;
928 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
930 struct swm_geometry gg
= *g
;
931 struct ws_win
*win
, *winfocus
;
932 int i
, hrh
, winno
, main_width
;
935 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
937 if ((winno
= count_win(ws
, 0)) == 0)
940 if (ws
->focus
== NULL
)
941 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
942 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
945 main_width
= (g
->w
/ SWM_V_SLICE
) *
946 ws
->l_state
.vertical_msize
;
951 hrh
= g
->h
/ (winno
- 1);
956 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
958 gg
.x
+= main_width
+ 2;
959 gg
.w
= g
->w
- (main_width
+ 2);
961 if (i
!= 0 && hrh
!= 0) {
962 /* correct the last window for lost pixels */
963 if (win
== TAILQ_LAST(&ws
->winlist
, ws_win_list
)) {
964 gg
.h
= hrh
+ (g
->h
- (i
* hrh
));
968 /* leave first right hand window at y = 0 */
974 if (win
->transient
!= 0 || win
->floating
!= 0)
975 stack_floater(win
, ws
->r
);
977 bzero(&wc
, sizeof wc
);
979 win
->g
.x
= wc
.x
= gg
.x
;
980 win
->g
.y
= wc
.y
= gg
.y
;
981 win
->g
.w
= wc
.width
= gg
.w
;
982 win
->g
.h
= wc
.height
= gg
.h
;
983 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
984 XConfigureWindow(display
, win
->id
, mask
, &wc
);
987 XMapRaised(display
, win
->id
);
992 focus_win(winfocus
); /* has to be done outside of the loop */
997 horizontal_init(struct workspace
*ws
)
999 DNPRINTF(SWM_D_STACK
, "horizontal_init: workspace: %d\n", ws
->idx
);
1001 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
1005 horizontal_resize(struct workspace
*ws
, int id
)
1007 DNPRINTF(SWM_D_STACK
, "horizontal_resize: workspace: %d\n", ws
->idx
);
1010 case SWM_ARG_ID_MASTERSHRINK
:
1011 ws
->l_state
.horizontal_msize
--;
1012 if (ws
->l_state
.horizontal_msize
< 1)
1013 ws
->l_state
.horizontal_msize
= 1;
1015 case SWM_ARG_ID_MASTERGROW
:
1016 ws
->l_state
.horizontal_msize
++;
1017 if (ws
->l_state
.horizontal_msize
> SWM_H_SLICE
- 1)
1018 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
- 1;
1027 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1029 struct swm_geometry gg
= *g
;
1030 struct ws_win
*win
, *winfocus
;
1031 int i
, hrw
, winno
, main_height
;
1034 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
1036 if ((winno
= count_win(ws
, 0)) == 0)
1039 if (ws
->focus
== NULL
)
1040 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1041 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1044 main_height
= (g
->h
/ SWM_H_SLICE
) *
1045 ws
->l_state
.horizontal_msize
;
1050 hrw
= g
->w
/ (winno
- 1);
1055 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1057 gg
.y
+= main_height
+ 2;
1058 gg
.h
= g
->h
- (main_height
+ 2);
1060 if (i
!= 0 && hrw
!= 0) {
1061 /* correct the last window for lost pixels */
1062 if (win
== TAILQ_LAST(&ws
->winlist
, ws_win_list
)) {
1063 gg
.w
= hrw
+ (g
->w
- (i
* hrw
));
1067 /* leave first bottom window at x = 0 */
1073 if (win
->transient
!= 0 || win
->floating
!= 0)
1074 stack_floater(win
, ws
->r
);
1076 bzero(&wc
, sizeof wc
);
1077 wc
.border_width
= 1;
1078 win
->g
.x
= wc
.x
= gg
.x
;
1079 win
->g
.y
= wc
.y
= gg
.y
;
1080 win
->g
.w
= wc
.width
= gg
.w
;
1081 win
->g
.h
= wc
.height
= gg
.h
;
1082 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1083 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1086 XMapRaised(display
, win
->id
);
1091 focus_win(winfocus
); /* this has to be done outside of the loop */
1094 /* fullscreen view */
1096 max_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1098 struct swm_geometry gg
= *g
;
1099 struct ws_win
*win
, *winfocus
;
1102 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
1104 if (count_win(ws
, 0) == 0)
1107 if (ws
->focus
== NULL
)
1108 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1109 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1111 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1112 if (win
->transient
!= 0 || win
->floating
!= 0) {
1113 if (win
== ws
->focus
) {
1115 stack_floater(win
, ws
->r
);
1116 XMapRaised(display
, win
->id
);
1118 XUnmapWindow(display
, win
->id
);
1120 bzero(&wc
, sizeof wc
);
1121 wc
.border_width
= 1;
1122 win
->g
.x
= wc
.x
= gg
.x
;
1123 win
->g
.y
= wc
.y
= gg
.y
;
1124 win
->g
.w
= wc
.width
= gg
.w
;
1125 win
->g
.h
= wc
.height
= gg
.h
;
1126 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1127 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1129 if (win
== ws
->focus
) {
1130 XMapRaised(display
, win
->id
);
1132 XUnmapWindow(display
, win
->id
);
1137 focus_win(winfocus
); /* has to be done outside of the loop */
1141 send_to_ws(struct swm_region
*r
, union arg
*args
)
1143 int wsid
= args
->id
;
1144 struct ws_win
*win
= cur_focus
;
1145 struct workspace
*ws
, *nws
;
1147 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
1150 nws
= &win
->s
->ws
[wsid
];
1152 XUnmapWindow(display
, win
->id
);
1154 /* find a window to focus */
1155 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1156 if (ws
->focus
== NULL
)
1157 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1158 if (ws
->focus
== win
)
1161 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1163 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
1166 if (count_win(nws
, 1) == 1)
1174 /* key definitions */
1178 void (*func
)(struct swm_region
*r
, union arg
*);
1181 /* modifier key function argument */
1182 { MODKEY
, XK_space
, cycle_layout
, {0} },
1183 { MODKEY
| ShiftMask
, XK_space
, stack_reset
, {0} },
1184 { MODKEY
, XK_h
, resize_master
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
1185 { MODKEY
, XK_l
, resize_master
, {.id
= SWM_ARG_ID_MASTERGROW
} },
1186 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
1187 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1188 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1189 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
1190 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
1191 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
1192 { MODKEY
, XK_p
, spawn
, {.argv
= spawn_menu
} },
1193 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
1194 { MODKEY
, XK_q
, restart
, {0} },
1195 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
1196 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
1197 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
1198 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
1199 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
1200 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
1201 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
1202 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
1203 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
1204 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
1205 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
1206 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
1207 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
1208 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
1209 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
1210 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
1211 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
1212 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
1213 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
1214 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
1215 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
1216 { MODKEY
, XK_b
, bar_toggle
, {0} },
1217 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1218 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1222 updatenumlockmask(void)
1225 XModifierKeymap
*modmap
;
1227 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
1229 modmap
= XGetModifierMapping(display
);
1230 for (i
= 0; i
< 8; i
++)
1231 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1232 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1233 == XKeysymToKeycode(display
, XK_Num_Lock
))
1234 numlockmask
= (1 << i
);
1236 XFreeModifiermap(modmap
);
1242 unsigned int i
, j
, k
;
1244 unsigned int modifiers
[] =
1245 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
1247 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
1248 updatenumlockmask();
1250 for (k
= 0; k
< ScreenCount(display
); k
++) {
1251 if (TAILQ_EMPTY(&screens
[k
].rl
))
1253 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
1254 for (i
= 0; i
< LENGTH(keys
); i
++) {
1255 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
1256 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1257 XGrabKey(display
, code
,
1258 keys
[i
].mod
| modifiers
[j
],
1259 screens
[k
].root
, True
,
1260 GrabModeAsync
, GrabModeAsync
);
1267 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
1275 XKeyEvent
*ev
= &e
->xkey
;
1277 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
1279 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
1280 for (i
= 0; i
< LENGTH(keys
); i
++)
1281 if (keysym
== keys
[i
].keysym
1282 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1284 keys
[i
].func(root_to_region(ev
->root
),
1289 buttonpress(XEvent
*e
)
1291 XButtonPressedEvent
*ev
= &e
->xbutton
;
1292 #ifdef SWM_CLICKTOFOCUS
1294 struct workspace
*ws
;
1295 struct swm_region
*r
;
1298 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
1300 if (ev
->window
== ev
->root
) {
1301 rootclick
= ev
->root
;
1304 if (ev
->window
== cur_focus
->id
)
1306 #ifdef SWM_CLICKTOFOCUS
1307 r
= root_to_region(ev
->root
);
1309 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
1310 if (win
->id
== ev
->window
) {
1311 /* focus in the clicked window */
1312 XSetWindowBorder(display
, ev
->window
, 0xff0000);
1313 XSetWindowBorder(display
, ws
->focus
->id
, 0x888888);
1314 XSetInputFocus(display
, ev
->window
, RevertToPointerRoot
,
1317 XSync(display
, False
);
1324 set_win_state(struct ws_win
*win
, long state
)
1326 long data
[] = {state
, None
};
1328 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
1330 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1331 (unsigned char *)data
, 2);
1335 manage_window(Window id
, struct workspace
*ws
)
1341 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1343 return (win
); /* already being managed */
1346 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
1347 errx(1, "calloc: failed to allocate memory for new window");
1351 win
->s
= ws
->r
->s
; /* this never changes */
1352 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
1354 /* make new win focused */
1357 XGetTransientForHint(display
, win
->id
, &trans
);
1359 win
->transient
= trans
;
1360 DNPRINTF(SWM_D_MISC
, "manage_window: win %u transient %u\n",
1361 (unsigned)win
->id
, win
->transient
);
1363 XGetWindowAttributes(display
, id
, &win
->wa
);
1364 win
->g
.w
= win
->wa
.width
;
1365 win
->g
.h
= win
->wa
.height
;
1366 win
->g
.x
= win
->wa
.x
;
1367 win
->g
.y
= win
->wa
.y
;
1369 /* XXX make this a table */
1370 bzero(&ch
, sizeof ch
);
1371 if (XGetClassHint(display
, win
->id
, &ch
)) {
1372 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1373 if (!strcmp(ch
.res_class
, "MPlayer") && !strcmp(ch
.res_name
, "xv")) {
1377 XFree(ch
.res_class
);
1382 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
1383 PropertyChangeMask
| StructureNotifyMask
);
1385 set_win_state(win
, NormalState
);
1391 configurerequest(XEvent
*e
)
1393 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
1398 if ((win
= find_window(ev
->window
)) == NULL
)
1402 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
1404 bzero(&wc
, sizeof wc
);
1407 wc
.width
= ev
->width
;
1408 wc
.height
= ev
->height
;
1409 wc
.border_width
= ev
->border_width
;
1410 wc
.sibling
= ev
->above
;
1411 wc
.stack_mode
= ev
->detail
;
1412 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
1414 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
1416 if (win
->floating
) {
1417 if (ev
->value_mask
& CWX
)
1419 if (ev
->value_mask
& CWY
)
1421 if (ev
->value_mask
& CWWidth
)
1422 win
->g
.w
= ev
->width
;
1423 if (ev
->value_mask
& CWHeight
)
1424 win
->g
.h
= ev
->height
;
1425 if (win
->ws
->r
!= NULL
) {
1426 /* this seems to be full screen */
1427 if (win
->g
.w
> WIDTH(win
->ws
->r
)) {
1432 if (win
->g
.h
> HEIGHT(win
->ws
->r
)) {
1438 if ((ev
->value_mask
& (CWX
|CWY
)) &&
1439 !(ev
->value_mask
& (CWWidth
|CWHeight
)))
1441 XMoveResizeWindow(display
, win
->id
,
1442 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
1449 configurenotify(XEvent
*e
)
1451 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
1452 e
->xconfigure
.window
);
1456 destroynotify(XEvent
*e
)
1459 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
1461 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
1463 if ((win
= find_window(ev
->window
)) != NULL
) {
1464 struct workspace
*ws
= win
->ws
;
1465 /* find a window to focus */
1466 if (ws
->focus
== win
)
1467 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1468 if (ws
->focus
== NULL
)
1469 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1470 if (ws
->focus
== win
)
1472 if (cur_focus
== win
) {
1473 if (ws
->focus
== NULL
)
1474 unfocus_win(win
); /* XXX focus another ws? */
1476 focus_win(ws
->focus
);
1479 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1480 set_win_state(win
, WithdrawnState
);
1487 enternotify(XEvent
*e
)
1489 XCrossingEvent
*ev
= &e
->xcrossing
;
1493 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
1495 if ((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) &&
1496 ev
->window
!= ev
->root
)
1499 /* eat event(r) to prevent autofocus */
1503 /* brute force for now */
1504 for (i
= 0; i
< ScreenCount(display
); i
++) {
1505 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
1506 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1507 if (win
->id
== ev
->window
)
1517 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", e
->xfocus
.window
);
1519 /* XXX this probably needs better handling now.
1520 if (ev->window == ev->root)
1524 * kill grab for now so that we can cut and paste , this screws up
1528 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1529 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1530 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1535 mappingnotify(XEvent
*e
)
1537 XMappingEvent
*ev
= &e
->xmapping
;
1539 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
1541 XRefreshKeyboardMapping(ev
);
1542 if (ev
->request
== MappingKeyboard
)
1547 maprequest(XEvent
*e
)
1549 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1550 XWindowAttributes wa
;
1551 struct swm_region
*r
;
1553 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
1554 e
->xmaprequest
.window
);
1556 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
1558 if (wa
.override_redirect
)
1560 r
= root_to_region(wa
.root
);
1561 manage_window(e
->xmaprequest
.window
, r
->ws
);
1566 propertynotify(XEvent
*e
)
1568 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
1569 e
->xproperty
.window
);
1573 unmapnotify(XEvent
*e
)
1575 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
1579 visibilitynotify(XEvent
*e
)
1582 struct swm_region
*r
;
1584 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
1585 e
->xvisibility
.window
);
1586 if (e
->xvisibility
.state
== VisibilityUnobscured
)
1587 for (i
= 0; i
< ScreenCount(display
); i
++)
1588 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1589 if (e
->xvisibility
.window
== r
->bar_window
)
1593 void (*handler
[LASTEvent
])(XEvent
*) = {
1595 [KeyPress
] = keypress
,
1596 [ButtonPress
] = buttonpress
,
1597 [ConfigureRequest
] = configurerequest
,
1598 [ConfigureNotify
] = configurenotify
,
1599 [DestroyNotify
] = destroynotify
,
1600 [EnterNotify
] = enternotify
,
1601 [FocusIn
] = focusin
,
1602 [MappingNotify
] = mappingnotify
,
1603 [MapRequest
] = maprequest
,
1604 [PropertyNotify
] = propertynotify
,
1605 [UnmapNotify
] = unmapnotify
,
1606 [VisibilityNotify
] = visibilitynotify
,
1610 xerror_start(Display
*d
, XErrorEvent
*ee
)
1617 xerror(Display
*d
, XErrorEvent
*ee
)
1619 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1627 xerrorxlib
= XSetErrorHandler(xerror_start
);
1629 /* this causes an error if some other window manager is running */
1630 XSelectInput(display
, DefaultRootWindow(display
),
1631 SubstructureRedirectMask
);
1632 XSync(display
, False
);
1636 XSetErrorHandler(xerror
);
1637 XSync(display
, False
);
1646 unsigned char *p
= NULL
;
1647 unsigned long n
, extra
;
1650 astate
= XInternAtom(display
, "WM_STATE", False
);
1651 status
= XGetWindowProperty(display
, w
, astate
, 0L, 2L, False
, astate
,
1652 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
1653 if (status
!= Success
)
1662 new_region(struct swm_screen
*s
, struct workspace
*ws
,
1663 int x
, int y
, int w
, int h
)
1665 struct swm_region
*r
;
1667 DNPRINTF(SWM_D_MISC
, "new region on screen %d: %dx%d (%d, %d)\n",
1668 s
->idx
, x
, y
, w
, h
);
1670 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
1671 errx(1, "calloc: failed to allocate memory for screen");
1680 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
1687 #ifdef SWM_XRR_HAS_CRTC
1689 XRRScreenResources
*sr
;
1691 #endif /* SWM_XRR_HAS_CRTC */
1692 Window d1
, d2
, *wins
= NULL
;
1693 XWindowAttributes wa
;
1694 struct swm_region
*r
;
1696 int errorbase
, major
, minor
;
1697 int ncrtc
= 0, w
= 0;
1699 struct workspace
*ws
;
1701 if ((screens
= calloc(ScreenCount(display
),
1702 sizeof(struct swm_screen
))) == NULL
)
1703 errx(1, "calloc: screens");
1705 /* map physical screens */
1706 for (i
= 0; i
< ScreenCount(display
); i
++) {
1707 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
1709 TAILQ_INIT(&screens
[i
].rl
);
1710 screens
[i
].root
= RootWindow(display
, i
);
1711 XGetWindowAttributes(display
, screens
[i
].root
, &wa
);
1712 XSelectInput(display
, screens
[i
].root
,
1713 ButtonPressMask
| wa
.your_event_mask
);
1715 /* set default colors */
1716 screens
[i
].color_focus
= name_to_color("red");
1717 screens
[i
].color_unfocus
= name_to_color("rgb:88/88/88");
1718 screens
[i
].bar_border
= name_to_color("rgb:00/80/80");
1719 screens
[i
].bar_color
= name_to_color("black");
1720 screens
[i
].bar_font_color
= name_to_color("rgb:a0/a0/a0");
1722 /* init all workspaces */
1723 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
1724 ws
= &screens
[i
].ws
[j
];
1729 TAILQ_INIT(&ws
->winlist
);
1731 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
1732 if (layouts
[k
].l_init
!= NULL
)
1733 layouts
[k
].l_init(ws
);
1734 ws
->cur_layout
= &layouts
[0];
1737 /* map virtual screens onto physical screens */
1738 screens
[i
].xrandr_support
= XRRQueryExtension(display
,
1739 &xrandr_eventbase
, &errorbase
);
1740 if (screens
[i
].xrandr_support
)
1741 if (XRRQueryVersion(display
, &major
, &minor
) &&
1743 screens
[i
].xrandr_support
= 0;
1745 #if 0 /* not ready for dynamic screen changes */
1746 if (screens
[i
].xrandr_support
)
1747 XRRSelectInput(display
,
1749 RRScreenChangeNotifyMask
);
1752 /* grab existing windows (before we build the bars)*/
1753 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
1756 #ifdef SWM_XRR_HAS_CRTC
1757 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
1759 new_region(&screens
[i
], &screens
[i
].ws
[w
],
1760 0, 0, DisplayWidth(display
, i
),
1761 DisplayHeight(display
, i
));
1765 for (c
= 0; c
< ncrtc
; c
++) {
1766 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
1767 if (ci
->noutput
== 0)
1770 if (ci
!= NULL
&& ci
->mode
== None
)
1771 new_region(&screens
[i
], &screens
[i
].ws
[w
], 0, 0,
1772 DisplayWidth(display
, i
),
1773 DisplayHeight(display
, i
));
1775 new_region(&screens
[i
], &screens
[i
].ws
[w
],
1776 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
1779 XRRFreeCrtcInfo(ci
);
1780 XRRFreeScreenResources(sr
);
1782 new_region(&screens
[i
], &screens
[i
].ws
[w
], 0, 0,
1783 DisplayWidth(display
, i
),
1784 DisplayHeight(display
, i
));
1785 #endif /* SWM_XRR_HAS_CRTC */
1787 /* attach windows to a region */
1788 /* normal windows */
1789 if ((r
= TAILQ_FIRST(&screens
[i
].rl
)) == NULL
)
1790 errx(1, "no regions on screen %d", i
);
1792 for (i
= 0; i
< no
; i
++) {
1793 XGetWindowAttributes(display
, wins
[i
], &wa
);
1794 if (!XGetWindowAttributes(display
, wins
[i
], &wa
) ||
1795 wa
.override_redirect
||
1796 XGetTransientForHint(display
, wins
[i
], &d1
))
1799 if (wa
.map_state
== IsViewable
||
1800 getstate(wins
[i
]) == NormalState
)
1801 manage_window(wins
[i
], r
->ws
);
1803 /* transient windows */
1804 for (i
= 0; i
< no
; i
++) {
1805 if (!XGetWindowAttributes(display
, wins
[i
], &wa
))
1808 if (XGetTransientForHint(display
, wins
[i
], &d1
) &&
1809 (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) ==
1811 manage_window(wins
[i
], r
->ws
);
1821 main(int argc
, char *argv
[])
1824 char conf
[PATH_MAX
], *cfile
= NULL
;
1831 fprintf(stderr
, "Welcome to scrotwm V%s\n", SWM_VERSION
);
1832 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1833 warnx("no locale support");
1835 if (!(display
= XOpenDisplay(0)))
1836 errx(1, "can not open display");
1839 errx(1, "other wm running");
1841 astate
= XInternAtom(display
, "WM_STATE", False
);
1843 /* look for local and global conf file */
1844 pwd
= getpwuid(getuid());
1846 errx(1, "invalid user %d", getuid());
1848 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
1849 if (stat(conf
, &sb
) != -1) {
1850 if (S_ISREG(sb
.st_mode
))
1853 /* try global conf file */
1854 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
1855 if (!stat(conf
, &sb
))
1856 if (S_ISREG(sb
.st_mode
))
1864 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
1869 xfd
= ConnectionNumber(display
);
1872 if (select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
1874 errx(1, "select failed");
1879 while(XPending(display
)) {
1880 XNextEvent(display
, &e
);
1881 if (handler
[e
.type
])
1882 handler
[e
.type
](&e
);
1886 XCloseDisplay(display
);