Debian Codegraph @

3rdparty/joystick/jsLinux.cxx

raw symbols SCIP
1 /*
2 PLIB - A Suite of Portable Game Libraries
3 Copyright (C) 1998,2002 Steve Baker
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 For further information visit http://plib.sourceforge.net
20
21 $Id: jsLinux.cxx 2017 2005-02-21 07:37:25Z bram $
22 */
23
24 #include "FlightGear_js.h"
25
26 #include <unistd.h>
27 #include <linux/joystick.h>
28
29 #include <simgear/debug/logstream.hxx>
30
31
32 #if defined(JS_VERSION) && JS_VERSION >= 0x010000
33
34 #include <sys/param.h>
35 #include <fcntl.h>
36 #include <sys/ioctl.h>
37
38 struct os_specific_s {
39 js_event js ;
40 int tmp_buttons ;
41 float tmp_axes [ _JS_MAX_AXES ] ;
42 char fname [ 128 ] ;
43 int fd ;
44 };
45
46 void jsInit () {}
47
48 void jsJoystick::open ()
49 {
50 name [0] = '\0' ;
51
52 for ( int i = 0 ; i < _JS_MAX_AXES ; i++ )
53 os->tmp_axes [ i ] = 0.0f ;
54
55 os->tmp_buttons = 0 ;
56
57 os->fd = ::open ( os->fname, O_RDONLY ) ;
58
59 error = ( os->fd < 0 ) ;
60
61 if ( error )
62 return ;
63
64 /*
65 Set the correct number of axes for the linux driver
66 */
67
68 /* Melchior Franz's fixes for big-endian Linuxes since writing
69 * to the upper byte of an uninitialized word doesn't work.
70 * 9 April 2003
71 */
72 unsigned char u ;
73 ioctl ( os->fd, JSIOCGAXES , &u ) ;
74 num_axes = u ;
75 ioctl ( os->fd, JSIOCGBUTTONS, &u ) ;
76 num_buttons = u ;
77 ioctl ( os->fd, JSIOCGNAME ( sizeof(name) ), name ) ;
78 fcntl ( os->fd, F_SETFL , O_NONBLOCK ) ;
79
80 if ( num_axes > _JS_MAX_AXES )
81 num_axes = _JS_MAX_AXES ;
82
83 for ( int i = 0 ; i < _JS_MAX_AXES ; i++ )
84 {
85 max [ i ] = 32767.0f ;
86 center [ i ] = 0.0f ;
87 min [ i ] = -32767.0f ;
88 dead_band [ i ] = 0.0f ;
89 saturate [ i ] = 1.0f ;
90 }
91 }
92
93
94
95 void jsJoystick::close ()
96 {
97 if ( ! error )
98 ::close ( os->fd ) ;
99 delete os;
100 }
101
102
103 jsJoystick::jsJoystick ( int ident )
104 {
105 id = ident ;
106 os = new struct os_specific_s;
107 name [0] = '\0' ;
108
109 sprintf ( os->fname, "/dev/input/js%d", ident ) ;
110
111 if ( access ( os->fname, F_OK ) != 0 )
112 sprintf ( os->fname, "/dev/js%d", ident ) ;
113
114 open () ;
115 }
116
117
118 void jsJoystick::rawRead ( int *buttons, float *axes )
119 {
120 if ( error )
121 {
122 if ( buttons )
123 *buttons = 0 ;
124
125 if ( axes )
126 for ( int i = 0 ; i < num_axes ; i++ )
127 axes[i] = 1500.0f ;
128
129 return ;
130 }
131
132 while (1)
133 {
134 int status = ::read ( os->fd, &(os->js), sizeof(js_event) ) ;
135
136 if ( status != sizeof(js_event) )
137 {
138 /* use the old values */
139
140 if ( buttons != NULL ) *buttons = os->tmp_buttons ;
141 if ( axes != NULL )
142 memcpy ( axes, os->tmp_axes, sizeof(float) * num_axes ) ;
143
144 if ( errno == EAGAIN )
145 return ;
146
147 perror( os->fname ) ;
148 setError () ;
149 return ;
150 }
151
152 switch ( os->js.type & ~JS_EVENT_INIT )
153 {
154 case JS_EVENT_BUTTON :
155 if ( os->js.value == 0 ) /* clear the flag */
156 os->tmp_buttons &= ~(1 << os->js.number) ;
157 else
158 os->tmp_buttons |= (1 << os->js.number) ;
159 break ;
160
161 case JS_EVENT_AXIS:
162 if ( os->js.number < num_axes )
163 {
164 os->tmp_axes [ os->js.number ] = (float) os->js.value ;
165
166 if ( axes )
167 memcpy ( axes, os->tmp_axes, sizeof(float) * num_axes ) ;
168 }
169 break ;
170
171 default:
172 jsSetError ( SG_WARN, "PLIB_JS: Unrecognised /dev/js return!?!" ) ;
173
174 /* use the old values */
175
176 if ( buttons != NULL ) *buttons = os->tmp_buttons ;
177 if ( axes != NULL )
178 memcpy ( axes, os->tmp_axes, sizeof(float) * num_axes ) ;
179
180 return ;
181 }
182
183 if ( buttons != NULL )
184 *buttons = os->tmp_buttons ;
185 }
186 }
187
188 #endif