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.9"
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"
145 int (*xerrorxlib)(Display *, XErrorEvent *);
150 int xrandr_eventbase;
151 int ignore_enter = 0;
152 unsigned int numlockmask = 0;
156 int cycle_visible = 0;
158 int font_adjusted = 0;
161 double dialog_ratio = .6;
163 #define SWM_BAR_MAX (128)
164 char *bar_argv[] = { NULL, NULL };
166 char bar_ext[SWM_BAR_MAX];
167 char bar_vertext[SWM_BAR_MAX];
169 sig_atomic_t bar_alarm = 0;
173 int bar_extra_running = 0;
181 char *bar_fonts[] = {
182 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
183 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
187 /* terminal + args */
188 char *spawn_term[] = { "xterm", NULL };
189 char *spawn_screenshot[] = { "screenshot.sh", NULL, NULL };
190 char *spawn_lock[] = { "xlock", NULL };
191 char *spawn_initscr[] = { "initscreen.sh", NULL };
192 char *spawn_menu[] = { "dmenu_run", "-fn", NULL, "-nb", NULL,
193 "-nf", NULL, "-sb", NULL, "-sf", NULL, NULL };
195 #define SWM_MENU_FN (2)
196 #define SWM_MENU_NB (4)
197 #define SWM_MENU_NF (6)
198 #define SWM_MENU_SB (8)
199 #define SWM_MENU_SF (10)
201 /* layout manager data */
202 struct swm_geometry {
212 /* virtual "screens" */
214 TAILQ_ENTRY(swm_region) entry;
215 struct swm_geometry g;
216 struct workspace *ws; /* current workspace on this region */
217 struct swm_screen *s; /* screen idx */
220 TAILQ_HEAD(swm_region_list, swm_region);
223 TAILQ_ENTRY(ws_win) entry;
225 struct swm_geometry g;
230 int font_size_boundary[SWM_MAX_FONT_STEPS];
234 unsigned long quirks;
235 struct workspace *ws; /* always valid */
236 struct swm_screen *s; /* always valid, never changes */
237 XWindowAttributes wa;
241 TAILQ_HEAD(ws_win_list, ws_win);
243 /* layout handlers */
245 void vertical_config(struct workspace *, int);
246 void vertical_stack(struct workspace *, struct swm_geometry *);
247 void horizontal_config(struct workspace *, int);
248 void horizontal_stack(struct workspace *, struct swm_geometry *);
249 void max_stack(struct workspace *, struct swm_geometry *);
251 void grabbuttons(struct ws_win *, int);
254 void (*l_stack)(struct workspace *, struct swm_geometry *);
255 void (*l_config)(struct workspace *, int);
257 /* stack, configure */
258 { vertical_stack, vertical_config},
259 { horizontal_stack, horizontal_config},
264 #define SWM_H_SLICE (32)
265 #define SWM_V_SLICE (32)
267 /* define work spaces */
269 int idx; /* workspace index */
270 int restack; /* restack on switch */
271 struct layout *cur_layout; /* current layout handlers */
272 struct ws_win *focus; /* may be NULL */
273 struct ws_win *focus_prev; /* may be NULL */
274 struct swm_region *r; /* may be NULL */
275 struct ws_win_list winlist; /* list of windows in ws */
279 int horizontal_msize;
281 int horizontal_stacks;
288 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
289 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
291 /* physical screen mapping */
292 #define SWM_WS_MAX (10) /* XXX Too small? */
294 int idx; /* screen index */
295 struct swm_region_list rl; /* list of regions on this screen */
296 struct swm_region_list orl; /* list of old regions */
298 struct workspace ws[SWM_WS_MAX];
304 } c[SWM_S_COLOR_MAX];
306 struct swm_screen *screens;
309 struct ws_win *cur_focus = NULL;
311 /* args to functions */
314 #define SWM_ARG_ID_FOCUSNEXT (0)
315 #define SWM_ARG_ID_FOCUSPREV (1)
316 #define SWM_ARG_ID_FOCUSMAIN (2)
317 #define SWM_ARG_ID_SWAPNEXT (3)
318 #define SWM_ARG_ID_SWAPPREV (4)
319 #define SWM_ARG_ID_SWAPMAIN (5)
320 #define SWM_ARG_ID_MASTERSHRINK (6)
321 #define SWM_ARG_ID_MASTERGROW (7)
322 #define SWM_ARG_ID_MASTERADD (8)
323 #define SWM_ARG_ID_MASTERDEL (9)
324 #define SWM_ARG_ID_STACKRESET (10)
325 #define SWM_ARG_ID_STACKINIT (11)
326 #define SWM_ARG_ID_CYCLEWS_UP (12)
327 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
328 #define SWM_ARG_ID_CYCLESC_UP (14)
329 #define SWM_ARG_ID_CYCLESC_DOWN (15)
330 #define SWM_ARG_ID_STACKINC (16)
331 #define SWM_ARG_ID_STACKDEC (17)
332 #define SWM_ARG_ID_SS_ALL (0)
333 #define SWM_ARG_ID_SS_WINDOW (1)
334 #define SWM_ARG_ID_DONTCENTER (0)
335 #define SWM_ARG_ID_CENTER (1)
336 #define SWM_ARG_ID_KILLWINDOW (0)
337 #define SWM_ARG_ID_DELETEWINDOW (1)
346 #define SWM_Q_FLOAT (1<<0) /* float this window */
347 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
348 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
349 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
351 { "MPlayer", "xv", SWM_Q_FLOAT },
352 { "OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT },
353 { "OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT },
354 { "Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ},
355 { "Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE},
356 { "XTerm", "xterm", SWM_Q_XTERM_FONTADJ},
361 void expose(XEvent *);
362 void keypress(XEvent *);
363 void buttonpress(XEvent *);
364 void configurerequest(XEvent *);
365 void configurenotify(XEvent *);
366 void destroynotify(XEvent *);
367 void enternotify(XEvent *);
368 void focusin(XEvent *);
369 void mappingnotify(XEvent *);
370 void maprequest(XEvent *);
371 void propertynotify(XEvent *);
372 void unmapnotify(XEvent *);
373 void visibilitynotify(XEvent *);
375 void (*handler[LASTEvent])(XEvent *) = {
377 [KeyPress] = keypress,
378 [ButtonPress] = buttonpress,
379 [ConfigureRequest] = configurerequest,
380 [ConfigureNotify] = configurenotify,
381 [DestroyNotify] = destroynotify,
382 [EnterNotify] = enternotify,
384 [MappingNotify] = mappingnotify,
385 [MapRequest] = maprequest,
386 [PropertyNotify] = propertynotify,
387 [UnmapNotify] = unmapnotify,
388 [VisibilityNotify] = visibilitynotify,
392 name_to_color(char *colorname)
396 XColor screen_def, exact_def;
397 unsigned long result = 0;
398 char cname[32] = "#";
400 cmap = DefaultColormap(display, screens[0].idx);
401 status = XAllocNamedColor(display, cmap, colorname,
402 &screen_def, &exact_def);
404 strlcat(cname, colorname + 2, sizeof cname - 1);
405 status = XAllocNamedColor(display, cmap, cname, &screen_def,
409 result = screen_def.pixel;
411 fprintf(stderr, "color '%s' not found.\n", colorname);
417 setscreencolor(char *val, int i, int c)
419 if (i > 0 && i <= ScreenCount(display)) {
420 screens[i - 1].c[c].color = name_to_color(val);
421 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
423 } else if (i == -1) {
424 for (i = 0; i < ScreenCount(display); i++)
425 screens[i].c[c].color = name_to_color(val);
426 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
429 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
430 i, ScreenCount(display));
433 void new_region(struct swm_screen *, int, int, int, int);
436 custom_region(char *val)
438 unsigned int sidx, x, y, w, h;
440 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
441 errx(1, "invalid custom region, "
442 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
443 if (sidx < 1 || sidx > ScreenCount(display))
444 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
445 sidx, ScreenCount(display));
449 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
451 if (x < 0 || x > DisplayWidth(display, sidx) ||
452 y < 0 || y > DisplayHeight(display, sidx) ||
453 w + x > DisplayWidth(display, sidx) ||
454 h + y > DisplayHeight(display, sidx))
455 errx(1, "region %ux%u+%u+%u not within screen boundaries "
456 "(%ux%u)\n", w, h, x, y,
457 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
459 new_region(&screens[sidx], x, y, w, h);
463 varmatch(char *var, char *name, int *index)
468 i = strncmp(var, name, 255);
475 p = var + strlen(name);
478 bzero(buf, sizeof buf);
480 while (isdigit(*p) && i < sizeof buf)
482 if (i == 0 || i >= sizeof buf || *p != ']')
484 *index = strtonum(buf, 0, 99, NULL);
488 /* conf file stuff */
489 #define SWM_CONF_WS "\n= \t"
490 #define SWM_CONF_FILE "scrotwm.conf"
492 conf_load(char *filename)
495 char *line, *cp, *var, *val;
496 size_t len, lineno = 0;
499 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
501 if (filename == NULL)
504 if ((config = fopen(filename, "r")) == NULL)
507 for (sc = ScreenCount(display);;) {
508 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
512 cp += (long)strspn(cp, SWM_CONF_WS);
518 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
520 cp += (long)strspn(cp, SWM_CONF_WS);
521 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
524 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
527 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
528 bar_enabled = atoi(val);
529 else if (!varmatch(var, "bar_border", &i))
530 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
531 else if (!varmatch(var, "bar_color", &i))
532 setscreencolor(val, i, SWM_S_COLOR_BAR);
533 else if (!varmatch(var, "bar_font_color", &i))
534 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
535 else if (!strncmp(var, "bar_font", strlen("bar_font")))
536 asprintf(&bar_fonts[0], "%s", val);
537 else if (!strncmp(var, "bar_action", strlen("bar_action")))
538 asprintf(&bar_argv[0], "%s", val);
539 else if (!strncmp(var, "bar_delay", strlen("bar_delay")))
540 bar_delay = atoi(val);
546 if (!varmatch(var, "color_focus", &i))
547 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
548 else if (!varmatch(var, "color_unfocus", &i))
549 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
550 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
551 cycle_visible = atoi(val);
552 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
553 cycle_visible = atoi(val);
559 if (!strncmp(var, "dialog_ratio",
560 strlen("dialog_ratio"))) {
561 dialog_ratio = atof(val);
562 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
569 if (!strncmp(var, "region", strlen("region")))
576 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
577 asprintf(&spawn_term[0], "%s", val);
578 else if (!strncmp(var, "screenshot_enabled",
579 strlen("screenshot_enabled")))
580 ss_enabled = atoi(val);
581 else if (!strncmp(var, "screenshot_app",
582 strlen("screenshot_app")))
583 asprintf(&spawn_screenshot[0], "%s", val);
589 if (!strncmp(var, "term_width", strlen("term_width")))
590 term_width = atoi(val);
605 errx(1, "invalid conf file entry: %s=%s", var, val);
609 socket_setnonblock(int fd)
613 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
614 err(1, "fcntl F_GETFL");
616 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
617 err(1, "fcntl F_SETFL");
621 bar_print(struct swm_region *r, char *s)
623 XClearWindow(display, r->bar_window);
624 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
625 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
634 bzero(bar_pipe, sizeof bar_pipe);
637 kill(bar_pid, SIGTERM);
640 strlcpy(bar_ext, "", sizeof bar_ext);
649 struct swm_region *r;
653 char loc[SWM_BAR_MAX];
656 if (bar_enabled == 0)
658 if (bar_extra && bar_extra_running) {
659 /* ignore short reads; it'll correct itself */
660 while ((b = fgetln(stdin, &len)) != NULL)
661 if (b && b[len - 1] == '\n') {
663 strlcpy(bar_ext, b, sizeof bar_ext);
665 if (b == NULL && errno != EAGAIN) {
666 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
667 errno, strerror(errno));
671 strlcpy(bar_ext, "", sizeof bar_ext);
674 localtime_r(&tmt, &tm);
675 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
676 for (i = 0; i < ScreenCount(display); i++) {
678 TAILQ_FOREACH(r, &screens[i].rl, entry) {
679 snprintf(loc, sizeof loc, "%s %d:%d %s %s",
680 s, x++, r->ws->idx + 1, bar_ext, bar_vertext);
684 XSync(display, False);
695 bar_toggle(struct swm_region *r, union arg *args)
697 struct swm_region *tmpr;
698 int i, j, sc = ScreenCount(display);
700 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
703 for (i = 0; i < sc; i++)
704 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
705 XUnmapWindow(display, tmpr->bar_window);
707 for (i = 0; i < sc; i++)
708 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
709 XMapRaised(display, tmpr->bar_window);
711 bar_enabled = !bar_enabled;
712 for (i = 0; i < sc; i++)
713 for (j = 0; j < SWM_WS_MAX; j++)
714 screens[i].ws[j].restack = 1;
717 /* must be after stack */
724 XSetWindowAttributes wa;
725 struct swm_region *r;
728 /* do this here because the conf file is in memory */
729 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
730 /* launch external status app */
731 bar_extra_running = 1;
732 if (pipe(bar_pipe) == -1)
733 err(1, "pipe error");
734 socket_setnonblock(bar_pipe[0]);
735 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
736 if (dup2(bar_pipe[0], 0) == -1)
738 if (dup2(bar_pipe[1], 1) == -1)
740 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
741 err(1, "could not disable SIGPIPE");
742 switch (bar_pid = fork()) {
744 err(1, "cannot fork");
748 execvp(bar_argv[0], bar_argv);
749 err(1, "%s external app failed", bar_argv[0]);
751 default: /* parent */
757 bzero(&wa, sizeof wa);
758 for (i = 0; i < ScreenCount(display); i++)
759 TAILQ_FOREACH(r, &screens[i].rl, entry) {
761 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
762 wa.background_pixel =
763 screens[i].c[SWM_S_COLOR_BAR].color;
764 XChangeWindowAttributes(display, r->bar_window,
765 CWBackPixel | CWBorderPixel, &wa);
771 bar_setup(struct swm_region *r)
775 for (i = 0; bar_fonts[i] != NULL; i++) {
776 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
782 if (bar_fonts[i] == NULL)
783 errx(1, "couldn't load font");
784 bar_height = bar_fs->ascent + bar_fs->descent + 3;
786 r->bar_window = XCreateSimpleWindow(display,
787 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
788 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
789 r->s->c[SWM_S_COLOR_BAR].color);
790 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
791 XSetFont(display, bar_gc, bar_fs->fid);
792 XSelectInput(display, r->bar_window, VisibilityChangeMask);
794 XMapRaised(display, r->bar_window);
795 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
797 if (signal(SIGALRM, bar_signal) == SIG_ERR)
798 err(1, "could not install bar_signal");
803 version(struct swm_region *r, union arg *args)
805 bar_version = !bar_version;
807 strlcpy(bar_vertext, cvstag, sizeof bar_vertext);
809 strlcpy(bar_vertext, "", sizeof bar_vertext);
814 client_msg(struct ws_win *win, Atom a)
816 XClientMessageEvent cm;
818 bzero(&cm, sizeof cm);
819 cm.type = ClientMessage;
821 cm.message_type = aprot;
824 cm.data.l[1] = CurrentTime;
825 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
829 config_win(struct ws_win *win)
833 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
834 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
835 ce.type = ConfigureNotify;
836 ce.display = display;
842 ce.height = win->g.h;
843 ce.border_width = 1; /* XXX store this! */
845 ce.override_redirect = False;
846 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
850 count_win(struct workspace *ws, int count_transient)
855 TAILQ_FOREACH(win, &ws->winlist, entry) {
856 if (count_transient == 0 && win->floating)
858 if (count_transient == 0 && win->transient)
862 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
868 quit(struct swm_region *r, union arg *args)
870 DNPRINTF(SWM_D_MISC, "quit\n");
880 for (i = 0; i < ScreenCount(display); i++)
881 for (j = 0; j < SWM_WS_MAX; j++)
882 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
883 XUnmapWindow(display, win->id);
887 fake_keypress(struct ws_win *win, int keysym, int modifiers)
891 event.display = display; /* Ignored, but what the hell */
892 event.window = win->id;
893 event.root = win->s->root;
894 event.subwindow = None;
895 event.time = CurrentTime;
900 event.same_screen = True;
901 event.keycode = XKeysymToKeycode(display, keysym);
902 event.state = modifiers;
904 event.type = KeyPress;
905 XSendEvent(event.display, event.window, True,
906 KeyPressMask, (XEvent *)&event);
908 event.type = KeyRelease;
909 XSendEvent(event.display, event.window, True,
910 KeyPressMask, (XEvent *)&event);
915 restart(struct swm_region *r, union arg *args)
917 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
919 /* disable alarm because the following code may not be interrupted */
921 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
922 errx(1, "can't disable alarm");
927 XCloseDisplay(display);
928 execvp(start_argv[0], start_argv);
929 fprintf(stderr, "execvp failed\n");
935 root_to_region(Window root)
937 struct swm_region *r = NULL;
942 for (i = 0; i < ScreenCount(display); i++)
943 if (screens[i].root == root)
946 if (XQueryPointer(display, screens[i].root,
947 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
948 /* choose a region based on pointer location */
949 TAILQ_FOREACH(r, &screens[i].rl, entry)
950 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
951 y >= Y(r) && y <= Y(r) + HEIGHT(r))
956 r = TAILQ_FIRST(&screens[i].rl);
962 find_window(Window id)
967 for (i = 0; i < ScreenCount(display); i++)
968 for (j = 0; j < SWM_WS_MAX; j++)
969 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
976 spawn(struct swm_region *r, union arg *args)
981 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
983 * The double-fork construct avoids zombie processes and keeps the code
984 * clean from stupid signal handlers.
989 close(ConnectionNumber(display));
990 setenv("LD_PRELOAD", SWM_LIB, 1);
991 if (asprintf(&ret, "%d", r->ws->idx)) {
992 setenv("_SWM_WS", ret, 1);
995 if (asprintf(&ret, "%d", getpid())) {
996 setenv("_SWM_PID", ret, 1);
1000 /* kill stdin, mplayer, ssh-add etc. need that */
1001 si = open("/dev/null", O_RDONLY, 0);
1003 err(1, "open /dev/null");
1004 if (dup2(si, 0) == -1)
1005 err(1, "dup2 /dev/null");
1006 execvp(args->argv[0], args->argv);
1007 fprintf(stderr, "execvp failed\n");
1016 spawnterm(struct swm_region *r, union arg *args)
1018 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1021 setenv("_SWM_XTERM_FONTADJ", "", 1);
1026 spawnmenu(struct swm_region *r, union arg *args)
1028 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
1030 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
1031 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
1032 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
1033 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
1034 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
1045 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
1047 for (i = 0; i < ScreenCount(display); i++)
1048 for (j = 0; j < SWM_WS_MAX; j++)
1049 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1050 if (win->ws->r == NULL)
1052 grabbuttons(win, 0);
1053 XSetWindowBorder(display, win->id,
1054 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1056 win->ws->focus = NULL;
1062 focus_win(struct ws_win *win)
1064 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1069 if (win->ws->focus != win && win->ws->focus != NULL)
1070 win->ws->focus_prev = win->ws->focus;
1073 win->ws->focus = win;
1074 if (win->ws->r != NULL) {
1076 if (!win->got_focus) {
1077 XSetWindowBorder(display, win->id,
1078 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1079 grabbuttons(win, 1);
1082 XSetInputFocus(display, win->id,
1083 RevertToPointerRoot, CurrentTime);
1088 switchws(struct swm_region *r, union arg *args)
1090 int wsid = args->id;
1091 struct swm_region *this_r, *other_r;
1093 struct workspace *new_ws, *old_ws;
1096 old_ws = this_r->ws;
1097 new_ws = &this_r->s->ws[wsid];
1099 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1100 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1103 if (new_ws == old_ws)
1106 other_r = new_ws->r;
1108 /* if the other workspace is hidden, switch windows */
1109 /* map new window first to prevent ugly blinking */
1111 old_ws->restack = 1;
1113 TAILQ_FOREACH(win, &new_ws->winlist, entry)
1114 XMapRaised(display, win->id);
1116 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1117 XUnmapWindow(display, win->id);
1119 other_r->ws = old_ws;
1120 old_ws->r = other_r;
1122 this_r->ws = new_ws;
1128 focus_win(new_ws->focus);
1134 cyclews(struct swm_region *r, union arg *args)
1137 struct swm_screen *s = r->s;
1139 DNPRINTF(SWM_D_WS, "cyclews id %d "
1140 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1141 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1146 case SWM_ARG_ID_CYCLEWS_UP:
1147 if (a.id < SWM_WS_MAX - 1)
1152 case SWM_ARG_ID_CYCLEWS_DOWN:
1156 a.id = SWM_WS_MAX - 1;
1162 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1164 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1168 } while (a.id != r->ws->idx);
1172 cyclescr(struct swm_region *r, union arg *args)
1174 struct swm_region *rr;
1179 case SWM_ARG_ID_CYCLESC_UP:
1180 rr = TAILQ_NEXT(r, entry);
1182 rr = TAILQ_FIRST(&screens[i].rl);
1184 case SWM_ARG_ID_CYCLESC_DOWN:
1185 rr = TAILQ_PREV(r, swm_region_list, entry);
1187 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1193 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1194 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, rr->g.x,
1195 rr->g.y + bar_enabled ? bar_height : 0);
1199 swapwin(struct swm_region *r, union arg *args)
1201 struct ws_win *target, *source;
1202 struct ws_win_list *wl;
1205 DNPRINTF(SWM_D_WS, "swapwin id %d "
1206 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1207 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1208 if (cur_focus == NULL)
1212 wl = &source->ws->winlist;
1215 case SWM_ARG_ID_SWAPPREV:
1216 target = TAILQ_PREV(source, ws_win_list, entry);
1217 TAILQ_REMOVE(wl, cur_focus, entry);
1219 TAILQ_INSERT_TAIL(wl, source, entry);
1221 TAILQ_INSERT_BEFORE(target, source, entry);
1223 case SWM_ARG_ID_SWAPNEXT:
1224 target = TAILQ_NEXT(source, entry);
1225 TAILQ_REMOVE(wl, source, entry);
1227 TAILQ_INSERT_HEAD(wl, source, entry);
1229 TAILQ_INSERT_AFTER(wl, target, source, entry);
1231 case SWM_ARG_ID_SWAPMAIN:
1232 target = TAILQ_FIRST(wl);
1233 if (target == source) {
1234 if (source->ws->focus_prev != NULL &&
1235 source->ws->focus_prev != target)
1237 source = source->ws->focus_prev;
1241 source->ws->focus_prev = target;
1242 TAILQ_REMOVE(wl, target, entry);
1243 TAILQ_INSERT_BEFORE(source, target, entry);
1244 TAILQ_REMOVE(wl, source, entry);
1245 TAILQ_INSERT_HEAD(wl, source, entry);
1248 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1257 focus(struct swm_region *r, union arg *args)
1259 struct ws_win *winfocus, *winlostfocus;
1260 struct ws_win_list *wl;
1262 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1263 if (cur_focus == NULL)
1266 wl = &cur_focus->ws->winlist;
1268 winlostfocus = cur_focus;
1271 case SWM_ARG_ID_FOCUSPREV:
1272 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1273 if (winfocus == NULL)
1274 winfocus = TAILQ_LAST(wl, ws_win_list);
1277 case SWM_ARG_ID_FOCUSNEXT:
1278 winfocus = TAILQ_NEXT(cur_focus, entry);
1279 if (winfocus == NULL)
1280 winfocus = TAILQ_FIRST(wl);
1283 case SWM_ARG_ID_FOCUSMAIN:
1284 winfocus = TAILQ_FIRST(wl);
1291 if (winfocus == winlostfocus || winfocus == NULL)
1294 XMapRaised(display, winfocus->id);
1295 focus_win(winfocus);
1296 XSync(display, False);
1300 cycle_layout(struct swm_region *r, union arg *args)
1302 struct workspace *ws = r->ws;
1304 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1307 if (ws->cur_layout->l_stack == NULL)
1308 ws->cur_layout = &layouts[0];
1314 stack_config(struct swm_region *r, union arg *args)
1316 struct workspace *ws = r->ws;
1318 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1321 if (ws->cur_layout->l_config != NULL)
1322 ws->cur_layout->l_config(ws, args->id);
1324 if (args->id != SWM_ARG_ID_STACKINIT);
1330 struct swm_geometry g;
1331 struct swm_region *r;
1334 DNPRINTF(SWM_D_STACK, "stack\n");
1336 for (i = 0; i < ScreenCount(display); i++) {
1338 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1339 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1340 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1342 /* start with screen geometry, adjust for bar */
1352 r->ws->cur_layout->l_stack(r->ws, &g);
1357 XSync(display, False);
1361 stack_floater(struct ws_win *win, struct swm_region *r)
1366 bzero(&wc, sizeof wc);
1367 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1368 wc.border_width = 1;
1369 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1370 win->g.w = (double)WIDTH(r) * dialog_ratio;
1371 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1373 wc.width = win->g.w;
1374 wc.height = win->g.h;
1379 wc.x = (WIDTH(r) - win->g.w) / 2;
1380 wc.y = (HEIGHT(r) - win->g.h) / 2;
1383 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1384 win->id, wc.x, wc.y, wc.width, wc.height);
1386 XConfigureWindow(display, win->id, mask, &wc);
1390 * Send keystrokes to terminal to decrease/increase the font size as the
1391 * window size changes.
1394 adjust_font(struct ws_win *win)
1396 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1397 win->floating || win->transient)
1400 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1401 win->g.w / win->sh.width_inc < term_width &&
1402 win->font_steps < SWM_MAX_FONT_STEPS) {
1403 win->font_size_boundary[win->font_steps] =
1404 (win->sh.width_inc * term_width) + win->sh.base_width;
1407 win->last_inc = win->sh.width_inc;
1408 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1409 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1410 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1413 win->last_inc = win->sh.width_inc;
1414 fake_keypress(win, XK_KP_Add, ShiftMask);
1418 #define SWAPXY(g) do { \
1420 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1421 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1424 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1427 struct swm_geometry win_g, r_g = *g;
1428 struct ws_win *win, *winfocus;
1429 int i, j, s, stacks;
1430 int w_inc = 1, h_inc, w_base = 1, h_base;
1431 int hrh, extra = 0, h_slice, last_h = 0;
1432 int split, colno, winno, mwin, msize, mscale;
1433 int remain, missing, v_slice;;
1436 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1437 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1439 if ((winno = count_win(ws, 0)) == 0)
1442 if (ws->focus == NULL)
1443 ws->focus = TAILQ_FIRST(&ws->winlist);
1444 winfocus = cur_focus ? cur_focus : ws->focus;
1446 TAILQ_FOREACH(win, &ws->winlist, entry)
1447 if (win->transient == 0 && win->floating == 0)
1454 w_inc = win->sh.width_inc;
1455 w_base = win->sh.base_width;
1456 mwin = ws->l_state.horizontal_mwin;
1457 mscale = ws->l_state.horizontal_msize;
1458 stacks = ws->l_state.horizontal_stacks;
1461 w_inc = win->sh.height_inc;
1462 w_base = win->sh.base_height;
1463 mwin = ws->l_state.vertical_mwin;
1464 mscale = ws->l_state.vertical_msize;
1465 stacks = ws->l_state.vertical_stacks;
1469 if (stacks > winno - mwin)
1470 stacks = winno - mwin;
1474 h_slice = r_g.h / SWM_H_SLICE;
1475 if (mwin && winno > mwin) {
1476 v_slice = r_g.w / SWM_V_SLICE;
1480 win_g.w = v_slice * mscale;
1482 if (w_inc > 1 && w_inc < v_slice) {
1483 /* adjust for window's requested size increment */
1484 remain = (win_g.w - w_base) % w_inc;
1485 missing = w_inc - remain;
1492 win_g.x += r_g.w - msize;
1495 colno = split = winno / stacks;
1496 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
1498 hrh = r_g.h / colno;
1499 extra = r_g.h - (colno * hrh);
1502 /* stack all the tiled windows */
1503 i = j = 0, s = stacks;
1504 TAILQ_FOREACH(win, &ws->winlist, entry) {
1505 if (win->transient != 0 || win->floating != 0)
1508 if (split && i == split) {
1509 colno = (winno - mwin) / stacks;
1510 if (s <= (winno - mwin) % stacks)
1512 split = split + colno;
1513 hrh = (r_g.h / colno);
1514 extra = r_g.h - (colno * hrh);
1518 win_g.x += win_g.w + 2;
1519 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
1521 win_g.w += (r_g.w - msize - (stacks * 2)) %
1528 h_inc = win->sh.width_inc;
1529 h_base = win->sh.base_width;
1531 h_inc = win->sh.height_inc;
1532 h_base = win->sh.base_height;
1534 if (j == colno - 1) {
1535 win_g.h = hrh + extra;
1536 } else if (h_inc > 1 && h_inc < h_slice) {
1537 /* adjust for window's requested size increment */
1538 remain = (win_g.h - h_base) % h_inc;
1539 missing = h_inc - remain;
1541 if (missing <= extra || j == 0) {
1553 win_g.y += last_h + 2;
1555 bzero(&wc, sizeof wc);
1556 wc.border_width = 1;
1558 win->g.x = wc.x = win_g.y;
1559 win->g.y = wc.y = win_g.x;
1560 win->g.w = wc.width = win_g.h;
1561 win->g.h = wc.height = win_g.w;
1563 win->g.x = wc.x = win_g.x;
1564 win->g.y = wc.y = win_g.y;
1565 win->g.w = wc.width = win_g.w;
1566 win->g.h = wc.height = win_g.h;
1569 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1570 XConfigureWindow(display, win->id, mask, &wc);
1571 XMapRaised(display, win->id);
1579 /* now, stack all the floaters and transients */
1580 TAILQ_FOREACH(win, &ws->winlist, entry) {
1581 if (win->transient == 0 && win->floating == 0)
1584 stack_floater(win, ws->r);
1585 XMapRaised(display, win->id);
1589 focus_win(winfocus); /* has to be done outside of the loop */
1593 vertical_config(struct workspace *ws, int id)
1595 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1598 case SWM_ARG_ID_STACKRESET:
1599 case SWM_ARG_ID_STACKINIT:
1600 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1601 ws->l_state.vertical_mwin = 1;
1602 ws->l_state.vertical_stacks = 1;
1604 case SWM_ARG_ID_MASTERSHRINK:
1605 if (ws->l_state.vertical_msize > 1)
1606 ws->l_state.vertical_msize--;
1608 case SWM_ARG_ID_MASTERGROW:
1609 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1610 ws->l_state.vertical_msize++;
1612 case SWM_ARG_ID_MASTERADD:
1613 ws->l_state.vertical_mwin++;
1615 case SWM_ARG_ID_MASTERDEL:
1616 if (ws->l_state.vertical_mwin > 0)
1617 ws->l_state.vertical_mwin--;
1619 case SWM_ARG_ID_STACKINC:
1620 ws->l_state.vertical_stacks++;
1622 case SWM_ARG_ID_STACKDEC:
1623 if (ws->l_state.vertical_stacks > 1)
1624 ws->l_state.vertical_stacks--;
1632 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1634 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1636 stack_master(ws, g, 0, 0);
1640 horizontal_config(struct workspace *ws, int id)
1642 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1645 case SWM_ARG_ID_STACKRESET:
1646 case SWM_ARG_ID_STACKINIT:
1647 ws->l_state.horizontal_mwin = 1;
1648 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1649 ws->l_state.horizontal_stacks = 1;
1651 case SWM_ARG_ID_MASTERSHRINK:
1652 if (ws->l_state.horizontal_msize > 1)
1653 ws->l_state.horizontal_msize--;
1655 case SWM_ARG_ID_MASTERGROW:
1656 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1657 ws->l_state.horizontal_msize++;
1659 case SWM_ARG_ID_MASTERADD:
1660 ws->l_state.horizontal_mwin++;
1662 case SWM_ARG_ID_MASTERDEL:
1663 if (ws->l_state.horizontal_mwin > 0)
1664 ws->l_state.horizontal_mwin--;
1666 case SWM_ARG_ID_STACKINC:
1667 ws->l_state.horizontal_stacks++;
1669 case SWM_ARG_ID_STACKDEC:
1670 if (ws->l_state.horizontal_stacks > 1)
1671 ws->l_state.horizontal_stacks--;
1679 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1681 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1683 stack_master(ws, g, 1, 0);
1686 /* fullscreen view */
1688 max_stack(struct workspace *ws, struct swm_geometry *g) {
1690 struct swm_geometry gg = *g;
1691 struct ws_win *win, *winfocus;
1694 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1696 if (count_win(ws, 0) == 0)
1699 if (ws->focus == NULL)
1700 ws->focus = TAILQ_FIRST(&ws->winlist);
1701 winfocus = cur_focus ? cur_focus : ws->focus;
1703 TAILQ_FOREACH(win, &ws->winlist, entry) {
1704 if (win->transient != 0 || win->floating != 0) {
1705 if (win == ws->focus) {
1707 stack_floater(win, ws->r);
1708 XMapRaised(display, win->id);
1710 XUnmapWindow(display, win->id);
1712 bzero(&wc, sizeof wc);
1713 wc.border_width = 1;
1714 win->g.x = wc.x = gg.x;
1715 win->g.y = wc.y = gg.y;
1716 win->g.w = wc.width = gg.w;
1717 win->g.h = wc.height = gg.h;
1718 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1719 XConfigureWindow(display, win->id, mask, &wc);
1721 if (win == ws->focus) {
1722 XMapRaised(display, win->id);
1724 XUnmapWindow(display, win->id);
1729 focus_win(winfocus); /* has to be done outside of the loop */
1733 send_to_ws(struct swm_region *r, union arg *args)
1735 int wsid = args->id;
1736 struct ws_win *win = cur_focus;
1737 struct workspace *ws, *nws;
1738 Atom ws_idx_atom = 0;
1739 unsigned char ws_idx_str[SWM_PROPLEN];
1744 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1747 nws = &win->s->ws[wsid];
1749 XUnmapWindow(display, win->id);
1751 /* find a window to focus */
1752 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1753 if (ws->focus == NULL)
1754 ws->focus = TAILQ_FIRST(&ws->winlist);
1755 if (ws->focus == win)
1758 TAILQ_REMOVE(&ws->winlist, win, entry);
1760 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1763 /* Try to update the window's workspace property */
1764 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1766 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1767 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1769 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1770 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1773 if (count_win(nws, 1) == 1)
1782 wkill(struct swm_region *r, union arg *args)
1784 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
1786 if(r->ws->focus == NULL)
1789 if (args->id == SWM_ARG_ID_KILLWINDOW)
1790 XKillClient(display, r->ws->focus->id);
1792 if (r->ws->focus->can_delete)
1793 client_msg(r->ws->focus, adelete);
1797 screenshot(struct swm_region *r, union arg *args)
1801 DNPRINTF(SWM_D_MISC, "screenshot\n");
1803 if (ss_enabled == 0)
1807 case SWM_ARG_ID_SS_ALL:
1808 spawn_screenshot[1] = "full";
1810 case SWM_ARG_ID_SS_WINDOW:
1811 spawn_screenshot[1] = "window";
1816 a.argv = spawn_screenshot;
1821 floating_toggle(struct swm_region *r, union arg *args)
1823 struct ws_win *win = cur_focus;
1828 win->floating = !win->floating;
1834 /* key definitions */
1838 void (*func)(struct swm_region *r, union arg *);
1841 /* modifier key function argument */
1842 { MODKEY, XK_space, cycle_layout, {0} },
1843 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1844 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1845 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1846 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1847 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1848 { MODKEY | ShiftMask, XK_comma, stack_config, {.id = SWM_ARG_ID_STACKINC} },
1849 { MODKEY | ShiftMask, XK_period, stack_config, {.id = SWM_ARG_ID_STACKDEC} },
1850 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1851 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1852 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1853 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1854 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1855 { MODKEY | ShiftMask, XK_Return, spawnterm, {.argv = spawn_term} },
1856 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1857 { MODKEY | ShiftMask, XK_q, quit, {0} },
1858 { MODKEY, XK_q, restart, {0} },
1859 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1860 { MODKEY, XK_1, switchws, {.id = 0} },
1861 { MODKEY, XK_2, switchws, {.id = 1} },
1862 { MODKEY, XK_3, switchws, {.id = 2} },
1863 { MODKEY, XK_4, switchws, {.id = 3} },
1864 { MODKEY, XK_5, switchws, {.id = 4} },
1865 { MODKEY, XK_6, switchws, {.id = 5} },
1866 { MODKEY, XK_7, switchws, {.id = 6} },
1867 { MODKEY, XK_8, switchws, {.id = 7} },
1868 { MODKEY, XK_9, switchws, {.id = 8} },
1869 { MODKEY, XK_0, switchws, {.id = 9} },
1870 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1871 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1872 { MODKEY | ShiftMask, XK_Right, cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
1873 { MODKEY | ShiftMask, XK_Left, cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
1874 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1875 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1876 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1877 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1878 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1879 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1880 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1881 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1882 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1883 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1884 { MODKEY, XK_b, bar_toggle, {0} },
1885 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1886 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1887 { MODKEY | ShiftMask, XK_x, wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
1888 { MODKEY, XK_x, wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
1889 { MODKEY, XK_s, screenshot, {.id = SWM_ARG_ID_SS_ALL} },
1890 { MODKEY | ShiftMask, XK_s, screenshot, {.id = SWM_ARG_ID_SS_WINDOW} },
1891 { MODKEY, XK_t, floating_toggle,{0} },
1892 { MODKEY | ShiftMask, XK_v, version, {0} },
1893 { MODKEY | ShiftMask, XK_Delete, spawn, {.argv = spawn_lock} },
1894 { MODKEY | ShiftMask, XK_i, spawn, {.argv = spawn_initscr} },
1898 resize_window(struct ws_win *win, int center)
1902 struct swm_region *r;
1904 r = root_to_region(win->wa.root);
1905 bzero(&wc, sizeof wc);
1906 mask = CWBorderWidth | CWWidth | CWHeight;
1907 wc.border_width = 1;
1908 wc.width = win->g.w;
1909 wc.height = win->g.h;
1910 if (center == SWM_ARG_ID_CENTER) {
1911 wc.x = (WIDTH(r) - win->g.w) / 2;
1912 wc.y = (HEIGHT(r) - win->g.h) / 2;
1916 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
1917 win->id, wc.x, wc.y, wc.width, wc.height);
1919 XConfigureWindow(display, win->id, mask, &wc);
1924 resize(struct ws_win *win, union arg *args)
1929 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %d\n",
1930 win->id, win->floating, win->transient);
1932 if (!(win->transient != 0 || win->floating != 0))
1935 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1936 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1938 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
1940 XMaskEvent(display, MOUSEMASK | ExposureMask |
1941 SubstructureRedirectMask, &ev);
1943 case ConfigureRequest:
1946 handler[ev.type](&ev);
1949 if (ev.xmotion.x <= 1)
1951 if (ev.xmotion.y <= 1)
1953 win->g.w = ev.xmotion.x;
1954 win->g.h = ev.xmotion.y;
1956 /* not free, don't sync more than 60 times / second */
1957 if ((ev.xmotion.time - time) > (1000 / 60) ) {
1958 time = ev.xmotion.time;
1959 XSync(display, False);
1960 resize_window(win, args->id);
1964 } while (ev.type != ButtonRelease);
1966 XSync(display, False);
1967 resize_window(win, args->id);
1969 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
1971 XUngrabPointer(display, CurrentTime);
1974 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1978 move_window(struct ws_win *win)
1982 struct swm_region *r;
1984 r = root_to_region(win->wa.root);
1985 bzero(&wc, sizeof wc);
1990 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
1991 win->id, wc.x, wc.y, wc.width, wc.height);
1993 XConfigureWindow(display, win->id, mask, &wc);
1998 move(struct ws_win *win, union arg *args)
2004 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %d\n",
2005 win->id, win->floating, win->transient);
2007 if (win->floating == 0) {
2013 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2014 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2016 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2018 XMaskEvent(display, MOUSEMASK | ExposureMask |
2019 SubstructureRedirectMask, &ev);
2021 case ConfigureRequest:
2024 handler[ev.type](&ev);
2027 win->g.x = ev.xmotion.x_root;
2028 win->g.y = ev.xmotion.y_root;
2030 /* not free, don't sync more than 60 times / second */
2031 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2032 time = ev.xmotion.time;
2033 XSync(display, False);
2038 } while (ev.type != ButtonRelease);
2040 XSync(display, False);
2043 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2044 XUngrabPointer(display, CurrentTime);
2049 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2053 enum { client_click, root_click };
2055 unsigned int action;
2057 unsigned int button;
2058 void (*func)(struct ws_win *, union arg *);
2061 /* action key mouse button func args */
2062 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2063 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2064 { client_click, MODKEY, Button1, move, {0} },
2068 updatenumlockmask(void)
2071 XModifierKeymap *modmap;
2073 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
2075 modmap = XGetModifierMapping(display);
2076 for (i = 0; i < 8; i++)
2077 for (j = 0; j < modmap->max_keypermod; j++)
2078 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2079 == XKeysymToKeycode(display, XK_Num_Lock))
2080 numlockmask = (1 << i);
2082 XFreeModifiermap(modmap);
2088 unsigned int i, j, k;
2090 unsigned int modifiers[] =
2091 { 0, LockMask, numlockmask, numlockmask | LockMask };
2093 DNPRINTF(SWM_D_MISC, "grabkeys\n");
2094 updatenumlockmask();
2096 for (k = 0; k < ScreenCount(display); k++) {
2097 if (TAILQ_EMPTY(&screens[k].rl))
2099 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
2100 for (i = 0; i < LENGTH(keys); i++) {
2101 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
2102 for (j = 0; j < LENGTH(modifiers); j++)
2103 XGrabKey(display, code,
2104 keys[i].mod | modifiers[j],
2105 screens[k].root, True,
2106 GrabModeAsync, GrabModeAsync);
2112 grabbuttons(struct ws_win *win, int focused)
2115 unsigned int modifiers[] =
2116 { 0, LockMask, numlockmask, numlockmask|LockMask };
2118 updatenumlockmask();
2119 XUngrabButton(display, AnyButton, AnyModifier, win->id);
2121 for (i = 0; i < LENGTH(buttons); i++)
2122 if (buttons[i].action == client_click)
2123 for (j = 0; j < LENGTH(modifiers); j++)
2124 XGrabButton(display, buttons[i].button,
2125 buttons[i].mask | modifiers[j],
2126 win->id, False, BUTTONMASK,
2127 GrabModeAsync, GrabModeSync, None,
2130 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
2131 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
2137 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
2145 XKeyEvent *ev = &e->xkey;
2147 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
2149 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
2150 for (i = 0; i < LENGTH(keys); i++)
2151 if (keysym == keys[i].keysym
2152 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
2154 keys[i].func(root_to_region(ev->root),
2159 buttonpress(XEvent *e)
2161 XButtonPressedEvent *ev = &e->xbutton;
2166 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
2168 action = root_click;
2169 if ((win = find_window(ev->window)) == NULL)
2173 action = client_click;
2176 for (i = 0; i < LENGTH(buttons); i++)
2177 if (action == buttons[i].action && buttons[i].func &&
2178 buttons[i].button == ev->button &&
2179 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
2180 buttons[i].func(win, &buttons[i].args);
2184 set_win_state(struct ws_win *win, long state)
2186 long data[] = {state, None};
2188 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
2190 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
2191 (unsigned char *)data, 2);
2195 manage_window(Window id)
2198 struct workspace *ws;
2200 int format, i, ws_idx, n;
2201 unsigned long nitems, bytes;
2202 Atom ws_idx_atom = 0, type;
2203 Atom *prot = NULL, *pp;
2204 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
2205 struct swm_region *r;
2210 if ((win = find_window(id)) != NULL)
2211 return (win); /* already being managed */
2213 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
2214 errx(1, "calloc: failed to allocate memory for new window");
2216 /* Get all the window data in one shot */
2217 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2219 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
2220 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
2221 XGetWindowAttributes(display, id, &win->wa);
2222 XGetTransientForHint(display, id, &trans);
2223 XGetWMNormalHints(display, id, &win->sh, &mask); /* XXX function? */
2225 win->transient = trans;
2226 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
2227 (unsigned)win->id, win->transient);
2229 /* get supported protocols */
2230 if (XGetWMProtocols(display, id, &prot, &n)) {
2231 for (i = 0, pp = prot; i < n; i++, pp++)
2233 win->can_delete = 1;
2239 * Figure out where to put the window. If it was previously assigned to
2240 * a workspace (either by spawn() or manually moving), and isn't
2241 * transient, * put it in the same workspace
2243 r = root_to_region(win->wa.root);
2244 if (prop && win->transient == 0) {
2245 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
2246 ws_idx = strtonum(prop, 0, 9, &errstr);
2248 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
2251 ws = &r->s->ws[ws_idx];
2255 /* set up the window layout */
2258 win->s = r->s; /* this never changes */
2259 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
2261 win->g.w = win->wa.width;
2262 win->g.h = win->wa.height;
2263 win->g.x = win->wa.x;
2264 win->g.y = win->wa.y;
2266 /* Set window properties so we can remember this after reincarnation */
2267 if (ws_idx_atom && prop == NULL &&
2268 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
2269 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2271 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2272 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2276 if (XGetClassHint(display, win->id, &win->ch)) {
2277 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
2278 win->ch.res_class, win->ch.res_name);
2279 for (i = 0; quirks[i].class != NULL && quirks[i].name != NULL &&
2280 quirks[i].quirk != 0; i++){
2281 if (!strcmp(win->ch.res_class, quirks[i].class) &&
2282 !strcmp(win->ch.res_name, quirks[i].name)) {
2283 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
2284 win->ch.res_class, win->ch.res_name);
2285 if (quirks[i].quirk & SWM_Q_FLOAT)
2287 win->quirks = quirks[i].quirk;
2292 /* alter window position if quirky */
2293 if (win->quirks & SWM_Q_ANYWHERE) {
2294 win->manual = 1; /* don't center the quirky windows */
2295 bzero(&wc, sizeof wc);
2297 if (win->g.y < bar_height) {
2298 win->g.y = wc.y = bar_height;
2301 if (win->g.w + win->g.x > WIDTH(r)) {
2302 win->g.x = wc.x = WIDTH(win->ws->r) - win->g.w - 2;
2305 wc.border_width = 1;
2306 mask |= CWBorderWidth;
2307 XConfigureWindow(display, win->id, mask, &wc);
2310 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
2311 PropertyChangeMask | StructureNotifyMask);
2313 set_win_state(win, NormalState);
2315 /* floaters need to be mapped if they are in the current workspace */
2316 if (win->floating && (ws->idx == r->ws->idx))
2317 XMapRaised(display, win->id);
2319 /* make new win focused */
2326 unmanage_window(struct ws_win *win)
2328 struct workspace *ws;
2333 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
2335 /* don't unmanage if we are switching workspaces */
2340 /* find a window to focus */
2341 if (ws->focus == win)
2342 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
2343 if (ws->focus == NULL)
2344 ws->focus = TAILQ_FIRST(&ws->winlist);
2345 if (ws->focus == NULL || ws->focus == win) {
2349 focus_win(ws->focus);
2350 if (ws->focus_prev == win)
2351 ws->focus_prev = NULL;
2353 TAILQ_REMOVE(&win->ws->winlist, win, entry);
2354 set_win_state(win, WithdrawnState);
2355 if (win->ch.res_class)
2356 XFree(win->ch.res_class);
2357 if (win->ch.res_name)
2358 XFree(win->ch.res_name);
2363 configurerequest(XEvent *e)
2365 XConfigureRequestEvent *ev = &e->xconfigurerequest;
2370 if ((win = find_window(ev->window)) == NULL)
2374 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
2376 bzero(&wc, sizeof wc);
2379 wc.width = ev->width;
2380 wc.height = ev->height;
2381 wc.border_width = ev->border_width;
2382 wc.sibling = ev->above;
2383 wc.stack_mode = ev->detail;
2384 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
2386 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
2388 if (win->floating) {
2389 if (ev->value_mask & CWX)
2391 if (ev->value_mask & CWY)
2393 if (ev->value_mask & CWWidth)
2394 win->g.w = ev->width;
2395 if (ev->value_mask & CWHeight)
2396 win->g.h = ev->height;
2397 if (win->ws->r != NULL) {
2398 /* this seems to be full screen */
2399 if (win->g.w >= WIDTH(win->ws->r)) {
2401 win->g.w = WIDTH(win->ws->r);
2402 ev->value_mask |= CWX | CWWidth;
2404 if (win->g.h >= HEIGHT(win->ws->r)) {
2407 win->g.h = HEIGHT(win->ws->r);
2408 ev->value_mask |= CWY | CWHeight;
2411 if ((ev->value_mask & (CWX | CWY)) &&
2412 !(ev->value_mask & (CWWidth | CWHeight)))
2414 XMoveResizeWindow(display, win->id,
2415 win->g.x, win->g.y, win->g.w, win->g.h);
2422 configurenotify(XEvent *e)
2427 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
2428 e->xconfigure.window);
2430 win = find_window(e->xconfigure.window);
2431 XMapWindow(display, win->id);
2432 XGetWMNormalHints(display, win->id, &win->sh, &mask);
2434 XMapWindow(display, win->id);
2440 destroynotify(XEvent *e)
2443 XDestroyWindowEvent *ev = &e->xdestroywindow;
2445 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
2447 if ((win = find_window(ev->window)) != NULL) {
2448 unmanage_window(win);
2454 enternotify(XEvent *e)
2456 XCrossingEvent *ev = &e->xcrossing;
2459 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
2462 /* eat event(r) to prevent autofocus */
2467 if ((win = find_window(ev->window)) != NULL)
2474 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
2478 mappingnotify(XEvent *e)
2480 XMappingEvent *ev = &e->xmapping;
2482 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
2484 XRefreshKeyboardMapping(ev);
2485 if (ev->request == MappingKeyboard)
2490 maprequest(XEvent *e)
2492 XMapRequestEvent *ev = &e->xmaprequest;
2493 XWindowAttributes wa;
2495 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
2496 e->xmaprequest.window);
2498 if (!XGetWindowAttributes(display, ev->window, &wa))
2500 if (wa.override_redirect)
2502 manage_window(e->xmaprequest.window);
2508 propertynotify(XEvent *e)
2511 XPropertyEvent *ev = &e->xproperty;
2513 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
2516 if (ev->state == PropertyDelete)
2517 return; /* ignore */
2518 win = find_window(ev->window);
2523 case XA_WM_NORMAL_HINTS:
2526 XGetWMNormalHints(display, win->id, &win->sh, &mask);
2527 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
2528 if (win->sh.flags & PMinSize) {
2529 win->g.w = win->sh.min_width;
2530 win->g.h = win->sh.min_height;
2531 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
2533 XMoveResizeWindow(display, win->id,
2534 win->g.x, win->g.y, win->g.w, win->g.h);
2543 unmapnotify(XEvent *e)
2545 XDestroyWindowEvent *ev = &e->xdestroywindow;
2548 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
2550 if ((win = find_window(ev->window)) != NULL)
2552 unmanage_window(win);
2556 visibilitynotify(XEvent *e)
2559 struct swm_region *r;
2561 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
2562 e->xvisibility.window);
2563 if (e->xvisibility.state == VisibilityUnobscured)
2564 for (i = 0; i < ScreenCount(display); i++)
2565 TAILQ_FOREACH(r, &screens[i].rl, entry)
2566 if (e->xvisibility.window == r->bar_window)
2571 xerror_start(Display *d, XErrorEvent *ee)
2578 xerror(Display *d, XErrorEvent *ee)
2580 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2588 xerrorxlib = XSetErrorHandler(xerror_start);
2590 /* this causes an error if some other window manager is running */
2591 XSelectInput(display, DefaultRootWindow(display),
2592 SubstructureRedirectMask);
2593 XSync(display, False);
2597 XSetErrorHandler(xerror);
2598 XSync(display, False);
2607 unsigned char *p = NULL;
2608 unsigned long n, extra;
2611 astate = XInternAtom(display, "WM_STATE", False);
2612 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
2613 &real, &format, &n, &extra, (unsigned char **)&p);
2614 if (status != Success)
2623 new_region(struct swm_screen *s, int x, int y, int w, int h)
2625 struct swm_region *r, *n;
2626 struct workspace *ws = NULL;
2629 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
2630 s->idx, w, h, x, y);
2632 /* remove any conflicting regions */
2633 n = TAILQ_FIRST(&s->rl);
2636 n = TAILQ_NEXT(r, entry);
2637 if (X(r) < (x + w) &&
2638 (X(r) + WIDTH(r)) > x &&
2640 (Y(r) + HEIGHT(r)) > y) {
2641 XDestroyWindow(display, r->bar_window);
2642 TAILQ_REMOVE(&s->rl, r, entry);
2643 TAILQ_INSERT_TAIL(&s->orl, r, entry);
2647 /* search old regions for one to reuse */
2649 /* size + location match */
2650 TAILQ_FOREACH(r, &s->orl, entry)
2651 if (X(r) == x && Y(r) == y &&
2652 HEIGHT(r) == h && WIDTH(r) == w)
2656 TAILQ_FOREACH(r, &s->orl, entry)
2657 if (HEIGHT(r) == h && WIDTH(r) == w)
2661 TAILQ_REMOVE(&s->orl, r, entry);
2662 /* try to use old region's workspace */
2663 if (r->ws->r == NULL)
2666 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
2667 errx(1, "calloc: failed to allocate memory for screen");
2669 /* if we don't have a workspace already, find one */
2671 for (i = 0; i < SWM_WS_MAX; i++)
2672 if (s->ws[i].r == NULL) {
2679 errx(1, "no free workspaces\n");
2688 TAILQ_INSERT_TAIL(&s->rl, r, entry);
2694 #ifdef SWM_XRR_HAS_CRTC
2696 XRRScreenResources *sr;
2699 #endif /* SWM_XRR_HAS_CRTC */
2700 struct swm_region *r;
2703 if (i >= ScreenCount(display))
2704 errx(1, "invalid screen");
2706 /* remove any old regions */
2707 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
2709 XDestroyWindow(display, r->bar_window);
2710 TAILQ_REMOVE(&screens[i].rl, r, entry);
2711 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
2714 /* map virtual screens onto physical screens */
2715 #ifdef SWM_XRR_HAS_CRTC
2716 if (xrandr_support) {
2717 sr = XRRGetScreenResources(display, screens[i].root);
2719 new_region(&screens[i], 0, 0,
2720 DisplayWidth(display, i),
2721 DisplayHeight(display, i));
2725 for (c = 0, ci = NULL; c < ncrtc; c++) {
2726 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
2727 if (ci->noutput == 0)
2730 if (ci != NULL && ci->mode == None)
2731 new_region(&screens[i], 0, 0,
2732 DisplayWidth(display, i),
2733 DisplayHeight(display, i));
2735 new_region(&screens[i],
2736 ci->x, ci->y, ci->width, ci->height);
2739 XRRFreeCrtcInfo(ci);
2740 XRRFreeScreenResources(sr);
2742 #endif /* SWM_XRR_HAS_CRTC */
2744 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
2745 DisplayHeight(display, i));
2750 screenchange(XEvent *e) {
2751 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
2752 struct swm_region *r;
2756 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
2758 if (!XRRUpdateConfiguration(e))
2761 /* silly event doesn't include the screen index */
2762 for (i = 0; i < ScreenCount(display); i++)
2763 if (screens[i].root == xe->root)
2765 if (i >= ScreenCount(display))
2766 errx(1, "screenchange: screen not found\n");
2768 /* brute force for now, just re-enumerate the regions */
2771 /* hide any windows that went away */
2772 TAILQ_FOREACH(r, &screens[i].rl, entry)
2773 TAILQ_FOREACH(win, &r->ws->winlist, entry)
2774 XUnmapWindow(display, win->id);
2781 Window d1, d2, *wins = NULL;
2782 XWindowAttributes wa;
2785 int errorbase, major, minor;
2786 struct workspace *ws;
2790 if ((screens = calloc(ScreenCount(display),
2791 sizeof(struct swm_screen))) == NULL)
2792 errx(1, "calloc: screens");
2794 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2796 /* initial Xrandr setup */
2797 xrandr_support = XRRQueryExtension(display,
2798 &xrandr_eventbase, &errorbase);
2800 if (XRRQueryVersion(display, &major, &minor) && major < 1)
2803 /* map physical screens */
2804 for (i = 0; i < ScreenCount(display); i++) {
2805 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
2807 TAILQ_INIT(&screens[i].rl);
2808 TAILQ_INIT(&screens[i].orl);
2809 screens[i].root = RootWindow(display, i);
2811 /* set default colors */
2812 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
2813 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
2814 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
2815 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
2816 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
2818 /* init all workspaces */
2819 /* XXX these should be dynamically allocated too */
2820 for (j = 0; j < SWM_WS_MAX; j++) {
2821 ws = &screens[i].ws[j];
2826 TAILQ_INIT(&ws->winlist);
2828 for (k = 0; layouts[k].l_stack != NULL; k++)
2829 if (layouts[k].l_config != NULL)
2830 layouts[k].l_config(ws,
2831 SWM_ARG_ID_STACKINIT);
2832 ws->cur_layout = &layouts[0];
2834 /* grab existing windows (before we build the bars)*/
2835 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
2841 XRRSelectInput(display, screens[i].root,
2842 RRScreenChangeNotifyMask);
2844 /* attach windows to a region */
2845 /* normal windows */
2846 for (j = 0; j < no; j++) {
2847 XGetWindowAttributes(display, wins[j], &wa);
2848 if (!XGetWindowAttributes(display, wins[j], &wa) ||
2849 wa.override_redirect ||
2850 XGetTransientForHint(display, wins[j], &d1))
2853 if (wa.map_state == IsViewable ||
2854 getstate(wins[j]) == NormalState)
2855 manage_window(wins[j]);
2857 /* transient windows */
2858 for (j = 0; j < no; j++) {
2859 if (!XGetWindowAttributes(display, wins[j], &wa))
2862 if (XGetTransientForHint(display, wins[j], &d1) &&
2863 (wa.map_state == IsViewable || getstate(wins[j]) ==
2865 manage_window(wins[j]);
2875 main(int argc, char *argv[])
2878 struct swm_region *r;
2879 char conf[PATH_MAX], *cfile = NULL;
2886 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
2887 SWM_VERSION, cvstag);
2888 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2889 warnx("no locale support");
2891 if (!(display = XOpenDisplay(0)))
2892 errx(1, "can not open display");
2895 errx(1, "other wm running");
2897 astate = XInternAtom(display, "WM_STATE", False);
2898 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
2899 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
2901 /* look for local and global conf file */
2902 pwd = getpwuid(getuid());
2904 errx(1, "invalid user %d", getuid());
2908 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2909 if (stat(conf, &sb) != -1) {
2910 if (S_ISREG(sb.st_mode))
2913 /* try global conf file */
2914 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2915 if (!stat(conf, &sb))
2916 if (S_ISREG(sb.st_mode))
2922 /* setup all bars */
2923 for (i = 0; i < ScreenCount(display); i++)
2924 TAILQ_FOREACH(r, &screens[i].rl, entry)
2927 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2932 xfd = ConnectionNumber(display);
2936 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2938 errx(1, "select failed");
2943 while (XPending(display)) {
2944 XNextEvent(display, &e);
2945 if (e.type < LASTEvent) {
2946 if (handler[e.type])
2947 handler[e.type](&e);
2949 DNPRINTF(SWM_D_EVENT,
2950 "win: %lu unknown event: %d\n",
2951 e.xany.window, e.type);
2953 switch (e.type - xrandr_eventbase) {
2954 case RRScreenChangeNotify:
2958 DNPRINTF(SWM_D_EVENT,
2959 "win: %lu unknown xrandr event: "
2960 "%d\n", e.xany.window, e.type);
2967 XCloseDisplay(display);