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