5
* Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
8
{ Pascal Translation: Peter N Lewis, <peter@stairways.com.au>, August 2005 }
10
Modified for use with Free Pascal
12
Please report any bugs to <gpc@microbizz.nl>
23
{$setc UNIVERSAL_INTERFACES_VERSION := $0342}
24
{$setc GAP_INTERFACES_VERSION := $0210}
26
{$ifc not defined USE_CFSTR_CONSTANT_MACROS}
27
{$setc USE_CFSTR_CONSTANT_MACROS := TRUE}
30
{$ifc defined CPUPOWERPC and defined CPUI386}
31
{$error Conflicting initial definitions for CPUPOWERPC and CPUI386}
33
{$ifc defined FPC_BIG_ENDIAN and defined FPC_LITTLE_ENDIAN}
34
{$error Conflicting initial definitions for FPC_BIG_ENDIAN and FPC_LITTLE_ENDIAN}
37
{$ifc not defined __ppc__ and defined CPUPOWERPC}
42
{$ifc not defined __i386__ and defined CPUI386}
48
{$ifc defined __ppc__ and __ppc__ and defined __i386__ and __i386__}
49
{$error Conflicting definitions for __ppc__ and __i386__}
52
{$ifc defined __ppc__ and __ppc__}
53
{$setc TARGET_CPU_PPC := TRUE}
54
{$setc TARGET_CPU_X86 := FALSE}
55
{$elifc defined __i386__ and __i386__}
56
{$setc TARGET_CPU_PPC := FALSE}
57
{$setc TARGET_CPU_X86 := TRUE}
59
{$error Neither __ppc__ nor __i386__ is defined.}
61
{$setc TARGET_CPU_PPC_64 := FALSE}
63
{$ifc defined FPC_BIG_ENDIAN}
64
{$setc TARGET_RT_BIG_ENDIAN := TRUE}
65
{$setc TARGET_RT_LITTLE_ENDIAN := FALSE}
66
{$elifc defined FPC_LITTLE_ENDIAN}
67
{$setc TARGET_RT_BIG_ENDIAN := FALSE}
68
{$setc TARGET_RT_LITTLE_ENDIAN := TRUE}
70
{$error Neither FPC_BIG_ENDIAN nor FPC_LITTLE_ENDIAN are defined.}
72
{$setc ACCESSOR_CALLS_ARE_FUNCTIONS := TRUE}
73
{$setc CALL_NOT_IN_CARBON := FALSE}
74
{$setc OLDROUTINENAMES := FALSE}
75
{$setc OPAQUE_TOOLBOX_STRUCTS := TRUE}
76
{$setc OPAQUE_UPP_TYPES := TRUE}
77
{$setc OTCARBONAPPLICATION := TRUE}
78
{$setc OTKERNEL := FALSE}
79
{$setc PM_USE_SESSION_APIS := TRUE}
80
{$setc TARGET_API_MAC_CARBON := TRUE}
81
{$setc TARGET_API_MAC_OS8 := FALSE}
82
{$setc TARGET_API_MAC_OSX := TRUE}
83
{$setc TARGET_CARBON := TRUE}
84
{$setc TARGET_CPU_68K := FALSE}
85
{$setc TARGET_CPU_MIPS := FALSE}
86
{$setc TARGET_CPU_SPARC := FALSE}
87
{$setc TARGET_OS_MAC := TRUE}
88
{$setc TARGET_OS_UNIX := FALSE}
89
{$setc TARGET_OS_WIN32 := FALSE}
90
{$setc TARGET_RT_MAC_68881 := FALSE}
91
{$setc TARGET_RT_MAC_CFM := FALSE}
92
{$setc TARGET_RT_MAC_MACHO := TRUE}
93
{$setc TYPED_FUNCTION_POINTERS := TRUE}
94
{$setc TYPE_BOOL := FALSE}
95
{$setc TYPE_EXTENDED := FALSE}
96
{$setc TYPE_LONGLONG := TRUE}
97
uses MacTypes,CFBase,CFData,CFMachPort,CGBase,CGDirectDisplay,CGEventTypes,CGGeometry,CGErrors,CGRemoteOperation,CGEventSource;
101
{ Return the CFTypeID for CGEventRefs. }
102
function CGEventGetTypeID: CFTypeID; external name '_CGEventGetTypeID'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
105
* APIs provide the ability to create a NULL event, or to create specialized
106
* events reflecting a state specified as parameters to the creation functions.
109
{ Create a NULL event to be filled in. 'source' may be NULL. }
110
function CGEventCreate( source: CGEventSourceRef ): CGEventRef; external name '_CGEventCreate'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
113
* Functions to flatten and reconstruct a CGEventRef for network transport.
114
* These may be useful in remote control and helpdesk applications.
116
* Returns a CGEventRef built from the flattened data representation, or NULL
117
* if the eventData is invalid.
119
function CGEventCreateFromData( allocator: CFAllocatorRef; eventData: CFDataRef ): CGEventRef; external name '_CGEventCreateFromData';
122
* Returns a CFDataRef containing the flattened data representation of the event,
123
* or NULL if the eventData is invalid.
125
function CGEventCreateData( allocator: CFAllocatorRef; event: CGEventRef ): CFDataRef; external name '_CGEventCreateData';
128
* Create mouse events.
130
* The event source may be taken from another event, or may be NULL.
131
* mouseType should be one of the mouse event types.
132
* mouseCursorPosition should be the global coordinates the mouse is at for the event.
133
* For kCGEventOtherMouseDown, kCGEventOtherMouseDragged, and
134
* kCGEventOtherMouseUp events, the mouseButton parameter should
135
* indicate which button is changing state.
137
* The current implemementation of the event system supports a maximum of thirty-two buttons.
138
* Mouse button 0 is the primary button on the mouse. Mouse button 1 is the secondary
139
* mouse button (right). Mouse button 2 is the center button, and the remaining
140
* buttons would be in USB device order.
142
function CGEventCreateMouseEvent( source: CGEventSourceRef; mouseType: CGEventType; mouseCursorPosition: CGPoint; mouseButton: CGMouseButton ): CGEventRef; external name '_CGEventCreateMouseEvent'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
146
* Create keyboard events.
148
* The event source may be taken from another event, or may be NULL.
149
* Based on the virtual key code values entered,
150
* the appropriate key down, key up, or flags changed events are generated.
152
* All keystrokes needed to generate a character must be entered, including
153
* SHIFT, CONTROL, OPTION, and COMMAND keys. For example, to produce a 'Z',
154
* the SHIFT key must be down, the 'z' key must go down, and then the SHIFT
155
* and 'z' key must be released:
156
* CGEventCreateKeyboardEvent((CGKeyCode)56, true ); // shift down
157
* CGEventCreateKeyboardEvent( (CGKeyCode)6, true ); // 'z' down
158
* CGEventCreateKeyboardEvent( (CGKeyCode)6, false ); // 'z' up
159
* CGEventCreateKeyboardEvent( (CGKeyCode)56, false ); // 'shift up
161
function CGEventCreateKeyboardEvent( source: CGEventSourceRef; virtualKey: CGKeyCode; keyDown: CBool ): CGEventRef; external name '_CGEventCreateKeyboardEvent'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
163
function CGEventCreateCopy( event: CGEventRef ): CGEventRef; external name '_CGEventCreateCopy'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
165
* CFRetain() and CFRelease() may be used to retain and release CGEventRefs.
168
function CGEventGetSource( event: CGEventRef ): CGEventSourceRef; external name '_CGEventGetSource'; (* DEPRECATED_IN_MAC_OS_X_VERSION_10_4_AND_LATER *)
170
* These functions provide access to the event source for an event.
171
* Event filters may use these to generate events that are compatible
172
* with an event being filtered.
174
* Note that CGEventCreateSourceFromEvent may return NULL if the event
175
* was generated with a private CGEventSourceStateID owned by another
176
* process. Such events should be filtered based on the public state.
178
function CGEventCreateSourceFromEvent( event: CGEventRef ): CGEventSourceRef; external name '_CGEventCreateSourceFromEvent'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
179
procedure CGEventSetSource( event: CGEventRef; source: CGEventSourceRef ); external name '_CGEventSetSource'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
182
* The following functions will provide high level access to selected event data.
184
function CGEventGetType( event: CGEventRef ): CGEventType; external name '_CGEventGetType'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
185
procedure CGEventSetType( event: CGEventRef; typ: CGEventType ); external name '_CGEventSetType'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
187
function CGEventGetTimestamp( event: CGEventRef ): CGEventTimestamp; external name '_CGEventGetTimestamp'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
188
procedure CGEventSetTimestamp( event: CGEventRef; timestamp: CGEventTimestamp ); external name '_CGEventSetTimestamp'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
190
function CGEventGetLocation( event: CGEventRef ): CGPoint; external name '_CGEventGetLocation'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
191
procedure CGEventSetLocation( event: CGEventRef; location: CGPoint ); external name '_CGEventSetLocation'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
193
function CGEventGetFlags( event: CGEventRef ): CGEventFlags; external name '_CGEventGetFlags'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
194
procedure CGEventSetFlags( event: CGEventRef; flags: CGEventFlags ); external name '_CGEventSetFlags'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
197
* Access to UniChar data in keyboard events.
199
* There are no runtime errors associated with these functions.
200
* Use on non-keyboard events leaves the event unchanged.
201
* Note that many frameworks use a fixed length representation
202
* of a CGEvent, and so are limited to a maximum string length
203
* of 20 Unicode characters. The CGEventRef itself is limited to 65535
206
* Calling CGEventKeyboardGetUnicodeString() with a NULL unicodeString
207
* or zero maxStringLength will still return the actual count of
208
* UniCode characters in the event data.
210
procedure CGEventKeyboardGetUnicodeString( event: CGEventRef; maxStringLength: UniCharCount; var actualStringLength: UniCharCount; unicodeString: {variable-size-array} UniCharPtr ); external name '_CGEventKeyboardGetUnicodeString'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
212
procedure CGEventKeyboardSetUnicodeString( event: CGEventRef; stringLength: UniCharCount; {const} unicodeString: {variable-size-array} UniCharPtr ); external name '_CGEventKeyboardSetUnicodeString'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
216
* Additional low level functions provide access to specialized fields of the events
220
* The CGEventGetDoubleValueField and CGEventSetDoubleValueField
221
* Functions renormalize certain fixed point and integer values
222
* to an appropriate floating point range. Keys this is done for
225
* kCGMouseEventPressure pressure value in the range 0-255
226
* scaled to [0.0-1.0]
227
* kCGTabletEventPointPressure scaled pressure value; MAX=(2^16)-1, MIN=0
228
* kCGTabletEventTiltX tilt range is -((2^15)-1) to (2^15)-1 (-32767 to 32767)
229
* kCGTabletEventTiltY scaled to [0.0-1.0]
230
* kCGTabletEventTangentialPressure Same range as tilt
231
* kCGTabletEventRotation Fixed-point representation of device
232
* rotation in a 10.6 format
235
function CGEventGetIntegerValueField( event: CGEventRef; field: CGEventField ): SInt64; external name '_CGEventGetIntegerValueField'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
236
function CGEventGetDoubleValueField( event: CGEventRef; field: CGEventField ): Float64; external name '_CGEventGetDoubleValueField'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
239
* Before using the Set functions, the event type must be set
240
* properly by using the appropriate type creation function or by calling
243
* If this is to be a mouse event generated by a tablet, call
244
* CGEventSetIntegerValueField(event, kCGMouseEventSubtype) with a value of
245
* kCGEventMouseSubtypeTabletPoint or kCGEventMouseSubtypeTabletProximity
246
* before setting other parameters.
248
* The correct type and subtype must be set so that the internal
249
* event-specific data structures may be properly filled in.
251
procedure CGEventSetIntegerValueField( event: CGEventRef; field: CGEventField; value: SInt64 ); external name '_CGEventSetIntegerValueField'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
252
procedure CGEventSetDoubleValueField( event: CGEventRef; field: CGEventField; value: Float64 ); external name '_CGEventSetDoubleValueField'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
256
* Registering an Event Tap
258
* A function registers an event tap, taking a pointer to the
259
* program's tap function and an arbitrary reference to be passed
260
* to the tap function, and returning a CFMachPortRef the program
261
* can add to the appropriate run loop by creating a surce and
262
* using CFRunLoopAddSource().
264
* Taps may be placed at the point where HIDSystem events enter
265
* the server, at the point where HIDSystem and remote control
266
* events enter a session, at the point where events have been
267
* annotated to flow to a specific application, or at the point
268
* where events are delivered to the application. Taps may be
269
* inserted at a specified point at the head of pre-existing filters,
270
* or appended after any pre-existing filters.
272
* Taps may be passive event listeners, or active filters.
273
* An active filter may pass an event through unmodified, modify
274
* an event, or discard an event. When a tap is registered, it
275
* identifies the set of events to be observed with a mask, and
276
* indicates if it is a passive or active event filter. Multiple
277
* event type bitmasks may be ORed together.
279
* Taps may only be placed at kCGHIDEventTap by a process running
280
* as the root user. NULL is returned for other users.
282
* Taps placed at kCGHIDEventTap, kCGSessionEventTap,
283
* kCGAnnotatedSessionEventTap, or on a specific process may
284
* only receive key up and down events if access for assistive
285
* devices is enabled (Preferences Universal Access panel,
286
* Keyboard view). If the tap is not permitted to monitor these
287
* when the tap is being created, then the appropriate bits
288
* in the mask are cleared. If that results in an empty mask,
289
* then NULL is returned.
291
* Releasing the CFMachPortRef will release the tap.
293
* The CGEventTapProxy is an opaque reference to state within
294
* the client application associated with the tap. The tap
295
* function may pass this reference to other functions, such as
296
* the event-posting routines.
298
* The event tap callback runs from the CFRunLoop to which the
299
* tap CFMachPort is added as a source. The thread safety is defined
300
* by the CFRunLoop and it's environment.
303
{ Return the a CFMachPortRef for the event tap. }
304
function CGEventTapCreate( tap: CGEventTapLocation; place: CGEventTapPlacement; options: CGEventTapOptions; eventsOfInterest: CGEventMask; callback: CGEventTapCallBack; refcon: UnivPtr ): CFMachPortRef; external name '_CGEventTapCreate'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
307
* Events being routed to individual applications may be tapped
308
* using another function. CGEventTapCreateForPSN() will report
309
* all events being routed to the specified application.
312
{ Return the a CFMachPortRef for an event tap for the specified process. }
313
function CGEventTapCreateForPSN( processSerialNumber: { Temp type, til def moves to CoreServices } UnivPtr; place: CGEventTapPlacement; options: CGEventTapOptions; eventsOfInterest: CGEventMask; callback: CGEventTapCallBack; refcon: UnivPtr ): CFMachPortRef; external name '_CGEventTapCreateForPSN'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
316
* Enable or disable an event tap.
318
* The taps are normally enabled when created.
319
* If a tap becomes unresponsive, or a user requests taps be disabled,
320
* then an appropriate kCGEventTapDisabled... event is passed to the
321
* registered CGEventTapCallBack function.
323
* Event taps may be re-enabled by calling this function.
325
procedure CGEventTapEnable( myTap: CFMachPortRef; enable: CBool ); external name '_CGEventTapEnable'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
326
function CGEventTapIsEnabled( myTap: CFMachPortRef ): CBool; external name '_CGEventTapIsEnabled'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
329
* A function to post events from a tap is provided. The events
330
* are posted to the same points that an event returned from an
331
* event tap would be posted to. If an event tap posts new events,
332
* the new events enter the system before the event returned by
333
* the tap enters the system. This order may be changed by
334
* explicitly posting the filtered event along with new events
335
* in the desired order, and then setting the returned event
336
* *pEventOut to NULL.
338
* Events posted into the system will be seen by all taps placed
339
* after the tap posting the event.
342
{ Post an event from the event tap into the event stream. }
343
procedure CGEventTapPostEvent( proxy: CGEventTapProxy; event: CGEventRef ); external name '_CGEventTapPostEvent'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
347
* Functions to post events into the system at various points
348
* are also provided. Each event posted by these functions
349
* enters the system at a point immediately before any taps
350
* instantiated for that point, and will pass through any such taps.
352
* This mechanism permits an external process to establish an
353
* event routing policy, for example, by tapping events at the
354
* kCGAnnotatedSessionEventTap and then posting the events to
358
{ Post an event from the event tap into the event stream. }
359
procedure CGEventPost( tap: CGEventTapLocation; event: CGEventRef ); external name '_CGEventPost'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
361
{ Post an event from the event tap into the event stream for a specific application. }
362
procedure CGEventPostToPSN( processSerialNumber: { Temp type, til def moves to CoreServices } UnivPtr; event: CGEventRef ); external name '_CGEventPostToPSN'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)
365
* Mechanism used to list event taps.
366
* An array length (maxNumberOfTaps) and array of CGEventTapInformation structures
368
* Up to maxNumberOfTaps elements of the array are filled in with event tap information.
369
* The actual number of elements filled in is returned in eventTapCount.
371
* The call resets the accumulated minUsecLatency and maxUsecLatency fields. Values
372
* reported in these fields reflect the min and max values seen since the preceding call,
373
* or the instantiation of the tap.
375
* If the CGEventTapInformation array is NULL, maxNumberOfTaps is ignored, and *eventTapCount
376
* is filled in with the number of event taps that are currently installed.
378
function CGGetEventTapList( maxNumberOfTaps: CGTableCount; tapList: {variable-size-array} CGEventTapInformationPtr; var eventTapCount: CGTableCount ): CGError; external name '_CGGetEventTapList'; (* AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER *)