File: cli.c

package info (click to toggle)
festalon 0.2.4-1
  • links: PTS
  • area: contrib
  • in suites: etch, etch-m68k, sarge
  • size: 448 kB
  • ctags: 986
  • sloc: ansic: 6,391; makefile: 68
file content (263 lines) | stat: -rw-r--r-- 6,527 bytes parent folder | download
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
/*  Festalon - NSF Player
 *  Copyright (C) 2002 Ben Parnell
 *
 *  This 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.
 *
 *  This library 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 this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <termios.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
#include <sys/time.h>

#include "../driver.h"
#ifdef DOS
#include <conio.h>
#include <pc.h>
#include "sb.h"
#else
#include "unixdsp.h"
#endif

static uint64 Stime,Ltime;
static uint64 BIGtime(void)
{
 uint64 r=0;
 struct timeval tv;
 if(! gettimeofday(&tv,0))
 {
  r=tv.tv_sec;
  r*=1000000;
  r+=tv.tv_usec;
 }
 return(r);
}

static int disabled=0;
static int current;
static int volume=200;
static NSF_HEADER *info;

static void ShowStatus(void)
{
  char chans[128];
  int maxos=5,x;

  chans[0]=0;
  if(info->SoundChip&1) maxos+=3;
  else if(info->SoundChip&2) maxos+=6;
  else if(info->SoundChip&4) maxos++;
  else if(info->SoundChip&8) maxos+=3;
  else if(info->SoundChip&0x10) maxos+=8;
  else if(info->SoundChip&0x20) maxos+=3;
  for(x=0;x<maxos;x++)
   sprintf(chans+strlen(chans),"%1d ",(disabled>>x)&1);
  #ifdef DOS
  gotoxy(1,wherey()-1);
  #else
  printf("\x1b[A");
  #endif
  Ltime=BIGtime();
  printf("Song:  %3d / %3d | %02d:%02d | Volume: %4d%% | %s\n",current,info->TotalSongs,(int)((Ltime-Stime)/60000000),(int)(((Ltime-Stime)/1000000)%60),volume,chans);
}
static void ShowInfo(NSF_HEADER *NSFHeader)
{
 int x;
 puts("NSF Loaded.  File information:\n");
 printf(" Name:       %s\n Artist:     %s\n Copyright:  %s\n",NSFHeader->SongName,NSFHeader->Artist,NSFHeader->Copyright);
 if(NSFHeader->SoundChip)
 {
  static char *tab[6]={"Konami VRCVI","Konami VRCVII","Nintendo FDS","Nintendo MMC5","Namco 106","Sunsoft FME-07"};
  for(x=0;x<6;x++)
   if(NSFHeader->SoundChip&(1<<x))
   {
    printf(" Expansion hardware:  %s\n",tab[x]);
    break;
   }
 }
 printf(" Speed:      %s\n\n",(NSFHeader->VideoSystem&1)?"PAL":"NTSC");
printf(" Load address:  $%04x\n Init address:  $%04x\n Play address:  $%04x\n\n",
NSFHeader->LoadAddressLow|(NSFHeader->LoadAddressHigh<<8),
NSFHeader->InitAddressLow|(NSFHeader->InitAddressHigh<<8),
NSFHeader->PlayAddressLow|(NSFHeader->PlayAddressHigh<<8));
 puts("");
// if(BSon)
//  puts(" Bank-switched.");
}

static volatile int eexit=0;
static int sa;
static struct termios oldtio,newtio;

static void siggo()
{
 eexit=1;
}

static void rmode()
{
 #ifndef DOS
 puts("\x1b[?25h");
 fcntl(fileno(stdin), F_SETFL, sa);
 #else
 _setcursortype(_NORMALCURSOR);
 #endif
 tcsetattr(0,TCSANOW,&oldtio);
}

static void badexit(int num)
{
 rmode();
 printf("Exiting on signal %d\n",num);
 exit(-1);
}

int main(int argc, char *argv[])
{
 int rate=96000;
 int size;
 char *buf;

 puts("\n*** Festalon v"VSTRING);
 if(argc<=1) 
 {
  printf("Usage:  %s file1.nsf file2.nsf ... fileN.nsf\n",argv[0]);
  return(-1);
 }
 tcgetattr(0,&oldtio);

 newtio=oldtio;
 newtio.c_lflag&=~(ICANON|ECHO);

 if(InitSound(&rate))
  FESTAI_Sound(rate);
 FESTAI_SetVolume(volume);
 FESTAI_Disable(disabled);

 #ifndef DOS
 {
  int x;
  for(x=0;x<_NSIG;x++) signal(x,badexit);
 }
 #endif
 signal(SIGTERM,siggo);
 signal(SIGINT,siggo);

 while(argc>1 && !eexit)
 {
  argv++;
  {
   FILE *fp;
   printf("\nLoading %s...",*argv);
   if(!(fp=fopen(*argv,"rb"))) {puts("Error opening file!");rmode();return(-1);}
   fseek(fp,0,SEEK_END);
   size=ftell(fp);
   fseek(fp,0,SEEK_SET);
   buf=malloc(size);
   fread(buf,1,size,fp); 
   fclose(fp);
  }
 
  if(!(info=FESTAI_Load(buf,size)))
  {
   puts("Not an NSF!");
   argc--;
   continue;
  }
  puts("");
  poosh();
  #ifndef DOS
  puts("\x1b[?25l");
  #endif
  ShowInfo(info);

 tcsetattr(0,TCSANOW,&newtio);
 #ifndef DOS
 sa = fcntl(fileno(stdin), F_GETFL);
 fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
 #else
 _setcursortype(_NOCURSOR);
 #endif
 current=FESTAI_NSFControl(0,0);
 Ltime=Stime=BIGtime();
 ShowStatus();
 while(!eexit)
 {
  static char t[3]={0,0,0};
  FESTAI_Emulate();
  if((BIGtime()-Ltime)/1000000) ShowStatus();
  #ifdef DOS
  while(kbhit())  
  {
   read(fileno(stdin),&t[0],1);
  #else
  while(read(fileno(stdin),&t[0],1)>=0)
  {
  #endif
    switch(tolower(t[0]))
    {
     case 10: poosh(); Stime=BIGtime();current=FESTAI_NSFControl(0,0);ShowStatus();break;
     case '`': poosh();break;
     case 'i':t[0]='1'+10;Stime=BIGtime();goto noyo;
     case 'o':t[0]='1'+11;Stime=BIGtime();goto noyo;
     case 'p':t[0]='1'+12;Stime=BIGtime();goto noyo;
     case '0':t[0]='1'+9;
     case '1' ... '9':noyo:disabled^=1<<(t[0]-'1');FESTAI_Disable(disabled);
		      poosh();ShowStatus();break;
     case '-': volume--; FESTAI_SetVolume(volume);poosh();ShowStatus();break;
     case '=': volume++; FESTAI_SetVolume(volume);poosh();ShowStatus();break;
     case 'q': goto exito;

     /* Alternate song selection keys.  Especially needed for DOS port. */
     case '\'':current=FESTAI_NSFControl(10,0);poosh();Stime=BIGtime();ShowStatus();break;
     case ';':current=FESTAI_NSFControl(-10,0);poosh();Stime=BIGtime();ShowStatus();break;
     case '.':current=FESTAI_NSFControl(1,0);poosh();Stime=BIGtime();ShowStatus();break;
     case ',':current=FESTAI_NSFControl(-1,0);poosh();Stime=BIGtime();ShowStatus();break;
    }
    #ifndef DOS
    if(t[2]==27 && t[1]==91)
     switch(t[0])
     {
      case 65:current=FESTAI_NSFControl(10,0);poosh();Stime=BIGtime();ShowStatus();break;
      case 66:current=FESTAI_NSFControl(-10,0);poosh();Stime=BIGtime();ShowStatus();break;
      case 67:current=FESTAI_NSFControl(1,0);poosh();Stime=BIGtime();ShowStatus();break;
      case 68:current=FESTAI_NSFControl(-1,0);poosh();Stime=BIGtime();ShowStatus();break;
     }
    #endif
    t[2]=t[1];
    t[1]=t[0];
  }

 }
 exito:
  argc--;
 }

 poosh();
 rmode();
 FESTAI_Close();
 KillSound();
 return(0);
}

int FESTAD_Update(int32 *Buffer, int Count)
{
 WriteSound(Buffer,Count);
 return(0);
}