os/boardsupport/emulator/emulatorbsp/specific/property.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
child 1 260cb5ec6c19
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1998-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
// wins\specific\property.cpp
sl@0
    15
// Emulator property management for emulator settings
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#define _CRTIMP			// we want to use the static runtime library
sl@0
    20
sl@0
    21
#define __INCLUDE_CAPABILITY_NAMES__
sl@0
    22
#include "variant.h"
sl@0
    23
#include <string.h>
sl@0
    24
#include <stdlib.h>
sl@0
    25
#include <emulator.h>
sl@0
    26
sl@0
    27
#ifdef _DEBUG
sl@0
    28
#define _DUMP_PROPERTY
sl@0
    29
#endif
sl@0
    30
sl@0
    31
const char* KDefaultMachineName = "epoc";
sl@0
    32
const char* KDefaultTestMachineName = "defaulttest";
sl@0
    33
sl@0
    34
// name of the environment variable to check for default path
sl@0
    35
const char* KDefaultEpocRootName = "EPOCROOT";
sl@0
    36
sl@0
    37
TInt ScreenId=0;
sl@0
    38
sl@0
    39
// At the point this is created there is no kernel heap available
sl@0
    40
// So all memory allocation is done from a custom allocator
sl@0
    41
sl@0
    42
const TInt KMaxTokenLength = 255;
sl@0
    43
sl@0
    44
class Allocator
sl@0
    45
	{
sl@0
    46
	enum {ETotalMemory=0x10000};	// 64K
sl@0
    47
public:
sl@0
    48
	Allocator();
sl@0
    49
	TAny* Alloc(TInt aSize);
sl@0
    50
	TAny* Realloc(TAny* aCell, TInt aSize);
sl@0
    51
	void Free(TAny* aCell);
sl@0
    52
private:
sl@0
    53
	TUint iBuf[ETotalMemory];
sl@0
    54
	TUint* iFree;
sl@0
    55
	};
sl@0
    56
sl@0
    57
Allocator::Allocator()
sl@0
    58
	:iFree(iBuf)
sl@0
    59
	{}
sl@0
    60
sl@0
    61
TAny* Allocator::Alloc(TInt aSize)
sl@0
    62
	{
sl@0
    63
	aSize = (aSize + 7) >> 2;
sl@0
    64
	if (iFree + aSize > iBuf + ETotalMemory)
sl@0
    65
		return NULL;
sl@0
    66
	TUint* p = iFree;
sl@0
    67
	iFree += aSize;
sl@0
    68
	*p++ = aSize;
sl@0
    69
	return p;
sl@0
    70
	}
sl@0
    71
sl@0
    72
TAny* Allocator::Realloc(TAny* aCell, TInt aSize)
sl@0
    73
	{
sl@0
    74
	if (!aCell)
sl@0
    75
		return Alloc(aSize);
sl@0
    76
sl@0
    77
	TUint* p = (TUint*)aCell;
sl@0
    78
	TInt size = *--p;
sl@0
    79
	if (iFree == p + size)
sl@0
    80
		{
sl@0
    81
		aSize = (aSize + 7) >> 2;
sl@0
    82
		if (p + aSize > iBuf + ETotalMemory)
sl@0
    83
			return NULL;
sl@0
    84
		iFree = p + aSize;
sl@0
    85
		*p = aSize;
sl@0
    86
		return aCell;
sl@0
    87
		}
sl@0
    88
sl@0
    89
	TAny* newp = Alloc(aSize);
sl@0
    90
	if (newp)
sl@0
    91
		{
sl@0
    92
		memcpy(newp, aCell, size*sizeof(TUint));
sl@0
    93
		Free(aCell);
sl@0
    94
		}
sl@0
    95
	return newp;
sl@0
    96
	}
sl@0
    97
sl@0
    98
void Allocator::Free(TAny* )
sl@0
    99
	{
sl@0
   100
	}
sl@0
   101
sl@0
   102
Allocator TheAllocator;
sl@0
   103
sl@0
   104
///////
sl@0
   105
sl@0
   106
char* Duplicate(const char* aString)
sl@0
   107
	{
sl@0
   108
	if (aString == NULL)
sl@0
   109
		aString = "";
sl@0
   110
sl@0
   111
	TInt size = strlen(aString) + 1;
sl@0
   112
	char* p = (char*)TheAllocator.Alloc(size);
sl@0
   113
	if (p)
sl@0
   114
		memcpy(p, aString, size);
sl@0
   115
	return p;
sl@0
   116
	}
sl@0
   117
sl@0
   118
// Properties class implementation
sl@0
   119
sl@0
   120
Properties::Properties()
sl@0
   121
	:iEntries(NULL),iCount(0),iSize(0)
sl@0
   122
	{}
sl@0
   123
sl@0
   124
const char* Properties::Insert(TInt aIndex, const char* aProperty, const char* aValue)
sl@0
   125
	{
sl@0
   126
	if (iCount == iSize)
sl@0
   127
		{
sl@0
   128
		TInt size = iSize == 0 ? 8 : iSize*2;
sl@0
   129
		TAny* array = TheAllocator.Realloc(iEntries, size*sizeof(SEntry));
sl@0
   130
		if (array == NULL)
sl@0
   131
			return NULL;
sl@0
   132
		iEntries = (SEntry*)array;
sl@0
   133
		iSize = size;
sl@0
   134
		}
sl@0
   135
sl@0
   136
	char* prop = Duplicate(aProperty);
sl@0
   137
	if (prop == NULL)
sl@0
   138
		return NULL;
sl@0
   139
	char* value = Duplicate(aValue);
sl@0
   140
	if (value == NULL)
sl@0
   141
		TheAllocator.Free(prop);
sl@0
   142
	else
sl@0
   143
		{
sl@0
   144
		SEntry* e = &iEntries[aIndex];
sl@0
   145
		memmove(e+1, e, (iCount-aIndex)*sizeof(SEntry));
sl@0
   146
		e->iProperty = prop;
sl@0
   147
		e->iValue = value;
sl@0
   148
		++iCount;
sl@0
   149
		}
sl@0
   150
	return value;
sl@0
   151
	}
sl@0
   152
sl@0
   153
const char* Properties::Replace(const char* aProperty, const char* aValue)
sl@0
   154
	{
sl@0
   155
	TInt ix = Find(aProperty);
sl@0
   156
	if (ix < 0)
sl@0
   157
		return Insert(~ix, aProperty, aValue);
sl@0
   158
	// replacing a property
sl@0
   159
	SEntry& e = iEntries[ix];
sl@0
   160
	char* value = Duplicate(aValue);
sl@0
   161
	if (value != NULL)
sl@0
   162
		{
sl@0
   163
		TheAllocator.Free(e.iValue);
sl@0
   164
		e.iValue = value;
sl@0
   165
		}
sl@0
   166
	return value;
sl@0
   167
	}
sl@0
   168
sl@0
   169
const char* Properties::Append(const char* aProperty, const char* aValue)
sl@0
   170
	{
sl@0
   171
	TInt ix = Find(aProperty);
sl@0
   172
	if (ix < 0)
sl@0
   173
		return Insert(~ix, aProperty, aValue);
sl@0
   174
sl@0
   175
	// append a property
sl@0
   176
	SEntry& e = iEntries[ix];
sl@0
   177
	TInt size = strlen(e.iValue) + strlen(aValue) + 2;
sl@0
   178
	char* value = (char*)TheAllocator.Realloc(e.iValue, size);
sl@0
   179
	if (value != NULL)
sl@0
   180
		{
sl@0
   181
		strcat(value, ";");
sl@0
   182
		strcat(value, aValue);
sl@0
   183
		e.iValue = value;
sl@0
   184
		}
sl@0
   185
	return value;
sl@0
   186
	}
sl@0
   187
sl@0
   188
TInt Properties::GetString(const char* aProperty, const char*& aValue) const
sl@0
   189
	{
sl@0
   190
	TInt ix = Find(aProperty);
sl@0
   191
	if (ix < 0)
sl@0
   192
		return KErrNotFound;
sl@0
   193
	aValue = iEntries[ix].iValue;
sl@0
   194
	return KErrNone;
sl@0
   195
	}
sl@0
   196
sl@0
   197
TInt Properties::GetInt(const char* aProperty, TInt& aValue) const
sl@0
   198
	{
sl@0
   199
	TInt ix = Find(aProperty);
sl@0
   200
	if (ix < 0)
sl@0
   201
		return KErrNotFound;
sl@0
   202
	char* value = iEntries[ix].iValue;
sl@0
   203
	char* end;
sl@0
   204
	TBool neg = *value=='-';
sl@0
   205
	value += neg;
sl@0
   206
	long val = strtoul(value, &end, 0);
sl@0
   207
	if(neg)
sl@0
   208
		{
sl@0
   209
		if(val<0)
sl@0
   210
			return KErrArgument;
sl@0
   211
		val = -val;
sl@0
   212
		}
sl@0
   213
	if (*end != '\0')
sl@0
   214
		return KErrArgument;
sl@0
   215
	aValue = val;
sl@0
   216
	return KErrNone;
sl@0
   217
	}
sl@0
   218
sl@0
   219
TInt Properties::GetBool(const char* aProperty, TBool& aValue, TBool aDefaultValue) const
sl@0
   220
	{
sl@0
   221
	TInt ix = Find(aProperty);
sl@0
   222
	if (ix < 0)
sl@0
   223
		{
sl@0
   224
		aValue = aDefaultValue;
sl@0
   225
		return KErrNone;
sl@0
   226
		}
sl@0
   227
	const char* value=iEntries[ix].iValue;
sl@0
   228
	if (_stricmp(value, "on")==0 || _stricmp(value, "yes")==0 || _stricmp(value, "1")==0 || strlen(value)==0)
sl@0
   229
		{
sl@0
   230
		aValue = ETrue;
sl@0
   231
		return KErrNone;
sl@0
   232
		}
sl@0
   233
	if (_stricmp(value, "off")==0 || _stricmp(value, "no")==0 || _stricmp(value, "0")==0 )
sl@0
   234
		{
sl@0
   235
		aValue = EFalse;
sl@0
   236
		return KErrNone;
sl@0
   237
		}
sl@0
   238
sl@0
   239
	// Bool property has an illegal value!
sl@0
   240
	return KErrArgument;
sl@0
   241
	}
sl@0
   242
sl@0
   243
TInt Properties::Find(const char* aProperty) const
sl@0
   244
//
sl@0
   245
// Lookup a property in the table
sl@0
   246
// return index (>=0) if found, ~insertion-point (<0) if not
sl@0
   247
//
sl@0
   248
	{
sl@0
   249
	TInt l = 0, r = iCount;
sl@0
   250
	while (l < r)
sl@0
   251
		{
sl@0
   252
		TInt m = (l + r) >> 1;
sl@0
   253
		const SEntry& e = iEntries[m];
sl@0
   254
		TInt k = _stricmp(aProperty,e.iProperty);
sl@0
   255
		if (k < 0)
sl@0
   256
			r = m;
sl@0
   257
		else if (k > 0)
sl@0
   258
			l = m + 1;
sl@0
   259
		else
sl@0
   260
			return m;
sl@0
   261
		}
sl@0
   262
	return ~l;
sl@0
   263
	}
sl@0
   264
sl@0
   265
#ifdef _DUMP_PROPERTY
sl@0
   266
void Properties::Dump() const
sl@0
   267
	{
sl@0
   268
	for (TInt i = 0; i < iCount; ++i)
sl@0
   269
		{
sl@0
   270
		const SEntry& e = iEntries[i];
sl@0
   271
		char buf[512];
sl@0
   272
		strcpy(buf, e.iProperty);
sl@0
   273
		TInt len = strlen(e.iValue);
sl@0
   274
		if (len)
sl@0
   275
			{
sl@0
   276
			strcat(buf, " = ");
sl@0
   277
			if (len <= 256)
sl@0
   278
				strcat(buf, e.iValue);
sl@0
   279
			else
sl@0
   280
				{
sl@0
   281
				strncat(buf, e.iValue, 256);
sl@0
   282
				strcat(buf, "...");
sl@0
   283
				}
sl@0
   284
			}
sl@0
   285
		strcat(buf, "\r\n");
sl@0
   286
		OutputDebugStringA(buf);
sl@0
   287
		}
sl@0
   288
	}
sl@0
   289
#endif
sl@0
   290
sl@0
   291
// Property related variant functions
sl@0
   292
sl@0
   293
TInt Wins::EmulatorHal(TInt aFunction, TAny* a1, TAny* a2)
sl@0
   294
	{
sl@0
   295
	TInt r=KErrNone;
sl@0
   296
	switch(aFunction)
sl@0
   297
		{
sl@0
   298
		case EEmulatorHalStringProperty:
sl@0
   299
			return iProperties.GetString((const char*)a1,*(const char**)a2);
sl@0
   300
		case EEmulatorHalIntProperty:
sl@0
   301
			return iProperties.GetInt((const char*)a1,*(TInt*)a2);
sl@0
   302
		case EEmulatorHalBoolProperty:
sl@0
   303
			return iProperties.GetBool((const char*)a1,*(TBool*)a2);
sl@0
   304
		case EEmulatorHalMapFilename:
sl@0
   305
			return MapFilename(*(const TDesC*)a1,*(TDes*)a2);
sl@0
   306
		case EEmulatorHalSetFlip:
sl@0
   307
			{
sl@0
   308
			if (iUi)
sl@0
   309
				{
sl@0
   310
				TInt screen = (TInt)a2;
sl@0
   311
				if((TUint)screen < (TUint)iUi->NumberOfScreens())
sl@0
   312
					return iUi->SetFlip(TEmulatorFlip(TInt(a1)),screen);
sl@0
   313
				}
sl@0
   314
			break;
sl@0
   315
			}
sl@0
   316
		case EEmulatorHalColorDepth:
sl@0
   317
			{
sl@0
   318
			TUint colorDepth = KDefaultColorDepth;
sl@0
   319
			if(iUi)
sl@0
   320
				{
sl@0
   321
				if((TUint)a2 < (TUint)iUi->NumberOfScreens())
sl@0
   322
					colorDepth = iUi->ColorDepth((TInt)a2);
sl@0
   323
				}
sl@0
   324
			*(TUint*)a1 = colorDepth;
sl@0
   325
			return KErrNone;
sl@0
   326
			}
sl@0
   327
		case EEmulatorHalCPUSpeed:
sl@0
   328
			if (a1)
sl@0
   329
				return SetCpuSpeed(TUint(a2)/1000);
sl@0
   330
			*(TInt*)a2 = iCpuSpeed ? iCpuSpeed * 1000 : 1;
sl@0
   331
			return KErrNone;
sl@0
   332
		case EEmulatorHalNumberOfScreens:
sl@0
   333
			*(TInt*)a2 = iUi ? iUi->NumberOfScreens() : 1;
sl@0
   334
			return KErrNone;
sl@0
   335
		case EEmulatorHalSetDisplayChannel:
sl@0
   336
			if (iUi && (TUint)a1 < (TUint)iUi->NumberOfScreens())
sl@0
   337
				{
sl@0
   338
				r = iUi->SetDisplayChannel((TInt)a1, static_cast<DDisplayChannel*>(a2));
sl@0
   339
				}
sl@0
   340
			else
sl@0
   341
				{
sl@0
   342
				r = KErrNotSupported;
sl@0
   343
				}
sl@0
   344
			break;
sl@0
   345
		default:
sl@0
   346
			r=KErrNotSupported;
sl@0
   347
			break;
sl@0
   348
		}
sl@0
   349
	return r;
sl@0
   350
	}
sl@0
   351
sl@0
   352
const char* KExtensionListNormal = "btracex.ldd;hcr.dll;winsgui;elocd.ldd;medint.pdd;medlfs.pdd;medmmc.pdd;epbusmmc.dll;epbusv.dll";
sl@0
   353
const char* KExtensionUsiiNand = "?medusiiw.pdd";
sl@0
   354
const char* KExtensionUsiiNandLoader = "?medusiiws.pdd";
sl@0
   355
const char* KExtensionUsiiNandTest = "?medusiiwt.pdd";
sl@0
   356
sl@0
   357
sl@0
   358
sl@0
   359
sl@0
   360
const int KMaxEpocRootSize = 120;
sl@0
   361
sl@0
   362
TInt Wins::InitProperties(TBool aRunExe)
sl@0
   363
	{
sl@0
   364
	if (iProperties.Replace("MachineName", KDefaultMachineName) == NULL)
sl@0
   365
		return KErrNoMemory;
sl@0
   366
sl@0
   367
    char epocRoot[KMaxEpocRootSize];
sl@0
   368
sl@0
   369
    TInt total = GetEnvironmentVariableA( KDefaultEpocRootName, epocRoot, KMaxEpocRootSize);
sl@0
   370
sl@0
   371
    if (total != 0)
sl@0
   372
		{
sl@0
   373
	    if (iProperties.Replace("EpocRoot", epocRoot) == NULL)
sl@0
   374
    	    return KErrNoMemory;
sl@0
   375
		}
sl@0
   376
sl@0
   377
	if (iProperties.Append("Extension", KExtensionListNormal) == NULL)
sl@0
   378
		return KErrNoMemory;
sl@0
   379
sl@0
   380
	char overrideCDrive[MAX_PATH];
sl@0
   381
	overrideCDrive[0] = '\0';
sl@0
   382
	TInt r = ProcessCommandLine(aRunExe, overrideCDrive);
sl@0
   383
	if (r != KErrNone)
sl@0
   384
		return r;
sl@0
   385
sl@0
   386
	r = SetupPaths();
sl@0
   387
	if (r != KErrNone)
sl@0
   388
		return r;
sl@0
   389
sl@0
   390
	r = LoadProperties();
sl@0
   391
	if (r != KErrNone)
sl@0
   392
		return r;
sl@0
   393
sl@0
   394
	//	get Unistore II Media Driver type from epoc.ini
sl@0
   395
	const char* value = NULL;
sl@0
   396
	
sl@0
   397
sl@0
   398
	iProperties.GetString("NandDriverType", value);
sl@0
   399
	if (value)
sl@0
   400
		{
sl@0
   401
		if (value && _stricmp("XSR",value)==0)
sl@0
   402
			{
sl@0
   403
			// epoc.ini "NandDriverType=XSR" for XSR/Unistore-II driver
sl@0
   404
			if (iProperties.Append("Extension", KExtensionUsiiNand) == NULL)
sl@0
   405
				return KErrNoMemory;		
sl@0
   406
			}
sl@0
   407
		else if (value && _stricmp("XSRNandloader",value)==0)
sl@0
   408
			{
sl@0
   409
			// epoc.ini "NandDriverType=XSRNandloader" for XSR/Unistore-II nandloader driver
sl@0
   410
			if (iProperties.Append("Extension", KExtensionUsiiNandLoader) == NULL)
sl@0
   411
				return KErrNoMemory;		
sl@0
   412
			}
sl@0
   413
		else if (value && _stricmp("XSRTest",value)==0)
sl@0
   414
			{
sl@0
   415
			// epoc.ini "NandDriverType=XSRTest" for XSR/Unistore-II test driver
sl@0
   416
			if (iProperties.Append("Extension", KExtensionUsiiNandTest) == NULL)
sl@0
   417
				return KErrNoMemory;
sl@0
   418
			}
sl@0
   419
		else	
sl@0
   420
			{
sl@0
   421
			// If epoc.ini contains "NandDriverType=???" but ??? not equal to any
sl@0
   422
			// of above XSR driver types then load production/release XSR
sl@0
   423
			// driver
sl@0
   424
			if (iProperties.Append("Extension", KExtensionUsiiNand) == NULL)
sl@0
   425
				return KErrNoMemory;
sl@0
   426
sl@0
   427
			}
sl@0
   428
		}
sl@0
   429
	else
sl@0
   430
		{
sl@0
   431
		// Load the production/release XSR driver, if value is NULL
sl@0
   432
		if (iProperties.Append("Extension", KExtensionUsiiNand) == NULL)
sl@0
   433
			return KErrNoMemory;
sl@0
   434
sl@0
   435
		}
sl@0
   436
	
sl@0
   437
sl@0
   438
//	load additional configuration specific properties
sl@0
   439
sl@0
   440
//	get the multi property "configuration"
sl@0
   441
	value = NULL;
sl@0
   442
	iProperties.GetString("configuration", value);
sl@0
   443
	
sl@0
   444
//	load each one of these
sl@0
   445
//	check for any screen specific properties in the main epoc.ini
sl@0
   446
//	if configuration property is set
sl@0
   447
	if (value && !iConfigPropertySet)	//configuration
sl@0
   448
		{
sl@0
   449
		iConfigId = 0;
sl@0
   450
		char configFileName[100];
sl@0
   451
		do
sl@0
   452
			{
sl@0
   453
			//load each set of properties
sl@0
   454
sl@0
   455
		   const char * pdest = strchr(value, ';');
sl@0
   456
		   TInt result = pdest - value;
sl@0
   457
		   if(pdest)
sl@0
   458
			   {
sl@0
   459
			   strncpy(configFileName, value, result);
sl@0
   460
			   configFileName[result] = '\0';
sl@0
   461
			   value = value + result + 1;
sl@0
   462
			   }
sl@0
   463
		   else
sl@0
   464
			   {
sl@0
   465
			   strcpy(configFileName, value);
sl@0
   466
			   value += strlen(value);
sl@0
   467
			   }
sl@0
   468
sl@0
   469
			r = LoadConfigSpecificProperties(configFileName);
sl@0
   470
			if (r == KErrNone)
sl@0
   471
				iConfigId++;
sl@0
   472
			}
sl@0
   473
		while(*value);
sl@0
   474
		}
sl@0
   475
sl@0
   476
	char scr[30];
sl@0
   477
	//if iConfigId is zero, there is only 1 configuration
sl@0
   478
	wsprintfA(scr, "ConfigCount %d", iConfigId ? iConfigId : 1);
sl@0
   479
	r = AddProperty(scr, scr+strlen(scr));
sl@0
   480
	if (r != KErrNone)
sl@0
   481
		return r;
sl@0
   482
sl@0
   483
	r = SetupMediaPath();
sl@0
   484
	if (r != KErrNone)
sl@0
   485
		return r;
sl@0
   486
sl@0
   487
	if (overrideCDrive[0] != '\0')
sl@0
   488
		SetupDrive('c', overrideCDrive);
sl@0
   489
sl@0
   490
	if (iProperties.Append("Extension", "exstart") == NULL)
sl@0
   491
		return KErrNoMemory;
sl@0
   492
sl@0
   493
#ifdef _DUMP_PROPERTY
sl@0
   494
	iProperties.Dump();
sl@0
   495
#endif
sl@0
   496
sl@0
   497
	return KErrNone;
sl@0
   498
	}
sl@0
   499
sl@0
   500
char* skipws(char* aPtr)
sl@0
   501
	{
sl@0
   502
	while (isspace(*aPtr))
sl@0
   503
		++aPtr;
sl@0
   504
	return aPtr;
sl@0
   505
	}
sl@0
   506
sl@0
   507
char* skiptok(char* aPtr)
sl@0
   508
	{
sl@0
   509
	if (*aPtr == '\"')
sl@0
   510
		{
sl@0
   511
		++aPtr;
sl@0
   512
		while (*aPtr && *aPtr++ != '\"')
sl@0
   513
			{}
sl@0
   514
		}
sl@0
   515
	else
sl@0
   516
		{
sl@0
   517
		while (*aPtr && !isspace(*aPtr))
sl@0
   518
			++aPtr;
sl@0
   519
		}
sl@0
   520
	return aPtr;
sl@0
   521
	}
sl@0
   522
sl@0
   523
#ifdef _DEBUG
sl@0
   524
struct TDebugTrace
sl@0
   525
	{
sl@0
   526
	const char* iName;
sl@0
   527
	TInt iMask;
sl@0
   528
	};
sl@0
   529
sl@0
   530
// Only the first 32 trace bits can be defined using these values
sl@0
   531
// "ALWAYS" in this context means all of the first 32 bits not all 256 bits
sl@0
   532
const TDebugTrace KTraceValues[] =
sl@0
   533
	{
sl@0
   534
	{"ALWAYS", KALWAYS},
sl@0
   535
	{"BOOT", 1<<KBOOT},
sl@0
   536
	{"DEVICE", 1<<KDEVICE},
sl@0
   537
	{"DFC", 1<<KDFC},
sl@0
   538
	{"DLL", 1<<KDLL},
sl@0
   539
	{"EVENT", 1<<KEVENT},      
sl@0
   540
	{"EXEC", 1<<KEXEC},
sl@0
   541
	{"DEBUGGER", 1<<KDEBUGGER},
sl@0
   542
	{"EXTENSION", 1<<KEXTENSION},
sl@0
   543
	{"FAIL", 1<<KFAIL},
sl@0
   544
	{"HARDWARE", 1<<KHARDWARE},
sl@0
   545
	{"IPC", 1<<KIPC},
sl@0
   546
	{"LOCDRV", 1<<KLOCDRV},
sl@0
   547
	{"MEMTRACE", 1<<KMEMTRACE},
sl@0
   548
	{"MMU", 1<<KMMU},
sl@0
   549
	{"NKERN", 1<<KNKERN},
sl@0
   550
	{"OBJECT", 1<<KOBJECT},
sl@0
   551
	{"PANIC", 1<<KPANIC},
sl@0
   552
	{"PBUS1", 1<<KPBUS1},
sl@0
   553
	{"PBUS2", 1<<KPBUS2},
sl@0
   554
	{"PBUSDRV", 1<<KPBUSDRV},
sl@0
   555
	{"POWER", 1<<KPOWER},      
sl@0
   556
	{"PROC", 1<<KPROC},
sl@0
   557
	{"SCHED", 1<<KSCHED},
sl@0
   558
	{"SCHED2", 1<<KSCHED2},
sl@0
   559
	{"SCRATCH", 1<<KSCRATCH},
sl@0
   560
	{"SEMAPHORE", 1<<KSEMAPHORE},
sl@0
   561
	{"SERVER", 1<<KSERVER},
sl@0
   562
	{"THREAD", 1<<KTHREAD},
sl@0
   563
	{"THREAD2", 1<<KTHREAD2},
sl@0
   564
	{"TIMING", 1<<KTIMING},
sl@0
   565
	{"DMA", 1<<KDMA},
sl@0
   566
	{"MMU2", 1<<KMMU2}
sl@0
   567
	};
sl@0
   568
const TInt KMaxTraceName = 9;
sl@0
   569
const TInt KCountTraceValues = sizeof(KTraceValues)/sizeof(TDebugTrace);
sl@0
   570
sl@0
   571
static const TDebugTrace* TraceType(const char* aTrace, TInt aLen)
sl@0
   572
	{
sl@0
   573
	if (aLen > KMaxTraceName)
sl@0
   574
		return 0;
sl@0
   575
sl@0
   576
	char name[KMaxTraceName + 1];
sl@0
   577
	strncpy(name, aTrace, aLen);
sl@0
   578
	name[aLen] = '\0';
sl@0
   579
sl@0
   580
	for (TInt i=KCountTraceValues; --i>=0;)
sl@0
   581
		{
sl@0
   582
		if (_stricmp(name, KTraceValues[i].iName)==0)
sl@0
   583
			return &KTraceValues[i];
sl@0
   584
		}
sl@0
   585
	return 0;
sl@0
   586
	}
sl@0
   587
#endif
sl@0
   588
sl@0
   589
TInt Wins::DebugMask()
sl@0
   590
	{
sl@0
   591
	TInt mask = KDefaultDebugMask;
sl@0
   592
	if (iProperties.GetInt("DebugMask", mask) != KErrArgument)
sl@0
   593
		return mask;
sl@0
   594
#ifdef _DEBUG
sl@0
   595
	// allow text ones
sl@0
   596
	const char* e;
sl@0
   597
	if (iProperties.GetString("DebugMask", e) != KErrNone)
sl@0
   598
		return mask;
sl@0
   599
sl@0
   600
	for (;;)
sl@0
   601
		{
sl@0
   602
		char* p = skipws((char*)e);
sl@0
   603
		if (*p == 0)
sl@0
   604
			break;
sl@0
   605
		e = skiptok(p);
sl@0
   606
		TBool add = ETrue;
sl@0
   607
		if (*p == '+')
sl@0
   608
			++p;
sl@0
   609
		else if (*p == '-')
sl@0
   610
			{
sl@0
   611
			add = EFalse;
sl@0
   612
			++p;
sl@0
   613
			}
sl@0
   614
		const TDebugTrace* type = TraceType(p, e - p);
sl@0
   615
		if (type)
sl@0
   616
			{
sl@0
   617
			if (add)
sl@0
   618
				mask |= type->iMask;
sl@0
   619
			else
sl@0
   620
				mask &= ~type->iMask;
sl@0
   621
			}
sl@0
   622
		}
sl@0
   623
#endif
sl@0
   624
	return mask;
sl@0
   625
	}
sl@0
   626
sl@0
   627
TUint32 Wins::KernelConfigFlags()
sl@0
   628
	{
sl@0
   629
	TUint32 flags = 0;
sl@0
   630
	TBool b;
sl@0
   631
sl@0
   632
	b=0;
sl@0
   633
	iProperties.GetBool("PlatSecEnforcement",b,EFalse);
sl@0
   634
	if(b) flags |= EKernelConfigPlatSecEnforcement;
sl@0
   635
	Wins::EarlyLogging("PlatSecEnforcement ",b?"ON":"OFF");
sl@0
   636
sl@0
   637
	b=0;
sl@0
   638
	iProperties.GetBool("PlatSecDiagnostics",b,EFalse);
sl@0
   639
	if(b) flags |= EKernelConfigPlatSecDiagnostics;
sl@0
   640
	Wins::EarlyLogging("PlatSecDiagnostics ",b?"ON":"OFF");
sl@0
   641
sl@0
   642
	b=0;
sl@0
   643
	iProperties.GetBool("PlatSecProcessIsolation",b,EFalse);
sl@0
   644
	if(b) flags |= EKernelConfigPlatSecProcessIsolation;
sl@0
   645
	Wins::EarlyLogging("PlatSecProcessIsolation ",b?"ON":"OFF");
sl@0
   646
sl@0
   647
	b=0;
sl@0
   648
	iProperties.GetBool("PlatSecEnforceSysBin",b,EFalse);
sl@0
   649
	if(b) flags |= EKernelConfigPlatSecEnforceSysBin;
sl@0
   650
	Wins::EarlyLogging("PlatSecEnforceSysBin ",b?"ON":"OFF");
sl@0
   651
sl@0
   652
	b=0;
sl@0
   653
	iProperties.GetBool("CrazyScheduling",b,EFalse);
sl@0
   654
	if(b) flags |= EKernelConfigCrazyScheduling;
sl@0
   655
	Wins::EarlyLogging("CrazyScheduling ",b?"ON":"OFF");
sl@0
   656
sl@0
   657
	return flags;
sl@0
   658
	}
sl@0
   659
sl@0
   660
void Wins::DisabledCapabilities(SCapabilitySet& aCapabilities)
sl@0
   661
	{
sl@0
   662
	const char* text;
sl@0
   663
	if(iProperties.GetString("PlatSecDisabledCaps", text)!=KErrNone)
sl@0
   664
		text = "NONE";
sl@0
   665
	Wins::EarlyLogging("PlatSecDisabledCaps ",text);
sl@0
   666
	ParseCapabilitiesArg(aCapabilities,text);
sl@0
   667
	}
sl@0
   668
sl@0
   669
#define PARSE_CAPABILITIES_ERROR(aMessage) Wins::EarlyLogging(aMessage,0)
sl@0
   670
#define PARSE_CAPABILITIES_ERROR2(aMessage,aArg) Wins::EarlyLogging(aMessage,aArg)
sl@0
   671
#define strnicmp _strnicmp
sl@0
   672
sl@0
   673
TInt Wins::ParseCapabilitiesArg(SCapabilitySet& aCapabilities, const char *aText)
sl@0
   674
//
sl@0
   675
// This is a cun'n'paste copy of the function in TOOLS\E32TOOLS\HOST\H_UTIL.CPP
sl@0
   676
// Keep both of these versions up to date with each other
sl@0
   677
//
sl@0
   678
	{
sl@0
   679
	memset(&aCapabilities,0,sizeof(aCapabilities));
sl@0
   680
	char c;
sl@0
   681
	while((c=*aText)!=0)
sl@0
   682
		{
sl@0
   683
		if(c<=' ')
sl@0
   684
			{
sl@0
   685
			++aText;
sl@0
   686
			continue;
sl@0
   687
			}
sl@0
   688
		int invert=0;
sl@0
   689
		if(c=='+')
sl@0
   690
			{
sl@0
   691
			++aText;
sl@0
   692
			c=*aText;
sl@0
   693
			}
sl@0
   694
		if(c=='-')
sl@0
   695
			{
sl@0
   696
			invert=1;
sl@0
   697
			++aText;
sl@0
   698
			}
sl@0
   699
		const char* name = aText;
sl@0
   700
		while((c=*aText)>' ')
sl@0
   701
			{
sl@0
   702
			if(c=='-' || c=='+')
sl@0
   703
				break;
sl@0
   704
			++aText;
sl@0
   705
			}
sl@0
   706
		TInt n = aText-name;
sl@0
   707
		TInt i;
sl@0
   708
sl@0
   709
		if(n==3 && strnicmp("all",name,n)==0)
sl@0
   710
			{
sl@0
   711
			if(invert)
sl@0
   712
				{
sl@0
   713
				PARSE_CAPABILITIES_ERROR("Capability '-ALL' not allowed");
sl@0
   714
				return KErrArgument;
sl@0
   715
				}
sl@0
   716
			for(i=0; i<ECapability_Limit; i++)
sl@0
   717
				{
sl@0
   718
				if(CapabilityNames[i])
sl@0
   719
					aCapabilities[i>>5] |= (1<<(i&31));
sl@0
   720
				}
sl@0
   721
			continue;
sl@0
   722
			}
sl@0
   723
sl@0
   724
		if(n==4 && strnicmp("none",name,n)==0)
sl@0
   725
			{
sl@0
   726
			if(invert)
sl@0
   727
				{
sl@0
   728
				PARSE_CAPABILITIES_ERROR("Capability '-NONE' not allowed");
sl@0
   729
				return KErrArgument;
sl@0
   730
				}
sl@0
   731
			memset(&aCapabilities,0,sizeof(aCapabilities));
sl@0
   732
			continue;
sl@0
   733
			}
sl@0
   734
sl@0
   735
		for(i=0; i<ECapability_Limit; i++)
sl@0
   736
			{
sl@0
   737
			const char* cap = CapabilityNames[i];
sl@0
   738
			if(!cap)
sl@0
   739
				continue;
sl@0
   740
			if((int)strlen(cap)!=n)
sl@0
   741
				continue;
sl@0
   742
			if(strnicmp(cap,name,n)!=0)
sl@0
   743
				continue;
sl@0
   744
			break;
sl@0
   745
			}
sl@0
   746
		if(i>=ECapability_Limit)
sl@0
   747
			{
sl@0
   748
			char badName[32];
sl@0
   749
			if(n>=sizeof(badName)) n=sizeof(badName)-1;
sl@0
   750
			memcpy(badName,name,n);
sl@0
   751
			badName[n]=0;
sl@0
   752
			PARSE_CAPABILITIES_ERROR2("Unrecognised capability name: ",badName);
sl@0
   753
			return KErrArgument;
sl@0
   754
			}
sl@0
   755
		if(invert)
sl@0
   756
			aCapabilities[i>>5] &= ~(1<<(i&31));
sl@0
   757
		else
sl@0
   758
			aCapabilities[i>>5] |= (1<<(i&31));
sl@0
   759
		}
sl@0
   760
	return KErrNone;
sl@0
   761
	}
sl@0
   762
sl@0
   763
TInt Wins::AddProperty(char* aProperty, const char* aEol)
sl@0
   764
	{
sl@0
   765
	const char* tok = aProperty;
sl@0
   766
	int c;
sl@0
   767
	do
sl@0
   768
		{
sl@0
   769
		if (aProperty == aEol)
sl@0
   770
			{
sl@0
   771
			// boolean property
sl@0
   772
			if (_stricmp(tok, "_NewScreen_") == 0)
sl@0
   773
 				{
sl@0
   774
 				++ScreenId;
sl@0
   775
 				return KErrNone;
sl@0
   776
 				}
sl@0
   777
			else
sl@0
   778
				{
sl@0
   779
				char newtok[KMaxTokenLength];
sl@0
   780
				if (ConfigSpecificProperty(tok))
sl@0
   781
					{
sl@0
   782
					wsprintfA(newtok, "Configuration[%d]", iConfigId);
sl@0
   783
					strcat(newtok, tok);
sl@0
   784
					tok = newtok;
sl@0
   785
					}
sl@0
   786
				}
sl@0
   787
			return iProperties.Replace(tok, NULL) == NULL ? KErrNoMemory : KErrNone;
sl@0
   788
			}
sl@0
   789
		c=*aProperty++;
sl@0
   790
		} while (isalnum(c) || c=='_');
sl@0
   791
	aProperty[-1]='\0';	// terminate property name
sl@0
   792
	while (isspace(c))
sl@0
   793
		c=*aProperty++;
sl@0
   794
	TBool append=ETrue;
sl@0
   795
	if (c=='=')
sl@0
   796
		{
sl@0
   797
		append=EFalse;
sl@0
   798
		c=*aProperty++;
sl@0
   799
		}
sl@0
   800
	else if (c=='+' && *aProperty=='=')
sl@0
   801
		{
sl@0
   802
		++aProperty;
sl@0
   803
		c=*aProperty++;
sl@0
   804
		}
sl@0
   805
	while (isspace(c))
sl@0
   806
		c=*aProperty++;
sl@0
   807
	--aProperty;	// point back to value
sl@0
   808
sl@0
   809
	if (_strnicmp(tok, "_epoc_drive_", 12) == 0)
sl@0
   810
		return SetupDrive(tok[12], aProperty);
sl@0
   811
	else
sl@0
   812
		{
sl@0
   813
		char newtok[KMaxTokenLength];
sl@0
   814
		if (ConfigSpecificProperty(tok))
sl@0
   815
			{
sl@0
   816
			if (ScreenSpecificProperty(tok))
sl@0
   817
				{
sl@0
   818
				wsprintfA(newtok, "Configuration[%d][%d]", iConfigId, ScreenId);
sl@0
   819
				}
sl@0
   820
			else
sl@0
   821
				{
sl@0
   822
				wsprintfA(newtok, "Configuration[%d]", iConfigId);
sl@0
   823
				}
sl@0
   824
			strcat(newtok, tok);
sl@0
   825
			tok = newtok;
sl@0
   826
			}
sl@0
   827
		if (append)
sl@0
   828
			return iProperties.Append(tok, aProperty) == NULL ? KErrNoMemory : KErrNone;
sl@0
   829
		else
sl@0
   830
			return iProperties.Replace(tok, aProperty) == NULL ? KErrNoMemory : KErrNone;
sl@0
   831
		}
sl@0
   832
	}
sl@0
   833
sl@0
   834
TInt Wins::ProcessCommandLine(TBool aRunExe, char* aCDrive)
sl@0
   835
	{
sl@0
   836
	if (aRunExe)
sl@0
   837
		{
sl@0
   838
		char exe[MAX_PATH];
sl@0
   839
		DWORD len=GetModuleFileNameA(NULL, exe, MAX_PATH);
sl@0
   840
		if (len == 0)
sl@0
   841
			return KErrGeneral;
sl@0
   842
		exe[len] = '\0';
sl@0
   843
		const char* base = strrchr(exe, '\\') + 1;
sl@0
   844
		if (iProperties.Replace("AutoRun", base) == NULL)
sl@0
   845
			return KErrNoMemory;
sl@0
   846
		}
sl@0
   847
sl@0
   848
	char* cmd = skipws(skiptok(GetCommandLineA()));
sl@0
   849
	if (strstr(cmd, "--") != NULL)
sl@0
   850
		{
sl@0
   851
		for (;;)
sl@0
   852
			{
sl@0
   853
			cmd = strchr(cmd, '-') + 1;
sl@0
   854
			TInt opt = *cmd++;
sl@0
   855
			if (opt == '-')
sl@0
   856
				break;
sl@0
   857
			char* end = skiptok(cmd);
sl@0
   858
			*end = '\0';
sl@0
   859
			switch (tolower(opt))
sl@0
   860
				{
sl@0
   861
			    case 'd':
sl@0
   862
				    {
sl@0
   863
				    TInt r = AddProperty(cmd, end);
sl@0
   864
				    if (r != KErrNone)
sl@0
   865
				    	return r;
sl@0
   866
				    }
sl@0
   867
				    break;
sl@0
   868
			    case 'm':
sl@0
   869
			    	// specify base name for .INI file
sl@0
   870
				    if (iProperties.Replace("MachineName", cmd) == NULL)
sl@0
   871
				    	return KErrNoMemory;
sl@0
   872
				    break;
sl@0
   873
			    case 'l':
sl@0
   874
			    	// specify language
sl@0
   875
			    	if (iProperties.Replace("TheMachineLanguageIndex", cmd) == NULL)
sl@0
   876
			    		return KErrNoMemory;
sl@0
   877
			    	break;
sl@0
   878
		    	case 'c':
sl@0
   879
		    		// specify path for emulated C drive
sl@0
   880
			    	{
sl@0
   881
			    	DWORD len=GetFullPathNameA(cmd, MAX_PATH, aCDrive, NULL);
sl@0
   882
			    	if (len==0 || len >= MAX_PATH)
sl@0
   883
			    		aCDrive[0] = '\0';
sl@0
   884
			    	}
sl@0
   885
			        break;
sl@0
   886
			    case 't':
sl@0
   887
			    	// specify the temp path as the emulated C drive
sl@0
   888
			    	{
sl@0
   889
			    	DWORD len=GetTempPathA(MAX_PATH, aCDrive);
sl@0
   890
			    	if (len==0 || len >= MAX_PATH)
sl@0
   891
			    		aCDrive[0] = '\0';
sl@0
   892
			    	}
sl@0
   893
			    	break;
sl@0
   894
				}
sl@0
   895
			cmd = end+1;
sl@0
   896
			}
sl@0
   897
		cmd = skipws(cmd);
sl@0
   898
		}
sl@0
   899
sl@0
   900
	if (aRunExe && iProperties.Replace("CommandLine", cmd) == NULL)
sl@0
   901
		return KErrNoMemory;
sl@0
   902
sl@0
   903
	return KErrNone;
sl@0
   904
	}
sl@0
   905
sl@0
   906
TInt Wins::LoadConfigSpecificProperties(const char * aFile)
sl@0
   907
	{
sl@0
   908
	const char* path;
sl@0
   909
	TInt r = iProperties.GetString("EmulatorDataPath", path);
sl@0
   910
	if (r != KErrNone)
sl@0
   911
		return r;
sl@0
   912
sl@0
   913
	char file[KMaxFileName + 1];
sl@0
   914
	strcpy(file, path);
sl@0
   915
	strcat(file, aFile);
sl@0
   916
sl@0
   917
	char* iniData;
sl@0
   918
	r = ReadIniFile(file, iniData);
sl@0
   919
	if (r == KErrNone)
sl@0
   920
		{
sl@0
   921
		r = ReadProperties(iniData);
sl@0
   922
		VirtualFree(iniData, 0, MEM_RELEASE);
sl@0
   923
		}
sl@0
   924
	else if (r == KErrNotFound)
sl@0
   925
		r = KErrNotFound;
sl@0
   926
sl@0
   927
	return r;
sl@0
   928
sl@0
   929
	}
sl@0
   930
sl@0
   931
sl@0
   932
sl@0
   933
TInt Wins::LoadProperties()
sl@0
   934
	{
sl@0
   935
	const char* path;
sl@0
   936
	TInt r = iProperties.GetString("EmulatorDataPath", path);
sl@0
   937
	if (r != KErrNone)
sl@0
   938
		return r;
sl@0
   939
	const char* name;
sl@0
   940
	r = iProperties.GetString("MachineName", name);
sl@0
   941
	if (r != KErrNone)
sl@0
   942
		return r;
sl@0
   943
retry:
sl@0
   944
	char file[KMaxFileName + 1];
sl@0
   945
	strcpy(file, path);
sl@0
   946
	strcat(file, name);
sl@0
   947
	strcat(file, ".ini");
sl@0
   948
sl@0
   949
	char* iniData;
sl@0
   950
	r = ReadIniFile(file, iniData);
sl@0
   951
	if (r == KErrNone)
sl@0
   952
		{
sl@0
   953
		r = ReadProperties(iniData);
sl@0
   954
		VirtualFree(iniData, 0, MEM_RELEASE);
sl@0
   955
		}
sl@0
   956
	else if (r == KErrNotFound)
sl@0
   957
		{
sl@0
   958
		if(_stricmp(name,KDefaultMachineName)==0)
sl@0
   959
			{
sl@0
   960
			// try test ini file
sl@0
   961
			name = KDefaultTestMachineName;
sl@0
   962
			goto retry;
sl@0
   963
			}
sl@0
   964
		r = KErrNone;		// no ini file - oh well
sl@0
   965
		}
sl@0
   966
sl@0
   967
	return r;
sl@0
   968
	}
sl@0
   969
sl@0
   970
TInt Wins::ReadProperties(char* aData)
sl@0
   971
	{
sl@0
   972
	ScreenId = 0;
sl@0
   973
	while (*aData)
sl@0
   974
		{
sl@0
   975
		char* beg = aData;
sl@0
   976
		char* eol = strchr(beg, '\n');
sl@0
   977
		aData = eol+1;
sl@0
   978
		if (eol == beg)
sl@0
   979
			continue;
sl@0
   980
		if (eol[-1] == '\r' && --eol == beg)
sl@0
   981
			continue;
sl@0
   982
		*eol = '\0';		// terminate line
sl@0
   983
sl@0
   984
		while (isspace(*beg))
sl@0
   985
			++beg;
sl@0
   986
		char* comment = strchr(beg, '#');
sl@0
   987
		if (comment)
sl@0
   988
			eol = comment;
sl@0
   989
		while (eol > beg && isspace(eol[-1]))
sl@0
   990
			--eol;
sl@0
   991
		if (beg == eol)
sl@0
   992
			continue;
sl@0
   993
		*eol = '\0';		// terminate line
sl@0
   994
sl@0
   995
		TInt r = AddProperty(beg, eol);
sl@0
   996
		if (r != KErrNone)
sl@0
   997
			return r;
sl@0
   998
		}
sl@0
   999
	char sc[5];
sl@0
  1000
 	wsprintfA(sc, "%d", ScreenId+1);
sl@0
  1001
	TInt screens;
sl@0
  1002
	if(iProperties.GetInt("[screens]", screens) == KErrNone && screens > ScreenId)
sl@0
  1003
		return KErrNone;
sl@0
  1004
 	else
sl@0
  1005
		return iProperties.Replace("[screens]", sc)  == NULL ? KErrNoMemory : KErrNone;
sl@0
  1006
	}
sl@0
  1007
sl@0
  1008
TInt Wins::ReadIniFile(const char* aFileName, char*& aContents)
sl@0
  1009
	{
sl@0
  1010
	TInt r = KErrNone;
sl@0
  1011
	HANDLE file=CreateFileA(aFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
sl@0
  1012
	if (!file || file==INVALID_HANDLE_VALUE)
sl@0
  1013
		r = KErrNotFound;	// More than likely !
sl@0
  1014
	else
sl@0
  1015
		{
sl@0
  1016
		TInt size=GetFileSize(file, NULL);
sl@0
  1017
		if (size==INVALID_FILE_SIZE)
sl@0
  1018
			r = KErrGeneral;
sl@0
  1019
		else
sl@0
  1020
			{
sl@0
  1021
			// fileSize+3 to ensure zero-termination of file and trailing CRLF
sl@0
  1022
			// VirtualAlloc initializes memory to zero
sl@0
  1023
			TAny* data = VirtualAlloc(NULL, size+3, MEM_COMMIT, PAGE_READWRITE);
sl@0
  1024
			if (!data)
sl@0
  1025
				r = KErrNoMemory;
sl@0
  1026
			else
sl@0
  1027
				{
sl@0
  1028
				DWORD bytesRead;
sl@0
  1029
				if (!ReadFile(file, data, size, &bytesRead, NULL))
sl@0
  1030
					{
sl@0
  1031
					VirtualFree(data, 0, MEM_RELEASE);
sl@0
  1032
					r = KErrGeneral;
sl@0
  1033
					}
sl@0
  1034
				else
sl@0
  1035
					{
sl@0
  1036
					aContents = (LPSTR)data;
sl@0
  1037
					strcpy(aContents + size,"\r\n");
sl@0
  1038
					}
sl@0
  1039
				}
sl@0
  1040
			}
sl@0
  1041
		CloseHandle(file);
sl@0
  1042
		}
sl@0
  1043
	return r;
sl@0
  1044
	}
sl@0
  1045
sl@0
  1046
sl@0
  1047
TInt Wins::SetupPaths()
sl@0
  1048
//
sl@0
  1049
// set up the Emulator paths
sl@0
  1050
//
sl@0
  1051
	{
sl@0
  1052
	// the Emulator path
sl@0
  1053
	CHAR path[KMaxFileName + 1];
sl@0
  1054
	DWORD len=GetModuleFileNameA(NULL, path, KMaxFileName);
sl@0
  1055
	if (len == 0)
sl@0
  1056
		return(KErrGeneral);
sl@0
  1057
	path[len] = '\0';
sl@0
  1058
	_strlwr(path);
sl@0
  1059
	*(strrchr(path, '\\') + 1) = '\0';
sl@0
  1060
	const char* emulatorPath = iProperties.Replace("EmulatorPath", path);
sl@0
  1061
	if (!emulatorPath)
sl@0
  1062
		return KErrNoMemory;
sl@0
  1063
sl@0
  1064
	CHAR drive[KMaxFileName + 1];
sl@0
  1065
sl@0
  1066
	// the Emulator data path
sl@0
  1067
	strcat(path, "data\\");
sl@0
  1068
	DWORD att = GetFileAttributesA(path);
sl@0
  1069
	if (att != -1 && (att&FILE_ATTRIBUTE_DIRECTORY))
sl@0
  1070
		{
sl@0
  1071
		// if Data directory exists in the emulator path, do things the new way
sl@0
  1072
		strcpy(drive, emulatorPath);
sl@0
  1073
		strcat(drive,"c\\");
sl@0
  1074
		}
sl@0
  1075
	else
sl@0
  1076
		{
sl@0
  1077
		// the old way
sl@0
  1078
#if defined(__VC32__)
sl@0
  1079
		char* p = strstr(path, "\\epoc32\\release\\wins\\");
sl@0
  1080
#elif defined(__CW32__)
sl@0
  1081
		char* p = strstr(path, "\\epoc32\\release\\winscw\\");
sl@0
  1082
#endif
sl@0
  1083
		if (p == NULL)
sl@0
  1084
			return KErrNotFound;
sl@0
  1085
		strcpy(p, "\\epoc32\\");
sl@0
  1086
		strcpy(drive, path);
sl@0
  1087
		strcat(path, "data\\");
sl@0
  1088
#if defined(__VC32__)
sl@0
  1089
		strcat(drive,"wins\\c\\");
sl@0
  1090
#elif defined(__CW32__)
sl@0
  1091
		strcat(drive,"winscw\\c\\");
sl@0
  1092
#endif
sl@0
  1093
		}
sl@0
  1094
	if (!iProperties.Replace("EmulatorDataPath", path))
sl@0
  1095
		return KErrNoMemory;
sl@0
  1096
sl@0
  1097
	// The Emulator Image path (for temporary EXE files)
sl@0
  1098
	const char* eip;
sl@0
  1099
	TInt r = iProperties.GetString("EmulatorImagePath", eip);
sl@0
  1100
	if (r!=KErrNone)
sl@0
  1101
		{
sl@0
  1102
		len=GetTempPathA(KMaxFileName, path);
sl@0
  1103
		strcat(path, "epoc\\");
sl@0
  1104
		char* p = path + strlen(path);
sl@0
  1105
		*p++ = emulatorPath[0];
sl@0
  1106
		strcpy(p, emulatorPath+2);
sl@0
  1107
		if (!iProperties.Replace("EmulatorImagePath", path))
sl@0
  1108
			return KErrNoMemory;
sl@0
  1109
		}
sl@0
  1110
	else
sl@0
  1111
		strcpy(path, eip);
sl@0
  1112
	if (!Emulator::CreateAllDirectories(path))
sl@0
  1113
		return Emulator::LastError();
sl@0
  1114
sl@0
  1115
	// Win32 filesystem paths mapped to local WINS drives
sl@0
  1116
	r = SetupDrive('c',drive);  // set up C here, can be overridden by system.ini settings
sl@0
  1117
	if (r)
sl@0
  1118
		return(r);
sl@0
  1119
sl@0
  1120
	strcpy(drive, emulatorPath);
sl@0
  1121
	strcat(drive,"z\\");
sl@0
  1122
sl@0
  1123
	r=SetupDrive('z',drive);  // set up Z here, can be overridden by system.ini settings
sl@0
  1124
	if (r)
sl@0
  1125
		return(r);
sl@0
  1126
sl@0
  1127
	return(KErrNone);
sl@0
  1128
	}
sl@0
  1129
sl@0
  1130
TInt Wins::SetupMediaPath()
sl@0
  1131
//
sl@0
  1132
// Set up the path for emulated media devices 'EmulatedMediaPath'
sl@0
  1133
// The default is <datapath>media/
sl@0
  1134
// The system temporary path can be set by value '%temp%'
sl@0
  1135
//
sl@0
  1136
	{
sl@0
  1137
	CHAR path[KMaxFileName + 1];
sl@0
  1138
	const char* mpath;
sl@0
  1139
	if (iProperties.GetString("EmulatorMediaPath", mpath) == KErrNotFound)
sl@0
  1140
		{
sl@0
  1141
		const char* dpath;
sl@0
  1142
		TInt r = iProperties.GetString("EmulatorDataPath", dpath);
sl@0
  1143
		if (r != KErrNone)
sl@0
  1144
			return r;
sl@0
  1145
		strcpy(path, dpath);
sl@0
  1146
		strcat(path, "media\\");
sl@0
  1147
		return iProperties.Replace("EmulatorMediaPath", path) ? KErrNone : KErrNoMemory;
sl@0
  1148
		}
sl@0
  1149
sl@0
  1150
	if (_stricmp(mpath, "%temp%") == 0)
sl@0
  1151
		{
sl@0
  1152
		DWORD len=GetTempPathA(KMaxFileName, path);
sl@0
  1153
		if (len > 0 && len < KMaxFileName)
sl@0
  1154
			return iProperties.Replace("EmulatorMediaPath", path) ? KErrNone : KErrNoMemory;
sl@0
  1155
		}
sl@0
  1156
sl@0
  1157
	return KErrNone;
sl@0
  1158
	}
sl@0
  1159
sl@0
  1160
const char* Wins::EmulatorMediaPath()
sl@0
  1161
	{
sl@0
  1162
	const char* mpath = NULL;
sl@0
  1163
	iProperties.GetString("EmulatorMediaPath", mpath);
sl@0
  1164
	return mpath;
sl@0
  1165
	}
sl@0
  1166
sl@0
  1167
TInt Wins::SetupDrive(int aDrive, const char* aPath)
sl@0
  1168
//
sl@0
  1169
// set up emulated drives
sl@0
  1170
//
sl@0
  1171
	{
sl@0
  1172
sl@0
  1173
	// Z drive can't end in anything but "Z\\", since we chop this off and use
sl@0
  1174
	// the resulting directory to find filenames with no drive specified in
sl@0
  1175
	// MapFileName() below.
sl@0
  1176
	aDrive = tolower(aDrive);
sl@0
  1177
	if (aDrive=='z')
sl@0
  1178
		{
sl@0
  1179
		const char* end = aPath + strlen(aPath);
sl@0
  1180
		if (_stricmp(end-2,"\\z") != 0 && _stricmp(end-3,"\\z\\") != 0)
sl@0
  1181
			return KErrArgument;
sl@0
  1182
		}
sl@0
  1183
sl@0
  1184
	char prop[] = "_epoc_drive_?";
sl@0
  1185
	*strchr(prop, '?') = char(aDrive);
sl@0
  1186
sl@0
  1187
sl@0
  1188
    // If the path begins with the keyword %epocroot%, replace this with EPOCROOT
sl@0
  1189
    if (_strnicmp(aPath, "%epocroot%", 10) == 0)
sl@0
  1190
        {
sl@0
  1191
		aPath += 10; // skip "%epocroot%"
sl@0
  1192
sl@0
  1193
        const char* eRoot;
sl@0
  1194
        TInt r = iProperties.GetString("EpocRoot", eRoot);
sl@0
  1195
        if (r != KErrNone)
sl@0
  1196
            return r;
sl@0
  1197
sl@0
  1198
		int rootSize = strlen(eRoot);
sl@0
  1199
		int pathSize = strlen(aPath);
sl@0
  1200
		if(rootSize+pathSize>MAX_PATH)
sl@0
  1201
			return KErrArgument;
sl@0
  1202
sl@0
  1203
        char fullPath[MAX_PATH+1];
sl@0
  1204
		memcpy(fullPath,eRoot,rootSize);
sl@0
  1205
		memcpy(fullPath+rootSize,aPath,pathSize+1); // +1 to get the terminating NULL char
sl@0
  1206
sl@0
  1207
        return iProperties.Replace(prop, fullPath) ? KErrNone : KErrNoMemory;
sl@0
  1208
sl@0
  1209
        }
sl@0
  1210
    else
sl@0
  1211
        // otherwise, aPath is fully qualified path name. Use that.
sl@0
  1212
        return iProperties.Replace(prop, aPath) ? KErrNone : KErrNoMemory;
sl@0
  1213
  
sl@0
  1214
	}
sl@0
  1215
sl@0
  1216
TInt Wins::MapDrive(int aDrive, TDes& aBuffer) const
sl@0
  1217
//
sl@0
  1218
// Map aDrive to a path given by environment variables or defaults
sl@0
  1219
// Use this function only in WINS builds
sl@0
  1220
//
sl@0
  1221
	{
sl@0
  1222
	char drive[KMaxFileName + 1];
sl@0
  1223
	char prop[] = "_epoc_drive_?";
sl@0
  1224
	*strchr(prop, '?') = char(tolower(aDrive));
sl@0
  1225
sl@0
  1226
	TInt len;
sl@0
  1227
	const char* val;
sl@0
  1228
	if (iProperties.GetString(prop, val) == KErrNone)
sl@0
  1229
		{
sl@0
  1230
		len = strlen(val);
sl@0
  1231
		if (len > KMaxFileName)
sl@0
  1232
			return KErrArgument;
sl@0
  1233
		strcpy(drive, val);
sl@0
  1234
		}
sl@0
  1235
	else
sl@0
  1236
		{
sl@0
  1237
		// not in properties, so check environment
sl@0
  1238
		len = GetEnvironmentVariableA(prop, drive, KMaxFileName + 1);
sl@0
  1239
		if (len > KMaxFileName)
sl@0
  1240
			return KErrArgument;
sl@0
  1241
		}
sl@0
  1242
	while (len > 0 && isspace(drive[len-1]))
sl@0
  1243
		--len;
sl@0
  1244
	if (len == 0)
sl@0
  1245
		return KErrNotFound;
sl@0
  1246
	if (drive[len-1] != '\\') // add trailing backslash
sl@0
  1247
		drive[len++] = '\\';
sl@0
  1248
	if (drive[0] == '\\')
sl@0
  1249
		{
sl@0
  1250
		// put in the emulator drive
sl@0
  1251
		TInt r = iProperties.GetString("EmulatorPath", val);
sl@0
  1252
		if (r != KErrNone)
sl@0
  1253
			return r;
sl@0
  1254
sl@0
  1255
		memmove(drive + 2, drive, len);
sl@0
  1256
		drive[0] = val[0];
sl@0
  1257
		drive[1] = ':';
sl@0
  1258
		len += 2;
sl@0
  1259
		}
sl@0
  1260
	else if (len < 3 || drive[1] != ':' || drive[2] != '\\')
sl@0
  1261
		return KErrArgument;
sl@0
  1262
#ifdef _UNICODE
sl@0
  1263
	TUint16* aBufPtr = (TUint16*)aBuffer.Ptr();
sl@0
  1264
	const TText* drv = (const TText*)drive;
sl@0
  1265
	for(int index=0;index<len;index++)
sl@0
  1266
		*aBufPtr++ = (TUint16)*drv++;
sl@0
  1267
	aBuffer.SetLength(len<<1);
sl@0
  1268
#else
sl@0
  1269
	aBuffer.Copy(TPtrC8((const TText8*)drive,len));
sl@0
  1270
#endif
sl@0
  1271
	return KErrNone;
sl@0
  1272
	}
sl@0
  1273
sl@0
  1274
TInt Wins::MapFilename(const TDesC& aFilename, TDes& aBuffer) const
sl@0
  1275
//
sl@0
  1276
// Map aFileName to real windows directory - aFileName must be a full filepath
sl@0
  1277
//
sl@0
  1278
	{
sl@0
  1279
sl@0
  1280
	// if the filename does not have a drive specified then don't imagine
sl@0
  1281
	// it describes an Epoc filepath
sl@0
  1282
	// Assume it's a subdirectory/file of the file containing the emulated Z drive
sl@0
  1283
	TInt offset;
sl@0
  1284
	if (aFilename.Length() < 4 || aFilename[2] != ':')
sl@0
  1285
		{
sl@0
  1286
		TInt r = MapDrive('z', aBuffer);
sl@0
  1287
		if (r)
sl@0
  1288
			return(r);
sl@0
  1289
		aBuffer.SetLength(aBuffer.Length()-4);	// chop "Z\\"
sl@0
  1290
		offset = aFilename[0] == '\\' ? 1 : 0; // remove the guaranteed backslash
sl@0
  1291
		}
sl@0
  1292
	else
sl@0
  1293
		{
sl@0
  1294
		TInt r = MapDrive(aFilename[0], aBuffer);
sl@0
  1295
		if (r)
sl@0
  1296
			return(r);
sl@0
  1297
		if (aFilename.Length() >= 6 && aFilename[4] == '\\')
sl@0
  1298
			offset = 3;
sl@0
  1299
		else
sl@0
  1300
			offset = 2;
sl@0
  1301
		}
sl@0
  1302
#ifdef _UNICODE
sl@0
  1303
	offset = offset<<1;
sl@0
  1304
	TUint8* ptrFilename = (TUint8*)aFilename.Ptr() + offset;
sl@0
  1305
	TUint8* ptrBuffer = (TUint8*)aBuffer.Ptr()+aBuffer.Length();
sl@0
  1306
	if (aBuffer.MaxLength()<aBuffer.Length()+aFilename.Length()-offset+1)
sl@0
  1307
		return KErrBadName;
sl@0
  1308
sl@0
  1309
	memcpy(ptrBuffer, ptrFilename, aFilename.Length()-offset);
sl@0
  1310
	aBuffer.SetLength(aBuffer.Length()+aFilename.Length()-offset);
sl@0
  1311
#else
sl@0
  1312
	TPtrC name(aFilename.Mid(offset));
sl@0
  1313
	if (aBuffer.MaxLength()<aBuffer.Length()+name.Length()+1)
sl@0
  1314
		return KErrBadName;
sl@0
  1315
	aBuffer.Append(name);
sl@0
  1316
#endif
sl@0
  1317
	return KErrNone;
sl@0
  1318
	}
sl@0
  1319
sl@0
  1320
sl@0
  1321
//table of the property names which can be used in multiple configurations
sl@0
  1322
const char * KConfigSpecificProperties[] =
sl@0
  1323
	{
sl@0
  1324
	"ScreenWidth",
sl@0
  1325
	"ScreenHeight",
sl@0
  1326
	"PhysicalScreenWidth",
sl@0
  1327
	"PhysicalScreenHeight",
sl@0
  1328
	"ScreenOffsetX",
sl@0
  1329
	"ScreenOffsetY",
sl@0
  1330
	"LedSize",
sl@0
  1331
	"LedArrangeVertically",
sl@0
  1332
	"LedArrangeHorizontally",
sl@0
  1333
	"LedOffsetX",
sl@0
  1334
	"LedOffsetY",
sl@0
  1335
	"LedGap",
sl@0
  1336
	"PointerType",
sl@0
  1337
	"ColorDepth",
sl@0
  1338
	"KeyMap",
sl@0
  1339
	"DrawVirtualKeys",
sl@0
  1340
	"VirtualKeyColor",
sl@0
  1341
	"VirtualKey",
sl@0
  1342
	"MouseTarget",
sl@0
  1343
	"FasciaBitmap",
sl@0
  1344
	"DigitizerOffsetX",
sl@0
  1345
	"DigitizerOffsetY",
sl@0
  1346
	"DigitizerWidth",
sl@0
  1347
	"DigitizerHeight",
sl@0
  1348
	"DisableDigitizer",
sl@0
  1349
	"DefineKeyName",
sl@0
  1350
	"WindowTitle",
sl@0
  1351
	"NoVersionInfo",
sl@0
  1352
	"OnActivation",
sl@0
  1353
	"EmulatorControl",
sl@0
  1354
	"EmulatorControlHotKey",
sl@0
  1355
	"CompositionBuffers",
sl@0
  1356
	"RefreshRateHz",
sl@0
  1357
	};
sl@0
  1358
sl@0
  1359
const char * KScreenSpecificProperties[] =
sl@0
  1360
 	{
sl@0
  1361
 	"ScreenWidth",
sl@0
  1362
 	"ScreenHeight",
sl@0
  1363
 	"PhysicalScreenWidth",
sl@0
  1364
 	"PhysicalScreenHeight",
sl@0
  1365
 	"ScreenOffsetX",
sl@0
  1366
 	"ScreenOffsetY",
sl@0
  1367
 	"ColorDepth",
sl@0
  1368
	"FasciaBitmap",
sl@0
  1369
	"CompositionBuffers",
sl@0
  1370
	"RefreshRateHz",
sl@0
  1371
 	};
sl@0
  1372
sl@0
  1373
sl@0
  1374
TBool Wins::ConfigSpecificProperty(const char * aProperty)
sl@0
  1375
	{
sl@0
  1376
	TInt x;
sl@0
  1377
	TInt count = sizeof(KConfigSpecificProperties) / sizeof(char *);
sl@0
  1378
	for (x = 0; x < count; ++x)
sl@0
  1379
		if (_stricmp(aProperty, KConfigSpecificProperties[x]) == 0)	return ETrue;
sl@0
  1380
	return EFalse;
sl@0
  1381
	}
sl@0
  1382
sl@0
  1383
TBool Wins::ScreenSpecificProperty(const char * aProperty)
sl@0
  1384
	{
sl@0
  1385
	TInt x;
sl@0
  1386
	TInt count = sizeof(KScreenSpecificProperties) / sizeof(char *);
sl@0
  1387
	for (x = 0; x < count; ++x)
sl@0
  1388
		if (_stricmp(aProperty, KScreenSpecificProperties[x]) == 0)	return ETrue;
sl@0
  1389
	return EFalse;
sl@0
  1390
	}