4 #include <sdlepocapi.h>
14 #include <aknglobalmsgquery.h>
19 LOCAL_C
void MakeCCmdLineL(const TDesC8
& aParam
, CDesC8Array
& aArray
)
26 lex
.SkipSpaceAndMark();
33 const TPtrC8 rem
= lex
.RemainderFromMark();
34 const int pos
= rem
.Locate(dq
);
38 ptr
.Set(lex
.MarkedToken());
49 ptr
.Set(lex
.NextToken());
50 const int pos
= ptr
.Locate(dq
);
56 continue; // back to in brace
59 lex
.SkipSpaceAndMark();
66 NONSHARABLE_CLASS(TSdlClass
)
70 void SetMain(const TMainFunc
& aFunc
, int aFlags
, MSDLMainObs
* aObs
, int aExeFlags
);
72 const TMainFunc
& Main() const;
73 void SendEvent(int aEvent
, int aParam
, CSDL
* aSDL
);
75 void AppFlags(int aFlags
);
85 void TSdlClass::AppFlags(int aFlags
)
90 void TSdlClass::SendEvent(int aEvent
, int aParam
, CSDL
* aSDL
)
93 iObs
->SDLMainEvent(aEvent
, aParam
, aSDL
);
96 int TSdlClass::AppFlags() const
101 void TSdlClass::SetMain(const TMainFunc
& aFunc
, int aFlags
, MSDLMainObs
* aObs
, int aExeFlags
)
105 iExeFlags
= aExeFlags
;
109 const TMainFunc
& TSdlClass::Main() const
115 int TSdlClass::SdlFlags() const
120 TSdlClass::TSdlClass()
122 Mem::FillZ(this, sizeof(this));
127 ////////////////////////////////////////////////////////////////
129 NONSHARABLE_CLASS(CSDLApplication
) : public CAknApplication
135 CApaDocument
* CreateDocumentL();
136 TFileName
ResourceFileName() const;
137 TUid
AppDllUid() const;
143 NONSHARABLE_CLASS(CSDLDocument
) : public CEikDocument
146 CSDLDocument(CEikApplication
& aApp
);
149 CEikAppUi
* CreateAppUiL();
152 ////////////////////////////////////////////////////////////////////
154 NONSHARABLE_CLASS(MExitWait
)
157 virtual void DoExit(int aErr
) = 0;
160 /////////////////////////////////////////////////////////////////////////
162 NONSHARABLE_CLASS(CExitWait
) : public CActive
165 CExitWait(MExitWait
& aWait
);
173 TRequestStatus
* iStatusPtr
;
176 ////////////////////////////////////////////////////////////////////////
178 NONSHARABLE_CLASS(CSDLWin
) : public CCoeControl
181 void ConstructL(const TRect
& aRect
);
182 RWindow
& GetWindow() const;
185 void Draw(const TRect
& aRect
) const;
188 ////////////////////////////////////////////////////////////////////////////
190 NONSHARABLE_CLASS(CSDLAppUi
) : public CAknAppUi
, public MExitWait
, public MSDLObserver
197 void HandleCommandL(int aCommand
);
198 void HandleWsEventL(const TWsEvent
& aEvent
, CCoeControl
* aDestination
);
199 void HandleResourceChangeL(int aType
);
201 void DoExit(int aErr
);
203 int SdlEvent(int aEvent
, int aParam
);
204 int SdlThreadEvent(int aEvent
, int aParam
);
207 static TBool
StartL(TAny
* aThis
);
209 TBool
ParamEditorL(TDes
& aCheat
);
211 TBool
ProcessCommandParametersL(CApaCommandLine
&aCommandLine
);
213 void PrepareToExit();
214 void HandleConsoleWindowL();
215 void HandleConsoleWindow();
216 void HandleForegroundEventL(TBool aForeground
);
218 static TBool
IdleRequestL(TAny
* aThis
);
224 CDesC8Array
* iParams
;
230 ////////////////////////////////////////////////////////////////////////////////////////
232 CApaDocument
* CSDLApplication::CreateDocumentL()
234 return new (ELeave
) CSDLDocument(*this);
237 TUid
CSDLApplication::AppDllUid() const
242 CSDLApplication::CSDLApplication()
244 TRAPD(err
, FindMeL());
245 ASSERT(err
== KErrNone
);
248 void CSDLApplication::FindMeL()
251 User::LeaveIfError(apa
.Connect());
252 CleanupClosePushL(apa
);
253 User::LeaveIfError(apa
.GetAllApps());
254 TFileName name
= RProcess().FileName();
256 while(apa
.GetNextApp(info
) == KErrNone
)
258 if(info
.iFullName
.CompareF(name
) == 0)
264 CleanupStack::PopAndDestroy();
267 TFileName
CSDLApplication::ResourceFileName() const
272 ///////////////////////////////////////////////////////////////////////////////////////////
274 CExitWait::CExitWait(MExitWait
& aWait
) : CActive(CActive::EPriorityStandard
), iWait(aWait
)
276 CActiveScheduler::Add(this);
278 iStatusPtr
= &iStatus
;
281 CExitWait::~CExitWait()
286 void CExitWait::RunL()
288 if(iStatusPtr
!= NULL
)
289 iWait
.DoExit(iStatus
.Int());
292 void CExitWait::DoCancel()
294 if(iStatusPtr
!= NULL
)
295 User::RequestComplete(iStatusPtr
, KErrCancel
);
298 //////////////////////////////////////////////////////////////////////////////////////////////
300 CSDLDocument::CSDLDocument(CEikApplication
& aApp
) : CEikDocument(aApp
)
303 CEikAppUi
* CSDLDocument::CreateAppUiL()
305 return new (ELeave
) CSDLAppUi
;
308 ///////////////////////////////////////////////////////////////////////////
310 void CSDLWin::ConstructL(const TRect
& aRect
)
317 RWindow
& CSDLWin::GetWindow() const
322 void CSDLWin::Draw(const TRect
& /*aRect*/) const
324 CWindowGc
& gc
= SystemGc();
325 gc
.SetPenStyle(CGraphicsContext::ESolidPen
);
326 gc
.SetPenColor(KRgbGray
);
327 gc
.SetBrushStyle(CGraphicsContext::ESolidBrush
);
328 gc
.SetBrushColor(0x000000);
332 /////////////////////////////////////////////////////////////////////////
334 CSDLAppUi::~CSDLAppUi()
348 void CSDLAppUi::ConstructL()
350 BaseConstructL(ENoAppResourceFile
| ENoScreenFurniture
);
355 fs
.PrivatePath(name
);
357 name
.Append( _L("sdlexe.rsc") );
358 name
.Insert( 0, _L("C:") );
359 iResOffset
= iCoeEnv
->AddResourceFileL(name
);
361 iIdle
= CIdle::NewL(CActive::EPriorityIdle
);
363 iSDLWin
= new (ELeave
) CSDLWin
;
364 iSDLWin
->ConstructL(ApplicationRect());
366 iSdl
= CSDL::NewL(gSDLClass
.SdlFlags());
368 gSDLClass
.SendEvent(MSDLMainObs::ESDLCreated
, 0, iSdl
);
370 iSdl
->SetObserver(this);
371 iSdl
->DisableKeyBlocking(*this);
372 iSdl
->SetContainerWindowL(
373 iSDLWin
->GetWindow(),
374 iEikonEnv
->WsSession(),
375 *iEikonEnv
->ScreenDevice());
377 iStarter
= CIdle::NewL(CActive::EPriorityLow
);
378 iStarter
->Start(TCallBack(StartL
, this));
381 TBool
CSDLAppUi::StartL(TAny
* aThis
)
383 static_cast<CSDLAppUi
*>(aThis
)->StartL();
387 void CSDLAppUi::PrepareToExit()
389 CAknAppUiBase::PrepareToExit(); //aknappu::PrepareToExit crashes
390 iCoeEnv
->DeleteResourceFile(iResOffset
);
393 TBool
CSDLAppUi::ProcessCommandParametersL(CApaCommandLine
&aCommandLine
)
395 const TPtrC8 cmdLine
= aCommandLine
.TailEnd();
396 iParams
= new (ELeave
) CDesC8ArrayFlat(8);
397 MakeCCmdLineL(cmdLine
, *iParams
);
401 TBool
CSDLAppUi::ParamEditorL(TDes
& aCheat
)
403 CAknTextQueryDialog
* query
= CAknTextQueryDialog::NewL(aCheat
);
404 CleanupStack::PushL(query
);
405 query
->SetPromptL(_L("Enter parameters"));
407 return query
->ExecuteLD(R_PARAMEDITOR
);
410 void CSDLAppUi::StartL()
412 if(gSDLClass
.AppFlags() & SDLEnv::EParamQuery
)
416 int err
= file
.Open(iEikonEnv
->FsSession(), _L("sdl_param.txt"),EFileRead
);
421 MakeCCmdLineL(cmd
, *iParams
);
423 if(err
!= KErrNone
|| gSDLClass
.AppFlags() & (SDLEnv::EParamQueryDialog
^ SDLEnv::EParamQuery
))
426 if(ParamEditorL(buffer
))
429 MakeCCmdLineL(cmd
, *iParams
);
433 iWait
= new (ELeave
) CExitWait(*this);
434 iSdl
->CallMainL(gSDLClass
.Main(), &iWait
->iStatus
, iParams
, CSDL::ENoParamFlags
, 0xA000);
437 void CSDLAppUi::HandleCommandL(int aCommand
)
441 case EAknSoftkeyBack
:
442 case EAknSoftkeyExit
:
445 gSDLClass
.AppFlags(SDLEnv::EAllowConsoleView
);
446 if(iWait
== NULL
|| !iWait
->IsActive() || iSdl
== NULL
)
453 event
.SetType(EEventSwitchOff
), event
.SetTimeNow();
454 iSdl
->AppendWsEvent(event
);
460 void CSDLAppUi::HandleWsEventL(const TWsEvent
& aEvent
, CCoeControl
* aDestination
)
463 iSdl
->AppendWsEvent(aEvent
);
464 CAknAppUi::HandleWsEventL(aEvent
, aDestination
);
467 void CSDLAppUi::HandleResourceChangeL(int aType
)
469 CAknAppUi::HandleResourceChangeL(aType
);
470 if(aType
== KEikDynamicLayoutVariantSwitch
)
472 iSDLWin
->SetRect(ApplicationRect());
473 iSdl
->SetContainerWindowL(
474 iSDLWin
->GetWindow(),
475 iEikonEnv
->WsSession(),
476 *iEikonEnv
->ScreenDevice());
480 void CSDLAppUi::DoExit(int/*Err*/)
485 int CSDLAppUi::SdlThreadEvent(int aEvent
, int /*aParam*/)
489 case MSDLObserver::EEventResume
:
491 case MSDLObserver::EEventSuspend
:
493 case MSDLObserver::EEventWindowReserved
:
495 case MSDLObserver::EEventWindowNotAvailable
:
497 case MSDLObserver::EEventScreenSizeChanged
:
500 return MSDLObserver::EParameterNone
;
503 int CSDLAppUi::SdlEvent(int aEvent
, int /*aParam*/)
507 case MSDLObserver::EEventResume
:
509 case MSDLObserver::EEventSuspend
:
511 case MSDLObserver::EEventWindowReserved
:
513 case MSDLObserver::EEventWindowNotAvailable
:
515 TRAP_IGNORE(HandleConsoleWindowL());
518 case MSDLObserver::EEventScreenSizeChanged
:
520 case MSDLObserver::EEventKeyMapInit
:
522 case MSDLObserver::EEventMainExit
:
525 gSDLClass
.AppFlags(SDLEnv::EAllowConsoleView
);
526 iEikonEnv
->WsSession().SetWindowGroupOrdinalPosition(iStdOut
, 0);
530 return MSDLObserver::EParameterNone
;
533 void CSDLAppUi::HandleForegroundEventL(TBool aForeground
)
535 CAknAppUi::HandleForegroundEventL(aForeground
);
537 HandleConsoleWindow();
540 void CSDLAppUi::HandleConsoleWindow()
542 if(!iIdle
->IsActive())
543 iIdle
->Start(TCallBack(IdleRequestL
, this));
546 TBool
CSDLAppUi::IdleRequestL(TAny
* aThis
)
548 static_cast<CSDLAppUi
*>(aThis
)->HandleConsoleWindowL();
552 void CSDLAppUi::HandleConsoleWindowL()
554 if(gSDLClass
.AppFlags() & SDLEnv::EAllowConsoleView
)
558 RWsSession
& ses
= iEikonEnv
->WsSession();
559 const int focus
= ses
.GetFocusWindowGroup();
560 CApaWindowGroupName
* name
= CApaWindowGroupName::NewLC(ses
, focus
);
561 const TPtrC caption
= name
->Caption();
562 if(0 == caption
.CompareF(_L("STDOUT")))
565 ses
.SetWindowGroupOrdinalPosition(iEikonEnv
->RootWin().Identifier(), 0);
567 CleanupStack::PopAndDestroy(); //name
570 ////////////////////////////////////////////////////////////////////////
572 CApaApplication
* NewApplication()
574 return new CSDLApplication();
577 EXPORT_C
int SDLEnv::SetMain(const TMainFunc
& aFunc
, int aSdlFlags
, MSDLMainObs
* aObs
, int aSdlExeFlags
)
579 gSDLClass
.SetMain(aFunc
, aSdlFlags
, aObs
, aSdlExeFlags
);
580 return EikStart::RunApplication(NewApplication
);