qstmgesturelib/qstmgestureengine.cpp
changeset 0 1450b09d0cfd
child 3 0954f5dd2cd0
equal deleted inserted replaced
-1:000000000000 0:1450b09d0cfd
       
     1 /*
       
     2 * Copyright (c) 2010 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 "qstmgestureengine.h"
       
    20 #include "qstmfilelogger.h"
       
    21 #include "qstmutils.h"
       
    22 
       
    23 
       
    24 using namespace qstmGesture ;
       
    25 
       
    26 QStm_GestureEngine::QStm_GestureEngine()
       
    27 {
       
    28 	m_loggingEnabled = 0;
       
    29     m_numOfActiveStreams = 0 ;
       
    30     m_currentGestureOwner = -1 ;
       
    31     m_currentLockedGesture = -1 ;
       
    32     for (int i = 0; i < qstmUiEventEngine::KMaxNumberOfPointers; i++) {
       
    33         m_uiEventStream[i] = NULL ;
       
    34     }
       
    35 }
       
    36 
       
    37 QStm_GestureEngine::~QStm_GestureEngine()
       
    38 {
       
    39     m_gestures.clear() ;
       
    40 }
       
    41 
       
    42 bool QStm_GestureEngine::addGesture(const QStm_GestureRecogniserIf* newGesture)
       
    43 {
       
    44     // Add the new gesture recogniser to our list of recognisers
       
    45 	if (newGesture) {
       
    46 		QStm_GestureRecogniserIf* p = const_cast<QStm_GestureRecogniserIf*>(newGesture);
       
    47 		m_gestures.append(p);
       
    48 	}
       
    49     return true;
       
    50 }
       
    51 
       
    52 bool QStm_GestureEngine::insertGesture(const QStm_GestureRecogniserIf* newGesture, int startPos)
       
    53 {
       
    54 	QStm_GestureRecogniserIf* p = const_cast<QStm_GestureRecogniserIf*>(newGesture);
       
    55     m_gestures.insert(startPos, p);
       
    56     return true;
       
    57 }
       
    58 
       
    59 int QStm_GestureEngine::findGesture(const QStm_GestureRecogniserIf* gesture, int startPos) const
       
    60 {
       
    61     for(int i = MAX(startPos, 0); i < m_gestures.count(); ++i) {
       
    62         if(m_gestures[i] == gesture) {
       
    63             return i;
       
    64         }
       
    65     }
       
    66     return -1;
       
    67 }
       
    68 
       
    69 int QStm_GestureEngine::findGestureReverse(const QStm_GestureRecogniserIf* gesture, int startPos) const
       
    70 {
       
    71     for(int i = MIN(startPos, m_gestures.count() - 1); i >=0; --i) {
       
    72         if(m_gestures[i] == gesture) {
       
    73             return i;
       
    74             }
       
    75         }
       
    76     return -1;
       
    77 }
       
    78 
       
    79 int QStm_GestureEngine::findGesture(QStm_GestureUid uid, int startPos) const
       
    80 {
       
    81     for(int i = MAX(startPos, 0); i < m_gestures.count(); ++i) {
       
    82         if(m_gestures[i]->gestureUid() == uid) {
       
    83             return i;
       
    84         }
       
    85     }
       
    86     return -1;
       
    87 }
       
    88 
       
    89 int QStm_GestureEngine::findGestureReverse(QStm_GestureUid uid, int startPos) const
       
    90 {
       
    91     for(int i = MIN(startPos, m_gestures.count() - 1); i >=0; --i) {
       
    92         if(m_gestures[i]->gestureUid() == uid) {
       
    93             return i;
       
    94         }
       
    95     }
       
    96     return -1;
       
    97 }
       
    98 
       
    99 int QStm_GestureEngine::gestureCount() const
       
   100 {
       
   101     return m_gestures.count();
       
   102 }
       
   103 
       
   104 bool QStm_GestureEngine::removeGesture(const QStm_GestureRecogniserIf* oldGesture)
       
   105 {
       
   106     // If gestures are removed, there cannot be current gesture owner...
       
   107     if (m_currentGestureOwner != -1)
       
   108     {
       
   109         QStm_GestureRecogniserIf* pgrif = m_gestures[m_currentGestureOwner] ;
       
   110         pgrif->release(this) ;
       
   111         m_currentGestureOwner = -1 ;    // no more gesture owners...
       
   112     }
       
   113     QStm_GestureRecogniserIf* p = const_cast<QStm_GestureRecogniserIf*>(oldGesture);
       
   114     int ix = m_gestures.indexOf(p) ;
       
   115     bool found = (ix != -1);
       
   116     if (found) {
       
   117         m_gestures.removeAt(ix) ;
       
   118     }
       
   119     return found ;
       
   120 }
       
   121 
       
   122 int QStm_GestureEngine::activeStreamCount() const
       
   123 {
       
   124     return m_numOfActiveStreams ;
       
   125 }
       
   126 
       
   127 const qstmUiEventEngine::QStm_UiEventIf* QStm_GestureEngine::getUiEvents(int indexOfActiveStream) const
       
   128 {
       
   129 //#if defined(ADVANCED_POINTER_EVENTS)
       
   130     // create temporary array of active event streams and initialize with zero
       
   131     const qstmUiEventEngine::QStm_UiEventIf* activeEventPointers[qstmUiEventEngine::KMaxNumberOfPointers] ;
       
   132     for (int x = 0; x < qstmUiEventEngine::KMaxNumberOfPointers; activeEventPointers[x++] = 0);
       
   133     
       
   134     // then fill from currently active event streams
       
   135     int indextoactiveEventPointers = 0 ;
       
   136     for (int i = 0; i < qstmUiEventEngine::KMaxNumberOfPointers; i++) {
       
   137         if (m_uiEventStream[i] != NULL) {
       
   138             activeEventPointers[indextoactiveEventPointers++] = m_uiEventStream[i] ;
       
   139         }
       
   140     }
       
   141     // then return the active event stream asked
       
   142     return activeEventPointers[indexOfActiveStream] ;
       
   143 //#else
       
   144     // in single touch it is enough to return the only possible pointer
       
   145 //    return m_uiEventStream[indexOfActiveStream] ;
       
   146 //#endif
       
   147 }
       
   148 
       
   149 /*!
       
   150  * Process the UI events
       
   151  */
       
   152 void QStm_GestureEngine::handleUiEvent(const qstmUiEventEngine::QStm_UiEventIf& event )
       
   153 {
       
   154     // process one incoming UI event
       
   155     storeUiEvent(event) ;  // store the event to the "stream" based on the index of pointer
       
   156     walkTroughGestures() ;  // and walk trough the gestures to process the UI event
       
   157     updateUiEvents() ;
       
   158     // If it was last release event, make sure no-one has the gestures locked
       
   159     m_numOfActiveStreams = 0 ;
       
   160     for (int i = 0; i < qstmUiEventEngine::KMaxNumberOfPointers; i++) {
       
   161         if (m_uiEventStream[i] != NULL) m_numOfActiveStreams++ ;
       
   162     }
       
   163     
       
   164     if (m_numOfActiveStreams == 0)
       
   165     {
       
   166         if (m_currentLockedGesture != -1)
       
   167         {
       
   168             QStm_GestureRecogniserIf* pgrif = m_gestures[m_currentLockedGesture] ;
       
   169             pgrif->release(this) ;
       
   170         }
       
   171         m_currentLockedGesture = -1 ;
       
   172     }
       
   173 }
       
   174 
       
   175 /*!
       
   176  * Store the UI event.  There are max X "streams" of events, one for each
       
   177  * pointer.  The streams are actually just pointers to the latest event, since the
       
   178  * MUiEvent interface has methods to walk trough the chain of events.
       
   179  */
       
   180 void QStm_GestureEngine::storeUiEvent(const qstmUiEventEngine::QStm_UiEventIf& event)
       
   181 {
       
   182     m_uiEventStream[event.index()] = &event ;
       
   183     m_numOfActiveStreams = 0 ;
       
   184     for (int i = 0; i < qstmUiEventEngine::KMaxNumberOfPointers; i++) {
       
   185         if (m_uiEventStream[i] != NULL) m_numOfActiveStreams++ ;
       
   186     }
       
   187 }
       
   188 
       
   189 /*!
       
   190  *  Call each gesture handler in turn until one claims to be in control of the gesture.
       
   191  */
       
   192 void QStm_GestureEngine::walkTroughGestures()
       
   193 {
       
   194     int newowner = -1 ;
       
   195     int newlocker =  -1; //m_currentLockedGesture ;
       
   196     // check if someone has locked the gesture
       
   197     QStm_GestureRecognitionState thestate = ENotMyGesture ;
       
   198     if (m_currentLockedGesture != -1) {
       
   199         QStm_GestureRecogniserIf* pgrif = m_gestures[m_currentLockedGesture] ;
       
   200         if (pgrif != NULL) {
       
   201             if (m_loggingEnabled) {
       
   202                 // log entry about locked gesture (hmm.. should have added names to the MGestureRecogniserIf
       
   203                 LOGARG("locked gesture recognizer %d (addr %d), active streams %d", 
       
   204                 		m_currentLockedGesture, pgrif, m_numOfActiveStreams);
       
   205             }
       
   206 
       
   207             thestate = pgrif->recognise(m_numOfActiveStreams, this) ;
       
   208             switch (thestate) {
       
   209 				case EGestureActive:
       
   210 				{
       
   211 					// This gesture recogniser owns the gesture, so release the lock
       
   212 					newlocker = -1 ;
       
   213 					newowner = m_currentLockedGesture ;
       
   214 					if (m_loggingEnabled) {
       
   215 						LOGARG("new owner %d lock release", m_currentLockedGesture);
       
   216 					}
       
   217 					break;
       
   218 				}
       
   219 				case ELockToThisGesture:
       
   220 				{
       
   221 					// this gesture recogniser wants to keep the lock
       
   222 					newowner = m_currentLockedGesture ;
       
   223 					newlocker = m_currentLockedGesture ;
       
   224 					if (m_loggingEnabled) {
       
   225 						LOGARG("new owner %d keep lock", m_currentLockedGesture);
       
   226 					}
       
   227 					break;
       
   228 	
       
   229 				}
       
   230 				case ENotMyGesture:
       
   231 				{
       
   232 					break;
       
   233 				}
       
   234             }
       
   235         }
       
   236         else {
       
   237             if (m_loggingEnabled) {
       
   238                 LOGARG("NULL recogniser for %d", m_currentLockedGesture);
       
   239             }
       
   240         }
       
   241     }
       
   242     QStm_GestureRecognitionState currentState = ENotMyGesture;
       
   243     if (thestate == ENotMyGesture) {
       
   244         if (m_loggingEnabled) {
       
   245             LOGARG("walk trough recognizers active streams %d", m_numOfActiveStreams);
       
   246         }
       
   247         // No locking gesture, walk trough the list until someone handles this
       
   248         int gcount = m_gestures.count();
       
   249         if (m_currentGestureOwner > -1) {
       
   250             QStm_GestureRecogniserIf* gestureOwner = m_gestures[m_currentGestureOwner] ;
       
   251             currentState = gestureOwner->state();
       
   252         }
       
   253         else {
       
   254             currentState = ENotMyGesture;
       
   255         }
       
   256         
       
   257         for (int i = 0; i < gcount; i++) {
       
   258             bool controlObtained = false;
       
   259             QStm_GestureRecogniserIf* pgrif = m_gestures[i];
       
   260             
       
   261             
       
   262             if (pgrif != NULL) {
       
   263                 switch (pgrif->recognise(m_numOfActiveStreams, this)) {
       
   264 					case EGestureActive:
       
   265 					{
       
   266 						// This gesture recogniser owns the gesture, stop looping...
       
   267 						controlObtained = true;
       
   268 						newowner = i;
       
   269 						break;
       
   270 					}
       
   271 					case ELockToThisGesture:
       
   272 					{
       
   273 						// this gesture recogniser wants to take ownership
       
   274 						controlObtained = true;
       
   275 						newowner = i;
       
   276 						newlocker = i;
       
   277 						break;
       
   278 	
       
   279 					}
       
   280 					case ENotMyGesture:
       
   281 					{
       
   282 						break;
       
   283 					}
       
   284                 }
       
   285                 LOGARG("walkTroughGestures: 0x%x, recognizers count %d, newowner %d, m_currentGestureOwner %d", 
       
   286                 		pgrif, gcount, newowner, m_currentGestureOwner);
       
   287             }
       
   288             if (controlObtained)  {
       
   289                 break; // do not process rest of the gestures
       
   290             }
       
   291 
       
   292         }
       
   293     }
       
   294     if (newowner != m_currentGestureOwner) {
       
   295         if (m_currentGestureOwner != -1 ) {
       
   296             QStm_GestureRecogniserIf* pgrif = m_gestures[m_currentGestureOwner] ;
       
   297             if ((currentState == EGestureActive || currentState == ELockToThisGesture) &&
       
   298                  pgrif->state() == ENotMyGesture) {
       
   299                 
       
   300                 pgrif->release(this) ;
       
   301             }
       
   302         }
       
   303         m_currentGestureOwner = newowner ;
       
   304     }
       
   305     m_currentLockedGesture = newlocker ;    // if someone locked it or released the lock
       
   306 }
       
   307 
       
   308 void QStm_GestureEngine::updateUiEvents()
       
   309 {
       
   310     for (int i = 0; i < qstmUiEventEngine::KMaxNumberOfPointers; i++) {
       
   311         if (m_uiEventStream[i] != NULL) {
       
   312             if (m_uiEventStream[i]->code() == qstmUiEventEngine::ERelease) {
       
   313                 // Event can be removed since Release is the last event
       
   314                 // note that it is the lower layer event engine
       
   315                 // which actually deletes the object
       
   316                 m_uiEventStream[i] = NULL ;
       
   317             }
       
   318         }
       
   319     }
       
   320 }
       
   321 
       
   322