|
1 // defcons.cpp |
|
2 // |
|
3 // Copyright (c) 2008 - 2010 Accenture. All rights reserved. |
|
4 // This component and the accompanying materials are made available |
|
5 // under the terms of the "Eclipse Public License v1.0" |
|
6 // which accompanies this distribution, and is available |
|
7 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 // |
|
9 // Initial Contributors: |
|
10 // Accenture - Initial contribution |
|
11 // |
|
12 |
|
13 #include <e32std.h> |
|
14 #include <e32cons.h> |
|
15 #include <e32keys.h> |
|
16 #include <e32uid.h> |
|
17 #ifdef __WINS__ |
|
18 #include <e32wins.h> |
|
19 #endif |
|
20 #include <fshell/descriptorutils.h> |
|
21 |
|
22 |
|
23 // |
|
24 // Constants. |
|
25 // |
|
26 |
|
27 const LtkUtils::SLitC KConsoleImplementations[] = |
|
28 { |
|
29 DESC("guicons"), |
|
30 DESC("econseik"), |
|
31 DESC("econs"), |
|
32 DESC("nullcons") |
|
33 }; |
|
34 const TInt KNumConsoleImplementations = sizeof(KConsoleImplementations) / sizeof(LtkUtils::SLitC); |
|
35 |
|
36 |
|
37 /** |
|
38 |
|
39 The default console implementation, used by iosrv. This console implentation has |
|
40 two reasons to exist: |
|
41 |
|
42 1) To hunt for a suitable real console implementation to be used by default. On |
|
43 GUI configurations this will either be guicons.dll or econseik.dll. On text |
|
44 configurations this will be econs.dll. |
|
45 |
|
46 2) To delay the creation of the real console implementation until it is known |
|
47 to be needed. This is useful because CCommandBase creates a default console |
|
48 even if --console has been specified on the command line. The default console |
|
49 may be used by the specified console during its construction to interact with |
|
50 the user (e.g. vt100tcpcons uses it to tell the user which TCP port and IP |
|
51 address it is listening on). However, if it weren't to be used, the user |
|
52 would see the default console briefly appear and then disappear when the |
|
53 actual console is created. Delaying the creation of the console underlying |
|
54 the default console avoids this. |
|
55 |
|
56 */ |
|
57 NONSHARABLE_CLASS(CDefaultConsole) : public CConsoleBase |
|
58 { |
|
59 public: |
|
60 enum TPanicReason |
|
61 { |
|
62 EDoubleRead = 0, |
|
63 EUnableToCreateConsole = 1 |
|
64 }; |
|
65 public: |
|
66 CDefaultConsole(); |
|
67 virtual ~CDefaultConsole(); |
|
68 virtual TInt Create(const TDesC &aTitle, TSize aSize); |
|
69 virtual void Read(TRequestStatus& aStatus); |
|
70 virtual void ReadCancel(); |
|
71 virtual void Write(const TDesC &aDes); |
|
72 virtual TPoint CursorPos() const; |
|
73 virtual void SetCursorPosAbs(const TPoint &aPoint); |
|
74 virtual void SetCursorPosRel(const TPoint &aPoint); |
|
75 virtual void SetCursorHeight(TInt aPercentage); |
|
76 virtual void SetTitle(const TDesC &aTitle); |
|
77 virtual void ClearScreen(); |
|
78 virtual void ClearToEndOfLine(); |
|
79 virtual TSize ScreenSize() const; |
|
80 virtual TKeyCode KeyCode() const; |
|
81 virtual TUint KeyModifiers() const; |
|
82 virtual TInt Extension_(TUint aExtensionId, TAny*& a0, TAny* a1); |
|
83 private: |
|
84 void CreateIfRequired(); |
|
85 void CreateIfRequired() const; |
|
86 TInt TryCreateConsole(const TDesC& aImplementation); |
|
87 void Panic(TPanicReason aReason); |
|
88 private: |
|
89 HBufC* iTitle; |
|
90 TSize iSize; |
|
91 RLibrary iConsoleLibrary; |
|
92 CConsoleBase* iUnderlyingConsole; |
|
93 }; |
|
94 |
|
95 CDefaultConsole::CDefaultConsole() |
|
96 { |
|
97 } |
|
98 |
|
99 CDefaultConsole::~CDefaultConsole() |
|
100 { |
|
101 delete iTitle; |
|
102 delete iUnderlyingConsole; |
|
103 iConsoleLibrary.Close(); |
|
104 } |
|
105 |
|
106 TInt CDefaultConsole::Create(const TDesC& aTitle, TSize aSize) |
|
107 { |
|
108 iTitle = aTitle.Alloc(); |
|
109 if (iTitle) |
|
110 { |
|
111 iSize = aSize; |
|
112 return KErrNone; |
|
113 } |
|
114 return KErrNoMemory; |
|
115 } |
|
116 |
|
117 void CDefaultConsole::Read(TRequestStatus& aStatus) |
|
118 { |
|
119 CreateIfRequired(); |
|
120 iUnderlyingConsole->Read(aStatus); |
|
121 } |
|
122 |
|
123 void CDefaultConsole::ReadCancel() |
|
124 { |
|
125 if (iUnderlyingConsole) |
|
126 { |
|
127 iUnderlyingConsole->ReadCancel(); |
|
128 } |
|
129 } |
|
130 |
|
131 void CDefaultConsole::Write(const TDesC& aDes) |
|
132 { |
|
133 CreateIfRequired(); |
|
134 iUnderlyingConsole->Write(aDes); |
|
135 } |
|
136 |
|
137 TPoint CDefaultConsole::CursorPos() const |
|
138 { |
|
139 CreateIfRequired(); |
|
140 return iUnderlyingConsole->CursorPos(); |
|
141 } |
|
142 |
|
143 void CDefaultConsole::SetCursorPosAbs(const TPoint& aPoint) |
|
144 { |
|
145 CreateIfRequired(); |
|
146 iUnderlyingConsole->SetCursorPosAbs(aPoint); |
|
147 } |
|
148 |
|
149 void CDefaultConsole::SetCursorPosRel(const TPoint& aPoint) |
|
150 { |
|
151 CreateIfRequired(); |
|
152 iUnderlyingConsole->SetCursorPosRel(aPoint); |
|
153 } |
|
154 |
|
155 void CDefaultConsole::SetCursorHeight(TInt aPercentage) |
|
156 { |
|
157 CreateIfRequired(); |
|
158 iUnderlyingConsole->SetCursorHeight(aPercentage); |
|
159 } |
|
160 |
|
161 void CDefaultConsole::SetTitle(const TDesC& aTitle) |
|
162 { |
|
163 CreateIfRequired(); |
|
164 iUnderlyingConsole->SetTitle(aTitle); |
|
165 } |
|
166 |
|
167 void CDefaultConsole::ClearScreen() |
|
168 { |
|
169 CreateIfRequired(); |
|
170 iUnderlyingConsole->ClearScreen(); |
|
171 } |
|
172 |
|
173 void CDefaultConsole::ClearToEndOfLine() |
|
174 { |
|
175 CreateIfRequired(); |
|
176 iUnderlyingConsole->ClearToEndOfLine(); |
|
177 } |
|
178 |
|
179 TSize CDefaultConsole::ScreenSize() const |
|
180 { |
|
181 CreateIfRequired(); |
|
182 return iUnderlyingConsole->ScreenSize(); |
|
183 } |
|
184 |
|
185 TKeyCode CDefaultConsole::KeyCode() const |
|
186 { |
|
187 CreateIfRequired(); |
|
188 return iUnderlyingConsole->KeyCode(); |
|
189 } |
|
190 |
|
191 TUint CDefaultConsole::KeyModifiers() const |
|
192 { |
|
193 CreateIfRequired(); |
|
194 return iUnderlyingConsole->KeyModifiers(); |
|
195 } |
|
196 |
|
197 TInt CDefaultConsole::Extension_(TUint aExtensionId, TAny*& a0, TAny* a1) |
|
198 { |
|
199 CreateIfRequired(); |
|
200 return ((CDefaultConsole*)iUnderlyingConsole)->Extension_(aExtensionId, a0, a1); // Evil cast to work around the fact that Extension_ is protected in CConsoleBase. |
|
201 } |
|
202 |
|
203 void CDefaultConsole::CreateIfRequired() |
|
204 { |
|
205 if (iUnderlyingConsole == NULL) |
|
206 { |
|
207 TInt err = KErrGeneral; |
|
208 #ifdef __WINS__ |
|
209 if (EmulatorNoGui()) |
|
210 { |
|
211 err = TryCreateConsole(_L("nullcons")); |
|
212 } |
|
213 else if (EmulatorTextShell()) |
|
214 { |
|
215 err = TryCreateConsole(_L("econs")); |
|
216 } |
|
217 else |
|
218 { |
|
219 #endif |
|
220 for (TInt i = 0; i < KNumConsoleImplementations; ++i) |
|
221 { |
|
222 err = TryCreateConsole(KConsoleImplementations[i]); |
|
223 if (err == KErrNone) |
|
224 { |
|
225 break; |
|
226 } |
|
227 } |
|
228 |
|
229 #ifdef __WINS__ |
|
230 } |
|
231 #endif |
|
232 |
|
233 __ASSERT_ALWAYS(err == KErrNone, Panic(EUnableToCreateConsole)); |
|
234 } |
|
235 } |
|
236 |
|
237 void CDefaultConsole::CreateIfRequired() const |
|
238 { |
|
239 const_cast<CDefaultConsole*>(this)->CreateIfRequired(); |
|
240 } |
|
241 |
|
242 TInt CDefaultConsole::TryCreateConsole(const TDesC& aImplementation) |
|
243 { |
|
244 TInt err = iConsoleLibrary.Load(aImplementation); |
|
245 if ((err == KErrNone) && (iConsoleLibrary.Type()[1] == KSharedLibraryUid) && (iConsoleLibrary.Type()[2] == KConsoleDllUid)) |
|
246 { |
|
247 TLibraryFunction entry = iConsoleLibrary.Lookup(1); |
|
248 CConsoleBase* console = (CConsoleBase*)entry(); |
|
249 if (console) |
|
250 { |
|
251 err = console->Create(*iTitle, iSize); |
|
252 } |
|
253 if (err == KErrNone) |
|
254 { |
|
255 iUnderlyingConsole = console; |
|
256 } |
|
257 else |
|
258 { |
|
259 delete console; |
|
260 iConsoleLibrary.Close(); |
|
261 } |
|
262 } |
|
263 return err; |
|
264 } |
|
265 |
|
266 void CDefaultConsole::Panic(TPanicReason aReason) |
|
267 { |
|
268 _LIT(KCategory, "defcons"); |
|
269 User::Panic(KCategory, aReason); |
|
270 } |
|
271 |
|
272 extern "C" EXPORT_C TAny *NewConsole() |
|
273 { |
|
274 return(new CDefaultConsole); |
|
275 } |