aboutsummaryrefslogtreecommitdiff
path: root/externals/sprinkler/sprinkler.c
blob: 123b1ed2794901c2013e0ded406d8914f0b62496 (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
/* -*- Mode: C -*- */
/*=============================================================================*\
 * File: sprinkler.c
 * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
 * Description: message-forwarding : workaround for missing dynamic 'send'
 *
 *   + code adapted from 'send_class' in $PD_ROOT/src/x_connective.c
 *   + formerly 'forward.c'

 *
 * Copyright (c) 2002 Bryan Jurish.
 *
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.
 *
 * 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.
 *
 * See file LICENSE for further informations on licensing terms.
 *
 * 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; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *=============================================================================*/

#include <m_pd.h>
//#include "sprinkler.h"

/* black magic */
#ifdef NT
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif

/*--------------------------------------------------------------------
 * DEBUG
 *--------------------------------------------------------------------*/
//#define SPRINKLER_DEBUG 1


/*=====================================================================
 * Constants
 *=====================================================================*/
#ifdef SPRINKLER_DEBUG
// error-message buffer
#define EBUFSIZE 256
static char sprinkler_errbuf[EBUFSIZE];
#endif

/*=====================================================================
 * Structures and Types
 *=====================================================================*/

static char *sprinkler_version = "\nsprinkler version 0.03 by Bryan Jurish : dynamic message dissemination";

static t_class *sprinkler_class;

typedef struct _sprinkler
{
    t_object x_obj;
} t_sprinkler;


/*--------------------------------------------------------------------
 * the guts:
 *  + send (the tail of) a list or message to the control-bus
 *    named by its initial element
 *  + HACK for single-element arglists *ONLY*:
 *    - sprinkler float- and pointer-initial arglists with 'pd_sprinklermess',
 *      everything else with 'pd_typedmess' as a *LIST*
 *--------------------------------------------------------------------*/
static void sprinkler_anything(t_sprinkler *x, t_symbol *dst, int argc, t_atom *argv)
{

#ifdef SPRINKLER_DEBUG
  atom_string(argv, sprinkler_errbuf, EBUFSIZE);
  post("sprinkler_debug : sprinkler_anything : dst=%s, argc=%d, arg1=%s", dst->s_name, argc, argc ? sprinkler_errbuf : "NULL");
#endif

  /*-----------------------------------------------------------------------
   * HACK:
   * + single-element arglists *ONLY*
   * + sprinkler float- and pointer-initial arglists with 'pd_sprinklermess',
   *   everything else with 'pd_typedmess' as a *LIST*
   *------------------------------------------------------------------------
   */
  if (dst->s_thing) {
    if (argc == 1) {
      switch (argv->a_type) {
      case A_FLOAT:
	pd_typedmess(dst->s_thing,&s_float,argc,argv);
	return;
      case A_SYMBOL:
	pd_typedmess(dst->s_thing,&s_symbol,argc,argv);
	return;
      case A_POINTER:
	pd_typedmess(dst->s_thing,&s_pointer,argc,argv);
	return;

      // everything else (stop 'gcc -Wall' from complaining)
      case A_NULL:
      case A_SEMI:
      case A_COMMA:
      case A_DEFFLOAT:
      case A_DOLLAR:
      case A_DOLLSYM:
      case A_GIMME:
      case A_CANT:
      default:
	// just fall though
      }
    }
    // default -- sprinkler anything else with 'pd_forwardmess'
    pd_forwardmess(dst->s_thing,argc,argv);
  }
}

static void sprinkler_list(t_sprinkler *x, t_symbol *s, int argc, t_atom *argv)
{
#ifdef SPRINKLER_DEBUG
  post("sprinkler_debug : sprinkler_list : argc=%d", argc);
#endif
  sprinkler_anything(x,atom_getsymbol(argv),--argc,++argv);
}

void *sprinkler_new(t_symbol *s)
{
    t_sprinkler *x = (t_sprinkler *)pd_new(sprinkler_class);
    return (x);
}

/*--------------------------------------------------------------------
 * setup
 *--------------------------------------------------------------------*/
void sprinkler_setup(void)
{
  post(sprinkler_version);
  sprinkler_class = class_new(gensym("sprinkler"), (t_newmethod)sprinkler_new, 0, sizeof(t_sprinkler), 0, 0);

#ifdef NON_MAX_FORWARD
  // add aliases [forward] and [fw]
  post("sprinkler : non-MAX [forward] alias enabled");
  class_addcreator((t_newmethod)sprinkler_new, gensym("forward"), A_DEFSYM, 0);
  class_addcreator((t_newmethod)sprinkler_new, gensym("fw"), A_DEFSYM, 0);
#endif

#ifdef SPRINKLER_DEBUG
  post("sprinkler : debugging enabled");
#endif
  
  // methods
  class_addlist(sprinkler_class, sprinkler_list);    
  class_addanything(sprinkler_class, sprinkler_anything);
  
  // help symbol
  class_sethelpsymbol(sprinkler_class, gensym("sprinkler-help.pd"));
}