]>
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
93 /* #define SWM_DEBUG */
95 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
96 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
97 #define SWM_D_MISC 0x0001
98 #define SWM_D_EVENT 0x0002
99 #define SWM_D_WS 0x0004
100 #define SWM_D_FOCUS 0x0008
101 #define SWM_D_MOVE 0x0010
102 #define SWM_D_STACK 0x0020
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))
121 #define X(r) (r)->g.x
122 #define Y(r) (r)->g.y
123 #define WIDTH(r) (r)->g.w
124 #define HEIGHT(r) (r)->g.h
128 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
131 int xrandr_eventbase
;
132 int ignore_enter
= 0;
133 unsigned int numlockmask
= 0;
137 int cycle_visible
= 0;
140 double dialog_ratio
= .6;
142 #define SWM_BAR_MAX (128)
143 sig_atomic_t bar_alarm
= 0;
150 char *bar_fonts
[] = {
151 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
152 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
156 /* terminal + args */
157 char *spawn_term
[] = { "xterm", NULL
};
158 char *spawn_menu
[] = { "dmenu_run", NULL
};
160 /* layout manager data */
161 struct swm_geometry
{
171 /* virtual "screens" */
173 TAILQ_ENTRY(swm_region
) entry
;
174 struct swm_geometry g
;
176 struct workspace
*ws
; /* current workspace on this region */
177 struct swm_screen
*s
; /* screen idx */
179 TAILQ_HEAD(swm_region_list
, swm_region
);
183 TAILQ_ENTRY(ws_win
) entry
;
185 struct swm_geometry g
;
189 struct workspace
*ws
; /* always valid */
190 struct swm_screen
*s
; /* always valid, never changes */
191 XWindowAttributes wa
;
193 TAILQ_HEAD(ws_win_list
, ws_win
);
195 /* layout handlers */
197 void vertical_config(struct workspace
*, int);
198 void vertical_stack(struct workspace
*, struct swm_geometry
*);
199 void horizontal_config(struct workspace
*, int);
200 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
201 void max_stack(struct workspace
*, struct swm_geometry
*);
204 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
205 void (*l_config
)(struct workspace
*, int);
207 /* stack, configure */
208 { vertical_stack
, vertical_config
},
209 { horizontal_stack
, horizontal_config
},
214 #define SWM_H_SLICE (32)
215 #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
;
235 /* physical screen mapping */
236 #define SWM_WS_MAX (10) /* XXX Too small? */
238 int idx
; /* screen index */
239 struct swm_region_list rl
; /* list of regions on this screen */
242 struct workspace ws
[SWM_WS_MAX
];
245 unsigned long bar_border
;
246 unsigned long bar_color
;
247 unsigned long bar_font_color
;
248 unsigned long color_focus
; /* XXX should this be per ws? */
249 unsigned long color_unfocus
;
251 struct swm_screen
*screens
;
253 Window rootclick
= 0;
255 struct ws_win
*cur_focus
= NULL
;
257 /* args to functions */
260 #define SWM_ARG_ID_FOCUSNEXT (0)
261 #define SWM_ARG_ID_FOCUSPREV (1)
262 #define SWM_ARG_ID_FOCUSMAIN (2)
263 #define SWM_ARG_ID_SWAPNEXT (3)
264 #define SWM_ARG_ID_SWAPPREV (4)
265 #define SWM_ARG_ID_SWAPMAIN (5)
266 #define SWM_ARG_ID_MASTERSHRINK (6)
267 #define SWM_ARG_ID_MASTERGROW (7)
268 #define SWM_ARG_ID_MASTERADD (8)
269 #define SWM_ARG_ID_MASTERDEL (9)
270 #define SWM_ARG_ID_STACKRESET (10)
271 #define SWM_ARG_ID_STACKINIT (11)
272 #define SWM_ARG_ID_CYCLEWS_UP (12)
273 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
278 name_to_color(char *colorname
)
282 XColor screen_def
, exact_def
;
283 unsigned long result
= 0;
284 char cname
[32] = "#";
286 cmap
= DefaultColormap(display
, screens
[0].idx
);
287 status
= XAllocNamedColor(display
, cmap
, colorname
,
288 &screen_def
, &exact_def
);
290 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
291 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
295 result
= screen_def
.pixel
;
297 fprintf(stderr
, "color '%s' not found.\n", colorname
);
303 varmatch(char *var
, char *name
, int *index
)
308 i
= strncmp(var
, name
, 255);
315 p
= var
+ strlen(name
);
318 bzero(buf
, sizeof buf
);
320 while (isdigit(*p
) && i
< sizeof buf
)
322 if (i
== 0 || i
>= sizeof buf
|| *p
!= ']')
324 *index
= strtonum(buf
, 0, 99, NULL
);
328 /* conf file stuff */
329 #define SWM_CONF_WS "\n= \t"
330 #define SWM_CONF_FILE "scrotwm.conf"
332 conf_load(char *filename
)
335 char *line
, *cp
, *var
, *val
;
336 size_t len
, lineno
= 0;
339 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
341 if (filename
== NULL
)
344 if ((config
= fopen(filename
, "r")) == NULL
)
347 for (sc
= ScreenCount(display
);;) {
348 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
352 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
358 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
360 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
361 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
364 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
367 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
368 bar_enabled
= atoi(val
);
369 else if (!varmatch(var
, "bar_border", &i
))
370 if (i
> 0 && i
<= sc
)
371 screens
[i
- 1].bar_border
=
374 for (i
= 0; i
< sc
; i
++)
375 screens
[i
].bar_border
=
379 else if (!varmatch(var
, "bar_color", &i
))
380 if (i
> 0 && i
<= sc
)
381 screens
[i
- 1].bar_color
=
384 for (i
= 0; i
< sc
; i
++)
385 screens
[i
].bar_color
=
389 else if (!varmatch(var
, "bar_font_color", &i
))
390 if (i
> 0 && i
<= sc
)
391 screens
[i
- 1].bar_font_color
=
394 for (i
= 0; i
< sc
; i
++)
395 screens
[i
].bar_font_color
=
400 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
401 asprintf(&bar_fonts
[0], "%s", val
);
407 if (!varmatch(var
, "color_focus", &i
))
408 if (i
> 0 && i
<= sc
)
409 screens
[i
- 1].color_focus
=
412 for (i
= 0; i
< sc
; i
++)
413 screens
[i
].color_focus
=
417 else if (!varmatch(var
, "color_unfocus", &i
))
418 if (i
> 0 && i
<= sc
)
419 screens
[i
- 1].color_unfocus
=
422 for (i
= 0; i
< sc
; i
++)
423 screens
[i
].color_unfocus
=
427 else if (!strncmp(var
, "cycle_empty", strlen("cycle_empty")))
428 cycle_visible
= atoi(val
);
429 else if (!strncmp(var
, "cycle_visible", strlen("cycle_visible")))
430 cycle_visible
= atoi(val
);
436 if (!strncmp(var
, "dialog_ratio",
437 strlen("dialog_ratio"))) {
438 dialog_ratio
= atof(val
);
439 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
446 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
447 asprintf(&spawn_term
[0], "%s", val
); /* XXX args? */
458 errx(1, "invalid conf file entry: %s=%s", var
, val
);
460 errx(1, "invalid screen index: %s out of bounds", var
);
464 bar_print(struct swm_region
*r
, char *s
)
466 XClearWindow(display
, r
->bar_window
);
467 XSetForeground(display
, bar_gc
, r
->s
->bar_font_color
);
468 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
, s
,
477 struct swm_region
*r
;
482 if (bar_enabled
== 0)
486 localtime_r(&tmt
, &tm
);
487 strftime(s
, sizeof s
, "%a %b %d %R %Z %Y", &tm
);
488 for (i
= 0; i
< ScreenCount(display
); i
++) {
490 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
491 snprintf(e
, sizeof e
, "%s %d:%d",
492 s
, x
++, r
->ws
->idx
+ 1);
496 XSync(display
, False
);
507 bar_toggle(struct swm_region
*r
, union arg
*args
)
509 struct swm_region
*tmpr
;
510 int i
, j
, sc
= ScreenCount(display
);
512 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
515 for (i
= 0; i
< sc
; i
++)
516 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
517 XUnmapWindow(display
, tmpr
->bar_window
);
519 for (i
= 0; i
< sc
; i
++)
520 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
521 XMapRaised(display
, tmpr
->bar_window
);
523 bar_enabled
= !bar_enabled
;
524 XSync(display
, False
);
525 for (i
= 0; i
< sc
; i
++)
526 for (j
= 0; j
< SWM_WS_MAX
; j
++)
527 screens
[i
].ws
[j
].restack
= 1;
530 /* must be after stack */
531 for (i
= 0; i
< sc
; i
++)
532 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
537 bar_setup(struct swm_region
*r
)
541 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
542 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
546 if (bar_fonts
[i
] == NULL
)
547 errx(1, "couldn't load font");
548 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
550 r
->bar_window
= XCreateSimpleWindow(display
,
551 r
->s
->root
, X(r
), Y(r
), WIDTH(r
) - 2, bar_height
- 2,
552 1, r
->s
->bar_border
, r
->s
->bar_color
);
553 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
554 XSetFont(display
, bar_gc
, bar_fs
->fid
);
555 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
557 XMapRaised(display
, r
->bar_window
);
558 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
560 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
561 err(1, "could not install bar_signal");
568 XSetWindowAttributes wa
;
569 struct swm_region
*r
;
572 bzero(&wa
, sizeof wa
);
573 for (i
= 0; i
< ScreenCount(display
); i
++)
574 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
575 wa
.border_pixel
= screens
[i
].bar_border
;
576 wa
.background_pixel
= screens
[i
].bar_color
;
577 XChangeWindowAttributes(display
, r
->bar_window
,
578 CWBackPixel
| CWBorderPixel
, &wa
);
584 config_win(struct ws_win
*win
)
588 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
589 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
590 ce
.type
= ConfigureNotify
;
591 ce
.display
= display
;
597 ce
.height
= win
->g
.h
;
598 ce
.border_width
= 1; /* XXX store this! */
600 ce
.override_redirect
= False
;
601 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
605 count_win(struct workspace
*ws
, int count_transient
)
610 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
611 if (count_transient
== 0 && win
->floating
)
613 if (count_transient
== 0 && win
->transient
)
617 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
623 quit(struct swm_region
*r
, union arg
*args
)
625 DNPRINTF(SWM_D_MISC
, "quit\n");
630 restart(struct swm_region
*r
, union arg
*args
)
632 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
634 /* disable alarm because the following code may not be interrupted */
636 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
637 errx(1, "can't disable alarm");
639 XCloseDisplay(display
);
640 execvp(start_argv
[0], start_argv
);
641 fprintf(stderr
, "execvp failed\n");
647 root_to_region(Window root
)
649 struct swm_region
*r
;
654 for (i
= 0; i
< ScreenCount(display
); i
++)
655 if (screens
[i
].root
== root
)
658 if (rootclick
!= root
&& /* if root was just clicked in, use cursor */
659 cur_focus
&& cur_focus
->ws
->r
&& cur_focus
->s
== &screens
[i
])
660 r
= cur_focus
->ws
->r
;
662 if (XQueryPointer(display
, screens
[i
].root
,
663 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) == False
) {
664 r
= TAILQ_FIRST(&screens
[i
].rl
);
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
);
680 find_window(Window id
)
685 for (i
= 0; i
< ScreenCount(display
); i
++)
686 for (j
= 0; j
< SWM_WS_MAX
; j
++)
687 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
694 spawn(struct swm_region
*r
, union arg
*args
)
696 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
698 * The double-fork construct avoids zombie processes and keeps the code
699 * clean from stupid signal handlers.
704 close(ConnectionNumber(display
));
706 execvp(args
->argv
[0], args
->argv
);
707 fprintf(stderr
, "execvp failed\n");
721 DNPRINTF(SWM_D_FOCUS
, "unfocus_all:\n");
723 for (i
= 0; i
< ScreenCount(display
); i
++)
724 for (j
= 0; j
< SWM_WS_MAX
; j
++)
725 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
726 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);
746 win
->ws
->focus
= win
;
747 if (win
->ws
->r
!= NULL
) {
750 XSetWindowBorder(display
, win
->id
,
751 win
->ws
->r
->s
->color_focus
);
753 XSetInputFocus(display
, win
->id
,
754 RevertToPointerRoot
, CurrentTime
);
759 switchws(struct swm_region
*r
, union arg
*args
)
762 struct swm_region
*this_r
, *other_r
;
764 struct workspace
*new_ws
, *old_ws
;
768 new_ws
= &this_r
->s
->ws
[wsid
];
770 DNPRINTF(SWM_D_WS
, "switchws screen %d region %dx%d+%d+%d: "
771 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
774 if (new_ws
== old_ws
)
779 /* if the other workspace is hidden, switch windows */
780 /* map new window first to prevent ugly blinking */
781 TAILQ_FOREACH(win
, &new_ws
->winlist
, entry
)
782 XMapRaised(display
, win
->id
);
784 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
785 XUnmapWindow(display
, win
->id
);
790 other_r
->ws
= old_ws
;
799 focus_win(new_ws
->focus
);
805 cyclews(struct swm_region
*r
, union arg
*args
)
808 struct swm_screen
*s
= r
->s
;
810 DNPRINTF(SWM_D_WS
, "cyclews id %d "
811 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
812 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
818 case SWM_ARG_ID_CYCLEWS_UP
:
819 if (a
.id
< SWM_WS_MAX
- 1)
824 case SWM_ARG_ID_CYCLEWS_DOWN
:
828 a
.id
= SWM_WS_MAX
- 1;
834 if (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
))
836 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
840 } while (a
.id
!= r
->ws
->idx
);
844 swapwin(struct swm_region
*r
, union arg
*args
)
846 struct ws_win
*target
;
847 struct ws_win_list
*wl
;
850 DNPRINTF(SWM_D_WS
, "swapwin id %d "
851 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
852 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
853 if (cur_focus
== NULL
)
856 wl
= &cur_focus
->ws
->winlist
;
859 case SWM_ARG_ID_SWAPPREV
:
860 target
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
861 TAILQ_REMOVE(wl
, cur_focus
, entry
);
863 TAILQ_INSERT_TAIL(wl
, cur_focus
, entry
);
865 TAILQ_INSERT_BEFORE(target
, cur_focus
, entry
);
867 case SWM_ARG_ID_SWAPNEXT
:
868 target
= TAILQ_NEXT(cur_focus
, entry
);
869 TAILQ_REMOVE(wl
, cur_focus
, entry
);
871 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
873 TAILQ_INSERT_AFTER(wl
, target
, cur_focus
, entry
);
875 case SWM_ARG_ID_SWAPMAIN
:
876 target
= TAILQ_FIRST(wl
);
877 if (target
== cur_focus
)
879 TAILQ_REMOVE(wl
, target
, entry
);
880 TAILQ_INSERT_BEFORE(cur_focus
, target
, entry
);
881 TAILQ_REMOVE(wl
, cur_focus
, entry
);
882 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
885 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
894 focus(struct swm_region
*r
, union arg
*args
)
896 struct ws_win
*winfocus
, *winlostfocus
;
897 struct ws_win_list
*wl
;
899 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
900 if (cur_focus
== NULL
)
903 wl
= &cur_focus
->ws
->winlist
;
905 winlostfocus
= cur_focus
;
908 case SWM_ARG_ID_FOCUSPREV
:
909 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
910 if (winfocus
== NULL
)
911 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
914 case SWM_ARG_ID_FOCUSNEXT
:
915 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
916 if (winfocus
== NULL
)
917 winfocus
= TAILQ_FIRST(wl
);
920 case SWM_ARG_ID_FOCUSMAIN
:
921 winfocus
= TAILQ_FIRST(wl
);
928 if (winfocus
== winlostfocus
)
932 XSync(display
, False
);
936 cycle_layout(struct swm_region
*r
, union arg
*args
)
938 struct workspace
*ws
= r
->ws
;
940 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
943 if (ws
->cur_layout
->l_stack
== NULL
)
944 ws
->cur_layout
= &layouts
[0];
951 stack_config(struct swm_region
*r
, union arg
*args
)
953 struct workspace
*ws
= r
->ws
;
955 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
958 if (ws
->cur_layout
->l_config
!= NULL
)
959 ws
->cur_layout
->l_config(ws
, args
->id
);
961 if (args
->id
!= SWM_ARG_ID_STACKINIT
);
967 struct swm_geometry g
;
968 struct swm_region
*r
;
971 DNPRINTF(SWM_D_STACK
, "stack\n");
973 for (i
= 0; i
< ScreenCount(display
); i
++) {
975 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
976 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
977 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
979 /* start with screen geometry, adjust for bar */
989 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
992 XSync(display
, False
);
996 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
1001 bzero(&wc
, sizeof wc
);
1002 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
1003 wc
.border_width
= 1;
1004 if (win
->transient
) {
1005 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
1006 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
1008 wc
.width
= win
->g
.w
;
1009 wc
.height
= win
->g
.h
;
1010 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1011 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1013 DNPRINTF(SWM_D_STACK
, "stack_floater: win %lu x %d y %d w %d h %d\n",
1014 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1016 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1021 vertical_config(struct workspace
*ws
, int id
)
1023 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
1026 case SWM_ARG_ID_STACKRESET
:
1027 case SWM_ARG_ID_STACKINIT
:
1028 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
1029 ws
->l_state
.vertical_mwin
= 1;
1031 case SWM_ARG_ID_MASTERSHRINK
:
1032 if (ws
->l_state
.vertical_msize
> 1)
1033 ws
->l_state
.vertical_msize
--;
1035 case SWM_ARG_ID_MASTERGROW
:
1036 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
1037 ws
->l_state
.vertical_msize
++;
1039 case SWM_ARG_ID_MASTERADD
:
1040 ws
->l_state
.vertical_mwin
++;
1042 case SWM_ARG_ID_MASTERDEL
:
1043 if (ws
->l_state
.vertical_mwin
> 0)
1044 ws
->l_state
.vertical_mwin
--;
1052 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1054 struct swm_geometry gg
= *g
;
1055 struct ws_win
*win
, *winfocus
;
1056 int i
, j
, split
, colno
, hrh
, winno
, main_width
;
1059 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1061 if ((winno
= count_win(ws
, 0)) == 0)
1064 if (ws
->focus
== NULL
)
1065 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1066 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1068 if (ws
->l_state
.vertical_mwin
&&
1069 winno
> ws
->l_state
.vertical_mwin
) {
1070 split
= ws
->l_state
.vertical_mwin
;
1072 main_width
= (g
->w
/ SWM_V_SLICE
) *
1073 ws
->l_state
.vertical_msize
;
1083 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1084 if (split
&& i
== split
) {
1085 colno
= winno
- split
;
1086 hrh
= (g
->h
/ colno
);
1087 gg
.x
+= main_width
+ 2;
1088 gg
.w
= g
->w
- (main_width
+ 2);
1093 gg
.h
= (hrh
+ (g
->h
- (colno
* hrh
)));
1101 if (win
->transient
!= 0 || win
->floating
!= 0)
1102 stack_floater(win
, ws
->r
);
1104 bzero(&wc
, sizeof wc
);
1105 wc
.border_width
= 1;
1106 win
->g
.x
= wc
.x
= gg
.x
;
1107 win
->g
.y
= wc
.y
= gg
.y
;
1108 win
->g
.w
= wc
.width
= gg
.w
;
1109 win
->g
.h
= wc
.height
= gg
.h
;
1110 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1111 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1114 XMapRaised(display
, win
->id
);
1120 focus_win(winfocus
); /* has to be done outside of the loop */
1125 horizontal_config(struct workspace
*ws
, int id
)
1127 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
1130 case SWM_ARG_ID_STACKRESET
:
1131 case SWM_ARG_ID_STACKINIT
:
1132 ws
->l_state
.horizontal_mwin
= 1;
1133 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
1135 case SWM_ARG_ID_MASTERSHRINK
:
1136 if (ws
->l_state
.horizontal_msize
> 1)
1137 ws
->l_state
.horizontal_msize
--;
1139 case SWM_ARG_ID_MASTERGROW
:
1140 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
1141 ws
->l_state
.horizontal_msize
++;
1143 case SWM_ARG_ID_MASTERADD
:
1144 ws
->l_state
.horizontal_mwin
++;
1146 case SWM_ARG_ID_MASTERDEL
:
1147 if (ws
->l_state
.horizontal_mwin
> 0)
1148 ws
->l_state
.horizontal_mwin
--;
1156 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1158 struct swm_geometry gg
= *g
;
1159 struct ws_win
*win
, *winfocus
;
1160 int i
, j
, split
, rowno
, hrw
, winno
, main_height
;
1163 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
1165 if ((winno
= count_win(ws
, 0)) == 0)
1168 if (ws
->focus
== NULL
)
1169 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1170 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1172 if (ws
->l_state
.horizontal_mwin
&&
1173 winno
> ws
->l_state
.horizontal_mwin
) {
1174 split
= ws
->l_state
.horizontal_mwin
;
1176 main_height
= (g
->h
/ SWM_V_SLICE
) *
1177 ws
->l_state
.horizontal_msize
;
1187 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1188 if (split
&& i
== split
) {
1189 rowno
= winno
- split
;
1190 hrw
= (g
->w
/ rowno
);
1191 gg
.y
+= main_height
+ 2;
1192 gg
.h
= g
->h
- (main_height
+ 2);
1197 gg
.w
= (hrw
+ (g
->w
- (rowno
* hrw
)));
1204 if (win
->transient
!= 0 || win
->floating
!= 0)
1205 stack_floater(win
, ws
->r
);
1207 bzero(&wc
, sizeof wc
);
1208 wc
.border_width
= 1;
1209 win
->g
.x
= wc
.x
= gg
.x
;
1210 win
->g
.y
= wc
.y
= gg
.y
;
1211 win
->g
.w
= wc
.width
= gg
.w
;
1212 win
->g
.h
= wc
.height
= gg
.h
;
1213 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1214 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1217 XMapRaised(display
, win
->id
);
1223 focus_win(winfocus
); /* this has to be done outside of the loop */
1226 /* fullscreen view */
1228 max_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1230 struct swm_geometry gg
= *g
;
1231 struct ws_win
*win
, *winfocus
;
1234 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
1236 if (count_win(ws
, 0) == 0)
1239 if (ws
->focus
== NULL
)
1240 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1241 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1243 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1244 if (win
->transient
!= 0 || win
->floating
!= 0) {
1245 if (win
== ws
->focus
) {
1247 stack_floater(win
, ws
->r
);
1248 XMapRaised(display
, win
->id
);
1250 XUnmapWindow(display
, win
->id
);
1252 bzero(&wc
, sizeof wc
);
1253 wc
.border_width
= 1;
1254 win
->g
.x
= wc
.x
= gg
.x
;
1255 win
->g
.y
= wc
.y
= gg
.y
;
1256 win
->g
.w
= wc
.width
= gg
.w
;
1257 win
->g
.h
= wc
.height
= gg
.h
;
1258 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1259 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1261 if (win
== ws
->focus
) {
1262 XMapRaised(display
, win
->id
);
1264 XUnmapWindow(display
, win
->id
);
1269 focus_win(winfocus
); /* has to be done outside of the loop */
1273 send_to_ws(struct swm_region
*r
, union arg
*args
)
1275 int wsid
= args
->id
;
1276 struct ws_win
*win
= cur_focus
;
1277 struct workspace
*ws
, *nws
;
1279 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
1282 nws
= &win
->s
->ws
[wsid
];
1284 XUnmapWindow(display
, win
->id
);
1286 /* find a window to focus */
1287 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1288 if (ws
->focus
== NULL
)
1289 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1290 if (ws
->focus
== win
)
1293 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1295 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
1298 if (count_win(nws
, 1) == 1)
1306 /* key definitions */
1310 void (*func
)(struct swm_region
*r
, union arg
*);
1313 /* modifier key function argument */
1314 { MODKEY
, XK_space
, cycle_layout
, {0} },
1315 { MODKEY
| ShiftMask
, XK_space
, stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
1316 { MODKEY
, XK_h
, stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
1317 { MODKEY
, XK_l
, stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
1318 { MODKEY
, XK_comma
, stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
1319 { MODKEY
, XK_period
, stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
1320 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
1321 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1322 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1323 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
1324 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
1325 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
1326 { MODKEY
, XK_p
, spawn
, {.argv
= spawn_menu
} },
1327 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
1328 { MODKEY
, XK_q
, restart
, {0} },
1329 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
1330 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
1331 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
1332 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
1333 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
1334 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
1335 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
1336 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
1337 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
1338 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
1339 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
1340 { MODKEY
, XK_Right
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
1341 { MODKEY
, XK_Left
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
1342 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
1343 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
1344 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
1345 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
1346 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
1347 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
1348 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
1349 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
1350 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
1351 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
1352 { MODKEY
, XK_b
, bar_toggle
, {0} },
1353 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1354 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1358 updatenumlockmask(void)
1361 XModifierKeymap
*modmap
;
1363 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
1365 modmap
= XGetModifierMapping(display
);
1366 for (i
= 0; i
< 8; i
++)
1367 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1368 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1369 == XKeysymToKeycode(display
, XK_Num_Lock
))
1370 numlockmask
= (1 << i
);
1372 XFreeModifiermap(modmap
);
1378 unsigned int i
, j
, k
;
1380 unsigned int modifiers
[] =
1381 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
1383 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
1384 updatenumlockmask();
1386 for (k
= 0; k
< ScreenCount(display
); k
++) {
1387 if (TAILQ_EMPTY(&screens
[k
].rl
))
1389 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
1390 for (i
= 0; i
< LENGTH(keys
); i
++) {
1391 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
1392 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1393 XGrabKey(display
, code
,
1394 keys
[i
].mod
| modifiers
[j
],
1395 screens
[k
].root
, True
,
1396 GrabModeAsync
, GrabModeAsync
);
1403 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
1411 XKeyEvent
*ev
= &e
->xkey
;
1413 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
1415 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
1416 for (i
= 0; i
< LENGTH(keys
); i
++)
1417 if (keysym
== keys
[i
].keysym
1418 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1420 keys
[i
].func(root_to_region(ev
->root
),
1425 buttonpress(XEvent
*e
)
1427 XButtonPressedEvent
*ev
= &e
->xbutton
;
1428 #ifdef SWM_CLICKTOFOCUS
1430 struct workspace
*ws
;
1431 struct swm_region
*r
;
1434 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
1436 if (ev
->window
== ev
->root
) {
1437 rootclick
= ev
->root
;
1440 if (ev
->window
== cur_focus
->id
)
1442 #ifdef SWM_CLICKTOFOCUS
1443 r
= root_to_region(ev
->root
);
1445 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
1446 if (win
->id
== ev
->window
) {
1447 /* focus in the clicked window */
1448 XSetWindowBorder(display
, ev
->window
, 0xff0000);
1449 XSetWindowBorder(display
, ws
->focus
->id
, 0x888888);
1450 XSetInputFocus(display
, ev
->window
, RevertToPointerRoot
,
1453 XSync(display
, False
);
1460 set_win_state(struct ws_win
*win
, long state
)
1462 long data
[] = {state
, None
};
1464 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
1466 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1467 (unsigned char *)data
, 2);
1471 manage_window(Window id
, struct workspace
*ws
)
1477 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1479 return (win
); /* already being managed */
1482 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
1483 errx(1, "calloc: failed to allocate memory for new window");
1487 win
->s
= ws
->r
->s
; /* this never changes */
1488 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
1490 /* make new win focused */
1493 XGetTransientForHint(display
, win
->id
, &trans
);
1495 win
->transient
= trans
;
1496 DNPRINTF(SWM_D_MISC
, "manage_window: win %u transient %u\n",
1497 (unsigned)win
->id
, win
->transient
);
1499 XGetWindowAttributes(display
, id
, &win
->wa
);
1500 win
->g
.w
= win
->wa
.width
;
1501 win
->g
.h
= win
->wa
.height
;
1502 win
->g
.x
= win
->wa
.x
;
1503 win
->g
.y
= win
->wa
.y
;
1505 /* XXX make this a table */
1506 bzero(&ch
, sizeof ch
);
1507 if (XGetClassHint(display
, win
->id
, &ch
)) {
1508 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1509 if (!strcmp(ch
.res_class
, "MPlayer") && !strcmp(ch
.res_name
, "xv")) {
1513 XFree(ch
.res_class
);
1518 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
1519 PropertyChangeMask
| StructureNotifyMask
);
1521 set_win_state(win
, NormalState
);
1527 configurerequest(XEvent
*e
)
1529 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
1534 if ((win
= find_window(ev
->window
)) == NULL
)
1538 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
1540 bzero(&wc
, sizeof wc
);
1543 wc
.width
= ev
->width
;
1544 wc
.height
= ev
->height
;
1545 wc
.border_width
= ev
->border_width
;
1546 wc
.sibling
= ev
->above
;
1547 wc
.stack_mode
= ev
->detail
;
1548 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
1550 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
1552 if (win
->floating
) {
1553 if (ev
->value_mask
& CWX
)
1555 if (ev
->value_mask
& CWY
)
1557 if (ev
->value_mask
& CWWidth
)
1558 win
->g
.w
= ev
->width
;
1559 if (ev
->value_mask
& CWHeight
)
1560 win
->g
.h
= ev
->height
;
1561 if (win
->ws
->r
!= NULL
) {
1562 /* this seems to be full screen */
1563 if (win
->g
.w
> WIDTH(win
->ws
->r
)) {
1568 if (win
->g
.h
> HEIGHT(win
->ws
->r
)) {
1574 if ((ev
->value_mask
& (CWX
|CWY
)) &&
1575 !(ev
->value_mask
& (CWWidth
|CWHeight
)))
1577 XMoveResizeWindow(display
, win
->id
,
1578 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
1585 configurenotify(XEvent
*e
)
1587 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
1588 e
->xconfigure
.window
);
1592 destroynotify(XEvent
*e
)
1595 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
1596 struct workspace
*ws
;
1598 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
1600 if ((win
= find_window(ev
->window
)) != NULL
) {
1602 /* find a window to focus */
1603 if (ws
->focus
== win
)
1604 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1605 if (ws
->focus
== NULL
)
1606 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1607 if (ws
->focus
== win
)
1609 if (cur_focus
== win
)
1610 focus_win(ws
->focus
);
1612 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1613 set_win_state(win
, WithdrawnState
);
1620 enternotify(XEvent
*e
)
1622 XCrossingEvent
*ev
= &e
->xcrossing
;
1626 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
1628 if ((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) &&
1629 ev
->window
!= ev
->root
)
1632 /* eat event(r) to prevent autofocus */
1636 /* brute force for now */
1637 for (i
= 0; i
< ScreenCount(display
); i
++) {
1638 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
1639 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1640 if (win
->id
== ev
->window
)
1650 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", e
->xfocus
.window
);
1652 /* XXX this probably needs better handling now.
1653 if (ev->window == ev->root)
1657 * kill grab for now so that we can cut and paste , this screws up
1661 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1662 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1663 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1668 mappingnotify(XEvent
*e
)
1670 XMappingEvent
*ev
= &e
->xmapping
;
1672 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
1674 XRefreshKeyboardMapping(ev
);
1675 if (ev
->request
== MappingKeyboard
)
1680 maprequest(XEvent
*e
)
1682 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1683 XWindowAttributes wa
;
1684 struct swm_region
*r
;
1686 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
1687 e
->xmaprequest
.window
);
1689 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
1691 if (wa
.override_redirect
)
1693 r
= root_to_region(wa
.root
);
1694 manage_window(e
->xmaprequest
.window
, r
->ws
);
1699 propertynotify(XEvent
*e
)
1701 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
1702 e
->xproperty
.window
);
1706 unmapnotify(XEvent
*e
)
1708 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
1712 visibilitynotify(XEvent
*e
)
1715 struct swm_region
*r
;
1717 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
1718 e
->xvisibility
.window
);
1719 if (e
->xvisibility
.state
== VisibilityUnobscured
)
1720 for (i
= 0; i
< ScreenCount(display
); i
++)
1721 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1722 if (e
->xvisibility
.window
== r
->bar_window
)
1726 void (*handler
[LASTEvent
])(XEvent
*) = {
1728 [KeyPress
] = keypress
,
1729 [ButtonPress
] = buttonpress
,
1730 [ConfigureRequest
] = configurerequest
,
1731 [ConfigureNotify
] = configurenotify
,
1732 [DestroyNotify
] = destroynotify
,
1733 [EnterNotify
] = enternotify
,
1734 [FocusIn
] = focusin
,
1735 [MappingNotify
] = mappingnotify
,
1736 [MapRequest
] = maprequest
,
1737 [PropertyNotify
] = propertynotify
,
1738 [UnmapNotify
] = unmapnotify
,
1739 [VisibilityNotify
] = visibilitynotify
,
1743 xerror_start(Display
*d
, XErrorEvent
*ee
)
1750 xerror(Display
*d
, XErrorEvent
*ee
)
1752 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1760 xerrorxlib
= XSetErrorHandler(xerror_start
);
1762 /* this causes an error if some other window manager is running */
1763 XSelectInput(display
, DefaultRootWindow(display
),
1764 SubstructureRedirectMask
);
1765 XSync(display
, False
);
1769 XSetErrorHandler(xerror
);
1770 XSync(display
, False
);
1779 unsigned char *p
= NULL
;
1780 unsigned long n
, extra
;
1783 astate
= XInternAtom(display
, "WM_STATE", False
);
1784 status
= XGetWindowProperty(display
, w
, astate
, 0L, 2L, False
, astate
,
1785 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
1786 if (status
!= Success
)
1795 new_region(struct swm_screen
*s
, struct workspace
*ws
,
1796 int x
, int y
, int w
, int h
)
1798 struct swm_region
*r
;
1800 DNPRINTF(SWM_D_MISC
, "new region on screen %d: %dx%d (%d, %d)\n",
1801 s
->idx
, x
, y
, w
, h
);
1803 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
1804 errx(1, "calloc: failed to allocate memory for screen");
1813 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
1820 #ifdef SWM_XRR_HAS_CRTC
1822 XRRScreenResources
*sr
;
1824 #endif /* SWM_XRR_HAS_CRTC */
1825 Window d1
, d2
, *wins
= NULL
;
1826 XWindowAttributes wa
;
1827 struct swm_region
*r
;
1829 int errorbase
, major
, minor
;
1830 int ncrtc
= 0, w
= 0;
1832 struct workspace
*ws
;
1834 if ((screens
= calloc(ScreenCount(display
),
1835 sizeof(struct swm_screen
))) == NULL
)
1836 errx(1, "calloc: screens");
1838 /* map physical screens */
1839 for (i
= 0; i
< ScreenCount(display
); i
++) {
1840 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
1842 TAILQ_INIT(&screens
[i
].rl
);
1843 screens
[i
].root
= RootWindow(display
, i
);
1844 XGetWindowAttributes(display
, screens
[i
].root
, &wa
);
1845 XSelectInput(display
, screens
[i
].root
,
1846 ButtonPressMask
| wa
.your_event_mask
);
1848 /* set default colors */
1849 screens
[i
].color_focus
= name_to_color("red");
1850 screens
[i
].color_unfocus
= name_to_color("rgb:88/88/88");
1851 screens
[i
].bar_border
= name_to_color("rgb:00/80/80");
1852 screens
[i
].bar_color
= name_to_color("black");
1853 screens
[i
].bar_font_color
= name_to_color("rgb:a0/a0/a0");
1855 /* init all workspaces */
1856 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
1857 ws
= &screens
[i
].ws
[j
];
1862 TAILQ_INIT(&ws
->winlist
);
1864 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
1865 if (layouts
[k
].l_config
!= NULL
)
1866 layouts
[k
].l_config(ws
,
1867 SWM_ARG_ID_STACKINIT
);
1868 ws
->cur_layout
= &layouts
[0];
1871 /* map virtual screens onto physical screens */
1872 screens
[i
].xrandr_support
= XRRQueryExtension(display
,
1873 &xrandr_eventbase
, &errorbase
);
1874 if (screens
[i
].xrandr_support
)
1875 if (XRRQueryVersion(display
, &major
, &minor
) &&
1877 screens
[i
].xrandr_support
= 0;
1879 #if 0 /* not ready for dynamic screen changes */
1880 if (screens
[i
].xrandr_support
)
1881 XRRSelectInput(display
,
1883 RRScreenChangeNotifyMask
);
1886 /* grab existing windows (before we build the bars)*/
1887 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
1890 #ifdef SWM_XRR_HAS_CRTC
1891 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
1893 new_region(&screens
[i
], &screens
[i
].ws
[w
],
1894 0, 0, DisplayWidth(display
, i
),
1895 DisplayHeight(display
, i
));
1899 for (c
= 0; c
< ncrtc
; c
++) {
1900 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
1901 if (ci
->noutput
== 0)
1904 if (ci
!= NULL
&& ci
->mode
== None
)
1905 new_region(&screens
[i
], &screens
[i
].ws
[w
], 0, 0,
1906 DisplayWidth(display
, i
),
1907 DisplayHeight(display
, i
));
1909 new_region(&screens
[i
], &screens
[i
].ws
[w
],
1910 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
1913 XRRFreeCrtcInfo(ci
);
1914 XRRFreeScreenResources(sr
);
1916 new_region(&screens
[i
], &screens
[i
].ws
[w
], 0, 0,
1917 DisplayWidth(display
, i
),
1918 DisplayHeight(display
, i
));
1919 #endif /* SWM_XRR_HAS_CRTC */
1921 /* attach windows to a region */
1922 /* normal windows */
1923 if ((r
= TAILQ_FIRST(&screens
[i
].rl
)) == NULL
)
1924 errx(1, "no regions on screen %d", i
);
1926 for (i
= 0; i
< no
; i
++) {
1927 XGetWindowAttributes(display
, wins
[i
], &wa
);
1928 if (!XGetWindowAttributes(display
, wins
[i
], &wa
) ||
1929 wa
.override_redirect
||
1930 XGetTransientForHint(display
, wins
[i
], &d1
))
1933 if (wa
.map_state
== IsViewable
||
1934 getstate(wins
[i
]) == NormalState
)
1935 manage_window(wins
[i
], r
->ws
);
1937 /* transient windows */
1938 for (i
= 0; i
< no
; i
++) {
1939 if (!XGetWindowAttributes(display
, wins
[i
], &wa
))
1942 if (XGetTransientForHint(display
, wins
[i
], &d1
) &&
1943 (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) ==
1945 manage_window(wins
[i
], r
->ws
);
1955 main(int argc
, char *argv
[])
1958 char conf
[PATH_MAX
], *cfile
= NULL
;
1965 fprintf(stderr
, "Welcome to scrotwm V%s\n", SWM_VERSION
);
1966 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1967 warnx("no locale support");
1969 if (!(display
= XOpenDisplay(0)))
1970 errx(1, "can not open display");
1973 errx(1, "other wm running");
1975 astate
= XInternAtom(display
, "WM_STATE", False
);
1977 /* look for local and global conf file */
1978 pwd
= getpwuid(getuid());
1980 errx(1, "invalid user %d", getuid());
1984 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
1985 if (stat(conf
, &sb
) != -1) {
1986 if (S_ISREG(sb
.st_mode
))
1989 /* try global conf file */
1990 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
1991 if (!stat(conf
, &sb
))
1992 if (S_ISREG(sb
.st_mode
))
1999 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2004 xfd
= ConnectionNumber(display
);
2007 if (select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
2009 errx(1, "select failed");
2014 while(XPending(display
)) {
2015 XNextEvent(display
, &e
);
2016 if (handler
[e
.type
])
2017 handler
[e
.type
](&e
);
2021 XCloseDisplay(display
);