Max 5 API Reference
00001 /* 00002 myregob 00003 Copyright 2004 - Cycling '74 00004 jeremy bernstein - jeremy@bootsquad.com 00005 */ 00006 00007 #include "ext.h" 00008 #include "ext_common.h" 00009 #include "ext_obex.h" 00010 00011 typedef struct _myregob 00012 { 00013 t_object ob; 00014 void *obex; 00015 void **proxy; // proxies 00016 void **out; // out 00017 void **mmo; // minimetro objects 00018 long count; 00019 long inlet_no; 00020 } t_myregob; 00021 00022 void *myregob_new(t_symbol *s, long argc, t_atom *argv); 00023 void myregob_free(t_myregob *x); 00024 void myregob_assist(t_myregob *x, void *b, long m, long a, char *s); 00025 void myregob_notify(t_myregob *x, t_symbol *s, t_symbol *msg, void *sender, void *data); 00026 00027 void myregob_int(t_myregob *x, long c); 00028 void myregob_speed(t_myregob *x, double speed); 00029 // pattr support function prototypes 00030 t_max_err myregob_getvalueof(t_myregob *x, long *ac, t_atom **av); 00031 t_max_err myregob_setvalueof(t_myregob *x, long ac, t_atom *av); 00032 00033 void minimetro_init(void); 00034 00035 void *myregob_class; 00036 00037 void main(void) 00038 { 00039 t_class *c; 00040 void *attr; 00041 long attrflags = 0; 00042 00043 c = class_new("myregob", (method)myregob_new, (method)myregob_free, (short)sizeof(t_myregob), 00044 0L, A_GIMME, 0); 00045 00046 // initialize the common symbols, since we want to use them 00047 common_symbols_init(); 00048 00049 // initialize the minimetro class 00050 minimetro_init(); 00051 00052 // register the byte offset of obex with the class 00053 class_obexoffset_set(c, calcoffset(t_myregob, obex)); 00054 00055 // add some attributes 00056 00057 // add methods to the class 00058 class_addmethod(c, (method)myregob_int, "int", A_LONG,0); 00059 class_addmethod(c, (method)myregob_speed, "speed", A_FLOAT,0); 00060 class_addmethod(c, (method)myregob_assist, "assist", A_CANT,0); 00061 00062 // these methods support the pattr set of objects 00063 // class_addmethod(c, (method)myregob_getvalueof, "getvalueof", A_CANT,0); 00064 // class_addmethod(c, (method)myregob_setvalueof, "setvalueof", A_CANT,0); 00065 00066 // add a notify method, so we get notifications from child objects 00067 class_addmethod(c, (method)myregob_notify, "notify", A_CANT, 0); 00068 // add methods for dumpout and quickref 00069 class_addmethod(c, (method)object_obex_dumpout, "dumpout", A_CANT, 0); 00070 class_addmethod(c, (method)object_obex_quickref, "quickref", A_CANT, 0); 00071 00072 // we want this class to instantiate inside of the Max UI; ergo CLASS_BOX 00073 class_register(CLASS_BOX, c); 00074 myregob_class = c; 00075 } 00076 00077 t_max_err myregob_getvalueof(t_myregob *x, long *ac, t_atom **av) 00078 { 00079 // not implemented 00080 return MAX_ERR_NONE; 00081 } 00082 00083 t_max_err myregob_setvalueof(t_myregob *x, long ac, t_atom *av) 00084 { 00085 // not implemented 00086 return MAX_ERR_NONE; 00087 } 00088 00089 void myregob_assist(t_myregob *x, void *b, long m, long a, char *s) 00090 { 00091 if (m == 1) { //input 00092 sprintf(s, "0 / 1, speed"); 00093 } 00094 else { //output 00095 if (a == x->count) 00096 sprintf(s, "dumpout"); 00097 else 00098 sprintf(s, "bang out"); 00099 } 00100 } 00101 00102 void myregob_int(t_myregob *x, long c) 00103 { 00104 long inlet; 00105 00106 inlet = proxy_getinlet((t_object *)x); 00107 00108 if (inlet >= x->count) 00109 return; 00110 00111 // send a method to the appropriate minimetro to turn it on or off 00112 object_method(x->mmo[inlet], gensym("onoff"), c); 00113 } 00114 00115 void myregob_speed(t_myregob *x, double speed) 00116 { 00117 t_symbol *newname; 00118 long inlet; 00119 t_object *mmo; 00120 00121 inlet = proxy_getinlet((t_object *)x); 00122 00123 if (inlet >= x->count) 00124 return; 00125 00126 if (speed < 0.) 00127 speed = 0.; 00128 00129 mmo = x->mmo[inlet]; 00130 // set the speed attribute of the internal minimetro object 00131 object_attr_setfloat(mmo, gensym("speed"), speed); 00132 } 00133 00134 void myregob_notify(t_myregob *x, t_symbol *s, t_symbol *msg, void *sender, void *data) 00135 { 00136 if (msg == _sym_bang) { 00137 // bang the outlet sent as data 00138 outlet_bang(x->out[(long)data]); 00139 } 00140 else if (msg == _sym_free) { // this message is sent when the object is freeing 00141 object_detach(gensym("_regob_test"), s, x); 00142 //object_unregister(sender); // DO NOT UNREGISTER IN RESPONSE TO FREE NOTIFICATION -jkc 00143 } 00144 } 00145 00146 void myregob_free(t_myregob *x) 00147 { 00148 long i; 00149 00150 if (x->mmo) { 00151 for (i = 0; i < x->count; i++) { 00152 object_free(x->mmo[i]); 00153 } 00154 freebytes(x->mmo, sizeof(void *) * x->count); 00155 } 00156 if (x->proxy) { 00157 for (i = 1; i < x->count; i++) { 00158 freeobject(x->proxy[i]); 00159 } 00160 freebytes(x->proxy, sizeof(void *) * x->count); 00161 } 00162 if (x->out) 00163 freebytes(x->out, sizeof(void *) * x->count); 00164 } 00165 00166 void *myregob_new(t_symbol *s, long ac, t_atom *av) 00167 { 00168 t_myregob *x; 00169 t_symbol *name; 00170 long i; 00171 00172 // we use object_alloc here, rather than newobject 00173 if (x = (t_myregob *)object_alloc(myregob_class)) { //only max object, no jit object 00174 // add a dumpout outlet 00175 object_obex_store(x, _sym_dumpout, outlet_new(x, NULL)); 00176 00177 if (ac && av) { 00178 // allocate memory for the proxy pointers 00179 x->proxy = (void **)getbytes(sizeof(void *) * ac); 00180 x->proxy[0] = 0; // unused 00181 // allocate memory for the outlet pointers 00182 x->out = (void **)getbytes(sizeof(void *) * ac); 00183 // allocate memory for the minimetro pointers 00184 x->mmo = (void **)getbytes(sizeof(void *) * ac); 00185 x->count = ac; 00186 00187 for (i = ac - 1; i >= 0; i--) { 00188 if (i) // don't make a 0th proxy 00189 x->proxy[i] = proxy_new(x, i, &x->inlet_no); 00190 // create some outlets 00191 x->out[i] = outlet_new(x, 0L); 00192 00193 // create some minimetro objects 00194 x->mmo[i] = object_new(CLASS_NOBOX, gensym("minimetro"), i, atom_getlong(av + i)); 00195 object_register(gensym("_regob_test"), name = symbol_unique(), x->mmo[i]); 00196 object_attach(gensym("_regob_test"), name, x); 00197 } 00198 } else { 00199 x->proxy = NULL; 00200 x->out = NULL; 00201 x->mmo = NULL; 00202 x->count = 0; 00203 } 00204 attr_args_process(x, ac, av); 00205 } 00206 return (x); 00207 }
Copyright © 2008, Cycling '74