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