multiple scaler 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 #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
194         Settings.JoystickEnabled = FALSE;
195         Settings.SoundPlaybackRate = 22050;
196         Settings.Stereo = TRUE;
197         Settings.SoundBufferSize = 512; // in samples
198         Settings.CyclesPercentage = 100;
199         Settings.DisableSoundEcho = FALSE;
200         Settings.APUEnabled = FALSE;
201         Settings.H_Max = SNES_CYCLES_PER_SCANLINE;
202         Settings.SkipFrames = AUTO_FRAMERATE;
203         Settings.Shutdown = Settings.ShutdownMaster = TRUE;
204         Settings.FrameTimePAL = 20;     // in msecs
205         Settings.FrameTimeNTSC = 16;
206         Settings.FrameTime = Settings.FrameTimeNTSC;
207         Settings.DisableSampleCaching = FALSE;
208         Settings.DisableMasterVolume = FALSE;
209         Settings.Mouse = FALSE;
210         Settings.SuperScope = FALSE;
211         Settings.MultiPlayer5 = FALSE;
212         Settings.ControllerOption = SNES_JOYPAD;
213         
214         Settings.ForceTransparency = FALSE;
215         Settings.Transparency = FALSE;
216         Settings.SixteenBit = TRUE;
217         
218         Settings.SupportHiRes = FALSE;
219         Settings.NetPlay = FALSE;
220         Settings.ServerName [0] = 0;
221         Settings.AutoSaveDelay = 30;
222         Settings.ApplyCheats = FALSE;
223         Settings.TurboMode = FALSE;
224         Settings.TurboSkipFrames = 15;
225     
226     Settings.ForcePAL = FALSE;
227     Settings.ForceNTSC = FALSE;
228
229     Settings.HacksEnabled = FALSE;
230     Settings.HacksFilter = FALSE;
231
232         Settings.HBlankStart = (256 * Settings.H_Max) / SNES_HCOUNTER_MAX;
233
234         Settings.AutoSaveDelay = 15*60; // Autosave each 15 minutes.
235 }
236
237 void S9xSetRomFile(const char * path)
238 {
239         if (romFile) {
240                 free(romFile);
241                 free(basePath);
242         }
243
244         romFile = strndup(path, PATH_MAX);
245         basePath = strdup(romFile);
246
247         // Truncate base path at the last '.' char
248         char * c = strrchr(basePath, '.');
249         if (c) {
250                 if (strcasecmp(c, ".gz") == 0) {
251                         // Ignore the .gz part when truncating
252                         *c = '\0';
253                         c = strrchr(basePath, '.');
254                         if (c) {
255                                 *c = '\0';
256                         }
257                 } else {
258                         *c = '\0';
259                 }
260         }
261 }
262
263 static bool gotRomFile() 
264 {
265         return romFile ? true : false;
266 }
267
268 static void loadConfig(poptContext optCon, const char * file)
269 {
270         char * out;
271         int newargc, ret;
272         const char ** newargv;
273         FILE * fp;
274
275         fp = fopen (file, "r");
276         if (!fp) {
277                 fprintf(stderr, "Cannot open config file %s\n", file);
278                 return;
279         }
280
281         ret = poptConfigFileToString (fp, &out, 0);
282         if (ret)
283             DIE("Cannot parse config file %s. ret=%d\n", file, ret);
284
285         poptParseArgvString(out, &newargc, &newargv);
286
287         poptStuffArgs(optCon, newargv);
288
289         free(out);
290         fclose(fp);
291         /* XXX: currently leaking newargv */
292 }
293
294 static void parseArgs(poptContext optCon)
295 {
296         int rc;
297         unsigned char scancode = 0;
298         
299         while ((rc = poptGetNextOpt(optCon)) > 0) {
300                 const char * val;
301                 switch (rc) {
302                         case 1:
303                                 Config.enableAudio = false;
304                                 break;
305                         case 2:
306                                 Settings.DisplayFrameRate = TRUE;
307                                 break;
308                         case 3:
309                                 Settings.SkipFrames = atoi(poptGetOptArg(optCon));
310                                 break;
311                         case 4:
312                                 Config.fullscreen = true;
313                                 break;
314                         case 5:
315                                 Settings.SixteenBit = TRUE;
316                                 Settings.Transparency = TRUE;
317                                 break;
318                         case 6:
319                                 free(Config.scaler);
320                                 Config.scaler = strdup(poptGetOptArg(optCon));
321                                 break;
322                         case 7:
323                                 Settings.ForcePAL = TRUE;
324                                 break;
325                         case 8:
326                                 Settings.ForceNTSC = TRUE;
327                                 break;
328                         case 9:
329                                 Settings.TurboMode = TRUE;
330                                 break;
331                         case 10:
332                                 loadConfig(optCon, poptGetOptArg(optCon));
333                                 break;
334                         case 11:
335                                 val = poptGetOptArg(optCon);
336                                 Settings.Mouse = TRUE;
337                                 if (!val || atoi(val) <= 1) {
338                                         // Enable mouse on first controller
339                                         Settings.ControllerOption = SNES_MOUSE_SWAPPED;
340                                 } else {
341                                         // Enable mouse on second controller
342                                         Settings.ControllerOption = SNES_MOUSE;
343                                 }
344                                 break;
345                         case 12:
346                                 Settings.SuperScope = TRUE;
347                                 Settings.ControllerOption = SNES_SUPERSCOPE;
348                                 break;
349                         case 13:
350                                 Config.snapshotLoad = true;
351                                 Config.snapshotSave = true;
352                                 break;
353                         case 14:
354                                 Settings.SoundPlaybackRate = atoi(poptGetOptArg(optCon));
355                                 break;
356                         case 15:
357                                 Settings.SoundBufferSize = atoi(poptGetOptArg(optCon));
358                                 break;
359                         case 16:
360                                 Config.touchscreenInput = true;
361                                 break;
362                         case 17:
363                                 Config.touchscreenInput = true;
364                                 // TODO  Touchscreen grid
365                                 break;
366                         case 18:
367                                 Settings.HacksEnabled = TRUE;
368                                 Settings.HacksFilter = TRUE;
369                                 break;
370                         case 19:
371                                 Settings.HacksEnabled = TRUE;
372                                 Settings.HacksFilter = FALSE;
373                                 break;
374                         case 100:
375                                 scancode = atoi(poptGetOptArg(optCon));
376                                 break;
377                         case 101:
378                                 Config.joypad1Mapping[scancode] |= 
379                                         buttonNameToBit(poptGetOptArg(optCon));
380                                 break;
381                         case 102:
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, const char ** argv)
410 {
411         poptContext optCon =
412                 poptGetContext("drnoksnes", argc, argv, optionsTable, 0);
413         poptSetOtherOptionHelp(optCon, "<rom>");
414
415         // Builtin defaults
416         loadDefaults();
417
418         // Read config file ~/apps/DrNokSnes.txt
419         char defConfFile[PATH_MAX];
420         sprintf(defConfFile, "%s/%s", getenv("HOME"), "apps/DrNokSnes.txt");
421         loadConfig(optCon, defConfFile);
422
423         // Command line parameters (including --conf args)
424         parseArgs(optCon);
425
426         if (!gotRomFile() && !hgwLaunched) {
427                 // User did not specify a ROM file, 
428                 // and we're not being launched from D-Bus.
429                 fprintf(stderr, "You need to specify a ROM, like this:\n");
430                 poptPrintUsage(optCon, stdout, 0);
431                 poptFreeContext(optCon); 
432                 exit(2);
433         }
434
435         poptFreeContext(optCon);
436 }
437
438 void S9xUnloadConfig()
439 {
440         if (romFile) {
441                 free(romFile);
442                 romFile = 0;
443         }
444         if (basePath) {
445                 free(basePath);
446                 basePath = 0;
447         }
448         if (Config.hacksFile) {
449                 free(Config.hacksFile);
450                 Config.hacksFile = 0;
451         }
452 }
453