// Implementation: called when read has completed.
void ConsoleUI::RunL()
{
    TKeyCode kc = con_->KeyCode();
    pj_bool_t reschedule = PJ_TRUE;

    switch (kc) {
    case 'w':
	    snd_stop();
	    CActiveScheduler::Stop();
	    reschedule = PJ_FALSE;
	    break;
    case 'a':
    	snd_start(PJMEDIA_DIR_CAPTURE_PLAYBACK);
	break;
    case 't':
    	snd_start(PJMEDIA_DIR_CAPTURE);
	break;
    case 'p':
    	snd_start(PJMEDIA_DIR_PLAYBACK);
    break;
    case 'd':
    	snd_stop();
	break;
    default:
	    PJ_LOG(3,(THIS_FILE, "Keycode '%c' (%d) is pressed",
		      kc, kc));
	    break;
    }

    PrintMenu();

    if (reschedule)
	Run();
}
void UseKernelCpuTime()
{
    test.Start(_L("Create CCpuMeter"));
    CCpuMeter* m = CCpuMeter::New();
    test_NotNull(m);
    TInt iv = 1000500;	// on average 1000.5 ms
    TRequestStatus s;
    CConsoleBase* console = test.Console();
    console->Read(s);
    FOREVER
    {
        User::AfterHighRes(1000000);
        m->Measure();
        m->Display(iv);
        while (s!=KRequestPending)
        {
            User::WaitForRequest(s);
            TKeyCode k = console->KeyCode();
            if (k == EKeyEscape)
            {
                delete m;
                return;
            }
            if (m->iNumCpus > 1)
            {
                // SMP only options
                if (k == EKeySpace)
                    m->DisplayCoreControlInfo();
                else if (k>='1' && k<=('0'+m->iNumCpus))
                    m->ChangeNumberOfCores(k - '0');
            }
            console->Read(s);
        }
    }
}
LOCAL_C void InitConsoleL()
    {
    // create a full screen console object
    CConsoleBase* console;
    console = Console::NewL(KTxtTitle, TSize(KConsFullScreen,KConsFullScreen));
      
    CleanupStack::PushL(console);
  
    //Gets the size of the console
    TSize screenSize = console->ScreenSize();
    test.Printf(_L("Screen size %d %d\r\n"),screenSize.iWidth,screenSize.iHeight);
  
    // Gets the cursor's x-position
    TInt x = console->WhereX();
    // Gets the cursor's y-position
    TInt y = console->WhereY();
    test_Equal(x, 0);
    test_Equal(y, 0);
    test.Printf(_L("**1** Cursor positions x: %d  y: %d\r\n"),x, y);
  
    // Sets the cursor's x-position
    for(TInt i=0; i<4; i++)
        {
        console->SetPos(screenSize.iWidth + i);
        x = console->WhereX();
        test_Equal(x, screenSize.iWidth -3);
		}
    
    test.Printf(_L("**2** Cursor positions x: %d  y: %d\r\n"),x, y);
      
    // Clears the console and set cursor to position 0,0
    console->ClearScreen();
    test_Equal(console->WhereX(), 0);
	test_Equal(console->WhereY(), 0);
        
    // Sets the cursor's x-position and y-position
    for(TInt j=0; j<4; j++)
        {
        console->SetPos(screenSize.iWidth - j, screenSize.iHeight - j);
        x = console->WhereX();
        y = console->WhereY();
        test_Equal(x, screenSize.iWidth -3);
		test_Equal(y, screenSize.iHeight -3);
		}
    test.Printf(_L("**3** Cursor positions x: %d  y: %d\r\n"),x, y);
     
    console->SetPos(0,0);
    x = console->WhereX();
    y = console->WhereY();
    test_Equal(x, 0);
    test_Equal(y, 0);
	test.Printf(_L("**4** Cursor positions x: %d  y: %d\r\n"),x, y);
  
    console->SetPos(screenSize.iWidth/2,screenSize.iHeight/2);
    x = console->WhereX();
    y = console->WhereY();
    test.Printf(_L("**5** Cursor positions x: %d  y: %d\r\n"),x, y);
  
    // Sets the percentage height of the cursor
    console->SetCursorHeight(50);
  
    // Gets the current cursor position relative to the console window
    TPoint cursorPos = console->CursorPos();
    test.Printf(_L("CursorPos iX: %d  iY: %d\r\n"),cursorPos.iX, cursorPos.iY);
    
    // Puts the cursor at the specified position relative
    // to the current cursor position
    TPoint relPos;
    relPos.iX = screenSize.iWidth/4;
    relPos.iY = screenSize.iHeight/4;
    console->SetCursorPosRel(relPos);
    cursorPos = console->CursorPos();
    test.Printf(_L("CursorPosRel iX: %d  iY: %d\r\n"),cursorPos.iX, cursorPos.iY);
  
    // Puts the cursor at the absolute position in the window
    cursorPos.iX = screenSize.iWidth/6;
    cursorPos.iY = screenSize.iHeight/6;
    console->SetCursorPosAbs(cursorPos);
    cursorPos = console->CursorPos();
    test.Printf(_L("CursorPosAbs iX: %d  iY: %d\r\n"),cursorPos.iX, cursorPos.iY);
  
    // Sets a new console title
    console->SetTitle(KTxtNewTitle);
    // Writes the content of the specified descriptor to the console window
    console->Write(KTxtWrite);
    cursorPos.iX = cursorPos.iX + 6;
    console->SetCursorPosAbs(cursorPos);
    // Clears the console from the current cursor position to the end of the line
    console->ClearToEndOfLine();
    // Clears the console and set cursor to position 0,0
    console->ClearScreen();
  
    TUint keyModifiers = console->KeyModifiers();
	test.Printf(_L("keyModifiers %d"),keyModifiers);
    TKeyCode keyCode = console->KeyCode();
    ReadConsole(console);
  
    SimulateKeyPress(EStdKeyEnter);
    keyCode = console->Getch();
  
    // cleanup and return
    CleanupStack::PopAndDestroy(); // close console
    }
TInt GetNumberEntry
(
	const TInt aMaxDigits,	// max numbers of digits
	const TInt aMin,		// min value allowed
	const TInt aMax,		// max value allowed
	const TInt aInputWait,	// (s) how long to wait for each user key entry
	const TInt aDefaultVal,	// default value if timed out
	const TDesC &aPrompt	// string prompt
)
/**
 * This method gets numeric user entry from the console. It checks that the key entered
 * is between 0 to 9 inclusive. This method exits by user hitting the
 * Enter key or timing out.
 *
 * @param aMaxNumbers integer for max numbers of digits
 * @param aMin integer for min value allowed
 * @param aMax integer for max value allowed
 * @param aInputWait integer for number of seconds to wait for each user key entry
 * @param aDefaultVal integer for default value if time-out occurs without
 * @param aPrompt string prompt
 * @return KErrNone.
 */
{
TInt keyFolded;				 // actual digit entered, not TKeyCode
TRequestStatus stat1,stat2;
RTimer timer;
TBool bCorrectEntry = EFalse;
TInt userNum = -1;
TInt limit;
TKeyCode key;

	TTimeIntervalMicroSeconds32 anInterval = aInputWait * 1000000;
	CConsoleBase *pConsole = test.Console();
	timer.CreateLocal();

	while (!bCorrectEntry)
		{
		userNum = -1;
		limit = aMaxDigits;
		key = EKeyNull;

		INFO_PRINTF1(aPrompt); // print prompt
		INFO_PRINTF1(_L(" (range %d-%d) or <CR> for default of %d: "), aMin, aMax, aDefaultVal);
		// exits loop when Enter keyed or limit reached (by decrement to 0)
		while ((key != EKeyEnter) && limit)
			{
			pConsole->Read(stat1);				// set read
			timer.After(stat2, anInterval);		// set wait for this period
			User::WaitForRequest(stat1,stat2);  // whatever comes first

			if(stat1 == KErrNone)					// user entered key
				{
				timer.Cancel();
				User::WaitForRequest(stat2);

				key = pConsole->KeyCode();
				if((key >= '0') && (key <= '9'))
					{
					// valid digit
					keyFolded = (TKeyCode)(key - '0');  // convert to digit
					INFO_PRINTF1(_L("%d"), keyFolded);	// echo
					limit--;			// tracks number of digits

					// "append" to number
					if (-1 == userNum)	   // ie first char entered
						{
						userNum = keyFolded;
						}
					else					//  next char entered
						{
						userNum = userNum * 10 + keyFolded;  // shift
						}
					}
				}
			else	// timer went off, use default unless valid key entered before timer expired
				{
				pConsole->ReadCancel();
				User::WaitForRequest(stat1);
				if (-1 == userNum)
					{
					// no value entered before timer went off
					userNum = aDefaultVal;
					}
				break;
				}
			} // endwhile

		test.Printf (_L("\n"));
		if ((userNum >= aMin) && (userNum <= aMax))
			{
			bCorrectEntry = ETrue;  // exit loop
			}
		else						// return to loop
			{
			if (userNum == -1)
				{
				// <CR> was entered before any numbers, so use default
				userNum = aDefaultVal;
				break;
				}
			INFO_PRINTF1(_L("Try again, Entry out of limit. Must be between %d and %d inclusive.\n\n"), aMin, aMax);
			}
		} // endwhile

	return (userNum);
}