Created
August 4, 2012 21:20
-
-
Save saitoha/3260028 to your computer and use it in GitHub Desktop.
OSC 52(Base64 Clipboard Access) / set access for mouseterm any-event-tracking branch(https://github.com/saitoha/mouseterm/tree/any-event-tracking)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/English.lproj/Localizable.strings b/English.lproj/Localizable.strings | |
| index 8995096..4610a47 100644 | |
| --- a/English.lproj/Localizable.strings | |
| +++ b/English.lproj/Localizable.strings | |
| @@ -1,2 +1,3 @@ | |
| /* Send Mouse Events menu item */ | |
| "Send Mouse Events" = "Send Mouse Events"; | |
| +"Enable Base64 Copy" = "Enable Base64 Copy"; | |
| diff --git a/French.lproj/Localizable.strings b/French.lproj/Localizable.strings | |
| index 8a90845..93ca920 100644 | |
| --- a/French.lproj/Localizable.strings | |
| +++ b/French.lproj/Localizable.strings | |
| @@ -1,2 +1,3 @@ | |
| /* Send Mouse Events menu item */ | |
| "Send Mouse Events" = "Envoyer les évènements souris"; | |
| +"Enable Base64 Copy" = "Permettre la base64 Copie"; | |
| diff --git a/MTParser.rl b/MTParser.rl | |
| index 7c80c85..db85428 100644 | |
| --- a/MTParser.rl | |
| +++ b/MTParser.rl | |
| @@ -1,10 +1,13 @@ | |
| #import <Cocoa/Cocoa.h> | |
| +#import <apr-1/apr.h> | |
| +#import <apr-1/apr_base64.h> | |
| #import <math.h> | |
| #import "Mouse.h" | |
| #import "MouseTerm.h" | |
| #import "MTParser.h" | |
| #import "MTParserState.h" | |
| #import "MTShell.h" | |
| +#import "MTView.h" | |
| #import "MTTabController.h" | |
| #import "Terminal.h" | |
| @@ -75,6 +78,40 @@ | |
| [mobj MouseTerm_setMouseProtocol: NORMAL_PROTOCOL]; | |
| } | |
| + action handle_osc52_start | |
| + { | |
| + if ([NSView MouseTerm_getBase64CopyEnabled]) { | |
| + if (osc52Buffer) | |
| + [osc52Buffer release]; | |
| + osc52Buffer = [[NSMutableData alloc] init]; | |
| + } | |
| + } | |
| + | |
| + action handle_osc52 | |
| + { | |
| + if (osc52Buffer) | |
| + [osc52Buffer appendBytes: &fc length: 1]; | |
| + } | |
| + | |
| + action handle_osc_end | |
| + { | |
| + if (osc52Buffer) { | |
| + NSString *str= [[NSString alloc] initWithData:osc52Buffer | |
| + encoding:NSASCIIStringEncoding]; | |
| + char *encodedBuffer = (char*)[str cStringUsingEncoding:NSASCIIStringEncoding]; | |
| + int destLength = apr_base64_decode_len(encodedBuffer); | |
| + char *decodedBuffer = malloc(destLength); | |
| + apr_base64_decode(decodedBuffer, encodedBuffer); | |
| + NSString *resultString = [NSString stringWithUTF8String:decodedBuffer]; | |
| + free(decodedBuffer); | |
| + | |
| + [[[[mobj controller] activePane] view] copy: nil]; | |
| + [mobj MouseTerm_writeToPasteBoard: resultString]; | |
| + [osc52Buffer release]; | |
| + osc52Buffer = nil; | |
| + } | |
| + } | |
| + | |
| esc = 0x1b; | |
| csi = esc . "["; | |
| flag = ("h" | "l") @handle_flag; | |
| @@ -88,15 +125,23 @@ | |
| | "1015" . flag @handle_flag @handle_urxvt_protocol | |
| | "1006" . flag @handle_flag @handle_sgr_protocol); | |
| bel = 0x07; | |
| - st = 0x9c; | |
| + st = esc . "\\" | 0x9c; | |
| + base64 = ([a-zA-Z0-9\+/]+[=]*); | |
| + osc52 = osc . "52;" . [0-9]* . ";" @handle_osc52_start; | |
| - main := ((any - csi | any - osc)* . (mode_toggle # @got_toggle | |
| - | cs_sda @handle_sda | |
| - | debug @got_debug))*; | |
| + main := ((base64 @handle_osc52 | |
| + | (bel | st) @handle_osc_end | |
| + | any - csi | |
| + | any - osc)* . (mode_toggle # @got_toggle | |
| + | osc52 | |
| + | cs_sda @handle_sda | |
| + | debug @got_debug))*; | |
| }%% | |
| %% write data; | |
| +static NSMutableData *osc52Buffer = nil; | |
| + | |
| int MTParser_execute(const char* data, int len, BOOL isEof, id obj, | |
| MTParserState* state) | |
| { | |
| diff --git a/MTShell.h b/MTShell.h | |
| index 22ce5c8..e4dabf6 100644 | |
| --- a/MTShell.h | |
| +++ b/MTShell.h | |
| @@ -19,6 +19,9 @@ | |
| - (void) MouseTerm_setIsMouseDown: (BOOL) isMouseDown; | |
| - (BOOL) MouseTerm_getIsMouseDown; | |
| +- (BOOL) MouseTerm_writeToPasteBoard: (NSString*) stringToWrite; | |
| +- (NSString*) MouseTerm_readFromPasteBoard; | |
| + | |
| - (void) MouseTerm_setParserState: (MTParserState*) parserState; | |
| - (MTParserState*) MouseTerm_getParserState; | |
| diff --git a/MTShell.m b/MTShell.m | |
| index 411c3d6..98d6d99 100644 | |
| --- a/MTShell.m | |
| +++ b/MTShell.m | |
| @@ -86,6 +86,17 @@ | |
| objectForKey: @"isMouseDown"] boolValue]; | |
| } | |
| +- (BOOL) MouseTerm_writeToPasteBoard: (NSString*) stringToWrite | |
| +{ | |
| + return [[NSPasteboard generalPasteboard] setString:stringToWrite | |
| + forType:NSStringPboardType]; | |
| +} | |
| + | |
| +- (NSString*) MouseTerm_readFromPasteBoard | |
| +{ | |
| + return [[NSPasteboard generalPasteboard] stringForType:NSStringPboardType]; | |
| +} | |
| + | |
| - (void) MouseTerm_setParserState: (MTParserState*) parserState | |
| { | |
| NSValue *ptr = [self MouseTerm_initVars]; | |
| diff --git a/MTTabController.m b/MTTabController.m | |
| index 1ef9ccc..0fa7926 100644 | |
| --- a/MTTabController.m | |
| +++ b/MTTabController.m | |
| @@ -3,6 +3,7 @@ | |
| #import "MTParser.h" | |
| #import "MTParserState.h" | |
| #import "MTShell.h" | |
| +#import "MTView.h" | |
| #import "MTTabController.h" | |
| #import "Terminal.h" | |
| diff --git a/MTView.h b/MTView.h | |
| index 2373679..207dc08 100644 | |
| --- a/MTView.h | |
| +++ b/MTView.h | |
| @@ -7,8 +7,10 @@ | |
| button: (MouseButton) button | |
| motion: (BOOL) motion | |
| release: (BOOL) release; | |
| -+ (void) MouseTerm_setEnabled: (BOOL) value; | |
| -+ (BOOL) MouseTerm_getEnabled; | |
| ++ (void) MouseTerm_setMouseEnabled: (BOOL) value; | |
| ++ (BOOL) MouseTerm_getMouseEnabled; | |
| ++ (void) MouseTerm_setBase64CopyEnabled: (BOOL) value; | |
| ++ (BOOL) MouseTerm_getBase64CopyEnabled; | |
| - (NSScroller*) MouseTerm_scroller; | |
| - (BOOL) MouseTerm_shouldIgnore: (NSEvent*) event button: (MouseButton) button; | |
| - (BOOL) MouseTerm_shouldIgnoreDown: (NSEvent*) event | |
| diff --git a/MTView.m b/MTView.m | |
| index a57c7a2..4efc5d2 100644 | |
| --- a/MTView.m | |
| +++ b/MTView.m | |
| @@ -8,7 +8,8 @@ | |
| @implementation NSView (MTView) | |
| -static BOOL enabled = YES; | |
| +static BOOL mouseEnabled = YES; | |
| +static BOOL base64CopyEnabled = YES; | |
| - (NSData*) MouseTerm_codeForEvent: (NSEvent*) event | |
| button: (MouseButton) button | |
| @@ -69,14 +70,24 @@ static BOOL enabled = YES; | |
| return [NSData dataWithBytes: buf length: len]; | |
| } | |
| -+ (void) MouseTerm_setEnabled: (BOOL) value | |
| ++ (void) MouseTerm_setMouseEnabled: (BOOL) value | |
| { | |
| - enabled = value; | |
| + mouseEnabled = value; | |
| } | |
| -+ (BOOL) MouseTerm_getEnabled | |
| ++ (BOOL) MouseTerm_getMouseEnabled | |
| { | |
| - return enabled; | |
| + return mouseEnabled; | |
| +} | |
| + | |
| ++ (void) MouseTerm_setBase64CopyEnabled: (BOOL) value | |
| +{ | |
| + base64CopyEnabled = value; | |
| +} | |
| + | |
| ++ (BOOL) MouseTerm_getBase64CopyEnabled | |
| +{ | |
| + return base64CopyEnabled; | |
| } | |
| - (NSScroller*) MouseTerm_scroller | |
| @@ -89,7 +100,7 @@ static BOOL enabled = YES; | |
| - (BOOL) MouseTerm_shouldIgnore: (NSEvent*) event button: (MouseButton) button | |
| { | |
| - if (![NSView MouseTerm_getEnabled]) | |
| + if (![NSView MouseTerm_getMouseEnabled]) | |
| return YES; | |
| // Don't handle if alt/option/control is pressed | |
| @@ -380,7 +391,7 @@ ignored: | |
| { | |
| MTProfile* profile = [(TTTabController*) [(TTView*) self controller] | |
| profile]; | |
| - if ([NSView MouseTerm_getEnabled] && | |
| + if ([NSView MouseTerm_getMouseEnabled] && | |
| [profile MouseTerm_emulationEnabled] && | |
| [screen isAlternateScreenActive] && | |
| [shell MouseTerm_getAppCursorMode]) | |
| diff --git a/Makefile b/Makefile | |
| index 0cf09f5..9ee1b74 100644 | |
| --- a/Makefile | |
| +++ b/Makefile | |
| @@ -12,7 +12,7 @@ endif | |
| OPTLEVEL=2 | |
| CFLAGS+=-std=c99 -O$(OPTLEVEL) -Wall -mmacosx-version-min=$(OSXVER) $(ARCHES) | |
| -LDFLAGS+=-bundle -framework Cocoa | |
| +LDFLAGS+=-bundle -framework Cocoa -laprutil-1 -lapr-1 | |
| OBJS=JRSwizzle.o MouseTerm.m MTAppPrefsController.o MTParser.o \ | |
| MTParserState.o MTProfile.o MTShell.o MTTabController.o MTView.o | |
| diff --git a/MouseTerm.h b/MouseTerm.h | |
| index 7f3fee0..db5aeaf 100644 | |
| --- a/MouseTerm.h | |
| +++ b/MouseTerm.h | |
| @@ -4,7 +4,8 @@ extern NSMutableDictionary* MouseTerm_ivars; | |
| @interface MouseTerm: NSWindowController | |
| + (void) load; | |
| -+ (IBAction) toggle: (NSMenuItem*) sender; | |
| ++ (IBAction) toggleMouse: (NSMenuItem*) sender; | |
| ++ (IBAction) toggleBase64Copy: (NSMenuItem*) sender; | |
| + (void) insertMenuItem; | |
| + (MouseTerm*) sharedInstance; | |
| - (void) orderFrontMouseConfiguration: (id) sender; | |
| diff --git a/MouseTerm.m b/MouseTerm.m | |
| index e2f2e3e..4fe50cd 100644 | |
| --- a/MouseTerm.m | |
| +++ b/MouseTerm.m | |
| @@ -130,10 +130,16 @@ NSMutableDictionary* MouseTerm_ivars = nil; | |
| [self insertMenuItem]; | |
| } | |
| -+ (IBAction) toggle: (NSMenuItem*) sender | |
| ++ (IBAction) toggleMouse: (NSMenuItem*) sender | |
| { | |
| [sender setState: ![sender state]]; | |
| - [NSView MouseTerm_setEnabled: [sender state]]; | |
| + [NSView MouseTerm_setMouseEnabled: [sender state]]; | |
| +} | |
| + | |
| ++ (IBAction) toggleBase64Copy: (NSMenuItem*) sender | |
| +{ | |
| + [sender setState: ![sender state]]; | |
| + [NSView MouseTerm_setBase64CopyEnabled: [sender state]]; | |
| } | |
| + (void) insertMenuItem; | |
| @@ -147,21 +153,37 @@ NSMutableDictionary* MouseTerm_ivars = nil; | |
| [shellMenu addItem: [NSMenuItem separatorItem]]; | |
| NSBundle *bundle = [NSBundle bundleForClass: self]; | |
| - NSString* t = NSLocalizedStringFromTableInBundle(@"Send Mouse Events", nil, | |
| + NSString* t1 = NSLocalizedStringFromTableInBundle(@"Send Mouse Events", nil, | |
| + bundle, nil); | |
| + NSMenuItem* itemToggleMouse = [shellMenu addItemWithTitle: t1 | |
| + action: @selector(toggleMouse:) | |
| + keyEquivalent: @"m"]; | |
| + if (!itemToggleMouse) | |
| + { | |
| + NSLog(@"[MouseTerm] ERROR: Unable to create menu item: toggleMouse"); | |
| + return; | |
| + } | |
| + | |
| + [itemToggleMouse setKeyEquivalentModifierMask: (NSShiftKeyMask | NSCommandKeyMask)]; | |
| + [itemToggleMouse setTarget: self]; | |
| + [itemToggleMouse setState: NSOnState]; | |
| + [itemToggleMouse setEnabled: YES]; | |
| + | |
| + NSString* t2 = NSLocalizedStringFromTableInBundle(@"Enable Base64 Copy", nil, | |
| bundle, nil); | |
| - NSMenuItem* item = [shellMenu addItemWithTitle: t | |
| - action: @selector(toggle:) | |
| - keyEquivalent: @"m"]; | |
| - if (!item) | |
| + NSMenuItem* itemToggleBase64Copy = [shellMenu addItemWithTitle: t2 | |
| + action: @selector(toggleBase64Copy:) | |
| + keyEquivalent: @"b"]; | |
| + if (!itemToggleBase64Copy) | |
| { | |
| - NSLog(@"[MouseTerm] ERROR: Unable to create menu item"); | |
| + NSLog(@"[MouseTerm] ERROR: Unable to create menu item: toggleBase64Copy"); | |
| return; | |
| } | |
| - [item setKeyEquivalentModifierMask: (NSShiftKeyMask | NSCommandKeyMask)]; | |
| - [item setTarget: self]; | |
| - [item setState: NSOnState]; | |
| - [item setEnabled: YES]; | |
| + [itemToggleBase64Copy setKeyEquivalentModifierMask: (NSShiftKeyMask | NSCommandKeyMask)]; | |
| + [itemToggleBase64Copy setTarget: self]; | |
| + [itemToggleBase64Copy setState: NSOnState]; | |
| + [itemToggleBase64Copy setEnabled: YES]; | |
| } | |
| + (MouseTerm*) sharedInstance | |
| diff --git a/Terminal.h b/Terminal.h | |
| index d509e53..e1a871f 100644 | |
| --- a/Terminal.h | |
| +++ b/Terminal.h | |
| @@ -3,6 +3,8 @@ | |
| @class MTShell; | |
| @class MTTabController; | |
| @class MTProfile; | |
| +@class TTPane; | |
| +@class TTView; | |
| // Classes from Terminal.app being overridden | |
| @@ -30,12 +32,14 @@ typedef struct | |
| // TTPane is new in OS X 10.6 | |
| @interface TTPane: NSObject | |
| - (NSScroller*) scroller; | |
| +@property(readonly) TTView *view; // @synthesize view; | |
| @end | |
| @interface TTTabController: NSObject | |
| - (NSScroller*) scroller; // This method exists only in OS X 10.5 or older | |
| - (MTShell*) shell; | |
| - (MTProfile*) profile; | |
| +@property(readonly) TTPane *activePane; // @synthesize activePane; | |
| @end | |
| @interface TTView: NSView | |
| @@ -46,6 +50,7 @@ typedef struct | |
| - (Position) displayPositionForPoint: (NSPoint) point; | |
| - (void) clearTextSelection; | |
| - (struct CGSize)cellSize; | |
| +- (void)copy:(id)arg1; | |
| @end | |
| @interface TTProfileArrayController: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment