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 | |
---|
38 | struct 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 | |
---|
52 | struct 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 | static struct roar_notify_core * _libroar_notify_core = NULL; |
---|
61 | |
---|
62 | #define _CKRCORE(ret) if ( core == NULL ) { if ( _libroar_notify_core == NULL ) { roar_errno = ROAR_ERROR_INVAL; return (ret); } else { core = _libroar_notify_core; } } |
---|
63 | #define _CKICORE() _CKRCORE(-1) |
---|
64 | |
---|
65 | static unsigned int _hash_event (struct roar_notify_core * core, struct roar_event * event) { |
---|
66 | unsigned int hash = 0; |
---|
67 | unsigned int mask = core->listc - 1; |
---|
68 | |
---|
69 | hash ^= event->event >> 0; |
---|
70 | hash ^= event->event >> 8; |
---|
71 | hash ^= event->event >> 16; |
---|
72 | hash ^= event->event >> 24; |
---|
73 | |
---|
74 | return hash & mask; |
---|
75 | } |
---|
76 | |
---|
77 | struct roar_notify_core * roar_notify_core_new(ssize_t lists) { |
---|
78 | struct roar_notify_core * core = NULL; |
---|
79 | |
---|
80 | switch (lists) { |
---|
81 | case 1: |
---|
82 | case 2: |
---|
83 | case 4: |
---|
84 | case 8: |
---|
85 | case 16: |
---|
86 | case 32: |
---|
87 | case 64: |
---|
88 | case 128: |
---|
89 | case 256: |
---|
90 | case 512: |
---|
91 | case 1024: |
---|
92 | // they all are ok. |
---|
93 | break; |
---|
94 | case -1: |
---|
95 | // handle defult: |
---|
96 | lists = 16; |
---|
97 | break; |
---|
98 | default: |
---|
99 | roar_errno = ROAR_ERROR_INVAL; |
---|
100 | return NULL; |
---|
101 | break; |
---|
102 | } |
---|
103 | |
---|
104 | if ( (core = roar_mm_malloc(sizeof(struct roar_notify_core))) == NULL ) { |
---|
105 | return NULL; |
---|
106 | } |
---|
107 | |
---|
108 | memset(core, 0, sizeof(struct roar_notify_core)); |
---|
109 | |
---|
110 | core->refc = 1; |
---|
111 | core->listc = lists; |
---|
112 | core->lists = roar_mm_malloc(lists*sizeof(struct roar_subscriber *)); |
---|
113 | core->proxy = NULL; |
---|
114 | core->proxy_userdata = NULL; |
---|
115 | |
---|
116 | if ( core->lists == NULL ) { |
---|
117 | roar_mm_free(core); |
---|
118 | return NULL; |
---|
119 | } |
---|
120 | |
---|
121 | memset(core->lists, 0, lists*sizeof(struct roar_subscriber *)); |
---|
122 | |
---|
123 | return core; |
---|
124 | } |
---|
125 | |
---|
126 | int roar_notify_core_ref(struct roar_notify_core * core) { |
---|
127 | _CKICORE(); |
---|
128 | |
---|
129 | core->refc++; |
---|
130 | |
---|
131 | return 0; |
---|
132 | } |
---|
133 | |
---|
134 | int roar_notify_core_unref(struct roar_notify_core * core) { |
---|
135 | struct roar_subscriber * cur, * next; |
---|
136 | size_t i; |
---|
137 | |
---|
138 | _CKICORE(); |
---|
139 | |
---|
140 | if ( core->refc == 0 ) { |
---|
141 | ROAR_ERR("roar_notify_core_unref(core=%p): refc is zero, must be greater zero. resolving by setting to one.", core); |
---|
142 | core->refc = 1; |
---|
143 | } |
---|
144 | |
---|
145 | core->refc--; |
---|
146 | |
---|
147 | if ( core->refc > 0 ) { |
---|
148 | return 0; |
---|
149 | } |
---|
150 | |
---|
151 | // if we work on the global core, reset it to NULL if we free it. |
---|
152 | if ( core == _libroar_notify_core ) { |
---|
153 | _libroar_notify_core = NULL; |
---|
154 | } |
---|
155 | |
---|
156 | for (i = 0; i < core->listc; i++) { |
---|
157 | cur = core->lists[i]; |
---|
158 | while (cur != NULL) { |
---|
159 | next = cur->next; |
---|
160 | |
---|
161 | roar_mm_free(cur); |
---|
162 | |
---|
163 | cur = next; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | roar_mm_free(core->lists); |
---|
168 | roar_mm_free(core); |
---|
169 | return 0; |
---|
170 | } |
---|
171 | |
---|
172 | int roar_notify_core_new_global(ssize_t lists) { |
---|
173 | if ( _libroar_notify_core != NULL ) { |
---|
174 | roar_errno = ROAR_ERROR_INVAL; |
---|
175 | return -1; |
---|
176 | } |
---|
177 | |
---|
178 | if ( (_libroar_notify_core = roar_notify_core_new(lists)) == NULL ) |
---|
179 | return -1; |
---|
180 | |
---|
181 | return 0; |
---|
182 | } |
---|
183 | |
---|
184 | int 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) { |
---|
185 | _CKICORE(); |
---|
186 | |
---|
187 | core->proxy = cb; |
---|
188 | core->proxy_userdata = userdata; |
---|
189 | |
---|
190 | return 0; |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | struct 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) { |
---|
195 | struct roar_subscriber * subs = NULL; |
---|
196 | struct roar_subscriber * cur, * old; |
---|
197 | |
---|
198 | ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = ?", core, event, cb, userdata); |
---|
199 | |
---|
200 | _CKRCORE(NULL); |
---|
201 | |
---|
202 | if ( event == NULL || cb == NULL ) { |
---|
203 | ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = NULL // errno = EINVAL", core, event, cb, userdata); |
---|
204 | roar_errno = ROAR_ERROR_INVAL; |
---|
205 | return NULL; |
---|
206 | } |
---|
207 | |
---|
208 | if ( (subs = roar_mm_malloc(sizeof(struct roar_subscriber))) == NULL ) { |
---|
209 | ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = NULL // errno = ENOMEM?", core, event, cb, userdata); |
---|
210 | return NULL; |
---|
211 | } |
---|
212 | |
---|
213 | memset(subs, 0, sizeof(struct roar_subscriber)); |
---|
214 | |
---|
215 | subs->flags = 0; |
---|
216 | subs->event = event->event; |
---|
217 | subs->emitter = event->emitter; |
---|
218 | subs->target = event->target; |
---|
219 | subs->target_type = event->target_type; |
---|
220 | subs->cb = cb; |
---|
221 | subs->userdata = userdata; |
---|
222 | subs->refc = 1; |
---|
223 | subs->hash = _hash_event(core, event); |
---|
224 | subs->next = NULL; |
---|
225 | |
---|
226 | if ( (cur = core->lists[subs->hash]) == NULL ) { |
---|
227 | core->lists[subs->hash] = subs; |
---|
228 | } else { |
---|
229 | old = cur; |
---|
230 | while (cur != NULL) { |
---|
231 | old = cur; |
---|
232 | cur = cur->next; |
---|
233 | } |
---|
234 | |
---|
235 | old->next = subs; |
---|
236 | } |
---|
237 | |
---|
238 | ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = %p", core, event, cb, userdata, subs); |
---|
239 | return subs; |
---|
240 | } |
---|
241 | |
---|
242 | int roar_notify_core_unsubscribe(struct roar_notify_core * core, struct roar_subscriber * subscriber) { |
---|
243 | struct roar_subscriber * cur, * old; |
---|
244 | |
---|
245 | _CKICORE(); |
---|
246 | |
---|
247 | if ( subscriber == NULL ) { |
---|
248 | roar_errno = ROAR_ERROR_INVAL; |
---|
249 | return -1; |
---|
250 | } |
---|
251 | |
---|
252 | if ( (cur = core->lists[subscriber->hash]) == subscriber ) { |
---|
253 | core->lists[subscriber->hash] = core->lists[subscriber->hash]->next; |
---|
254 | } else { |
---|
255 | old = NULL; |
---|
256 | while (cur != NULL && cur != subscriber ) { |
---|
257 | old = cur; |
---|
258 | cur = cur->next; |
---|
259 | } |
---|
260 | |
---|
261 | if ( cur != subscriber ) |
---|
262 | return -1; |
---|
263 | |
---|
264 | old->next = cur->next; |
---|
265 | roar_mm_free(cur); |
---|
266 | } |
---|
267 | |
---|
268 | return 0; |
---|
269 | } |
---|
270 | |
---|
271 | int roar_notify_core_emit(struct roar_notify_core * core, struct roar_event * event) { |
---|
272 | struct roar_subscriber * cur; |
---|
273 | |
---|
274 | _CKICORE(); |
---|
275 | |
---|
276 | if ( event == NULL ) { |
---|
277 | roar_errno = ROAR_ERROR_INVAL; |
---|
278 | return -1; |
---|
279 | } |
---|
280 | |
---|
281 | if ( core->proxy != NULL && !(event->flags & ROAR_EVENT_FLAG_PROXYEVENT) ) { |
---|
282 | core->proxy(core, event, core->proxy_userdata); |
---|
283 | } |
---|
284 | |
---|
285 | cur = core->lists[_hash_event(core, event)]; |
---|
286 | ROAR_DBG("roar_notify_core_emit(core=%p, event=%p): cur=%p", core, event, cur); |
---|
287 | |
---|
288 | while (cur != NULL) { |
---|
289 | ROAR_DBG("roar_notify_core_emit(core=%p, event=%p): cur=%p", core, event, cur); |
---|
290 | |
---|
291 | // test if we can skip this one: |
---|
292 | if ( !( (cur->event == ROAR_NOTIFY_SPECIAL || cur->event == event->event) && |
---|
293 | (cur->emitter == -1 || cur->emitter == event->emitter) && |
---|
294 | (cur->target == -1 || cur->target == event->target) && |
---|
295 | (cur->target_type == -1 || cur->target_type == event->target_type) |
---|
296 | ) ) { |
---|
297 | cur = cur->next; |
---|
298 | continue; |
---|
299 | } |
---|
300 | |
---|
301 | if ( cur->cb == NULL ) { |
---|
302 | ROAR_ERR("roar_notify_core_emit(core=%p, event=%p): cur=%p, cb is set NULL, bad.", core, event, cur); |
---|
303 | } else { |
---|
304 | cur->cb(core, event, cur->userdata); |
---|
305 | } |
---|
306 | cur = cur->next; |
---|
307 | } |
---|
308 | |
---|
309 | return 0; |
---|
310 | } |
---|
311 | |
---|
312 | int roar_notify_core_emit_simple(uint32_t event, int emitter, int target, int target_type, int arg0, int arg1, void * arg2, ssize_t arg2_len) { |
---|
313 | struct roar_event locevent; |
---|
314 | |
---|
315 | memset(&locevent, 0, sizeof(locevent)); |
---|
316 | |
---|
317 | locevent.event = event; |
---|
318 | locevent.emitter = emitter; |
---|
319 | locevent.target = target; |
---|
320 | locevent.target_type = target_type; |
---|
321 | locevent.arg0 = arg0; |
---|
322 | locevent.arg1 = arg1; |
---|
323 | locevent.arg2 = arg2; |
---|
324 | locevent.arg2_len = arg2_len; |
---|
325 | |
---|
326 | if ( arg2 == NULL && (arg2_len == 0 || arg2_len == -1) ) { |
---|
327 | locevent.flags |= ROAR_EVENT_FLAG_NETTRANS; // we guss packages with only int args are network transparent. |
---|
328 | } |
---|
329 | |
---|
330 | return roar_notify_core_emit(NULL, &locevent); |
---|
331 | } |
---|
332 | |
---|
333 | |
---|
334 | int roar_event_to_blob(struct roar_event * event, void * blob, size_t * len) { |
---|
335 | size_t needed_len = (2 * 4) + (4 * 2); |
---|
336 | uint32_t * u32 = blob; |
---|
337 | uint16_t * u16 = blob; |
---|
338 | size_t data_offset_neg = 0; |
---|
339 | |
---|
340 | u16 += 4; |
---|
341 | |
---|
342 | // error checking: |
---|
343 | if ( event == NULL || blob == NULL || len == NULL ) |
---|
344 | return -1; |
---|
345 | |
---|
346 | if ( *len == 0 ) |
---|
347 | return -1; |
---|
348 | |
---|
349 | // calc and check length: |
---|
350 | if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT ) |
---|
351 | needed_len += 4; |
---|
352 | |
---|
353 | if ( event->flags & ROAR_EVENT_FLAG_NETTRANS ) { |
---|
354 | |
---|
355 | // this is not a real maximum but a value good to detect all kinds of errors. |
---|
356 | // a notify event should not be longer anyway. |
---|
357 | if ( event->arg2_len > 32768 ) |
---|
358 | return -1; |
---|
359 | |
---|
360 | needed_len += 4; |
---|
361 | if ( event->arg2_len > 0 ) { |
---|
362 | needed_len += event->arg2_len; |
---|
363 | data_offset_neg = event->arg2_len; |
---|
364 | } |
---|
365 | } |
---|
366 | |
---|
367 | if ( *len < needed_len ) |
---|
368 | return -1; |
---|
369 | |
---|
370 | *len = 0; |
---|
371 | |
---|
372 | // fill in the data... |
---|
373 | memset(blob, 0, needed_len); |
---|
374 | |
---|
375 | u32[1] = ROAR_HOST2NET32(0); |
---|
376 | |
---|
377 | if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT ) { |
---|
378 | u32[0] |= ROAR_HOST2NET32(ROAR_EVENT_NETFLAG_PROXYEVENT); |
---|
379 | u16 += 2; |
---|
380 | } |
---|
381 | |
---|
382 | if ( event->flags & ROAR_EVENT_FLAG_NETTRANS ) { |
---|
383 | u32[0] |= ROAR_HOST2NET32(ROAR_EVENT_NETFLAG_DATA); |
---|
384 | } |
---|
385 | |
---|
386 | u32[1] = ROAR_HOST2NET32(event->event); |
---|
387 | |
---|
388 | if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT ) |
---|
389 | u32[2] = ROAR_HOST2NET32(event->event_proxy); |
---|
390 | |
---|
391 | u16[0] = ROAR_HOST2NET16(event->emitter); |
---|
392 | u16[1] = ROAR_HOST2NET16(event->target); |
---|
393 | u16[2] = ROAR_HOST2NET16(event->target_type); |
---|
394 | if ( event->flags & ROAR_EVENT_FLAG_NETTRANS ) { |
---|
395 | u16[3] = ROAR_HOST2NET16(event->arg2_len); |
---|
396 | u16[4] = ROAR_HOST2NET16(event->arg0); |
---|
397 | u16[5] = ROAR_HOST2NET16(event->arg1); |
---|
398 | } else { |
---|
399 | u16[3] = ROAR_HOST2NET16(0); |
---|
400 | } |
---|
401 | |
---|
402 | memcpy(blob + needed_len - data_offset_neg, event->arg2, event->arg2_len); |
---|
403 | |
---|
404 | *len = needed_len; |
---|
405 | |
---|
406 | return 0; |
---|
407 | } |
---|
408 | |
---|
409 | int roar_event_from_blob(struct roar_event * event, void * blob, size_t * len) { |
---|
410 | size_t needed_len = (2 * 4) + (4 * 2); |
---|
411 | uint32_t * u32 = blob; |
---|
412 | uint16_t * u16 = blob; |
---|
413 | uint32_t flags; |
---|
414 | |
---|
415 | u16 += 4; |
---|
416 | |
---|
417 | // error checking: |
---|
418 | if ( event == NULL || blob == NULL || len == NULL ) |
---|
419 | return -1; |
---|
420 | |
---|
421 | // check for minimum length. |
---|
422 | if ( *len < needed_len ) |
---|
423 | return -1; |
---|
424 | |
---|
425 | flags = ROAR_NET2HOST32(u32[0]); |
---|
426 | |
---|
427 | #if 0 |
---|
428 | if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT ) { |
---|
429 | u32[0] |= ROAR_HOST2NET32(ROAR_EVENT_NETFLAG_PROXYEVENT); |
---|
430 | u16 += 2; |
---|
431 | } |
---|
432 | |
---|
433 | if ( event->flags & ROAR_EVENT_FLAG_NETTRANS ) { |
---|
434 | u32[0] |= ROAR_HOST2NET32(ROAR_EVENT_NETFLAG_DATA); |
---|
435 | } |
---|
436 | #endif |
---|
437 | |
---|
438 | if ( flags & ROAR_EVENT_NETFLAG_PROXYEVENT ) { |
---|
439 | needed_len += 4; |
---|
440 | u16 += 2; |
---|
441 | } |
---|
442 | |
---|
443 | if ( flags & ROAR_EVENT_NETFLAG_DATA ) { |
---|
444 | needed_len += 4; |
---|
445 | } |
---|
446 | |
---|
447 | // do we have a full header? |
---|
448 | if ( *len < needed_len ) |
---|
449 | return -1; |
---|
450 | |
---|
451 | needed_len += ROAR_NET2HOST16(u16[3]); |
---|
452 | |
---|
453 | // is all of the event complet? |
---|
454 | if ( *len < needed_len ) |
---|
455 | return -1; |
---|
456 | |
---|
457 | // now we know everything is complet we can start to extract data... |
---|
458 | |
---|
459 | *len = 0; |
---|
460 | |
---|
461 | memset(event, 0, sizeof(struct roar_event)); |
---|
462 | |
---|
463 | if ( flags & ROAR_EVENT_NETFLAG_PROXYEVENT ) { |
---|
464 | flags -= ROAR_EVENT_NETFLAG_PROXYEVENT; |
---|
465 | event->flags |= ROAR_EVENT_FLAG_PROXYEVENT; |
---|
466 | } |
---|
467 | |
---|
468 | if ( flags & ROAR_EVENT_NETFLAG_DATA ) { |
---|
469 | flags -= ROAR_EVENT_NETFLAG_DATA; |
---|
470 | event->flags |= ROAR_EVENT_FLAG_NETTRANS; |
---|
471 | } |
---|
472 | |
---|
473 | // test if there are flags left we do not understand: |
---|
474 | if ( flags ) { |
---|
475 | return -1; |
---|
476 | } |
---|
477 | |
---|
478 | event->event = ROAR_NET2HOST32(u32[1]); |
---|
479 | |
---|
480 | if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT ) |
---|
481 | event->event_proxy = ROAR_NET2HOST32(u32[2]); |
---|
482 | |
---|
483 | event->emitter = ROAR_NET2HOST16(u16[0]); |
---|
484 | event->target = ROAR_NET2HOST16(u16[1]); |
---|
485 | event->target_type = ROAR_NET2HOST16(u16[2]); |
---|
486 | event->arg2_len = ROAR_NET2HOST16(u16[3]); |
---|
487 | |
---|
488 | if ( event->emitter == (uint16_t)-1) |
---|
489 | event->emitter = -1; |
---|
490 | |
---|
491 | if ( event->target == (uint16_t)-1) |
---|
492 | event->target = -1; |
---|
493 | |
---|
494 | if ( event->target_type == (uint16_t)-1) |
---|
495 | event->target_type = -1; |
---|
496 | |
---|
497 | if ( event->flags & ROAR_EVENT_FLAG_NETTRANS ) { |
---|
498 | event->arg0 = ROAR_NET2HOST16(u16[4]); |
---|
499 | event->arg1 = ROAR_NET2HOST16(u16[5]); |
---|
500 | |
---|
501 | if ( event->arg0 == (uint16_t)-1) |
---|
502 | event->arg0 = -1; |
---|
503 | |
---|
504 | if ( event->arg1 == (uint16_t)-1) |
---|
505 | event->arg1 = -1; |
---|
506 | |
---|
507 | event->arg2 = blob + needed_len - event->arg2_len; |
---|
508 | } else { |
---|
509 | event->arg0 = -1; |
---|
510 | event->arg1 = -1; |
---|
511 | event->arg2 = NULL; |
---|
512 | } |
---|
513 | |
---|
514 | *len = needed_len; |
---|
515 | |
---|
516 | return 0; |
---|
517 | } |
---|
518 | |
---|
519 | |
---|
520 | //ll |
---|