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