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