aboutsummaryrefslogtreecommitdiffstats
path: root/jsi
diff options
context:
space:
mode:
authornathansmith117 <nathansmith117@sdf.org>2024-05-22 04:51:55 +0000
committernathansmith117 <nathansmith117@sdf.org>2024-05-22 04:51:55 +0000
commitbdd8d563ff3f0eec41cc45d07f6c00622a531a72 (patch)
treeceb9cac325b893b20b5d0303e988252136680e03 /jsi
downloadforgorttonProjects-bdd8d563ff3f0eec41cc45d07f6c00622a531a72.tar.gz
forgorttonProjects-bdd8d563ff3f0eec41cc45d07f6c00622a531a72.tar.bz2
forgorttonProjects-bdd8d563ff3f0eec41cc45d07f6c00622a531a72.zip
first commitHEADmain
Diffstat (limited to 'jsi')
-rw-r--r--jsi/Makefile4
-rw-r--r--jsi/README.md2
-rw-r--r--jsi/src/Makefile22
-rw-r--r--jsi/src/jsi.c83
-rw-r--r--jsi/src/jsi.h74
-rw-r--r--jsi/src/jsi_win.c44
-rw-r--r--jsi/src/test.c45
7 files changed, 274 insertions, 0 deletions
diff --git a/jsi/Makefile b/jsi/Makefile
new file mode 100644
index 0000000..6a2adaf
--- /dev/null
+++ b/jsi/Makefile
@@ -0,0 +1,4 @@
+all:
+ @make -C src
+%:
+ @make -C src $@
diff --git a/jsi/README.md b/jsi/README.md
new file mode 100644
index 0000000..b174a19
--- /dev/null
+++ b/jsi/README.md
@@ -0,0 +1,2 @@
+# jsi
+There is a lack of crossplatform joystick librarys in plain c so I decided to write my own but I never finished.
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 <Windows.h>
+
+#else
+
+// Linux header.
+#include <linux/types.h>
+#include <linux/input.h>
+#include <linux/joystick.h>
+
+// Unix headers.
+#include <fcntl.h>
+#include <unistd.h>
+
+#endif
+
+// OS headers.
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+// C.
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <limits.h>
+
+#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 <stdio.h>
+#include <signal.h>
+
+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;
+}