1 ! Copyright (C) 2010 Erik Charlebois, William Schlieper.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data arrays kernel game.input
4 namespaces math classes bit-arrays system sequences vectors
5 x11 x11.xlib assocs generalizations ;
8 SINGLETON: x11-game-input-backend
10 x11-game-input-backend game-input-backend set-global
12 M: x11-game-input-backend (open-game-input)
15 M: x11-game-input-backend (close-game-input)
18 M: x11-game-input-backend (reset-game-input)
21 M: x11-game-input-backend get-controllers
24 M: x11-game-input-backend product-string
27 M: x11-game-input-backend product-id
30 M: x11-game-input-backend instance-id
33 M: x11-game-input-backend read-controller
34 drop controller-state new ;
36 M: x11-game-input-backend calibrate-controller
39 M: x11-game-input-backend vibrate-controller
42 HOOK: x>hid-bit-order os ( -- x )
44 M: linux x>hid-bit-order
47 0 41 30 31 32 33 34 35
48 36 37 38 39 45 46 42 43
49 20 26 8 21 23 28 24 12
50 18 19 47 48 40 224 4 22
52 52 53 225 49 29 27 6 25
53 5 17 16 54 55 56 229 85
54 226 44 57 58 59 60 61 62
55 63 64 65 66 67 83 71 95
56 96 97 86 92 93 94 87 91
59 88 228 84 70 0 0 74 82
60 75 80 79 77 81 78 73 76
61 127 129 128 102 103 0 72 0
80 : x-bits>hid-bits ( bit-array -- bit-array )
81 256 iota [ 2array ] { } 2map-as [ first ] filter values
82 x>hid-bit-order [ nth ] curry map
83 256 <bit-array> swap [ t swap pick set-nth ] each ;
85 M: x11-game-input-backend read-keyboard
86 dpy get 256 <bit-array> [ XQueryKeymap drop ] keep
87 x-bits>hid-bits keyboard-state boa ;
89 : query-pointer ( -- x y buttons )
90 dpy get dup XDefaultRootWindow
91 { int int int int int int int }
92 [ XQueryPointer drop ] with-out-parameters
97 M: x11-game-input-backend read-mouse
98 mouse-reset? get [ reset-mouse ] unless
101 swap 256 /i >>buttons
104 0 >>scroll-dy 0 >>scroll-dx ;
106 M: x11-game-input-backend reset-mouse
107 dpy get dup XDefaultRootWindow dup
108 0 0 0 0 400 400 XWarpPointer drop t mouse-reset? set-global ;