]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/mainwin.cpp
Fixed GUI to be able to load carts, updated documentation.
[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 <QtGui>
29 //#include <QtOpenGL>
30 #include "glwidget.h"
31 #include "about.h"
32 #include "settings.h"
33 #include "filepicker.h"
34
35 #include "jaguar.h"
36 #include "video.h"
37 #include "tom.h"
38 #include "log.h"
39 #include "file.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(): showUntunedTankCircuit(true)
67 {
68         videoWidget = new GLWidget(this);
69         setCentralWidget(videoWidget);
70         setWindowIcon(QIcon(":/res/vj.xpm"));
71         setWindowTitle("Virtual Jaguar v2.0.0");
72
73         ReadSettings();
74         setUnifiedTitleAndToolBarOnMac(true);
75
76         aboutWin = new AboutWindow(this);
77         filePickWin = new FilePickerWindow(this);
78
79     videoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
80     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
81
82         // Create actions
83
84         quitAppAct = new QAction(tr("E&xit"), this);
85         quitAppAct->setShortcuts(QKeySequence::Quit);
86         quitAppAct->setStatusTip(tr("Quit Virtual Jaguar"));
87         connect(quitAppAct, SIGNAL(triggered()), this, SLOT(close()));
88
89         powerAct = new QAction(QIcon(":/res/power.png"), tr("&Power"), this);
90         powerAct->setStatusTip(tr("Toggle running state"));
91         powerAct->setCheckable(true);
92         connect(powerAct, SIGNAL(triggered()), this, SLOT(ToggleRunState()));
93
94         zoomActs = new QActionGroup(this);
95
96         x1Act = new QAction(QIcon(":/res/zoom100.png"), tr("Zoom 100%"), zoomActs);
97         x1Act->setStatusTip(tr("Set window zoom to 100%"));
98         x1Act->setCheckable(true);
99         connect(x1Act, SIGNAL(triggered()), this, SLOT(SetZoom100()));
100
101         x2Act = new QAction(QIcon(":/res/zoom200.png"), tr("Zoom 200%"), zoomActs);
102         x2Act->setStatusTip(tr("Set window zoom to 200%"));
103         x2Act->setCheckable(true);
104         connect(x2Act, SIGNAL(triggered()), this, SLOT(SetZoom200()));
105
106         x3Act = new QAction(QIcon(":/res/zoom300.png"), tr("Zoom 300%"), zoomActs);
107         x3Act->setStatusTip(tr("Set window zoom to 300%"));
108         x3Act->setCheckable(true);
109         connect(x3Act, SIGNAL(triggered()), this, SLOT(SetZoom300()));
110
111         tvTypeActs = new QActionGroup(this);
112
113         ntscAct = new QAction(QIcon(":/res/generic.png"), tr("NTSC"), tvTypeActs);
114         ntscAct->setStatusTip(tr("Sets Jaguar to NTSC mode"));
115         ntscAct->setCheckable(true);
116         connect(ntscAct, SIGNAL(triggered()), this, SLOT(SetNTSC()));
117
118         palAct = new QAction(QIcon(":/res/generic.png"), tr("PAL"), tvTypeActs);
119         palAct->setStatusTip(tr("Sets Jaguar to PAL mode"));
120         palAct->setCheckable(true);
121         connect(palAct, SIGNAL(triggered()), this, SLOT(SetPAL()));
122
123         blurAct = new QAction(QIcon(":/res/generic.png"), tr("Blur"), this);
124         blurAct->setStatusTip(tr("Sets OpenGL rendering to GL_NEAREST"));
125         blurAct->setCheckable(true);
126         connect(blurAct, SIGNAL(triggered()), this, SLOT(ToggleBlur()));
127
128         aboutAct = new QAction(QIcon(":/res/generic.png"), tr("&About..."), this);
129         aboutAct->setStatusTip(tr("Blatant self-promotion"));
130         connect(aboutAct, SIGNAL(triggered()), this, SLOT(ShowAboutWin()));
131
132         filePickAct = new QAction(QIcon(":/res/generic.png"), tr("&Insert Cartridge..."), this);
133         filePickAct->setStatusTip(tr("Insert a cartridge into Virtual Jaguar"));
134         connect(filePickAct, SIGNAL(triggered()), this, SLOT(InsertCart()));
135
136         // Misc. connections...
137         connect(filePickWin, SIGNAL(RequestLoad(QString)), this, SLOT(LoadSoftware(QString)));
138
139         // Create menus & toolbars
140
141         fileMenu = menuBar()->addMenu(tr("&File"));
142         fileMenu->addAction(filePickAct);
143         fileMenu->addAction(powerAct);
144         fileMenu->addAction(quitAppAct);
145
146         helpMenu = menuBar()->addMenu(tr("&Help"));
147         helpMenu->addAction(aboutAct);
148
149         toolbar = addToolBar(tr("Stuff"));
150         toolbar->addAction(powerAct);
151         toolbar->addAction(filePickAct);
152         toolbar->addSeparator();
153         toolbar->addAction(x1Act);
154         toolbar->addAction(x2Act);
155         toolbar->addAction(x3Act);
156         toolbar->addSeparator();
157         toolbar->addAction(ntscAct);
158         toolbar->addAction(palAct);
159         toolbar->addSeparator();
160         toolbar->addAction(blurAct);
161
162         //      Create status bar
163         statusBar()->showMessage(tr("Ready"));
164
165         // Set toolbar buttons/menus based on settings read in (sync the UI)...
166         blurAct->setChecked(vjs.glFilter);
167         x1Act->setChecked(zoomLevel == 1);
168         x2Act->setChecked(zoomLevel == 2);
169         x3Act->setChecked(zoomLevel == 3);
170         running = powerAct->isChecked();
171         ntscAct->setChecked(vjs.hardwareTypeNTSC);
172         palAct->setChecked(!vjs.hardwareTypeNTSC);
173
174         // Do this in case original size isn't correct (mostly for the first-run case)
175         ResizeMainWindow();
176
177         // Set up timer based loop for animation...
178         timer = new QTimer(this);
179         connect(timer, SIGNAL(timeout()), this, SLOT(Timer()));
180         timer->start(20);
181
182         // NOTE: Keyboards/joysticks will *not* work until SDL is brought back in, or
183         //       the key handling is improved in Qt...
184         // Wait a minute... it seems they already are... So why no keyboard love?
185
186 #ifdef VJ_RELEASE_VERSION
187         WriteLog("Virtual Jaguar %s (Last full build was on %s %s)\n", VJ_RELEASE_VERSION, __DATE__, __TIME__);
188 #else
189         WriteLog("Virtual Jaguar SVN %s (Last full build was on %s %s)\n", __DATE__, __DATE__, __TIME__);
190 #endif
191         WriteLog("Initializing jaguar subsystem...\n");
192         JaguarInit();
193
194         // Get the BIOS ROM
195 #ifdef USE_BUILT_IN_BIOS
196         WriteLog("VJ: Using built in BIOS/CD BIOS...\n");
197         memcpy(jaguarBootROM, jagBootROM, 0x20000);
198         memcpy(jaguarCDBootROM, jagCDROM, 0x40000);
199         BIOSLoaded = CDBIOSLoaded = true;
200 #else
201 // What would be nice here would be a way to check if the BIOS was loaded so that we
202 // could disable the pushbutton on the Misc Options menu... !!! FIX !!! [DONE here, but needs to be fixed in GUI as well!]
203 WriteLog("About to attempt to load BIOSes...\n");
204 #if 1
205 //This is short-circuiting the file finding thread... ??? WHY ???
206         BIOSLoaded = (JaguarLoadROM(jaguarBootROM, vjs.jagBootPath) == 0x20000 ? true : false);
207 #else
208         BIOSLoaded = false;
209 #endif
210         WriteLog("VJ: BIOS is %savailable...\n", (BIOSLoaded ? "" : "not "));
211         CDBIOSLoaded = (JaguarLoadROM(jaguarCDBootROM, vjs.CDBootPath) == 0x40000 ? true : false);
212         WriteLog("VJ: CD BIOS is %savailable...\n", (CDBIOSLoaded ? "" : "not "));
213 #endif
214
215         SET32(jaguarMainRAM, 0, 0x00200000);                    // Set top of stack...
216
217 //Let's try this...
218 //      JaguarLoadFile("./software/Rayman (World).j64");
219 //      JaguarLoadFile("./software/I-War (World).j64");
220 //      JaguarLoadFile("./software/Alien vs Predator (World).j64");
221 //no    JaguarLoadFile("./software/battlesphere.bin");
222 //      JaguarLoadFile("./software/Battle Sphere Gold (World).j64");
223 //      JaguarLoadFile("./software/Rayman (USA, Europe).zip");
224 //This is crappy!!! !!! FIX !!!
225 //Is this even needed any more? Hmm. Maybe. Dunno.
226 //Seems like it is... But then again, maybe not. Have to test it to see.
227 //WriteLog("GUI: Resetting Jaguar...\n");
228 //      JaguarReset();
229 }
230
231 void MainWin::closeEvent(QCloseEvent * event)
232 {
233         WriteSettings();
234         event->accept(); // ignore() if can't close for some reason
235 }
236
237 void MainWin::Open(void)
238 {
239 }
240
241 //
242 // Here's the main emulator loop
243 //
244 void MainWin::Timer(void)
245 {
246         if (!running)
247                 return;
248
249         if (showUntunedTankCircuit)
250         {
251                 // Random hash & trash
252                 // We try to simulate an untuned tank circuit here... :-)
253                 for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
254                 {
255                         for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
256                         {
257                                 videoWidget->buffer[(y * videoWidget->textureWidth) + x] = (rand() & 0xFF) << 8 | (rand() & 0xFF) << 16 | (rand() & 0xFF) << 24;// | (rand() & 0xFF);//0x000000FF;
258         //                      buffer[(y * textureWidth) + x] = x*y;
259                         }
260                 }
261         }
262         else
263         {
264                 // Otherwise, run the Jaguar simulation
265                 JaguarExecuteNew();
266 //              memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->rasterWidth);
267                 memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->textureWidth * sizeof(uint32_t));
268 //              memcpy(surface->pixels, backbuffer, TOMGetVideoModeWidth() * TOMGetVideoModeHeight() * 4);
269         }
270
271         videoWidget->updateGL();
272 }
273
274 #if 0
275 Window * RunEmu(void)
276 {
277 //      extern uint32 * backbuffer;
278         uint32 * overlayPixels = (uint32 *)sdlemuGetOverlayPixels();
279         memset(overlayPixels, 0x00, 640 * 480 * 4);                     // Clear out overlay...
280
281 //This is crappy... !!! FIX !!!
282 //      extern bool finished, showGUI;
283
284         sdlemuDisableOverlay();
285
286 //      uint32 nFrame = 0, nFrameskip = 0;
287         uint32 totalFrames = 0;
288         finished = false;
289         bool showMessage = true;
290         uint32 showMsgFrames = 120;
291         uint8 transparency = 0xFF;
292         // Pass a message to the "joystick" code to debounce the ESC key...
293         debounceRunKey = true;
294
295         uint32 cartType = 4;
296         if (jaguarRomSize == 0x200000)
297                 cartType = 0;
298         else if (jaguarRomSize == 0x400000)
299                 cartType = 1;
300         else if (jaguarMainRomCRC32 == 0x687068D5)
301                 cartType = 2;
302         else if (jaguarMainRomCRC32 == 0x55A0669C)
303                 cartType = 3;
304
305         const char * cartTypeName[5] = { "2M Cartridge", "4M Cartridge", "CD BIOS", "CD Dev BIOS", "Homebrew" };
306         uint32 elapsedTicks = SDL_GetTicks(), frameCount = 0, framesPerSecond = 0;
307
308         while (!finished)
309         {
310                 // Set up new backbuffer with new pixels and data
311                 JaguarExecuteNew();
312                 totalFrames++;
313 //WriteLog("Frame #%u...\n", totalFrames);
314 //extern bool doDSPDis;
315 //if (totalFrames == 373)
316 //      doDSPDis = true;
317
318 //Problem: Need to do this *only* when the state changes from visible to not...
319 //Also, need to clear out the GUI when not on (when showMessage is active...)
320 if (showGUI || showMessage)
321         sdlemuEnableOverlay();
322 else
323         sdlemuDisableOverlay();
324
325 //Add in a new function for clearing patches of screen (ClearOverlayRect)
326
327 // Also: Take frame rate into account when calculating fade time...
328
329                 // Some QnD GUI stuff here...
330                 if (showGUI)
331                 {
332                         FillScreenRectangle(overlayPixels, 8, 1*FONT_HEIGHT, 128, 4*FONT_HEIGHT, 0x00000000);
333                         extern uint32 gpu_pc, dsp_pc;
334                         DrawString(overlayPixels, 8, 1*FONT_HEIGHT, false, "GPU PC: %08X", gpu_pc);
335                         DrawString(overlayPixels, 8, 2*FONT_HEIGHT, false, "DSP PC: %08X", dsp_pc);
336                         DrawString(overlayPixels, 8, 4*FONT_HEIGHT, false, "%u FPS", framesPerSecond);
337                 }
338
339                 if (showMessage)
340                 {
341                         DrawString2(overlayPixels, 8, 24*FONT_HEIGHT, 0x007F63FF, transparency, "Running...");
342                         DrawString2(overlayPixels, 8, 26*FONT_HEIGHT, 0x001FFF3F, transparency, "%s, run address: %06X", cartTypeName[cartType], jaguarRunAddress);
343                         DrawString2(overlayPixels, 8, 27*FONT_HEIGHT, 0x001FFF3F, transparency, "CRC: %08X", jaguarMainRomCRC32);
344
345                         if (showMsgFrames == 0)
346                         {
347                                 transparency--;
348
349                                 if (transparency == 0)
350 {
351                                         showMessage = false;
352 /*extern bool doGPUDis;
353 doGPUDis = true;//*/
354 }
355
356                         }
357                         else
358                                 showMsgFrames--;
359                 }
360
361                 frameCount++;
362
363                 if (SDL_GetTicks() - elapsedTicks > 250)
364                         elapsedTicks += 250, framesPerSecond = frameCount * 4, frameCount = 0;
365         }
366
367         // Save the background for the GUI...
368         // In this case, we squash the color to monochrome, then force it to blue + green...
369         for(uint32 i=0; i<TOMGetVideoModeWidth() * 256; i++)
370         {
371                 uint32 pixel = backbuffer[i];
372                 uint8 b = (pixel >> 16) & 0xFF, g = (pixel >> 8) & 0xFF, r = pixel & 0xFF;
373                 pixel = ((r + g + b) / 3) & 0x00FF;
374                 backbuffer[i] = 0xFF000000 | (pixel << 16) | (pixel << 8);
375         }
376
377         sdlemuEnableOverlay();
378
379         return NULL;
380 }
381 #endif
382
383 void MainWin::ToggleRunState(void)
384 {
385         running = !running;
386
387         if (!running)
388         {
389 #if 0
390                 for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
391                         for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
392                                 videoWidget->buffer[(y * videoWidget->textureWidth) + x] = 0x00000000;
393 #else
394 //              for(uint32_t i=0; i<TOMGetVideoModeWidth() * 256; i++)
395                 for(uint32_t i=0; i<videoWidget->textureWidth * 256; i++)
396                 {
397                         uint32_t pixel = backbuffer[i];
398 //                      uint8_t b = (pixel >> 16) & 0xFF, g = (pixel >> 8) & 0xFF, r = pixel & 0xFF;
399                         uint8_t r = (pixel >> 24) & 0xFF, g = (pixel >> 16) & 0xFF, b = (pixel >> 8) & 0xFF;
400                         pixel = ((r + g + b) / 3) & 0x00FF;
401 //                      backbuffer[i] = 0xFF000000 | (pixel << 16) | (pixel << 8);
402                         backbuffer[i] = 0x000000FF | (pixel << 16) | (pixel << 8);
403                 }
404
405 //              memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->rasterWidth);
406                 memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->textureWidth * sizeof(uint32_t));
407 #endif
408
409                 videoWidget->updateGL();
410         }
411 }
412
413 void MainWin::SetZoom100(void)
414 {
415         zoomLevel = 1;
416         ResizeMainWindow();
417 }
418
419 void MainWin::SetZoom200(void)
420 {
421         zoomLevel = 2;
422         ResizeMainWindow();
423 }
424
425 void MainWin::SetZoom300(void)
426 {
427         zoomLevel = 3;
428         ResizeMainWindow();
429 }
430
431 void MainWin::SetNTSC(void)
432 {
433         vjs.hardwareTypeNTSC = true;
434         ResizeMainWindow();
435 }
436
437 void MainWin::SetPAL(void)
438 {
439         vjs.hardwareTypeNTSC = false;
440         ResizeMainWindow();
441 }
442
443 void MainWin::ToggleBlur(void)
444 {
445         vjs.glFilter = !vjs.glFilter;
446 }
447
448 void MainWin::ShowAboutWin(void)
449 {
450         aboutWin->show();
451 }
452
453 void MainWin::InsertCart(void)
454 {
455         filePickWin->show();
456 }
457
458 void MainWin::LoadSoftware(QString file)
459 {
460         running = false;                                                        //  Prevent bad things(TM) from happening...
461         SET32(jaguarMainRAM, 0, 0x00200000);                    // Set top of stack...
462         showUntunedTankCircuit = (JaguarLoadFile(file.toAscii().data()) ? false : true);
463 //This is crappy!!! !!! FIX !!!
464 //Is this even needed any more? Hmm. Maybe. Dunno.
465 //Seems like it is... But then again, maybe not. Have to test it to see.
466         WriteLog("GUI: Resetting Jaguar...\n");
467         JaguarReset();
468         running = true;
469 }
470
471 void MainWin::ResizeMainWindow(void)
472 {
473         videoWidget->setFixedSize(zoomLevel * 320, zoomLevel * (vjs.hardwareTypeNTSC ? 240 : 256));
474         show();
475
476         for(int i=0; i<2; i++)
477         {
478                 resize(0, 0);
479                 usleep(2000);
480                 QApplication::processEvents();
481         }
482 }
483
484 void MainWin::ReadSettings(void)
485 {
486         QSettings settings("Underground Software", "Virtual Jaguar");
487         QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
488         QSize size = settings.value("size", QSize(400, 400)).toSize();
489         resize(size);
490         move(pos);
491
492         zoomLevel = settings.value("zoom", 1).toInt();
493
494         vjs.useJoystick      = settings.value("useJoystick", false).toBool();
495         vjs.joyport          = settings.value("joyport", 0).toInt();
496         vjs.hardwareTypeNTSC = settings.value("hardwareTypeNTSC", true).toBool();
497         vjs.frameSkip        = settings.value("frameSkip", 0).toInt();
498         vjs.useJaguarBIOS    = settings.value("useJaguarBIOS", false).toBool();
499         vjs.DSPEnabled       = settings.value("DSPEnabled", false).toBool();
500         vjs.usePipelinedDSP  = settings.value("usePipelinedDSP", false).toBool();
501         vjs.fullscreen       = settings.value("fullscreen", false).toBool();
502         vjs.useOpenGL        = settings.value("useOpenGL", true).toBool();
503         vjs.glFilter         = settings.value("glFilterType", 0).toInt();
504         vjs.renderType       = settings.value("renderType", 0).toInt();
505         strcpy(vjs.jagBootPath, settings.value("JagBootROM", "./bios/[BIOS] Atari Jaguar (USA, Europe).zip").toString().toAscii().data());
506         strcpy(vjs.CDBootPath, settings.value("CDBootROM", "./bios/jagcd.rom").toString().toAscii().data());
507         strcpy(vjs.EEPROMPath, settings.value("EEPROMs", "./eeproms").toString().toAscii().data());
508         strcpy(vjs.ROMPath, settings.value("ROMs", "./software").toString().toAscii().data());
509 WriteLog("MainWin: Paths\n");
510 WriteLog("    jagBootPath = \"%s\"\n", vjs.jagBootPath);
511 WriteLog("    CDBootPath  = \"%s\"\n", vjs.CDBootPath);
512 WriteLog("    EEPROMPath  = \"%s\"\n", vjs.EEPROMPath);
513 WriteLog("    ROMPath     = \"%s\"\n", vjs.ROMPath);
514 }
515
516 void MainWin::WriteSettings(void)
517 {
518         QSettings settings("Underground Software", "Virtual Jaguar");
519         settings.setValue("pos", pos());
520         settings.setValue("size", size());
521
522         settings.setValue("zoom", zoomLevel);
523
524         settings.setValue("useJoystick", vjs.useJoystick);
525         settings.setValue("joyport", vjs.joyport);
526         settings.setValue("hardwareTypeNTSC", vjs.hardwareTypeNTSC);
527         settings.setValue("frameSkip", vjs.frameSkip);
528         settings.setValue("useJaguarBIOS", vjs.useJaguarBIOS);
529         settings.setValue("DSPEnabled", vjs.DSPEnabled);
530         settings.setValue("usePipelinedDSP", vjs.usePipelinedDSP);
531         settings.setValue("fullscreen", vjs.fullscreen);
532         settings.setValue("useOpenGL", vjs.useOpenGL);
533         settings.setValue("glFilterType", vjs.glFilter);
534         settings.setValue("renderType", vjs.renderType);
535         settings.setValue("JagBootROM", vjs.jagBootPath);
536         settings.setValue("CDBootROM", vjs.CDBootPath);
537         settings.setValue("EEPROMs", vjs.EEPROMPath);
538         settings.setValue("ROMs", vjs.ROMPath);
539 }
540
541 // Here's how Byuu does it...
542 // I think I have it working now... :-)
543 #if 0
544 void Utility::resizeMainWindow()
545 {
546   unsigned region = config().video.context->region;
547   unsigned multiplier = config().video.context->multiplier;
548   unsigned width = 256 * multiplier;
549   unsigned height = (region == 0 ? 224 : 239) * multiplier;
550
551   if(config().video.context->correctAspectRatio)
552   {
553     if(region == 0)
554         {
555       width = (double)width * config().video.ntscAspectRatio + 0.5;  //NTSC adjust
556     }
557         else
558         {
559       width = (double)width * config().video.palAspectRatio  + 0.5;  //PAL adjust
560     }
561   }
562
563   if(config().video.isFullscreen == false)
564   {
565     //get effective desktop work area region (ignore Windows taskbar, OS X dock, etc.)
566     QRect deskRect = QApplication::desktop()->availableGeometry(mainWindow);
567
568     //ensure window size will not be larger than viewable desktop area
569     constrainSize(height, width, deskRect.height()); //- frameHeight);
570     constrainSize(width, height, deskRect.width());  //- frameWidth );
571
572     mainWindow->canvas->setFixedSize(width, height);
573     mainWindow->show();
574   }
575   else
576   {
577     for(unsigned i = 0; i < 2; i++)
578         {
579       unsigned iWidth = width, iHeight = height;
580
581       constrainSize(iHeight, iWidth, mainWindow->canvasContainer->size().height());
582       constrainSize(iWidth, iHeight, mainWindow->canvasContainer->size().width());
583
584       //center canvas onscreen; ensure it is not larger than viewable area
585       mainWindow->canvas->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
586       mainWindow->canvas->setFixedSize(iWidth, iHeight);
587       mainWindow->canvas->setMinimumSize(0, 0);
588
589       usleep(2000);
590       QApplication::processEvents();
591     }
592   }
593
594   //workaround for Qt/Xlib bug:
595   //if window resize occurs with cursor over it, Qt shows Qt::Size*DiagCursor;
596   //so force it to show Qt::ArrowCursor, as expected
597   mainWindow->setCursor(Qt::ArrowCursor);
598   mainWindow->canvasContainer->setCursor(Qt::ArrowCursor);
599   mainWindow->canvas->setCursor(Qt::ArrowCursor);
600
601   //workaround for DirectSound(?) bug:
602   //window resizing sometimes breaks audio sync, this call re-initializes it
603   updateAvSync();
604 }
605
606 void Utility::setScale(unsigned scale)
607 {
608   config().video.context->multiplier = scale;
609   resizeMainWindow();
610   mainWindow->shrink();
611   mainWindow->syncUi();
612 }
613
614 void QbWindow::shrink()
615 {
616   if(config().video.isFullscreen == false)
617   {
618     for(unsigned i = 0; i < 2; i++)
619         {
620       resize(0, 0);
621       usleep(2000);
622       QApplication::processEvents();
623     }
624   }
625 }
626 #endif