]>
code.delx.au - spectrwm/blob - scrotwm.c
3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Much code and ideas taken from dwm under the following license:
20 * MIT/X Consortium License
22 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
23 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
24 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
25 * 2007 Premysl Hruby <dfenze at gmail dot com>
26 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
27 * 2007 Christof Musik <christof at sendfax dot de>
28 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
29 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
30 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, including without limitation
35 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 * and/or sell copies of the Software, and to permit persons to whom the
37 * Software is furnished to do so, subject to the following conditions:
39 * The above copyright notice and this permission notice shall be included in
40 * all copies or substantial portions of the Software.
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
51 #define SWM_VERSION "0.5"
65 #include <sys/types.h>
69 #include <sys/queue.h>
70 #include <sys/param.h>
71 #include <sys/select.h>
73 #include <X11/cursorfont.h>
74 #include <X11/keysym.h>
75 #include <X11/Xatom.h>
77 #include <X11/Xproto.h>
78 #include <X11/Xutil.h>
79 #include <X11/extensions/Xrandr.h>
82 # error XRandR versions less than 1.0 are not supported
87 #define SWM_XRR_HAS_CRTC
91 /* #define SWM_DEBUG */
93 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
94 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
95 #define SWM_D_MISC 0x0001
96 #define SWM_D_EVENT 0x0002
97 #define SWM_D_WS 0x0004
98 #define SWM_D_FOCUS 0x0008
99 #define SWM_D_MOVE 0x0010
100 #define SWM_D_STACK 0x0020
102 u_int32_t swm_debug
= 0
111 #define DPRINTF(x...)
112 #define DNPRINTF(n,x...)
115 #define LENGTH(x) (sizeof x / sizeof x[0])
116 #define MODKEY Mod1Mask
117 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
119 #define X(r) (r)->g.x
120 #define Y(r) (r)->g.y
121 #define WIDTH(r) (r)->g.w
122 #define HEIGHT(r) (r)->g.h
126 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
129 int xrandr_eventbase
;
130 int ignore_enter
= 0;
131 unsigned int numlockmask
= 0;
135 double dialog_ratio
= .6;
137 #define SWM_BAR_MAX (128)
138 sig_atomic_t bar_alarm
= 0;
145 char *bar_fonts
[] = {
146 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
147 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
151 /* terminal + args */
152 char *spawn_term
[] = { "xterm", NULL
};
153 char *spawn_menu
[] = { "dmenu_run", NULL
};
155 /* layout manager data */
156 struct swm_geometry
{
166 /* virtual "screens" */
168 TAILQ_ENTRY(swm_region
) entry
;
169 struct swm_geometry g
;
171 struct workspace
*ws
; /* current workspace on this region */
172 struct swm_screen
*s
; /* screen idx */
174 TAILQ_HEAD(swm_region_list
, swm_region
);
178 TAILQ_ENTRY(ws_win
) entry
;
180 struct swm_geometry g
;
184 struct workspace
*ws
; /* always valid */
185 struct swm_screen
*s
; /* always valid, never changes */
186 XWindowAttributes wa
;
188 TAILQ_HEAD(ws_win_list
, ws_win
);
190 /* layout handlers */
192 void vertical_config(struct workspace
*, int);
193 void vertical_stack(struct workspace
*, struct swm_geometry
*);
194 void horizontal_config(struct workspace
*, int);
195 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
196 void max_stack(struct workspace
*, struct swm_geometry
*);
199 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
200 void (*l_config
)(struct workspace
*, int);
202 /* stack, configure */
203 { vertical_stack
, vertical_config
},
204 { horizontal_stack
, horizontal_config
},
209 #define SWM_H_SLICE (32)
210 #define SWM_V_SLICE (32)
212 /* define work spaces */
214 int idx
; /* workspace index */
215 int restack
; /* restack on switch */
216 struct layout
*cur_layout
; /* current layout handlers */
217 struct ws_win
*focus
; /* may be NULL */
218 struct swm_region
*r
; /* may be NULL */
219 struct ws_win_list winlist
; /* list of windows in ws */
223 int horizontal_msize
;
230 /* physical screen mapping */
231 #define SWM_WS_MAX (10) /* XXX Too small? */
233 int idx
; /* screen index */
234 struct swm_region_list rl
; /* list of regions on this screen */
237 struct workspace ws
[SWM_WS_MAX
];
240 unsigned long bar_border
;
241 unsigned long bar_color
;
242 unsigned long bar_font_color
;
243 unsigned long color_focus
; /* XXX should this be per ws? */
244 unsigned long color_unfocus
;
245 char bar_text
[SWM_BAR_MAX
];
247 struct swm_screen
*screens
;
249 Window rootclick
= 0;
251 struct ws_win
*cur_focus
= NULL
;
253 /* args to functions */
256 #define SWM_ARG_ID_FOCUSNEXT (0)
257 #define SWM_ARG_ID_FOCUSPREV (1)
258 #define SWM_ARG_ID_FOCUSMAIN (2)
259 #define SWM_ARG_ID_SWAPNEXT (3)
260 #define SWM_ARG_ID_SWAPPREV (4)
261 #define SWM_ARG_ID_SWAPMAIN (5)
262 #define SWM_ARG_ID_MASTERSHRINK (6)
263 #define SWM_ARG_ID_MASTERGROW (7)
264 #define SWM_ARG_ID_MASTERADD (8)
265 #define SWM_ARG_ID_MASTERDEL (9)
266 #define SWM_ARG_ID_STACKRESET (10)
267 #define SWM_ARG_ID_STACKINIT (11)
272 name_to_color(char *colorname
)
276 XColor screen_def
, exact_def
;
277 unsigned long result
= 0;
278 char cname
[32] = "#";
280 cmap
= DefaultColormap(display
, screens
[0].idx
);
281 status
= XAllocNamedColor(display
, cmap
, colorname
,
282 &screen_def
, &exact_def
);
284 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
285 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
289 result
= screen_def
.pixel
;
291 fprintf(stderr
, "color '%s' not found.\n", colorname
);
296 /* conf file stuff */
297 #define SWM_CONF_WS "\n= \t"
298 #define SWM_CONF_FILE "scrotwm.conf"
300 conf_load(char *filename
)
303 char *line
, *cp
, *var
, *val
;
304 size_t len
, lineno
= 0;
307 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
309 if (filename
== NULL
)
312 if ((config
= fopen(filename
, "r")) == NULL
)
316 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
320 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
326 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
328 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
329 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
332 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
335 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
336 bar_enabled
= atoi(val
);
337 else if (!strncmp(var
, "bar_border",
338 strlen("bar_border")))
339 for (i
= 0; i
< ScreenCount(display
); i
++)
340 screens
[i
].bar_border
= name_to_color(val
);
341 else if (!strncmp(var
, "bar_color",
342 strlen("bar_color")))
343 for (i
= 0; i
< ScreenCount(display
); i
++)
344 screens
[i
].bar_color
= name_to_color(val
);
345 else if (!strncmp(var
, "bar_font_color",
346 strlen("bar_font_color")))
347 for (i
= 0; i
< ScreenCount(display
); i
++)
348 screens
[i
].bar_font_color
= name_to_color(val
);
349 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
350 asprintf(&bar_fonts
[0], "%s", val
);
356 if (!strncmp(var
, "color_focus", strlen("color_focus")))
357 for (i
= 0; i
< ScreenCount(display
); i
++)
358 screens
[i
].color_focus
= name_to_color(val
);
359 else if (!strncmp(var
, "color_unfocus",
360 strlen("color_unfocus")))
361 for (i
= 0; i
< ScreenCount(display
); i
++)
362 screens
[i
].color_unfocus
= name_to_color(val
);
368 if (!strncmp(var
, "dialog_ratio",
369 strlen("dialog_ratio"))) {
370 dialog_ratio
= atof(val
);
371 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
378 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
379 asprintf(&spawn_term
[0], "%s", val
); /* XXX args? */
390 errx(1, "invalid conf file entry: %s=%s", var
, val
);
394 bar_print(struct swm_region
*r
, char *s
, int erase
)
397 XSetForeground(display
, bar_gc
, r
->s
->bar_color
);
398 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
,
399 r
->s
->bar_text
, strlen(r
->s
->bar_text
));
402 strlcpy(r
->s
->bar_text
, s
, sizeof r
->s
->bar_text
);
403 XSetForeground(display
, bar_gc
, r
->s
->bar_font_color
);
404 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
,
405 r
->s
->bar_text
, strlen(r
->s
->bar_text
));
413 struct swm_region
*r
;
418 if (bar_enabled
== 0)
422 localtime_r(&tmt
, &tm
);
423 strftime(s
, sizeof s
, "%a %b %d %R %Z %Y", &tm
);
424 for (i
= 0; i
< ScreenCount(display
); i
++) {
426 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
427 snprintf(e
, sizeof e
, "%s %d:%d",
428 s
, x
++, r
->ws
->idx
+ 1);
432 XSync(display
, False
);
443 bar_toggle(struct swm_region
*r
, union arg
*args
)
445 struct swm_region
*tmpr
;
448 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
451 for (i
= 0; i
< ScreenCount(display
); i
++)
452 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
453 XUnmapWindow(display
, tmpr
->bar_window
);
455 for (i
= 0; i
< ScreenCount(display
); i
++)
456 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
457 XMapRaised(display
, tmpr
->bar_window
);
459 bar_enabled
= !bar_enabled
;
460 XSync(display
, False
);
461 for (i
= 0; i
< ScreenCount(display
); i
++)
462 for (j
= 0; j
< SWM_WS_MAX
; j
++)
463 screens
[i
].ws
[j
].restack
= 1;
466 /* must be after stack */
467 for (i
= 0; i
< ScreenCount(display
); i
++)
468 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
473 bar_setup(struct swm_region
*r
)
477 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
478 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
482 if (bar_fonts
[i
] == NULL
)
483 errx(1, "couldn't load font");
484 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
486 r
->bar_window
= XCreateSimpleWindow(display
,
487 r
->s
->root
, X(r
), Y(r
), WIDTH(r
) - 2, bar_height
- 2,
488 1, r
->s
->bar_border
, r
->s
->bar_color
);
489 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
490 XSetFont(display
, bar_gc
, bar_fs
->fid
);
491 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
493 XMapRaised(display
, r
->bar_window
);
494 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
496 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
497 err(1, "could not install bar_signal");
502 config_win(struct ws_win
*win
)
506 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
507 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
508 ce
.type
= ConfigureNotify
;
509 ce
.display
= display
;
515 ce
.height
= win
->g
.h
;
516 ce
.border_width
= 1; /* XXX store this! */
518 ce
.override_redirect
= False
;
519 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
523 count_win(struct workspace
*ws
, int count_transient
)
528 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
529 if (count_transient
== 0 && win
->floating
)
531 if (count_transient
== 0 && win
->transient
)
535 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
541 quit(struct swm_region
*r
, union arg
*args
)
543 DNPRINTF(SWM_D_MISC
, "quit\n");
548 restart(struct swm_region
*r
, union arg
*args
)
550 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
552 /* disable alarm because the following code may not be interrupted */
554 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
555 errx(1, "can't disable alarm");
557 XCloseDisplay(display
);
558 execvp(start_argv
[0], start_argv
);
559 fprintf(stderr
, "execvp failed\n");
565 root_to_region(Window root
)
567 struct swm_region
*r
;
572 for (i
= 0; i
< ScreenCount(display
); i
++)
573 if (screens
[i
].root
== root
)
576 if (rootclick
!= root
&& /* if root was just clicked in, use cursor */
577 cur_focus
&& cur_focus
->ws
->r
&& cur_focus
->s
== &screens
[i
])
578 r
= cur_focus
->ws
->r
;
580 if (XQueryPointer(display
, screens
[i
].root
,
581 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) == False
) {
582 r
= TAILQ_FIRST(&screens
[i
].rl
);
584 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
585 if (x
> X(r
) && x
< X(r
) + WIDTH(r
) &&
586 y
> Y(r
) && y
< Y(r
) + HEIGHT(r
))
591 r
= TAILQ_FIRST(&screens
[i
].rl
);
598 find_window(Window id
)
603 for (i
= 0; i
< ScreenCount(display
); i
++)
604 for (j
= 0; j
< SWM_WS_MAX
; j
++)
605 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
612 spawn(struct swm_region
*r
, union arg
*args
)
614 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
616 * The double-fork construct avoids zombie processes and keeps the code
617 * clean from stupid signal handlers.
622 close(ConnectionNumber(display
));
624 execvp(args
->argv
[0], args
->argv
);
625 fprintf(stderr
, "execvp failed\n");
634 unfocus_win(struct ws_win
*win
)
636 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: id: %lu\n", win
->id
);
637 if (win
->ws
->r
&& win
->focus
)
638 XSetWindowBorder(display
, win
->id
,
639 win
->ws
->r
->s
->color_unfocus
);
641 if (win
->ws
->focus
== win
)
642 win
->ws
->focus
= NULL
;
643 if (cur_focus
== win
)
649 focus_win(struct ws_win
*win
)
651 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
->id
);
655 win
->ws
->focus
= win
;
656 if (win
->ws
->r
!= NULL
) {
657 if (cur_focus
&& cur_focus
!= win
)
658 unfocus_win(cur_focus
);
661 XSetWindowBorder(display
, win
->id
,
662 win
->ws
->r
->s
->color_focus
);
664 XSetInputFocus(display
, win
->id
,
665 RevertToPointerRoot
, CurrentTime
);
670 switchws(struct swm_region
*r
, union arg
*args
)
673 struct swm_region
*this_r
, *other_r
;
675 struct workspace
*new_ws
, *old_ws
;
679 new_ws
= &this_r
->s
->ws
[wsid
];
681 DNPRINTF(SWM_D_WS
, "switchws screen %d region %dx%d+%d+%d: "
682 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
685 if (new_ws
== old_ws
)
690 /* if the other workspace is hidden, switch windows */
691 /* map new window first to prevent ugly blinking */
692 TAILQ_FOREACH(win
, &new_ws
->winlist
, entry
)
693 XMapRaised(display
, win
->id
);
695 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
696 XUnmapWindow(display
, win
->id
);
701 other_r
->ws
= old_ws
;
710 focus_win(new_ws
->focus
);
716 swapwin(struct swm_region
*r
, union arg
*args
)
718 struct ws_win
*target
;
719 struct ws_win_list
*wl
;
722 DNPRINTF(SWM_D_WS
, "swapwin id %d "
723 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
724 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
725 if (cur_focus
== NULL
)
728 wl
= &cur_focus
->ws
->winlist
;
731 case SWM_ARG_ID_SWAPPREV
:
732 target
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
733 TAILQ_REMOVE(wl
, cur_focus
, entry
);
735 TAILQ_INSERT_TAIL(wl
, cur_focus
, entry
);
737 TAILQ_INSERT_BEFORE(target
, cur_focus
, entry
);
739 case SWM_ARG_ID_SWAPNEXT
:
740 target
= TAILQ_NEXT(cur_focus
, entry
);
741 TAILQ_REMOVE(wl
, cur_focus
, entry
);
743 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
745 TAILQ_INSERT_AFTER(wl
, target
, cur_focus
, entry
);
747 case SWM_ARG_ID_SWAPMAIN
:
748 target
= TAILQ_FIRST(wl
);
749 if (target
== cur_focus
)
751 TAILQ_REMOVE(wl
, target
, entry
);
752 TAILQ_INSERT_BEFORE(cur_focus
, target
, entry
);
753 TAILQ_REMOVE(wl
, cur_focus
, entry
);
754 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
757 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
766 focus(struct swm_region
*r
, union arg
*args
)
768 struct ws_win
*winfocus
, *winlostfocus
;
769 struct ws_win_list
*wl
;
771 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
772 if (cur_focus
== NULL
)
775 wl
= &cur_focus
->ws
->winlist
;
777 winlostfocus
= cur_focus
;
780 case SWM_ARG_ID_FOCUSPREV
:
781 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
782 if (winfocus
== NULL
)
783 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
786 case SWM_ARG_ID_FOCUSNEXT
:
787 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
788 if (winfocus
== NULL
)
789 winfocus
= TAILQ_FIRST(wl
);
792 case SWM_ARG_ID_FOCUSMAIN
:
793 winfocus
= TAILQ_FIRST(wl
);
800 if (winfocus
== winlostfocus
)
804 XSync(display
, False
);
808 cycle_layout(struct swm_region
*r
, union arg
*args
)
810 struct workspace
*ws
= r
->ws
;
812 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
815 if (ws
->cur_layout
->l_stack
== NULL
)
816 ws
->cur_layout
= &layouts
[0];
823 stack_config(struct swm_region
*r
, union arg
*args
)
825 struct workspace
*ws
= r
->ws
;
827 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
830 if (ws
->cur_layout
->l_config
!= NULL
)
831 ws
->cur_layout
->l_config(ws
, args
->id
);
833 if (args
->id
!= SWM_ARG_ID_STACKINIT
);
839 struct swm_geometry g
;
840 struct swm_region
*r
;
843 DNPRINTF(SWM_D_STACK
, "stack\n");
845 for (i
= 0; i
< ScreenCount(display
); i
++) {
847 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
848 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
849 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
851 /* start with screen geometry, adjust for bar */
861 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
864 XSync(display
, False
);
868 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
873 bzero(&wc
, sizeof wc
);
874 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
876 if (win
->transient
) {
877 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
878 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
881 wc
.height
= win
->g
.h
;
882 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
883 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
885 DNPRINTF(SWM_D_STACK
, "stack_floater: win %lu x %d y %d w %d h %d\n",
886 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
888 XConfigureWindow(display
, win
->id
, mask
, &wc
);
893 vertical_config(struct workspace
*ws
, int id
)
895 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
898 case SWM_ARG_ID_STACKRESET
:
899 case SWM_ARG_ID_STACKINIT
:
900 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
901 ws
->l_state
.vertical_mwin
= 1;
903 case SWM_ARG_ID_MASTERSHRINK
:
904 if (ws
->l_state
.vertical_msize
> 1)
905 ws
->l_state
.vertical_msize
--;
907 case SWM_ARG_ID_MASTERGROW
:
908 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
909 ws
->l_state
.vertical_msize
++;
911 case SWM_ARG_ID_MASTERADD
:
912 ws
->l_state
.vertical_mwin
++;
914 case SWM_ARG_ID_MASTERDEL
:
915 if (ws
->l_state
.vertical_mwin
> 0)
916 ws
->l_state
.vertical_mwin
--;
924 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
926 struct swm_geometry gg
= *g
;
927 struct ws_win
*win
, *winfocus
;
928 int i
, j
, split
, colno
, hrh
, winno
, main_width
;
931 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
933 if ((winno
= count_win(ws
, 0)) == 0)
936 if (ws
->focus
== NULL
)
937 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
938 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
940 if (ws
->l_state
.vertical_mwin
&&
941 winno
> ws
->l_state
.vertical_mwin
) {
942 split
= ws
->l_state
.vertical_mwin
;
944 main_width
= (g
->w
/ SWM_V_SLICE
) *
945 ws
->l_state
.vertical_msize
;
955 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
956 if (split
&& i
== split
) {
957 colno
= winno
- split
;
958 hrh
= (g
->h
/ colno
);
959 gg
.x
+= main_width
+ 2;
960 gg
.w
= g
->w
- (main_width
+ 2);
963 } else if (j
== colno
- 1)
964 gg
.h
= (hrh
+ (g
->h
- (colno
* hrh
))) - 2;
972 if (win
->transient
!= 0 || win
->floating
!= 0)
973 stack_floater(win
, ws
->r
);
975 bzero(&wc
, sizeof wc
);
977 win
->g
.x
= wc
.x
= gg
.x
;
978 win
->g
.y
= wc
.y
= gg
.y
;
979 win
->g
.w
= wc
.width
= gg
.w
;
980 win
->g
.h
= wc
.height
= gg
.h
;
981 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
982 XConfigureWindow(display
, win
->id
, mask
, &wc
);
985 XMapRaised(display
, win
->id
);
991 focus_win(winfocus
); /* has to be done outside of the loop */
996 horizontal_config(struct workspace
*ws
, int id
)
998 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
1001 case SWM_ARG_ID_STACKRESET
:
1002 case SWM_ARG_ID_STACKINIT
:
1003 ws
->l_state
.horizontal_mwin
= 1;
1004 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
1006 case SWM_ARG_ID_MASTERSHRINK
:
1007 if (ws
->l_state
.horizontal_msize
> 1)
1008 ws
->l_state
.horizontal_msize
--;
1010 case SWM_ARG_ID_MASTERGROW
:
1011 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
1012 ws
->l_state
.horizontal_msize
++;
1014 case SWM_ARG_ID_MASTERADD
:
1015 ws
->l_state
.horizontal_mwin
++;
1017 case SWM_ARG_ID_MASTERDEL
:
1018 if (ws
->l_state
.horizontal_mwin
> 0)
1019 ws
->l_state
.horizontal_mwin
--;
1027 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1029 struct swm_geometry gg
= *g
;
1030 struct ws_win
*win
, *winfocus
;
1031 int i
, j
, split
, rowno
, hrw
, winno
, main_height
;
1034 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
1036 if ((winno
= count_win(ws
, 0)) == 0)
1039 if (ws
->focus
== NULL
)
1040 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1041 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1043 if (ws
->l_state
.horizontal_mwin
&&
1044 winno
> ws
->l_state
.horizontal_mwin
) {
1045 split
= ws
->l_state
.horizontal_mwin
;
1047 main_height
= (g
->h
/ SWM_V_SLICE
) *
1048 ws
->l_state
.horizontal_msize
;
1058 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1059 if (split
&& i
== split
) {
1060 rowno
= winno
- split
;
1061 hrw
= (g
->w
/ rowno
);
1062 gg
.y
+= main_height
+ 2;
1063 gg
.h
= g
->h
- (main_height
+ 2);
1066 } else if (j
== rowno
- 1)
1067 gg
.w
= (hrw
+ (g
->w
- (rowno
* hrw
))) - 2;
1074 if (win
->transient
!= 0 || win
->floating
!= 0)
1075 stack_floater(win
, ws
->r
);
1077 bzero(&wc
, sizeof wc
);
1078 wc
.border_width
= 1;
1079 win
->g
.x
= wc
.x
= gg
.x
;
1080 win
->g
.y
= wc
.y
= gg
.y
;
1081 win
->g
.w
= wc
.width
= gg
.w
;
1082 win
->g
.h
= wc
.height
= gg
.h
;
1083 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1084 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1087 XMapRaised(display
, win
->id
);
1093 focus_win(winfocus
); /* this has to be done outside of the loop */
1096 /* fullscreen view */
1098 max_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1100 struct swm_geometry gg
= *g
;
1101 struct ws_win
*win
, *winfocus
;
1104 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
1106 if (count_win(ws
, 0) == 0)
1109 if (ws
->focus
== NULL
)
1110 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1111 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1113 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1114 if (win
->transient
!= 0 || win
->floating
!= 0) {
1115 if (win
== ws
->focus
) {
1117 stack_floater(win
, ws
->r
);
1118 XMapRaised(display
, win
->id
);
1120 XUnmapWindow(display
, win
->id
);
1122 bzero(&wc
, sizeof wc
);
1123 wc
.border_width
= 1;
1124 win
->g
.x
= wc
.x
= gg
.x
;
1125 win
->g
.y
= wc
.y
= gg
.y
;
1126 win
->g
.w
= wc
.width
= gg
.w
;
1127 win
->g
.h
= wc
.height
= gg
.h
;
1128 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1129 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1131 if (win
== ws
->focus
) {
1132 XMapRaised(display
, win
->id
);
1134 XUnmapWindow(display
, win
->id
);
1139 focus_win(winfocus
); /* has to be done outside of the loop */
1143 send_to_ws(struct swm_region
*r
, union arg
*args
)
1145 int wsid
= args
->id
;
1146 struct ws_win
*win
= cur_focus
;
1147 struct workspace
*ws
, *nws
;
1149 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
1152 nws
= &win
->s
->ws
[wsid
];
1154 XUnmapWindow(display
, win
->id
);
1156 /* find a window to focus */
1157 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1158 if (ws
->focus
== NULL
)
1159 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1160 if (ws
->focus
== win
)
1163 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1165 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
1168 if (count_win(nws
, 1) == 1)
1176 /* key definitions */
1180 void (*func
)(struct swm_region
*r
, union arg
*);
1183 /* modifier key function argument */
1184 { MODKEY
, XK_space
, cycle_layout
, {0} },
1185 { MODKEY
| ShiftMask
, XK_space
, stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
1186 { MODKEY
, XK_h
, stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
1187 { MODKEY
, XK_l
, stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
1188 { MODKEY
, XK_comma
, stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
1189 { MODKEY
, XK_period
, stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
1190 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
1191 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1192 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1193 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
1194 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
1195 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
1196 { MODKEY
, XK_p
, spawn
, {.argv
= spawn_menu
} },
1197 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
1198 { MODKEY
, XK_q
, restart
, {0} },
1199 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
1200 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
1201 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
1202 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
1203 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
1204 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
1205 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
1206 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
1207 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
1208 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
1209 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
1210 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
1211 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
1212 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
1213 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
1214 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
1215 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
1216 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
1217 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
1218 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
1219 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
1220 { MODKEY
, XK_b
, bar_toggle
, {0} },
1221 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1222 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1226 updatenumlockmask(void)
1229 XModifierKeymap
*modmap
;
1231 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
1233 modmap
= XGetModifierMapping(display
);
1234 for (i
= 0; i
< 8; i
++)
1235 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1236 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1237 == XKeysymToKeycode(display
, XK_Num_Lock
))
1238 numlockmask
= (1 << i
);
1240 XFreeModifiermap(modmap
);
1246 unsigned int i
, j
, k
;
1248 unsigned int modifiers
[] =
1249 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
1251 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
1252 updatenumlockmask();
1254 for (k
= 0; k
< ScreenCount(display
); k
++) {
1255 if (TAILQ_EMPTY(&screens
[k
].rl
))
1257 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
1258 for (i
= 0; i
< LENGTH(keys
); i
++) {
1259 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
1260 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1261 XGrabKey(display
, code
,
1262 keys
[i
].mod
| modifiers
[j
],
1263 screens
[k
].root
, True
,
1264 GrabModeAsync
, GrabModeAsync
);
1271 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
1279 XKeyEvent
*ev
= &e
->xkey
;
1281 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
1283 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
1284 for (i
= 0; i
< LENGTH(keys
); i
++)
1285 if (keysym
== keys
[i
].keysym
1286 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1288 keys
[i
].func(root_to_region(ev
->root
),
1293 buttonpress(XEvent
*e
)
1295 XButtonPressedEvent
*ev
= &e
->xbutton
;
1296 #ifdef SWM_CLICKTOFOCUS
1298 struct workspace
*ws
;
1299 struct swm_region
*r
;
1302 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
1304 if (ev
->window
== ev
->root
) {
1305 rootclick
= ev
->root
;
1308 if (ev
->window
== cur_focus
->id
)
1310 #ifdef SWM_CLICKTOFOCUS
1311 r
= root_to_region(ev
->root
);
1313 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
1314 if (win
->id
== ev
->window
) {
1315 /* focus in the clicked window */
1316 XSetWindowBorder(display
, ev
->window
, 0xff0000);
1317 XSetWindowBorder(display
, ws
->focus
->id
, 0x888888);
1318 XSetInputFocus(display
, ev
->window
, RevertToPointerRoot
,
1321 XSync(display
, False
);
1328 set_win_state(struct ws_win
*win
, long state
)
1330 long data
[] = {state
, None
};
1332 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
1334 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1335 (unsigned char *)data
, 2);
1339 manage_window(Window id
, struct workspace
*ws
)
1345 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1347 return (win
); /* already being managed */
1350 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
1351 errx(1, "calloc: failed to allocate memory for new window");
1355 win
->s
= ws
->r
->s
; /* this never changes */
1356 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
1358 /* make new win focused */
1361 XGetTransientForHint(display
, win
->id
, &trans
);
1363 win
->transient
= trans
;
1364 DNPRINTF(SWM_D_MISC
, "manage_window: win %u transient %u\n",
1365 (unsigned)win
->id
, win
->transient
);
1367 XGetWindowAttributes(display
, id
, &win
->wa
);
1368 win
->g
.w
= win
->wa
.width
;
1369 win
->g
.h
= win
->wa
.height
;
1370 win
->g
.x
= win
->wa
.x
;
1371 win
->g
.y
= win
->wa
.y
;
1373 /* XXX make this a table */
1374 bzero(&ch
, sizeof ch
);
1375 if (XGetClassHint(display
, win
->id
, &ch
)) {
1376 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1377 if (!strcmp(ch
.res_class
, "MPlayer") && !strcmp(ch
.res_name
, "xv")) {
1381 XFree(ch
.res_class
);
1386 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
1387 PropertyChangeMask
| StructureNotifyMask
);
1389 set_win_state(win
, NormalState
);
1395 configurerequest(XEvent
*e
)
1397 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
1402 if ((win
= find_window(ev
->window
)) == NULL
)
1406 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
1408 bzero(&wc
, sizeof wc
);
1411 wc
.width
= ev
->width
;
1412 wc
.height
= ev
->height
;
1413 wc
.border_width
= ev
->border_width
;
1414 wc
.sibling
= ev
->above
;
1415 wc
.stack_mode
= ev
->detail
;
1416 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
1418 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
1420 if (win
->floating
) {
1421 if (ev
->value_mask
& CWX
)
1423 if (ev
->value_mask
& CWY
)
1425 if (ev
->value_mask
& CWWidth
)
1426 win
->g
.w
= ev
->width
;
1427 if (ev
->value_mask
& CWHeight
)
1428 win
->g
.h
= ev
->height
;
1429 if (win
->ws
->r
!= NULL
) {
1430 /* this seems to be full screen */
1431 if (win
->g
.w
> WIDTH(win
->ws
->r
)) {
1436 if (win
->g
.h
> HEIGHT(win
->ws
->r
)) {
1442 if ((ev
->value_mask
& (CWX
|CWY
)) &&
1443 !(ev
->value_mask
& (CWWidth
|CWHeight
)))
1445 XMoveResizeWindow(display
, win
->id
,
1446 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
1453 configurenotify(XEvent
*e
)
1455 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
1456 e
->xconfigure
.window
);
1460 destroynotify(XEvent
*e
)
1463 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
1465 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
1467 if ((win
= find_window(ev
->window
)) != NULL
) {
1468 struct workspace
*ws
= win
->ws
;
1469 /* find a window to focus */
1470 if (ws
->focus
== win
)
1471 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1472 if (ws
->focus
== NULL
)
1473 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1474 if (ws
->focus
== win
)
1476 if (cur_focus
== win
) {
1477 if (ws
->focus
== NULL
)
1478 unfocus_win(win
); /* XXX focus another ws? */
1480 focus_win(ws
->focus
);
1483 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1484 set_win_state(win
, WithdrawnState
);
1491 enternotify(XEvent
*e
)
1493 XCrossingEvent
*ev
= &e
->xcrossing
;
1497 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
1499 if ((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) &&
1500 ev
->window
!= ev
->root
)
1503 /* eat event(r) to prevent autofocus */
1507 /* brute force for now */
1508 for (i
= 0; i
< ScreenCount(display
); i
++) {
1509 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
1510 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1511 if (win
->id
== ev
->window
)
1521 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", e
->xfocus
.window
);
1523 /* XXX this probably needs better handling now.
1524 if (ev->window == ev->root)
1528 * kill grab for now so that we can cut and paste , this screws up
1532 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1533 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1534 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1539 mappingnotify(XEvent
*e
)
1541 XMappingEvent
*ev
= &e
->xmapping
;
1543 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
1545 XRefreshKeyboardMapping(ev
);
1546 if (ev
->request
== MappingKeyboard
)
1551 maprequest(XEvent
*e
)
1553 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1554 XWindowAttributes wa
;
1555 struct swm_region
*r
;
1557 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
1558 e
->xmaprequest
.window
);
1560 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
1562 if (wa
.override_redirect
)
1564 r
= root_to_region(wa
.root
);
1565 manage_window(e
->xmaprequest
.window
, r
->ws
);
1570 propertynotify(XEvent
*e
)
1572 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
1573 e
->xproperty
.window
);
1577 unmapnotify(XEvent
*e
)
1579 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
1583 visibilitynotify(XEvent
*e
)
1586 struct swm_region
*r
;
1588 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
1589 e
->xvisibility
.window
);
1590 if (e
->xvisibility
.state
== VisibilityUnobscured
)
1591 for (i
= 0; i
< ScreenCount(display
); i
++)
1592 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1593 if (e
->xvisibility
.window
== r
->bar_window
)
1597 void (*handler
[LASTEvent
])(XEvent
*) = {
1599 [KeyPress
] = keypress
,
1600 [ButtonPress
] = buttonpress
,
1601 [ConfigureRequest
] = configurerequest
,
1602 [ConfigureNotify
] = configurenotify
,
1603 [DestroyNotify
] = destroynotify
,
1604 [EnterNotify
] = enternotify
,
1605 [FocusIn
] = focusin
,
1606 [MappingNotify
] = mappingnotify
,
1607 [MapRequest
] = maprequest
,
1608 [PropertyNotify
] = propertynotify
,
1609 [UnmapNotify
] = unmapnotify
,
1610 [VisibilityNotify
] = visibilitynotify
,
1614 xerror_start(Display
*d
, XErrorEvent
*ee
)
1621 xerror(Display
*d
, XErrorEvent
*ee
)
1623 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1631 xerrorxlib
= XSetErrorHandler(xerror_start
);
1633 /* this causes an error if some other window manager is running */
1634 XSelectInput(display
, DefaultRootWindow(display
),
1635 SubstructureRedirectMask
);
1636 XSync(display
, False
);
1640 XSetErrorHandler(xerror
);
1641 XSync(display
, False
);
1650 unsigned char *p
= NULL
;
1651 unsigned long n
, extra
;
1654 astate
= XInternAtom(display
, "WM_STATE", False
);
1655 status
= XGetWindowProperty(display
, w
, astate
, 0L, 2L, False
, astate
,
1656 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
1657 if (status
!= Success
)
1666 new_region(struct swm_screen
*s
, struct workspace
*ws
,
1667 int x
, int y
, int w
, int h
)
1669 struct swm_region
*r
;
1671 DNPRINTF(SWM_D_MISC
, "new region on screen %d: %dx%d (%d, %d)\n",
1672 s
->idx
, x
, y
, w
, h
);
1674 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
1675 errx(1, "calloc: failed to allocate memory for screen");
1684 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
1691 #ifdef SWM_XRR_HAS_CRTC
1693 XRRScreenResources
*sr
;
1695 #endif /* SWM_XRR_HAS_CRTC */
1696 Window d1
, d2
, *wins
= NULL
;
1697 XWindowAttributes wa
;
1698 struct swm_region
*r
;
1700 int errorbase
, major
, minor
;
1701 int ncrtc
= 0, w
= 0;
1703 struct workspace
*ws
;
1705 if ((screens
= calloc(ScreenCount(display
),
1706 sizeof(struct swm_screen
))) == NULL
)
1707 errx(1, "calloc: screens");
1709 /* map physical screens */
1710 for (i
= 0; i
< ScreenCount(display
); i
++) {
1711 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
1713 TAILQ_INIT(&screens
[i
].rl
);
1714 screens
[i
].root
= RootWindow(display
, i
);
1715 XGetWindowAttributes(display
, screens
[i
].root
, &wa
);
1716 XSelectInput(display
, screens
[i
].root
,
1717 ButtonPressMask
| wa
.your_event_mask
);
1719 /* set default colors */
1720 screens
[i
].color_focus
= name_to_color("red");
1721 screens
[i
].color_unfocus
= name_to_color("rgb:88/88/88");
1722 screens
[i
].bar_border
= name_to_color("rgb:00/80/80");
1723 screens
[i
].bar_color
= name_to_color("black");
1724 screens
[i
].bar_font_color
= name_to_color("rgb:a0/a0/a0");
1726 /* init all workspaces */
1727 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
1728 ws
= &screens
[i
].ws
[j
];
1733 TAILQ_INIT(&ws
->winlist
);
1735 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
1736 if (layouts
[k
].l_config
!= NULL
)
1737 layouts
[k
].l_config(ws
,
1738 SWM_ARG_ID_STACKINIT
);
1739 ws
->cur_layout
= &layouts
[0];
1742 /* map virtual screens onto physical screens */
1743 screens
[i
].xrandr_support
= XRRQueryExtension(display
,
1744 &xrandr_eventbase
, &errorbase
);
1745 if (screens
[i
].xrandr_support
)
1746 if (XRRQueryVersion(display
, &major
, &minor
) &&
1748 screens
[i
].xrandr_support
= 0;
1750 #if 0 /* not ready for dynamic screen changes */
1751 if (screens
[i
].xrandr_support
)
1752 XRRSelectInput(display
,
1754 RRScreenChangeNotifyMask
);
1757 /* grab existing windows (before we build the bars)*/
1758 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
1761 #ifdef SWM_XRR_HAS_CRTC
1762 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
1764 new_region(&screens
[i
], &screens
[i
].ws
[w
],
1765 0, 0, DisplayWidth(display
, i
),
1766 DisplayHeight(display
, i
));
1770 for (c
= 0; c
< ncrtc
; c
++) {
1771 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
1772 if (ci
->noutput
== 0)
1775 if (ci
!= NULL
&& ci
->mode
== None
)
1776 new_region(&screens
[i
], &screens
[i
].ws
[w
], 0, 0,
1777 DisplayWidth(display
, i
),
1778 DisplayHeight(display
, i
));
1780 new_region(&screens
[i
], &screens
[i
].ws
[w
],
1781 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
1784 XRRFreeCrtcInfo(ci
);
1785 XRRFreeScreenResources(sr
);
1787 new_region(&screens
[i
], &screens
[i
].ws
[w
], 0, 0,
1788 DisplayWidth(display
, i
),
1789 DisplayHeight(display
, i
));
1790 #endif /* SWM_XRR_HAS_CRTC */
1792 /* attach windows to a region */
1793 /* normal windows */
1794 if ((r
= TAILQ_FIRST(&screens
[i
].rl
)) == NULL
)
1795 errx(1, "no regions on screen %d", i
);
1797 for (i
= 0; i
< no
; i
++) {
1798 XGetWindowAttributes(display
, wins
[i
], &wa
);
1799 if (!XGetWindowAttributes(display
, wins
[i
], &wa
) ||
1800 wa
.override_redirect
||
1801 XGetTransientForHint(display
, wins
[i
], &d1
))
1804 if (wa
.map_state
== IsViewable
||
1805 getstate(wins
[i
]) == NormalState
)
1806 manage_window(wins
[i
], r
->ws
);
1808 /* transient windows */
1809 for (i
= 0; i
< no
; i
++) {
1810 if (!XGetWindowAttributes(display
, wins
[i
], &wa
))
1813 if (XGetTransientForHint(display
, wins
[i
], &d1
) &&
1814 (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) ==
1816 manage_window(wins
[i
], r
->ws
);
1826 main(int argc
, char *argv
[])
1829 char conf
[PATH_MAX
], *cfile
= NULL
;
1836 fprintf(stderr
, "Welcome to scrotwm V%s\n", SWM_VERSION
);
1837 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1838 warnx("no locale support");
1840 if (!(display
= XOpenDisplay(0)))
1841 errx(1, "can not open display");
1844 errx(1, "other wm running");
1846 astate
= XInternAtom(display
, "WM_STATE", False
);
1848 /* look for local and global conf file */
1849 pwd
= getpwuid(getuid());
1851 errx(1, "invalid user %d", getuid());
1853 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
1854 if (stat(conf
, &sb
) != -1) {
1855 if (S_ISREG(sb
.st_mode
))
1858 /* try global conf file */
1859 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
1860 if (!stat(conf
, &sb
))
1861 if (S_ISREG(sb
.st_mode
))
1871 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
1876 xfd
= ConnectionNumber(display
);
1879 if (select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
1881 errx(1, "select failed");
1886 while(XPending(display
)) {
1887 XNextEvent(display
, &e
);
1888 if (handler
[e
.type
])
1889 handler
[e
.type
](&e
);
1893 XCloseDisplay(display
);