source: roaraudio/libroar/notify.c @ 4316:f3729165e570

Last change on this file since 4316:f3729165e570 was 4316:f3729165e570, checked in by phi, 14 years ago

only emit to proxy if not a proxy event

File size: 7.5 KB
Line 
1//notify.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38struct roar_subscriber {
39 uint32_t flags;
40 uint32_t event;
41 int emitter;
42 int target;
43 int target_type;
44 void (*cb)(struct roar_notify_core * core, struct roar_event * event, void * userdata);
45 void * userdata;
46 size_t refc;
47 unsigned int hash;
48// struct roar_subscriber * prev;
49 struct roar_subscriber * next;
50};
51
52struct roar_notify_core {
53 size_t refc;
54 size_t listc;
55 struct roar_subscriber ** lists;
56 void (*proxy)(struct roar_notify_core * core, struct roar_event * event, void * userdata);
57 void * proxy_userdata;
58};
59
60#define _CKRCORE(ret) if ( core == NULL ) { roar_errno = ROAR_ERROR_INVAL; return (ret); }
61#define _CKICORE() _CKRCORE(-1)
62
63static unsigned int _hash_event (struct roar_notify_core * core, struct roar_event * event) {
64 unsigned int hash = 0;
65 unsigned int mask = core->listc - 1;
66
67 hash ^= event->event >>  0;
68 hash ^= event->event >>  8;
69 hash ^= event->event >> 16;
70 hash ^= event->event >> 24;
71
72 return hash & mask;
73}
74
75struct roar_notify_core * roar_notify_core_new(ssize_t lists) {
76 struct roar_notify_core * core = NULL;
77
78 switch (lists) {
79  case    1:
80  case    2:
81  case    4:
82  case    8:
83  case   16:
84  case   32:
85  case   64:
86  case  128:
87  case  256:
88  case  512:
89  case 1024:
90    // they all are ok.
91   break;
92  case  -1:
93    // handle defult:
94    lists = 16;
95   break;
96  default:
97    roar_errno = ROAR_ERROR_INVAL;
98    return NULL;
99   break;
100 }
101
102 if ( (core = roar_mm_malloc(sizeof(struct roar_notify_core))) == NULL ) {
103  return NULL;
104 }
105
106 memset(core, 0, sizeof(struct roar_notify_core));
107
108 core->refc  = 1;
109 core->listc = lists;
110 core->lists = roar_mm_malloc(lists*sizeof(struct roar_subscriber *));
111 core->proxy = NULL;
112 core->proxy_userdata = NULL;
113
114 if ( core->lists == NULL ) {
115  roar_mm_free(core);
116  return NULL;
117 }
118
119 memset(core->lists, 0, lists*sizeof(struct roar_subscriber *));
120
121 return core;
122}
123
124int roar_notify_core_ref(struct roar_notify_core * core) {
125 _CKICORE();
126
127 core->refc++;
128
129 return 0;
130}
131
132int roar_notify_core_unref(struct roar_notify_core * core) {
133 struct roar_subscriber * cur, * next;
134 size_t i;
135
136 _CKICORE();
137
138 if ( core->refc == 0 ) {
139  ROAR_ERR("roar_notify_core_unref(core=%p): refc is zero, must be greater zero. resolving by setting to one.", core);
140  core->refc = 1;
141 }
142
143 core->refc--;
144
145 if ( core->refc > 0 ) {
146  return 0;
147 }
148
149 for (i = 0; i < core->listc; i++) {
150  cur = core->lists[i];
151  while (cur != NULL) {
152   next = cur->next;
153
154   roar_mm_free(cur);
155
156   cur = next;
157  }
158 }
159
160 roar_mm_free(core->lists);
161 roar_mm_free(core);
162 return 0;
163}
164
165int roar_notify_core_register_proxy(struct roar_notify_core * core, void (*cb)(struct roar_notify_core * core, struct roar_event * event, void * userdata), void * userdata) {
166 _CKICORE();
167
168 core->proxy = cb;
169 core->proxy_userdata = userdata;
170
171 return 0;
172}
173
174
175struct roar_subscriber * roar_notify_core_subscribe(struct roar_notify_core * core, struct roar_event * event, void (*cb)(struct roar_notify_core * core, struct roar_event * event, void * userdata), void * userdata) {
176 struct roar_subscriber * subs = NULL;
177 struct roar_subscriber * cur, * old;
178
179 ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = ?", core, event, cb, userdata);
180
181 _CKRCORE(NULL);
182
183 if ( event == NULL || cb == NULL ) {
184  ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = NULL // errno = EINVAL", core, event, cb, userdata);
185  roar_errno = ROAR_ERROR_INVAL;
186  return NULL;
187 }
188
189 if ( (subs = roar_mm_malloc(sizeof(struct roar_subscriber))) == NULL ) {
190  ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = NULL // errno = ENOMEM?", core, event, cb, userdata);
191  return NULL;
192 }
193
194 memset(subs, 0, sizeof(struct roar_subscriber));
195
196 subs->flags       = 0;
197 subs->event       = event->event;
198 subs->emitter     = event->emitter;
199 subs->target      = event->target;
200 subs->target_type = event->target_type;
201 subs->cb          = cb;
202 subs->userdata    = userdata;
203 subs->refc        = 1;
204 subs->hash        = _hash_event(core, event);
205 subs->next        = NULL;
206
207 if ( (cur = core->lists[subs->hash]) == NULL ) {
208  core->lists[subs->hash] = subs;
209 } else {
210  old = cur;
211  while (cur != NULL) {
212   old = cur;
213   cur = cur->next;
214  }
215
216  old->next = subs;
217 }
218
219 ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = %p", core, event, cb, userdata, subs);
220 return subs;
221}
222
223int roar_notify_core_unsubscribe(struct roar_notify_core * core, struct roar_subscriber * subscriber) {
224 struct roar_subscriber * cur, * old;
225
226 _CKICORE();
227
228 if ( subscriber == NULL ) {
229  roar_errno = ROAR_ERROR_INVAL;
230  return -1;
231 }
232
233 if ( (cur = core->lists[subscriber->hash]) == subscriber ) {
234  core->lists[subscriber->hash] = core->lists[subscriber->hash]->next;
235 } else {
236  old = NULL;
237  while (cur != NULL && cur != subscriber ) {
238   old = cur;
239   cur = cur->next;
240  }
241
242  if ( cur != subscriber )
243   return -1;
244
245  old->next = cur->next;
246  roar_mm_free(cur);
247 }
248
249 return 0;
250}
251
252int roar_notify_core_emit(struct roar_notify_core * core, struct roar_event * event) {
253 struct roar_subscriber * cur;
254
255 _CKICORE();
256
257 if ( event == NULL ) {
258  roar_errno = ROAR_ERROR_INVAL;
259  return -1;
260 }
261
262 if ( core->proxy != NULL && !(event->flags & ROAR_EVENT_FLAG_PROXYEVENT) ) {
263  core->proxy(core, event, core->proxy_userdata);
264 }
265
266 cur = core->lists[_hash_event(core, event)];
267 ROAR_DBG("roar_notify_core_emit(core=%p, event=%p): cur=%p", core, event, cur);
268
269 while (cur != NULL) {
270  ROAR_DBG("roar_notify_core_emit(core=%p, event=%p): cur=%p", core, event, cur);
271
272  // test if we can skip this one:
273  if ( !( (cur->event       == ROAR_NOTIFY_SPECIAL || cur->event       == event->event)   &&
274          (cur->emitter     == -1                  || cur->emitter     == event->emitter) &&
275          (cur->target      == -1                  || cur->target      == event->target)  &&
276          (cur->target_type == -1                  || cur->target_type == event->target_type)
277        ) ) {
278   cur = cur->next;
279   continue;
280  }
281
282  if ( cur->cb == NULL ) {
283   ROAR_ERR("roar_notify_core_emit(core=%p, event=%p): cur=%p, cb is set NULL, bad.", core, event, cur);
284  } else {
285   cur->cb(core, event, cur->userdata);
286  }
287  cur = cur->next;
288 }
289
290 return 0;
291}
292
293//ll
Note: See TracBrowser for help on using the repository browser.