aboutsummaryrefslogtreecommitdiff
path: root/bin/synthpod_nsm.c
blob: c95bee2c1717e0224ccba8f34b86c8ffbcbd2980 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 * Copyright (c) 2015 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // getpid

#include <Ecore.h>
#include <Ecore_Con.h>
#include <Ecore_File.h>
#include <Efreet.h>

#include <osc.h>

#include <synthpod_app.h>
#include <synthpod_nsm.h>

struct _synthpod_nsm_t {
	int managed;

	char *url;
	char *call;
	char *exe;

	const synthpod_nsm_driver_t *driver;
	void *data;

	Ecore_Con_Server *serv;
	Ecore_Event_Handler *add;
	Ecore_Event_Handler *del;
	Ecore_Event_Handler *dat;

	osc_data_t send [0x10000];
	osc_data_t recv [0x10000];
};

static int
_reply(osc_time_t time, const char *path, const char *fmt, const osc_data_t *buf,
	size_t size, void *data)
{
	const char *target = NULL;

	const osc_data_t *ptr = buf;
	ptr = osc_get_string(ptr, &target);

	//fprintf(stdout, "synthpod_nsm reply: %s\n", target);

	if(target && !strcmp(target, "/nsm/server/announce"))
	{
		const char *message;
		const char *manager;
		const char *capabilities;

		ptr = osc_get_string(ptr, &message);
		ptr = osc_get_string(ptr, &manager);
		osc_get_string(ptr, &capabilities);

		//TODO, e.g. toggle SM LED
	}

	return 1;
}

static int
_error(osc_time_t time, const char *path, const char *fmt, const osc_data_t *buf,
	size_t size, void *data)
{
	const char *msg = NULL;
	int32_t code = 0;
	const char *err = NULL;

	const osc_data_t *ptr = buf;
	ptr = osc_get_string(ptr, &msg);
	ptr = osc_get_int32(ptr, &code);
	osc_get_string(ptr, &err);

	fprintf(stderr, "synthpod_nsm error: #%i in %s: %s\n", code, msg, err);

	return 1;
}

static int
_client_open(osc_time_t time, const char *path, const char *fmt, const osc_data_t *buf,
	size_t size, void *data)
{
	synthpod_nsm_t *nsm = data;	
	
	const char *dir = NULL;
	const char *name = NULL;
	const char *id = NULL;

	const osc_data_t *ptr = buf;
	ptr = osc_get_string(ptr, &dir);
	ptr = osc_get_string(ptr, &name);
	osc_get_string(ptr, &id);

	// open/create app
	if(nsm->driver->open(dir, name, id, nsm->data))
		fprintf(stderr, "NSM load failed\n");

	return 1;
}

static int
_client_save(osc_time_t time, const char *path, const char *fmt, const osc_data_t *buf,
	size_t size, void *data)
{
	synthpod_nsm_t *nsm = data;	
	
	// save app
	if(nsm->driver->save(nsm->data))
		fprintf(stderr, "NSM save failed\n");

	return 1;
}

static int
_client_show_optional_gui(osc_time_t time, const char *path, const char *fmt, const osc_data_t *buf,
	size_t size, void *data)
{
	synthpod_nsm_t *nsm = data;	

	// show gui
	if(nsm->driver->show(nsm->data))
	{
		fprintf(stderr, "NSM show GUI failed\n");
		return 1;
	}

	// reply
	const osc_data_t *ptr;
	osc_data_t *buf0 = nsm->send;

	ptr = osc_set_vararg(buf0, buf0+256, "/nsm/client/gui_is_shown", "");

	size_t written = ptr ? ptr - buf0 : 0;
	if(written)
		ecore_con_server_send(nsm->serv, nsm->send, written);
	else
		fprintf(stderr, "OSC sending failed\n");

	return 1;
}

static int
_client_hide_optional_gui(osc_time_t time, const char *path, const char *fmt, const osc_data_t *buf,
	size_t size, void *data)
{
	synthpod_nsm_t *nsm = data;	

	// hide gui
	if(nsm->driver->hide(nsm->data))
	{
		fprintf(stderr, "NSM hide GUI failed\n");
		return 1;
	}

	// reply
	const osc_data_t *ptr;
	osc_data_t *buf0 = nsm->send;

	ptr = osc_set_vararg(buf0, buf0+256, "/nsm/client/gui_is_hidden", "");

	size_t written = ptr ? ptr - buf0 : 0;
	if(written)
		ecore_con_server_send(nsm->serv, nsm->send, written);
	else
		fprintf(stderr, "OSC sending failed\n");

	return 1;
}

static void
_announce(synthpod_nsm_t *nsm)
{
	// send announce message
	pid_t pid = getpid();

	int has_gui = nsm->driver->show && nsm->driver->hide;
	const char *capabilities = has_gui
		? ":message:optional-gui:"
		: ":message:";

	osc_data_t *buf0 = nsm->send;
	osc_data_t *ptr = buf0;
	osc_data_t *end = buf0 + 512;
	osc_data_t *bndl, *itm;

	ptr = osc_start_bundle(ptr, end, OSC_IMMEDIATE, &bndl);
	ptr = osc_start_bundle_item(ptr, end, &itm);
	ptr = osc_set_vararg(ptr, end, "/nsm/server/announce", "sssiii",
		nsm->call, capabilities, nsm->exe, 1, 2, pid);
	ptr = osc_end_bundle_item(ptr, end, itm);
	if(has_gui)
	{
		ptr = osc_start_bundle_item(ptr, end, &itm);
		ptr = osc_set_vararg(ptr, end, "/nsm/client/gui_is_shown", "");
		ptr = osc_end_bundle_item(ptr, end, itm);
	}
	ptr = osc_end_bundle(ptr, end, bndl);

	size_t written = ptr ? ptr - buf0 : 0;
	if(written)
		ecore_con_server_send(nsm->serv, nsm->send, written);
	else
		fprintf(stderr, "OSC sending failed\n");
}

static const osc_method_t methods [] = {
	{"/reply", NULL, _reply},
	{"/error", "sis", _error},
	
	{"/nsm/client/open", "sss", _client_open},
	{"/nsm/client/save", "", _client_save},
	{"/nsm/client/show_optional_gui", "", _client_show_optional_gui},
	{"/nsm/client/hide_optional_gui", "", _client_hide_optional_gui},

	{NULL, NULL, NULL}
};

static Eina_Bool
_con_add(void *data, int type, void *info)
{
	synthpod_nsm_t *nsm = data;

	assert(type == ECORE_CON_EVENT_SERVER_ADD);

	//printf("_client_add\n");
	//TODO
			
	_announce(nsm);

	return EINA_TRUE;
}

static Eina_Bool
_con_del(void *data, int type, void *info)
{
	assert(type == ECORE_CON_EVENT_SERVER_DEL);
	
	//printf("_client_del\n");
	//TODO

	return EINA_TRUE;
}

static Eina_Bool
_con_dat(void *data, int type, void *info)
{
	synthpod_nsm_t *nsm = data;
	Ecore_Con_Event_Client_Data *ev = info;

	assert(type == ECORE_CON_EVENT_SERVER_DATA);
	
	//printf("_client_data\n");

	if(osc_check_packet(ev->data, ev->size))
		osc_dispatch_method(ev->data, ev->size, methods, NULL, NULL, nsm);
	else
		fprintf(stderr, "_client_dat: malformed OSC packet\n");

	return EINA_TRUE;
}

synthpod_nsm_t *
synthpod_nsm_new(const char *exe, const char *path,
	const synthpod_nsm_driver_t *nsm_driver, void *data)
{
	efreet_init();

	if(!nsm_driver)
		return NULL;

	synthpod_nsm_t *nsm = calloc(1, sizeof(synthpod_nsm_t));
	if(!nsm)
		return NULL;

	nsm->driver = nsm_driver;
	nsm->data = data;

	nsm->call = strdup("Synthpod");
	nsm->exe = exe ? strdup(exe) : NULL;
	
	nsm->url = getenv("NSM_URL");
	if(nsm->url)
	{
		nsm->managed = 1;

		nsm->url = strdup(nsm->url);
		if(!nsm->url)
			return NULL;
		
		//printf("url: %s\n", nsm->url);

		Ecore_Con_Type type;
		if(!strncmp(nsm->url, "osc.udp", 7))
			type = ECORE_CON_REMOTE_UDP;
		else if(!strncmp(nsm->url, "osc.tcp", 7))
			type = ECORE_CON_REMOTE_TCP;
		else
			goto fail;

		char *addr = strstr(nsm->url, "://");
		if(!addr)
			goto fail;
		addr += 3; // skip "://"

		char *dst = strchr(addr, ':');
		if(!dst)
			goto fail;
		*dst++ = '\0';

		uint16_t port;
		if(sscanf(dst, "%hu", &port) != 1)
			goto fail;
		
		printf("addr: %s, dst: %hu\n", addr, port);

		nsm->serv = ecore_con_server_connect(type,
			addr, port, nsm);
		if(!nsm->serv)
			goto fail;

		nsm->add = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD, _con_add, nsm);
		nsm->del = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL, _con_del, nsm);
		nsm->dat = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DATA, _con_dat, nsm);

		if(type == ECORE_CON_REMOTE_UDP)
			_announce(nsm);
	}
	else
	{
		nsm->managed = 0;

		if(path)
		{
			nsm->driver->open(path, nsm->call, nsm->exe, nsm->data);
		}
		else
		{
			const char *data_dir = efreet_data_home_get();

			char *synthpod_dir = NULL;
			asprintf(&synthpod_dir, "%s/synthpod", data_dir);
			if(synthpod_dir)
			{
				ecore_file_mkpath(synthpod_dir);

				nsm->driver->open(synthpod_dir,
					nsm->call, nsm->exe, nsm->data);

				free(synthpod_dir);
			}
		}
	}

	return nsm;

fail:
	if(nsm->url)
		free(nsm->url);

	return NULL;
}

void
synthpod_nsm_free(synthpod_nsm_t *nsm)
{
	if(nsm)
	{
		if(nsm->call)
			free(nsm->call);
		if(nsm->exe)
			free(nsm->exe);

		if(nsm->url)
		{
			if(nsm->add)
				ecore_event_handler_del(nsm->add);
			if(nsm->del)
				ecore_event_handler_del(nsm->del);
			if(nsm->dat)
				ecore_event_handler_del(nsm->dat);
			if(nsm->serv)
				ecore_con_server_del(nsm->serv);

			free(nsm->url);
		}

		free(nsm);
	}

	efreet_shutdown();
}

void
synthpod_nsm_opened(synthpod_nsm_t *nsm, int status)
{
	if(!nsm)
		return;

	const osc_data_t *ptr;
	osc_data_t *buf0 = nsm->send;

	if(status == 0)
	{
		ptr = osc_set_vararg(buf0, buf0+256, "/reply", "ss",
			"/nsm/client/open", "opened");
	}
	else
	{
		ptr = osc_set_vararg(buf0, buf0+256, "/error", "sis",
			"/nsm/client/open", 2, "opening failed");
	}

	size_t written = ptr ? ptr - buf0 : 0;
	if(written)
		ecore_con_server_send(nsm->serv, nsm->send, written);
	else
		fprintf(stderr, "OSC sending failed\n");
}

void
synthpod_nsm_saved(synthpod_nsm_t *nsm, int status)
{
	if(!nsm)
		return;

	const osc_data_t *ptr;
	osc_data_t *buf0 = nsm->send;

	if(status == 0)
	{
		ptr = osc_set_vararg(buf0, buf0+256, "/reply", "ss",
			"/nsm/client/save", "saved");
	}
	else
	{
		ptr = osc_set_vararg(buf0, buf0+256, "/error", "sis",
			"/nsm/client/save", 1, "save failed");
	}

	size_t written = ptr ? ptr - buf0 : 0;
	if(written)
		ecore_con_server_send(nsm->serv, nsm->send, written);
	else
		fprintf(stderr, "OSC sending failed\n");
}

int
synthpod_nsm_managed()
{
	return getenv("NSM_URL") ? 1 : 0;
}