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 assocs bit-arrays
4 game.input kernel math namespaces sequences system x11 x11.xlib ;
7 SINGLETON: x11-game-input-backend
9 x11-game-input-backend game-input-backend set-global
11 M: x11-game-input-backend (open-game-input)
14 M: x11-game-input-backend (close-game-input)
17 M: x11-game-input-backend (reset-game-input)
20 M: x11-game-input-backend get-controllers
23 M: x11-game-input-backend product-string
26 M: x11-game-input-backend product-id
29 M: x11-game-input-backend instance-id
32 M: x11-game-input-backend read-controller
33 drop controller-state new ;
35 M: x11-game-input-backend calibrate-controller
38 M: x11-game-input-backend vibrate-controller
41 HOOK: x>hid-bit-order os ( -- x )
43 M: linux x>hid-bit-order
46 0 41 30 31 32 33 34 35
47 36 37 38 39 45 46 42 43
48 20 26 8 21 23 28 24 12
49 18 19 47 48 40 224 4 22
51 52 53 225 49 29 27 6 25
52 5 17 16 54 55 56 229 85
53 226 44 57 58 59 60 61 62
54 63 64 65 66 67 83 71 95
55 96 97 86 92 93 94 87 91
58 88 228 84 70 0 0 74 82
59 75 80 79 77 81 78 73 76
60 127 129 128 102 103 0 72 0
79 : x-bits>hid-bits ( bit-array -- bit-array )
80 256 <iota> { } zip-as [ first ] filter values
81 x>hid-bit-order [ nth ] curry map
82 256 <bit-array> swap [ t swap pick set-nth ] each ;
84 M: x11-game-input-backend read-keyboard
85 dpy get 256 <bit-array> [ XQueryKeymap drop ] keep
86 x-bits>hid-bits keyboard-state boa ;
88 : query-pointer ( -- x y buttons )
89 dpy get dup XDefaultRootWindow
90 { int int int int int int int }
91 [ XQueryPointer drop ] with-out-parameters
96 M: x11-game-input-backend read-mouse
97 mouse-reset? get [ reset-mouse ] unless
100 swap 256 /i >>buttons
103 0 >>scroll-dy 0 >>scroll-dx ;
105 M: x11-game-input-backend reset-mouse
106 dpy get dup XDefaultRootWindow dup
107 0 0 0 0 400 400 XWarpPointer drop t mouse-reset? set-global ;