espanso/native/libmacbridge/bridge.mm

119 lines
3.4 KiB
Plaintext
Raw Normal View History

2019-09-05 15:20:52 +00:00
#include "bridge.h"
#import <Foundation/Foundation.h>
2019-09-05 16:34:03 +00:00
#include "AppDelegate.h"
2019-09-05 17:18:55 +00:00
#include <string.h>
2019-09-05 15:20:52 +00:00
extern "C" {
2019-09-05 16:34:03 +00:00
}
#include <vector>
void register_keypress_callback(void * self, KeypressCallback callback) {
keypress_callback = callback;
interceptor_instance = self;
2019-09-05 15:20:52 +00:00
}
int32_t initialize() {
AppDelegate *delegate = [[AppDelegate alloc] init];
NSApplication * application = [NSApplication sharedApplication];
[application setDelegate:delegate];
}
int32_t eventloop() {
[NSApp run];
2019-09-05 16:34:03 +00:00
}
void send_string(const char * string) {
2019-09-05 17:18:55 +00:00
char * stringCopy = strdup(string);
dispatch_async(dispatch_get_main_queue(), ^(void) {
// Convert the c string to a UniChar array as required by the CGEventKeyboardSetUnicodeString method
2019-09-06 09:04:15 +00:00
NSString *nsString = [NSString stringWithUTF8String:stringCopy];CFStringRef cfString = (__bridge CFStringRef) nsString;
2019-09-05 17:18:55 +00:00
std::vector <UniChar> buffer(nsString.length);
CFStringGetCharacters(cfString, CFRangeMake(0, nsString.length), buffer.data());
free(stringCopy);
// Send the event
2019-09-06 09:04:15 +00:00
// Because of a bug ( or undocumented limit ) of the CGEventKeyboardSetUnicodeString method
// the string gets truncated after 20 characters, so we need to send multiple events.
int i = 0;
while (i < buffer.size()) {
int chunk_size = 20;
if ((i+chunk_size) > buffer.size()) {
chunk_size = buffer.size() - i;
}
UniChar * offset_buffer = buffer.data() + i;
CGEventRef e = CGEventCreateKeyboardEvent(NULL, 0x31, true);
CGEventKeyboardSetUnicodeString(e, chunk_size, offset_buffer);
CGEventPost(kCGHIDEventTap, e);
CFRelease(e);
usleep(2000);
i += chunk_size;
}
2019-09-05 17:18:55 +00:00
});
2019-09-05 16:34:03 +00:00
}
void delete_string(int32_t count) {
2019-09-05 17:18:55 +00:00
dispatch_async(dispatch_get_main_queue(), ^(void) {
for (int i = 0; i < count; i++) {
CGEventRef keydown;
keydown = CGEventCreateKeyboardEvent(NULL, 0x33, true);
CGEventPost(kCGHIDEventTap, keydown);
CFRelease(keydown);
2019-09-05 16:34:03 +00:00
2019-09-05 17:18:55 +00:00
usleep(2000);
2019-09-05 16:34:03 +00:00
2019-09-05 17:18:55 +00:00
CGEventRef keyup;
keyup = CGEventCreateKeyboardEvent(NULL, 0x33, false);
CGEventPost(kCGHIDEventTap, keyup);
CFRelease(keyup);
2019-09-05 16:34:03 +00:00
2019-09-05 17:18:55 +00:00
usleep(2000);
}
});
2019-09-06 08:49:05 +00:00
}
void send_vkey(int32_t vk) {
dispatch_async(dispatch_get_main_queue(), ^(void) {
CGEventRef keydown;
keydown = CGEventCreateKeyboardEvent(NULL, vk, true);
CGEventPost(kCGHIDEventTap, keydown);
CFRelease(keydown);
usleep(2000);
CGEventRef keyup;
keyup = CGEventCreateKeyboardEvent(NULL, vk, false);
CGEventPost(kCGHIDEventTap, keyup);
CFRelease(keyup);
usleep(2000);
});
2019-09-09 13:46:57 +00:00
}
int32_t get_active_app_bundle(char * buffer, int32_t size) {
NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
NSString *bundlePath = [frontApp bundleURL].path;
const char * path = [bundlePath UTF8String];
snprintf(buffer, size, "%s", path);
return 1;
}
int32_t get_active_app_identifier(char * buffer, int32_t size) {
NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
NSString *bundlePath = frontApp.bundleIdentifier;
const char * path = [bundlePath UTF8String];
snprintf(buffer, size, "%s", path);
return 1;
2019-09-05 15:20:52 +00:00
}