File: testjoystick.pl

package info (click to toggle)
sdlperl 1.20.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,804 kB
  • ctags: 2,169
  • sloc: perl: 7,397; ansic: 232; makefile: 72; sh: 1
file content (219 lines) | stat: -rwxr-xr-x 6,212 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env perl
#
#
# testjoystick.pl
#
#	adapted from SDL-1.2.x/test/testjoystick.c

use strict;
#use warnings;

use SDL;
use SDL::App;
use SDL::Rect;
use SDL::Event;


sub WatchJoystick($){
	(my $joystick) = @_;
	my $screenWidth = 640;
	my $screenHeight = 480;

	my $app = new SDL::App(-title => "Joystick Test",
			       -width => $screenWidth,
			       -height => $screenHeight,
			       -depth=> 16 );
	#Print information about the joystick we are watching
	my $name = SDL::JoystickName(SDL::JoystickIndex($joystick));
	print "Watching joystick ".SDL::JoystickIndex($joystick).
	      ": (".($name ? $name : "Unknown Joystick" ).")\n";
	print "Joystick has ".SDL::JoystickNumAxes($joystick)." axes, ".
	      SDL::JoystickNumHats($joystick)." hats, ".
	      SDL::JoystickNumBalls($joystick)." balls, and ".
	      SDL::JoystickNumButtons($joystick)." buttons\n";
	
	my $event = new SDL::Event;
	my $done = 0;	
	my $colorWhite = new SDL::Color(-r=>255, -g=>255, -b=>255);
	my $colorBlack = new SDL::Color();
	my @axisRect = ();
	my $numAxes=SDL::JoystickNumAxes($joystick);


	while(!$done)
	  {
		while($event->poll())
		  {
			if($event->type() eq SDL::JOYAXISMOTION())
			  {
				print "Joystick ".SDL::JoyAxisEventWhich($event->{-event}).
				  " axis ".SDL::JoyAxisEventAxis($event->{-event}).
					" value: ".SDL::JoyAxisEventValue($event->{-event})."\n";
			  } 
			elsif($event->type() eq SDL::JOYHATMOTION())
			  {
				print "Joystick ".SDL::JoyHatEventWhich($event->{-event}).
				  " hat ".SDL::JoyHatEventHat($event->{-event});
				if(SDL::JoyHatEventValue($event->{-event}) == SDL::HAT_CENTERED() )
				  {
					print " centered";
				  } elsif(SDL::JoyHatEventValue($event->{-event}) == SDL::HAT_UP() ) { 
					print " up";
				  } elsif(SDL::JoyHatEventValue($event->{-event}) == SDL::HAT_RIGHT() ) {
					print " right";
				  } elsif(SDL::JoyHatEventValue($event->{-event}) == SDL::HAT_DOWN() ) {
					print " down";
				  } elsif(SDL::JoyHatEventValue($event->{-event}) == SDL::HAT_LEFT()) {
					print " left";
				  } elsif(SDL::JoyHatEventValue($event->{-event}) == SDL::HAT_RIGHTUP() ) { 
					print " right & up";
				  } elsif(SDL::JoyHatEventValue($event->{-event}) == SDL::HAT_RIGHTDOWN() ) {
					print " right & down";
				  } elsif(SDL::JoyHatEventValue($event->{-event}) == SDL::HAT_LEFTDOWN() ) {
					print " left & down";
				  } elsif(SDL::JoyHatEventValue($event->{-event}) == SDL::HAT_LEFTUP()) {
					print " left & up";
				  }
				print "\n";
			  } elsif($event->type() eq SDL::JOYBALLMOTION()){
				print "Joystick ".SDL::JoyBallEventWhich($event->{-event}).
				  " ball ".SDL::JoyBallEventBall($event->{-event}).
				      " delta: (".SDL::JoyBallEventXrel($event->{-event}).
				      ",".SDL::JoyBallEventYrel($event->{-event})."\n";
			} elsif($event->type() eq SDL::JOYBUTTONDOWN()){
				print "Joystick ".SDL::JoyButtonEventWhich($event->{-event}).
				      " button ".SDL::JoyButtonEventButton($event->{-event})." down\n";
			} elsif($event->type() eq SDL::JOYBUTTONUP()){
				print "Joystick ".SDL::JoyButtonEventWhich($event->{-event}).
				      " button ".SDL::JoyButtonEventButton($event->{-event})." up\n";
			} elsif($event->type() eq SDL_QUIT() or 
			        ($event->type() eq SDL_KEYDOWN() and 
				 $event->key_sym() == SDLK_ESCAPE)){
				$done = 1;
			}
			


			#Update visual joystick state
			for(my $i =0; $i < SDL::JoystickNumButtons($joystick); $i++)
			  {
				my $rect = new SDL::Rect( -width => 32,
										  -height => 32,
										  -x => $i*34,
										  -y => $screenHeight-34); 
				if(SDL::JoystickGetButton($joystick, $i) eq SDL::PRESSED())
				  {
					$app->fill($rect, $colorWhite); 
				  } else {
					$app->fill($rect, $colorBlack); 
				  }
				$app->update($rect);
			  }


			  for (my $i = 0; $i < $numAxes; $i+=1)
				{
				  #Remove previous axis box
				  if($axisRect[$i]){
					$app->fill($axisRect[$i], $colorBlack);
					$app->update($axisRect[$i]);
				  }
				  # Draw the axis
				  my $ox = SDL::JoystickGetAxis($joystick, $i);
				  my $x= abs ($ox/256);
				  if( $x < 0) {
					$x=0;
				  } elsif ( $x > ($screenWidth-16) ){
					$x = $screenWidth-16;
				  }


				  if ($ox < 0)
					{
					  $axisRect[$i] = new SDL::Rect( -width=> $x,
													 -height=> 32,
													 -x => ($screenWidth/2) - $x,
													 -y => $i*34
												   );
					}
				  else
					{
					  $axisRect[$i] = new SDL::Rect( -width=> $x,
													 -height=> 32,
													 -x => $screenWidth/2 ,
													 -y => $i*34
												   );
					}


				  $app->fill($axisRect[$i], $colorWhite);
				  $app->update($axisRect[$i]);
				}
		  }
	  }
  }
				
die "Could not initialize SDL: ", SDL::GetError()
	if( 0 > SDL::Init(SDL_INIT_JOYSTICK));

printf "There are %d joysticks attched\n", SDL::NumJoysticks();
for(my $i = 0; $i < SDL::NumJoysticks(); $i++){
	my $name = SDL::JoystickName($i);
	print "Joystick ".$i.": ".($name ? $name : "Unknown Joystick")."\n"; 
}

if ( $ARGV[0] ne undef){
	my $joystick = SDL::JoystickOpen($ARGV[0]);
	if(!$joystick){
		print "Couldn't open joystick ".$ARGV[0].": ".SDL::GetError()."\n";
	} else {
		WatchJoystick($joystick);
		SDL::JoystickClose($joystick);
	}
	SDL::QuitSubSystem(SDL::INIT_JOYSTICK());
}


exit;

sub draw_axis_method_2()
{
}

__DATA__
sub draw_axis_method_1()
{
				for (my $i = 0; $i < ($numAxes/2); $i+=2)
				  {
					#Remove previous axis box
					if($axisRect[$i]){
					  $app->fill($axisRect[$i], $colorBlack);
					  $app->update($axisRect[$i]);
					}
					# Draw the X/Y axis
					my $x = SDL::JoystickGetAxis($joystick, $i)+32768;
					$x *= $screenWidth;
					$x /= 65535;
					if( $x < 0) {
					  $x=0;
					} elsif ( $x > ($screenWidth-16) ){
					  $x = $screenWidth-16;
					}
					my $y = SDL::JoystickGetAxis($joystick, $i+1)+32768;
					$y *= $screenHeight;
					$y /= 65535;
					if( $y < 0) {
					  $y=0;
					} elsif ( $y > ($screenHeight-16) ){
					  $y = $screenHeight-16; 
					}
					$axisRect[$i] = new SDL::Rect( -width=> 16,
												   -height=> 16,
												   -x => $x,
												   -y => $y);
					$app->fill($axisRect[$i], $colorWhite);
					$app->update($axisRect[$i]);
				  }
			  }

}