strawberry-audio-player-win.../src/core/mac_startup.mm

423 lines
12 KiB
Plaintext
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#import <AppKit/NSApplication.h>
2019-08-16 22:52:50 +02:00
#import <AppKit/NSWindow.h>
2018-02-27 18:06:05 +01:00
#import <AppKit/NSEvent.h>
#import <AppKit/NSGraphics.h>
#import <AppKit/NSNibDeclarations.h>
#import <AppKit/NSViewController.h>
#import <Foundation/NSBundle.h>
#import <Foundation/NSError.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSPathUtilities.h>
#import <Foundation/NSProcessInfo.h>
#import <Foundation/NSThread.h>
#import <Foundation/NSTimer.h>
#import <Foundation/NSURL.h>
#import <IOKit/hidsystem/ev_keymap.h>
#import <Kernel/AvailabilityMacros.h>
#import <QuartzCore/CALayer.h>
2021-01-06 14:46:21 +01:00
#import "3rdparty/SPMediaKeyTap/SPMediaKeyTap.h"
2018-02-27 18:06:05 +01:00
#include "config.h"
#include "platforminterface.h"
2018-02-27 18:06:05 +01:00
#include "mac_delegate.h"
#include "mac_startup.h"
#include "mac_utilities.h"
#include "scoped_cftyperef.h"
2018-07-01 22:26:46 +02:00
#include "core/logging.h"
#include "core/scoped_nsautorelease_pool.h"
2021-01-26 19:13:29 +01:00
#include "globalshortcuts/globalshortcutsmanager.h"
#include "globalshortcuts/globalshortcutsbackend-macos.h"
2018-02-27 18:06:05 +01:00
#include <QApplication>
#include <QCoreApplication>
#include <QWidget>
2018-02-27 18:06:05 +01:00
#include <QDir>
#include <QEvent>
#include <QFile>
#include <QSettings>
#include <QtDebug>
2021-06-12 20:53:23 +02:00
QDebug operator<<(QDebug dbg, NSObject *object) {
2022-06-10 02:30:39 +02:00
QString ns_format = [[NSString stringWithFormat:@"%@", object] UTF8String];
2018-02-27 18:06:05 +01:00
dbg.nospace() << ns_format;
return dbg.space();
2018-02-27 18:06:05 +01:00
}
// Capture global media keys on Mac (Cocoa only!)
// See: http://www.rogueamoeba.com/utm/2007/09/29/apple-keyboard-media-key-event-handling/
@interface MacApplication : NSApplication {
2021-06-12 20:53:23 +02:00
PlatformInterface *application_handler_;
AppDelegate *delegate_;
// shortcut_handler_ only used to temporarily save it AppDelegate does all the heavy-shortcut-lifting
2021-06-12 20:53:23 +02:00
GlobalShortcutsBackendMacOS *shortcut_handler_;
2018-02-27 18:06:05 +01:00
}
2021-01-26 19:13:29 +01:00
- (GlobalShortcutsBackendMacOS*)shortcut_handler;
- (void)SetShortcutHandler:(GlobalShortcutsBackendMacOS*)handler;
2018-02-27 18:06:05 +01:00
- (PlatformInterface*)application_handler;
- (void)SetApplicationHandler:(PlatformInterface*)handler;
@end
@implementation AppDelegate
- (id)init {
2018-02-27 18:06:05 +01:00
if ((self = [super init])) {
application_handler_ = nil;
shortcut_handler_ = nil;
dock_menu_ = nil;
}
return self;
2018-02-27 18:06:05 +01:00
}
- (id)initWithHandler:(PlatformInterface*)handler {
application_handler_ = handler;
2018-02-27 18:06:05 +01:00
return self;
2018-02-27 18:06:05 +01:00
}
- (BOOL) applicationShouldHandleReopen: (NSApplication*)app hasVisibleWindows:(BOOL)flag {
2020-08-01 03:23:50 +02:00
Q_UNUSED(app);
Q_UNUSED(flag);
2018-02-27 18:06:05 +01:00
if (application_handler_) {
application_handler_->Activate();
}
return YES;
2018-02-27 18:06:05 +01:00
}
- (void)setDockMenu:(NSMenu*)menu {
dock_menu_ = menu;
}
- (NSMenu*)applicationDockMenu:(NSApplication*)sender {
2020-08-01 03:23:50 +02:00
Q_UNUSED(sender);
2018-02-27 18:06:05 +01:00
return dock_menu_;
}
2021-01-26 19:13:29 +01:00
- (void)setShortcutHandler:(GlobalShortcutsBackendMacOS*)backend {
2018-02-27 18:06:05 +01:00
shortcut_handler_ = backend;
}
2021-01-26 19:13:29 +01:00
- (GlobalShortcutsBackendMacOS*)shortcut_handler {
2018-02-27 18:06:05 +01:00
return shortcut_handler_;
}
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
2021-04-11 18:33:35 +02:00
2020-08-01 03:23:50 +02:00
Q_UNUSED(aNotification);
2021-01-06 14:46:21 +01:00
2021-04-11 18:33:35 +02:00
[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(handleURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
2022-06-10 02:30:39 +02:00
key_tap_ = [[SPMediaKeyTap alloc] initWithDelegate:self];
2021-01-06 14:46:21 +01:00
if ([SPMediaKeyTap usesGlobalMediaKeyTap]) {
if ([key_tap_ startWatchingMediaKeys]) {
qLog(Debug) << "Media key monitoring started";
2022-06-10 02:30:39 +02:00
}
else {
2021-01-06 14:46:21 +01:00
qLog(Warning) << "Failed to start media key monitoring";
}
}
else {
qLog(Warning) << "Media key monitoring disabled";
}
2018-02-27 18:06:05 +01:00
}
- (BOOL)application:(NSApplication*)app openFile:(NSString*)filename {
2020-08-01 03:23:50 +02:00
Q_UNUSED(app);
2018-02-27 18:06:05 +01:00
qLog(Debug) << "Wants to open:" << [filename UTF8String];
if (application_handler_->LoadUrl(QString::fromUtf8([filename UTF8String]))) {
return YES;
}
return NO;
2018-02-27 18:06:05 +01:00
}
- (void)application:(NSApplication*)app openFiles:(NSArray*)filenames {
2018-02-27 18:06:05 +01:00
qLog(Debug) << "Wants to open:" << filenames;
2020-07-17 16:36:24 +02:00
[filenames enumerateObjectsUsingBlock:^(id object, NSUInteger, BOOL*) {
2022-06-10 02:30:39 +02:00
[self application:app openFile:reinterpret_cast<NSString*>(object)];
}];
2018-02-27 18:06:05 +01:00
}
2021-04-11 18:33:35 +02:00
- (void)handleURLEvent:(NSAppleEventDescriptor*)theEvent withReplyEvent:(NSAppleEventDescriptor*)replyEvent {
2021-04-11 19:16:07 +02:00
#pragma unused(replyEvent)
2021-04-11 18:33:35 +02:00
NSString *url = [[theEvent paramDescriptorForKeyword:keyDirectObject] stringValue];
application_handler_->LoadUrl(QString::fromNSString(url));
}
2021-01-06 14:46:21 +01:00
- (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;
}
2018-02-27 18:06:05 +01:00
- (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication*) sender {
2020-08-01 03:23:50 +02:00
Q_UNUSED(sender);
2018-02-27 18:06:05 +01:00
return NSTerminateNow;
}
- (BOOL) userNotificationCenter: (id)center shouldPresentNotification: (id)notification {
2020-08-01 03:23:50 +02:00
Q_UNUSED(center);
Q_UNUSED(notification);
2018-02-27 18:06:05 +01:00
// Always show notifications, even if Strawberry is in the foreground.
return YES;
}
@end
@implementation MacApplication
- (id)init {
if ((self = [super init])) {
[self SetShortcutHandler:nil];
}
return self;
}
2021-01-26 19:13:29 +01:00
- (GlobalShortcutsBackendMacOS*)shortcut_handler {
2018-02-27 18:06:05 +01:00
// should be the same as delegate_'s shortcut handler
return shortcut_handler_;
}
2021-01-26 19:13:29 +01:00
- (void)SetShortcutHandler:(GlobalShortcutsBackendMacOS*)handler {
2018-02-27 18:06:05 +01:00
shortcut_handler_ = handler;
if (delegate_) [delegate_ setShortcutHandler:handler];
}
- (PlatformInterface*)application_handler {
return application_handler_;
}
- (void)SetApplicationHandler:(PlatformInterface*)handler {
2022-06-10 02:30:39 +02:00
delegate_ = [[AppDelegate alloc] initWithHandler:handler];
2018-02-27 18:06:05 +01:00
// App-shortcut-handler set before delegate is set.
// this makes sure the delegate's shortcut_handler is set
[delegate_ setShortcutHandler:shortcut_handler_];
[self setDelegate:delegate_];
// FIXME
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2022-06-10 02:30:39 +02:00
[[NSUserNotificationCenter defaultUserNotificationCenter]setDelegate:delegate_];
#pragma GCC diagnostic pop
2018-02-27 18:06:05 +01:00
}
- (void)sendEvent:(NSEvent*)event {
2021-01-06 14:46:21 +01:00
[delegate_ handleMediaEvent:event];
2018-02-27 18:06:05 +01:00
[super sendEvent:event];
2018-02-27 18:06:05 +01:00
}
@end
namespace mac {
void MacMain() {
2018-02-27 18:06:05 +01:00
ScopedNSAutoreleasePool pool;
// Creates and sets the magic global variable so QApplication will find it.
[MacApplication sharedApplication];
2018-02-27 18:06:05 +01:00
}
2021-06-12 20:53:23 +02:00
void SetShortcutHandler(GlobalShortcutsBackendMacOS *handler) {
2018-02-27 18:06:05 +01:00
[NSApp SetShortcutHandler:handler];
}
2021-06-12 20:53:23 +02:00
void SetApplicationHandler(PlatformInterface *handler) {
2018-02-27 18:06:05 +01:00
[NSApp SetApplicationHandler:handler];
}
static int MapFunctionKey(int keycode) {
2018-02-27 18:06:05 +01:00
switch (keycode) {
// Function keys
case NSInsertFunctionKey: return Qt::Key_Insert;
case NSDeleteFunctionKey: return Qt::Key_Delete;
case NSPauseFunctionKey: return Qt::Key_Pause;
case NSPrintFunctionKey: return Qt::Key_Print;
case NSSysReqFunctionKey: return Qt::Key_SysReq;
case NSHomeFunctionKey: return Qt::Key_Home;
case NSEndFunctionKey: return Qt::Key_End;
case NSLeftArrowFunctionKey: return Qt::Key_Left;
case NSUpArrowFunctionKey: return Qt::Key_Up;
case NSRightArrowFunctionKey: return Qt::Key_Right;
case NSDownArrowFunctionKey: return Qt::Key_Down;
case NSPageUpFunctionKey: return Qt::Key_PageUp;
case NSPageDownFunctionKey: return Qt::Key_PageDown;
case NSScrollLockFunctionKey: return Qt::Key_ScrollLock;
case NSF1FunctionKey: return Qt::Key_F1;
case NSF2FunctionKey: return Qt::Key_F2;
case NSF3FunctionKey: return Qt::Key_F3;
case NSF4FunctionKey: return Qt::Key_F4;
case NSF5FunctionKey: return Qt::Key_F5;
case NSF6FunctionKey: return Qt::Key_F6;
case NSF7FunctionKey: return Qt::Key_F7;
case NSF8FunctionKey: return Qt::Key_F8;
case NSF9FunctionKey: return Qt::Key_F9;
case NSF10FunctionKey: return Qt::Key_F10;
case NSF11FunctionKey: return Qt::Key_F11;
case NSF12FunctionKey: return Qt::Key_F12;
case NSF13FunctionKey: return Qt::Key_F13;
case NSF14FunctionKey: return Qt::Key_F14;
case NSF15FunctionKey: return Qt::Key_F15;
case NSF16FunctionKey: return Qt::Key_F16;
case NSF17FunctionKey: return Qt::Key_F17;
case NSF18FunctionKey: return Qt::Key_F18;
case NSF19FunctionKey: return Qt::Key_F19;
case NSF20FunctionKey: return Qt::Key_F20;
case NSF21FunctionKey: return Qt::Key_F21;
case NSF22FunctionKey: return Qt::Key_F22;
case NSF23FunctionKey: return Qt::Key_F23;
case NSF24FunctionKey: return Qt::Key_F24;
case NSF25FunctionKey: return Qt::Key_F25;
case NSF26FunctionKey: return Qt::Key_F26;
case NSF27FunctionKey: return Qt::Key_F27;
case NSF28FunctionKey: return Qt::Key_F28;
case NSF29FunctionKey: return Qt::Key_F29;
case NSF30FunctionKey: return Qt::Key_F30;
case NSF31FunctionKey: return Qt::Key_F31;
case NSF32FunctionKey: return Qt::Key_F32;
case NSF33FunctionKey: return Qt::Key_F33;
case NSF34FunctionKey: return Qt::Key_F34;
case NSF35FunctionKey: return Qt::Key_F35;
case NSMenuFunctionKey: return Qt::Key_Menu;
case NSHelpFunctionKey: return Qt::Key_Help;
}
return 0;
}
2021-06-12 20:53:23 +02:00
QKeySequence KeySequenceFromNSEvent(NSEvent *event) {
2021-06-12 20:53:23 +02:00
NSString *str = [event charactersIgnoringModifiers];
NSString *upper = [str uppercaseString];
const char *chars = [upper UTF8String];
2018-02-27 18:06:05 +01:00
NSUInteger modifiers = [event modifierFlags];
int key = 0;
unsigned char c = chars[0];
switch (c) {
case 0x1b: key = Qt::Key_Escape; break;
case 0x09: key = Qt::Key_Tab; break;
case 0x0d: key = Qt::Key_Return; break;
case 0x08: key = Qt::Key_Backspace; break;
case 0x03: key = Qt::Key_Enter; break;
}
if (key == 0) {
if (c >= 0x20 && c <= 0x7e) { // ASCII from space to ~
key = c;
}
else {
2018-02-27 18:06:05 +01:00
key = MapFunctionKey([event keyCode]);
if (key == 0) {
return QKeySequence();
}
}
}
2019-08-16 22:27:48 +02:00
if (modifiers & NSEventModifierFlagShift) {
2018-02-27 18:06:05 +01:00
key += Qt::SHIFT;
}
2019-08-16 22:27:48 +02:00
if (modifiers & NSEventModifierFlagControl) {
2018-02-27 18:06:05 +01:00
key += Qt::META;
}
2019-08-16 22:27:48 +02:00
if (modifiers & NSEventModifierFlagOption) {
2018-02-27 18:06:05 +01:00
key += Qt::ALT;
}
2019-08-16 22:27:48 +02:00
if (modifiers & NSEventModifierFlagCommand) {
2018-02-27 18:06:05 +01:00
key += Qt::CTRL;
}
return QKeySequence(key);
2018-02-27 18:06:05 +01:00
}
void DumpDictionary(CFDictionaryRef dict) {
2022-06-10 02:30:39 +02:00
NSDictionary *d = reinterpret_cast<NSDictionary*>(dict);
2018-02-27 18:06:05 +01:00
NSLog(@"%@", d);
2018-02-27 18:06:05 +01:00
}
// NSWindowCollectionBehaviorFullScreenPrimary
static const NSUInteger kFullScreenPrimary = 1 << 7;
2021-06-12 20:53:23 +02:00
void EnableFullScreen(const QWidget &main_window) {
2018-02-27 18:06:05 +01:00
2021-06-12 20:53:23 +02:00
NSView *view = reinterpret_cast<NSView*>(main_window.winId());
NSWindow *window = [view window];
2018-02-27 18:06:05 +01:00
[window setCollectionBehavior:kFullScreenPrimary];
2018-02-27 18:06:05 +01:00
}
} // namespace mac