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 startListeningKeys(); |
|
61 CX_DEBUG_EXIT_FUNCTION(); |
|
62 } |
|
63 |
|
64 CxuiCaptureKeyHandlerPrivate::~CxuiCaptureKeyHandlerPrivate() |
|
65 { |
|
66 CX_DEBUG_ENTER_FUNCTION(); |
|
67 stopListeningKeys(); |
|
68 CX_DEBUG_EXIT_FUNCTION(); |
|
69 } |
|
70 |
|
71 /*! |
|
72 * Starts listening key events. |
|
73 */ |
|
74 void CxuiCaptureKeyHandlerPrivate::startListeningKeys() |
|
75 { |
|
76 CX_DEBUG_ENTER_FUNCTION(); |
|
77 // Protect from multiple calls |
|
78 if (mCapturedKeyUpDownHandles.empty() && mCapturedKeyHandles.empty()) { |
|
79 |
|
80 int key(0); |
|
81 foreach (key, mPrimaryCameraAutofocusKeys) { |
|
82 CX_DEBUG(("CxuiCaptureKeyHandlerPrivate - hooking autofocus key with scan / key code: %d", key)); |
|
83 listenKey(key); |
|
84 } |
|
85 foreach (key, mPrimaryCameraCaptureKeys) { |
|
86 CX_DEBUG(("CxuiCaptureKeyHandlerPrivate - hooking capture key with scan / key code: %d", key)); |
|
87 listenKey(key); |
|
88 } |
|
89 } |
|
90 CX_DEBUG_EXIT_FUNCTION(); |
|
91 } |
|
92 |
|
93 /*! |
|
94 * Stops listening key events. |
|
95 */ |
|
96 void CxuiCaptureKeyHandlerPrivate::stopListeningKeys() |
|
97 { |
|
98 CX_DEBUG_ENTER_FUNCTION(); |
|
99 int handle(0); |
|
100 foreach (handle, mCapturedKeyUpDownHandles) { |
|
101 mWindowGroup.CancelCaptureKeyUpAndDowns(handle); |
|
102 } |
|
103 mCapturedKeyUpDownHandles.clear(); |
|
104 |
|
105 foreach (handle, mCapturedKeyHandles) { |
|
106 mWindowGroup.CancelCaptureKey(handle); |
|
107 } |
|
108 mCapturedKeyHandles.clear(); |
|
109 CX_DEBUG_EXIT_FUNCTION(); |
|
110 } |
|
111 |
|
112 bool CxuiCaptureKeyHandlerPrivate::isAutofocusKeyPressed() |
|
113 { |
|
114 return mAutofocusKeyPressed; |
|
115 } |
|
116 |
|
117 bool CxuiCaptureKeyHandlerPrivate::isCaptureKeyPressed() |
|
118 { |
|
119 return mCaptureKeyPressed; |
|
120 } |
|
121 |
|
122 bool CxuiCaptureKeyHandlerPrivate::handleKeyEvent(QEvent *event) |
|
123 { |
|
124 CX_DEBUG_ENTER_FUNCTION(); |
|
125 Q_Q(CxuiCaptureKeyHandler); |
|
126 bool eventWasConsumed = false; |
|
127 |
|
128 if (event->type() == QEvent::KeyPress) { |
|
129 QKeyEvent *keyEvent = static_cast<QKeyEvent*> (event); |
|
130 if ( mPrimaryCameraAutofocusKeys.contains(keyEvent->nativeScanCode()) |
|
131 && !mAutofocusKeyPressed ) { |
|
132 |
|
133 mAutofocusKeyPressed = true; |
|
134 eventWasConsumed = true; |
|
135 emit q->autofocusKeyPressed(); |
|
136 |
|
137 } else if (mPrimaryCameraCaptureKeys.contains(keyEvent->nativeScanCode()) |
|
138 && !mCaptureKeyPressed) { |
|
139 mCaptureKeyPressed = true; |
|
140 eventWasConsumed = true; |
|
141 emit q->captureKeyPressed(); |
|
142 } |
|
143 } else if (event->type() == QEvent::KeyRelease) { |
|
144 QKeyEvent *keyEvent = static_cast<QKeyEvent*> (event); |
|
145 |
|
146 if ( mPrimaryCameraAutofocusKeys.contains(keyEvent->nativeScanCode()) |
|
147 && mAutofocusKeyPressed ) { |
|
148 |
|
149 mAutofocusKeyPressed = false; |
|
150 eventWasConsumed = true; |
|
151 emit q->autofocusKeyReleased(); |
|
152 |
|
153 } else if (mPrimaryCameraCaptureKeys.contains(keyEvent->nativeScanCode()) |
|
154 && mCaptureKeyPressed) { |
|
155 |
|
156 mCaptureKeyPressed = false; |
|
157 eventWasConsumed = true; |
|
158 emit q->captureKeyReleased(); |
|
159 } |
|
160 } |
|
161 CX_DEBUG_EXIT_FUNCTION(); |
|
162 return eventWasConsumed; |
|
163 } |
|
164 |
|
165 /*! |
|
166 * Helper method to listen to given key (key code or scan code). |
|
167 * We need to listen to both "key up", "key down" and "key pressed" events to |
|
168 * get all the necessary events to handleKeyEvent(). If we e.g. just listen |
|
169 * to up/down events, the way native events are translated to QKeyEvents, |
|
170 * we only get QEvent::KeyRelease event when partially in background. |
|
171 * @param key Keycode or scancode for the key to listen. Both should be listened. |
|
172 */ |
|
173 void CxuiCaptureKeyHandlerPrivate::listenKey(int key) |
|
174 { |
|
175 // Capture key down and up events |
|
176 int handle = mWindowGroup.CaptureKeyUpAndDowns(key, 0, 0, CXUI_KEY_PRIORITY); |
|
177 |
|
178 // Handle < 0 means error. |
|
179 if (handle >= 0) { |
|
180 mCapturedKeyUpDownHandles.append(handle); |
|
181 } else { |
|
182 CX_DEBUG(("[WARNING] CxuiCaptureKeyHandlerPrivate - Problem hooking to key-up/key-down with code: %d", key)); |
|
183 } |
|
184 |
|
185 // Capture key press events |
|
186 handle = mWindowGroup.CaptureKey(key, 0, 0, CXUI_KEY_PRIORITY); |
|
187 |
|
188 if (handle >= 0) { |
|
189 mCapturedKeyHandles.append(handle); |
|
190 } else { |
|
191 CX_DEBUG(("[WARNING] CxuiCaptureKeyHandlerPrivate - Problem hooking to key-press with code: %d", key)); |
|
192 } |
|
193 } |
|
194 |
|
195 // end of file |
|