Skip to content

Commit 2f80667

Browse files
committed
Merge pull request #234 from macvim-dev/fix/channel
Fix channel in GUI
2 parents 7069fe5 + f1caea5 commit 2f80667

File tree

9 files changed

+87
-102
lines changed

9 files changed

+87
-102
lines changed

src/MacVim/MMBackend.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ extern NSTimeInterval MMBalloonEvalInternalDelay;
5656
unsigned numWholeLineChanges;
5757
unsigned offsetForDrawDataPrune;
5858
BOOL imState;
59-
CFSocketRef netbeansSocket;
60-
CFRunLoopSourceRef netbeansRunLoopSource;
59+
NSMutableDictionary *channelDict;
6160
int winposX;
6261
int winposY;
6362
#ifdef FEAT_BEVAL
@@ -157,8 +156,8 @@ extern NSTimeInterval MMBalloonEvalInternalDelay;
157156
- (BOOL)imState;
158157
- (void)setImState:(BOOL)activated;
159158

160-
- (void)messageFromNetbeans;
161-
- (void)setNetbeansSocket:(int)socket;
159+
- (void)addChannel:(int)idx fileDescriptor:(int)fd;
160+
- (void)removeChannel:(int)idx;
162161

163162
#ifdef FEAT_BEVAL
164163
- (void)setLastToolTip:(NSString *)toolTip;

src/MacVim/MMBackend.m

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ - (NSComparisonResult)serverNameCompare:(NSString *)string;
163163
@end
164164

165165

166+
@interface MMChannel : NSObject {
167+
CFSocketRef socket;
168+
CFRunLoopSourceRef runLoopSource;
169+
}
170+
171+
- (id)initWithIndex:(int)idx fileDescriptor:(int)fd;
172+
@end
166173

167174

168175
@interface MMBackend (Private)
@@ -234,6 +241,7 @@ - (id)init
234241
connectionNameDict = [[NSMutableDictionary alloc] init];
235242
clientProxyDict = [[NSMutableDictionary alloc] init];
236243
serverReplyDict = [[NSMutableDictionary alloc] init];
244+
channelDict = [[NSMutableDictionary alloc] init];
237245

238246
NSBundle *mainBundle = [NSBundle mainBundle];
239247
NSString *path = [mainBundle pathForResource:@"Colors" ofType:@"plist"];
@@ -265,6 +273,7 @@ - (void)dealloc
265273
gui_mch_free_font(oldWideFont); oldWideFont = NOFONT;
266274
[blinkTimer release]; blinkTimer = nil;
267275
[alternateServerName release]; alternateServerName = nil;
276+
[channelDict release]; channelDict = nil;
268277
[serverReplyDict release]; serverReplyDict = nil;
269278
[clientProxyDict release]; clientProxyDict = nil;
270279
[connectionNameDict release]; connectionNameDict = nil;
@@ -1675,49 +1684,21 @@ - (void)setImState:(BOOL)activated
16751684
[self flushQueue:YES];
16761685
}
16771686

1678-
static void netbeansReadCallback(CFSocketRef s,
1679-
CFSocketCallBackType callbackType,
1680-
CFDataRef address,
1681-
const void *data,
1682-
void *info)
1687+
- (void)addChannel:(int)idx fileDescriptor:(int)fd
16831688
{
1684-
// NetBeans socket is readable.
1685-
[[MMBackend sharedInstance] messageFromNetbeans];
1686-
}
1689+
if (fd == -1)
1690+
return;
16871691

1688-
- (void)messageFromNetbeans
1689-
{
1690-
[inputQueue addObject:[NSNumber numberWithInt:NetBeansMsgID]];
1691-
[inputQueue addObject:[NSNull null]];
1692+
NSNumber *key = [NSNumber numberWithInt:idx];
1693+
MMChannel *channel =
1694+
[[[MMChannel alloc] initWithIndex:idx fileDescriptor:fd] autorelease];
1695+
[channelDict setObject:channel forKey:key];
16921696
}
16931697

1694-
- (void)setNetbeansSocket:(int)socket
1698+
- (void)removeChannel:(int)idx
16951699
{
1696-
if (netbeansSocket) {
1697-
CFRelease(netbeansSocket);
1698-
netbeansSocket = NULL;
1699-
}
1700-
1701-
if (netbeansRunLoopSource) {
1702-
CFRunLoopSourceInvalidate(netbeansRunLoopSource);
1703-
netbeansRunLoopSource = NULL;
1704-
}
1705-
1706-
if (socket == -1)
1707-
return;
1708-
1709-
// Tell CFRunLoop that we are interested in NetBeans socket input.
1710-
netbeansSocket = CFSocketCreateWithNative(kCFAllocatorDefault,
1711-
socket,
1712-
kCFSocketReadCallBack,
1713-
&netbeansReadCallback,
1714-
NULL);
1715-
netbeansRunLoopSource = CFSocketCreateRunLoopSource(NULL,
1716-
netbeansSocket,
1717-
0);
1718-
CFRunLoopAddSource(CFRunLoopGetCurrent(),
1719-
netbeansRunLoopSource,
1720-
kCFRunLoopCommonModes);
1700+
NSNumber *key = [NSNumber numberWithInt:idx];
1701+
[channelDict removeObjectForKey:key];
17211702
}
17221703

17231704
#ifdef FEAT_BEVAL
@@ -2075,10 +2056,6 @@ - (void)handleInputEvent:(int)msgid data:(NSData *)data
20752056
[self handleOpenWithArguments:[NSDictionary dictionaryWithData:data]];
20762057
} else if (FindReplaceMsgID == msgid) {
20772058
[self handleFindReplace:[NSDictionary dictionaryWithData:data]];
2078-
} else if (NetBeansMsgID == msgid) {
2079-
#ifdef FEAT_NETBEANS_INTG
2080-
netbeans_read();
2081-
#endif
20822059
} else if (ZoomMsgID == msgid) {
20832060
if (!data) return;
20842061
const void *bytes = [data bytes];
@@ -3435,3 +3412,51 @@ - (char_u *)vimStringSave
34353412
}
34363413

34373414
@end // NSString (VimStrings)
3415+
3416+
3417+
3418+
@implementation MMChannel
3419+
3420+
- (void)dealloc
3421+
{
3422+
CFRunLoopSourceInvalidate(runLoopSource);
3423+
CFRelease(runLoopSource);
3424+
CFRelease(socket);
3425+
[super dealloc];
3426+
}
3427+
3428+
static void socketReadCallback(CFSocketRef s,
3429+
CFSocketCallBackType callbackType,
3430+
CFDataRef address,
3431+
const void *data,
3432+
void *info)
3433+
{
3434+
#ifdef FEAT_CHANNEL
3435+
int idx = (int)(intptr_t)info;
3436+
channel_read(idx);
3437+
#endif
3438+
}
3439+
3440+
- (id)initWithIndex:(int)idx fileDescriptor:(int)fd
3441+
{
3442+
self = [super init];
3443+
if (!self) return nil;
3444+
3445+
// Tell CFRunLoop that we are interested in channel socket input.
3446+
CFSocketContext ctx = {0, (void *)(intptr_t)idx, NULL, NULL, NULL};
3447+
socket = CFSocketCreateWithNative(kCFAllocatorDefault,
3448+
fd,
3449+
kCFSocketReadCallBack,
3450+
&socketReadCallback,
3451+
&ctx);
3452+
runLoopSource = CFSocketCreateRunLoopSource(NULL,
3453+
socket,
3454+
0);
3455+
CFRunLoopAddSource(CFRunLoopGetCurrent(),
3456+
runLoopSource,
3457+
kCFRunLoopCommonModes);
3458+
3459+
return self;
3460+
}
3461+
3462+
@end

src/MacVim/MacVim.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ enum {
191191
DeactivatedImMsgID,
192192
BrowseForFileMsgID,
193193
ShowDialogMsgID,
194-
NetBeansMsgID,
195194
SetMarkedTextMsgID,
196195
ZoomMsgID,
197196
SetWindowPositionMsgID,

src/MacVim/MacVim.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
"DeactivatedImMsgID",
9191
"BrowseForFileMsgID",
9292
"ShowDialogMsgID",
93-
"NetBeansMsgID",
9493
"SetMarkedTextMsgID",
9594
"ZoomMsgID",
9695
"SetWindowPositionMsgID",

src/MacVim/gui_macvim.m

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,18 +2239,19 @@ static int vimModMaskToEventModifierFlags(int mods)
22392239

22402240

22412241

2242-
// -- NetBeans Support ------------------------------------------------------
2242+
// -- Channel Support ------------------------------------------------------
22432243

2244-
#ifdef FEAT_NETBEANS_INTG
2245-
2246-
/* Set NetBeans socket to CFRunLoop */
22472244
void
2248-
gui_macvim_set_netbeans_socket(int socket)
2245+
gui_macvim_add_channel(int idx, int fd)
22492246
{
2250-
[[MMBackend sharedInstance] setNetbeansSocket:socket];
2247+
[[MMBackend sharedInstance] addChannel:idx fileDescriptor:fd];
22512248
}
22522249

2253-
#endif // FEAT_NETBEANS_INTG
2250+
void
2251+
gui_macvim_remove_channel(int idx)
2252+
{
2253+
[[MMBackend sharedInstance] removeChannel:idx];
2254+
}
22542255

22552256

22562257

@@ -2309,13 +2310,6 @@ static int vimModMaskToEventModifierFlags(int mods)
23092310
[imgName release];
23102311
}
23112312

2312-
# ifdef FEAT_NETBEANS_INTG
2313-
void
2314-
netbeans_draw_multisign_indicator(int row)
2315-
{
2316-
}
2317-
# endif // FEAT_NETBEANS_INTG
2318-
23192313
#endif // FEAT_SIGN_ICONS
23202314

23212315

src/channel.c

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ typedef struct {
9595
int ch_inputHandler; /* simply ret.value of WSAAsyncSelect() */
9696
#endif
9797
#ifdef FEAT_GUI_MACVIM
98-
int ch_inputHandler;
98+
int ch_inputHandler;
9999
#endif
100100

101101
void (*ch_close_cb)(void); /* callback for when channel is closed */
@@ -155,6 +155,9 @@ add_channel(void)
155155
#ifdef FEAT_GUI_W32
156156
channels[channel_count].ch_inputHandler = -1;
157157
#endif
158+
#ifdef FEAT_GUI_MACVIM
159+
channels[channel_count].ch_inputHandler = -1;
160+
#endif
158161

159162
return channel_count++;
160163
}
@@ -183,22 +186,6 @@ messageFromNetbeans(gpointer clientData,
183186
}
184187
#endif
185188

186-
#ifdef FEAT_GUI_MACVIM
187-
static int
188-
sock_select(int s)
189-
{
190-
fd_set readset;
191-
struct timeval timeout;
192-
193-
FD_ZERO(&readset);
194-
FD_SET(s, &readset);
195-
timeout.tv_sec = 0;
196-
timeout.tv_usec = 0;
197-
198-
return select(s + 1, &readset, NULL, NULL, &timeout);
199-
}
200-
#endif /* FEAT_GUI_MACVIM */
201-
202189
static void
203190
channel_gui_register(int idx)
204191
{
@@ -244,9 +231,7 @@ channel_gui_register(int idx)
244231
*/
245232
if (channel->ch_inputHandler == -1) {
246233
channel->ch_inputHandler = 0;
247-
# ifdef FEAT_NETBEANS_INTG
248-
gui_macvim_set_netbeans_socket(channel->ch_fd);
249-
# endif
234+
gui_macvim_add_channel(idx, channel->ch_fd);
250235
}
251236
# endif
252237
# endif
@@ -297,9 +282,7 @@ channel_gui_unregister(int idx)
297282
# ifdef FEAT_GUI_MACVIM
298283
if (channel->ch_inputHandler == 0)
299284
{
300-
# ifdef FEAT_NETBEANS_INTG
301-
gui_macvim_set_netbeans_socket(-1);
302-
# endif
285+
gui_macvim_remove_channel(idx);
303286
channel->ch_inputHandler = -1;
304287
}
305288
# endif
@@ -812,13 +795,6 @@ channel_read(int idx)
812795
return;
813796
}
814797

815-
#ifdef FEAT_GUI_MACVIM
816-
/* It may happen that socket is not readable because socket has been already
817-
* read by timing of CFRunLoop callback. So check socket using select. */
818-
if (sock_select(channel->ch_fd) <= 0)
819-
return;
820-
#endif
821-
822798
/* Allocate a buffer to read into. */
823799
if (buf == NULL)
824800
{

src/netbeans.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -427,14 +427,6 @@ netbeans_parse_messages(void)
427427
}
428428
}
429429

430-
/* TODO: remove */
431-
void
432-
netbeans_read()
433-
{
434-
if (nb_channel_idx >= 0)
435-
channel_read(nb_channel_idx);
436-
}
437-
438430
/*
439431
* Handle one NUL terminated command.
440432
*

src/proto/gui_macvim.pro

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@ gui_mch_replace_dialog(exarg_T *eap);
228228
im_set_control(int enable);
229229

230230
void
231-
gui_macvim_set_netbeans_socket(int socket);
231+
gui_macvim_add_channel(int idx, int fd);
232+
void
233+
gui_macvim_remove_channel(int idx);
232234

233235
void
234236
gui_mch_drawsign(int row, int col, int typenr);

src/proto/netbeans.pro

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* netbeans.c */
22
void netbeans_parse_messages(void);
3-
void netbeans_read(void);
43
int isNetbeansBuffer(buf_T *bufp);
54
int isNetbeansModified(buf_T *bufp);
65
void netbeans_end(void);

0 commit comments

Comments
 (0)