aboutsummaryrefslogtreecommitdiff
path: root/src/osc_server.cpp
blob: a9c2eaaabcc0d3ab1babf0d2ba0575430c303889 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdarg.h>
#include <stdio.h>

#include <SDL.h>

#include <lo/lo.h>

#include "osc_graphics.h"
#include "layer.h"

#include "osc_server.h"

/*
 * liblo callbacks
 */
extern "C" {

static void error_handler(int num, const char *msg, const char *path);
static int generic_handler(const char *path, const char *types, lo_arg **argv,
			   int argc, void *data, void *user_data);

static int dtor_generic_handler(const char *path, const char *types,
		       		lo_arg **argv, int argc,
		       		void *data, void *user_data);
static int ctor_generic_handler(const char *path, const char *types,
		       		lo_arg **argv, int argc,
		       		void *data, void *user_data);
static int method_generic_handler(const char *path, const char *types,
		       		  lo_arg **argv, int argc,
		       		  void *data, void *user_data);

}

extern LayerList layers;

static void
error_handler(int num, const char *msg, const char *path)
{
	fprintf(stderr, "liblo server error %d in path %s: %s\n",
		num, path, msg);
}

/* catch any incoming messages and display them. returning 1 means that the
 * message has not been fully handled and the server should try other methods */
static int
generic_handler(const char *path, const char *types, lo_arg **argv,
		int argc, void *data,
		void *user_data __attribute__((unused)))
{
	if (!config_dump_osc)
		return 1;

	printf("path: <%s>\n", path);
	for (int i = 0; i < argc; i++) {
		printf("arg %d '%c' ", i, types[i]);
		lo_arg_pp((lo_type)types[i], argv[i]);
		printf("\n");
	}
	printf("\n");

	return 1;
}

void
OSCServer::open(const char *port)
{
	server = lo_server_thread_new(port, error_handler);

	add_method(NULL, generic_handler, NULL, NULL);
}

void
OSCServer::add_method_v(MethodHandlerId **hnd, const char *types,
			lo_method_handler handler, void *data,
			const char *fmt, va_list ap)
{
	char buf[255];

	if (fmt)
		vsnprintf(buf, sizeof(buf), fmt, ap);
	lo_server_thread_add_method(server, fmt ? buf : NULL, types,
				    handler, data);

	if (hnd)
		*hnd = new MethodHandlerId(types, buf, data);
}

void
OSCServer::del_method(const char *types, const char *fmt, ...)
{
	char buf[255];
	va_list ap;

	va_start(ap, fmt);
	vsnprintf(buf, sizeof(buf), fmt, ap);
	lo_server_thread_del_method(server, buf, types);
	va_end(ap);
}

static int
dtor_generic_handler(const char *path,
		     const char *types __attribute__((unused)),
		     lo_arg **argv __attribute__((unused)),
		     int argc __attribute__((unused)),
		     void *data __attribute__((unused)),
		     void *user_data)
{
	Layer *layer = (Layer *)user_data;

	layers.delete_layer(layer);
	osc_server.del_method("", "%s", path);

	return 0;
}

static int
ctor_generic_handler(const char *path __attribute__((unused)),
		     const char *types __attribute__((unused)),
		     lo_arg **argv, int argc,
		     void *data __attribute__((unused)),
		     void *user_data)
{
	OSCServer::CtorHandlerCb const_cb = (OSCServer::CtorHandlerCb)user_data;
	Layer *layer;

	SDL_Rect geo = {
		(Sint16)argv[2]->i, (Sint16)argv[3]->i,
		(Uint16)argv[4]->i, (Uint16)argv[5]->i
	};

	layer = const_cb(&argv[1]->s, geo, argv[6]->f, argv + 7);
	layers.insert(argv[0]->i, layer);

	osc_server.add_method("", dtor_generic_handler, layer,
			      "/layer/%s/delete", layer->name);

	return 0;
}

void
OSCServer::register_layer(const char *name, const char *types,
			  CtorHandlerCb ctor_cb)
{
	char buf[255];

	snprintf(buf, sizeof(buf), "%s%s", NEW_LAYER_TYPES, types);
	add_method(buf, ctor_generic_handler, (void *)ctor_cb,
		   "/layer/new/%s", name);
}

struct OscMethodDefaultCtx {
	Layer				*layer;
	OSCServer::MethodHandlerCb	method_cb;
};

static int
method_generic_handler(const char *path __attribute__((unused)),
		       const char *types __attribute__((unused)),
		       lo_arg **argv, int argc,
		       void *data __attribute__((unused)),
		       void *user_data)
{
	OscMethodDefaultCtx *ctx = (OscMethodDefaultCtx *)user_data;

	ctx->layer->lock();
	ctx->method_cb(ctx->layer, argv);
	ctx->layer->unlock();

	return 0;
}

OSCServer::MethodHandlerId *
OSCServer::register_method(Layer *layer, const char *method, const char *types,
			   MethodHandlerCb method_cb)
{
	MethodHandlerId *hnd;
	OscMethodDefaultCtx *ctx = new OscMethodDefaultCtx;

	ctx->layer = layer;
	ctx->method_cb = method_cb;

	add_method(&hnd, types, method_generic_handler, ctx,
		   "/layer/%s/%s", layer->name, method);

	return hnd;
}

void
OSCServer::unregister_method(MethodHandlerId *hnd)
{
	delete (OscMethodDefaultCtx *)hnd->data;
	del_method(hnd);
}

OSCServer::~OSCServer()
{
	if (server)
		lo_server_thread_free(server);
}