1 USING: accessors alien alien.c-types arrays assocs bit-arrays
2 cocoa.application cocoa.enumeration cocoa.plists combinators
3 combinators.short-circuit core-foundation core-foundation.data
4 core-foundation.run-loop core-foundation.strings destructors
5 game.input hints iokit iokit.hid kernel locals math namespaces
7 FROM: namespaces => change-global ;
10 SINGLETON: iokit-game-input-backend
12 SYMBOLS: +hid-manager+ +keyboard-state+ +mouse-state+ +controller-states+ ;
14 iokit-game-input-backend game-input-backend set-global
16 : make-hid-manager ( -- alien )
17 f 0 IOHIDManagerCreate ;
19 : set-hid-manager-matching ( alien matching-seq -- )
20 >plist IOHIDManagerSetDeviceMatchingMultiple ;
22 : devices-from-hid-manager ( manager -- vector )
24 IOHIDManagerCopyDevices
25 [ &CFRelease NSFastEnumeration>vector ] [ f ] if*
28 CONSTANT: game-devices-matching-seq
30 H{ { "DeviceUsage" 2 } { "DeviceUsagePage" 1 } } ! mouses
31 H{ { "DeviceUsage" 4 } { "DeviceUsagePage" 1 } } ! joysticks
32 H{ { "DeviceUsage" 5 } { "DeviceUsagePage" 1 } } ! gamepads
33 H{ { "DeviceUsage" 6 } { "DeviceUsagePage" 1 } } ! keyboards
34 H{ { "DeviceUsage" 7 } { "DeviceUsagePage" 1 } } ! keypads
35 H{ { "DeviceUsage" 8 } { "DeviceUsagePage" 1 } } ! multiaxis controllers
38 CONSTANT: buttons-matching-hash
39 H{ { "UsagePage" 9 } { "Type" 2 } }
40 CONSTANT: keys-matching-hash
41 H{ { "UsagePage" 7 } { "Type" 2 } }
42 CONSTANT: x-axis-matching-hash
43 H{ { "UsagePage" 1 } { "Usage" 0x30 } { "Type" 1 } }
44 CONSTANT: y-axis-matching-hash
45 H{ { "UsagePage" 1 } { "Usage" 0x31 } { "Type" 1 } }
46 CONSTANT: z-axis-matching-hash
47 H{ { "UsagePage" 1 } { "Usage" 0x32 } { "Type" 1 } }
48 CONSTANT: rx-axis-matching-hash
49 H{ { "UsagePage" 1 } { "Usage" 0x33 } { "Type" 1 } }
50 CONSTANT: ry-axis-matching-hash
51 H{ { "UsagePage" 1 } { "Usage" 0x34 } { "Type" 1 } }
52 CONSTANT: rz-axis-matching-hash
53 H{ { "UsagePage" 1 } { "Usage" 0x35 } { "Type" 1 } }
54 CONSTANT: slider-matching-hash
55 H{ { "UsagePage" 1 } { "Usage" 0x36 } { "Type" 1 } }
56 CONSTANT: wheel-matching-hash
57 H{ { "UsagePage" 1 } { "Usage" 0x38 } { "Type" 1 } }
58 CONSTANT: hat-switch-matching-hash
59 H{ { "UsagePage" 1 } { "Usage" 0x39 } { "Type" 1 } }
61 : device-elements-matching ( device matching-hash -- vector )
63 >plist 0 IOHIDDeviceCopyMatchingElements
64 [ &CFRelease NSFastEnumeration>vector ] [ f ] if*
67 : button-count ( device -- button-count )
68 buttons-matching-hash device-elements-matching length ;
70 : ?axis ( device hash -- axis/f )
71 device-elements-matching ?first ;
73 : ?x-axis ( device -- ? )
74 x-axis-matching-hash ?axis ;
75 : ?y-axis ( device -- ? )
76 y-axis-matching-hash ?axis ;
77 : ?z-axis ( device -- ? )
78 z-axis-matching-hash ?axis ;
79 : ?rx-axis ( device -- ? )
80 rx-axis-matching-hash ?axis ;
81 : ?ry-axis ( device -- ? )
82 ry-axis-matching-hash ?axis ;
83 : ?rz-axis ( device -- ? )
84 rz-axis-matching-hash ?axis ;
85 : ?slider ( device -- ? )
86 slider-matching-hash ?axis ;
87 : ?hat-switch ( device -- ? )
88 hat-switch-matching-hash ?axis ;
90 : device-property ( device key -- value )
91 <NSString> IOHIDDeviceGetProperty [ plist> ] [ f ] if* ;
92 : element-property ( element key -- value )
93 <NSString> IOHIDElementGetProperty [ plist> ] [ f ] if* ;
94 : set-element-property ( element key value -- )
95 [ <NSString> ] [ >plist ] bi* IOHIDElementSetProperty drop ;
96 : transfer-element-property ( element from-key to-key -- )
97 [ dupd element-property ] dip swap
98 [ set-element-property ] [ 2drop ] if* ;
100 : mouse-device? ( device -- ? )
101 1 2 IOHIDDeviceConformsTo ;
103 : controller-device? ( device -- ? )
105 [ 1 4 IOHIDDeviceConformsTo ]
106 [ 1 5 IOHIDDeviceConformsTo ]
107 [ 1 8 IOHIDDeviceConformsTo ]
110 : element-usage ( element -- {usage-page,usage} )
111 [ IOHIDElementGetUsagePage ] [ IOHIDElementGetUsage ] bi
114 : button? ( element -- ? )
115 IOHIDElementGetUsagePage 9 = ; inline
116 : keyboard-key? ( element -- ? )
117 IOHIDElementGetUsagePage 7 = ; inline
118 : axis? ( element -- ? )
119 IOHIDElementGetUsagePage 1 = ; inline
121 : x-axis? ( {usage-page,usage} -- ? )
122 IOHIDElementGetUsage 0x30 = ; inline
123 : y-axis? ( {usage-page,usage} -- ? )
124 IOHIDElementGetUsage 0x31 = ; inline
125 : z-axis? ( {usage-page,usage} -- ? )
126 IOHIDElementGetUsage 0x32 = ; inline
127 : rx-axis? ( {usage-page,usage} -- ? )
128 IOHIDElementGetUsage 0x33 = ; inline
129 : ry-axis? ( {usage-page,usage} -- ? )
130 IOHIDElementGetUsage 0x34 = ; inline
131 : rz-axis? ( {usage-page,usage} -- ? )
132 IOHIDElementGetUsage 0x35 = ; inline
133 : slider? ( {usage-page,usage} -- ? )
134 IOHIDElementGetUsage 0x36 = ; inline
135 : wheel? ( {usage-page,usage} -- ? )
136 IOHIDElementGetUsage 0x38 = ; inline
137 : hat-switch? ( {usage-page,usage} -- ? )
138 IOHIDElementGetUsage 0x39 = ; inline
142 pov-up pov-up-right pov-right pov-down-right
143 pov-down pov-down-left pov-left pov-up-left
147 : button-value ( value -- f/(0,1] )
148 IOHIDValueGetIntegerValue dup zero? [ drop f ] when ;
149 : axis-value ( value -- [-1,1] )
150 kIOHIDValueScaleTypeCalibrated IOHIDValueGetScaledValue ;
151 : mouse-axis-value ( value -- n )
152 IOHIDValueGetIntegerValue ;
153 : pov-value ( value -- pov-direction )
154 IOHIDValueGetIntegerValue pov-values ?nth [ pov-neutral ] unless* ;
156 : record-button ( state hid-value element -- )
157 [ buttons>> ] [ button-value ] [ IOHIDElementGetUsage 1 - ] tri* rot set-nth ;
159 : record-controller ( controller-state value -- )
160 dup IOHIDValueGetElement {
161 { [ dup button? ] [ record-button ] }
163 { [ dup x-axis? ] [ drop axis-value >>x drop ] }
164 { [ dup y-axis? ] [ drop axis-value >>y drop ] }
165 { [ dup z-axis? ] [ drop axis-value >>z drop ] }
166 { [ dup rx-axis? ] [ drop axis-value >>rx drop ] }
167 { [ dup ry-axis? ] [ drop axis-value >>ry drop ] }
168 { [ dup rz-axis? ] [ drop axis-value >>rz drop ] }
169 { [ dup slider? ] [ drop axis-value >>slider drop ] }
170 { [ dup hat-switch? ] [ drop pov-value >>pov drop ] }
176 HINTS: record-controller { controller-state alien } ;
178 : record-keyboard ( keyboard-state value -- )
179 dup IOHIDValueGetElement dup keyboard-key? [
180 [ IOHIDValueGetIntegerValue c-bool> ]
181 [ IOHIDElementGetUsage ] bi*
185 HINTS: record-keyboard { bit-array alien } ;
187 : record-mouse ( mouse-state value -- )
188 dup IOHIDValueGetElement {
189 { [ dup button? ] [ record-button ] }
191 { [ dup x-axis? ] [ drop mouse-axis-value [ + ] curry change-dx drop ] }
192 { [ dup y-axis? ] [ drop mouse-axis-value [ + ] curry change-dy drop ] }
193 { [ dup wheel? ] [ drop mouse-axis-value [ + ] curry change-scroll-dx drop ] }
194 { [ dup z-axis? ] [ drop mouse-axis-value [ + ] curry change-scroll-dy drop ] }
200 HINTS: record-mouse { mouse-state alien } ;
202 M: iokit-game-input-backend read-mouse
203 +mouse-state+ get-global ;
205 M: iokit-game-input-backend reset-mouse
206 +mouse-state+ get-global
213 : default-calibrate-saturation ( element -- )
214 [ kIOHIDElementMinKey kIOHIDElementCalibrationSaturationMinKey transfer-element-property ]
215 [ kIOHIDElementMaxKey kIOHIDElementCalibrationSaturationMaxKey transfer-element-property ]
218 : default-calibrate-axis ( element -- )
219 [ kIOHIDElementCalibrationMinKey -1.0 set-element-property ]
220 [ kIOHIDElementCalibrationMaxKey 1.0 set-element-property ]
221 [ default-calibrate-saturation ]
224 : default-calibrate-slider ( element -- )
225 [ kIOHIDElementCalibrationMinKey 0.0 set-element-property ]
226 [ kIOHIDElementCalibrationMaxKey 1.0 set-element-property ]
227 [ default-calibrate-saturation ]
230 : (default) ( ? quot -- )
233 : <device-controller-state> ( device -- controller-state )
235 [ ?x-axis [ default-calibrate-axis 0.0 ] (default) ]
236 [ ?y-axis [ default-calibrate-axis 0.0 ] (default) ]
237 [ ?z-axis [ default-calibrate-axis 0.0 ] (default) ]
238 [ ?rx-axis [ default-calibrate-axis 0.0 ] (default) ]
239 [ ?ry-axis [ default-calibrate-axis 0.0 ] (default) ]
240 [ ?rz-axis [ default-calibrate-axis 0.0 ] (default) ]
241 [ ?slider [ default-calibrate-slider 0.0 ] (default) ]
242 [ ?hat-switch pov-neutral and ]
243 [ button-count f <array> ]
244 } cleave controller-state boa ;
246 : ?add-mouse-buttons ( device -- )
247 button-count +mouse-state+ get-global buttons>>
249 [ set-length ] [ 2drop ] if ;
251 :: (device-matched-callback) ( context result sender device -- )
253 { [ device mouse-device? ] [ device ?add-mouse-buttons ] }
254 { [ device controller-device? ] [
255 device <device-controller-state>
256 device +controller-states+ get-global set-at
261 : device-matched-callback ( -- alien )
262 [ (device-matched-callback) ] IOHIDDeviceCallback ;
264 :: (device-removed-callback) ( context result sender device -- )
265 device +controller-states+ get-global delete-at ;
267 : device-removed-callback ( -- alien )
268 [ (device-removed-callback) ] IOHIDDeviceCallback ;
270 ! Lion sends the input callback an IOHIDQueue as the "sender".
271 ! Leopard and Snow Leopard send an IOHIDDevice.
272 ! This function gets the IOHIDDevice regardless of which is received
273 : get-input-device ( sender -- device )
275 { [ dup IOHIDDeviceGetTypeID = ] [ drop ] }
276 { [ dup IOHIDQueueGetTypeID = ] [ drop IOHIDQueueGetDevice ] }
279 "input callback doesn't know how to deal with "
280 swap CF>description append throw
284 :: (device-input-callback) ( context result sender value -- )
285 sender get-input-device :> device
287 { [ device mouse-device? ] [ +mouse-state+ get-global value record-mouse ] }
288 { [ device controller-device? ] [
289 device +controller-states+ get-global at value record-controller
291 [ +keyboard-state+ get-global value record-keyboard ]
294 : device-input-callback ( -- alien )
295 [ (device-input-callback) ] IOHIDValueCallback ;
297 : initialize-variables ( manager -- )
298 +hid-manager+ set-global
299 4 <vector> +controller-states+ set-global
300 0 0 0 0 2 <vector> mouse-state boa
301 +mouse-state+ set-global
302 256 <bit-array> +keyboard-state+ set-global ;
304 M: iokit-game-input-backend (open-game-input)
306 [ initialize-variables ]
307 [ device-matched-callback f IOHIDManagerRegisterDeviceMatchingCallback ]
308 [ device-removed-callback f IOHIDManagerRegisterDeviceRemovalCallback ]
309 [ device-input-callback f IOHIDManagerRegisterInputValueCallback ]
310 [ 0 IOHIDManagerOpen mach-error ]
311 [ game-devices-matching-seq set-hid-manager-matching ]
313 CFRunLoopGetMain CFRunLoopDefaultMode
314 IOHIDManagerScheduleWithRunLoop
318 M: iokit-game-input-backend (reset-game-input)
319 { +hid-manager+ +keyboard-state+ +mouse-state+ +controller-states+ }
320 [ f swap set-global ] each ;
322 M: iokit-game-input-backend (close-game-input)
323 +hid-manager+ get-global [
326 CFRunLoopGetMain CFRunLoopDefaultMode
327 IOHIDManagerUnscheduleFromRunLoop
329 [ 0 IOHIDManagerClose drop ]
333 f +keyboard-state+ set-global
334 f +mouse-state+ set-global
335 f +controller-states+ set-global
338 M: iokit-game-input-backend get-controllers ( -- sequence )
339 +controller-states+ get-global keys [ controller boa ] map ;
341 : ?join ( pre post sep -- string )
342 2over start [ swap 2nip ] [ [ 2array ] dip join ] if ;
344 M: iokit-game-input-backend product-string ( controller -- string )
346 [ kIOHIDManufacturerKey device-property ]
347 [ kIOHIDProductKey device-property ] bi " " ?join ;
348 M: iokit-game-input-backend product-id ( controller -- integer )
350 [ kIOHIDVendorIDKey device-property ]
351 [ kIOHIDProductIDKey device-property ] bi 2array ;
352 M: iokit-game-input-backend instance-id ( controller -- integer )
353 handle>> kIOHIDLocationIDKey device-property ;
355 M: iokit-game-input-backend read-controller ( controller -- controller-state )
356 handle>> +controller-states+ get-global at clone ;
358 M: iokit-game-input-backend read-keyboard ( -- keyboard-state )
359 +keyboard-state+ get-global clone keyboard-state boa ;
361 M: iokit-game-input-backend calibrate-controller ( controller -- )