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 #include "filters/all.h"
24 typedef struct _ObActionFilterDefinition ObActionFilterDefinition;
26 struct _ObActionFilterDefinition {
28 ObActionFilterSetupFunc setup;
29 ObActionFilterDestroyFunc destroy;
30 ObActionFilterReduceFunc reduce;
31 ObActionFilterExpandFunc expand;
34 struct _ObActionFilter {
37 ObActionFilterDefinition *def;
41 static void action_filter_unregister(ObActionFilterDefinition *def);
43 /*! Holds all registered filters. */
44 static GSList *registered = NULL;
46 void action_filter_startup(gboolean reconfig)
48 filters_all_startup();
51 void action_filter_shutdown(gboolean reconfig)
55 for (it = registered; it; it = g_slist_next(it))
56 action_filter_unregister(it->data);
60 gboolean action_filter_register(const gchar *name,
61 ObActionFilterSetupFunc setup,
62 ObActionFilterDestroyFunc destroy,
63 ObActionFilterReduceFunc reduce,
64 ObActionFilterExpandFunc expand)
66 ObActionFilterDefinition *def;
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);
74 for (it = registered; it; it = it->next) {
76 if (g_strcasecmp(name, def->name) == 0)
77 return FALSE; /* already registered */
80 def = g_slice_new(ObActionFilterDefinition);
81 def->name = g_strdup(name);
83 def->destroy = destroy;
86 registered = g_slist_prepend(registered, def);
91 static void action_filter_unregister(ObActionFilterDefinition *def)
95 g_slice_free(ObActionFilterDefinition, def);
99 ObActionFilter* action_filter_new(const gchar *key, struct _ObActionValue *v)
101 ObActionFilterDefinition *def;
102 ObActionFilter *filter;
107 if (key[0] == 'n' || key[0] == 'N')
108 if (key[1] == 'o' || key[1] == 'O') {
113 for (it = registered; it; it = g_slist_next(it)) {
115 if (g_strcasecmp(key, def->name) == 0)
119 g_message(_("Invalid filter \"%s\" requested. No such filter exists."),
124 filter = g_slice_new(ObActionFilter);
127 filter->data = def->setup(invert, v);
131 void action_filter_ref(ObActionFilter *f)
136 void action_filter_unref(ObActionFilter *f)
138 if (f && --f->ref < 1) {
139 if (f->def->destroy) f->def->destroy(f->data);
140 g_slice_free(ObActionFilter, f);
144 void action_filter_expand(ObActionFilter *f, struct _ObClientSet *set)
146 g_return_if_fail(f != NULL);
147 return f->def->expand(set);
150 void action_filter_reduce(ObActionFilter *f, struct _ObClientSet *set)
152 g_return_if_fail(f != NULL);
153 return f->def->reduce(set);