2016-07-09 15:04:09 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "NeoVimAgent.h"
|
2016-07-09 23:52:59 +03:00
|
|
|
#import "NeoVimMsgIds.h"
|
|
|
|
#import "NeoVimUiBridgeProtocol.h"
|
2016-07-10 15:43:26 +03:00
|
|
|
#import "Logging.h"
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
|
2016-07-10 15:24:45 +03:00
|
|
|
static const double qTimeout = 10;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
#define data_to_array(type) \
|
|
|
|
static type *data_to_ ## type ## _array(NSData *data, NSUInteger count) { \
|
|
|
|
NSUInteger length = count * sizeof( type ); \
|
|
|
|
if (data.length != length) { \
|
|
|
|
return NULL; \
|
|
|
|
} \
|
|
|
|
return ( type *) data.bytes; \
|
|
|
|
}
|
|
|
|
|
|
|
|
data_to_array(int)
|
2016-07-18 21:16:56 +03:00
|
|
|
data_to_array(bool)
|
2016-07-09 23:52:59 +03:00
|
|
|
data_to_array(CellAttributes)
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
@interface NeoVimAgent ()
|
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
- (void)handleMessageWithId:(SInt32)msgid data:(NSData *)data;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
static CFDataRef local_server_callback(CFMessagePortRef local, SInt32 msgid, CFDataRef data, void *info) {
|
|
|
|
@autoreleasepool {
|
|
|
|
NeoVimAgent *agent = (__bridge NeoVimAgent *) info;
|
2016-07-09 23:52:59 +03:00
|
|
|
[agent handleMessageWithId:msgid data:(__bridge NSData *) (data)];
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
2016-07-10 15:24:45 +03:00
|
|
|
|
|
|
|
return NULL;
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@implementation NeoVimAgent {
|
|
|
|
NSString *_uuid;
|
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
CFMessagePortRef _remoteServerPort;
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
CFMessagePortRef _localServerPort;
|
|
|
|
NSThread *_localServerThread;
|
2016-07-18 21:16:56 +03:00
|
|
|
CFRunLoopRef _localServerRunLoop;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
NSTask *_neoVimServerTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithUuid:(NSString *)uuid {
|
|
|
|
self = [super init];
|
|
|
|
if (self == nil) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
_uuid = uuid;
|
2016-07-09 23:52:59 +03:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-07-17 16:56:20 +03:00
|
|
|
// We cannot use -dealloc for this since -dealloc is not called until the run loop in the thread stops.
|
2016-07-09 23:52:59 +03:00
|
|
|
- (void)cleanUp {
|
2016-07-18 21:16:56 +03:00
|
|
|
if (CFMessagePortIsValid(_remoteServerPort)) {
|
|
|
|
CFMessagePortInvalidate(_remoteServerPort);
|
|
|
|
}
|
2016-07-09 23:52:59 +03:00
|
|
|
CFRelease(_remoteServerPort);
|
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
if (CFMessagePortIsValid(_localServerPort)) {
|
|
|
|
CFMessagePortInvalidate(_localServerPort);
|
|
|
|
}
|
2016-07-09 23:52:59 +03:00
|
|
|
CFRelease(_localServerPort);
|
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
CFRunLoopStop(_localServerRunLoop);
|
2016-07-09 23:52:59 +03:00
|
|
|
[_localServerThread cancel];
|
2016-07-17 16:56:20 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
[_neoVimServerTask interrupt];
|
|
|
|
[_neoVimServerTask terminate];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)establishLocalServer {
|
2016-07-09 15:04:09 +03:00
|
|
|
_localServerThread = [[NSThread alloc] initWithTarget:self selector:@selector(runLocalServer) object:nil];
|
|
|
|
[_localServerThread start];
|
|
|
|
|
|
|
|
_neoVimServerTask = [[NSTask alloc] init];
|
2016-07-10 15:05:10 +03:00
|
|
|
_neoVimServerTask.currentDirectoryPath = NSHomeDirectory();
|
2016-07-09 15:04:09 +03:00
|
|
|
_neoVimServerTask.launchPath = [self neoVimServerExecutablePath];
|
2016-07-10 15:43:26 +03:00
|
|
|
_neoVimServerTask.arguments = @[ [self localServerName], [self remoteServerName] ];
|
2016-07-09 15:04:09 +03:00
|
|
|
[_neoVimServerTask launch];
|
2016-07-09 23:52:59 +03:00
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
- (void)vimInput:(NSString *)string {
|
|
|
|
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
[self sendMessageWithId:NeoVimAgentMsgIdInput data:data];
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
- (void)vimInputMarkedText:(NSString *_Nonnull)markedText {
|
|
|
|
NSData *data = [markedText dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
[self sendMessageWithId:NeoVimAgentMsgIdInputMarked data:data];
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
- (void)deleteCharacters:(NSInteger)count {
|
|
|
|
NSData *data = [[NSData alloc] initWithBytes:&count length:sizeof(NSInteger)];
|
|
|
|
[self sendMessageWithId:NeoVimAgentMsgIdDelete data:data];
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
- (void)forceRedraw {
|
|
|
|
[self sendMessageWithId:NeoVimAgentMsgIdRedraw data:nil];
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
- (void)resizeToWidth:(int)width height:(int)height {
|
|
|
|
int values[] = { width, height };
|
|
|
|
NSData *data = [[NSData alloc] initWithBytes:values length:(2 * sizeof(int))];
|
|
|
|
[self sendMessageWithId:NeoVimAgentMsgIdResize data:data];
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
- (bool)hasDirtyDocs {
|
|
|
|
NSData *responseData = [self sendMessageWithId:NeoVimAgentMsgIdDirtyDocs data:nil];
|
|
|
|
if (responseData == NULL) {
|
|
|
|
NSLog(@"WARNING: agent got response data null");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool *values = data_to_bool_array(responseData, 1);
|
|
|
|
return values[0];
|
|
|
|
}
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
- (void)runLocalServer {
|
|
|
|
@autoreleasepool {
|
|
|
|
CFMessagePortContext localContext = {
|
|
|
|
.version = 0,
|
|
|
|
.info = (__bridge void *) self,
|
|
|
|
.retain = NULL,
|
|
|
|
.release = NULL,
|
|
|
|
.copyDescription = NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned char shouldFreeLocalServer = false;
|
|
|
|
_localServerPort = CFMessagePortCreateLocal(
|
|
|
|
kCFAllocatorDefault,
|
|
|
|
(__bridge CFStringRef) [self localServerName],
|
|
|
|
local_server_callback,
|
|
|
|
&localContext,
|
|
|
|
&shouldFreeLocalServer
|
|
|
|
);
|
|
|
|
|
|
|
|
// FIXME: handle shouldFreeLocalServer = true
|
|
|
|
}
|
2016-07-10 15:10:09 +03:00
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
_localServerRunLoop = CFRunLoopGetCurrent();
|
2016-07-10 15:10:09 +03:00
|
|
|
CFRunLoopSourceRef runLoopSrc = CFMessagePortCreateRunLoopSource(kCFAllocatorDefault, _localServerPort, 0);
|
2016-07-18 21:16:56 +03:00
|
|
|
CFRunLoopAddSource(_localServerRunLoop, runLoopSrc, kCFRunLoopCommonModes);
|
2016-07-10 15:10:09 +03:00
|
|
|
CFRelease(runLoopSrc);
|
|
|
|
CFRunLoopRun();
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
- (void)establishNeoVimConnection {
|
|
|
|
_remoteServerPort = CFMessagePortCreateRemote(
|
|
|
|
kCFAllocatorDefault,
|
|
|
|
(__bridge CFStringRef) [self remoteServerName]
|
|
|
|
);
|
|
|
|
|
|
|
|
[self sendMessageWithId:NeoVimAgentMsgIdAgentReady data:nil];
|
|
|
|
}
|
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
- (NSData *)sendMessageWithId:(NeoVimAgentMsgId)msgid data:(NSData *)data {
|
2016-07-09 23:52:59 +03:00
|
|
|
if (_remoteServerPort == NULL) {
|
2016-07-10 15:43:26 +03:00
|
|
|
log4Warn("Remote server is null: The msg (%lu:%@) could not be sent.", (unsigned long) msgid, data);
|
2016-07-18 21:16:56 +03:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFDataRef responseData = NULL;
|
|
|
|
CFStringRef replyMode = NULL;
|
|
|
|
|
|
|
|
// We only expect reply from NeoVimAgentMsgIdDirtyDocs
|
|
|
|
if (msgid == NeoVimAgentMsgIdDirtyDocs) {
|
|
|
|
replyMode = kCFRunLoopDefaultMode;
|
2016-07-09 23:52:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SInt32 responseCode = CFMessagePortSendRequest(
|
2016-07-18 21:16:56 +03:00
|
|
|
_remoteServerPort, msgid, (__bridge CFDataRef) data, qTimeout, qTimeout, replyMode, &responseData
|
2016-07-09 23:52:59 +03:00
|
|
|
);
|
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
if (responseCode != kCFMessagePortSuccess) {
|
|
|
|
log4Warn("The msg (%lu:%@) could not be sent: %d", (unsigned long) msgid, data, responseCode);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (responseData == NULL) {
|
|
|
|
return nil;
|
2016-07-09 23:52:59 +03:00
|
|
|
}
|
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
return [NSData dataWithData:(__bridge NSData *) responseData];
|
2016-07-09 23:52:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)neoVimServerExecutablePath {
|
|
|
|
return [[[NSBundle bundleForClass:[self class]] builtInPlugInsPath] stringByAppendingPathComponent:@"NeoVimServer"];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)localServerName {
|
2016-07-12 09:13:40 +03:00
|
|
|
return [NSString stringWithFormat:@"com.qvacua.vimr.%@", _uuid];
|
2016-07-09 23:52:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)remoteServerName {
|
2016-07-12 09:13:40 +03:00
|
|
|
return [NSString stringWithFormat:@"com.qvacua.vimr.neovim-server.%@", _uuid];
|
2016-07-09 23:52:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)handleMessageWithId:(SInt32)msgid data:(NSData *)data {
|
2016-07-09 15:04:09 +03:00
|
|
|
switch (msgid) {
|
|
|
|
|
|
|
|
case NeoVimServerMsgIdServerReady:
|
2016-07-09 23:52:59 +03:00
|
|
|
[self establishNeoVimConnection];
|
|
|
|
return;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
case NeoVimServerMsgIdNeoVimReady:
|
2016-07-09 23:52:59 +03:00
|
|
|
[_bridge neoVimUiIsReady];
|
|
|
|
return;
|
|
|
|
|
|
|
|
case NeoVimServerMsgIdResize: {
|
|
|
|
int *values = data_to_int_array(data, 2);
|
|
|
|
if (values == nil) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[_bridge resizeToWidth:values[0] height:values[1]];
|
|
|
|
return;
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
case NeoVimServerMsgIdClear:
|
2016-07-09 23:52:59 +03:00
|
|
|
[_bridge clear];
|
|
|
|
return;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
case NeoVimServerMsgIdEolClear:
|
2016-07-09 23:52:59 +03:00
|
|
|
[_bridge eolClear];
|
|
|
|
return;
|
|
|
|
|
|
|
|
case NeoVimServerMsgIdSetPosition: {
|
|
|
|
int *values = data_to_int_array(data, 4);
|
|
|
|
[_bridge gotoPosition:(Position) { .row = values[0], .column = values[1] }
|
|
|
|
screenCursor:(Position) { .row = values[2], .column = values[3] }];
|
|
|
|
return;
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
case NeoVimServerMsgIdSetMenu:
|
2016-07-09 23:52:59 +03:00
|
|
|
[_bridge updateMenu];
|
|
|
|
return;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
case NeoVimServerMsgIdBusyStart:
|
2016-07-09 23:52:59 +03:00
|
|
|
[_bridge busyStart];
|
|
|
|
return;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
case NeoVimServerMsgIdBusyStop:
|
2016-07-09 23:52:59 +03:00
|
|
|
[_bridge busyStop];
|
|
|
|
return;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
case NeoVimServerMsgIdMouseOn:
|
2016-07-09 23:52:59 +03:00
|
|
|
[_bridge mouseOn];
|
|
|
|
return;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
case NeoVimServerMsgIdMouseOff:
|
2016-07-09 23:52:59 +03:00
|
|
|
[_bridge mouseOff];
|
|
|
|
return;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
case NeoVimServerMsgIdModeChange: {
|
|
|
|
int *values = data_to_int_array(data, 1);
|
|
|
|
[_bridge modeChange:values[0]];
|
|
|
|
return;
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
case NeoVimServerMsgIdSetScrollRegion: {
|
|
|
|
int *values = data_to_int_array(data, 4);
|
|
|
|
[_bridge setScrollRegionToTop:values[0] bottom:values[1] left:values[2] right:values[3]];
|
|
|
|
return;
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
case NeoVimServerMsgIdScroll: {
|
|
|
|
int *values = data_to_int_array(data, 1);
|
|
|
|
[_bridge scroll:values[0]];
|
|
|
|
return;
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
case NeoVimServerMsgIdSetHighlightAttributes: {
|
|
|
|
CellAttributes *values = data_to_CellAttributes_array(data, 1);
|
|
|
|
[_bridge highlightSet:values[0]];
|
|
|
|
return;
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
case NeoVimServerMsgIdPut: {
|
|
|
|
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
|
|
|
[_bridge put:string];
|
|
|
|
return;
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
case NeoVimServerMsgIdPutMarked: {
|
|
|
|
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
|
|
|
[_bridge putMarkedText:string];
|
|
|
|
return;
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
case NeoVimServerMsgIdUnmark: {
|
|
|
|
int *values = data_to_int_array(data, 2);
|
|
|
|
[_bridge unmarkRow:values[0] column:values[1]];
|
|
|
|
return;
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
case NeoVimServerMsgIdBell:
|
2016-07-09 23:52:59 +03:00
|
|
|
[_bridge bell];
|
|
|
|
return;
|
|
|
|
|
|
|
|
case NeoVimServerMsgIdVisualBell:
|
|
|
|
[_bridge visualBell];
|
|
|
|
return;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
case NeoVimServerMsgIdFlush:
|
2016-07-09 23:52:59 +03:00
|
|
|
[_bridge flush];
|
|
|
|
return;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
case NeoVimServerMsgIdSetForeground: {
|
|
|
|
int *values = data_to_int_array(data, 1);
|
|
|
|
[_bridge updateForeground:values[0]];
|
|
|
|
return;
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
case NeoVimServerMsgIdSetBackground: {
|
|
|
|
int *values = data_to_int_array(data, 1);
|
|
|
|
[_bridge updateBackground:values[0]];
|
|
|
|
return;
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
case NeoVimServerMsgIdSetSpecial: {
|
|
|
|
int *values = data_to_int_array(data, 1);
|
|
|
|
[_bridge updateSpecial:values[0]];
|
|
|
|
return;
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
case NeoVimServerMsgIdSetTitle: {
|
|
|
|
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
|
|
|
[_bridge setTitle:string];
|
|
|
|
return;
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
case NeoVimServerMsgIdSetIcon: {
|
|
|
|
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
|
|
|
[_bridge setIcon:string];
|
|
|
|
return;
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
case NeoVimServerMsgIdStop:
|
2016-07-09 23:52:59 +03:00
|
|
|
[_bridge stop];
|
|
|
|
return;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
default:
|
2016-07-09 23:52:59 +03:00
|
|
|
return;
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|