1 USING: arrays accessors continuations kernel math system
2 sequences namespaces init vocabs combinators ;
5 SYMBOLS: game-input-backend game-input-opened ;
7 game-input-opened [ 0 ] initialize
9 HOOK: (open-game-input) game-input-backend ( -- )
10 HOOK: (close-game-input) game-input-backend ( -- )
11 HOOK: (reset-game-input) game-input-backend ( -- )
13 HOOK: get-controllers game-input-backend ( -- sequence )
15 HOOK: product-string game-input-backend ( controller -- string )
16 HOOK: product-id game-input-backend ( controller -- id )
17 HOOK: instance-id game-input-backend ( controller -- id )
19 HOOK: read-controller game-input-backend ( controller -- controller-state )
20 HOOK: calibrate-controller game-input-backend ( controller -- )
21 HOOK: vibrate-controller game-input-backend ( controller motor1 motor2 -- )
23 HOOK: read-keyboard game-input-backend ( -- keyboard-state )
25 HOOK: read-mouse game-input-backend ( -- mouse-state )
27 HOOK: reset-mouse game-input-backend ( -- )
29 : game-input-opened? ( -- ? )
30 game-input-opened get zero? not ;
34 M: f (reset-game-input) ;
36 : reset-game-input ( -- )
39 [ reset-game-input ] "game-input" add-startup-hook
43 ERROR: game-input-not-open ;
45 : open-game-input ( -- )
49 game-input-opened [ 1 + ] change-global
51 : close-game-input ( -- )
53 dup zero? [ game-input-not-open ] when
61 : with-game-input ( quot -- )
62 open-game-input [ close-game-input ] [ ] cleanup ; inline
64 TUPLE: controller handle ;
65 TUPLE: controller-state x y z rx ry rz slider pov buttons ;
67 M: controller-state clone
68 call-next-method dup buttons>> clone >>buttons ;
72 pov-up pov-up-right pov-right pov-down-right
73 pov-down pov-down-left pov-left pov-up-left ;
75 : find-controller-products ( product-id -- sequence )
76 get-controllers [ product-id = ] with filter ;
77 : find-controller-instance ( product-id instance-id -- controller/f )
80 [ instance-id = ] bi-curry bi* and
83 TUPLE: keyboard-state keys ;
85 M: keyboard-state clone
86 call-next-method dup keys>> clone >>keys ;
88 TUPLE: mouse-state dx dy scroll-dx scroll-dy buttons ;
91 call-next-method dup buttons>> clone >>buttons ;
93 SYMBOLS: pressed released ;
95 : button-delta ( old? new? -- delta )
97 { [ 2dup xor not ] [ 2drop f ] }
98 { [ dup not ] [ 2drop released ] }
99 { [ over not ] [ 2drop pressed ] }
102 : buttons-delta-as ( old-buttons new-buttons exemplar -- delta )
103 [ button-delta ] swap 2map-as ; inline
105 : buttons-delta ( old-buttons new-buttons -- delta )
106 { } buttons-delta-as ; inline
109 { [ os windows? ] [ "game.input.dinput" require ] }
110 { [ os macosx? ] [ "game.input.iokit" require ] }
111 { [ os linux? ] [ "game.input.gtk" require ] }