]> icculus.org git repositories - dana/openbox.git/blob - openbox/action_filter.c
Add filters/all.c which will run startup on all the filters to register them
[dana/openbox.git] / openbox / action_filter.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    action_filter.c for the Openbox window manager
4    Copyright (c) 2011        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 "action_filter.h"
20 #include "gettext.h"
21
22 #include "filters/all.h"
23
24 typedef struct _ObActionFilterDefinition ObActionFilterDefinition;
25
26 struct _ObActionFilterDefinition {
27     gchar *name;
28     ObActionFilterSetupFunc setup;
29     ObActionFilterDestroyFunc destroy;
30     ObActionFilterReduceFunc reduce;
31     ObActionFilterExpandFunc expand;
32 };
33
34 struct _ObActionFilter {
35     gint ref;
36
37     ObActionFilterDefinition *def;
38     gpointer data;
39 };
40
41 static void action_filter_unregister(ObActionFilterDefinition *def);
42
43 /*! Holds all registered filters. */
44 static GSList *registered = NULL;
45
46 void action_filter_startup(gboolean reconfig)
47 {
48     filters_all_startup();
49 }
50
51 void action_filter_shutdown(gboolean reconfig)
52 {
53     GSList *it;
54
55     for (it = registered; it; it = g_slist_next(it))
56         action_filter_unregister(it->data);
57     g_slist_free(it);
58 }
59
60 gboolean action_filter_register(const gchar *name,
61                                 ObActionFilterSetupFunc setup,
62                                 ObActionFilterDestroyFunc destroy,
63                                 ObActionFilterReduceFunc reduce,
64                                 ObActionFilterExpandFunc expand)
65 {
66     ObActionFilterDefinition *def;
67     GSList *it;
68
69     g_return_val_if_fail(name != NULL, FALSE);
70     g_return_val_if_fail(setup != NULL, FALSE);
71     g_return_val_if_fail(reduce != NULL, FALSE);
72     g_return_val_if_fail(expand != NULL, FALSE);
73
74     for (it = registered; it; it = it->next) {
75         def = it->data;
76         if (g_strcasecmp(name, def->name) == 0)
77             return FALSE; /* already registered */
78     }
79
80     def = g_slice_new(ObActionFilterDefinition);
81     def->name = g_strdup(name);
82     def->setup = setup;
83     def->destroy = destroy;
84     def->reduce = reduce;
85     def->expand = expand;
86     registered = g_slist_prepend(registered, def);
87
88     return TRUE;
89 }
90
91 static void action_filter_unregister(ObActionFilterDefinition *def)
92 {
93     if (def) {
94         g_free(def->name);
95         g_slice_free(ObActionFilterDefinition, def);
96     }
97 }
98
99 ObActionFilter* action_filter_new(const gchar *key, struct _ObActionValue *v)
100 {
101     ObActionFilterDefinition *def;
102     ObActionFilter *filter;
103     GSList *it;
104     gboolean invert;
105
106     invert = FALSE;
107     if (key[0] == 'n' || key[0] == 'N')
108         if (key[1] == 'o' || key[1] == 'O') {
109             key += 2;
110             invert = TRUE;
111         }
112
113     for (it = registered; it; it = g_slist_next(it)) {
114         def = it->data;
115         if (g_strcasecmp(key, def->name) == 0)
116             break;
117     }
118     if (!it) {
119         g_message(_("Invalid filter \"%s\" requested. No such filter exists."),
120                   key);
121         return NULL;
122     }
123
124     filter = g_slice_new(ObActionFilter);
125     filter->ref = 1;
126     filter->def = def;
127     filter->data = def->setup(invert, v);
128     return filter;
129 }
130
131 void action_filter_ref(ObActionFilter *f)
132 {
133     if (f) ++f->ref;
134 }
135
136 void action_filter_unref(ObActionFilter *f)
137 {
138     if (f && --f->ref < 1) {
139         if (f->def->destroy) f->def->destroy(f->data);
140         g_slice_free(ObActionFilter, f);
141     }
142 }
143
144 void action_filter_expand(ObActionFilter *f, GHashTable *client_set)
145 {
146     g_return_if_fail(f != NULL);
147     g_return_if_fail(client_set != NULL);
148     return f->def->expand(client_set);
149 }
150
151 void action_filter_reduce(ObActionFilter *f, GHashTable *client_set)
152 {
153     g_return_if_fail(f != NULL);
154     g_return_if_fail(client_set != NULL);
155     return f->def->reduce(client_set);
156 }