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