sl@0: // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // Simple implementation of stdin/stdout/stderr class which uses a console, created on demand. sl@0: // sl@0: // sl@0: sl@0: #include "FDESC.H" sl@0: #include // for CConsoleBase sl@0: #include sl@0: sl@0: void CTtyDesc::CheckConsoleCreated() sl@0: { sl@0: if (iConsole==NULL) sl@0: { sl@0: TRAPD(r,iConsole=Console::NewL(_L("STDOUT"),TSize(KConsFullScreen,KConsFullScreen))) sl@0: __ASSERT_ALWAYS(r==KErrNone,User::Panic(_L("ESTLIB Console"),0)); sl@0: } sl@0: } sl@0: sl@0: CTtyDesc::~CTtyDesc() sl@0: { sl@0: CConsoleBase *it = iConsole; sl@0: iConsole = 0; sl@0: delete it; sl@0: } sl@0: sl@0: TInt CTtyDesc::FinalClose() sl@0: { sl@0: CConsoleBase *it = iConsole; sl@0: iConsole = 0; sl@0: if (it) sl@0: { sl@0: // it->SetTitle(_L("Console closed - press any key")); sl@0: it->Printf(_L("Console closed - press any key")); sl@0: it->Getch(); sl@0: delete it; sl@0: } sl@0: return KErrNone; sl@0: } sl@0: sl@0: void CTtyDesc::Read(TDes8& /*aDesc*/, TRequestStatus& aStatus) sl@0: { sl@0: CheckConsoleCreated(); sl@0: // See implemention of Getch() in E32\UBAS\UB_CONS.CPP sl@0: iConsole->Read(aStatus); sl@0: } sl@0: sl@0: void CTtyDesc::ReadCancel() sl@0: { sl@0: if (iConsole) sl@0: iConsole->ReadCancel(); sl@0: } sl@0: sl@0: TInt CTtyDesc::ReadCompletion(TDes8& aDesc, TInt aStatus) sl@0: { sl@0: MapCodeAndEcho(aDesc, iConsole->KeyCode()); sl@0: return aStatus; sl@0: } sl@0: sl@0: void CTtyDesc::Write(TDes8 &aDesc, TRequestStatus& aStatus) sl@0: { sl@0: Write(aDesc); sl@0: Complete(aStatus, KErrNone); sl@0: } sl@0: sl@0: #if !defined(_UNICODE) sl@0: void CTtyDesc::Write(TDes8 &aDesc) sl@0: { sl@0: CheckConsoleCreated(); sl@0: iConsole->Write(aDesc); sl@0: } sl@0: #else sl@0: void CTtyDesc::Write(TDes8 &aDesc) sl@0: { sl@0: CheckConsoleCreated(); sl@0: sl@0: TInt remaining = aDesc.Length(); sl@0: const TText8* cp = aDesc.Ptr(); sl@0: while (remaining) sl@0: { sl@0: TBuf16<256> buffer; sl@0: TInt len = Min(remaining, 256); sl@0: buffer.Copy(TPtrC8(cp, len)); sl@0: sl@0: iConsole->Write(buffer); sl@0: sl@0: cp += len; sl@0: remaining -= len; sl@0: } sl@0: } sl@0: #endif sl@0: sl@0: void CTtyDesc::MapCodeAndEcho(TDes8& aDesc, TKeyCode aCode) sl@0: { sl@0: TText8 ch; sl@0: sl@0: aDesc.Zero(); sl@0: switch (aCode) sl@0: { sl@0: case EKeyPrintScreen: sl@0: return; // unknowable keycodes are ignored sl@0: sl@0: case EKeyEnter: sl@0: ch='\n'; break; sl@0: case EKeyBackspace: sl@0: ch=0x08; break; sl@0: default: sl@0: ch=(TText8)(aCode&0xff); // who knows - it's not documented sl@0: break; sl@0: } sl@0: aDesc.Append(ch); sl@0: // Could suppress echoing at this point sl@0: Write(aDesc); sl@0: return; sl@0: } sl@0: sl@0: void CTtyDesc::Ioctl(int /*aCmd*/, void* /*aParam*/, TRequestStatus& aStatus) sl@0: { sl@0: // bodge for now - this will become more complicated if we develop a better sl@0: // terminal device implementation sl@0: Complete(aStatus,KErrNone); sl@0: } sl@0: sl@0: TInt CTtyDesc::IoctlCompletion(int aCmd, void* aParam, TInt aStatus) sl@0: { sl@0: TInt ret=aStatus; sl@0: if (ret!=KErrNone) sl@0: return ret; sl@0: int *param=REINTERPRET_CAST(int*,aParam); sl@0: switch (aCmd) sl@0: { sl@0: case E32IONREAD: sl@0: *param=1; // claim that there is always data available sl@0: break; sl@0: case E32IOSELECT: sl@0: *param=(*param)&(E32SELECT_READ|E32SELECT_WRITE); sl@0: break; sl@0: default: sl@0: ret=KErrNotSupported; sl@0: break; sl@0: } sl@0: return ret; sl@0: } sl@0: