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