+ printf("shift_window: %p - %+d,%+d\n", win, col, row);
+ print_txt();
+ printf("shift_window: >>>\n");
+ list_t *ldpy, *lcol, *lrow, *lflt;
+ if (tiling != searchl(wm_tag, win, &ldpy, &lcol, &lrow, &lflt))
+ return;
+ dpy_t *dpy = ldpy->data;
+ if (row != 0) {
+ /* Move with a column, just swap rows */
+ list_t *src = lrow, *dst = NULL;
+ if (row < 0) dst = src->prev;
+ if (row > 0) dst = src->next;
+ if (src && dst) {
+ printf("swap: %p <-> %p\n", src->data, dst->data);
+ row_t *tmp = src->data;
+ src->data = dst->data;
+ dst->data = tmp;
+ goto update;
+ }
+ } else {
+ /* Moving between columns */
+ int onlyrow = !lrow->prev && !lrow->next;
+ list_t *src = lcol, *dst = NULL;
+ if (col < 0) {
+ if (src->prev) {
+ /* Normal move between columns */
+ dst = src->prev;
+ } else if (!onlyrow) {
+ /* Create new column */
+ dpy->cols = list_insert(dpy->cols, new0(col_t));
+ dst = src->prev;
+ } else if (ldpy->prev) {
+ /* Move to next monitor */
+ dpy = ldpy->prev->data;
+ dst = list_last(dpy->cols);
+ } else {
+ /* We, shall, not, be,
+ * we shall not be moved */
+ return;
+ }
+ }
+ if (col > 0) {
+ if (src->next) {
+ dst = src->next;
+ } else if (!onlyrow) {
+ dpy->cols = list_append(dpy->cols, new0(col_t));
+ dst = src->next;
+ } else if (ldpy->next) {
+ dpy = ldpy->next->data;
+ dst = dpy->cols;
+ } else {
+ return;
+ }
+ }
+ cut_win(win, wm_tag);
+ put_win_col(win, wm_tag, dpy, dst ? dst->data : NULL);
+ goto update;
+ }
+update:
+ print_txt();
+ wm_update();
+}
+
+/* Get next/prev item, with wraparound */
+static list_t *get_next(list_t *list, int forward)
+{
+ list_t *next = forward ? list->next : list->prev;
+ if (next == NULL) {
+ next = list;
+ while ((list = forward ? next->prev : next->next))
+ next = list;
+ }
+ return next;
+}
+
+/* Move keyboard focus in a given direction */
+static void shift_focus(int cols, int rows)
+{
+ printf("shift_focus: %+d,%+d\n", cols, rows);
+ if (rows != 0 && wm_focus) {
+ /* Move focus up/down */
+ list_t *dpy, *col, *row;
+ if (tiling != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
+ return;
+ row_t *next = get_next(row, rows > 0)->data;
+ set_focus(next->win);
+ if (COL(col)->mode != split)
+ wm_update();
+ }
+ if (cols != 0) {
+ /* Move focus left/right */
+ list_t *dpy, *col, *row, *ndpy, *ncol = NULL;
+ if (wm_focus) {
+ /* Currently focused on a window */
+ if (tiling != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
+ return;
+ ncol = cols > 0 ? col->next : col->prev;
+ } else {
+ /* Currently focused on an empty display */
+ dpy = list_find(wm_tag->dpys, wm_dpy);
+ }
+ if (ncol == NULL) {
+ /* Moving focus to a different display */
+ ndpy = get_next(dpy, cols > 0);
+ ncol = cols > 0 ? DPY(ndpy)->cols :
+ list_last(DPY(ndpy)->cols);
+ wm_dpy = ndpy->data;
+ }
+ if (ncol && COL(ncol) && COL(ncol)->row)
+ set_focus(COL(ncol)->row->win);
+ else
+ sys_focus(wm->root);
+ }
+}
+
+/* Raise the window in the floating */
+static void raise_float(win_t *win)
+{
+ printf("raise_float: %p\n", win);
+ list_t *cur;
+ for (cur = wm_dpy->flts; cur; cur = cur->next)
+ if (FLT(cur)->win == win)
+ break;
+ if (cur) {
+ flt_t *flt = cur->data;
+ wm_dpy->flts = list_remove(wm_dpy->flts, cur);
+ wm_dpy->flts = list_append(wm_dpy->flts, flt);
+ }
+ sys_raise(win);
+}
+
+/* Toggle between floating and tiling layers */
+static void switch_layer(void)
+{
+ printf("switch_float: %p %d\n",
+ wm_dpy, wm_dpy->layer);
+ wm_dpy->layer = !wm_dpy->layer;
+ wm_update();
+}
+
+/* Move current window between floating and tiling layers */
+static void set_layer(win_t *win)
+{
+ if (!win) return;
+ printf("set_float: %p %p\n", wm_dpy, win);
+ wm_dpy->layer = !cut_win(win, wm_tag);
+ put_win(win, wm_tag, wm_dpy->layer);
+ wm_update();
+}
+
+/* Allocate a new tag */
+static tag_t *tag_new(list_t *screens, int name)
+{
+ tag_t *tag = new0(tag_t);
+ tag->name = name;
+ for (list_t *cur = screens; cur; cur = cur->next) {
+ dpy_t *dpy = new0(dpy_t);
+ dpy->geom = cur->data;
+ tag->dpys = list_append(tag->dpys, dpy);
+ }
+ tag->dpy = tag->dpys->data;
+ return tag;
+}
+
+/* Search for a tag
+ * If it does not exist it is based on the
+ * display geometry in wm->screens */
+static tag_t *tag_find(int name)
+{
+ tag_t *tag = NULL;
+ for (list_t *cur = wm->tags; cur; cur = cur->next)
+ if (name == TAG(cur)->name) {
+ tag = cur->data;
+ break;
+ }
+ if (!tag) {
+ tag = tag_new(wm->screens, name);
+ wm->tags = list_append(wm->tags, tag);
+ }
+ return tag;
+}
+
+/* Move the window from the current tag to the new tag
+ * Unlike wmii, only remove the current tag, not all tags */
+static void tag_set(win_t *win, int name)
+{
+ printf("tag_set: %p %d\n", win, name);
+ if (wm_tag->name == name)
+ return;
+ tag_t *tag = tag_find(name);
+ layer_t layer = cut_win(win, wm_tag);
+ put_win(win, tag, layer);
+ set_focus(wm_focus);
+}
+
+/* Switch to a different tag */
+static void tag_switch(int name)
+{
+ printf("tag_switch: %d\n", name);
+ if ((wm_col == NULL || wm_row == NULL) && wm_flt == NULL)
+ wm->tags = list_remove(wm->tags,
+ list_find(wm->tags, wm_tag));
+ wm_tag = tag_find(name);
+}
+
+/* Tile all windows in the given display
+ * This performs all the actual window tiling
+ * Currently supports split, stack and maximized modes */
+static void wm_update_cols(dpy_t *dpy)
+{
+ int x=0, y=0; // Current window top-left position
+ int tx=0, ty=0; // Total size (sum of initial col widths and row heights w/o margin)
+ int mx=0, my=0; // Maximum usable size (screen size minus margins)
+ int sy=0; // Stack size (height of focused stack window)
+
+ /* Scale horizontally */
+ x = dpy->geom->x;
+ mx = dpy->geom->w - (list_length(dpy->cols)+1)*MARGIN;
+ for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
+ tx += COL(lcol)->width;
+ for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
+ COL(lcol)->width *= (float)mx / tx;
+
+ /* Scale each column vertically */
+ for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
+ col_t *col = lcol->data;
+ int nrows = list_length(col->rows);
+ ty = 0;
+ for (list_t *lrow = col->rows; lrow; lrow = lrow->next)
+ ty += ROW(lrow)->height;
+ y = dpy->geom->y;
+ my = dpy->geom->h - (MARGIN + (nrows-1)* MARGIN + MARGIN);
+ sy = dpy->geom->h - (MARGIN + (nrows-1)*(MARGIN/2) + MARGIN)
+ - (nrows-1)* STACK;
+ for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
+ win_t *win = ROW(lrow)->win;
+ win->h = ROW(lrow)->height;
+ int height = 0;
+ switch (col->mode) {
+ case split:
+ sys_move(win, x+MARGIN, y+MARGIN,
+ col->width, win->h * ((float)my / ty));
+ height = win->h;
+ y += height + MARGIN;
+ break;
+ case stack:
+ if (lrow->next && ROW(lrow->next)->win == col->row->win) {
+ /* Hack to prevent flashing */
+ win_t *next = ROW(lrow->next)->win;
+ sys_move(next, x+MARGIN, y+MARGIN+STACK+MARGIN/2,
+ col->width, sy);
+ }
+ height = win == col->row->win ? sy : STACK;
+ sys_move(win, x+MARGIN, y+MARGIN,
+ col->width, height);
+ y += height + (MARGIN/2);
+ break;
+ case max:
+ case tab:
+ sys_move(win, x+MARGIN, 0+MARGIN,
+ col->width, dpy->geom->h-2*MARGIN);
+ if (col->row->win == win)
+ sys_raise(win);
+ break;
+ }
+ ROW(lrow)->height = win->h;
+ }
+ x += col->width + MARGIN;
+ }
+}
+
+/*******************************
+ * Window management functions *
+ *******************************/
+void wm_update(void)
+{
+ /* Show/hide tags */
+ tag_foreach_col(wm_tag, dpy, col, row, win)
+ sys_show(win, st_show);
+ tag_foreach_flt(wm_tag, dpy, flt, win)
+ sys_show(win, st_show);
+ for (list_t *tag = wm ->tags; tag; tag = tag->next)
+ if (tag->data != wm_tag) {
+ tag_foreach_col(TAG(tag), dpy, col, row, win)
+ sys_show(win, st_hide);
+ tag_foreach_flt(TAG(tag), dpy, flt, win)
+ sys_show(win, st_hide);
+ }
+
+ /* Refresh the display */
+ for (list_t *ldpy = wm_tag->dpys; ldpy; ldpy = ldpy->next)
+ wm_update_cols(ldpy->data);
+ tag_foreach_flt(wm_tag, ldpy, lflt, win) {
+ flt_t *flt = lflt->data;
+ sys_move(win, flt->x, flt->y, flt->w, flt->h);
+ sys_raise(flt->win);
+ }
+ if (wm_focus)
+ set_focus(wm_focus);
+}
+
+int wm_handle_key(win_t *win, Key_t key, mod_t mod, ptr_t ptr)
+{
+ if (!win || win == wm_dpy->geom) return 0;
+ //printf("wm_handle_key: %p - %x %c%c%c%c%c\n", win, key,
+ // mod.up ? '^' : 'v',
+ // mod.alt ? 'a' : '-',
+ // mod.ctrl ? 'c' : '-',
+ // mod.shift ? 's' : '-',
+ // mod.win ? 'w' : '-');
+
+ /* Mouse movement */
+ if (key == key_mouse1)
+ raise_float(win);
+ if (key_mouse0 <= key && key <= key_mouse7) {
+ if (key == key_mouse1 && mod.MODKEY && !mod.up)
+ return set_move(win,ptr,move), 1;
+ if (key == key_mouse3 && mod.MODKEY && !mod.up)
+ return set_move(win,ptr,resize), 1;
+ if (move_mode != none && mod.up)
+ return set_move(win,ptr,none), 1;
+ if (key == key_mouse1 && !mod.up && win->h == STACK)
+ return wm_update(), 0;
+ return 0;
+ }
+
+ /* Only handle key-down */
+ if (mod.up)
+ return 0;
+
+ /* Misc */
+ if (mod.MODKEY) {
+#ifdef DEBUG
+ if (key == key_f1) return raise_float(win), 1;
+ if (key == key_f2) return set_focus(win), 1;
+ if (key == key_f3) return sys_show(win, st_show), 1;
+ if (key == key_f4) return sys_show(win, st_hide), 1;
+#endif
+ if (key == key_f5) return wm_update(), 1;
+ if (key == key_f6) return print_txt(), 1;
+ }
+
+ /* Floating layer */
+ if (key == ' ') {
+ if (mod.MODKEY && mod.shift)
+ return set_layer(win), 1;
+ if (mod.MODKEY)
+ return switch_layer(), 1;
+ }
+
+ /* Movement commands */
+ if (mod.MODKEY && mod.shift) {
+ switch (key) {
+ case 'h': return shift_window(wm_focus,-1, 0), 1;
+ case 'j': return shift_window(wm_focus, 0,+1), 1;
+ case 'k': return shift_window(wm_focus, 0,-1), 1;
+ case 'l': return shift_window(wm_focus,+1, 0), 1;
+ default: break;
+ }
+ }
+ else if (mod.MODKEY) {
+ switch (key) {
+ case 'h': return shift_focus(-1, 0), 1;
+ case 'j': return shift_focus( 0,+1), 1;
+ case 'k': return shift_focus( 0,-1), 1;
+ case 'l': return shift_focus(+1, 0), 1;
+ default: break;
+ }
+ }
+
+ /* Column mode commands */
+ if (mod.MODKEY) {
+ switch (key) {
+ case 'd': return set_mode(win, split), 1;
+ case 's': return set_mode(win, stack), 1;
+ case 'm': return set_mode(win, max), 1;
+ case 't': return set_mode(win, tab), 1;
+ default: break;
+ }
+ }
+
+ /* Tag switching */
+ if (mod.MODKEY && '0' <= key && key <= '9') {
+ int name = key - '0';
+ if (mod.shift)
+ tag_set(win, name);
+ else
+ tag_switch(name);
+ wm_update();
+ }
+
+ /* Focus change */
+ if (key == key_enter)
+ return set_focus(win), 1;
+
+ if (key_mouse0 <= key && key <= key_mouse7)
+ return set_focus(win), 0;
+
+ /* Reset focus after after focus change,
+ * not sure what is causing the focus change in the first place
+ * but preventing that would be a better solution */
+ if (key == key_focus)
+ sys_focus(wm_focus ?: wm->root);