5 #include <sdlepocapi.h>
15 #include <aknglobalmsgquery.h>
20 // FORWARD DECLARATIONS
24 //const TUid KSDLUID = { 0xF01F605E };
26 LOCAL_C void MakeCCmdLineL(const TDesC8& aParam, CDesC8Array& aArray)
34 lex.SkipSpaceAndMark();
41 const TPtrC8 rem = lex.RemainderFromMark();
42 const TInt pos = rem.Locate(dq);
46 ptr.Set(lex.MarkedToken());
57 ptr.Set(lex.NextToken());
58 const TInt pos = ptr.Locate(dq);
64 continue; // back to in brace
67 lex.SkipSpaceAndMark();
75 NONSHARABLE_CLASS(TVirtualCursor) : public MOverlay
79 void Set(const TRect& aRect, CFbsBitmap* aBmp, CFbsBitmap* aAlpha);
80 void Move(TInt aX, TInt aY);
81 void MakeEvent(TWsEvent& aEvent, const TPoint& aBasePos) const;
85 void Draw(CBitmapContext& aGc, const TRect& aTargetRect, const TSize& aSize);
96 TVirtualCursor::TVirtualCursor() : iInc(0, 0), iIsOn(EFalse), iCBmp(NULL)
100 const TInt KMaxMove = 10;
102 void TVirtualCursor::Move(TInt aX, TInt aY)
104 if(aX > 0 && iInc.iX > 0)
106 else if(aX < 0 && iInc.iX < 0)
111 if(aY > 0 && iInc.iY > 0)
113 else if(aY < 0 && iInc.iY < 0)
118 iInc.iX = Min(KMaxMove, iInc.iX);
120 iInc.iX = Max(-KMaxMove, iInc.iX);
122 iInc.iY = Min(KMaxMove, iInc.iY);
124 iInc.iY =Max(-KMaxMove, iInc.iY);
126 const TPoint pos = iPos + iInc;
127 if(iRect.Contains(pos))
138 void TVirtualCursor::Toggle()
144 TBool TVirtualCursor::IsOn() const
149 void TVirtualCursor::Set(const TRect& aRect, CFbsBitmap* aBmp, CFbsBitmap* aAlpha)
157 void TVirtualCursor::MakeEvent(TWsEvent& aEvent, const TPoint& aBasePos) const
159 aEvent.SetType(EEventPointer),
161 TPointerEvent& pointer = *aEvent.Pointer();
162 pointer.iType = TPointerEvent::EButton1Down;
163 pointer.iPosition = iPos;
164 pointer.iParentPosition = aBasePos;
168 void TVirtualCursor::Draw(CBitmapContext& aGc, const TRect& /*aTargetRect*/, const TSize& /*aSize*/)
170 if(iIsOn && iCBmp != NULL)
172 const TRect rect(TPoint(0, 0), iCBmp->SizeInPixels());
173 aGc.AlphaBlendBitmaps(iPos, iCBmp, rect, iAlpha, TPoint(0, 0));
178 NONSHARABLE_CLASS(TSdlClass)
182 void SetMain(const TMainFunc& aFunc, TInt aFlags, MSDLMainObs* aObs, TInt aExeFlags);
183 TInt SdlFlags() const;
184 const TMainFunc& Main() const;
185 void SendEvent(TInt aEvent, TInt aParam, CSDL* aSDL);
186 TInt AppFlags() const;
187 void AppFlags(TInt aFlags);
196 void TSdlClass::AppFlags(TInt aFlags)
201 void TSdlClass::SendEvent(TInt aEvent, TInt aParam, CSDL* aSDL)
204 iObs->SDLMainEvent(aEvent, aParam, aSDL);
207 TInt TSdlClass::AppFlags() const
212 void TSdlClass::SetMain(const TMainFunc& aFunc, TInt aFlags, MSDLMainObs* aObs, TInt aExeFlags)
216 iExeFlags = aExeFlags;
220 const TMainFunc& TSdlClass::Main() const
226 TInt TSdlClass::SdlFlags() const
233 TSdlClass::TSdlClass()
235 Mem::FillZ(this, sizeof(this));
241 ////////////////////////////////////////////////////////////////
243 NONSHARABLE_CLASS(CSDLApplication) : public CAknApplication
248 CApaDocument* CreateDocumentL();
249 TFileName ResourceFileName() const;
250 TUid AppDllUid() const;
255 NONSHARABLE_CLASS(CSDLDocument) : public CEikDocument
258 CSDLDocument(CEikApplication& aApp);
260 CEikAppUi* CreateAppUiL();
264 ////////////////////////////////////////////////////////////////////
267 NONSHARABLE_CLASS(MExitWait)
270 virtual void DoExit(TInt aErr) = 0;
273 /////////////////////////////////////////////////////////////////////////
275 NONSHARABLE_CLASS(CExitWait) : public CActive
278 CExitWait(MExitWait& aWait);
285 TRequestStatus* iStatusPtr;
288 ////////////////////////////////////////////////////////////////////////
291 NONSHARABLE_CLASS(CSDLWin) : public CCoeControl
294 void ConstructL(const TRect& aRect);
295 RWindow& GetWindow() const;
298 void Draw(const TRect& aRect) const;
304 ////////////////////////////////////////////////////////////////////////////
306 NONSHARABLE_CLASS(CSDLAppUi) : public CAknAppUi, public MExitWait, MSDLObserver
310 private: // New functions
312 void HandleCommandL(TInt aCommand);
313 void HandleWsEventL(const TWsEvent& aEvent, CCoeControl* aDestination);
314 void HandleResourceChangeL(TInt aType);
316 void DoExit(TInt aErr);
318 TInt SdlEvent(TInt aEvent, TInt aParam);
319 TInt SdlThreadEvent(TInt aEvent, TInt aParam);
322 static TBool StartL(TAny* aThis);
324 TBool ParamEditorL(TDes& aCheat);
326 TBool ProcessCommandParametersL(CApaCommandLine &aCommandLine);
328 void PrepareToExit();
329 void HandleConsoleWindowL();
330 void HandleConsoleWindow();
331 void HandleForegroundEventL(TBool aForeground);
333 static TBool IdleRequestL(TAny* aThis);
335 TBool HandleKeyL(const TWsEvent& aEvent);
344 CDesC8Array* iParams;
348 TVirtualCursor iCursor;
352 // CSDL::TOrientationMode iOrientation;
355 ////////////////////////////////////////////////////////////////////////////////////////7
357 CApaDocument* CSDLApplication::CreateDocumentL()
359 return new (ELeave) CSDLDocument(*this);
362 TUid CSDLApplication::AppDllUid() const
368 CSDLApplication::CSDLApplication()
370 TRAPD(err, FindMeL());
371 ASSERT(err == KErrNone);
374 void CSDLApplication::FindMeL()
377 User::LeaveIfError(apa.Connect());
378 CleanupClosePushL(apa);
379 User::LeaveIfError(apa.GetAllApps());
380 TFileName name = RProcess().FileName();
382 while(apa.GetNextApp(info) == KErrNone)
384 if(info.iFullName.CompareF(name) == 0)
390 CleanupStack::PopAndDestroy();
393 TFileName CSDLApplication::ResourceFileName() const
398 ///////////////////////////////////////////////////////////////////////////////////////////
400 CExitWait::CExitWait(MExitWait& aWait) : CActive(CActive::EPriorityStandard), iWait(aWait)
402 CActiveScheduler::Add(this);
404 iStatusPtr = &iStatus;
407 CExitWait::~CExitWait()
412 void CExitWait::RunL()
414 if(iStatusPtr != NULL )
415 iWait.DoExit(iStatus.Int());
418 void CExitWait::DoCancel()
420 if(iStatusPtr != NULL )
421 User::RequestComplete(iStatusPtr , KErrCancel);
425 //////////////////////////////////////////////////////////////////////////////////////////////
427 CSDLDocument::CSDLDocument(CEikApplication& aApp) : CEikDocument(aApp)
430 CEikAppUi* CSDLDocument::CreateAppUiL()
432 return new (ELeave) CSDLAppUi;
435 ///////////////////////////////////////////////////////////////////////////
437 void CSDLWin:: ConstructL(const TRect& aRect)
445 RWindow& CSDLWin::GetWindow() const
451 void CSDLWin::Draw(const TRect& /*aRect*/) const
455 CWindowGc& gc = SystemGc();
456 gc.SetPenStyle(CGraphicsContext::ESolidPen);
457 gc.SetPenColor(KRgbGray);
458 gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
459 gc.SetBrushColor(0xaaaaaa);
464 void CSDLWin::SetNoDraw()
469 /////////////////////////////////////////////////////////////////////////
471 CSDLAppUi::~CSDLAppUi()
488 void CSDLAppUi::ConstructL()
490 BaseConstructL(ENoAppResourceFile | ENoScreenFurniture);
494 User::LeaveIfError(lib.Load(_L("sdlexe.dll")));
495 TFileName name = lib.FileName();
497 name.Replace(3, name.Length() - 3, _L("resource\\apps\\sdlexe.rsc"));
498 BaflUtils::NearestLanguageFile(iEikonEnv->FsSession(), name);
499 iResOffset = iCoeEnv->AddResourceFileL(name);
501 name.Replace(name.Length() - 3, 3, _L("mbm"));
504 const TInt err = iEikonEnv->FsSession().Entry(name, e);
506 iCBmp = iEikonEnv->CreateBitmapL(name, 0);
507 iAlpha = iEikonEnv->CreateBitmapL(name, 1);
509 iIdle = CIdle::NewL(CActive::EPriorityIdle);
511 iSDLWin = new (ELeave) CSDLWin;
512 iSDLWin->ConstructL(ApplicationRect());
514 iSdl = CSDL::NewL(gSDLClass.SdlFlags());
516 gSDLClass.SendEvent(MSDLMainObs::ESDLCreated, 0, iSdl);
518 iSdl->SetObserver(this);
519 iSdl->DisableKeyBlocking(*this);
520 iSdl->SetContainerWindowL(
521 iSDLWin->GetWindow(),
522 iEikonEnv->WsSession(),
523 *iEikonEnv->ScreenDevice());
524 iSdl->AppendOverlay(iCursor, 0);
526 iCursor.Set(TRect(TPoint(0, 0), iSDLWin->Size()), iCBmp, iAlpha);
528 iStarter = CIdle::NewL(CActive::EPriorityLow);
529 iStarter->Start(TCallBack(StartL, this));
536 TBool CSDLAppUi::StartL(TAny* aThis)
538 static_cast<CSDLAppUi*>(aThis)->StartL();
543 void CSDLAppUi::PrepareToExit()
545 CAknAppUiBase::PrepareToExit(); //aknappu::PrepareToExit crashes
546 iCoeEnv->DeleteResourceFile(iResOffset);
549 TBool CSDLAppUi::ProcessCommandParametersL(CApaCommandLine &aCommandLine)
551 const TPtrC8 cmdLine = aCommandLine.TailEnd();
552 iParams = new (ELeave) CDesC8ArrayFlat(8);
553 MakeCCmdLineL(cmdLine, *iParams);
558 TBool CSDLAppUi::ParamEditorL(TDes& aCheat)
560 CAknTextQueryDialog* query = CAknTextQueryDialog::NewL(aCheat);
561 CleanupStack::PushL(query);
562 query->SetPromptL(_L("Enter parameters"));
564 return query->ExecuteLD(R_PARAMEDITOR);
567 void CSDLAppUi::StartL()
569 if(gSDLClass.AppFlags() & SDLEnv::EParamQuery)
573 TInt err = file.Open(iEikonEnv->FsSession(), _L("sdl_param.txt"),EFileRead);
578 MakeCCmdLineL(cmd, *iParams);
580 if(err != KErrNone || gSDLClass.AppFlags() & (SDLEnv::EParamQueryDialog ^ SDLEnv::EParamQuery))
583 if(ParamEditorL(buffer))
586 MakeCCmdLineL(cmd, *iParams);
590 iWait = new (ELeave) CExitWait(*this);
591 iSdl->CallMainL(gSDLClass.Main(), &iWait->iStatus, iParams, CSDL::ENoParamFlags, 0xA000);
594 void CSDLAppUi::HandleCommandL(TInt aCommand)
598 case EAknSoftkeyBack:
599 case EAknSoftkeyExit:
602 gSDLClass.AppFlags(SDLEnv::EAllowConsoleView);
603 if(iWait == NULL || !iWait->IsActive() || iSdl == NULL)
607 else if(!iExitRequest)
609 iExitRequest = ETrue; //trick how SDL can be closed!
618 TBool CSDLAppUi::HandleKeyL(const TWsEvent& aEvent)
620 const TInt type = aEvent.Type();
621 if(!(type == EEventKey || type == EEventKeyUp || type == EEventKeyDown))
625 const TKeyEvent& key = *aEvent.Key();
626 if((key.iScanCode == EStdKeyYes) && (gSDLClass.AppFlags() & SDLEnv::EVirtualMouse))
628 if(type == EEventKeyUp)
631 iSdl->RedrawRequest();
637 switch(key.iScanCode)
642 case EStdKeyDownArrow:
645 case EStdKeyLeftArrow:
648 case EStdKeyRightArrow:
652 if(type == EEventKeyUp)
655 iCursor.MakeEvent(event, iSDLWin->Position());
656 iSdl->AppendWsEvent(event);
662 iSdl->RedrawRequest();
668 void CSDLAppUi::HandleWsEventL(const TWsEvent& aEvent, CCoeControl* aDestination)
670 if(iSdl && iWait && HandleKeyL(aEvent))
671 iSdl->AppendWsEvent(aEvent);
672 CAknAppUi::HandleWsEventL(aEvent, aDestination);
675 void CSDLAppUi::HandleResourceChangeL(TInt aType)
677 CAknAppUi::HandleResourceChangeL(aType);
678 if(aType == KEikDynamicLayoutVariantSwitch)
680 iSDLWin->SetRect(ApplicationRect());
681 iSdl->SetContainerWindowL(
682 iSDLWin->GetWindow(),
683 iEikonEnv->WsSession(),
684 *iEikonEnv->ScreenDevice());
689 void CSDLAppUi::DoExit(TInt/*Err*/)
691 iExitRequest = ETrue;
696 TInt CSDLAppUi::SdlThreadEvent(TInt aEvent, TInt /*aParam*/)
700 case MSDLObserver::EEventResume:
702 case MSDLObserver::EEventSuspend:
704 return MSDLObserver::ESuspendNoSuspend;
706 case MSDLObserver::EEventWindowReserved:
708 case MSDLObserver::EEventWindowNotAvailable:
710 case MSDLObserver::EEventScreenSizeChanged:
713 return MSDLObserver::EParameterNone;
716 TInt CSDLAppUi::SdlEvent(TInt aEvent, TInt /*aParam*/)
720 case MSDLObserver::EEventResume:
722 case MSDLObserver::EEventSuspend:
724 return MSDLObserver::ESuspendNoSuspend;
726 case MSDLObserver::EEventWindowReserved:
728 case MSDLObserver::EEventWindowNotAvailable:
730 TRAP_IGNORE(HandleConsoleWindowL());
733 case MSDLObserver::EEventScreenSizeChanged:
735 case MSDLObserver::EEventKeyMapInit:
737 case MSDLObserver::EEventMainExit:
740 gSDLClass.AppFlags(SDLEnv::EAllowConsoleView);
741 iEikonEnv->WsSession().SetWindowGroupOrdinalPosition(iStdOut, 0);
745 return MSDLObserver::EParameterNone;
748 void CSDLAppUi::HandleForegroundEventL(TBool aForeground)
750 CAknAppUi::HandleForegroundEventL(aForeground);
752 HandleConsoleWindow();
755 void CSDLAppUi::HandleConsoleWindow()
757 if(!iIdle->IsActive())
758 iIdle->Start(TCallBack(IdleRequestL, this));
761 TBool CSDLAppUi::IdleRequestL(TAny* aThis)
763 static_cast<CSDLAppUi*>(aThis)->HandleConsoleWindowL();
767 void CSDLAppUi::HandleConsoleWindowL()
769 if(gSDLClass.AppFlags() & SDLEnv::EAllowConsoleView)
773 RWsSession& ses = iEikonEnv->WsSession();
774 const TInt focus = ses.GetFocusWindowGroup();
775 CApaWindowGroupName* name = CApaWindowGroupName::NewLC(ses, focus);
776 const TPtrC caption = name->Caption();
777 if(0 == caption.CompareF(_L("STDOUT")))
780 ses.SetWindowGroupOrdinalPosition(iEikonEnv->RootWin().Identifier(), 0);
782 CleanupStack::PopAndDestroy(); //name
786 ////////////////////////////////////////////////////////////////////////
789 CApaApplication* NewApplication()
791 return new CSDLApplication();
795 EXPORT_C TInt SDLEnv::SetMain(const TMainFunc& aFunc, TInt aSdlFlags, MSDLMainObs* aObs, TInt aSdlExeFlags)
797 gSDLClass.SetMain(aFunc, aSdlFlags, aObs, aSdlExeFlags);
798 return EikStart::RunApplication(NewApplication);
801 //////////////////////////////////////////////////////////////////////
803 TInt SDLUiPrint(const TDesC8& /*aInfo*/)