gpfce patch part3
[fceu.git] / drivers / pc / sdl.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <pthread.h>
5
6 #include "sdl.h"
7 #include "sdl-video.h"
8 #ifdef NETWORK
9 #include "unix-netplay.h"
10 #endif
11 #include "minimal.h"
12 //extern int soundvol;
13 int CLImain(int argc, char *argv[]);
14 extern int gp2x_in_sound_thread;
15 extern void pthread_yield(void);
16 extern void SetVideoScaling(int, int, int);
17  
18 //#define SOUND_RATE 44100
19 #define SOUND_RATE 22050
20
21 DSETTINGS Settings;
22 CFGSTRUCT DriverConfig[]={
23         AC(_xscale),
24         AC(_yscale),
25         AC(_xscalefs),
26         AC(_yscalefs),
27         AC(_efx),
28         AC(_efxfs),
29         AC(_sound),
30         #ifdef DSPSOUND
31         AC(_f8bit),
32         #else
33         AC(_ebufsize),
34         AC(_lbufsize),
35         #endif
36         AC(_fullscreen),
37         AC(_xres),
38         AC(_yres),
39         ACA(joyBMap),
40         ACA(joyAMap),
41         ACA(joy),
42         //ACS(_fshack),
43         ENDCFGSTRUCT
44 };
45
46 //-fshack x       Set the environment variable SDL_VIDEODRIVER to \"x\" when
47 //                entering full screen mode and x is not \"0\".
48
49 char *DriverUsage=
50 "-xres   x       Set horizontal resolution to x for full screen mode.\n\
51 -yres   x       Set vertical resolution to x for full screen mode.\n\
52 -xscale(fs) x   Multiply width by x.\n\
53 -yscale(fs) x   Multiply height by x.\n\
54 -efx(fs) x      Enable scanlines effect if x is non zero.  yscale must be >=2\n\
55                 and preferably a multiple of 2.\n\
56 -fs      x      Select full screen mode if x is non zero.\n\
57 -joyx   y       Use joystick y as virtual joystick x.\n\
58 -sound x        Sound.\n\
59                  0 = Disabled.\n\
60                  Otherwise, x = playback rate.\n\
61 "
62 #ifdef DSPSOUND
63 "-f8bit x        Force 8-bit sound.\n\
64                  0 = Disabled.\n\
65                  1 = Enabled.\n\
66 "
67 #else
68 "-lbufsize x    Internal FCE Ultra sound buffer size. Size = 2^x samples.\n\
69 -ebufsize x     External SDL sound buffer size. Size = 2^x samples.\n\
70 "
71 #endif
72 "-connect s      Connect to server 's' for TCP/IP network play.\n\
73 -server         Be a host/server for TCP/IP network play.\n\
74 -netport x      Use TCP/IP port x for network play.";
75
76 #ifdef NETWORK
77 static int docheckie[2]={0,0};
78 #endif
79 ARGPSTRUCT DriverArgs[]={
80          {"-joy1",0,&joy[0],0},{"-joy2",0,&joy[1],0},
81          {"-joy3",0,&joy[2],0},{"-joy4",0,&joy[3],0},
82          {"-xscale",0,&_xscale,0},
83          {"-yscale",0,&_yscale,0},
84          {"-efx",0,&_efx,0},
85          {"-xscalefs",0,&_xscalefs,0},
86          {"-yscalefs",0,&_yscalefs,0},
87          {"-efxfs",0,&_efxfs,0},
88          {"-xres",0,&_xres,0},
89          {"-yres",0,&_yres,0},
90          {"-fs",0,&_fullscreen,0},
91          //{"-fshack",0,&_fshack,0x4001},
92          {"-sound",0,&_sound,0},
93          #ifdef DSPSOUND
94          {"-f8bit",0,&_f8bit,0},
95          #else
96          {"-lbufsize",0,&_lbufsize,0},
97          {"-ebufsize",0,&_ebufsize,0},
98          #endif
99          #ifdef NETWORK
100          {"-connect",&docheckie[0],&netplayhost,0x4001},
101          {"-server",&docheckie[1],0,0},
102          {"-netport",0,&Port,0},
103          #endif
104          {0,0,0,0}
105 };
106
107
108
109
110
111 void GetBaseDirectory(char *BaseDirectory)
112 {
113  char *ol;
114
115 #ifdef GP2X
116  ol="/mnt/sd/roms/nes";
117 #else
118  ol=getenv("HOME");
119 #endif
120  BaseDirectory[0]=0;
121  if(ol)
122  {
123   strncpy(BaseDirectory,ol,2047);
124   BaseDirectory[2047]=0;
125 #ifdef GP2X
126   strcat(BaseDirectory,"/fceultra");
127 #else
128   strcat(BaseDirectory,"/.fceultra");
129 #endif
130  }
131 }
132
133 static void SetDefaults(void)
134 {
135  _xres=320;
136  _yres=240;
137  _fullscreen=0;
138  _sound=SOUND_RATE; // 48000 wrong
139  #ifdef DSPSOUND
140  _f8bit=0;
141  #else
142  _lbufsize=10;
143  _ebufsize=8;
144  #endif
145  _xscale=_yscale=_xscalefs=_yscalefs=1;
146  _efx=_efxfs=0;
147  //_fshack=_fshacksave=0;
148  memset(joy,0,sizeof(joy));
149 }
150
151 void DoDriverArgs(void)
152 {
153         int x;
154
155         #ifdef BROKEN
156         if(_fshack)
157         {
158          if(_fshack[0]=='0')
159           if(_fshack[1]==0)
160           {
161            free(_fshack);
162            _fshack=0;
163           }
164         }
165         #endif
166
167         #ifdef NETWORK
168         if(docheckie[0])
169          netplay=2;
170         else if(docheckie[1])
171          netplay=1;
172
173         if(netplay)
174          FCEUI_SetNetworkPlay(netplay);
175         #endif
176
177         for(x=0;x<4;x++)
178          if(!joy[x]) 
179          {
180           memset(joyBMap[x],0,sizeof(joyBMap[0]));
181           memset(joyAMap[x],0,sizeof(joyAMap[0]));
182          }
183 }
184 int InitMouse(void)
185 {
186  return(0);
187 }
188 void KillMouse(void){}
189 void GetMouseData(uint32 *d)
190 {
191 #ifndef GP2X
192  int x,y;
193  uint32 t;
194
195  t=SDL_GetMouseState(&x,&y);
196  d[2]=0;
197  if(t&SDL_BUTTON(1))
198   d[2]|=1;
199  if(t&SDL_BUTTON(3))
200   d[2]|=2;
201  t=PtoV(x,y); 
202  d[0]=t&0xFFFF;
203  d[1]=(t>>16)&0xFFFF;
204 #endif
205 }
206
207 int InitKeyboard(void)
208 {
209  return(1);
210 }
211
212 int UpdateKeyboard(void)
213 {
214  return(1);
215 }
216
217 void KillKeyboard(void)
218 {
219
220 }
221
222 char *GetKeyboard(void)
223 {
224 #ifndef GP2X
225  SDL_PumpEvents();
226  return (char *)(SDL_GetKeyState(0));
227 #else
228  return NULL;
229 #endif
230 }
231 #include "unix-basedir.h"
232 extern int showfps;
233 extern int swapbuttons;
234
235 int main(int argc, char *argv[])
236 {
237
238         puts("Starting GPFCE - Port version 0.2 05-29-2006");           
239         puts("Based on FCE Ultra "VERSION_STRING"...");
240         puts("Ported by Zheng Zhu\n");
241 #ifdef GP2X
242          //  stereo
243          //gp2x_init (1000, 8, SOUND_RATE, 16, 1, 60);
244          
245          // mono 44khz
246         //gp2x_init (1000, 8, SOUND_RATE<<1, 16, 0, 60);
247          // mono 22khz
248         gp2x_init (1000, 8, SOUND_RATE, 16, 0, 60);
249        
250         SetDefaults();
251         int ret=CLImain(argc,argv);
252
253         // unscale the screen, in case this is bad.
254         SetVideoScaling(320, 320, 240);
255
256         gp2x_deinit();
257         // make sure sound thread has exited cleanly
258         while (gp2x_in_sound_thread) pthread_yield();
259         printf("Sound thread exited\n");
260         printf("Exiting main().  terminated");
261         if (showfps && swapbuttons)
262         {
263           execl("./selector","./selector","./gpfce_showfps_swapbuttons_config",NULL);
264         }
265         else if (showfps)
266         {
267           execl("./selector","./selector","./gpfce_showfps_config",NULL);
268         }
269         else if (swapbuttons)
270         {
271           execl("./selector","./selector","./gpfce_swapbuttons_config",NULL);
272         }
273         else
274         {
275           execl("./selector","./selector","./gpfce_config",NULL);
276         }
277         return(ret?0:-1);
278 #else
279         gp2x_init (1000, 8, SOUND_RATE, 16, 1, 60);
280         
281         if(SDL_Init(0))
282         {
283          printf("Could not initialize SDL: %s.\n", SDL_GetError());
284          return(-1);
285         }
286         SetDefaults();
287
288         #ifdef BROKEN
289         if(getenv("SDL_VIDEODRIVER"))
290         {
291          if((_fshacksave=malloc(strlen(getenv("SDL_VIDEODRIVER"))+1)))
292           strcpy(_fshacksave,getenv("SDL_VIDEODRIVER"));
293         }
294         else
295          _fshacksave=0;
296         #endif
297
298         {
299          int ret=CLImain(argc,argv);
300          SDL_Quit();
301          // make sure sound thread has exited cleanly
302          while (gp2x_in_sound_thread) pthread_yield();
303          printf("Sound thread exited\n");
304          printf("Exiting main().  terminated");
305          return(ret);
306         }
307 #endif
308 }
309