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-09-01 21:37:11 +02: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
|
|
|
*/
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2021-01-26 19:13:29 +01:00
|
|
|
#include "globalshortcutsbackend-macos.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
|
|
|
|
#include <AppKit/NSEvent.h>
|
|
|
|
#include <AppKit/NSWorkspace.h>
|
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
#include <IOKit/hidsystem/ev_keymap.h>
|
2019-08-06 20:31:54 +02:00
|
|
|
#include <ApplicationServices/ApplicationServices.h>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
#include <QAction>
|
|
|
|
#include <QList>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QPushButton>
|
|
|
|
|
|
|
|
#include "config.h"
|
2021-01-26 19:13:29 +01:00
|
|
|
#include "globalshortcutsmanager.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/mac_startup.h"
|
|
|
|
#include "core/utilities.h"
|
|
|
|
|
|
|
|
#import "core/mac_utilities.h"
|
2018-07-01 22:26:46 +02:00
|
|
|
#import "core/SBSystemPreferences.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-01-26 19:13:29 +01:00
|
|
|
class GlobalShortcutsBackendMacOSPrivate : boost::noncopyable {
|
2018-02-27 18:06:05 +01:00
|
|
|
public:
|
2021-06-12 20:53:23 +02:00
|
|
|
explicit GlobalShortcutsBackendMacOSPrivate(GlobalShortcutsBackendMacOS *backend)
|
2018-02-27 18:06:05 +01:00
|
|
|
: global_monitor_(nil), local_monitor_(nil), backend_(backend) {}
|
|
|
|
|
|
|
|
bool Register() {
|
2021-06-12 20:53:23 +02:00
|
|
|
global_monitor_ = [NSEvent addGlobalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^(NSEvent *event) {
|
2019-08-06 20:31:54 +02:00
|
|
|
HandleKeyEvent(event);
|
|
|
|
} ];
|
2021-06-12 20:53:23 +02:00
|
|
|
local_monitor_ = [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^(NSEvent *event) {
|
2018-02-27 18:06:05 +01:00
|
|
|
// Filter event if we handle it as a global shortcut.
|
|
|
|
return HandleKeyEvent(event) ? nil : event;
|
2019-08-06 20:31:54 +02:00
|
|
|
} ];
|
2018-02-27 18:06:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unregister() {
|
|
|
|
[NSEvent removeMonitor:global_monitor_];
|
|
|
|
[NSEvent removeMonitor:local_monitor_];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-06-12 20:53:23 +02:00
|
|
|
bool HandleKeyEvent(NSEvent *event) {
|
2018-02-27 18:06:05 +01:00
|
|
|
QKeySequence sequence = mac::KeySequenceFromNSEvent(event);
|
|
|
|
return backend_->KeyPressed(sequence);
|
|
|
|
}
|
|
|
|
|
|
|
|
id global_monitor_;
|
|
|
|
id local_monitor_;
|
2021-06-12 20:53:23 +02:00
|
|
|
GlobalShortcutsBackendMacOS *backend_;
|
2018-02-27 18:06:05 +01:00
|
|
|
};
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
GlobalShortcutsBackendMacOS::GlobalShortcutsBackendMacOS(GlobalShortcutsManager *manager, QObject *parent)
|
2021-09-01 21:37:11 +02:00
|
|
|
: GlobalShortcutsBackend(manager, GlobalShortcutsBackend::Type_MacOS, parent),
|
2021-01-26 19:13:29 +01:00
|
|
|
p_(new GlobalShortcutsBackendMacOSPrivate(this)) {}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-01-26 19:13:29 +01:00
|
|
|
GlobalShortcutsBackendMacOS::~GlobalShortcutsBackendMacOS() {}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-01-26 19:13:29 +01:00
|
|
|
bool GlobalShortcutsBackendMacOS::DoRegister() {
|
2019-08-06 20:31:54 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
// Always enable media keys.
|
|
|
|
mac::SetShortcutHandler(this);
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
QList<GlobalShortcutsManager::Shortcut> shortcuts = manager_->shortcuts().values();
|
|
|
|
for (const GlobalShortcutsManager::Shortcut &shortcut : shortcuts) {
|
2018-07-01 22:26:46 +02:00
|
|
|
shortcuts_[shortcut.action->shortcut()] = shortcut.action;
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return p_->Register();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:13:29 +01:00
|
|
|
void GlobalShortcutsBackendMacOS::DoUnregister() {
|
2019-08-06 20:31:54 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
p_->Unregister();
|
|
|
|
shortcuts_.clear();
|
2019-08-06 20:31:54 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-01 21:37:11 +02:00
|
|
|
void GlobalShortcutsBackendMacOS::MacMediaKeyPressed(const int key) {
|
2019-08-06 20:31:54 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
switch (key) {
|
|
|
|
case NX_KEYTYPE_PLAY:
|
|
|
|
KeyPressed(Qt::Key_MediaPlay);
|
|
|
|
break;
|
|
|
|
case NX_KEYTYPE_FAST:
|
|
|
|
KeyPressed(Qt::Key_MediaNext);
|
|
|
|
break;
|
|
|
|
case NX_KEYTYPE_REWIND:
|
|
|
|
KeyPressed(Qt::Key_MediaPrevious);
|
|
|
|
break;
|
|
|
|
}
|
2019-08-06 20:31:54 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
bool GlobalShortcutsBackendMacOS::KeyPressed(const QKeySequence &sequence) {
|
2019-08-06 20:31:54 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
if (sequence.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-12 20:53:23 +02:00
|
|
|
QAction *action = shortcuts_[sequence];
|
2018-02-27 18:06:05 +01:00
|
|
|
if (action) {
|
|
|
|
action->trigger();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2019-08-06 20:31:54 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-01 21:37:11 +02:00
|
|
|
bool GlobalShortcutsBackendMacOS::IsAccessibilityEnabled() {
|
2019-08-06 20:31:54 +02:00
|
|
|
|
2022-06-10 02:30:39 +02:00
|
|
|
NSDictionary *options = @{reinterpret_cast<id>(kAXTrustedCheckOptionPrompt): @YES};
|
|
|
|
return AXIsProcessTrustedWithOptions(reinterpret_cast<CFDictionaryRef>(options));
|
2019-08-06 20:31:54 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2021-01-26 19:13:29 +01:00
|
|
|
void GlobalShortcutsBackendMacOS::ShowAccessibilityDialog() {
|
2019-01-01 20:07:29 +01:00
|
|
|
|
2018-07-01 22:26:46 +02:00
|
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSSystemDomainMask, YES);
|
2018-02-27 18:06:05 +01:00
|
|
|
if ([paths count] == 1) {
|
2021-06-12 20:53:23 +02:00
|
|
|
SBSystemPreferencesApplication *system_prefs = [SBApplication applicationWithBundleIdentifier:@"com.apple.systempreferences"];
|
2018-02-27 18:06:05 +01:00
|
|
|
[system_prefs activate];
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
SBElementArray *panes = [system_prefs panes];
|
|
|
|
SBSystemPreferencesPane *security_pane = nil;
|
|
|
|
for (SBSystemPreferencesPane *pane : panes) {
|
2022-06-10 02:30:39 +02:00
|
|
|
if ([[pane id] isEqualToString:@"com.apple.preference.security"]) {
|
2018-07-01 22:26:46 +02:00
|
|
|
security_pane = pane;
|
|
|
|
break;
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
[system_prefs setCurrentPane:security_pane];
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
SBElementArray *anchors = [security_pane anchors];
|
|
|
|
for (SBSystemPreferencesAnchor *anchor : anchors) {
|
2022-06-10 02:30:39 +02:00
|
|
|
if ([[anchor name] isEqualToString:@"Privacy_Accessibility"]) {
|
2018-07-01 22:26:46 +02:00
|
|
|
[anchor reveal];
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-01 21:37:11 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|