diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2012-09-26 20:08:10 +0200 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2012-09-26 20:08:10 +0200 |
commit | 7e5561d02f257f5c2ae54f4d55dee1093cf2ad4b (patch) | |
tree | a447fe2a109da09d9f06c67a0534873f9ac3a27b /osc_server.h | |
parent | eac5ac91ba27ae8e6d24625d6fa170425c51d459 (diff) | |
download | osc-graphics-7e5561d02f257f5c2ae54f4d55dee1093cf2ad4b.tar.gz |
isolated OSC server specific code in OscServer class and allow to register methods in order to localize OSC method handling
i.e. every Layer class is stand-alone now
Diffstat (limited to 'osc_server.h')
-rw-r--r-- | osc_server.h | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/osc_server.h b/osc_server.h new file mode 100644 index 0000000..362ded9 --- /dev/null +++ b/osc_server.h @@ -0,0 +1,99 @@ +#ifndef __OSC_SERVER_H +#define __OSC_SERVER_H + +#include <string.h> +#include <stdarg.h> + +#include <SDL.h> + +#include <lo/lo.h> + +#include "osc_graphics.h" + +class Layer; + +class OscServer { + lo_server_thread server; + +public: + struct MethodHandlerId { + char *types; + char *path; + void *data; + + MethodHandlerId(const char *types, const char *path, + void *d = NULL) : data(d) + { + MethodHandlerId::types = strdup(types); + MethodHandlerId::path = strdup(path); + } + ~MethodHandlerId() + { + free(types); + free(path); + } + }; + +private: + void add_method_v(MethodHandlerId **hnd, const char *types, + lo_method_handler handler, void *data, + const char *fmt, va_list ap); + +public: + typedef void (*MethodHandlerCb)(Layer *obj, lo_arg **argv); + typedef Layer *(*ConstructorHandlerCb)(const char *name, SDL_Rect geo, + float alpha, lo_arg **argv); + + OscServer(const char *port); + ~OscServer(); + + inline void + start() + { + lo_server_thread_start(server); + } + inline void + stop() + { + lo_server_thread_stop(server); + } + + inline void + add_method(MethodHandlerId **hnd, const char *types, + lo_method_handler handler, void *data, + const char *fmt, ...) + { + va_list ap; + va_start(ap, fmt); + add_method_v(hnd, types, handler, data, fmt, ap); + va_end(ap); + } + inline void + add_method(const char *types, + lo_method_handler handler, void *data, + const char *fmt, ...) + { + va_list ap; + va_start(ap, fmt); + add_method_v(NULL, types, handler, data, fmt, ap); + va_end(ap); + } + + void del_method(const char *types, const char *fmt, ...); + inline void + del_method(MethodHandlerId *hnd) + { + del_method(hnd->types, "%s", hnd->path); + delete hnd; + } + + MethodHandlerId *register_method(Layer *layer, const char *method, + const char *types, + MethodHandlerCb method_cb); + void unregister_method(MethodHandlerId *hnd); +}; + +#define GEO_TYPES "iiii" /* x, y, width, height */ +#define NEW_LAYER_TYPES "is" GEO_TYPES "f" /* position, name, GEO, alpha */ + +#endif |