Remove everything orientation related.
[SDL.s60v3.git] / src / main / symbian / SDL_main.cpp
blob1df7b062e7296bd10f141b1067b2a69b4074c58f
1 #include <queue>
2 #include "epoc_sdl.h"
3 #include "sdlepocapi.h"
4 #include <e32base.h>
5 #include <estlib.h>
6 #include <stdio.h>
7 #include <badesca.h>
8 #include <w32std.h>
9 #include <aknappui.h>
10 #include <aknapp.h>
11 #include "SDL_epocevents_c.h"
12 #include "SDL_keysym.h"
13 #include "dsa.h"
14 #include "SDL_loadso.h"
16 extern SDLKey* KeyMap();
17 extern void ResetKeyMap();
19 class CCurrentAppUi;
20 class CEikonEnv;
21 class CSdlAppServ;
22 class CEventQueue;
24 NONSHARABLE_CLASS(EpocSdlEnvData)
26 public:
27 void Free();
28 void Delete();
29 CEventQueue* iEventQueue;
30 TMainFunc iMain;
31 int iEpocEnvFlags;
32 int iArgc;
33 char** iArgv;
34 CDsa* iDsa;
35 CSdlAppServ* iAppSrv;
36 TThreadId iId;
37 CArrayFix<TSdlCleanupItem>* iCleanupItems;
38 CSDL* iSdl;
39 TRequestStatus* iCallerStatus;
42 EpocSdlEnvData* gEpocEnv;
44 NONSHARABLE_CLASS(EnvUtils)
46 public:
47 static inline TBool IsOwnThreaded();
48 static TBool Rendezvous(RThread& aThread, TRequestStatus& aStatus);
49 static void RunSingleThread();
52 inline TBool EnvUtils::IsOwnThreaded()
54 return gEpocEnv->iEpocEnvFlags & CSDL::EOwnThread;
57 void EnvUtils::RunSingleThread()
59 if(!EnvUtils::IsOwnThreaded())
61 int count = RThread().RequestCount();
62 if(count > 0)
64 int err;
65 if(CActiveScheduler::RunIfReady(err, CActive::EPriorityIdle))
67 CActiveScheduler::Current()->WaitForAnyRequest();
73 int Panic(int aErr, int aLine)
75 TBuf<64> b;
76 b.Format(_L("Main at %d"), aLine);
77 User::Panic(b, aErr);
78 return 0;
82 class CEventQueue : public CBase, public MEventQueue
84 public:
85 void ConstructL();
86 ~CEventQueue();
87 int Append(const TWsEvent& aEvent);
88 const TWsEvent Shift();
89 void Lock();
90 void Unlock();
91 TBool HasData();
93 private:
94 std::queue<TWsEvent> m_queue;
95 RCriticalSection iCS;
98 void CEventQueue::ConstructL()
100 if(EnvUtils::IsOwnThreaded())
101 User::LeaveIfError(iCS.CreateLocal());
104 CEventQueue::~CEventQueue()
106 iCS.Close();
109 int CEventQueue::Append(const TWsEvent& aEvent)
111 Lock();
112 m_queue.push(aEvent);
113 Unlock();
114 return 0;
118 TBool CEventQueue::HasData()
120 EnvUtils::RunSingleThread();
121 return !m_queue.empty();
124 void CEventQueue::Lock()
126 if(EnvUtils::IsOwnThreaded())
127 iCS.Wait();
130 void CEventQueue::Unlock()
132 if(EnvUtils::IsOwnThreaded())
133 iCS.Signal();
136 const TWsEvent CEventQueue::Shift()
138 const TWsEvent event = m_queue.front();
139 m_queue.pop();
140 return event;
144 TSdlCleanupItem::TSdlCleanupItem(TSdlCleanupOperation aOperation, TAny* aItem) :
145 iOperation(aOperation), iItem(aItem), iThread(RThread().Id())
148 #define MAINFUNC(x) EXPORT_C TMainFunc::TMainFunc(mainfunc##x aFunc){Mem::FillZ(iMainFunc, sizeof(iMainFunc)); iMainFunc[x - 1] = (void*) aFunc;}
150 MAINFUNC(1)
151 MAINFUNC(2)
152 MAINFUNC(3)
153 MAINFUNC(4)
154 MAINFUNC(5)
155 MAINFUNC(6)
157 EXPORT_C TMainFunc::TMainFunc()
159 Mem::FillZ(iMainFunc, sizeof(iMainFunc));
163 const void* TMainFunc::operator[](int aIndex) const
165 return iMainFunc[aIndex];
169 NONSHARABLE_CLASS(CSdlAppServ) : public CActive
171 public:
172 enum
174 EAppSrvNoop = CDsa::ELastDsaRequest,
175 EAppSrvWindowWidth,
176 EAppSrvWindowHeight,
177 EAppSrvWindowDisplayMode,
178 EAppSrvWindowPointerCursorMode,
179 EAppSrvDsaStatus,
180 EAppSrvStopThread,
181 EAppSrvWaitDsa
183 CSdlAppServ();
184 void ConstructL();
185 ~CSdlAppServ();
186 int Request(int aService);
187 int RequestValue(int aService);
188 void Init();
189 void PanicMain(int aReason);
190 void PanicMain(const TDesC& aInfo, int aReason);
191 void SetObserver(MSDLObserver* aObserver);
192 int ObserverEvent(int aEvent, int aParam);
193 void SetParam(int aParam);
194 void HandleObserverValue(int aService, int aReturnValue, TBool aMainThread);
195 MSDLObserver* Observer();
197 private:
198 void RunL();
199 void DoCancel();
200 const TThreadId iMainId;
201 RThread iAppThread;
202 int iService;
203 int iReturnValue;
204 RSemaphore iSema;
205 MSDLObserver* iObserver;
206 TRequestStatus* iStatusPtr;
209 CSdlAppServ::CSdlAppServ() : CActive(CActive::EPriorityHigh), iMainId(RThread().Id()), iObserver( NULL )
213 MSDLObserver* CSdlAppServ::Observer()
215 return iObserver;
218 void CSdlAppServ::SetObserver(MSDLObserver* aObserver)
220 iObserver = aObserver;
223 int CSdlAppServ::ObserverEvent(int aEvent, int aParam)
225 if(iObserver != NULL)
227 if(RThread().Id() == gEpocEnv->iId && EnvUtils::IsOwnThreaded())
229 return iObserver->SdlThreadEvent(aEvent, aParam);
231 else if(RThread().Id() == iMainId)
233 return iObserver->SdlEvent(aEvent, aParam);
235 PANIC(KErrNotSupported);
237 return 0;
240 void CSdlAppServ::PanicMain(int aReason)
242 iAppThread.Panic(RThread().Name(), aReason);
245 void CSdlAppServ::PanicMain(const TDesC& aInfo, int aReason)
247 iAppThread.Panic(aInfo, aReason);
250 void CSdlAppServ::ConstructL()
252 CActiveScheduler::Add(this);
253 if(EnvUtils::IsOwnThreaded())
254 User::LeaveIfError(iSema.CreateLocal(1));
255 iStatus = KRequestPending;
256 iStatusPtr = &iStatus;
257 SetActive();
260 CSdlAppServ::~CSdlAppServ()
262 Cancel();
263 if(iSema.Handle() != NULL)
264 iSema.Signal();
265 iSema.Close();
266 iAppThread.Close();
269 int CSdlAppServ::Request(int aService)
271 if(EnvUtils::IsOwnThreaded())
273 if(RThread().Id() == iAppThread.Id())
274 return KErrBadHandle;
275 iSema.Wait();
277 EnvUtils::RunSingleThread();
278 iService = aService;
279 iAppThread.RequestComplete(iStatusPtr, KErrNone);
280 return KErrNone;
283 int CSdlAppServ::RequestValue(int aService)
285 Request(aService);
286 Request(EAppSrvNoop);
287 return iReturnValue;
290 void CSdlAppServ::Init()
292 PANIC_IF_ERROR(iAppThread.Open(iMainId));
295 void CSdlAppServ::SetParam(int aParam)
297 iReturnValue = aParam;
300 void CSdlAppServ::HandleObserverValue(int aService, int aReturnValue, TBool aMainThread)
302 if(iObserver != NULL && aMainThread)
304 switch(aService)
306 case MSDLObserver::EEventScreenSizeChanged:
307 if(aReturnValue == MSDLObserver::EScreenSizeChangedDefaultPalette)
308 EpocSdlEnv::LockPalette(EFalse);
309 break;
312 if(!aMainThread && aService == MSDLObserver::EEventSuspend)
314 if(iObserver == NULL || (gEpocEnv->iDsa != NULL && gEpocEnv->iDsa->Stopped() && aReturnValue != MSDLObserver::ESuspendNoSuspend))
316 EpocSdlEnv::Suspend();
321 void CSdlAppServ::RunL()
323 if(iStatus == KErrNone)
325 switch(iService)
327 case CSdlAppServ::EAppSrvWaitDsa:
328 EpocSdlEnv::SetWaitDsa();
329 iReturnValue = EpocSdlEnv::IsDsaAvailable();
330 break;
332 case CSdlAppServ::EAppSrvStopThread:
333 if(gEpocEnv->iDsa != NULL)
334 gEpocEnv->iDsa->SetSuspend();
335 break;
337 case EAppSrvWindowPointerCursorMode:
338 iReturnValue = gEpocEnv->iDsa != NULL ? gEpocEnv->iDsa->Session().PointerCursorMode() : KErrNotReady;
339 break;
341 case EAppSrvDsaStatus:
342 if(gEpocEnv->iDsa != NULL)
343 gEpocEnv->iDsa->Stop();
344 iReturnValue = KErrNone;
345 break;
347 case CDsa::ERequestUpdate:
348 gEpocEnv->iDsa->UnlockHWSurfaceRequestComplete();
349 break;
351 case EAppSrvNoop:
352 break;
354 case MSDLObserver::EEventResume:
355 case MSDLObserver::EEventSuspend:
356 case MSDLObserver::EEventScreenSizeChanged:
357 case MSDLObserver::EEventWindowReserved:
358 case MSDLObserver::EEventKeyMapInit:
359 case MSDLObserver::EEventWindowNotAvailable:
360 case MSDLObserver::EEventMainExit:
361 iReturnValue = ObserverEvent(iService, iReturnValue);
362 HandleObserverValue(iService, iReturnValue, ETrue);
363 break;
365 default:
366 PANIC(KErrNotSupported);
367 break;
370 iStatus = KRequestPending;
371 iStatusPtr = &iStatus;
372 SetActive();
375 if(EnvUtils::IsOwnThreaded())
376 iSema.Signal();
379 void CSdlAppServ::DoCancel()
381 if(EnvUtils::IsOwnThreaded())
382 iSema.Wait();
383 TRequestStatus* s = &iStatus;
384 iAppThread.RequestComplete(s, KErrCancel);
387 MEventQueue& EpocSdlEnv::EventQueue()
389 __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
390 return *gEpocEnv->iEventQueue;
394 TBool EpocSdlEnv::Flags(int aFlag)
396 const int flag = gEpocEnv->iEpocEnvFlags & aFlag;
397 return flag == aFlag;
400 int EpocSdlEnv::Argc()
402 __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
403 return gEpocEnv->iArgc;
406 char** EpocSdlEnv::Argv()
408 __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
409 return gEpocEnv->iArgv;
412 TBool EpocSdlEnv::IsDsaAvailable()
414 __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
415 return gEpocEnv->iDsa != NULL && gEpocEnv->iDsa->IsDsaAvailable();
418 void EpocSdlEnv::WaitDsaAvailable()
420 EpocSdlEnv::ObserverEvent(MSDLObserver::EEventWindowNotAvailable, 0);
421 gEpocEnv->iAppSrv->Request(CSdlAppServ::EAppSrvStopThread);
422 if(EpocSdlEnv::Flags(CSDL::EEnableFocusStop))
424 EpocSdlEnv::ObserverEvent(MSDLObserver::EEventSuspend, 0);
428 void EpocSdlEnv::Suspend()
430 if(gEpocEnv->iDsa != NULL && (gEpocEnv->iDsa->Stopped() || EpocSdlEnv::Flags(CSDL::EEnableFocusStop)))
432 gEpocEnv->iDsa->SetSuspend();
433 if(EnvUtils::IsOwnThreaded())
435 RThread().Suspend();
436 EpocSdlEnv::ObserverEvent(MSDLObserver::EEventResume, 0);
441 void EpocSdlEnv::SetWaitDsa()
443 if(!IsDsaAvailable())
445 if(EnvUtils::IsOwnThreaded())
447 RThread th;
448 th.Open(gEpocEnv->iId);
449 th.Suspend();
450 th.Close();
452 if(gEpocEnv->iDsa != NULL)
453 gEpocEnv->iDsa->SetSuspend();
457 void EpocSdlEnv::Resume()
459 if(gEpocEnv->iDsa != NULL)
461 gEpocEnv->iDsa->Resume();
463 if(EnvUtils::IsOwnThreaded())
465 RThread th;
466 th.Open(gEpocEnv->iId);
467 th.Resume();
468 th.Close();
470 const int value = gEpocEnv->iAppSrv->ObserverEvent(MSDLObserver::EEventResume, 0);
471 gEpocEnv->iAppSrv->HandleObserverValue(MSDLObserver::EEventResume, value, ETrue);
475 int EpocSdlEnv::AllocSwSurface(const TSize& aSize, TDisplayMode aMode)
477 return gEpocEnv->iDsa->AllocSurface(EFalse, aSize, aMode);
480 int EpocSdlEnv::AllocHwSurface(const TSize& aSize, TDisplayMode aMode)
482 return gEpocEnv->iDsa->AllocSurface(ETrue, aSize, aMode);
485 void EpocSdlEnv::UnlockHwSurface()
487 gEpocEnv->iDsa->UnlockHwSurface();
490 TUint8* EpocSdlEnv::LockHwSurface()
492 return gEpocEnv->iDsa->LockHwSurface();
495 void EpocSdlEnv::UpdateSwSurface()
497 gEpocEnv->iDsa->UpdateSwSurface();
500 TBool EpocSdlEnv::AddUpdateRect(TUint8* aAddress, const TRect& aUpdateRect, const TRect& aRect)
502 return gEpocEnv->iDsa->AddUpdateRect(aAddress, aUpdateRect, aRect);
505 void EpocSdlEnv::Request(int aService)
507 __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
508 gEpocEnv->iAppSrv->Request(aService);
511 TSize EpocSdlEnv::WindowSize(const TSize& aRequestedSize)
513 __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
514 return gEpocEnv->iDsa == NULL ? TSize(0, 0) : gEpocEnv->iDsa->WindowSize();
517 TSize EpocSdlEnv::WindowSize()
519 __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
520 return gEpocEnv->iDsa == NULL ? TSize(0, 0) : gEpocEnv->iDsa->WindowSize();
523 TDisplayMode EpocSdlEnv::DisplayMode()
525 return gEpocEnv->iDsa == NULL ? ENone : gEpocEnv->iDsa->DisplayMode();
528 TPointerCursorMode EpocSdlEnv::PointerMode()
530 return static_cast<TPointerCursorMode>(gEpocEnv->iAppSrv->RequestValue(CSdlAppServ::EAppSrvWindowPointerCursorMode));
533 int EpocSdlEnv::SetPalette(int aFirstcolor, int aColorCount, TUint32* aPalette)
535 return gEpocEnv->iDsa->SetPalette(aFirstcolor, aColorCount, aPalette);
538 void EpocSdlEnv::PanicMain(int aErr)
540 gEpocEnv->iAppSrv->PanicMain(aErr);
544 int EpocSdlEnv::AppendCleanupItem(const TSdlCleanupItem& aItem)
546 TRAPD(err, gEpocEnv->iCleanupItems->AppendL(aItem));
547 return err;
550 void EpocSdlEnv::RemoveCleanupItem(TAny* aItem)
552 for(int i = 0; i < gEpocEnv->iCleanupItems->Count(); i++)
554 if(gEpocEnv->iCleanupItems->At(i).iItem == aItem)
555 gEpocEnv->iCleanupItems->Delete(i);
559 void EpocSdlEnv::CleanupItems()
561 const TThreadId id = RThread().Id();
562 int last = gEpocEnv->iCleanupItems->Count() - 1;
563 int i;
565 for(i = last; i >= 0 ; i--)
567 TSdlCleanupItem& item = gEpocEnv->iCleanupItems->At(i);
568 if(item.iThread == id)
570 item.iThread = TThreadId(0);
571 item.iOperation(item.iItem);
575 last = gEpocEnv->iCleanupItems->Count() - 1;
577 for(i = last; i >= 0 ; i--)
579 TSdlCleanupItem& item = gEpocEnv->iCleanupItems->At(i);
580 if(item.iThread == TThreadId(0))
582 gEpocEnv->iCleanupItems->Delete(i);
587 void EpocSdlEnv::FreeSurface()
589 Request(CSdlAppServ::EAppSrvDsaStatus);
590 if(gEpocEnv->iDsa != NULL)
591 gEpocEnv->iDsa->Free();
594 void EpocSdlEnv::LockPalette(TBool aLock)
596 gEpocEnv->iDsa->LockPalette(aLock);
599 void EpocSdlEnv::ObserverEvent(int aService, int aParam)
601 const TBool sdlThread = RThread().Id() == gEpocEnv->iId;
602 const int valuea = gEpocEnv->iAppSrv->ObserverEvent(aService, aParam);
603 gEpocEnv->iAppSrv->HandleObserverValue(aService, valuea, !sdlThread);
604 if(sdlThread)
606 gEpocEnv->iAppSrv->SetParam(aParam);
607 const int valuet = gEpocEnv->iAppSrv->RequestValue(aService);
608 gEpocEnv->iAppSrv->HandleObserverValue(aService, valuet, EFalse);
612 TPoint EpocSdlEnv::WindowCoordinates(const TPoint& aPoint)
614 return gEpocEnv->iDsa == NULL ? aPoint : gEpocEnv->iDsa->WindowCoordinates(aPoint);
617 void EpocSdlEnv::PanicMain(const TDesC& aInfo, int aErr)
619 gEpocEnv->iAppSrv->PanicMain(aInfo, aErr);
622 //Dsa is a low priority ao, it has to wait if its pending event, but ws
623 //event has been prioritized before it
624 //this is not called from app thread!
625 void EpocSdlEnv::WaitDeviceChange()
627 LockPalette(ETrue);
628 gEpocEnv->iAppSrv->RequestValue(CSdlAppServ::EAppSrvWaitDsa);
629 const TSize sz = WindowSize();
630 const int param = reinterpret_cast<int>(&sz);
631 ObserverEvent(MSDLObserver::EEventScreenSizeChanged, param);
634 LOCAL_C TBool CheckSdl()
636 int isExit = ETrue;
637 RThread sdl;
638 if(sdl.Open(gEpocEnv->iId) == KErrNone)
640 if(sdl.ExitType() == EExitPending)
642 isExit = EFalse;
644 sdl.Close();
646 return isExit;
649 void EpocSdlEnvData::Free()
651 if(RThread().Id() == gEpocEnv->iId)
653 if(iDsa != NULL)
654 iDsa->Free();
655 return;
658 __ASSERT_ALWAYS(iArgv == NULL || CheckSdl(), PANIC(KErrNotReady));
661 void EpocSdlEnvData::Delete()
663 for(int i = 0; i <= iArgc; i++)
665 if(iArgv != NULL)
666 User::Free( iArgv[i] );
669 User::Free(iArgv);
671 iArgv = NULL;
672 iArgc = 0;
674 delete iEventQueue;
676 if(iDsa != NULL)
677 iDsa->Free();
679 delete iDsa;
680 delete iAppSrv;
683 _LIT(KSDLMain, "SDLMain");
685 LOCAL_C int MainL()
687 gEpocEnv->iCleanupItems = new (ELeave) CArrayFixFlat<TSdlCleanupItem>(8);
689 char** envp=0;
690 /* !! process exits here if there is "exit()" in main! */
691 int ret = 0;
692 for(int i = 0; i < 6; i++)
694 void* f = (void*) gEpocEnv->iMain[i];
695 if(f != NULL)
697 switch(i)
699 case 0:
700 ret = ((mainfunc1)f)();
701 return ret;
703 case 3:
704 ((mainfunc1)f)();
705 return ret;
707 case 1:
708 ret = ((mainfunc2)f)(EpocSdlEnv::Argc(), EpocSdlEnv::Argv());
709 return ret;
711 case 4:
712 ((mainfunc2)f)(EpocSdlEnv::Argc(), EpocSdlEnv::Argv());
713 return ret;
715 case 2:
716 ret = ((mainfunc3)f)(EpocSdlEnv::Argc(), EpocSdlEnv::Argv(), envp);
717 return ret;
719 case 5:
720 ((mainfunc3)f)(EpocSdlEnv::Argc(), EpocSdlEnv::Argv(), envp);
721 return ret;
725 PANIC(KErrNotFound);
726 return 0;
729 LOCAL_C int DoMain(TAny* /*aParam*/)
731 TBool fbsconnected = EFalse;
732 CTrapCleanup* cleanup = NULL;
734 if(EnvUtils::IsOwnThreaded())
736 cleanup = CTrapCleanup::New();
738 if(RFbsSession::GetSession() == NULL)
740 PANIC_IF_ERROR(RFbsSession::Connect());
741 fbsconnected = ETrue;
744 gEpocEnv->iAppSrv->Init();
746 // Call stdlib main
747 int ret = 0;
748 if(EnvUtils::IsOwnThreaded())
750 //completes waiting rendesvous
751 RThread::Rendezvous(KErrNone);
754 TRAPD(err, err = MainL());
756 EpocSdlEnv::ObserverEvent(MSDLObserver::EEventMainExit, err);
758 // Free resources and return
759 EpocSdlEnv::CleanupItems();
761 gEpocEnv->iCleanupItems->Reset();
762 delete gEpocEnv->iCleanupItems;
763 gEpocEnv->iCleanupItems = NULL;
765 gEpocEnv->Free(); //free up in thread resources
767 if(fbsconnected)
768 RFbsSession::Disconnect();
770 delete cleanup;
772 if(gEpocEnv->iCallerStatus != NULL)
774 User::RequestComplete(gEpocEnv->iCallerStatus, err);
775 return 0;
777 else
779 return err == KErrNone ? ret : err;
783 EXPORT_C CSDL::~CSDL()
785 gEpocEnv->Free();
786 gEpocEnv->Delete();
788 User::Free(gEpocEnv);
789 gEpocEnv = NULL;
792 EXPORT_C CSDL* CSDL::NewL(int aFlags)
794 __ASSERT_ALWAYS(gEpocEnv == NULL, PANIC(KErrAlreadyExists));
795 gEpocEnv = (EpocSdlEnvData*) User::AllocL(sizeof(EpocSdlEnvData));
796 Mem::FillZ(gEpocEnv, sizeof(EpocSdlEnvData));
798 gEpocEnv->iEpocEnvFlags = aFlags;
799 gEpocEnv->iEventQueue = new (ELeave) CEventQueue();
800 gEpocEnv->iAppSrv = new (ELeave) CSdlAppServ();
802 CSDL* sdl = new (ELeave) CSDL();
804 gEpocEnv->iSdl = sdl;
806 return sdl;
809 EXPORT_C void CSDL::SetContainerWindowL(RWindow& aWindow, RWsSession& aSession, CWsScreenDevice& aDevice)
811 if(gEpocEnv->iDsa == NULL)
812 gEpocEnv->iDsa = CDsa::CreateL(aSession);
813 gEpocEnv->iDsa->ConstructL(aWindow, aDevice);
816 int EpocSdlEnv::ApplyGlesDsa()
818 CDsa* dsa = NULL;
819 TRAPD(err, dsa = gEpocEnv->iDsa->CreateGlesDsaL());
820 gEpocEnv->iDsa = dsa;
821 return err;
824 RWindow* EpocSdlEnv::Window()
826 return gEpocEnv->iDsa->Window();
829 EXPORT_C TThreadId CSDL::CallMainL(const TMainFunc& aFunc, TRequestStatus* const aStatus, const CDesC8Array* const aArg, int aStackSize)
831 ASSERT(gEpocEnv != NULL);
832 gEpocEnv->iMain = aFunc;
833 const TBool args = aArg != NULL;
835 if(gEpocEnv->iArgv != NULL)
836 User::Leave(KErrAlreadyExists);
838 gEpocEnv->iArgc = args ? aArg->Count() + 1 : 0;
839 gEpocEnv->iArgv = (char**) User::AllocL(sizeof(char*) * (gEpocEnv->iArgc + 2));
841 int k = 0;
842 const TFileName processName = RProcess().FileName();
843 const int len = processName.Length();
844 gEpocEnv->iArgv[k] = (char*) User::AllocL(len + 1);
845 Mem::Copy(gEpocEnv->iArgv[k], processName.Ptr(), len);
846 gEpocEnv->iArgv[k][len] = 0;
848 for(int i = 0; args && (i < aArg->Count()); i++)
850 k++;
851 const int len = aArg->MdcaPoint(i).Length();
852 gEpocEnv->iArgv[k] = (char*) User::AllocL(len + 1);
853 Mem::Copy(gEpocEnv->iArgv[k], aArg->MdcaPoint(i).Ptr(), len);
854 gEpocEnv->iArgv[k][len] = 0;
857 gEpocEnv->iArgv[k + 1] = NULL;
859 gEpocEnv->iEventQueue->ConstructL();
860 gEpocEnv->iAppSrv->ConstructL();
862 if(EnvUtils::IsOwnThreaded())
864 RThread thread;
865 User::LeaveIfError(thread.Create(KSDLMain, DoMain, aStackSize, NULL, NULL));
867 if(aStatus != NULL)
869 thread.Logon(*aStatus);
872 gEpocEnv->iId = thread.Id();
873 thread.SetPriority(EPriorityLess);
874 thread.Resume();
875 thread.Close();
877 else
879 gEpocEnv->iCallerStatus = aStatus;
880 if(aStatus != NULL)
881 *aStatus = KRequestPending;
882 gEpocEnv->iId = RThread().Id();
883 // when priority is not lowered screen updates much more frequently, which
884 // may be undesired, for example in case of openttd's generating world dialog
885 RThread().SetPriority(EPriorityLess);
886 DoMain( NULL );
889 return gEpocEnv->iId;
892 EXPORT_C int CSDL::AppendWsEvent(const TWsEvent& aEvent)
894 return EpocSdlEnv::EventQueue().Append(aEvent);
897 EXPORT_C void CSDL::SDLPanic(const TDesC& aInfo, int aErr)
899 EpocSdlEnv::PanicMain(aInfo, aErr);
902 EXPORT_C int CSDL::GetSDLCode(int aScanCode)
904 if(aScanCode < 0)
905 return MAX_SCANCODE;
906 if(aScanCode >= MAX_SCANCODE)
907 return -1;
908 return KeyMap()[aScanCode];
911 EXPORT_C int CSDL::SDLCodesCount() const
913 return MAX_SCANCODE;
916 EXPORT_C void CSDL::ResetSDLCodes()
918 ResetKeyMap();
921 EXPORT_C int CSDL::SetSDLCode(int aScanCode, int aSDLCode)
923 const int current = GetSDLCode(aScanCode);
924 if(aScanCode >= 0 && aScanCode < MAX_SCANCODE)
925 KeyMap()[aScanCode] = static_cast<SDLKey>(aSDLCode);
926 return current;
930 EXPORT_C MSDLObserver* CSDL::Observer()
932 return gEpocEnv->iAppSrv->Observer();
935 EXPORT_C void CSDL::SetObserver(MSDLObserver* aObserver)
937 gEpocEnv->iAppSrv->SetObserver(aObserver);
940 EXPORT_C void CSDL::Resume()
942 EpocSdlEnv::Resume();
945 EXPORT_C void CSDL::Suspend()
947 if(gEpocEnv->iDsa != NULL)
948 gEpocEnv->iDsa->DoStop();
951 EXPORT_C CSDL::CSDL()
954 EXPORT_C int CSDL::SetBlitter(MBlitter* aBlitter)
956 if(gEpocEnv && gEpocEnv->iDsa)
958 gEpocEnv->iDsa->SetBlitter(aBlitter);
959 return KErrNone;
961 return KErrNotReady;
964 EXPORT_C int CSDL::RedrawRequest()
966 if(gEpocEnv && gEpocEnv->iDsa)
968 const int err = gEpocEnv->iDsa->RedrawRequest();
969 EnvUtils::RunSingleThread();
970 return err;
972 return KErrNotReady;
975 TBool EnvUtils::Rendezvous(RThread& aThread, TRequestStatus& aStatus)
977 if(gEpocEnv->iId != TThreadId(0) &&
978 aThread.Open(gEpocEnv->iId) &&
979 aThread.ExitType() == EExitPending)
981 aThread.Rendezvous(aStatus);
982 return ETrue;
984 return EFalse;
987 void* SDL_LoadObject(const char *sofile)
989 RLibrary* lib = new RLibrary();
990 if(lib == NULL)
991 return NULL;
992 TFileName name;
993 name.Copy(TPtrC8((const TUint8*)sofile));
994 if(KErrNone == lib->Load(name))
995 return lib;
996 delete lib;
997 return NULL;
1000 void* SDL_LoadFunction(void *handle, const char *name)
1002 TLex8 v((const TUint8*)(name));
1003 int ord;
1005 if(KErrNone != v.Val(ord))
1006 return NULL;
1008 const RLibrary* lib = reinterpret_cast<RLibrary*>(handle);
1009 TLibraryFunction f = lib->Lookup(ord);
1010 return (void*)(f);
1013 void SDL_UnloadObject(void *handle)
1015 RLibrary* lib = reinterpret_cast<RLibrary*>(handle);
1016 lib->Close();
1017 delete lib;