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.7"
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
137 #define SWM_LIB "/usr/X11R6/lib/swmhack.so"
142 int (*xerrorxlib)(Display *, XErrorEvent *);
147 int xrandr_eventbase;
148 int ignore_enter = 0;
149 unsigned int numlockmask = 0;
153 int cycle_visible = 0;
156 double dialog_ratio = .6;
158 #define SWM_BAR_MAX (128)
159 char *bar_argv[] = { NULL, NULL };
161 char bar_ext[SWM_BAR_MAX];
162 char bar_vertext[SWM_BAR_MAX];
164 sig_atomic_t bar_alarm = 0;
168 int bar_extra_running = 0;
176 char *bar_fonts[] = {
177 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
178 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
182 /* terminal + args */
183 char *spawn_term[] = { "xterm", NULL };
184 char *spawn_screenshot[] = { "screenshot.sh", NULL, NULL };
185 char *spawn_lock[] = { "xlock", NULL };
186 char *spawn_initscr[] = { "initscreen.sh", NULL };
187 char *spawn_menu[] = { "dmenu_run", "-fn", NULL, "-nb", NULL,
188 "-nf", NULL, "-sb", NULL, "-sf", NULL, NULL };
190 #define SWM_MENU_FN (2)
191 #define SWM_MENU_NB (4)
192 #define SWM_MENU_NF (6)
193 #define SWM_MENU_SB (8)
194 #define SWM_MENU_SF (10)
196 /* layout manager data */
197 struct swm_geometry {
207 /* virtual "screens" */
209 TAILQ_ENTRY(swm_region) entry;
210 struct swm_geometry g;
211 struct workspace *ws; /* current workspace on this region */
212 struct swm_screen *s; /* screen idx */
215 TAILQ_HEAD(swm_region_list, swm_region);
218 TAILQ_ENTRY(ws_win) entry;
220 struct swm_geometry g;
225 unsigned long quirks;
226 struct workspace *ws; /* always valid */
227 struct swm_screen *s; /* always valid, never changes */
228 XWindowAttributes wa;
232 TAILQ_HEAD(ws_win_list, ws_win);
234 /* layout handlers */
236 void vertical_config(struct workspace *, int);
237 void vertical_stack(struct workspace *, struct swm_geometry *);
238 void horizontal_config(struct workspace *, int);
239 void horizontal_stack(struct workspace *, struct swm_geometry *);
240 void max_stack(struct workspace *, struct swm_geometry *);
242 void grabbuttons(struct ws_win *, int);
245 void (*l_stack)(struct workspace *, struct swm_geometry *);
246 void (*l_config)(struct workspace *, int);
248 /* stack, configure */
249 { vertical_stack, vertical_config},
250 { horizontal_stack, horizontal_config},
255 #define SWM_H_SLICE (32)
256 #define SWM_V_SLICE (32)
258 /* define work spaces */
260 int idx; /* workspace index */
261 int restack; /* restack on switch */
262 struct layout *cur_layout; /* current layout handlers */
263 struct ws_win *focus; /* may be NULL */
264 struct swm_region *r; /* may be NULL */
265 struct ws_win_list winlist; /* list of windows in ws */
269 int horizontal_msize;
271 int horizontal_stacks;
278 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
279 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
281 /* physical screen mapping */
282 #define SWM_WS_MAX (10) /* XXX Too small? */
284 int idx; /* screen index */
285 struct swm_region_list rl; /* list of regions on this screen */
286 struct swm_region_list orl; /* list of old regions */
288 struct workspace ws[SWM_WS_MAX];
294 } c[SWM_S_COLOR_MAX];
296 struct swm_screen *screens;
299 struct ws_win *cur_focus = NULL;
301 /* args to functions */
304 #define SWM_ARG_ID_FOCUSNEXT (0)
305 #define SWM_ARG_ID_FOCUSPREV (1)
306 #define SWM_ARG_ID_FOCUSMAIN (2)
307 #define SWM_ARG_ID_SWAPNEXT (3)
308 #define SWM_ARG_ID_SWAPPREV (4)
309 #define SWM_ARG_ID_SWAPMAIN (5)
310 #define SWM_ARG_ID_MASTERSHRINK (6)
311 #define SWM_ARG_ID_MASTERGROW (7)
312 #define SWM_ARG_ID_MASTERADD (8)
313 #define SWM_ARG_ID_MASTERDEL (9)
314 #define SWM_ARG_ID_STACKRESET (10)
315 #define SWM_ARG_ID_STACKINIT (11)
316 #define SWM_ARG_ID_CYCLEWS_UP (12)
317 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
318 #define SWM_ARG_ID_CYCLESC_UP (14)
319 #define SWM_ARG_ID_CYCLESC_DOWN (15)
320 #define SWM_ARG_ID_COLINC (16)
321 #define SWM_ARG_ID_COLDEC (17)
322 #define SWM_ARG_ID_ROWINC (16)
323 #define SWM_ARG_ID_ROWDEL (17)
324 #define SWM_ARG_ID_SS_ALL (0)
325 #define SWM_ARG_ID_SS_WINDOW (1)
326 #define SWM_ARG_ID_DONTCENTER (0)
327 #define SWM_ARG_ID_CENTER (1)
336 #define SWM_Q_FLOAT (1<<0) /* float this window */
337 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
338 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
340 { "MPlayer", "xv", SWM_Q_FLOAT },
341 { "OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT },
342 { "OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT },
343 { "Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ},
344 { "Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE},
349 void expose(XEvent *);
350 void keypress(XEvent *);
351 void buttonpress(XEvent *);
352 void configurerequest(XEvent *);
353 void configurenotify(XEvent *);
354 void destroynotify(XEvent *);
355 void enternotify(XEvent *);
356 void focusin(XEvent *);
357 void mappingnotify(XEvent *);
358 void maprequest(XEvent *);
359 void propertynotify(XEvent *);
360 void unmapnotify(XEvent *);
361 void visibilitynotify(XEvent *);
363 void (*handler[LASTEvent])(XEvent *) = {
365 [KeyPress] = keypress,
366 [ButtonPress] = buttonpress,
367 [ConfigureRequest] = configurerequest,
368 [ConfigureNotify] = configurenotify,
369 [DestroyNotify] = destroynotify,
370 [EnterNotify] = enternotify,
372 [MappingNotify] = mappingnotify,
373 [MapRequest] = maprequest,
374 [PropertyNotify] = propertynotify,
375 [UnmapNotify] = unmapnotify,
376 [VisibilityNotify] = visibilitynotify,
380 name_to_color(char *colorname)
384 XColor screen_def, exact_def;
385 unsigned long result = 0;
386 char cname[32] = "#";
388 cmap = DefaultColormap(display, screens[0].idx);
389 status = XAllocNamedColor(display, cmap, colorname,
390 &screen_def, &exact_def);
392 strlcat(cname, colorname + 2, sizeof cname - 1);
393 status = XAllocNamedColor(display, cmap, cname, &screen_def,
397 result = screen_def.pixel;
399 fprintf(stderr, "color '%s' not found.\n", colorname);
405 setscreencolor(char *val, int i, int c)
407 if (i > 0 && i <= ScreenCount(display)) {
408 screens[i - 1].c[c].color = name_to_color(val);
409 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
411 } else if (i == -1) {
412 for (i = 0; i < ScreenCount(display); i++)
413 screens[i].c[c].color = name_to_color(val);
414 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
417 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
418 i, ScreenCount(display));
421 void new_region(struct swm_screen *, int, int, int, int);
424 custom_region(char *val)
426 unsigned int sidx, x, y, w, h;
428 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
429 errx(1, "invalid custom region, "
430 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
431 if (sidx < 1 || sidx > ScreenCount(display))
432 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
433 sidx, ScreenCount(display));
437 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
439 if (x < 0 || x > DisplayWidth(display, sidx) ||
440 y < 0 || y > DisplayHeight(display, sidx) ||
441 w + x > DisplayWidth(display, sidx) ||
442 h + y > DisplayHeight(display, sidx))
443 errx(1, "region %ux%u+%u+%u not within screen boundaries "
444 "(%ux%u)\n", w, h, x, y,
445 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
447 new_region(&screens[sidx], x, y, w, h);
451 varmatch(char *var, char *name, int *index)
456 i = strncmp(var, name, 255);
463 p = var + strlen(name);
466 bzero(buf, sizeof buf);
468 while (isdigit(*p) && i < sizeof buf)
470 if (i == 0 || i >= sizeof buf || *p != ']')
472 *index = strtonum(buf, 0, 99, NULL);
476 /* conf file stuff */
477 #define SWM_CONF_WS "\n= \t"
478 #define SWM_CONF_FILE "scrotwm.conf"
480 conf_load(char *filename)
483 char *line, *cp, *var, *val;
484 size_t len, lineno = 0;
487 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
489 if (filename == NULL)
492 if ((config = fopen(filename, "r")) == NULL)
495 for (sc = ScreenCount(display);;) {
496 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
500 cp += (long)strspn(cp, SWM_CONF_WS);
506 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
508 cp += (long)strspn(cp, SWM_CONF_WS);
509 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
512 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
515 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
516 bar_enabled = atoi(val);
517 else if (!varmatch(var, "bar_border", &i))
518 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
519 else if (!varmatch(var, "bar_color", &i))
520 setscreencolor(val, i, SWM_S_COLOR_BAR);
521 else if (!varmatch(var, "bar_font_color", &i))
522 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
523 else if (!strncmp(var, "bar_font", strlen("bar_font")))
524 asprintf(&bar_fonts[0], "%s", val);
525 else if (!strncmp(var, "bar_action", strlen("bar_action")))
526 asprintf(&bar_argv[0], "%s", val);
527 else if (!strncmp(var, "bar_delay", strlen("bar_delay")))
528 bar_delay = atoi(val);
534 if (!varmatch(var, "color_focus", &i))
535 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
536 else if (!varmatch(var, "color_unfocus", &i))
537 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
538 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
539 cycle_visible = atoi(val);
540 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
541 cycle_visible = atoi(val);
547 if (!strncmp(var, "dialog_ratio",
548 strlen("dialog_ratio"))) {
549 dialog_ratio = atof(val);
550 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
557 if (!strncmp(var, "region", strlen("region")))
564 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
565 asprintf(&spawn_term[0], "%s", val);
566 if (!strncmp(var, "screenshot_enabled",
567 strlen("screenshot_enabled")))
568 ss_enabled = atoi(val);
569 if (!strncmp(var, "screenshot_app",
570 strlen("screenshot_app")))
571 asprintf(&spawn_screenshot[0], "%s", val);
583 errx(1, "invalid conf file entry: %s=%s", var, val);
587 socket_setnonblock(int fd)
591 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
592 err(1, "fcntl F_GETFL");
594 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
595 err(1, "fcntl F_SETFL");
599 bar_print(struct swm_region *r, char *s)
601 XClearWindow(display, r->bar_window);
602 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
603 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
612 bzero(bar_pipe, sizeof bar_pipe);
615 kill(bar_pid, SIGTERM);
618 strlcpy(bar_ext, "", sizeof bar_ext);
627 struct swm_region *r;
631 char loc[SWM_BAR_MAX];
634 if (bar_enabled == 0)
637 if (bar_extra && bar_extra_running) {
638 /* ignore short reads; it'll correct itself */
639 while ((b = fgetln(stdin, &len)) != NULL)
640 if (b && b[len - 1] == '\n') {
642 strlcpy(bar_ext, b, sizeof bar_ext);
644 if (b == NULL && errno != EAGAIN) {
645 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
646 errno, strerror(errno));
650 strlcpy(bar_ext, "", sizeof bar_ext);
653 localtime_r(&tmt, &tm);
654 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
655 for (i = 0; i < ScreenCount(display); i++) {
657 TAILQ_FOREACH(r, &screens[i].rl, entry) {
658 snprintf(loc, sizeof loc, "%s %d:%d %s %s",
659 s, x++, r->ws->idx + 1, bar_ext, bar_vertext);
663 XSync(display, False);
674 bar_toggle(struct swm_region *r, union arg *args)
676 struct swm_region *tmpr;
677 int i, j, sc = ScreenCount(display);
679 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
682 for (i = 0; i < sc; i++)
683 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
684 XUnmapWindow(display, tmpr->bar_window);
686 for (i = 0; i < sc; i++)
687 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
688 XMapRaised(display, tmpr->bar_window);
690 bar_enabled = !bar_enabled;
691 XSync(display, False);
692 for (i = 0; i < sc; i++)
693 for (j = 0; j < SWM_WS_MAX; j++)
694 screens[i].ws[j].restack = 1;
697 /* must be after stack */
698 for (i = 0; i < sc; i++)
699 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
706 XSetWindowAttributes wa;
707 struct swm_region *r;
710 /* do this here because the conf file is in memory */
711 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
712 /* launch external status app */
713 bar_extra_running = 1;
714 if (pipe(bar_pipe) == -1)
715 err(1, "pipe error");
716 socket_setnonblock(bar_pipe[0]);
717 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
718 if (dup2(bar_pipe[0], 0) == -1)
720 if (dup2(bar_pipe[1], 1) == -1)
722 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
723 err(1, "could not disable SIGPIPE");
724 switch (bar_pid = fork()) {
726 err(1, "cannot fork");
730 execvp(bar_argv[0], bar_argv);
731 err(1, "%s external app failed", bar_argv[0]);
733 default: /* parent */
739 bzero(&wa, sizeof wa);
740 for (i = 0; i < ScreenCount(display); i++)
741 TAILQ_FOREACH(r, &screens[i].rl, entry) {
743 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
744 wa.background_pixel =
745 screens[i].c[SWM_S_COLOR_BAR].color;
746 XChangeWindowAttributes(display, r->bar_window,
747 CWBackPixel | CWBorderPixel, &wa);
753 bar_setup(struct swm_region *r)
757 for (i = 0; bar_fonts[i] != NULL; i++) {
758 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
764 if (bar_fonts[i] == NULL)
765 errx(1, "couldn't load font");
766 bar_height = bar_fs->ascent + bar_fs->descent + 3;
768 r->bar_window = XCreateSimpleWindow(display,
769 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
770 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
771 r->s->c[SWM_S_COLOR_BAR].color);
772 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
773 XSetFont(display, bar_gc, bar_fs->fid);
774 XSelectInput(display, r->bar_window, VisibilityChangeMask);
776 XMapRaised(display, r->bar_window);
777 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
779 if (signal(SIGALRM, bar_signal) == SIG_ERR)
780 err(1, "could not install bar_signal");
785 version(struct swm_region *r, union arg *args)
787 bar_version = !bar_version;
789 strlcpy(bar_vertext, cvstag, sizeof bar_vertext);
791 strlcpy(bar_vertext, "", sizeof bar_vertext);
796 config_win(struct ws_win *win)
800 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
801 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
802 ce.type = ConfigureNotify;
803 ce.display = display;
809 ce.height = win->g.h;
810 ce.border_width = 1; /* XXX store this! */
812 ce.override_redirect = False;
813 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
817 count_win(struct workspace *ws, int count_transient)
822 TAILQ_FOREACH(win, &ws->winlist, entry) {
823 if (count_transient == 0 && win->floating)
825 if (count_transient == 0 && win->transient)
829 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
835 quit(struct swm_region *r, union arg *args)
837 DNPRINTF(SWM_D_MISC, "quit\n");
847 for (i = 0; i < ScreenCount(display); i++)
848 for (j = 0; j < SWM_WS_MAX; j++)
849 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
850 XUnmapWindow(display, win->id);
854 restart(struct swm_region *r, union arg *args)
856 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
858 /* disable alarm because the following code may not be interrupted */
860 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
861 errx(1, "can't disable alarm");
866 XCloseDisplay(display);
867 execvp(start_argv[0], start_argv);
868 fprintf(stderr, "execvp failed\n");
874 root_to_region(Window root)
876 struct swm_region *r = NULL;
881 for (i = 0; i < ScreenCount(display); i++)
882 if (screens[i].root == root)
885 if (XQueryPointer(display, screens[i].root,
886 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
887 /* choose a region based on pointer location */
888 TAILQ_FOREACH(r, &screens[i].rl, entry)
889 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
890 y >= Y(r) && y <= Y(r) + HEIGHT(r))
895 r = TAILQ_FIRST(&screens[i].rl);
901 find_window(Window id)
906 for (i = 0; i < ScreenCount(display); i++)
907 for (j = 0; j < SWM_WS_MAX; j++)
908 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
915 spawn(struct swm_region *r, union arg *args)
920 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
922 * The double-fork construct avoids zombie processes and keeps the code
923 * clean from stupid signal handlers.
928 close(ConnectionNumber(display));
929 setenv("LD_PRELOAD", SWM_LIB, 1);
930 if (asprintf(&ret, "%d", r->ws->idx)) {
931 setenv("_SWM_WS", ret, 1);
934 if (asprintf(&ret, "%d", getpid())) {
935 setenv("_SWM_PID", ret, 1);
939 /* kill stdin, mplayer, ssh-add etc. need that */
940 si = open("/dev/null", O_RDONLY, 0);
942 err(1, "open /dev/null");
943 if (dup2(si, 0) == -1)
944 err(1, "dup2 /dev/null");
945 execvp(args->argv[0], args->argv);
946 fprintf(stderr, "execvp failed\n");
955 spawnmenu(struct swm_region *r, union arg *args)
957 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
959 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
960 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
961 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
962 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
963 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
974 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
976 for (i = 0; i < ScreenCount(display); i++)
977 for (j = 0; j < SWM_WS_MAX; j++)
978 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
979 if (win->ws->r == NULL)
982 XSetWindowBorder(display, win->id,
983 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
985 win->ws->focus = NULL;
991 focus_win(struct ws_win *win)
993 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
999 win->ws->focus = win;
1000 if (win->ws->r != NULL) {
1002 if (!win->got_focus) {
1003 XSetWindowBorder(display, win->id,
1004 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1005 grabbuttons(win, 1);
1008 XSetInputFocus(display, win->id,
1009 RevertToPointerRoot, CurrentTime);
1014 switchws(struct swm_region *r, union arg *args)
1016 int wsid = args->id;
1017 struct swm_region *this_r, *other_r;
1019 struct workspace *new_ws, *old_ws;
1022 old_ws = this_r->ws;
1023 new_ws = &this_r->s->ws[wsid];
1025 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1026 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1029 if (new_ws == old_ws)
1032 other_r = new_ws->r;
1034 /* if the other workspace is hidden, switch windows */
1035 /* map new window first to prevent ugly blinking */
1037 old_ws->restack = 1;
1039 TAILQ_FOREACH(win, &new_ws->winlist, entry)
1040 XMapRaised(display, win->id);
1042 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1043 XUnmapWindow(display, win->id);
1045 other_r->ws = old_ws;
1046 old_ws->r = other_r;
1048 this_r->ws = new_ws;
1054 focus_win(new_ws->focus);
1060 cyclews(struct swm_region *r, union arg *args)
1063 struct swm_screen *s = r->s;
1065 DNPRINTF(SWM_D_WS, "cyclews id %d "
1066 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1067 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1072 case SWM_ARG_ID_CYCLEWS_UP:
1073 if (a.id < SWM_WS_MAX - 1)
1078 case SWM_ARG_ID_CYCLEWS_DOWN:
1082 a.id = SWM_WS_MAX - 1;
1088 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1090 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1094 } while (a.id != r->ws->idx);
1098 cyclescr(struct swm_region *r, union arg *args)
1100 struct swm_region *rr;
1105 case SWM_ARG_ID_CYCLESC_UP:
1106 rr = TAILQ_NEXT(r, entry);
1108 rr = TAILQ_FIRST(&screens[i].rl);
1110 case SWM_ARG_ID_CYCLESC_DOWN:
1111 rr = TAILQ_PREV(r, swm_region_list, entry);
1113 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1119 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1120 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, rr->g.x,
1121 rr->g.y + bar_enabled ? bar_height : 0);
1125 swapwin(struct swm_region *r, union arg *args)
1127 struct ws_win *target;
1128 struct ws_win_list *wl;
1131 DNPRINTF(SWM_D_WS, "swapwin id %d "
1132 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1133 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1134 if (cur_focus == NULL)
1137 wl = &cur_focus->ws->winlist;
1140 case SWM_ARG_ID_SWAPPREV:
1141 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
1142 TAILQ_REMOVE(wl, cur_focus, entry);
1144 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
1146 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
1148 case SWM_ARG_ID_SWAPNEXT:
1149 target = TAILQ_NEXT(cur_focus, entry);
1150 TAILQ_REMOVE(wl, cur_focus, entry);
1152 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
1154 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
1156 case SWM_ARG_ID_SWAPMAIN:
1157 target = TAILQ_FIRST(wl);
1158 if (target == cur_focus)
1160 TAILQ_REMOVE(wl, target, entry);
1161 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
1162 TAILQ_REMOVE(wl, cur_focus, entry);
1163 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
1166 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1175 focus(struct swm_region *r, union arg *args)
1177 struct ws_win *winfocus, *winlostfocus;
1178 struct ws_win_list *wl;
1180 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1181 if (cur_focus == NULL)
1184 wl = &cur_focus->ws->winlist;
1186 winlostfocus = cur_focus;
1189 case SWM_ARG_ID_FOCUSPREV:
1190 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1191 if (winfocus == NULL)
1192 winfocus = TAILQ_LAST(wl, ws_win_list);
1195 case SWM_ARG_ID_FOCUSNEXT:
1196 winfocus = TAILQ_NEXT(cur_focus, entry);
1197 if (winfocus == NULL)
1198 winfocus = TAILQ_FIRST(wl);
1201 case SWM_ARG_ID_FOCUSMAIN:
1202 winfocus = TAILQ_FIRST(wl);
1209 if (winfocus == winlostfocus || winfocus == NULL)
1212 XMapRaised(display, winfocus->id);
1213 focus_win(winfocus);
1214 XSync(display, False);
1218 cycle_layout(struct swm_region *r, union arg *args)
1220 struct workspace *ws = r->ws;
1222 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1225 if (ws->cur_layout->l_stack == NULL)
1226 ws->cur_layout = &layouts[0];
1232 stack_config(struct swm_region *r, union arg *args)
1234 struct workspace *ws = r->ws;
1236 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1239 if (ws->cur_layout->l_config != NULL)
1240 ws->cur_layout->l_config(ws, args->id);
1242 if (args->id != SWM_ARG_ID_STACKINIT);
1248 struct swm_geometry g;
1249 struct swm_region *r;
1252 DNPRINTF(SWM_D_STACK, "stack\n");
1254 for (i = 0; i < ScreenCount(display); i++) {
1256 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1257 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1258 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1260 /* start with screen geometry, adjust for bar */
1270 r->ws->cur_layout->l_stack(r->ws, &g);
1273 XSync(display, False);
1277 stack_floater(struct ws_win *win, struct swm_region *r)
1282 bzero(&wc, sizeof wc);
1283 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1284 wc.border_width = 1;
1285 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1286 win->g.w = (double)WIDTH(r) * dialog_ratio;
1287 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1289 wc.width = win->g.w;
1290 wc.height = win->g.h;
1295 wc.x = (WIDTH(r) - win->g.w) / 2;
1296 wc.y = (HEIGHT(r) - win->g.h) / 2;
1299 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1300 win->id, wc.x, wc.y, wc.width, wc.height);
1302 XConfigureWindow(display, win->id, mask, &wc);
1305 #define SWAPXY(g) do { \
1307 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1308 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1311 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1314 struct swm_geometry win_g, r_g = *g;
1315 struct ws_win *win, *winfocus;
1316 int i, j, s, w_inc, h_inc, w_base, h_base, stacks;
1317 int hrh, extra, h_slice, last_h = 0;
1318 int split, colno, winno, mwin, msize, mscale;
1319 int remain, missing, v_slice;;
1322 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1323 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1325 if ((winno = count_win(ws, 0)) == 0)
1328 if (ws->focus == NULL)
1329 ws->focus = TAILQ_FIRST(&ws->winlist);
1330 winfocus = cur_focus ? cur_focus : ws->focus;
1332 win = TAILQ_FIRST(&ws->winlist);
1334 w_inc = win->sh.width_inc;
1335 w_base = win->sh.base_width;
1336 mwin = ws->l_state.horizontal_mwin;
1337 mscale = ws->l_state.horizontal_msize;
1338 stacks = ws->l_state.horizontal_stacks;
1341 w_inc = win->sh.height_inc;
1342 w_base = win->sh.base_height;
1343 mwin = ws->l_state.vertical_mwin;
1344 mscale = ws->l_state.vertical_msize;
1345 stacks = ws->l_state.vertical_stacks;
1349 if (stacks > winno - mwin)
1350 stacks = winno - mwin;
1352 h_slice = r_g.h / SWM_H_SLICE;
1353 if (mwin && winno > mwin) {
1354 v_slice = r_g.w / SWM_V_SLICE;
1358 win_g.w = v_slice * mscale;
1360 if (w_inc > 1 && w_inc < v_slice) {
1361 /* adjust for window's requested size increment */
1362 remain = (win_g.w - w_base) % w_inc;
1363 missing = w_inc - remain;
1365 if (missing <= extra || j == 0) {
1376 win_g.x += r_g.w - msize;
1379 colno = split = (winno - mwin) / stacks;
1385 hrh = r_g.h / colno;
1386 extra = r_g.h - (colno * hrh);
1389 /* stack all the tiled windows */
1390 i = j = 0, s = stacks;
1391 TAILQ_FOREACH(win, &ws->winlist, entry) {
1392 if (win->transient != 0 || win->floating != 0)
1395 if (split && i == split) {
1396 colno = (winno - mwin) / stacks;
1397 if (s <= (winno - mwin) % stacks)
1399 split = split + colno;
1400 hrh = (r_g.h / colno);
1401 extra = r_g.h - (colno * hrh);
1405 win_g.x += win_g.w + 2;
1406 win_g.w = (((r_g.w - (msize + 2)) -
1407 ((stacks - 1) * 2)) / stacks);
1409 win_g.w += (((r_g.w - (msize + 2)) -
1410 ((stacks - 1) * 2)) % stacks);
1416 h_inc = win->sh.width_inc;
1417 h_base = win->sh.base_width;
1419 h_inc = win->sh.height_inc;
1420 h_base = win->sh.base_height;
1422 if (j == colno - 1) {
1423 win_g.h = hrh + extra;
1424 } else if (h_inc > 1 && h_inc < h_slice) {
1425 /* adjust for window's requested size increment */
1426 remain = (win_g.h - h_base) % h_inc;
1427 missing = h_inc - remain;
1429 if (missing <= extra || j == 0) {
1441 win_g.y += last_h + 2;
1443 bzero(&wc, sizeof wc);
1444 wc.border_width = 1;
1446 win->g.x = wc.x = win_g.y;
1447 win->g.y = wc.y = win_g.x;
1448 win->g.w = wc.width = win_g.h;
1449 win->g.h = wc.height = win_g.w;
1451 win->g.x = wc.x = win_g.x;
1452 win->g.y = wc.y = win_g.y;
1453 win->g.w = wc.width = win_g.w;
1454 win->g.h = wc.height = win_g.h;
1456 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1457 XConfigureWindow(display, win->id, mask, &wc);
1458 XMapRaised(display, win->id);
1465 /* now, stack all the floaters and transients */
1466 TAILQ_FOREACH(win, &ws->winlist, entry) {
1467 if (win->transient == 0 && win->floating == 0)
1470 stack_floater(win, ws->r);
1471 XMapRaised(display, win->id);
1475 focus_win(winfocus); /* has to be done outside of the loop */
1479 vertical_config(struct workspace *ws, int id)
1481 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1484 case SWM_ARG_ID_STACKRESET:
1485 case SWM_ARG_ID_STACKINIT:
1486 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1487 ws->l_state.vertical_mwin = 1;
1488 ws->l_state.vertical_stacks = 1;
1490 case SWM_ARG_ID_MASTERSHRINK:
1491 if (ws->l_state.vertical_msize > 1)
1492 ws->l_state.vertical_msize--;
1494 case SWM_ARG_ID_MASTERGROW:
1495 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1496 ws->l_state.vertical_msize++;
1498 case SWM_ARG_ID_MASTERADD:
1499 ws->l_state.vertical_mwin++;
1501 case SWM_ARG_ID_MASTERDEL:
1502 if (ws->l_state.vertical_mwin > 0)
1503 ws->l_state.vertical_mwin--;
1504 case SWM_ARG_ID_COLINC:
1505 ws->l_state.vertical_stacks++;
1507 case SWM_ARG_ID_COLDEC:
1508 if (ws->l_state.vertical_stacks > 1)
1509 ws->l_state.vertical_stacks--;
1517 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1519 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1521 stack_master(ws, g, 0, 0);
1525 horizontal_config(struct workspace *ws, int id)
1527 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1530 case SWM_ARG_ID_STACKRESET:
1531 case SWM_ARG_ID_STACKINIT:
1532 ws->l_state.horizontal_mwin = 1;
1533 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1534 ws->l_state.horizontal_stacks = 1;
1536 case SWM_ARG_ID_MASTERSHRINK:
1537 if (ws->l_state.horizontal_msize > 1)
1538 ws->l_state.horizontal_msize--;
1540 case SWM_ARG_ID_MASTERGROW:
1541 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1542 ws->l_state.horizontal_msize++;
1544 case SWM_ARG_ID_MASTERADD:
1545 ws->l_state.horizontal_mwin++;
1547 case SWM_ARG_ID_MASTERDEL:
1548 if (ws->l_state.horizontal_mwin > 0)
1549 ws->l_state.horizontal_mwin--;
1551 case SWM_ARG_ID_COLINC:
1552 ws->l_state.horizontal_stacks++;
1554 case SWM_ARG_ID_COLDEC:
1555 if (ws->l_state.horizontal_stacks > 1)
1556 ws->l_state.horizontal_stacks--;
1563 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1565 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1567 stack_master(ws, g, 1, 0);
1570 /* fullscreen view */
1572 max_stack(struct workspace *ws, struct swm_geometry *g) {
1574 struct swm_geometry gg = *g;
1575 struct ws_win *win, *winfocus;
1578 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1580 if (count_win(ws, 0) == 0)
1583 if (ws->focus == NULL)
1584 ws->focus = TAILQ_FIRST(&ws->winlist);
1585 winfocus = cur_focus ? cur_focus : ws->focus;
1587 TAILQ_FOREACH(win, &ws->winlist, entry) {
1588 if (win->transient != 0 || win->floating != 0) {
1589 if (win == ws->focus) {
1591 stack_floater(win, ws->r);
1592 XMapRaised(display, win->id);
1594 XUnmapWindow(display, win->id);
1596 bzero(&wc, sizeof wc);
1597 wc.border_width = 1;
1598 win->g.x = wc.x = gg.x;
1599 win->g.y = wc.y = gg.y;
1600 win->g.w = wc.width = gg.w;
1601 win->g.h = wc.height = gg.h;
1602 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1603 XConfigureWindow(display, win->id, mask, &wc);
1605 if (win == ws->focus) {
1606 XMapRaised(display, win->id);
1608 XUnmapWindow(display, win->id);
1613 focus_win(winfocus); /* has to be done outside of the loop */
1617 send_to_ws(struct swm_region *r, union arg *args)
1619 int wsid = args->id;
1620 struct ws_win *win = cur_focus;
1621 struct workspace *ws, *nws;
1622 Atom ws_idx_atom = 0;
1623 unsigned char ws_idx_str[SWM_PROPLEN];
1628 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1631 nws = &win->s->ws[wsid];
1633 XUnmapWindow(display, win->id);
1635 /* find a window to focus */
1636 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1637 if (ws->focus == NULL)
1638 ws->focus = TAILQ_FIRST(&ws->winlist);
1639 if (ws->focus == win)
1642 TAILQ_REMOVE(&ws->winlist, win, entry);
1644 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1647 /* Try to update the window's workspace property */
1648 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1650 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1651 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1653 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1654 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1657 if (count_win(nws, 1) == 1)
1666 wkill(struct swm_region *r, union arg *args)
1668 DNPRINTF(SWM_D_MISC, "wkill\n");
1669 if(r->ws->focus != NULL)
1670 XKillClient(display, r->ws->focus->id);
1674 screenshot(struct swm_region *r, union arg *args)
1678 DNPRINTF(SWM_D_MISC, "screenshot\n");
1680 if (ss_enabled == 0)
1684 case SWM_ARG_ID_SS_ALL:
1685 spawn_screenshot[1] = "full";
1687 case SWM_ARG_ID_SS_WINDOW:
1688 spawn_screenshot[1] = "window";
1693 a.argv = spawn_screenshot;
1698 floating_toggle(struct swm_region *r, union arg *args)
1700 struct ws_win *win = cur_focus;
1705 win->floating = !win->floating;
1711 /* key definitions */
1715 void (*func)(struct swm_region *r, union arg *);
1718 /* modifier key function argument */
1719 { MODKEY, XK_space, cycle_layout, {0} },
1720 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1721 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1722 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1723 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1724 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1725 { MODKEY | ShiftMask, XK_comma, stack_config, {.id = SWM_ARG_ID_COLINC} },
1726 { MODKEY | ShiftMask, XK_period, stack_config, {.id = SWM_ARG_ID_COLDEC} },
1727 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1728 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1729 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1730 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1731 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1732 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1733 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1734 { MODKEY | ShiftMask, XK_q, quit, {0} },
1735 { MODKEY, XK_q, restart, {0} },
1736 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1737 { MODKEY, XK_1, switchws, {.id = 0} },
1738 { MODKEY, XK_2, switchws, {.id = 1} },
1739 { MODKEY, XK_3, switchws, {.id = 2} },
1740 { MODKEY, XK_4, switchws, {.id = 3} },
1741 { MODKEY, XK_5, switchws, {.id = 4} },
1742 { MODKEY, XK_6, switchws, {.id = 5} },
1743 { MODKEY, XK_7, switchws, {.id = 6} },
1744 { MODKEY, XK_8, switchws, {.id = 7} },
1745 { MODKEY, XK_9, switchws, {.id = 8} },
1746 { MODKEY, XK_0, switchws, {.id = 9} },
1747 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1748 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1749 { MODKEY | ShiftMask, XK_Right, cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
1750 { MODKEY | ShiftMask, XK_Left, cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
1751 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1752 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1753 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1754 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1755 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1756 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1757 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1758 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1759 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1760 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1761 { MODKEY, XK_b, bar_toggle, {0} },
1762 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1763 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1764 { MODKEY | ShiftMask, XK_x, wkill, {0} },
1765 { MODKEY, XK_s, screenshot, {.id = SWM_ARG_ID_SS_ALL} },
1766 { MODKEY | ShiftMask, XK_s, screenshot, {.id = SWM_ARG_ID_SS_WINDOW} },
1767 { MODKEY, XK_t, floating_toggle,{0} },
1768 { MODKEY | ShiftMask, XK_v, version, {0} },
1769 { MODKEY | ShiftMask, XK_Delete, spawn, {.argv = spawn_lock} },
1770 { MODKEY | ShiftMask, XK_i, spawn, {.argv = spawn_initscr} },
1774 resize_window(struct ws_win *win, int center)
1778 struct swm_region *r;
1780 r = root_to_region(win->wa.root);
1781 bzero(&wc, sizeof wc);
1782 mask = CWBorderWidth | CWWidth | CWHeight;
1783 wc.border_width = 1;
1784 wc.width = win->g.w;
1785 wc.height = win->g.h;
1786 if (center == SWM_ARG_ID_CENTER) {
1787 wc.x = (WIDTH(r) - win->g.w) / 2;
1788 wc.y = (HEIGHT(r) - win->g.h) / 2;
1792 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
1793 win->id, wc.x, wc.y, wc.width, wc.height);
1795 XConfigureWindow(display, win->id, mask, &wc);
1800 resize(struct ws_win *win, union arg *args)
1805 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %d\n",
1806 win->id, win->floating, win->transient);
1808 if (!(win->transient != 0 || win->floating != 0))
1811 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1812 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1814 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
1816 XMaskEvent(display, MOUSEMASK | ExposureMask |
1817 SubstructureRedirectMask, &ev);
1819 case ConfigureRequest:
1822 handler[ev.type](&ev);
1825 if (ev.xmotion.x < 0)
1827 if (ev.xmotion.y < 0)
1829 win->g.w = ev.xmotion.x;
1830 win->g.h = ev.xmotion.y;
1832 /* not free, don't sync more than 60 times / second */
1833 if ((ev.xmotion.time - time) > (1000 / 60) ) {
1834 time = ev.xmotion.time;
1835 XSync(display, False);
1836 resize_window(win, args->id);
1840 } while (ev.type != ButtonRelease);
1842 XSync(display, False);
1843 resize_window(win, args->id);
1845 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
1847 XUngrabPointer(display, CurrentTime);
1850 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1854 move_window(struct ws_win *win)
1858 struct swm_region *r;
1860 r = root_to_region(win->wa.root);
1861 bzero(&wc, sizeof wc);
1866 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
1867 win->id, wc.x, wc.y, wc.width, wc.height);
1869 XConfigureWindow(display, win->id, mask, &wc);
1874 move(struct ws_win *win, union arg *args)
1880 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %d\n",
1881 win->id, win->floating, win->transient);
1883 if (win->floating == 0) {
1889 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1890 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1892 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
1894 XMaskEvent(display, MOUSEMASK | ExposureMask |
1895 SubstructureRedirectMask, &ev);
1897 case ConfigureRequest:
1900 handler[ev.type](&ev);
1903 win->g.x = ev.xmotion.x_root;
1904 win->g.y = ev.xmotion.y_root;
1906 /* not free, don't sync more than 60 times / second */
1907 if ((ev.xmotion.time - time) > (1000 / 60) ) {
1908 time = ev.xmotion.time;
1909 XSync(display, False);
1914 } while (ev.type != ButtonRelease);
1916 XSync(display, False);
1919 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
1920 XUngrabPointer(display, CurrentTime);
1925 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1929 enum { client_click, root_click };
1931 unsigned int action;
1933 unsigned int button;
1934 void (*func)(struct ws_win *, union arg *);
1937 /* action key mouse button func args */
1938 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
1939 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
1940 { client_click, MODKEY, Button1, move, {0} },
1944 updatenumlockmask(void)
1947 XModifierKeymap *modmap;
1949 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1951 modmap = XGetModifierMapping(display);
1952 for (i = 0; i < 8; i++)
1953 for (j = 0; j < modmap->max_keypermod; j++)
1954 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1955 == XKeysymToKeycode(display, XK_Num_Lock))
1956 numlockmask = (1 << i);
1958 XFreeModifiermap(modmap);
1964 unsigned int i, j, k;
1966 unsigned int modifiers[] =
1967 { 0, LockMask, numlockmask, numlockmask | LockMask };
1969 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1970 updatenumlockmask();
1972 for (k = 0; k < ScreenCount(display); k++) {
1973 if (TAILQ_EMPTY(&screens[k].rl))
1975 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1976 for (i = 0; i < LENGTH(keys); i++) {
1977 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1978 for (j = 0; j < LENGTH(modifiers); j++)
1979 XGrabKey(display, code,
1980 keys[i].mod | modifiers[j],
1981 screens[k].root, True,
1982 GrabModeAsync, GrabModeAsync);
1988 grabbuttons(struct ws_win *win, int focused)
1991 unsigned int modifiers[] =
1992 { 0, LockMask, numlockmask, numlockmask|LockMask };
1994 updatenumlockmask();
1995 XUngrabButton(display, AnyButton, AnyModifier, win->id);
1997 for (i = 0; i < LENGTH(buttons); i++)
1998 if (buttons[i].action == client_click)
1999 for (j = 0; j < LENGTH(modifiers); j++)
2000 XGrabButton(display, buttons[i].button,
2001 buttons[i].mask | modifiers[j],
2002 win->id, False, BUTTONMASK,
2003 GrabModeAsync, GrabModeSync, None,
2006 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
2007 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
2013 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
2021 XKeyEvent *ev = &e->xkey;
2023 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
2025 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
2026 for (i = 0; i < LENGTH(keys); i++)
2027 if (keysym == keys[i].keysym
2028 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
2030 keys[i].func(root_to_region(ev->root),
2035 buttonpress(XEvent *e)
2037 XButtonPressedEvent *ev = &e->xbutton;
2042 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
2044 action = root_click;
2045 if ((win = find_window(ev->window)) == NULL)
2049 action = client_click;
2052 for (i = 0; i < LENGTH(buttons); i++)
2053 if (action == buttons[i].action && buttons[i].func &&
2054 buttons[i].button == ev->button &&
2055 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
2056 buttons[i].func(win, &buttons[i].args);
2060 set_win_state(struct ws_win *win, long state)
2062 long data[] = {state, None};
2064 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
2066 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
2067 (unsigned char *)data, 2);
2071 manage_window(Window id)
2074 struct workspace *ws;
2076 int format, i, ws_idx;
2077 unsigned long nitems, bytes;
2078 Atom ws_idx_atom = 0, type;
2079 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
2080 struct swm_region *r;
2085 if ((win = find_window(id)) != NULL)
2086 return (win); /* already being managed */
2088 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
2089 errx(1, "calloc: failed to allocate memory for new window");
2091 /* Get all the window data in one shot */
2092 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2094 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
2095 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
2096 XGetWindowAttributes(display, id, &win->wa);
2097 XGetTransientForHint(display, id, &trans);
2098 XGetWMNormalHints(display, id, &win->sh, &mask); /* XXX function? */
2100 win->transient = trans;
2101 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
2102 (unsigned)win->id, win->transient);
2106 * Figure out where to put the window. If it was previously assigned to
2107 * a workspace (either by spawn() or manually moving), and isn't
2108 * transient, * put it in the same workspace
2110 r = root_to_region(win->wa.root);
2111 if (prop && win->transient == 0) {
2112 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
2113 ws_idx = strtonum(prop, 0, 9, &errstr);
2115 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
2118 ws = &r->s->ws[ws_idx];
2122 /* set up the window layout */
2125 win->s = r->s; /* this never changes */
2126 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
2128 win->g.w = win->wa.width;
2129 win->g.h = win->wa.height;
2130 win->g.x = win->wa.x;
2131 win->g.y = win->wa.y;
2133 /* Set window properties so we can remember this after reincarnation */
2134 if (ws_idx_atom && prop == NULL &&
2135 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
2136 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2138 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2139 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2143 if (XGetClassHint(display, win->id, &win->ch)) {
2144 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
2145 win->ch.res_class, win->ch.res_name);
2146 for (i = 0; quirks[i].class != NULL && quirks[i].name != NULL &&
2147 quirks[i].quirk != 0; i++){
2148 if (!strcmp(win->ch.res_class, quirks[i].class) &&
2149 !strcmp(win->ch.res_name, quirks[i].name)) {
2150 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
2151 win->ch.res_class, win->ch.res_name);
2152 if (quirks[i].quirk & SWM_Q_FLOAT)
2154 win->quirks = quirks[i].quirk;
2159 /* alter window position if quirky */
2160 if (win->quirks & SWM_Q_ANYWHERE) {
2161 win->manual = 1; /* don't center the quirky windows */
2162 bzero(&wc, sizeof wc);
2164 if (win->g.y < bar_height) {
2165 win->g.y = wc.y = bar_height;
2168 if (win->g.w + win->g.x > WIDTH(r)) {
2169 win->g.x = wc.x = WIDTH(win->ws->r) - win->g.w - 2;
2172 wc.border_width = 1;
2173 mask |= CWBorderWidth;
2174 XConfigureWindow(display, win->id, mask, &wc);
2177 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
2178 PropertyChangeMask | StructureNotifyMask);
2180 set_win_state(win, NormalState);
2182 /* floaters need this */
2184 XMapRaised(display, win->id);
2186 /* make new win focused */
2193 unmanage_window(struct ws_win *win)
2195 struct workspace *ws;
2200 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
2202 /* don't unmanage if we are switching workspaces */
2207 /* find a window to focus */
2208 if (ws->focus == win)
2209 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
2210 if (ws->focus == NULL)
2211 ws->focus = TAILQ_FIRST(&ws->winlist);
2212 if (ws->focus == NULL || ws->focus == win) {
2216 focus_win(ws->focus);
2218 TAILQ_REMOVE(&win->ws->winlist, win, entry);
2219 set_win_state(win, WithdrawnState);
2220 if (win->ch.res_class)
2221 XFree(win->ch.res_class);
2222 if (win->ch.res_name)
2223 XFree(win->ch.res_name);
2228 configurerequest(XEvent *e)
2230 XConfigureRequestEvent *ev = &e->xconfigurerequest;
2235 if ((win = find_window(ev->window)) == NULL)
2239 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
2241 bzero(&wc, sizeof wc);
2244 wc.width = ev->width;
2245 wc.height = ev->height;
2246 wc.border_width = ev->border_width;
2247 wc.sibling = ev->above;
2248 wc.stack_mode = ev->detail;
2249 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
2251 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
2253 if (win->floating) {
2254 if (ev->value_mask & CWX)
2256 if (ev->value_mask & CWY)
2258 if (ev->value_mask & CWWidth)
2259 win->g.w = ev->width;
2260 if (ev->value_mask & CWHeight)
2261 win->g.h = ev->height;
2262 if (win->ws->r != NULL) {
2263 /* this seems to be full screen */
2264 if (win->g.w >= WIDTH(win->ws->r)) {
2266 win->g.w = WIDTH(win->ws->r);
2267 ev->value_mask |= CWX | CWWidth;
2269 if (win->g.h >= HEIGHT(win->ws->r)) {
2272 win->g.h = HEIGHT(win->ws->r);
2273 ev->value_mask |= CWY | CWHeight;
2276 if ((ev->value_mask & (CWX | CWY)) &&
2277 !(ev->value_mask & (CWWidth | CWHeight)))
2279 XMoveResizeWindow(display, win->id,
2280 win->g.x, win->g.y, win->g.w, win->g.h);
2287 configurenotify(XEvent *e)
2289 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
2290 e->xconfigure.window);
2294 destroynotify(XEvent *e)
2297 XDestroyWindowEvent *ev = &e->xdestroywindow;
2299 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
2301 if ((win = find_window(ev->window)) != NULL) {
2302 unmanage_window(win);
2308 enternotify(XEvent *e)
2310 XCrossingEvent *ev = &e->xcrossing;
2313 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
2316 /* eat event(r) to prevent autofocus */
2321 if ((win = find_window(ev->window)) != NULL)
2328 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
2332 mappingnotify(XEvent *e)
2334 XMappingEvent *ev = &e->xmapping;
2336 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
2338 XRefreshKeyboardMapping(ev);
2339 if (ev->request == MappingKeyboard)
2344 maprequest(XEvent *e)
2346 XMapRequestEvent *ev = &e->xmaprequest;
2347 XWindowAttributes wa;
2349 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
2350 e->xmaprequest.window);
2352 if (!XGetWindowAttributes(display, ev->window, &wa))
2354 if (wa.override_redirect)
2356 manage_window(e->xmaprequest.window);
2362 propertynotify(XEvent *e)
2365 XPropertyEvent *ev = &e->xproperty;
2367 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
2370 if (ev->state == PropertyDelete)
2371 return; /* ignore */
2372 win = find_window(ev->window);
2377 case XA_WM_NORMAL_HINTS:
2380 XGetWMNormalHints(display, win->id, &win->sh, &mask);
2381 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
2382 if (win->sh.flags & PMinSize) {
2383 win->g.w = win->sh.min_width;
2384 win->g.h = win->sh.min_height;
2385 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
2387 XMoveResizeWindow(display, win->id,
2388 win->g.x, win->g.y, win->g.w, win->g.h);
2397 unmapnotify(XEvent *e)
2399 XDestroyWindowEvent *ev = &e->xdestroywindow;
2402 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
2404 if ((win = find_window(ev->window)) != NULL)
2406 unmanage_window(win);
2410 visibilitynotify(XEvent *e)
2413 struct swm_region *r;
2415 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
2416 e->xvisibility.window);
2417 if (e->xvisibility.state == VisibilityUnobscured)
2418 for (i = 0; i < ScreenCount(display); i++)
2419 TAILQ_FOREACH(r, &screens[i].rl, entry)
2420 if (e->xvisibility.window == r->bar_window)
2425 xerror_start(Display *d, XErrorEvent *ee)
2432 xerror(Display *d, XErrorEvent *ee)
2434 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2442 xerrorxlib = XSetErrorHandler(xerror_start);
2444 /* this causes an error if some other window manager is running */
2445 XSelectInput(display, DefaultRootWindow(display),
2446 SubstructureRedirectMask);
2447 XSync(display, False);
2451 XSetErrorHandler(xerror);
2452 XSync(display, False);
2461 unsigned char *p = NULL;
2462 unsigned long n, extra;
2465 astate = XInternAtom(display, "WM_STATE", False);
2466 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
2467 &real, &format, &n, &extra, (unsigned char **)&p);
2468 if (status != Success)
2477 new_region(struct swm_screen *s, int x, int y, int w, int h)
2479 struct swm_region *r, *n;
2480 struct workspace *ws = NULL;
2483 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
2484 s->idx, w, h, x, y);
2486 /* remove any conflicting regions */
2487 n = TAILQ_FIRST(&s->rl);
2490 n = TAILQ_NEXT(r, entry);
2491 if (X(r) < (x + w) &&
2492 (X(r) + WIDTH(r)) > x &&
2494 (Y(r) + HEIGHT(r)) > y) {
2495 XDestroyWindow(display, r->bar_window);
2496 TAILQ_REMOVE(&s->rl, r, entry);
2497 TAILQ_INSERT_TAIL(&s->orl, r, entry);
2501 /* search old regions for one to reuse */
2503 /* size + location match */
2504 TAILQ_FOREACH(r, &s->orl, entry)
2505 if (X(r) == x && Y(r) == y &&
2506 HEIGHT(r) == h && WIDTH(r) == w)
2510 TAILQ_FOREACH(r, &s->orl, entry)
2511 if (HEIGHT(r) == h && WIDTH(r) == w)
2515 TAILQ_REMOVE(&s->orl, r, entry);
2516 /* try to use old region's workspace */
2517 if (r->ws->r == NULL)
2520 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
2521 errx(1, "calloc: failed to allocate memory for screen");
2523 /* if we don't have a workspace already, find one */
2525 for (i = 0; i < SWM_WS_MAX; i++)
2526 if (s->ws[i].r == NULL) {
2533 errx(1, "no free workspaces\n");
2542 TAILQ_INSERT_TAIL(&s->rl, r, entry);
2549 #ifdef SWM_XRR_HAS_CRTC
2551 XRRScreenResources *sr;
2554 #endif /* SWM_XRR_HAS_CRTC */
2555 struct swm_region *r;
2558 if (i >= ScreenCount(display))
2559 errx(1, "invalid screen");
2561 /* remove any old regions */
2562 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
2564 XDestroyWindow(display, r->bar_window);
2565 TAILQ_REMOVE(&screens[i].rl, r, entry);
2566 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
2569 /* map virtual screens onto physical screens */
2570 #ifdef SWM_XRR_HAS_CRTC
2571 if (xrandr_support) {
2572 sr = XRRGetScreenResources(display, screens[i].root);
2574 new_region(&screens[i], 0, 0,
2575 DisplayWidth(display, i),
2576 DisplayHeight(display, i));
2580 for (c = 0, ci = NULL; c < ncrtc; c++) {
2581 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
2582 if (ci->noutput == 0)
2585 if (ci != NULL && ci->mode == None)
2586 new_region(&screens[i], 0, 0,
2587 DisplayWidth(display, i),
2588 DisplayHeight(display, i));
2590 new_region(&screens[i],
2591 ci->x, ci->y, ci->width, ci->height);
2594 XRRFreeCrtcInfo(ci);
2595 XRRFreeScreenResources(sr);
2597 #endif /* SWM_XRR_HAS_CRTC */
2599 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
2600 DisplayHeight(display, i));
2605 screenchange(XEvent *e) {
2606 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
2607 struct swm_region *r;
2611 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
2613 if (!XRRUpdateConfiguration(e))
2616 /* silly event doesn't include the screen index */
2617 for (i = 0; i < ScreenCount(display); i++)
2618 if (screens[i].root == xe->root)
2620 if (i >= ScreenCount(display))
2621 errx(1, "screenchange: screen not found\n");
2623 /* brute force for now, just re-enumerate the regions */
2626 /* hide any windows that went away */
2627 TAILQ_FOREACH(r, &screens[i].rl, entry)
2628 TAILQ_FOREACH(win, &r->ws->winlist, entry)
2629 XUnmapWindow(display, win->id);
2636 Window d1, d2, *wins = NULL;
2637 XWindowAttributes wa;
2640 int errorbase, major, minor;
2641 struct workspace *ws;
2645 if ((screens = calloc(ScreenCount(display),
2646 sizeof(struct swm_screen))) == NULL)
2647 errx(1, "calloc: screens");
2649 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2651 /* initial Xrandr setup */
2652 xrandr_support = XRRQueryExtension(display,
2653 &xrandr_eventbase, &errorbase);
2655 if (XRRQueryVersion(display, &major, &minor) && major < 1)
2658 /* map physical screens */
2659 for (i = 0; i < ScreenCount(display); i++) {
2660 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
2662 TAILQ_INIT(&screens[i].rl);
2663 TAILQ_INIT(&screens[i].orl);
2664 screens[i].root = RootWindow(display, i);
2666 /* set default colors */
2667 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
2668 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
2669 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
2670 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
2671 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
2673 /* init all workspaces */
2674 /* XXX these should be dynamically allocated too */
2675 for (j = 0; j < SWM_WS_MAX; j++) {
2676 ws = &screens[i].ws[j];
2681 TAILQ_INIT(&ws->winlist);
2683 for (k = 0; layouts[k].l_stack != NULL; k++)
2684 if (layouts[k].l_config != NULL)
2685 layouts[k].l_config(ws,
2686 SWM_ARG_ID_STACKINIT);
2687 ws->cur_layout = &layouts[0];
2689 /* grab existing windows (before we build the bars)*/
2690 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
2696 XRRSelectInput(display, screens[i].root,
2697 RRScreenChangeNotifyMask);
2699 /* attach windows to a region */
2700 /* normal windows */
2701 for (j = 0; j < no; j++) {
2702 XGetWindowAttributes(display, wins[j], &wa);
2703 if (!XGetWindowAttributes(display, wins[j], &wa) ||
2704 wa.override_redirect ||
2705 XGetTransientForHint(display, wins[j], &d1))
2708 if (wa.map_state == IsViewable ||
2709 getstate(wins[j]) == NormalState)
2710 manage_window(wins[j]);
2712 /* transient windows */
2713 for (j = 0; j < no; j++) {
2714 if (!XGetWindowAttributes(display, wins[j], &wa))
2717 if (XGetTransientForHint(display, wins[j], &d1) &&
2718 (wa.map_state == IsViewable || getstate(wins[j]) ==
2720 manage_window(wins[j]);
2730 main(int argc, char *argv[])
2733 char conf[PATH_MAX], *cfile = NULL;
2740 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
2741 SWM_VERSION, cvstag);
2742 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2743 warnx("no locale support");
2745 if (!(display = XOpenDisplay(0)))
2746 errx(1, "can not open display");
2749 errx(1, "other wm running");
2751 astate = XInternAtom(display, "WM_STATE", False);
2753 /* look for local and global conf file */
2754 pwd = getpwuid(getuid());
2756 errx(1, "invalid user %d", getuid());
2760 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2761 if (stat(conf, &sb) != -1) {
2762 if (S_ISREG(sb.st_mode))
2765 /* try global conf file */
2766 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2767 if (!stat(conf, &sb))
2768 if (S_ISREG(sb.st_mode))
2775 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2780 xfd = ConnectionNumber(display);
2784 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2786 errx(1, "select failed");
2791 while (XPending(display)) {
2792 XNextEvent(display, &e);
2793 if (e.type < LASTEvent) {
2794 if (handler[e.type])
2795 handler[e.type](&e);
2797 DNPRINTF(SWM_D_EVENT,
2798 "win: %lu unknown event: %d\n",
2799 e.xany.window, e.type);
2801 switch (e.type - xrandr_eventbase) {
2802 case RRScreenChangeNotify:
2806 DNPRINTF(SWM_D_EVENT,
2807 "win: %lu unknown xrandr event: "
2808 "%d\n", e.xany.window, e.type);
2815 XCloseDisplay(display);