awful.tag: allow table as layout argument (FS#670)
[awesome.git] / common / signal.h
blob56e5faaad8c316b902ae64468708f25da5830b92
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/lualib.h"
26 #include "common/array.h"
28 DO_ARRAY(const void *, cptr, DO_NOTHING)
30 typedef struct
32 unsigned long id;
33 cptr_array_t sigfuncs;
34 } signal_t;
36 static inline int
37 signal_cmp(const void *a, const void *b)
39 const signal_t *x = a, *y = b;
40 return x->id > y->id ? 1 : (x->id < y->id ? -1 : 0);
43 static inline void
44 signal_wipe(signal_t *sig)
46 cptr_array_wipe(&sig->sigfuncs);
49 DO_BARRAY(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 return signal_array_lookup(arr, &sig);
58 /** Add a signal inside a signal array.
59 * You are in charge of reference counting.
60 * \param arr The signal array.
61 * \param name The signal name.
62 * \param ref The reference to add.
64 static inline void
65 signal_add(signal_array_t *arr, const char *name, const void *ref)
67 unsigned long tok = a_strhash((const unsigned char *) name);
68 signal_t *sigfound = signal_array_getbyid(arr, tok);
69 if(sigfound)
70 cptr_array_append(&sigfound->sigfuncs, ref);
71 else
73 signal_t sig = { .id = tok };
74 cptr_array_append(&sig.sigfuncs, ref);
75 signal_array_insert(arr, sig);
79 /** Remove a signal inside a signal array.
80 * You are in charge of reference counting.
81 * \param arr The signal array.
82 * \param name The signal name.
83 * \param ref The reference to remove.
85 static inline void
86 signal_remove(signal_array_t *arr, const char *name, const void *ref)
88 signal_t *sigfound = signal_array_getbyid(arr,
89 a_strhash((const unsigned char *) name));
90 if(sigfound)
91 foreach(func, sigfound->sigfuncs)
92 if(ref == *func)
94 cptr_array_remove(&sigfound->sigfuncs, func);
95 break;
99 #endif
101 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80