sl@0
|
1 |
// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// Simple implementation of stdin/stdout/stderr class which uses a console, created on demand.
|
sl@0
|
15 |
//
|
sl@0
|
16 |
//
|
sl@0
|
17 |
|
sl@0
|
18 |
#include "FDESC.H"
|
sl@0
|
19 |
#include <e32cons.h> // for CConsoleBase
|
sl@0
|
20 |
#include <sys/ioctl.h>
|
sl@0
|
21 |
|
sl@0
|
22 |
void CTtyDesc::CheckConsoleCreated()
|
sl@0
|
23 |
{
|
sl@0
|
24 |
if (iConsole==NULL)
|
sl@0
|
25 |
{
|
sl@0
|
26 |
TRAPD(r,iConsole=Console::NewL(_L("STDOUT"),TSize(KConsFullScreen,KConsFullScreen)))
|
sl@0
|
27 |
__ASSERT_ALWAYS(r==KErrNone,User::Panic(_L("ESTLIB Console"),0));
|
sl@0
|
28 |
}
|
sl@0
|
29 |
}
|
sl@0
|
30 |
|
sl@0
|
31 |
CTtyDesc::~CTtyDesc()
|
sl@0
|
32 |
{
|
sl@0
|
33 |
CConsoleBase *it = iConsole;
|
sl@0
|
34 |
iConsole = 0;
|
sl@0
|
35 |
delete it;
|
sl@0
|
36 |
}
|
sl@0
|
37 |
|
sl@0
|
38 |
TInt CTtyDesc::FinalClose()
|
sl@0
|
39 |
{
|
sl@0
|
40 |
CConsoleBase *it = iConsole;
|
sl@0
|
41 |
iConsole = 0;
|
sl@0
|
42 |
if (it)
|
sl@0
|
43 |
{
|
sl@0
|
44 |
// it->SetTitle(_L("Console closed - press any key"));
|
sl@0
|
45 |
it->Printf(_L("Console closed - press any key"));
|
sl@0
|
46 |
it->Getch();
|
sl@0
|
47 |
delete it;
|
sl@0
|
48 |
}
|
sl@0
|
49 |
return KErrNone;
|
sl@0
|
50 |
}
|
sl@0
|
51 |
|
sl@0
|
52 |
void CTtyDesc::Read(TDes8& /*aDesc*/, TRequestStatus& aStatus)
|
sl@0
|
53 |
{
|
sl@0
|
54 |
CheckConsoleCreated();
|
sl@0
|
55 |
// See implemention of Getch() in E32\UBAS\UB_CONS.CPP
|
sl@0
|
56 |
iConsole->Read(aStatus);
|
sl@0
|
57 |
}
|
sl@0
|
58 |
|
sl@0
|
59 |
void CTtyDesc::ReadCancel()
|
sl@0
|
60 |
{
|
sl@0
|
61 |
if (iConsole)
|
sl@0
|
62 |
iConsole->ReadCancel();
|
sl@0
|
63 |
}
|
sl@0
|
64 |
|
sl@0
|
65 |
TInt CTtyDesc::ReadCompletion(TDes8& aDesc, TInt aStatus)
|
sl@0
|
66 |
{
|
sl@0
|
67 |
MapCodeAndEcho(aDesc, iConsole->KeyCode());
|
sl@0
|
68 |
return aStatus;
|
sl@0
|
69 |
}
|
sl@0
|
70 |
|
sl@0
|
71 |
void CTtyDesc::Write(TDes8 &aDesc, TRequestStatus& aStatus)
|
sl@0
|
72 |
{
|
sl@0
|
73 |
Write(aDesc);
|
sl@0
|
74 |
Complete(aStatus, KErrNone);
|
sl@0
|
75 |
}
|
sl@0
|
76 |
|
sl@0
|
77 |
#if !defined(_UNICODE)
|
sl@0
|
78 |
void CTtyDesc::Write(TDes8 &aDesc)
|
sl@0
|
79 |
{
|
sl@0
|
80 |
CheckConsoleCreated();
|
sl@0
|
81 |
iConsole->Write(aDesc);
|
sl@0
|
82 |
}
|
sl@0
|
83 |
#else
|
sl@0
|
84 |
void CTtyDesc::Write(TDes8 &aDesc)
|
sl@0
|
85 |
{
|
sl@0
|
86 |
CheckConsoleCreated();
|
sl@0
|
87 |
|
sl@0
|
88 |
TInt remaining = aDesc.Length();
|
sl@0
|
89 |
const TText8* cp = aDesc.Ptr();
|
sl@0
|
90 |
while (remaining)
|
sl@0
|
91 |
{
|
sl@0
|
92 |
TBuf16<256> buffer;
|
sl@0
|
93 |
TInt len = Min(remaining, 256);
|
sl@0
|
94 |
buffer.Copy(TPtrC8(cp, len));
|
sl@0
|
95 |
|
sl@0
|
96 |
iConsole->Write(buffer);
|
sl@0
|
97 |
|
sl@0
|
98 |
cp += len;
|
sl@0
|
99 |
remaining -= len;
|
sl@0
|
100 |
}
|
sl@0
|
101 |
}
|
sl@0
|
102 |
#endif
|
sl@0
|
103 |
|
sl@0
|
104 |
void CTtyDesc::MapCodeAndEcho(TDes8& aDesc, TKeyCode aCode)
|
sl@0
|
105 |
{
|
sl@0
|
106 |
TText8 ch;
|
sl@0
|
107 |
|
sl@0
|
108 |
aDesc.Zero();
|
sl@0
|
109 |
switch (aCode)
|
sl@0
|
110 |
{
|
sl@0
|
111 |
case EKeyPrintScreen:
|
sl@0
|
112 |
return; // unknowable keycodes are ignored
|
sl@0
|
113 |
|
sl@0
|
114 |
case EKeyEnter:
|
sl@0
|
115 |
ch='\n'; break;
|
sl@0
|
116 |
case EKeyBackspace:
|
sl@0
|
117 |
ch=0x08; break;
|
sl@0
|
118 |
default:
|
sl@0
|
119 |
ch=(TText8)(aCode&0xff); // who knows - it's not documented
|
sl@0
|
120 |
break;
|
sl@0
|
121 |
}
|
sl@0
|
122 |
aDesc.Append(ch);
|
sl@0
|
123 |
// Could suppress echoing at this point
|
sl@0
|
124 |
Write(aDesc);
|
sl@0
|
125 |
return;
|
sl@0
|
126 |
}
|
sl@0
|
127 |
|
sl@0
|
128 |
void CTtyDesc::Ioctl(int /*aCmd*/, void* /*aParam*/, TRequestStatus& aStatus)
|
sl@0
|
129 |
{
|
sl@0
|
130 |
// bodge for now - this will become more complicated if we develop a better
|
sl@0
|
131 |
// terminal device implementation
|
sl@0
|
132 |
Complete(aStatus,KErrNone);
|
sl@0
|
133 |
}
|
sl@0
|
134 |
|
sl@0
|
135 |
TInt CTtyDesc::IoctlCompletion(int aCmd, void* aParam, TInt aStatus)
|
sl@0
|
136 |
{
|
sl@0
|
137 |
TInt ret=aStatus;
|
sl@0
|
138 |
if (ret!=KErrNone)
|
sl@0
|
139 |
return ret;
|
sl@0
|
140 |
int *param=REINTERPRET_CAST(int*,aParam);
|
sl@0
|
141 |
switch (aCmd)
|
sl@0
|
142 |
{
|
sl@0
|
143 |
case E32IONREAD:
|
sl@0
|
144 |
*param=1; // claim that there is always data available
|
sl@0
|
145 |
break;
|
sl@0
|
146 |
case E32IOSELECT:
|
sl@0
|
147 |
*param=(*param)&(E32SELECT_READ|E32SELECT_WRITE);
|
sl@0
|
148 |
break;
|
sl@0
|
149 |
default:
|
sl@0
|
150 |
ret=KErrNotSupported;
|
sl@0
|
151 |
break;
|
sl@0
|
152 |
}
|
sl@0
|
153 |
return ret;
|
sl@0
|
154 |
}
|
sl@0
|
155 |
|