+ 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: %lu\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, &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 CVS: %s",
+ SWM_VERSION, cvstag);
+ 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);
+}
+
+void
+config_win(struct ws_win *win, XConfigureRequestEvent *ev)
+{
+ XConfigureEvent ce;
+
+ if (win == NULL)
+ return;
+
+ if (ev == NULL) {
+ DNPRINTF(SWM_D_MISC,
+ "config_win: win %lu x %d y %d w %d h %d\n",
+ win->id, win->g.x, win->g.y, win->g.w, win->g.h);
+
+ ce.type = ConfigureNotify;
+ ce.display = display;
+ ce.event = win->id;
+ ce.window = win->id;
+ ce.x = win->g.x;
+ ce.y = win->g.y;
+ ce.width = win->g.w;
+ ce.height = win->g.h;
+ ce.border_width = border_width;
+ ce.above = None;
+ ce.override_redirect = False;
+ } else {
+ DNPRINTF(SWM_D_MISC,
+ "config_win: ev win %lu x %d y %d w %d h %d\n",
+ ev->window, ev->x, ev->y, ev->width, ev->height);
+ ce.type = ConfigureNotify;
+ ce.display = ev->display;
+ ce.event = ev->window;
+ ce.window = ev->window;
+ ce.x = ev->x;
+ ce.y = ev->y;
+ ce.width = ev->width;
+ ce.height = ev->height;
+ ce.border_width = ev->border_width;
+ ce.above = ev->above;
+ ce.override_redirect = False;
+ }
+
+ 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 = win->g.x;
+ event.y = win->g.y;
+ 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)
+ errx(1, "can't disable alarm");
+
+ bar_extra_stop();
+ bar_extra = 1;
+ unmap_all();
+ XCloseDisplay(display);
+ execvp(start_argv[0], start_argv);
+ fprintf(stderr, "execvp failed\n");
+ perror(" 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) {
+ perror("_SWM_WS");
+ _exit(1);
+ }
+ setenv("_SWM_WS", ret, 1);
+ free(ret);
+ ret = NULL;
+
+ if (asprintf(&ret, "%d", getpid()) == -1) {
+ perror("_SWM_PID");
+ _exit(1);
+ }
+ setenv("_SWM_PID", ret, 1);
+ free(ret);
+ ret = NULL;
+
+ if (setsid() == -1) {
+ perror("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) {
+ perror("open");
+ _exit(1);
+ }
+ dup2(fd, STDIN_FILENO);
+ dup2(fd, STDOUT_FILENO);
+ if (fd > 2)
+ close(fd);
+ }
+
+ execvp(args->argv[0], args->argv);
+
+ perror("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, foundit = 0;
+
+ if (testwin == NULL)
+ return (0);
+
+ for (i = 0, foundit = 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)
+{
+ struct swm_region *r;
+ struct workspace *ws;
+ int foundit, i, x;
+
+ /* validate all ws */
+ for (i = 0, foundit = 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 (ws == testws)
+ return (0);
+ }
+ return (1);
+}
+
+void
+unfocus_win(struct ws_win *win)
+{
+ XEvent cne;
+ Window none = None;
+
+ DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
+
+ if (win == NULL)
+ return;
+ if (win->ws == NULL)
+ return;
+
+ if (validate_ws(win->ws))
+ return; /* XXX this gets hit with thunderbird, needs fixing */
+
+ if (win->ws->r == NULL)
+ return;
+
+ if (validate_win(win)) {
+ kill_refs(win);
+ return;
+ }
+
+ if (win->ws->focus == win) {
+ win->ws->focus = NULL;
+ win->ws->focus_prev = win;
+ }
+
+ if (validate_win(win->ws->focus)) {
+ kill_refs(win->ws->focus);
+ win->ws->focus = NULL;
+ }
+ if (validate_win(win->ws->focus_prev)) {
+ kill_refs(win->ws->focus_prev);
+ win->ws->focus_prev = NULL;
+ }
+
+ /* drain all previous unfocus events */
+ while (XCheckTypedEvent(display, FocusOut, &cne) == True)
+ ;
+
+ grabbuttons(win, 0);
+ XSetWindowBorder(display, win->id,
+ win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
+
+ XChangeProperty(display, win->s->root,
+ ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
+ PropModeReplace, (unsigned char *)&none,1);
+}
+
+void
+unfocus_all(void)
+{
+ struct ws_win *win;
+ int i, j;
+
+ DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
+
+ for (i = 0; i < ScreenCount(display); i++)
+ for (j = 0; j < SWM_WS_MAX; j++)
+ TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
+ unfocus_win(win);
+}
+
+void
+focus_win(struct ws_win *win)
+{
+ XEvent cne;
+ Window cur_focus;
+ int rr;
+ struct ws_win *cfw = NULL;
+
+
+ DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
+
+ if (win == NULL)
+ return;
+ if (win->ws == NULL)
+ return;
+
+ if (validate_ws(win->ws))
+ return; /* XXX this gets hit with thunderbird, needs fixing */
+
+ if (validate_win(win)) {
+ kill_refs(win);
+ return;
+ }
+
+ if (validate_win(win)) {
+ kill_refs(win);
+ return;
+ }
+
+ XGetInputFocus(display, &cur_focus, &rr);
+ if ((cfw = find_window(cur_focus)) != NULL)
+ unfocus_win(cfw);
+ else {
+ /* use larger hammer since the window was killed somehow */
+ TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
+ if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
+ XSetWindowBorder(display, cfw->id,
+ cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
+ }
+
+ win->ws->focus = win;
+
+ if (win->ws->r != NULL) {
+ /* drain all previous focus events */
+ while (XCheckTypedEvent(display, FocusIn, &cne) == True)
+ ;
+
+ if (win->java == 0)
+ XSetInputFocus(display, win->id,
+ RevertToParent, CurrentTime);
+ grabbuttons(win, 1);
+ XSetWindowBorder(display, win->id,
+ win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
+ if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
+ win->ws->always_raise)
+ XMapRaised(display, win->id);
+
+ XChangeProperty(display, win->s->root,
+ ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
+ PropModeReplace, (unsigned char *)&win->id,1);
+ }
+
+ if (window_name_enabled)
+ bar_update();
+}
+
+void
+switchws(struct swm_region *r, union arg *args)
+{
+ int wsid = args->id, unmap_old = 0;
+ struct swm_region *this_r, *other_r;
+ struct ws_win *win;
+ struct workspace *new_ws, *old_ws;
+ union arg a;
+
+ if (!(r && r->s))
+ return;
+
+ this_r = r;
+ old_ws = this_r->ws;
+ new_ws = &this_r->s->ws[wsid];
+
+ DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
+ "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
+ old_ws->idx, wsid);
+
+ if (new_ws == NULL || old_ws == NULL)
+ return;
+ if (new_ws == old_ws)
+ return;
+
+ other_r = new_ws->r;
+ if (other_r == NULL) {
+ /* the other workspace is hidden, hide this one */
+ old_ws->r = NULL;
+ unmap_old = 1;
+ } else {
+ /* the other ws is visible in another region, exchange them */
+ other_r->ws_prior = new_ws;
+ other_r->ws = old_ws;
+ old_ws->r = other_r;
+ }
+ this_r->ws_prior = old_ws;
+ this_r->ws = new_ws;
+ new_ws->r = this_r;
+
+ /* this is needed so that we can click on a window after a restart */
+ unfocus_all();
+
+ stack();
+ a.id = SWM_ARG_ID_FOCUSCUR;
+ focus(new_ws->r, &a);
+
+ bar_update();
+
+ /* unmap old windows */
+ if (unmap_old)
+ TAILQ_FOREACH(win, &old_ws->winlist, entry)
+ unmap_window(win);
+
+ if (focus_mode == SWM_FOCUS_DEFAULT)
+ drain_enter_notify();
+}
+
+void
+cyclews(struct swm_region *r, union arg *args)
+{
+ union arg a;
+ struct swm_screen *s = r->s;
+
+ DNPRINTF(SWM_D_WS, "cyclews id %d "
+ "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
+ r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
+
+ a.id = r->ws->idx;
+ do {
+ switch (args->id) {
+ case SWM_ARG_ID_CYCLEWS_UP:
+ if (a.id < SWM_WS_MAX - 1)
+ a.id++;
+ else
+ a.id = 0;
+ break;
+ case SWM_ARG_ID_CYCLEWS_DOWN:
+ if (a.id > 0)
+ a.id--;
+ else
+ a.id = SWM_WS_MAX - 1;
+ break;
+ default:
+ return;
+ };
+
+ if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
+ continue;
+ if (cycle_visible == 0 && s->ws[a.id].r != NULL)
+ continue;
+
+ switchws(r, &a);
+ } while (a.id != r->ws->idx);
+}
+
+void
+priorws(struct swm_region *r, union arg *args)
+{
+ union arg a;
+
+ DNPRINTF(SWM_D_WS, "priorws id %d "
+ "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
+ r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
+
+ if (r->ws_prior == NULL)
+ return;
+
+ a.id = r->ws_prior->idx;
+ switchws(r, &a);
+}
+
+void
+cyclescr(struct swm_region *r, union arg *args)
+{
+ struct swm_region *rr = NULL;
+ union arg a;
+ int i, x, y;
+
+ /* do nothing if we don't have more than one screen */
+ if (!(ScreenCount(display) > 1 || outputs > 1))
+ return;
+
+ i = r->s->idx;
+ switch (args->id) {
+ case SWM_ARG_ID_CYCLESC_UP:
+ rr = TAILQ_NEXT(r, entry);
+ if (rr == NULL)
+ rr = TAILQ_FIRST(&screens[i].rl);
+ break;
+ case SWM_ARG_ID_CYCLESC_DOWN:
+ rr = TAILQ_PREV(r, swm_region_list, entry);
+ if (rr == NULL)
+ rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
+ break;
+ default:
+ return;
+ };
+ if (rr == NULL)
+ return;
+
+ /* move mouse to region */
+ x = rr->g.x + 1;
+ y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
+ XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
+
+ a.id = SWM_ARG_ID_FOCUSCUR;
+ focus(rr, &a);
+
+ if (rr->ws->focus) {
+ /* move to focus window */
+ x = rr->ws->focus->g.x + 1;
+ y = rr->ws->focus->g.y + 1;
+ XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
+ }
+}
+
+void
+sort_windows(struct ws_win_list *wl)
+{
+ struct ws_win *win, *parent, *nxt;
+
+ if (wl == NULL)
+ return;
+
+ for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
+ nxt = TAILQ_NEXT(win, entry);
+ if (win->transient) {
+ parent = find_window(win->transient);
+ if (parent == NULL) {
+ fprintf(stderr, "not possible bug\n");
+ continue;
+ }
+ TAILQ_REMOVE(wl, win, entry);
+ TAILQ_INSERT_AFTER(wl, parent, win, entry);
+ }
+ }
+
+}
+
+void
+swapwin(struct swm_region *r, union arg *args)
+{
+ struct ws_win *target, *source;
+ struct ws_win *cur_focus;
+ struct ws_win_list *wl;
+
+
+ DNPRINTF(SWM_D_WS, "swapwin id %d "
+ "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
+ r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
+
+ cur_focus = r->ws->focus;
+ if (cur_focus == NULL)
+ return;
+
+ source = cur_focus;
+ wl = &source->ws->winlist;
+
+ switch (args->id) {
+ case SWM_ARG_ID_SWAPPREV:
+ if (source->transient)
+ source = find_window(source->transient);
+ target = TAILQ_PREV(source, ws_win_list, entry);
+ if (target && target->transient)
+ target = find_window(target->transient);
+ TAILQ_REMOVE(wl, source, entry);
+ if (target == NULL)
+ TAILQ_INSERT_TAIL(wl, source, entry);
+ else
+ TAILQ_INSERT_BEFORE(target, source, entry);
+ break;
+ case SWM_ARG_ID_SWAPNEXT:
+ target = TAILQ_NEXT(source, entry);
+ /* move the parent and let the sort handle the move */
+ if (source->transient)
+ source = find_window(source->transient);
+ TAILQ_REMOVE(wl, source, entry);
+ if (target == NULL)
+ TAILQ_INSERT_HEAD(wl, source, entry);
+ else
+ TAILQ_INSERT_AFTER(wl, target, source, entry);
+ break;
+ case SWM_ARG_ID_SWAPMAIN:
+ target = TAILQ_FIRST(wl);
+ if (target == source) {
+ if (source->ws->focus_prev != NULL &&
+ source->ws->focus_prev != target)
+
+ source = source->ws->focus_prev;
+ else
+ return;
+ }
+ if (target == NULL || source == NULL)
+ return;
+ source->ws->focus_prev = target;
+ TAILQ_REMOVE(wl, target, entry);
+ TAILQ_INSERT_BEFORE(source, target, entry);
+ TAILQ_REMOVE(wl, source, entry);
+ TAILQ_INSERT_HEAD(wl, source, entry);
+ break;
+ case SWM_ARG_ID_MOVELAST:
+ TAILQ_REMOVE(wl, source, entry);
+ TAILQ_INSERT_TAIL(wl, source, entry);
+ break;
+ default:
+ DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
+ return;
+ }
+
+ sort_windows(wl);
+
+ stack();
+}
+
+void
+focus_prev(struct ws_win *win)
+{
+ struct ws_win *winfocus = NULL, *winlostfocus = NULL;
+ struct ws_win *cur_focus = NULL;
+ struct ws_win_list *wl = NULL;
+ struct workspace *ws = NULL;
+
+ DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
+
+ if (!(win && win->ws))
+ return;
+
+ ws = win->ws;
+ wl = &ws->winlist;
+ cur_focus = ws->focus;
+ winlostfocus = cur_focus;
+
+ /* pickle, just focus on whatever */
+ if (cur_focus == NULL) {
+ /* use prev_focus if valid */
+ if (ws->focus_prev && ws->focus_prev != cur_focus &&
+ find_window(WINID(ws->focus_prev)))
+ winfocus = ws->focus_prev;
+ if (winfocus == NULL)
+ winfocus = TAILQ_FIRST(wl);
+ goto done;
+ }
+
+ /* if transient focus on parent */
+ if (cur_focus->transient) {
+ winfocus = find_window(cur_focus->transient);
+ goto done;
+ }
+
+ /* if in max_stack try harder */
+ if ((win->quirks & SWM_Q_FOCUSPREV) ||
+ (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
+ if (cur_focus != ws->focus_prev)
+ winfocus = ws->focus_prev;
+ else if (cur_focus != ws->focus)
+ winfocus = ws->focus;
+ else
+ winfocus = TAILQ_PREV(win, ws_win_list, entry);
+ if (winfocus)
+ goto done;
+ }
+
+ if (cur_focus == win)
+ winfocus = TAILQ_PREV(win, ws_win_list, entry);
+ if (winfocus == NULL)
+ winfocus = TAILQ_LAST(wl, ws_win_list);
+ if (winfocus == NULL || winfocus == win)
+ winfocus = TAILQ_NEXT(cur_focus, entry);
+done:
+ if (winfocus == winlostfocus || winfocus == NULL)
+ return;
+
+ focus_magic(winfocus);
+}
+
+void
+focus(struct swm_region *r, union arg *args)
+{
+ struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
+ struct ws_win *cur_focus = NULL;
+ struct ws_win_list *wl = NULL;
+ struct workspace *ws = NULL;
+
+ if (!(r && r->ws))
+ return;
+
+ DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
+
+ /* treat FOCUS_CUR special */
+ if (args->id == SWM_ARG_ID_FOCUSCUR) {
+ if (r->ws->focus && r->ws->focus->iconic == 0)
+ winfocus = r->ws->focus;
+ else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
+ winfocus = r->ws->focus_prev;
+ else
+ TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
+ if (winfocus->iconic == 0)
+ break;
+
+ focus_magic(winfocus);
+ return;
+ }
+
+ if ((cur_focus = r->ws->focus) == NULL)
+ return;
+ ws = r->ws;
+ wl = &ws->winlist;
+
+ winlostfocus = cur_focus;
+
+ switch (args->id) {
+ case SWM_ARG_ID_FOCUSPREV:
+ head = TAILQ_PREV(cur_focus, ws_win_list, entry);
+ if (head == NULL)
+ head = TAILQ_LAST(wl, ws_win_list);
+ winfocus = head;
+ if (WINID(winfocus) == cur_focus->transient) {
+ head = TAILQ_PREV(winfocus, ws_win_list, entry);
+ if (head == NULL)
+ head = TAILQ_LAST(wl, ws_win_list);
+ winfocus = head;
+ }
+
+ /* skip iconics */
+ if (winfocus && winfocus->iconic) {
+ TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
+ if (winfocus->iconic == 0)
+ break;
+ }
+ break;
+
+ case SWM_ARG_ID_FOCUSNEXT:
+ head = TAILQ_NEXT(cur_focus, entry);
+ if (head == NULL)
+ head = TAILQ_FIRST(wl);
+ winfocus = head;
+
+ /* skip iconics */
+ if (winfocus && winfocus->iconic) {
+ TAILQ_FOREACH(winfocus, wl, entry)
+ if (winfocus->iconic == 0)
+ break;
+ }
+ break;
+
+ case SWM_ARG_ID_FOCUSMAIN:
+ winfocus = TAILQ_FIRST(wl);
+ if (winfocus == cur_focus)
+ winfocus = cur_focus->ws->focus_prev;
+ break;
+
+ default:
+ return;
+ }
+ if (winfocus == winlostfocus || winfocus == NULL)
+ return;
+
+ focus_magic(winfocus);
+}
+
+void
+cycle_layout(struct swm_region *r, union arg *args)
+{
+ struct workspace *ws = r->ws;
+ struct ws_win *winfocus;
+ union arg a;
+
+ DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
+
+ winfocus = ws->focus;
+
+ ws->cur_layout++;
+ if (ws->cur_layout->l_stack == NULL)
+ ws->cur_layout = &layouts[0];
+
+ stack();
+ if (focus_mode == SWM_FOCUS_DEFAULT)
+ drain_enter_notify();
+ a.id = SWM_ARG_ID_FOCUSCUR;
+ focus(r, &a);
+ bar_update();
+}
+
+void
+stack_config(struct swm_region *r, union arg *args)
+{
+ struct workspace *ws = r->ws;
+
+ DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
+ args->id, ws->idx);
+
+ if (ws->cur_layout->l_config != NULL)
+ ws->cur_layout->l_config(ws, args->id);
+
+ if (args->id != SWM_ARG_ID_STACKINIT)
+ stack();
+ bar_update();
+}
+
+void
+stack(void) {
+ struct swm_geometry g;
+ struct swm_region *r;
+ int i, j;
+
+ DNPRINTF(SWM_D_STACK, "stack\n");
+
+ for (i = 0; i < ScreenCount(display); i++) {
+ j = 0;
+ TAILQ_FOREACH(r, &screens[i].rl, entry) {
+ DNPRINTF(SWM_D_STACK, "stacking workspace %d "
+ "(screen %d, region %d)\n", r->ws->idx, i, j++);
+
+ /* start with screen geometry, adjust for bar */
+ g = r->g;
+ g.w -= 2 * border_width;
+ g.h -= 2 * border_width;
+ if (bar_enabled) {
+ if (!bar_at_bottom)
+ g.y += bar_height;
+ g.h -= bar_height;
+ }
+ r->ws->cur_layout->l_stack(r->ws, &g);
+ r->ws->cur_layout->l_string(r->ws);
+ /* save r so we can track region changes */
+ r->ws->old_r = r;
+ }
+ }
+ if (font_adjusted)
+ font_adjusted--;
+
+ if (focus_mode == SWM_FOCUS_DEFAULT)
+ drain_enter_notify();
+}
+
+void
+store_float_geom(struct ws_win *win, struct swm_region *r)
+{
+ /* retain window geom and region geom */
+ win->g_float.x = win->g.x;
+ win->g_float.y = win->g.y;
+ win->g_float.w = win->g.w;
+ win->g_float.h = win->g.h;
+ win->rg_float.x = r->g.x;
+ win->rg_float.y = r->g.y;
+ win->rg_float.w = r->g.w;
+ win->rg_float.h = r->g.h;
+ win->g_floatvalid = 1;
+}
+
+void
+stack_floater(struct ws_win *win, struct swm_region *r)
+{
+ unsigned int mask;
+ XWindowChanges wc;
+
+ if (win == NULL)
+ return;
+
+ bzero(&wc, sizeof wc);
+ mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
+
+ /*
+ * to allow windows to change their size (e.g. mplayer fs) only retrieve
+ * geom on ws switches or return from max mode
+ */
+
+ if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
+ && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
+ /*
+ * use stored g and rg to set relative position and size
+ * as in old region or before max stack mode
+ */
+ win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
+ win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
+ win->g.w = win->g_float.w;
+ win->g.h = win->g_float.h;
+ win->g_floatvalid = 0;
+ }
+
+ win->floatmaxed = 0;
+
+ if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
+ (win->g.h >= HEIGHT(r)))
+ wc.border_width = 0;
+ else
+ wc.border_width = border_width;
+ if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
+ win->g.w = (double)WIDTH(r) * dialog_ratio;
+ win->g.h = (double)HEIGHT(r) * dialog_ratio;
+ }
+
+ if (!win->manual) {
+ /*
+ * floaters and transients are auto-centred unless moved
+ * or resized
+ */
+ win->g.x = r->g.x + (WIDTH(r) - win->g.w) /
+ 2 - wc.border_width;
+ win->g.y = r->g.y + (HEIGHT(r) - win->g.h) /
+ 2 - wc.border_width;
+ }
+
+ /* win can be outside r if new r smaller than old r */
+ /* Ensure top left corner inside r (move probs otherwise) */
+ if (win->g.x < r->g.x - wc.border_width)
+ win->g.x = r->g.x - wc.border_width;
+ if (win->g.x > r->g.x + r->g.w - 1)
+ win->g.x = (win->g.w > r->g.w) ? r->g.x :
+ (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
+ if (win->g.y < r->g.y - wc.border_width)
+ win->g.y = r->g.y - wc.border_width;
+ if (win->g.y > r->g.y + r->g.h - 1)
+ win->g.y = (win->g.h > r->g.h) ? r->g.y :
+ (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
+
+ wc.x = win->g.x;
+ wc.y = win->g.y;
+ wc.width = win->g.w;
+ wc.height = win->g.h;
+
+ /*
+ * Retain floater and transient geometry for correct positioning
+ * when ws changes region
+ */
+ if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
+ store_float_geom(win, r);
+
+ DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
+ win->id, wc.x, wc.y, wc.width, wc.height);
+
+ XConfigureWindow(display, win->id, mask, &wc);
+}
+
+/*
+ * Send keystrokes to terminal to decrease/increase the font size as the
+ * window size changes.
+ */
+void
+adjust_font(struct ws_win *win)
+{
+ if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
+ win->floating || win->transient)
+ return;
+
+ if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
+ win->g.w / win->sh.width_inc < term_width &&
+ win->font_steps < SWM_MAX_FONT_STEPS) {
+ win->font_size_boundary[win->font_steps] =
+ (win->sh.width_inc * term_width) + win->sh.base_width;
+ win->font_steps++;
+ font_adjusted++;
+ win->last_inc = win->sh.width_inc;
+ fake_keypress(win, XK_KP_Subtract, ShiftMask);
+ } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
+ win->g.w > win->font_size_boundary[win->font_steps - 1]) {
+ win->font_steps--;
+ font_adjusted++;
+ win->last_inc = win->sh.width_inc;
+ fake_keypress(win, XK_KP_Add, ShiftMask);
+ }
+}
+
+#define SWAPXY(g) do { \
+ int tmp; \
+ tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
+ tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
+} while (0)
+void
+stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
+{
+ XWindowChanges wc;
+ XWindowAttributes wa;
+ struct swm_geometry win_g, r_g = *g;
+ struct ws_win *win, *fs_win = 0;
+ int i, j, s, stacks;
+ int w_inc = 1, h_inc, w_base = 1, h_base;
+ int hrh, extra = 0, h_slice, last_h = 0;
+ int split, colno, winno, mwin, msize, mscale;
+ int remain, missing, v_slice, reconfigure;
+ unsigned int mask;
+
+ DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
+ ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
+
+ winno = count_win(ws, 0);
+ if (winno == 0 && count_win(ws, 1) == 0)
+ return;
+
+ TAILQ_FOREACH(win, &ws->winlist, entry)
+ if (win->transient == 0 && win->floating == 0
+ && win->iconic == 0)
+ break;
+
+ if (win == NULL)
+ goto notiles;
+
+ if (rot) {
+ w_inc = win->sh.width_inc;
+ w_base = win->sh.base_width;
+ mwin = ws->l_state.horizontal_mwin;
+ mscale = ws->l_state.horizontal_msize;
+ stacks = ws->l_state.horizontal_stacks;
+ SWAPXY(&r_g);
+ } else {
+ w_inc = win->sh.height_inc;
+ w_base = win->sh.base_height;
+ mwin = ws->l_state.vertical_mwin;
+ mscale = ws->l_state.vertical_msize;
+ stacks = ws->l_state.vertical_stacks;
+ }
+ win_g = r_g;
+
+ if (stacks > winno - mwin)
+ stacks = winno - mwin;
+ if (stacks < 1)
+ stacks = 1;
+
+ h_slice = r_g.h / SWM_H_SLICE;
+ if (mwin && winno > mwin) {
+ v_slice = r_g.w / SWM_V_SLICE;
+
+ split = mwin;
+ colno = split;
+ win_g.w = v_slice * mscale;
+
+ if (w_inc > 1 && w_inc < v_slice) {
+ /* adjust for window's requested size increment */
+ remain = (win_g.w - w_base) % w_inc;
+ missing = w_inc - remain;
+ win_g.w -= remain;
+ extra += remain;
+ }
+
+ msize = win_g.w;
+ if (flip)
+ win_g.x += r_g.w - msize;
+ } else {
+ msize = -2;
+ colno = split = winno / stacks;
+ win_g.w = ((r_g.w - (stacks * 2 * border_width) +
+ 2 * border_width) / stacks);
+ }
+ hrh = r_g.h / colno;
+ extra = r_g.h - (colno * hrh);
+ win_g.h = hrh - 2 * border_width;
+
+ /* stack all the tiled windows */
+ i = j = 0, s = stacks;
+ TAILQ_FOREACH(win, &ws->winlist, entry) {
+ if (win->transient != 0 || win->floating != 0)
+ continue;
+ if (win->iconic != 0)
+ continue;
+
+ if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
+ fs_win = win;
+ continue;
+ }
+
+ if (split && i == split) {
+ colno = (winno - mwin) / stacks;
+ if (s <= (winno - mwin) % stacks)
+ colno++;
+ split = split + colno;
+ hrh = (r_g.h / colno);
+ extra = r_g.h - (colno * hrh);
+ if (flip)
+ win_g.x = r_g.x;
+ else
+ win_g.x += win_g.w + 2 * border_width;
+ win_g.w = (r_g.w - msize -
+ (stacks * 2 * border_width)) / stacks;
+ if (s == 1)
+ win_g.w += (r_g.w - msize -
+ (stacks * 2 * border_width)) % stacks;
+ s--;
+ j = 0;
+ }
+ win_g.h = hrh - 2 * border_width;
+ if (rot) {
+ h_inc = win->sh.width_inc;
+ h_base = win->sh.base_width;
+ } else {
+ h_inc = win->sh.height_inc;
+ h_base = win->sh.base_height;
+ }
+ if (j == colno - 1) {
+ win_g.h = hrh + extra;
+ } else if (h_inc > 1 && h_inc < h_slice) {
+ /* adjust for window's requested size increment */
+ remain = (win_g.h - h_base) % h_inc;
+ missing = h_inc - remain;
+
+ if (missing <= extra || j == 0) {
+ extra -= missing;
+ win_g.h += missing;
+ } else {
+ win_g.h -= remain;
+ extra += remain;
+ }
+ }
+
+ if (j == 0)
+ win_g.y = r_g.y;
+ else
+ win_g.y += last_h + 2 * border_width;
+
+ bzero(&wc, sizeof wc);
+ if (disable_border && bar_enabled == 0 && winno == 1){
+ wc.border_width = 0;
+ win_g.w += 2 * border_width;
+ win_g.h += 2 * border_width;
+ } else
+ wc.border_width = border_width;
+ reconfigure = 0;
+ if (rot) {
+ if (win->g.x != win_g.y || win->g.y != win_g.x ||
+ win->g.w != win_g.h || win->g.h != win_g.w) {
+ reconfigure = 1;
+ win->g.x = wc.x = win_g.y;
+ win->g.y = wc.y = win_g.x;
+ win->g.w = wc.width = win_g.h;
+ win->g.h = wc.height = win_g.w;
+ }
+ } else {
+ if (win->g.x != win_g.x || win->g.y != win_g.y ||
+ win->g.w != win_g.w || win->g.h != win_g.h) {
+ reconfigure = 1;
+ win->g.x = wc.x = win_g.x;
+ win->g.y = wc.y = win_g.y;
+ win->g.w = wc.width = win_g.w;
+ win->g.h = wc.height = win_g.h;
+ }
+ }
+ if (reconfigure) {
+ adjust_font(win);
+ mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
+ XConfigureWindow(display, win->id, mask, &wc);
+ }
+
+ if (XGetWindowAttributes(display, win->id, &wa))
+ if (wa.map_state == IsUnmapped)
+ XMapRaised(display, win->id);
+
+ last_h = win_g.h;
+ i++;
+ j++;
+ }
+
+notiles:
+ /* now, stack all the floaters and transients */
+ TAILQ_FOREACH(win, &ws->winlist, entry) {
+ if (win->transient == 0 && win->floating == 0)
+ continue;
+ if (win->iconic == 1)
+ continue;
+ if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
+ fs_win = win;
+ continue;
+ }
+
+ stack_floater(win, ws->r);
+ XMapRaised(display, win->id);
+ }
+
+ if (fs_win) {
+ stack_floater(fs_win, ws->r);
+ XMapRaised(display, fs_win->id);
+ }
+}
+
+void
+vertical_config(struct workspace *ws, int id)
+{
+ DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
+
+ switch (id) {
+ case SWM_ARG_ID_STACKRESET:
+ case SWM_ARG_ID_STACKINIT:
+ ws->l_state.vertical_msize = SWM_V_SLICE / 2;
+ ws->l_state.vertical_mwin = 1;
+ ws->l_state.vertical_stacks = 1;
+ break;
+ case SWM_ARG_ID_MASTERSHRINK:
+ if (ws->l_state.vertical_msize > 1)
+ ws->l_state.vertical_msize--;
+ break;
+ case SWM_ARG_ID_MASTERGROW:
+ if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
+ ws->l_state.vertical_msize++;
+ break;
+ case SWM_ARG_ID_MASTERADD:
+ ws->l_state.vertical_mwin++;
+ break;
+ case SWM_ARG_ID_MASTERDEL:
+ if (ws->l_state.vertical_mwin > 0)
+ ws->l_state.vertical_mwin--;
+ break;
+ case SWM_ARG_ID_STACKINC:
+ ws->l_state.vertical_stacks++;
+ break;
+ case SWM_ARG_ID_STACKDEC:
+ if (ws->l_state.vertical_stacks > 1)
+ ws->l_state.vertical_stacks--;
+ break;
+ default:
+ return;
+ }
+}
+
+void
+vertical_stack(struct workspace *ws, struct swm_geometry *g)
+{
+ DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
+
+ stack_master(ws, g, 0, 0);
+}
+
+void
+horizontal_config(struct workspace *ws, int id)
+{
+ DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
+
+ switch (id) {
+ case SWM_ARG_ID_STACKRESET:
+ case SWM_ARG_ID_STACKINIT:
+ ws->l_state.horizontal_mwin = 1;
+ ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
+ ws->l_state.horizontal_stacks = 1;
+ break;
+ case SWM_ARG_ID_MASTERSHRINK:
+ if (ws->l_state.horizontal_msize > 1)
+ ws->l_state.horizontal_msize--;
+ break;
+ case SWM_ARG_ID_MASTERGROW:
+ if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
+ ws->l_state.horizontal_msize++;
+ break;
+ case SWM_ARG_ID_MASTERADD:
+ ws->l_state.horizontal_mwin++;
+ break;
+ case SWM_ARG_ID_MASTERDEL:
+ if (ws->l_state.horizontal_mwin > 0)
+ ws->l_state.horizontal_mwin--;
+ break;
+ case SWM_ARG_ID_STACKINC:
+ ws->l_state.horizontal_stacks++;
+ break;
+ case SWM_ARG_ID_STACKDEC:
+ if (ws->l_state.horizontal_stacks > 1)
+ ws->l_state.horizontal_stacks--;
+ break;
+ default:
+ return;
+ }
+}
+
+void
+horizontal_stack(struct workspace *ws, struct swm_geometry *g)
+{
+ DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
+
+ stack_master(ws, g, 1, 0);
+}
+
+/* fullscreen view */
+void
+max_stack(struct workspace *ws, struct swm_geometry *g)
+{
+ XWindowChanges wc;
+ struct swm_geometry gg = *g;
+ struct ws_win *win, *wintrans = NULL, *parent = NULL;
+ unsigned int mask;
+ int winno;
+
+ DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
+
+ if (ws == NULL)
+ return;
+
+ winno = count_win(ws, 0);
+ if (winno == 0 && count_win(ws, 1) == 0)
+ return;
+
+ TAILQ_FOREACH(win, &ws->winlist, entry) {
+ if (win->transient) {
+ wintrans = win;
+ parent = find_window(win->transient);
+ continue;
+ }
+
+ if (win->floating && win->floatmaxed == 0 ) {
+ /*
+ * retain geometry for retrieval on exit from
+ * max_stack mode
+ */
+ store_float_geom(win, ws->r);
+ win->floatmaxed = 1;
+ }
+
+ /* only reconfigure if necessary */
+ if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
+ win->g.h != gg.h) {
+ bzero(&wc, sizeof wc);
+ win->g.x = wc.x = gg.x;
+ win->g.y = wc.y = gg.y;
+ if (bar_enabled){
+ wc.border_width = border_width;
+ win->g.w = wc.width = gg.w;
+ win->g.h = wc.height = gg.h;
+ } else {
+ wc.border_width = 0;
+ win->g.w = wc.width = gg.w + 2 * border_width;
+ win->g.h = wc.height = gg.h + 2 * border_width;
+ }
+ mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
+ XConfigureWindow(display, win->id, mask, &wc);
+ }
+ /* unmap only if we don't have multi screen */
+ if (win != ws->focus)
+ if (!(ScreenCount(display) > 1 || outputs > 1))
+ unmap_window(win);
+ }
+
+ /* put the last transient on top */
+ if (wintrans) {
+ if (parent)
+ XMapRaised(display, parent->id);
+ stack_floater(wintrans, ws->r);
+ focus_magic(wintrans);
+ }
+}
+
+void
+send_to_ws(struct swm_region *r, union arg *args)
+{
+ int wsid = args->id;
+ struct ws_win *win = NULL, *parent;
+ struct workspace *ws, *nws;
+ Atom ws_idx_atom = 0;
+ unsigned char ws_idx_str[SWM_PROPLEN];
+ union arg a;
+
+ if (r && r->ws)
+ win = r->ws->focus;
+ else
+ return;
+ if (win == NULL)
+ return;
+ if (win->ws->idx == wsid)
+ return;
+
+ DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
+
+ ws = win->ws;
+ nws = &win->s->ws[wsid];
+
+ a.id = SWM_ARG_ID_FOCUSPREV;
+ focus(r, &a);
+ if (win->transient) {
+ parent = find_window(win->transient);
+ if (parent) {
+ unmap_window(parent);
+ TAILQ_REMOVE(&ws->winlist, parent, entry);
+ TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
+ parent->ws = nws;
+ }
+ }
+ unmap_window(win);
+ TAILQ_REMOVE(&ws->winlist, win, entry);
+ TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
+ win->ws = nws;
+
+ /* Try to update the window's workspace property */
+ ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
+ if (ws_idx_atom &&
+ snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
+ SWM_PROPLEN) {
+ DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
+ ws_idx_str);
+ XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
+ PropModeReplace, ws_idx_str, SWM_PROPLEN);
+ }