6 # adapted from SDL-1.2.x/test/testjoystick.c
19 my $screenWidth = 640;
20 my $screenHeight = 480;
22 my $app = new SDL::App(-title => "Joystick Test",
23 -width => $screenWidth,
24 -height => $screenHeight,
26 #Print information about the joystick we are watching
27 my $name = SDL::JoystickName(SDL::JoystickIndex($joystick));
28 print "Watching joystick ".SDL::JoystickIndex($joystick).
29 ": (".($name ? $name : "Unknown Joystick" ).")\n";
30 print "Joystick has ".SDL::JoystickNumAxes($joystick)." axes, ".
31 SDL::JoystickNumHats($joystick)." hats, ".
32 SDL::JoystickNumBalls($joystick)." balls, and ".
33 SDL::JoystickNumButtons($joystick)." buttons\n";
35 my $event = new SDL::Event;
37 my $colorWhite = new SDL::Color(-r=>255, -g=>255, -b=>255);
38 my $colorBlack = new SDL::Color();
40 my $numAxes=SDL::JoystickNumAxes($joystick);
47 if($event->type() eq SDL::JOYAXISMOTION())
49 print "Joystick ".SDL::JoyAxisEventWhich($$event).
50 " axis ".SDL::JoyAxisEventAxis($$event).
51 " value: ".SDL::JoyAxisEventValue($$event)."\n";
53 elsif($event->type() eq SDL::JOYHATMOTION())
55 print "Joystick ".SDL::JoyHatEventWhich($$event).
56 " hat ".SDL::JoyHatEventHat($$event);
57 if(SDL::JoyHatEventValue($$event) == SDL::HAT_CENTERED() )
60 } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_UP() ) {
62 } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_RIGHT() ) {
64 } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_DOWN() ) {
66 } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_LEFT()) {
68 } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_RIGHTUP() ) {
70 } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_RIGHTDOWN() ) {
71 print " right & down";
72 } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_LEFTDOWN() ) {
74 } elsif(SDL::JoyHatEventValue($$event) == SDL::HAT_LEFTUP()) {
78 } elsif($event->type() eq SDL::JOYBALLMOTION()){
79 print "Joystick ".SDL::JoyBallEventWhich($$event).
80 " ball ".SDL::JoyBallEventBall($$event).
81 " delta: (".SDL::JoyBallEventXrel($$event).
82 ",".SDL::JoyBallEventYrel($$event)."\n";
83 } elsif($event->type() eq SDL::JOYBUTTONDOWN()){
84 print "Joystick ".SDL::JoyButtonEventWhich($$event).
85 " button ".SDL::JoyButtonEventButton($$event)." down\n";
86 } elsif($event->type() eq SDL::JOYBUTTONUP()){
87 print "Joystick ".SDL::JoyButtonEventWhich($$event).
88 " button ".SDL::JoyButtonEventButton($$event)." up\n";
89 } elsif($event->type() eq SDL_QUIT() or
90 ($event->type() eq SDL_KEYDOWN() and
91 $event->key_sym() == SDLK_ESCAPE)){
97 #Update visual joystick state
98 for(my $i =0; $i < SDL::JoystickNumButtons($joystick); $i++)
100 my $rect = new SDL::Rect( -width => 32,
103 -y => $screenHeight-34);
104 if(SDL::JoystickGetButton($joystick, $i) eq SDL::PRESSED())
106 $app->fill($rect, $colorWhite);
108 $app->fill($rect, $colorBlack);
114 for (my $i = 0; $i < $numAxes; $i+=1)
116 #Remove previous axis box
118 $app->fill($axisRect[$i], $colorBlack);
119 $app->update($axisRect[$i]);
122 my $ox = SDL::JoystickGetAxis($joystick, $i);
123 my $x= abs ($ox/256);
126 } elsif ( $x > ($screenWidth-16) ){
127 $x = $screenWidth-16;
133 $axisRect[$i] = new SDL::Rect( -width=> $x,
135 -x => ($screenWidth/2) - $x,
141 $axisRect[$i] = new SDL::Rect( -width=> $x,
143 -x => $screenWidth/2 ,
149 $app->fill($axisRect[$i], $colorWhite);
150 $app->update($axisRect[$i]);
156 die "Could not initialize SDL: ", SDL::GetError()
157 if( 0 > SDL::Init(SDL_INIT_JOYSTICK()));
159 printf "There are %d joysticks attched\n", SDL::NumJoysticks();
160 for(my $i = 0; $i < SDL::NumJoysticks(); $i++){
161 my $name = SDL::JoystickName($i);
162 print "Joystick ".$i.": ".($name ? $name : "Unknown Joystick")."\n";
165 if ( $ARGV[0] ne undef){
166 my $joystick = SDL::JoystickOpen($ARGV[0]);
168 print "Couldn't open joystick ".$ARGV[0].": ".SDL::GetError()."\n";
170 WatchJoystick($joystick);
171 SDL::JoystickClose($joystick);
173 SDL::QuitSubSystem(SDL_INIT_JOYSTICK());
179 sub draw_axis_method_2()
184 sub draw_axis_method_1()
186 for (my $i = 0; $i < ($numAxes/2); $i+=2)
188 #Remove previous axis box
190 $app->fill($axisRect[$i], $colorBlack);
191 $app->update($axisRect[$i]);
194 my $x = SDL::JoystickGetAxis($joystick, $i)+32768;
199 } elsif ( $x > ($screenWidth-16) ){
200 $x = $screenWidth-16;
202 my $y = SDL::JoystickGetAxis($joystick, $i+1)+32768;
207 } elsif ( $y > ($screenHeight-16) ){
208 $y = $screenHeight-16;
210 $axisRect[$i] = new SDL::Rect( -width=> 16,
214 $app->fill($axisRect[$i], $colorWhite);
215 $app->update($axisRect[$i]);