x_interface.c 27.6 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1
2
3
4
5
6
7
/* Copyright (c) 1997-1999 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* interface objects */

#include "m_pd.h"
8
#include "m_imp.h"
9
#include "g_canvas.h"
10
#include "s_stuff.h"
11
12
13
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
Miller Puckette's avatar
Miller Puckette committed
14

15
16
17
18
19
20
21
/* we need the following for [pdinfo] ... */

#define MAXNDEV 20
#define DEVDESCSIZE 80

extern t_class *classtable_findbyname(t_symbol *s);

Miller Puckette's avatar
Miller Puckette committed
22
/* -------------------------- print ------------------------------ */
23
t_class *print_class;
Miller Puckette's avatar
Miller Puckette committed
24
25
26
27
28
29
30

typedef struct _print
{
    t_object x_obj;
    t_symbol *x_sym;
} t_print;

31
static void *print_new(t_symbol *sel, int argc, t_atom *argv)
Miller Puckette's avatar
Miller Puckette committed
32
33
{
    t_print *x = (t_print *)pd_new(print_class);
34
35
36
37
38
39
40
41
42
43
    if (argc == 0)
        x->x_sym = gensym("print");
    else if (argc == 1 && argv->a_type == A_SYMBOL)
    {
        t_symbol *s = atom_getsymbolarg(0, argc, argv);
        if (!strcmp(s->s_name, "-n"))
            x->x_sym = &s_;
        else x->x_sym = s;
    }
    else
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
44
    {
45
46
        int bufsize;
        char *buf;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
47
48
49
        t_binbuf *bb = binbuf_new();
        binbuf_add(bb, argc, argv);
        binbuf_gettext(bb, &buf, &bufsize);
50
        buf = resizebytes(buf, bufsize, bufsize+1);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
51
52
        buf[bufsize] = 0;
        x->x_sym = gensym(buf);
53
        freebytes(buf, bufsize+1);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
54
55
        binbuf_free(bb);
    }
Miller Puckette's avatar
Miller Puckette committed
56
57
58
59
60
    return (x);
}

static void print_bang(t_print *x)
{
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
61
    post("%s: bang", x->x_sym->s_name);
Miller Puckette's avatar
Miller Puckette committed
62
63
64
65
}

static void print_pointer(t_print *x, t_gpointer *gp)
{
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
66
    post("%s: (gpointer)", x->x_sym->s_name);
Miller Puckette's avatar
Miller Puckette committed
67
68
69
70
}

static void print_float(t_print *x, t_float f)
{
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
71
    post("%s: %g", x->x_sym->s_name, f);
Miller Puckette's avatar
Miller Puckette committed
72
73
74
75
76
}

static void print_list(t_print *x, t_symbol *s, int argc, t_atom *argv)
{
    int i;
77
78
79
80
    if (argc && argv->a_type != A_SYMBOL)
		startpost("%s%s%g", x->x_sym->s_name,
            (*x->x_sym->s_name ? ": " : ""),
            atom_getfloatarg(0, argc--, argv++));
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
81
    else startpost("%s: %s", x->x_sym->s_name,
Miller Puckette's avatar
Miller Puckette committed
82
83
84
85
86
87
88
89
90
        (argc > 1 ? s_list.s_name : (argc == 1 ? s_symbol.s_name :
            s_bang.s_name)));
    postatom(argc, argv);
    endpost();
}

static void print_anything(t_print *x, t_symbol *s, int argc, t_atom *argv)
{
    int i;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
91
    startpost("%s: %s", x->x_sym->s_name, s->s_name);
Miller Puckette's avatar
Miller Puckette committed
92
93
94
95
96
97
98
    postatom(argc, argv);
    endpost();
}

static void print_setup(void)
{
    print_class = class_new(gensym("print"), (t_newmethod)print_new, 0,
99
        sizeof(t_print), 0, A_GIMME, 0);
Miller Puckette's avatar
Miller Puckette committed
100
101
102
103
104
105
106
    class_addbang(print_class, print_bang);
    class_addfloat(print_class, print_float);
    class_addpointer(print_class, print_pointer);
    class_addlist(print_class, print_list);
    class_addanything(print_class, print_anything);
}

107
108
/* -----------------canvasinfo, pdinfo, classinfo ------------------------------ */
static t_class *canvasinfo_class;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
109

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
typedef struct _canvasinfo {
    t_object x_obj;
    t_canvas *x_canvas;
    t_float   x_depth;
} t_canvasinfo;

static t_class *pdinfo_class;

typedef struct _pdinfo {
    t_object x_obj;
} t_pdinfo;

static t_class *classinfo_class;

typedef struct _classinfo {
    t_object x_obj;
126
127
    t_outlet *x_out2;
    t_symbol *x_name;
128
129
130
} t_classinfo;

/* used by all the *info objects */
131
132
133
134

static t_int info_to_console = 0;

void info_out(t_text *te, t_symbol *s, int argc, t_atom *argv)
135
{
136
    if (info_to_console)
137
138
139
140
141
142
143
144
145
146
147
148
    {
        startpost("%s:", s->s_name);
        if (argc > 0)
            postatom(argc, argv);
        endpost();
    }
    else {
        outlet_list(te->ob_outlet,
            &s_list, argc, argv);
    }
}

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
void info_print(t_text *te)
{
    t_class *c = classtable_findbyname(te->ob_pd->c_name);
    if(c == NULL)
    {
        pd_error(te, "s: can't find entry in classtable");
        return;
    }
    info_to_console = 1;
    t_int i;
    t_methodentry *m;
    for(i = c->c_nmethod, m = c->c_methods; i; i--, m++)
        if(m->me_name != gensym("print"))
            (m->me_fun)(te, m->me_name, 0, 0);
/*  pd_forwardmess(te->te_pd, 0, 0); not sure why this doesn't work */
    info_to_console = 0;
}

167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* -------------------------- canvasinfo ------------------------------ */
t_canvas *canvasinfo_dig(t_canvasinfo *x)
{
  int depth = (int)x->x_depth;
  t_canvas *c = x->x_canvas;
  if(depth<0) depth = 0;

  while(depth && c->gl_owner) {
    c = c->gl_owner;
    depth--;
  }
  return c;
}

181
void canvasinfo_args(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
182
183
{
    t_canvas *c = canvasinfo_dig(x);
184
185
186
187
188
189
190
    int n = 0;
    t_atom *a = 0;
    t_binbuf *b;
    if(!c) return;
    if (s == gensym("args")) c = canvas_getrootfor(c);
    b = c->gl_obj.te_binbuf;
    if(!b)
191
    {
192
        info_out((t_text *)x, s, 0, 0);
193
    }
194
    else
195
    {
196
197
198
199
        n = binbuf_getnatom(b);
        a = binbuf_getvec(b);
        if (s == gensym("args"))
            info_out((t_text *)x, s, n-1, a+1);
200
        else
201
            info_out((t_text *)x, s, n, a);
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
}

void canvasinfo_coords(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
{
    t_canvas *c = canvasinfo_dig(x);
    t_int gop = c->gl_isgraph + c->gl_hidetext;
    t_atom at[9];
    SETFLOAT(at, (c->gl_isgraph) ? c->gl_x1 : 0);
    SETFLOAT(at+1, (c->gl_isgraph) ? c->gl_y1 : 0);
    SETFLOAT(at+2, (c->gl_isgraph) ? c->gl_x2 : 0);
    SETFLOAT(at+3, (c->gl_isgraph) ? c->gl_y2 : 0);
    SETFLOAT(at+4, (c->gl_isgraph) ? c->gl_pixwidth : 0);
    SETFLOAT(at+5, (c->gl_isgraph) ? c->gl_pixheight : 0);
    SETFLOAT(at+6, (c->gl_isgraph) ? gop : 0);
    SETFLOAT(at+7, (c->gl_isgraph) ? c->gl_xmargin : 0);
    SETFLOAT(at+8, (c->gl_isgraph) ? c->gl_ymargin : 0);
    info_out((t_text *)x, s, 9, at);
}

void canvasinfo_dir(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
{
    t_canvas *c = canvasinfo_dig(x);
    c = canvas_getrootfor(c);
    t_atom at[1];
    SETSYMBOL(at, canvas_getdir(c));
    info_out((t_text *)x, s, 1, at);
}

void canvasinfo_dirty(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
{
    t_canvas *c = canvasinfo_dig(x);
    t_atom at[1];
    SETFLOAT(at, c->gl_dirty);
    info_out((t_text *)x, s, 1, at);
}

void canvasinfo_dollarzero(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
{
    t_canvas *c = canvasinfo_dig(x);
    c = canvas_getrootfor(c);
    t_symbol *d = gensym("$0");
    t_symbol *ret = canvas_realizedollar(c, d);
    float f = (float)strtod(ret->s_name,NULL);
    t_atom at[1];
    SETFLOAT(at, f);
    info_out((t_text *)x, s, 1, at);
}

void canvasinfo_editmode(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
{
    t_canvas *c = canvasinfo_dig(x);
    t_atom at[1];
    SETFLOAT(at, c->gl_edit);
    info_out((t_text *)x, s, 1, at);
}

void canvasinfo_filename(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
{
    t_canvas *c = canvasinfo_dig(x);
    c = canvas_getrootfor(c);
    t_atom at[1];
    SETSYMBOL(at, c->gl_name);
    info_out((t_text *)x, s, 1, at);
}

void canvasinfo_hitbox(t_canvasinfo *x, t_floatarg xpos, t_floatarg ypos)
{
    t_canvas *c = canvasinfo_dig(x);
    int x1, y1, x2, y2;
    t_gobj *ob = canvas_findhitbox(c, xpos, ypos, &x1, &y1, &x2, &y2);
    if (ob)
274
    {
275
276
277
278
279
280
281
282
        t_atom at[5];
        char *classname = class_getname(ob->g_pd);
        SETSYMBOL(at, gensym(classname));
        SETFLOAT(at+1, (t_float)x1);
        SETFLOAT(at+2, (t_float)y1);
        SETFLOAT(at+3, (t_float)x2);
        SETFLOAT(at+4, (t_float)y2);
        info_out((t_text *)x, gensym("hitbox"), 5, at);
283
284
    }
    else
285
        info_out((t_text *)x, gensym("hitbox"), 0, 0);
286
287
}

288
void canvasinfo_name(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
289
{
290
291
292
293
294
295
    t_canvas *c = canvasinfo_dig(x);
    char buf[MAXPDSTRING];
    snprintf(buf, MAXPDSTRING, ".x%lx", (long unsigned int)c);
    t_atom at[1];
    SETSYMBOL(at, gensym(buf));
    info_out((t_text *)x, s, 1, at);
296
297
}

298
void canvasinfo_pointer(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
299
{
300
301
302
303
304
305
306
307
    t_canvas *c = canvasinfo_dig(x);
    t_atom at[1];
    t_gpointer gp;
    gpointer_init(&gp);
    gpointer_setglist(&gp, c, 0);
    SETPOINTER(at, &gp);
    info_out((t_text *)x, s, 1, at);
    gpointer_unset(&gp);
308
309
}

310
311
void canvasinfo_posonparent(t_canvasinfo *x, t_symbol *s,
    int argc, t_atom *argv)
312
{
313
314
315
316
317
    t_canvas *c = canvasinfo_dig(x);
    t_atom at[2];
    SETFLOAT(at, c->gl_obj.te_xpix);
    SETFLOAT(at+1, c->gl_obj.te_ypix);
    info_out((t_text *)x, s, 2, at);
318
319
}

320
void canvasinfo_screenpos(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
321
{
322
323
324
325
326
327
328
    t_canvas *c = canvasinfo_dig(x);
    t_atom at[4];
    SETFLOAT(at, c->gl_screenx1);
    SETFLOAT(at+1, c->gl_screeny1);
    SETFLOAT(at+2, c->gl_screenx2);
    SETFLOAT(at+3, c->gl_screeny2);
    info_out((t_text *)x, s, 4, at);
329
330
}

331
void canvasinfo_toplevel(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
332
{
333
334
335
336
337
    t_canvas *c = canvasinfo_dig(x);
    t_float f = c->gl_owner ? 0 : 1;
    t_atom at[1];
    SETFLOAT(at, f);
    info_out((t_text *)x, s, 1, at);
338
339
}

340
void canvasinfo_vis(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
341
{
342
343
344
345
346
347
348
349
350
    t_canvas *c = canvasinfo_dig(x);
    t_atom at[1];
    SETFLOAT(at, glist_isvisible(c));
    info_out((t_text *)x, s, 1, at);
}

void canvasinfo_print(t_canvasinfo *x)
{
    info_print((t_text *)x);
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
}

void *canvasinfo_new(t_floatarg f)
{
    t_canvasinfo *x = (t_canvasinfo *)pd_new(canvasinfo_class);
    t_glist *glist = (t_glist *)canvas_getcurrent();
    t_canvas *c = (t_canvas*)glist_getcanvas(glist);
    x->x_canvas = c;
    floatinlet_new(&x->x_obj, &x->x_depth);
    outlet_new(&x->x_obj, &s_list);
    x->x_depth = f;
    return (void *)x;
}

void canvasinfo_setup(void)
{
    canvasinfo_class = class_new(gensym("canvasinfo"),
        (t_newmethod)canvasinfo_new, 0,
        sizeof(t_canvasinfo),
        CLASS_DEFAULT, A_DEFFLOAT, 0);

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
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_args,
        gensym("args"), A_GIMME, 0);
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_args,
        gensym("boxtext"), A_GIMME, 0);
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_coords,
        gensym("coords"), A_GIMME, 0);
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_dir,
        gensym("dir"), A_GIMME, 0);
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_dirty,
        gensym("dirty"), A_GIMME, 0);
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_dollarzero,
        gensym("dollarzero"), A_GIMME, 0);
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_editmode,
        gensym("editmode"), A_GIMME, 0);
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_filename,
        gensym("filename"), A_GIMME, 0);
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_hitbox,
        gensym("hitbox"), A_DEFFLOAT, A_DEFFLOAT, 0);
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_name,
        gensym("name"), A_GIMME, 0);
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_pointer,
        gensym("pointer"), A_GIMME, 0);
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_posonparent,
        gensym("posonparent"), A_GIMME, 0);
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_screenpos,
        gensym("screenpos"), A_GIMME, 0);
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_toplevel,
        gensym("toplevel"), A_GIMME, 0);
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_vis,
        gensym("vis"), A_GIMME, 0);
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_print,
        gensym("print"), 0);

    post("canvasinfo: v0.1");
    post("stable canvasinfo methods: args dir dirty editmode vis");

408
409
410
411
412
}

/* -------------------------- pdinfo ------------------------------ */
static t_class *pdinfo_class;

413
t_symbol *getapiname(int id)
414
{
415
416
    t_symbol *s = 0;
    switch (id)
417
    {
418
419
420
421
422
423
424
425
426
427
        case API_NONE: s = gensym("none"); break;
        case API_ALSA: s = gensym("ALSA"); break;
        case API_OSS: s = gensym("OSS"); break;
        case API_MMIO: s = gensym("MMIO"); break;
        case API_PORTAUDIO: s = gensym("PortAudio"); break;
        case API_JACK: s = gensym("JACK"); break;
        case API_SGI: s = gensym("SGI"); break;
        //case API_AUDIOUNIT: s = gensym("AudioUnit"); break;
        //case API_ESD: s = gensym("ESD"); break;
        //case API_DUMMY: s = gensym("dummy"); break;
428
    }
429
430
431
432
433
434
435
436
    return s;
}

void pdinfo_dir(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
{
    t_atom at[1];
    t_symbol *dir = pd_getdirname();
    if (!dir)
437
    {
438
439
        pd_error(x, "pdinfo: can't find pd's directory");
        return;
440
    }
441
442
    SETSYMBOL(at, dir);
    info_out((t_text *)x, s, 1, at);
443
444
}

445
void pdinfo_dsp(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
446
{
447
448
449
    t_atom at[1];
    SETFLOAT(at, (t_float)canvas_dspstate);
    info_out((t_text *)x, s, 1, at);
450
451
}

452
void pdinfo_audio_api(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
453
{
454
455
456
457
    t_atom at[1];
    t_symbol *api = getapiname(sys_audioapi);
    SETSYMBOL(at, api);
    info_out((t_text *)x, s, 1, at);
458
459
}

460
void pdinfo_audioin(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
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
//        char i
}

void pdinfo_audio_api_list_raw(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
{
    t_atom at[7];
    int i;
    for(i = 0; i < 7; i++)
        SETSYMBOL(at+i, getapiname(i));
    info_out((t_text *)x, s, i, at);
}

void pdinfo_audio_apilist(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
{
    t_atom at[8];
    int n = 0;
#ifdef USEAPI_OSS
    SETSYMBOL(at+n, getapiname(API_OSS)); n++;
#endif
#ifdef USEAPI_MMIO
    SETSYMBOL(at+n, getapiname(API_MMIO)); n++;
#endif
#ifdef USEAPI_ALSA
    SETSYMBOL(at+n, getapiname(API_ALSA)); n++;
#endif
#ifdef USEAPI_PORTAUDIO
#ifdef MSW
    SETSYMBOL(at+n, getapiname(API_PORTAUDIO));
#else
#ifdef OSX
    SETSYMBOL(at+n, getapiname(API_PORTAUDIO));
#else
    SETSYMBOL(at+n, getapiname(API_PORTAUDIO));
#endif
#endif
    n++;
#endif
#ifdef USEAPI_JACK
    SETSYMBOL(at+n, getapiname(API_JACK)); n++;
#endif
#ifdef USEAPI_AUDIOUNIT
    SETSYMBOL(at+n, getapiname(API_AUDIOUNIT)); n++;
#endif
#ifdef USEAPI_ESD
    SETSYMBOL(at+n, getapiname(API_ESD)); n++;
#endif
#ifdef USEAPI_DUMMY
    SETSYMBOL(at+n, getapiname(API_DUMMY)); n++;
#endif
    info_out((t_text *)x, s, n, at);
}

void pdinfo_audio_listdevs(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
{
    char indevlist[MAXNDEV*DEVDESCSIZE], outdevlist[MAXNDEV*DEVDESCSIZE];
    int nindevs = 0, noutdevs = 0, i, canmulti = 0, cancallback = 0;
    sys_get_audio_devs(indevlist, &nindevs,
            outdevlist, &noutdevs,
            &canmulti, &cancallback,
            MAXNDEV, DEVDESCSIZE);
    t_atom at[4];
    if (s == gensym("audio-multidev-support"))
524
    {
525
526
        SETFLOAT(at, canmulti);
        info_out((t_text *)x, s, 1, at); 
527
    }
528
    else if (s == gensym("audio-indevlist"))
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
        for (i = 0; i < nindevs; i++)
           SETSYMBOL(at+i, gensym(indevlist + i * DEVDESCSIZE));
        info_out((t_text *)x, s, i, at);
    }
    else if (s == gensym("audio-outdevlist"))
    {
        for (i = 0; i < noutdevs; i++)
           SETSYMBOL(at+i, gensym(outdevlist + i * DEVDESCSIZE));
        info_out((t_text *)x, s, i, at);
    }
}

void pdinfo_audio_dev(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
{
    int devno;
    if (argc) devno = (int)atom_getfloatarg(0, argc, arg);
    else devno = 0;
    int naudioindev, audioindev[MAXAUDIOINDEV], chindev[MAXAUDIOINDEV];
    int naudiooutdev, audiooutdev[MAXAUDIOOUTDEV], choutdev[MAXAUDIOOUTDEV];
    int rate, advance, callback, blocksize;
    sys_get_audio_params(&naudioindev, audioindev, chindev,
        &naudiooutdev, audiooutdev, choutdev, &rate, &advance, &callback);
    int *dev, *chan, ndev;
    if (s == gensym("audio-indev"))
        dev = audioindev, chan = chindev, ndev = naudioindev;
    else
        dev = audiooutdev, chan = choutdev, ndev = naudiooutdev;
    if (devno >= 0 && devno < ndev)
    {
        t_atom at[2];
        SETFLOAT(at, (t_float)dev[devno]);
        SETFLOAT(at+1, (t_float)chan[devno]);
        info_out((t_text *)x, s, 2, at);
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
    else
        info_out((t_text *)x, s, 0, 0);
}

void pdinfo_midi_api(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
{
    t_atom at[1];
    t_symbol *api, *def = gensym("DEFAULT");
#ifdef USEAPI_OSS
    def = gensym("OSS");
#endif
    api = sys_midiapi ? gensym("ALSA") : def;
    SETSYMBOL(at, api);
    info_out((t_text *)x, s, 1, at);
}

void pdinfo_midi_apilist(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
{
    t_atom at[8];
    int n = 0;
    SETSYMBOL(at+n, gensym("DEFAULT"));
#ifdef USEAPI_OSS
    SETSYMBOL(at+n, gensym("OSS"));
#endif
    n++;
#ifdef USEAPI_ALSA
    SETSYMBOL(at+n, getapiname(API_ALSA)); n++;
#endif
    info_out((t_text *)x, s, n, at);
}

void pdinfo_midi_listdevs(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
{
    char indevlist[MAXMIDIINDEV*DEVDESCSIZE],
        outdevlist[MAXMIDIOUTDEV*DEVDESCSIZE];
    int nindevs = 0, noutdevs = 0, i;
    sys_get_midi_devs(indevlist, &nindevs,
            outdevlist, &noutdevs,
            MAXNDEV, DEVDESCSIZE);
    t_atom at[4];
    if (s == gensym("midi-indevlist"))
    {
        for (i = 0; i < nindevs; i++)
           SETSYMBOL(at+i, gensym(indevlist + i * DEVDESCSIZE));
        info_out((t_text *)x, s, i, at);
    }
    else if (s == gensym("midi-outdevlist"))
    {
        for (i = 0; i < noutdevs; i++)
           SETSYMBOL(at+i, gensym(outdevlist + i * DEVDESCSIZE));
        info_out((t_text *)x, s, i, at);
    }
}

void pdinfo_midi_dev(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
{
    int devno, nmidiindev, midiindev[MAXMIDIINDEV],
        nmidioutdev, midioutdev[MAXMIDIOUTDEV];
    int *dev, *chan, ndev;
    t_atom at[4];
    if (argc) devno = (int)atom_getfloatarg(0, argc, arg);
    else devno = 0;
    sys_get_midi_params(&nmidiindev, midiindev, &nmidioutdev, midioutdev);
    if (s == gensym("midi-indev"))
        dev = midiindev, ndev = nmidiindev;
    else
        dev = midioutdev, ndev = nmidioutdev;
    if (devno >= 0 && devno < ndev)
    {
        t_atom at[1];
        SETFLOAT(at, (t_float)dev[devno]);
        info_out((t_text *)x, s, 1, at);
    }
    else
        info_out((t_text *)x, s, 0, 0);
}

void pdinfo_audio_outdev(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
{
    int devno;
    if (argc) devno = (int)atom_getfloatarg(0, argc, arg);
    else devno = 0;
    int naudioindev, audioindev[MAXAUDIOINDEV], chindev[MAXAUDIOINDEV];
    int naudiooutdev, audiooutdev[MAXAUDIOOUTDEV], choutdev[MAXAUDIOOUTDEV];
    int rate, advance, callback, blocksize;
    sys_get_audio_params(&naudioindev, audioindev, chindev,
        &naudiooutdev, audiooutdev, choutdev, &rate, &advance, &callback);
    if (devno >= 0 && devno < naudioindev)
    {
        t_atom at[2];
        SETFLOAT(at, (t_float)audioindev[devno]);
        SETFLOAT(at+1, (t_float)chindev[devno]);
        info_out((t_text *)x, s, 2, at);
    }
    else
        info_out((t_text *)x, s, 0, 0);
}

void pdinfo_audio_inchannels(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
{
    t_atom at[1];
    SETFLOAT(at, (t_float)sys_get_inchannels());
    info_out((t_text *)x, s, 1, at);
}

void pdinfo_audio_outchannels(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
{
    t_atom at[1];
    SETFLOAT(at, (t_float)sys_get_outchannels());
    info_out((t_text *)x, s, 1, at);
}


void pdinfo_audio_samplerate(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
{
    t_atom at[1];
    SETFLOAT(at, (t_float)sys_getsr());
    info_out((t_text *)x, s, 1, at);
}

void pdinfo_audio_blocksize(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
{
    t_atom at[1];
    SETFLOAT(at, (t_float)sys_getblksize());
    info_out((t_text *)x, s, 1, at);
}

void pdinfo_version(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
{
    int major=0, minor=0, bugfix=0;
    sys_getversion(&major, &minor, &bugfix);
    t_atom at[3];
    SETFLOAT(at, (t_float)major);
    SETFLOAT(at+1, (t_float)minor);
    SETFLOAT(at+2, (t_float)bugfix);
    info_out((t_text *)x, s, 3, at);
}

void pdinfo_pi(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
{
    t_atom at[1];
    const t_float Pi = 3.141592653589793;
    SETFLOAT(at, Pi);
    info_out((t_text *)x, s, 1, at);
}

void pdinfo_print(t_pdinfo *x)
{
    info_print((t_text *)x);
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
}

void *pdinfo_new(t_symbol *s, t_int argc, t_atom *argv)
{
    t_pdinfo *x = (t_pdinfo *)pd_new(pdinfo_class);
    outlet_new(&x->x_obj, &s_list);    
    return (void *)x;
}

void pdinfo_setup(void)
{
    pdinfo_class = class_new(gensym("pdinfo"),
        (t_newmethod)pdinfo_new, 0,
        sizeof(t_pdinfo),
        CLASS_DEFAULT, 0);

729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
    class_addmethod(pdinfo_class, (t_method)pdinfo_audio_api,
        gensym("audio-api"), A_DEFFLOAT, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_audio_apilist,
        gensym("audio-apilist"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_audio_api_list_raw,
        gensym("audio-apilist-raw"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_audio_inchannels,
        gensym("audio-inchannels"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_audio_dev,
        gensym("audio-indev"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_audio_listdevs,
        gensym("audio-indevlist"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_audio_listdevs,
        gensym("audio-multidev-support"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_audio_outchannels,
        gensym("audio-outchannels"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_audio_dev,
        gensym("audio-outdev"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_audio_listdevs,
        gensym("audio-outdevlist"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_audio_blocksize,
        gensym("blocksize"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_dir,
        gensym("dir"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_dsp,
        gensym("dsp-status"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_midi_api,
        gensym("midi-api"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_midi_apilist,
        gensym("midi-apilist"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_midi_dev,
        gensym("midi-indev"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_midi_listdevs,
        gensym("midi-indevlist"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_midi_dev,
        gensym("midi-outdev"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_midi_listdevs,
        gensym("midi-outdevlist"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_pi,
        gensym("pi"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_audio_samplerate,
        gensym("samplerate"), A_GIMME, 0);
    class_addmethod(pdinfo_class, (t_method)pdinfo_version,
        gensym("version"), A_GIMME, 0);

    class_addmethod(pdinfo_class, (t_method)pdinfo_print,
        gensym("print"), 0);

    post("pdinfo: v.0.1");
    post("stable pdinfo methods: dir dsp version");
779
780
781
}

/* -------------------------- classinfo ------------------------------ */
782
t_symbol *attosym(t_atomtype at)
783
{
784
785
786
787
788
789
790
791
792
793
794
795
796
    t_symbol *s;
    switch (at)
    {
        case A_FLOAT: s = gensym("A_FLOAT"); break;
        case A_SYMBOL: s = gensym("A_SYMBOL"); break;
        case A_POINTER: s = gensym("A_POINTER"); break;
        case A_DEFFLOAT: s = gensym("A_DEFFLOAT"); break;
        case A_DEFSYM: s = gensym("A_DEFSYM"); break;
        case A_GIMME: s = gensym("A_GIMME"); break;
        case A_CANT: s = gensym("A_CANT"); break;
        default: s = 0;
    }
    return s;
797
798
}

799
void classinfo_args(t_classinfo *x, t_symbol *s, int argc, t_atom *argv)
800
{
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
    t_class *c;
    if(!(c = classtable_findbyname(x->x_name)))
    {
        outlet_bang(x->x_out2);
        return;
    }
    c = classtable_findbyname(gensym("objectmaker"));
    if (!c)
    {
        pd_error(x, "classinfo: no objectmaker.");
        return;
    }
    t_atom ap[MAXPDARG];
    t_int i;
    t_methodentry *m;
    for(i = c->c_nmethod, m = c->c_methods; i; i--, m++)
    {
        if(m->me_name == x->x_name)
            break;
    }
    /* We have to check if there was a match-- while the class
       table holds all classes, the objectmaker only creates a
       new method entry if the class has a t_newmethod defined
       in its setup routine. This misses a few objects such as 
       gatom which can't be typed into a box. Consequently I'm
       sending a bang to the reject outlet for those objects--
       unfortunately that means a "reject" bang could mean the
       object either doesn't exist, or it doesn't have its own
       t_newmethod. However, one can use the other [classinfo]
       methods to unambiguously check the class for existence.
    */
    if (i)
    {
        t_atomtype arg, *args = m->me_arg;
        for(i = 0; arg = *args; args++, i++)
        {
            t_symbol *sym = attosym(arg);
            if (!sym)
            {
                pd_error(x, "classinfo: %s: bad argtype", x->x_name->s_name);
                return;
            }
            SETSYMBOL(ap+i, sym);
        }
        info_out((t_text *)x, s, i, ap);
    }
    else
        outlet_bang(x->x_out2);
849
850
}

851
void classinfo_externdir(t_classinfo *x, t_symbol *s, int argc, t_atom *argv)
852
{
853
854
855
856
857
858
859
860
861
    t_class *c;
    if(!(c = classtable_findbyname(x->x_name)))
    {
        outlet_bang(x->x_out2);
        return;
    }
    t_atom at[1];
    SETSYMBOL(at, c->c_externdir);
    info_out((t_text *)x, s, 1, at);
862
863
}

864
void classinfo_methods(t_classinfo *x, t_symbol *s, int argc, t_atom *argv)
865
{
866
867
    t_class *c;
    if(!(c = classtable_findbyname(x->x_name)))
868
    {
869
        outlet_bang(x->x_out2);
870
871
        return;
    }
872
873
874
875
876
877
878
879
880
    t_atom at[1];
    SETFLOAT(at, c->c_nmethod);
    info_out((t_text *)x, s, 1, at);
}

void classinfo_size(t_classinfo *x, t_symbol *s, int argc, t_atom *argv)
{
    t_class *c;
    if(!(c = classtable_findbyname(x->x_name)))
881
    {
882
        outlet_bang(x->x_out2);
883
884
        return;
    }
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
    t_atom at[1];
    SETFLOAT(at, (t_float)c->c_size);
    info_out((t_text *)x, s, 1, at);
} 
 
void classinfo_float(t_classinfo *x, t_float f)
{
    t_class *c;
    if(c = classtable_findbyname(x->x_name))
    {
        if(f >= 0 && (t_int)f < c->c_nmethod)
        {
            t_atom ap[MAXPDARG+1];
            if(c->c_nmethod)
            {
                t_methodentry *me = c->c_methods;
                SETSYMBOL(ap, (me+(t_int)f)->me_name);
                t_atomtype arg, *args = (me+(t_int)f)->me_arg;
                t_int n;
                for(n = 1; arg = *args; args++, n++)
                {
                    t_symbol *s = attosym(arg);
                    SETSYMBOL(ap+n, s);
                }
                info_out((t_text *)x, &s_list, n, ap);
            }
        }
        else
            info_out((t_text *)x, &s_bang, 0, 0);
    }
    else
        outlet_bang(x->x_out2);
917
918
}

919
void classinfo_print(t_classinfo *x)
920
{
921
922
    info_print((t_text *)x);
}
923

924
925
926
927
928
929
930
void *classinfo_new(t_symbol *s)
{
    t_classinfo *x = (t_classinfo *)pd_new(classinfo_class);
    x->x_name = s;
    symbolinlet_new(&x->x_obj, &x->x_name);
    outlet_new(&x->x_obj, &s_anything);
    x->x_out2 = outlet_new(&x->x_obj, &s_bang);
931
932
933
934
935
936
937
938
    return (void *)x;
}

void classinfo_setup(void)
{
    classinfo_class = class_new(gensym("classinfo"),
        (t_newmethod)classinfo_new, 0,
        sizeof(t_classinfo),
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
        CLASS_DEFAULT, A_DEFSYM, 0);

    class_addfloat(classinfo_class, classinfo_float);
    class_addmethod(classinfo_class, (t_method)classinfo_args, gensym("args"),
        A_GIMME, 0);
    class_addmethod(classinfo_class, (t_method)classinfo_externdir,
        gensym("externdir"), A_GIMME, 0);
    class_addmethod(classinfo_class, (t_method)classinfo_methods,
        gensym("methods"), A_GIMME, 0);
    class_addmethod(classinfo_class, (t_method)classinfo_size, gensym("size"),
        A_GIMME, 0);
    class_addmethod(classinfo_class, (t_method)classinfo_print,
        gensym("print"), 0);

    post("classinfo: v.0.1");
    post("stable classinfo methods: size");

/* todo: add "instance" method to return instances of a class on a canvas */
957
}
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
958

Miller Puckette's avatar
Miller Puckette committed
959
960
void x_interface_setup(void)
{
961
962
963
964
	print_setup();
	canvasinfo_setup();
	pdinfo_setup();
	classinfo_setup();
Miller Puckette's avatar
Miller Puckette committed
965
}