VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIKeyboardHandler.h

Last change on this file was 106611, checked in by vboxsync, 6 weeks ago

FE/Qt: bugref:10407. Handle machine view key press event in keyboard handler.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 KB
Line 
1/* $Id: UIKeyboardHandler.h 106611 2024-10-23 07:50:23Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIKeyboardHandler class declaration.
4 */
5
6/*
7 * Copyright (C) 2010-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#ifndef FEQT_INCLUDED_SRC_runtime_UIKeyboardHandler_h
29#define FEQT_INCLUDED_SRC_runtime_UIKeyboardHandler_h
30#ifndef RT_WITHOUT_PRAGMA_ONCE
31# pragma once
32#endif
33
34/* Qt includes: */
35#include <QMap>
36#include <QObject>
37
38/* GUI includes: */
39#include "UIExtraDataDefs.h"
40
41/* Other VBox includes: */
42#include <VBox/com/defs.h>
43
44/* External includes: */
45#ifdef VBOX_WS_MAC
46# include <Carbon/Carbon.h>
47# include <CoreFoundation/CFBase.h>
48#endif /* VBOX_WS_MAC */
49
50/* Forward declarations: */
51class QWidget;
52class UIActionPool;
53class UIMachine;
54class UIMachineLogic;
55class UIMachineView;
56class UIMachineWindow;
57#ifdef VBOX_WS_WIN
58class WinAltGrMonitor;
59#endif
60#ifdef VBOX_WS_NIX
61# include <xcb/xcb.h>
62#endif
63
64
65/* Delegate to control VM keyboard functionality: */
66class UIKeyboardHandler : public QObject
67{
68 Q_OBJECT;
69
70signals:
71
72 /** Notifies listeners about state-change. */
73 void sigStateChange(int iState);
74
75public:
76
77 /* Factory functions to create/destroy keyboard-handler: */
78 static UIKeyboardHandler* create(UIMachineLogic *pMachineLogic, UIVisualStateType visualStateType);
79 static void destroy(UIKeyboardHandler *pKeyboardHandler);
80
81 /* Prepare/cleanup listeners: */
82 void prepareListener(ulong uScreenId, UIMachineWindow *pMachineWindow);
83 void cleanupListener(ulong uScreenId);
84
85 /** Captures the keyboard for @a uScreenId. */
86 void captureKeyboard(ulong uScreenId);
87 /** Finalises keyboard capture. */
88 bool finaliseCaptureKeyboard();
89 /** Releases the keyboard. */
90 void releaseKeyboard();
91
92 void releaseAllPressedKeys(bool aReleaseHostKey = true);
93
94 /* Current keyboard state: */
95 int state() const;
96
97 /* Some getters required by side-code: */
98 bool isHostKeyPressed() const { return m_fHostComboPressed; }
99#ifdef VBOX_WS_MAC
100 bool isHostKeyAlone() const { return m_bIsHostComboAlone; }
101 bool isKeyboardGrabbed() const { return m_iKeyboardHookViewIndex != -1; }
102#endif /* VBOX_WS_MAC */
103
104#ifdef VBOX_WITH_DEBUGGER_GUI
105 /* For the debugger. */
106 void setDebuggerActive(bool aActive = true);
107#endif
108
109#ifdef VBOX_WS_WIN
110 /** Tells the keyboard event handler to skip host keyboard events.
111 * Used for HID LEDs sync when on Windows host a keyboard event
112 * is generated in order to change corresponding LED. */
113 void winSkipKeyboardEvents(bool fSkip);
114#endif /* VBOX_WS_WIN */
115
116 /** Qt5: Performs pre-processing of all the native events. */
117 bool nativeEventFilter(void *pMessage, ulong uScreenId);
118
119 /** Called whenever host key press/release scan codes are inserted to the guest.
120 * @a bPressed is true for press and false for release inserts. */
121 void setHostKeyComboPressedFlag(bool bPressed);
122
123#ifdef VBOX_WS_NIX
124 void handleKeyEvent(quint32 nativeScanCode, bool fRelease);
125#endif
126
127protected slots:
128
129 /* Machine state-change handler: */
130 virtual void sltMachineStateChanged();
131
132 /** Finalises keyboard capture. */
133 void sltFinaliseCaptureKeyboard();
134
135protected:
136
137 /* Keyboard-handler constructor/destructor: */
138 UIKeyboardHandler(UIMachineLogic *pMachineLogic);
139 virtual ~UIKeyboardHandler();
140
141 /* Prepare helpers: */
142 virtual void prepareCommon();
143 virtual void loadSettings();
144
145 /* Cleanup helpers: */
146 //virtual void saveSettings() {}
147 virtual void cleanupCommon();
148
149 /* Common getters: */
150 UIMachineLogic *machineLogic() const { return m_pMachineLogic; }
151 UIActionPool *actionPool() const;
152 UIMachine *uimachine() const;
153
154 /* Event handler for registered machine-view(s): */
155 bool eventFilter(QObject *pWatchedObject, QEvent *pEvent) RT_OVERRIDE;
156
157#if defined(VBOX_WS_MAC)
158 /** Mac: Performs initial pre-processing of all the native keyboard events. */
159 static bool macKeyboardProc(const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser);
160 /** Mac: Performs initial pre-processing of all the native keyboard events. */
161 bool macKeyboardEvent(const void *pvCocoaEvent, EventRef inEvent);
162#elif defined(VBOX_WS_WIN)
163 /** Win: Performs initial pre-processing of all the native keyboard events. */
164 static LRESULT CALLBACK winKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) RT_NOTHROW_PROTO;
165 /** Win: Performs initial pre-processing of all the native keyboard events. */
166 bool winKeyboardEvent(UINT msg, const KBDLLHOOKSTRUCT &event);
167#endif /* VBOX_WS_WIN */
168
169 bool keyEventCADHandled(uint8_t uScan);
170 bool keyEventHandleNormal(int iKey, uint8_t uScan, int fFlags, LONG *pCodes, uint *puCodesCount);
171 bool keyEventHostComboHandled(int iKey, wchar_t *pUniKey, bool isHostComboStateChanged, bool *pfResult);
172 void keyEventHandleHostComboRelease(ulong uScreenId);
173 /* Separate function to handle most of existing keyboard-events: */
174 bool keyEvent(int iKey, uint8_t uScan, int fFlags, ulong uScreenId, wchar_t *pUniKey = 0);
175 bool processHotKey(int iHotKey, wchar_t *pUniKey);
176
177 /* Private helpers: */
178 void fixModifierState(LONG *piCodes, uint *puCount);
179 void saveKeyStates();
180 void sendChangedKeyStates();
181 bool isAutoCaptureDisabled();
182 void setAutoCaptureDisabled(bool fIsAutoCaptureDisabled);
183 bool autoCaptureSetGlobally();
184 bool viewHasFocus(ulong uScreenId);
185 bool isSessionRunning();
186 bool isSessionStuck();
187
188 UIMachineWindow* isItListenedWindow(QObject *pWatchedObject) const;
189 UIMachineView* isItListenedView(QObject *pWatchedObject) const;
190
191 /* Machine logic parent: */
192 UIMachineLogic *m_pMachineLogic;
193
194 /* Registered machine-window(s): */
195 QMap<ulong, UIMachineWindow*> m_windows;
196 /* Registered machine-view(s): */
197 QMap<ulong, UIMachineView*> m_views;
198
199 /* Other keyboard variables: */
200 int m_iKeyboardCaptureViewIndex;
201
202 uint8_t m_pressedKeys[128];
203 uint8_t m_pressedKeysCopy[128];
204
205 QMap<int, uint8_t> m_pressedHostComboKeys;
206
207 bool m_fKeyboardCaptured : 1;
208 bool m_fHostComboPressed : 1;
209 bool m_bIsHostComboAlone : 1;
210 bool m_bIsHostComboProcessed : 1;
211 bool m_fPassCADtoGuest : 1;
212 bool m_fHostKeyComboPressInserted : 1;
213 /** Whether the debugger is active.
214 * Currently only affects auto capturing. */
215 bool m_fDebuggerActive : 1;
216
217 /** Holds the keyboard hook view index. */
218 int m_iKeyboardHookViewIndex;
219
220#if defined(VBOX_WS_MAC)
221 /** Mac: Holds the current modifiers key mask. */
222 UInt32 m_uDarwinKeyModifiers;
223#elif defined(VBOX_WS_WIN)
224 /** Win: Currently this is used in winKeyboardEvent() only. */
225 bool m_fIsHostkeyInCapture;
226 /** Win: Holds whether the keyboard event filter should ignore keyboard events. */
227 bool m_fSkipKeyboardEvents;
228 /** Win: Holds the keyboard hook instance. */
229 HHOOK m_keyboardHook;
230 /** Win: Holds the object monitoring key event stream for problematic AltGr events. */
231 WinAltGrMonitor *m_pAltGrMonitor;
232 /** Win: Holds the keyboard handler reference to be accessible from the keyboard hook. */
233 static UIKeyboardHandler *m_spKeyboardHandler;
234#elif defined(VBOX_WS_NIX)
235 /** The root window at the time we grab the mouse buttons. */
236 xcb_window_t m_hButtonGrabWindow;
237#endif /* VBOX_WS_NIX */
238};
239
240#endif /* !FEQT_INCLUDED_SRC_runtime_UIKeyboardHandler_h */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette