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