Handle orientation change events.
[SDL.s60v3.git] / src / main / symbian / sdlexe.cpp
blobbededaefb00b468330fe048000b9f96d621cd268
1 #include <aknapp.h>
2 #include <aknappui.h>
3 #include <eikdoc.h>
4 #include <sdlepocapi.h>
5 #include <bautils.h>
6 #include <eikstart.h>
7 #include <badesca.h>
8 #include <bautils.h>
9 #include <apgcli.h>
10 #include <sdlmain.h>
11 #include <eikedwin.h>
12 #include <eiklabel.h>
13 #include <sdlexe.rsg>
14 #include <aknglobalmsgquery.h>
15 #include <apgwgnam.h>
17 class CApaDocument;
19 LOCAL_C void MakeCCmdLineL(const TDesC8& aParam, CDesC8Array& aArray)
21 const TChar dq('\"');
23 TLex8 lex(aParam);
24 TBool in = EFalse;
26 lex.SkipSpaceAndMark();
28 while(!lex.Eos())
30 TPtrC8 ptr;
31 if(in)
33 const TPtrC8 rem = lex.RemainderFromMark();
34 const int pos = rem.Locate(dq);
35 if(pos > 0)
37 lex.Inc(pos);
38 ptr.Set(lex.MarkedToken());
39 lex.SkipAndMark(1);
41 else
43 ptr.Set(rem);
45 in = EFalse;
47 else
49 ptr.Set(lex.NextToken());
50 const int pos = ptr.Locate(dq);
51 if(pos == 0)
53 lex.UnGetToMark();
54 lex.SkipAndMark(1);
55 in = ETrue;
56 continue; // back to in brace
58 else
59 lex.SkipSpaceAndMark();
62 aArray.AppendL(ptr);
66 NONSHARABLE_CLASS(TSdlClass)
68 public:
69 TSdlClass();
70 void SetMain(const TMainFunc& aFunc, int aFlags, MSDLMainObs* aObs, int aExeFlags);
71 int SdlFlags() const;
72 const TMainFunc& Main() const;
73 void SendEvent(int aEvent, int aParam, CSDL* aSDL);
74 int AppFlags() const;
75 void AppFlags(int aFlags);
77 private:
78 TMainFunc iFunc;
79 int iSdlFlags;
80 int iExeFlags;
81 MSDLMainObs* iObs;
85 void TSdlClass::AppFlags(int aFlags)
87 iExeFlags |= aFlags;
90 void TSdlClass::SendEvent(int aEvent, int aParam, CSDL* aSDL)
92 if(iObs != NULL)
93 iObs->SDLMainEvent(aEvent, aParam, aSDL);
96 int TSdlClass::AppFlags() const
98 return iExeFlags;
101 void TSdlClass::SetMain(const TMainFunc& aFunc, int aFlags, MSDLMainObs* aObs, int aExeFlags)
103 iFunc = aFunc;
104 iSdlFlags = aFlags;
105 iExeFlags = aExeFlags;
106 iObs = aObs;
109 const TMainFunc& TSdlClass::Main() const
111 return iFunc;
115 int TSdlClass::SdlFlags() const
117 return iSdlFlags;
120 TSdlClass::TSdlClass()
122 Mem::FillZ(this, sizeof(this));
125 TSdlClass gSDLClass;
127 ////////////////////////////////////////////////////////////////
129 NONSHARABLE_CLASS(CSDLApplication) : public CAknApplication
131 public:
132 CSDLApplication();
134 private:
135 CApaDocument* CreateDocumentL();
136 TFileName ResourceFileName() const;
137 TUid AppDllUid() const;
138 void FindMeL();
140 TUid iUid;
143 NONSHARABLE_CLASS(CSDLDocument) : public CEikDocument
145 public:
146 CSDLDocument(CEikApplication& aApp);
148 private:
149 CEikAppUi* CreateAppUiL();
152 ////////////////////////////////////////////////////////////////////
154 NONSHARABLE_CLASS(CExitWait) : public CActive
156 public:
157 CExitWait(CAknAppUi& aWait);
158 ~CExitWait();
160 private:
161 void RunL();
162 void DoCancel();
164 CAknAppUi& iWait;
165 TRequestStatus* iStatusPtr;
168 ////////////////////////////////////////////////////////////////////////
170 NONSHARABLE_CLASS(CSDLWin) : public CCoeControl
172 public:
173 CSDLWin() : m_draw(false) {}
174 void ConstructL(const TRect& aRect);
175 RWindow& GetWindow() const;
176 void SetDraw(bool draw) { m_draw = draw; }
178 private:
179 void Draw(const TRect& aRect) const;
181 bool m_draw;
184 ////////////////////////////////////////////////////////////////////////////
186 NONSHARABLE_CLASS(CSDLAppUi) : public CAknAppUi
188 public:
189 ~CSDLAppUi();
191 private:
192 void ConstructL();
193 void HandleCommandL(int aCommand);
194 void HandleWsEventL(const TWsEvent& aEvent, CCoeControl* aDestination);
195 void HandleResourceChangeL(int aType);
197 void StartL();
198 static TBool StartL(TAny* aThis);
200 TBool ParamEditorL(TDes& aCheat);
202 TBool ProcessCommandParametersL(CApaCommandLine &aCommandLine);
204 void HandleForegroundEventL(TBool aForeground);
206 CExitWait* iWait;
207 CSDLWin* iSDLWin;
208 CSDL* iSdl;
209 CIdle* iStarter;
210 CDesC8Array* iParams;
211 int iResOffset;
214 ////////////////////////////////////////////////////////////////////////////////////////
216 CApaDocument* CSDLApplication::CreateDocumentL()
218 return new (ELeave) CSDLDocument(*this);
221 TUid CSDLApplication::AppDllUid() const
223 return iUid;
226 CSDLApplication::CSDLApplication()
228 TRAPD(err, FindMeL());
229 ASSERT(err == KErrNone);
232 void CSDLApplication::FindMeL()
234 RApaLsSession apa;
235 User::LeaveIfError(apa.Connect());
236 CleanupClosePushL(apa);
237 User::LeaveIfError(apa.GetAllApps());
238 TFileName name = RProcess().FileName();
239 TApaAppInfo info;
240 while(apa.GetNextApp(info) == KErrNone)
242 if(info.iFullName.CompareF(name) == 0)
244 iUid = info.iUid;
245 break;
248 CleanupStack::PopAndDestroy();
251 TFileName CSDLApplication::ResourceFileName() const
253 return KNullDesC();
256 ///////////////////////////////////////////////////////////////////////////////////////////
258 CExitWait::CExitWait(CAknAppUi& aWait) : CActive(CActive::EPriorityStandard), iWait(aWait)
260 CActiveScheduler::Add(this);
261 SetActive();
262 iStatusPtr = &iStatus;
265 CExitWait::~CExitWait()
267 Cancel();
270 void CExitWait::RunL()
272 if(iStatusPtr != NULL)
273 iWait.Exit();
276 void CExitWait::DoCancel()
278 if(iStatusPtr != NULL)
279 User::RequestComplete(iStatusPtr, KErrCancel);
282 //////////////////////////////////////////////////////////////////////////////////////////////
284 CSDLDocument::CSDLDocument(CEikApplication& aApp) : CEikDocument(aApp)
287 CEikAppUi* CSDLDocument::CreateAppUiL()
289 return new (ELeave) CSDLAppUi;
292 ///////////////////////////////////////////////////////////////////////////
294 void CSDLWin::ConstructL(const TRect& aRect)
296 CreateWindowL();
297 SetRect(aRect);
298 ActivateL();
301 RWindow& CSDLWin::GetWindow() const
303 return Window();
306 void CSDLWin::Draw(const TRect& /*aRect*/) const
308 if(m_draw)
310 CWindowGc& gc = SystemGc();
311 gc.SetPenStyle(CGraphicsContext::ESolidPen);
312 gc.SetPenColor(KRgbGray);
313 gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
314 gc.SetBrushColor(0x000000);
315 gc.DrawRect(Rect());
319 /////////////////////////////////////////////////////////////////////////
321 CSDLAppUi::~CSDLAppUi()
323 if(iStarter)
324 iStarter->Cancel();
325 delete iStarter;
326 delete iWait;
327 delete iSdl;
328 delete iSDLWin;
329 delete iParams;
332 void CSDLAppUi::ConstructL()
334 BaseConstructL(ENoAppResourceFile | ENoScreenFurniture);
336 RFs fs;
337 fs.Connect();
338 TFileName name;
339 fs.PrivatePath(name);
340 fs.Close();
341 name.Append( _L("sdlexe.rsc") );
342 name.Insert( 0, _L("C:") );
343 iResOffset = iCoeEnv->AddResourceFileL(name);
345 iSDLWin = new (ELeave) CSDLWin;
346 iSDLWin->ConstructL(ApplicationRect());
348 iSdl = CSDL::NewL(gSDLClass.SdlFlags());
350 gSDLClass.SendEvent(MSDLMainObs::ESDLCreated, 0, iSdl);
352 SetKeyBlockMode(ENoKeyBlock);
354 iSdl->SetContainerWindowL(iSDLWin->GetWindow(), iEikonEnv->WsSession(), *iEikonEnv->ScreenDevice());
356 iStarter = CIdle::NewL(CActive::EPriorityLow);
357 iStarter->Start(TCallBack(StartL, this));
360 TBool CSDLAppUi::StartL(TAny* aThis)
362 static_cast<CSDLAppUi*>(aThis)->StartL();
363 return EFalse;
366 TBool CSDLAppUi::ProcessCommandParametersL(CApaCommandLine &aCommandLine)
368 const TPtrC8 cmdLine = aCommandLine.TailEnd();
369 iParams = new (ELeave) CDesC8ArrayFlat(8);
370 MakeCCmdLineL(cmdLine, *iParams);
371 return EFalse;
374 TBool CSDLAppUi::ParamEditorL(TDes& aCheat)
376 CAknTextQueryDialog* query = CAknTextQueryDialog::NewL(aCheat);
377 CleanupStack::PushL(query);
378 query->SetPromptL(_L("Enter parameters"));
379 CleanupStack::Pop();
380 return query->ExecuteLD(R_PARAMEDITOR);
383 void CSDLAppUi::StartL()
385 if(gSDLClass.AppFlags() & SDLEnv::EParamQuery)
387 iSDLWin->SetDraw(true);
389 TBuf8<256> cmd;
390 RFile file;
391 int err = file.Open(iEikonEnv->FsSession(), _L("sdl_param.txt"),EFileRead);
392 if(err == KErrNone)
394 file.Read(cmd);
395 file.Close();
396 MakeCCmdLineL(cmd, *iParams);
398 if(err != KErrNone || gSDLClass.AppFlags() & (SDLEnv::EParamQueryDialog ^ SDLEnv::EParamQuery))
400 TBuf<256> buffer;
401 if(ParamEditorL(buffer))
403 cmd.Copy(buffer);
404 MakeCCmdLineL(cmd, *iParams);
408 iSDLWin->SetDraw(false);
410 iCoeEnv->DeleteResourceFile(iResOffset);
412 iWait = new (ELeave) CExitWait(*this);
413 iSdl->CallMainL(gSDLClass.Main(), &iWait->iStatus, iParams, 0xA000);
416 void CSDLAppUi::HandleCommandL(int aCommand)
418 switch(aCommand)
420 case EAknSoftkeyBack:
421 case EAknSoftkeyExit:
422 case EAknCmdExit:
423 case EEikCmdExit:
424 if(iWait == NULL || !iWait->IsActive() || iSdl == NULL)
426 Exit();
428 else
430 TWsEvent event;
431 event.SetType(EEventSwitchOff), event.SetTimeNow();
432 iSdl->AppendWsEvent(event);
434 break;
436 default:
437 break;
441 void CSDLAppUi::HandleWsEventL(const TWsEvent& aEvent, CCoeControl* aDestination)
443 if(aEvent.Type() == KAknUidValueEndKeyCloseEvent)
445 if(iWait != NULL && iWait->IsActive() && iSdl != NULL)
447 TWsEvent event;
448 event.SetType(EEventSwitchOff), event.SetTimeNow();
449 iSdl->AppendWsEvent(event);
450 return;
454 if(iSdl && iWait)
455 iSdl->AppendWsEvent(aEvent);
456 CAknAppUi::HandleWsEventL(aEvent, aDestination);
459 void CSDLAppUi::HandleResourceChangeL(int aType)
461 CAknAppUi::HandleResourceChangeL(aType);
462 if(aType == KEikDynamicLayoutVariantSwitch)
464 iSDLWin->SetRect(ApplicationRect());
465 iSdl->SetContainerWindowL(iSDLWin->GetWindow(), iEikonEnv->WsSession(), *iEikonEnv->ScreenDevice());
466 iSdl->Resize();
470 void CSDLAppUi::HandleForegroundEventL(TBool aForeground)
472 CAknAppUi::HandleForegroundEventL(aForeground);
475 ////////////////////////////////////////////////////////////////////////
477 CApaApplication* NewApplication()
479 return new CSDLApplication();
482 EXPORT_C int SDLEnv::SetMain(const TMainFunc& aFunc, int aSdlFlags, MSDLMainObs* aObs, int aSdlExeFlags)
484 gSDLClass.SetMain(aFunc, aSdlFlags, aObs, aSdlExeFlags);
485 return EikStart::RunApplication(NewApplication);