2016-07-09 15:04:09 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "NeoVimServer.h"
|
|
|
|
#import "server_globals.h"
|
2016-07-10 15:43:26 +03:00
|
|
|
#import "Logging.h"
|
2016-08-26 16:31:30 +03:00
|
|
|
#import "CocoaCategories.h"
|
2017-01-06 12:31:45 +03:00
|
|
|
#import "DataWrapper.h"
|
2017-01-05 21:35:44 +03:00
|
|
|
|
2017-01-06 12:16:55 +03:00
|
|
|
// FileInfo and Boolean are #defined by Carbon and NeoVim: Since we don't need the Carbon versions of them, we rename
|
|
|
|
// them.
|
2017-01-05 21:35:44 +03:00
|
|
|
#define FileInfo CarbonFileInfo
|
|
|
|
#define Boolean CarbonBoolean
|
|
|
|
|
|
|
|
#import <nvim/vim.h>
|
|
|
|
#import <nvim/main.h>
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
|
2016-08-15 11:56:19 +03:00
|
|
|
// When #define'd you can execute the NeoVimServer binary and neovim will be started:
|
|
|
|
// $ ./NeoVimServer local remote
|
|
|
|
#undef DEBUG_NEOVIM_SERVER_STANDALONE
|
|
|
|
//#define DEBUG_NEOVIM_SERVER_STANDALONE
|
|
|
|
|
|
|
|
|
2016-12-07 22:53:12 +03:00
|
|
|
static const double qTimeout = 10;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
|
|
|
|
@interface NeoVimServer ()
|
|
|
|
|
2017-06-12 01:30:33 +03:00
|
|
|
- (NSArray<NSString *> *)nvimArgs;
|
2017-01-05 21:35:44 +03:00
|
|
|
- (NSCondition *)outputCondition;
|
2016-07-09 23:52:59 +03:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2017-01-06 09:12:05 +03:00
|
|
|
static CFDataRef data_sync(CFDataRef data, NSCondition *condition, argv_callback cb) {
|
2017-01-06 12:31:45 +03:00
|
|
|
DataWrapper *wrapper = [[DataWrapper alloc] init];
|
2017-01-05 21:35:44 +03:00
|
|
|
NSDate *deadline = [[NSDate date] dateByAddingTimeInterval:qTimeout];
|
|
|
|
|
|
|
|
[condition lock];
|
|
|
|
|
2017-04-29 09:37:44 +03:00
|
|
|
loop_schedule(&main_loop, event_create(cb, 3, data, condition, wrapper));
|
2017-01-05 21:35:44 +03:00
|
|
|
|
2017-01-06 10:28:04 +03:00
|
|
|
while (wrapper.isDataReady == false && [condition waitUntilDate:deadline]);
|
2017-01-05 21:35:44 +03:00
|
|
|
[condition unlock];
|
|
|
|
|
2017-01-06 11:25:11 +03:00
|
|
|
if (wrapper.data == nil) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CFDataCreateCopy(kCFAllocatorDefault, (__bridge CFDataRef) wrapper.data);
|
2017-01-05 21:35:44 +03:00
|
|
|
}
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
static CFDataRef local_server_callback(CFMessagePortRef local, SInt32 msgid, CFDataRef data, void *info) {
|
|
|
|
@autoreleasepool {
|
2016-07-10 15:24:45 +03:00
|
|
|
NeoVimServer *neoVimServer = (__bridge NeoVimServer *) info;
|
2017-01-06 08:42:23 +03:00
|
|
|
NSCondition *outputCondition = neoVimServer.outputCondition;
|
2017-01-05 21:35:44 +03:00
|
|
|
CFRetain(data); // release in the loop callbacks!
|
|
|
|
|
|
|
|
switch (msgid) {
|
|
|
|
|
2017-06-12 20:28:28 +03:00
|
|
|
case NeoVimAgentMsgIdAgentReady: {
|
|
|
|
NSInteger *values = (NSInteger *) CFDataGetBytePtr(data);
|
|
|
|
start_neovim(values[0], values[1], neoVimServer.nvimArgs);
|
2017-01-06 12:16:55 +03:00
|
|
|
return NULL;
|
2017-06-12 20:28:28 +03:00
|
|
|
}
|
2017-01-06 12:16:55 +03:00
|
|
|
|
2017-05-30 23:13:52 +03:00
|
|
|
case NeoVimAgentMsgIdScroll: return data_sync(data, outputCondition, neovim_scroll);
|
|
|
|
|
2017-01-06 10:38:13 +03:00
|
|
|
case NeoVimAgentMsgIdGetEscapeFileNames: return data_sync(data, outputCondition, neovim_escaped_filenames);
|
|
|
|
|
2017-12-01 23:59:31 +03:00
|
|
|
case NeoVimAgentMsgIdResize: return data_sync(data, outputCondition, neovim_resize);
|
|
|
|
|
2017-05-08 21:25:11 +03:00
|
|
|
case NeoVimAgentMsgIdInput: return data_sync(data, outputCondition, neovim_vim_input);
|
2017-01-06 12:06:47 +03:00
|
|
|
|
2017-05-08 21:25:11 +03:00
|
|
|
case NeoVimAgentMsgIdInputMarked: return data_sync(data, outputCondition, neovim_vim_input_marked_text);
|
2017-01-06 12:06:47 +03:00
|
|
|
|
2017-05-08 21:25:11 +03:00
|
|
|
case NeoVimAgentMsgIdDelete: return data_sync(data, outputCondition, neovim_delete);
|
2017-08-24 01:37:48 +03:00
|
|
|
|
2017-09-07 19:24:06 +03:00
|
|
|
case NeoVimAgentMsgIdFocusGained: return data_sync(data, outputCondition, neovim_focus_gained);
|
|
|
|
|
2017-06-26 00:31:10 +03:00
|
|
|
#ifdef DEBUG
|
2017-06-25 18:35:51 +03:00
|
|
|
case NeoVimAgentDebug1: return data_sync(data, outputCondition, neovim_debug1);
|
2017-06-26 00:31:10 +03:00
|
|
|
#endif
|
2017-06-25 18:35:51 +03:00
|
|
|
|
2017-01-06 12:16:55 +03:00
|
|
|
default: return NULL;
|
2016-07-18 21:16:56 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@implementation NeoVimServer {
|
|
|
|
NSString *_localServerName;
|
|
|
|
NSString *_remoteServerName;
|
2017-06-12 01:30:33 +03:00
|
|
|
NSArray<NSString *> *_nvimArgs;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
CFMessagePortRef _remoteServerPort;
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
NSThread *_localServerThread;
|
|
|
|
CFMessagePortRef _localServerPort;
|
2016-07-18 21:16:56 +03:00
|
|
|
CFRunLoopRef _localServerRunLoop;
|
2017-01-05 21:35:44 +03:00
|
|
|
|
|
|
|
NSCondition *_outputCondition;
|
|
|
|
}
|
|
|
|
|
2017-06-12 01:30:33 +03:00
|
|
|
- (NSArray<NSString *> *)nvimArgs {
|
|
|
|
return _nvimArgs;
|
|
|
|
}
|
|
|
|
|
2017-01-05 21:35:44 +03:00
|
|
|
- (NSCondition *)outputCondition {
|
|
|
|
return _outputCondition;
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2017-06-12 01:30:33 +03:00
|
|
|
- (instancetype)initWithLocalServerName:(NSString *)localServerName
|
|
|
|
remoteServerName:(NSString *)remoteServerName
|
|
|
|
nvimArgs:(NSArray<NSString*> *)nvimArgs {
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
self = [super init];
|
|
|
|
if (self == nil) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2017-01-05 21:35:44 +03:00
|
|
|
_outputCondition = [[NSCondition alloc] init];
|
|
|
|
|
2016-07-10 15:24:45 +03:00
|
|
|
_localServerName = localServerName;
|
|
|
|
_remoteServerName = remoteServerName;
|
2017-06-12 01:30:33 +03:00
|
|
|
_nvimArgs = nvimArgs;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
_localServerThread = [[NSThread alloc] initWithTarget:self selector:@selector(runLocalServer) object:nil];
|
2016-08-26 18:27:42 +03:00
|
|
|
_localServerThread.name = localServerName;
|
2016-07-09 15:04:09 +03:00
|
|
|
[_localServerThread start];
|
|
|
|
|
2016-08-15 11:56:19 +03:00
|
|
|
#ifndef DEBUG_NEOVIM_SERVER_STANDALONE
|
2016-07-10 15:24:45 +03:00
|
|
|
_remoteServerPort = CFMessagePortCreateRemote(kCFAllocatorDefault, (__bridge CFStringRef) _remoteServerName);
|
2016-08-15 11:56:19 +03:00
|
|
|
#endif
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
2016-07-18 21:16:56 +03:00
|
|
|
if (CFMessagePortIsValid(_remoteServerPort)) {
|
|
|
|
CFMessagePortInvalidate(_remoteServerPort);
|
|
|
|
}
|
|
|
|
CFRelease(_remoteServerPort);
|
|
|
|
|
|
|
|
if (CFMessagePortIsValid(_localServerPort)) {
|
|
|
|
CFMessagePortInvalidate(_localServerPort);
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
CFRelease(_localServerPort);
|
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
CFRunLoopStop(_localServerRunLoop);
|
2016-07-09 15:04:09 +03:00
|
|
|
[_localServerThread cancel];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)runLocalServer {
|
|
|
|
@autoreleasepool {
|
|
|
|
unsigned char shouldFree = false;
|
|
|
|
CFMessagePortContext localContext = {
|
|
|
|
.version = 0,
|
2016-07-10 15:24:45 +03:00
|
|
|
.info = (__bridge void *) self,
|
2016-07-09 15:04:09 +03:00
|
|
|
.retain = NULL,
|
|
|
|
.release = NULL,
|
|
|
|
.copyDescription = NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
_localServerPort = CFMessagePortCreateLocal(
|
|
|
|
kCFAllocatorDefault,
|
2016-07-10 15:24:45 +03:00
|
|
|
(__bridge CFStringRef) _localServerName,
|
2016-07-09 15:04:09 +03:00
|
|
|
local_server_callback,
|
|
|
|
&localContext,
|
|
|
|
&shouldFree
|
|
|
|
);
|
|
|
|
|
|
|
|
// FIXME: handle shouldFree == 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);
|
2016-08-15 11:56:19 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG_NEOVIM_SERVER_STANDALONE
|
|
|
|
server_start_neovim();
|
|
|
|
#endif
|
|
|
|
|
2016-07-10 15:10:09 +03:00
|
|
|
CFRunLoopRun();
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)sendMessageWithId:(NeoVimServerMsgId)msgid {
|
|
|
|
[self sendMessageWithId:msgid data:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)sendMessageWithId:(NeoVimServerMsgId)msgid data:(NSData *)data {
|
2016-08-15 11:56:19 +03:00
|
|
|
#ifdef DEBUG_NEOVIM_SERVER_STANDALONE
|
|
|
|
return;
|
|
|
|
#endif
|
2016-11-20 14:05:14 +03:00
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
if (_remoteServerPort == NULL) {
|
2016-08-26 16:31:30 +03:00
|
|
|
WLOG("Remote server is null: The msg (%lu:%s) could not be sent.", (unsigned long) msgid, data.cdesc);
|
2016-07-09 23:52:59 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
SInt32 responseCode = CFMessagePortSendRequest(
|
2016-07-10 15:24:45 +03:00
|
|
|
_remoteServerPort, msgid, (__bridge CFDataRef) data, qTimeout, qTimeout, NULL, NULL
|
2016-07-09 15:04:09 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
if (responseCode == kCFMessagePortSuccess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-26 16:31:30 +03:00
|
|
|
WLOG("The msg (%lu:%s) could not be sent: %d", (unsigned long) msgid, data.cdesc, responseCode);
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)notifyReadiness {
|
2016-08-15 11:56:19 +03:00
|
|
|
#ifndef DEBUG_NEOVIM_SERVER_STANDALONE
|
2016-07-09 15:04:09 +03:00
|
|
|
[self sendMessageWithId:NeoVimServerMsgIdServerReady data:nil];
|
2016-08-15 11:56:19 +03:00
|
|
|
#endif
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|