]> gitweb.factorcode.org Git - factor.git/blob - basis/game/input/iokit/iokit.factor
core, basis, extra: Remove DOS line endings from files.
[factor.git] / basis / game / input / iokit / iokit.factor
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
6 sequences vectors ;
7 FROM: namespaces => change-global ;
8 IN: game.input.iokit
9
10 SINGLETON: iokit-game-input-backend
11
12 SYMBOLS: +hid-manager+ +keyboard-state+ +mouse-state+ +controller-states+ ;
13
14 iokit-game-input-backend game-input-backend set-global
15
16 : make-hid-manager ( -- alien )
17     f 0 IOHIDManagerCreate ;
18
19 : set-hid-manager-matching ( alien matching-seq -- )
20     >plist IOHIDManagerSetDeviceMatchingMultiple ;
21
22 : devices-from-hid-manager ( manager -- vector )
23     [
24         IOHIDManagerCopyDevices
25         [ &CFRelease NSFastEnumeration>vector ] [ f ] if*
26     ] with-destructors ;
27
28 CONSTANT: game-devices-matching-seq
29     {
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
36     }
37
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 } }
60
61 : device-elements-matching ( device matching-hash -- vector )
62     [
63         >plist 0 IOHIDDeviceCopyMatchingElements
64         [ &CFRelease NSFastEnumeration>vector ] [ f ] if*
65     ] with-destructors ;
66
67 : button-count ( device -- button-count )
68     buttons-matching-hash device-elements-matching length ;
69
70 : ?axis ( device hash -- axis/f )
71     device-elements-matching ?first ;
72
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 ;
89
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* ;
99
100 : mouse-device? ( device -- ? )
101     1 2 IOHIDDeviceConformsTo ;
102
103 : controller-device? ( device -- ? )
104     {
105         [ 1 4 IOHIDDeviceConformsTo ]
106         [ 1 5 IOHIDDeviceConformsTo ]
107         [ 1 8 IOHIDDeviceConformsTo ]
108     } 1|| ;
109
110 : element-usage ( element -- {usage-page,usage} )
111     [ IOHIDElementGetUsagePage ] [ IOHIDElementGetUsage ] bi
112     2array ;
113
114 : button? ( element -- ? )
115     IOHIDElementGetUsagePage 9 = ; inline
116 : keyboard-key? ( element -- ? )
117     IOHIDElementGetUsagePage 7 = ; inline
118 : axis? ( element -- ? )
119     IOHIDElementGetUsagePage 1 = ; inline
120
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
139
140 CONSTANT: pov-values
141     {
142         pov-up pov-up-right pov-right pov-down-right
143         pov-down pov-down-left pov-left pov-up-left
144         pov-neutral
145     }
146
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* ;
155
156 : record-button ( state hid-value element -- )
157     [ buttons>> ] [ button-value ] [ IOHIDElementGetUsage 1 - ] tri* rot set-nth ;
158
159 : record-controller ( controller-state value -- )
160     dup IOHIDValueGetElement {
161         { [ dup button? ] [ record-button ] }
162         { [ dup axis? ] [ {
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 ] }
171             [ 3drop ]
172         } cond ] }
173         [ 3drop ]
174     } cond ;
175
176 HINTS: record-controller { controller-state alien } ;
177
178 : record-keyboard ( keyboard-state value -- )
179     dup IOHIDValueGetElement dup keyboard-key? [
180         [ IOHIDValueGetIntegerValue c-bool> ]
181         [ IOHIDElementGetUsage ] bi*
182         rot ?set-nth
183     ] [ 3drop ] if ;
184
185 HINTS: record-keyboard { bit-array alien } ;
186
187 : record-mouse ( mouse-state value -- )
188     dup IOHIDValueGetElement {
189         { [ dup button? ] [ record-button ] }
190         { [ dup axis? ] [ {
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 ] }
195             [ 3drop ]
196         } cond ] }
197         [ 3drop ]
198     } cond ;
199
200 HINTS: record-mouse { mouse-state alien } ;
201
202 M: iokit-game-input-backend read-mouse
203     +mouse-state+ get-global ;
204
205 M: iokit-game-input-backend reset-mouse
206     +mouse-state+ get-global
207         0 >>dx
208         0 >>dy
209         0 >>scroll-dx
210         0 >>scroll-dy
211         drop ;
212
213 : default-calibrate-saturation ( element -- )
214     [ kIOHIDElementMinKey kIOHIDElementCalibrationSaturationMinKey transfer-element-property ]
215     [ kIOHIDElementMaxKey kIOHIDElementCalibrationSaturationMaxKey transfer-element-property ]
216     bi ;
217
218 : default-calibrate-axis ( element -- )
219     [ kIOHIDElementCalibrationMinKey -1.0 set-element-property ]
220     [ kIOHIDElementCalibrationMaxKey 1.0 set-element-property ]
221     [ default-calibrate-saturation ]
222     tri ;
223
224 : default-calibrate-slider ( element -- )
225     [ kIOHIDElementCalibrationMinKey 0.0 set-element-property ]
226     [ kIOHIDElementCalibrationMaxKey 1.0 set-element-property ]
227     [ default-calibrate-saturation ]
228     tri ;
229
230 : (default) ( ? quot -- )
231     [ f ] if* ; inline
232
233 : <device-controller-state> ( device -- controller-state )
234     {
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 ;
245
246 : ?add-mouse-buttons ( device -- )
247     button-count +mouse-state+ get-global buttons>>
248     2dup length >
249     [ set-length ] [ 2drop ] if ;
250
251 :: (device-matched-callback) ( context result sender device -- )
252     {
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
257         ] }
258         [ ]
259     } cond ;
260
261 : device-matched-callback ( -- alien )
262     [ (device-matched-callback) ] IOHIDDeviceCallback ;
263
264 :: (device-removed-callback) ( context result sender device -- )
265     device +controller-states+ get-global delete-at ;
266
267 : device-removed-callback ( -- alien )
268     [ (device-removed-callback) ] IOHIDDeviceCallback ;
269
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 )
274     dup CFGetTypeID {
275         { [ dup IOHIDDeviceGetTypeID = ] [ drop ] }
276         { [ dup IOHIDQueueGetTypeID = ] [ drop IOHIDQueueGetDevice ] }
277         [
278             drop
279             "input callback doesn't know how to deal with "
280             swap CF>description append throw
281         ]
282     } cond ;
283
284 :: (device-input-callback) ( context result sender value -- )
285     sender get-input-device :> device
286     {
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
290         ] }
291         [ +keyboard-state+ get-global value record-keyboard ]
292     } cond ;
293
294 : device-input-callback ( -- alien )
295     [ (device-input-callback) ] IOHIDValueCallback ;
296
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 ;
303
304 M: iokit-game-input-backend (open-game-input)
305     make-hid-manager {
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 ]
312         [
313             CFRunLoopGetMain CFRunLoopDefaultMode
314             IOHIDManagerScheduleWithRunLoop
315         ]
316     } cleave ;
317
318 M: iokit-game-input-backend (reset-game-input)
319     { +hid-manager+ +keyboard-state+ +mouse-state+ +controller-states+ }
320     [ f swap set-global ] each ;
321
322 M: iokit-game-input-backend (close-game-input)
323     +hid-manager+ get-global [
324         +hid-manager+ [
325             [
326                 CFRunLoopGetMain CFRunLoopDefaultMode
327                 IOHIDManagerUnscheduleFromRunLoop
328             ]
329             [ 0 IOHIDManagerClose drop ]
330             [ CFRelease ] tri
331             f
332         ] change-global
333         f +keyboard-state+ set-global
334         f +mouse-state+ set-global
335         f +controller-states+ set-global
336     ] when ;
337
338 M: iokit-game-input-backend get-controllers ( -- sequence )
339     +controller-states+ get-global keys [ controller boa ] map ;
340
341 : ?join ( pre post sep -- string )
342     2over start [ swap 2nip ] [ [ 2array ] dip join ] if ;
343
344 M: iokit-game-input-backend product-string ( controller -- string )
345     handle>>
346     [ kIOHIDManufacturerKey device-property ]
347     [ kIOHIDProductKey      device-property ] bi " " ?join ;
348 M: iokit-game-input-backend product-id ( controller -- integer )
349     handle>>
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 ;
354
355 M: iokit-game-input-backend read-controller ( controller -- controller-state )
356     handle>> +controller-states+ get-global at clone ;
357
358 M: iokit-game-input-backend read-keyboard ( -- keyboard-state )
359     +keyboard-state+ get-global clone keyboard-state boa ;
360
361 M: iokit-game-input-backend calibrate-controller ( controller -- )
362     drop ;