File: gtk_c.c

package info (click to toggle)
timidity 2.14.0-8.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,364 kB
  • sloc: ansic: 168,735; sh: 3,730; makefile: 1,405; tcl: 1,048; perl: 285; ruby: 126
file content (593 lines) | stat: -rw-r--r-- 12,917 bytes parent folder | download | duplicates (3)
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
    TiMidity++ -- MIDI to WAVE converter and player
    Copyright (C) 1999-2004 Masanao Izumo <iz@onicos.co.jp>
    Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>

    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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

    motif_ctl.c: written by Vincent Pagel (pagel@loria.fr) 10/4/95

    A motif interface for TIMIDITY: to prevent X redrawings from
    interfering with the audio computation, I don't use the XtAppAddWorkProc

    I create a pipe between the timidity process and a Motif interface
    process forked from the 1st one

    Copied the Motif file to create a Gtk+ interface.
        - Glenn Trigg 29 Oct 1998

    Modified for TiMidity++
        - Isaku Yamahata 03 Dec 1998
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#ifndef NO_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#include "timidity.h"
#include "common.h"
#include "instrum.h"
#include "playmidi.h"
#include "output.h"
#include "controls.h"
#include "gtk_h.h"
#include "readmidi.h"

static int ctl_open(int using_stdin, int using_stdout);
static void ctl_close(void);
static int ctl_pass_playing_list(int number_of_files, char *list_of_files[]);
static int ctl_read(int32 *valp);
static int cmsg(int type, int verbosity_level, char *fmt, ...);
static void ctl_event(CtlEvent *e);

static void ctl_refresh(void);
static void ctl_total_time(int tt);
static void ctl_master_volume(int mv);
static void ctl_file_name(char *name);
static void ctl_current_time(int secs, int voices);
static void ctl_note(int status, int channel, int note, int velocity);
static void ctl_program(int ch, int val, char *vp);
static void ctl_volume(int channel, int val);
static void ctl_expression(int channel, int val);
static void ctl_panning(int channel, int val);
static void ctl_sustain(int channel, int val);
static void ctl_pitch_bend(int channel, int val);
static void ctl_reset(void);
static void ctl_lyric(int);


/**********************************************/
/* export the interface functions */

#define ctl gtk_control_mode
ControlMode ctl = 
{
    "gtk+ interface", 'g',
    "gtk",
    1,0,0,
    0,
    ctl_open,
    ctl_close,
    ctl_pass_playing_list,
    ctl_read,
    NULL,
    cmsg,
    ctl_event
};


/***********************************************************************/
/* Put controls on the pipe                                            */
/***********************************************************************/
static int
cmsg(int type, int verbosity_level, char *fmt, ...)
{
    char local[255];

    va_list ap;
    if ((type==CMSG_TEXT || type==CMSG_INFO || type==CMSG_WARNING) &&
	ctl.verbosity<verbosity_level)
	return 0;

    va_start(ap, fmt);
    if (!ctl.opened) {
	vfprintf(stderr, fmt, ap);
	fprintf(stderr, "\n");
    }
    else {
	vsnprintf(local, sizeof(local), fmt, ap);
	gtk_pipe_int_write(CMSG_MESSAGE);
	gtk_pipe_int_write(type);
	gtk_pipe_string_write(local);
    }
    va_end(ap);
    return 0;
}


/*
  ctl_event is stolen from ncurses_c.c
 */
static void ctl_event(CtlEvent *e)
{
    switch(e->type)
    {
      case CTLE_NOW_LOADING:
	ctl_file_name((char *)e->v1);
	break;
      case CTLE_LOADING_DONE:
	break;
      case CTLE_PLAY_START:
	ctl_total_time((int)e->v1);
	break;
      case CTLE_PLAY_END:
	break;
      case CTLE_TEMPO:
	break;
      case CTLE_METRONOME:
	break;
      case CTLE_CURRENT_TIME:
	ctl_current_time((int)e->v1, (int)e->v2);
	break;
      case CTLE_NOTE:
	ctl_note((int)e->v1, (int)e->v2, (int)e->v3, (int)e->v4);
	break;
      case CTLE_MASTER_VOLUME:
	ctl_master_volume((int)e->v1);
	break;
      case CTLE_PROGRAM:
	ctl_program((int)e->v1, (int)e->v2, (char *)e->v3);
	break;
      case CTLE_VOLUME:
	ctl_volume((int)e->v1, (int)e->v2);
	break;
      case CTLE_EXPRESSION:
	ctl_expression((int)e->v1, (int)e->v2);
	break;
      case CTLE_PANNING:
	ctl_panning((int)e->v1, (int)e->v2);
	break;
      case CTLE_SUSTAIN:
	ctl_sustain((int)e->v1, (int)e->v2);
	break;
      case CTLE_PITCH_BEND:
	ctl_pitch_bend((int)e->v1, (int)e->v2);
	break;
      case CTLE_MOD_WHEEL:
	ctl_pitch_bend((int)e->v1, e->v2);
	break;
      case CTLE_CHORUS_EFFECT:
	break;
      case CTLE_REVERB_EFFECT:
	break;
      case CTLE_LYRIC:
	ctl_lyric((int)e->v1);
	break;
      case CTLE_REFRESH:
	ctl_refresh();
	break;
      case CTLE_RESET:
	ctl_reset();
	break;
      case CTLE_SPEANA:
	break;
    }
}

static void
ctl_refresh(void)
{
    /* gtk_pipe_int_write(REFRESH_MESSAGE); */
}

static void
ctl_total_time(int tt)
{
    gtk_pipe_int_write(TOTALTIME_MESSAGE);
    gtk_pipe_int_write(tt);
}

static void
ctl_master_volume(int mv)
{
    gtk_pipe_int_write(MASTERVOL_MESSAGE);
    gtk_pipe_int_write(mv);
}

static void
ctl_file_name(char *name)
{
    gtk_pipe_int_write(FILENAME_MESSAGE);
    gtk_pipe_string_write(name);
}

static void
ctl_current_time(int secs, int voices)
{
    gtk_pipe_int_write(CURTIME_MESSAGE);
    gtk_pipe_int_write(secs);
    gtk_pipe_int_write(voices);
}

static void
ctl_note(int status, int channel, int note, int velocity)
{
    /*   int xl;
    if (!ctl.trace_playing) 
	return;
    xl=voice[v].note%(COLS-24);
    wmove(dftwin, 8+voice[v].channel,xl+3);
    switch(voice[v].status)
	{
	case VOICE_DIE:
	    waddch(dftwin, ',');
	    break;
	case VOICE_FREE: 
	    waddch(dftwin, '.');
	    break;
	case VOICE_ON:
	    wattron(dftwin, A_BOLD);
	    waddch(dftwin, '0'+(10*voice[v].velocity)/128); 
	    wattroff(dftwin, A_BOLD);
	    break;
	case VOICE_OFF:
	case VOICE_SUSTAINED:
	    waddch(dftwin, '0'+(10*voice[v].velocity)/128);
	    break;
	}
	*/
}

static void
ctl_program(int ch, int val, char *vp)
{
/*  if (!ctl.trace_playing) 
    return;
  wmove(dftwin, 8+ch, COLS-20);
  if (ISDRUMCHANNEL(ch))
    {
      wattron(dftwin, A_BOLD);
      wprintw(dftwin, "%03d", val);
      wattroff(dftwin, A_BOLD);
    }
  else
    wprintw(dftwin, "%03d", val);
    */
}

static void
ctl_volume(int channel, int val)
{
    /*
      if (!ctl.trace_playing) 
    return;
  wmove(dftwin, 8+channel, COLS-16);
  wprintw(dftwin, "%3d", (val*100)/127);
  */
}

static void
ctl_expression(int channel, int val)
{
/*  if (!ctl.trace_playing) 
    return;
  wmove(dftwin, 8+channel, COLS-12);
  wprintw(dftwin, "%3d", (val*100)/127);
  */
}

static void
ctl_panning(int channel, int val)
{
/*  if (!ctl.trace_playing) 
    return;
  
  if (val==NO_PANNING)
    waddstr(dftwin, "   ");
  else if (val<5)
    waddstr(dftwin, " L ");
  else if (val>123)
    waddstr(dftwin, " R ");
  else if (val>60 && val<68)
    waddstr(dftwin, " C ");
    */
}

static void
ctl_sustain(int channel, int val)
{
/*
  if (!ctl.trace_playing) 
    return;

  if (val) waddch(dftwin, 'S');
  else waddch(dftwin, ' ');
  */
}

static void
ctl_pitch_bend(int channel, int val)
{
/*  if (!ctl.trace_playing) 
    return;

  if (val>0x2000) waddch(dftwin, '+');
  else if (val<0x2000) waddch(dftwin, '-');
  else waddch(dftwin, ' ');
  */
}

static void
ctl_reset(void)
{
/*  int i,j;
  if (!ctl.trace_playing) 
    return;
  for (i=0; i<16; i++)
    {
	ctl_program(i, channel[i].program);
	ctl_volume(i, channel[i].volume);
	ctl_expression(i, channel[i].expression);
	ctl_panning(i, channel[i].panning);
	ctl_sustain(i, channel[i].sustain);
	ctl_pitch_bend(i, channel[i].pitchbend);
    }
  ctl_refresh();
  */
}

static void
ctl_lyric(int lyricid)
{
    char	*lyric;
    static char	lyric_buf[300];

    lyric = event2string(lyricid);
    if(lyric != NULL)
    {
	if(lyric[0] == ME_KARAOKE_LYRIC)
	{
	    if(!lyric[1])
		return;
	    if(lyric[1] == '/' || lyric[1] == '\\')
	    {
		snprintf(lyric_buf, sizeof(lyric_buf), "\n%s", lyric + 2);
		gtk_pipe_int_write(LYRIC_MESSAGE);
		gtk_pipe_string_write(lyric_buf);
	    }
	    else if(lyric[1] == '@')
	    {
		if(lyric[2] == 'L')
		    snprintf(lyric_buf, sizeof(lyric_buf), "Language: %s\n", lyric + 3);
		else if(lyric[2] == 'T')
		    snprintf(lyric_buf, sizeof(lyric_buf), "Title: %s\n", lyric + 3);
		else
		    snprintf(lyric_buf, sizeof(lyric_buf), "%s\n", lyric + 1);
		gtk_pipe_int_write(LYRIC_MESSAGE);
		gtk_pipe_string_write(lyric_buf);
	    }
	    else
	    {
		strncpy(lyric_buf, lyric + 1, sizeof(lyric_buf) - 1);
		gtk_pipe_int_write(LYRIC_MESSAGE);
		gtk_pipe_string_write(lyric_buf);
	    }
	}
	else
	{
	    strncpy(lyric_buf, lyric + 1, sizeof(lyric_buf) - 1);
	    gtk_pipe_int_write(LYRIC_MESSAGE);
	    gtk_pipe_string_write(lyric_buf);
	}
    }
}

/***********************************************************************/
/* OPEN THE CONNECTION                                                */
/***********************************************************************/
static int
ctl_open(int using_stdin, int using_stdout)
{
    ctl.opened=1;
  
    /* The child process won't come back from this call  */
    gtk_pipe_open();

    return 0;
}

/* Tells the window to disapear */
static void
ctl_close(void)
{
    if (ctl.opened) {
	gtk_pipe_int_write(CLOSE_MESSAGE);
	ctl.opened=0;
    }
}


/* 
 * Read information coming from the window in a BLOCKING way
 */
static int
ctl_blocking_read(int32 *valp)
{
    int command;
    int new_volume;
    int new_centiseconds;

    gtk_pipe_int_read(&command);
  
    while (1)    /* Loop after pause sleeping to treat other buttons! */
    {

	switch(command) {
	case GTK_CHANGE_VOLUME:
	    gtk_pipe_int_read(&new_volume);
	    *valp= new_volume - amplification ;
	    return RC_CHANGE_VOLUME;
		  
	case GTK_CHANGE_LOCATOR:
	    gtk_pipe_int_read(&new_centiseconds);
	    *valp= new_centiseconds*(play_mode->rate / 100) ;
	    return RC_JUMP;
		  
	case GTK_QUIT:
	    return RC_QUIT;
		
	case GTK_PLAY_FILE:
	    return RC_LOAD_FILE;		  
		  
	case GTK_NEXT:
	    return RC_NEXT;
		  
	case GTK_PREV:
	    return RC_REALLY_PREVIOUS;
		  
	case GTK_RESTART:
	    return RC_RESTART;
		  
	case GTK_FWD:
	    *valp=play_mode->rate;
	    return RC_FORWARD;
		  
	case GTK_RWD:
	    *valp=play_mode->rate;
	    return RC_BACK;

	case GTK_KEYUP:
	    *valp = 1;
	    return RC_KEYUP;

	case GTK_KEYDOWN:
	    *valp = -1;
	    return RC_KEYDOWN;

	case GTK_SLOWER:
	    *valp = 1;
	    return RC_SPEEDDOWN;

	case GTK_FASTER:
	    *valp = 1;
	    return RC_SPEEDUP;
	}
	  
	  
	if (command==GTK_PAUSE) {
	    gtk_pipe_int_read(&command); /* Blocking reading => Sleep ! */
	    if (command==GTK_PAUSE)
		return RC_NONE; /* Resume where we stopped */
	}
	else {
	    fprintf(stderr,"gtk UNKNOWN RC_MESSAGE %i\n",command);
	    return RC_NONE;
	}
    }
}

/* 
 * Read information coming from the window in a non blocking way
 */
static int
ctl_read(int32 *valp)
{
    int num;

    /* We don't wan't to lock on reading  */
    num = gtk_pipe_read_ready();

    if (num==0)
	return RC_NONE;
  
    return(ctl_blocking_read(valp));
#if 0
    num = ctl_blocking_read(valp);
    fprintf (stderr, "cmd=%i", num);
    return num;
#endif
}

static int 
ctl_pass_playing_list(int number_of_files, char *list_of_files[])
{
    int i=0;
    char file_to_play[1000];
    int command;
    int32 val;

    if( number_of_files > 0 ) {
	/* Pass the list to the interface */
	gtk_pipe_int_write(FILE_LIST_MESSAGE);
	gtk_pipe_int_write(number_of_files);
	for (i=0;i<number_of_files;i++)
	    gtk_pipe_string_write(list_of_files[i]);
    
	/* Ask the interface for a filename to play -> begin to play automatically */
	gtk_pipe_int_write(NEXT_FILE_MESSAGE);
    }

    command = ctl_blocking_read(&val);

    /* Main Loop */
    for (;;) { 
	if (command==RC_LOAD_FILE) {
	    /* Read a LoadFile command */
	    gtk_pipe_string_read(file_to_play);
	    command=play_midi_file(file_to_play);
	}
	else {
	    if (command==RC_QUIT)
		return 0;
	    if (command==RC_ERROR)
		command=RC_TUNE_END; /* Launch next file */
	    

	    switch(command) {
	    case RC_NEXT:
		gtk_pipe_int_write(NEXT_FILE_MESSAGE);
		break;
	    case RC_REALLY_PREVIOUS:
		gtk_pipe_int_write(PREV_FILE_MESSAGE);
		break;
	    case RC_TUNE_END:
		gtk_pipe_int_write(TUNE_END_MESSAGE);
		break;
	    default:
		printf("PANIC !!! OTHER COMMAND ERROR ?!?! %i\n",command);
	    }
		    
	    command = ctl_blocking_read(&val);
	}
    }
    return 0;
}

/*
 * interface_<id>_loader();
 */
ControlMode *interface_g_loader(void)
{
    return &ctl;
}