]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/mainwin.cpp
Added missing headers for win32, code cleanup for file detection code.
[virtualjaguar] / src / gui / mainwin.cpp
1 //
2 // mainwin.cpp - Qt-based GUI for Virtual Jaguar: Main Application Window
3 // by James L. Hammons
4 // (C) 2009 Underground Software
5 //
6 // JLH = James L. Hammons <jlhamm@acm.org>
7 //
8 // Who  When        What
9 // ---  ----------  -------------------------------------------------------------
10 // JLH  12/23/2009  Created this file
11 // JLH  12/20/2010  Added settings, menus & toolbars
12 //
13
14 // FIXED:
15 //
16 //
17 // STILL TO BE DONE:
18 //
19 //
20
21 // Uncomment this for debugging...
22 //#define DEBUG
23 //#define DEBUGFOO                      // Various tool debugging...
24 //#define DEBUGTP                               // Toolpalette debugging...
25
26 #include "mainwin.h"
27
28 #include "SDL.h"
29 #include "glwidget.h"
30 #include "about.h"
31 #include "settings.h"
32 #include "filepicker.h"
33 #include "configdialog.h"
34 #include "version.h"
35
36 #include "jaguar.h"
37 #include "tom.h"
38 #include "log.h"
39 #include "file.h"
40 #include "joystick.h"
41
42 #ifdef __GCCWIN32__
43 // Apparently on win32, usleep() is not pulled in by the usual suspects.
44 #include <unistd.h>
45 #endif
46
47 // Uncomment this to use built-in BIOS/CD-ROM BIOS
48 // You'll need a copy of jagboot.h & jagcd.h for this to work...!
49 //#define USE_BUILT_IN_BIOS
50
51 #ifdef USE_BUILT_IN_BIOS
52 #include "jagboot.h"
53 #include "jagcd.h"
54 #endif
55
56 // The way BSNES controls things is by setting a timer with a zero
57 // timeout, sleeping if not emulating anything. Seems there has to be a
58 // better way.
59
60 // It has a novel approach to plugging-in/using different video/audio/input
61 // methods, can we do something similar or should we just use the built-in
62 // QOpenGL?
63
64 // We're going to try to use the built-in OpenGL support and see how it goes.
65 // We'll make the VJ core modular so that it doesn't matter what GUI is in
66 // use, we can drop it in anywhere and use it as-is.
67
68 MainWin::MainWin(): running(false), powerButtonOn(false), showUntunedTankCircuit(true),
69         cartridgeLoaded(false)
70 {
71         videoWidget = new GLWidget(this);
72         setCentralWidget(videoWidget);
73         setWindowIcon(QIcon(":/res/vj-icon.png"));
74 //      setWindowTitle("Virtual Jaguar v2.0.0");
75         setWindowTitle("Virtual Jaguar " VJ_RELEASE_VERSION );
76
77         aboutWin = new AboutWindow(this);
78         filePickWin = new FilePickerWindow(this);
79
80     videoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
81     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
82
83         ReadSettings();
84         setUnifiedTitleAndToolBarOnMac(true);
85
86         // Create actions
87
88         quitAppAct = new QAction(tr("E&xit"), this);
89         quitAppAct->setShortcuts(QKeySequence::Quit);
90         quitAppAct->setStatusTip(tr("Quit Virtual Jaguar"));
91         connect(quitAppAct, SIGNAL(triggered()), this, SLOT(close()));
92
93         powerAct = new QAction(QIcon(":/res/power.png"), tr("&Power"), this);
94         powerAct->setStatusTip(tr("Powers Jaguar on/off"));
95         powerAct->setCheckable(true);
96         powerAct->setChecked(false);
97         powerAct->setDisabled(true);
98         connect(powerAct, SIGNAL(triggered()), this, SLOT(TogglePowerState()));
99
100         pauseAct = new QAction(QIcon(":/res/pause.png"), tr("Pause"), this);
101         pauseAct->setStatusTip(tr("Toggles the running state"));
102         pauseAct->setCheckable(true);
103         pauseAct->setDisabled(true);
104         pauseAct->setShortcut(QKeySequence(tr("Esc")));
105         connect(pauseAct, SIGNAL(triggered()), this, SLOT(ToggleRunState()));
106
107         zoomActs = new QActionGroup(this);
108
109         x1Act = new QAction(QIcon(":/res/zoom100.png"), tr("Zoom 100%"), zoomActs);
110         x1Act->setStatusTip(tr("Set window zoom to 100%"));
111         x1Act->setCheckable(true);
112         connect(x1Act, SIGNAL(triggered()), this, SLOT(SetZoom100()));
113
114         x2Act = new QAction(QIcon(":/res/zoom200.png"), tr("Zoom 200%"), zoomActs);
115         x2Act->setStatusTip(tr("Set window zoom to 200%"));
116         x2Act->setCheckable(true);
117         connect(x2Act, SIGNAL(triggered()), this, SLOT(SetZoom200()));
118
119         x3Act = new QAction(QIcon(":/res/zoom300.png"), tr("Zoom 300%"), zoomActs);
120         x3Act->setStatusTip(tr("Set window zoom to 300%"));
121         x3Act->setCheckable(true);
122         connect(x3Act, SIGNAL(triggered()), this, SLOT(SetZoom300()));
123
124         tvTypeActs = new QActionGroup(this);
125
126         ntscAct = new QAction(QIcon(":/res/ntsc.png"), tr("NTSC"), tvTypeActs);
127         ntscAct->setStatusTip(tr("Sets Jaguar to NTSC mode"));
128         ntscAct->setCheckable(true);
129         connect(ntscAct, SIGNAL(triggered()), this, SLOT(SetNTSC()));
130
131         palAct = new QAction(QIcon(":/res/pal.png"), tr("PAL"), tvTypeActs);
132         palAct->setStatusTip(tr("Sets Jaguar to PAL mode"));
133         palAct->setCheckable(true);
134         connect(palAct, SIGNAL(triggered()), this, SLOT(SetPAL()));
135
136         blurAct = new QAction(QIcon(":/res/generic.png"), tr("Blur"), this);
137         blurAct->setStatusTip(tr("Sets OpenGL rendering to GL_NEAREST"));
138         blurAct->setCheckable(true);
139         connect(blurAct, SIGNAL(triggered()), this, SLOT(ToggleBlur()));
140
141         aboutAct = new QAction(QIcon(":/res/vj-icon.png"), tr("&About..."), this);
142         aboutAct->setStatusTip(tr("Blatant self-promotion"));
143         connect(aboutAct, SIGNAL(triggered()), this, SLOT(ShowAboutWin()));
144
145         filePickAct = new QAction(QIcon(":/res/software.png"), tr("&Insert Cartridge..."), this);
146         filePickAct->setStatusTip(tr("Insert a cartridge into Virtual Jaguar"));
147         filePickAct->setShortcut(QKeySequence(tr("Ctrl+i")));
148         connect(filePickAct, SIGNAL(triggered()), this, SLOT(InsertCart()));
149
150         configAct = new QAction(QIcon(":/res/generic.png"), tr("&Configure"), this);
151         configAct->setStatusTip(tr("Configure options for Virtual Jaguar"));
152         configAct->setShortcut(QKeySequence(tr("Ctrl+c")));
153         connect(configAct, SIGNAL(triggered()), this, SLOT(Configure()));
154
155         // Misc. connections...
156         connect(filePickWin, SIGNAL(RequestLoad(QString)), this, SLOT(LoadSoftware(QString)));
157
158         // Create menus & toolbars
159
160         fileMenu = menuBar()->addMenu(tr("&File"));
161         fileMenu->addAction(filePickAct);
162         fileMenu->addAction(powerAct);
163         fileMenu->addAction(pauseAct);
164         fileMenu->addAction(configAct);
165         fileMenu->addAction(quitAppAct);
166
167         helpMenu = menuBar()->addMenu(tr("&Help"));
168         helpMenu->addAction(aboutAct);
169
170         toolbar = addToolBar(tr("Stuff"));
171         toolbar->addAction(powerAct);
172         toolbar->addAction(pauseAct);
173         toolbar->addAction(filePickAct);
174         toolbar->addSeparator();
175         toolbar->addAction(x1Act);
176         toolbar->addAction(x2Act);
177         toolbar->addAction(x3Act);
178         toolbar->addSeparator();
179         toolbar->addAction(ntscAct);
180         toolbar->addAction(palAct);
181         toolbar->addSeparator();
182         toolbar->addAction(blurAct);
183
184         //      Create status bar
185         statusBar()->showMessage(tr("Ready"));
186
187         // Set toolbar buttons/menus based on settings read in (sync the UI)...
188         blurAct->setChecked(vjs.glFilter);
189         x1Act->setChecked(zoomLevel == 1);
190         x2Act->setChecked(zoomLevel == 2);
191         x3Act->setChecked(zoomLevel == 3);
192         running = powerAct->isChecked();
193         ntscAct->setChecked(vjs.hardwareTypeNTSC);
194         palAct->setChecked(!vjs.hardwareTypeNTSC);
195
196         // Do this in case original size isn't correct (mostly for the first-run case)
197         ResizeMainWindow();
198
199         // Set up timer based loop for animation...
200         timer = new QTimer(this);
201         connect(timer, SIGNAL(timeout()), this, SLOT(Timer()));
202         timer->start(20);
203
204         WriteLog("Virtual Jaguar %s (Last full build was on %s %s)\n", VJ_RELEASE_VERSION, __DATE__, __TIME__);
205         WriteLog("VJ: Initializing jaguar subsystem...\n");
206         JaguarInit();
207
208         // Get the BIOS ROM
209 #ifdef USE_BUILT_IN_BIOS
210         WriteLog("VJ: Using built in BIOS/CD BIOS...\n");
211         memcpy(jaguarBootROM, jagBootROM, 0x20000);
212         memcpy(jaguarCDBootROM, jagCDROM, 0x40000);
213         BIOSLoaded = CDBIOSLoaded = true;
214 #else
215 // What would be nice here would be a way to check if the BIOS was loaded so that we
216 // could disable the pushbutton on the Misc Options menu... !!! FIX !!! [DONE here, but needs to be fixed in GUI as well!]
217         WriteLog("VJ: About to attempt to load BIOSes...\n");
218 //This is short-circuiting the file finding thread... ??? WHY ???
219 //Not anymore. Was related to a QImage object creation/corruption bug elsewhere.
220         BIOSLoaded = (JaguarLoadROM(jaguarBootROM, vjs.jagBootPath) == 0x20000 ? true : false);
221         WriteLog("VJ: BIOS is %savailable...\n", (BIOSLoaded ? "" : "not "));
222         CDBIOSLoaded = (JaguarLoadROM(jaguarCDBootROM, vjs.CDBootPath) == 0x40000 ? true : false);
223         WriteLog("VJ: CD BIOS is %savailable...\n", (CDBIOSLoaded ? "" : "not "));
224 #endif
225 }
226
227 void MainWin::closeEvent(QCloseEvent * event)
228 {
229         WriteSettings();
230         event->accept(); // ignore() if can't close for some reason
231 }
232
233 void MainWin::keyPressEvent(QKeyEvent * e)
234 {
235         HandleKeys(e, true);
236 }
237
238 void MainWin::keyReleaseEvent(QKeyEvent * e)
239 {
240         HandleKeys(e, false);
241 }
242
243 void MainWin::HandleKeys(QKeyEvent * e, bool state)
244 {
245         // We kill bad key combos here, before they can get to the emulator...
246         // This also kills the illegal instruction problem that cropped up in Rayman!
247         // May want to do this by killing the old one instead of ignoring the new one...
248         // Seems to work better that way...
249 #if 0
250         if ((e->key() == vjs.p1KeyBindings[BUTTON_L] && joypad_0_buttons[BUTTON_R])
251                 || (e->key() == vjs.p1KeyBindings[BUTTON_R] && joypad_0_buttons[BUTTON_L])
252                 || (e->key() == vjs.p1KeyBindings[BUTTON_U] && joypad_0_buttons[BUTTON_D])
253                 || (e->key() == vjs.p1KeyBindings[BUTTON_D] && joypad_0_buttons[BUTTON_U]))
254                 return;
255 #else
256         if (e->key() == vjs.p1KeyBindings[BUTTON_L] && joypad_0_buttons[BUTTON_R])
257                 joypad_0_buttons[BUTTON_R] = 0;
258         if (e->key() == vjs.p1KeyBindings[BUTTON_R] && joypad_0_buttons[BUTTON_L])
259                 joypad_0_buttons[BUTTON_L] = 0;
260         if (e->key() == vjs.p1KeyBindings[BUTTON_U] && joypad_0_buttons[BUTTON_D])
261                 joypad_0_buttons[BUTTON_D] = 0;
262         if (e->key() == vjs.p1KeyBindings[BUTTON_D] && joypad_0_buttons[BUTTON_U])
263                 joypad_0_buttons[BUTTON_U] = 0;
264 #endif
265
266         // No bad combos exist, let's stuff the emulator key buffers...!
267         for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
268         {
269                 if (e->key() == vjs.p1KeyBindings[i])
270                         joypad_0_buttons[i] = (uint8)state;
271         }
272 }
273
274 void MainWin::Open(void)
275 {
276 }
277
278 void MainWin::Configure(void)
279 {
280         // Call the configuration dialog and update settings
281         ConfigDialog dlg(this);
282
283         if (dlg.exec() == false)
284                 return;
285
286         dlg.UpdateVJSettings();
287 }
288
289 //
290 // Here's the main emulator loop
291 //
292 void MainWin::Timer(void)
293 {
294         if (!running)
295                 return;
296
297         if (showUntunedTankCircuit)
298         {
299                 // Random hash & trash
300                 // We try to simulate an untuned tank circuit here... :-)
301                 for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
302                 {
303                         for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
304                         {
305                                 videoWidget->buffer[(y * videoWidget->textureWidth) + x] = (rand() & 0xFF) << 8 | (rand() & 0xFF) << 16 | (rand() & 0xFF) << 24;// | (rand() & 0xFF);//0x000000FF;
306         //                      buffer[(y * textureWidth) + x] = x*y;
307                         }
308                 }
309         }
310         else
311         {
312                 // Otherwise, run the Jaguar simulation
313                 JaguarExecuteNew();
314 //              memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->rasterWidth);
315                 memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->textureWidth * sizeof(uint32_t));
316 //              memcpy(surface->pixels, backbuffer, TOMGetVideoModeWidth() * TOMGetVideoModeHeight() * 4);
317         }
318
319         videoWidget->updateGL();
320 }
321
322 #if 0
323 Window * RunEmu(void)
324 {
325 //      extern uint32 * backbuffer;
326         uint32 * overlayPixels = (uint32 *)sdlemuGetOverlayPixels();
327         memset(overlayPixels, 0x00, 640 * 480 * 4);                     // Clear out overlay...
328
329 //This is crappy... !!! FIX !!!
330 //      extern bool finished, showGUI;
331
332         sdlemuDisableOverlay();
333
334 //      uint32 nFrame = 0, nFrameskip = 0;
335         uint32 totalFrames = 0;
336         finished = false;
337         bool showMessage = true;
338         uint32 showMsgFrames = 120;
339         uint8 transparency = 0xFF;
340         // Pass a message to the "joystick" code to debounce the ESC key...
341         debounceRunKey = true;
342
343         uint32 cartType = 4;
344         if (jaguarRomSize == 0x200000)
345                 cartType = 0;
346         else if (jaguarRomSize == 0x400000)
347                 cartType = 1;
348         else if (jaguarMainRomCRC32 == 0x687068D5)
349                 cartType = 2;
350         else if (jaguarMainRomCRC32 == 0x55A0669C)
351                 cartType = 3;
352
353         const char * cartTypeName[5] = { "2M Cartridge", "4M Cartridge", "CD BIOS", "CD Dev BIOS", "Homebrew" };
354         uint32 elapsedTicks = SDL_GetTicks(), frameCount = 0, framesPerSecond = 0;
355
356         while (!finished)
357         {
358                 // Set up new backbuffer with new pixels and data
359                 JaguarExecuteNew();
360                 totalFrames++;
361 //WriteLog("Frame #%u...\n", totalFrames);
362 //extern bool doDSPDis;
363 //if (totalFrames == 373)
364 //      doDSPDis = true;
365
366 //Problem: Need to do this *only* when the state changes from visible to not...
367 //Also, need to clear out the GUI when not on (when showMessage is active...)
368 if (showGUI || showMessage)
369         sdlemuEnableOverlay();
370 else
371         sdlemuDisableOverlay();
372
373 //Add in a new function for clearing patches of screen (ClearOverlayRect)
374
375 // Also: Take frame rate into account when calculating fade time...
376
377                 // Some QnD GUI stuff here...
378                 if (showGUI)
379                 {
380                         FillScreenRectangle(overlayPixels, 8, 1*FONT_HEIGHT, 128, 4*FONT_HEIGHT, 0x00000000);
381                         extern uint32 gpu_pc, dsp_pc;
382                         DrawString(overlayPixels, 8, 1*FONT_HEIGHT, false, "GPU PC: %08X", gpu_pc);
383                         DrawString(overlayPixels, 8, 2*FONT_HEIGHT, false, "DSP PC: %08X", dsp_pc);
384                         DrawString(overlayPixels, 8, 4*FONT_HEIGHT, false, "%u FPS", framesPerSecond);
385                 }
386
387                 if (showMessage)
388                 {
389                         DrawString2(overlayPixels, 8, 24*FONT_HEIGHT, 0x007F63FF, transparency, "Running...");
390                         DrawString2(overlayPixels, 8, 26*FONT_HEIGHT, 0x001FFF3F, transparency, "%s, run address: %06X", cartTypeName[cartType], jaguarRunAddress);
391                         DrawString2(overlayPixels, 8, 27*FONT_HEIGHT, 0x001FFF3F, transparency, "CRC: %08X", jaguarMainRomCRC32);
392
393                         if (showMsgFrames == 0)
394                         {
395                                 transparency--;
396
397                                 if (transparency == 0)
398 {
399                                         showMessage = false;
400 /*extern bool doGPUDis;
401 doGPUDis = true;//*/
402 }
403
404                         }
405                         else
406                                 showMsgFrames--;
407                 }
408
409                 frameCount++;
410
411                 if (SDL_GetTicks() - elapsedTicks > 250)
412                         elapsedTicks += 250, framesPerSecond = frameCount * 4, frameCount = 0;
413         }
414
415         // Save the background for the GUI...
416         // In this case, we squash the color to monochrome, then force it to blue + green...
417         for(uint32 i=0; i<TOMGetVideoModeWidth() * 256; i++)
418         {
419                 uint32 pixel = backbuffer[i];
420                 uint8 b = (pixel >> 16) & 0xFF, g = (pixel >> 8) & 0xFF, r = pixel & 0xFF;
421                 pixel = ((r + g + b) / 3) & 0x00FF;
422                 backbuffer[i] = 0xFF000000 | (pixel << 16) | (pixel << 8);
423         }
424
425         sdlemuEnableOverlay();
426
427         return NULL;
428 }
429 #endif
430
431 void MainWin::TogglePowerState(void)
432 {
433         powerButtonOn = !powerButtonOn;
434
435         if (!powerButtonOn)
436         {
437                 pauseAct->setChecked(false);
438                 pauseAct->setDisabled(true);
439                 showUntunedTankCircuit = true;
440                 running = true;
441         }
442         else
443         {
444                 showUntunedTankCircuit = (cartridgeLoaded ? false : true);
445                 pauseAct->setChecked(false);
446                 pauseAct->setDisabled(!cartridgeLoaded);
447
448 //(Err, what's so crappy about this? It seems to do what it's supposed to...)
449 //This is crappy!!! !!! FIX !!!
450 //Is this even needed any more? Hmm. Maybe. Dunno.
451 //Seems like it is... But then again, maybe not. Have to test it to see.
452                 WriteLog("GUI: Resetting Jaguar...\n");
453                 JaguarReset();
454                 running = true;
455         }
456 }
457
458 void MainWin::ToggleRunState(void)
459 {
460         running = !running;
461
462         if (!running)
463         {
464 #if 0
465                 for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
466                         for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
467                                 videoWidget->buffer[(y * videoWidget->textureWidth) + x] = 0x00000000;
468 #else
469 //              for(uint32_t i=0; i<TOMGetVideoModeWidth() * 256; i++)
470                 for(uint32_t i=0; i<videoWidget->textureWidth * 256; i++)
471                 {
472                         uint32_t pixel = backbuffer[i];
473 //                      uint8_t b = (pixel >> 16) & 0xFF, g = (pixel >> 8) & 0xFF, r = pixel & 0xFF;
474                         uint8_t r = (pixel >> 24) & 0xFF, g = (pixel >> 16) & 0xFF, b = (pixel >> 8) & 0xFF;
475                         pixel = ((r + g + b) / 3) & 0x00FF;
476 //                      backbuffer[i] = 0xFF000000 | (pixel << 16) | (pixel << 8);
477                         backbuffer[i] = 0x000000FF | (pixel << 16) | (pixel << 8);
478                 }
479
480 //              memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->rasterWidth);
481                 memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->textureWidth * sizeof(uint32_t));
482 #endif
483
484                 videoWidget->updateGL();
485         }
486 }
487
488 void MainWin::SetZoom100(void)
489 {
490         zoomLevel = 1;
491         ResizeMainWindow();
492 }
493
494 void MainWin::SetZoom200(void)
495 {
496         zoomLevel = 2;
497         ResizeMainWindow();
498 }
499
500 void MainWin::SetZoom300(void)
501 {
502         zoomLevel = 3;
503         ResizeMainWindow();
504 }
505
506 void MainWin::SetNTSC(void)
507 {
508         vjs.hardwareTypeNTSC = true;
509         ResizeMainWindow();
510 }
511
512 void MainWin::SetPAL(void)
513 {
514         vjs.hardwareTypeNTSC = false;
515         ResizeMainWindow();
516 }
517
518 void MainWin::ToggleBlur(void)
519 {
520         vjs.glFilter = !vjs.glFilter;
521 }
522
523 void MainWin::ShowAboutWin(void)
524 {
525         aboutWin->show();
526 }
527
528 void MainWin::InsertCart(void)
529 {
530         filePickWin->show();
531 }
532
533 void MainWin::LoadSoftware(QString file)
534 {
535         running = false;                                                        //  Prevent bad things(TM) from happening...
536         SET32(jaguarMainRAM, 0, 0x00200000);            // Set top of stack...
537         cartridgeLoaded = (JaguarLoadFile(file.toAscii().data()) ? true : false);
538
539         powerAct->setDisabled(false);
540         powerAct->setChecked(true);
541         powerButtonOn = false;
542         TogglePowerState();
543
544 //      QString newTitle = QString("Virtual Jaguar v2.0.0 - Now playing: %1")
545         QString newTitle = QString("Virtual Jaguar " VJ_RELEASE_VERSION
546                 " - Now playing: %1")
547                 .arg(filePickWin->GetSelectedPrettyName());
548         setWindowTitle(newTitle);
549 }
550
551 void MainWin::ResizeMainWindow(void)
552 {
553         videoWidget->setFixedSize(zoomLevel * 320, zoomLevel * (vjs.hardwareTypeNTSC ? 240 : 256));
554         show();
555
556         for(int i=0; i<2; i++)
557         {
558                 resize(0, 0);
559                 usleep(2000);
560                 QApplication::processEvents();
561         }
562 }
563
564 void MainWin::ReadSettings(void)
565 {
566         QSettings settings("Underground Software", "Virtual Jaguar");
567         QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
568         QSize size = settings.value("size", QSize(400, 400)).toSize();
569         resize(size);
570         move(pos);
571         pos = settings.value("cartLoadPos", QPoint(200, 200)).toPoint();
572         filePickWin->move(pos);
573
574         zoomLevel = settings.value("zoom", 1).toInt();
575
576         vjs.useJoystick      = settings.value("useJoystick", false).toBool();
577         vjs.joyport          = settings.value("joyport", 0).toInt();
578         vjs.hardwareTypeNTSC = settings.value("hardwareTypeNTSC", true).toBool();
579         vjs.frameSkip        = settings.value("frameSkip", 0).toInt();
580         vjs.useJaguarBIOS    = settings.value("useJaguarBIOS", false).toBool();
581         vjs.DSPEnabled       = settings.value("DSPEnabled", false).toBool();
582         vjs.usePipelinedDSP  = settings.value("usePipelinedDSP", false).toBool();
583         vjs.fullscreen       = settings.value("fullscreen", false).toBool();
584         vjs.useOpenGL        = settings.value("useOpenGL", true).toBool();
585         vjs.glFilter         = settings.value("glFilterType", 0).toInt();
586         vjs.renderType       = settings.value("renderType", 0).toInt();
587         strcpy(vjs.jagBootPath, settings.value("JagBootROM", "./bios/[BIOS] Atari Jaguar (USA, Europe).zip").toString().toAscii().data());
588         strcpy(vjs.CDBootPath, settings.value("CDBootROM", "./bios/jagcd.rom").toString().toAscii().data());
589         strcpy(vjs.EEPROMPath, settings.value("EEPROMs", "./eeproms").toString().toAscii().data());
590         strcpy(vjs.ROMPath, settings.value("ROMs", "./software").toString().toAscii().data());
591 WriteLog("MainWin: Paths\n");
592 WriteLog("    jagBootPath = \"%s\"\n", vjs.jagBootPath);
593 WriteLog("    CDBootPath  = \"%s\"\n", vjs.CDBootPath);
594 WriteLog("    EEPROMPath  = \"%s\"\n", vjs.EEPROMPath);
595 WriteLog("    ROMPath     = \"%s\"\n", vjs.ROMPath);
596
597         // Keybindings in order of U, D, L, R, C, B, A, Op, Pa, 0-9, #, *
598         vjs.p1KeyBindings[BUTTON_U] = settings.value("p1k_up", Qt::Key_Up).toInt();
599         vjs.p1KeyBindings[BUTTON_D] = settings.value("p1k_down", Qt::Key_Down).toInt();
600         vjs.p1KeyBindings[BUTTON_L] = settings.value("p1k_left", Qt::Key_Left).toInt();
601         vjs.p1KeyBindings[BUTTON_R] = settings.value("p1k_right", Qt::Key_Right).toInt();
602         vjs.p1KeyBindings[BUTTON_C] = settings.value("p1k_c", Qt::Key_Z).toInt();
603         vjs.p1KeyBindings[BUTTON_B] = settings.value("p1k_b", Qt::Key_X).toInt();
604         vjs.p1KeyBindings[BUTTON_A] = settings.value("p1k_a", Qt::Key_C).toInt();
605         vjs.p1KeyBindings[BUTTON_OPTION] = settings.value("p1k_option", Qt::Key_Apostrophe).toInt();
606         vjs.p1KeyBindings[BUTTON_PAUSE] = settings.value("p1k_pause", Qt::Key_Return).toInt();
607         vjs.p1KeyBindings[BUTTON_0] = settings.value("p1k_0", Qt::Key_0).toInt();
608         vjs.p1KeyBindings[BUTTON_1] = settings.value("p1k_1", Qt::Key_1).toInt();
609         vjs.p1KeyBindings[BUTTON_2] = settings.value("p1k_2", Qt::Key_2).toInt();
610         vjs.p1KeyBindings[BUTTON_3] = settings.value("p1k_3", Qt::Key_3).toInt();
611         vjs.p1KeyBindings[BUTTON_4] = settings.value("p1k_4", Qt::Key_4).toInt();
612         vjs.p1KeyBindings[BUTTON_5] = settings.value("p1k_5", Qt::Key_5).toInt();
613         vjs.p1KeyBindings[BUTTON_6] = settings.value("p1k_6", Qt::Key_6).toInt();
614         vjs.p1KeyBindings[BUTTON_7] = settings.value("p1k_7", Qt::Key_7).toInt();
615         vjs.p1KeyBindings[BUTTON_8] = settings.value("p1k_8", Qt::Key_8).toInt();
616         vjs.p1KeyBindings[BUTTON_9] = settings.value("p1k_9", Qt::Key_9).toInt();
617         vjs.p1KeyBindings[BUTTON_d] = settings.value("p1k_pound", Qt::Key_Slash).toInt();
618         vjs.p1KeyBindings[BUTTON_s] = settings.value("p1k_star", Qt::Key_Asterisk).toInt();
619
620         vjs.p2KeyBindings[BUTTON_U] = settings.value("p2k_up", Qt::Key_Up).toInt();
621         vjs.p2KeyBindings[BUTTON_D] = settings.value("p2k_down", Qt::Key_Down).toInt();
622         vjs.p2KeyBindings[BUTTON_L] = settings.value("p2k_left", Qt::Key_Left).toInt();
623         vjs.p2KeyBindings[BUTTON_R] = settings.value("p2k_right", Qt::Key_Right).toInt();
624         vjs.p2KeyBindings[BUTTON_C] = settings.value("p2k_c", Qt::Key_Z).toInt();
625         vjs.p2KeyBindings[BUTTON_B] = settings.value("p2k_b", Qt::Key_X).toInt();
626         vjs.p2KeyBindings[BUTTON_A] = settings.value("p2k_a", Qt::Key_C).toInt();
627         vjs.p2KeyBindings[BUTTON_OPTION] = settings.value("p2k_option", Qt::Key_Apostrophe).toInt();
628         vjs.p2KeyBindings[BUTTON_PAUSE] = settings.value("p2k_pause", Qt::Key_Return).toInt();
629         vjs.p2KeyBindings[BUTTON_0] = settings.value("p2k_0", Qt::Key_0).toInt();
630         vjs.p2KeyBindings[BUTTON_1] = settings.value("p2k_1", Qt::Key_1).toInt();
631         vjs.p2KeyBindings[BUTTON_2] = settings.value("p2k_2", Qt::Key_2).toInt();
632         vjs.p2KeyBindings[BUTTON_3] = settings.value("p2k_3", Qt::Key_3).toInt();
633         vjs.p2KeyBindings[BUTTON_4] = settings.value("p2k_4", Qt::Key_4).toInt();
634         vjs.p2KeyBindings[BUTTON_5] = settings.value("p2k_5", Qt::Key_5).toInt();
635         vjs.p2KeyBindings[BUTTON_6] = settings.value("p2k_6", Qt::Key_6).toInt();
636         vjs.p2KeyBindings[BUTTON_7] = settings.value("p2k_7", Qt::Key_7).toInt();
637         vjs.p2KeyBindings[BUTTON_8] = settings.value("p2k_8", Qt::Key_8).toInt();
638         vjs.p2KeyBindings[BUTTON_9] = settings.value("p2k_9", Qt::Key_9).toInt();
639         vjs.p2KeyBindings[BUTTON_d] = settings.value("p2k_pound", Qt::Key_Slash).toInt();
640         vjs.p2KeyBindings[BUTTON_s] = settings.value("p2k_star", Qt::Key_Asterisk).toInt();
641 }
642
643 void MainWin::WriteSettings(void)
644 {
645         QSettings settings("Underground Software", "Virtual Jaguar");
646         settings.setValue("pos", pos());
647         settings.setValue("size", size());
648         settings.setValue("cartLoadPos", filePickWin->pos());
649
650         settings.setValue("zoom", zoomLevel);
651
652         settings.setValue("useJoystick", vjs.useJoystick);
653         settings.setValue("joyport", vjs.joyport);
654         settings.setValue("hardwareTypeNTSC", vjs.hardwareTypeNTSC);
655         settings.setValue("frameSkip", vjs.frameSkip);
656         settings.setValue("useJaguarBIOS", vjs.useJaguarBIOS);
657         settings.setValue("DSPEnabled", vjs.DSPEnabled);
658         settings.setValue("usePipelinedDSP", vjs.usePipelinedDSP);
659         settings.setValue("fullscreen", vjs.fullscreen);
660         settings.setValue("useOpenGL", vjs.useOpenGL);
661         settings.setValue("glFilterType", vjs.glFilter);
662         settings.setValue("renderType", vjs.renderType);
663         settings.setValue("JagBootROM", vjs.jagBootPath);
664         settings.setValue("CDBootROM", vjs.CDBootPath);
665         settings.setValue("EEPROMs", vjs.EEPROMPath);
666         settings.setValue("ROMs", vjs.ROMPath);
667
668         settings.setValue("p1k_up", vjs.p1KeyBindings[BUTTON_U]);
669         settings.setValue("p1k_down", vjs.p1KeyBindings[BUTTON_D]);
670         settings.setValue("p1k_left", vjs.p1KeyBindings[BUTTON_L]);
671         settings.setValue("p1k_right", vjs.p1KeyBindings[BUTTON_R]);
672         settings.setValue("p1k_c", vjs.p1KeyBindings[BUTTON_C]);
673         settings.setValue("p1k_b", vjs.p1KeyBindings[BUTTON_B]);
674         settings.setValue("p1k_a", vjs.p1KeyBindings[BUTTON_A]);
675         settings.setValue("p1k_option", vjs.p1KeyBindings[BUTTON_OPTION]);
676         settings.setValue("p1k_pause", vjs.p1KeyBindings[BUTTON_PAUSE]);
677         settings.setValue("p1k_0", vjs.p1KeyBindings[BUTTON_0]);
678         settings.setValue("p1k_1", vjs.p1KeyBindings[BUTTON_1]);
679         settings.setValue("p1k_2", vjs.p1KeyBindings[BUTTON_2]);
680         settings.setValue("p1k_3", vjs.p1KeyBindings[BUTTON_3]);
681         settings.setValue("p1k_4", vjs.p1KeyBindings[BUTTON_4]);
682         settings.setValue("p1k_5", vjs.p1KeyBindings[BUTTON_5]);
683         settings.setValue("p1k_6", vjs.p1KeyBindings[BUTTON_6]);
684         settings.setValue("p1k_7", vjs.p1KeyBindings[BUTTON_7]);
685         settings.setValue("p1k_8", vjs.p1KeyBindings[BUTTON_8]);
686         settings.setValue("p1k_9", vjs.p1KeyBindings[BUTTON_9]);
687         settings.setValue("p1k_pound", vjs.p1KeyBindings[BUTTON_d]);
688         settings.setValue("p1k_star", vjs.p1KeyBindings[BUTTON_s]);
689
690         settings.setValue("p2k_up", vjs.p2KeyBindings[BUTTON_U]);
691         settings.setValue("p2k_down", vjs.p2KeyBindings[BUTTON_D]);
692         settings.setValue("p2k_left", vjs.p2KeyBindings[BUTTON_L]);
693         settings.setValue("p2k_right", vjs.p2KeyBindings[BUTTON_R]);
694         settings.setValue("p2k_c", vjs.p2KeyBindings[BUTTON_C]);
695         settings.setValue("p2k_b", vjs.p2KeyBindings[BUTTON_B]);
696         settings.setValue("p2k_a", vjs.p2KeyBindings[BUTTON_A]);
697         settings.setValue("p2k_option", vjs.p2KeyBindings[BUTTON_OPTION]);
698         settings.setValue("p2k_pause", vjs.p2KeyBindings[BUTTON_PAUSE]);
699         settings.setValue("p2k_0", vjs.p2KeyBindings[BUTTON_0]);
700         settings.setValue("p2k_1", vjs.p2KeyBindings[BUTTON_1]);
701         settings.setValue("p2k_2", vjs.p2KeyBindings[BUTTON_2]);
702         settings.setValue("p2k_3", vjs.p2KeyBindings[BUTTON_3]);
703         settings.setValue("p2k_4", vjs.p2KeyBindings[BUTTON_4]);
704         settings.setValue("p2k_5", vjs.p2KeyBindings[BUTTON_5]);
705         settings.setValue("p2k_6", vjs.p2KeyBindings[BUTTON_6]);
706         settings.setValue("p2k_7", vjs.p2KeyBindings[BUTTON_7]);
707         settings.setValue("p2k_8", vjs.p2KeyBindings[BUTTON_8]);
708         settings.setValue("p2k_9", vjs.p2KeyBindings[BUTTON_9]);
709         settings.setValue("p2k_pound", vjs.p2KeyBindings[BUTTON_d]);
710         settings.setValue("p2k_star", vjs.p2KeyBindings[BUTTON_s]);
711 }
712
713 // Here's how Byuu does it...
714 // I think I have it working now... :-)
715 #if 0
716 void Utility::resizeMainWindow()
717 {
718   unsigned region = config().video.context->region;
719   unsigned multiplier = config().video.context->multiplier;
720   unsigned width = 256 * multiplier;
721   unsigned height = (region == 0 ? 224 : 239) * multiplier;
722
723   if(config().video.context->correctAspectRatio)
724   {
725     if(region == 0)
726         {
727       width = (double)width * config().video.ntscAspectRatio + 0.5;  //NTSC adjust
728     }
729         else
730         {
731       width = (double)width * config().video.palAspectRatio  + 0.5;  //PAL adjust
732     }
733   }
734
735   if(config().video.isFullscreen == false)
736   {
737     //get effective desktop work area region (ignore Windows taskbar, OS X dock, etc.)
738     QRect deskRect = QApplication::desktop()->availableGeometry(mainWindow);
739
740     //ensure window size will not be larger than viewable desktop area
741     constrainSize(height, width, deskRect.height()); //- frameHeight);
742     constrainSize(width, height, deskRect.width());  //- frameWidth );
743
744     mainWindow->canvas->setFixedSize(width, height);
745     mainWindow->show();
746   }
747   else
748   {
749     for(unsigned i = 0; i < 2; i++)
750         {
751       unsigned iWidth = width, iHeight = height;
752
753       constrainSize(iHeight, iWidth, mainWindow->canvasContainer->size().height());
754       constrainSize(iWidth, iHeight, mainWindow->canvasContainer->size().width());
755
756       //center canvas onscreen; ensure it is not larger than viewable area
757       mainWindow->canvas->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
758       mainWindow->canvas->setFixedSize(iWidth, iHeight);
759       mainWindow->canvas->setMinimumSize(0, 0);
760
761       usleep(2000);
762       QApplication::processEvents();
763     }
764   }
765
766   //workaround for Qt/Xlib bug:
767   //if window resize occurs with cursor over it, Qt shows Qt::Size*DiagCursor;
768   //so force it to show Qt::ArrowCursor, as expected
769   mainWindow->setCursor(Qt::ArrowCursor);
770   mainWindow->canvasContainer->setCursor(Qt::ArrowCursor);
771   mainWindow->canvas->setCursor(Qt::ArrowCursor);
772
773   //workaround for DirectSound(?) bug:
774   //window resizing sometimes breaks audio sync, this call re-initializes it
775   updateAvSync();
776 }
777
778 void Utility::setScale(unsigned scale)
779 {
780   config().video.context->multiplier = scale;
781   resizeMainWindow();
782   mainWindow->shrink();
783   mainWindow->syncUi();
784 }
785
786 void QbWindow::shrink()
787 {
788   if(config().video.isFullscreen == false)
789   {
790     for(unsigned i = 0; i < 2; i++)
791         {
792       resize(0, 0);
793       usleep(2000);
794       QApplication::processEvents();
795     }
796   }
797 }
798 #endif