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