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