Make callbacks to rules.execute optional
[awesome.git] / common / signal.h
blobf9a4cc1bb04f1bdda49fe4c9b055a34d3634d13b
1 /*
2 * common/signal.h - Signal handling functions
4 * Copyright © 2009 Julien Danjou <julien@danjou.info>
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 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #ifndef AWESOME_COMMON_SIGNAL
23 #define AWESOME_COMMON_SIGNAL
25 #include "common/array.h"
27 DO_ARRAY(const void *, cptr, DO_NOTHING)
29 typedef struct
31 unsigned long id;
32 cptr_array_t sigfuncs;
33 } signal_t;
35 static inline int
36 signal_cmp(const void *a, const void *b)
38 const signal_t *x = a, *y = b;
39 return x->id > y->id ? 1 : (x->id < y->id ? -1 : 0);
42 static inline void
43 signal_wipe(signal_t *sig)
45 cptr_array_wipe(&sig->sigfuncs);
48 ARRAY_TYPE_EXTRA(signal_t, signal, struct signal_array_t *inherits_from)
49 BARRAY_FUNCS(signal_t, signal, signal_wipe, signal_cmp)
51 static inline signal_t *
52 signal_array_getbyid(signal_array_t *arr, unsigned long id)
54 signal_t sig = { .id = id };
55 signal_t *result;
57 result = signal_array_lookup(arr, &sig);
58 if(result)
59 return result;
61 /* The signal doesn't exist yet. Check if some of our inherits_from have the
62 * signal and if yes, add it.
64 signal_array_t *inherit = arr->inherits_from;
65 while(inherit != NULL)
67 if(signal_array_lookup(inherit, &sig))
68 break;
69 inherit = inherit->inherits_from;
71 if(inherit)
73 /* Add the signal to this array to pretend it always existed */
74 signal_array_insert(arr, sig);
75 result = signal_array_lookup(arr, &sig);
76 assert(result != NULL);
78 return result;
81 /** Add a signal to a signal array.
82 * Signals have to be added before they can be added or emitted.
83 * \param arr The signal array.
84 * \param name The signal name.
86 static inline void
87 signal_add(signal_array_t *arr, const char *name)
89 unsigned long tok = a_strhash((const unsigned char *) name);
90 signal_t *sigfound = signal_array_getbyid(arr, tok);
91 if(!sigfound)
93 signal_t sig = { .id = tok };
94 signal_array_insert(arr, sig);
98 /** Connect a signal inside a signal array.
99 * You are in charge of reference counting.
100 * \param arr The signal array.
101 * \param name The signal name.
102 * \param ref The reference to add.
104 static inline void
105 signal_connect(signal_array_t *arr, const char *name, const void *ref)
107 unsigned long tok = a_strhash((const unsigned char *) name);
108 signal_t *sigfound = signal_array_getbyid(arr, tok);
109 if(sigfound)
110 cptr_array_append(&sigfound->sigfuncs, ref);
111 else
112 warn("Trying to connect to unknown signal '%s'", name);
115 /** Disconnect a signal inside a signal array.
116 * You are in charge of reference counting.
117 * \param arr The signal array.
118 * \param name The signal name.
119 * \param ref The reference to remove.
121 static inline void
122 signal_disconnect(signal_array_t *arr, const char *name, const void *ref)
124 signal_t *sigfound = signal_array_getbyid(arr,
125 a_strhash((const unsigned char *) name));
126 if(sigfound)
128 foreach(func, sigfound->sigfuncs)
129 if(ref == *func)
131 cptr_array_remove(&sigfound->sigfuncs, func);
132 break;
134 } else
135 warn("Trying to disconnect from unknown signal '%s'", name);
138 #endif
140 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80