windowing/windowserver/nonnga/SERVER/UTILS.CPP
changeset 0 5d03bc08d59c
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 1995-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 // General utility functions that don't belong anywhere else
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "server.h"
       
    19 #include "rootwin.h"
       
    20 #include "wstop.h"
       
    21 #include "panics.h"
       
    22 
       
    23 GLREF_D CDebugLogBase *wsDebugLog;
       
    24 
       
    25 GLDEF_C RWsRegion *GetRegionFromClientL(CWsClient *aClient, TInt aCount)
       
    26 	{
       
    27 	TInt bufSize=sizeof(TRect)*aCount;
       
    28 	TUint8* rectList=STATIC_CAST(TUint8*,User::Alloc(bufSize));
       
    29 	User::LeaveIfNull(rectList);
       
    30 	CleanupStack::PushL(rectList);
       
    31 	TPtr8 rectBuf(rectList,bufSize);
       
    32 	aClient->RemoteRead(rectBuf,0);
       
    33 	RWsRegion* region=new(ELeave) RWsRegion(aCount,REINTERPRET_CAST(TRect*,rectList));
       
    34 	CleanupStack::Pop(rectList);
       
    35 	return(region);
       
    36 	}
       
    37 
       
    38 GLDEF_C TInt ExternalizeRegionL(RWriteStream& aWriteStream, const RWsRegion& aRegion)
       
    39 	{
       
    40 	TInt dataLen = sizeof(TRect)*aRegion.Count();
       
    41 	aWriteStream.WriteInt32L(aRegion.Count());
       
    42 	aWriteStream.WriteL(REINTERPRET_CAST(const TUint8*,aRegion.RectangleList()),dataLen);
       
    43 	aWriteStream.CommitL();
       
    44 	return sizeof(TInt32)+dataLen;
       
    45 	}
       
    46 
       
    47 GLDEF_C RWsRegion* InternalizeRegionL(RReadStream& aReadStream)
       
    48 	{
       
    49 	TInt numRects = aReadStream.ReadInt32L();
       
    50 	TInt bufSize = sizeof(TRect)*numRects;
       
    51 	// Allocate buffer for list of clipping rectangles. We leave if we 
       
    52 	// cannot create the buffer
       
    53 	TUint8* rectList = static_cast<TUint8*>(User::AllocL(bufSize));
       
    54 	CleanupStack::PushL(rectList);
       
    55 	// Read the list of rectangles into the buffer
       
    56 	aReadStream.ReadL(rectList,bufSize);
       
    57 	// Point member pointer to our new rectangle list buffer
       
    58 	RWsRegion* region = new(ELeave) RWsRegion(numRects,reinterpret_cast<TRect*>(rectList));
       
    59 	CleanupStack::Pop(rectList);
       
    60 	return region;
       
    61 	}
       
    62 
       
    63 void Panic(TWservPanic aPanic)
       
    64 	{
       
    65 	if (wsDebugLog)
       
    66 		wsDebugLog->Panic(CDebugLogBase::EDummyConnectionId,aPanic);		//Dummy value meaning WSERV
       
    67 	_LIT(KWSERVInternalPanicCategory,"WSERV-INTERNAL");
       
    68 	User::Panic(KWSERVInternalPanicCategory,aPanic);
       
    69 	}
       
    70 
       
    71 _LIT(KWSERVPanicDesc1,"WServ internal Panic %S, in file %S @ line %i");
       
    72 _LIT(KWSERVPanicDesc2,"Assert condition = \"%S\"");
       
    73 
       
    74 void PanicWithInfo(TWservPanic aPanic, const TDesC& aFileName, const TDesC& aPanicName, TInt aLine)
       
    75 	{
       
    76 	TBuf<256> buf;
       
    77 	buf.Format(KWSERVPanicDesc1, &aPanicName, &aFileName, aLine);
       
    78 	RDebug::Print(buf);
       
    79 	if (wsDebugLog)
       
    80 		{
       
    81 		wsDebugLog->MiscMessage(CDebugLogBase::ELogImportant, buf);
       
    82 		}
       
    83 	Panic(aPanic);
       
    84 	}
       
    85 
       
    86 void PanicWithCondAndInfo(TWservPanic aPanic, const TDesC& aCondition, const TDesC& aFileName, const TDesC& aPanicName, TInt aLine)
       
    87 	{
       
    88 	TBuf<256> buf;
       
    89 	buf.Format(KWSERVPanicDesc1, &aPanicName, &aFileName, aLine);
       
    90 	RDebug::Print(buf);
       
    91 	if (wsDebugLog)
       
    92 		{
       
    93 		wsDebugLog->MiscMessage(CDebugLogBase::ELogImportant, buf);
       
    94 		}
       
    95 	buf.Format(KWSERVPanicDesc2, &aCondition);
       
    96 	RDebug::Print(buf);
       
    97 	if (wsDebugLog)
       
    98 		{
       
    99 		wsDebugLog->MiscMessage(CDebugLogBase::ELogImportant, buf);
       
   100 		}
       
   101 	Panic(aPanic);
       
   102 	}
       
   103 
       
   104 
       
   105 GLDEF_C void StateDump()
       
   106 	{
       
   107 	CWsTop::StateDump();
       
   108 	}
       
   109 
       
   110 GLDEF_C void StateDump(CWsRootWindow* aRootWindow)
       
   111 	{
       
   112 	TBool enabled=wsDebugLog!=NULL;
       
   113 	if (!enabled)
       
   114 		{
       
   115 		CWsTop::EnableLogging();
       
   116 		if (!wsDebugLog)
       
   117 			return;	//	Failed to enable logging so give up
       
   118 		}
       
   119 //
       
   120 	_LIT(LogLine,"===========");
       
   121 	_LIT(KWSERVStateLogWindowTree,"Window tree");
       
   122 	TBuf<128> buf;
       
   123 	wsDebugLog->MiscMessage(CDebugLogBase::ELogImportant,LogLine);
       
   124 	wsDebugLog->MiscMessage(CDebugLogBase::ELogImportant,KWSERVStateLogWindowTree);
       
   125 	wsDebugLog->MiscMessage(CDebugLogBase::ELogImportant,LogLine);
       
   126 
       
   127 	CWsWindowBase *win=aRootWindow;
       
   128 	TInt inset=0;
       
   129 	_LIT(KWSERVStateInsetLevelValue,"%*p");
       
   130 	FOREVER
       
   131 		{
       
   132 		buf.Format(KWSERVStateInsetLevelValue,Min(inset<<1,20));
       
   133 		win->StatusDump(buf);
       
   134 		wsDebugLog->MiscMessage(CDebugLogBase::ELogImportant,buf);
       
   135 		if (win->BaseChild())
       
   136 			{
       
   137 			++inset;
       
   138 			win=win->BaseChild();
       
   139 			continue;
       
   140 			}
       
   141 		while(!win->NextSibling() && win!=aRootWindow)
       
   142 			{
       
   143 			win=win->BaseParent();
       
   144 			--inset;
       
   145 			}
       
   146 		if (win==aRootWindow)
       
   147 			break;		//Will break here if there is only the root window or the tree walk has returned to it
       
   148 		win=win->NextSibling();
       
   149 		}
       
   150 	wsDebugLog->MiscMessage(CDebugLogBase::ELogImportant,LogLine);
       
   151 //
       
   152 	if (!enabled)
       
   153 		CWsTop::DisableLogging();
       
   154 	}
       
   155 
       
   156 GLDEF_C void HeapDump()
       
   157 	{
       
   158 	TBool enabled=(wsDebugLog!=NULL);
       
   159 	if (!enabled)
       
   160 		CWsTop::EnableLogging();
       
   161 	if (wsDebugLog)	// Just in case enable failed
       
   162 		wsDebugLog->HeapDump();
       
   163 	if (!enabled)
       
   164 		CWsTop::DisableLogging();
       
   165 	}
       
   166 
       
   167 #if defined(_DEBUG)
       
   168 //GLREF_C void ForceLog(TInt aPriority,const TDesC &aText,TInt aParam=0);
       
   169 //ForceLog(CDebugLogBase::ELogImportant,_L("Value=%d"),345);
       
   170 //TLogMessageText buf;
       
   171 //_LIT(KLog,"Count=%d, Object=0x%x");
       
   172 //buf.Format(KLog,Count(),this);
       
   173 //ForceLog(CDebugLogBase::ELogImportant,buf);
       
   174 GLDEF_C void ForceLog(TInt aPriority,const TDesC &aText,TInt aParam=0)
       
   175 	{
       
   176 	TBool enabled=(wsDebugLog!=NULL);
       
   177 	if (!enabled)
       
   178 		CWsTop::EnableLogging();
       
   179 	if (wsDebugLog)	// Just in case enable failed
       
   180 		{
       
   181 		wsDebugLog->MiscMessage(aPriority,aText,aParam);
       
   182 		if (!enabled)
       
   183 			CWsTop::DisableLogging();
       
   184 		}
       
   185 	}
       
   186 #endif
       
   187 
       
   188 LOCAL_C TDisplayMode DisplayMode(TBool aIsColor,TInt aNumColors)
       
   189 	{
       
   190 
       
   191 	if (aIsColor)
       
   192 		{
       
   193 		switch(aNumColors)
       
   194 			{
       
   195 		case 16:
       
   196 			return EColor16;
       
   197 		case 256:
       
   198 			return EColor256;
       
   199 		case 4096:
       
   200 			return EColor4K;
       
   201 		case 65536:
       
   202 			return EColor64K;
       
   203 		case 16777216:
       
   204 			return CFbsDevice::DisplayMode16M();
       
   205 		default:
       
   206 			return ENone;
       
   207 			}
       
   208 		}
       
   209 	else
       
   210 		{
       
   211 		switch(aNumColors)
       
   212 			{
       
   213 		case 2:
       
   214 			return EGray2;
       
   215 		case 4:
       
   216 			return EGray4;
       
   217 		case 16:
       
   218 			return EGray16;
       
   219 		case 256:
       
   220 			return EGray256;
       
   221 		default:
       
   222 			return ENone;
       
   223 			}
       
   224 		}
       
   225 	}
       
   226 
       
   227 GLDEF_C TDisplayMode ParseDisplayMode(const TDesC& aModeName)
       
   228 	{
       
   229 	// Not using _LIT because we only want the string temporarily, not permanently on the heap.
       
   230 	if (!aModeName.CompareF(_L("EColor16MAP")))
       
   231 		return EColor16MAP;
       
   232 	else if (!aModeName.CompareF(_L("EColor16MA")))
       
   233 		return EColor16MA;
       
   234 	else if (!aModeName.CompareF(_L("EColor16MU")))
       
   235 		return EColor16MU;
       
   236 	else if (!aModeName.CompareF(_L("EColor64K")))
       
   237 		return EColor64K;
       
   238 	else if (!aModeName.CompareF(_L("EColor4K")))
       
   239 		return EColor4K;
       
   240 	else if (!aModeName.CompareF(_L("EColor256")))
       
   241 		return EColor256;
       
   242 	else if (!aModeName.CompareF(_L("EColor16")))
       
   243 		return EColor16;
       
   244 	else if (!aModeName.CompareF(_L("EGray256")))
       
   245 		return EGray256;
       
   246 	else if (!aModeName.CompareF(_L("EGray16")))
       
   247 		return EGray16;
       
   248 	else if (!aModeName.CompareF(_L("EGray4")))
       
   249 		return EGray4;
       
   250 	else if (!aModeName.CompareF(_L("EGray2")))
       
   251 		return EGray2;
       
   252 	else	// old wserv magic
       
   253 		{	// this code supports the old "ColorXXX" and "GrayXXX" formats etc
       
   254 		TPtrC ptr(aModeName);
       
   255 		TBool isColor=EFalse;
       
   256 
       
   257 		if (ptr[0]=='C' || ptr[0]=='c')
       
   258 			isColor=ETrue;
       
   259 		else if (ptr[0]=='A' || ptr[0]=='a')
       
   260 			return EColor16MA;
       
   261 		else if (ptr[0]=='P' || ptr[0]=='p')
       
   262 			return EColor16MAP;
       
   263 		else if (ptr[0]!='G' && ptr[0]!='g')
       
   264 			return ENone;
       
   265 		
       
   266 		TInt pos=0;
       
   267 		while (!TChar(ptr[++pos]).IsDigit())
       
   268 			{}
       
   269 		TLex lex(ptr.Mid(pos));
       
   270 		TInt numCols;
       
   271 		if (lex.Val(numCols)!=KErrNone)
       
   272 			return ENone;
       
   273 		lex.SkipSpace();
       
   274 		TChar units=lex.Get();
       
   275 		if (units=='K' || units=='k')
       
   276 			numCols<<=10;
       
   277 		else if (units=='M' || units=='m')
       
   278 			numCols<<=20;
       
   279 		TDisplayMode mode = DisplayMode(isColor,numCols);
       
   280 
       
   281 		#if defined(__WINS__)
       
   282 			//This is a special case for the emulator since the default
       
   283 			//screen packing can be overridden by the wsini.ini setting
       
   284 
       
   285 			//This code does nothing is iDefaultDisplayMode is not
       
   286 			//EColor16M.- see graphics/screendriver/swins/scnew.cpp
       
   287 			//function CFbsDrawDevice::DisplayMode16M().
       
   288 			if(mode==EColor16M)
       
   289 				{
       
   290 				TChar packed=lex.Get();
       
   291 				if ((packed=='U')||(packed=='u'))
       
   292 					{
       
   293 					mode=EColor16MU;
       
   294 					}
       
   295 				}
       
   296 
       
   297 			//However setting this will give four shades of grey since
       
   298 			//the code does not support 16MU and 16M at the same time.
       
   299 		#endif
       
   300 		return mode;
       
   301 		}
       
   302 	}
       
   303