aboutsummaryrefslogtreecommitdiff
path: root/threadlib/src/join.c
blob: 65c80921ce82b38327b014e008722880dd403ba0 (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
/* 
* 
* join
* Copyright (C) 2005 Georg Holzmann, <grh@mur.at>
* Copyright (C) 2005  Tim Blechmann
* 
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING.  If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/

#include "threadlib.h"
#include <string.h>

static t_class *join_class;

typedef struct _join
{
  t_object x_obj;
  t_outlet * x_outlet;
} join_t;

static join_t * join_new(void)
{
  join_t *x = (join_t*) pd_new(join_class);
  x->x_outlet = outlet_new(&x->x_obj, NULL);
  return x;
}

static t_int join_bang_callback(t_int * argv)
{
  outlet_bang((t_outlet*)argv[0]);
  return 0;
}

static void join_bang(join_t * x)
{
  t_int* argv = getbytes(sizeof(t_int*));
  argv[0] = (t_int)x->x_outlet;
  
  sys_callback(join_bang_callback, argv, 1);
}

static t_int join_pointer_callback(t_int * argv)
{
  outlet_pointer((t_outlet*)argv[0], (t_gpointer*)argv[1]);
  return 0;
}

static void join_pointer(join_t * x, t_gpointer * gp)
{
  t_int* argv = getbytes(2*sizeof(t_int*));
  argv[0] = (t_int)x->x_outlet;
  argv[1] = (t_int)gp;

  sys_callback(join_pointer_callback, argv, 2);
}

static t_int join_float_callback(t_int * argv)
{
  outlet_float((t_outlet*)argv[0], (t_float)argv[1]);
  return 0;
}

static void join_float(join_t * x, t_float f)
{
  t_int* argv = getbytes(2*sizeof(t_int*));
  argv[0] = (t_int)x->x_outlet;
  argv[1] = (t_int)f;

  sys_callback(join_float_callback, argv, 2);
}

static t_int join_symbol_callback(t_int * argv)
{
  outlet_symbol((t_outlet*)argv[0], (t_symbol*)argv[1]);
  return 0;
}

static void join_symbol(join_t * x, t_symbol * s)
{
  t_int* argv = getbytes(2*sizeof(t_int*));
  argv[0] = (t_int)x->x_outlet;
  argv[1] = (t_int)s;

  sys_callback(join_symbol_callback, argv, 2);
}

static t_int join_list_callback(t_int * argv)
{
  outlet_list((t_outlet*)argv[0], 0, (int)argv[1], (t_atom*)argv[2]);
  freebytes ((t_atom*)argv[2], (int)argv[1] * sizeof(t_atom));
  return 0;
}

static void join_list(join_t * x, t_symbol * s, int argc, t_atom* largv)
{
  t_int* argv = getbytes(3*sizeof(t_int*));
  t_atom* copied_argv = copybytes(largv, argc * sizeof(t_atom));

  argv[0] = (t_int)x->x_outlet;
  argv[1] = (t_int)argc;
  argv[2] = (t_int)copied_argv;

  sys_callback(join_list_callback, argv, 3);
}

static t_int join_anything_callback(t_int * argv)
{
  outlet_anything((t_outlet*)argv[0], &s_list,
		(int)argv[1], (t_atom*)argv[2]);
  freebytes ((t_atom*)argv[2], (int)argv[1] * sizeof(t_atom));
  return 0;
}

static void join_anything(join_t * x, t_symbol * s, int argc, t_atom* largv)
{
  t_int* argv = getbytes(3*sizeof(t_int*));
  
  // also copy selector symbol
  int copied_argc = argc+1;
  t_atom *copied_argv;
  copied_argv = (t_atom*)getbytes(copied_argc * sizeof(t_atom));
  
  if(copied_argc)
  {
    memcpy(copied_argv, s, sizeof(t_atom));
    SETSYMBOL(copied_argv, s);
    memcpy(copied_argv+1, largv, argc * sizeof(t_atom));
  }
  
  argv[0] = (t_int)x->x_outlet;
  argv[1] = (t_int)copied_argc;
  argv[2] = (t_int)copied_argv;

  sys_callback(join_anything_callback, argv, 3);
}

void join_setup(void)
{
  join_class = class_new(gensym("join"), (t_newmethod)join_new,
			 0, sizeof(join_t), CLASS_DEFAULT, 0);

  class_addbang(join_class, join_bang);
  class_addfloat(join_class, join_float);
  class_addpointer(join_class, join_pointer);
  class_addsymbol(join_class, join_symbol);
  class_addlist(join_class, join_list);
  class_addanything(join_class, join_anything);
}