63a2501f3ab6ccf1b02f6a40c7830d295ee72a38
[clinton/Virtual-Jaguar-Rx.git] / src / gui / app.cpp
1 //
2 // app.cpp - Qt-based GUI for Virtual Jaguar
3 //
4 // by James Hammons
5 // (C) 2010 Underground Software
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 // JPM = Jean-Paul Mari <djipi.mari@gmail.com>
9 //
10 // Who When What
11 // --- ---------- -----------------------------------------------------------
12 // JLH 12/23/2009 Created this file
13 // JLH 01/21/2011 Added SDL initialization
14 // JLH 06/26/2011 Added fix to keep SDL from hijacking main() on win32
15 // JLH 05/24/2012 Added option switches
16 // JLH 03/05/2013 Fixed console redirection on win32 platform :-P
17 // JPM Sept./2016 Visual Studio support, and Soft debugger support (--debugger)
18 // JPM 09/ /2017 Added option (--dram-max) to support 8MB ram (which doesn't exist)
19 // JPM Sept./2017 Added the 'Rx' word to the emulator name, updated the credits line, added option (--es-all, --es-ui, --es-alpine & --es-debugger) to support the erase settings
20 // JPM Oct./2018 Added the Rx version's contact in the help text, added timer initialisation in the SDL_Init
21 //
22
23 #include "app.h"
24
25 #include <SDL.h>
26 #include <QApplication>
27 #include "gamepad.h"
28 #include "log.h"
29 #include "mainwin.h"
30 #include "profile.h"
31 #include "settings.h"
32 #include "version.h"
33 #include "debugger/DBGManager.h"
34
35 // Apparently on win32, SDL is hijacking main from Qt. So let's do this:
36 #if defined (__GCCWIN32__) || defined (_MSC_VER)
37 #undef main
38 #include <windows.h> // Ick, but needed for console redirection on win32 :-O
39 #endif
40
41 // Function prototypes...
42 static bool ParseCommandLine(int argc, char * argv[]);
43 static void ParseOptions(int argc, char * argv[]);
44
45
46 //hm. :-/
47 // This is stuff we pass into the mainWindow...
48 // Also, these are defaults. :-)
49 bool noUntunedTankPlease = false;
50 bool loadAndGo = false;
51 bool useLogfile = false;
52 QString filename;
53
54 // Here's the main application loop--short and simple...
55 int main(int argc, char * argv[])
56 {
57 // Win32 console redirection, because MS and their band of super geniuses
58 // decided that nobody would ever launch an app from the command line. :-P
59 // [Unfortunately, this doesn't seem to work on Vista/7. :-(]
60 #if defined (__GCCWIN32__) || defined (_MSC_VER)
61 BOOL(WINAPI * AttachConsole)(DWORD dwProcessId);
62
63 AttachConsole = (BOOL (WINAPI *)(DWORD))GetProcAddress(LoadLibraryA("kernel32.dll"), "AttachConsole");
64
65 if (AttachConsole != NULL && AttachConsole(((DWORD)-1)))
66 {
67 if (_fileno(stdout) == -1)
68 freopen("CONOUT$", "wb", stdout);
69 if (_fileno(stderr) == -1)
70 freopen("CONOUT$", "wb", stderr);
71 if (_fileno(stdin) == -1)
72 freopen("CONIN$", "rb", stdin);
73
74 // Fix C++
75 std::ios::sync_with_stdio();
76 }
77 #endif
78
79 // Normally, this would be read in from the settings module... :-P
80 vjs.hardwareTypeAlpine = false;
81 vjs.softTypeDebugger = false;
82 vjs.DRAM_size = 0x200000;
83 // This is stuff we pass into the mainWindow...
84 // noUntunedTankPlease = false;
85
86 // Check for options that must be in place be constructing the App object
87 if (!ParseCommandLine(argc, argv))
88 {
89 return 0;
90 }
91
92 Q_INIT_RESOURCE(virtualjaguar); // This must the same name as the exe filename
93 //or is it the .qrc filename???
94 // This is so we can pass this stuff using signal/slot mechanism...
95 //this is left here to remind me not to try doing this again :-P
96 //ick int id = qRegisterMetaType<uint32>();
97
98 int retVal = -1; // Default is failure
99
100 if (useLogfile)
101 {
102 bool success = (bool)LogInit("./virtualjaguar.log"); // Init logfile
103
104 if (!success)
105 printf("Failed to open virtualjaguar.log for writing!\n");
106 }
107
108 // Set up SDL library
109 if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0)
110 {
111 WriteLog("VJ: Could not initialize the SDL library: %s\n", SDL_GetError());
112 }
113 else
114 {
115 WriteLog("VJ: SDL (joystick, audio) successfully initialized.\n");
116 DBGManager_Init();
117 App app(argc, argv); // Declare an instance of the application
118 Gamepad::AllocateJoysticks();
119 AutoConnectProfiles();
120 retVal = app.exec(); // And run it!
121 DBGManager_Close();
122 Gamepad::DeallocateJoysticks();
123
124 // Free SDL components last...!
125 SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_AUDIO);
126 SDL_Quit();
127 }
128
129 #if defined (__GCCWIN32__) || defined (_MSC_VER)
130 #if 0
131 fclose(ctt);
132 #endif
133 #endif
134 // Close logfile
135 LogDone();
136 return retVal;
137 }
138
139
140 //
141 // Main app constructor--we stick globally accessible stuff here... (?)
142 //
143 App::App(int & argc, char * argv[]): QApplication(argc, argv)
144 {
145 bool loadAndGo = !filename.isEmpty();
146
147 mainWindow = new MainWin(loadAndGo);
148 mainWindow->plzDontKillMyComputer = noUntunedTankPlease;
149 // Override defaults with command line (if any)
150 ParseOptions(argc, argv);
151 mainWindow->SyncUI();
152
153 if (loadAndGo)
154 {
155 mainWindow->LoadFile(filename);
156
157 if (!mainWindow->cartridgeLoaded)
158 printf("Could not load file \"%s\"!\n", filename.toUtf8().data());
159 }
160
161 mainWindow->show();
162 }
163
164
165 //
166 // Here we parse out stuff that needs to be looked at *before* we construct the
167 // App object.
168 //
169 bool ParseCommandLine(int argc, char * argv[])
170 {
171 for(int i=1; i<argc; i++)
172 {
173 if ((strcmp(argv[i], "--help") == 0) || (strcmp(argv[i], "-h") == 0) || (strcmp(argv[i], "-?") == 0))
174 {
175 printf(
176 "Virtual Jaguar " VJ_RELEASE_VERSION " (" VJ_RELEASE_SUBVERSION ") Rx\n"
177 "Based upon Virtual Jaguar core v1.0.0 by David Raingeard.\n"
178 "Based upon the work by James Hammons (Linux/WIN32), Niels Wagenaar (Linux/WIN32),\n"
179 "Carwin Jones (BeOS), and Adam Green (MacOS)\n"
180 "Contact: http://sdlemu.ngemu.com/ | sdlemu@ngemu.com\n"
181 "Contact: https://github.com/djipi/Virtual-Jaguar-Rx | djipi.mari@gmail.com\n"
182 "\n"
183 "Usage:\n"
184 " virtualjaguar [<filename>] [switches]\n"
185 "\n"
186 " Option Description\n"
187 " ---------------- -----------------------------------\n"
188 " <filename> Name of file to autoload\n"
189 " --alpine -a Put Virtual Jaguar into Alpine mode\n"
190 " --debugger -d Put Virtual Jaguar into Debugger mode\n"
191 " --pal -p PAL mode\n"
192 " --ntsc -n NTSC mode\n"
193 " --dram-max Set DRAM size to 8MB\n"
194 " --bios -b Boot using Jaguar BIOS\n"
195 " --no-bios Do not use Jaguar BIOS\n"
196 " --gpu -g Enable GPU\n"
197 " --no-gpu Disable GPU\n"
198 " --dsp -d Enable DSP\n"
199 " --no-dsp Disable DSP\n"
200 " --fullscreen -f Start in full screen mode\n"
201 " --blur -B Enable GL bilinear filter\n"
202 " --no-blur Disable GL bilinear filtering\n"
203 " --log -l Create and use log file\n"
204 " --no-log Do not use log file (default)\n"
205 " --help -h Show this message\n"
206 " -? Show this message\n"
207 " --es-all Erase all settings\n"
208 " --es-ui Erase UI settings only\n"
209 " --es-alpine Erase alpine mode settings only\n"
210 " --es-debugger Erase debugger mode settings only\n"
211 " --please-dont-kill-my-computer\n"
212 " -z Run Virtual Jaguar without \"snow\"\n"
213 "\n"
214 "Invoking Virtual Jaguar with no filename will cause it to boot up\n"
215 "with the VJ GUI. Using Alpine mode will enable log file.\n"
216 "\n");
217 return false;
218 }
219
220 // Easter egg
221 if (strcmp(argv[i], "--yarrr") == 0)
222 {
223 printf("\n");
224 printf("Shiver me timbers!\n");
225 printf("\n");
226 return false;
227 }
228
229 // Erase settings
230 if (strstr(argv[i], "--es-"))
231 {
232 printf("\n");
233 if (EraseSettings(&argv[i][5]))
234 {
235 printf("Settings have been erased");
236 }
237 else
238 {
239 printf("No requested settings have been found");
240 }
241 printf("\n\n");
242 return false;
243 }
244
245 // Alpine/Debug mode
246 if ((strcmp(argv[i], "--alpine") == 0) || (strcmp(argv[i], "-a") == 0))
247 {
248 printf("Alpine Mode enabled.\n");
249 vjs.hardwareTypeAlpine = true;
250 // We also enable logging as well :-)
251 useLogfile = true;
252 }
253
254 // Debugger mode
255 if ((strcmp(argv[i], "--debugger") == 0) || (strcmp(argv[i], "-d") == 0))
256 {
257 printf("Debugger mode enabled.\n");
258 vjs.softTypeDebugger = true;
259 }
260
261 // No snow display
262 if ((strcmp(argv[i], "--please-dont-kill-my-computer") == 0) || (strcmp(argv[i], "-z") == 0))
263 {
264 noUntunedTankPlease = true;
265 }
266
267 // Log file
268 if ((strcmp(argv[i], "--log") == 0) || (strcmp(argv[i], "-l") == 0))
269 {
270 printf("Log file enabled.\n");
271 useLogfile = true;
272 }
273
274 // No log file
275 if (strcmp(argv[i], "--no-log") == 0)
276 {
277 printf("Log file disabled.\n");
278 useLogfile = false;
279 }
280
281 // DRAM size max
282 if (strcmp(argv[i], "--dram-max") == 0)
283 {
284 printf("DRAM size set at 8 MBytes.\n");
285 vjs.DRAM_size = 0x800000;
286 }
287
288 // Check for filename
289 if (argv[i][0] != '-')
290 {
291 loadAndGo = true;
292 filename = argv[i];
293 }
294 }
295
296 return true;
297 }
298
299
300 //
301 // This is to override settings loaded from the config file.
302 // Note that settings set here will become the new defaults!
303 // (Not any more: Settings are only saved if the config dialog was OKed, or the toolbar buttons were pressed.)
304 //
305 void ParseOptions(int argc, char * argv[])
306 {
307 for(int i=1; i<argc; i++)
308 {
309 // PAL mode
310 if ((strcmp(argv[i], "--pal") == 0) || (strcmp(argv[i], "-p") == 0))
311 {
312 vjs.hardwareTypeNTSC = false;
313 }
314
315 // NTSC mode
316 if ((strcmp(argv[i], "--ntsc") == 0) || (strcmp(argv[i], "-n") == 0))
317 {
318 vjs.hardwareTypeNTSC = true;
319 }
320
321 // Boot with Bios
322 if ((strcmp(argv[i], "--bios") == 0) || (strcmp(argv[i], "-b") == 0))
323 {
324 vjs.useJaguarBIOS = true;
325 }
326
327 // No boot with Bios
328 if (strcmp(argv[i], "--no-bios") == 0)
329 {
330 vjs.useJaguarBIOS = false;
331 }
332
333 // GPU enable
334 if ((strcmp(argv[i], "--gpu") == 0) || (strcmp(argv[i], "-g") == 0))
335 {
336 vjs.GPUEnabled = true;
337 }
338
339 // GPU disable
340 if (strcmp(argv[i], "--no-gpu") == 0)
341 {
342 vjs.GPUEnabled = false;
343 }
344
345 // DSP enable
346 if ((strcmp(argv[i], "--dsp") == 0) || (strcmp(argv[i], "-d") == 0))
347 {
348 vjs.DSPEnabled = true;
349 vjs.audioEnabled = true;
350 }
351
352 // DSP disable
353 if (strcmp(argv[i], "--no-dsp") == 0)
354 {
355 vjs.DSPEnabled = false;
356 vjs.audioEnabled = false;
357 }
358
359 // Fullscreen mode
360 if ((strcmp(argv[i], "--fullscreen") == 0) || (strcmp(argv[i], "-f") == 0))
361 {
362 vjs.fullscreen = true;
363 }
364
365 // Enable GL bilinear filter
366 if ((strcmp(argv[i], "--blur") == 0) || (strcmp(argv[i], "-B") == 0))
367 {
368 vjs.glFilter = 1;
369 }
370
371 // Disable GL bilinear filter
372 if (strcmp(argv[i], "--no-blur") == 0)
373 {
374 vjs.glFilter = 0;
375 }
376 }
377 }
378
379 #if 0
380 bool useJoystick;
381 int32 joyport; // Joystick port
382 bool hardwareTypeNTSC; // Set to false for PAL
383 bool useJaguarBIOS;
384 bool GPUEnabled;
385 bool DSPEnabled;
386 bool usePipelinedDSP;
387 bool fullscreen;
388 bool useOpenGL;
389 uint32 glFilter;
390 bool hardwareTypeAlpine;
391 bool softTypeDebugger;
392 bool audioEnabled;
393 uint32 frameSkip;
394 uint32 renderType;
395 bool allowWritesToROM;
396
397 // Keybindings in order of U, D, L, R, C, B, A, Op, Pa, 0-9, #, *
398
399 uint32 p1KeyBindings[21];
400 uint32 p2KeyBindings[21];
401
402 // Paths
403
404 char ROMPath[MAX_PATH];
405 char jagBootPath[MAX_PATH];
406 char CDBootPath[MAX_PATH];
407 char EEPROMPath[MAX_PATH];
408 char alpineROMPath[MAX_PATH];
409 char absROMPath[MAX_PATH];
410 #endif
411