commit a86c2b9c85b331fae8c45ee8d2f7c6013fc3c7f8
parent 6a93fddf92a5a6efc0dcde3654d0304dd9feb1fc
Author: krasjet
Date: 2021-02-03 02:03Z

sdl: add joystick

Diffstat:
Minput/joystick.c | 6+++---
Minput/joystick_nonblock.c | 2+-
Msdl/Makefile | 1+
Msdl/README | 2++
Asdl/joystick.c | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 77 insertions(+), 4 deletions(-)

diff --git a/input/joystick.c b/input/joystick.c @@ -35,7 +35,7 @@ main(void) /* open joystick device */ fd = open("/dev/input/js0", O_RDONLY); if (fd < 0) - die("can't open joystick device"); + die("fail to open joystick device"); /* get joystick name */ if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0) @@ -44,12 +44,12 @@ main(void) /* get number of axes */ if (ioctl(fd, JSIOCGAXES, &axis_cnt) < 0) - die("can't get number of axes"); + die("fail to get number of axes"); printf("axes: %d\n", axis_cnt); /* get number of buttons */ if (ioctl(fd, JSIOCGBUTTONS, &btn_cnt) < 0) - die("can't get number of buttons"); + die("fail to get number of buttons"); printf("buttons: %d\n", btn_cnt); /* install signal handler for grace shutdown (Ctrl-C) */ diff --git a/input/joystick_nonblock.c b/input/joystick_nonblock.c @@ -34,7 +34,7 @@ main(void) /* open joystick device in non-blocking mode */ fd = open("/dev/input/js0", O_RDONLY | O_NONBLOCK); if (fd < 0) - die("can't open joystick"); + die("fail to open joystick device"); /* install signal handler for grace shutdown (Ctrl-C) */ signal(SIGINT, sig_handler); diff --git a/sdl/Makefile b/sdl/Makefile @@ -11,6 +11,7 @@ BINS = \ fb \ simple \ mouse \ + joystick \ nogui \ gl_simple \ gl_triangle_compat \ diff --git a/sdl/README b/sdl/README @@ -7,6 +7,8 @@ simple.c: the basis of any SDL application. does nothing by itself. mouse.c: access mouse position +joystick.c: + read joystick input nogui.c: SDL without GUI gl_simple.c diff --git a/sdl/joystick.c b/sdl/joystick.c @@ -0,0 +1,70 @@ +/* joystick.c: read joystick input */ +#include <SDL2/SDL.h> +#include <signal.h> + +static volatile int done = 0; + +static void +die(const char *msg) +{ + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", msg); + exit(EXIT_FAILURE); +} + +static void +on_signal(int signum) +{ + (void)signum; + done = 1; +} + +int +main(void) +{ + SDL_Joystick *js; + + if (SDL_Init(SDL_INIT_JOYSTICK) != 0) + die("fail to init sdl"); + + js = SDL_JoystickOpen(0); + if (!js) + die("fail to open joystick device"); + + /* install signal handler for grace shutdown */ + signal(SIGINT, on_signal); + + while (!done) { + SDL_Event evt; + + while (SDL_PollEvent(&evt)) { + switch(evt.type) { + case SDL_JOYAXISMOTION: + puts("axis event:"); + printf(" number: %d\n", evt.jaxis.axis); + printf(" value: %d\n", evt.jaxis.value); + printf(" time: %d\n", evt.jaxis.timestamp); + break; + case SDL_JOYHATMOTION: + puts("hat motion event:"); + printf(" number: %d\n", evt.jhat.hat); + printf(" value: %d\n", evt.jhat.value); + printf(" time: %d\n", evt.jhat.timestamp); + break; + case SDL_JOYBUTTONDOWN: + puts("button down event:"); + printf(" number: %d\n", evt.jbutton.button); + printf(" time: %d\n", evt.jbutton.timestamp); + break; + case SDL_JOYBUTTONUP: + puts("button up event:"); + printf(" number: %d\n", evt.jbutton.button); + printf(" time: %d\n", evt.jbutton.timestamp); + break; + } + } + } + + SDL_JoystickClose(js); + SDL_Quit(); + return 0; +}