aboutsummaryrefslogtreecommitdiff
path: root/src/pad
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
parent39f50e601d395bbd2d78d0147ac530b756da2fff (diff)
downloadjancity-980858186149651df5543b6fc99a4f7db0cdd089.tar.gz
WIP
Diffstat (limited to 'src/pad')
-rw-r--r--src/pad/inc/pad.h8
-rw-r--r--src/pad/ps1/src/pad.c26
-rw-r--r--src/pad/sdl-1.2/inc/pad/port.h4
-rw-r--r--src/pad/sdl-1.2/src/pad.c48
4 files changed, 78 insertions, 8 deletions
diff --git a/src/pad/inc/pad.h b/src/pad/inc/pad.h
index c8f6b31..d68f734 100644
--- a/src/pad/inc/pad.h
+++ b/src/pad/inc/pad.h
@@ -3,6 +3,7 @@
#include <pad/port.h>
#include <stdbool.h>
+#include <stddef.h>
#ifdef __cplusplus
extern "C"
@@ -34,17 +35,20 @@ enum pad_key
struct pad
{
- int player;
+ int id;
int mask, oldmask;
struct pad_port port;
};
-void pad_init(int player, struct pad *p);
+void pad_init(int id, struct pad *p);
+void pad_deinit(int id, struct pad *p);
void pad_update(struct pad *p);
bool pad_pressed(const struct pad *p, enum pad_key k);
bool pad_justpressed(const struct pad *p, enum pad_key k);
bool pad_released(const struct pad *p, enum pad_key k);
const char *pad_str(enum pad_key k);
+size_t pad_count(void);
+const char *pad_name(int id);
#ifdef __cplusplus
}
diff --git a/src/pad/ps1/src/pad.c b/src/pad/ps1/src/pad.c
index eea56ed..660cf89 100644
--- a/src/pad/ps1/src/pad.c
+++ b/src/pad/ps1/src/pad.c
@@ -4,10 +4,18 @@
#include <stddef.h>
#include <string.h>
-void pad_init(const int player, struct pad *const p)
+static const char *const pads[] =
{
- memset(p, 0, sizeof *p);
- p->player = player;
+ "Pad 1",
+ "Pad 2"
+};
+
+void pad_init(const int id, struct pad *const p)
+{
+ *p = (const struct pad)
+ {
+ .id = id
+ };
}
void pad_port_update(struct pad *const p)
@@ -27,7 +35,7 @@ void pad_port_update(struct pad *const p)
};
p->mask = 0;
- PSX_PollPad(p->player, &p->port.pps);
+ PSX_PollPad(p->id, &p->port.pps);
for (size_t i = 0; i < sizeof psx_keys / sizeof *psx_keys; i++)
{
@@ -39,3 +47,13 @@ void pad_port_update(struct pad *const p)
p->mask &= ~mask;
}
}
+
+size_t pad_count(void)
+{
+ return sizeof pads / sizeof *pads;
+}
+
+const char *pad_name(const int id)
+{
+ return pads[id];
+}
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);
}