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