File: control.c

package info (click to toggle)
csound 1%3A6.18.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 63,220 kB
  • sloc: ansic: 192,643; cpp: 14,149; javascript: 9,654; objc: 9,181; python: 3,376; java: 3,337; sh: 1,840; yacc: 1,255; xml: 985; perl: 635; lisp: 411; tcl: 341; lex: 217; makefile: 128
file content (344 lines) | stat: -rw-r--r-- 11,200 bytes parent folder | download | duplicates (4)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
    control.c:

    Copyright (C) 2000 John ffitch

    This file is part of Csound.

    The Csound Library is free software; you can redistribute it
    and/or modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    Csound 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with Csound; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    02110-1301 USA
*/

#include "csdl.h"
#include "control.h"

#include <sys/time.h>
#include <sys/types.h>
#include <signal.h>

#if defined(__MACH__)
#include <unistd.h>
#endif

static CS_NOINLINE CONTROL_GLOBALS *get_globals_(CSOUND *csound)
{
    CONTROL_GLOBALS *p;
    p = (CONTROL_GLOBALS*) csound->QueryGlobalVariable(csound,
                                                       "controlGlobals_");
    if (p != NULL)
      return p;
    if (csound->CreateGlobalVariable(csound, "controlGlobals_",
                                     sizeof(CONTROL_GLOBALS)) != 0){
      csound->Warning(csound, "%s", Str("control: failed to allocate globals"));
      return NULL;
    }
    p = (CONTROL_GLOBALS*) csound->QueryGlobalVariable(csound,
                                                       "controlGlobals_");
    p->csound = csound;
    return p;
}

static inline CONTROL_GLOBALS *get_globals(CSOUND *csound, CONTROL_GLOBALS **p)
{
    if (*p == NULL)
      *p = get_globals_(csound);
    return (*p);
}

static int32_t kill_wish(CSOUND *csound, CONTROL_GLOBALS *p)
{
    csound->Message(csound, Str("Closing down wish(%d)\n"), p->wish_pid);
    kill(p->wish_pid, 9);
    if (p->values != NULL)  csound->Free(csound,p->values);
    if (p->minvals != NULL) csound->Free(csound,p->minvals);
    if (p->maxvals != NULL) csound->Free(csound,p->maxvals);
    if (p->buttons != NULL) csound->Free(csound,p->buttons);
    if (p->checks != NULL)  csound->Free(csound,p->checks);
    fclose(p->wish_cmd);
    fclose(p->wish_res);
    return OK;
}

static void start_tcl_tk(CONTROL_GLOBALS *p)
{
    int32_t i;

    p->csound->Message(p->csound, "TCL/Tk\n");
    if (UNLIKELY(pipe(p->pip1) || pipe(p->pip2))) {
      printf("Failed to create pipes");
      return;
    }
    if ((p->wish_pid = fork()) < 0)
      return;
    if (p->wish_pid == 0) {     /* Child process */
      char *argv[7];
      argv[0] = "sh";
      argv[1] = "-c";
      argv[2] = "wish";
      argv[3] = "-name";
      argv[4] = "sliders";
      argv[5] = NULL;
      close(p->pip1[0]); close(p->pip2[1]);
      close(0); close(1);
      dup2(p->pip2[0], 0);
      dup2(p->pip1[1], 1);
      setvbuf(stdout, (char*) NULL, _IOLBF, 0);
      signal(SIGINT, SIG_IGN);  /* child process should ignore ^C */
      execvp("/bin/sh", argv);
      exit(127);
    }
                                /* Main process -- create communications */
    close(p->pip1[1]); close(p->pip2[0]);
    p->wish_res = fdopen(p->pip1[0], "r");
    p->wish_cmd = fdopen(p->pip2[1], "w");
    setvbuf(p->wish_cmd, (char*) NULL, _IOLBF, 0);
    setvbuf(p->wish_res, (char*) NULL, _IOLBF, 0);
    p->csound->RegisterResetCallback(p->csound, (void*) p,
                                     (int32_t (*)(CSOUND *, void *)) kill_wish);
    fprintf(p->wish_cmd, "source nsliders.tk\n");
    if (UNLIKELY(NULL==fgets(p->cmd, 100, p->wish_res))) {
      printf("Failed to read from child");
      return;
    };
    p->csound->Message(p->csound, "Wish %s\n", p->cmd);
    p->values = (int32_t*) p->csound->Calloc(p->csound,8*sizeof(int32_t));
    p->minvals = (int32_t*) p->csound->Calloc(p->csound,8* sizeof(int32_t));
    p->maxvals = (int32_t*) p->csound->Calloc(p->csound,8* sizeof(int32_t));
    p->buttons = (int32_t*) p->csound->Calloc(p->csound,8* sizeof(int32_t));
    p->checks  = (int32_t*) p->csound->Calloc(p->csound,8* sizeof(int32_t));
    p->max_sliders = 8;
    p->max_button = 8;
    p->max_check = 8;
    for (i = 0; i < p->max_sliders; i++) {
      p->minvals[i] = 0; p->maxvals[i] = 127;
    }
    p->csound->Sleep(1500);
}

static void ensure_slider(CONTROL_GLOBALS *p, int32_t n)
{
/*  p->csound->Message(p->csound, "Ensure_slider %d\n", n); */
    if (p->wish_pid == 0)
      start_tcl_tk(p);
    if (n > p->max_sliders) {
      int32_t i, nn = n + 1;
      p->values  = (int32_t*) p->csound->ReAlloc(p->csound,
                                                 p->values, nn * sizeof(int32_t));
      p->minvals = (int32_t*) p->csound->ReAlloc(p->csound,
                                                 p->minvals,nn * sizeof(int32_t));
      p->maxvals = (int32_t*) p->csound->ReAlloc(p->csound,
                                                 p->maxvals,nn * sizeof(int32_t));
      for (i = p->max_sliders + 1; i < nn; i++) {
        p->values[i] = 0; p->minvals[i] = 0; p->maxvals[i] = 127;
      }
      p->max_sliders = n;
    }
/*  p->csound->Message(p->csound, "displayslider %d\n", n); */
    fprintf(p->wish_cmd, "displayslider %d\n", n);
}

static void readvalues(CONTROL_GLOBALS *p)
{
    fd_set rfds;
    struct timeval tv;

    /* Watch wish_res to see when it has input. */
    FD_ZERO(&rfds);
    FD_SET(p->pip1[0], &rfds);
    tv.tv_sec = 0;
    tv.tv_usec = 0;
                                /* Read all changes */
    while (select(p->pip1[0] + 1, &rfds, NULL, NULL, &tv)) {
      int32_t n, val;
      if (UNLIKELY(2!=fscanf(p->wish_res, "%d %d", &n, &val))) {
        printf("Failed to read from child");
        return;
      }
      if (n>p->max_sliders);    /* ignore error case */
      else if (n > 0) p->values[n] = val;
      else if (n == 0) p->buttons[val] = 1;
      else p->checks[-n] = val;
      tv.tv_sec = 0;
      tv.tv_usec = 0;
    }
}

static int32_t cntrl_set(CSOUND *csound, CNTRL *p)
{
    ensure_slider(get_globals(csound, &(p->p)), (int32_t) MYFLT2LONG(*p->kcntl));
    return OK;
}

static int32_t control(CSOUND *csound, CNTRL *p)
{
    CONTROL_GLOBALS *pp = get_globals(csound, &(p->p));
    readvalues(pp);
    *p->kdest = pp->values[(int32_t)MYFLT2LONG(*p->kcntl)];
    return OK;
}

static int32_t ocontrol_(CSOUND *csound, SCNTRL *p, int32_t istring)
{
    CONTROL_GLOBALS *pp = get_globals(csound, &(p->p));
    int32_t c = (int32_t) *p->which;
    int32_t slider = (int32_t) MYFLT2LONG(*p->kcntl);

/*  csound->Message(csound, "ocontrol: %d %d %f\n", slider, c, *p->val); */
    ensure_slider(pp, slider);
    switch (c) {
    case 1:
      fprintf(pp->wish_cmd, "setvalue %d %d\n", slider, (int32_t) *p->val);
      pp->values[slider] = (int32_t) *p->val;
      break;
    case 2:
      if (pp->minvals[slider] != (int32_t) *p->val) {
        fprintf(pp->wish_cmd, "setmin %d %d\n", slider, (int32_t) *p->val);
        pp->minvals[slider] = (int32_t) *p->val;
      }
      break;
    case 3:
      if (pp->maxvals[slider] != (int32_t) *p->val) {
        fprintf(pp->wish_cmd, "setmax %d %d\n", slider, (int32_t) *p->val);
        pp->maxvals[slider] = (int32_t) *p->val;
      }
      break;
    case 4:
      {
        char buffer[100];
        if (istring) {
          csound->strarg2name(csound, buffer,
                              ((STRINGDAT *)p->val)->data, "Control ",istring);
        }
        else
         csound->strarg2name(csound, buffer, p->val, "Control ",istring);
        csound->Message(csound, Str("Slider %d set to %s\n"), slider, buffer);
        fprintf(pp->wish_cmd, "setlab %d \"%s\"\n", slider, buffer);
        break;
      }
    default:
      return csound->InitError(csound, Str("Unknown control %d"), c);
    }
    return OK;
}

static int32_t ocontrol(CSOUND *csound, SCNTRL *p){
  return ocontrol_(csound,p,0);
}

static int32_t ocontrol_S(CSOUND *csound, SCNTRL *p){
  return ocontrol_(csound,p,1);
}

static int32_t button_set(CSOUND *csound, CNTRL *p)
{
    CONTROL_GLOBALS *pp = get_globals(csound, &(p->p));
    int32_t n = (int32_t) MYFLT2LONG(*p->kcntl);

    if (pp->wish_pid == 0)
      start_tcl_tk(pp);
    if (n > pp->max_button) {
      pp->buttons = (int32_t*) csound->ReAlloc(csound, pp->buttons,
                                           (n + 1) * sizeof(int32_t));
      do {
        pp->buttons[++(pp->max_button)] = 0;
      } while (pp->max_button < n);
    }
    fprintf(pp->wish_cmd, "displaybutton %d\n", n);
    return OK;
}

static int32_t button(CSOUND *csound, CNTRL *p)
{
    CONTROL_GLOBALS *pp = get_globals(csound, &(p->p));
    int32_t t = (int32_t)MYFLT2LONG(*p->kcntl);
    readvalues(pp);
    *p->kdest = pp->buttons[t];
    pp->buttons[t] = 0;
    return OK;
}

static int32_t check_set(CSOUND *csound, CNTRL *p)
{
    CONTROL_GLOBALS *pp = get_globals(csound, &(p->p));
    int32_t n = (int32_t) MYFLT2LONG(*p->kcntl);

    if (pp->wish_pid == 0)
      start_tcl_tk(pp);
    if (n > pp->max_check) {
      pp->checks = (int32_t*) csound->ReAlloc(csound,pp->checks,
                                              (n + 1) * sizeof(int32_t));
      do {
        pp->checks[++(pp->max_check)] = 0;
      } while (pp->max_check < n);
    }
    fprintf(pp->wish_cmd, "displaycheck %d\n", n);
    return OK;
}

static int32_t check(CSOUND *csound, CNTRL *p)
{
    CONTROL_GLOBALS *pp = get_globals(csound, &(p->p));
    readvalues(pp);
    *p->kdest = pp->checks[(int32_t) MYFLT2LONG(*p->kcntl)];
    return OK;
}

/* **** Text Windows **** */

static int32_t textflash_(CSOUND *csound, TXTWIN *p, int32_t istring)
{
    CONTROL_GLOBALS *pp = get_globals(csound, &(p->p));
    int32_t   wind = (int32_t) MYFLT2LONG(*p->kcntl);
    char  buffer[100];

    if (pp->wish_pid == 0)
      start_tcl_tk(pp);
    if (istring) {
      csound->strarg2name(csound, buffer, ((STRINGDAT *)p->val)->data, "", istring);
      fprintf(pp->wish_cmd, "settext %d \"%s\"\n", wind, buffer);
    }
    else if (csound->ISSTRCOD(*p->val)) {
      csound->strarg2name(csound, buffer,
                          csound->GetString(csound, *p->val), "", 1);
    }
    else {
      fprintf(pp->wish_cmd, "deltext %d\n", wind);
    }
    return OK;
}

static int32_t textflash(CSOUND *csound, TXTWIN *p){
    return textflash_(csound, p, 0);
}

static int32_t
textflash_S(CSOUND *csound, TXTWIN *p){
    return textflash_(csound, p, 1);
}


#define S(x)    sizeof(x)

static OENTRY control_localops[] = {
  { "control",  S(CNTRL), 0, 3, "k", "k", (SUBR) cntrl_set, (SUBR) control, NULL },
{ "setctrl",  S(SCNTRL), 0, 1, "",  "iii", (SUBR) ocontrol, NULL, NULL           },
{ "setctrl.S",  S(SCNTRL), 0, 1, "",  "iSi", (SUBR) ocontrol_S, NULL, NULL       },
{ "button",   S(CNTRL),  0, 3, "k", "k",  (SUBR) button_set, (SUBR) button, NULL },
{ "checkbox", S(CNTRL),  0, 3, "k", "k",   (SUBR) check_set, (SUBR) check, NULL  },
{ "flashtxt", S(TXTWIN), 0, 1, "",  "ii",  (SUBR) textflash, NULL, NULL          },
{ "flashtxt.S", S(TXTWIN), 0, 1, "",  "iS",  (SUBR) textflash_S, NULL, NULL      },
};

LINKAGE_BUILTIN(control_localops)