Skip to content

Commit f689002

Browse files
Tweak the state machine
I think this works better?
1 parent e724da8 commit f689002

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

macpaste.m

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <sys/time.h> // gettimeofday
2323

2424
char isDragging = 0;
25+
char debug = 0;
2526
long long prevPrevClickTime = 0;
2627
long long prevClickTime = 0;
2728
long long curClickTime = 0;
@@ -61,22 +62,24 @@ long long now() {
6162
}
6263

6364
static void logInfo(NSString *location) {
64-
// NSString *pinfo = NULL;
65-
// NSString *linfo = NULL;
66-
// NSString *cinfo = NULL;
67-
// if (pasteItems != NULL && [pasteItems count]) {
68-
// pinfo = [pasteItems[0] stringForType:NSPasteboardTypeString];
69-
// }
70-
// if (lastClipItems != NULL && [lastClipItems count]) {
71-
// linfo = [lastClipItems[0] stringForType:NSPasteboardTypeString];
72-
// }
73-
// if ([[[NSPasteboard generalPasteboard] pasteboardItems] count]) {
74-
// cinfo = [[[NSPasteboard generalPasteboard] pasteboardItems][0]
75-
// stringForType:NSPasteboardTypeString];
76-
// }
77-
// NSLog(@"%@: %ld\npaste: %@\nlastclip %@\nactual clip: %@ (%ld)", location,
78-
// lastTouchedCount, pinfo, linfo, cinfo, [[NSPasteboard
79-
// generalPasteboard] changeCount]);
65+
if (debug) {
66+
NSString *pinfo = NULL;
67+
NSString *linfo = NULL;
68+
NSString *cinfo = NULL;
69+
if (pasteItems != NULL && [pasteItems count]) {
70+
pinfo = [pasteItems[0] stringForType:NSPasteboardTypeString];
71+
}
72+
if (lastClipItems != NULL && [lastClipItems count]) {
73+
linfo = [lastClipItems[0] stringForType:NSPasteboardTypeString];
74+
}
75+
if ([[[NSPasteboard generalPasteboard] pasteboardItems] count]) {
76+
cinfo = [[[NSPasteboard generalPasteboard] pasteboardItems][0]
77+
stringForType:NSPasteboardTypeString];
78+
}
79+
NSLog(@"%@: %ld\npaste: %@\nlastclip %@ (%ld)\nactual clip: %@ (%ld)", location,
80+
lastTouchedCount, pinfo, linfo, lastClipCount, cinfo, [[NSPasteboard
81+
generalPasteboard] changeCount]);
82+
}
8083
}
8184

8285
static void paste(CGEventRef event) {
@@ -130,16 +133,16 @@ static void paste(CGEventRef event) {
130133
static void copy() {
131134
NSPasteboard *pb = [NSPasteboard generalPasteboard];
132135
lastClipCount = [pb changeCount];
133-
lastClipItems = copy_paste_items([pb pasteboardItems]);
136+
//lastClipItems = copy_paste_items([pb pasteboardItems]);
134137
// NSLog(@"SET LCI %@", [lastClipItems[0]
135138
// stringForType:NSPasteboardTypeString]);
136139
CGEventSourceRef source =
137140
CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
138141
CGEventRef kbdEventDown = CGEventCreateKeyboardEvent(source, kVK_ANSI_C, 1);
139142
CGEventRef kbdEventUp = CGEventCreateKeyboardEvent(source, kVK_ANSI_C, 0);
140143
CGEventSetFlags(kbdEventDown, kCGEventFlagMaskCommand);
141-
CGEventPost(tapA, kbdEventDown);
142-
CGEventPost(tapA, kbdEventUp);
144+
CGEventPost(tapH, kbdEventDown);
145+
CGEventPost(tapH, kbdEventUp);
143146
CFRelease(kbdEventDown);
144147
CFRelease(kbdEventUp);
145148
CFRelease(source);
@@ -173,19 +176,20 @@ static CGEventRef mouseCallback(CGEventTapProxy proxy, CGEventType type,
173176
[pasteItems release];
174177
}
175178
pasteItems = copy_paste_items([pb pasteboardItems]);
179+
lastTouchedCount = [pb changeCount];
176180

177181
// NSLog(@"WRITING %@ to clip in first step, set %@ to selection",
178182
// [lastClipItems[0] stringForType:NSPasteboardTypeString],
179183
// [pasteItems[0] stringForType:NSPasteboardTypeString]);
180-
logInfo(@"before restore");
181-
[pb clearContents];
182-
[pb writeObjects:lastClipItems];
183-
lastTouchedCount = [pb changeCount];
184-
NSMutableArray *newLastClipItems = copy_paste_items(lastClipItems);
185-
[lastClipItems release];
186-
lastClipItems = newLastClipItems;
184+
// logInfo(@"before restore");
185+
// [pb clearContents];
186+
// [pb writeObjects:lastClipItems];
187+
// lastTouchedCount = [pb changeCount];
188+
// NSMutableArray *newLastClipItems = copy_paste_items(lastClipItems);
189+
// [lastClipItems release];
190+
// lastClipItems = newLastClipItems;
187191
lastClipCount = 0;
188-
logInfo(@"after restore");
192+
// logInfo(@"after restore");
189193
}
190194
} else if (lastTouchedCount != [pb changeCount]) {
191195
// NSLog(@"@NEW clipboard?");
@@ -194,6 +198,7 @@ static CGEventRef mouseCallback(CGEventTapProxy proxy, CGEventType type,
194198
[lastClipItems release];
195199
}
196200
lastClipItems = copy_paste_items([pb pasteboardItems]);
201+
logInfo(@"Detected new clipboard");
197202
}
198203

199204
switch (type) {
@@ -209,6 +214,7 @@ static CGEventRef mouseCallback(CGEventTapProxy proxy, CGEventType type,
209214
break;
210215

211216
case kCGEventKeyDown:
217+
// NSLog(@"EVENT key: %lld (%lld)", CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode), CGEventGetFlags(event));
212218
if (CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode) ==
213219
kVK_ANSI_V &&
214220
(CGEventGetFlags(event) & kCGEventFlagMaskCommand)) {

0 commit comments

Comments
 (0)