]> git.mxchange.org Git - flightgear.git/blob - src/Input/jsinput.cxx
Ralf Gerlich:
[flightgear.git] / src / Input / jsinput.cxx
1 // jsinput.cxx -- wait for and identify input from joystick
2 //
3 // Written by Tony Peden, started May 2001
4 //
5 // Copyright (C) 2001  Tony Peden (apeden@earthlink.net)
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 #include "jsinput.h"
22
23 jsInput::jsInput(jsSuper *j) {
24   jss=j;
25   pretty_display=true;
26   joystick=axis=button=-1;
27   axis_threshold=0.2;
28 }
29
30 jsInput::~jsInput(void) {}
31
32 int jsInput::getInput(){
33       
34       bool gotit=false;
35       
36       float delta;
37       int i, current_button = 0, button_bits = 0;
38
39       joystick=axis=button=-1;
40       axis_positive=false;
41       
42       if(pretty_display) {
43           printf ( "+----------------------------------------------\n" ) ;
44           printf ( "| Btns " ) ;
45
46           for ( i = 0 ; i < jss->getJoystick()->getNumAxes() ; i++ )
47             printf ( "Ax:%1d ", i ) ;
48
49           for ( ; i < 8 ; i++ )
50             printf ( "     " ) ;
51
52           printf ( "|\n" ) ;
53
54           printf ( "+----------------------------------------------\n" ) ;
55       }
56       
57
58       jss->firstJoystick();
59       do {
60         jss->getJoystick()->read ( &button_iv[jss->getCurrentJoystickId()],
61                                        axes_iv[jss->getCurrentJoystickId()] ) ;
62       } while( jss->nextJoystick() );  
63       
64       
65       
66       while(!gotit) {
67         jss->firstJoystick();
68         do {
69
70           jss->getJoystick()->read ( &current_button, axes ) ;
71
72           if(pretty_display) printf ( "| %04x ", current_button ) ;
73
74                 for ( i = 0 ; i < jss->getJoystick()->getNumAxes(); i++ ) {
75
76             delta = axes[i] - axes_iv[jss->getCurrentJoystickId()][i]; 
77             if(pretty_display) printf ( "%+.1f ", delta ) ; 
78             if(!gotit) {
79               if( fabs(delta) > axis_threshold ) {
80                 gotit=true;
81                 joystick=jss->getCurrentJoystickId();
82                 axis=i;
83                 axis_positive=(delta>0);
84               } else if( current_button != 0 ) {
85                 gotit=true;
86                 joystick=jss->getCurrentJoystickId();  
87                 button_bits=current_button;
88               } 
89             }            
90           }
91                 
92           if(pretty_display) {
93             for ( ; i < 8 ; i++ )
94                     printf ( "  .  " ) ;
95           }
96
97
98         } while( jss->nextJoystick() && !gotit); 
99         if(pretty_display) {
100           printf ( "|\r" ) ;
101           fflush ( stdout ) ;
102         }
103
104         ulMilliSecondSleep(1);
105       }
106       if(button_bits != 0) {
107         for(int i=0;i<=31;i++) {
108           if( ( button_bits & (1 << i) ) > 0 ) {
109              button=i;
110              break;
111           } 
112         }    
113       } 
114
115       return 0;
116 }
117