+spawnmenu(struct swm_region *r, union arg *args)
+{
+ DNPRINTF(SWM_D_MISC, "spawnmenu\n");
+
+ spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
+ spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
+ spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
+ spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
+ spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
+
+ spawn(r, args);
+}
+
+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) {
+ if (win->ws->r == NULL)
+ continue;
+ grabbuttons(win, 0);
+ XSetWindowBorder(display, win->id,
+ win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
+ win->got_focus = 0;
+ win->ws->focus = NULL;
+ cur_focus = NULL;
+ }
+}
+
+void
+focus_win(struct ws_win *win)
+{
+ DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
+
+ if (win == NULL)
+ return;
+
+ unfocus_all();
+ win->ws->focus = win;
+ if (win->ws->r != NULL) {
+ cur_focus = win;
+ if (!win->got_focus) {
+ XSetWindowBorder(display, win->id,
+ win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
+ grabbuttons(win, 1);
+ }
+ win->got_focus = 1;
+ XSetInputFocus(display, win->id,
+ RevertToPointerRoot, CurrentTime);
+ }
+}
+
+void
+switchws(struct swm_region *r, union arg *args)
+{
+ int wsid = args->id;
+ struct swm_region *this_r, *other_r;
+ struct ws_win *win;
+ struct workspace *new_ws, *old_ws;
+
+ this_r = r;
+ old_ws = this_r->ws;
+ new_ws = &this_r->s->ws[wsid];
+
+ DNPRINTF(SWM_D_WS, "switchws screen %d region %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 == old_ws)
+ return;
+
+ other_r = new_ws->r;
+ if (!other_r) {
+ /* if the other workspace is hidden, switch windows */
+ /* map new window first to prevent ugly blinking */
+ TAILQ_FOREACH(win, &new_ws->winlist, entry)
+ XMapRaised(display, win->id);
+
+ TAILQ_FOREACH(win, &old_ws->winlist, entry)
+ XUnmapWindow(display, win->id);
+ old_ws->r = NULL;
+ old_ws->restack = 1;
+ } else {
+ other_r->ws = old_ws;
+ old_ws->r = other_r;
+ }
+ this_r->ws = new_ws;
+ new_ws->r = this_r;
+
+ ignore_enter = 1;
+ /* set focus */
+ if (new_ws->focus)
+ focus_win(new_ws->focus);
+ stack();
+ bar_update();
+}
+
+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 region %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
+cyclescr(struct swm_region *r, union arg *args)
+{
+ struct swm_region *rr;
+ int i;
+
+ 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;
+ };
+ unfocus_all();
+ XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
+ XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, rr->g.x,
+ rr->g.y + bar_enabled ? bar_height : 0);
+}
+
+void
+swapwin(struct swm_region *r, union arg *args)
+{
+ struct ws_win *target;
+ 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);
+ if (cur_focus == NULL)
+ return;
+
+ wl = &cur_focus->ws->winlist;
+
+ switch (args->id) {
+ case SWM_ARG_ID_SWAPPREV:
+ target = TAILQ_PREV(cur_focus, ws_win_list, entry);
+ TAILQ_REMOVE(wl, cur_focus, entry);
+ if (target == NULL)
+ TAILQ_INSERT_TAIL(wl, cur_focus, entry);
+ else
+ TAILQ_INSERT_BEFORE(target, cur_focus, entry);
+ break;
+ case SWM_ARG_ID_SWAPNEXT:
+ target = TAILQ_NEXT(cur_focus, entry);
+ TAILQ_REMOVE(wl, cur_focus, entry);
+ if (target == NULL)
+ TAILQ_INSERT_HEAD(wl, cur_focus, entry);
+ else
+ TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
+ break;
+ case SWM_ARG_ID_SWAPMAIN:
+ target = TAILQ_FIRST(wl);
+ if (target == cur_focus)
+ return;
+ TAILQ_REMOVE(wl, target, entry);
+ TAILQ_INSERT_BEFORE(cur_focus, target, entry);
+ TAILQ_REMOVE(wl, cur_focus, entry);
+ TAILQ_INSERT_HEAD(wl, cur_focus, entry);
+ break;
+ default:
+ DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
+ return;
+ }
+
+ ignore_enter = 2;
+ stack();
+}
+
+void
+focus(struct swm_region *r, union arg *args)
+{
+ struct ws_win *winfocus, *winlostfocus;
+ struct ws_win_list *wl;
+
+ DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
+ if (cur_focus == NULL)
+ return;
+
+ wl = &cur_focus->ws->winlist;
+
+ winlostfocus = cur_focus;
+
+ switch (args->id) {
+ case SWM_ARG_ID_FOCUSPREV:
+ winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
+ if (winfocus == NULL)
+ winfocus = TAILQ_LAST(wl, ws_win_list);
+ break;
+
+ case SWM_ARG_ID_FOCUSNEXT:
+ winfocus = TAILQ_NEXT(cur_focus, entry);
+ if (winfocus == NULL)
+ winfocus = TAILQ_FIRST(wl);
+ break;
+
+ case SWM_ARG_ID_FOCUSMAIN:
+ winfocus = TAILQ_FIRST(wl);
+ break;
+
+ default:
+ return;
+ }
+
+ if (winfocus == winlostfocus || winfocus == NULL)
+ return;
+
+ XMapRaised(display, winfocus->id);
+ focus_win(winfocus);
+ XSync(display, False);
+}
+
+void
+cycle_layout(struct swm_region *r, union arg *args)
+{
+ struct workspace *ws = r->ws;
+
+ DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
+
+ ws->cur_layout++;
+ if (ws->cur_layout->l_stack == NULL)
+ ws->cur_layout = &layouts[0];
+ ignore_enter = 1;
+ stack();
+}
+
+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();
+}
+
+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;
+ g.h -= 2;
+ if (bar_enabled) {
+ g.y += bar_height;
+ g.h -= bar_height;
+ }
+
+ r->ws->restack = 0;
+ r->ws->cur_layout->l_stack(r->ws, &g);
+ }
+ }
+ XSync(display, False);
+}
+
+void
+stack_floater(struct ws_win *win, struct swm_region *r)
+{
+ unsigned int mask;
+ XWindowChanges wc;
+
+ bzero(&wc, sizeof wc);
+ mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
+ wc.border_width = 1;
+ if (win->transient) {
+ win->g.w = (double)WIDTH(r) * dialog_ratio;
+ win->g.h = (double)HEIGHT(r) * dialog_ratio;
+ }
+ wc.width = win->g.w;
+ wc.height = win->g.h;
+ if (win->manual) {
+ wc.x = win->g.x;
+ wc.y = win->g.y;
+ } else {
+ wc.x = (WIDTH(r) - win->g.w) / 2;
+ wc.y = (HEIGHT(r) - win->g.h) / 2;
+ }
+
+ DNPRINTF(SWM_D_STACK, "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);
+}
+
+#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;
+ struct swm_geometry win_g, r_g = *g;
+ struct ws_win *win, *winfocus;
+ int i, j, w_inc, h_inc, w_base, h_base;
+ int hrh, extra, h_slice, last_h = 0;
+ int split, colno, winno, mwin, msize, mscale;
+ int remain, missing, v_slice;;
+ unsigned int mask;
+
+ DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
+ ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
+
+ if ((winno = count_win(ws, 0)) == 0)
+ return;
+
+ if (ws->focus == NULL)
+ ws->focus = TAILQ_FIRST(&ws->winlist);
+ winfocus = cur_focus ? cur_focus : ws->focus;
+
+ win = TAILQ_FIRST(&ws->winlist);
+ 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;
+ 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;
+ }
+ win_g = r_g;
+
+ h_slice = r_g.h / SWM_H_SLICE;
+ if (mwin && winno > mwin) {
+ v_slice = r_g.w / SWM_V_SLICE;
+
+ split = mwin;
+ colno = split;
+ msize = 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;
+
+ if (missing <= extra || j == 0) {
+ extra -= missing;
+ win_g.w += missing;
+ } else {
+ win_g.w -= remain;
+ extra += remain;
+ }
+ }
+
+ win_g.w = msize;
+ if (flip)
+ win_g.x += r_g.w - msize;
+ } else {
+ colno = winno;
+ split = 0;
+ }
+ hrh = r_g.h / colno;
+ extra = r_g.h - (colno * hrh);
+ win_g.h = hrh - 2;
+
+ /* stack all the tiled windows */
+ i = j = 0;
+ TAILQ_FOREACH(win, &ws->winlist, entry) {
+ if (win->transient != 0 || win->floating != 0)
+ continue;
+
+ if (split && i == split) {
+ colno = winno - split;
+ hrh = (r_g.h / colno);
+ extra = r_g.h - (colno * hrh);
+ if (flip)
+ win_g.x = r_g.x;
+ else
+ win_g.x += msize + 2;
+ win_g.w = r_g.w - (msize + 2);
+ j = 0;
+ }
+ win_g.h = hrh - 2;
+ 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;
+
+ bzero(&wc, sizeof wc);
+ wc.border_width = 1;
+ if (rot) {
+ 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 {
+ 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;
+ }
+ mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
+ XConfigureWindow(display, win->id, mask, &wc);
+ XMapRaised(display, win->id);
+ /*
+ fprintf(stderr, "vertical_stack: win %d x %d y %d w %d h %d bw %d\n", win->id, win->g.x, win->g.y, win->g.w , win->g.h, wc.border_width);
+ */
+
+ last_h = win_g.h;
+ i++;
+ j++;
+ }
+
+ /* now, stack all the floaters and transients */
+ TAILQ_FOREACH(win, &ws->winlist, entry) {
+ if (win->transient == 0 && win->floating == 0)
+ continue;
+
+ stack_floater(win, ws->r);
+ XMapRaised(display, win->id);
+ }
+
+ if (winfocus)
+ focus_win(winfocus); /* has to be done outside of the loop */
+}
+
+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;
+ 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;
+ 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)