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
|
/*
Written by Adrian Freed, The Center for New Music and Audio Technologies,
University of California, Berkeley. Copyright (c) 1992,93,94,95,96,97,98,99,2000,01,02,03,04
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.
The OSC webpage is http://cnmat.cnmat.berkeley.edu/OpenSoundControl
*/
/* OSC-route.c
Max object for OSC-style dispatching
To-do:
Match a pattern against a pattern?
Declare outlet types / distinguish leaf nodes from other children
More sophisticated (2-pass?) allmessages scheme
set message?
pd
-------------
-- tweaks for Win32 www.zeggz.com/raf 13-April-2002
*/
#ifdef ROCKBOX
#include "plugin.h"
#include "../../pdbox.h"
#else /* ROCKBOX */
#ifdef WIN32
#include <stdlib.h>
#include <string.h>
#endif
#ifdef __APPLE__
#include <stdio.h>
#endif
#ifdef UNIX
#include <stdio.h>
#endif
#endif /* ROCKBOX */
/* structure definition of your object */
#define MAX_NUM 20
#define OSC_ROUTE_VERSION "1.05"
#define OSCWarning(x...) post(x)
/* the required include files */
#include "../src/m_pd.h"
#ifndef TRUE
typedef int Boolean;
#define TRUE 1
#define FALSE 0
#endif
/* Fixed byte width types */
typedef int int4; /* 4 byte int */
Boolean PatternMatch (const char *pattern, const char *test);
/* Version 1.04: Allows #1 thru #9 as typed-in arguments
Version 1.05: Allows "list" messages as well as "message" messages.
*/
static t_class *OSCroute_class;
typedef struct _OSCroute
{
t_object x_obj; // required header
t_int x_num; // Number of address prefixes we store
t_int x_complainmode; // Do we print a message if no match?
t_int x_sendmode; // use pd internal sends instead of outlets
char *x_prefixes[MAX_NUM];
void *x_outlets[MAX_NUM+1];
} t_OSCroute;
t_symbol *ps_list, *ps_complain, *ps_emptySymbol;
/* prototypes */
void OSCroute_doanything(t_OSCroute *x, t_symbol *s, int argc, t_atom *argv);
void OSCroute_anything(t_OSCroute *x, t_symbol *s, int argc, t_atom *argv);
void OSCroute_list(t_OSCroute *x, t_symbol *s, int argc, t_atom *argv);
/* //void *OSCroute_new(t_symbol *s, int argc, atom *argv); */
void *OSCroute_new(t_symbol *s, int argc, t_atom *argv);
void OSCroute_version (t_OSCroute *x);
/* void OSCroute_assist (OSCroute *x, void *box, long msg, long arg, */
/* char *dstString); */
void OSCroute_allmessages(t_OSCroute *x, t_symbol *s, int argc, t_atom *argv);
static char *NextSlashOrNull(char *p);
static void StrCopyUntilSlash(char *target, const char *source);
// free
static void OSCroute_free(t_OSCroute *x)
{
#ifdef ROCKBOX
(void) x;
#endif
// freebytes(x->x_vec, x->x_nelement * sizeof(*x->x_vec));
}
/* initialization routine */
// setup
#ifdef WIN32
OSC_API void OSCroute_setup(void) {
#else
void OSCroute_setup(void) {
#endif
OSCroute_class = class_new(gensym("OSCroute"), (t_newmethod)OSCroute_new,
(t_method)OSCroute_free,sizeof(t_OSCroute), 0, A_GIMME, 0);
class_addlist(OSCroute_class, OSCroute_list);
class_addanything(OSCroute_class, OSCroute_anything);
class_addmethod(OSCroute_class, (t_method)OSCroute_version, gensym("version"), A_NULL, 0, 0);
class_sethelpsymbol(OSCroute_class, gensym("OSCroute-help.pd"));
/*
class_addmethod(OSCroute_class, (t_method)OSCroute_connect,
gensym("connect"), A_SYMBOL, A_FLOAT, 0);
class_addmethod(OSCroute_class, (t_method)OSCroute_disconnect,
gensym("disconnect"), 0);
class_addmethod(OSCroute_class, (t_method)OSCroute_send, gensym("send"),
A_GIMME, 0);
*/
/* ps_list = gensym("list"); */
/* ps_complain = gensym("complain"); */
ps_emptySymbol = gensym("");
post("OSCroute object version " OSC_ROUTE_VERSION " by Matt Wright. pd: jdl Win32 raf.");
post("OSCroute Copyright © 1999 Regents of the University of California. All Rights Reserved.");
}
/* instance creation routine */
void *OSCroute_new(t_symbol *s, int argc, t_atom *argv)
{
#ifdef ROCKBOX
(void) s;
#endif
t_OSCroute *x = (t_OSCroute *)pd_new(OSCroute_class); // get memory for a new object & initialize
int i; //{{raf}} n not used
// EnterCallback();
if (argc > MAX_NUM) {
post("* OSC-route: too many arguments: %ld (max %ld)", argc, MAX_NUM);
// ExitCallback();
return 0;
}
x->x_complainmode = 0;
x->x_num = 0;
for (i = 0; i < argc; ++i) {
if (argv[i].a_type == A_SYMBOL) {
if (argv[i].a_w.w_symbol->s_name[0] == '/') {
/* Now that's a nice prefix */
x->x_prefixes[i] = argv[i].a_w.w_symbol->s_name;
++(x->x_num);
} else if (argv[i].a_w.w_symbol->s_name[0] == '#' &&
argv[i].a_w.w_symbol->s_name[1] >= '1' &&
argv[i].a_w.w_symbol->s_name[1] <= '9') {
/* The Max programmer is trying to make a patch that will be
a subpatch with arguments. We have to make an outlet for this
argument. */
x->x_prefixes[i] = "dummy";
++(x->x_num);
} else {
/* Maybe this is an option we support */
/* if (argv[i].a_w.w_sym == ps_complain) { */
/* x->x_complainmode = 1; */
/* } else { */
/* post("* OSC-route: Unrecognized argument %s", argv[i].a_w.w_sym->s_name); */
/* } */
}
// no LONG
/* } else if (argv[i].a_type == A_FLOAD) { */
/* // Convert to a numeral. Max ints are -2147483648 to 2147483647 */
/* char *string = getbytes(12); */
/* // I can't be bothered to plug this 12 byte memory leak */
/* if (string == 0) { */
/* post("* OSC-route: out of memory!"); */
/* // ExitCallback(); */
/* return 0; */
/* } */
/* sprintf(string, "%d", argv[i].a_w.w_long); */
/* x->x_prefixes[i] = string; */
/* ++(x->x_num); */
} else if (argv[i].a_type == A_FLOAT) {
post("* OSC-route: float arguments are not OK.");
// ExitCallback();
return 0;
} else {
post("* OSC-route: unrecognized argument type!");
// ExitCallback();
return 0;
}
}
/* Have to create the outlets in reverse order */
/* well, not in pd ? */
// for (i = x->x_num-1; i >= 0; --i) {
// for (i = 0; i <= x->x_num-1; i++) {
for (i = 0; i <= x->x_num; i++) {
// x->x_outlets[i] = listout(x);
x->x_outlets[i] = outlet_new(&x->x_obj, &s_list);
}
// ExitCallback();
return (x);
}
void OSCroute_version (t_OSCroute *x) {
#ifdef ROCKBOX
(void) x;
#endif
// EnterCallback();
post("OSCroute Version " OSC_ROUTE_VERSION
", by Matt Wright. pd jdl, win32: raf.\nOSCroute Compiled " __TIME__ " " __DATE__);
// ExitCallback();
}
/* I don't know why these aren't defined in some Max #include file. */
#define ASSIST_INLET 1
#define ASSIST_OUTLET 2
void OSCroute_assist (t_OSCroute *x, void *box, long msg, long arg,
char *dstString) {
#ifdef ROCKBOX
(void) box;
#endif
// EnterCallback();
if (msg==ASSIST_INLET) {
#ifdef ROCKBOX
strcpy(dstString, "Incoming OSC messages");
#else
sprintf(dstString, "Incoming OSC messages");
#endif
} else if (msg==ASSIST_OUTLET) {
if (arg < 0 || arg >= x->x_num) {
post("* OSCroute_assist: No outlet corresponds to arg %ld!", arg);
} else {
#ifdef ROCKBOX
strcpy(dstString, "subaddress + args for prefix ");
strcat(dstString, x->x_prefixes[arg]);
#else
sprintf(dstString, "subaddress + args for prefix %s", x->x_prefixes[arg]);
#endif
}
} else {
post("* OSCroute_assist: unrecognized message %ld", msg);
}
// ExitCallback();
}
void OSCroute_list(t_OSCroute *x, t_symbol *s, int argc, t_atom *argv) {
#ifdef ROCKBOX
(void) s;
#endif
// EnterCallback();
if (argc > 0 && argv[0].a_type == A_SYMBOL) {
/* Ignore the fact that this is a "list" */
OSCroute_doanything(x, argv[0].a_w.w_symbol, argc-1, argv+1);
} else {
// post("* OSC-route: invalid list beginning with a number");
// output on unmatched outlet jdl 20020908
if (argv[0].a_type == A_FLOAT) {
outlet_float(x->x_outlets[x->x_num], argv[0].a_w.w_float);
} else {
post("* OSC-route: unrecognized atom type!");
}
}
// ExitCallback();
}
void OSCroute_anything(t_OSCroute *x, t_symbol *s, int argc, t_atom *argv) {
// EnterCallback();
OSCroute_doanything(x, s, argc, argv);
// ExitCallback();
}
void OSCroute_doanything(t_OSCroute *x, t_symbol *s, int argc, t_atom *argv) {
char *pattern, *nextSlash;
int i;
int matchedAnything;
// post("*** OSCroute_anything(s %s, argc %ld)", s->s_name, (long) argc);
pattern = s->s_name;
if (pattern[0] != '/') {
post("* OSC-route: invalid message pattern %s does not begin with /", s->s_name);
outlet_anything(x->x_outlets[x->x_num], s, argc, argv);
return;
}
matchedAnything = 0;
nextSlash = NextSlashOrNull(pattern+1);
if (*nextSlash == '\0') {
/* last level of the address, so we'll output the argument list */
#ifdef NULL_IS_DIFFERENT_FROM_BANG
if (argc==0) {
post("* OSC-route: why are you matching one level pattern %s with no args?",
pattern);
return;
}
#endif
for (i = 0; i < x->x_num; ++i) {
if (PatternMatch(pattern+1, x->x_prefixes[i]+1)) {
++matchedAnything;
// I hate stupid Max lists with a special first element
if (argc == 0) {
outlet_bang(x->x_outlets[i]);
} else if (argv[0].a_type == A_SYMBOL) {
// Promote the symbol that was argv[0] to the special symbol
outlet_anything(x->x_outlets[i], argv[0].a_w.w_symbol, argc-1, argv+1);
} else if (argc > 1) {
// Multiple arguments starting with a number, so naturally we have
// to use a special function to output this "list", since it's what
// Max originally meant by "list".
outlet_list(x->x_outlets[i], 0L, argc, argv);
} else {
// There was only one argument, and it was a number, so we output it
// not as a list
/* if (argv[0].a_type == A_LONG) { */
/* outlet_int(x->x_outlets[i], argv[0].a_w.w_long); */
// } else
if (argv[0].a_type == A_FLOAT) {
outlet_float(x->x_outlets[i], argv[0].a_w.w_float);
} else {
post("* OSC-route: unrecognized atom type!");
}
}
}
}
} else {
/* There's more address after this part, so our output list will begin with
the next slash. */
t_symbol *restOfPattern = 0; /* avoid the gensym unless we have to output */
char patternBegin[1000];
/* Get the first level of the incoming pattern to match against all our prefixes */
StrCopyUntilSlash(patternBegin, pattern+1);
for (i = 0; i < x->x_num; ++i) {
if (PatternMatch(patternBegin, x->x_prefixes[i]+1)) {
++matchedAnything;
if (restOfPattern == 0) {
restOfPattern = gensym(nextSlash);
}
outlet_anything(x->x_outlets[i], restOfPattern, argc, argv);
}
}
}
if (x->x_complainmode) {
if (!matchedAnything) {
post("* OSC-route: pattern %s did not match any prefixes", pattern);
}
}
// output unmatched data on rightmost outlet a la normal 'route' object, jdl 20020908
if (!matchedAnything) {
outlet_anything(x->x_outlets[x->x_num], s, argc, argv);
}
}
static char *NextSlashOrNull(char *p) {
while (*p != '/' && *p != '\0') {
p++;
}
return p;
}
static void StrCopyUntilSlash(char *target, const char *source) {
while (*source != '/' && *source != '\0') {
*target = *source;
++target;
++source;
}
*target = 0;
}
static int MyStrCopy(char *target, const char *source) {
int i = 0;
while (*source != '\0') {
*target = *source;
++target;
++source;
++i;
}
*target = 0;
return i;
}
void OSCroute_allmessages(t_OSCroute *x, t_symbol *s, int argc, t_atom *argv) {
int i;
t_symbol *prefixSymbol = 0;
char prefixBuf[1000];
char *endOfPrefix;
t_atom a[1];
if (argc >= 1 && argv[0].a_type == A_SYMBOL) {
prefixSymbol = argv[0].a_w.w_symbol;
endOfPrefix = prefixBuf + MyStrCopy(prefixBuf,
prefixSymbol->s_name);
} else {
prefixSymbol = ps_emptySymbol;
prefixBuf[0] = '\0';
endOfPrefix = prefixBuf;
}
for (i = 0; i < x->x_num; ++i) {
post("OSC: %s%s", prefixSymbol->s_name, x->x_prefixes[i]);
MyStrCopy(endOfPrefix, x->x_prefixes[i]);
SETSYMBOL(a, gensym(prefixBuf));
outlet_anything(x->x_outlets[i], s, 1, a);
}
}
/* --------------------------------------------------- */
static const char *theWholePattern; /* Just for warning messages */
static Boolean MatchBrackets (const char *pattern, const char *test);
static Boolean MatchList (const char *pattern, const char *test);
Boolean PatternMatch (const char * pattern, const char * test) {
theWholePattern = pattern;
if (pattern == 0 || pattern[0] == 0) {
return test[0] == 0;
}
if (test[0] == 0) {
if (pattern[0] == '*')
return PatternMatch (pattern+1,test);
else
return FALSE;
}
switch (pattern[0]) {
case 0 : return test[0] == 0;
case '?' : return PatternMatch (pattern + 1, test + 1);
case '*' :
if (PatternMatch (pattern+1, test)) {
return TRUE;
} else {
return PatternMatch (pattern, test+1);
}
case ']' :
case '}' :
OSCWarning("Spurious %c in pattern \".../%s/...\"",pattern[0], theWholePattern);
return FALSE;
case '[' :
return MatchBrackets (pattern,test);
case '{' :
return MatchList (pattern,test);
case '\\' :
if (pattern[1] == 0) {
return test[0] == 0;
} else if (pattern[1] == test[0]) {
return PatternMatch (pattern+2,test+1);
} else {
return FALSE;
}
default :
if (pattern[0] == test[0]) {
return PatternMatch (pattern+1,test+1);
} else {
return FALSE;
}
}
}
/* we know that pattern[0] == '[' and test[0] != 0 */
static Boolean MatchBrackets (const char *pattern, const char *test) {
Boolean result;
Boolean negated = FALSE;
const char *p = pattern;
if (pattern[1] == 0) {
OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern);
return FALSE;
}
if (pattern[1] == '!') {
negated = TRUE;
p++;
}
while (*p != ']') {
if (*p == 0) {
OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern);
return FALSE;
}
if (p[1] == '-' && p[2] != 0) {
if (test[0] >= p[0] && test[0] <= p[2]) {
result = !negated;
goto advance;
}
}
if (p[0] == test[0]) {
result = !negated;
goto advance;
}
p++;
}
result = negated;
advance:
if (!result)
return FALSE;
while (*p != ']') {
if (*p == 0) {
OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern);
return FALSE;
}
p++;
}
return PatternMatch (p+1,test+1);
}
static Boolean MatchList (const char *pattern, const char *test) {
const char *restOfPattern, *tp = test;
for(restOfPattern = pattern; *restOfPattern != '}'; restOfPattern++) {
if (*restOfPattern == 0) {
OSCWarning("Unterminated { in pattern \".../%s/...\"", theWholePattern);
return FALSE;
}
}
restOfPattern++; /* skip close curly brace */
pattern++; /* skip open curly brace */
while (1) {
if (*pattern == ',') {
if (PatternMatch (restOfPattern, tp)) {
return TRUE;
} else {
tp = test;
++pattern;
}
} else if (*pattern == '}') {
return PatternMatch (restOfPattern, tp);
} else if (*pattern == *tp) {
++pattern;
++tp;
} else {
tp = test;
while (*pattern != ',' && *pattern != '}') {
pattern++;
}
if (*pattern == ',') {
pattern++;
}
}
}
}
|