add interactive action functions. some other changes to stuff that wasnt going to...
[mikachu/openbox.git] / openbox / actions.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    actions.h for the Openbox window manager
4    Copyright (c) 2007        Dana Jansens
5
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.
10
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.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "actions.h"
20 #include "gettext.h"
21
22 static void actions_definition_ref(ObActionsDefinition *def);
23 static void actions_definition_unref(ObActionsDefinition *def);
24
25 struct _ObActionsDefinition {
26     guint ref;
27
28     gchar *name;
29     ObActionsType type;
30
31     ObActionsDataSetupFunc setup;
32     ObActionsDataFreeFunc free;
33     ObActionsRunFunc run;
34     ObActionsInteractiveInputFunc i_input;
35     ObActionsInteractiveCancelFunc i_cancel;
36 };
37
38 struct _ObActionsAct {
39     guint ref;
40
41     ObActionsDefinition *def;
42     gpointer options;
43 };
44
45 static GSList *registered = NULL;
46
47
48 void actions_startup(gboolean reconfig)
49 {
50     if (reconfig) return;
51
52     
53 }
54
55 void actions_shutdown(gboolean reconfig)
56 {
57     if (reconfig) return;
58
59     /* free all the registered actions */
60     while (registered) {
61         actions_definition_unref(registered->data);
62         registered = g_slist_delete_link(registered, registered);
63     }
64 }
65
66 gboolean actions_register(const gchar *name,
67                           ObActionsType type,
68                           ObActionsDataSetupFunc setup,
69                           ObActionsDataFreeFunc free,
70                           ObActionsRunFunc run,
71                           ObActionsInteractiveInputFunc i_input,
72                           ObActionsInteractiveCancelFunc i_cancel)
73 {
74     GSList *it;
75     ObActionsDefinition *def;
76
77     for (it = registered; it; it = g_slist_next(it)) {
78         def = it->data;
79         if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
80             return FALSE;
81     }
82
83     g_assert((i_input == NULL) == (i_cancel == NULL));
84
85     def = g_new(ObActionsDefinition, 1);
86     def->ref = 1;
87     def->name = g_strdup(name);
88     def->type = type;
89     def->setup = setup;
90     def->free = free;
91     def->run = run;
92     def->i_input = i_input;
93     def->i_cancel = i_cancel;
94     return TRUE;
95 }
96
97 static void actions_definition_ref(ObActionsDefinition *def)
98 {
99     ++def->ref;
100 }
101
102 static void actions_definition_unref(ObActionsDefinition *def)
103 {
104     if (def && --def->ref == 0) {
105         g_free(def->name);
106         g_free(def);
107     }
108 }
109
110 ObActionsAct* actions_parse_string(const gchar *name)
111 {
112     GSList *it;
113     ObActionsDefinition *def;
114     ObActionsAct *act = NULL;
115
116     /* find the requested action */
117     for (it = registered; it; it = g_slist_next(it)) {
118         def = it->data;
119         if (!g_ascii_strcasecmp(name, def->name))
120             break;
121         def = NULL;
122     }
123
124     /* if we found the action */
125     if (def) {
126         act = g_new(ObActionsAct, 1);
127         act->ref = 1;
128         act->def = def;
129         actions_definition_ref(act->def);
130         act->options = NULL;
131     } else
132         g_message(_("Invalid action '%s' requested. No such action exists."),
133                   name);
134
135     return act;
136 }
137
138 ObActionsAct* actions_parse(ObParseInst *i,
139                             xmlDocPtr doc,
140                             xmlNodePtr node)
141 {
142     gchar *name;
143     ObActionsAct *act = NULL;
144
145     if (parse_attr_string("name", node, &name)) {
146         if ((act = actions_parse_string(name)))
147             /* there is more stuff to parse here */
148             act->options = act->def->setup(i, doc, node->children);
149
150         g_free(name);
151     }
152
153     return act;
154 }
155
156 gboolean actions_act_is_interactive(ObActionsAct *act)
157 {
158     return act->def->i_cancel != NULL;
159 }
160
161 void actions_act_ref(ObActionsAct *act)
162 {
163     ++act->ref;
164 }
165
166 void actions_act_unref(ObActionsAct *act)
167 {
168     if (act && --act->ref == 0) {
169         /* free the action specific options */
170         act->def->free(act->options);
171         /* unref the definition */
172         actions_definition_unref(act->def);
173         g_free(act);
174     }
175 }
176
177 static void actions_setup_data(ObActionsData *data,
178                                ObUserAction uact,
179                                Time time,
180                                guint state,
181                                gint x,
182                                gint y)
183 {
184     data->any.uact = uact;
185     data->any.time = time;
186     data->any.state = state;
187     data->any.x = x;
188     data->any.y = y;
189 }
190
191 void actions_run_acts(GSList *acts,
192                       ObUserAction uact,
193                       Time time,
194                       guint state,
195                       gint x,
196                       gint y,
197                       ObFrameContext con,
198                       struct _ObClient *client)
199 {
200     GSList *it;
201
202     for (it = acts; it; it = g_slist_next(it)) {
203         ObActionsData data;
204         ObActionsAct *act = it->data;
205
206         data.type = act->def->type;
207         actions_setup_data(&data, uact, time, state, x, y);
208         switch (data.type) {
209         case OB_ACTION_TYPE_GLOBAL:
210             break;
211         case OB_ACTION_TYPE_CLIENT:
212             data.client.context = con;
213             data.client.c = client;
214             break;
215         default:
216             g_assert_not_reached();
217         }
218
219         /* fire the action's run function with this data */
220         act->def->run(&data, act->options);
221     }
222 }