os/graphics/windowing/windowserver/nonnga/SERVER/EVQUEUE.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1994-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 // Window server event queue handling
    15 // 
    16 //
    17 
    18 #include "EVQUEUE.H"
    19 
    20 #include "server.h"
    21 #include "wstop.h"
    22 #include "panics.h"
    23 #include "pointer.h"
    24 #include "wstraces.h"
    25 
    26 GLREF_D CDebugLogBase *wsDebugLog;
    27 
    28 #if defined(_DEBUG)
    29 #define __CHECK_QUEUE() CheckQueue()
    30 #define __ZAP_EVENTS(pointer,len) ZapEvents(pointer,len)
    31 #define __ZAP_EVENT(pointer) ZapEvent(pointer)
    32 #else
    33 #define __CHECK_QUEUE()
    34 #define __ZAP_EVENTS(pointer,len)
    35 #define __ZAP_EVENT(pointer)
    36 #endif
    37 
    38 const TInt ECopyBufSize=4;
    39 
    40 TDblQue<CEventQueue> CEventQueue::iQueueList(_FOFF(CEventQueue,iLink));
    41 TWsEvent *CEventQueue::iGlobalEventQueue=NULL;
    42 TInt CEventQueue::iGlobalEventQueueSize=0;
    43 TInt CEventQueue::iNumConnections=0;
    44 RMutex CEventQueue::iMutex;
    45 TWsEvent CEventQueue::iNullEvent;
    46 
    47 // CEventBase
    48 
    49 CEventBase::~CEventBase()
    50 	{
    51 	if (!iEventMsg.IsNull() && CWsTop::ShuttingDown())
    52 		{
    53 		iEventMsg.Complete(KErrServerTerminated);
    54 		}
    55 	}
    56 
    57 CEventBase::CEventBase(CWsClient *aOwner) : iWsOwner(aOwner)
    58 	{
    59 	}
    60 
    61 void CEventBase::SignalEvent(TInt aCode)
    62 	{
    63 	if (wsDebugLog)
    64 		wsDebugLog->SignalEvent(iWsOwner->ConnectionHandle());
    65 	iEventMsg.Complete(aCode);
    66 	iEventSignalledState|=EEventFlagSignalled;
    67 	}
    68 
    69 inline TBool CEventBase::IsEventCancelled()
    70 	{
    71 	return (iEventSignalledState & EEventFlagCancelled);
    72 	}
    73 
    74 void CEventBase::CancelRead()
    75 //
    76 // If there is an outstanding read cancel it.
    77 //
    78 	{
    79 	if (!iEventMsg.IsNull())
    80 		{
    81 		iEventMsg.Complete(KErrCancel);
    82 		}
    83 	iEventSignalledState|=EEventFlagCancelled;
    84 	}
    85 
    86 void CEventBase::GetData(TAny *aData, TInt aLen)
    87 	{
    88 	if (!(iEventSignalledState&EEventFlagSignalled))
    89 		{
    90 		iWsOwner->PPanic(EWservPanicUnsignalledEventData);
    91 		}
    92 	iEventSignalledState&=~EEventFlagSignalled;
    93 	CWsClient::ReplyBuf(aData,aLen);
    94 	}
    95 
    96 void CEventBase::EventReadyCheck()
    97 //
    98 // Queue a read of an event notification
    99 //
   100 	{
   101 	if ((iEventSignalledState&EEventFlagSignalled && !(iEventSignalledState&EEventFlagCancelled)) || !iEventMsg.IsNull())
   102 		{
   103 		iWsOwner->PPanic(EWservPanicReadOutstanding);
   104 		}
   105 	}
   106 
   107 void CEventBase::EventReady(const RMessagePtr2& aEventMsg)
   108 	{
   109 	EventReadyCheck();
   110 	iEventMsg=aEventMsg;
   111 	}
   112 
   113 // CEventQueue - Queue organisation functions
   114 
   115 #if defined(_DEBUG)
   116 void CEventQueue::CheckQueue()
   117 	{
   118 	TDblQueIter<CEventQueue> iter(iQueueList);
   119 	CEventQueue *qptr;
   120 	iter.SetToFirst();
   121 	while((qptr=iter++)!=NULL)
   122 		{
   123 		WS_ASSERT_DEBUG(qptr->iQueueSize>=0 && qptr->iQueueSize<=iGlobalEventQueueSize, EWsPanicCheckEventQueue);
   124 		WS_ASSERT_DEBUG(qptr->iQueueSize==0 || qptr->iHead<qptr->iQueueSize, EWsPanicCheckEventQueue);
   125 		WS_ASSERT_DEBUG(qptr->iCount<=qptr->iQueueSize, EWsPanicCheckEventQueue);
   126 		WS_ASSERT_DEBUG(qptr->iEventPtr>=iGlobalEventQueue && qptr->iEventPtr<=(iGlobalEventQueue+iGlobalEventQueueSize), EWsPanicCheckEventQueue);
   127 		}
   128 	for(TInt index=0;index<iCount;index++)
   129 		{
   130 		TWsEvent *ev=EventPtr(index);
   131 		WS_ASSERT_DEBUG(ev->Type()!=EEventMarkInvalid, EWsPanicCheckEventQueue);
   132 		}
   133 	}
   134 
   135 void CEventQueue::ZapEvent(TWsEvent *aTarget)
   136 	{
   137 	aTarget->SetType(EEventMarkInvalid);
   138 	aTarget->SetHandle(555);
   139 	}
   140 
   141 void CEventQueue::ZapEvents(TWsEvent *aTarget, TInt aLen)
   142 	{
   143 	for(TInt index=0;index<aLen;index++)
   144 		ZapEvent(aTarget+index);
   145 	}
   146 
   147 #endif
   148 
   149 void CEventQueue::EventCopy(TWsEvent *aTarget, TWsEvent *aSource, TInt aNumEvents)
   150 	{
   151 	WS_ASSERT_DEBUG(aTarget>=iGlobalEventQueue, EWsPanicEventQueueCopy);
   152 	WS_ASSERT_DEBUG((aTarget+aNumEvents)<=(iGlobalEventQueue+iGlobalEventQueueSize), EWsPanicEventQueueCopy);
   153 	Mem::Copy(aTarget,aSource,aNumEvents*sizeof(TWsEvent));
   154 	}
   155 
   156 TInt CEventQueue::RequiredQueueSize(TInt aNumConnections)
   157 	{
   158 	return(aNumConnections*EMinQueueSize+EMaxQueueSize+EExtraQueueSize);
   159 	}
   160 
   161 /**
   162 Adjust the Global Queue Size.
   163 @param aNewSize the size for event queue. 
   164 */
   165 void CEventQueue::AdjustQueueSizeL(TInt aNewSize)
   166 	{
   167 	TWsEvent* oldQ=iGlobalEventQueue;
   168 	if (aNewSize < iGlobalEventQueueSize)
   169 		{
   170 		// Re-alloc wont move the cell down to a lower address, this means once this cell is
   171 		// high up in memory it may never move down again, thus wasting loads of global heap space.
   172 		const CEventQueue* last=iQueueList.Last();
   173 		if ((last->iEventPtr + last->iQueueSize) >= (iGlobalEventQueue + aNewSize))
   174 			{
   175 			return;
   176 			}
   177 		TInt allocSize = aNewSize * sizeof(TWsEvent);
   178 		iGlobalEventQueue=static_cast<TWsEvent*>(User::AllocL(allocSize));
   179 		Mem::Copy(iGlobalEventQueue,oldQ,allocSize);
   180 		User::Free(oldQ);
   181 		}
   182 	else
   183 		{
   184 		iGlobalEventQueue = static_cast<TWsEvent*>(User::ReAllocL(iGlobalEventQueue, aNewSize * sizeof(TWsEvent)));
   185 		}
   186 	__ZAP_EVENTS(iGlobalEventQueue + iGlobalEventQueueSize, aNewSize - iGlobalEventQueueSize); 
   187 	iGlobalEventQueueSize = aNewSize; 
   188 	// coverity[use_after_free]
   189 	TInt diff=(reinterpret_cast<TInt8*>(iGlobalEventQueue)-reinterpret_cast<TInt8*>(oldQ));
   190 	if (diff)
   191 		{
   192 		TDblQueIter<CEventQueue> iter(iQueueList);
   193 		CEventQueue* qptr;
   194 		while((qptr=iter++)!=NULL)
   195 			{
   196 			qptr->iEventPtr=reinterpret_cast<TWsEvent*>(reinterpret_cast<TInt8*>(qptr->iEventPtr)+diff);
   197 			}
   198 		}
   199 	}
   200 
   201 void CEventQueue::AddQueueL()
   202 	{
   203 	Wait();
   204 	if ((iNumConnections%EQueueGranularity)==0)
   205 		{
   206 		const TInt newSize = RequiredQueueSize(iNumConnections + EQueueGranularity);
   207 		TRAPD(err,AdjustQueueSizeL(newSize));
   208 		if (err!=KErrNone)
   209 			{
   210 			__CHECK_QUEUE();
   211 			Signal();
   212 			User::Leave(err);
   213 			}
   214 		}
   215 	iNumConnections++;
   216 	if (iQueueList.IsEmpty())
   217 		iEventPtr=iGlobalEventQueue;
   218 	else
   219 		{
   220 		CEventQueue *qptr=iQueueList.Last();
   221 		iEventPtr=qptr->iEventPtr+qptr->iQueueSize;
   222 		}
   223 	iQueueList.AddLast(*this);
   224 	
   225 	//Initialize the queue size to at least EMinQueueSize
   226 	//Check the queue by doing standard queue compression.
   227 	TBool isExpanded = ETrue;
   228 	do {isExpanded = Expand(EEventPriorityLow);} while ((iQueueSize < EMinQueueSize) && isExpanded);
   229 	while (iQueueSize < EMinQueueSize)
   230 		{
   231 		//Cannot get enough spaces by doing standard queue compression,
   232 		//try to grow the global queue.
   233 		TInt sizeRequired = EMinQueueSize - iQueueSize;
   234 		const TInt newSize = iGlobalEventQueueSize + sizeRequired;
   235 		TRAPD(err, AdjustQueueSizeL(newSize));
   236 		if (err != KErrNone)
   237 			{
   238 			//Cannot get enough spaces by growing global queue.
   239 			//try to purge the oldest events from inactive clients.				
   240 			TInt numEventCleared = PurgeInactiveEvents(sizeRequired);
   241 			if (numEventCleared == 0)
   242 				{
   243 				__CHECK_QUEUE();
   244 				Signal();
   245 				User::Leave(err);
   246 				}
   247 			}
   248 		while (doExpand(ECompressNoPurge)) {};
   249 		}
   250 	__CHECK_QUEUE();
   251 	Signal();
   252 	}
   253 
   254 void CEventQueue::RemoveQueue()
   255 	{
   256 	Wait();
   257 	if (iEventPtr)	// If this is still NULL this class hasn't been linked into the Q list
   258 		{
   259 		__ZAP_EVENTS(iEventPtr, iQueueSize);
   260 		iLink.Deque();
   261 		if (--iNumConnections==0)
   262 			{
   263 			User::Free(iGlobalEventQueue);
   264 			iGlobalEventQueue=NULL;
   265 			iGlobalEventQueueSize=0;
   266 			}
   267 		else if ((iNumConnections%EQueueGranularity)==0)
   268 			{
   269 			TDblQueIter<CEventQueue> iter(iQueueList);
   270 			CEventQueue* qptr=iter++;
   271 			qptr->Compress(ECompressNoPurge);
   272 			while((qptr=iter++)!=NULL)
   273 				{
   274 				qptr->SqueezeDown();
   275 				}
   276 			const TInt newSize = RequiredQueueSize(iNumConnections);
   277 			TRAP_IGNORE(AdjustQueueSizeL(newSize));
   278 			// Can easily leave as we need to allocate the new smaller queue before freeing
   279 			// the old queue. But if it does it doesn't matter as we'll simply be left with
   280 			// a larger than necessary buffer until the next realloc
   281 			}
   282 		iCount=0;
   283 		}
   284 	__CHECK_QUEUE();
   285 	Signal();
   286 	}
   287 
   288 void CEventQueue::IncreaseQueueSize(TInt aNumSpaces)
   289 	{
   290 	if ((iQueueSize+aNumSpaces)>EMaxQueueSize)
   291 		aNumSpaces=EMaxQueueSize-iQueueSize;
   292 	EventCopy(iEventPtr+iHead+aNumSpaces, iEventPtr+iHead, iQueueSize-iHead);
   293 	__ZAP_EVENTS(iEventPtr+iHead, aNumSpaces);
   294 	iQueueSize+=aNumSpaces;
   295 	iHead=(iHead+aNumSpaces)%iQueueSize;
   296 	}
   297 
   298 TBool CEventQueue::Expand(TWservEventPriorities aPriority)
   299 	{
   300 	if (iQueueSize==EMaxQueueSize)
   301 		{
   302 		if (aPriority==EEventPriorityHigh)
   303 			{
   304 			Purge();
   305 			if (iCount<iQueueSize)
   306 				return(ETrue);	// Success!
   307 			}
   308 		}
   309 	else if (doExpand(ECompressNoPurge) ||
   310 			  doExpand(ECompressPurge1) ||
   311 			   (aPriority==EEventPriorityHigh && doExpand(ECompressPurge2)))
   312 		return(ETrue);
   313 	return(EFalse);				// Failure
   314 	}
   315 
   316 TBool CEventQueue::doExpand(TCompressMode aCompressMode)
   317 	{
   318 	TDblQueIter<CEventQueue> iter(iQueueList);
   319 	iter.SetToLast();
   320 	CEventQueue* qptr=NULL;
   321 	TInt spare=0;
   322 	
   323 	// while loop from last queue till current queue, moving all queues up 
   324 	// to get all the space from between them
   325 	while((qptr=iter)!=this)
   326 		{
   327 		spare=qptr->SqueezeUp();
   328 		if (spare==0)
   329 			qptr->Compress(aCompressMode);
   330 		iter--;
   331 		}
   332 
   333 	// current queue, if we have space then expand the same and return
   334 	spare=FollowingGap();
   335 	if (spare>0)
   336 		{
   337 		IncreaseQueueSize(spare);
   338 		__CHECK_QUEUE();
   339 		return(ETrue);
   340 		}
   341 
   342 	// while loop from current queue till first queue, looking for a gap 
   343 	// between queues and using the first one it finds
   344 	iter--;
   345 	TInt expanded=0;
   346 	while((qptr=iter)!=NULL)
   347 		{
   348 		expanded=qptr->SqueezeUp();
   349 		if (expanded>0)
   350 			{
   351 			return MoveDownAndExpand(iter, expanded);
   352 			}
   353 		else
   354 			qptr->Compress(aCompressMode);
   355 		iter--;
   356 		}
   357 
   358 	// Even by doing all the above if we did not find space then check if first 
   359 	// queue has some space before it. If so then make use of it and movedown all the 
   360 	// queue till the current queue and then use the space for expansion
   361 	iter.SetToFirst();
   362 	qptr=iter;
   363 	if (qptr->iEventPtr>iGlobalEventQueue)
   364 		{
   365 		return MoveDownAndExpand(iter, qptr->iEventPtr-iGlobalEventQueue);
   366 		}
   367 
   368 	__CHECK_QUEUE();
   369 	return(EFalse);	// Failed to expand enough room
   370 	}
   371 
   372 // This function moves the queue down by given amount and repeats this until
   373 // the current queue and then expand the same
   374 TBool CEventQueue::MoveDownAndExpand(TDblQueIter<CEventQueue> &aIter, TInt aExpand)
   375 	{
   376 	CEventQueue* qptr=NULL;
   377 	FOREVER
   378 		{
   379 		qptr=aIter++;
   380 		qptr->MoveDown(aExpand);
   381 		if (qptr==this)
   382 			{
   383 			IncreaseQueueSize(aExpand);
   384 			__CHECK_QUEUE();
   385 			break;
   386 			}
   387 		}
   388 	return ETrue;
   389 	}
   390 
   391 void CEventQueue::MoveDown(TInt aMove)
   392 	{
   393 	if (!aMove)
   394 		{
   395 		return;
   396 		}
   397 	EventCopy(iEventPtr-aMove,iEventPtr,iQueueSize);
   398 	iEventPtr-=aMove;
   399 	}
   400 
   401 /*void CEventQueue::LogUpDownEvents(TChar aChar)
   402 	{
   403 	TWsEvent *event;
   404 	TBuf<128> buf;
   405 	buf.Zero();
   406 	buf.Append(aChar);
   407 	buf.Append('#');
   408 	buf.Append('#');
   409 	TBool some=EFalse;
   410 	TInt index;
   411 
   412 	for (index=0;index<iCount;index++)
   413 		{
   414 		event=EventPtr(index);
   415 		if (event->Type()==EEventPointer)
   416 			{
   417 			if (event->Pointer()->iType==TPointerEvent::EButton1Down
   418 										|| event->Pointer()->iType==TPointerEvent::EButton1Up)
   419 				{
   420 				some=ETrue;
   421 				if (event->Pointer()->iType==TPointerEvent::EButton1Down)
   422 					buf.Append('D');
   423 				else
   424 					buf.Append('U');
   425 				buf.AppendNum(index);
   426 				}
   427 			}
   428 		}
   429 	if (wsDebugLog)
   430 		wsDebugLog->MiscMessage(ELogImportant,buf);
   431 	}*/
   432 
   433 void CEventQueue::Purge()
   434 	{
   435 // Purgable events are:
   436 //	Pointer Up/Down pairs
   437 //  Pointer moves & drags
   438 //  Key messages
   439 //  Key Up/Down pairs
   440 //  Key Ups if not foreground connection
   441 //  Focus lost/gained pairs
   442 //
   443 // Events that must no be purged
   444 //  Key ups for foreground connections queue
   445 //  Lone pointer ups, must be delivered to match preceeding pointer down
   446 //  Lone focus lost/gained messages
   447 //	
   448 	TWsEvent *event;
   449 	TWsEvent *event2;
   450 	TInt index2;
   451 	TInt index=iCount;
   452 	while(index>0)
   453 		{
   454 		event=EventPtr(--index);
   455 		switch(event->Type())
   456 			{
   457 			case EEventPassword:
   458 				break;
   459 			case EEventMarkInvalid:
   460 #if defined(_DEBUG)
   461 				WS_PANIC_DEBUG(EWsPanicCheckEventQueue);
   462 #endif
   463 			case EEventNull:
   464 			case EEventKey:
   465 			case EEventPointerEnter:
   466 			case EEventPointerExit:
   467 			case EEventDragDrop:
   468 breakLoopAndRemoveEvent:
   469 				RemoveEvent(index);
   470 				return;
   471 			case EEventKeyUp:
   472 				if (iQueueList.First()!=this)
   473 					goto breakLoopAndRemoveEvent;
   474 				break;
   475 			case EEventKeyDown:
   476 				if (iQueueList.First()!=this)
   477 					goto breakLoopAndRemoveEvent;
   478 				for (index2=index+1;index2<iCount;index2++)
   479 					{
   480 					event2=EventPtr(index2);
   481 					if (event2->Type()==EEventKeyUp && event2->Key()->iScanCode==event->Key()->iScanCode)
   482 						{
   483 						*event2=iNullEvent;
   484 						goto breakLoopAndRemoveEvent;
   485 						}
   486 					}
   487 				break;
   488 			case EEventModifiersChanged:
   489 				for (index2=index;index2>0;)
   490 					{
   491 					event2=EventPtr(--index2);
   492 					if (event2->Type()==EEventModifiersChanged)
   493 						{
   494 						event->ModifiersChanged()->iChangedModifiers|=event2->ModifiersChanged()->iChangedModifiers;
   495 						index=index2;
   496 						goto breakLoopAndRemoveEvent;
   497 						}
   498 					}
   499 				break;
   500 			case EEventPointerBufferReady:
   501 				CWsPointerBuffer::DiscardPointerMoveBuffer(event->Handle());
   502 				goto breakLoopAndRemoveEvent;
   503 			case EEventFocusLost:
   504 			case EEventFocusGained:
   505 				if ((index+1)<iCount)
   506 					{
   507 					event2=EventPtr(index+1);
   508 					if (event2->Type()==EEventFocusLost || event2->Type()==EEventFocusGained)
   509 						{
   510 						*event2=iNullEvent;
   511 						goto breakLoopAndRemoveEvent;
   512 						}
   513 					}
   514 				break;
   515 			case EEventSwitchOn:
   516 				if ((index+1)<iCount && EventPtr(index+1)->Type()==EEventSwitchOn)
   517 					goto breakLoopAndRemoveEvent;
   518 				break;
   519 			case EEventPointer:
   520 				{
   521 				TPointerEvent::TType upType;
   522 				switch(event->Pointer()->iType)
   523 					{
   524 					case TPointerEvent::EDrag:
   525 					case TPointerEvent::EMove:
   526 					case TPointerEvent::EButtonRepeat:
   527 					case TPointerEvent::ESwitchOn:
   528 						goto breakLoopAndRemoveEvent;
   529 					case TPointerEvent::EButton1Down:
   530 						upType=TPointerEvent::EButton1Up;
   531 						goto purgeDownUp;
   532 					case TPointerEvent::EButton2Down:
   533 						upType=TPointerEvent::EButton2Up;
   534 						goto purgeDownUp;
   535 					case TPointerEvent::EButton3Down:
   536 						upType=TPointerEvent::EButton3Up;
   537 purgeDownUp:			for (index2=index+1;index2<iCount;index2++)
   538 							{
   539 							event2=EventPtr(index2);
   540 							if (event2->Type()==EEventPointer && event2->Handle()==event->Handle() && event2->Pointer()->iType==upType)
   541 								{
   542 								*event2=iNullEvent;
   543 								goto breakLoopAndRemoveEvent;
   544 								}
   545 							}
   546 						WsPointer::UnmatchedDownPurged(upType, event->Handle());
   547 						goto breakLoopAndRemoveEvent;
   548 					case TPointerEvent::EButton1Up:
   549 					case TPointerEvent::EButton2Up:
   550 					case TPointerEvent::EButton3Up:
   551 						break;
   552 					}
   553 				}
   554 				break;
   555 			}
   556 		}
   557 	}
   558 
   559 void CEventQueue::PurgePointerEvents()
   560 	{
   561 	TWsEvent *event;
   562 	TWsEvent *event2;
   563 	TInt index2;
   564 	TInt index=iCount;
   565 	while(index>0)
   566 		{
   567 		event=EventPtr(--index);
   568 		switch(event->Type())
   569 			{
   570 			case EEventPointerBufferReady:
   571 				CWsPointerBuffer::DiscardPointerMoveBuffer(event->Handle());
   572 				RemoveEvent(index);
   573 				break;
   574 			case EEventPointer:
   575 				{
   576 				TPointerEvent::TType upType;
   577 				switch(event->Pointer()->iType)
   578 					{
   579 					case TPointerEvent::EDrag:
   580 					case TPointerEvent::EMove:
   581 					case TPointerEvent::EButtonRepeat:
   582 					case TPointerEvent::ESwitchOn:
   583 						RemoveEvent(index);
   584 						break;
   585 					case TPointerEvent::EButton1Down:
   586 						upType=TPointerEvent::EButton1Up;
   587 						goto purgeDownUp2;
   588 					case TPointerEvent::EButton2Down:
   589 						upType=TPointerEvent::EButton2Up;
   590 						goto purgeDownUp2;
   591 					case TPointerEvent::EButton3Down:
   592 						upType=TPointerEvent::EButton3Up;
   593 purgeDownUp2:			for (index2=index+1;index2<iCount;index2++)
   594 							{
   595 							event2=EventPtr(index2);
   596 							if (event2->Type()==EEventPointer && event2->Handle()==event->Handle() && event2->Pointer()->iType==upType)
   597 								{
   598 								*event2=iNullEvent;
   599 								goto purgedUp;
   600 								}
   601 							}
   602 						WsPointer::UnmatchedDownPurged(upType, event->Handle());
   603 purgedUp:				RemoveEvent(index);
   604 						break;
   605 					case TPointerEvent::EButton1Up:
   606 					case TPointerEvent::EButton2Up:
   607 					case TPointerEvent::EButton3Up:
   608 						break;
   609 					}
   610 				}
   611 				break;
   612 			}
   613 		}
   614 	}
   615 
   616 /**
   617 Purge requested number of oldest events from inactive event queue.
   618 @param aSizeRequired the total events required to be cleared. 
   619 @return The number of events cleared.
   620 */
   621 TInt CEventQueue::PurgeInactiveEvents(const TInt& aSizeRequired)
   622 	{
   623 	TInt numEventsCleared = 0;
   624 	CEventQueue* qptr;
   625 	TBool isRemoved;
   626 	do	{
   627 		TDblQueIter<CEventQueue> iter(iQueueList);
   628 		isRemoved = EFalse;
   629 		while ((qptr = iter++) != NULL && (aSizeRequired > numEventsCleared))
   630 			{
   631 			if ((qptr->IsEventCancelled() || (qptr->iEventMsg.IsNull() && !qptr->iEventSignalledState)) &&
   632 				(qptr->iQueueSize > EMinQueueSize))
   633 				{
   634 				// we have a client that is not listening with a size larger than min queue size.
   635 				// so lets remove it's oldest event until the number of removed events meet the requirement.
   636 				qptr->RemoveEvent(0);
   637 				numEventsCleared++;
   638 				isRemoved = ETrue;
   639 				}
   640 			}
   641 		} while ((aSizeRequired > numEventsCleared) && isRemoved);
   642 	return numEventsCleared;
   643 	}
   644 
   645 void CEventQueue::Compress(TCompressMode aCompressMode)
   646 	{
   647 //
   648 // The different purge modes are
   649 //
   650 // ECompressNoPurge,	// Don't purge anything
   651 // ECompressPurge1,		// Don't purge foreground queue
   652 // ECompressPurge2,		// Purge all queues
   653 //
   654 	if (aCompressMode==ECompressPurge2 ||
   655 		(this!=iQueueList.First() && aCompressMode==ECompressPurge1))
   656 		Purge();
   657 	TInt compress=iQueueSize-(iCount>EMinQueueSize?iCount:EMinQueueSize);
   658 	if (compress>0)
   659 		{
   660 		compress=(compress+1)/2;	// Compress half the free space in the queue
   661 		TWsEvent *head=EventPtr(0);
   662 		TWsEvent *tail=EventPtr(iCount);
   663 		if (head>tail)
   664 			{
   665 			EventCopy(iEventPtr+compress,iEventPtr,tail-iEventPtr);
   666 			iHead-=compress;
   667 			}
   668 		else
   669 			{
   670 			EventCopy(iEventPtr+compress,head,iCount);
   671 			iHead=0;
   672 			}
   673 		iEventPtr+=compress;
   674 		iQueueSize-=compress;
   675 		}
   676 	}
   677 
   678 void CEventQueue::MoveUp(TInt aNumEvents)
   679 	{
   680 	if (!aNumEvents)
   681 		{
   682 		return;
   683 		}
   684 	EventCopy(iEventPtr+aNumEvents,iEventPtr,iQueueSize);
   685 	iEventPtr+=aNumEvents;
   686 	}
   687 
   688 TInt CEventQueue::FollowingGap() const
   689 	{
   690 	TDblQueIter<CEventQueue> iter(iQueueList);
   691 	CEventQueue *qptr;
   692 	iter.Set(*(CEventQueue *)this);
   693 	iter++;
   694 	TWsEvent *end;
   695 	if ((qptr=iter)!=NULL)
   696 		end=qptr->iEventPtr;
   697 	else
   698 		end=iGlobalEventQueue+iGlobalEventQueueSize;
   699 	return(end-(iEventPtr+iQueueSize));
   700 	}
   701 
   702 TInt CEventQueue::SqueezeUp()
   703 	{
   704 	TInt gap=FollowingGap();
   705 	MoveUp(gap);
   706 	return(gap);
   707 	}
   708 
   709 void CEventQueue::SqueezeDown()
   710 	{
   711 	TDblQueIter<CEventQueue> iter(iQueueList);
   712 	iter.Set(*this);
   713 	iter--;
   714 	CEventQueue *qptr=iter;
   715 	if (qptr!=NULL)
   716 		{
   717 		Compress(ECompressNoPurge);
   718 		TInt gap=qptr->FollowingGap();
   719 		MoveDown(gap);
   720 		}
   721 	}
   722 
   723 void CEventQueue::MoveToFront()
   724 	{
   725 	if (this==iQueueList.First())
   726 		return;
   727 	Wait();
   728 	CEventQueue *qptr;
   729 	TInt gap=0;
   730 	TDblQueIter<CEventQueue> iter(iQueueList);
   731 	iter.SetToLast();
   732 	while((qptr=iter--)!=NULL)
   733 		{
   734 		if (gap<iQueueSize)
   735 			qptr->Compress(ECompressNoPurge);
   736 		gap=qptr->SqueezeUp();
   737 		}
   738 	if (gap>=iQueueSize)
   739 		EventCopy(iGlobalEventQueue,iEventPtr,iQueueSize);
   740 	else
   741 		{
   742 		EventCopy(iGlobalEventQueue,iEventPtr,gap);
   743 		iEventPtr+=gap;
   744 		TWsEvent copyBuf[ECopyBufSize];	// temp buffer, can copy upto ECopyBufSize events at a time
   745 		TInt eventsToGo=iQueueSize-gap;
   746 		iQueueSize=gap;
   747 		do
   748 			{
   749 			TInt copy=Min(eventsToGo,ECopyBufSize);
   750 			Mem::Copy(&copyBuf[0],iEventPtr,copy*sizeof(TWsEvent));
   751 			iter.Set(*this);
   752 			iter--;
   753 			while((qptr=iter--)!=NULL)
   754 				qptr->MoveUp(copy);
   755 			EventCopy(iGlobalEventQueue+iQueueSize,&copyBuf[0],copy);
   756 			iQueueSize+=copy;
   757 			eventsToGo-=copy;
   758 			iEventPtr+=copy;
   759 			} while(eventsToGo>0);
   760 		}
   761 	iEventPtr=iGlobalEventQueue;
   762 	this->iLink.Deque();
   763 	iQueueList.AddFirst(*this);
   764 	__CHECK_QUEUE();
   765 	Signal();
   766 	}
   767 
   768 // CEventQueue
   769 
   770 CEventQueue::CEventQueue(CWsClient *aOwner) : CEventBase(aOwner)
   771 	{
   772 	__DECLARE_NAME(_S("CEventQueue"));
   773 	}
   774 
   775 CEventQueue::~CEventQueue()
   776 	{
   777 	RemoveQueue();
   778 	}
   779 
   780 void CEventQueue::InitStaticsL()
   781 	{
   782 	User::LeaveIfError(iMutex.CreateLocal());
   783 	}
   784 
   785 void CEventQueue::DeleteStaticsL()
   786 	{
   787 	iMutex.Close();
   788 	}
   789 
   790 void CEventQueue::ConstructL()
   791 	{
   792 	AddQueueL();
   793 	Mem::FillZ(&iNullEvent,sizeof(iNullEvent));
   794 	}
   795 
   796 TWsEvent *CEventQueue::EventPtr(TInt index)	
   797 	{
   798 	return(iEventPtr+((iHead+index)%iQueueSize));
   799 	}
   800 
   801 TBool CEventQueue::QueueEvent(const TWsEvent &event)
   802 	{
   803 	TWservEventPriorities priority=EEventPriorityLow;
   804 #ifdef SYMBIAN_PROCESS_MONITORING_AND_STARTUP
   805 	if (event.Type()==EEventPassword || event.Type()==EEventSwitchOff || 
   806 		event.Type()==EEventKeySwitchOff || event.Type()==EEventRestartSystem)
   807 #else
   808 	if (event.Type()==EEventPassword || event.Type()==EEventSwitchOff || event.Type()==EEventKeySwitchOff)
   809 #endif
   810 		{
   811 		priority=EEventPriorityHigh;
   812 		}
   813 	return(QueueEvent(event,priority));
   814 	}
   815 
   816 TBool CEventQueue::CheckRoom()
   817 //
   818 // If the queue is full and room is created return ETrue
   819 //
   820 	{
   821 	TBool ret=EFalse;
   822 	Wait();
   823 	if (iCount==iQueueSize && Expand(EEventPriorityHigh))
   824 		ret=ETrue;
   825 	Signal();
   826 	return(ret);
   827 	}
   828 
   829 TBool CEventQueue::QueueEvent(const TWsEvent &event, TWservEventPriorities aPriority)
   830 //
   831 // Queue an event, returns ETrue if queued or delivered, EFalse if the queue was full.
   832 //
   833 	{
   834 	WS_TRACE_SERVER_QUEUEEVENT();
   835 	TBool ret=ETrue;
   836 	Wait();
   837 	if (iCount==iQueueSize && !Expand(aPriority))
   838 		ret=EFalse;
   839 	else
   840 		{
   841 		if (!iEventMsg.IsNull())
   842 			{
   843 			SignalEvent();
   844 			}
   845 		*EventPtr(iCount++)=event;
   846 		}
   847 	Signal();
   848 	return(ret);
   849 	}
   850 
   851 TBool CEventQueue::QueueEvent(TUint32 aTarget, TInt aEvent)
   852 	{
   853 	TWsEvent event;
   854 	event.SetType(aEvent);
   855 	event.SetHandle(aTarget);
   856 	event.SetTimeNow();
   857 	return(QueueEvent(event));
   858 	}
   859 
   860 void CEventQueue::UpdateLastEvent(const TWsEvent &event)
   861 	{
   862 	WS_ASSERT_DEBUG(iCount>0, EWsPanicQueueUpdateCount);
   863 	Mem::Copy(EventPtr(iCount-1)->EventData(),event.EventData(),TWsEvent::EWsEventDataSize);
   864 	Signal();
   865 	}
   866 
   867 void CEventQueue::GetData()
   868 //
   869 // If there is an outstanding event in the queue, reply with it's data and remove it from the Q
   870 //
   871 	{
   872 	if (iCount>0)
   873 		{
   874 		WS_ASSERT_DEBUG((iEventPtr+iHead)->Type()!=EEventMarkInvalid, EWsPanicCheckEventQueue);
   875 		CEventBase::GetData(iEventPtr+iHead,sizeof(*iEventPtr));
   876 		__ZAP_EVENT(iEventPtr+iHead);
   877 		iHead=(iHead+1)%iQueueSize;
   878 		iCount--;
   879 		}
   880 	else
   881 		CEventBase::GetData(&iNullEvent,sizeof(iNullEvent));
   882 	}
   883 
   884 void CEventQueue::EventReady(const RMessagePtr2& aEventMsg)
   885 //
   886 // Queue a read of an event notification
   887 //
   888 	{
   889 	EventReadyCheck();
   890 	Wait();
   891 	iEventMsg=aEventMsg;
   892 	if (iCount>0)
   893 		SignalEvent();
   894 	Signal();
   895 	}
   896 
   897 void CEventQueue::RemoveEvent(TInt index)
   898 //
   899 // Remove event 'index' in the queue, this event MUST exist in the queue
   900 //
   901 	{
   902 	WS_ASSERT_DEBUG(index < iCount, EWsPanicCheckEventQueue);
   903 	iCount--;
   904 	for(;index<iCount;index++)
   905 		*EventPtr(index)= *EventPtr(index+1);
   906 	__ZAP_EVENT(EventPtr(iCount));
   907 	}
   908 
   909 const TWsEvent *CEventQueue::PeekLastEvent()
   910 //
   911 // Return a read only pointer to the last event in the queue (or NULL if no event)
   912 //
   913 	{
   914 	if (iCount==0)
   915 		return(NULL);
   916 	return(EventPtr(iCount-1));
   917 	}
   918 
   919 void CEventQueue::Wait()
   920 	{
   921 	iMutex.Wait();
   922 	}
   923 
   924 void CEventQueue::Signal()
   925 	{
   926 	iMutex.Signal();
   927 	}
   928 
   929 void CEventQueue::WalkEventQueue(EventQueueWalk aFunc, TAny *aFuncParam)
   930 	{
   931 	Wait();
   932 restart:
   933 	for (TInt index=0;index<iCount;index++)
   934 		{
   935 		TWsEvent *event=EventPtr(index);
   936 		switch((aFunc)(aFuncParam,event))
   937 			{
   938 			case EEventQueueWalkDeleteEvent:
   939 				RemoveEvent(index--);
   940 			case EEventQueueWalkOk:
   941 				break;
   942 			case EEventQueueWalkRestart:
   943 				goto restart;
   944 			}
   945 		}
   946 	Signal();
   947 	}