os/graphics/windowing/windowserver/minigui/src/econswserv.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2008-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 //
    15 
    16 #include <e32cons.h>
    17 #include <e32keys.h>
    18 #include <e32base.h>
    19 #include <w32std.h>
    20 #include <gdi.h>
    21 #include <bitstd.h>
    22 #include <bitdev.h>
    23 
    24 const TInt KGroupId = 0x100039e7;
    25 
    26 class CWsConsoleMux;
    27 class CWsConsole;
    28 
    29 // Active object to listen to Wserv events. Only used when
    30 // application thread has installed an active scheduler
    31 //
    32 NONSHARABLE_CLASS(CWsKeyReader): public CActive
    33 	{
    34 public:
    35 	CWsKeyReader(CWsConsoleMux* aMux);
    36 	void MakeRequest();
    37 
    38 protected:
    39 	void RunL();
    40 	void DoCancel();
    41 
    42 private:
    43 	CWsConsoleMux* iMux; //uses
    44 	};
    45 
    46 // Master console to dispatch key events to relevant console in situation where 
    47 // a single application thread owns multiple instances of console (CConsoleBase).
    48 // Reference counted as only a single instance of this class will be created
    49 // for each thread.
    50 //
    51 NONSHARABLE_CLASS(CWsConsoleMux): public CBase
    52 	{
    53 public:
    54 	static TInt Open();
    55 	static void Close();
    56 	static CWsConsoleMux* Static();
    57 	~CWsConsoleMux();
    58 	
    59 	void MakeRequest(CWsConsole* aConsole);
    60 	void CancelRequest(CWsConsole* aConsole);
    61 	void RequestComplete();
    62 
    63 	inline RWsSession& Session();
    64 	inline RWindowGroup& Group();
    65 	inline CWindowGc& Gc();
    66 	inline CWsScreenDevice& Screen();
    67 
    68 private:
    69 	static CWsConsoleMux* NewL();
    70 	CWsConsoleMux();
    71 	void ConstructL();
    72 
    73 private:
    74 	TInt iRefCount;
    75 	RWsSession iWs;
    76 	RWindowGroup iGroup;
    77 	CWsScreenDevice* iScr;
    78 	CWindowGc* iGc;
    79 
    80 	CWsConsole* iConsole;
    81 	CWsKeyReader* iReader;
    82 	};
    83 
    84 // Console that uses direct Wserv API. Each console will own a window and 
    85 // a bitmap for its backing store, but will be sharing Wserv session, window group,
    86 // screen device and window drawing context
    87 //
    88 NONSHARABLE_CLASS(CWsConsole): public CConsoleBase
    89 	{
    90 	friend class CWsConsoleMux;
    91 
    92 public:
    93 	CWsConsole();
    94 	virtual ~CWsConsole();
    95 	
    96 	//from CConsoleBase 
    97 	virtual TInt Create(const TDesC &aTitle,TSize aSize);
    98 	virtual void Read(TRequestStatus &aStatus);
    99 	virtual void ReadCancel();
   100 	virtual void Write(const TDesC &aDes);
   101 	virtual TPoint CursorPos() const;
   102 	virtual void SetCursorPosAbs(const TPoint &aPoint);
   103 	virtual void SetCursorPosRel(const TPoint &aPoint);
   104 	virtual void SetCursorHeight(TInt aPercentage);
   105 	virtual void SetTitle(const TDesC &aTitle);
   106 	virtual void ClearScreen();
   107 	virtual void ClearToEndOfLine();
   108 	virtual TSize ScreenSize() const;
   109 	virtual TKeyCode KeyCode() const;
   110 	virtual TUint KeyModifiers() const;
   111 
   112 private:	
   113 	// for CWsConsoleMux
   114 	TRequestStatus* ReadStatus();
   115 	void SetKey(TUint aCode, TUint aModifier);
   116 
   117 	// internal
   118 	void ConstructL();
   119 	void DrawNow();
   120 	void Scroll();
   121 	TPoint WritePos();
   122 	void CarriageReturn();
   123 	void LineFeed();
   124 	void Left();
   125 	void Right();
   126 	void WriteChar(TText);
   127 	
   128 private:
   129 	// window stuff
   130 	RWindow iWin;
   131 	TSize iWindowSize;
   132 
   133 	// back buffer
   134 	CFbsBitmap* iBitmap;
   135 	CFbsBitGc* iBitGc;
   136 	CFbsBitmapDevice* iBitDev;
   137 
   138 	// console stuff in chars dimension
   139 	TPoint iCursorPos; //The position of the cursor is on the baseline
   140 	TSize iSizeInChars;
   141 
   142 	// font stuff
   143 	CFont* iFont;
   144 	TSize iCharSize;
   145 	TInt iFontAscent;
   146 	TInt iFontDescent;
   147 	
   148 	TKeyCode iKeyCode;
   149 	TUint iModifiers;
   150 	TTextCursor iTextCursor;
   151 	
   152 	// master console
   153 	CWsConsoleMux* iMux;
   154 	TRequestStatus* iReadStatus;
   155 	};
   156 
   157 //
   158 // CWsKeyReader implementation
   159 //
   160 //
   161 //
   162 CWsKeyReader::CWsKeyReader(CWsConsoleMux* aMux):
   163 	CActive(CActive::EPriorityStandard),
   164 	iMux(aMux)	
   165 	{
   166 	CActiveScheduler::Add(this);
   167 	}
   168 
   169 void CWsKeyReader::MakeRequest()
   170 	{
   171 	SetActive();
   172 	iMux->Session().EventReady(&iStatus);
   173 	}
   174 
   175 void CWsKeyReader::RunL()
   176 	{
   177 	// let master console decide what to do
   178 	iMux->RequestComplete();
   179 	}
   180 
   181 void CWsKeyReader::DoCancel()
   182 	{
   183 	iMux->Session().EventReadyCancel();
   184 	}
   185 
   186 //
   187 // CWsConsoleMux implementation
   188 //
   189 //
   190 //
   191 TInt CWsConsoleMux::Open()
   192 	{
   193 	CWsConsoleMux* mux = (CWsConsoleMux*)Dll::Tls();
   194 	
   195 	// not the first time, simply increment refcount
   196 	if (mux)
   197 		{
   198 		++mux->iRefCount;
   199 		return KErrNone;
   200 		}
   201 
   202 	// first time, let's create master console and
   203 	// stick it to TLS
   204 	TRAPD(err, mux = CWsConsoleMux::NewL());
   205 	if (err != KErrNone)
   206 		{
   207 		return err;
   208 		}
   209 
   210 	err = Dll::SetTls(mux);
   211 	if (err != KErrNone)
   212 		{
   213 		delete mux;
   214 		return err;
   215 		}
   216 
   217 	++mux->iRefCount;			
   218 	return KErrNone;
   219 	}
   220 
   221 void CWsConsoleMux::Close()
   222 	{
   223 	CWsConsoleMux* mux = (CWsConsoleMux*)Dll::Tls();
   224 	if (!mux)
   225 		{
   226 		return;
   227 		}
   228 		
   229 	// destroy master console if this is the last use from
   230 	// this thread and reset TLS
   231 	if (--mux->iRefCount == 0)
   232 		{
   233 		delete mux;
   234 		Dll::FreeTls();
   235 		}
   236 	}
   237 
   238 CWsConsoleMux* CWsConsoleMux::Static()
   239 	{
   240 	return (CWsConsoleMux*)Dll::Tls();
   241 	}
   242 
   243 CWsConsoleMux::CWsConsoleMux()
   244 	{
   245 	}
   246 	
   247 CWsConsoleMux::~CWsConsoleMux()
   248 	{
   249 	delete iReader;
   250 	delete iGc;
   251 	delete iScr;
   252 	iGroup.Close();
   253 	iWs.Close();
   254 	}
   255 
   256 CWsConsoleMux* CWsConsoleMux::NewL()
   257 	{
   258 	CWsConsoleMux* mux = new(ELeave) CWsConsoleMux;
   259 	CleanupStack::PushL(mux);
   260 	mux->ConstructL();
   261 	CleanupStack::Pop(mux);
   262 	
   263 	return mux;
   264 	}
   265 	
   266 void CWsConsoleMux::ConstructL()
   267 	{
   268 	TInt err = iWs.Connect();
   269 	User::LeaveIfError(err);
   270 	
   271 	iScr = new(ELeave) CWsScreenDevice(iWs);
   272 	err = iScr->Construct();
   273 	User::LeaveIfError(err);
   274 	
   275 	iGc = new(ELeave) CWindowGc(iScr);
   276 	err = iGc->Construct();
   277 	User::LeaveIfError(err);
   278 	
   279 	iGroup = RWindowGroup(iWs);
   280 	err = iGroup.Construct(KGroupId, ETrue);
   281 	User::LeaveIfError(err);
   282 	
   283 	// we will be using active object to listen to Wserv events
   284 	// only if this thread already has active scheduler
   285 	//
   286 	if (CActiveScheduler::Current())
   287 		{
   288 		iReader = new(ELeave) CWsKeyReader(this);
   289 		}
   290 	}
   291 
   292 RWsSession& CWsConsoleMux::Session()
   293  	{
   294  	return iWs;
   295  	}
   296  	
   297 RWindowGroup& CWsConsoleMux::Group()
   298 	{
   299 	return iGroup;
   300 	}
   301 	
   302 CWindowGc& CWsConsoleMux::Gc()
   303 	{
   304 	return *iGc;
   305 	}
   306 	
   307 CWsScreenDevice& CWsConsoleMux::Screen()
   308 	{
   309 	return *iScr;
   310 	}
   311 
   312 void CWsConsoleMux::MakeRequest(CWsConsole* aConsole)
   313 	{
   314 	if (!iReader)
   315 		{
   316 		// client does not have scheduler, ideally we should use
   317 		// secondary thread to make this asynchronous.
   318 		//
   319 		// however, we can get away with this because application 
   320 		// that doesn't use active scheduler will be calling asynchronous console read 
   321 		// and immediately followed by WaitForRequest e.g.
   322 		//
   323 		// while (1)
   324 		//	{
   325 		//	...
   326 		//	console->Read(status);
   327 		//	User::WaitForRequest(status);
   328 		//	...
   329 		//	}
   330 		//
   331 		TWsEvent event;
   332 
   333 		// keep going until we got key event
   334 		do
   335 			{
   336 			TRequestStatus s;
   337 			s = KRequestPending;
   338 			iWs.EventReady(&s);
   339 			User::WaitForRequest(s);
   340 			
   341 			iWs.GetEvent(event);
   342 			}
   343 		while (event.Type() != EEventKey);
   344 		
   345 		aConsole->SetKey(event.Key()->iCode, event.Key()->iModifiers);
   346 		TRequestStatus* pS = aConsole->ReadStatus();
   347 		User::RequestComplete(pS, KErrNone);
   348 		}
   349 	else
   350 		{
   351 		iConsole = aConsole;
   352 		iReader->MakeRequest();
   353 		}
   354 	}
   355 
   356 // called from key listener RunL
   357 //
   358 void CWsConsoleMux::RequestComplete()
   359 	{
   360 	TWsEvent event;
   361 	iWs.GetEvent(event);
   362 
   363 	if (event.Type() == EEventKey)
   364 		{
   365 		iConsole->SetKey(event.Key()->iCode, event.Key()->iModifiers);
   366 		TRequestStatus* pS = iConsole->ReadStatus();
   367 		User::RequestComplete(pS, KErrNone);
   368 		iConsole = NULL;
   369 		}
   370 	else
   371 		{
   372 		// keep going until we got key event
   373 		//
   374 		iReader->MakeRequest();
   375 		}
   376 	}
   377 
   378 void CWsConsoleMux::CancelRequest(CWsConsole* aConsole)
   379 	{
   380 	if (iReader && iConsole == aConsole)
   381 		{
   382 		iReader->Cancel();
   383 		}
   384 	}
   385 	
   386 //
   387 // CWsConsole implementation
   388 //
   389 //
   390 //
   391 CWsConsole::CWsConsole()
   392 	{
   393 	}
   394 
   395 CWsConsole::~CWsConsole()
   396 	{
   397 	if(iBitGc)
   398 		{
   399 		iBitGc->DiscardFont();
   400 		}
   401 	if(iBitDev)
   402 		{
   403 		iBitDev->ReleaseFont(iFont);
   404 		}
   405 
   406 	delete iBitGc;
   407 	delete iBitDev;
   408 	delete iBitmap;	
   409 	iWin.Close();
   410 	iMux->Session().Flush();
   411 
   412 	CWsConsoleMux::Close();
   413 	}
   414 
   415 TInt CWsConsole::Create(const TDesC& /*aTitle*/, TSize /*aSize*/)
   416 	{
   417 	// we must not push this object to cleanup stack during construction because
   418 	// caller will explicitly call delete on this object when Create() returns error
   419 	//
   420 	TRAPD(err, ConstructL());
   421 	return err;
   422 	}
   423 	
   424 void CWsConsole::ConstructL()
   425 	{
   426 	TInt err = CWsConsoleMux::Open();
   427 	User::LeaveIfError(err);
   428 
   429 	iMux = CWsConsoleMux::Static();
   430 	
   431 	iWindowSize = iMux->Screen().SizeInPixels();
   432 	
   433 	iBitmap = new(ELeave) CFbsBitmap;
   434 	err = iBitmap->Create(iWindowSize, EGray2);
   435 	User::LeaveIfError(err);
   436 
   437 	iBitDev = CFbsBitmapDevice::NewL(iBitmap);
   438 	err = iBitDev->CreateContext(iBitGc);
   439 	User::LeaveIfError(err);
   440 
   441 	iBitGc->SetPenColor(KRgbBlack);
   442 	iBitGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
   443 	iBitGc->SetBrushColor(KRgbWhite);
   444 	iBitGc->Clear();
   445 
   446 	TFontSpec fs(_L("DejaVu Sans Mono"), 12);
   447 	err = iBitDev->GetNearestFontToDesignHeightInPixels(iFont, fs);
   448 	User::LeaveIfError(err);
   449 
   450 	iBitGc->UseFont(iFont);
   451 
   452 	// uses monospace font to get uniform glpyh size
   453 	iCharSize.iHeight = iFont->FontMaxAscent() + iFont->FontMaxDescent();
   454 	iCharSize.iWidth = iFont->MaxCharWidthInPixels();
   455 	iFontAscent = iFont->FontMaxAscent();
   456 	iFontDescent= iFont->FontMaxDescent();
   457 
   458 	// cursor represent char position in character-based area
   459 	// eshell is using 0,0 based
   460 	iCursorPos = TPoint(1,1);
   461 	// console size in number of characters and lines
   462 	iSizeInChars.iWidth = iWindowSize.iWidth / iCharSize.iWidth;
   463 	iSizeInChars.iHeight = iWindowSize.iHeight / iCharSize.iHeight;
   464 	
   465 	iTextCursor.iType = TTextCursor::ETypeRectangle;
   466 	iTextCursor.iHeight = iCharSize.iHeight;
   467 	iTextCursor.iAscent = iFontAscent;
   468 	iTextCursor.iWidth = iCharSize.iWidth;
   469 	iTextCursor.iFlags = 0;
   470 	iTextCursor.iColor = KRgbWhite;
   471 
   472 
   473 	iWin = RWindow(iMux->Session());
   474 	err = iWin.Construct(iMux->Group(), (TInt)this);
   475 	User::LeaveIfError(err);
   476 		
   477 	iWin.Activate();
   478 	iMux->Session().Flush();
   479 	}
   480 
   481 TRequestStatus* CWsConsole::ReadStatus()
   482 	{
   483 	return iReadStatus;
   484 	}
   485 
   486 void CWsConsole::SetKey(TUint aCode, TUint aModifier) 
   487 	{
   488 	iKeyCode = (TKeyCode)aCode;
   489 	iModifiers = aModifier;
   490 	}
   491 
   492 TPoint CWsConsole::WritePos()
   493 	{
   494 	return TPoint((iCursorPos.iX - 1) * iCharSize.iWidth, (iCursorPos.iY - 1) * iCharSize.iHeight + iFontAscent);
   495 	}
   496 	
   497 void CWsConsole::Read(TRequestStatus &aStatus)
   498 	{
   499 	iReadStatus = &aStatus;
   500 	*iReadStatus = KRequestPending;
   501 	iMux->MakeRequest(this);
   502 	}
   503 
   504 void CWsConsole::ReadCancel()
   505 	{
   506 	iMux->CancelRequest(this);
   507 	iReadStatus = NULL;
   508 	}
   509 
   510 void CWsConsole::CarriageReturn()
   511 	{
   512 	iCursorPos.iX = 1;
   513 	}
   514 
   515 void CWsConsole::LineFeed()
   516 	{
   517 	CarriageReturn();
   518 	if (iCursorPos.iY < iSizeInChars.iHeight)
   519 		{
   520 		++iCursorPos.iY;
   521 		}
   522 	else
   523 		{
   524 		Scroll();
   525 		}
   526 	}
   527 
   528 void CWsConsole::Left()
   529 	{
   530 	if (iCursorPos != TPoint(1,1))
   531 		{
   532 		if (iCursorPos.iX == 1)
   533 			{
   534 			iCursorPos.iX += iSizeInChars.iWidth;
   535 			--iCursorPos.iY;
   536 			}
   537 		--iCursorPos.iX;
   538 		}
   539 	}
   540 
   541 void CWsConsole::Right()
   542 	{
   543 	++iCursorPos.iX;
   544 	if (iCursorPos.iX > iSizeInChars.iWidth)
   545 		{
   546 		LineFeed();
   547 		}
   548 	}
   549 
   550 void CWsConsole::Write(const TDesC& aDes)
   551 	{
   552 	const TInt length = aDes.Length();
   553 	if (length == 0)
   554 		{
   555 		return;
   556 		}
   557 	
   558 	for (TInt i=0; i<length; ++i)
   559 		{
   560 		switch (aDes[i])
   561 			{
   562 			case 0x07:
   563 				RDebug::Print(_L("WSCON: End of command line!"));
   564 				break;
   565 			case 0x0a:
   566 				LineFeed();
   567 				break;
   568 			case 0x0d:
   569 				CarriageReturn();
   570 				break;
   571 			default:
   572 				WriteChar(aDes[i]);
   573 				break;
   574 			}
   575 		}
   576 	
   577 	DrawNow();
   578 	}
   579 
   580 void CWsConsole::WriteChar(TText aChar)
   581 	{
   582 	TBuf<1> s;
   583 	s.Append(aChar);
   584 	// use draw text box to clear the glpyh box
   585 	TPoint writePos = WritePos();
   586 	TRect r(writePos.iX, writePos.iY-iFontAscent,writePos.iX+iCharSize.iWidth, writePos.iY+iFontDescent);
   587 	iBitGc->DrawText(s, r, iFontAscent);
   588 	Right();
   589 	}
   590 
   591 TPoint CWsConsole::CursorPos() const
   592 	{
   593 	// eshell is using 0,0 based
   594 	return iCursorPos - TPoint(1,1);
   595 	}
   596 
   597 void CWsConsole::SetCursorPosAbs(const TPoint& aPoint)
   598 	{
   599 	// eshell is using 0,0 based
   600 	iCursorPos = aPoint + TPoint(1,1);
   601 	iMux->Group().SetTextCursor(iWin, WritePos(), iTextCursor);
   602 	}
   603 
   604 void CWsConsole::SetCursorPosRel(const TPoint& aPoint)
   605 	{
   606 	iCursorPos += aPoint;
   607 	iMux->Group().SetTextCursor(iWin, WritePos(), iTextCursor);
   608 	}
   609 
   610 void CWsConsole::SetCursorHeight(TInt aPercentage)
   611 	{
   612 	if (aPercentage == 0)
   613 		{
   614 		// none
   615 		iTextCursor.iHeight = 0;
   616 		}
   617 	else if (aPercentage == 100)
   618 		{
   619 		// insert
   620 		iTextCursor.iHeight = iCharSize.iHeight;
   621 		iTextCursor.iAscent = iFontAscent;
   622 		}
   623 	else
   624 		{
   625 		// normal
   626 		iTextCursor.iHeight = 1;
   627 		iTextCursor.iAscent = 0;
   628 		}
   629 
   630 	iMux->Group().SetTextCursor(iWin, WritePos(), iTextCursor);
   631 	}
   632 
   633 void CWsConsole::SetTitle(const TDesC& /*aTitle*/)
   634 	{
   635 	}
   636 
   637 void CWsConsole::ClearScreen()
   638 	{
   639 	iBitGc->Clear();
   640 	iCursorPos.iX = iCursorPos.iY = 1;
   641 	DrawNow();
   642 	}
   643 
   644 void CWsConsole::ClearToEndOfLine()
   645 	{
   646 	iMux->Group().CancelTextCursor();
   647 	TPoint writePos = WritePos();
   648 	TRect r(writePos.iX, writePos.iY-iFontAscent, iWindowSize.iWidth, writePos.iY+iFontDescent);
   649 	iBitGc->Clear(r);
   650 	DrawNow();
   651 	iMux->Group().SetTextCursor(iWin, WritePos(), iTextCursor);
   652 	}
   653 
   654 TSize CWsConsole::ScreenSize() const
   655 	{
   656 	return iSizeInChars;
   657 	}
   658 
   659 TKeyCode CWsConsole::KeyCode() const
   660 	{
   661 	return iKeyCode;
   662 	}
   663 
   664 TUint CWsConsole::KeyModifiers() const
   665 	{
   666 	return iModifiers;
   667 	}
   668 
   669 void CWsConsole::DrawNow()
   670 	{
   671 	iWin.Invalidate();
   672 	iWin.BeginRedraw();
   673 	iMux->Gc().Activate(iWin);
   674 	iMux->Gc().BitBlt(TPoint(0,0), iBitmap);
   675 	iMux->Gc().Deactivate();
   676 	iWin.EndRedraw();
   677 	iMux->Session().Flush();
   678 	}
   679 
   680 void CWsConsole::Scroll()
   681 	{
   682 	TRect r(0, iCharSize.iHeight, iWindowSize.iWidth, iSizeInChars.iHeight * iCharSize.iHeight);
   683 
   684 	iBitGc->CopyRect(TPoint(0, -iCharSize.iHeight), r);
   685 	// clear last line
   686 	TRect rect(0, WritePos().iY-iFontAscent, iWindowSize.iWidth, iWindowSize.iHeight);
   687 	iBitGc->Clear(rect);
   688 	}
   689 
   690 extern "C" EXPORT_C TAny *NewConsole()
   691 	{
   692 	return new CWsConsole;
   693 	}