]> gitweb.factorcode.org Git - factor.git/blob - basis/game/input/x11/x11.factor
Updating code to use with-out-parameters
[factor.git] / basis / game / input / x11 / x11.factor
1 ! Copyright (C) 2010 Erik Charlebois, William Schlieper.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types arrays kernel game.input
4 namespaces math classes bit-arrays system sequences vectors
5 x11 x11.xlib assocs generalizations ;
6 IN: game.input.x11
7
8 SINGLETON: x11-game-input-backend
9
10 x11-game-input-backend game-input-backend set-global
11
12 M: x11-game-input-backend (open-game-input)
13     ;
14
15 M: x11-game-input-backend (close-game-input)
16     ;
17
18 M: x11-game-input-backend (reset-game-input)
19     ;
20
21 M: x11-game-input-backend get-controllers
22     { } ;
23
24 M: x11-game-input-backend product-string
25     drop "" ;
26      
27 M: x11-game-input-backend product-id
28     drop f ;
29      
30 M: x11-game-input-backend instance-id
31     drop f ;
32      
33 M: x11-game-input-backend read-controller
34     drop controller-state new ;
35      
36 M: x11-game-input-backend calibrate-controller
37     drop ;
38      
39 M: x11-game-input-backend vibrate-controller
40     3drop ;
41
42 HOOK: x>hid-bit-order os ( -- x )
43
44 M: linux x>hid-bit-order
45     {
46         0 0 0 0 0 0 0 0 
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 
51         7 9 10 11 13 14 15 51 
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 
57         90 89 98 99 0 0 0 68 
58         69 0 0 0 0 0 0 0 
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 
62         0 0 0 227 231 0 0 0 
63         0 0 0 0 0 0 0 0 
64         0 0 0 0 0 0 0 0 
65         0 0 0 0 0 0 0 0 
66         0 0 0 0 0 0 0 0 
67         0 0 0 0 0 0 0 0 
68         0 0 0 0 0 0 0 0 
69         0 0 0 0 0 0 0 0 
70         0 0 0 0 0 0 0 0 
71         0 0 0 0 0 0 0 0 
72         0 0 0 0 0 0 0 0 
73         0 0 0 0 0 0 0 0 
74         0 0 0 0 0 0 0 0 
75         0 0 0 0 0 0 0 0 
76         0 0 0 0 0 0 0 0 
77         0 0 0 0 0 0 0 0 
78     } ; inline
79      
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 ;
84         
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 ;
88
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
93     [ 4 ndrop ] 3dip ;
94
95 SYMBOL: mouse-reset?
96      
97 M: x11-game-input-backend read-mouse
98     mouse-reset? get [ reset-mouse ] unless
99     query-pointer
100     mouse-state new
101     swap 256 /i >>buttons
102     swap 400 - >>dy
103     swap 400 - >>dx
104     0 >>scroll-dy 0 >>scroll-dx ;
105      
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 ;