remove unused/unmaintained code
[picodrive.git] / platform / uiq3 / engine / main.cpp
diff --git a/platform/uiq3/engine/main.cpp b/platform/uiq3/engine/main.cpp
deleted file mode 100644 (file)
index 66fee33..0000000
+++ /dev/null
@@ -1,921 +0,0 @@
-// mainloop with window server event handling\r
-// event polling mechnism was taken from\r
-// Peter van Sebille's projects\r
-\r
-// (c) Copyright 2006, notaz\r
-// All Rights Reserved\r
-\r
-#include <e32base.h>\r
-#include <hal.h>\r
-#include <e32keys.h>\r
-#include <w32std.h>\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <sys/time.h>\r
-\r
-#include "debug.h"\r
-#include "../Engine.h"\r
-\r
-#include <pico/pico_int.h>\r
-#include "../../common/emu.h"\r
-#include "../emu.h"\r
-#include "vid.h"\r
-#include "PolledAS.h"\r
-//#include "audio.h"\r
-#include "audio_mediaserver.h"\r
-\r
-//#include <ezlib.h>\r
-#include <zlib/zlib.h>\r
-\r
-\r
-//#define BENCHMARK\r
-\r
-\r
-// scancodes we care about\r
-enum TUsedScanCodes {\r
-       EStdKeyM600JogUp   = EStdKeyDevice1,\r
-       EStdKeyM600JogDown = EStdKeyDevice2,\r
-};\r
-\r
-static unsigned char keyFlags[256];   // lsb->msb: key_down, pulse_only, ?, ?,  ?, ?, not_configurable, disabled\r
-static unsigned char pressedKeys[11]; // List of pressed key scancodes, up to 10\r
-\r
-// list of areas\r
-TPicoAreaConfigEntry areaConfig[] = {\r
-       { TRect(  0,   0,   0,   0) },\r
-       // small corner bottons\r
-       { TRect(  0,   0,  15,  15) },\r
-       { TRect(224,   0, 239,  15) },\r
-       { TRect(  0, 304,  15, 319) },\r
-       { TRect(224, 304, 239, 319) },\r
-       // normal buttons\r
-       { TRect(  0,   0,  79,  63) },\r
-       { TRect( 80,   0, 159,  63) },\r
-       { TRect(160,   0, 239,  63) },\r
-       { TRect(  0,  64,  79, 127) },\r
-       { TRect( 80,  64, 159, 127) },\r
-       { TRect(160,  64, 239, 127) },\r
-       { TRect(  0, 128,  79, 191) },\r
-       { TRect( 80, 128, 159, 191) },\r
-       { TRect(160, 128, 239, 191) },\r
-       { TRect(  0, 192,  79, 255) },\r
-       { TRect( 80, 192, 159, 255) },\r
-       { TRect(160, 192, 239, 255) },\r
-       { TRect(  0, 256,  79, 319) },\r
-       { TRect( 80, 256, 159, 319) },\r
-       { TRect(160, 256, 239, 319) },\r
-       { TRect(  0,   0,   0,   0) }\r
-};\r
-\r
-// PicoPad[] format: SACB RLDU\r
-const char *actionNames[] = {\r
-       "UP", "DOWN", "LEFT", "RIGHT", "B", "C", "A", "START",\r
-       0, 0, 0, 0, 0, 0, 0, 0, // Z, Y, X, MODE (enabled only when needed), ?, ?, ?, ?\r
-       0, 0, 0, 0, "VOLUME@UP", "VOLUME@DOWN", "NEXT@SAVE@SLOT", "PREV@SAVE@SLOT", // ?, ?, ?, ?, vol_up, vol_down, next_slot, prev_slot\r
-       0, 0, "PAUSE@EMU", "SAVE@STATE", "LOAD@STATE", 0, 0, "DONE" // ?, switch_renderer, [...], "FRAMESKIP@8", "AUTO@FRAMESKIP"\r
-};\r
-\r
-\r
-// globals are allowed, so why not to (ab)use them?\r
-//TInt machineUid = 0;\r
-int gamestate = PGS_Paused, gamestate_next = PGS_Paused;\r
-char *loadrom_fname = NULL;\r
-int   loadrom_result = 0;\r
-static timeval noticeMsgTime = { 0, 0 };       // when started showing\r
-static CGameAudioMS *gameAudio = 0;                    // the audio object itself\r
-static int reset_timing = 0;\r
-static int pico_was_reset = 0;\r
-extern RSemaphore initSemaphore;\r
-extern RSemaphore pauseSemaphore;\r
-extern RSemaphore loadWaitSemaphore;\r
-\r
-// some forward declarations\r
-static void MainInit();\r
-static void MainExit();\r
-static void DumpMemInfo();\r
-\r
-\r
-class TPicoDirectScreenAccess : public MDirectScreenAccess\r
-{\r
-public: // implements MDirectScreenAccess\r
-       void Restart(RDirectScreenAccess::TTerminationReasons aReason);\r
-public: // implements MAbortDirectScreenAccess\r
-       void AbortNow(RDirectScreenAccess::TTerminationReasons aReason);\r
-};\r
-\r
-\r
-// just for a nicer grouping of WS related stuff\r
-class CGameWindow\r
-{\r
-public:\r
-       static void ConstructResourcesL(void);\r
-       static void FreeResources(void);\r
-       static void DoKeys(void);\r
-       static void DoKeysConfig(TUint &which);\r
-       static void RunEvents(TUint32 which);\r
-\r
-       static RWsSession*                              iWsSession;\r
-       static RWindowGroup                             iWsWindowGroup;\r
-       static RWindow                                  iWsWindow;\r
-       static CWsScreenDevice*                 iWsScreen;\r
-       static CWindowGc*                               iWindowGc;\r
-       static TRequestStatus                   iWsEventStatus;\r
-//     static TThreadId                                iLauncherThreadId;\r
-//     static RDirectScreenAccess*             iDSA;\r
-//     static TRequestStatus                   iDSAstatus;\r
-       static TPicoDirectScreenAccess  iPDSA;\r
-       static CDirectScreenAccess*             iDSA;\r
-};\r
-\r
-\r
-static void updateSound(int len)\r
-{\r
-       PsndOut = gameAudio->NextFrameL(len);\r
-       if(!PsndOut) { // sound output problems?\r
-               strcpy(noticeMsg, "SOUND@OUTPUT@ERROR;@SOUND@DISABLED");\r
-               gettimeofday(&noticeMsgTime, 0);\r
-       }\r
-}\r
-\r
-\r
-static void SkipFrame(void)\r
-{\r
-       PicoSkipFrame=1;\r
-       PicoFrame();\r
-       PicoSkipFrame=0;\r
-}\r
-\r
-\r
-static void simpleWait(int thissec, int lim_time)\r
-{\r
-       struct timeval tval;\r
-       int sleep = 0;\r
-\r
-       gettimeofday(&tval, 0);\r
-       if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
-\r
-       sleep = lim_time - tval.tv_usec - 2000;\r
-       if (sleep > 0) {\r
-//             User::After((sleep = lim_time - tval.tv_usec));\r
-               User::AfterHighRes(sleep);\r
-       }\r
-}\r
-\r
-\r
-static void TargetEpocGameL()\r
-{\r
-       char buff[24]; // fps count c string\r
-       struct timeval tval; // timing\r
-       int thissec = 0, frames_done = 0, frames_shown = 0;\r
-       int target_fps, target_frametime;\r
-       int i, lim_time;\r
-\r
-       MainInit();\r
-       buff[0] = 0;\r
-\r
-       // try to start pico\r
-       DEBUGPRINT(_L("PicoInit()"));\r
-       PicoInit();\r
-       PicoDrawSetColorFormat(2);\r
-       PicoWriteSound = updateSound;\r
-\r
-       // loop?\r
-       for(;;)\r
-       {\r
-               if (gamestate == PGS_Running)\r
-               {\r
-                       #ifdef __DEBUG_PRINT\r
-                       TInt mem, cells = User::CountAllocCells();\r
-                       User::AllocSize(mem);\r
-                       DEBUGPRINT(_L("worker: cels=%d, size=%d KB"), cells, mem/1024);\r
-                       #endif\r
-\r
-                       // switch context to other thread\r
-                       User::After(50000);\r
-                       // prepare window and stuff\r
-                       CGameWindow::ConstructResourcesL();\r
-\r
-                       // if the system has something to do, it should better do it now\r
-                       User::After(50000);\r
-                       //CPolledActiveScheduler::Instance()->Schedule();\r
-\r
-                       // pal/ntsc might have changed, reset related stuff\r
-                       if(Pico.m.pal) {\r
-                               target_fps = 50;\r
-                               if(!noticeMsgTime.tv_sec) strcpy(noticeMsg, "PAL@SYSTEM@/@50@FPS");\r
-                       } else {\r
-                               target_fps = 60;\r
-                               if(!noticeMsgTime.tv_sec) strcpy(noticeMsg, "NTSC@SYSTEM@/@60@FPS");\r
-                       }\r
-                       target_frametime = 1000000/target_fps;\r
-                       if (!noticeMsgTime.tv_sec && pico_was_reset)\r
-                               gettimeofday(&noticeMsgTime, 0);\r
-\r
-                       // prepare CD buffer\r
-                       if (PicoAHW & PAHW_MCD) PicoCDBufferInit();\r
-\r
-                       pico_was_reset = 0;\r
-                       reset_timing = 1;\r
-\r
-                       while (gamestate == PGS_Running)\r
-                       {\r
-                               gettimeofday(&tval, 0);\r
-                               if(reset_timing) {\r
-                                       reset_timing = 0;\r
-                                       thissec = tval.tv_sec;\r
-                                       frames_done = tval.tv_usec/target_frametime;\r
-                               }\r
-\r
-                               // show notice message?\r
-                               char *notice = 0;\r
-                               if(noticeMsgTime.tv_sec) {\r
-                                       if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) // > 2.0 sec\r
-                                                noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;\r
-                                       else notice = noticeMsg;\r
-                               }\r
-\r
-                               // second changed?\r
-                               if (thissec != tval.tv_sec)\r
-                               {\r
-#ifdef BENCHMARK\r
-                                       static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r
-                                       if(++bench == 10) {\r
-                                               bench = 0;\r
-                                               bench_fps_s = bench_fps;\r
-                                               bf[bfp++ & 3] = bench_fps;\r
-                                               bench_fps = 0;\r
-                                       }\r
-                                       bench_fps += frames_shown;\r
-                                       sprintf(buff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r
-#else\r
-                                       if (currentConfig.EmuOpt & EOPT_SHOW_FPS) \r
-                                               sprintf(buff, "%02i/%02i", frames_shown, frames_done);\r
-#endif\r
-\r
-\r
-                                       thissec = tval.tv_sec;\r
-\r
-                                       if(PsndOut == 0 && currentConfig.Frameskip >= 0) {\r
-                                               frames_done = frames_shown = 0;\r
-                                       } else {\r
-                                               // it is quite common for this implementation to leave 1 fame unfinished\r
-                                               // when second changes, but we don't want buffer to starve.\r
-                                               if(PsndOut && frames_done < target_fps && frames_done > target_fps-5) {\r
-                                                       SkipFrame(); frames_done++;\r
-                                               }\r
-\r
-                                               frames_done  -= target_fps; if (frames_done  < 0) frames_done  = 0;\r
-                                               frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;\r
-                                               if (frames_shown > frames_done) frames_shown = frames_done;\r
-                                       }\r
-                                       User::ResetInactivityTime();\r
-                               }\r
-\r
-\r
-                               lim_time = (frames_done+1) * target_frametime;\r
-                               if (currentConfig.Frameskip >= 0) // frameskip enabled\r
-                               {\r
-                                       for (i = 0; i < currentConfig.Frameskip && gamestate == PGS_Running; i++)\r
-                                       {\r
-                                               CGameWindow::DoKeys();\r
-                                               SkipFrame(); frames_done++;\r
-                                               if (PsndOut) { // do framelimitting if sound is enabled\r
-                                                       gettimeofday(&tval, 0);\r
-                                                       if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
-                                                       if(tval.tv_usec < lim_time) { // we are too fast\r
-                                                               simpleWait(thissec, lim_time);\r
-                                                       }\r
-                                               }\r
-                                               lim_time += target_frametime;\r
-                                       }\r
-                               }\r
-                               else if(tval.tv_usec > lim_time) { // auto frameskip\r
-                                       // no time left for this frame - skip\r
-                                       CGameWindow::DoKeys();\r
-                                       SkipFrame(); frames_done++;\r
-                                       continue;\r
-                               }\r
-\r
-                               // we might have lost focus already\r
-                               if (gamestate != PGS_Running) break;\r
-\r
-                               CGameWindow::DoKeys();\r
-                               PicoFrame();\r
-\r
-                               // check time\r
-                               gettimeofday(&tval, 0);\r
-                               if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
-\r
-                               // sleep if we are still too fast\r
-                               if(PsndOut != 0 || currentConfig.Frameskip < 0)\r
-                               {\r
-                                       // TODO: check if User::After() is accurate\r
-                                       gettimeofday(&tval, 0);\r
-                                       if(thissec != tval.tv_sec) tval.tv_usec+=1000000;\r
-                                       if(tval.tv_usec < lim_time)\r
-                                       {\r
-                                               // we are too fast\r
-                                               simpleWait(thissec, lim_time);\r
-                                       }\r
-                               }\r
-\r
-                               CPolledActiveScheduler::Instance()->Schedule();\r
-\r
-                               if (gamestate != PGS_Paused)\r
-                                       vidDrawFrame(notice, buff, frames_shown);\r
-\r
-                               frames_done++; frames_shown++;\r
-                       } // while\r
-\r
-                       if (PicoAHW & PAHW_MCD) PicoCDBufferFree();\r
-\r
-                       // save SRAM\r
-                       if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {\r
-                               emu_save_load_game(0, 1);\r
-                               SRam.changed = 0;\r
-                       }\r
-                       CPolledActiveScheduler::Instance()->Schedule();\r
-                       CGameWindow::FreeResources();\r
-               }\r
-               else if(gamestate == PGS_Reset)\r
-               {\r
-                       PicoReset();\r
-                       pico_was_reset = 1;\r
-                       gamestate = PGS_Running;\r
-               }\r
-               else if(gamestate == PGS_ReloadRom)\r
-               {\r
-                       loadrom_result = emu_reload_rom(loadrom_fname);\r
-                       pico_was_reset = 1;\r
-                       if (loadrom_result)\r
-                               gamestate = PGS_Running;\r
-                       else {\r
-                               extern char menuErrorMsg[];\r
-                               gamestate = PGS_Paused;\r
-                               lprintf("%s\n", menuErrorMsg);\r
-                       }\r
-                       DEBUGPRINT(_L("done loading ROM, retval=%i"), loadrom_result);\r
-                       loadWaitSemaphore.Signal();\r
-                       User::After(50000);\r
-               }\r
-               else if(gamestate == PGS_Paused) {\r
-                       DEBUGPRINT(_L("pausing.."));\r
-                       pauseSemaphore.Wait();\r
-               }\r
-               else if(gamestate == PGS_KeyConfig)\r
-               {\r
-                       // switch context to other thread\r
-                       User::After(50000);\r
-                       // prepare window and stuff\r
-                       CGameWindow::ConstructResourcesL();\r
-\r
-                       TUint whichAction = 0;\r
-                       while(gamestate == PGS_KeyConfig) {\r
-                               CGameWindow::DoKeysConfig(whichAction);\r
-                               CPolledActiveScheduler::Instance()->Schedule();\r
-                               if (gamestate != PGS_Paused)\r
-                                       vidKeyConfigFrame(whichAction);\r
-                               User::After(150000);\r
-                       }\r
-\r
-                       emu_write_config(0);\r
-                       CGameWindow::FreeResources();\r
-               } else if(gamestate == PGS_Quit) {\r
-                       break;\r
-               }\r
-       }\r
-\r
-       // this thread has to close it's own handles,\r
-       // other one will crash trying to do that\r
-       PicoExit();\r
-\r
-       MainExit();\r
-}\r
-\r
-\r
-// main initialization\r
-static void MainInit()\r
-{\r
-       DEBUGPRINT(_L("\r\n\r\nstarting.."));\r
-\r
-       DEBUGPRINT(_L("CPolledActiveScheduler::NewL()"));\r
-       CPolledActiveScheduler::NewL(); // create Polled AS for the sound engine\r
-\r
-//     HAL::Get(HALData::EMachineUid, machineUid); // find out the machine UID\r
-\r
-       DumpMemInfo();\r
-\r
-//     if (pauseSemaphore.Handle() <= 0)\r
-//             pauseSemaphore.CreateLocal(0);\r
-       DEBUGPRINT(_L("initSemaphore.Signal()"));\r
-       initSemaphore.Signal();\r
-}\r
-\r
-\r
-// does not return\r
-static void MainExit()\r
-{\r
-       RThread thisThread;\r
-\r
-       DEBUGPRINT(_L("%i: cleaning up.."), (TInt32) thisThread.Id());\r
-\r
-//     pauseSemaphore.Close();\r
-\r
-       if(gameAudio) delete gameAudio;\r
-\r
-       // Polled AS\r
-       delete CPolledActiveScheduler::Instance();\r
-}\r
-\r
-static void DumpMemInfo()\r
-{\r
-       TInt    ramSize, ramSizeFree, romSize;\r
-       \r
-       HAL::Get(HALData::EMemoryRAM, ramSize);\r
-       HAL::Get(HALData::EMemoryRAMFree, ramSizeFree);\r
-       HAL::Get(HALData::EMemoryROM, romSize);\r
-\r
-       DEBUGPRINT(_L("ram=%dKB, ram_free=%dKB, rom=%dKB"), ramSize/1024, ramSizeFree/1024, romSize/1024);\r
-}\r
-\r
-\r
-extern "C" TInt my_SetExceptionHandler(TInt, TExceptionHandler, TUint32);\r
-\r
-TInt EmuThreadFunction(TAny*)\r
-{\r
-       TInt ret;\r
-       const TUint32 exs = KExceptionAbort|KExceptionKill|KExceptionUserInterrupt|KExceptionFpe|KExceptionFault|KExceptionInteger|KExceptionDebug;\r
-       \r
-       DEBUGPRINT(_L("EmuThreadFunction(), def ExceptionHandler %08x, my %08x"),\r
-               User::ExceptionHandler(), ExceptionHandler);\r
-       User::SetJustInTime(1);\r
-       ret = User::SetExceptionHandler(ExceptionHandler, exs/*(TUint32) -1*/); // does not work :(\r
-       // my_SetExceptionHandler(KCurrentThreadHandle, ExceptionHandler, 0xffffffff);\r
-       DEBUGPRINT(_L("SetExceptionHandler %i, %08x"), ret, User::ExceptionHandler());\r
-       User::ModifyExceptionMask(0, exs);\r
-\r
-       //TInt pc, sp;\r
-       //asm volatile ("str pc, %0" : "=m" (pc) );\r
-       //asm volatile ("str sp, %0" : "=m" (sp) );\r
-       //RDebug::Print(_L("executing @ 0x%08x, sp=0x%08x"), pc, sp);\r
-\r
-/*\r
-       RDebug::Print(_L("Base     Bottom   Top      Size     RW Name"));\r
-       TBuf<4> l_r(_L("R")), l_w(_L("W")), l_d(_L("-"));\r
-       RChunk chunk;\r
-       TFullName chunkname;\r
-       TFindChunk findChunk(_L("*"));\r
-       while( findChunk.Next(chunkname) != KErrNotFound ) {\r
-               chunk.Open(findChunk);\r
-               RDebug::Print(_L("%08x %08x %08x %08x %S%S %S"), chunk.Base(), chunk.Base()+chunk.Bottom(), chunk.Base()+chunk.Top(), chunk.Size(), chunk.IsReadable() ? &l_r : &l_d, chunk.IsWritable() ? &l_w : &l_d, &chunkname);\r
-               chunk.Close();\r
-       }\r
-*/\r
-\r
-       // can't do that, will crash here\r
-//     if(cleanup) {\r
-//             DEBUGPRINT(_L("found old CTrapCleanup, deleting.."));\r
-//             delete cleanup;\r
-//     }\r
-       \r
-       CTrapCleanup *cleanup = CTrapCleanup::New();\r
-\r
-       TRAPD(error, TargetEpocGameL());\r
-\r
-       __ASSERT_ALWAYS(!error, User::Panic(_L("PicoDrive"), error));\r
-       delete cleanup;\r
-\r
-       DEBUGPRINT(_L("exitting.."));   \r
-       return 1;\r
-}\r
-\r
-\r
-void TPicoDirectScreenAccess::Restart(RDirectScreenAccess::TTerminationReasons aReason)\r
-{\r
-       DEBUGPRINT(_L("TPicoDirectScreenAccess::Restart(%i)"), aReason);\r
-\r
-//     if (CGameWindow::iDSA) {\r
-//             TRAPD(error, CGameWindow::iDSA->StartL());\r
-//             if (error) DEBUGPRINT(_L("iDSA->StartL() error: %i"), error);\r
-//     }\r
-}\r
-\r
-\r
-void TPicoDirectScreenAccess::AbortNow(RDirectScreenAccess::TTerminationReasons aReason)\r
-{\r
-       DEBUGPRINT(_L("TPicoDirectScreenAccess::AbortNow(%i)"), aReason);\r
-\r
-       // the WS wants us to stop, so let's obey\r
-       gamestate = PGS_Paused;\r
-}\r
-\r
-\r
-void CGameWindow::ConstructResourcesL()\r
-{\r
-       DEBUGPRINT(_L("ConstructResourcesL()"));\r
-       // connect to window server\r
-       // tried to create it globally and not re-connect everytime,\r
-       // but my window started to lose focus strangely\r
-       iWsSession = new(ELeave) RWsSession();\r
-       User::LeaveIfError(iWsSession->Connect());\r
-\r
-       //       * Tell the Window Server not to mess about with our process priority\r
-       //       * Also, because of the way legacy games are written, they never sleep\r
-       //       * and thus never voluntarily yield the CPU. We set our process priority\r
-       //       * to EPriorityForeground and hope that a Telephony application on\r
-       //       * this device runs at EPriorityForeground as well. If not, tough! ;-)\r
-\r
-       iWsSession->ComputeMode(RWsSession::EPriorityControlDisabled);\r
-       RProcess me;\r
-       me.SetPriority(EPriorityForeground);\r
-\r
-       iWsScreen = new(ELeave) CWsScreenDevice(*iWsSession);\r
-       User::LeaveIfError(iWsScreen->Construct());\r
-//     User::LeaveIfError(iWsScreen->CreateContext(iWindowGc));\r
-\r
-       iWsWindowGroup = RWindowGroup(*iWsSession);\r
-       User::LeaveIfError(iWsWindowGroup.Construct((TUint32)&iWsWindowGroup));\r
-       //iWsWindowGroup.SetOrdinalPosition(0);\r
-       //iWsWindowGroup.SetName(KServerWGName);\r
-       iWsWindowGroup.EnableScreenChangeEvents(); // flip events (EEventScreenDeviceChanged)\r
-       iWsWindowGroup.EnableFocusChangeEvents(); // EEventFocusGroupChanged\r
-       iWsWindowGroup.SetOrdinalPosition(0, 1); // TInt aPos, TInt aOrdinalPriority\r
-\r
-       iWsWindow=RWindow(*iWsSession);\r
-       User::LeaveIfError(iWsWindow.Construct(iWsWindowGroup, (TUint32)&iWsWindow));\r
-       iWsWindow.SetSize(iWsScreen->SizeInPixels());\r
-       iWsWindow.PointerFilter(EPointerFilterDrag, 0);\r
-       iWsWindow.SetPointerGrab(ETrue);\r
-       iWsWindow.SetVisible(ETrue);\r
-       iWsWindow.Activate();\r
-\r
-#if 0\r
-       // request access through RDirectScreenAccess api, but don't care about the result\r
-       // hangs?\r
-       RRegion *dsa_region = 0;\r
-       iDSA = new(ELeave) RDirectScreenAccess(*iWsSession);\r
-       if(iDSA->Construct() == KErrNone)\r
-               iDSA->Request(dsa_region, iDSAstatus, iWsWindow);\r
-       DEBUGPRINT(_L("DSA: %i"), dsa_region ? dsa_region->Count() : -1);\r
-#endif\r
-\r
-       TInt ret;\r
-\r
-       // request access through CDirectScreenAccess\r
-       iDSA = CDirectScreenAccess::NewL(*iWsSession, *iWsScreen, iWsWindow, iPDSA);\r
-\r
-       // now get the screenbuffer\r
-       TScreenInfoV01                  screenInfo;\r
-       TPckg<TScreenInfoV01>   sI(screenInfo);\r
-       UserSvr::ScreenInfo(sI);\r
-\r
-       if(!screenInfo.iScreenAddressValid)\r
-               User::Leave(KErrNotSupported);\r
-\r
-       DEBUGPRINT(_L("framebuffer=0x%08x (%dx%d)"), screenInfo.iScreenAddress,\r
-                                       screenInfo.iScreenSize.iWidth, screenInfo.iScreenSize.iHeight);\r
-       \r
-       // vidInit\r
-       DEBUGPRINT(_L("vidInit()"));\r
-       ret = vidInit((void *)screenInfo.iScreenAddress, 0);\r
-       DEBUGPRINT(_L("vidInit() done (%i)"), ret);\r
-\r
-       User::LeaveIfError(ret);\r
-\r
-       memset(keyFlags, 0, 256);\r
-       keyFlags[EStdKeyM600JogUp] = keyFlags[EStdKeyM600JogDown] = 2; // add "pulse only" for jog up/down\r
-       keyFlags[EStdKeyOff] = 0x40; // not configurable\r
-\r
-       // try to start the audio engine\r
-       static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;\r
-\r
-       if (gamestate == PGS_Running && (currentConfig.EmuOpt & EOPT_EN_SOUND))\r
-       {\r
-               TInt err = 0;\r
-               if(PsndRate != PsndRate_old || (PicoOpt&11) != (PicoOpt_old&11) || Pico.m.pal != pal_old) {\r
-                       // if rate changed, reset all enabled chips, else reset only those chips, which were recently enabled\r
-                       //sound_reset(PsndRate != PsndRate_old ? PicoOpt : (PicoOpt&(PicoOpt^PicoOpt_old)));\r
-                       PsndRerate(1);\r
-               }\r
-               if(!gameAudio || PsndRate != PsndRate_old || ((PicoOpt&8) ^ (PicoOpt_old&8)) || Pico.m.pal != pal_old) { // rate or stereo or pal/ntsc changed\r
-                       if(gameAudio) delete gameAudio; gameAudio = 0;\r
-                       DEBUGPRINT(_L("starting audio: %i len: %i stereo: %i, pal: %i"), PsndRate, PsndLen, PicoOpt&8, Pico.m.pal);\r
-                       TRAP(err, gameAudio = CGameAudioMS::NewL(PsndRate, (PicoOpt&8) ? 1 : 0,\r
-                                               Pico.m.pal ? 50 : 60, currentConfig.volume));\r
-               }\r
-               if( gameAudio) {\r
-                       TRAP(err, PsndOut = gameAudio->ResumeL());\r
-               }\r
-               if(err) {\r
-                       if(gameAudio) delete gameAudio;\r
-                       gameAudio = 0;\r
-                       PsndOut = 0;\r
-                       strcpy(noticeMsg, "SOUND@STARTUP@FAILED");\r
-                       gettimeofday(&noticeMsgTime, 0);\r
-               }\r
-               PsndRate_old = PsndRate;\r
-               PicoOpt_old  = PicoOpt;\r
-               pal_old = Pico.m.pal;\r
-       } else {\r
-               if(gameAudio) delete gameAudio;\r
-               gameAudio = 0;\r
-               PsndOut = 0;\r
-       }\r
-\r
-       CPolledActiveScheduler::Instance()->Schedule();\r
-\r
-       // start key WS event polling\r
-       iWsSession->EventReady(&iWsEventStatus);\r
-\r
-       iWsSession->Flush(); // check: short hang in UIQ2\r
-       User::After(1);\r
-\r
-       // I don't know why but the Window server sometimes hangs completely (hanging the phone too) after calling StartL()\r
-       // Is this a sync broblem? weird bug?\r
-       TRAP(ret, iDSA->StartL());\r
-       if (ret) DEBUGPRINT(_L("iDSA->StartL() error: %i"), ret);\r
-\r
-//     User::After(1);\r
-//     CPolledActiveScheduler::Instance()->Schedule();\r
-\r
-       DEBUGPRINT(_L("CGameWindow::ConstructResourcesL() finished."));\r
-}\r
-\r
-// this may be run even if there is nothing to free\r
-void CGameWindow::FreeResources()\r
-{\r
-       if(gameAudio) gameAudio->Pause();\r
-\r
-       //DEBUGPRINT(_L("CPolledActiveScheduler::Instance(): %08x"), CPolledActiveScheduler::Instance());\r
-       if(CPolledActiveScheduler::Instance())\r
-               CPolledActiveScheduler::Instance()->Schedule();\r
-\r
-#if 0\r
-       // free RDirectScreenAccess stuff (seems to be deleted automatically after crash?)\r
-       if(iDSA) {\r
-               iDSA->Cancel();\r
-               iDSA->Close();\r
-               delete iDSA;\r
-       }\r
-       iDSA = NULL;\r
-#endif\r
-       if(iDSA) delete iDSA;\r
-       iDSA = 0;\r
-\r
-       if(iWsSession->WsHandle() > 0 && iWsEventStatus != KRequestPending) // TODO: 2 UIQ2 (?)\r
-               iWsSession->EventReadyCancel();\r
-\r
-       if(iWsWindow.WsHandle() > 0)\r
-               iWsWindow.Close();\r
-\r
-       if(iWsWindowGroup.WsHandle() > 0)\r
-               iWsWindowGroup.Close();\r
-\r
-       // these must be deleted before calling iWsSession->Close()\r
-       if(iWsScreen) {\r
-               delete iWsScreen;\r
-               iWsScreen = NULL;\r
-       }\r
-\r
-       if(iWsSession->WsHandle() > 0) {\r
-               iWsSession->Close();\r
-               delete iWsSession;\r
-       }\r
-       \r
-       vidFree();\r
-}\r
-\r
-\r
-void CGameWindow::DoKeys(void)\r
-{\r
-       TWsEvent iWsEvent;\r
-       TInt iWsEventType;\r
-       unsigned long allActions = 0;\r
-       static unsigned long areaActions = 0, forceUpdate = 0;\r
-       int i, nEvents;\r
-\r
-       for(nEvents = 0; iWsEventStatus != KRequestPending; nEvents++)\r
-       {\r
-               iWsSession->GetEvent(iWsEvent);\r
-               iWsEventType = iWsEvent.Type();\r
-\r
-               // pointer events?\r
-               if(iWsEventType == EEventPointer) {\r
-                       if(iWsEvent.Pointer()->iType == TPointerEvent::EButton1Up) {\r
-                               areaActions = 0; // remove all directionals\r
-                       } else { // if(iWsEvent.Pointer()->iType == TPointerEvent::EButton1Down) {\r
-                               TPoint p = iWsEvent.Pointer()->iPosition;\r
-                               const TPicoAreaConfigEntry *e = areaConfig + 1;\r
-                               for(i = 0; !e->rect.IsEmpty(); e++, i++)\r
-                                       if(e->rect.Contains(p)) {\r
-                                               areaActions = currentConfig.KeyBinds[i+256];\r
-                                               break;\r
-                                       }\r
-                               //DEBUGPRINT(_L("pointer event: %i %i"), p.iX, p.iY);\r
-                       }\r
-               }\r
-               else if(iWsEventType == EEventKeyDown || iWsEventType == EEventKeyUp) {\r
-                       TInt iScanCode = iWsEvent.Key()->iScanCode;\r
-                       //DEBUGPRINT(_L("key event: 0x%02x"), iScanCode);\r
-\r
-                       if(iScanCode < 256)\r
-                       {\r
-                               if(iWsEventType == EEventKeyDown) {\r
-                                       keyFlags[iScanCode] |=  1;\r
-                                       for(i=0; i < 10; i++) {\r
-                                               if( pressedKeys[i] == (TUint8) iScanCode) break;\r
-                                               if(!pressedKeys[i]) { pressedKeys[i] = (TUint8) iScanCode; break; }\r
-                                       }\r
-                               } else if(!(keyFlags[iScanCode]&2)) {\r
-                                       keyFlags[iScanCode] &= ~1;\r
-                                       for(i=0; i < 10; i++) {\r
-                                               if(pressedKeys[i] == (TUint8) iScanCode) { pressedKeys[i] = 0; break; }\r
-                                       }\r
-                               }\r
-\r
-                               // power?\r
-                               if(iScanCode == EStdKeyOff) gamestate = PGS_Paused;\r
-                       } else {\r
-                               DEBUGPRINT(_L("weird scancode: 0x%02x"), iScanCode);\r
-                       }\r
-               }\r
-               else if(iWsEventType == EEventScreenDeviceChanged) {\r
-                       // ???\r
-                       //User::After(500000);\r
-                       //reset_timing = 1;\r
-                       DEBUGPRINT(_L("EEventScreenDeviceChanged, focus: %i, our: %i"),\r
-                                               iWsSession->GetFocusWindowGroup(), iWsWindowGroup.Identifier());\r
-               }\r
-               else if(iWsEventType == EEventFocusGroupChanged) {\r
-                       TInt focusGrpId = iWsSession->GetFocusWindowGroup();\r
-                       DEBUGPRINT(_L("EEventFocusGroupChanged: %i, our: %i"),\r
-                                               focusGrpId, iWsWindowGroup.Identifier());\r
-                       // if it is not us and not launcher that got focus, pause emu\r
-                       if(focusGrpId != iWsWindowGroup.Identifier())\r
-                               gamestate = PGS_Paused;\r
-               }\r
-\r
-               iWsEventStatus = KRequestPending;\r
-               iWsSession->EventReady(&iWsEventStatus);\r
-       }\r
-\r
-       if(nEvents || forceUpdate) {\r
-               allActions = areaActions;\r
-               forceUpdate = 0;\r
-\r
-               // add all pushed button actions\r
-               for(i = 9; i >= 0; i--) {\r
-                       int scan = pressedKeys[i];\r
-                       if(scan) {\r
-                               if(keyFlags[scan] & 1) allActions |= currentConfig.KeyBinds[scan];\r
-                               if((keyFlags[scan]& 3)==3) forceUpdate = 1;\r
-                               if(keyFlags[scan] & 2) keyFlags[scan] &= ~1;\r
-                       }\r
-               }\r
-\r
-               PicoPad[0] = (unsigned short) allActions;\r
-               if(allActions & 0xFFFF0000) {\r
-                       RunEvents(allActions >> 16);\r
-                       areaActions = 0;\r
-               }\r
-       }\r
-\r
-       if (movie_data) emu_updateMovie();\r
-}\r
-\r
-\r
-void CGameWindow::DoKeysConfig(TUint &which)\r
-{\r
-       TWsEvent iWsEvent;\r
-       int i;\r
-\r
-       while(iWsEventStatus != KRequestPending)\r
-       {\r
-               TUint currentActCode = 1 << which;\r
-\r
-               iWsSession->GetEvent(iWsEvent);\r
-\r
-               // pointer events?\r
-               if(iWsEvent.Type() == EEventPointer) {\r
-                       TPoint p = iWsEvent.Pointer()->iPosition;\r
-                       TRect prev(56,  0, 120, 26);\r
-                       TRect next(120, 0, 180, 26);\r
-\r
-                       if(iWsEvent.Pointer()->iType == TPointerEvent::EButton1Down) {\r
-                                    if(prev.Contains(p)) do { which = (which-1) & 0x1F; } while(!actionNames[which]);\r
-                               else if(next.Contains(p)) do { which = (which+1) & 0x1F; } while(!actionNames[which]);\r
-                               else if(which == 31) gamestate = PGS_Paused; // done\r
-                               else {\r
-                                       const TPicoAreaConfigEntry *e = areaConfig + 1;\r
-                                       for(i = 0; e->rect != TRect(0,0,0,0); e++, i++)\r
-                                               if(e->rect.Contains(p)) {\r
-                                                       currentConfig.KeyBinds[i+256] ^= currentActCode;\r
-                                                       break;\r
-                                               }\r
-                               }\r
-                       }\r
-               }\r
-               else if(iWsEvent.Type() == EEventKeyDown || iWsEvent.Type() == EEventKeyUp)\r
-               {\r
-                       TUint scan = (TUint) iWsEvent.Key()->iScanCode;\r
-\r
-                       // key events?\r
-                       if(iWsEvent.Type() == EEventKeyDown) {\r
-                               if(which == 31) {\r
-                                       gamestate = PGS_Paused;\r
-                               } else if (scan < 256) {\r
-                                       if(!(keyFlags[scan]&0x40)) currentConfig.KeyBinds[scan] ^= currentActCode;\r
-                               }\r
-                       }\r
-\r
-                       // power?\r
-                       if(iWsEvent.Key()->iScanCode == EStdKeyOff) gamestate = PGS_Paused;\r
-               }\r
-               else if(iWsEvent.Type() == EEventFocusGroupChanged) {\r
-                       TInt focusGrpId = iWsSession->GetFocusWindowGroup();\r
-                       // if we lost focus, exit config mode\r
-                       if(focusGrpId != iWsWindowGroup.Identifier())\r
-                               gamestate = PGS_Paused;\r
-               }\r
-\r
-//             iWsEventStatus = KRequestPending;\r
-               iWsSession->EventReady(&iWsEventStatus);\r
-       }\r
-}\r
-\r
-\r
-void CGameWindow::RunEvents(TUint32 which)\r
-{\r
-       if (which & 0x4000) currentConfig.Frameskip = -1;\r
-       if (which & 0x2000) currentConfig.Frameskip =  8;\r
-       if (which & 0x1800) { // save or load (but not both)\r
-               if(PsndOut) gameAudio->Pause(); // this may take a while, so we pause sound output\r
-\r
-               vidDrawNotice((which & 0x1000) ? "LOADING@GAME" : "SAVING@GAME");\r
-               emu_save_load_game(which & 0x1000, 0);\r
-\r
-               if(PsndOut) PsndOut = gameAudio->ResumeL();\r
-               reset_timing = 1;\r
-       }\r
-       if (which & 0x0400) gamestate = PGS_Paused;\r
-       if (which & 0x0200) { // switch renderer\r
-               if (!(currentConfig.scaling == TPicoConfig::PMFit &&\r
-                       (currentConfig.rotation == TPicoConfig::PRot0 || currentConfig.rotation == TPicoConfig::PRot180)))\r
-               {\r
-                       PicoOpt^=0x10;\r
-                       vidInit(0, 1);\r
-\r
-                       strcpy(noticeMsg, (PicoOpt&0x10) ? "ALT@RENDERER" : "DEFAULT@RENDERER");\r
-                       gettimeofday(&noticeMsgTime, 0);\r
-               }\r
-       }\r
-       if(which & 0x00c0) {\r
-               if(which&0x0080) {\r
-                       state_slot -= 1;\r
-                       if(state_slot < 0) state_slot = 9;\r
-               } else {\r
-                       state_slot += 1;\r
-                       if(state_slot > 9) state_slot = 0;\r
-               }\r
-               sprintf(noticeMsg, "SAVE@SLOT@%i@SELECTED", state_slot);\r
-               gettimeofday(&noticeMsgTime, 0);\r
-       }\r
-       if ((which & 0x0030) && gameAudio != NULL) {\r
-               currentConfig.volume = gameAudio->ChangeVolume((which & 0x0010) ? 1 : 0);\r
-               sprintf(noticeMsg, "VOL@%02i@", currentConfig.volume);\r
-               gettimeofday(&noticeMsgTime, 0);\r
-       }\r
-}\r
-\r
-\r
-extern "C" void plat_status_msg(const char *format, ...)\r
-{\r
-       va_list vl;\r
-       char *p;\r
-\r
-       va_start(vl, format);\r
-       vsnprintf(noticeMsg, sizeof(noticeMsg), fmt, vl);\r
-       va_end(vl);\r
-\r
-       p = noticeMsg;\r
-       while (*p) {\r
-               if (*p == ' ') *p = '@';\r
-               if (*p < '0' || *p > 'Z') { *p = 0; break; }\r
-               p++;\r
-       }\r
-       gettimeofday(&noticeMsgTime, 0);\r
-}\r
-\r
-// static class members\r
-RWsSession*                            CGameWindow::iWsSession;\r
-RWindowGroup                   CGameWindow::iWsWindowGroup;\r
-RWindow                                        CGameWindow::iWsWindow;\r
-CWsScreenDevice*               CGameWindow::iWsScreen = NULL;\r
-CWindowGc*                             CGameWindow::iWindowGc = NULL;\r
-TRequestStatus                 CGameWindow::iWsEventStatus = KRequestPending;\r
-//RDirectScreenAccess* CGameWindow::iDSA;\r
-//TRequestStatus                       CGameWindow::iDSAstatus = KRequestPending;\r
-TPicoDirectScreenAccess        CGameWindow::iPDSA;\r
-CDirectScreenAccess*   CGameWindow::iDSA = NULL;\r
-\r