Skip to content

Instantly share code, notes, and snippets.

@jmercouris
Created November 2, 2018 16:56
Show Gist options
  • Select an option

  • Save jmercouris/01fa3a736fa333d693732df31a07b760 to your computer and use it in GitHub Desktop.

Select an option

Save jmercouris/01fa3a736fa333d693732df31a07b760 to your computer and use it in GitHub Desktop.
///////////////////////////////////////////////////////////////////////////
// Application Delegate
///////////////////////////////////////////////////////////////////////////
// The Application delegate launches a server written in C
// This server is listening for XML-RPC Calls
///////////////////////////////////////////////////////////////////////////
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification
{
Server *server = [[Server alloc] init];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[server start];
});
}
///////////////////////////////////////////////////////////////////////////
// Server Start
///////////////////////////////////////////////////////////////////////////
// The Application delegate above calls the below code
///////////////////////////////////////////////////////////////////////////
- (void) start {
...
xmlrpc_server_abyss(&env, &serverparm, XMLRPC_APSIZE(log_file_name));
if (env.fault_occurred) {
printf("xmlrpc_server_abyss() failed. %s\n", env.fault_string);
exit(1);
}
}
///////////////////////////////////////////////////////////////////////////
// The C Server
// The Abyss Server (XML-RPC server) invokes some functions like the one below
// The function below works when called from an external XML RPC Program
// HOWEVER if NSApplication SendEvent is invoked it will freeze the application
///////////////////////////////////////////////////////////////////////////
static xmlrpc_value *
window_make(xmlrpc_env * const envP,
xmlrpc_value * const paramArrayP,
void * const serverInfo,
void * const channelInfo) {
__block NSString* operationResult = @"";
dispatch_sync(dispatch_get_main_queue(), ^{
NextApplicationDelegate *delegate = [NSApp delegate];
operationResult = [delegate windowMake];
});
return xmlrpc_build_value(envP, "s", [operationResult UTF8String]);
}
///////////////////////////////////////////////////////////////////////////
// NSApplication
///////////////////////////////////////////////////////////////////////////
- (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);
xmlrpc_env env;
xmlrpc_value * resultP;
xmlrpc_bool consumed;
const char * const serverUrl = "http://localhost:8081/RPC2";
const char * const methodName = "PUSH-KEY-CHORD";
// Initialize our error-handling environment.
xmlrpc_env_init(&env);
// Start up our XML-RPC client library.
xmlrpc_client_init2(&env, XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION, NULL, 0);
reportIfFaultOccurred(&env);
// Make the remote procedure call
resultP = xmlrpc_client_call(&env, serverUrl, methodName,
"(bbbi)",
(xmlrpc_bool) controlPressed,
(xmlrpc_bool) alternatePressed,
(xmlrpc_bool) commandPressed,
(xmlrpc_int) characterCodePressed);
reportIfFaultOccurred(&env);
xmlrpc_read_bool(&env, resultP, &consumed);
reportIfFaultOccurred(&env);
printf("The sequence was consumed: %d\n", consumed);
xmlrpc_client_cleanup();
// If the key sequence was not consumed, pass it along
if (!consumed) {
[super sendEvent:event];
}
} else {
[super sendEvent:event];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment