VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/platform/darwin/CocoaEventHelper.mm

Last change on this file was 106061, checked in by vboxsync, 3 weeks ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.4 KB
Line 
1/* $Id: CocoaEventHelper.mm 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - Declarations of utility functions for handling Darwin Cocoa specific event handling tasks.
4 */
5
6/*
7 * Copyright (C) 2009-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/* GUI includes: */
29#include "CocoaEventHelper.h"
30#include "DarwinKeyboard.h"
31
32/* External includes: */
33#import <Cocoa/Cocoa.h>
34#import <AppKit/NSEvent.h>
35#include <Carbon/Carbon.h>
36
37/* They just had to rename a whole load of constants in 10.12. Just wrap the carp up
38 in some defines for now as we need to keep building with both 10.9 and 10.13+: */
39#if MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
40# define VBOX_NSAlphaShiftKeyMask NSEventModifierFlagCapsLock
41# define VBOX_NSAlternateKeyMask NSEventModifierFlagOption
42# define VBOX_NSAppKitDefined NSEventTypeAppKitDefined
43# define VBOX_NSApplicationDefined NSEventTypeApplicationDefined
44# define VBOX_NSCommandKeyMask NSEventModifierFlagCommand
45# define VBOX_NSControlKeyMask NSEventModifierFlagControl
46# define VBOX_NSCursorUpdate NSEventTypeCursorUpdate
47# define VBOX_NSFlagsChanged NSEventTypeFlagsChanged
48# define VBOX_NSFunctionKeyMask NSEventModifierFlagFunction
49# define VBOX_NSHelpKeyMask NSEventModifierFlagHelp
50# define VBOX_NSKeyDown NSEventTypeKeyDown
51# define VBOX_NSKeyUp NSEventTypeKeyUp
52# define VBOX_NSLeftMouseDown NSEventTypeLeftMouseDown
53# define VBOX_NSLeftMouseDragged NSEventTypeLeftMouseDragged
54# define VBOX_NSLeftMouseUp NSEventTypeLeftMouseUp
55# define VBOX_NSMouseEntered NSEventTypeMouseEntered
56# define VBOX_NSMouseExited NSEventTypeMouseExited
57# define VBOX_NSMouseMoved NSEventTypeMouseMoved
58# define VBOX_NSNumericPadKeyMask NSEventModifierFlagNumericPad
59# define VBOX_NSOtherMouseDown NSEventTypeOtherMouseDown
60# define VBOX_NSOtherMouseDragged NSEventTypeOtherMouseDragged
61# define VBOX_NSOtherMouseUp NSEventTypeOtherMouseUp
62# define VBOX_NSPeriodic NSEventTypePeriodic
63# define VBOX_NSRightMouseDown NSEventTypeRightMouseDown
64# define VBOX_NSRightMouseDragged NSEventTypeRightMouseDragged
65# define VBOX_NSRightMouseUp NSEventTypeRightMouseUp
66# define VBOX_NSScrollWheel NSEventTypeScrollWheel
67# define VBOX_NSShiftKeyMask NSEventModifierFlagShift
68# define VBOX_NSSystemDefined NSEventTypeSystemDefined
69# define VBOX_NSTabletPoint NSEventTypeTabletPoint
70# define VBOX_NSTabletProximity NSEventTypeTabletProximity
71#else
72# define VBOX_NSAlphaShiftKeyMask NSAlphaShiftKeyMask
73# define VBOX_NSAlternateKeyMask NSAlternateKeyMask
74# define VBOX_NSAppKitDefined NSAppKitDefined
75# define VBOX_NSApplicationDefined NSApplicationDefined
76# define VBOX_NSCommandKeyMask NSCommandKeyMask
77# define VBOX_NSControlKeyMask NSControlKeyMask
78# define VBOX_NSCursorUpdate NSCursorUpdate
79# define VBOX_NSFlagsChanged NSFlagsChanged
80# define VBOX_NSFunctionKeyMask NSFunctionKeyMask
81# define VBOX_NSHelpKeyMask NSHelpKeyMask
82# define VBOX_NSKeyDown NSKeyDown
83# define VBOX_NSKeyUp NSKeyUp
84# define VBOX_NSLeftMouseDown NSLeftMouseDown
85# define VBOX_NSLeftMouseDragged NSLeftMouseDragged
86# define VBOX_NSLeftMouseUp NSLeftMouseUp
87# define VBOX_NSMouseEntered NSMouseEntered
88# define VBOX_NSMouseExited NSMouseExited
89# define VBOX_NSMouseMoved NSMouseMoved
90# define VBOX_NSNumericPadKeyMask NSNumericPadKeyMask
91# define VBOX_NSOtherMouseDown NSOtherMouseDown
92# define VBOX_NSOtherMouseDragged NSOtherMouseDragged
93# define VBOX_NSOtherMouseUp NSOtherMouseUp
94# define VBOX_NSPeriodic NSPeriodic
95# define VBOX_NSRightMouseDown NSRightMouseDown
96# define VBOX_NSRightMouseDragged NSRightMouseDragged
97# define VBOX_NSRightMouseUp NSRightMouseUp
98# define VBOX_NSScrollWheel NSScrollWheel
99# define VBOX_NSShiftKeyMask NSShiftKeyMask
100# define VBOX_NSSystemDefined NSSystemDefined
101# define VBOX_NSTabletPoint NSTabletPoint
102# define VBOX_NSTabletProximity NSTabletProximity
103#endif
104
105uint32_t darwinEventModifierFlagsXlated(ConstNativeNSEventRef pEvent)
106{
107 NSUInteger fCocoa = [pEvent modifierFlags];
108 uint32_t fCarbon = 0;
109 if (fCocoa)
110 {
111 if (fCocoa & VBOX_NSAlphaShiftKeyMask)
112 fCarbon |= alphaLock;
113 if (fCocoa & (VBOX_NSShiftKeyMask | NX_DEVICELSHIFTKEYMASK | NX_DEVICERSHIFTKEYMASK))
114 {
115 if (fCocoa & (NX_DEVICERSHIFTKEYMASK | NX_DEVICERSHIFTKEYMASK))
116 {
117 if (fCocoa & NX_DEVICERSHIFTKEYMASK)
118 fCarbon |= rightShiftKey;
119 if (fCocoa & NX_DEVICELSHIFTKEYMASK)
120 fCarbon |= shiftKey;
121 }
122 else
123 fCarbon |= shiftKey;
124 }
125
126 if (fCocoa & (VBOX_NSControlKeyMask | NX_DEVICELCTLKEYMASK | NX_DEVICERCTLKEYMASK))
127 {
128 if (fCocoa & (NX_DEVICELCTLKEYMASK | NX_DEVICERCTLKEYMASK))
129 {
130 if (fCocoa & NX_DEVICERCTLKEYMASK)
131 fCarbon |= rightControlKey;
132 if (fCocoa & NX_DEVICELCTLKEYMASK)
133 fCarbon |= controlKey;
134 }
135 else
136 fCarbon |= controlKey;
137 }
138
139 if (fCocoa & (VBOX_NSAlternateKeyMask | NX_DEVICELALTKEYMASK | NX_DEVICERALTKEYMASK))
140 {
141 if (fCocoa & (NX_DEVICELALTKEYMASK | NX_DEVICERALTKEYMASK))
142 {
143 if (fCocoa & NX_DEVICERALTKEYMASK)
144 fCarbon |= rightOptionKey;
145 if (fCocoa & NX_DEVICELALTKEYMASK)
146 fCarbon |= optionKey;
147 }
148 else
149 fCarbon |= optionKey;
150 }
151
152 if (fCocoa & (VBOX_NSCommandKeyMask | NX_DEVICELCMDKEYMASK | NX_DEVICERCMDKEYMASK))
153 {
154 if (fCocoa & (NX_DEVICELCMDKEYMASK | NX_DEVICERCMDKEYMASK))
155 {
156 if (fCocoa & NX_DEVICERCMDKEYMASK)
157 fCarbon |= kEventKeyModifierRightCmdKeyMask;
158 if (fCocoa & NX_DEVICELCMDKEYMASK)
159 fCarbon |= cmdKey;
160 }
161 else
162 fCarbon |= cmdKey;
163 }
164
165 /*
166 if (fCocoa & VBOX_NSNumericPadKeyMask)
167 fCarbon |= ???;
168
169 if (fCocoa & VBOX_NSHelpKeyMask)
170 fCarbon |= ???;
171
172 if (fCocoa & VBOX_NSFunctionKeyMask)
173 fCarbon |= ???;
174 */
175 }
176
177 return fCarbon;
178}
179
180const char *darwinEventTypeName(unsigned long enmEventType)
181{
182 switch (enmEventType)
183 {
184#define EVT_CASE(nm) case nm: return #nm
185 EVT_CASE(VBOX_NSLeftMouseDown);
186 EVT_CASE(VBOX_NSLeftMouseUp);
187 EVT_CASE(VBOX_NSRightMouseDown);
188 EVT_CASE(VBOX_NSRightMouseUp);
189 EVT_CASE(VBOX_NSMouseMoved);
190 EVT_CASE(VBOX_NSLeftMouseDragged);
191 EVT_CASE(VBOX_NSRightMouseDragged);
192 EVT_CASE(VBOX_NSMouseEntered);
193 EVT_CASE(VBOX_NSMouseExited);
194 EVT_CASE(VBOX_NSKeyDown);
195 EVT_CASE(VBOX_NSKeyUp);
196 EVT_CASE(VBOX_NSFlagsChanged);
197 EVT_CASE(VBOX_NSAppKitDefined);
198 EVT_CASE(VBOX_NSSystemDefined);
199 EVT_CASE(VBOX_NSApplicationDefined);
200 EVT_CASE(VBOX_NSPeriodic);
201 EVT_CASE(VBOX_NSCursorUpdate);
202 EVT_CASE(VBOX_NSScrollWheel);
203 EVT_CASE(VBOX_NSTabletPoint);
204 EVT_CASE(VBOX_NSTabletProximity);
205 EVT_CASE(VBOX_NSOtherMouseDown);
206 EVT_CASE(VBOX_NSOtherMouseUp);
207 EVT_CASE(VBOX_NSOtherMouseDragged);
208#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
209 EVT_CASE(NSEventTypeGesture);
210 EVT_CASE(NSEventTypeMagnify);
211 EVT_CASE(NSEventTypeSwipe);
212 EVT_CASE(NSEventTypeRotate);
213 EVT_CASE(NSEventTypeBeginGesture);
214 EVT_CASE(NSEventTypeEndGesture);
215#endif
216#undef EVT_CASE
217 default:
218 return "Unknown!";
219 }
220}
221
222void darwinPrintEvent(const char *pszPrefix, ConstNativeNSEventRef pEvent)
223{
224 NSEventType enmEventType = [pEvent type];
225 NSUInteger fEventMask = [pEvent modifierFlags];
226 NSWindow *pEventWindow = [pEvent window];
227 NSInteger iEventWindow = [pEvent windowNumber];
228#if MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
229 NSGraphicsContext *pEventGraphicsContext = nil; /* NSEvent::context is deprecated and said to always return nil. */
230#else
231 NSGraphicsContext *pEventGraphicsContext = [pEvent context];
232#endif
233
234 printf("%s%p: Type=%lu Modifiers=%08lx pWindow=%p #Wnd=%ld pGraphCtx=%p %s\n",
235 pszPrefix, (void*)pEvent, (unsigned long)enmEventType, (unsigned long)fEventMask, (void*)pEventWindow,
236 (long)iEventWindow, (void*)pEventGraphicsContext, darwinEventTypeName(enmEventType));
237
238 /* Dump type specific info: */
239 switch (enmEventType)
240 {
241 case VBOX_NSLeftMouseDown:
242 case VBOX_NSLeftMouseUp:
243 case VBOX_NSRightMouseDown:
244 case VBOX_NSRightMouseUp:
245 case VBOX_NSMouseMoved:
246
247 case VBOX_NSLeftMouseDragged:
248 case VBOX_NSRightMouseDragged:
249 case VBOX_NSMouseEntered:
250 case VBOX_NSMouseExited:
251 break;
252
253 case VBOX_NSKeyDown:
254 case VBOX_NSKeyUp:
255 {
256 NSUInteger i;
257 NSUInteger cch;
258 NSString *pChars = [pEvent characters];
259 NSString *pCharsIgnMod = [pEvent charactersIgnoringModifiers];
260 BOOL fIsARepeat = [pEvent isARepeat];
261 unsigned short KeyCode = [pEvent keyCode];
262
263 printf(" KeyCode=%04x isARepeat=%d", KeyCode, fIsARepeat);
264 if (pChars)
265 {
266 cch = [pChars length];
267 printf(" characters={");
268 for (i = 0; i < cch; i++)
269 printf(i == 0 ? "%02x" : ",%02x", [pChars characterAtIndex: i]);
270 printf("}");
271 }
272
273 if (pCharsIgnMod)
274 {
275 cch = [pCharsIgnMod length];
276 printf(" charactersIgnoringModifiers={");
277 for (i = 0; i < cch; i++)
278 printf(i == 0 ? "%02x" : ",%02x", [pCharsIgnMod characterAtIndex: i]);
279 printf("}");
280 }
281 printf("\n");
282 break;
283 }
284
285 case VBOX_NSFlagsChanged:
286 {
287 NSUInteger fOddBits = VBOX_NSAlphaShiftKeyMask | VBOX_NSShiftKeyMask | VBOX_NSControlKeyMask | VBOX_NSAlternateKeyMask
288 | VBOX_NSCommandKeyMask | VBOX_NSNumericPadKeyMask | VBOX_NSHelpKeyMask | VBOX_NSFunctionKeyMask
289 | NX_DEVICELCTLKEYMASK | NX_DEVICELSHIFTKEYMASK | NX_DEVICERSHIFTKEYMASK
290 | NX_DEVICELCMDKEYMASK | NX_DEVICERCMDKEYMASK | NX_DEVICELALTKEYMASK
291 | NX_DEVICERALTKEYMASK | NX_DEVICERCTLKEYMASK;
292
293 printf(" KeyCode=%04x", (int)[pEvent keyCode]);
294#define PRINT_MOD(cnst, nm) do { if (fEventMask & (cnst)) printf(" %s", #nm); } while (0)
295 /* device-independent: */
296 PRINT_MOD(VBOX_NSAlphaShiftKeyMask, "AlphaShift");
297 PRINT_MOD(VBOX_NSShiftKeyMask, "Shift");
298 PRINT_MOD(VBOX_NSControlKeyMask, "Ctrl");
299 PRINT_MOD(VBOX_NSAlternateKeyMask, "Alt");
300 PRINT_MOD(VBOX_NSCommandKeyMask, "Cmd");
301 PRINT_MOD(VBOX_NSNumericPadKeyMask, "NumLock");
302 PRINT_MOD(VBOX_NSHelpKeyMask, "Help");
303 PRINT_MOD(VBOX_NSFunctionKeyMask, "Fn");
304 /* device-dependent (sort of): */
305 PRINT_MOD(NX_DEVICELCTLKEYMASK, "$L-Ctrl");
306 PRINT_MOD(NX_DEVICELSHIFTKEYMASK, "$L-Shift");
307 PRINT_MOD(NX_DEVICERSHIFTKEYMASK, "$R-Shift");
308 PRINT_MOD(NX_DEVICELCMDKEYMASK, "$L-Cmd");
309 PRINT_MOD(NX_DEVICERCMDKEYMASK, "$R-Cmd");
310 PRINT_MOD(NX_DEVICELALTKEYMASK, "$L-Alt");
311 PRINT_MOD(NX_DEVICERALTKEYMASK, "$R-Alt");
312 PRINT_MOD(NX_DEVICERCTLKEYMASK, "$R-Ctrl");
313#undef PRINT_MOD
314
315 fOddBits = fEventMask & ~fOddBits;
316 if (fOddBits)
317 printf(" fOddBits=%#08lx", (unsigned long)fOddBits);
318#undef KNOWN_BITS
319 printf("\n");
320 break;
321 }
322
323 case VBOX_NSAppKitDefined:
324 case VBOX_NSSystemDefined:
325 case VBOX_NSApplicationDefined:
326 case VBOX_NSPeriodic:
327 case VBOX_NSCursorUpdate:
328 case VBOX_NSScrollWheel:
329 case VBOX_NSTabletPoint:
330 case VBOX_NSTabletProximity:
331 case VBOX_NSOtherMouseDown:
332 case VBOX_NSOtherMouseUp:
333 case VBOX_NSOtherMouseDragged:
334#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
335 case NSEventTypeGesture:
336 case NSEventTypeMagnify:
337 case NSEventTypeSwipe:
338 case NSEventTypeRotate:
339 case NSEventTypeBeginGesture:
340 case NSEventTypeEndGesture:
341#endif
342 default:
343 printf(" Unknown!\n");
344 break;
345 }
346}
347
348void darwinPostStrippedMouseEvent(ConstNativeNSEventRef pEvent)
349{
350 /* Create and post new stripped event: */
351 NSEvent *pNewEvent = [NSEvent mouseEventWithType:[pEvent type]
352 location:[pEvent locationInWindow]
353 modifierFlags:0
354 timestamp:[pEvent timestamp] // [NSDate timeIntervalSinceReferenceDate] ?
355 windowNumber:[pEvent windowNumber]
356#if MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
357 context:nil /* NSEvent::context is deprecated and said to always return nil. */
358#else
359 context:[pEvent context]
360#endif
361 eventNumber:[pEvent eventNumber]
362 clickCount:[pEvent clickCount]
363 pressure:[pEvent pressure]];
364 [NSApp postEvent:pNewEvent atStart:YES];
365}
366
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