Further simplification.
[SDL.s60v3.git] / src / main / symbian / SDL_main.cpp
blob7a4d90d112007c4ef9b3b2662eeca397a80374ad
1 extern "C" {
2 #include "SDL_events_c.h"
4 #include "epoc_sdl.h"
5 #include "sdlepocapi.h"
6 #include <e32base.h>
7 #include <estlib.h>
8 #include <stdio.h>
9 #include <badesca.h>
10 #include <w32std.h>
11 #include <aknappui.h>
12 #include <aknapp.h>
13 #include "SDL_epocevents_c.h"
14 #include "SDL_keysym.h"
15 #include "dsa.h"
16 #include "SDL_loadso.h"
17 #include <remconcoreapitargetobserver.h>
18 #include <remconinterfaceselector.h>
19 #include <remconcoreapitarget.h>
21 class CCurrentAppUi;
22 class CEikonEnv;
23 class CSdlAppServ;
24 class CEventQueue;
26 class EpocSdlEnvData
28 public:
29 void Free();
30 void Delete();
31 CEventQueue* iEventQueue;
32 CDsa* iDsa;
33 CSdlAppServ* iAppSrv;
34 TThreadId iId;
35 CArrayFix<TSdlCleanupItem>* iCleanupItems;
36 CSDL* iSdl;
37 TRequestStatus* iCallerStatus;
38 bool iWaitingForOrientationChange;
40 TSize iSize;
41 TDisplayMode iMode;
44 EpocSdlEnvData* gEpocEnv;
46 static void RunSingleThread()
48 if(RThread().RequestCount() > 0)
50 int err;
51 if(CActiveScheduler::RunIfReady(err, CActive::EPriorityIdle))
53 CActiveScheduler::Current()->WaitForAnyRequest();
58 int Panic(int aErr, int aLine)
60 TBuf<64> b;
61 b.Format(_L("Main at %d"), aLine);
62 User::Panic(b, aErr);
63 return 0;
67 bool CEventQueue::HasData()
69 RunSingleThread();
70 return !m_queue.empty();
73 const TWsEvent CEventQueue::Shift()
75 const TWsEvent event = m_queue.front();
76 m_queue.pop();
77 return event;
81 TSdlCleanupItem::TSdlCleanupItem(TSdlCleanupOperation aOperation, void* aItem) :
82 iOperation(aOperation), iItem(aItem), iThread(RThread().Id())
85 #define MAINFUNC(x) TMainFunc::TMainFunc(mainfunc##x aFunc){Mem::FillZ(iMainFunc, sizeof(iMainFunc)); iMainFunc[x - 1] = (void*) aFunc;}
87 MAINFUNC(1)
88 MAINFUNC(2)
89 MAINFUNC(3)
90 MAINFUNC(4)
91 MAINFUNC(5)
92 MAINFUNC(6)
94 TMainFunc::TMainFunc()
96 Mem::FillZ(iMainFunc, sizeof(iMainFunc));
99 const void* TMainFunc::operator[](int aIndex) const
101 return iMainFunc[aIndex];
104 class CSdlAppServ : public CActive
106 public:
107 enum
109 EAppSrvDsaStatus,
110 EAppSrvStopThread
112 CSdlAppServ();
113 void ConstructL();
114 ~CSdlAppServ();
115 int Request(int aService);
116 void Init();
117 void SetParam(int aParam);
119 private:
120 void RunL();
121 void DoCancel();
122 const TThreadId iMainId;
123 RThread iAppThread;
124 int iService;
125 int iReturnValue;
126 TRequestStatus* iStatusPtr;
129 CSdlAppServ::CSdlAppServ() : CActive(CActive::EPriorityHigh), iMainId(RThread().Id())
132 void CSdlAppServ::ConstructL()
134 CActiveScheduler::Add(this);
135 iStatus = KRequestPending;
136 iStatusPtr = &iStatus;
137 SetActive();
140 CSdlAppServ::~CSdlAppServ()
142 Cancel();
143 iAppThread.Close();
146 int CSdlAppServ::Request(int aService)
148 RunSingleThread();
149 iService = aService;
150 iAppThread.RequestComplete(iStatusPtr, KErrNone);
151 return KErrNone;
154 void CSdlAppServ::Init()
156 PANIC_IF_ERROR(iAppThread.Open(iMainId));
159 void CSdlAppServ::SetParam(int aParam)
161 iReturnValue = aParam;
164 void CSdlAppServ::RunL()
166 if(iStatus == KErrNone)
168 switch(iService)
170 case CSdlAppServ::EAppSrvStopThread:
171 if(gEpocEnv->iDsa != NULL)
172 gEpocEnv->iDsa->SetSuspend();
173 break;
175 case EAppSrvDsaStatus:
176 if(gEpocEnv->iDsa != NULL)
177 gEpocEnv->iDsa->Stop();
178 iReturnValue = KErrNone;
179 break;
181 default:
182 PANIC(KErrNotSupported);
183 break;
186 iStatus = KRequestPending;
187 iStatusPtr = &iStatus;
188 SetActive();
192 void CSdlAppServ::DoCancel()
194 TRequestStatus* s = &iStatus;
195 iAppThread.RequestComplete(s, KErrCancel);
198 CEventQueue& EpocSdlEnv::EventQueue()
200 __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
201 return *gEpocEnv->iEventQueue;
204 TBool EpocSdlEnv::IsDsaAvailable()
206 __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
207 return gEpocEnv->iDsa != NULL && gEpocEnv->iDsa->IsDsaAvailable();
210 void EpocSdlEnv::WaitDsaAvailable()
212 gEpocEnv->iAppSrv->Request(CSdlAppServ::EAppSrvStopThread);
215 int EpocSdlEnv::AllocSurface(const TSize& aSize, TDisplayMode aMode)
217 return gEpocEnv->iDsa->AllocSurface(aSize, aMode);
220 void EpocSdlEnv::UnlockHwSurface()
222 gEpocEnv->iDsa->UnlockHwSurface();
225 TUint8* EpocSdlEnv::LockHwSurface()
227 return gEpocEnv->iDsa->LockHwSurface();
230 void EpocSdlEnv::UpdateSwSurface()
232 gEpocEnv->iDsa->UpdateSwSurface();
235 TBool EpocSdlEnv::AddUpdateRect(TUint8* aAddress, const TRect& aUpdateRect, const TRect& aRect)
237 return gEpocEnv->iDsa->AddUpdateRect(aAddress, aUpdateRect, aRect);
240 void EpocSdlEnv::Request(int aService)
242 __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
243 gEpocEnv->iAppSrv->Request(aService);
246 TDisplayMode EpocSdlEnv::DisplayMode()
248 return gEpocEnv->iDsa == NULL ? ENone : gEpocEnv->iDsa->DisplayMode();
251 int EpocSdlEnv::SetPalette(int aFirstcolor, int aColorCount, TUint32* aPalette)
253 return gEpocEnv->iDsa->SetPalette(aFirstcolor, aColorCount, aPalette);
256 int EpocSdlEnv::AppendCleanupItem(const TSdlCleanupItem& aItem)
258 TRAPD(err, gEpocEnv->iCleanupItems->AppendL(aItem));
259 return err;
262 void EpocSdlEnv::RemoveCleanupItem(void* aItem)
264 for(int i = 0; i < gEpocEnv->iCleanupItems->Count(); i++)
266 if(gEpocEnv->iCleanupItems->At(i).iItem == aItem)
267 gEpocEnv->iCleanupItems->Delete(i);
271 void EpocSdlEnv::CleanupItems()
273 const TThreadId id = RThread().Id();
274 int last = gEpocEnv->iCleanupItems->Count() - 1;
275 int i;
277 for(i = last; i >= 0 ; i--)
279 TSdlCleanupItem& item = gEpocEnv->iCleanupItems->At(i);
280 if(item.iThread == id)
282 item.iThread = TThreadId(0);
283 item.iOperation(item.iItem);
287 last = gEpocEnv->iCleanupItems->Count() - 1;
289 for(i = last; i >= 0 ; i--)
291 TSdlCleanupItem& item = gEpocEnv->iCleanupItems->At(i);
292 if(item.iThread == TThreadId(0))
294 gEpocEnv->iCleanupItems->Delete(i);
299 void EpocSdlEnv::FreeSurface()
301 Request(CSdlAppServ::EAppSrvDsaStatus);
302 if(gEpocEnv->iDsa != NULL)
303 gEpocEnv->iDsa->Free();
306 static TBool CheckSdl()
308 int isExit = ETrue;
309 RThread sdl;
310 if(sdl.Open(gEpocEnv->iId) == KErrNone)
312 if(sdl.ExitType() == EExitPending)
314 isExit = EFalse;
316 sdl.Close();
318 return isExit;
321 void EpocSdlEnvData::Free()
323 if(RThread().Id() == gEpocEnv->iId)
325 if(iDsa != NULL)
326 iDsa->Free();
327 return;
330 __ASSERT_ALWAYS(CheckSdl(), PANIC(KErrNotReady));
333 void EpocSdlEnvData::Delete()
335 delete iEventQueue;
337 if(iDsa != NULL)
338 iDsa->Free();
340 delete iDsa;
341 delete iAppSrv;
344 static int MainL()
346 gEpocEnv->iCleanupItems = new CArrayFixFlat<TSdlCleanupItem>(8);
348 char** envp = 0;
349 char** argv = new char*[1];
350 argv[0] = new char[8];
351 strcpy(argv[0], "app.exe");
353 TMainFunc iMain = SDL_main;
355 /* !! process exits here if there is "exit()" in main! */
356 int ret = 0;
357 for(int i = 0; i < 6; i++)
359 void* f = (void*) iMain[i];
360 if(f != NULL)
362 switch(i)
364 case 0:
365 ret = ((mainfunc1)f)();
366 break;
368 case 3:
369 ((mainfunc1)f)();
370 break;
372 case 1:
373 ret = ((mainfunc2)f)(1, argv);
374 break;
376 case 4:
377 ((mainfunc2)f)(1, argv);
378 break;
380 case 2:
381 ret = ((mainfunc3)f)(1, argv, envp);
382 break;
384 case 5:
385 ((mainfunc3)f)(1, argv, envp);
386 break;
388 default:
389 break;
392 delete[] argv[0];
393 delete[] argv;
394 return ret;
398 PANIC(KErrNotFound);
399 return 0;
402 static int DoMain()
404 gEpocEnv->iAppSrv->Init();
406 TRAPD(err, err = MainL());
408 // Free resources and return
409 EpocSdlEnv::CleanupItems();
411 gEpocEnv->iCleanupItems->Reset();
412 delete gEpocEnv->iCleanupItems;
413 gEpocEnv->iCleanupItems = NULL;
415 gEpocEnv->Free(); //free up in thread resources
417 if(gEpocEnv->iCallerStatus != NULL)
419 User::RequestComplete(gEpocEnv->iCallerStatus, err);
420 return 0;
422 else
424 return err == KErrNone ? 0 : err;
428 CSDL::~CSDL()
430 gEpocEnv->Free();
431 gEpocEnv->Delete();
433 delete gEpocEnv;
434 gEpocEnv = NULL;
437 CSDL* CSDL::NewL()
439 __ASSERT_ALWAYS(gEpocEnv == NULL, PANIC(KErrAlreadyExists));
440 gEpocEnv = new EpocSdlEnvData;
441 Mem::FillZ(gEpocEnv, sizeof(EpocSdlEnvData));
443 gEpocEnv->iEventQueue = new CEventQueue();
444 gEpocEnv->iAppSrv = new CSdlAppServ();
446 CSDL* sdl = new CSDL();
448 gEpocEnv->iSdl = sdl;
450 return sdl;
453 void CSDL::SetContainerWindowL(RWindow& aWindow, RWsSession& aSession, CWsScreenDevice& aDevice)
455 if(gEpocEnv->iDsa == NULL)
456 gEpocEnv->iDsa = CDsa::CreateL(aSession);
457 gEpocEnv->iDsa->ConstructL(aWindow, aDevice);
460 int EpocSdlEnv::ApplyGlesDsa()
462 CDsa* dsa = NULL;
463 TRAPD(err, dsa = gEpocEnv->iDsa->CreateGlesDsaL());
464 gEpocEnv->iDsa = dsa;
465 return err;
468 RWindow* EpocSdlEnv::Window()
470 return gEpocEnv->iDsa->Window();
473 void EpocSdlEnv::UpdateWholeScreen(bool val)
475 gEpocEnv->iDsa->m_updateWholeScreen = val;
478 bool EpocSdlEnv::GetUpdateWholeScreen()
480 return gEpocEnv->iDsa->m_updateWholeScreen;
483 void EpocSdlEnv::SetOrientation(CAknAppUi::TAppUiOrientation orientation, const TSize& aSize, TDisplayMode aMode)
485 gEpocEnv->iWaitingForOrientationChange = true;
486 gEpocEnv->iSize = aSize;
487 gEpocEnv->iMode = aMode;
489 TRAPD(err, static_cast<CAknAppUi*>(CEikonEnv::Static()->EikAppUi())->SetOrientationL(orientation));
492 TThreadId CSDL::CallMainL(TRequestStatus* const aStatus)
494 ASSERT(gEpocEnv != NULL);
496 gEpocEnv->iAppSrv->ConstructL();
498 // for handling volume up/down keys
499 CRemConInterfaceSelector *iSelector = CRemConInterfaceSelector::NewL();
500 CRemConCoreApiTarget::NewL( *iSelector, *this );
501 iSelector->OpenTargetL();
503 gEpocEnv->iCallerStatus = aStatus;
504 if(aStatus != NULL)
505 *aStatus = KRequestPending;
506 gEpocEnv->iId = RThread().Id();
507 // when priority is not lowered screen updates much more frequently, which
508 // may be undesired, for example in case of openttd's generating world dialog
509 RThread().SetPriority(EPriorityLess);
510 DoMain();
512 return gEpocEnv->iId;
515 void CSDL::AppendWsEvent(const TWsEvent& aEvent)
517 EpocSdlEnv::EventQueue().Append(aEvent);
520 void CSDL::Resize()
522 if(gEpocEnv->iWaitingForOrientationChange)
524 EpocSdlEnv::AllocSurface(gEpocEnv->iSize, gEpocEnv->iMode);
525 gEpocEnv->iWaitingForOrientationChange = false;
527 else
529 TSize size = gEpocEnv->iDsa->Window()->Size();
530 SDL_PrivateResize(size.iWidth, size.iHeight);
534 void CSDL::MrccatoCommand(TRemConCoreApiOperationId aOperationId, TRemConCoreApiButtonAction aButtonAct)
536 if(aButtonAct != ERemConCoreApiButtonClick)
537 return;
539 TWsEvent event;
540 event.SetType(EEventKey);
541 event.SetTimeNow();
543 switch(aOperationId)
545 case ERemConCoreApiVolumeDown:
546 event.Key()->iScanCode = EStdKeyDecVolume;
547 event.SetType(EEventKeyDown);
548 AppendWsEvent(event);
549 event.SetType(EEventKeyUp);
550 AppendWsEvent(event);
551 break;
553 case ERemConCoreApiVolumeUp:
554 event.Key()->iScanCode = EStdKeyIncVolume;
555 event.SetType(EEventKeyDown);
556 AppendWsEvent(event);
557 event.SetType(EEventKeyUp);
558 AppendWsEvent(event);
559 break;
561 default:
562 break;