]> gitweb.factorcode.org Git - factor.git/blob - basis/game/input/dinput/dinput.factor
f03147205fa94607e1823d9afde08cba670af6b2
[factor.git] / basis / game / input / dinput / dinput.factor
1 USING: accessors alien alien.c-types alien.strings arrays
2 assocs byte-arrays combinators combinators.short-circuit
3 continuations game.input game.input.dinput.keys-array
4 io.encodings.utf16 io.encodings.utf16n kernel locals math
5 math.bitwise math.rectangles namespaces parser sequences
6 shuffle specialized-arrays ui.backend.windows vectors
7 windows.com windows.dinput windows.dinput.constants
8 windows.errors windows.kernel32 windows.messages
9 windows.ole32 windows.user32 classes.struct alien.data ;
10 SPECIALIZED-ARRAY: DIDEVICEOBJECTDATA
11 IN: game.input.dinput
12
13 CONSTANT: MOUSE-BUFFER-SIZE 16
14
15 SINGLETON: dinput-game-input-backend
16
17 dinput-game-input-backend game-input-backend set-global
18
19 SYMBOLS: +dinput+ +keyboard-device+ +keyboard-state+
20     +controller-devices+ +controller-guids+
21     +device-change-window+ +device-change-handle+
22     +mouse-device+ +mouse-state+ +mouse-buffer+ ;
23
24 : create-dinput ( -- )
25     f GetModuleHandle DIRECTINPUT_VERSION IDirectInput8W-iid
26     f <void*> [ f DirectInput8Create ole32-error ] keep *void*
27     +dinput+ set-global ;
28
29 : delete-dinput ( -- )
30     +dinput+ [ com-release f ] change-global ;
31
32 : device-for-guid ( guid -- device )
33     +dinput+ get swap f <void*>
34     [ f IDirectInput8W::CreateDevice ole32-error ] keep *void* ;
35
36 : set-coop-level ( device -- )
37     +device-change-window+ get DISCL_BACKGROUND DISCL_NONEXCLUSIVE bitor
38     IDirectInputDevice8W::SetCooperativeLevel ole32-error ;
39
40 : set-data-format ( device format-symbol -- )
41     get IDirectInputDevice8W::SetDataFormat ole32-error ;
42
43 : <buffer-size-diprop> ( size -- DIPROPDWORD )
44     DIPROPDWORD <struct> [
45         diph>>
46         DIPROPDWORD heap-size  >>dwSize
47         DIPROPHEADER heap-size >>dwHeaderSize
48         0           >>dwObj
49         DIPH_DEVICE >>dwHow
50         drop
51     ] keep swap >>dwData ;
52
53 : set-buffer-size ( device size -- )
54     DIPROP_BUFFERSIZE swap <buffer-size-diprop>
55     IDirectInputDevice8W::SetProperty ole32-error ;
56
57 : configure-keyboard ( keyboard -- )
58     [ c_dfDIKeyboard_HID set-data-format ] [ set-coop-level ] bi ;
59 : configure-mouse ( mouse -- )
60     [ c_dfDIMouse2 set-data-format ]
61     [ MOUSE-BUFFER-SIZE set-buffer-size ]
62     [ set-coop-level ] tri ;
63 : configure-controller ( controller -- )
64     [ c_dfDIJoystick2 set-data-format ] [ set-coop-level ] bi ;
65
66 : find-keyboard ( -- )
67     GUID_SysKeyboard device-for-guid
68     [ configure-keyboard ]
69     [ +keyboard-device+ set-global ] bi
70     256 <byte-array> 256 <keys-array> keyboard-state boa
71     +keyboard-state+ set-global ;
72
73 : find-mouse ( -- )
74     GUID_SysMouse device-for-guid
75     [ configure-mouse ] [ +mouse-device+ set-global ] bi
76     0 0 0 0 8 f <array> mouse-state boa +mouse-state+ set-global
77     MOUSE-BUFFER-SIZE <DIDEVICEOBJECTDATA-array> +mouse-buffer+ set-global ;
78
79 : device-info ( device -- DIDEVICEIMAGEINFOW )
80     DIDEVICEINSTANCEW <struct>
81         DIDEVICEINSTANCEW heap-size >>dwSize
82     [ IDirectInputDevice8W::GetDeviceInfo ole32-error ] keep ; inline
83 : device-caps ( device -- DIDEVCAPS )
84     DIDEVCAPS <struct>
85         DIDEVCAPS heap-size >>dwSize
86     [ IDirectInputDevice8W::GetCapabilities ole32-error ] keep ; inline
87
88 : device-guid ( device -- guid )
89     device-info guidInstance>> ; inline
90
91 : device-attached? ( device -- ? )
92     +dinput+ get swap device-guid
93     IDirectInput8W::GetDeviceStatus S_OK = ;
94
95 : find-device-axes-callback ( -- alien )
96     [ ! ( lpddoi pvRef -- BOOL )
97         [ DIDEVICEOBJECTINSTANCEW memory>struct ] dip
98         +controller-devices+ get at
99         swap guidType>> {
100             { [ dup GUID_XAxis = ] [ drop 0.0 >>x ] }
101             { [ dup GUID_YAxis = ] [ drop 0.0 >>y ] }
102             { [ dup GUID_ZAxis = ] [ drop 0.0 >>z ] }
103             { [ dup GUID_RxAxis = ] [ drop 0.0 >>rx ] }
104             { [ dup GUID_RyAxis = ] [ drop 0.0 >>ry ] }
105             { [ dup GUID_RzAxis = ] [ drop 0.0 >>rz ] }
106             { [ dup GUID_Slider = ] [ drop 0.0 >>slider ] }
107             [ drop ]
108         } cond drop
109         DIENUM_CONTINUE
110     ] LPDIENUMDEVICEOBJECTSCALLBACKW ;
111
112 : find-device-axes ( device controller-state -- controller-state )
113     swap [ +controller-devices+ get set-at ] 2keep
114     find-device-axes-callback over DIDFT_AXIS
115     IDirectInputDevice8W::EnumObjects ole32-error ;
116
117 : controller-state-template ( device -- controller-state )
118     controller-state new
119     over device-caps
120     [ dwButtons>> f <array> >>buttons ]
121     [ dwPOVs>> zero? f pov-neutral ? >>pov ] bi
122     find-device-axes ;
123
124 : device-known? ( guid -- ? )
125     +controller-guids+ get key? ; inline
126
127 : (add-controller) ( guid -- )
128     device-for-guid {
129         [ configure-controller ]
130         [ controller-state-template ]
131         [ dup device-guid clone +controller-guids+ get set-at ]
132         [ +controller-devices+ get set-at ]
133     } cleave ;
134
135 : add-controller ( guid -- )
136     dup device-known? [ drop ] [ (add-controller) ] if ;
137
138 : remove-controller ( device -- )
139     [ +controller-devices+ get delete-at ]
140     [ device-guid +controller-guids+ get delete-at ]
141     [ com-release ] tri ;
142
143 : find-controller-callback ( -- alien )
144     [ ! ( lpddi pvRef -- BOOL )
145         drop DIDEVICEINSTANCEW memory>struct guidInstance>> add-controller
146         DIENUM_CONTINUE
147     ] LPDIENUMDEVICESCALLBACKW ; inline
148
149 : find-controllers ( -- )
150     +dinput+ get DI8DEVCLASS_GAMECTRL find-controller-callback
151     f DIEDFL_ATTACHEDONLY IDirectInput8W::EnumDevices ole32-error ;
152
153 : set-up-controllers ( -- )
154     4 <vector> +controller-devices+ set-global
155     4 <vector> +controller-guids+ set-global
156     find-controllers ;
157
158 : find-and-remove-detached-devices ( -- )
159     +controller-devices+ get keys
160     [ device-attached? not ] filter
161     [ remove-controller ] each ;
162
163 : ?device-interface ( dbt-broadcast-hdr -- ? )
164     dup dbch_devicetype>> DBT_DEVTYP_DEVICEINTERFACE =
165     [ >c-ptr DEV_BROADCAST_DEVICEW memory>struct ]
166     [ drop f ] if ; inline
167
168 : device-arrived ( dbt-broadcast-hdr -- )
169     ?device-interface [ find-controllers ] when ; inline
170
171 : device-removed ( dbt-broadcast-hdr -- )
172     ?device-interface [ find-and-remove-detached-devices ] when ; inline
173
174 : <DEV_BROADCAST_HDR> ( wParam -- struct )
175     <alien> DEV_BROADCAST_HDR memory>struct ;
176
177 : handle-wm-devicechange ( hWnd uMsg wParam lParam -- )
178     [ 2drop ] 2dip swap {
179         { [ dup DBT_DEVICEARRIVAL = ]         [ drop <DEV_BROADCAST_HDR> device-arrived ] }
180         { [ dup DBT_DEVICEREMOVECOMPLETE = ]  [ drop <DEV_BROADCAST_HDR> device-removed ] }
181         [ 2drop ]
182     } cond ;
183
184 TUPLE: window-rect < rect window-loc ;
185 : <zero-window-rect> ( -- window-rect )
186     window-rect new
187     { 0 0 } >>window-loc
188     { 0 0 } >>loc
189     { 0 0 } >>dim ;
190
191 : (device-notification-filter) ( -- DEV_BROADCAST_DEVICEW )
192     DEV_BROADCAST_DEVICEW <struct>
193         DEV_BROADCAST_DEVICEW heap-size >>dbcc_size
194         DBT_DEVTYP_DEVICEINTERFACE >>dbcc_devicetype ;
195
196 : create-device-change-window ( -- )
197     <zero-window-rect> WS_OVERLAPPEDWINDOW 0 create-window
198     [
199         (device-notification-filter)
200         DEVICE_NOTIFY_WINDOW_HANDLE DEVICE_NOTIFY_ALL_INTERFACE_CLASSES bitor
201         RegisterDeviceNotification
202         +device-change-handle+ set-global
203     ]
204     [ +device-change-window+ set-global ] bi ;
205
206 : close-device-change-window ( -- )
207     +device-change-handle+ [ UnregisterDeviceNotification drop f ] change-global
208     +device-change-window+ [ DestroyWindow win32-error=0/f f ] change-global ;
209
210 : add-wm-devicechange ( -- )
211     [ 4dup handle-wm-devicechange DefWindowProc ]
212     WM_DEVICECHANGE add-wm-handler ;
213
214 : remove-wm-devicechange ( -- )
215     WM_DEVICECHANGE wm-handlers get-global delete-at ;
216
217 : release-controllers ( -- )
218     +controller-devices+ [ [ drop com-release ] assoc-each f ] change-global
219     f +controller-guids+ set-global ;
220
221 : release-keyboard ( -- )
222     +keyboard-device+ [ com-release f ] change-global
223     f +keyboard-state+ set-global ;
224
225 : release-mouse ( -- )
226     +mouse-device+ [ com-release f ] change-global
227     f +mouse-state+ set-global ;
228
229 M: dinput-game-input-backend (open-game-input)
230     create-dinput
231     create-device-change-window
232     find-keyboard
233     find-mouse
234     set-up-controllers
235     add-wm-devicechange ;
236
237 M: dinput-game-input-backend (close-game-input)
238     remove-wm-devicechange
239     release-controllers
240     release-mouse
241     release-keyboard
242     close-device-change-window
243     delete-dinput ;
244
245 M: dinput-game-input-backend (reset-game-input)
246     global [
247         {
248             +dinput+ +keyboard-device+ +keyboard-state+
249             +controller-devices+ +controller-guids+
250             +device-change-window+ +device-change-handle+
251         } [ off ] each
252     ] bind ;
253
254 M: dinput-game-input-backend get-controllers
255     +controller-devices+ get
256     [ drop controller boa ] { } assoc>map ;
257
258 M: dinput-game-input-backend product-string
259     handle>> device-info tszProductName>>
260     utf16n alien>string ;
261
262 M: dinput-game-input-backend product-id
263     handle>> device-info guidProduct>> ;
264 M: dinput-game-input-backend instance-id
265     handle>> device-guid ;
266
267 :: with-acquisition ( device acquired-quot succeeded-quot failed-quot -- result/f )
268     device { [ ] [ IDirectInputDevice8W::Acquire succeeded? ] } 1&& [
269         device acquired-quot call
270         succeeded-quot call
271     ] failed-quot if ; inline
272
273 CONSTANT: pov-values
274     {
275         pov-up pov-up-right pov-right pov-down-right
276         pov-down pov-down-left pov-left pov-up-left
277     }
278
279 : >axis ( long -- float )
280     32767 - 32767.0 /f ; inline
281 : >slider ( long -- float )
282     65535.0 /f ; inline
283 : >pov ( long -- symbol )
284     dup HEX: FFFF bitand HEX: FFFF =
285     [ drop pov-neutral ]
286     [ 2750 + 4500 /i pov-values nth ] if ; inline
287
288 : (fill-if) ( controller-state DIJOYSTATE2 ? quot -- )
289     [ drop ] compose [ 2drop ] if ; inline
290
291 : fill-controller-state ( controller-state DIJOYSTATE2 -- controller-state )
292     {
293         [ over x>> [ lX>> >axis >>x ] (fill-if) ]
294         [ over y>> [ lY>> >axis >>y ] (fill-if) ]
295         [ over z>> [ lZ>> >axis >>z ] (fill-if) ]
296         [ over rx>> [ lRx>> >axis >>rx ] (fill-if) ]
297         [ over ry>> [ lRy>> >axis >>ry ] (fill-if) ]
298         [ over rz>> [ lRz>> >axis >>rz ] (fill-if) ]
299         [ over slider>> [ rglSlider>> first >slider >>slider ] (fill-if) ]
300         [ over pov>> [ rgdwPOV>> first >pov >>pov ] (fill-if) ]
301         [ rgbButtons>> over buttons>> length <keys-array> >>buttons ]
302     } 2cleave ;
303
304 : read-device-buffer ( device buffer count -- buffer count' )
305     [ DIDEVICEOBJECTDATA heap-size ] 2dip <uint>
306     [ 0 IDirectInputDevice8W::GetDeviceData ole32-error ] 2keep *uint ;
307
308 : (fill-mouse-state) ( state DIDEVICEOBJECTDATA -- state )
309     [ dwData>> 32 >signed ] [ dwOfs>> ] bi {
310         { DIMOFS_X [ [ + ] curry change-dx ] }
311         { DIMOFS_Y [ [ + ] curry change-dy ] }
312         { DIMOFS_Z [ [ + ] curry change-scroll-dy ] }
313         [ [ c-bool> ] [ DIMOFS_BUTTON0 - ] bi* rot [ buttons>> set-nth ] keep ]
314     } case ;
315
316 : fill-mouse-state ( buffer count -- state )
317     [ +mouse-state+ get ] 2dip swap [ nth (fill-mouse-state) ] curry each ;
318
319 : get-device-state ( device DIJOYSTATE2 -- )
320     [ dup IDirectInputDevice8W::Poll ole32-error ] dip
321     [ byte-length ] keep
322     IDirectInputDevice8W::GetDeviceState ole32-error ;
323
324 : (read-controller) ( handle template -- state )
325     swap [ DIJOYSTATE2 <struct> [ get-device-state ] keep ]
326     [ fill-controller-state ] [ drop f ] with-acquisition ;
327
328 M: dinput-game-input-backend read-controller
329     handle>> dup +controller-devices+ get at
330     [ (read-controller) ] [ drop f ] if* ;
331
332 M: dinput-game-input-backend calibrate-controller
333     handle>> f 0 IDirectInputDevice8W::RunControlPanel ole32-error ;
334
335 M: dinput-game-input-backend read-keyboard
336     +keyboard-device+ get
337     [ +keyboard-state+ get [ keys>> underlying>> get-device-state ] keep ]
338     [ ] [ f ] with-acquisition ;
339
340 M: dinput-game-input-backend read-mouse
341     +mouse-device+ get [ +mouse-buffer+ get MOUSE-BUFFER-SIZE read-device-buffer ]
342     [ fill-mouse-state ] [ f ] with-acquisition ;
343
344 M: dinput-game-input-backend reset-mouse
345     +mouse-device+ get [ f MOUSE-BUFFER-SIZE read-device-buffer ]
346     [ 2drop ] [ ] with-acquisition
347     +mouse-state+ get
348         0 >>dx
349         0 >>dy
350         0 >>scroll-dx
351         0 >>scroll-dy
352         drop ;