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