aboutsummaryrefslogtreecommitdiff
path: root/src/pad/sdl-1.2
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-09-27 17:03:06 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-11-01 16:26:16 +0100
commit980858186149651df5543b6fc99a4f7db0cdd089 (patch)
treed347200b0a562d84df505097651ad0642f207fdd /src/pad/sdl-1.2
parent39f50e601d395bbd2d78d0147ac530b756da2fff (diff)
downloadjancity-980858186149651df5543b6fc99a4f7db0cdd089.tar.gz
WIP
Diffstat (limited to 'src/pad/sdl-1.2')
-rw-r--r--src/pad/sdl-1.2/inc/pad/port.h4
-rw-r--r--src/pad/sdl-1.2/src/pad.c48
2 files changed, 50 insertions, 2 deletions
diff --git a/src/pad/sdl-1.2/inc/pad/port.h b/src/pad/sdl-1.2/inc/pad/port.h
index 8334b6a..d28bb11 100644
--- a/src/pad/sdl-1.2/inc/pad/port.h
+++ b/src/pad/sdl-1.2/inc/pad/port.h
@@ -1,6 +1,8 @@
#ifndef PAD_SDL_1_2_H
#define PAD_SDL_1_2_H
+#include <SDL_joystick.h>
+
#ifdef __cplusplus
extern "C"
{
@@ -8,7 +10,7 @@ extern "C"
struct pad_port
{
- int dummy;
+ SDL_Joystick *joystick;
};
#ifdef __cplusplus
diff --git a/src/pad/sdl-1.2/src/pad.c b/src/pad/sdl-1.2/src/pad.c
index 57fdb25..97aa639 100644
--- a/src/pad/sdl-1.2/src/pad.c
+++ b/src/pad/sdl-1.2/src/pad.c
@@ -1,11 +1,57 @@
#include <pad.h>
#include <SDL.h>
+#include <SDL_joystick.h>
#include <stdio.h>
+static void global_init(void)
+{
+ SDL_JoystickEventState(SDL_ENABLE);
+}
+
void pad_port_update(struct pad *const p)
{
+ enum {FLAGS = SDL_JOYBUTTONDOWNMASK
+ | SDL_JOYBUTTONUPMASK
+ | SDL_JOYAXISMOTIONMASK
+ | SDL_QUITMASK};
+
+ SDL_Event ev;
+ int n;
+
+ while ((n = SDL_PeepEvents(&ev, 1, SDL_GETEVENT, FLAGS)) > 0)
+ {
+ }
+
+ if (n < 0)
+ fprintf(stderr, "%s: SDL_PeepEvents: %s\n", __func__, SDL_GetError());
+}
+
+void pad_init(const int id, struct pad *const p)
+{
+ static bool init;
+
+ if (!init)
+ {
+ global_init();
+ init = true;
+ }
+
+ if (!(p->port.joystick = SDL_JoystickOpen(id)))
+ fprintf(stderr, "%s: SDL_JoystickOpen failed: %s\n",
+ __func__, SDL_GetError());
+ else
+ printf("%s: num axis: %d\n", __func__,
+ SDL_JoystickNumAxes(p->port.joystick));
+}
+
+size_t pad_count(void)
+{
+ const int res = SDL_NumJoysticks();
+
+ return res > 0 ? res : 0;
}
-void pad_init(const int player, struct pad *const p)
+const char *pad_name(const int id)
{
+ return SDL_JoystickName(id);
}