1 #include "kernel/focus.h"
2 #include "kernel/dispatch.h"
3 #include "kernel/openbox.h"
4 #include "kernel/event.h"
5 #include "kernel/grab.h"
6 #include "kernel/action.h"
7 #include "kernel/prop.h"
8 #include "kernel/parse.h"
9 #include "kernel/timer.h"
12 #include "translate.h"
18 <action name="ChangeDesktop">
25 static void parse_key(xmlDocPtr doc
, xmlNodePtr node
, GList
*keylist
)
32 n
= parse_find_node("keybind", node
);
34 if (parse_attr_string("key", n
, &key
)) {
35 keylist
= g_list_append(keylist
, key
);
37 parse_key(doc
, n
->xmlChildrenNode
, keylist
);
39 it
= g_list_last(keylist
);
41 keylist
= g_list_delete_link(keylist
, it
);
43 n
= parse_find_node("keybind", n
->next
);
46 nact
= parse_find_node("action", node
);
48 if ((action
= parse_action(doc
, nact
))) {
49 /* validate that its okay for a key binding */
50 if (action
->func
== action_moveresize
&&
51 action
->data
.moveresize
.corner
!=
52 prop_atoms
.net_wm_moveresize_move_keyboard
&&
53 action
->data
.moveresize
.corner
!=
54 prop_atoms
.net_wm_moveresize_size_keyboard
) {
60 kbind(keylist
, action
);
62 nact
= parse_find_node("action", nact
->next
);
67 static void parse_xml(xmlDocPtr doc
, xmlNodePtr node
, void *d
)
69 parse_key(doc
, node
, NULL
);
72 void plugin_setup_config()
74 parse_register("keyboard", parse_xml
, NULL
);
77 KeyBindingTree
*firstnode
= NULL
;
79 static KeyBindingTree
*curpos
;
80 static guint reset_key
, reset_state
, button_return
, button_escape
;
81 static Timer
*chain_timer
;
83 static void grab_keys()
89 p
= curpos
? curpos
->first_child
: firstnode
;
91 grab_key(p
->key
, p
->state
, GrabModeAsync
);
95 grab_key(reset_key
, reset_state
, GrabModeAsync
);
98 static void reset_chains()
101 timer_stop(chain_timer
);
110 static void chain_timeout(void *data
)
115 gboolean
kbind(GList
*keylist
, Action
*action
)
117 KeyBindingTree
*tree
, *t
;
120 g_assert(keylist
!= NULL
);
121 g_assert(action
!= NULL
);
123 if (!(tree
= tree_build(keylist
)))
126 if ((t
= tree_find(tree
, &conflict
)) != NULL
) {
127 /* already bound to something, use the existing tree */
132 while (t
->first_child
) t
= t
->first_child
;
135 g_message("conflict with binding");
141 t
->actions
= g_slist_append(t
->actions
, action
);
142 /* assimilate this built tree into the main tree. assimilation
143 destroys/uses the tree */
144 if (tree
) tree_assimilate(tree
);
149 static void event(ObEvent
*e
, void *foo
)
151 static KeyBindingTree
*grabbed_key
= NULL
;
154 gboolean done
= FALSE
;
156 if ((e
->type
== Event_X_KeyRelease
&&
157 !(grabbed_key
->state
& e
->data
.x
.e
->xkey
.state
)))
159 else if (e
->type
== Event_X_KeyPress
) {
160 if (e
->data
.x
.e
->xkey
.keycode
== button_return
)
162 else if (e
->data
.x
.e
->xkey
.keycode
== button_escape
) {
164 for (it
= grabbed_key
->actions
; it
; it
= it
->next
) {
165 Action
*act
= it
->data
;
166 act
->data
.cycle
.cancel
= TRUE
;
173 for (it
= grabbed_key
->actions
; it
; it
= it
->next
) {
174 Action
*act
= it
->data
;
175 act
->data
.cycle
.final
= TRUE
;
176 act
->func(&act
->data
);
179 grab_keyboard(FALSE
);
184 if (e
->type
== Event_X_KeyRelease
)
187 g_assert(e
->type
== Event_X_KeyPress
);
189 if (e
->data
.x
.e
->xkey
.keycode
== reset_key
&&
190 e
->data
.x
.e
->xkey
.state
== reset_state
) {
197 p
= curpos
->first_child
;
199 if (p
->key
== e
->data
.x
.e
->xkey
.keycode
&&
200 p
->state
== e
->data
.x
.e
->xkey
.state
) {
201 if (p
->first_child
!= NULL
) { /* part of a chain */
202 if (chain_timer
) timer_stop(chain_timer
);
203 /* 5 second timeout for chains */
204 chain_timer
= timer_start(5000*1000, chain_timeout
,
210 for (it
= p
->actions
; it
; it
= it
->next
) {
211 Action
*act
= it
->data
;
212 if (act
->func
!= NULL
) {
213 act
->data
.any
.c
= focus_client
;
215 if (act
->func
== action_cycle_windows
) {
216 act
->data
.cycle
.final
= FALSE
;
217 act
->data
.cycle
.cancel
= FALSE
;
220 act
->data
.any
.c
= focus_client
;
221 act
->func(&act
->data
);
223 if (act
->func
== action_cycle_windows
&&
241 void plugin_startup()
248 dispatch_register(Event_X_KeyPress
| Event_X_KeyRelease
,
249 (EventHandler
)event
, NULL
);
251 translate_key("C-g", &reset_state
, &reset_key
);
252 translate_key("Escape", &i
, &button_escape
);
253 translate_key("Return", &i
, &button_return
);
258 void plugin_shutdown()
260 dispatch_register(0, (EventHandler
)event
, NULL
);
262 tree_destroy(firstnode
);