/
sqMacServices.c
366 lines (318 loc) · 10.9 KB
/
sqMacServices.c
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* sqMacServices.c
* SqueakServices
*
* Created by John M McIntosh on 12/06/05.
*
*/
#include "sqMacServices.h"
#include "ServicesPlugin.h"
/* The virtual machine proxy definition */
#include "sqVirtualMachine.h"
/* Configuration options */
#include "sqConfig.h"
/* Platform specific definitions */
#include "sqPlatformSpecific.h"
#include <pthread.h>
extern struct VirtualMachine *interpreterProxy;
OSType MyAppsDataTypes[64];
long MyAppsDataTypesSize;
OSType myEventServiceCopyOSType = kScrapFlavorTypeText;
OSType myEventServicePasteOSType = kScrapFlavorTypeText;
OSType myEventServicePerformOSType = kScrapFlavorTypeText;
char * myTextStringOut=NULL;
long myTextStringOutLength=0;
char * myTextStringIn=NULL;
long myTextStringInLength=0;
ScrapRef specificScrap=NULL;
long mySemaphore=0;
Boolean gWaiting=false;
EventHandlerRef myEventRef=NULL;
EventTypeSpec serviceEventList[] = {
{ kEventClassService, kEventServiceGetTypes },
{ kEventClassService, kEventServicePerform },
{ kEventClassService, kEventServiceCopy },
{ kEventClassService, kEventServicePaste } };
static pascal OSStatus MyServicesEventHandler (EventHandlerCallRef myHandlerChain,EventRef event, void* userData);
void makeItHideLogic();
void waitAFewMilliseconds();
int sqServicesInitialize() {
static Boolean initialized=false;
if (initialized)
return 1;
else
initialized = true;
InstallApplicationEventHandler(NewEventHandlerUPP(MyServicesEventHandler), GetEventTypeCount(serviceEventList), serviceEventList, 0, &myEventRef);
return 1;
}
int sqServicesShutdown() {
if (myEventRef)
RemoveEventHandler(myEventRef);
myEventRef = NULL;
myTextStringInLength = 0;
myTextStringOutLength = 0;
if (myTextStringIn) {
free(myTextStringIn);
myTextStringIn = NULL;
}
if (myTextStringOut) {
free(myTextStringOut);
myTextStringOut = NULL;
}
return 1;
}
void sqServicesSetDataTypeslength(char *dataTypes, long dataTypesLength) {
if (dataTypesLength > 256)
return;
MyAppsDataTypesSize = dataTypesLength;
memcpy(MyAppsDataTypes,dataTypes,MyAppsDataTypesSize);
}
void sqServicesSetSemaphore(int semi){
mySemaphore = semi;
}
void sqServicesSetTextStringlength(char *aTextString, int stringLength){
if (myTextStringOut != NULL)
free(myTextStringOut);
myTextStringOutLength = stringLength;
myTextStringOut = malloc(myTextStringOutLength+1);
memcpy(myTextStringOut,aTextString,myTextStringOutLength);
myTextStringOut[myTextStringOutLength] = 0x00;
}
void sqServicesSetReturnTextStringlength(char *aTextString, int stringLength){
if(specificScrap == NULL)
return;
ClearScrap (&specificScrap);
PutScrapFlavor (specificScrap, kScrapFlavorTypeText,kScrapFlavorMaskNone,stringLength,aTextString);
gWaiting = false;
specificScrap = NULL;
}
void sqServicesSetByteslengthosType(char *aByteArray, long dataTypesLength, char *aOSTypeString) {
OSType typeToUse;
if(specificScrap == NULL)
return;
typeToUse = CFSwapInt32HostToBig((int) *(int *) aOSTypeString);
ClearScrap (&specificScrap);
PutScrapFlavor (specificScrap, typeToUse ,kScrapFlavorMaskNone,dataTypesLength,aByteArray);
gWaiting = false;
specificScrap = NULL;
}
void sqServicesSetCopyOSTypeString(char *aOSTypeString) {
myEventServiceCopyOSType = CFSwapInt32HostToBig((int) *(int *) aOSTypeString);
}
void sqServicesSetPasteOSTypeString(char *aOSTypeString) {
myEventServicePasteOSType = CFSwapInt32HostToBig((int) *(int *) aOSTypeString);
}
void sqServicesSetPerformOSTypeString(char *aOSTypeString) {
myEventServicePerformOSType = CFSwapInt32HostToBig((int) *(int *)aOSTypeString);
}
int sqServicesGetTextStringLength(void){
return myTextStringInLength;
}
void sqServicesGetTextStringInto(char *buffer) {
memcpy(buffer,myTextStringIn,myTextStringInLength);
}
static pascal OSStatus MyServicesEventHandler (EventHandlerCallRef myHandlerChain,
EventRef event, void* userData)
{
UInt32 whatHappened;
OSStatus result = eventNotHandledErr; /* report failure by default */
whatHappened = GetEventKind(event);
switch (whatHappened)
{
case kEventServiceGetTypes:
{
Boolean textSelection = true;
CFMutableArrayRef copyTypes, pasteTypes;
short index, count;
if (textSelection) {
GetEventParameter (event,
kEventParamServiceCopyTypes,
typeCFMutableArrayRef,
NULL,
sizeof (CFMutableArrayRef),
NULL,
©Types);
}
GetEventParameter (event,
kEventParamServicePasteTypes,
typeCFMutableArrayRef,
NULL,
sizeof (CFMutableArrayRef),
NULL,
&pasteTypes);
count = MyAppsDataTypesSize / sizeof (OSType);
for ( index = 0; index < count; index++ ) {
CFStringRef type =
CreateTypeStringWithOSType (CFSwapInt32HostToBig(MyAppsDataTypes [index]));
if (type) {
if (textSelection)
CFArrayAppendValue (copyTypes, type);
CFArrayAppendValue (pasteTypes, type);
CFRelease (type);
}
}
result = noErr;
}
break;
case kEventServiceCopy:
{
OSStatus status = noErr;
ScrapRef scrap;
status = GetEventParameter (event,
kEventParamScrapRef,
typeScrapRef,
NULL,
sizeof (ScrapRef),
NULL,
&scrap );
if (status != noErr) return result;
status = ClearScrap (&scrap);
if (status != noErr) return result;
status = PutScrapFlavor (scrap,
myEventServiceCopyOSType,
kScrapFlavorMaskNone,
myTextStringOutLength,
myTextStringOut);
if (status != noErr) return result;
result = status;
}
break;
case kEventServicePaste:
{
OSStatus status = noErr;
ScrapRef scrap;
status = GetEventParameter (event,
kEventParamScrapRef,
typeScrapRef,
NULL,
sizeof (ScrapRef),
NULL,
&scrap);
if (status != noErr) return result;
status = GetScrapFlavorSize (scrap, myEventServicePasteOSType, &myTextStringInLength);
if (status != noErr) return result;
if (myTextStringIn)
free(myTextStringIn);
myTextStringIn = malloc (myTextStringInLength);
status = GetScrapFlavorData (scrap, myEventServicePasteOSType, &myTextStringInLength, myTextStringIn );
result = status;
}
break;
case kEventServicePerform:
{
OSStatus status = noErr;
CFStringRef message;
status = GetEventParameter (event,
kEventParamServiceMessageName,
typeCFStringRef,
NULL,
sizeof (CFStringRef),
NULL,
&message);
if (status != noErr) return result;
status = GetEventParameter (event,
kEventParamScrapRef,
typeScrapRef,
NULL,
sizeof (ScrapRef),
NULL,
&specificScrap);
if (status != noErr) return result;
status = GetScrapFlavorSize (specificScrap, myEventServicePerformOSType, &myTextStringInLength);
if (status != noErr) return result;
if (myTextStringIn)
free(myTextStringIn);
myTextStringIn = malloc (myTextStringInLength);
status = GetScrapFlavorData (specificScrap, myEventServicePerformOSType, &myTextStringInLength, myTextStringIn );
if (status != noErr) return result;
if (mySemaphore)
interpreterProxy->signalSemaphoreWithIndex(mySemaphore);
gWaiting = true;
while(gWaiting)
waitAFewMilliseconds();
makeItHideLogic();
result = status;
}
break;
default:
break;
}
return result;
}
EventLoopTimerRef ghideStupidApplication;
Boolean ghideStupidApplicationNow=false;
static pascal void hideStupidApplication (EventLoopTimerRef theTimer,void* userData);
static pascal void hideStupidApplication (EventLoopTimerRef theTimer,void* userData) {
if (ghideStupidApplicationNow ) {
ProcessSerialNumber psn = { 0, kCurrentProcess };
if (IsProcessVisible(&psn))
ShowHideProcess( &psn, false );
ghideStupidApplicationNow = false;
RemoveEventLoopTimer(ghideStupidApplication);
}
}
void makeItHideLogic(int foo)
{
InstallEventLoopTimer (GetMainEventLoop(),
250*kEventDurationMillisecond,
kEventDurationMillisecond,
NewEventLoopTimerUPP(hideStupidApplication),
NULL,&ghideStupidApplication);
ghideStupidApplicationNow = true;
}
pthread_mutex_t sleepLock;
pthread_cond_t sleepLockCondition;
void waitAFewMilliseconds()
{
static Boolean doInitialization=true;
const int realTimeToWait = 16;
struct timespec tspec;
int err;
if (doInitialization) {
doInitialization = false;
pthread_mutex_init(&sleepLock, NULL);
pthread_cond_init(&sleepLockCondition,NULL);
}
tspec.tv_sec= realTimeToWait / 1000;
tspec.tv_nsec= (realTimeToWait % 1000)*1000000;
err = pthread_mutex_lock(&sleepLock);
err = pthread_cond_timedwait_relative_np(&sleepLockCondition,&sleepLock,&tspec);
err = pthread_mutex_unlock(&sleepLock);
}
/* PasteboardRef pasteboard;
PasteboardItemID item;
CFDataRef sourceData;
CFIndex sourceSize;
CFMutableDataRef returnData = NULL;
const UInt8* sourceBytes;
UInt8* returnBytes;
CFIndex i;
status = GetEventParameter( event, kEventParamPasteboardRef, typePasteboardRef,
NULL, sizeof( pasteboard ), NULL, &pasteboard );
if (status != noErr) return result;
PasteboardSynchronize(pasteboard);
status = PasteboardGetItemIdentifier( pasteboard, 1, &item );
status = PasteboardCopyItemFlavorData( pasteboard, item, CFSTR("com.apple.traditional-mac-plain-text"), &sourceData );
sourceSize = CFDataGetLength( sourceData );
// create the translation's return data storage
returnData = CFDataCreateMutable( kCFAllocatorDefault, sourceSize );
sourceBytes = CFDataGetBytePtr( sourceData );
returnBytes = CFDataGetMutableBytePtr( returnData );
for(i = 0; i< sourceSize; i++ )
returnBytes[i] = ( UInt8 )toupper( sourceBytes[ i ] );
status = PasteboardClear( pasteboard );
status = PasteboardPutItemFlavor( pasteboard, item, CFSTR("com.apple.traditional-mac-plain-text"), returnData, kPasteboardFlavorNoFlags );
PasteboardSynchronize(pasteboard);
*/
/*
status = GetScrapFlavorSize (specificScrap, kScrapFlavorTypeText, &myTextStringInLength);
if (status != noErr) return result;
if (myTextStringIn)
free(myTextStringIn);
myTextStringIn = malloc (myTextStringInLength);
status = GetScrapFlavorData (specificScrap, kScrapFlavorTypeText, &myTextStringInLength, myTextStringIn );
if (status != noErr) return result;
ClearScrap (&specificScrap);
myTextStringIn[0] = '!';
status = PutScrapFlavor (specificScrap,kScrapFlavorTypeText,kScrapFlavorMaskNone,myTextStringInLength,myTextStringIn);
*/