test/testjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Sep 2009 07:32:26 +0000
branchSDL-1.2
changeset 4209 62e86ab81e3c
parent 4073 0dd0ca51d941
permissions -rw-r--r--
Fixed bug #570

SDL_SemWaitTimeout in src/thread/generic/SDL_syssem.c line 179 (SVN trunk):

--sem->count;

should be

if (retval == 0) {
--sem->count;
}

Without this, sem->count will underflow on timeout effectively breaking the
semaphore. It appears that the implementation has been wrong since the initial
revision.
slouken@0
     1
slouken@0
     2
/* Simple program to test the SDL joystick routines */
slouken@0
     3
slouken@0
     4
#include <stdio.h>
slouken@0
     5
#include <stdlib.h>
slouken@0
     6
#include <string.h>
slouken@0
     7
slouken@0
     8
#include "SDL.h"
slouken@0
     9
slouken@0
    10
#define SCREEN_WIDTH	640
slouken@0
    11
#define SCREEN_HEIGHT	480
slouken@0
    12
slouken@0
    13
void WatchJoystick(SDL_Joystick *joystick)
slouken@0
    14
{
slouken@0
    15
	SDL_Surface *screen;
slouken@0
    16
	const char *name;
slouken@0
    17
	int i, done;
slouken@0
    18
	SDL_Event event;
slouken@0
    19
	int x, y, draw;
slouken@1855
    20
	SDL_Rect axis_area[2];
slouken@0
    21
slouken@0
    22
	/* Set a video mode to display joystick axis position */
slouken@0
    23
	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0);
slouken@0
    24
	if ( screen == NULL ) {
slouken@0
    25
		fprintf(stderr, "Couldn't set video mode: %s\n",SDL_GetError());
slouken@0
    26
		return;
slouken@0
    27
	}
slouken@0
    28
slouken@0
    29
	/* Print info about the joystick we are watching */
slouken@0
    30
	name = SDL_JoystickName(SDL_JoystickIndex(joystick));
slouken@0
    31
	printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
slouken@0
    32
	       name ? name : "Unknown Joystick");
slouken@0
    33
	printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
slouken@0
    34
	       SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
slouken@0
    35
	       SDL_JoystickNumBalls(joystick),SDL_JoystickNumButtons(joystick));
slouken@0
    36
slouken@0
    37
	/* Initialize drawing rectangles */
slouken@0
    38
	memset(axis_area, 0, (sizeof axis_area));
slouken@0
    39
	draw = 0;
slouken@0
    40
slouken@0
    41
	/* Loop, getting joystick events! */
slouken@0
    42
	done = 0;
slouken@0
    43
	while ( ! done ) {
slouken@0
    44
		while ( SDL_PollEvent(&event) ) {
slouken@0
    45
			switch (event.type) {
slouken@0
    46
			    case SDL_JOYAXISMOTION:
slouken@0
    47
				printf("Joystick %d axis %d value: %d\n",
slouken@0
    48
				       event.jaxis.which,
slouken@0
    49
				       event.jaxis.axis,
slouken@0
    50
				       event.jaxis.value);
slouken@0
    51
				break;
slouken@0
    52
			    case SDL_JOYHATMOTION:
slouken@0
    53
				printf("Joystick %d hat %d value:",
slouken@0
    54
				       event.jhat.which,
slouken@0
    55
				       event.jhat.hat);
slouken@0
    56
				if ( event.jhat.value == SDL_HAT_CENTERED )
slouken@0
    57
					printf(" centered");
slouken@0
    58
				if ( event.jhat.value & SDL_HAT_UP )
slouken@0
    59
					printf(" up");
slouken@0
    60
				if ( event.jhat.value & SDL_HAT_RIGHT )
slouken@0
    61
					printf(" right");
slouken@0
    62
				if ( event.jhat.value & SDL_HAT_DOWN )
slouken@0
    63
					printf(" down");
slouken@0
    64
				if ( event.jhat.value & SDL_HAT_LEFT )
slouken@0
    65
					printf(" left");
slouken@0
    66
				printf("\n");
slouken@0
    67
				break;
slouken@0
    68
			    case SDL_JOYBALLMOTION:
slouken@0
    69
				printf("Joystick %d ball %d delta: (%d,%d)\n",
slouken@0
    70
				       event.jball.which,
slouken@0
    71
				       event.jball.ball,
slouken@0
    72
				       event.jball.xrel,
slouken@0
    73
				       event.jball.yrel);
slouken@0
    74
				break;
slouken@0
    75
			    case SDL_JOYBUTTONDOWN:
slouken@0
    76
				printf("Joystick %d button %d down\n",
slouken@0
    77
				       event.jbutton.which,
slouken@0
    78
				       event.jbutton.button);
slouken@0
    79
				break;
slouken@0
    80
			    case SDL_JOYBUTTONUP:
slouken@0
    81
				printf("Joystick %d button %d up\n",
slouken@0
    82
				       event.jbutton.which,
slouken@0
    83
				       event.jbutton.button);
slouken@0
    84
				break;
slouken@0
    85
			    case SDL_KEYDOWN:
slouken@0
    86
				if ( event.key.keysym.sym != SDLK_ESCAPE ) {
slouken@0
    87
					break;
slouken@0
    88
				}
slouken@0
    89
				/* Fall through to signal quit */
slouken@0
    90
			    case SDL_QUIT:
slouken@0
    91
				done = 1;
slouken@0
    92
				break;
slouken@0
    93
			    default:
slouken@0
    94
				break;
slouken@0
    95
			}
slouken@0
    96
		}
slouken@0
    97
		/* Update visual joystick state */
slouken@0
    98
		for ( i=0; i<SDL_JoystickNumButtons(joystick); ++i ) {
slouken@0
    99
			SDL_Rect area;
slouken@0
   100
slouken@0
   101
			area.x = i*34;
slouken@0
   102
			area.y = SCREEN_HEIGHT-34;
slouken@0
   103
			area.w = 32;
slouken@0
   104
			area.h = 32;
slouken@0
   105
			if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
slouken@0
   106
				SDL_FillRect(screen, &area, 0xFFFF);
slouken@0
   107
			} else {
slouken@0
   108
				SDL_FillRect(screen, &area, 0x0000);
slouken@0
   109
			}
slouken@0
   110
			SDL_UpdateRects(screen, 1, &area);
slouken@0
   111
		}
slouken@0
   112
slouken@1855
   113
		/* Erase previous axes */
slouken@1855
   114
		SDL_FillRect(screen, &axis_area[draw], 0x0000);
slouken@0
   115
slouken@1855
   116
		/* Draw the X/Y axis */
slouken@1855
   117
		draw = !draw;
slouken@1855
   118
		x = (((int)SDL_JoystickGetAxis(joystick, 0))+32768);
slouken@1855
   119
		x *= SCREEN_WIDTH;
slouken@1855
   120
		x /= 65535;
slouken@1855
   121
		if ( x < 0 ) {
slouken@1855
   122
			x = 0;
slouken@1855
   123
		} else
slouken@1855
   124
		if ( x > (SCREEN_WIDTH-16) ) {
slouken@1855
   125
			x = SCREEN_WIDTH-16;
slouken@1855
   126
		}
slouken@1855
   127
		y = (((int)SDL_JoystickGetAxis(joystick, 1))+32768);
slouken@1855
   128
		y *= SCREEN_HEIGHT;
slouken@1855
   129
		y /= 65535;
slouken@1855
   130
		if ( y < 0 ) {
slouken@1855
   131
			y = 0;
slouken@1855
   132
		} else
slouken@1855
   133
		if ( y > (SCREEN_HEIGHT-16) ) {
slouken@1855
   134
			y = SCREEN_HEIGHT-16;
slouken@1855
   135
		}
slouken@1855
   136
		axis_area[draw].x = (Sint16)x;
slouken@1855
   137
		axis_area[draw].y = (Sint16)y;
slouken@1855
   138
		axis_area[draw].w = 16;
slouken@1855
   139
		axis_area[draw].h = 16;
slouken@1855
   140
		SDL_FillRect(screen, &axis_area[draw], 0xFFFF);
slouken@1854
   141
slouken@1855
   142
		SDL_UpdateRects(screen, 2, axis_area);
slouken@0
   143
	}
slouken@0
   144
}
slouken@0
   145
slouken@0
   146
int main(int argc, char *argv[])
slouken@0
   147
{
slouken@0
   148
	const char *name;
slouken@0
   149
	int i;
slouken@0
   150
	SDL_Joystick *joystick;
slouken@0
   151
slouken@0
   152
	/* Initialize SDL (Note: video is required to start event loop) */
slouken@0
   153
	if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0 ) {
slouken@0
   154
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@0
   155
		exit(1);
slouken@0
   156
	}
slouken@0
   157
slouken@0
   158
	/* Print information about the joysticks */
slouken@0
   159
	printf("There are %d joysticks attached\n", SDL_NumJoysticks());
slouken@0
   160
	for ( i=0; i<SDL_NumJoysticks(); ++i ) {
slouken@0
   161
		name = SDL_JoystickName(i);
slouken@0
   162
		printf("Joystick %d: %s\n",i,name ? name : "Unknown Joystick");
icculus@4073
   163
		joystick = SDL_JoystickOpen(i);
icculus@4073
   164
		if (joystick == NULL) {
icculus@4071
   165
			fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i, SDL_GetError());
icculus@4071
   166
		} else {
icculus@4073
   167
			printf("       axes: %d\n", SDL_JoystickNumAxes(joystick));
icculus@4073
   168
			printf("      balls: %d\n", SDL_JoystickNumBalls(joystick));
icculus@4073
   169
			printf("       hats: %d\n", SDL_JoystickNumHats(joystick));
icculus@4073
   170
			printf("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
icculus@4073
   171
			SDL_JoystickClose(joystick);
icculus@4071
   172
		}
slouken@0
   173
	}
slouken@0
   174
slouken@0
   175
	if ( argv[1] ) {
slouken@0
   176
		joystick = SDL_JoystickOpen(atoi(argv[1]));
slouken@0
   177
		if ( joystick == NULL ) {
slouken@0
   178
			printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
slouken@0
   179
			       SDL_GetError());
slouken@0
   180
		} else {
slouken@0
   181
			WatchJoystick(joystick);
slouken@0
   182
			SDL_JoystickClose(joystick);
slouken@0
   183
		}
slouken@0
   184
	}
slouken@0
   185
	SDL_QuitSubSystem(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK);
slouken@0
   186
slouken@0
   187
	return(0);
slouken@0
   188
}