aboutsummaryrefslogtreecommitdiff
path: root/src/savebangs.c
blob: d4ace75ac55e62c80723f539a12e85de0b09f8b5 (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
189
190
191
192
193
194
195

/******************************************************
 *
 * propertybang - implementation file
 *
 * copyleft (c) IOhannes m zm-bölnig-A
 *
 *   2007:forum::f-bür::umläute:2007-A
 *
 *   institute of electronic music and acoustics (iem)
 *
 ******************************************************
 *
 * license: GNU General Public License v.2
 *
 ******************************************************/


/* 
 * this object outputs a bang when the savfn of the parent abstraction is called
 */

/*
 * LATER sketch: 
 *     monkey-patching the parent canvas so that stores a local function table
 *     then modify this function-table so that the savefn points to us
 *   on call: we call the parents savefunction and output bangs
 */

/* 
 * TODO: how does this behave in sub-patches?
 *      -> BUG: the depth should _really_ refer to the abstraction-depth 
 *              else we get weird duplicates (most likely due to the "$0" trick
 */

#include "m_pd.h"
#include "g_canvas.h"

/* ------------------------- help methods ---------------------------- */


typedef struct _savefuns {
  t_class*class;
  t_savefn savefn;

  struct _savefuns *next;
} t_savefuns;

static t_savefuns*s_savefuns=0;


static t_savefn find_savefn(const t_class*class) 
{
  t_savefuns*fun=s_savefuns;
  if(0==s_savefuns || 0==class)
    return 0;
  for(fun=s_savefuns; fun; fun=fun->next) {
    if(class == fun->class) {
      return fun->savefn;
    }
  }

  return 0;
}
static void add_savefn(t_class*class)
{
  if(0!=find_savefn(class)) {
    return;
  } else {
    t_savefuns*sfun=(t_savefuns*)getbytes(sizeof(t_savefuns));
    sfun->class=class;
    sfun->savefn=class_getsavefn(class);
    sfun->next=0;

    if(0==s_savefuns) {
      s_savefuns=sfun;
    } else {
      t_savefuns*sfp=s_savefuns;
      while(sfp->next)
        sfp=sfp->next;
      sfp->next = sfun;      
    }
  }
}



/* ------------------------- savebangs ---------------------------- */

static t_class *savebangs_class;

typedef struct _savebangs
{
  t_object  x_obj;

  t_symbol *x_d0;
  t_outlet *x_pre, *x_post;

  t_savefn x_parentsavefn;
} t_savebangs;


static void savebangs_free(t_savebangs *x)
{
  /* unpatch the parent canvas */
  if(x->x_d0) {
    pd_unbind(&x->x_obj.ob_pd, x->x_d0);
  }
}

static void orig_savefn(t_gobj*z, t_binbuf*b)
{
   t_class*class=z->g_pd;
    t_savefn savefn=find_savefn(class);
    if(savefn) {
      savefn(z, b);
    }
}

static void savebangs_savefn(t_gobj*z, t_binbuf*b) {
  /* argh: z is the abstraction! but we need to access ourselfs!
   * we handle this by binding to a special symbol. e.g. "$0 savebangs"
   * (we use the space between in order to make it hard for the ordinary user 
   * to use this symbol for other things...
   */

  /* alternatively we could just search the abstraction for all instances of savebangs_class
   * and bang these;
   * but using the pd_bind-trick is simpler for now
   * though not as sweet, as somebody could use our bind-symbol for other things...
   */

  t_symbol*s_d0=canvas_realizedollar((t_canvas*)z, gensym("$0 savebangs"));
  t_atom ap[2];
  SETPOINTER(ap+0, (t_gpointer*)z);
  SETPOINTER(ap+1, (t_gpointer*)b);

  if(s_d0->s_thing) {
    pd_list(s_d0->s_thing, &s_list, 2, ap);
  } else {
    orig_savefn(z, b);
  }
}

static void savebangs_list(t_savebangs *x, t_symbol*s, int argc, t_atom*argv)
{
  if(argv[0].a_type == A_POINTER && argv[1].a_type == A_POINTER) {    
    t_gobj *z    =(t_gobj*)  argv[0].a_w.w_gpointer;
    t_binbuf*b   =(t_binbuf*)argv[1].a_w.w_gpointer;

    outlet_bang(x->x_pre);
    orig_savefn(z, b);
    outlet_bang(x->x_post);
  }
}

static void *savebangs_new(t_floatarg f)
{
  t_savebangs *x = (t_savebangs *)pd_new(savebangs_class);
  t_glist *glist=(t_glist *)canvas_getcurrent();
  t_canvas *canvas=(t_canvas*)glist_getcanvas(glist);
  t_class *class = 0;

  
  int depth=(int)f;
  if(depth<0)depth=0;

  while(depth && canvas) {
    canvas=canvas->gl_owner;
    depth--;
  }

  if(canvas) {
    class=((t_gobj*)canvas)->g_pd;
    x->x_d0=canvas_realizedollar(canvas, gensym("$0 savebangs"));
    pd_bind(&x->x_obj.ob_pd, x->x_d0);

    add_savefn(class);
    class_setsavefn(class, savebangs_savefn);
  } else {
    x->x_d0=0;
  }

  x->x_pre=outlet_new(&x->x_obj, &s_bang);
  x->x_post=outlet_new(&x->x_obj, &s_bang);

  return (x);
}

void savebangs_setup(void)
{
  savebangs_class = class_new(gensym("savebangs"), (t_newmethod)savebangs_new,
                              (t_method)savebangs_free, sizeof(t_savebangs), CLASS_NOINLET, A_DEFFLOAT, 0);
  class_addlist(savebangs_class, savebangs_list);
}