3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Much code and ideas taken from dwm under the following license:
20 * MIT/X Consortium License
22 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
23 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
24 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
25 * 2007 Premysl Hruby <dfenze at gmail dot com>
26 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
27 * 2007 Christof Musik <christof at sendfax dot de>
28 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
29 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
30 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, including without limitation
35 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 * and/or sell copies of the Software, and to permit persons to whom the
37 * Software is furnished to do so, subject to the following conditions:
39 * The above copyright notice and this permission notice shall be included in
40 * all copies or substantial portions of the Software.
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
51 static const char *cvstag = "$scrotwm$";
53 #define SWM_VERSION "0.8"
69 #include <sys/types.h>
73 #include <sys/queue.h>
74 #include <sys/param.h>
75 #include <sys/select.h>
77 #include <X11/cursorfont.h>
78 #include <X11/keysym.h>
79 #include <X11/Xatom.h>
81 #include <X11/Xproto.h>
82 #include <X11/Xutil.h>
83 #include <X11/extensions/Xrandr.h>
86 # error XRandR versions less than 1.0 are not supported
91 #define SWM_XRR_HAS_CRTC
95 /* #define SWM_DEBUG */
97 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
98 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
99 #define SWM_D_MISC 0x0001
100 #define SWM_D_EVENT 0x0002
101 #define SWM_D_WS 0x0004
102 #define SWM_D_FOCUS 0x0008
103 #define SWM_D_MOVE 0x0010
104 #define SWM_D_STACK 0x0020
105 #define SWM_D_MOUSE 0x0040
106 #define SWM_D_PROP 0x0080
107 #define SWM_D_CLASS 0x0100
109 u_int32_t swm_debug = 0
121 #define DPRINTF(x...)
122 #define DNPRINTF(n,x...)
125 #define LENGTH(x) (sizeof x / sizeof x[0])
126 #define MODKEY Mod1Mask
127 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
128 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
129 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
130 #define SWM_PROPLEN (16)
131 #define X(r) (r)->g.x
132 #define Y(r) (r)->g.y
133 #define WIDTH(r) (r)->g.w
134 #define HEIGHT(r) (r)->g.h
135 #define SWM_MAX_FONT_STEPS (3)
138 #define SWM_LIB "/usr/X11R6/lib/swmhack.so"
143 int (*xerrorxlib)(Display *, XErrorEvent *);
148 int xrandr_eventbase;
149 int ignore_enter = 0;
150 unsigned int numlockmask = 0;
154 int cycle_visible = 0;
156 int font_adjusted = 0;
159 double dialog_ratio = .6;
161 #define SWM_BAR_MAX (128)
162 char *bar_argv[] = { NULL, NULL };
164 char bar_ext[SWM_BAR_MAX];
165 char bar_vertext[SWM_BAR_MAX];
167 sig_atomic_t bar_alarm = 0;
171 int bar_extra_running = 0;
179 char *bar_fonts[] = {
180 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
181 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
185 /* terminal + args */
186 char *spawn_term[] = { "xterm", NULL };
187 char *spawn_screenshot[] = { "screenshot.sh", NULL, NULL };
188 char *spawn_lock[] = { "xlock", NULL };
189 char *spawn_initscr[] = { "initscreen.sh", NULL };
190 char *spawn_menu[] = { "dmenu_run", "-fn", NULL, "-nb", NULL,
191 "-nf", NULL, "-sb", NULL, "-sf", NULL, NULL };
193 #define SWM_MENU_FN (2)
194 #define SWM_MENU_NB (4)
195 #define SWM_MENU_NF (6)
196 #define SWM_MENU_SB (8)
197 #define SWM_MENU_SF (10)
199 /* layout manager data */
200 struct swm_geometry {
210 /* virtual "screens" */
212 TAILQ_ENTRY(swm_region) entry;
213 struct swm_geometry g;
214 struct workspace *ws; /* current workspace on this region */
215 struct swm_screen *s; /* screen idx */
218 TAILQ_HEAD(swm_region_list, swm_region);
221 TAILQ_ENTRY(ws_win) entry;
223 struct swm_geometry g;
228 int font_size_boundary[SWM_MAX_FONT_STEPS];
231 unsigned long quirks;
232 struct workspace *ws; /* always valid */
233 struct swm_screen *s; /* always valid, never changes */
234 XWindowAttributes wa;
238 TAILQ_HEAD(ws_win_list, ws_win);
240 /* layout handlers */
242 void vertical_config(struct workspace *, int);
243 void vertical_stack(struct workspace *, struct swm_geometry *);
244 void horizontal_config(struct workspace *, int);
245 void horizontal_stack(struct workspace *, struct swm_geometry *);
246 void max_stack(struct workspace *, struct swm_geometry *);
248 void grabbuttons(struct ws_win *, int);
251 void (*l_stack)(struct workspace *, struct swm_geometry *);
252 void (*l_config)(struct workspace *, int);
254 /* stack, configure */
255 { vertical_stack, vertical_config},
256 { horizontal_stack, horizontal_config},
261 #define SWM_H_SLICE (32)
262 #define SWM_V_SLICE (32)
264 /* define work spaces */
266 int idx; /* workspace index */
267 int restack; /* restack on switch */
268 struct layout *cur_layout; /* current layout handlers */
269 struct ws_win *focus; /* may be NULL */
270 struct ws_win *focus_prev; /* may be NULL */
271 struct swm_region *r; /* may be NULL */
272 struct ws_win_list winlist; /* list of windows in ws */
276 int horizontal_msize;
278 int horizontal_stacks;
285 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
286 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
288 /* physical screen mapping */
289 #define SWM_WS_MAX (10) /* XXX Too small? */
291 int idx; /* screen index */
292 struct swm_region_list rl; /* list of regions on this screen */
293 struct swm_region_list orl; /* list of old regions */
295 struct workspace ws[SWM_WS_MAX];
301 } c[SWM_S_COLOR_MAX];
303 struct swm_screen *screens;
306 struct ws_win *cur_focus = NULL;
308 /* args to functions */
311 #define SWM_ARG_ID_FOCUSNEXT (0)
312 #define SWM_ARG_ID_FOCUSPREV (1)
313 #define SWM_ARG_ID_FOCUSMAIN (2)
314 #define SWM_ARG_ID_SWAPNEXT (3)
315 #define SWM_ARG_ID_SWAPPREV (4)
316 #define SWM_ARG_ID_SWAPMAIN (5)
317 #define SWM_ARG_ID_MASTERSHRINK (6)
318 #define SWM_ARG_ID_MASTERGROW (7)
319 #define SWM_ARG_ID_MASTERADD (8)
320 #define SWM_ARG_ID_MASTERDEL (9)
321 #define SWM_ARG_ID_STACKRESET (10)
322 #define SWM_ARG_ID_STACKINIT (11)
323 #define SWM_ARG_ID_CYCLEWS_UP (12)
324 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
325 #define SWM_ARG_ID_CYCLESC_UP (14)
326 #define SWM_ARG_ID_CYCLESC_DOWN (15)
327 #define SWM_ARG_ID_STACKINC (16)
328 #define SWM_ARG_ID_STACKDEC (17)
329 #define SWM_ARG_ID_SS_ALL (0)
330 #define SWM_ARG_ID_SS_WINDOW (1)
331 #define SWM_ARG_ID_DONTCENTER (0)
332 #define SWM_ARG_ID_CENTER (1)
341 #define SWM_Q_FLOAT (1<<0) /* float this window */
342 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
343 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
344 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
346 { "MPlayer", "xv", SWM_Q_FLOAT },
347 { "OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT },
348 { "OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT },
349 { "Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ},
350 { "Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE},
351 { "XTerm", "xterm", SWM_Q_XTERM_FONTADJ},
356 void expose(XEvent *);
357 void keypress(XEvent *);
358 void buttonpress(XEvent *);
359 void configurerequest(XEvent *);
360 void configurenotify(XEvent *);
361 void destroynotify(XEvent *);
362 void enternotify(XEvent *);
363 void focusin(XEvent *);
364 void mappingnotify(XEvent *);
365 void maprequest(XEvent *);
366 void propertynotify(XEvent *);
367 void unmapnotify(XEvent *);
368 void visibilitynotify(XEvent *);
370 void (*handler[LASTEvent])(XEvent *) = {
372 [KeyPress] = keypress,
373 [ButtonPress] = buttonpress,
374 [ConfigureRequest] = configurerequest,
375 [ConfigureNotify] = configurenotify,
376 [DestroyNotify] = destroynotify,
377 [EnterNotify] = enternotify,
379 [MappingNotify] = mappingnotify,
380 [MapRequest] = maprequest,
381 [PropertyNotify] = propertynotify,
382 [UnmapNotify] = unmapnotify,
383 [VisibilityNotify] = visibilitynotify,
387 name_to_color(char *colorname)
391 XColor screen_def, exact_def;
392 unsigned long result = 0;
393 char cname[32] = "#";
395 cmap = DefaultColormap(display, screens[0].idx);
396 status = XAllocNamedColor(display, cmap, colorname,
397 &screen_def, &exact_def);
399 strlcat(cname, colorname + 2, sizeof cname - 1);
400 status = XAllocNamedColor(display, cmap, cname, &screen_def,
404 result = screen_def.pixel;
406 fprintf(stderr, "color '%s' not found.\n", colorname);
412 setscreencolor(char *val, int i, int c)
414 if (i > 0 && i <= ScreenCount(display)) {
415 screens[i - 1].c[c].color = name_to_color(val);
416 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
418 } else if (i == -1) {
419 for (i = 0; i < ScreenCount(display); i++)
420 screens[i].c[c].color = name_to_color(val);
421 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
424 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
425 i, ScreenCount(display));
428 void new_region(struct swm_screen *, int, int, int, int);
431 custom_region(char *val)
433 unsigned int sidx, x, y, w, h;
435 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
436 errx(1, "invalid custom region, "
437 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
438 if (sidx < 1 || sidx > ScreenCount(display))
439 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
440 sidx, ScreenCount(display));
444 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
446 if (x < 0 || x > DisplayWidth(display, sidx) ||
447 y < 0 || y > DisplayHeight(display, sidx) ||
448 w + x > DisplayWidth(display, sidx) ||
449 h + y > DisplayHeight(display, sidx))
450 errx(1, "region %ux%u+%u+%u not within screen boundaries "
451 "(%ux%u)\n", w, h, x, y,
452 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
454 new_region(&screens[sidx], x, y, w, h);
458 varmatch(char *var, char *name, int *index)
463 i = strncmp(var, name, 255);
470 p = var + strlen(name);
473 bzero(buf, sizeof buf);
475 while (isdigit(*p) && i < sizeof buf)
477 if (i == 0 || i >= sizeof buf || *p != ']')
479 *index = strtonum(buf, 0, 99, NULL);
483 /* conf file stuff */
484 #define SWM_CONF_WS "\n= \t"
485 #define SWM_CONF_FILE "scrotwm.conf"
487 conf_load(char *filename)
490 char *line, *cp, *var, *val;
491 size_t len, lineno = 0;
494 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
496 if (filename == NULL)
499 if ((config = fopen(filename, "r")) == NULL)
502 for (sc = ScreenCount(display);;) {
503 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
507 cp += (long)strspn(cp, SWM_CONF_WS);
513 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
515 cp += (long)strspn(cp, SWM_CONF_WS);
516 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
519 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
522 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
523 bar_enabled = atoi(val);
524 else if (!varmatch(var, "bar_border", &i))
525 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
526 else if (!varmatch(var, "bar_color", &i))
527 setscreencolor(val, i, SWM_S_COLOR_BAR);
528 else if (!varmatch(var, "bar_font_color", &i))
529 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
530 else if (!strncmp(var, "bar_font", strlen("bar_font")))
531 asprintf(&bar_fonts[0], "%s", val);
532 else if (!strncmp(var, "bar_action", strlen("bar_action")))
533 asprintf(&bar_argv[0], "%s", val);
534 else if (!strncmp(var, "bar_delay", strlen("bar_delay")))
535 bar_delay = atoi(val);
541 if (!varmatch(var, "color_focus", &i))
542 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
543 else if (!varmatch(var, "color_unfocus", &i))
544 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
545 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
546 cycle_visible = atoi(val);
547 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
548 cycle_visible = atoi(val);
554 if (!strncmp(var, "dialog_ratio",
555 strlen("dialog_ratio"))) {
556 dialog_ratio = atof(val);
557 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
564 if (!strncmp(var, "region", strlen("region")))
571 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
572 asprintf(&spawn_term[0], "%s", val);
573 else if (!strncmp(var, "screenshot_enabled",
574 strlen("screenshot_enabled")))
575 ss_enabled = atoi(val);
576 else if (!strncmp(var, "screenshot_app",
577 strlen("screenshot_app")))
578 asprintf(&spawn_screenshot[0], "%s", val);
584 if (!strncmp(var, "term_width", strlen("term_width")))
585 term_width = atoi(val);
600 errx(1, "invalid conf file entry: %s=%s", var, val);
604 socket_setnonblock(int fd)
608 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
609 err(1, "fcntl F_GETFL");
611 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
612 err(1, "fcntl F_SETFL");
616 bar_print(struct swm_region *r, char *s)
618 XClearWindow(display, r->bar_window);
619 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
620 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
629 bzero(bar_pipe, sizeof bar_pipe);
632 kill(bar_pid, SIGTERM);
635 strlcpy(bar_ext, "", sizeof bar_ext);
644 struct swm_region *r;
648 char loc[SWM_BAR_MAX];
651 if (bar_enabled == 0)
654 if (bar_extra && bar_extra_running) {
655 /* ignore short reads; it'll correct itself */
656 while ((b = fgetln(stdin, &len)) != NULL)
657 if (b && b[len - 1] == '\n') {
659 strlcpy(bar_ext, b, sizeof bar_ext);
661 if (b == NULL && errno != EAGAIN) {
662 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
663 errno, strerror(errno));
667 strlcpy(bar_ext, "", sizeof bar_ext);
670 localtime_r(&tmt, &tm);
671 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
672 for (i = 0; i < ScreenCount(display); i++) {
674 TAILQ_FOREACH(r, &screens[i].rl, entry) {
675 snprintf(loc, sizeof loc, "%s %d:%d %s %s",
676 s, x++, r->ws->idx + 1, bar_ext, bar_vertext);
680 XSync(display, False);
691 bar_toggle(struct swm_region *r, union arg *args)
693 struct swm_region *tmpr;
694 int i, j, sc = ScreenCount(display);
696 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
699 for (i = 0; i < sc; i++)
700 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
701 XUnmapWindow(display, tmpr->bar_window);
703 for (i = 0; i < sc; i++)
704 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
705 XMapRaised(display, tmpr->bar_window);
707 bar_enabled = !bar_enabled;
708 XSync(display, False);
709 for (i = 0; i < sc; i++)
710 for (j = 0; j < SWM_WS_MAX; j++)
711 screens[i].ws[j].restack = 1;
714 /* must be after stack */
715 for (i = 0; i < sc; i++)
716 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
723 XSetWindowAttributes wa;
724 struct swm_region *r;
727 /* do this here because the conf file is in memory */
728 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
729 /* launch external status app */
730 bar_extra_running = 1;
731 if (pipe(bar_pipe) == -1)
732 err(1, "pipe error");
733 socket_setnonblock(bar_pipe[0]);
734 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
735 if (dup2(bar_pipe[0], 0) == -1)
737 if (dup2(bar_pipe[1], 1) == -1)
739 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
740 err(1, "could not disable SIGPIPE");
741 switch (bar_pid = fork()) {
743 err(1, "cannot fork");
747 execvp(bar_argv[0], bar_argv);
748 err(1, "%s external app failed", bar_argv[0]);
750 default: /* parent */
756 bzero(&wa, sizeof wa);
757 for (i = 0; i < ScreenCount(display); i++)
758 TAILQ_FOREACH(r, &screens[i].rl, entry) {
760 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
761 wa.background_pixel =
762 screens[i].c[SWM_S_COLOR_BAR].color;
763 XChangeWindowAttributes(display, r->bar_window,
764 CWBackPixel | CWBorderPixel, &wa);
770 bar_setup(struct swm_region *r)
774 for (i = 0; bar_fonts[i] != NULL; i++) {
775 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
781 if (bar_fonts[i] == NULL)
782 errx(1, "couldn't load font");
783 bar_height = bar_fs->ascent + bar_fs->descent + 3;
785 r->bar_window = XCreateSimpleWindow(display,
786 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
787 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
788 r->s->c[SWM_S_COLOR_BAR].color);
789 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
790 XSetFont(display, bar_gc, bar_fs->fid);
791 XSelectInput(display, r->bar_window, VisibilityChangeMask);
793 XMapRaised(display, r->bar_window);
794 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
796 if (signal(SIGALRM, bar_signal) == SIG_ERR)
797 err(1, "could not install bar_signal");
802 version(struct swm_region *r, union arg *args)
804 bar_version = !bar_version;
806 strlcpy(bar_vertext, cvstag, sizeof bar_vertext);
808 strlcpy(bar_vertext, "", sizeof bar_vertext);
813 config_win(struct ws_win *win)
817 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
818 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
819 ce.type = ConfigureNotify;
820 ce.display = display;
826 ce.height = win->g.h;
827 ce.border_width = 1; /* XXX store this! */
829 ce.override_redirect = False;
830 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
834 count_win(struct workspace *ws, int count_transient)
839 TAILQ_FOREACH(win, &ws->winlist, entry) {
840 if (count_transient == 0 && win->floating)
842 if (count_transient == 0 && win->transient)
846 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
852 quit(struct swm_region *r, union arg *args)
854 DNPRINTF(SWM_D_MISC, "quit\n");
864 for (i = 0; i < ScreenCount(display); i++)
865 for (j = 0; j < SWM_WS_MAX; j++)
866 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
867 XUnmapWindow(display, win->id);
871 fake_keypress(struct ws_win *win, int keysym, int modifiers)
875 event.display = display; /* Ignored, but what the hell */
876 event.window = win->id;
877 event.root = win->s->root;
878 event.subwindow = None;
879 event.time = CurrentTime;
884 event.same_screen = True;
885 event.keycode = XKeysymToKeycode(display, keysym);
886 event.state = modifiers;
888 event.type = KeyPress;
889 XSendEvent(event.display, event.window, True,
890 KeyPressMask, (XEvent *)&event);
892 event.type = KeyRelease;
893 XSendEvent(event.display, event.window, True,
894 KeyPressMask, (XEvent *)&event);
899 restart(struct swm_region *r, union arg *args)
901 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
903 /* disable alarm because the following code may not be interrupted */
905 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
906 errx(1, "can't disable alarm");
911 XCloseDisplay(display);
912 execvp(start_argv[0], start_argv);
913 fprintf(stderr, "execvp failed\n");
919 root_to_region(Window root)
921 struct swm_region *r = NULL;
926 for (i = 0; i < ScreenCount(display); i++)
927 if (screens[i].root == root)
930 if (XQueryPointer(display, screens[i].root,
931 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
932 /* choose a region based on pointer location */
933 TAILQ_FOREACH(r, &screens[i].rl, entry)
934 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
935 y >= Y(r) && y <= Y(r) + HEIGHT(r))
940 r = TAILQ_FIRST(&screens[i].rl);
946 find_window(Window id)
951 for (i = 0; i < ScreenCount(display); i++)
952 for (j = 0; j < SWM_WS_MAX; j++)
953 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
960 spawn(struct swm_region *r, union arg *args)
965 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
967 * The double-fork construct avoids zombie processes and keeps the code
968 * clean from stupid signal handlers.
973 close(ConnectionNumber(display));
974 setenv("LD_PRELOAD", SWM_LIB, 1);
975 if (asprintf(&ret, "%d", r->ws->idx)) {
976 setenv("_SWM_WS", ret, 1);
979 if (asprintf(&ret, "%d", getpid())) {
980 setenv("_SWM_PID", ret, 1);
984 /* kill stdin, mplayer, ssh-add etc. need that */
985 si = open("/dev/null", O_RDONLY, 0);
987 err(1, "open /dev/null");
988 if (dup2(si, 0) == -1)
989 err(1, "dup2 /dev/null");
990 execvp(args->argv[0], args->argv);
991 fprintf(stderr, "execvp failed\n");
1000 spawnterm(struct swm_region *r, union arg *args)
1002 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1005 setenv("_SWM_XTERM_FONTADJ", "", 1);
1010 spawnmenu(struct swm_region *r, union arg *args)
1012 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
1014 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
1015 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
1016 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
1017 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
1018 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
1029 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
1031 for (i = 0; i < ScreenCount(display); i++)
1032 for (j = 0; j < SWM_WS_MAX; j++)
1033 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1034 if (win->ws->r == NULL)
1036 grabbuttons(win, 0);
1037 XSetWindowBorder(display, win->id,
1038 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1040 win->ws->focus = NULL;
1046 focus_win(struct ws_win *win)
1048 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1053 if (win->ws->focus != win && win->ws->focus != NULL)
1054 win->ws->focus_prev = win->ws->focus;
1057 win->ws->focus = win;
1058 if (win->ws->r != NULL) {
1060 if (!win->got_focus) {
1061 XSetWindowBorder(display, win->id,
1062 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1063 grabbuttons(win, 1);
1066 XSetInputFocus(display, win->id,
1067 RevertToPointerRoot, CurrentTime);
1072 switchws(struct swm_region *r, union arg *args)
1074 int wsid = args->id;
1075 struct swm_region *this_r, *other_r;
1077 struct workspace *new_ws, *old_ws;
1080 old_ws = this_r->ws;
1081 new_ws = &this_r->s->ws[wsid];
1083 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1084 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1087 if (new_ws == old_ws)
1090 other_r = new_ws->r;
1092 /* if the other workspace is hidden, switch windows */
1093 /* map new window first to prevent ugly blinking */
1095 old_ws->restack = 1;
1097 TAILQ_FOREACH(win, &new_ws->winlist, entry)
1098 XMapRaised(display, win->id);
1100 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1101 XUnmapWindow(display, win->id);
1103 other_r->ws = old_ws;
1104 old_ws->r = other_r;
1106 this_r->ws = new_ws;
1112 focus_win(new_ws->focus);
1118 cyclews(struct swm_region *r, union arg *args)
1121 struct swm_screen *s = r->s;
1123 DNPRINTF(SWM_D_WS, "cyclews id %d "
1124 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1125 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1130 case SWM_ARG_ID_CYCLEWS_UP:
1131 if (a.id < SWM_WS_MAX - 1)
1136 case SWM_ARG_ID_CYCLEWS_DOWN:
1140 a.id = SWM_WS_MAX - 1;
1146 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1148 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1152 } while (a.id != r->ws->idx);
1156 cyclescr(struct swm_region *r, union arg *args)
1158 struct swm_region *rr;
1163 case SWM_ARG_ID_CYCLESC_UP:
1164 rr = TAILQ_NEXT(r, entry);
1166 rr = TAILQ_FIRST(&screens[i].rl);
1168 case SWM_ARG_ID_CYCLESC_DOWN:
1169 rr = TAILQ_PREV(r, swm_region_list, entry);
1171 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1177 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1178 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, rr->g.x,
1179 rr->g.y + bar_enabled ? bar_height : 0);
1183 swapwin(struct swm_region *r, union arg *args)
1185 struct ws_win *target, *source;
1186 struct ws_win_list *wl;
1189 DNPRINTF(SWM_D_WS, "swapwin id %d "
1190 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1191 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1192 if (cur_focus == NULL)
1196 wl = &source->ws->winlist;
1199 case SWM_ARG_ID_SWAPPREV:
1200 target = TAILQ_PREV(source, ws_win_list, entry);
1201 TAILQ_REMOVE(wl, cur_focus, entry);
1203 TAILQ_INSERT_TAIL(wl, source, entry);
1205 TAILQ_INSERT_BEFORE(target, source, entry);
1207 case SWM_ARG_ID_SWAPNEXT:
1208 target = TAILQ_NEXT(source, entry);
1209 TAILQ_REMOVE(wl, source, entry);
1211 TAILQ_INSERT_HEAD(wl, source, entry);
1213 TAILQ_INSERT_AFTER(wl, target, source, entry);
1215 case SWM_ARG_ID_SWAPMAIN:
1216 target = TAILQ_FIRST(wl);
1217 if (target == source) {
1218 if (source->ws->focus_prev != NULL &&
1219 source->ws->focus_prev != target)
1221 source = source->ws->focus_prev;
1225 source->ws->focus_prev = target;
1226 TAILQ_REMOVE(wl, target, entry);
1227 TAILQ_INSERT_BEFORE(source, target, entry);
1228 TAILQ_REMOVE(wl, source, entry);
1229 TAILQ_INSERT_HEAD(wl, source, entry);
1232 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1241 focus(struct swm_region *r, union arg *args)
1243 struct ws_win *winfocus, *winlostfocus;
1244 struct ws_win_list *wl;
1246 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1247 if (cur_focus == NULL)
1250 wl = &cur_focus->ws->winlist;
1252 winlostfocus = cur_focus;
1255 case SWM_ARG_ID_FOCUSPREV:
1256 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1257 if (winfocus == NULL)
1258 winfocus = TAILQ_LAST(wl, ws_win_list);
1261 case SWM_ARG_ID_FOCUSNEXT:
1262 winfocus = TAILQ_NEXT(cur_focus, entry);
1263 if (winfocus == NULL)
1264 winfocus = TAILQ_FIRST(wl);
1267 case SWM_ARG_ID_FOCUSMAIN:
1268 winfocus = TAILQ_FIRST(wl);
1275 if (winfocus == winlostfocus || winfocus == NULL)
1278 XMapRaised(display, winfocus->id);
1279 focus_win(winfocus);
1280 XSync(display, False);
1284 cycle_layout(struct swm_region *r, union arg *args)
1286 struct workspace *ws = r->ws;
1288 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1291 if (ws->cur_layout->l_stack == NULL)
1292 ws->cur_layout = &layouts[0];
1298 stack_config(struct swm_region *r, union arg *args)
1300 struct workspace *ws = r->ws;
1302 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1305 if (ws->cur_layout->l_config != NULL)
1306 ws->cur_layout->l_config(ws, args->id);
1308 if (args->id != SWM_ARG_ID_STACKINIT);
1314 struct swm_geometry g;
1315 struct swm_region *r;
1318 DNPRINTF(SWM_D_STACK, "stack\n");
1320 for (i = 0; i < ScreenCount(display); i++) {
1322 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1323 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1324 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1326 /* start with screen geometry, adjust for bar */
1336 r->ws->cur_layout->l_stack(r->ws, &g);
1341 XSync(display, False);
1345 stack_floater(struct ws_win *win, struct swm_region *r)
1350 bzero(&wc, sizeof wc);
1351 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1352 wc.border_width = 1;
1353 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1354 win->g.w = (double)WIDTH(r) * dialog_ratio;
1355 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1357 wc.width = win->g.w;
1358 wc.height = win->g.h;
1363 wc.x = (WIDTH(r) - win->g.w) / 2;
1364 wc.y = (HEIGHT(r) - win->g.h) / 2;
1367 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1368 win->id, wc.x, wc.y, wc.width, wc.height);
1370 XConfigureWindow(display, win->id, mask, &wc);
1374 * Send keystrokes to terminal to decrease/increase the font size as the
1375 * window size changes.
1378 adjust_font(struct ws_win *win)
1380 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1381 win->floating || win->transient)
1384 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1385 win->g.w / win->sh.width_inc < term_width &&
1386 win->font_steps < SWM_MAX_FONT_STEPS) {
1387 win->font_size_boundary[win->font_steps] =
1388 (win->sh.width_inc * term_width) + win->sh.base_width;
1391 win->last_inc = win->sh.width_inc;
1392 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1393 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1394 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1397 win->last_inc = win->sh.width_inc;
1398 fake_keypress(win, XK_KP_Add, ShiftMask);
1402 #define SWAPXY(g) do { \
1404 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1405 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1408 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1411 struct swm_geometry win_g, r_g = *g;
1412 struct ws_win *win, *winfocus;
1413 int i, j, s, stacks;
1414 int w_inc = 1, h_inc, w_base = 1, h_base;
1415 int hrh, extra = 0, h_slice, last_h = 0;
1416 int split, colno, winno, mwin, msize, mscale;
1417 int remain, missing, v_slice;;
1420 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1421 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1423 if ((winno = count_win(ws, 0)) == 0)
1426 if (ws->focus == NULL)
1427 ws->focus = TAILQ_FIRST(&ws->winlist);
1428 winfocus = cur_focus ? cur_focus : ws->focus;
1430 TAILQ_FOREACH(win, &ws->winlist, entry)
1431 if (win->transient == 0 && win->floating == 0)
1438 w_inc = win->sh.width_inc;
1439 w_base = win->sh.base_width;
1440 mwin = ws->l_state.horizontal_mwin;
1441 mscale = ws->l_state.horizontal_msize;
1442 stacks = ws->l_state.horizontal_stacks;
1445 w_inc = win->sh.height_inc;
1446 w_base = win->sh.base_height;
1447 mwin = ws->l_state.vertical_mwin;
1448 mscale = ws->l_state.vertical_msize;
1449 stacks = ws->l_state.vertical_stacks;
1453 if (stacks > winno - mwin)
1454 stacks = winno - mwin;
1458 h_slice = r_g.h / SWM_H_SLICE;
1459 if (mwin && winno > mwin) {
1460 v_slice = r_g.w / SWM_V_SLICE;
1464 win_g.w = v_slice * mscale;
1466 if (w_inc > 1 && w_inc < v_slice) {
1467 /* adjust for window's requested size increment */
1468 remain = (win_g.w - w_base) % w_inc;
1469 missing = w_inc - remain;
1476 win_g.x += r_g.w - msize;
1479 colno = split = winno / stacks;
1480 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
1482 hrh = r_g.h / colno;
1483 extra = r_g.h - (colno * hrh);
1486 /* stack all the tiled windows */
1487 i = j = 0, s = stacks;
1488 TAILQ_FOREACH(win, &ws->winlist, entry) {
1489 if (win->transient != 0 || win->floating != 0)
1492 if (split && i == split) {
1493 colno = (winno - mwin) / stacks;
1494 if (s <= (winno - mwin) % stacks)
1496 split = split + colno;
1497 hrh = (r_g.h / colno);
1498 extra = r_g.h - (colno * hrh);
1502 win_g.x += win_g.w + 2;
1503 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
1505 win_g.w += (r_g.w - msize - (stacks * 2)) %
1512 h_inc = win->sh.width_inc;
1513 h_base = win->sh.base_width;
1515 h_inc = win->sh.height_inc;
1516 h_base = win->sh.base_height;
1518 if (j == colno - 1) {
1519 win_g.h = hrh + extra;
1520 } else if (h_inc > 1 && h_inc < h_slice) {
1521 /* adjust for window's requested size increment */
1522 remain = (win_g.h - h_base) % h_inc;
1523 missing = h_inc - remain;
1525 if (missing <= extra || j == 0) {
1537 win_g.y += last_h + 2;
1539 bzero(&wc, sizeof wc);
1540 wc.border_width = 1;
1542 win->g.x = wc.x = win_g.y;
1543 win->g.y = wc.y = win_g.x;
1544 win->g.w = wc.width = win_g.h;
1545 win->g.h = wc.height = win_g.w;
1547 win->g.x = wc.x = win_g.x;
1548 win->g.y = wc.y = win_g.y;
1549 win->g.w = wc.width = win_g.w;
1550 win->g.h = wc.height = win_g.h;
1553 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1554 XConfigureWindow(display, win->id, mask, &wc);
1555 XMapRaised(display, win->id);
1563 /* now, stack all the floaters and transients */
1564 TAILQ_FOREACH(win, &ws->winlist, entry) {
1565 if (win->transient == 0 && win->floating == 0)
1568 stack_floater(win, ws->r);
1569 XMapRaised(display, win->id);
1573 focus_win(winfocus); /* has to be done outside of the loop */
1577 vertical_config(struct workspace *ws, int id)
1579 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1582 case SWM_ARG_ID_STACKRESET:
1583 case SWM_ARG_ID_STACKINIT:
1584 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1585 ws->l_state.vertical_mwin = 1;
1586 ws->l_state.vertical_stacks = 1;
1588 case SWM_ARG_ID_MASTERSHRINK:
1589 if (ws->l_state.vertical_msize > 1)
1590 ws->l_state.vertical_msize--;
1592 case SWM_ARG_ID_MASTERGROW:
1593 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1594 ws->l_state.vertical_msize++;
1596 case SWM_ARG_ID_MASTERADD:
1597 ws->l_state.vertical_mwin++;
1599 case SWM_ARG_ID_MASTERDEL:
1600 if (ws->l_state.vertical_mwin > 0)
1601 ws->l_state.vertical_mwin--;
1603 case SWM_ARG_ID_STACKINC:
1604 ws->l_state.vertical_stacks++;
1606 case SWM_ARG_ID_STACKDEC:
1607 if (ws->l_state.vertical_stacks > 1)
1608 ws->l_state.vertical_stacks--;
1616 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1618 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1620 stack_master(ws, g, 0, 0);
1624 horizontal_config(struct workspace *ws, int id)
1626 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1629 case SWM_ARG_ID_STACKRESET:
1630 case SWM_ARG_ID_STACKINIT:
1631 ws->l_state.horizontal_mwin = 1;
1632 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1633 ws->l_state.horizontal_stacks = 1;
1635 case SWM_ARG_ID_MASTERSHRINK:
1636 if (ws->l_state.horizontal_msize > 1)
1637 ws->l_state.horizontal_msize--;
1639 case SWM_ARG_ID_MASTERGROW:
1640 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1641 ws->l_state.horizontal_msize++;
1643 case SWM_ARG_ID_MASTERADD:
1644 ws->l_state.horizontal_mwin++;
1646 case SWM_ARG_ID_MASTERDEL:
1647 if (ws->l_state.horizontal_mwin > 0)
1648 ws->l_state.horizontal_mwin--;
1650 case SWM_ARG_ID_STACKINC:
1651 ws->l_state.horizontal_stacks++;
1653 case SWM_ARG_ID_STACKDEC:
1654 if (ws->l_state.horizontal_stacks > 1)
1655 ws->l_state.horizontal_stacks--;
1663 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1665 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1667 stack_master(ws, g, 1, 0);
1670 /* fullscreen view */
1672 max_stack(struct workspace *ws, struct swm_geometry *g) {
1674 struct swm_geometry gg = *g;
1675 struct ws_win *win, *winfocus;
1678 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1680 if (count_win(ws, 0) == 0)
1683 if (ws->focus == NULL)
1684 ws->focus = TAILQ_FIRST(&ws->winlist);
1685 winfocus = cur_focus ? cur_focus : ws->focus;
1687 TAILQ_FOREACH(win, &ws->winlist, entry) {
1688 if (win->transient != 0 || win->floating != 0) {
1689 if (win == ws->focus) {
1691 stack_floater(win, ws->r);
1692 XMapRaised(display, win->id);
1694 XUnmapWindow(display, win->id);
1696 bzero(&wc, sizeof wc);
1697 wc.border_width = 1;
1698 win->g.x = wc.x = gg.x;
1699 win->g.y = wc.y = gg.y;
1700 win->g.w = wc.width = gg.w;
1701 win->g.h = wc.height = gg.h;
1702 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1703 XConfigureWindow(display, win->id, mask, &wc);
1705 if (win == ws->focus) {
1706 XMapRaised(display, win->id);
1708 XUnmapWindow(display, win->id);
1713 focus_win(winfocus); /* has to be done outside of the loop */
1717 send_to_ws(struct swm_region *r, union arg *args)
1719 int wsid = args->id;
1720 struct ws_win *win = cur_focus;
1721 struct workspace *ws, *nws;
1722 Atom ws_idx_atom = 0;
1723 unsigned char ws_idx_str[SWM_PROPLEN];
1728 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1731 nws = &win->s->ws[wsid];
1733 XUnmapWindow(display, win->id);
1735 /* find a window to focus */
1736 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1737 if (ws->focus == NULL)
1738 ws->focus = TAILQ_FIRST(&ws->winlist);
1739 if (ws->focus == win)
1742 TAILQ_REMOVE(&ws->winlist, win, entry);
1744 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1747 /* Try to update the window's workspace property */
1748 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1750 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1751 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1753 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1754 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1757 if (count_win(nws, 1) == 1)
1766 wkill(struct swm_region *r, union arg *args)
1768 DNPRINTF(SWM_D_MISC, "wkill\n");
1769 if(r->ws->focus != NULL)
1770 XKillClient(display, r->ws->focus->id);
1774 screenshot(struct swm_region *r, union arg *args)
1778 DNPRINTF(SWM_D_MISC, "screenshot\n");
1780 if (ss_enabled == 0)
1784 case SWM_ARG_ID_SS_ALL:
1785 spawn_screenshot[1] = "full";
1787 case SWM_ARG_ID_SS_WINDOW:
1788 spawn_screenshot[1] = "window";
1793 a.argv = spawn_screenshot;
1798 floating_toggle(struct swm_region *r, union arg *args)
1800 struct ws_win *win = cur_focus;
1805 win->floating = !win->floating;
1811 /* key definitions */
1815 void (*func)(struct swm_region *r, union arg *);
1818 /* modifier key function argument */
1819 { MODKEY, XK_space, cycle_layout, {0} },
1820 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1821 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1822 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1823 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1824 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1825 { MODKEY | ShiftMask, XK_comma, stack_config, {.id = SWM_ARG_ID_STACKINC} },
1826 { MODKEY | ShiftMask, XK_period, stack_config, {.id = SWM_ARG_ID_STACKDEC} },
1827 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1828 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1829 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1830 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1831 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1832 { MODKEY | ShiftMask, XK_Return, spawnterm, {.argv = spawn_term} },
1833 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1834 { MODKEY | ShiftMask, XK_q, quit, {0} },
1835 { MODKEY, XK_q, restart, {0} },
1836 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1837 { MODKEY, XK_1, switchws, {.id = 0} },
1838 { MODKEY, XK_2, switchws, {.id = 1} },
1839 { MODKEY, XK_3, switchws, {.id = 2} },
1840 { MODKEY, XK_4, switchws, {.id = 3} },
1841 { MODKEY, XK_5, switchws, {.id = 4} },
1842 { MODKEY, XK_6, switchws, {.id = 5} },
1843 { MODKEY, XK_7, switchws, {.id = 6} },
1844 { MODKEY, XK_8, switchws, {.id = 7} },
1845 { MODKEY, XK_9, switchws, {.id = 8} },
1846 { MODKEY, XK_0, switchws, {.id = 9} },
1847 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1848 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1849 { MODKEY | ShiftMask, XK_Right, cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
1850 { MODKEY | ShiftMask, XK_Left, cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
1851 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1852 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1853 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1854 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1855 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1856 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1857 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1858 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1859 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1860 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1861 { MODKEY, XK_b, bar_toggle, {0} },
1862 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1863 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1864 { MODKEY | ShiftMask, XK_x, wkill, {0} },
1865 { MODKEY, XK_s, screenshot, {.id = SWM_ARG_ID_SS_ALL} },
1866 { MODKEY | ShiftMask, XK_s, screenshot, {.id = SWM_ARG_ID_SS_WINDOW} },
1867 { MODKEY, XK_t, floating_toggle,{0} },
1868 { MODKEY | ShiftMask, XK_v, version, {0} },
1869 { MODKEY | ShiftMask, XK_Delete, spawn, {.argv = spawn_lock} },
1870 { MODKEY | ShiftMask, XK_i, spawn, {.argv = spawn_initscr} },
1874 resize_window(struct ws_win *win, int center)
1878 struct swm_region *r;
1880 r = root_to_region(win->wa.root);
1881 bzero(&wc, sizeof wc);
1882 mask = CWBorderWidth | CWWidth | CWHeight;
1883 wc.border_width = 1;
1884 wc.width = win->g.w;
1885 wc.height = win->g.h;
1886 if (center == SWM_ARG_ID_CENTER) {
1887 wc.x = (WIDTH(r) - win->g.w) / 2;
1888 wc.y = (HEIGHT(r) - win->g.h) / 2;
1892 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
1893 win->id, wc.x, wc.y, wc.width, wc.height);
1895 XConfigureWindow(display, win->id, mask, &wc);
1900 resize(struct ws_win *win, union arg *args)
1905 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %d\n",
1906 win->id, win->floating, win->transient);
1908 if (!(win->transient != 0 || win->floating != 0))
1911 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1912 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1914 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
1916 XMaskEvent(display, MOUSEMASK | ExposureMask |
1917 SubstructureRedirectMask, &ev);
1919 case ConfigureRequest:
1922 handler[ev.type](&ev);
1925 if (ev.xmotion.x < 0)
1927 if (ev.xmotion.y < 0)
1929 win->g.w = ev.xmotion.x;
1930 win->g.h = ev.xmotion.y;
1932 /* not free, don't sync more than 60 times / second */
1933 if ((ev.xmotion.time - time) > (1000 / 60) ) {
1934 time = ev.xmotion.time;
1935 XSync(display, False);
1936 resize_window(win, args->id);
1940 } while (ev.type != ButtonRelease);
1942 XSync(display, False);
1943 resize_window(win, args->id);
1945 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
1947 XUngrabPointer(display, CurrentTime);
1950 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1954 move_window(struct ws_win *win)
1958 struct swm_region *r;
1960 r = root_to_region(win->wa.root);
1961 bzero(&wc, sizeof wc);
1966 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
1967 win->id, wc.x, wc.y, wc.width, wc.height);
1969 XConfigureWindow(display, win->id, mask, &wc);
1974 move(struct ws_win *win, union arg *args)
1980 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %d\n",
1981 win->id, win->floating, win->transient);
1983 if (win->floating == 0) {
1989 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1990 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1992 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
1994 XMaskEvent(display, MOUSEMASK | ExposureMask |
1995 SubstructureRedirectMask, &ev);
1997 case ConfigureRequest:
2000 handler[ev.type](&ev);
2003 win->g.x = ev.xmotion.x_root;
2004 win->g.y = ev.xmotion.y_root;
2006 /* not free, don't sync more than 60 times / second */
2007 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2008 time = ev.xmotion.time;
2009 XSync(display, False);
2014 } while (ev.type != ButtonRelease);
2016 XSync(display, False);
2019 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2020 XUngrabPointer(display, CurrentTime);
2025 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2029 enum { client_click, root_click };
2031 unsigned int action;
2033 unsigned int button;
2034 void (*func)(struct ws_win *, union arg *);
2037 /* action key mouse button func args */
2038 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2039 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2040 { client_click, MODKEY, Button1, move, {0} },
2044 updatenumlockmask(void)
2047 XModifierKeymap *modmap;
2049 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
2051 modmap = XGetModifierMapping(display);
2052 for (i = 0; i < 8; i++)
2053 for (j = 0; j < modmap->max_keypermod; j++)
2054 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2055 == XKeysymToKeycode(display, XK_Num_Lock))
2056 numlockmask = (1 << i);
2058 XFreeModifiermap(modmap);
2064 unsigned int i, j, k;
2066 unsigned int modifiers[] =
2067 { 0, LockMask, numlockmask, numlockmask | LockMask };
2069 DNPRINTF(SWM_D_MISC, "grabkeys\n");
2070 updatenumlockmask();
2072 for (k = 0; k < ScreenCount(display); k++) {
2073 if (TAILQ_EMPTY(&screens[k].rl))
2075 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
2076 for (i = 0; i < LENGTH(keys); i++) {
2077 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
2078 for (j = 0; j < LENGTH(modifiers); j++)
2079 XGrabKey(display, code,
2080 keys[i].mod | modifiers[j],
2081 screens[k].root, True,
2082 GrabModeAsync, GrabModeAsync);
2088 grabbuttons(struct ws_win *win, int focused)
2091 unsigned int modifiers[] =
2092 { 0, LockMask, numlockmask, numlockmask|LockMask };
2094 updatenumlockmask();
2095 XUngrabButton(display, AnyButton, AnyModifier, win->id);
2097 for (i = 0; i < LENGTH(buttons); i++)
2098 if (buttons[i].action == client_click)
2099 for (j = 0; j < LENGTH(modifiers); j++)
2100 XGrabButton(display, buttons[i].button,
2101 buttons[i].mask | modifiers[j],
2102 win->id, False, BUTTONMASK,
2103 GrabModeAsync, GrabModeSync, None,
2106 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
2107 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
2113 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
2121 XKeyEvent *ev = &e->xkey;
2123 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
2125 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
2126 for (i = 0; i < LENGTH(keys); i++)
2127 if (keysym == keys[i].keysym
2128 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
2130 keys[i].func(root_to_region(ev->root),
2135 buttonpress(XEvent *e)
2137 XButtonPressedEvent *ev = &e->xbutton;
2142 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
2144 action = root_click;
2145 if ((win = find_window(ev->window)) == NULL)
2149 action = client_click;
2152 for (i = 0; i < LENGTH(buttons); i++)
2153 if (action == buttons[i].action && buttons[i].func &&
2154 buttons[i].button == ev->button &&
2155 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
2156 buttons[i].func(win, &buttons[i].args);
2160 set_win_state(struct ws_win *win, long state)
2162 long data[] = {state, None};
2164 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
2166 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
2167 (unsigned char *)data, 2);
2171 manage_window(Window id)
2174 struct workspace *ws;
2176 int format, i, ws_idx;
2177 unsigned long nitems, bytes;
2178 Atom ws_idx_atom = 0, type;
2179 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
2180 struct swm_region *r;
2185 if ((win = find_window(id)) != NULL)
2186 return (win); /* already being managed */
2188 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
2189 errx(1, "calloc: failed to allocate memory for new window");
2191 /* Get all the window data in one shot */
2192 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2194 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
2195 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
2196 XGetWindowAttributes(display, id, &win->wa);
2197 XGetTransientForHint(display, id, &trans);
2198 XGetWMNormalHints(display, id, &win->sh, &mask); /* XXX function? */
2200 win->transient = trans;
2201 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
2202 (unsigned)win->id, win->transient);
2206 * Figure out where to put the window. If it was previously assigned to
2207 * a workspace (either by spawn() or manually moving), and isn't
2208 * transient, * put it in the same workspace
2210 r = root_to_region(win->wa.root);
2211 if (prop && win->transient == 0) {
2212 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
2213 ws_idx = strtonum(prop, 0, 9, &errstr);
2215 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
2218 ws = &r->s->ws[ws_idx];
2222 /* set up the window layout */
2225 win->s = r->s; /* this never changes */
2226 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
2228 win->g.w = win->wa.width;
2229 win->g.h = win->wa.height;
2230 win->g.x = win->wa.x;
2231 win->g.y = win->wa.y;
2233 /* Set window properties so we can remember this after reincarnation */
2234 if (ws_idx_atom && prop == NULL &&
2235 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
2236 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2238 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2239 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2243 if (XGetClassHint(display, win->id, &win->ch)) {
2244 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
2245 win->ch.res_class, win->ch.res_name);
2246 for (i = 0; quirks[i].class != NULL && quirks[i].name != NULL &&
2247 quirks[i].quirk != 0; i++){
2248 if (!strcmp(win->ch.res_class, quirks[i].class) &&
2249 !strcmp(win->ch.res_name, quirks[i].name)) {
2250 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
2251 win->ch.res_class, win->ch.res_name);
2252 if (quirks[i].quirk & SWM_Q_FLOAT)
2254 win->quirks = quirks[i].quirk;
2259 /* alter window position if quirky */
2260 if (win->quirks & SWM_Q_ANYWHERE) {
2261 win->manual = 1; /* don't center the quirky windows */
2262 bzero(&wc, sizeof wc);
2264 if (win->g.y < bar_height) {
2265 win->g.y = wc.y = bar_height;
2268 if (win->g.w + win->g.x > WIDTH(r)) {
2269 win->g.x = wc.x = WIDTH(win->ws->r) - win->g.w - 2;
2272 wc.border_width = 1;
2273 mask |= CWBorderWidth;
2274 XConfigureWindow(display, win->id, mask, &wc);
2277 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
2278 PropertyChangeMask | StructureNotifyMask);
2280 set_win_state(win, NormalState);
2282 /* floaters need to be mapped if they are in the current workspace */
2283 if (win->floating && (ws->idx == r->ws->idx))
2284 XMapRaised(display, win->id);
2286 /* make new win focused */
2293 unmanage_window(struct ws_win *win)
2295 struct workspace *ws;
2300 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
2302 /* don't unmanage if we are switching workspaces */
2307 /* find a window to focus */
2308 if (ws->focus == win)
2309 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
2310 if (ws->focus == NULL)
2311 ws->focus = TAILQ_FIRST(&ws->winlist);
2312 if (ws->focus == NULL || ws->focus == win) {
2316 focus_win(ws->focus);
2317 if (ws->focus_prev == win)
2318 ws->focus_prev = NULL;
2320 TAILQ_REMOVE(&win->ws->winlist, win, entry);
2321 set_win_state(win, WithdrawnState);
2322 if (win->ch.res_class)
2323 XFree(win->ch.res_class);
2324 if (win->ch.res_name)
2325 XFree(win->ch.res_name);
2330 configurerequest(XEvent *e)
2332 XConfigureRequestEvent *ev = &e->xconfigurerequest;
2337 if ((win = find_window(ev->window)) == NULL)
2341 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
2343 bzero(&wc, sizeof wc);
2346 wc.width = ev->width;
2347 wc.height = ev->height;
2348 wc.border_width = ev->border_width;
2349 wc.sibling = ev->above;
2350 wc.stack_mode = ev->detail;
2351 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
2353 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
2355 if (win->floating) {
2356 if (ev->value_mask & CWX)
2358 if (ev->value_mask & CWY)
2360 if (ev->value_mask & CWWidth)
2361 win->g.w = ev->width;
2362 if (ev->value_mask & CWHeight)
2363 win->g.h = ev->height;
2364 if (win->ws->r != NULL) {
2365 /* this seems to be full screen */
2366 if (win->g.w >= WIDTH(win->ws->r)) {
2368 win->g.w = WIDTH(win->ws->r);
2369 ev->value_mask |= CWX | CWWidth;
2371 if (win->g.h >= HEIGHT(win->ws->r)) {
2374 win->g.h = HEIGHT(win->ws->r);
2375 ev->value_mask |= CWY | CWHeight;
2378 if ((ev->value_mask & (CWX | CWY)) &&
2379 !(ev->value_mask & (CWWidth | CWHeight)))
2381 XMoveResizeWindow(display, win->id,
2382 win->g.x, win->g.y, win->g.w, win->g.h);
2389 configurenotify(XEvent *e)
2394 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
2395 e->xconfigure.window);
2397 win = find_window(e->xconfigure.window);
2398 XMapWindow(display, win->id);
2399 XGetWMNormalHints(display, win->id, &win->sh, &mask);
2401 XMapWindow(display, win->id);
2407 destroynotify(XEvent *e)
2410 XDestroyWindowEvent *ev = &e->xdestroywindow;
2412 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
2414 if ((win = find_window(ev->window)) != NULL) {
2415 unmanage_window(win);
2421 enternotify(XEvent *e)
2423 XCrossingEvent *ev = &e->xcrossing;
2426 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
2429 /* eat event(r) to prevent autofocus */
2434 if ((win = find_window(ev->window)) != NULL)
2441 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
2445 mappingnotify(XEvent *e)
2447 XMappingEvent *ev = &e->xmapping;
2449 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
2451 XRefreshKeyboardMapping(ev);
2452 if (ev->request == MappingKeyboard)
2457 maprequest(XEvent *e)
2459 XMapRequestEvent *ev = &e->xmaprequest;
2460 XWindowAttributes wa;
2462 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
2463 e->xmaprequest.window);
2465 if (!XGetWindowAttributes(display, ev->window, &wa))
2467 if (wa.override_redirect)
2469 manage_window(e->xmaprequest.window);
2475 propertynotify(XEvent *e)
2478 XPropertyEvent *ev = &e->xproperty;
2480 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
2483 if (ev->state == PropertyDelete)
2484 return; /* ignore */
2485 win = find_window(ev->window);
2490 case XA_WM_NORMAL_HINTS:
2493 XGetWMNormalHints(display, win->id, &win->sh, &mask);
2494 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
2495 if (win->sh.flags & PMinSize) {
2496 win->g.w = win->sh.min_width;
2497 win->g.h = win->sh.min_height;
2498 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
2500 XMoveResizeWindow(display, win->id,
2501 win->g.x, win->g.y, win->g.w, win->g.h);
2510 unmapnotify(XEvent *e)
2512 XDestroyWindowEvent *ev = &e->xdestroywindow;
2515 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
2517 if ((win = find_window(ev->window)) != NULL)
2519 unmanage_window(win);
2523 visibilitynotify(XEvent *e)
2526 struct swm_region *r;
2528 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
2529 e->xvisibility.window);
2530 if (e->xvisibility.state == VisibilityUnobscured)
2531 for (i = 0; i < ScreenCount(display); i++)
2532 TAILQ_FOREACH(r, &screens[i].rl, entry)
2533 if (e->xvisibility.window == r->bar_window)
2538 xerror_start(Display *d, XErrorEvent *ee)
2545 xerror(Display *d, XErrorEvent *ee)
2547 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2555 xerrorxlib = XSetErrorHandler(xerror_start);
2557 /* this causes an error if some other window manager is running */
2558 XSelectInput(display, DefaultRootWindow(display),
2559 SubstructureRedirectMask);
2560 XSync(display, False);
2564 XSetErrorHandler(xerror);
2565 XSync(display, False);
2574 unsigned char *p = NULL;
2575 unsigned long n, extra;
2578 astate = XInternAtom(display, "WM_STATE", False);
2579 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
2580 &real, &format, &n, &extra, (unsigned char **)&p);
2581 if (status != Success)
2590 new_region(struct swm_screen *s, int x, int y, int w, int h)
2592 struct swm_region *r, *n;
2593 struct workspace *ws = NULL;
2596 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
2597 s->idx, w, h, x, y);
2599 /* remove any conflicting regions */
2600 n = TAILQ_FIRST(&s->rl);
2603 n = TAILQ_NEXT(r, entry);
2604 if (X(r) < (x + w) &&
2605 (X(r) + WIDTH(r)) > x &&
2607 (Y(r) + HEIGHT(r)) > y) {
2608 XDestroyWindow(display, r->bar_window);
2609 TAILQ_REMOVE(&s->rl, r, entry);
2610 TAILQ_INSERT_TAIL(&s->orl, r, entry);
2614 /* search old regions for one to reuse */
2616 /* size + location match */
2617 TAILQ_FOREACH(r, &s->orl, entry)
2618 if (X(r) == x && Y(r) == y &&
2619 HEIGHT(r) == h && WIDTH(r) == w)
2623 TAILQ_FOREACH(r, &s->orl, entry)
2624 if (HEIGHT(r) == h && WIDTH(r) == w)
2628 TAILQ_REMOVE(&s->orl, r, entry);
2629 /* try to use old region's workspace */
2630 if (r->ws->r == NULL)
2633 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
2634 errx(1, "calloc: failed to allocate memory for screen");
2636 /* if we don't have a workspace already, find one */
2638 for (i = 0; i < SWM_WS_MAX; i++)
2639 if (s->ws[i].r == NULL) {
2646 errx(1, "no free workspaces\n");
2655 TAILQ_INSERT_TAIL(&s->rl, r, entry);
2662 #ifdef SWM_XRR_HAS_CRTC
2664 XRRScreenResources *sr;
2667 #endif /* SWM_XRR_HAS_CRTC */
2668 struct swm_region *r;
2671 if (i >= ScreenCount(display))
2672 errx(1, "invalid screen");
2674 /* remove any old regions */
2675 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
2677 XDestroyWindow(display, r->bar_window);
2678 TAILQ_REMOVE(&screens[i].rl, r, entry);
2679 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
2682 /* map virtual screens onto physical screens */
2683 #ifdef SWM_XRR_HAS_CRTC
2684 if (xrandr_support) {
2685 sr = XRRGetScreenResources(display, screens[i].root);
2687 new_region(&screens[i], 0, 0,
2688 DisplayWidth(display, i),
2689 DisplayHeight(display, i));
2693 for (c = 0, ci = NULL; c < ncrtc; c++) {
2694 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
2695 if (ci->noutput == 0)
2698 if (ci != NULL && ci->mode == None)
2699 new_region(&screens[i], 0, 0,
2700 DisplayWidth(display, i),
2701 DisplayHeight(display, i));
2703 new_region(&screens[i],
2704 ci->x, ci->y, ci->width, ci->height);
2707 XRRFreeCrtcInfo(ci);
2708 XRRFreeScreenResources(sr);
2710 #endif /* SWM_XRR_HAS_CRTC */
2712 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
2713 DisplayHeight(display, i));
2718 screenchange(XEvent *e) {
2719 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
2720 struct swm_region *r;
2724 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
2726 if (!XRRUpdateConfiguration(e))
2729 /* silly event doesn't include the screen index */
2730 for (i = 0; i < ScreenCount(display); i++)
2731 if (screens[i].root == xe->root)
2733 if (i >= ScreenCount(display))
2734 errx(1, "screenchange: screen not found\n");
2736 /* brute force for now, just re-enumerate the regions */
2739 /* hide any windows that went away */
2740 TAILQ_FOREACH(r, &screens[i].rl, entry)
2741 TAILQ_FOREACH(win, &r->ws->winlist, entry)
2742 XUnmapWindow(display, win->id);
2749 Window d1, d2, *wins = NULL;
2750 XWindowAttributes wa;
2753 int errorbase, major, minor;
2754 struct workspace *ws;
2758 if ((screens = calloc(ScreenCount(display),
2759 sizeof(struct swm_screen))) == NULL)
2760 errx(1, "calloc: screens");
2762 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2764 /* initial Xrandr setup */
2765 xrandr_support = XRRQueryExtension(display,
2766 &xrandr_eventbase, &errorbase);
2768 if (XRRQueryVersion(display, &major, &minor) && major < 1)
2771 /* map physical screens */
2772 for (i = 0; i < ScreenCount(display); i++) {
2773 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
2775 TAILQ_INIT(&screens[i].rl);
2776 TAILQ_INIT(&screens[i].orl);
2777 screens[i].root = RootWindow(display, i);
2779 /* set default colors */
2780 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
2781 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
2782 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
2783 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
2784 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
2786 /* init all workspaces */
2787 /* XXX these should be dynamically allocated too */
2788 for (j = 0; j < SWM_WS_MAX; j++) {
2789 ws = &screens[i].ws[j];
2794 TAILQ_INIT(&ws->winlist);
2796 for (k = 0; layouts[k].l_stack != NULL; k++)
2797 if (layouts[k].l_config != NULL)
2798 layouts[k].l_config(ws,
2799 SWM_ARG_ID_STACKINIT);
2800 ws->cur_layout = &layouts[0];
2802 /* grab existing windows (before we build the bars)*/
2803 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
2809 XRRSelectInput(display, screens[i].root,
2810 RRScreenChangeNotifyMask);
2812 /* attach windows to a region */
2813 /* normal windows */
2814 for (j = 0; j < no; j++) {
2815 XGetWindowAttributes(display, wins[j], &wa);
2816 if (!XGetWindowAttributes(display, wins[j], &wa) ||
2817 wa.override_redirect ||
2818 XGetTransientForHint(display, wins[j], &d1))
2821 if (wa.map_state == IsViewable ||
2822 getstate(wins[j]) == NormalState)
2823 manage_window(wins[j]);
2825 /* transient windows */
2826 for (j = 0; j < no; j++) {
2827 if (!XGetWindowAttributes(display, wins[j], &wa))
2830 if (XGetTransientForHint(display, wins[j], &d1) &&
2831 (wa.map_state == IsViewable || getstate(wins[j]) ==
2833 manage_window(wins[j]);
2843 main(int argc, char *argv[])
2846 char conf[PATH_MAX], *cfile = NULL;
2853 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
2854 SWM_VERSION, cvstag);
2855 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2856 warnx("no locale support");
2858 if (!(display = XOpenDisplay(0)))
2859 errx(1, "can not open display");
2862 errx(1, "other wm running");
2864 astate = XInternAtom(display, "WM_STATE", False);
2866 /* look for local and global conf file */
2867 pwd = getpwuid(getuid());
2869 errx(1, "invalid user %d", getuid());
2873 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2874 if (stat(conf, &sb) != -1) {
2875 if (S_ISREG(sb.st_mode))
2878 /* try global conf file */
2879 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2880 if (!stat(conf, &sb))
2881 if (S_ISREG(sb.st_mode))
2888 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2893 xfd = ConnectionNumber(display);
2897 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2899 errx(1, "select failed");
2904 while (XPending(display)) {
2905 XNextEvent(display, &e);
2906 if (e.type < LASTEvent) {
2907 if (handler[e.type])
2908 handler[e.type](&e);
2910 DNPRINTF(SWM_D_EVENT,
2911 "win: %lu unknown event: %d\n",
2912 e.xany.window, e.type);
2914 switch (e.type - xrandr_eventbase) {
2915 case RRScreenChangeNotify:
2919 DNPRINTF(SWM_D_EVENT,
2920 "win: %lu unknown xrandr event: "
2921 "%d\n", e.xany.window, e.type);
2928 XCloseDisplay(display);