aboutsummaryrefslogtreecommitdiff
path: root/osc.lv2/util.h
blob: 35176181c38f7e515623d6751880f170b8349b0d (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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
 * Copyright (c) 2015-2016 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.
 */

#ifndef LV2_OSC_UTIL_H
#define LV2_OSC_UTIL_H

#include <assert.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#if !defined(_WIN32)
# include <fnmatch.h>
#endif

#include <osc.lv2/osc.h>

#include <lv2/lv2plug.in/ns/ext/atom/util.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifndef __unused
#	define __unused __attribute__((unused))
#endif

#undef LV2_ATOM_TUPLE_FOREACH // there is a bug in LV2 1.10.0
#define LV2_ATOM_TUPLE_FOREACH(tuple, iter) \
	for (LV2_Atom* (iter) = lv2_atom_tuple_begin(tuple); \
	     !lv2_atom_tuple_is_end(LV2_ATOM_BODY(tuple), (tuple)->atom.size, (iter)); \
	     (iter) = lv2_atom_tuple_next(iter))

typedef void (*LV2_OSC_Method)(const char *path,
	const LV2_Atom_Tuple *arguments, void *data);

typedef struct _LV2_OSC_Hook LV2_OSC_Hook;

struct _LV2_OSC_Hook {
	const char *name;
	const LV2_OSC_Hook *hooks;
	LV2_OSC_Method method;
	void *data;
};

// characters not allowed in OSC path string
static const char invalid_path_chars [] = {
	' ', '#',
	'\0'
};

// allowed characters in OSC format string
static const char valid_format_chars [] = {
	LV2_OSC_INT32, LV2_OSC_FLOAT, LV2_OSC_STRING, LV2_OSC_BLOB,
	LV2_OSC_TRUE, LV2_OSC_FALSE, LV2_OSC_NIL, LV2_OSC_IMPULSE,
	LV2_OSC_INT64, LV2_OSC_DOUBLE, LV2_OSC_TIMETAG,
	LV2_OSC_SYMBOL, LV2_OSC_MIDI,
	'\0'
};

static bool
lv2_osc_pattern_match(const char *from, const char *name, size_t len)
{
#if !defined(_WIN32)
	size_t nbrace = 0;

#	if defined(FNM_EXTMATCH)
	// count opening curly braces
	for(size_t i = 0; i < len; i++)
	{
		if(from[i] == '{')
		{
			nbrace++;
		}
	}
#	endif

	// allocate temporary pattern buffer
	char *pattern = alloca(len + nbrace + 1);

	if(!pattern)
	{
		return false;
	}

#	if defined(FNM_EXTMATCH)
	// convert {x,y} to @(x|y) for extended fnmatch
	if(nbrace)
	{
		char *ptr = pattern;

		for(size_t i = 0; i < len; i++)
		{
			switch(from[i])
			{
				case '{':
				{
					*ptr++ = '@';
					*ptr++ = '(';
				} break;
				case ',':
				{
					*ptr++ = '|';
				} break;
				case '}':
				{
					*ptr++ = ')';
				} break;
				default:
				{
					*ptr++ = from[i];
				} break;
			}
		}
	}
	else
#	endif
	{
		memcpy(pattern, from, len);
	}

	// terminate pattern string with null terminator
	pattern[len + nbrace] = '\0';

#	if defined(FNM_EXTMATCH)
	return fnmatch(pattern, name, FNM_NOESCAPE | FNM_EXTMATCH) == 0 ? true : false;
#	else
	return fnmatch(pattern, name, FNM_NOESCAPE) == 0 ? true : false;
#	endif
#else
	return strncmp(from, name, len) == 0 ? true : false;
#endif
}

static void
_lv2_osc_hooks_internal(const char *path, const char *from,
	const LV2_Atom_Tuple *arguments, const LV2_OSC_Hook *hooks)
{
	const char *ptr = strchr(from, '/');

	const size_t len = ptr
		? (size_t)(ptr - from)
		: strlen(from);

	for(const LV2_OSC_Hook *hook = hooks; hook && hook->name; hook++)
	{
		if(lv2_osc_pattern_match(from, hook->name, len))
		{
			if(hook->hooks && ptr)
			{
				from = &ptr[1];

				_lv2_osc_hooks_internal(path, from, arguments, hook->hooks);
			}
			else if(hook->method && !ptr)
			{
				hook->method(path, arguments, hook->data);
			}
		}
	}
}

/**
   TODO
*/
static void
lv2_osc_hooks(const char *path, const LV2_Atom_Tuple *arguments, void *data)
{
	const LV2_OSC_Hook *hooks = data;
	const char *from = &path[1];

	_lv2_osc_hooks_internal(path, from, arguments, hooks);
}

/**
   TODO
*/
static inline bool
lv2_osc_check_path(const char *path)
{
	assert(path);

	if(path[0] != '/')
		return false;

	for(const char *ptr=path+1; *ptr!='\0'; ptr++)
		if( (isprint(*ptr) == 0) || (strchr(invalid_path_chars, *ptr) != NULL) )
			return false;

	return true;
}

/**
   TODO
*/
static inline bool
lv2_osc_check_fmt(const char *format, int offset)
{
	assert(format);

	if(offset && (format[0] != ',') )
		return false;

	for(const char *ptr=format+offset; *ptr!='\0'; ptr++)
		if(strchr(valid_format_chars, *ptr) == NULL)
			return false;

	return true;
}

/**
   TODO
*/
static inline uint64_t 
lv2_osc_timetag_parse(const LV2_OSC_Timetag *timetag)
{
	return ((uint64_t)timetag->integral << 32) | timetag->fraction;
}

/**
   TODO
*/
static inline LV2_OSC_Timetag *
lv2_osc_timetag_create(LV2_OSC_Timetag *timetag, uint64_t tt)
{
	timetag->integral = tt >> 32;
	timetag->fraction = tt & 0xffffffff;

	return timetag;
}

#define LV2_OSC_TIMETAG_CREATE(tt) \
	lv2_osc_timetag_create(&(LV2_OSC_Timetag){.integral = 0, .fraction = 0}, (tt))

/**
   TODO
*/
static inline bool
lv2_osc_is_packet_type(LV2_OSC_URID *osc_urid, LV2_URID type)
{
	return type == osc_urid->OSC_Packet;
}

/**
   TODO
*/
static inline bool
lv2_osc_is_bundle_type(LV2_OSC_URID *osc_urid, LV2_URID type)
{
	return type == osc_urid->OSC_Bundle;
}

/**
   TODO
*/
static inline bool
lv2_osc_is_message_type(LV2_OSC_URID *osc_urid, LV2_URID type)
{
	return type == osc_urid->OSC_Message;
}

/**
   TODO
*/
static inline bool
lv2_osc_is_message_or_bundle_type(LV2_OSC_URID *osc_urid, LV2_URID type)
{
	return lv2_osc_is_message_type(osc_urid, type)
		|| lv2_osc_is_bundle_type(osc_urid, type);
}

static inline LV2_OSC_Type
lv2_osc_argument_type(LV2_OSC_URID *osc_urid, const LV2_Atom *atom)
{
	const LV2_Atom_Object *obj = (const LV2_Atom_Object *)atom;

	if(atom->type == osc_urid->ATOM_Int)
		return LV2_OSC_INT32;
	else if(atom->type == osc_urid->ATOM_Float)
		return LV2_OSC_FLOAT;
	else if(atom->type == osc_urid->ATOM_String)
		return LV2_OSC_STRING;
	else if(atom->type == osc_urid->ATOM_Chunk)
		return LV2_OSC_BLOB;

	else if(atom->type == osc_urid->ATOM_Long)
		return LV2_OSC_INT64;
	else if(atom->type == osc_urid->ATOM_Double)
		return LV2_OSC_DOUBLE;
	else if( (atom->type == osc_urid->ATOM_Object) && (obj->body.otype == osc_urid->OSC_Timetag) )
		return LV2_OSC_TIMETAG;

	else if(atom->type == osc_urid->ATOM_Bool)
	{
		if(((const LV2_Atom_Bool *)atom)->body)
			return LV2_OSC_TRUE;
		else
			return LV2_OSC_FALSE;
	}
	else if(atom->type == osc_urid->ATOM_Literal)
	{
		const LV2_Atom_Literal *lit = (const LV2_Atom_Literal *)atom;
		if(lit->body.datatype == osc_urid->OSC_Nil)
			return LV2_OSC_NIL;
		else if(lit->body.datatype == osc_urid->OSC_Impulse)
			return LV2_OSC_IMPULSE;
		else if(lit->body.datatype == osc_urid->OSC_Char)
			return LV2_OSC_CHAR;
		else if(lit->body.datatype == osc_urid->OSC_RGBA)
			return LV2_OSC_RGBA;
	}

	else if(atom->type == osc_urid->ATOM_URID)
		return LV2_OSC_SYMBOL;
	else if(atom->type == osc_urid->MIDI_MidiEvent)
		return LV2_OSC_MIDI;

	return '\0';
}

static inline const LV2_Atom *
lv2_osc_int32_get(LV2_OSC_URID *osc_urid __unused, const LV2_Atom *atom,
	int32_t *i)
{
	assert(i);
	*i = ((const LV2_Atom_Int *)atom)->body;

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
lv2_osc_float_get(LV2_OSC_URID *osc_urid __unused, const LV2_Atom *atom,
	float *f)
{
	assert(f);
	*f = ((const LV2_Atom_Float *)atom)->body;

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
lv2_osc_string_get(LV2_OSC_URID *osc_urid __unused, const LV2_Atom *atom,
	const char **s)
{
	assert(s);
	*s = LV2_ATOM_BODY_CONST(atom);

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
lv2_osc_blob_get(LV2_OSC_URID *osc_urid __unused, const LV2_Atom *atom,
	uint32_t *size, const uint8_t **b)
{
	assert(size && b);
	*size = atom->size;
	*b = LV2_ATOM_BODY_CONST(atom);

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
lv2_osc_int64_get(LV2_OSC_URID *osc_urid __unused, const LV2_Atom *atom,
	int64_t *h)
{
	assert(h);
	*h = ((const LV2_Atom_Long *)atom)->body;

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
lv2_osc_double_get(LV2_OSC_URID *osc_urid __unused, const LV2_Atom *atom,
	double *d)
{
	assert(d);
	*d = ((const LV2_Atom_Double *)atom)->body;

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom * 
lv2_osc_timetag_get(LV2_OSC_URID *osc_urid, const LV2_Atom *atom,
	LV2_OSC_Timetag *timetag)
{
	assert(timetag);

	const LV2_Atom_Long *integral = NULL;
	const LV2_Atom_Long *fraction = NULL;

	lv2_atom_object_get((const LV2_Atom_Object *)atom,
		osc_urid->OSC_timetagIntegral, &integral,
		osc_urid->OSC_timetagFraction, &fraction, 
		0);

	if(  integral && (integral->atom.type == osc_urid->ATOM_Long)
		&& fraction && (fraction->atom.type == osc_urid->ATOM_Long) )
	{
		timetag->integral = integral->body;
		timetag->fraction = fraction->body;
	}
	else
	{
		// set to immediate
		timetag->integral = 0;
		timetag->fraction = 1;
	}

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
lv2_osc_true_get(LV2_OSC_URID *osc_urid __unused, const LV2_Atom *atom)
{
	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
lv2_osc_false_get(LV2_OSC_URID *osc_urid __unused, const LV2_Atom *atom)
{
	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
lv2_osc_nil_get(LV2_OSC_URID *osc_urid __unused, const LV2_Atom *atom)
{
	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
lv2_osc_impulse_get(LV2_OSC_URID *osc_urid __unused, const LV2_Atom *atom)
{
	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
lv2_osc_symbol_get(LV2_OSC_URID *osc_urid __unused, const LV2_Atom *atom,
	LV2_URID *S)
{
	assert(S);
	*S = ((const LV2_Atom_URID *)atom)->body;

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
lv2_osc_midi_get(LV2_OSC_URID *osc_urid __unused, const LV2_Atom *atom,
	uint32_t *size, const uint8_t **m)
{
	assert(size && m);
	*size = atom->size;
	*m = LV2_ATOM_BODY_CONST(atom);

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
lv2_osc_char_get(LV2_OSC_URID *osc_urid __unused, const LV2_Atom *atom, char *c)
{
	assert(c);
	const char *str = LV2_ATOM_CONTENTS_CONST(LV2_Atom_Literal, atom);
	*c = str[0];

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
lv2_osc_rgba_get(LV2_OSC_URID *osc_urid __unused, const LV2_Atom *atom,
	uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *a)
{
	assert(r && g && b && a);
	const char *str = LV2_ATOM_CONTENTS_CONST(LV2_Atom_Literal, atom);

	uint8_t *key [4] = {
		r, g, b, a
	};

	const char *pos = str;
	char *endptr;

	for(unsigned count = 0; count < 4; count++, pos += 2)
	{
		char buf [5] = {'0', 'x', pos[0], pos[1], '\0'};

		*key[count] = strtol(buf, &endptr, 16);
	}

	return lv2_atom_tuple_next(atom);
}

/**
   TODO
*/
static inline bool
lv2_osc_bundle_body_get(LV2_OSC_URID *osc_urid, uint32_t size, const LV2_Atom_Object_Body *body,
	const LV2_Atom_Object **timetag, const LV2_Atom_Tuple **items)
{
	assert(timetag && items);

	*timetag = NULL;
	*items = NULL;

	lv2_atom_object_body_get(size, body,
		osc_urid->OSC_bundleTimetag, timetag,
		osc_urid->OSC_bundleItems, items, 
		0);

	if(!*timetag || ((*timetag)->atom.type != osc_urid->ATOM_Object) || ((*timetag)->body.otype != osc_urid->OSC_Timetag))
		return false;
	if(!*items || ((*items)->atom.type != osc_urid->ATOM_Tuple))
		return false;

	return true;
}

/**
   TODO
*/
static inline bool
lv2_osc_bundle_get(LV2_OSC_URID *osc_urid, const LV2_Atom_Object *obj,
	const LV2_Atom_Object **timetag, const LV2_Atom_Tuple **items)
{
	return lv2_osc_bundle_body_get(osc_urid, obj->atom.size, &obj->body,
		timetag, items);
}

/**
   TODO
*/
static inline bool
lv2_osc_message_body_get(LV2_OSC_URID *osc_urid, uint32_t size, const LV2_Atom_Object_Body *body,
	const LV2_Atom_String **path, const LV2_Atom_Tuple **arguments)
{
	assert(path && arguments);

	*path = NULL;
	*arguments = NULL;

	lv2_atom_object_body_get(size, body,
		osc_urid->OSC_messagePath, path,
		osc_urid->OSC_messageArguments, arguments,
		0);

	if(!*path || ((*path)->atom.type != osc_urid->ATOM_String))
		return false;
	// message without arguments is valid
	if( *arguments && ((*arguments)->atom.type != osc_urid->ATOM_Tuple))
		return false;

	return true;
}

/**
   TODO
*/
static inline bool
lv2_osc_message_get(LV2_OSC_URID *osc_urid, const LV2_Atom_Object *obj,
	const LV2_Atom_String **path, const LV2_Atom_Tuple **arguments)
{
	return lv2_osc_message_body_get(osc_urid, obj->atom.size, &obj->body,
		path, arguments);
}

static inline bool
lv2_osc_body_unroll(LV2_OSC_URID *osc_urid, uint32_t size, const LV2_Atom_Object_Body *body,
	LV2_OSC_Method method, void *data)
{
	if(body->otype == osc_urid->OSC_Bundle)
	{
		const LV2_Atom_Object *timetag = NULL;
		const LV2_Atom_Tuple *items = NULL;

		if(!lv2_osc_bundle_body_get(osc_urid, size, body, &timetag, &items))
			return false;

		LV2_OSC_Timetag tt;
		lv2_osc_timetag_get(osc_urid, &timetag->atom, &tt);

		LV2_ATOM_TUPLE_FOREACH(items, atom)
		{
			const LV2_Atom_Object *obj= (const LV2_Atom_Object *)atom;

			if(!lv2_osc_body_unroll(osc_urid, obj->atom.size, &obj->body, method, data))
				return false;
		}

		return true;
	}
	else if(body->otype == osc_urid->OSC_Message)
	{
		const LV2_Atom_String *path = NULL;
		const LV2_Atom_Tuple *arguments = NULL;

		if(!lv2_osc_message_body_get(osc_urid, size, body, &path, &arguments))
			return false;

		if(method)
			method(LV2_ATOM_BODY_CONST(path), arguments, data);

		return true;
	}

	return false;
}

static inline bool
lv2_osc_unroll(LV2_OSC_URID *osc_urid, const LV2_Atom_Object *obj,
	LV2_OSC_Method method, void *data)
{
	return lv2_osc_body_unroll(osc_urid, obj->atom.size, &obj->body, method, data);
}

#ifdef __cplusplus
} // extern "C"
#endif

#endif // LV2_OSC_UTIL_H