+ }
+}
+
+/* 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, const char *name)
+{
+ tag_t *tag = new0(tag_t);
+ strncpy(tag->name, name, sizeof(tag->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;
+ for (list_t *dpy = tag->dpys; dpy; dpy = dpy->next)
+ if (DPY(dpy)->geom->z > tag->dpy->geom->z)
+ tag->dpy = dpy->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(const char *name)
+{
+ tag_t *tag = NULL;
+ for (list_t *cur = wm->tags; cur; cur = cur->next)
+ if (!strcmp(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, const char *name)
+{
+ printf("tag_set: %p %s\n", win, name);
+ if (!strcmp(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(const char *name)
+{
+ printf("tag_switch: %s\n", name);
+ tag_t *old = wm_tag;
+ if ((wm_col == NULL || wm_row == NULL) && wm_flt == NULL) {
+ while (old->dpys) {
+ dpy_t *dpy = old->dpys->data;
+ while (dpy->cols)
+ dpy->cols = list_remove(dpy->cols, dpy->cols, 1);
+ old->dpys = list_remove(old->dpys, old->dpys, 1);
+ }
+ list_t *ltag = list_find(wm->tags, old);
+ wm->tags = list_remove(wm->tags, ltag, 1);
+ }
+ 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 */
+ win_t *focus = get_focus();
+ 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;
+ if (ROW(lrow)->state != ST_SHOW) {
+ sys_show(win, ROW(lrow)->state);
+ continue;
+ }
+ win->h = ROW(lrow)->height;
+ state_t state = ST_SHOW;
+ int height = 0;
+ switch (col->layout) {
+ 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);
+ sys_show(next, ST_SHOW);
+ }
+ int isfocus = win == col->row->win;
+ state = isfocus ? ST_SHOW : ST_SHADE;
+ sys_show(win, state);
+ sys_move(win, x+margin, y+margin, col->width, sy);
+ y += (isfocus ? sy : stack) + (margin/2);
+ break;
+ case FULL:
+ case TAB:
+ sys_move(win, x+margin, 0+margin,
+ col->width, dpy->geom->h-2*margin);
+ break;
+ }
+ sys_show(win, state);
+ if (focus == win)
+ sys_raise(win);
+ ROW(lrow)->height = win->h;
+ }
+ x += col->width + margin;
+ }
+}
+
+/* Refresh the window layout */
+void wm_update(void)
+{
+ /* Updates window sizes */
+ 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);
+ sys_show(flt->win, flt->state);
+ }
+
+ /* Hide other tags */
+ 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);
+ }
+
+ /* Set focused window */
+ if (wm_focus)
+ set_focus(wm_focus);
+}
+
+/*******************************
+ * Window management functions *
+ *******************************/
+int wm_handle_event(win_t *win, event_t ev, mod_t mod, ptr_t ptr)
+{
+ if (!win || win == wm_dpy->geom)
+ return 0;
+ //printf("wm_handle_event: %p - %x %c%c%c%c%c\n", win, ev,
+ // mod.up ? '^' : 'v',
+ // mod.alt ? 'a' : '-',
+ // mod.ctrl ? 'c' : '-',
+ // mod.shift ? 's' : '-',
+ // mod.win ? 'w' : '-');
+
+ /* Mouse events */
+ if (EV_MOUSE0 <= ev && ev <= EV_MOUSE7) {
+ if (ev == EV_MOUSE1 && !mod.MODKEY && !mod.up)
+ return raise_float(win), 0;
+ if ((ev == EV_MOUSE3 && mod.MODKEY && !mod.up) ||
+ (ev == EV_MOUSE1 && mod.MODKEY && !mod.up && mod.shift))
+ return set_move(win,ptr,RESIZE), 1;
+ if (ev == EV_MOUSE1 && mod.MODKEY && !mod.up)
+ return set_move(win,ptr,MOVE), 1;
+ if (move_mode != NONE && mod.up)
+ return set_move(win,ptr,NONE), 1;
+ if (ev == EV_MOUSE1 && !mod.up && win->state == ST_SHADE)
+ return set_focus(win), wm_update(), 0;
+ return 0;
+ }