]>
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"
66 #include <sys/types.h>
70 #include <sys/queue.h>
71 #include <sys/param.h>
72 #include <sys/select.h>
74 #include <X11/cursorfont.h>
75 #include <X11/keysym.h>
76 #include <X11/Xatom.h>
78 #include <X11/Xproto.h>
79 #include <X11/Xutil.h>
80 #include <X11/extensions/Xrandr.h>
83 # error XRandR versions less than 1.0 are not supported
88 #define SWM_XRR_HAS_CRTC
92 /* #define SWM_DEBUG */
94 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
95 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
96 #define SWM_D_MISC 0x0001
97 #define SWM_D_EVENT 0x0002
98 #define SWM_D_WS 0x0004
99 #define SWM_D_FOCUS 0x0008
100 #define SWM_D_MOVE 0x0010
101 #define SWM_D_STACK 0x0020
102 #define SWM_D_MOUSE 0x0040
104 u_int32_t swm_debug
= 0
113 #define DPRINTF(x...)
114 #define DNPRINTF(n,x...)
117 #define LENGTH(x) (sizeof x / sizeof x[0])
118 #define MODKEY Mod1Mask
119 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
120 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
122 #define X(r) (r)->g.x
123 #define Y(r) (r)->g.y
124 #define WIDTH(r) (r)->g.w
125 #define HEIGHT(r) (r)->g.h
129 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
132 int xrandr_eventbase
;
133 int ignore_enter
= 0;
134 unsigned int numlockmask
= 0;
138 int cycle_visible
= 0;
141 double dialog_ratio
= .6;
143 #define SWM_BAR_MAX (128)
144 sig_atomic_t bar_alarm
= 0;
151 char *bar_fonts
[] = {
152 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
153 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
157 /* terminal + args */
158 char *spawn_term
[] = { "xterm", NULL
};
159 char *spawn_menu
[] = { "dmenu_run", NULL
};
161 /* layout manager data */
162 struct swm_geometry
{
172 /* virtual "screens" */
174 TAILQ_ENTRY(swm_region
) entry
;
175 struct swm_geometry g
;
177 struct workspace
*ws
; /* current workspace on this region */
178 struct swm_screen
*s
; /* screen idx */
180 TAILQ_HEAD(swm_region_list
, swm_region
);
184 TAILQ_ENTRY(ws_win
) entry
;
186 struct swm_geometry g
;
190 struct workspace
*ws
; /* always valid */
191 struct swm_screen
*s
; /* always valid, never changes */
192 XWindowAttributes wa
;
194 TAILQ_HEAD(ws_win_list
, ws_win
);
196 /* layout handlers */
198 void vertical_config(struct workspace
*, int);
199 void vertical_stack(struct workspace
*, struct swm_geometry
*);
200 void horizontal_config(struct workspace
*, int);
201 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
202 void max_stack(struct workspace
*, struct swm_geometry
*);
204 void grabbuttons(struct ws_win
*, int);
207 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
208 void (*l_config
)(struct workspace
*, int);
210 /* stack, configure */
211 { vertical_stack
, vertical_config
},
212 { horizontal_stack
, horizontal_config
},
217 #define SWM_H_SLICE (32)
218 #define SWM_V_SLICE (32)
220 /* define work spaces */
222 int idx
; /* workspace index */
223 int restack
; /* restack on switch */
224 struct layout
*cur_layout
; /* current layout handlers */
225 struct ws_win
*focus
; /* may be NULL */
226 struct swm_region
*r
; /* may be NULL */
227 struct ws_win_list winlist
; /* list of windows in ws */
231 int horizontal_msize
;
238 /* physical screen mapping */
239 #define SWM_WS_MAX (10) /* XXX Too small? */
241 int idx
; /* screen index */
242 struct swm_region_list rl
; /* list of regions on this screen */
245 struct workspace ws
[SWM_WS_MAX
];
248 unsigned long bar_border
;
249 unsigned long bar_color
;
250 unsigned long bar_font_color
;
251 unsigned long color_focus
; /* XXX should this be per ws? */
252 unsigned long color_unfocus
;
254 struct swm_screen
*screens
;
257 struct ws_win
*cur_focus
= NULL
;
259 /* args to functions */
262 #define SWM_ARG_ID_FOCUSNEXT (0)
263 #define SWM_ARG_ID_FOCUSPREV (1)
264 #define SWM_ARG_ID_FOCUSMAIN (2)
265 #define SWM_ARG_ID_SWAPNEXT (3)
266 #define SWM_ARG_ID_SWAPPREV (4)
267 #define SWM_ARG_ID_SWAPMAIN (5)
268 #define SWM_ARG_ID_MASTERSHRINK (6)
269 #define SWM_ARG_ID_MASTERGROW (7)
270 #define SWM_ARG_ID_MASTERADD (8)
271 #define SWM_ARG_ID_MASTERDEL (9)
272 #define SWM_ARG_ID_STACKRESET (10)
273 #define SWM_ARG_ID_STACKINIT (11)
274 #define SWM_ARG_ID_CYCLEWS_UP (12)
275 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
280 name_to_color(char *colorname
)
284 XColor screen_def
, exact_def
;
285 unsigned long result
= 0;
286 char cname
[32] = "#";
288 cmap
= DefaultColormap(display
, screens
[0].idx
);
289 status
= XAllocNamedColor(display
, cmap
, colorname
,
290 &screen_def
, &exact_def
);
292 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
293 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
297 result
= screen_def
.pixel
;
299 fprintf(stderr
, "color '%s' not found.\n", colorname
);
305 varmatch(char *var
, char *name
, int *index
)
310 i
= strncmp(var
, name
, 255);
317 p
= var
+ strlen(name
);
320 bzero(buf
, sizeof buf
);
322 while (isdigit(*p
) && i
< sizeof buf
)
324 if (i
== 0 || i
>= sizeof buf
|| *p
!= ']')
326 *index
= strtonum(buf
, 0, 99, NULL
);
330 /* conf file stuff */
331 #define SWM_CONF_WS "\n= \t"
332 #define SWM_CONF_FILE "scrotwm.conf"
334 conf_load(char *filename
)
337 char *line
, *cp
, *var
, *val
;
338 size_t len
, lineno
= 0;
341 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
343 if (filename
== NULL
)
346 if ((config
= fopen(filename
, "r")) == NULL
)
349 for (sc
= ScreenCount(display
);;) {
350 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
354 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
360 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
362 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
363 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
366 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
369 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
370 bar_enabled
= atoi(val
);
371 else if (!varmatch(var
, "bar_border", &i
))
372 if (i
> 0 && i
<= sc
)
373 screens
[i
- 1].bar_border
=
376 for (i
= 0; i
< sc
; i
++)
377 screens
[i
].bar_border
=
381 else if (!varmatch(var
, "bar_color", &i
))
382 if (i
> 0 && i
<= sc
)
383 screens
[i
- 1].bar_color
=
386 for (i
= 0; i
< sc
; i
++)
387 screens
[i
].bar_color
=
391 else if (!varmatch(var
, "bar_font_color", &i
))
392 if (i
> 0 && i
<= sc
)
393 screens
[i
- 1].bar_font_color
=
396 for (i
= 0; i
< sc
; i
++)
397 screens
[i
].bar_font_color
=
402 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
403 asprintf(&bar_fonts
[0], "%s", val
);
409 if (!varmatch(var
, "color_focus", &i
))
410 if (i
> 0 && i
<= sc
)
411 screens
[i
- 1].color_focus
=
414 for (i
= 0; i
< sc
; i
++)
415 screens
[i
].color_focus
=
419 else if (!varmatch(var
, "color_unfocus", &i
))
420 if (i
> 0 && i
<= sc
)
421 screens
[i
- 1].color_unfocus
=
424 for (i
= 0; i
< sc
; i
++)
425 screens
[i
].color_unfocus
=
429 else if (!strncmp(var
, "cycle_empty", strlen("cycle_empty")))
430 cycle_visible
= atoi(val
);
431 else if (!strncmp(var
, "cycle_visible", strlen("cycle_visible")))
432 cycle_visible
= atoi(val
);
438 if (!strncmp(var
, "dialog_ratio",
439 strlen("dialog_ratio"))) {
440 dialog_ratio
= atof(val
);
441 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
448 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
449 asprintf(&spawn_term
[0], "%s", val
); /* XXX args? */
460 errx(1, "invalid conf file entry: %s=%s", var
, val
);
462 errx(1, "invalid screen index: %s out of bounds", var
);
466 bar_print(struct swm_region
*r
, char *s
)
468 XClearWindow(display
, r
->bar_window
);
469 XSetForeground(display
, bar_gc
, r
->s
->bar_font_color
);
470 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
, s
,
479 struct swm_region
*r
;
484 if (bar_enabled
== 0)
488 localtime_r(&tmt
, &tm
);
489 strftime(s
, sizeof s
, "%a %b %d %R %Z %Y", &tm
);
490 for (i
= 0; i
< ScreenCount(display
); i
++) {
492 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
493 snprintf(e
, sizeof e
, "%s %d:%d",
494 s
, x
++, r
->ws
->idx
+ 1);
498 XSync(display
, False
);
509 bar_toggle(struct swm_region
*r
, union arg
*args
)
511 struct swm_region
*tmpr
;
512 int i
, j
, sc
= ScreenCount(display
);
514 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
517 for (i
= 0; i
< sc
; i
++)
518 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
519 XUnmapWindow(display
, tmpr
->bar_window
);
521 for (i
= 0; i
< sc
; i
++)
522 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
523 XMapRaised(display
, tmpr
->bar_window
);
525 bar_enabled
= !bar_enabled
;
526 XSync(display
, False
);
527 for (i
= 0; i
< sc
; i
++)
528 for (j
= 0; j
< SWM_WS_MAX
; j
++)
529 screens
[i
].ws
[j
].restack
= 1;
532 /* must be after stack */
533 for (i
= 0; i
< sc
; i
++)
534 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
539 bar_setup(struct swm_region
*r
)
543 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
544 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
548 if (bar_fonts
[i
] == NULL
)
549 errx(1, "couldn't load font");
550 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
552 r
->bar_window
= XCreateSimpleWindow(display
,
553 r
->s
->root
, X(r
), Y(r
), WIDTH(r
) - 2, bar_height
- 2,
554 1, r
->s
->bar_border
, r
->s
->bar_color
);
555 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
556 XSetFont(display
, bar_gc
, bar_fs
->fid
);
557 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
559 XMapRaised(display
, r
->bar_window
);
560 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
562 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
563 err(1, "could not install bar_signal");
570 XSetWindowAttributes wa
;
571 struct swm_region
*r
;
574 bzero(&wa
, sizeof wa
);
575 for (i
= 0; i
< ScreenCount(display
); i
++)
576 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
577 wa
.border_pixel
= screens
[i
].bar_border
;
578 wa
.background_pixel
= screens
[i
].bar_color
;
579 XChangeWindowAttributes(display
, r
->bar_window
,
580 CWBackPixel
| CWBorderPixel
, &wa
);
586 config_win(struct ws_win
*win
)
590 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
591 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
592 ce
.type
= ConfigureNotify
;
593 ce
.display
= display
;
599 ce
.height
= win
->g
.h
;
600 ce
.border_width
= 1; /* XXX store this! */
602 ce
.override_redirect
= False
;
603 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
607 count_win(struct workspace
*ws
, int count_transient
)
612 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
613 if (count_transient
== 0 && win
->floating
)
615 if (count_transient
== 0 && win
->transient
)
619 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
625 quit(struct swm_region
*r
, union arg
*args
)
627 DNPRINTF(SWM_D_MISC
, "quit\n");
632 restart(struct swm_region
*r
, union arg
*args
)
634 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
636 /* disable alarm because the following code may not be interrupted */
638 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
639 errx(1, "can't disable alarm");
641 XCloseDisplay(display
);
642 execvp(start_argv
[0], start_argv
);
643 fprintf(stderr
, "execvp failed\n");
649 root_to_region(Window root
)
651 struct swm_region
*r
;
656 for (i
= 0; i
< ScreenCount(display
); i
++)
657 if (screens
[i
].root
== root
)
660 if (XQueryPointer(display
, screens
[i
].root
,
661 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) == False
) {
662 /* if we can't query the pointer, grab the first region */
663 r
= TAILQ_FIRST(&screens
[i
].rl
);
665 /* otherwise, choose a region based on pointer location */
666 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
667 if (x
> X(r
) && x
< X(r
) + WIDTH(r
) &&
668 y
> Y(r
) && y
< Y(r
) + HEIGHT(r
))
673 r
= TAILQ_FIRST(&screens
[i
].rl
);
679 find_window(Window id
)
684 for (i
= 0; i
< ScreenCount(display
); i
++)
685 for (j
= 0; j
< SWM_WS_MAX
; j
++)
686 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
693 spawn(struct swm_region
*r
, union arg
*args
)
695 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
697 * The double-fork construct avoids zombie processes and keeps the code
698 * clean from stupid signal handlers.
703 close(ConnectionNumber(display
));
705 execvp(args
->argv
[0], args
->argv
);
706 fprintf(stderr
, "execvp failed\n");
720 DNPRINTF(SWM_D_FOCUS
, "unfocus_all:\n");
722 for (i
= 0; i
< ScreenCount(display
); i
++)
723 for (j
= 0; j
< SWM_WS_MAX
; j
++)
724 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
725 if (win
->ws
->r
== NULL
)
728 XSetWindowBorder(display
, win
->id
,
729 win
->ws
->r
->s
->color_unfocus
);
731 win
->ws
->focus
= NULL
;
737 focus_win(struct ws_win
*win
)
739 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
? win
->id
: 0);
745 win
->ws
->focus
= win
;
746 if (win
->ws
->r
!= NULL
) {
748 if (!win
->got_focus
) {
749 XSetWindowBorder(display
, win
->id
,
750 win
->ws
->r
->s
->color_focus
);
754 XSetInputFocus(display
, win
->id
,
755 RevertToPointerRoot
, CurrentTime
);
760 switchws(struct swm_region
*r
, union arg
*args
)
763 struct swm_region
*this_r
, *other_r
;
765 struct workspace
*new_ws
, *old_ws
;
769 new_ws
= &this_r
->s
->ws
[wsid
];
771 DNPRINTF(SWM_D_WS
, "switchws screen %d region %dx%d+%d+%d: "
772 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
775 if (new_ws
== old_ws
)
780 /* if the other workspace is hidden, switch windows */
781 /* map new window first to prevent ugly blinking */
782 TAILQ_FOREACH(win
, &new_ws
->winlist
, entry
)
783 XMapRaised(display
, win
->id
);
785 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
786 XUnmapWindow(display
, win
->id
);
791 other_r
->ws
= old_ws
;
800 focus_win(new_ws
->focus
);
806 cyclews(struct swm_region
*r
, union arg
*args
)
809 struct swm_screen
*s
= r
->s
;
811 DNPRINTF(SWM_D_WS
, "cyclews id %d "
812 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
813 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
819 case SWM_ARG_ID_CYCLEWS_UP
:
820 if (a
.id
< SWM_WS_MAX
- 1)
825 case SWM_ARG_ID_CYCLEWS_DOWN
:
829 a
.id
= SWM_WS_MAX
- 1;
835 if (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
))
837 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
841 } while (a
.id
!= r
->ws
->idx
);
845 swapwin(struct swm_region
*r
, union arg
*args
)
847 struct ws_win
*target
;
848 struct ws_win_list
*wl
;
851 DNPRINTF(SWM_D_WS
, "swapwin id %d "
852 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
853 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
854 if (cur_focus
== NULL
)
857 wl
= &cur_focus
->ws
->winlist
;
860 case SWM_ARG_ID_SWAPPREV
:
861 target
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
862 TAILQ_REMOVE(wl
, cur_focus
, entry
);
864 TAILQ_INSERT_TAIL(wl
, cur_focus
, entry
);
866 TAILQ_INSERT_BEFORE(target
, cur_focus
, entry
);
868 case SWM_ARG_ID_SWAPNEXT
:
869 target
= TAILQ_NEXT(cur_focus
, entry
);
870 TAILQ_REMOVE(wl
, cur_focus
, entry
);
872 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
874 TAILQ_INSERT_AFTER(wl
, target
, cur_focus
, entry
);
876 case SWM_ARG_ID_SWAPMAIN
:
877 target
= TAILQ_FIRST(wl
);
878 if (target
== cur_focus
)
880 TAILQ_REMOVE(wl
, target
, entry
);
881 TAILQ_INSERT_BEFORE(cur_focus
, target
, entry
);
882 TAILQ_REMOVE(wl
, cur_focus
, entry
);
883 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
886 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
895 focus(struct swm_region
*r
, union arg
*args
)
897 struct ws_win
*winfocus
, *winlostfocus
;
898 struct ws_win_list
*wl
;
900 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
901 if (cur_focus
== NULL
)
904 wl
= &cur_focus
->ws
->winlist
;
906 winlostfocus
= cur_focus
;
909 case SWM_ARG_ID_FOCUSPREV
:
910 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
911 if (winfocus
== NULL
)
912 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
915 case SWM_ARG_ID_FOCUSNEXT
:
916 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
917 if (winfocus
== NULL
)
918 winfocus
= TAILQ_FIRST(wl
);
921 case SWM_ARG_ID_FOCUSMAIN
:
922 winfocus
= TAILQ_FIRST(wl
);
929 if (winfocus
== winlostfocus
)
933 XSync(display
, False
);
937 cycle_layout(struct swm_region
*r
, union arg
*args
)
939 struct workspace
*ws
= r
->ws
;
941 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
944 if (ws
->cur_layout
->l_stack
== NULL
)
945 ws
->cur_layout
= &layouts
[0];
952 stack_config(struct swm_region
*r
, union arg
*args
)
954 struct workspace
*ws
= r
->ws
;
956 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
959 if (ws
->cur_layout
->l_config
!= NULL
)
960 ws
->cur_layout
->l_config(ws
, args
->id
);
962 if (args
->id
!= SWM_ARG_ID_STACKINIT
);
968 struct swm_geometry g
;
969 struct swm_region
*r
;
972 DNPRINTF(SWM_D_STACK
, "stack\n");
974 for (i
= 0; i
< ScreenCount(display
); i
++) {
976 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
977 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
978 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
980 /* start with screen geometry, adjust for bar */
990 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
993 XSync(display
, False
);
997 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
1002 bzero(&wc
, sizeof wc
);
1003 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
1004 wc
.border_width
= 1;
1005 if (win
->transient
) {
1006 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
1007 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
1009 wc
.width
= win
->g
.w
;
1010 wc
.height
= win
->g
.h
;
1011 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1012 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1014 DNPRINTF(SWM_D_STACK
, "stack_floater: win %lu x %d y %d w %d h %d\n",
1015 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1017 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1022 vertical_config(struct workspace
*ws
, int id
)
1024 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
1027 case SWM_ARG_ID_STACKRESET
:
1028 case SWM_ARG_ID_STACKINIT
:
1029 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
1030 ws
->l_state
.vertical_mwin
= 1;
1032 case SWM_ARG_ID_MASTERSHRINK
:
1033 if (ws
->l_state
.vertical_msize
> 1)
1034 ws
->l_state
.vertical_msize
--;
1036 case SWM_ARG_ID_MASTERGROW
:
1037 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
1038 ws
->l_state
.vertical_msize
++;
1040 case SWM_ARG_ID_MASTERADD
:
1041 ws
->l_state
.vertical_mwin
++;
1043 case SWM_ARG_ID_MASTERDEL
:
1044 if (ws
->l_state
.vertical_mwin
> 0)
1045 ws
->l_state
.vertical_mwin
--;
1053 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1055 struct swm_geometry gg
= *g
;
1056 struct ws_win
*win
, *winfocus
;
1057 int i
, j
, split
, colno
, hrh
, winno
, main_width
;
1060 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1062 if ((winno
= count_win(ws
, 0)) == 0)
1065 if (ws
->focus
== NULL
)
1066 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1067 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1069 if (ws
->l_state
.vertical_mwin
&&
1070 winno
> ws
->l_state
.vertical_mwin
) {
1071 split
= ws
->l_state
.vertical_mwin
;
1073 main_width
= (g
->w
/ SWM_V_SLICE
) *
1074 ws
->l_state
.vertical_msize
;
1084 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1085 if (split
&& i
== split
) {
1086 colno
= winno
- split
;
1087 hrh
= (g
->h
/ colno
);
1088 gg
.x
+= main_width
+ 2;
1089 gg
.w
= g
->w
- (main_width
+ 2);
1094 gg
.h
= (hrh
+ (g
->h
- (colno
* hrh
)));
1102 if (win
->transient
!= 0 || win
->floating
!= 0)
1103 stack_floater(win
, ws
->r
);
1105 bzero(&wc
, sizeof wc
);
1106 wc
.border_width
= 1;
1107 win
->g
.x
= wc
.x
= gg
.x
;
1108 win
->g
.y
= wc
.y
= gg
.y
;
1109 win
->g
.w
= wc
.width
= gg
.w
;
1110 win
->g
.h
= wc
.height
= gg
.h
;
1111 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1112 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1115 XMapRaised(display
, win
->id
);
1121 focus_win(winfocus
); /* has to be done outside of the loop */
1126 horizontal_config(struct workspace
*ws
, int id
)
1128 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
1131 case SWM_ARG_ID_STACKRESET
:
1132 case SWM_ARG_ID_STACKINIT
:
1133 ws
->l_state
.horizontal_mwin
= 1;
1134 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
1136 case SWM_ARG_ID_MASTERSHRINK
:
1137 if (ws
->l_state
.horizontal_msize
> 1)
1138 ws
->l_state
.horizontal_msize
--;
1140 case SWM_ARG_ID_MASTERGROW
:
1141 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
1142 ws
->l_state
.horizontal_msize
++;
1144 case SWM_ARG_ID_MASTERADD
:
1145 ws
->l_state
.horizontal_mwin
++;
1147 case SWM_ARG_ID_MASTERDEL
:
1148 if (ws
->l_state
.horizontal_mwin
> 0)
1149 ws
->l_state
.horizontal_mwin
--;
1157 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1159 struct swm_geometry gg
= *g
;
1160 struct ws_win
*win
, *winfocus
;
1161 int i
, j
, split
, rowno
, hrw
, winno
, main_height
;
1164 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
1166 if ((winno
= count_win(ws
, 0)) == 0)
1169 if (ws
->focus
== NULL
)
1170 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1171 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1173 if (ws
->l_state
.horizontal_mwin
&&
1174 winno
> ws
->l_state
.horizontal_mwin
) {
1175 split
= ws
->l_state
.horizontal_mwin
;
1177 main_height
= (g
->h
/ SWM_V_SLICE
) *
1178 ws
->l_state
.horizontal_msize
;
1188 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1189 if (split
&& i
== split
) {
1190 rowno
= winno
- split
;
1191 hrw
= (g
->w
/ rowno
);
1192 gg
.y
+= main_height
+ 2;
1193 gg
.h
= g
->h
- (main_height
+ 2);
1198 gg
.w
= (hrw
+ (g
->w
- (rowno
* hrw
)));
1205 if (win
->transient
!= 0 || win
->floating
!= 0)
1206 stack_floater(win
, ws
->r
);
1208 bzero(&wc
, sizeof wc
);
1209 wc
.border_width
= 1;
1210 win
->g
.x
= wc
.x
= gg
.x
;
1211 win
->g
.y
= wc
.y
= gg
.y
;
1212 win
->g
.w
= wc
.width
= gg
.w
;
1213 win
->g
.h
= wc
.height
= gg
.h
;
1214 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1215 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1218 XMapRaised(display
, win
->id
);
1224 focus_win(winfocus
); /* this has to be done outside of the loop */
1227 /* fullscreen view */
1229 max_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1231 struct swm_geometry gg
= *g
;
1232 struct ws_win
*win
, *winfocus
;
1235 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
1237 if (count_win(ws
, 0) == 0)
1240 if (ws
->focus
== NULL
)
1241 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1242 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1244 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1245 if (win
->transient
!= 0 || win
->floating
!= 0) {
1246 if (win
== ws
->focus
) {
1248 stack_floater(win
, ws
->r
);
1249 XMapRaised(display
, win
->id
);
1251 XUnmapWindow(display
, win
->id
);
1253 bzero(&wc
, sizeof wc
);
1254 wc
.border_width
= 1;
1255 win
->g
.x
= wc
.x
= gg
.x
;
1256 win
->g
.y
= wc
.y
= gg
.y
;
1257 win
->g
.w
= wc
.width
= gg
.w
;
1258 win
->g
.h
= wc
.height
= gg
.h
;
1259 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1260 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1262 if (win
== ws
->focus
) {
1263 XMapRaised(display
, win
->id
);
1265 XUnmapWindow(display
, win
->id
);
1270 focus_win(winfocus
); /* has to be done outside of the loop */
1274 send_to_ws(struct swm_region
*r
, union arg
*args
)
1276 int wsid
= args
->id
;
1277 struct ws_win
*win
= cur_focus
;
1278 struct workspace
*ws
, *nws
;
1280 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
1283 nws
= &win
->s
->ws
[wsid
];
1285 XUnmapWindow(display
, win
->id
);
1287 /* find a window to focus */
1288 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1289 if (ws
->focus
== NULL
)
1290 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1291 if (ws
->focus
== win
)
1294 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1296 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
1299 if (count_win(nws
, 1) == 1)
1307 /* key definitions */
1311 void (*func
)(struct swm_region
*r
, union arg
*);
1314 /* modifier key function argument */
1315 { MODKEY
, XK_space
, cycle_layout
, {0} },
1316 { MODKEY
| ShiftMask
, XK_space
, stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
1317 { MODKEY
, XK_h
, stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
1318 { MODKEY
, XK_l
, stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
1319 { MODKEY
, XK_comma
, stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
1320 { MODKEY
, XK_period
, stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
1321 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
1322 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1323 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1324 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
1325 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
1326 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
1327 { MODKEY
, XK_p
, spawn
, {.argv
= spawn_menu
} },
1328 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
1329 { MODKEY
, XK_q
, restart
, {0} },
1330 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
1331 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
1332 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
1333 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
1334 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
1335 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
1336 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
1337 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
1338 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
1339 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
1340 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
1341 { MODKEY
, XK_Right
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
1342 { MODKEY
, XK_Left
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
1343 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
1344 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
1345 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
1346 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
1347 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
1348 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
1349 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
1350 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
1351 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
1352 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
1353 { MODKEY
, XK_b
, bar_toggle
, {0} },
1354 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1355 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1359 click(struct swm_region
*r
, union arg
*args
)
1361 DNPRINTF(SWM_D_MOUSE
, "click: button: %d\n", args
->id
);
1376 enum { client_click
, root_click
};
1378 unsigned int action
;
1380 unsigned int button
;
1381 void (*func
)(struct swm_region
*r
, union arg
*);
1384 /* action key mouse button func args */
1385 { client_click
, MODKEY
, Button1
, click
, {.id
=Button1
} },
1389 updatenumlockmask(void)
1392 XModifierKeymap
*modmap
;
1394 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
1396 modmap
= XGetModifierMapping(display
);
1397 for (i
= 0; i
< 8; i
++)
1398 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1399 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1400 == XKeysymToKeycode(display
, XK_Num_Lock
))
1401 numlockmask
= (1 << i
);
1403 XFreeModifiermap(modmap
);
1409 unsigned int i
, j
, k
;
1411 unsigned int modifiers
[] =
1412 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
1414 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
1415 updatenumlockmask();
1417 for (k
= 0; k
< ScreenCount(display
); k
++) {
1418 if (TAILQ_EMPTY(&screens
[k
].rl
))
1420 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
1421 for (i
= 0; i
< LENGTH(keys
); i
++) {
1422 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
1423 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1424 XGrabKey(display
, code
,
1425 keys
[i
].mod
| modifiers
[j
],
1426 screens
[k
].root
, True
,
1427 GrabModeAsync
, GrabModeAsync
);
1433 grabbuttons(struct ws_win
*win
, int focused
)
1436 unsigned int modifiers
[] =
1437 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1439 updatenumlockmask();
1440 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
1442 for (i
= 0; i
< LENGTH(buttons
); i
++)
1443 if (buttons
[i
].action
== client_click
)
1444 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1445 XGrabButton(display
, buttons
[i
].button
,
1446 buttons
[i
].mask
| modifiers
[j
],
1447 win
->id
, False
, BUTTONMASK
,
1448 GrabModeAsync
, GrabModeSync
, None
,
1451 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
1452 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
1458 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
1466 XKeyEvent
*ev
= &e
->xkey
;
1468 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
1470 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
1471 for (i
= 0; i
< LENGTH(keys
); i
++)
1472 if (keysym
== keys
[i
].keysym
1473 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1475 keys
[i
].func(root_to_region(ev
->root
),
1480 buttonpress(XEvent
*e
)
1482 XButtonPressedEvent
*ev
= &e
->xbutton
;
1487 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
1489 action
= root_click
;
1490 if ((win
= find_window(ev
->window
)) == NULL
)
1494 action
= client_click
;
1497 for (i
= 0; i
< LENGTH(buttons
); i
++)
1498 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
1499 buttons
[i
].button
== ev
->button
&&
1500 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
1501 buttons
[i
].func(root_to_region(ev
->root
),
1506 set_win_state(struct ws_win
*win
, long state
)
1508 long data
[] = {state
, None
};
1510 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
1512 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1513 (unsigned char *)data
, 2);
1517 manage_window(Window id
, struct workspace
*ws
)
1523 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1525 return (win
); /* already being managed */
1528 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
1529 errx(1, "calloc: failed to allocate memory for new window");
1533 win
->s
= ws
->r
->s
; /* this never changes */
1534 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
1536 /* make new win focused */
1539 XGetTransientForHint(display
, win
->id
, &trans
);
1541 win
->transient
= trans
;
1542 DNPRINTF(SWM_D_MISC
, "manage_window: win %u transient %u\n",
1543 (unsigned)win
->id
, win
->transient
);
1545 XGetWindowAttributes(display
, id
, &win
->wa
);
1546 win
->g
.w
= win
->wa
.width
;
1547 win
->g
.h
= win
->wa
.height
;
1548 win
->g
.x
= win
->wa
.x
;
1549 win
->g
.y
= win
->wa
.y
;
1551 /* XXX make this a table */
1552 bzero(&ch
, sizeof ch
);
1553 if (XGetClassHint(display
, win
->id
, &ch
)) {
1554 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1555 if (!strcmp(ch
.res_class
, "MPlayer") && !strcmp(ch
.res_name
, "xv")) {
1559 XFree(ch
.res_class
);
1564 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
1565 PropertyChangeMask
| StructureNotifyMask
);
1567 set_win_state(win
, NormalState
);
1573 configurerequest(XEvent
*e
)
1575 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
1580 if ((win
= find_window(ev
->window
)) == NULL
)
1584 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
1586 bzero(&wc
, sizeof wc
);
1589 wc
.width
= ev
->width
;
1590 wc
.height
= ev
->height
;
1591 wc
.border_width
= ev
->border_width
;
1592 wc
.sibling
= ev
->above
;
1593 wc
.stack_mode
= ev
->detail
;
1594 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
1596 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
1598 if (win
->floating
) {
1599 if (ev
->value_mask
& CWX
)
1601 if (ev
->value_mask
& CWY
)
1603 if (ev
->value_mask
& CWWidth
)
1604 win
->g
.w
= ev
->width
;
1605 if (ev
->value_mask
& CWHeight
)
1606 win
->g
.h
= ev
->height
;
1607 if (win
->ws
->r
!= NULL
) {
1608 /* this seems to be full screen */
1609 if (win
->g
.w
> WIDTH(win
->ws
->r
)) {
1614 if (win
->g
.h
> HEIGHT(win
->ws
->r
)) {
1620 if ((ev
->value_mask
& (CWX
|CWY
)) &&
1621 !(ev
->value_mask
& (CWWidth
|CWHeight
)))
1623 XMoveResizeWindow(display
, win
->id
,
1624 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
1631 configurenotify(XEvent
*e
)
1633 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
1634 e
->xconfigure
.window
);
1638 destroynotify(XEvent
*e
)
1641 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
1642 struct workspace
*ws
;
1644 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
1646 if ((win
= find_window(ev
->window
)) != NULL
) {
1648 /* find a window to focus */
1649 if (ws
->focus
== win
)
1650 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1651 if (ws
->focus
== NULL
)
1652 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1653 if (ws
->focus
== win
)
1655 if (cur_focus
== win
)
1656 focus_win(ws
->focus
);
1658 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1659 set_win_state(win
, WithdrawnState
);
1666 enternotify(XEvent
*e
)
1668 XCrossingEvent
*ev
= &e
->xcrossing
;
1671 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
1674 /* eat event(r) to prevent autofocus */
1679 if ((win
= find_window(ev
->window
)) != NULL
)
1686 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", e
->xfocus
.window
);
1690 mappingnotify(XEvent
*e
)
1692 XMappingEvent
*ev
= &e
->xmapping
;
1694 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
1696 XRefreshKeyboardMapping(ev
);
1697 if (ev
->request
== MappingKeyboard
)
1702 maprequest(XEvent
*e
)
1704 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1705 XWindowAttributes wa
;
1706 struct swm_region
*r
;
1708 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
1709 e
->xmaprequest
.window
);
1711 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
1713 if (wa
.override_redirect
)
1715 r
= root_to_region(wa
.root
);
1716 manage_window(e
->xmaprequest
.window
, r
->ws
);
1721 propertynotify(XEvent
*e
)
1723 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
1724 e
->xproperty
.window
);
1728 unmapnotify(XEvent
*e
)
1730 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
1734 visibilitynotify(XEvent
*e
)
1737 struct swm_region
*r
;
1739 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
1740 e
->xvisibility
.window
);
1741 if (e
->xvisibility
.state
== VisibilityUnobscured
)
1742 for (i
= 0; i
< ScreenCount(display
); i
++)
1743 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1744 if (e
->xvisibility
.window
== r
->bar_window
)
1748 void (*handler
[LASTEvent
])(XEvent
*) = {
1750 [KeyPress
] = keypress
,
1751 [ButtonPress
] = buttonpress
,
1752 [ConfigureRequest
] = configurerequest
,
1753 [ConfigureNotify
] = configurenotify
,
1754 [DestroyNotify
] = destroynotify
,
1755 [EnterNotify
] = enternotify
,
1756 [FocusIn
] = focusin
,
1757 [MappingNotify
] = mappingnotify
,
1758 [MapRequest
] = maprequest
,
1759 [PropertyNotify
] = propertynotify
,
1760 [UnmapNotify
] = unmapnotify
,
1761 [VisibilityNotify
] = visibilitynotify
,
1765 xerror_start(Display
*d
, XErrorEvent
*ee
)
1772 xerror(Display
*d
, XErrorEvent
*ee
)
1774 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1782 xerrorxlib
= XSetErrorHandler(xerror_start
);
1784 /* this causes an error if some other window manager is running */
1785 XSelectInput(display
, DefaultRootWindow(display
),
1786 SubstructureRedirectMask
);
1787 XSync(display
, False
);
1791 XSetErrorHandler(xerror
);
1792 XSync(display
, False
);
1801 unsigned char *p
= NULL
;
1802 unsigned long n
, extra
;
1805 astate
= XInternAtom(display
, "WM_STATE", False
);
1806 status
= XGetWindowProperty(display
, w
, astate
, 0L, 2L, False
, astate
,
1807 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
1808 if (status
!= Success
)
1817 new_region(struct swm_screen
*s
, struct workspace
*ws
,
1818 int x
, int y
, int w
, int h
)
1820 struct swm_region
*r
;
1822 DNPRINTF(SWM_D_MISC
, "new region on screen %d: %dx%d (%d, %d)\n",
1823 s
->idx
, x
, y
, w
, h
);
1825 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
1826 errx(1, "calloc: failed to allocate memory for screen");
1835 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
1842 #ifdef SWM_XRR_HAS_CRTC
1844 XRRScreenResources
*sr
;
1846 #endif /* SWM_XRR_HAS_CRTC */
1847 Window d1
, d2
, *wins
= NULL
;
1848 XWindowAttributes wa
;
1849 struct swm_region
*r
;
1851 int errorbase
, major
, minor
;
1852 int ncrtc
= 0, w
= 0;
1854 struct workspace
*ws
;
1856 if ((screens
= calloc(ScreenCount(display
),
1857 sizeof(struct swm_screen
))) == NULL
)
1858 errx(1, "calloc: screens");
1860 /* map physical screens */
1861 for (i
= 0; i
< ScreenCount(display
); i
++) {
1862 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
1864 TAILQ_INIT(&screens
[i
].rl
);
1865 screens
[i
].root
= RootWindow(display
, i
);
1867 /* set default colors */
1868 screens
[i
].color_focus
= name_to_color("red");
1869 screens
[i
].color_unfocus
= name_to_color("rgb:88/88/88");
1870 screens
[i
].bar_border
= name_to_color("rgb:00/80/80");
1871 screens
[i
].bar_color
= name_to_color("black");
1872 screens
[i
].bar_font_color
= name_to_color("rgb:a0/a0/a0");
1874 /* init all workspaces */
1875 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
1876 ws
= &screens
[i
].ws
[j
];
1881 TAILQ_INIT(&ws
->winlist
);
1883 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
1884 if (layouts
[k
].l_config
!= NULL
)
1885 layouts
[k
].l_config(ws
,
1886 SWM_ARG_ID_STACKINIT
);
1887 ws
->cur_layout
= &layouts
[0];
1890 /* map virtual screens onto physical screens */
1891 screens
[i
].xrandr_support
= XRRQueryExtension(display
,
1892 &xrandr_eventbase
, &errorbase
);
1893 if (screens
[i
].xrandr_support
)
1894 if (XRRQueryVersion(display
, &major
, &minor
) &&
1896 screens
[i
].xrandr_support
= 0;
1898 #if 0 /* not ready for dynamic screen changes */
1899 if (screens
[i
].xrandr_support
)
1900 XRRSelectInput(display
,
1902 RRScreenChangeNotifyMask
);
1905 /* grab existing windows (before we build the bars)*/
1906 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
1909 #ifdef SWM_XRR_HAS_CRTC
1910 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
1912 new_region(&screens
[i
], &screens
[i
].ws
[w
],
1913 0, 0, DisplayWidth(display
, i
),
1914 DisplayHeight(display
, i
));
1918 for (c
= 0; c
< ncrtc
; c
++) {
1919 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
1920 if (ci
->noutput
== 0)
1923 if (ci
!= NULL
&& ci
->mode
== None
)
1924 new_region(&screens
[i
], &screens
[i
].ws
[w
], 0, 0,
1925 DisplayWidth(display
, i
),
1926 DisplayHeight(display
, i
));
1928 new_region(&screens
[i
], &screens
[i
].ws
[w
],
1929 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
1932 XRRFreeCrtcInfo(ci
);
1933 XRRFreeScreenResources(sr
);
1935 new_region(&screens
[i
], &screens
[i
].ws
[w
], 0, 0,
1936 DisplayWidth(display
, i
),
1937 DisplayHeight(display
, i
));
1938 #endif /* SWM_XRR_HAS_CRTC */
1940 /* attach windows to a region */
1941 /* normal windows */
1942 if ((r
= TAILQ_FIRST(&screens
[i
].rl
)) == NULL
)
1943 errx(1, "no regions on screen %d", i
);
1945 for (i
= 0; i
< no
; i
++) {
1946 XGetWindowAttributes(display
, wins
[i
], &wa
);
1947 if (!XGetWindowAttributes(display
, wins
[i
], &wa
) ||
1948 wa
.override_redirect
||
1949 XGetTransientForHint(display
, wins
[i
], &d1
))
1952 if (wa
.map_state
== IsViewable
||
1953 getstate(wins
[i
]) == NormalState
)
1954 manage_window(wins
[i
], r
->ws
);
1956 /* transient windows */
1957 for (i
= 0; i
< no
; i
++) {
1958 if (!XGetWindowAttributes(display
, wins
[i
], &wa
))
1961 if (XGetTransientForHint(display
, wins
[i
], &d1
) &&
1962 (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) ==
1964 manage_window(wins
[i
], r
->ws
);
1974 main(int argc
, char *argv
[])
1977 char conf
[PATH_MAX
], *cfile
= NULL
;
1984 fprintf(stderr
, "Welcome to scrotwm V%s\n", SWM_VERSION
);
1985 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1986 warnx("no locale support");
1988 if (!(display
= XOpenDisplay(0)))
1989 errx(1, "can not open display");
1992 errx(1, "other wm running");
1994 astate
= XInternAtom(display
, "WM_STATE", False
);
1996 /* look for local and global conf file */
1997 pwd
= getpwuid(getuid());
1999 errx(1, "invalid user %d", getuid());
2003 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
2004 if (stat(conf
, &sb
) != -1) {
2005 if (S_ISREG(sb
.st_mode
))
2008 /* try global conf file */
2009 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
2010 if (!stat(conf
, &sb
))
2011 if (S_ISREG(sb
.st_mode
))
2018 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2023 xfd
= ConnectionNumber(display
);
2026 if (select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
2028 errx(1, "select failed");
2033 while(XPending(display
)) {
2034 XNextEvent(display
, &e
);
2035 if (handler
[e
.type
])
2036 handler
[e
.type
](&e
);
2040 XCloseDisplay(display
);