5 # Copyright (C) 2005 David J. Goehrig <dgoehrig@cpan.org>
7 # ------------------------------------------------------------------------------
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.
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.
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
23 # ------------------------------------------------------------------------------
25 # Please feel free to send questions, suggestions or improvements to:
40 my $screenWidth = 640;
41 my $screenHeight = 480;
43 my $app = new SDL::App(-title => "Joystick Test",
44 -width => $screenWidth,
45 -height => $screenHeight,
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";
56 my $event = new SDL::Event;
58 my $colorWhite = new SDL::Color(-r=>255, -g=>255, -b=>255);
59 my $colorBlack = new SDL::Color();
61 my $numAxes=SDL::JoystickNumAxes($joystick);
68 if($event->type() eq SDL::JOYAXISMOTION())
70 print "Joystick ".SDL::JoyAxisEventWhich($$event).
71 " axis ".SDL::JoyAxisEventAxis($$event).
72 " value: ".SDL::JoyAxisEventValue($$event)."\n";
74 elsif($event->type() eq SDL::JOYHATMOTION())
76 print "Joystick ".SDL::JoyHatEventWhich($$event).
77 " hat ".SDL::JoyHatEventHat($$event);
78 if(SDL::JoyHatEventValue($$event) == SDL::HAT_CENTERED() )
81 } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_UP() ) {
83 } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_RIGHT() ) {
85 } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_DOWN() ) {
87 } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_LEFT()) {
89 } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_RIGHTUP() ) {
91 } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_RIGHTDOWN() ) {
92 print " right & down";
93 } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_LEFTDOWN() ) {
95 } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_LEFTUP()) {
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)){
118 #Update visual joystick state
119 for(my $i =0; $i < SDL::JoystickNumButtons($joystick); $i++)
121 my $rect = new SDL::Rect( -width => 32,
124 -y => $screenHeight-34);
125 if(SDL::JoystickGetButton($joystick, $i) eq SDL::PRESSED())
127 $app->fill($rect, $colorWhite);
129 $app->fill($rect, $colorBlack);
135 for (my $i = 0; $i < $numAxes; $i+=1)
137 #Remove previous axis box
139 $app->fill($axisRect[$i], $colorBlack);
140 $app->update($axisRect[$i]);
143 my $ox = SDL::JoystickGetAxis($joystick, $i);
144 my $x= abs ($ox/256);
147 } elsif ( $x > ($screenWidth-16) ){
148 $x = $screenWidth-16;
154 $axisRect[$i] = new SDL::Rect( -width=> $x,
156 -x => ($screenWidth/2) - $x,
162 $axisRect[$i] = new SDL::Rect( -width=> $x,
164 -x => $screenWidth/2 ,
170 $app->fill($axisRect[$i], $colorWhite);
171 $app->update($axisRect[$i]);
177 die "Could not initialize SDL: ", SDL::GetError()
178 if( 0 > SDL::Init(SDL_INIT_JOYSTICK()));
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";
186 if ( $ARGV[0] ne undef){
187 my $joystick = SDL::JoystickOpen($ARGV[0]);
189 print "Couldn't open joystick ".$ARGV[0].": ".SDL::GetError()."\n";
191 WatchJoystick($joystick);
192 SDL::JoystickClose($joystick);
194 SDL::QuitSubSystem(SDL_INIT_JOYSTICK());
200 sub draw_axis_method_2()
205 sub draw_axis_method_1()
207 for (my $i = 0; $i < ($numAxes/2); $i+=2)
209 #Remove previous axis box
211 $app->fill($axisRect[$i], $colorBlack);
212 $app->update($axisRect[$i]);
215 my $x = SDL::JoystickGetAxis($joystick, $i)+32768;
220 } elsif ( $x > ($screenWidth-16) ){
221 $x = $screenWidth-16;
223 my $y = SDL::JoystickGetAxis($joystick, $i+1)+32768;
228 } elsif ( $y > ($screenHeight-16) ){
229 $y = $screenHeight-16;
231 $axisRect[$i] = new SDL::Rect( -width=> 16,
235 $app->fill($axisRect[$i], $colorWhite);
236 $app->update($axisRect[$i]);