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" },
59 static struct poptOption configOptionsTable[] = {
60 { "scancode", '\0', POPT_ARG_INT, 0, 100,
61 "Scancode to map", "CODE" },
62 { "button", '\0', POPT_ARG_STRING, 0, 101,
63 "SNES Button to press (A, B, X, Y, L, R, Up, Down, Left, Right)", "name" },
64 { "action", '\0', POPT_ARG_STRING, 0, 102,
65 "Emulator action to do (fullscreen, quit, ...)", "action" },
66 { "hacks-file", '\0', POPT_ARG_STRING, 0, 200,
67 "Path to snesadvance.dat file", "FILE" },
71 static struct poptOption optionsTable[] = {
72 { 0, '\0', POPT_ARG_INCLUDE_TABLE, commonOptionsTable, 0,
73 "Common options", 0 },
74 { 0, '\0', POPT_ARG_INCLUDE_TABLE, configOptionsTable, 0,
75 "Configuration file options", 0 },
80 static unsigned short buttonNameToBit(const char *s) {
81 if (strcasecmp(s, "A") == 0) {
83 } else if (strcasecmp(s, "B") == 0) {
85 } else if (strcasecmp(s, "X") == 0) {
87 } else if (strcasecmp(s, "Y") == 0) {
89 } else if (strcasecmp(s, "L") == 0) {
91 } else if (strcasecmp(s, "R") == 0) {
93 } else if (strcasecmp(s, "UP") == 0) {
95 } else if (strcasecmp(s, "DOWN") == 0) {
96 return SNES_DOWN_MASK;
97 } else if (strcasecmp(s, "LEFT") == 0) {
98 return SNES_LEFT_MASK;
99 } else if (strcasecmp(s, "RIGHT") == 0) {
100 return SNES_RIGHT_MASK;
101 } else if (strcasecmp(s, "START") == 0) {
102 return SNES_START_MASK;
103 } else if (strcasecmp(s, "SELECT") == 0) {
104 return SNES_SELECT_MASK;
106 DIE("Bad button name: %s\n", s);
110 static unsigned char actionNameToBit(const char *s) {
111 if (strcasecmp(s, "quit") == 0) {
113 } else if (strcasecmp(s, "fullscreen") == 0) {
114 return kActionToggleFullscreen;
116 DIE("Bad action name: %s\n", s);
120 const char * S9xGetFilename(FileTypes file)
122 static char filename [PATH_MAX + 1];
139 case FILE_SCREENSHOT:
150 snprintf(filename, PATH_MAX, "%s.%s", basePath, ext);
154 static void loadDefaults()
156 ZeroMemory(&Settings, sizeof(Settings));
157 ZeroMemory(&Config, sizeof(Config));
162 Config.quitting = false;
163 Config.enableAudio = true;
164 Config.fullscreen = false;
166 Config.hacksFile = 0;
168 Settings.JoystickEnabled = FALSE;
169 Settings.SoundPlaybackRate = 22050;
170 Settings.Stereo = TRUE;
171 Settings.SoundBufferSize = 0;
172 Settings.CyclesPercentage = 100;
173 Settings.DisableSoundEcho = FALSE;
174 Settings.APUEnabled = FALSE;
175 Settings.H_Max = SNES_CYCLES_PER_SCANLINE;
176 Settings.SkipFrames = AUTO_FRAMERATE;
177 Settings.Shutdown = Settings.ShutdownMaster = TRUE;
178 Settings.FrameTimePAL = 20; // in msecs
179 Settings.FrameTimeNTSC = 16;
180 Settings.FrameTime = Settings.FrameTimeNTSC;
181 Settings.DisableSampleCaching = FALSE;
182 Settings.DisableMasterVolume = FALSE;
183 Settings.Mouse = FALSE;
184 Settings.SuperScope = FALSE;
185 Settings.MultiPlayer5 = FALSE;
186 Settings.ControllerOption = SNES_JOYPAD;
188 Settings.ForceTransparency = FALSE;
189 Settings.Transparency = FALSE;
190 Settings.SixteenBit = TRUE;
192 Settings.SupportHiRes = FALSE;
193 Settings.NetPlay = FALSE;
194 Settings.ServerName [0] = 0;
195 Settings.AutoSaveDelay = 30;
196 Settings.ApplyCheats = FALSE;
197 Settings.TurboMode = FALSE;
198 Settings.TurboSkipFrames = 15;
200 Settings.ForcePAL = FALSE;
201 Settings.ForceNTSC = FALSE;
203 Settings.HacksEnabled = FALSE;
204 Settings.HacksFilter = FALSE;
206 Settings.HBlankStart = (256 * Settings.H_Max) / SNES_HCOUNTER_MAX;
208 Settings.AutoSaveDelay = 15*60; // Autosave each 15 minutes.
211 void S9xSetRomFile(const char * path)
218 romFile = strndup(path, PATH_MAX);
219 basePath = strdup(romFile);
221 // Truncate base path at the last '.' char
222 char * c = strrchr(basePath, '.');
228 static bool gotRomFile()
230 return romFile ? true : false;
233 static void setHacks(const char * value)
235 // Unconditionally enable hacks even if no argument passed
236 Settings.HacksEnabled = TRUE;
240 if (strcasecmp(value, "speed-only") == 0 ||
241 strcasecmp(value, "speed") == 0 ||
242 strcasecmp(value, "s") == 0) {
243 Settings.HacksFilter = TRUE;
244 } else if (strcasecmp(value, "yes") == 0 ||
245 strcasecmp(value, "y") == 0) {
247 } else if (strcasecmp(value, "no") == 0 ||
248 strcasecmp(value, "n") == 0) {
249 Settings.HacksEnabled = FALSE;
251 // Hack: the user probably wants to enable hacks
252 // and use this argument as the ROM file.
253 // Wonder why popt does not support this or if there's a better way.
254 S9xSetRomFile(value);
258 static void loadConfig(poptContext optCon, const char * file)
262 const char ** newargv;
265 fp = fopen (file, "r");
267 fprintf(stderr, "Cannot open config file %s\n", file);
271 ret = poptConfigFileToString (fp, &out, 0);
273 DIE("Cannot parse config file %s. ret=%d\n", file, ret);
275 poptParseArgvString(out, &newargc, &newargv);
277 poptStuffArgs(optCon, newargv);
281 /* XXX: currently leaking newargv */
284 static void parseArgs(poptContext optCon)
287 unsigned char scancode = 0;
289 while ((rc = poptGetNextOpt(optCon)) > 0) {
293 Config.enableAudio = false;
296 Settings.DisplayFrameRate = TRUE;
299 Settings.SkipFrames = atoi(poptGetOptArg(optCon));
302 Config.fullscreen = true;
305 Settings.SixteenBit = TRUE;
306 Settings.Transparency = TRUE;
309 Settings.HacksEnabled = TRUE;
310 setHacks(poptGetOptArg(optCon));
313 Settings.ForcePAL = TRUE;
316 Settings.ForceNTSC = TRUE;
319 Settings.TurboMode = TRUE;
322 loadConfig(optCon, poptGetOptArg(optCon));
325 val = poptGetOptArg(optCon);
326 Settings.Mouse = TRUE;
327 if (!val || atoi(val) <= 1) {
328 // Enable mouse on first controller
329 Settings.ControllerOption = SNES_MOUSE_SWAPPED;
331 // Enable mouse on second controller
332 Settings.ControllerOption = SNES_MOUSE;
336 Settings.SuperScope = TRUE;
337 Settings.ControllerOption = SNES_SUPERSCOPE;
340 Config.snapshotLoad = true;
341 Config.snapshotSave = true;
344 Settings.SoundPlaybackRate = atoi(poptGetOptArg(optCon));
347 scancode = atoi(poptGetOptArg(optCon));
350 Config.joypad1Mapping[scancode] |=
351 buttonNameToBit(poptGetOptArg(optCon));
354 Config.action[scancode] |=
355 actionNameToBit(poptGetOptArg(optCon));
358 free(Config.hacksFile);
359 Config.hacksFile = strdup(poptGetOptArg(optCon));
362 DIE("Invalid popt argument (this is a bug): %d", rc);
368 /* an error occurred during option processing */
369 fprintf(stderr, "%s: %s\n",
370 poptBadOption(optCon, 0),
375 /* if there's an extra unparsed arg it's our rom file */
376 const char * extra_arg = poptGetArg(optCon);
378 S9xSetRomFile(extra_arg);
381 void S9xLoadConfig(int argc, const char ** argv)
384 poptGetContext("drnoksnes", argc, argv, optionsTable, 0);
385 poptSetOtherOptionHelp(optCon, "<rom>");
390 // Read config file ~/apps/DrNokSnes.txt
391 char defConfFile[PATH_MAX];
392 sprintf(defConfFile, "%s/%s", getenv("HOME"), "apps/DrNokSnes.txt");
393 loadConfig(optCon, defConfFile);
395 // Command line parameters (including --conf args)
398 if (!gotRomFile() && !hgwLaunched) {
399 // User did not specify a ROM file,
400 // and we're not being launched from D-Bus.
401 fprintf(stderr, "You need to specify a ROM, like this:\n");
402 poptPrintUsage(optCon, stdout, 0);
403 poptFreeContext(optCon);
407 poptFreeContext(optCon);
410 void S9xUnloadConfig()
420 if (Config.hacksFile) {
421 free(Config.hacksFile);
422 Config.hacksFile = 0;