-
Notifications
You must be signed in to change notification settings - Fork 3
/
omax_util.c
672 lines (621 loc) · 15.9 KB
/
omax_util.c
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
Written by John MacCallum, The Center for New Music and Audio Technologies,
University of California, Berkeley. Copyright (c) 2010-12, The Regents of
the University of California (Regents).
Permission to use, copy, modify, distribute, and distribute modified versions
of this software and its documentation without fee and without a signed
licensing agreement, is hereby granted, provided that the above copyright
notice, this paragraph and the following two paragraphs appear in all copies,
modifications, and distributions.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
/** \file omax_util.c
\author John MacCallum
*/
#if defined(__linux__)
#include <string.h>
#else
#ifndef WIN_VERSION
// #include <Carbon/Carbon.h>
#include <CoreServices/CoreServices.h>
#else
#include <windows.h>
#endif
#endif
#include <inttypes.h>
#include "osc.h"
#include "osc_mem.h"
#include "osc_byteorder.h"
#include "osc_match.h"
#include "osc_bundle_s.h"
#include "osc_bundle_u.h"
#include "osc_bundle_iterator_s.h"
#include "osc_bundle_iterator_u.h"
#include "osc_message_u.h"
#include "osc_message_iterator_u.h"
#include "osc_message_s.h"
#include "osc_message_iterator_s.h"
#include "osc_atom_s.h"
#include "omax_util.h"
#include "omax_dict.h"
#ifdef OMAX_PD_VERSION
#define NUMATOMSINMESS 3
#define A_LONG -666
#define A_SYM A_SYMBOL
extern void atom_setfloat(t_atom *atom, t_float f);
extern void atom_setlong(t_atom *atom, long l);
extern void atom_setsym(t_atom *atom, t_symbol *s);
extern t_int atom_getlong(t_atom *atom);
extern t_symbol *atom_getsym(t_atom *atom);
extern t_atomtype atom_gettype(t_atom *atom);
#else // not OMAX_PD_VERSION
#define NUMATOMSINMESS 2
#include "ext.h"
#include "ext_dictionary.h"
#include "ext_dictobj.h"
#endif // OMAX_PD_VERSION
#ifdef WIN_VERSION
// currently we have to compile windows versions with gcc 3 on cygwin and i'm getting undefined
// refs to strsep, so here it is fucker.
char *
strsep(stringp, delim)
register char **stringp;
register const char *delim;
{
register char *s;
register const char *spanp;
register int c, sc;
char *tok;
if ((s = *stringp) == NULL)
return (NULL);
for (tok = s;;) {
c = *s++;
spanp = delim;
do {
if ((sc = *spanp++) == c) {
if (c == 0)
s = NULL;
else
s[-1] = 0;
*stringp = s;
return (tok);
}
} while (sc != 0);
}
/* NOTREACHED */
}
#endif
t_symbol *omax_util_ps_FullPacket = NULL;
//#define __ODOT_PROFILE__
//#include "osc_profile.h"
int omax_util_liboErrorHandler(void *context, const char * const errorstr)
{
// printf("%s context ->> %p err str %s \n", __func__, context, errorstr);
//printf("%s context ->> %p e\n", __func__, context);
//object_error((t_object*)context, "context %p", context);
// stupid max window doesn't respect newlines
int len = strlen(errorstr) + 1;
char buf[len];
strncpy(buf, errorstr, len);
char *s = buf;
char *e = buf;
int line = 0;
while(*e){
if(*e == '\n'){
*e = '\0';
if( line == 0 ){
#ifdef OMAX_PD_VERSION
pd_error(context, "%s", s);
#else
object_error((t_object*)context, "%s", s);
#endif
}else{
#ifdef OMAX_PD_VERSION
pd_error(context, "^ %s", s );
#else
object_error((t_object*)context, "^ %s", s);
#endif
}
line++;
s = e + 1;
}
e++;
}
if(e != s){
#ifdef OMAX_PD_VERSION
pd_error(context, "^ %s", s );
#else
object_error((t_object*)context, "^ %s", s);
#endif
}
return 0;
}
void omax_util_oscLenAndPtr2Atoms(t_atom *argv, long len, char *ptr)
{
#ifdef OMAX_PD_VERSION
uint32_t l = (uint32_t)len;
atom_setfloat(argv, *((t_float *)&l));
#if UINTPTR_MAX == 0xffffffff
uint32_t i2 = (uint32_t)ptr;
float f1 = 0;
float f2 = *((float *)&i2);
#elif UINTPTR_MAX == 0xffffffffffffffff
uint32_t i1 = (uint32_t)((((uint64_t)ptr) & 0xffffffff00000000) >> 32);
uint32_t i2 = (uint32_t)((((uint64_t)ptr) & 0xffffffff));
float f1 = *((float *)&i1);
float f2 = *((float *)&i2);
#else
#error This is BAD BAD BAD
#endif
atom_setfloat(argv+1, f1);
atom_setfloat(argv+2, f2);
#else
atom_setlong(argv, len);
atom_setlong(argv + 1, (long)ptr);
#endif
}
void omax_util_outletOSC(void *outlet, long len, char *ptr)
{
if(!omax_util_ps_FullPacket){
omax_util_ps_FullPacket = gensym("FullPacket");
}
t_atom out[NUMATOMSINMESS];
omax_util_oscLenAndPtr2Atoms(out, len, ptr);
outlet_anything(outlet, omax_util_ps_FullPacket, NUMATOMSINMESS, out);
}
t_osc_err omax_util_outletOSC_u(void *outlet, t_osc_bndl_u *bndl)
{
if(!omax_util_ps_FullPacket){
omax_util_ps_FullPacket = gensym("FullPacket");
}
//long len = 0;
//char *ptr = NULL;
//t_osc_err e = osc_bundle_u_serialize(bndl, &len, &ptr);
//if(e){
//return e;
//}
t_osc_bndl_s *bs = osc_bundle_u_serialize(bndl);
if(bs){
omax_util_outletOSC(outlet, osc_bundle_s_getLen(bs), osc_bundle_s_getPtr(bs));
osc_bundle_s_deepFree(bs);
}
return OSC_ERR_NONE;
}
int omax_util_getNumAtomsInOSCMsg(t_osc_msg_s *m)
{
int n = 1; // address;
t_osc_msg_it_s *it = osc_msg_it_s_get(m);
while(osc_msg_it_s_hasNext(it)){
t_osc_atom_s *a = osc_msg_it_s_next(it);
switch(osc_atom_s_getTypetag(a)){
case OSC_BUNDLE_TYPETAG:
n += (NUMATOMSINMESS+1); // FullPacket <len> <address>
break;
case 'b':
{
char *data = osc_atom_s_getData(a);
n += ntoh32(*((int32_t *)data)) + 1;
}
break;
default:
n += 1;
break;
}
}
osc_msg_it_s_destroy(it);
return n;
}
#ifdef OMAX_PD_VERSION
int omax_util_braceError(char *s)
{
int i, len = strlen(s);
for( i = 0; i < len; i++ )
{
if(s[i] == '{' || s[i] == '}')
return 1;
}
return 0;
}
void omax_util_hashBrackets2Curlies(char *s)
{
char c;
int len = strlen(s);
int i, j = 0;
for( i = 0; i < len; i++ )
{
c = s[i];
if( i < len-1 )
{
if(s[i] == '#' && s[i+1] == '['){
c = '{';
i++;
} else if(s[i] == ']' && s[i+1] == '#') {
c = '}';
i++;
}
}
s[j++] = c;
}
while(j < len)
s[j++] = '\0';
}
void omax_util_curlies2hashBrackets(char **ptr, long bufsize)
{
// printf("%s", __func__);
char *str = (*ptr);
if(!str)
{
pd_error(NULL, "no string in buffer");
return;
}
int i, j = 0;
int len = strlen(str);
char buf[len * 2]; //<< max possible size with every character being a { or }
memset(buf, '\0', len * 2);
for( i = 0; i < len; i++ )
{
if (str[i] == '{')
{
buf[j++] = '#';
buf[j++] = '[';
}
else if (str[i] == '}')
{
buf[j++] = ']';
buf[j++] = '#';
} else {
buf[j++] = str[i];
}
}
if(j != i)
{
memset(*ptr, '\0', bufsize);
// *ptr = (char *)realloc(*ptr, sizeof(char) * j);
strcpy(*ptr, buf);
}
}
#endif
int omax_util_oscMsg2MaxAtoms(t_osc_msg_s *m, t_atom *av)
{
t_atom *ptr = av;
if(osc_message_s_getAddress(m)){
atom_setsym(ptr, gensym(osc_message_s_getAddress(m)));
}else{
// there are some cases when a message won't have an address
}
ptr++;
t_osc_msg_it_s *it = osc_msg_it_s_get(m);
while(osc_msg_it_s_hasNext(it)){
t_osc_atom_s *a = osc_msg_it_s_next(it);
switch(osc_atom_s_getTypetag(a)){
case 'c':
case 'C':
case 'i':
case 'I':
case 'h':
case 'H':
case 'u':
case 'U':
case 'T':
case 'F':
case 'N':
atom_setlong(ptr++, osc_atom_s_getInt32(a));
break;
case 'f':
case 'd':
atom_setfloat(ptr++, osc_atom_s_getDouble(a));
break;
case 't':
case 's':
{
int len = osc_atom_s_getStringLen(a);
char buf[len + 1];
char *bufptr = buf;
osc_atom_s_getString(a, len + 1, &bufptr);
#ifdef OMAX_PD_VERSION
int maxsize = (len * 2) + 1; // maximum possible size if all characters are {} + \0
char extrabuf[maxsize];
strcpy(extrabuf, bufptr);
char *pdbufptr = extrabuf;
omax_util_curlies2hashBrackets(&pdbufptr, maxsize);
if(omax_util_braceError(pdbufptr))
return 1;
atom_setsym(ptr++, gensym(extrabuf));
#else
atom_setsym(ptr++, gensym(buf));
#endif
}
break;
case 'b':
{
char *blob = osc_atom_s_getBlob(a);
int32_t size = ntoh32(*((int32_t *)blob));
atom_setlong(ptr++, size);
for(int j = 0; j < size; j++){
atom_setlong(ptr++, (long)blob[j + 4]);
}
}
break;
case OSC_BUNDLE_TYPETAG:
{
char *data = osc_atom_s_getData(a);
atom_setsym(ptr++, gensym("FullPacket"));
t_atom bnddata[NUMATOMSINMESS];
omax_util_oscLenAndPtr2Atoms(bnddata, (long)ntoh32(*((uint32_t *)data)), (char *)((long)(data + 4)));
int i;
for(i = 0; i < NUMATOMSINMESS; i++)
{
*ptr++ = bnddata[i];
}
/*
atom_setlong(ptr++, ntoh32(*((uint32_t *)data)));
atom_setlong(ptr++, (long)(data + 4));
*/
}
break;
default:
#ifdef OMAX_PD_VERSION
pd_error(NULL, "unknown OSC type %c (%d)\n", osc_atom_s_getTypetag(a), osc_atom_s_getTypetag(a));
#else
error("unknown OSC type %c (%d)\n", osc_atom_s_getTypetag(a), osc_atom_s_getTypetag(a));
#endif
}
}
osc_msg_it_s_destroy(it);
return 0;
}
// encode a FullPacket <len> <ptr> message as a nested bundle
void omax_util_maxFullPacketToOSCAtom_u(t_osc_atom_u **osc_atom, t_atom *len, t_atom *ptr)
{
if(!(*osc_atom)){
*osc_atom = osc_atom_u_alloc();
}
#ifdef OMAX_PD_VERSION
float ff = atom_getfloat(len);
long l = (long)*((uint32_t *)&ff);
ff = atom_getfloat(ptr);
uint64_t l1 = *((uint64_t *)&ff);
l1 <<= 32;
ff = atom_getfloat(ptr+1);
uint64_t l2 = *((uint64_t *)&ff);
char *p = (char *)(l1 | l2);
#else
long l = atom_getlong(len);
char *p = (char *)atom_getlong(ptr);
#endif
osc_atom_u_setBndl(*osc_atom, l, p);
}
void omax_util_maxAtomToOSCAtom_u(t_osc_atom_u **osc_atom, t_atom *max_atom)
{
if(!(*osc_atom)){
*osc_atom = osc_atom_u_alloc();
}
switch(atom_gettype(max_atom)){
case A_FLOAT:
osc_atom_u_setDouble(*osc_atom, atom_getfloat(max_atom));
break;
case A_LONG:
osc_atom_u_setInt32(*osc_atom, atom_getlong(max_atom));
break;
#ifndef OMAX_PD_VERSION
case A_OBJ:
{
t_dictionary *dict2 = object_dictionaryarg(1, max_atom);
if(dict2){
t_osc_bndl_u *bndl2_u = osc_bundle_u_alloc();
omax_dict_dictionaryToOSC(dict2, bndl2_u);
osc_atom_u_setBndl_u(*osc_atom, bndl2_u);
//long len = 0;
//char *bndl2 = NULL;
//osc_bundle_u_serialize(bndl2_u, &len, &bndl2);
//t_osc_msg_u *msg = osc_message_u_alloc();
//osc_message_u_setAddress(msg, addy);
//osc_message_u_appendBndl(msg, len, bndl2);
//osc_bundle_u_addMsg(bndl_u, msg);
//osc_bundle_u_free(bndl2_u);
//osc_mem_free(bndl2);
//continue;
}
}
break;
#endif
case A_SYM:
{
#ifdef OMAX_PD_VERSION
t_symbol *sym = atom_getsym(max_atom);
if(sym && sym->s_name)
{
char buf[ strlen(sym->s_name) + 1 ];
strcpy(buf, sym->s_name);
omax_util_hashBrackets2Curlies(buf);
osc_atom_u_setString(*osc_atom, buf);
break;
}
#else
t_symbol *s = atom_getsym(max_atom);
if(s && s->s_name){
osc_atom_u_setString(*osc_atom, s->s_name);
break;
}
#endif \
// intentional fall-through to default
}
default:
osc_atom_u_free(*osc_atom);
*osc_atom = NULL;
break;
}
}
t_osc_err omax_util_maxAtomsToOSCMsg_u(t_osc_msg_u **msg, t_symbol *address, int argc, t_atom *argv)
{
if(!(*msg)){
*msg = osc_message_u_alloc();
if(address){
t_osc_err e = osc_message_u_setAddress(*msg, address->s_name);
if(e){
return e;
}
}
int i;
for(i = 0; i < argc; i++){
t_osc_atom_u *a = NULL;
if(atom_gettype(argv + i) == A_SYM){
if((atom_getsym(argv + i) == gensym("FullPacket")) && argc - i >= 3){
// FullPacket to be encoded as nested bundle
omax_util_maxFullPacketToOSCAtom_u(&a, argv + 1, argv + 2);
#ifdef OMAX_PD_VERSION
i += 3;
#else
i += 2;
#endif
}else{
omax_util_maxAtomToOSCAtom_u(&a, argv + i);
}
}else{
omax_util_maxAtomToOSCAtom_u(&a, argv + i);
}
if(a){
osc_message_u_appendAtom(*msg, a);
}
}
}
return OSC_ERR_NONE;
}
t_osc_err omax_util_copyBundleWithSubs_u(t_osc_bndl_u **dest, t_osc_bndl_u *src, int argc, t_atom *argv, int *hassubs)
{
if(!src){
return -1;
}
*hassubs = 0;
t_osc_bndl_u *bcopy = osc_bundle_u_alloc();
t_osc_bndl_it_u *bit = osc_bndl_it_u_get(src);
while(osc_bndl_it_u_hasNext(bit)){
t_osc_msg_u *m = osc_bndl_it_u_next(bit);
t_osc_msg_u *mcopy = osc_message_u_alloc();
{
char *address = osc_message_u_getAddress(m);
int addresslen = strlen(address) + 1;
char copy[addresslen];
char *copyptr = copy;
memcpy(copy, address, addresslen);
int newaddresslen = addresslen;
int dosub = 0;
int addedtolist = 0;
char *tok = NULL;
while((tok = strsep(©ptr, "$"))){
if(copyptr){
if(copyptr[0] < 58 && copyptr[0] > 47){
dosub = 1;
char *endp = NULL;
long l = strtol(copyptr, &endp, 0) - 1;
if(l < argc){
switch(atom_gettype(argv + l)){
case A_LONG:
newaddresslen += snprintf(NULL, 0, "%lld", (long long)atom_getlong(argv + l));
break;
case A_FLOAT:
newaddresslen += snprintf(NULL, 0, "%f", atom_getfloat(argv + l));
break;
case A_SYM:
newaddresslen += strlen(atom_getsym(argv + l)->s_name);
break;
default:
break;
}
}else{
newaddresslen += snprintf(NULL, 0, "$%ld", l + 1);
}
}else{
newaddresslen++;
}
}
}
if(dosub){
*hassubs = 1;
//newaddresslen += 16; // never can be too careful...
char *newaddress = osc_mem_alloc(newaddresslen);
char *ptr = newaddress;
memcpy(copy, address, addresslen);
copyptr = copy;
char *lasttok = copy, *tok = NULL;
while((tok = strsep(©ptr, "$"))){
ptr += sprintf(ptr, "%s", lasttok);
if(copyptr){
if(copyptr[0] < 58 && copyptr[0] > 47){
char *endp = NULL;
long l = strtol(copyptr, &endp, 0) - 1;
if(l < argc){
switch(atom_gettype(argv + l)){
case A_LONG:
ptr += sprintf(ptr, "%lld", (long long)atom_getlong(argv + l));
break;
case A_FLOAT:
ptr += sprintf(ptr, "%f", atom_getfloat(argv + l));
break;
case A_SYM:
ptr += sprintf(ptr, "%s", atom_getsym(argv + l)->s_name);
break;
default:
break;
}
}else{
ptr += sprintf(ptr, "$%ld", l + 1);
}
lasttok = endp;
}else{
*ptr++ = '$';
*ptr = '\0';
}
}
}
osc_message_u_setAddressPtr(mcopy, newaddress, NULL);
}else{
osc_message_u_setAddress(mcopy, osc_message_u_getAddress(m));
}
}
t_osc_msg_it_u *mit = osc_msg_it_u_get(m);
while(osc_msg_it_u_hasNext(mit)){
t_osc_atom_u *a = osc_msg_it_u_next(mit);
t_osc_atom_u *acopy = NULL;
if(osc_atom_u_getTypetag(a) == 's'){
char *s = osc_atom_u_getStringPtr(a);
if(s && s[0] == '$' && strlen(s) > 1){
long l = strtol(s + 1, NULL, 0);
if(l > 0){
*hassubs = 1;
if(l < argc + 1){
omax_util_maxAtomToOSCAtom_u(&acopy, argv + (l - 1));
}else{
acopy = osc_atom_u_copy(a);
}
}else{
acopy = osc_atom_u_copy(a);
}
}else{
acopy = osc_atom_u_copy(a);
}
}else if(osc_atom_u_getTypetag(a) == OSC_BUNDLE_TYPETAG){
t_osc_bndl_u *ncopy = NULL;
omax_util_copyBundleWithSubs_u(&ncopy, osc_atom_u_getBndl(a), argc, argv, hassubs);
acopy = osc_atom_u_alloc();
osc_atom_u_setBndl_u(acopy, ncopy);
}else{
acopy = osc_atom_u_copy(a);
}
osc_message_u_appendAtom(mcopy, acopy);
}
osc_msg_it_u_destroy(mit);
osc_bundle_u_addMsg(bcopy, mcopy);
}
osc_bndl_it_u_destroy(bit);
*dest = bcopy;
return OSC_ERR_NONE;
}