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