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"
67 #include <sys/types.h>
71 #include <sys/queue.h>
72 #include <sys/param.h>
73 #include <sys/select.h>
75 #include <X11/cursorfont.h>
76 #include <X11/keysym.h>
77 #include <X11/Xatom.h>
79 #include <X11/Xproto.h>
80 #include <X11/Xutil.h>
81 #include <X11/extensions/Xrandr.h>
84 # error XRandR versions less than 1.0 are not supported
89 #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
103 #define SWM_D_MOUSE 0x0040
105 u_int32_t swm_debug = 0
114 #define DPRINTF(x...)
115 #define DNPRINTF(n,x...)
118 #define LENGTH(x) (sizeof x / sizeof x[0])
119 #define MODKEY Mod1Mask
120 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
121 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
123 #define X(r) (r)->g.x
124 #define Y(r) (r)->g.y
125 #define WIDTH(r) (r)->g.w
126 #define HEIGHT(r) (r)->g.h
130 int (*xerrorxlib)(Display *, XErrorEvent *);
133 int xrandr_eventbase;
134 int ignore_enter = 0;
135 unsigned int numlockmask = 0;
139 int cycle_visible = 0;
142 double dialog_ratio = .6;
144 #define SWM_BAR_MAX (128)
145 char *bar_argv[] = { NULL, NULL };
147 char bar_ext[SWM_BAR_MAX];
148 sig_atomic_t bar_alarm = 0;
151 int bar_extra_running = 0;
159 char *bar_fonts[] = {
160 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
161 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
165 /* terminal + args */
166 char *spawn_term[] = { "xterm", NULL };
167 char *spawn_menu[] = { "dmenu_run", "-fn", NULL,
168 "-nb", NULL, "-nf", NULL, "-sb", NULL, "-sf", NULL, NULL };
170 #define SWM_MENU_FN (2)
171 #define SWM_MENU_NB (4)
172 #define SWM_MENU_NF (6)
173 #define SWM_MENU_SB (8)
174 #define SWM_MENU_SF (10)
176 /* layout manager data */
177 struct swm_geometry {
187 /* virtual "screens" */
189 TAILQ_ENTRY(swm_region) entry;
190 struct swm_geometry g;
192 struct workspace *ws; /* current workspace on this region */
193 struct swm_screen *s; /* screen idx */
195 TAILQ_HEAD(swm_region_list, swm_region);
198 TAILQ_ENTRY(ws_win) entry;
200 struct swm_geometry g;
204 struct workspace *ws; /* always valid */
205 struct swm_screen *s; /* always valid, never changes */
206 XWindowAttributes wa;
208 TAILQ_HEAD(ws_win_list, ws_win);
210 /* layout handlers */
212 void vertical_config(struct workspace *, int);
213 void vertical_stack(struct workspace *, struct swm_geometry *);
214 void horizontal_config(struct workspace *, int);
215 void horizontal_stack(struct workspace *, struct swm_geometry *);
216 void max_stack(struct workspace *, struct swm_geometry *);
218 void grabbuttons(struct ws_win *, int);
221 void (*l_stack)(struct workspace *, struct swm_geometry *);
222 void (*l_config)(struct workspace *, int);
224 /* stack, configure */
225 { vertical_stack, vertical_config},
226 { horizontal_stack, horizontal_config},
231 #define SWM_H_SLICE (32)
232 #define SWM_V_SLICE (32)
234 /* define work spaces */
236 int idx; /* workspace index */
237 int restack; /* restack on switch */
238 struct layout *cur_layout; /* current layout handlers */
239 struct ws_win *focus; /* may be NULL */
240 struct swm_region *r; /* may be NULL */
241 struct ws_win_list winlist; /* list of windows in ws */
245 int horizontal_msize;
252 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
253 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
255 /* physical screen mapping */
256 #define SWM_WS_MAX (10) /* XXX Too small? */
258 int idx; /* screen index */
259 struct swm_region_list rl; /* list of regions on this screen */
262 struct workspace ws[SWM_WS_MAX];
268 } c[SWM_S_COLOR_MAX];
270 struct swm_screen *screens;
273 struct ws_win *cur_focus = NULL;
275 /* args to functions */
278 #define SWM_ARG_ID_FOCUSNEXT (0)
279 #define SWM_ARG_ID_FOCUSPREV (1)
280 #define SWM_ARG_ID_FOCUSMAIN (2)
281 #define SWM_ARG_ID_SWAPNEXT (3)
282 #define SWM_ARG_ID_SWAPPREV (4)
283 #define SWM_ARG_ID_SWAPMAIN (5)
284 #define SWM_ARG_ID_MASTERSHRINK (6)
285 #define SWM_ARG_ID_MASTERGROW (7)
286 #define SWM_ARG_ID_MASTERADD (8)
287 #define SWM_ARG_ID_MASTERDEL (9)
288 #define SWM_ARG_ID_STACKRESET (10)
289 #define SWM_ARG_ID_STACKINIT (11)
290 #define SWM_ARG_ID_CYCLEWS_UP (12)
291 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
296 name_to_color(char *colorname)
300 XColor screen_def, exact_def;
301 unsigned long result = 0;
302 char cname[32] = "#";
304 cmap = DefaultColormap(display, screens[0].idx);
305 status = XAllocNamedColor(display, cmap, colorname,
306 &screen_def, &exact_def);
308 strlcat(cname, colorname + 2, sizeof cname - 1);
309 status = XAllocNamedColor(display, cmap, cname, &screen_def,
313 result = screen_def.pixel;
315 fprintf(stderr, "color '%s' not found.\n", colorname);
321 setscreencolor(char *val, int i, int c)
323 if (i > 0 && i <= ScreenCount(display)) {
324 screens[i - 1].c[c].color = name_to_color(val);
325 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
327 } else if (i == -1) {
328 for (i = 0; i < ScreenCount(display); i++)
329 screens[i].c[c].color = name_to_color(val);
330 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
333 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
334 i, ScreenCount(display));
338 varmatch(char *var, char *name, int *index)
343 i = strncmp(var, name, 255);
350 p = var + strlen(name);
353 bzero(buf, sizeof buf);
355 while (isdigit(*p) && i < sizeof buf)
357 if (i == 0 || i >= sizeof buf || *p != ']')
359 *index = strtonum(buf, 0, 99, NULL);
363 /* conf file stuff */
364 #define SWM_CONF_WS "\n= \t"
365 #define SWM_CONF_FILE "scrotwm.conf"
367 conf_load(char *filename)
370 char *line, *cp, *var, *val;
371 size_t len, lineno = 0;
374 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
376 if (filename == NULL)
379 if ((config = fopen(filename, "r")) == NULL)
382 for (sc = ScreenCount(display);;) {
383 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
387 cp += (long)strspn(cp, SWM_CONF_WS);
393 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
395 cp += (long)strspn(cp, SWM_CONF_WS);
396 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
399 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
402 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
403 bar_enabled = atoi(val);
404 else if (!varmatch(var, "bar_border", &i))
405 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
406 else if (!varmatch(var, "bar_color", &i))
407 setscreencolor(val, i, SWM_S_COLOR_BAR);
408 else if (!varmatch(var, "bar_font_color", &i))
409 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
410 else if (!strncmp(var, "bar_font", strlen("bar_font")))
411 asprintf(&bar_fonts[0], "%s", val);
412 else if (!strncmp(var, "bar_action", strlen("bar_action")))
413 asprintf(&bar_argv[0], "%s", val);
419 if (!varmatch(var, "color_focus", &i))
420 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
421 else if (!varmatch(var, "color_unfocus", &i))
422 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
423 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
424 cycle_visible = atoi(val);
425 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
426 cycle_visible = atoi(val);
432 if (!strncmp(var, "dialog_ratio",
433 strlen("dialog_ratio"))) {
434 dialog_ratio = atof(val);
435 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
442 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
443 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
455 errx(1, "invalid conf file entry: %s=%s", var, val);
459 socket_setnonblock(int fd)
463 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
464 err(1, "fcntl F_GETFL");
466 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
467 err(1, "fcntl F_SETFL");
471 bar_print(struct swm_region *r, char *s)
473 XClearWindow(display, r->bar_window);
474 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
475 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
484 bzero(bar_pipe, sizeof bar_pipe);
487 kill(bar_pid, SIGTERM);
490 strlcpy(bar_ext, "", sizeof bar_ext);
499 struct swm_region *r;
503 char loc[SWM_BAR_MAX];
506 if (bar_enabled == 0)
509 if (bar_extra && bar_extra_running) {
510 /* ignore short reads; it'll correct itself */
511 while ((b = fgetln(stdin, &len)) != NULL)
512 if (b && b[len - 1] == '\n') {
514 strlcpy(bar_ext, b, sizeof bar_ext);
516 if (b == NULL && errno != EAGAIN) {
517 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
518 errno, strerror(errno));
522 strlcpy(bar_ext, "", sizeof bar_ext);
525 localtime_r(&tmt, &tm);
526 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
527 for (i = 0; i < ScreenCount(display); i++) {
529 TAILQ_FOREACH(r, &screens[i].rl, entry) {
530 snprintf(loc, sizeof loc, "%s %d:%d %s",
531 s, x++, r->ws->idx + 1, bar_ext);
535 XSync(display, False);
547 bar_toggle(struct swm_region *r, union arg *args)
549 struct swm_region *tmpr;
550 int i, j, sc = ScreenCount(display);
552 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
555 for (i = 0; i < sc; i++)
556 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
557 XUnmapWindow(display, tmpr->bar_window);
559 for (i = 0; i < sc; i++)
560 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
561 XMapRaised(display, tmpr->bar_window);
563 bar_enabled = !bar_enabled;
564 XSync(display, False);
565 for (i = 0; i < sc; i++)
566 for (j = 0; j < SWM_WS_MAX; j++)
567 screens[i].ws[j].restack = 1;
570 /* must be after stack */
571 for (i = 0; i < sc; i++)
572 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
579 XSetWindowAttributes wa;
580 struct swm_region *r;
583 /* do this here because the conf file is in memory */
584 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
585 /* launch external status app */
586 bar_extra_running = 1;
587 if (pipe(bar_pipe) == -1)
588 err(1, "pipe error");
589 socket_setnonblock(bar_pipe[0]);
590 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
591 if (dup2(bar_pipe[0], 0) == -1)
593 if (dup2(bar_pipe[1], 1) == -1)
595 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
596 err(1, "could not disable SIGPIPE");
597 switch (bar_pid = fork()) {
599 err(1, "cannot fork");
603 execvp(bar_argv[0], bar_argv);
604 err(1, "%s external app failed", bar_argv[0]);
606 default: /* parent */
612 bzero(&wa, sizeof wa);
613 for (i = 0; i < ScreenCount(display); i++)
614 TAILQ_FOREACH(r, &screens[i].rl, entry) {
616 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
617 wa.background_pixel =
618 screens[i].c[SWM_S_COLOR_BAR].color;
619 XChangeWindowAttributes(display, r->bar_window,
620 CWBackPixel | CWBorderPixel, &wa);
626 bar_setup(struct swm_region *r)
630 for (i = 0; bar_fonts[i] != NULL; i++) {
631 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
637 if (bar_fonts[i] == NULL)
638 errx(1, "couldn't load font");
639 bar_height = bar_fs->ascent + bar_fs->descent + 3;
641 r->bar_window = XCreateSimpleWindow(display,
642 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
643 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
644 r->s->c[SWM_S_COLOR_BAR].color);
645 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
646 XSetFont(display, bar_gc, bar_fs->fid);
647 XSelectInput(display, r->bar_window, VisibilityChangeMask);
649 XMapRaised(display, r->bar_window);
650 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
652 if (signal(SIGALRM, bar_signal) == SIG_ERR)
653 err(1, "could not install bar_signal");
658 config_win(struct ws_win *win)
662 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
663 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
664 ce.type = ConfigureNotify;
665 ce.display = display;
671 ce.height = win->g.h;
672 ce.border_width = 1; /* XXX store this! */
674 ce.override_redirect = False;
675 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
679 count_win(struct workspace *ws, int count_transient)
684 TAILQ_FOREACH(win, &ws->winlist, entry) {
685 if (count_transient == 0 && win->floating)
687 if (count_transient == 0 && win->transient)
691 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
697 quit(struct swm_region *r, union arg *args)
699 DNPRINTF(SWM_D_MISC, "quit\n");
704 restart(struct swm_region *r, union arg *args)
706 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
708 /* disable alarm because the following code may not be interrupted */
710 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
711 errx(1, "can't disable alarm");
715 XCloseDisplay(display);
716 execvp(start_argv[0], start_argv);
717 fprintf(stderr, "execvp failed\n");
723 root_to_region(Window root)
725 struct swm_region *r;
730 for (i = 0; i < ScreenCount(display); i++)
731 if (screens[i].root == root)
734 if (XQueryPointer(display, screens[i].root,
735 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
736 /* if we can't query the pointer, grab the first region */
737 r = TAILQ_FIRST(&screens[i].rl);
739 /* otherwise, choose a region based on pointer location */
740 TAILQ_FOREACH(r, &screens[i].rl, entry) {
741 if (x > X(r) && x < X(r) + WIDTH(r) &&
742 y > Y(r) && y < Y(r) + HEIGHT(r))
747 r = TAILQ_FIRST(&screens[i].rl);
753 find_window(Window id)
758 for (i = 0; i < ScreenCount(display); i++)
759 for (j = 0; j < SWM_WS_MAX; j++)
760 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
767 spawn(struct swm_region *r, union arg *args)
769 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
771 * The double-fork construct avoids zombie processes and keeps the code
772 * clean from stupid signal handlers.
777 close(ConnectionNumber(display));
779 execvp(args->argv[0], args->argv);
780 fprintf(stderr, "execvp failed\n");
789 spawnmenu(struct swm_region *r, union arg *args)
791 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
793 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
794 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
795 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
796 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
797 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
808 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
810 for (i = 0; i < ScreenCount(display); i++)
811 for (j = 0; j < SWM_WS_MAX; j++)
812 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
813 if (win->ws->r == NULL)
816 XSetWindowBorder(display, win->id,
817 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
819 win->ws->focus = NULL;
825 focus_win(struct ws_win *win)
827 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
833 win->ws->focus = win;
834 if (win->ws->r != NULL) {
836 if (!win->got_focus) {
837 XSetWindowBorder(display, win->id,
838 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
842 XSetInputFocus(display, win->id,
843 RevertToPointerRoot, CurrentTime);
848 switchws(struct swm_region *r, union arg *args)
851 struct swm_region *this_r, *other_r;
853 struct workspace *new_ws, *old_ws;
857 new_ws = &this_r->s->ws[wsid];
859 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
860 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
863 if (new_ws == old_ws)
868 /* if the other workspace is hidden, switch windows */
869 /* map new window first to prevent ugly blinking */
870 TAILQ_FOREACH(win, &new_ws->winlist, entry)
871 XMapRaised(display, win->id);
873 TAILQ_FOREACH(win, &old_ws->winlist, entry)
874 XUnmapWindow(display, win->id);
879 other_r->ws = old_ws;
888 focus_win(new_ws->focus);
894 cyclews(struct swm_region *r, union arg *args)
897 struct swm_screen *s = r->s;
899 DNPRINTF(SWM_D_WS, "cyclews id %d "
900 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
901 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
907 case SWM_ARG_ID_CYCLEWS_UP:
908 if (a.id < SWM_WS_MAX - 1)
913 case SWM_ARG_ID_CYCLEWS_DOWN:
917 a.id = SWM_WS_MAX - 1;
923 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
925 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
929 } while (a.id != r->ws->idx);
933 swapwin(struct swm_region *r, union arg *args)
935 struct ws_win *target;
936 struct ws_win_list *wl;
939 DNPRINTF(SWM_D_WS, "swapwin id %d "
940 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
941 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
942 if (cur_focus == NULL)
945 wl = &cur_focus->ws->winlist;
948 case SWM_ARG_ID_SWAPPREV:
949 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
950 TAILQ_REMOVE(wl, cur_focus, entry);
952 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
954 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
956 case SWM_ARG_ID_SWAPNEXT:
957 target = TAILQ_NEXT(cur_focus, entry);
958 TAILQ_REMOVE(wl, cur_focus, entry);
960 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
962 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
964 case SWM_ARG_ID_SWAPMAIN:
965 target = TAILQ_FIRST(wl);
966 if (target == cur_focus)
968 TAILQ_REMOVE(wl, target, entry);
969 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
970 TAILQ_REMOVE(wl, cur_focus, entry);
971 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
974 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
983 focus(struct swm_region *r, union arg *args)
985 struct ws_win *winfocus, *winlostfocus;
986 struct ws_win_list *wl;
988 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
989 if (cur_focus == NULL)
992 wl = &cur_focus->ws->winlist;
994 winlostfocus = cur_focus;
997 case SWM_ARG_ID_FOCUSPREV:
998 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
999 if (winfocus == NULL)
1000 winfocus = TAILQ_LAST(wl, ws_win_list);
1003 case SWM_ARG_ID_FOCUSNEXT:
1004 winfocus = TAILQ_NEXT(cur_focus, entry);
1005 if (winfocus == NULL)
1006 winfocus = TAILQ_FIRST(wl);
1009 case SWM_ARG_ID_FOCUSMAIN:
1010 winfocus = TAILQ_FIRST(wl);
1017 if (winfocus == winlostfocus)
1020 focus_win(winfocus);
1021 XSync(display, False);
1025 cycle_layout(struct swm_region *r, union arg *args)
1027 struct workspace *ws = r->ws;
1029 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1032 if (ws->cur_layout->l_stack == NULL)
1033 ws->cur_layout = &layouts[0];
1040 stack_config(struct swm_region *r, union arg *args)
1042 struct workspace *ws = r->ws;
1044 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1047 if (ws->cur_layout->l_config != NULL)
1048 ws->cur_layout->l_config(ws, args->id);
1050 if (args->id != SWM_ARG_ID_STACKINIT);
1056 struct swm_geometry g;
1057 struct swm_region *r;
1060 DNPRINTF(SWM_D_STACK, "stack\n");
1062 for (i = 0; i < ScreenCount(display); i++) {
1064 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1065 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1066 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1068 /* start with screen geometry, adjust for bar */
1078 r->ws->cur_layout->l_stack(r->ws, &g);
1081 XSync(display, False);
1085 stack_floater(struct ws_win *win, struct swm_region *r)
1090 bzero(&wc, sizeof wc);
1091 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1092 wc.border_width = 1;
1093 if (win->transient) {
1094 win->g.w = (double)WIDTH(r) * dialog_ratio;
1095 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1097 wc.width = win->g.w;
1098 wc.height = win->g.h;
1099 wc.x = (WIDTH(r) - win->g.w) / 2;
1100 wc.y = (HEIGHT(r) - win->g.h) / 2;
1102 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1103 win->id, wc.x, wc.y, wc.width, wc.height);
1105 XConfigureWindow(display, win->id, mask, &wc);
1109 vertical_config(struct workspace *ws, int id)
1111 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1114 case SWM_ARG_ID_STACKRESET:
1115 case SWM_ARG_ID_STACKINIT:
1116 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1117 ws->l_state.vertical_mwin = 1;
1119 case SWM_ARG_ID_MASTERSHRINK:
1120 if (ws->l_state.vertical_msize > 1)
1121 ws->l_state.vertical_msize--;
1123 case SWM_ARG_ID_MASTERGROW:
1124 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1125 ws->l_state.vertical_msize++;
1127 case SWM_ARG_ID_MASTERADD:
1128 ws->l_state.vertical_mwin++;
1130 case SWM_ARG_ID_MASTERDEL:
1131 if (ws->l_state.vertical_mwin > 0)
1132 ws->l_state.vertical_mwin--;
1140 vertical_stack(struct workspace *ws, struct swm_geometry *g) {
1142 struct swm_geometry gg = *g;
1143 struct ws_win *win, *winfocus;
1144 int i, j, split, colno, hrh, winno, main_width;
1147 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1149 if ((winno = count_win(ws, 0)) == 0)
1152 if (ws->focus == NULL)
1153 ws->focus = TAILQ_FIRST(&ws->winlist);
1154 winfocus = cur_focus ? cur_focus : ws->focus;
1156 if (ws->l_state.vertical_mwin &&
1157 winno > ws->l_state.vertical_mwin) {
1158 split = ws->l_state.vertical_mwin;
1160 main_width = (g->w / SWM_V_SLICE) *
1161 ws->l_state.vertical_msize;
1171 TAILQ_FOREACH(win, &ws->winlist, entry) {
1172 if (split && i == split) {
1173 colno = winno - split;
1174 hrh = (g->h / colno);
1175 gg.x += main_width + 2;
1176 gg.w = g->w - (main_width + 2);
1181 gg.h = (hrh + (g->h - (colno * hrh)));
1189 if (win->transient != 0 || win->floating != 0)
1190 stack_floater(win, ws->r);
1192 bzero(&wc, sizeof wc);
1193 wc.border_width = 1;
1194 win->g.x = wc.x = gg.x;
1195 win->g.y = wc.y = gg.y;
1196 win->g.w = wc.width = gg.w;
1197 win->g.h = wc.height = gg.h;
1198 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1199 XConfigureWindow(display, win->id, mask, &wc);
1202 XMapRaised(display, win->id);
1208 focus_win(winfocus); /* has to be done outside of the loop */
1212 horizontal_config(struct workspace *ws, int id)
1214 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1217 case SWM_ARG_ID_STACKRESET:
1218 case SWM_ARG_ID_STACKINIT:
1219 ws->l_state.horizontal_mwin = 1;
1220 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1222 case SWM_ARG_ID_MASTERSHRINK:
1223 if (ws->l_state.horizontal_msize > 1)
1224 ws->l_state.horizontal_msize--;
1226 case SWM_ARG_ID_MASTERGROW:
1227 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1228 ws->l_state.horizontal_msize++;
1230 case SWM_ARG_ID_MASTERADD:
1231 ws->l_state.horizontal_mwin++;
1233 case SWM_ARG_ID_MASTERDEL:
1234 if (ws->l_state.horizontal_mwin > 0)
1235 ws->l_state.horizontal_mwin--;
1243 horizontal_stack(struct workspace *ws, struct swm_geometry *g) {
1245 struct swm_geometry gg = *g;
1246 struct ws_win *win, *winfocus;
1247 int i, j, split, rowno, hrw, winno, main_height;
1250 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
1252 if ((winno = count_win(ws, 0)) == 0)
1255 if (ws->focus == NULL)
1256 ws->focus = TAILQ_FIRST(&ws->winlist);
1257 winfocus = cur_focus ? cur_focus : ws->focus;
1259 if (ws->l_state.horizontal_mwin &&
1260 winno > ws->l_state.horizontal_mwin) {
1261 split = ws->l_state.horizontal_mwin;
1263 main_height = (g->h / SWM_V_SLICE) *
1264 ws->l_state.horizontal_msize;
1274 TAILQ_FOREACH(win, &ws->winlist, entry) {
1275 if (split && i == split) {
1276 rowno = winno - split;
1277 hrw = (g->w / rowno);
1278 gg.y += main_height + 2;
1279 gg.h = g->h - (main_height + 2);
1284 gg.w = (hrw + (g->w - (rowno * hrw)));
1291 if (win->transient != 0 || win->floating != 0)
1292 stack_floater(win, ws->r);
1294 bzero(&wc, sizeof wc);
1295 wc.border_width = 1;
1296 win->g.x = wc.x = gg.x;
1297 win->g.y = wc.y = gg.y;
1298 win->g.w = wc.width = gg.w;
1299 win->g.h = wc.height = gg.h;
1300 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1301 XConfigureWindow(display, win->id, mask, &wc);
1304 XMapRaised(display, win->id);
1310 focus_win(winfocus); /* this has to be done outside of the loop */
1313 /* fullscreen view */
1315 max_stack(struct workspace *ws, struct swm_geometry *g) {
1317 struct swm_geometry gg = *g;
1318 struct ws_win *win, *winfocus;
1321 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1323 if (count_win(ws, 0) == 0)
1326 if (ws->focus == NULL)
1327 ws->focus = TAILQ_FIRST(&ws->winlist);
1328 winfocus = cur_focus ? cur_focus : ws->focus;
1330 TAILQ_FOREACH(win, &ws->winlist, entry) {
1331 if (win->transient != 0 || win->floating != 0) {
1332 if (win == ws->focus) {
1334 stack_floater(win, ws->r);
1335 XMapRaised(display, win->id);
1337 XUnmapWindow(display, win->id);
1339 bzero(&wc, sizeof wc);
1340 wc.border_width = 1;
1341 win->g.x = wc.x = gg.x;
1342 win->g.y = wc.y = gg.y;
1343 win->g.w = wc.width = gg.w;
1344 win->g.h = wc.height = gg.h;
1345 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1346 XConfigureWindow(display, win->id, mask, &wc);
1348 if (win == ws->focus) {
1349 XMapRaised(display, win->id);
1351 XUnmapWindow(display, win->id);
1356 focus_win(winfocus); /* has to be done outside of the loop */
1360 send_to_ws(struct swm_region *r, union arg *args)
1362 int wsid = args->id;
1363 struct ws_win *win = cur_focus;
1364 struct workspace *ws, *nws;
1366 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1369 nws = &win->s->ws[wsid];
1371 XUnmapWindow(display, win->id);
1373 /* find a window to focus */
1374 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1375 if (ws->focus == NULL)
1376 ws->focus = TAILQ_FIRST(&ws->winlist);
1377 if (ws->focus == win)
1380 TAILQ_REMOVE(&ws->winlist, win, entry);
1382 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1385 if (count_win(nws, 1) == 1)
1394 wkill(struct swm_region *r, union arg *args)
1396 DNPRINTF(SWM_D_MISC, "wkill\n");
1397 if(r->ws->focus != NULL)
1398 XKillClient(display, r->ws->focus->id);
1401 /* key definitions */
1405 void (*func)(struct swm_region *r, union arg *);
1408 /* modifier key function argument */
1409 { MODKEY, XK_space, cycle_layout, {0} },
1410 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1411 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1412 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1413 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1414 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1415 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1416 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1417 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1418 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1419 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1420 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1421 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1422 { MODKEY | ShiftMask, XK_q, quit, {0} },
1423 { MODKEY, XK_q, restart, {0} },
1424 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1425 { MODKEY, XK_1, switchws, {.id = 0} },
1426 { MODKEY, XK_2, switchws, {.id = 1} },
1427 { MODKEY, XK_3, switchws, {.id = 2} },
1428 { MODKEY, XK_4, switchws, {.id = 3} },
1429 { MODKEY, XK_5, switchws, {.id = 4} },
1430 { MODKEY, XK_6, switchws, {.id = 5} },
1431 { MODKEY, XK_7, switchws, {.id = 6} },
1432 { MODKEY, XK_8, switchws, {.id = 7} },
1433 { MODKEY, XK_9, switchws, {.id = 8} },
1434 { MODKEY, XK_0, switchws, {.id = 9} },
1435 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1436 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1437 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1438 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1439 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1440 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1441 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1442 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1443 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1444 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1445 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1446 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1447 { MODKEY, XK_b, bar_toggle, {0} },
1448 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1449 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1450 { MODKEY | ShiftMask, XK_x, wkill, {0} },
1454 click(struct swm_region *r, union arg *args)
1456 DNPRINTF(SWM_D_MOUSE, "click: button: %d\n", args->id);
1471 enum { client_click, root_click };
1473 unsigned int action;
1475 unsigned int button;
1476 void (*func)(struct swm_region *r, union arg *);
1479 /* action key mouse button func args */
1480 { client_click, MODKEY, Button1, click, {.id=Button1} },
1484 updatenumlockmask(void)
1487 XModifierKeymap *modmap;
1489 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1491 modmap = XGetModifierMapping(display);
1492 for (i = 0; i < 8; i++)
1493 for (j = 0; j < modmap->max_keypermod; j++)
1494 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1495 == XKeysymToKeycode(display, XK_Num_Lock))
1496 numlockmask = (1 << i);
1498 XFreeModifiermap(modmap);
1504 unsigned int i, j, k;
1506 unsigned int modifiers[] =
1507 { 0, LockMask, numlockmask, numlockmask | LockMask };
1509 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1510 updatenumlockmask();
1512 for (k = 0; k < ScreenCount(display); k++) {
1513 if (TAILQ_EMPTY(&screens[k].rl))
1515 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1516 for (i = 0; i < LENGTH(keys); i++) {
1517 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1518 for (j = 0; j < LENGTH(modifiers); j++)
1519 XGrabKey(display, code,
1520 keys[i].mod | modifiers[j],
1521 screens[k].root, True,
1522 GrabModeAsync, GrabModeAsync);
1528 grabbuttons(struct ws_win *win, int focused)
1531 unsigned int modifiers[] =
1532 { 0, LockMask, numlockmask, numlockmask|LockMask };
1534 updatenumlockmask();
1535 XUngrabButton(display, AnyButton, AnyModifier, win->id);
1537 for (i = 0; i < LENGTH(buttons); i++)
1538 if (buttons[i].action == client_click)
1539 for (j = 0; j < LENGTH(modifiers); j++)
1540 XGrabButton(display, buttons[i].button,
1541 buttons[i].mask | modifiers[j],
1542 win->id, False, BUTTONMASK,
1543 GrabModeAsync, GrabModeSync, None,
1546 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
1547 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1553 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1561 XKeyEvent *ev = &e->xkey;
1563 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1565 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1566 for (i = 0; i < LENGTH(keys); i++)
1567 if (keysym == keys[i].keysym
1568 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1570 keys[i].func(root_to_region(ev->root),
1575 buttonpress(XEvent *e)
1577 XButtonPressedEvent *ev = &e->xbutton;
1582 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1584 action = root_click;
1585 if ((win = find_window(ev->window)) == NULL)
1589 action = client_click;
1592 for (i = 0; i < LENGTH(buttons); i++)
1593 if (action == buttons[i].action && buttons[i].func &&
1594 buttons[i].button == ev->button &&
1595 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
1596 buttons[i].func(root_to_region(ev->root),
1601 set_win_state(struct ws_win *win, long state)
1603 long data[] = {state, None};
1605 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1607 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1608 (unsigned char *)data, 2);
1612 manage_window(Window id, struct workspace *ws)
1618 TAILQ_FOREACH(win, &ws->winlist, entry) {
1620 return (win); /* already being managed */
1623 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1624 errx(1, "calloc: failed to allocate memory for new window");
1628 win->s = ws->r->s; /* this never changes */
1629 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1631 /* make new win focused */
1634 XGetTransientForHint(display, win->id, &trans);
1636 win->transient = trans;
1637 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1638 (unsigned)win->id, win->transient);
1640 XGetWindowAttributes(display, id, &win->wa);
1641 win->g.w = win->wa.width;
1642 win->g.h = win->wa.height;
1643 win->g.x = win->wa.x;
1644 win->g.y = win->wa.y;
1646 /* XXX make this a table */
1647 bzero(&ch, sizeof ch);
1648 if (XGetClassHint(display, win->id, &ch)) {
1649 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1650 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1654 XFree(ch.res_class);
1659 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1660 PropertyChangeMask | StructureNotifyMask);
1662 set_win_state(win, NormalState);
1668 configurerequest(XEvent *e)
1670 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1675 if ((win = find_window(ev->window)) == NULL)
1679 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1681 bzero(&wc, sizeof wc);
1684 wc.width = ev->width;
1685 wc.height = ev->height;
1686 wc.border_width = ev->border_width;
1687 wc.sibling = ev->above;
1688 wc.stack_mode = ev->detail;
1689 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1691 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1693 if (win->floating) {
1694 if (ev->value_mask & CWX)
1696 if (ev->value_mask & CWY)
1698 if (ev->value_mask & CWWidth)
1699 win->g.w = ev->width;
1700 if (ev->value_mask & CWHeight)
1701 win->g.h = ev->height;
1702 if (win->ws->r != NULL) {
1703 /* this seems to be full screen */
1704 if (win->g.w > WIDTH(win->ws->r)) {
1709 if (win->g.h > HEIGHT(win->ws->r)) {
1715 if ((ev->value_mask & (CWX|CWY)) &&
1716 !(ev->value_mask & (CWWidth|CWHeight)))
1718 XMoveResizeWindow(display, win->id,
1719 win->g.x, win->g.y, win->g.w, win->g.h);
1726 configurenotify(XEvent *e)
1728 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1729 e->xconfigure.window);
1733 destroynotify(XEvent *e)
1736 XDestroyWindowEvent *ev = &e->xdestroywindow;
1737 struct workspace *ws;
1739 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1741 if ((win = find_window(ev->window)) != NULL) {
1743 /* find a window to focus */
1744 if (ws->focus == win)
1745 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1746 if (ws->focus == NULL)
1747 ws->focus = TAILQ_FIRST(&ws->winlist);
1748 if (ws->focus == win)
1750 if (cur_focus == win)
1751 focus_win(ws->focus);
1753 TAILQ_REMOVE(&ws->winlist, win, entry);
1754 set_win_state(win, WithdrawnState);
1761 enternotify(XEvent *e)
1763 XCrossingEvent *ev = &e->xcrossing;
1766 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1769 /* eat event(r) to prevent autofocus */
1774 if ((win = find_window(ev->window)) != NULL)
1781 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1785 mappingnotify(XEvent *e)
1787 XMappingEvent *ev = &e->xmapping;
1789 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1791 XRefreshKeyboardMapping(ev);
1792 if (ev->request == MappingKeyboard)
1797 maprequest(XEvent *e)
1799 XMapRequestEvent *ev = &e->xmaprequest;
1800 XWindowAttributes wa;
1801 struct swm_region *r;
1803 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1804 e->xmaprequest.window);
1806 if (!XGetWindowAttributes(display, ev->window, &wa))
1808 if (wa.override_redirect)
1810 r = root_to_region(wa.root);
1811 manage_window(e->xmaprequest.window, r->ws);
1816 propertynotify(XEvent *e)
1818 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1819 e->xproperty.window);
1823 unmapnotify(XEvent *e)
1825 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1829 visibilitynotify(XEvent *e)
1832 struct swm_region *r;
1834 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
1835 e->xvisibility.window);
1836 if (e->xvisibility.state == VisibilityUnobscured)
1837 for (i = 0; i < ScreenCount(display); i++)
1838 TAILQ_FOREACH(r, &screens[i].rl, entry)
1839 if (e->xvisibility.window == r->bar_window)
1843 void (*handler[LASTEvent])(XEvent *) = {
1845 [KeyPress] = keypress,
1846 [ButtonPress] = buttonpress,
1847 [ConfigureRequest] = configurerequest,
1848 [ConfigureNotify] = configurenotify,
1849 [DestroyNotify] = destroynotify,
1850 [EnterNotify] = enternotify,
1851 [FocusIn] = focusin,
1852 [MappingNotify] = mappingnotify,
1853 [MapRequest] = maprequest,
1854 [PropertyNotify] = propertynotify,
1855 [UnmapNotify] = unmapnotify,
1856 [VisibilityNotify] = visibilitynotify,
1860 xerror_start(Display *d, XErrorEvent *ee)
1867 xerror(Display *d, XErrorEvent *ee)
1869 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1877 xerrorxlib = XSetErrorHandler(xerror_start);
1879 /* this causes an error if some other window manager is running */
1880 XSelectInput(display, DefaultRootWindow(display),
1881 SubstructureRedirectMask);
1882 XSync(display, False);
1886 XSetErrorHandler(xerror);
1887 XSync(display, False);
1896 unsigned char *p = NULL;
1897 unsigned long n, extra;
1900 astate = XInternAtom(display, "WM_STATE", False);
1901 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
1902 &real, &format, &n, &extra, (unsigned char **)&p);
1903 if (status != Success)
1912 new_region(struct swm_screen *s, struct workspace *ws,
1913 int x, int y, int w, int h)
1915 struct swm_region *r;
1917 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
1918 s->idx, x, y, w, h);
1920 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
1921 errx(1, "calloc: failed to allocate memory for screen");
1930 TAILQ_INSERT_TAIL(&s->rl, r, entry);
1937 #ifdef SWM_XRR_HAS_CRTC
1939 XRRScreenResources *sr;
1941 #endif /* SWM_XRR_HAS_CRTC */
1942 Window d1, d2, *wins = NULL;
1943 XWindowAttributes wa;
1944 struct swm_region *r;
1946 int errorbase, major, minor;
1947 int ncrtc = 0, w = 0;
1949 struct workspace *ws;
1951 if ((screens = calloc(ScreenCount(display),
1952 sizeof(struct swm_screen))) == NULL)
1953 errx(1, "calloc: screens");
1955 /* map physical screens */
1956 for (i = 0; i < ScreenCount(display); i++) {
1957 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
1959 TAILQ_INIT(&screens[i].rl);
1960 screens[i].root = RootWindow(display, i);
1962 /* set default colors */
1963 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
1964 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
1965 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
1966 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
1967 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
1969 /* init all workspaces */
1970 for (j = 0; j < SWM_WS_MAX; j++) {
1971 ws = &screens[i].ws[j];
1976 TAILQ_INIT(&ws->winlist);
1978 for (k = 0; layouts[k].l_stack != NULL; k++)
1979 if (layouts[k].l_config != NULL)
1980 layouts[k].l_config(ws,
1981 SWM_ARG_ID_STACKINIT);
1982 ws->cur_layout = &layouts[0];
1985 /* map virtual screens onto physical screens */
1986 screens[i].xrandr_support = XRRQueryExtension(display,
1987 &xrandr_eventbase, &errorbase);
1988 if (screens[i].xrandr_support)
1989 if (XRRQueryVersion(display, &major, &minor) &&
1991 screens[i].xrandr_support = 0;
1993 #if 0 /* not ready for dynamic screen changes */
1994 if (screens[i].xrandr_support)
1995 XRRSelectInput(display,
1997 RRScreenChangeNotifyMask);
2000 /* grab existing windows (before we build the bars)*/
2001 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
2004 #ifdef SWM_XRR_HAS_CRTC
2005 sr = XRRGetScreenResources(display, screens[i].root);
2007 new_region(&screens[i], &screens[i].ws[w],
2008 0, 0, DisplayWidth(display, i),
2009 DisplayHeight(display, i));
2013 for (c = 0; c < ncrtc; c++) {
2014 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
2015 if (ci->noutput == 0)
2018 if (ci != NULL && ci->mode == None)
2019 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2020 DisplayWidth(display, i),
2021 DisplayHeight(display, i));
2023 new_region(&screens[i], &screens[i].ws[w],
2024 ci->x, ci->y, ci->width, ci->height);
2027 XRRFreeCrtcInfo(ci);
2028 XRRFreeScreenResources(sr);
2030 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2031 DisplayWidth(display, i),
2032 DisplayHeight(display, i));
2033 #endif /* SWM_XRR_HAS_CRTC */
2035 /* attach windows to a region */
2036 /* normal windows */
2037 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
2038 errx(1, "no regions on screen %d", i);
2040 for (i = 0; i < no; i++) {
2041 XGetWindowAttributes(display, wins[i], &wa);
2042 if (!XGetWindowAttributes(display, wins[i], &wa) ||
2043 wa.override_redirect ||
2044 XGetTransientForHint(display, wins[i], &d1))
2047 if (wa.map_state == IsViewable ||
2048 getstate(wins[i]) == NormalState)
2049 manage_window(wins[i], r->ws);
2051 /* transient windows */
2052 for (i = 0; i < no; i++) {
2053 if (!XGetWindowAttributes(display, wins[i], &wa))
2056 if (XGetTransientForHint(display, wins[i], &d1) &&
2057 (wa.map_state == IsViewable || getstate(wins[i]) ==
2059 manage_window(wins[i], r->ws);
2069 main(int argc, char *argv[])
2072 char conf[PATH_MAX], *cfile = NULL;
2079 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
2080 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2081 warnx("no locale support");
2083 if (!(display = XOpenDisplay(0)))
2084 errx(1, "can not open display");
2087 errx(1, "other wm running");
2089 astate = XInternAtom(display, "WM_STATE", False);
2091 /* look for local and global conf file */
2092 pwd = getpwuid(getuid());
2094 errx(1, "invalid user %d", getuid());
2098 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2099 if (stat(conf, &sb) != -1) {
2100 if (S_ISREG(sb.st_mode))
2103 /* try global conf file */
2104 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2105 if (!stat(conf, &sb))
2106 if (S_ISREG(sb.st_mode))
2113 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2118 xfd = ConnectionNumber(display);
2122 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2124 errx(1, "select failed");
2129 while(XPending(display)) {
2130 XNextEvent(display, &e);
2131 if (handler[e.type])
2132 handler[e.type](&e);
2136 XCloseDisplay(display);