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