Remove LOCAL_C abomination.
[SDL.s60v3.git] / src / main / symbian / sdlexe.cpp
bloba8a9bc7511152c556eb52bdcce302c0aaac1196a
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 static 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 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 class CSDLApplication : public CAknApplication
131 public:
132 CSDLApplication();
134 private:
135 CApaDocument* CreateDocumentL() { return new (ELeave) CSDLDocument(*this); }
136 TUid AppDllUid() const { return iUid; }
138 TUid iUid;
141 class CSDLDocument : public CEikDocument
143 public:
144 CSDLDocument(CEikApplication& aApp);
146 private:
147 CEikAppUi* CreateAppUiL();
150 ////////////////////////////////////////////////////////////////////
152 class CExitWait : public CActive
154 public:
155 CExitWait(CAknAppUi& aWait);
156 ~CExitWait();
158 private:
159 void RunL();
160 void DoCancel();
162 CAknAppUi& iWait;
163 TRequestStatus* iStatusPtr;
166 ////////////////////////////////////////////////////////////////////////
168 class CSDLWin : public CCoeControl
170 public:
171 CSDLWin() : m_draw(false) {}
172 void ConstructL(const TRect& aRect);
173 RWindow& GetWindow() const;
174 void SetDraw(bool draw) { m_draw = draw; }
176 private:
177 void Draw(const TRect& aRect) const;
179 bool m_draw;
182 ////////////////////////////////////////////////////////////////////////////
184 class CSDLAppUi : public CAknAppUi
186 public:
187 ~CSDLAppUi();
189 private:
190 void ConstructL();
191 void HandleCommandL(int aCommand);
192 void HandleWsEventL(const TWsEvent& aEvent, CCoeControl* aDestination);
193 void HandleResourceChangeL(int aType);
195 void StartL();
196 static TBool StartL(TAny* aThis);
198 TBool ParamEditorL(TDes& aCheat);
200 TBool ProcessCommandParametersL(CApaCommandLine &aCommandLine);
202 void HandleForegroundEventL(TBool aForeground);
204 CExitWait* iWait;
205 CSDLWin* iSDLWin;
206 CSDL* iSdl;
207 CIdle* iStarter;
208 CDesC8Array* iParams;
209 int iResOffset;
212 ////////////////////////////////////////////////////////////////////////////////////////
214 CSDLApplication::CSDLApplication()
216 RApaLsSession apa;
217 User::LeaveIfError(apa.Connect());
218 CleanupClosePushL(apa);
219 User::LeaveIfError(apa.GetAllApps());
220 TFileName name = RProcess().FileName();
221 TApaAppInfo info;
222 while(apa.GetNextApp(info) == KErrNone)
224 if(info.iFullName.CompareF(name) == 0)
226 iUid = info.iUid;
227 break;
230 CleanupStack::PopAndDestroy();
233 ///////////////////////////////////////////////////////////////////////////////////////////
235 CExitWait::CExitWait(CAknAppUi& aWait) : CActive(CActive::EPriorityStandard), iWait(aWait)
237 CActiveScheduler::Add(this);
238 SetActive();
239 iStatusPtr = &iStatus;
242 CExitWait::~CExitWait()
244 Cancel();
247 void CExitWait::RunL()
249 if(iStatusPtr != NULL)
250 iWait.Exit();
253 void CExitWait::DoCancel()
255 if(iStatusPtr != NULL)
256 User::RequestComplete(iStatusPtr, KErrCancel);
259 //////////////////////////////////////////////////////////////////////////////////////////////
261 CSDLDocument::CSDLDocument(CEikApplication& aApp) : CEikDocument(aApp)
264 CEikAppUi* CSDLDocument::CreateAppUiL()
266 return new (ELeave) CSDLAppUi;
269 ///////////////////////////////////////////////////////////////////////////
271 void CSDLWin::ConstructL(const TRect& aRect)
273 CreateWindowL();
274 SetRect(aRect);
275 ActivateL();
278 RWindow& CSDLWin::GetWindow() const
280 return Window();
283 void CSDLWin::Draw(const TRect& /*aRect*/) const
285 if(m_draw)
287 CWindowGc& gc = SystemGc();
288 gc.SetPenStyle(CGraphicsContext::ESolidPen);
289 gc.SetPenColor(KRgbGray);
290 gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
291 gc.SetBrushColor(0x000000);
292 gc.DrawRect(Rect());
296 /////////////////////////////////////////////////////////////////////////
298 CSDLAppUi::~CSDLAppUi()
300 if(iStarter)
301 iStarter->Cancel();
302 delete iStarter;
303 delete iWait;
304 delete iSdl;
305 delete iSDLWin;
306 delete iParams;
309 void CSDLAppUi::ConstructL()
311 BaseConstructL(ENoAppResourceFile|EAknEnableSkin);
313 RFs fs;
314 fs.Connect();
315 TFileName name;
316 fs.PrivatePath(name);
317 fs.Close();
318 name.Append( _L("sdlexe.rsc") );
319 name.Insert( 0, _L("C:") );
320 iResOffset = iCoeEnv->AddResourceFileL(name);
322 iSDLWin = new (ELeave) CSDLWin;
323 iSDLWin->ConstructL(ApplicationRect());
325 iSdl = CSDL::NewL(gSDLClass.SdlFlags());
327 gSDLClass.SendEvent(MSDLMainObs::ESDLCreated, 0, iSdl);
329 SetKeyBlockMode(ENoKeyBlock);
331 iSdl->SetContainerWindowL(iSDLWin->GetWindow(), iEikonEnv->WsSession(), *iEikonEnv->ScreenDevice());
333 iStarter = CIdle::NewL(CActive::EPriorityLow);
334 iStarter->Start(TCallBack(StartL, this));
337 TBool CSDLAppUi::StartL(TAny* aThis)
339 static_cast<CSDLAppUi*>(aThis)->StartL();
340 return EFalse;
343 TBool CSDLAppUi::ProcessCommandParametersL(CApaCommandLine &aCommandLine)
345 const TPtrC8 cmdLine = aCommandLine.TailEnd();
346 iParams = new (ELeave) CDesC8ArrayFlat(8);
347 MakeCCmdLineL(cmdLine, *iParams);
348 return EFalse;
351 TBool CSDLAppUi::ParamEditorL(TDes& aCheat)
353 CAknTextQueryDialog* query = CAknTextQueryDialog::NewL(aCheat);
354 CleanupStack::PushL(query);
355 query->SetPromptL(_L("Enter parameters"));
356 CleanupStack::Pop();
357 return query->ExecuteLD(R_PARAMEDITOR);
360 void CSDLAppUi::StartL()
362 if(gSDLClass.AppFlags() & SDLEnv::EParamQuery)
364 iSDLWin->SetDraw(true);
366 TBuf8<256> cmd;
367 RFile file;
368 int err = file.Open(iEikonEnv->FsSession(), _L("sdl_param.txt"),EFileRead);
369 if(err == KErrNone)
371 file.Read(cmd);
372 file.Close();
373 MakeCCmdLineL(cmd, *iParams);
375 if(err != KErrNone || gSDLClass.AppFlags() & (SDLEnv::EParamQueryDialog ^ SDLEnv::EParamQuery))
377 TBuf<256> buffer;
378 if(ParamEditorL(buffer))
380 cmd.Copy(buffer);
381 MakeCCmdLineL(cmd, *iParams);
385 iSDLWin->SetDraw(false);
387 iCoeEnv->DeleteResourceFile(iResOffset);
389 iWait = new (ELeave) CExitWait(*this);
390 iSdl->CallMainL(gSDLClass.Main(), &iWait->iStatus, iParams, 0xA000);
393 void CSDLAppUi::HandleCommandL(int aCommand)
395 switch(aCommand)
397 case EAknSoftkeyBack:
398 case EAknSoftkeyExit:
399 case EAknCmdExit:
400 case EEikCmdExit:
401 if(iWait == NULL || !iWait->IsActive() || iSdl == NULL)
403 Exit();
405 else
407 TWsEvent event;
408 event.SetType(EEventSwitchOff), event.SetTimeNow();
409 iSdl->AppendWsEvent(event);
411 break;
413 default:
414 break;
418 void CSDLAppUi::HandleWsEventL(const TWsEvent& aEvent, CCoeControl* aDestination)
420 if(aEvent.Type() == KAknUidValueEndKeyCloseEvent)
422 if(iWait != NULL && iWait->IsActive() && iSdl != NULL)
424 TWsEvent event;
425 event.SetType(EEventSwitchOff), event.SetTimeNow();
426 iSdl->AppendWsEvent(event);
427 return;
431 if(iSdl && iWait)
432 iSdl->AppendWsEvent(aEvent);
433 CAknAppUi::HandleWsEventL(aEvent, aDestination);
436 void CSDLAppUi::HandleResourceChangeL(int aType)
438 CAknAppUi::HandleResourceChangeL(aType);
439 if(aType == KEikDynamicLayoutVariantSwitch)
441 iSDLWin->SetRect(ApplicationRect());
442 iSdl->SetContainerWindowL(iSDLWin->GetWindow(), iEikonEnv->WsSession(), *iEikonEnv->ScreenDevice());
443 iSdl->Resize();
447 void CSDLAppUi::HandleForegroundEventL(TBool aForeground)
449 CAknAppUi::HandleForegroundEventL(aForeground);
452 ////////////////////////////////////////////////////////////////////////
454 CApaApplication* NewApplication()
456 return new CSDLApplication();
459 int SDLEnv::SetMain(const TMainFunc& aFunc, int aSdlFlags, MSDLMainObs* aObs, int aSdlExeFlags)
461 gSDLClass.SetMain(aFunc, aSdlFlags, aObs, aSdlExeFlags);
462 return EikStart::RunApplication(NewApplication);