source: roaraudio/libroar/notify.c @ 4310:5da1a172157d

Last change on this file since 4310:5da1a172157d was 4310:5da1a172157d, checked in by phi, 14 years ago

allow up to 1024 lists

File size: 6.9 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};
57
58#define _CKRCORE(ret) if ( core == NULL ) { roar_errno = ROAR_ERROR_INVAL; return (ret); }
59#define _CKICORE() _CKRCORE(-1)
60
61static unsigned int _hash_event (struct roar_notify_core * core, struct roar_event * event) {
62 unsigned int hash = 0;
63 unsigned int mask = core->listc - 1;
64
65 hash ^= event->event >>  0;
66 hash ^= event->event >>  8;
67 hash ^= event->event >> 16;
68 hash ^= event->event >> 24;
69
70 return hash & mask;
71}
72
73struct roar_notify_core * roar_notify_core_new(ssize_t lists) {
74 struct roar_notify_core * core = NULL;
75
76 switch (lists) {
77  case    1:
78  case    2:
79  case    4:
80  case    8:
81  case   16:
82  case   32:
83  case   64:
84  case  128:
85  case  256:
86  case  512:
87  case 1024:
88    // they all are ok.
89   break;
90  case  -1:
91    // handle defult:
92    lists = 16;
93   break;
94  default:
95    roar_errno = ROAR_ERROR_INVAL;
96    return NULL;
97   break;
98 }
99
100 if ( (core = roar_mm_malloc(sizeof(struct roar_notify_core))) == NULL ) {
101  return NULL;
102 }
103
104 memset(core, 0, sizeof(struct roar_notify_core));
105
106 core->refc  = 1;
107 core->listc = lists;
108 core->lists = roar_mm_malloc(lists*sizeof(struct roar_subscriber *));
109
110 if ( core->lists == NULL ) {
111  roar_mm_free(core);
112  return NULL;
113 }
114
115 memset(core->lists, 0, lists*sizeof(struct roar_subscriber *));
116
117 return core;
118}
119
120int roar_notify_core_ref(struct roar_notify_core * core) {
121 _CKICORE();
122
123 core->refc++;
124
125 return 0;
126}
127
128int roar_notify_core_unref(struct roar_notify_core * core) {
129 struct roar_subscriber * cur, * next;
130 size_t i;
131
132 _CKICORE();
133
134 if ( core->refc == 0 ) {
135  ROAR_ERR("roar_notify_core_unref(core=%p): refc is zero, must be greater zero. resolving by setting to one.", core);
136  core->refc = 1;
137 }
138
139 core->refc--;
140
141 if ( core->refc > 0 ) {
142  return 0;
143 }
144
145 for (i = 0; i < core->listc; i++) {
146  cur = core->lists[i];
147  while (cur != NULL) {
148   next = cur->next;
149
150   roar_mm_free(cur);
151
152   cur = next;
153  }
154 }
155
156 roar_mm_free(core->lists);
157 roar_mm_free(core);
158 return 0;
159}
160
161struct 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) {
162 struct roar_subscriber * subs = NULL;
163 struct roar_subscriber * cur, * old;
164
165 ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = ?", core, event, cb, userdata);
166
167 _CKRCORE(NULL);
168
169 if ( event == NULL || cb == NULL ) {
170  ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = NULL // errno = EINVAL", core, event, cb, userdata);
171  roar_errno = ROAR_ERROR_INVAL;
172  return NULL;
173 }
174
175 if ( (subs = roar_mm_malloc(sizeof(struct roar_subscriber))) == NULL ) {
176  ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = NULL // errno = ENOMEM?", core, event, cb, userdata);
177  return NULL;
178 }
179
180 memset(subs, 0, sizeof(struct roar_subscriber));
181
182 subs->flags       = 0;
183 subs->event       = event->event;
184 subs->emitter     = event->emitter;
185 subs->target      = event->target;
186 subs->target_type = event->target_type;
187 subs->cb          = cb;
188 subs->userdata    = userdata;
189 subs->refc        = 1;
190 subs->hash        = _hash_event(core, event);
191 subs->next        = NULL;
192
193 if ( (cur = core->lists[subs->hash]) == NULL ) {
194  core->lists[subs->hash] = subs;
195 } else {
196  old = cur;
197  while (cur != NULL) {
198   old = cur;
199   cur = cur->next;
200  }
201
202  old->next = subs;
203 }
204
205 ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = %p", core, event, cb, userdata, subs);
206 return subs;
207}
208
209int roar_notify_core_unsubscribe(struct roar_notify_core * core, struct roar_subscriber * subscriber) {
210 struct roar_subscriber * cur, * old;
211
212 _CKICORE();
213
214 if ( subscriber == NULL ) {
215  roar_errno = ROAR_ERROR_INVAL;
216  return -1;
217 }
218
219 if ( (cur = core->lists[subscriber->hash]) == subscriber ) {
220  core->lists[subscriber->hash] = core->lists[subscriber->hash]->next;
221 } else {
222  old = NULL;
223  while (cur != NULL && cur != subscriber ) {
224   old = cur;
225   cur = cur->next;
226  }
227
228  if ( cur != subscriber )
229   return -1;
230
231  old->next = cur->next;
232  roar_mm_free(cur);
233 }
234
235 return 0;
236}
237
238int roar_notify_core_emit(struct roar_notify_core * core, struct roar_event * event) {
239 struct roar_subscriber * cur;
240
241 _CKICORE();
242
243 if ( event == NULL ) {
244  roar_errno = ROAR_ERROR_INVAL;
245  return -1;
246 }
247
248 cur = core->lists[_hash_event(core, event)];
249 ROAR_DBG("roar_notify_core_emit(core=%p, event=%p): cur=%p", core, event, cur);
250
251 while (cur != NULL) {
252  ROAR_DBG("roar_notify_core_emit(core=%p, event=%p): cur=%p", core, event, cur);
253
254  // test if we can skip this one:
255  if ( !( (cur->event       == ROAR_NOTIFY_SPECIAL || cur->event       == event->event)   &&
256          (cur->emitter     == -1                  || cur->emitter     == event->emitter) &&
257          (cur->target      == -1                  || cur->target      == event->target)  &&
258          (cur->target_type == -1                  || cur->target_type == event->target_type)
259        ) ) {
260   cur = cur->next;
261   continue;
262  }
263
264  if ( cur->cb == NULL ) {
265   ROAR_ERR("roar_notify_core_emit(core=%p, event=%p): cur=%p, cb is set NULL, bad.", core, event, cur);
266  } else {
267   cur->cb(core, event, cur->userdata);
268  }
269  cur = cur->next;
270 }
271
272 return 0;
273}
274
275//ll
Note: See TracBrowser for help on using the repository browser.