-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvkb.c
374 lines (328 loc) · 9.68 KB
/
vkb.c
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* main routine
*
* Virtual Tiny Keyboard
*
* Copyright (c) 1997-2000 by Takashi Iwai
*
* 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; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "vkb.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <tk.h>
#ifndef Tk_Main
#define Tk_Main(argc, argv, proc) \
Tk_MainEx(argc, argv, proc, Tcl_CreateInterp())
#endif
/*
*/
#define DEF_INST_CHAN_NO "0"
#define DEF_DRUM_CHAN_NO "9"
#define DEF_OCTAVES "3"
/*
* prototypes
*/
static int usage(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[]);
static int seq_on(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[]);
static int seq_off(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[]);
static int seq_start_note(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[]);
static int seq_stop_note(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[]);
static int seq_control(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[]);
static int seq_program(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[]);
static int seq_bender(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[]);
static int seq_chorus_mode(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[]);
static int seq_reverb_mode(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[]);
static int vkb_app_init(Tcl_Interp *interp);
/*
* local common variables
*/
static void *private;
static int seq_opened = 0;
static int seq_bank = 0, seq_preset = 0;
static int seq_bend = 0;
static vkb_oper_t *oper;
#ifdef HAVE_LASH
lash_args_t * lash_args = NULL;
#endif
/*
* main routine
*/
int main(int argc, char **argv)
{
char **nargv;
int c, nargc;
#ifdef HAVE_LASH
lash_args = lash_extract_args(&argc, &argv);
#endif /* HAVE_LASH */
nargc = argc + 1;
if ((nargv = (char**)malloc(sizeof(char*) * nargc)) == NULL) {
fprintf(stderr, "vkeybd: can't malloc\n");
exit(1);
}
nargv[0] = "-f";
nargv[1] = VKB_TCLFILE;
for (c = 1; c < argc; c++)
nargv[c+1] = argv[c];
/* copy the default device operator */
if (vkb_num_devices <= 0) {
fprintf(stderr, "vkeybd: no device is defined\n");
exit(1);
}
/* call Tk main routine */
Tk_Main(nargc, nargv, vkb_app_init);
return 0;
}
/*
* print usage
*/
static int
usage(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
{
int i;
fprintf(stderr, "vkeybd -- virtual keyboard\n");
fprintf(stderr, " version %s\n", VERSION_STR);
fprintf(stderr, "Copyright (c) 1997-2000 Takashi Iwai\n");
fprintf(stderr, "usage: vkeybd [-options]\n");
fprintf(stderr, "* general options:\n");
fprintf(stderr, " --device mode : specify output device (default = %s)\n", vkb_device[0]->name);
fprintf(stderr, " --keymap file : use own keymap file\n");
fprintf(stderr, " --preset file : use preset list file\n");
fprintf(stderr, " --channel number : specify instrument channel (0-15) (default = %s)\n", DEF_INST_CHAN_NO);
fprintf(stderr, " --drum number : specify drum channel (0-15) (default = %s)\n", DEF_DRUM_CHAN_NO);
fprintf(stderr, " --octave number : specify octaves of displaying keys (default = %s)\n", DEF_OCTAVES);
fprintf(stderr, "* output devices:\n");
for (i = 0; i < vkb_num_devices; i++) {
vkb_optarg_t *p;
fprintf(stderr, " %s = %s\n", vkb_device[i]->name, vkb_device[i]->desc);
if (! (p = vkb_device[i]->opts))
continue;
for (; p->name; p++)
fprintf(stderr, " %s (default = %s)\n",
p->desc, p->defval);
}
return TCL_OK;
}
/*
* Tcl commands to sequencer device
*/
/* open device */
static int
seq_on(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
{
int i;
const char *var;
if (seq_opened)
return TCL_OK;
if (! (var = Tcl_GetVar2(interp, "optvar", "device", TCL_GLOBAL_ONLY))) {
vkb_error(interp, "no output device defined");
return TCL_ERROR;
}
oper = NULL;
for (i = 0; i < vkb_num_devices && vkb_device[i]; i++) {
if (strcmp(vkb_device[i]->name, var) == 0) {
oper = vkb_device[i]->oper;
break;
}
}
if (! oper) {
vkb_error(interp, "no output device defined");
return TCL_ERROR;
}
if (argc > 1 && vkb_device[i]->delayed_open)
return TCL_OK;
if (oper->open(interp, &private)) {
seq_opened = 1;
Tcl_SetVar(interp, "seqswitch", "1", TCL_GLOBAL_ONLY);
if (oper->program)
oper->program(interp, private, seq_bank, seq_preset);
if (oper->bender)
oper->bender(interp, private, seq_bend);
}
return TCL_OK;
}
/* close device */
static int
seq_off(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
{
if (seq_opened) {
oper->close(interp, private);
seq_opened = 0;
Tcl_SetVar(interp, "seqswitch", "0", TCL_GLOBAL_ONLY);
}
return TCL_OK;
}
static int
seq_start_note(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
{
int note, vel;
if (argc < 3)
return TCL_ERROR;
if (! seq_opened)
return TCL_OK;
note = atoi(argv[1]);
vel = atoi(argv[2]);
if (oper->noteon)
oper->noteon(interp, private, note, vel);
return TCL_OK;
}
static int
seq_stop_note(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
{
int note, vel;
if (argc < 3)
return TCL_ERROR;
if (! seq_opened)
return TCL_OK;
note = atoi(argv[1]);
vel = atoi(argv[2]);
if (oper->noteoff)
oper->noteoff(interp, private, note, vel);
return TCL_OK;
}
static int
seq_control(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
{
int type, val;
if (argc < 3)
return TCL_ERROR;
if (! seq_opened)
return TCL_OK;
type = atoi(argv[1]);
val = atoi(argv[2]);
if (oper->control)
oper->control(interp, private, type, val);
return TCL_OK;
}
static int
seq_program(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
{
if (argc < 3)
return TCL_ERROR;
seq_bank = atoi(argv[1]);
seq_preset = atoi(argv[2]);
if (! seq_opened)
return TCL_OK;
if (oper->program)
oper->program(interp, private, seq_bank, seq_preset);
return TCL_OK;
}
static int
seq_bender(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
{
if (argc < 2)
return TCL_ERROR;
seq_bend = atoi(argv[1]);
if (! seq_opened)
return TCL_OK;
if (oper->bender)
oper->bender(interp, private, seq_bend);
return TCL_OK;
}
static int
seq_chorus_mode(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
{
if (argc < 2)
return TCL_ERROR;
if (seq_on(clientData, interp, argc, argv) != TCL_OK)
return TCL_ERROR;
if (oper->chorus_mode)
oper->chorus_mode(interp, private, atoi(argv[1]));
return TCL_OK;
}
static int
seq_reverb_mode(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[])
{
if (argc < 2)
return TCL_ERROR;
if (seq_on(clientData, interp, argc, argv) != TCL_OK)
return TCL_ERROR;
if (oper->reverb_mode)
oper->reverb_mode(interp, private, atoi(argv[1]));
return TCL_OK;
}
/*
* Misc. functions
*/
void
vkb_error(Tcl_Interp *ip, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fputs("ERROR: ", stderr);
vfprintf(stderr, fmt, ap);
putc('\n', stderr);
va_end(ap);
}
int
vkb_get_int(Tcl_Interp *ip, char *opt, int *ret)
{
const char *var;
if ((var = Tcl_GetVar2(ip, "optvar", opt, TCL_GLOBAL_ONLY)) != NULL && *var) {
*ret = atoi(var);
return 1;
}
return 0;
}
/*
* Initialize Tcl/Tk components
*/
static int
vkb_app_init(Tcl_Interp *interp)
{
int i;
if (Tcl_Init(interp) == TCL_ERROR) {
return TCL_ERROR;
}
if (Tk_Init(interp) == TCL_ERROR) {
return TCL_ERROR;
}
Tcl_CreateCommand(interp, "usage", usage,
(ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
Tcl_CreateCommand(interp, "SeqOn", seq_on,
(ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
Tcl_CreateCommand(interp, "SeqOff", seq_off,
(ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
Tcl_CreateCommand(interp, "SeqStartNote", seq_start_note,
(ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
Tcl_CreateCommand(interp, "SeqStopNote", seq_stop_note,
(ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
Tcl_CreateCommand(interp, "SeqControl", seq_control,
(ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
Tcl_CreateCommand(interp, "SeqProgram", seq_program,
(ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
Tcl_CreateCommand(interp, "SeqBender", seq_bender,
(ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
Tcl_CreateCommand(interp, "SeqChorusMode", seq_chorus_mode,
(ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
Tcl_CreateCommand(interp, "SeqReverbMode", seq_reverb_mode,
(ClientData)NULL, (Tcl_CmdDeleteProc*)NULL);
for (i = 0; i < vkb_num_devices; i++) {
vkb_optarg_t *p;
if (! (p = vkb_device[i]->opts))
continue;
for (; p->name; p++) {
Tcl_SetVar2(interp, "optvar", p->name, p->defval, TCL_GLOBAL_ONLY);
}
}
Tcl_SetVar2(interp, "optvar", "device", vkb_device[0]->name, TCL_GLOBAL_ONLY);
Tcl_SetVar2(interp, "optvar", "libpath", VKBLIB_DIR, TCL_GLOBAL_ONLY);
Tcl_SetVar2(interp, "optvar", "channel", DEF_INST_CHAN_NO, TCL_GLOBAL_ONLY);
Tcl_SetVar2(interp, "optvar", "drum", DEF_DRUM_CHAN_NO, TCL_GLOBAL_ONLY);
Tcl_SetVar2(interp, "optvar", "octave", DEF_OCTAVES, TCL_GLOBAL_ONLY);
return TCL_OK;
}