Set c.screen in ewmh.tag and before tags in rules.execute
[awesome.git] / common / signal.h
bloba30a9ab81ea625a0c70640b9ca104acbf86d8d1b
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 ARRAY_TYPE_EXTRA(signal_t, signal, struct signal_array_t *inherits_from)
50 BARRAY_FUNCS(signal_t, signal, signal_wipe, signal_cmp)
52 static inline signal_t *
53 signal_array_getbyid(signal_array_t *arr, unsigned long id)
55 signal_t sig = { .id = id };
56 signal_t *result;
58 result = signal_array_lookup(arr, &sig);
59 if(result)
60 return result;
62 /* The signal doesn't exist yet. Check if some of our inherits_from have the
63 * signal and if yes, add it.
65 signal_array_t *inherit = arr->inherits_from;
66 while(inherit != NULL)
68 if(signal_array_lookup(inherit, &sig))
69 break;
70 inherit = inherit->inherits_from;
72 if(inherit)
74 /* Add the signal to this array to pretend it always existed */
75 signal_array_insert(arr, sig);
76 result = signal_array_lookup(arr, &sig);
77 assert(result != NULL);
79 return result;
82 /** Add a signal to a signal array.
83 * Signals have to be added before they can be added or emitted.
84 * \param arr The signal array.
85 * \param name The signal name.
87 static inline void
88 signal_add(signal_array_t *arr, const char *name)
90 unsigned long tok = a_strhash((const unsigned char *) name);
91 signal_t *sigfound = signal_array_getbyid(arr, tok);
92 if(!sigfound)
94 signal_t sig = { .id = tok };
95 signal_array_insert(arr, sig);
99 /** Connect a signal inside a signal array.
100 * You are in charge of reference counting.
101 * \param arr The signal array.
102 * \param name The signal name.
103 * \param ref The reference to add.
105 static inline void
106 signal_connect(signal_array_t *arr, const char *name, const void *ref)
108 unsigned long tok = a_strhash((const unsigned char *) name);
109 signal_t *sigfound = signal_array_getbyid(arr, tok);
110 if(sigfound)
111 cptr_array_append(&sigfound->sigfuncs, ref);
112 else
113 warn("Trying to connect to unknown signal '%s'", name);
116 /** Disconnect a signal inside a signal array.
117 * You are in charge of reference counting.
118 * \param arr The signal array.
119 * \param name The signal name.
120 * \param ref The reference to remove.
122 static inline void
123 signal_disconnect(signal_array_t *arr, const char *name, const void *ref)
125 signal_t *sigfound = signal_array_getbyid(arr,
126 a_strhash((const unsigned char *) name));
127 if(sigfound)
129 foreach(func, sigfound->sigfuncs)
130 if(ref == *func)
132 cptr_array_remove(&sigfound->sigfuncs, func);
133 break;
135 } else
136 warn("Trying to disconnect from unknown signal '%s'", name);
139 #endif
141 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80