loadgen/src/loadgen_pointerevent.cpp
changeset 0 d6fe6244b863
equal deleted inserted replaced
-1:000000000000 0:d6fe6244b863
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "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 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "loadgen_pointerevent.h"
       
    21 #include "loadgen_model.h"
       
    22 #include "loadgen.hrh"
       
    23 #include <loadgen.rsg>
       
    24 
       
    25 #include <e32math.h>
       
    26 
       
    27 _LIT(KThreadName, "PointerEvent %d");
       
    28 
       
    29 const TInt KDefaultStart = 50;
       
    30 const TInt KDefaultPeriod = 5000000;
       
    31     
       
    32 // ===================================== MEMBER FUNCTIONS =====================================
       
    33 
       
    34 CPointerEvent* CPointerEvent::NewL(TPointerEventAttributes& aAttributes, TInt aReferenceNumber)
       
    35     {
       
    36     CPointerEvent* self = new(ELeave) CPointerEvent(aAttributes, aReferenceNumber);
       
    37     CleanupStack::PushL(self);
       
    38     self->ConstructL();
       
    39     CleanupStack::Pop(self);
       
    40     return self;    
       
    41     }
       
    42 
       
    43 // --------------------------------------------------------------------------------------------
       
    44 
       
    45 CPointerEvent::~CPointerEvent()
       
    46     {
       
    47     Close();
       
    48     }
       
    49 
       
    50 // --------------------------------------------------------------------------------------------
       
    51 
       
    52 CPointerEvent::CPointerEvent(TPointerEventAttributes& aAttributes, TInt aReferenceNumber) : iAttributes(aAttributes)
       
    53     {
       
    54     iAttributes.iId = aReferenceNumber;
       
    55     }
       
    56 
       
    57 // --------------------------------------------------------------------------------------------
       
    58 
       
    59 void CPointerEvent::ConstructL()
       
    60     {
       
    61     CLoadBase::ConstructL();
       
    62     
       
    63     iType = ELoadGenCmdNewLoadPointerEvent;
       
    64     
       
    65     TBuf<64> threadName;
       
    66     threadName.Format(KThreadName, iAttributes.iId);
       
    67     
       
    68     // create a thread
       
    69     User::LeaveIfError(iThread.Create(threadName, ThreadFunction, KDefaultStackSize*2, KMinHeapSize, 1024*KMinHeapSize, (TAny*) &iAttributes ));
       
    70     
       
    71     // set priority of the thread
       
    72     SetPriority();
       
    73     }
       
    74 
       
    75 // --------------------------------------------------------------------------------------------
       
    76 
       
    77 TInt CPointerEvent::ThreadFunction(TAny* aThreadArg)
       
    78     {
       
    79     CTrapCleanup* pC = CTrapCleanup::New();
       
    80     CActiveScheduler* pS = new CActiveScheduler;
       
    81     CActiveScheduler::Install(pS);
       
    82 
       
    83     // start generating load, pass pointer to arguments
       
    84     GenerateLoad(*((TPointerEventAttributes*) aThreadArg));
       
    85 
       
    86     delete pS;
       
    87     delete pC;
       
    88     
       
    89     return KErrNone;
       
    90     }
       
    91 
       
    92 // --------------------------------------------------------------------------------------------
       
    93 
       
    94 void CPointerEvent::GenerateLoad(TPointerEventAttributes& aAttributes)
       
    95     {
       
    96     CPointerEventManager* pointerEventManager = NULL;
       
    97     TRAPD(err, pointerEventManager = CPointerEventManager::NewL(aAttributes));
       
    98     if (err == KErrNone) CActiveScheduler::Start();
       
    99     delete pointerEventManager;
       
   100     }
       
   101 
       
   102 // --------------------------------------------------------------------------------------------
       
   103 
       
   104 void CPointerEvent::Resume()
       
   105     {
       
   106     CLoadBase::Resume();
       
   107     
       
   108     iThread.Resume();
       
   109     }
       
   110 
       
   111 // --------------------------------------------------------------------------------------------
       
   112 
       
   113 void CPointerEvent::Suspend()
       
   114     {
       
   115     CLoadBase::Suspend();
       
   116     
       
   117     iThread.Suspend();
       
   118     }
       
   119 
       
   120 // --------------------------------------------------------------------------------------------
       
   121 
       
   122 void CPointerEvent::SetPriority()
       
   123     {
       
   124     CLoadBase::SetPriority();
       
   125     
       
   126     iThread.SetPriority(CLoadGenModel::SettingItemToThreadPriority(iAttributes.iPriority));
       
   127     }
       
   128     
       
   129 // --------------------------------------------------------------------------------------------
       
   130 
       
   131 void CPointerEvent::Close()
       
   132     {
       
   133     CLoadBase::Close();
       
   134     
       
   135     if (iThread.ExitReason() == 0) // check if the thread is still alive
       
   136         {
       
   137         // signal the thread that it needs to close
       
   138         iThread.RequestComplete(iAttributes.iDeathStatus, KErrCancel);
       
   139 
       
   140         // wait the thread to die
       
   141         TRequestStatus waiter;
       
   142         iThread.Logon(waiter);
       
   143         User::WaitForRequest(waiter);
       
   144         iThread.Close();
       
   145         }
       
   146     }
       
   147     
       
   148 // --------------------------------------------------------------------------------------------
       
   149 
       
   150 TPtrC CPointerEvent::Description()
       
   151     {
       
   152     TBuf<256> buf;
       
   153     TBuf<16> prioBuf;
       
   154     CLoadGenModel::SettingItemToThreadDescription(iAttributes.iPriority, prioBuf);
       
   155     
       
   156     _LIT(KPointerEventEntry, "[%d] PointerEvent prio=%S heartbeat=%dms random=%d%%");
       
   157     buf.Format(KPointerEventEntry, iAttributes.iId, &prioBuf, iAttributes.iHeartBeat, iAttributes.iRandomVariance);
       
   158    
       
   159     return TPtrC(buf);
       
   160     }               
       
   161 
       
   162 // --------------------------------------------------------------------------------------------
       
   163 // --------------------------------------------------------------------------------------------
       
   164 
       
   165 CPointerEventManager* CPointerEventManager::NewL(TPointerEventAttributes& aAttributes)
       
   166     {
       
   167     CPointerEventManager* self = new(ELeave) CPointerEventManager(aAttributes);
       
   168     CleanupStack::PushL(self);
       
   169     self->ConstructL();
       
   170     CleanupStack::Pop();
       
   171     return self;
       
   172     }
       
   173 
       
   174 // --------------------------------------------------------------------------------------------
       
   175 
       
   176 CPointerEventManager::CPointerEventManager(TPointerEventAttributes& aAttributes) :
       
   177     CActive(EPriorityStandard), iAttributes(aAttributes)
       
   178     {
       
   179     }
       
   180 
       
   181 // --------------------------------------------------------------------------------------------
       
   182     
       
   183 CPointerEventManager::~CPointerEventManager()
       
   184     {
       
   185     Cancel();    
       
   186     }
       
   187 
       
   188 // --------------------------------------------------------------------------------------------
       
   189  
       
   190 void CPointerEventManager::ConstructL()
       
   191     {
       
   192     CActiveScheduler::Add(this);
       
   193     
       
   194     // set the status as pending
       
   195     iStatus = KRequestPending;
       
   196     SetActive();
       
   197     
       
   198     // set the death status pointer point to the request status of this ao
       
   199     iAttributes.iDeathStatus = &iStatus;
       
   200     
       
   201     // init    
       
   202     
       
   203     // start timer    
       
   204     iPeriodicTimer = CPeriodic::NewL(CActive::EPriorityStandard);
       
   205     iPeriodicTimer->Start(KDefaultStart, KDefaultPeriod, TCallBack(PeriodicTimerCallBack, this));
       
   206     }
       
   207     
       
   208 // --------------------------------------------------------------------------------------------
       
   209  
       
   210 void CPointerEventManager::RunL()
       
   211     {
       
   212     // request status has completed by the main thread meaning that we need to stop now
       
   213     CActiveScheduler::Stop();
       
   214     }
       
   215 
       
   216 // --------------------------------------------------------------------------------------------
       
   217  
       
   218 void CPointerEventManager::DoCancel()
       
   219     {
       
   220     }
       
   221 
       
   222 // --------------------------------------------------------------------------------------------
       
   223 
       
   224 TInt CPointerEventManager::PeriodicTimerCallBack(TAny* aAny)
       
   225     {
       
   226     CPointerEventManager* self = static_cast<CPointerEventManager*>( aAny );
       
   227 
       
   228     self->iPeriodicTimer->Cancel();
       
   229     self->SimulatePointerEvent();
       
   230 
       
   231     return KErrNone;
       
   232     }
       
   233     
       
   234 // --------------------------------------------------------------------------------------------
       
   235  
       
   236 void CPointerEventManager::SimulatePointerEvent()
       
   237     {
       
   238     // Check the screen size and select pointer event position
       
   239     // randomly
       
   240     TSize screenSize = CLoadGenModel::ScreenSize();
       
   241         
       
   242     TInt x1 = CLoadGenModel::RandomNumber(0, screenSize.iWidth);
       
   243     TInt y1 = CLoadGenModel::RandomNumber(0, screenSize.iHeight);
       
   244     
       
   245     // Generate "Pointer down" event:
       
   246     TRawEvent eventPointerDown;    
       
   247     eventPointerDown.Set(TRawEvent::EButton1Down, x1, y1);
       
   248     UserSvr::AddEvent(eventPointerDown);
       
   249     
       
   250     // Get new random position for "Move pointer" and 
       
   251     // "Pointer up" events
       
   252     TInt x2 = CLoadGenModel::RandomNumber(0, screenSize.iWidth);
       
   253     TInt y2 = CLoadGenModel::RandomNumber(0, screenSize.iHeight);
       
   254     
       
   255     // Generate "Move pointer" event:
       
   256     TRawEvent eventMovePointer;
       
   257     eventMovePointer.Set(TRawEvent::EPointerMove, x2, y2);
       
   258     UserSvr::AddEvent(eventMovePointer);
       
   259     
       
   260     // Generate "Pointer up" event:
       
   261     TRawEvent eventPointerUp;
       
   262     eventPointerUp.Set(TRawEvent::EButton1Up, x2, y2);
       
   263     UserSvr::AddEvent(eventPointerUp);
       
   264     
       
   265     // call timer
       
   266     iPeriodicTimer->Start(CLoadGenModel::MilliSecondsToMicroSeconds(iAttributes.iHeartBeat, iAttributes.iRandomVariance), KDefaultPeriod, TCallBack(PeriodicTimerCallBack, this));
       
   267     }
       
   268     
       
   269 // --------------------------------------------------------------------------------------------
       
   270 
       
   271 // End of File