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