[vlc-commits] macosx: several fixes and improvements for hotkey handling

David Fuhrmann git at videolan.org
Fri May 25 19:37:25 CEST 2012


vlc | branch: master | David Fuhrmann <david.fuhrmann at googlemail.com> | Fri May 25 18:39:01 2012 +0200| [1536ed21be0f545840bf8b15e89e4f81fd1908a8] | committer: David Fuhrmann

macosx: several fixes and improvements for hotkey handling

- volume and mute hotkeys are handled by core now, providing a proper osd screen
- some fixes in detection if key combo is valid hotkey
- fix for mem leak

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=1536ed21be0f545840bf8b15e89e4f81fd1908a8
---

 modules/gui/macosx/MainWindow.m |   35 +++++++++++++---
 modules/gui/macosx/VideoView.m  |    6 +++
 modules/gui/macosx/intf.m       |   86 +++++++++++++++++++--------------------
 3 files changed, 79 insertions(+), 48 deletions(-)

diff --git a/modules/gui/macosx/MainWindow.m b/modules/gui/macosx/MainWindow.m
index f4c92e7..aaec66b 100644
--- a/modules/gui/macosx/MainWindow.m
+++ b/modules/gui/macosx/MainWindow.m
@@ -102,14 +102,39 @@ static VLCMainWindow *_o_sharedInstance = nil;
     return self;
 }
 
+- (BOOL)isEvent:(NSEvent *)o_event forKey:(const char *)keyString
+{
+    char *key;
+    NSString *o_key;
+
+    key = config_GetPsz( VLCIntf, keyString );
+    o_key = [NSString stringWithFormat:@"%s", key];
+    FREENULL( key );
+
+    unsigned int i_keyModifiers = [[VLCMain sharedInstance] VLCModifiersToCocoa:o_key];
+
+    return [[[o_event charactersIgnoringModifiers] lowercaseString] isEqualToString: [[VLCMain sharedInstance] VLCKeyToString: o_key]] && 
+            (i_keyModifiers & NSShiftKeyMask)     == ([o_event modifierFlags] & NSShiftKeyMask) && 
+            (i_keyModifiers & NSControlKeyMask)   == ([o_event modifierFlags] & NSControlKeyMask) && 
+            (i_keyModifiers & NSAlternateKeyMask) == ([o_event modifierFlags] & NSAlternateKeyMask) && 
+            (i_keyModifiers & NSCommandKeyMask)   == ([o_event modifierFlags] & NSCommandKeyMask);
+}
+
 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
 {
-    /* We indeed want to prioritize Cocoa key equivalent against libvlc,
-     so we perform the menu equivalent now. */
-    if([[NSApp mainMenu] performKeyEquivalent:o_event])
-        return TRUE;
+    // these are key events which should be handled by vlc core, but are attached to a main menu item
+    if( ![self isEvent: o_event forKey: "key-vol-up"] &&
+        ![self isEvent: o_event forKey: "key-vol-down"] &&
+        ![self isEvent: o_event forKey: "key-vol-mute"] )
+    {
+        /* We indeed want to prioritize some Cocoa key equivalent against libvlc,
+         so we perform the menu equivalent now. */
+        if([[NSApp mainMenu] performKeyEquivalent:o_event])
+            return TRUE;
+    }
 
-    return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event] || [(VLCControls *)[[VLCMain sharedInstance] controls] keyEvent:o_event];
+    return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event] ||
+           [(VLCControls *)[[VLCMain sharedInstance] controls] keyEvent:o_event];
 }
 
 - (void)dealloc
diff --git a/modules/gui/macosx/VideoView.m b/modules/gui/macosx/VideoView.m
index 14086c4..b191bc7 100644
--- a/modules/gui/macosx/VideoView.m
+++ b/modules/gui/macosx/VideoView.m
@@ -35,6 +35,7 @@
 #import "VideoView.h"
 #import "CoreInteraction.h"
 #import "MainMenu.h"
+#import "MainWindow.h"
 
 #import <vlc_common.h>
 #import <vlc_keys.h>
@@ -174,6 +175,11 @@ int DeviceCallback( vlc_object_t *p_this, const char *psz_variable,
         [super keyDown: o_event];
 }
 
+- (BOOL)performKeyEquivalent:(NSEvent *)o_event
+{
+    return [[VLCMainWindow sharedInstance] performKeyEquivalent: o_event];
+}
+
 - (void)mouseDown:(NSEvent *)o_event
 {
     if( ( [o_event type] == NSLeftMouseDown ) &&
diff --git a/modules/gui/macosx/intf.m b/modules/gui/macosx/intf.m
index 1161b7f..719ec63 100644
--- a/modules/gui/macosx/intf.m
+++ b/modules/gui/macosx/intf.m
@@ -802,6 +802,8 @@ static VLCMain *_o_sharedMainInstance = nil;
     [o_msg_arr removeAllObjects];
     [o_msg_arr release];
     o_msg_arr = NULL;
+    [o_usedHotkeys release];
+    o_usedHotkeys = NULL;
 
     [o_msg_lock release];
 
@@ -1309,88 +1311,81 @@ unsigned int CocoaKeyToVLC( unichar i_key )
     unichar key = 0;
     vlc_value_t val;
     unsigned int i_pressed_modifiers = 0;
-    const struct hotkey *p_hotkeys;
-    int i;
-    NSMutableString *tempString = [[NSMutableString alloc] init];
-    NSMutableString *tempStringPlus = [[NSMutableString alloc] init];
 
     val.i_int = 0;
-    p_hotkeys = p_intf->p_libvlc->p_hotkeys;
-
     i_pressed_modifiers = [o_event modifierFlags];
 
-    if( i_pressed_modifiers & NSShiftKeyMask ) {
-        val.i_int |= KEY_MODIFIER_SHIFT;
-        [tempString appendString:@"Shift-"];
-        [tempStringPlus appendString:@"Shift+"];
-    }
     if( i_pressed_modifiers & NSControlKeyMask ) {
         val.i_int |= KEY_MODIFIER_CTRL;
-        [tempString appendString:@"Ctrl-"];
-        [tempStringPlus appendString:@"Ctrl+"];
     }
     if( i_pressed_modifiers & NSAlternateKeyMask ) {
         val.i_int |= KEY_MODIFIER_ALT;
-        [tempString appendString:@"Alt-"];
-        [tempStringPlus appendString:@"Alt+"];
+    }
+    if( i_pressed_modifiers & NSShiftKeyMask ) {
+        val.i_int |= KEY_MODIFIER_SHIFT;
     }
     if( i_pressed_modifiers & NSCommandKeyMask ) {
         val.i_int |= KEY_MODIFIER_COMMAND;
-        [tempString appendString:@"Command-"];
-        [tempStringPlus appendString:@"Command+"];
     }
 
-    [tempString appendString:[[o_event charactersIgnoringModifiers] lowercaseString]];
-    [tempStringPlus appendString:[[o_event charactersIgnoringModifiers] lowercaseString]];
-
     key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
 
     /* handle Lion's default key combo for fullscreen-toggle in addition to our own hotkeys */
     if( key == 'f' && i_pressed_modifiers & NSControlKeyMask && i_pressed_modifiers & NSCommandKeyMask )
     {
         [[VLCCoreInteraction sharedInstance] toggleFullscreen];
-        [tempString release];
-        [tempStringPlus release];
         return YES;
     }
 
-    switch( key )
+    if( val.i_int == 0 ) // ignore only when no modifier is pressed
     {
-        case NSDeleteCharacter:
-        case NSDeleteFunctionKey:
-        case NSDeleteCharFunctionKey:
-        case NSBackspaceCharacter:
-        case NSUpArrowFunctionKey:
-        case NSDownArrowFunctionKey:
-        case NSRightArrowFunctionKey:
-        case NSLeftArrowFunctionKey:
-        case NSEnterCharacter:
-        case NSCarriageReturnCharacter:
-            [tempString release];
-            [tempStringPlus release];
-            return NO;
+        switch( key )
+        {
+            case NSDeleteCharacter:
+            case NSDeleteFunctionKey:
+            case NSDeleteCharFunctionKey:
+            case NSBackspaceCharacter:
+            case NSUpArrowFunctionKey:
+            case NSDownArrowFunctionKey:
+            case NSRightArrowFunctionKey:
+            case NSLeftArrowFunctionKey:
+            case NSEnterCharacter:
+            case NSCarriageReturnCharacter:
+                return NO;
+        }
     }
 
     if( key == 0x0020 ) // space key
     {
         [[VLCCoreInteraction sharedInstance] play];
-        [tempString release];
-        [tempStringPlus release];
         return YES;
     }
 
     val.i_int |= CocoaKeyToVLC( key );
 
-    if( [o_usedHotkeys indexOfObject: tempString] != NSNotFound || [o_usedHotkeys indexOfObject: tempStringPlus] != NSNotFound )
+    BOOL b_found_key = NO;
+    for( int i = 0; i < [o_usedHotkeys count]; i++ )
+    {
+        NSString *str = [o_usedHotkeys objectAtIndex: i];
+        unsigned int i_keyModifiers = [self VLCModifiersToCocoa: str];
+
+        if( [[[o_event charactersIgnoringModifiers] lowercaseString] isEqualToString: [self VLCKeyToString: str]] &&
+           (i_keyModifiers & NSShiftKeyMask)     == (i_pressed_modifiers & NSShiftKeyMask) && 
+           (i_keyModifiers & NSControlKeyMask)   == (i_pressed_modifiers & NSControlKeyMask) && 
+           (i_keyModifiers & NSAlternateKeyMask) == (i_pressed_modifiers & NSAlternateKeyMask) && 
+           (i_keyModifiers & NSCommandKeyMask)   == (i_pressed_modifiers & NSCommandKeyMask) )
+        {
+            b_found_key = YES;
+            break;
+        }
+    }
+
+    if( b_found_key )
     {
         var_SetInteger( p_intf->p_libvlc, "key-pressed", val.i_int );
-        [tempString release];
-        [tempStringPlus release];
         return YES;
     }
 
-    [tempString release];
-    [tempStringPlus release];
     return NO;
 }
 
@@ -1414,10 +1409,15 @@ unsigned int CocoaKeyToVLC( unichar i_key )
            && !EMPTY_STR( p_item->psz_text ) )
         {
             if (p_item->value.psz)
+            {
                 [o_tempArray addObject: [NSString stringWithUTF8String:p_item->value.psz]];
+            }
         }
     }
     module_config_free (p_config);
+    
+    if( o_usedHotkeys )
+        [o_usedHotkeys release];
     o_usedHotkeys = [[NSArray alloc] initWithArray: o_tempArray copyItems: YES];
     [o_tempArray release];
 }



More information about the vlc-commits mailing list