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