diff options
author | Stefano Pigozzi <stefano.pigozzi@gmail.com> | 2013-02-19 21:21:50 +0100 |
---|---|---|
committer | Stefano Pigozzi <stefano.pigozzi@gmail.com> | 2013-02-19 21:21:50 +0100 |
commit | 145c965135dc01869051173007257123df7f0a4b (patch) | |
tree | 8aabe0a6f4cecd1c97640c218c823deaeaa3fadb /video/out/cocoa_common.m | |
parent | a090c0745354791874167349498bb635c3cba3ba (diff) | |
download | mpv-145c965135dc01869051173007257123df7f0a4b.tar.bz2 mpv-145c965135dc01869051173007257123df7f0a4b.tar.xz |
cocoa_common: fix crash with dead key input and simplify related code
Fixes #29. When a user used dead input keys (like the accent key), `mpv`
crashed because the code tried to access the 0 element of a characters array
(which was empty).
While I was closing this bug, I refactored some related conditionals to
make the code more readable.
Diffstat (limited to 'video/out/cocoa_common.m')
-rw-r--r-- | video/out/cocoa_common.m | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/video/out/cocoa_common.m b/video/out/cocoa_common.m index fd8e43b18b..4d4d7f927b 100644 --- a/video/out/cocoa_common.m +++ b/video/out/cocoa_common.m @@ -51,9 +51,21 @@ #define NSOpenGLProfileVersion3_2Core 0x3200 #endif -#define NSLeftAlternateKeyMask (0x000020 | NSAlternateKeyMask) +#define NSLeftAlternateKeyMask (0x000020 | NSAlternateKeyMask) #define NSRightAlternateKeyMask (0x000040 | NSAlternateKeyMask) +static bool LeftAltPressed(NSEvent *event) +{ + return ([event modifierFlags] & NSLeftAlternateKeyMask) == + NSLeftAlternateKeyMask; +} + +static bool RightAltPressed(NSEvent *event) +{ + return ([event modifierFlags] & NSRightAlternateKeyMask) == + NSRightAlternateKeyMask; +} + // add methods not available on OSX versions prior to 10.7 #ifndef MAC_OS_X_VERSION_10_7 @interface NSView (IntroducedInLion) @@ -678,22 +690,21 @@ void create_menu() - (void)keyDown:(NSEvent *)theEvent { - unsigned char charcode; - if (([theEvent modifierFlags] & NSRightAlternateKeyMask) == - NSRightAlternateKeyMask) - charcode = *[[theEvent characters] UTF8String]; + NSString *chars; + + if (RightAltPressed(theEvent)) + chars = [theEvent characters]; else - charcode = [[theEvent charactersIgnoringModifiers] characterAtIndex:0]; + chars = [theEvent charactersIgnoringModifiers]; - int key = convert_key([theEvent keyCode], charcode); + int key = convert_key([theEvent keyCode], *[chars UTF8String]); if (key > -1) { if ([theEvent modifierFlags] & NSShiftKeyMask) key |= MP_KEY_MODIFIER_SHIFT; if ([theEvent modifierFlags] & NSControlKeyMask) key |= MP_KEY_MODIFIER_CTRL; - if (([theEvent modifierFlags] & NSLeftAlternateKeyMask) == - NSLeftAlternateKeyMask) + if (LeftAltPressed(theEvent)) key |= MP_KEY_MODIFIER_ALT; if ([theEvent modifierFlags] & NSCommandKeyMask) key |= MP_KEY_MODIFIER_META; |