VirtualBox

Changeset 30114 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Jun 9, 2010 12:40:16 PM (15 years ago)
Author:
vboxsync
Message:

FE/Qt4-OSX: Rewrote the NSApplication interface and event handling to be more
class oriented. Added a global NSAutoreleasePool for the
initialization/uninitialization time. So there is no need for one in every new
method anymore. Use correct bit mask when registering for global events.

Location:
trunk/src/VBox/Frontends/VirtualBox
Files:
9 edited
2 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/Makefile.kmk

    r30023 r30114  
    523523 VirtualBox_DEFS += VBOX_DARWIN_USE_NATIVE_CONTROLS
    524524 VirtualBox_SOURCES.darwin += \
    525         src/darwin/VBoxCocoaApplication.m \
     525        src/darwin/UICocoaApplication.mm \
    526526        src/darwin/VBoxUtils-darwin-cocoa.mm \
    527527        src/darwin/VBoxCocoaSpecialControls.mm
     
    850850        src/darwin/DarwinKeyboard.cpp
    851851tstDarwinKeyboard_SOURCES.amd64 = \
    852         src/darwin/VBoxCocoaApplication.m
     852        src/darwin/UICocoaApplication.mm
    853853tstDarwinKeyboard_LDFLAGS = -framework IOKit -framework Carbon -framework AppKit
    854854tstDarwinKeyboard_LIBS = \
  • trunk/src/VBox/Frontends/VirtualBox/src/darwin/DarwinKeyboard.cpp

    r28800 r30114  
     1/* $Id$ */
    12/** @file
    23 * Common GUI Library - Darwin Keyboard routines.
     
    4445
    4546#ifndef USE_HID_FOR_MODIFIERS
    46 # include "VBoxCocoaApplication.h"
     47# include "VBoxUtils-darwin.h"
    4748#endif
    4849
     
    986987         */
    987988        AssertPtr(pvCocoaEvent);
    988         //VBoxCocoaApplication_printEvent("dbg-adjMods: ", pvCocoaEvent);
    989         uint32_t fAltModifiers = VBoxCocoaApplication_getEventModifierFlagsXlated(pvCocoaEvent);
     989        //::darwinPrintEvent("dbg-adjMods: ", pvCocoaEvent);
     990        uint32_t fAltModifiers = ::darwinEventModifierFlagsXlated(pvCocoaEvent);
    990991
    991992#else  /* USE_HID_FOR_MODIFIERS */
  • trunk/src/VBox/Frontends/VirtualBox/src/darwin/UICocoaApplication.h

    r30085 r30114  
    1 /* $Id$ */
    21/** @file
    3  * VBoxCocoaApplication - NSApplication subclass for handling -sendEvent.
     2 * UICocoaApplication - C++ interface to NSApplication for handling -sendEvent.
    43 */
    54
    65/*
    7  * Copyright (C) 2009 Oracle Corporation
     6 * Copyright (C) 2009-2010 Oracle Corporation
    87 *
    98 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    1918#define ___darwin_VBoxCocoaApplication_h
    2019
    21 #include <iprt/cdefs.h>
    22 #ifdef __OBJC__
    23 # import <AppKit/NSApplication.h>
    24 #endif
    25 
    26 RT_C_DECLS_BEGIN
     20#include "VBoxCocoaHelper.h"
     21ADD_COCOA_NATIVE_REF(UICocoaApplicationPrivate);
     22ADD_COCOA_NATIVE_REF(NSAutoreleasePool);
    2723
    2824/** Event handler callback.
     
    3430typedef bool (*PFNVBOXCACALLBACK)(const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser);
    3531
     32/* C++ singleton for our private NSApplication object */
     33class UICocoaApplication
     34{
     35public:
     36    static UICocoaApplication* instance();
     37    ~UICocoaApplication();
    3638
    37 #ifdef __OBJC__
     39    void registerForNativeEvents(uint32_t fMask, PFNVBOXCACALLBACK pfnCallback, void *pvUser);
     40    void unregisterForNativeEvents(uint32_t fMask, PFNVBOXCACALLBACK pfnCallback, void *pvUser);
    3841
    39 /** Structure for tracking a callback. */
    40 typedef struct VBOXCAENTRY
    41 {
    42     /** Mask of events to send to this callback. */
    43     uint32_t            fMask;
    44     /** The callback. */
    45     PFNVBOXCACALLBACK   pfnCallback;
    46     /** The user argument. */
    47     void               *pvUser;
    48 } VBOXCAENTRY;
    49 typedef VBOXCAENTRY *PVBOXCAENTRY;
    50 typedef VBOXCAENTRY const *PCVBOXCAENTRY;
     42private:
     43    UICocoaApplication();
     44    static UICocoaApplication *m_pInstance;
     45    NativeUICocoaApplicationPrivateRef m_pNative;
     46    NativeNSAutoreleasePoolRef m_pPool;
     47};
    5148
     49#endif /* ___darwin_VBoxCocoaApplication_h */
    5250
    53 /**
    54  * Subclass for intercepting sendEvent messages.
    55  */
    56 @interface VBoxCocoaApplication : NSApplication
    57 {
    58     /** The event mask for which there currently are callbacks. */
    59     uint32_t    m_fMask;
    60     /** The number of current callbacks. */
    61     uint32_t    m_cCallbacks;
    62     /** Array of callbacks. */
    63     VBOXCAENTRY m_aCallbacks[4];
    64 }
    65 - (void)sendEvent:(NSEvent *)theEvent;
    66 - (void)setCallback:(uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser;
    67 - (void)unsetCallback:(uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser;
    68 
    69 @end /* @interface VBoxCocoaApplication */
    70 
    71 extern VBoxCocoaApplication *g_pVBoxCocoaApp;
    72 
    73 #endif /* __OBJC__ */
    74 
    75 /** @name The C/C++ interface.
    76  *
    77  * @remarks This is a bit illogical as both NSApplication and NSEvent stuff
    78  *          ended up here...
    79  * @{
    80  */
    81 void VBoxCocoaApplication_sharedApplication(void);
    82 void VBoxCocoaApplication_setCallback(uint32_t fMask, PFNVBOXCACALLBACK pfnCallback, void *pvUser);
    83 void VBoxCocoaApplication_unsetCallback(uint32_t fMask, PFNVBOXCACALLBACK pfnCallback, void *pvUser);
    84 unsigned long VBoxCocoaApplication_getEventModifierFlags(const void *pvEvent);
    85 uint32_t VBoxCocoaApplication_getEventModifierFlagsXlated(const void *pvEvent);
    86 void VBoxCocoaApplication_setMouseCoalescingEnabled(bool fEnabled);
    87 const char *VBoxCocoaApplication_eventTypeName(unsigned long eEvtType);
    88 bool VBoxCocoaApplication_isApplicationCommand(const void *pvEvent);
    89 void VBoxCocoaApplication_printEvent(const char *pszPrefix, const void *pvEvent);
    90 /** @} */
    91 
    92 RT_C_DECLS_END
    93 
    94 #endif
    95 
  • trunk/src/VBox/Frontends/VirtualBox/src/darwin/UICocoaApplication.mm

    r30085 r30114  
    11/* $Id$ */
    22/** @file
    3  * VBoxCocoaApplication - NSApplication subclass for handling -sendEvent.
     3 * UICocoaApplication - C++ interface to NSApplication for handling -sendEvent.
    44 */
    55
    66/*
    7  * Copyright (C) 2009 Oracle Corporation
     7 * Copyright (C) 2009-2010 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    1616 */
    1717
    18 /*******************************************************************************
    19 *   Header Files                                                               *
    20 *******************************************************************************/
    21 #include "VBoxCocoaApplication.h"
    22 #include "DarwinKeyboard.h"
     18/* Local includes */
     19#include "UICocoaApplication.h"
     20#include "VBoxUtils-darwin.h"
     21
     22/* Global includes */
     23#import <AppKit/NSEvent.h>
     24#import <AppKit/NSApplication.h>
     25#import <Foundation/NSArray.h>
     26
    2327#include <iprt/assert.h>
    24 #import <AppKit/NSWindow.h>
    25 #import <AppKit/NSEvent.h>
    26 
    27 #include <Carbon/Carbon.h>
    28 
    29 #include <stdio.h>
    30 
    31 
    32 /*******************************************************************************
    33 *   Global Variables                                                           *
    34 *******************************************************************************/
    35 /** Pointer to the VBoxCocoaApplication instance.
    36  * This is also available thru NSApp, but this way there is no need to cast any thing.
    37  */
    38 VBoxCocoaApplication *g_pVBoxCocoaApp = NULL;
    39 
    40 @implementation VBoxCocoaApplication
    41 
    42 
     28
     29/** Class for tracking a callback. */
     30@interface CallbackData: NSObject
     31{
     32@public
     33    /** Mask of events to send to this callback. */
     34    uint32_t            fMask;
     35    /** The callback. */
     36    PFNVBOXCACALLBACK   pfnCallback;
     37    /** The user argument. */
     38    void               *pvUser;
     39}
     40- (id) initWithMask:(uint32)mask callback:(PFNVBOXCACALLBACK)callback user:(void*)user;
     41@end /* @interface CallbackData  */
     42
     43@implementation CallbackData
     44- (id) initWithMask:(uint32)mask callback:(PFNVBOXCACALLBACK)callback user:(void*)user
     45{
     46    self = [super init];
     47    if (self)
     48    {
     49        fMask = mask;
     50        pfnCallback = callback;
     51        pvUser =  user;
     52    }
     53    return self;
     54}
     55@end /* @implementation CallbackData  */
     56
     57/** Class for event handling */
     58@interface UICocoaApplicationPrivate: NSApplication
     59{
     60    /** The event mask for which there currently are callbacks. */
     61    uint32_t        m_fMask;
     62    /** Array of callbacks. */
     63    NSMutableArray *m_pCallbacks;
     64}
     65- (id)init;
     66- (void)sendEvent:(NSEvent *)theEvent;
     67- (void)setCallback:(uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser;
     68- (void)unsetCallback:(uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser;
     69@end /* @interface UICocoaApplicationPrivate */
     70
     71@implementation UICocoaApplicationPrivate
    4372-(id) init
    4473{
    4574    self = [super init];
    46     self->m_cCallbacks = 0;
     75    if (self)
     76        m_pCallbacks = [[NSMutableArray alloc] init];
     77
    4778    return self;
    4879}
    49 
    5080
    5181-(void) sendEvent:(NSEvent *)pEvent
     
    5484     * Check if the type matches any of the registered callbacks.
    5585     */
    56     uint32_t const  fMask = self->m_fMask;
     86    uint32_t const fMask = m_fMask;
    5787#if 0 /* for debugging */
    58     VBoxCocoaApplication_printEvent("sendEvent: ", pEvent);
     88    ::darwinPrintEvent("sendEvent: ", pEvent);
    5989#endif
    6090    if (fMask != 0)
    6191    {
    6292        NSEventType EvtType = [pEvent type];
    63         uint32_t    fEvtMask = RT_LIKELY(EvtType < 32) ? RT_BIT_32(EvtType) : 0;
     93        uint32_t fEvtMask = RT_LIKELY(EvtType < 32) ? RT_BIT_32(EvtType) : 0;
    6494        if (fMask & fEvtMask)
    6595        {
     
    6797             * Do the callouts in LIFO order.
    6898             */
    69             int i = self->m_cCallbacks;
    70             PCVBOXCAENTRY pCur = &self->m_aCallbacks[i];
    71             while (i-- > 0)
     99            for (CallbackData *pData in [m_pCallbacks reverseObjectEnumerator])
    72100            {
    73                 pCur--;
    74                 if (pCur->fMask & fEvtMask)
     101                if (pData->fMask & fEvtMask)
    75102                {
    76                     if (pCur->pfnCallback(pEvent, [pEvent eventRef], pCur->pvUser))
     103                    if (pData->pfnCallback(pEvent, [pEvent eventRef], pData->pvUser))
    77104                        return;
    78105                }
     106
    79107            }
    80108        }
     
    86114    [super sendEvent:pEvent];
    87115}
    88 
    89116
    90117/**
     
    97124-(void) setCallback: (uint32_t)fMask :(PFNVBOXCACALLBACK)pfnCallback :(void *)pvUser
    98125{
    99     unsigned i = self->m_cCallbacks;
    100     AssertReleaseReturnVoid(i < RT_ELEMENTS(self->m_aCallbacks));
    101 
    102     self->m_aCallbacks[i].pfnCallback = pfnCallback;
    103     self->m_aCallbacks[i].pvUser      = pvUser;
    104     self->m_aCallbacks[i].fMask       = fMask;
    105     self->m_cCallbacks++;
    106     self->m_fMask |= fMask;
    107 }
    108 
     126    /* Add the callback data to the array */
     127    CallbackData *pData = [[[CallbackData alloc] initWithMask: fMask callback: pfnCallback user: pvUser] autorelease];
     128    [m_pCallbacks addObject: pData];
     129
     130    /* Update the global mask */
     131    m_fMask |= fMask;
     132}
    109133
    110134/**
     
    120144     * Loop the event array LIFO fashion searching for a matching callback.
    121145     */
    122     int i = self->m_cCallbacks;
    123     while (i-- > 0)
     146    for (CallbackData *pData in [m_pCallbacks reverseObjectEnumerator])
    124147    {
    125         if (    self->m_aCallbacks[i].pfnCallback == pfnCallback
    126             &&  self->m_aCallbacks[i].pvUser      == pvUser
    127             &&  self->m_aCallbacks[i].fMask       == fMask)
     148        if (   pData->pfnCallback == pfnCallback
     149            && pData->pvUser      == pvUser
     150            && pData->fMask       == fMask)
    128151        {
    129             uint32_t fNewMask;
    130 
    131             if (i + 1 != (int)self->m_cCallbacks)
    132                 self->m_aCallbacks[i] = self->m_aCallbacks[self->m_cCallbacks - 1];
    133             self->m_cCallbacks--;
    134 
    135             /*
    136              * Recalculate the event type mask.
    137              */
    138             fNewMask = 0;
    139             i = self->m_cCallbacks;
    140             while (i-- > 0)
    141                 fNewMask |= self->m_aCallbacks[i].fMask;
    142             self->m_fMask = fNewMask;
    143             return;
     152            [m_pCallbacks removeObject: pData];
     153            break;
    144154        }
    145155    }
    146     AssertFailed();
    147 }
    148 
    149 
    150 @end /* @implementation VBoxCocoaApplication */
    151 
    152 
    153 /**
    154  * C/C++ interface for calling VBoxCocoaApplication::sharedApplication.
    155  *
    156  */
    157 void VBoxCocoaApplication_sharedApplication(void)
    158 {
    159     if (!g_pVBoxCocoaApp)
    160     {
    161         /*
    162          * It is essential that we use the inherited sharedApplication class
    163          * method, otherwise we'll be screwed later by static variables in it.
    164          */
    165         NSApplication *pApp = [VBoxCocoaApplication sharedApplication];
    166         g_pVBoxCocoaApp = (VBoxCocoaApplication *)pApp;
    167     }
    168 }
    169 
    170 
    171 /**
    172  * Register an event callback.
    173  *
    174  * @param   fMask           The event mask for which the callback is to be invoked.
    175  * @param   pfnCallback     The callback function.
    176  * @param   pvUser          The user argument.
    177  */
    178 void VBoxCocoaApplication_setCallback(uint32_t fMask, PFNVBOXCACALLBACK pfnCallback, void *pvUser)
    179 {
    180     [g_pVBoxCocoaApp setCallback:fMask :pfnCallback :pvUser];
    181 }
    182 
    183 
    184 /**
    185  * Deregister an event callback.
    186  *
    187  * @param   fMask           Same as setCallback.
    188  * @param   pfnCallback     Same as setCallback.
    189  * @param   pvUser          Same as setCallback.
    190  */
    191 void VBoxCocoaApplication_unsetCallback(uint32_t fMask, PFNVBOXCACALLBACK pfnCallback, void *pvUser)
    192 {
    193     [g_pVBoxCocoaApp unsetCallback:fMask :pfnCallback :pvUser];
    194 }
    195 
    196 
    197 /**
    198  * Calls the -(NSUInteger)modifierFlags method on a NSEvent object.
    199  *
    200  * @return  The Cocoa event modifier mask.
    201  * @param   pvEvent     The NSEvent object.
    202  */
    203 unsigned long VBoxCocoaApplication_getEventModifierFlags(const void *pvEvent)
    204 {
    205     NSEvent *pEvent = (NSEvent *)pvEvent;
    206     return [pEvent modifierFlags];
    207 }
    208 
    209 
    210 /**
    211  * Calls the -(NSUInteger)modifierFlags method on a NSEvent object and
    212  * converts the flags to carbon style.
    213  *
    214  * @return  The Carbon modifier mask.
    215  * @param   pvEvent     The NSEvent object.
    216  */
    217 uint32_t VBoxCocoaApplication_getEventModifierFlagsXlated(const void *pvEvent)
    218 {
    219     NSEvent    *pEvent  = (NSEvent *)pvEvent;
    220     NSUInteger  fCocoa  = [pEvent modifierFlags];
    221     uint32_t    fCarbon = 0;
    222     if (fCocoa)
    223     {
    224         if (fCocoa & NSAlphaShiftKeyMask)
    225             fCarbon |= alphaLock;
    226         if (fCocoa & (NSShiftKeyMask | NX_DEVICELSHIFTKEYMASK | NX_DEVICERSHIFTKEYMASK))
    227         {
    228             if (fCocoa & (NX_DEVICERSHIFTKEYMASK | NX_DEVICERSHIFTKEYMASK))
    229             {
    230                 if (fCocoa & NX_DEVICERSHIFTKEYMASK)
    231                     fCarbon |= rightShiftKey;
    232                 if (fCocoa & NX_DEVICELSHIFTKEYMASK)
    233                     fCarbon |= shiftKey;
    234             }
    235             else
    236                 fCarbon |= shiftKey;
    237         }
    238 
    239         if (fCocoa & (NSControlKeyMask | NX_DEVICELCTLKEYMASK | NX_DEVICERCTLKEYMASK))
    240         {
    241             if (fCocoa & (NX_DEVICELCTLKEYMASK | NX_DEVICERCTLKEYMASK))
    242             {
    243                 if (fCocoa & NX_DEVICERCTLKEYMASK)
    244                     fCarbon |= rightControlKey;
    245                 if (fCocoa & NX_DEVICELCTLKEYMASK)
    246                     fCarbon |= controlKey;
    247             }
    248             else
    249                 fCarbon |= controlKey;
    250         }
    251 
    252         if (fCocoa & (NSAlternateKeyMask | NX_DEVICELALTKEYMASK | NX_DEVICERALTKEYMASK))
    253         {
    254             if (fCocoa & (NX_DEVICELALTKEYMASK | NX_DEVICERALTKEYMASK))
    255             {
    256                 if (fCocoa & NX_DEVICERALTKEYMASK)
    257                     fCarbon |= rightOptionKey;
    258                 if (fCocoa & NX_DEVICELALTKEYMASK)
    259                     fCarbon |= optionKey;
    260             }
    261             else
    262                 fCarbon |= optionKey;
    263         }
    264 
    265         if (fCocoa & (NSCommandKeyMask | NX_DEVICELCMDKEYMASK | NX_DEVICERCMDKEYMASK))
    266         {
    267             if (fCocoa & (NX_DEVICELCMDKEYMASK | NX_DEVICERCMDKEYMASK))
    268             {
    269                 if (fCocoa & NX_DEVICERCMDKEYMASK)
    270                     fCarbon |= kEventKeyModifierRightCmdKeyMask;
    271                 if (fCocoa & NX_DEVICELCMDKEYMASK)
    272                     fCarbon |= cmdKey;
    273             }
    274             else
    275                 fCarbon |= cmdKey;
    276         }
    277 
    278         /*
    279         if (fCocoa & NSNumericPadKeyMask)
    280             fCarbon |= ???;
    281 
    282         if (fCocoa & NSHelpKeyMask)
    283             fCarbon |= ???;
    284 
    285         if (fCocoa & NSFunctionKeyMask)
    286             fCarbon |= ???;
    287         */
    288     }
    289 
    290     return fCarbon;
    291 }
    292 
    293 
    294 /**
    295  * Get the name for a Cocoa event type.
    296  *
    297  * @returns Read-only name string.
    298  * @param   eEvtType        The Cocoa event type.
    299  */
    300 const char *VBoxCocoaApplication_eventTypeName(unsigned long eEvtType)
    301 {
    302     switch (eEvtType)
    303     {
    304 #define EVT_CASE(nm) case nm: return #nm
    305         EVT_CASE(NSLeftMouseDown);
    306         EVT_CASE(NSLeftMouseUp);
    307         EVT_CASE(NSRightMouseDown);
    308         EVT_CASE(NSRightMouseUp);
    309         EVT_CASE(NSMouseMoved);
    310         EVT_CASE(NSLeftMouseDragged);
    311         EVT_CASE(NSRightMouseDragged);
    312         EVT_CASE(NSMouseEntered);
    313         EVT_CASE(NSMouseExited);
    314         EVT_CASE(NSKeyDown);
    315         EVT_CASE(NSKeyUp);
    316         EVT_CASE(NSFlagsChanged);
    317         EVT_CASE(NSAppKitDefined);
    318         EVT_CASE(NSSystemDefined);
    319         EVT_CASE(NSApplicationDefined);
    320         EVT_CASE(NSPeriodic);
    321         EVT_CASE(NSCursorUpdate);
    322         EVT_CASE(NSScrollWheel);
    323         EVT_CASE(NSTabletPoint);
    324         EVT_CASE(NSTabletProximity);
    325         EVT_CASE(NSOtherMouseDown);
    326         EVT_CASE(NSOtherMouseUp);
    327         EVT_CASE(NSOtherMouseDragged);
    328 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
    329         EVT_CASE(NSEventTypeGesture);
    330         EVT_CASE(NSEventTypeMagnify);
    331         EVT_CASE(NSEventTypeSwipe);
    332         EVT_CASE(NSEventTypeRotate);
    333         EVT_CASE(NSEventTypeBeginGesture);
    334         EVT_CASE(NSEventTypeEndGesture);
    335 #endif
    336 #undef EVT_CASE
    337         default:
    338             return "Unknown!";
    339     }
    340 }
    341 
    342 /**
    343  * Check for some default application key combinations a Mac user expect, like
    344  * CMD+Q or CMD+H.
    345  *
    346  * @returns true if such a key combo was hit, false otherwise.
    347  * @param   eEvtType        The Cocoa event type.
    348  */
    349 bool VBoxCocoaApplication_isApplicationCommand(const void *pvEvent)
    350 {
    351     NSEvent     *pEvent = (NSEvent *)pvEvent;
    352     NSEventType  eEvtType = [pEvent type];
    353     bool         fGlobalHotkey = false;
    354 
    355     switch (eEvtType)
    356     {
    357         case NSKeyDown:
    358         case NSKeyUp:
    359         {
    360             NSUInteger fEvtMask = [pEvent modifierFlags];
    361             unsigned short KeyCode = [pEvent keyCode];
    362             if (   ((fEvtMask & (NX_NONCOALSESCEDMASK | NX_COMMANDMASK | NX_DEVICELCMDKEYMASK)) == (NX_NONCOALSESCEDMASK | NX_COMMANDMASK | NX_DEVICELCMDKEYMASK))  /* L+CMD */
    363                 || ((fEvtMask & (NX_NONCOALSESCEDMASK | NX_COMMANDMASK | NX_DEVICERCMDKEYMASK)) == (NX_NONCOALSESCEDMASK | NX_COMMANDMASK | NX_DEVICERCMDKEYMASK))) /* R+CMD */
    364             {
    365                 if (   KeyCode == 0x0c  /* CMD+Q (Quit) */
    366                     || KeyCode == 0x04) /* CMD+H (Hide) */
    367                     fGlobalHotkey = true;
    368             }
    369             else if (   ((fEvtMask & (NX_NONCOALSESCEDMASK | NX_ALTERNATEMASK | NX_DEVICELALTKEYMASK | NX_COMMANDMASK | NX_DEVICELCMDKEYMASK)) == (NX_NONCOALSESCEDMASK | NX_ALTERNATEMASK | NX_DEVICELALTKEYMASK | NX_COMMANDMASK | NX_DEVICELCMDKEYMASK)) /* L+ALT+CMD */
    370                      || ((fEvtMask & (NX_NONCOALSESCEDMASK | NX_ALTERNATEMASK | NX_DEVICERCMDKEYMASK | NX_COMMANDMASK | NX_DEVICERCMDKEYMASK)) == (NX_NONCOALSESCEDMASK | NX_ALTERNATEMASK | NX_DEVICERCMDKEYMASK | NX_COMMANDMASK | NX_DEVICERCMDKEYMASK))) /* R+ALT+CMD */
    371             {
    372                 if (KeyCode == 0x04)    /* ALT+CMD+H (Hide-Others) */
    373                     fGlobalHotkey = true;
    374             }
    375             break;
    376         }
    377         default: break;
    378     }
    379     return fGlobalHotkey;
    380 }
    381 
    382 /**
    383  * Debug helper function for dumping a Cocoa event to stdout.
    384  *
    385  * @param   pszPrefix       Message prefix.
    386  * @param   pvEvent         The Cocoa event.
    387  */
    388 void VBoxCocoaApplication_printEvent(const char *pszPrefix, const void *pvEvent)
    389 {
    390     NSEvent            *pEvent = (NSEvent *)pvEvent;
    391     NSEventType         eEvtType = [pEvent type];
    392     NSUInteger          fEvtMask = [pEvent modifierFlags];
    393     NSWindow           *pEvtWindow = [pEvent window];
    394     NSInteger           iEvtWindow = [pEvent windowNumber];
    395     NSGraphicsContext  *pEvtGraphCtx = [pEvent context];
    396 
    397     printf("%s%p: Type=%lu Modifiers=%08lx pWindow=%p #Wnd=%ld pGraphCtx=%p %s\n",
    398            pszPrefix, pvEvent, (unsigned long)eEvtType, (unsigned long)fEvtMask, (void*)pEvtWindow,
    399            (long)iEvtWindow, (void*)pEvtGraphCtx, VBoxCocoaApplication_eventTypeName(eEvtType));
    400 
    401     /* dump type specific into. */
    402     switch (eEvtType)
    403     {
    404         case NSLeftMouseDown:
    405         case NSLeftMouseUp:
    406         case NSRightMouseDown:
    407         case NSRightMouseUp:
    408         case NSMouseMoved:
    409 
    410         case NSLeftMouseDragged:
    411         case NSRightMouseDragged:
    412         case NSMouseEntered:
    413         case NSMouseExited:
    414             break;
    415 
    416         case NSKeyDown:
    417         case NSKeyUp:
    418         {
    419             NSUInteger i;
    420             NSUInteger cch;
    421             NSString *pChars = [pEvent characters];
    422             NSString *pCharsIgnMod = [pEvent charactersIgnoringModifiers];
    423             BOOL fIsARepeat = [pEvent isARepeat];
    424             unsigned short KeyCode = [pEvent keyCode];
    425 
    426             printf("    KeyCode=%04x isARepeat=%d", KeyCode, fIsARepeat);
    427             if (pChars)
    428             {
    429                 cch = [pChars length];
    430                 printf(" characters={");
    431                 for (i = 0; i < cch; i++)
    432                     printf(i == 0 ? "%02x" : ",%02x", [pChars characterAtIndex: i]);
    433                 printf("}");
    434             }
    435 
    436             if (pCharsIgnMod)
    437             {
    438                 cch = [pCharsIgnMod length];
    439                 printf(" charactersIgnoringModifiers={");
    440                 for (i = 0; i < cch; i++)
    441                     printf(i == 0 ? "%02x" : ",%02x", [pCharsIgnMod characterAtIndex: i]);
    442                 printf("}");
    443             }
    444             printf("\n");
    445             break;
    446         }
    447 
    448         case NSFlagsChanged:
    449         {
    450             NSUInteger fOddBits = NSAlphaShiftKeyMask | NSShiftKeyMask | NSControlKeyMask | NSAlternateKeyMask
    451                                 | NSCommandKeyMask | NSNumericPadKeyMask | NSHelpKeyMask | NSFunctionKeyMask
    452                                 | NX_DEVICELCTLKEYMASK | NX_DEVICELSHIFTKEYMASK | NX_DEVICERSHIFTKEYMASK
    453                                 | NX_DEVICELCMDKEYMASK | NX_DEVICERCMDKEYMASK | NX_DEVICELALTKEYMASK
    454                                 | NX_DEVICERALTKEYMASK | NX_DEVICERCTLKEYMASK;
    455 
    456             printf("    KeyCode=%04x", (int)[pEvent keyCode]);
    457 #define PRINT_MOD(cnst, nm) do { if (fEvtMask & (cnst)) printf(" %s", #nm); } while (0)
    458             /* device-independent: */
    459             PRINT_MOD(NSAlphaShiftKeyMask, "AlphaShift");
    460             PRINT_MOD(NSShiftKeyMask, "Shift");
    461             PRINT_MOD(NSControlKeyMask, "Ctrl");
    462             PRINT_MOD(NSAlternateKeyMask, "Alt");
    463             PRINT_MOD(NSCommandKeyMask, "Cmd");
    464             PRINT_MOD(NSNumericPadKeyMask, "NumLock");
    465             PRINT_MOD(NSHelpKeyMask, "Help");
    466             PRINT_MOD(NSFunctionKeyMask, "Fn");
    467             /* device-dependent (sort of): */
    468             PRINT_MOD(NX_DEVICELCTLKEYMASK,   "$L-Ctrl");
    469             PRINT_MOD(NX_DEVICELSHIFTKEYMASK, "$L-Shift");
    470             PRINT_MOD(NX_DEVICERSHIFTKEYMASK, "$R-Shift");
    471             PRINT_MOD(NX_DEVICELCMDKEYMASK,   "$L-Cmd");
    472             PRINT_MOD(NX_DEVICERCMDKEYMASK,   "$R-Cmd");
    473             PRINT_MOD(NX_DEVICELALTKEYMASK,   "$L-Alt");
    474             PRINT_MOD(NX_DEVICERALTKEYMASK,   "$R-Alt");
    475             PRINT_MOD(NX_DEVICERCTLKEYMASK,   "$R-Ctrl");
    476 #undef  PRINT_MOD
    477 
    478             fOddBits = fEvtMask & ~fOddBits;
    479             if (fOddBits)
    480                 printf(" fOddBits=%#08lx", (unsigned long)fOddBits);
    481 #undef  KNOWN_BITS
    482             printf("\n");
    483             break;
    484         }
    485 
    486         case NSAppKitDefined:
    487         case NSSystemDefined:
    488         case NSApplicationDefined:
    489         case NSPeriodic:
    490         case NSCursorUpdate:
    491         case NSScrollWheel:
    492         case NSTabletPoint:
    493         case NSTabletProximity:
    494         case NSOtherMouseDown:
    495         case NSOtherMouseUp:
    496         case NSOtherMouseDragged:
    497 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
    498         case NSEventTypeGesture:
    499         case NSEventTypeMagnify:
    500         case NSEventTypeSwipe:
    501         case NSEventTypeRotate:
    502         case NSEventTypeBeginGesture:
    503         case NSEventTypeEndGesture:
    504 #endif
    505         default:
    506             printf(" Unknown!\n");
    507             break;
    508     }
    509 
    510 }
    511 
     156    uint32_t fNewMask = 0;
     157    for (CallbackData *pData in m_pCallbacks)
     158        fNewMask |= pData->fMask;
     159    m_fMask = fNewMask;
     160}
     161@end /* @implementation UICocoaApplicationPrivate */
     162
     163/* C++ singleton for our private NSApplication object */
     164UICocoaApplication* UICocoaApplication::m_pInstance = 0;
     165
     166/* static */
     167UICocoaApplication* UICocoaApplication::instance()
     168{
     169    if (!m_pInstance)
     170        m_pInstance = new UICocoaApplication();
     171
     172    return m_pInstance;
     173}
     174
     175UICocoaApplication::UICocoaApplication()
     176{
     177    /* Make sure our private NSApplication object is created */
     178    m_pNative = (UICocoaApplicationPrivate*)[UICocoaApplicationPrivate sharedApplication];
     179    /* Create one auto release pool which is in place for all the
     180       initialization and deinitialization stuff. That is when the
     181       NSApplication is not running the run loop (there is a separate auto
     182       release pool defined). */
     183    m_pPool = [[NSAutoreleasePool alloc] init];
     184}
     185
     186UICocoaApplication::~UICocoaApplication()
     187{
     188    [m_pNative release];
     189    [m_pPool release];
     190}
     191
     192void UICocoaApplication::registerForNativeEvents(uint32_t fMask, PFNVBOXCACALLBACK pfnCallback, void *pvUser)
     193{
     194    [m_pNative setCallback:fMask :pfnCallback :pvUser];
     195}
     196
     197void UICocoaApplication::unregisterForNativeEvents(uint32_t fMask, PFNVBOXCACALLBACK pfnCallback, void *pvUser)
     198{
     199    [m_pNative unsetCallback:fMask :pfnCallback :pvUser];
     200}
     201
  • trunk/src/VBox/Frontends/VirtualBox/src/darwin/VBoxCocoaHelper.h

    r28800 r30114  
    2222/* Macro which add a typedef of the given Cocoa class in an appropriate form
    2323 * for the current context. This means void* in the C/CPP context and
    24  * NSWhatever* in the ObjC/ObjCPP context. Use NativeNSWhateverRef when you
    25  * reference the Cocoa type somewhere. The use of this prevents extensive
    26  * casting of void* to the right type in the Cocoa context. */
     24 * NSWhatever* in the ObjC/ObjCPP context. Use
     25 * NativeNSWhateverRef/ConstNativeNSWhateverRef when you reference the Cocoa
     26 * type somewhere. The use of this prevents extensive casting of void* to the
     27 * right type in the Cocoa context. */
    2728#ifdef __OBJC__
    2829#define ADD_COCOA_NATIVE_REF(CocoaClass) \
    2930@class CocoaClass; \
    30 typedef CocoaClass *Native##CocoaClass##Ref
     31typedef CocoaClass *Native##CocoaClass##Ref; \
     32typedef const CocoaClass *ConstNative##CocoaClass##Ref
    3133#else /* __OBJC__ */
    32 #define ADD_COCOA_NATIVE_REF(CocoaClass) typedef void *Native##CocoaClass##Ref
     34#define ADD_COCOA_NATIVE_REF(CocoaClass) \
     35typedef void *Native##CocoaClass##Ref; \
     36typedef const void *ConstNative##CocoaClass##Ref
    3337#endif /* __OBJC__ */
    3438
  • trunk/src/VBox/Frontends/VirtualBox/src/darwin/VBoxUtils-darwin-cocoa.mm

    r30022 r30114  
    3030#import <AppKit/NSFont.h>
    3131
    32 NativeWindowRef darwinToNativeWindowImpl (NativeViewRef aView)
    33 {
    34     CocoaAutoreleasePool pool;
    35 
     32/* For the keyboard stuff */
     33#include <Carbon/Carbon.h>
     34#include "DarwinKeyboard.h"
     35
     36NativeWindowRef darwinToNativeWindowImpl(NativeViewRef aView)
     37{
    3638    NativeWindowRef window = NULL;
    3739    if (aView)
     
    4143}
    4244
    43 NativeViewRef darwinToNativeViewImpl (NativeWindowRef aWindow)
    44 {
    45     CocoaAutoreleasePool pool;
    46 
     45NativeViewRef darwinToNativeViewImpl(NativeWindowRef aWindow)
     46{
    4747    NativeViewRef view = NULL;
    4848    if (aWindow)
     
    5252}
    5353
    54 void darwinSetShowsToolbarButtonImpl (NativeWindowRef aWindow, bool aEnabled)
    55 {
    56     CocoaAutoreleasePool pool;
    57 
     54void darwinSetShowsToolbarButtonImpl(NativeWindowRef aWindow, bool aEnabled)
     55{
    5856    [aWindow setShowsToolbarButton:aEnabled];
    5957}
    6058
    61 void darwinSetShowsResizeIndicatorImpl (NativeWindowRef aWindow, bool aEnabled)
    62 {
    63     CocoaAutoreleasePool pool;
    64 
     59void darwinSetShowsResizeIndicatorImpl(NativeWindowRef aWindow, bool aEnabled)
     60{
    6561    [aWindow setShowsResizeIndicator:aEnabled];
    6662}
    6763
    68 void darwinSetHidesAllTitleButtonsImpl (NativeWindowRef aWindow)
    69 {
    70     CocoaAutoreleasePool pool;
    71 
     64void darwinSetHidesAllTitleButtonsImpl(NativeWindowRef aWindow)
     65{
    7266    /* Remove all title buttons by changing the style mask. This method is
    7367       available from 10.6 on only. */
     
    9488}
    9589
    96 void darwinSetShowsWindowTransparentImpl (NativeWindowRef aWindow, bool aEnabled)
    97 {
    98     CocoaAutoreleasePool pool;
    99 
     90void darwinSetShowsWindowTransparentImpl(NativeWindowRef aWindow, bool aEnabled)
     91{
    10092    if (aEnabled)
    10193    {
     
    114106void darwinSetDockIconMenu(QMenu* pMenu)
    115107{
    116     CocoaAutoreleasePool pool;
    117 
    118108    extern void qt_mac_set_dock_menu(QMenu *);
    119109    qt_mac_set_dock_menu(pMenu);
     
    125115 * @param   fEnabled    Whether to enable or disable coalescing.
    126116 */
    127 void darwinSetMouseCoalescingEnabled (bool aEnabled)
    128 {
    129     CocoaAutoreleasePool pool;
    130 
     117void darwinSetMouseCoalescingEnabled(bool aEnabled)
     118{
    131119    [NSEvent setMouseCoalescingEnabled:aEnabled];
    132120}
    133121
    134 void darwinWindowAnimateResizeImpl (NativeWindowRef aWindow, int x, int y, int width, int height)
    135 {
    136     CocoaAutoreleasePool pool;
    137 
     122void darwinWindowAnimateResizeImpl(NativeWindowRef aWindow, int x, int y, int width, int height)
     123{
    138124    /* It seems that Qt doesn't return the height of the window with the
    139125     * toolbar height included. So add this size manually. Could easily be that
     
    143129    int toolbarHeight = 0;
    144130    if(toolbar && [toolbar isVisible])
    145         toolbarHeight = NSHeight (windowFrame) - NSHeight ([[aWindow contentView] frame]);
     131        toolbarHeight = NSHeight(windowFrame) - NSHeight([[aWindow contentView] frame]);
    146132    int h = height + toolbarHeight;
    147     int h1 = h - NSHeight (windowFrame);
     133    int h1 = h - NSHeight(windowFrame);
    148134    windowFrame.size.height = h;
    149135    windowFrame.origin.y -= h1;
     
    152138}
    153139
    154 void darwinWindowInvalidateShadowImpl (NativeWindowRef aWindow)
    155 {
    156     CocoaAutoreleasePool pool;
    157 
     140void darwinWindowInvalidateShadowImpl(NativeWindowRef aWindow)
     141{
    158142    [aWindow invalidateShadow];
    159143}
    160144
    161 int darwinWindowToolBarHeight (NativeWindowRef aWindow)
    162 {
    163     CocoaAutoreleasePool pool;
    164 
     145int darwinWindowToolBarHeight(NativeWindowRef aWindow)
     146{
    165147    NSToolbar *toolbar = [aWindow toolbar];
    166148    NSRect windowFrame = [aWindow frame];
     
    170152    if(toolbar && [toolbar isVisible])
    171153        /* title bar height: */
    172         toolbarHeight = NSHeight (windowFrame) - NSHeight ([[aWindow contentView] frame]) - theight;
     154        toolbarHeight = NSHeight(windowFrame) - NSHeight([[aWindow contentView] frame]) - theight;
    173155
    174156    return toolbarHeight;
     
    177159bool darwinIsToolbarVisible(NativeWindowRef pWindow)
    178160{
    179     CocoaAutoreleasePool pool;
    180 
    181161    NSToolbar *pToolbar = [pWindow toolbar];
    182162
     
    186166bool darwinIsWindowMaximized(NativeWindowRef aWindow)
    187167{
    188     CocoaAutoreleasePool pool;
    189 
    190168    bool fResult = [aWindow isZoomed];
    191169
     
    195173float darwinSmallFontSize()
    196174{
    197     CocoaAutoreleasePool pool;
    198 
    199175    float size = [NSFont systemFontSizeForControlSize: NSSmallControlSize];
    200176
     
    231207}
    232208
     209/**
     210 * Calls the -(NSUInteger)modifierFlags method on a NSEvent object.
     211 *
     212 * @return  The Cocoa event modifier mask.
     213 * @param   pEvent          The Cocoa event.
     214 */
     215unsigned long darwinEventModifierFlags(ConstNativeNSEventRef pEvent)
     216{
     217    return [pEvent modifierFlags];
     218}
     219
     220/**
     221 * Calls the -(NSUInteger)modifierFlags method on a NSEvent object and
     222 * converts the flags to carbon style.
     223 *
     224 * @return  The Carbon modifier mask.
     225 * @param   pEvent          The Cocoa event.
     226 */
     227uint32_t darwinEventModifierFlagsXlated(ConstNativeNSEventRef pEvent)
     228{
     229    NSUInteger  fCocoa  = [pEvent modifierFlags];
     230    uint32_t    fCarbon = 0;
     231    if (fCocoa)
     232    {
     233        if (fCocoa & NSAlphaShiftKeyMask)
     234            fCarbon |= alphaLock;
     235        if (fCocoa & (NSShiftKeyMask | NX_DEVICELSHIFTKEYMASK | NX_DEVICERSHIFTKEYMASK))
     236        {
     237            if (fCocoa & (NX_DEVICERSHIFTKEYMASK | NX_DEVICERSHIFTKEYMASK))
     238            {
     239                if (fCocoa & NX_DEVICERSHIFTKEYMASK)
     240                    fCarbon |= rightShiftKey;
     241                if (fCocoa & NX_DEVICELSHIFTKEYMASK)
     242                    fCarbon |= shiftKey;
     243            }
     244            else
     245                fCarbon |= shiftKey;
     246        }
     247
     248        if (fCocoa & (NSControlKeyMask | NX_DEVICELCTLKEYMASK | NX_DEVICERCTLKEYMASK))
     249        {
     250            if (fCocoa & (NX_DEVICELCTLKEYMASK | NX_DEVICERCTLKEYMASK))
     251            {
     252                if (fCocoa & NX_DEVICERCTLKEYMASK)
     253                    fCarbon |= rightControlKey;
     254                if (fCocoa & NX_DEVICELCTLKEYMASK)
     255                    fCarbon |= controlKey;
     256            }
     257            else
     258                fCarbon |= controlKey;
     259        }
     260
     261        if (fCocoa & (NSAlternateKeyMask | NX_DEVICELALTKEYMASK | NX_DEVICERALTKEYMASK))
     262        {
     263            if (fCocoa & (NX_DEVICELALTKEYMASK | NX_DEVICERALTKEYMASK))
     264            {
     265                if (fCocoa & NX_DEVICERALTKEYMASK)
     266                    fCarbon |= rightOptionKey;
     267                if (fCocoa & NX_DEVICELALTKEYMASK)
     268                    fCarbon |= optionKey;
     269            }
     270            else
     271                fCarbon |= optionKey;
     272        }
     273
     274        if (fCocoa & (NSCommandKeyMask | NX_DEVICELCMDKEYMASK | NX_DEVICERCMDKEYMASK))
     275        {
     276            if (fCocoa & (NX_DEVICELCMDKEYMASK | NX_DEVICERCMDKEYMASK))
     277            {
     278                if (fCocoa & NX_DEVICERCMDKEYMASK)
     279                    fCarbon |= kEventKeyModifierRightCmdKeyMask;
     280                if (fCocoa & NX_DEVICELCMDKEYMASK)
     281                    fCarbon |= cmdKey;
     282            }
     283            else
     284                fCarbon |= cmdKey;
     285        }
     286
     287        /*
     288        if (fCocoa & NSNumericPadKeyMask)
     289            fCarbon |= ???;
     290
     291        if (fCocoa & NSHelpKeyMask)
     292            fCarbon |= ???;
     293
     294        if (fCocoa & NSFunctionKeyMask)
     295            fCarbon |= ???;
     296        */
     297    }
     298
     299    return fCarbon;
     300}
     301
     302/**
     303 * Check for some default application key combinations a Mac user expect, like
     304 * CMD+Q or CMD+H.
     305 *
     306 * @returns true if such a key combo was hit, false otherwise.
     307 * @param   pEvent          The Cocoa event.
     308 */
     309bool darwinIsApplicationCommand(ConstNativeNSEventRef pEvent)
     310{
     311    NSEventType  eEvtType = [pEvent type];
     312    bool         fGlobalHotkey = false;
     313
     314    switch (eEvtType)
     315    {
     316        case NSKeyDown:
     317        case NSKeyUp:
     318        {
     319            NSUInteger fEvtMask = [pEvent modifierFlags];
     320            unsigned short KeyCode = [pEvent keyCode];
     321            if (   ((fEvtMask & (NX_NONCOALSESCEDMASK | NX_COMMANDMASK | NX_DEVICELCMDKEYMASK)) == (NX_NONCOALSESCEDMASK | NX_COMMANDMASK | NX_DEVICELCMDKEYMASK))  /* L+CMD */
     322                || ((fEvtMask & (NX_NONCOALSESCEDMASK | NX_COMMANDMASK | NX_DEVICERCMDKEYMASK)) == (NX_NONCOALSESCEDMASK | NX_COMMANDMASK | NX_DEVICERCMDKEYMASK))) /* R+CMD */
     323            {
     324                if (   KeyCode == 0x0c  /* CMD+Q (Quit) */
     325                    || KeyCode == 0x04) /* CMD+H (Hide) */
     326                    fGlobalHotkey = true;
     327            }
     328            else if (   ((fEvtMask & (NX_NONCOALSESCEDMASK | NX_ALTERNATEMASK | NX_DEVICELALTKEYMASK | NX_COMMANDMASK | NX_DEVICELCMDKEYMASK)) == (NX_NONCOALSESCEDMASK | NX_ALTERNATEMASK | NX_DEVICELALTKEYMASK | NX_COMMANDMASK | NX_DEVICELCMDKEYMASK)) /* L+ALT+CMD */
     329                     || ((fEvtMask & (NX_NONCOALSESCEDMASK | NX_ALTERNATEMASK | NX_DEVICERCMDKEYMASK | NX_COMMANDMASK | NX_DEVICERCMDKEYMASK)) == (NX_NONCOALSESCEDMASK | NX_ALTERNATEMASK | NX_DEVICERCMDKEYMASK | NX_COMMANDMASK | NX_DEVICERCMDKEYMASK))) /* R+ALT+CMD */
     330            {
     331                if (KeyCode == 0x04)    /* ALT+CMD+H (Hide-Others) */
     332                    fGlobalHotkey = true;
     333            }
     334            break;
     335        }
     336        default: break;
     337    }
     338    return fGlobalHotkey;
     339}
     340
     341/**
     342 * Get the name for a Cocoa event type.
     343 *
     344 * @returns Read-only name string.
     345 * @param   eEvtType        The Cocoa event type.
     346 */
     347const char *darwinEventTypeName(unsigned long eEvtType)
     348{
     349    switch (eEvtType)
     350    {
     351#define EVT_CASE(nm) case nm: return #nm
     352        EVT_CASE(NSLeftMouseDown);
     353        EVT_CASE(NSLeftMouseUp);
     354        EVT_CASE(NSRightMouseDown);
     355        EVT_CASE(NSRightMouseUp);
     356        EVT_CASE(NSMouseMoved);
     357        EVT_CASE(NSLeftMouseDragged);
     358        EVT_CASE(NSRightMouseDragged);
     359        EVT_CASE(NSMouseEntered);
     360        EVT_CASE(NSMouseExited);
     361        EVT_CASE(NSKeyDown);
     362        EVT_CASE(NSKeyUp);
     363        EVT_CASE(NSFlagsChanged);
     364        EVT_CASE(NSAppKitDefined);
     365        EVT_CASE(NSSystemDefined);
     366        EVT_CASE(NSApplicationDefined);
     367        EVT_CASE(NSPeriodic);
     368        EVT_CASE(NSCursorUpdate);
     369        EVT_CASE(NSScrollWheel);
     370        EVT_CASE(NSTabletPoint);
     371        EVT_CASE(NSTabletProximity);
     372        EVT_CASE(NSOtherMouseDown);
     373        EVT_CASE(NSOtherMouseUp);
     374        EVT_CASE(NSOtherMouseDragged);
     375#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
     376        EVT_CASE(NSEventTypeGesture);
     377        EVT_CASE(NSEventTypeMagnify);
     378        EVT_CASE(NSEventTypeSwipe);
     379        EVT_CASE(NSEventTypeRotate);
     380        EVT_CASE(NSEventTypeBeginGesture);
     381        EVT_CASE(NSEventTypeEndGesture);
     382#endif
     383#undef EVT_CASE
     384        default:
     385            return "Unknown!";
     386    }
     387}
     388
     389/**
     390 * Debug helper function for dumping a Cocoa event to stdout.
     391 *
     392 * @param   pszPrefix       Message prefix.
     393 * @param   pEvent          The Cocoa event.
     394 */
     395void darwinPrintEvent(const char *pszPrefix, ConstNativeNSEventRef pEvent)
     396{
     397    NSEventType         eEvtType = [pEvent type];
     398    NSUInteger          fEvtMask = [pEvent modifierFlags];
     399    NSWindow           *pEvtWindow = [pEvent window];
     400    NSInteger           iEvtWindow = [pEvent windowNumber];
     401    NSGraphicsContext  *pEvtGraphCtx = [pEvent context];
     402
     403    printf("%s%p: Type=%lu Modifiers=%08lx pWindow=%p #Wnd=%ld pGraphCtx=%p %s\n",
     404           pszPrefix, (void*)pEvent, (unsigned long)eEvtType, (unsigned long)fEvtMask, (void*)pEvtWindow,
     405           (long)iEvtWindow, (void*)pEvtGraphCtx, darwinEventTypeName(eEvtType));
     406
     407    /* dump type specific into. */
     408    switch (eEvtType)
     409    {
     410        case NSLeftMouseDown:
     411        case NSLeftMouseUp:
     412        case NSRightMouseDown:
     413        case NSRightMouseUp:
     414        case NSMouseMoved:
     415
     416        case NSLeftMouseDragged:
     417        case NSRightMouseDragged:
     418        case NSMouseEntered:
     419        case NSMouseExited:
     420            break;
     421
     422        case NSKeyDown:
     423        case NSKeyUp:
     424        {
     425            NSUInteger i;
     426            NSUInteger cch;
     427            NSString *pChars = [pEvent characters];
     428            NSString *pCharsIgnMod = [pEvent charactersIgnoringModifiers];
     429            BOOL fIsARepeat = [pEvent isARepeat];
     430            unsigned short KeyCode = [pEvent keyCode];
     431
     432            printf("    KeyCode=%04x isARepeat=%d", KeyCode, fIsARepeat);
     433            if (pChars)
     434            {
     435                cch = [pChars length];
     436                printf(" characters={");
     437                for (i = 0; i < cch; i++)
     438                    printf(i == 0 ? "%02x" : ",%02x", [pChars characterAtIndex: i]);
     439                printf("}");
     440            }
     441
     442            if (pCharsIgnMod)
     443            {
     444                cch = [pCharsIgnMod length];
     445                printf(" charactersIgnoringModifiers={");
     446                for (i = 0; i < cch; i++)
     447                    printf(i == 0 ? "%02x" : ",%02x", [pCharsIgnMod characterAtIndex: i]);
     448                printf("}");
     449            }
     450            printf("\n");
     451            break;
     452        }
     453
     454        case NSFlagsChanged:
     455        {
     456            NSUInteger fOddBits = NSAlphaShiftKeyMask | NSShiftKeyMask | NSControlKeyMask | NSAlternateKeyMask
     457                                | NSCommandKeyMask | NSNumericPadKeyMask | NSHelpKeyMask | NSFunctionKeyMask
     458                                | NX_DEVICELCTLKEYMASK | NX_DEVICELSHIFTKEYMASK | NX_DEVICERSHIFTKEYMASK
     459                                | NX_DEVICELCMDKEYMASK | NX_DEVICERCMDKEYMASK | NX_DEVICELALTKEYMASK
     460                                | NX_DEVICERALTKEYMASK | NX_DEVICERCTLKEYMASK;
     461
     462            printf("    KeyCode=%04x", (int)[pEvent keyCode]);
     463#define PRINT_MOD(cnst, nm) do { if (fEvtMask & (cnst)) printf(" %s", #nm); } while (0)
     464            /* device-independent: */
     465            PRINT_MOD(NSAlphaShiftKeyMask, "AlphaShift");
     466            PRINT_MOD(NSShiftKeyMask, "Shift");
     467            PRINT_MOD(NSControlKeyMask, "Ctrl");
     468            PRINT_MOD(NSAlternateKeyMask, "Alt");
     469            PRINT_MOD(NSCommandKeyMask, "Cmd");
     470            PRINT_MOD(NSNumericPadKeyMask, "NumLock");
     471            PRINT_MOD(NSHelpKeyMask, "Help");
     472            PRINT_MOD(NSFunctionKeyMask, "Fn");
     473            /* device-dependent (sort of): */
     474            PRINT_MOD(NX_DEVICELCTLKEYMASK,   "$L-Ctrl");
     475            PRINT_MOD(NX_DEVICELSHIFTKEYMASK, "$L-Shift");
     476            PRINT_MOD(NX_DEVICERSHIFTKEYMASK, "$R-Shift");
     477            PRINT_MOD(NX_DEVICELCMDKEYMASK,   "$L-Cmd");
     478            PRINT_MOD(NX_DEVICERCMDKEYMASK,   "$R-Cmd");
     479            PRINT_MOD(NX_DEVICELALTKEYMASK,   "$L-Alt");
     480            PRINT_MOD(NX_DEVICERALTKEYMASK,   "$R-Alt");
     481            PRINT_MOD(NX_DEVICERCTLKEYMASK,   "$R-Ctrl");
     482#undef  PRINT_MOD
     483
     484            fOddBits = fEvtMask & ~fOddBits;
     485            if (fOddBits)
     486                printf(" fOddBits=%#08lx", (unsigned long)fOddBits);
     487#undef  KNOWN_BITS
     488            printf("\n");
     489            break;
     490        }
     491
     492        case NSAppKitDefined:
     493        case NSSystemDefined:
     494        case NSApplicationDefined:
     495        case NSPeriodic:
     496        case NSCursorUpdate:
     497        case NSScrollWheel:
     498        case NSTabletPoint:
     499        case NSTabletProximity:
     500        case NSOtherMouseDown:
     501        case NSOtherMouseUp:
     502        case NSOtherMouseDragged:
     503#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
     504        case NSEventTypeGesture:
     505        case NSEventTypeMagnify:
     506        case NSEventTypeSwipe:
     507        case NSEventTypeRotate:
     508        case NSEventTypeBeginGesture:
     509        case NSEventTypeEndGesture:
     510#endif
     511        default:
     512            printf(" Unknown!\n");
     513            break;
     514    }
     515}
     516
  • trunk/src/VBox/Frontends/VirtualBox/src/darwin/VBoxUtils-darwin.cpp

    r30022 r30114  
    1717
    1818#include "VBoxUtils-darwin.h"
    19 #include "VBoxCocoaApplication.h"
     19#include "UICocoaApplication.h"
    2020
    2121#include <iprt/mem.h>
     
    304304void darwinRegisterForUnifiedToolbarContextMenuEvents(QMainWindow *pWindow)
    305305{
    306     ::VBoxCocoaApplication_setCallback(UINT32_MAX, ::darwinUnifiedToolbarEvents, pWindow);
     306    UICocoaApplication::instance()->registerForNativeEvents(RT_BIT_32(3) /* NSRightMouseDown */, ::darwinUnifiedToolbarEvents, pWindow);
    307307}
    308308
    309309void darwinUnregisterForUnifiedToolbarContextMenuEvents(QMainWindow *pWindow)
    310310{
    311     ::VBoxCocoaApplication_unsetCallback(UINT32_MAX, ::darwinUnifiedToolbarEvents, pWindow);
     311    UICocoaApplication::instance()->unregisterForNativeEvents(RT_BIT_32(3) /* NSRightMouseDown */, ::darwinUnifiedToolbarEvents, pWindow);
    312312}
    313313
  • trunk/src/VBox/Frontends/VirtualBox/src/darwin/VBoxUtils-darwin.h

    r30022 r30114  
    5252#endif /* __OBJC__ */
    5353
     54#include "VBoxCocoaHelper.h"
    5455#include <iprt/cdefs.h> /* for RT_C_DECLS_BEGIN/RT_C_DECLS_END & stuff */
    5556#include <QRect>
    5657
    5758class QWidget;
     59
     60ADD_COCOA_NATIVE_REF(NSEvent);
    5861
    5962RT_C_DECLS_BEGIN
     
    9699bool darwinUnifiedToolbarEvents(const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser);
    97100void darwinCreateContextMenuEvent(void *pvWin, int x, int y);
     101
     102/********************************************************************************
     103 *
     104 * Event/Keyboard helpers (OS System native)
     105 *
     106 ********************************************************************************/
     107unsigned long darwinEventModifierFlags(ConstNativeNSEventRef pEvent);
     108uint32_t darwinEventModifierFlagsXlated(ConstNativeNSEventRef pEvent);
     109bool darwinIsApplicationCommand(ConstNativeNSEventRef pEvent);
     110const char *darwinEventTypeName(unsigned long eEvtType);
     111void darwinPrintEvent(const char *pszPrefix, ConstNativeNSEventRef pEvent);
    98112
    99113RT_C_DECLS_END
  • trunk/src/VBox/Frontends/VirtualBox/src/extensions/QIHotKeyEdit.cpp

    r29921 r30114  
    6767
    6868#ifdef Q_WS_MAC
     69# include "UICocoaApplication.h"
    6970# include "DarwinKeyboard.h"
     71# include "VBoxUtils.h"
    7072# include <Carbon/Carbon.h>
    71 # include "darwin/VBoxCocoaApplication.h"
    72 # include "VBoxUtils.h"
    7373#endif
    7474
     
    141141#ifdef Q_WS_MAC
    142142    mDarwinKeyModifiers = GetCurrentEventKeyModifiers();
    143     ::VBoxCocoaApplication_setCallback (UINT32_MAX, QIHotKeyEdit::darwinEventHandlerProc, this);
     143    UICocoaApplication::instance()->registerForNativeEvents(RT_BIT_32(10) | RT_BIT_32(11) | RT_BIT_32(12) /* NSKeyDown  | NSKeyUp | | NSFlagsChanged */, QIHotKeyEdit::darwinEventHandlerProc, this);
    144144    ::DarwinGrabKeyboard (false /* just modifiers */);
    145145#endif
     
    150150#ifdef Q_WS_MAC
    151151    ::DarwinReleaseKeyboard();
    152     ::VBoxCocoaApplication_unsetCallback (UINT32_MAX, QIHotKeyEdit::darwinEventHandlerProc, this);
     152    UICocoaApplication::instance()->unregisterForNativeEvents(RT_BIT_32(10) | RT_BIT_32(11) | RT_BIT_32(12) /* NSKeyDown  | NSKeyUp | | NSFlagsChanged */, QIHotKeyEdit::darwinEventHandlerProc, this);
    153153#endif
    154154}
  • trunk/src/VBox/Frontends/VirtualBox/src/main.cpp

    r29794 r30114  
    1919
    2020#ifdef VBOX_WITH_PRECOMPILED_HEADERS
    21 # include "precomp.h"
    22 # ifdef QT_MAC_USE_COCOA
    23 #  include "darwin/VBoxCocoaApplication.h"
    24 # endif
     21#include "precomp.h"
     22#ifdef Q_WS_MAC
     23# include "UICocoaApplication.h"
     24#endif /* Q_WS_MAC */
    2525#else /* !VBOX_WITH_PRECOMPILED_HEADERS */
    2626#include "VBoxGlobal.h"
     
    2828#include "VBoxSelectorWnd.h"
    2929#include "VBoxUtils.h"
    30 #ifdef QT_MAC_USE_COCOA
    31 # include "darwin/VBoxCocoaApplication.h"
     30#ifdef Q_WS_MAC
     31# include "UICocoaApplication.h"
    3232#endif
    3333
     
    325325    /* Instantiate our NSApplication derivative before QApplication
    326326     * forces NSApplication to be instantiated. */
    327     VBoxCocoaApplication_sharedApplication();
     327    UICocoaApplication::instance();
    328328#endif
    329329
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp

    r30074 r30114  
    8282# include "DockIconPreview.h"
    8383# include "DarwinKeyboard.h"
    84 # include "VBoxCocoaApplication.h"
     84# include "UICocoaApplication.h"
    8585# include <VBox/err.h>
    8686# include <Carbon/Carbon.h>
     
    23462346
    23472347        /* Register the event callback/hook and grab the keyboard. */
    2348         ::VBoxCocoaApplication_setCallback (UINT32_MAX, /** @todo fix mask */
    2349                                             UIMachineView::darwinEventHandlerProc, this);
     2348        UICocoaApplication::instance()->registerForNativeEvents(RT_BIT_32(10) | RT_BIT_32(11) | RT_BIT_32(12) /* NSKeyDown  | NSKeyUp | | NSFlagsChanged */,
     2349                                                                UIMachineView::darwinEventHandlerProc, this);
    23502350
    23512351        ::DarwinGrabKeyboard (false);
     
    23542354    {
    23552355        ::DarwinReleaseKeyboard();
    2356         ::VBoxCocoaApplication_unsetCallback(UINT32_MAX, /** @todo fix mask */
    2357                                              UIMachineView::darwinEventHandlerProc, this);
     2356        UICocoaApplication::instance()->unregisterForNativeEvents(RT_BIT_32(10) | RT_BIT_32(11) | RT_BIT_32(12) /* NSKeyDown  | NSKeyUp | | NSFlagsChanged */,
     2357                                                                  UIMachineView::darwinEventHandlerProc, this);
    23582358    }
    23592359}
     
    23672367    /* Check if this is an application key combo. In that case we will not pass
    23682368     * the event to the guest, but let the host process it. */
    2369     if (VBoxCocoaApplication_isApplicationCommand(pvCocoaEvent))
     2369    if (::darwinIsApplicationCommand(pvCocoaEvent))
    23702370        return false;
    23712371
Note: See TracChangeset for help on using the changeset viewer.

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