2014-07-03 14:15:38 +02:00
|
|
|
#include <platform.h>
|
2014-07-03 14:10:53 +02:00
|
|
|
#include "callback.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static struct reg {
|
|
|
|
struct reg * next;
|
2014-07-03 21:20:50 +02:00
|
|
|
HCALLBACK cb;
|
2014-07-03 14:15:38 +02:00
|
|
|
char *name;
|
2014-07-03 14:10:53 +02:00
|
|
|
} *registry;
|
|
|
|
|
2014-07-03 21:20:50 +02:00
|
|
|
HCALLBACK create_callback(void(*cbv)(va_list va)) {
|
|
|
|
HCALLBACK cb;
|
2014-07-03 14:10:53 +02:00
|
|
|
cb.cbv = cbv;
|
|
|
|
return cb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_callbacks(void) {
|
2015-01-30 20:37:14 +01:00
|
|
|
while (registry) {
|
2014-07-03 14:15:38 +02:00
|
|
|
struct reg *r = registry;
|
|
|
|
registry = r->next;
|
|
|
|
free(r->name);
|
|
|
|
free(r);
|
|
|
|
}
|
2014-07-03 14:10:53 +02:00
|
|
|
registry = 0;
|
|
|
|
}
|
|
|
|
|
2014-07-03 21:20:50 +02:00
|
|
|
HCALLBACK register_callback(const char *name, void(*cbv)(va_list va))
|
2014-07-03 14:10:53 +02:00
|
|
|
{
|
|
|
|
struct reg * r = (struct reg *)malloc(sizeof(struct reg));
|
|
|
|
r->next = registry;
|
2014-07-03 14:15:38 +02:00
|
|
|
r->name = _strdup(name);
|
2014-07-03 14:10:53 +02:00
|
|
|
r->cb.cbv = cbv;
|
|
|
|
registry = r;
|
|
|
|
return r->cb;
|
|
|
|
}
|
|
|
|
|
2014-07-03 21:20:50 +02:00
|
|
|
int find_callback(const char *name, HCALLBACK *result) {
|
2014-07-03 14:10:53 +02:00
|
|
|
if (result && name) {
|
|
|
|
struct reg *r;
|
2015-01-30 20:37:14 +01:00
|
|
|
for (r = registry; r; r = r->next) {
|
|
|
|
if (strcmp(r->name, name) == 0) {
|
2014-07-03 14:10:53 +02:00
|
|
|
*result = r->cb;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-03 21:20:50 +02:00
|
|
|
int call_callback(HCALLBACK cb, const char *name, ...) {
|
2014-07-03 14:10:53 +02:00
|
|
|
va_list ap;
|
|
|
|
if (name) {
|
|
|
|
int err = find_callback(name, &cb);
|
|
|
|
if (err) return err;
|
|
|
|
}
|
|
|
|
va_start(ap, name);
|
|
|
|
cb.cbv(ap);
|
|
|
|
va_end(ap);
|
|
|
|
return 0;
|
|
|
|
}
|