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