os/boardsupport/emulator/emulatorbsp/specific/property.cpp
changeset 0 bde4ae8d615e
child 1 260cb5ec6c19
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/boardsupport/emulator/emulatorbsp/specific/property.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1390 @@
     1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// wins\specific\property.cpp
    1.18 +// Emulator property management for emulator settings
    1.19 +// 
    1.20 +//
    1.21 +
    1.22 +#define _CRTIMP			// we want to use the static runtime library
    1.23 +
    1.24 +#define __INCLUDE_CAPABILITY_NAMES__
    1.25 +#include "variant.h"
    1.26 +#include <string.h>
    1.27 +#include <stdlib.h>
    1.28 +#include <emulator.h>
    1.29 +
    1.30 +#ifdef _DEBUG
    1.31 +#define _DUMP_PROPERTY
    1.32 +#endif
    1.33 +
    1.34 +const char* KDefaultMachineName = "epoc";
    1.35 +const char* KDefaultTestMachineName = "defaulttest";
    1.36 +
    1.37 +// name of the environment variable to check for default path
    1.38 +const char* KDefaultEpocRootName = "EPOCROOT";
    1.39 +
    1.40 +TInt ScreenId=0;
    1.41 +
    1.42 +// At the point this is created there is no kernel heap available
    1.43 +// So all memory allocation is done from a custom allocator
    1.44 +
    1.45 +const TInt KMaxTokenLength = 255;
    1.46 +
    1.47 +class Allocator
    1.48 +	{
    1.49 +	enum {ETotalMemory=0x10000};	// 64K
    1.50 +public:
    1.51 +	Allocator();
    1.52 +	TAny* Alloc(TInt aSize);
    1.53 +	TAny* Realloc(TAny* aCell, TInt aSize);
    1.54 +	void Free(TAny* aCell);
    1.55 +private:
    1.56 +	TUint iBuf[ETotalMemory];
    1.57 +	TUint* iFree;
    1.58 +	};
    1.59 +
    1.60 +Allocator::Allocator()
    1.61 +	:iFree(iBuf)
    1.62 +	{}
    1.63 +
    1.64 +TAny* Allocator::Alloc(TInt aSize)
    1.65 +	{
    1.66 +	aSize = (aSize + 7) >> 2;
    1.67 +	if (iFree + aSize > iBuf + ETotalMemory)
    1.68 +		return NULL;
    1.69 +	TUint* p = iFree;
    1.70 +	iFree += aSize;
    1.71 +	*p++ = aSize;
    1.72 +	return p;
    1.73 +	}
    1.74 +
    1.75 +TAny* Allocator::Realloc(TAny* aCell, TInt aSize)
    1.76 +	{
    1.77 +	if (!aCell)
    1.78 +		return Alloc(aSize);
    1.79 +
    1.80 +	TUint* p = (TUint*)aCell;
    1.81 +	TInt size = *--p;
    1.82 +	if (iFree == p + size)
    1.83 +		{
    1.84 +		aSize = (aSize + 7) >> 2;
    1.85 +		if (p + aSize > iBuf + ETotalMemory)
    1.86 +			return NULL;
    1.87 +		iFree = p + aSize;
    1.88 +		*p = aSize;
    1.89 +		return aCell;
    1.90 +		}
    1.91 +
    1.92 +	TAny* newp = Alloc(aSize);
    1.93 +	if (newp)
    1.94 +		{
    1.95 +		memcpy(newp, aCell, size*sizeof(TUint));
    1.96 +		Free(aCell);
    1.97 +		}
    1.98 +	return newp;
    1.99 +	}
   1.100 +
   1.101 +void Allocator::Free(TAny* )
   1.102 +	{
   1.103 +	}
   1.104 +
   1.105 +Allocator TheAllocator;
   1.106 +
   1.107 +///////
   1.108 +
   1.109 +char* Duplicate(const char* aString)
   1.110 +	{
   1.111 +	if (aString == NULL)
   1.112 +		aString = "";
   1.113 +
   1.114 +	TInt size = strlen(aString) + 1;
   1.115 +	char* p = (char*)TheAllocator.Alloc(size);
   1.116 +	if (p)
   1.117 +		memcpy(p, aString, size);
   1.118 +	return p;
   1.119 +	}
   1.120 +
   1.121 +// Properties class implementation
   1.122 +
   1.123 +Properties::Properties()
   1.124 +	:iEntries(NULL),iCount(0),iSize(0)
   1.125 +	{}
   1.126 +
   1.127 +const char* Properties::Insert(TInt aIndex, const char* aProperty, const char* aValue)
   1.128 +	{
   1.129 +	if (iCount == iSize)
   1.130 +		{
   1.131 +		TInt size = iSize == 0 ? 8 : iSize*2;
   1.132 +		TAny* array = TheAllocator.Realloc(iEntries, size*sizeof(SEntry));
   1.133 +		if (array == NULL)
   1.134 +			return NULL;
   1.135 +		iEntries = (SEntry*)array;
   1.136 +		iSize = size;
   1.137 +		}
   1.138 +
   1.139 +	char* prop = Duplicate(aProperty);
   1.140 +	if (prop == NULL)
   1.141 +		return NULL;
   1.142 +	char* value = Duplicate(aValue);
   1.143 +	if (value == NULL)
   1.144 +		TheAllocator.Free(prop);
   1.145 +	else
   1.146 +		{
   1.147 +		SEntry* e = &iEntries[aIndex];
   1.148 +		memmove(e+1, e, (iCount-aIndex)*sizeof(SEntry));
   1.149 +		e->iProperty = prop;
   1.150 +		e->iValue = value;
   1.151 +		++iCount;
   1.152 +		}
   1.153 +	return value;
   1.154 +	}
   1.155 +
   1.156 +const char* Properties::Replace(const char* aProperty, const char* aValue)
   1.157 +	{
   1.158 +	TInt ix = Find(aProperty);
   1.159 +	if (ix < 0)
   1.160 +		return Insert(~ix, aProperty, aValue);
   1.161 +	// replacing a property
   1.162 +	SEntry& e = iEntries[ix];
   1.163 +	char* value = Duplicate(aValue);
   1.164 +	if (value != NULL)
   1.165 +		{
   1.166 +		TheAllocator.Free(e.iValue);
   1.167 +		e.iValue = value;
   1.168 +		}
   1.169 +	return value;
   1.170 +	}
   1.171 +
   1.172 +const char* Properties::Append(const char* aProperty, const char* aValue)
   1.173 +	{
   1.174 +	TInt ix = Find(aProperty);
   1.175 +	if (ix < 0)
   1.176 +		return Insert(~ix, aProperty, aValue);
   1.177 +
   1.178 +	// append a property
   1.179 +	SEntry& e = iEntries[ix];
   1.180 +	TInt size = strlen(e.iValue) + strlen(aValue) + 2;
   1.181 +	char* value = (char*)TheAllocator.Realloc(e.iValue, size);
   1.182 +	if (value != NULL)
   1.183 +		{
   1.184 +		strcat(value, ";");
   1.185 +		strcat(value, aValue);
   1.186 +		e.iValue = value;
   1.187 +		}
   1.188 +	return value;
   1.189 +	}
   1.190 +
   1.191 +TInt Properties::GetString(const char* aProperty, const char*& aValue) const
   1.192 +	{
   1.193 +	TInt ix = Find(aProperty);
   1.194 +	if (ix < 0)
   1.195 +		return KErrNotFound;
   1.196 +	aValue = iEntries[ix].iValue;
   1.197 +	return KErrNone;
   1.198 +	}
   1.199 +
   1.200 +TInt Properties::GetInt(const char* aProperty, TInt& aValue) const
   1.201 +	{
   1.202 +	TInt ix = Find(aProperty);
   1.203 +	if (ix < 0)
   1.204 +		return KErrNotFound;
   1.205 +	char* value = iEntries[ix].iValue;
   1.206 +	char* end;
   1.207 +	TBool neg = *value=='-';
   1.208 +	value += neg;
   1.209 +	long val = strtoul(value, &end, 0);
   1.210 +	if(neg)
   1.211 +		{
   1.212 +		if(val<0)
   1.213 +			return KErrArgument;
   1.214 +		val = -val;
   1.215 +		}
   1.216 +	if (*end != '\0')
   1.217 +		return KErrArgument;
   1.218 +	aValue = val;
   1.219 +	return KErrNone;
   1.220 +	}
   1.221 +
   1.222 +TInt Properties::GetBool(const char* aProperty, TBool& aValue, TBool aDefaultValue) const
   1.223 +	{
   1.224 +	TInt ix = Find(aProperty);
   1.225 +	if (ix < 0)
   1.226 +		{
   1.227 +		aValue = aDefaultValue;
   1.228 +		return KErrNone;
   1.229 +		}
   1.230 +	const char* value=iEntries[ix].iValue;
   1.231 +	if (_stricmp(value, "on")==0 || _stricmp(value, "yes")==0 || _stricmp(value, "1")==0 || strlen(value)==0)
   1.232 +		{
   1.233 +		aValue = ETrue;
   1.234 +		return KErrNone;
   1.235 +		}
   1.236 +	if (_stricmp(value, "off")==0 || _stricmp(value, "no")==0 || _stricmp(value, "0")==0 )
   1.237 +		{
   1.238 +		aValue = EFalse;
   1.239 +		return KErrNone;
   1.240 +		}
   1.241 +
   1.242 +	// Bool property has an illegal value!
   1.243 +	return KErrArgument;
   1.244 +	}
   1.245 +
   1.246 +TInt Properties::Find(const char* aProperty) const
   1.247 +//
   1.248 +// Lookup a property in the table
   1.249 +// return index (>=0) if found, ~insertion-point (<0) if not
   1.250 +//
   1.251 +	{
   1.252 +	TInt l = 0, r = iCount;
   1.253 +	while (l < r)
   1.254 +		{
   1.255 +		TInt m = (l + r) >> 1;
   1.256 +		const SEntry& e = iEntries[m];
   1.257 +		TInt k = _stricmp(aProperty,e.iProperty);
   1.258 +		if (k < 0)
   1.259 +			r = m;
   1.260 +		else if (k > 0)
   1.261 +			l = m + 1;
   1.262 +		else
   1.263 +			return m;
   1.264 +		}
   1.265 +	return ~l;
   1.266 +	}
   1.267 +
   1.268 +#ifdef _DUMP_PROPERTY
   1.269 +void Properties::Dump() const
   1.270 +	{
   1.271 +	for (TInt i = 0; i < iCount; ++i)
   1.272 +		{
   1.273 +		const SEntry& e = iEntries[i];
   1.274 +		char buf[512];
   1.275 +		strcpy(buf, e.iProperty);
   1.276 +		TInt len = strlen(e.iValue);
   1.277 +		if (len)
   1.278 +			{
   1.279 +			strcat(buf, " = ");
   1.280 +			if (len <= 256)
   1.281 +				strcat(buf, e.iValue);
   1.282 +			else
   1.283 +				{
   1.284 +				strncat(buf, e.iValue, 256);
   1.285 +				strcat(buf, "...");
   1.286 +				}
   1.287 +			}
   1.288 +		strcat(buf, "\r\n");
   1.289 +		OutputDebugStringA(buf);
   1.290 +		}
   1.291 +	}
   1.292 +#endif
   1.293 +
   1.294 +// Property related variant functions
   1.295 +
   1.296 +TInt Wins::EmulatorHal(TInt aFunction, TAny* a1, TAny* a2)
   1.297 +	{
   1.298 +	TInt r=KErrNone;
   1.299 +	switch(aFunction)
   1.300 +		{
   1.301 +		case EEmulatorHalStringProperty:
   1.302 +			return iProperties.GetString((const char*)a1,*(const char**)a2);
   1.303 +		case EEmulatorHalIntProperty:
   1.304 +			return iProperties.GetInt((const char*)a1,*(TInt*)a2);
   1.305 +		case EEmulatorHalBoolProperty:
   1.306 +			return iProperties.GetBool((const char*)a1,*(TBool*)a2);
   1.307 +		case EEmulatorHalMapFilename:
   1.308 +			return MapFilename(*(const TDesC*)a1,*(TDes*)a2);
   1.309 +		case EEmulatorHalSetFlip:
   1.310 +			{
   1.311 +			if (iUi)
   1.312 +				{
   1.313 +				TInt screen = (TInt)a2;
   1.314 +				if((TUint)screen < (TUint)iUi->NumberOfScreens())
   1.315 +					return iUi->SetFlip(TEmulatorFlip(TInt(a1)),screen);
   1.316 +				}
   1.317 +			break;
   1.318 +			}
   1.319 +		case EEmulatorHalColorDepth:
   1.320 +			{
   1.321 +			TUint colorDepth = KDefaultColorDepth;
   1.322 +			if(iUi)
   1.323 +				{
   1.324 +				if((TUint)a2 < (TUint)iUi->NumberOfScreens())
   1.325 +					colorDepth = iUi->ColorDepth((TInt)a2);
   1.326 +				}
   1.327 +			*(TUint*)a1 = colorDepth;
   1.328 +			return KErrNone;
   1.329 +			}
   1.330 +		case EEmulatorHalCPUSpeed:
   1.331 +			if (a1)
   1.332 +				return SetCpuSpeed(TUint(a2)/1000);
   1.333 +			*(TInt*)a2 = iCpuSpeed ? iCpuSpeed * 1000 : 1;
   1.334 +			return KErrNone;
   1.335 +		case EEmulatorHalNumberOfScreens:
   1.336 +			*(TInt*)a2 = iUi ? iUi->NumberOfScreens() : 1;
   1.337 +			return KErrNone;
   1.338 +		case EEmulatorHalSetDisplayChannel:
   1.339 +			if (iUi && (TUint)a1 < (TUint)iUi->NumberOfScreens())
   1.340 +				{
   1.341 +				r = iUi->SetDisplayChannel((TInt)a1, static_cast<DDisplayChannel*>(a2));
   1.342 +				}
   1.343 +			else
   1.344 +				{
   1.345 +				r = KErrNotSupported;
   1.346 +				}
   1.347 +			break;
   1.348 +		default:
   1.349 +			r=KErrNotSupported;
   1.350 +			break;
   1.351 +		}
   1.352 +	return r;
   1.353 +	}
   1.354 +
   1.355 +const char* KExtensionListNormal = "btracex.ldd;hcr.dll;winsgui;elocd.ldd;medint.pdd;medlfs.pdd;medmmc.pdd;epbusmmc.dll;epbusv.dll";
   1.356 +const char* KExtensionUsiiNand = "?medusiiw.pdd";
   1.357 +const char* KExtensionUsiiNandLoader = "?medusiiws.pdd";
   1.358 +const char* KExtensionUsiiNandTest = "?medusiiwt.pdd";
   1.359 +
   1.360 +
   1.361 +
   1.362 +
   1.363 +const int KMaxEpocRootSize = 120;
   1.364 +
   1.365 +TInt Wins::InitProperties(TBool aRunExe)
   1.366 +	{
   1.367 +	if (iProperties.Replace("MachineName", KDefaultMachineName) == NULL)
   1.368 +		return KErrNoMemory;
   1.369 +
   1.370 +    char epocRoot[KMaxEpocRootSize];
   1.371 +
   1.372 +    TInt total = GetEnvironmentVariableA( KDefaultEpocRootName, epocRoot, KMaxEpocRootSize);
   1.373 +
   1.374 +    if (total != 0)
   1.375 +		{
   1.376 +	    if (iProperties.Replace("EpocRoot", epocRoot) == NULL)
   1.377 +    	    return KErrNoMemory;
   1.378 +		}
   1.379 +
   1.380 +	if (iProperties.Append("Extension", KExtensionListNormal) == NULL)
   1.381 +		return KErrNoMemory;
   1.382 +
   1.383 +	char overrideCDrive[MAX_PATH];
   1.384 +	overrideCDrive[0] = '\0';
   1.385 +	TInt r = ProcessCommandLine(aRunExe, overrideCDrive);
   1.386 +	if (r != KErrNone)
   1.387 +		return r;
   1.388 +
   1.389 +	r = SetupPaths();
   1.390 +	if (r != KErrNone)
   1.391 +		return r;
   1.392 +
   1.393 +	r = LoadProperties();
   1.394 +	if (r != KErrNone)
   1.395 +		return r;
   1.396 +
   1.397 +	//	get Unistore II Media Driver type from epoc.ini
   1.398 +	const char* value = NULL;
   1.399 +	
   1.400 +
   1.401 +	iProperties.GetString("NandDriverType", value);
   1.402 +	if (value)
   1.403 +		{
   1.404 +		if (value && _stricmp("XSR",value)==0)
   1.405 +			{
   1.406 +			// epoc.ini "NandDriverType=XSR" for XSR/Unistore-II driver
   1.407 +			if (iProperties.Append("Extension", KExtensionUsiiNand) == NULL)
   1.408 +				return KErrNoMemory;		
   1.409 +			}
   1.410 +		else if (value && _stricmp("XSRNandloader",value)==0)
   1.411 +			{
   1.412 +			// epoc.ini "NandDriverType=XSRNandloader" for XSR/Unistore-II nandloader driver
   1.413 +			if (iProperties.Append("Extension", KExtensionUsiiNandLoader) == NULL)
   1.414 +				return KErrNoMemory;		
   1.415 +			}
   1.416 +		else if (value && _stricmp("XSRTest",value)==0)
   1.417 +			{
   1.418 +			// epoc.ini "NandDriverType=XSRTest" for XSR/Unistore-II test driver
   1.419 +			if (iProperties.Append("Extension", KExtensionUsiiNandTest) == NULL)
   1.420 +				return KErrNoMemory;
   1.421 +			}
   1.422 +		else	
   1.423 +			{
   1.424 +			// If epoc.ini contains "NandDriverType=???" but ??? not equal to any
   1.425 +			// of above XSR driver types then load production/release XSR
   1.426 +			// driver
   1.427 +			if (iProperties.Append("Extension", KExtensionUsiiNand) == NULL)
   1.428 +				return KErrNoMemory;
   1.429 +
   1.430 +			}
   1.431 +		}
   1.432 +	else
   1.433 +		{
   1.434 +		// Load the production/release XSR driver, if value is NULL
   1.435 +		if (iProperties.Append("Extension", KExtensionUsiiNand) == NULL)
   1.436 +			return KErrNoMemory;
   1.437 +
   1.438 +		}
   1.439 +	
   1.440 +
   1.441 +//	load additional configuration specific properties
   1.442 +
   1.443 +//	get the multi property "configuration"
   1.444 +	value = NULL;
   1.445 +	iProperties.GetString("configuration", value);
   1.446 +	
   1.447 +//	load each one of these
   1.448 +//	check for any screen specific properties in the main epoc.ini
   1.449 +//	if configuration property is set
   1.450 +	if (value && !iConfigPropertySet)	//configuration
   1.451 +		{
   1.452 +		iConfigId = 0;
   1.453 +		char configFileName[100];
   1.454 +		do
   1.455 +			{
   1.456 +			//load each set of properties
   1.457 +
   1.458 +		   const char * pdest = strchr(value, ';');
   1.459 +		   TInt result = pdest - value;
   1.460 +		   if(pdest)
   1.461 +			   {
   1.462 +			   strncpy(configFileName, value, result);
   1.463 +			   configFileName[result] = '\0';
   1.464 +			   value = value + result + 1;
   1.465 +			   }
   1.466 +		   else
   1.467 +			   {
   1.468 +			   strcpy(configFileName, value);
   1.469 +			   value += strlen(value);
   1.470 +			   }
   1.471 +
   1.472 +			r = LoadConfigSpecificProperties(configFileName);
   1.473 +			if (r == KErrNone)
   1.474 +				iConfigId++;
   1.475 +			}
   1.476 +		while(*value);
   1.477 +		}
   1.478 +
   1.479 +	char scr[30];
   1.480 +	//if iConfigId is zero, there is only 1 configuration
   1.481 +	wsprintfA(scr, "ConfigCount %d", iConfigId ? iConfigId : 1);
   1.482 +	r = AddProperty(scr, scr+strlen(scr));
   1.483 +	if (r != KErrNone)
   1.484 +		return r;
   1.485 +
   1.486 +	r = SetupMediaPath();
   1.487 +	if (r != KErrNone)
   1.488 +		return r;
   1.489 +
   1.490 +	if (overrideCDrive[0] != '\0')
   1.491 +		SetupDrive('c', overrideCDrive);
   1.492 +
   1.493 +	if (iProperties.Append("Extension", "exstart") == NULL)
   1.494 +		return KErrNoMemory;
   1.495 +
   1.496 +#ifdef _DUMP_PROPERTY
   1.497 +	iProperties.Dump();
   1.498 +#endif
   1.499 +
   1.500 +	return KErrNone;
   1.501 +	}
   1.502 +
   1.503 +char* skipws(char* aPtr)
   1.504 +	{
   1.505 +	while (isspace(*aPtr))
   1.506 +		++aPtr;
   1.507 +	return aPtr;
   1.508 +	}
   1.509 +
   1.510 +char* skiptok(char* aPtr)
   1.511 +	{
   1.512 +	if (*aPtr == '\"')
   1.513 +		{
   1.514 +		++aPtr;
   1.515 +		while (*aPtr && *aPtr++ != '\"')
   1.516 +			{}
   1.517 +		}
   1.518 +	else
   1.519 +		{
   1.520 +		while (*aPtr && !isspace(*aPtr))
   1.521 +			++aPtr;
   1.522 +		}
   1.523 +	return aPtr;
   1.524 +	}
   1.525 +
   1.526 +#ifdef _DEBUG
   1.527 +struct TDebugTrace
   1.528 +	{
   1.529 +	const char* iName;
   1.530 +	TInt iMask;
   1.531 +	};
   1.532 +
   1.533 +// Only the first 32 trace bits can be defined using these values
   1.534 +// "ALWAYS" in this context means all of the first 32 bits not all 256 bits
   1.535 +const TDebugTrace KTraceValues[] =
   1.536 +	{
   1.537 +	{"ALWAYS", KALWAYS},
   1.538 +	{"BOOT", 1<<KBOOT},
   1.539 +	{"DEVICE", 1<<KDEVICE},
   1.540 +	{"DFC", 1<<KDFC},
   1.541 +	{"DLL", 1<<KDLL},
   1.542 +	{"EVENT", 1<<KEVENT},      
   1.543 +	{"EXEC", 1<<KEXEC},
   1.544 +	{"DEBUGGER", 1<<KDEBUGGER},
   1.545 +	{"EXTENSION", 1<<KEXTENSION},
   1.546 +	{"FAIL", 1<<KFAIL},
   1.547 +	{"HARDWARE", 1<<KHARDWARE},
   1.548 +	{"IPC", 1<<KIPC},
   1.549 +	{"LOCDRV", 1<<KLOCDRV},
   1.550 +	{"MEMTRACE", 1<<KMEMTRACE},
   1.551 +	{"MMU", 1<<KMMU},
   1.552 +	{"NKERN", 1<<KNKERN},
   1.553 +	{"OBJECT", 1<<KOBJECT},
   1.554 +	{"PANIC", 1<<KPANIC},
   1.555 +	{"PBUS1", 1<<KPBUS1},
   1.556 +	{"PBUS2", 1<<KPBUS2},
   1.557 +	{"PBUSDRV", 1<<KPBUSDRV},
   1.558 +	{"POWER", 1<<KPOWER},      
   1.559 +	{"PROC", 1<<KPROC},
   1.560 +	{"SCHED", 1<<KSCHED},
   1.561 +	{"SCHED2", 1<<KSCHED2},
   1.562 +	{"SCRATCH", 1<<KSCRATCH},
   1.563 +	{"SEMAPHORE", 1<<KSEMAPHORE},
   1.564 +	{"SERVER", 1<<KSERVER},
   1.565 +	{"THREAD", 1<<KTHREAD},
   1.566 +	{"THREAD2", 1<<KTHREAD2},
   1.567 +	{"TIMING", 1<<KTIMING},
   1.568 +	{"DMA", 1<<KDMA},
   1.569 +	{"MMU2", 1<<KMMU2}
   1.570 +	};
   1.571 +const TInt KMaxTraceName = 9;
   1.572 +const TInt KCountTraceValues = sizeof(KTraceValues)/sizeof(TDebugTrace);
   1.573 +
   1.574 +static const TDebugTrace* TraceType(const char* aTrace, TInt aLen)
   1.575 +	{
   1.576 +	if (aLen > KMaxTraceName)
   1.577 +		return 0;
   1.578 +
   1.579 +	char name[KMaxTraceName + 1];
   1.580 +	strncpy(name, aTrace, aLen);
   1.581 +	name[aLen] = '\0';
   1.582 +
   1.583 +	for (TInt i=KCountTraceValues; --i>=0;)
   1.584 +		{
   1.585 +		if (_stricmp(name, KTraceValues[i].iName)==0)
   1.586 +			return &KTraceValues[i];
   1.587 +		}
   1.588 +	return 0;
   1.589 +	}
   1.590 +#endif
   1.591 +
   1.592 +TInt Wins::DebugMask()
   1.593 +	{
   1.594 +	TInt mask = KDefaultDebugMask;
   1.595 +	if (iProperties.GetInt("DebugMask", mask) != KErrArgument)
   1.596 +		return mask;
   1.597 +#ifdef _DEBUG
   1.598 +	// allow text ones
   1.599 +	const char* e;
   1.600 +	if (iProperties.GetString("DebugMask", e) != KErrNone)
   1.601 +		return mask;
   1.602 +
   1.603 +	for (;;)
   1.604 +		{
   1.605 +		char* p = skipws((char*)e);
   1.606 +		if (*p == 0)
   1.607 +			break;
   1.608 +		e = skiptok(p);
   1.609 +		TBool add = ETrue;
   1.610 +		if (*p == '+')
   1.611 +			++p;
   1.612 +		else if (*p == '-')
   1.613 +			{
   1.614 +			add = EFalse;
   1.615 +			++p;
   1.616 +			}
   1.617 +		const TDebugTrace* type = TraceType(p, e - p);
   1.618 +		if (type)
   1.619 +			{
   1.620 +			if (add)
   1.621 +				mask |= type->iMask;
   1.622 +			else
   1.623 +				mask &= ~type->iMask;
   1.624 +			}
   1.625 +		}
   1.626 +#endif
   1.627 +	return mask;
   1.628 +	}
   1.629 +
   1.630 +TUint32 Wins::KernelConfigFlags()
   1.631 +	{
   1.632 +	TUint32 flags = 0;
   1.633 +	TBool b;
   1.634 +
   1.635 +	b=0;
   1.636 +	iProperties.GetBool("PlatSecEnforcement",b,EFalse);
   1.637 +	if(b) flags |= EKernelConfigPlatSecEnforcement;
   1.638 +	Wins::EarlyLogging("PlatSecEnforcement ",b?"ON":"OFF");
   1.639 +
   1.640 +	b=0;
   1.641 +	iProperties.GetBool("PlatSecDiagnostics",b,EFalse);
   1.642 +	if(b) flags |= EKernelConfigPlatSecDiagnostics;
   1.643 +	Wins::EarlyLogging("PlatSecDiagnostics ",b?"ON":"OFF");
   1.644 +
   1.645 +	b=0;
   1.646 +	iProperties.GetBool("PlatSecProcessIsolation",b,EFalse);
   1.647 +	if(b) flags |= EKernelConfigPlatSecProcessIsolation;
   1.648 +	Wins::EarlyLogging("PlatSecProcessIsolation ",b?"ON":"OFF");
   1.649 +
   1.650 +	b=0;
   1.651 +	iProperties.GetBool("PlatSecEnforceSysBin",b,EFalse);
   1.652 +	if(b) flags |= EKernelConfigPlatSecEnforceSysBin;
   1.653 +	Wins::EarlyLogging("PlatSecEnforceSysBin ",b?"ON":"OFF");
   1.654 +
   1.655 +	b=0;
   1.656 +	iProperties.GetBool("CrazyScheduling",b,EFalse);
   1.657 +	if(b) flags |= EKernelConfigCrazyScheduling;
   1.658 +	Wins::EarlyLogging("CrazyScheduling ",b?"ON":"OFF");
   1.659 +
   1.660 +	return flags;
   1.661 +	}
   1.662 +
   1.663 +void Wins::DisabledCapabilities(SCapabilitySet& aCapabilities)
   1.664 +	{
   1.665 +	const char* text;
   1.666 +	if(iProperties.GetString("PlatSecDisabledCaps", text)!=KErrNone)
   1.667 +		text = "NONE";
   1.668 +	Wins::EarlyLogging("PlatSecDisabledCaps ",text);
   1.669 +	ParseCapabilitiesArg(aCapabilities,text);
   1.670 +	}
   1.671 +
   1.672 +#define PARSE_CAPABILITIES_ERROR(aMessage) Wins::EarlyLogging(aMessage,0)
   1.673 +#define PARSE_CAPABILITIES_ERROR2(aMessage,aArg) Wins::EarlyLogging(aMessage,aArg)
   1.674 +#define strnicmp _strnicmp
   1.675 +
   1.676 +TInt Wins::ParseCapabilitiesArg(SCapabilitySet& aCapabilities, const char *aText)
   1.677 +//
   1.678 +// This is a cun'n'paste copy of the function in TOOLS\E32TOOLS\HOST\H_UTIL.CPP
   1.679 +// Keep both of these versions up to date with each other
   1.680 +//
   1.681 +	{
   1.682 +	memset(&aCapabilities,0,sizeof(aCapabilities));
   1.683 +	char c;
   1.684 +	while((c=*aText)!=0)
   1.685 +		{
   1.686 +		if(c<=' ')
   1.687 +			{
   1.688 +			++aText;
   1.689 +			continue;
   1.690 +			}
   1.691 +		int invert=0;
   1.692 +		if(c=='+')
   1.693 +			{
   1.694 +			++aText;
   1.695 +			c=*aText;
   1.696 +			}
   1.697 +		if(c=='-')
   1.698 +			{
   1.699 +			invert=1;
   1.700 +			++aText;
   1.701 +			}
   1.702 +		const char* name = aText;
   1.703 +		while((c=*aText)>' ')
   1.704 +			{
   1.705 +			if(c=='-' || c=='+')
   1.706 +				break;
   1.707 +			++aText;
   1.708 +			}
   1.709 +		TInt n = aText-name;
   1.710 +		TInt i;
   1.711 +
   1.712 +		if(n==3 && strnicmp("all",name,n)==0)
   1.713 +			{
   1.714 +			if(invert)
   1.715 +				{
   1.716 +				PARSE_CAPABILITIES_ERROR("Capability '-ALL' not allowed");
   1.717 +				return KErrArgument;
   1.718 +				}
   1.719 +			for(i=0; i<ECapability_Limit; i++)
   1.720 +				{
   1.721 +				if(CapabilityNames[i])
   1.722 +					aCapabilities[i>>5] |= (1<<(i&31));
   1.723 +				}
   1.724 +			continue;
   1.725 +			}
   1.726 +
   1.727 +		if(n==4 && strnicmp("none",name,n)==0)
   1.728 +			{
   1.729 +			if(invert)
   1.730 +				{
   1.731 +				PARSE_CAPABILITIES_ERROR("Capability '-NONE' not allowed");
   1.732 +				return KErrArgument;
   1.733 +				}
   1.734 +			memset(&aCapabilities,0,sizeof(aCapabilities));
   1.735 +			continue;
   1.736 +			}
   1.737 +
   1.738 +		for(i=0; i<ECapability_Limit; i++)
   1.739 +			{
   1.740 +			const char* cap = CapabilityNames[i];
   1.741 +			if(!cap)
   1.742 +				continue;
   1.743 +			if((int)strlen(cap)!=n)
   1.744 +				continue;
   1.745 +			if(strnicmp(cap,name,n)!=0)
   1.746 +				continue;
   1.747 +			break;
   1.748 +			}
   1.749 +		if(i>=ECapability_Limit)
   1.750 +			{
   1.751 +			char badName[32];
   1.752 +			if(n>=sizeof(badName)) n=sizeof(badName)-1;
   1.753 +			memcpy(badName,name,n);
   1.754 +			badName[n]=0;
   1.755 +			PARSE_CAPABILITIES_ERROR2("Unrecognised capability name: ",badName);
   1.756 +			return KErrArgument;
   1.757 +			}
   1.758 +		if(invert)
   1.759 +			aCapabilities[i>>5] &= ~(1<<(i&31));
   1.760 +		else
   1.761 +			aCapabilities[i>>5] |= (1<<(i&31));
   1.762 +		}
   1.763 +	return KErrNone;
   1.764 +	}
   1.765 +
   1.766 +TInt Wins::AddProperty(char* aProperty, const char* aEol)
   1.767 +	{
   1.768 +	const char* tok = aProperty;
   1.769 +	int c;
   1.770 +	do
   1.771 +		{
   1.772 +		if (aProperty == aEol)
   1.773 +			{
   1.774 +			// boolean property
   1.775 +			if (_stricmp(tok, "_NewScreen_") == 0)
   1.776 + 				{
   1.777 + 				++ScreenId;
   1.778 + 				return KErrNone;
   1.779 + 				}
   1.780 +			else
   1.781 +				{
   1.782 +				char newtok[KMaxTokenLength];
   1.783 +				if (ConfigSpecificProperty(tok))
   1.784 +					{
   1.785 +					wsprintfA(newtok, "Configuration[%d]", iConfigId);
   1.786 +					strcat(newtok, tok);
   1.787 +					tok = newtok;
   1.788 +					}
   1.789 +				}
   1.790 +			return iProperties.Replace(tok, NULL) == NULL ? KErrNoMemory : KErrNone;
   1.791 +			}
   1.792 +		c=*aProperty++;
   1.793 +		} while (isalnum(c) || c=='_');
   1.794 +	aProperty[-1]='\0';	// terminate property name
   1.795 +	while (isspace(c))
   1.796 +		c=*aProperty++;
   1.797 +	TBool append=ETrue;
   1.798 +	if (c=='=')
   1.799 +		{
   1.800 +		append=EFalse;
   1.801 +		c=*aProperty++;
   1.802 +		}
   1.803 +	else if (c=='+' && *aProperty=='=')
   1.804 +		{
   1.805 +		++aProperty;
   1.806 +		c=*aProperty++;
   1.807 +		}
   1.808 +	while (isspace(c))
   1.809 +		c=*aProperty++;
   1.810 +	--aProperty;	// point back to value
   1.811 +
   1.812 +	if (_strnicmp(tok, "_epoc_drive_", 12) == 0)
   1.813 +		return SetupDrive(tok[12], aProperty);
   1.814 +	else
   1.815 +		{
   1.816 +		char newtok[KMaxTokenLength];
   1.817 +		if (ConfigSpecificProperty(tok))
   1.818 +			{
   1.819 +			if (ScreenSpecificProperty(tok))
   1.820 +				{
   1.821 +				wsprintfA(newtok, "Configuration[%d][%d]", iConfigId, ScreenId);
   1.822 +				}
   1.823 +			else
   1.824 +				{
   1.825 +				wsprintfA(newtok, "Configuration[%d]", iConfigId);
   1.826 +				}
   1.827 +			strcat(newtok, tok);
   1.828 +			tok = newtok;
   1.829 +			}
   1.830 +		if (append)
   1.831 +			return iProperties.Append(tok, aProperty) == NULL ? KErrNoMemory : KErrNone;
   1.832 +		else
   1.833 +			return iProperties.Replace(tok, aProperty) == NULL ? KErrNoMemory : KErrNone;
   1.834 +		}
   1.835 +	}
   1.836 +
   1.837 +TInt Wins::ProcessCommandLine(TBool aRunExe, char* aCDrive)
   1.838 +	{
   1.839 +	if (aRunExe)
   1.840 +		{
   1.841 +		char exe[MAX_PATH];
   1.842 +		DWORD len=GetModuleFileNameA(NULL, exe, MAX_PATH);
   1.843 +		if (len == 0)
   1.844 +			return KErrGeneral;
   1.845 +		exe[len] = '\0';
   1.846 +		const char* base = strrchr(exe, '\\') + 1;
   1.847 +		if (iProperties.Replace("AutoRun", base) == NULL)
   1.848 +			return KErrNoMemory;
   1.849 +		}
   1.850 +
   1.851 +	char* cmd = skipws(skiptok(GetCommandLineA()));
   1.852 +	if (strstr(cmd, "--") != NULL)
   1.853 +		{
   1.854 +		for (;;)
   1.855 +			{
   1.856 +			cmd = strchr(cmd, '-') + 1;
   1.857 +			TInt opt = *cmd++;
   1.858 +			if (opt == '-')
   1.859 +				break;
   1.860 +			char* end = skiptok(cmd);
   1.861 +			*end = '\0';
   1.862 +			switch (tolower(opt))
   1.863 +				{
   1.864 +			    case 'd':
   1.865 +				    {
   1.866 +				    TInt r = AddProperty(cmd, end);
   1.867 +				    if (r != KErrNone)
   1.868 +				    	return r;
   1.869 +				    }
   1.870 +				    break;
   1.871 +			    case 'm':
   1.872 +			    	// specify base name for .INI file
   1.873 +				    if (iProperties.Replace("MachineName", cmd) == NULL)
   1.874 +				    	return KErrNoMemory;
   1.875 +				    break;
   1.876 +			    case 'l':
   1.877 +			    	// specify language
   1.878 +			    	if (iProperties.Replace("TheMachineLanguageIndex", cmd) == NULL)
   1.879 +			    		return KErrNoMemory;
   1.880 +			    	break;
   1.881 +		    	case 'c':
   1.882 +		    		// specify path for emulated C drive
   1.883 +			    	{
   1.884 +			    	DWORD len=GetFullPathNameA(cmd, MAX_PATH, aCDrive, NULL);
   1.885 +			    	if (len==0 || len >= MAX_PATH)
   1.886 +			    		aCDrive[0] = '\0';
   1.887 +			    	}
   1.888 +			        break;
   1.889 +			    case 't':
   1.890 +			    	// specify the temp path as the emulated C drive
   1.891 +			    	{
   1.892 +			    	DWORD len=GetTempPathA(MAX_PATH, aCDrive);
   1.893 +			    	if (len==0 || len >= MAX_PATH)
   1.894 +			    		aCDrive[0] = '\0';
   1.895 +			    	}
   1.896 +			    	break;
   1.897 +				}
   1.898 +			cmd = end+1;
   1.899 +			}
   1.900 +		cmd = skipws(cmd);
   1.901 +		}
   1.902 +
   1.903 +	if (aRunExe && iProperties.Replace("CommandLine", cmd) == NULL)
   1.904 +		return KErrNoMemory;
   1.905 +
   1.906 +	return KErrNone;
   1.907 +	}
   1.908 +
   1.909 +TInt Wins::LoadConfigSpecificProperties(const char * aFile)
   1.910 +	{
   1.911 +	const char* path;
   1.912 +	TInt r = iProperties.GetString("EmulatorDataPath", path);
   1.913 +	if (r != KErrNone)
   1.914 +		return r;
   1.915 +
   1.916 +	char file[KMaxFileName + 1];
   1.917 +	strcpy(file, path);
   1.918 +	strcat(file, aFile);
   1.919 +
   1.920 +	char* iniData;
   1.921 +	r = ReadIniFile(file, iniData);
   1.922 +	if (r == KErrNone)
   1.923 +		{
   1.924 +		r = ReadProperties(iniData);
   1.925 +		VirtualFree(iniData, 0, MEM_RELEASE);
   1.926 +		}
   1.927 +	else if (r == KErrNotFound)
   1.928 +		r = KErrNotFound;
   1.929 +
   1.930 +	return r;
   1.931 +
   1.932 +	}
   1.933 +
   1.934 +
   1.935 +
   1.936 +TInt Wins::LoadProperties()
   1.937 +	{
   1.938 +	const char* path;
   1.939 +	TInt r = iProperties.GetString("EmulatorDataPath", path);
   1.940 +	if (r != KErrNone)
   1.941 +		return r;
   1.942 +	const char* name;
   1.943 +	r = iProperties.GetString("MachineName", name);
   1.944 +	if (r != KErrNone)
   1.945 +		return r;
   1.946 +retry:
   1.947 +	char file[KMaxFileName + 1];
   1.948 +	strcpy(file, path);
   1.949 +	strcat(file, name);
   1.950 +	strcat(file, ".ini");
   1.951 +
   1.952 +	char* iniData;
   1.953 +	r = ReadIniFile(file, iniData);
   1.954 +	if (r == KErrNone)
   1.955 +		{
   1.956 +		r = ReadProperties(iniData);
   1.957 +		VirtualFree(iniData, 0, MEM_RELEASE);
   1.958 +		}
   1.959 +	else if (r == KErrNotFound)
   1.960 +		{
   1.961 +		if(_stricmp(name,KDefaultMachineName)==0)
   1.962 +			{
   1.963 +			// try test ini file
   1.964 +			name = KDefaultTestMachineName;
   1.965 +			goto retry;
   1.966 +			}
   1.967 +		r = KErrNone;		// no ini file - oh well
   1.968 +		}
   1.969 +
   1.970 +	return r;
   1.971 +	}
   1.972 +
   1.973 +TInt Wins::ReadProperties(char* aData)
   1.974 +	{
   1.975 +	ScreenId = 0;
   1.976 +	while (*aData)
   1.977 +		{
   1.978 +		char* beg = aData;
   1.979 +		char* eol = strchr(beg, '\n');
   1.980 +		aData = eol+1;
   1.981 +		if (eol == beg)
   1.982 +			continue;
   1.983 +		if (eol[-1] == '\r' && --eol == beg)
   1.984 +			continue;
   1.985 +		*eol = '\0';		// terminate line
   1.986 +
   1.987 +		while (isspace(*beg))
   1.988 +			++beg;
   1.989 +		char* comment = strchr(beg, '#');
   1.990 +		if (comment)
   1.991 +			eol = comment;
   1.992 +		while (eol > beg && isspace(eol[-1]))
   1.993 +			--eol;
   1.994 +		if (beg == eol)
   1.995 +			continue;
   1.996 +		*eol = '\0';		// terminate line
   1.997 +
   1.998 +		TInt r = AddProperty(beg, eol);
   1.999 +		if (r != KErrNone)
  1.1000 +			return r;
  1.1001 +		}
  1.1002 +	char sc[5];
  1.1003 + 	wsprintfA(sc, "%d", ScreenId+1);
  1.1004 +	TInt screens;
  1.1005 +	if(iProperties.GetInt("[screens]", screens) == KErrNone && screens > ScreenId)
  1.1006 +		return KErrNone;
  1.1007 + 	else
  1.1008 +		return iProperties.Replace("[screens]", sc)  == NULL ? KErrNoMemory : KErrNone;
  1.1009 +	}
  1.1010 +
  1.1011 +TInt Wins::ReadIniFile(const char* aFileName, char*& aContents)
  1.1012 +	{
  1.1013 +	TInt r = KErrNone;
  1.1014 +	HANDLE file=CreateFileA(aFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  1.1015 +	if (!file || file==INVALID_HANDLE_VALUE)
  1.1016 +		r = KErrNotFound;	// More than likely !
  1.1017 +	else
  1.1018 +		{
  1.1019 +		TInt size=GetFileSize(file, NULL);
  1.1020 +		if (size==INVALID_FILE_SIZE)
  1.1021 +			r = KErrGeneral;
  1.1022 +		else
  1.1023 +			{
  1.1024 +			// fileSize+3 to ensure zero-termination of file and trailing CRLF
  1.1025 +			// VirtualAlloc initializes memory to zero
  1.1026 +			TAny* data = VirtualAlloc(NULL, size+3, MEM_COMMIT, PAGE_READWRITE);
  1.1027 +			if (!data)
  1.1028 +				r = KErrNoMemory;
  1.1029 +			else
  1.1030 +				{
  1.1031 +				DWORD bytesRead;
  1.1032 +				if (!ReadFile(file, data, size, &bytesRead, NULL))
  1.1033 +					{
  1.1034 +					VirtualFree(data, 0, MEM_RELEASE);
  1.1035 +					r = KErrGeneral;
  1.1036 +					}
  1.1037 +				else
  1.1038 +					{
  1.1039 +					aContents = (LPSTR)data;
  1.1040 +					strcpy(aContents + size,"\r\n");
  1.1041 +					}
  1.1042 +				}
  1.1043 +			}
  1.1044 +		CloseHandle(file);
  1.1045 +		}
  1.1046 +	return r;
  1.1047 +	}
  1.1048 +
  1.1049 +
  1.1050 +TInt Wins::SetupPaths()
  1.1051 +//
  1.1052 +// set up the Emulator paths
  1.1053 +//
  1.1054 +	{
  1.1055 +	// the Emulator path
  1.1056 +	CHAR path[KMaxFileName + 1];
  1.1057 +	DWORD len=GetModuleFileNameA(NULL, path, KMaxFileName);
  1.1058 +	if (len == 0)
  1.1059 +		return(KErrGeneral);
  1.1060 +	path[len] = '\0';
  1.1061 +	_strlwr(path);
  1.1062 +	*(strrchr(path, '\\') + 1) = '\0';
  1.1063 +	const char* emulatorPath = iProperties.Replace("EmulatorPath", path);
  1.1064 +	if (!emulatorPath)
  1.1065 +		return KErrNoMemory;
  1.1066 +
  1.1067 +	CHAR drive[KMaxFileName + 1];
  1.1068 +
  1.1069 +	// the Emulator data path
  1.1070 +	strcat(path, "data\\");
  1.1071 +	DWORD att = GetFileAttributesA(path);
  1.1072 +	if (att != -1 && (att&FILE_ATTRIBUTE_DIRECTORY))
  1.1073 +		{
  1.1074 +		// if Data directory exists in the emulator path, do things the new way
  1.1075 +		strcpy(drive, emulatorPath);
  1.1076 +		strcat(drive,"c\\");
  1.1077 +		}
  1.1078 +	else
  1.1079 +		{
  1.1080 +		// the old way
  1.1081 +#if defined(__VC32__)
  1.1082 +		char* p = strstr(path, "\\epoc32\\release\\wins\\");
  1.1083 +#elif defined(__CW32__)
  1.1084 +		char* p = strstr(path, "\\epoc32\\release\\winscw\\");
  1.1085 +#endif
  1.1086 +		if (p == NULL)
  1.1087 +			return KErrNotFound;
  1.1088 +		strcpy(p, "\\epoc32\\");
  1.1089 +		strcpy(drive, path);
  1.1090 +		strcat(path, "data\\");
  1.1091 +#if defined(__VC32__)
  1.1092 +		strcat(drive,"wins\\c\\");
  1.1093 +#elif defined(__CW32__)
  1.1094 +		strcat(drive,"winscw\\c\\");
  1.1095 +#endif
  1.1096 +		}
  1.1097 +	if (!iProperties.Replace("EmulatorDataPath", path))
  1.1098 +		return KErrNoMemory;
  1.1099 +
  1.1100 +	// The Emulator Image path (for temporary EXE files)
  1.1101 +	const char* eip;
  1.1102 +	TInt r = iProperties.GetString("EmulatorImagePath", eip);
  1.1103 +	if (r!=KErrNone)
  1.1104 +		{
  1.1105 +		len=GetTempPathA(KMaxFileName, path);
  1.1106 +		strcat(path, "epoc\\");
  1.1107 +		char* p = path + strlen(path);
  1.1108 +		*p++ = emulatorPath[0];
  1.1109 +		strcpy(p, emulatorPath+2);
  1.1110 +		if (!iProperties.Replace("EmulatorImagePath", path))
  1.1111 +			return KErrNoMemory;
  1.1112 +		}
  1.1113 +	else
  1.1114 +		strcpy(path, eip);
  1.1115 +	if (!Emulator::CreateAllDirectories(path))
  1.1116 +		return Emulator::LastError();
  1.1117 +
  1.1118 +	// Win32 filesystem paths mapped to local WINS drives
  1.1119 +	r = SetupDrive('c',drive);  // set up C here, can be overridden by system.ini settings
  1.1120 +	if (r)
  1.1121 +		return(r);
  1.1122 +
  1.1123 +	strcpy(drive, emulatorPath);
  1.1124 +	strcat(drive,"z\\");
  1.1125 +
  1.1126 +	r=SetupDrive('z',drive);  // set up Z here, can be overridden by system.ini settings
  1.1127 +	if (r)
  1.1128 +		return(r);
  1.1129 +
  1.1130 +	return(KErrNone);
  1.1131 +	}
  1.1132 +
  1.1133 +TInt Wins::SetupMediaPath()
  1.1134 +//
  1.1135 +// Set up the path for emulated media devices 'EmulatedMediaPath'
  1.1136 +// The default is <datapath>media/
  1.1137 +// The system temporary path can be set by value '%temp%'
  1.1138 +//
  1.1139 +	{
  1.1140 +	CHAR path[KMaxFileName + 1];
  1.1141 +	const char* mpath;
  1.1142 +	if (iProperties.GetString("EmulatorMediaPath", mpath) == KErrNotFound)
  1.1143 +		{
  1.1144 +		const char* dpath;
  1.1145 +		TInt r = iProperties.GetString("EmulatorDataPath", dpath);
  1.1146 +		if (r != KErrNone)
  1.1147 +			return r;
  1.1148 +		strcpy(path, dpath);
  1.1149 +		strcat(path, "media\\");
  1.1150 +		return iProperties.Replace("EmulatorMediaPath", path) ? KErrNone : KErrNoMemory;
  1.1151 +		}
  1.1152 +
  1.1153 +	if (_stricmp(mpath, "%temp%") == 0)
  1.1154 +		{
  1.1155 +		DWORD len=GetTempPathA(KMaxFileName, path);
  1.1156 +		if (len > 0 && len < KMaxFileName)
  1.1157 +			return iProperties.Replace("EmulatorMediaPath", path) ? KErrNone : KErrNoMemory;
  1.1158 +		}
  1.1159 +
  1.1160 +	return KErrNone;
  1.1161 +	}
  1.1162 +
  1.1163 +const char* Wins::EmulatorMediaPath()
  1.1164 +	{
  1.1165 +	const char* mpath = NULL;
  1.1166 +	iProperties.GetString("EmulatorMediaPath", mpath);
  1.1167 +	return mpath;
  1.1168 +	}
  1.1169 +
  1.1170 +TInt Wins::SetupDrive(int aDrive, const char* aPath)
  1.1171 +//
  1.1172 +// set up emulated drives
  1.1173 +//
  1.1174 +	{
  1.1175 +
  1.1176 +	// Z drive can't end in anything but "Z\\", since we chop this off and use
  1.1177 +	// the resulting directory to find filenames with no drive specified in
  1.1178 +	// MapFileName() below.
  1.1179 +	aDrive = tolower(aDrive);
  1.1180 +	if (aDrive=='z')
  1.1181 +		{
  1.1182 +		const char* end = aPath + strlen(aPath);
  1.1183 +		if (_stricmp(end-2,"\\z") != 0 && _stricmp(end-3,"\\z\\") != 0)
  1.1184 +			return KErrArgument;
  1.1185 +		}
  1.1186 +
  1.1187 +	char prop[] = "_epoc_drive_?";
  1.1188 +	*strchr(prop, '?') = char(aDrive);
  1.1189 +
  1.1190 +
  1.1191 +    // If the path begins with the keyword %epocroot%, replace this with EPOCROOT
  1.1192 +    if (_strnicmp(aPath, "%epocroot%", 10) == 0)
  1.1193 +        {
  1.1194 +		aPath += 10; // skip "%epocroot%"
  1.1195 +
  1.1196 +        const char* eRoot;
  1.1197 +        TInt r = iProperties.GetString("EpocRoot", eRoot);
  1.1198 +        if (r != KErrNone)
  1.1199 +            return r;
  1.1200 +
  1.1201 +		int rootSize = strlen(eRoot);
  1.1202 +		int pathSize = strlen(aPath);
  1.1203 +		if(rootSize+pathSize>MAX_PATH)
  1.1204 +			return KErrArgument;
  1.1205 +
  1.1206 +        char fullPath[MAX_PATH+1];
  1.1207 +		memcpy(fullPath,eRoot,rootSize);
  1.1208 +		memcpy(fullPath+rootSize,aPath,pathSize+1); // +1 to get the terminating NULL char
  1.1209 +
  1.1210 +        return iProperties.Replace(prop, fullPath) ? KErrNone : KErrNoMemory;
  1.1211 +
  1.1212 +        }
  1.1213 +    else
  1.1214 +        // otherwise, aPath is fully qualified path name. Use that.
  1.1215 +        return iProperties.Replace(prop, aPath) ? KErrNone : KErrNoMemory;
  1.1216 +  
  1.1217 +	}
  1.1218 +
  1.1219 +TInt Wins::MapDrive(int aDrive, TDes& aBuffer) const
  1.1220 +//
  1.1221 +// Map aDrive to a path given by environment variables or defaults
  1.1222 +// Use this function only in WINS builds
  1.1223 +//
  1.1224 +	{
  1.1225 +	char drive[KMaxFileName + 1];
  1.1226 +	char prop[] = "_epoc_drive_?";
  1.1227 +	*strchr(prop, '?') = char(tolower(aDrive));
  1.1228 +
  1.1229 +	TInt len;
  1.1230 +	const char* val;
  1.1231 +	if (iProperties.GetString(prop, val) == KErrNone)
  1.1232 +		{
  1.1233 +		len = strlen(val);
  1.1234 +		if (len > KMaxFileName)
  1.1235 +			return KErrArgument;
  1.1236 +		strcpy(drive, val);
  1.1237 +		}
  1.1238 +	else
  1.1239 +		{
  1.1240 +		// not in properties, so check environment
  1.1241 +		len = GetEnvironmentVariableA(prop, drive, KMaxFileName + 1);
  1.1242 +		if (len > KMaxFileName)
  1.1243 +			return KErrArgument;
  1.1244 +		}
  1.1245 +	while (len > 0 && isspace(drive[len-1]))
  1.1246 +		--len;
  1.1247 +	if (len == 0)
  1.1248 +		return KErrNotFound;
  1.1249 +	if (drive[len-1] != '\\') // add trailing backslash
  1.1250 +		drive[len++] = '\\';
  1.1251 +	if (drive[0] == '\\')
  1.1252 +		{
  1.1253 +		// put in the emulator drive
  1.1254 +		TInt r = iProperties.GetString("EmulatorPath", val);
  1.1255 +		if (r != KErrNone)
  1.1256 +			return r;
  1.1257 +
  1.1258 +		memmove(drive + 2, drive, len);
  1.1259 +		drive[0] = val[0];
  1.1260 +		drive[1] = ':';
  1.1261 +		len += 2;
  1.1262 +		}
  1.1263 +	else if (len < 3 || drive[1] != ':' || drive[2] != '\\')
  1.1264 +		return KErrArgument;
  1.1265 +#ifdef _UNICODE
  1.1266 +	TUint16* aBufPtr = (TUint16*)aBuffer.Ptr();
  1.1267 +	const TText* drv = (const TText*)drive;
  1.1268 +	for(int index=0;index<len;index++)
  1.1269 +		*aBufPtr++ = (TUint16)*drv++;
  1.1270 +	aBuffer.SetLength(len<<1);
  1.1271 +#else
  1.1272 +	aBuffer.Copy(TPtrC8((const TText8*)drive,len));
  1.1273 +#endif
  1.1274 +	return KErrNone;
  1.1275 +	}
  1.1276 +
  1.1277 +TInt Wins::MapFilename(const TDesC& aFilename, TDes& aBuffer) const
  1.1278 +//
  1.1279 +// Map aFileName to real windows directory - aFileName must be a full filepath
  1.1280 +//
  1.1281 +	{
  1.1282 +
  1.1283 +	// if the filename does not have a drive specified then don't imagine
  1.1284 +	// it describes an Epoc filepath
  1.1285 +	// Assume it's a subdirectory/file of the file containing the emulated Z drive
  1.1286 +	TInt offset;
  1.1287 +	if (aFilename.Length() < 4 || aFilename[2] != ':')
  1.1288 +		{
  1.1289 +		TInt r = MapDrive('z', aBuffer);
  1.1290 +		if (r)
  1.1291 +			return(r);
  1.1292 +		aBuffer.SetLength(aBuffer.Length()-4);	// chop "Z\\"
  1.1293 +		offset = aFilename[0] == '\\' ? 1 : 0; // remove the guaranteed backslash
  1.1294 +		}
  1.1295 +	else
  1.1296 +		{
  1.1297 +		TInt r = MapDrive(aFilename[0], aBuffer);
  1.1298 +		if (r)
  1.1299 +			return(r);
  1.1300 +		if (aFilename.Length() >= 6 && aFilename[4] == '\\')
  1.1301 +			offset = 3;
  1.1302 +		else
  1.1303 +			offset = 2;
  1.1304 +		}
  1.1305 +#ifdef _UNICODE
  1.1306 +	offset = offset<<1;
  1.1307 +	TUint8* ptrFilename = (TUint8*)aFilename.Ptr() + offset;
  1.1308 +	TUint8* ptrBuffer = (TUint8*)aBuffer.Ptr()+aBuffer.Length();
  1.1309 +	if (aBuffer.MaxLength()<aBuffer.Length()+aFilename.Length()-offset+1)
  1.1310 +		return KErrBadName;
  1.1311 +
  1.1312 +	memcpy(ptrBuffer, ptrFilename, aFilename.Length()-offset);
  1.1313 +	aBuffer.SetLength(aBuffer.Length()+aFilename.Length()-offset);
  1.1314 +#else
  1.1315 +	TPtrC name(aFilename.Mid(offset));
  1.1316 +	if (aBuffer.MaxLength()<aBuffer.Length()+name.Length()+1)
  1.1317 +		return KErrBadName;
  1.1318 +	aBuffer.Append(name);
  1.1319 +#endif
  1.1320 +	return KErrNone;
  1.1321 +	}
  1.1322 +
  1.1323 +
  1.1324 +//table of the property names which can be used in multiple configurations
  1.1325 +const char * KConfigSpecificProperties[] =
  1.1326 +	{
  1.1327 +	"ScreenWidth",
  1.1328 +	"ScreenHeight",
  1.1329 +	"PhysicalScreenWidth",
  1.1330 +	"PhysicalScreenHeight",
  1.1331 +	"ScreenOffsetX",
  1.1332 +	"ScreenOffsetY",
  1.1333 +	"LedSize",
  1.1334 +	"LedArrangeVertically",
  1.1335 +	"LedArrangeHorizontally",
  1.1336 +	"LedOffsetX",
  1.1337 +	"LedOffsetY",
  1.1338 +	"LedGap",
  1.1339 +	"PointerType",
  1.1340 +	"ColorDepth",
  1.1341 +	"KeyMap",
  1.1342 +	"DrawVirtualKeys",
  1.1343 +	"VirtualKeyColor",
  1.1344 +	"VirtualKey",
  1.1345 +	"MouseTarget",
  1.1346 +	"FasciaBitmap",
  1.1347 +	"DigitizerOffsetX",
  1.1348 +	"DigitizerOffsetY",
  1.1349 +	"DigitizerWidth",
  1.1350 +	"DigitizerHeight",
  1.1351 +	"DisableDigitizer",
  1.1352 +	"DefineKeyName",
  1.1353 +	"WindowTitle",
  1.1354 +	"NoVersionInfo",
  1.1355 +	"OnActivation",
  1.1356 +	"EmulatorControl",
  1.1357 +	"EmulatorControlHotKey",
  1.1358 +	"CompositionBuffers",
  1.1359 +	"RefreshRateHz",
  1.1360 +	};
  1.1361 +
  1.1362 +const char * KScreenSpecificProperties[] =
  1.1363 + 	{
  1.1364 + 	"ScreenWidth",
  1.1365 + 	"ScreenHeight",
  1.1366 + 	"PhysicalScreenWidth",
  1.1367 + 	"PhysicalScreenHeight",
  1.1368 + 	"ScreenOffsetX",
  1.1369 + 	"ScreenOffsetY",
  1.1370 + 	"ColorDepth",
  1.1371 +	"FasciaBitmap",
  1.1372 +	"CompositionBuffers",
  1.1373 +	"RefreshRateHz",
  1.1374 + 	};
  1.1375 +
  1.1376 +
  1.1377 +TBool Wins::ConfigSpecificProperty(const char * aProperty)
  1.1378 +	{
  1.1379 +	TInt x;
  1.1380 +	TInt count = sizeof(KConfigSpecificProperties) / sizeof(char *);
  1.1381 +	for (x = 0; x < count; ++x)
  1.1382 +		if (_stricmp(aProperty, KConfigSpecificProperties[x]) == 0)	return ETrue;
  1.1383 +	return EFalse;
  1.1384 +	}
  1.1385 +
  1.1386 +TBool Wins::ScreenSpecificProperty(const char * aProperty)
  1.1387 +	{
  1.1388 +	TInt x;
  1.1389 +	TInt count = sizeof(KScreenSpecificProperties) / sizeof(char *);
  1.1390 +	for (x = 0; x < count; ++x)
  1.1391 +		if (_stricmp(aProperty, KScreenSpecificProperties[x]) == 0)	return ETrue;
  1.1392 +	return EFalse;
  1.1393 +	}