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>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * Much code and ideas taken from dwm under the following license:
21 * MIT/X Consortium License
23 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
24 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
25 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
26 * 2007 Premysl Hruby <dfenze at gmail dot com>
27 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
28 * 2007 Christof Musik <christof at sendfax dot de>
29 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
30 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
31 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
33 * Permission is hereby granted, free of charge, to any person obtaining a
34 * copy of this software and associated documentation files (the "Software"),
35 * to deal in the Software without restriction, including without limitation
36 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
37 * and/or sell copies of the Software, and to permit persons to whom the
38 * Software is furnished to do so, subject to the following conditions:
40 * The above copyright notice and this permission notice shall be included in
41 * all copies or substantial portions of the Software.
43 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
46 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
47 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
48 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
49 * DEALINGS IN THE SOFTWARE.
52 static const char *cvstag = "$scrotwm$";
54 #define SWM_VERSION "0.9.5"
70 #include <sys/types.h>
74 #include <sys/queue.h>
75 #include <sys/param.h>
76 #include <sys/select.h>
78 #include <X11/cursorfont.h>
79 #include <X11/keysym.h>
80 #include <X11/Xatom.h>
82 #include <X11/Xproto.h>
83 #include <X11/Xutil.h>
84 #include <X11/extensions/Xrandr.h>
87 # error XRandR versions less than 1.0 are not supported
92 #define SWM_XRR_HAS_CRTC
96 /* #define SWM_DEBUG */
98 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
99 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
100 #define SWM_D_MISC 0x0001
101 #define SWM_D_EVENT 0x0002
102 #define SWM_D_WS 0x0004
103 #define SWM_D_FOCUS 0x0008
104 #define SWM_D_MOVE 0x0010
105 #define SWM_D_STACK 0x0020
106 #define SWM_D_MOUSE 0x0040
107 #define SWM_D_PROP 0x0080
108 #define SWM_D_CLASS 0x0100
109 #define SWM_D_KEY 0x0200
110 #define SWM_D_QUIRK 0x0400
112 u_int32_t swm_debug = 0
126 #define DPRINTF(x...)
127 #define DNPRINTF(n,x...)
130 #define LENGTH(x) (sizeof x / sizeof x[0])
131 #define MODKEY Mod1Mask
132 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
133 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
134 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
135 #define SWM_PROPLEN (16)
136 #define SWM_FUNCNAME_LEN (32)
137 #define SWM_KEYS_LEN (255)
138 #define SWM_QUIRK_LEN (32)
139 #define X(r) (r)->g.x
140 #define Y(r) (r)->g.y
141 #define WIDTH(r) (r)->g.w
142 #define HEIGHT(r) (r)->g.h
143 #define SWM_MAX_FONT_STEPS (3)
146 #define SWM_LIB "/usr/X11R6/lib/swmhack.so"
153 int (*xerrorxlib)(Display *, XErrorEvent *);
158 int xrandr_eventbase;
159 int ignore_enter = 0;
160 unsigned int numlockmask = 0;
164 int cycle_visible = 0;
166 int font_adjusted = 0;
169 double dialog_ratio = .6;
171 #define SWM_BAR_MAX (256)
172 char *bar_argv[] = { NULL, NULL };
174 char bar_ext[SWM_BAR_MAX];
175 char bar_vertext[SWM_BAR_MAX];
177 sig_atomic_t bar_alarm = 0;
181 int bar_extra_running = 0;
184 int clock_enabled = 1;
190 char *bar_fonts[] = {
191 "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*",
192 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
196 /* terminal + args */
197 char *spawn_term[] = { "xterm", NULL };
198 char *spawn_screenshot[] = { "screenshot.sh", NULL, NULL };
199 char *spawn_lock[] = { "xlock", NULL };
200 char *spawn_initscr[] = { "initscreen.sh", NULL };
201 char *spawn_menu[] = { "dmenu_run", "-fn", NULL, "-nb", NULL,
202 "-nf", NULL, "-sb", NULL, "-sf", NULL, NULL };
204 #define SWM_MENU_FN (2)
205 #define SWM_MENU_NB (4)
206 #define SWM_MENU_NF (6)
207 #define SWM_MENU_SB (8)
208 #define SWM_MENU_SF (10)
210 /* layout manager data */
211 struct swm_geometry {
221 /* virtual "screens" */
223 TAILQ_ENTRY(swm_region) entry;
224 struct swm_geometry g;
225 struct workspace *ws; /* current workspace on this region */
226 struct swm_screen *s; /* screen idx */
229 TAILQ_HEAD(swm_region_list, swm_region);
232 TAILQ_ENTRY(ws_win) entry;
234 struct swm_geometry g;
239 int font_size_boundary[SWM_MAX_FONT_STEPS];
243 unsigned long quirks;
244 struct workspace *ws; /* always valid */
245 struct swm_screen *s; /* always valid, never changes */
246 XWindowAttributes wa;
250 TAILQ_HEAD(ws_win_list, ws_win);
252 /* user/key callable function IDs */
308 /* layout handlers */
310 void vertical_config(struct workspace *, int);
311 void vertical_stack(struct workspace *, struct swm_geometry *);
312 void horizontal_config(struct workspace *, int);
313 void horizontal_stack(struct workspace *, struct swm_geometry *);
314 void max_stack(struct workspace *, struct swm_geometry *);
316 void grabbuttons(struct ws_win *, int);
317 void new_region(struct swm_screen *, int, int, int, int);
318 void update_modkey(unsigned int);
319 /* user functions / key binding */
320 int bindmatch(const char *var, const char *name, unsigned int currmod, char *keystr,
321 enum keyfuncid *kfid, unsigned int *mod, KeySym *ks);
322 void setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid);
324 int quirkmatch(const char *var, const char *name, char *qstr,
325 char *qclass, char *qname, unsigned long *qquirk);
326 void setquirk(const char *class, const char *name, const int quirk);
329 void (*l_stack)(struct workspace *, struct swm_geometry *);
330 void (*l_config)(struct workspace *, int);
332 /* stack, configure */
333 { vertical_stack, vertical_config},
334 { horizontal_stack, horizontal_config},
339 #define SWM_H_SLICE (32)
340 #define SWM_V_SLICE (32)
342 /* define work spaces */
344 int idx; /* workspace index */
345 int restack; /* restack on switch */
346 struct layout *cur_layout; /* current layout handlers */
347 struct ws_win *focus; /* may be NULL */
348 struct ws_win *focus_prev; /* may be NULL */
349 struct swm_region *r; /* may be NULL */
350 struct ws_win_list winlist; /* list of windows in ws */
354 int horizontal_msize;
356 int horizontal_stacks;
363 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
364 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
366 /* physical screen mapping */
367 #define SWM_WS_MAX (10) /* XXX Too small? */
369 int idx; /* screen index */
370 struct swm_region_list rl; /* list of regions on this screen */
371 struct swm_region_list orl; /* list of old regions */
373 struct workspace ws[SWM_WS_MAX];
379 } c[SWM_S_COLOR_MAX];
381 struct swm_screen *screens;
384 struct ws_win *cur_focus = NULL;
386 /* args to functions */
389 #define SWM_ARG_ID_FOCUSNEXT (0)
390 #define SWM_ARG_ID_FOCUSPREV (1)
391 #define SWM_ARG_ID_FOCUSMAIN (2)
392 #define SWM_ARG_ID_SWAPNEXT (3)
393 #define SWM_ARG_ID_SWAPPREV (4)
394 #define SWM_ARG_ID_SWAPMAIN (5)
395 #define SWM_ARG_ID_MASTERSHRINK (6)
396 #define SWM_ARG_ID_MASTERGROW (7)
397 #define SWM_ARG_ID_MASTERADD (8)
398 #define SWM_ARG_ID_MASTERDEL (9)
399 #define SWM_ARG_ID_STACKRESET (10)
400 #define SWM_ARG_ID_STACKINIT (11)
401 #define SWM_ARG_ID_CYCLEWS_UP (12)
402 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
403 #define SWM_ARG_ID_CYCLESC_UP (14)
404 #define SWM_ARG_ID_CYCLESC_DOWN (15)
405 #define SWM_ARG_ID_STACKINC (16)
406 #define SWM_ARG_ID_STACKDEC (17)
407 #define SWM_ARG_ID_SS_ALL (0)
408 #define SWM_ARG_ID_SS_WINDOW (1)
409 #define SWM_ARG_ID_DONTCENTER (0)
410 #define SWM_ARG_ID_CENTER (1)
411 #define SWM_ARG_ID_KILLWINDOW (0)
412 #define SWM_ARG_ID_DELETEWINDOW (1)
418 char class[SWM_QUIRK_LEN];
419 char name[SWM_QUIRK_LEN];
421 #define SWM_Q_FLOAT (1<<0) /* float this window */
422 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
423 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
424 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
425 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
427 int quirks_size = 0, quirks_length = 0;
428 struct quirk *quirks = NULL;
431 void expose(XEvent *);
432 void keypress(XEvent *);
433 void buttonpress(XEvent *);
434 void configurerequest(XEvent *);
435 void configurenotify(XEvent *);
436 void destroynotify(XEvent *);
437 void enternotify(XEvent *);
438 void focusin(XEvent *);
439 void focusout(XEvent *);
440 void mappingnotify(XEvent *);
441 void maprequest(XEvent *);
442 void propertynotify(XEvent *);
443 void unmapnotify(XEvent *);
444 void visibilitynotify(XEvent *);
446 void (*handler[LASTEvent])(XEvent *) = {
448 [KeyPress] = keypress,
449 [ButtonPress] = buttonpress,
450 [ConfigureRequest] = configurerequest,
451 [ConfigureNotify] = configurenotify,
452 [DestroyNotify] = destroynotify,
453 [EnterNotify] = enternotify,
455 [FocusOut] = focusout,
456 [MappingNotify] = mappingnotify,
457 [MapRequest] = maprequest,
458 [PropertyNotify] = propertynotify,
459 [UnmapNotify] = unmapnotify,
460 [VisibilityNotify] = visibilitynotify,
464 name_to_color(char *colorname)
468 XColor screen_def, exact_def;
469 unsigned long result = 0;
470 char cname[32] = "#";
472 cmap = DefaultColormap(display, screens[0].idx);
473 status = XAllocNamedColor(display, cmap, colorname,
474 &screen_def, &exact_def);
476 strlcat(cname, colorname + 2, sizeof cname - 1);
477 status = XAllocNamedColor(display, cmap, cname, &screen_def,
481 result = screen_def.pixel;
483 fprintf(stderr, "color '%s' not found.\n", colorname);
489 setscreencolor(char *val, int i, int c)
491 if (i > 0 && i <= ScreenCount(display)) {
492 screens[i - 1].c[c].color = name_to_color(val);
493 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
495 } else if (i == -1) {
496 for (i = 0; i < ScreenCount(display); i++)
497 screens[i].c[c].color = name_to_color(val);
498 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
501 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
502 i, ScreenCount(display));
506 custom_region(char *val)
508 unsigned int sidx, x, y, w, h;
510 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
511 errx(1, "invalid custom region, "
512 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
513 if (sidx < 1 || sidx > ScreenCount(display))
514 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
515 sidx, ScreenCount(display));
519 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
521 if (x < 0 || x > DisplayWidth(display, sidx) ||
522 y < 0 || y > DisplayHeight(display, sidx) ||
523 w + x > DisplayWidth(display, sidx) ||
524 h + y > DisplayHeight(display, sidx))
525 errx(1, "region %ux%u+%u+%u not within screen boundaries "
526 "(%ux%u)\n", w, h, x, y,
527 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
529 new_region(&screens[sidx], x, y, w, h);
533 varmatch(char *var, char *name, int *index)
538 i = strncmp(var, name, 255);
545 p = var + strlen(name);
548 bzero(buf, sizeof buf);
550 while (isdigit(*p) && i < sizeof buf)
552 if (i == 0 || i >= sizeof buf || *p != ']')
554 *index = strtonum(buf, 0, 99, NULL);
558 /* conf file stuff */
559 #define SWM_CONF_WS "\n= \t"
560 #define SWM_CONF_FILE "scrotwm.conf"
562 conf_load(char *filename)
565 char *line, *cp, *var, *val;
566 size_t len, lineno = 0;
568 unsigned int modkey = MODKEY, modmask;
571 char class[SWM_QUIRK_LEN];
572 char name[SWM_QUIRK_LEN];
575 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
577 if (filename == NULL)
580 if ((config = fopen(filename, "r")) == NULL)
583 for (sc = ScreenCount(display);;) {
584 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
588 cp += (long)strspn(cp, SWM_CONF_WS);
594 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
596 cp += (long)strspn(cp, SWM_CONF_WS);
597 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
600 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
603 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
604 bar_enabled = atoi(val);
605 else if (!varmatch(var, "bar_border", &i))
606 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
607 else if (!varmatch(var, "bar_color", &i))
608 setscreencolor(val, i, SWM_S_COLOR_BAR);
609 else if (!varmatch(var, "bar_font_color", &i))
610 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
611 else if (!strncmp(var, "bar_font", strlen("bar_font")))
612 asprintf(&bar_fonts[0], "%s", val);
613 else if (!strncmp(var, "bar_action", strlen("bar_action")))
614 asprintf(&bar_argv[0], "%s", val);
615 else if (!strncmp(var, "bar_delay", strlen("bar_delay")))
616 bar_delay = atoi(val);
617 else if (!bindmatch(var, "bind", modkey, val,
618 &kfid, &modmask, &ks))
619 setkeybinding(modmask, ks, kfid);
625 if (!strncmp(var, "clock_enabled", strlen("clock_enabled")))
626 clock_enabled = atoi(val);
627 else if (!varmatch(var, "color_focus", &i))
628 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
629 else if (!varmatch(var, "color_unfocus", &i))
630 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
631 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
632 cycle_visible = atoi(val);
633 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
634 cycle_visible = atoi(val);
640 if (!strncmp(var, "dialog_ratio",
641 strlen("dialog_ratio"))) {
642 dialog_ratio = atof(val);
643 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
650 if (!strncmp(var, "modkey", strlen("modkey"))) {
652 if (!strncmp(val, "Mod2", strlen("Mod2")))
654 else if (!strncmp(val, "Mod3", strlen("Mod3")))
656 else if (!strncmp(val, "Mod4", strlen("Mod4")))
660 update_modkey(modkey);
666 if (!quirkmatch(var, "quirk", val, class, name, &quirk))
667 setquirk(class, name, quirk);
673 if (!strncmp(var, "region", strlen("region")))
680 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
681 asprintf(&spawn_term[0], "%s", val);
682 else if (!strncmp(var, "screenshot_enabled",
683 strlen("screenshot_enabled")))
684 ss_enabled = atoi(val);
685 else if (!strncmp(var, "screenshot_app",
686 strlen("screenshot_app")))
687 asprintf(&spawn_screenshot[0], "%s", val);
693 if (!strncmp(var, "term_width", strlen("term_width")))
694 term_width = atoi(val);
709 errx(1, "invalid conf file entry: %s=%s", var, val);
713 socket_setnonblock(int fd)
717 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
718 err(1, "fcntl F_GETFL");
720 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
721 err(1, "fcntl F_SETFL");
725 bar_print(struct swm_region *r, char *s)
727 XClearWindow(display, r->bar_window);
728 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
729 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
738 bzero(bar_pipe, sizeof bar_pipe);
741 kill(bar_pid, SIGTERM);
744 strlcpy(bar_ext, "", sizeof bar_ext);
753 struct swm_region *r;
757 char loc[SWM_BAR_MAX];
760 if (bar_enabled == 0)
762 if (bar_extra && bar_extra_running) {
763 /* ignore short reads; it'll correct itself */
764 while ((b = fgetln(stdin, &len)) != NULL)
765 if (b && b[len - 1] == '\n') {
767 strlcpy(bar_ext, b, sizeof bar_ext);
769 if (b == NULL && errno != EAGAIN) {
770 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
771 errno, strerror(errno));
775 strlcpy(bar_ext, "", sizeof bar_ext);
777 if (clock_enabled == 0)
778 strlcpy(s, "", sizeof s);
781 localtime_r(&tmt, &tm);
782 strftime(s, sizeof s, "%a %b %d %R %Z %Y ", &tm);
784 for (i = 0; i < ScreenCount(display); i++) {
786 TAILQ_FOREACH(r, &screens[i].rl, entry) {
787 snprintf(loc, sizeof loc, "%d:%d %s%s %s",
788 x++, r->ws->idx + 1, s, bar_ext, bar_vertext);
792 XSync(display, False);
803 bar_toggle(struct swm_region *r, union arg *args)
805 struct swm_region *tmpr;
806 int i, j, sc = ScreenCount(display);
808 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
811 for (i = 0; i < sc; i++)
812 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
813 XUnmapWindow(display, tmpr->bar_window);
815 for (i = 0; i < sc; i++)
816 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
817 XMapRaised(display, tmpr->bar_window);
819 bar_enabled = !bar_enabled;
820 for (i = 0; i < sc; i++)
821 for (j = 0; j < SWM_WS_MAX; j++)
822 screens[i].ws[j].restack = 1;
825 /* must be after stack */
832 XSetWindowAttributes wa;
833 struct swm_region *r;
836 /* do this here because the conf file is in memory */
837 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
838 /* launch external status app */
839 bar_extra_running = 1;
840 if (pipe(bar_pipe) == -1)
841 err(1, "pipe error");
842 socket_setnonblock(bar_pipe[0]);
843 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
844 if (dup2(bar_pipe[0], 0) == -1)
846 if (dup2(bar_pipe[1], 1) == -1)
848 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
849 err(1, "could not disable SIGPIPE");
850 switch (bar_pid = fork()) {
852 err(1, "cannot fork");
856 execvp(bar_argv[0], bar_argv);
857 err(1, "%s external app failed", bar_argv[0]);
859 default: /* parent */
865 bzero(&wa, sizeof wa);
866 for (i = 0; i < ScreenCount(display); i++)
867 TAILQ_FOREACH(r, &screens[i].rl, entry) {
869 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
870 wa.background_pixel =
871 screens[i].c[SWM_S_COLOR_BAR].color;
872 XChangeWindowAttributes(display, r->bar_window,
873 CWBackPixel | CWBorderPixel, &wa);
879 bar_setup(struct swm_region *r)
883 for (i = 0; bar_fonts[i] != NULL; i++) {
884 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
890 if (bar_fonts[i] == NULL)
891 errx(1, "couldn't load font");
892 bar_height = bar_fs->ascent + bar_fs->descent + 3;
894 r->bar_window = XCreateSimpleWindow(display,
895 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
896 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
897 r->s->c[SWM_S_COLOR_BAR].color);
898 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
899 XSetFont(display, bar_gc, bar_fs->fid);
900 XSelectInput(display, r->bar_window, VisibilityChangeMask);
902 XMapRaised(display, r->bar_window);
903 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
905 if (signal(SIGALRM, bar_signal) == SIG_ERR)
906 err(1, "could not install bar_signal");
911 version(struct swm_region *r, union arg *args)
913 bar_version = !bar_version;
915 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
916 SWM_VERSION, cvstag);
918 strlcpy(bar_vertext, "", sizeof bar_vertext);
923 client_msg(struct ws_win *win, Atom a)
925 XClientMessageEvent cm;
927 bzero(&cm, sizeof cm);
928 cm.type = ClientMessage;
930 cm.message_type = aprot;
933 cm.data.l[1] = CurrentTime;
934 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
938 config_win(struct ws_win *win)
942 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
943 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
944 ce.type = ConfigureNotify;
945 ce.display = display;
951 ce.height = win->g.h;
952 ce.border_width = 1; /* XXX store this! */
954 ce.override_redirect = False;
955 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
959 count_win(struct workspace *ws, int count_transient)
964 TAILQ_FOREACH(win, &ws->winlist, entry) {
965 if (count_transient == 0 && win->floating)
967 if (count_transient == 0 && win->transient)
971 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
977 quit(struct swm_region *r, union arg *args)
979 DNPRINTF(SWM_D_MISC, "quit\n");
989 for (i = 0; i < ScreenCount(display); i++)
990 for (j = 0; j < SWM_WS_MAX; j++)
991 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
992 XUnmapWindow(display, win->id);
996 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1000 event.display = display; /* Ignored, but what the hell */
1001 event.window = win->id;
1002 event.root = win->s->root;
1003 event.subwindow = None;
1004 event.time = CurrentTime;
1009 event.same_screen = True;
1010 event.keycode = XKeysymToKeycode(display, keysym);
1011 event.state = modifiers;
1013 event.type = KeyPress;
1014 XSendEvent(event.display, event.window, True,
1015 KeyPressMask, (XEvent *)&event);
1017 event.type = KeyRelease;
1018 XSendEvent(event.display, event.window, True,
1019 KeyPressMask, (XEvent *)&event);
1024 restart(struct swm_region *r, union arg *args)
1026 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1028 /* disable alarm because the following code may not be interrupted */
1030 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1031 errx(1, "can't disable alarm");
1036 XCloseDisplay(display);
1037 execvp(start_argv[0], start_argv);
1038 fprintf(stderr, "execvp failed\n");
1044 root_to_region(Window root)
1046 struct swm_region *r = NULL;
1048 int i, x, y, wx, wy;
1051 for (i = 0; i < ScreenCount(display); i++)
1052 if (screens[i].root == root)
1055 if (XQueryPointer(display, screens[i].root,
1056 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1057 /* choose a region based on pointer location */
1058 TAILQ_FOREACH(r, &screens[i].rl, entry)
1059 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1060 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1065 r = TAILQ_FIRST(&screens[i].rl);
1071 find_window(Window id)
1076 for (i = 0; i < ScreenCount(display); i++)
1077 for (j = 0; j < SWM_WS_MAX; j++)
1078 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1085 spawn(struct swm_region *r, union arg *args)
1090 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1092 * The double-fork construct avoids zombie processes and keeps the code
1093 * clean from stupid signal handlers.
1098 close(ConnectionNumber(display));
1099 setenv("LD_PRELOAD", SWM_LIB, 1);
1100 if (asprintf(&ret, "%d", r->ws->idx)) {
1101 setenv("_SWM_WS", ret, 1);
1104 if (asprintf(&ret, "%d", getpid())) {
1105 setenv("_SWM_PID", ret, 1);
1109 /* kill stdin, mplayer, ssh-add etc. need that */
1110 si = open("/dev/null", O_RDONLY, 0);
1112 err(1, "open /dev/null");
1113 if (dup2(si, 0) == -1)
1114 err(1, "dup2 /dev/null");
1115 execvp(args->argv[0], args->argv);
1116 fprintf(stderr, "execvp failed\n");
1125 spawnterm(struct swm_region *r, union arg *args)
1127 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1130 setenv("_SWM_XTERM_FONTADJ", "", 1);
1135 spawnmenu(struct swm_region *r, union arg *args)
1137 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
1139 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
1140 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
1141 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
1142 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
1143 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
1149 unfocus_win(struct ws_win *win)
1154 if (win->ws->focus != win && win->ws->focus != NULL)
1155 win->ws->focus_prev = win->ws->focus;
1157 if (win->ws->r == NULL)
1160 grabbuttons(win, 0);
1161 XSetWindowBorder(display, win->id,
1162 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1164 if (win->ws->focus == win)
1165 win->ws->focus = NULL;
1166 if (cur_focus == win)
1176 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
1178 for (i = 0; i < ScreenCount(display); i++)
1179 for (j = 0; j < SWM_WS_MAX; j++)
1180 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1185 focus_win(struct ws_win *win)
1187 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1193 unfocus_win(cur_focus);
1194 if (win->ws->focus) {
1195 /* probably shouldn't happen due to the previous unfocus_win */
1196 DNPRINTF(SWM_D_FOCUS, "unfocusing win->ws->focus: %lu\n",
1197 win->ws->focus->id);
1198 unfocus_win(win->ws->focus);
1200 win->ws->focus = win;
1201 if (win->ws->r != NULL) {
1203 if (!win->got_focus) {
1204 XSetWindowBorder(display, win->id,
1205 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1206 grabbuttons(win, 1);
1209 XSetInputFocus(display, win->id,
1210 RevertToPointerRoot, CurrentTime);
1215 switchws(struct swm_region *r, union arg *args)
1217 int wsid = args->id;
1218 struct swm_region *this_r, *other_r;
1220 struct workspace *new_ws, *old_ws;
1223 old_ws = this_r->ws;
1224 new_ws = &this_r->s->ws[wsid];
1226 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1227 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1230 if (new_ws == old_ws)
1233 other_r = new_ws->r;
1235 /* if the other workspace is hidden, switch windows */
1236 /* map new window first to prevent ugly blinking */
1238 old_ws->restack = 1;
1240 TAILQ_FOREACH(win, &new_ws->winlist, entry)
1241 XMapRaised(display, win->id);
1243 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1244 XUnmapWindow(display, win->id);
1246 other_r->ws = old_ws;
1247 old_ws->r = other_r;
1249 this_r->ws = new_ws;
1254 if (new_ws->focus == NULL)
1255 new_ws->focus = TAILQ_FIRST(&new_ws->winlist);
1257 focus_win(new_ws->focus);
1263 cyclews(struct swm_region *r, union arg *args)
1266 struct swm_screen *s = r->s;
1268 DNPRINTF(SWM_D_WS, "cyclews id %d "
1269 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1270 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1275 case SWM_ARG_ID_CYCLEWS_UP:
1276 if (a.id < SWM_WS_MAX - 1)
1281 case SWM_ARG_ID_CYCLEWS_DOWN:
1285 a.id = SWM_WS_MAX - 1;
1291 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1293 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1297 } while (a.id != r->ws->idx);
1301 cyclescr(struct swm_region *r, union arg *args)
1303 struct swm_region *rr;
1308 case SWM_ARG_ID_CYCLESC_UP:
1309 rr = TAILQ_NEXT(r, entry);
1311 rr = TAILQ_FIRST(&screens[i].rl);
1313 case SWM_ARG_ID_CYCLESC_DOWN:
1314 rr = TAILQ_PREV(r, swm_region_list, entry);
1316 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1322 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1323 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, rr->g.x,
1324 rr->g.y + bar_enabled ? bar_height : 0);
1328 swapwin(struct swm_region *r, union arg *args)
1330 struct ws_win *target, *source;
1331 struct ws_win_list *wl;
1334 DNPRINTF(SWM_D_WS, "swapwin id %d "
1335 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1336 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1337 if (cur_focus == NULL)
1341 wl = &source->ws->winlist;
1344 case SWM_ARG_ID_SWAPPREV:
1345 target = TAILQ_PREV(source, ws_win_list, entry);
1346 TAILQ_REMOVE(wl, cur_focus, entry);
1348 TAILQ_INSERT_TAIL(wl, source, entry);
1350 TAILQ_INSERT_BEFORE(target, source, entry);
1352 case SWM_ARG_ID_SWAPNEXT:
1353 target = TAILQ_NEXT(source, entry);
1354 TAILQ_REMOVE(wl, source, entry);
1356 TAILQ_INSERT_HEAD(wl, source, entry);
1358 TAILQ_INSERT_AFTER(wl, target, source, entry);
1360 case SWM_ARG_ID_SWAPMAIN:
1361 target = TAILQ_FIRST(wl);
1362 if (target == source) {
1363 if (source->ws->focus_prev != NULL &&
1364 source->ws->focus_prev != target)
1366 source = source->ws->focus_prev;
1370 source->ws->focus_prev = target;
1371 TAILQ_REMOVE(wl, target, entry);
1372 TAILQ_INSERT_BEFORE(source, target, entry);
1373 TAILQ_REMOVE(wl, source, entry);
1374 TAILQ_INSERT_HEAD(wl, source, entry);
1377 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1386 focus(struct swm_region *r, union arg *args)
1388 struct ws_win *winfocus, *winlostfocus;
1389 struct ws_win_list *wl;
1391 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1392 if (cur_focus == NULL)
1395 wl = &cur_focus->ws->winlist;
1397 winlostfocus = cur_focus;
1400 case SWM_ARG_ID_FOCUSPREV:
1401 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1402 if (winfocus == NULL)
1403 winfocus = TAILQ_LAST(wl, ws_win_list);
1406 case SWM_ARG_ID_FOCUSNEXT:
1407 winfocus = TAILQ_NEXT(cur_focus, entry);
1408 if (winfocus == NULL)
1409 winfocus = TAILQ_FIRST(wl);
1412 case SWM_ARG_ID_FOCUSMAIN:
1413 winfocus = TAILQ_FIRST(wl);
1414 if (winfocus == cur_focus)
1415 winfocus = cur_focus->ws->focus_prev;
1416 if (winfocus == NULL)
1424 if (winfocus == winlostfocus || winfocus == NULL)
1427 XMapRaised(display, winfocus->id);
1428 focus_win(winfocus);
1429 XSync(display, False);
1433 cycle_layout(struct swm_region *r, union arg *args)
1435 struct workspace *ws = r->ws;
1437 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1440 if (ws->cur_layout->l_stack == NULL)
1441 ws->cur_layout = &layouts[0];
1447 stack_config(struct swm_region *r, union arg *args)
1449 struct workspace *ws = r->ws;
1451 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1454 if (ws->cur_layout->l_config != NULL)
1455 ws->cur_layout->l_config(ws, args->id);
1457 if (args->id != SWM_ARG_ID_STACKINIT);
1463 struct swm_geometry g;
1464 struct swm_region *r;
1467 DNPRINTF(SWM_D_STACK, "stack\n");
1469 for (i = 0; i < ScreenCount(display); i++) {
1471 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1472 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1473 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1475 /* start with screen geometry, adjust for bar */
1485 r->ws->cur_layout->l_stack(r->ws, &g);
1490 XSync(display, False);
1494 stack_floater(struct ws_win *win, struct swm_region *r)
1499 bzero(&wc, sizeof wc);
1500 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1501 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w == WIDTH(r)) &&
1502 (win->g.h == HEIGHT(r)))
1503 wc.border_width = 0;
1505 wc.border_width = 1;
1506 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1507 win->g.w = (double)WIDTH(r) * dialog_ratio;
1508 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1510 wc.width = win->g.w;
1511 wc.height = win->g.h;
1516 wc.x = (WIDTH(r) - win->g.w) / 2;
1517 wc.y = (HEIGHT(r) - win->g.h) / 2;
1520 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1521 win->id, wc.x, wc.y, wc.width, wc.height);
1523 XConfigureWindow(display, win->id, mask, &wc);
1527 * Send keystrokes to terminal to decrease/increase the font size as the
1528 * window size changes.
1531 adjust_font(struct ws_win *win)
1533 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1534 win->floating || win->transient)
1537 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1538 win->g.w / win->sh.width_inc < term_width &&
1539 win->font_steps < SWM_MAX_FONT_STEPS) {
1540 win->font_size_boundary[win->font_steps] =
1541 (win->sh.width_inc * term_width) + win->sh.base_width;
1544 win->last_inc = win->sh.width_inc;
1545 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1546 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1547 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1550 win->last_inc = win->sh.width_inc;
1551 fake_keypress(win, XK_KP_Add, ShiftMask);
1555 #define SWAPXY(g) do { \
1557 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1558 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1561 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1564 struct swm_geometry win_g, r_g = *g;
1565 struct ws_win *win, *winfocus;
1566 int i, j, s, stacks;
1567 int w_inc = 1, h_inc, w_base = 1, h_base;
1568 int hrh, extra = 0, h_slice, last_h = 0;
1569 int split, colno, winno, mwin, msize, mscale;
1570 int remain, missing, v_slice;
1573 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1574 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1576 if ((winno = count_win(ws, 0)) == 0)
1579 if (ws->focus == NULL)
1580 ws->focus = TAILQ_FIRST(&ws->winlist);
1581 winfocus = cur_focus ? cur_focus : ws->focus;
1583 TAILQ_FOREACH(win, &ws->winlist, entry)
1584 if (win->transient == 0 && win->floating == 0)
1591 w_inc = win->sh.width_inc;
1592 w_base = win->sh.base_width;
1593 mwin = ws->l_state.horizontal_mwin;
1594 mscale = ws->l_state.horizontal_msize;
1595 stacks = ws->l_state.horizontal_stacks;
1598 w_inc = win->sh.height_inc;
1599 w_base = win->sh.base_height;
1600 mwin = ws->l_state.vertical_mwin;
1601 mscale = ws->l_state.vertical_msize;
1602 stacks = ws->l_state.vertical_stacks;
1606 if (stacks > winno - mwin)
1607 stacks = winno - mwin;
1611 h_slice = r_g.h / SWM_H_SLICE;
1612 if (mwin && winno > mwin) {
1613 v_slice = r_g.w / SWM_V_SLICE;
1617 win_g.w = v_slice * mscale;
1619 if (w_inc > 1 && w_inc < v_slice) {
1620 /* adjust for window's requested size increment */
1621 remain = (win_g.w - w_base) % w_inc;
1622 missing = w_inc - remain;
1629 win_g.x += r_g.w - msize;
1632 colno = split = winno / stacks;
1633 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
1635 hrh = r_g.h / colno;
1636 extra = r_g.h - (colno * hrh);
1639 /* stack all the tiled windows */
1640 i = j = 0, s = stacks;
1641 TAILQ_FOREACH(win, &ws->winlist, entry) {
1642 if (win->transient != 0 || win->floating != 0)
1645 if (split && i == split) {
1646 colno = (winno - mwin) / stacks;
1647 if (s <= (winno - mwin) % stacks)
1649 split = split + colno;
1650 hrh = (r_g.h / colno);
1651 extra = r_g.h - (colno * hrh);
1655 win_g.x += win_g.w + 2;
1656 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
1658 win_g.w += (r_g.w - msize - (stacks * 2)) %
1665 h_inc = win->sh.width_inc;
1666 h_base = win->sh.base_width;
1668 h_inc = win->sh.height_inc;
1669 h_base = win->sh.base_height;
1671 if (j == colno - 1) {
1672 win_g.h = hrh + extra;
1673 } else if (h_inc > 1 && h_inc < h_slice) {
1674 /* adjust for window's requested size increment */
1675 remain = (win_g.h - h_base) % h_inc;
1676 missing = h_inc - remain;
1678 if (missing <= extra || j == 0) {
1690 win_g.y += last_h + 2;
1692 bzero(&wc, sizeof wc);
1693 wc.border_width = 1;
1695 win->g.x = wc.x = win_g.y;
1696 win->g.y = wc.y = win_g.x;
1697 win->g.w = wc.width = win_g.h;
1698 win->g.h = wc.height = win_g.w;
1700 win->g.x = wc.x = win_g.x;
1701 win->g.y = wc.y = win_g.y;
1702 win->g.w = wc.width = win_g.w;
1703 win->g.h = wc.height = win_g.h;
1706 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1707 XConfigureWindow(display, win->id, mask, &wc);
1708 XMapRaised(display, win->id);
1716 /* now, stack all the floaters and transients */
1717 TAILQ_FOREACH(win, &ws->winlist, entry) {
1718 if (win->transient == 0 && win->floating == 0)
1721 stack_floater(win, ws->r);
1722 XMapRaised(display, win->id);
1726 focus_win(winfocus); /* has to be done outside of the loop */
1730 vertical_config(struct workspace *ws, int id)
1732 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1735 case SWM_ARG_ID_STACKRESET:
1736 case SWM_ARG_ID_STACKINIT:
1737 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1738 ws->l_state.vertical_mwin = 1;
1739 ws->l_state.vertical_stacks = 1;
1741 case SWM_ARG_ID_MASTERSHRINK:
1742 if (ws->l_state.vertical_msize > 1)
1743 ws->l_state.vertical_msize--;
1745 case SWM_ARG_ID_MASTERGROW:
1746 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1747 ws->l_state.vertical_msize++;
1749 case SWM_ARG_ID_MASTERADD:
1750 ws->l_state.vertical_mwin++;
1752 case SWM_ARG_ID_MASTERDEL:
1753 if (ws->l_state.vertical_mwin > 0)
1754 ws->l_state.vertical_mwin--;
1756 case SWM_ARG_ID_STACKINC:
1757 ws->l_state.vertical_stacks++;
1759 case SWM_ARG_ID_STACKDEC:
1760 if (ws->l_state.vertical_stacks > 1)
1761 ws->l_state.vertical_stacks--;
1769 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1771 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1773 stack_master(ws, g, 0, 0);
1777 horizontal_config(struct workspace *ws, int id)
1779 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1782 case SWM_ARG_ID_STACKRESET:
1783 case SWM_ARG_ID_STACKINIT:
1784 ws->l_state.horizontal_mwin = 1;
1785 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1786 ws->l_state.horizontal_stacks = 1;
1788 case SWM_ARG_ID_MASTERSHRINK:
1789 if (ws->l_state.horizontal_msize > 1)
1790 ws->l_state.horizontal_msize--;
1792 case SWM_ARG_ID_MASTERGROW:
1793 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1794 ws->l_state.horizontal_msize++;
1796 case SWM_ARG_ID_MASTERADD:
1797 ws->l_state.horizontal_mwin++;
1799 case SWM_ARG_ID_MASTERDEL:
1800 if (ws->l_state.horizontal_mwin > 0)
1801 ws->l_state.horizontal_mwin--;
1803 case SWM_ARG_ID_STACKINC:
1804 ws->l_state.horizontal_stacks++;
1806 case SWM_ARG_ID_STACKDEC:
1807 if (ws->l_state.horizontal_stacks > 1)
1808 ws->l_state.horizontal_stacks--;
1816 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1818 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1820 stack_master(ws, g, 1, 0);
1823 /* fullscreen view */
1825 max_stack(struct workspace *ws, struct swm_geometry *g) {
1827 struct swm_geometry gg = *g;
1828 struct ws_win *win, *winfocus;
1831 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1833 if (count_win(ws, 0) == 0)
1836 if (ws->focus == NULL)
1837 ws->focus = TAILQ_FIRST(&ws->winlist);
1838 winfocus = cur_focus ? cur_focus : ws->focus;
1840 TAILQ_FOREACH(win, &ws->winlist, entry) {
1841 if (win->transient != 0 || win->floating != 0) {
1842 if (win == ws->focus) {
1844 stack_floater(win, ws->r);
1845 XMapRaised(display, win->id);
1847 XUnmapWindow(display, win->id);
1849 bzero(&wc, sizeof wc);
1850 wc.border_width = 1;
1851 win->g.x = wc.x = gg.x;
1852 win->g.y = wc.y = gg.y;
1853 win->g.w = wc.width = gg.w;
1854 win->g.h = wc.height = gg.h;
1855 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1856 XConfigureWindow(display, win->id, mask, &wc);
1858 if (win == ws->focus) {
1859 XMapRaised(display, win->id);
1861 XUnmapWindow(display, win->id);
1866 focus_win(winfocus); /* has to be done outside of the loop */
1870 send_to_ws(struct swm_region *r, union arg *args)
1872 int wsid = args->id;
1873 struct ws_win *win = cur_focus;
1874 struct workspace *ws, *nws;
1875 Atom ws_idx_atom = 0;
1876 unsigned char ws_idx_str[SWM_PROPLEN];
1881 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1884 nws = &win->s->ws[wsid];
1886 XUnmapWindow(display, win->id);
1888 /* find a window to focus */
1889 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1890 if (ws->focus == NULL)
1891 ws->focus = TAILQ_FIRST(&ws->winlist);
1892 if (ws->focus == win)
1895 TAILQ_REMOVE(&ws->winlist, win, entry);
1897 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1900 /* Try to update the window's workspace property */
1901 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1903 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1904 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1906 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1907 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1910 if (count_win(nws, 1) == 1)
1919 wkill(struct swm_region *r, union arg *args)
1921 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
1923 if(r->ws->focus == NULL)
1926 if (args->id == SWM_ARG_ID_KILLWINDOW)
1927 XKillClient(display, r->ws->focus->id);
1929 if (r->ws->focus->can_delete)
1930 client_msg(r->ws->focus, adelete);
1934 screenshot(struct swm_region *r, union arg *args)
1938 DNPRINTF(SWM_D_MISC, "screenshot\n");
1940 if (ss_enabled == 0)
1944 case SWM_ARG_ID_SS_ALL:
1945 spawn_screenshot[1] = "full";
1947 case SWM_ARG_ID_SS_WINDOW:
1948 spawn_screenshot[1] = "window";
1953 a.argv = spawn_screenshot;
1958 floating_toggle(struct swm_region *r, union arg *args)
1960 struct ws_win *win = cur_focus;
1965 win->floating = !win->floating;
1972 resize_window(struct ws_win *win, int center)
1976 struct swm_region *r;
1978 r = root_to_region(win->wa.root);
1979 bzero(&wc, sizeof wc);
1980 mask = CWBorderWidth | CWWidth | CWHeight;
1981 wc.border_width = 1;
1982 wc.width = win->g.w;
1983 wc.height = win->g.h;
1984 if (center == SWM_ARG_ID_CENTER) {
1985 wc.x = (WIDTH(r) - win->g.w) / 2;
1986 wc.y = (HEIGHT(r) - win->g.h) / 2;
1990 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
1991 win->id, wc.x, wc.y, wc.width, wc.height);
1993 XConfigureWindow(display, win->id, mask, &wc);
1998 resize(struct ws_win *win, union arg *args)
2003 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %d\n",
2004 win->id, win->floating, win->transient);
2006 if (!(win->transient != 0 || win->floating != 0))
2009 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2010 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2012 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
2014 XMaskEvent(display, MOUSEMASK | ExposureMask |
2015 SubstructureRedirectMask, &ev);
2017 case ConfigureRequest:
2020 handler[ev.type](&ev);
2023 if (ev.xmotion.x <= 1)
2025 if (ev.xmotion.y <= 1)
2027 win->g.w = ev.xmotion.x;
2028 win->g.h = ev.xmotion.y;
2030 /* not free, don't sync more than 60 times / second */
2031 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2032 time = ev.xmotion.time;
2033 XSync(display, False);
2034 resize_window(win, args->id);
2038 } while (ev.type != ButtonRelease);
2040 XSync(display, False);
2041 resize_window(win, args->id);
2043 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2045 XUngrabPointer(display, CurrentTime);
2048 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2052 move_window(struct ws_win *win)
2056 struct swm_region *r;
2058 r = root_to_region(win->wa.root);
2059 bzero(&wc, sizeof wc);
2064 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2065 win->id, wc.x, wc.y, wc.width, wc.height);
2067 XConfigureWindow(display, win->id, mask, &wc);
2072 move(struct ws_win *win, union arg *args)
2078 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %d\n",
2079 win->id, win->floating, win->transient);
2081 if (win->floating == 0) {
2087 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2088 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2090 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2092 XMaskEvent(display, MOUSEMASK | ExposureMask |
2093 SubstructureRedirectMask, &ev);
2095 case ConfigureRequest:
2098 handler[ev.type](&ev);
2101 win->g.x = ev.xmotion.x_root;
2102 win->g.y = ev.xmotion.y_root;
2104 /* not free, don't sync more than 60 times / second */
2105 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2106 time = ev.xmotion.time;
2107 XSync(display, False);
2112 } while (ev.type != ButtonRelease);
2114 XSync(display, False);
2117 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2118 XUngrabPointer(display, CurrentTime);
2123 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2126 /* key definitions */
2128 char name[SWM_FUNCNAME_LEN];
2129 void (*func)(struct swm_region *r, union arg *);
2131 } keyfuncs[kf_invalid] = {
2132 /* name function argument */
2133 { "cycle_layout", cycle_layout, {0} },
2134 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
2135 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
2136 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
2137 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
2138 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
2139 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
2140 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
2141 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
2142 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
2143 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
2144 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
2145 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
2146 { "spawn_term", spawnterm, {.argv = spawn_term} },
2147 { "spawn_menu", spawnmenu, {.argv = spawn_menu} },
2148 { "quit", quit, {0} },
2149 { "restart", restart, {0} },
2150 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
2151 { "ws_1", switchws, {.id = 0} },
2152 { "ws_2", switchws, {.id = 1} },
2153 { "ws_3", switchws, {.id = 2} },
2154 { "ws_4", switchws, {.id = 3} },
2155 { "ws_5", switchws, {.id = 4} },
2156 { "ws_6", switchws, {.id = 5} },
2157 { "ws_7", switchws, {.id = 6} },
2158 { "ws_8", switchws, {.id = 7} },
2159 { "ws_9", switchws, {.id = 8} },
2160 { "ws_10", switchws, {.id = 9} },
2161 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
2162 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
2163 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
2164 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
2165 { "mvws_1", send_to_ws, {.id = 0} },
2166 { "mvws_2", send_to_ws, {.id = 1} },
2167 { "mvws_3", send_to_ws, {.id = 2} },
2168 { "mvws_4", send_to_ws, {.id = 3} },
2169 { "mvws_5", send_to_ws, {.id = 4} },
2170 { "mvws_6", send_to_ws, {.id = 5} },
2171 { "mvws_7", send_to_ws, {.id = 6} },
2172 { "mvws_8", send_to_ws, {.id = 7} },
2173 { "mvws_9", send_to_ws, {.id = 8} },
2174 { "mvws_10", send_to_ws, {.id = 9} },
2175 { "bar_toggle", bar_toggle, {0} },
2176 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
2177 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
2178 { "screenshot_all", screenshot, {.id = SWM_ARG_ID_SS_ALL} },
2179 { "screenshot_wind", screenshot, {.id = SWM_ARG_ID_SS_WINDOW} },
2180 { "float_toggle", floating_toggle,{0} },
2181 { "version", version, {0} },
2182 { "spawn_lock", spawn, {.argv = spawn_lock} },
2183 { "spawn_initscr", spawn, {.argv = spawn_initscr} },
2188 enum keyfuncid funcid;
2190 int keys_size = 0, keys_length = 0;
2191 struct key *keys = NULL;
2194 enum { client_click, root_click };
2196 unsigned int action;
2198 unsigned int button;
2199 void (*func)(struct ws_win *, union arg *);
2202 /* action key mouse button func args */
2203 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2204 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2205 { client_click, MODKEY, Button1, move, {0} },
2209 update_modkey(unsigned int mod)
2213 for (i = 0; i < keys_length; i++)
2214 if (keys[i].mod & ShiftMask)
2215 keys[i].mod = mod | ShiftMask;
2219 for (i = 0; i < LENGTH(buttons); i++)
2220 if (buttons[i].mask & ShiftMask)
2221 buttons[i].mask = mod | ShiftMask;
2223 buttons[i].mask = mod;
2226 #define SWM_MODNAME_SIZE 32
2227 #define SWM_KEY_WS "\n+ \t"
2229 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
2233 if (mod == NULL || ks == NULL)
2237 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
2239 cp += (long)strspn(cp, SWM_KEY_WS);
2240 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
2242 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
2244 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
2246 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
2248 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
2250 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
2252 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
2253 *mod |= ControlMask;
2255 *ks = XStringToKeysym(name);
2256 XConvertCase(*ks, ks, &uks);
2257 if (ks == NoSymbol) {
2259 "parsekeys: invalid key %s\n",
2268 bindmatch(const char *var, const char *name, unsigned int currmod, char *keystr,
2269 enum keyfuncid *kfid, unsigned int *mod, KeySym *ks)
2273 char funcname[SWM_FUNCNAME_LEN];
2274 i = strncmp(var, name, 255);
2275 if (kfid == NULL || mod == NULL || ks == NULL)
2280 bzero(funcname, LENGTH(funcname));
2283 p = (char *)var + strlen(name);
2287 while (isgraph(*p) && *p != ']' && i < LENGTH(funcname))
2288 funcname[i++] = *p++;
2289 if (i >= LENGTH(funcname) || *p != ']')
2292 return (!parsekeys(keystr, currmod, mod, ks));
2293 for (*kfid = 0; *kfid < kf_invalid; (*kfid)++) {
2294 if (strncasecmp(funcname, keyfuncs[*kfid].name,
2295 SWM_FUNCNAME_LEN) == 0) {
2296 return (!parsekeys(keystr, currmod, mod, ks));
2303 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid)
2307 for (i = 0; i < keys_length; i++) {
2308 if (keys[i].mod == mod && keys[i].keysym == ks) {
2309 if (kfid == kf_invalid) {
2312 "setkeybinding: delete #%d %s\n",
2313 i, keyfuncs[keys[i].funcid].name);
2314 j = keys_length - 1;
2320 /* found: replace */
2322 "setkeybinding: replace #%d %s\n",
2323 i, keyfuncs[keys[i].funcid].name);
2325 keys[i].keysym = ks;
2326 keys[i].funcid = kfid;
2331 if (kfid == kf_invalid) {
2333 "error: setkeybinding: cannot find mod/key combination");
2336 /* not found: add */
2337 if (keys_size == 0 || keys == NULL) {
2339 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
2340 keys = malloc((size_t)keys_size * sizeof(struct key));
2342 fprintf(stderr, "malloc failed\n");
2346 } else if (keys_length == keys_size) {
2348 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
2349 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
2351 fprintf(stderr, "realloc failed\n");
2356 if (keys_length < keys_size) {
2357 DNPRINTF(SWM_D_KEY, "setkeybinding: add %d\n", keys_length);
2360 keys[j].keysym = ks;
2361 keys[j].funcid = kfid;
2363 fprintf(stderr, "keys array problem?\n");
2365 fprintf(stderr, "keys array problem\n");
2373 setkeybinding(MODKEY, XK_space, kf_cycle_layout);
2374 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset);
2375 setkeybinding(MODKEY, XK_h, kf_master_shrink);
2376 setkeybinding(MODKEY, XK_l, kf_master_grow);
2377 setkeybinding(MODKEY, XK_comma, kf_master_add);
2378 setkeybinding(MODKEY, XK_period, kf_master_del);
2379 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc);
2380 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec);
2381 setkeybinding(MODKEY, XK_Return, kf_swap_main);
2382 setkeybinding(MODKEY, XK_j, kf_focus_next);
2383 setkeybinding(MODKEY, XK_k, kf_focus_prev);
2384 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next);
2385 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev);
2386 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term);
2387 setkeybinding(MODKEY, XK_p, kf_spawn_menu);
2388 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit);
2389 setkeybinding(MODKEY, XK_q, kf_restart);
2390 setkeybinding(MODKEY, XK_m, kf_focus_main);
2391 setkeybinding(MODKEY, XK_1, kf_ws_1);
2392 setkeybinding(MODKEY, XK_2, kf_ws_2);
2393 setkeybinding(MODKEY, XK_3, kf_ws_3);
2394 setkeybinding(MODKEY, XK_4, kf_ws_4);
2395 setkeybinding(MODKEY, XK_5, kf_ws_5);
2396 setkeybinding(MODKEY, XK_6, kf_ws_6);
2397 setkeybinding(MODKEY, XK_7, kf_ws_7);
2398 setkeybinding(MODKEY, XK_8, kf_ws_8);
2399 setkeybinding(MODKEY, XK_9, kf_ws_9);
2400 setkeybinding(MODKEY, XK_0, kf_ws_10);
2401 setkeybinding(MODKEY, XK_Right, kf_ws_next);
2402 setkeybinding(MODKEY, XK_Left, kf_ws_prev);
2403 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next);
2404 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev);
2405 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1);
2406 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2);
2407 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3);
2408 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4);
2409 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5);
2410 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6);
2411 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7);
2412 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8);
2413 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9);
2414 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10);
2415 setkeybinding(MODKEY, XK_b, kf_bar_toggle);
2416 setkeybinding(MODKEY, XK_Tab, kf_focus_next);
2417 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev);
2418 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill);
2419 setkeybinding(MODKEY, XK_x, kf_wind_del);
2420 setkeybinding(MODKEY, XK_s, kf_screenshot_all);
2421 setkeybinding(MODKEY|ShiftMask, XK_s, kf_screenshot_wind);
2422 setkeybinding(MODKEY, XK_t, kf_float_toggle);
2423 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version);
2424 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_lock);
2425 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_initscr);
2428 updatenumlockmask(void)
2431 XModifierKeymap *modmap;
2433 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
2435 modmap = XGetModifierMapping(display);
2436 for (i = 0; i < 8; i++)
2437 for (j = 0; j < modmap->max_keypermod; j++)
2438 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2439 == XKeysymToKeycode(display, XK_Num_Lock))
2440 numlockmask = (1 << i);
2442 XFreeModifiermap(modmap);
2448 unsigned int i, j, k;
2450 unsigned int modifiers[] =
2451 { 0, LockMask, numlockmask, numlockmask | LockMask };
2453 DNPRINTF(SWM_D_MISC, "grabkeys\n");
2454 updatenumlockmask();
2456 for (k = 0; k < ScreenCount(display); k++) {
2457 if (TAILQ_EMPTY(&screens[k].rl))
2459 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
2460 for (i = 0; i < keys_length; i++) {
2461 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
2462 for (j = 0; j < LENGTH(modifiers); j++)
2463 XGrabKey(display, code,
2464 keys[i].mod | modifiers[j],
2465 screens[k].root, True,
2466 GrabModeAsync, GrabModeAsync);
2472 grabbuttons(struct ws_win *win, int focused)
2475 unsigned int modifiers[] =
2476 { 0, LockMask, numlockmask, numlockmask|LockMask };
2478 updatenumlockmask();
2479 XUngrabButton(display, AnyButton, AnyModifier, win->id);
2481 for (i = 0; i < LENGTH(buttons); i++)
2482 if (buttons[i].action == client_click)
2483 for (j = 0; j < LENGTH(modifiers); j++)
2484 XGrabButton(display, buttons[i].button,
2485 buttons[i].mask | modifiers[j],
2486 win->id, False, BUTTONMASK,
2487 GrabModeAsync, GrabModeSync, None,
2490 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
2491 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
2497 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
2505 XKeyEvent *ev = &e->xkey;
2507 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
2509 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
2510 for (i = 0; i < keys_length; i++)
2511 if (keysym == keys[i].keysym
2512 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
2513 && keyfuncs[keys[i].funcid].func)
2514 keyfuncs[keys[i].funcid].func(
2515 root_to_region(ev->root),
2516 &(keyfuncs[keys[i].funcid].args)
2521 buttonpress(XEvent *e)
2523 XButtonPressedEvent *ev = &e->xbutton;
2528 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
2530 action = root_click;
2531 if ((win = find_window(ev->window)) == NULL)
2535 action = client_click;
2538 for (i = 0; i < LENGTH(buttons); i++)
2539 if (action == buttons[i].action && buttons[i].func &&
2540 buttons[i].button == ev->button &&
2541 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
2542 buttons[i].func(win, &buttons[i].args);
2546 set_win_state(struct ws_win *win, long state)
2548 long data[] = {state, None};
2550 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
2552 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
2553 (unsigned char *)data, 2);
2556 const char *quirkname[] = {
2557 "NONE", /* config string for "no value" */
2565 #define SWM_Q_WS "\n| \t"
2567 parsequirks(char *qstr, unsigned long *quirk)
2575 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
2577 cp += (long)strspn(cp, SWM_Q_WS);
2578 for (i = 0; i < LENGTH(quirkname); i++) {
2579 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
2580 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
2585 *quirk |= 1 << (i-1);
2589 if (i >= LENGTH(quirkname)) {
2590 DNPRINTF(SWM_D_QUIRK,
2591 "parsequirks: invalid quirk [%s]\n", name);
2598 quirkmatch(const char *var, const char *name, char *qstr, char *qclass,
2599 char *qname, unsigned long *qquirk)
2603 char classname[SWM_QUIRK_LEN*2+1];
2604 DNPRINTF(SWM_D_QUIRK, "quirkmatch: in [%s]\n", var);
2605 i = strncmp(var, name, 255);
2606 if (qclass == NULL || qname == NULL || qquirk == NULL)
2611 bzero(classname, LENGTH(classname));
2614 p = (char *)var + strlen(name);
2618 while (isgraph(*p) && *p != ']' && i < LENGTH(classname))
2619 classname[i++] = *p++;
2620 if (i >= LENGTH(classname) || *p != ']')
2622 if ((p = strchr(classname, ':')) == NULL || p-classname >= SWM_QUIRK_LEN)
2624 strlcpy(qclass, classname, p-classname+1);
2625 strlcpy(qname, ++p, SWM_QUIRK_LEN);
2626 for (p = qclass; *p && p-qclass < SWM_QUIRK_LEN; p++)
2629 for (p = qname; *p && p-qname < SWM_QUIRK_LEN; p++)
2632 i = (!parsequirks(qstr, qquirk));
2633 DNPRINTF(SWM_D_QUIRK, "quirkmatch: [%s][%s] %d\n", qclass, qname, i);
2637 setquirk(const char *class, const char *name, const int quirk)
2641 for (i = 0; i < quirks_length; i++) {
2642 if (!strcmp(quirks[i].class, class) &&
2643 !strcmp(quirks[i].name, name)) {
2646 DNPRINTF(SWM_D_QUIRK,
2647 "setquirk: delete #%d %s:%s\n",
2648 i, quirks[i].class, quirks[i].name);
2649 j = quirks_length - 1;
2651 quirks[i] = quirks[j];
2655 /* found: replace */
2656 DNPRINTF(SWM_D_QUIRK,
2657 "setquirk: replace #%d %s:%s\n",
2658 i, quirks[i].class, quirks[i].name);
2659 strlcpy(quirks[i].class, class,
2660 sizeof quirks->class);
2661 strlcpy(quirks[i].name, name,
2662 sizeof quirks->name);
2663 quirks[i].quirk = quirk;
2670 "error: setquirk: cannot find class/name combination");
2673 /* not found: add */
2674 if (quirks_size == 0 || quirks == NULL) {
2676 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
2677 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
2679 fprintf(stderr, "setquirk: malloc failed\n");
2683 } else if (quirks_length == quirks_size) {
2685 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
2686 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
2688 fprintf(stderr, "setquirk: realloc failed\n");
2693 if (quirks_length < quirks_size) {
2694 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
2695 j = quirks_length++;
2696 strlcpy(quirks[j].class, class, sizeof quirks->class);
2697 strlcpy(quirks[j].name, name, sizeof quirks->name);
2698 quirks[j].quirk = quirk;
2700 fprintf(stderr, "quirks array problem?\n");
2702 fprintf(stderr, "quirks array problem!\n");
2711 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
2712 setquirk("OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT);
2713 setquirk("OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT);
2714 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
2715 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
2716 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2717 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
2718 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2719 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2720 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2721 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2722 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
2723 setquirk("pcb", "pcb", SWM_Q_FLOAT);
2727 manage_window(Window id)
2730 struct workspace *ws;
2732 int format, i, ws_idx, n;
2733 unsigned long nitems, bytes;
2734 Atom ws_idx_atom = 0, type;
2735 Atom *prot = NULL, *pp;
2736 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
2737 struct swm_region *r;
2742 if ((win = find_window(id)) != NULL)
2743 return (win); /* already being managed */
2745 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
2746 errx(1, "calloc: failed to allocate memory for new window");
2748 /* Get all the window data in one shot */
2749 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2751 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
2752 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
2753 XGetWindowAttributes(display, id, &win->wa);
2754 XGetTransientForHint(display, id, &trans);
2755 XGetWMNormalHints(display, id, &win->sh, &mask); /* XXX function? */
2757 win->transient = trans;
2758 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
2759 (unsigned)win->id, win->transient);
2761 /* get supported protocols */
2762 if (XGetWMProtocols(display, id, &prot, &n)) {
2763 for (i = 0, pp = prot; i < n; i++, pp++)
2765 win->can_delete = 1;
2771 * Figure out where to put the window. If it was previously assigned to
2772 * a workspace (either by spawn() or manually moving), and isn't
2773 * transient, * put it in the same workspace
2775 r = root_to_region(win->wa.root);
2776 if (prop && win->transient == 0) {
2777 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
2778 ws_idx = strtonum(prop, 0, 9, &errstr);
2780 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
2783 ws = &r->s->ws[ws_idx];
2787 /* set up the window layout */
2790 win->s = r->s; /* this never changes */
2791 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
2793 win->g.w = win->wa.width;
2794 win->g.h = win->wa.height;
2795 win->g.x = win->wa.x;
2796 win->g.y = win->wa.y;
2798 /* Set window properties so we can remember this after reincarnation */
2799 if (ws_idx_atom && prop == NULL &&
2800 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
2801 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2803 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2804 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2808 if (XGetClassHint(display, win->id, &win->ch)) {
2809 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
2810 win->ch.res_class, win->ch.res_name);
2811 for (i = 0; i < quirks_length; i++){
2812 if (!strcmp(win->ch.res_class, quirks[i].class) &&
2813 !strcmp(win->ch.res_name, quirks[i].name)) {
2814 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
2815 win->ch.res_class, win->ch.res_name);
2816 if (quirks[i].quirk & SWM_Q_FLOAT)
2818 win->quirks = quirks[i].quirk;
2823 /* alter window position if quirky */
2824 if (win->quirks & SWM_Q_ANYWHERE) {
2825 win->manual = 1; /* don't center the quirky windows */
2826 bzero(&wc, sizeof wc);
2828 if (win->g.y < bar_height) {
2829 win->g.y = wc.y = bar_height;
2832 if (win->g.w + win->g.x > WIDTH(r)) {
2833 win->g.x = wc.x = WIDTH(win->ws->r) - win->g.w - 2;
2836 wc.border_width = 1;
2837 mask |= CWBorderWidth;
2838 XConfigureWindow(display, win->id, mask, &wc);
2841 /* Reset font sizes (the bruteforce way; no default keybinding). */
2842 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
2843 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
2844 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2845 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
2846 fake_keypress(win, XK_KP_Add, ShiftMask);
2849 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
2850 PropertyChangeMask | StructureNotifyMask);
2852 set_win_state(win, NormalState);
2854 /* floaters need to be mapped if they are in the current workspace */
2855 if (win->floating && (ws->idx == r->ws->idx))
2856 XMapRaised(display, win->id);
2858 /* make new win focused */
2865 unmanage_window(struct ws_win *win)
2867 struct workspace *ws;
2872 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
2874 /* don't unmanage if we are switching workspaces */
2879 /* find a window to focus */
2880 if (ws->focus == win)
2881 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
2882 if (ws->focus == NULL)
2883 ws->focus = TAILQ_FIRST(&ws->winlist);
2884 if (ws->focus == NULL || ws->focus == win) {
2888 focus_win(ws->focus);
2889 if (ws->focus_prev == win)
2890 ws->focus_prev = NULL;
2892 TAILQ_REMOVE(&win->ws->winlist, win, entry);
2893 set_win_state(win, WithdrawnState);
2894 if (win->ch.res_class)
2895 XFree(win->ch.res_class);
2896 if (win->ch.res_name)
2897 XFree(win->ch.res_name);
2902 configurerequest(XEvent *e)
2904 XConfigureRequestEvent *ev = &e->xconfigurerequest;
2909 if ((win = find_window(ev->window)) == NULL)
2913 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
2915 bzero(&wc, sizeof wc);
2918 wc.width = ev->width;
2919 wc.height = ev->height;
2920 wc.border_width = ev->border_width;
2921 wc.sibling = ev->above;
2922 wc.stack_mode = ev->detail;
2923 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
2925 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
2927 if (win->floating) {
2928 if (ev->value_mask & CWX)
2930 if (ev->value_mask & CWY)
2932 if (ev->value_mask & CWWidth)
2933 win->g.w = ev->width;
2934 if (ev->value_mask & CWHeight)
2935 win->g.h = ev->height;
2936 if (win->ws->r != NULL) {
2937 /* this seems to be full screen */
2938 if (win->g.w >= WIDTH(win->ws->r)) {
2940 win->g.w = WIDTH(win->ws->r);
2941 ev->value_mask |= CWX | CWWidth;
2943 if (win->g.h >= HEIGHT(win->ws->r)) {
2946 win->g.h = HEIGHT(win->ws->r);
2947 ev->value_mask |= CWY | CWHeight;
2950 if ((ev->value_mask & (CWX | CWY)) &&
2951 !(ev->value_mask & (CWWidth | CWHeight)))
2953 XMoveResizeWindow(display, win->id,
2954 win->g.x, win->g.y, win->g.w, win->g.h);
2961 configurenotify(XEvent *e)
2966 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
2967 e->xconfigure.window);
2969 XMapWindow(display, e->xconfigure.window);
2970 win = find_window(e->xconfigure.window);
2972 XGetWMNormalHints(display, win->id, &win->sh, &mask);
2974 XMapWindow(display, win->id);
2981 destroynotify(XEvent *e)
2984 XDestroyWindowEvent *ev = &e->xdestroywindow;
2986 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
2988 if ((win = find_window(ev->window)) != NULL) {
2989 unmanage_window(win);
2995 enternotify(XEvent *e)
2997 XCrossingEvent *ev = &e->xcrossing;
3000 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
3003 /* eat event(r) to prevent autofocus */
3008 if ((win = find_window(ev->window)) != NULL)
3015 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
3021 DNPRINTF(SWM_D_EVENT, "focusout: window: %lu\n", e->xfocus.window);
3023 if (cur_focus && cur_focus->ws->r &&
3024 cur_focus->id == e->xfocus.window) {
3025 struct swm_screen *s = cur_focus->ws->r->s;
3030 /* Try to detect synergy hiding the cursor. */
3031 if (XQueryPointer(display, cur_focus->id,
3032 &rr, &cr, &x, &y, &wx, &wy, &mask) != False &&
3034 x == DisplayWidth(display, s->idx)/2 &&
3035 y == DisplayHeight(display, s->idx)/2) {
3036 unfocus_win(cur_focus);
3042 mappingnotify(XEvent *e)
3044 XMappingEvent *ev = &e->xmapping;
3046 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
3048 XRefreshKeyboardMapping(ev);
3049 if (ev->request == MappingKeyboard)
3054 maprequest(XEvent *e)
3056 XMapRequestEvent *ev = &e->xmaprequest;
3057 XWindowAttributes wa;
3059 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
3060 e->xmaprequest.window);
3062 if (!XGetWindowAttributes(display, ev->window, &wa))
3064 if (wa.override_redirect)
3066 manage_window(e->xmaprequest.window);
3072 propertynotify(XEvent *e)
3075 XPropertyEvent *ev = &e->xproperty;
3077 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
3080 if (ev->state == PropertyDelete)
3081 return; /* ignore */
3082 win = find_window(ev->window);
3087 case XA_WM_NORMAL_HINTS:
3090 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3091 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
3092 if (win->sh.flags & PMinSize) {
3093 win->g.w = win->sh.min_width;
3094 win->g.h = win->sh.min_height;
3095 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
3097 XMoveResizeWindow(display, win->id,
3098 win->g.x, win->g.y, win->g.w, win->g.h);
3107 unmapnotify(XEvent *e)
3109 XDestroyWindowEvent *ev = &e->xdestroywindow;
3112 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
3114 if ((win = find_window(ev->window)) != NULL)
3116 unmanage_window(win);
3120 visibilitynotify(XEvent *e)
3123 struct swm_region *r;
3125 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
3126 e->xvisibility.window);
3127 if (e->xvisibility.state == VisibilityUnobscured)
3128 for (i = 0; i < ScreenCount(display); i++)
3129 TAILQ_FOREACH(r, &screens[i].rl, entry)
3130 if (e->xvisibility.window == r->bar_window)
3135 xerror_start(Display *d, XErrorEvent *ee)
3142 xerror(Display *d, XErrorEvent *ee)
3144 /* fprintf(stderr, "error: %p %p\n", display, ee); */
3152 xerrorxlib = XSetErrorHandler(xerror_start);
3154 /* this causes an error if some other window manager is running */
3155 XSelectInput(display, DefaultRootWindow(display),
3156 SubstructureRedirectMask);
3157 XSync(display, False);
3161 XSetErrorHandler(xerror);
3162 XSync(display, False);
3171 unsigned char *p = NULL;
3172 unsigned long n, extra;
3175 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
3176 &real, &format, &n, &extra, (unsigned char **)&p);
3177 if (status != Success)
3180 result = *((long *)p);
3186 new_region(struct swm_screen *s, int x, int y, int w, int h)
3188 struct swm_region *r, *n;
3189 struct workspace *ws = NULL;
3192 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
3193 s->idx, w, h, x, y);
3195 /* remove any conflicting regions */
3196 n = TAILQ_FIRST(&s->rl);
3199 n = TAILQ_NEXT(r, entry);
3200 if (X(r) < (x + w) &&
3201 (X(r) + WIDTH(r)) > x &&
3203 (Y(r) + HEIGHT(r)) > y) {
3204 XDestroyWindow(display, r->bar_window);
3205 TAILQ_REMOVE(&s->rl, r, entry);
3206 TAILQ_INSERT_TAIL(&s->orl, r, entry);
3210 /* search old regions for one to reuse */
3212 /* size + location match */
3213 TAILQ_FOREACH(r, &s->orl, entry)
3214 if (X(r) == x && Y(r) == y &&
3215 HEIGHT(r) == h && WIDTH(r) == w)
3219 TAILQ_FOREACH(r, &s->orl, entry)
3220 if (HEIGHT(r) == h && WIDTH(r) == w)
3224 TAILQ_REMOVE(&s->orl, r, entry);
3225 /* try to use old region's workspace */
3226 if (r->ws->r == NULL)
3229 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
3230 errx(1, "calloc: failed to allocate memory for screen");
3232 /* if we don't have a workspace already, find one */
3234 for (i = 0; i < SWM_WS_MAX; i++)
3235 if (s->ws[i].r == NULL) {
3242 errx(1, "no free workspaces\n");
3251 TAILQ_INSERT_TAIL(&s->rl, r, entry);
3257 #ifdef SWM_XRR_HAS_CRTC
3259 XRRScreenResources *sr;
3262 #endif /* SWM_XRR_HAS_CRTC */
3263 struct swm_region *r;
3266 if (i >= ScreenCount(display))
3267 errx(1, "invalid screen");
3269 /* remove any old regions */
3270 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
3272 XDestroyWindow(display, r->bar_window);
3273 TAILQ_REMOVE(&screens[i].rl, r, entry);
3274 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
3277 /* map virtual screens onto physical screens */
3278 #ifdef SWM_XRR_HAS_CRTC
3279 if (xrandr_support) {
3280 sr = XRRGetScreenResources(display, screens[i].root);
3282 new_region(&screens[i], 0, 0,
3283 DisplayWidth(display, i),
3284 DisplayHeight(display, i));
3288 for (c = 0, ci = NULL; c < ncrtc; c++) {
3289 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
3290 if (ci->noutput == 0)
3293 if (ci != NULL && ci->mode == None)
3294 new_region(&screens[i], 0, 0,
3295 DisplayWidth(display, i),
3296 DisplayHeight(display, i));
3298 new_region(&screens[i],
3299 ci->x, ci->y, ci->width, ci->height);
3302 XRRFreeCrtcInfo(ci);
3303 XRRFreeScreenResources(sr);
3305 #endif /* SWM_XRR_HAS_CRTC */
3307 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
3308 DisplayHeight(display, i));
3313 screenchange(XEvent *e) {
3314 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
3315 struct swm_region *r;
3319 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
3321 if (!XRRUpdateConfiguration(e))
3324 /* silly event doesn't include the screen index */
3325 for (i = 0; i < ScreenCount(display); i++)
3326 if (screens[i].root == xe->root)
3328 if (i >= ScreenCount(display))
3329 errx(1, "screenchange: screen not found\n");
3331 /* brute force for now, just re-enumerate the regions */
3334 /* hide any windows that went away */
3335 TAILQ_FOREACH(r, &screens[i].rl, entry)
3336 TAILQ_FOREACH(win, &r->ws->winlist, entry)
3337 XUnmapWindow(display, win->id);
3339 /* add bars to all regions */
3340 for (i = 0; i < ScreenCount(display); i++)
3341 TAILQ_FOREACH(r, &screens[i].rl, entry)
3349 Window d1, d2, *wins = NULL;
3350 XWindowAttributes wa;
3353 int errorbase, major, minor;
3354 struct workspace *ws;
3358 if ((screens = calloc(ScreenCount(display),
3359 sizeof(struct swm_screen))) == NULL)
3360 errx(1, "calloc: screens");
3362 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3364 /* initial Xrandr setup */
3365 xrandr_support = XRRQueryExtension(display,
3366 &xrandr_eventbase, &errorbase);
3368 if (XRRQueryVersion(display, &major, &minor) && major < 1)
3371 /* map physical screens */
3372 for (i = 0; i < ScreenCount(display); i++) {
3373 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
3375 TAILQ_INIT(&screens[i].rl);
3376 TAILQ_INIT(&screens[i].orl);
3377 screens[i].root = RootWindow(display, i);
3379 /* set default colors */
3380 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
3381 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
3382 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
3383 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
3384 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
3386 /* init all workspaces */
3387 /* XXX these should be dynamically allocated too */
3388 for (j = 0; j < SWM_WS_MAX; j++) {
3389 ws = &screens[i].ws[j];
3394 TAILQ_INIT(&ws->winlist);
3396 for (k = 0; layouts[k].l_stack != NULL; k++)
3397 if (layouts[k].l_config != NULL)
3398 layouts[k].l_config(ws,
3399 SWM_ARG_ID_STACKINIT);
3400 ws->cur_layout = &layouts[0];
3402 /* grab existing windows (before we build the bars)*/
3403 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
3409 XRRSelectInput(display, screens[i].root,
3410 RRScreenChangeNotifyMask);
3412 /* attach windows to a region */
3413 /* normal windows */
3414 for (j = 0; j < no; j++) {
3415 XGetWindowAttributes(display, wins[j], &wa);
3416 if (!XGetWindowAttributes(display, wins[j], &wa) ||
3417 wa.override_redirect ||
3418 XGetTransientForHint(display, wins[j], &d1))
3421 if (wa.map_state == IsViewable ||
3422 getstate(wins[j]) == NormalState)
3423 manage_window(wins[j]);
3425 /* transient windows */
3426 for (j = 0; j < no; j++) {
3427 if (!XGetWindowAttributes(display, wins[j], &wa))
3430 if (XGetTransientForHint(display, wins[j], &d1) &&
3431 (wa.map_state == IsViewable || getstate(wins[j]) ==
3433 manage_window(wins[j]);
3446 Atom netwmcheck, netwmname, utf8_string;
3449 /* work around sun jdk bugs, code from wmname */
3450 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
3451 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
3452 utf8_string = XInternAtom(display, "UTF8_STRING", False);
3453 for (i = 0; i < ScreenCount(display); i++) {
3454 root = screens[i].root;
3455 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
3456 PropModeReplace, (unsigned char *)&root, 1);
3457 XChangeProperty(display, root, netwmname, utf8_string, 8,
3458 PropModeReplace, "LG3D", strlen("LG3D"));
3463 main(int argc, char *argv[])
3466 struct swm_region *r;
3467 char conf[PATH_MAX], *cfile = NULL;
3474 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
3475 SWM_VERSION, cvstag);
3476 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
3477 warnx("no locale support");
3479 if (!(display = XOpenDisplay(0)))
3480 errx(1, "can not open display");
3483 errx(1, "other wm running");
3485 astate = XInternAtom(display, "WM_STATE", False);
3486 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
3487 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
3489 /* look for local and global conf file */
3490 pwd = getpwuid(getuid());
3492 errx(1, "invalid user %d", getuid());
3498 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
3499 if (stat(conf, &sb) != -1) {
3500 if (S_ISREG(sb.st_mode))
3503 /* try global conf file */
3504 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
3505 if (!stat(conf, &sb))
3506 if (S_ISREG(sb.st_mode))
3512 /* setup all bars */
3513 for (i = 0; i < ScreenCount(display); i++)
3514 TAILQ_FOREACH(r, &screens[i].rl, entry)
3517 /* set some values to work around bad programs */
3523 xfd = ConnectionNumber(display);
3527 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
3529 errx(1, "select failed");
3534 while (XPending(display)) {
3535 XNextEvent(display, &e);
3536 if (e.type < LASTEvent) {
3537 if (handler[e.type])
3538 handler[e.type](&e);
3540 DNPRINTF(SWM_D_EVENT,
3541 "win: %lu unknown event: %d\n",
3542 e.xany.window, e.type);
3544 switch (e.type - xrandr_eventbase) {
3545 case RRScreenChangeNotify:
3549 DNPRINTF(SWM_D_EVENT,
3550 "win: %lu unknown xrandr event: "
3551 "%d\n", e.xany.window, e.type);
3558 XCloseDisplay(display);