1 #include "kernel/focus.h"
2 #include "kernel/frame.h"
3 #include "kernel/dispatch.h"
4 #include "kernel/openbox.h"
5 #include "kernel/event.h"
6 #include "kernel/grab.h"
7 #include "kernel/action.h"
8 #include "kernel/prop.h"
9 #include "kernel/timer.h"
10 #include "parser/parse.h"
13 #include "translate.h"
19 <action name="ChangeDesktop">
26 static void parse_key(xmlDocPtr doc, xmlNodePtr node, GList *keylist)
33 n = parse_find_node("keybind", node);
35 if (parse_attr_string("key", n, &key)) {
36 keylist = g_list_append(keylist, key);
38 parse_key(doc, n->xmlChildrenNode, keylist);
40 it = g_list_last(keylist);
42 keylist = g_list_delete_link(keylist, it);
44 n = parse_find_node("keybind", n->next);
47 nact = parse_find_node("action", node);
49 if ((action = action_parse(doc, nact))) {
50 /* validate that its okay for a key binding */
51 if (action->func == action_moveresize &&
52 action->data.moveresize.corner !=
53 prop_atoms.net_wm_moveresize_move_keyboard &&
54 action->data.moveresize.corner !=
55 prop_atoms.net_wm_moveresize_size_keyboard) {
61 kbind(keylist, action);
63 nact = parse_find_node("action", nact->next);
68 static void parse_xml(xmlDocPtr doc, xmlNodePtr node, void *d)
70 parse_key(doc, node, NULL);
73 void plugin_setup_config()
75 parse_register("keyboard", parse_xml, NULL);
78 KeyBindingTree *firstnode = NULL;
80 static KeyBindingTree *curpos;
81 static guint reset_key, reset_state, button_return, button_escape;
82 static Timer *chain_timer;
84 static void grab_for_window(Window win, gboolean grab)
91 p = curpos ? curpos->first_child : firstnode;
93 grab_key(p->key, p->state, win, GrabModeAsync);
97 grab_key(reset_key, reset_state, win, GrabModeAsync);
101 static void grab_keys(gboolean grab)
105 grab_for_window(focus_backup, grab);
106 for (it = client_list; it; it = g_list_next(it))
107 grab_for_window(((Client*)it->data)->frame->window, grab);
110 static void reset_chains()
113 timer_stop(chain_timer);
122 static void chain_timeout(void *data)
127 gboolean kbind(GList *keylist, Action *action)
129 KeyBindingTree *tree, *t;
132 g_assert(keylist != NULL);
133 g_assert(action != NULL);
135 if (!(tree = tree_build(keylist)))
138 if ((t = tree_find(tree, &conflict)) != NULL) {
139 /* already bound to something, use the existing tree */
144 while (t->first_child) t = t->first_child;
147 g_message("conflict with binding");
153 t->actions = g_slist_append(t->actions, action);
154 /* assimilate this built tree into the main tree. assimilation
155 destroys/uses the tree */
156 if (tree) tree_assimilate(tree);
161 static void event(ObEvent *e, void *foo)
163 static KeyBindingTree *grabbed_key = NULL;
165 if (e->type == Event_Client_Mapped) {
166 grab_for_window(e->data.c.client->window, TRUE);
168 } else if (e->type == Event_Client_Destroy) {
169 grab_for_window(e->data.c.client->window, FALSE);
174 gboolean done = FALSE;
176 if ((e->type == Event_X_KeyRelease &&
177 !(grabbed_key->state & e->data.x.e->xkey.state)))
179 else if (e->type == Event_X_KeyPress) {
180 if (e->data.x.e->xkey.keycode == button_return)
182 else if (e->data.x.e->xkey.keycode == button_escape) {
184 for (it = grabbed_key->actions; it; it = it->next) {
185 Action *act = it->data;
186 act->data.cycle.cancel = TRUE;
193 for (it = grabbed_key->actions; it; it = it->next) {
194 Action *act = it->data;
195 act->data.cycle.final = TRUE;
196 act->func(&act->data);
199 grab_keyboard(FALSE);
204 if (e->type == Event_X_KeyRelease)
207 g_assert(e->type == Event_X_KeyPress);
209 if (e->data.x.e->xkey.keycode == reset_key &&
210 e->data.x.e->xkey.state == reset_state) {
217 p = curpos->first_child;
219 if (p->key == e->data.x.e->xkey.keycode &&
220 p->state == e->data.x.e->xkey.state) {
221 if (p->first_child != NULL) { /* part of a chain */
222 if (chain_timer) timer_stop(chain_timer);
223 /* 5 second timeout for chains */
224 chain_timer = timer_start(5000*1000, chain_timeout,
230 for (it = p->actions; it; it = it->next) {
231 Action *act = it->data;
232 if (act->func != NULL) {
233 act->data.any.c = focus_client;
235 if (act->func == action_cycle_windows) {
236 act->data.cycle.final = FALSE;
237 act->data.cycle.cancel = FALSE;
240 if (act->func == action_cycle_windows &&
241 !grabbed_key && grab_keyboard(TRUE)) {
245 act->data.any.c = focus_client;
246 act->func(&act->data);
259 void plugin_startup()
266 dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
267 Event_X_KeyPress | Event_X_KeyRelease,
268 (EventHandler)event, NULL);
270 translate_key("C-g", &reset_state, &reset_key);
271 translate_key("Escape", &i, &button_escape);
272 translate_key("Return", &i, &button_return);
277 void plugin_shutdown()
279 dispatch_register(0, (EventHandler)event, NULL);
281 tree_destroy(firstnode);