core/src/console.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // console.cpp
       
     2 // 
       
     3 // Copyright (c) 2006 - 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 <e32cons.h>
       
    14 #include <fshell/ioutils.h>
       
    15 #include "console.h"
       
    16 
       
    17 
       
    18 //
       
    19 // CConsoleReader.
       
    20 //
       
    21 
       
    22 CConsoleReader* CConsoleReader::NewL(RIoConsoleReadHandle& aStdin, MConsoleObserver& aObserver)
       
    23 	{
       
    24 	CConsoleReader* self = new(ELeave) CConsoleReader(aStdin, aObserver);
       
    25 	self->Queue();
       
    26 	return self;
       
    27 	}
       
    28 
       
    29 CConsoleReader::~CConsoleReader()
       
    30 	{
       
    31 	Cancel();
       
    32 	}
       
    33 
       
    34 CConsoleReader::CConsoleReader(RIoConsoleReadHandle& aStdin, MConsoleObserver& aObserver)
       
    35 	: CActive(CActive::EPriorityStandard), iStdin(aStdin), iObserver(aObserver)
       
    36 	{
       
    37 	CActiveScheduler::Add(this);
       
    38 	}
       
    39 
       
    40 void CConsoleReader::Queue()
       
    41 	{
       
    42 	TInt result = iStdin.AttachedToConsole();
       
    43 	if (result > 0)
       
    44 		{
       
    45 		iRawRead = ETrue;
       
    46 		iStdin.Read(iRawReadBuf, iStatus);
       
    47 		}
       
    48 	else if (result == 0)
       
    49 		{
       
    50 		iRawRead = EFalse;
       
    51 		iStdin.WaitForKey(iStatus);
       
    52 		}
       
    53 	else
       
    54 		{
       
    55 		// This seems to happen if the console failed to construct
       
    56 		// [I've seen a E32USER-CBase 45 panic as a result of this call so possibly it's not always the right thing to do (I added this in the first place). -TomS]
       
    57 		iObserver.CoHandleError(result);
       
    58 		return;
       
    59 		}
       
    60 	SetActive();
       
    61 	}
       
    62 
       
    63 void CConsoleReader::RunL()
       
    64 	{
       
    65 	TInt err = iStatus.Int();
       
    66 	if (err)
       
    67 		{
       
    68 		iObserver.CoHandleError(err);
       
    69 		}
       
    70 	else
       
    71 		{
       
    72 		TUint keyCode = iRawRead ? iRawReadBuf[0] : iStdin.KeyCode();
       
    73 		TUint modifiers = iRawRead ? 0 : iStdin.KeyModifiers();
       
    74 		iObserver.CoHandleKey(keyCode, modifiers);
       
    75 		}
       
    76 	Queue();
       
    77 	}
       
    78 
       
    79 void CConsoleReader::DoCancel()
       
    80 	{
       
    81 	if (iRawRead)
       
    82 		{
       
    83 		iStdin.ReadCancel();
       
    84 		}
       
    85 	else
       
    86 		{
       
    87 		iStdin.WaitForKeyCancel();
       
    88 		}
       
    89 	}