~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to kmidi/ncurs_c.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 
 
3
    TiMidity -- Experimental MIDI to WAVE converter
 
4
    Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
 
5
 
 
6
    This program is free software; you can redistribute it and/or modify
 
7
    it under the terms of the GNU General Public License as published by
 
8
    the Free Software Foundation; either version 2 of the License, or
 
9
    (at your option) any later version.
 
10
 
 
11
    This program is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
    GNU General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU General Public License
 
17
    along with this program; if not, write to the Free Software
 
18
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 
 
20
    ncurs_c.c
 
21
   
 
22
    */
 
23
 
 
24
#ifdef IA_NCURSES
 
25
 
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
#include <unistd.h>
 
29
#include <stdarg.h>
 
30
#include "config.h"
 
31
 
 
32
#ifdef HAVE_NCURSES_CURSES_H
 
33
#include <ncurses/curses.h>
 
34
#else
 
35
#ifdef HAVE_NCURSES_H
 
36
#include <ncurses.h>
 
37
#else
 
38
#include <curses.h>
 
39
#endif
 
40
#endif
 
41
 
 
42
#ifdef HAVE_STRING_H
 
43
#include <string.h>
 
44
#else
 
45
#include <strings.h>
 
46
#endif
 
47
 
 
48
#include "common.h"
 
49
#include "instrum.h"
 
50
#include "playmidi.h"
 
51
#include "output.h"
 
52
#include "controls.h"
 
53
#include "version.h"
 
54
 
 
55
static void ctl_refresh(void);
 
56
static void ctl_help_mode(void);
 
57
static void ctl_total_time(uint32 tt);
 
58
static void ctl_master_volume(int mv);
 
59
static void ctl_file_name(char *name);
 
60
static void ctl_current_time(uint32 ct);
 
61
static void ctl_note(int v);
 
62
static void ctl_note_display(void);
 
63
static void ctl_program(int ch, int val, const char *name);
 
64
static void ctl_volume(int ch, int val);
 
65
static void ctl_expression(int ch, int val);
 
66
static void ctl_panning(int ch, int val);
 
67
static void ctl_sustain(int ch, int val);
 
68
static void ctl_pitch_bend(int ch, int val);
 
69
static void ctl_reset(void);
 
70
static int ctl_open(int using_stdin, int using_stdout);
 
71
static void ctl_close(void);
 
72
static int ctl_read(int32 *valp);
 
73
static int cmsg(int type, int verbosity_level, const char *fmt, ...);
 
74
 
 
75
/**********************************************/
 
76
/* export the interface functions */
 
77
 
 
78
#undef ctl
 
79
#define ctl ncurses_control_mode
 
80
 
 
81
ControlMode ctl= 
 
82
{
 
83
  "ncurses interface", 'n',
 
84
  1,0,0,
 
85
  ctl_open,dumb_pass_playing_list, ctl_close, ctl_read, cmsg,
 
86
  ctl_refresh, ctl_reset, ctl_file_name, ctl_total_time, ctl_current_time, 
 
87
  ctl_note, 
 
88
  ctl_master_volume, ctl_program, ctl_volume, 
 
89
  ctl_expression, ctl_panning, ctl_sustain, ctl_pitch_bend
 
90
};
 
91
 
 
92
 
 
93
/***********************************************************************/
 
94
/* foreground/background checks disabled since switching to curses */
 
95
/* static int in_foreground=1; */
 
96
static int ctl_helpmode=0;
 
97
static int maxy, maxx;
 
98
static int save_master_volume;
 
99
static int save_total_time;
 
100
static char save_file_name[50];
 
101
static int screen_bugfix = 0;
 
102
static uint32 current_centiseconds = 0;
 
103
static int songoffset = 0, current_voices = 0;
 
104
 
 
105
extern MidiEvent *current_event;
 
106
 
 
107
typedef struct {
 
108
  uint8 status, channel, note, velocity, voices;
 
109
  uint32 time;
 
110
} OldVoice;
 
111
#define MAX_OLD_NOTES 500
 
112
static OldVoice old_note[MAX_OLD_NOTES];
 
113
static int leading_pointer=0, trailing_pointer=0;
 
114
 
 
115
static int tr_lm, tr_nw;
 
116
#define MAX_PNW 12
 
117
static char patch_name[16][MAX_PNW];
 
118
 
 
119
static char short_cfg_names[4][6];
 
120
 
 
121
static int my_bg = COLOR_BLACK;
 
122
 
 
123
static void set_color(WINDOW *win, chtype color)
 
124
{
 
125
        if (has_colors()) {
 
126
                static bool *pairs;
 
127
                int n = (color + 1);
 
128
                if (pairs == 0)
 
129
                        pairs = (bool *)calloc(COLORS+1, sizeof(bool));
 
130
                if (!pairs[n]) {
 
131
                        init_pair(n, color, my_bg);
 
132
                        pairs[n] = TRUE;
 
133
                }
 
134
                wattroff(win, A_COLOR);
 
135
                wattron(win, COLOR_PAIR(n));
 
136
        }
 
137
}
 
138
 
 
139
static void unset_color(WINDOW *win)
 
140
{
 
141
        if (has_colors())
 
142
                wattrset(win, COLOR_PAIR(0));
 
143
}
 
144
 
 
145
 
 
146
static WINDOW *dftwin=0, *msgwin=0;
 
147
 
 
148
static void _ctl_refresh(void)
 
149
{
 
150
  wmove(dftwin, 0,0);
 
151
  wrefresh(dftwin);
 
152
}
 
153
 
 
154
static void ctl_refresh(void)
 
155
{
 
156
  if (ctl.trace_playing)
 
157
    {
 
158
       ctl_current_time(0);
 
159
       ctl_note_display();
 
160
       wmove(dftwin, 4,48);
 
161
       wprintw(dftwin, (char *)"%2d", current_voices);
 
162
    }
 
163
    _ctl_refresh();
 
164
}
 
165
 
 
166
/* This is taken from TiMidity++. */
 
167
static void re_init_screen(void)
 
168
{
 
169
    if(screen_bugfix)
 
170
        return;
 
171
    screen_bugfix = 1;
 
172
  wmove(dftwin, 1,3);
 
173
  wprintw(dftwin, (char *)"patchset: %-20s interpolation: %-8s",
 
174
        cfg_names[cfg_select]? cfg_names[cfg_select] : "?",
 
175
        current_interpolation? ((current_interpolation==1)? "C-spline":"LaGrange") : "linear" );
 
176
    touchwin(dftwin);
 
177
    _ctl_refresh();
 
178
    if(msgwin)
 
179
    {
 
180
        touchwin(msgwin);
 
181
        wrefresh(msgwin);
 
182
    }
 
183
}
 
184
 
 
185
static void ctl_help_mode(void)
 
186
{
 
187
  static WINDOW *helpwin;
 
188
  if (ctl_helpmode)
 
189
    {
 
190
      ctl_helpmode=0;
 
191
      delwin(helpwin);
 
192
      touchwin(dftwin);
 
193
      _ctl_refresh();
 
194
    }
 
195
  else
 
196
    {
 
197
      ctl_helpmode=1;
 
198
      /* And here I thought the point of curses was that you could put
 
199
         stuff on windows without having to worry about whether this
 
200
         one is overlapping that or the other way round... */
 
201
      helpwin=newwin(2,maxx,0,0);
 
202
      wattron(helpwin, A_REVERSE);
 
203
      werase(helpwin); 
 
204
      waddstr(helpwin, 
 
205
              "V/Up=Louder    b/Left=Skip back      "
 
206
              "n/Next=Next file      r/Home=Restart file");
 
207
      wmove(helpwin, 1,0);
 
208
      waddstr(helpwin, 
 
209
              "v/Down=Softer  f/Right=Skip forward  "
 
210
              "p/Prev=Previous file  q/End=Quit program");
 
211
      wattroff(helpwin, A_REVERSE);
 
212
      wrefresh(helpwin);
 
213
    }
 
214
}
 
215
 
 
216
static void ctl_total_time(uint32 tt)
 
217
{
 
218
  int mins, secs=(int)tt/play_mode->rate;
 
219
  save_total_time = tt;
 
220
  mins=secs/60;
 
221
  secs-=mins*60;
 
222
 
 
223
  wmove(dftwin, 4,6+6+3);
 
224
  wattron(dftwin, A_BOLD);
 
225
  wprintw(dftwin, (char *)"%3d:%02d", mins, secs);
 
226
  wattroff(dftwin, A_BOLD);
 
227
  _ctl_refresh();
 
228
 
 
229
  songoffset = 0;
 
230
  current_centiseconds = 0;
 
231
  for (secs=0; secs<16; secs++) patch_name[secs][0] = '\0';
 
232
}
 
233
 
 
234
static void ctl_master_volume(int mv)
 
235
{
 
236
  save_master_volume = mv;
 
237
  wmove(dftwin, 4,maxx-5);
 
238
  wattron(dftwin, A_BOLD);
 
239
  wprintw(dftwin, (char *)"%3d%%", mv);
 
240
  wattroff(dftwin, A_BOLD);
 
241
  _ctl_refresh();
 
242
}
 
243
 
 
244
static void ctl_file_name(char *name)
 
245
{
 
246
  strncpy(save_file_name, name, 50);
 
247
  save_file_name[49] = '\0';
 
248
  wmove(dftwin, 3,6);
 
249
  wclrtoeol(dftwin );
 
250
  wattron(dftwin, A_BOLD);
 
251
  waddstr(dftwin, name);
 
252
  wattroff(dftwin, A_BOLD);
 
253
  _ctl_refresh();
 
254
}
 
255
 
 
256
static void ctl_current_time(uint32 ct)
 
257
{
 
258
  int centisecs, realct;
 
259
  int mins, secs;
 
260
 
 
261
  if (!ctl.trace_playing) 
 
262
    return;
 
263
 
 
264
  realct = play_mode->output_count(ct);
 
265
  if (realct < 0) realct = 0;
 
266
  else realct += songoffset;
 
267
  centisecs = realct / (play_mode->rate/100);
 
268
  current_centiseconds = (uint32)centisecs;
 
269
 
 
270
  secs=centisecs/100;
 
271
  mins=secs/60;
 
272
  secs-=mins*60;
 
273
  wmove(dftwin, 4,6);
 
274
  if (has_colors()) set_color(dftwin, COLOR_GREEN);
 
275
  else wattron(dftwin, A_BOLD);
 
276
  wprintw(dftwin, (char *)"%3d:%02d", mins, secs);
 
277
  if (has_colors()) unset_color(dftwin);
 
278
  else wattroff(dftwin, A_BOLD);
 
279
  _ctl_refresh();
 
280
}
 
281
 
 
282
static void ctl_note(int v)
 
283
{
 
284
  int i, n;
 
285
  if (!ctl.trace_playing) 
 
286
    return;
 
287
  if (voice[v].clone_type != 0) return;
 
288
 
 
289
  old_note[leading_pointer].status = voice[v].status;
 
290
  old_note[leading_pointer].channel = voice[v].channel;
 
291
  old_note[leading_pointer].note = voice[v].note;
 
292
  old_note[leading_pointer].velocity = voice[v].velocity;
 
293
  old_note[leading_pointer].time = current_event->time / (play_mode->rate/100);
 
294
  n=0;
 
295
  i=voices;
 
296
  while (i--)
 
297
    if (voice[i].status!=VOICE_FREE) n++;
 
298
  old_note[leading_pointer].voices = n;
 
299
  leading_pointer++;
 
300
  if (leading_pointer == MAX_OLD_NOTES) leading_pointer = 0;
 
301
 
 
302
}
 
303
 
 
304
#define LOW_CLIP 32
 
305
#define HIGH_CLIP 59
 
306
 
 
307
static void ctl_note_display(void)
 
308
{
 
309
  int v = trailing_pointer;
 
310
  uint32 then;
 
311
 
 
312
  if (tr_nw < 10) return;
 
313
 
 
314
  then = old_note[v].time;
 
315
 
 
316
  while (then <= current_centiseconds && v != leading_pointer)
 
317
    {
 
318
        int xl, new_note;
 
319
        new_note = old_note[v].note - LOW_CLIP;
 
320
        if (new_note < 0) new_note = 0;
 
321
        if (new_note > HIGH_CLIP) new_note = HIGH_CLIP;
 
322
        xl = (new_note * tr_nw) / (HIGH_CLIP+1);
 
323
        wmove(dftwin, 8+(old_note[v].channel & 0x0f),xl + tr_lm);
 
324
        switch(old_note[v].status)
 
325
          {
 
326
            case VOICE_DIE:
 
327
              waddch(dftwin, ACS_CKBOARD);
 
328
              break;
 
329
            case VOICE_FREE: 
 
330
              waddch(dftwin, ACS_BULLET);
 
331
              break;
 
332
            case VOICE_ON:
 
333
              if (has_colors())
 
334
                {
 
335
                  if (channel[old_note[v].channel].kit) set_color(dftwin, COLOR_YELLOW);
 
336
                  else set_color(dftwin, COLOR_RED);
 
337
                }
 
338
              else wattron(dftwin, A_BOLD);
 
339
              waddch(dftwin, '0'+(10*old_note[v].velocity)/128); 
 
340
              if (has_colors()) unset_color(dftwin);
 
341
              else wattroff(dftwin, A_BOLD);
 
342
              break;
 
343
            case VOICE_OFF:
 
344
            case VOICE_SUSTAINED:
 
345
              if (has_colors()) set_color(dftwin, COLOR_GREEN);
 
346
              waddch(dftwin, '0'+(10*old_note[v].velocity)/128);
 
347
              if (has_colors()) unset_color(dftwin);
 
348
              break;
 
349
          }
 
350
        current_voices = old_note[v].voices;
 
351
        v++;
 
352
        if (v == MAX_OLD_NOTES) v = 0;
 
353
        then = old_note[v].time;
 
354
    }
 
355
  trailing_pointer = v;
 
356
}
 
357
 
 
358
static void ctl_program(int ch, int val, const char *name)
 
359
{
 
360
  int realch = ch;
 
361
  if (!ctl.trace_playing) 
 
362
    return;
 
363
  ch &= 0x0f;
 
364
 
 
365
  if (name && realch<16)
 
366
    {
 
367
      strncpy(patch_name[ch], name, MAX_PNW);
 
368
      patch_name[ch][MAX_PNW-1] = '\0';
 
369
    }
 
370
  if (tr_lm > 3)
 
371
    {
 
372
      wmove(dftwin, 8+ch, 3);
 
373
      if (patch_name[ch][0])
 
374
        wprintw(dftwin, (char *)"%-12s", patch_name[ch]);
 
375
      else waddstr(dftwin, "            ");
 
376
    }
 
377
 
 
378
  wmove(dftwin, 8+ch, maxx-20);
 
379
  if (channel[ch].kit)
 
380
    {
 
381
      if (has_colors()) set_color(dftwin, COLOR_YELLOW);
 
382
      else wattron(dftwin, A_BOLD);
 
383
      wprintw(dftwin, (char *)"%03d", val);
 
384
      if (has_colors()) unset_color(dftwin);
 
385
      else wattroff(dftwin, A_BOLD);
 
386
    }
 
387
  else
 
388
    wprintw(dftwin, (char *)"%03d", val);
 
389
}
 
390
 
 
391
static void ctl_volume(int ch, int val)
 
392
{
 
393
  if (!ctl.trace_playing) 
 
394
    return;
 
395
  ch &= 0x0f;
 
396
  wmove(dftwin, 8+ch, maxx-16);
 
397
  wprintw(dftwin, (char *)"%3d", (val*100)/127);
 
398
}
 
399
 
 
400
static void ctl_expression(int ch, int val)
 
401
{
 
402
  if (!ctl.trace_playing) 
 
403
    return;
 
404
  ch &= 0x0f;
 
405
  wmove(dftwin, 8+ch, maxx-12);
 
406
  wprintw(dftwin, (char *)"%3d", (val*100)/127);
 
407
}
 
408
 
 
409
static void ctl_panning(int ch, int val)
 
410
{
 
411
  if (!ctl.trace_playing) 
 
412
    return;
 
413
  ch &= 0x0f;
 
414
  wmove(dftwin, 8+ch, maxx-8);
 
415
  if (val != NO_PANNING && has_colors())
 
416
    {
 
417
      if (val <= 60) set_color(dftwin, COLOR_CYAN);
 
418
      if (val >= 68) set_color(dftwin, COLOR_GREEN);
 
419
    }
 
420
  if (val==NO_PANNING)
 
421
    waddstr(dftwin, "   ");
 
422
  else if (val<5)
 
423
    waddstr(dftwin, " L ");
 
424
  else if (val>123)
 
425
    waddstr(dftwin, " R ");
 
426
  else if (val>60 && val<68)
 
427
    waddstr(dftwin, " C ");
 
428
  else
 
429
    {
 
430
      /* wprintw(dftwin, "%+02d", (100*(val-64))/64); */
 
431
      val = (100*(val-64))/64; /* piss on curses */
 
432
      if (val<0)
 
433
        {
 
434
          waddch(dftwin, '-');
 
435
          val=-val;
 
436
        }
 
437
      else waddch(dftwin, '+');
 
438
      wprintw(dftwin, (char *)"%02d", val);
 
439
    }
 
440
  if (val != NO_PANNING && has_colors())
 
441
    {
 
442
      unset_color(dftwin);
 
443
    }
 
444
}
 
445
 
 
446
static void ctl_sustain(int ch, int val)
 
447
{
 
448
  if (!ctl.trace_playing) 
 
449
    return;
 
450
  ch &= 0x0f;
 
451
  wmove(dftwin, 8+ch, maxx-4);
 
452
  if (val) waddch(dftwin, 'S');
 
453
  else waddch(dftwin, ' ');
 
454
}
 
455
 
 
456
static void ctl_pitch_bend(int ch, int val)
 
457
{
 
458
  if (!ctl.trace_playing) 
 
459
    return;
 
460
  ch &= 0x0f;
 
461
  wmove(dftwin, 8+ch, maxx-2);
 
462
  if (val>0x2000) waddch(dftwin, '+');
 
463
  else if (val<0x2000) waddch(dftwin, '-');
 
464
  else waddch(dftwin, ' ');
 
465
}
 
466
 
 
467
static void ctl_reset(void)
 
468
{
 
469
  int i,j;
 
470
  if (!ctl.trace_playing) 
 
471
    return;
 
472
 
 
473
  for (i=0; i<16; i++)
 
474
    {
 
475
      wmove(dftwin, 8+i, tr_lm);
 
476
      if (tr_nw >= 10)
 
477
        for (j=0; j<tr_nw; j++) waddch(dftwin, ACS_BULLET);
 
478
      ctl_program(i, channel[i].program, 0);
 
479
      ctl_volume(i, channel[i].volume);
 
480
      ctl_expression(i, channel[i].expression);
 
481
      ctl_panning(i, channel[i].panning);
 
482
      ctl_sustain(i, channel[i].sustain);
 
483
      ctl_pitch_bend(i, channel[i].pitchbend);
 
484
    }
 
485
  for (i=0; i<MAX_OLD_NOTES; i++)
 
486
    {
 
487
      old_note[i].time = 0;
 
488
    }
 
489
  leading_pointer = trailing_pointer = current_voices = 0;
 
490
  _ctl_refresh();
 
491
}
 
492
 
 
493
/***********************************************************************/
 
494
 
 
495
/*#define CURSED_REDIR_HACK*/
 
496
 
 
497
#ifdef CURSED_REDIR_HACK
 
498
static SCREEN *oldscr;
 
499
#endif
 
500
 
 
501
static void draw_windows(void)
 
502
{
 
503
  int i;
 
504
  tr_lm = 3;
 
505
  tr_nw = maxx - 25;
 
506
  if (tr_nw > MAX_PNW + 1 + 20)
 
507
    {
 
508
        tr_nw -= MAX_PNW + 1;
 
509
        tr_lm += MAX_PNW + 1;
 
510
    }
 
511
  werase(dftwin);
 
512
  wmove(dftwin, 0,0);
 
513
  waddstr(dftwin, "TiMidity v" TIMID_VERSION);
 
514
  wmove(dftwin, 0,maxx-52);
 
515
  waddstr(dftwin, "(C) 1995 Tuukka Toivonen <toivonen@clinet.fi>");
 
516
/*
 
517
  wmove(dftwin, 1,0);
 
518
  waddstr(dftwin, "Press 'h' for help with keys, or 'q' to quit.");
 
519
*/
 
520
  wmove(dftwin, 1,3);
 
521
  wprintw(dftwin, (char *)"patchset: %-20s interpolation: %-8s",
 
522
        cfg_names[cfg_select]? cfg_names[cfg_select] : "?",
 
523
        current_interpolation? ((current_interpolation==1)? "C-spline":"LaGrange") : "linear" );
 
524
  wmove(dftwin, 3,0);
 
525
  waddstr(dftwin, "File:");
 
526
  wmove(dftwin, 4,0);
 
527
  if (ctl.trace_playing)
 
528
    {
 
529
      waddstr(dftwin, "Time:");
 
530
      wmove(dftwin, 4,6+6+1);
 
531
      waddch(dftwin, '/');
 
532
      wmove(dftwin, 4,40);
 
533
      wprintw(dftwin, (char *)"Voices:    / %d", voices);
 
534
    }
 
535
  else
 
536
    {
 
537
      waddstr(dftwin, "Playing time:");
 
538
    }
 
539
  wmove(dftwin, 4,maxx-20);
 
540
  waddstr(dftwin, "Master volume:");
 
541
  if (save_master_volume) ctl_master_volume(save_master_volume);
 
542
  if (save_total_time) ctl_total_time(save_total_time);
 
543
  if (save_file_name[0]) ctl_file_name(save_file_name);
 
544
  wmove(dftwin, 5,0);
 
545
  for (i=0; i<maxx; i++)
 
546
    waddch(dftwin, ACS_HLINE);
 
547
  if (ctl.trace_playing)
 
548
    {
 
549
      wmove(dftwin, 6,0);
 
550
      waddstr(dftwin, "Ch");
 
551
      wmove(dftwin, 6,maxx-20);
 
552
      waddstr(dftwin, "Prg Vol Exp Pan S B");
 
553
      wmove(dftwin, 7,0);
 
554
      for (i=0; i<maxx; i++)
 
555
        waddch(dftwin, ACS_HLINE);
 
556
      for (i=0; i<16; i++)
 
557
        {
 
558
          wmove(dftwin, 8+i, 0);
 
559
          wprintw(dftwin, (char *)"%02d", i+1);
 
560
        }
 
561
    }
 
562
  else
 
563
    {
 
564
      if (msgwin) wresize(msgwin, maxy-6, maxx);
 
565
      else
 
566
        {
 
567
          msgwin=newwin(maxy-6,maxx,6,0);
 
568
          scrollok(msgwin, TRUE);
 
569
          werase(msgwin);
 
570
        }
 
571
      touchwin(msgwin);
 
572
      wrefresh(msgwin);
 
573
    }
 
574
  touchwin(dftwin);
 
575
  wrefresh(dftwin);
 
576
  screen_bugfix = 0;
 
577
}
 
578
 
 
579
static int ctl_open(int using_stdin, int using_stdout)
 
580
{
 
581
  int i;
 
582
#ifdef CURSED_REDIR_HACK
 
583
  FILE *infp=stdin, *outfp=stdout;
 
584
  SCREEN *dftscr;
 
585
 
 
586
  /* This doesn't work right */
 
587
  if (using_stdin && using_stdout)
 
588
    {
 
589
      infp=outfp=stderr;
 
590
      fflush(stderr);
 
591
      setvbuf(stderr, 0, _IOFBF, BUFSIZ);
 
592
    }
 
593
  else if (using_stdout)
 
594
    {
 
595
      outfp=stderr;
 
596
      fflush(stderr);
 
597
      setvbuf(stderr, 0, _IOFBF, BUFSIZ);
 
598
    }
 
599
  else if (using_stdin)
 
600
    {
 
601
      infp=stdout;
 
602
      fflush(stdout);
 
603
      setvbuf(stdout, 0, _IOFBF, BUFSIZ);
 
604
    }
 
605
 
 
606
  slk_init(3);
 
607
  dftscr=newterm(0, outfp, infp);
 
608
  if (!dftscr)
 
609
    return -1;
 
610
  oldscr=set_term(dftscr);
 
611
  /* dftwin=stdscr; */
 
612
#else
 
613
  slk_init(3);
 
614
  initscr();
 
615
#endif
 
616
 
 
617
  cbreak();
 
618
  noecho();
 
619
  nonl();
 
620
  nodelay(stdscr, 1);
 
621
  scrollok(stdscr, 0);
 
622
  /*leaveok(stdscr, 1);*/
 
623
  idlok(stdscr, 1);
 
624
  keypad(stdscr, TRUE);
 
625
 
 
626
 
 
627
  if (has_colors())
 
628
    {
 
629
        start_color();
 
630
#ifdef HAVE_USE_DEFAULT_COLORS
 
631
        if (use_default_colors() == OK)
 
632
            my_bg = -1;
 
633
#endif
 
634
    }
 
635
 
 
636
  curs_set(0);
 
637
  getmaxyx(stdscr,maxy,maxx);
 
638
  ctl.opened=1;
 
639
 
 
640
  if (ctl.trace_playing)
 
641
    dftwin=stdscr;
 
642
  else
 
643
    dftwin=newwin(6,maxx,0,0);
 
644
 
 
645
  slk_set(1, "Help", 1);
 
646
  slk_set(2, "Lnear", 1);
 
647
  slk_set(3, "Cspln", 1);
 
648
  slk_set(4, "Lgrng", 1);
 
649
 
 
650
  for (i=0; i<4; i++)
 
651
    {
 
652
      if (cfg_names[i]) strncpy(short_cfg_names[i], cfg_names[i], 5);
 
653
      else strcpy(short_cfg_names[i], "(nne)");
 
654
      short_cfg_names[i][5] = '\0';
 
655
      slk_set(i+5, short_cfg_names[i], 1);
 
656
    }
 
657
 
 
658
  slk_set(10, "Quit", 1);
 
659
  if (opt_dry) slk_set(11, "Dry", 1);
 
660
  else slk_set(11, "Wet", 1);
 
661
  slk_refresh();
 
662
  draw_windows();
 
663
  
 
664
  return 0;
 
665
}
 
666
 
 
667
static void ctl_close(void)
 
668
{
 
669
  if (ctl.opened)
 
670
    {
 
671
        refresh();
 
672
        endwin();
 
673
        curs_set(1);
 
674
        ctl.opened=0;
 
675
    }
 
676
}
 
677
 
 
678
static int ctl_read(int32 *valp)
 
679
{
 
680
  int c;
 
681
  if (last_rc_command)
 
682
    {
 
683
        *valp = last_rc_arg;
 
684
        c = last_rc_command;
 
685
        last_rc_command = 0;
 
686
        return c;
 
687
    }
 
688
  while ((c=getch())!=ERR)
 
689
    {
 
690
      re_init_screen();
 
691
 
 
692
      switch(c)
 
693
        {
 
694
        case 'h':
 
695
        case '?':
 
696
        case KEY_F(1):
 
697
          ctl_help_mode();
 
698
          return RC_NONE;
 
699
          
 
700
        case 'V':
 
701
        case KEY_UP:
 
702
          *valp=10;
 
703
          return RC_CHANGE_VOLUME;
 
704
        case 'v':
 
705
        case KEY_DOWN:
 
706
          *valp=-10;
 
707
          return RC_CHANGE_VOLUME;
 
708
        case 'q':
 
709
        case KEY_END:
 
710
        case KEY_F(10):
 
711
          return RC_QUIT;
 
712
        case 'n':
 
713
        case KEY_NPAGE:
 
714
          return RC_NEXT;
 
715
        case 'p':
 
716
        case KEY_PPAGE:
 
717
          return RC_REALLY_PREVIOUS;
 
718
        case 'r':
 
719
        case KEY_HOME:
 
720
          return RC_RESTART;
 
721
 
 
722
        case 'f':
 
723
        case KEY_RIGHT:
 
724
         songoffset +=
 
725
          *valp=play_mode->rate;
 
726
          return RC_FORWARD;
 
727
        case 'b':
 
728
        case KEY_LEFT:
 
729
         songoffset -=
 
730
          *valp=play_mode->rate;
 
731
         if (songoffset<0) songoffset=0;
 
732
          return RC_BACK;
 
733
#ifdef KEY_RESIZE
 
734
        case KEY_RESIZE:
 
735
          getmaxyx(stdscr,maxy,maxx);
 
736
          draw_windows();
 
737
          ctl_reset();
 
738
          return RC_NONE;
 
739
#endif
 
740
        case KEY_F(2):
 
741
          current_interpolation = 0;
 
742
          screen_bugfix = 0;
 
743
          re_init_screen();
 
744
          return RC_NONE;
 
745
        case KEY_F(3):
 
746
          current_interpolation = 1;
 
747
          screen_bugfix = 0;
 
748
          re_init_screen();
 
749
          return RC_NONE;
 
750
        case KEY_F(4):
 
751
          current_interpolation = 2;
 
752
          screen_bugfix = 0;
 
753
          re_init_screen();
 
754
          return RC_NONE;
 
755
        case KEY_F(5):
 
756
          cfg_select = 0;
 
757
          screen_bugfix = 0;
 
758
          re_init_screen();
 
759
          return RC_PATCHCHANGE;
 
760
        case KEY_F(6):
 
761
          cfg_select = 1;
 
762
          screen_bugfix = 0;
 
763
          re_init_screen();
 
764
          return RC_PATCHCHANGE;
 
765
        case KEY_F(7):
 
766
          cfg_select = 2;
 
767
          screen_bugfix = 0;
 
768
          re_init_screen();
 
769
          return RC_PATCHCHANGE;
 
770
        case KEY_F(8):
 
771
          cfg_select = 3;
 
772
          screen_bugfix = 0;
 
773
          re_init_screen();
 
774
          return RC_PATCHCHANGE;
 
775
        case KEY_F(11):
 
776
          if (!opt_dry)
 
777
            {
 
778
                opt_dry = 1;
 
779
                slk_set(11, "Dry", 1);
 
780
            }
 
781
          else
 
782
            {
 
783
                opt_dry = 0;
 
784
                slk_set(11, "Wet", 1);
 
785
            }
 
786
          slk_refresh();
 
787
          return RC_NONE;
 
788
        case KEY_F(12):
 
789
          return RC_NONE;
 
790
          /* case ' ':
 
791
             return RC_TOGGLE_PAUSE; */
 
792
        }
 
793
    }
 
794
    re_init_screen();
 
795
 
 
796
  return RC_NONE;
 
797
}
 
798
 
 
799
static int cmsg(int type, int verbosity_level, const char *fmt, ...)
 
800
{
 
801
  va_list ap;
 
802
  int flagnl = 1;
 
803
  if ((type==CMSG_TEXT || type==CMSG_INFO || type==CMSG_WARNING) &&
 
804
      ctl.verbosity<verbosity_level)
 
805
    return 0;
 
806
  if (*fmt == '~')
 
807
   {
 
808
     flagnl = 0;
 
809
     fmt++;
 
810
   }
 
811
  va_start(ap, fmt);
 
812
  if (!ctl.opened)
 
813
    {
 
814
      vfprintf(stderr, fmt, ap);
 
815
      fprintf(stderr, "\n");
 
816
    }
 
817
  else if (ctl.trace_playing)
 
818
    {
 
819
      switch(type)
 
820
        {
 
821
          /* Pretty pointless to only have one line for messages, but... */
 
822
        case CMSG_WARNING:
 
823
        case CMSG_ERROR:
 
824
        case CMSG_FATAL:
 
825
          wmove(dftwin, 2,0);
 
826
          wclrtoeol(dftwin);
 
827
          if (has_colors()) set_color(dftwin, COLOR_YELLOW);
 
828
          else wattron(dftwin, A_REVERSE);
 
829
          vwprintw(dftwin, (char *)fmt, ap);
 
830
          if (has_colors()) unset_color(dftwin);
 
831
          else wattroff(dftwin, A_REVERSE);
 
832
          _ctl_refresh();
 
833
          if (type==CMSG_WARNING)
 
834
            sleep(1); /* Don't you just _HATE_ it when programs do this... */
 
835
          else
 
836
            sleep(2);
 
837
          wmove(dftwin, 2,0);
 
838
          wclrtoeol(dftwin);
 
839
          _ctl_refresh();
 
840
          break;
 
841
        }
 
842
    }
 
843
  else
 
844
    {
 
845
      switch(type)
 
846
        {
 
847
        default:
 
848
          vwprintw(msgwin, (char *)fmt, ap);
 
849
          if (flagnl) waddch(msgwin, '\n');
 
850
          break;
 
851
 
 
852
        case CMSG_WARNING:
 
853
          if (has_colors()) set_color(msgwin, COLOR_YELLOW);
 
854
          else wattron(msgwin, A_BOLD);
 
855
          vwprintw(msgwin, (char *)fmt, ap);
 
856
          if (has_colors()) unset_color(msgwin);
 
857
          else wattroff(msgwin, A_BOLD);
 
858
          waddch(msgwin, '\n');
 
859
          break;
 
860
          
 
861
        case CMSG_ERROR:
 
862
        case CMSG_FATAL:
 
863
          if (has_colors()) set_color(msgwin, COLOR_RED);
 
864
          else wattron(msgwin, A_REVERSE);
 
865
          vwprintw(msgwin, (char *)fmt, ap);
 
866
          if (has_colors()) unset_color(msgwin);
 
867
          else wattroff(msgwin, A_REVERSE);
 
868
          waddch(msgwin, '\n');
 
869
          if (type==CMSG_FATAL)
 
870
            {
 
871
              wrefresh(msgwin);
 
872
              sleep(2);
 
873
            }
 
874
          break;
 
875
        }
 
876
      wrefresh(msgwin);
 
877
    }
 
878
 
 
879
  va_end(ap);
 
880
  return 0;
 
881
}
 
882
 
 
883
#endif