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;
193 Config.touchscreenShow = false;
195 Settings.JoystickEnabled = FALSE;
196 Settings.SoundPlaybackRate = 22050;
197 Settings.Stereo = TRUE;
198 Settings.SoundBufferSize = 512; // in samples
199 Settings.CyclesPercentage = 100;
200 Settings.DisableSoundEcho = FALSE;
201 Settings.APUEnabled = FALSE;
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.DisableSampleCaching = FALSE;
209 Settings.DisableMasterVolume = FALSE;
210 Settings.Mouse = FALSE;
211 Settings.SuperScope = FALSE;
212 Settings.MultiPlayer5 = FALSE;
213 Settings.ControllerOption = SNES_JOYPAD;
215 Settings.ForceTransparency = FALSE;
216 Settings.Transparency = FALSE;
217 Settings.SixteenBit = TRUE;
219 Settings.SupportHiRes = FALSE;
220 Settings.NetPlay = FALSE;
221 Settings.ServerName [0] = 0;
222 Settings.AutoSaveDelay = 30;
223 Settings.ApplyCheats = FALSE;
224 Settings.TurboMode = FALSE;
225 Settings.TurboSkipFrames = 15;
227 Settings.ForcePAL = FALSE;
228 Settings.ForceNTSC = FALSE;
230 Settings.HacksEnabled = FALSE;
231 Settings.HacksFilter = FALSE;
233 Settings.HBlankStart = (256 * Settings.H_Max) / SNES_HCOUNTER_MAX;
235 Settings.AutoSaveDelay = 15*60; // Autosave each 15 minutes.
238 void S9xSetRomFile(const char * path)
245 romFile = strndup(path, PATH_MAX);
246 basePath = strdup(romFile);
248 // Truncate base path at the last '.' char
249 char * c = strrchr(basePath, '.');
251 if (strcasecmp(c, ".gz") == 0) {
252 // Ignore the .gz part when truncating
254 c = strrchr(basePath, '.');
264 static bool gotRomFile()
266 return romFile ? true : false;
269 static void loadConfig(poptContext optCon, const char * file)
273 const char ** newargv;
276 fp = fopen (file, "r");
278 fprintf(stderr, "Cannot open config file %s\n", file);
282 ret = poptConfigFileToString (fp, &out, 0);
284 DIE("Cannot parse config file %s. ret=%d\n", file, ret);
286 poptParseArgvString(out, &newargc, &newargv);
288 poptStuffArgs(optCon, newargv);
292 /* XXX: currently leaking newargv */
295 static void parseArgs(poptContext optCon)
298 unsigned char scancode = 0;
300 while ((rc = poptGetNextOpt(optCon)) > 0) {
304 Config.enableAudio = false;
307 Settings.DisplayFrameRate = TRUE;
310 Settings.SkipFrames = atoi(poptGetOptArg(optCon));
313 Config.fullscreen = true;
316 Settings.SixteenBit = TRUE;
317 Settings.Transparency = TRUE;
321 Config.scaler = strdup(poptGetOptArg(optCon));
324 Settings.ForcePAL = TRUE;
327 Settings.ForceNTSC = TRUE;
330 Settings.TurboMode = TRUE;
333 loadConfig(optCon, poptGetOptArg(optCon));
336 val = poptGetOptArg(optCon);
337 Settings.Mouse = TRUE;
338 if (!val || atoi(val) <= 1) {
339 // Enable mouse on first controller
340 Settings.ControllerOption = SNES_MOUSE_SWAPPED;
342 // Enable mouse on second controller
343 Settings.ControllerOption = SNES_MOUSE;
347 Settings.SuperScope = TRUE;
348 Settings.ControllerOption = SNES_SUPERSCOPE;
351 Config.snapshotLoad = true;
352 Config.snapshotSave = true;
355 Settings.SoundPlaybackRate = atoi(poptGetOptArg(optCon));
358 Settings.SoundBufferSize = atoi(poptGetOptArg(optCon));
361 Config.touchscreenInput = true;
364 Config.touchscreenInput = true;
365 Config.touchscreenShow = true;
368 Settings.HacksEnabled = TRUE;
369 Settings.HacksFilter = TRUE;
372 Settings.HacksEnabled = TRUE;
373 Settings.HacksFilter = FALSE;
376 scancode = atoi(poptGetOptArg(optCon));
379 Config.joypad1Mapping[scancode] |=
380 buttonNameToBit(poptGetOptArg(optCon));
383 Config.action[scancode] |=
384 actionNameToBit(poptGetOptArg(optCon));
387 free(Config.hacksFile);
388 Config.hacksFile = strdup(poptGetOptArg(optCon));
391 DIE("Invalid popt argument (this is a bug): %d", rc);
397 /* an error occurred during option processing */
398 fprintf(stderr, "%s: %s\n",
399 poptBadOption(optCon, 0),
404 /* if there's an extra unparsed arg it's our rom file */
405 const char * extra_arg = poptGetArg(optCon);
407 S9xSetRomFile(extra_arg);
410 void S9xLoadConfig(int argc, const char ** argv)
413 poptGetContext("drnoksnes", argc, argv, optionsTable, 0);
414 poptSetOtherOptionHelp(optCon, "<rom>");
419 // Read config file ~/apps/DrNokSnes.txt
420 char defConfFile[PATH_MAX];
421 sprintf(defConfFile, "%s/%s", getenv("HOME"), "apps/DrNokSnes.txt");
422 loadConfig(optCon, defConfFile);
424 // Command line parameters (including --conf args)
427 if (!gotRomFile() && !hgwLaunched) {
428 // User did not specify a ROM file,
429 // and we're not being launched from D-Bus.
430 fprintf(stderr, "You need to specify a ROM, like this:\n");
431 poptPrintUsage(optCon, stdout, 0);
432 poptFreeContext(optCon);
436 poptFreeContext(optCon);
439 void S9xUnloadConfig()
449 if (Config.hacksFile) {
450 free(Config.hacksFile);
451 Config.hacksFile = 0;