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"
20 #include "action_list_run.h"
21 #include "client_set.h"
24 #include "filters/_all.h"
26 typedef struct _ObActionFilterDefinition ObActionFilterDefinition;
28 struct _ObActionFilterDefinition {
30 ObActionFilterSetupFunc setup;
31 ObActionFilterDestroyFunc destroy;
32 ObActionFilterFunc set;
35 struct _ObActionFilter {
38 ObActionFilterDefinition *def;
43 static void action_filter_unregister(ObActionFilterDefinition *def);
45 /*! Holds all registered filters. */
46 static GSList *registered = NULL;
48 void action_filter_startup(gboolean reconfig)
50 filters__all_startup();
53 void action_filter_shutdown(gboolean reconfig)
57 for (it = registered; it; it = g_slist_next(it))
58 action_filter_unregister(it->data);
62 gboolean action_filter_register(const gchar *name,
63 ObActionFilterSetupFunc setup,
64 ObActionFilterDestroyFunc destroy,
65 ObActionFilterFunc set)
67 ObActionFilterDefinition *def;
70 g_return_val_if_fail(name != NULL, FALSE);
71 g_return_val_if_fail(set != NULL, FALSE);
73 for (it = registered; it; it = it->next) {
75 if (g_strcasecmp(name, def->name) == 0)
76 return FALSE; /* already registered */
79 def = g_slice_new(ObActionFilterDefinition);
80 def->name = g_strdup(name);
82 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 _ObConfigValue *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->invert = invert;
126 filter->data = def->setup ? def->setup(invert, v) : NULL;
130 void action_filter_ref(ObActionFilter *f)
135 void action_filter_unref(ObActionFilter *f)
137 if (f && --f->ref < 1) {
138 if (f->def->destroy) f->def->destroy(f->data);
139 g_slice_free(ObActionFilter, f);
143 struct _ObClientSet* action_filter_set(ObActionFilter *f,
144 const ObActionListRun *run)
146 return f->def->set(f->invert, run, f->data);