using SDL for timekeeping
[drnoksnes] / platform / config.cpp
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <popt.h>
5
6 #include "platform.h"
7 #include "port.h"
8 #include "snes9x.h"
9 #include "display.h"
10 #include "hgw.h"
11
12 #define DIE(format, ...) do { \
13                 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
14                 fprintf(stderr, format "\n", ## __VA_ARGS__); \
15                 abort(); \
16         } while (0);
17
18 struct config Config;
19
20 /** Path to current rom file, with extension. */
21 static char * romFile;
22 /** Path to rom file, without extension.
23  *  Used as a simple optimization to S9xGetFilename
24  */
25 static char * basePath;
26
27 static struct poptOption commonOptionsTable[] = {
28         { "disable-audio", 'a', POPT_ARG_NONE, 0, 1,
29         "disable emulation and output of audio", 0 },
30         { "display-framerate", 'r', POPT_ARG_NONE, 0, 2,
31         "Show frames per second counter in lower left corner", 0 },
32         { "skip-frames", 's', POPT_ARG_INT, 0, 3,
33         "Render only 1 in every N frames", "NUM" },
34         { "fullscreen", 'f', POPT_ARG_NONE, 0, 4,
35         "Start in fullscreen mode", 0 },
36         { "transparency", 'y', POPT_ARG_NONE, 0, 5,
37         "Enable transparency effects (slower)", 0 },
38         { "hacks", 'h', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, 0, 6,
39         "Enable hacks (yes, speed-only, no)", "option" },
40         { "pal", 'p', POPT_ARG_NONE, 0, 7,
41         "Run in PAL mode", 0 },
42         { "ntsc", 'n', POPT_ARG_NONE, 0, 8,
43         "Run in NTSC mode", 0 },
44         { "turbo", 't', POPT_ARG_NONE, 0, 9,
45         "Turbo mode (do not try to sleep between frames)", 0 },
46         { "conf", 'c', POPT_ARG_STRING, 0, 10,
47         "Extra configuration file to load", "FILE" },
48         { "mouse", 'm', POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, 0, 11,
49         "Enable mouse on controller NUM", "NUM"},
50         { "superscope", 'e', POPT_ARG_NONE, 0, 12,
51         "Enable SuperScope", 0},
52         { "snapshot", 'o', POPT_ARG_NONE, 0, 13,
53         "Unfreeze previous game on start and freeze game on exit", 0 },
54         { "audio-rate", 'u', POPT_ARG_INT, 0, 14,
55         "Audio output rate", "HZ" },
56         POPT_TABLEEND
57 };
58
59 static struct poptOption configOptionsTable[] = {
60         { "scancode", '\0', POPT_ARG_INT, 0, 100,
61         "Scancode to map", "CODE" },
62         { "button", '\0', POPT_ARG_STRING, 0, 101,
63         "SNES Button to press (A, B, X, Y, L, R, Up, Down, Left, Right)", "name" },
64         { "action", '\0', POPT_ARG_STRING, 0, 102,
65         "Emulator action to do (fullscreen, quit, ...)", "action" },
66         { "hacks-file", '\0', POPT_ARG_STRING, 0, 200,
67         "Path to snesadvance.dat file", "FILE" },
68         POPT_TABLEEND
69 };
70
71 static struct poptOption optionsTable[] = {
72         { 0, '\0', POPT_ARG_INCLUDE_TABLE, commonOptionsTable, 0,
73         "Common options", 0 },
74         { 0, '\0', POPT_ARG_INCLUDE_TABLE, configOptionsTable, 0,
75         "Configuration file options", 0 },
76         POPT_AUTOHELP
77         POPT_TABLEEND
78 };
79
80 static unsigned short buttonNameToBit(const char *s) {
81         if (strcasecmp(s, "A") == 0) {
82                 return SNES_A_MASK;
83         } else if (strcasecmp(s, "B") == 0) {
84                 return SNES_B_MASK;
85         } else if (strcasecmp(s, "X") == 0) {
86                 return SNES_X_MASK;
87         } else if (strcasecmp(s, "Y") == 0) {
88                 return SNES_Y_MASK;
89         } else if (strcasecmp(s, "L") == 0) {
90                 return SNES_TL_MASK;
91         } else if (strcasecmp(s, "R") == 0) {
92                 return SNES_TR_MASK;
93         } else if (strcasecmp(s, "UP") == 0) {
94                 return SNES_UP_MASK;
95         } else if (strcasecmp(s, "DOWN") == 0) {
96                 return SNES_DOWN_MASK;
97         } else if (strcasecmp(s, "LEFT") == 0) {
98                 return SNES_LEFT_MASK;
99         } else if (strcasecmp(s, "RIGHT") == 0) {
100                 return SNES_RIGHT_MASK;
101         } else if (strcasecmp(s, "START") == 0) {
102                 return SNES_START_MASK;
103         } else if (strcasecmp(s, "SELECT") == 0) {
104                 return SNES_SELECT_MASK;
105         } else {
106                 DIE("Bad button name: %s\n", s);
107         }
108 }
109
110 static unsigned char actionNameToBit(const char *s) {
111         if (strcasecmp(s, "quit") == 0) {
112                 return kActionQuit;
113         } else if (strcasecmp(s, "fullscreen") == 0) {
114                 return kActionToggleFullscreen;
115         } else {
116                 DIE("Bad action name: %s\n", s);
117         }
118 }
119
120 const char * S9xGetFilename(FileTypes file)
121 {
122         static char filename [PATH_MAX + 1];
123         const char * ext;
124         switch (file) {
125                 case FILE_ROM:
126                         return romFile;
127                 case FILE_SRAM:
128                         ext = "srm";
129                         break;
130                 case FILE_FREEZE:
131                         ext = "frz.gz";
132                         break;
133                 case FILE_CHT:
134                         ext = "cht";
135                         break;
136                 case FILE_IPS:
137                         ext = "ips";
138                         break;
139                 case FILE_SCREENSHOT:
140                         ext = "png";
141                         break;
142                 case FILE_SDD1_DAT:
143                         ext = "dat";
144                         break;
145                 default:
146                         ext = "???";
147                         break;
148         }
149
150         snprintf(filename, PATH_MAX, "%s.%s", basePath, ext);
151         return filename;
152 }
153
154 static void loadDefaults()
155 {
156         ZeroMemory(&Settings, sizeof(Settings));
157         ZeroMemory(&Config, sizeof(Config)); 
158         
159         romFile = 0;
160         basePath = 0;
161
162         Config.quitting = false;
163         Config.enableAudio = true;
164         Config.fullscreen = false;
165         Config.xsp = false;
166         Config.hacksFile = 0;
167
168         Settings.JoystickEnabled = FALSE;
169         Settings.SoundPlaybackRate = 22050;
170         Settings.Stereo = TRUE;
171         Settings.SoundBufferSize = 0;
172         Settings.CyclesPercentage = 100;
173         Settings.DisableSoundEcho = FALSE;
174         Settings.APUEnabled = FALSE;
175         Settings.H_Max = SNES_CYCLES_PER_SCANLINE;
176         Settings.SkipFrames = AUTO_FRAMERATE;
177         Settings.Shutdown = Settings.ShutdownMaster = TRUE;
178         Settings.FrameTimePAL = 20;     // in msecs
179         Settings.FrameTimeNTSC = 16;
180         Settings.FrameTime = Settings.FrameTimeNTSC;
181         Settings.DisableSampleCaching = FALSE;
182         Settings.DisableMasterVolume = FALSE;
183         Settings.Mouse = FALSE;
184         Settings.SuperScope = FALSE;
185         Settings.MultiPlayer5 = FALSE;
186         Settings.ControllerOption = SNES_JOYPAD;
187         
188         Settings.ForceTransparency = FALSE;
189         Settings.Transparency = FALSE;
190         Settings.SixteenBit = TRUE;
191         
192         Settings.SupportHiRes = FALSE;
193         Settings.NetPlay = FALSE;
194         Settings.ServerName [0] = 0;
195         Settings.AutoSaveDelay = 30;
196         Settings.ApplyCheats = FALSE;
197         Settings.TurboMode = FALSE;
198         Settings.TurboSkipFrames = 15;
199     
200     Settings.ForcePAL = FALSE;
201     Settings.ForceNTSC = FALSE;
202
203     Settings.HacksEnabled = FALSE;
204     Settings.HacksFilter = FALSE;
205
206         Settings.HBlankStart = (256 * Settings.H_Max) / SNES_HCOUNTER_MAX;
207
208         Settings.AutoSaveDelay = 15*60; // Autosave each 15 minutes.
209 }
210
211 void S9xSetRomFile(const char * path)
212 {
213         if (romFile) {
214                 free(romFile);
215                 free(basePath);
216         }
217
218         romFile = strndup(path, PATH_MAX);
219         basePath = strdup(romFile);
220
221         // Truncate base path at the last '.' char
222         char * c = strrchr(basePath, '.');
223         if (c) {
224                 *c = '\0';
225         }
226 }
227
228 static bool gotRomFile() 
229 {
230         return romFile ? true : false;
231 }
232
233 static void setHacks(const char * value)
234 {
235         // Unconditionally enable hacks even if no argument passed
236         Settings.HacksEnabled = TRUE;
237
238         if (!value) return;
239
240         if (strcasecmp(value, "speed-only") == 0 ||
241                 strcasecmp(value, "speed") == 0 ||
242                 strcasecmp(value, "s") == 0) {
243                         Settings.HacksFilter = TRUE;
244         } else if (strcasecmp(value, "yes") == 0 ||
245                 strcasecmp(value, "y") == 0) {
246                         // Do nothing
247         } else if (strcasecmp(value, "no") == 0 ||
248                 strcasecmp(value, "n") == 0) {
249                         Settings.HacksEnabled = FALSE;
250         } else {
251                 // Hack: the user probably wants to enable hacks
252                 // and use this argument as the ROM file.
253                 // Wonder why popt does not support this or if there's a better way.
254                 S9xSetRomFile(value);
255         }
256 }
257
258 static void loadConfig(poptContext optCon, const char * file)
259 {
260         char * out;
261         int newargc, ret;
262         const char ** newargv;
263         FILE * fp;
264
265         fp = fopen (file, "r");
266         if (!fp) {
267                 fprintf(stderr, "Cannot open config file %s\n", file);
268                 return;
269         }
270
271         ret = poptConfigFileToString (fp, &out, 0);
272         if (ret)
273             DIE("Cannot parse config file %s. ret=%d\n", file, ret);
274
275         poptParseArgvString(out, &newargc, &newargv);
276
277         poptStuffArgs(optCon, newargv);
278
279         free(out);
280         fclose(fp);
281         /* XXX: currently leaking newargv */
282 }
283
284 static void parseArgs(poptContext optCon)
285 {
286         int rc;
287         unsigned char scancode = 0;
288         
289         while ((rc = poptGetNextOpt(optCon)) > 0) {
290                 const char * val;
291                 switch (rc) {
292                         case 1:
293                                 Config.enableAudio = false;
294                                 break;
295                         case 2:
296                                 Settings.DisplayFrameRate = TRUE;
297                                 break;
298                         case 3:
299                                 Settings.SkipFrames = atoi(poptGetOptArg(optCon));
300                                 break;
301                         case 4:
302                                 Config.fullscreen = true;
303                                 break;
304                         case 5:
305                                 Settings.SixteenBit = TRUE;
306                                 Settings.Transparency = TRUE;
307                                 break;
308                         case 6:
309                                 Settings.HacksEnabled = TRUE;
310                                 setHacks(poptGetOptArg(optCon));
311                                 break;
312                         case 7:
313                                 Settings.ForcePAL = TRUE;
314                                 break;
315                         case 8:
316                                 Settings.ForceNTSC = TRUE;
317                                 break;
318                         case 9:
319                                 Settings.TurboMode = TRUE;
320                                 break;
321                         case 10:
322                                 loadConfig(optCon, poptGetOptArg(optCon));
323                                 break;
324                         case 11:
325                                 val = poptGetOptArg(optCon);
326                                 Settings.Mouse = TRUE;
327                                 if (!val || atoi(val) <= 1) {
328                                         // Enable mouse on first controller
329                                         Settings.ControllerOption = SNES_MOUSE_SWAPPED;
330                                 } else {
331                                         // Enable mouse on second controller
332                                         Settings.ControllerOption = SNES_MOUSE;
333                                 }
334                                 break;
335                         case 12:
336                                 Settings.SuperScope = TRUE;
337                                 Settings.ControllerOption = SNES_SUPERSCOPE;
338                                 break;
339                         case 13:
340                                 Config.snapshotLoad = true;
341                                 Config.snapshotSave = true;
342                                 break;
343                         case 14:
344                                 Settings.SoundPlaybackRate = atoi(poptGetOptArg(optCon));
345                                 break;
346                         case 100:
347                                 scancode = atoi(poptGetOptArg(optCon));
348                                 break;
349                         case 101:
350                                 Config.joypad1Mapping[scancode] |= 
351                                         buttonNameToBit(poptGetOptArg(optCon));
352                                 break;
353                         case 102:
354                                 Config.action[scancode] |= 
355                                         actionNameToBit(poptGetOptArg(optCon));
356                                 break;
357                         case 200:
358                                 free(Config.hacksFile);
359                                 Config.hacksFile = strdup(poptGetOptArg(optCon));
360                                 break;
361                         default:
362                                 DIE("Invalid popt argument (this is a bug): %d", rc);
363                                 break;
364                 }
365         }
366         
367         if (rc < -1) {
368                 /* an error occurred during option processing */
369                 fprintf(stderr, "%s: %s\n",
370                         poptBadOption(optCon, 0),
371                         poptStrerror(rc));
372                 exit(2);
373         }
374
375         /* if there's an extra unparsed arg it's our rom file */
376         const char * extra_arg = poptGetArg(optCon);
377         if (extra_arg) 
378                 S9xSetRomFile(extra_arg);
379 }
380
381 void S9xLoadConfig(int argc, const char ** argv)
382 {
383         poptContext optCon =
384                 poptGetContext("drnoksnes", argc, argv, optionsTable, 0);
385         poptSetOtherOptionHelp(optCon, "<rom>");
386
387         // Builtin defaults
388         loadDefaults();
389
390         // Read config file ~/apps/DrNokSnes.txt
391         char defConfFile[PATH_MAX];
392         sprintf(defConfFile, "%s/%s", getenv("HOME"), "apps/DrNokSnes.txt");
393         loadConfig(optCon, defConfFile);
394
395         // Command line parameters (including --conf args)
396         parseArgs(optCon);
397
398         if (!gotRomFile() && !hgwLaunched) {
399                 // User did not specify a ROM file, 
400                 // and we're not being launched from D-Bus.
401                 fprintf(stderr, "You need to specify a ROM, like this:\n");
402                 poptPrintUsage(optCon, stdout, 0);
403                 poptFreeContext(optCon); 
404                 exit(2);
405         }
406
407         poptFreeContext(optCon);
408 }
409
410 void S9xUnloadConfig()
411 {
412         if (romFile) {
413                 free(romFile);
414                 romFile = 0;
415         }
416         if (basePath) {
417                 free(basePath);
418                 basePath = 0;
419         }
420         if (Config.hacksFile) {
421                 free(Config.hacksFile);
422                 Config.hacksFile = 0;
423         }
424 }
425