aboutsummaryrefslogtreecommitdiff
path: root/osc_server.h
blob: b14dfa8b73ec1a96c781e6df8cccc76c3e975b62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#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 *(*CtorHandlerCb)(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;
	}

	void register_layer(const char *name, const char *types,
			    CtorHandlerCb ctor_cb);

	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