Cosmetics.
[SDL.s60v3.git] / src / main / symbian / sdlexe.cpp
blob5fbd5b207778ac12421a4d08840c5f3a1bc3e3f9
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() : iExeFlags(0) {}
70 void SetMain(const TMainFunc& aFunc, int aExeFlags);
71 const TMainFunc& Main() const { return iFunc; }
72 int AppFlags() const { return iExeFlags; }
73 void AppFlags(int aFlags) { iExeFlags |= aFlags; }
75 private:
76 TMainFunc iFunc;
77 int iExeFlags;
80 void TSdlClass::SetMain(const TMainFunc& aFunc, int aExeFlags)
82 iFunc = aFunc;
83 iExeFlags = aExeFlags;
86 TSdlClass gSDLClass;
88 ////////////////////////////////////////////////////////////////
90 class CExitWait : public CActive
92 public:
93 CExitWait(CAknAppUi& aWait);
94 ~CExitWait() { Cancel(); }
96 private:
97 void RunL() { if(iStatusPtr != NULL) iWait.Exit(); }
98 void DoCancel() { if(iStatusPtr != NULL) User::RequestComplete(iStatusPtr, KErrCancel); }
100 CAknAppUi& iWait;
101 TRequestStatus* iStatusPtr;
104 CExitWait::CExitWait(CAknAppUi& aWait) : CActive(CActive::EPriorityStandard), iWait(aWait)
106 CActiveScheduler::Add(this);
107 SetActive();
108 iStatusPtr = &iStatus;
111 ////////////////////////////////////////////////////////////////////////
113 class CSDLWin : public CCoeControl
115 public:
116 CSDLWin( const TRect& aRect );
117 RWindow& GetWindow() const { return Window(); }
118 void SetDraw(bool draw) { m_draw = draw; }
120 private:
121 void Draw(const TRect& aRect) const;
123 bool m_draw;
126 CSDLWin::CSDLWin( const TRect& aRect )
127 : m_draw(false)
129 CreateWindowL();
130 SetRect(aRect);
131 ActivateL();
135 void CSDLWin::Draw(const TRect& /*aRect*/) const
137 if(m_draw)
139 CWindowGc& gc = SystemGc();
140 gc.SetPenStyle(CGraphicsContext::ESolidPen);
141 gc.SetPenColor(KRgbGray);
142 gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
143 gc.SetBrushColor(0x000000);
144 gc.DrawRect(Rect());
148 ////////////////////////////////////////////////////////////////////////////
150 class CSDLAppUi : public CAknAppUi
152 public:
153 ~CSDLAppUi();
155 private:
156 void ConstructL();
157 void HandleCommandL(int aCommand);
158 void HandleWsEventL(const TWsEvent& aEvent, CCoeControl* aDestination);
159 void HandleResourceChangeL(int aType);
161 void StartL();
162 static TBool StartL(TAny* aThis);
164 TBool ParamEditorL(TDes& aCheat);
166 TBool ProcessCommandParametersL(CApaCommandLine &aCommandLine);
168 void HandleForegroundEventL(TBool aForeground);
170 CExitWait* iWait;
171 CSDLWin* iSDLWin;
172 CSDL* iSdl;
173 CIdle* iStarter;
174 CDesC8Array* iParams;
175 int iResOffset;
178 ////////////////////////////////////////////////////////////////////////////
180 class CSDLDocument : public CEikDocument
182 public:
183 CSDLDocument(CEikApplication& aApp) : CEikDocument(aApp) {}
185 private:
186 CEikAppUi* CreateAppUiL() { return new CSDLAppUi; }
189 ////////////////////////////////////////////////////////////////////////////
191 class CSDLApplication : public CAknApplication
193 public:
194 CSDLApplication();
196 private:
197 CApaDocument* CreateDocumentL() { return new CSDLDocument(*this); }
198 TUid AppDllUid() const { return iUid; }
200 TUid iUid;
203 ////////////////////////////////////////////////////////////////////////////////////////
205 CSDLApplication::CSDLApplication()
207 RApaLsSession apa;
208 User::LeaveIfError(apa.Connect());
209 CleanupClosePushL(apa);
210 User::LeaveIfError(apa.GetAllApps());
211 TFileName name = RProcess().FileName();
212 TApaAppInfo info;
213 while(apa.GetNextApp(info) == KErrNone)
215 if(info.iFullName.CompareF(name) == 0)
217 iUid = info.iUid;
218 break;
221 CleanupStack::PopAndDestroy();
224 /////////////////////////////////////////////////////////////////////////
226 CSDLAppUi::~CSDLAppUi()
228 if(iStarter)
229 iStarter->Cancel();
230 delete iStarter;
231 delete iWait;
232 delete iSdl;
233 delete iSDLWin;
234 delete iParams;
237 void CSDLAppUi::ConstructL()
239 BaseConstructL(ENoAppResourceFile|EAknEnableSkin);
241 RFs fs;
242 fs.Connect();
243 TFileName name;
244 fs.PrivatePath(name);
245 fs.Close();
246 name.Append( _L("sdlexe.rsc") );
247 name.Insert( 0, _L("C:") );
248 iResOffset = iCoeEnv->AddResourceFileL(name);
250 iSDLWin = new CSDLWin(ApplicationRect());
252 iSdl = CSDL::NewL();
254 SetKeyBlockMode(ENoKeyBlock);
256 iSdl->SetContainerWindowL(iSDLWin->GetWindow(), iEikonEnv->WsSession(), *iEikonEnv->ScreenDevice());
258 iStarter = CIdle::NewL(CActive::EPriorityLow);
259 iStarter->Start(TCallBack(StartL, this));
262 TBool CSDLAppUi::StartL(TAny* aThis)
264 static_cast<CSDLAppUi*>(aThis)->StartL();
265 return EFalse;
268 TBool CSDLAppUi::ProcessCommandParametersL(CApaCommandLine &aCommandLine)
270 const TPtrC8 cmdLine = aCommandLine.TailEnd();
271 iParams = new CDesC8ArrayFlat(8);
272 MakeCCmdLineL(cmdLine, *iParams);
273 return EFalse;
276 TBool CSDLAppUi::ParamEditorL(TDes& aCheat)
278 CAknTextQueryDialog* query = CAknTextQueryDialog::NewL(aCheat);
279 CleanupStack::PushL(query);
280 query->SetPromptL(_L("Enter parameters"));
281 CleanupStack::Pop();
282 return query->ExecuteLD(R_PARAMEDITOR);
285 void CSDLAppUi::StartL()
287 if(gSDLClass.AppFlags() & SDLEnv::EParamQuery)
289 iSDLWin->SetDraw(true);
291 TBuf8<256> cmd;
292 RFile file;
293 int err = file.Open(iEikonEnv->FsSession(), _L("sdl_param.txt"),EFileRead);
294 if(err == KErrNone)
296 file.Read(cmd);
297 file.Close();
298 MakeCCmdLineL(cmd, *iParams);
300 if(err != KErrNone || gSDLClass.AppFlags() & (SDLEnv::EParamQueryDialog ^ SDLEnv::EParamQuery))
302 TBuf<256> buffer;
303 if(ParamEditorL(buffer))
305 cmd.Copy(buffer);
306 MakeCCmdLineL(cmd, *iParams);
310 iSDLWin->SetDraw(false);
312 iCoeEnv->DeleteResourceFile(iResOffset);
314 iWait = new CExitWait(*this);
315 iSdl->CallMainL(gSDLClass.Main(), &iWait->iStatus, iParams);
318 void CSDLAppUi::HandleCommandL(int aCommand)
320 switch(aCommand)
322 case EAknSoftkeyBack:
323 case EAknSoftkeyExit:
324 case EAknCmdExit:
325 case EEikCmdExit:
326 if(iWait == NULL || !iWait->IsActive() || iSdl == NULL)
328 Exit();
330 else
332 TWsEvent event;
333 event.SetType(EEventSwitchOff), event.SetTimeNow();
334 iSdl->AppendWsEvent(event);
336 break;
338 default:
339 break;
343 void CSDLAppUi::HandleWsEventL(const TWsEvent& aEvent, CCoeControl* aDestination)
345 if(aEvent.Type() == KAknUidValueEndKeyCloseEvent)
347 if(iWait != NULL && iWait->IsActive() && iSdl != NULL)
349 TWsEvent event;
350 event.SetType(EEventSwitchOff), event.SetTimeNow();
351 iSdl->AppendWsEvent(event);
352 return;
356 if(iSdl && iWait)
357 iSdl->AppendWsEvent(aEvent);
358 CAknAppUi::HandleWsEventL(aEvent, aDestination);
361 void CSDLAppUi::HandleResourceChangeL(int aType)
363 CAknAppUi::HandleResourceChangeL(aType);
364 if(aType == KEikDynamicLayoutVariantSwitch)
366 iSDLWin->SetRect(ApplicationRect());
367 iSdl->SetContainerWindowL(iSDLWin->GetWindow(), iEikonEnv->WsSession(), *iEikonEnv->ScreenDevice());
368 iSdl->Resize();
372 void CSDLAppUi::HandleForegroundEventL(TBool aForeground)
374 CAknAppUi::HandleForegroundEventL(aForeground);
377 ////////////////////////////////////////////////////////////////////////
379 CApaApplication* NewApplication()
381 return new CSDLApplication();
384 int SDLEnv::SetMain(const TMainFunc& aFunc, int aSdlExeFlags)
386 gSDLClass.SetMain(aFunc, aSdlExeFlags);
387 return EikStart::RunApplication(NewApplication);