Skip to content

Commit ce1f19c

Browse files
authored
Remove Carbon from Mac Auto-Type (keepassxreboot#3347)
* Fix keepassxreboot#3310
1 parent c27ee6a commit ce1f19c

File tree

9 files changed

+558
-321
lines changed

9 files changed

+558
-321
lines changed

src/autotype/mac/AutoTypeMac.cpp

Lines changed: 307 additions & 298 deletions
Large diffs are not rendered by default.

src/autotype/mac/AutoTypeMac.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
#ifndef KEEPASSX_AUTOTYPEMAC_H
2020
#define KEEPASSX_AUTOTYPEMAC_H
2121

22-
#include <Carbon/Carbon.h>
22+
#include <ApplicationServices/ApplicationServices.h>
2323
#include <QtPlugin>
2424
#include <memory>
2525

26-
#include "autotype/AutoTypePlatformPlugin.h"
2726
#include "autotype/AutoTypeAction.h"
27+
#include "autotype/AutoTypePlatformPlugin.h"
2828

2929
class AutoTypePlatformMac : public QObject, public AutoTypePlatformInterface
3030
{
@@ -48,20 +48,19 @@ class AutoTypePlatformMac : public QObject, public AutoTypePlatformInterface
4848
bool raiseOwnWindow() override;
4949

5050
void sendChar(const QChar& ch, bool isKeyDown);
51-
void sendKey(Qt::Key key, bool isKeyDown, Qt::KeyboardModifiers modifiers);
51+
void sendKey(Qt::Key key, bool isKeyDown, Qt::KeyboardModifiers modifiers = 0);
5252

5353
signals:
5454
void globalShortcutTriggered();
5555

5656
private:
57-
EventHotKeyRef m_hotkeyRef;
58-
EventHotKeyID m_hotkeyId;
59-
60-
static uint16 qtToNativeKeyCode(Qt::Key key);
61-
static CGEventFlags qtToNativeModifiers(Qt::KeyboardModifiers modifiers, bool native);
57+
static void hotkeyHandler(void* userData);
58+
static CGKeyCode qtToNativeKeyCode(Qt::Key key);
59+
static CGEventFlags qtToNativeModifiers(Qt::KeyboardModifiers modifiers);
6260
static int windowLayer(CFDictionaryRef window);
6361
static QString windowTitle(CFDictionaryRef window);
64-
static OSStatus hotkeyHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData);
62+
63+
void* m_globalMonitor;
6564
};
6665

6766
class AutoTypeExecutorMac : public AutoTypeExecutor
@@ -77,4 +76,4 @@ class AutoTypeExecutorMac : public AutoTypeExecutor
7776
AutoTypePlatformMac* const m_platform;
7877
};
7978

80-
#endif // KEEPASSX_AUTOTYPEMAC_H
79+
#endif // KEEPASSX_AUTOTYPEMAC_H
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
//
2+
// Taken from HIToolbox/Events.h
3+
//
4+
5+
/*
6+
* Summary:
7+
* Virtual keycodes
8+
*
9+
* Discussion:
10+
* These constants are the virtual keycodes defined originally in
11+
* Inside Mac Volume V, pg. V-191. They identify physical keys on a
12+
* keyboard. Those constants with "ANSI" in the name are labeled
13+
* according to the key position on an ANSI-standard US keyboard.
14+
* For example, kVK_ANSI_A indicates the virtual keycode for the key
15+
* with the letter 'A' in the US keyboard layout. Other keyboard
16+
* layouts may have the 'A' key label on a different physical key;
17+
* in this case, pressing 'A' will generate a different virtual
18+
* keycode.
19+
*/
20+
21+
#ifndef KEEPASSXC_AUTOTYPEMAC_KEYCODES_H
22+
#define KEEPASSXC_AUTOTYPEMAC_KEYCODES_H
23+
24+
// clang-format off
25+
enum {
26+
kVK_ANSI_A = 0x00,
27+
kVK_ANSI_S = 0x01,
28+
kVK_ANSI_D = 0x02,
29+
kVK_ANSI_F = 0x03,
30+
kVK_ANSI_H = 0x04,
31+
kVK_ANSI_G = 0x05,
32+
kVK_ANSI_Z = 0x06,
33+
kVK_ANSI_X = 0x07,
34+
kVK_ANSI_C = 0x08,
35+
kVK_ANSI_V = 0x09,
36+
kVK_ANSI_B = 0x0B,
37+
kVK_ANSI_Q = 0x0C,
38+
kVK_ANSI_W = 0x0D,
39+
kVK_ANSI_E = 0x0E,
40+
kVK_ANSI_R = 0x0F,
41+
kVK_ANSI_Y = 0x10,
42+
kVK_ANSI_T = 0x11,
43+
kVK_ANSI_1 = 0x12,
44+
kVK_ANSI_2 = 0x13,
45+
kVK_ANSI_3 = 0x14,
46+
kVK_ANSI_4 = 0x15,
47+
kVK_ANSI_6 = 0x16,
48+
kVK_ANSI_5 = 0x17,
49+
kVK_ANSI_Equal = 0x18,
50+
kVK_ANSI_9 = 0x19,
51+
kVK_ANSI_7 = 0x1A,
52+
kVK_ANSI_Minus = 0x1B,
53+
kVK_ANSI_8 = 0x1C,
54+
kVK_ANSI_0 = 0x1D,
55+
kVK_ANSI_RightBracket = 0x1E,
56+
kVK_ANSI_O = 0x1F,
57+
kVK_ANSI_U = 0x20,
58+
kVK_ANSI_LeftBracket = 0x21,
59+
kVK_ANSI_I = 0x22,
60+
kVK_ANSI_P = 0x23,
61+
kVK_ANSI_L = 0x25,
62+
kVK_ANSI_J = 0x26,
63+
kVK_ANSI_Quote = 0x27,
64+
kVK_ANSI_K = 0x28,
65+
kVK_ANSI_Semicolon = 0x29,
66+
kVK_ANSI_Backslash = 0x2A,
67+
kVK_ANSI_Comma = 0x2B,
68+
kVK_ANSI_Slash = 0x2C,
69+
kVK_ANSI_N = 0x2D,
70+
kVK_ANSI_M = 0x2E,
71+
kVK_ANSI_Period = 0x2F,
72+
kVK_ANSI_Grave = 0x32,
73+
kVK_ANSI_KeypadDecimal = 0x41,
74+
kVK_ANSI_KeypadMultiply = 0x43,
75+
kVK_ANSI_KeypadPlus = 0x45,
76+
kVK_ANSI_KeypadClear = 0x47,
77+
kVK_ANSI_KeypadDivide = 0x4B,
78+
kVK_ANSI_KeypadEnter = 0x4C,
79+
kVK_ANSI_KeypadMinus = 0x4E,
80+
kVK_ANSI_KeypadEquals = 0x51,
81+
kVK_ANSI_Keypad0 = 0x52,
82+
kVK_ANSI_Keypad1 = 0x53,
83+
kVK_ANSI_Keypad2 = 0x54,
84+
kVK_ANSI_Keypad3 = 0x55,
85+
kVK_ANSI_Keypad4 = 0x56,
86+
kVK_ANSI_Keypad5 = 0x57,
87+
kVK_ANSI_Keypad6 = 0x58,
88+
kVK_ANSI_Keypad7 = 0x59,
89+
kVK_ANSI_Keypad8 = 0x5B,
90+
kVK_ANSI_Keypad9 = 0x5C
91+
};
92+
93+
/* keycodes for keys that are independent of keyboard layout*/
94+
enum {
95+
kVK_Return = 0x24,
96+
kVK_Tab = 0x30,
97+
kVK_Space = 0x31,
98+
kVK_Delete = 0x33,
99+
kVK_Escape = 0x35,
100+
kVK_Command = 0x37,
101+
kVK_Shift = 0x38,
102+
kVK_CapsLock = 0x39,
103+
kVK_Option = 0x3A,
104+
kVK_Control = 0x3B,
105+
kVK_RightCommand = 0x36,
106+
kVK_RightShift = 0x3C,
107+
kVK_RightOption = 0x3D,
108+
kVK_RightControl = 0x3E,
109+
kVK_Function = 0x3F,
110+
kVK_F17 = 0x40,
111+
kVK_VolumeUp = 0x48,
112+
kVK_VolumeDown = 0x49,
113+
kVK_Mute = 0x4A,
114+
kVK_F18 = 0x4F,
115+
kVK_F19 = 0x50,
116+
kVK_F20 = 0x5A,
117+
kVK_F5 = 0x60,
118+
kVK_F6 = 0x61,
119+
kVK_F7 = 0x62,
120+
kVK_F3 = 0x63,
121+
kVK_F8 = 0x64,
122+
kVK_F9 = 0x65,
123+
kVK_F11 = 0x67,
124+
kVK_F13 = 0x69,
125+
kVK_F16 = 0x6A,
126+
kVK_F14 = 0x6B,
127+
kVK_F10 = 0x6D,
128+
kVK_F12 = 0x6F,
129+
kVK_F15 = 0x71,
130+
kVK_Help = 0x72,
131+
kVK_Home = 0x73,
132+
kVK_PageUp = 0x74,
133+
kVK_ForwardDelete = 0x75,
134+
kVK_F4 = 0x76,
135+
kVK_End = 0x77,
136+
kVK_F2 = 0x78,
137+
kVK_PageDown = 0x79,
138+
kVK_F1 = 0x7A,
139+
kVK_LeftArrow = 0x7B,
140+
kVK_RightArrow = 0x7C,
141+
kVK_DownArrow = 0x7D,
142+
kVK_UpArrow = 0x7E
143+
};
144+
145+
/* ISO keyboards only*/
146+
enum {
147+
kVK_ISO_Section = 0x0A
148+
};
149+
150+
/* JIS keyboards only*/
151+
enum {
152+
kVK_JIS_Yen = 0x5D,
153+
kVK_JIS_Underscore = 0x5E,
154+
kVK_JIS_KeypadComma = 0x5F,
155+
kVK_JIS_Eisu = 0x66,
156+
kVK_JIS_Kana = 0x68
157+
};
158+
// clang-format on
159+
160+
#endif

src/autotype/mac/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
set(autotype_mac_SOURCES AutoTypeMac.cpp)
22

33
add_library(keepassx-autotype-cocoa MODULE ${autotype_mac_SOURCES})
4-
set_target_properties(keepassx-autotype-cocoa PROPERTIES LINK_FLAGS "-framework Foundation -framework AppKit -framework Carbon")
4+
set_target_properties(keepassx-autotype-cocoa PROPERTIES LINK_FLAGS "-framework Foundation -framework AppKit")
55
target_link_libraries(keepassx-autotype-cocoa ${PROGNAME} Qt5::Core Qt5::Widgets)
66

77
if(WITH_APP_BUNDLE)

src/gui/macutils/AppKit.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#define KEEPASSX_APPKIT_H
2121

2222
#include <QObject>
23-
#include <unistd.h>
23+
#include <CoreGraphics/CGEvent.h>
2424

2525
class AppKit : public QObject
2626
{
@@ -37,12 +37,15 @@ class AppKit : public QObject
3737
bool hideProcess(pid_t pid);
3838
bool isHidden(pid_t pid);
3939
bool isDarkMode();
40+
void* addGlobalMonitor(CGKeyCode keycode, CGEventFlags modifier, void* userData, void (*handler)(void*));
41+
void removeGlobalMonitor(void* monitor);
42+
bool enableAccessibility();
4043

4144
signals:
4245
void lockDatabases();
4346

4447
private:
45-
void *self;
48+
void* self;
4649
};
4750

4851
#endif // KEEPASSX_APPKIT_H

src/gui/macutils/AppKitImpl.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
#import "AppKit.h"
2020

2121
#import <Foundation/Foundation.h>
22+
#import <AppKit/NSEvent.h>
2223
#import <AppKit/NSRunningApplication.h>
2324

2425
@interface AppKitImpl : NSObject
2526
{
26-
AppKit *m_appkit;
27+
AppKit* m_appkit;
2728
}
28-
- (id) initWithObject:(AppKit *)appkit;
29+
- (id) initWithObject:(AppKit*)appkit;
2930

3031
@property (strong) NSRunningApplication *lastActiveApplication;
3132

@@ -36,5 +37,8 @@
3637
- (bool) isHidden:(pid_t) pid;
3738
- (bool) isDarkMode;
3839
- (void) userSwitchHandler:(NSNotification*) notification;
40+
- (id) addGlobalMonitor:(NSEventMask) mask handler:(void (^)(NSEvent*)) handler;
41+
- (void) removeGlobalMonitor:(id) monitor;
42+
- (bool) enableAccessibility;
3943

4044
@end

src/gui/macutils/AppKitImpl.mm

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
@implementation AppKitImpl
2424

25-
- (id) initWithObject:(AppKit *)appkit
25+
- (id) initWithObject:(AppKit*)appkit
2626
{
2727
self = [super init];
2828
if (self) {
@@ -43,10 +43,10 @@ - (id) initWithObject:(AppKit *)appkit
4343
//
4444
// Update last active application property
4545
//
46-
- (void) didDeactivateApplicationObserver:(NSNotification *) notification
46+
- (void) didDeactivateApplicationObserver:(NSNotification*) notification
4747
{
48-
NSDictionary *userInfo = notification.userInfo;
49-
NSRunningApplication *app = userInfo[NSWorkspaceApplicationKey];
48+
NSDictionary* userInfo = notification.userInfo;
49+
NSRunningApplication* app = userInfo[NSWorkspaceApplicationKey];
5050

5151
if (app.processIdentifier != [self ownProcessId]) {
5252
self.lastActiveApplication = app;
@@ -74,7 +74,7 @@ - (pid_t) ownProcessId
7474
//
7575
- (bool) activateProcess:(pid_t) pid
7676
{
77-
NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
77+
NSRunningApplication* app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
7878
return [app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
7979
}
8080

@@ -83,7 +83,7 @@ - (bool) activateProcess:(pid_t) pid
8383
//
8484
- (bool) hideProcess:(pid_t) pid
8585
{
86-
NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
86+
NSRunningApplication* app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
8787
return [app hide];
8888
}
8989

@@ -92,7 +92,7 @@ - (bool) hideProcess:(pid_t) pid
9292
//
9393
- (bool) isHidden:(pid_t) pid
9494
{
95-
NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
95+
NSRunningApplication* app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
9696
return [app isHidden];
9797
}
9898

@@ -101,7 +101,7 @@ - (bool) isHidden:(pid_t) pid
101101
//
102102
- (bool) isDarkMode
103103
{
104-
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] persistentDomainForName:NSGlobalDomain];
104+
NSDictionary* dict = [[NSUserDefaults standardUserDefaults] persistentDomainForName:NSGlobalDomain];
105105
id style = [dict objectForKey:@"AppleInterfaceStyle"];
106106
return ( style && [style isKindOfClass:[NSString class]]
107107
&& NSOrderedSame == [style caseInsensitiveCompare:@"dark"] );
@@ -118,6 +118,31 @@ - (void) userSwitchHandler:(NSNotification*) notification
118118
}
119119
}
120120

121+
//
122+
// Add global event monitor
123+
//
124+
- (id) addGlobalMonitor:(NSEventMask) mask handler:(void (^)(NSEvent*)) handler
125+
{
126+
return [NSEvent addGlobalMonitorForEventsMatchingMask:mask handler:handler];
127+
}
128+
129+
//
130+
// Remove global event monitor
131+
//
132+
- (void) removeGlobalMonitor:(id) monitor
133+
{
134+
[NSEvent removeMonitor:monitor];
135+
}
136+
137+
//
138+
// Check if accessibility is enabled, may show an popup asking for permissions
139+
//
140+
- (bool) enableAccessibility
141+
{
142+
NSDictionary* opts = @{static_cast<id>(kAXTrustedCheckOptionPrompt): @YES};
143+
return AXIsProcessTrustedWithOptions(static_cast<CFDictionaryRef>(opts));
144+
}
145+
121146
@end
122147

123148
//
@@ -169,3 +194,22 @@ - (void) userSwitchHandler:(NSNotification*) notification
169194
{
170195
return [static_cast<id>(self) isDarkMode];
171196
}
197+
198+
void* AppKit::addGlobalMonitor(CGKeyCode keycode, CGEventFlags modifier, void* userData, void (*handler)(void*))
199+
{
200+
return [static_cast<id>(self) addGlobalMonitor:NSEventMaskKeyDown handler:^(NSEvent* event) {
201+
if (event.keyCode == keycode && (event.modifierFlags & modifier) == modifier) {
202+
handler(userData);
203+
}
204+
}];
205+
}
206+
207+
void AppKit::removeGlobalMonitor(void* monitor)
208+
{
209+
[static_cast<id>(self) removeGlobalMonitor:static_cast<id>(monitor)];
210+
}
211+
212+
bool AppKit::enableAccessibility()
213+
{
214+
return [static_cast<id>(self) enableAccessibility];
215+
}

0 commit comments

Comments
 (0)