]> git.mxchange.org Git - flightgear.git/blob - src/Input/jsinput.cxx
Files moved from ../Joystick to here.
[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 <jssuper.h>
22 #include <jsinput.h>
23 #include <plib/js.h>
24 #include <plib/ul.h>
25
26
27 jsInput::jsInput(jsSuper *j) {
28   jss=j;
29   pretty_display=false;
30   joystick=axis=button=-1;
31   axis_threshold=0.2;
32 }
33
34 jsInput::~jsInput(void) {}
35
36 int jsInput::getInput(void){
37       
38       bool gotit=false;
39       
40       float delta;
41       int i,current_button=0,button_bits;       
42
43       joystick=axis=button=-1;
44       
45       if(pretty_display) {
46           printf ( "+----------------------------------------------\n" ) ;
47           printf ( "| Btns " ) ;
48
49           for ( i = 0 ; i < jss->getJoystick()->getNumAxes() ; i++ )
50             printf ( "Ax:%1d ", i ) ;
51
52           for ( ; i < 8 ; i++ )
53             printf ( "     " ) ;
54
55           printf ( "|\n" ) ;
56
57           printf ( "+----------------------------------------------\n" ) ;
58       }
59       
60
61       jss->firstJoystick();
62       do {
63         jss->getJoystick()->read ( &button_iv[jss->getCurrentJoystickId()],
64                                        axes_iv[jss->getCurrentJoystickId()] ) ;
65       } while( jss->nextJoystick() );  
66       
67       
68       
69       while(!gotit) {
70         jss->firstJoystick();
71         do {
72
73           jss->getJoystick()->read ( &current_button, axes ) ;
74
75           if(pretty_display) printf ( "| %04x ", current_button ) ;
76
77                 for ( i = 0 ; i < jss->getJoystick()->getNumAxes(); i++ ) {
78
79             delta = axes[i] - axes_iv[jss->getCurrentJoystickId()][i]; 
80             if(pretty_display) printf ( "%+.1f ", delta ) ; 
81             if(!gotit) {
82               if( fabs(delta) > axis_threshold ) {
83                 gotit=true;
84                 joystick=jss->getCurrentJoystickId();
85                 axis=i;
86               } else if( current_button != 0 ) {
87                 gotit=true;
88                 joystick=jss->getCurrentJoystickId();  
89                 button_bits=current_button;
90               } 
91             }            
92           }
93                 
94           if(pretty_display) {
95             for ( ; i < 8 ; i++ )
96                     printf ( "  .  " ) ;
97           }
98
99
100         } while( jss->nextJoystick() && !gotit); 
101         if(pretty_display) {
102           printf ( "|\r" ) ;
103           fflush ( stdout ) ;
104         }
105
106         ulMilliSecondSleep(1);
107       }
108       if(button_bits != 0) {
109         for(int i=1;i<=31;i++) {
110           if( ( button_bits & (1 << i) ) > 0 ) {
111              button=i;
112              break;
113           } 
114         }    
115       } 
116 }
117