3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
5 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
6 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Much code and ideas taken from dwm under the following license:
22 * MIT/X Consortium License
24 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
25 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
26 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
27 * 2007 Premysl Hruby <dfenze at gmail dot com>
28 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
29 * 2007 Christof Musik <christof at sendfax dot de>
30 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
31 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
32 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
34 * Permission is hereby granted, free of charge, to any person obtaining a
35 * copy of this software and associated documentation files (the "Software"),
36 * to deal in the Software without restriction, including without limitation
37 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
38 * and/or sell copies of the Software, and to permit persons to whom the
39 * Software is furnished to do so, subject to the following conditions:
41 * The above copyright notice and this permission notice shall be included in
42 * all copies or substantial portions of the Software.
44 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
47 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
49 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
50 * DEALINGS IN THE SOFTWARE.
53 static const char *cvstag = "$scrotwm$";
55 #define SWM_VERSION "0.9.5"
71 #include <sys/types.h>
75 #include <sys/queue.h>
76 #include <sys/param.h>
77 #include <sys/select.h>
79 #include <X11/cursorfont.h>
80 #include <X11/keysym.h>
81 #include <X11/Xatom.h>
83 #include <X11/Xproto.h>
84 #include <X11/Xutil.h>
85 #include <X11/extensions/Xrandr.h>
88 # error XRandR versions less than 1.0 are not supported
93 #define SWM_XRR_HAS_CRTC
97 /* #define SWM_DEBUG */
99 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
100 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
101 #define SWM_D_MISC 0x0001
102 #define SWM_D_EVENT 0x0002
103 #define SWM_D_WS 0x0004
104 #define SWM_D_FOCUS 0x0008
105 #define SWM_D_MOVE 0x0010
106 #define SWM_D_STACK 0x0020
107 #define SWM_D_MOUSE 0x0040
108 #define SWM_D_PROP 0x0080
109 #define SWM_D_CLASS 0x0100
110 #define SWM_D_KEY 0x0200
111 #define SWM_D_QUIRK 0x0400
113 u_int32_t swm_debug = 0
127 #define DPRINTF(x...)
128 #define DNPRINTF(n,x...)
131 #define LENGTH(x) (sizeof x / sizeof x[0])
132 #define MODKEY Mod1Mask
133 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
134 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
135 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
136 #define SWM_PROPLEN (16)
137 #define SWM_FUNCNAME_LEN (32)
138 #define SWM_KEYS_LEN (255)
139 #define SWM_QUIRK_LEN (64)
140 #define X(r) (r)->g.x
141 #define Y(r) (r)->g.y
142 #define WIDTH(r) (r)->g.w
143 #define HEIGHT(r) (r)->g.h
144 #define SWM_MAX_FONT_STEPS (3)
147 #define SWM_LIB "/usr/X11R6/lib/swmhack.so"
154 int (*xerrorxlib)(Display *, XErrorEvent *);
159 int xrandr_eventbase;
160 int ignore_enter = 0;
161 unsigned int numlockmask = 0;
165 int cycle_visible = 0;
167 int font_adjusted = 0;
168 unsigned int mod_key = MODKEY;
171 double dialog_ratio = .6;
173 #define SWM_BAR_MAX (256)
174 char *bar_argv[] = { NULL, NULL };
176 char bar_ext[SWM_BAR_MAX];
177 char bar_vertext[SWM_BAR_MAX];
179 sig_atomic_t bar_alarm = 0;
183 int bar_extra_running = 0;
186 int clock_enabled = 1;
187 int title_name_enabled = 0;
188 int title_class_enabled = 0;
194 char *bar_fonts[] = {
195 "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*",
196 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
200 /* terminal + args */
201 char *spawn_term[] = { "xterm", NULL };
202 char *spawn_screenshot[] = { "screenshot.sh", NULL, NULL };
203 char *spawn_lock[] = { "xlock", NULL };
204 char *spawn_initscr[] = { "initscreen.sh", NULL };
205 char *spawn_menu[] = { "dmenu_run", "-fn", NULL, "-nb", NULL,
206 "-nf", NULL, "-sb", NULL, "-sf", NULL, NULL };
208 #define SWM_MENU_FN (2)
209 #define SWM_MENU_NB (4)
210 #define SWM_MENU_NF (6)
211 #define SWM_MENU_SB (8)
212 #define SWM_MENU_SF (10)
214 /* layout manager data */
215 struct swm_geometry {
225 /* virtual "screens" */
227 TAILQ_ENTRY(swm_region) entry;
228 struct swm_geometry g;
229 struct workspace *ws; /* current workspace on this region */
230 struct swm_screen *s; /* screen idx */
233 TAILQ_HEAD(swm_region_list, swm_region);
236 TAILQ_ENTRY(ws_win) entry;
238 struct swm_geometry g;
243 int font_size_boundary[SWM_MAX_FONT_STEPS];
247 unsigned long quirks;
248 struct workspace *ws; /* always valid */
249 struct swm_screen *s; /* always valid, never changes */
250 XWindowAttributes wa;
254 TAILQ_HEAD(ws_win_list, ws_win);
256 /* user/key callable function IDs */
312 /* layout handlers */
314 void vertical_config(struct workspace *, int);
315 void vertical_stack(struct workspace *, struct swm_geometry *);
316 void horizontal_config(struct workspace *, int);
317 void horizontal_stack(struct workspace *, struct swm_geometry *);
318 void max_stack(struct workspace *, struct swm_geometry *);
320 void grabbuttons(struct ws_win *, int);
321 void new_region(struct swm_screen *, int, int, int, int);
324 void (*l_stack)(struct workspace *, struct swm_geometry *);
325 void (*l_config)(struct workspace *, int);
327 /* stack, configure */
328 { vertical_stack, vertical_config},
329 { horizontal_stack, horizontal_config},
334 #define SWM_H_SLICE (32)
335 #define SWM_V_SLICE (32)
337 /* define work spaces */
339 int idx; /* workspace index */
340 int restack; /* restack on switch */
341 struct layout *cur_layout; /* current layout handlers */
342 struct ws_win *focus; /* may be NULL */
343 struct ws_win *focus_prev; /* may be NULL */
344 struct swm_region *r; /* may be NULL */
345 struct ws_win_list winlist; /* list of windows in ws */
349 int horizontal_msize;
351 int horizontal_stacks;
358 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
359 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
361 /* physical screen mapping */
362 #define SWM_WS_MAX (10) /* XXX Too small? */
364 int idx; /* screen index */
365 struct swm_region_list rl; /* list of regions on this screen */
366 struct swm_region_list orl; /* list of old regions */
368 struct workspace ws[SWM_WS_MAX];
374 } c[SWM_S_COLOR_MAX];
376 struct swm_screen *screens;
379 struct ws_win *cur_focus = NULL;
381 /* args to functions */
384 #define SWM_ARG_ID_FOCUSNEXT (0)
385 #define SWM_ARG_ID_FOCUSPREV (1)
386 #define SWM_ARG_ID_FOCUSMAIN (2)
387 #define SWM_ARG_ID_SWAPNEXT (3)
388 #define SWM_ARG_ID_SWAPPREV (4)
389 #define SWM_ARG_ID_SWAPMAIN (5)
390 #define SWM_ARG_ID_MASTERSHRINK (6)
391 #define SWM_ARG_ID_MASTERGROW (7)
392 #define SWM_ARG_ID_MASTERADD (8)
393 #define SWM_ARG_ID_MASTERDEL (9)
394 #define SWM_ARG_ID_STACKRESET (10)
395 #define SWM_ARG_ID_STACKINIT (11)
396 #define SWM_ARG_ID_CYCLEWS_UP (12)
397 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
398 #define SWM_ARG_ID_CYCLESC_UP (14)
399 #define SWM_ARG_ID_CYCLESC_DOWN (15)
400 #define SWM_ARG_ID_STACKINC (16)
401 #define SWM_ARG_ID_STACKDEC (17)
402 #define SWM_ARG_ID_SS_ALL (0)
403 #define SWM_ARG_ID_SS_WINDOW (1)
404 #define SWM_ARG_ID_DONTCENTER (0)
405 #define SWM_ARG_ID_CENTER (1)
406 #define SWM_ARG_ID_KILLWINDOW (0)
407 #define SWM_ARG_ID_DELETEWINDOW (1)
416 #define SWM_Q_FLOAT (1<<0) /* float this window */
417 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
418 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
419 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
420 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
422 int quirks_size = 0, quirks_length = 0;
423 struct quirk *quirks = NULL;
426 void expose(XEvent *);
427 void keypress(XEvent *);
428 void buttonpress(XEvent *);
429 void configurerequest(XEvent *);
430 void configurenotify(XEvent *);
431 void destroynotify(XEvent *);
432 void enternotify(XEvent *);
433 void focusin(XEvent *);
434 void focusout(XEvent *);
435 void mappingnotify(XEvent *);
436 void maprequest(XEvent *);
437 void propertynotify(XEvent *);
438 void unmapnotify(XEvent *);
439 void visibilitynotify(XEvent *);
441 void (*handler[LASTEvent])(XEvent *) = {
443 [KeyPress] = keypress,
444 [ButtonPress] = buttonpress,
445 [ConfigureRequest] = configurerequest,
446 [ConfigureNotify] = configurenotify,
447 [DestroyNotify] = destroynotify,
448 [EnterNotify] = enternotify,
450 [FocusOut] = focusout,
451 [MappingNotify] = mappingnotify,
452 [MapRequest] = maprequest,
453 [PropertyNotify] = propertynotify,
454 [UnmapNotify] = unmapnotify,
455 [VisibilityNotify] = visibilitynotify,
459 name_to_color(char *colorname)
463 XColor screen_def, exact_def;
464 unsigned long result = 0;
465 char cname[32] = "#";
467 cmap = DefaultColormap(display, screens[0].idx);
468 status = XAllocNamedColor(display, cmap, colorname,
469 &screen_def, &exact_def);
471 strlcat(cname, colorname + 2, sizeof cname - 1);
472 status = XAllocNamedColor(display, cmap, cname, &screen_def,
476 result = screen_def.pixel;
478 fprintf(stderr, "color '%s' not found.\n", colorname);
484 setscreencolor(char *val, int i, int c)
486 if (i > 0 && i <= ScreenCount(display)) {
487 screens[i - 1].c[c].color = name_to_color(val);
488 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
490 } else if (i == -1) {
491 for (i = 0; i < ScreenCount(display); i++)
492 screens[i].c[c].color = name_to_color(val);
493 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
496 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
497 i, ScreenCount(display));
501 custom_region(char *val)
503 unsigned int sidx, x, y, w, h;
505 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
506 errx(1, "invalid custom region, "
507 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
508 if (sidx < 1 || sidx > ScreenCount(display))
509 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
510 sidx, ScreenCount(display));
514 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
516 if (x < 0 || x > DisplayWidth(display, sidx) ||
517 y < 0 || y > DisplayHeight(display, sidx) ||
518 w + x > DisplayWidth(display, sidx) ||
519 h + y > DisplayHeight(display, sidx))
520 errx(1, "region %ux%u+%u+%u not within screen boundaries "
521 "(%ux%u)\n", w, h, x, y,
522 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
524 new_region(&screens[sidx], x, y, w, h);
528 socket_setnonblock(int fd)
532 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
533 err(1, "fcntl F_GETFL");
535 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
536 err(1, "fcntl F_SETFL");
540 bar_print(struct swm_region *r, char *s)
542 XClearWindow(display, r->bar_window);
543 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
544 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
553 bzero(bar_pipe, sizeof bar_pipe);
556 kill(bar_pid, SIGTERM);
559 strlcpy(bar_ext, "", sizeof bar_ext);
568 struct swm_region *r;
569 int i, x, do_class, do_name;
572 char loc[SWM_BAR_MAX];
577 if (bar_enabled == 0)
579 if (bar_extra && bar_extra_running) {
580 /* ignore short reads; it'll correct itself */
581 while ((b = fgetln(stdin, &len)) != NULL)
582 if (b && b[len - 1] == '\n') {
584 strlcpy(bar_ext, b, sizeof bar_ext);
586 if (b == NULL && errno != EAGAIN) {
587 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
588 errno, strerror(errno));
592 strlcpy(bar_ext, "", sizeof bar_ext);
594 if (clock_enabled == 0)
595 strlcpy(s, "", sizeof s);
598 localtime_r(&tmt, &tm);
599 strftime(s, sizeof s, "%a %b %d %R %Z %Y ", &tm);
602 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
604 if ((xch = XAllocClassHint()) == NULL)
606 status = XGetClassHint(display, cur_focus->id, xch);
607 if (status == BadWindow || status == BadAlloc)
609 do_class = (title_class_enabled && xch->res_class != NULL);
610 do_name = (title_name_enabled && xch->res_name != NULL);
612 strlcat(s, xch->res_class, sizeof s);
613 if (do_class && do_name)
614 strlcat(s, ":", sizeof s);
616 strlcat(s, xch->res_name, sizeof s);
621 for (i = 0; i < ScreenCount(display); i++) {
623 TAILQ_FOREACH(r, &screens[i].rl, entry) {
624 snprintf(loc, sizeof loc, "%d:%d %s %s %s",
625 x++, r->ws->idx + 1, s, bar_ext, bar_vertext);
629 XSync(display, False);
640 bar_toggle(struct swm_region *r, union arg *args)
642 struct swm_region *tmpr;
643 int i, j, sc = ScreenCount(display);
645 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
648 for (i = 0; i < sc; i++)
649 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
650 XUnmapWindow(display, tmpr->bar_window);
652 for (i = 0; i < sc; i++)
653 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
654 XMapRaised(display, tmpr->bar_window);
656 bar_enabled = !bar_enabled;
657 for (i = 0; i < sc; i++)
658 for (j = 0; j < SWM_WS_MAX; j++)
659 screens[i].ws[j].restack = 1;
662 /* must be after stack */
669 XSetWindowAttributes wa;
670 struct swm_region *r;
673 /* do this here because the conf file is in memory */
674 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
675 /* launch external status app */
676 bar_extra_running = 1;
677 if (pipe(bar_pipe) == -1)
678 err(1, "pipe error");
679 socket_setnonblock(bar_pipe[0]);
680 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
681 if (dup2(bar_pipe[0], 0) == -1)
683 if (dup2(bar_pipe[1], 1) == -1)
685 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
686 err(1, "could not disable SIGPIPE");
687 switch (bar_pid = fork()) {
689 err(1, "cannot fork");
693 execvp(bar_argv[0], bar_argv);
694 err(1, "%s external app failed", bar_argv[0]);
696 default: /* parent */
702 bzero(&wa, sizeof wa);
703 for (i = 0; i < ScreenCount(display); i++)
704 TAILQ_FOREACH(r, &screens[i].rl, entry) {
706 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
707 wa.background_pixel =
708 screens[i].c[SWM_S_COLOR_BAR].color;
709 XChangeWindowAttributes(display, r->bar_window,
710 CWBackPixel | CWBorderPixel, &wa);
716 bar_setup(struct swm_region *r)
720 for (i = 0; bar_fonts[i] != NULL; i++) {
721 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
727 if (bar_fonts[i] == NULL)
728 errx(1, "couldn't load font");
729 bar_height = bar_fs->ascent + bar_fs->descent + 3;
731 r->bar_window = XCreateSimpleWindow(display,
732 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
733 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
734 r->s->c[SWM_S_COLOR_BAR].color);
735 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
736 XSetFont(display, bar_gc, bar_fs->fid);
737 XSelectInput(display, r->bar_window, VisibilityChangeMask);
739 XMapRaised(display, r->bar_window);
740 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
742 if (signal(SIGALRM, bar_signal) == SIG_ERR)
743 err(1, "could not install bar_signal");
748 version(struct swm_region *r, union arg *args)
750 bar_version = !bar_version;
752 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
753 SWM_VERSION, cvstag);
755 strlcpy(bar_vertext, "", sizeof bar_vertext);
760 client_msg(struct ws_win *win, Atom a)
762 XClientMessageEvent cm;
764 bzero(&cm, sizeof cm);
765 cm.type = ClientMessage;
767 cm.message_type = aprot;
770 cm.data.l[1] = CurrentTime;
771 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
775 config_win(struct ws_win *win)
779 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
780 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
781 ce.type = ConfigureNotify;
782 ce.display = display;
788 ce.height = win->g.h;
789 ce.border_width = 1; /* XXX store this! */
791 ce.override_redirect = False;
792 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
796 count_win(struct workspace *ws, int count_transient)
801 TAILQ_FOREACH(win, &ws->winlist, entry) {
802 if (count_transient == 0 && win->floating)
804 if (count_transient == 0 && win->transient)
808 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
814 quit(struct swm_region *r, union arg *args)
816 DNPRINTF(SWM_D_MISC, "quit\n");
826 for (i = 0; i < ScreenCount(display); i++)
827 for (j = 0; j < SWM_WS_MAX; j++)
828 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
829 XUnmapWindow(display, win->id);
833 fake_keypress(struct ws_win *win, int keysym, int modifiers)
837 event.display = display; /* Ignored, but what the hell */
838 event.window = win->id;
839 event.root = win->s->root;
840 event.subwindow = None;
841 event.time = CurrentTime;
846 event.same_screen = True;
847 event.keycode = XKeysymToKeycode(display, keysym);
848 event.state = modifiers;
850 event.type = KeyPress;
851 XSendEvent(event.display, event.window, True,
852 KeyPressMask, (XEvent *)&event);
854 event.type = KeyRelease;
855 XSendEvent(event.display, event.window, True,
856 KeyPressMask, (XEvent *)&event);
861 restart(struct swm_region *r, union arg *args)
863 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
865 /* disable alarm because the following code may not be interrupted */
867 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
868 errx(1, "can't disable alarm");
873 XCloseDisplay(display);
874 execvp(start_argv[0], start_argv);
875 fprintf(stderr, "execvp failed\n");
881 root_to_region(Window root)
883 struct swm_region *r = NULL;
888 for (i = 0; i < ScreenCount(display); i++)
889 if (screens[i].root == root)
892 if (XQueryPointer(display, screens[i].root,
893 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
894 /* choose a region based on pointer location */
895 TAILQ_FOREACH(r, &screens[i].rl, entry)
896 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
897 y >= Y(r) && y <= Y(r) + HEIGHT(r))
902 r = TAILQ_FIRST(&screens[i].rl);
908 find_window(Window id)
913 for (i = 0; i < ScreenCount(display); i++)
914 for (j = 0; j < SWM_WS_MAX; j++)
915 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
922 spawn(struct swm_region *r, union arg *args)
927 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
929 * The double-fork construct avoids zombie processes and keeps the code
930 * clean from stupid signal handlers.
935 close(ConnectionNumber(display));
936 setenv("LD_PRELOAD", SWM_LIB, 1);
937 if (asprintf(&ret, "%d", r->ws->idx)) {
938 setenv("_SWM_WS", ret, 1);
941 if (asprintf(&ret, "%d", getpid())) {
942 setenv("_SWM_PID", ret, 1);
946 /* kill stdin, mplayer, ssh-add etc. need that */
947 si = open("/dev/null", O_RDONLY, 0);
949 err(1, "open /dev/null");
950 if (dup2(si, 0) == -1)
951 err(1, "dup2 /dev/null");
952 execvp(args->argv[0], args->argv);
953 fprintf(stderr, "execvp failed\n");
962 spawnterm(struct swm_region *r, union arg *args)
964 DNPRINTF(SWM_D_MISC, "spawnterm\n");
967 setenv("_SWM_XTERM_FONTADJ", "", 1);
972 spawnmenu(struct swm_region *r, union arg *args)
974 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
976 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
977 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
978 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
979 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
980 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
986 unfocus_win(struct ws_win *win)
991 if (win->ws->focus != win && win->ws->focus != NULL)
992 win->ws->focus_prev = win->ws->focus;
994 if (win->ws->r == NULL)
998 XSetWindowBorder(display, win->id,
999 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1001 if (win->ws->focus == win)
1002 win->ws->focus = NULL;
1003 if (cur_focus == win)
1013 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
1015 for (i = 0; i < ScreenCount(display); i++)
1016 for (j = 0; j < SWM_WS_MAX; j++)
1017 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1022 focus_win(struct ws_win *win)
1024 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1030 unfocus_win(cur_focus);
1031 if (win->ws->focus) {
1032 /* probably shouldn't happen due to the previous unfocus_win */
1033 DNPRINTF(SWM_D_FOCUS, "unfocusing win->ws->focus: %lu\n",
1034 win->ws->focus->id);
1035 unfocus_win(win->ws->focus);
1037 win->ws->focus = win;
1038 if (win->ws->r != NULL) {
1040 if (!win->got_focus) {
1041 XSetWindowBorder(display, win->id,
1042 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1043 grabbuttons(win, 1);
1046 XSetInputFocus(display, win->id,
1047 RevertToPointerRoot, CurrentTime);
1052 switchws(struct swm_region *r, union arg *args)
1054 int wsid = args->id;
1055 struct swm_region *this_r, *other_r;
1057 struct workspace *new_ws, *old_ws;
1060 old_ws = this_r->ws;
1061 new_ws = &this_r->s->ws[wsid];
1063 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1064 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1067 if (new_ws == old_ws)
1070 other_r = new_ws->r;
1072 /* if the other workspace is hidden, switch windows */
1073 /* map new window first to prevent ugly blinking */
1075 old_ws->restack = 1;
1077 TAILQ_FOREACH(win, &new_ws->winlist, entry)
1078 XMapRaised(display, win->id);
1080 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1081 XUnmapWindow(display, win->id);
1083 other_r->ws = old_ws;
1084 old_ws->r = other_r;
1086 this_r->ws = new_ws;
1091 if (new_ws->focus == NULL)
1092 new_ws->focus = TAILQ_FIRST(&new_ws->winlist);
1094 focus_win(new_ws->focus);
1100 cyclews(struct swm_region *r, union arg *args)
1103 struct swm_screen *s = r->s;
1105 DNPRINTF(SWM_D_WS, "cyclews id %d "
1106 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1107 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1112 case SWM_ARG_ID_CYCLEWS_UP:
1113 if (a.id < SWM_WS_MAX - 1)
1118 case SWM_ARG_ID_CYCLEWS_DOWN:
1122 a.id = SWM_WS_MAX - 1;
1128 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1130 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1134 } while (a.id != r->ws->idx);
1138 cyclescr(struct swm_region *r, union arg *args)
1140 struct swm_region *rr;
1145 case SWM_ARG_ID_CYCLESC_UP:
1146 rr = TAILQ_NEXT(r, entry);
1148 rr = TAILQ_FIRST(&screens[i].rl);
1150 case SWM_ARG_ID_CYCLESC_DOWN:
1151 rr = TAILQ_PREV(r, swm_region_list, entry);
1153 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1159 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1160 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, rr->g.x,
1161 rr->g.y + bar_enabled ? bar_height : 0);
1165 swapwin(struct swm_region *r, union arg *args)
1167 struct ws_win *target, *source;
1168 struct ws_win_list *wl;
1171 DNPRINTF(SWM_D_WS, "swapwin id %d "
1172 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1173 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1174 if (cur_focus == NULL)
1178 wl = &source->ws->winlist;
1181 case SWM_ARG_ID_SWAPPREV:
1182 target = TAILQ_PREV(source, ws_win_list, entry);
1183 TAILQ_REMOVE(wl, cur_focus, entry);
1185 TAILQ_INSERT_TAIL(wl, source, entry);
1187 TAILQ_INSERT_BEFORE(target, source, entry);
1189 case SWM_ARG_ID_SWAPNEXT:
1190 target = TAILQ_NEXT(source, entry);
1191 TAILQ_REMOVE(wl, source, entry);
1193 TAILQ_INSERT_HEAD(wl, source, entry);
1195 TAILQ_INSERT_AFTER(wl, target, source, entry);
1197 case SWM_ARG_ID_SWAPMAIN:
1198 target = TAILQ_FIRST(wl);
1199 if (target == source) {
1200 if (source->ws->focus_prev != NULL &&
1201 source->ws->focus_prev != target)
1203 source = source->ws->focus_prev;
1207 source->ws->focus_prev = target;
1208 TAILQ_REMOVE(wl, target, entry);
1209 TAILQ_INSERT_BEFORE(source, target, entry);
1210 TAILQ_REMOVE(wl, source, entry);
1211 TAILQ_INSERT_HEAD(wl, source, entry);
1214 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1223 focus(struct swm_region *r, union arg *args)
1225 struct ws_win *winfocus, *winlostfocus;
1226 struct ws_win_list *wl;
1228 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1229 if (cur_focus == NULL)
1232 wl = &cur_focus->ws->winlist;
1234 winlostfocus = cur_focus;
1237 case SWM_ARG_ID_FOCUSPREV:
1238 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1239 if (winfocus == NULL)
1240 winfocus = TAILQ_LAST(wl, ws_win_list);
1243 case SWM_ARG_ID_FOCUSNEXT:
1244 winfocus = TAILQ_NEXT(cur_focus, entry);
1245 if (winfocus == NULL)
1246 winfocus = TAILQ_FIRST(wl);
1249 case SWM_ARG_ID_FOCUSMAIN:
1250 winfocus = TAILQ_FIRST(wl);
1251 if (winfocus == cur_focus)
1252 winfocus = cur_focus->ws->focus_prev;
1253 if (winfocus == NULL)
1261 if (winfocus == winlostfocus || winfocus == NULL)
1264 XMapRaised(display, winfocus->id);
1265 focus_win(winfocus);
1266 XSync(display, False);
1270 cycle_layout(struct swm_region *r, union arg *args)
1272 struct workspace *ws = r->ws;
1274 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1277 if (ws->cur_layout->l_stack == NULL)
1278 ws->cur_layout = &layouts[0];
1284 stack_config(struct swm_region *r, union arg *args)
1286 struct workspace *ws = r->ws;
1288 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1291 if (ws->cur_layout->l_config != NULL)
1292 ws->cur_layout->l_config(ws, args->id);
1294 if (args->id != SWM_ARG_ID_STACKINIT);
1300 struct swm_geometry g;
1301 struct swm_region *r;
1304 DNPRINTF(SWM_D_STACK, "stack\n");
1306 for (i = 0; i < ScreenCount(display); i++) {
1308 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1309 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1310 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1312 /* start with screen geometry, adjust for bar */
1322 r->ws->cur_layout->l_stack(r->ws, &g);
1327 XSync(display, False);
1331 stack_floater(struct ws_win *win, struct swm_region *r)
1336 bzero(&wc, sizeof wc);
1337 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1338 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w == WIDTH(r)) &&
1339 (win->g.h == HEIGHT(r)))
1340 wc.border_width = 0;
1342 wc.border_width = 1;
1343 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1344 win->g.w = (double)WIDTH(r) * dialog_ratio;
1345 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1347 wc.width = win->g.w;
1348 wc.height = win->g.h;
1353 wc.x = (WIDTH(r) - win->g.w) / 2;
1354 wc.y = (HEIGHT(r) - win->g.h) / 2;
1357 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1358 win->id, wc.x, wc.y, wc.width, wc.height);
1360 XConfigureWindow(display, win->id, mask, &wc);
1364 * Send keystrokes to terminal to decrease/increase the font size as the
1365 * window size changes.
1368 adjust_font(struct ws_win *win)
1370 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1371 win->floating || win->transient)
1374 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1375 win->g.w / win->sh.width_inc < term_width &&
1376 win->font_steps < SWM_MAX_FONT_STEPS) {
1377 win->font_size_boundary[win->font_steps] =
1378 (win->sh.width_inc * term_width) + win->sh.base_width;
1381 win->last_inc = win->sh.width_inc;
1382 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1383 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1384 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1387 win->last_inc = win->sh.width_inc;
1388 fake_keypress(win, XK_KP_Add, ShiftMask);
1392 #define SWAPXY(g) do { \
1394 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1395 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1398 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1401 struct swm_geometry win_g, r_g = *g;
1402 struct ws_win *win, *winfocus;
1403 int i, j, s, stacks;
1404 int w_inc = 1, h_inc, w_base = 1, h_base;
1405 int hrh, extra = 0, h_slice, last_h = 0;
1406 int split, colno, winno, mwin, msize, mscale;
1407 int remain, missing, v_slice;
1410 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1411 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1413 if ((winno = count_win(ws, 0)) == 0)
1416 if (ws->focus == NULL)
1417 ws->focus = TAILQ_FIRST(&ws->winlist);
1418 winfocus = cur_focus ? cur_focus : ws->focus;
1420 TAILQ_FOREACH(win, &ws->winlist, entry)
1421 if (win->transient == 0 && win->floating == 0)
1428 w_inc = win->sh.width_inc;
1429 w_base = win->sh.base_width;
1430 mwin = ws->l_state.horizontal_mwin;
1431 mscale = ws->l_state.horizontal_msize;
1432 stacks = ws->l_state.horizontal_stacks;
1435 w_inc = win->sh.height_inc;
1436 w_base = win->sh.base_height;
1437 mwin = ws->l_state.vertical_mwin;
1438 mscale = ws->l_state.vertical_msize;
1439 stacks = ws->l_state.vertical_stacks;
1443 if (stacks > winno - mwin)
1444 stacks = winno - mwin;
1448 h_slice = r_g.h / SWM_H_SLICE;
1449 if (mwin && winno > mwin) {
1450 v_slice = r_g.w / SWM_V_SLICE;
1454 win_g.w = v_slice * mscale;
1456 if (w_inc > 1 && w_inc < v_slice) {
1457 /* adjust for window's requested size increment */
1458 remain = (win_g.w - w_base) % w_inc;
1459 missing = w_inc - remain;
1466 win_g.x += r_g.w - msize;
1469 colno = split = winno / stacks;
1470 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
1472 hrh = r_g.h / colno;
1473 extra = r_g.h - (colno * hrh);
1476 /* stack all the tiled windows */
1477 i = j = 0, s = stacks;
1478 TAILQ_FOREACH(win, &ws->winlist, entry) {
1479 if (win->transient != 0 || win->floating != 0)
1482 if (split && i == split) {
1483 colno = (winno - mwin) / stacks;
1484 if (s <= (winno - mwin) % stacks)
1486 split = split + colno;
1487 hrh = (r_g.h / colno);
1488 extra = r_g.h - (colno * hrh);
1492 win_g.x += win_g.w + 2;
1493 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
1495 win_g.w += (r_g.w - msize - (stacks * 2)) %
1502 h_inc = win->sh.width_inc;
1503 h_base = win->sh.base_width;
1505 h_inc = win->sh.height_inc;
1506 h_base = win->sh.base_height;
1508 if (j == colno - 1) {
1509 win_g.h = hrh + extra;
1510 } else if (h_inc > 1 && h_inc < h_slice) {
1511 /* adjust for window's requested size increment */
1512 remain = (win_g.h - h_base) % h_inc;
1513 missing = h_inc - remain;
1515 if (missing <= extra || j == 0) {
1527 win_g.y += last_h + 2;
1529 bzero(&wc, sizeof wc);
1530 wc.border_width = 1;
1532 win->g.x = wc.x = win_g.y;
1533 win->g.y = wc.y = win_g.x;
1534 win->g.w = wc.width = win_g.h;
1535 win->g.h = wc.height = win_g.w;
1537 win->g.x = wc.x = win_g.x;
1538 win->g.y = wc.y = win_g.y;
1539 win->g.w = wc.width = win_g.w;
1540 win->g.h = wc.height = win_g.h;
1543 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1544 XConfigureWindow(display, win->id, mask, &wc);
1545 XMapRaised(display, win->id);
1553 /* now, stack all the floaters and transients */
1554 TAILQ_FOREACH(win, &ws->winlist, entry) {
1555 if (win->transient == 0 && win->floating == 0)
1558 stack_floater(win, ws->r);
1559 XMapRaised(display, win->id);
1563 focus_win(winfocus); /* has to be done outside of the loop */
1567 vertical_config(struct workspace *ws, int id)
1569 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1572 case SWM_ARG_ID_STACKRESET:
1573 case SWM_ARG_ID_STACKINIT:
1574 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1575 ws->l_state.vertical_mwin = 1;
1576 ws->l_state.vertical_stacks = 1;
1578 case SWM_ARG_ID_MASTERSHRINK:
1579 if (ws->l_state.vertical_msize > 1)
1580 ws->l_state.vertical_msize--;
1582 case SWM_ARG_ID_MASTERGROW:
1583 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1584 ws->l_state.vertical_msize++;
1586 case SWM_ARG_ID_MASTERADD:
1587 ws->l_state.vertical_mwin++;
1589 case SWM_ARG_ID_MASTERDEL:
1590 if (ws->l_state.vertical_mwin > 0)
1591 ws->l_state.vertical_mwin--;
1593 case SWM_ARG_ID_STACKINC:
1594 ws->l_state.vertical_stacks++;
1596 case SWM_ARG_ID_STACKDEC:
1597 if (ws->l_state.vertical_stacks > 1)
1598 ws->l_state.vertical_stacks--;
1606 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1608 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1610 stack_master(ws, g, 0, 0);
1614 horizontal_config(struct workspace *ws, int id)
1616 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1619 case SWM_ARG_ID_STACKRESET:
1620 case SWM_ARG_ID_STACKINIT:
1621 ws->l_state.horizontal_mwin = 1;
1622 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1623 ws->l_state.horizontal_stacks = 1;
1625 case SWM_ARG_ID_MASTERSHRINK:
1626 if (ws->l_state.horizontal_msize > 1)
1627 ws->l_state.horizontal_msize--;
1629 case SWM_ARG_ID_MASTERGROW:
1630 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1631 ws->l_state.horizontal_msize++;
1633 case SWM_ARG_ID_MASTERADD:
1634 ws->l_state.horizontal_mwin++;
1636 case SWM_ARG_ID_MASTERDEL:
1637 if (ws->l_state.horizontal_mwin > 0)
1638 ws->l_state.horizontal_mwin--;
1640 case SWM_ARG_ID_STACKINC:
1641 ws->l_state.horizontal_stacks++;
1643 case SWM_ARG_ID_STACKDEC:
1644 if (ws->l_state.horizontal_stacks > 1)
1645 ws->l_state.horizontal_stacks--;
1653 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1655 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1657 stack_master(ws, g, 1, 0);
1660 /* fullscreen view */
1662 max_stack(struct workspace *ws, struct swm_geometry *g) {
1664 struct swm_geometry gg = *g;
1665 struct ws_win *win, *winfocus;
1668 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1670 if (count_win(ws, 0) == 0)
1673 if (ws->focus == NULL)
1674 ws->focus = TAILQ_FIRST(&ws->winlist);
1675 winfocus = cur_focus ? cur_focus : ws->focus;
1677 TAILQ_FOREACH(win, &ws->winlist, entry) {
1678 if (win->transient != 0 || win->floating != 0) {
1679 if (win == ws->focus) {
1681 stack_floater(win, ws->r);
1682 XMapRaised(display, win->id);
1684 XUnmapWindow(display, win->id);
1686 bzero(&wc, sizeof wc);
1687 wc.border_width = 1;
1688 win->g.x = wc.x = gg.x;
1689 win->g.y = wc.y = gg.y;
1690 win->g.w = wc.width = gg.w;
1691 win->g.h = wc.height = gg.h;
1692 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1693 XConfigureWindow(display, win->id, mask, &wc);
1695 if (win == ws->focus) {
1696 XMapRaised(display, win->id);
1698 XUnmapWindow(display, win->id);
1703 focus_win(winfocus); /* has to be done outside of the loop */
1707 send_to_ws(struct swm_region *r, union arg *args)
1709 int wsid = args->id;
1710 struct ws_win *win = cur_focus;
1711 struct workspace *ws, *nws;
1712 Atom ws_idx_atom = 0;
1713 unsigned char ws_idx_str[SWM_PROPLEN];
1718 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1721 nws = &win->s->ws[wsid];
1723 XUnmapWindow(display, win->id);
1725 /* find a window to focus */
1726 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1727 if (ws->focus == NULL)
1728 ws->focus = TAILQ_FIRST(&ws->winlist);
1729 if (ws->focus == win)
1732 TAILQ_REMOVE(&ws->winlist, win, entry);
1734 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1737 /* Try to update the window's workspace property */
1738 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1740 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1741 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1743 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1744 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1747 if (count_win(nws, 1) == 1)
1756 wkill(struct swm_region *r, union arg *args)
1758 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
1760 if(r->ws->focus == NULL)
1763 if (args->id == SWM_ARG_ID_KILLWINDOW)
1764 XKillClient(display, r->ws->focus->id);
1766 if (r->ws->focus->can_delete)
1767 client_msg(r->ws->focus, adelete);
1771 screenshot(struct swm_region *r, union arg *args)
1775 DNPRINTF(SWM_D_MISC, "screenshot\n");
1777 if (ss_enabled == 0)
1781 case SWM_ARG_ID_SS_ALL:
1782 spawn_screenshot[1] = "full";
1784 case SWM_ARG_ID_SS_WINDOW:
1785 spawn_screenshot[1] = "window";
1790 a.argv = spawn_screenshot;
1795 floating_toggle(struct swm_region *r, union arg *args)
1797 struct ws_win *win = cur_focus;
1802 win->floating = !win->floating;
1809 resize_window(struct ws_win *win, int center)
1813 struct swm_region *r;
1815 r = root_to_region(win->wa.root);
1816 bzero(&wc, sizeof wc);
1817 mask = CWBorderWidth | CWWidth | CWHeight;
1818 wc.border_width = 1;
1819 wc.width = win->g.w;
1820 wc.height = win->g.h;
1821 if (center == SWM_ARG_ID_CENTER) {
1822 wc.x = (WIDTH(r) - win->g.w) / 2;
1823 wc.y = (HEIGHT(r) - win->g.h) / 2;
1827 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
1828 win->id, wc.x, wc.y, wc.width, wc.height);
1830 XConfigureWindow(display, win->id, mask, &wc);
1835 resize(struct ws_win *win, union arg *args)
1840 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %d\n",
1841 win->id, win->floating, win->transient);
1843 if (!(win->transient != 0 || win->floating != 0))
1846 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1847 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1849 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
1851 XMaskEvent(display, MOUSEMASK | ExposureMask |
1852 SubstructureRedirectMask, &ev);
1854 case ConfigureRequest:
1857 handler[ev.type](&ev);
1860 if (ev.xmotion.x <= 1)
1862 if (ev.xmotion.y <= 1)
1864 win->g.w = ev.xmotion.x;
1865 win->g.h = ev.xmotion.y;
1867 /* not free, don't sync more than 60 times / second */
1868 if ((ev.xmotion.time - time) > (1000 / 60) ) {
1869 time = ev.xmotion.time;
1870 XSync(display, False);
1871 resize_window(win, args->id);
1875 } while (ev.type != ButtonRelease);
1877 XSync(display, False);
1878 resize_window(win, args->id);
1880 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
1882 XUngrabPointer(display, CurrentTime);
1885 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1889 move_window(struct ws_win *win)
1893 struct swm_region *r;
1895 r = root_to_region(win->wa.root);
1896 bzero(&wc, sizeof wc);
1901 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
1902 win->id, wc.x, wc.y, wc.width, wc.height);
1904 XConfigureWindow(display, win->id, mask, &wc);
1909 move(struct ws_win *win, union arg *args)
1915 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %d\n",
1916 win->id, win->floating, win->transient);
1918 if (win->floating == 0) {
1924 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1925 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1927 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
1929 XMaskEvent(display, MOUSEMASK | ExposureMask |
1930 SubstructureRedirectMask, &ev);
1932 case ConfigureRequest:
1935 handler[ev.type](&ev);
1938 win->g.x = ev.xmotion.x_root;
1939 win->g.y = ev.xmotion.y_root;
1941 /* not free, don't sync more than 60 times / second */
1942 if ((ev.xmotion.time - time) > (1000 / 60) ) {
1943 time = ev.xmotion.time;
1944 XSync(display, False);
1949 } while (ev.type != ButtonRelease);
1951 XSync(display, False);
1954 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
1955 XUngrabPointer(display, CurrentTime);
1960 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1963 /* key definitions */
1965 char name[SWM_FUNCNAME_LEN];
1966 void (*func)(struct swm_region *r, union arg *);
1968 } keyfuncs[kf_invalid] = {
1969 /* name function argument */
1970 { "cycle_layout", cycle_layout, {0} },
1971 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1972 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1973 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1974 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1975 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1976 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
1977 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
1978 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1979 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1980 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1981 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1982 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1983 { "spawn_term", spawnterm, {.argv = spawn_term} },
1984 { "spawn_menu", spawnmenu, {.argv = spawn_menu} },
1985 { "quit", quit, {0} },
1986 { "restart", restart, {0} },
1987 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1988 { "ws_1", switchws, {.id = 0} },
1989 { "ws_2", switchws, {.id = 1} },
1990 { "ws_3", switchws, {.id = 2} },
1991 { "ws_4", switchws, {.id = 3} },
1992 { "ws_5", switchws, {.id = 4} },
1993 { "ws_6", switchws, {.id = 5} },
1994 { "ws_7", switchws, {.id = 6} },
1995 { "ws_8", switchws, {.id = 7} },
1996 { "ws_9", switchws, {.id = 8} },
1997 { "ws_10", switchws, {.id = 9} },
1998 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1999 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
2000 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
2001 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
2002 { "mvws_1", send_to_ws, {.id = 0} },
2003 { "mvws_2", send_to_ws, {.id = 1} },
2004 { "mvws_3", send_to_ws, {.id = 2} },
2005 { "mvws_4", send_to_ws, {.id = 3} },
2006 { "mvws_5", send_to_ws, {.id = 4} },
2007 { "mvws_6", send_to_ws, {.id = 5} },
2008 { "mvws_7", send_to_ws, {.id = 6} },
2009 { "mvws_8", send_to_ws, {.id = 7} },
2010 { "mvws_9", send_to_ws, {.id = 8} },
2011 { "mvws_10", send_to_ws, {.id = 9} },
2012 { "bar_toggle", bar_toggle, {0} },
2013 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
2014 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
2015 { "screenshot_all", screenshot, {.id = SWM_ARG_ID_SS_ALL} },
2016 { "screenshot_wind", screenshot, {.id = SWM_ARG_ID_SS_WINDOW} },
2017 { "float_toggle", floating_toggle,{0} },
2018 { "version", version, {0} },
2019 { "spawn_lock", spawn, {.argv = spawn_lock} },
2020 { "spawn_initscr", spawn, {.argv = spawn_initscr} },
2025 enum keyfuncid funcid;
2027 int keys_size = 0, keys_length = 0;
2028 struct key *keys = NULL;
2031 enum { client_click, root_click };
2033 unsigned int action;
2035 unsigned int button;
2036 void (*func)(struct ws_win *, union arg *);
2039 /* action key mouse button func args */
2040 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2041 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2042 { client_click, MODKEY, Button1, move, {0} },
2046 update_modkey(unsigned int mod)
2051 for (i = 0; i < keys_length; i++)
2052 if (keys[i].mod & ShiftMask)
2053 keys[i].mod = mod | ShiftMask;
2057 for (i = 0; i < LENGTH(buttons); i++)
2058 if (buttons[i].mask & ShiftMask)
2059 buttons[i].mask = mod | ShiftMask;
2061 buttons[i].mask = mod;
2064 #define SWM_MODNAME_SIZE 32
2065 #define SWM_KEY_WS "\n+ \t"
2067 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
2071 if (mod == NULL || ks == NULL)
2075 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
2077 cp += (long)strspn(cp, SWM_KEY_WS);
2078 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
2080 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
2082 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
2084 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
2086 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
2088 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
2090 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
2091 *mod |= ControlMask;
2093 *ks = XStringToKeysym(name);
2094 XConvertCase(*ks, ks, &uks);
2095 if (ks == NoSymbol) {
2097 "parsekeys: invalid key %s\n",
2106 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid)
2110 for (i = 0; i < keys_length; i++) {
2111 if (keys[i].mod == mod && keys[i].keysym == ks) {
2112 if (kfid == kf_invalid) {
2115 "setkeybinding: delete #%d %s\n",
2116 i, keyfuncs[keys[i].funcid].name);
2117 j = keys_length - 1;
2123 /* found: replace */
2125 "setkeybinding: replace #%d %s\n",
2126 i, keyfuncs[keys[i].funcid].name);
2128 keys[i].keysym = ks;
2129 keys[i].funcid = kfid;
2134 if (kfid == kf_invalid) {
2136 "error: setkeybinding: cannot find mod/key combination");
2139 /* not found: add */
2140 if (keys_size == 0 || keys == NULL) {
2142 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
2143 keys = malloc((size_t)keys_size * sizeof(struct key));
2145 fprintf(stderr, "malloc failed\n");
2149 } else if (keys_length == keys_size) {
2151 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
2152 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
2154 fprintf(stderr, "realloc failed\n");
2159 if (keys_length < keys_size) {
2160 DNPRINTF(SWM_D_KEY, "setkeybinding: add %d\n", keys_length);
2163 keys[j].keysym = ks;
2164 keys[j].funcid = kfid;
2166 fprintf(stderr, "keys array problem?\n");
2168 fprintf(stderr, "keys array problem\n");
2174 setconfbinding(char *selector, char *value, int flags)
2176 enum keyfuncid kfid;
2179 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
2180 if (strncasecmp(selector, keyfuncs[kfid].name,
2181 SWM_FUNCNAME_LEN) == 0) {
2182 if (parsekeys(value, mod_key, &mod, &ks))
2183 setkeybinding(mod, ks, kfid);
2194 setkeybinding(MODKEY, XK_space, kf_cycle_layout);
2195 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset);
2196 setkeybinding(MODKEY, XK_h, kf_master_shrink);
2197 setkeybinding(MODKEY, XK_l, kf_master_grow);
2198 setkeybinding(MODKEY, XK_comma, kf_master_add);
2199 setkeybinding(MODKEY, XK_period, kf_master_del);
2200 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc);
2201 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec);
2202 setkeybinding(MODKEY, XK_Return, kf_swap_main);
2203 setkeybinding(MODKEY, XK_j, kf_focus_next);
2204 setkeybinding(MODKEY, XK_k, kf_focus_prev);
2205 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next);
2206 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev);
2207 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term);
2208 setkeybinding(MODKEY, XK_p, kf_spawn_menu);
2209 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit);
2210 setkeybinding(MODKEY, XK_q, kf_restart);
2211 setkeybinding(MODKEY, XK_m, kf_focus_main);
2212 setkeybinding(MODKEY, XK_1, kf_ws_1);
2213 setkeybinding(MODKEY, XK_2, kf_ws_2);
2214 setkeybinding(MODKEY, XK_3, kf_ws_3);
2215 setkeybinding(MODKEY, XK_4, kf_ws_4);
2216 setkeybinding(MODKEY, XK_5, kf_ws_5);
2217 setkeybinding(MODKEY, XK_6, kf_ws_6);
2218 setkeybinding(MODKEY, XK_7, kf_ws_7);
2219 setkeybinding(MODKEY, XK_8, kf_ws_8);
2220 setkeybinding(MODKEY, XK_9, kf_ws_9);
2221 setkeybinding(MODKEY, XK_0, kf_ws_10);
2222 setkeybinding(MODKEY, XK_Right, kf_ws_next);
2223 setkeybinding(MODKEY, XK_Left, kf_ws_prev);
2224 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next);
2225 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev);
2226 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1);
2227 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2);
2228 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3);
2229 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4);
2230 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5);
2231 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6);
2232 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7);
2233 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8);
2234 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9);
2235 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10);
2236 setkeybinding(MODKEY, XK_b, kf_bar_toggle);
2237 setkeybinding(MODKEY, XK_Tab, kf_focus_next);
2238 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev);
2239 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill);
2240 setkeybinding(MODKEY, XK_x, kf_wind_del);
2241 setkeybinding(MODKEY, XK_s, kf_screenshot_all);
2242 setkeybinding(MODKEY|ShiftMask, XK_s, kf_screenshot_wind);
2243 setkeybinding(MODKEY, XK_t, kf_float_toggle);
2244 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version);
2245 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_lock);
2246 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_initscr);
2249 updatenumlockmask(void)
2252 XModifierKeymap *modmap;
2254 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
2256 modmap = XGetModifierMapping(display);
2257 for (i = 0; i < 8; i++)
2258 for (j = 0; j < modmap->max_keypermod; j++)
2259 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2260 == XKeysymToKeycode(display, XK_Num_Lock))
2261 numlockmask = (1 << i);
2263 XFreeModifiermap(modmap);
2269 unsigned int i, j, k;
2271 unsigned int modifiers[] =
2272 { 0, LockMask, numlockmask, numlockmask | LockMask };
2274 DNPRINTF(SWM_D_MISC, "grabkeys\n");
2275 updatenumlockmask();
2277 for (k = 0; k < ScreenCount(display); k++) {
2278 if (TAILQ_EMPTY(&screens[k].rl))
2280 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
2281 for (i = 0; i < keys_length; i++) {
2282 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
2283 for (j = 0; j < LENGTH(modifiers); j++)
2284 XGrabKey(display, code,
2285 keys[i].mod | modifiers[j],
2286 screens[k].root, True,
2287 GrabModeAsync, GrabModeAsync);
2293 grabbuttons(struct ws_win *win, int focused)
2296 unsigned int modifiers[] =
2297 { 0, LockMask, numlockmask, numlockmask|LockMask };
2299 updatenumlockmask();
2300 XUngrabButton(display, AnyButton, AnyModifier, win->id);
2302 for (i = 0; i < LENGTH(buttons); i++)
2303 if (buttons[i].action == client_click)
2304 for (j = 0; j < LENGTH(modifiers); j++)
2305 XGrabButton(display, buttons[i].button,
2306 buttons[i].mask | modifiers[j],
2307 win->id, False, BUTTONMASK,
2308 GrabModeAsync, GrabModeSync, None,
2311 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
2312 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
2318 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
2326 XKeyEvent *ev = &e->xkey;
2328 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
2330 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
2331 for (i = 0; i < keys_length; i++)
2332 if (keysym == keys[i].keysym
2333 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
2334 && keyfuncs[keys[i].funcid].func)
2335 keyfuncs[keys[i].funcid].func(
2336 root_to_region(ev->root),
2337 &(keyfuncs[keys[i].funcid].args)
2342 buttonpress(XEvent *e)
2344 XButtonPressedEvent *ev = &e->xbutton;
2349 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
2351 action = root_click;
2352 if ((win = find_window(ev->window)) == NULL)
2356 action = client_click;
2359 for (i = 0; i < LENGTH(buttons); i++)
2360 if (action == buttons[i].action && buttons[i].func &&
2361 buttons[i].button == ev->button &&
2362 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
2363 buttons[i].func(win, &buttons[i].args);
2367 set_win_state(struct ws_win *win, long state)
2369 long data[] = {state, None};
2371 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
2373 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
2374 (unsigned char *)data, 2);
2377 const char *quirkname[] = {
2378 "NONE", /* config string for "no value" */
2386 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
2387 #define SWM_Q_WS "\n|+ \t"
2389 parsequirks(char *qstr, unsigned long *quirk)
2397 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
2399 cp += (long)strspn(cp, SWM_Q_WS);
2400 for (i = 0; i < LENGTH(quirkname); i++) {
2401 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
2402 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
2407 *quirk |= 1 << (i-1);
2411 if (i >= LENGTH(quirkname)) {
2412 DNPRINTF(SWM_D_QUIRK,
2413 "parsequirks: invalid quirk [%s]\n", name);
2420 setquirk(const char *class, const char *name, const int quirk)
2424 for (i = 0; i < quirks_length; i++) {
2425 if (!strcmp(quirks[i].class, class) &&
2426 !strcmp(quirks[i].name, name)) {
2429 DNPRINTF(SWM_D_QUIRK,
2430 "setquirk: delete #%d %s:%s\n",
2431 i, quirks[i].class, quirks[i].name);
2432 free(quirks[i].class);
2433 free(quirks[i].name);
2434 j = quirks_length - 1;
2436 quirks[i] = quirks[j];
2440 /* found: replace */
2441 DNPRINTF(SWM_D_QUIRK,
2442 "setquirk: replace #%d %s:%s\n",
2443 i, quirks[i].class, quirks[i].name);
2444 free(quirks[i].class);
2445 free(quirks[i].name);
2446 quirks[i].class = strdup(class);
2447 quirks[i].name = strdup(name);
2448 quirks[i].quirk = quirk;
2455 "error: setquirk: cannot find class/name combination");
2458 /* not found: add */
2459 if (quirks_size == 0 || quirks == NULL) {
2461 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
2462 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
2464 fprintf(stderr, "setquirk: malloc failed\n");
2468 } else if (quirks_length == quirks_size) {
2470 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
2471 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
2473 fprintf(stderr, "setquirk: realloc failed\n");
2478 if (quirks_length < quirks_size) {
2479 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
2480 j = quirks_length++;
2481 quirks[j].class = strdup(class);
2482 quirks[j].name = strdup(name);
2483 quirks[j].quirk = quirk;
2485 fprintf(stderr, "quirks array problem?\n");
2487 fprintf(stderr, "quirks array problem!\n");
2493 setconfquirk(char *selector, char *value, int flags)
2495 char *cp, *class, *name;
2497 unsigned long quirks;
2498 if (selector == NULL)
2500 if ((cp = strchr(selector, ':')) == NULL)
2505 if ((retval = parsequirks(value, &quirks)))
2506 setquirk(class, name, quirks);
2513 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
2514 setquirk("OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT);
2515 setquirk("OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT);
2516 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
2517 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
2518 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2519 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
2520 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2521 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2522 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2523 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2524 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
2525 setquirk("pcb", "pcb", SWM_Q_FLOAT);
2528 /* conf file stuff */
2529 #define SWM_CONF_FILE "scrotwm.conf"
2531 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_CLOCK_ENABLED,
2532 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
2533 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED,
2534 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP,
2535 SWM_S_DIALOG_RATIO };
2538 setconfvalue(char *selector, char *value, int flags)
2541 case SWM_S_BAR_DELAY:
2542 bar_delay = atoi(value);
2544 case SWM_S_BAR_ENABLED:
2545 bar_enabled = atoi(value);
2547 case SWM_S_CLOCK_ENABLED:
2548 clock_enabled = atoi(value);
2550 case SWM_S_CYCLE_EMPTY:
2551 cycle_empty = atoi(value);
2553 case SWM_S_CYCLE_VISIBLE:
2554 cycle_visible = atoi(value);
2556 case SWM_S_SS_ENABLED:
2557 ss_enabled = atoi(value);
2559 case SWM_S_TERM_WIDTH:
2560 term_width = atoi(value);
2562 case SWM_S_TITLE_CLASS_ENABLED:
2563 title_class_enabled = atoi(value);
2565 case SWM_S_TITLE_NAME_ENABLED:
2566 title_name_enabled = atoi(value);
2568 case SWM_S_BAR_FONT:
2569 bar_fonts[0] = strdup(value);
2571 case SWM_S_BAR_ACTION:
2572 bar_argv[0] = strdup(value);
2574 case SWM_S_SPAWN_TERM:
2575 spawn_term[0] = strdup(value);
2578 spawn_screenshot[0] = strdup(value);
2580 case SWM_S_DIALOG_RATIO:
2581 dialog_ratio = atof(value);
2582 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
2592 setconfmodkey(char *selector, char *value, int flags)
2594 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
2595 update_modkey(Mod1Mask);
2596 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
2597 update_modkey(Mod2Mask);
2598 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
2599 update_modkey(Mod3Mask);
2600 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
2601 update_modkey(Mod4Mask);
2608 setconfcolor(char *selector, char *value, int flags)
2610 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
2615 setconfregion(char *selector, char *value, int flags)
2617 custom_region(value);
2621 /* config options */
2622 struct config_option {
2624 int (*func)(char*, char*, int);
2627 struct config_option configopt[] = {
2628 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
2629 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
2630 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
2631 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
2632 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
2633 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
2634 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
2635 { "bind", setconfbinding, 0 },
2636 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
2637 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
2638 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
2639 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
2640 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
2641 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
2642 { "modkey", setconfmodkey, 0 },
2643 { "quirk", setconfquirk, 0 },
2644 { "region", setconfregion, 0 },
2645 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
2646 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
2647 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
2648 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
2649 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
2650 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED }
2655 conf_load(char *filename)
2658 char *line, *cp, *optsub, *optval;
2659 size_t linelen, lineno = 0;
2660 int wordlen, i, optind;
2661 struct config_option *opt;
2662 if (filename == NULL)
2664 if ((config = fopen(filename, "r")) == NULL)
2666 while (!feof(config)) {
2667 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
2670 err(1, "%s", filename);
2675 cp += strspn(cp, " \t\n"); /* eat whitespace */
2676 if (cp[0] == '\0') {
2681 /* get config option */
2682 wordlen = strcspn(cp, "=[ \t\n");
2684 warnx("%s: line %zd: no option found",
2689 for (i = 0; i < LENGTH(configopt); i++) {
2690 opt = &configopt[i];
2691 if (!strncasecmp(cp, opt->optname, wordlen) &&
2692 strlen(opt->optname) == wordlen) {
2698 warnx("%s: line %zd: unknown option %.*s",
2699 filename, lineno, wordlen, cp);
2703 cp += strspn(cp, " \t\n"); /* eat whitespace */
2704 /* get [selector] if any */
2708 wordlen = strcspn(cp, "]");
2710 warnx("%s: line %zd: syntax error",
2714 asprintf(&optsub, "%.*s", wordlen, cp);
2716 cp += strspn(cp, "] \t\n"); /* eat trailing */
2718 cp += strspn(cp, "= \t\n"); /* eat trailing */
2720 optval = strdup(cp);
2721 /* call function to deal with it all */
2722 if (!configopt[optind].func(optsub, optval,
2723 configopt[optind].funcflags))
2724 errx(1, "%s: line %zd: invalid data for %s",
2725 filename, lineno, configopt[optind].optname);
2734 manage_window(Window id)
2737 struct workspace *ws;
2739 int format, i, ws_idx, n;
2740 unsigned long nitems, bytes;
2741 Atom ws_idx_atom = 0, type;
2742 Atom *prot = NULL, *pp;
2743 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
2744 struct swm_region *r;
2749 if ((win = find_window(id)) != NULL)
2750 return (win); /* already being managed */
2752 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
2753 errx(1, "calloc: failed to allocate memory for new window");
2755 /* Get all the window data in one shot */
2756 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2758 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
2759 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
2760 XGetWindowAttributes(display, id, &win->wa);
2761 XGetTransientForHint(display, id, &trans);
2762 XGetWMNormalHints(display, id, &win->sh, &mask); /* XXX function? */
2764 win->transient = trans;
2765 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
2766 (unsigned)win->id, win->transient);
2768 /* get supported protocols */
2769 if (XGetWMProtocols(display, id, &prot, &n)) {
2770 for (i = 0, pp = prot; i < n; i++, pp++)
2772 win->can_delete = 1;
2778 * Figure out where to put the window. If it was previously assigned to
2779 * a workspace (either by spawn() or manually moving), and isn't
2780 * transient, * put it in the same workspace
2782 r = root_to_region(win->wa.root);
2783 if (prop && win->transient == 0) {
2784 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
2785 ws_idx = strtonum(prop, 0, 9, &errstr);
2787 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
2790 ws = &r->s->ws[ws_idx];
2794 /* set up the window layout */
2797 win->s = r->s; /* this never changes */
2798 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
2800 win->g.w = win->wa.width;
2801 win->g.h = win->wa.height;
2802 win->g.x = win->wa.x;
2803 win->g.y = win->wa.y;
2805 /* Set window properties so we can remember this after reincarnation */
2806 if (ws_idx_atom && prop == NULL &&
2807 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
2808 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2810 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2811 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2815 if (XGetClassHint(display, win->id, &win->ch)) {
2816 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
2817 win->ch.res_class, win->ch.res_name);
2818 for (i = 0; i < quirks_length; i++){
2819 if (!strcmp(win->ch.res_class, quirks[i].class) &&
2820 !strcmp(win->ch.res_name, quirks[i].name)) {
2821 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
2822 win->ch.res_class, win->ch.res_name);
2823 if (quirks[i].quirk & SWM_Q_FLOAT)
2825 win->quirks = quirks[i].quirk;
2830 /* alter window position if quirky */
2831 if (win->quirks & SWM_Q_ANYWHERE) {
2832 win->manual = 1; /* don't center the quirky windows */
2833 bzero(&wc, sizeof wc);
2835 if (win->g.y < bar_height) {
2836 win->g.y = wc.y = bar_height;
2839 if (win->g.w + win->g.x > WIDTH(r)) {
2840 win->g.x = wc.x = WIDTH(win->ws->r) - win->g.w - 2;
2843 wc.border_width = 1;
2844 mask |= CWBorderWidth;
2845 XConfigureWindow(display, win->id, mask, &wc);
2848 /* Reset font sizes (the bruteforce way; no default keybinding). */
2849 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
2850 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
2851 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2852 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
2853 fake_keypress(win, XK_KP_Add, ShiftMask);
2856 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
2857 PropertyChangeMask | StructureNotifyMask);
2859 set_win_state(win, NormalState);
2861 /* floaters need to be mapped if they are in the current workspace */
2862 if (win->floating && (ws->idx == r->ws->idx))
2863 XMapRaised(display, win->id);
2865 /* make new win focused */
2872 unmanage_window(struct ws_win *win)
2874 struct workspace *ws;
2879 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
2881 /* don't unmanage if we are switching workspaces */
2886 /* find a window to focus */
2887 if (ws->focus == win)
2888 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
2889 if (ws->focus == NULL)
2890 ws->focus = TAILQ_FIRST(&ws->winlist);
2891 if (ws->focus == NULL || ws->focus == win) {
2895 focus_win(ws->focus);
2896 if (ws->focus_prev == win)
2897 ws->focus_prev = NULL;
2899 TAILQ_REMOVE(&win->ws->winlist, win, entry);
2900 set_win_state(win, WithdrawnState);
2901 if (win->ch.res_class)
2902 XFree(win->ch.res_class);
2903 if (win->ch.res_name)
2904 XFree(win->ch.res_name);
2909 configurerequest(XEvent *e)
2911 XConfigureRequestEvent *ev = &e->xconfigurerequest;
2916 if ((win = find_window(ev->window)) == NULL)
2920 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
2922 bzero(&wc, sizeof wc);
2925 wc.width = ev->width;
2926 wc.height = ev->height;
2927 wc.border_width = ev->border_width;
2928 wc.sibling = ev->above;
2929 wc.stack_mode = ev->detail;
2930 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
2932 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
2934 if (win->floating) {
2935 if (ev->value_mask & CWX)
2937 if (ev->value_mask & CWY)
2939 if (ev->value_mask & CWWidth)
2940 win->g.w = ev->width;
2941 if (ev->value_mask & CWHeight)
2942 win->g.h = ev->height;
2943 if (win->ws->r != NULL) {
2944 /* this seems to be full screen */
2945 if (win->g.w >= WIDTH(win->ws->r)) {
2947 win->g.w = WIDTH(win->ws->r);
2948 ev->value_mask |= CWX | CWWidth;
2950 if (win->g.h >= HEIGHT(win->ws->r)) {
2953 win->g.h = HEIGHT(win->ws->r);
2954 ev->value_mask |= CWY | CWHeight;
2957 if ((ev->value_mask & (CWX | CWY)) &&
2958 !(ev->value_mask & (CWWidth | CWHeight)))
2960 XMoveResizeWindow(display, win->id,
2961 win->g.x, win->g.y, win->g.w, win->g.h);
2968 configurenotify(XEvent *e)
2973 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
2974 e->xconfigure.window);
2976 XMapWindow(display, e->xconfigure.window);
2977 win = find_window(e->xconfigure.window);
2979 XGetWMNormalHints(display, win->id, &win->sh, &mask);
2981 XMapWindow(display, win->id);
2988 destroynotify(XEvent *e)
2991 XDestroyWindowEvent *ev = &e->xdestroywindow;
2993 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
2995 if ((win = find_window(ev->window)) != NULL) {
2996 unmanage_window(win);
3002 enternotify(XEvent *e)
3004 XCrossingEvent *ev = &e->xcrossing;
3007 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
3010 /* eat event(r) to prevent autofocus */
3015 if ((win = find_window(ev->window)) != NULL)
3022 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
3028 DNPRINTF(SWM_D_EVENT, "focusout: window: %lu\n", e->xfocus.window);
3030 if (cur_focus && cur_focus->ws->r &&
3031 cur_focus->id == e->xfocus.window) {
3032 struct swm_screen *s = cur_focus->ws->r->s;
3037 /* Try to detect synergy hiding the cursor. */
3038 if (XQueryPointer(display, cur_focus->id,
3039 &rr, &cr, &x, &y, &wx, &wy, &mask) != False &&
3041 x == DisplayWidth(display, s->idx)/2 &&
3042 y == DisplayHeight(display, s->idx)/2) {
3043 unfocus_win(cur_focus);
3049 mappingnotify(XEvent *e)
3051 XMappingEvent *ev = &e->xmapping;
3053 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
3055 XRefreshKeyboardMapping(ev);
3056 if (ev->request == MappingKeyboard)
3061 maprequest(XEvent *e)
3063 XMapRequestEvent *ev = &e->xmaprequest;
3064 XWindowAttributes wa;
3066 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
3067 e->xmaprequest.window);
3069 if (!XGetWindowAttributes(display, ev->window, &wa))
3071 if (wa.override_redirect)
3073 manage_window(e->xmaprequest.window);
3079 propertynotify(XEvent *e)
3082 XPropertyEvent *ev = &e->xproperty;
3084 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
3087 if (ev->state == PropertyDelete)
3088 return; /* ignore */
3089 win = find_window(ev->window);
3094 case XA_WM_NORMAL_HINTS:
3097 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3098 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
3099 if (win->sh.flags & PMinSize) {
3100 win->g.w = win->sh.min_width;
3101 win->g.h = win->sh.min_height;
3102 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
3104 XMoveResizeWindow(display, win->id,
3105 win->g.x, win->g.y, win->g.w, win->g.h);
3114 unmapnotify(XEvent *e)
3116 XDestroyWindowEvent *ev = &e->xdestroywindow;
3119 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
3121 if ((win = find_window(ev->window)) != NULL)
3123 unmanage_window(win);
3127 visibilitynotify(XEvent *e)
3130 struct swm_region *r;
3132 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
3133 e->xvisibility.window);
3134 if (e->xvisibility.state == VisibilityUnobscured)
3135 for (i = 0; i < ScreenCount(display); i++)
3136 TAILQ_FOREACH(r, &screens[i].rl, entry)
3137 if (e->xvisibility.window == r->bar_window)
3142 xerror_start(Display *d, XErrorEvent *ee)
3149 xerror(Display *d, XErrorEvent *ee)
3151 /* fprintf(stderr, "error: %p %p\n", display, ee); */
3159 xerrorxlib = XSetErrorHandler(xerror_start);
3161 /* this causes an error if some other window manager is running */
3162 XSelectInput(display, DefaultRootWindow(display),
3163 SubstructureRedirectMask);
3164 XSync(display, False);
3168 XSetErrorHandler(xerror);
3169 XSync(display, False);
3178 unsigned char *p = NULL;
3179 unsigned long n, extra;
3182 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
3183 &real, &format, &n, &extra, (unsigned char **)&p);
3184 if (status != Success)
3187 result = *((long *)p);
3193 new_region(struct swm_screen *s, int x, int y, int w, int h)
3195 struct swm_region *r, *n;
3196 struct workspace *ws = NULL;
3199 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
3200 s->idx, w, h, x, y);
3202 /* remove any conflicting regions */
3203 n = TAILQ_FIRST(&s->rl);
3206 n = TAILQ_NEXT(r, entry);
3207 if (X(r) < (x + w) &&
3208 (X(r) + WIDTH(r)) > x &&
3210 (Y(r) + HEIGHT(r)) > y) {
3211 XDestroyWindow(display, r->bar_window);
3212 TAILQ_REMOVE(&s->rl, r, entry);
3213 TAILQ_INSERT_TAIL(&s->orl, r, entry);
3217 /* search old regions for one to reuse */
3219 /* size + location match */
3220 TAILQ_FOREACH(r, &s->orl, entry)
3221 if (X(r) == x && Y(r) == y &&
3222 HEIGHT(r) == h && WIDTH(r) == w)
3226 TAILQ_FOREACH(r, &s->orl, entry)
3227 if (HEIGHT(r) == h && WIDTH(r) == w)
3231 TAILQ_REMOVE(&s->orl, r, entry);
3232 /* try to use old region's workspace */
3233 if (r->ws->r == NULL)
3236 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
3237 errx(1, "calloc: failed to allocate memory for screen");
3239 /* if we don't have a workspace already, find one */
3241 for (i = 0; i < SWM_WS_MAX; i++)
3242 if (s->ws[i].r == NULL) {
3249 errx(1, "no free workspaces\n");
3258 TAILQ_INSERT_TAIL(&s->rl, r, entry);
3264 #ifdef SWM_XRR_HAS_CRTC
3266 XRRScreenResources *sr;
3269 #endif /* SWM_XRR_HAS_CRTC */
3270 struct swm_region *r;
3273 if (i >= ScreenCount(display))
3274 errx(1, "invalid screen");
3276 /* remove any old regions */
3277 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
3279 XDestroyWindow(display, r->bar_window);
3280 TAILQ_REMOVE(&screens[i].rl, r, entry);
3281 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
3284 /* map virtual screens onto physical screens */
3285 #ifdef SWM_XRR_HAS_CRTC
3286 if (xrandr_support) {
3287 sr = XRRGetScreenResources(display, screens[i].root);
3289 new_region(&screens[i], 0, 0,
3290 DisplayWidth(display, i),
3291 DisplayHeight(display, i));
3295 for (c = 0, ci = NULL; c < ncrtc; c++) {
3296 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
3297 if (ci->noutput == 0)
3300 if (ci != NULL && ci->mode == None)
3301 new_region(&screens[i], 0, 0,
3302 DisplayWidth(display, i),
3303 DisplayHeight(display, i));
3305 new_region(&screens[i],
3306 ci->x, ci->y, ci->width, ci->height);
3309 XRRFreeCrtcInfo(ci);
3310 XRRFreeScreenResources(sr);
3312 #endif /* SWM_XRR_HAS_CRTC */
3314 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
3315 DisplayHeight(display, i));
3320 screenchange(XEvent *e) {
3321 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
3322 struct swm_region *r;
3326 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
3328 if (!XRRUpdateConfiguration(e))
3331 /* silly event doesn't include the screen index */
3332 for (i = 0; i < ScreenCount(display); i++)
3333 if (screens[i].root == xe->root)
3335 if (i >= ScreenCount(display))
3336 errx(1, "screenchange: screen not found\n");
3338 /* brute force for now, just re-enumerate the regions */
3341 /* hide any windows that went away */
3342 TAILQ_FOREACH(r, &screens[i].rl, entry)
3343 TAILQ_FOREACH(win, &r->ws->winlist, entry)
3344 XUnmapWindow(display, win->id);
3346 /* add bars to all regions */
3347 for (i = 0; i < ScreenCount(display); i++)
3348 TAILQ_FOREACH(r, &screens[i].rl, entry)
3356 Window d1, d2, *wins = NULL;
3357 XWindowAttributes wa;
3360 int errorbase, major, minor;
3361 struct workspace *ws;
3365 if ((screens = calloc(ScreenCount(display),
3366 sizeof(struct swm_screen))) == NULL)
3367 errx(1, "calloc: screens");
3369 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3371 /* initial Xrandr setup */
3372 xrandr_support = XRRQueryExtension(display,
3373 &xrandr_eventbase, &errorbase);
3375 if (XRRQueryVersion(display, &major, &minor) && major < 1)
3378 /* map physical screens */
3379 for (i = 0; i < ScreenCount(display); i++) {
3380 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
3382 TAILQ_INIT(&screens[i].rl);
3383 TAILQ_INIT(&screens[i].orl);
3384 screens[i].root = RootWindow(display, i);
3386 /* set default colors */
3387 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
3388 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
3389 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
3390 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
3391 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
3393 /* init all workspaces */
3394 /* XXX these should be dynamically allocated too */
3395 for (j = 0; j < SWM_WS_MAX; j++) {
3396 ws = &screens[i].ws[j];
3401 TAILQ_INIT(&ws->winlist);
3403 for (k = 0; layouts[k].l_stack != NULL; k++)
3404 if (layouts[k].l_config != NULL)
3405 layouts[k].l_config(ws,
3406 SWM_ARG_ID_STACKINIT);
3407 ws->cur_layout = &layouts[0];
3409 /* grab existing windows (before we build the bars)*/
3410 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
3416 XRRSelectInput(display, screens[i].root,
3417 RRScreenChangeNotifyMask);
3419 /* attach windows to a region */
3420 /* normal windows */
3421 for (j = 0; j < no; j++) {
3422 XGetWindowAttributes(display, wins[j], &wa);
3423 if (!XGetWindowAttributes(display, wins[j], &wa) ||
3424 wa.override_redirect ||
3425 XGetTransientForHint(display, wins[j], &d1))
3428 if (wa.map_state == IsViewable ||
3429 getstate(wins[j]) == NormalState)
3430 manage_window(wins[j]);
3432 /* transient windows */
3433 for (j = 0; j < no; j++) {
3434 if (!XGetWindowAttributes(display, wins[j], &wa))
3437 if (XGetTransientForHint(display, wins[j], &d1) &&
3438 (wa.map_state == IsViewable || getstate(wins[j]) ==
3440 manage_window(wins[j]);
3453 Atom netwmcheck, netwmname, utf8_string;
3456 /* work around sun jdk bugs, code from wmname */
3457 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
3458 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
3459 utf8_string = XInternAtom(display, "UTF8_STRING", False);
3460 for (i = 0; i < ScreenCount(display); i++) {
3461 root = screens[i].root;
3462 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
3463 PropModeReplace, (unsigned char *)&root, 1);
3464 XChangeProperty(display, root, netwmname, utf8_string, 8,
3465 PropModeReplace, "LG3D", strlen("LG3D"));
3470 main(int argc, char *argv[])
3473 struct swm_region *r;
3474 char conf[PATH_MAX], *cfile = NULL;
3481 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
3482 SWM_VERSION, cvstag);
3483 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
3484 warnx("no locale support");
3486 if (!(display = XOpenDisplay(0)))
3487 errx(1, "can not open display");
3490 errx(1, "other wm running");
3492 astate = XInternAtom(display, "WM_STATE", False);
3493 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
3494 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
3496 /* look for local and global conf file */
3497 pwd = getpwuid(getuid());
3499 errx(1, "invalid user %d", getuid());
3505 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
3506 if (stat(conf, &sb) != -1) {
3507 if (S_ISREG(sb.st_mode))
3510 /* try global conf file */
3511 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
3512 if (!stat(conf, &sb))
3513 if (S_ISREG(sb.st_mode))
3519 /* setup all bars */
3520 for (i = 0; i < ScreenCount(display); i++)
3521 TAILQ_FOREACH(r, &screens[i].rl, entry)
3524 /* set some values to work around bad programs */
3530 xfd = ConnectionNumber(display);
3534 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
3536 errx(1, "select failed");
3541 while (XPending(display)) {
3542 XNextEvent(display, &e);
3543 if (e.type < LASTEvent) {
3544 if (handler[e.type])
3545 handler[e.type](&e);
3547 DNPRINTF(SWM_D_EVENT,
3548 "win: %lu unknown event: %d\n",
3549 e.xany.window, e.type);
3551 switch (e.type - xrandr_eventbase) {
3552 case RRScreenChangeNotify:
3556 DNPRINTF(SWM_D_EVENT,
3557 "win: %lu unknown xrandr event: "
3558 "%d\n", e.xany.window, e.type);
3565 XCloseDisplay(display);