camerauis/cameraxui/cxui/src/cxuicapturekeyhandler_symbian_p.cpp
changeset 48 42ba2d16bf40
child 52 7e18d488ac5f
equal deleted inserted replaced
37:64817133cd1d 48:42ba2d16bf40
       
     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 *   Private implementation for CxuiCaptureKeyHandler
       
    16 */
       
    17 
       
    18 #include <QVariant>
       
    19 #include <QKeyEvent>
       
    20 
       
    21 #include "cxuicapturekeyhandler_symbian_p.h"
       
    22 #include "cxuicapturekeyhandler.h"
       
    23 #include "cxutils.h"
       
    24 #include "cxeengine.h"
       
    25 #include "cxenamespace.h"
       
    26 #include "cxefeaturemanager.h"
       
    27 
       
    28 #include <w32std.h>
       
    29 #include <eikon.hrh>
       
    30 #include <coemain.h>
       
    31 #include <e32keys.h> // for EKeyCamera
       
    32 
       
    33 namespace {
       
    34     const int CXUI_KEY_PRIORITY = 100;
       
    35     const int CXUI_CAPTURE_KEY_CODE1 = 0xab;
       
    36     const int CXUI_CAPTURE_KEY_CODE2 = 0xf849;
       
    37     const int CXUI_AUTOFOCUS_KEY_CODE1 = 0xe2;
       
    38     const int CXUI_AUTOFOCUS_KEY_CODE2 = 0xf880;
       
    39 
       
    40 }
       
    41 CxuiCaptureKeyHandlerPrivate::CxuiCaptureKeyHandlerPrivate(CxeEngine &aEngine, CxuiCaptureKeyHandler *parent) :
       
    42     q_ptr(parent),
       
    43     mEngine(aEngine),
       
    44     mAutofocusKeyPressed(false),
       
    45     mCaptureKeyPressed(false),
       
    46     mWsSession(CCoeEnv::Static()->WsSession()),
       
    47     mWindowGroup(CCoeEnv::Static()->RootWin())
       
    48 {
       
    49     CX_DEBUG_ENTER_FUNCTION();
       
    50 
       
    51     // key codes hardcoded for now
       
    52     // Autofocus key
       
    53     mPrimaryCameraAutofocusKeys.append(CXUI_AUTOFOCUS_KEY_CODE1);
       
    54     mPrimaryCameraAutofocusKeys.append(CXUI_AUTOFOCUS_KEY_CODE2);
       
    55 
       
    56     // Capture keys for primary camera
       
    57     mPrimaryCameraCaptureKeys.append(CXUI_CAPTURE_KEY_CODE1);
       
    58     mPrimaryCameraCaptureKeys.append(CXUI_CAPTURE_KEY_CODE2);
       
    59 
       
    60     listenKeys(true);
       
    61     CX_DEBUG_EXIT_FUNCTION();
       
    62 }
       
    63 
       
    64 CxuiCaptureKeyHandlerPrivate::~CxuiCaptureKeyHandlerPrivate()
       
    65 {
       
    66     CX_DEBUG_ENTER_FUNCTION();
       
    67     listenKeys(false);
       
    68     CX_DEBUG_EXIT_FUNCTION();
       
    69 }
       
    70 
       
    71 /*!
       
    72 * Start or stop listening key events.
       
    73 * @param listen Should we start (true) or stop (false) listening key events.
       
    74 */
       
    75 void CxuiCaptureKeyHandlerPrivate::listenKeys(bool listen)
       
    76 {
       
    77     CX_DEBUG_ENTER_FUNCTION();
       
    78 
       
    79     if (listen) {
       
    80         // Protect from multiple calls
       
    81         if (mCapturedKeyUpDownHandles.empty() && mCapturedKeyHandles.empty()) {
       
    82 
       
    83             int key(0);
       
    84             foreach (key, mPrimaryCameraAutofocusKeys) {
       
    85                 CX_DEBUG(("CxuiCaptureKeyHandlerPrivate - hooking autofocus key with scan / key code: %d", key));
       
    86                 listenKey(key);
       
    87             }
       
    88             foreach (key, mPrimaryCameraCaptureKeys) {
       
    89                 CX_DEBUG(("CxuiCaptureKeyHandlerPrivate - hooking capture key with scan / key code: %d", key));
       
    90                 listenKey(key);
       
    91             }
       
    92         }
       
    93     } else {
       
    94 
       
    95         int handle(0);
       
    96         foreach (handle, mCapturedKeyUpDownHandles) {
       
    97             mWindowGroup.CancelCaptureKeyUpAndDowns(handle);
       
    98         }
       
    99         mCapturedKeyUpDownHandles.clear();
       
   100 
       
   101         foreach (handle, mCapturedKeyHandles) {
       
   102             mWindowGroup.CancelCaptureKey(handle);
       
   103         }
       
   104         mCapturedKeyHandles.clear();
       
   105     }
       
   106     CX_DEBUG_EXIT_FUNCTION();
       
   107 }
       
   108 
       
   109 bool CxuiCaptureKeyHandlerPrivate::isAutofocusKeyPressed()
       
   110 {
       
   111     return mAutofocusKeyPressed;
       
   112 }
       
   113 
       
   114 bool CxuiCaptureKeyHandlerPrivate::isCaptureKeyPressed()
       
   115 {
       
   116     return mCaptureKeyPressed;
       
   117 }
       
   118 
       
   119 bool CxuiCaptureKeyHandlerPrivate::handleKeyEvent(QEvent *event)
       
   120 {
       
   121     CX_DEBUG_ENTER_FUNCTION();
       
   122     Q_Q(CxuiCaptureKeyHandler);
       
   123     bool eventWasConsumed = false;
       
   124 
       
   125     if (event->type() == QEvent::KeyPress) {
       
   126         QKeyEvent *keyEvent = static_cast<QKeyEvent*> (event);
       
   127         if ( mPrimaryCameraAutofocusKeys.contains(keyEvent->nativeScanCode())
       
   128             && !mAutofocusKeyPressed ) {
       
   129 
       
   130             mAutofocusKeyPressed = true;
       
   131             eventWasConsumed = true;
       
   132             emit q->autofocusKeyPressed();
       
   133 
       
   134         } else if (mPrimaryCameraCaptureKeys.contains(keyEvent->nativeScanCode())
       
   135             && !mCaptureKeyPressed) {
       
   136             mCaptureKeyPressed = true;
       
   137             eventWasConsumed = true;
       
   138             emit q->captureKeyPressed();
       
   139         }
       
   140     } else if (event->type() == QEvent::KeyRelease) {
       
   141         QKeyEvent *keyEvent = static_cast<QKeyEvent*> (event);
       
   142 
       
   143         if ( mPrimaryCameraAutofocusKeys.contains(keyEvent->nativeScanCode())
       
   144             && mAutofocusKeyPressed ) {
       
   145 
       
   146             mAutofocusKeyPressed = false;
       
   147             eventWasConsumed = true;
       
   148             emit q->autofocusKeyReleased();
       
   149 
       
   150         } else if (mPrimaryCameraCaptureKeys.contains(keyEvent->nativeScanCode())
       
   151             && mCaptureKeyPressed) {
       
   152 
       
   153             mCaptureKeyPressed = false;
       
   154             eventWasConsumed = true;
       
   155             emit q->captureKeyReleased();
       
   156         }
       
   157     }
       
   158     CX_DEBUG_EXIT_FUNCTION();
       
   159     return eventWasConsumed;
       
   160 }
       
   161 
       
   162 /*!
       
   163 * Helper method to listen to given key (key code or scan code).
       
   164 * We need to listen to both "key up", "key down" and "key pressed" events to
       
   165 * get all the necessary events to handleKeyEvent(). If we e.g. just listen
       
   166 * to up/down events, the way native events are translated to QKeyEvents,
       
   167 * we only get QEvent::KeyRelease event when partially in background.
       
   168 * @param key Keycode or scancode for the key to listen. Both should be listened.
       
   169 */
       
   170 void CxuiCaptureKeyHandlerPrivate::listenKey(int key)
       
   171 {
       
   172     // Capture key down and up events
       
   173     int handle = mWindowGroup.CaptureKeyUpAndDowns(key, 0, 0, CXUI_KEY_PRIORITY);
       
   174 
       
   175     // Handle < 0 means error.
       
   176     if (handle >= 0) {
       
   177         mCapturedKeyUpDownHandles.append(handle);
       
   178     } else {
       
   179         CX_DEBUG(("[WARNING] CxuiCaptureKeyHandlerPrivate - Problem hooking to key-up/key-down with code: %d", key));
       
   180     }
       
   181 
       
   182     // Capture key press events
       
   183     handle = mWindowGroup.CaptureKey(key, 0, 0, CXUI_KEY_PRIORITY);
       
   184 
       
   185     if (handle >= 0) {
       
   186         mCapturedKeyHandles.append(handle);
       
   187     } else {
       
   188         CX_DEBUG(("[WARNING] CxuiCaptureKeyHandlerPrivate - Problem hooking to key-press with code: %d", key));
       
   189     }
       
   190 }
       
   191 
       
   192 // end of file