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 { "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 },
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" },
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 },
90 static unsigned short buttonNameToBit(const char *s) {
91 if (strcasecmp(s, "A") == 0) {
93 } else if (strcasecmp(s, "B") == 0) {
95 } else if (strcasecmp(s, "X") == 0) {
97 } else if (strcasecmp(s, "Y") == 0) {
99 } else if (strcasecmp(s, "L") == 0) {
101 } else if (strcasecmp(s, "R") == 0) {
103 } else if (strcasecmp(s, "UP") == 0) {
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;
116 DIE("Bad button name: %s\n", s);
120 static unsigned char actionNameToBit(const char *s) {
121 if (strcasecmp(s, "quit") == 0) {
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;
134 DIE("Bad action name: %s\n", s);
138 const char * S9xGetFilename(FileTypes file)
140 static char filename[PATH_MAX + 1];
157 case FILE_SCREENSHOT:
168 snprintf(filename, PATH_MAX, "%s.%s", basePath, ext);
172 const char * S9xGetQuickSaveFilename(unsigned int slot)
174 static char filename[PATH_MAX + 1];
175 snprintf(filename, PATH_MAX, "%s.frz.%u.gz", basePath, slot);
179 static void loadDefaults()
181 ZeroMemory(&Settings, sizeof(Settings));
182 ZeroMemory(&Config, sizeof(Config));
187 Config.quitting = false;
188 Config.enableAudio = true;
189 Config.fullscreen = false;
191 Config.hacksFile = 0;
192 Config.touchscreenInput = false;
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;
214 Settings.ForceTransparency = FALSE;
215 Settings.Transparency = FALSE;
216 Settings.SixteenBit = TRUE;
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;
226 Settings.ForcePAL = FALSE;
227 Settings.ForceNTSC = FALSE;
229 Settings.HacksEnabled = FALSE;
230 Settings.HacksFilter = FALSE;
232 Settings.HBlankStart = (256 * Settings.H_Max) / SNES_HCOUNTER_MAX;
234 Settings.AutoSaveDelay = 15*60; // Autosave each 15 minutes.
237 void S9xSetRomFile(const char * path)
244 romFile = strndup(path, PATH_MAX);
245 basePath = strdup(romFile);
247 // Truncate base path at the last '.' char
248 char * c = strrchr(basePath, '.');
250 if (strcasecmp(c, ".gz") == 0) {
251 // Ignore the .gz part when truncating
253 c = strrchr(basePath, '.');
263 static bool gotRomFile()
265 return romFile ? true : false;
268 static void loadConfig(poptContext optCon, const char * file)
272 const char ** newargv;
275 fp = fopen (file, "r");
277 fprintf(stderr, "Cannot open config file %s\n", file);
281 ret = poptConfigFileToString (fp, &out, 0);
283 DIE("Cannot parse config file %s. ret=%d\n", file, ret);
285 poptParseArgvString(out, &newargc, &newargv);
287 poptStuffArgs(optCon, newargv);
291 /* XXX: currently leaking newargv */
294 static void parseArgs(poptContext optCon)
297 unsigned char scancode = 0;
299 while ((rc = poptGetNextOpt(optCon)) > 0) {
303 Config.enableAudio = false;
306 Settings.DisplayFrameRate = TRUE;
309 Settings.SkipFrames = atoi(poptGetOptArg(optCon));
312 Config.fullscreen = true;
315 Settings.SixteenBit = TRUE;
316 Settings.Transparency = TRUE;
320 Config.scaler = strdup(poptGetOptArg(optCon));
323 Settings.ForcePAL = TRUE;
326 Settings.ForceNTSC = TRUE;
329 Settings.TurboMode = TRUE;
332 loadConfig(optCon, poptGetOptArg(optCon));
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;
341 // Enable mouse on second controller
342 Settings.ControllerOption = SNES_MOUSE;
346 Settings.SuperScope = TRUE;
347 Settings.ControllerOption = SNES_SUPERSCOPE;
350 Config.snapshotLoad = true;
351 Config.snapshotSave = true;
354 Settings.SoundPlaybackRate = atoi(poptGetOptArg(optCon));
357 Settings.SoundBufferSize = atoi(poptGetOptArg(optCon));
360 Config.touchscreenInput = true;
363 Config.touchscreenInput = true;
364 // TODO Touchscreen grid
367 Settings.HacksEnabled = TRUE;
368 Settings.HacksFilter = TRUE;
371 Settings.HacksEnabled = TRUE;
372 Settings.HacksFilter = FALSE;
375 scancode = atoi(poptGetOptArg(optCon));
378 Config.joypad1Mapping[scancode] |=
379 buttonNameToBit(poptGetOptArg(optCon));
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, const char ** argv)
412 poptGetContext("drnoksnes", argc, argv, optionsTable, 0);
413 poptSetOtherOptionHelp(optCon, "<rom>");
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);
423 // Command line parameters (including --conf args)
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);
435 poptFreeContext(optCon);
438 void S9xUnloadConfig()
448 if (Config.hacksFile) {
449 free(Config.hacksFile);
450 Config.hacksFile = 0;