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