15 #define DIE(format, ...) do { \
16 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
17 fprintf(stderr, format "\n", ## __VA_ARGS__); \
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
28 static char * basePath;
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 },
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" },
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 },
93 static unsigned short buttonNameToBit(const char *s) {
94 if (strcasecmp(s, "A") == 0) {
96 } else if (strcasecmp(s, "B") == 0) {
98 } else if (strcasecmp(s, "X") == 0) {
100 } else if (strcasecmp(s, "Y") == 0) {
102 } else if (strcasecmp(s, "L") == 0) {
104 } else if (strcasecmp(s, "R") == 0) {
106 } else if (strcasecmp(s, "UP") == 0) {
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;
119 DIE("Bad button name: %s\n", s);
123 static unsigned char actionNameToBit(const char *s) {
124 if (strcasecmp(s, "quit") == 0) {
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;
137 DIE("Bad action name: %s\n", s);
141 const char * S9xGetFilename(FileTypes file)
143 static char filename[PATH_MAX + 1];
160 case FILE_SCREENSHOT:
171 snprintf(filename, PATH_MAX, "%s.%s", basePath, ext);
175 const char * S9xGetQuickSaveFilename(unsigned int slot)
177 static char filename[PATH_MAX + 1];
178 snprintf(filename, PATH_MAX, "%s.frz.%u.gz", basePath, slot);
182 static void loadDefaults()
184 ZeroMemory(&Settings, sizeof(Settings));
185 ZeroMemory(&Config, sizeof(Config));
190 Config.quitting = false;
191 Config.enableAudio = true;
192 Config.fullscreen = false;
194 Config.hacksFile = 0;
195 Config.touchscreenInput = false;
196 Config.touchscreenShow = false;
198 Settings.JoystickEnabled = FALSE;
199 Settings.SoundPlaybackRate = 22050;
200 Settings.Stereo = TRUE;
201 Settings.SoundBufferSize = 512; // in samples
202 Settings.CyclesPercentage = 100;
203 Settings.DisableSoundEcho = FALSE;
204 Settings.APUEnabled = FALSE;
205 Settings.H_Max = SNES_CYCLES_PER_SCANLINE;
206 Settings.SkipFrames = AUTO_FRAMERATE;
207 Settings.Shutdown = Settings.ShutdownMaster = TRUE;
208 Settings.FrameTimePAL = 20; // in msecs
209 Settings.FrameTimeNTSC = 16;
210 Settings.FrameTime = Settings.FrameTimeNTSC;
211 Settings.DisableSampleCaching = FALSE;
212 Settings.DisableMasterVolume = FALSE;
213 Settings.Mouse = FALSE;
214 Settings.SuperScope = FALSE;
215 Settings.MultiPlayer5 = FALSE;
216 Settings.ControllerOption = SNES_JOYPAD;
218 Settings.ForceTransparency = FALSE;
219 Settings.Transparency = FALSE;
220 Settings.SixteenBit = TRUE;
222 Settings.SupportHiRes = FALSE;
223 Settings.NetPlay = FALSE;
224 Settings.ServerName [0] = 0;
225 Settings.AutoSaveDelay = 30;
226 Settings.ApplyCheats = FALSE;
227 Settings.TurboMode = FALSE;
228 Settings.TurboSkipFrames = 15;
230 Settings.ForcePAL = FALSE;
231 Settings.ForceNTSC = FALSE;
233 Settings.HacksEnabled = FALSE;
234 Settings.HacksFilter = FALSE;
236 Settings.HBlankStart = (256 * Settings.H_Max) / SNES_HCOUNTER_MAX;
238 Settings.AutoSaveDelay = 15*60; // Autosave each 15 minutes.
241 void S9xSetRomFile(const char * path)
248 romFile = strndup(path, PATH_MAX);
249 basePath = strdup(romFile);
251 // Truncate base path at the last '.' char
252 char * c = strrchr(basePath, '.');
254 if (strcasecmp(c, ".gz") == 0) {
255 // Ignore the .gz part when truncating
257 c = strrchr(basePath, '.');
267 static bool gotRomFile()
269 return romFile ? true : false;
272 static void loadConfig(poptContext optCon, const char * file)
276 const char ** newargv;
279 fp = fopen (file, "r");
281 fprintf(stderr, "Cannot open config file %s\n", file);
285 ret = poptConfigFileToString (fp, &out, 0);
287 DIE("Cannot parse config file %s. ret=%d\n", file, ret);
289 poptParseArgvString(out, &newargc, &newargv);
291 poptStuffArgs(optCon, newargv);
295 /* XXX: currently leaking newargv */
298 static void parseArgs(poptContext optCon)
301 unsigned char scancode = 0;
303 while ((rc = poptGetNextOpt(optCon)) > 0) {
307 Config.enableAudio = false;
310 Settings.DisplayFrameRate = TRUE;
313 Settings.SkipFrames = atoi(poptGetOptArg(optCon));
316 Config.fullscreen = true;
319 Settings.SixteenBit = TRUE;
320 Settings.Transparency = TRUE;
324 Config.scaler = strdup(poptGetOptArg(optCon));
327 Settings.ForcePAL = TRUE;
330 Settings.ForceNTSC = TRUE;
333 Settings.TurboMode = TRUE;
336 loadConfig(optCon, poptGetOptArg(optCon));
339 val = poptGetOptArg(optCon);
340 Settings.Mouse = TRUE;
341 if (!val || atoi(val) <= 1) {
342 // Enable mouse on first controller
343 Settings.ControllerOption = SNES_MOUSE_SWAPPED;
345 // Enable mouse on second controller
346 Settings.ControllerOption = SNES_MOUSE;
350 Settings.SuperScope = TRUE;
351 Settings.ControllerOption = SNES_SUPERSCOPE;
354 Config.snapshotLoad = true;
355 Config.snapshotSave = true;
358 Settings.SoundPlaybackRate = atoi(poptGetOptArg(optCon));
361 Settings.SoundBufferSize = atoi(poptGetOptArg(optCon));
364 Config.touchscreenInput = true;
367 Config.touchscreenInput = true;
368 Config.touchscreenShow = true;
371 Settings.HacksEnabled = TRUE;
372 Settings.HacksFilter = TRUE;
375 Settings.HacksEnabled = TRUE;
376 Settings.HacksFilter = FALSE;
379 scancode = atoi(poptGetOptArg(optCon));
382 Config.joypad1Mapping[scancode] |=
383 buttonNameToBit(poptGetOptArg(optCon));
386 Config.action[scancode] |=
387 actionNameToBit(poptGetOptArg(optCon));
390 free(Config.hacksFile);
391 Config.hacksFile = strdup(poptGetOptArg(optCon));
394 DIE("Invalid popt argument (this is a bug): %d", rc);
400 /* an error occurred during option processing */
401 fprintf(stderr, "%s: %s\n",
402 poptBadOption(optCon, 0),
407 /* if there's an extra unparsed arg it's our rom file */
408 const char * extra_arg = poptGetArg(optCon);
410 S9xSetRomFile(extra_arg);
413 void S9xLoadConfig(int argc, char ** argv)
415 poptContext optCon = poptGetContext("drnoksnes",
416 argc, const_cast<const char **>(argv), optionsTable, 0);
417 poptSetOtherOptionHelp(optCon, "<rom>");
422 // Read config file ~/.config/drnoksnes.conf
423 char defConfFile[PATH_MAX];
424 sprintf(defConfFile, "%s/%s", getenv("HOME"), ".config/drnoksnes.conf");
425 loadConfig(optCon, defConfFile);
427 // Command line parameters (including --conf args)
435 // User did not specify a ROM file in the command line
436 fprintf(stderr, "You need to specify a ROM, like this:\n");
437 poptPrintUsage(optCon, stdout, 0);
438 poptFreeContext(optCon);
443 poptFreeContext(optCon);
446 void S9xUnloadConfig()
456 if (Config.hacksFile) {
457 free(Config.hacksFile);
458 Config.hacksFile = 0;