2 * Copyright (c) 2011-2012, Andy Spencer <andy753421@gmail.com>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
30 static int margin = 0;
31 static int stack = 25;
39 SPLIT, STACK, FULL, TAB
46 /* Window structure types */
50 win_t *win; // the window
51 int height; // win height in _this_ tag
52 state_t state; // state of window
56 list_t *rows; // of row_t
57 row_t *row; // focused row
58 int width; // column width
59 layout_t layout; // column layout
63 win_t *win; // the window
64 int x, y, w, h; // position of window (in this tag)
65 state_t state; // state of window
69 list_t *cols; // of col_t
70 col_t *col; // focused col
71 list_t *flts; // of flt_t
72 flt_t *flt; // focused flt
73 layer_t layer; // focused layer
74 win_t *geom; // display size and position
78 list_t *dpys; // of dpy_t
79 dpy_t *dpy; // focused dpy
80 char name[64]; // tag name
84 list_t *tags; // of tag_t
85 tag_t *tag; // focused tag
86 list_t *screens; // display geometry
89 #define WIN(node) ((win_t*)(node)->data)
90 #define ROW(node) ((row_t*)(node)->data)
91 #define COL(node) ((col_t*)(node)->data)
92 #define FLT(node) ((flt_t*)(node)->data)
93 #define DPY(node) ((dpy_t*)(node)->data)
94 #define TAG(node) ((tag_t*)(node)->data)
96 #define tag_foreach_col(tag, dpy, col, row, win) \
97 for (list_t *dpy = tag ->dpys; dpy; dpy = dpy->next) \
98 for (list_t *col = DPY(dpy)->cols; col; col = col->next) \
99 for (list_t *row = COL(col)->rows; row; row = row->next) \
100 for (win_t *win = ROW(row)->win; win; win = NULL)
102 #define tag_foreach_flt(tag, dpy, flt, win) \
103 for (list_t *dpy = tag ->dpys; dpy; dpy = dpy->next) \
104 for (list_t *flt = DPY(dpy)->flts; flt; flt = flt->next) \
105 for (win_t *win = FLT(flt)->win; win; win = NULL)
107 /* Window management data
108 * wm_* macros represent the currently focused item
109 * _only_ wm_focus protects against NULL pointers */
111 #define wm_tag wm->tag
112 #define wm_dpy wm->tag->dpy
113 #define wm_col wm->tag->dpy->col
114 #define wm_row wm->tag->dpy->col->row
115 #define wm_flt wm->tag->dpy->flt
116 #define wm_focus get_focus()
118 /* Mouse drag data */
119 static drag_t move_mode;
120 static list_t *move_lrow;
121 static list_t *move_lcol;
122 static list_t *move_lflt;
123 static ptr_t move_prev;
124 static layer_t move_layer;
125 static struct { int v, h; } move_dir;
128 void wm_update(void);
130 /********************
132 ********************/
133 static int sort_win(void *a, void *b)
135 return ((win_t*)a)->x > ((win_t*)b)->x ? 1 :
136 ((win_t*)a)->x < ((win_t*)b)->x ? -1 : 0;
139 static win_t *get_focus(void)
141 if (!wm_tag || !wm_dpy)
143 switch (wm_dpy->layer) {
145 return wm_col && wm_row ? wm_row->win : NULL;
147 return wm_flt ? wm_flt->win : NULL;
152 /* Search for the target window in a given tag
153 * win may exist in other tags as well */
154 static int search(tag_t *tag, win_t *target)
156 tag_foreach_col(tag, dpy, col, row, win)
159 tag_foreach_flt(tag, dpy, flt, win)
165 static int searchl(tag_t *tag, win_t *target,
166 list_t **_dpy, list_t **_col, list_t **_row, list_t **_flt)
168 tag_foreach_col(tag, dpy, col, row, win) {
170 if (_dpy) *_dpy = dpy;
171 if (_col) *_col = col;
172 if (_row) *_row = row;
176 tag_foreach_flt(tag, dpy, flt, win) {
178 if (_dpy) *_dpy = dpy;
179 if (_flt) *_flt = flt;
186 static int searchw(tag_t *tag, win_t *target,
187 dpy_t **_dpy, col_t **_col, row_t **_row, flt_t **_flt)
189 list_t *dpy, *col, *row, *flt;
190 switch (searchl(tag, target, &dpy, &col, &row, &flt)) {
192 if (_dpy) *_dpy = DPY(dpy);
193 if (_col) *_col = COL(col);
194 if (_row) *_row = ROW(row);
197 if (_dpy) *_dpy = DPY(dpy);
198 if (_flt) *_flt = FLT(flt);
204 /* Set the layout for the windows column in the current tag */
205 static void set_mode(win_t *win, layout_t layout)
208 if (TILING != searchw(wm_tag, win, NULL, &col, NULL, NULL))
210 printf("set_mode: %p, %d -> %d\n",
211 col, col->layout, layout);
212 col->layout = layout;
213 if (col->layout == SPLIT)
214 for (list_t *cur = col->rows; cur; cur = cur->next) {
215 row_t *row = cur->data;
216 row->height = wm_dpy->geom->h;
221 /* Focus the window in the current tag and record
222 * it as the currently focused window */
223 static void set_focus(win_t *win)
230 dpy_t *dpy; col_t *col; row_t *row; flt_t *flt;
231 switch (searchw(wm_tag, win, &dpy, &col, &row, &flt)) {
241 dpy->layer = FLOATING;
247 /* Save mouse start location when moving/resizing windows */
248 static void set_move(win_t *win, ptr_t ptr, drag_t drag)
250 printf("set_move: %d - %p@%d,%d\n",
251 drag, win, ptr.rx, ptr.ry);
253 if (drag == MOVE || drag == RESIZE) {
254 move_layer = searchl(wm_tag, win, NULL,
255 &move_lcol, &move_lrow, &move_lflt);
259 int midy = win->y + (win->h/2);
260 int midx = win->x + (win->w/2);
261 move_dir.v = ptr.ry < midy ? -1 : +1;
262 move_dir.h = ptr.rx < midx ? -1 : +1;
266 /* Print a text representation of the window layout
267 * Quite useful for debugging */
268 static void print_txt(void)
270 for (list_t *ltag = wm->tags; ltag; ltag = ltag->next) {
271 tag_t *tag = ltag->data;
272 printf("tag: <%-9p [%p->%p] >%-9p d=%-9p - %s\n",
273 ltag->prev, ltag, ltag->data, ltag->next,
274 tag->dpy, tag->name);
275 for (list_t *ldpy = tag->dpys; ldpy; ldpy = ldpy->next) {
276 dpy_t *dpy = ldpy->data;
277 win_t *geom = dpy->geom;
278 printf(" dpy: <%-9p [%p->%p] >%-9p %c=%-9p - %d,%d %dx%d\n",
279 ldpy->prev, ldpy, ldpy->data, ldpy->next,
280 dpy->layer == TILING ? 'c' : 'f',
281 dpy->layer == TILING ? (void*)dpy->col : (void*)dpy->flt,
282 geom->x, geom->y, geom->h, geom->w);
283 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
284 col_t *col = lcol->data;
285 printf(" col: <%-9p [%p->%p] >%-9p r=%-9p - %dpx @ %d\n",
286 lcol->prev, lcol, lcol->data, lcol->next,
287 col->row, col->width, col->layout);
288 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
289 row_t *row = lrow->data;
290 win_t *win = row->win;
291 printf(" row: <%-9p [%p>>%p] >%-9p focus=%d%d - %4dpx \n",
292 lrow->prev, lrow, win, lrow->next,
293 col->row == row, wm_focus == win, win->h);
295 for (list_t *lflt = dpy->flts; lflt; lflt = lflt->next) {
296 flt_t *flt = lflt->data;
297 win_t *win = flt->win;
298 printf(" flt: <%-9p [%p>>%p] >%-9p focus=%d%d - %d,%d %dx%d \n",
299 lflt->prev, lflt, win, lflt->next,
300 dpy->flt == flt, wm_focus == flt->win,
301 flt->x, flt->y, flt->h, flt->w);
305 /* Cleanly remove a window from a tag
306 * Determines the new focused row/col
307 * Prunes empty lists */
308 static layer_t cut_win(win_t *win, tag_t *tag)
310 list_t *ldpy, *lcol, *lrow, *lflt;
311 layer_t layer = searchl(tag, win, &ldpy, &lcol, &lrow, &lflt);
313 if (layer == TILING) {
314 dpy_t *dpy = DPY(ldpy);
315 col_t *col = COL(lcol);
316 col->row = lrow->prev ? lrow->prev->data :
317 lrow->next ? lrow->next->data : NULL;
318 col->rows = list_remove(col->rows, lrow, 1);
319 if (col->rows == NULL && (lcol->next || lcol->prev)) {
320 dpy->col = lcol->prev ? lcol->prev->data :
321 lcol->next ? lcol->next->data : NULL;
322 dpy->cols = list_remove(dpy->cols, lcol, 1);
326 if (layer == FLOATING) {
327 dpy_t *dpy = DPY(ldpy);
328 dpy->flts = list_remove(dpy->flts, lflt, 1);
329 dpy->flt = dpy->flts ? list_last(dpy->flts)->data : NULL;
330 if (!dpy->flt && dpy->col && dpy->col->row)
337 /* Insert a window into the tiling layer
338 * The window is added immediately after the
339 * columns currently focused row */
340 static void put_win_col(win_t *win, tag_t *tag, dpy_t *dpy, col_t *col)
342 row_t *row = new0(row_t);
344 row->state = win->state;
348 dpy->cols = list_insert(dpy->cols, col);
351 int nrows = list_length(col->rows);
353 list_t *prev = list_find(col->rows, col->row);
354 list_insert_after(prev, row);
356 col->rows = list_insert(col->rows, row);
360 tag->dpy->col->row = row;
361 tag->dpy->layer = TILING;
363 row->height = dpy->geom->h / MAX(nrows,1);
365 int ncols = list_length(dpy->cols);
366 col->width = dpy->geom->w / MAX(ncols-1,1);
370 /* Insert a window into the floating layer */
371 static void put_win_flt(win_t *win, tag_t *tag, dpy_t *dpy)
373 flt_t *flt = new0(flt_t);
375 flt->w = dpy->geom->w / 2;
376 flt->h = dpy->geom->h / 2;
377 flt->x = dpy->geom->x + flt->w / 2;
378 flt->y = dpy->geom->y + flt->h / 2;
379 flt->state = win->state ?: ST_SHOW;
381 flt->x = dpy->flt->x + 20;
382 flt->y = dpy->flt->y + 20;
384 dpy->flts = list_append(dpy->flts, flt);
387 tag->dpy->layer = FLOATING;
390 /* Insert a window into a tag */
391 static void put_win(win_t *win, tag_t *tag, layer_t layer)
394 put_win_col(win, tag, tag->dpy, tag->dpy->col);
395 if (layer == FLOATING)
396 put_win_flt(win, tag, tag->dpy);
399 /* Move a window up, down, left, or right
400 * This handles moving with a column, between
401 * columns, and between multiple monitors. */
402 static void shift_window(win_t *win, int col, int row)
405 printf("shift_window: %p - %+d,%+d\n", win, col, row);
407 printf("shift_window: >>>\n");
408 list_t *ldpy, *lcol, *lrow, *lflt;
409 if (TILING != searchl(wm_tag, win, &ldpy, &lcol, &lrow, &lflt))
411 dpy_t *dpy = ldpy->data;
413 /* Move with a column, just swap rows */
414 list_t *src = lrow, *dst = NULL;
415 if (row < 0) dst = src->prev;
416 if (row > 0) dst = src->next;
418 printf("swap: %p <-> %p\n", src->data, dst->data);
419 row_t *tmp = src->data;
420 src->data = dst->data;
425 /* Moving between columns */
426 int onlyrow = !lrow->prev && !lrow->next;
427 list_t *src = lcol, *dst = NULL;
430 /* Normal move between columns */
432 } else if (!onlyrow) {
433 /* Create new column */
434 dpy->cols = list_insert(dpy->cols, new0(col_t));
436 } else if (ldpy->prev) {
437 /* Move to next monitor */
438 dpy = ldpy->prev->data;
439 dst = list_last(dpy->cols);
441 /* We, shall, not, be,
442 * we shall not be moved */
449 } else if (!onlyrow) {
450 dpy->cols = list_append(dpy->cols, new0(col_t));
452 } else if (ldpy->next) {
453 dpy = ldpy->next->data;
459 cut_win(win, wm_tag);
460 put_win_col(win, wm_tag, dpy, dst ? dst->data : NULL);
468 /* Get next/prev item, with wraparound */
469 static list_t *get_next(list_t *list, int forward)
471 list_t *next = forward ? list->next : list->prev;
474 while ((list = forward ? next->prev : next->next))
480 /* Move keyboard focus in a given direction */
481 static void shift_focus(int cols, int rows)
483 printf("shift_focus: %+d,%+d\n", cols, rows);
484 if (rows != 0 && wm_focus) {
485 /* Move focus up/down */
486 list_t *dpy, *col, *row;
487 if (TILING != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
489 row_t *next = get_next(row, rows > 0)->data;
490 set_focus(next->win);
491 if (COL(col)->layout != SPLIT)
495 /* Move focus left/right */
496 list_t *dpy, *col, *row, *ndpy, *ncol = NULL;
498 /* Currently focused on a window */
499 if (TILING != searchl(wm_tag, wm_focus, &dpy, &col, &row, NULL))
501 ncol = cols > 0 ? col->next : col->prev;
503 /* Currently focused on an empty display */
504 dpy = list_find(wm_tag->dpys, wm_dpy);
507 /* Moving focus to a different display */
508 ndpy = get_next(dpy, cols > 0);
509 ncol = cols > 0 ? DPY(ndpy)->cols :
510 list_last(DPY(ndpy)->cols);
513 if (ncol && COL(ncol) && COL(ncol)->row)
514 set_focus(COL(ncol)->row->win);
520 /* Raise the window in the floating */
521 static void raise_float(win_t *win)
523 printf("raise_float: %p\n", win);
525 for (cur = wm_dpy->flts; cur; cur = cur->next)
526 if (FLT(cur)->win == win)
529 flt_t *flt = cur->data;
530 wm_dpy->flts = list_remove(wm_dpy->flts, cur, 0);
531 wm_dpy->flts = list_append(wm_dpy->flts, flt);
536 /* Toggle between floating and tiling layers */
537 static void switch_layer(void)
539 printf("switch_float: %p %d\n",
540 wm_dpy, wm_dpy->layer);
541 wm_dpy->layer = !wm_dpy->layer;
545 /* Move current window between floating and tiling layers */
546 static void set_layer(win_t *win)
549 printf("set_float: %p %p\n", wm_dpy, win);
550 wm_dpy->layer = !cut_win(win, wm_tag);
551 put_win(win, wm_tag, wm_dpy->layer);
555 /* Allocate a new tag */
556 static tag_t *tag_new(list_t *screens, const char *name)
558 tag_t *tag = new0(tag_t);
559 strncpy(tag->name, name, sizeof(tag->name));
560 for (list_t *cur = screens; cur; cur = cur->next) {
561 dpy_t *dpy = new0(dpy_t);
562 dpy->geom = cur->data;
563 tag->dpys = list_append(tag->dpys, dpy);
565 tag->dpy = tag->dpys->data;
566 for (list_t *dpy = tag->dpys; dpy; dpy = dpy->next)
567 if (DPY(dpy)->geom->z > tag->dpy->geom->z)
568 tag->dpy = dpy->data;
573 * If it does not exist it is based on the
574 * display geometry in wm->screens */
575 static tag_t *tag_find(const char *name, int create)
578 for (list_t *cur = wm->tags; cur; cur = cur->next)
579 if (!strcmp(name, TAG(cur)->name)) {
583 if (!tag && create) {
584 tag = tag_new(wm->screens, name);
585 wm->tags = list_append(wm->tags, tag);
590 /* Compute the number of windows in a tag */
591 static int tag_size(tag_t *tag)
594 tag_foreach_col(tag, dpy, col, row, win)
596 tag_foreach_flt(tag, dpy, flt, win)
601 /* Remove a tag if it contains no windows */
602 static void tag_prune(tag_t *tag)
604 if (tag == wm_tag || tag_size(tag) >= 1)
607 dpy_t *dpy = tag->dpys->data;
609 dpy->cols = list_remove(dpy->cols, dpy->cols, 1);
610 tag->dpys = list_remove(tag->dpys, tag->dpys, 1);
612 list_t *ltag = list_find(wm->tags, tag);
613 wm->tags = list_remove(wm->tags, ltag, 1);
616 /* Move the window from the current tag to the new tag
617 * Unlike wmii, only remove the current tag, not all tags */
618 static void tag_set(win_t *win, const char *name, int toggle)
620 tag_t *tag = tag_find(name, 1);
622 int src_layer = search(wm_tag, win);
623 int dst_layer = search(tag, win);
626 if (toggle && dst_layer >= 0)
627 for (list_t *tag = wm->tags; tag; tag = tag->next)
628 if (search(TAG(tag), win) >= 0)
631 printf("tag_set: %p %s -- %s layer=%d:%d count=%d\n",
632 win, name, toggle ? "toggle" : "change",
633 src_layer, dst_layer, count);
637 put_win(win, tag, src_layer);
646 cut_win(win, wm_tag);
648 put_win(win, tag, src_layer);
654 /* Switch to a different tag */
655 static void tag_switch(const char *name)
657 printf("tag_switch: %s\n", name);
658 tag_t *tag = tag_find(name, 1);
666 /* Tile all windows in the given display
667 * This performs all the actual window tiling
668 * Currently supports split, stack and maximized modes */
669 static void wm_update_cols(dpy_t *dpy)
671 int x=0, y=0; // Current window top-left position
672 int tx=0, ty=0; // Total size (sum of initial col widths and row heights w/o margin)
673 int mx=0, my=0; // Maximum usable size (screen size minus margins)
674 int sy=0; // Stack size (height of focused stack window)
676 float rx=0, ry=0; // Residuals for floating point round off
678 /* Scale horizontally */
680 mx = dpy->geom->w - (list_length(dpy->cols)+1)*margin;
681 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
682 tx += COL(lcol)->width;
683 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next)
684 COL(lcol)->width = residual(COL(lcol)->width * (float)mx/tx, &rx);
686 /* Scale each column vertically */
687 win_t *focus = get_focus();
688 for (list_t *lcol = dpy->cols; lcol; lcol = lcol->next) {
689 col_t *col = lcol->data;
690 int nrows = list_length(col->rows);
692 for (list_t *lrow = col->rows; lrow; lrow = lrow->next)
693 ty += ROW(lrow)->height;
695 my = dpy->geom->h - (margin + (nrows-1)* margin + margin);
696 sy = dpy->geom->h - (margin + (nrows-1)*(margin/2) + margin)
698 for (list_t *lrow = col->rows; lrow; lrow = lrow->next) {
699 win_t *win = ROW(lrow)->win;
700 if (ROW(lrow)->state != ST_SHOW) {
701 sys_show(win, ROW(lrow)->state);
704 win->h = ROW(lrow)->height;
705 state_t state = ST_SHOW;
706 switch (col->layout) {
708 sys_move(win, x+margin, y+margin, col->width,
709 residual(win->h * ((float)my/ty), &ry));
710 y += win->h + margin;
713 if (lrow->next && ROW(lrow->next)->win == col->row->win) {
714 /* Hack to prevent flashing */
715 win_t *next = ROW(lrow->next)->win;
716 sys_move(next, x+margin, y+margin+stack+margin/2,
718 sys_show(next, ST_SHOW);
720 int isfocus = win == col->row->win;
721 state = isfocus ? ST_SHOW : ST_SHADE;
722 sys_show(win, state);
723 sys_move(win, x+margin, y+margin, col->width, sy);
724 y += (isfocus ? sy : stack) + (margin/2);
728 sys_move(win, x+margin, 0+margin,
729 col->width, dpy->geom->h-2*margin);
732 sys_show(win, state);
735 ROW(lrow)->height = win->h;
737 x += col->width + margin;
741 /* Refresh the window layout */
744 /* Updates window sizes */
745 for (list_t *ldpy = wm_tag->dpys; ldpy; ldpy = ldpy->next)
746 wm_update_cols(ldpy->data);
747 tag_foreach_flt(wm_tag, ldpy, lflt, win) {
748 flt_t *flt = lflt->data;
749 sys_move(win, flt->x, flt->y, flt->w, flt->h);
751 sys_show(flt->win, flt->state);
754 /* Hide other tags */
755 for (list_t *tag = wm ->tags; tag; tag = tag->next)
756 if (tag->data != wm_tag) {
757 tag_foreach_col(TAG(tag), dpy, col, row, win)
758 if (search(wm_tag, win) < 0)
759 sys_show(win, ST_HIDE);
760 tag_foreach_flt(TAG(tag), dpy, flt, win)
761 if (search(wm_tag, win) < 0)
762 sys_show(win, ST_HIDE);
765 /* Set focused window */
770 /*******************************
771 * Window management functions *
772 *******************************/
773 int wm_handle_event(win_t *win, event_t ev, mod_t mod, ptr_t ptr)
775 //printf("wm_handle_event: %p - %x %c%c%c%c%c\n", win, ev,
776 // mod.up ? '^' : 'v',
777 // mod.alt ? 'a' : '-',
778 // mod.ctrl ? 'c' : '-',
779 // mod.shift ? 's' : '-',
780 // mod.win ? 'w' : '-');
783 if (win && EV_MOUSE0 <= ev && ev <= EV_MOUSE7) {
784 if (ev == EV_MOUSE1 && !mod.MODKEY && !mod.up)
785 return raise_float(win), 0;
786 if ((ev == EV_MOUSE3 && mod.MODKEY && !mod.up) ||
787 (ev == EV_MOUSE1 && mod.MODKEY && !mod.up && mod.shift))
788 return set_move(win,ptr,RESIZE), 1;
789 if (ev == EV_MOUSE1 && mod.MODKEY && !mod.up)
790 return set_move(win,ptr,MOVE), 1;
791 if (move_mode != NONE && mod.up)
792 return set_move(win,ptr,NONE), 1;
793 if (ev == EV_MOUSE1 && !mod.up && win->state == ST_SHADE)
794 return set_focus(win), wm_update(), 0;
798 /* Only handle key-down */
800 return mod.MODKEY || ev == EV_ALT;
804 if (win && mod.MODKEY) {
805 if (ev == EV_F1) return raise_float(win), 1;
806 if (ev == EV_F2) return set_focus(win), 1;
807 if (ev == EV_F3) return sys_show(win, ST_SHOW), 1;
808 if (ev == EV_F4) return sys_show(win, ST_HIDE), 1;
809 if (ev == EV_F7) return sys_show(win, ST_SHADE), 1;
813 if (ev == EV_F5) return wm_update(), 1;
814 if (ev == EV_F6) return print_txt(), 1;
815 if (ev == 'q') return sys_exit(), 1;
817 if (win && mod.MODKEY) {
818 if (ev == 'f') return wm_handle_state(win, win->state,
819 win->state == ST_FULL ? ST_SHOW : ST_FULL);
820 if (ev == 'g') return wm_handle_state(win, win->state,
821 win->state == ST_MAX ? ST_SHOW : ST_MAX);
823 if (win && mod.MODKEY && mod.shift) {
824 if (ev == 'c') return sys_show(win, ST_CLOSE), 1;
829 if (win && mod.MODKEY && mod.shift)
830 return set_layer(win), 1;
832 return switch_layer(), 1;
835 /* Movement commands */
836 if (mod.MODKEY && mod.shift) {
838 case 'h': return shift_window(wm_focus,-1, 0), 1;
839 case 'j': return shift_window(wm_focus, 0,+1), 1;
840 case 'k': return shift_window(wm_focus, 0,-1), 1;
841 case 'l': return shift_window(wm_focus,+1, 0), 1;
845 else if (mod.MODKEY) {
847 case 'h': return shift_focus(-1, 0), 1;
848 case 'j': return shift_focus( 0,+1), 1;
849 case 'k': return shift_focus( 0,-1), 1;
850 case 'l': return shift_focus(+1, 0), 1;
855 /* Column layout commands */
858 case 'd': return set_mode(win, SPLIT), 1;
859 case 's': return set_mode(win, STACK), 1;
860 case 'm': return set_mode(win, FULL), 1;
861 case 't': return set_mode(win, TAB), 1;
867 if (mod.MODKEY && '0' <= ev && ev <= '9') {
868 char name[] = {ev, '\0'};
869 if (!mod.shift && !mod.ctrl) tag_switch(name);
870 if ( mod.shift && !mod.ctrl && win) tag_set(win, name, 0);
871 if (!mod.shift && mod.ctrl && win) tag_set(win, name, 1);
875 if (win && ev == EV_ENTER && win->state != ST_SHADE)
876 return set_focus(win), 1;
878 /* Reset focus after after focus change,
879 * not sure what is causing the focus change in the first place
880 * but preventing that would be a better solution */
887 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
889 //printf("wm_handle_ptr: %p - %d,%d %d,%d (%d) -- \n",
890 // cwin, ptr.x, ptr.y, ptr.rx, ptr.ry, move_mode);
892 if (move_mode == NONE)
895 int dx = ptr.rx - move_prev.rx;
896 int dy = ptr.ry - move_prev.ry;
899 if (move_layer == TILING && move_mode == RESIZE) {
900 list_t *vert = move_dir.v < 0 ? move_lrow->prev : move_lrow->next;
901 list_t *horz = move_dir.h < 0 ? move_lcol->prev : move_lcol->next;
903 ROW(move_lrow)->height += move_dir.v * dy;
904 ROW(vert)->height -= move_dir.v * dy;
907 COL(move_lcol)->width += move_dir.h * dx;
908 COL(horz)->width -= move_dir.h * dx;
913 if (move_layer == FLOATING) {
914 flt_t *flt = move_lflt->data;
915 win_t *win = flt->win;
916 if (move_mode == MOVE)
917 sys_move(win, win->x+dx, win->y+dy, win->w, win->h);
918 else if (move_mode == RESIZE)
920 win->x + dx * (move_dir.h < 0),
921 win->y + dy * (move_dir.v < 0),
922 win->w + dx * move_dir.h,
923 win->h + dy * move_dir.v);
924 flt->x = win->x; flt->y = win->y;
925 flt->w = win->w; flt->h = win->h;
931 int wm_handle_state(win_t *win, state_t prev, state_t next)
936 printf("wm_handle_state - %p %x -> %x\n", win, prev, next);
938 searchw(wm_tag, win, NULL, NULL, &row, &flt);
940 if (!row && !flt && next == ST_SHOW)
941 return wm_insert(win), 1;
942 if ((row || flt) && (next == ST_HIDE || next == ST_ICON))
943 return wm_remove(win), 1;
945 if (row) row->state = next;
946 if (flt) flt->state = next;
948 if (prev == ST_MAX || prev == ST_FULL ||
949 next == ST_MAX || next == ST_FULL)
955 void wm_insert(win_t *win)
957 printf("wm_insert: %p\n", win);
959 /* Make sure it's visible */
960 if (win->state == ST_HIDE)
963 /* Check for toolbars */
964 if (win->type == TYPE_TOOLBAR)
969 /* Initialize window */
970 sys_watch(win, EV_ENTER, MOD());
971 sys_watch(win, EV_FOCUS, MOD());
974 if (win->type == TYPE_DIALOG || win->parent)
975 wm_dpy->layer = FLOATING;
976 put_win(win, wm_tag, wm_dpy->layer);
984 void wm_remove(win_t *win)
986 printf("wm_remove: %p\n", win);
988 if (win->type == TYPE_TOOLBAR)
990 for (list_t *tag = wm->tags; tag; tag = tag->next)
991 cut_win(win, tag->data);
1001 /* Load configuration */
1002 margin = conf_get_int("main.margin", margin);
1003 stack = conf_get_int("main.stack", stack);
1006 wm->screens = list_sort(sys_info(), 0, sort_win);
1007 wm->tag = tag_new(wm->screens, "1");
1008 wm->tags = list_insert(NULL, wm->tag);
1010 event_t ev_e[] = {EV_ENTER, EV_FOCUS};
1011 event_t ev_c[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
1012 event_t ev_s[] = {'h', 'j', 'k', 'l', 'c', 'q', ' ',
1013 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
1014 EV_MOUSE1, EV_MOUSE3};
1015 event_t ev_m[] = {'h', 'j', 'k', 'l', 'd', 's', 'm', 't', 'f', 'g', ' ',
1016 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
1017 EV_F1, EV_F2, EV_F3, EV_F4, EV_F5, EV_F6,
1018 EV_F7, EV_F8, EV_F9, EV_F10, EV_F11, EV_F12,
1019 EV_MOUSE1, EV_MOUSE3};
1020 for (int i = 0; i < countof(ev_e); i++)
1021 sys_watch(NULL, ev_e[i], MOD());
1022 for (int i = 0; i < countof(ev_m); i++)
1023 sys_watch(NULL, ev_m[i], MOD(.MODKEY=1));
1024 for (int i = 0; i < countof(ev_c); i++)
1025 sys_watch(NULL, ev_c[i], MOD(.MODKEY=1,.ctrl=1));
1026 for (int i = 0; i < countof(ev_s); i++)
1027 sys_watch(NULL, ev_s[i], MOD(.MODKEY=1,.shift=1));
1032 /* Re-show and free all windows */
1033 while ( wm->tags) { tag_t *tag = wm->tags->data;
1034 while (tag->dpys) { dpy_t *dpy = tag->dpys->data;
1035 while (dpy->cols) { col_t *col = dpy->cols->data;
1036 while (col->rows) { row_t *row = col->rows->data;
1037 sys_show(row->win, ST_SHOW);
1038 col->rows = list_remove(col->rows, col->rows, 1); }
1039 dpy->cols = list_remove(dpy->cols, dpy->cols, 1); }
1040 while (dpy->flts) { flt_t *flt = dpy->flts->data;
1041 sys_show(flt->win, ST_SHOW);
1042 dpy->flts = list_remove(dpy->flts, dpy->flts, 1); }
1043 tag->dpys = list_remove(tag->dpys, tag->dpys, 1); }
1044 wm->tags = list_remove( wm->tags, wm->tags, 1); }
1046 /* Free remaining data */