camerauis/cameraxui/cxui/src/cxuicapturekeyhandler.cpp
changeset 48 42ba2d16bf40
parent 21 fa6d9f75d6a6
child 52 7e18d488ac5f
equal deleted inserted replaced
37:64817133cd1d 48:42ba2d16bf40
     1 /*
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 */
    16 */
    17 #include <w32std.h>
    17 
    18 #include <QVariant>
    18 #include <QVariant>
    19 #include <QKeyEvent>
    19 #include <QKeyEvent>
    20 #include <eikon.hrh>
       
    21 #include <coemain.h>
       
    22 #include <e32keys.h> // for EKeyCamera
       
    23 
       
    24 
    20 
    25 #include "cxuicapturekeyhandler.h"
    21 #include "cxuicapturekeyhandler.h"
       
    22 #ifdef Q_OS_SYMBIAN
       
    23 #include "cxuicapturekeyhandler_symbian_p.h"
       
    24 #else
       
    25 #include "cxuicapturekeyhandler_desktop_p.h"
       
    26 #endif
       
    27 
    26 #include "cxutils.h"
    28 #include "cxutils.h"
    27 #include "cxeengine.h"
    29 #include "cxeengine.h"
    28 #include "cxenamespace.h"
    30 #include "cxenamespace.h"
    29 #include "cxefeaturemanager.h"
    31 #include "cxefeaturemanager.h"
    30 
    32 
    31 const int CXUI_KEY_PRIORITY = 100;
    33 
    32 
    34 
    33 CxuiCaptureKeyHandler::CxuiCaptureKeyHandler(CxeEngine &aEngine) :
    35 CxuiCaptureKeyHandler::CxuiCaptureKeyHandler(CxeEngine &aEngine) :
    34     mAutofocusKeyPressed(false), mCaptureKeyPressed(false), mEngine(aEngine), mWsSession(CCoeEnv::Static()->WsSession()),
    36         d_ptr(NULL)
    35     mWindowGroup(CCoeEnv::Static()->RootWin())
       
    36 {
    37 {
       
    38     d_ptr = new CxuiCaptureKeyHandlerPrivate(aEngine, this);
       
    39     
       
    40     d_ptr->listenKeys(true);
       
    41 }
       
    42 
       
    43 CxuiCaptureKeyHandler::~CxuiCaptureKeyHandler()
       
    44 {    
    37     CX_DEBUG_ENTER_FUNCTION();
    45     CX_DEBUG_ENTER_FUNCTION();
    38 
    46 
    39     // Autofocus key
    47     Q_D(CxuiCaptureKeyHandler);
    40     mEngine.featureManager().configuredValues(CxeRuntimeKeys::PRIMARY_CAMERA_AUTOFOCUS_KEYS,
    48     d->listenKeys(false);
    41                                               mPrimaryCameraAutofocusKeys);
       
    42 
    49 
    43     // Capture keys for primary camera
    50     delete d;
    44     mEngine.featureManager().configuredValues(CxeRuntimeKeys::PRIMARY_CAMERA_CAPTURE_KEYS,
       
    45                                               mPrimaryCameraCaptureKeys);
       
    46 
    51 
    47     listenKeys(true);
       
    48     CX_DEBUG_EXIT_FUNCTION();
    52     CX_DEBUG_EXIT_FUNCTION();
    49 }
    53 }
    50 
    54 
    51 CxuiCaptureKeyHandler::~CxuiCaptureKeyHandler()
       
    52 {
       
    53     CX_DEBUG_ENTER_FUNCTION();
       
    54     listenKeys(false);
       
    55     CX_DEBUG_EXIT_FUNCTION();
       
    56 }
       
    57 
       
    58 /*!
       
    59 * Start or stop listening key events.
       
    60 * @param listen Should we start (true) or stop (false) listening key events.
       
    61 */
       
    62 void CxuiCaptureKeyHandler::listenKeys(bool listen)
    55 void CxuiCaptureKeyHandler::listenKeys(bool listen)
    63 {
    56 {
    64     CX_DEBUG_ENTER_FUNCTION();
    57     Q_D(CxuiCaptureKeyHandler);
    65 
    58     d->listenKeys(listen);
    66     if (listen) {
       
    67         // Protect from multiple calls
       
    68         if (mCapturedKeyUpDownHandles.empty() && mCapturedKeyHandles.empty()) {
       
    69 
       
    70             int key(0);
       
    71             foreach (key, mPrimaryCameraAutofocusKeys) {
       
    72                 CX_DEBUG(("CxuiCaptureKeyHandler - hooking autofocus key with scan / key code: %d", key));
       
    73                 listenKey(key);
       
    74             }
       
    75             foreach (key, mPrimaryCameraCaptureKeys) {
       
    76                 CX_DEBUG(("CxuiCaptureKeyHandler - hooking capture key with scan / key code: %d", key));
       
    77                 listenKey(key);
       
    78             }
       
    79         }
       
    80     } else {
       
    81 
       
    82         int handle(0);
       
    83         foreach (handle, mCapturedKeyUpDownHandles) {
       
    84             mWindowGroup.CancelCaptureKeyUpAndDowns(handle);
       
    85         }
       
    86         mCapturedKeyUpDownHandles.clear();
       
    87 
       
    88         foreach (handle, mCapturedKeyHandles) {
       
    89             mWindowGroup.CancelCaptureKey(handle);
       
    90         }
       
    91         mCapturedKeyHandles.clear();
       
    92     }
       
    93     CX_DEBUG_EXIT_FUNCTION();
       
    94 }
    59 }
    95 
    60 
    96 bool CxuiCaptureKeyHandler::isAutofocusKeyPressed()
    61 bool CxuiCaptureKeyHandler::isAutofocusKeyPressed()
    97 {
    62 {
    98     return mAutofocusKeyPressed;
    63     Q_D(CxuiCaptureKeyHandler);
       
    64     return d->isAutofocusKeyPressed();
    99 }
    65 }
   100 
    66 
   101 bool CxuiCaptureKeyHandler::isCaptureKeyPressed()
    67 bool CxuiCaptureKeyHandler::isCaptureKeyPressed()
   102 {
    68 {
   103     return mCaptureKeyPressed;
    69     Q_D(CxuiCaptureKeyHandler);
       
    70     return d->isCaptureKeyPressed();
   104 }
    71 }
   105 
    72 
   106 bool CxuiCaptureKeyHandler::handleKeyEvent(QEvent *event)
    73 bool CxuiCaptureKeyHandler::handleKeyEvent(QEvent *event)
   107 {
    74 {
   108     CX_DEBUG_ENTER_FUNCTION();
    75     Q_D(CxuiCaptureKeyHandler);
   109     bool eventWasConsumed = false;
    76     return d->handleKeyEvent(event);
   110 
       
   111     if (event->type() == QEvent::KeyPress) {
       
   112         QKeyEvent *keyEvent = static_cast<QKeyEvent*> (event);
       
   113         CX_DEBUG(("CxuiCaptureKeyHandler - key press with nativeVirtualKey(): %d", keyEvent->nativeVirtualKey()));
       
   114         CX_DEBUG(("CxuiCaptureKeyHandler - key press with nativeScanCode(): %d", keyEvent->nativeScanCode()));
       
   115 
       
   116         if ( mPrimaryCameraAutofocusKeys.contains(keyEvent->nativeScanCode())
       
   117             && !mAutofocusKeyPressed ) {
       
   118 
       
   119             mAutofocusKeyPressed = true;
       
   120             eventWasConsumed = true;
       
   121             emit autofocusKeyPressed();
       
   122 
       
   123         } else if (mPrimaryCameraCaptureKeys.contains(keyEvent->nativeScanCode())
       
   124             && !mCaptureKeyPressed) {
       
   125             mCaptureKeyPressed = true;
       
   126             eventWasConsumed = true;
       
   127             emit captureKeyPressed();
       
   128         }
       
   129     } else if (event->type() == QEvent::KeyRelease) {
       
   130         QKeyEvent *keyEvent = static_cast<QKeyEvent*> (event);
       
   131         CX_DEBUG(("CxuiCaptureKeyHandler - key press with nativeVirtualKey(): %d", keyEvent->nativeVirtualKey()));
       
   132         CX_DEBUG(("CxuiCaptureKeyHandler - key press with nativeScanCode(): %d", keyEvent->nativeScanCode()));
       
   133 
       
   134         if ( mPrimaryCameraAutofocusKeys.contains(keyEvent->nativeScanCode())
       
   135             && mAutofocusKeyPressed ) {
       
   136 
       
   137             mAutofocusKeyPressed = false;
       
   138             eventWasConsumed = true;
       
   139             emit autofocusKeyReleased();
       
   140 
       
   141         } else if (mPrimaryCameraCaptureKeys.contains(keyEvent->nativeScanCode())
       
   142             && mCaptureKeyPressed) {
       
   143 
       
   144             mCaptureKeyPressed = false;
       
   145             eventWasConsumed = true;
       
   146             emit captureKeyReleased();
       
   147         }
       
   148     }
       
   149     CX_DEBUG_EXIT_FUNCTION();
       
   150     return eventWasConsumed;
       
   151 }
    77 }
   152 
    78 
   153 /*!
       
   154 * Helper method to listen to given key (key code or scan code).
       
   155 * We need to listen to both "key up", "key down" and "key pressed" events to
       
   156 * get all the necessary events to handleKeyEvent(). If we e.g. just listen
       
   157 * to up/down events, the way native events are translated to QKeyEvents,
       
   158 * we only get QEvent::KeyRelease event when partially in background.
       
   159 * @param key Keycode or scancode for the key to listen. Both should be listened.
       
   160 */
       
   161 void CxuiCaptureKeyHandler::listenKey(int key)
       
   162 {
       
   163     // Capture key down and up events
       
   164     int handle = mWindowGroup.CaptureKeyUpAndDowns(key, 0, 0, CXUI_KEY_PRIORITY);
       
   165 
       
   166     // Handle < 0 means error.
       
   167     if (handle >= 0) {
       
   168         mCapturedKeyUpDownHandles.append(handle);
       
   169     } else {
       
   170         CX_DEBUG(("[WARNING] CxuiCaptureKeyHandler - Problem hooking to key-up/key-down with code: %d", key));
       
   171     }
       
   172 
       
   173     // Capture key press events
       
   174     handle = mWindowGroup.CaptureKey(key, 0, 0, CXUI_KEY_PRIORITY);
       
   175 
       
   176     if (handle >= 0) {
       
   177         mCapturedKeyHandles.append(handle);
       
   178     } else {
       
   179         CX_DEBUG(("[WARNING] CxuiCaptureKeyHandler - Problem hooking to key-press with code: %d", key));
       
   180     }
       
   181 }
       
   182 
       
   183 
       
   184 // end of file
    79 // end of file