Imported Upstream version 0.13.2+dsfg1

This commit is contained in:
Sebastian Ramacher 2016-02-24 00:16:51 +01:00
commit fb3990e9e5
2036 changed files with 287360 additions and 0 deletions

250
libobs/callback/calldata.c Normal file
View file

@ -0,0 +1,250 @@
/*
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <string.h>
#include "../util/bmem.h"
#include "../util/base.h"
#include "calldata.h"
/*
* Uses a data stack. Probably more complex than it should be, but reduces
* fetching.
*
* Stack format is:
* [size_t param1_name_size]
* [char[] param1_name]
* [size_t param1_data_size]
* [uint8_t[] param1_data]
* [size_t param2_name_size]
* [char[] param2_name]
* [size_t param2_data_size]
* [uint8_t[] param2_data]
* [...]
* [size_t 0]
*
* Strings and string sizes always include the null terminator to allow for
* direct referencing.
*/
static inline void cd_serialize(uint8_t **pos, void *ptr, size_t size)
{
memcpy(ptr, *pos, size);
*pos += size;
}
static inline size_t cd_serialize_size(uint8_t **pos)
{
size_t size = 0;
memcpy(&size, *pos, sizeof(size_t));
*pos += sizeof(size_t);
return size;
}
static inline const char *cd_serialize_string(uint8_t **pos)
{
size_t size = cd_serialize_size(pos);
const char *str = (const char *)*pos;
*pos += size;
return (size != 0) ? str : NULL;
}
static bool cd_getparam(const calldata_t *data, const char *name,
uint8_t **pos)
{
size_t name_size;
if (!data->size)
return false;
*pos = data->stack;
name_size = cd_serialize_size(pos);
while (name_size != 0) {
const char *param_name = (const char *)*pos;
size_t param_size;
*pos += name_size;
if (strcmp(param_name, name) == 0)
return true;
param_size = cd_serialize_size(pos);
*pos += param_size;
name_size = cd_serialize_size(pos);
}
*pos -= sizeof(size_t);
return false;
}
static inline void cd_copy_string(uint8_t **pos, const char *str, size_t len)
{
if (!len)
len = strlen(str)+1;
memcpy(*pos, &len, sizeof(size_t));
*pos += sizeof(size_t);
memcpy(*pos, str, len);
*pos += len;
}
static inline void cd_copy_data(uint8_t **pos, const void *in, size_t size)
{
memcpy(*pos, &size, sizeof(size_t));
*pos += sizeof(size_t);
if (size) {
memcpy(*pos, in, size);
*pos += size;
}
}
static inline void cd_set_first_param(calldata_t *data, const char *name,
const void *in, size_t size)
{
uint8_t *pos;
size_t capacity;
size_t name_len = strlen(name)+1;
capacity = sizeof(size_t)*3 + name_len + size;
data->size = capacity;
if (capacity < 128)
capacity = 128;
data->capacity = capacity;
data->stack = bmalloc(capacity);
pos = data->stack;
cd_copy_string(&pos, name, name_len);
cd_copy_data(&pos, in, size);
memset(pos, 0, sizeof(size_t));
}
static inline bool cd_ensure_capacity(calldata_t *data, uint8_t **pos,
size_t new_size)
{
size_t offset;
size_t new_capacity;
if (new_size < data->capacity)
return true;
if (data->fixed) {
blog(LOG_ERROR, "Tried to go above fixed calldata stack size!");
return false;
}
offset = *pos - data->stack;
new_capacity = data->capacity * 2;
if (new_capacity < new_size)
new_capacity = new_size;
data->stack = brealloc(data->stack, new_capacity);
data->capacity = new_capacity;
*pos = data->stack + offset;
return true;
}
/* ------------------------------------------------------------------------- */
bool calldata_get_data(const calldata_t *data, const char *name, void *out,
size_t size)
{
uint8_t *pos;
size_t data_size;
if (!data || !name || !*name)
return false;
if (!cd_getparam(data, name, &pos))
return false;
data_size = cd_serialize_size(&pos);
if (data_size != size)
return false;
memcpy(out, pos, size);
return true;
}
void calldata_set_data(calldata_t *data, const char *name, const void *in,
size_t size)
{
uint8_t *pos = NULL;
if (!data || !name || !*name)
return;
if (!data->fixed && !data->stack) {
cd_set_first_param(data, name, in, size);
return;
}
if (cd_getparam(data, name, &pos)) {
size_t cur_size;
memcpy(&cur_size, pos, sizeof(size_t));
if (cur_size < size) {
size_t offset = size - cur_size;
size_t bytes = data->size;
if (!cd_ensure_capacity(data, &pos, bytes + offset))
return;
memmove(pos+offset, pos, bytes - (pos - data->stack));
data->size += offset;
} else if (cur_size > size) {
size_t offset = cur_size - size;
size_t bytes = data->size - offset;
memmove(pos, pos+offset, bytes - (pos - data->stack));
data->size -= offset;
}
cd_copy_data(&pos, in, size);
} else {
size_t name_len = strlen(name)+1;
size_t offset = name_len + size + sizeof(size_t)*2;
if (!cd_ensure_capacity(data, &pos, data->size + offset))
return;
data->size += offset;
cd_copy_string(&pos, name, 0);
cd_copy_data(&pos, in, size);
memset(pos, 0, sizeof(size_t));
}
}
bool calldata_get_string(const calldata_t *data, const char *name,
const char **str)
{
uint8_t *pos;
if (!data || !name || !*name)
return false;
if (!cd_getparam(data, name, &pos))
return false;
*str = cd_serialize_string(&pos);
return true;
}

198
libobs/callback/calldata.h Normal file
View file

@ -0,0 +1,198 @@
/*
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include <string.h>
#include "../util/c99defs.h"
#include "../util/bmem.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
* Procedure call data structure
*
* This is used to store parameters (and return value) sent to/from signals,
* procedures, and callbacks.
*/
enum call_param_type {
CALL_PARAM_TYPE_VOID,
CALL_PARAM_TYPE_INT,
CALL_PARAM_TYPE_FLOAT,
CALL_PARAM_TYPE_BOOL,
CALL_PARAM_TYPE_PTR,
CALL_PARAM_TYPE_STRING
};
#define CALL_PARAM_IN (1<<0)
#define CALL_PARAM_OUT (1<<1)
struct calldata {
uint8_t *stack;
size_t size; /* size of the stack, in bytes */
size_t capacity; /* capacity of the stack, in bytes */
bool fixed; /* fixed size (using call stack) */
};
typedef struct calldata calldata_t;
static inline void calldata_init(struct calldata *data)
{
memset(data, 0, sizeof(struct calldata));
}
static inline void calldata_clear(struct calldata *data);
static inline void calldata_init_fixed(struct calldata *data, uint8_t *stack,
size_t size)
{
data->stack = stack;
data->capacity = size;
data->fixed = true;
data->size = 0;
calldata_clear(data);
}
static inline void calldata_free(struct calldata *data)
{
if (!data->fixed)
bfree(data->stack);
}
EXPORT bool calldata_get_data(const calldata_t *data, const char *name,
void *out, size_t size);
EXPORT void calldata_set_data(calldata_t *data, const char *name,
const void *in, size_t new_size);
static inline void calldata_clear(struct calldata *data)
{
if (data->stack) {
data->size = sizeof(size_t);
memset(data->stack, 0, sizeof(size_t));
}
}
/* ------------------------------------------------------------------------- */
/* NOTE: 'get' functions return true only if paramter exists, and is the
* same type. They return false otherwise. */
static inline bool calldata_get_int(const calldata_t *data, const char *name,
long long *val)
{
return calldata_get_data(data, name, val, sizeof(*val));
}
static inline bool calldata_get_float (const calldata_t *data, const char *name,
double *val)
{
return calldata_get_data(data, name, val, sizeof(*val));
}
static inline bool calldata_get_bool (const calldata_t *data, const char *name,
bool *val)
{
return calldata_get_data(data, name, val, sizeof(*val));
}
static inline bool calldata_get_ptr (const calldata_t *data, const char *name,
void *p_ptr)
{
return calldata_get_data(data, name, p_ptr, sizeof(p_ptr));
}
EXPORT bool calldata_get_string(const calldata_t *data, const char *name,
const char **str);
/* ------------------------------------------------------------------------- */
/* call if you know your data is valid */
static inline long long calldata_int(const calldata_t *data, const char *name)
{
long long val = 0;
calldata_get_int(data, name, &val);
return val;
}
static inline double calldata_float(const calldata_t *data, const char *name)
{
double val = 0.0;
calldata_get_float(data, name, &val);
return val;
}
static inline bool calldata_bool(const calldata_t *data, const char *name)
{
bool val = false;
calldata_get_bool(data, name, &val);
return val;
}
static inline void *calldata_ptr(const calldata_t *data, const char *name)
{
void *val = NULL;
calldata_get_ptr(data, name, &val);
return val;
}
static inline const char *calldata_string(const calldata_t *data,
const char *name)
{
const char *val = NULL;
calldata_get_string(data, name, &val);
return val;
}
/* ------------------------------------------------------------------------- */
static inline void calldata_set_int (calldata_t *data, const char *name,
long long val)
{
calldata_set_data(data, name, &val, sizeof(val));
}
static inline void calldata_set_float (calldata_t *data, const char *name,
double val)
{
calldata_set_data(data, name, &val, sizeof(val));
}
static inline void calldata_set_bool (calldata_t *data, const char *name,
bool val)
{
calldata_set_data(data, name, &val, sizeof(val));
}
static inline void calldata_set_ptr (calldata_t *data, const char *name,
void *ptr)
{
calldata_set_data(data, name, &ptr, sizeof(ptr));
}
static inline void calldata_set_string(calldata_t *data, const char *name,
const char *str)
{
if (str)
calldata_set_data(data, name, str, strlen(str)+1);
else
calldata_set_data(data, name, NULL, 0);
}
#ifdef __cplusplus
}
#endif

239
libobs/callback/decl.c Normal file
View file

@ -0,0 +1,239 @@
/*
* Copyright (c) 2014 Hugh Bailey <obs.jim@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "../util/cf-parser.h"
#include "decl.h"
static inline void err_specifier_exists(struct cf_parser *cfp,
const char *storage)
{
cf_adderror(cfp, "'$1' specifier already exists", LEX_ERROR,
storage, NULL, NULL);
}
static inline void err_reserved_name(struct cf_parser *cfp, const char *name)
{
cf_adderror(cfp, "'$1' is a reserved name", LEX_ERROR,
name, NULL, NULL);
}
static inline void err_existing_name(struct cf_parser *cfp, const char *name)
{
cf_adderror(cfp, "'$1' already exists", LEX_ERROR, name, NULL, NULL);
}
static bool is_in_out_specifier(struct cf_parser *cfp, struct strref *name,
uint32_t *type)
{
if (strref_cmp(name, "in") == 0) {
if (*type & CALL_PARAM_IN)
err_specifier_exists(cfp, "in");
*type |= CALL_PARAM_IN;
} else if (strref_cmp(name, "out") == 0) {
if (*type & CALL_PARAM_OUT)
err_specifier_exists(cfp, "out");
*type |= CALL_PARAM_OUT;
} else {
return false;
}
return true;
}
#define TYPE_OR_STORAGE "type or storage specifier"
static bool get_type(struct strref *ref, enum call_param_type *type,
bool is_return)
{
if (strref_cmp(ref, "int") == 0)
*type = CALL_PARAM_TYPE_INT;
else if (strref_cmp(ref, "float") == 0)
*type = CALL_PARAM_TYPE_FLOAT;
else if (strref_cmp(ref, "bool") == 0)
*type = CALL_PARAM_TYPE_BOOL;
else if (strref_cmp(ref, "ptr") == 0)
*type = CALL_PARAM_TYPE_PTR;
else if (strref_cmp(ref, "string") == 0)
*type = CALL_PARAM_TYPE_STRING;
else if (is_return && strref_cmp(ref, "void") == 0)
*type = CALL_PARAM_TYPE_VOID;
else
return false;
return true;
}
static bool is_reserved_name(const char *str)
{
return (strcmp(str, "int") == 0) ||
(strcmp(str, "float") == 0) ||
(strcmp(str, "bool") == 0) ||
(strcmp(str, "ptr") == 0) ||
(strcmp(str, "string") == 0) ||
(strcmp(str, "void") == 0) ||
(strcmp(str, "return") == 0);
}
static bool name_exists(struct decl_info *decl, const char *name)
{
for (size_t i = 0; i < decl->params.num; i++) {
const char *param_name = decl->params.array[i].name;
if (strcmp(name, param_name) == 0)
return true;
}
return false;
}
static int parse_param(struct cf_parser *cfp, struct decl_info *decl)
{
struct strref ref;
int code;
struct decl_param param = {0};
/* get stprage specifiers */
code = cf_next_name_ref(cfp, &ref, TYPE_OR_STORAGE, ",");
if (code != PARSE_SUCCESS)
return code;
while (is_in_out_specifier(cfp, &ref, &param.flags)) {
code = cf_next_name_ref(cfp, &ref, TYPE_OR_STORAGE, ",");
if (code != PARSE_SUCCESS)
return code;
}
/* parameters not marked with specifers are input parameters */
if (param.flags == 0)
param.flags = CALL_PARAM_IN;
if (!get_type(&ref, &param.type, false)) {
cf_adderror_expecting(cfp, "type");
cf_go_to_token(cfp, ",", ")");
return PARSE_CONTINUE;
}
/* name */
code = cf_next_name(cfp, &param.name, "parameter name", ",");
if (code != PARSE_SUCCESS)
return code;
if (name_exists(decl, param.name))
err_existing_name(cfp, param.name);
if (is_reserved_name(param.name))
err_reserved_name(cfp, param.name);
da_push_back(decl->params, &param);
return PARSE_SUCCESS;
}
static void parse_params(struct cf_parser *cfp, struct decl_info *decl)
{
struct cf_token peek;
int code;
if (!cf_peek_valid_token(cfp, &peek))
return;
while (peek.type == CFTOKEN_NAME) {
code = parse_param(cfp, decl);
if (code == PARSE_EOF)
return;
if (code != PARSE_CONTINUE && !cf_next_valid_token(cfp))
return;
if (cf_token_is(cfp, ")"))
break;
else if (cf_token_should_be(cfp, ",", ",", NULL) == PARSE_EOF)
return;
if (!cf_peek_valid_token(cfp, &peek))
return;
}
if (!cf_token_is(cfp, ")"))
cf_next_token_should_be(cfp, ")", NULL, NULL);
}
static void print_errors(struct cf_parser *cfp, const char *decl_string)
{
char *errors = error_data_buildstring(&cfp->error_list);
if (errors) {
blog(LOG_WARNING, "Errors/warnings for '%s':\n\n%s",
decl_string, errors);
bfree(errors);
}
}
bool parse_decl_string(struct decl_info *decl, const char *decl_string)
{
struct cf_parser cfp;
struct strref ret_type;
struct decl_param ret_param = {0};
int code;
bool success;
decl->decl_string = decl_string;
ret_param.flags = CALL_PARAM_OUT;
cf_parser_init(&cfp);
if (!cf_parser_parse(&cfp, decl_string, "declaraion"))
goto fail;
code = cf_get_name_ref(&cfp, &ret_type, "return type", NULL);
if (code == PARSE_EOF)
goto fail;
if (!get_type(&ret_type, &ret_param.type, true))
cf_adderror_expecting(&cfp, "return type");
code = cf_next_name(&cfp, &decl->name, "function name", "(");
if (code == PARSE_EOF)
goto fail;
if (is_reserved_name(decl->name))
err_reserved_name(&cfp, decl->name);
code = cf_next_token_should_be(&cfp, "(", "(", NULL);
if (code == PARSE_EOF)
goto fail;
parse_params(&cfp, decl);
fail:
success = !error_data_has_errors(&cfp.error_list);
if (success && ret_param.type != CALL_PARAM_TYPE_VOID) {
ret_param.name = bstrdup("return");
da_push_back(decl->params, &ret_param);
}
if (!success)
decl_info_free(decl);
print_errors(&cfp, decl_string);
cf_parser_free(&cfp);
return success;
}

61
libobs/callback/decl.h Normal file
View file

@ -0,0 +1,61 @@
/*
* Copyright (c) 2014 Hugh Bailey <obs.jim@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include "calldata.h"
#include "../util/darray.h"
#ifdef __cplusplus
extern "C" {
#endif
struct decl_param {
char *name;
enum call_param_type type;
uint32_t flags;
};
static inline void decl_param_free(struct decl_param *param)
{
if (param)
bfree(param->name);
memset(param, 0, sizeof(struct decl_param));
}
struct decl_info {
char *name;
const char *decl_string;
DARRAY(struct decl_param) params;
};
static inline void decl_info_free(struct decl_info *decl)
{
if (decl) {
for (size_t i = 0; i < decl->params.num; i++)
decl_param_free(decl->params.array+i);
da_free(decl->params);
bfree(decl->name);
memset(decl, 0, sizeof(struct decl_info));
}
}
EXPORT bool parse_decl_string(struct decl_info *decl, const char *decl_string);
#ifdef __cplusplus
}
#endif

90
libobs/callback/proc.c Normal file
View file

@ -0,0 +1,90 @@
/*
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "../util/darray.h"
#include "decl.h"
#include "proc.h"
struct proc_info {
struct decl_info func;
void *data;
proc_handler_proc_t callback;
};
static inline void proc_info_free(struct proc_info *pi)
{
decl_info_free(&pi->func);
}
struct proc_handler {
/* TODO: replace with hash table lookup? */
DARRAY(struct proc_info) procs;
};
proc_handler_t *proc_handler_create(void)
{
struct proc_handler *handler = bmalloc(sizeof(struct proc_handler));
da_init(handler->procs);
return handler;
}
void proc_handler_destroy(proc_handler_t *handler)
{
if (handler) {
for (size_t i = 0; i < handler->procs.num; i++)
proc_info_free(handler->procs.array+i);
da_free(handler->procs);
bfree(handler);
}
}
void proc_handler_add(proc_handler_t *handler, const char *decl_string,
proc_handler_proc_t proc, void *data)
{
if (!handler) return;
struct proc_info pi;
memset(&pi, 0, sizeof(struct proc_info));
if (!parse_decl_string(&pi.func, decl_string)) {
blog(LOG_ERROR, "Function declaration invalid: %s",
decl_string);
return;
}
pi.callback = proc;
pi.data = data;
da_push_back(handler->procs, &pi);
}
bool proc_handler_call(proc_handler_t *handler, const char *name,
calldata_t *params)
{
if (!handler) return false;
for (size_t i = 0; i < handler->procs.num; i++) {
struct proc_info *info = handler->procs.array+i;
if (strcmp(info->func.name, name) == 0) {
info->callback(info->data, params);
return true;
}
}
return false;
}

54
libobs/callback/proc.h Normal file
View file

@ -0,0 +1,54 @@
/*
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include "../util/c99defs.h"
#include "calldata.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
* Procedure handler
*
* This handler is used to allow access to one or more procedures that can be
* added and called without having to have direct access to declarations or
* procedure callback pointers.
*/
struct proc_handler;
typedef struct proc_handler proc_handler_t;
typedef void (*proc_handler_proc_t)(void*, calldata_t*);
EXPORT proc_handler_t *proc_handler_create(void);
EXPORT void proc_handler_destroy(proc_handler_t *handler);
EXPORT void proc_handler_add(proc_handler_t *handler, const char *decl_string,
proc_handler_proc_t proc, void *data);
/**
* Calls a function in a procedure handler. Returns false if the named
* procedure is not found.
*/
EXPORT bool proc_handler_call(proc_handler_t *handler, const char *name,
calldata_t *params);
#ifdef __cplusplus
}
#endif

268
libobs/callback/signal.c Normal file
View file

@ -0,0 +1,268 @@
/*
* Copyright (c) 2013-2014 Hugh Bailey <obs.jim@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "../util/darray.h"
#include "../util/threading.h"
#include "decl.h"
#include "signal.h"
struct signal_callback {
signal_callback_t callback;
void *data;
bool remove;
};
struct signal_info {
struct decl_info func;
DARRAY(struct signal_callback) callbacks;
pthread_mutex_t mutex;
bool signalling;
struct signal_info *next;
};
static inline struct signal_info *signal_info_create(struct decl_info *info)
{
pthread_mutexattr_t attr;
struct signal_info *si;
if (pthread_mutexattr_init(&attr) != 0)
return NULL;
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
return NULL;
si = bmalloc(sizeof(struct signal_info));
si->func = *info;
si->next = NULL;
si->signalling = false;
da_init(si->callbacks);
if (pthread_mutex_init(&si->mutex, &attr) != 0) {
blog(LOG_ERROR, "Could not create signal");
decl_info_free(&si->func);
bfree(si);
return NULL;
}
return si;
}
static inline void signal_info_destroy(struct signal_info *si)
{
if (si) {
pthread_mutex_destroy(&si->mutex);
decl_info_free(&si->func);
da_free(si->callbacks);
bfree(si);
}
}
static inline size_t signal_get_callback_idx(struct signal_info *si,
signal_callback_t callback, void *data)
{
for (size_t i = 0; i < si->callbacks.num; i++) {
struct signal_callback *sc = si->callbacks.array+i;
if (sc->callback == callback && sc->data == data)
return i;
}
return DARRAY_INVALID;
}
struct signal_handler {
struct signal_info *first;
pthread_mutex_t mutex;
};
static struct signal_info *getsignal(signal_handler_t *handler,
const char *name, struct signal_info **p_last)
{
struct signal_info *signal, *last= NULL;
signal = handler->first;
while (signal != NULL) {
if (strcmp(signal->func.name, name) == 0)
break;
last = signal;
signal = signal->next;
}
if (p_last)
*p_last = last;
return signal;
}
/* ------------------------------------------------------------------------- */
signal_handler_t *signal_handler_create(void)
{
struct signal_handler *handler = bmalloc(sizeof(struct signal_handler));
handler->first = NULL;
if (pthread_mutex_init(&handler->mutex, NULL) != 0) {
blog(LOG_ERROR, "Couldn't create signal handler!");
bfree(handler);
return NULL;
}
return handler;
}
void signal_handler_destroy(signal_handler_t *handler)
{
if (handler) {
struct signal_info *sig = handler->first;
while (sig != NULL) {
struct signal_info *next = sig->next;
signal_info_destroy(sig);
sig = next;
}
pthread_mutex_destroy(&handler->mutex);
bfree(handler);
}
}
bool signal_handler_add(signal_handler_t *handler, const char *signal_decl)
{
struct decl_info func = {0};
struct signal_info *sig, *last;
bool success = true;
if (!parse_decl_string(&func, signal_decl)) {
blog(LOG_ERROR, "Signal declaration invalid: %s", signal_decl);
return false;
}
pthread_mutex_lock(&handler->mutex);
sig = getsignal(handler, func.name, &last);
if (sig) {
blog(LOG_WARNING, "Signal declaration '%s' exists", func.name);
decl_info_free(&func);
success = false;
} else {
sig = signal_info_create(&func);
if (!last)
handler->first = sig;
else
last->next = sig;
}
pthread_mutex_unlock(&handler->mutex);
return success;
}
void signal_handler_connect(signal_handler_t *handler, const char *signal,
signal_callback_t callback, void *data)
{
struct signal_info *sig, *last;
struct signal_callback cb_data = {callback, data, false};
size_t idx;
if (!handler)
return;
pthread_mutex_lock(&handler->mutex);
sig = getsignal(handler, signal, &last);
pthread_mutex_unlock(&handler->mutex);
if (!sig) {
blog(LOG_WARNING, "signal_handler_connect: "
"signal '%s' not found", signal);
return;
}
/* -------------- */
pthread_mutex_lock(&sig->mutex);
idx = signal_get_callback_idx(sig, callback, data);
if (idx == DARRAY_INVALID)
da_push_back(sig->callbacks, &cb_data);
pthread_mutex_unlock(&sig->mutex);
}
static inline struct signal_info *getsignal_locked(signal_handler_t *handler,
const char *name)
{
struct signal_info *sig;
if (!handler)
return NULL;
pthread_mutex_lock(&handler->mutex);
sig = getsignal(handler, name, NULL);
pthread_mutex_unlock(&handler->mutex);
return sig;
}
void signal_handler_disconnect(signal_handler_t *handler, const char *signal,
signal_callback_t callback, void *data)
{
struct signal_info *sig = getsignal_locked(handler, signal);
size_t idx;
if (!sig)
return;
pthread_mutex_lock(&sig->mutex);
idx = signal_get_callback_idx(sig, callback, data);
if (idx != DARRAY_INVALID) {
if (sig->signalling)
sig->callbacks.array[idx].remove = true;
else
da_erase(sig->callbacks, idx);
}
pthread_mutex_unlock(&sig->mutex);
}
void signal_handler_signal(signal_handler_t *handler, const char *signal,
calldata_t *params)
{
struct signal_info *sig = getsignal_locked(handler, signal);
if (!sig)
return;
pthread_mutex_lock(&sig->mutex);
sig->signalling = true;
for (size_t i = 0; i < sig->callbacks.num; i++) {
struct signal_callback *cb = sig->callbacks.array+i;
if (!cb->remove)
cb->callback(cb->data, params);
}
for (size_t i = sig->callbacks.num; i > 0; i--) {
struct signal_callback *cb = sig->callbacks.array+i-1;
if (cb->remove)
da_erase(sig->callbacks, i-1);
}
sig->signalling = false;
pthread_mutex_unlock(&sig->mutex);
}

68
libobs/callback/signal.h Normal file
View file

@ -0,0 +1,68 @@
/*
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include "../util/c99defs.h"
#include "calldata.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
* Signal handler
*
* This is used to create a signal handler which can broadcast events
* to one or more callbacks connected to a signal.
*/
struct signal_handler;
typedef struct signal_handler signal_handler_t;
typedef void (*signal_callback_t)(void*, calldata_t*);
EXPORT signal_handler_t *signal_handler_create(void);
EXPORT void signal_handler_destroy(signal_handler_t *handler);
EXPORT bool signal_handler_add(signal_handler_t *handler,
const char *signal_decl);
static inline bool signal_handler_add_array(signal_handler_t *handler,
const char **signal_decls)
{
bool success = true;
if (!signal_decls)
return false;
while (*signal_decls)
if (!signal_handler_add(handler, *(signal_decls++)))
success = false;
return success;
}
EXPORT void signal_handler_connect(signal_handler_t *handler,
const char *signal, signal_callback_t callback, void *data);
EXPORT void signal_handler_disconnect(signal_handler_t *handler,
const char *signal, signal_callback_t callback, void *data);
EXPORT void signal_handler_signal(signal_handler_t *handler, const char *signal,
calldata_t *params);
#ifdef __cplusplus
}
#endif