]>
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
103 u_int32_t swm_debug
= 0
112 #define DPRINTF(x...)
113 #define DNPRINTF(n,x...)
116 #define LENGTH(x) (sizeof x / sizeof x[0])
117 #define MODKEY Mod1Mask
118 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
120 #define X(r) (r)->g.x
121 #define Y(r) (r)->g.y
122 #define WIDTH(r) (r)->g.w
123 #define HEIGHT(r) (r)->g.h
127 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
130 int xrandr_eventbase
;
131 int ignore_enter
= 0;
132 unsigned int numlockmask
= 0;
136 int cycle_visible
= 0;
139 double dialog_ratio
= .6;
141 #define SWM_BAR_MAX (128)
142 sig_atomic_t bar_alarm
= 0;
149 char *bar_fonts
[] = {
150 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
151 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
155 /* terminal + args */
156 char *spawn_term
[] = { "xterm", NULL
};
157 char *spawn_menu
[] = { "dmenu_run", NULL
};
159 /* layout manager data */
160 struct swm_geometry
{
170 /* virtual "screens" */
172 TAILQ_ENTRY(swm_region
) entry
;
173 struct swm_geometry g
;
175 struct workspace
*ws
; /* current workspace on this region */
176 struct swm_screen
*s
; /* screen idx */
178 TAILQ_HEAD(swm_region_list
, swm_region
);
182 TAILQ_ENTRY(ws_win
) entry
;
184 struct swm_geometry g
;
188 struct workspace
*ws
; /* always valid */
189 struct swm_screen
*s
; /* always valid, never changes */
190 XWindowAttributes wa
;
192 TAILQ_HEAD(ws_win_list
, ws_win
);
194 /* layout handlers */
196 void vertical_config(struct workspace
*, int);
197 void vertical_stack(struct workspace
*, struct swm_geometry
*);
198 void horizontal_config(struct workspace
*, int);
199 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
200 void max_stack(struct workspace
*, struct swm_geometry
*);
203 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
204 void (*l_config
)(struct workspace
*, int);
206 /* stack, configure */
207 { vertical_stack
, vertical_config
},
208 { horizontal_stack
, horizontal_config
},
213 #define SWM_H_SLICE (32)
214 #define SWM_V_SLICE (32)
216 /* define work spaces */
218 int idx
; /* workspace index */
219 int restack
; /* restack on switch */
220 struct layout
*cur_layout
; /* current layout handlers */
221 struct ws_win
*focus
; /* may be NULL */
222 struct swm_region
*r
; /* may be NULL */
223 struct ws_win_list winlist
; /* list of windows in ws */
227 int horizontal_msize
;
234 /* physical screen mapping */
235 #define SWM_WS_MAX (10) /* XXX Too small? */
237 int idx
; /* screen index */
238 struct swm_region_list rl
; /* list of regions on this screen */
241 struct workspace ws
[SWM_WS_MAX
];
244 unsigned long bar_border
;
245 unsigned long bar_color
;
246 unsigned long bar_font_color
;
247 unsigned long color_focus
; /* XXX should this be per ws? */
248 unsigned long color_unfocus
;
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)
267 #define SWM_ARG_ID_MASTERADD (8)
268 #define SWM_ARG_ID_MASTERDEL (9)
269 #define SWM_ARG_ID_STACKRESET (10)
270 #define SWM_ARG_ID_STACKINIT (11)
271 #define SWM_ARG_ID_CYCLEWS_UP (12)
272 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
277 name_to_color(char *colorname
)
281 XColor screen_def
, exact_def
;
282 unsigned long result
= 0;
283 char cname
[32] = "#";
285 cmap
= DefaultColormap(display
, screens
[0].idx
);
286 status
= XAllocNamedColor(display
, cmap
, colorname
,
287 &screen_def
, &exact_def
);
289 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
290 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
294 result
= screen_def
.pixel
;
296 fprintf(stderr
, "color '%s' not found.\n", colorname
);
302 varmatch(char *var
, char *name
, int *index
)
307 i
= strncmp(var
, name
, 255);
314 p
= var
+ strlen(name
);
317 bzero(buf
, sizeof buf
);
319 while (isdigit(*p
) && i
< sizeof buf
)
321 if (i
== 0 || i
>= sizeof buf
|| *p
!= ']')
323 *index
= strtonum(buf
, 0, 99, NULL
);
327 /* conf file stuff */
328 #define SWM_CONF_WS "\n= \t"
329 #define SWM_CONF_FILE "scrotwm.conf"
331 conf_load(char *filename
)
334 char *line
, *cp
, *var
, *val
;
335 size_t len
, lineno
= 0;
338 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
340 if (filename
== NULL
)
343 if ((config
= fopen(filename
, "r")) == NULL
)
346 for (sc
= ScreenCount(display
);;) {
347 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
351 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
357 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
359 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
360 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
363 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
366 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
367 bar_enabled
= atoi(val
);
368 else if (!varmatch(var
, "bar_border", &i
))
369 if (i
> 0 && i
<= sc
)
370 screens
[i
- 1].bar_border
=
373 for (i
= 0; i
< sc
; i
++)
374 screens
[i
].bar_border
=
378 else if (!varmatch(var
, "bar_color", &i
))
379 if (i
> 0 && i
<= sc
)
380 screens
[i
- 1].bar_color
=
383 for (i
= 0; i
< sc
; i
++)
384 screens
[i
].bar_color
=
388 else if (!varmatch(var
, "bar_font_color", &i
))
389 if (i
> 0 && i
<= sc
)
390 screens
[i
- 1].bar_font_color
=
393 for (i
= 0; i
< sc
; i
++)
394 screens
[i
].bar_font_color
=
399 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
400 asprintf(&bar_fonts
[0], "%s", val
);
406 if (!varmatch(var
, "color_focus", &i
))
407 if (i
> 0 && i
<= sc
)
408 screens
[i
- 1].color_focus
=
411 for (i
= 0; i
< sc
; i
++)
412 screens
[i
].color_focus
=
416 else if (!varmatch(var
, "color_unfocus", &i
))
417 if (i
> 0 && i
<= sc
)
418 screens
[i
- 1].color_unfocus
=
421 for (i
= 0; i
< sc
; i
++)
422 screens
[i
].color_unfocus
=
426 else if (!strncmp(var
, "cycle_empty", strlen("cycle_empty")))
427 cycle_visible
= atoi(val
);
428 else if (!strncmp(var
, "cycle_visible", strlen("cycle_visible")))
429 cycle_visible
= atoi(val
);
435 if (!strncmp(var
, "dialog_ratio",
436 strlen("dialog_ratio"))) {
437 dialog_ratio
= atof(val
);
438 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
445 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
446 asprintf(&spawn_term
[0], "%s", val
); /* XXX args? */
457 errx(1, "invalid conf file entry: %s=%s", var
, val
);
459 errx(1, "invalid screen index: %s out of bounds", var
);
463 bar_print(struct swm_region
*r
, char *s
)
465 XClearWindow(display
, r
->bar_window
);
466 XSetForeground(display
, bar_gc
, r
->s
->bar_font_color
);
467 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
, s
,
476 struct swm_region
*r
;
481 if (bar_enabled
== 0)
485 localtime_r(&tmt
, &tm
);
486 strftime(s
, sizeof s
, "%a %b %d %R %Z %Y", &tm
);
487 for (i
= 0; i
< ScreenCount(display
); i
++) {
489 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
490 snprintf(e
, sizeof e
, "%s %d:%d",
491 s
, x
++, r
->ws
->idx
+ 1);
495 XSync(display
, False
);
506 bar_toggle(struct swm_region
*r
, union arg
*args
)
508 struct swm_region
*tmpr
;
509 int i
, j
, sc
= ScreenCount(display
);
511 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
514 for (i
= 0; i
< sc
; i
++)
515 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
516 XUnmapWindow(display
, tmpr
->bar_window
);
518 for (i
= 0; i
< sc
; i
++)
519 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
520 XMapRaised(display
, tmpr
->bar_window
);
522 bar_enabled
= !bar_enabled
;
523 XSync(display
, False
);
524 for (i
= 0; i
< sc
; i
++)
525 for (j
= 0; j
< SWM_WS_MAX
; j
++)
526 screens
[i
].ws
[j
].restack
= 1;
529 /* must be after stack */
530 for (i
= 0; i
< sc
; i
++)
531 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
536 bar_setup(struct swm_region
*r
)
540 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
541 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
545 if (bar_fonts
[i
] == NULL
)
546 errx(1, "couldn't load font");
547 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
549 r
->bar_window
= XCreateSimpleWindow(display
,
550 r
->s
->root
, X(r
), Y(r
), WIDTH(r
) - 2, bar_height
- 2,
551 1, r
->s
->bar_border
, r
->s
->bar_color
);
552 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
553 XSetFont(display
, bar_gc
, bar_fs
->fid
);
554 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
556 XMapRaised(display
, r
->bar_window
);
557 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
559 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
560 err(1, "could not install bar_signal");
567 XSetWindowAttributes wa
;
568 struct swm_region
*r
;
571 bzero(&wa
, sizeof wa
);
572 for (i
= 0; i
< ScreenCount(display
); i
++)
573 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
574 wa
.border_pixel
= screens
[i
].bar_border
;
575 wa
.background_pixel
= screens
[i
].bar_color
;
576 XChangeWindowAttributes(display
, r
->bar_window
,
577 CWBackPixel
| CWBorderPixel
, &wa
);
583 config_win(struct ws_win
*win
)
587 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
588 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
589 ce
.type
= ConfigureNotify
;
590 ce
.display
= display
;
596 ce
.height
= win
->g
.h
;
597 ce
.border_width
= 1; /* XXX store this! */
599 ce
.override_redirect
= False
;
600 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
604 count_win(struct workspace
*ws
, int count_transient
)
609 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
610 if (count_transient
== 0 && win
->floating
)
612 if (count_transient
== 0 && win
->transient
)
616 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
622 quit(struct swm_region
*r
, union arg
*args
)
624 DNPRINTF(SWM_D_MISC
, "quit\n");
629 restart(struct swm_region
*r
, union arg
*args
)
631 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
633 /* disable alarm because the following code may not be interrupted */
635 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
636 errx(1, "can't disable alarm");
638 XCloseDisplay(display
);
639 execvp(start_argv
[0], start_argv
);
640 fprintf(stderr
, "execvp failed\n");
646 root_to_region(Window root
)
648 struct swm_region
*r
;
653 for (i
= 0; i
< ScreenCount(display
); i
++)
654 if (screens
[i
].root
== root
)
657 if (rootclick
!= root
&& /* if root was just clicked in, use cursor */
658 cur_focus
&& cur_focus
->ws
->r
&& cur_focus
->s
== &screens
[i
])
659 r
= cur_focus
->ws
->r
;
661 if (XQueryPointer(display
, screens
[i
].root
,
662 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) == False
) {
663 r
= TAILQ_FIRST(&screens
[i
].rl
);
665 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
666 if (x
> X(r
) && x
< X(r
) + WIDTH(r
) &&
667 y
> Y(r
) && y
< Y(r
) + HEIGHT(r
))
672 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
)
727 XSetWindowBorder(display
, win
->id
,
728 win
->ws
->r
->s
->color_unfocus
);
730 win
->ws
->focus
= NULL
;
736 focus_win(struct ws_win
*win
)
738 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
? win
->id
: 0);
745 win
->ws
->focus
= win
;
746 if (win
->ws
->r
!= NULL
) {
749 XSetWindowBorder(display
, win
->id
,
750 win
->ws
->r
->s
->color_focus
);
752 XSetInputFocus(display
, win
->id
,
753 RevertToPointerRoot
, CurrentTime
);
758 switchws(struct swm_region
*r
, union arg
*args
)
761 struct swm_region
*this_r
, *other_r
;
763 struct workspace
*new_ws
, *old_ws
;
767 new_ws
= &this_r
->s
->ws
[wsid
];
769 DNPRINTF(SWM_D_WS
, "switchws screen %d region %dx%d+%d+%d: "
770 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
773 if (new_ws
== old_ws
)
778 /* if the other workspace is hidden, switch windows */
779 /* map new window first to prevent ugly blinking */
780 TAILQ_FOREACH(win
, &new_ws
->winlist
, entry
)
781 XMapRaised(display
, win
->id
);
783 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
784 XUnmapWindow(display
, win
->id
);
789 other_r
->ws
= old_ws
;
798 focus_win(new_ws
->focus
);
804 cyclews(struct swm_region
*r
, union arg
*args
)
807 struct swm_screen
*s
= r
->s
;
809 DNPRINTF(SWM_D_WS
, "cyclews id %d "
810 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
811 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
817 case SWM_ARG_ID_CYCLEWS_UP
:
818 if (a
.id
< SWM_WS_MAX
- 1)
823 case SWM_ARG_ID_CYCLEWS_DOWN
:
827 a
.id
= SWM_WS_MAX
- 1;
833 if (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
))
835 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
839 } while (a
.id
!= r
->ws
->idx
);
843 swapwin(struct swm_region
*r
, union arg
*args
)
845 struct ws_win
*target
;
846 struct ws_win_list
*wl
;
849 DNPRINTF(SWM_D_WS
, "swapwin id %d "
850 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
851 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
852 if (cur_focus
== NULL
)
855 wl
= &cur_focus
->ws
->winlist
;
858 case SWM_ARG_ID_SWAPPREV
:
859 target
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
860 TAILQ_REMOVE(wl
, cur_focus
, entry
);
862 TAILQ_INSERT_TAIL(wl
, cur_focus
, entry
);
864 TAILQ_INSERT_BEFORE(target
, cur_focus
, entry
);
866 case SWM_ARG_ID_SWAPNEXT
:
867 target
= TAILQ_NEXT(cur_focus
, entry
);
868 TAILQ_REMOVE(wl
, cur_focus
, entry
);
870 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
872 TAILQ_INSERT_AFTER(wl
, target
, cur_focus
, entry
);
874 case SWM_ARG_ID_SWAPMAIN
:
875 target
= TAILQ_FIRST(wl
);
876 if (target
== cur_focus
)
878 TAILQ_REMOVE(wl
, target
, entry
);
879 TAILQ_INSERT_BEFORE(cur_focus
, target
, entry
);
880 TAILQ_REMOVE(wl
, cur_focus
, entry
);
881 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
884 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
893 focus(struct swm_region
*r
, union arg
*args
)
895 struct ws_win
*winfocus
, *winlostfocus
;
896 struct ws_win_list
*wl
;
898 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
899 if (cur_focus
== NULL
)
902 wl
= &cur_focus
->ws
->winlist
;
904 winlostfocus
= cur_focus
;
907 case SWM_ARG_ID_FOCUSPREV
:
908 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
909 if (winfocus
== NULL
)
910 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
913 case SWM_ARG_ID_FOCUSNEXT
:
914 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
915 if (winfocus
== NULL
)
916 winfocus
= TAILQ_FIRST(wl
);
919 case SWM_ARG_ID_FOCUSMAIN
:
920 winfocus
= TAILQ_FIRST(wl
);
927 if (winfocus
== winlostfocus
)
931 XSync(display
, False
);
935 cycle_layout(struct swm_region
*r
, union arg
*args
)
937 struct workspace
*ws
= r
->ws
;
939 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
942 if (ws
->cur_layout
->l_stack
== NULL
)
943 ws
->cur_layout
= &layouts
[0];
950 stack_config(struct swm_region
*r
, union arg
*args
)
952 struct workspace
*ws
= r
->ws
;
954 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
957 if (ws
->cur_layout
->l_config
!= NULL
)
958 ws
->cur_layout
->l_config(ws
, args
->id
);
960 if (args
->id
!= SWM_ARG_ID_STACKINIT
);
966 struct swm_geometry g
;
967 struct swm_region
*r
;
970 DNPRINTF(SWM_D_STACK
, "stack\n");
972 for (i
= 0; i
< ScreenCount(display
); i
++) {
974 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
975 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
976 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
978 /* start with screen geometry, adjust for bar */
988 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
991 XSync(display
, False
);
995 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
1000 bzero(&wc
, sizeof wc
);
1001 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
1002 wc
.border_width
= 1;
1003 if (win
->transient
) {
1004 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
1005 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
1007 wc
.width
= win
->g
.w
;
1008 wc
.height
= win
->g
.h
;
1009 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1010 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1012 DNPRINTF(SWM_D_STACK
, "stack_floater: win %lu x %d y %d w %d h %d\n",
1013 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1015 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1020 vertical_config(struct workspace
*ws
, int id
)
1022 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
1025 case SWM_ARG_ID_STACKRESET
:
1026 case SWM_ARG_ID_STACKINIT
:
1027 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
1028 ws
->l_state
.vertical_mwin
= 1;
1030 case SWM_ARG_ID_MASTERSHRINK
:
1031 if (ws
->l_state
.vertical_msize
> 1)
1032 ws
->l_state
.vertical_msize
--;
1034 case SWM_ARG_ID_MASTERGROW
:
1035 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
1036 ws
->l_state
.vertical_msize
++;
1038 case SWM_ARG_ID_MASTERADD
:
1039 ws
->l_state
.vertical_mwin
++;
1041 case SWM_ARG_ID_MASTERDEL
:
1042 if (ws
->l_state
.vertical_mwin
> 0)
1043 ws
->l_state
.vertical_mwin
--;
1051 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1053 struct swm_geometry gg
= *g
;
1054 struct ws_win
*win
, *winfocus
;
1055 int i
, j
, split
, colno
, hrh
, winno
, main_width
;
1058 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1060 if ((winno
= count_win(ws
, 0)) == 0)
1063 if (ws
->focus
== NULL
)
1064 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1065 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1067 if (ws
->l_state
.vertical_mwin
&&
1068 winno
> ws
->l_state
.vertical_mwin
) {
1069 split
= ws
->l_state
.vertical_mwin
;
1071 main_width
= (g
->w
/ SWM_V_SLICE
) *
1072 ws
->l_state
.vertical_msize
;
1082 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1083 if (split
&& i
== split
) {
1084 colno
= winno
- split
;
1085 hrh
= (g
->h
/ colno
);
1086 gg
.x
+= main_width
+ 2;
1087 gg
.w
= g
->w
- (main_width
+ 2);
1092 gg
.h
= (hrh
+ (g
->h
- (colno
* hrh
)));
1100 if (win
->transient
!= 0 || win
->floating
!= 0)
1101 stack_floater(win
, ws
->r
);
1103 bzero(&wc
, sizeof wc
);
1104 wc
.border_width
= 1;
1105 win
->g
.x
= wc
.x
= gg
.x
;
1106 win
->g
.y
= wc
.y
= gg
.y
;
1107 win
->g
.w
= wc
.width
= gg
.w
;
1108 win
->g
.h
= wc
.height
= gg
.h
;
1109 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1110 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1113 XMapRaised(display
, win
->id
);
1119 focus_win(winfocus
); /* has to be done outside of the loop */
1124 horizontal_config(struct workspace
*ws
, int id
)
1126 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
1129 case SWM_ARG_ID_STACKRESET
:
1130 case SWM_ARG_ID_STACKINIT
:
1131 ws
->l_state
.horizontal_mwin
= 1;
1132 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
1134 case SWM_ARG_ID_MASTERSHRINK
:
1135 if (ws
->l_state
.horizontal_msize
> 1)
1136 ws
->l_state
.horizontal_msize
--;
1138 case SWM_ARG_ID_MASTERGROW
:
1139 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
1140 ws
->l_state
.horizontal_msize
++;
1142 case SWM_ARG_ID_MASTERADD
:
1143 ws
->l_state
.horizontal_mwin
++;
1145 case SWM_ARG_ID_MASTERDEL
:
1146 if (ws
->l_state
.horizontal_mwin
> 0)
1147 ws
->l_state
.horizontal_mwin
--;
1155 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1157 struct swm_geometry gg
= *g
;
1158 struct ws_win
*win
, *winfocus
;
1159 int i
, j
, split
, rowno
, hrw
, winno
, main_height
;
1162 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
1164 if ((winno
= count_win(ws
, 0)) == 0)
1167 if (ws
->focus
== NULL
)
1168 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1169 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1171 if (ws
->l_state
.horizontal_mwin
&&
1172 winno
> ws
->l_state
.horizontal_mwin
) {
1173 split
= ws
->l_state
.horizontal_mwin
;
1175 main_height
= (g
->h
/ SWM_V_SLICE
) *
1176 ws
->l_state
.horizontal_msize
;
1186 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1187 if (split
&& i
== split
) {
1188 rowno
= winno
- split
;
1189 hrw
= (g
->w
/ rowno
);
1190 gg
.y
+= main_height
+ 2;
1191 gg
.h
= g
->h
- (main_height
+ 2);
1196 gg
.w
= (hrw
+ (g
->w
- (rowno
* hrw
)));
1203 if (win
->transient
!= 0 || win
->floating
!= 0)
1204 stack_floater(win
, ws
->r
);
1206 bzero(&wc
, sizeof wc
);
1207 wc
.border_width
= 1;
1208 win
->g
.x
= wc
.x
= gg
.x
;
1209 win
->g
.y
= wc
.y
= gg
.y
;
1210 win
->g
.w
= wc
.width
= gg
.w
;
1211 win
->g
.h
= wc
.height
= gg
.h
;
1212 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1213 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1216 XMapRaised(display
, win
->id
);
1222 focus_win(winfocus
); /* this has to be done outside of the loop */
1225 /* fullscreen view */
1227 max_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1229 struct swm_geometry gg
= *g
;
1230 struct ws_win
*win
, *winfocus
;
1233 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
1235 if (count_win(ws
, 0) == 0)
1238 if (ws
->focus
== NULL
)
1239 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1240 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1242 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1243 if (win
->transient
!= 0 || win
->floating
!= 0) {
1244 if (win
== ws
->focus
) {
1246 stack_floater(win
, ws
->r
);
1247 XMapRaised(display
, win
->id
);
1249 XUnmapWindow(display
, win
->id
);
1251 bzero(&wc
, sizeof wc
);
1252 wc
.border_width
= 1;
1253 win
->g
.x
= wc
.x
= gg
.x
;
1254 win
->g
.y
= wc
.y
= gg
.y
;
1255 win
->g
.w
= wc
.width
= gg
.w
;
1256 win
->g
.h
= wc
.height
= gg
.h
;
1257 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1258 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1260 if (win
== ws
->focus
) {
1261 XMapRaised(display
, win
->id
);
1263 XUnmapWindow(display
, win
->id
);
1268 focus_win(winfocus
); /* has to be done outside of the loop */
1272 send_to_ws(struct swm_region
*r
, union arg
*args
)
1274 int wsid
= args
->id
;
1275 struct ws_win
*win
= cur_focus
;
1276 struct workspace
*ws
, *nws
;
1278 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
1281 nws
= &win
->s
->ws
[wsid
];
1283 XUnmapWindow(display
, win
->id
);
1285 /* find a window to focus */
1286 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1287 if (ws
->focus
== NULL
)
1288 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1289 if (ws
->focus
== win
)
1292 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1294 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
1297 if (count_win(nws
, 1) == 1)
1305 /* key definitions */
1309 void (*func
)(struct swm_region
*r
, union arg
*);
1312 /* modifier key function argument */
1313 { MODKEY
, XK_space
, cycle_layout
, {0} },
1314 { MODKEY
| ShiftMask
, XK_space
, stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
1315 { MODKEY
, XK_h
, stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
1316 { MODKEY
, XK_l
, stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
1317 { MODKEY
, XK_comma
, stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
1318 { MODKEY
, XK_period
, stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
1319 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
1320 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1321 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1322 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
1323 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
1324 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
1325 { MODKEY
, XK_p
, spawn
, {.argv
= spawn_menu
} },
1326 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
1327 { MODKEY
, XK_q
, restart
, {0} },
1328 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
1329 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
1330 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
1331 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
1332 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
1333 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
1334 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
1335 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
1336 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
1337 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
1338 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
1339 { MODKEY
, XK_Right
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
1340 { MODKEY
, XK_Left
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
1341 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
1342 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
1343 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
1344 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
1345 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
1346 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
1347 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
1348 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
1349 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
1350 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
1351 { MODKEY
, XK_b
, bar_toggle
, {0} },
1352 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1353 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1358 enum { client_click
, root_click
};
1362 unsigned int button
;
1363 void (*func
)(struct swm_region
*r
, union arg
*);
1366 /* action key mouse button func args */
1367 { client_click
, MODKEY
, Button2
, NULL
, {0} },
1372 updatenumlockmask(void)
1375 XModifierKeymap
*modmap
;
1377 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
1379 modmap
= XGetModifierMapping(display
);
1380 for (i
= 0; i
< 8; i
++)
1381 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1382 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1383 == XKeysymToKeycode(display
, XK_Num_Lock
))
1384 numlockmask
= (1 << i
);
1386 XFreeModifiermap(modmap
);
1392 unsigned int i
, j
, k
;
1394 unsigned int modifiers
[] =
1395 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
1397 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
1398 updatenumlockmask();
1400 for (k
= 0; k
< ScreenCount(display
); k
++) {
1401 if (TAILQ_EMPTY(&screens
[k
].rl
))
1403 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
1404 for (i
= 0; i
< LENGTH(keys
); i
++) {
1405 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
1406 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1407 XGrabKey(display
, code
,
1408 keys
[i
].mod
| modifiers
[j
],
1409 screens
[k
].root
, True
,
1410 GrabModeAsync
, GrabModeAsync
);
1417 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
1425 XKeyEvent
*ev
= &e
->xkey
;
1427 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
1429 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
1430 for (i
= 0; i
< LENGTH(keys
); i
++)
1431 if (keysym
== keys
[i
].keysym
1432 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1434 keys
[i
].func(root_to_region(ev
->root
),
1439 buttonpress(XEvent
*e
)
1441 XButtonPressedEvent
*ev
= &e
->xbutton
;
1442 #ifdef SWM_CLICKTOFOCUS
1444 struct workspace
*ws
;
1445 struct swm_region
*r
;
1448 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
1450 if (ev
->window
== ev
->root
) {
1451 rootclick
= ev
->root
;
1454 if (ev
->window
== cur_focus
->id
)
1456 #ifdef SWM_CLICKTOFOCUS
1457 r
= root_to_region(ev
->root
);
1459 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
1460 if (win
->id
== ev
->window
) {
1461 /* focus in the clicked window */
1462 XSetWindowBorder(display
, ev
->window
, 0xff0000);
1463 XSetWindowBorder(display
, ws
->focus
->id
, 0x888888);
1464 XSetInputFocus(display
, ev
->window
, RevertToPointerRoot
,
1467 XSync(display
, False
);
1474 set_win_state(struct ws_win
*win
, long state
)
1476 long data
[] = {state
, None
};
1478 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
1480 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1481 (unsigned char *)data
, 2);
1485 manage_window(Window id
, struct workspace
*ws
)
1491 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1493 return (win
); /* already being managed */
1496 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
1497 errx(1, "calloc: failed to allocate memory for new window");
1501 win
->s
= ws
->r
->s
; /* this never changes */
1502 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
1504 /* make new win focused */
1507 XGetTransientForHint(display
, win
->id
, &trans
);
1509 win
->transient
= trans
;
1510 DNPRINTF(SWM_D_MISC
, "manage_window: win %u transient %u\n",
1511 (unsigned)win
->id
, win
->transient
);
1513 XGetWindowAttributes(display
, id
, &win
->wa
);
1514 win
->g
.w
= win
->wa
.width
;
1515 win
->g
.h
= win
->wa
.height
;
1516 win
->g
.x
= win
->wa
.x
;
1517 win
->g
.y
= win
->wa
.y
;
1519 /* XXX make this a table */
1520 bzero(&ch
, sizeof ch
);
1521 if (XGetClassHint(display
, win
->id
, &ch
)) {
1522 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1523 if (!strcmp(ch
.res_class
, "MPlayer") && !strcmp(ch
.res_name
, "xv")) {
1527 XFree(ch
.res_class
);
1532 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
1533 PropertyChangeMask
| StructureNotifyMask
);
1535 set_win_state(win
, NormalState
);
1541 configurerequest(XEvent
*e
)
1543 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
1548 if ((win
= find_window(ev
->window
)) == NULL
)
1552 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
1554 bzero(&wc
, sizeof wc
);
1557 wc
.width
= ev
->width
;
1558 wc
.height
= ev
->height
;
1559 wc
.border_width
= ev
->border_width
;
1560 wc
.sibling
= ev
->above
;
1561 wc
.stack_mode
= ev
->detail
;
1562 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
1564 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
1566 if (win
->floating
) {
1567 if (ev
->value_mask
& CWX
)
1569 if (ev
->value_mask
& CWY
)
1571 if (ev
->value_mask
& CWWidth
)
1572 win
->g
.w
= ev
->width
;
1573 if (ev
->value_mask
& CWHeight
)
1574 win
->g
.h
= ev
->height
;
1575 if (win
->ws
->r
!= NULL
) {
1576 /* this seems to be full screen */
1577 if (win
->g
.w
> WIDTH(win
->ws
->r
)) {
1582 if (win
->g
.h
> HEIGHT(win
->ws
->r
)) {
1588 if ((ev
->value_mask
& (CWX
|CWY
)) &&
1589 !(ev
->value_mask
& (CWWidth
|CWHeight
)))
1591 XMoveResizeWindow(display
, win
->id
,
1592 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
1599 configurenotify(XEvent
*e
)
1601 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
1602 e
->xconfigure
.window
);
1606 destroynotify(XEvent
*e
)
1609 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
1610 struct workspace
*ws
;
1612 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
1614 if ((win
= find_window(ev
->window
)) != NULL
) {
1616 /* find a window to focus */
1617 if (ws
->focus
== win
)
1618 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1619 if (ws
->focus
== NULL
)
1620 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1621 if (ws
->focus
== win
)
1623 if (cur_focus
== win
)
1624 focus_win(ws
->focus
);
1626 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1627 set_win_state(win
, WithdrawnState
);
1634 enternotify(XEvent
*e
)
1636 XCrossingEvent
*ev
= &e
->xcrossing
;
1639 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
1642 /* eat event(r) to prevent autofocus */
1647 if ((win
= find_window(ev
->window
)) != NULL
)
1654 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", e
->xfocus
.window
);
1656 /* XXX this probably needs better handling now.
1657 if (ev->window == ev->root)
1661 * kill grab for now so that we can cut and paste , this screws up
1665 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1666 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1667 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1672 mappingnotify(XEvent
*e
)
1674 XMappingEvent
*ev
= &e
->xmapping
;
1676 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
1678 XRefreshKeyboardMapping(ev
);
1679 if (ev
->request
== MappingKeyboard
)
1684 maprequest(XEvent
*e
)
1686 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1687 XWindowAttributes wa
;
1688 struct swm_region
*r
;
1690 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
1691 e
->xmaprequest
.window
);
1693 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
1695 if (wa
.override_redirect
)
1697 r
= root_to_region(wa
.root
);
1698 manage_window(e
->xmaprequest
.window
, r
->ws
);
1703 propertynotify(XEvent
*e
)
1705 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
1706 e
->xproperty
.window
);
1710 unmapnotify(XEvent
*e
)
1712 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
1716 visibilitynotify(XEvent
*e
)
1719 struct swm_region
*r
;
1721 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
1722 e
->xvisibility
.window
);
1723 if (e
->xvisibility
.state
== VisibilityUnobscured
)
1724 for (i
= 0; i
< ScreenCount(display
); i
++)
1725 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1726 if (e
->xvisibility
.window
== r
->bar_window
)
1730 void (*handler
[LASTEvent
])(XEvent
*) = {
1732 [KeyPress
] = keypress
,
1733 [ButtonPress
] = buttonpress
,
1734 [ConfigureRequest
] = configurerequest
,
1735 [ConfigureNotify
] = configurenotify
,
1736 [DestroyNotify
] = destroynotify
,
1737 [EnterNotify
] = enternotify
,
1738 [FocusIn
] = focusin
,
1739 [MappingNotify
] = mappingnotify
,
1740 [MapRequest
] = maprequest
,
1741 [PropertyNotify
] = propertynotify
,
1742 [UnmapNotify
] = unmapnotify
,
1743 [VisibilityNotify
] = visibilitynotify
,
1747 xerror_start(Display
*d
, XErrorEvent
*ee
)
1754 xerror(Display
*d
, XErrorEvent
*ee
)
1756 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1764 xerrorxlib
= XSetErrorHandler(xerror_start
);
1766 /* this causes an error if some other window manager is running */
1767 XSelectInput(display
, DefaultRootWindow(display
),
1768 SubstructureRedirectMask
);
1769 XSync(display
, False
);
1773 XSetErrorHandler(xerror
);
1774 XSync(display
, False
);
1783 unsigned char *p
= NULL
;
1784 unsigned long n
, extra
;
1787 astate
= XInternAtom(display
, "WM_STATE", False
);
1788 status
= XGetWindowProperty(display
, w
, astate
, 0L, 2L, False
, astate
,
1789 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
1790 if (status
!= Success
)
1799 new_region(struct swm_screen
*s
, struct workspace
*ws
,
1800 int x
, int y
, int w
, int h
)
1802 struct swm_region
*r
;
1804 DNPRINTF(SWM_D_MISC
, "new region on screen %d: %dx%d (%d, %d)\n",
1805 s
->idx
, x
, y
, w
, h
);
1807 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
1808 errx(1, "calloc: failed to allocate memory for screen");
1817 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
1824 #ifdef SWM_XRR_HAS_CRTC
1826 XRRScreenResources
*sr
;
1828 #endif /* SWM_XRR_HAS_CRTC */
1829 Window d1
, d2
, *wins
= NULL
;
1830 XWindowAttributes wa
;
1831 struct swm_region
*r
;
1833 int errorbase
, major
, minor
;
1834 int ncrtc
= 0, w
= 0;
1836 struct workspace
*ws
;
1838 if ((screens
= calloc(ScreenCount(display
),
1839 sizeof(struct swm_screen
))) == NULL
)
1840 errx(1, "calloc: screens");
1842 /* map physical screens */
1843 for (i
= 0; i
< ScreenCount(display
); i
++) {
1844 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
1846 TAILQ_INIT(&screens
[i
].rl
);
1847 screens
[i
].root
= RootWindow(display
, i
);
1848 XGetWindowAttributes(display
, screens
[i
].root
, &wa
);
1849 XSelectInput(display
, screens
[i
].root
,
1850 EnterWindowMask
| ButtonPressMask
| wa
.your_event_mask
);
1852 /* set default colors */
1853 screens
[i
].color_focus
= name_to_color("red");
1854 screens
[i
].color_unfocus
= name_to_color("rgb:88/88/88");
1855 screens
[i
].bar_border
= name_to_color("rgb:00/80/80");
1856 screens
[i
].bar_color
= name_to_color("black");
1857 screens
[i
].bar_font_color
= name_to_color("rgb:a0/a0/a0");
1859 /* init all workspaces */
1860 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
1861 ws
= &screens
[i
].ws
[j
];
1866 TAILQ_INIT(&ws
->winlist
);
1868 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
1869 if (layouts
[k
].l_config
!= NULL
)
1870 layouts
[k
].l_config(ws
,
1871 SWM_ARG_ID_STACKINIT
);
1872 ws
->cur_layout
= &layouts
[0];
1875 /* map virtual screens onto physical screens */
1876 screens
[i
].xrandr_support
= XRRQueryExtension(display
,
1877 &xrandr_eventbase
, &errorbase
);
1878 if (screens
[i
].xrandr_support
)
1879 if (XRRQueryVersion(display
, &major
, &minor
) &&
1881 screens
[i
].xrandr_support
= 0;
1883 #if 0 /* not ready for dynamic screen changes */
1884 if (screens
[i
].xrandr_support
)
1885 XRRSelectInput(display
,
1887 RRScreenChangeNotifyMask
);
1890 /* grab existing windows (before we build the bars)*/
1891 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
1894 #ifdef SWM_XRR_HAS_CRTC
1895 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
1897 new_region(&screens
[i
], &screens
[i
].ws
[w
],
1898 0, 0, DisplayWidth(display
, i
),
1899 DisplayHeight(display
, i
));
1903 for (c
= 0; c
< ncrtc
; c
++) {
1904 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
1905 if (ci
->noutput
== 0)
1908 if (ci
!= NULL
&& ci
->mode
== None
)
1909 new_region(&screens
[i
], &screens
[i
].ws
[w
], 0, 0,
1910 DisplayWidth(display
, i
),
1911 DisplayHeight(display
, i
));
1913 new_region(&screens
[i
], &screens
[i
].ws
[w
],
1914 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
1917 XRRFreeCrtcInfo(ci
);
1918 XRRFreeScreenResources(sr
);
1920 new_region(&screens
[i
], &screens
[i
].ws
[w
], 0, 0,
1921 DisplayWidth(display
, i
),
1922 DisplayHeight(display
, i
));
1923 #endif /* SWM_XRR_HAS_CRTC */
1925 /* attach windows to a region */
1926 /* normal windows */
1927 if ((r
= TAILQ_FIRST(&screens
[i
].rl
)) == NULL
)
1928 errx(1, "no regions on screen %d", i
);
1930 for (i
= 0; i
< no
; i
++) {
1931 XGetWindowAttributes(display
, wins
[i
], &wa
);
1932 if (!XGetWindowAttributes(display
, wins
[i
], &wa
) ||
1933 wa
.override_redirect
||
1934 XGetTransientForHint(display
, wins
[i
], &d1
))
1937 if (wa
.map_state
== IsViewable
||
1938 getstate(wins
[i
]) == NormalState
)
1939 manage_window(wins
[i
], r
->ws
);
1941 /* transient windows */
1942 for (i
= 0; i
< no
; i
++) {
1943 if (!XGetWindowAttributes(display
, wins
[i
], &wa
))
1946 if (XGetTransientForHint(display
, wins
[i
], &d1
) &&
1947 (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) ==
1949 manage_window(wins
[i
], r
->ws
);
1959 main(int argc
, char *argv
[])
1962 char conf
[PATH_MAX
], *cfile
= NULL
;
1969 fprintf(stderr
, "Welcome to scrotwm V%s\n", SWM_VERSION
);
1970 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1971 warnx("no locale support");
1973 if (!(display
= XOpenDisplay(0)))
1974 errx(1, "can not open display");
1977 errx(1, "other wm running");
1979 astate
= XInternAtom(display
, "WM_STATE", False
);
1981 /* look for local and global conf file */
1982 pwd
= getpwuid(getuid());
1984 errx(1, "invalid user %d", getuid());
1988 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
1989 if (stat(conf
, &sb
) != -1) {
1990 if (S_ISREG(sb
.st_mode
))
1993 /* try global conf file */
1994 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
1995 if (!stat(conf
, &sb
))
1996 if (S_ISREG(sb
.st_mode
))
2003 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2008 xfd
= ConnectionNumber(display
);
2011 if (select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
2013 errx(1, "select failed");
2018 while(XPending(display
)) {
2019 XNextEvent(display
, &e
);
2020 if (handler
[e
.type
])
2021 handler
[e
.type
](&e
);
2025 XCloseDisplay(display
);