1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/USTLIB/UCONSOLE.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,155 @@
1.4 +// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// Simple implementation of stdin/stdout/stderr class which uses a console, created on demand.
1.18 +//
1.19 +//
1.20 +
1.21 +#include "FDESC.H"
1.22 +#include <e32cons.h> // for CConsoleBase
1.23 +#include <sys/ioctl.h>
1.24 +
1.25 +void CTtyDesc::CheckConsoleCreated()
1.26 + {
1.27 + if (iConsole==NULL)
1.28 + {
1.29 + TRAPD(r,iConsole=Console::NewL(_L("STDOUT"),TSize(KConsFullScreen,KConsFullScreen)))
1.30 + __ASSERT_ALWAYS(r==KErrNone,User::Panic(_L("ESTLIB Console"),0));
1.31 + }
1.32 + }
1.33 +
1.34 +CTtyDesc::~CTtyDesc()
1.35 + {
1.36 + CConsoleBase *it = iConsole;
1.37 + iConsole = 0;
1.38 + delete it;
1.39 + }
1.40 +
1.41 +TInt CTtyDesc::FinalClose()
1.42 + {
1.43 + CConsoleBase *it = iConsole;
1.44 + iConsole = 0;
1.45 + if (it)
1.46 + {
1.47 + // it->SetTitle(_L("Console closed - press any key"));
1.48 + it->Printf(_L("Console closed - press any key"));
1.49 + it->Getch();
1.50 + delete it;
1.51 + }
1.52 + return KErrNone;
1.53 + }
1.54 +
1.55 +void CTtyDesc::Read(TDes8& /*aDesc*/, TRequestStatus& aStatus)
1.56 + {
1.57 + CheckConsoleCreated();
1.58 + // See implemention of Getch() in E32\UBAS\UB_CONS.CPP
1.59 + iConsole->Read(aStatus);
1.60 + }
1.61 +
1.62 +void CTtyDesc::ReadCancel()
1.63 + {
1.64 + if (iConsole)
1.65 + iConsole->ReadCancel();
1.66 + }
1.67 +
1.68 +TInt CTtyDesc::ReadCompletion(TDes8& aDesc, TInt aStatus)
1.69 + {
1.70 + MapCodeAndEcho(aDesc, iConsole->KeyCode());
1.71 + return aStatus;
1.72 + }
1.73 +
1.74 +void CTtyDesc::Write(TDes8 &aDesc, TRequestStatus& aStatus)
1.75 + {
1.76 + Write(aDesc);
1.77 + Complete(aStatus, KErrNone);
1.78 + }
1.79 +
1.80 +#if !defined(_UNICODE)
1.81 +void CTtyDesc::Write(TDes8 &aDesc)
1.82 + {
1.83 + CheckConsoleCreated();
1.84 + iConsole->Write(aDesc);
1.85 + }
1.86 +#else
1.87 +void CTtyDesc::Write(TDes8 &aDesc)
1.88 + {
1.89 + CheckConsoleCreated();
1.90 +
1.91 + TInt remaining = aDesc.Length();
1.92 + const TText8* cp = aDesc.Ptr();
1.93 + while (remaining)
1.94 + {
1.95 + TBuf16<256> buffer;
1.96 + TInt len = Min(remaining, 256);
1.97 + buffer.Copy(TPtrC8(cp, len));
1.98 +
1.99 + iConsole->Write(buffer);
1.100 +
1.101 + cp += len;
1.102 + remaining -= len;
1.103 + }
1.104 + }
1.105 +#endif
1.106 +
1.107 +void CTtyDesc::MapCodeAndEcho(TDes8& aDesc, TKeyCode aCode)
1.108 + {
1.109 + TText8 ch;
1.110 +
1.111 + aDesc.Zero();
1.112 + switch (aCode)
1.113 + {
1.114 + case EKeyPrintScreen:
1.115 + return; // unknowable keycodes are ignored
1.116 +
1.117 + case EKeyEnter:
1.118 + ch='\n'; break;
1.119 + case EKeyBackspace:
1.120 + ch=0x08; break;
1.121 + default:
1.122 + ch=(TText8)(aCode&0xff); // who knows - it's not documented
1.123 + break;
1.124 + }
1.125 + aDesc.Append(ch);
1.126 + // Could suppress echoing at this point
1.127 + Write(aDesc);
1.128 + return;
1.129 + }
1.130 +
1.131 +void CTtyDesc::Ioctl(int /*aCmd*/, void* /*aParam*/, TRequestStatus& aStatus)
1.132 + {
1.133 + // bodge for now - this will become more complicated if we develop a better
1.134 + // terminal device implementation
1.135 + Complete(aStatus,KErrNone);
1.136 + }
1.137 +
1.138 +TInt CTtyDesc::IoctlCompletion(int aCmd, void* aParam, TInt aStatus)
1.139 + {
1.140 + TInt ret=aStatus;
1.141 + if (ret!=KErrNone)
1.142 + return ret;
1.143 + int *param=REINTERPRET_CAST(int*,aParam);
1.144 + switch (aCmd)
1.145 + {
1.146 + case E32IONREAD:
1.147 + *param=1; // claim that there is always data available
1.148 + break;
1.149 + case E32IOSELECT:
1.150 + *param=(*param)&(E32SELECT_READ|E32SELECT_WRITE);
1.151 + break;
1.152 + default:
1.153 + ret=KErrNotSupported;
1.154 + break;
1.155 + }
1.156 + return ret;
1.157 + }
1.158 +