x_interface.c 50.4 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
#include "s_version.h"
12 13 14
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
Miller Puckette's avatar
Miller Puckette committed
15

16
#if PD_FLOATSIZE == 32
17
#define FLOAT_SPECIFIER "%.6g"
18
#elif PD_FLOATSIZE == 64
19
#define FLOAT_SPECIFIER "%.14g"
20 21
#endif

22 23 24 25 26
/* we need the following for [pdinfo] ... */

#define MAXNDEV 20
#define DEVDESCSIZE 80

27 28


Miller Puckette's avatar
Miller Puckette committed
29
/* -------------------------- print ------------------------------ */
30
t_class *print_class;
Miller Puckette's avatar
Miller Puckette committed
31 32 33 34 35 36 37

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

38
static void *print_new(t_symbol *sel, int argc, t_atom *argv)
Miller Puckette's avatar
Miller Puckette committed
39 40
{
    t_print *x = (t_print *)pd_new(print_class);
41 42 43 44 45 46 47 48 49 50
    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
51
    {
52 53
        int bufsize;
        char *buf;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
54 55 56
        t_binbuf *bb = binbuf_new();
        binbuf_add(bb, argc, argv);
        binbuf_gettext(bb, &buf, &bufsize);
57
        buf = resizebytes(buf, bufsize, bufsize+1);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
58 59
        buf[bufsize] = 0;
        x->x_sym = gensym(buf);
60
        freebytes(buf, bufsize+1);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
61 62
        binbuf_free(bb);
    }
Miller Puckette's avatar
Miller Puckette committed
63 64 65 66 67
    return (x);
}

static void print_bang(t_print *x)
{
68
    if (sys_nogui || sys_printhook)
69 70 71 72 73 74 75 76 77
        post("%s%sbang", x->x_sym->s_name, (*x->x_sym->s_name ? ": " : ""));
    else
    {
        gui_start_vmess("gui_print", "xs", x, x->x_sym->s_name);
        gui_start_array();
        gui_s(s_bang.s_name);
        gui_end_array();
        gui_end_vmess();
    }
Miller Puckette's avatar
Miller Puckette committed
78 79 80 81
}

static void print_pointer(t_print *x, t_gpointer *gp)
{
82
    if (sys_nogui || sys_printhook)
83 84 85 86 87 88 89 90 91 92
        post("%s%s(gpointer)", x->x_sym->s_name,
            (*x->x_sym->s_name ? ": " : ""));
    else
    {
        gui_start_vmess("gui_print", "xs", x, x->x_sym->s_name);
        gui_start_array();
        gui_s("(gpointer)");
        gui_end_array();
        gui_end_vmess();
    }
Miller Puckette's avatar
Miller Puckette committed
93 94
}

95
static void print_float(t_print *x, t_floatarg f)
Miller Puckette's avatar
Miller Puckette committed
96
{
97
    if (sys_nogui || sys_printhook)
98 99
        post("%s%s" FLOAT_SPECIFIER, x->x_sym->s_name,
            (*x->x_sym->s_name ? ": " : ""), f);
100 101 102 103 104 105 106 107
    else
    {
        gui_start_vmess("gui_print", "xs", x, x->x_sym->s_name);
        gui_start_array();
        gui_f(f);
        gui_end_array();
        gui_end_vmess();
    }
Miller Puckette's avatar
Miller Puckette committed
108 109
}

110
static void print_symbol(t_print *x, t_symbol *s)
Miller Puckette's avatar
Miller Puckette committed
111
{
112
    if (sys_nogui || sys_printhook)
113 114 115 116 117 118 119 120 121 122 123
        post("%s%s%s", x->x_sym->s_name, (*x->x_sym->s_name ? ": " : ""),
            s->s_name);
    else
    {
        gui_start_vmess("gui_print", "xs", x, x->x_sym->s_name);
        gui_start_array();
        gui_s(s_symbol.s_name);
        gui_s(s->s_name);
        gui_end_array();
        gui_end_vmess();
    }
Miller Puckette's avatar
Miller Puckette committed
124 125 126 127
}

static void print_anything(t_print *x, t_symbol *s, int argc, t_atom *argv)
{
128
    char buf[MAXPDSTRING];
129
    t_atom at;
130
    if (sys_nogui || sys_printhook)
131 132 133 134 135 136 137 138 139 140 141
    {
        startpost("%s%s", x->x_sym->s_name, (*x->x_sym->s_name ? ":" : ""));
        if (s && (s != &s_list || (argc && argv->a_type != A_FLOAT)))
        {
            SETSYMBOL(&at, s);
            postatom(1, &at);
        }
        postatom(argc, argv);
        endpost();
    }
    else
142
    {
143 144 145 146 147 148 149 150 151 152 153
        gui_start_vmess("gui_print", "xs", x, x->x_sym->s_name);
        gui_start_array();
        if (s && (s != &s_list || (argc && argv->a_type != A_FLOAT)))
            gui_s(s->s_name);
        for(; argc; argv++, argc--)
        {
            atom_string(argv, buf, MAXPDSTRING);
            gui_s(buf);
        }
        gui_end_array();
        gui_end_vmess();
154
    }
Miller Puckette's avatar
Miller Puckette committed
155 156 157 158 159
}

static void print_setup(void)
{
    print_class = class_new(gensym("print"), (t_newmethod)print_new, 0,
160
        sizeof(t_print), 0, A_GIMME, 0);
Miller Puckette's avatar
Miller Puckette committed
161 162 163
    class_addbang(print_class, print_bang);
    class_addfloat(print_class, print_float);
    class_addpointer(print_class, print_pointer);
164
    class_addsymbol(print_class, print_symbol);
Miller Puckette's avatar
Miller Puckette committed
165 166 167
    class_addanything(print_class, print_anything);
}

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
/* -------------------------- unpost ------------------------------ */

#define NEWBUFSIZE MAXPDSTRING * 2
static t_class *unpost_class;

typedef struct _unpost {
    t_object x_obj;
    t_outlet *x_out0;
    t_outlet *x_out1;
    int x_all;
} t_unpost;

typedef struct _unpost_frame {
    t_unpost *u_self;
    char *u_buf;
    unsigned int u_bufsize;
    unsigned int u_index;
} t_unpost_frame;

static t_unpost_frame *current_unpost;

static void *unpost_frame_new(t_unpost *x)
{
    t_unpost_frame *u = (t_unpost_frame *)t_getbytes(sizeof(t_unpost_frame));
    u->u_self = x;
    u->u_buf = (char *)t_getbytes(sizeof(char) * NEWBUFSIZE);
    u->u_buf[0] = '\0';
    u->u_bufsize = NEWBUFSIZE;
    u->u_index = 0;
    return u;
}

static void unpost_frame_resize(t_unpost_frame *u, int newsize)
{
    u->u_buf = (char *)t_resizebytes(u->u_buf, sizeof(*u->u_buf) * u->u_bufsize,
       sizeof(*u->u_buf) * newsize);
    u->u_bufsize = newsize;
}

static void unpost_frame_free(t_unpost_frame* u)
{
    t_freebytes(u->u_buf, sizeof(*u->u_buf));
210
    u->u_buf = NULL;
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
}

static void *unpost_new(t_symbol *s)
{
    t_unpost *x = (t_unpost *)pd_new(unpost_class);
    x->x_all = s != gensym("error");
    x->x_out0 = outlet_new(&x->x_obj, &s_symbol);
    x->x_out1 = outlet_new(&x->x_obj, &s_symbol);
    return x;
}

extern t_printhook sys_printhook;
extern t_printhook sys_printhook_error;
static void unpost_printhook(const char *s) {
    t_unpost *x = current_unpost->u_self;
    /* does strlen include terminator? can't remember... */
    unsigned int needbytes = strlen(current_unpost->u_buf) + strlen(s) + 1;
    if (needbytes >= current_unpost->u_bufsize)
    {
        /* Raise the bufsize to NEWBUFSIZE * n where n is a power of two that
           will fit the number of bytes we need) */
        unsigned int n = ((needbytes / NEWBUFSIZE) >> 1) +
            (needbytes % NEWBUFSIZE != 0);
        unpost_frame_resize(current_unpost,
            current_unpost->u_bufsize * (1 << n));
    }
237

238 239 240
    else if (strlen(current_unpost->u_buf) + strlen(s) <
        current_unpost->u_bufsize / 4)
    {
241 242 243 244 245
        /* Pretty sure I prematurely optimized here. There's no good reason to
           shrink the buffer in the midst of receiving some enormous number of
           posts from an object chain. Besides, we free the buffer once the
           object chain downstream from the [unpost] outlet finishes computing.
           Anyhow, here we are. */
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
        unpost_frame_resize(current_unpost,
            (current_unpost->u_bufsize / 4 < NEWBUFSIZE) ?
                NEWBUFSIZE :
                current_unpost->u_bufsize / 4);
    }
    strcat(current_unpost->u_buf, s);
    const char *p;
    const char *d = current_unpost->u_buf, *dd = d;
    for (;;) {
        p = strchr(d, '\n');
        if (!p) break;
        /* To avoid infinite recursion, let's reset our hooks before we
           send to the right outlet. */
        t_printhook print_backup = sys_printhook,
                    error_backup = sys_printhook_error;
        sys_printhook = NULL;
        sys_printhook_error = NULL;
        outlet_symbol(x->x_out0, gensym(d));
        sys_printhook = print_backup;
        sys_printhook_error = error_backup;
        d = p + 1;
    }
    if (d != dd)
    {
        char *q = strdup(d);
        strcpy(current_unpost->u_buf, q);
        t_freebytes(q, sizeof(*q) * strlen(q));
    }
}

static void unpost_anything(t_unpost *x, t_symbol *s, int argc, t_atom *argv)
{
    t_printhook *myhook = x->x_all ? &sys_printhook : &sys_printhook_error;
    t_printhook backup1 = *myhook;
    t_unpost_frame *backup2 = current_unpost;
    *myhook = unpost_printhook;
    current_unpost = unpost_frame_new(x);
    outlet_anything(x->x_out1, s, argc, argv);
    *myhook = backup1;
    unpost_frame_free(current_unpost);
    current_unpost = backup2;
}

static void unpost_setup(void)
{
    unpost_class = class_new(gensym("unpost"), (t_newmethod)unpost_new, 0,
        sizeof(t_unpost), 0, A_DEFSYM, 0);
    class_addanything(unpost_class, unpost_anything);
}

296
/* --------------pdinfo, canvasinfo, classinfo, objectinfo --------------- */
297
static t_class *canvasinfo_class;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
298

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
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;
315 316
    t_outlet *x_out2;
    t_symbol *x_name;
317 318
} t_classinfo;

319 320 321 322 323
static t_class *objectinfo_class;

typedef struct _objectinfo {
    t_object x_obj;
    t_outlet *x_out2;
324
    t_gpointer x_gp;
325 326 327
    t_canvas *x_canvas;
} t_objectinfo;

328 329 330 331 332 333
static t_class *debuginfo_class;

typedef struct _debuginfo {
    t_object x_obj;
} t_debuginfo;

Guillem Bartrina's avatar
Guillem Bartrina committed
334 335 336 337 338 339 340 341
static t_class *abinfo_class;

typedef struct _abinfo
{
    t_object x_obj;
    t_ab_definition *abdef;
} t_abinfo;

342
/* used by all the *info objects */
343

344
static int info_to_console = 0;
345 346

void info_out(t_text *te, t_symbol *s, int argc, t_atom *argv)
347
{
348
    if (info_to_console)
349 350 351 352 353 354
    {
        startpost("%s:", s->s_name);
        if (argc > 0)
            postatom(argc, argv);
        endpost();
    }
355 356
    else
    {
357 358 359 360 361
        outlet_list(te->ob_outlet,
            &s_list, argc, argv);
    }
}

362 363 364 365 366 367 368 369 370 371 372 373 374 375
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);
376 377
    /* not sure why this doesn't work... */
    /* pd_forwardmess(te->te_pd, 0, 0); */
378 379 380
    info_to_console = 0;
}

381
/* -------------------------- canvasinfo ------------------------------ */
382 383
/* climb up to a parent canvas */
t_canvas *canvas_climb(t_canvas *c, int level)
384
{
385 386 387 388 389 390 391 392 393 394
  if(level <= 0)
      return c;
  else
  {
      t_canvas *ret = c;
      while(level && ret->gl_owner)
      {
        ret = ret->gl_owner;
        level--;
      }
395
      return ret;
396 397 398
  }
}

399 400
void canvas_getargs_after_creation(t_canvas *c, int *argcp, t_atom **argvp);

401
void canvasinfo_args(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
402
{
403
    t_canvas *c = canvas_climb(x->x_canvas, x->x_depth);
404 405 406 407 408 409 410
    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)
411
    {
412
        info_out((t_text *)x, s, 0, 0);
413
    }
414
    else
415
    {
416
        if (s == gensym("args"))
417 418 419 420
        {
            canvas_getargs_after_creation(c, &n, &a);
            info_out((t_text *)x, s, n, a);
        }
421
        else
422
        {
423 424 425 426 427 428 429 430 431 432
            /* For "boxtext" we have to convert semi, comma, dollar, and
               dollsym atoms to symbol atoms. Otherwise we could end up
               outputting a message containing stray semis/commas/etc. which
               might cause trouble.

               We sent the atoms through binbuf_addbinbuf which does the
               conversion to symbols for us. That way the user will get
               expected output-- e.g., special characters will be properly
               escaped when printing.
            */
433 434 435 436
            t_binbuf *escaped = binbuf_new();
            binbuf_addbinbuf(escaped, b);
            n = binbuf_getnatom(escaped);
            a = binbuf_getvec(escaped);
437
            info_out((t_text *)x, s, n, a);
438 439
            binbuf_free(escaped);
        }
440
    }
441 442 443 444
}

void canvasinfo_coords(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
{
445
    t_canvas *c = canvas_climb(x->x_canvas, x->x_depth);
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    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)
{
462
    t_canvas *c = canvas_climb(x->x_canvas, x->x_depth);
463 464 465 466 467 468 469 470
    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)
{
471
    t_canvas *c = canvas_climb(x->x_canvas, x->x_depth);
472 473 474 475 476 477 478
    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)
{
479
    t_canvas *c = canvas_climb(x->x_canvas, x->x_depth);
480 481 482 483 484 485 486 487 488 489 490
    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)
{
491
    t_canvas *c = canvas_climb(x->x_canvas, x->x_depth);
492 493 494 495 496 497 498
    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)
{
499
    t_canvas *c = canvas_climb(x->x_canvas, x->x_depth);
500 501 502 503 504 505
    c = canvas_getrootfor(c);
    t_atom at[1];
    SETSYMBOL(at, c->gl_name);
    info_out((t_text *)x, s, 1, at);
}

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
int binbuf_match(t_binbuf *inbuf, t_binbuf *searchbuf, int wholeword);

void canvasinfo_find(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
{
    t_canvas *c = canvas_climb(x->x_canvas, x->x_depth);
    int i, match = 0;
    t_atom at[1], *ap;
    t_gpointer *gp = (t_gpointer *)t_getbytes(500 * sizeof(*gp));
    t_gobj *y;
    t_binbuf *searchbuf = binbuf_new();
    t_binbuf *outbuf = binbuf_new();
    binbuf_add(searchbuf, argc, argv);
    for (y = c->gl_list; y && match < 500; y = y->g_next)
    {
        t_binbuf *objbuf;
        /* if it's not t_object (e.g., a scalar), or if it is but
           does not have any binbuf content, send a bang... */
        if (pd_checkobject(&y->g_pd) &&
            (objbuf = ((t_text *)y)->te_binbuf) &&
            binbuf_match(objbuf, searchbuf, 1))
        {
            match++;
            gpointer_init(gp+match-1);
            gpointer_setglist(gp+match-1, c, y);
            SETPOINTER(at, gp+match-1);
            binbuf_add(outbuf, 1, at); 
        }
    }
    if (match >= 500)
        post("canvasinfo: warning: find is currently limited to 500 results. "
             "Truncating the output to 500 elements...");
    info_out((t_text *)x, s, binbuf_getnatom(outbuf), binbuf_getvec(outbuf));
    for (i = 0, ap = binbuf_getvec(outbuf); i < binbuf_getnatom(outbuf); i++)
    {
        t_gpointer *gp = (ap+i)->a_w.w_gpointer;
        gpointer_unset(gp);
    }
    binbuf_free(outbuf);
    binbuf_free(searchbuf);
    freebytes(gp, 500 * sizeof(*gp));
}

void canvasinfo_gobjs(t_canvasinfo *x, t_float xpos, t_float ypos,
    int all)
550
{
551
    t_canvas *c = canvas_climb(x->x_canvas, x->x_depth);
552
    int x1, y1, x2, y2, i, atom_count = 0;
553 554 555 556 557
    /* hack to avoid memory allocation. Maybe there's a way to use the
       XLIST_ATOMS_ALLOCA macro? */
    t_atom at[500];
    t_gpointer *gp, *gvec;
    gp = gvec = (t_gpointer *)t_getbytes(500 * sizeof (*gvec));
558 559
    t_gobj *y;
    for (y = c->gl_list, i = 0; y && atom_count < 500; y = y->g_next, i++)
560
    {
561
        if (all || canvas_hitbox(c, y, xpos, ypos, &x1, &y1, &x2, &y2))
562
        {
563 564 565
            gpointer_init(gp);
            gpointer_setglist(gp, c, y);
            SETPOINTER(at+atom_count, gp);
566
            atom_count++;
567
            gp++;
568
        }
569
    }
570 571
    if (atom_count >= 500)
        post("canvasinfo: warning: hitbox is currently limited to 500 objects. "
572
             "Truncating the output to 500 elements...");
573
    info_out((t_text *)x, gensym("hitbox"), atom_count, at);
574 575 576 577 578 579 580 581 582 583 584 585 586
    for (i = 0, gp = gvec; i < atom_count; i++, gp++)
        gpointer_unset(gp);
    freebytes(gvec, 500 * sizeof(*gvec));
}

void canvasinfo_bang(t_canvasinfo *x)
{
    canvasinfo_gobjs(x, 0, 0, 1);
}

void canvasinfo_hitbox(t_canvasinfo *x, t_floatarg xpos, t_floatarg ypos)
{
    canvasinfo_gobjs(x, xpos, ypos, 0);
587 588
}

589
void canvasinfo_name(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
590
{
591
    t_canvas *c = canvas_climb(x->x_canvas, x->x_depth);
592
    char buf[MAXPDSTRING];
593
    snprintf(buf, MAXPDSTRING, "x%zx", (t_uint)c);
594 595 596
    t_atom at[1];
    SETSYMBOL(at, gensym(buf));
    info_out((t_text *)x, s, 1, at);
597 598
}

599
void canvasinfo_pointer(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
600
{
601
    t_canvas *c = canvas_climb(x->x_canvas, x->x_depth);
602 603 604 605 606 607 608
    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);
609 610
}

611 612
void canvasinfo_posonparent(t_canvasinfo *x, t_symbol *s,
    int argc, t_atom *argv)
613
{
614
    t_canvas *c = canvas_climb(x->x_canvas, x->x_depth);
615 616 617 618
    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);
619 620
}

621
void canvasinfo_screenpos(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
622
{
623
    t_canvas *c = canvas_climb(x->x_canvas, x->x_depth);
624 625 626 627 628 629
    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);
630 631
}

632
void canvasinfo_toplevel(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
633
{
634
    t_canvas *c = canvas_climb(x->x_canvas, x->x_depth);
635 636 637 638
    t_float f = c->gl_owner ? 0 : 1;
    t_atom at[1];
    SETFLOAT(at, f);
    info_out((t_text *)x, s, 1, at);
639 640
}

641
void canvasinfo_vis(t_canvasinfo *x, t_symbol *s, int argc, t_atom *argv)
642
{
643
    t_canvas *c = canvas_climb(x->x_canvas, x->x_depth);
644 645 646 647 648 649 650 651
    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);
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
}

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);

673
    class_addbang(canvasinfo_class, canvasinfo_bang);
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
    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);
690 691
    class_addmethod(canvasinfo_class, (t_method)canvasinfo_find,
        gensym("find"), A_GIMME, 0);
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
    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");

712 713 714 715 716
}

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

717
t_symbol *getapiname(int id)
718
{
719 720
    t_symbol *s = 0;
    switch (id)
721
    {
722 723 724 725 726 727 728 729 730 731
        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;
732
    }
733 734 735 736 737 738 739 740
    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)
741
    {
742 743
        pd_error(x, "pdinfo: can't find pd's directory");
        return;
744
    }
745 746
    SETSYMBOL(at, dir);
    info_out((t_text *)x, s, 1, at);
747 748
}

749 750 751
/* Instead of reporting the actual value for dsp when it's temporarily
   suspended, we report what it will be when dsp is resumed. This way the
   user can get a meaningful value at load time. */
752
void pdinfo_dsp(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
753
{
754
    t_atom at[1];
755
    SETFLOAT(at, (t_float)(pd_this->pd_dspstate_user));
756
    info_out((t_text *)x, s, 1, at);
757 758
}

759 760 761 762 763 764 765
void pdinfo_floatsize(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
{
    t_atom at[1];
    SETFLOAT(at, (t_float)PD_FLOATSIZE);
    info_out((t_text *)x, s, 1, at);
}

766
void pdinfo_audio_api(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
767
{
768 769 770 771
    t_atom at[1];
    t_symbol *api = getapiname(sys_audioapi);
    SETSYMBOL(at, api);
    info_out((t_text *)x, s, 1, at);
772 773
}

774 775 776 777 778 779
void pdinfo_canvaslist(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
{
    t_canvas *c;
    int j, i = 0;
    t_binbuf *outbuf = binbuf_new();
    t_atom at[1];
780
    for (c = pd_this->pd_canvaslist; c; c = c->gl_next)
781 782
        i++;
    t_gpointer *gp = (t_gpointer *)t_getbytes(i * sizeof(*gp));
783
    for (c = pd_this->pd_canvaslist, i = 0; c; c = c->gl_next, i++)
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
    {
        gpointer_init(gp+i);
        gpointer_setglist(gp+i, c, 0);
        SETPOINTER(at, gp+i);
        binbuf_add(outbuf, 1, at); 
    }
    info_out((t_text *)x, s, binbuf_getnatom(outbuf), binbuf_getvec(outbuf));
    binbuf_free(outbuf);
    for (j = 0; j < i; j++)
        gpointer_unset(gp+j);
    freebytes(gp, i * sizeof(*gp));
}

/* maybe this should be the bang method? */
void pdinfo_classlist(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
799 800
{
    int size = classtable_size();
801 802 803 804 805 806 807 808 809 810 811 812 813 814
    if (info_to_console)
    {
        t_atom at[2];
        SETFLOAT(at, size);
        SETSYMBOL(at+1, gensym("classes loaded (\"classtable\" outputs "
                               "the full list)"));
        info_out((t_text *)x, s, 2, at);
    }
    else
    {
        t_atom at[size];
        classtable_tovec(size, at);
        info_out((t_text *)x, s, size, at);
    }
815 816
}

817
void pdinfo_audioin(t_pdinfo *x, t_symbol *s, int argc, t_atom *arg)
818
{
819 820 821
//        char i
}

822
void pdinfo_audio_api_list_all(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
823 824 825 826 827 828 829 830
{
    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);
}

831
void pdinfo_audio_apilist(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
{
    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);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
879
    t_atom at[MAXNDEV];
880
    if (s == gensym("audio-multidev-support"))
881
    {
882 883
        SETFLOAT(at, canmulti);
        info_out((t_text *)x, s, 1, at); 
884
    }
885
    else if (s == gensym("audio-indevlist"))
886
    {
887 888 889 890 891 892 893 894 895 896 897 898
        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);
    }
}

899
void pdinfo_audio_dev(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
900 901
{
    int devno;
902
    if (argc) devno = (int)atom_getfloatarg(0, argc, argv);
903 904 905
    else devno = 0;
    int naudioindev, audioindev[MAXAUDIOINDEV], chindev[MAXAUDIOINDEV];
    int naudiooutdev, audiooutdev[MAXAUDIOOUTDEV], choutdev[MAXAUDIOOUTDEV];
906
    int rate, advance, callback, blocksize;
907
    sys_get_audio_params(&naudioindev, audioindev, chindev,
908
        &naudiooutdev, audiooutdev, choutdev, &rate, &advance, &callback, &blocksize);
909 910 911 912 913 914 915 916 917 918 919
    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);
920
    }
921 922 923 924
    else
        info_out((t_text *)x, s, 0, 0);
}

925
void pdinfo_midi_api(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
926 927 928 929 930 931 932 933 934 935 936
{
    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);
}

937
void pdinfo_midi_apilist(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
{
    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);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
960
    t_atom at[MAXNDEV];
961 962 963 964 965 966 967 968 969 970 971 972 973 974
    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);
    }
}

975
void pdinfo_midi_dev(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
976 977 978
{
    int devno, nmidiindev, midiindev[MAXMIDIINDEV],
        nmidioutdev, midioutdev[MAXMIDIOUTDEV];
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
979
    int *dev, ndev;
980
    if (argc) devno = (int)atom_getfloatarg(0, argc, argv);
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
    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);
}

997
void pdinfo_audio_outdev(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
998 999
{
    int devno;
1000
    if (argc) devno = (int)atom_getfloatarg(0, argc, argv);
1001 1002 1003
    else devno = 0;
    int naudioindev, audioindev[MAXAUDIOINDEV], chindev[MAXAUDIOINDEV];
    int naudiooutdev, audiooutdev[MAXAUDIOOUTDEV], choutdev[MAXAUDIOOUTDEV];
1004
    int rate, advance, callback, blocksize;
1005
    sys_get_audio_params(&naudioindev, audioindev, chindev,
1006
        &naudiooutdev, audiooutdev, choutdev, &rate, &advance, &callback, &blocksize);
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
    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);
}

1018
void pdinfo_audio_inchannels(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
1019 1020 1021 1022 1023 1024
{
    t_atom at[1];
    SETFLOAT(at, (t_float)sys_get_inchannels());
    info_out((t_text *)x, s, 1, at);
}

1025
void pdinfo_audio_outchannels(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
1026 1027 1028 1029 1030 1031 1032
{
    t_atom at[1];
    SETFLOAT(at, (t_float)sys_get_outchannels());
    info_out((t_text *)x, s, 1, at);
}


1033
void pdinfo_audio_samplerate(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
1034 1035 1036 1037 1038 1039
{
    t_atom at[1];
    SETFLOAT(at, (t_float)sys_getsr());
    info_out((t_text *)x, s, 1, at);
}

1040
void pdinfo_audio_blocksize(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
1041 1042
{
    t_atom at[1];
1043
    SETFLOAT(at, (t_float)sys_audio_get_blocksize());
1044 1045 1046
    info_out((t_text *)x, s, 1, at);
}

1047 1048 1049 1050 1051 1052 1053
void pdinfo_gui(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
{
    t_atom at[1];
    SETFLOAT(at, (t_float)(!sys_nogui));
    info_out((t_text *)x, s, 1, at);
}

1054 1055 1056
/* directory where extra and doc are found. Might also want to add
   another method to return a list of all paths searched for libs-- i.e.,
   "extrapath". */
1057 1058 1059
void pdinfo_libdir(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
{
    t_atom at[1];
1060
    SETSYMBOL(at, sys_libdir);
1061 1062 1063
    info_out((t_text *)x, s, 1, at);
}

1064
t_symbol* pd_getplatform(void)
1065 1066
{
#ifdef __APPLE__
1067
    return gensym("darwin");
1068 1069
#endif
#ifdef __FreeBSD__
1070
    return gensym("freebsd");
1071 1072
#endif
#ifdef _WIN32
1073
    return gensym("win32");
1074 1075
#endif
#ifdef __linux__
1076
    return gensym("linux");
1077
#endif
1078 1079 1080 1081 1082 1083 1084 1085
    /* don't know the platform... */
    return gensym("unknown");
}

void pdinfo_platform(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
{
    t_atom at[1];
    SETSYMBOL(at, pd_getplatform());
1086