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