source: roaraudio/libroar/notify.c @ 5271:09aa484b25f4

Last change on this file since 5271:09aa484b25f4 was 5151:2f26230719b5, checked in by phi, 13 years ago

use fresh error context for each notify callback.

File size: 13.0 KB
Line 
1//notify.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011
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
60static struct roar_notify_core * _libroar_notify_core = NULL;
61
62#define _CKRCORE(ret) if ( core == NULL ) { if ( _libroar_notify_core == NULL ) { roar_err_set(ROAR_ERROR_INVAL); return (ret); } else { core = _libroar_notify_core; } }
63#define _CKICORE() _CKRCORE(-1)
64
65static 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
77struct 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_err_set(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
126int roar_notify_core_ref(struct roar_notify_core * core) {
127 _CKICORE();
128
129 core->refc++;
130
131 return 0;
132}
133
134int 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  roar_panic(ROAR_FATAL_ERROR_MEMORY_CORRUPTION, NULL);
144 }
145
146 core->refc--;
147
148 if ( core->refc > 0 ) {
149  return 0;
150 }
151
152 // if we work on the global core, reset it to NULL if we free it.
153 if ( core == _libroar_notify_core ) {
154  _libroar_notify_core = NULL;
155 }
156
157 for (i = 0; i < core->listc; i++) {
158  cur = core->lists[i];
159  while (cur != NULL) {
160   next = cur->next;
161
162   roar_mm_free(cur);
163
164   cur = next;
165  }
166 }
167
168 roar_mm_free(core->lists);
169 roar_mm_free(core);
170 return 0;
171}
172
173int roar_notify_core_new_global(ssize_t lists) {
174 if ( _libroar_notify_core != NULL ) {
175  roar_err_set(ROAR_ERROR_INVAL);
176  return -1;
177 }
178
179 if ( (_libroar_notify_core = roar_notify_core_new(lists)) == NULL )
180  return -1;
181
182 return 0;
183}
184
185int 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) {
186 _CKICORE();
187
188 core->proxy = cb;
189 core->proxy_userdata = userdata;
190
191 return 0;
192}
193
194
195struct 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) {
196 struct roar_subscriber * subs = NULL;
197 struct roar_subscriber * cur, * old;
198
199 ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = ?", core, event, cb, userdata);
200
201 _CKRCORE(NULL);
202
203 if ( event == NULL || cb == NULL ) {
204  ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = NULL // errno = EINVAL", core, event, cb, userdata);
205  roar_err_set(ROAR_ERROR_FAULT);
206  return NULL;
207 }
208
209 if ( (subs = roar_mm_malloc(sizeof(struct roar_subscriber))) == NULL ) {
210  ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = NULL // errno = ENOMEM?", core, event, cb, userdata);
211  return NULL;
212 }
213
214 memset(subs, 0, sizeof(struct roar_subscriber));
215
216 subs->flags       = 0;
217 subs->event       = event->event;
218 subs->emitter     = event->emitter;
219 subs->target      = event->target;
220 subs->target_type = event->target_type;
221 subs->cb          = cb;
222 subs->userdata    = userdata;
223 subs->refc        = 1;
224 subs->hash        = _hash_event(core, event);
225 subs->next        = NULL;
226
227 if ( (cur = core->lists[subs->hash]) == NULL ) {
228  core->lists[subs->hash] = subs;
229 } else {
230  old = cur;
231  while (cur != NULL) {
232   old = cur;
233   cur = cur->next;
234  }
235
236  old->next = subs;
237 }
238
239 ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = %p", core, event, cb, userdata, subs);
240 return subs;
241}
242
243int roar_notify_core_unsubscribe(struct roar_notify_core * core, struct roar_subscriber * subscriber) {
244 struct roar_subscriber * cur, * old;
245
246 _CKICORE();
247
248 if ( subscriber == NULL ) {
249  roar_err_set(ROAR_ERROR_FAULT);
250  return -1;
251 }
252
253 if ( (cur = core->lists[subscriber->hash]) == subscriber ) {
254  core->lists[subscriber->hash] = core->lists[subscriber->hash]->next;
255 } else {
256  old = NULL;
257  while (cur != NULL && cur != subscriber ) {
258   old = cur;
259   cur = cur->next;
260  }
261
262  if ( cur != subscriber )
263   return -1;
264
265  old->next = cur->next;
266  roar_mm_free(cur);
267 }
268
269 return 0;
270}
271
272int roar_notify_core_emit(struct roar_notify_core * core, struct roar_event * event) {
273 struct roar_subscriber * cur;
274 struct roar_error_state errstate;
275
276 _CKICORE();
277
278 if ( event == NULL ) {
279  roar_err_set(ROAR_ERROR_FAULT);
280  return -1;
281 }
282
283 if ( core->proxy != NULL && !(event->flags & ROAR_EVENT_FLAG_PROXYEVENT) ) {
284  core->proxy(core, event, core->proxy_userdata);
285 }
286
287 cur = core->lists[_hash_event(core, event)];
288 ROAR_DBG("roar_notify_core_emit(core=%p, event=%p): cur=%p", core, event, cur);
289
290 while (cur != NULL) {
291  ROAR_DBG("roar_notify_core_emit(core=%p, event=%p): cur=%p", core, event, cur);
292
293  // test if we can skip this one:
294  if ( !( (cur->event       == ROAR_NOTIFY_SPECIAL || cur->event       == event->event)   &&
295          (cur->emitter     == -1                  || cur->emitter     == event->emitter) &&
296          (cur->target      == -1                  || cur->target      == event->target)  &&
297          (cur->target_type == -1                  || cur->target_type == event->target_type)
298        ) ) {
299   cur = cur->next;
300   continue;
301  }
302
303  if ( cur->cb == NULL ) {
304   ROAR_ERR("roar_notify_core_emit(core=%p, event=%p): cur=%p, cb is set NULL, bad.", core, event, cur);
305  } else {
306   // The callback is started in an clear error context which is destroyed after it finished so it
307   // does not alter our caller's context.
308   roar_err_store(&errstate);
309   roar_err_clear_all();
310
311   cur->cb(core, event, cur->userdata);
312
313   roar_err_restore(&errstate);
314  }
315  cur = cur->next;
316 }
317
318 return 0;
319}
320
321int 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) {
322 struct roar_event locevent;
323
324 memset(&locevent, 0, sizeof(locevent));
325
326 locevent.event = event;
327 locevent.emitter = emitter;
328 locevent.target = target;
329 locevent.target_type = target_type;
330 locevent.arg0 = arg0;
331 locevent.arg1 = arg1;
332 locevent.arg2 = arg2;
333 locevent.arg2_len = arg2_len;
334
335 if ( arg2 == NULL && (arg2_len == 0 || arg2_len == -1) ) {
336  locevent.flags |= ROAR_EVENT_FLAG_NETTRANS; // we guss packages with only int args are network transparent.
337 }
338
339 return roar_notify_core_emit(NULL, &locevent);
340}
341
342
343int roar_event_to_blob(struct roar_event * event, void * blob, size_t * len) {
344 size_t needed_len = (2 * 4) + (4 * 2);
345 uint32_t * u32 = blob;
346 uint16_t * u16 = blob;
347 size_t data_offset_neg = 0;
348
349 u16 += 4;
350
351 // error checking:
352 if ( event == NULL || blob == NULL || len == NULL )
353  return -1;
354
355 if ( *len == 0 )
356  return -1;
357
358 // calc and check length:
359 if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT )
360  needed_len += 4;
361
362 if ( event->flags & ROAR_EVENT_FLAG_NETTRANS ) {
363
364  // this is not a real maximum but a value good to detect all kinds of errors.
365  // a notify event should not be longer anyway.
366  if ( event->arg2_len > 32767 )
367   return -1;
368
369  needed_len += 4;
370  if ( event->arg2_len > 0 ) {
371    needed_len += event->arg2_len;
372    data_offset_neg = event->arg2_len;
373  }
374 }
375
376 if ( *len < needed_len )
377  return -1;
378
379 *len = 0;
380
381 // fill in the data...
382 memset(blob, 0, needed_len);
383
384 u32[1] = ROAR_HOST2NET32(0);
385
386 if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT ) {
387  u32[0] |= ROAR_HOST2NET32(ROAR_EVENT_NETFLAG_PROXYEVENT);
388  u16 += 2;
389 }
390
391 if ( event->flags & ROAR_EVENT_FLAG_NETTRANS ) {
392  u32[0] |= ROAR_HOST2NET32(ROAR_EVENT_NETFLAG_DATA);
393 }
394
395 u32[1] = ROAR_HOST2NET32(event->event);
396
397 if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT )
398  u32[2] = ROAR_HOST2NET32(event->event_proxy);
399
400 u16[0] = ROAR_HOST2NET16(event->emitter);
401 u16[1] = ROAR_HOST2NET16(event->target);
402 u16[2] = ROAR_HOST2NET16(event->target_type);
403 if ( event->flags & ROAR_EVENT_FLAG_NETTRANS ) {
404  u16[3] = ROAR_HOST2NET16(event->arg2_len);
405  u16[4] = ROAR_HOST2NET16(event->arg0);
406  u16[5] = ROAR_HOST2NET16(event->arg1);
407 } else {
408  u16[3] = ROAR_HOST2NET16(0);
409 }
410
411 memcpy(blob + needed_len - data_offset_neg, event->arg2, event->arg2_len);
412
413 *len = needed_len;
414
415 return 0;
416}
417
418int roar_event_from_blob(struct roar_event * event, void * blob, size_t * len) {
419 size_t needed_len = (2 * 4) + (4 * 2);
420 uint32_t * u32 = blob;
421 uint16_t * u16 = blob;
422 uint32_t flags;
423
424 u16 += 4;
425
426 // error checking:
427 if ( event == NULL || blob == NULL || len == NULL )
428  return -1;
429
430 // check for minimum length.
431 if ( *len < needed_len )
432  return -1;
433
434 flags = ROAR_NET2HOST32(u32[0]);
435
436#if 0
437 if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT ) {
438  u32[0] |= ROAR_HOST2NET32(ROAR_EVENT_NETFLAG_PROXYEVENT);
439  u16 += 2;
440 }
441
442 if ( event->flags & ROAR_EVENT_FLAG_NETTRANS ) {
443  u32[0] |= ROAR_HOST2NET32(ROAR_EVENT_NETFLAG_DATA);
444 }
445#endif
446
447 if ( flags & ROAR_EVENT_NETFLAG_PROXYEVENT ) {
448  needed_len += 4;
449  u16 += 2;
450 }
451
452 if ( flags & ROAR_EVENT_NETFLAG_DATA ) {
453  needed_len += 4;
454 }
455
456 // do we have a full header?
457 if ( *len < needed_len )
458  return -1;
459
460 needed_len += ROAR_NET2HOST16(u16[3]);
461
462 // is all of the event complet?
463 if ( *len < needed_len )
464  return -1;
465
466 // now we know everything is complet we can start to extract data...
467
468 *len = 0;
469
470 memset(event, 0, sizeof(struct roar_event));
471
472 if ( flags & ROAR_EVENT_NETFLAG_PROXYEVENT ) {
473  flags -= ROAR_EVENT_NETFLAG_PROXYEVENT;
474  event->flags |= ROAR_EVENT_FLAG_PROXYEVENT;
475 }
476
477 if ( flags & ROAR_EVENT_NETFLAG_DATA ) {
478  flags -= ROAR_EVENT_NETFLAG_DATA;
479  event->flags |= ROAR_EVENT_FLAG_NETTRANS;
480 }
481
482 // test if there are flags left we do not understand:
483 if ( flags ) {
484  return -1;
485 }
486
487 event->event = ROAR_NET2HOST32(u32[1]);
488
489 if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT )
490  event->event_proxy = ROAR_NET2HOST32(u32[2]);
491
492 event->emitter     = ROAR_NET2HOST16(u16[0]);
493 event->target      = ROAR_NET2HOST16(u16[1]);
494 event->target_type = ROAR_NET2HOST16(u16[2]);
495 event->arg2_len    = ROAR_NET2HOST16(u16[3]);
496
497 if ( event->emitter == (uint16_t)-1)
498  event->emitter = -1;
499
500 if ( event->target == (uint16_t)-1)
501  event->target = -1;
502
503 if ( event->target_type == (uint16_t)-1)
504  event->target_type = -1;
505
506 if ( event->flags & ROAR_EVENT_FLAG_NETTRANS ) {
507  event->arg0 = ROAR_NET2HOST16(u16[4]);
508  event->arg1 = ROAR_NET2HOST16(u16[5]);
509
510  if ( event->arg0 == (uint16_t)-1)
511   event->arg0 = -1;
512
513  if ( event->arg1 == (uint16_t)-1)
514   event->arg1 = -1;
515
516  event->arg2 = blob + needed_len - event->arg2_len;
517 } else {
518  event->arg0 = -1;
519  event->arg1 = -1;
520  event->arg2 = NULL;
521 }
522
523 *len = needed_len;
524
525 return 0;
526}
527
528
529//ll
Note: See TracBrowser for help on using the repository browser.