plugins/consoles/consoleproxy/src/client.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // client.cpp
       
     2 // 
       
     3 // Copyright (c) 2009 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <consoleproxy.h>
       
    14 #include "server.h"
       
    15 
       
    16 //______________________________________________________________________________
       
    17 //						RConsoleProxy
       
    18 static TInt StartServer(const TDesC& aServerName, const TDesC& aServerArgs)
       
    19 	{
       
    20 	TPtrC processName(aServerName);
       
    21 	if (processName.Length() && processName[0] == '!')
       
    22 		{
       
    23 		processName.Set(processName.Mid(1));
       
    24 		}
       
    25 	RProcess server;
       
    26 	TInt r = server.Create(processName, aServerArgs);
       
    27 	if (r!=KErrNone) return r;
       
    28 	TRequestStatus stat;
       
    29 	server.Rendezvous(stat);
       
    30 	if (stat != KRequestPending)
       
    31 		{
       
    32 		server.Kill(0);
       
    33 		}
       
    34 	else
       
    35 		{
       
    36 		server.Resume();
       
    37 		}
       
    38 	User::WaitForRequest(stat);		// wait for start or death
       
    39 	r = (server.ExitType() == EExitPanic) ? KErrGeneral : stat.Int();
       
    40 	server.Close();
       
    41 	return r;
       
    42 	}
       
    43 	
       
    44 EXPORT_C TInt RConsoleProxy::Connect(const TDesC& aServerName, const TDesC& aServerArgs)
       
    45 	{
       
    46 	TInt retry = 2;
       
    47 	for (;;)
       
    48 		{
       
    49 		TInt r = CreateSession(aServerName, TVersion(0,0,0));
       
    50 		if ((r != KErrNotFound) && (r != KErrServerTerminated))
       
    51 			{
       
    52 			return r;
       
    53 			}
       
    54 		if (--retry == 0)
       
    55 			{
       
    56 			return r;
       
    57 			}
       
    58 		r = StartServer(aServerName, aServerArgs);
       
    59 		if ((r != KErrNone) && (r != KErrAlreadyExists))
       
    60 			{
       
    61 			return r;
       
    62 			}
       
    63 		}
       
    64 	}
       
    65 
       
    66 EXPORT_C TInt RConsoleProxy::Connect(RServer2 aServer)
       
    67 	{
       
    68 	return CreateSession(aServer, TVersion(0,0,0));
       
    69 	}
       
    70 	
       
    71 EXPORT_C TInt RConsoleProxy::Connect(TConsoleCreateFunction aConsoleCreate, const TDesC& aThreadNameBase, TInt aStackSize, TInt aHeapMinSize, TInt aHeapMaxSize, RServer2& aServer, RThread& aServerThread)
       
    72 	{
       
    73 	TConsoleProxyServerNewLParams newLParams(KNullDesC, CActive::EPriorityStandard, aConsoleCreate);
       
    74 	TServerParams params;
       
    75 	params.iServerNewL = &CConsoleProxyServer::NewL;
       
    76 	params.iServerParams = &newLParams;
       
    77 	return DoConnect(&params, aThreadNameBase, aStackSize, aHeapMinSize, aHeapMaxSize, aServer, aServerThread);
       
    78 	}
       
    79 	
       
    80 EXPORT_C TInt RConsoleProxy::Connect(TServerNewL aServerConstructor, TAny* aServerParams, const TDesC& aThreadNameBase, TInt aStackSize, TInt aHeapMinSize, TInt aHeapMaxSize, RServer2& aServer, RThread& aServerThread)
       
    81 	{
       
    82 	TServerParams params;
       
    83 	params.iServerNewL = aServerConstructor;
       
    84 	params.iServerParams = aServerParams;
       
    85 	return DoConnect(&params, aThreadNameBase, aStackSize, aHeapMinSize, aHeapMaxSize, aServer, aServerThread);
       
    86 	}
       
    87 	
       
    88 TInt RConsoleProxy::DoConnect(TServerParams* aParams, const TDesC& aThreadNameBase, TInt aStackSize, TInt aHeapMinSize, TInt aHeapMaxSize, RServer2& aServer, RThread& aServerThread)
       
    89 	{
       
    90 	TName threadName;
       
    91 	RThread server;
       
    92 	TInt threadId = 0;
       
    93 	_LIT(KThreadIdFmt, "%08x");
       
    94 	TInt err;
       
    95 	do
       
    96 		{
       
    97 		threadName = aThreadNameBase.Left(threadName.MaxLength()-8);
       
    98 		threadName.AppendFormat(KThreadIdFmt, threadId);
       
    99 		err = server.Create(threadName, &ServerThreadFunction, aStackSize, aHeapMinSize, aHeapMaxSize, aParams);
       
   100 		++threadId;
       
   101 		} while (err==KErrAlreadyExists);
       
   102 	if (err!=KErrNone) return err;
       
   103 	
       
   104 	TRequestStatus rendezvous;
       
   105 	server.Rendezvous(rendezvous);
       
   106 	if (rendezvous == KRequestPending)
       
   107 		{
       
   108 		server.Resume();
       
   109 		}
       
   110 	User::WaitForRequest(rendezvous);
       
   111 	err = rendezvous.Int();
       
   112 	if (server.ExitType() != EExitPending && err >= 0) err = KErrDied;
       
   113 	if (err==KErrNone)
       
   114 		{
       
   115 		err = Connect(aParams->iServer);
       
   116 		}
       
   117 	aServer = aParams->iServer;
       
   118 	aServerThread = server;
       
   119 	return err;
       
   120 	}
       
   121 
       
   122 
       
   123 EXPORT_C TInt RConsoleProxy::Server(RServer2& aServer)
       
   124 	{
       
   125 	TInt r = SendReceive(EGetServer);
       
   126 	return aServer.SetReturnedHandle(r);
       
   127 	}
       
   128 
       
   129 EXPORT_C void RConsoleProxy::Create(const TDesC& aConsoleTitle, TSize aSize, TRequestStatus& aStatus)
       
   130 	{
       
   131 	SendReceive(ECreate, TIpcArgs(&aConsoleTitle, aSize.iWidth, aSize.iHeight), aStatus);
       
   132 	}
       
   133 
       
   134 EXPORT_C void RConsoleProxy::Read(TRequestStatus& aStatus)
       
   135 	{
       
   136 	SendReceive(ERead, aStatus);
       
   137 	}
       
   138 	
       
   139 EXPORT_C void RConsoleProxy::Read(TPckg<TKeyCode>& aKeyCode, TPckg<TUint>& aKeyModifiers, TRequestStatus& aStatus)
       
   140 	{
       
   141 	SendReceive(EReadKey, TIpcArgs(&aKeyCode, &aKeyModifiers), aStatus);
       
   142 	}
       
   143 
       
   144 EXPORT_C void RConsoleProxy::ReadCancel()
       
   145 	{
       
   146 	Send(EReadCancel);
       
   147 	}
       
   148 
       
   149 EXPORT_C void RConsoleProxy::Write(const TDesC& aDescriptor, TRequestStatus& aStatus)
       
   150 	{
       
   151 	SendReceive(EWrite, TIpcArgs(&aDescriptor), aStatus);
       
   152 	}
       
   153 
       
   154 EXPORT_C void RConsoleProxy::CursorPos(TPckg<TPoint>& aPos, TRequestStatus& aStatus) const
       
   155 	{
       
   156 	SendReceive(EGetCursorPos, TIpcArgs(&aPos), aStatus);
       
   157 	}
       
   158 
       
   159 EXPORT_C void RConsoleProxy::SetCursorPosAbs(const TPoint& aPoint, TRequestStatus& aStatus)
       
   160 	{
       
   161 	SendReceive(ESetCursorPosAbs, TIpcArgs(aPoint.iX, aPoint.iY), aStatus);
       
   162 	}
       
   163 
       
   164 EXPORT_C void RConsoleProxy::SetCursorPosRel(const TPoint& aPoint, TRequestStatus& aStatus)
       
   165 	{
       
   166 	SendReceive(ESetCursorPosRel, TIpcArgs(aPoint.iX, aPoint.iY), aStatus);
       
   167 	}
       
   168 
       
   169 EXPORT_C void RConsoleProxy::SetCursorHeight(TInt aPercentage, TRequestStatus& aStatus)
       
   170 	{
       
   171 	SendReceive(ESetCursorHeight, TIpcArgs(aPercentage), aStatus);
       
   172 	}
       
   173 
       
   174 EXPORT_C void RConsoleProxy::SetTitle(const TDesC& aTitle, TRequestStatus& aStatus)
       
   175 	{
       
   176 	SendReceive(ESetTitle, TIpcArgs(&aTitle), aStatus);
       
   177 	}
       
   178 
       
   179 EXPORT_C void RConsoleProxy::ClearScreen(TRequestStatus& aStatus)
       
   180 	{
       
   181 	SendReceive(EClearScreen, aStatus);
       
   182 	}
       
   183 
       
   184 EXPORT_C void RConsoleProxy::ClearToEndOfLine(TRequestStatus& aStatus)
       
   185 	{
       
   186 	SendReceive(EClearToEndOfLine, aStatus);
       
   187 	}
       
   188 
       
   189 EXPORT_C void RConsoleProxy::GetScreenSize(TPckg<TSize>& aSize, TRequestStatus& aStatus) const
       
   190 	{
       
   191 	SendReceive(EGetScreenSize, TIpcArgs(&aSize), aStatus);
       
   192 	}
       
   193 
       
   194 EXPORT_C void RConsoleProxy::SetAttributes(TUint aAttributes, ConsoleAttributes::TColor aForegroundColor, ConsoleAttributes::TColor aBackgroundColor, TRequestStatus& aStatus)
       
   195 	{
       
   196 	SendReceive(ESetAttributes, TIpcArgs(aAttributes, aForegroundColor, aBackgroundColor), aStatus);
       
   197 	}
       
   198 	
       
   199 EXPORT_C TInt RConsoleProxy::IsConstructed(TBool& aLazy)
       
   200 	{
       
   201 	TPckg<TBool> constructed(aLazy);
       
   202 	return SendReceive(EIsConstructed, TIpcArgs(&constructed));
       
   203 	}
       
   204 
       
   205 EXPORT_C TInt RConsoleProxy::GetKeyCode(TKeyCode& aCode) const
       
   206 	{
       
   207 	TPckg<TKeyCode> kc(aCode);
       
   208 	return SendReceive(EGetKeyCode, TIpcArgs(&(kc)));
       
   209 	}
       
   210 
       
   211 EXPORT_C TInt RConsoleProxy::GetKeyModifiers(TUint& aModifiers) const
       
   212 	{
       
   213 	TPckg<TUint> mod(aModifiers);
       
   214 	return SendReceive(EGetKeyCode, TIpcArgs(&mod));
       
   215 	}
       
   216 
       
   217 EXPORT_C TInt RConsoleProxy::Create(const TDesC& aConsoleTitle, TSize aSize)
       
   218 	{
       
   219 	TRequestStatus stat;
       
   220 	Create(aConsoleTitle, aSize, stat);
       
   221 	User::WaitForRequest(stat);
       
   222 	return stat.Int();
       
   223 	}
       
   224 
       
   225 EXPORT_C TInt RConsoleProxy::Write(const TDesC& aDescriptor)
       
   226 	{
       
   227 	TRequestStatus stat;
       
   228 	Write(aDescriptor, stat);
       
   229 	User::WaitForRequest(stat);
       
   230 	return stat.Int();
       
   231 	}
       
   232 
       
   233 EXPORT_C TInt RConsoleProxy::CursorPos(TPoint& aPos) const
       
   234 	{
       
   235 	TRequestStatus stat;
       
   236 	TPckg<TPoint> pos(aPos);
       
   237 	CursorPos(pos, stat);
       
   238 	User::WaitForRequest(stat);
       
   239 	return stat.Int();
       
   240 	}
       
   241 
       
   242 EXPORT_C TInt RConsoleProxy::SetCursorPosAbs(const TPoint& aPoint)
       
   243 	{
       
   244 	TRequestStatus stat;
       
   245 	SetCursorPosAbs(aPoint, stat);
       
   246 	User::WaitForRequest(stat);
       
   247 	return stat.Int();
       
   248 	}
       
   249 
       
   250 EXPORT_C TInt RConsoleProxy::SetCursorPosRel(const TPoint& aPoint)
       
   251 	{
       
   252 	TRequestStatus stat;
       
   253 	SetCursorPosRel(aPoint, stat);
       
   254 	User::WaitForRequest(stat);
       
   255 	return stat.Int();
       
   256 	}
       
   257 
       
   258 EXPORT_C TInt RConsoleProxy::SetCursorHeight(TInt aPercentage)
       
   259 	{
       
   260 	TRequestStatus stat;
       
   261 	SetCursorHeight(aPercentage, stat);
       
   262 	User::WaitForRequest(stat);
       
   263 	return stat.Int();
       
   264 	}
       
   265 
       
   266 EXPORT_C TInt RConsoleProxy::SetTitle(const TDesC& aTitle)
       
   267 	{
       
   268 	TRequestStatus stat;
       
   269 	SetTitle(aTitle, stat);
       
   270 	User::WaitForRequest(stat);
       
   271 	return stat.Int();
       
   272 	}
       
   273 
       
   274 EXPORT_C TInt RConsoleProxy::ClearScreen()
       
   275 	{
       
   276 	TRequestStatus stat;
       
   277 	ClearScreen(stat);
       
   278 	User::WaitForRequest(stat);
       
   279 	return stat.Int();
       
   280 	}
       
   281 
       
   282 EXPORT_C TInt RConsoleProxy::ClearToEndOfLine()
       
   283 	{
       
   284 	TRequestStatus stat;
       
   285 	ClearToEndOfLine(stat);
       
   286 	User::WaitForRequest(stat);
       
   287 	return stat.Int();
       
   288 	}
       
   289 
       
   290 EXPORT_C TInt RConsoleProxy::GetScreenSize(TSize& aSize) const
       
   291 	{
       
   292 	TRequestStatus stat;
       
   293 	TPckg<TSize> size(aSize);
       
   294 	GetScreenSize(size, stat);
       
   295 	User::WaitForRequest(stat);
       
   296 	return stat.Int();
       
   297 	}
       
   298 
       
   299 //______________________________________________________________________________
       
   300 //						CConsoleProxy
       
   301 EXPORT_C CConsoleProxy* CConsoleProxy::NewL(const RConsoleProxy& aProxySession)
       
   302 	{
       
   303 	CConsoleProxy* self = new(ELeave)CConsoleProxy();
       
   304 	CleanupStack::PushL(self);
       
   305 	self->ConstructL(aProxySession);
       
   306 	CleanupStack::Pop(self);
       
   307 	return self;
       
   308 	}
       
   309 
       
   310 EXPORT_C CConsoleProxy::CConsoleProxy()
       
   311 	: iKeyCodePckg(iKeyCode), iKeyModifiersPckg(iKeyModifiers)
       
   312 	{
       
   313 	}
       
   314 
       
   315 EXPORT_C TInt CConsoleProxy::Extension_(TUint aExtensionId, TAny*& a0, TAny* a1)
       
   316 	{
       
   317 	if (aExtensionId == ConsoleAttributes::KSetConsoleAttributesExtension)
       
   318 		{
       
   319 		TRequestStatus status;
       
   320 		ConsoleAttributes::TAttributes* attributes = (ConsoleAttributes::TAttributes*)a1;
       
   321 		iConsole.SetAttributes(attributes->iAttributes, attributes->iForegroundColor, attributes->iBackgroundColor, status);
       
   322 		User::WaitForRequest(status);
       
   323 		return status.Int();
       
   324 		}
       
   325 	if (aExtensionId == LazyConsole::KLazyConsoleExtension)
       
   326 		{
       
   327 		TBool* constructed = (TBool*)a1;
       
   328 		return iConsole.IsConstructed(*constructed);
       
   329 		}
       
   330 	return CConsoleBase::Extension_(aExtensionId, a0, a1);
       
   331 	}
       
   332 	
       
   333 EXPORT_C void CConsoleProxy::ConstructL(const RConsoleProxy& aProxySession)
       
   334 	{
       
   335 	RConsoleProxy cons = aProxySession;
       
   336 	User::LeaveIfError(cons.Duplicate(RThread(), EOwnerThread));
       
   337 	iConsole = cons;
       
   338 	}
       
   339 
       
   340 EXPORT_C CConsoleProxy::~CConsoleProxy()
       
   341 	{
       
   342 	iConsole.Close();
       
   343 	}
       
   344 
       
   345 EXPORT_C TInt CConsoleProxy::Create(const TDesC &aTitle, TSize aSize)
       
   346 	{
       
   347 	return iConsole.Create(aTitle, aSize);
       
   348 	}
       
   349 
       
   350 EXPORT_C void CConsoleProxy::Read(TRequestStatus &aStatus)
       
   351 	{
       
   352 	iConsole.Read(iKeyCodePckg, iKeyModifiersPckg, aStatus);
       
   353 	}
       
   354 
       
   355 EXPORT_C void CConsoleProxy::ReadCancel()
       
   356 	{
       
   357 	iConsole.ReadCancel();
       
   358 	}
       
   359 
       
   360 EXPORT_C void CConsoleProxy::Write(const TDesC &aDes)
       
   361 	{
       
   362 	iConsole.Write(aDes);
       
   363 	}
       
   364 
       
   365 EXPORT_C TPoint CConsoleProxy::CursorPos() const
       
   366 	{
       
   367 	TPoint pos;
       
   368 	iConsole.CursorPos(pos);
       
   369 	return pos;
       
   370 	}
       
   371 
       
   372 
       
   373 EXPORT_C void CConsoleProxy::SetCursorPosAbs(const TPoint &aPoint)
       
   374 	{
       
   375 	iConsole.SetCursorPosAbs(aPoint);
       
   376 	}
       
   377 
       
   378 EXPORT_C void CConsoleProxy::SetCursorPosRel(const TPoint &aPoint)
       
   379 	{
       
   380 	iConsole.SetCursorPosRel(aPoint);
       
   381 	}
       
   382 
       
   383 EXPORT_C void CConsoleProxy::SetCursorHeight(TInt aPercentage)
       
   384 	{
       
   385 	iConsole.SetCursorHeight(aPercentage);
       
   386 	}
       
   387 
       
   388 EXPORT_C void CConsoleProxy::SetTitle(const TDesC &aTitle)
       
   389 	{
       
   390 	iConsole.SetTitle(aTitle);
       
   391 	}
       
   392 
       
   393 EXPORT_C void CConsoleProxy::ClearScreen()
       
   394 	{
       
   395 	iConsole.ClearScreen();
       
   396 	}
       
   397 
       
   398 EXPORT_C void CConsoleProxy::ClearToEndOfLine()
       
   399 	{
       
   400 	iConsole.ClearToEndOfLine();
       
   401 	}
       
   402 
       
   403 EXPORT_C TSize CConsoleProxy::ScreenSize() const
       
   404 	{
       
   405 	TSize size;
       
   406 	iConsole.GetScreenSize(size);
       
   407 	return size;
       
   408 	}
       
   409 
       
   410 EXPORT_C TKeyCode CConsoleProxy::KeyCode() const
       
   411 	{
       
   412 	return iKeyCode;
       
   413 	}
       
   414 
       
   415 EXPORT_C TUint CConsoleProxy::KeyModifiers() const
       
   416 	{
       
   417 	return iKeyModifiers;
       
   418 	}