]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/mainwin.cpp
Mask out ALT for now, small optimization in screen rendering code.
[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
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(true), 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         // We ignore the Alt key for now, since it causes problems with the GUI
298         if (e->key() == Qt::Key_Alt)
299         {
300                 e->accept();
301                 return;
302         }
303
304         HandleKeys(e, true);
305 }
306
307 void MainWin::keyReleaseEvent(QKeyEvent * e)
308 {
309         // We ignore the Alt key for now, since it causes problems with the GUI
310         if (e->key() == Qt::Key_Alt)
311         {
312                 e->accept();
313                 return;
314         }
315
316         HandleKeys(e, false);
317 }
318
319 void MainWin::HandleKeys(QKeyEvent * e, bool state)
320 {
321         // We kill bad key combos here, before they can get to the emulator...
322         // This also kills the illegal instruction problem that cropped up in Rayman!
323         // May want to do this by killing the old one instead of ignoring the new one...
324         // Seems to work better that way...
325 #if 0
326         if ((e->key() == vjs.p1KeyBindings[BUTTON_L] && joypad_0_buttons[BUTTON_R])
327                 || (e->key() == vjs.p1KeyBindings[BUTTON_R] && joypad_0_buttons[BUTTON_L])
328                 || (e->key() == vjs.p1KeyBindings[BUTTON_U] && joypad_0_buttons[BUTTON_D])
329                 || (e->key() == vjs.p1KeyBindings[BUTTON_D] && joypad_0_buttons[BUTTON_U]))
330                 return;
331 #else
332         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_L] && joypad_0_buttons[BUTTON_R])
333                 joypad_0_buttons[BUTTON_R] = 0;
334         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_R] && joypad_0_buttons[BUTTON_L])
335                 joypad_0_buttons[BUTTON_L] = 0;
336         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_U] && joypad_0_buttons[BUTTON_D])
337                 joypad_0_buttons[BUTTON_D] = 0;
338         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_D] && joypad_0_buttons[BUTTON_U])
339                 joypad_0_buttons[BUTTON_U] = 0;
340
341         if (e->key() == (int)vjs.p2KeyBindings[BUTTON_L] && joypad_1_buttons[BUTTON_R])
342                 joypad_1_buttons[BUTTON_R] = 0;
343         if (e->key() == (int)vjs.p2KeyBindings[BUTTON_R] && joypad_1_buttons[BUTTON_L])
344                 joypad_1_buttons[BUTTON_L] = 0;
345         if (e->key() == (int)vjs.p2KeyBindings[BUTTON_U] && joypad_1_buttons[BUTTON_D])
346                 joypad_1_buttons[BUTTON_D] = 0;
347         if (e->key() == (int)vjs.p2KeyBindings[BUTTON_D] && joypad_1_buttons[BUTTON_U])
348                 joypad_1_buttons[BUTTON_U] = 0;
349 #endif
350
351         // No bad combos exist, let's stuff the emulator key buffers...!
352         for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
353         {
354                 if (e->key() == (int)vjs.p1KeyBindings[i])
355 //                      joypad_0_buttons[i] = (uint8)state;
356                         joypad_0_buttons[i] = (state ? 0x01 : 0x00);
357
358 // Pad #2 is screwing up pad #1. Prolly a problem in joystick.cpp...
359 // So let's try to fix it there. :-P [DONE]
360                 if (e->key() == (int)vjs.p2KeyBindings[i])
361 //                      joypad_1_buttons[i] = (uint8)state;
362                         joypad_1_buttons[i] = (state ? 0x01 : 0x00);
363         }
364 }
365
366 void MainWin::Open(void)
367 {
368 }
369
370 void MainWin::Configure(void)
371 {
372         // Call the configuration dialog and update settings
373         ConfigDialog dlg(this);
374         //ick.
375         dlg.generalTab->useUnknownSoftware->setChecked(allowUnknownSoftware);
376
377         if (dlg.exec() == false)
378                 return;
379
380         QString before = vjs.ROMPath;
381         QString alpineBefore = vjs.alpineROMPath;
382         QString absBefore = vjs.absROMPath;
383         bool audioBefore = vjs.audioEnabled;
384         dlg.UpdateVJSettings();
385         QString after = vjs.ROMPath;
386         QString alpineAfter = vjs.alpineROMPath;
387         QString absAfter = vjs.absROMPath;
388         bool audioAfter = vjs.audioEnabled;
389
390         bool allowOld = allowUnknownSoftware;
391         //ick.
392         allowUnknownSoftware = dlg.generalTab->useUnknownSoftware->isChecked();
393
394         // We rescan the "software" folder if the user either changed the path or
395         // checked/unchecked the "Allow unknown files" option in the config dialog.
396         if ((before != after) || (allowOld != allowUnknownSoftware))
397                 filePickWin->ScanSoftwareFolder(allowUnknownSoftware);
398
399         // If the "Alpine" ROM is changed, then let's load it...
400         if (alpineBefore != alpineAfter)
401         {
402                 if (!JaguarLoadFile(vjs.alpineROMPath) && !AlpineLoadFile(vjs.alpineROMPath))
403                 {
404                         // Oh crap, we couldn't get the file! Alert the media!
405                         QMessageBox msg;
406                         msg.setText(QString(tr("Could not load file \"%1\"!")).arg(vjs.alpineROMPath));
407                         msg.setIcon(QMessageBox::Warning);
408                         msg.exec();
409                 }
410         }
411
412         // If the "ABS" ROM is changed, then let's load it...
413         if (absBefore != absAfter)
414         {
415                 if (!JaguarLoadFile(vjs.absROMPath))
416                 {
417                         // Oh crap, we couldn't get the file! Alert the media!
418                         QMessageBox msg;
419                         msg.setText(QString(tr("Could not load file \"%1\"!")).arg(vjs.absROMPath));
420                         msg.setIcon(QMessageBox::Warning);
421                         msg.exec();
422                 }
423         }
424
425         // If the "Enable audio" checkbox changed, then we have to re-init the DAC...
426         if (audioBefore != audioAfter)
427         {
428                 DACDone();
429                 DACInit();
430         }
431
432         // Just in case we crash before a clean exit...
433         WriteSettings();
434 }
435
436 //
437 // Here's the main emulator loop
438 //
439 void MainWin::Timer(void)
440 {
441         if (!running)
442                 return;
443
444         if (showUntunedTankCircuit)
445         {
446                 // Random hash & trash
447                 // We try to simulate an untuned tank circuit here... :-)
448                 for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
449                 {
450                         for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
451                         {
452                                 videoWidget->buffer[(y * videoWidget->textureWidth) + x] = (rand() & 0xFF) << 8 | (rand() & 0xFF) << 16 | (rand() & 0xFF) << 24;// | (rand() & 0xFF);//0x000000FF;
453         //                      buffer[(y * textureWidth) + x] = x*y;
454                         }
455                 }
456         }
457         else
458         {
459                 // Otherwise, run the Jaguar simulation
460                 JaguarExecuteNew();
461 ////            memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->rasterWidth);
462 //No longer needed--see glwidget.cpp for details!
463 //              memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->textureWidth * sizeof(uint32_t));
464 ////            memcpy(surface->pixels, backbuffer, TOMGetVideoModeWidth() * TOMGetVideoModeHeight() * 4);
465         }
466
467         videoWidget->updateGL();
468 }
469
470 void MainWin::TogglePowerState(void)
471 {
472         powerButtonOn = !powerButtonOn;
473
474         // With the power off, we simulate white noise on the screen. :-)
475         if (!powerButtonOn)
476         {
477                 pauseAct->setChecked(false);
478                 pauseAct->setDisabled(true);
479                 showUntunedTankCircuit = true;
480                 running = true;
481                 // This is just in case the ROM we were playing was in a narrow or wide field mode,
482                 // so the untuned tank sim doesn't look wrong. :-)
483                 TOMReset();
484         }
485         else
486         {
487 //NOTE: Low hanging fruit: We can simplify this a lot...
488                 // Otherwise, we prepare for running regular software...
489                 if (!CDActive)
490                 {
491                         showUntunedTankCircuit = false;//(cartridgeLoaded ? false : true);
492                         pauseAct->setChecked(false);
493                         pauseAct->setDisabled(false);//!cartridgeLoaded);
494                 }
495                 // Or, set up for the Jaguar CD
496                 else
497                 {
498 // Should check for cartridgeLoaded here as well...!
499 // We can clear it when toggling CDActive on, so that when we power cycle it does the
500 // expected thing. Otherwise, if we use the file picker to insert a cart, we expect
501 // to run the cart! Maybe have a RemoveCart function that only works if the CD unit
502 // is active?
503                         showUntunedTankCircuit = false;
504                         pauseAct->setChecked(false);
505                         pauseAct->setDisabled(false);
506                         memcpy(jagMemSpace + 0x800000, jaguarCDBootROM, 0x40000);
507                         setWindowTitle(QString("Virtual Jaguar " VJ_RELEASE_VERSION
508                                 " - Now playing: Jaguar CD"));
509                 }
510
511                 WriteLog("GUI: Resetting Jaguar...\n");
512                 JaguarReset();
513                 running = true;
514         }
515 }
516
517 void MainWin::ToggleRunState(void)
518 {
519         running = !running;
520
521         if (!running)
522         {
523                 for(uint32_t i=0; i<(uint32_t)(videoWidget->textureWidth * 256); i++)
524                 {
525                         uint32_t pixel = backbuffer[i];
526                         uint8_t r = (pixel >> 24) & 0xFF, g = (pixel >> 16) & 0xFF, b = (pixel >> 8) & 0xFF;
527                         pixel = ((r + g + b) / 3) & 0x00FF;
528                         backbuffer[i] = 0x000000FF | (pixel << 16) | (pixel << 8);
529                 }
530
531                 memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->textureWidth * sizeof(uint32_t));
532
533                 videoWidget->updateGL();
534         }
535 }
536
537 void MainWin::SetZoom100(void)
538 {
539         zoomLevel = 1;
540         ResizeMainWindow();
541 }
542
543 void MainWin::SetZoom200(void)
544 {
545         zoomLevel = 2;
546         ResizeMainWindow();
547 }
548
549 void MainWin::SetZoom300(void)
550 {
551         zoomLevel = 3;
552         ResizeMainWindow();
553 }
554
555 void MainWin::SetNTSC(void)
556 {
557         vjs.hardwareTypeNTSC = true;
558         ResizeMainWindow();
559 }
560
561 void MainWin::SetPAL(void)
562 {
563         vjs.hardwareTypeNTSC = false;
564         ResizeMainWindow();
565 }
566
567 void MainWin::ToggleBlur(void)
568 {
569         vjs.glFilter = !vjs.glFilter;
570 }
571
572 void MainWin::ShowAboutWin(void)
573 {
574         aboutWin->show();
575 }
576
577 void MainWin::ShowHelpWin(void)
578 {
579         helpWin->show();
580 }
581
582 void MainWin::InsertCart(void)
583 {
584         // If the emulator is running, we pause it here and unpause it later
585         // if we dismiss the file selector without choosing anything
586         if (running && powerButtonOn)
587         {
588                 ToggleRunState();
589                 pauseForFileSelector = true;
590         }
591
592         filePickWin->show();
593 }
594
595 void MainWin::Unpause(void)
596 {
597         // Here we unpause the emulator if it was paused when we went into the file selector
598         if (pauseForFileSelector)
599         {
600                 pauseForFileSelector = false;
601                 ToggleRunState();
602         }
603 }
604
605 void MainWin::LoadSoftware(QString file)
606 {
607         running = false;                                                        // Prevent bad things(TM) from happening...
608         pauseForFileSelector = false;                           // Reset the file selector pause flag
609         SET32(jaguarMainRAM, 0, 0x00200000);            // Set top of stack...
610         cartridgeLoaded = JaguarLoadFile(file.toAscii().data());
611
612         char * biosPointer = jaguarBootROM;
613
614         if (vjs.hardwareTypeAlpine)
615                 biosPointer = jaguarDevBootROM2;
616
617         memcpy(jagMemSpace + 0xE00000, biosPointer, 0x20000);
618
619         powerAct->setDisabled(false);
620         powerAct->setChecked(true);
621         powerButtonOn = false;
622         TogglePowerState();
623
624         if (!vjs.hardwareTypeAlpine)
625         {
626                 QString newTitle = QString("Virtual Jaguar " VJ_RELEASE_VERSION " - Now playing: %1")
627                         .arg(filePickWin->GetSelectedPrettyName());
628                 setWindowTitle(newTitle);
629         }
630 }
631
632 void MainWin::ToggleCDUsage(void)
633 {
634         CDActive = !CDActive;
635
636 #if 0
637         if (CDActive)
638         {
639                 powerAct->setDisabled(false);
640         }
641         else
642         {
643                 powerAct->setDisabled(true);
644         }
645 #endif
646 }
647
648 void MainWin::ResizeMainWindow(void)
649 {
650         videoWidget->setFixedSize(zoomLevel * 320, zoomLevel * (vjs.hardwareTypeNTSC ? 240 : 256));
651         show();
652
653         for(int i=0; i<2; i++)
654         {
655                 resize(0, 0);
656                 usleep(2000);
657                 QApplication::processEvents();
658         }
659 }
660
661 void MainWin::ReadSettings(void)
662 {
663         QSettings settings("Underground Software", "Virtual Jaguar");
664         QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
665         QSize size = settings.value("size", QSize(400, 400)).toSize();
666         resize(size);
667         move(pos);
668         pos = settings.value("cartLoadPos", QPoint(200, 200)).toPoint();
669         filePickWin->move(pos);
670
671         zoomLevel = settings.value("zoom", 2).toInt();
672         allowUnknownSoftware = settings.value("showUnknownSoftware", false).toBool();
673
674         vjs.useJoystick      = settings.value("useJoystick", false).toBool();
675         vjs.joyport          = settings.value("joyport", 0).toInt();
676         vjs.hardwareTypeNTSC = settings.value("hardwareTypeNTSC", true).toBool();
677         vjs.frameSkip        = settings.value("frameSkip", 0).toInt();
678         vjs.useJaguarBIOS    = settings.value("useJaguarBIOS", false).toBool();
679         vjs.GPUEnabled       = settings.value("GPUEnabled", true).toBool();
680         vjs.DSPEnabled       = settings.value("DSPEnabled", false).toBool();
681         vjs.audioEnabled     = settings.value("audioEnabled", true).toBool();
682         vjs.usePipelinedDSP  = settings.value("usePipelinedDSP", false).toBool();
683         vjs.fullscreen       = settings.value("fullscreen", false).toBool();
684         vjs.useOpenGL        = settings.value("useOpenGL", true).toBool();
685         vjs.glFilter         = settings.value("glFilterType", 1).toInt();
686         vjs.renderType       = settings.value("renderType", 0).toInt();
687         vjs.allowWritesToROM = settings.value("writeROM", false).toBool();
688 //      strcpy(vjs.jagBootPath, settings.value("JagBootROM", "./bios/[BIOS] Atari Jaguar (USA, Europe).zip").toString().toAscii().data());
689 //      strcpy(vjs.CDBootPath, settings.value("CDBootROM", "./bios/jagcd.rom").toString().toAscii().data());
690         strcpy(vjs.EEPROMPath, settings.value("EEPROMs", "./eeproms/").toString().toAscii().data());
691         strcpy(vjs.ROMPath, settings.value("ROMs", "./software/").toString().toAscii().data());
692         strcpy(vjs.alpineROMPath, settings.value("DefaultROM", "").toString().toAscii().data());
693         strcpy(vjs.absROMPath, settings.value("DefaultABS", "").toString().toAscii().data());
694 WriteLog("MainWin: Paths\n");
695 //WriteLog("    jagBootPath = \"%s\"\n", vjs.jagBootPath);
696 //WriteLog("    CDBootPath  = \"%s\"\n", vjs.CDBootPath);
697 WriteLog("   EEPROMPath = \"%s\"\n", vjs.EEPROMPath);
698 WriteLog("      ROMPath = \"%s\"\n", vjs.ROMPath);
699 WriteLog("AlpineROMPath = \"%s\"\n", vjs.alpineROMPath);
700 WriteLog("   absROMPath = \"%s\"\n", vjs.absROMPath);
701
702         // Keybindings in order of U, D, L, R, C, B, A, Op, Pa, 0-9, #, *
703         vjs.p1KeyBindings[BUTTON_U] = settings.value("p1k_up", Qt::Key_S).toInt();
704         vjs.p1KeyBindings[BUTTON_D] = settings.value("p1k_down", Qt::Key_X).toInt();
705         vjs.p1KeyBindings[BUTTON_L] = settings.value("p1k_left", Qt::Key_A).toInt();
706         vjs.p1KeyBindings[BUTTON_R] = settings.value("p1k_right", Qt::Key_D).toInt();
707         vjs.p1KeyBindings[BUTTON_C] = settings.value("p1k_c", Qt::Key_J).toInt();
708         vjs.p1KeyBindings[BUTTON_B] = settings.value("p1k_b", Qt::Key_K).toInt();
709         vjs.p1KeyBindings[BUTTON_A] = settings.value("p1k_a", Qt::Key_L).toInt();
710         vjs.p1KeyBindings[BUTTON_OPTION] = settings.value("p1k_option", Qt::Key_O).toInt();
711         vjs.p1KeyBindings[BUTTON_PAUSE] = settings.value("p1k_pause", Qt::Key_P).toInt();
712         vjs.p1KeyBindings[BUTTON_0] = settings.value("p1k_0", Qt::Key_0).toInt();
713         vjs.p1KeyBindings[BUTTON_1] = settings.value("p1k_1", Qt::Key_1).toInt();
714         vjs.p1KeyBindings[BUTTON_2] = settings.value("p1k_2", Qt::Key_2).toInt();
715         vjs.p1KeyBindings[BUTTON_3] = settings.value("p1k_3", Qt::Key_3).toInt();
716         vjs.p1KeyBindings[BUTTON_4] = settings.value("p1k_4", Qt::Key_4).toInt();
717         vjs.p1KeyBindings[BUTTON_5] = settings.value("p1k_5", Qt::Key_5).toInt();
718         vjs.p1KeyBindings[BUTTON_6] = settings.value("p1k_6", Qt::Key_6).toInt();
719         vjs.p1KeyBindings[BUTTON_7] = settings.value("p1k_7", Qt::Key_7).toInt();
720         vjs.p1KeyBindings[BUTTON_8] = settings.value("p1k_8", Qt::Key_8).toInt();
721         vjs.p1KeyBindings[BUTTON_9] = settings.value("p1k_9", Qt::Key_9).toInt();
722         vjs.p1KeyBindings[BUTTON_d] = settings.value("p1k_pound", Qt::Key_Minus).toInt();
723         vjs.p1KeyBindings[BUTTON_s] = settings.value("p1k_star", Qt::Key_Equal).toInt();
724
725         vjs.p2KeyBindings[BUTTON_U] = settings.value("p2k_up", Qt::Key_Up).toInt();
726         vjs.p2KeyBindings[BUTTON_D] = settings.value("p2k_down", Qt::Key_Down).toInt();
727         vjs.p2KeyBindings[BUTTON_L] = settings.value("p2k_left", Qt::Key_Left).toInt();
728         vjs.p2KeyBindings[BUTTON_R] = settings.value("p2k_right", Qt::Key_Right).toInt();
729         vjs.p2KeyBindings[BUTTON_C] = settings.value("p2k_c", Qt::Key_Z).toInt();
730         vjs.p2KeyBindings[BUTTON_B] = settings.value("p2k_b", Qt::Key_X).toInt();
731         vjs.p2KeyBindings[BUTTON_A] = settings.value("p2k_a", Qt::Key_C).toInt();
732         vjs.p2KeyBindings[BUTTON_OPTION] = settings.value("p2k_option", Qt::Key_Apostrophe).toInt();
733         vjs.p2KeyBindings[BUTTON_PAUSE] = settings.value("p2k_pause", Qt::Key_Return).toInt();
734         vjs.p2KeyBindings[BUTTON_0] = settings.value("p2k_0", Qt::Key_0).toInt();
735         vjs.p2KeyBindings[BUTTON_1] = settings.value("p2k_1", Qt::Key_1).toInt();
736         vjs.p2KeyBindings[BUTTON_2] = settings.value("p2k_2", Qt::Key_2).toInt();
737         vjs.p2KeyBindings[BUTTON_3] = settings.value("p2k_3", Qt::Key_3).toInt();
738         vjs.p2KeyBindings[BUTTON_4] = settings.value("p2k_4", Qt::Key_4).toInt();
739         vjs.p2KeyBindings[BUTTON_5] = settings.value("p2k_5", Qt::Key_5).toInt();
740         vjs.p2KeyBindings[BUTTON_6] = settings.value("p2k_6", Qt::Key_6).toInt();
741         vjs.p2KeyBindings[BUTTON_7] = settings.value("p2k_7", Qt::Key_7).toInt();
742         vjs.p2KeyBindings[BUTTON_8] = settings.value("p2k_8", Qt::Key_8).toInt();
743         vjs.p2KeyBindings[BUTTON_9] = settings.value("p2k_9", Qt::Key_9).toInt();
744         vjs.p2KeyBindings[BUTTON_d] = settings.value("p2k_pound", Qt::Key_Slash).toInt();
745         vjs.p2KeyBindings[BUTTON_s] = settings.value("p2k_star", Qt::Key_Asterisk).toInt();
746 }
747
748 void MainWin::WriteSettings(void)
749 {
750         QSettings settings("Underground Software", "Virtual Jaguar");
751         settings.setValue("pos", pos());
752         settings.setValue("size", size());
753         settings.setValue("cartLoadPos", filePickWin->pos());
754
755         settings.setValue("zoom", zoomLevel);
756         settings.setValue("showUnknownSoftware", allowUnknownSoftware);
757
758         settings.setValue("useJoystick", vjs.useJoystick);
759         settings.setValue("joyport", vjs.joyport);
760         settings.setValue("hardwareTypeNTSC", vjs.hardwareTypeNTSC);
761         settings.setValue("frameSkip", vjs.frameSkip);
762         settings.setValue("useJaguarBIOS", vjs.useJaguarBIOS);
763         settings.setValue("GPUEnabled", vjs.GPUEnabled);
764         settings.setValue("DSPEnabled", vjs.DSPEnabled);
765         settings.setValue("audioEnabled", vjs.audioEnabled);
766         settings.setValue("usePipelinedDSP", vjs.usePipelinedDSP);
767         settings.setValue("fullscreen", vjs.fullscreen);
768         settings.setValue("useOpenGL", vjs.useOpenGL);
769         settings.setValue("glFilterType", vjs.glFilter);
770         settings.setValue("renderType", vjs.renderType);
771         settings.setValue("writeROM", vjs.allowWritesToROM);
772         settings.setValue("JagBootROM", vjs.jagBootPath);
773         settings.setValue("CDBootROM", vjs.CDBootPath);
774         settings.setValue("EEPROMs", vjs.EEPROMPath);
775         settings.setValue("ROMs", vjs.ROMPath);
776         settings.setValue("DefaultROM", vjs.alpineROMPath);
777         settings.setValue("DefaultABS", vjs.absROMPath);
778
779         settings.setValue("p1k_up", vjs.p1KeyBindings[BUTTON_U]);
780         settings.setValue("p1k_down", vjs.p1KeyBindings[BUTTON_D]);
781         settings.setValue("p1k_left", vjs.p1KeyBindings[BUTTON_L]);
782         settings.setValue("p1k_right", vjs.p1KeyBindings[BUTTON_R]);
783         settings.setValue("p1k_c", vjs.p1KeyBindings[BUTTON_C]);
784         settings.setValue("p1k_b", vjs.p1KeyBindings[BUTTON_B]);
785         settings.setValue("p1k_a", vjs.p1KeyBindings[BUTTON_A]);
786         settings.setValue("p1k_option", vjs.p1KeyBindings[BUTTON_OPTION]);
787         settings.setValue("p1k_pause", vjs.p1KeyBindings[BUTTON_PAUSE]);
788         settings.setValue("p1k_0", vjs.p1KeyBindings[BUTTON_0]);
789         settings.setValue("p1k_1", vjs.p1KeyBindings[BUTTON_1]);
790         settings.setValue("p1k_2", vjs.p1KeyBindings[BUTTON_2]);
791         settings.setValue("p1k_3", vjs.p1KeyBindings[BUTTON_3]);
792         settings.setValue("p1k_4", vjs.p1KeyBindings[BUTTON_4]);
793         settings.setValue("p1k_5", vjs.p1KeyBindings[BUTTON_5]);
794         settings.setValue("p1k_6", vjs.p1KeyBindings[BUTTON_6]);
795         settings.setValue("p1k_7", vjs.p1KeyBindings[BUTTON_7]);
796         settings.setValue("p1k_8", vjs.p1KeyBindings[BUTTON_8]);
797         settings.setValue("p1k_9", vjs.p1KeyBindings[BUTTON_9]);
798         settings.setValue("p1k_pound", vjs.p1KeyBindings[BUTTON_d]);
799         settings.setValue("p1k_star", vjs.p1KeyBindings[BUTTON_s]);
800
801         settings.setValue("p2k_up", vjs.p2KeyBindings[BUTTON_U]);
802         settings.setValue("p2k_down", vjs.p2KeyBindings[BUTTON_D]);
803         settings.setValue("p2k_left", vjs.p2KeyBindings[BUTTON_L]);
804         settings.setValue("p2k_right", vjs.p2KeyBindings[BUTTON_R]);
805         settings.setValue("p2k_c", vjs.p2KeyBindings[BUTTON_C]);
806         settings.setValue("p2k_b", vjs.p2KeyBindings[BUTTON_B]);
807         settings.setValue("p2k_a", vjs.p2KeyBindings[BUTTON_A]);
808         settings.setValue("p2k_option", vjs.p2KeyBindings[BUTTON_OPTION]);
809         settings.setValue("p2k_pause", vjs.p2KeyBindings[BUTTON_PAUSE]);
810         settings.setValue("p2k_0", vjs.p2KeyBindings[BUTTON_0]);
811         settings.setValue("p2k_1", vjs.p2KeyBindings[BUTTON_1]);
812         settings.setValue("p2k_2", vjs.p2KeyBindings[BUTTON_2]);
813         settings.setValue("p2k_3", vjs.p2KeyBindings[BUTTON_3]);
814         settings.setValue("p2k_4", vjs.p2KeyBindings[BUTTON_4]);
815         settings.setValue("p2k_5", vjs.p2KeyBindings[BUTTON_5]);
816         settings.setValue("p2k_6", vjs.p2KeyBindings[BUTTON_6]);
817         settings.setValue("p2k_7", vjs.p2KeyBindings[BUTTON_7]);
818         settings.setValue("p2k_8", vjs.p2KeyBindings[BUTTON_8]);
819         settings.setValue("p2k_9", vjs.p2KeyBindings[BUTTON_9]);
820         settings.setValue("p2k_pound", vjs.p2KeyBindings[BUTTON_d]);
821         settings.setValue("p2k_star", vjs.p2KeyBindings[BUTTON_s]);
822 }
823
824 // Here's how Byuu does it...
825 // I think I have it working now... :-)
826 #if 0
827 void Utility::resizeMainWindow()
828 {
829   unsigned region = config().video.context->region;
830   unsigned multiplier = config().video.context->multiplier;
831   unsigned width = 256 * multiplier;
832   unsigned height = (region == 0 ? 224 : 239) * multiplier;
833
834   if(config().video.context->correctAspectRatio)
835   {
836     if(region == 0)
837         {
838       width = (double)width * config().video.ntscAspectRatio + 0.5;  //NTSC adjust
839     }
840         else
841         {
842       width = (double)width * config().video.palAspectRatio  + 0.5;  //PAL adjust
843     }
844   }
845
846   if(config().video.isFullscreen == false)
847   {
848     //get effective desktop work area region (ignore Windows taskbar, OS X dock, etc.)
849     QRect deskRect = QApplication::desktop()->availableGeometry(mainWindow);
850
851     //ensure window size will not be larger than viewable desktop area
852     constrainSize(height, width, deskRect.height()); //- frameHeight);
853     constrainSize(width, height, deskRect.width());  //- frameWidth );
854
855     mainWindow->canvas->setFixedSize(width, height);
856     mainWindow->show();
857   }
858   else
859   {
860     for(unsigned i = 0; i < 2; i++)
861         {
862       unsigned iWidth = width, iHeight = height;
863
864       constrainSize(iHeight, iWidth, mainWindow->canvasContainer->size().height());
865       constrainSize(iWidth, iHeight, mainWindow->canvasContainer->size().width());
866
867       //center canvas onscreen; ensure it is not larger than viewable area
868       mainWindow->canvas->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
869       mainWindow->canvas->setFixedSize(iWidth, iHeight);
870       mainWindow->canvas->setMinimumSize(0, 0);
871
872       usleep(2000);
873       QApplication::processEvents();
874     }
875   }
876
877   //workaround for Qt/Xlib bug:
878   //if window resize occurs with cursor over it, Qt shows Qt::Size*DiagCursor;
879   //so force it to show Qt::ArrowCursor, as expected
880   mainWindow->setCursor(Qt::ArrowCursor);
881   mainWindow->canvasContainer->setCursor(Qt::ArrowCursor);
882   mainWindow->canvas->setCursor(Qt::ArrowCursor);
883
884   //workaround for DirectSound(?) bug:
885   //window resizing sometimes breaks audio sync, this call re-initializes it
886   updateAvSync();
887 }
888
889 void Utility::setScale(unsigned scale)
890 {
891   config().video.context->multiplier = scale;
892   resizeMainWindow();
893   mainWindow->shrink();
894   mainWindow->syncUi();
895 }
896
897 void QbWindow::shrink()
898 {
899   if(config().video.isFullscreen == false)
900   {
901     for(unsigned i = 0; i < 2; i++)
902         {
903       resize(0, 0);
904       usleep(2000);
905       QApplication::processEvents();
906     }
907   }
908 }
909 #endif