1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 actions.h for the Openbox window manager
4 Copyright (c) 2007 Dana Jansens
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 See the COPYING file for a copy of the GNU General Public License.
24 #include "actions/all.h"
26 static void actions_definition_ref(ObActionsDefinition
*def
);
27 static void actions_definition_unref(ObActionsDefinition
*def
);
28 static gboolean
actions_interactive_begin_act(ObActionsAct
*act
, guint state
);
29 static void actions_interactive_end_act();
31 static ObActionsAct
*interactive_act
= NULL
;
32 static guint interactive_initial_state
= 0;
34 struct _ObActionsDefinition
{
39 ObActionsDataSetupFunc setup
;
40 ObActionsDataFreeFunc free
;
42 ObActionsInteractiveInputFunc i_input
;
43 ObActionsInteractiveCancelFunc i_cancel
;
46 struct _ObActionsAct
{
49 ObActionsDefinition
*def
;
53 static GSList
*registered
= NULL
;
56 void actions_startup(gboolean reconfig
)
63 void actions_shutdown(gboolean reconfig
)
65 actions_interactive_cancel_act();
69 /* free all the registered actions */
71 actions_definition_unref(registered
->data
);
72 registered
= g_slist_delete_link(registered
, registered
);
76 gboolean
actions_register(const gchar
*name
,
77 ObActionsDataSetupFunc setup
,
78 ObActionsDataFreeFunc free
,
80 ObActionsInteractiveInputFunc i_input
,
81 ObActionsInteractiveCancelFunc i_cancel
)
84 ObActionsDefinition
*def
;
86 g_assert(run
!= NULL
);
87 g_assert((i_input
== NULL
) == (i_cancel
== NULL
));
89 for (it
= registered
; it
; it
= g_slist_next(it
)) {
91 if (!g_ascii_strcasecmp(name
, def
->name
)) /* already registered */
95 def
= g_new(ObActionsDefinition
, 1);
97 def
->name
= g_strdup(name
);
101 def
->i_input
= i_input
;
102 def
->i_cancel
= i_cancel
;
104 registered
= g_slist_prepend(registered
, def
);
109 static void actions_definition_ref(ObActionsDefinition
*def
)
114 static void actions_definition_unref(ObActionsDefinition
*def
)
116 if (def
&& --def
->ref
== 0) {
122 ObActionsAct
* actions_parse_string(const gchar
*name
)
125 ObActionsDefinition
*def
= NULL
;
126 ObActionsAct
*act
= NULL
;
128 /* find the requested action */
129 for (it
= registered
; it
; it
= g_slist_next(it
)) {
131 if (!g_ascii_strcasecmp(name
, def
->name
))
136 /* if we found the action */
138 act
= g_new(ObActionsAct
, 1);
141 actions_definition_ref(act
->def
);
144 g_message(_("Invalid action '%s' requested. No such action exists."),
150 ObActionsAct
* actions_parse(ObParseInst
*i
,
155 ObActionsAct
*act
= NULL
;
157 if (parse_attr_string("name", node
, &name
)) {
158 if ((act
= actions_parse_string(name
)))
159 /* there is more stuff to parse here */
161 act
->options
= act
->def
->setup(i
, doc
, node
->xmlChildrenNode
);
169 gboolean
actions_act_is_interactive(ObActionsAct
*act
)
171 return act
->def
->i_cancel
!= NULL
;
174 void actions_act_ref(ObActionsAct
*act
)
179 void actions_act_unref(ObActionsAct
*act
)
181 if (act
&& --act
->ref
== 0) {
182 /* free the action specific options */
184 act
->def
->free(act
->options
);
185 /* unref the definition */
186 actions_definition_unref(act
->def
);
191 static void actions_setup_data(ObActionsData
*data
,
197 struct _ObClient
*client
)
204 data
->client
= client
;
207 void actions_run_acts(GSList
*acts
,
213 struct _ObClient
*client
)
217 /* Don't allow saving the initial state when running things from the
219 if (uact
== OB_USER_ACTION_MENU_SELECTION
)
221 /* If x and y are < 0 then use the current pointer position */
223 screen_pointer_pos(&x
, &y
);
225 for (it
= acts
; it
; it
= g_slist_next(it
)) {
227 ObActionsAct
*act
= it
->data
;
230 actions_setup_data(&data
, uact
, state
, x
, y
, con
, client
);
232 if (actions_act_is_interactive(act
) &&
233 (!interactive_act
|| interactive_act
->def
!= act
->def
))
235 ok
= actions_interactive_begin_act(act
, state
);
238 /* fire the action's run function with this data */
240 if (!act
->def
->run(&data
, act
->options
))
241 actions_interactive_end_act();
243 /* make sure its interactive if it returned TRUE */
244 g_assert(act
->def
->i_cancel
&& act
->def
->i_input
);
246 /* no actions are run after the interactive one */
253 gboolean
actions_interactive_act_running()
255 return interactive_act
!= NULL
;
258 void actions_interactive_cancel_act()
260 if (interactive_act
) {
261 interactive_act
->def
->i_cancel(interactive_act
->options
);
262 actions_interactive_end_act();
266 static gboolean
actions_interactive_begin_act(ObActionsAct
*act
, guint state
)
268 /* cancel the old one */
270 actions_interactive_cancel_act();
272 if (grab_keyboard()) {
273 interactive_act
= act
;
274 actions_act_ref(interactive_act
);
276 interactive_initial_state
= state
;
283 static void actions_interactive_end_act()
285 if (interactive_act
) {
288 actions_act_unref(interactive_act
);
289 interactive_act
= NULL
;
293 gboolean
actions_interactive_input_event(XEvent
*e
)
295 gboolean used
= FALSE
;
296 if (interactive_act
) {
297 if (!interactive_act
->def
->i_input(interactive_initial_state
, e
,
298 interactive_act
->options
, &used
))
300 used
= TRUE
; /* if it cancelled the action then it has to of
302 actions_interactive_end_act();