]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/mainwin.cpp
Added more developer friendly stuff to Alpine mode.
[virtualjaguar] / src / gui / mainwin.cpp
1 //
2 // mainwin.cpp - Qt-based GUI for Virtual Jaguar: Main Application Window
3 // by James L. Hammons
4 // (C) 2009 Underground Software
5 //
6 // JLH = James L. 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 //
21 // STILL TO BE DONE:
22 //
23 // - Controller configuration
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
28 // Uncomment this for debugging...
29 //#define DEBUG
30 //#define DEBUGFOO                      // Various tool debugging...
31 //#define DEBUGTP                               // Toolpalette debugging...
32
33 #include "mainwin.h"
34
35 #include "SDL.h"
36 #include "glwidget.h"
37 #include "about.h"
38 #include "settings.h"
39 #include "filepicker.h"
40 #include "configdialog.h"
41 #include "generaltab.h"
42 #include "version.h"
43
44 #include "dac.h"
45 #include "jaguar.h"
46 #include "tom.h"
47 #include "log.h"
48 #include "file.h"
49 #include "joystick.h"
50
51 #ifdef __GCCWIN32__
52 // Apparently on win32, usleep() is not pulled in by the usual suspects.
53 #include <unistd.h>
54 #endif
55
56 // Uncomment this to use built-in BIOS/CD-ROM BIOS
57 // You'll need a copy of jagboot.h & jagcd.h for this to work...!
58 // Creating those is left as an exercise for the reader. ;-)
59 //#define USE_BUILT_IN_BIOS
60
61 #ifdef USE_BUILT_IN_BIOS
62 #include "jagboot.h"
63 #include "jagcd.h"
64 #endif
65
66 // The way BSNES controls things is by setting a timer with a zero
67 // timeout, sleeping if not emulating anything. Seems there has to be a
68 // better way.
69
70 // It has a novel approach to plugging-in/using different video/audio/input
71 // methods, can we do something similar or should we just use the built-in
72 // QOpenGL?
73
74 // We're going to try to use the built-in OpenGL support and see how it goes.
75 // We'll make the VJ core modular so that it doesn't matter what GUI is in
76 // use, we can drop it in anywhere and use it as-is.
77
78 MainWin::MainWin(): running(false), powerButtonOn(false), showUntunedTankCircuit(true),
79         cartridgeLoaded(false), CDActive(false)//, alpineLoadSuccessful(false)
80 {
81         videoWidget = new GLWidget(this);
82         setCentralWidget(videoWidget);
83         setWindowIcon(QIcon(":/res/vj-icon.png"));
84 //      setWindowTitle("Virtual Jaguar v2.0.0");
85
86         QString title = QString(tr("Virtual Jaguar " VJ_RELEASE_VERSION ));
87
88         if (vjs.hardwareTypeAlpine)
89                 title += QString(tr(" - Alpine Mode"));
90
91         setWindowTitle(title);
92
93         aboutWin = new AboutWindow(this);
94         filePickWin = new FilePickerWindow(this);
95
96     videoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
97     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
98
99         setUnifiedTitleAndToolBarOnMac(true);
100
101         // Create actions
102
103         quitAppAct = new QAction(tr("E&xit"), this);
104         quitAppAct->setShortcuts(QKeySequence::Quit);
105         quitAppAct->setStatusTip(tr("Quit Virtual Jaguar"));
106         connect(quitAppAct, SIGNAL(triggered()), this, SLOT(close()));
107
108         powerAct = new QAction(QIcon(":/res/power.png"), tr("&Power"), this);
109         powerAct->setStatusTip(tr("Powers Jaguar on/off"));
110         powerAct->setCheckable(true);
111         powerAct->setChecked(false);
112         powerAct->setDisabled(true);
113         connect(powerAct, SIGNAL(triggered()), this, SLOT(TogglePowerState()));
114
115         pauseAct = new QAction(QIcon(":/res/pause.png"), tr("Pause"), this);
116         pauseAct->setStatusTip(tr("Toggles the running state"));
117         pauseAct->setCheckable(true);
118         pauseAct->setDisabled(true);
119         pauseAct->setShortcut(QKeySequence(tr("Esc")));
120         connect(pauseAct, SIGNAL(triggered()), this, SLOT(ToggleRunState()));
121
122         zoomActs = new QActionGroup(this);
123
124         x1Act = new QAction(QIcon(":/res/zoom100.png"), tr("Zoom 100%"), zoomActs);
125         x1Act->setStatusTip(tr("Set window zoom to 100%"));
126         x1Act->setCheckable(true);
127         connect(x1Act, SIGNAL(triggered()), this, SLOT(SetZoom100()));
128
129         x2Act = new QAction(QIcon(":/res/zoom200.png"), tr("Zoom 200%"), zoomActs);
130         x2Act->setStatusTip(tr("Set window zoom to 200%"));
131         x2Act->setCheckable(true);
132         connect(x2Act, SIGNAL(triggered()), this, SLOT(SetZoom200()));
133
134         x3Act = new QAction(QIcon(":/res/zoom300.png"), tr("Zoom 300%"), zoomActs);
135         x3Act->setStatusTip(tr("Set window zoom to 300%"));
136         x3Act->setCheckable(true);
137         connect(x3Act, SIGNAL(triggered()), this, SLOT(SetZoom300()));
138
139         tvTypeActs = new QActionGroup(this);
140
141         ntscAct = new QAction(QIcon(":/res/ntsc.png"), tr("NTSC"), tvTypeActs);
142         ntscAct->setStatusTip(tr("Sets Jaguar to NTSC mode"));
143         ntscAct->setCheckable(true);
144         connect(ntscAct, SIGNAL(triggered()), this, SLOT(SetNTSC()));
145
146         palAct = new QAction(QIcon(":/res/pal.png"), tr("PAL"), tvTypeActs);
147         palAct->setStatusTip(tr("Sets Jaguar to PAL mode"));
148         palAct->setCheckable(true);
149         connect(palAct, SIGNAL(triggered()), this, SLOT(SetPAL()));
150
151         blurAct = new QAction(QIcon(":/res/generic.png"), tr("Blur"), this);
152         blurAct->setStatusTip(tr("Sets OpenGL rendering to GL_NEAREST"));
153         blurAct->setCheckable(true);
154         connect(blurAct, SIGNAL(triggered()), this, SLOT(ToggleBlur()));
155
156         aboutAct = new QAction(QIcon(":/res/vj-icon.png"), tr("&About..."), this);
157         aboutAct->setStatusTip(tr("Blatant self-promotion"));
158         connect(aboutAct, SIGNAL(triggered()), this, SLOT(ShowAboutWin()));
159
160         filePickAct = new QAction(QIcon(":/res/software.png"), tr("&Insert Cartridge..."), this);
161         filePickAct->setStatusTip(tr("Insert a cartridge into Virtual Jaguar"));
162         filePickAct->setShortcut(QKeySequence(tr("Ctrl+i")));
163         connect(filePickAct, SIGNAL(triggered()), this, SLOT(InsertCart()));
164
165         configAct = new QAction(QIcon(":/res/generic.png"), tr("&Configure"), this);
166         configAct->setStatusTip(tr("Configure options for Virtual Jaguar"));
167         configAct->setShortcut(QKeySequence(tr("Ctrl+c")));
168         connect(configAct, SIGNAL(triggered()), this, SLOT(Configure()));
169
170         useCDAct = new QAction(QIcon(":/res/compact-disc.png"), tr("&Use CD Unit"), this);
171         useCDAct->setStatusTip(tr("Use Jaguar Virtual CD unit"));
172 //      useCDAct->setShortcut(QKeySequence(tr("Ctrl+c")));
173         useCDAct->setCheckable(true);
174         connect(useCDAct, SIGNAL(triggered()), this, SLOT(ToggleCDUsage()));
175
176         // Misc. connections...
177         connect(filePickWin, SIGNAL(RequestLoad(QString)), this, SLOT(LoadSoftware(QString)));
178
179         // Create menus & toolbars
180
181         fileMenu = menuBar()->addMenu(tr("&File"));
182         fileMenu->addAction(filePickAct);
183         fileMenu->addAction(useCDAct);
184         fileMenu->addAction(powerAct);
185         fileMenu->addAction(pauseAct);
186         fileMenu->addAction(configAct);
187         fileMenu->addAction(quitAppAct);
188
189         helpMenu = menuBar()->addMenu(tr("&Help"));
190         helpMenu->addAction(aboutAct);
191
192         toolbar = addToolBar(tr("Stuff"));
193         toolbar->addAction(powerAct);
194         toolbar->addAction(pauseAct);
195         toolbar->addAction(filePickAct);
196         toolbar->addAction(useCDAct);
197         toolbar->addSeparator();
198         toolbar->addAction(x1Act);
199         toolbar->addAction(x2Act);
200         toolbar->addAction(x3Act);
201         toolbar->addSeparator();
202         toolbar->addAction(ntscAct);
203         toolbar->addAction(palAct);
204         toolbar->addSeparator();
205         toolbar->addAction(blurAct);
206
207         //      Create status bar
208         statusBar()->showMessage(tr("Ready"));
209
210         ReadSettings();
211
212         // Set toolbar buttons/menus based on settings read in (sync the UI)...
213         blurAct->setChecked(vjs.glFilter);
214         x1Act->setChecked(zoomLevel == 1);
215         x2Act->setChecked(zoomLevel == 2);
216         x3Act->setChecked(zoomLevel == 3);
217         running = powerAct->isChecked();
218         ntscAct->setChecked(vjs.hardwareTypeNTSC);
219         palAct->setChecked(!vjs.hardwareTypeNTSC);
220
221         // Do this in case original size isn't correct (mostly for the first-run case)
222         ResizeMainWindow();
223
224         // Set up timer based loop for animation...
225         timer = new QTimer(this);
226         connect(timer, SIGNAL(timeout()), this, SLOT(Timer()));
227         timer->start(20);
228
229         WriteLog("Virtual Jaguar %s (Last full build was on %s %s)\n", VJ_RELEASE_VERSION, __DATE__, __TIME__);
230         WriteLog("VJ: Initializing jaguar subsystem...\n");
231         JaguarInit();
232
233         // Get the BIOS ROM
234 #ifdef USE_BUILT_IN_BIOS
235         WriteLog("VJ: Using built in BIOS/CD BIOS...\n");
236         memcpy(jaguarBootROM, jagBootROM, 0x20000);
237         memcpy(jaguarCDBootROM, jagCDROM, 0x40000);
238 //      BIOSLoaded = CDBIOSLoaded = true;
239         biosAvailable |= (BIOS_NORMAL | BIOS_CD);
240 #else
241 // What would be nice here would be a way to check if the BIOS was loaded so that we
242 // could disable the pushbutton on the Misc Options menu... !!! FIX !!! [DONE here, but needs to be fixed in GUI as well!]
243 //      WriteLog("VJ: About to attempt to load BIOSes...\n");
244 //This is short-circuiting the file finding thread... ??? WHY ???
245 //Not anymore. Was related to a QImage object creation/corruption bug elsewhere.
246 //      BIOSLoaded = (JaguarLoadROM(jaguarBootROM, vjs.jagBootPath) == 0x20000 ? true : false);
247 //      WriteLog("VJ: BIOS is %savailable...\n", (BIOSLoaded ? "" : "not "));
248 //      CDBIOSLoaded = (JaguarLoadROM(jaguarCDBootROM, vjs.CDBootPath) == 0x40000 ? true : false);
249 //      WriteLog("VJ: CD BIOS is %savailable...\n", (CDBIOSLoaded ? "" : "not "));
250 #endif
251
252         filePickWin->ScanSoftwareFolder(allowUnknownSoftware);
253
254         // Load up the default ROM if in Alpine mode:
255         if (vjs.hardwareTypeAlpine)
256         {
257                 bool romLoaded = JaguarLoadFile(vjs.alpineROMPath);
258
259                 // If regular load failed, try just a straight file load
260                 // (Dev only! I don't want people to start getting lazy with their releases again! :-P)
261                 if (!romLoaded)
262                         romLoaded = AlpineLoadFile(vjs.alpineROMPath);
263
264                 if (romLoaded)
265                         WriteLog("Alpine Mode: Successfully loaded file \"%s\".\n", vjs.alpineROMPath);
266                 else
267                         WriteLog("Alpine Mode: Unable to load file \"%s\"!\n", vjs.alpineROMPath);
268
269                 // Attempt to load/run the ABS file...
270                 LoadSoftware(vjs.absROMPath);
271         }
272 }
273
274 void MainWin::closeEvent(QCloseEvent * event)
275 {
276         JaguarDone();
277         WriteSettings();
278         event->accept(); // ignore() if can't close for some reason
279 }
280
281 void MainWin::keyPressEvent(QKeyEvent * e)
282 {
283         HandleKeys(e, true);
284 }
285
286 void MainWin::keyReleaseEvent(QKeyEvent * e)
287 {
288         HandleKeys(e, false);
289 }
290
291 void MainWin::HandleKeys(QKeyEvent * e, bool state)
292 {
293         // We kill bad key combos here, before they can get to the emulator...
294         // This also kills the illegal instruction problem that cropped up in Rayman!
295         // May want to do this by killing the old one instead of ignoring the new one...
296         // Seems to work better that way...
297 #if 0
298         if ((e->key() == vjs.p1KeyBindings[BUTTON_L] && joypad_0_buttons[BUTTON_R])
299                 || (e->key() == vjs.p1KeyBindings[BUTTON_R] && joypad_0_buttons[BUTTON_L])
300                 || (e->key() == vjs.p1KeyBindings[BUTTON_U] && joypad_0_buttons[BUTTON_D])
301                 || (e->key() == vjs.p1KeyBindings[BUTTON_D] && joypad_0_buttons[BUTTON_U]))
302                 return;
303 #else
304         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_L] && joypad_0_buttons[BUTTON_R])
305                 joypad_0_buttons[BUTTON_R] = 0;
306         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_R] && joypad_0_buttons[BUTTON_L])
307                 joypad_0_buttons[BUTTON_L] = 0;
308         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_U] && joypad_0_buttons[BUTTON_D])
309                 joypad_0_buttons[BUTTON_D] = 0;
310         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_D] && joypad_0_buttons[BUTTON_U])
311                 joypad_0_buttons[BUTTON_U] = 0;
312 #endif
313
314         // No bad combos exist, let's stuff the emulator key buffers...!
315         for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
316         {
317                 if (e->key() == (int)vjs.p1KeyBindings[i])
318                         joypad_0_buttons[i] = (uint8)state;
319         }
320 }
321
322 void MainWin::Open(void)
323 {
324 }
325
326 void MainWin::Configure(void)
327 {
328         // Call the configuration dialog and update settings
329         ConfigDialog dlg(this);
330         //ick.
331         dlg.generalTab->useUnknownSoftware->setChecked(allowUnknownSoftware);
332
333         if (dlg.exec() == false)
334                 return;
335
336         QString before = vjs.ROMPath;
337         QString alpineBefore = vjs.alpineROMPath;
338         QString absBefore = vjs.absROMPath;
339         bool audioBefore = vjs.audioEnabled;
340         dlg.UpdateVJSettings();
341         QString after = vjs.ROMPath;
342         QString alpineAfter = vjs.alpineROMPath;
343         QString absAfter = vjs.absROMPath;
344         bool audioAfter = vjs.audioEnabled;
345
346         bool allowOld = allowUnknownSoftware;
347         //ick.
348         allowUnknownSoftware = dlg.generalTab->useUnknownSoftware->isChecked();
349
350         // We rescan the "software" folder if the user either changed the path or
351         // checked/unchecked the "Allow unknown files" option in the config dialog.
352         if ((before != after) || (allowOld != allowUnknownSoftware))
353                 filePickWin->ScanSoftwareFolder(allowUnknownSoftware);
354
355         // If the "Alpine" ROM is changed, then let's load it...
356         if (alpineBefore != alpineAfter)
357         {
358                 if (!JaguarLoadFile(vjs.alpineROMPath) || !AlpineLoadFile(vjs.alpineROMPath))
359                 {
360                         // Oh crap, we couldn't get the file! Alert the media!
361                         QMessageBox msg;
362                         msg.setText(QString(tr("Could not load file \"%1\"!")).arg(vjs.alpineROMPath));
363                         msg.setIcon(QMessageBox::Warning);
364                         msg.exec();
365                 }
366         }
367
368         // If the "ABS" ROM is changed, then let's load it...
369         if (absBefore != absAfter)
370         {
371                 if (!JaguarLoadFile(vjs.absROMPath))
372                 {
373                         // Oh crap, we couldn't get the file! Alert the media!
374                         QMessageBox msg;
375                         msg.setText(QString(tr("Could not load file \"%1\"!")).arg(vjs.absROMPath));
376                         msg.setIcon(QMessageBox::Warning);
377                         msg.exec();
378                 }
379         }
380
381         // If the "Enable audio" checkbox changed, then we have to re-init the DAC...
382         if (audioBefore != audioAfter)
383         {
384                 DACDone();
385                 DACInit();
386         }
387
388         // Just in case we crash before a clean exit...
389         WriteSettings();
390 }
391
392 //
393 // Here's the main emulator loop
394 //
395 void MainWin::Timer(void)
396 {
397         if (!running)
398                 return;
399
400         if (showUntunedTankCircuit)
401         {
402                 // Random hash & trash
403                 // We try to simulate an untuned tank circuit here... :-)
404                 for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
405                 {
406                         for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
407                         {
408                                 videoWidget->buffer[(y * videoWidget->textureWidth) + x] = (rand() & 0xFF) << 8 | (rand() & 0xFF) << 16 | (rand() & 0xFF) << 24;// | (rand() & 0xFF);//0x000000FF;
409         //                      buffer[(y * textureWidth) + x] = x*y;
410                         }
411                 }
412         }
413         else
414         {
415                 // Otherwise, run the Jaguar simulation
416                 JaguarExecuteNew();
417 //              memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->rasterWidth);
418                 memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->textureWidth * sizeof(uint32_t));
419 //              memcpy(surface->pixels, backbuffer, TOMGetVideoModeWidth() * TOMGetVideoModeHeight() * 4);
420         }
421
422         videoWidget->updateGL();
423 }
424
425 #if 0
426 Window * RunEmu(void)
427 {
428 //      extern uint32 * backbuffer;
429         uint32 * overlayPixels = (uint32 *)sdlemuGetOverlayPixels();
430         memset(overlayPixels, 0x00, 640 * 480 * 4);                     // Clear out overlay...
431
432 //This is crappy... !!! FIX !!!
433 //      extern bool finished, showGUI;
434
435         sdlemuDisableOverlay();
436
437 //      uint32 nFrame = 0, nFrameskip = 0;
438         uint32 totalFrames = 0;
439         finished = false;
440         bool showMessage = true;
441         uint32 showMsgFrames = 120;
442         uint8 transparency = 0xFF;
443         // Pass a message to the "joystick" code to debounce the ESC key...
444         debounceRunKey = true;
445
446         uint32 cartType = 4;
447         if (jaguarRomSize == 0x200000)
448                 cartType = 0;
449         else if (jaguarRomSize == 0x400000)
450                 cartType = 1;
451         else if (jaguarMainRomCRC32 == 0x687068D5)
452                 cartType = 2;
453         else if (jaguarMainRomCRC32 == 0x55A0669C)
454                 cartType = 3;
455
456         const char * cartTypeName[5] = { "2M Cartridge", "4M Cartridge", "CD BIOS", "CD Dev BIOS", "Homebrew" };
457         uint32 elapsedTicks = SDL_GetTicks(), frameCount = 0, framesPerSecond = 0;
458
459         while (!finished)
460         {
461                 // Set up new backbuffer with new pixels and data
462                 JaguarExecuteNew();
463                 totalFrames++;
464 //WriteLog("Frame #%u...\n", totalFrames);
465 //extern bool doDSPDis;
466 //if (totalFrames == 373)
467 //      doDSPDis = true;
468
469 //Problem: Need to do this *only* when the state changes from visible to not...
470 //Also, need to clear out the GUI when not on (when showMessage is active...)
471 if (showGUI || showMessage)
472         sdlemuEnableOverlay();
473 else
474         sdlemuDisableOverlay();
475
476 //Add in a new function for clearing patches of screen (ClearOverlayRect)
477
478 // Also: Take frame rate into account when calculating fade time...
479
480                 // Some QnD GUI stuff here...
481                 if (showGUI)
482                 {
483                         FillScreenRectangle(overlayPixels, 8, 1*FONT_HEIGHT, 128, 4*FONT_HEIGHT, 0x00000000);
484                         extern uint32 gpu_pc, dsp_pc;
485                         DrawString(overlayPixels, 8, 1*FONT_HEIGHT, false, "GPU PC: %08X", gpu_pc);
486                         DrawString(overlayPixels, 8, 2*FONT_HEIGHT, false, "DSP PC: %08X", dsp_pc);
487                         DrawString(overlayPixels, 8, 4*FONT_HEIGHT, false, "%u FPS", framesPerSecond);
488                 }
489
490                 if (showMessage)
491                 {
492                         DrawString2(overlayPixels, 8, 24*FONT_HEIGHT, 0x007F63FF, transparency, "Running...");
493                         DrawString2(overlayPixels, 8, 26*FONT_HEIGHT, 0x001FFF3F, transparency, "%s, run address: %06X", cartTypeName[cartType], jaguarRunAddress);
494                         DrawString2(overlayPixels, 8, 27*FONT_HEIGHT, 0x001FFF3F, transparency, "CRC: %08X", jaguarMainRomCRC32);
495
496                         if (showMsgFrames == 0)
497                         {
498                                 transparency--;
499
500                                 if (transparency == 0)
501 {
502                                         showMessage = false;
503 /*extern bool doGPUDis;
504 doGPUDis = true;//*/
505 }
506
507                         }
508                         else
509                                 showMsgFrames--;
510                 }
511
512                 frameCount++;
513
514                 if (SDL_GetTicks() - elapsedTicks > 250)
515                         elapsedTicks += 250, framesPerSecond = frameCount * 4, frameCount = 0;
516         }
517
518         // Save the background for the GUI...
519         // In this case, we squash the color to monochrome, then force it to blue + green...
520         for(uint32 i=0; i<TOMGetVideoModeWidth() * 256; i++)
521         {
522                 uint32 pixel = backbuffer[i];
523                 uint8 b = (pixel >> 16) & 0xFF, g = (pixel >> 8) & 0xFF, r = pixel & 0xFF;
524                 pixel = ((r + g + b) / 3) & 0x00FF;
525                 backbuffer[i] = 0xFF000000 | (pixel << 16) | (pixel << 8);
526         }
527
528         sdlemuEnableOverlay();
529
530         return NULL;
531 }
532 #endif
533
534 void MainWin::TogglePowerState(void)
535 {
536         powerButtonOn = !powerButtonOn;
537
538         if (!powerButtonOn)
539         {
540                 pauseAct->setChecked(false);
541                 pauseAct->setDisabled(true);
542                 showUntunedTankCircuit = true;
543                 running = true;
544                 // This is just in case the ROM we were playing was in a narrow or wide field mode,
545                 // so the untuned tank sim doesn't look wrong. :-)
546                 TOMReset();
547         }
548         else
549         {
550                 if (!CDActive)
551                 {
552                         showUntunedTankCircuit = false;//(cartridgeLoaded ? false : true);
553                         pauseAct->setChecked(false);
554                         pauseAct->setDisabled(!cartridgeLoaded);
555                 }
556                 else
557                 {
558 // Should check for cartridgeLoaded here as well...!
559 // We can clear it when toggling CDActive on, so that when we power cycle it does the
560 // expected thing. Otherwise, if we use the file picker to insert a cart, we expect
561 // to run the cart! Maybe have a RemoveCart function that only works if the CD unit
562 // is active?
563                         showUntunedTankCircuit = false;
564                         pauseAct->setChecked(false);
565                         pauseAct->setDisabled(false);
566                         memcpy(jagMemSpace + 0x800000, jaguarCDBootROM, 0x40000);
567                         setWindowTitle(QString("Virtual Jaguar " VJ_RELEASE_VERSION
568                                 " - Now playing: Jaguar CD"));
569                 }
570
571 //(Err, what's so crappy about this? It seems to do what it's supposed to...)
572 //This is crappy!!! !!! FIX !!!
573 //Is this even needed any more? Hmm. Maybe. Dunno.
574 //Seems like it is... But then again, maybe not. Have to test it to see.
575                 WriteLog("GUI: Resetting Jaguar...\n");
576                 JaguarReset();
577                 running = true;
578         }
579 }
580
581 void MainWin::ToggleRunState(void)
582 {
583         running = !running;
584
585         if (!running)
586         {
587                 for(uint32_t i=0; i<(uint32_t)(videoWidget->textureWidth * 256); i++)
588                 {
589                         uint32_t pixel = backbuffer[i];
590                         uint8_t r = (pixel >> 24) & 0xFF, g = (pixel >> 16) & 0xFF, b = (pixel >> 8) & 0xFF;
591                         pixel = ((r + g + b) / 3) & 0x00FF;
592                         backbuffer[i] = 0x000000FF | (pixel << 16) | (pixel << 8);
593                 }
594
595                 memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->textureWidth * sizeof(uint32_t));
596
597                 videoWidget->updateGL();
598         }
599 }
600
601 void MainWin::SetZoom100(void)
602 {
603         zoomLevel = 1;
604         ResizeMainWindow();
605 }
606
607 void MainWin::SetZoom200(void)
608 {
609         zoomLevel = 2;
610         ResizeMainWindow();
611 }
612
613 void MainWin::SetZoom300(void)
614 {
615         zoomLevel = 3;
616         ResizeMainWindow();
617 }
618
619 void MainWin::SetNTSC(void)
620 {
621         vjs.hardwareTypeNTSC = true;
622         ResizeMainWindow();
623 }
624
625 void MainWin::SetPAL(void)
626 {
627         vjs.hardwareTypeNTSC = false;
628         ResizeMainWindow();
629 }
630
631 void MainWin::ToggleBlur(void)
632 {
633         vjs.glFilter = !vjs.glFilter;
634 }
635
636 void MainWin::ShowAboutWin(void)
637 {
638         aboutWin->show();
639 }
640
641 void MainWin::InsertCart(void)
642 {
643         filePickWin->show();
644 }
645
646 void MainWin::LoadSoftware(QString file)
647 {
648         running = false;                                                        //  Prevent bad things(TM) from happening...
649         SET32(jaguarMainRAM, 0, 0x00200000);            // Set top of stack...
650         cartridgeLoaded = (JaguarLoadFile(file.toAscii().data()) ? true : false);
651
652         uint8_t * biosPointer = jaguarBootROM;
653
654         if (vjs.hardwareTypeAlpine)
655         {
656                 if (biosAvailable & BIOS_STUB1)
657 //                      memcpy(jagMemSpace + 0xE00000, jaguarDevBootROM1, 0x20000);
658                         biosPointer = jaguarDevBootROM1;
659                 else if (biosAvailable & BIOS_STUB2)
660 //                      memcpy(jagMemSpace + 0xE00000, jaguarDevBootROM2, 0x20000);
661                         biosPointer = jaguarDevBootROM2;
662         }
663 //      else
664 //              memcpy(jagMemSpace + 0xE00000, jaguarBootROM, 0x20000);
665
666         memcpy(jagMemSpace + 0xE00000, biosPointer, 0x20000);
667
668         powerAct->setDisabled(false);
669         powerAct->setChecked(true);
670         powerButtonOn = false;
671         TogglePowerState();
672
673         if (!vjs.hardwareTypeAlpine)
674         {
675                 QString newTitle = QString("Virtual Jaguar " VJ_RELEASE_VERSION " - Now playing: %1")
676                         .arg(filePickWin->GetSelectedPrettyName());
677                 setWindowTitle(newTitle);
678         }
679 }
680
681 void MainWin::ToggleCDUsage(void)
682 {
683         CDActive = !CDActive;
684
685         if (CDActive)
686         {
687                 powerAct->setDisabled(false);
688         }
689         else
690         {
691                 powerAct->setDisabled(true);
692         }
693 }
694
695 void MainWin::ResizeMainWindow(void)
696 {
697         videoWidget->setFixedSize(zoomLevel * 320, zoomLevel * (vjs.hardwareTypeNTSC ? 240 : 256));
698         show();
699
700         for(int i=0; i<2; i++)
701         {
702                 resize(0, 0);
703                 usleep(2000);
704                 QApplication::processEvents();
705         }
706 }
707
708 void MainWin::ReadSettings(void)
709 {
710         QSettings settings("Underground Software", "Virtual Jaguar");
711         QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
712         QSize size = settings.value("size", QSize(400, 400)).toSize();
713         resize(size);
714         move(pos);
715         pos = settings.value("cartLoadPos", QPoint(200, 200)).toPoint();
716         filePickWin->move(pos);
717
718         zoomLevel = settings.value("zoom", 1).toInt();
719         allowUnknownSoftware = settings.value("showUnknownSoftware", false).toBool();
720
721         vjs.useJoystick      = settings.value("useJoystick", false).toBool();
722         vjs.joyport          = settings.value("joyport", 0).toInt();
723         vjs.hardwareTypeNTSC = settings.value("hardwareTypeNTSC", true).toBool();
724         vjs.frameSkip        = settings.value("frameSkip", 0).toInt();
725         vjs.useJaguarBIOS    = settings.value("useJaguarBIOS", false).toBool();
726         vjs.DSPEnabled       = settings.value("DSPEnabled", false).toBool();
727         vjs.audioEnabled     = settings.value("audioEnabled", true).toBool();
728         vjs.usePipelinedDSP  = settings.value("usePipelinedDSP", false).toBool();
729         vjs.fullscreen       = settings.value("fullscreen", false).toBool();
730         vjs.useOpenGL        = settings.value("useOpenGL", true).toBool();
731         vjs.glFilter         = settings.value("glFilterType", 0).toInt();
732         vjs.renderType       = settings.value("renderType", 0).toInt();
733         vjs.allowWritesToROM = settings.value("writeROM", false).toBool();
734 //      strcpy(vjs.jagBootPath, settings.value("JagBootROM", "./bios/[BIOS] Atari Jaguar (USA, Europe).zip").toString().toAscii().data());
735 //      strcpy(vjs.CDBootPath, settings.value("CDBootROM", "./bios/jagcd.rom").toString().toAscii().data());
736         strcpy(vjs.EEPROMPath, settings.value("EEPROMs", "./eeproms/").toString().toAscii().data());
737         strcpy(vjs.ROMPath, settings.value("ROMs", "./software/").toString().toAscii().data());
738         strcpy(vjs.alpineROMPath, settings.value("DefaultROM", "").toString().toAscii().data());
739         strcpy(vjs.absROMPath, settings.value("DefaultABS", "").toString().toAscii().data());
740 WriteLog("MainWin: Paths\n");
741 //WriteLog("    jagBootPath = \"%s\"\n", vjs.jagBootPath);
742 //WriteLog("    CDBootPath  = \"%s\"\n", vjs.CDBootPath);
743 WriteLog("   EEPROMPath = \"%s\"\n", vjs.EEPROMPath);
744 WriteLog("      ROMPath = \"%s\"\n", vjs.ROMPath);
745 WriteLog("AlpineROMPath = \"%s\"\n", vjs.alpineROMPath);
746 WriteLog("   absROMPath = \"%s\"\n", vjs.absROMPath);
747
748         // Keybindings in order of U, D, L, R, C, B, A, Op, Pa, 0-9, #, *
749         vjs.p1KeyBindings[BUTTON_U] = settings.value("p1k_up", Qt::Key_Up).toInt();
750         vjs.p1KeyBindings[BUTTON_D] = settings.value("p1k_down", Qt::Key_Down).toInt();
751         vjs.p1KeyBindings[BUTTON_L] = settings.value("p1k_left", Qt::Key_Left).toInt();
752         vjs.p1KeyBindings[BUTTON_R] = settings.value("p1k_right", Qt::Key_Right).toInt();
753         vjs.p1KeyBindings[BUTTON_C] = settings.value("p1k_c", Qt::Key_Z).toInt();
754         vjs.p1KeyBindings[BUTTON_B] = settings.value("p1k_b", Qt::Key_X).toInt();
755         vjs.p1KeyBindings[BUTTON_A] = settings.value("p1k_a", Qt::Key_C).toInt();
756         vjs.p1KeyBindings[BUTTON_OPTION] = settings.value("p1k_option", Qt::Key_Apostrophe).toInt();
757         vjs.p1KeyBindings[BUTTON_PAUSE] = settings.value("p1k_pause", Qt::Key_Return).toInt();
758         vjs.p1KeyBindings[BUTTON_0] = settings.value("p1k_0", Qt::Key_0).toInt();
759         vjs.p1KeyBindings[BUTTON_1] = settings.value("p1k_1", Qt::Key_1).toInt();
760         vjs.p1KeyBindings[BUTTON_2] = settings.value("p1k_2", Qt::Key_2).toInt();
761         vjs.p1KeyBindings[BUTTON_3] = settings.value("p1k_3", Qt::Key_3).toInt();
762         vjs.p1KeyBindings[BUTTON_4] = settings.value("p1k_4", Qt::Key_4).toInt();
763         vjs.p1KeyBindings[BUTTON_5] = settings.value("p1k_5", Qt::Key_5).toInt();
764         vjs.p1KeyBindings[BUTTON_6] = settings.value("p1k_6", Qt::Key_6).toInt();
765         vjs.p1KeyBindings[BUTTON_7] = settings.value("p1k_7", Qt::Key_7).toInt();
766         vjs.p1KeyBindings[BUTTON_8] = settings.value("p1k_8", Qt::Key_8).toInt();
767         vjs.p1KeyBindings[BUTTON_9] = settings.value("p1k_9", Qt::Key_9).toInt();
768         vjs.p1KeyBindings[BUTTON_d] = settings.value("p1k_pound", Qt::Key_Slash).toInt();
769         vjs.p1KeyBindings[BUTTON_s] = settings.value("p1k_star", Qt::Key_Asterisk).toInt();
770
771         vjs.p2KeyBindings[BUTTON_U] = settings.value("p2k_up", Qt::Key_Up).toInt();
772         vjs.p2KeyBindings[BUTTON_D] = settings.value("p2k_down", Qt::Key_Down).toInt();
773         vjs.p2KeyBindings[BUTTON_L] = settings.value("p2k_left", Qt::Key_Left).toInt();
774         vjs.p2KeyBindings[BUTTON_R] = settings.value("p2k_right", Qt::Key_Right).toInt();
775         vjs.p2KeyBindings[BUTTON_C] = settings.value("p2k_c", Qt::Key_Z).toInt();
776         vjs.p2KeyBindings[BUTTON_B] = settings.value("p2k_b", Qt::Key_X).toInt();
777         vjs.p2KeyBindings[BUTTON_A] = settings.value("p2k_a", Qt::Key_C).toInt();
778         vjs.p2KeyBindings[BUTTON_OPTION] = settings.value("p2k_option", Qt::Key_Apostrophe).toInt();
779         vjs.p2KeyBindings[BUTTON_PAUSE] = settings.value("p2k_pause", Qt::Key_Return).toInt();
780         vjs.p2KeyBindings[BUTTON_0] = settings.value("p2k_0", Qt::Key_0).toInt();
781         vjs.p2KeyBindings[BUTTON_1] = settings.value("p2k_1", Qt::Key_1).toInt();
782         vjs.p2KeyBindings[BUTTON_2] = settings.value("p2k_2", Qt::Key_2).toInt();
783         vjs.p2KeyBindings[BUTTON_3] = settings.value("p2k_3", Qt::Key_3).toInt();
784         vjs.p2KeyBindings[BUTTON_4] = settings.value("p2k_4", Qt::Key_4).toInt();
785         vjs.p2KeyBindings[BUTTON_5] = settings.value("p2k_5", Qt::Key_5).toInt();
786         vjs.p2KeyBindings[BUTTON_6] = settings.value("p2k_6", Qt::Key_6).toInt();
787         vjs.p2KeyBindings[BUTTON_7] = settings.value("p2k_7", Qt::Key_7).toInt();
788         vjs.p2KeyBindings[BUTTON_8] = settings.value("p2k_8", Qt::Key_8).toInt();
789         vjs.p2KeyBindings[BUTTON_9] = settings.value("p2k_9", Qt::Key_9).toInt();
790         vjs.p2KeyBindings[BUTTON_d] = settings.value("p2k_pound", Qt::Key_Slash).toInt();
791         vjs.p2KeyBindings[BUTTON_s] = settings.value("p2k_star", Qt::Key_Asterisk).toInt();
792 }
793
794 void MainWin::WriteSettings(void)
795 {
796         QSettings settings("Underground Software", "Virtual Jaguar");
797         settings.setValue("pos", pos());
798         settings.setValue("size", size());
799         settings.setValue("cartLoadPos", filePickWin->pos());
800
801         settings.setValue("zoom", zoomLevel);
802         settings.setValue("showUnknownSoftware", allowUnknownSoftware);
803
804         settings.setValue("useJoystick", vjs.useJoystick);
805         settings.setValue("joyport", vjs.joyport);
806         settings.setValue("hardwareTypeNTSC", vjs.hardwareTypeNTSC);
807         settings.setValue("frameSkip", vjs.frameSkip);
808         settings.setValue("useJaguarBIOS", vjs.useJaguarBIOS);
809         settings.setValue("DSPEnabled", vjs.DSPEnabled);
810         settings.setValue("audioEnabled", vjs.audioEnabled);
811         settings.setValue("usePipelinedDSP", vjs.usePipelinedDSP);
812         settings.setValue("fullscreen", vjs.fullscreen);
813         settings.setValue("useOpenGL", vjs.useOpenGL);
814         settings.setValue("glFilterType", vjs.glFilter);
815         settings.setValue("renderType", vjs.renderType);
816         settings.setValue("writeROM", vjs.allowWritesToROM);
817         settings.setValue("JagBootROM", vjs.jagBootPath);
818         settings.setValue("CDBootROM", vjs.CDBootPath);
819         settings.setValue("EEPROMs", vjs.EEPROMPath);
820         settings.setValue("ROMs", vjs.ROMPath);
821         settings.setValue("DefaultROM", vjs.alpineROMPath);
822         settings.setValue("DefaultABS", vjs.absROMPath);
823
824         settings.setValue("p1k_up", vjs.p1KeyBindings[BUTTON_U]);
825         settings.setValue("p1k_down", vjs.p1KeyBindings[BUTTON_D]);
826         settings.setValue("p1k_left", vjs.p1KeyBindings[BUTTON_L]);
827         settings.setValue("p1k_right", vjs.p1KeyBindings[BUTTON_R]);
828         settings.setValue("p1k_c", vjs.p1KeyBindings[BUTTON_C]);
829         settings.setValue("p1k_b", vjs.p1KeyBindings[BUTTON_B]);
830         settings.setValue("p1k_a", vjs.p1KeyBindings[BUTTON_A]);
831         settings.setValue("p1k_option", vjs.p1KeyBindings[BUTTON_OPTION]);
832         settings.setValue("p1k_pause", vjs.p1KeyBindings[BUTTON_PAUSE]);
833         settings.setValue("p1k_0", vjs.p1KeyBindings[BUTTON_0]);
834         settings.setValue("p1k_1", vjs.p1KeyBindings[BUTTON_1]);
835         settings.setValue("p1k_2", vjs.p1KeyBindings[BUTTON_2]);
836         settings.setValue("p1k_3", vjs.p1KeyBindings[BUTTON_3]);
837         settings.setValue("p1k_4", vjs.p1KeyBindings[BUTTON_4]);
838         settings.setValue("p1k_5", vjs.p1KeyBindings[BUTTON_5]);
839         settings.setValue("p1k_6", vjs.p1KeyBindings[BUTTON_6]);
840         settings.setValue("p1k_7", vjs.p1KeyBindings[BUTTON_7]);
841         settings.setValue("p1k_8", vjs.p1KeyBindings[BUTTON_8]);
842         settings.setValue("p1k_9", vjs.p1KeyBindings[BUTTON_9]);
843         settings.setValue("p1k_pound", vjs.p1KeyBindings[BUTTON_d]);
844         settings.setValue("p1k_star", vjs.p1KeyBindings[BUTTON_s]);
845
846         settings.setValue("p2k_up", vjs.p2KeyBindings[BUTTON_U]);
847         settings.setValue("p2k_down", vjs.p2KeyBindings[BUTTON_D]);
848         settings.setValue("p2k_left", vjs.p2KeyBindings[BUTTON_L]);
849         settings.setValue("p2k_right", vjs.p2KeyBindings[BUTTON_R]);
850         settings.setValue("p2k_c", vjs.p2KeyBindings[BUTTON_C]);
851         settings.setValue("p2k_b", vjs.p2KeyBindings[BUTTON_B]);
852         settings.setValue("p2k_a", vjs.p2KeyBindings[BUTTON_A]);
853         settings.setValue("p2k_option", vjs.p2KeyBindings[BUTTON_OPTION]);
854         settings.setValue("p2k_pause", vjs.p2KeyBindings[BUTTON_PAUSE]);
855         settings.setValue("p2k_0", vjs.p2KeyBindings[BUTTON_0]);
856         settings.setValue("p2k_1", vjs.p2KeyBindings[BUTTON_1]);
857         settings.setValue("p2k_2", vjs.p2KeyBindings[BUTTON_2]);
858         settings.setValue("p2k_3", vjs.p2KeyBindings[BUTTON_3]);
859         settings.setValue("p2k_4", vjs.p2KeyBindings[BUTTON_4]);
860         settings.setValue("p2k_5", vjs.p2KeyBindings[BUTTON_5]);
861         settings.setValue("p2k_6", vjs.p2KeyBindings[BUTTON_6]);
862         settings.setValue("p2k_7", vjs.p2KeyBindings[BUTTON_7]);
863         settings.setValue("p2k_8", vjs.p2KeyBindings[BUTTON_8]);
864         settings.setValue("p2k_9", vjs.p2KeyBindings[BUTTON_9]);
865         settings.setValue("p2k_pound", vjs.p2KeyBindings[BUTTON_d]);
866         settings.setValue("p2k_star", vjs.p2KeyBindings[BUTTON_s]);
867 }
868
869 // Here's how Byuu does it...
870 // I think I have it working now... :-)
871 #if 0
872 void Utility::resizeMainWindow()
873 {
874   unsigned region = config().video.context->region;
875   unsigned multiplier = config().video.context->multiplier;
876   unsigned width = 256 * multiplier;
877   unsigned height = (region == 0 ? 224 : 239) * multiplier;
878
879   if(config().video.context->correctAspectRatio)
880   {
881     if(region == 0)
882         {
883       width = (double)width * config().video.ntscAspectRatio + 0.5;  //NTSC adjust
884     }
885         else
886         {
887       width = (double)width * config().video.palAspectRatio  + 0.5;  //PAL adjust
888     }
889   }
890
891   if(config().video.isFullscreen == false)
892   {
893     //get effective desktop work area region (ignore Windows taskbar, OS X dock, etc.)
894     QRect deskRect = QApplication::desktop()->availableGeometry(mainWindow);
895
896     //ensure window size will not be larger than viewable desktop area
897     constrainSize(height, width, deskRect.height()); //- frameHeight);
898     constrainSize(width, height, deskRect.width());  //- frameWidth );
899
900     mainWindow->canvas->setFixedSize(width, height);
901     mainWindow->show();
902   }
903   else
904   {
905     for(unsigned i = 0; i < 2; i++)
906         {
907       unsigned iWidth = width, iHeight = height;
908
909       constrainSize(iHeight, iWidth, mainWindow->canvasContainer->size().height());
910       constrainSize(iWidth, iHeight, mainWindow->canvasContainer->size().width());
911
912       //center canvas onscreen; ensure it is not larger than viewable area
913       mainWindow->canvas->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
914       mainWindow->canvas->setFixedSize(iWidth, iHeight);
915       mainWindow->canvas->setMinimumSize(0, 0);
916
917       usleep(2000);
918       QApplication::processEvents();
919     }
920   }
921
922   //workaround for Qt/Xlib bug:
923   //if window resize occurs with cursor over it, Qt shows Qt::Size*DiagCursor;
924   //so force it to show Qt::ArrowCursor, as expected
925   mainWindow->setCursor(Qt::ArrowCursor);
926   mainWindow->canvasContainer->setCursor(Qt::ArrowCursor);
927   mainWindow->canvas->setCursor(Qt::ArrowCursor);
928
929   //workaround for DirectSound(?) bug:
930   //window resizing sometimes breaks audio sync, this call re-initializes it
931   updateAvSync();
932 }
933
934 void Utility::setScale(unsigned scale)
935 {
936   config().video.context->multiplier = scale;
937   resizeMainWindow();
938   mainWindow->shrink();
939   mainWindow->syncUi();
940 }
941
942 void QbWindow::shrink()
943 {
944   if(config().video.isFullscreen == false)
945   {
946     for(unsigned i = 0; i < 2; i++)
947         {
948       resize(0, 0);
949       usleep(2000);
950       QApplication::processEvents();
951     }
952   }
953 }
954 #endif