1 // mainloop with window server event handling
\r
2 // event polling mechnism was taken from
\r
3 // Peter van Sebille's projects
\r
5 // (c) Copyright 2006, notaz
\r
6 // All Rights Reserved
\r
10 #include <e32keys.h>
\r
15 #include <sys/time.h>
\r
18 #include "../Engine.h"
\r
20 #include <Pico/PicoInt.h>
\r
21 #include "../../common/emu.h"
\r
24 #include "PolledAS.h"
\r
25 //#include "audio.h"
\r
26 #include "audio_mediaserver.h"
\r
28 //#include <ezlib.h>
\r
29 #include <zlib/zlib.h>
\r
35 // scancodes we care about
\r
36 enum TUsedScanCodes {
\r
37 EStdKeyM600JogUp = EStdKeyDevice1,
\r
38 EStdKeyM600JogDown = EStdKeyDevice2,
\r
41 static unsigned char keyFlags[256]; // lsb->msb: key_down, pulse_only, ?, ?, ?, ?, not_configurable, disabled
\r
42 static unsigned char pressedKeys[11]; // List of pressed key scancodes, up to 10
\r
45 TPicoAreaConfigEntry areaConfig[] = {
\r
46 { TRect( 0, 0, 0, 0) },
\r
47 // small corner bottons
\r
48 { TRect( 0, 0, 15, 15) },
\r
49 { TRect(224, 0, 239, 15) },
\r
50 { TRect( 0, 304, 15, 319) },
\r
51 { TRect(224, 304, 239, 319) },
\r
53 { TRect( 0, 0, 79, 63) },
\r
54 { TRect( 80, 0, 159, 63) },
\r
55 { TRect(160, 0, 239, 63) },
\r
56 { TRect( 0, 64, 79, 127) },
\r
57 { TRect( 80, 64, 159, 127) },
\r
58 { TRect(160, 64, 239, 127) },
\r
59 { TRect( 0, 128, 79, 191) },
\r
60 { TRect( 80, 128, 159, 191) },
\r
61 { TRect(160, 128, 239, 191) },
\r
62 { TRect( 0, 192, 79, 255) },
\r
63 { TRect( 80, 192, 159, 255) },
\r
64 { TRect(160, 192, 239, 255) },
\r
65 { TRect( 0, 256, 79, 319) },
\r
66 { TRect( 80, 256, 159, 319) },
\r
67 { TRect(160, 256, 239, 319) },
\r
68 { TRect( 0, 0, 0, 0) }
\r
71 // PicoPad[] format: SACB RLDU
\r
72 const char *actionNames[] = {
\r
73 "UP", "DOWN", "LEFT", "RIGHT", "B", "C", "A", "START",
\r
74 0, 0, 0, 0, 0, 0, 0, 0, // Z, Y, X, MODE (enabled only when needed), ?, ?, ?, ?
\r
75 0, 0, 0, 0, "VOLUME@UP", "VOLUME@DOWN", "NEXT@SAVE@SLOT", "PREV@SAVE@SLOT", // ?, ?, ?, ?, vol_up, vol_down, next_slot, prev_slot
\r
76 0, 0, "PAUSE@EMU", "SAVE@STATE", "LOAD@STATE", 0, 0, "DONE" // ?, switch_renderer, [...], "FRAMESKIP@8", "AUTO@FRAMESKIP"
\r
80 // globals are allowed, so why not to (ab)use them?
\r
81 //TInt machineUid = 0;
\r
82 int gamestate = PGS_Paused, gamestate_next = PGS_Paused;
\r
83 char *loadrom_fname = NULL;
\r
84 int loadrom_result = 0;
\r
85 static timeval noticeMsgTime = { 0, 0 }; // when started showing
\r
86 static CGameAudioMS *gameAudio = 0; // the audio object itself
\r
87 static int reset_timing;
\r
88 extern int pico_was_reset;
\r
89 extern RSemaphore initSemaphore;
\r
90 extern RSemaphore pauseSemaphore;
\r
91 extern RSemaphore loadWaitSemaphore;
\r
93 // some forward declarations
\r
94 static void MainInit();
\r
95 static void MainExit();
\r
96 static void DumpMemInfo();
\r
99 class TPicoDirectScreenAccess : public MDirectScreenAccess
\r
101 public: // implements MDirectScreenAccess
\r
102 void Restart(RDirectScreenAccess::TTerminationReasons aReason);
\r
103 public: // implements MAbortDirectScreenAccess
\r
104 void AbortNow(RDirectScreenAccess::TTerminationReasons aReason);
\r
108 // just for a nicer grouping of WS related stuff
\r
112 static void ConstructResourcesL(void);
\r
113 static void FreeResources(void);
\r
114 static void DoKeys(void);
\r
115 static void DoKeysConfig(TUint &which);
\r
116 static void RunEvents(TUint32 which);
\r
118 static RWsSession* iWsSession;
\r
119 static RWindowGroup iWsWindowGroup;
\r
120 static RWindow iWsWindow;
\r
121 static CWsScreenDevice* iWsScreen;
\r
122 static CWindowGc* iWindowGc;
\r
123 static TRequestStatus iWsEventStatus;
\r
124 // static TThreadId iLauncherThreadId;
\r
125 // static RDirectScreenAccess* iDSA;
\r
126 // static TRequestStatus iDSAstatus;
\r
127 static TPicoDirectScreenAccess iPDSA;
\r
128 static CDirectScreenAccess* iDSA;
\r
132 static void updateSound(int len)
\r
134 PsndOut = gameAudio->NextFrameL(len);
\r
135 if(!PsndOut) { // sound output problems?
\r
136 strcpy(noticeMsg, "SOUND@OUTPUT@ERROR;@SOUND@DISABLED");
\r
137 gettimeofday(¬iceMsgTime, 0);
\r
142 static void SkipFrame(void)
\r
150 static void simpleWait(int thissec, int lim_time)
\r
152 struct timeval tval;
\r
155 gettimeofday(&tval, 0);
\r
156 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
158 sleep = lim_time - tval.tv_usec - 2000;
\r
160 // User::After((sleep = lim_time - tval.tv_usec));
\r
161 User::AfterHighRes(sleep);
\r
166 static void TargetEpocGameL()
\r
168 char buff[24]; // fps count c string
\r
169 struct timeval tval; // timing
\r
170 int thissec = 0, frames_done = 0, frames_shown = 0;
\r
171 int target_fps, target_frametime;
\r
173 //TRawEvent blevent;
\r
180 // just to keep the backlight on (works only on UIQ2)
\r
181 //blevent.Set(TRawEvent::EActive);
\r
186 if (gamestate == PGS_Running)
\r
188 // switch context to other thread
\r
189 User::After(50000);
\r
190 // prepare window and stuff
\r
191 CGameWindow::ConstructResourcesL();
\r
193 // if the system has something to do, it should better do it now
\r
194 User::After(50000);
\r
195 //CPolledActiveScheduler::Instance()->Schedule();
\r
197 // pal/ntsc might have changed, reset related stuff
\r
200 if(!noticeMsgTime.tv_sec) strcpy(noticeMsg, "PAL@SYSTEM@/@50@FPS");
\r
203 if(!noticeMsgTime.tv_sec) strcpy(noticeMsg, "NTSC@SYSTEM@/@60@FPS");
\r
205 target_frametime = 1000000/target_fps;
\r
206 if(!noticeMsgTime.tv_sec && pico_was_reset)
\r
207 gettimeofday(¬iceMsgTime, 0);
\r
209 // prepare CD buffer
\r
210 if (PicoAHW & PAHW_MCD) PicoCDBufferInit();
\r
212 pico_was_reset = 0;
\r
215 while (gamestate == PGS_Running)
\r
217 gettimeofday(&tval, 0);
\r
220 thissec = tval.tv_sec;
\r
221 frames_done = tval.tv_usec/target_frametime;
\r
224 // show notice message?
\r
226 if(noticeMsgTime.tv_sec) {
\r
227 if((tval.tv_sec*1000000+tval.tv_usec) - (noticeMsgTime.tv_sec*1000000+noticeMsgTime.tv_usec) > 2000000) // > 2.0 sec
\r
228 noticeMsgTime.tv_sec = noticeMsgTime.tv_usec = 0;
\r
229 else notice = noticeMsg;
\r
233 if (thissec != tval.tv_sec)
\r
236 static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];
\r
237 if(++bench == 10) {
\r
239 bench_fps_s = bench_fps;
\r
240 bf[bfp++ & 3] = bench_fps;
\r
243 bench_fps += frames_shown;
\r
244 sprintf(buff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);
\r
246 if (currentConfig.EmuOpt & EOPT_SHOW_FPS)
\r
247 sprintf(buff, "%02i/%02i", frames_shown, frames_done);
\r
251 thissec = tval.tv_sec;
\r
253 if(PsndOut == 0 && currentConfig.Frameskip >= 0) {
\r
254 frames_done = frames_shown = 0;
\r
256 // it is quite common for this implementation to leave 1 fame unfinished
\r
257 // when second changes, but we don't want buffer to starve.
\r
258 if(PsndOut && frames_done < target_fps && frames_done > target_fps-5) {
\r
259 SkipFrame(); frames_done++;
\r
262 frames_done -= target_fps; if (frames_done < 0) frames_done = 0;
\r
263 frames_shown -= target_fps; if (frames_shown < 0) frames_shown = 0;
\r
264 if (frames_shown > frames_done) frames_shown = frames_done;
\r
269 lim_time = (frames_done+1) * target_frametime;
\r
270 if (currentConfig.Frameskip >= 0) // frameskip enabled
\r
272 for (i = 0; i < currentConfig.Frameskip && gamestate == PGS_Running; i++)
\r
274 CGameWindow::DoKeys();
\r
275 SkipFrame(); frames_done++;
\r
276 if (PsndOut) { // do framelimitting if sound is enabled
\r
277 gettimeofday(&tval, 0);
\r
278 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
279 if(tval.tv_usec < lim_time) { // we are too fast
\r
280 simpleWait(thissec, lim_time);
\r
283 lim_time += target_frametime;
\r
286 else if(tval.tv_usec > lim_time) { // auto frameskip
\r
287 // no time left for this frame - skip
\r
288 CGameWindow::DoKeys();
\r
289 SkipFrame(); frames_done++;
\r
293 // we might have lost focus already
\r
294 if (gamestate != PGS_Running) break;
\r
296 CGameWindow::DoKeys();
\r
300 gettimeofday(&tval, 0);
\r
301 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
303 // sleep if we are still too fast
\r
304 if(PsndOut != 0 || currentConfig.Frameskip < 0)
\r
306 // TODO: check if User::After() is accurate
\r
307 gettimeofday(&tval, 0);
\r
308 if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
\r
309 if(tval.tv_usec < lim_time)
\r
312 simpleWait(thissec, lim_time);
\r
316 CPolledActiveScheduler::Instance()->Schedule();
\r
318 if (gamestate != PGS_Paused)
\r
319 vidDrawFrame(notice, buff, frames_shown);
\r
321 frames_done++; frames_shown++;
\r
324 if (PicoAHW & PAHW_MCD) PicoCDBufferFree();
\r
327 if ((currentConfig.EmuOpt & EOPT_USE_SRAM) && SRam.changed) {
\r
328 emu_SaveLoadGame(0, 1);
\r
331 CPolledActiveScheduler::Instance()->Schedule();
\r
332 CGameWindow::FreeResources();
\r
334 else if(gamestate == PGS_ReloadRom)
\r
336 loadrom_result = emu_ReloadRom(loadrom_fname);
\r
337 pico_was_reset = 1;
\r
338 if (loadrom_result)
\r
339 gamestate = PGS_Running;
\r
341 gamestate = PGS_Paused;
\r
342 DEBUGPRINT(_L("done loading ROM, retval=%i"), loadrom_result);
\r
343 loadWaitSemaphore.Signal();
\r
344 User::After(50000);
\r
346 else if(gamestate == PGS_Paused) {
\r
347 DEBUGPRINT(_L("pausing.."));
\r
348 pauseSemaphore.Wait();
\r
350 else if(gamestate == PGS_KeyConfig)
\r
352 // switch context to other thread
\r
353 User::After(50000);
\r
354 // prepare window and stuff
\r
355 CGameWindow::ConstructResourcesL();
\r
357 TUint whichAction = 0;
\r
358 while(gamestate == PGS_KeyConfig) {
\r
359 CGameWindow::DoKeysConfig(whichAction);
\r
360 CPolledActiveScheduler::Instance()->Schedule();
\r
361 if (gamestate != PGS_Paused)
\r
362 vidKeyConfigFrame(whichAction);
\r
363 User::After(150000);
\r
366 CGameWindow::FreeResources();
\r
367 } else if(gamestate == PGS_DebugHeap) {
\r
368 #ifdef __DEBUG_PRINT
\r
369 TInt cells = User::CountAllocCells();
\r
371 User::AllocSize(mem);
\r
372 DEBUGPRINT(_L("worker: cels=%d, size=%d KB"), cells, mem/1024);
\r
373 gamestate = gamestate_next;
\r
375 } else if(gamestate == PGS_Quit) {
\r
380 // this thread has to close it's own handles,
\r
381 // other one will crash trying to do that
\r
388 // main initialization
\r
389 static void MainInit()
\r
391 DEBUGPRINT(_L("\r\n\r\nstarting.."));
\r
393 DEBUGPRINT(_L("CPolledActiveScheduler::NewL()"));
\r
394 CPolledActiveScheduler::NewL(); // create Polled AS for the sound engine
\r
396 // HAL::Get(HALData::EMachineUid, machineUid); // find out the machine UID
\r
400 // try to start pico
\r
401 DEBUGPRINT(_L("PicoInit()"));
\r
402 PicoDrawSetColorFormat(2);
\r
403 PicoWriteSound = updateSound;
\r
405 // if (pauseSemaphore.Handle() <= 0)
\r
406 // pauseSemaphore.CreateLocal(0);
\r
407 DEBUGPRINT(_L("initSemaphore.Signal()"));
\r
408 initSemaphore.Signal();
\r
413 static void MainExit()
\r
415 RThread thisThread;
\r
417 DEBUGPRINT(_L("%i: cleaning up.."), (TInt32) thisThread.Id());
\r
419 // pauseSemaphore.Close();
\r
421 if(gameAudio) delete gameAudio;
\r
424 delete CPolledActiveScheduler::Instance();
\r
427 static void DumpMemInfo()
\r
429 TInt ramSize, ramSizeFree, romSize;
\r
431 HAL::Get(HALData::EMemoryRAM, ramSize);
\r
432 HAL::Get(HALData::EMemoryRAMFree, ramSizeFree);
\r
433 HAL::Get(HALData::EMemoryROM, romSize);
\r
435 DEBUGPRINT(_L("ram=%dKB, ram_free=%dKB, rom=%dKB"), ramSize/1024, ramSizeFree/1024, romSize/1024);
\r
439 extern "C" TInt my_SetExceptionHandler(TInt, TExceptionHandler, TUint32);
\r
441 TInt EmuThreadFunction(TAny*)
\r
444 const TUint32 exs = KExceptionAbort|KExceptionKill|KExceptionUserInterrupt|KExceptionFpe|KExceptionFault|KExceptionInteger|KExceptionDebug;
\r
446 DEBUGPRINT(_L("EmuThreadFunction(), def ExceptionHandler %08x, my %08x"),
\r
447 User::ExceptionHandler(), ExceptionHandler);
\r
448 User::SetJustInTime(1);
\r
449 ret = User::SetExceptionHandler(ExceptionHandler, exs/*(TUint32) -1*/); // does not work :(
\r
450 // my_SetExceptionHandler(KCurrentThreadHandle, ExceptionHandler, 0xffffffff);
\r
451 DEBUGPRINT(_L("SetExceptionHandler %i, %08x"), ret, User::ExceptionHandler());
\r
452 User::ModifyExceptionMask(0, exs);
\r
455 //asm volatile ("str pc, %0" : "=m" (pc) );
\r
456 //asm volatile ("str sp, %0" : "=m" (sp) );
\r
457 //RDebug::Print(_L("executing @ 0x%08x, sp=0x%08x"), pc, sp);
\r
460 RDebug::Print(_L("Base Bottom Top Size RW Name"));
\r
461 TBuf<4> l_r(_L("R")), l_w(_L("W")), l_d(_L("-"));
\r
463 TFullName chunkname;
\r
464 TFindChunk findChunk(_L("*"));
\r
465 while( findChunk.Next(chunkname) != KErrNotFound ) {
\r
466 chunk.Open(findChunk);
\r
467 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
472 // can't do that, will crash here
\r
474 // DEBUGPRINT(_L("found old CTrapCleanup, deleting.."));
\r
478 CTrapCleanup *cleanup = CTrapCleanup::New();
\r
480 TRAPD(error, TargetEpocGameL());
\r
482 __ASSERT_ALWAYS(!error, User::Panic(_L("PicoDrive"), error));
\r
485 DEBUGPRINT(_L("exitting.."));
\r
490 void TPicoDirectScreenAccess::Restart(RDirectScreenAccess::TTerminationReasons aReason)
\r
492 DEBUGPRINT(_L("TPicoDirectScreenAccess::Restart(%i)"), aReason);
\r
494 // if (CGameWindow::iDSA) {
\r
495 // TRAPD(error, CGameWindow::iDSA->StartL());
\r
496 // if (error) DEBUGPRINT(_L("iDSA->StartL() error: %i"), error);
\r
501 void TPicoDirectScreenAccess::AbortNow(RDirectScreenAccess::TTerminationReasons aReason)
\r
503 DEBUGPRINT(_L("TPicoDirectScreenAccess::AbortNow(%i)"), aReason);
\r
505 // the WS wants us to stop, so let's obey
\r
506 gamestate = PGS_Paused;
\r
510 void CGameWindow::ConstructResourcesL()
\r
512 DEBUGPRINT(_L("ConstructResourcesL()"));
\r
513 // connect to window server
\r
514 // tried to create it globally and not re-connect everytime,
\r
515 // but my window started to lose focus strangely
\r
516 iWsSession = new(ELeave) RWsSession();
\r
517 User::LeaveIfError(iWsSession->Connect());
\r
519 // * Tell the Window Server not to mess about with our process priority
\r
520 // * Also, because of the way legacy games are written, they never sleep
\r
521 // * and thus never voluntarily yield the CPU. We set our process priority
\r
522 // * to EPriorityForeground and hope that a Telephony application on
\r
523 // * this device runs at EPriorityForeground as well. If not, tough! ;-)
\r
525 iWsSession->ComputeMode(RWsSession::EPriorityControlDisabled);
\r
527 me.SetPriority(EPriorityForeground);
\r
529 iWsScreen = new(ELeave) CWsScreenDevice(*iWsSession);
\r
530 User::LeaveIfError(iWsScreen->Construct());
\r
531 // User::LeaveIfError(iWsScreen->CreateContext(iWindowGc));
\r
533 iWsWindowGroup = RWindowGroup(*iWsSession);
\r
534 User::LeaveIfError(iWsWindowGroup.Construct((TUint32)&iWsWindowGroup));
\r
535 //iWsWindowGroup.SetOrdinalPosition(0);
\r
536 //iWsWindowGroup.SetName(KServerWGName);
\r
537 iWsWindowGroup.EnableScreenChangeEvents(); // flip events (EEventScreenDeviceChanged)
\r
538 iWsWindowGroup.EnableFocusChangeEvents(); // EEventFocusGroupChanged
\r
539 iWsWindowGroup.SetOrdinalPosition(0, 1); // TInt aPos, TInt aOrdinalPriority
\r
541 iWsWindow=RWindow(*iWsSession);
\r
542 User::LeaveIfError(iWsWindow.Construct(iWsWindowGroup, (TUint32)&iWsWindow));
\r
543 iWsWindow.SetSize(iWsScreen->SizeInPixels());
\r
544 iWsWindow.PointerFilter(EPointerFilterDrag, 0);
\r
545 iWsWindow.SetPointerGrab(ETrue);
\r
546 iWsWindow.SetVisible(ETrue);
\r
547 iWsWindow.Activate();
\r
550 // request access through RDirectScreenAccess api, but don't care about the result
\r
552 RRegion *dsa_region = 0;
\r
553 iDSA = new(ELeave) RDirectScreenAccess(*iWsSession);
\r
554 if(iDSA->Construct() == KErrNone)
\r
555 iDSA->Request(dsa_region, iDSAstatus, iWsWindow);
\r
556 DEBUGPRINT(_L("DSA: %i"), dsa_region ? dsa_region->Count() : -1);
\r
561 // request access through CDirectScreenAccess
\r
562 iDSA = CDirectScreenAccess::NewL(*iWsSession, *iWsScreen, iWsWindow, iPDSA);
\r
564 // now get the screenbuffer
\r
565 TScreenInfoV01 screenInfo;
\r
566 TPckg<TScreenInfoV01> sI(screenInfo);
\r
567 UserSvr::ScreenInfo(sI);
\r
569 if(!screenInfo.iScreenAddressValid)
\r
570 User::Leave(KErrNotSupported);
\r
572 DEBUGPRINT(_L("framebuffer=0x%08x (%dx%d)"), screenInfo.iScreenAddress,
\r
573 screenInfo.iScreenSize.iWidth, screenInfo.iScreenSize.iHeight);
\r
576 DEBUGPRINT(_L("vidInit()"));
\r
577 ret = vidInit((void *)screenInfo.iScreenAddress, 0);
\r
578 DEBUGPRINT(_L("vidInit() done (%i)"), ret);
\r
580 User::LeaveIfError(ret);
\r
582 memset(keyFlags, 0, 256);
\r
583 keyFlags[EStdKeyM600JogUp] = keyFlags[EStdKeyM600JogDown] = 2; // add "pulse only" for jog up/down
\r
584 keyFlags[EStdKeyOff] = 0x40; // not configurable
\r
586 // try to start the audio engine
\r
587 static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;
\r
589 if (gamestate == PGS_Running && (currentConfig.EmuOpt & EOPT_EN_SOUND))
\r
592 if(PsndRate != PsndRate_old || (PicoOpt&11) != (PicoOpt_old&11) || Pico.m.pal != pal_old) {
\r
593 // if rate changed, reset all enabled chips, else reset only those chips, which were recently enabled
\r
594 //sound_reset(PsndRate != PsndRate_old ? PicoOpt : (PicoOpt&(PicoOpt^PicoOpt_old)));
\r
597 if(!gameAudio || PsndRate != PsndRate_old || ((PicoOpt&8) ^ (PicoOpt_old&8)) || Pico.m.pal != pal_old) { // rate or stereo or pal/ntsc changed
\r
598 if(gameAudio) delete gameAudio; gameAudio = 0;
\r
599 DEBUGPRINT(_L("starting audio: %i len: %i stereo: %i, pal: %i"), PsndRate, PsndLen, PicoOpt&8, Pico.m.pal);
\r
600 TRAP(err, gameAudio = CGameAudioMS::NewL(PsndRate, (PicoOpt&8) ? 1 : 0,
\r
601 Pico.m.pal ? 50 : 60, currentConfig.volume));
\r
604 TRAP(err, PsndOut = gameAudio->ResumeL());
\r
607 if(gameAudio) delete gameAudio;
\r
610 strcpy(noticeMsg, "SOUND@STARTUP@FAILED");
\r
611 gettimeofday(¬iceMsgTime, 0);
\r
613 PsndRate_old = PsndRate;
\r
614 PicoOpt_old = PicoOpt;
\r
615 pal_old = Pico.m.pal;
\r
617 if(gameAudio) delete gameAudio;
\r
622 CPolledActiveScheduler::Instance()->Schedule();
\r
624 // start key WS event polling
\r
625 iWsSession->EventReady(&iWsEventStatus);
\r
627 iWsSession->Flush(); // check: short hang in UIQ2
\r
630 // I don't know why but the Window server sometimes hangs completely (hanging the phone too) after calling StartL()
\r
631 // Is this a sync broblem? weird bug?
\r
632 TRAP(ret, iDSA->StartL());
\r
633 if (ret) DEBUGPRINT(_L("iDSA->StartL() error: %i"), ret);
\r
636 // CPolledActiveScheduler::Instance()->Schedule();
\r
638 DEBUGPRINT(_L("CGameWindow::ConstructResourcesL() finished."));
\r
641 // this may be run even if there is nothing to free
\r
642 void CGameWindow::FreeResources()
\r
644 if(gameAudio) gameAudio->Pause();
\r
646 //DEBUGPRINT(_L("CPolledActiveScheduler::Instance(): %08x"), CPolledActiveScheduler::Instance());
\r
647 if(CPolledActiveScheduler::Instance())
\r
648 CPolledActiveScheduler::Instance()->Schedule();
\r
651 // free RDirectScreenAccess stuff (seems to be deleted automatically after crash?)
\r
659 if(iDSA) delete iDSA;
\r
662 if(iWsSession->WsHandle() > 0 && iWsEventStatus != KRequestPending) // TODO: 2 UIQ2 (?)
\r
663 iWsSession->EventReadyCancel();
\r
665 if(iWsWindow.WsHandle() > 0)
\r
668 if(iWsWindowGroup.WsHandle() > 0)
\r
669 iWsWindowGroup.Close();
\r
671 // these must be deleted before calling iWsSession->Close()
\r
677 if(iWsSession->WsHandle() > 0) {
\r
678 iWsSession->Close();
\r
686 void CGameWindow::DoKeys(void)
\r
690 unsigned long allActions = 0;
\r
691 static unsigned long areaActions = 0, forceUpdate = 0;
\r
694 for(nEvents = 0; iWsEventStatus != KRequestPending; nEvents++)
\r
696 iWsSession->GetEvent(iWsEvent);
\r
697 iWsEventType = iWsEvent.Type();
\r
700 if(iWsEventType == EEventPointer) {
\r
701 if(iWsEvent.Pointer()->iType == TPointerEvent::EButton1Up) {
\r
702 areaActions = 0; // remove all directionals
\r
703 } else { // if(iWsEvent.Pointer()->iType == TPointerEvent::EButton1Down) {
\r
704 TPoint p = iWsEvent.Pointer()->iPosition;
\r
705 const TPicoAreaConfigEntry *e = areaConfig + 1;
\r
706 for(i = 0; !e->rect.IsEmpty(); e++, i++)
\r
707 if(e->rect.Contains(p)) {
\r
708 areaActions = currentConfig.KeyBinds[i+256];
\r
711 //DEBUGPRINT(_L("pointer event: %i %i"), p.iX, p.iY);
\r
714 else if(iWsEventType == EEventKeyDown || iWsEventType == EEventKeyUp) {
\r
715 TInt iScanCode = iWsEvent.Key()->iScanCode;
\r
716 //DEBUGPRINT(_L("key event: 0x%02x"), iScanCode);
\r
718 if(iScanCode < 256)
\r
720 if(iWsEventType == EEventKeyDown) {
\r
721 keyFlags[iScanCode] |= 1;
\r
722 for(i=0; i < 10; i++) {
\r
723 if( pressedKeys[i] == (TUint8) iScanCode) break;
\r
724 if(!pressedKeys[i]) { pressedKeys[i] = (TUint8) iScanCode; break; }
\r
726 } else if(!(keyFlags[iScanCode]&2)) {
\r
727 keyFlags[iScanCode] &= ~1;
\r
728 for(i=0; i < 10; i++) {
\r
729 if(pressedKeys[i] == (TUint8) iScanCode) { pressedKeys[i] = 0; break; }
\r
734 if(iScanCode == EStdKeyOff) gamestate = PGS_Paused;
\r
736 DEBUGPRINT(_L("weird scancode: 0x%02x"), iScanCode);
\r
739 else if(iWsEventType == EEventScreenDeviceChanged) {
\r
741 //User::After(500000);
\r
742 //reset_timing = 1;
\r
743 DEBUGPRINT(_L("EEventScreenDeviceChanged, focus: %i, our: %i"),
\r
744 iWsSession->GetFocusWindowGroup(), iWsWindowGroup.Identifier());
\r
746 else if(iWsEventType == EEventFocusGroupChanged) {
\r
747 TInt focusGrpId = iWsSession->GetFocusWindowGroup();
\r
748 DEBUGPRINT(_L("EEventFocusGroupChanged: %i, our: %i"),
\r
749 focusGrpId, iWsWindowGroup.Identifier());
\r
750 // if it is not us and not launcher that got focus, pause emu
\r
751 if(focusGrpId != iWsWindowGroup.Identifier())
\r
752 gamestate = PGS_Paused;
\r
755 iWsEventStatus = KRequestPending;
\r
756 iWsSession->EventReady(&iWsEventStatus);
\r
759 if(nEvents || forceUpdate) {
\r
760 allActions = areaActions;
\r
763 // add all pushed button actions
\r
764 for(i = 9; i >= 0; i--) {
\r
765 int scan = pressedKeys[i];
\r
767 if(keyFlags[scan] & 1) allActions |= currentConfig.KeyBinds[scan];
\r
768 if((keyFlags[scan]& 3)==3) forceUpdate = 1;
\r
769 if(keyFlags[scan] & 2) keyFlags[scan] &= ~1;
\r
773 PicoPad[0] = (unsigned short) allActions;
\r
774 if(allActions & 0xFFFF0000) {
\r
775 RunEvents(allActions >> 16);
\r
782 void CGameWindow::DoKeysConfig(TUint &which)
\r
787 while(iWsEventStatus != KRequestPending)
\r
789 TUint currentActCode = 1 << which;
\r
791 iWsSession->GetEvent(iWsEvent);
\r
794 if(iWsEvent.Type() == EEventPointer) {
\r
795 TPoint p = iWsEvent.Pointer()->iPosition;
\r
796 TRect prev(56, 0, 120, 26);
\r
797 TRect next(120, 0, 180, 26);
\r
799 if(iWsEvent.Pointer()->iType == TPointerEvent::EButton1Down) {
\r
800 if(prev.Contains(p)) do { which = (which-1) & 0x1F; } while(!actionNames[which]);
\r
801 else if(next.Contains(p)) do { which = (which+1) & 0x1F; } while(!actionNames[which]);
\r
802 else if(which == 31) gamestate = PGS_Paused; // done
\r
804 const TPicoAreaConfigEntry *e = areaConfig + 1;
\r
805 for(i = 0; e->rect != TRect(0,0,0,0); e++, i++)
\r
806 if(e->rect.Contains(p)) {
\r
807 currentConfig.KeyBinds[i+256] ^= currentActCode;
\r
813 else if(iWsEvent.Type() == EEventKeyDown || iWsEvent.Type() == EEventKeyUp)
\r
815 TUint scan = (TUint) iWsEvent.Key()->iScanCode;
\r
818 if(iWsEvent.Type() == EEventKeyDown) {
\r
820 gamestate = PGS_Paused;
\r
821 } else if (scan < 256) {
\r
822 if(!(keyFlags[scan]&0x40)) currentConfig.KeyBinds[scan] ^= currentActCode;
\r
827 if(iWsEvent.Key()->iScanCode == EStdKeyOff) gamestate = PGS_Paused;
\r
829 else if(iWsEvent.Type() == EEventFocusGroupChanged) {
\r
830 TInt focusGrpId = iWsSession->GetFocusWindowGroup();
\r
831 // if we lost focus, exit config mode
\r
832 if(focusGrpId != iWsWindowGroup.Identifier())
\r
833 gamestate = PGS_Paused;
\r
836 // iWsEventStatus = KRequestPending;
\r
837 iWsSession->EventReady(&iWsEventStatus);
\r
842 void CGameWindow::RunEvents(TUint32 which)
\r
844 if (which & 0x4000) currentConfig.Frameskip = -1;
\r
845 if (which & 0x2000) currentConfig.Frameskip = 8;
\r
846 if (which & 0x1800) { // save or load (but not both)
\r
847 if(PsndOut) gameAudio->Pause(); // this may take a while, so we pause sound output
\r
849 vidDrawNotice((which & 0x1000) ? "LOADING@GAME" : "SAVING@GAME");
\r
850 emu_SaveLoadGame(which & 0x1000, 0);
\r
852 if(PsndOut) PsndOut = gameAudio->ResumeL();
\r
855 if (which & 0x0400) gamestate = PGS_Paused;
\r
856 if (which & 0x0200) { // switch renderer
\r
857 if (!(currentConfig.scaling == TPicoConfig::PMFit &&
\r
858 (currentConfig.rotation == TPicoConfig::PRot0 || currentConfig.rotation == TPicoConfig::PRot180)))
\r
863 strcpy(noticeMsg, (PicoOpt&0x10) ? "ALT@RENDERER" : "DEFAULT@RENDERER");
\r
864 gettimeofday(¬iceMsgTime, 0);
\r
867 if(which & 0x00c0) {
\r
870 if(state_slot < 0) state_slot = 9;
\r
873 if(state_slot > 9) state_slot = 0;
\r
875 sprintf(noticeMsg, "SAVE@SLOT@%i@SELECTED", state_slot);
\r
876 gettimeofday(¬iceMsgTime, 0);
\r
878 if(which & 0x0020) if(gameAudio) currentConfig.volume = gameAudio->ChangeVolume(0);
\r
879 if(which & 0x0010) if(gameAudio) currentConfig.volume = gameAudio->ChangeVolume(1);
\r
883 extern "C" void emu_noticeMsgUpdated(void)
\r
885 char *p = noticeMsg;
\r
887 if (*p == ' ') *p = '@';
\r
888 if (*p < '0' || *p > 'Z') { *p = 0; break; }
\r
891 gettimeofday(¬iceMsgTime, 0);
\r
894 // static class members
\r
895 RWsSession* CGameWindow::iWsSession;
\r
896 RWindowGroup CGameWindow::iWsWindowGroup;
\r
897 RWindow CGameWindow::iWsWindow;
\r
898 CWsScreenDevice* CGameWindow::iWsScreen = NULL;
\r
899 CWindowGc* CGameWindow::iWindowGc = NULL;
\r
900 TRequestStatus CGameWindow::iWsEventStatus = KRequestPending;
\r
901 //RDirectScreenAccess* CGameWindow::iDSA;
\r
902 //TRequestStatus CGameWindow::iDSAstatus = KRequestPending;
\r
903 TPicoDirectScreenAccess CGameWindow::iPDSA;
\r
904 CDirectScreenAccess* CGameWindow::iDSA = NULL;
\r