maybe the new actions framework is kinda there now
[dana/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     gboolean allow_interactive;
30
31     ObActionsDataSetupFunc setup;
32     ObActionsDataFreeFunc free;
33     ObActionsRunFunc run;
34 };
35
36 struct _ObActionsAct {
37     guint ref;
38
39     ObActionsDefinition *def;
40
41     gpointer options;
42 };
43
44 static GSList *registered = NULL;
45
46
47 void actions_startup(gboolean reconfig)
48 {
49     if (reconfig) return;
50
51     
52 }
53
54 void actions_shutdown(gboolean reconfig)
55 {
56     if (reconfig) return;
57
58     /* free all the registered actions */
59     while (registered) {
60         actions_definition_unref(registered->data);
61         registered = g_slist_delete_link(registered, registered);
62     }
63 }
64
65 gboolean actions_register(const gchar *name,
66                           gboolean allow_interactive,
67                           ObActionsDataSetupFunc setup,
68                           ObActionsDataFreeFunc free,
69                           ObActionsRunFunc run)
70 {
71     GSList *it;
72     ObActionsDefinition *def;
73
74     for (it = registered; it; it = g_slist_next(it)) {
75         def = it->data;
76         if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
77             return FALSE;
78     }
79
80     def = g_new(ObActionsDefinition, 1);
81     def->ref = 1;
82     def->name = g_strdup(name);
83     def->allow_interactive = allow_interactive;
84     def->setup = setup;
85     def->free = free;
86     def->run = run;
87     return TRUE;
88 }
89
90 static void actions_definition_ref(ObActionsDefinition *def)
91 {
92     ++def->ref;
93 }
94
95 static void actions_definition_unref(ObActionsDefinition *def)
96 {
97     if (def && --def->ref == 0) {
98         g_free(def->name);
99         g_free(def);
100     }
101 }
102
103 ObActionsAct* actions_parse(ObParseInst *i,
104                             xmlDocPtr doc,
105                             xmlNodePtr node)
106 {
107     GSList *it;
108     gchar *name;
109     ObActionsDefinition *def;
110     ObActionsAct *act = NULL;
111
112     if (!parse_attr_string("name", node, &name)) return NULL;
113
114     /* find the requested action */
115     for (it = registered; it; it = g_slist_next(it)) {
116         def = it->data;
117         if (!g_ascii_strcasecmp(name, def->name))
118             break;
119     }
120
121     /* if we found the action */
122     if (it != NULL) {
123         act = g_new(ObActionsAct, 1);
124         act->ref = 1;
125         act->def = def;
126         actions_definition_ref(act->def);
127         act->options = def->setup(i, doc, node->children);
128     } else
129         g_message(_("Invalid action '%s' requested. No such action exists."),
130                   name);
131
132     g_free(name);
133
134     return act;
135 }
136
137 void actions_act_ref(ObActionsAct *act)
138 {
139     ++act->ref;
140 }
141
142 void actions_act_unref(ObActionsAct *act)
143 {
144     if (act && --act->ref == 0) {
145         /* free the action specific options */
146         act->def->free(act->options);
147         /* unref the definition */
148         actions_definition_unref(act->def);
149         g_free(act);
150     }
151 }