Skip to content

Commit

Permalink
Add support for reporting focus lost/gained (esc[?1004h enables, esc[…
Browse files Browse the repository at this point in the history
…I is sent by term when focus gained, esc[O when lost, esc[?1004l disables)
  • Loading branch information
gnachman committed Oct 11, 2011
1 parent b7a7900 commit e5865f0
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Headers/iTerm/PTYSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ typedef enum {

// The current triggers.
NSMutableArray *triggers_;

// Does the terminal think this session is focused?
BOOL focused_;
}

// Return the current pasteboard value as a string.
Expand Down Expand Up @@ -498,6 +501,8 @@ typedef enum {
- (void)stopCoprocess;
- (void)launchCoprocessWithCommand:(NSString *)command;

- (void)setFocused:(BOOL)focused;

@end

@interface PTYSession (ScriptingSupport)
Expand Down
2 changes: 2 additions & 0 deletions Headers/iTerm/VT100Terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ typedef enum {
BOOL shouldBounceDockIcon; // YES=Bounce, NO=cancel;
MouseMode MOUSE_MODE;
MouseFormat MOUSE_FORMAT;
BOOL REPORT_FOCUS;

int FG_COLORCODE;
BOOL alternateForegroundSemantics;
Expand Down Expand Up @@ -379,6 +380,7 @@ typedef enum {
- (NSData *)keypadData: (unichar) unicode keystr: (NSString *) keystr;

- (char *)mouseReport:(int)button atX:(int)x Y:(int)y;
- (BOOL)reportFocus;
- (NSData *)mousePress:(int)button withModifiers:(unsigned int)modflag atX:(int)x Y:(int)y;
- (NSData *)mouseReleaseWithModifiers:(unsigned int)modflag atX:(int)x Y:(int)y;
- (NSData *)mouseMotion:(int)button withModifiers:(unsigned int)modflag atX:(int)x Y:(int)y;
Expand Down
12 changes: 12 additions & 0 deletions PTYSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -3140,6 +3140,18 @@ - (void)launchCoprocessWithCommand:(NSString *)command
[TEXTVIEW setNeedsDisplay:YES];
}

- (void)setFocused:(BOOL)focused
{
if (focused != focused_) {
focused_ = focused;
if ([TERMINAL reportFocus]) {
char flag = focused ? 'I' : 'O';
NSString *message = [NSString stringWithFormat:@"%c[%c", 27, flag];
[self writeTask:[message dataUsingEncoding:[self encoding]]];
}
}
}

@end

@implementation PTYSession (ScriptingSupport)
Expand Down
10 changes: 9 additions & 1 deletion PTYTab.m
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,15 @@ - (void)setActiveSessionPreservingViewOrder:(PTYSession*)session
[self setLabelAttributes];
[[NSNotificationCenter defaultCenter] postNotificationName:@"iTermSessionBecameKey"
object:activeSession_];

// If the active session changed in the active tab in the key window then update the
// focused state of all sessions in that window.
if ([[self realParentWindow] currentTab] == self &&
[[[self realParentWindow] window] isKeyWindow]) {
for (PTYSession *aSession in [[self realParentWindow] sessions]) {
[aSession setFocused:(aSession == session)];
}
}

NSUInteger i = [viewOrder_ indexOfObject:[NSNumber numberWithInt:[[session view] viewId]]];
if (i != NSNotFound) {
currentViewIndex_ = i;
Expand Down
9 changes: 9 additions & 0 deletions PseudoTerminal.m
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,7 @@ - (void)windowDidBecomeKey:(NSNotification *)aNotification
for (PTYSession* aSession in [self sessions]) {
[aSession updateDisplay];
[[aSession view] setBackgroundDimmed:NO];
[aSession setFocused:aSession == [self currentSession]];
}
}

Expand Down Expand Up @@ -1643,6 +1644,9 @@ - (void)windowDidResignKey:(NSNotification *)aNotification
[[aSession view] setBackgroundDimmed:YES];
}
}
for (PTYSession* aSession in [self sessions]) {
[aSession setFocused:NO];
}
}

- (void)windowDidResignMain:(NSNotification *)aNotification
Expand Down Expand Up @@ -2357,6 +2361,11 @@ - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabVi
// Post notifications
[[NSNotificationCenter defaultCenter] postNotificationName:@"iTermSessionBecameKey"
object:[[tabViewItem identifier] activeSession]];

PTYSession *activeSession = [self currentSession];
for (PTYSession *s in [self sessions]) {
[aSession setFocused:(s == activeSession)];
}
[self showOrHideInstantReplayBar];
}

Expand Down
7 changes: 7 additions & 0 deletions VT100Terminal.m
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,10 @@ - (NSData *)mouseMotion:(int)button withModifiers:(unsigned int)modflag atX:(int
return [NSData dataWithBytes: buf length: strlen(buf)];
}

- (BOOL)reportFocus
{
return REPORT_FOCUS;
}

- (BOOL)lineMode
{
Expand Down Expand Up @@ -2460,6 +2464,9 @@ - (void)_setMode:(VT100TCC)token
}
[SCREEN mouseModeDidChange:MOUSE_MODE];
break;
case 1004:
REPORT_FOCUS = mode;
break;

case 1005:
if (mode) {
Expand Down

0 comments on commit e5865f0

Please sign in to comment.