]>
code.delx.au - spectrwm/blob - scrotwm.c
8b59687522a9133b01b38d6ef7b6aa630eaec0f2
3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Much code and ideas taken from dwm under the following license:
20 * MIT/X Consortium License
22 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
23 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
24 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
25 * 2007 Premysl Hruby <dfenze at gmail dot com>
26 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
27 * 2007 Christof Musik <christof at sendfax dot de>
28 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
29 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
30 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, including without limitation
35 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 * and/or sell copies of the Software, and to permit persons to whom the
37 * Software is furnished to do so, subject to the following conditions:
39 * The above copyright notice and this permission notice shall be included in
40 * all copies or substantial portions of the Software.
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
51 #define SWM_VERSION "0.5"
66 #include <sys/types.h>
70 #include <sys/queue.h>
71 #include <sys/param.h>
72 #include <sys/select.h>
74 #include <X11/cursorfont.h>
75 #include <X11/keysym.h>
76 #include <X11/Xatom.h>
78 #include <X11/Xproto.h>
79 #include <X11/Xutil.h>
80 #include <X11/extensions/Xrandr.h>
83 # error XRandR versions less than 1.0 are not supported
88 #define SWM_XRR_HAS_CRTC
92 /* #define SWM_DEBUG */
94 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
95 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
96 #define SWM_D_MISC 0x0001
97 #define SWM_D_EVENT 0x0002
98 #define SWM_D_WS 0x0004
99 #define SWM_D_FOCUS 0x0008
100 #define SWM_D_MOVE 0x0010
101 #define SWM_D_STACK 0x0020
102 #define SWM_D_MOUSE 0x0040
104 u_int32_t swm_debug
= 0
113 #define DPRINTF(x...)
114 #define DNPRINTF(n,x...)
117 #define LENGTH(x) (sizeof x / sizeof x[0])
118 #define MODKEY Mod1Mask
119 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
120 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
122 #define X(r) (r)->g.x
123 #define Y(r) (r)->g.y
124 #define WIDTH(r) (r)->g.w
125 #define HEIGHT(r) (r)->g.h
129 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
132 int xrandr_eventbase
;
133 int ignore_enter
= 0;
134 unsigned int numlockmask
= 0;
138 int cycle_visible
= 0;
141 double dialog_ratio
= .6;
143 #define SWM_BAR_MAX (128)
144 sig_atomic_t bar_alarm
= 0;
152 char *bar_fonts
[] = {
153 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
154 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
160 /* terminal + args */
161 char *spawn_term
[] = { "xterm", NULL
};
162 char *spawn_menu
[] = { "dmenu_run", "-fn", NULL
,
163 "-nb", NULL
, "-nf", NULL
, "-sb", NULL
, "-sf", NULL
, NULL
};
165 #define SWM_MENU_FN (2)
166 #define SWM_MENU_NB (4)
167 #define SWM_MENU_NF (6)
168 #define SWM_MENU_SB (8)
169 #define SWM_MENU_SF (10)
171 /* layout manager data */
172 struct swm_geometry
{
182 /* virtual "screens" */
184 TAILQ_ENTRY(swm_region
) entry
;
185 struct swm_geometry g
;
187 struct workspace
*ws
; /* current workspace on this region */
188 struct swm_screen
*s
; /* screen idx */
190 TAILQ_HEAD(swm_region_list
, swm_region
);
194 TAILQ_ENTRY(ws_win
) entry
;
196 struct swm_geometry g
;
200 struct workspace
*ws
; /* always valid */
201 struct swm_screen
*s
; /* always valid, never changes */
202 XWindowAttributes wa
;
204 TAILQ_HEAD(ws_win_list
, ws_win
);
206 /* layout handlers */
208 void vertical_config(struct workspace
*, int);
209 void vertical_stack(struct workspace
*, struct swm_geometry
*);
210 void horizontal_config(struct workspace
*, int);
211 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
212 void max_stack(struct workspace
*, struct swm_geometry
*);
214 void grabbuttons(struct ws_win
*, int);
217 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
218 void (*l_config
)(struct workspace
*, int);
220 /* stack, configure */
221 { vertical_stack
, vertical_config
},
222 { horizontal_stack
, horizontal_config
},
227 #define SWM_H_SLICE (32)
228 #define SWM_V_SLICE (32)
230 /* define work spaces */
232 int idx
; /* workspace index */
233 int restack
; /* restack on switch */
234 struct layout
*cur_layout
; /* current layout handlers */
235 struct ws_win
*focus
; /* may be NULL */
236 struct swm_region
*r
; /* may be NULL */
237 struct ws_win_list winlist
; /* list of windows in ws */
241 int horizontal_msize
;
249 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
250 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
252 /* physical screen mapping */
253 #define SWM_WS_MAX (10) /* XXX Too small? */
255 int idx
; /* screen index */
256 struct swm_region_list rl
; /* list of regions on this screen */
259 struct workspace ws
[SWM_WS_MAX
];
265 } c
[SWM_S_COLOR_MAX
];
267 struct swm_screen
*screens
;
270 struct ws_win
*cur_focus
= NULL
;
272 /* args to functions */
275 #define SWM_ARG_ID_FOCUSNEXT (0)
276 #define SWM_ARG_ID_FOCUSPREV (1)
277 #define SWM_ARG_ID_FOCUSMAIN (2)
278 #define SWM_ARG_ID_SWAPNEXT (3)
279 #define SWM_ARG_ID_SWAPPREV (4)
280 #define SWM_ARG_ID_SWAPMAIN (5)
281 #define SWM_ARG_ID_MASTERSHRINK (6)
282 #define SWM_ARG_ID_MASTERGROW (7)
283 #define SWM_ARG_ID_MASTERADD (8)
284 #define SWM_ARG_ID_MASTERDEL (9)
285 #define SWM_ARG_ID_STACKRESET (10)
286 #define SWM_ARG_ID_STACKINIT (11)
287 #define SWM_ARG_ID_CYCLEWS_UP (12)
288 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
293 name_to_color(char *colorname
)
297 XColor screen_def
, exact_def
;
298 unsigned long result
= 0;
299 char cname
[32] = "#";
301 cmap
= DefaultColormap(display
, screens
[0].idx
);
302 status
= XAllocNamedColor(display
, cmap
, colorname
,
303 &screen_def
, &exact_def
);
305 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
306 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
310 result
= screen_def
.pixel
;
312 fprintf(stderr
, "color '%s' not found.\n", colorname
);
318 setscreencolor(char *val
, int i
, int c
)
320 if (i
> 0 && i
<= ScreenCount(display
)) {
321 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
322 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
324 } else if (i
== -1) {
325 for (i
= 0; i
< ScreenCount(display
); i
++)
326 screens
[i
].c
[c
].color
= name_to_color(val
);
327 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
330 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
331 i
, ScreenCount(display
));
335 varmatch(char *var
, char *name
, int *index
)
340 i
= strncmp(var
, name
, 255);
347 p
= var
+ strlen(name
);
350 bzero(buf
, sizeof buf
);
352 while (isdigit(*p
) && i
< sizeof buf
)
354 if (i
== 0 || i
>= sizeof buf
|| *p
!= ']')
356 *index
= strtonum(buf
, 0, 99, NULL
);
360 /* conf file stuff */
361 #define SWM_CONF_WS "\n= \t"
362 #define SWM_CONF_FILE "scrotwm.conf"
364 conf_load(char *filename
)
367 char *line
, *cp
, *var
, *val
;
368 size_t len
, lineno
= 0;
371 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
373 if (filename
== NULL
)
376 if ((config
= fopen(filename
, "r")) == NULL
)
379 for (sc
= ScreenCount(display
);;) {
380 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
384 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
390 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
392 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
393 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
396 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
399 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
400 bar_enabled
= atoi(val
);
401 else if (!varmatch(var
, "bar_border", &i
))
402 setscreencolor(var
, i
, SWM_S_COLOR_BAR_BORDER
);
403 else if (!varmatch(var
, "bar_color", &i
))
404 setscreencolor(var
, i
, SWM_S_COLOR_BAR
);
405 else if (!varmatch(var
, "bar_font_color", &i
))
406 setscreencolor(var
, i
, SWM_S_COLOR_BAR_FONT
);
407 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
408 asprintf(&bar_fonts
[0], "%s", val
);
414 if (!varmatch(var
, "color_focus", &i
))
415 setscreencolor(var
, i
, SWM_S_COLOR_FOCUS
);
416 else if (!varmatch(var
, "color_unfocus", &i
))
417 setscreencolor(var
, i
, SWM_S_COLOR_UNFOCUS
);
418 else if (!strncmp(var
, "cycle_empty", strlen("cycle_empty")))
419 cycle_visible
= atoi(val
);
420 else if (!strncmp(var
, "cycle_visible", strlen("cycle_visible")))
421 cycle_visible
= atoi(val
);
427 if (!strncmp(var
, "dialog_ratio",
428 strlen("dialog_ratio"))) {
429 dialog_ratio
= atof(val
);
430 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
437 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
438 asprintf(&spawn_term
[0], "%s", val
); /* XXX args? */
450 errx(1, "invalid conf file entry: %s=%s", var
, val
);
454 bar_print(struct swm_region
*r
, char *s
)
456 XClearWindow(display
, r
->bar_window
);
457 XSetForeground(display
, bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
458 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
, s
,
467 struct swm_region
*r
;
472 if (bar_enabled
== 0)
476 localtime_r(&tmt
, &tm
);
477 strftime(s
, sizeof s
, "%a %b %d %R %Z %Y", &tm
);
478 for (i
= 0; i
< ScreenCount(display
); i
++) {
480 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
481 snprintf(e
, sizeof e
, "%s %d:%d",
482 s
, x
++, r
->ws
->idx
+ 1);
486 XSync(display
, False
);
497 bar_toggle(struct swm_region
*r
, union arg
*args
)
499 struct swm_region
*tmpr
;
500 int i
, j
, sc
= ScreenCount(display
);
502 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
505 for (i
= 0; i
< sc
; i
++)
506 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
507 XUnmapWindow(display
, tmpr
->bar_window
);
509 for (i
= 0; i
< sc
; i
++)
510 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
511 XMapRaised(display
, tmpr
->bar_window
);
513 bar_enabled
= !bar_enabled
;
514 XSync(display
, False
);
515 for (i
= 0; i
< sc
; i
++)
516 for (j
= 0; j
< SWM_WS_MAX
; j
++)
517 screens
[i
].ws
[j
].restack
= 1;
520 /* must be after stack */
521 for (i
= 0; i
< sc
; i
++)
522 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
527 bar_setup(struct swm_region
*r
)
531 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
532 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
538 if (bar_fonts
[i
] == NULL
)
539 errx(1, "couldn't load font");
540 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
542 r
->bar_window
= XCreateSimpleWindow(display
,
543 r
->s
->root
, X(r
), Y(r
), WIDTH(r
) - 2, bar_height
- 2,
544 1, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
545 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
546 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
547 XSetFont(display
, bar_gc
, bar_fs
->fid
);
548 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
550 XMapRaised(display
, r
->bar_window
);
551 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
553 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
554 err(1, "could not install bar_signal");
561 XSetWindowAttributes wa
;
562 struct swm_region
*r
;
565 bzero(&wa
, sizeof wa
);
566 for (i
= 0; i
< ScreenCount(display
); i
++)
567 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
569 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
570 wa
.background_pixel
=
571 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
572 XChangeWindowAttributes(display
, r
->bar_window
,
573 CWBackPixel
| CWBorderPixel
, &wa
);
579 config_win(struct ws_win
*win
)
583 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
584 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
585 ce
.type
= ConfigureNotify
;
586 ce
.display
= display
;
592 ce
.height
= win
->g
.h
;
593 ce
.border_width
= 1; /* XXX store this! */
595 ce
.override_redirect
= False
;
596 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
600 count_win(struct workspace
*ws
, int count_transient
)
605 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
606 if (count_transient
== 0 && win
->floating
)
608 if (count_transient
== 0 && win
->transient
)
612 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
618 quit(struct swm_region
*r
, union arg
*args
)
620 DNPRINTF(SWM_D_MISC
, "quit\n");
625 restart(struct swm_region
*r
, union arg
*args
)
627 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
629 /* disable alarm because the following code may not be interrupted */
631 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
632 errx(1, "can't disable alarm");
634 XCloseDisplay(display
);
635 execvp(start_argv
[0], start_argv
);
636 fprintf(stderr
, "execvp failed\n");
642 root_to_region(Window root
)
644 struct swm_region
*r
;
649 for (i
= 0; i
< ScreenCount(display
); i
++)
650 if (screens
[i
].root
== root
)
653 if (XQueryPointer(display
, screens
[i
].root
,
654 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) == False
) {
655 /* if we can't query the pointer, grab the first region */
656 r
= TAILQ_FIRST(&screens
[i
].rl
);
658 /* otherwise, choose a region based on pointer location */
659 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
660 if (x
> X(r
) && x
< X(r
) + WIDTH(r
) &&
661 y
> Y(r
) && y
< Y(r
) + HEIGHT(r
))
666 r
= TAILQ_FIRST(&screens
[i
].rl
);
672 find_window(Window id
)
677 for (i
= 0; i
< ScreenCount(display
); i
++)
678 for (j
= 0; j
< SWM_WS_MAX
; j
++)
679 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
686 spawn(struct swm_region
*r
, union arg
*args
)
688 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
690 * The double-fork construct avoids zombie processes and keeps the code
691 * clean from stupid signal handlers.
696 close(ConnectionNumber(display
));
698 execvp(args
->argv
[0], args
->argv
);
699 fprintf(stderr
, "execvp failed\n");
709 spawnmenu(struct swm_region
*r
, union arg
*args
)
711 DNPRINTF(SWM_D_MISC
, "spawnmenu\n");
713 spawn_menu
[SWM_MENU_FN
] = bar_fonts
[bar_fidx
];
714 spawn_menu
[SWM_MENU_NB
] = r
->s
->c
[SWM_S_COLOR_BAR
].name
;
715 spawn_menu
[SWM_MENU_NF
] = r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
;
716 spawn_menu
[SWM_MENU_SB
] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
;
717 spawn_menu
[SWM_MENU_SF
] = r
->s
->c
[SWM_S_COLOR_BAR
].name
;
728 DNPRINTF(SWM_D_FOCUS
, "unfocus_all:\n");
730 for (i
= 0; i
< ScreenCount(display
); i
++)
731 for (j
= 0; j
< SWM_WS_MAX
; j
++)
732 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
733 if (win
->ws
->r
== NULL
)
736 XSetWindowBorder(display
, win
->id
,
737 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
739 win
->ws
->focus
= NULL
;
745 focus_win(struct ws_win
*win
)
747 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
? win
->id
: 0);
753 win
->ws
->focus
= win
;
754 if (win
->ws
->r
!= NULL
) {
756 if (!win
->got_focus
) {
757 XSetWindowBorder(display
, win
->id
,
758 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
762 XSetInputFocus(display
, win
->id
,
763 RevertToPointerRoot
, CurrentTime
);
768 switchws(struct swm_region
*r
, union arg
*args
)
771 struct swm_region
*this_r
, *other_r
;
773 struct workspace
*new_ws
, *old_ws
;
777 new_ws
= &this_r
->s
->ws
[wsid
];
779 DNPRINTF(SWM_D_WS
, "switchws screen %d region %dx%d+%d+%d: "
780 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
783 if (new_ws
== old_ws
)
788 /* if the other workspace is hidden, switch windows */
789 /* map new window first to prevent ugly blinking */
790 TAILQ_FOREACH(win
, &new_ws
->winlist
, entry
)
791 XMapRaised(display
, win
->id
);
793 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
794 XUnmapWindow(display
, win
->id
);
799 other_r
->ws
= old_ws
;
808 focus_win(new_ws
->focus
);
814 cyclews(struct swm_region
*r
, union arg
*args
)
817 struct swm_screen
*s
= r
->s
;
819 DNPRINTF(SWM_D_WS
, "cyclews id %d "
820 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
821 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
827 case SWM_ARG_ID_CYCLEWS_UP
:
828 if (a
.id
< SWM_WS_MAX
- 1)
833 case SWM_ARG_ID_CYCLEWS_DOWN
:
837 a
.id
= SWM_WS_MAX
- 1;
843 if (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
))
845 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
849 } while (a
.id
!= r
->ws
->idx
);
853 swapwin(struct swm_region
*r
, union arg
*args
)
855 struct ws_win
*target
;
856 struct ws_win_list
*wl
;
859 DNPRINTF(SWM_D_WS
, "swapwin id %d "
860 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
861 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
862 if (cur_focus
== NULL
)
865 wl
= &cur_focus
->ws
->winlist
;
868 case SWM_ARG_ID_SWAPPREV
:
869 target
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
870 TAILQ_REMOVE(wl
, cur_focus
, entry
);
872 TAILQ_INSERT_TAIL(wl
, cur_focus
, entry
);
874 TAILQ_INSERT_BEFORE(target
, cur_focus
, entry
);
876 case SWM_ARG_ID_SWAPNEXT
:
877 target
= TAILQ_NEXT(cur_focus
, entry
);
878 TAILQ_REMOVE(wl
, cur_focus
, entry
);
880 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
882 TAILQ_INSERT_AFTER(wl
, target
, cur_focus
, entry
);
884 case SWM_ARG_ID_SWAPMAIN
:
885 target
= TAILQ_FIRST(wl
);
886 if (target
== cur_focus
)
888 TAILQ_REMOVE(wl
, target
, entry
);
889 TAILQ_INSERT_BEFORE(cur_focus
, target
, entry
);
890 TAILQ_REMOVE(wl
, cur_focus
, entry
);
891 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
894 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
903 focus(struct swm_region
*r
, union arg
*args
)
905 struct ws_win
*winfocus
, *winlostfocus
;
906 struct ws_win_list
*wl
;
908 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
909 if (cur_focus
== NULL
)
912 wl
= &cur_focus
->ws
->winlist
;
914 winlostfocus
= cur_focus
;
917 case SWM_ARG_ID_FOCUSPREV
:
918 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
919 if (winfocus
== NULL
)
920 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
923 case SWM_ARG_ID_FOCUSNEXT
:
924 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
925 if (winfocus
== NULL
)
926 winfocus
= TAILQ_FIRST(wl
);
929 case SWM_ARG_ID_FOCUSMAIN
:
930 winfocus
= TAILQ_FIRST(wl
);
937 if (winfocus
== winlostfocus
)
941 XSync(display
, False
);
945 cycle_layout(struct swm_region
*r
, union arg
*args
)
947 struct workspace
*ws
= r
->ws
;
949 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
952 if (ws
->cur_layout
->l_stack
== NULL
)
953 ws
->cur_layout
= &layouts
[0];
960 stack_config(struct swm_region
*r
, union arg
*args
)
962 struct workspace
*ws
= r
->ws
;
964 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
967 if (ws
->cur_layout
->l_config
!= NULL
)
968 ws
->cur_layout
->l_config(ws
, args
->id
);
970 if (args
->id
!= SWM_ARG_ID_STACKINIT
);
976 struct swm_geometry g
;
977 struct swm_region
*r
;
980 DNPRINTF(SWM_D_STACK
, "stack\n");
982 for (i
= 0; i
< ScreenCount(display
); i
++) {
984 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
985 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
986 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
988 /* start with screen geometry, adjust for bar */
998 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
1001 XSync(display
, False
);
1005 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
1010 bzero(&wc
, sizeof wc
);
1011 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
1012 wc
.border_width
= 1;
1013 if (win
->transient
) {
1014 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
1015 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
1017 wc
.width
= win
->g
.w
;
1018 wc
.height
= win
->g
.h
;
1019 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1020 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1022 DNPRINTF(SWM_D_STACK
, "stack_floater: win %lu x %d y %d w %d h %d\n",
1023 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1025 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1030 vertical_config(struct workspace
*ws
, int id
)
1032 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
1035 case SWM_ARG_ID_STACKRESET
:
1036 case SWM_ARG_ID_STACKINIT
:
1037 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
1038 ws
->l_state
.vertical_mwin
= 1;
1040 case SWM_ARG_ID_MASTERSHRINK
:
1041 if (ws
->l_state
.vertical_msize
> 1)
1042 ws
->l_state
.vertical_msize
--;
1044 case SWM_ARG_ID_MASTERGROW
:
1045 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
1046 ws
->l_state
.vertical_msize
++;
1048 case SWM_ARG_ID_MASTERADD
:
1049 ws
->l_state
.vertical_mwin
++;
1051 case SWM_ARG_ID_MASTERDEL
:
1052 if (ws
->l_state
.vertical_mwin
> 0)
1053 ws
->l_state
.vertical_mwin
--;
1061 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1063 struct swm_geometry gg
= *g
;
1064 struct ws_win
*win
, *winfocus
;
1065 int i
, j
, split
, colno
, hrh
, winno
, main_width
;
1068 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1070 if ((winno
= count_win(ws
, 0)) == 0)
1073 if (ws
->focus
== NULL
)
1074 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1075 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1077 if (ws
->l_state
.vertical_mwin
&&
1078 winno
> ws
->l_state
.vertical_mwin
) {
1079 split
= ws
->l_state
.vertical_mwin
;
1081 main_width
= (g
->w
/ SWM_V_SLICE
) *
1082 ws
->l_state
.vertical_msize
;
1092 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1093 if (split
&& i
== split
) {
1094 colno
= winno
- split
;
1095 hrh
= (g
->h
/ colno
);
1096 gg
.x
+= main_width
+ 2;
1097 gg
.w
= g
->w
- (main_width
+ 2);
1102 gg
.h
= (hrh
+ (g
->h
- (colno
* hrh
)));
1110 if (win
->transient
!= 0 || win
->floating
!= 0)
1111 stack_floater(win
, ws
->r
);
1113 bzero(&wc
, sizeof wc
);
1114 wc
.border_width
= 1;
1115 win
->g
.x
= wc
.x
= gg
.x
;
1116 win
->g
.y
= wc
.y
= gg
.y
;
1117 win
->g
.w
= wc
.width
= gg
.w
;
1118 win
->g
.h
= wc
.height
= gg
.h
;
1119 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1120 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1123 XMapRaised(display
, win
->id
);
1129 focus_win(winfocus
); /* has to be done outside of the loop */
1134 horizontal_config(struct workspace
*ws
, int id
)
1136 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
1139 case SWM_ARG_ID_STACKRESET
:
1140 case SWM_ARG_ID_STACKINIT
:
1141 ws
->l_state
.horizontal_mwin
= 1;
1142 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
1144 case SWM_ARG_ID_MASTERSHRINK
:
1145 if (ws
->l_state
.horizontal_msize
> 1)
1146 ws
->l_state
.horizontal_msize
--;
1148 case SWM_ARG_ID_MASTERGROW
:
1149 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
1150 ws
->l_state
.horizontal_msize
++;
1152 case SWM_ARG_ID_MASTERADD
:
1153 ws
->l_state
.horizontal_mwin
++;
1155 case SWM_ARG_ID_MASTERDEL
:
1156 if (ws
->l_state
.horizontal_mwin
> 0)
1157 ws
->l_state
.horizontal_mwin
--;
1165 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1167 struct swm_geometry gg
= *g
;
1168 struct ws_win
*win
, *winfocus
;
1169 int i
, j
, split
, rowno
, hrw
, winno
, main_height
;
1172 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
1174 if ((winno
= count_win(ws
, 0)) == 0)
1177 if (ws
->focus
== NULL
)
1178 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1179 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1181 if (ws
->l_state
.horizontal_mwin
&&
1182 winno
> ws
->l_state
.horizontal_mwin
) {
1183 split
= ws
->l_state
.horizontal_mwin
;
1185 main_height
= (g
->h
/ SWM_V_SLICE
) *
1186 ws
->l_state
.horizontal_msize
;
1196 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1197 if (split
&& i
== split
) {
1198 rowno
= winno
- split
;
1199 hrw
= (g
->w
/ rowno
);
1200 gg
.y
+= main_height
+ 2;
1201 gg
.h
= g
->h
- (main_height
+ 2);
1206 gg
.w
= (hrw
+ (g
->w
- (rowno
* hrw
)));
1213 if (win
->transient
!= 0 || win
->floating
!= 0)
1214 stack_floater(win
, ws
->r
);
1216 bzero(&wc
, sizeof wc
);
1217 wc
.border_width
= 1;
1218 win
->g
.x
= wc
.x
= gg
.x
;
1219 win
->g
.y
= wc
.y
= gg
.y
;
1220 win
->g
.w
= wc
.width
= gg
.w
;
1221 win
->g
.h
= wc
.height
= gg
.h
;
1222 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1223 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1226 XMapRaised(display
, win
->id
);
1232 focus_win(winfocus
); /* this has to be done outside of the loop */
1235 /* fullscreen view */
1237 max_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1239 struct swm_geometry gg
= *g
;
1240 struct ws_win
*win
, *winfocus
;
1243 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
1245 if (count_win(ws
, 0) == 0)
1248 if (ws
->focus
== NULL
)
1249 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1250 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1252 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1253 if (win
->transient
!= 0 || win
->floating
!= 0) {
1254 if (win
== ws
->focus
) {
1256 stack_floater(win
, ws
->r
);
1257 XMapRaised(display
, win
->id
);
1259 XUnmapWindow(display
, win
->id
);
1261 bzero(&wc
, sizeof wc
);
1262 wc
.border_width
= 1;
1263 win
->g
.x
= wc
.x
= gg
.x
;
1264 win
->g
.y
= wc
.y
= gg
.y
;
1265 win
->g
.w
= wc
.width
= gg
.w
;
1266 win
->g
.h
= wc
.height
= gg
.h
;
1267 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1268 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1270 if (win
== ws
->focus
) {
1271 XMapRaised(display
, win
->id
);
1273 XUnmapWindow(display
, win
->id
);
1278 focus_win(winfocus
); /* has to be done outside of the loop */
1282 send_to_ws(struct swm_region
*r
, union arg
*args
)
1284 int wsid
= args
->id
;
1285 struct ws_win
*win
= cur_focus
;
1286 struct workspace
*ws
, *nws
;
1288 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
1291 nws
= &win
->s
->ws
[wsid
];
1293 XUnmapWindow(display
, win
->id
);
1295 /* find a window to focus */
1296 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1297 if (ws
->focus
== NULL
)
1298 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1299 if (ws
->focus
== win
)
1302 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1304 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
1307 if (count_win(nws
, 1) == 1)
1315 /* key definitions */
1319 void (*func
)(struct swm_region
*r
, union arg
*);
1322 /* modifier key function argument */
1323 { MODKEY
, XK_space
, cycle_layout
, {0} },
1324 { MODKEY
| ShiftMask
, XK_space
, stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
1325 { MODKEY
, XK_h
, stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
1326 { MODKEY
, XK_l
, stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
1327 { MODKEY
, XK_comma
, stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
1328 { MODKEY
, XK_period
, stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
1329 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
1330 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1331 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1332 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
1333 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
1334 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
1335 { MODKEY
, XK_p
, spawnmenu
, {.argv
= spawn_menu
} },
1336 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
1337 { MODKEY
, XK_q
, restart
, {0} },
1338 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
1339 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
1340 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
1341 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
1342 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
1343 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
1344 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
1345 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
1346 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
1347 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
1348 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
1349 { MODKEY
, XK_Right
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
1350 { MODKEY
, XK_Left
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
1351 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
1352 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
1353 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
1354 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
1355 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
1356 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
1357 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
1358 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
1359 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
1360 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
1361 { MODKEY
, XK_b
, bar_toggle
, {0} },
1362 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1363 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1367 click(struct swm_region
*r
, union arg
*args
)
1369 DNPRINTF(SWM_D_MOUSE
, "click: button: %d\n", args
->id
);
1384 enum { client_click
, root_click
};
1386 unsigned int action
;
1388 unsigned int button
;
1389 void (*func
)(struct swm_region
*r
, union arg
*);
1392 /* action key mouse button func args */
1393 { client_click
, MODKEY
, Button1
, click
, {.id
=Button1
} },
1397 updatenumlockmask(void)
1400 XModifierKeymap
*modmap
;
1402 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
1404 modmap
= XGetModifierMapping(display
);
1405 for (i
= 0; i
< 8; i
++)
1406 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1407 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1408 == XKeysymToKeycode(display
, XK_Num_Lock
))
1409 numlockmask
= (1 << i
);
1411 XFreeModifiermap(modmap
);
1417 unsigned int i
, j
, k
;
1419 unsigned int modifiers
[] =
1420 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
1422 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
1423 updatenumlockmask();
1425 for (k
= 0; k
< ScreenCount(display
); k
++) {
1426 if (TAILQ_EMPTY(&screens
[k
].rl
))
1428 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
1429 for (i
= 0; i
< LENGTH(keys
); i
++) {
1430 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
1431 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1432 XGrabKey(display
, code
,
1433 keys
[i
].mod
| modifiers
[j
],
1434 screens
[k
].root
, True
,
1435 GrabModeAsync
, GrabModeAsync
);
1441 grabbuttons(struct ws_win
*win
, int focused
)
1444 unsigned int modifiers
[] =
1445 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1447 updatenumlockmask();
1448 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
1450 for (i
= 0; i
< LENGTH(buttons
); i
++)
1451 if (buttons
[i
].action
== client_click
)
1452 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1453 XGrabButton(display
, buttons
[i
].button
,
1454 buttons
[i
].mask
| modifiers
[j
],
1455 win
->id
, False
, BUTTONMASK
,
1456 GrabModeAsync
, GrabModeSync
, None
,
1459 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
1460 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
1466 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
1474 XKeyEvent
*ev
= &e
->xkey
;
1476 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
1478 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
1479 for (i
= 0; i
< LENGTH(keys
); i
++)
1480 if (keysym
== keys
[i
].keysym
1481 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1483 keys
[i
].func(root_to_region(ev
->root
),
1488 buttonpress(XEvent
*e
)
1490 XButtonPressedEvent
*ev
= &e
->xbutton
;
1495 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
1497 action
= root_click
;
1498 if ((win
= find_window(ev
->window
)) == NULL
)
1502 action
= client_click
;
1505 for (i
= 0; i
< LENGTH(buttons
); i
++)
1506 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
1507 buttons
[i
].button
== ev
->button
&&
1508 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
1509 buttons
[i
].func(root_to_region(ev
->root
),
1514 set_win_state(struct ws_win
*win
, long state
)
1516 long data
[] = {state
, None
};
1518 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
1520 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1521 (unsigned char *)data
, 2);
1525 manage_window(Window id
, struct workspace
*ws
)
1531 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1533 return (win
); /* already being managed */
1536 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
1537 errx(1, "calloc: failed to allocate memory for new window");
1541 win
->s
= ws
->r
->s
; /* this never changes */
1542 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
1544 /* make new win focused */
1547 XGetTransientForHint(display
, win
->id
, &trans
);
1549 win
->transient
= trans
;
1550 DNPRINTF(SWM_D_MISC
, "manage_window: win %u transient %u\n",
1551 (unsigned)win
->id
, win
->transient
);
1553 XGetWindowAttributes(display
, id
, &win
->wa
);
1554 win
->g
.w
= win
->wa
.width
;
1555 win
->g
.h
= win
->wa
.height
;
1556 win
->g
.x
= win
->wa
.x
;
1557 win
->g
.y
= win
->wa
.y
;
1559 /* XXX make this a table */
1560 bzero(&ch
, sizeof ch
);
1561 if (XGetClassHint(display
, win
->id
, &ch
)) {
1562 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1563 if (!strcmp(ch
.res_class
, "MPlayer") && !strcmp(ch
.res_name
, "xv")) {
1567 XFree(ch
.res_class
);
1572 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
1573 PropertyChangeMask
| StructureNotifyMask
);
1575 set_win_state(win
, NormalState
);
1581 configurerequest(XEvent
*e
)
1583 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
1588 if ((win
= find_window(ev
->window
)) == NULL
)
1592 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
1594 bzero(&wc
, sizeof wc
);
1597 wc
.width
= ev
->width
;
1598 wc
.height
= ev
->height
;
1599 wc
.border_width
= ev
->border_width
;
1600 wc
.sibling
= ev
->above
;
1601 wc
.stack_mode
= ev
->detail
;
1602 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
1604 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
1606 if (win
->floating
) {
1607 if (ev
->value_mask
& CWX
)
1609 if (ev
->value_mask
& CWY
)
1611 if (ev
->value_mask
& CWWidth
)
1612 win
->g
.w
= ev
->width
;
1613 if (ev
->value_mask
& CWHeight
)
1614 win
->g
.h
= ev
->height
;
1615 if (win
->ws
->r
!= NULL
) {
1616 /* this seems to be full screen */
1617 if (win
->g
.w
> WIDTH(win
->ws
->r
)) {
1622 if (win
->g
.h
> HEIGHT(win
->ws
->r
)) {
1628 if ((ev
->value_mask
& (CWX
|CWY
)) &&
1629 !(ev
->value_mask
& (CWWidth
|CWHeight
)))
1631 XMoveResizeWindow(display
, win
->id
,
1632 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
1639 configurenotify(XEvent
*e
)
1641 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
1642 e
->xconfigure
.window
);
1646 destroynotify(XEvent
*e
)
1649 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
1650 struct workspace
*ws
;
1652 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
1654 if ((win
= find_window(ev
->window
)) != NULL
) {
1656 /* find a window to focus */
1657 if (ws
->focus
== win
)
1658 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1659 if (ws
->focus
== NULL
)
1660 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1661 if (ws
->focus
== win
)
1663 if (cur_focus
== win
)
1664 focus_win(ws
->focus
);
1666 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1667 set_win_state(win
, WithdrawnState
);
1674 enternotify(XEvent
*e
)
1676 XCrossingEvent
*ev
= &e
->xcrossing
;
1679 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
1682 /* eat event(r) to prevent autofocus */
1687 if ((win
= find_window(ev
->window
)) != NULL
)
1694 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", e
->xfocus
.window
);
1698 mappingnotify(XEvent
*e
)
1700 XMappingEvent
*ev
= &e
->xmapping
;
1702 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
1704 XRefreshKeyboardMapping(ev
);
1705 if (ev
->request
== MappingKeyboard
)
1710 maprequest(XEvent
*e
)
1712 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1713 XWindowAttributes wa
;
1714 struct swm_region
*r
;
1716 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
1717 e
->xmaprequest
.window
);
1719 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
1721 if (wa
.override_redirect
)
1723 r
= root_to_region(wa
.root
);
1724 manage_window(e
->xmaprequest
.window
, r
->ws
);
1729 propertynotify(XEvent
*e
)
1731 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
1732 e
->xproperty
.window
);
1736 unmapnotify(XEvent
*e
)
1738 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
1742 visibilitynotify(XEvent
*e
)
1745 struct swm_region
*r
;
1747 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
1748 e
->xvisibility
.window
);
1749 if (e
->xvisibility
.state
== VisibilityUnobscured
)
1750 for (i
= 0; i
< ScreenCount(display
); i
++)
1751 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1752 if (e
->xvisibility
.window
== r
->bar_window
)
1756 void (*handler
[LASTEvent
])(XEvent
*) = {
1758 [KeyPress
] = keypress
,
1759 [ButtonPress
] = buttonpress
,
1760 [ConfigureRequest
] = configurerequest
,
1761 [ConfigureNotify
] = configurenotify
,
1762 [DestroyNotify
] = destroynotify
,
1763 [EnterNotify
] = enternotify
,
1764 [FocusIn
] = focusin
,
1765 [MappingNotify
] = mappingnotify
,
1766 [MapRequest
] = maprequest
,
1767 [PropertyNotify
] = propertynotify
,
1768 [UnmapNotify
] = unmapnotify
,
1769 [VisibilityNotify
] = visibilitynotify
,
1773 xerror_start(Display
*d
, XErrorEvent
*ee
)
1780 xerror(Display
*d
, XErrorEvent
*ee
)
1782 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1790 xerrorxlib
= XSetErrorHandler(xerror_start
);
1792 /* this causes an error if some other window manager is running */
1793 XSelectInput(display
, DefaultRootWindow(display
),
1794 SubstructureRedirectMask
);
1795 XSync(display
, False
);
1799 XSetErrorHandler(xerror
);
1800 XSync(display
, False
);
1809 unsigned char *p
= NULL
;
1810 unsigned long n
, extra
;
1813 astate
= XInternAtom(display
, "WM_STATE", False
);
1814 status
= XGetWindowProperty(display
, w
, astate
, 0L, 2L, False
, astate
,
1815 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
1816 if (status
!= Success
)
1825 new_region(struct swm_screen
*s
, struct workspace
*ws
,
1826 int x
, int y
, int w
, int h
)
1828 struct swm_region
*r
;
1830 DNPRINTF(SWM_D_MISC
, "new region on screen %d: %dx%d (%d, %d)\n",
1831 s
->idx
, x
, y
, w
, h
);
1833 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
1834 errx(1, "calloc: failed to allocate memory for screen");
1843 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
1850 #ifdef SWM_XRR_HAS_CRTC
1852 XRRScreenResources
*sr
;
1854 #endif /* SWM_XRR_HAS_CRTC */
1855 Window d1
, d2
, *wins
= NULL
;
1856 XWindowAttributes wa
;
1857 struct swm_region
*r
;
1859 int errorbase
, major
, minor
;
1860 int ncrtc
= 0, w
= 0;
1862 struct workspace
*ws
;
1864 if ((screens
= calloc(ScreenCount(display
),
1865 sizeof(struct swm_screen
))) == NULL
)
1866 errx(1, "calloc: screens");
1868 /* map physical screens */
1869 for (i
= 0; i
< ScreenCount(display
); i
++) {
1870 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
1872 TAILQ_INIT(&screens
[i
].rl
);
1873 screens
[i
].root
= RootWindow(display
, i
);
1875 /* set default colors */
1876 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
1877 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
1878 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
1879 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
1880 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
1882 /* init all workspaces */
1883 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
1884 ws
= &screens
[i
].ws
[j
];
1889 TAILQ_INIT(&ws
->winlist
);
1891 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
1892 if (layouts
[k
].l_config
!= NULL
)
1893 layouts
[k
].l_config(ws
,
1894 SWM_ARG_ID_STACKINIT
);
1895 ws
->cur_layout
= &layouts
[0];
1898 /* map virtual screens onto physical screens */
1899 screens
[i
].xrandr_support
= XRRQueryExtension(display
,
1900 &xrandr_eventbase
, &errorbase
);
1901 if (screens
[i
].xrandr_support
)
1902 if (XRRQueryVersion(display
, &major
, &minor
) &&
1904 screens
[i
].xrandr_support
= 0;
1906 #if 0 /* not ready for dynamic screen changes */
1907 if (screens
[i
].xrandr_support
)
1908 XRRSelectInput(display
,
1910 RRScreenChangeNotifyMask
);
1913 /* grab existing windows (before we build the bars)*/
1914 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
1917 #ifdef SWM_XRR_HAS_CRTC
1918 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
1920 new_region(&screens
[i
], &screens
[i
].ws
[w
],
1921 0, 0, DisplayWidth(display
, i
),
1922 DisplayHeight(display
, i
));
1926 for (c
= 0; c
< ncrtc
; c
++) {
1927 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
1928 if (ci
->noutput
== 0)
1931 if (ci
!= NULL
&& ci
->mode
== None
)
1932 new_region(&screens
[i
], &screens
[i
].ws
[w
], 0, 0,
1933 DisplayWidth(display
, i
),
1934 DisplayHeight(display
, i
));
1936 new_region(&screens
[i
], &screens
[i
].ws
[w
],
1937 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
1940 XRRFreeCrtcInfo(ci
);
1941 XRRFreeScreenResources(sr
);
1943 new_region(&screens
[i
], &screens
[i
].ws
[w
], 0, 0,
1944 DisplayWidth(display
, i
),
1945 DisplayHeight(display
, i
));
1946 #endif /* SWM_XRR_HAS_CRTC */
1948 /* attach windows to a region */
1949 /* normal windows */
1950 if ((r
= TAILQ_FIRST(&screens
[i
].rl
)) == NULL
)
1951 errx(1, "no regions on screen %d", i
);
1953 for (i
= 0; i
< no
; i
++) {
1954 XGetWindowAttributes(display
, wins
[i
], &wa
);
1955 if (!XGetWindowAttributes(display
, wins
[i
], &wa
) ||
1956 wa
.override_redirect
||
1957 XGetTransientForHint(display
, wins
[i
], &d1
))
1960 if (wa
.map_state
== IsViewable
||
1961 getstate(wins
[i
]) == NormalState
)
1962 manage_window(wins
[i
], r
->ws
);
1964 /* transient windows */
1965 for (i
= 0; i
< no
; i
++) {
1966 if (!XGetWindowAttributes(display
, wins
[i
], &wa
))
1969 if (XGetTransientForHint(display
, wins
[i
], &d1
) &&
1970 (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) ==
1972 manage_window(wins
[i
], r
->ws
);
1982 main(int argc
, char *argv
[])
1985 char conf
[PATH_MAX
], *cfile
= NULL
;
1992 fprintf(stderr
, "Welcome to scrotwm V%s\n", SWM_VERSION
);
1993 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1994 warnx("no locale support");
1996 if (!(display
= XOpenDisplay(0)))
1997 errx(1, "can not open display");
2000 errx(1, "other wm running");
2002 astate
= XInternAtom(display
, "WM_STATE", False
);
2004 /* look for local and global conf file */
2005 pwd
= getpwuid(getuid());
2007 errx(1, "invalid user %d", getuid());
2011 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
2012 if (stat(conf
, &sb
) != -1) {
2013 if (S_ISREG(sb
.st_mode
))
2016 /* try global conf file */
2017 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
2018 if (!stat(conf
, &sb
))
2019 if (S_ISREG(sb
.st_mode
))
2026 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2031 xfd
= ConnectionNumber(display
);
2035 if (select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
2037 errx(1, "select failed");
2042 while(XPending(display
)) {
2043 XNextEvent(display
, &e
);
2044 if (handler
[e
.type
])
2045 handler
[e
.type
](&e
);
2049 XCloseDisplay(display
);