webengine/webkitutils/stmgesturefw/src/gestureengine.cpp
changeset 65 5bfc169077b2
parent 42 d39add9822e2
child 66 cacf6ee57968
equal deleted inserted replaced
42:d39add9822e2 65:5bfc169077b2
     1 /*
       
     2 * Copyright (c) 2008 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 the License "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:  Gesture helper implementation
       
    15 *
       
    16 */
       
    17 
       
    18 #include "GestureEngine.h"
       
    19 
       
    20 #include "filelogger.h"
       
    21 
       
    22 
       
    23 using namespace stmGesture ;
       
    24 
       
    25 CGestureEngine::CGestureEngine()
       
    26 {
       
    27     m_numOfActiveStreams = 0 ;
       
    28     m_currentGestureOwner = -1 ;
       
    29     m_currentLockedGesture = -1 ;
       
    30     for (int i = 0; i < stmUiEventEngine::KMaxNumberOfPointers; i++)
       
    31     {
       
    32         m_uiEventStream[i] = NULL ;
       
    33     }
       
    34 }
       
    35 
       
    36 CGestureEngine::~CGestureEngine()
       
    37 {
       
    38     m_gestures.Reset() ;
       
    39 }
       
    40 
       
    41 bool CGestureEngine::addGesture(const MGestureRecogniserIf* aNewGesture)
       
    42 {
       
    43     // Add the new gesture recogniser to our list of recognisers
       
    44     return m_gestures.Append(aNewGesture) == 0;
       
    45 }
       
    46 
       
    47 bool CGestureEngine::insertGesture(const MGestureRecogniserIf* aNewGesture)
       
    48 {
       
    49     return insertGestureAt(aNewGesture, 0) == 0;
       
    50 }
       
    51 
       
    52 bool CGestureEngine::insertGestureAt(const MGestureRecogniserIf* aNewGesture, int position)
       
    53 {
       
    54     return m_gestures.Insert(aNewGesture, position) == 0 ;
       
    55 }
       
    56 
       
    57 bool CGestureEngine::removeGesture(const MGestureRecogniserIf* aOldGesture)
       
    58 {
       
    59     // If gestures are removed, there cannot be current gesture owner...
       
    60     if (m_currentGestureOwner != -1)
       
    61     {
       
    62         MGestureRecogniserIf* pgrif = m_gestures[m_currentGestureOwner] ;
       
    63         pgrif->release(this) ;
       
    64         m_currentGestureOwner = -1 ;    // no more gesture owners...
       
    65     }
       
    66     TInt ix = m_gestures.Find(aOldGesture) ;
       
    67     bool found = (ix != -1);
       
    68     if (found)
       
    69     {
       
    70         m_gestures.Remove(ix) ;
       
    71     }
       
    72     return found ;
       
    73 }
       
    74 
       
    75 int CGestureEngine::activeStreamCount() const
       
    76 {
       
    77     return m_numOfActiveStreams ;
       
    78 }
       
    79 
       
    80 const stmUiEventEngine::MUiEvent* CGestureEngine::getUiEvents(int indexOfActiveStream) const
       
    81 {
       
    82 #if defined(ADVANCED_POINTER_EVENTS)
       
    83 	// create temporary array of active event streams and initialize with zero
       
    84 	const stmUiEventEngine::MUiEvent* activeEventPointers[stmUiEventEngine::KMaxNumberOfPointers] ;
       
    85 	for (int x = 0; x < stmUiEventEngine::KMaxNumberOfPointers; x++) activeEventPointers[x] = 0 ;
       
    86 	// then fill from currently active event streams
       
    87 	int indextoactiveEventPointers = 0 ;
       
    88     for (int i = 0; i < stmUiEventEngine::KMaxNumberOfPointers; i++)
       
    89     {
       
    90     	if (m_uiEventStream[i])
       
    91     	    activeEventPointers[indextoactiveEventPointers++] = m_uiEventStream[i] ;
       
    92     }
       
    93 	// then return the active event stream asked
       
    94     return activeEventPointers[indexOfActiveStream] ;
       
    95 #else
       
    96     // in single touch it is enough to return the only possible pointer
       
    97     return m_uiEventStream[indexOfActiveStream] ;
       
    98 #endif
       
    99 }
       
   100 
       
   101 /*!
       
   102  * Process the UI events
       
   103  */
       
   104 void CGestureEngine::HandleUiEventL(const stmUiEventEngine::MUiEvent& aEvent )
       
   105 {
       
   106     // process one incoming UI event
       
   107     storeUiEvent(aEvent) ;  // store the event to the "stream" based on the index of pointer
       
   108     walkTroughGestures() ;  // and walk trough the gestures to process the UI event
       
   109     updateUiEvents() ;
       
   110     // If it was last release event, make sure no-one has the gestures locked
       
   111     m_numOfActiveStreams = 0 ;
       
   112     for (int i = 0; i < stmUiEventEngine::KMaxNumberOfPointers; i++)
       
   113     {
       
   114         if (m_uiEventStream[i]) m_numOfActiveStreams++ ;
       
   115     }
       
   116     if (m_numOfActiveStreams == 0)
       
   117     {
       
   118         if (m_currentLockedGesture != -1)
       
   119         {
       
   120             MGestureRecogniserIf* pgrif = m_gestures[m_currentLockedGesture] ;
       
   121             pgrif->release(this) ;
       
   122         }
       
   123         m_currentLockedGesture = -1 ;
       
   124     }
       
   125 }
       
   126 
       
   127 /*!
       
   128  * Store the UI event.  There are max X "streams" of events, one for each
       
   129  * pointer.  The streams are actually just pointers to the latest event, since the
       
   130  * MUiEvent interface has methods to walk trough the chain of events.
       
   131  */
       
   132 void CGestureEngine::storeUiEvent(const stmUiEventEngine::MUiEvent& aEvent)
       
   133 {
       
   134     m_uiEventStream[aEvent.Index()] = &aEvent ;
       
   135     m_numOfActiveStreams = 0 ;
       
   136     for (int i = 0; i < stmUiEventEngine::KMaxNumberOfPointers; i++)
       
   137     {
       
   138         if (m_uiEventStream[i]) m_numOfActiveStreams++ ;
       
   139     }
       
   140 }
       
   141 
       
   142 /*!
       
   143  *  Call each gesture handler in turn until one claims to be in control of the gesture.
       
   144  */
       
   145 void CGestureEngine::walkTroughGestures()
       
   146 {
       
   147     int newowner = -1 ;
       
   148     int newlocker = m_currentLockedGesture ;
       
   149     // check if someone has locked the gesture
       
   150     TGestureRecognitionState thestate = ENotMyGesture ;
       
   151     if (m_currentLockedGesture != -1)
       
   152     {
       
   153         MGestureRecogniserIf* pgrif = m_gestures[m_currentLockedGesture] ;
       
   154         if (pgrif)
       
   155         {
       
   156             if (m_loggingEnabled)
       
   157             {
       
   158                 // log entry about locked gesture (hmm.. should have added names to the MGestureRecogniserIf
       
   159                 LOGARG("locked gesture recognizer %d (addr %d), active streams %d",
       
   160                         m_currentLockedGesture, pgrif, m_numOfActiveStreams);
       
   161             }
       
   162 
       
   163             thestate = pgrif->recognise(m_numOfActiveStreams, this) ;
       
   164             switch (thestate)
       
   165             {
       
   166             case EGestureActive:
       
   167             {
       
   168                 // This gesture recogniser owns the gesture, so release the lock
       
   169                 newlocker = -1 ;
       
   170                 newowner = m_currentLockedGesture ;
       
   171                 if (m_loggingEnabled)
       
   172                 {
       
   173                     LOGARG("new owner %d lock release", m_currentLockedGesture);
       
   174                 }
       
   175                 break;
       
   176             }
       
   177             case ELockToThisGesture:
       
   178             {
       
   179                 // this gesture recogniser wants to keep the lock
       
   180                 newowner = m_currentLockedGesture ;
       
   181                 newlocker = m_currentLockedGesture ;
       
   182                 if (m_loggingEnabled)
       
   183                 {
       
   184                     LOGARG("new owner %d keep lock", m_currentLockedGesture);
       
   185                 }
       
   186                 break;
       
   187 
       
   188             }
       
   189             case ENotMyGesture:
       
   190             {
       
   191                 break;
       
   192             }
       
   193             }
       
   194         }
       
   195         else
       
   196         {
       
   197             if (m_loggingEnabled)
       
   198             {
       
   199                 LOGARG("NULL recogniser for %d", m_currentLockedGesture);
       
   200             }
       
   201         }
       
   202     }
       
   203 
       
   204     if (thestate == ENotMyGesture)
       
   205     {
       
   206         if (m_loggingEnabled)
       
   207         {
       
   208             LOGARG("walk trough recognizers active streams %d", m_numOfActiveStreams);
       
   209         }
       
   210         // No locking gesture, walk trough the list until someone handles this
       
   211         for (int i = 0; i < m_gestures.Count(); i++)
       
   212         {
       
   213             bool controlObtained = false;
       
   214             MGestureRecogniserIf* pgrif = m_gestures[i];
       
   215             if (pgrif)
       
   216             {
       
   217                 switch (pgrif->recognise(m_numOfActiveStreams, this))
       
   218                 {
       
   219                 case EGestureActive:
       
   220                 {
       
   221                     // This gesture recogniser owns the gesture, stop looping...
       
   222                     controlObtained = true;
       
   223                     newowner = i;
       
   224                     break;
       
   225                 }
       
   226                 case ELockToThisGesture:
       
   227                 {
       
   228                     // this gesture recogniser wants to take ownership
       
   229                     controlObtained = true;
       
   230                     newowner = i;
       
   231                     newlocker = i;
       
   232                     break;
       
   233 
       
   234                 }
       
   235                 case ENotMyGesture:
       
   236                 {
       
   237                     break;
       
   238                 }
       
   239                 }
       
   240             }
       
   241             if (controlObtained)
       
   242             {
       
   243                 break; // do not process rest of the gestures
       
   244             }
       
   245 
       
   246         }
       
   247     }
       
   248     if (newowner != -1 && newowner != m_currentGestureOwner)
       
   249     {
       
   250         if (m_currentGestureOwner != -1)
       
   251         {
       
   252             MGestureRecogniserIf* pgrif = m_gestures[m_currentGestureOwner] ;
       
   253             pgrif->release(this) ;
       
   254         }
       
   255         m_currentGestureOwner = newowner ;
       
   256     }
       
   257     m_currentLockedGesture = newlocker ;    // if someone locked it or released the lock
       
   258 }
       
   259 
       
   260 void CGestureEngine::updateUiEvents()
       
   261 {
       
   262     for (int i = 0; i < stmUiEventEngine::KMaxNumberOfPointers; i++)
       
   263     {
       
   264         if (m_uiEventStream[i])
       
   265         {
       
   266             if (m_uiEventStream[i]->Code() == stmUiEventEngine::ERelease)
       
   267             {
       
   268                 // Event can be removed since Release is the last event
       
   269                 // note that it is the lower layer event engine
       
   270                 // which actually deletes the object
       
   271                 m_uiEventStream[i] = NULL ;
       
   272             }
       
   273         }
       
   274     }
       
   275 }