Skip to content

Commit

Permalink
Support both tmux2.2 and earlier versions, which accepted UTF-8 input…
Browse files Browse the repository at this point in the history
… in different ways. Issue 4736
  • Loading branch information
gnachman committed Jun 4, 2016
1 parent 5e87cf0 commit befc1bb
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions sources/PTYSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -4325,6 +4325,7 @@ - (void)tmuxWindowRenamedWithId:(int)windowId to:(NSString *)newName {
- (void)tmuxInitialCommandDidCompleteSuccessfully {
// This kicks off a chain reaction that leads to windows being opened.
[_tmuxController validateOptions];
[_tmuxController guessVersion];
}

- (void)tmuxInitialCommandDidFailWithError:(NSString *)error {
Expand Down
4 changes: 4 additions & 0 deletions sources/TmuxController.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ extern NSString *const kTmuxControllerSessionWasRenamed;
- (BOOL)windowDidResize:(NSWindowController<iTermWindowController> *)term;
- (void)fitLayoutToWindows;
- (void)validateOptions;

// Issue tmux commands to infer bounds on the version.
- (void)guessVersion;

- (void)setClientSize:(NSSize)size;
- (void)windowPane:(int)wp
resizedBy:(int)amount
Expand Down
18 changes: 18 additions & 0 deletions sources/TmuxController.m
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,24 @@ - (void)validateOptions
}
}

- (void)guessVersion {
[gateway_ sendCommand:@"list-windows -F \"#{socket_path}\""
responseTarget:self
responseSelector:@selector(guessVersionResponse:)
responseObject:nil
flags:kTmuxGatewayCommandShouldTolerateErrors];
}

- (void)guessVersionResponse:(NSString *)response {
if (response.length == 0) {
DLog(@"Looks like tmux 2.1 or earlier");
gateway_.maximumServerVersion = @2.1;
} else {
DLog(@"Looks like tmux 2.2 or later");
gateway_.minimumServerVersion = @2.2;
}
}

// Show an error and terminate the connection because tmux has an unsupported option turned on.
- (void)optionValidationFailedForOption:(NSString *)option
{
Expand Down
2 changes: 2 additions & 0 deletions sources/TmuxGateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ typedef NS_ENUM(NSInteger, ControlCommand) {
@property(nonatomic, assign) BOOL tmuxLogging;
@property(nonatomic, readonly) NSWindowController<iTermWindowController> *window;
@property(nonatomic, readonly) id<TmuxGatewayDelegate> delegate;
@property(nonatomic, retain) NSNumber *minimumServerVersion;
@property(nonatomic, retain) NSNumber *maximumServerVersion;

- (instancetype)initWithDelegate:(id<TmuxGatewayDelegate>)delegate;

Expand Down
21 changes: 20 additions & 1 deletion sources/TmuxGateway.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
NSString * const kTmuxGatewayErrorDomain = @"kTmuxGatewayErrorDomain";;
const int kTmuxGatewayCommandShouldTolerateErrors = (1 << 0);
const int kTmuxGatewayCommandWantsData = (1 << 1);
static const double kVersionNumberComparisonEpsilon = 0.00001;

#define NEWLINE @"\r"

Expand Down Expand Up @@ -78,6 +79,8 @@ - (void)dealloc {
[currentCommandResponse_ release];
[currentCommandData_ release];
[strayMessages_ release];
[_minimumServerVersion release];
[_maximumServerVersion release];

[super dealloc];
}
Expand Down Expand Up @@ -520,7 +523,15 @@ - (NSString *)stringForKeyEncodedData:(NSData *)data
}

- (void)sendKeys:(NSString *)string toWindowPane:(int)windowPane {
[self sendCodePoints:[string codePoints] toWindowPane:windowPane];
if ([self serverSupportsUTF8]) {
// Send the actual code point of each character.
[self sendCodePoints:[string codePoints] toWindowPane:windowPane];
} else {
// Send each byte of UTF-8 as a separate "keystroke". For tmux 2.1 and earlier.
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSString *temp = [[[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding] autorelease];
[self sendCodePoints:[temp codePoints] toWindowPane:windowPane];
}
}

- (void)sendCodePoints:(NSArray<NSNumber *> *)codePoints toWindowPane:(int)windowPane {
Expand All @@ -543,6 +554,14 @@ - (void)sendCodePoints:(NSArray<NSNumber *> *)codePoints toWindowPane:(int)windo
[delegate_ tmuxSetSecureLogging:NO];
}

- (BOOL)doubleValue:(double)value1 isGreaterOrEqualTo:(double)value2 epsilon:(double)epsilon {
return value1 - value2 >= -epsilon;
}
- (BOOL)serverSupportsUTF8 {
return (self.minimumServerVersion &&
[self doubleValue:self.minimumServerVersion.doubleValue isGreaterOrEqualTo:2.2 epsilon:kVersionNumberComparisonEpsilon]);
}

- (NSDictionary *)dictionaryForSendKeysCommandWithCodePoints:(NSArray<NSNumber *> *)codePoints
windowPane:(int)windowPane {
NSString *command = [NSString stringWithFormat:@"send-keys -t %%%d %@",
Expand Down

0 comments on commit befc1bb

Please sign in to comment.