test/testjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 21 May 2006 16:47:41 +0000
changeset 1854 2280e314a978
parent 0 74212992fb08
child 1662 782fd950bd46
child 1855 5ff2c01e475e
permissions -rw-r--r--
Closed bug #74

Added DirectInput joystick code, contributed by Glenn Maynard.

This fixes a problem with the Windows Multimedia joystick driver
not showing all 6 axes on a GameCube controller converter, which
was donated by Jacob Kolding.
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@1854
    20
	SDL_Rect axis_area[6][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@1854
   113
		for ( i=0; i<SDL_JoystickNumAxes(joystick)/2 && i < SDL_arraysize(axis_area); ++i ) {
slouken@1854
   114
			/* Erase previous axes */
slouken@1854
   115
			SDL_FillRect(screen, &axis_area[i][draw], 0x0000);
slouken@0
   116
slouken@1854
   117
			/* Draw the X/Y axis */
slouken@1854
   118
			draw = !draw;
slouken@1854
   119
			x = (((int)SDL_JoystickGetAxis(joystick, i*2+0))+32768);
slouken@1854
   120
			x *= SCREEN_WIDTH;
slouken@1854
   121
			x /= 65535;
slouken@1854
   122
			if ( x < 0 ) {
slouken@1854
   123
				x = 0;
slouken@1854
   124
			} else
slouken@1854
   125
			if ( x > (SCREEN_WIDTH-16) ) {
slouken@1854
   126
				x = SCREEN_WIDTH-16;
slouken@1854
   127
			}
slouken@1854
   128
			y = (((int)SDL_JoystickGetAxis(joystick, i*2+1))+32768);
slouken@1854
   129
			y *= SCREEN_HEIGHT;
slouken@1854
   130
			y /= 65535;
slouken@1854
   131
			if ( y < 0 ) {
slouken@1854
   132
				y = 0;
slouken@1854
   133
			} else
slouken@1854
   134
			if ( y > (SCREEN_HEIGHT-16) ) {
slouken@1854
   135
				y = SCREEN_HEIGHT-16;
slouken@1854
   136
			}
slouken@1854
   137
			axis_area[i][draw].x = (Sint16)x;
slouken@1854
   138
			axis_area[i][draw].y = (Sint16)y;
slouken@1854
   139
			axis_area[i][draw].w = 16;
slouken@1854
   140
			axis_area[i][draw].h = 16;
slouken@1854
   141
			SDL_FillRect(screen, &axis_area[i][draw], 0xFFFF);
slouken@1854
   142
slouken@1854
   143
			SDL_UpdateRects(screen, 2, axis_area[i]);
slouken@0
   144
		}
slouken@0
   145
	}
slouken@0
   146
}
slouken@0
   147
slouken@0
   148
int main(int argc, char *argv[])
slouken@0
   149
{
slouken@0
   150
	const char *name;
slouken@0
   151
	int i;
slouken@0
   152
	SDL_Joystick *joystick;
slouken@0
   153
slouken@0
   154
	/* Initialize SDL (Note: video is required to start event loop) */
slouken@0
   155
	if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0 ) {
slouken@0
   156
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
slouken@0
   157
		exit(1);
slouken@0
   158
	}
slouken@0
   159
slouken@0
   160
	/* Print information about the joysticks */
slouken@0
   161
	printf("There are %d joysticks attached\n", SDL_NumJoysticks());
slouken@0
   162
	for ( i=0; i<SDL_NumJoysticks(); ++i ) {
slouken@0
   163
		name = SDL_JoystickName(i);
slouken@0
   164
		printf("Joystick %d: %s\n",i,name ? name : "Unknown Joystick");
slouken@0
   165
	}
slouken@0
   166
slouken@0
   167
	if ( argv[1] ) {
slouken@0
   168
		joystick = SDL_JoystickOpen(atoi(argv[1]));
slouken@0
   169
		if ( joystick == NULL ) {
slouken@0
   170
			printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
slouken@0
   171
			       SDL_GetError());
slouken@0
   172
		} else {
slouken@0
   173
			WatchJoystick(joystick);
slouken@0
   174
			SDL_JoystickClose(joystick);
slouken@0
   175
		}
slouken@0
   176
	}
slouken@0
   177
	SDL_QuitSubSystem(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK);
slouken@0
   178
slouken@0
   179
	return(0);
slouken@0
   180
}