From bdd8d563ff3f0eec41cc45d07f6c00622a531a72 Mon Sep 17 00:00:00 2001 From: nathansmith117 Date: Tue, 21 May 2024 22:51:55 -0600 Subject: first commit --- jsi/src/Makefile | 22 +++++++++++++++ jsi/src/jsi.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ jsi/src/jsi.h | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ jsi/src/jsi_win.c | 44 +++++++++++++++++++++++++++++ jsi/src/test.c | 45 ++++++++++++++++++++++++++++++ 5 files changed, 268 insertions(+) create mode 100644 jsi/src/Makefile create mode 100644 jsi/src/jsi.c create mode 100644 jsi/src/jsi.h create mode 100644 jsi/src/jsi_win.c create mode 100644 jsi/src/test.c (limited to 'jsi/src') diff --git a/jsi/src/Makefile b/jsi/src/Makefile new file mode 100644 index 0000000..34b60e7 --- /dev/null +++ b/jsi/src/Makefile @@ -0,0 +1,22 @@ +COMPILER = gcc +TARGET = ../lib/libjsi.a +OBJS = jsi.o + +%.o: %.c + @echo compiling $< + @$(COMPILER) -c -o $@ $< + +$(TARGET): $(OBJS) + @echo making static libary + @ar rcs $(TARGET) $(OBJS) +test: test.c jsi.* + @echo making test + @$(COMPILER) -o $@ $< $(TARGET) -g + +# Objects. +jsi.o: jsi.c jsi.h + +clean: + rm *.o + rm test + rm $(TARGET) diff --git a/jsi/src/jsi.c b/jsi/src/jsi.c new file mode 100644 index 0000000..1a9f44d --- /dev/null +++ b/jsi/src/jsi.c @@ -0,0 +1,83 @@ +/* + * jsi (Joystick interface) c libray +*/ + +#include "jsi.h" + +int jsi_get_joystick(int joy_num, jsi_joystick * joystick_data) { + int fd; + char dev_path[NAME_MAX]; + + // Format path. + snprintf(dev_path, sizeof(dev_path), "/dev/input/js%d", joy_num); + + fd = open(dev_path, O_RDONLY); + + if (fd == -1) + return -1; + + joystick_data->fd = fd; + joystick_data->num = joy_num; + joystick_data->joystick_count_init = jsi_joy_count(); + + // Get axes. + char num_of_axes; + + if (ioctl(fd, JSIOCGAXES, &num_of_axes) == -1) + return -1; + + joystick_data->layout.axes = (int)num_of_axes; + + // Get buttons. + char num_of_buttons; + + if (ioctl(fd, JSIOCGBUTTONS, &num_of_buttons) == -1) + return -1; + + joystick_data->layout.buttons = (int)num_of_buttons; + + // Get name. + char joy_name[JSI_NAME_MAX]; + + if (ioctl(fd, JSIOCGNAME(JSI_NAME_MAX), joy_name) == -1) + return -1; + + strncat(joystick_data->layout.name, joy_name, JSI_NAME_MAX); + + return 0; +} + +void jsi_close_joystick(jsi_joystick joystick_data) { + close(joystick_data.fd); +} + +int jsi_joy_count() { + int joy_count = 0; + char dev_path[NAME_MAX]; + struct stat dev_stat; + + while (1) { + // Format path. + snprintf(dev_path, sizeof(dev_path), "/dev/input/js%d", joy_count); + + if (stat(dev_path, &dev_stat) == -1) + return joy_count; + + joy_count++; + } + + return joy_count; +} + +int jsi_get_event(jsi_joystick joystick_data, struct jsi_event * event_data) { + struct js_event jse; + + if (read(joystick_data.fd, &jse, sizeof(jse)) == -1) + return -1; + + event_data->value = (int)jse.value; + event_data->type = (int)jse.type; + event_data->num = (int)jse.number; + + return 0; +} diff --git a/jsi/src/jsi.h b/jsi/src/jsi.h new file mode 100644 index 0000000..718b4dc --- /dev/null +++ b/jsi/src/jsi.h @@ -0,0 +1,74 @@ +/* + * jsi (Joystick interface) c libray +*/ + +#ifdef _WIN32 + +#include + +#else + +// Linux header. +#include +#include +#include + +// Unix headers. +#include +#include + +#endif + +// OS headers. +#include +#include +#include + +// C. +#include +#include +#include +#include +#include + +#ifndef JSI_H +#define JSI_H + +#define JSI_VERSION "Still in making" + +#define JSI_TRUE 1 +#define JSI_FALSE 0 +#define JSI_NAME_MAX 128 + +#define JSI_UP 0x0 +#define JSI_DOWN 0x1 + +#define JSI_TYPE_BUTTON 0x1 +#define JSI_TYPE_AXIS 0x2 +#define JSI_TYPE_INIT 0x80 + +struct jsi_joy_layout { + int axes; + int buttons; + char name[JSI_NAME_MAX]; +}; + +typedef struct { + int fd; + int num; + int joystick_count_init; + struct jsi_joy_layout layout; +} jsi_joystick; + +struct jsi_event { + int value; + int type; + int num; +}; + +int jsi_get_joystick(int joy_num, jsi_joystick * joystick_data); +void jsi_close_joystick(jsi_joystick joystick_data); +int jsi_joy_count(); +int jsi_get_event(jsi_joystick joystick_data, struct jsi_event * event_data); + +#endif // JSI_H diff --git a/jsi/src/jsi_win.c b/jsi/src/jsi_win.c new file mode 100644 index 0000000..17d91b5 --- /dev/null +++ b/jsi/src/jsi_win.c @@ -0,0 +1,44 @@ +/* + * jsi (Joystick interface) c libray +*/ + +#include "jsi.h" + +int jsi_get_joystick(int joy_num, jsi_joystick * joystick_data) { + DWORD dw_result; + + switch (joy_num) { + case 1: + dw_result = joySetCapture(GetDesktopWindow(), JOYSTICKID1, 0, FALSE); + break; + case 2: + dw_result = joySetCapture(GetDesktopWindow(), JOYSTICKID2, 0, FALSE); + break; + default: + return -1; + } + + // Check for errors. + switch (dw_result) { + case JOYERR_UNPLUGGED: + return -1; + case MMSYSERR_NODRIVER: + return -1; + case JOYERR_NOCANDO: + return -1; + default: + break; + } + + return 0; +} + +void jsi_close_joystick(jsi_joystick joystick_data) {} + +int jsi_joy_count() { + return (int)joyGetNumDevs(); +} + +int jsi_get_event(jsi_joystick joystick_data, struct jsi_event * event_data) { + return -1; +} diff --git a/jsi/src/test.c b/jsi/src/test.c new file mode 100644 index 0000000..fa34b01 --- /dev/null +++ b/jsi/src/test.c @@ -0,0 +1,45 @@ +/* + * jsi (Joystick interface) c libray +*/ + +#include "jsi.h" +#include +#include + +jsi_joystick a_joystick; + +void handle_sig(int sig) { + jsi_close_joystick(a_joystick); + putchar('\n'); + exit(0); +} + +int main() { + signal(SIGINT, handle_sig); + + puts("test"); + printf("joystick count: %d\n", jsi_joy_count()); + + if (jsi_get_joystick(0, &a_joystick) == -1) { + perror("Couldn't get joystick"); + return -1; + } + + printf("axes: %d, buttons: %d, name: %s\n", a_joystick.layout.axes, + a_joystick.layout.buttons, a_joystick.layout.name); + + struct jsi_event a_event; + + while (JSI_TRUE) { + + if (jsi_get_event(a_joystick, &a_event) == -1) { + perror("Error getting input"); + return -1; + } + + printf("value: %hx, type: %hx, num: %hx\n", a_event.value, a_event.type, a_event.num); + } + + jsi_close_joystick(a_joystick); + return 0; +} -- cgit v1.2.3