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