Skip to content
Permalink
Browse files

osx: implement media keys

Media keys are pretty handy if you use mpv as a music player (yes I'm one of
those people that do).

These are the bindings (which lead to the same behaviour as iTunes):
  * NX_KEYTYPE_PLAY   -> MP_KEY_PLAY
  * NX_KEYTYPE_FAST   -> MP_KEY_NEXT
  * NX_KEYTYPE_REWIND -> MP_KEY_PREV

I just handled these ones as the volume one would be pretty invasive. I could
maybe change it to increase the application's volume instead of system volume
only when mpv is frontmost (iTunes does this), but some users would probably
hate it.
  • Loading branch information...
pigoz committed Jun 2, 2013
1 parent d67b687 commit c39efb96d1109b7a5960cd17cc4c392c2b93b61b
Showing with 33 additions and 1 deletion.
  1. +13 −1 osdep/macosx_application.m
  2. +1 −0 osdep/macosx_application_objc.h
  3. +19 −0 osdep/macosx_events.m
@@ -120,7 +120,19 @@ @implementation Application

- (void)sendEvent:(NSEvent *)event
{
[super sendEvent:event];
if ([event type] == NSSystemDefined && [event subtype] == 8) {
// It's a media key! Handle it specially. The magic numbers are reverse
// engineered and found on several blog posts. Unfortunately there is
// no public API for this. F-bomb.
int code = (([event data1] & 0xFFFF0000) >> 16);
int flags = ([event data1] & 0x0000FFFF);
int down = (((flags & 0xFF00) >> 8)) == 0xA;

if (down)
[self.eventsResponder handleMediaKey:code];
} else {
[super sendEvent:event];
}

if (self.inputContext)
mp_input_wakeup(self.inputContext);
@@ -27,6 +27,7 @@ struct cocoa_input_queue;
@end

@interface EventsResponder : NSResponder
- (void)handleMediaKey:(int)key;
- (NSEvent *)handleKeyDown:(NSEvent *)event;
@end

@@ -20,6 +20,9 @@
// Carbon header is included but Carbon is NOT linked to mpv's binary. This
// file only needs this include to use the keycode definitions in keymap.
#import <Carbon/Carbon.h>

// Media keys definitions
#import <IOKit/hidsystem/ev_keymap.h>
#import <Cocoa/Cocoa.h>

#include "talloc.h"
@@ -107,6 +110,22 @@ - (NSArray *) keyEquivalents
{
return @[@"h", @"q", @"Q", @"0", @"1", @"2"];
}
- (void)handleMediaKey:(int)key
{
switch (key) {
case NX_KEYTYPE_PLAY:
cocoa_put_key(MP_KEY_PLAY);
break;

case NX_KEYTYPE_FAST:
cocoa_put_key(MP_KEY_NEXT);
break;

case NX_KEYTYPE_REWIND:
cocoa_put_key(MP_KEY_PREV);
break;
}
}
- (NSEvent*)handleKeyDown:(NSEvent *)event
{
NSString *chars;

5 comments on commit c39efb9

@Bilalh

This comment has been minimized.

Copy link
Contributor

replied Jun 3, 2013

Nice but iTunes still opens when pressing playpause(F8) , one way to get round this is to do what https://github.com/nevyn/SPMediaKeyTap does which is to catch the event before iTunes gets it.

I would be nice to config the mediakeys from the input.conf, since I use different settings for the mediakeys when playing music (previous/next track) compared with watching videos (seek -10s / +10s)

@franzf

This comment has been minimized.

Copy link

replied Jun 4, 2013

It would be nice to have a way to disable this, similar to --no-ar. I have itunes open 24/7, and I really dislike when applications interfere with it.

@pigoz

This comment has been minimized.

Copy link
Member Author

replied Jun 4, 2013

These are all valid issues. I will try to address them this evening.

one way to get round this is to do what https://github.com/nevyn/SPMediaKeyTap does which is to catch the event before iTunes gets it

I will modify this to use Quartz Event Taps. I don't want to implement application whitelisting from SPMediaKeyTap because imho that's a retarded solution :)

@Nyx0uf

This comment has been minimized.

Copy link
Contributor

replied Jun 4, 2013

I agree with @franzf my iTunes is always opened and I use the media keys exclusively with it.

@pigoz

This comment has been minimized.

Copy link
Member Author

replied Jun 4, 2013

I addressed all of your remarks in commit 213ad5d. Let me know if there's any other issue :)

Please sign in to comment.
You can’t perform that action at this time.