aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/dyn/dyn_listen.cpp
blob: beb5d7fc21bbac5ff8bf86d307d73db3cd2666b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* 
dyn - dynamical object management

Copyright (c)2003-2004 Thomas Grill (gr@grrrr.org)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  
*/

#include "dyn_proto.h"

DYN_EXPORT int dyn_Listen(int sched,dyn_id *id,dyn_id oid,int outlet,dyn_listener cb,void *data)
{
    ASSERT(id);

    if(oid == DYN_ID_NONE) return DYN_ERROR_NOTFOUND;

    dyn_patchable *obj = oid->Patchable();
    if(obj) {
        proxyout *px = obj->GetProxyOut(outlet);
        if(!px) {
            dyn_patcher *patcher = obj->owner;
            ASSERT(patcher);

            // put proxy on the same canvas as this object for it can be connected to it!
            px = (proxyout *)NewPDObject(DYN_TYPE_OBJECT,patcher->glist(),sym_dynpxout);
            if(px) {
                px->init(obj,outlet);

//                sys_lock();

//                canvas_setcurrent(patcher->glist());
                //  connect to associated object
                if(obj_connect((t_object *)obj->pdobj,outlet,&px->pdobj,0)) {
                    obj->AddProxyOut(outlet,px);
                }
                else {
                    // delete object;
                    glist_delete(patcher->glist(),(t_gobj *)px);
                    px = NULL;

                    // could not connect
                    post("Couldn't connect proxy object");
                }
//                canvas_unsetcurrent(patcher->glist());

//                sys_unlock();
            }
        }

        if(px) {
            dyn_ident *nid = new dyn_ident(DYN_TYPE_LISTENER,oid->callback,data);
            dyn_listen *l = new dyn_listen(nid,px,cb,data);
            proxyout::Add(px,l);
            nid->Set(l);
            *id = nid;
            return DYN_ERROR_NONE;
        }
        else
            return DYN_ERROR_GENERAL;
    }
    else
        return DYN_ERROR_TYPEMISMATCH;
}

dyn_listen::~dyn_listen()
{
    proxyout::Rmv(px,this);
}