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