cmdline parser fix, missing init stuff
[fceu.git] / drivers / gp2x / main.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <signal.h>
22 #include <sys/time.h>
23 #include <sys/stat.h>
24 #include <string.h>
25 #include <strings.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "main.h"
31 #include "minimal.h"
32 #include "throttle.h"
33 #include "menu.h"
34 #include "gp2x.h"
35
36 #include "../common/config.h"
37 #include "../common/args.h"
38 #include "../common/unixdsp.h"
39 #include "../common/cheat.h"
40
41 #include "../../fce.h"
42 #include "../../cart.h"
43
44 #include "dface.h"
45
46
47 void CleanSurface(void);
48
49 // internals
50 extern char lastLoadedGameName[2048];
51 extern uint8 Exit; // exit emu loop flag
52 void CloseGame(void);
53
54 FCEUGI *fceugi = NULL;
55 int ntsccol=0,ntschue=-1,ntsctint=-1;
56 int soundvol=70;
57 int inited=0;
58
59 int srendlinev[2]={8,0};
60 int erendlinev[2]={231,239};
61 int srendline,erendline;
62
63
64 static char BaseDirectory[2048];
65
66 int eoptions=0;
67
68 static void DriverKill(void);
69 static int DriverInitialize(void);
70
71 static int gametype;
72 #include "input.c"
73
74 static void ParseGI(FCEUGI *gi)
75 {
76  gametype=gi->type;
77
78  InputType[0]=UsrInputType[0];
79  InputType[1]=UsrInputType[1];
80  InputTypeFC=UsrInputTypeFC;
81
82  if(gi->input[0]>=0)
83   InputType[0]=gi->input[0];
84  if(gi->input[1]>=0)
85   InputType[1]=gi->input[1];
86  if(gi->inputfc>=0)
87   InputTypeFC=gi->inputfc;
88 }
89
90 void FCEUD_PrintError(char *s)
91 {
92  puts(s);
93 }
94
95 char *cpalette=0;
96 void LoadCPalette(void)
97 {
98  char tmpp[192];
99  FILE *fp;
100
101  if(!(fp=fopen(cpalette,"rb")))
102  {
103   printf(" Error loading custom palette from file: %s\n",cpalette);
104   free(cpalette);
105   cpalette=0;
106   return;
107  }
108  fread(tmpp,1,192,fp);
109  FCEUI_SetPaletteArray((uint8 *)tmpp);
110  fclose(fp);
111 }
112
113 static CFGSTRUCT fceuconfig[]={
114         AC(soundvol),
115         ACS(cpalette),
116         AC(ntsctint),
117         AC(ntschue),
118         AC(ntsccol),
119         AC(UsrInputTypeFC),
120         ACA(UsrInputType),
121         AC(powerpadside),
122         AC(powerpadsc),
123         AC(eoptions),
124         ACA(srendlinev),
125         ACA(erendlinev),
126         ADDCFGSTRUCT(DriverConfig),
127         ENDCFGSTRUCT
128 };
129
130 void SaveConfig(const char *name)
131 {
132         char tdir[2048];
133         if (name)
134              sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
135         else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
136         FCEUI_GetNTSCTH(&ntsctint, &ntschue);
137         SaveFCEUConfig(tdir,fceuconfig);
138 }
139
140 static void LoadConfig(const char *name)
141 {
142         char tdir[2048];
143         if (name)
144              sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
145         else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
146         FCEUI_GetNTSCTH(&ntsctint, &ntschue); /* Get default settings for if no config file exists. */
147         LoadFCEUConfig(tdir,fceuconfig);
148         FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
149 }
150
151 static void LoadLLGN(void)
152 {
153  char tdir[2048];
154  FILE *f;
155  int len;
156  sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
157  f=fopen(tdir, "r");
158  if(f)
159  {
160   len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f);
161   lastLoadedGameName[len] = 0;
162   fclose(f);
163  }
164 }
165
166 static void SaveLLGN(void)
167 {
168  // save last loaded game name
169  if (lastLoadedGameName[0])
170  {
171   char tdir[2048];
172   FILE *f;
173   sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
174   f=fopen(tdir, "w");
175   if(f)
176   {
177    fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f);
178    fclose(f);
179    sync();
180   }
181  }
182 }
183
184
185 static void CreateDirs(void)
186 {
187  char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg","pal"};
188  char tdir[2048];
189  int x;
190
191  mkdir(BaseDirectory,S_IRWXU);
192  for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
193  {
194   sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
195   mkdir(tdir,S_IRWXU);
196  }
197 }
198
199 static void SetSignals(void (*t)(int))
200 {
201   int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
202   int x;
203   for(x=0;x<11;x++)
204    signal(sigs[x],t);
205 }
206
207 static void CloseStuff(int signum)
208 {
209         DriverKill();
210         printf("\nSignal %d has been caught and dealt with...\n",signum);
211         switch(signum)
212         {
213          case SIGINT:printf("How DARE you interrupt me!\n");break;
214          case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break;
215          case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
216          case SIGPIPE:printf("The pipe has broken!  Better watch out for floods...\n");break;
217          case SIGSEGV:printf("Iyeeeeeeeee!!!  A segmentation fault has occurred.  Have a fluffy day.\n");break;
218          #if(SIGBUS!=SIGSEGV)
219          case SIGBUS:printf("I told you to be nice to the driver.\n");break;
220          #endif
221          case SIGFPE:printf("Those darn floating points.  Ne'er know when they'll bite!\n");break;
222          case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
223          case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
224          case SIGUSR1:
225          case SIGUSR2:printf("Killing your processes is not nice.\n");break;
226         }
227         exit(1);
228 }
229
230 static int DoArgs(int argc, char *argv[])
231 {
232         static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
233         static int cortabi[5]={SI_NONE,SI_GAMEPAD,
234                                SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
235         static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"};
236         static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW,
237                                  SIFC_4PLAYER,SIFC_FKB};
238
239         int x, ret;
240         static char *inputa[2]={0,0};
241         static char *fcexp=0;
242         static int docheckie[4];
243
244         static ARGPSTRUCT FCEUArgs[]={
245          {"-soundvol",0,&soundvol,0},
246          {"-cpalette",0,&cpalette,0x4001},
247
248          {"-ntsccol",0,&ntsccol,0},
249          {"-pal",&docheckie[0],0,0},
250          {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
251          {"-fcexp",0,&fcexp,0x4001},
252
253          {"-gg",0,&eoptions,0x8000|EO_GG},
254          {"-no8lim",0,&eoptions,0x8000|EO_NO8LIM},
255          {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
256          {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
257          {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
258          {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
259          {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
260          {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
261          {0,(void *)DriverArgs,0,0},
262          {0,0,0,0}
263         };
264
265         memset(docheckie,0,sizeof(docheckie));
266         ret=ParseArguments(argc, argv, FCEUArgs);
267         if(cpalette)
268         {
269          if(cpalette[0]=='0')
270           if(cpalette[1]==0)
271           {
272            free(cpalette);
273            cpalette=0;
274           }
275         }
276         if(docheckie[0])
277          Settings.region_force=2;
278         FCEUI_SetGameGenie(eoptions&EO_GG);
279         FCEUI_DisableSpriteLimitation(eoptions&EO_NO8LIM);
280         FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
281
282         for(x=0;x<2;x++)
283         {
284          if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
285          if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
286         }
287
288         FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
289         FCEUI_SetSoundVolume(soundvol);
290         DoDriverArgs();
291
292         if(fcexp)
293         {
294          int y;
295          for(y=0;y<5;y++)
296          {
297           if(!strncmp(fccortab[y],fcexp,8))
298           {
299            UsrInputTypeFC=fccortabi[y];
300            break;
301           }
302          }
303          free(fcexp);
304         }
305         for(x=0;x<2;x++)
306         {
307          int y;
308
309          if(!inputa[x])
310           continue;
311
312          for(y=0;y<5;y++)
313          {
314           if(!strncmp(cortab[y],inputa[x],8))
315           {
316            UsrInputType[x]=cortabi[y];
317            if(y==3)
318            {
319             powerpadside&=~(1<<x);
320             powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
321            }
322            free(inputa[x]);
323           }
324          }
325         }
326         return ret;
327 }
328
329
330 #include "usage.h"
331
332 int CLImain(int argc, char *argv[])
333 {
334         int last_arg_parsed;
335         /* TODO if(argc<=1)
336         {
337          ShowUsage(argv[0]);
338          return 1;
339         }*/
340
341         if(!DriverInitialize())
342         {
343          return 1;
344         }
345
346         if(!FCEUI_Initialize())
347          return(1);
348         GetBaseDirectory(BaseDirectory);
349         FCEUI_SetBaseDirectory(BaseDirectory);
350         lastLoadedGameName[0] = 0;
351
352         CreateDirs();
353         LoadConfig(NULL);
354         last_arg_parsed=DoArgs(argc-1,&argv[1]);
355         gp2x_opt_setup();
356         gp2x_cpuclock_gamma_update();
357         LoadLLGN();
358         FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
359         if(cpalette)
360          LoadCPalette();
361         if(InitSound())
362          inited|=1;
363
364         if (argc > 1 && !last_arg_parsed)
365         {
366          strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
367          lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
368          Exit = 0;
369         }
370         else
371         {
372          Exit = 1;
373         }
374
375         while (1)
376         {
377          if(!Exit)
378          {
379           if (fceugi)
380            CloseGame();
381           fceugi=FCEUI_LoadGame(lastLoadedGameName);
382           if (fceugi)
383           {
384            char infostring[32];
385            LoadConfig(lastLoadedGameName);
386            if (Settings.region_force)
387             FCEUI_SetVidSystem(Settings.region_force - 1);
388            ParseGI(fceugi);
389            InitOtherInput();
390
391            if ((eoptions&EO_GG) && geniestage == 0) {
392             strcpy(infostring, "gg.rom is missing, GG disabled");
393             eoptions&=~EO_GG;
394             FCEUI_SetGameGenie(0);
395            } else
396             GameInterface(GI_INFOSTRING, infostring);
397            FCEU_DispMessage("%s", infostring);
398           }
399           else
400           {
401            switch(LoadGameLastError) {
402             default: strcpy(menuErrorMsg, "failed to load ROM"); break;
403             case  2: strcpy(menuErrorMsg, "Can't find a ROM for movie"); break;
404             case 10: strcpy(menuErrorMsg, "FDS BIOS ROM is missing, read docs"); break;
405             case 11: strcpy(menuErrorMsg, "Error reading auxillary FDS file"); break;
406            }
407           }
408          }
409          if(Exit || !fceugi)
410          {
411           int ret;
412           ret = gp2x_menu_do();
413           if (ret == 1) break;          // exit emu
414           if (ret == 2) {               // reload ROM
415            Exit = 0;
416            continue;
417           }
418          }
419
420          PrepareOtherInput();
421          RefreshThrottleFPS();
422          FCEUI_GetCurrentVidSystem(&srendline,&erendline);
423          gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
424          switch (Settings.scaling & 3) {
425                  case 0: gp2x_video_set_offs(0);  gp2x_video_RGB_setscaling(0, 320, 240); break;
426                  case 1: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(0, 256, 240); break;
427                  case 2: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(srendline, 256, erendline-srendline); break;
428                  case 3: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(0, 320, 240); break;
429          }
430          CleanSurface();
431          gp2x_start_sound(Settings.sound_rate, 16, 0);
432          FCEUI_Emulate();
433         }
434
435         if (fceugi)
436          CloseGame();
437
438         SaveLLGN();
439         DriverKill();
440         return 0;
441 }
442
443 static int DriverInitialize(void)
444 {
445    SetSignals((void *)CloseStuff);
446
447    if(!InitVideo()) return 0;
448    inited|=4;
449    return 1;
450 }
451
452 static void DriverKill(void)
453 {
454  // SaveConfig(NULL); // done explicitly in menu now
455  SetSignals(SIG_DFL);
456
457  if(cpalette) free(cpalette);
458  cpalette=0;
459
460  if(inited&4)
461   KillVideo();
462  if(inited&1)
463   KillSound();
464  inited=0;
465 }
466
467 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
468 {
469  if(!(eoptions&EO_NOTHROTTLE))
470  {
471   if(Count)
472    WriteSound(Buffer,Count);
473   SpeedThrottle();
474  }
475  FCEUD_UpdateInput();   // must update input before blitting because of save confirmation stuff
476  BlitScreen(xbuf);
477 }
478