Change LUAKIT_LUA_LIB_PATH to LUAKIT_INSTALL_PATH
[luakit.git] / common / signal.h
blobfa8f6fbbde848c0a51826a4987a11d2c471bfe43
1 /*
2 * signal.h - Signal handling functions
4 * Copyright (C) 2010 Mason Larobina <mason.larobina@gmail.com>
5 * Copyright (C) 2009 Julien Danjou <julien@danjou.info>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef LUAKIT_COMMON_SIGNAL
23 #define LUAKIT_COMMON_SIGNAL
25 #include <glib/gstrfuncs.h>
26 #include <glib/garray.h>
27 #include <glib/gtree.h>
29 #include "common/util.h"
31 typedef GTree signal_t;
32 typedef GPtrArray signal_array_t;
34 /* wrapper around g_ptr_array_new */
35 static inline signal_array_t *
36 signal_array_new(void) {
37 return (signal_array_t *) g_ptr_array_new();
40 /* wrapper around g_ptr_array_remove */
41 static inline void
42 signal_array_remove(signal_array_t *sigfuncs, gpointer ref) {
43 g_ptr_array_remove((GPtrArray *) sigfuncs, ref);
46 /* wrapper around g_ptr_array_free */
47 static inline void
48 signal_array_destroy(signal_array_t *sigfuncs) {
49 if (sigfuncs)
50 g_ptr_array_free((GPtrArray * )sigfuncs, TRUE);
51 sigfuncs = NULL;
54 /* wrapper around g_ptr_array_add */
55 static inline void
56 signal_array_insert(signal_array_t *sigfuncs, gpointer ref) {
57 g_ptr_array_add((GPtrArray *) sigfuncs, ref);
60 /* wrapper around g_tree_new */
61 static inline signal_t *
62 signal_tree_new(void) {
63 return (signal_t *) g_tree_new((GCompareFunc) strcmp);
66 /* wrapper around g_tree_remove */
67 static inline void
68 signal_tree_remove(signal_t *signals, const gchar *name) {
69 g_tree_remove((GTree *) signals, (gpointer) name);
72 /* wrapper around g_tree_destroy */
73 static inline void
74 signal_tree_destroy(signal_t *signals) {
75 if (signals)
76 g_tree_destroy((GTree *) signals);
77 signals = NULL;
80 static inline signal_array_t *
81 signal_lookup(signal_t *signals, const gchar *name, gboolean create) {
82 if (!signals) return NULL;
84 signal_array_t *sigfuncs = g_tree_lookup((GTree *) signals,
85 (gpointer) name);
87 /* create if asked and not found */
88 if (create && !sigfuncs) {
89 sigfuncs = signal_array_new();
90 g_tree_insert((GTree *) signals, (gpointer) g_strdup(name), sigfuncs);
92 return sigfuncs;
95 static inline void
96 signal_add(signal_t *signals, const gchar *name, gpointer ref) {
97 /* find ptr array for this signal */
98 signal_array_t *sigfuncs = signal_lookup(signals, name, TRUE);
99 /* add the handler to this signals ptr array */
100 g_ptr_array_add((GPtrArray *) sigfuncs, ref);
103 static inline void
104 signal_remove(signal_t *signals, const gchar *name, gpointer ref) {
105 if(!signals) return;
106 /* try to find ptr array for this signal */
107 signal_array_t *sigfuncs = signal_lookup(signals, name, FALSE);
108 /* remove the signal handler if found */
109 if (sigfuncs)
110 signal_array_remove(sigfuncs, ref);
111 /* remove empty sigfuncs array from signals */
112 if (!sigfuncs->len)
113 signal_tree_remove(signals, name);
116 #endif
117 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80