summaryrefslogtreecommitdiffstats
path: root/osdep
diff options
context:
space:
mode:
authorStefano Pigozzi <stefano.pigozzi@gmail.com>2013-06-02 18:23:18 +0200
committerStefano Pigozzi <stefano.pigozzi@gmail.com>2013-06-03 22:31:14 +0200
commitc39efb96d1109b7a5960cd17cc4c392c2b93b61b (patch)
tree31090c2798ecd074ef29dcdcf0095014648aad3f /osdep
parentd67b687530967780f54a4a6a8fa89655394afbb8 (diff)
downloadmpv-c39efb96d1109b7a5960cd17cc4c392c2b93b61b.tar.bz2
mpv-c39efb96d1109b7a5960cd17cc4c392c2b93b61b.tar.xz
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.
Diffstat (limited to 'osdep')
-rw-r--r--osdep/macosx_application.m14
-rw-r--r--osdep/macosx_application_objc.h1
-rw-r--r--osdep/macosx_events.m19
3 files changed, 33 insertions, 1 deletions
diff --git a/osdep/macosx_application.m b/osdep/macosx_application.m
index 665a04b222..1329091e12 100644
--- a/osdep/macosx_application.m
+++ b/osdep/macosx_application.m
@@ -120,7 +120,19 @@ static NSString *escape_loadfile_name(NSString *input)
- (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);
diff --git a/osdep/macosx_application_objc.h b/osdep/macosx_application_objc.h
index 7c87ce6fa5..8abe9b98e2 100644
--- a/osdep/macosx_application_objc.h
+++ b/osdep/macosx_application_objc.h
@@ -27,6 +27,7 @@ struct cocoa_input_queue;
@end
@interface EventsResponder : NSResponder
+- (void)handleMediaKey:(int)key;
- (NSEvent *)handleKeyDown:(NSEvent *)event;
@end
diff --git a/osdep/macosx_events.m b/osdep/macosx_events.m
index d1228bb471..26853628b3 100644
--- a/osdep/macosx_events.m
+++ b/osdep/macosx_events.m
@@ -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 @@ void cocoa_put_key(int keycode)
{
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;