2010-06-17 22:31:34 +02:00
|
|
|
/* This file is part of Clementine.
|
2014-12-10 01:25:52 +01:00
|
|
|
Copyright 2010, David Sansome <davidsansome@gmail.com>
|
|
|
|
Copyright 2010-2011, 2014, John Maguire <john.maguire@gmail.com>
|
2010-06-17 22:31:34 +02:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "macglobalshortcutbackend.h"
|
|
|
|
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
|
2014-04-02 16:12:03 +02:00
|
|
|
#include <AppKit/NSEvent.h>
|
|
|
|
#include <AppKit/NSWorkspace.h>
|
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
#include <IOKit/hidsystem/ev_keymap.h>
|
2018-11-23 06:11:36 +01:00
|
|
|
#include <ApplicationServices/ApplicationServices.h>
|
2014-04-02 16:12:03 +02:00
|
|
|
|
2010-06-17 22:31:34 +02:00
|
|
|
#include <QAction>
|
|
|
|
#include <QList>
|
2010-06-17 23:33:16 +02:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QPushButton>
|
2010-06-17 22:31:34 +02:00
|
|
|
#include <QtDebug>
|
|
|
|
|
2014-04-02 16:12:03 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "core/globalshortcuts.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/mac_startup.h"
|
|
|
|
#include "core/utilities.h"
|
|
|
|
|
|
|
|
#import "core/mac_utilities.h"
|
|
|
|
#import "mac/SBSystemPreferences.h"
|
2010-06-17 22:31:34 +02:00
|
|
|
|
|
|
|
class MacGlobalShortcutBackendPrivate : boost::noncopyable {
|
|
|
|
public:
|
|
|
|
explicit MacGlobalShortcutBackendPrivate(MacGlobalShortcutBackend* backend)
|
2014-01-30 14:48:49 +01:00
|
|
|
: global_monitor_(nil), local_monitor_(nil), backend_(backend) {}
|
2010-06-17 22:31:34 +02:00
|
|
|
|
|
|
|
bool Register() {
|
2014-01-30 14:48:49 +01:00
|
|
|
global_monitor_ =
|
|
|
|
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask
|
|
|
|
handler:^(NSEvent* event) {
|
|
|
|
HandleKeyEvent(event);
|
|
|
|
}];
|
|
|
|
local_monitor_ =
|
|
|
|
[NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask
|
|
|
|
handler:^(NSEvent* event) {
|
|
|
|
// Filter event if we handle
|
|
|
|
// it as a global shortcut.
|
|
|
|
return HandleKeyEvent(event)
|
|
|
|
? nil
|
|
|
|
: event;
|
|
|
|
}];
|
2010-06-17 22:31:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unregister() {
|
2010-06-17 23:33:16 +02:00
|
|
|
[NSEvent removeMonitor:global_monitor_];
|
|
|
|
[NSEvent removeMonitor:local_monitor_];
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:31:34 +02:00
|
|
|
private:
|
2010-09-13 12:14:46 +02:00
|
|
|
bool HandleKeyEvent(NSEvent* event) {
|
2011-09-01 15:10:30 +02:00
|
|
|
QKeySequence sequence = mac::KeySequenceFromNSEvent(event);
|
2010-09-13 12:14:46 +02:00
|
|
|
return backend_->KeyPressed(sequence);
|
2010-06-17 22:31:34 +02:00
|
|
|
}
|
|
|
|
|
2010-06-17 23:33:16 +02:00
|
|
|
id global_monitor_;
|
|
|
|
id local_monitor_;
|
2010-06-17 22:31:34 +02:00
|
|
|
MacGlobalShortcutBackend* backend_;
|
|
|
|
};
|
|
|
|
|
|
|
|
MacGlobalShortcutBackend::MacGlobalShortcutBackend(GlobalShortcuts* parent)
|
2014-01-30 14:48:49 +01:00
|
|
|
: GlobalShortcutBackend(parent),
|
|
|
|
p_(new MacGlobalShortcutBackendPrivate(this)) {}
|
2010-06-17 22:31:34 +02:00
|
|
|
|
2014-01-30 14:48:49 +01:00
|
|
|
MacGlobalShortcutBackend::~MacGlobalShortcutBackend() {}
|
2010-06-17 22:31:34 +02:00
|
|
|
|
|
|
|
bool MacGlobalShortcutBackend::DoRegister() {
|
2010-06-17 23:33:16 +02:00
|
|
|
// Always enable media keys.
|
2010-06-17 22:31:34 +02:00
|
|
|
mac::SetShortcutHandler(this);
|
2010-06-17 23:33:16 +02:00
|
|
|
|
2015-03-15 13:40:27 +01:00
|
|
|
for (const GlobalShortcuts::Shortcut& shortcut :
|
|
|
|
manager_->shortcuts().values()) {
|
|
|
|
shortcuts_[shortcut.action->shortcut()] = shortcut.action;
|
2010-06-17 22:31:34 +02:00
|
|
|
}
|
2015-03-15 13:40:27 +01:00
|
|
|
return p_->Register();
|
2010-06-17 22:31:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MacGlobalShortcutBackend::DoUnregister() {
|
|
|
|
p_->Unregister();
|
2010-06-17 23:33:16 +02:00
|
|
|
shortcuts_.clear();
|
2010-06-17 22:31:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MacGlobalShortcutBackend::MacMediaKeyPressed(int key) {
|
|
|
|
switch (key) {
|
|
|
|
case NX_KEYTYPE_PLAY:
|
2010-06-17 23:54:42 +02:00
|
|
|
KeyPressed(Qt::Key_MediaPlay);
|
2010-06-17 22:31:34 +02:00
|
|
|
break;
|
|
|
|
case NX_KEYTYPE_FAST:
|
2010-06-17 23:54:42 +02:00
|
|
|
KeyPressed(Qt::Key_MediaNext);
|
2010-06-17 22:31:34 +02:00
|
|
|
break;
|
|
|
|
case NX_KEYTYPE_REWIND:
|
2010-06-17 23:54:42 +02:00
|
|
|
KeyPressed(Qt::Key_MediaPrevious);
|
2010-06-17 22:31:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-13 12:14:46 +02:00
|
|
|
bool MacGlobalShortcutBackend::KeyPressed(const QKeySequence& sequence) {
|
2010-06-29 20:44:36 +02:00
|
|
|
if (sequence.isEmpty()) {
|
2010-09-13 12:14:46 +02:00
|
|
|
return false;
|
2010-06-29 20:44:36 +02:00
|
|
|
}
|
2010-06-17 22:31:34 +02:00
|
|
|
QAction* action = shortcuts_[sequence];
|
|
|
|
if (action) {
|
|
|
|
action->trigger();
|
2010-09-13 12:14:46 +02:00
|
|
|
return true;
|
2010-06-17 22:31:34 +02:00
|
|
|
}
|
2010-09-13 12:14:46 +02:00
|
|
|
return false;
|
2010-06-17 22:31:34 +02:00
|
|
|
}
|
2010-06-17 23:33:16 +02:00
|
|
|
|
2010-06-18 01:11:51 +02:00
|
|
|
bool MacGlobalShortcutBackend::IsAccessibilityEnabled() const {
|
2018-11-23 06:11:36 +01:00
|
|
|
bool accessibilityEnabled;
|
|
|
|
try{
|
|
|
|
accessibilityEnabled = AXAPIEnabled();
|
|
|
|
}catch(...){
|
|
|
|
NSDictionary *options = @{(id)kAXTrustedCheckOptionPrompt: @YES};
|
|
|
|
accessibilityEnabled = AXIsProcessTrustedWithOptions((CFDictionaryRef)options);
|
|
|
|
}
|
|
|
|
return accessibilityEnabled;
|
2010-06-18 01:11:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MacGlobalShortcutBackend::ShowAccessibilityDialog() {
|
|
|
|
NSArray* paths = NSSearchPathForDirectoriesInDomains(
|
|
|
|
NSPreferencePanesDirectory, NSSystemDomainMask, YES);
|
|
|
|
if ([paths count] == 1) {
|
2015-02-18 18:03:05 +01:00
|
|
|
SBSystemPreferencesApplication* system_prefs = [SBApplication
|
|
|
|
applicationWithBundleIdentifier:@"com.apple.systempreferences"];
|
|
|
|
[system_prefs activate];
|
|
|
|
|
|
|
|
SBElementArray* panes = [system_prefs panes];
|
|
|
|
SBSystemPreferencesPane* security_pane = nil;
|
|
|
|
for (SBSystemPreferencesPane* pane : panes) {
|
|
|
|
if ([[pane id] isEqualToString:@"com.apple.preference.security"]) {
|
|
|
|
security_pane = pane;
|
|
|
|
break;
|
2014-04-02 16:12:03 +02:00
|
|
|
}
|
2015-02-18 18:03:05 +01:00
|
|
|
}
|
|
|
|
[system_prefs setCurrentPane:security_pane];
|
2014-04-02 16:12:03 +02:00
|
|
|
|
2015-02-18 18:03:05 +01:00
|
|
|
SBElementArray* anchors = [security_pane anchors];
|
|
|
|
for (SBSystemPreferencesAnchor* anchor : anchors) {
|
|
|
|
if ([[anchor name] isEqualToString:@"Privacy_Accessibility"]) {
|
|
|
|
[anchor reveal];
|
2014-04-02 16:12:03 +02:00
|
|
|
}
|
|
|
|
}
|
2010-06-18 01:11:51 +02:00
|
|
|
}
|
2010-06-17 23:33:16 +02:00
|
|
|
}
|