1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 action_filter.c for the Openbox window manager
4 Copyright (c) 2011 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.
19 #include "action_filter.h"
22 typedef struct _ObActionFilterDefinition ObActionFilterDefinition;
24 struct _ObActionFilterDefinition {
26 ObActionFilterSetupFunc setup;
27 ObActionFilterDestroyFunc destroy;
28 ObActionFilterReduceFunc reduce;
29 ObActionFilterExpandFunc expand;
32 struct _ObActionFilter {
35 ObActionFilterDefinition *def;
39 static void action_filter_unregister(ObActionFilterDefinition *def);
41 /*! Holds all registered filters. */
42 static GSList *registered = NULL;
44 void action_filter_startup(gboolean reconfig)
46 //XXX filters_all_startup();
49 void action_filter_shutdown(gboolean reconfig)
53 for (it = registered; it; it = g_slist_next(it))
54 action_filter_unregister(it->data);
58 gboolean action_filter_register(const gchar *name,
59 ObActionFilterSetupFunc setup,
60 ObActionFilterDestroyFunc destroy,
61 ObActionFilterReduceFunc reduce,
62 ObActionFilterExpandFunc expand)
64 ObActionFilterDefinition *def;
67 g_return_val_if_fail(name != NULL, FALSE);
68 g_return_val_if_fail(setup != NULL, FALSE);
69 g_return_val_if_fail(reduce != NULL, FALSE);
70 g_return_val_if_fail(expand != NULL, FALSE);
72 for (it = registered; it; it = it->next) {
74 if (g_strcasecmp(name, def->name) == 0)
75 return FALSE; /* already registered */
78 def = g_slice_new(ObActionFilterDefinition);
79 def->name = g_strdup(name);
81 def->destroy = destroy;
84 registered = g_slist_prepend(registered, def);
89 static void action_filter_unregister(ObActionFilterDefinition *def)
93 g_slice_free(ObActionFilterDefinition, def);
97 ObActionFilter* action_filter_new(const gchar *key, struct _ObActionValue *v)
99 ObActionFilterDefinition *def;
100 ObActionFilter *filter;
105 if (key[0] == 'n' || key[0] == 'N')
106 if (key[1] == 'o' || key[1] == 'O') {
111 for (it = registered; it; it = g_slist_next(it)) {
113 if (g_strcasecmp(key, def->name) == 0)
117 g_message(_("Invalid filter \"%s\" requested. No such filter exists."),
122 filter = g_slice_new(ObActionFilter);
125 filter->data = def->setup(invert, v);
129 void action_filter_ref(ObActionFilter *f)
134 void action_filter_unref(ObActionFilter *f)
136 if (f && --f->ref < 1) {
137 if (f->def->destroy) f->def->destroy(f->data);
138 g_slice_free(ObActionFilter, f);
142 void action_filter_expand(ObActionFilter *f, GHashTable *client_set)
144 g_return_if_fail(f != NULL);
145 g_return_if_fail(client_set != NULL);
146 return f->def->expand(client_set);
149 void action_filter_reduce(ObActionFilter *f, GHashTable *client_set)
151 g_return_if_fail(f != NULL);
152 g_return_if_fail(client_set != NULL);
153 return f->def->reduce(client_set);