2016-07-09 15:04:09 +03:00
|
|
|
/**
|
|
|
|
* 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"
|
2016-08-26 16:31:30 +03:00
|
|
|
#import "CocoaCategories.h"
|
2017-01-05 21:35:44 +03:00
|
|
|
|
2019-03-03 23:57:03 +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/main.h>
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
|
2019-03-03 23:57:03 +03:00
|
|
|
// When #define'd you can execute the NvimServer binary
|
|
|
|
// and neovim will be started:
|
2017-12-20 23:35:36 +03:00
|
|
|
// $ ./NvimServer local remote
|
2016-08-15 11:56:19 +03:00
|
|
|
#undef DEBUG_NEOVIM_SERVER_STANDALONE
|
|
|
|
//#define DEBUG_NEOVIM_SERVER_STANDALONE
|
|
|
|
|
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
static const double qTimeout = 5;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
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
|
|
|
|
2017-06-12 01:30:33 +03:00
|
|
|
- (NSArray<NSString *> *)nvimArgs;
|
2016-07-09 23:52:59 +03:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2018-05-10 20:33:50 +03:00
|
|
|
static CFDataRef data_async(CFDataRef data, argv_callback cb) {
|
|
|
|
loop_schedule(&main_loop, event_create(cb, 3, data));
|
|
|
|
return NULL;
|
2017-01-05 21:35:44 +03:00
|
|
|
}
|
|
|
|
|
2019-03-03 23:57:03 +03:00
|
|
|
static CFDataRef local_server_callback(
|
|
|
|
CFMessagePortRef local, SInt32 msgid, CFDataRef data, void *info
|
|
|
|
) {
|
|
|
|
// release in the loop callbacks!
|
|
|
|
// (or in the case clause when not passed to the callback)
|
|
|
|
CFRetain(data);
|
2017-01-05 21:35:44 +03:00
|
|
|
|
2018-05-11 11:41:08 +03:00
|
|
|
switch (msgid) {
|
2017-01-05 21:35:44 +03:00
|
|
|
|
2018-05-11 11:41:08 +03:00
|
|
|
case NvimBridgeMsgIdAgentReady: {
|
|
|
|
@autoreleasepool {
|
2017-06-12 20:28:28 +03:00
|
|
|
NSInteger *values = (NSInteger *) CFDataGetBytePtr(data);
|
2018-05-11 11:41:08 +03:00
|
|
|
NvimServer *nvimServer = (__bridge NvimServer *) info;
|
|
|
|
|
|
|
|
start_neovim(values[0], values[1], nvimServer.nvimArgs);
|
|
|
|
|
|
|
|
CFRelease(data);
|
2017-06-12 20:28:28 +03:00
|
|
|
}
|
2018-05-11 11:41:08 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-01-06 12:16:55 +03:00
|
|
|
|
2018-05-11 11:41:08 +03:00
|
|
|
case NvimBridgeMsgIdScroll:
|
|
|
|
return data_async(data, neovim_scroll);
|
2017-05-30 23:13:52 +03:00
|
|
|
|
2018-05-11 11:41:08 +03:00
|
|
|
case NvimBridgeMsgIdResize:
|
|
|
|
return data_async(data, neovim_resize);
|
2017-12-01 23:59:31 +03:00
|
|
|
|
2018-05-11 11:41:08 +03:00
|
|
|
case NvimBridgeMsgIdInput:
|
|
|
|
return data_async(data, neovim_vim_input);
|
2017-01-06 12:06:47 +03:00
|
|
|
|
2018-10-03 15:36:28 +03:00
|
|
|
case NvimBridgeMsgIdDeleteInput:
|
|
|
|
return data_async(data, neovim_delete_and_input);
|
2017-08-24 01:37:48 +03:00
|
|
|
|
2018-05-11 11:41:08 +03:00
|
|
|
case NvimBridgeMsgIdFocusGained:
|
|
|
|
return data_async(data, neovim_focus_gained);
|
2017-09-07 19:24:06 +03:00
|
|
|
|
2019-03-01 00:55:45 +03:00
|
|
|
case NvimBridgeMsgIdReadyForRpcEvents:
|
|
|
|
return data_async(data, neovim_ready_for_rpcevents);
|
|
|
|
|
2018-05-11 11:41:08 +03:00
|
|
|
default:
|
2018-05-13 12:09:40 +03:00
|
|
|
CFRelease(data);
|
2018-05-11 11:41:08 +03:00
|
|
|
return NULL;
|
2016-07-18 21:16:56 +03:00
|
|
|
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-20 23:35:36 +03:00
|
|
|
@implementation NvimServer {
|
2016-07-09 15:04:09 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-06-12 01:30:33 +03:00
|
|
|
- (NSArray<NSString *> *)nvimArgs {
|
|
|
|
return _nvimArgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithLocalServerName:(NSString *)localServerName
|
|
|
|
remoteServerName:(NSString *)remoteServerName
|
2018-05-11 11:41:08 +03:00
|
|
|
nvimArgs:(NSArray<NSString *> *)nvimArgs {
|
2017-06-12 01:30:33 +03:00
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
self = [super init];
|
|
|
|
if (self == nil) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-03-03 23:57:03 +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
|
2019-03-03 23:57:03 +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();
|
2019-03-03 23:57:03 +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
|
|
|
}
|
|
|
|
|
2018-01-23 22:40:20 +03:00
|
|
|
- (void)sendMessageWithId:(NvimServerMsgId)msgid {
|
2018-05-13 10:29:15 +03:00
|
|
|
[self sendMessageWithId:msgid data:NULL];
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2018-05-13 10:29:15 +03:00
|
|
|
- (void)sendMessageWithId:(NvimServerMsgId)msgid data:(CFDataRef)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) {
|
2019-03-03 23:57:03 +03:00
|
|
|
WLOG("Remote server is null: The msg (%lu) could not be sent.",
|
|
|
|
(unsigned long) msgid);
|
2016-07-09 23:52:59 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-03 23:57:03 +03:00
|
|
|
SInt32 responseCode = CFMessagePortSendRequest(
|
|
|
|
_remoteServerPort, msgid, data, qTimeout, qTimeout, NULL, NULL
|
|
|
|
);
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
if (responseCode == kCFMessagePortSuccess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-03 23:57:03 +03:00
|
|
|
WLOG("The msg (%lu) could not be sent: %d",
|
|
|
|
(unsigned long) msgid, responseCode);
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)notifyReadiness {
|
2016-08-15 11:56:19 +03:00
|
|
|
#ifndef DEBUG_NEOVIM_SERVER_STANDALONE
|
2018-05-13 10:29:15 +03:00
|
|
|
[self sendMessageWithId:NvimServerMsgIdServerReady data:NULL];
|
2016-08-15 11:56:19 +03:00
|
|
|
#endif
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|