Add SPMediaKeyTap back (#621)

This commit is contained in:
Mikalai Daronin
2021-01-06 16:46:21 +03:00
committed by GitHub
parent fb634bc2d4
commit 57709413e1
11 changed files with 500 additions and 9 deletions

View File

@@ -1240,6 +1240,7 @@ if(APPLE)
"-framework IOKit"
"-framework ScriptingBridge"
)
target_link_libraries(strawberry_lib PRIVATE ${SPMEDIAKEYTAP_LIBRARIES})
if(HAVE_SPARKLE)
target_include_directories(strawberry_lib SYSTEM PRIVATE ${SPARKLE}/Headers)
target_link_libraries(strawberry_lib PRIVATE ${SPARKLE})

View File

@@ -4,11 +4,13 @@
#include "globalshortcuts/globalshortcutbackend-macos.h"
class PlatformInterface;
@class SPMediaKeyTap;
@interface AppDelegate : NSObject<NSApplicationDelegate, NSUserNotificationCenterDelegate> {
PlatformInterface* application_handler_;
NSMenu* dock_menu_;
GlobalShortcutBackendMacOS* shortcut_handler_;
SPMediaKeyTap* key_tap_;
}
@@ -27,4 +29,5 @@ class PlatformInterface;
- (void) setDockMenu: (NSMenu*)menu;
- (GlobalShortcutBackendMacOS*) shortcut_handler;
- (void) setShortcutHandler: (GlobalShortcutBackendMacOS*)backend;
- (void) mediaKeyTap: (SPMediaKeyTap*)keyTap receivedMediaKeyEvent:(NSEvent*)event;
@end

View File

@@ -40,6 +40,8 @@
#import <QuartzCore/CALayer.h>
#import "3rdparty/SPMediaKeyTap/SPMediaKeyTap.h"
#include "config.h"
#include "mac_delegate.h"
@@ -146,6 +148,19 @@ QDebug operator<<(QDebug dbg, NSObject* object) {
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
Q_UNUSED(aNotification);
key_tap_ = [ [SPMediaKeyTap alloc] initWithDelegate:self];
if ([SPMediaKeyTap usesGlobalMediaKeyTap]) {
if ([key_tap_ startWatchingMediaKeys]) {
qLog(Debug) << "Media key monitoring started";
} else {
qLog(Warning) << "Failed to start media key monitoring";
}
}
else {
qLog(Warning) << "Media key monitoring disabled";
}
}
- (BOOL)application:(NSApplication*)app openFile:(NSString*)filename {
@@ -171,6 +186,26 @@ QDebug operator<<(QDebug dbg, NSObject* object) {
}
- (void) mediaKeyTap: (SPMediaKeyTap*)keyTap receivedMediaKeyEvent:(NSEvent*)event {
#pragma unused(keyTap)
[self handleMediaEvent:event];
}
- (BOOL) handleMediaEvent:(NSEvent*)event {
// if it is not a media key event, then ignore
if ([event type] == NSEventTypeSystemDefined && [event subtype] == 8) {
int keyCode = (([event data1] & 0xFFFF0000) >> 16);
int keyFlags = ([event data1] & 0x0000FFFF);
int keyIsReleased = (((keyFlags & 0xFF00) >> 8)) == 0xB;
if (keyIsReleased) {
shortcut_handler_->MacMediaKeyPressed(keyCode);
return YES;
}
}
return NO;
}
- (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication*) sender {
Q_UNUSED(sender);
return NSTerminateNow;
@@ -226,15 +261,7 @@ QDebug operator<<(QDebug dbg, NSObject* object) {
- (void)sendEvent:(NSEvent*)event {
if ([event type] == NSEventTypeSystemDefined && [event subtype] == 8) {
int keyCode = (([event data1] & 0xFFFF0000) >> 16);
int keyFlags = ([event data1] & 0x0000FFFF);
int keyIsReleased = (((keyFlags & 0xFF00) >> 8)) == 0xB;
if (keyIsReleased) {
shortcut_handler_->MacMediaKeyPressed(keyCode);
}
}
[delegate_ handleMediaEvent:event];
[super sendEvent:event];
}