/
QSGlobalSelectionProvider.m
182 lines (160 loc) · 6.32 KB
/
QSGlobalSelectionProvider.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
// QSGlobalSelectionProvider.m
// Quicksilver
//
// Created by Alcor on 1/21/05.
// Copyright 2005 Blacktree. All rights reserved.
//
#import "QSGlobalSelectionProvider.h"
#import "QSRegistry.h"
#import "QSProcessMonitor.h"
#import "QSProxyObject.h"
#import "QSObject_StringHandling.h"
#import "QSObject_Pasteboard.h"
#import "NDProcess+QSMods.h"
@interface QSTemporaryServiceProvider : NSObject {
NSPasteboard *resultPboard;
// NSString *resultUserData;
}
- (void)invokeService;
- (NSPasteboard *)getSelectionFromFrontApp;
@end
@implementation QSTemporaryServiceProvider
- (id)init {
if (self = [super init]) {
resultPboard = nil;
}
return self;
}
- (void)getSelection:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error {
#ifdef DEBUG
if (VERBOSE) NSLog(@"Get Selection: %@ %C", userData, [userData characterAtIndex:0]);
#endif
if(resultPboard)
[resultPboard release];
resultPboard = [pboard retain];
}
#ifdef DEBUG
- (void)performService:(NSPasteboard *)pboard
userData:(NSString *)userData
error:(NSString **)error {
if (VERBOSE) NSLog(@"xPerform Service: %@ %C", userData, [userData characterAtIndex:0]);
}
#endif
- (NSPasteboard *)getSelectionFromFrontApp {
//NSLog(@"GET SEL");
id oldServicesProvider = [[NSApp servicesProvider] retain];
[NSApp setServicesProvider:self];
[NSThread detachNewThreadSelector:@selector(invokeService)
toTarget:self withObject:nil];
NSRunLoop *loop = [NSRunLoop currentRunLoop];
NSDate *date = [NSDate date];
while(!resultPboard && [date timeIntervalSinceNow] >-2) {
[loop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
// NSLog(@"loop");
}
// NSLog(@"got %@", resultPboard);
[NSApp setServicesProvider:oldServicesProvider];
[oldServicesProvider release];
id result = [resultPboard autorelease];
resultPboard = nil;
return result;
}
- (void)dealloc {
// NSLog(@"release");
[resultPboard release];
[super dealloc];
}
- (void)invokeService {
@autoreleasepool {
// If the active application is Quicksilver, grab the selection from the previous app (QS most likely stole focus)
NSDictionary *activeApplication = [[QSProcessMonitor sharedInstance] currentApplication];
if ([[activeApplication objectForKey:@"NSApplicationBundleIdentifier"] isEqualToString:@"com.blacktree.Quicksilver"]) {
activeApplication = [[QSProcessMonitor sharedInstance] previousApplication];
}
pid_t pid = [[activeApplication objectForKey:@"NSApplicationProcessIdentifier"] intValue];
if ([NSApplication isLion]) {
//AXUIElement* is unable to post keys into sandboxed app since 10.7, use Quartz Event Services instead
ProcessSerialNumber psn;
BOOL usePID = GetProcessForPID(pid, &psn) == 0;
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStatePrivate);
CGEventRef keyDown = CGEventCreateKeyboardEvent (source, (CGKeyCode)53, true); //Escape
CGEventSetFlags(keyDown, kCGEventFlagMaskCommand);
if (usePID) {
CGEventPostToPSN(&psn, keyDown);
} else {
CGEventPost(kCGHIDEventTap, keyDown);
}
CGEventRef keyUp = CGEventCreateKeyboardEvent (source, (CGKeyCode)53, false); //Escape
CGEventSetFlags(keyUp, kCGEventFlagMaskCommand);
if (usePID) {
CGEventPostToPSN(&psn, keyUp);
} else {
CGEventPost(kCGHIDEventTap, keyUp);
}
CFRelease(keyDown);
CFRelease(keyUp);
CFRelease(source);
} else { // 10.6 method (apps don't lose focus when service is invoked)
AXUIElementRef app = AXUIElementCreateApplication (pid);
AXUIElementPostKeyboardEvent (app, (CGCharCode) 0, (CGKeyCode)55, true ); //Command
AXUIElementPostKeyboardEvent (app, (CGCharCode) 0, (CGKeyCode)53, true ); //Escape
AXUIElementPostKeyboardEvent (app, (CGCharCode) 0, (CGKeyCode)53, false ); //Escape
AXUIElementPostKeyboardEvent (app, (CGCharCode) 0, (CGKeyCode)55, true ); //Command
CFRelease( app );
}
}
}
@end
@implementation QSGlobalSelectionProvider
//static QSObject *dropletSelection;
NSTimeInterval failDate = 0;
+ (id)currentSelection {
NSString *identifier = [[[NSWorkspace sharedWorkspace] activeApplication] objectForKey:@"NSApplicationBundleIdentifier"];
NSDictionary *info = [[QSReg tableNamed:@"QSProxies"] objectForKey:identifier];
if (info) {
id provider = [QSReg getClassInstance:[info objectForKey:kQSProxyProviderClass]];
//if (VERBOSE)
// NSLog(@"Using provider %@ for %@", provider, identifier);
return [provider resolveProxyObject:nil];
} else {
QSTemporaryServiceProvider *sp = [[[QSTemporaryServiceProvider alloc] init] autorelease];
NSPasteboard *pb = nil;
if ([NSDate timeIntervalSinceReferenceDate] -failDate > 3.0)
pb = [sp getSelectionFromFrontApp];
if (!pb) {
failDate = [NSDate timeIntervalSinceReferenceDate];
return nil;
}
return [QSObject objectWithPasteboard:pb];
}
return [QSObject objectWithString:@"No Selection"]; //[QSObject nullObject];
}
- (id)resolveProxyObject:(id)proxy {
id object = [QSGlobalSelectionProvider currentSelection];
//NSLog(@"object %@", object);
return object;
}
- (BOOL)bypassValidation {
NSDictionary *appDictionary = [[NSWorkspace sharedWorkspace] activeApplication];
NSString *identifier = [appDictionary objectForKey:@"NSApplicationBundleIdentifier"];
if ([identifier isEqualToString:kQSBundleID])
return YES;
else
return NO;
}
- (NSArray *)typesForProxyObject:(id)proxy {
NSDictionary *appDictionary = [[NSWorkspace sharedWorkspace] activeApplication];
NSString *identifier = [appDictionary objectForKey:@"NSApplicationBundleIdentifier"];
if ([identifier isEqualToString:kQSBundleID]) {
appDictionary = [[QSProcessMonitor sharedInstance] previousApplication];
identifier = [appDictionary objectForKey:@"NSApplicationBundleIdentifier"];
}
NSDictionary *info = [[QSReg tableNamed:@"QSProxies"] objectForKey:identifier];
NSArray *array = [info objectForKey:kQSProxyTypes];
if (!info) return [NSArray arrayWithObjects:NSStringPboardType, NSFilenamesPboardType, nil];
if (array) return array;
id provider = [QSReg getClassInstance:[info objectForKey:kQSProxyProviderClass]];
return [provider typesForProxyObject:self];
}
@end