]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/mainwin.cpp
First stab at adding translations. Translators needed! :-)
[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         // FPS handling
685         // Approach: We use a ring buffer to store times (in ms) over a given
686         // amount of frames, then sum them to figure out the FPS.
687         uint32_t timestamp = SDL_GetTicks();
688         // This assumes the ring buffer size is a power of 2
689 //      ringBufferPointer = (ringBufferPointer + 1) & (RING_BUFFER_SIZE - 1);
690         // Doing it this way is better. Ring buffer size can be arbitrary then.
691         ringBufferPointer = (ringBufferPointer + 1) % RING_BUFFER_SIZE;
692         ringBuffer[ringBufferPointer] = timestamp - oldTimestamp;
693         uint32_t elapsedTime = 0;
694
695         for(uint32_t i=0; i<RING_BUFFER_SIZE; i++)
696                 elapsedTime += ringBuffer[i];
697
698         // elapsedTime must be non-zero
699         if (elapsedTime == 0)
700                 elapsedTime = 1;
701
702         // This is in frames per 10 seconds, so we can have 1 decimal
703         uint32_t framesPerSecond = (uint32_t)(((float)RING_BUFFER_SIZE / (float)elapsedTime) * 10000.0);
704         uint32_t fpsIntegerPart = framesPerSecond / 10;
705         uint32_t fpsDecimalPart = framesPerSecond % 10;
706         // If this is updated too frequently to be useful, we can throttle it down
707         // so that it only updates every 10th frame or so
708         statusBar()->showMessage(QString("%1.%2 FPS").arg(fpsIntegerPart).arg(fpsDecimalPart));
709         oldTimestamp = timestamp;
710 }
711
712
713 void MainWin::TogglePowerState(void)
714 {
715         powerButtonOn = !powerButtonOn;
716         running = true;
717
718         // With the power off, we simulate white noise on the screen. :-)
719         if (!powerButtonOn)
720         {
721                 // Restore the mouse pointer, if hidden:
722                 videoWidget->CheckAndRestoreMouseCursor();
723                 useCDAct->setDisabled(false);
724                 palAct->setDisabled(false);
725                 ntscAct->setDisabled(false);
726                 pauseAct->setChecked(false);
727                 pauseAct->setDisabled(true);
728                 showUntunedTankCircuit = true;
729                 DACPauseAudioThread();
730                 // This is just in case the ROM we were playing was in a narrow or wide
731                 // field mode, so the untuned tank sim doesn't look wrong. :-)
732                 TOMReset();
733
734                 if (plzDontKillMyComputer)
735                 {
736                         // We have to do it line by line, because the texture pitch is not
737                         // the same as the picture buffer's pitch.
738                         for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
739                         {
740                                 memcpy(videoWidget->buffer + (y * videoWidget->textureWidth), testPattern + (y * VIRTUAL_SCREEN_WIDTH), VIRTUAL_SCREEN_WIDTH * sizeof(uint32_t));
741                         }
742                 }
743         }
744         else
745         {
746                 useCDAct->setDisabled(true);
747                 palAct->setDisabled(true);
748                 ntscAct->setDisabled(true);
749                 pauseAct->setChecked(false);
750                 pauseAct->setDisabled(false);
751                 showUntunedTankCircuit = false;
752
753                 // Otherwise, we prepare for running regular software...
754                 if (CDActive)
755                 {
756 // Should check for cartridgeLoaded here as well...!
757 // We can clear it when toggling CDActive on, so that when we power cycle it
758 // does the expected thing. Otherwise, if we use the file picker to insert a
759 // cart, we expect to run the cart! Maybe have a RemoveCart function that only
760 // works if the CD unit is active?
761                         setWindowTitle(QString("Virtual Jaguar " VJ_RELEASE_VERSION
762                                 " - Now playing: Jaguar CD"));
763                 }
764
765                 WriteLog("GUI: Resetting Jaguar...\n");
766                 JaguarReset();
767                 DACPauseAudioThread(false);
768         }
769 }
770
771
772 void MainWin::ToggleRunState(void)
773 {
774         running = !running;
775
776         if (!running)
777         {
778                 // Restore the mouse pointer, if hidden:
779                 videoWidget->CheckAndRestoreMouseCursor();
780                 frameAdvanceAct->setDisabled(false);
781
782                 for(uint32_t i=0; i<(uint32_t)(videoWidget->textureWidth * 256); i++)
783                 {
784                         uint32_t pixel = videoWidget->buffer[i];
785                         uint8_t r = (pixel >> 24) & 0xFF, g = (pixel >> 16) & 0xFF, b = (pixel >> 8) & 0xFF;
786                         pixel = ((r + g + b) / 3) & 0x00FF;
787                         videoWidget->buffer[i] = 0x000000FF | (pixel << 16) | (pixel << 8);
788                 }
789
790                 videoWidget->updateGL();
791         }
792         else
793                 frameAdvanceAct->setDisabled(true);
794
795         // Pause/unpause any running/non-running threads...
796         DACPauseAudioThread(!running);
797 }
798
799
800 void MainWin::SetZoom100(void)
801 {
802         zoomLevel = 1;
803         ResizeMainWindow();
804 }
805
806
807 void MainWin::SetZoom200(void)
808 {
809         zoomLevel = 2;
810         ResizeMainWindow();
811 }
812
813
814 void MainWin::SetZoom300(void)
815 {
816         zoomLevel = 3;
817         ResizeMainWindow();
818 }
819
820
821 void MainWin::SetNTSC(void)
822 {
823         powerAct->setIcon(powerRed);
824         timer->setInterval(16);
825         vjs.hardwareTypeNTSC = true;
826         ResizeMainWindow();
827         WriteSettings();
828 }
829
830
831 void MainWin::SetPAL(void)
832 {
833         powerAct->setIcon(powerGreen);
834         timer->setInterval(20);
835         vjs.hardwareTypeNTSC = false;
836         ResizeMainWindow();
837         WriteSettings();
838 }
839
840
841 void MainWin::ToggleBlur(void)
842 {
843         vjs.glFilter = !vjs.glFilter;
844         WriteSettings();
845 }
846
847
848 void MainWin::ShowAboutWin(void)
849 {
850         aboutWin->show();
851 }
852
853
854 void MainWin::ShowHelpWin(void)
855 {
856         helpWin->show();
857 }
858
859
860 void MainWin::InsertCart(void)
861 {
862         // If the emulator is running, we pause it here and unpause it later
863         // if we dismiss the file selector without choosing anything
864         if (running && powerButtonOn)
865         {
866                 ToggleRunState();
867                 pauseForFileSelector = true;
868         }
869
870         filePickWin->show();
871 }
872
873
874 void MainWin::Unpause(void)
875 {
876         // Here we unpause the emulator if it was paused when we went into the file selector
877         if (pauseForFileSelector)
878         {
879                 pauseForFileSelector = false;
880
881                 // Some nutter might have unpaused while in the file selector, so check for that
882                 if (!running)
883                         ToggleRunState();
884         }
885 }
886
887
888 void MainWin::LoadSoftware(QString file)
889 {
890         running = false;                                                        // Prevent bad things(TM) from happening...
891         pauseForFileSelector = false;                           // Reset the file selector pause flag
892
893         char * biosPointer = jaguarBootROM;
894
895         if (vjs.hardwareTypeAlpine)
896                 biosPointer = jaguarDevBootROM2;
897
898         memcpy(jagMemSpace + 0xE00000, biosPointer, 0x20000);
899
900         powerAct->setDisabled(false);
901         powerAct->setChecked(true);
902         powerButtonOn = false;
903         TogglePowerState();
904         // We have to load our software *after* the Jaguar RESET
905         cartridgeLoaded = JaguarLoadFile(file.toAscii().data());
906         SET32(jaguarMainRAM, 0, 0x00200000);            // Set top of stack...
907
908         // This is icky because we've already done it
909 // it gets worse :-P
910 if (!vjs.useJaguarBIOS)
911         SET32(jaguarMainRAM, 4, jaguarRunAddress);
912
913         m68k_pulse_reset();
914
915         if (!vjs.hardwareTypeAlpine && !loadAndGo)
916         {
917                 QString newTitle = QString("Virtual Jaguar " VJ_RELEASE_VERSION " - Now playing: %1")
918                         .arg(filePickWin->GetSelectedPrettyName());
919                 setWindowTitle(newTitle);
920         }
921 }
922
923
924 void MainWin::ToggleCDUsage(void)
925 {
926         CDActive = !CDActive;
927
928         // Set up the Jaguar CD for execution, otherwise, clear memory
929         if (CDActive)
930                 memcpy(jagMemSpace + 0x800000, jaguarCDBootROM, 0x40000);
931         else
932                 memset(jagMemSpace + 0x800000, 0xFF, 0x40000);
933 }
934
935
936 void MainWin::FrameAdvance(void)
937 {
938 //printf("Frame Advance...\n");
939         // Execute 1 frame, then exit (only useful in Pause mode)
940         JaguarExecuteNew();
941         videoWidget->updateGL();
942         // Need to execute 1 frames' worth of DSP thread as well :-/
943 #warning "!!! Need to execute the DSP thread for 1 frame too !!!"
944 }
945
946
947 void MainWin::SetFullScreen(bool state/*= true*/)
948 {
949         if (state)
950         {
951                 mainWinPosition = pos();
952                 menuBar()->hide();
953                 statusBar()->hide();
954                 toolbar->hide();
955
956                 if (debugbar)
957                         debugbar->hide();
958
959                 showFullScreen();
960                 // This is needed because the fullscreen may happen on a different
961                 // screen than screen 0:
962                 int screenNum = QApplication::desktop()->screenNumber(videoWidget);
963 //              QRect r = QApplication::desktop()->availableGeometry(screenNum);
964                 QRect r = QApplication::desktop()->screenGeometry(screenNum);
965                 double targetWidth = (double)VIRTUAL_SCREEN_WIDTH,
966                         targetHeight = (double)(vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL);
967                 double aspectRatio = targetWidth / targetHeight;
968                 // NOTE: Really should check here to see which dimension constrains the
969                 //       other. Right now, we assume that height is the constraint.
970                 int newWidth = (int)(aspectRatio * (double)r.height());
971                 videoWidget->offset = (r.width() - newWidth) / 2;
972                 videoWidget->fullscreen = true;
973                 videoWidget->outputWidth = newWidth;
974                 videoWidget->setFixedSize(r.width(), r.height());
975                 showFullScreen();
976         }
977         else
978         {
979                 // Reset the video widget to windowed mode
980                 videoWidget->offset = 0;
981                 videoWidget->fullscreen = false;
982                 menuBar()->show();
983                 statusBar()->show();
984                 toolbar->show();
985
986                 if (debugbar)
987                         debugbar->show();
988
989                 showNormal();
990                 ResizeMainWindow();
991                 move(mainWinPosition);
992         }
993 }
994
995
996 void MainWin::ToggleFullScreen(void)
997 {
998         fullScreen = !fullScreen;
999         SetFullScreen(fullScreen);
1000 }
1001
1002
1003 void MainWin::ShowMemoryBrowserWin(void)
1004 {
1005         memBrowseWin->show();
1006         memBrowseWin->RefreshContents();
1007 }
1008
1009
1010 void MainWin::ShowCPUBrowserWin(void)
1011 {
1012         cpuBrowseWin->show();
1013         cpuBrowseWin->RefreshContents();
1014 }
1015
1016
1017 void MainWin::ShowOPBrowserWin(void)
1018 {
1019         opBrowseWin->show();
1020         opBrowseWin->RefreshContents();
1021 }
1022
1023
1024 void MainWin::ShowM68KDasmBrowserWin(void)
1025 {
1026         m68kDasmBrowseWin->show();
1027         m68kDasmBrowseWin->RefreshContents();
1028 }
1029
1030
1031 void MainWin::ShowRISCDasmBrowserWin(void)
1032 {
1033         riscDasmBrowseWin->show();
1034         riscDasmBrowseWin->RefreshContents();
1035 }
1036
1037
1038 void MainWin::ResizeMainWindow(void)
1039 {
1040         videoWidget->setFixedSize(zoomLevel * VIRTUAL_SCREEN_WIDTH,
1041                 zoomLevel * (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL));
1042
1043         // Show the test pattern if user requested plzDontKillMyComputer mode
1044         if (!powerButtonOn && plzDontKillMyComputer)
1045         {
1046                 for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
1047                 {
1048                         memcpy(videoWidget->buffer + (y * videoWidget->textureWidth), testPattern + (y * VIRTUAL_SCREEN_WIDTH), VIRTUAL_SCREEN_WIDTH * sizeof(uint32_t));
1049                 }
1050         }
1051
1052         show();
1053
1054         for(int i=0; i<2; i++)
1055         {
1056                 resize(0, 0);
1057                 usleep(2000);
1058                 QApplication::processEvents();
1059         }
1060 }
1061
1062
1063 #warning "!!! Need to check the window geometry to see if the positions are legal !!!"
1064 // i.e., someone could drag it to another screen, close it, then disconnect that screen
1065 void MainWin::ReadSettings(void)
1066 {
1067         QSettings settings("Underground Software", "Virtual Jaguar");
1068         mainWinPosition = settings.value("pos", QPoint(200, 200)).toPoint();
1069         QSize size = settings.value("size", QSize(400, 400)).toSize();
1070         resize(size);
1071         move(mainWinPosition);
1072         QPoint pos = settings.value("cartLoadPos", QPoint(200, 200)).toPoint();
1073         filePickWin->move(pos);
1074
1075         zoomLevel = settings.value("zoom", 2).toInt();
1076         allowUnknownSoftware = settings.value("showUnknownSoftware", false).toBool();
1077
1078         vjs.useJoystick      = settings.value("useJoystick", false).toBool();
1079         vjs.joyport          = settings.value("joyport", 0).toInt();
1080         vjs.hardwareTypeNTSC = settings.value("hardwareTypeNTSC", true).toBool();
1081         vjs.frameSkip        = settings.value("frameSkip", 0).toInt();
1082         vjs.useJaguarBIOS    = settings.value("useJaguarBIOS", false).toBool();
1083         vjs.GPUEnabled       = settings.value("GPUEnabled", true).toBool();
1084         vjs.DSPEnabled       = settings.value("DSPEnabled", false).toBool();
1085         vjs.audioEnabled     = settings.value("audioEnabled", true).toBool();
1086         vjs.usePipelinedDSP  = settings.value("usePipelinedDSP", false).toBool();
1087         vjs.fullscreen       = settings.value("fullscreen", false).toBool();
1088         vjs.useOpenGL        = settings.value("useOpenGL", true).toBool();
1089         vjs.glFilter         = settings.value("glFilterType", 1).toInt();
1090         vjs.renderType       = settings.value("renderType", 0).toInt();
1091         vjs.allowWritesToROM = settings.value("writeROM", false).toBool();
1092         vjs.biosType         = settings.value("biosType", BT_M_SERIES).toInt();
1093         strcpy(vjs.EEPROMPath, settings.value("EEPROMs", "./eeproms/").toString().toAscii().data());
1094         strcpy(vjs.ROMPath, settings.value("ROMs", "./software/").toString().toAscii().data());
1095         strcpy(vjs.alpineROMPath, settings.value("DefaultROM", "").toString().toAscii().data());
1096         strcpy(vjs.absROMPath, settings.value("DefaultABS", "").toString().toAscii().data());
1097
1098 WriteLog("MainWin: Paths\n");
1099 WriteLog("   EEPROMPath = \"%s\"\n", vjs.EEPROMPath);
1100 WriteLog("      ROMPath = \"%s\"\n", vjs.ROMPath);
1101 WriteLog("AlpineROMPath = \"%s\"\n", vjs.alpineROMPath);
1102 WriteLog("   absROMPath = \"%s\"\n", vjs.absROMPath);
1103 WriteLog("Pipelined DSP = %s\n", (vjs.usePipelinedDSP ? "ON" : "off"));
1104
1105         // Keybindings in order of U, D, L, R, C, B, A, Op, Pa, 0-9, #, *
1106         vjs.p1KeyBindings[BUTTON_U] = settings.value("p1k_up", Qt::Key_S).toInt();
1107         vjs.p1KeyBindings[BUTTON_D] = settings.value("p1k_down", Qt::Key_X).toInt();
1108         vjs.p1KeyBindings[BUTTON_L] = settings.value("p1k_left", Qt::Key_A).toInt();
1109         vjs.p1KeyBindings[BUTTON_R] = settings.value("p1k_right", Qt::Key_D).toInt();
1110         vjs.p1KeyBindings[BUTTON_C] = settings.value("p1k_c", Qt::Key_J).toInt();
1111         vjs.p1KeyBindings[BUTTON_B] = settings.value("p1k_b", Qt::Key_K).toInt();
1112         vjs.p1KeyBindings[BUTTON_A] = settings.value("p1k_a", Qt::Key_L).toInt();
1113         vjs.p1KeyBindings[BUTTON_OPTION] = settings.value("p1k_option", Qt::Key_O).toInt();
1114         vjs.p1KeyBindings[BUTTON_PAUSE] = settings.value("p1k_pause", Qt::Key_P).toInt();
1115         vjs.p1KeyBindings[BUTTON_0] = settings.value("p1k_0", Qt::Key_0).toInt();
1116         vjs.p1KeyBindings[BUTTON_1] = settings.value("p1k_1", Qt::Key_1).toInt();
1117         vjs.p1KeyBindings[BUTTON_2] = settings.value("p1k_2", Qt::Key_2).toInt();
1118         vjs.p1KeyBindings[BUTTON_3] = settings.value("p1k_3", Qt::Key_3).toInt();
1119         vjs.p1KeyBindings[BUTTON_4] = settings.value("p1k_4", Qt::Key_4).toInt();
1120         vjs.p1KeyBindings[BUTTON_5] = settings.value("p1k_5", Qt::Key_5).toInt();
1121         vjs.p1KeyBindings[BUTTON_6] = settings.value("p1k_6", Qt::Key_6).toInt();
1122         vjs.p1KeyBindings[BUTTON_7] = settings.value("p1k_7", Qt::Key_7).toInt();
1123         vjs.p1KeyBindings[BUTTON_8] = settings.value("p1k_8", Qt::Key_8).toInt();
1124         vjs.p1KeyBindings[BUTTON_9] = settings.value("p1k_9", Qt::Key_9).toInt();
1125         vjs.p1KeyBindings[BUTTON_d] = settings.value("p1k_pound", Qt::Key_Minus).toInt();
1126         vjs.p1KeyBindings[BUTTON_s] = settings.value("p1k_star", Qt::Key_Equal).toInt();
1127
1128         vjs.p2KeyBindings[BUTTON_U] = settings.value("p2k_up", Qt::Key_Up).toInt();
1129         vjs.p2KeyBindings[BUTTON_D] = settings.value("p2k_down", Qt::Key_Down).toInt();
1130         vjs.p2KeyBindings[BUTTON_L] = settings.value("p2k_left", Qt::Key_Left).toInt();
1131         vjs.p2KeyBindings[BUTTON_R] = settings.value("p2k_right", Qt::Key_Right).toInt();
1132         vjs.p2KeyBindings[BUTTON_C] = settings.value("p2k_c", Qt::Key_Z).toInt();
1133         vjs.p2KeyBindings[BUTTON_B] = settings.value("p2k_b", Qt::Key_X).toInt();
1134         vjs.p2KeyBindings[BUTTON_A] = settings.value("p2k_a", Qt::Key_C).toInt();
1135         vjs.p2KeyBindings[BUTTON_OPTION] = settings.value("p2k_option", Qt::Key_Apostrophe).toInt();
1136         vjs.p2KeyBindings[BUTTON_PAUSE] = settings.value("p2k_pause", Qt::Key_Return).toInt();
1137         vjs.p2KeyBindings[BUTTON_0] = settings.value("p2k_0", Qt::Key_0).toInt();
1138         vjs.p2KeyBindings[BUTTON_1] = settings.value("p2k_1", Qt::Key_1).toInt();
1139         vjs.p2KeyBindings[BUTTON_2] = settings.value("p2k_2", Qt::Key_2).toInt();
1140         vjs.p2KeyBindings[BUTTON_3] = settings.value("p2k_3", Qt::Key_3).toInt();
1141         vjs.p2KeyBindings[BUTTON_4] = settings.value("p2k_4", Qt::Key_4).toInt();
1142         vjs.p2KeyBindings[BUTTON_5] = settings.value("p2k_5", Qt::Key_5).toInt();
1143         vjs.p2KeyBindings[BUTTON_6] = settings.value("p2k_6", Qt::Key_6).toInt();
1144         vjs.p2KeyBindings[BUTTON_7] = settings.value("p2k_7", Qt::Key_7).toInt();
1145         vjs.p2KeyBindings[BUTTON_8] = settings.value("p2k_8", Qt::Key_8).toInt();
1146         vjs.p2KeyBindings[BUTTON_9] = settings.value("p2k_9", Qt::Key_9).toInt();
1147         vjs.p2KeyBindings[BUTTON_d] = settings.value("p2k_pound", Qt::Key_Slash).toInt();
1148         vjs.p2KeyBindings[BUTTON_s] = settings.value("p2k_star", Qt::Key_Asterisk).toInt();
1149 }
1150
1151
1152 void MainWin::WriteSettings(void)
1153 {
1154         QSettings settings("Underground Software", "Virtual Jaguar");
1155         settings.setValue("pos", pos());
1156         settings.setValue("size", size());
1157         settings.setValue("cartLoadPos", filePickWin->pos());
1158
1159         settings.setValue("zoom", zoomLevel);
1160         settings.setValue("showUnknownSoftware", allowUnknownSoftware);
1161
1162         settings.setValue("useJoystick", vjs.useJoystick);
1163         settings.setValue("joyport", vjs.joyport);
1164         settings.setValue("hardwareTypeNTSC", vjs.hardwareTypeNTSC);
1165         settings.setValue("frameSkip", vjs.frameSkip);
1166         settings.setValue("useJaguarBIOS", vjs.useJaguarBIOS);
1167         settings.setValue("GPUEnabled", vjs.GPUEnabled);
1168         settings.setValue("DSPEnabled", vjs.DSPEnabled);
1169         settings.setValue("audioEnabled", vjs.audioEnabled);
1170         settings.setValue("usePipelinedDSP", vjs.usePipelinedDSP);
1171         settings.setValue("fullscreen", vjs.fullscreen);
1172         settings.setValue("useOpenGL", vjs.useOpenGL);
1173         settings.setValue("glFilterType", vjs.glFilter);
1174         settings.setValue("renderType", vjs.renderType);
1175         settings.setValue("writeROM", vjs.allowWritesToROM);
1176         settings.setValue("biosType", vjs.biosType);
1177         settings.setValue("JagBootROM", vjs.jagBootPath);
1178         settings.setValue("CDBootROM", vjs.CDBootPath);
1179         settings.setValue("EEPROMs", vjs.EEPROMPath);
1180         settings.setValue("ROMs", vjs.ROMPath);
1181         settings.setValue("DefaultROM", vjs.alpineROMPath);
1182         settings.setValue("DefaultABS", vjs.absROMPath);
1183
1184         settings.setValue("p1k_up", vjs.p1KeyBindings[BUTTON_U]);
1185         settings.setValue("p1k_down", vjs.p1KeyBindings[BUTTON_D]);
1186         settings.setValue("p1k_left", vjs.p1KeyBindings[BUTTON_L]);
1187         settings.setValue("p1k_right", vjs.p1KeyBindings[BUTTON_R]);
1188         settings.setValue("p1k_c", vjs.p1KeyBindings[BUTTON_C]);
1189         settings.setValue("p1k_b", vjs.p1KeyBindings[BUTTON_B]);
1190         settings.setValue("p1k_a", vjs.p1KeyBindings[BUTTON_A]);
1191         settings.setValue("p1k_option", vjs.p1KeyBindings[BUTTON_OPTION]);
1192         settings.setValue("p1k_pause", vjs.p1KeyBindings[BUTTON_PAUSE]);
1193         settings.setValue("p1k_0", vjs.p1KeyBindings[BUTTON_0]);
1194         settings.setValue("p1k_1", vjs.p1KeyBindings[BUTTON_1]);
1195         settings.setValue("p1k_2", vjs.p1KeyBindings[BUTTON_2]);
1196         settings.setValue("p1k_3", vjs.p1KeyBindings[BUTTON_3]);
1197         settings.setValue("p1k_4", vjs.p1KeyBindings[BUTTON_4]);
1198         settings.setValue("p1k_5", vjs.p1KeyBindings[BUTTON_5]);
1199         settings.setValue("p1k_6", vjs.p1KeyBindings[BUTTON_6]);
1200         settings.setValue("p1k_7", vjs.p1KeyBindings[BUTTON_7]);
1201         settings.setValue("p1k_8", vjs.p1KeyBindings[BUTTON_8]);
1202         settings.setValue("p1k_9", vjs.p1KeyBindings[BUTTON_9]);
1203         settings.setValue("p1k_pound", vjs.p1KeyBindings[BUTTON_d]);
1204         settings.setValue("p1k_star", vjs.p1KeyBindings[BUTTON_s]);
1205
1206         settings.setValue("p2k_up", vjs.p2KeyBindings[BUTTON_U]);
1207         settings.setValue("p2k_down", vjs.p2KeyBindings[BUTTON_D]);
1208         settings.setValue("p2k_left", vjs.p2KeyBindings[BUTTON_L]);
1209         settings.setValue("p2k_right", vjs.p2KeyBindings[BUTTON_R]);
1210         settings.setValue("p2k_c", vjs.p2KeyBindings[BUTTON_C]);
1211         settings.setValue("p2k_b", vjs.p2KeyBindings[BUTTON_B]);
1212         settings.setValue("p2k_a", vjs.p2KeyBindings[BUTTON_A]);
1213         settings.setValue("p2k_option", vjs.p2KeyBindings[BUTTON_OPTION]);
1214         settings.setValue("p2k_pause", vjs.p2KeyBindings[BUTTON_PAUSE]);
1215         settings.setValue("p2k_0", vjs.p2KeyBindings[BUTTON_0]);
1216         settings.setValue("p2k_1", vjs.p2KeyBindings[BUTTON_1]);
1217         settings.setValue("p2k_2", vjs.p2KeyBindings[BUTTON_2]);
1218         settings.setValue("p2k_3", vjs.p2KeyBindings[BUTTON_3]);
1219         settings.setValue("p2k_4", vjs.p2KeyBindings[BUTTON_4]);
1220         settings.setValue("p2k_5", vjs.p2KeyBindings[BUTTON_5]);
1221         settings.setValue("p2k_6", vjs.p2KeyBindings[BUTTON_6]);
1222         settings.setValue("p2k_7", vjs.p2KeyBindings[BUTTON_7]);
1223         settings.setValue("p2k_8", vjs.p2KeyBindings[BUTTON_8]);
1224         settings.setValue("p2k_9", vjs.p2KeyBindings[BUTTON_9]);
1225         settings.setValue("p2k_pound", vjs.p2KeyBindings[BUTTON_d]);
1226         settings.setValue("p2k_star", vjs.p2KeyBindings[BUTTON_s]);
1227 }
1228
1229
1230 void MainWin::WriteUISettings(void)
1231 {
1232         QSettings settings("Underground Software", "Virtual Jaguar");
1233         settings.setValue("pos", pos());
1234         settings.setValue("size", size());
1235         settings.setValue("cartLoadPos", filePickWin->pos());
1236
1237         settings.setValue("zoom", zoomLevel);
1238 }