1
0
mirror of https://github.com/mpv-player/mpv synced 2025-03-20 10:17:31 +00:00

cocoa_common: handle keyboard modifiers for mouse events

This commit is contained in:
Stefano Pigozzi 2013-07-21 10:33:18 +02:00
parent 03fd2fe61c
commit 5f265d5930
4 changed files with 72 additions and 57 deletions

View File

@ -35,6 +35,8 @@ struct cocoa_input_queue;
- (void)startMediaKeys; - (void)startMediaKeys;
- (void)restartMediaKeys; - (void)restartMediaKeys;
- (void)stopMediaKeys; - (void)stopMediaKeys;
- (int)mapKeyModifiers:(int)cocoaModifiers;
- (int)keyModifierMask:(NSEvent *)event;
@end @end
@interface Application : NSApplication @interface Application : NSApplication

View File

@ -22,6 +22,7 @@
#include "core/input/keycodes.h" #include "core/input/keycodes.h"
void cocoa_put_key(int keycode); void cocoa_put_key(int keycode);
void cocoa_put_key_with_modifiers(int keycode, int modifiers);
void cocoa_check_events(void); void cocoa_check_events(void);
void cocoa_init_apple_remote(void); void cocoa_init_apple_remote(void);

View File

@ -37,16 +37,14 @@
#define NSLeftAlternateKeyMask (0x000020 | NSAlternateKeyMask) #define NSLeftAlternateKeyMask (0x000020 | NSAlternateKeyMask)
#define NSRightAlternateKeyMask (0x000040 | NSAlternateKeyMask) #define NSRightAlternateKeyMask (0x000040 | NSAlternateKeyMask)
static bool LeftAltPressed(NSEvent *event) static bool LeftAltPressed(int mask)
{ {
return ([event modifierFlags] & NSLeftAlternateKeyMask) == return (mask & NSLeftAlternateKeyMask) == NSLeftAlternateKeyMask;
NSLeftAlternateKeyMask;
} }
static bool RightAltPressed(NSEvent *event) static bool RightAltPressed(int mask)
{ {
return ([event modifierFlags] & NSRightAlternateKeyMask) == return (mask & NSRightAlternateKeyMask) == NSRightAlternateKeyMask;
NSRightAlternateKeyMask;
} }
static const struct mp_keymap keymap[] = { static const struct mp_keymap keymap[] = {
@ -174,6 +172,12 @@ void cocoa_put_key(int keycode)
[mpv_shared_app().iqueue push:keycode]; [mpv_shared_app().iqueue push:keycode];
} }
void cocoa_put_key_with_modifiers(int keycode, int modifiers)
{
keycode |= [mpv_shared_app().eventsResponder mapKeyModifiers:modifiers];
cocoa_put_key(keycode);
}
@implementation EventsResponder { @implementation EventsResponder {
CFMachPortRef _mk_tap_port; CFMachPortRef _mk_tap_port;
HIDRemote *_remote; HIDRemote *_remote;
@ -262,7 +266,7 @@ void cocoa_put_key(int keycode)
{ {
NSString *chars; NSString *chars;
if (RightAltPressed(event)) if (RightAltPressed([event modifierFlags]))
chars = [event characters]; chars = [event characters];
else else
chars = [event charactersIgnoringModifiers]; chars = [event charactersIgnoringModifiers];
@ -308,21 +312,25 @@ void cocoa_put_key(int keycode)
[self handleKey:buttonCode withMask:0 andMapping:keymap]; [self handleKey:buttonCode withMask:0 andMapping:keymap];
} }
- (int)keyModifierMask:(NSEvent *)event - (int)mapKeyModifiers:(int)cocoaModifiers
{ {
int mask = 0; int mask = 0;
if ([event modifierFlags] & NSShiftKeyMask) if (cocoaModifiers & NSShiftKeyMask)
mask |= MP_KEY_MODIFIER_SHIFT; mask |= MP_KEY_MODIFIER_SHIFT;
if ([event modifierFlags] & NSControlKeyMask) if (cocoaModifiers & NSControlKeyMask)
mask |= MP_KEY_MODIFIER_CTRL; mask |= MP_KEY_MODIFIER_CTRL;
if (LeftAltPressed(event)) if (LeftAltPressed(cocoaModifiers))
mask |= MP_KEY_MODIFIER_ALT; mask |= MP_KEY_MODIFIER_ALT;
if ([event modifierFlags] & NSCommandKeyMask) if (cocoaModifiers & NSCommandKeyMask)
mask |= MP_KEY_MODIFIER_META; mask |= MP_KEY_MODIFIER_META;
return mask; return mask;
} }
- (int)keyModifierMask:(NSEvent *)event
{
return [self mapKeyModifiers:[event modifierFlags]];
}
-(BOOL)handleKey:(int)key withMask:(int)mask andMapping:(NSDictionary *)mapping -(BOOL)handleKey:(int)key withMask:(int)mask andMapping:(NSDictionary *)mapping
{ {
int mpkey = [mapping[@(key)] intValue]; int mpkey = [mapping[@(key)] intValue];

View File

@ -56,7 +56,6 @@
@property(nonatomic, assign) struct vo *videoOutput; @property(nonatomic, assign) struct vo *videoOutput;
- (BOOL)containsMouseLocation; - (BOOL)containsMouseLocation;
- (void)recalcDraggableState; - (void)recalcDraggableState;
- (void)mouseEvent:(NSEvent *)theEvent;
@property(nonatomic, assign, getter=hasMouseDown) BOOL mouseDown; @property(nonatomic, assign, getter=hasMouseDown) BOOL mouseDown;
@end @end
@ -65,6 +64,10 @@
- (BOOL)hasMenubar; - (BOOL)hasMenubar;
@end @end
@interface NSEvent (mpvadditions)
- (int)mpvButtonNumber;
@end
struct vo_cocoa_state { struct vo_cocoa_state {
GLMPlayerWindow *window; GLMPlayerWindow *window;
GLMPlayerOpenGLView *view; GLMPlayerOpenGLView *view;
@ -888,73 +891,62 @@ int vo_cocoa_cgl_color_size(struct vo *vo)
- (void)mouseMoved:(NSEvent *)event { [self signalMouseMovement:event]; } - (void)mouseMoved:(NSEvent *)event { [self signalMouseMovement:event]; }
- (void)mouseDragged:(NSEvent *)event { [self signalMouseMovement:event]; } - (void)mouseDragged:(NSEvent *)event { [self signalMouseMovement:event]; }
- (void)mouseDown:(NSEvent *)evt { [self mouseEvent:evt]; } - (void)mouseDown:(NSEvent *)evt { [self mouseDownEvent:evt]; }
- (void)mouseUp:(NSEvent *)evt { [self mouseEvent:evt]; } - (void)mouseUp:(NSEvent *)evt { [self mouseUpEvent:evt]; }
- (void)rightMouseDown:(NSEvent *)evt { [self mouseEvent:evt]; } - (void)rightMouseDown:(NSEvent *)evt { [self mouseDownEvent:evt]; }
- (void)rightMouseUp:(NSEvent *)evt { [self mouseEvent:evt]; } - (void)rightMouseUp:(NSEvent *)evt { [self mouseUpEvent:evt]; }
- (void)otherMouseDown:(NSEvent *)evt { [self mouseEvent:evt]; } - (void)otherMouseDown:(NSEvent *)evt { [self mouseDownEvent:evt]; }
- (void)otherMouseUp:(NSEvent *)evt { [self mouseEvent:evt]; } - (void)otherMouseUp:(NSEvent *)evt { [self mouseUpEvent:evt]; }
- (void)scrollWheel:(NSEvent *)theEvent - (void)scrollWheel:(NSEvent *)event
{ {
struct vo_cocoa_state *s = self.videoOutput->cocoa; struct vo_cocoa_state *s = self.videoOutput->cocoa;
CGFloat delta; CGFloat delta;
// Use the dimention with the most delta as the scrolling one // Use the dimention with the most delta as the scrolling one
if (FFABS([theEvent deltaY]) > FFABS([theEvent deltaX])) { if (FFABS([event deltaY]) > FFABS([event deltaX])) {
delta = [theEvent deltaY]; delta = [event deltaY];
} else { } else {
delta = - [theEvent deltaX]; delta = - [event deltaX];
} }
if ([theEvent hasPreciseScrollingDeltas]) { if ([event hasPreciseScrollingDeltas]) {
s->accumulated_scroll += delta; s->accumulated_scroll += delta;
static const CGFloat threshold = 10; static const CGFloat threshold = 10;
while (s->accumulated_scroll >= threshold) { while (s->accumulated_scroll >= threshold) {
s->accumulated_scroll -= threshold; s->accumulated_scroll -= threshold;
cocoa_put_key(MP_MOUSE_BTN3); cocoa_put_key_with_modifiers(MP_MOUSE_BTN3, [event modifierFlags]);
} }
while (s->accumulated_scroll <= -threshold) { while (s->accumulated_scroll <= -threshold) {
s->accumulated_scroll += threshold; s->accumulated_scroll += threshold;
cocoa_put_key(MP_MOUSE_BTN4); cocoa_put_key_with_modifiers(MP_MOUSE_BTN4, [event modifierFlags]);
} }
} else { } else {
if (delta > 0) if (delta > 0)
cocoa_put_key(MP_MOUSE_BTN3); cocoa_put_key_with_modifiers(MP_MOUSE_BTN3, [event modifierFlags]);
else else
cocoa_put_key(MP_MOUSE_BTN4); cocoa_put_key_with_modifiers(MP_MOUSE_BTN4, [event modifierFlags]);
} }
} }
- (void)mouseEvent:(NSEvent *)theEvent - (void)mouseDownEvent:(NSEvent *)event
{ {
if ([theEvent buttonNumber] >= 0 && [theEvent buttonNumber] <= 9) { [self putMouseEvent:event withState:MP_KEY_STATE_DOWN];
int buttonNumber = [theEvent buttonNumber];
// Fix to mplayer defined button order: left, middle, right if ([event clickCount] > 1)
if (buttonNumber == 1) buttonNumber = 2; [self putMouseEvent:event withState:MP_KEY_STATE_UP];
else if (buttonNumber == 2) buttonNumber = 1; }
switch ([theEvent type]) {
case NSLeftMouseDown: - (void)mouseUpEvent:(NSEvent *)event
case NSRightMouseDown: {
case NSOtherMouseDown: [self putMouseEvent:event withState:MP_KEY_STATE_UP];
cocoa_put_key((MP_MOUSE_BTN0 + buttonNumber) | MP_KEY_STATE_DOWN); }
self.mouseDown = YES;
// Looks like Cocoa doesn't create MouseUp events when we are - (void)putMouseEvent:(NSEvent *)event withState:(int)state
// doing the second click in a double click. Put in the key_fifo {
// the key that would be put from the MouseUp handling code. self.mouseDown = (state == MP_KEY_STATE_DOWN);
if([theEvent clickCount] == 2) { int mp_key = (MP_MOUSE_BTN0 + [event mpvButtonNumber]);
cocoa_put_key((MP_MOUSE_BTN0 + buttonNumber) | MP_KEY_STATE_UP); cocoa_put_key_with_modifiers(mp_key | state, [event modifierFlags]);
self.mouseDown = NO;
}
break;
case NSLeftMouseUp:
case NSRightMouseUp:
case NSOtherMouseUp:
cocoa_put_key((MP_MOUSE_BTN0 + buttonNumber) | MP_KEY_STATE_UP);
self.mouseDown = NO;
break;
}
}
} }
- (void)drawRect: (NSRect)rect - (void)drawRect: (NSRect)rect
@ -998,3 +990,15 @@ int vo_cocoa_cgl_color_size(struct vo *vo)
return [self isEqual: [NSScreen screens][0]]; return [self isEqual: [NSScreen screens][0]];
} }
@end @end
@implementation NSEvent (mpvadditions)
- (int)mpvButtonNumber
{
int buttonNumber = [self buttonNumber];
switch (buttonNumber) {
case 1: return 2;
case 2: return 1;
default: return buttonNumber;
}
}
@end