Force hardware pixel format to 565.
[SDL.s60v3.git] / src / main / symbian / SDL_main.cpp
blob7ccf2df0918033a65c50a96ec519652e6d64b52e
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>
20 #include "ProfileWatcher.h"
22 CSDL* g_SDL = NULL;
24 class CCurrentAppUi;
25 class CEikonEnv;
26 class CSdlAppServ;
27 class CEventQueue;
29 class EpocSdlEnvData
31 public:
32 void Free();
33 void Delete();
34 CDsa* iDsa;
35 CSdlAppServ* iAppSrv;
36 CArrayFix<TSdlCleanupItem>* iCleanupItems;
39 EpocSdlEnvData* gEpocEnv;
41 static void RunSingleThread()
43 if(RThread().RequestCount() > 0)
45 int err;
46 if(CActiveScheduler::RunIfReady(err, CActive::EPriorityIdle))
48 CActiveScheduler::Current()->WaitForAnyRequest();
53 int Panic(int aErr, int aLine)
55 TBuf<64> b;
56 b.Format(_L("Main at %d"), aLine);
57 User::Panic(b, aErr);
58 return 0;
61 bool CEventQueue::HasData()
63 RunSingleThread();
64 return !m_queue.empty();
67 const TWsEvent CEventQueue::Shift()
69 const TWsEvent event = m_queue.front();
70 m_queue.pop();
71 return event;
75 TSdlCleanupItem::TSdlCleanupItem(TSdlCleanupOperation aOperation, void* aItem) :
76 iOperation(aOperation), iItem(aItem), iThread(RThread().Id())
80 ProfileWatcher::ProfileWatcher(MProfileWatcher& observer)
81 : CActive(EPriorityLow)
82 , m_repository(CRepository::NewL(KCRUidProfileEngine))
83 , m_observer(observer)
85 CActiveScheduler::Add(this);
86 m_repository->NotifyRequest(KProEngActiveProfile, iStatus);
87 SetActive();
90 ProfileWatcher::~ProfileWatcher()
92 Cancel();
93 delete m_repository;
96 int ProfileWatcher::GetProfile()
98 int value;
99 m_repository->Get(KProEngActiveProfile, value);
100 return value;
103 void ProfileWatcher::DoCancel()
105 m_repository->NotifyCancel(KProEngActiveProfile);
108 void ProfileWatcher::RunL()
110 int value;
111 if(m_repository->Get(KProEngActiveProfile, value) == KErrNone)
113 m_observer.ProfileChanged(value);
115 m_repository->NotifyRequest(KProEngActiveProfile, iStatus);
116 SetActive();
120 class CSdlAppServ : public CActive
122 public:
123 CSdlAppServ() : CActive(CActive::EPriorityIdle)
125 CActiveScheduler::Add(this);
126 SetActive();
128 ~CSdlAppServ() { Cancel(); }
129 void Request()
131 TRequestStatus* status = &iStatus;
132 User::RequestComplete(status, KErrNone);
135 private:
136 void RunL() {}
137 void DoCancel()
139 TRequestStatus* status = &iStatus;
140 User::RequestComplete(status, KErrCancel);
144 bool EpocSdlEnv::IsDsaAvailable()
146 __ASSERT_DEBUG(gEpocEnv != NULL, PANIC(KErrNotReady));
147 return gEpocEnv->iDsa != NULL && gEpocEnv->iDsa->IsDsaAvailable();
150 int EpocSdlEnv::AllocSurface(const TSize& aSize, int bpp)
152 return gEpocEnv->iDsa->AllocSurface(aSize, bpp);
155 void EpocSdlEnv::UnlockHwSurface()
157 gEpocEnv->iDsa->UnlockHwSurface();
160 TUint8* EpocSdlEnv::LockHwSurface()
162 return gEpocEnv->iDsa->LockSurface();
165 void EpocSdlEnv::UpdateSwSurface()
167 gEpocEnv->iDsa->UpdateSwSurface();
170 bool EpocSdlEnv::AddUpdateRect(TUint8* aAddress, const TRect& aUpdateRect, const TRect& aRect)
172 return gEpocEnv->iDsa->AddUpdateRect(aAddress, aUpdateRect, aRect);
175 int EpocSdlEnv::SetPalette(int aFirstcolor, int aColorCount, TUint32* aPalette)
177 return gEpocEnv->iDsa->SetPalette(aFirstcolor, aColorCount, aPalette);
180 int EpocSdlEnv::AppendCleanupItem(const TSdlCleanupItem& aItem)
182 TRAPD(err, gEpocEnv->iCleanupItems->AppendL(aItem));
183 return err;
186 void EpocSdlEnv::RemoveCleanupItem(void* aItem)
188 for(int i = 0; i < gEpocEnv->iCleanupItems->Count(); i++)
190 if(gEpocEnv->iCleanupItems->At(i).iItem == aItem)
191 gEpocEnv->iCleanupItems->Delete(i);
195 void EpocSdlEnv::CleanupItems()
197 const TThreadId id = RThread().Id();
198 int last = gEpocEnv->iCleanupItems->Count() - 1;
199 int i;
201 for(i = last; i >= 0 ; i--)
203 TSdlCleanupItem& item = gEpocEnv->iCleanupItems->At(i);
204 if(item.iThread == id)
206 item.iThread = TThreadId(0);
207 item.iOperation(item.iItem);
211 last = gEpocEnv->iCleanupItems->Count() - 1;
213 for(i = last; i >= 0 ; i--)
215 TSdlCleanupItem& item = gEpocEnv->iCleanupItems->At(i);
216 if(item.iThread == TThreadId(0))
218 gEpocEnv->iCleanupItems->Delete(i);
223 void EpocSdlEnv::FreeSurface()
225 gEpocEnv->iAppSrv->Request();
227 if(gEpocEnv->iDsa != NULL)
229 gEpocEnv->iDsa->Stop();
230 gEpocEnv->iDsa->Free();
234 void EpocSdlEnvData::Free()
236 if(iDsa != NULL)
237 iDsa->Free();
240 void EpocSdlEnvData::Delete()
242 if(iDsa != NULL)
243 iDsa->Free();
245 delete iDsa;
246 delete iAppSrv;
249 static int MainL()
251 gEpocEnv->iCleanupItems = new CArrayFixFlat<TSdlCleanupItem>(8);
253 char** argv = new char*[1];
254 argv[0] = new char[8];
255 strcpy(argv[0], "app.exe");
257 int ret = SDL_main( 1, argv );
259 delete[] argv[0];
260 delete[] argv;
262 return ret;
265 static int DoMain()
267 TRAPD(err, err = MainL());
269 // Free resources and return
270 EpocSdlEnv::CleanupItems();
272 gEpocEnv->iCleanupItems->Reset();
273 delete gEpocEnv->iCleanupItems;
274 gEpocEnv->iCleanupItems = NULL;
276 gEpocEnv->Free(); //free up in thread resources
278 return err;
281 CSDL::CSDL()
282 : m_orientationWait( false )
283 , m_soundPauseReason( SPR_NONE )
284 , m_profileWatcher( new ProfileWatcher(*this) )
286 if(m_profileWatcher->GetProfile() == 1)
288 m_soundPauseReason |= SPR_SILENTPROFILE;
291 __ASSERT_ALWAYS(gEpocEnv == NULL, PANIC(KErrAlreadyExists));
292 gEpocEnv = new EpocSdlEnvData;
293 Mem::FillZ(gEpocEnv, sizeof(EpocSdlEnvData));
295 m_eventQueue = new CEventQueue();
297 gEpocEnv->iAppSrv = new CSdlAppServ();
299 g_SDL = this;
302 CSDL::~CSDL()
304 delete m_profileWatcher;
306 g_SDL = NULL;
308 delete m_eventQueue;
310 gEpocEnv->Free();
311 gEpocEnv->Delete();
313 delete gEpocEnv;
314 gEpocEnv = NULL;
317 void CSDL::SetContainerWindowL(RWindow& aWindow, RWsSession& aSession, CWsScreenDevice& aDevice)
319 if(gEpocEnv->iDsa == NULL)
320 gEpocEnv->iDsa = CDsa::CreateL(aSession);
321 gEpocEnv->iDsa->ConstructL(aWindow, aDevice);
324 int EpocSdlEnv::ApplyGlesDsa()
326 CDsa* dsa = NULL;
327 TRAPD(err, dsa = gEpocEnv->iDsa->CreateGlesDsaL());
328 gEpocEnv->iDsa = dsa;
329 return err;
332 RWindow* EpocSdlEnv::Window()
334 return gEpocEnv->iDsa->Window();
337 void CSDL::CallMainL()
339 ASSERT(gEpocEnv != NULL);
341 // for handling volume up/down keys
342 CRemConInterfaceSelector *iSelector = CRemConInterfaceSelector::NewL();
343 CRemConCoreApiTarget::NewL( *iSelector, *this );
344 iSelector->OpenTargetL();
346 // when priority is not lowered screen updates much more frequently, which
347 // may be undesired, for example in case of openttd's generating world dialog
348 RThread().SetPriority(EPriorityLess);
349 DoMain();
352 void CSDL::AppendWsEvent(const TWsEvent& aEvent)
354 m_eventQueue->Append(aEvent);
357 void CSDL::Resize()
359 if(m_orientationWait)
361 EpocSdlEnv::AllocSurface(m_size, m_bpp);
362 m_orientationWait = false;
364 else
366 TSize size = gEpocEnv->iDsa->Window()->Size();
367 SDL_PrivateResize(size.iWidth, size.iHeight);
371 void CSDL::SetOrientation(CAknAppUi::TAppUiOrientation orientation, const TSize& aSize, int bpp)
373 m_orientationWait = true;
374 m_size = aSize;
375 m_bpp = bpp;
377 TRAPD(err, static_cast<CAknAppUi*>(CEikonEnv::Static()->EikAppUi())->SetOrientationL(orientation));
380 void CSDL::SetFocus(bool focused)
382 if(focused)
384 m_soundPauseReason &= ~SPR_FOCUSLOST;
386 else
388 m_soundPauseReason |= SPR_FOCUSLOST;
392 void CSDL::MrccatoCommand(TRemConCoreApiOperationId aOperationId, TRemConCoreApiButtonAction aButtonAct)
394 if(aButtonAct != ERemConCoreApiButtonClick)
395 return;
397 TWsEvent event;
398 event.SetType(EEventKey);
399 event.SetTimeNow();
401 switch(aOperationId)
403 case ERemConCoreApiVolumeDown:
404 event.Key()->iScanCode = EStdKeyDecVolume;
405 event.SetType(EEventKeyDown);
406 AppendWsEvent(event);
407 event.SetType(EEventKeyUp);
408 AppendWsEvent(event);
409 break;
411 case ERemConCoreApiVolumeUp:
412 event.Key()->iScanCode = EStdKeyIncVolume;
413 event.SetType(EEventKeyDown);
414 AppendWsEvent(event);
415 event.SetType(EEventKeyUp);
416 AppendWsEvent(event);
417 break;
419 default:
420 break;
424 void CSDL::ProfileChanged(int profile)
426 // Check for silent profile
427 if(profile == 1)
429 m_soundPauseReason |= SPR_SILENTPROFILE;
431 else
433 m_soundPauseReason &= ~SPR_SILENTPROFILE;