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/client.h"
8 #include "kernel/action.h"
9 #include "kernel/prop.h"
10 #include "kernel/timer.h"
11 #include "parser/parse.h"
14 #include "translate.h"
20 <action name="ChangeDesktop">
27 static void parse_key(xmlDocPtr doc, xmlNodePtr node, GList *keylist)
34 n = parse_find_node("keybind", node);
36 if (parse_attr_string("key", n, &key)) {
37 keylist = g_list_append(keylist, key);
39 parse_key(doc, n->xmlChildrenNode, keylist);
41 it = g_list_last(keylist);
43 keylist = g_list_delete_link(keylist, it);
45 n = parse_find_node("keybind", n->next);
48 nact = parse_find_node("action", node);
50 if ((action = action_parse(doc, nact))) {
51 /* validate that its okay for a key binding */
52 if (action->func == action_moveresize &&
53 action->data.moveresize.corner !=
54 prop_atoms.net_wm_moveresize_move_keyboard &&
55 action->data.moveresize.corner !=
56 prop_atoms.net_wm_moveresize_size_keyboard) {
62 kbind(keylist, action);
64 nact = parse_find_node("action", nact->next);
69 static void parse_xml(xmlDocPtr doc, xmlNodePtr node, void *d)
71 parse_key(doc, node, NULL);
74 void plugin_setup_config()
76 parse_register("keyboard", parse_xml, NULL);
79 KeyBindingTree *firstnode = NULL;
81 static KeyBindingTree *curpos;
82 static guint reset_key, reset_state;
83 static ObTimer *chain_timer;
85 static void grab_for_window(Window win, gboolean grab)
92 p = curpos ? curpos->first_child : firstnode;
94 grab_key(p->key, p->state, win, GrabModeAsync);
98 grab_key(reset_key, reset_state, win, GrabModeAsync);
102 static void grab_keys(gboolean grab)
106 grab_for_window(focus_backup, grab);
107 for (it = client_list; it; it = g_list_next(it))
108 grab_for_window(((ObClient*)it->data)->frame->window, grab);
111 static void reset_chains()
114 timer_stop(chain_timer);
123 static void chain_timeout(void *data)
128 gboolean kbind(GList *keylist, Action *action)
130 KeyBindingTree *tree, *t;
133 g_assert(keylist != NULL);
134 g_assert(action != NULL);
136 if (!(tree = tree_build(keylist)))
139 if ((t = tree_find(tree, &conflict)) != NULL) {
140 /* already bound to something, use the existing tree */
145 while (t->first_child) t = t->first_child;
148 g_message("conflict with binding");
154 t->actions = g_slist_append(t->actions, action);
155 /* assimilate this built tree into the main tree. assimilation
156 destroys/uses the tree */
157 if (tree) tree_assimilate(tree);
162 static void event(ObEvent *e, void *foo)
164 static KeyBindingTree *grabbed_key = NULL;
166 if (e->type == Event_Client_Mapped) {
167 grab_for_window(e->data.c.client->window, TRUE);
169 } else if (e->type == Event_Client_Destroy) {
170 grab_for_window(e->data.c.client->window, FALSE);
175 gboolean done = FALSE;
177 if ((e->type == Event_X_KeyRelease &&
178 !(grabbed_key->state & e->data.x.e->xkey.state)))
180 else if (e->type == Event_X_KeyPress) {
181 if (e->data.x.e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
183 else if (e->data.x.e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
185 for (it = grabbed_key->actions; it; it = it->next) {
186 Action *act = it->data;
187 act->data.cycle.cancel = TRUE;
194 for (it = grabbed_key->actions; it; it = it->next) {
195 Action *act = it->data;
196 act->data.cycle.final = TRUE;
197 act->func(&act->data);
200 grab_keyboard(FALSE);
205 if (e->type == Event_X_KeyRelease)
208 g_assert(e->type == Event_X_KeyPress);
210 if (e->data.x.e->xkey.keycode == reset_key &&
211 e->data.x.e->xkey.state == reset_state) {
218 p = curpos->first_child;
220 if (p->key == e->data.x.e->xkey.keycode &&
221 p->state == e->data.x.e->xkey.state) {
222 if (p->first_child != NULL) { /* part of a chain */
223 if (chain_timer) timer_stop(chain_timer);
224 /* 5 second timeout for chains */
225 chain_timer = timer_start(5000*1000, chain_timeout,
231 for (it = p->actions; it; it = it->next) {
232 Action *act = it->data;
233 if (act->func != NULL) {
234 act->data.any.c = focus_client;
236 if (act->func == action_cycle_windows) {
237 act->data.cycle.final = FALSE;
238 act->data.cycle.cancel = FALSE;
241 if (act->func == action_cycle_windows &&
242 !grabbed_key && grab_keyboard(TRUE)) {
246 act->data.any.c = focus_client;
247 act->func(&act->data);
260 void plugin_startup()
265 dispatch_register(Event_Client_Mapped | Event_Client_Destroy |
266 Event_X_KeyPress | Event_X_KeyRelease,
267 (EventHandler)event, NULL);
269 translate_key("C-g", &reset_state, &reset_key);
274 void plugin_shutdown()
276 dispatch_register(0, (EventHandler)event, NULL);
278 tree_destroy(firstnode);