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