mirror of
https://github.com/chocolate-doom/joytest.git
synced 2024-11-21 20:01:09 +00:00
Add first version of joystick test program.
This commit is contained in:
commit
5759ef7d8e
2 changed files with 158 additions and 0 deletions
7
Makefile
Normal file
7
Makefile
Normal file
|
@ -0,0 +1,7 @@
|
|||
|
||||
CFLAGS = $(shell sdl-config --cflags)
|
||||
LDFLAGS = $(shell sdl-config --libs)
|
||||
|
||||
joytest : joytest.c
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
|
||||
|
151
joytest.c
Normal file
151
joytest.c
Normal file
|
@ -0,0 +1,151 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "SDL.h"
|
||||
|
||||
void ProcessAxisMotion(SDL_Event *ev)
|
||||
{
|
||||
static int lastjoy = -1, lastaxis, lastdirection;
|
||||
int direction;
|
||||
|
||||
direction = ev->jaxis.value > 0;
|
||||
|
||||
if (ev->jaxis.which == lastjoy && ev->jaxis.axis == lastaxis
|
||||
&& direction == lastdirection)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Only print a message when pushed to the extreme
|
||||
if (abs(ev->jaxis.value) < 30000)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
printf("%i: Axis %i pushed to extreme: %i\n",
|
||||
ev->jaxis.which, ev->jaxis.axis, ev->jaxis.value);
|
||||
|
||||
lastjoy = ev->jaxis.which;
|
||||
lastaxis = ev->jaxis.axis;
|
||||
lastdirection = direction;
|
||||
}
|
||||
|
||||
void ProcessBallMotion(SDL_Event *ev)
|
||||
{
|
||||
printf("%i: Ball %i motion: %i, %i\n",
|
||||
ev->jball.which, ev->jball.ball, ev->jball.xrel, ev->jball.yrel);
|
||||
}
|
||||
|
||||
char *HatDirection(int value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case SDL_HAT_UP:
|
||||
return "UP";
|
||||
case SDL_HAT_DOWN:
|
||||
return "DOWN";
|
||||
case SDL_HAT_LEFT:
|
||||
return "LEFT";
|
||||
case SDL_HAT_RIGHT:
|
||||
return "RIGHT";
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessHatMotion(SDL_Event *ev)
|
||||
{
|
||||
char *dirstr;
|
||||
|
||||
dirstr = HatDirection(ev->jhat.value);
|
||||
|
||||
if (dirstr != NULL)
|
||||
{
|
||||
printf("%i: Hat %i moved to %s", ev->jhat.which, ev->jhat.hat,
|
||||
dirstr);
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessButtonDown(SDL_Event *ev)
|
||||
{
|
||||
printf("%i: Button %i pressed\n", ev->jbutton.which, ev->jbutton.button);
|
||||
}
|
||||
|
||||
void StartJoysticks(void)
|
||||
{
|
||||
SDL_Joystick *js;
|
||||
int i;
|
||||
|
||||
// Open all joysticks.
|
||||
|
||||
printf("%i joysticks:\n", SDL_NumJoysticks());
|
||||
|
||||
for (i = 0 ; i < SDL_NumJoysticks(); ++i)
|
||||
{
|
||||
printf("\t#%i: '%s'\n", i, SDL_JoystickName(i));
|
||||
|
||||
js = SDL_JoystickOpen(i);
|
||||
printf("\t\taxes: %i\n", SDL_JoystickNumAxes(js));
|
||||
printf("\t\tbuttons: %i\n", SDL_JoystickNumButtons(js));
|
||||
printf("\t\tballs: %i\n", SDL_JoystickNumBalls(js));
|
||||
printf("\t\thats: %i\n", SDL_JoystickNumHats(js));
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Event ev;
|
||||
|
||||
SDL_Init(SDL_INIT_JOYSTICK|SDL_INIT_VIDEO);
|
||||
|
||||
if (SDL_NumJoysticks() == 0)
|
||||
{
|
||||
fprintf(stderr, "Found no joysticks to configure!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_SetVideoMode(400, 200, 0, 0);
|
||||
SDL_WM_SetCaption("Close this window to quit.", NULL);
|
||||
|
||||
StartJoysticks();
|
||||
|
||||
for (;;)
|
||||
{
|
||||
SDL_PumpEvents();
|
||||
SDL_WaitEvent(&ev);
|
||||
switch (ev.type)
|
||||
{
|
||||
case SDL_QUIT:
|
||||
goto exitloop;
|
||||
|
||||
case SDL_KEYDOWN:
|
||||
if (ev.key.keysym.sym == SDLK_ESCAPE)
|
||||
{
|
||||
goto exitloop;
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_JOYAXISMOTION:
|
||||
ProcessAxisMotion(&ev);
|
||||
break;
|
||||
|
||||
case SDL_JOYBALLMOTION:
|
||||
ProcessBallMotion(&ev);
|
||||
break;
|
||||
|
||||
case SDL_JOYHATMOTION:
|
||||
ProcessHatMotion(&ev);
|
||||
break;
|
||||
|
||||
case SDL_JOYBUTTONDOWN:
|
||||
ProcessButtonDown(&ev);
|
||||
break;
|
||||
}
|
||||
}
|
||||
exitloop:
|
||||
|
||||
SDL_Quit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in a new issue