os/graphics/windowing/windowserver/nga/SERVER/EVQUEUE.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// Window server event queue handling
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "EVQUEUE.H"
sl@0
    19
sl@0
    20
#include "server.h"
sl@0
    21
#include "wstop.h"
sl@0
    22
#include "panics.h"
sl@0
    23
#include "pointer.h"
sl@0
    24
#include "advancedpointereventhelper.h"
sl@0
    25
#include "debughelper.h"
sl@0
    26
sl@0
    27
GLREF_D CDebugLogBase* wsDebugLog;
sl@0
    28
sl@0
    29
#if defined(_DEBUG)
sl@0
    30
#define __CHECK_QUEUE() CheckQueue()
sl@0
    31
#define __ZAP_EVENTS(pointer,len) ZapEvents(pointer,len)
sl@0
    32
#define __ZAP_EVENT(pointer) ZapEvent(pointer)
sl@0
    33
#else
sl@0
    34
#define __CHECK_QUEUE()
sl@0
    35
#define __ZAP_EVENTS(pointer,len)
sl@0
    36
#define __ZAP_EVENT(pointer)
sl@0
    37
#endif
sl@0
    38
sl@0
    39
const TInt ECopyBufSize=4;
sl@0
    40
sl@0
    41
TDblQue<CEventQueue> CEventQueue::iQueueList(_FOFF(CEventQueue,iLink));
sl@0
    42
TWsEvent* CEventQueue::iGlobalEventQueue=NULL;
sl@0
    43
TInt CEventQueue::iGlobalEventQueueSize=0;
sl@0
    44
TInt CEventQueue::iNumConnections=0;
sl@0
    45
RMutex CEventQueue::iMutex;
sl@0
    46
TWsEvent CEventQueue::iNullEvent;
sl@0
    47
sl@0
    48
// CEventBase
sl@0
    49
sl@0
    50
CEventBase::~CEventBase()
sl@0
    51
	{
sl@0
    52
	if (!iEventMsg.IsNull() && CWsTop::ShuttingDown())
sl@0
    53
		{
sl@0
    54
		iEventMsg.Complete(KErrServerTerminated);
sl@0
    55
		}
sl@0
    56
	}
sl@0
    57
sl@0
    58
CEventBase::CEventBase(CWsClient* aOwner) : iWsOwner(aOwner)
sl@0
    59
	{
sl@0
    60
	}
sl@0
    61
sl@0
    62
void CEventBase::SignalEvent(TInt aCode)
sl@0
    63
	{
sl@0
    64
	if (wsDebugLog)
sl@0
    65
		wsDebugLog->SignalEvent(iWsOwner->ConnectionHandle());
sl@0
    66
	iEventMsg.Complete(aCode);
sl@0
    67
	iEventSignalledState|=EEventFlagSignalled;
sl@0
    68
	}
sl@0
    69
sl@0
    70
inline TBool CEventBase::IsEventCancelled()
sl@0
    71
	{
sl@0
    72
	return (iEventSignalledState & EEventFlagCancelled);
sl@0
    73
	}
sl@0
    74
sl@0
    75
/**
sl@0
    76
If there is an outstanding read cancel it.
sl@0
    77
*/
sl@0
    78
void CEventBase::CancelRead()
sl@0
    79
	{
sl@0
    80
	if (!iEventMsg.IsNull())
sl@0
    81
		{
sl@0
    82
		iEventMsg.Complete(KErrCancel);
sl@0
    83
		}
sl@0
    84
	iEventSignalledState|=EEventFlagCancelled;
sl@0
    85
	}
sl@0
    86
sl@0
    87
void CEventBase::GetData(TAny* aData, TInt aLen)
sl@0
    88
	{
sl@0
    89
	if (!(iEventSignalledState&EEventFlagSignalled))
sl@0
    90
		{
sl@0
    91
		iWsOwner->PPanic(EWservPanicUnsignalledEventData);
sl@0
    92
		}
sl@0
    93
	iEventSignalledState&=~EEventFlagSignalled;
sl@0
    94
	CWsClient::ReplyBuf(aData,aLen);
sl@0
    95
	}
sl@0
    96
sl@0
    97
/**
sl@0
    98
Queue a read of an event notification
sl@0
    99
*/
sl@0
   100
void CEventBase::EventReadyCheck()
sl@0
   101
	{
sl@0
   102
	if ((iEventSignalledState&EEventFlagSignalled && !(iEventSignalledState&EEventFlagCancelled)) || !iEventMsg.IsNull())
sl@0
   103
		{
sl@0
   104
#ifdef LOG_WSERV_EVENTS
sl@0
   105
		RDebug::Printf("_WSEVENT: CEventBase::EventReadyCheck, Event is not ready");
sl@0
   106
#endif
sl@0
   107
		iWsOwner->PPanic(EWservPanicReadOutstanding);
sl@0
   108
		}
sl@0
   109
	}
sl@0
   110
sl@0
   111
void CEventBase::EventReady(const RMessagePtr2& aEventMsg)
sl@0
   112
	{
sl@0
   113
	EventReadyCheck();
sl@0
   114
	iEventMsg=aEventMsg;
sl@0
   115
	}
sl@0
   116
sl@0
   117
// CEventQueue - Queue organisation functions
sl@0
   118
sl@0
   119
#if defined(_DEBUG)
sl@0
   120
void CEventQueue::CheckQueue()
sl@0
   121
	{
sl@0
   122
	TDblQueIter<CEventQueue> iter(iQueueList);
sl@0
   123
	CEventQueue* qptr;
sl@0
   124
	iter.SetToFirst();
sl@0
   125
	while((qptr=iter++)!=NULL)
sl@0
   126
		{
sl@0
   127
		WS_ASSERT_DEBUG(qptr->iQueueSize>=0 && qptr->iQueueSize<=iGlobalEventQueueSize, EWsPanicCheckEventQueue);
sl@0
   128
		WS_ASSERT_DEBUG(qptr->iQueueSize==0 || qptr->iHead<qptr->iQueueSize, EWsPanicCheckEventQueue);
sl@0
   129
		WS_ASSERT_DEBUG(qptr->iCount<=qptr->iQueueSize, EWsPanicCheckEventQueue);
sl@0
   130
		WS_ASSERT_DEBUG(qptr->iEventPtr>=iGlobalEventQueue && qptr->iEventPtr<=(iGlobalEventQueue+iGlobalEventQueueSize), EWsPanicCheckEventQueue);
sl@0
   131
		}
sl@0
   132
	for(TInt index=0;index<iCount;index++)
sl@0
   133
		{
sl@0
   134
		TWsEvent* ev=EventPtr(index);
sl@0
   135
		WS_ASSERT_DEBUG(ev->Type()!=EEventMarkInvalid, EWsPanicCheckEventQueue);
sl@0
   136
		}
sl@0
   137
	}
sl@0
   138
sl@0
   139
void CEventQueue::ZapEvent(TWsEvent* aTarget)
sl@0
   140
	{
sl@0
   141
	aTarget->SetType(EEventMarkInvalid);
sl@0
   142
	aTarget->SetHandle(555);
sl@0
   143
	}
sl@0
   144
sl@0
   145
void CEventQueue::ZapEvents(TWsEvent* aTarget, TInt aLen)
sl@0
   146
	{
sl@0
   147
	for(TInt index=0;index<aLen;index++)
sl@0
   148
		ZapEvent(aTarget+index);
sl@0
   149
	}
sl@0
   150
sl@0
   151
#endif
sl@0
   152
sl@0
   153
void CEventQueue::EventCopy(TWsEvent* aTarget, TWsEvent* aSource, TInt aNumEvents)
sl@0
   154
	{
sl@0
   155
	WS_ASSERT_DEBUG(aTarget>=iGlobalEventQueue, EWsPanicEventQueueCopy);
sl@0
   156
	WS_ASSERT_DEBUG((aTarget+aNumEvents)<=(iGlobalEventQueue+iGlobalEventQueueSize), EWsPanicEventQueueCopy);
sl@0
   157
	Mem::Copy(aTarget,aSource,aNumEvents*sizeof(TWsEvent));
sl@0
   158
	}
sl@0
   159
sl@0
   160
TInt CEventQueue::RequiredQueueSize(TInt aNumConnections)
sl@0
   161
	{
sl@0
   162
	return(aNumConnections*EMinQueueSize+EMaxQueueSize+EExtraQueueSize);
sl@0
   163
	}
sl@0
   164
sl@0
   165
/**
sl@0
   166
Adjust the Global Queue Size.
sl@0
   167
@param aNewSize the size for event queue. 
sl@0
   168
*/
sl@0
   169
void CEventQueue::AdjustQueueSizeL(TInt aNewSize)
sl@0
   170
	{
sl@0
   171
	TWsEvent* oldQ=iGlobalEventQueue;
sl@0
   172
	if (aNewSize < iGlobalEventQueueSize)
sl@0
   173
		{
sl@0
   174
		// Re-alloc wont move the cell down to a lower address, this means once this cell is
sl@0
   175
		// high up in memory it may never move down again, thus wasting loads of global heap space.
sl@0
   176
		const CEventQueue* last=iQueueList.Last();
sl@0
   177
		if ((last->iEventPtr + last->iQueueSize) >= (iGlobalEventQueue + aNewSize))
sl@0
   178
			{
sl@0
   179
			return;
sl@0
   180
			}
sl@0
   181
		TInt allocSize = aNewSize * sizeof(TWsEvent);
sl@0
   182
		iGlobalEventQueue=static_cast<TWsEvent*>(User::AllocL(allocSize));
sl@0
   183
		Mem::Copy(iGlobalEventQueue,oldQ,allocSize);
sl@0
   184
		User::Free(oldQ);
sl@0
   185
		}
sl@0
   186
	else
sl@0
   187
		{
sl@0
   188
		iGlobalEventQueue = static_cast<TWsEvent*>(User::ReAllocL(iGlobalEventQueue, aNewSize * sizeof(TWsEvent)));
sl@0
   189
		}
sl@0
   190
	__ZAP_EVENTS(iGlobalEventQueue + iGlobalEventQueueSize, aNewSize - iGlobalEventQueueSize); 
sl@0
   191
	iGlobalEventQueueSize = aNewSize; 
sl@0
   192
	// coverity[use_after_free]
sl@0
   193
	TInt diff=(reinterpret_cast<TInt8*>(iGlobalEventQueue)-reinterpret_cast<TInt8*>(oldQ));
sl@0
   194
	if (diff)
sl@0
   195
		{
sl@0
   196
		TDblQueIter<CEventQueue> iter(iQueueList);
sl@0
   197
		CEventQueue* qptr;
sl@0
   198
		while((qptr=iter++)!=NULL)
sl@0
   199
			{
sl@0
   200
			qptr->iEventPtr=reinterpret_cast<TWsEvent*>(reinterpret_cast<TInt8*>(qptr->iEventPtr)+diff);
sl@0
   201
			}
sl@0
   202
		}
sl@0
   203
	}
sl@0
   204
sl@0
   205
void CEventQueue::AddQueueL()
sl@0
   206
	{
sl@0
   207
	Wait();
sl@0
   208
	if ((iNumConnections%EQueueGranularity)==0)
sl@0
   209
		{
sl@0
   210
		const TInt newSize = RequiredQueueSize(iNumConnections + EQueueGranularity);
sl@0
   211
		TRAPD(err,AdjustQueueSizeL(newSize));
sl@0
   212
		if (err!=KErrNone)
sl@0
   213
			{
sl@0
   214
			__CHECK_QUEUE();
sl@0
   215
			Signal();
sl@0
   216
			User::Leave(err);
sl@0
   217
			}
sl@0
   218
		}
sl@0
   219
	iNumConnections++;
sl@0
   220
	if (iQueueList.IsEmpty())
sl@0
   221
		iEventPtr=iGlobalEventQueue;
sl@0
   222
	else
sl@0
   223
		{
sl@0
   224
		CEventQueue* qptr=iQueueList.Last();
sl@0
   225
		iEventPtr=qptr->iEventPtr+qptr->iQueueSize;
sl@0
   226
		}
sl@0
   227
	iQueueList.AddLast(*this);
sl@0
   228
	
sl@0
   229
	//Initialize the queue size to at least EMinQueueSize
sl@0
   230
	//Check the queue by doing standard queue compression.
sl@0
   231
	TBool isExpanded = ETrue;
sl@0
   232
	do {isExpanded = Expand(EEventPriorityLow);} while ((iQueueSize < EMinQueueSize) && isExpanded);
sl@0
   233
	while (iQueueSize < EMinQueueSize)
sl@0
   234
		{
sl@0
   235
		//Cannot get enough spaces by doing standard queue compression,
sl@0
   236
		//try to grow the global queue.
sl@0
   237
		TInt sizeRequired = EMinQueueSize - iQueueSize;
sl@0
   238
		const TInt newSize = iGlobalEventQueueSize + sizeRequired;
sl@0
   239
		TRAPD(err, AdjustQueueSizeL(newSize));
sl@0
   240
		if (err != KErrNone)
sl@0
   241
			{
sl@0
   242
			//Cannot get enough spaces by growing global queue.
sl@0
   243
			//try to purge the oldest events from inactive clients.				
sl@0
   244
			TInt numEventCleared = PurgeInactiveEvents(sizeRequired);
sl@0
   245
			if (numEventCleared == 0)
sl@0
   246
				{
sl@0
   247
				__CHECK_QUEUE();
sl@0
   248
				Signal();
sl@0
   249
				User::Leave(err);
sl@0
   250
				}
sl@0
   251
			}
sl@0
   252
		while (doExpand(ECompressNoPurge)) {};
sl@0
   253
		}
sl@0
   254
	__CHECK_QUEUE();
sl@0
   255
	Signal();
sl@0
   256
	}
sl@0
   257
sl@0
   258
void CEventQueue::RemoveQueue()
sl@0
   259
	{
sl@0
   260
	Wait();
sl@0
   261
	if (iEventPtr)	// If this is still NULL this class hasn't been linked into the Q list
sl@0
   262
		{
sl@0
   263
		__ZAP_EVENTS(iEventPtr, iQueueSize);
sl@0
   264
		iLink.Deque();
sl@0
   265
		if (--iNumConnections==0)
sl@0
   266
			{
sl@0
   267
			User::Free(iGlobalEventQueue);
sl@0
   268
			iGlobalEventQueue=NULL;
sl@0
   269
			iGlobalEventQueueSize=0;
sl@0
   270
			}
sl@0
   271
		else if ((iNumConnections%EQueueGranularity)==0)
sl@0
   272
			{
sl@0
   273
			TDblQueIter<CEventQueue> iter(iQueueList);
sl@0
   274
			CEventQueue* qptr=iter++;
sl@0
   275
			qptr->Compress(ECompressNoPurge);
sl@0
   276
			while((qptr=iter++)!=NULL)
sl@0
   277
				{
sl@0
   278
				qptr->SqueezeDown();
sl@0
   279
				}
sl@0
   280
			const TInt newSize = RequiredQueueSize(iNumConnections);
sl@0
   281
			TRAP_IGNORE(AdjustQueueSizeL(newSize));
sl@0
   282
			// Can easily leave as we need to allocate the new smaller queue before freeing
sl@0
   283
			// the old queue. But if it does it doesn't matter as we'll simply be left with
sl@0
   284
			// a larger than necessary buffer until the next realloc
sl@0
   285
			}
sl@0
   286
		iCount=0;
sl@0
   287
		}
sl@0
   288
	__CHECK_QUEUE();
sl@0
   289
	Signal();
sl@0
   290
	}
sl@0
   291
sl@0
   292
void CEventQueue::IncreaseQueueSize(TInt aNumSpaces)
sl@0
   293
	{
sl@0
   294
	if ((iQueueSize+aNumSpaces)>EMaxQueueSize)
sl@0
   295
		aNumSpaces=EMaxQueueSize-iQueueSize;
sl@0
   296
	EventCopy(iEventPtr+iHead+aNumSpaces, iEventPtr+iHead, iQueueSize-iHead);
sl@0
   297
	__ZAP_EVENTS(iEventPtr+iHead, aNumSpaces);
sl@0
   298
	iQueueSize+=aNumSpaces;
sl@0
   299
	iHead=(iHead+aNumSpaces)%iQueueSize;
sl@0
   300
	}
sl@0
   301
sl@0
   302
TBool CEventQueue::Expand(TWservEventPriorities aPriority)
sl@0
   303
	{
sl@0
   304
	if (iQueueSize==EMaxQueueSize)
sl@0
   305
		{
sl@0
   306
		if (aPriority==EEventPriorityHigh)
sl@0
   307
			{
sl@0
   308
			Purge();
sl@0
   309
			if (iCount<iQueueSize)
sl@0
   310
				return(ETrue);	// Success!
sl@0
   311
			}
sl@0
   312
		}
sl@0
   313
	else if (doExpand(ECompressNoPurge) ||
sl@0
   314
			  doExpand(ECompressPurge1) ||
sl@0
   315
			   (aPriority==EEventPriorityHigh && doExpand(ECompressPurge2)))
sl@0
   316
		return(ETrue);
sl@0
   317
	return(EFalse);				// Failure
sl@0
   318
	}
sl@0
   319
sl@0
   320
TBool CEventQueue::doExpand(TCompressMode aCompressMode)
sl@0
   321
	{
sl@0
   322
	TDblQueIter<CEventQueue> iter(iQueueList);
sl@0
   323
	iter.SetToLast();
sl@0
   324
	CEventQueue* qptr=NULL;
sl@0
   325
	TInt spare=0;
sl@0
   326
	
sl@0
   327
	// while loop from last queue till current queue, moving all queues up 
sl@0
   328
	// to get all the space from between them
sl@0
   329
	while((qptr=iter)!=this)
sl@0
   330
		{
sl@0
   331
		spare=qptr->SqueezeUp();
sl@0
   332
		if (spare==0)
sl@0
   333
			qptr->Compress(aCompressMode);
sl@0
   334
		iter--;
sl@0
   335
		}
sl@0
   336
sl@0
   337
	// current queue, if we have space then expand the same and return
sl@0
   338
	spare=FollowingGap();
sl@0
   339
	if (spare>0)
sl@0
   340
		{
sl@0
   341
		IncreaseQueueSize(spare);
sl@0
   342
		__CHECK_QUEUE();
sl@0
   343
		return(ETrue);
sl@0
   344
		}
sl@0
   345
sl@0
   346
	// while loop from current queue till first queue, looking for a gap 
sl@0
   347
	// between queues and using the first one it finds
sl@0
   348
	iter--;
sl@0
   349
	TInt expanded=0;
sl@0
   350
	while((qptr=iter)!=NULL)
sl@0
   351
		{
sl@0
   352
		expanded=qptr->SqueezeUp();
sl@0
   353
		if (expanded>0)
sl@0
   354
			{
sl@0
   355
			return MoveDownAndExpand(iter, expanded);
sl@0
   356
			}
sl@0
   357
		else
sl@0
   358
			qptr->Compress(aCompressMode);
sl@0
   359
		iter--;
sl@0
   360
		}
sl@0
   361
sl@0
   362
	// Even by doing all the above if we did not find space then check if first 
sl@0
   363
	// queue has some space before it. If so then make use of it and movedown all the 
sl@0
   364
	// queue till the current queue and then use the space for expansion
sl@0
   365
	iter.SetToFirst();
sl@0
   366
	qptr=iter;
sl@0
   367
	if (qptr->iEventPtr>iGlobalEventQueue)
sl@0
   368
		{
sl@0
   369
		return MoveDownAndExpand(iter, qptr->iEventPtr-iGlobalEventQueue);
sl@0
   370
		}
sl@0
   371
sl@0
   372
	__CHECK_QUEUE();
sl@0
   373
	return(EFalse);	// Failed to expand enough room
sl@0
   374
	}
sl@0
   375
sl@0
   376
// This function moves the queue down by given amount and repeats this until
sl@0
   377
// the current queue and then expand the same
sl@0
   378
TBool CEventQueue::MoveDownAndExpand(TDblQueIter<CEventQueue> &aIter, TInt aExpand)
sl@0
   379
	{
sl@0
   380
	CEventQueue* qptr=NULL;
sl@0
   381
	FOREVER
sl@0
   382
		{
sl@0
   383
		qptr=aIter++;
sl@0
   384
		qptr->MoveDown(aExpand);
sl@0
   385
		if (qptr==this)
sl@0
   386
			{
sl@0
   387
			IncreaseQueueSize(aExpand);
sl@0
   388
			__CHECK_QUEUE();
sl@0
   389
			break;
sl@0
   390
			}
sl@0
   391
		}
sl@0
   392
	return ETrue;
sl@0
   393
	}
sl@0
   394
sl@0
   395
void CEventQueue::MoveDown(TInt aMove)
sl@0
   396
	{
sl@0
   397
	if (!aMove)
sl@0
   398
		{
sl@0
   399
		return;
sl@0
   400
		}
sl@0
   401
	EventCopy(iEventPtr-aMove,iEventPtr,iQueueSize);
sl@0
   402
	iEventPtr-=aMove;
sl@0
   403
	}
sl@0
   404
sl@0
   405
/*void CEventQueue::LogUpDownEvents(TChar aChar)
sl@0
   406
	{
sl@0
   407
	TWsEvent *event;
sl@0
   408
	TBuf<128> buf;
sl@0
   409
	buf.Zero();
sl@0
   410
	buf.Append(aChar);
sl@0
   411
	buf.Append('#');
sl@0
   412
	buf.Append('#');
sl@0
   413
	TBool some=EFalse;
sl@0
   414
	TInt index;
sl@0
   415
sl@0
   416
	for (index=0;index<iCount;index++)
sl@0
   417
		{
sl@0
   418
		event=EventPtr(index);
sl@0
   419
		if (event->Type()==EEventPointer)
sl@0
   420
			{
sl@0
   421
			if (event->Pointer()->iType==TPointerEvent::EButton1Down
sl@0
   422
										|| event->Pointer()->iType==TPointerEvent::EButton1Up)
sl@0
   423
				{
sl@0
   424
				some=ETrue;
sl@0
   425
				if (event->Pointer()->iType==TPointerEvent::EButton1Down)
sl@0
   426
					buf.Append('D');
sl@0
   427
				else
sl@0
   428
					buf.Append('U');
sl@0
   429
				buf.AppendNum(index);
sl@0
   430
				}
sl@0
   431
			}
sl@0
   432
		}
sl@0
   433
	if (wsDebugLog)
sl@0
   434
		wsDebugLog->MiscMessage(ELogImportant,buf);
sl@0
   435
	}*/
sl@0
   436
sl@0
   437
inline void CEventQueue::IncEventPointer(TWsEvent*& aEventPtr)
sl@0
   438
	{
sl@0
   439
	// iEventPtr[iQueueSize] is the element beyond the array, used for efficient bounds checking of the circular buffer only, do not access!!
sl@0
   440
	if(++aEventPtr==&iEventPtr[iQueueSize])
sl@0
   441
		{
sl@0
   442
		aEventPtr=iEventPtr;
sl@0
   443
		}
sl@0
   444
	}
sl@0
   445
sl@0
   446
inline void CEventQueue::DecEventPointer(TWsEvent*& aEventPtr)
sl@0
   447
	{
sl@0
   448
	if(aEventPtr--==iEventPtr)
sl@0
   449
		{
sl@0
   450
		aEventPtr=&iEventPtr[iQueueSize - 1];
sl@0
   451
		}
sl@0
   452
	}
sl@0
   453
sl@0
   454
/*
sl@0
   455
Starting from aEventToPurge searches the queue for matching event by iterating towards end of queue.
sl@0
   456
Matching event will be a pointer event with the same window handle and pointer number
sl@0
   457
as aEventToPurge, but will have type aMatchingType. If matching event is found, it will 
sl@0
   458
be removed from the queue and search will finish.
sl@0
   459
sl@0
   460
Search will be stopped if an event of type aSearchTerminator is found with the same pointer number 
sl@0
   461
as aEventToPurge.
sl@0
   462
sl@0
   463
If search is not stopped by aSearchTerminator and matching event is not found, TWsPointer
sl@0
   464
class is notified that matching event has not been removed, so if it arrives in the future,
sl@0
   465
TWsPointer class will be able to ignore it.
sl@0
   466
*/
sl@0
   467
void CEventQueue::PurgeEventPairs(TWsEvent* aEventToPurge, TPointerEvent::TType aMatchingType, TPointerEvent::TType aSearchTerminator)
sl@0
   468
		{
sl@0
   469
		TWsEvent* eventToMatch = aEventToPurge;
sl@0
   470
		TWsEvent* lastEvent = EventPtr(iCount);
sl@0
   471
		for(IncEventPointer(eventToMatch);eventToMatch!=lastEvent;IncEventPointer(eventToMatch))
sl@0
   472
			{
sl@0
   473
			if ( (eventToMatch->Type()==EEventPointer)  // Must be checked first to ensure it is pointer data checked later
sl@0
   474
					&&	(TAdvancedPointerEventHelper::PointerNumber(*eventToMatch) == TAdvancedPointerEventHelper::PointerNumber(*aEventToPurge))) // same pointer 
sl@0
   475
				{
sl@0
   476
				if ((eventToMatch->Pointer()->iType==aMatchingType) // correct event type
sl@0
   477
						&& (eventToMatch->Handle()==aEventToPurge->Handle())) // same window
sl@0
   478
					{
sl@0
   479
					*eventToMatch=iNullEvent;
sl@0
   480
					return;
sl@0
   481
					}
sl@0
   482
				else if (eventToMatch->Pointer()->iType==aSearchTerminator) // stop searching for mathing type
sl@0
   483
					{
sl@0
   484
					return;
sl@0
   485
					}
sl@0
   486
				}
sl@0
   487
			};
sl@0
   488
		TWsPointer::UnmatchedEventPurged(aMatchingType, aEventToPurge);
sl@0
   489
		}
sl@0
   490
sl@0
   491
/**
sl@0
   492
Starting from pointer event aBasePointerEvent searches the queue for next pointer event for 
sl@0
   493
the same pointer by iterating towards end of queue.
sl@0
   494
sl@0
   495
@param aBasePointerEvent must be of type EEventPointer
sl@0
   496
@return Next pointer event in the queue for the same pointer as aBasePointerEvent 
sl@0
   497
        if it has the same handle as aBasePointerEvent or NULL if it has different handle.
sl@0
   498
        NULL if there is no next pointer event for the same pointer in the queue.
sl@0
   499
*/
sl@0
   500
TWsEvent* CEventQueue::NextPointerEvent(TWsEvent* aBasePointerEvent)
sl@0
   501
	{
sl@0
   502
	WS_ASSERT_DEBUG(aBasePointerEvent->Type() == EEventPointer, EWsPanicCheckEventQueue);
sl@0
   503
	TWsEvent* currentEvent = aBasePointerEvent;
sl@0
   504
	TWsEvent* lastEvent = EventPtr(iCount);
sl@0
   505
	TUint8 pointerNumber = TAdvancedPointerEventHelper::PointerNumber(*aBasePointerEvent);
sl@0
   506
	TUint handle = aBasePointerEvent->Handle();
sl@0
   507
	for(IncEventPointer(currentEvent);currentEvent!=lastEvent;IncEventPointer(currentEvent))
sl@0
   508
		{
sl@0
   509
		if ((currentEvent->Type() == EEventPointer) && 
sl@0
   510
			(TAdvancedPointerEventHelper::PointerNumber(*currentEvent) == pointerNumber))
sl@0
   511
			{
sl@0
   512
			if (currentEvent->Handle() == handle)
sl@0
   513
				{
sl@0
   514
				return currentEvent;
sl@0
   515
				}
sl@0
   516
			else
sl@0
   517
				{
sl@0
   518
				return NULL;
sl@0
   519
				}
sl@0
   520
			}
sl@0
   521
		};
sl@0
   522
	return NULL;
sl@0
   523
	}
sl@0
   524
	
sl@0
   525
/**
sl@0
   526
Checks if aEventToPurge should be purged. If it can be purged, then events that should
sl@0
   527
be purged together with aEventToPurge (matching events) are overwritten with iNullEvent.
sl@0
   528
sl@0
   529
@return ETrue if aEventToPurge should be purged, EFalse otherwise.
sl@0
   530
*/
sl@0
   531
TBool CEventQueue::CheckPurgePointerEvent(TWsEvent* aEventToPurge)
sl@0
   532
	{
sl@0
   533
	switch(aEventToPurge->Pointer()->iType)
sl@0
   534
		{
sl@0
   535
		case TPointerEvent::EDrag:
sl@0
   536
		case TPointerEvent::EMove:
sl@0
   537
		case TPointerEvent::EButtonRepeat:
sl@0
   538
		case TPointerEvent::ESwitchOn:
sl@0
   539
			return ETrue;
sl@0
   540
		case TPointerEvent::EButton1Down:
sl@0
   541
			PurgeEventPairs(aEventToPurge,TPointerEvent::EButton1Up, TPointerEvent::ENullType);
sl@0
   542
			return ETrue;
sl@0
   543
		case TPointerEvent::EButton2Down:
sl@0
   544
			PurgeEventPairs(aEventToPurge,TPointerEvent::EButton2Up, TPointerEvent::ENullType);
sl@0
   545
			return ETrue;
sl@0
   546
		case TPointerEvent::EButton3Down:
sl@0
   547
			PurgeEventPairs(aEventToPurge,TPointerEvent::EButton3Up, TPointerEvent::ENullType);
sl@0
   548
			return ETrue;
sl@0
   549
		case TPointerEvent::EEnterHighPressure:
sl@0
   550
			PurgeEventPairs(aEventToPurge,TPointerEvent::EExitHighPressure, TPointerEvent::EButton1Up);
sl@0
   551
			return ETrue;
sl@0
   552
		case TPointerEvent::EEnterCloseProximity:
sl@0
   553
			{
sl@0
   554
			TWsEvent* nextEvent = NextPointerEvent(aEventToPurge);
sl@0
   555
			if (nextEvent != NULL)
sl@0
   556
				{
sl@0
   557
				switch(nextEvent->Pointer()->iType)
sl@0
   558
					{
sl@0
   559
					case TPointerEvent::EExitCloseProximity: 
sl@0
   560
						*nextEvent = iNullEvent;
sl@0
   561
					case TPointerEvent::EOutOfRange:
sl@0
   562
						return ETrue;
sl@0
   563
					}
sl@0
   564
				}
sl@0
   565
			break;
sl@0
   566
			}
sl@0
   567
		case TPointerEvent::EExitCloseProximity:
sl@0
   568
			{
sl@0
   569
			TWsEvent* nextEvent = NextPointerEvent(aEventToPurge);
sl@0
   570
			if (nextEvent != NULL)
sl@0
   571
				{
sl@0
   572
				switch(nextEvent->Pointer()->iType)
sl@0
   573
					{
sl@0
   574
					case TPointerEvent::EEnterCloseProximity: 
sl@0
   575
						*nextEvent = iNullEvent;
sl@0
   576
					case TPointerEvent::EOutOfRange:
sl@0
   577
						return ETrue;
sl@0
   578
					}
sl@0
   579
				}
sl@0
   580
			break;
sl@0
   581
			}
sl@0
   582
		case TPointerEvent::EOutOfRange:
sl@0
   583
			{
sl@0
   584
			TWsEvent* nextEvent = NextPointerEvent(aEventToPurge);
sl@0
   585
			if ((nextEvent != NULL) &&
sl@0
   586
				(nextEvent->Pointer()->iType == TPointerEvent::EOutOfRange))
sl@0
   587
				{
sl@0
   588
				return ETrue;
sl@0
   589
				}
sl@0
   590
			break;
sl@0
   591
			}
sl@0
   592
		case TPointerEvent::EExitHighPressure:
sl@0
   593
			{
sl@0
   594
			TWsEvent* nextEvent = NextPointerEvent(aEventToPurge);
sl@0
   595
			if ((nextEvent != NULL) &&
sl@0
   596
				(nextEvent->Pointer()->iType == TPointerEvent::EButton1Up))
sl@0
   597
				{
sl@0
   598
				return ETrue;
sl@0
   599
				}
sl@0
   600
			break;
sl@0
   601
			}
sl@0
   602
		case TPointerEvent::EButton1Up:
sl@0
   603
		case TPointerEvent::EButton2Up:
sl@0
   604
		case TPointerEvent::EButton3Up:
sl@0
   605
			break;
sl@0
   606
		}
sl@0
   607
	return EFalse;
sl@0
   608
	}
sl@0
   609
sl@0
   610
/** Purgable events are:
sl@0
   611
	Pointer Up/Down pairs belonging to the same pointer, button and window
sl@0
   612
    Pointer moves & drags
sl@0
   613
    Key messages
sl@0
   614
    Key Up/Down pairs
sl@0
   615
    Key Ups if not foreground connection
sl@0
   616
    Focus lost/gained pairs
sl@0
   617
    EEnterHighPressure/EExitHighPressure pairs belonging to the same pointer
sl@0
   618
    EEnterCloseProximity/EExitCloseProximity pairs belonging to the same pointer
sl@0
   619
    Lone EEnterHighPressure, EExitHighPressure if followed by Up for the same pointer
sl@0
   620
    Lone EEnterCloseProximity, EExitCloseProximity if followed by EOutOfRange for the same pointer
sl@0
   621
    EOutOfRange if followed by another EOutOfRange for the same pointer
sl@0
   622
sl@0
   623
    Events that must not be purged:
sl@0
   624
    Key ups for foreground connections queue
sl@0
   625
    Lone pointer ups, must be delivered to match preceeding pointer down
sl@0
   626
    Lone EExitHighPressure if not followed by Up, must be delivered to match preceeding EEnterHighPressure
sl@0
   627
    Lone EEnterCloseProximity, EExitCloseProximity not followed by EOutOfRange for the same pointer
sl@0
   628
    Lone focus lost/gained messages
sl@0
   629
*/	
sl@0
   630
void CEventQueue::Purge()
sl@0
   631
	{
sl@0
   632
	TWsEvent* eventToPurge;
sl@0
   633
	TWsEvent* eventToMatch;
sl@0
   634
	TInt indexToMatch;
sl@0
   635
	TInt indexToPurge=iCount;
sl@0
   636
	while(indexToPurge>0)
sl@0
   637
		{
sl@0
   638
		eventToPurge=EventPtr(--indexToPurge);
sl@0
   639
		switch(eventToPurge->Type())
sl@0
   640
			{
sl@0
   641
			case EEventPassword:
sl@0
   642
				break;
sl@0
   643
			case EEventMarkInvalid:
sl@0
   644
#if defined(_DEBUG)
sl@0
   645
				WS_PANIC_DEBUG(EWsPanicCheckEventQueue);
sl@0
   646
#endif
sl@0
   647
			case EEventNull:
sl@0
   648
			case EEventKey:
sl@0
   649
			case EEventPointerEnter:
sl@0
   650
			case EEventPointerExit:
sl@0
   651
			case EEventDragDrop:
sl@0
   652
breakLoopAndRemoveEvent:
sl@0
   653
#ifdef LOG_WSERV_EVENTS
sl@0
   654
				RDebug::Print(_L("_WSEVENT: CEventQueue::Purge(), The event to be purged is %S"), &WsEventName(*eventToPurge));
sl@0
   655
#endif
sl@0
   656
				RemoveEvent(indexToPurge);
sl@0
   657
				return;
sl@0
   658
			case EEventKeyUp:
sl@0
   659
				if (iQueueList.First()!=this)
sl@0
   660
					goto breakLoopAndRemoveEvent;
sl@0
   661
				break;
sl@0
   662
			case EEventKeyDown:
sl@0
   663
				if (iQueueList.First()!=this)
sl@0
   664
					goto breakLoopAndRemoveEvent;
sl@0
   665
				for (indexToMatch=indexToPurge+1;indexToMatch<iCount;indexToMatch++)
sl@0
   666
					{
sl@0
   667
					eventToMatch=EventPtr(indexToMatch);
sl@0
   668
					if (eventToMatch->Type()==EEventKeyUp && eventToMatch->Key()->iScanCode==eventToPurge->Key()->iScanCode)
sl@0
   669
						{
sl@0
   670
						*eventToMatch=iNullEvent;
sl@0
   671
						goto breakLoopAndRemoveEvent;
sl@0
   672
						}
sl@0
   673
					}
sl@0
   674
				break;
sl@0
   675
			case EEventModifiersChanged:
sl@0
   676
				for (indexToMatch=indexToPurge;indexToMatch>0;)
sl@0
   677
					{
sl@0
   678
					eventToMatch=EventPtr(--indexToMatch);
sl@0
   679
					if (eventToMatch->Type()==EEventModifiersChanged)
sl@0
   680
						{
sl@0
   681
						eventToPurge->ModifiersChanged()->iChangedModifiers|=eventToMatch->ModifiersChanged()->iChangedModifiers;
sl@0
   682
						indexToPurge=indexToMatch;
sl@0
   683
						goto breakLoopAndRemoveEvent;
sl@0
   684
						}
sl@0
   685
					}
sl@0
   686
				break;
sl@0
   687
			case EEventPointerBufferReady:
sl@0
   688
				CWsPointerBuffer::DiscardPointerMoveBuffer(eventToPurge->Handle());
sl@0
   689
				goto breakLoopAndRemoveEvent;
sl@0
   690
			case EEventFocusLost:
sl@0
   691
			case EEventFocusGained:
sl@0
   692
				if ((indexToPurge+1)<iCount)
sl@0
   693
					{
sl@0
   694
					eventToMatch=EventPtr(indexToPurge+1);
sl@0
   695
					if (eventToMatch->Type()==EEventFocusLost || eventToMatch->Type()==EEventFocusGained)
sl@0
   696
						{
sl@0
   697
						*eventToMatch=iNullEvent;
sl@0
   698
						goto breakLoopAndRemoveEvent;
sl@0
   699
						}
sl@0
   700
					}
sl@0
   701
				break;
sl@0
   702
			case EEventSwitchOn:
sl@0
   703
				if ((indexToPurge+1)<iCount && EventPtr(indexToPurge+1)->Type()==EEventSwitchOn)
sl@0
   704
					goto breakLoopAndRemoveEvent;
sl@0
   705
				break;
sl@0
   706
			case EEventPointer:
sl@0
   707
				if (CheckPurgePointerEvent(eventToPurge))
sl@0
   708
					{
sl@0
   709
					goto breakLoopAndRemoveEvent;
sl@0
   710
					}
sl@0
   711
				break;
sl@0
   712
			}
sl@0
   713
		}
sl@0
   714
	}
sl@0
   715
sl@0
   716
void CEventQueue::PurgePointerEvents()
sl@0
   717
	{
sl@0
   718
	TWsEvent* eventToPurge;
sl@0
   719
	TInt indexToPurge=iCount;
sl@0
   720
	while(indexToPurge>0)
sl@0
   721
		{
sl@0
   722
		eventToPurge=EventPtr(--indexToPurge);
sl@0
   723
		switch(eventToPurge->Type())
sl@0
   724
			{
sl@0
   725
			case EEventPointerBufferReady:
sl@0
   726
				CWsPointerBuffer::DiscardPointerMoveBuffer(eventToPurge->Handle());
sl@0
   727
				RemoveEvent(indexToPurge);
sl@0
   728
				break;
sl@0
   729
			case EEventPointer:
sl@0
   730
				if (CheckPurgePointerEvent(eventToPurge))
sl@0
   731
					{
sl@0
   732
					RemoveEvent(indexToPurge);
sl@0
   733
					}
sl@0
   734
				break;
sl@0
   735
			}
sl@0
   736
		}
sl@0
   737
	}
sl@0
   738
sl@0
   739
/**
sl@0
   740
Purge requested number of oldest events from inactive event queue.
sl@0
   741
@param aSizeRequired the total events required to be cleared. 
sl@0
   742
@return The number of events cleared.
sl@0
   743
*/
sl@0
   744
TInt CEventQueue::PurgeInactiveEvents(const TInt& aSizeRequired)
sl@0
   745
	{
sl@0
   746
	TInt numEventsCleared = 0;
sl@0
   747
	CEventQueue* qptr;
sl@0
   748
	TBool isRemoved;
sl@0
   749
	do	{
sl@0
   750
		TDblQueIter<CEventQueue> iter(iQueueList);
sl@0
   751
		isRemoved = EFalse;
sl@0
   752
		while ((qptr = iter++) != NULL && (aSizeRequired > numEventsCleared))
sl@0
   753
			{
sl@0
   754
			if ((qptr->IsEventCancelled() || (qptr->iEventMsg.IsNull() && !qptr->iEventSignalledState)) &&
sl@0
   755
				(qptr->iQueueSize > EMinQueueSize))
sl@0
   756
				{
sl@0
   757
				// we have a client that is not listening with a size larger than min queue size.
sl@0
   758
				// so lets remove it's oldest event until the number of removed events meet the requirement.
sl@0
   759
				qptr->RemoveEvent(0);
sl@0
   760
				numEventsCleared++;
sl@0
   761
				isRemoved = ETrue;
sl@0
   762
				}
sl@0
   763
			}
sl@0
   764
		} while ((aSizeRequired > numEventsCleared) && isRemoved);
sl@0
   765
	return numEventsCleared;
sl@0
   766
	}
sl@0
   767
sl@0
   768
void CEventQueue::Compress(TCompressMode aCompressMode)
sl@0
   769
	{
sl@0
   770
//
sl@0
   771
// The different purge modes are
sl@0
   772
//
sl@0
   773
// ECompressNoPurge,	// Don't purge anything
sl@0
   774
// ECompressPurge1,		// Don't purge foreground queue
sl@0
   775
// ECompressPurge2,		// Purge all queues
sl@0
   776
//
sl@0
   777
	if (aCompressMode==ECompressPurge2 ||
sl@0
   778
		(this!=iQueueList.First() && aCompressMode==ECompressPurge1))
sl@0
   779
		Purge();
sl@0
   780
	TInt compress=iQueueSize-(iCount>EMinQueueSize?iCount:EMinQueueSize);
sl@0
   781
	if (compress>0)
sl@0
   782
		{
sl@0
   783
		compress=(compress+1)/2;	// Compress half the free space in the queue
sl@0
   784
		TWsEvent* head=EventPtr(0);
sl@0
   785
		TWsEvent* tail=EventPtr(iCount);
sl@0
   786
		if (head>tail)
sl@0
   787
			{
sl@0
   788
			EventCopy(iEventPtr+compress,iEventPtr,tail-iEventPtr);
sl@0
   789
			iHead-=compress;
sl@0
   790
			}
sl@0
   791
		else
sl@0
   792
			{
sl@0
   793
			EventCopy(iEventPtr+compress,head,iCount);
sl@0
   794
			iHead=0;
sl@0
   795
			}
sl@0
   796
		iEventPtr+=compress;
sl@0
   797
		iQueueSize-=compress;
sl@0
   798
		}
sl@0
   799
	}
sl@0
   800
sl@0
   801
void CEventQueue::MoveUp(TInt aNumEvents)
sl@0
   802
	{
sl@0
   803
	if (!aNumEvents)
sl@0
   804
		{
sl@0
   805
		return;
sl@0
   806
		}
sl@0
   807
	EventCopy(iEventPtr+aNumEvents,iEventPtr,iQueueSize);
sl@0
   808
	iEventPtr+=aNumEvents;
sl@0
   809
	}
sl@0
   810
sl@0
   811
TInt CEventQueue::FollowingGap() const
sl@0
   812
	{
sl@0
   813
	TDblQueIter<CEventQueue> iter(iQueueList);
sl@0
   814
	CEventQueue* qptr;
sl@0
   815
	iter.Set(*(CEventQueue *)this);
sl@0
   816
	iter++;
sl@0
   817
	TWsEvent* end;
sl@0
   818
	if ((qptr=iter)!=NULL)
sl@0
   819
		end=qptr->iEventPtr;
sl@0
   820
	else
sl@0
   821
		end=iGlobalEventQueue+iGlobalEventQueueSize;
sl@0
   822
	return(end-(iEventPtr+iQueueSize));
sl@0
   823
	}
sl@0
   824
sl@0
   825
TInt CEventQueue::SqueezeUp()
sl@0
   826
	{
sl@0
   827
	TInt gap=FollowingGap();
sl@0
   828
	MoveUp(gap);
sl@0
   829
	return(gap);
sl@0
   830
	}
sl@0
   831
sl@0
   832
void CEventQueue::SqueezeDown()
sl@0
   833
	{
sl@0
   834
	TDblQueIter<CEventQueue> iter(iQueueList);
sl@0
   835
	iter.Set(*this);
sl@0
   836
	iter--;
sl@0
   837
	CEventQueue* qptr=iter;
sl@0
   838
	if (qptr!=NULL)
sl@0
   839
		{
sl@0
   840
		Compress(ECompressNoPurge);
sl@0
   841
		TInt gap=qptr->FollowingGap();
sl@0
   842
		MoveDown(gap);
sl@0
   843
		}
sl@0
   844
	}
sl@0
   845
sl@0
   846
void CEventQueue::MoveToFront()
sl@0
   847
	{
sl@0
   848
	if (this==iQueueList.First())
sl@0
   849
		return;
sl@0
   850
	Wait();
sl@0
   851
	CEventQueue* qptr;
sl@0
   852
	TInt gap=0;
sl@0
   853
	TDblQueIter<CEventQueue> iter(iQueueList);
sl@0
   854
	iter.SetToLast();
sl@0
   855
	while((qptr=iter--)!=NULL)
sl@0
   856
		{
sl@0
   857
		if (gap<iQueueSize)
sl@0
   858
			qptr->Compress(ECompressNoPurge);
sl@0
   859
		gap=qptr->SqueezeUp();
sl@0
   860
		}
sl@0
   861
	if (gap>=iQueueSize)
sl@0
   862
		EventCopy(iGlobalEventQueue,iEventPtr,iQueueSize);
sl@0
   863
	else
sl@0
   864
		{
sl@0
   865
		EventCopy(iGlobalEventQueue,iEventPtr,gap);
sl@0
   866
		iEventPtr+=gap;
sl@0
   867
		TWsEvent copyBuf[ECopyBufSize];	// temp buffer, can copy upto ECopyBufSize events at a time
sl@0
   868
		TInt eventsToGo=iQueueSize-gap;
sl@0
   869
		iQueueSize=gap;
sl@0
   870
		do
sl@0
   871
			{
sl@0
   872
			TInt copy=Min(eventsToGo,ECopyBufSize);
sl@0
   873
			Mem::Copy(&copyBuf[0],iEventPtr,copy*sizeof(TWsEvent));
sl@0
   874
			iter.Set(*this);
sl@0
   875
			iter--;
sl@0
   876
			while((qptr=iter--)!=NULL)
sl@0
   877
				qptr->MoveUp(copy);
sl@0
   878
			EventCopy(iGlobalEventQueue+iQueueSize,&copyBuf[0],copy);
sl@0
   879
			iQueueSize+=copy;
sl@0
   880
			eventsToGo-=copy;
sl@0
   881
			iEventPtr+=copy;
sl@0
   882
			} while(eventsToGo>0);
sl@0
   883
		}
sl@0
   884
	iEventPtr=iGlobalEventQueue;
sl@0
   885
	this->iLink.Deque();
sl@0
   886
	iQueueList.AddFirst(*this);
sl@0
   887
	__CHECK_QUEUE();
sl@0
   888
	Signal();
sl@0
   889
	}
sl@0
   890
sl@0
   891
// CEventQueue
sl@0
   892
sl@0
   893
CEventQueue::CEventQueue(CWsClient* aOwner) : CEventBase(aOwner)
sl@0
   894
	{
sl@0
   895
	__DECLARE_NAME(_S("CEventQueue"));
sl@0
   896
	}
sl@0
   897
sl@0
   898
CEventQueue::~CEventQueue()
sl@0
   899
	{
sl@0
   900
	RemoveQueue();
sl@0
   901
	}
sl@0
   902
sl@0
   903
void CEventQueue::InitStaticsL()
sl@0
   904
	{
sl@0
   905
	User::LeaveIfError(iMutex.CreateLocal());
sl@0
   906
	}
sl@0
   907
sl@0
   908
void CEventQueue::DeleteStaticsL()
sl@0
   909
	{
sl@0
   910
	iMutex.Close();
sl@0
   911
	}
sl@0
   912
sl@0
   913
void CEventQueue::ConstructL()
sl@0
   914
	{
sl@0
   915
	AddQueueL();
sl@0
   916
	Mem::FillZ(&iNullEvent,sizeof(iNullEvent));
sl@0
   917
	}
sl@0
   918
sl@0
   919
TWsEvent* CEventQueue::EventPtr(TInt index)	
sl@0
   920
	{
sl@0
   921
	return(iEventPtr+((iHead+index)%iQueueSize));
sl@0
   922
	}
sl@0
   923
sl@0
   924
TBool CEventQueue::QueueEvent(const TWsEvent &event)
sl@0
   925
	{
sl@0
   926
	TWservEventPriorities priority=EEventPriorityLow;
sl@0
   927
#ifdef SYMBIAN_PROCESS_MONITORING_AND_STARTUP
sl@0
   928
	if (event.Type()==EEventPassword || event.Type()==EEventSwitchOff || 
sl@0
   929
		event.Type()==EEventKeySwitchOff || event.Type()==EEventRestartSystem)
sl@0
   930
#else
sl@0
   931
	if (event.Type()==EEventPassword || event.Type()==EEventSwitchOff || event.Type()==EEventKeySwitchOff)
sl@0
   932
#endif
sl@0
   933
		{
sl@0
   934
		priority=EEventPriorityHigh;
sl@0
   935
		}
sl@0
   936
	return(QueueEvent(event,priority));
sl@0
   937
	}
sl@0
   938
sl@0
   939
TBool CEventQueue::CheckRoom()
sl@0
   940
//
sl@0
   941
// If the queue is full and room is created return ETrue
sl@0
   942
//
sl@0
   943
	{
sl@0
   944
	TBool ret=EFalse;
sl@0
   945
	Wait();
sl@0
   946
	if (iCount==iQueueSize && Expand(EEventPriorityHigh))
sl@0
   947
		ret=ETrue;
sl@0
   948
	Signal();
sl@0
   949
	return(ret);
sl@0
   950
	}
sl@0
   951
sl@0
   952
TBool CEventQueue::QueueEvent(const TWsEvent &event, TWservEventPriorities aPriority)
sl@0
   953
//
sl@0
   954
// Queue an event, returns ETrue if queued or delivered, EFalse if the queue was full.
sl@0
   955
//
sl@0
   956
	{
sl@0
   957
	TBool ret=ETrue;
sl@0
   958
	Wait();
sl@0
   959
	if (iCount==iQueueSize && !Expand(aPriority))
sl@0
   960
		{
sl@0
   961
		ret=EFalse;
sl@0
   962
#ifdef LOG_WSERV_EVENTS
sl@0
   963
		RDebug::Printf("WSEVENT: CEventQueue::QueueEvent(): 0x%X:  Queue Full!!!!!, iCount = %d, iQueueSize = %d", this, iCount, iQueueSize);
sl@0
   964
		RDebug::Print(_L("WSEVENT: CEventQueue::QueueEvent(): 0x%X:  Queue Full!!!!! TWsEvent.Type() = %S"), this, &WsEventName(event));
sl@0
   965
#endif
sl@0
   966
		}
sl@0
   967
	else
sl@0
   968
		{
sl@0
   969
		if (!iEventMsg.IsNull())
sl@0
   970
			{
sl@0
   971
			SignalEvent();
sl@0
   972
			}
sl@0
   973
#ifdef LOG_WSERV_EVENTS
sl@0
   974
		RDebug::Printf("_WSEVENT: CEventQueue::QueueEvent, Right before adding the event");
sl@0
   975
#endif
sl@0
   976
		*EventPtr(iCount++)=event;
sl@0
   977
#ifdef LOG_WSERV_EVENTS
sl@0
   978
		RDebug::Print(_L("_WSEVENT: CEventQueue::QueueEvent, Event %S successfully queued"), &WsEventName(event));
sl@0
   979
#endif
sl@0
   980
		}
sl@0
   981
	Signal();
sl@0
   982
	return(ret);
sl@0
   983
	}
sl@0
   984
sl@0
   985
TBool CEventQueue::QueueEvent(TUint32 aTarget, TInt aEvent, TInt aIntVal)
sl@0
   986
	{
sl@0
   987
	TWsEvent event;
sl@0
   988
	event.SetType(aEvent);
sl@0
   989
	event.SetHandle(aTarget);
sl@0
   990
	event.SetTimeNow();
sl@0
   991
	*(event.Int()) = aIntVal;
sl@0
   992
	return(QueueEvent(event));
sl@0
   993
	}
sl@0
   994
sl@0
   995
void CEventQueue::UpdateLastEvent(const TWsEvent &event)
sl@0
   996
	{
sl@0
   997
	WS_ASSERT_DEBUG(iCount>0, EWsPanicQueueUpdateCount);
sl@0
   998
	Mem::Copy(EventPtr(iCount-1)->EventData(),event.EventData(),TWsEvent::EWsEventDataSize);
sl@0
   999
	}
sl@0
  1000
sl@0
  1001
/*
sl@0
  1002
Replaces last pointer event related to particular pointer with new one.
sl@0
  1003
sl@0
  1004
While searching for event to replace this method considers all events on the
sl@0
  1005
queue except EMove and EDrag pointer events from pointers different than aEvent. 
sl@0
  1006
If the last of these events under consideration:
sl@0
  1007
(1) is a pointer event, 
sl@0
  1008
(2) has the same type as aEvent,
sl@0
  1009
(3) its type is either EMove or EDrag and
sl@0
  1010
(4) has the same window handle as aEvent,
sl@0
  1011
then it is removed from the queue and aEvent is put at the end of the queue.
sl@0
  1012
sl@0
  1013
@return ETrue if event on the queue has been replaced with aEvent, EFalse otherwise. 
sl@0
  1014
*/
sl@0
  1015
TBool CEventQueue::UpdateLastPointerEvent(const TWsEvent &aEvent)
sl@0
  1016
	{
sl@0
  1017
	if (aEvent.Pointer()->iType == TPointerEvent::EMove || aEvent.Pointer()->iType == TPointerEvent::EDrag)
sl@0
  1018
		{
sl@0
  1019
		Wait();
sl@0
  1020
		
sl@0
  1021
		if (iCount == 0)
sl@0
  1022
			{
sl@0
  1023
			Signal();
sl@0
  1024
			return EFalse;
sl@0
  1025
			}
sl@0
  1026
		
sl@0
  1027
		// loop through all events on the queue starting from the last one
sl@0
  1028
		TWsEvent* evToUpdate = EventPtr(iCount);
sl@0
  1029
		TWsEvent* evOnHead   = &iEventPtr[iHead]; 
sl@0
  1030
		while (evToUpdate != evOnHead)
sl@0
  1031
			{
sl@0
  1032
			DecEventPointer(evToUpdate);
sl@0
  1033
			
sl@0
  1034
			// conditions that stop searching
sl@0
  1035
			if (   (evToUpdate->Type() != EEventPointer)	// found non-pointer event
sl@0
  1036
				|| (evToUpdate->Pointer()->iType != TPointerEvent::EMove && evToUpdate->Pointer()->iType != TPointerEvent::EDrag)	// pointer event but wrong type
sl@0
  1037
				|| (   (TAdvancedPointerEventHelper::PointerNumber(*evToUpdate) == TAdvancedPointerEventHelper::PointerNumber(aEvent))
sl@0
  1038
				    && (   (evToUpdate->Handle() != aEvent.Handle())					// good number & bad handle         
sl@0
  1039
				        || (evToUpdate->Pointer()->iType != aEvent.Pointer()->iType)))) // good number & bad type
sl@0
  1040
				{
sl@0
  1041
				Signal();
sl@0
  1042
				return EFalse;
sl@0
  1043
				}
sl@0
  1044
			else if (TAdvancedPointerEventHelper::PointerNumber(*evToUpdate) == TAdvancedPointerEventHelper::PointerNumber(aEvent))
sl@0
  1045
				{
sl@0
  1046
				// we found event to update: evToUpdate is pointer event with right type, pointer number
sl@0
  1047
				// and window handle
sl@0
  1048
				
sl@0
  1049
				if (evToUpdate == EventPtr(iCount - 1))
sl@0
  1050
					{
sl@0
  1051
					UpdateLastEvent(aEvent);
sl@0
  1052
					}
sl@0
  1053
				else
sl@0
  1054
					{
sl@0
  1055
					RemoveEvent(evToUpdate);
sl@0
  1056
					*EventPtr(iCount++) = aEvent;
sl@0
  1057
					}
sl@0
  1058
				Signal();
sl@0
  1059
				return ETrue;
sl@0
  1060
				}
sl@0
  1061
			
sl@0
  1062
			// evToUpdate is EMove or EDrag pointer event with different pointer id,
sl@0
  1063
			// continue to loop through the queue
sl@0
  1064
			}
sl@0
  1065
		Signal();
sl@0
  1066
		}
sl@0
  1067
	return EFalse;
sl@0
  1068
	}
sl@0
  1069
sl@0
  1070
void CEventQueue::GetData()
sl@0
  1071
//
sl@0
  1072
// If there is an outstanding event in the queue, reply with it's data and remove it from the Q
sl@0
  1073
//
sl@0
  1074
	{
sl@0
  1075
	if (iCount>0)
sl@0
  1076
		{
sl@0
  1077
		WS_ASSERT_DEBUG((iEventPtr+iHead)->Type()!=EEventMarkInvalid, EWsPanicCheckEventQueue);
sl@0
  1078
		CEventBase::GetData(iEventPtr+iHead,sizeof(*iEventPtr));
sl@0
  1079
#ifdef LOG_WSERV_EVENTS
sl@0
  1080
		RDebug::Printf("_WSEVENT: CEventQueue::GetData(), TWsEvent.Type() = %d", (iEventPtr+iHead)->Type());
sl@0
  1081
#endif
sl@0
  1082
		__ZAP_EVENT(iEventPtr+iHead);
sl@0
  1083
		iHead=(iHead+1)%iQueueSize;
sl@0
  1084
		iCount--;
sl@0
  1085
		}
sl@0
  1086
	else
sl@0
  1087
		CEventBase::GetData(&iNullEvent,sizeof(iNullEvent));
sl@0
  1088
	}
sl@0
  1089
sl@0
  1090
void CEventQueue::EventReady(const RMessagePtr2& aEventMsg)
sl@0
  1091
//
sl@0
  1092
// Queue a read of an event notification
sl@0
  1093
//
sl@0
  1094
	{
sl@0
  1095
	EventReadyCheck();
sl@0
  1096
	Wait();
sl@0
  1097
	iEventMsg=aEventMsg;
sl@0
  1098
	if (iCount>0)
sl@0
  1099
		SignalEvent();
sl@0
  1100
	Signal();
sl@0
  1101
	}
sl@0
  1102
sl@0
  1103
void CEventQueue::RemoveEvent(TInt index)
sl@0
  1104
//
sl@0
  1105
// Remove event 'index' in the queue, this event MUST exist in the queue
sl@0
  1106
//
sl@0
  1107
	{
sl@0
  1108
#ifdef LOG_WSERV_EVENTS
sl@0
  1109
	RDebug::Printf("_WSEVENT: CEventQueue::RemoveEvent(index), Remove event index %d in the queue", index);
sl@0
  1110
#endif
sl@0
  1111
	WS_ASSERT_DEBUG(index < iCount, EWsPanicCheckEventQueue);
sl@0
  1112
	RemoveEvent(EventPtr(index));
sl@0
  1113
	}
sl@0
  1114
sl@0
  1115
void CEventQueue::RemoveEvent(TWsEvent* aEvToRemove)
sl@0
  1116
//
sl@0
  1117
// Remove event in the queue, this event MUST exist in the queue
sl@0
  1118
//
sl@0
  1119
	{
sl@0
  1120
#ifdef LOG_WSERV_EVENTS
sl@0
  1121
	RDebug::Print(_L("_WSEVENT: CEventQueue::RemoveEvent(aEvToRemove), Remove event %S in the queue"), &WsEventName(*aEvToRemove));
sl@0
  1122
#endif
sl@0
  1123
	iCount--;
sl@0
  1124
	TWsEvent* last = EventPtr(iCount);
sl@0
  1125
	TWsEvent* prev;
sl@0
  1126
	while(aEvToRemove!=last)
sl@0
  1127
		{
sl@0
  1128
		prev = aEvToRemove;
sl@0
  1129
		IncEventPointer(aEvToRemove);
sl@0
  1130
		*prev = *aEvToRemove;
sl@0
  1131
		}
sl@0
  1132
	__ZAP_EVENT(last);
sl@0
  1133
	}
sl@0
  1134
sl@0
  1135
const TWsEvent* CEventQueue::PeekLastEvent()
sl@0
  1136
//
sl@0
  1137
// Return a read only pointer to the last event in the queue (or NULL if no event)
sl@0
  1138
//
sl@0
  1139
	{
sl@0
  1140
	if (iCount==0)
sl@0
  1141
		return(NULL);
sl@0
  1142
	return(EventPtr(iCount-1));
sl@0
  1143
	}
sl@0
  1144
sl@0
  1145
void CEventQueue::Wait()
sl@0
  1146
	{
sl@0
  1147
	iMutex.Wait();
sl@0
  1148
	}
sl@0
  1149
sl@0
  1150
void CEventQueue::Signal()
sl@0
  1151
	{
sl@0
  1152
	iMutex.Signal();
sl@0
  1153
	}
sl@0
  1154
sl@0
  1155
void CEventQueue::WalkEventQueue(EventQueueWalk aFunc, TAny* aFuncParam)
sl@0
  1156
	{
sl@0
  1157
	Wait();
sl@0
  1158
restart:
sl@0
  1159
	for (TInt index=0;index<iCount;index++)
sl@0
  1160
		{
sl@0
  1161
		TWsEvent* event=EventPtr(index);
sl@0
  1162
		switch((aFunc)(aFuncParam,event))
sl@0
  1163
			{
sl@0
  1164
			case EEventQueueWalkDeleteEvent:
sl@0
  1165
				RemoveEvent(index--);
sl@0
  1166
			case EEventQueueWalkOk:
sl@0
  1167
				break;
sl@0
  1168
			case EEventQueueWalkRestart:
sl@0
  1169
				goto restart;
sl@0
  1170
			}
sl@0
  1171
		}
sl@0
  1172
	Signal();
sl@0
  1173
	}