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.
28 #include "actions/all.h"
30 static void actions_definition_ref(ObActionsDefinition
*def
);
31 static void actions_definition_unref(ObActionsDefinition
*def
);
32 static gboolean
actions_interactive_begin_act(ObActionsAct
*act
, guint state
);
33 static void actions_interactive_end_act();
34 static ObActionsAct
* actions_build_act_from_string(const gchar
*name
);
36 static ObActionsAct
*interactive_act
= NULL
;
37 static guint interactive_initial_state
= 0;
39 struct _ObActionsDefinition
{
44 ObActionsDataSetupFunc setup
;
45 ObActionsDataFreeFunc free
;
47 ObActionsInteractiveInputFunc i_input
;
48 ObActionsInteractiveCancelFunc i_cancel
;
51 struct _ObActionsAct
{
54 ObActionsDefinition
*def
;
58 static GSList
*registered
= NULL
;
61 void actions_startup(gboolean reconfig
)
68 void actions_shutdown(gboolean reconfig
)
70 actions_interactive_cancel_act();
74 /* free all the registered actions */
76 actions_definition_unref(registered
->data
);
77 registered
= g_slist_delete_link(registered
, registered
);
81 gboolean
actions_register(const gchar
*name
,
82 ObActionsDataSetupFunc setup
,
83 ObActionsDataFreeFunc free
,
85 ObActionsInteractiveInputFunc i_input
,
86 ObActionsInteractiveCancelFunc i_cancel
)
89 ObActionsDefinition
*def
;
91 g_assert(run
!= NULL
);
92 g_assert((i_input
== NULL
) == (i_cancel
== NULL
));
94 for (it
= registered
; it
; it
= g_slist_next(it
)) {
96 if (!g_ascii_strcasecmp(name
, def
->name
)) /* already registered */
100 def
= g_new(ObActionsDefinition
, 1);
102 def
->name
= g_strdup(name
);
106 def
->i_input
= i_input
;
107 def
->i_cancel
= i_cancel
;
109 registered
= g_slist_prepend(registered
, def
);
114 static void actions_definition_ref(ObActionsDefinition
*def
)
119 static void actions_definition_unref(ObActionsDefinition
*def
)
121 if (def
&& --def
->ref
== 0) {
127 ObActionsAct
* actions_build_act_from_string(const gchar
*name
)
130 ObActionsDefinition
*def
= NULL
;
131 ObActionsAct
*act
= NULL
;
133 /* find the requested action */
134 for (it
= registered
; it
; it
= g_slist_next(it
)) {
136 if (!g_ascii_strcasecmp(name
, def
->name
))
141 /* if we found the action */
143 act
= g_new(ObActionsAct
, 1);
146 actions_definition_ref(act
->def
);
149 g_message(_("Invalid action '%s' requested. No such action exists."),
155 ObActionsAct
* actions_parse_string(const gchar
*name
)
157 ObActionsAct
*act
= NULL
;
159 if ((act
= actions_build_act_from_string(name
)))
161 act
->options
= act
->def
->setup(NULL
, NULL
, NULL
);
166 ObActionsAct
* actions_parse(ObParseInst
*i
,
171 ObActionsAct
*act
= NULL
;
173 if (parse_attr_string("name", node
, &name
)) {
174 if ((act
= actions_build_act_from_string(name
)))
175 /* there is more stuff to parse here */
177 act
->options
= act
->def
->setup(i
, doc
, node
->xmlChildrenNode
);
185 gboolean
actions_act_is_interactive(ObActionsAct
*act
)
187 return act
->def
->i_cancel
!= NULL
;
190 void actions_act_ref(ObActionsAct
*act
)
195 void actions_act_unref(ObActionsAct
*act
)
197 if (act
&& --act
->ref
== 0) {
198 /* free the action specific options */
200 act
->def
->free(act
->options
);
201 /* unref the definition */
202 actions_definition_unref(act
->def
);
207 static void actions_setup_data(ObActionsData
*data
,
214 struct _ObClient
*client
)
220 data
->button
= button
;
222 data
->client
= client
;
225 void actions_run_acts(GSList
*acts
,
232 struct _ObClient
*client
)
236 /* Don't allow saving the initial state when running things from the
238 if (uact
== OB_USER_ACTION_MENU_SELECTION
)
240 /* If x and y are < 0 then use the current pointer position */
242 screen_pointer_pos(&x
, &y
);
244 for (it
= acts
; it
; it
= g_slist_next(it
)) {
246 ObActionsAct
*act
= it
->data
;
249 actions_setup_data(&data
, uact
, state
, x
, y
, button
, con
, client
);
251 if (!interactive_act
|| interactive_act
->def
!= act
->def
) {
252 if (actions_act_is_interactive(act
)) {
253 /* cancel the old one */
255 actions_interactive_cancel_act();
256 ok
= actions_interactive_begin_act(act
, state
);
260 /* fire the action's run function with this data */
262 if (!act
->def
->run(&data
, act
->options
))
263 actions_interactive_end_act();
265 /* make sure its interactive if it returned TRUE */
266 g_assert(act
->def
->i_cancel
&& act
->def
->i_input
);
268 /* no actions are run after the interactive one */
275 gboolean
actions_interactive_act_running()
277 return interactive_act
!= NULL
;
280 void actions_interactive_cancel_act()
282 if (interactive_act
) {
283 interactive_act
->def
->i_cancel(interactive_act
->options
);
284 actions_interactive_end_act();
288 static gboolean
actions_interactive_begin_act(ObActionsAct
*act
, guint state
)
290 if (grab_keyboard()) {
291 interactive_act
= act
;
292 actions_act_ref(interactive_act
);
294 interactive_initial_state
= state
;
301 static void actions_interactive_end_act()
303 if (interactive_act
) {
306 actions_act_unref(interactive_act
);
307 interactive_act
= NULL
;
311 gboolean
actions_interactive_input_event(XEvent
*e
)
313 gboolean used
= FALSE
;
314 if (interactive_act
) {
315 if (!interactive_act
->def
->i_input(interactive_initial_state
, e
,
316 interactive_act
->options
, &used
))
318 used
= TRUE
; /* if it cancelled the action then it has to of
320 actions_interactive_end_act();
326 void actions_client_move(ObActionsData
*data
, gboolean start
)
328 static gulong ignore_start
= 0;
330 ignore_start
= event_start_ignore_all_enters();
331 else if (config_focus_follow
&&
332 data
->context
!= OB_FRAME_CONTEXT_CLIENT
)
334 if (!data
->button
&& data
->client
&& !config_focus_under_mouse
)
335 event_end_ignore_all_enters(ignore_start
);
339 /* usually this is sorta redundant, but with a press action
340 that moves windows our from under the cursor, the enter
341 event will come as a GrabNotify which is ignored, so this
342 makes a fake enter event
344 if ((c
= client_under_pointer()) && c
!= data
->client
) {
345 ob_debug_type(OB_DEBUG_FOCUS
,
346 "Generating fake enter because we did a "
347 "mouse-event action");
348 event_enter_client(c
);