1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-19 19:51:35 +03:00
vimr/NvimView/NvimServer/NvimServer.m

206 lines
5.0 KiB
Mathematica
Raw Normal View History

/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
2017-12-20 23:35:36 +03:00
#import "NvimServer.h"
2018-03-20 09:53:46 +03:00
#import "server_ui.h"
2016-07-10 15:43:26 +03:00
#import "Logging.h"
#import "CocoaCategories.h"
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.
#define FileInfo CarbonFileInfo
#define Boolean CarbonBoolean
#import <nvim/main.h>
2017-12-20 23:35:36 +03:00
// When #define'd you can execute the NvimServer binary and neovim will be started:
// $ ./NvimServer local remote
#undef DEBUG_NEOVIM_SERVER_STANDALONE
//#define DEBUG_NEOVIM_SERVER_STANDALONE
static const double qTimeout = 5;
2016-07-09 23:52:59 +03:00
2017-12-20 23:35:36 +03:00
@interface NvimServer ()
2016-07-09 23:52:59 +03:00
- (NSArray<NSString *> *)nvimArgs;
2016-07-09 23:52:59 +03:00
@end
static CFDataRef data_async(CFDataRef data, argv_callback cb) {
loop_schedule(&main_loop, event_create(cb, 3, data));
return NULL;
}
static CFDataRef local_server_callback(CFMessagePortRef local, SInt32 msgid, CFDataRef data, void *info) {
@autoreleasepool {
2017-12-20 23:35:36 +03:00
NvimServer *neoVimServer = (__bridge NvimServer *) info;
CFRetain(data); // release in the loop callbacks!
switch (msgid) {
2018-01-23 22:40:20 +03:00
case NvimBridgeMsgIdAgentReady: {
NSInteger *values = (NSInteger *) CFDataGetBytePtr(data);
start_neovim(values[0], values[1], neoVimServer.nvimArgs);
2017-01-06 12:16:55 +03:00
return NULL;
}
2017-01-06 12:16:55 +03:00
case NvimBridgeMsgIdScroll:
return data_async(data, neovim_scroll);
2017-05-30 23:13:52 +03:00
case NvimBridgeMsgIdResize:
return data_async(data, neovim_resize);
case NvimBridgeMsgIdInput:
return data_async(data, neovim_vim_input);
2017-01-06 12:06:47 +03:00
case NvimBridgeMsgIdInputMarked:
return data_async(data, neovim_vim_input_marked_text);
2017-01-06 12:06:47 +03:00
case NvimBridgeMsgIdDelete:
return data_async(data, neovim_delete);
case NvimBridgeMsgIdFocusGained:
return data_async(data, neovim_focus_gained);
2017-01-06 12:16:55 +03:00
default: return NULL;
}
}
}
2017-12-20 23:35:36 +03:00
@implementation NvimServer {
NSString *_localServerName;
NSString *_remoteServerName;
NSArray<NSString *> *_nvimArgs;
CFMessagePortRef _remoteServerPort;
NSThread *_localServerThread;
CFMessagePortRef _localServerPort;
CFRunLoopRef _localServerRunLoop;
NSCondition *_outputCondition;
}
- (NSArray<NSString *> *)nvimArgs {
return _nvimArgs;
}
- (NSCondition *)outputCondition {
return _outputCondition;
}
- (instancetype)initWithLocalServerName:(NSString *)localServerName
remoteServerName:(NSString *)remoteServerName
nvimArgs:(NSArray<NSString*> *)nvimArgs {
self = [super init];
if (self == nil) {
return nil;
}
_outputCondition = [[NSCondition alloc] init];
2016-07-10 15:24:45 +03:00
_localServerName = localServerName;
_remoteServerName = remoteServerName;
_nvimArgs = nvimArgs;
_localServerThread = [[NSThread alloc] initWithTarget:self selector:@selector(runLocalServer) object:nil];
_localServerThread.name = localServerName;
[_localServerThread start];
#ifndef DEBUG_NEOVIM_SERVER_STANDALONE
2016-07-10 15:24:45 +03:00
_remoteServerPort = CFMessagePortCreateRemote(kCFAllocatorDefault, (__bridge CFStringRef) _remoteServerName);
#endif
return self;
}
- (void)dealloc {
if (CFMessagePortIsValid(_remoteServerPort)) {
CFMessagePortInvalidate(_remoteServerPort);
}
CFRelease(_remoteServerPort);
if (CFMessagePortIsValid(_localServerPort)) {
CFMessagePortInvalidate(_localServerPort);
}
CFRelease(_localServerPort);
CFRunLoopStop(_localServerRunLoop);
[_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,
.retain = NULL,
.release = NULL,
.copyDescription = NULL
};
_localServerPort = CFMessagePortCreateLocal(
kCFAllocatorDefault,
2016-07-10 15:24:45 +03:00
(__bridge CFStringRef) _localServerName,
local_server_callback,
&localContext,
&shouldFree
);
// FIXME: handle shouldFree == true
}
2016-07-10 15:10:09 +03:00
_localServerRunLoop = CFRunLoopGetCurrent();
2016-07-10 15:10:09 +03:00
CFRunLoopSourceRef runLoopSrc = CFMessagePortCreateRunLoopSource(kCFAllocatorDefault, _localServerPort, 0);
CFRunLoopAddSource(_localServerRunLoop, runLoopSrc, kCFRunLoopCommonModes);
2016-07-10 15:10:09 +03:00
CFRelease(runLoopSrc);
#ifdef DEBUG_NEOVIM_SERVER_STANDALONE
server_start_neovim();
#endif
2016-07-10 15:10:09 +03:00
CFRunLoopRun();
}
2018-01-23 22:40:20 +03:00
- (void)sendMessageWithId:(NvimServerMsgId)msgid {
[self sendMessageWithId:msgid data:nil];
}
2018-01-23 22:40:20 +03:00
- (void)sendMessageWithId:(NvimServerMsgId)msgid data:(NSData *)data {
#ifdef DEBUG_NEOVIM_SERVER_STANDALONE
return;
#endif
2016-07-09 23:52:59 +03:00
if (_remoteServerPort == NULL) {
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;
}
SInt32 responseCode = CFMessagePortSendRequest(
2016-07-10 15:24:45 +03:00
_remoteServerPort, msgid, (__bridge CFDataRef) data, qTimeout, qTimeout, NULL, NULL
);
if (responseCode == kCFMessagePortSuccess) {
return;
}
WLOG("The msg (%lu:%s) could not be sent: %d", (unsigned long) msgid, data.cdesc, responseCode);
}
- (void)notifyReadiness {
#ifndef DEBUG_NEOVIM_SERVER_STANDALONE
2018-01-23 22:40:20 +03:00
[self sendMessageWithId:NvimServerMsgIdServerReady data:nil];
#endif
}
@end