aboutsummaryrefslogtreecommitdiff
path: root/src/onlyone.c
blob: 60287b00858de45418f87157754a414af77bca42 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
144
145
146
147
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188

#include "m_pd.h"

struct _onlyone;

typedef struct _oo {
    struct _onlyone*     x;
    //t_symbol*         name;
    struct _oo* 	    next; 
    struct _oo* 	    previous; 
} t_oo;

static t_oo* oos = NULL;

static t_class *onlyone_class;

typedef struct _onlyone {
  t_object  		x_obj;
  t_outlet*         outlet;
  t_outlet*         outlet2;
  t_symbol*         name;
  int               iamtheone;
  t_symbol*         s_empty;
  //t_sample 			f_common;
  //t_common*	pg; 
  //t_sample 			f;
} t_onlyone;


static void onlyone_bang( t_onlyone *x) {
    if (x->iamtheone) {
        outlet_bang(x->outlet);
    } else {
        outlet_bang(x->outlet2);
    }
}


static void onlyone_register(t_onlyone* x) {
  

  t_oo* oo = oos;
  
  int outputme = 1;
  
  
   t_oo* new_oo = getbytes(sizeof(*new_oo));
   new_oo->x = x;
    
    if ( oo != NULL) {
        //post("oo is not null");
        if (outputme && (oo->x->name == x->name)) outputme = 0;
        // Get the last oo
        while (oo->next) {
            oo = oo->next;
        }
    }
    
    // Append
   
    new_oo->previous = oo;
    new_oo->next = NULL;
    if (oo != NULL) {
        oo->next = new_oo;
    } else {
        //post("Adding the first");
        oos = new_oo;
    }
   
    x->iamtheone = outputme;
	
}


static void onlyone_unregister(t_onlyone* x) {
	
    t_oo* oo = oos;
    
    t_onlyone* theotherone = NULL;
    t_oo* deleteone = NULL;
    
    if ( oo != NULL) {
        // Get the mathching oo and the x to output to
        while ( oo ) {
            if ( oo->x == x ) {
                //post("Found oo to delete");
                deleteone = oo;
            } else if (x->iamtheone && theotherone==NULL && (oo->x->name == x->name) ) {
                theotherone = oo->x;
            }
            if ( deleteone && theotherone) break;
            oo = oo->next;
        }
        
       
        
        // Delete the oo matching the x that called this function
        if (deleteone) {
            
            if (deleteone->previous) {
				deleteone->previous->next = deleteone->next;
				if (deleteone->next) deleteone->next->previous = deleteone->previous;
				
			} else {
				oos = deleteone->next;
				if ( deleteone->next != NULL) deleteone->next->previous = NULL;
			}
            
            freebytes(deleteone,sizeof(*deleteone));
            x->iamtheone = 0;
            x->name = NULL;
        } else {
            post("Hum, did not find oo to delete!");
        }
        
        // Output the bang to the other one
         if (theotherone) {
             theotherone->iamtheone = 1;
             onlyone_bang(theotherone);
         }
        
    } else {
        post("Weird, the list is empty...");
    }
    
   // if (oos==NULL) post("this is the end");

}


static void onlyone_free( t_onlyone *x) {

	 if ( x->name) onlyone_unregister(x);
}

static void onlyone_set( t_onlyone *x, t_symbol *s) {
   if ( s == x->s_empty) {
       if ( x->name) {
           onlyone_unregister(x);
           //onlyone_bang(x);
       }
   } else  if ( x->name && s != x->name) {
	 onlyone_unregister(x);
     x->name = s;
     onlyone_register(x);
     //onlyone_bang(x);
    } else if ( x->name == NULL) {
     x->name = s;
     onlyone_register(x);
     //onlyone_bang(x);
    }
    onlyone_bang(x);
}




static void *onlyone_new(t_symbol* s)
{
  t_onlyone *x = (t_onlyone *)pd_new(onlyone_class);
  
  x->s_empty = gensym("");
   x->iamtheone = 0;
   x->name = NULL;
  
  if ( s != x->s_empty) x->name = s;
  
  x->outlet = outlet_new(&x->x_obj, &s_bang);
 
  if ( x->name) onlyone_register(x);
  
  
  x->outlet2 = outlet_new(&x->x_obj, &s_bang);
 
  return (void *)x;
}

void onlyone_setup(void) {
  onlyone_class = class_new(gensym("onlyone"),
        (t_newmethod)onlyone_new,
        (t_method)onlyone_free, sizeof(t_onlyone),
        0, A_DEFSYMBOL, 0);
  
  class_addbang(onlyone_class, onlyone_bang);
        
    class_addmethod(onlyone_class, (t_method)onlyone_set, gensym("set"),A_DEFSYMBOL,0);
    
}