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 (256)
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);
252 void new_region(struct swm_screen *, int, int, int, int);
255 void (*l_stack)(struct workspace *, struct swm_geometry *);
256 void (*l_config)(struct workspace *, int);
258 /* stack, configure */
259 { vertical_stack, vertical_config},
260 { horizontal_stack, horizontal_config},
265 #define SWM_H_SLICE (32)
266 #define SWM_V_SLICE (32)
268 /* define work spaces */
270 int idx; /* workspace index */
271 int restack; /* restack on switch */
272 struct layout *cur_layout; /* current layout handlers */
273 struct ws_win *focus; /* may be NULL */
274 struct ws_win *focus_prev; /* may be NULL */
275 struct swm_region *r; /* may be NULL */
276 struct ws_win_list winlist; /* list of windows in ws */
280 int horizontal_msize;
282 int horizontal_stacks;
289 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
290 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
292 /* physical screen mapping */
293 #define SWM_WS_MAX (10) /* XXX Too small? */
295 int idx; /* screen index */
296 struct swm_region_list rl; /* list of regions on this screen */
297 struct swm_region_list orl; /* list of old regions */
299 struct workspace ws[SWM_WS_MAX];
305 } c[SWM_S_COLOR_MAX];
307 struct swm_screen *screens;
310 struct ws_win *cur_focus = NULL;
312 /* args to functions */
315 #define SWM_ARG_ID_FOCUSNEXT (0)
316 #define SWM_ARG_ID_FOCUSPREV (1)
317 #define SWM_ARG_ID_FOCUSMAIN (2)
318 #define SWM_ARG_ID_SWAPNEXT (3)
319 #define SWM_ARG_ID_SWAPPREV (4)
320 #define SWM_ARG_ID_SWAPMAIN (5)
321 #define SWM_ARG_ID_MASTERSHRINK (6)
322 #define SWM_ARG_ID_MASTERGROW (7)
323 #define SWM_ARG_ID_MASTERADD (8)
324 #define SWM_ARG_ID_MASTERDEL (9)
325 #define SWM_ARG_ID_STACKRESET (10)
326 #define SWM_ARG_ID_STACKINIT (11)
327 #define SWM_ARG_ID_CYCLEWS_UP (12)
328 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
329 #define SWM_ARG_ID_CYCLESC_UP (14)
330 #define SWM_ARG_ID_CYCLESC_DOWN (15)
331 #define SWM_ARG_ID_STACKINC (16)
332 #define SWM_ARG_ID_STACKDEC (17)
333 #define SWM_ARG_ID_SS_ALL (0)
334 #define SWM_ARG_ID_SS_WINDOW (1)
335 #define SWM_ARG_ID_DONTCENTER (0)
336 #define SWM_ARG_ID_CENTER (1)
337 #define SWM_ARG_ID_KILLWINDOW (0)
338 #define SWM_ARG_ID_DELETEWINDOW (1)
347 #define SWM_Q_FLOAT (1<<0) /* float this window */
348 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
349 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
350 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
351 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
353 { "MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN },
354 { "OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT },
355 { "OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT },
356 { "Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ },
357 { "Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE },
358 { "XTerm", "xterm", SWM_Q_XTERM_FONTADJ },
359 { "xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE },
360 { "Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE },
361 { "xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE },
362 { "Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE },
363 { "xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT },
364 { "pcb", "pcb", SWM_Q_FLOAT },
369 void expose(XEvent *);
370 void keypress(XEvent *);
371 void buttonpress(XEvent *);
372 void configurerequest(XEvent *);
373 void configurenotify(XEvent *);
374 void destroynotify(XEvent *);
375 void enternotify(XEvent *);
376 void focusin(XEvent *);
377 void mappingnotify(XEvent *);
378 void maprequest(XEvent *);
379 void propertynotify(XEvent *);
380 void unmapnotify(XEvent *);
381 void visibilitynotify(XEvent *);
383 void (*handler[LASTEvent])(XEvent *) = {
385 [KeyPress] = keypress,
386 [ButtonPress] = buttonpress,
387 [ConfigureRequest] = configurerequest,
388 [ConfigureNotify] = configurenotify,
389 [DestroyNotify] = destroynotify,
390 [EnterNotify] = enternotify,
392 [MappingNotify] = mappingnotify,
393 [MapRequest] = maprequest,
394 [PropertyNotify] = propertynotify,
395 [UnmapNotify] = unmapnotify,
396 [VisibilityNotify] = visibilitynotify,
400 name_to_color(char *colorname)
404 XColor screen_def, exact_def;
405 unsigned long result = 0;
406 char cname[32] = "#";
408 cmap = DefaultColormap(display, screens[0].idx);
409 status = XAllocNamedColor(display, cmap, colorname,
410 &screen_def, &exact_def);
412 strlcat(cname, colorname + 2, sizeof cname - 1);
413 status = XAllocNamedColor(display, cmap, cname, &screen_def,
417 result = screen_def.pixel;
419 fprintf(stderr, "color '%s' not found.\n", colorname);
425 setscreencolor(char *val, int i, int c)
427 if (i > 0 && i <= ScreenCount(display)) {
428 screens[i - 1].c[c].color = name_to_color(val);
429 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
431 } else if (i == -1) {
432 for (i = 0; i < ScreenCount(display); i++)
433 screens[i].c[c].color = name_to_color(val);
434 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
437 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
438 i, ScreenCount(display));
442 custom_region(char *val)
444 unsigned int sidx, x, y, w, h;
446 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
447 errx(1, "invalid custom region, "
448 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
449 if (sidx < 1 || sidx > ScreenCount(display))
450 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
451 sidx, ScreenCount(display));
455 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
457 if (x < 0 || x > DisplayWidth(display, sidx) ||
458 y < 0 || y > DisplayHeight(display, sidx) ||
459 w + x > DisplayWidth(display, sidx) ||
460 h + y > DisplayHeight(display, sidx))
461 errx(1, "region %ux%u+%u+%u not within screen boundaries "
462 "(%ux%u)\n", w, h, x, y,
463 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
465 new_region(&screens[sidx], x, y, w, h);
469 varmatch(char *var, char *name, int *index)
474 i = strncmp(var, name, 255);
481 p = var + strlen(name);
484 bzero(buf, sizeof buf);
486 while (isdigit(*p) && i < sizeof buf)
488 if (i == 0 || i >= sizeof buf || *p != ']')
490 *index = strtonum(buf, 0, 99, NULL);
494 /* conf file stuff */
495 #define SWM_CONF_WS "\n= \t"
496 #define SWM_CONF_FILE "scrotwm.conf"
498 conf_load(char *filename)
501 char *line, *cp, *var, *val;
502 size_t len, lineno = 0;
505 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
507 if (filename == NULL)
510 if ((config = fopen(filename, "r")) == NULL)
513 for (sc = ScreenCount(display);;) {
514 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
518 cp += (long)strspn(cp, SWM_CONF_WS);
524 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
526 cp += (long)strspn(cp, SWM_CONF_WS);
527 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
530 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
533 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
534 bar_enabled = atoi(val);
535 else if (!varmatch(var, "bar_border", &i))
536 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
537 else if (!varmatch(var, "bar_color", &i))
538 setscreencolor(val, i, SWM_S_COLOR_BAR);
539 else if (!varmatch(var, "bar_font_color", &i))
540 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
541 else if (!strncmp(var, "bar_font", strlen("bar_font")))
542 asprintf(&bar_fonts[0], "%s", val);
543 else if (!strncmp(var, "bar_action", strlen("bar_action")))
544 asprintf(&bar_argv[0], "%s", val);
545 else if (!strncmp(var, "bar_delay", strlen("bar_delay")))
546 bar_delay = atoi(val);
552 if (!varmatch(var, "color_focus", &i))
553 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
554 else if (!varmatch(var, "color_unfocus", &i))
555 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
556 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
557 cycle_visible = atoi(val);
558 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
559 cycle_visible = atoi(val);
565 if (!strncmp(var, "dialog_ratio",
566 strlen("dialog_ratio"))) {
567 dialog_ratio = atof(val);
568 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
575 if (!strncmp(var, "region", strlen("region")))
582 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
583 asprintf(&spawn_term[0], "%s", val);
584 else if (!strncmp(var, "screenshot_enabled",
585 strlen("screenshot_enabled")))
586 ss_enabled = atoi(val);
587 else if (!strncmp(var, "screenshot_app",
588 strlen("screenshot_app")))
589 asprintf(&spawn_screenshot[0], "%s", val);
595 if (!strncmp(var, "term_width", strlen("term_width")))
596 term_width = atoi(val);
611 errx(1, "invalid conf file entry: %s=%s", var, val);
615 socket_setnonblock(int fd)
619 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
620 err(1, "fcntl F_GETFL");
622 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
623 err(1, "fcntl F_SETFL");
627 bar_print(struct swm_region *r, char *s)
629 XClearWindow(display, r->bar_window);
630 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
631 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
640 bzero(bar_pipe, sizeof bar_pipe);
643 kill(bar_pid, SIGTERM);
646 strlcpy(bar_ext, "", sizeof bar_ext);
655 struct swm_region *r;
659 char loc[SWM_BAR_MAX];
662 if (bar_enabled == 0)
664 if (bar_extra && bar_extra_running) {
665 /* ignore short reads; it'll correct itself */
666 while ((b = fgetln(stdin, &len)) != NULL)
667 if (b && b[len - 1] == '\n') {
669 strlcpy(bar_ext, b, sizeof bar_ext);
671 if (b == NULL && errno != EAGAIN) {
672 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
673 errno, strerror(errno));
677 strlcpy(bar_ext, "", sizeof bar_ext);
680 localtime_r(&tmt, &tm);
681 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
682 for (i = 0; i < ScreenCount(display); i++) {
684 TAILQ_FOREACH(r, &screens[i].rl, entry) {
685 snprintf(loc, sizeof loc, "%s %d:%d %s %s",
686 s, x++, r->ws->idx + 1, bar_ext, bar_vertext);
690 XSync(display, False);
701 bar_toggle(struct swm_region *r, union arg *args)
703 struct swm_region *tmpr;
704 int i, j, sc = ScreenCount(display);
706 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
709 for (i = 0; i < sc; i++)
710 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
711 XUnmapWindow(display, tmpr->bar_window);
713 for (i = 0; i < sc; i++)
714 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
715 XMapRaised(display, tmpr->bar_window);
717 bar_enabled = !bar_enabled;
718 for (i = 0; i < sc; i++)
719 for (j = 0; j < SWM_WS_MAX; j++)
720 screens[i].ws[j].restack = 1;
723 /* must be after stack */
730 XSetWindowAttributes wa;
731 struct swm_region *r;
734 /* do this here because the conf file is in memory */
735 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
736 /* launch external status app */
737 bar_extra_running = 1;
738 if (pipe(bar_pipe) == -1)
739 err(1, "pipe error");
740 socket_setnonblock(bar_pipe[0]);
741 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
742 if (dup2(bar_pipe[0], 0) == -1)
744 if (dup2(bar_pipe[1], 1) == -1)
746 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
747 err(1, "could not disable SIGPIPE");
748 switch (bar_pid = fork()) {
750 err(1, "cannot fork");
754 execvp(bar_argv[0], bar_argv);
755 err(1, "%s external app failed", bar_argv[0]);
757 default: /* parent */
763 bzero(&wa, sizeof wa);
764 for (i = 0; i < ScreenCount(display); i++)
765 TAILQ_FOREACH(r, &screens[i].rl, entry) {
767 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
768 wa.background_pixel =
769 screens[i].c[SWM_S_COLOR_BAR].color;
770 XChangeWindowAttributes(display, r->bar_window,
771 CWBackPixel | CWBorderPixel, &wa);
777 bar_setup(struct swm_region *r)
781 for (i = 0; bar_fonts[i] != NULL; i++) {
782 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
788 if (bar_fonts[i] == NULL)
789 errx(1, "couldn't load font");
790 bar_height = bar_fs->ascent + bar_fs->descent + 3;
792 r->bar_window = XCreateSimpleWindow(display,
793 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
794 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
795 r->s->c[SWM_S_COLOR_BAR].color);
796 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
797 XSetFont(display, bar_gc, bar_fs->fid);
798 XSelectInput(display, r->bar_window, VisibilityChangeMask);
800 XMapRaised(display, r->bar_window);
801 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
803 if (signal(SIGALRM, bar_signal) == SIG_ERR)
804 err(1, "could not install bar_signal");
809 version(struct swm_region *r, union arg *args)
811 bar_version = !bar_version;
813 strlcpy(bar_vertext, cvstag, sizeof bar_vertext);
815 strlcpy(bar_vertext, "", sizeof bar_vertext);
820 client_msg(struct ws_win *win, Atom a)
822 XClientMessageEvent cm;
824 bzero(&cm, sizeof cm);
825 cm.type = ClientMessage;
827 cm.message_type = aprot;
830 cm.data.l[1] = CurrentTime;
831 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
835 config_win(struct ws_win *win)
839 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
840 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
841 ce.type = ConfigureNotify;
842 ce.display = display;
848 ce.height = win->g.h;
849 ce.border_width = 1; /* XXX store this! */
851 ce.override_redirect = False;
852 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
856 count_win(struct workspace *ws, int count_transient)
861 TAILQ_FOREACH(win, &ws->winlist, entry) {
862 if (count_transient == 0 && win->floating)
864 if (count_transient == 0 && win->transient)
868 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
874 quit(struct swm_region *r, union arg *args)
876 DNPRINTF(SWM_D_MISC, "quit\n");
886 for (i = 0; i < ScreenCount(display); i++)
887 for (j = 0; j < SWM_WS_MAX; j++)
888 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
889 XUnmapWindow(display, win->id);
893 fake_keypress(struct ws_win *win, int keysym, int modifiers)
897 event.display = display; /* Ignored, but what the hell */
898 event.window = win->id;
899 event.root = win->s->root;
900 event.subwindow = None;
901 event.time = CurrentTime;
906 event.same_screen = True;
907 event.keycode = XKeysymToKeycode(display, keysym);
908 event.state = modifiers;
910 event.type = KeyPress;
911 XSendEvent(event.display, event.window, True,
912 KeyPressMask, (XEvent *)&event);
914 event.type = KeyRelease;
915 XSendEvent(event.display, event.window, True,
916 KeyPressMask, (XEvent *)&event);
921 restart(struct swm_region *r, union arg *args)
923 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
925 /* disable alarm because the following code may not be interrupted */
927 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
928 errx(1, "can't disable alarm");
933 XCloseDisplay(display);
934 execvp(start_argv[0], start_argv);
935 fprintf(stderr, "execvp failed\n");
941 root_to_region(Window root)
943 struct swm_region *r = NULL;
948 for (i = 0; i < ScreenCount(display); i++)
949 if (screens[i].root == root)
952 if (XQueryPointer(display, screens[i].root,
953 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
954 /* choose a region based on pointer location */
955 TAILQ_FOREACH(r, &screens[i].rl, entry)
956 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
957 y >= Y(r) && y <= Y(r) + HEIGHT(r))
962 r = TAILQ_FIRST(&screens[i].rl);
968 find_window(Window id)
973 for (i = 0; i < ScreenCount(display); i++)
974 for (j = 0; j < SWM_WS_MAX; j++)
975 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
982 spawn(struct swm_region *r, union arg *args)
987 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
989 * The double-fork construct avoids zombie processes and keeps the code
990 * clean from stupid signal handlers.
995 close(ConnectionNumber(display));
996 setenv("LD_PRELOAD", SWM_LIB, 1);
997 if (asprintf(&ret, "%d", r->ws->idx)) {
998 setenv("_SWM_WS", ret, 1);
1001 if (asprintf(&ret, "%d", getpid())) {
1002 setenv("_SWM_PID", ret, 1);
1006 /* kill stdin, mplayer, ssh-add etc. need that */
1007 si = open("/dev/null", O_RDONLY, 0);
1009 err(1, "open /dev/null");
1010 if (dup2(si, 0) == -1)
1011 err(1, "dup2 /dev/null");
1012 execvp(args->argv[0], args->argv);
1013 fprintf(stderr, "execvp failed\n");
1022 spawnterm(struct swm_region *r, union arg *args)
1024 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1027 setenv("_SWM_XTERM_FONTADJ", "", 1);
1032 spawnmenu(struct swm_region *r, union arg *args)
1034 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
1036 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
1037 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
1038 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
1039 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
1040 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
1051 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
1053 for (i = 0; i < ScreenCount(display); i++)
1054 for (j = 0; j < SWM_WS_MAX; j++)
1055 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1056 if (win->ws->r == NULL)
1058 grabbuttons(win, 0);
1059 XSetWindowBorder(display, win->id,
1060 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1062 win->ws->focus = NULL;
1068 focus_win(struct ws_win *win)
1070 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1075 if (win->ws->focus != win && win->ws->focus != NULL)
1076 win->ws->focus_prev = win->ws->focus;
1079 win->ws->focus = win;
1080 if (win->ws->r != NULL) {
1082 if (!win->got_focus) {
1083 XSetWindowBorder(display, win->id,
1084 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1085 grabbuttons(win, 1);
1088 XSetInputFocus(display, win->id,
1089 RevertToPointerRoot, CurrentTime);
1094 switchws(struct swm_region *r, union arg *args)
1096 int wsid = args->id;
1097 struct swm_region *this_r, *other_r;
1099 struct workspace *new_ws, *old_ws;
1102 old_ws = this_r->ws;
1103 new_ws = &this_r->s->ws[wsid];
1105 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1106 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1109 if (new_ws == old_ws)
1112 other_r = new_ws->r;
1114 /* if the other workspace is hidden, switch windows */
1115 /* map new window first to prevent ugly blinking */
1117 old_ws->restack = 1;
1119 TAILQ_FOREACH(win, &new_ws->winlist, entry)
1120 XMapRaised(display, win->id);
1122 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1123 XUnmapWindow(display, win->id);
1125 other_r->ws = old_ws;
1126 old_ws->r = other_r;
1128 this_r->ws = new_ws;
1134 focus_win(new_ws->focus);
1140 cyclews(struct swm_region *r, union arg *args)
1143 struct swm_screen *s = r->s;
1145 DNPRINTF(SWM_D_WS, "cyclews id %d "
1146 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1147 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1152 case SWM_ARG_ID_CYCLEWS_UP:
1153 if (a.id < SWM_WS_MAX - 1)
1158 case SWM_ARG_ID_CYCLEWS_DOWN:
1162 a.id = SWM_WS_MAX - 1;
1168 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1170 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1174 } while (a.id != r->ws->idx);
1178 cyclescr(struct swm_region *r, union arg *args)
1180 struct swm_region *rr;
1185 case SWM_ARG_ID_CYCLESC_UP:
1186 rr = TAILQ_NEXT(r, entry);
1188 rr = TAILQ_FIRST(&screens[i].rl);
1190 case SWM_ARG_ID_CYCLESC_DOWN:
1191 rr = TAILQ_PREV(r, swm_region_list, entry);
1193 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1199 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1200 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, rr->g.x,
1201 rr->g.y + bar_enabled ? bar_height : 0);
1205 swapwin(struct swm_region *r, union arg *args)
1207 struct ws_win *target, *source;
1208 struct ws_win_list *wl;
1211 DNPRINTF(SWM_D_WS, "swapwin id %d "
1212 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1213 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1214 if (cur_focus == NULL)
1218 wl = &source->ws->winlist;
1221 case SWM_ARG_ID_SWAPPREV:
1222 target = TAILQ_PREV(source, ws_win_list, entry);
1223 TAILQ_REMOVE(wl, cur_focus, entry);
1225 TAILQ_INSERT_TAIL(wl, source, entry);
1227 TAILQ_INSERT_BEFORE(target, source, entry);
1229 case SWM_ARG_ID_SWAPNEXT:
1230 target = TAILQ_NEXT(source, entry);
1231 TAILQ_REMOVE(wl, source, entry);
1233 TAILQ_INSERT_HEAD(wl, source, entry);
1235 TAILQ_INSERT_AFTER(wl, target, source, entry);
1237 case SWM_ARG_ID_SWAPMAIN:
1238 target = TAILQ_FIRST(wl);
1239 if (target == source) {
1240 if (source->ws->focus_prev != NULL &&
1241 source->ws->focus_prev != target)
1243 source = source->ws->focus_prev;
1247 source->ws->focus_prev = target;
1248 TAILQ_REMOVE(wl, target, entry);
1249 TAILQ_INSERT_BEFORE(source, target, entry);
1250 TAILQ_REMOVE(wl, source, entry);
1251 TAILQ_INSERT_HEAD(wl, source, entry);
1254 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1263 focus(struct swm_region *r, union arg *args)
1265 struct ws_win *winfocus, *winlostfocus;
1266 struct ws_win_list *wl;
1268 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1269 if (cur_focus == NULL)
1272 wl = &cur_focus->ws->winlist;
1274 winlostfocus = cur_focus;
1277 case SWM_ARG_ID_FOCUSPREV:
1278 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1279 if (winfocus == NULL)
1280 winfocus = TAILQ_LAST(wl, ws_win_list);
1283 case SWM_ARG_ID_FOCUSNEXT:
1284 winfocus = TAILQ_NEXT(cur_focus, entry);
1285 if (winfocus == NULL)
1286 winfocus = TAILQ_FIRST(wl);
1289 case SWM_ARG_ID_FOCUSMAIN:
1290 winfocus = TAILQ_FIRST(wl);
1291 if (winfocus == cur_focus)
1292 winfocus = cur_focus->ws->focus_prev;
1293 if (winfocus == NULL)
1301 if (winfocus == winlostfocus || winfocus == NULL)
1304 XMapRaised(display, winfocus->id);
1305 focus_win(winfocus);
1306 XSync(display, False);
1310 cycle_layout(struct swm_region *r, union arg *args)
1312 struct workspace *ws = r->ws;
1314 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1317 if (ws->cur_layout->l_stack == NULL)
1318 ws->cur_layout = &layouts[0];
1324 stack_config(struct swm_region *r, union arg *args)
1326 struct workspace *ws = r->ws;
1328 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1331 if (ws->cur_layout->l_config != NULL)
1332 ws->cur_layout->l_config(ws, args->id);
1334 if (args->id != SWM_ARG_ID_STACKINIT);
1340 struct swm_geometry g;
1341 struct swm_region *r;
1344 DNPRINTF(SWM_D_STACK, "stack\n");
1346 for (i = 0; i < ScreenCount(display); i++) {
1348 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1349 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1350 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1352 /* start with screen geometry, adjust for bar */
1362 r->ws->cur_layout->l_stack(r->ws, &g);
1367 XSync(display, False);
1371 stack_floater(struct ws_win *win, struct swm_region *r)
1376 bzero(&wc, sizeof wc);
1377 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1378 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w == WIDTH(r)) &&
1379 (win->g.h == HEIGHT(r)))
1380 wc.border_width = 0;
1382 wc.border_width = 1;
1383 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1384 win->g.w = (double)WIDTH(r) * dialog_ratio;
1385 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1387 wc.width = win->g.w;
1388 wc.height = win->g.h;
1393 wc.x = (WIDTH(r) - win->g.w) / 2;
1394 wc.y = (HEIGHT(r) - win->g.h) / 2;
1397 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1398 win->id, wc.x, wc.y, wc.width, wc.height);
1400 XConfigureWindow(display, win->id, mask, &wc);
1404 * Send keystrokes to terminal to decrease/increase the font size as the
1405 * window size changes.
1408 adjust_font(struct ws_win *win)
1410 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1411 win->floating || win->transient)
1414 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1415 win->g.w / win->sh.width_inc < term_width &&
1416 win->font_steps < SWM_MAX_FONT_STEPS) {
1417 win->font_size_boundary[win->font_steps] =
1418 (win->sh.width_inc * term_width) + win->sh.base_width;
1421 win->last_inc = win->sh.width_inc;
1422 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1423 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1424 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1427 win->last_inc = win->sh.width_inc;
1428 fake_keypress(win, XK_KP_Add, ShiftMask);
1432 #define SWAPXY(g) do { \
1434 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1435 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1438 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1441 struct swm_geometry win_g, r_g = *g;
1442 struct ws_win *win, *winfocus;
1443 int i, j, s, stacks;
1444 int w_inc = 1, h_inc, w_base = 1, h_base;
1445 int hrh, extra = 0, h_slice, last_h = 0;
1446 int split, colno, winno, mwin, msize, mscale;
1447 int remain, missing, v_slice;;
1450 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1451 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1453 if ((winno = count_win(ws, 0)) == 0)
1456 if (ws->focus == NULL)
1457 ws->focus = TAILQ_FIRST(&ws->winlist);
1458 winfocus = cur_focus ? cur_focus : ws->focus;
1460 TAILQ_FOREACH(win, &ws->winlist, entry)
1461 if (win->transient == 0 && win->floating == 0)
1468 w_inc = win->sh.width_inc;
1469 w_base = win->sh.base_width;
1470 mwin = ws->l_state.horizontal_mwin;
1471 mscale = ws->l_state.horizontal_msize;
1472 stacks = ws->l_state.horizontal_stacks;
1475 w_inc = win->sh.height_inc;
1476 w_base = win->sh.base_height;
1477 mwin = ws->l_state.vertical_mwin;
1478 mscale = ws->l_state.vertical_msize;
1479 stacks = ws->l_state.vertical_stacks;
1483 if (stacks > winno - mwin)
1484 stacks = winno - mwin;
1488 h_slice = r_g.h / SWM_H_SLICE;
1489 if (mwin && winno > mwin) {
1490 v_slice = r_g.w / SWM_V_SLICE;
1494 win_g.w = v_slice * mscale;
1496 if (w_inc > 1 && w_inc < v_slice) {
1497 /* adjust for window's requested size increment */
1498 remain = (win_g.w - w_base) % w_inc;
1499 missing = w_inc - remain;
1506 win_g.x += r_g.w - msize;
1509 colno = split = winno / stacks;
1510 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
1512 hrh = r_g.h / colno;
1513 extra = r_g.h - (colno * hrh);
1516 /* stack all the tiled windows */
1517 i = j = 0, s = stacks;
1518 TAILQ_FOREACH(win, &ws->winlist, entry) {
1519 if (win->transient != 0 || win->floating != 0)
1522 if (split && i == split) {
1523 colno = (winno - mwin) / stacks;
1524 if (s <= (winno - mwin) % stacks)
1526 split = split + colno;
1527 hrh = (r_g.h / colno);
1528 extra = r_g.h - (colno * hrh);
1532 win_g.x += win_g.w + 2;
1533 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
1535 win_g.w += (r_g.w - msize - (stacks * 2)) %
1542 h_inc = win->sh.width_inc;
1543 h_base = win->sh.base_width;
1545 h_inc = win->sh.height_inc;
1546 h_base = win->sh.base_height;
1548 if (j == colno - 1) {
1549 win_g.h = hrh + extra;
1550 } else if (h_inc > 1 && h_inc < h_slice) {
1551 /* adjust for window's requested size increment */
1552 remain = (win_g.h - h_base) % h_inc;
1553 missing = h_inc - remain;
1555 if (missing <= extra || j == 0) {
1567 win_g.y += last_h + 2;
1569 bzero(&wc, sizeof wc);
1570 wc.border_width = 1;
1572 win->g.x = wc.x = win_g.y;
1573 win->g.y = wc.y = win_g.x;
1574 win->g.w = wc.width = win_g.h;
1575 win->g.h = wc.height = win_g.w;
1577 win->g.x = wc.x = win_g.x;
1578 win->g.y = wc.y = win_g.y;
1579 win->g.w = wc.width = win_g.w;
1580 win->g.h = wc.height = win_g.h;
1583 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1584 XConfigureWindow(display, win->id, mask, &wc);
1585 XMapRaised(display, win->id);
1593 /* now, stack all the floaters and transients */
1594 TAILQ_FOREACH(win, &ws->winlist, entry) {
1595 if (win->transient == 0 && win->floating == 0)
1598 stack_floater(win, ws->r);
1599 XMapRaised(display, win->id);
1603 focus_win(winfocus); /* has to be done outside of the loop */
1607 vertical_config(struct workspace *ws, int id)
1609 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1612 case SWM_ARG_ID_STACKRESET:
1613 case SWM_ARG_ID_STACKINIT:
1614 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1615 ws->l_state.vertical_mwin = 1;
1616 ws->l_state.vertical_stacks = 1;
1618 case SWM_ARG_ID_MASTERSHRINK:
1619 if (ws->l_state.vertical_msize > 1)
1620 ws->l_state.vertical_msize--;
1622 case SWM_ARG_ID_MASTERGROW:
1623 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1624 ws->l_state.vertical_msize++;
1626 case SWM_ARG_ID_MASTERADD:
1627 ws->l_state.vertical_mwin++;
1629 case SWM_ARG_ID_MASTERDEL:
1630 if (ws->l_state.vertical_mwin > 0)
1631 ws->l_state.vertical_mwin--;
1633 case SWM_ARG_ID_STACKINC:
1634 ws->l_state.vertical_stacks++;
1636 case SWM_ARG_ID_STACKDEC:
1637 if (ws->l_state.vertical_stacks > 1)
1638 ws->l_state.vertical_stacks--;
1646 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1648 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1650 stack_master(ws, g, 0, 0);
1654 horizontal_config(struct workspace *ws, int id)
1656 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1659 case SWM_ARG_ID_STACKRESET:
1660 case SWM_ARG_ID_STACKINIT:
1661 ws->l_state.horizontal_mwin = 1;
1662 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1663 ws->l_state.horizontal_stacks = 1;
1665 case SWM_ARG_ID_MASTERSHRINK:
1666 if (ws->l_state.horizontal_msize > 1)
1667 ws->l_state.horizontal_msize--;
1669 case SWM_ARG_ID_MASTERGROW:
1670 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1671 ws->l_state.horizontal_msize++;
1673 case SWM_ARG_ID_MASTERADD:
1674 ws->l_state.horizontal_mwin++;
1676 case SWM_ARG_ID_MASTERDEL:
1677 if (ws->l_state.horizontal_mwin > 0)
1678 ws->l_state.horizontal_mwin--;
1680 case SWM_ARG_ID_STACKINC:
1681 ws->l_state.horizontal_stacks++;
1683 case SWM_ARG_ID_STACKDEC:
1684 if (ws->l_state.horizontal_stacks > 1)
1685 ws->l_state.horizontal_stacks--;
1693 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1695 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1697 stack_master(ws, g, 1, 0);
1700 /* fullscreen view */
1702 max_stack(struct workspace *ws, struct swm_geometry *g) {
1704 struct swm_geometry gg = *g;
1705 struct ws_win *win, *winfocus;
1708 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1710 if (count_win(ws, 0) == 0)
1713 if (ws->focus == NULL)
1714 ws->focus = TAILQ_FIRST(&ws->winlist);
1715 winfocus = cur_focus ? cur_focus : ws->focus;
1717 TAILQ_FOREACH(win, &ws->winlist, entry) {
1718 if (win->transient != 0 || win->floating != 0) {
1719 if (win == ws->focus) {
1721 stack_floater(win, ws->r);
1722 XMapRaised(display, win->id);
1724 XUnmapWindow(display, win->id);
1726 bzero(&wc, sizeof wc);
1727 wc.border_width = 1;
1728 win->g.x = wc.x = gg.x;
1729 win->g.y = wc.y = gg.y;
1730 win->g.w = wc.width = gg.w;
1731 win->g.h = wc.height = gg.h;
1732 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1733 XConfigureWindow(display, win->id, mask, &wc);
1735 if (win == ws->focus) {
1736 XMapRaised(display, win->id);
1738 XUnmapWindow(display, win->id);
1743 focus_win(winfocus); /* has to be done outside of the loop */
1747 send_to_ws(struct swm_region *r, union arg *args)
1749 int wsid = args->id;
1750 struct ws_win *win = cur_focus;
1751 struct workspace *ws, *nws;
1752 Atom ws_idx_atom = 0;
1753 unsigned char ws_idx_str[SWM_PROPLEN];
1758 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1761 nws = &win->s->ws[wsid];
1763 XUnmapWindow(display, win->id);
1765 /* find a window to focus */
1766 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1767 if (ws->focus == NULL)
1768 ws->focus = TAILQ_FIRST(&ws->winlist);
1769 if (ws->focus == win)
1772 TAILQ_REMOVE(&ws->winlist, win, entry);
1774 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1777 /* Try to update the window's workspace property */
1778 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1780 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1781 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1783 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1784 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1787 if (count_win(nws, 1) == 1)
1796 wkill(struct swm_region *r, union arg *args)
1798 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
1800 if(r->ws->focus == NULL)
1803 if (args->id == SWM_ARG_ID_KILLWINDOW)
1804 XKillClient(display, r->ws->focus->id);
1806 if (r->ws->focus->can_delete)
1807 client_msg(r->ws->focus, adelete);
1811 screenshot(struct swm_region *r, union arg *args)
1815 DNPRINTF(SWM_D_MISC, "screenshot\n");
1817 if (ss_enabled == 0)
1821 case SWM_ARG_ID_SS_ALL:
1822 spawn_screenshot[1] = "full";
1824 case SWM_ARG_ID_SS_WINDOW:
1825 spawn_screenshot[1] = "window";
1830 a.argv = spawn_screenshot;
1835 floating_toggle(struct swm_region *r, union arg *args)
1837 struct ws_win *win = cur_focus;
1842 win->floating = !win->floating;
1848 /* key definitions */
1852 void (*func)(struct swm_region *r, union arg *);
1855 /* modifier key function argument */
1856 { MODKEY, XK_space, cycle_layout, {0} },
1857 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1858 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1859 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1860 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1861 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1862 { MODKEY | ShiftMask, XK_comma, stack_config, {.id = SWM_ARG_ID_STACKINC} },
1863 { MODKEY | ShiftMask, XK_period, stack_config, {.id = SWM_ARG_ID_STACKDEC} },
1864 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1865 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1866 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1867 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1868 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1869 { MODKEY | ShiftMask, XK_Return, spawnterm, {.argv = spawn_term} },
1870 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1871 { MODKEY | ShiftMask, XK_q, quit, {0} },
1872 { MODKEY, XK_q, restart, {0} },
1873 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1874 { MODKEY, XK_1, switchws, {.id = 0} },
1875 { MODKEY, XK_2, switchws, {.id = 1} },
1876 { MODKEY, XK_3, switchws, {.id = 2} },
1877 { MODKEY, XK_4, switchws, {.id = 3} },
1878 { MODKEY, XK_5, switchws, {.id = 4} },
1879 { MODKEY, XK_6, switchws, {.id = 5} },
1880 { MODKEY, XK_7, switchws, {.id = 6} },
1881 { MODKEY, XK_8, switchws, {.id = 7} },
1882 { MODKEY, XK_9, switchws, {.id = 8} },
1883 { MODKEY, XK_0, switchws, {.id = 9} },
1884 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1885 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1886 { MODKEY | ShiftMask, XK_Right, cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
1887 { MODKEY | ShiftMask, XK_Left, cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
1888 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1889 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1890 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1891 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1892 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1893 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1894 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1895 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1896 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1897 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1898 { MODKEY, XK_b, bar_toggle, {0} },
1899 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1900 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1901 { MODKEY | ShiftMask, XK_x, wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
1902 { MODKEY, XK_x, wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
1903 { MODKEY, XK_s, screenshot, {.id = SWM_ARG_ID_SS_ALL} },
1904 { MODKEY | ShiftMask, XK_s, screenshot, {.id = SWM_ARG_ID_SS_WINDOW} },
1905 { MODKEY, XK_t, floating_toggle,{0} },
1906 { MODKEY | ShiftMask, XK_v, version, {0} },
1907 { MODKEY | ShiftMask, XK_Delete, spawn, {.argv = spawn_lock} },
1908 { MODKEY | ShiftMask, XK_i, spawn, {.argv = spawn_initscr} },
1912 resize_window(struct ws_win *win, int center)
1916 struct swm_region *r;
1918 r = root_to_region(win->wa.root);
1919 bzero(&wc, sizeof wc);
1920 mask = CWBorderWidth | CWWidth | CWHeight;
1921 wc.border_width = 1;
1922 wc.width = win->g.w;
1923 wc.height = win->g.h;
1924 if (center == SWM_ARG_ID_CENTER) {
1925 wc.x = (WIDTH(r) - win->g.w) / 2;
1926 wc.y = (HEIGHT(r) - win->g.h) / 2;
1930 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
1931 win->id, wc.x, wc.y, wc.width, wc.height);
1933 XConfigureWindow(display, win->id, mask, &wc);
1938 resize(struct ws_win *win, union arg *args)
1943 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %d\n",
1944 win->id, win->floating, win->transient);
1946 if (!(win->transient != 0 || win->floating != 0))
1949 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1950 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1952 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
1954 XMaskEvent(display, MOUSEMASK | ExposureMask |
1955 SubstructureRedirectMask, &ev);
1957 case ConfigureRequest:
1960 handler[ev.type](&ev);
1963 if (ev.xmotion.x <= 1)
1965 if (ev.xmotion.y <= 1)
1967 win->g.w = ev.xmotion.x;
1968 win->g.h = ev.xmotion.y;
1970 /* not free, don't sync more than 60 times / second */
1971 if ((ev.xmotion.time - time) > (1000 / 60) ) {
1972 time = ev.xmotion.time;
1973 XSync(display, False);
1974 resize_window(win, args->id);
1978 } while (ev.type != ButtonRelease);
1980 XSync(display, False);
1981 resize_window(win, args->id);
1983 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
1985 XUngrabPointer(display, CurrentTime);
1988 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1992 move_window(struct ws_win *win)
1996 struct swm_region *r;
1998 r = root_to_region(win->wa.root);
1999 bzero(&wc, sizeof wc);
2004 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2005 win->id, wc.x, wc.y, wc.width, wc.height);
2007 XConfigureWindow(display, win->id, mask, &wc);
2012 move(struct ws_win *win, union arg *args)
2018 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %d\n",
2019 win->id, win->floating, win->transient);
2021 if (win->floating == 0) {
2027 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2028 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2030 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2032 XMaskEvent(display, MOUSEMASK | ExposureMask |
2033 SubstructureRedirectMask, &ev);
2035 case ConfigureRequest:
2038 handler[ev.type](&ev);
2041 win->g.x = ev.xmotion.x_root;
2042 win->g.y = ev.xmotion.y_root;
2044 /* not free, don't sync more than 60 times / second */
2045 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2046 time = ev.xmotion.time;
2047 XSync(display, False);
2052 } while (ev.type != ButtonRelease);
2054 XSync(display, False);
2057 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2058 XUngrabPointer(display, CurrentTime);
2063 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2067 enum { client_click, root_click };
2069 unsigned int action;
2071 unsigned int button;
2072 void (*func)(struct ws_win *, union arg *);
2075 /* action key mouse button func args */
2076 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2077 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2078 { client_click, MODKEY, Button1, move, {0} },
2082 updatenumlockmask(void)
2085 XModifierKeymap *modmap;
2087 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
2089 modmap = XGetModifierMapping(display);
2090 for (i = 0; i < 8; i++)
2091 for (j = 0; j < modmap->max_keypermod; j++)
2092 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2093 == XKeysymToKeycode(display, XK_Num_Lock))
2094 numlockmask = (1 << i);
2096 XFreeModifiermap(modmap);
2102 unsigned int i, j, k;
2104 unsigned int modifiers[] =
2105 { 0, LockMask, numlockmask, numlockmask | LockMask };
2107 DNPRINTF(SWM_D_MISC, "grabkeys\n");
2108 updatenumlockmask();
2110 for (k = 0; k < ScreenCount(display); k++) {
2111 if (TAILQ_EMPTY(&screens[k].rl))
2113 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
2114 for (i = 0; i < LENGTH(keys); i++) {
2115 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
2116 for (j = 0; j < LENGTH(modifiers); j++)
2117 XGrabKey(display, code,
2118 keys[i].mod | modifiers[j],
2119 screens[k].root, True,
2120 GrabModeAsync, GrabModeAsync);
2126 grabbuttons(struct ws_win *win, int focused)
2129 unsigned int modifiers[] =
2130 { 0, LockMask, numlockmask, numlockmask|LockMask };
2132 updatenumlockmask();
2133 XUngrabButton(display, AnyButton, AnyModifier, win->id);
2135 for (i = 0; i < LENGTH(buttons); i++)
2136 if (buttons[i].action == client_click)
2137 for (j = 0; j < LENGTH(modifiers); j++)
2138 XGrabButton(display, buttons[i].button,
2139 buttons[i].mask | modifiers[j],
2140 win->id, False, BUTTONMASK,
2141 GrabModeAsync, GrabModeSync, None,
2144 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
2145 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
2151 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
2159 XKeyEvent *ev = &e->xkey;
2161 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
2163 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
2164 for (i = 0; i < LENGTH(keys); i++)
2165 if (keysym == keys[i].keysym
2166 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
2168 keys[i].func(root_to_region(ev->root),
2173 buttonpress(XEvent *e)
2175 XButtonPressedEvent *ev = &e->xbutton;
2180 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
2182 action = root_click;
2183 if ((win = find_window(ev->window)) == NULL)
2187 action = client_click;
2190 for (i = 0; i < LENGTH(buttons); i++)
2191 if (action == buttons[i].action && buttons[i].func &&
2192 buttons[i].button == ev->button &&
2193 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
2194 buttons[i].func(win, &buttons[i].args);
2198 set_win_state(struct ws_win *win, long state)
2200 long data[] = {state, None};
2202 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
2204 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
2205 (unsigned char *)data, 2);
2209 manage_window(Window id)
2212 struct workspace *ws;
2214 int format, i, ws_idx, n;
2215 unsigned long nitems, bytes;
2216 Atom ws_idx_atom = 0, type;
2217 Atom *prot = NULL, *pp;
2218 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
2219 struct swm_region *r;
2224 if ((win = find_window(id)) != NULL)
2225 return (win); /* already being managed */
2227 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
2228 errx(1, "calloc: failed to allocate memory for new window");
2230 /* Get all the window data in one shot */
2231 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2233 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
2234 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
2235 XGetWindowAttributes(display, id, &win->wa);
2236 XGetTransientForHint(display, id, &trans);
2237 XGetWMNormalHints(display, id, &win->sh, &mask); /* XXX function? */
2239 win->transient = trans;
2240 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
2241 (unsigned)win->id, win->transient);
2243 /* get supported protocols */
2244 if (XGetWMProtocols(display, id, &prot, &n)) {
2245 for (i = 0, pp = prot; i < n; i++, pp++)
2247 win->can_delete = 1;
2253 * Figure out where to put the window. If it was previously assigned to
2254 * a workspace (either by spawn() or manually moving), and isn't
2255 * transient, * put it in the same workspace
2257 r = root_to_region(win->wa.root);
2258 if (prop && win->transient == 0) {
2259 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
2260 ws_idx = strtonum(prop, 0, 9, &errstr);
2262 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
2265 ws = &r->s->ws[ws_idx];
2269 /* set up the window layout */
2272 win->s = r->s; /* this never changes */
2273 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
2275 win->g.w = win->wa.width;
2276 win->g.h = win->wa.height;
2277 win->g.x = win->wa.x;
2278 win->g.y = win->wa.y;
2280 /* Set window properties so we can remember this after reincarnation */
2281 if (ws_idx_atom && prop == NULL &&
2282 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
2283 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2285 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2286 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2290 if (XGetClassHint(display, win->id, &win->ch)) {
2291 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
2292 win->ch.res_class, win->ch.res_name);
2293 for (i = 0; quirks[i].class != NULL && quirks[i].name != NULL &&
2294 quirks[i].quirk != 0; i++){
2295 if (!strcmp(win->ch.res_class, quirks[i].class) &&
2296 !strcmp(win->ch.res_name, quirks[i].name)) {
2297 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
2298 win->ch.res_class, win->ch.res_name);
2299 if (quirks[i].quirk & SWM_Q_FLOAT)
2301 win->quirks = quirks[i].quirk;
2306 /* alter window position if quirky */
2307 if (win->quirks & SWM_Q_ANYWHERE) {
2308 win->manual = 1; /* don't center the quirky windows */
2309 bzero(&wc, sizeof wc);
2311 if (win->g.y < bar_height) {
2312 win->g.y = wc.y = bar_height;
2315 if (win->g.w + win->g.x > WIDTH(r)) {
2316 win->g.x = wc.x = WIDTH(win->ws->r) - win->g.w - 2;
2319 wc.border_width = 1;
2320 mask |= CWBorderWidth;
2321 XConfigureWindow(display, win->id, mask, &wc);
2324 /* Reset font sizes (the bruteforce way; no default keybinding). */
2325 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
2326 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
2327 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2328 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
2329 fake_keypress(win, XK_KP_Add, ShiftMask);
2332 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
2333 PropertyChangeMask | StructureNotifyMask);
2335 set_win_state(win, NormalState);
2337 /* floaters need to be mapped if they are in the current workspace */
2338 if (win->floating && (ws->idx == r->ws->idx))
2339 XMapRaised(display, win->id);
2341 /* make new win focused */
2348 unmanage_window(struct ws_win *win)
2350 struct workspace *ws;
2355 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
2357 /* don't unmanage if we are switching workspaces */
2362 /* find a window to focus */
2363 if (ws->focus == win)
2364 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
2365 if (ws->focus == NULL)
2366 ws->focus = TAILQ_FIRST(&ws->winlist);
2367 if (ws->focus == NULL || ws->focus == win) {
2371 focus_win(ws->focus);
2372 if (ws->focus_prev == win)
2373 ws->focus_prev = NULL;
2375 TAILQ_REMOVE(&win->ws->winlist, win, entry);
2376 set_win_state(win, WithdrawnState);
2377 if (win->ch.res_class)
2378 XFree(win->ch.res_class);
2379 if (win->ch.res_name)
2380 XFree(win->ch.res_name);
2385 configurerequest(XEvent *e)
2387 XConfigureRequestEvent *ev = &e->xconfigurerequest;
2392 if ((win = find_window(ev->window)) == NULL)
2396 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
2398 bzero(&wc, sizeof wc);
2401 wc.width = ev->width;
2402 wc.height = ev->height;
2403 wc.border_width = ev->border_width;
2404 wc.sibling = ev->above;
2405 wc.stack_mode = ev->detail;
2406 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
2408 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
2410 if (win->floating) {
2411 if (ev->value_mask & CWX)
2413 if (ev->value_mask & CWY)
2415 if (ev->value_mask & CWWidth)
2416 win->g.w = ev->width;
2417 if (ev->value_mask & CWHeight)
2418 win->g.h = ev->height;
2419 if (win->ws->r != NULL) {
2420 /* this seems to be full screen */
2421 if (win->g.w >= WIDTH(win->ws->r)) {
2423 win->g.w = WIDTH(win->ws->r);
2424 ev->value_mask |= CWX | CWWidth;
2426 if (win->g.h >= HEIGHT(win->ws->r)) {
2429 win->g.h = HEIGHT(win->ws->r);
2430 ev->value_mask |= CWY | CWHeight;
2433 if ((ev->value_mask & (CWX | CWY)) &&
2434 !(ev->value_mask & (CWWidth | CWHeight)))
2436 XMoveResizeWindow(display, win->id,
2437 win->g.x, win->g.y, win->g.w, win->g.h);
2444 configurenotify(XEvent *e)
2449 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
2450 e->xconfigure.window);
2452 win = find_window(e->xconfigure.window);
2453 XMapWindow(display, win->id);
2454 XGetWMNormalHints(display, win->id, &win->sh, &mask);
2456 XMapWindow(display, win->id);
2462 destroynotify(XEvent *e)
2465 XDestroyWindowEvent *ev = &e->xdestroywindow;
2467 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
2469 if ((win = find_window(ev->window)) != NULL) {
2470 unmanage_window(win);
2476 enternotify(XEvent *e)
2478 XCrossingEvent *ev = &e->xcrossing;
2481 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
2484 /* eat event(r) to prevent autofocus */
2489 if ((win = find_window(ev->window)) != NULL)
2496 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
2500 mappingnotify(XEvent *e)
2502 XMappingEvent *ev = &e->xmapping;
2504 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
2506 XRefreshKeyboardMapping(ev);
2507 if (ev->request == MappingKeyboard)
2512 maprequest(XEvent *e)
2514 XMapRequestEvent *ev = &e->xmaprequest;
2515 XWindowAttributes wa;
2517 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
2518 e->xmaprequest.window);
2520 if (!XGetWindowAttributes(display, ev->window, &wa))
2522 if (wa.override_redirect)
2524 manage_window(e->xmaprequest.window);
2530 propertynotify(XEvent *e)
2533 XPropertyEvent *ev = &e->xproperty;
2535 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
2538 if (ev->state == PropertyDelete)
2539 return; /* ignore */
2540 win = find_window(ev->window);
2545 case XA_WM_NORMAL_HINTS:
2548 XGetWMNormalHints(display, win->id, &win->sh, &mask);
2549 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
2550 if (win->sh.flags & PMinSize) {
2551 win->g.w = win->sh.min_width;
2552 win->g.h = win->sh.min_height;
2553 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
2555 XMoveResizeWindow(display, win->id,
2556 win->g.x, win->g.y, win->g.w, win->g.h);
2565 unmapnotify(XEvent *e)
2567 XDestroyWindowEvent *ev = &e->xdestroywindow;
2570 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
2572 if ((win = find_window(ev->window)) != NULL)
2574 unmanage_window(win);
2578 visibilitynotify(XEvent *e)
2581 struct swm_region *r;
2583 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
2584 e->xvisibility.window);
2585 if (e->xvisibility.state == VisibilityUnobscured)
2586 for (i = 0; i < ScreenCount(display); i++)
2587 TAILQ_FOREACH(r, &screens[i].rl, entry)
2588 if (e->xvisibility.window == r->bar_window)
2593 xerror_start(Display *d, XErrorEvent *ee)
2600 xerror(Display *d, XErrorEvent *ee)
2602 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2610 xerrorxlib = XSetErrorHandler(xerror_start);
2612 /* this causes an error if some other window manager is running */
2613 XSelectInput(display, DefaultRootWindow(display),
2614 SubstructureRedirectMask);
2615 XSync(display, False);
2619 XSetErrorHandler(xerror);
2620 XSync(display, False);
2629 unsigned char *p = NULL;
2630 unsigned long n, extra;
2633 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
2634 &real, &format, &n, &extra, (unsigned char **)&p);
2635 if (status != Success)
2638 result = *((long *)p);
2644 new_region(struct swm_screen *s, int x, int y, int w, int h)
2646 struct swm_region *r, *n;
2647 struct workspace *ws = NULL;
2650 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
2651 s->idx, w, h, x, y);
2653 /* remove any conflicting regions */
2654 n = TAILQ_FIRST(&s->rl);
2657 n = TAILQ_NEXT(r, entry);
2658 if (X(r) < (x + w) &&
2659 (X(r) + WIDTH(r)) > x &&
2661 (Y(r) + HEIGHT(r)) > y) {
2662 XDestroyWindow(display, r->bar_window);
2663 TAILQ_REMOVE(&s->rl, r, entry);
2664 TAILQ_INSERT_TAIL(&s->orl, r, entry);
2668 /* search old regions for one to reuse */
2670 /* size + location match */
2671 TAILQ_FOREACH(r, &s->orl, entry)
2672 if (X(r) == x && Y(r) == y &&
2673 HEIGHT(r) == h && WIDTH(r) == w)
2677 TAILQ_FOREACH(r, &s->orl, entry)
2678 if (HEIGHT(r) == h && WIDTH(r) == w)
2682 TAILQ_REMOVE(&s->orl, r, entry);
2683 /* try to use old region's workspace */
2684 if (r->ws->r == NULL)
2687 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
2688 errx(1, "calloc: failed to allocate memory for screen");
2690 /* if we don't have a workspace already, find one */
2692 for (i = 0; i < SWM_WS_MAX; i++)
2693 if (s->ws[i].r == NULL) {
2700 errx(1, "no free workspaces\n");
2709 TAILQ_INSERT_TAIL(&s->rl, r, entry);
2715 #ifdef SWM_XRR_HAS_CRTC
2717 XRRScreenResources *sr;
2720 #endif /* SWM_XRR_HAS_CRTC */
2721 struct swm_region *r;
2724 if (i >= ScreenCount(display))
2725 errx(1, "invalid screen");
2727 /* remove any old regions */
2728 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
2730 XDestroyWindow(display, r->bar_window);
2731 TAILQ_REMOVE(&screens[i].rl, r, entry);
2732 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
2735 /* map virtual screens onto physical screens */
2736 #ifdef SWM_XRR_HAS_CRTC
2737 if (xrandr_support) {
2738 sr = XRRGetScreenResources(display, screens[i].root);
2740 new_region(&screens[i], 0, 0,
2741 DisplayWidth(display, i),
2742 DisplayHeight(display, i));
2746 for (c = 0, ci = NULL; c < ncrtc; c++) {
2747 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
2748 if (ci->noutput == 0)
2751 if (ci != NULL && ci->mode == None)
2752 new_region(&screens[i], 0, 0,
2753 DisplayWidth(display, i),
2754 DisplayHeight(display, i));
2756 new_region(&screens[i],
2757 ci->x, ci->y, ci->width, ci->height);
2760 XRRFreeCrtcInfo(ci);
2761 XRRFreeScreenResources(sr);
2763 #endif /* SWM_XRR_HAS_CRTC */
2765 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
2766 DisplayHeight(display, i));
2771 screenchange(XEvent *e) {
2772 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
2773 struct swm_region *r;
2777 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
2779 if (!XRRUpdateConfiguration(e))
2782 /* silly event doesn't include the screen index */
2783 for (i = 0; i < ScreenCount(display); i++)
2784 if (screens[i].root == xe->root)
2786 if (i >= ScreenCount(display))
2787 errx(1, "screenchange: screen not found\n");
2789 /* brute force for now, just re-enumerate the regions */
2792 /* hide any windows that went away */
2793 TAILQ_FOREACH(r, &screens[i].rl, entry)
2794 TAILQ_FOREACH(win, &r->ws->winlist, entry)
2795 XUnmapWindow(display, win->id);
2797 /* add bars to all regions */
2798 for (i = 0; i < ScreenCount(display); i++)
2799 TAILQ_FOREACH(r, &screens[i].rl, entry)
2807 Window d1, d2, *wins = NULL;
2808 XWindowAttributes wa;
2811 int errorbase, major, minor;
2812 struct workspace *ws;
2816 if ((screens = calloc(ScreenCount(display),
2817 sizeof(struct swm_screen))) == NULL)
2818 errx(1, "calloc: screens");
2820 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2822 /* initial Xrandr setup */
2823 xrandr_support = XRRQueryExtension(display,
2824 &xrandr_eventbase, &errorbase);
2826 if (XRRQueryVersion(display, &major, &minor) && major < 1)
2829 /* map physical screens */
2830 for (i = 0; i < ScreenCount(display); i++) {
2831 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
2833 TAILQ_INIT(&screens[i].rl);
2834 TAILQ_INIT(&screens[i].orl);
2835 screens[i].root = RootWindow(display, i);
2837 /* set default colors */
2838 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
2839 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
2840 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
2841 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
2842 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
2844 /* init all workspaces */
2845 /* XXX these should be dynamically allocated too */
2846 for (j = 0; j < SWM_WS_MAX; j++) {
2847 ws = &screens[i].ws[j];
2852 TAILQ_INIT(&ws->winlist);
2854 for (k = 0; layouts[k].l_stack != NULL; k++)
2855 if (layouts[k].l_config != NULL)
2856 layouts[k].l_config(ws,
2857 SWM_ARG_ID_STACKINIT);
2858 ws->cur_layout = &layouts[0];
2860 /* grab existing windows (before we build the bars)*/
2861 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
2867 XRRSelectInput(display, screens[i].root,
2868 RRScreenChangeNotifyMask);
2870 /* attach windows to a region */
2871 /* normal windows */
2872 for (j = 0; j < no; j++) {
2873 XGetWindowAttributes(display, wins[j], &wa);
2874 if (!XGetWindowAttributes(display, wins[j], &wa) ||
2875 wa.override_redirect ||
2876 XGetTransientForHint(display, wins[j], &d1))
2879 if (wa.map_state == IsViewable ||
2880 getstate(wins[j]) == NormalState)
2881 manage_window(wins[j]);
2883 /* transient windows */
2884 for (j = 0; j < no; j++) {
2885 if (!XGetWindowAttributes(display, wins[j], &wa))
2888 if (XGetTransientForHint(display, wins[j], &d1) &&
2889 (wa.map_state == IsViewable || getstate(wins[j]) ==
2891 manage_window(wins[j]);
2901 main(int argc, char *argv[])
2904 struct swm_region *r;
2905 char conf[PATH_MAX], *cfile = NULL;
2912 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
2913 SWM_VERSION, cvstag);
2914 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2915 warnx("no locale support");
2917 if (!(display = XOpenDisplay(0)))
2918 errx(1, "can not open display");
2921 errx(1, "other wm running");
2923 astate = XInternAtom(display, "WM_STATE", False);
2924 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
2925 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
2927 /* look for local and global conf file */
2928 pwd = getpwuid(getuid());
2930 errx(1, "invalid user %d", getuid());
2934 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2935 if (stat(conf, &sb) != -1) {
2936 if (S_ISREG(sb.st_mode))
2939 /* try global conf file */
2940 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2941 if (!stat(conf, &sb))
2942 if (S_ISREG(sb.st_mode))
2948 /* setup all bars */
2949 for (i = 0; i < ScreenCount(display); i++)
2950 TAILQ_FOREACH(r, &screens[i].rl, entry)
2953 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2958 xfd = ConnectionNumber(display);
2962 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2964 errx(1, "select failed");
2969 while (XPending(display)) {
2970 XNextEvent(display, &e);
2971 if (e.type < LASTEvent) {
2972 if (handler[e.type])
2973 handler[e.type](&e);
2975 DNPRINTF(SWM_D_EVENT,
2976 "win: %lu unknown event: %d\n",
2977 e.xany.window, e.type);
2979 switch (e.type - xrandr_eventbase) {
2980 case RRScreenChangeNotify:
2984 DNPRINTF(SWM_D_EVENT,
2985 "win: %lu unknown xrandr event: "
2986 "%d\n", e.xany.window, e.type);
2993 XCloseDisplay(display);