5 #include <sdlepocapi.h>
15 #include <aknglobalmsgquery.h>
21 // FORWARD DECLARATIONS
25 //const TUid KSDLUID = { 0xF01F605E };
27 LOCAL_C
void MakeCCmdLineL(const TDesC8
& aParam
, CDesC8Array
& aArray
)
35 lex
.SkipSpaceAndMark();
42 const TPtrC8 rem
= lex
.RemainderFromMark();
43 const TInt pos
= rem
.Locate(dq
);
47 ptr
.Set(lex
.MarkedToken());
58 ptr
.Set(lex
.NextToken());
59 const TInt pos
= ptr
.Locate(dq
);
65 continue; // back to in brace
68 lex
.SkipSpaceAndMark();
76 NONSHARABLE_CLASS(TVirtualCursor
) : public MOverlay
80 void Set(const TRect
& aRect
, CFbsBitmap
* aBmp
, CFbsBitmap
* aAlpha
);
81 void Move(TInt aX
, TInt aY
);
82 void MakeEvent(TWsEvent
& aEvent
, const TPoint
& aBasePos
) const;
86 void Draw(CBitmapContext
& aGc
, const TRect
& aTargetRect
, const TSize
& aSize
);
97 TVirtualCursor::TVirtualCursor() : iInc(0, 0), iIsOn(EFalse
), iCBmp(NULL
)
101 const TInt KMaxMove
= 10;
103 void TVirtualCursor::Move(TInt aX
, TInt aY
)
105 if(aX
> 0 && iInc
.iX
> 0)
107 else if(aX
< 0 && iInc
.iX
< 0)
112 if(aY
> 0 && iInc
.iY
> 0)
114 else if(aY
< 0 && iInc
.iY
< 0)
119 iInc
.iX
= Min(KMaxMove
, iInc
.iX
);
121 iInc
.iX
= Max(-KMaxMove
, iInc
.iX
);
123 iInc
.iY
= Min(KMaxMove
, iInc
.iY
);
125 iInc
.iY
=Max(-KMaxMove
, iInc
.iY
);
127 const TPoint pos
= iPos
+ iInc
;
128 if(iRect
.Contains(pos
))
139 void TVirtualCursor::Toggle()
145 TBool
TVirtualCursor::IsOn() const
150 void TVirtualCursor::Set(const TRect
& aRect
, CFbsBitmap
* aBmp
, CFbsBitmap
* aAlpha
)
158 void TVirtualCursor::MakeEvent(TWsEvent
& aEvent
, const TPoint
& aBasePos
) const
160 aEvent
.SetType(EEventPointer
),
162 TPointerEvent
& pointer
= *aEvent
.Pointer();
163 pointer
.iType
= TPointerEvent::EButton1Down
;
164 pointer
.iPosition
= iPos
;
165 pointer
.iParentPosition
= aBasePos
;
169 void TVirtualCursor::Draw(CBitmapContext
& aGc
, const TRect
& /*aTargetRect*/, const TSize
& /*aSize*/)
171 if(iIsOn
&& iCBmp
!= NULL
)
173 const TRect
rect(TPoint(0, 0), iCBmp
->SizeInPixels());
174 aGc
.AlphaBlendBitmaps(iPos
, iCBmp
, rect
, iAlpha
, TPoint(0, 0));
179 NONSHARABLE_CLASS(TSdlClass
)
183 void SetMain(const TMainFunc
& aFunc
, TInt aFlags
, MSDLMainObs
* aObs
, TInt aExeFlags
);
184 TInt
SdlFlags() const;
185 const TMainFunc
& Main() const;
186 void SendEvent(TInt aEvent
, TInt aParam
, CSDL
* aSDL
);
187 TInt
AppFlags() const;
188 void AppFlags(TInt aFlags
);
197 void TSdlClass::AppFlags(TInt aFlags
)
202 void TSdlClass::SendEvent(TInt aEvent
, TInt aParam
, CSDL
* aSDL
)
205 iObs
->SDLMainEvent(aEvent
, aParam
, aSDL
);
208 TInt
TSdlClass::AppFlags() const
213 void TSdlClass::SetMain(const TMainFunc
& aFunc
, TInt aFlags
, MSDLMainObs
* aObs
, TInt aExeFlags
)
217 iExeFlags
= aExeFlags
;
221 const TMainFunc
& TSdlClass::Main() const
227 TInt
TSdlClass::SdlFlags() const
234 TSdlClass::TSdlClass()
236 Mem::FillZ(this, sizeof(this));
242 ////////////////////////////////////////////////////////////////
244 NONSHARABLE_CLASS(CSDLApplication
) : public CAknApplication
249 CApaDocument
* CreateDocumentL();
250 TFileName
ResourceFileName() const;
251 TUid
AppDllUid() const;
256 NONSHARABLE_CLASS(CSDLDocument
) : public CEikDocument
259 CSDLDocument(CEikApplication
& aApp
);
261 CEikAppUi
* CreateAppUiL();
265 ////////////////////////////////////////////////////////////////////
268 NONSHARABLE_CLASS(MExitWait
)
271 virtual void DoExit(TInt aErr
) = 0;
274 /////////////////////////////////////////////////////////////////////////
276 NONSHARABLE_CLASS(CExitWait
) : public CActive
279 CExitWait(MExitWait
& aWait
);
286 TRequestStatus
* iStatusPtr
;
289 ////////////////////////////////////////////////////////////////////////
292 NONSHARABLE_CLASS(CSDLWin
) : public CCoeControl
295 void ConstructL(const TRect
& aRect
);
296 RWindow
& GetWindow() const;
299 void Draw(const TRect
& aRect
) const;
305 ////////////////////////////////////////////////////////////////////////////
307 NONSHARABLE_CLASS(CSDLAppUi
) : public CAknAppUi
, public MExitWait
, public MSDLObserver
311 private: // New functions
313 void HandleCommandL(TInt aCommand
);
314 void HandleWsEventL(const TWsEvent
& aEvent
, CCoeControl
* aDestination
);
315 void HandleResourceChangeL(TInt aType
);
317 void DoExit(TInt aErr
);
319 TInt
SdlEvent(TInt aEvent
, TInt aParam
);
320 TInt
SdlThreadEvent(TInt aEvent
, TInt aParam
);
323 static TBool
StartL(TAny
* aThis
);
325 TBool
ParamEditorL(TDes
& aCheat
);
327 TBool
ProcessCommandParametersL(CApaCommandLine
&aCommandLine
);
329 void PrepareToExit();
330 void HandleConsoleWindowL();
331 void HandleConsoleWindow();
332 void HandleForegroundEventL(TBool aForeground
);
334 static TBool
IdleRequestL(TAny
* aThis
);
336 TBool
HandleKeyL(const TWsEvent
& aEvent
);
345 CDesC8Array
* iParams
;
349 TVirtualCursor iCursor
;
354 ////////////////////////////////////////////////////////////////////////////////////////7
356 CApaDocument
* CSDLApplication::CreateDocumentL()
358 return new (ELeave
) CSDLDocument(*this);
361 TUid
CSDLApplication::AppDllUid() const
367 CSDLApplication::CSDLApplication()
369 TRAPD(err
, FindMeL());
370 ASSERT(err
== KErrNone
);
373 void CSDLApplication::FindMeL()
376 User::LeaveIfError(apa
.Connect());
377 CleanupClosePushL(apa
);
378 User::LeaveIfError(apa
.GetAllApps());
379 TFileName name
= RProcess().FileName();
381 while(apa
.GetNextApp(info
) == KErrNone
)
383 if(info
.iFullName
.CompareF(name
) == 0)
389 CleanupStack::PopAndDestroy();
392 TFileName
CSDLApplication::ResourceFileName() const
397 ///////////////////////////////////////////////////////////////////////////////////////////
399 CExitWait::CExitWait(MExitWait
& aWait
) : CActive(CActive::EPriorityStandard
), iWait(aWait
)
401 CActiveScheduler::Add(this);
403 iStatusPtr
= &iStatus
;
406 CExitWait::~CExitWait()
411 void CExitWait::RunL()
413 if(iStatusPtr
!= NULL
)
414 iWait
.DoExit(iStatus
.Int());
417 void CExitWait::DoCancel()
419 if(iStatusPtr
!= NULL
)
420 User::RequestComplete(iStatusPtr
, KErrCancel
);
424 //////////////////////////////////////////////////////////////////////////////////////////////
426 CSDLDocument::CSDLDocument(CEikApplication
& aApp
) : CEikDocument(aApp
)
429 CEikAppUi
* CSDLDocument::CreateAppUiL()
431 return new (ELeave
) CSDLAppUi
;
434 ///////////////////////////////////////////////////////////////////////////
436 void CSDLWin:: ConstructL(const TRect
& aRect
)
444 RWindow
& CSDLWin::GetWindow() const
450 void CSDLWin::Draw(const TRect
& /*aRect*/) const
454 CWindowGc
& gc
= SystemGc();
455 gc
.SetPenStyle(CGraphicsContext::ESolidPen
);
456 gc
.SetPenColor(KRgbGray
);
457 gc
.SetBrushStyle(CGraphicsContext::ESolidBrush
);
458 gc
.SetBrushColor(0x000000);
463 void CSDLWin::SetNoDraw()
468 /////////////////////////////////////////////////////////////////////////
470 CSDLAppUi::~CSDLAppUi()
487 void CSDLAppUi::ConstructL()
489 BaseConstructL(ENoAppResourceFile
| ENoScreenFurniture
);
493 User::LeaveIfError(lib
.Load(_L("sdlexe.dll")));
494 TFileName name
= lib
.FileName();
496 name
.Replace(3, name
.Length() - 3, _L("resource\\apps\\sdlexe.rsc"));
497 BaflUtils::NearestLanguageFile(iEikonEnv
->FsSession(), name
);
498 iResOffset
= iCoeEnv
->AddResourceFileL(name
);
500 name
.Replace(name
.Length() - 3, 3, _L("mbm"));
503 const TInt err
= iEikonEnv
->FsSession().Entry(name
, e
);
505 iCBmp
= iEikonEnv
->CreateBitmapL(name
, 0);
506 iAlpha
= iEikonEnv
->CreateBitmapL(name
, 1);
508 iIdle
= CIdle::NewL(CActive::EPriorityIdle
);
510 iSDLWin
= new (ELeave
) CSDLWin
;
511 iSDLWin
->ConstructL(ApplicationRect());
513 iSdl
= CSDL::NewL(gSDLClass
.SdlFlags());
515 gSDLClass
.SendEvent(MSDLMainObs::ESDLCreated
, 0, iSdl
);
517 iSdl
->SetObserver(this);
518 iSdl
->DisableKeyBlocking(*this);
519 iSdl
->SetContainerWindowL(
520 iSDLWin
->GetWindow(),
521 iEikonEnv
->WsSession(),
522 *iEikonEnv
->ScreenDevice());
523 iSdl
->AppendOverlay(iCursor
, 0);
525 iCursor
.Set(TRect(TPoint(0, 0), iSDLWin
->Size()), iCBmp
, iAlpha
);
527 iStarter
= CIdle::NewL(CActive::EPriorityLow
);
528 iStarter
->Start(TCallBack(StartL
, this));
535 TBool
CSDLAppUi::StartL(TAny
* aThis
)
537 static_cast<CSDLAppUi
*>(aThis
)->StartL();
542 void CSDLAppUi::PrepareToExit()
544 CAknAppUiBase::PrepareToExit(); //aknappu::PrepareToExit crashes
545 iCoeEnv
->DeleteResourceFile(iResOffset
);
548 TBool
CSDLAppUi::ProcessCommandParametersL(CApaCommandLine
&aCommandLine
)
550 const TPtrC8 cmdLine
= aCommandLine
.TailEnd();
551 iParams
= new (ELeave
) CDesC8ArrayFlat(8);
552 MakeCCmdLineL(cmdLine
, *iParams
);
557 TBool
CSDLAppUi::ParamEditorL(TDes
& aCheat
)
559 CAknTextQueryDialog
* query
= CAknTextQueryDialog::NewL(aCheat
);
560 CleanupStack::PushL(query
);
561 query
->SetPromptL(_L("Enter parameters"));
563 return query
->ExecuteLD(R_PARAMEDITOR
);
566 void CSDLAppUi::StartL()
568 if(gSDLClass
.AppFlags() & SDLEnv::EParamQuery
)
572 TInt err
= file
.Open(iEikonEnv
->FsSession(), _L("sdl_param.txt"),EFileRead
);
577 MakeCCmdLineL(cmd
, *iParams
);
579 if(err
!= KErrNone
|| gSDLClass
.AppFlags() & (SDLEnv::EParamQueryDialog
^ SDLEnv::EParamQuery
))
582 if(ParamEditorL(buffer
))
585 MakeCCmdLineL(cmd
, *iParams
);
589 iWait
= new (ELeave
) CExitWait(*this);
590 iSdl
->CallMainL(gSDLClass
.Main(), &iWait
->iStatus
, iParams
, CSDL::ENoParamFlags
, 0xA000);
593 void CSDLAppUi::HandleCommandL(TInt aCommand
)
597 case EAknSoftkeyBack
:
598 case EAknSoftkeyExit
:
601 gSDLClass
.AppFlags(SDLEnv::EAllowConsoleView
);
602 if(iWait
== NULL
|| !iWait
->IsActive() || iSdl
== NULL
)
606 else if(!iExitRequest
)
609 event
.SetType(EEventSwitchOff
),
611 iSdl
->AppendWsEvent(event
);
612 User::After(1000000);
613 iExitRequest
= ETrue
; //trick how SDL can be closed!
622 TBool
CSDLAppUi::HandleKeyL(const TWsEvent
& aEvent
)
624 const TInt type
= aEvent
.Type();
625 if(!(type
== EEventKey
|| type
== EEventKeyUp
|| type
== EEventKeyDown
))
629 const TKeyEvent
& key
= *aEvent
.Key();
630 if((key
.iScanCode
== EStdKeyYes
) && (gSDLClass
.AppFlags() & SDLEnv::EVirtualMouse
))
632 if(type
== EEventKeyUp
)
635 iSdl
->RedrawRequest();
641 switch(key
.iScanCode
)
646 case EStdKeyDownArrow
:
649 case EStdKeyLeftArrow
:
652 case EStdKeyRightArrow
:
656 if(type
== EEventKeyUp
)
659 iCursor
.MakeEvent(event
, iSDLWin
->Position());
660 iSdl
->AppendWsEvent(event
);
666 iSdl
->RedrawRequest();
672 void CSDLAppUi::HandleWsEventL(const TWsEvent
& aEvent
, CCoeControl
* aDestination
)
674 if(iSdl
&& iWait
&& HandleKeyL(aEvent
))
675 iSdl
->AppendWsEvent(aEvent
);
676 CAknAppUi::HandleWsEventL(aEvent
, aDestination
);
679 void CSDLAppUi::HandleResourceChangeL(TInt aType
)
681 CAknAppUi::HandleResourceChangeL(aType
);
682 if(aType
== KEikDynamicLayoutVariantSwitch
)
684 iSDLWin
->SetRect(ApplicationRect());
685 iSdl
->SetContainerWindowL(
686 iSDLWin
->GetWindow(),
687 iEikonEnv
->WsSession(),
688 *iEikonEnv
->ScreenDevice());
693 void CSDLAppUi::DoExit(TInt
/*Err*/)
695 iExitRequest
= ETrue
;
700 TInt
CSDLAppUi::SdlThreadEvent(TInt aEvent
, TInt
/*aParam*/)
704 case MSDLObserver::EEventResume
:
706 case MSDLObserver::EEventSuspend
:
708 return MSDLObserver::ESuspendNoSuspend
;
710 case MSDLObserver::EEventWindowReserved
:
712 case MSDLObserver::EEventWindowNotAvailable
:
714 case MSDLObserver::EEventScreenSizeChanged
:
717 return MSDLObserver::EParameterNone
;
720 TInt
CSDLAppUi::SdlEvent(TInt aEvent
, TInt
/*aParam*/)
724 case MSDLObserver::EEventResume
:
726 case MSDLObserver::EEventSuspend
:
728 return MSDLObserver::ESuspendNoSuspend
;
730 case MSDLObserver::EEventWindowReserved
:
732 case MSDLObserver::EEventWindowNotAvailable
:
734 TRAP_IGNORE(HandleConsoleWindowL());
737 case MSDLObserver::EEventScreenSizeChanged
:
739 case MSDLObserver::EEventKeyMapInit
:
741 case MSDLObserver::EEventMainExit
:
744 gSDLClass
.AppFlags(SDLEnv::EAllowConsoleView
);
745 iEikonEnv
->WsSession().SetWindowGroupOrdinalPosition(iStdOut
, 0);
749 return MSDLObserver::EParameterNone
;
752 void CSDLAppUi::HandleForegroundEventL(TBool aForeground
)
754 CAknAppUi::HandleForegroundEventL(aForeground
);
756 HandleConsoleWindow();
759 void CSDLAppUi::HandleConsoleWindow()
761 if(!iIdle
->IsActive())
762 iIdle
->Start(TCallBack(IdleRequestL
, this));
765 TBool
CSDLAppUi::IdleRequestL(TAny
* aThis
)
767 static_cast<CSDLAppUi
*>(aThis
)->HandleConsoleWindowL();
771 void CSDLAppUi::HandleConsoleWindowL()
773 if(gSDLClass
.AppFlags() & SDLEnv::EAllowConsoleView
)
777 RWsSession
& ses
= iEikonEnv
->WsSession();
778 const TInt focus
= ses
.GetFocusWindowGroup();
779 CApaWindowGroupName
* name
= CApaWindowGroupName::NewLC(ses
, focus
);
780 const TPtrC caption
= name
->Caption();
781 if(0 == caption
.CompareF(_L("STDOUT")))
784 ses
.SetWindowGroupOrdinalPosition(iEikonEnv
->RootWin().Identifier(), 0);
786 CleanupStack::PopAndDestroy(); //name
792 ////////////////////////////////////////////////////////////////////////
795 CApaApplication
* NewApplication()
797 return new CSDLApplication();
801 EXPORT_C TInt
SDLEnv::SetMain(const TMainFunc
& aFunc
, TInt aSdlFlags
, MSDLMainObs
* aObs
, TInt aSdlExeFlags
)
803 gSDLClass
.SetMain(aFunc
, aSdlFlags
, aObs
, aSdlExeFlags
);
804 return EikStart::RunApplication(NewApplication
);
807 //////////////////////////////////////////////////////////////////////
809 TInt
SDLUiPrint(const TDesC8
& /*aInfo*/)