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