symport/e32/euser/cbase/ub_cons.cpp
changeset 1 0a7b44b10206
child 2 806186ab5e14
equal deleted inserted replaced
0:c55016431358 1:0a7b44b10206
       
     1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32\euser\cbase\ub_cons.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "ub_std.h"
       
    19 #ifndef __TOOLS2__
       
    20 #include <e32uid.h>
       
    21 #include <e32wins.h>
       
    22 #else
       
    23 #if defined _WIN32
       
    24 #include <cstdio>
       
    25 #else
       
    26 #include <stdio.h>
       
    27 #include <wchar.h>
       
    28 #endif
       
    29 #endif
       
    30 
       
    31 
       
    32 /**
       
    33 Default constructor.
       
    34 */
       
    35 EXPORT_C CConsoleBase::CConsoleBase()
       
    36 	{
       
    37 	}
       
    38 
       
    39 
       
    40 
       
    41 /**
       
    42 Destructor.
       
    43 */
       
    44 EXPORT_C CConsoleBase::~CConsoleBase()
       
    45 	{
       
    46 	}
       
    47 
       
    48 
       
    49 
       
    50 
       
    51 /**
       
    52 Gets a character from the console.
       
    53 
       
    54 @return the key code from the console.
       
    55 */
       
    56 EXPORT_C TKeyCode CConsoleBase::Getch()
       
    57 	{
       
    58 
       
    59 	TRequestStatus s;
       
    60 	Read(s);
       
    61 	User::WaitForRequest(s);
       
    62 	__ASSERT_ALWAYS(s==KErrNone,Panic(EConsGetchFailed));
       
    63 	return(KeyCode());
       
    64 	}
       
    65 
       
    66 
       
    67 
       
    68 
       
    69 /**
       
    70 Prints characters to the console window.
       
    71 
       
    72 @param aFmt             The  non-modifiable descriptor containing the
       
    73                         format string. The TRefByValue class provides a
       
    74                         constructor which takes a TDesC type. 
       
    75 
       
    76 @param ...              A variable number of arguments to be converted to text
       
    77                         as dictated by the format string. 
       
    78 */
       
    79 EXPORT_C void CConsoleBase::Printf(TRefByValue<const TDesC> aFmt,...)
       
    80 	{
       
    81 
       
    82 	TestOverflowTruncate overflow;
       
    83 	VA_LIST list;
       
    84 	VA_START(list,aFmt);
       
    85 	TBuf<0x100> aBuf;
       
    86 	aBuf.AppendFormatList(aFmt,list,&overflow);
       
    87 	Write(aBuf);
       
    88 	}
       
    89 
       
    90 
       
    91 
       
    92 /**
       
    93 Sets the cursor's x-position.
       
    94 
       
    95 @param aX The x-position.
       
    96 */
       
    97 EXPORT_C void CConsoleBase::SetPos(TInt aX)
       
    98 	{
       
    99 
       
   100 	SetCursorPosAbs(TPoint(aX,WhereY()));
       
   101 	}
       
   102 
       
   103 
       
   104 
       
   105 
       
   106 /**
       
   107 Sets the cursor's x-position and y-position.
       
   108 
       
   109 @param aX The x-position.
       
   110 @param aY The y-position.
       
   111 */
       
   112 EXPORT_C void CConsoleBase::SetPos(TInt aX,TInt aY)
       
   113 	{
       
   114 
       
   115 	SetCursorPosAbs(TPoint(aX,aY));
       
   116 	}
       
   117 
       
   118 
       
   119 
       
   120 
       
   121 /**
       
   122 Gets the cursor's x-position.
       
   123 
       
   124 @return The cursor's x-position.
       
   125 */
       
   126 EXPORT_C TInt CConsoleBase::WhereX() const
       
   127 	{
       
   128 
       
   129 	return(CursorPos().iX);
       
   130 	}
       
   131 
       
   132 
       
   133 
       
   134 /**
       
   135 Gets the cursor's y-position.
       
   136 
       
   137 @return The cursor's y-position.
       
   138 */
       
   139 EXPORT_C TInt CConsoleBase::WhereY() const
       
   140 	{
       
   141 
       
   142 	return(CursorPos().iY);
       
   143 	}
       
   144 
       
   145 
       
   146 /**
       
   147 Extension function
       
   148 
       
   149 
       
   150 */
       
   151 EXPORT_C TInt CConsoleBase::Extension_(TUint aExtensionId, TAny*& a0, TAny* a1)
       
   152 	{
       
   153 	return CBase::Extension_(aExtensionId, a0, a1);
       
   154 	}
       
   155 
       
   156 
       
   157 #ifndef __TOOLS2__
       
   158 
       
   159 void CColorConsoleBase::SetTextAttribute(TTextAttribute /*anAttribute*/)
       
   160 //
       
   161 //
       
   162 //
       
   163 	{
       
   164 	}
       
   165 
       
   166 
       
   167 /**
       
   168 Extension function
       
   169 
       
   170 
       
   171 */
       
   172 EXPORT_C TInt CColorConsoleBase::Extension_(TUint aExtensionId, TAny*& a0, TAny* a1)
       
   173 	{
       
   174 	return CConsoleBase::Extension_(aExtensionId, a0, a1);
       
   175 	}
       
   176 
       
   177 
       
   178 NONSHARABLE_CLASS(CProxyConsole) : public CColorConsoleBase
       
   179 	{
       
   180 public:
       
   181 	~CProxyConsole();
       
   182 	TInt Construct(const TDesC& aImplDll);
       
   183 public:
       
   184 // implement for CConsoleBase
       
   185 	TInt Create(const TDesC &aTitle,TSize aSize);
       
   186 	void Read(TRequestStatus &aStatus);
       
   187 	void ReadCancel();
       
   188 	void Write(const TDesC &aDes);
       
   189 	TPoint CursorPos() const;
       
   190 	void SetCursorPosAbs(const TPoint &aPoint);
       
   191 	void SetCursorPosRel(const TPoint &aPoint);
       
   192 	void SetCursorHeight(TInt aPercentage);
       
   193 	void SetTitle(const TDesC &aTitle);
       
   194 	void ClearScreen();
       
   195 	void ClearToEndOfLine();
       
   196 	TSize ScreenSize() const;
       
   197 	TKeyCode KeyCode() const;
       
   198 	TUint KeyModifiers() const;
       
   199 // implement for CColorConsoleBase
       
   200 	void SetTextAttribute(TTextAttribute anAttribute); 
       
   201 	virtual TInt Extension_(TUint aExtensionId, TAny*& a0, TAny* a1);
       
   202 private:
       
   203 	RLibrary iLib;
       
   204 	CColorConsoleBase* iConsole;
       
   205 	};
       
   206 
       
   207 TInt CProxyConsole::Construct(const TDesC& aImplDll)
       
   208 	{
       
   209 	const TUidType type(KNullUid, KSharedLibraryUid, KConsoleDllUid);
       
   210 	TInt r=iLib.Load(aImplDll,type);
       
   211 	if (r==KErrNone)
       
   212 		{
       
   213 		iConsole=(CColorConsoleBase*)(iLib.Lookup(1)());
       
   214 		if (!iConsole)
       
   215 			r=KErrNoMemory;
       
   216 		}
       
   217 	return r;
       
   218 	}
       
   219 
       
   220 CProxyConsole::~CProxyConsole()
       
   221 	{
       
   222 	delete iConsole;
       
   223 	iLib.Close();
       
   224 	}
       
   225 
       
   226 TInt CProxyConsole::Create(const TDesC &aTitle,TSize aSize)
       
   227 	{
       
   228 	return iConsole->Create(aTitle,aSize);
       
   229 	}
       
   230 	
       
   231 void CProxyConsole::Read(TRequestStatus &aStatus)
       
   232 	{
       
   233 	iConsole->Read(aStatus);
       
   234 	}
       
   235 	
       
   236 void CProxyConsole::ReadCancel()
       
   237 	{
       
   238 	iConsole->ReadCancel();
       
   239 	}
       
   240 	
       
   241 void CProxyConsole::Write(const TDesC &aDes)
       
   242 	{
       
   243 	iConsole->Write(aDes);
       
   244 	}
       
   245 	
       
   246 TPoint CProxyConsole::CursorPos() const
       
   247 	{
       
   248 	return iConsole->CursorPos();
       
   249 	}
       
   250 	
       
   251 void CProxyConsole::SetCursorPosAbs(const TPoint &aPoint)
       
   252 	{
       
   253 	iConsole->SetCursorPosAbs(aPoint);
       
   254 	}
       
   255 	
       
   256 void CProxyConsole::SetCursorPosRel(const TPoint &aPoint)
       
   257 	{
       
   258 	iConsole->SetCursorPosRel(aPoint);
       
   259 	}
       
   260 	
       
   261 void CProxyConsole::SetCursorHeight(TInt aPercentage)
       
   262 	{
       
   263 	iConsole->SetCursorHeight(aPercentage);
       
   264 	}
       
   265 	
       
   266 void CProxyConsole::SetTitle(const TDesC &aTitle)
       
   267 	{
       
   268 	iConsole->SetTitle(aTitle);
       
   269 	}
       
   270 	
       
   271 void CProxyConsole::ClearScreen()
       
   272 	{
       
   273 	iConsole->ClearScreen();
       
   274 	}
       
   275 	
       
   276 void CProxyConsole::ClearToEndOfLine()
       
   277 	{
       
   278 	iConsole->ClearToEndOfLine();
       
   279 	}
       
   280 	
       
   281 TSize CProxyConsole::ScreenSize() const
       
   282 	{
       
   283 	return iConsole->ScreenSize();
       
   284 	}
       
   285 	
       
   286 TKeyCode CProxyConsole::KeyCode() const
       
   287 	{
       
   288 	return iConsole->KeyCode();
       
   289 	}
       
   290 	
       
   291 TUint CProxyConsole::KeyModifiers() const
       
   292 	{
       
   293 	return iConsole->KeyModifiers();
       
   294 	}
       
   295 	
       
   296 void CProxyConsole::SetTextAttribute(TTextAttribute anAttribute)
       
   297 	{
       
   298 	iConsole->SetTextAttribute(anAttribute);
       
   299 	}
       
   300 
       
   301 TInt CProxyConsole::Extension_(TUint aExtensionId, TAny*& a0, TAny* a1)
       
   302 	{
       
   303 	return iConsole->Extension_(aExtensionId, a0, a1);
       
   304 	}
       
   305 
       
   306 _LIT(KConsImpl,"econs");
       
   307 #ifdef __WINS__
       
   308 _LIT(KConsGuiImpl,"econseik");
       
   309 _LIT(KConsNoGuiImpl,"econsnogui");
       
   310 #endif
       
   311 
       
   312 
       
   313 
       
   314 /**
       
   315 Creates a new console object.
       
   316 
       
   317 @param aTitle The title text for the console.
       
   318               This should not be longer than 256 characters.
       
   319 @param aSize  The size of the console window.
       
   320 
       
   321 @return A pointer to the new console object.
       
   322 
       
   323 @see CConsoleBase::Create() 
       
   324 */
       
   325 EXPORT_C CConsoleBase *Console::NewL(const TDesC &aTitle,TSize aSize)
       
   326 	{
       
   327 	CProxyConsole *pC=new(ELeave) CProxyConsole;
       
   328 	TInt r=pC->Construct(KConsImpl);
       
   329 	if (r==KErrNone)
       
   330 		r=pC->Create(aTitle,aSize);
       
   331 #ifdef __WINS__
       
   332 	if (r!=KErrNone)
       
   333 		{
       
   334 		delete pC;
       
   335 		pC=new(ELeave) CProxyConsole;
       
   336 		if (EmulatorNoGui())
       
   337 			{
       
   338 			// try and create a dummy console via ECONSNOGUI
       
   339 			r=pC->Construct(KConsNoGuiImpl);
       
   340 			}
       
   341 		else
       
   342 			{
       
   343 			// try and create a GUI console via ECONSEIK instead
       
   344 			r=pC->Construct(KConsGuiImpl);
       
   345 			}
       
   346 		if (r==KErrNone)
       
   347 			r=pC->Create(aTitle,aSize);
       
   348 		}
       
   349 #endif
       
   350 	if (r!=KErrNone)
       
   351 		{
       
   352 		delete pC;
       
   353 		User::Leave(r);
       
   354 		}
       
   355 	return(pC);
       
   356 	}
       
   357 #else // __TOOLS2__
       
   358 class CStdConsole : public CConsoleBase
       
   359 	{
       
   360 public:
       
   361 	CStdConsole() {};
       
   362 	~CStdConsole() {};
       
   363 	//
       
   364 	TInt Create(const TDesC &aTitle,TSize aSize) { return KErrNone; };
       
   365 	void ReadCancel() {};
       
   366 	TPoint CursorPos() const { return TPoint(0,0); };
       
   367 	void SetCursorPosAbs(const TPoint &aPoint) {};
       
   368 	void SetCursorPosRel(const TPoint &aPoint) {};
       
   369 	void SetCursorHeight(TInt aPercentage) {};
       
   370 	void SetTitle(const TDesC &aTitle) {};
       
   371 	void ClearScreen() {};
       
   372 	void ClearToEndOfLine() {};
       
   373 	TSize ScreenSize() const { return TSize(0,0); };
       
   374 	TUint KeyModifiers() const { return 0; };
       
   375 	//
       
   376 	void Read(TRequestStatus &aStatus);
       
   377 	void Write(const TDesC &aDes);
       
   378 	TKeyCode KeyCode() const { return iKey; };
       
   379 	//
       
   380 private:
       
   381 	TKeyCode iKey;
       
   382 	TBuf16<256> iBuf;
       
   383 	};
       
   384 
       
   385 void CStdConsole::Read(TRequestStatus &aStatus)
       
   386 	{
       
   387 	iKey = TKeyCode(getchar());
       
   388 	aStatus = KErrNone;
       
   389 	}
       
   390 
       
   391 void CStdConsole::Write(const TDesC &aDes)
       
   392 	{
       
   393 	TInt pos = 0;
       
   394 #ifdef _WIN32
       
   395 	while(pos < aDes.Length())
       
   396 		{
       
   397 		TInt len = Min(aDes.Length() - pos, iBuf.MaxLength() - 1);
       
   398 		iBuf.Copy(aDes.Mid(pos, len));
       
   399 		
       
   400 		pos += iBuf.Length();
       
   401 		wprintf(reinterpret_cast<const wchar_t*>(iBuf.PtrZ()));
       
   402 		}
       
   403 #else
       
   404 	while(pos < aDes.Length())
       
   405 	{
       
   406 		putchar(aDes[pos++]);
       
   407 	}
       
   408 #endif
       
   409 	}
       
   410 
       
   411 EXPORT_C CConsoleBase *Console::NewL(const TDesC &aTitle, TSize aSize)
       
   412 	{
       
   413 	return new(ELeave)CStdConsole;
       
   414 	}
       
   415 #endif // __TOOLS2__