commands/focus/focus.cpp
changeset 0 7f656887cf89
child 97 562156073bab
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // focus.cpp
       
     2 // 
       
     3 // Copyright (c) 2008 - 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 <w32std.h>
       
    14 #include <apgwgnam.h>
       
    15 #include <fshell/ioutils.h>
       
    16 
       
    17 const TInt KPriorityNeverAtFront = -999;
       
    18 
       
    19 using namespace IoUtils;
       
    20 
       
    21 class CCmdFocus : public CCommandBase
       
    22 	{
       
    23 public:
       
    24 	static CCommandBase* NewLC();
       
    25 	~CCmdFocus();
       
    26 private:
       
    27 	CCmdFocus();
       
    28 	void PrintDetailsL();
       
    29 	void FocusWindowGroupL(TInt aWindowGroupId);
       
    30 private: // From CCommandBase.
       
    31 	virtual const TDesC& Name() const;
       
    32 	virtual void DoRunL();
       
    33 	virtual void OptionsL(RCommandOptionList& aOptions);
       
    34 private:
       
    35 	RWsSession iWsSession;
       
    36 	RWindowGroup iWindowGroup;
       
    37 	TInt iWindowGroupId;
       
    38 	HBufC* iWindowGroupPattern;
       
    39 	TBool iFollow;
       
    40 	TBool iVerbose;
       
    41 	};
       
    42 
       
    43 
       
    44 CCommandBase* CCmdFocus::NewLC()
       
    45 	{
       
    46 	CCmdFocus* self = new(ELeave) CCmdFocus();
       
    47 	CleanupStack::PushL(self);
       
    48 	self->BaseConstructL();
       
    49 	return self;
       
    50 	}
       
    51 
       
    52 CCmdFocus::~CCmdFocus()
       
    53 	{
       
    54 	iWindowGroup.Close();
       
    55 	iWsSession.Close();
       
    56 	delete iWindowGroupPattern;
       
    57 	}
       
    58 
       
    59 CCmdFocus::CCmdFocus()
       
    60 	{
       
    61 	}
       
    62 
       
    63 const TDesC& CCmdFocus::Name() const
       
    64 	{
       
    65 	_LIT(KName, "focus");	
       
    66 	return KName;
       
    67 	}
       
    68 
       
    69 void CCmdFocus::DoRunL()
       
    70 	{
       
    71 	LeaveIfErr(iWsSession.Connect(), _L("Couldn't connect to windowserver"));
       
    72 
       
    73 	if (iOptions.IsPresent(&iWindowGroupId))
       
    74 		{
       
    75 		FocusWindowGroupL(iWindowGroupId);
       
    76 		}
       
    77 	else if (iWindowGroupPattern)
       
    78 		{
       
    79 		TInt id = 0;
       
    80 		id = iWsSession.FindWindowGroupIdentifier(id, *iWindowGroupPattern);
       
    81 		if (id >= 0)
       
    82 			{
       
    83 			FocusWindowGroupL(id);
       
    84 			}
       
    85 		}
       
    86 	else
       
    87 		{
       
    88 		PrintDetailsL();
       
    89 		if (iFollow)
       
    90 			{
       
    91 			iWindowGroup = RWindowGroup(iWsSession);
       
    92 			User::LeaveIfError(iWindowGroup.Construct((TUint32)this,EFalse));
       
    93 			iWindowGroup.SetOrdinalPosition(0, KPriorityNeverAtFront);
       
    94 			iWindowGroup.EnableFocusChangeEvents();
       
    95 
       
    96 			TRequestStatus status;
       
    97 			FOREVER
       
    98 				{
       
    99 				iWsSession.EventReady(&status);
       
   100 				User::WaitForRequest(status);
       
   101 				User::LeaveIfError(status.Int());
       
   102 				TWsEvent event;
       
   103 				iWsSession.GetEvent(event);
       
   104 				if (event.Type() == EEventFocusGroupChanged) 
       
   105 					{
       
   106 					PrintDetailsL();
       
   107 					}
       
   108 				}
       
   109 			}
       
   110 		}
       
   111 	}
       
   112 
       
   113 void CCmdFocus::PrintDetailsL()
       
   114 	{
       
   115 	TInt focusedWgId = iWsSession.GetFocusWindowGroup();
       
   116 	CApaWindowGroupName* wgn = CApaWindowGroupName::NewLC(iWsSession, focusedWgId);
       
   117 	_LIT(KUnknown, "Un-named");
       
   118 	TPtrC caption(wgn->Caption());
       
   119 	if (caption.Length() == 0)
       
   120 		{
       
   121 		caption.Set(KUnknown);
       
   122 		}
       
   123 	Printf(_L("Focused window group: %S\r\n"), &caption);
       
   124 	CleanupStack::PopAndDestroy(wgn);
       
   125 
       
   126 	if (iVerbose)
       
   127 		{
       
   128 		TThreadId focusedThreadId;
       
   129 		User::LeaveIfError(iWsSession.GetWindowGroupClientThreadId(focusedWgId, focusedThreadId));
       
   130 		RThread thread;
       
   131 		User::LeaveIfError(thread.Open(focusedThreadId));
       
   132 		TFullName name(thread.FullName());
       
   133 		thread.Close();
       
   134 		Printf(_L("  Window group id:    %u\r\n"), focusedWgId);
       
   135 		Printf(_L("  Owning thread:      %S\r\n"), &name);
       
   136 		Printf(_L("  Owning thread id:   %u\r\n"), (TUint32)focusedThreadId);
       
   137 		Printf(_L("  Tick count:         %u\r\n"), User::NTickCount());
       
   138 		}
       
   139 	}
       
   140 
       
   141 void CCmdFocus::FocusWindowGroupL(TInt aWindowGroupId)
       
   142 	{
       
   143 	LeaveIfErr(iWsSession.SetWindowGroupOrdinalPosition(aWindowGroupId, 0), _L("Unable to set window group %d to ordinal position zero"), aWindowGroupId);
       
   144 	}
       
   145 
       
   146 void CCmdFocus::OptionsL(RCommandOptionList& aOptions)
       
   147 	{
       
   148 	_LIT(KOptWindowGroupId, "id");
       
   149 	aOptions.AppendIntL(iWindowGroupId, KOptWindowGroupId);
       
   150 	_LIT(KOptWindowGroupPattern, "match");
       
   151 	aOptions.AppendStringL(iWindowGroupPattern, KOptWindowGroupPattern);
       
   152 	_LIT(KOptFollow, "follow");
       
   153 	aOptions.AppendBoolL(iFollow, KOptFollow);
       
   154 	_LIT(KOptVerbose, "verbose");
       
   155 	aOptions.AppendBoolL(iVerbose, KOptVerbose);
       
   156 	}
       
   157 
       
   158 
       
   159 EXE_BOILER_PLATE(CCmdFocus)
       
   160