Commit | Line | Data |
bfd90409 |
1 | #!/usr/bin/env perl |
2 | # |
3 | # testjoystick.pl |
4 | # |
5 | # Copyright (C) 2005 David J. Goehrig <dgoehrig@cpan.org> |
6 | # |
7 | # ------------------------------------------------------------------------------ |
8 | # |
9 | # This library is free software; you can redistribute it and/or |
10 | # modify it under the terms of the GNU Lesser General Public |
11 | # License as published by the Free Software Foundation; either |
12 | # version 2.1 of the License, or (at your option) any later version. |
13 | # |
14 | # This library is distributed in the hope that it will be useful, |
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | # Lesser General Public License for more details. |
18 | # |
19 | # You should have received a copy of the GNU Lesser General Public |
20 | # License along with this library; if not, write to the Free Software |
21 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | # |
23 | # ------------------------------------------------------------------------------ |
24 | # |
25 | # Please feel free to send questions, suggestions or improvements to: |
26 | # |
27 | # David J. Goehrig |
28 | # dgoehrig@cpan.org |
29 | # |
30 | |
31 | use strict; |
32 | use SDL; |
33 | use SDL::App; |
34 | use SDL::Rect; |
35 | use SDL::Event; |
36 | |
37 | |
38 | sub WatchJoystick($){ |
39 | (my $joystick) = @_; |
40 | my $screenWidth = 640; |
41 | my $screenHeight = 480; |
42 | |
43 | my $app = new SDL::App(-title => "Joystick Test", |
44 | -width => $screenWidth, |
45 | -height => $screenHeight, |
46 | -depth=> 16 ); |
47 | #Print information about the joystick we are watching |
48 | my $name = SDL::JoystickName(SDL::JoystickIndex($joystick)); |
49 | print "Watching joystick ".SDL::JoystickIndex($joystick). |
50 | ": (".($name ? $name : "Unknown Joystick" ).")\n"; |
51 | print "Joystick has ".SDL::JoystickNumAxes($joystick)." axes, ". |
52 | SDL::JoystickNumHats($joystick)." hats, ". |
53 | SDL::JoystickNumBalls($joystick)." balls, and ". |
54 | SDL::JoystickNumButtons($joystick)." buttons\n"; |
55 | |
56 | my $event = new SDL::Event; |
57 | my $done = 0; |
58 | my $colorWhite = new SDL::Color(-r=>255, -g=>255, -b=>255); |
59 | my $colorBlack = new SDL::Color(); |
60 | my @axisRect = (); |
61 | my $numAxes=SDL::JoystickNumAxes($joystick); |
62 | |
63 | |
64 | while(!$done) |
65 | { |
66 | while($event->poll()) |
67 | { |
68 | if($event->type() eq SDL::JOYAXISMOTION()) |
69 | { |
70 | print "Joystick ".SDL::JoyAxisEventWhich($$event). |
71 | " axis ".SDL::JoyAxisEventAxis($$event). |
72 | " value: ".SDL::JoyAxisEventValue($$event)."\n"; |
73 | } |
74 | elsif($event->type() eq SDL::JOYHATMOTION()) |
75 | { |
76 | print "Joystick ".SDL::JoyHatEventWhich($$event). |
77 | " hat ".SDL::JoyHatEventHat($$event); |
78 | if(SDL::JoyHatEventValue($$event) == SDL::HAT_CENTERED() ) |
79 | { |
80 | print " centered"; |
81 | } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_UP() ) { |
82 | print " up"; |
83 | } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_RIGHT() ) { |
84 | print " right"; |
85 | } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_DOWN() ) { |
86 | print " down"; |
87 | } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_LEFT()) { |
88 | print " left"; |
89 | } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_RIGHTUP() ) { |
90 | print " right & up"; |
91 | } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_RIGHTDOWN() ) { |
92 | print " right & down"; |
93 | } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_LEFTDOWN() ) { |
94 | print " left & down"; |
95 | } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_LEFTUP()) { |
96 | print " left & up"; |
97 | } |
98 | print "\n"; |
99 | } elsif($event->type() eq SDL::JOYBALLMOTION()){ |
100 | print "Joystick ".SDL::JoyBallEventWhich($$event). |
101 | " ball ".SDL::JoyBallEventBall($$event). |
102 | " delta: (".SDL::JoyBallEventXrel($$event). |
103 | ",".SDL::JoyBallEventYrel($$event)."\n"; |
104 | } elsif($event->type() eq SDL::JOYBUTTONDOWN()){ |
105 | print "Joystick ".SDL::JoyButtonEventWhich($$event). |
106 | " button ".SDL::JoyButtonEventButton($$event)." down\n"; |
107 | } elsif($event->type() eq SDL::JOYBUTTONUP()){ |
108 | print "Joystick ".SDL::JoyButtonEventWhich($$event). |
109 | " button ".SDL::JoyButtonEventButton($$event)." up\n"; |
110 | } elsif($event->type() eq SDL_QUIT() or |
111 | ($event->type() eq SDL_KEYDOWN() and |
112 | $event->key_sym() == SDLK_ESCAPE)){ |
113 | $done = 1; |
114 | } |
115 | |
116 | |
117 | |
118 | #Update visual joystick state |
119 | for(my $i =0; $i < SDL::JoystickNumButtons($joystick); $i++) |
120 | { |
121 | my $rect = new SDL::Rect( -width => 32, |
122 | -height => 32, |
123 | -x => $i*34, |
124 | -y => $screenHeight-34); |
125 | if(SDL::JoystickGetButton($joystick, $i) eq SDL::PRESSED()) |
126 | { |
127 | $app->fill($rect, $colorWhite); |
128 | } else { |
129 | $app->fill($rect, $colorBlack); |
130 | } |
131 | $app->update($rect); |
132 | } |
133 | |
134 | |
135 | for (my $i = 0; $i < $numAxes; $i+=1) |
136 | { |
137 | #Remove previous axis box |
138 | if($axisRect[$i]){ |
139 | $app->fill($axisRect[$i], $colorBlack); |
140 | $app->update($axisRect[$i]); |
141 | } |
142 | # Draw the axis |
143 | my $ox = SDL::JoystickGetAxis($joystick, $i); |
144 | my $x= abs ($ox/256); |
145 | if( $x < 0) { |
146 | $x=0; |
147 | } elsif ( $x > ($screenWidth-16) ){ |
148 | $x = $screenWidth-16; |
149 | } |
150 | |
151 | |
152 | if ($ox < 0) |
153 | { |
154 | $axisRect[$i] = new SDL::Rect( -width=> $x, |
155 | -height=> 32, |
156 | -x => ($screenWidth/2) - $x, |
157 | -y => $i*34 |
158 | ); |
159 | } |
160 | else |
161 | { |
162 | $axisRect[$i] = new SDL::Rect( -width=> $x, |
163 | -height=> 32, |
164 | -x => $screenWidth/2 , |
165 | -y => $i*34 |
166 | ); |
167 | } |
168 | |
169 | |
170 | $app->fill($axisRect[$i], $colorWhite); |
171 | $app->update($axisRect[$i]); |
172 | } |
173 | } |
174 | } |
175 | } |
176 | |
177 | die "Could not initialize SDL: ", SDL::GetError() |
178 | if( 0 > SDL::Init(SDL_INIT_JOYSTICK())); |
179 | |
180 | printf "There are %d joysticks attched\n", SDL::NumJoysticks(); |
181 | for(my $i = 0; $i < SDL::NumJoysticks(); $i++){ |
182 | my $name = SDL::JoystickName($i); |
183 | print "Joystick ".$i.": ".($name ? $name : "Unknown Joystick")."\n"; |
184 | } |
185 | |
186 | if ( $ARGV[0] ne undef){ |
187 | my $joystick = SDL::JoystickOpen($ARGV[0]); |
188 | if(!$joystick){ |
189 | print "Couldn't open joystick ".$ARGV[0].": ".SDL::GetError()."\n"; |
190 | } else { |
191 | WatchJoystick($joystick); |
192 | SDL::JoystickClose($joystick); |
193 | } |
194 | SDL::QuitSubSystem(SDL_INIT_JOYSTICK()); |
195 | } |
196 | |
197 | |
198 | exit; |
199 | |
200 | sub draw_axis_method_2() |
201 | { |
202 | } |
203 | |
204 | __DATA__ |
205 | sub draw_axis_method_1() |
206 | { |
207 | for (my $i = 0; $i < ($numAxes/2); $i+=2) |
208 | { |
209 | #Remove previous axis box |
210 | if($axisRect[$i]){ |
211 | $app->fill($axisRect[$i], $colorBlack); |
212 | $app->update($axisRect[$i]); |
213 | } |
214 | # Draw the X/Y axis |
215 | my $x = SDL::JoystickGetAxis($joystick, $i)+32768; |
216 | $x *= $screenWidth; |
217 | $x /= 65535; |
218 | if( $x < 0) { |
219 | $x=0; |
220 | } elsif ( $x > ($screenWidth-16) ){ |
221 | $x = $screenWidth-16; |
222 | } |
223 | my $y = SDL::JoystickGetAxis($joystick, $i+1)+32768; |
224 | $y *= $screenHeight; |
225 | $y /= 65535; |
226 | if( $y < 0) { |
227 | $y=0; |
228 | } elsif ( $y > ($screenHeight-16) ){ |
229 | $y = $screenHeight-16; |
230 | } |
231 | $axisRect[$i] = new SDL::Rect( -width=> 16, |
232 | -height=> 16, |
233 | -x => $x, |
234 | -y => $y); |
235 | $app->fill($axisRect[$i], $colorWhite); |
236 | $app->update($axisRect[$i]); |
237 | } |
238 | } |
239 | |
240 | } |