]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/mainwin.cpp
Fixed DSP/audio options to be unambiguous and consistent.
[virtualjaguar] / src / gui / mainwin.cpp
1 //
2 // mainwin.cpp - Qt-based GUI for Virtual Jaguar: Main Application Window
3 // by James Hammons
4 // (C) 2009 Underground Software
5 //
6 // JLH = James Hammons <jlhamm@acm.org>
7 //
8 // Who  When        What
9 // ---  ----------  -------------------------------------------------------------
10 // JLH  12/23/2009  Created this file
11 // JLH  12/20/2010  Added settings, menus & toolbars
12 // JLH  07/05/2011  Added CD BIOS functionality to GUI
13 //
14
15 // FIXED:
16 //
17 // - Add dbl click/enter to select in cart list, ESC to dimiss [DONE]
18 // - Autoscan/autoload all available BIOS from 'software' folder [DONE]
19 // - Add 1 key jumping in cartridge list (press 'R', jumps to carts starting with 'R', etc) [DONE]
20 // - Controller configuration [DONE]
21 //
22 // STILL TO BE DONE:
23 //
24 // - Remove SDL dependencies (sound, mainly) from Jaguar core lib
25 // - Fix inconsistency with trailing slashes in paths (eeproms needs one, software doesn't)
26 //
27 // SFDX CODE: 9XF9TUHFM2359
28
29 // Uncomment this for debugging...
30 //#define DEBUG
31 //#define DEBUGFOO                      // Various tool debugging...
32 //#define DEBUGTP                               // Toolpalette debugging...
33
34 #include "mainwin.h"
35
36 #include "SDL.h"
37 #include "app.h"
38 #include "glwidget.h"
39 #include "about.h"
40 #include "help.h"
41 #include "settings.h"
42 #include "filepicker.h"
43 #include "configdialog.h"
44 #include "generaltab.h"
45 #include "version.h"
46
47 #include "dac.h"
48 #include "jaguar.h"
49 #include "tom.h"
50 #include "log.h"
51 #include "file.h"
52 #include "jagbios.h"
53 #include "jagcdbios.h"
54 #include "jagstub2bios.h"
55 #include "joystick.h"
56
57 // According to SebRmv, this header isn't seen on Arch Linux either... :-/
58 //#ifdef __GCCWIN32__
59 // Apparently on win32, usleep() is not pulled in by the usual suspects.
60 #include <unistd.h>
61 //#endif
62
63 // The way BSNES controls things is by setting a timer with a zero
64 // timeout, sleeping if not emulating anything. Seems there has to be a
65 // better way.
66
67 // It has a novel approach to plugging-in/using different video/audio/input
68 // methods, can we do something similar or should we just use the built-in
69 // QOpenGL?
70
71 // We're going to try to use the built-in OpenGL support and see how it goes.
72 // We'll make the VJ core modular so that it doesn't matter what GUI is in
73 // use, we can drop it in anywhere and use it as-is.
74
75 //MainWin::MainWin(QString filenameToRun): running(true), powerButtonOn(false),
76 MainWin::MainWin(bool autoRun): running(true), powerButtonOn(false),
77         showUntunedTankCircuit(true), cartridgeLoaded(false), CDActive(false),
78         //, alpineLoadSuccessful(false),
79 //      pauseForFileSelector(false), loadAndGo(false), plzDontKillMyComputer(false)
80         pauseForFileSelector(false), loadAndGo(autoRun), plzDontKillMyComputer(false)
81 {
82         for(int i=0; i<8; i++)
83                 keyHeld[i] = false;
84
85         videoWidget = new GLWidget(this);
86         setCentralWidget(videoWidget);
87         setWindowIcon(QIcon(":/res/vj-icon.png"));
88
89         QString title = QString(tr("Virtual Jaguar " VJ_RELEASE_VERSION ));
90
91         if (vjs.hardwareTypeAlpine)
92                 title += QString(tr(" - Alpine Mode"));
93
94         setWindowTitle(title);
95
96         aboutWin = new AboutWindow(this);
97         helpWin = new HelpWindow(this);
98         filePickWin = new FilePickerWindow(this);
99
100     videoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
101     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
102
103         setUnifiedTitleAndToolBarOnMac(true);
104
105         // Create actions
106
107         quitAppAct = new QAction(tr("E&xit"), this);
108 //      quitAppAct->setShortcuts(QKeySequence::Quit);
109 //      quitAppAct->setShortcut(QKeySequence(tr("Alt+x")));
110         quitAppAct->setShortcut(QKeySequence(tr("Ctrl+q")));
111         quitAppAct->setStatusTip(tr("Quit Virtual Jaguar"));
112         connect(quitAppAct, SIGNAL(triggered()), this, SLOT(close()));
113
114         powerGreen.addFile(":/res/power-off.png", QSize(), QIcon::Normal, QIcon::Off);
115         powerGreen.addFile(":/res/power-on-green.png", QSize(), QIcon::Normal, QIcon::On);
116         powerRed.addFile(":/res/power-off.png", QSize(), QIcon::Normal, QIcon::Off);
117         powerRed.addFile(":/res/power-on-red.png", QSize(), QIcon::Normal, QIcon::On);
118
119 //      powerAct = new QAction(QIcon(":/res/power.png"), tr("&Power"), this);
120         powerAct = new QAction(powerGreen, tr("&Power"), this);
121         powerAct->setStatusTip(tr("Powers Jaguar on/off"));
122         powerAct->setCheckable(true);
123         powerAct->setChecked(false);
124 //      powerAct->setDisabled(true);
125         connect(powerAct, SIGNAL(triggered()), this, SLOT(TogglePowerState()));
126
127         QIcon pauseIcon;
128         pauseIcon.addFile(":/res/pause-off", QSize(), QIcon::Normal, QIcon::Off);
129         pauseIcon.addFile(":/res/pause-on", QSize(), QIcon::Normal, QIcon::On);
130 //      pauseAct = new QAction(QIcon(":/res/pause.png"), tr("Pause"), this);
131         pauseAct = new QAction(pauseIcon, tr("Pause"), this);
132         pauseAct->setStatusTip(tr("Toggles the running state"));
133         pauseAct->setCheckable(true);
134         pauseAct->setDisabled(true);
135         pauseAct->setShortcut(QKeySequence(tr("Esc")));
136         connect(pauseAct, SIGNAL(triggered()), this, SLOT(ToggleRunState()));
137
138         zoomActs = new QActionGroup(this);
139
140         x1Act = new QAction(QIcon(":/res/zoom100.png"), tr("Zoom 100%"), zoomActs);
141         x1Act->setStatusTip(tr("Set window zoom to 100%"));
142         x1Act->setCheckable(true);
143         connect(x1Act, SIGNAL(triggered()), this, SLOT(SetZoom100()));
144
145         x2Act = new QAction(QIcon(":/res/zoom200.png"), tr("Zoom 200%"), zoomActs);
146         x2Act->setStatusTip(tr("Set window zoom to 200%"));
147         x2Act->setCheckable(true);
148         connect(x2Act, SIGNAL(triggered()), this, SLOT(SetZoom200()));
149
150         x3Act = new QAction(QIcon(":/res/zoom300.png"), tr("Zoom 300%"), zoomActs);
151         x3Act->setStatusTip(tr("Set window zoom to 300%"));
152         x3Act->setCheckable(true);
153         connect(x3Act, SIGNAL(triggered()), this, SLOT(SetZoom300()));
154
155         tvTypeActs = new QActionGroup(this);
156
157         ntscAct = new QAction(QIcon(":/res/ntsc.png"), tr("NTSC"), tvTypeActs);
158         ntscAct->setStatusTip(tr("Sets Jaguar to NTSC mode"));
159         ntscAct->setCheckable(true);
160         connect(ntscAct, SIGNAL(triggered()), this, SLOT(SetNTSC()));
161
162         palAct = new QAction(QIcon(":/res/pal.png"), tr("PAL"), tvTypeActs);
163         palAct->setStatusTip(tr("Sets Jaguar to PAL mode"));
164         palAct->setCheckable(true);
165         connect(palAct, SIGNAL(triggered()), this, SLOT(SetPAL()));
166
167         blurAct = new QAction(QIcon(":/res/generic.png"), tr("Blur"), this);
168         blurAct->setStatusTip(tr("Sets OpenGL rendering to GL_NEAREST"));
169         blurAct->setCheckable(true);
170         connect(blurAct, SIGNAL(triggered()), this, SLOT(ToggleBlur()));
171
172         aboutAct = new QAction(QIcon(":/res/vj-icon.png"), tr("&About..."), this);
173         aboutAct->setStatusTip(tr("Blatant self-promotion"));
174         connect(aboutAct, SIGNAL(triggered()), this, SLOT(ShowAboutWin()));
175
176         helpAct = new QAction(QIcon(":/res/vj-icon.png"), tr("&Contents..."), this);
177         helpAct->setStatusTip(tr("Help is available, if you should need it"));
178         connect(helpAct, SIGNAL(triggered()), this, SLOT(ShowHelpWin()));
179
180         filePickAct = new QAction(QIcon(":/res/software.png"), tr("&Insert Cartridge..."), this);
181         filePickAct->setStatusTip(tr("Insert a cartridge into Virtual Jaguar"));
182         filePickAct->setShortcut(QKeySequence(tr("Ctrl+i")));
183         connect(filePickAct, SIGNAL(triggered()), this, SLOT(InsertCart()));
184
185         configAct = new QAction(QIcon(":/res/wrench.png"), tr("&Configure"), this);
186         configAct->setStatusTip(tr("Configure options for Virtual Jaguar"));
187         configAct->setShortcut(QKeySequence(tr("Ctrl+c")));
188         connect(configAct, SIGNAL(triggered()), this, SLOT(Configure()));
189
190         useCDAct = new QAction(QIcon(":/res/compact-disc.png"), tr("&Use CD Unit"), this);
191         useCDAct->setStatusTip(tr("Use Jaguar Virtual CD unit"));
192 //      useCDAct->setShortcut(QKeySequence(tr("Ctrl+c")));
193         useCDAct->setCheckable(true);
194         connect(useCDAct, SIGNAL(triggered()), this, SLOT(ToggleCDUsage()));
195
196         frameAdvanceAct = new QAction(QIcon(":/res/frame-advance.png"), tr("&Frame Advance"), this);
197         frameAdvanceAct->setShortcut(QKeySequence(tr("F7")));
198         connect(frameAdvanceAct, SIGNAL(triggered()), this, SLOT(FrameAdvance()));
199
200         // Misc. connections...
201         connect(filePickWin, SIGNAL(RequestLoad(QString)), this, SLOT(LoadSoftware(QString)));
202         connect(filePickWin, SIGNAL(FilePickerHiding()), this, SLOT(Unpause()));
203
204         // Create menus & toolbars
205
206         fileMenu = menuBar()->addMenu(tr("&Jaguar"));
207         fileMenu->addAction(powerAct);
208         fileMenu->addAction(pauseAct);
209         fileMenu->addAction(frameAdvanceAct);
210         fileMenu->addAction(filePickAct);
211         fileMenu->addAction(useCDAct);
212         fileMenu->addAction(configAct);
213         fileMenu->addAction(quitAppAct);
214
215         helpMenu = menuBar()->addMenu(tr("&Help"));
216         helpMenu->addAction(helpAct);
217         helpMenu->addAction(aboutAct);
218
219         toolbar = addToolBar(tr("Stuff"));
220         toolbar->addAction(powerAct);
221         toolbar->addAction(pauseAct);
222         toolbar->addAction(filePickAct);
223         toolbar->addAction(useCDAct);
224         toolbar->addSeparator();
225         toolbar->addAction(x1Act);
226         toolbar->addAction(x2Act);
227         toolbar->addAction(x3Act);
228         toolbar->addSeparator();
229         toolbar->addAction(ntscAct);
230         toolbar->addAction(palAct);
231         toolbar->addSeparator();
232         toolbar->addAction(blurAct);
233
234         //      Create status bar
235         statusBar()->showMessage(tr("Ready"));
236
237         ReadSettings();
238
239         // Do this in case original size isn't correct (mostly for the first-run case)
240         ResizeMainWindow();
241
242         // Set up timer based loop for animation...
243         timer = new QTimer(this);
244         connect(timer, SIGNAL(timeout()), this, SLOT(Timer()));
245 //This is only correct for PAL. !!! FIX !!! [DONE]
246 //Use timer->setInterval( int msec ) to fix this on the fly
247 //      timer->start(20);
248         // This isn't very accurate for NTSC: This is early by 40 msec per frame.
249         // This is because it's discarding the 0.6666... on the end of the fraction.
250         // Alas, 6 doesn't divide cleanly into 10. :-P
251         timer->start(vjs.hardwareTypeNTSC ? 16 : 20);
252
253         // We set this initially, to make VJ behave somewhat as it would if no
254         // cart were inserted and the BIOS was set as active...
255         jaguarCartInserted = true;
256         WriteLog("Virtual Jaguar %s (Last full build was on %s %s)\n", VJ_RELEASE_VERSION, __DATE__, __TIME__);
257         WriteLog("VJ: Initializing jaguar subsystem...\n");
258         JaguarInit();
259         memcpy(jagMemSpace + 0xE00000, jaguarBootROM, 0x20000); // Use the stock BIOS
260
261         // Check for filename passed in on the command line...
262 //      if (!filenameToRun.isEmpty())
263         if (autoRun)
264         {
265 //              loadAndGo = true;
266                 // Attempt to load/run the file the user passed in...
267 //              LoadSoftware(filenameToRun);
268 ////            memcpy(jagMemSpace + 0xE00000, jaguarBootROM, 0x20000); // Use the stock BIOS
269                 // Prevent the file scanner from running...
270                 return;
271         }
272
273         // Load up the default ROM if in Alpine mode:
274         if (vjs.hardwareTypeAlpine)
275         {
276                 bool romLoaded = JaguarLoadFile(vjs.alpineROMPath);
277
278                 // If regular load failed, try just a straight file load
279                 // (Dev only! I don't want people to start getting lazy with their releases again! :-P)
280                 if (!romLoaded)
281                         romLoaded = AlpineLoadFile(vjs.alpineROMPath);
282
283                 if (romLoaded)
284                         WriteLog("Alpine Mode: Successfully loaded file \"%s\".\n", vjs.alpineROMPath);
285                 else
286                         WriteLog("Alpine Mode: Unable to load file \"%s\"!\n", vjs.alpineROMPath);
287
288                 // Attempt to load/run the ABS file...
289                 LoadSoftware(vjs.absROMPath);
290                 memcpy(jagMemSpace + 0xE00000, jaguarDevBootROM2, 0x20000);     // Use the stub BIOS
291                 // Prevent the scanner from running...
292                 return;
293         }
294 //      else
295 //              memcpy(jagMemSpace + 0xE00000, jaguarBootROM, 0x20000); // Otherwise, use the stock BIOS
296
297         // Run the scanner if nothing passed in and *not* Alpine mode...
298         // NB: Really need to look into caching the info scanned in here...
299         filePickWin->ScanSoftwareFolder(allowUnknownSoftware);
300 }
301
302
303 void MainWin::LoadFile(QString file)
304 {
305         LoadSoftware(file);
306 }
307
308
309 void MainWin::SyncUI(void)
310 {
311         // Set toolbar buttons/menus based on settings read in (sync the UI)...
312         blurAct->setChecked(vjs.glFilter);
313         x1Act->setChecked(zoomLevel == 1);
314         x2Act->setChecked(zoomLevel == 2);
315         x3Act->setChecked(zoomLevel == 3);
316 //      running = powerAct->isChecked();
317         ntscAct->setChecked(vjs.hardwareTypeNTSC);
318         palAct->setChecked(!vjs.hardwareTypeNTSC);
319         powerAct->setIcon(vjs.hardwareTypeNTSC ? powerRed : powerGreen);
320 }
321
322
323 void MainWin::closeEvent(QCloseEvent * event)
324 {
325         JaguarDone();
326 // This should only be done by the config dialog
327 //      WriteSettings();
328         WriteUISettings();
329         event->accept(); // ignore() if can't close for some reason
330 }
331
332
333 void MainWin::keyPressEvent(QKeyEvent * e)
334 {
335         // We ignore the Alt key for now, since it causes problems with the GUI
336         if (e->key() == Qt::Key_Alt)
337         {
338                 e->accept();
339                 return;
340         }
341
342         HandleKeys(e, true);
343 }
344
345
346 void MainWin::keyReleaseEvent(QKeyEvent * e)
347 {
348         // We ignore the Alt key for now, since it causes problems with the GUI
349         if (e->key() == Qt::Key_Alt)
350         {
351                 e->accept();
352                 return;
353         }
354
355         HandleKeys(e, false);
356 }
357
358
359 void MainWin::HandleKeys(QKeyEvent * e, bool state)
360 {
361         enum { P1LEFT = 0, P1RIGHT, P1UP, P1DOWN, P2LEFT, P2RIGHT, P2UP, P2DOWN };
362         // We kill bad key combos here, before they can get to the emulator...
363         // This also kills the illegal instruction problem that cropped up in Rayman!
364         // May want to do this by killing the old one instead of ignoring the new one...
365         // Seems to work better that way...
366
367 // The problem with this approach is that it causes bad results because it doesn't do
368 // any checking of previous states. Need to come up with something better because this
369 // causes problems where the keyboard acts as if it were unresponsive. :-P
370 #if 0
371         if ((e->key() == vjs.p1KeyBindings[BUTTON_L] && joypad_0_buttons[BUTTON_R])
372                 || (e->key() == vjs.p1KeyBindings[BUTTON_R] && joypad_0_buttons[BUTTON_L])
373                 || (e->key() == vjs.p1KeyBindings[BUTTON_U] && joypad_0_buttons[BUTTON_D])
374                 || (e->key() == vjs.p1KeyBindings[BUTTON_D] && joypad_0_buttons[BUTTON_U]))
375                 return;
376 #else
377 #if 0
378         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_L] && joypad_0_buttons[BUTTON_R])
379                 joypad_0_buttons[BUTTON_R] = 0;
380         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_R] && joypad_0_buttons[BUTTON_L])
381                 joypad_0_buttons[BUTTON_L] = 0;
382         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_U] && joypad_0_buttons[BUTTON_D])
383                 joypad_0_buttons[BUTTON_D] = 0;
384         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_D] && joypad_0_buttons[BUTTON_U])
385                 joypad_0_buttons[BUTTON_U] = 0;
386
387         if (e->key() == (int)vjs.p2KeyBindings[BUTTON_L] && joypad_1_buttons[BUTTON_R])
388                 joypad_1_buttons[BUTTON_R] = 0;
389         if (e->key() == (int)vjs.p2KeyBindings[BUTTON_R] && joypad_1_buttons[BUTTON_L])
390                 joypad_1_buttons[BUTTON_L] = 0;
391         if (e->key() == (int)vjs.p2KeyBindings[BUTTON_U] && joypad_1_buttons[BUTTON_D])
392                 joypad_1_buttons[BUTTON_D] = 0;
393         if (e->key() == (int)vjs.p2KeyBindings[BUTTON_D] && joypad_1_buttons[BUTTON_U])
394                 joypad_1_buttons[BUTTON_U] = 0;
395 #else
396 //hrm, this still has sticky state problems... Ugh!
397         // First, settle key states...
398         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_L])
399                 keyHeld[P1LEFT] = state;
400         else if (e->key() == (int)vjs.p1KeyBindings[BUTTON_R])
401                 keyHeld[P1RIGHT] = state;
402         else if (e->key() == (int)vjs.p1KeyBindings[BUTTON_U])
403                 keyHeld[P1UP] = state;
404         else if (e->key() == (int)vjs.p1KeyBindings[BUTTON_D])
405                 keyHeld[P1DOWN] = state;
406         else if (e->key() == (int)vjs.p2KeyBindings[BUTTON_L])
407                 keyHeld[P2LEFT] = state;
408         else if (e->key() == (int)vjs.p2KeyBindings[BUTTON_R])
409                 keyHeld[P2RIGHT] = state;
410         else if (e->key() == (int)vjs.p2KeyBindings[BUTTON_U])
411                 keyHeld[P2UP] = state;
412         else if (e->key() == (int)vjs.p2KeyBindings[BUTTON_D])
413                 keyHeld[P2DOWN] = state;
414
415         // Next, check for conflicts and bail out if there are any...
416         if ((keyHeld[P1LEFT] && keyHeld[P1RIGHT])
417                 || (keyHeld[P1UP] && keyHeld[P1DOWN])
418                 || (keyHeld[P2LEFT] && keyHeld[P2RIGHT])
419                 || (keyHeld[P2UP] && keyHeld[P2DOWN]))
420                 return;
421 #endif
422 #endif
423
424         // No bad combos exist, let's stuff the emulator key buffers...!
425         for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
426         {
427                 if (e->key() == (int)vjs.p1KeyBindings[i])
428 //                      joypad_0_buttons[i] = (uint8)state;
429                         joypad_0_buttons[i] = (state ? 0x01 : 0x00);
430
431 // Pad #2 is screwing up pad #1. Prolly a problem in joystick.cpp...
432 // So let's try to fix it there. :-P [DONE]
433                 if (e->key() == (int)vjs.p2KeyBindings[i])
434 //                      joypad_1_buttons[i] = (uint8)state;
435                         joypad_1_buttons[i] = (state ? 0x01 : 0x00);
436         }
437 }
438
439
440 void MainWin::Open(void)
441 {
442 }
443
444
445 void MainWin::Configure(void)
446 {
447         // Call the configuration dialog and update settings
448         ConfigDialog dlg(this);
449         //ick.
450         dlg.generalTab->useUnknownSoftware->setChecked(allowUnknownSoftware);
451
452         if (dlg.exec() == false)
453                 return;
454
455         QString before = vjs.ROMPath;
456         QString alpineBefore = vjs.alpineROMPath;
457         QString absBefore = vjs.absROMPath;
458 //      bool audioBefore = vjs.audioEnabled;
459         bool audioBefore = vjs.DSPEnabled;
460         dlg.UpdateVJSettings();
461         QString after = vjs.ROMPath;
462         QString alpineAfter = vjs.alpineROMPath;
463         QString absAfter = vjs.absROMPath;
464 //      bool audioAfter = vjs.audioEnabled;
465         bool audioAfter = vjs.DSPEnabled;
466
467         bool allowOld = allowUnknownSoftware;
468         //ick.
469         allowUnknownSoftware = dlg.generalTab->useUnknownSoftware->isChecked();
470
471         // We rescan the "software" folder if the user either changed the path or
472         // checked/unchecked the "Allow unknown files" option in the config dialog.
473         if ((before != after) || (allowOld != allowUnknownSoftware))
474                 filePickWin->ScanSoftwareFolder(allowUnknownSoftware);
475
476         // If the "Alpine" ROM is changed, then let's load it...
477         if (alpineBefore != alpineAfter)
478         {
479                 if (!JaguarLoadFile(vjs.alpineROMPath) && !AlpineLoadFile(vjs.alpineROMPath))
480                 {
481                         // Oh crap, we couldn't get the file! Alert the media!
482                         QMessageBox msg;
483                         msg.setText(QString(tr("Could not load file \"%1\"!")).arg(vjs.alpineROMPath));
484                         msg.setIcon(QMessageBox::Warning);
485                         msg.exec();
486                 }
487         }
488
489         // If the "ABS" ROM is changed, then let's load it...
490         if (absBefore != absAfter)
491         {
492                 if (!JaguarLoadFile(vjs.absROMPath))
493                 {
494                         // Oh crap, we couldn't get the file! Alert the media!
495                         QMessageBox msg;
496                         msg.setText(QString(tr("Could not load file \"%1\"!")).arg(vjs.absROMPath));
497                         msg.setIcon(QMessageBox::Warning);
498                         msg.exec();
499                 }
500         }
501
502         // If the "Enable DSP" checkbox changed, then we have to re-init the DAC,
503         // since it's running in the host audio IRQ...
504         if (audioBefore != audioAfter)
505         {
506                 DACDone();
507                 DACInit();
508         }
509
510         // Just in case we crash before a clean exit...
511         WriteSettings();
512 }
513
514
515 //
516 // Here's the main emulator loop
517 //
518 void MainWin::Timer(void)
519 {
520         if (!running)
521                 return;
522
523         if (showUntunedTankCircuit)
524         {
525                 // Some machines can't handle this, so we give them the option to disable it. :-)
526                 if (!plzDontKillMyComputer)
527                 {
528                 // Random hash & trash
529                 // We try to simulate an untuned tank circuit here... :-)
530                 for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
531                 {
532                         for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
533                         {
534                                 videoWidget->buffer[(y * videoWidget->textureWidth) + x]
535                                         = (rand() & 0xFF) << 8 | (rand() & 0xFF) << 16 | (rand() & 0xFF) << 24;
536                         }
537                 }
538                 }
539         }
540         else
541         {
542                 // Otherwise, run the Jaguar simulation
543                 JaguarExecuteNew();
544         }
545
546         videoWidget->updateGL();
547 }
548
549
550 void MainWin::TogglePowerState(void)
551 {
552         powerButtonOn = !powerButtonOn;
553         running = true;
554
555         // With the power off, we simulate white noise on the screen. :-)
556         if (!powerButtonOn)
557         {
558                 useCDAct->setDisabled(false);
559                 palAct->setDisabled(false);
560                 ntscAct->setDisabled(false);
561                 pauseAct->setChecked(false);
562                 pauseAct->setDisabled(true);
563                 showUntunedTankCircuit = true;
564                 // This is just in case the ROM we were playing was in a narrow or wide field mode,
565                 // so the untuned tank sim doesn't look wrong. :-)
566                 TOMReset();
567         }
568         else
569         {
570                 useCDAct->setDisabled(true);
571                 palAct->setDisabled(true);
572                 ntscAct->setDisabled(true);
573                 pauseAct->setChecked(false);
574                 pauseAct->setDisabled(false);
575                 showUntunedTankCircuit = false;
576
577                 // Otherwise, we prepare for running regular software...
578                 if (CDActive)
579                 {
580 // Should check for cartridgeLoaded here as well...!
581 // We can clear it when toggling CDActive on, so that when we power cycle it does the
582 // expected thing. Otherwise, if we use the file picker to insert a cart, we expect
583 // to run the cart! Maybe have a RemoveCart function that only works if the CD unit
584 // is active?
585                         setWindowTitle(QString("Virtual Jaguar " VJ_RELEASE_VERSION
586                                 " - Now playing: Jaguar CD"));
587                 }
588
589                 WriteLog("GUI: Resetting Jaguar...\n");
590                 JaguarReset();
591         }
592 }
593
594
595 void MainWin::ToggleRunState(void)
596 {
597         running = !running;
598
599         if (!running)
600         {
601                 for(uint32_t i=0; i<(uint32_t)(videoWidget->textureWidth * 256); i++)
602                 {
603                         uint32_t pixel = videoWidget->buffer[i];
604                         uint8_t r = (pixel >> 24) & 0xFF, g = (pixel >> 16) & 0xFF, b = (pixel >> 8) & 0xFF;
605                         pixel = ((r + g + b) / 3) & 0x00FF;
606                         videoWidget->buffer[i] = 0x000000FF | (pixel << 16) | (pixel << 8);
607                 }
608
609                 videoWidget->updateGL();
610         }
611 }
612
613
614 void MainWin::SetZoom100(void)
615 {
616         zoomLevel = 1;
617         ResizeMainWindow();
618 }
619
620
621 void MainWin::SetZoom200(void)
622 {
623         zoomLevel = 2;
624         ResizeMainWindow();
625 }
626
627
628 void MainWin::SetZoom300(void)
629 {
630         zoomLevel = 3;
631         ResizeMainWindow();
632 }
633
634
635 void MainWin::SetNTSC(void)
636 {
637         powerAct->setIcon(powerRed);
638         timer->setInterval(16);
639         vjs.hardwareTypeNTSC = true;
640         ResizeMainWindow();
641         WriteSettings();
642 }
643
644
645 void MainWin::SetPAL(void)
646 {
647         powerAct->setIcon(powerGreen);
648         timer->setInterval(20);
649         vjs.hardwareTypeNTSC = false;
650         ResizeMainWindow();
651         WriteSettings();
652 }
653
654
655 void MainWin::ToggleBlur(void)
656 {
657         vjs.glFilter = !vjs.glFilter;
658         WriteSettings();
659 }
660
661
662 void MainWin::ShowAboutWin(void)
663 {
664         aboutWin->show();
665 }
666
667
668 void MainWin::ShowHelpWin(void)
669 {
670         helpWin->show();
671 }
672
673
674 void MainWin::InsertCart(void)
675 {
676         // If the emulator is running, we pause it here and unpause it later
677         // if we dismiss the file selector without choosing anything
678         if (running && powerButtonOn)
679         {
680                 ToggleRunState();
681                 pauseForFileSelector = true;
682         }
683
684         filePickWin->show();
685 }
686
687
688 void MainWin::Unpause(void)
689 {
690         // Here we unpause the emulator if it was paused when we went into the file selector
691         if (pauseForFileSelector)
692         {
693                 pauseForFileSelector = false;
694
695                 // Some nutter might have unpaused while in the file selector, so check for that
696                 if (!running)
697                         ToggleRunState();
698         }
699 }
700
701
702 void MainWin::LoadSoftware(QString file)
703 {
704         running = false;                                                        // Prevent bad things(TM) from happening...
705         pauseForFileSelector = false;                           // Reset the file selector pause flag
706         SET32(jaguarMainRAM, 0, 0x00200000);            // Set top of stack...
707         cartridgeLoaded = JaguarLoadFile(file.toAscii().data());
708
709         char * biosPointer = jaguarBootROM;
710
711         if (vjs.hardwareTypeAlpine)
712                 biosPointer = jaguarDevBootROM2;
713
714         memcpy(jagMemSpace + 0xE00000, biosPointer, 0x20000);
715
716         powerAct->setDisabled(false);
717         powerAct->setChecked(true);
718         powerButtonOn = false;
719         TogglePowerState();
720
721         if (!vjs.hardwareTypeAlpine && !loadAndGo)
722         {
723                 QString newTitle = QString("Virtual Jaguar " VJ_RELEASE_VERSION " - Now playing: %1")
724                         .arg(filePickWin->GetSelectedPrettyName());
725                 setWindowTitle(newTitle);
726         }
727 }
728
729
730 void MainWin::ToggleCDUsage(void)
731 {
732         CDActive = !CDActive;
733
734 #if 0
735         if (CDActive)
736         {
737                 powerAct->setDisabled(false);
738         }
739         else
740         {
741                 powerAct->setDisabled(true);
742         }
743 #else
744         // Set up the Jaguar CD for execution, otherwise, clear memory
745         if (CDActive)
746                 memcpy(jagMemSpace + 0x800000, jaguarCDBootROM, 0x40000);
747         else
748                 memset(jagMemSpace + 0x800000, 0xFF, 0x40000);
749 #endif
750 }
751
752
753 void MainWin::FrameAdvance(void)
754 {
755 //printf("Frame Advance...\n");
756         // Execute 1 frame, then exit (only useful in Pause mode)
757         JaguarExecuteNew();
758         videoWidget->updateGL();
759 }
760
761
762 void MainWin::ResizeMainWindow(void)
763 {
764         videoWidget->setFixedSize(zoomLevel * 320, zoomLevel * (vjs.hardwareTypeNTSC ? 240 : 256));
765         show();
766
767         for(int i=0; i<2; i++)
768         {
769                 resize(0, 0);
770                 usleep(2000);
771                 QApplication::processEvents();
772         }
773 }
774
775
776 #warning "!!! Need to check the window geometry to see if the positions are legal !!!"
777 // i.e., someone could drag it to another screen, close it, then disconnect that screen
778 void MainWin::ReadSettings(void)
779 {
780         QSettings settings("Underground Software", "Virtual Jaguar");
781         QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
782         QSize size = settings.value("size", QSize(400, 400)).toSize();
783         resize(size);
784         move(pos);
785         pos = settings.value("cartLoadPos", QPoint(200, 200)).toPoint();
786         filePickWin->move(pos);
787
788         zoomLevel = settings.value("zoom", 2).toInt();
789         allowUnknownSoftware = settings.value("showUnknownSoftware", false).toBool();
790
791         vjs.useJoystick      = settings.value("useJoystick", false).toBool();
792         vjs.joyport          = settings.value("joyport", 0).toInt();
793         vjs.hardwareTypeNTSC = settings.value("hardwareTypeNTSC", true).toBool();
794         vjs.frameSkip        = settings.value("frameSkip", 0).toInt();
795         vjs.useJaguarBIOS    = settings.value("useJaguarBIOS", false).toBool();
796         vjs.GPUEnabled       = settings.value("GPUEnabled", true).toBool();
797         vjs.DSPEnabled       = settings.value("DSPEnabled", false).toBool();
798         vjs.audioEnabled     = settings.value("audioEnabled", true).toBool();
799         vjs.usePipelinedDSP  = settings.value("usePipelinedDSP", false).toBool();
800         vjs.fullscreen       = settings.value("fullscreen", false).toBool();
801         vjs.useOpenGL        = settings.value("useOpenGL", true).toBool();
802         vjs.glFilter         = settings.value("glFilterType", 1).toInt();
803         vjs.renderType       = settings.value("renderType", 0).toInt();
804         vjs.allowWritesToROM = settings.value("writeROM", false).toBool();
805 //      strcpy(vjs.jagBootPath, settings.value("JagBootROM", "./bios/[BIOS] Atari Jaguar (USA, Europe).zip").toString().toAscii().data());
806 //      strcpy(vjs.CDBootPath, settings.value("CDBootROM", "./bios/jagcd.rom").toString().toAscii().data());
807         strcpy(vjs.EEPROMPath, settings.value("EEPROMs", "./eeproms/").toString().toAscii().data());
808         strcpy(vjs.ROMPath, settings.value("ROMs", "./software/").toString().toAscii().data());
809         strcpy(vjs.alpineROMPath, settings.value("DefaultROM", "").toString().toAscii().data());
810         strcpy(vjs.absROMPath, settings.value("DefaultABS", "").toString().toAscii().data());
811 WriteLog("MainWin: Paths\n");
812 //WriteLog("    jagBootPath = \"%s\"\n", vjs.jagBootPath);
813 //WriteLog("    CDBootPath  = \"%s\"\n", vjs.CDBootPath);
814 WriteLog("   EEPROMPath = \"%s\"\n", vjs.EEPROMPath);
815 WriteLog("      ROMPath = \"%s\"\n", vjs.ROMPath);
816 WriteLog("AlpineROMPath = \"%s\"\n", vjs.alpineROMPath);
817 WriteLog("   absROMPath = \"%s\"\n", vjs.absROMPath);
818 WriteLog("Pipelined DSP = %s\n", (vjs.usePipelinedDSP ? "ON" : "off"));
819
820         // Keybindings in order of U, D, L, R, C, B, A, Op, Pa, 0-9, #, *
821         vjs.p1KeyBindings[BUTTON_U] = settings.value("p1k_up", Qt::Key_S).toInt();
822         vjs.p1KeyBindings[BUTTON_D] = settings.value("p1k_down", Qt::Key_X).toInt();
823         vjs.p1KeyBindings[BUTTON_L] = settings.value("p1k_left", Qt::Key_A).toInt();
824         vjs.p1KeyBindings[BUTTON_R] = settings.value("p1k_right", Qt::Key_D).toInt();
825         vjs.p1KeyBindings[BUTTON_C] = settings.value("p1k_c", Qt::Key_J).toInt();
826         vjs.p1KeyBindings[BUTTON_B] = settings.value("p1k_b", Qt::Key_K).toInt();
827         vjs.p1KeyBindings[BUTTON_A] = settings.value("p1k_a", Qt::Key_L).toInt();
828         vjs.p1KeyBindings[BUTTON_OPTION] = settings.value("p1k_option", Qt::Key_O).toInt();
829         vjs.p1KeyBindings[BUTTON_PAUSE] = settings.value("p1k_pause", Qt::Key_P).toInt();
830         vjs.p1KeyBindings[BUTTON_0] = settings.value("p1k_0", Qt::Key_0).toInt();
831         vjs.p1KeyBindings[BUTTON_1] = settings.value("p1k_1", Qt::Key_1).toInt();
832         vjs.p1KeyBindings[BUTTON_2] = settings.value("p1k_2", Qt::Key_2).toInt();
833         vjs.p1KeyBindings[BUTTON_3] = settings.value("p1k_3", Qt::Key_3).toInt();
834         vjs.p1KeyBindings[BUTTON_4] = settings.value("p1k_4", Qt::Key_4).toInt();
835         vjs.p1KeyBindings[BUTTON_5] = settings.value("p1k_5", Qt::Key_5).toInt();
836         vjs.p1KeyBindings[BUTTON_6] = settings.value("p1k_6", Qt::Key_6).toInt();
837         vjs.p1KeyBindings[BUTTON_7] = settings.value("p1k_7", Qt::Key_7).toInt();
838         vjs.p1KeyBindings[BUTTON_8] = settings.value("p1k_8", Qt::Key_8).toInt();
839         vjs.p1KeyBindings[BUTTON_9] = settings.value("p1k_9", Qt::Key_9).toInt();
840         vjs.p1KeyBindings[BUTTON_d] = settings.value("p1k_pound", Qt::Key_Minus).toInt();
841         vjs.p1KeyBindings[BUTTON_s] = settings.value("p1k_star", Qt::Key_Equal).toInt();
842
843         vjs.p2KeyBindings[BUTTON_U] = settings.value("p2k_up", Qt::Key_Up).toInt();
844         vjs.p2KeyBindings[BUTTON_D] = settings.value("p2k_down", Qt::Key_Down).toInt();
845         vjs.p2KeyBindings[BUTTON_L] = settings.value("p2k_left", Qt::Key_Left).toInt();
846         vjs.p2KeyBindings[BUTTON_R] = settings.value("p2k_right", Qt::Key_Right).toInt();
847         vjs.p2KeyBindings[BUTTON_C] = settings.value("p2k_c", Qt::Key_Z).toInt();
848         vjs.p2KeyBindings[BUTTON_B] = settings.value("p2k_b", Qt::Key_X).toInt();
849         vjs.p2KeyBindings[BUTTON_A] = settings.value("p2k_a", Qt::Key_C).toInt();
850         vjs.p2KeyBindings[BUTTON_OPTION] = settings.value("p2k_option", Qt::Key_Apostrophe).toInt();
851         vjs.p2KeyBindings[BUTTON_PAUSE] = settings.value("p2k_pause", Qt::Key_Return).toInt();
852         vjs.p2KeyBindings[BUTTON_0] = settings.value("p2k_0", Qt::Key_0).toInt();
853         vjs.p2KeyBindings[BUTTON_1] = settings.value("p2k_1", Qt::Key_1).toInt();
854         vjs.p2KeyBindings[BUTTON_2] = settings.value("p2k_2", Qt::Key_2).toInt();
855         vjs.p2KeyBindings[BUTTON_3] = settings.value("p2k_3", Qt::Key_3).toInt();
856         vjs.p2KeyBindings[BUTTON_4] = settings.value("p2k_4", Qt::Key_4).toInt();
857         vjs.p2KeyBindings[BUTTON_5] = settings.value("p2k_5", Qt::Key_5).toInt();
858         vjs.p2KeyBindings[BUTTON_6] = settings.value("p2k_6", Qt::Key_6).toInt();
859         vjs.p2KeyBindings[BUTTON_7] = settings.value("p2k_7", Qt::Key_7).toInt();
860         vjs.p2KeyBindings[BUTTON_8] = settings.value("p2k_8", Qt::Key_8).toInt();
861         vjs.p2KeyBindings[BUTTON_9] = settings.value("p2k_9", Qt::Key_9).toInt();
862         vjs.p2KeyBindings[BUTTON_d] = settings.value("p2k_pound", Qt::Key_Slash).toInt();
863         vjs.p2KeyBindings[BUTTON_s] = settings.value("p2k_star", Qt::Key_Asterisk).toInt();
864 }
865
866
867 void MainWin::WriteSettings(void)
868 {
869         QSettings settings("Underground Software", "Virtual Jaguar");
870         settings.setValue("pos", pos());
871         settings.setValue("size", size());
872         settings.setValue("cartLoadPos", filePickWin->pos());
873
874         settings.setValue("zoom", zoomLevel);
875         settings.setValue("showUnknownSoftware", allowUnknownSoftware);
876
877         settings.setValue("useJoystick", vjs.useJoystick);
878         settings.setValue("joyport", vjs.joyport);
879         settings.setValue("hardwareTypeNTSC", vjs.hardwareTypeNTSC);
880         settings.setValue("frameSkip", vjs.frameSkip);
881         settings.setValue("useJaguarBIOS", vjs.useJaguarBIOS);
882         settings.setValue("GPUEnabled", vjs.GPUEnabled);
883         settings.setValue("DSPEnabled", vjs.DSPEnabled);
884         settings.setValue("audioEnabled", vjs.audioEnabled);
885         settings.setValue("usePipelinedDSP", vjs.usePipelinedDSP);
886         settings.setValue("fullscreen", vjs.fullscreen);
887         settings.setValue("useOpenGL", vjs.useOpenGL);
888         settings.setValue("glFilterType", vjs.glFilter);
889         settings.setValue("renderType", vjs.renderType);
890         settings.setValue("writeROM", vjs.allowWritesToROM);
891         settings.setValue("JagBootROM", vjs.jagBootPath);
892         settings.setValue("CDBootROM", vjs.CDBootPath);
893         settings.setValue("EEPROMs", vjs.EEPROMPath);
894         settings.setValue("ROMs", vjs.ROMPath);
895         settings.setValue("DefaultROM", vjs.alpineROMPath);
896         settings.setValue("DefaultABS", vjs.absROMPath);
897
898         settings.setValue("p1k_up", vjs.p1KeyBindings[BUTTON_U]);
899         settings.setValue("p1k_down", vjs.p1KeyBindings[BUTTON_D]);
900         settings.setValue("p1k_left", vjs.p1KeyBindings[BUTTON_L]);
901         settings.setValue("p1k_right", vjs.p1KeyBindings[BUTTON_R]);
902         settings.setValue("p1k_c", vjs.p1KeyBindings[BUTTON_C]);
903         settings.setValue("p1k_b", vjs.p1KeyBindings[BUTTON_B]);
904         settings.setValue("p1k_a", vjs.p1KeyBindings[BUTTON_A]);
905         settings.setValue("p1k_option", vjs.p1KeyBindings[BUTTON_OPTION]);
906         settings.setValue("p1k_pause", vjs.p1KeyBindings[BUTTON_PAUSE]);
907         settings.setValue("p1k_0", vjs.p1KeyBindings[BUTTON_0]);
908         settings.setValue("p1k_1", vjs.p1KeyBindings[BUTTON_1]);
909         settings.setValue("p1k_2", vjs.p1KeyBindings[BUTTON_2]);
910         settings.setValue("p1k_3", vjs.p1KeyBindings[BUTTON_3]);
911         settings.setValue("p1k_4", vjs.p1KeyBindings[BUTTON_4]);
912         settings.setValue("p1k_5", vjs.p1KeyBindings[BUTTON_5]);
913         settings.setValue("p1k_6", vjs.p1KeyBindings[BUTTON_6]);
914         settings.setValue("p1k_7", vjs.p1KeyBindings[BUTTON_7]);
915         settings.setValue("p1k_8", vjs.p1KeyBindings[BUTTON_8]);
916         settings.setValue("p1k_9", vjs.p1KeyBindings[BUTTON_9]);
917         settings.setValue("p1k_pound", vjs.p1KeyBindings[BUTTON_d]);
918         settings.setValue("p1k_star", vjs.p1KeyBindings[BUTTON_s]);
919
920         settings.setValue("p2k_up", vjs.p2KeyBindings[BUTTON_U]);
921         settings.setValue("p2k_down", vjs.p2KeyBindings[BUTTON_D]);
922         settings.setValue("p2k_left", vjs.p2KeyBindings[BUTTON_L]);
923         settings.setValue("p2k_right", vjs.p2KeyBindings[BUTTON_R]);
924         settings.setValue("p2k_c", vjs.p2KeyBindings[BUTTON_C]);
925         settings.setValue("p2k_b", vjs.p2KeyBindings[BUTTON_B]);
926         settings.setValue("p2k_a", vjs.p2KeyBindings[BUTTON_A]);
927         settings.setValue("p2k_option", vjs.p2KeyBindings[BUTTON_OPTION]);
928         settings.setValue("p2k_pause", vjs.p2KeyBindings[BUTTON_PAUSE]);
929         settings.setValue("p2k_0", vjs.p2KeyBindings[BUTTON_0]);
930         settings.setValue("p2k_1", vjs.p2KeyBindings[BUTTON_1]);
931         settings.setValue("p2k_2", vjs.p2KeyBindings[BUTTON_2]);
932         settings.setValue("p2k_3", vjs.p2KeyBindings[BUTTON_3]);
933         settings.setValue("p2k_4", vjs.p2KeyBindings[BUTTON_4]);
934         settings.setValue("p2k_5", vjs.p2KeyBindings[BUTTON_5]);
935         settings.setValue("p2k_6", vjs.p2KeyBindings[BUTTON_6]);
936         settings.setValue("p2k_7", vjs.p2KeyBindings[BUTTON_7]);
937         settings.setValue("p2k_8", vjs.p2KeyBindings[BUTTON_8]);
938         settings.setValue("p2k_9", vjs.p2KeyBindings[BUTTON_9]);
939         settings.setValue("p2k_pound", vjs.p2KeyBindings[BUTTON_d]);
940         settings.setValue("p2k_star", vjs.p2KeyBindings[BUTTON_s]);
941 }
942
943
944 void MainWin::WriteUISettings(void)
945 {
946         QSettings settings("Underground Software", "Virtual Jaguar");
947         settings.setValue("pos", pos());
948         settings.setValue("size", size());
949         settings.setValue("cartLoadPos", filePickWin->pos());
950
951         settings.setValue("zoom", zoomLevel);
952 }