+/* Window structure types */
+struct win_wm { };
+
+typedef struct {
+ win_t *win; // the window
+ int height; // win height in _this_ tag
+} row_t;
+
+typedef struct {
+ list_t *rows; // of row_t
+ row_t *row; // focused row
+ int width; // column width
+ mode_t mode; // display mode
+} col_t;
+
+typedef struct {
+ list_t *cols; // of col_t
+ col_t *col; // focused col
+ win_t *geom; // display size and position
+} dpy_t;
+
+typedef struct {
+ list_t *dpys; // of dpy_t
+ dpy_t *dpy; // focused dpy
+ int name; // tag name
+} tag_t;
+
+typedef struct {
+ list_t *tags; // of tag_t
+ tag_t *tag; // focused tag
+ win_t *root; // root/background window
+ list_t *screens; // display geometry
+} wm_t;
+
+#define WIN(node) ((win_t*)(node)->data)
+#define ROW(node) ((row_t*)(node)->data)
+#define COL(node) ((col_t*)(node)->data)
+#define DPY(node) ((dpy_t*)(node)->data)
+#define TAG(node) ((tag_t*)(node)->data)
+
+#define tag_foreach(tag, dpy, col, row, win) \
+ for (list_t *dpy = tag ->dpys; dpy; dpy = dpy->next) \
+ for (list_t *col = DPY(dpy)->cols; col; col = col->next) \
+ for (list_t *row = COL(col)->rows; row; row = row->next) \
+ for (win_t *win = ROW(row)->win; win; win = NULL) \
+
+/* Window management data
+ * wm_* macros represent the currently focused item
+ * _only_ wm_focus protects against NULL pointers */
+static wm_t *wm;
+#define wm_win wm->tag->dpy->col->row->win
+#define wm_row wm->tag->dpy->col->row
+#define wm_col wm->tag->dpy->col
+#define wm_dpy wm->tag->dpy
+#define wm_tag wm->tag
+#define wm_focus (wm_tag && wm_dpy && wm_col && wm_row ? wm_win : NULL)
+
+/* Mouse drag data */
+static drag_t move_mode;
+static list_t *move_lrow;
+static list_t *move_lcol;
+static ptr_t move_prev;
+static struct { int v, h; } move_dir;
+
+/********************
+ * Helper functions *
+ ********************/
+/* Search for the target window in a given tag
+ * win may exist in other tags as well */
+static int searchl(tag_t *tag, win_t *target,
+ list_t **_dpy, list_t **_col, list_t **_row)
+{
+ tag_foreach(tag, dpy, col, row, win) {
+ if (win == target) {
+ if (_dpy) *_dpy = dpy;
+ if (_col) *_col = col;
+ if (_row) *_row = row;
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static int search(tag_t *tag, win_t *target,
+ dpy_t **_dpy, col_t **_col, row_t **_row)
+{
+ list_t *dpy, *col, *row;
+ if (searchl(tag, target, &dpy, &col, &row)) {
+ if (_dpy) *_dpy = DPY(dpy);
+ if (_col) *_col = COL(col);
+ if (_row) *_row = ROW(row);
+ return 1;
+ }
+ return 0;
+}
+
+/* Set the mode for the windows column in the current tag */
+static void set_mode(win_t *win, mode_t mode)
+{
+ col_t *col;
+ if (!search(wm_tag, win, NULL, &col, NULL))
+ return;
+ printf("set_mode: %p, %d -> %d\n",
+ col, col->mode, mode);
+ col->mode = mode;
+ if (col->mode == split)
+ for (list_t *cur = col->rows; cur; cur = cur->next) {
+ row_t *row = cur->data;
+ row->height = wm_dpy->geom->h;
+ }
+ wm_update();
+}
+
+/* Focus the window in the current tag and record
+ * it as the currently focused window */
+static void set_focus(win_t *win)
+{
+ if (win == NULL || win == wm->root) {
+ sys_focus(wm->root);
+ return;
+ }
+
+ /* - Only grab mouse button on unfocused window,
+ * this prevents stealing all mouse clicks from client windows,
+ * - A better way may be to re-send mouse clicks to client windows
+ * using the return value from wm_handle_key */
+ for (int i = key_mouse1; i < key_mouse7; i++) {
+ if (wm_focus)
+ sys_watch(wm_focus, i, MOD());
+ sys_unwatch(win, i, MOD());
+ }
+
+ dpy_t *dpy; col_t *col; row_t *row;
+ if (search(wm_tag, win, &dpy, &col, &row)) {
+ wm_dpy = dpy;
+ wm_col = col;
+ wm_row = row;
+ }
+ sys_focus(win);
+}
+
+/* Save mouse start location when moving/resizing windows */
+static void set_move(win_t *win, ptr_t ptr, drag_t drag)
+{
+ printf("set_move: %d - %p@%d,%d\n",
+ drag, win, ptr.rx, ptr.ry);
+ move_mode = drag;
+ if (drag == move || drag == resize) {
+ searchl(wm_tag, win, NULL, &move_lcol, &move_lrow);
+ move_prev = ptr;
+ int midy = win->y + (win->h/2);
+ int midx = win->x + (win->w/2);
+ move_dir.v = ptr.ry < midy ? -1 : +1;
+ move_dir.h = ptr.rx < midx ? -1 : +1;
+ }
+}
+
+/* Print a text representation of the window layout
+ * Quite useful for debugging */
+static void print_txt(void)
+{
+ for (list_t *ltag = wm->tags; ltag; ltag = ltag->next) {
+ tag_t *tag = ltag->data;
+ printf("tag: <%-9p [%p->%p] >%-9p !%-9p - %d\n",
+ ltag->prev, ltag, ltag->data, ltag->next,
+ tag->dpy, tag->name);
+ for (list_t *ldpy = tag->dpys; ldpy; ldpy = ldpy->next) {
+ dpy_t *dpy = ldpy->data;
+ win_t *geom = dpy->geom;
+ printf(" dpy: <%-9p [%p->%p] >%-9p !%-9p - %d,%d %dx%d\n",
+ ldpy->prev, ldpy, ldpy->data, ldpy->next,
+ dpy->col, geom->x, geom->y, geom->h, geom->w);
+ for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
+ col_t *col = lcol->data;
+ printf(" col: <%-9p [%p->%p] >%-9p !%-9p - %dpx @ %d\n",
+ lcol->prev, lcol, lcol->data, lcol->next,
+ col->row, col->width, col->mode);
+ for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
+ row_t *row = lrow->data;
+ win_t *win = row->win;
+ printf(" win: <%-9p [%p>>%p] >%-9p !%-9p - %4dpx focus=%d%d\n",
+ lrow->prev, lrow, win, lrow->next,
+ win, win->h, col->row == row, wm_focus == win);
+ } } } }
+}
+
+/* Cleanly remove a window from a tag
+ * Determines the new focused row/col
+ * Prunes empty lists */
+static void cut_win(tag_t *tag, win_t *win)
+{
+ list_t *ldpy, *lcol, *lrow;
+ if (!searchl(tag, win, &ldpy, &lcol, &lrow))
+ return;
+ col_t *col = COL(lcol);
+ dpy_t *dpy = DPY(ldpy);
+
+ col->row = lrow->prev ? lrow->prev->data :
+ lrow->next ? lrow->next->data : NULL;
+ col->rows = list_remove(col->rows, lrow);
+
+ if (col->rows == NULL && (lcol->next || lcol->prev)) {
+ dpy->col = lcol->prev ? lcol->prev->data :
+ lcol->next ? lcol->next->data : NULL;
+ dpy->cols = list_remove(dpy->cols, lcol);
+ }
+}
+
+/* Insert a window at a given location
+ * The window is added immediately after the
+ * columns currently focused row */
+static void put_win(win_t *win, tag_t *tag, dpy_t *dpy, col_t *col)
+{
+ row_t *row = new0(row_t);
+ row->win = win;
+
+ if (col == NULL) {
+ col = new0(col_t);
+ dpy->cols = list_insert(dpy->cols, col);
+ }
+
+ int nrows = list_length(col->rows);
+ if (col->row) {
+ list_t *prev = list_find(col->rows, col->row);
+ list_insert_after(prev, row);
+ } else {
+ col->rows = list_insert(col->rows, row);
+ }
+ tag->dpy = dpy;
+ tag->dpy->col = col;
+ tag->dpy->col->row = row;
+
+ row->height = dpy->geom->h / MAX(nrows,1);
+ if (nrows == 0) {
+ int ncols = list_length(dpy->cols);
+ col->width = dpy->geom->w / MAX(ncols-1,1);
+ }
+}
+
+/* Move a window up, down, left, or right
+ * This handles moving with a column, between
+ * columns, and between multiple monitors. */
+static void shift_window(win_t *win, int col, int row)
+{
+ if (!win) return;
+ printf("shift_window: %p - %+d,%+d\n", win, col, row);
+ print_txt();
+ printf("shift_window: >>>\n");
+ list_t *ldpy, *lcol, *lrow;
+ if (!searchl(wm_tag, win, &ldpy, &lcol, &lrow))
+ 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(wm_tag, win);
+ put_win(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;
+}