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