[4302] | 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 | |
---|
[4304] | 38 | struct roar_subscriber { |
---|
| 39 | uint32_t flags; |
---|
| 40 | uint32_t event; |
---|
| 41 | int emitter; |
---|
| 42 | int target; |
---|
| 43 | int target_type; |
---|
[4306] | 44 | void (*cb)(struct roar_notify_core * core, struct roar_event * event, void * userdata); |
---|
[4304] | 45 | void * userdata; |
---|
| 46 | size_t refc; |
---|
[4305] | 47 | unsigned int hash; |
---|
| 48 | // struct roar_subscriber * prev; |
---|
[4304] | 49 | struct roar_subscriber * next; |
---|
| 50 | }; |
---|
| 51 | |
---|
[4305] | 52 | struct roar_notify_core { |
---|
| 53 | size_t refc; |
---|
| 54 | size_t listc; |
---|
| 55 | struct roar_subscriber ** lists; |
---|
[4313] | 56 | void (*proxy)(struct roar_notify_core * core, struct roar_event * event, void * userdata); |
---|
| 57 | void * proxy_userdata; |
---|
[4305] | 58 | }; |
---|
| 59 | |
---|
[4320] | 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; } } |
---|
[4305] | 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) { |
---|
[4310] | 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: |
---|
[4305] | 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 *)); |
---|
[4313] | 113 | core->proxy = NULL; |
---|
| 114 | core->proxy_userdata = NULL; |
---|
[4305] | 115 | |
---|
| 116 | if ( core->lists == NULL ) { |
---|
| 117 | roar_mm_free(core); |
---|
| 118 | return NULL; |
---|
| 119 | } |
---|
| 120 | |
---|
[4308] | 121 | memset(core->lists, 0, lists*sizeof(struct roar_subscriber *)); |
---|
| 122 | |
---|
[4305] | 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 | |
---|
[4322] | 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 | |
---|
[4305] | 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 | |
---|
[4320] | 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 | |
---|
[4313] | 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 | |
---|
[4306] | 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) { |
---|
[4305] | 195 | struct roar_subscriber * subs = NULL; |
---|
| 196 | struct roar_subscriber * cur, * old; |
---|
| 197 | |
---|
[4309] | 198 | ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = ?", core, event, cb, userdata); |
---|
| 199 | |
---|
[4305] | 200 | _CKRCORE(NULL); |
---|
| 201 | |
---|
| 202 | if ( event == NULL || cb == NULL ) { |
---|
[4309] | 203 | ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = NULL // errno = EINVAL", core, event, cb, userdata); |
---|
[4305] | 204 | roar_errno = ROAR_ERROR_INVAL; |
---|
| 205 | return NULL; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | if ( (subs = roar_mm_malloc(sizeof(struct roar_subscriber))) == NULL ) { |
---|
[4309] | 209 | ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = NULL // errno = ENOMEM?", core, event, cb, userdata); |
---|
[4305] | 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 | |
---|
[4309] | 238 | ROAR_DBG("roar_notify_core_subscribe(core=%p, event=%p, cb=%p, userdata=%p) = %p", core, event, cb, userdata, subs); |
---|
[4305] | 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 | |
---|
[4309] | 247 | if ( subscriber == NULL ) { |
---|
[4305] | 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 | |
---|
[4309] | 276 | if ( event == NULL ) { |
---|
[4305] | 277 | roar_errno = ROAR_ERROR_INVAL; |
---|
| 278 | return -1; |
---|
| 279 | } |
---|
| 280 | |
---|
[4316] | 281 | if ( core->proxy != NULL && !(event->flags & ROAR_EVENT_FLAG_PROXYEVENT) ) { |
---|
[4313] | 282 | core->proxy(core, event, core->proxy_userdata); |
---|
| 283 | } |
---|
| 284 | |
---|
[4305] | 285 | cur = core->lists[_hash_event(core, event)]; |
---|
[4309] | 286 | ROAR_DBG("roar_notify_core_emit(core=%p, event=%p): cur=%p", core, event, cur); |
---|
[4305] | 287 | |
---|
| 288 | while (cur != NULL) { |
---|
[4309] | 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 | |
---|
[4305] | 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 { |
---|
[4306] | 304 | cur->cb(core, event, cur->userdata); |
---|
[4305] | 305 | } |
---|
| 306 | cur = cur->next; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | return 0; |
---|
| 310 | } |
---|
| 311 | |
---|
[4321] | 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 | return roar_notify_core_emit(NULL, &locevent); |
---|
| 327 | } |
---|
| 328 | |
---|
[4302] | 329 | //ll |
---|