]> Pileus Git - wmpus/blob - wm-mono.c
Add monocule window manager
[wmpus] / wm-mono.c
1 /*
2  * Copyright (c) 2013 Andy Spencer <andy753421@gmail.com>
3  *
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.
7  *
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
14  */
15
16 /* Brain-dead monocule window manager */
17
18 #include <stdio.h>
19
20 #include "util.h"
21 #include "sys.h"
22 #include "wm.h"
23
24 /* Configuration */
25 #ifndef MODKEY
26 #define MODKEY alt
27 #endif
28
29 /* Data */
30 list_t *wins;
31 list_t *focus;
32 list_t *screens;
33
34 /* Helper functions */
35 void wm_show(list_t *node)
36 {
37         focus = node;
38         if (!node) return;
39         sys_show(node->data, ST_MAX);
40         sys_raise(node->data);
41         sys_focus(node->data);
42 }
43
44 /* Window management functions */
45 int wm_handle_event(win_t *win, event_t ev, mod_t mod, ptr_t ptr)
46 {
47         list_t *node = list_find(wins, win);
48
49         if (node && mod.MODKEY && ev == 'j')
50                 return wm_show(node->next), 1;
51         if (node && mod.MODKEY && ev == 'k')
52                 return wm_show(node->prev), 1;
53
54         if (mod.MODKEY && mod.shift && ev == 'c')
55                 return sys_show(win, ST_CLOSE), 1;
56         if (mod.MODKEY && mod.shift && ev == 'q')
57                 return sys_exit(), 1;
58
59         return 0;
60 }
61
62 int wm_handle_ptr(win_t *cwin, ptr_t ptr)
63 {
64         return 0;
65 }
66
67 int wm_handle_state(win_t *win, state_t prev, state_t next)
68 {
69         return 0;
70 }
71
72 void wm_insert(win_t *win)
73 {
74         if (win->type == TYPE_NORMAL) {
75                 wins = list_append(wins, win);
76                 wm_show(list_last(wins));
77         }
78         if (win->type == TYPE_TOOLBAR) {
79                 wm_show(focus);
80         }
81 }
82
83 void wm_remove(win_t *win)
84 {
85         list_t *node = list_find(wins, win);
86         if (node == focus)
87                 wm_show(node->prev ?: node->next);
88         wins = list_remove(wins, node, 0);
89 }
90
91 void wm_init(win_t *root)
92 {
93         screens = sys_info(root);
94         sys_watch(root, 'j', MOD(.MODKEY=1));
95         sys_watch(root, 'k', MOD(.MODKEY=1));
96         sys_watch(root, 'c', MOD(.MODKEY=1,.shift=1));
97         sys_watch(root, 'q', MOD(.MODKEY=1,.shift=1));
98 }
99
100 void wm_free(win_t *root)
101 {
102         while (wins)
103                 wins = list_remove(wins, wins, 0);
104 }