12 #define DIE(format, ...) do { \
13 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
14 fprintf(stderr, format "\n", ## __VA_ARGS__); \
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
25 static char * basePath;
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 { "hacks", 'h', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, 0, 6,
39 "enable hacks (yes, speed-only, no)", "option" },
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 },
63 static struct poptOption configOptionsTable[] = {
64 { "scancode", '\0', POPT_ARG_INT, 0, 100,
65 "scancode to map", "CODE" },
66 { "button", '\0', POPT_ARG_STRING, 0, 101,
67 "SNES Button to press (A, B, X, Y, L, R, Up, Down, Left, Right)", "name" },
68 { "action", '\0', POPT_ARG_STRING, 0, 102,
69 "emulator action to do (fullscreen, quit, ...)", "action" },
70 { "hacks-file", '\0', POPT_ARG_STRING, 0, 200,
71 "path to snesadvance.dat file", "FILE" },
75 static struct poptOption optionsTable[] = {
76 { 0, '\0', POPT_ARG_INCLUDE_TABLE, commonOptionsTable, 0,
77 "Common options", 0 },
78 { 0, '\0', POPT_ARG_INCLUDE_TABLE, configOptionsTable, 0,
79 "Configuration file options", 0 },
84 static unsigned short buttonNameToBit(const char *s) {
85 if (strcasecmp(s, "A") == 0) {
87 } else if (strcasecmp(s, "B") == 0) {
89 } else if (strcasecmp(s, "X") == 0) {
91 } else if (strcasecmp(s, "Y") == 0) {
93 } else if (strcasecmp(s, "L") == 0) {
95 } else if (strcasecmp(s, "R") == 0) {
97 } else if (strcasecmp(s, "UP") == 0) {
99 } else if (strcasecmp(s, "DOWN") == 0) {
100 return SNES_DOWN_MASK;
101 } else if (strcasecmp(s, "LEFT") == 0) {
102 return SNES_LEFT_MASK;
103 } else if (strcasecmp(s, "RIGHT") == 0) {
104 return SNES_RIGHT_MASK;
105 } else if (strcasecmp(s, "START") == 0) {
106 return SNES_START_MASK;
107 } else if (strcasecmp(s, "SELECT") == 0) {
108 return SNES_SELECT_MASK;
110 DIE("Bad button name: %s\n", s);
114 static unsigned char actionNameToBit(const char *s) {
115 if (strcasecmp(s, "quit") == 0) {
117 } else if (strcasecmp(s, "fullscreen") == 0) {
118 return kActionToggleFullscreen;
120 DIE("Bad action name: %s\n", s);
124 const char * S9xGetFilename(FileTypes file)
126 static char filename [PATH_MAX + 1];
143 case FILE_SCREENSHOT:
154 snprintf(filename, PATH_MAX, "%s.%s", basePath, ext);
158 static void loadDefaults()
160 ZeroMemory(&Settings, sizeof(Settings));
161 ZeroMemory(&Config, sizeof(Config));
166 Config.quitting = false;
167 Config.enableAudio = true;
168 Config.fullscreen = false;
170 Config.hacksFile = 0;
171 Config.touchscreenInput = false;
173 Settings.JoystickEnabled = FALSE;
174 Settings.SoundPlaybackRate = 22050;
175 Settings.Stereo = TRUE;
176 Settings.SoundBufferSize = 512; // in samples
177 Settings.CyclesPercentage = 100;
178 Settings.DisableSoundEcho = FALSE;
179 Settings.APUEnabled = FALSE;
180 Settings.H_Max = SNES_CYCLES_PER_SCANLINE;
181 Settings.SkipFrames = AUTO_FRAMERATE;
182 Settings.Shutdown = Settings.ShutdownMaster = TRUE;
183 Settings.FrameTimePAL = 20; // in msecs
184 Settings.FrameTimeNTSC = 16;
185 Settings.FrameTime = Settings.FrameTimeNTSC;
186 Settings.DisableSampleCaching = FALSE;
187 Settings.DisableMasterVolume = FALSE;
188 Settings.Mouse = FALSE;
189 Settings.SuperScope = FALSE;
190 Settings.MultiPlayer5 = FALSE;
191 Settings.ControllerOption = SNES_JOYPAD;
193 Settings.ForceTransparency = FALSE;
194 Settings.Transparency = FALSE;
195 Settings.SixteenBit = TRUE;
197 Settings.SupportHiRes = FALSE;
198 Settings.NetPlay = FALSE;
199 Settings.ServerName [0] = 0;
200 Settings.AutoSaveDelay = 30;
201 Settings.ApplyCheats = FALSE;
202 Settings.TurboMode = FALSE;
203 Settings.TurboSkipFrames = 15;
205 Settings.ForcePAL = FALSE;
206 Settings.ForceNTSC = FALSE;
208 Settings.HacksEnabled = FALSE;
209 Settings.HacksFilter = FALSE;
211 Settings.HBlankStart = (256 * Settings.H_Max) / SNES_HCOUNTER_MAX;
213 Settings.AutoSaveDelay = 15*60; // Autosave each 15 minutes.
216 void S9xSetRomFile(const char * path)
223 romFile = strndup(path, PATH_MAX);
224 basePath = strdup(romFile);
226 // Truncate base path at the last '.' char
227 char * c = strrchr(basePath, '.');
229 if (strcasecmp(c, ".gz") == 0) {
230 // Ignore the .gz part when truncating
232 c = strrchr(basePath, '.');
242 static bool gotRomFile()
244 return romFile ? true : false;
247 static void setHacks(const char * value)
249 // Unconditionally enable hacks even if no argument passed
250 Settings.HacksEnabled = TRUE;
254 if (strcasecmp(value, "speed-only") == 0 ||
255 strcasecmp(value, "speed") == 0 ||
256 strcasecmp(value, "s") == 0) {
257 Settings.HacksFilter = TRUE;
258 } else if (strcasecmp(value, "yes") == 0 ||
259 strcasecmp(value, "y") == 0) {
261 } else if (strcasecmp(value, "no") == 0 ||
262 strcasecmp(value, "n") == 0) {
263 Settings.HacksEnabled = FALSE;
265 // Hack: the user probably wants to enable hacks
266 // and use this argument as the ROM file.
267 // Wonder why popt does not support this or if there's a better way.
268 S9xSetRomFile(value);
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;
323 Settings.HacksEnabled = TRUE;
324 setHacks(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 scancode = atoi(poptGetOptArg(optCon));
370 Config.joypad1Mapping[scancode] |=
371 buttonNameToBit(poptGetOptArg(optCon));
374 Config.action[scancode] |=
375 actionNameToBit(poptGetOptArg(optCon));
378 free(Config.hacksFile);
379 Config.hacksFile = strdup(poptGetOptArg(optCon));
382 DIE("Invalid popt argument (this is a bug): %d", rc);
388 /* an error occurred during option processing */
389 fprintf(stderr, "%s: %s\n",
390 poptBadOption(optCon, 0),
395 /* if there's an extra unparsed arg it's our rom file */
396 const char * extra_arg = poptGetArg(optCon);
398 S9xSetRomFile(extra_arg);
401 void S9xLoadConfig(int argc, const char ** argv)
404 poptGetContext("drnoksnes", argc, argv, optionsTable, 0);
405 poptSetOtherOptionHelp(optCon, "<rom>");
410 // Read config file ~/apps/DrNokSnes.txt
411 char defConfFile[PATH_MAX];
412 sprintf(defConfFile, "%s/%s", getenv("HOME"), "apps/DrNokSnes.txt");
413 loadConfig(optCon, defConfFile);
415 // Command line parameters (including --conf args)
418 if (!gotRomFile() && !hgwLaunched) {
419 // User did not specify a ROM file,
420 // and we're not being launched from D-Bus.
421 fprintf(stderr, "You need to specify a ROM, like this:\n");
422 poptPrintUsage(optCon, stdout, 0);
423 poptFreeContext(optCon);
427 poptFreeContext(optCon);
430 void S9xUnloadConfig()
440 if (Config.hacksFile) {
441 free(Config.hacksFile);
442 Config.hacksFile = 0;