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