x_preset.c 99.4 KB
Newer Older
1 2 3
/*  preset_node and preset_hub implementation by
    Ivica Ico Bukvic <ico@vt.edu> (c) 2012
     distributed under GPL v3 license or newer
4 5 6 7 8 9 10 11 12 13
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "m_pd.h"
#include "g_canvas.h"
#include "x_preset.h"
#include "s_stuff.h"

14
#define PH_DEBUG 0
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
/*      changes in order happen when doing one of the following: cut, 
        undo cut, delete, undo delete, to front, and to back.
        this will trigger cascading check for all preset_nodes 
        is there a way to limit it to ones that are affected?
        potentially... by taking all the ordinal values above the one moved
        e.g. if object 1,5 was moved, everything above it
             (1,5,1 or 1,6 etc.) needs to be reevaluated BEFORE
             reevaluating moved object (if applicable)
             this does not affect objects like 2 or 2,7
             since it is a different branch altogether
             this is however, not currently implemented as said changes
             typically occur during editing and as such have little to no
             impact on the cpu optimization. live-coding may be affected,
             however, and further investigation there may be warranted. */

/*  gl_loc int array explanation:

    Each number corresponds to object or parent's location starting with
    root window. This ensures that each object has a unique ID within the
    preset even when using multiple instances of the same abstraction within
    the same patch. This also means that if the object has changed its location
    within the gl_list (e.g. via to_front/to_back), all corresponding IDs have
    to be updated in the parent.

    EXAMPLE:
    1,4,7 means the preset_node is 8th object in the subpatch or abstraction
    that is a child of 5th object in a parent patch, that is a child of 2nd
    object of the root patch.
44 45 46 47 48
*/

//==================== forward declarations ========================//

void preset_hub_add_a_node(t_preset_hub *h, t_preset_node *x);
49 50 51
void preset_hub_set_status(t_preset_hub *h, t_float f);
int  preset_hub_get_status(t_preset_hub *h);
void preset_hub_status(t_preset_hub *h);
52 53 54 55
void preset_hub_recall(t_preset_hub *h, t_float f);
void preset_hub_store(t_preset_hub *h, t_float f);
void preset_hub_delete_a_node(t_preset_hub *h, t_preset_node *x);
void preset_node_seek_hub(t_preset_node *x);
56
int  preset_hub_compare_loc(int *h_loc, int h_loc_length, int *n_loc, int n_loc_length);
57 58
void preset_hub_reset(t_preset_hub *h);
void preset_hub_purge(t_preset_hub *h);
59
void preset_hub_sort(t_preset_hub *x, t_float f);
60
void preset_hub_clear(t_preset_hub *x, t_float f);
61 62 63 64
void preset_hub_read(t_preset_hub *x, t_symbol *filename);
void preset_hub_write(t_preset_hub *x, t_symbol *filename);
void preset_hub_readpreset(t_preset_hub *x, t_symbol *filename);
void preset_hub_writepreset(t_preset_hub *x, t_symbol *filename, float preset);
65 66 67

static int preset_node_location_changed(t_preset_node *x);
static void preset_node_update_my_glist_location(t_preset_node *x);
68 69 70

//======================== global vars =============================//

71 72 73
/* following contains a linear list of all hubs and nodes which ensures easy
   access, pairing, deletion, etc. of all nodes and hubs regardless whether
   they are a part of subpatches, root canvases, or abstractions.
74 75 76 77 78 79 80
*/

t_glob_preset_node_list *gpnl;
t_glob_preset_hub_list *gphl;

int glob_preset_node_list_add(t_preset_node *x)
{
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    if(PH_DEBUG) fprintf(stderr,"glob_preset_node_list_add\n");
    t_glob_preset_node_list *n1, *n2;

    if (!gpnl)
    {
        gpnl = (t_glob_preset_node_list *)t_getbytes(sizeof(*gpnl));
        n2 = gpnl;
    }
    else
    {
        n1 = gpnl;
        while(n1->gpnl_next)
            n1 = n1->gpnl_next;
        n2 = (t_glob_preset_node_list *)t_getbytes(sizeof(*n2));
        n1->gpnl_next = n2;
    }
    n2->gpnl_paired = 0;
    n2->gpnl_node = x;
    return(0);
100 101 102 103
}

int glob_preset_node_list_delete(t_preset_node *x)
{
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
    if(PH_DEBUG) fprintf(stderr,"glob_preset_node_list_delete\n");
    t_glob_preset_node_list *n1, *n2;

    if (!gpnl)
        return(1);

    n1 = gpnl;
    n2 = gpnl;

    // first check if we are the first gpnl
    if (n2->gpnl_node == x)
    {
        if(PH_DEBUG) fprintf(stderr,"    we are first\n");
        gpnl = n2->gpnl_next;
        freebytes(n2, sizeof(*n2));
    } 
    else
    {
        if(PH_DEBUG) fprintf(stderr,"    we are NOT first\n");
        while (n2->gpnl_node != x && n2->gpnl_next)
        {
            n1 = n2;
            n2 = n2->gpnl_next;
        }
        if (n2->gpnl_node == x)
        {
            n1->gpnl_next = n2->gpnl_next;
            freebytes(n2, sizeof(*n2));
        }
        else
        {
            // we should never get here
            if(PH_DEBUG)
                fprintf(stderr,
                    "error, could not find appropriate gpnl to delete\n");
            return(1);
        }
    }
    
    return(0);
144 145 146 147
}

int glob_preset_node_list_update_paired(t_preset_node *x, int paired)
{
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
    if(PH_DEBUG)
        fprintf(stderr, "glob_preset_node_list_update_paired %d\n",
            paired);
    t_glob_preset_node_list *n;

    if (!gpnl)
        return(1);

    n = gpnl;

    while (n->gpnl_node != x && n->gpnl_next)
    {
        n = n->gpnl_next;
    }
    if (n->gpnl_node == x)
    {
        n->gpnl_paired = paired;
    }
    else
    {
        // we should never get here
        if (PH_DEBUG)
            fprintf(stderr, "error, could not find appropriate gpnl to pair\n");
        return(1);
    }
    
    return(0);
175 176 177 178
}

void glob_preset_node_list_seek_hub(void)
{
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    if (PH_DEBUG) fprintf(stderr,"glob_preset_node_list_seek_hub\n");
    if (we_are_undoing)
        return;
    t_glob_preset_node_list *nl;

    if (gpnl)
    {
        nl = gpnl;
        while(nl)
        {
            if(PH_DEBUG) fprintf(stderr,"    got node\n");
            if (!nl->gpnl_paired)
            {
                if(PH_DEBUG) fprintf(stderr,"    seeking\n");
                preset_node_seek_hub(nl->gpnl_node);
            }
            nl = nl->gpnl_next;
        }
    }
    if(PH_DEBUG) fprintf(stderr,"    done\n");
199 200
}

201 202
/* this should be called whenever glist has been changed
   (tofront/back, cut, delete, undo/redo cut/delete) */
203 204
void glob_preset_node_list_check_loc_and_update(void)
{
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
    if(PH_DEBUG) fprintf(stderr,"glob_preset_node_list_check_loc_and_update\n");
    t_glob_preset_hub_list *hl;
    t_preset_hub_data *hd;
    int i = 0;

    if (gphl)
    {
        hl = gphl;
        while(hl)
        {
            if(PH_DEBUG) fprintf(stderr,"    searching\n");
            hd = hl->gphl_hub->ph_data;
            while (hd)
            {
                if(PH_DEBUG) fprintf(stderr,"    checking data\n");
                if (hd->phd_node)
                {
                    if(PH_DEBUG) fprintf(stderr,"    node is active\n");
                    preset_node_update_my_glist_location(hd->phd_node);
                    if (preset_node_location_changed(hd->phd_node))
                    {
                        if(PH_DEBUG)
                            fprintf(stderr, "    location changed...adjusting "
                                            "length %d to %d\n",
                                hd->phd_pn_gl_loc_length,
                                hd->phd_node->pn_gl_loc_length);
                        hd->phd_pn_gl_loc_length =
                            hd->phd_node->pn_gl_loc_length;
                        hd->phd_pn_gl_loc = (int*)realloc(hd->phd_pn_gl_loc,
                            hd->phd_pn_gl_loc_length*sizeof(int));
                        for (i=0; i < hd->phd_pn_gl_loc_length; i++)
                        {
                            if(PH_DEBUG)
                                fprintf(stderr,"    loc old:%d new:%d\n",
                                    hd->phd_pn_gl_loc[i],
                                    hd->phd_node->pn_gl_loc[i]);
                            hd->phd_pn_gl_loc[i] = hd->phd_node->pn_gl_loc[i];
                        }
                    }
                }
                hd = hd->phd_next;
            }
            hl = hl->gphl_next;
        }
    }
250 251
}

252 253
int glob_preset_hub_list_add(t_preset_hub *x)
{
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    if(PH_DEBUG) fprintf(stderr,"glob_preset_hub_list_add\n");
    t_glob_preset_hub_list *h1, *h2;

    if (!gphl)
    {
        gphl = (t_glob_preset_hub_list *)t_getbytes(sizeof(*gphl));
        h2 = gphl;
    }
    else
    {
        h1 = gphl;
        while(h1->gphl_next)
            h1 = h1->gphl_next;
        h2 = (t_glob_preset_hub_list *)t_getbytes(sizeof(*h2));
        h1->gphl_next = h2;
    }
    h2->gphl_hub = x;
    return(0);
272 273 274 275
}

int glob_preset_hub_list_delete(t_preset_hub *x)
{
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
    if(PH_DEBUG) fprintf(stderr,"glob_preset_hub_list_delete\n");
    t_glob_preset_hub_list *h1, *h2;

    if (!gphl)
        return(1);

    h1 = gphl;
    h2 = gphl;

    // first check if we are the first gphl
    if (h2->gphl_hub == x)
    {
        gphl = h2->gphl_next;
        freebytes(h2, sizeof(*h2));
    } 
    else
    {
        while (h2->gphl_hub != x && h2->gphl_next)
        {
            h1 = h2;
            h2 = h2->gphl_next;
        }
        if (h2->gphl_hub == x)
        {
            h1->gphl_next = h2->gphl_next;
            freebytes(h2, sizeof(*h2));
        }
        else
        {
            // we should never get here
            if(PH_DEBUG)
                fprintf(stderr, "error, could not find appropriate "
                                "gphl to delete\n");
            return(1);
        }
    }    
    
    return(0);
314 315 316 317
}

//====================== end global vars ===========================//

318 319 320 321
//====================== helper functions ==========================//

static int compare_hub_node_names(t_atom *a, t_atom *b)
{
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
    if(PH_DEBUG)
    {
        fprintf(stderr,"    comparing: ");
        if (a->a_type == A_SYMBOL)
            fprintf(stderr,"A_SYMBOL %s |", atom_getsymbol(a)->s_name);
        else
            fprintf(stderr,"A_FLOAT %f |", atom_getfloat(a));
        if (b->a_type == A_SYMBOL)
            fprintf(stderr," A_SYMBOL %s\n", atom_getsymbol(b)->s_name);
        else
            fprintf(stderr," A_FLOAT %f\n", atom_getfloat(b));
    }
    if (a->a_type == A_SYMBOL && b->a_type == A_SYMBOL)
    {
        if (!strcmp(atom_getsymbol(a)->s_name, atom_getsymbol(b)->s_name))
        {
            return(0);
        }
    }
    else if (a->a_type == A_FLOAT && b->a_type == A_FLOAT)
    {
        if (atom_getfloat(a) == atom_getfloat(b))
        {
            return(0);
        }
    }
    return 1;
349 350 351 352
}

//==================== end helper functions ========================//

353 354
//======================== preset_node =============================//

355 356
/* we declare this class as part of the g_canvas.h (to expose it to the
   rest of the code) */
357 358 359

static void preset_node_update_my_glist_location(t_preset_node *x)
{
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
    if(PH_DEBUG) fprintf(stderr,"node_update_glist_location\n");
    // location is calculated in respect to the hub (if any), otherwise it is 0
    t_preset_hub *h;
    t_canvas *c = x->pn_canvas;
    int found = 0;

    // do all this only if we are already paired with a hub
    if (x->pn_hub)
    {
        if (x->pn_old_gl_loc && x->pn_old_gl_loc != x->pn_gl_loc)
            free(x->pn_old_gl_loc);
        x->pn_old_gl_loc = x->pn_gl_loc;
        x->pn_old_gl_loc_length = x->pn_gl_loc_length;
    
        x->pn_gl_loc = NULL;
        x->pn_gl_loc_length = 0;

        // let's try to find our hub (if any)
        int depth = 0;
        while (!found && c)
        {
            if (c->gl_phub)
            {
                if(PH_DEBUG) fprintf(stderr,"    gl_phub != NULL\n");
                h = c->gl_phub;
                while (h)
                {
                    if(PH_DEBUG) fprintf(stderr,"    analyzing hub\n");
                    if (!compare_hub_node_names(h->ph_name, x->pn_hub_name))
                    {
                        if(PH_DEBUG) fprintf(stderr,"    found a match\n");
                        found = 1;
                        break;
                    }
                    h = h->ph_next;
                }
            }
            if (found) break; /* need to stop the main while loop here
                                 before we increase depth unnecessarily below
                                 otherwise hubs in subpatches will fail to pair
                                 its local nodes */
            c = c->gl_owner;
            if (c)
                depth++;
        }

        if (found)
        {

            if(PH_DEBUG) fprintf(stderr,"    depth = %d\n", depth);
    
            // allocate depth array
            x->pn_gl_loc = (int*)calloc(depth+1, sizeof(x->pn_gl_loc));

            // now let's figure out where each of the child gobj's location is
            // starting from the back
            int i = 0;    // iteration within each gl_list
            int j = 0;    // number of gl_list to traverse
            c = x->pn_canvas;
            t_gobj *g = c->gl_list;
            t_gobj *target = (t_gobj *)x;

            for (j = depth; j >= 0; j--)
            {
                /* have to do sanity check for g in the event this is run at
                   patch creation time */
                while (g && g != target)
                {
                    g = g->g_next;
                    i++;
                    //if(PH_DEBUG) fprintf(stderr,"    searching... %d\n", i);
                }
                /* even if the g fails sanity check due to creation time,
                   it will still land on the last created element whose pointer
                   at this point is still null since this means this is being
                   called at the end of the preset_node_new call */
                if (PH_DEBUG)
                    fprintf(stderr,"    location = %d %lx %lx\n",
                        i, (t_int)g, (t_int)target);
                x->pn_gl_loc[j] = i;

                /* now readjust the target, c, g, and i variables for the
                   next level up */
                if (c->gl_owner)
                {
                    target = (t_gobj *)c;
                    c = c->gl_owner;
                    g = c->gl_list;
                    i = 0;
                }
            }
            x->pn_gl_loc_length = depth+1;

            if(PH_DEBUG) fprintf(stderr,"    final structure:\n");
            for (j = 0; j < x->pn_gl_loc_length; j++)
            {
                if(PH_DEBUG) fprintf(stderr,"    %d: %d\n", j, x->pn_gl_loc[j]);
            }
        }
        else
        {
            if(PH_DEBUG)
                fprintf(stderr,"    preset_node: no matching hub found\n");
        }

        /* finally if this is the first time we are creating the object,
           old_location should be the same as the current location */
        if (x->pn_old_gl_loc_length == 0)
        {
            x->pn_old_gl_loc = x->pn_gl_loc;
            x->pn_old_gl_loc_length = x->pn_gl_loc_length;
        }
    }
473 474
}

475 476 477
/* this is called during the canvas creation (e.g. at load time) as part of
   pre-loadbang event, during object creation (in case it is manually created),
   as well as when hubs try to query nodes that have not been paired yet.  */
478 479
void preset_node_seek_hub(t_preset_node *x)
{
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
    if(PH_DEBUG) fprintf(stderr,"preset_node_seek_hub %lx\n", (t_int)x->pn_hub);
    if (we_are_undoing)
        return;
    t_canvas *y = x->pn_canvas;
    t_preset_hub *h;

    if (!x->pn_hub)
    {
        if(PH_DEBUG) fprintf(stderr,"    have to seek\n");
        while (!x->pn_hub && y)
        {
            h = y->gl_phub;
            while (h)
            {
                if (!compare_hub_node_names(h->ph_name, x->pn_hub_name))
                {
                    x->pn_hub = h;
                    if(PH_DEBUG) fprintf(stderr,"    node found hub\n");
                    // update our location in respect to the newfound hub
                    preset_node_update_my_glist_location(x);
                    /* add a node on the hub's list of nodes and copy
                       location to its struct */
                    preset_hub_add_a_node(x->pn_hub, x);
                    /* reflect that this node is paired in the global list
                       of all existing nodes */
                    glob_preset_node_list_update_paired(x, 1);        
                    break;
                }
                h = h->ph_next;            
            }
            y = y->gl_owner;
        }
    }
513 514 515 516
}

static int preset_node_location_changed(t_preset_node *x)
{
517 518 519 520 521 522 523 524 525 526 527
    int i;
    if (x->pn_old_gl_loc_length != x->pn_gl_loc_length)
    {
        return(1);
    }
    for (i = 0; i < x->pn_gl_loc_length; i++)
    {
        if (x->pn_gl_loc[i] != x->pn_old_gl_loc[i])
            return(1);
    }
    return(0);
528 529
}

530
static void preset_node_anything(t_preset_node *x, t_symbol *s, int argc, t_atom *argv)
531
{
532
    if (PH_DEBUG) fprintf(stderr,"preset_node_anything %lx %s %d\n", (t_int)x, s->s_name, argc);
533 534 535 536 537 538 539 540 541 542 543 544
    int i;
    alist_list(&x->pn_val, 0, argc, argv);
    if (PH_DEBUG)
    {
        if (x->pn_val.l_vec->l_a.a_type == A_SYMBOL)
            fprintf(stderr,"    %lx data is %s\n",
                (t_int)x, x->pn_val.l_vec->l_a.a_w.w_symbol->s_name);
        else if (x->pn_val.l_vec->l_a.a_type == A_FLOAT)
            fprintf(stderr,"    %lx data is %f\n",
                (t_int)x, x->pn_val.l_vec->l_a.a_w.w_float);
    }
    // check for pointers and warn user presetting them has not been tested
545 546 547 548
    for (i = 0; i < x->pn_val.l_n; i++)
    {
        if (x->pn_val.l_vec[i].l_a.a_type == A_POINTER)
        {
549 550 551 552 553
            pd_error(x, "preset_node preset received a pointer as part "
                        "of a list--this has not been tested, use at your "
                        "own risk and please report any successes/failures. "
                        "Thank you!");
            break;
554 555
        }
    }
556
    if (x->pn_hub)
557
        preset_hub_set_status(x->pn_hub, 1);    
558 559
}

560
//======== following functions are for interaction with the hub/pd =======//
561 562 563

int preset_node_check_location(t_preset_node *x)
{
564 565 566 567 568 569
    if (x->pn_hub)
    {
        preset_node_update_my_glist_location(x);
        return(preset_node_location_changed(x));
    }
    return(0);
570 571 572 573
}

void preset_node_request_hub_recall(t_preset_node *x, t_float f)
{
574 575
    if (x->pn_hub)
        preset_hub_recall(x->pn_hub, f);
576 577 578 579
}

void preset_node_request_hub_store(t_preset_node *x, t_float f)
{
580 581
    if (x->pn_hub)
        preset_hub_store(x->pn_hub, f);
582 583
}

584 585
void preset_node_request_hub_read(t_preset_node *x, t_symbol *filename)
{
586 587
    if (x->pn_hub)
        preset_hub_read(x->pn_hub, filename);
588 589 590 591
}

void preset_node_request_hub_write(t_preset_node *x, t_symbol *filename)
{
592 593
    if (x->pn_hub)
        preset_hub_write(x->pn_hub, filename);
594 595 596 597
}

void preset_node_request_hub_readpreset(t_preset_node *x, t_symbol *filename)
{
598 599
    if (x->pn_hub)
        preset_hub_readpreset(x->pn_hub, filename);
600 601
}

602 603
void preset_node_request_hub_writepreset(t_preset_node *x, t_symbol *filename,
    float preset)
604
{
605 606
    if (x->pn_hub)
        preset_hub_writepreset(x->pn_hub, filename, preset);
607 608
}

609
void preset_node_set_and_output_value(t_preset_node *x, t_alist val)
610
{
611 612 613 614 615 616
    if(PH_DEBUG)
        fprintf(stderr,"preset_node_set_and_output_value %lx\n", (t_int)x);
    t_atom *outv;
    if (val.l_n > 0)
    {
        alist_clear(&x->pn_val);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
617
        alist_clone(&val, &x->pn_val, 0, val.l_n);
618
        XL_ATOMS_ALLOCA(outv, x->pn_val.l_n);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
619
        alist_toatoms(&x->pn_val, outv, 0, x->pn_val.l_n);
620 621 622 623 624 625 626 627 628 629 630 631
        outlet_list(x->pn_outlet, &s_list, x->pn_val.l_n, outv);
        if(PH_DEBUG)
        {
            if (outv->a_type == A_SYMBOL)
                fprintf(stderr,"    %lx outputs %s\n",
                    (t_int)x, outv->a_w.w_symbol->s_name);
            else if (outv->a_type == A_FLOAT)
                fprintf(stderr,"    %lx outputs %f\n",
                    (t_int)x, outv->a_w.w_float);
        }
        XL_ATOMS_FREEA(outv, x->pn_val.l_n);
    }
632 633
}

634 635
void preset_node_clear(t_preset_node *x, t_float f)
{
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
    t_atom ap[2];
    t_preset_hub_data *hd2;
    t_node_preset *np1, *np2;
    int changed = 0;

    if(PH_DEBUG) fprintf(stderr,"preset_node_clear %d\n", (int)f);

    if (x->pn_hub)
    {
        hd2 = x->pn_hub->ph_data;

        // only remove this object's preset
        if (hd2)
        {
            if(PH_DEBUG) fprintf(stderr,"    got ph_data\n");
            while (hd2 && hd2->phd_node != x)
            {
                hd2 = hd2->phd_next;
            }
            if (hd2)
            {
                np1 = hd2->phd_npreset;
                // if it is first one
659
                if (np1 && np1->np_preset == (int)f)
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
                {
                    hd2->phd_npreset = np1->np_next;
                    if (np1->np_val.l_n)
                        alist_clear(&np1->np_val);
                    freebytes(np1, sizeof(*np1));
                    changed = 1;
                    if(PH_DEBUG)
                        fprintf(stderr,"    found preset to delete (first)\n");
                }
                else
                {
                    while (np1)
                    {
                        np2 = np1->np_next;
                        if (np2 && np2->np_preset == (int)f)
                        {
                            np1->np_next = np2->np_next;
                            if (np2->np_val.l_n)
                                alist_clear(&np2->np_val);
                            freebytes(np2, sizeof(*np2));
                            changed = 1;
                            if(PH_DEBUG)
                                fprintf(stderr,"    found preset to delete\n");
                            break;
                        }
                        np1 = np1->np_next;
                    }
                }
            }
        }
690 691 692
        SETFLOAT(ap+0, f);
        SETFLOAT(ap+1, (t_float)changed);
        outlet_anything(x->pn_hub->ph_outlet, gensym("node_clear"), 2, ap);
693
    }
694
    if (changed && (!x->pn_hub || !x->pn_hub->ph_extern_file))
695
        canvas_dirty(x->pn_hub->ph_canvas, 1);
696 697 698
}


699 700 701 702
void preset_node_clearall(t_preset_node *x, t_float f)
{
    if (x->pn_hub)
        preset_hub_clear(x->pn_hub, f);
703 704
}

705 706 707 708
void preset_node_reset(t_preset_node *x)
{
    if (x->pn_hub)
        preset_hub_reset(x->pn_hub);
709 710
}

711 712 713 714
void preset_node_purge(t_preset_node *x)
{
    if (x->pn_hub)
        preset_hub_purge(x->pn_hub);
715 716
}

717 718 719 720
void preset_node_sort(t_preset_node *x, t_float f)
{
    if (x->pn_hub)
        preset_hub_sort(x->pn_hub, f);
721 722
}

723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
void preset_node_status(t_preset_node *x)
{
    // in preset node we also have -1 value (unlike hubs)
    // -1 = node is not paired
    //  0 = hub is clean
    //  1 = hub nodes have been changed since preset was stored (dirty)
    int value = -1;
    if (x->pn_hub)
    {
        preset_hub_status(x->pn_hub);
        value = preset_hub_get_status(x->pn_hub);
    }

    t_atom ap[1];
    SETFLOAT(ap+0, (t_float)value);
    outlet_anything(x->pn_status_outlet, gensym("status"), 1, ap); 
}

741
//=========== end functions are for interaction with the hub ============//
742

743 744
static void preset_node_set(t_preset_node *x, t_symbol *s, int argc,
    t_atom *argv)
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
    if(PH_DEBUG) fprintf(stderr,"preset_node_set %d\n", argc);

    if (argc == 0)
    {
        x->pn_hub_name->a_type = A_SYMBOL;
        x->pn_hub_name->a_w.w_symbol = &s_;
        if (PH_DEBUG) fprintf(stderr,"    name is null symbol\n");        
    }
    else
    {
        if (argv[0].a_type == A_SYMBOL)
            SETSYMBOL(x->pn_hub_name, atom_getsymbolarg(0, argc, argv));
        else
            SETFLOAT(x->pn_hub_name, atom_getfloatarg(0, argc, argv));
        if(PH_DEBUG)
        {
            if (x->pn_hub_name->a_type == A_SYMBOL)
                fprintf(stderr,"    name is a symbol %s\n",
                    atom_getsymbol(x->pn_hub_name)->s_name);
            else fprintf(stderr,"    name is a float %f\n",
                atom_getfloat(x->pn_hub_name));
        }
    }

    if (x->pn_hub)
    {
        preset_hub_delete_a_node(x->pn_hub, x);
        glob_preset_node_list_update_paired(x, 1);
    }

    x->pn_hub = NULL;
    preset_node_seek_hub(x);
778 779
}

780 781
static void *preset_node_new(t_symbol *s, int argc, t_atom *argv)
{
782 783 784
    if (PH_DEBUG) fprintf(stderr,"===preset_node_new===\n");
    t_glist *glist=(t_glist *)canvas_getcurrent();
    t_canvas *canvas = (t_canvas *)glist_getcanvas(glist);
785 786 787

    t_preset_node *x = (t_preset_node *)pd_new(preset_node_class);

788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
    x->pn_hub_name = (t_atom *)getbytes(sizeof(t_atom));

    /* read creation arguments and substitute default for objects without
       optional arguments */
    if (argc == 0)
    {    
        x->pn_hub_name->a_type = A_SYMBOL;
        x->pn_hub_name->a_w.w_symbol = &s_;
        if (PH_DEBUG) fprintf(stderr,"    name is null symbol\n");
    }
    else
    {
        if (argv[0].a_type == A_SYMBOL)
            SETSYMBOL(x->pn_hub_name, atom_getsymbolarg(0, argc, argv));
        else
            SETFLOAT(x->pn_hub_name, atom_getfloatarg(0, argc, argv));
        if(PH_DEBUG)
        {
            if (x->pn_hub_name->a_type == A_SYMBOL)
                fprintf(stderr,"    name is a symbol %s\n",
                    atom_getsymbol(x->pn_hub_name)->s_name);
            else fprintf(stderr,"    name is a float %f\n",
                atom_getfloat(x->pn_hub_name));
        }
    }

    x->pn_canvas = canvas;
    //t_preset_hub *h;
    alist_init(&x->pn_val);

    x->pn_hub = NULL;
    x->pn_gl_loc_length = 0;
    x->pn_gl_loc = NULL;
    x->pn_old_gl_loc_length = 0;  
    x->pn_old_gl_loc = NULL;
823 824
    x->pn_outlet = outlet_new(&x->pn_obj, 0);
    x->pn_status_outlet = outlet_new(&x->pn_obj, 0);
825 826 827 828 829 830 831 832 833 834

    glob_preset_node_list_add(x);

    /* now we seek our potential hub (if it exists), this is also called as
       part of pre-loadbang if the patch is being loaded (this one is for
       manually created objects) do this only if we are not undoing, otherwise,
       we'll have the undo do it for us once it has repositioned objects to
       their original locations (the undo check is done inside the
       preset_node_seek_hub) */
    preset_node_seek_hub(x);
835 836 837 838 839 840

    return(x);
}

static void preset_node_free(t_preset_node* x)
{
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
    // deactivate a node on the hub's list of nodes
    if (x->pn_hub)
        preset_hub_delete_a_node(x->pn_hub, x);
    glob_preset_node_list_delete(x);

    alist_clear(&x->pn_val);

    /* the two arrays can point to same locations so here we prevent double
       free (this is only possible initially when the object is first
       instantiated) */
    if (x->pn_gl_loc != x->pn_old_gl_loc)
    {
        free(x->pn_gl_loc);
        free(x->pn_old_gl_loc);
    }
    else
    {
        free(x->pn_gl_loc);
    }    
    //if (x->pn_hub_name->a_type == A_SYMBOL &&
    //    x->pn_hub_name->a_w.w_symbol == &s_)
    if (x->pn_hub_name)
        freebytes(x->pn_hub_name, sizeof(t_atom));
864 865 866 867
}

void preset_node_setup(void)
{
868 869 870 871 872 873 874 875 876
    preset_node_class = class_new(gensym("preset_node"), 
        (t_newmethod)preset_node_new, (t_method)preset_node_free, 
        sizeof(t_preset_node), 0, A_GIMME, 0);

    /* have to call this after everything has loaded, otherwise, the hub may
       not yet exist we do this using pre-loadbang call that is issued before
       other loadbangs, otherwise out-of-order loadbangs can issue preset
       recall before the preset has been properly configured */
    class_addmethod(preset_node_class, (t_method)preset_node_seek_hub,
877 878 879 880 881 882
        gensym("pre-loadbang"), 0);

    class_addmethod(preset_node_class, (t_method)preset_node_request_hub_recall,
        gensym("recall"), A_DEFFLOAT, 0);
    class_addmethod(preset_node_class, (t_method)preset_node_request_hub_store,
        gensym("store"), A_DEFFLOAT, 0);
883
    class_addmethod(preset_node_class, (t_method)preset_node_set,
884
        gensym("set"), A_GIMME, 0);
885

886 887 888 889 890 891 892 893
    class_addmethod(preset_node_class, (t_method)preset_node_clear,
        gensym("clear"), A_DEFFLOAT, 0);
    class_addmethod(preset_node_class, (t_method)preset_node_clearall,
        gensym("clearall"), A_DEFFLOAT, 0);
    class_addmethod(preset_node_class, (t_method)preset_node_reset,
        gensym("reset"), A_NULL, 0);
    class_addmethod(preset_node_class, (t_method)preset_node_purge,
        gensym("purge"), A_NULL, 0);
894 895
    class_addmethod(preset_node_class, (t_method)preset_node_sort,
        gensym("sort"), A_DEFFLOAT, 0);
896 897
    class_addmethod(preset_node_class, (t_method)preset_node_status,
        gensym("status"), A_NULL, 0);
898

899 900 901 902
    class_addmethod(preset_node_class, (t_method)preset_node_request_hub_read,
        gensym("read"), A_DEFSYM, 0);
    class_addmethod(preset_node_class, (t_method)preset_node_request_hub_write,
        gensym("write"), A_DEFSYM, 0);
903 904
    class_addmethod(preset_node_class,
        (t_method)preset_node_request_hub_readpreset,
905
        gensym("readpreset"), A_DEFSYM, A_DEFFLOAT, 0);
906 907
    class_addmethod(preset_node_class,
        (t_method)preset_node_request_hub_writepreset,
908 909
        gensym("writepreset"), A_DEFSYM, A_DEFFLOAT, 0);

910 911
    // we use anything to cover virtually all presetable types of data
    class_addanything(preset_node_class, preset_node_anything);
912 913 914 915 916 917 918 919 920 921
}

//====================== end preset_node ===========================//

//======================== preset_hub ==============================//

// we declare this class as part of the g_canvas.h (to expose it to the rest of the code)

typedef enum
{
922
    H_NONE,
923
    H_NODE,
924
    H_LOCATION,
925 926 927 928
    H_PRESET,
    H_PRESET_DATA
}  t_hub_parser;

929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
/*    syntax for saving a preset hub (all in a single line, here it is
    separated for legibility sakes):
    #X obj X Y preset_hub NAME(optional) file(optional) 1(optional) %hidden%
    %node% LOCATION_ARRAY_LENGTH LOCATION_ARRAY_(INT) 1 2 3 etc.
    %preset% 1 data
    %preset% 2 4
    etc
    %node% 2 0
    %preset% 1 5.561
    %preset% 3 7.00001 anything including lists
    %preset% 5 some_text
    ;

    NB: %hidden% is used to hide optional arguments following that argument
        it can be used by any other object as well

    file(optional) arg is used to tell hub it will be dealing exclusively with
    external files (this prevents dirty flag when storing new presets)
    
    1(optional) arg is used for k12 mode to prevent copying and pasting
    multiple hubs with same names
950 951 952 953
*/

void preset_hub_save(t_gobj *z, t_binbuf *b)
{
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
    if(PH_DEBUG) fprintf(stderr,"preset_hub_save\n");
    int i;
    t_preset_hub_data *phd;
    t_node_preset *np;

    t_preset_hub *x = (t_preset_hub *)z;

    binbuf_addv(b, "ssii", gensym("#X"), gensym("obj"), (int)x->ph_obj.te_xpix,
        (int)x->ph_obj.te_ypix);

    binbuf_addbinbuf(b, x->ph_obj.te_binbuf);

    binbuf_addv(b, "s", gensym("%hidden%"));

    /* save preset data with the patch only if we are not
       saving into an external file */
    if (!x->ph_extern_file)
    {

        phd = x->ph_data;
        while (phd)
        {
976 977 978 979
        	// only save node and preset if it is active, this prevents stale
        	// data that will be unnecessary since undo is also purged once
        	// the file is reloaded from the disk
        	if (phd->phd_node)
980
            {
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
	            if(PH_DEBUG) fprintf(stderr,"    saving phd\n");
	            /* designate a node and state whether it is active or disabled
	               (disabled nodes are ones that have presets saved but have been
	               deleted since-- we keep these in the case of undo actions during
	               the session that may go beyond saving something into a file) */
	            binbuf_addv(b, "si", gensym("%node%"), phd->phd_pn_gl_loc_length);

	            // gather info about the length of the node's location and store it
	            for (i = 0; i < phd->phd_pn_gl_loc_length; i++)
	            {
	                binbuf_addv(b,"i", (int)phd->phd_pn_gl_loc[i]);
	            }

	            // save preset data
	            np = phd->phd_npreset;
	            while (np)
	            {
	                if (np->np_val.l_n > 0)
	                {
	                    binbuf_addv(b, "si", gensym("%preset%"),
	                        (int)np->np_preset);
	                    for (i = 0; i < np->np_val.l_n; i++)
	                    {
	                        if (np->np_val.l_vec[i].l_a.a_type == A_FLOAT)
	                            binbuf_addv(b, "f",
	                                np->np_val.l_vec[i].l_a.a_w.w_float);
	                        else if (np->np_val.l_vec[i].l_a.a_type == A_SYMBOL)
	                            binbuf_addv(b, "s",
	                                np->np_val.l_vec[i].l_a.a_w.w_symbol);    
	                    }
	                }
	                np = np->np_next;
	            }
	        }
1015 1016 1017 1018 1019 1020

            phd = phd->phd_next;
        }
    }
    if (PH_DEBUG) fprintf(stderr,"    done\n");
    binbuf_addv(b, ";");
1021 1022 1023 1024
}

void preset_hub_bang(t_preset_hub *x)
{
1025 1026
    t_atom ap[1];
    SETFLOAT(ap+0, (t_float)x->ph_preset);
1027 1028 1029
    outlet_anything(x->ph_outlet, gensym("current"), 1, ap);
}

1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
void preset_hub_set_status(t_preset_hub *x, t_float f)
{
    x->ph_status = (int)f;
}

int preset_hub_get_status(t_preset_hub *x)
{
    return(x->ph_status);
}

void preset_hub_status(t_preset_hub *x)
1041 1042
{
    t_atom ap[1];
1043 1044
    SETFLOAT(ap+0, (t_float)x->ph_status);
    outlet_anything(x->ph_outlet, gensym("status"), 1, ap);    
1045 1046
}

1047 1048
void preset_hub_recall(t_preset_hub *x, t_float f)
{
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
    if(PH_DEBUG) fprintf(stderr,"hub_recall\n");
    t_atom ap[2];
    t_preset_hub_data *hd;
    t_node_preset *np;
    t_float valid = 0;

    if (f>=0)
    {
        // check if preset exists and apply it
        if (x->ph_data)
        {
            hd = x->ph_data;
            while (hd)
            {
1063
                if(PH_DEBUG) fprintf(stderr,"    searching\n");
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
                // now check if the object is active (node pointer is not NULL)
                if (hd->phd_node)
                {
                    if(PH_DEBUG) fprintf(stderr,"    object active\n");
                    if (hd->phd_npreset)
                    {
                        if(PH_DEBUG) fprintf(stderr,"    object has presets\n");
                        np = hd->phd_npreset;
                        while (np)
                        {
                            if(PH_DEBUG)
                                fprintf(stderr,"    searching presets\n");
                            if (np->np_preset == (int)f)
                            {
                                valid = 1;
                                if(PH_DEBUG)
                                    fprintf(stderr,"    valid %d\n",
                                        (hd->phd_node ? 1:0));
                                if (np->np_val.l_n > 0)
                                    preset_node_set_and_output_value(
                                        hd->phd_node, np->np_val);
                                break;
                            }
                            np = np->np_next;
                        }
                    }
                }
                hd = hd->phd_next;
            }
        }
        if (valid)
1095
        {
1096
            x->ph_preset = f;
1097 1098
            x->ph_status = 0;
        }
1099 1100 1101 1102 1103 1104
        if(PH_DEBUG) fprintf(stderr,"    done\n");

        SETFLOAT(ap+0, f);
        SETFLOAT(ap+1, (t_float)valid);
        outlet_anything(x->ph_outlet, gensym("recall"), 2, ap);
    }
1105 1106
}

1107
void preset_hub_store(t_preset_hub *h, t_float f)
1108
{
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
    if(PH_DEBUG) fprintf(stderr,"preset_hub_store\n");
    t_atom ap[2];
    t_preset_hub_data *hd1;
    t_node_preset *np1, *np2;
    int overwrite;
    int changed = 0;

    np1 = NULL;
    np2 = NULL;

    int dspstate = canvas_suspend_dsp();

    if (f>=0)
    {
        //check if there are any existing nodes
        if (h->ph_data)
        {
            hd1 = h->ph_data;
            while (hd1)
            {
                if(PH_DEBUG) fprintf(stderr,"    analyzing phd\n");
                if (hd1->phd_node)
                {
                    if(PH_DEBUG) fprintf(stderr,"    node is active\n");
                    /* only if the node is active (not NULL/disabled)

                       reset np1 and np2 from the previous run
                       (if we have more phd_nodes, stale data will append
                       new presets to the last phd_node eventually resulting
                       in crash) */
                    np1 = NULL;
                    np2 = NULL;

                    overwrite = 0;
                    if (hd1->phd_npreset)
                    {
                        /* if this node has already pre-existing presets
                           first check if there is already one we need to
                           overwrite */
                        np2 = hd1->phd_npreset;
                        while (np2)
                        {
                            if (np2->np_preset == (int)f)
                            {
                                if(PH_DEBUG)
                                    fprintf(stderr,"    overwriting\n");
                                overwrite = 1;
                                break;
                            }
                            np1 = np2;
                            np2 = np2->np_next;
                        }
                    }

                    if (!overwrite && hd1->phd_node->pn_val.l_n > 0)
                    {
                        /* we need to create a new preset (this is also true
                           if hd1->phd_npreset is NULL) */
                        changed = 1;
                        if (PH_DEBUG)
                            fprintf(stderr,"    creating new preset\n");
                        np2 = (t_node_preset *)t_getbytes(sizeof(*np2));
                        if (np1)
                            np1->np_next = np2;
                        else hd1->phd_npreset = np2;
                        np2->np_preset = (int)f;
                        np2->np_next = NULL;
                    }

                    if (hd1->phd_node->pn_val.l_n > 0)
                    {
                        changed = 1;
                        if(PH_DEBUG)
                        {
                            fprintf(stderr,"    node data len = %d, "
                                           "old hub data len = %d\n",
                                hd1->phd_node->pn_val.l_n, np2->np_val.l_n);
                            if (hd1->phd_node->pn_val.l_vec->l_a.a_type ==
                                A_SYMBOL)
                            {
                                fprintf(stderr,"    %lx outputs %s\n",
                                    (t_int)hd1->phd_node,
                                    hd1->phd_node->pn_val.l_vec->l_a.a_w.w_symbol->s_name);
                            }
                            else if (hd1->phd_node->pn_val.l_vec->l_a.a_type ==
                                     A_FLOAT)
                            {
                                fprintf(stderr,"    %lx outputs %f\n",
                                (t_int)hd1->phd_node,
                                hd1->phd_node->pn_val.l_vec->l_a.a_w.w_float);
                            }
                        }
                        alist_clear(&np2->np_val);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1202 1203
                        alist_clone(&hd1->phd_node->pn_val, &np2->np_val,
                            0, hd1->phd_node->pn_val.l_n);
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
                        if (PH_DEBUG)
                        {
                            fprintf(stderr,"    node data len = %d, "
                                           "NEW hub data len = %d\n",
                                hd1->phd_node->pn_val.l_n, np2->np_val.l_n);
                            if (hd1->phd_node->pn_val.l_vec->l_a.a_type ==
                                A_SYMBOL)
                            {
                                fprintf(stderr,"    %lx outputs %s\n",
                                    (t_int)hd1->phd_node,
                                    np2->np_val.l_vec->l_a.a_w.w_symbol->s_name);
                            }
                            else if (hd1->phd_node->pn_val.l_vec->l_a.a_type ==
                                     A_FLOAT)
                            {
                                fprintf(stderr,"    %lx outputs %f\n",
                                    (t_int)hd1->phd_node,
                                    np2->np_val.l_vec->l_a.a_w.w_float);
                            }
                        }
                        /* finally if this is the first preset,
                           hd1->phd_npreset will be NULL so, let's have it
                           point to the newly created n2 */
                        if (!hd1->phd_npreset)
                            hd1->phd_npreset = np2;
                    }
                }
                hd1 = hd1->phd_next;
            }
        }
        canvas_resume_dsp(dspstate);

        if (changed && !h->ph_extern_file) canvas_dirty(h->ph_canvas, 1);
1237
        h->ph_status = 0;
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248

        SETFLOAT(ap+0, f);
        SETFLOAT(ap+1, 1);
        outlet_anything(h->ph_outlet, gensym("store"), 2, ap);
    }
    else
    {
        SETFLOAT(ap+0, f);
        SETFLOAT(ap+1, 0);
        outlet_anything(h->ph_outlet, gensym("store"), 2, ap);
    }
1249 1250
}

1251 1252
int preset_hub_compare_loc(int *h_loc, int h_loc_length, int *n_loc,
    int n_loc_length)
1253
{
1254 1255 1256 1257 1258 1259 1260 1261 1262
    int i;
    if (h_loc_length != n_loc_length)
        return(1);
    for (i = 0; i < h_loc_length; i++)
    {
        if (h_loc[i] != n_loc[i])
            return(1);
    }
    return(0);
1263 1264 1265 1266
}

void preset_hub_add_a_node(t_preset_hub *h, t_preset_node *x)
{
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
    t_preset_hub_data *hd1, *hd2;
    int found = 0;
    int i;

    hd1 = NULL;
    hd2 = NULL;

    if (PH_DEBUG) fprintf(stderr,"preset_hub_add_a_node\n");
    if (h->ph_data)
    {
        /* first check for disabled nodes and reenable them if they match
           location */
        hd1 = h->ph_data;
        while (hd1)
        {
            if (!hd1->phd_node)
            {
                // only if the node is disabled (NULL)
                if (!preset_hub_compare_loc(hd1->phd_pn_gl_loc,
                    hd1->phd_pn_gl_loc_length,
                    x->pn_gl_loc, x->pn_gl_loc_length))
                {
                    // if this hub node data's location matches that of the node
                    if(PH_DEBUG) fprintf(stderr,"    found disabled -> "
                                                "enabling\n");
                    found = 1;
                    hd1->phd_node = x;
                    break;
                }
            }
            hd1 = hd1->phd_next;
        }
    }

    if (!found)
    {
        /* we have no stored node data (or none that match node's location) so
           let's create a new one */
        if (PH_DEBUG) fprintf(stderr,"    creating a new\n");
        // create a new data struct
        hd2 = (t_preset_hub_data *)t_getbytes(sizeof(*hd2));
        // reconstruct the dynamic location array
        hd2->phd_pn_gl_loc_length = x->pn_gl_loc_length;
        hd2->phd_pn_gl_loc = (int*)calloc(hd2->phd_pn_gl_loc_length,
            sizeof(hd2->phd_pn_gl_loc));
        for (i=0; i < hd2->phd_pn_gl_loc_length; i++)
        {
            if(PH_DEBUG) fprintf(stderr,"    loc %d\n", x->pn_gl_loc[i]);
            hd2->phd_pn_gl_loc[i] = x->pn_gl_loc[i];
        }
        // assign node value
        hd2->phd_node = x;
        hd2->phd_next = NULL;

        // adjust pointers
        if (!h->ph_data)
        {
            h->ph_data = hd2;
        }
        else
        {
            hd1 = h->ph_data;
            while (hd1->phd_next)
            {
                hd1 = hd1->phd_next;    
            }
            hd1->phd_next = hd2;
        }
    }
1336 1337 1338 1339
}

void preset_hub_delete_a_node(t_preset_hub *h, t_preset_node *x)
{
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
    t_preset_hub_data *hd1;
    hd1 = NULL;

    if(PH_DEBUG) fprintf(stderr,"preset_hub_delete_a_node\n");
    if (h->ph_data)
    {
        // check for enabled nodes only
        hd1 = h->ph_data;
        while (hd1)
        {
            if (hd1->phd_node && hd1->phd_node == x)
            {
                // only if the node is enabled and matches ours
                if(PH_DEBUG) fprintf(stderr,"    found enabled -> disabling\n");
                hd1->phd_node = NULL;
                break;
            }
            hd1 = hd1->phd_next;
        }
    }
1360 1361
}

1362 1363
void preset_hub_reset(t_preset_hub *h)
{
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
    t_atom ap[1];
    t_glob_preset_node_list *nl;
    t_preset_hub_data *hd1, *hd2;
    t_node_preset *np1, *np2;
    int changed = 0;

    if(PH_DEBUG) fprintf(stderr,"preset_hub_reset\n");

    // inform all nodes that the hub is letting go of them
    if (gpnl)
    {
        if(PH_DEBUG) fprintf(stderr,"    we got gpnl\n");
        nl = gpnl;
        while(nl)
        {
            if(PH_DEBUG)
                fprintf(stderr,"    analyzing gpnl entry %d\n",
                    nl->gpnl_paired);
            if (nl->gpnl_paired && nl->gpnl_node->pn_hub == h)
            {
                nl->gpnl_paired = 0;
                nl->gpnl_node->pn_hub = NULL;
                if(PH_DEBUG) fprintf(stderr,"    removed gpnl reference\n");
            }
            nl = nl->gpnl_next;
        }
    }

    // deallocate all the dynamically-allocated memory
    if (h->ph_data)
    {
        if(PH_DEBUG) fprintf(stderr,"    got ph_data\n");
        hd1 = h->ph_data;
        while (hd1)
        {
            if (hd1->phd_npreset)
            {
                np1 = hd1->phd_npreset;
                while (np1)
                {
                    np2 = np1->np_next;
                    if (np1->np_val.l_n)
                        alist_clear(&np1->np_val);
                    freebytes(np1, sizeof(*np1));
                    changed = 1;
                    if(PH_DEBUG) fprintf(stderr,"    deleting preset\n");
                    np1 = np2;
                }
            }
            hd2 = hd1->phd_next;
            freebytes(hd1, sizeof(*hd1));
            changed = 1;
            if(PH_DEBUG) fprintf(stderr,"    deleting ph_data\n");
            hd1 = hd2;
        }
    }

    h->ph_data = NULL;

    /* and finally request pairing with nodes (since we deleted all our
       references) */
    glob_preset_node_list_seek_hub();

    if (changed && !h->ph_extern_file) canvas_dirty(h->ph_canvas, 1);

    SETFLOAT(ap+0, (t_float)changed);
    outlet_anything(h->ph_outlet, gensym("reset"), 1, ap);
1431 1432 1433 1434
}

void preset_hub_clear(t_preset_hub *h, t_float f)
{
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
    t_atom ap[1];
    t_preset_hub_data *hd2;
    t_node_preset *np1, *np2;
    int changed = 0;

    if(PH_DEBUG) fprintf(stderr,"preset_hub_clear\n");

    if (h->ph_data)
    {
        if(PH_DEBUG) fprintf(stderr,"    got ph_data\n");
        hd2 = h->ph_data;
        while (hd2)
        {
            /* all nodes will get their preset cleared regardless whether