Remove unused functionality.
[SDL.s60v3.git] / src / main / symbian / SDL_main.cpp
bloba0f70e5c021270b99f88c18e8d399d3a733ab601
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 bool iWaitingForOrientationChange;
39 TSize iSize;
40 TDisplayMode iMode;
43 EpocSdlEnvData* gEpocEnv;
45 static void RunSingleThread()
47 if(RThread().RequestCount() > 0)
49 int err;
50 if(CActiveScheduler::RunIfReady(err, CActive::EPriorityIdle))
52 CActiveScheduler::Current()->WaitForAnyRequest();
57 int Panic(int aErr, int aLine)
59 TBuf<64> b;
60 b.Format(_L("Main at %d"), aLine);
61 User::Panic(b, aErr);
62 return 0;
66 bool CEventQueue::HasData()
68 RunSingleThread();
69 return !m_queue.empty();
72 const TWsEvent CEventQueue::Shift()
74 const TWsEvent event = m_queue.front();
75 m_queue.pop();
76 return event;
80 TSdlCleanupItem::TSdlCleanupItem(TSdlCleanupOperation aOperation, void* aItem) :
81 iOperation(aOperation), iItem(aItem), iThread(RThread().Id())
84 #define MAINFUNC(x) TMainFunc::TMainFunc(mainfunc##x aFunc){Mem::FillZ(iMainFunc, sizeof(iMainFunc)); iMainFunc[x - 1] = (void*) aFunc;}
86 MAINFUNC(1)
87 MAINFUNC(2)
88 MAINFUNC(3)
89 MAINFUNC(4)
90 MAINFUNC(5)
91 MAINFUNC(6)
93 TMainFunc::TMainFunc()
95 Mem::FillZ(iMainFunc, sizeof(iMainFunc));
98 const void* TMainFunc::operator[](int aIndex) const
100 return iMainFunc[aIndex];
103 class CSdlAppServ : public CActive
105 public:
106 enum
108 EAppSrvDsaStatus,
110 CSdlAppServ();
111 void ConstructL();
112 ~CSdlAppServ();
113 int Request(int aService);
114 void Init();
115 void SetParam(int aParam);
117 private:
118 void RunL();
119 void DoCancel();
120 const TThreadId iMainId;
121 RThread iAppThread;
122 int iService;
123 int iReturnValue;
124 TRequestStatus* iStatusPtr;
127 CSdlAppServ::CSdlAppServ() : CActive(CActive::EPriorityHigh), iMainId(RThread().Id())
130 void CSdlAppServ::ConstructL()
132 CActiveScheduler::Add(this);
133 iStatus = KRequestPending;
134 iStatusPtr = &iStatus;
135 SetActive();
138 CSdlAppServ::~CSdlAppServ()
140 Cancel();
141 iAppThread.Close();
144 int CSdlAppServ::Request(int aService)
146 RunSingleThread();
147 iService = aService;
148 iAppThread.RequestComplete(iStatusPtr, KErrNone);
149 return KErrNone;
152 void CSdlAppServ::Init()
154 PANIC_IF_ERROR(iAppThread.Open(iMainId));
157 void CSdlAppServ::SetParam(int aParam)
159 iReturnValue = aParam;
162 void CSdlAppServ::RunL()
164 if(iStatus == KErrNone)
166 switch(iService)
168 case EAppSrvDsaStatus:
169 if(gEpocEnv->iDsa != NULL)
170 gEpocEnv->iDsa->Stop();
171 iReturnValue = KErrNone;
172 break;
174 default:
175 PANIC(KErrNotSupported);
176 break;
179 iStatus = KRequestPending;
180 iStatusPtr = &iStatus;
181 SetActive();
185 void CSdlAppServ::DoCancel()
187 TRequestStatus* s = &iStatus;
188 iAppThread.RequestComplete(s, KErrCancel);
191 CEventQueue& EpocSdlEnv::EventQueue()
193 __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
194 return *gEpocEnv->iEventQueue;
197 TBool EpocSdlEnv::IsDsaAvailable()
199 __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
200 return gEpocEnv->iDsa != NULL && gEpocEnv->iDsa->IsDsaAvailable();
203 int EpocSdlEnv::AllocSurface(const TSize& aSize, TDisplayMode aMode)
205 return gEpocEnv->iDsa->AllocSurface(aSize, aMode);
208 void EpocSdlEnv::UnlockHwSurface()
210 gEpocEnv->iDsa->UnlockHwSurface();
213 TUint8* EpocSdlEnv::LockHwSurface()
215 return gEpocEnv->iDsa->LockHwSurface();
218 void EpocSdlEnv::UpdateSwSurface()
220 gEpocEnv->iDsa->UpdateSwSurface();
223 TBool EpocSdlEnv::AddUpdateRect(TUint8* aAddress, const TRect& aUpdateRect, const TRect& aRect)
225 return gEpocEnv->iDsa->AddUpdateRect(aAddress, aUpdateRect, aRect);
228 void EpocSdlEnv::Request(int aService)
230 __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
231 gEpocEnv->iAppSrv->Request(aService);
234 TDisplayMode EpocSdlEnv::DisplayMode()
236 return gEpocEnv->iDsa == NULL ? ENone : gEpocEnv->iDsa->DisplayMode();
239 int EpocSdlEnv::SetPalette(int aFirstcolor, int aColorCount, TUint32* aPalette)
241 return gEpocEnv->iDsa->SetPalette(aFirstcolor, aColorCount, aPalette);
244 int EpocSdlEnv::AppendCleanupItem(const TSdlCleanupItem& aItem)
246 TRAPD(err, gEpocEnv->iCleanupItems->AppendL(aItem));
247 return err;
250 void EpocSdlEnv::RemoveCleanupItem(void* aItem)
252 for(int i = 0; i < gEpocEnv->iCleanupItems->Count(); i++)
254 if(gEpocEnv->iCleanupItems->At(i).iItem == aItem)
255 gEpocEnv->iCleanupItems->Delete(i);
259 void EpocSdlEnv::CleanupItems()
261 const TThreadId id = RThread().Id();
262 int last = gEpocEnv->iCleanupItems->Count() - 1;
263 int i;
265 for(i = last; i >= 0 ; i--)
267 TSdlCleanupItem& item = gEpocEnv->iCleanupItems->At(i);
268 if(item.iThread == id)
270 item.iThread = TThreadId(0);
271 item.iOperation(item.iItem);
275 last = gEpocEnv->iCleanupItems->Count() - 1;
277 for(i = last; i >= 0 ; i--)
279 TSdlCleanupItem& item = gEpocEnv->iCleanupItems->At(i);
280 if(item.iThread == TThreadId(0))
282 gEpocEnv->iCleanupItems->Delete(i);
287 void EpocSdlEnv::FreeSurface()
289 Request(CSdlAppServ::EAppSrvDsaStatus);
290 if(gEpocEnv->iDsa != NULL)
291 gEpocEnv->iDsa->Free();
294 static TBool CheckSdl()
296 int isExit = ETrue;
297 RThread sdl;
298 if(sdl.Open(gEpocEnv->iId) == KErrNone)
300 if(sdl.ExitType() == EExitPending)
302 isExit = EFalse;
304 sdl.Close();
306 return isExit;
309 void EpocSdlEnvData::Free()
311 if(RThread().Id() == gEpocEnv->iId)
313 if(iDsa != NULL)
314 iDsa->Free();
315 return;
318 __ASSERT_ALWAYS(CheckSdl(), PANIC(KErrNotReady));
321 void EpocSdlEnvData::Delete()
323 delete iEventQueue;
325 if(iDsa != NULL)
326 iDsa->Free();
328 delete iDsa;
329 delete iAppSrv;
332 static int MainL()
334 gEpocEnv->iCleanupItems = new CArrayFixFlat<TSdlCleanupItem>(8);
336 char** envp = 0;
337 char** argv = new char*[1];
338 argv[0] = new char[8];
339 strcpy(argv[0], "app.exe");
341 TMainFunc iMain = SDL_main;
343 /* !! process exits here if there is "exit()" in main! */
344 int ret = 0;
345 for(int i = 0; i < 6; i++)
347 void* f = (void*) iMain[i];
348 if(f != NULL)
350 switch(i)
352 case 0:
353 ret = ((mainfunc1)f)();
354 break;
356 case 3:
357 ((mainfunc1)f)();
358 break;
360 case 1:
361 ret = ((mainfunc2)f)(1, argv);
362 break;
364 case 4:
365 ((mainfunc2)f)(1, argv);
366 break;
368 case 2:
369 ret = ((mainfunc3)f)(1, argv, envp);
370 break;
372 case 5:
373 ((mainfunc3)f)(1, argv, envp);
374 break;
376 default:
377 break;
380 delete[] argv[0];
381 delete[] argv;
382 return ret;
386 PANIC(KErrNotFound);
387 return 0;
390 static int DoMain()
392 gEpocEnv->iAppSrv->Init();
394 TRAPD(err, err = MainL());
396 // Free resources and return
397 EpocSdlEnv::CleanupItems();
399 gEpocEnv->iCleanupItems->Reset();
400 delete gEpocEnv->iCleanupItems;
401 gEpocEnv->iCleanupItems = NULL;
403 gEpocEnv->Free(); //free up in thread resources
405 return err;
408 CSDL::CSDL()
410 __ASSERT_ALWAYS(gEpocEnv == NULL, PANIC(KErrAlreadyExists));
411 gEpocEnv = new EpocSdlEnvData;
412 Mem::FillZ(gEpocEnv, sizeof(EpocSdlEnvData));
414 gEpocEnv->iEventQueue = new CEventQueue();
415 gEpocEnv->iAppSrv = new CSdlAppServ();
417 gEpocEnv->iSdl = this;
420 CSDL::~CSDL()
422 gEpocEnv->Free();
423 gEpocEnv->Delete();
425 delete gEpocEnv;
426 gEpocEnv = NULL;
429 void CSDL::SetContainerWindowL(RWindow& aWindow, RWsSession& aSession, CWsScreenDevice& aDevice)
431 if(gEpocEnv->iDsa == NULL)
432 gEpocEnv->iDsa = CDsa::CreateL(aSession);
433 gEpocEnv->iDsa->ConstructL(aWindow, aDevice);
436 int EpocSdlEnv::ApplyGlesDsa()
438 CDsa* dsa = NULL;
439 TRAPD(err, dsa = gEpocEnv->iDsa->CreateGlesDsaL());
440 gEpocEnv->iDsa = dsa;
441 return err;
444 RWindow* EpocSdlEnv::Window()
446 return gEpocEnv->iDsa->Window();
449 void EpocSdlEnv::UpdateWholeScreen(bool val)
451 gEpocEnv->iDsa->m_updateWholeScreen = val;
454 bool EpocSdlEnv::GetUpdateWholeScreen()
456 return gEpocEnv->iDsa->m_updateWholeScreen;
459 void EpocSdlEnv::SetOrientation(CAknAppUi::TAppUiOrientation orientation, const TSize& aSize, TDisplayMode aMode)
461 gEpocEnv->iWaitingForOrientationChange = true;
462 gEpocEnv->iSize = aSize;
463 gEpocEnv->iMode = aMode;
465 TRAPD(err, static_cast<CAknAppUi*>(CEikonEnv::Static()->EikAppUi())->SetOrientationL(orientation));
468 void CSDL::CallMainL()
470 ASSERT(gEpocEnv != NULL);
472 gEpocEnv->iAppSrv->ConstructL();
474 // for handling volume up/down keys
475 CRemConInterfaceSelector *iSelector = CRemConInterfaceSelector::NewL();
476 CRemConCoreApiTarget::NewL( *iSelector, *this );
477 iSelector->OpenTargetL();
479 gEpocEnv->iId = RThread().Id();
480 // when priority is not lowered screen updates much more frequently, which
481 // may be undesired, for example in case of openttd's generating world dialog
482 RThread().SetPriority(EPriorityLess);
483 DoMain();
486 void CSDL::AppendWsEvent(const TWsEvent& aEvent)
488 EpocSdlEnv::EventQueue().Append(aEvent);
491 void CSDL::Resize()
493 if(gEpocEnv->iWaitingForOrientationChange)
495 EpocSdlEnv::AllocSurface(gEpocEnv->iSize, gEpocEnv->iMode);
496 gEpocEnv->iWaitingForOrientationChange = false;
498 else
500 TSize size = gEpocEnv->iDsa->Window()->Size();
501 SDL_PrivateResize(size.iWidth, size.iHeight);
505 void CSDL::MrccatoCommand(TRemConCoreApiOperationId aOperationId, TRemConCoreApiButtonAction aButtonAct)
507 if(aButtonAct != ERemConCoreApiButtonClick)
508 return;
510 TWsEvent event;
511 event.SetType(EEventKey);
512 event.SetTimeNow();
514 switch(aOperationId)
516 case ERemConCoreApiVolumeDown:
517 event.Key()->iScanCode = EStdKeyDecVolume;
518 event.SetType(EEventKeyDown);
519 AppendWsEvent(event);
520 event.SetType(EEventKeyUp);
521 AppendWsEvent(event);
522 break;
524 case ERemConCoreApiVolumeUp:
525 event.Key()->iScanCode = EStdKeyIncVolume;
526 event.SetType(EEventKeyDown);
527 AppendWsEvent(event);
528 event.SetType(EEventKeyUp);
529 AppendWsEvent(event);
530 break;
532 default:
533 break;