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