Skip to content

Commit

Permalink
Merge pull request #10066 from keymanapp/chore/mac/use-core-actions-s…
Browse files Browse the repository at this point in the history
…truct

chore(mac): use core actions struct
  • Loading branch information
sgschantz authored Dec 14, 2023
2 parents 4df5132 + 4b5495e commit 55f69ae
Show file tree
Hide file tree
Showing 33 changed files with 579 additions and 1,252 deletions.
80 changes: 7 additions & 73 deletions mac/Keyman4Mac/Keyman4Mac/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ - (BOOL)createEventTap {
return YES;
}

// TODO: investigate -- event handler for OSK only?
CGEventRef eventTapFunction(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
// If key map is not enable, return the event without modifying
if (!isKeyMapEnabled)
Expand All @@ -114,15 +113,14 @@ CGEventRef eventTapFunction(CGEventTapProxy proxy, CGEventType type, CGEventRef
BOOL handled = NO;

if (type == NX_KEYDOWN) {
NSLog(@"AppDelegate eventTapFunction key down event: %@", event);
// Key down event
NSEvent *mEvent = [NSEvent eventWithCGEvent:event];
KMEngine *kme = [[KMEngine alloc] initWithKMX:kmx context:contextBuffer verboseLogging:debugMode];
NSArray *actions = [kme processEvent:mEvent];
//if (debugMode)
NSLog(@"%@", actions);
for (CoreAction *action in actions) {
if (action.isCharacter) {
NSString *output = action.content;
CoreKeyOutput *coreKeyOutput = [kme processEvent:mEvent];
if (coreKeyOutput) {
if (coreKeyOutput.hasTextToInsert) {
NSString *output = coreKeyOutput.textToInsert;
UniChar *outCStr = (UniChar *)[output cStringUsingEncoding:NSUTF16StringEncoding];
unsigned short kc = [mEvent keyCode];
CGEventRef kEventDown = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)kc, true);
Expand All @@ -131,84 +129,20 @@ CGEventRef eventTapFunction(CGEventTapProxy proxy, CGEventType type, CGEventRef
CGEventKeyboardSetUnicodeString(kEventUp, output.length, outCStr);
CGEventTapPostEvent(proxy, kEventDown);
CGEventTapPostEvent(proxy, kEventUp);
[contextBuffer appendString:output];
CFRelease(kEventDown);
CFRelease(kEventUp);
}
else if (action.isMarkerBackspace) {
NSInteger n = action.backspaceCount;
NSUInteger dk = [contextBuffer deleteLastDeadkeys];
n -= dk;

for (int i = 0; i < n; i++) {
CGEventRef kEventDown = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)51, true);
CGEventRef kEventUp = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)51, false);
CGEventTapPostEvent(proxy, kEventDown);
CGEventTapPostEvent(proxy, kEventUp);
CFRelease(kEventDown);
CFRelease(kEventUp);
}

[contextBuffer deleteLastNChars:n];
}
else if (action.isMarker) {
[contextBuffer appendDeadkey:action.content];
}
else if (action.actionType == EndAction) {
continue;
}
else if (action.actionType == EmitKeystrokeAction) {
if (coreKeyOutput.emitKeystroke) {
return NULL;
}
else if (action.actionType == AlertAction) {
if (coreKeyOutput.alert) {
[[NSSound soundNamed:@"Tink"] play];
}

handled = YES;
}

// Apply context changes if not handled
if (!handled) {
mEvent = [NSEvent eventWithCGEvent:event];
unsigned short keyCode = [mEvent keyCode];
if (keyCode <= 0x33) { // Main keys
if (keyCode == 0x24) // Enter
[contextBuffer appendString:@"\n"];
else if (keyCode == 0x33) { // Backspace
[contextBuffer deleteLastDeadkeys];
[contextBuffer deleteLastNChars:1];
[contextBuffer deleteLastDeadkeys];
}
else
[contextBuffer appendString:[mEvent characters]];
}
else {
unichar ch = [[mEvent characters] characterAtIndex:0];
if (ch >= 0x2A && ch <= 0x39) // Numpad char range
[contextBuffer appendString:[mEvent characters]];
else if (keyCode == 0x4C) // Enter (Numpad)
[contextBuffer appendString:@"\n"];
else if (keyCode >= 0x7B && keyCode <= 0x7E) // Arrow keys
contextBuffer = [NSMutableString stringWithString:@""]; // Clear context
else if (keyCode == 0x73 || keyCode == 0x77 || keyCode == 0x74 || keyCode == 0x79) {
// Home, End, Page Up, Page Down
contextBuffer = [NSMutableString stringWithString:@""]; // Clear context
}
else {
// Other keys
}
}
}
}
else {
// Mouse button up event (left | right)
contextBuffer = [NSMutableString stringWithString:@""]; // Clear context
if (debugMode)
NSLog(@"Mouse event");
}

if (debugMode)
NSLog(@"contextBuffer = %@\n***", [contextBuffer codeString]);
return handled?NULL:event;
}

Expand Down
12 changes: 0 additions & 12 deletions mac/Keyman4MacIM/Keyman4MacIM.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
297A501928DF4D360074EB1B /* PrivacyConsent.m in Sources */ = {isa = PBXBuildFile; fileRef = 297A501628DF4D360074EB1B /* PrivacyConsent.m */; };
298D09F72A1F4533006B9DFE /* TextApiCompliance.m in Sources */ = {isa = PBXBuildFile; fileRef = 298D09F52A1F4533006B9DFE /* TextApiCompliance.m */; };
2992F41C2A28479400E08929 /* KeySender.m in Sources */ = {isa = PBXBuildFile; fileRef = 299ABD6F29ECE75B00AA5948 /* KeySender.m */; };
2992F41D2A2847A900E08929 /* KMCoreActionHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = 2996D63929F2719F00DF4D56 /* KMCoreActionHandler.m */; };
2992F41E2A2847AF00E08929 /* PrivacyConsent.m in Sources */ = {isa = PBXBuildFile; fileRef = 297A501628DF4D360074EB1B /* PrivacyConsent.m */; };
2992F41F2A2847C900E08929 /* TextApiCompliance.m in Sources */ = {isa = PBXBuildFile; fileRef = 298D09F52A1F4533006B9DFE /* TextApiCompliance.m */; };
2992F4202A28482800E08929 /* PrivacyWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = 297A501228DF4D360074EB1B /* PrivacyWindowController.m */; };
2996D63A29F2719F00DF4D56 /* KMCoreActionHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = 2996D63929F2719F00DF4D56 /* KMCoreActionHandler.m */; };
2999C69929D3E4CE006366F5 /* InputMethodTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2999C69829D3E4CE006366F5 /* InputMethodTests.m */; };
299ABD7129ECE75B00AA5948 /* KeySender.m in Sources */ = {isa = PBXBuildFile; fileRef = 299ABD6F29ECE75B00AA5948 /* KeySender.m */; };
299B5E2029126CA20070841D /* keyman-icon@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 299B5E1E29126CA20070841D /* keyman-icon@2x.png */; };
299B5E2129126CA20070841D /* keyman-icon.png in Resources */ = {isa = PBXBuildFile; fileRef = 299B5E1F29126CA20070841D /* keyman-icon.png */; };
29A2D94E2A44FC4500FABFDB /* CoreActionHandlerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 29A2D94D2A44FC4500FABFDB /* CoreActionHandlerTests.m */; };
29B42A572728339900EDD5D3 /* KMInfoWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 29B42A592728339900EDD5D3 /* KMInfoWindowController.xib */; };
29B42A602728343B00EDD5D3 /* KMKeyboardHelpWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 29B42A622728343B00EDD5D3 /* KMKeyboardHelpWindowController.xib */; };
37A245C12565DFA6000BBF92 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 37A245C02565DFA6000BBF92 /* Assets.xcassets */; };
Expand Down Expand Up @@ -195,14 +192,11 @@
298566E52980E584004ACA95 /* ru */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ru; path = ru.lproj/Localizable.strings; sourceTree = "<group>"; };
298D09F52A1F4533006B9DFE /* TextApiCompliance.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TextApiCompliance.m; sourceTree = "<group>"; };
298D09F62A1F4533006B9DFE /* TextApiCompliance.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextApiCompliance.h; sourceTree = "<group>"; };
2996D63829F2719F00DF4D56 /* KMCoreActionHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KMCoreActionHandler.h; sourceTree = "<group>"; };
2996D63929F2719F00DF4D56 /* KMCoreActionHandler.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KMCoreActionHandler.m; sourceTree = "<group>"; };
2999C69829D3E4CE006366F5 /* InputMethodTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = InputMethodTests.m; sourceTree = "<group>"; };
299ABD6F29ECE75B00AA5948 /* KeySender.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KeySender.m; sourceTree = "<group>"; };
299ABD7029ECE75B00AA5948 /* KeySender.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KeySender.h; sourceTree = "<group>"; };
299B5E1E29126CA20070841D /* keyman-icon@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "keyman-icon@2x.png"; sourceTree = "<group>"; };
299B5E1F29126CA20070841D /* keyman-icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "keyman-icon.png"; sourceTree = "<group>"; };
29A2D94D2A44FC4500FABFDB /* CoreActionHandlerTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CoreActionHandlerTests.m; sourceTree = "<group>"; };
29A4730527E9D6D500DE07C7 /* ha */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ha; path = ha.lproj/KMAboutWindowController.strings; sourceTree = "<group>"; };
29A4730627E9D6D500DE07C7 /* ha */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ha; path = ha.lproj/preferences.strings; sourceTree = "<group>"; };
29A4730727E9D6D600DE07C7 /* ha */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ha; path = ha.lproj/KMInfoWindowController.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -572,8 +566,6 @@
98A778C31A8C53BF00CF809D /* KMInputMethodAppDelegate.m */,
E21799031FC5B74D00F2D66A /* KMInputMethodEventHandler.h */,
E21799041FC5B7BC00F2D66A /* KMInputMethodEventHandler.m */,
2996D63829F2719F00DF4D56 /* KMCoreActionHandler.h */,
2996D63929F2719F00DF4D56 /* KMCoreActionHandler.m */,
298D09F62A1F4533006B9DFE /* TextApiCompliance.h */,
298D09F52A1F4533006B9DFE /* TextApiCompliance.m */,
E24C79EC1FFFEA6B00D8E46F /* KMInputMethodEventHandlerProtected.h */,
Expand Down Expand Up @@ -708,7 +700,6 @@
E211769E20E18C0B00F8065D /* AppleCompliantTestClient.h */,
E211769F20E18C5200F8065D /* AppleCompliantTestClient.m */,
2999C69829D3E4CE006366F5 /* InputMethodTests.m */,
29A2D94D2A44FC4500FABFDB /* CoreActionHandlerTests.m */,
);
path = KeymanTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -1011,7 +1002,6 @@
297A501928DF4D360074EB1B /* PrivacyConsent.m in Sources */,
98FE105E1B4DE8F400525F54 /* NSWindow+SuppMethods.m in Sources */,
9836B3741AE5F11D00780482 /* ZipArchive.mm in Sources */,
2996D63A29F2719F00DF4D56 /* KMCoreActionHandler.m in Sources */,
983247B21A9EB9690010B90C /* KMConfigColumn3CellView.m in Sources */,
290BC75E274F3FD7005CD1C3 /* KMKeyboardInfo.m in Sources */,
297A501728DF4D360074EB1B /* PrivacyWindowController.m in Sources */,
Expand All @@ -1028,9 +1018,7 @@
2992F4202A28482800E08929 /* PrivacyWindowController.m in Sources */,
2992F41F2A2847C900E08929 /* TextApiCompliance.m in Sources */,
2992F41E2A2847AF00E08929 /* PrivacyConsent.m in Sources */,
2992F41D2A2847A900E08929 /* KMCoreActionHandler.m in Sources */,
2992F41C2A28479400E08929 /* KeySender.m in Sources */,
29A2D94E2A44FC4500FABFDB /* CoreActionHandlerTests.m in Sources */,
2999C69929D3E4CE006366F5 /* InputMethodTests.m in Sources */,
E211769D20E182DD00F8065D /* NoContextTestClient.m in Sources */,
E2D5529D20DD8E0200D77CFB /* LegacyTestClient.m in Sources */,
Expand Down
45 changes: 0 additions & 45 deletions mac/Keyman4MacIM/Keyman4MacIM/KMCoreActionHandler.h

This file was deleted.

Loading

0 comments on commit 55f69ae

Please sign in to comment.