1 ! Copyright (C) 2010 Erik Charlebois, William Schlieper.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data alien.syntax arrays
4 assocs bit-arrays destructors game.input gdk.ffi
5 io.encodings.binary io.files kernel linux.input-events
6 linux.input-events.ffi math namespaces sequences
7 system unix.ffi x11.xlib ;
10 SINGLETON: gtk-game-input-backend
12 gtk-game-input-backend game-input-backend set-global
15 FUNCTION: Display* gdk_x11_display_get_xdisplay ( GdkDisplay* display )
17 TUPLE: linux-controller < controller path meta state thread fd buttons quit? ;
18 : <linux-controller> ( path -- controller )
21 dup path>> get-event-device-info >>meta
22 dup meta>> "path" of binary <file-reader> handle>> fd>> >>fd
24 dup meta>> "capabilities" of EV_KEY of keys seq>explode-positions >>buttons ; inline
25 ! tuck state>> '[ _ _ read-events ] in-thread ;
27 M: linux-controller dispose* fd>> close drop ;
29 TUPLE: linux-controller-state < controller-state abs ;
32 gdk_display_get_default [ gdk_x11_display_get_xdisplay ] [
33 "No default display." throw
36 M: gtk-game-input-backend (open-game-input)
39 M: gtk-game-input-backend (close-game-input)
42 M: gtk-game-input-backend (reset-game-input)
45 M: gtk-game-input-backend get-controllers
46 get-input-events-joysticks values [ <linux-controller> ] map ;
48 M: gtk-game-input-backend product-string
51 M: gtk-game-input-backend product-id
54 M: gtk-game-input-backend instance-id
57 M: gtk-game-input-backend read-controller
58 [ linux-controller-state new ] dip
59 [ fd>> ] [ meta>> ] bi
60 [ drop evdev-get-key seq>explode-positions [ <INPUT_KEY> ] zip-with >>buttons ]
61 [ "capabilities" of EV_ABS of [ [ first first evdev-get-abs ] [ first ] bi swap 2array ] with map >>abs ] 2bi ;
63 M: gtk-game-input-backend calibrate-controller
66 M: gtk-game-input-backend vibrate-controller
69 HOOK: x>hid-bit-order os ( -- x )
71 M: linux x>hid-bit-order
74 0 41 30 31 32 33 34 35
75 36 37 38 39 45 46 42 43
76 20 26 8 21 23 28 24 12
77 18 19 47 48 40 224 4 22
79 52 53 225 49 29 27 6 25
80 5 17 16 54 55 56 229 85
81 226 44 57 58 59 60 61 62
82 63 64 65 66 67 83 71 95
83 96 97 86 92 93 94 87 91
86 88 228 84 70 0 0 74 82
87 75 80 79 77 81 78 73 76
88 127 129 128 102 103 0 72 0
107 : x-bits>hid-bits ( bit-array -- bit-array )
108 256 <iota> zip [ first ] filter values
109 x>hid-bit-order [ nth ] curry map
110 256 <bit-array> swap [ t swap pick set-nth ] each ;
112 M: gtk-game-input-backend read-keyboard
113 get-dpy 256 <bit-array> [ XQueryKeymap drop ] keep
114 x-bits>hid-bits keyboard-state boa ;
116 : query-pointer ( -- x y buttons )
117 get-dpy dup XDefaultRootWindow
118 { int int int int int int int }
119 [ XQueryPointer drop ] with-out-parameters
122 M: gtk-game-input-backend read-mouse
125 swap 256 /i >>buttons
128 0 >>scroll-dy 0 >>scroll-dx ;
130 M: gtk-game-input-backend reset-mouse
131 get-dpy dup XDefaultRootWindow dup
132 0 0 0 0 400 400 XWarpPointer drop ;