]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/mainwin.cpp
Added FPS counter.
[virtualjaguar] / src / gui / mainwin.cpp
1 //
2 // mainwin.cpp - Qt-based GUI for Virtual Jaguar: Main Application Window
3 // by James Hammons
4 // (C) 2009 Underground Software
5 //
6 // JLH = James Hammons <jlhamm@acm.org>
7 //
8 // Who  When        What
9 // ---  ----------  -------------------------------------------------------------
10 // JLH  12/23/2009  Created this file
11 // JLH  12/20/2010  Added settings, menus & toolbars
12 // JLH  07/05/2011  Added CD BIOS functionality to GUI
13 //
14
15 // FIXED:
16 //
17 // - Add dbl click/enter to select in cart list, ESC to dimiss [DONE]
18 // - Autoscan/autoload all available BIOS from 'software' folder [DONE]
19 // - Add 1 key jumping in cartridge list (press 'R', jumps to carts starting with 'R', etc) [DONE]
20 // - Controller configuration [DONE]
21 //
22 // STILL TO BE DONE:
23 //
24 // - Fix bug in switching between PAL & NTSC in fullscreen mode.
25 // - Remove SDL dependencies (sound, mainly) from Jaguar core lib
26 // - Fix inconsistency with trailing slashes in paths (eeproms needs one, software doesn't)
27 //
28 // SFDX CODE: S1E9T8H5M23YS
29
30 // Uncomment this for debugging...
31 //#define DEBUG
32 //#define DEBUGFOO                      // Various tool debugging...
33 //#define DEBUGTP                               // Toolpalette debugging...
34
35 #include "mainwin.h"
36
37 #include "SDL.h"
38 #include "app.h"
39 #include "about.h"
40 #include "configdialog.h"
41 #include "filepicker.h"
42 #include "gamepad.h"
43 #include "generaltab.h"
44 #include "glwidget.h"
45 #include "help.h"
46 #include "settings.h"
47 #include "version.h"
48 #include "debug/cpubrowser.h"
49 #include "debug/m68kdasmbrowser.h"
50 #include "debug/memorybrowser.h"
51 #include "debug/opbrowser.h"
52 #include "debug/riscdasmbrowser.h"
53
54 #include "dac.h"
55 #include "jaguar.h"
56 #include "log.h"
57 #include "file.h"
58 #include "jagbios.h"
59 #include "jagbios2.h"
60 #include "jagcdbios.h"
61 #include "jagstub2bios.h"
62 #include "joystick.h"
63 #include "m68000/m68kinterface.h"
64
65 // According to SebRmv, this header isn't seen on Arch Linux either... :-/
66 //#ifdef __GCCWIN32__
67 // Apparently on win32, usleep() is not pulled in by the usual suspects.
68 #include <unistd.h>
69 //#endif
70
71 // The way BSNES controls things is by setting a timer with a zero
72 // timeout, sleeping if not emulating anything. Seems there has to be a
73 // better way.
74
75 // It has a novel approach to plugging-in/using different video/audio/input
76 // methods, can we do something similar or should we just use the built-in
77 // QOpenGL?
78
79 // We're going to try to use the built-in OpenGL support and see how it goes.
80 // We'll make the VJ core modular so that it doesn't matter what GUI is in
81 // use, we can drop it in anywhere and use it as-is.
82
83 MainWin::MainWin(bool autoRun): running(true), powerButtonOn(false),
84         showUntunedTankCircuit(true), cartridgeLoaded(false), CDActive(false),
85         pauseForFileSelector(false), loadAndGo(autoRun), plzDontKillMyComputer(false)
86 {
87         debugbar = NULL;
88
89         for(int i=0; i<8; i++)
90                 keyHeld[i] = false;
91
92         // FPS management
93         for(int i=0; i<RING_BUFFER_SIZE; i++)
94                 ringBuffer[i] = 0;
95
96         ringBufferPointer = RING_BUFFER_SIZE - 1;
97
98         videoWidget = new GLWidget(this);
99         setCentralWidget(videoWidget);
100         setWindowIcon(QIcon(":/res/vj-icon.png"));
101
102         QString title = QString(tr("Virtual Jaguar " VJ_RELEASE_VERSION ));
103
104         if (vjs.hardwareTypeAlpine)
105                 title += QString(tr(" - Alpine Mode"));
106
107         setWindowTitle(title);
108
109         aboutWin = new AboutWindow(this);
110         helpWin = new HelpWindow(this);
111         filePickWin = new FilePickerWindow(this);
112         memBrowseWin = new MemoryBrowserWindow(this);
113         cpuBrowseWin = new CPUBrowserWindow(this);
114         opBrowseWin = new OPBrowserWindow(this);
115         m68kDasmBrowseWin = new M68KDasmBrowserWindow(this);
116         riscDasmBrowseWin = new RISCDasmBrowserWindow(this);
117
118         videoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
119         setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
120
121         setUnifiedTitleAndToolBarOnMac(true);
122
123         // Create actions
124
125         quitAppAct = new QAction(tr("E&xit"), this);
126 //      quitAppAct->setShortcuts(QKeySequence::Quit);
127 //      quitAppAct->setShortcut(QKeySequence(tr("Alt+x")));
128         quitAppAct->setShortcut(QKeySequence(tr("Ctrl+q")));
129         quitAppAct->setShortcutContext(Qt::ApplicationShortcut);
130         quitAppAct->setStatusTip(tr("Quit Virtual Jaguar"));
131         connect(quitAppAct, SIGNAL(triggered()), this, SLOT(close()));
132
133         powerGreen.addFile(":/res/power-off.png", QSize(), QIcon::Normal, QIcon::Off);
134         powerGreen.addFile(":/res/power-on-green.png", QSize(), QIcon::Normal, QIcon::On);
135         powerRed.addFile(":/res/power-off.png", QSize(), QIcon::Normal, QIcon::Off);
136         powerRed.addFile(":/res/power-on-red.png", QSize(), QIcon::Normal, QIcon::On);
137
138 //      powerAct = new QAction(QIcon(":/res/power.png"), tr("&Power"), this);
139         powerAct = new QAction(powerGreen, tr("&Power"), this);
140         powerAct->setStatusTip(tr("Powers Jaguar on/off"));
141         powerAct->setCheckable(true);
142         powerAct->setChecked(false);
143 //      powerAct->setDisabled(true);
144         connect(powerAct, SIGNAL(triggered()), this, SLOT(TogglePowerState()));
145
146         QIcon pauseIcon;
147         pauseIcon.addFile(":/res/pause-off", QSize(), QIcon::Normal, QIcon::Off);
148         pauseIcon.addFile(":/res/pause-on", QSize(), QIcon::Normal, QIcon::On);
149 //      pauseAct = new QAction(QIcon(":/res/pause.png"), tr("Pause"), this);
150         pauseAct = new QAction(pauseIcon, tr("Pause"), this);
151         pauseAct->setStatusTip(tr("Toggles the running state"));
152         pauseAct->setCheckable(true);
153         pauseAct->setDisabled(true);
154         pauseAct->setShortcut(QKeySequence(tr("Esc")));
155         pauseAct->setShortcutContext(Qt::ApplicationShortcut);
156         connect(pauseAct, SIGNAL(triggered()), this, SLOT(ToggleRunState()));
157
158         zoomActs = new QActionGroup(this);
159
160         x1Act = new QAction(QIcon(":/res/zoom100.png"), tr("Zoom 100%"), zoomActs);
161         x1Act->setStatusTip(tr("Set window zoom to 100%"));
162         x1Act->setCheckable(true);
163         connect(x1Act, SIGNAL(triggered()), this, SLOT(SetZoom100()));
164
165         x2Act = new QAction(QIcon(":/res/zoom200.png"), tr("Zoom 200%"), zoomActs);
166         x2Act->setStatusTip(tr("Set window zoom to 200%"));
167         x2Act->setCheckable(true);
168         connect(x2Act, SIGNAL(triggered()), this, SLOT(SetZoom200()));
169
170         x3Act = new QAction(QIcon(":/res/zoom300.png"), tr("Zoom 300%"), zoomActs);
171         x3Act->setStatusTip(tr("Set window zoom to 300%"));
172         x3Act->setCheckable(true);
173         connect(x3Act, SIGNAL(triggered()), this, SLOT(SetZoom300()));
174
175         tvTypeActs = new QActionGroup(this);
176
177         ntscAct = new QAction(QIcon(":/res/ntsc.png"), tr("NTSC"), tvTypeActs);
178         ntscAct->setStatusTip(tr("Sets Jaguar to NTSC mode"));
179         ntscAct->setCheckable(true);
180         connect(ntscAct, SIGNAL(triggered()), this, SLOT(SetNTSC()));
181
182         palAct = new QAction(QIcon(":/res/pal.png"), tr("PAL"), tvTypeActs);
183         palAct->setStatusTip(tr("Sets Jaguar to PAL mode"));
184         palAct->setCheckable(true);
185         connect(palAct, SIGNAL(triggered()), this, SLOT(SetPAL()));
186
187         blurAct = new QAction(QIcon(":/res/blur.png"), tr("Blur"), this);
188         blurAct->setStatusTip(tr("Sets OpenGL rendering to GL_NEAREST"));
189         blurAct->setCheckable(true);
190         connect(blurAct, SIGNAL(triggered()), this, SLOT(ToggleBlur()));
191
192         aboutAct = new QAction(QIcon(":/res/vj-icon.png"), tr("&About..."), this);
193         aboutAct->setStatusTip(tr("Blatant self-promotion"));
194         connect(aboutAct, SIGNAL(triggered()), this, SLOT(ShowAboutWin()));
195
196         helpAct = new QAction(QIcon(":/res/vj-icon.png"), tr("&Contents..."), this);
197         helpAct->setStatusTip(tr("Help is available, if you should need it"));
198         connect(helpAct, SIGNAL(triggered()), this, SLOT(ShowHelpWin()));
199
200         filePickAct = new QAction(QIcon(":/res/software.png"), tr("&Insert Cartridge..."), this);
201         filePickAct->setStatusTip(tr("Insert a cartridge into Virtual Jaguar"));
202         filePickAct->setShortcut(QKeySequence(tr("Ctrl+i")));
203         filePickAct->setShortcutContext(Qt::ApplicationShortcut);
204         connect(filePickAct, SIGNAL(triggered()), this, SLOT(InsertCart()));
205
206         configAct = new QAction(QIcon(":/res/wrench.png"), tr("&Configure"), this);
207         configAct->setStatusTip(tr("Configure options for Virtual Jaguar"));
208         configAct->setShortcut(QKeySequence(tr("Ctrl+c")));
209         configAct->setShortcutContext(Qt::ApplicationShortcut);
210         connect(configAct, SIGNAL(triggered()), this, SLOT(Configure()));
211
212         useCDAct = new QAction(QIcon(":/res/compact-disc.png"), tr("&Use CD Unit"), this);
213         useCDAct->setStatusTip(tr("Use Jaguar Virtual CD unit"));
214 //      useCDAct->setShortcut(QKeySequence(tr("Ctrl+c")));
215         useCDAct->setCheckable(true);
216         connect(useCDAct, SIGNAL(triggered()), this, SLOT(ToggleCDUsage()));
217
218         frameAdvanceAct = new QAction(QIcon(":/res/frame-advance.png"), tr("&Frame Advance"), this);
219         frameAdvanceAct->setShortcut(QKeySequence(tr("F7")));
220         frameAdvanceAct->setShortcutContext(Qt::ApplicationShortcut);
221         frameAdvanceAct->setDisabled(true);
222         connect(frameAdvanceAct, SIGNAL(triggered()), this, SLOT(FrameAdvance()));
223
224         fullScreenAct = new QAction(QIcon(":/res/fullscreen.png"), tr("F&ull Screen"), this);
225         fullScreenAct->setShortcut(QKeySequence(tr("F9")));
226         fullScreenAct->setShortcutContext(Qt::ApplicationShortcut);
227         fullScreenAct->setCheckable(true);
228         connect(fullScreenAct, SIGNAL(triggered()), this, SLOT(ToggleFullScreen()));
229
230         // Debugger Actions
231         memBrowseAct = new QAction(QIcon(":/res/tool-memory.png"), tr("Memory Browser"), this);
232         memBrowseAct->setStatusTip(tr("Shows the Jaguar memory browser window"));
233 //      memBrowseAct->setCheckable(true);
234         connect(memBrowseAct, SIGNAL(triggered()), this, SLOT(ShowMemoryBrowserWin()));
235
236         cpuBrowseAct = new QAction(QIcon(":/res/tool-cpu.png"), tr("CPU Browser"), this);
237         cpuBrowseAct->setStatusTip(tr("Shows the Jaguar CPU browser window"));
238 //      memBrowseAct->setCheckable(true);
239         connect(cpuBrowseAct, SIGNAL(triggered()), this, SLOT(ShowCPUBrowserWin()));
240
241         opBrowseAct = new QAction(QIcon(":/res/tool-op.png"), tr("OP Browser"), this);
242         opBrowseAct->setStatusTip(tr("Shows the Jaguar OP browser window"));
243 //      memBrowseAct->setCheckable(true);
244         connect(opBrowseAct, SIGNAL(triggered()), this, SLOT(ShowOPBrowserWin()));
245
246         m68kDasmBrowseAct = new QAction(QIcon(":/res/tool-68k-dis.png"), tr("68K Listing Browser"), this);
247         m68kDasmBrowseAct->setStatusTip(tr("Shows the 68K disassembly browser window"));
248 //      memBrowseAct->setCheckable(true);
249         connect(m68kDasmBrowseAct, SIGNAL(triggered()), this, SLOT(ShowM68KDasmBrowserWin()));
250
251         riscDasmBrowseAct = new QAction(QIcon(":/res/tool-risc-dis.png"), tr("RISC Listing Browser"), this);
252         riscDasmBrowseAct->setStatusTip(tr("Shows the RISC disassembly browser window"));
253 //      memBrowseAct->setCheckable(true);
254         connect(riscDasmBrowseAct, SIGNAL(triggered()), this, SLOT(ShowRISCDasmBrowserWin()));
255
256         // Misc. connections...
257         connect(filePickWin, SIGNAL(RequestLoad(QString)), this, SLOT(LoadSoftware(QString)));
258         connect(filePickWin, SIGNAL(FilePickerHiding()), this, SLOT(Unpause()));
259
260         // Create menus & toolbars
261
262         fileMenu = menuBar()->addMenu(tr("&Jaguar"));
263         fileMenu->addAction(powerAct);
264         fileMenu->addAction(pauseAct);
265 //      fileMenu->addAction(frameAdvanceAct);
266         fileMenu->addAction(filePickAct);
267         fileMenu->addAction(useCDAct);
268         fileMenu->addAction(configAct);
269         fileMenu->addAction(quitAppAct);
270
271         if (vjs.hardwareTypeAlpine)
272         {
273                 debugMenu = menuBar()->addMenu(tr("&Debug"));
274                 debugMenu->addAction(memBrowseAct);
275                 debugMenu->addAction(cpuBrowseAct);
276                 debugMenu->addAction(opBrowseAct);
277                 debugMenu->addAction(m68kDasmBrowseAct);
278                 debugMenu->addAction(riscDasmBrowseAct);
279         }
280
281         helpMenu = menuBar()->addMenu(tr("&Help"));
282         helpMenu->addAction(helpAct);
283         helpMenu->addAction(aboutAct);
284
285         toolbar = addToolBar(tr("Stuff"));
286         toolbar->addAction(powerAct);
287         toolbar->addAction(pauseAct);
288         toolbar->addAction(frameAdvanceAct);
289         toolbar->addAction(filePickAct);
290         toolbar->addAction(useCDAct);
291         toolbar->addSeparator();
292         toolbar->addAction(x1Act);
293         toolbar->addAction(x2Act);
294         toolbar->addAction(x3Act);
295         toolbar->addSeparator();
296         toolbar->addAction(ntscAct);
297         toolbar->addAction(palAct);
298         toolbar->addSeparator();
299         toolbar->addAction(blurAct);
300         toolbar->addAction(fullScreenAct);
301
302         if (vjs.hardwareTypeAlpine)
303         {
304                 debugbar = addToolBar(tr("&Debug"));
305                 debugbar->addAction(memBrowseAct);
306                 debugbar->addAction(cpuBrowseAct);
307                 debugbar->addAction(opBrowseAct);
308                 debugbar->addAction(m68kDasmBrowseAct);
309                 debugbar->addAction(riscDasmBrowseAct);
310         }
311
312         // Add actions to the main window, as hiding widgets with them
313         // disables them :-P
314         addAction(fullScreenAct);
315         addAction(quitAppAct);
316         addAction(configAct);
317         addAction(pauseAct);
318         addAction(filePickAct);
319         addAction(frameAdvanceAct);
320
321         //      Create status bar
322         statusBar()->showMessage(tr("Ready"));
323
324         ReadSettings();
325
326         // Do this in case original size isn't correct (mostly for the first-run case)
327         ResizeMainWindow();
328
329         // Create our test pattern bitmap
330         QImage tempImg(":/res/test-pattern.jpg");
331         QImage tempImgScaled = tempImg.scaled(VIRTUAL_SCREEN_WIDTH, VIRTUAL_SCREEN_HEIGHT_PAL, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
332
333         for(uint32_t y=0; y<VIRTUAL_SCREEN_HEIGHT_PAL; y++)
334         {
335                 const QRgb * scanline = (QRgb *)tempImgScaled.constScanLine(y);
336
337                 for(uint32_t x=0; x<VIRTUAL_SCREEN_WIDTH; x++)
338                 {
339                         uint32_t pixel = (qRed(scanline[x]) << 24) | (qGreen(scanline[x]) << 16) | (qBlue(scanline[x]) << 8) | 0xFF;
340                         testPattern[(y * VIRTUAL_SCREEN_WIDTH) + x] = pixel;
341                 }
342         }
343
344         // Set up timer based loop for animation...
345         timer = new QTimer(this);
346         connect(timer, SIGNAL(timeout()), this, SLOT(Timer()));
347
348         // This isn't very accurate for NTSC: This is early by 40 msec per frame.
349         // This is because it's discarding the 0.6666... on the end of the fraction.
350         // Alas, 6 doesn't divide cleanly into 10. :-P
351 //Should we defer this until SyncUI? Probably.
352 //No, it doesn't work, because it uses setInterval() instead of start()...
353 //      timer->start(vjs.hardwareTypeNTSC ? 16 : 20);
354
355         // We set this initially, to make VJ behave somewhat as it would if no
356         // cart were inserted and the BIOS was set as active...
357         jaguarCartInserted = true;
358         WriteLog("Virtual Jaguar %s (Last full build was on %s %s)\n", VJ_RELEASE_VERSION, __DATE__, __TIME__);
359         WriteLog("VJ: Initializing jaguar subsystem...\n");
360         JaguarInit();
361 //      memcpy(jagMemSpace + 0xE00000, jaguarBootROM, 0x20000); // Use the stock BIOS
362         memcpy(jagMemSpace + 0xE00000, (vjs.biosType == BT_K_SERIES ? jaguarBootROM : jaguarBootROM2), 0x20000);        // Use the stock BIOS
363
364         // Prevent the file scanner from running if filename passed
365         // in on the command line...
366         if (autoRun)
367                 return;
368
369         // Load up the default ROM if in Alpine mode:
370         if (vjs.hardwareTypeAlpine)
371         {
372                 bool romLoaded = JaguarLoadFile(vjs.alpineROMPath);
373
374                 // If regular load failed, try just a straight file load
375                 // (Dev only! I don't want people to start getting lazy with their releases again! :-P)
376                 if (!romLoaded)
377                         romLoaded = AlpineLoadFile(vjs.alpineROMPath);
378
379                 if (romLoaded)
380                         WriteLog("Alpine Mode: Successfully loaded file \"%s\".\n", vjs.alpineROMPath);
381                 else
382                         WriteLog("Alpine Mode: Unable to load file \"%s\"!\n", vjs.alpineROMPath);
383
384                 // Attempt to load/run the ABS file...
385                 LoadSoftware(vjs.absROMPath);
386                 memcpy(jagMemSpace + 0xE00000, jaguarDevBootROM2, 0x20000);     // Use the stub BIOS
387                 // Prevent the scanner from running...
388                 return;
389         }
390
391         // Run the scanner if nothing passed in and *not* Alpine mode...
392         // NB: Really need to look into caching the info scanned in here...
393         filePickWin->ScanSoftwareFolder(allowUnknownSoftware);
394 }
395
396
397 void MainWin::LoadFile(QString file)
398 {
399         LoadSoftware(file);
400 }
401
402
403 void MainWin::SyncUI(void)
404 {
405         // Set toolbar buttons/menus based on settings read in (sync the UI)...
406         // (Really, this is to sync command line options passed in)
407         blurAct->setChecked(vjs.glFilter);
408         x1Act->setChecked(zoomLevel == 1);
409         x2Act->setChecked(zoomLevel == 2);
410         x3Act->setChecked(zoomLevel == 3);
411 //      running = powerAct->isChecked();
412         ntscAct->setChecked(vjs.hardwareTypeNTSC);
413         palAct->setChecked(!vjs.hardwareTypeNTSC);
414         powerAct->setIcon(vjs.hardwareTypeNTSC ? powerRed : powerGreen);
415
416         fullScreenAct->setChecked(vjs.fullscreen);
417         fullScreen = vjs.fullscreen;
418         SetFullScreen(fullScreen);
419
420         // Reset the timer to be what was set in the command line (if any):
421 //      timer->setInterval(vjs.hardwareTypeNTSC ? 16 : 20);
422         timer->start(vjs.hardwareTypeNTSC ? 16 : 20);
423 }
424
425
426 void MainWin::closeEvent(QCloseEvent * event)
427 {
428         JaguarDone();
429 // This should only be done by the config dialog
430 //      WriteSettings();
431         WriteUISettings();
432         event->accept(); // ignore() if can't close for some reason
433 }
434
435
436 void MainWin::keyPressEvent(QKeyEvent * e)
437 {
438         // From jaguar.cpp
439         extern bool startM68KTracing;
440
441         // We ignore the Alt key for now, since it causes problems with the GUI
442         if (e->key() == Qt::Key_Alt)
443         {
444                 e->accept();
445                 return;
446         }
447         else if (e->key() == Qt::Key_F11)
448         {
449                 startM68KTracing = true;
450                 e->accept();
451                 return;
452         }
453
454 /*
455         if (e->key() == Qt::Key_F9)
456         {
457                 ToggleFullScreen();
458                 return;
459         }
460 */
461         HandleKeys(e, true);
462 }
463
464
465 void MainWin::keyReleaseEvent(QKeyEvent * e)
466 {
467         // We ignore the Alt key for now, since it causes problems with the GUI
468         if (e->key() == Qt::Key_Alt)
469         {
470                 e->accept();
471                 return;
472         }
473
474         HandleKeys(e, false);
475 }
476
477
478 void MainWin::HandleKeys(QKeyEvent * e, bool state)
479 {
480         enum { P1LEFT = 0, P1RIGHT, P1UP, P1DOWN, P2LEFT, P2RIGHT, P2UP, P2DOWN };
481         // We kill bad key combos here, before they can get to the emulator...
482         // This also kills the illegal instruction problem that cropped up in Rayman!
483         // May want to do this by killing the old one instead of ignoring the new one...
484         // Seems to work better that way...
485
486 // The problem with this approach is that it causes bad results because it doesn't do
487 // any checking of previous states. Need to come up with something better because this
488 // causes problems where the keyboard acts as if it were unresponsive. :-P
489 #if 0
490         if ((e->key() == vjs.p1KeyBindings[BUTTON_L] && joypad_0_buttons[BUTTON_R])
491                 || (e->key() == vjs.p1KeyBindings[BUTTON_R] && joypad_0_buttons[BUTTON_L])
492                 || (e->key() == vjs.p1KeyBindings[BUTTON_U] && joypad_0_buttons[BUTTON_D])
493                 || (e->key() == vjs.p1KeyBindings[BUTTON_D] && joypad_0_buttons[BUTTON_U]))
494                 return;
495 #else
496 #if 0
497         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_L] && joypad_0_buttons[BUTTON_R])
498                 joypad_0_buttons[BUTTON_R] = 0;
499         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_R] && joypad_0_buttons[BUTTON_L])
500                 joypad_0_buttons[BUTTON_L] = 0;
501         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_U] && joypad_0_buttons[BUTTON_D])
502                 joypad_0_buttons[BUTTON_D] = 0;
503         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_D] && joypad_0_buttons[BUTTON_U])
504                 joypad_0_buttons[BUTTON_U] = 0;
505
506         if (e->key() == (int)vjs.p2KeyBindings[BUTTON_L] && joypad_1_buttons[BUTTON_R])
507                 joypad_1_buttons[BUTTON_R] = 0;
508         if (e->key() == (int)vjs.p2KeyBindings[BUTTON_R] && joypad_1_buttons[BUTTON_L])
509                 joypad_1_buttons[BUTTON_L] = 0;
510         if (e->key() == (int)vjs.p2KeyBindings[BUTTON_U] && joypad_1_buttons[BUTTON_D])
511                 joypad_1_buttons[BUTTON_D] = 0;
512         if (e->key() == (int)vjs.p2KeyBindings[BUTTON_D] && joypad_1_buttons[BUTTON_U])
513                 joypad_1_buttons[BUTTON_U] = 0;
514 #else
515 //hrm, this still has sticky state problems... Ugh!
516         // First, settle key states...
517         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_L])
518                 keyHeld[P1LEFT] = state;
519         else if (e->key() == (int)vjs.p1KeyBindings[BUTTON_R])
520                 keyHeld[P1RIGHT] = state;
521         else if (e->key() == (int)vjs.p1KeyBindings[BUTTON_U])
522                 keyHeld[P1UP] = state;
523         else if (e->key() == (int)vjs.p1KeyBindings[BUTTON_D])
524                 keyHeld[P1DOWN] = state;
525         else if (e->key() == (int)vjs.p2KeyBindings[BUTTON_L])
526                 keyHeld[P2LEFT] = state;
527         else if (e->key() == (int)vjs.p2KeyBindings[BUTTON_R])
528                 keyHeld[P2RIGHT] = state;
529         else if (e->key() == (int)vjs.p2KeyBindings[BUTTON_U])
530                 keyHeld[P2UP] = state;
531         else if (e->key() == (int)vjs.p2KeyBindings[BUTTON_D])
532                 keyHeld[P2DOWN] = state;
533
534         // Next, check for conflicts and bail out if there are any...
535         if ((keyHeld[P1LEFT] && keyHeld[P1RIGHT])
536                 || (keyHeld[P1UP] && keyHeld[P1DOWN])
537                 || (keyHeld[P2LEFT] && keyHeld[P2RIGHT])
538                 || (keyHeld[P2UP] && keyHeld[P2DOWN]))
539                 return;
540 #endif
541 #endif
542
543         // No bad combos exist, let's stuff the emulator key buffers...!
544         for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
545         {
546                 if (e->key() == (int)vjs.p1KeyBindings[i])
547 //                      joypad_0_buttons[i] = (uint8)state;
548                         joypad0Buttons[i] = (state ? 0x01 : 0x00);
549
550 // Pad #2 is screwing up pad #1. Prolly a problem in joystick.cpp...
551 // So let's try to fix it there. :-P [DONE]
552                 if (e->key() == (int)vjs.p2KeyBindings[i])
553 //                      joypad_1_buttons[i] = (uint8)state;
554                         joypad1Buttons[i] = (state ? 0x01 : 0x00);
555         }
556 }
557
558
559 void MainWin::HandleGamepads(void)
560 {
561         Gamepad::Update();
562
563         for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
564         {
565                 if (vjs.p1KeyBindings[i] & (JOY_BUTTON | JOY_HAT | JOY_AXIS))
566                         joypad0Buttons[i] = (Gamepad::GetState(0, vjs.p1KeyBindings[i]) ? 0x01 : 0x00);
567
568                 if (vjs.p2KeyBindings[i] & (JOY_BUTTON | JOY_HAT | JOY_AXIS))
569                         joypad1Buttons[i] = (Gamepad::GetState(1, vjs.p2KeyBindings[i]) ? 0x01 : 0x00);
570         }
571 }
572
573
574 void MainWin::Open(void)
575 {
576 }
577
578
579 void MainWin::Configure(void)
580 {
581         // Call the configuration dialog and update settings
582         ConfigDialog dlg(this);
583         //ick.
584         dlg.generalTab->useUnknownSoftware->setChecked(allowUnknownSoftware);
585
586         if (dlg.exec() == false)
587                 return;
588
589         QString before = vjs.ROMPath;
590         QString alpineBefore = vjs.alpineROMPath;
591         QString absBefore = vjs.absROMPath;
592 //      bool audioBefore = vjs.audioEnabled;
593         bool audioBefore = vjs.DSPEnabled;
594         dlg.UpdateVJSettings();
595         QString after = vjs.ROMPath;
596         QString alpineAfter = vjs.alpineROMPath;
597         QString absAfter = vjs.absROMPath;
598 //      bool audioAfter = vjs.audioEnabled;
599         bool audioAfter = vjs.DSPEnabled;
600
601         bool allowOld = allowUnknownSoftware;
602         //ick.
603         allowUnknownSoftware = dlg.generalTab->useUnknownSoftware->isChecked();
604
605         // We rescan the "software" folder if the user either changed the path or
606         // checked/unchecked the "Allow unknown files" option in the config dialog.
607         if ((before != after) || (allowOld != allowUnknownSoftware))
608                 filePickWin->ScanSoftwareFolder(allowUnknownSoftware);
609
610         // If the "Alpine" ROM is changed, then let's load it...
611         if (alpineBefore != alpineAfter)
612         {
613                 if (!JaguarLoadFile(vjs.alpineROMPath) && !AlpineLoadFile(vjs.alpineROMPath))
614                 {
615                         // Oh crap, we couldn't get the file! Alert the media!
616                         QMessageBox msg;
617                         msg.setText(QString(tr("Could not load file \"%1\"!")).arg(vjs.alpineROMPath));
618                         msg.setIcon(QMessageBox::Warning);
619                         msg.exec();
620                 }
621         }
622
623         // If the "ABS" ROM is changed, then let's load it...
624         if (absBefore != absAfter)
625         {
626                 if (!JaguarLoadFile(vjs.absROMPath))
627                 {
628                         // Oh crap, we couldn't get the file! Alert the media!
629                         QMessageBox msg;
630                         msg.setText(QString(tr("Could not load file \"%1\"!")).arg(vjs.absROMPath));
631                         msg.setIcon(QMessageBox::Warning);
632                         msg.exec();
633                 }
634         }
635
636         // If the "Enable DSP" checkbox changed, then we have to re-init the DAC,
637         // since it's running in the host audio IRQ...
638         if (audioBefore != audioAfter)
639         {
640                 DACDone();
641                 DACInit();
642         }
643
644         // Just in case we crash before a clean exit...
645         WriteSettings();
646 }
647
648
649 //
650 // Here's the main emulator loop
651 //
652 void MainWin::Timer(void)
653 {
654         if (!running)
655                 return;
656
657         if (showUntunedTankCircuit)
658         {
659                 // Some machines can't handle this, so we give them the option to disable it. :-)
660                 if (!plzDontKillMyComputer)
661                 {
662                         // Random hash & trash
663                         // We try to simulate an untuned tank circuit here... :-)
664                         for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
665                         {
666                                 for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
667                                 {
668                                         videoWidget->buffer[(y * videoWidget->textureWidth) + x]
669                                                 = (rand() & 0xFF) << 8 | (rand() & 0xFF) << 16 | (rand() & 0xFF) << 24;
670                                 }
671                         }
672                 }
673         }
674         else
675         {
676                 // Otherwise, run the Jaguar simulation
677                 HandleGamepads();
678                 JaguarExecuteNew();
679                 videoWidget->HandleMouseHiding();
680         }
681
682         videoWidget->updateGL();
683
684 #if 1
685         // FPS handling
686         // Approach: We use a ring buffer to store timestamps over a given amount
687         // of frames, then sum them to figure out the FPS.
688         uint32_t timestamp = SDL_GetTicks();
689         // This assumes the ring buffer size is a power of 2
690         ringBufferPointer = (ringBufferPointer + 1) & (RING_BUFFER_SIZE - 1);
691         ringBuffer[ringBufferPointer] = timestamp - oldTimestamp;
692         uint32_t elapsedTime = 0;
693
694         for(uint32_t i=0; i<RING_BUFFER_SIZE; i++)
695                 elapsedTime += ringBuffer[i];
696
697         // elapsedTime must be non-zero
698         if (elapsedTime == 0)
699                 elapsedTime = 1;
700
701 #if 0
702         float framesPerSecond = ((float)RING_BUFFER_SIZE / (float)elapsedTime) * 1000.0;
703         statusBar()->showMessage(QString("%1 FPS").arg(framesPerSecond));
704 #else
705         // This is in frames per 10 seconds, so we can have 1 decimal
706         uint32_t framesPerSecond = (uint32_t)(((float)RING_BUFFER_SIZE / (float)elapsedTime) * 10000.0);
707         uint32_t fpsIntegerPart = framesPerSecond / 10;
708         uint32_t fpsDecimalPart = framesPerSecond % 10;
709         statusBar()->showMessage(QString("%1.%2 FPS").arg(fpsIntegerPart).arg(fpsDecimalPart));
710 #endif
711         oldTimestamp = timestamp;
712 #endif
713 }
714
715
716 void MainWin::TogglePowerState(void)
717 {
718         powerButtonOn = !powerButtonOn;
719         running = true;
720
721         // With the power off, we simulate white noise on the screen. :-)
722         if (!powerButtonOn)
723         {
724                 // Restore the mouse pointer, if hidden:
725                 videoWidget->CheckAndRestoreMouseCursor();
726                 useCDAct->setDisabled(false);
727                 palAct->setDisabled(false);
728                 ntscAct->setDisabled(false);
729                 pauseAct->setChecked(false);
730                 pauseAct->setDisabled(true);
731                 showUntunedTankCircuit = true;
732                 DACPauseAudioThread();
733                 // This is just in case the ROM we were playing was in a narrow or wide
734                 // field mode, so the untuned tank sim doesn't look wrong. :-)
735                 TOMReset();
736
737                 if (plzDontKillMyComputer)
738                 {
739                         // We have to do it line by line, because the texture pitch is not
740                         // the same as the picture buffer's pitch.
741                         for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
742                         {
743                                 memcpy(videoWidget->buffer + (y * videoWidget->textureWidth), testPattern + (y * VIRTUAL_SCREEN_WIDTH), VIRTUAL_SCREEN_WIDTH * sizeof(uint32_t));
744                         }
745                 }
746         }
747         else
748         {
749                 useCDAct->setDisabled(true);
750                 palAct->setDisabled(true);
751                 ntscAct->setDisabled(true);
752                 pauseAct->setChecked(false);
753                 pauseAct->setDisabled(false);
754                 showUntunedTankCircuit = false;
755
756                 // Otherwise, we prepare for running regular software...
757                 if (CDActive)
758                 {
759 // Should check for cartridgeLoaded here as well...!
760 // We can clear it when toggling CDActive on, so that when we power cycle it
761 // does the expected thing. Otherwise, if we use the file picker to insert a
762 // cart, we expect to run the cart! Maybe have a RemoveCart function that only
763 // works if the CD unit is active?
764                         setWindowTitle(QString("Virtual Jaguar " VJ_RELEASE_VERSION
765                                 " - Now playing: Jaguar CD"));
766                 }
767
768                 WriteLog("GUI: Resetting Jaguar...\n");
769                 JaguarReset();
770                 DACPauseAudioThread(false);
771         }
772 }
773
774
775 void MainWin::ToggleRunState(void)
776 {
777         running = !running;
778
779         if (!running)
780         {
781                 // Restore the mouse pointer, if hidden:
782                 videoWidget->CheckAndRestoreMouseCursor();
783                 frameAdvanceAct->setDisabled(false);
784
785                 for(uint32_t i=0; i<(uint32_t)(videoWidget->textureWidth * 256); i++)
786                 {
787                         uint32_t pixel = videoWidget->buffer[i];
788                         uint8_t r = (pixel >> 24) & 0xFF, g = (pixel >> 16) & 0xFF, b = (pixel >> 8) & 0xFF;
789                         pixel = ((r + g + b) / 3) & 0x00FF;
790                         videoWidget->buffer[i] = 0x000000FF | (pixel << 16) | (pixel << 8);
791                 }
792
793                 videoWidget->updateGL();
794         }
795         else
796                 frameAdvanceAct->setDisabled(true);
797
798         // Pause/unpause any running/non-running threads...
799         DACPauseAudioThread(!running);
800 }
801
802
803 void MainWin::SetZoom100(void)
804 {
805         zoomLevel = 1;
806         ResizeMainWindow();
807 }
808
809
810 void MainWin::SetZoom200(void)
811 {
812         zoomLevel = 2;
813         ResizeMainWindow();
814 }
815
816
817 void MainWin::SetZoom300(void)
818 {
819         zoomLevel = 3;
820         ResizeMainWindow();
821 }
822
823
824 void MainWin::SetNTSC(void)
825 {
826         powerAct->setIcon(powerRed);
827         timer->setInterval(16);
828         vjs.hardwareTypeNTSC = true;
829         ResizeMainWindow();
830         WriteSettings();
831 }
832
833
834 void MainWin::SetPAL(void)
835 {
836         powerAct->setIcon(powerGreen);
837         timer->setInterval(20);
838         vjs.hardwareTypeNTSC = false;
839         ResizeMainWindow();
840         WriteSettings();
841 }
842
843
844 void MainWin::ToggleBlur(void)
845 {
846         vjs.glFilter = !vjs.glFilter;
847         WriteSettings();
848 }
849
850
851 void MainWin::ShowAboutWin(void)
852 {
853         aboutWin->show();
854 }
855
856
857 void MainWin::ShowHelpWin(void)
858 {
859         helpWin->show();
860 }
861
862
863 void MainWin::InsertCart(void)
864 {
865         // If the emulator is running, we pause it here and unpause it later
866         // if we dismiss the file selector without choosing anything
867         if (running && powerButtonOn)
868         {
869                 ToggleRunState();
870                 pauseForFileSelector = true;
871         }
872
873         filePickWin->show();
874 }
875
876
877 void MainWin::Unpause(void)
878 {
879         // Here we unpause the emulator if it was paused when we went into the file selector
880         if (pauseForFileSelector)
881         {
882                 pauseForFileSelector = false;
883
884                 // Some nutter might have unpaused while in the file selector, so check for that
885                 if (!running)
886                         ToggleRunState();
887         }
888 }
889
890
891 void MainWin::LoadSoftware(QString file)
892 {
893         running = false;                                                        // Prevent bad things(TM) from happening...
894         pauseForFileSelector = false;                           // Reset the file selector pause flag
895
896         char * biosPointer = jaguarBootROM;
897
898         if (vjs.hardwareTypeAlpine)
899                 biosPointer = jaguarDevBootROM2;
900
901         memcpy(jagMemSpace + 0xE00000, biosPointer, 0x20000);
902
903         powerAct->setDisabled(false);
904         powerAct->setChecked(true);
905         powerButtonOn = false;
906         TogglePowerState();
907         // We have to load our software *after* the Jaguar RESET
908         cartridgeLoaded = JaguarLoadFile(file.toAscii().data());
909         SET32(jaguarMainRAM, 0, 0x00200000);            // Set top of stack...
910
911         // This is icky because we've already done it
912 // it gets worse :-P
913 if (!vjs.useJaguarBIOS)
914         SET32(jaguarMainRAM, 4, jaguarRunAddress);
915
916         m68k_pulse_reset();
917
918         if (!vjs.hardwareTypeAlpine && !loadAndGo)
919         {
920                 QString newTitle = QString("Virtual Jaguar " VJ_RELEASE_VERSION " - Now playing: %1")
921                         .arg(filePickWin->GetSelectedPrettyName());
922                 setWindowTitle(newTitle);
923         }
924 }
925
926
927 void MainWin::ToggleCDUsage(void)
928 {
929         CDActive = !CDActive;
930
931         // Set up the Jaguar CD for execution, otherwise, clear memory
932         if (CDActive)
933                 memcpy(jagMemSpace + 0x800000, jaguarCDBootROM, 0x40000);
934         else
935                 memset(jagMemSpace + 0x800000, 0xFF, 0x40000);
936 }
937
938
939 void MainWin::FrameAdvance(void)
940 {
941 //printf("Frame Advance...\n");
942         // Execute 1 frame, then exit (only useful in Pause mode)
943         JaguarExecuteNew();
944         videoWidget->updateGL();
945         // Need to execute 1 frames' worth of DSP thread as well :-/
946 #warning "!!! Need to execute the DSP thread for 1 frame too !!!"
947 }
948
949
950 void MainWin::SetFullScreen(bool state/*= true*/)
951 {
952         if (state)
953         {
954                 mainWinPosition = pos();
955                 menuBar()->hide();
956                 statusBar()->hide();
957                 toolbar->hide();
958
959                 if (debugbar)
960                         debugbar->hide();
961
962                 showFullScreen();
963                 // This is needed because the fullscreen may happen on a different
964                 // screen than screen 0:
965                 int screenNum = QApplication::desktop()->screenNumber(videoWidget);
966 //              QRect r = QApplication::desktop()->availableGeometry(screenNum);
967                 QRect r = QApplication::desktop()->screenGeometry(screenNum);
968                 double targetWidth = (double)VIRTUAL_SCREEN_WIDTH,
969                         targetHeight = (double)(vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL);
970                 double aspectRatio = targetWidth / targetHeight;
971                 // NOTE: Really should check here to see which dimension constrains the
972                 //       other. Right now, we assume that height is the constraint.
973                 int newWidth = (int)(aspectRatio * (double)r.height());
974                 videoWidget->offset = (r.width() - newWidth) / 2;
975                 videoWidget->fullscreen = true;
976                 videoWidget->outputWidth = newWidth;
977                 videoWidget->setFixedSize(r.width(), r.height());
978                 showFullScreen();
979         }
980         else
981         {
982                 // Reset the video widget to windowed mode
983                 videoWidget->offset = 0;
984                 videoWidget->fullscreen = false;
985                 menuBar()->show();
986                 statusBar()->show();
987                 toolbar->show();
988
989                 if (debugbar)
990                         debugbar->show();
991
992                 showNormal();
993                 ResizeMainWindow();
994                 move(mainWinPosition);
995         }
996 }
997
998
999 void MainWin::ToggleFullScreen(void)
1000 {
1001         fullScreen = !fullScreen;
1002         SetFullScreen(fullScreen);
1003 }
1004
1005
1006 void MainWin::ShowMemoryBrowserWin(void)
1007 {
1008         memBrowseWin->show();
1009         memBrowseWin->RefreshContents();
1010 }
1011
1012
1013 void MainWin::ShowCPUBrowserWin(void)
1014 {
1015         cpuBrowseWin->show();
1016         cpuBrowseWin->RefreshContents();
1017 }
1018
1019
1020 void MainWin::ShowOPBrowserWin(void)
1021 {
1022         opBrowseWin->show();
1023         opBrowseWin->RefreshContents();
1024 }
1025
1026
1027 void MainWin::ShowM68KDasmBrowserWin(void)
1028 {
1029         m68kDasmBrowseWin->show();
1030         m68kDasmBrowseWin->RefreshContents();
1031 }
1032
1033
1034 void MainWin::ShowRISCDasmBrowserWin(void)
1035 {
1036         riscDasmBrowseWin->show();
1037         riscDasmBrowseWin->RefreshContents();
1038 }
1039
1040
1041 void MainWin::ResizeMainWindow(void)
1042 {
1043         videoWidget->setFixedSize(zoomLevel * VIRTUAL_SCREEN_WIDTH,
1044                 zoomLevel * (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL));
1045
1046         // Show the test pattern if user requested plzDontKillMyComputer mode
1047         if (!powerButtonOn && plzDontKillMyComputer)
1048         {
1049                 for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
1050                 {
1051                         memcpy(videoWidget->buffer + (y * videoWidget->textureWidth), testPattern + (y * VIRTUAL_SCREEN_WIDTH), VIRTUAL_SCREEN_WIDTH * sizeof(uint32_t));
1052                 }
1053         }
1054
1055         show();
1056
1057         for(int i=0; i<2; i++)
1058         {
1059                 resize(0, 0);
1060                 usleep(2000);
1061                 QApplication::processEvents();
1062         }
1063 }
1064
1065
1066 #warning "!!! Need to check the window geometry to see if the positions are legal !!!"
1067 // i.e., someone could drag it to another screen, close it, then disconnect that screen
1068 void MainWin::ReadSettings(void)
1069 {
1070         QSettings settings("Underground Software", "Virtual Jaguar");
1071         mainWinPosition = settings.value("pos", QPoint(200, 200)).toPoint();
1072         QSize size = settings.value("size", QSize(400, 400)).toSize();
1073         resize(size);
1074         move(mainWinPosition);
1075         QPoint pos = settings.value("cartLoadPos", QPoint(200, 200)).toPoint();
1076         filePickWin->move(pos);
1077
1078         zoomLevel = settings.value("zoom", 2).toInt();
1079         allowUnknownSoftware = settings.value("showUnknownSoftware", false).toBool();
1080
1081         vjs.useJoystick      = settings.value("useJoystick", false).toBool();
1082         vjs.joyport          = settings.value("joyport", 0).toInt();
1083         vjs.hardwareTypeNTSC = settings.value("hardwareTypeNTSC", true).toBool();
1084         vjs.frameSkip        = settings.value("frameSkip", 0).toInt();
1085         vjs.useJaguarBIOS    = settings.value("useJaguarBIOS", false).toBool();
1086         vjs.GPUEnabled       = settings.value("GPUEnabled", true).toBool();
1087         vjs.DSPEnabled       = settings.value("DSPEnabled", false).toBool();
1088         vjs.audioEnabled     = settings.value("audioEnabled", true).toBool();
1089         vjs.usePipelinedDSP  = settings.value("usePipelinedDSP", false).toBool();
1090         vjs.fullscreen       = settings.value("fullscreen", false).toBool();
1091         vjs.useOpenGL        = settings.value("useOpenGL", true).toBool();
1092         vjs.glFilter         = settings.value("glFilterType", 1).toInt();
1093         vjs.renderType       = settings.value("renderType", 0).toInt();
1094         vjs.allowWritesToROM = settings.value("writeROM", false).toBool();
1095         vjs.biosType         = settings.value("biosType", BT_M_SERIES).toInt();
1096         strcpy(vjs.EEPROMPath, settings.value("EEPROMs", "./eeproms/").toString().toAscii().data());
1097         strcpy(vjs.ROMPath, settings.value("ROMs", "./software/").toString().toAscii().data());
1098         strcpy(vjs.alpineROMPath, settings.value("DefaultROM", "").toString().toAscii().data());
1099         strcpy(vjs.absROMPath, settings.value("DefaultABS", "").toString().toAscii().data());
1100
1101 WriteLog("MainWin: Paths\n");
1102 WriteLog("   EEPROMPath = \"%s\"\n", vjs.EEPROMPath);
1103 WriteLog("      ROMPath = \"%s\"\n", vjs.ROMPath);
1104 WriteLog("AlpineROMPath = \"%s\"\n", vjs.alpineROMPath);
1105 WriteLog("   absROMPath = \"%s\"\n", vjs.absROMPath);
1106 WriteLog("Pipelined DSP = %s\n", (vjs.usePipelinedDSP ? "ON" : "off"));
1107
1108         // Keybindings in order of U, D, L, R, C, B, A, Op, Pa, 0-9, #, *
1109         vjs.p1KeyBindings[BUTTON_U] = settings.value("p1k_up", Qt::Key_S).toInt();
1110         vjs.p1KeyBindings[BUTTON_D] = settings.value("p1k_down", Qt::Key_X).toInt();
1111         vjs.p1KeyBindings[BUTTON_L] = settings.value("p1k_left", Qt::Key_A).toInt();
1112         vjs.p1KeyBindings[BUTTON_R] = settings.value("p1k_right", Qt::Key_D).toInt();
1113         vjs.p1KeyBindings[BUTTON_C] = settings.value("p1k_c", Qt::Key_J).toInt();
1114         vjs.p1KeyBindings[BUTTON_B] = settings.value("p1k_b", Qt::Key_K).toInt();
1115         vjs.p1KeyBindings[BUTTON_A] = settings.value("p1k_a", Qt::Key_L).toInt();
1116         vjs.p1KeyBindings[BUTTON_OPTION] = settings.value("p1k_option", Qt::Key_O).toInt();
1117         vjs.p1KeyBindings[BUTTON_PAUSE] = settings.value("p1k_pause", Qt::Key_P).toInt();
1118         vjs.p1KeyBindings[BUTTON_0] = settings.value("p1k_0", Qt::Key_0).toInt();
1119         vjs.p1KeyBindings[BUTTON_1] = settings.value("p1k_1", Qt::Key_1).toInt();
1120         vjs.p1KeyBindings[BUTTON_2] = settings.value("p1k_2", Qt::Key_2).toInt();
1121         vjs.p1KeyBindings[BUTTON_3] = settings.value("p1k_3", Qt::Key_3).toInt();
1122         vjs.p1KeyBindings[BUTTON_4] = settings.value("p1k_4", Qt::Key_4).toInt();
1123         vjs.p1KeyBindings[BUTTON_5] = settings.value("p1k_5", Qt::Key_5).toInt();
1124         vjs.p1KeyBindings[BUTTON_6] = settings.value("p1k_6", Qt::Key_6).toInt();
1125         vjs.p1KeyBindings[BUTTON_7] = settings.value("p1k_7", Qt::Key_7).toInt();
1126         vjs.p1KeyBindings[BUTTON_8] = settings.value("p1k_8", Qt::Key_8).toInt();
1127         vjs.p1KeyBindings[BUTTON_9] = settings.value("p1k_9", Qt::Key_9).toInt();
1128         vjs.p1KeyBindings[BUTTON_d] = settings.value("p1k_pound", Qt::Key_Minus).toInt();
1129         vjs.p1KeyBindings[BUTTON_s] = settings.value("p1k_star", Qt::Key_Equal).toInt();
1130
1131         vjs.p2KeyBindings[BUTTON_U] = settings.value("p2k_up", Qt::Key_Up).toInt();
1132         vjs.p2KeyBindings[BUTTON_D] = settings.value("p2k_down", Qt::Key_Down).toInt();
1133         vjs.p2KeyBindings[BUTTON_L] = settings.value("p2k_left", Qt::Key_Left).toInt();
1134         vjs.p2KeyBindings[BUTTON_R] = settings.value("p2k_right", Qt::Key_Right).toInt();
1135         vjs.p2KeyBindings[BUTTON_C] = settings.value("p2k_c", Qt::Key_Z).toInt();
1136         vjs.p2KeyBindings[BUTTON_B] = settings.value("p2k_b", Qt::Key_X).toInt();
1137         vjs.p2KeyBindings[BUTTON_A] = settings.value("p2k_a", Qt::Key_C).toInt();
1138         vjs.p2KeyBindings[BUTTON_OPTION] = settings.value("p2k_option", Qt::Key_Apostrophe).toInt();
1139         vjs.p2KeyBindings[BUTTON_PAUSE] = settings.value("p2k_pause", Qt::Key_Return).toInt();
1140         vjs.p2KeyBindings[BUTTON_0] = settings.value("p2k_0", Qt::Key_0).toInt();
1141         vjs.p2KeyBindings[BUTTON_1] = settings.value("p2k_1", Qt::Key_1).toInt();
1142         vjs.p2KeyBindings[BUTTON_2] = settings.value("p2k_2", Qt::Key_2).toInt();
1143         vjs.p2KeyBindings[BUTTON_3] = settings.value("p2k_3", Qt::Key_3).toInt();
1144         vjs.p2KeyBindings[BUTTON_4] = settings.value("p2k_4", Qt::Key_4).toInt();
1145         vjs.p2KeyBindings[BUTTON_5] = settings.value("p2k_5", Qt::Key_5).toInt();
1146         vjs.p2KeyBindings[BUTTON_6] = settings.value("p2k_6", Qt::Key_6).toInt();
1147         vjs.p2KeyBindings[BUTTON_7] = settings.value("p2k_7", Qt::Key_7).toInt();
1148         vjs.p2KeyBindings[BUTTON_8] = settings.value("p2k_8", Qt::Key_8).toInt();
1149         vjs.p2KeyBindings[BUTTON_9] = settings.value("p2k_9", Qt::Key_9).toInt();
1150         vjs.p2KeyBindings[BUTTON_d] = settings.value("p2k_pound", Qt::Key_Slash).toInt();
1151         vjs.p2KeyBindings[BUTTON_s] = settings.value("p2k_star", Qt::Key_Asterisk).toInt();
1152 }
1153
1154
1155 void MainWin::WriteSettings(void)
1156 {
1157         QSettings settings("Underground Software", "Virtual Jaguar");
1158         settings.setValue("pos", pos());
1159         settings.setValue("size", size());
1160         settings.setValue("cartLoadPos", filePickWin->pos());
1161
1162         settings.setValue("zoom", zoomLevel);
1163         settings.setValue("showUnknownSoftware", allowUnknownSoftware);
1164
1165         settings.setValue("useJoystick", vjs.useJoystick);
1166         settings.setValue("joyport", vjs.joyport);
1167         settings.setValue("hardwareTypeNTSC", vjs.hardwareTypeNTSC);
1168         settings.setValue("frameSkip", vjs.frameSkip);
1169         settings.setValue("useJaguarBIOS", vjs.useJaguarBIOS);
1170         settings.setValue("GPUEnabled", vjs.GPUEnabled);
1171         settings.setValue("DSPEnabled", vjs.DSPEnabled);
1172         settings.setValue("audioEnabled", vjs.audioEnabled);
1173         settings.setValue("usePipelinedDSP", vjs.usePipelinedDSP);
1174         settings.setValue("fullscreen", vjs.fullscreen);
1175         settings.setValue("useOpenGL", vjs.useOpenGL);
1176         settings.setValue("glFilterType", vjs.glFilter);
1177         settings.setValue("renderType", vjs.renderType);
1178         settings.setValue("writeROM", vjs.allowWritesToROM);
1179         settings.setValue("biosType", vjs.biosType);
1180         settings.setValue("JagBootROM", vjs.jagBootPath);
1181         settings.setValue("CDBootROM", vjs.CDBootPath);
1182         settings.setValue("EEPROMs", vjs.EEPROMPath);
1183         settings.setValue("ROMs", vjs.ROMPath);
1184         settings.setValue("DefaultROM", vjs.alpineROMPath);
1185         settings.setValue("DefaultABS", vjs.absROMPath);
1186
1187         settings.setValue("p1k_up", vjs.p1KeyBindings[BUTTON_U]);
1188         settings.setValue("p1k_down", vjs.p1KeyBindings[BUTTON_D]);
1189         settings.setValue("p1k_left", vjs.p1KeyBindings[BUTTON_L]);
1190         settings.setValue("p1k_right", vjs.p1KeyBindings[BUTTON_R]);
1191         settings.setValue("p1k_c", vjs.p1KeyBindings[BUTTON_C]);
1192         settings.setValue("p1k_b", vjs.p1KeyBindings[BUTTON_B]);
1193         settings.setValue("p1k_a", vjs.p1KeyBindings[BUTTON_A]);
1194         settings.setValue("p1k_option", vjs.p1KeyBindings[BUTTON_OPTION]);
1195         settings.setValue("p1k_pause", vjs.p1KeyBindings[BUTTON_PAUSE]);
1196         settings.setValue("p1k_0", vjs.p1KeyBindings[BUTTON_0]);
1197         settings.setValue("p1k_1", vjs.p1KeyBindings[BUTTON_1]);
1198         settings.setValue("p1k_2", vjs.p1KeyBindings[BUTTON_2]);
1199         settings.setValue("p1k_3", vjs.p1KeyBindings[BUTTON_3]);
1200         settings.setValue("p1k_4", vjs.p1KeyBindings[BUTTON_4]);
1201         settings.setValue("p1k_5", vjs.p1KeyBindings[BUTTON_5]);
1202         settings.setValue("p1k_6", vjs.p1KeyBindings[BUTTON_6]);
1203         settings.setValue("p1k_7", vjs.p1KeyBindings[BUTTON_7]);
1204         settings.setValue("p1k_8", vjs.p1KeyBindings[BUTTON_8]);
1205         settings.setValue("p1k_9", vjs.p1KeyBindings[BUTTON_9]);
1206         settings.setValue("p1k_pound", vjs.p1KeyBindings[BUTTON_d]);
1207         settings.setValue("p1k_star", vjs.p1KeyBindings[BUTTON_s]);
1208
1209         settings.setValue("p2k_up", vjs.p2KeyBindings[BUTTON_U]);
1210         settings.setValue("p2k_down", vjs.p2KeyBindings[BUTTON_D]);
1211         settings.setValue("p2k_left", vjs.p2KeyBindings[BUTTON_L]);
1212         settings.setValue("p2k_right", vjs.p2KeyBindings[BUTTON_R]);
1213         settings.setValue("p2k_c", vjs.p2KeyBindings[BUTTON_C]);
1214         settings.setValue("p2k_b", vjs.p2KeyBindings[BUTTON_B]);
1215         settings.setValue("p2k_a", vjs.p2KeyBindings[BUTTON_A]);
1216         settings.setValue("p2k_option", vjs.p2KeyBindings[BUTTON_OPTION]);
1217         settings.setValue("p2k_pause", vjs.p2KeyBindings[BUTTON_PAUSE]);
1218         settings.setValue("p2k_0", vjs.p2KeyBindings[BUTTON_0]);
1219         settings.setValue("p2k_1", vjs.p2KeyBindings[BUTTON_1]);
1220         settings.setValue("p2k_2", vjs.p2KeyBindings[BUTTON_2]);
1221         settings.setValue("p2k_3", vjs.p2KeyBindings[BUTTON_3]);
1222         settings.setValue("p2k_4", vjs.p2KeyBindings[BUTTON_4]);
1223         settings.setValue("p2k_5", vjs.p2KeyBindings[BUTTON_5]);
1224         settings.setValue("p2k_6", vjs.p2KeyBindings[BUTTON_6]);
1225         settings.setValue("p2k_7", vjs.p2KeyBindings[BUTTON_7]);
1226         settings.setValue("p2k_8", vjs.p2KeyBindings[BUTTON_8]);
1227         settings.setValue("p2k_9", vjs.p2KeyBindings[BUTTON_9]);
1228         settings.setValue("p2k_pound", vjs.p2KeyBindings[BUTTON_d]);
1229         settings.setValue("p2k_star", vjs.p2KeyBindings[BUTTON_s]);
1230 }
1231
1232
1233 void MainWin::WriteUISettings(void)
1234 {
1235         QSettings settings("Underground Software", "Virtual Jaguar");
1236         settings.setValue("pos", pos());
1237         settings.setValue("size", size());
1238         settings.setValue("cartLoadPos", filePickWin->pos());
1239
1240         settings.setValue("zoom", zoomLevel);
1241 }