2 #include "SDL_events_c.h"
5 #include "sdlepocapi.h"
13 #include "SDL_epocevents_c.h"
14 #include "SDL_keysym.h"
16 #include "SDL_loadso.h"
17 #include <remconcoreapitargetobserver.h>
18 #include <remconinterfaceselector.h>
19 #include <remconcoreapitarget.h>
31 CEventQueue
* iEventQueue
;
35 CArrayFix
<TSdlCleanupItem
>* iCleanupItems
;
37 bool iWaitingForOrientationChange
;
43 EpocSdlEnvData
* gEpocEnv
;
45 static void RunSingleThread()
47 if(RThread().RequestCount() > 0)
50 if(CActiveScheduler::RunIfReady(err
, CActive::EPriorityIdle
))
52 CActiveScheduler::Current()->WaitForAnyRequest();
57 int Panic(int aErr
, int aLine
)
60 b
.Format(_L("Main at %d"), aLine
);
66 bool CEventQueue::HasData()
69 return !m_queue
.empty();
72 const TWsEvent
CEventQueue::Shift()
74 const TWsEvent event
= m_queue
.front();
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;}
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
113 int Request(int aService
);
115 void SetParam(int aParam
);
120 const TThreadId iMainId
;
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
;
138 CSdlAppServ::~CSdlAppServ()
144 int CSdlAppServ::Request(int aService
)
148 iAppThread
.RequestComplete(iStatusPtr
, 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
)
168 case EAppSrvDsaStatus
:
169 if(gEpocEnv
->iDsa
!= NULL
)
170 gEpocEnv
->iDsa
->Stop();
171 iReturnValue
= KErrNone
;
175 PANIC(KErrNotSupported
);
179 iStatus
= KRequestPending
;
180 iStatusPtr
= &iStatus
;
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
));
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;
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()
298 if(sdl
.Open(gEpocEnv
->iId
) == KErrNone
)
300 if(sdl
.ExitType() == EExitPending
)
309 void EpocSdlEnvData::Free()
311 if(RThread().Id() == gEpocEnv
->iId
)
318 __ASSERT_ALWAYS(CheckSdl(), PANIC(KErrNotReady
));
321 void EpocSdlEnvData::Delete()
334 gEpocEnv
->iCleanupItems
= new CArrayFixFlat
<TSdlCleanupItem
>(8);
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! */
345 for(int i
= 0; i
< 6; i
++)
347 void* f
= (void*) iMain
[i
];
353 ret
= ((mainfunc1
)f
)();
361 ret
= ((mainfunc2
)f
)(1, argv
);
365 ((mainfunc2
)f
)(1, argv
);
369 ret
= ((mainfunc3
)f
)(1, argv
, envp
);
373 ((mainfunc3
)f
)(1, argv
, envp
);
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
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;
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()
439 TRAPD(err
, dsa
= gEpocEnv
->iDsa
->CreateGlesDsaL());
440 gEpocEnv
->iDsa
= dsa
;
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
);
486 void CSDL::AppendWsEvent(const TWsEvent
& aEvent
)
488 EpocSdlEnv::EventQueue().Append(aEvent
);
493 if(gEpocEnv
->iWaitingForOrientationChange
)
495 EpocSdlEnv::AllocSurface(gEpocEnv
->iSize
, gEpocEnv
->iMode
);
496 gEpocEnv
->iWaitingForOrientationChange
= false;
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
)
511 event
.SetType(EEventKey
);
516 case ERemConCoreApiVolumeDown
:
517 event
.Key()->iScanCode
= EStdKeyDecVolume
;
518 event
.SetType(EEventKeyDown
);
519 AppendWsEvent(event
);
520 event
.SetType(EEventKeyUp
);
521 AppendWsEvent(event
);
524 case ERemConCoreApiVolumeUp
:
525 event
.Key()->iScanCode
= EStdKeyIncVolume
;
526 event
.SetType(EEventKeyDown
);
527 AppendWsEvent(event
);
528 event
.SetType(EEventKeyUp
);
529 AppendWsEvent(event
);