1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/* This file is part of Clementine.
Copyright 2010, David Sansome <davidsansome@gmail.com>
Copyright 2010-2011, 2014, John Maguire <john.maguire@gmail.com>
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"
#ifndef Q_MOC_RUN
#include <boost/noncopyable.hpp>
#endif
#include <AppKit/NSEvent.h>
#include <AppKit/NSWorkspace.h>
#include <ApplicationServices/ApplicationServices.h>
#include <Foundation/NSString.h>
#include <IOKit/hidsystem/ev_keymap.h>
#include <QAction>
#include <QList>
#include <QMessageBox>
#include <QPushButton>
#include <QtDebug>
#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"
class MacGlobalShortcutBackendPrivate : boost::noncopyable {
public:
explicit MacGlobalShortcutBackendPrivate(MacGlobalShortcutBackend* backend)
: global_monitor_(nil), local_monitor_(nil), backend_(backend) {}
bool Register() {
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;
}];
return true;
}
void Unregister() {
[NSEvent removeMonitor:global_monitor_];
[NSEvent removeMonitor:local_monitor_];
}
private:
bool HandleKeyEvent(NSEvent* event) {
QKeySequence sequence = mac::KeySequenceFromNSEvent(event);
return backend_->KeyPressed(sequence);
}
id global_monitor_;
id local_monitor_;
MacGlobalShortcutBackend* backend_;
};
MacGlobalShortcutBackend::MacGlobalShortcutBackend(GlobalShortcuts* parent)
: GlobalShortcutBackend(parent), p_(new MacGlobalShortcutBackendPrivate(this)) {}
MacGlobalShortcutBackend::~MacGlobalShortcutBackend() {}
bool MacGlobalShortcutBackend::DoRegister() {
// Always enable media keys.
mac::SetShortcutHandler(this);
for (const GlobalShortcuts::Shortcut& shortcut : manager_->shortcuts().values()) {
shortcuts_[shortcut.action->shortcut()] = shortcut.action;
}
return p_->Register();
}
void MacGlobalShortcutBackend::DoUnregister() {
p_->Unregister();
shortcuts_.clear();
}
void MacGlobalShortcutBackend::MacMediaKeyPressed(int key) {
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;
}
}
bool MacGlobalShortcutBackend::KeyPressed(const QKeySequence& sequence) {
if (sequence.isEmpty()) {
return false;
}
QAction* action = shortcuts_[sequence];
if (action) {
action->trigger();
return true;
}
return false;
}
bool MacGlobalShortcutBackend::IsAccessibilityEnabled() const {
bool accessibilityEnabled;
try {
accessibilityEnabled = AXAPIEnabled();
} catch (...) {
NSDictionary* options = @{(id)kAXTrustedCheckOptionPrompt : @YES};
accessibilityEnabled = AXIsProcessTrustedWithOptions((CFDictionaryRef)options);
}
return accessibilityEnabled;
}
void MacGlobalShortcutBackend::ShowAccessibilityDialog() {
NSArray* paths =
NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSSystemDomainMask, YES);
if ([paths count] == 1) {
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;
}
}
[system_prefs setCurrentPane:security_pane];
SBElementArray* anchors = [security_pane anchors];
for (SBSystemPreferencesAnchor* anchor : anchors) {
if ([[anchor name] isEqualToString:@"Privacy_Accessibility"]) {
[anchor reveal];
}
}
}
}
|