os/graphics/windowing/windowserver/nga/graphicdrawer/graphicmsgbuf.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1995-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
//
sl@0
    15
sl@0
    16
#include "graphics/WSGRAPHICMSGBUF.H"
sl@0
    17
#include "W32STDGRAPHIC.H"
sl@0
    18
sl@0
    19
//so sue us; fixing a very nasty-to-track-down unsigned/signed conversion defect in code below
sl@0
    20
#define KSizeOfTInt (TInt)sizeof(TInt)
sl@0
    21
sl@0
    22
// TWsGraphicMsgBufParser \\\\\\\\\\\\\\\\\\\\\\\\
sl@0
    23
sl@0
    24
EXPORT_C TWsGraphicMsgBufParser::TWsGraphicMsgBufParser(const TDesC8& aData): iData(aData)
sl@0
    25
/** Initialise a parser for the specified message buffer */
sl@0
    26
	{
sl@0
    27
	}
sl@0
    28
sl@0
    29
EXPORT_C TInt TWsGraphicMsgBufParser::Verify() const
sl@0
    30
/** Verifies that the message buffer is properly formed
sl@0
    31
	@return KErrNone if buffer is ok, else a system-wide error code */
sl@0
    32
	{
sl@0
    33
	const TInt length = iData.Length();
sl@0
    34
	TInt ofs = 0;
sl@0
    35
	const TInt tmp = (length - sizeof(TInt));
sl@0
    36
 	while(ofs < tmp)
sl@0
    37
		{
sl@0
    38
		ofs += IntAt(ofs+KSizeOfTInt) + (KSizeOfTInt*2);
sl@0
    39
		}
sl@0
    40
	if(ofs == length)
sl@0
    41
		{
sl@0
    42
		return KErrNone;
sl@0
    43
		}
sl@0
    44
	return KErrCorrupt;
sl@0
    45
	}
sl@0
    46
sl@0
    47
EXPORT_C TInt TWsGraphicMsgBufParser::Count() const
sl@0
    48
/** Returns the number of elements in the buffer.
sl@0
    49
@return the count of elements.
sl@0
    50
*/
sl@0
    51
	{
sl@0
    52
	const TInt length = iData.Length();
sl@0
    53
	TInt ofs = 0, count = 0;
sl@0
    54
	while(ofs < length)
sl@0
    55
		{
sl@0
    56
		count++;
sl@0
    57
		ofs += IntAt(ofs+KSizeOfTInt) + (KSizeOfTInt*2);
sl@0
    58
		}
sl@0
    59
	return count;
sl@0
    60
	}
sl@0
    61
sl@0
    62
EXPORT_C TUid TWsGraphicMsgBufParser::Uid(TInt aIndex) const
sl@0
    63
/** Returns the UID if the message, or null UID if the buffer is empty.
sl@0
    64
@return KNullUid if the buffer is empty, otherwise the stored Uid
sl@0
    65
*/
sl@0
    66
	{
sl@0
    67
	const TInt length = iData.Length();
sl@0
    68
	TInt ofs = 0, count = 0;
sl@0
    69
	while(ofs < length)
sl@0
    70
		{
sl@0
    71
		if(count == aIndex)
sl@0
    72
			{
sl@0
    73
			return TUid::Uid(IntAt(ofs));
sl@0
    74
			}
sl@0
    75
		count++;
sl@0
    76
		ofs += IntAt(ofs+KSizeOfTInt) + (KSizeOfTInt*2);
sl@0
    77
		}
sl@0
    78
	return KNullUid;
sl@0
    79
	}
sl@0
    80
sl@0
    81
EXPORT_C TPtrC8 TWsGraphicMsgBufParser::Data(TInt aIndex) const
sl@0
    82
/**Returns the buffer contents at a perticular offset (aIndex).
sl@0
    83
@param aIndex - the index into of the buffer element required.
sl@0
    84
@return KNullDesC8 if index is more than the element count, otherwise, the element at index aIndex.
sl@0
    85
*/
sl@0
    86
	{
sl@0
    87
	const TInt length = iData.Length();
sl@0
    88
	TInt ofs = 0, count = 0;
sl@0
    89
	while(ofs < length)
sl@0
    90
		{
sl@0
    91
		if(count == aIndex)
sl@0
    92
			{
sl@0
    93
			return iData.Mid(ofs+(KSizeOfTInt*2),IntAt(ofs+KSizeOfTInt));
sl@0
    94
			}
sl@0
    95
		count++;
sl@0
    96
		ofs += IntAt(ofs+KSizeOfTInt) + (KSizeOfTInt*2);
sl@0
    97
		}
sl@0
    98
	return TPtrC8(KNullDesC8());
sl@0
    99
	}
sl@0
   100
sl@0
   101
EXPORT_C TInt TWsGraphicMsgBufParser::Find(TUid aUid,TInt aStartingFrom) const
sl@0
   102
/** Finds the element equal to the aUid, and returns the index of the element in the buffer.
sl@0
   103
@param aUid - the search item to be found in the buffer.
sl@0
   104
@param aStartingFrom - the starting index.
sl@0
   105
@return the position (index) of the found element, or KErrNotFound
sl@0
   106
*/
sl@0
   107
	{
sl@0
   108
	const TInt length = iData.Length();
sl@0
   109
	TInt ofs = 0, count = 0;
sl@0
   110
	while(ofs < length)
sl@0
   111
		{
sl@0
   112
		if((count >= aStartingFrom) && (aUid == TUid::Uid(IntAt(ofs))))
sl@0
   113
			{
sl@0
   114
			return count;
sl@0
   115
			}
sl@0
   116
		count++;
sl@0
   117
		ofs += IntAt(ofs+KSizeOfTInt) + (KSizeOfTInt*2);
sl@0
   118
		}
sl@0
   119
	return KErrNotFound;
sl@0
   120
	}
sl@0
   121
sl@0
   122
EXPORT_C TBool TWsGraphicMsgBufParser::LoadFixed(TUid aUid,TAny* aMsg,TInt aMsgSize,TInt aStartingFrom) const
sl@0
   123
/** Loads the buffer of aMsg with the contents of the buffer, based on the aUid and aMsgSize.
sl@0
   124
@param aUid - the Uid to match after which the loading is performed.
sl@0
   125
@param aMsg - the pointer to the output buffer.
sl@0
   126
@param aMsgSize - the size of the output buffer.
sl@0
   127
@param aStartingFrom - the starting position to be used in the search of the buffer.
sl@0
   128
@return ETrue if loaded, EFalse otherwise.
sl@0
   129
*/
sl@0
   130
	{
sl@0
   131
	const TInt length = iData.Length();
sl@0
   132
	TInt ofs = 0, count = 0;
sl@0
   133
	while(ofs < length)
sl@0
   134
		{
sl@0
   135
		if((count >= aStartingFrom) && (aUid == TUid::Uid(IntAt(ofs))))
sl@0
   136
			{
sl@0
   137
			// found it?  return it
sl@0
   138
			const TInt len = IntAt(ofs+KSizeOfTInt);
sl@0
   139
			if(len == aMsgSize)
sl@0
   140
				{
sl@0
   141
				TPtr8 msg(reinterpret_cast<TUint8*>(aMsg),aMsgSize);
sl@0
   142
				msg = iData.Mid(ofs+(KSizeOfTInt*2),len);
sl@0
   143
				return ETrue;
sl@0
   144
				}
sl@0
   145
			else // message was not the expected size!
sl@0
   146
				{
sl@0
   147
				return EFalse;
sl@0
   148
				}
sl@0
   149
			}
sl@0
   150
		count++;
sl@0
   151
		ofs += IntAt(ofs+KSizeOfTInt) + (KSizeOfTInt*2);
sl@0
   152
		}
sl@0
   153
	return EFalse;
sl@0
   154
	}
sl@0
   155
sl@0
   156
TInt TWsGraphicMsgBufParser::IntAt(TInt aOfs) const
sl@0
   157
/** @internalComponent @released */
sl@0
   158
	{
sl@0
   159
	if((aOfs < 0) || ((aOfs+KSizeOfTInt) > iData.Length()))
sl@0
   160
		{
sl@0
   161
		return 0;
sl@0
   162
		}
sl@0
   163
	TInt ret;
sl@0
   164
	memcpy(&ret,iData.Ptr()+aOfs,KSizeOfTInt);
sl@0
   165
	return ret;
sl@0
   166
	}
sl@0
   167
sl@0
   168
// TWsGraphicMsgAnimation \\\\\\\\\\\\\\\\\\\\\\\\
sl@0
   169
sl@0
   170
EXPORT_C TWsGraphicMsgAnimation::TWsGraphicMsgAnimation(): iFlags(EStopped)
sl@0
   171
	{
sl@0
   172
	}
sl@0
   173
sl@0
   174
EXPORT_C TInt TWsGraphicMsgAnimation::Load(const TWsGraphicMsgBufParser& aData)
sl@0
   175
	{
sl@0
   176
	const TInt index = aData.Find(TUid::Uid(TWsGraphicAnimation::ETypeId));
sl@0
   177
	if(index >= 0)
sl@0
   178
		{
sl@0
   179
		return Load(aData,index);
sl@0
   180
		}
sl@0
   181
	return index;
sl@0
   182
	}
sl@0
   183
sl@0
   184
EXPORT_C TInt TWsGraphicMsgAnimation::Load(const TWsGraphicMsgBufParser& aData,TInt aIndex)
sl@0
   185
	{
sl@0
   186
	if(aData.Uid(aIndex).iUid != TWsGraphicAnimation::ETypeId)
sl@0
   187
		{
sl@0
   188
		return KErrArgument;
sl@0
   189
		}
sl@0
   190
	const TPtrC8 pckg = aData.Data(aIndex);
sl@0
   191
	if(pckg.Size() != sizeof(TWsGraphicMsgAnimation))
sl@0
   192
		{
sl@0
   193
		return KErrCorrupt;
sl@0
   194
		}
sl@0
   195
	memcpy(this,pckg.Ptr(),sizeof(TWsGraphicMsgAnimation));
sl@0
   196
	return KErrNone;
sl@0
   197
	}
sl@0
   198
sl@0
   199
EXPORT_C TTimeIntervalMicroSeconds TWsGraphicMsgAnimation::AnimationTime(const TTime& aNow,const TTimeIntervalMicroSeconds& aAnimationLength) const
sl@0
   200
	{
sl@0
   201
	// an animation to time?
sl@0
   202
	if(aAnimationLength <= 0LL)
sl@0
   203
		{
sl@0
   204
		return 0LL;
sl@0
   205
		}
sl@0
   206
	switch(iFlags & EStateMask)
sl@0
   207
		{
sl@0
   208
		case EPaused:
sl@0
   209
			return ((iPauseOrStopping.Int64() - iPlay.Int64()) % aAnimationLength.Int64());
sl@0
   210
		case EStopping:
sl@0
   211
			{
sl@0
   212
			const TInt64 elapsed = (aNow.Int64() - iPlay.Int64());
sl@0
   213
			if(elapsed <= aAnimationLength.Int64())
sl@0
   214
				{
sl@0
   215
				return (elapsed % aAnimationLength.Int64());
sl@0
   216
				}
sl@0
   217
			return 0LL;
sl@0
   218
			}
sl@0
   219
		case EStopped:
sl@0
   220
			return 0LL;
sl@0
   221
		case EPlaying:
sl@0
   222
			{
sl@0
   223
			const TInt64 elapsed = (aNow.Int64() - iPlay.Int64());
sl@0
   224
			if((iFlags & ELoop) || (elapsed <= aAnimationLength.Int64()))
sl@0
   225
				{
sl@0
   226
				return (elapsed % aAnimationLength.Int64());
sl@0
   227
				}
sl@0
   228
			return 0LL;
sl@0
   229
			}
sl@0
   230
		default:
sl@0
   231
			return 0LL;
sl@0
   232
		}
sl@0
   233
	}
sl@0
   234
sl@0
   235
EXPORT_C TBool TWsGraphicMsgAnimation::IsPlaying(const TTime& aNow,const TTimeIntervalMicroSeconds& aAnimationLength) const
sl@0
   236
	{
sl@0
   237
	// an animation to time?
sl@0
   238
	if(aAnimationLength <= 0LL)
sl@0
   239
		{
sl@0
   240
		return EFalse;
sl@0
   241
		}
sl@0
   242
	switch(iFlags & EStateMask)
sl@0
   243
		{
sl@0
   244
		case EPaused:
sl@0
   245
			return EFalse;
sl@0
   246
		case EStopping:
sl@0
   247
			{
sl@0
   248
			const TInt64 elapsed = (aNow.Int64() - iPlay.Int64());
sl@0
   249
			if(elapsed <= aAnimationLength.Int64())
sl@0
   250
				{
sl@0
   251
				return ETrue;
sl@0
   252
				}
sl@0
   253
			return EFalse;
sl@0
   254
			}
sl@0
   255
		case EStopped:
sl@0
   256
			return EFalse;
sl@0
   257
		case EPlaying:
sl@0
   258
			{
sl@0
   259
			const TInt64 elapsed = (aNow.Int64() - iPlay.Int64());
sl@0
   260
			if((iFlags & ELoop) || (elapsed <= aAnimationLength.Int64()))
sl@0
   261
				{
sl@0
   262
				return ETrue;
sl@0
   263
				}
sl@0
   264
			return EFalse;
sl@0
   265
			}
sl@0
   266
		default:
sl@0
   267
			return EFalse;
sl@0
   268
		}
sl@0
   269
	}
sl@0
   270