14 #define DIE(format, ...) do { \
15 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
16 fprintf(stderr, format "\n", ## __VA_ARGS__); \
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
27 static char * basePath;
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 },
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" },
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 },
96 static unsigned short buttonNameToBit(const char *s) {
97 if (strcasecmp(s, "A") == 0) {
99 } else if (strcasecmp(s, "B") == 0) {
101 } else if (strcasecmp(s, "X") == 0) {
103 } else if (strcasecmp(s, "Y") == 0) {
105 } else if (strcasecmp(s, "L") == 0) {
107 } else if (strcasecmp(s, "R") == 0) {
109 } else if (strcasecmp(s, "UP") == 0) {
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;
122 DIE("Bad button name: %s\n", s);
126 static unsigned char actionNameToBit(const char *s) {
127 if (strcasecmp(s, "quit") == 0) {
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;
142 DIE("Bad action name: %s\n", s);
146 const char * S9xGetFilename(FileTypes file)
148 static char filename[PATH_MAX + 1];
165 case FILE_SCREENSHOT:
176 snprintf(filename, PATH_MAX, "%s.%s", basePath, ext);
180 const char * S9xGetQuickSaveFilename(unsigned int slot)
182 static char filename[PATH_MAX + 1];
183 snprintf(filename, PATH_MAX, "%s.frz.%u.gz", basePath, slot);
187 static void loadDefaults()
189 ZeroMemory(&Settings, sizeof(Settings));
190 ZeroMemory(&Config, sizeof(Config));
195 Config.enableAudio = true;
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;
210 Settings.ForceTransparency = FALSE; // We'll enable those later
211 Settings.Transparency = FALSE;
213 Settings.SupportHiRes = FALSE;
214 Settings.ApplyCheats = FALSE;
215 Settings.TurboMode = FALSE;
216 Settings.TurboSkipFrames = 15;
218 Settings.ForcePAL = FALSE;
219 Settings.ForceNTSC = FALSE;
221 Settings.HacksEnabled = FALSE;
222 Settings.HacksFilter = FALSE;
224 Settings.HBlankStart = (256 * Settings.H_Max) / SNES_HCOUNTER_MAX;
226 Settings.AutoSaveDelay = 15*60; // Autosave each 15 minutes.
229 void S9xSetRomFile(const char * path)
236 romFile = strndup(path, PATH_MAX);
237 basePath = strdup(romFile);
239 // Truncate base path at the last '.' char
240 char * c = strrchr(basePath, '.');
242 if (strcasecmp(c, ".gz") == 0) {
243 // Ignore the .gz part when truncating
245 c = strrchr(basePath, '.');
255 static bool gotRomFile()
257 return romFile ? true : false;
260 static void loadConfig(poptContext optCon, const char * file)
264 const char ** newargv;
267 fp = fopen (file, "r");
269 fprintf(stderr, "Cannot open config file %s\n", file);
273 ret = poptConfigFileToString (fp, &out, 0);
275 DIE("Cannot parse config file %s. ret=%d\n", file, ret);
277 poptParseArgvString(out, &newargc, &newargv);
279 poptStuffArgs(optCon, newargv);
283 /* XXX: currently leaking newargv */
286 static void parseArgs(poptContext optCon)
289 unsigned char scancode = 0;
291 while ((rc = poptGetNextOpt(optCon)) > 0) {
295 Config.enableAudio = false;
298 Settings.DisplayFrameRate = TRUE;
301 Settings.SkipFrames = atoi(poptGetOptArg(optCon));
304 Config.fullscreen = true;
307 Settings.Transparency = TRUE;
311 Config.scaler = strdup(poptGetOptArg(optCon));
314 Settings.ForcePAL = TRUE;
317 Settings.ForceNTSC = TRUE;
320 Settings.TurboMode = TRUE;
323 loadConfig(optCon, poptGetOptArg(optCon));
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;
332 // Enable mouse on second controller
333 Settings.ControllerOption = SNES_MOUSE;
337 Settings.SuperScope = TRUE;
338 Settings.ControllerOption = SNES_SUPERSCOPE;
341 Config.snapshotLoad = true;
342 Config.snapshotSave = true;
345 Settings.SoundPlaybackRate = atoi(poptGetOptArg(optCon));
348 Settings.SoundBufferSize = atoi(poptGetOptArg(optCon));
351 Config.touchscreenInput = 1;
354 Config.touchscreenInput = 1;
355 Config.touchscreenShow = true;
358 Settings.HacksEnabled = TRUE;
359 Settings.HacksFilter = TRUE;
362 Settings.HacksEnabled = TRUE;
363 Settings.HacksFilter = FALSE;
369 scancode = atoi(poptGetOptArg(optCon));
372 Config.joypad1Mapping[scancode] |=
373 buttonNameToBit(poptGetOptArg(optCon));
374 Config.joypad1Enabled = true;
377 Config.joypad2Mapping[scancode] |=
378 buttonNameToBit(poptGetOptArg(optCon));
379 Config.joypad2Enabled = true;
382 Config.action[scancode] |=
383 actionNameToBit(poptGetOptArg(optCon));
386 free(Config.hacksFile);
387 Config.hacksFile = strdup(poptGetOptArg(optCon));
390 DIE("Invalid popt argument (this is a bug): %d", rc);
396 /* an error occurred during option processing */
397 fprintf(stderr, "%s: %s\n",
398 poptBadOption(optCon, 0),
403 /* if there's an extra unparsed arg it's our rom file */
404 const char * extra_arg = poptGetArg(optCon);
406 S9xSetRomFile(extra_arg);
409 void S9xLoadConfig(int argc, char ** argv)
411 poptContext optCon = poptGetContext("drnoksnes",
412 argc, const_cast<const char **>(argv), optionsTable, 0);
413 poptSetOtherOptionHelp(optCon, "<rom>");
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);
423 // Command line parameters (including --conf args)
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);
439 poptFreeContext(optCon);
442 void S9xUnloadConfig()
452 if (Config.hacksFile) {
453 free(Config.hacksFile);
454 Config.hacksFile = 0;