+void
+bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
+{
+ int do_class, do_name;
+ Status status;
+ XClassHint *xch = NULL;
+
+ if ((title_name_enabled == 1 || title_class_enabled == 1) &&
+ cur_focus != NULL) {
+ if ((xch = XAllocClassHint()) == NULL)
+ goto out;
+ status = XGetClassHint(display, cur_focus->id, xch);
+ if (status == BadWindow || status == BadAlloc)
+ goto out;
+ do_class = (title_class_enabled && xch->res_class != NULL);
+ do_name = (title_name_enabled && xch->res_name != NULL);
+ if (do_class)
+ strlcat(s, xch->res_class, sz);
+ if (do_class && do_name)
+ strlcat(s, ":", sz);
+ if (do_name)
+ strlcat(s, xch->res_name, sz);
+ strlcat(s, " ", sz);
+ }
+out:
+ if (xch) {
+ XFree(xch->res_name);
+ XFree(xch->res_class);
+ XFree(xch);
+ }
+}
+
+void
+bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
+{
+ unsigned char *title;
+
+ if (window_name_enabled && cur_focus != NULL) {
+ title = get_win_name(cur_focus->id);
+ if (title != NULL) {
+ DNPRINTF(SWM_D_BAR, "bar_window_name: title: %s\n",
+ title);
+
+ if (cur_focus->floating)
+ strlcat(s, "(f) ", sz);
+ strlcat(s, (char *)title, sz);
+ strlcat(s, " ", sz);
+ XFree(title);
+ }
+ }
+}
+
+int urgent[SWM_WS_MAX];
+void
+bar_urgent(char *s, ssize_t sz)
+{
+ XWMHints *wmh = NULL;
+ struct ws_win *win;
+ int i, j;
+ char b[8];
+
+ if (urgent_enabled == 0)
+ return;
+
+ for (i = 0; i < SWM_WS_MAX; i++)
+ urgent[i] = 0;
+
+ for (i = 0; i < ScreenCount(display); i++)
+ for (j = 0; j < SWM_WS_MAX; j++)
+ TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
+ wmh = XGetWMHints(display, win->id);
+ if (wmh == NULL)
+ continue;
+
+ if (wmh->flags & XUrgencyHint)
+ urgent[j] = 1;
+ XFree(wmh);
+ }
+
+ strlcat(s, "* ", sz);
+ for (i = 0; i < SWM_WS_MAX; i++) {
+ if (urgent[i])
+ snprintf(b, sizeof b, "%d ", i + 1);
+ else
+ snprintf(b, sizeof b, "- ");
+ strlcat(s, b, sz);
+ }
+ strlcat(s, "* ", sz);
+}
+
+void
+bar_update(void)
+{
+ time_t tmt;
+ struct tm tm;
+ struct swm_region *r;
+ int i, x;
+ size_t len;
+ char ws[SWM_BAR_MAX];
+ char s[SWM_BAR_MAX];
+ unsigned char cn[SWM_BAR_MAX];
+ char loc[SWM_BAR_MAX];
+ char *b, *stack = "";
+
+ if (bar_enabled == 0)
+ return;
+ if (bar_extra && bar_extra_running) {
+ /* ignore short reads; it'll correct itself */
+ while ((b = fgetln(stdin, &len)) != NULL)
+ if (b && b[len - 1] == '\n') {
+ b[len - 1] = '\0';
+ strlcpy((char *)bar_ext, b, sizeof bar_ext);
+ }
+ if (b == NULL && errno != EAGAIN) {
+ warn("bar_update: bar_extra failed");
+ bar_extra_stop();
+ }
+ } else
+ strlcpy((char *)bar_ext, "", sizeof bar_ext);
+
+ if (clock_enabled == 0)
+ strlcpy(s, "", sizeof s);
+ else {
+ time(&tmt);
+ localtime_r(&tmt, &tm);
+ len = strftime(s, sizeof s, clock_format, &tm);
+ s[len] = '\0';
+ strlcat(s, " ", sizeof s);
+ }
+
+ for (i = 0; i < ScreenCount(display); i++) {
+ x = 1;
+ TAILQ_FOREACH(r, &screens[i].rl, entry) {
+ strlcpy((char *)cn, "", sizeof cn);
+ strlcpy(ws, "", sizeof ws);
+ if (r && r->ws) {
+ bar_urgent((char *)cn, sizeof cn);
+ bar_class_name((char *)cn, sizeof cn,
+ r->ws->focus);
+ bar_window_name((char *)cn, sizeof cn,
+ r->ws->focus);
+ if (r->ws->name)
+ snprintf(ws, sizeof ws, "<%s>",
+ r->ws->name);
+ }
+ if (stack_enabled)
+ stack = r->ws->stacker;
+
+ snprintf(loc, sizeof loc, "%d:%d %s %s %s%s %s "
+ "%s", x++, r->ws->idx + 1, stack, ws, s, cn,
+ bar_ext, bar_vertext);
+ bar_print(r, loc);
+ }
+ }
+ alarm(bar_delay);
+}
+
+void
+bar_check_opts(void)
+{
+ if (title_class_enabled || title_name_enabled || window_name_enabled)
+ bar_update();
+}
+
+void
+bar_signal(int sig)
+{
+ bar_alarm = 1;
+}
+
+void
+bar_toggle(struct swm_region *r, union arg *args)
+{
+ struct swm_region *tmpr;
+ int i, sc = ScreenCount(display);
+
+ DNPRINTF(SWM_D_BAR, "bar_toggle\n");
+
+ if (bar_enabled)
+ for (i = 0; i < sc; i++)
+ TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
+ XUnmapWindow(display, tmpr->bar_window);
+ else
+ for (i = 0; i < sc; i++)
+ TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
+ XMapRaised(display, tmpr->bar_window);
+
+ bar_enabled = !bar_enabled;
+
+ stack();
+ /* must be after stack */
+ bar_update();
+}
+
+void
+bar_refresh(void)
+{
+ XSetWindowAttributes wa;
+ struct swm_region *r;
+ int i;
+
+ /* do this here because the conf file is in memory */
+ if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
+ /* launch external status app */
+ bar_extra_running = 1;
+ if (pipe(bar_pipe) == -1)
+ err(1, "pipe error");
+ socket_setnonblock(bar_pipe[0]);
+ socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
+ if (dup2(bar_pipe[0], 0) == -1)
+ err(1, "dup2");
+ if (dup2(bar_pipe[1], 1) == -1)
+ err(1, "dup2");
+ if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
+ err(1, "could not disable SIGPIPE");
+ switch (bar_pid = fork()) {
+ case -1:
+ err(1, "cannot fork");
+ break;
+ case 0: /* child */
+ close(bar_pipe[0]);
+ execvp(bar_argv[0], bar_argv);
+ err(1, "%s external app failed", bar_argv[0]);
+ break;
+ default: /* parent */
+ close(bar_pipe[1]);
+ break;
+ }
+ }
+
+ bzero(&wa, sizeof wa);
+ for (i = 0; i < ScreenCount(display); i++)
+ TAILQ_FOREACH(r, &screens[i].rl, entry) {
+ wa.border_pixel =
+ screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
+ wa.background_pixel =
+ screens[i].c[SWM_S_COLOR_BAR].color;
+ XChangeWindowAttributes(display, r->bar_window,
+ CWBackPixel | CWBorderPixel, &wa);
+ }
+ bar_update();
+}
+
+void
+bar_setup(struct swm_region *r)
+{
+ char *default_string;
+ char **missing_charsets;
+ int num_missing_charsets = 0;
+ int i, x, y;
+
+ if (bar_fs) {
+ XFreeFontSet(display, bar_fs);
+ bar_fs = NULL;
+ }
+
+
+ DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
+
+ bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
+ &num_missing_charsets, &default_string);
+
+ if (num_missing_charsets > 0) {
+ warnx("Unable to load charset(s):");
+
+ for (i = 0; i < num_missing_charsets; ++i)
+ warnx("%s", missing_charsets[i]);
+
+ XFreeStringList(missing_charsets);
+
+ if (strcmp(default_string, ""))
+ warnx("Glyphs from those sets will be replaced "
+ "by '%s'.", default_string);
+ else
+ warnx("Glyphs from those sets won't be drawn.");
+ }
+
+ if (bar_fs == NULL)
+ errx(1, "Error creating font set structure.");
+
+ bar_fs_extents = XExtentsOfFontSet(bar_fs);
+
+ bar_height = bar_fs_extents->max_logical_extent.height +
+ 2 * bar_border_width;
+
+ if (bar_height < 1)
+ bar_height = 1;
+
+ x = X(r);
+ y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
+
+ r->bar_window = XCreateSimpleWindow(display,
+ r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
+ bar_height - 2 * bar_border_width,
+ bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
+ r->s->c[SWM_S_COLOR_BAR].color);
+ XSelectInput(display, r->bar_window, VisibilityChangeMask);
+ if (bar_enabled)
+ XMapRaised(display, r->bar_window);
+ DNPRINTF(SWM_D_BAR, "bar_setup: bar_window: 0x%lx\n", r->bar_window);
+
+ if (signal(SIGALRM, bar_signal) == SIG_ERR)
+ err(1, "could not install bar_signal");
+ bar_refresh();
+}
+
+void
+drain_enter_notify(void)
+{
+ int i = 0;
+ XEvent cne;
+
+ while (XCheckMaskEvent(display, EnterWindowMask, &cne))
+ i++;
+
+ DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained: %d\n", i);
+}
+
+void
+set_win_state(struct ws_win *win, long state)
+{
+ long data[] = {state, None};
+
+ DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%lx\n", win->id);
+
+ if (win == NULL)
+ return;
+
+ XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
+ (unsigned char *)data, 2);
+}
+
+long
+getstate(Window w)
+{
+ long result = -1;
+ unsigned char *p = NULL;
+ unsigned long n;
+
+ if (!get_property(w, astate, 2L, astate, &n, NULL, &p))
+ return (-1);
+ if (n != 0)
+ result = *((long *)p);
+ XFree(p);
+ return (result);
+}
+
+void
+version(struct swm_region *r, union arg *args)
+{
+ bar_version = !bar_version;
+ if (bar_version)
+ snprintf(bar_vertext, sizeof bar_vertext,
+ "Version: %s Build: %s", SCROTWM_VERSION, buildstr);
+ else
+ strlcpy(bar_vertext, "", sizeof bar_vertext);
+ bar_update();
+}
+
+void
+client_msg(struct ws_win *win, Atom a)
+{
+ XClientMessageEvent cm;
+
+ if (win == NULL)
+ return;
+
+ bzero(&cm, sizeof cm);
+ cm.type = ClientMessage;
+ cm.window = win->id;
+ cm.message_type = aprot;
+ cm.format = 32;
+ cm.data.l[0] = a;
+ cm.data.l[1] = CurrentTime;
+ XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
+}
+
+/* synthetic response to a ConfigureRequest when not making a change */
+void
+config_win(struct ws_win *win, XConfigureRequestEvent *ev)
+{
+ XConfigureEvent ce;
+
+ if (win == NULL)
+ return;
+
+ /* send notification of unchanged state. */
+ ce.type = ConfigureNotify;
+ ce.x = X(win);
+ ce.y = Y(win);
+ ce.width = WIDTH(win);
+ ce.height = HEIGHT(win);
+ ce.override_redirect = False;
+
+ if (ev == NULL) {
+ /* EWMH */
+ ce.display = display;
+ ce.event = win->id;
+ ce.window = win->id;
+ ce.border_width = border_width;
+ ce.above = None;
+ } else {
+ /* normal */
+ ce.display = ev->display;
+ ce.event = ev->window;
+ ce.window = ev->window;
+
+ /* make response appear more WM_SIZE_HINTS-compliant */
+ if (win->sh_mask)
+ DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%lx,"
+ " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
+ "%d x %d\n", win->id, win->sh_mask, SH_MIN_W(win),
+ SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
+ SH_INC_W(win), SH_INC_H(win));
+
+ /* min size */
+ if (SH_MIN(win)) {
+ /* the hint may be set... to 0! */
+ if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
+ ce.width = SH_MIN_W(win);
+ if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
+ ce.height = SH_MIN_H(win);
+ }
+
+ /* max size */
+ if (SH_MAX(win)) {
+ /* may also be advertized as 0 */
+ if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
+ ce.width = SH_MAX_W(win);
+ if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
+ ce.height = SH_MAX_H(win);
+ }
+
+ /* resize increment. */
+ if (SH_INC(win)) {
+ if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
+ ce.width -= (ce.width - SH_MIN_W(win)) %
+ SH_INC_W(win);
+ if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
+ ce.height -= (ce.height - SH_MIN_H(win)) %
+ SH_INC_H(win);
+ }
+
+ /* adjust x and y for requested border_width. */
+ ce.x += border_width - ev->border_width;
+ ce.y += border_width - ev->border_width;
+ ce.border_width = ev->border_width;
+ ce.above = ev->above;
+ }
+
+ DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
+ "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
+ ce.y, ce.width, ce.height, ce.border_width);
+
+ XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
+}
+
+int
+count_win(struct workspace *ws, int count_transient)
+{
+ struct ws_win *win;
+ int count = 0;
+
+ TAILQ_FOREACH(win, &ws->winlist, entry) {
+ if (count_transient == 0 && win->floating)
+ continue;
+ if (count_transient == 0 && win->transient)
+ continue;
+ if (win->iconic)
+ continue;
+ count++;
+ }
+ DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
+
+ return (count);
+}
+
+void
+quit(struct swm_region *r, union arg *args)
+{
+ DNPRINTF(SWM_D_MISC, "quit\n");
+ running = 0;
+}
+
+void
+unmap_window(struct ws_win *win)
+{
+ if (win == NULL)
+ return;
+
+ /* don't unmap again */
+ if (getstate(win->id) == IconicState)
+ return;
+
+ set_win_state(win, IconicState);
+
+ XUnmapWindow(display, win->id);
+ XSetWindowBorder(display, win->id,
+ win->s->c[SWM_S_COLOR_UNFOCUS].color);
+}
+
+void
+unmap_all(void)
+{
+ struct ws_win *win;
+ int i, j;
+
+ for (i = 0; i < ScreenCount(display); i++)
+ for (j = 0; j < SWM_WS_MAX; j++)
+ TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
+ unmap_window(win);
+}
+
+void
+fake_keypress(struct ws_win *win, int keysym, int modifiers)
+{
+ XKeyEvent event;
+
+ if (win == NULL)
+ return;
+
+ event.display = display; /* Ignored, but what the hell */
+ event.window = win->id;
+ event.root = win->s->root;
+ event.subwindow = None;
+ event.time = CurrentTime;
+ event.x = X(win);
+ event.y = Y(win);
+ event.x_root = 1;
+ event.y_root = 1;
+ event.same_screen = True;
+ event.keycode = XKeysymToKeycode(display, keysym);
+ event.state = modifiers;
+
+ event.type = KeyPress;
+ XSendEvent(event.display, event.window, True,
+ KeyPressMask, (XEvent *)&event);
+
+ event.type = KeyRelease;
+ XSendEvent(event.display, event.window, True,
+ KeyPressMask, (XEvent *)&event);
+
+}
+
+void
+restart(struct swm_region *r, union arg *args)
+{
+ DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
+
+ /* disable alarm because the following code may not be interrupted */
+ alarm(0);
+ if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
+ err(1, "can't disable alarm");
+
+ bar_extra_stop();
+ bar_extra = 1;
+ unmap_all();
+ XCloseDisplay(display);
+ execvp(start_argv[0], start_argv);
+ warn("execvp failed");
+ quit(NULL, NULL);
+}
+
+struct swm_region *
+root_to_region(Window root)
+{
+ struct swm_region *r = NULL;
+ Window rr, cr;
+ int i, x, y, wx, wy;
+ unsigned int mask;
+
+ for (i = 0; i < ScreenCount(display); i++)
+ if (screens[i].root == root)
+ break;
+
+ if (XQueryPointer(display, screens[i].root,
+ &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
+ /* choose a region based on pointer location */
+ TAILQ_FOREACH(r, &screens[i].rl, entry)
+ if (x >= X(r) && x <= X(r) + WIDTH(r) &&
+ y >= Y(r) && y <= Y(r) + HEIGHT(r))
+ break;
+ }
+
+ if (r == NULL)
+ r = TAILQ_FIRST(&screens[i].rl);
+
+ return (r);
+}
+
+struct ws_win *
+find_unmanaged_window(Window id)
+{
+ struct ws_win *win;
+ int i, j;
+
+ for (i = 0; i < ScreenCount(display); i++)
+ for (j = 0; j < SWM_WS_MAX; j++)
+ TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
+ entry)
+ if (id == win->id)
+ return (win);
+ return (NULL);
+}
+
+struct ws_win *
+find_window(Window id)
+{
+ struct ws_win *win;
+ Window wrr, wpr, *wcr = NULL;
+ int i, j;
+ unsigned int nc;
+
+ for (i = 0; i < ScreenCount(display); i++)
+ for (j = 0; j < SWM_WS_MAX; j++)
+ TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
+ if (id == win->id)
+ return (win);
+
+ /* if we were looking for the parent return that window instead */
+ if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
+ return (NULL);
+ if (wcr)
+ XFree(wcr);
+
+ /* ignore not found and root */
+ if (wpr == 0 || wrr == wpr)
+ return (NULL);
+
+ /* look for parent */
+ for (i = 0; i < ScreenCount(display); i++)
+ for (j = 0; j < SWM_WS_MAX; j++)
+ TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
+ if (wpr == win->id)
+ return (win);
+
+ return (NULL);
+}
+
+void
+spawn(int ws_idx, union arg *args, int close_fd)
+{
+ int fd;
+ char *ret = NULL;
+
+ DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
+
+ if (display)
+ close(ConnectionNumber(display));
+
+ setenv("LD_PRELOAD", SWM_LIB, 1);
+
+ if (asprintf(&ret, "%d", ws_idx) == -1) {
+ warn("spawn: asprintf SWM_WS");
+ _exit(1);
+ }
+ setenv("_SWM_WS", ret, 1);
+ free(ret);
+ ret = NULL;
+
+ if (asprintf(&ret, "%d", getpid()) == -1) {
+ warn("spawn: asprintf _SWM_PID");
+ _exit(1);
+ }
+ setenv("_SWM_PID", ret, 1);
+ free(ret);
+ ret = NULL;
+
+ if (setsid() == -1) {
+ warn("spawn: setsid");
+ _exit(1);
+ }
+
+ if (close_fd) {
+ /*
+ * close stdin and stdout to prevent interaction between apps
+ * and the baraction script
+ * leave stderr open to record errors
+ */
+ if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
+ warn("spawn: open");
+ _exit(1);
+ }
+ dup2(fd, STDIN_FILENO);
+ dup2(fd, STDOUT_FILENO);
+ if (fd > 2)
+ close(fd);
+ }
+
+ execvp(args->argv[0], args->argv);
+
+ warn("spawn: execvp");
+ _exit(1);
+}
+
+void
+spawnterm(struct swm_region *r, union arg *args)
+{
+ DNPRINTF(SWM_D_MISC, "spawnterm\n");
+
+ if (fork() == 0) {
+ if (term_width)
+ setenv("_SWM_XTERM_FONTADJ", "", 1);
+ spawn(r->ws->idx, args, 1);
+ }
+}
+
+void
+kill_refs(struct ws_win *win)
+{
+ int i, x;
+ struct swm_region *r;
+ struct workspace *ws;
+
+ if (win == NULL)
+ return;
+
+ for (i = 0; i < ScreenCount(display); i++)
+ TAILQ_FOREACH(r, &screens[i].rl, entry)
+ for (x = 0; x < SWM_WS_MAX; x++) {
+ ws = &r->s->ws[x];
+ if (win == ws->focus)
+ ws->focus = NULL;
+ if (win == ws->focus_prev)
+ ws->focus_prev = NULL;
+ }
+}
+
+int
+validate_win(struct ws_win *testwin)
+{
+ struct ws_win *win;
+ struct workspace *ws;
+ struct swm_region *r;
+ int i, x;
+
+ if (testwin == NULL)
+ return (0);
+
+ for (i = 0; i < ScreenCount(display); i++)
+ TAILQ_FOREACH(r, &screens[i].rl, entry)
+ for (x = 0; x < SWM_WS_MAX; x++) {
+ ws = &r->s->ws[x];
+ TAILQ_FOREACH(win, &ws->winlist, entry)
+ if (win == testwin)
+ return (0);
+ }
+ return (1);
+}
+
+int
+validate_ws(struct workspace *testws)