Last active
November 16, 2018 18:55
-
-
Save jmercouris/e67c24e2267f3ad8037aa53a64ee7663 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Copyright © 2017-2018 Atlas Engineer LLC. | |
// Use of this file is governed by the license that can be found in LICENSE. | |
// | |
#import "NextApplication.h" | |
#include <xmlrpc-c/base.h> | |
#include <xmlrpc-c/client.h> | |
#include <xmlrpc-c/config.h> | |
#include "Global.h" | |
@implementation NextApplication | |
- (void)sendEvent:(NSEvent *)event | |
{ | |
if ([event type] == NSEventTypeKeyDown) { | |
NSEventModifierFlags modifierFlags = [event modifierFlags]; | |
char characterCodePressed = [[event charactersIgnoringModifiers] characterAtIndex: 0]; | |
bool controlPressed = (modifierFlags & NSEventModifierFlagControl); | |
bool alternatePressed = (modifierFlags & NSEventModifierFlagOption); | |
bool commandPressed = (modifierFlags & NSEventModifierFlagCommand); | |
dispatch_async([[Global sharedInstance] getKeyQueue], ^{ | |
xmlrpc_env env = [[Global sharedInstance] getXMLRPCEnv]; | |
const char * const serverUrl = "http://localhost:8081/RPC2"; | |
const char * const methodName = "PUSH-KEY-CHORD"; | |
// Make the remote procedure call | |
xmlrpc_client_call(&env, serverUrl, methodName, | |
"(bbbi)", | |
(xmlrpc_bool) controlPressed, | |
(xmlrpc_bool) alternatePressed, | |
(xmlrpc_bool) commandPressed, | |
(xmlrpc_int) characterCodePressed); | |
dispatch_sync(dispatch_get_main_queue(), ^{ | |
[super sendEvent: event]; | |
}); | |
}); | |
} | |
else if ([event type] == NSEventTypeKeyUp) { | |
dispatch_async([[Global sharedInstance] getKeyQueue], ^{ | |
dispatch_sync(dispatch_get_main_queue(), ^{ | |
[super sendEvent: event]; | |
}); | |
}); | |
} else { | |
[super sendEvent:event]; | |
} | |
} | |
@end | |
// | |
// Copyright © 2017-2018 Atlas Engineer LLC. | |
// Use of this file is governed by the license that can be found in LICENSE. | |
// | |
#import "Global.h" | |
#include <xmlrpc-c/base.h> | |
#include <xmlrpc-c/client.h> | |
#include <xmlrpc-c/config.h> | |
#define NAME "Next" | |
#define VERSION "0.1" | |
@implementation Global | |
+ (Global *)sharedInstance { | |
static dispatch_once_t onceToken; | |
static Global *instance = nil; | |
dispatch_once(&onceToken, ^{ | |
instance = [[Global alloc] init]; | |
}); | |
return instance; | |
} | |
static void | |
reportIfFaultOccurred (xmlrpc_env * const envP) { | |
if (envP->fault_occurred) { | |
fprintf(stderr, "ERROR: %s (%d)\n", | |
envP->fault_string, envP->fault_code); | |
} | |
} | |
- (id)init { | |
self = [super init]; | |
if (self) { | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ | |
// Initialize our error-handling environment. | |
xmlrpc_env_init(&self->env); | |
// Start up our XML-RPC client library. | |
xmlrpc_client_init2(&self->env, XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION, NULL, 0); | |
reportIfFaultOccurred(&self->env); | |
}); | |
keyQueue = dispatch_queue_create("key_event_queue", DISPATCH_QUEUE_SERIAL); | |
} | |
return self; | |
} | |
- (dispatch_queue_t) getKeyQueue { | |
return self->keyQueue; | |
} | |
- (xmlrpc_env)getXMLRPCEnv { | |
return self->env; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment