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