diff --git a/Makefile b/Makefile index 671e50f..05c0c7c 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CFLAGS=-g -std=c11 -pedantic -Wall # -Werror LDFLAGS=-lpthread CC=gcc -OBJECTS=main.o gamepad_output.o imu_output.o input_dev.o output_dev.o +OBJECTS=main.o input_dev.o output_dev.o TARGET=rogue_enemy all: $(TARGET) @@ -15,4 +15,4 @@ depends: $(CC) -MM $(OBJECTS:.o=.c) > depends clean: - rm ./$(TARGET) *.o + rm -f ./$(TARGET) *.o depends diff --git a/gamepad_output.c b/gamepad_output.c deleted file mode 100644 index 39638b1..0000000 --- a/gamepad_output.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "gamepad_output.h" - -#include -#include - -void *gamepad_thread_func(void *ptr) { - output_dev_t *out_dev = (output_dev_t*)ptr; - - for (;;) { - pthread_mutex_lock(&out_dev->ctrl_mutex); - - if (out_dev->crtl_flags & OUTPUT_DEV_CTRL_FLAG_EXIT) { - pthread_mutex_unlock(&out_dev->ctrl_mutex); - break; - } - - pthread_mutex_unlock(&out_dev->ctrl_mutex); - } - - return NULL; -} \ No newline at end of file diff --git a/gamepad_output.h b/gamepad_output.h deleted file mode 100644 index 3a961be..0000000 --- a/gamepad_output.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -#include "output_dev.h" - -void *gamepad_thread_func(void *ptr); \ No newline at end of file diff --git a/imu_output.c b/imu_output.c deleted file mode 100644 index 4b0ac0a..0000000 --- a/imu_output.c +++ /dev/null @@ -1,29 +0,0 @@ -#include "imu_output.h" - -#include -#include - -void *imu_thread_func(void *ptr) { - output_dev_t *out_dev = (output_dev_t*)ptr; - - /* - int64_t secAtInit = 0; - int64_t usecAtInit = 0; - timeval now = {0}; - gettimeofday(&now, NULL); - secAtInit = now.tv_sec; - usecAtInit = now.tv_usec;*/ - - for (;;) { - pthread_mutex_lock(&out_dev->ctrl_mutex); - - if (out_dev->crtl_flags & OUTPUT_DEV_CTRL_FLAG_EXIT) { - pthread_mutex_unlock(&out_dev->ctrl_mutex); - break; - } - - pthread_mutex_unlock(&out_dev->ctrl_mutex); - } - - return NULL; -} \ No newline at end of file diff --git a/imu_output.h b/imu_output.h deleted file mode 100644 index a5f55aa..0000000 --- a/imu_output.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -#include "output_dev.h" - -void *imu_thread_func(void *ptr); \ No newline at end of file diff --git a/main.c b/main.c index a9d82f5..c7a59ae 100644 --- a/main.c +++ b/main.c @@ -1,18 +1,22 @@ #include -#include "imu_output.h" -#include "gamepad_output.h" +#include "input_dev.h" +#include "output_dev.h" output_dev_t imu_dev = { .fd = -1, .ctrl_mutex = PTHREAD_MUTEX_INITIALIZER, .crtl_flags = 0x00000000U, + .max_events = 32, + .events_list = NULL, }; output_dev_t gamepadd_dev = { .fd = -1, .ctrl_mutex = PTHREAD_MUTEX_INITIALIZER, .crtl_flags = 0x00000000U, + .max_events = 32, + .events_list = NULL, }; void request_termination() { @@ -34,8 +38,23 @@ void sig_handler(int signo) } int main(int argc, char ** argv) { + imu_dev.events_list = calloc(sizeof(struct input_event), imu_dev.max_events); + if (imu_dev.events_list == NULL) { + fprintf(stderr, "Unable to allocate events list for IMU output\n"); + return EXIT_FAILURE; + } + + imu_dev.events_list = calloc(sizeof(struct input_event), imu_dev.max_events); + if (gamepadd_dev.events_list == NULL) { + free(imu_dev.events_list); + fprintf(stderr, "Unable to allocate events list for gamepad output\n"); + return EXIT_FAILURE; + } + imu_dev.fd = create_output_dev("/dev/uinput", "Virtual IMU - ROGueENEMY", output_dev_imu); if (imu_dev.fd < 0) { + free(imu_dev.events_list); + free(gamepadd_dev.events_list); fprintf(stderr, "Unable to create IMU virtual device\n"); return EXIT_FAILURE; } @@ -57,7 +76,7 @@ int main(int argc, char ** argv) { pthread_t imu_thread, gamepad_thread; - int imu_thread_creation = pthread_create(&imu_thread, NULL, imu_thread_func, (void*)(&imu_dev)); + int imu_thread_creation = pthread_create(&imu_thread, NULL, output_dev_thread_func, (void*)(&imu_dev)); if (imu_thread_creation != 0) { fprintf(stderr, "Error creating IMU output thread: %d\n", imu_thread_creation); ret = -1; @@ -65,7 +84,7 @@ int main(int argc, char ** argv) { goto imu_thread_err; } - int gamepad_thread_creation = pthread_create(&gamepad_thread, NULL, gamepad_thread_func, (void*)(&gamepadd_dev)); + int gamepad_thread_creation = pthread_create(&gamepad_thread, NULL, output_dev_thread_func, (void*)(&gamepadd_dev)); if (gamepad_thread_creation != 0) { fprintf(stderr, "Error creating gamepad output thread: %d\n", gamepad_thread_creation); ret = -1; @@ -85,5 +104,8 @@ imu_thread_err: if (!(imu_dev.fd < 0)) close(imu_dev.fd); + free(imu_dev.events_list); + free(gamepadd_dev.events_list); + return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } \ No newline at end of file diff --git a/output_dev.c b/output_dev.c index a61195d..f459de8 100644 --- a/output_dev.c +++ b/output_dev.c @@ -130,6 +130,11 @@ int create_output_dev(const char* uinput_path, const char* name, output_dev_type break; } + case output_dev_gamepad: { + + break; + } + default: // error close(fd); @@ -140,3 +145,31 @@ int create_output_dev(const char* uinput_path, const char* name, output_dev_type create_output_dev_err: return fd; } + +void *output_dev_thread_func(void *ptr) { + output_dev_t *out_dev = (output_dev_t*)ptr; + + for (;;) { + pthread_mutex_lock(&out_dev->ctrl_mutex); + + if (out_dev->crtl_flags & OUTPUT_DEV_CTRL_FLAG_EXIT) { + pthread_mutex_unlock(&out_dev->ctrl_mutex); + break; + } else if (out_dev->crtl_flags & OUTPUT_DEV_CTRL_FLAG_DATA) { + const uint32_t events_count = out_dev->events_count; + for (uint32_t i = 0; i < events_count; ++i) { + // send the event + write(out_dev->fd, (const void*)&out_dev->events_list[i], sizeof(struct input_event)); + + out_dev->events_count -= 1; + } + + // clear out the data present flag + out_dev->crtl_flags &= ~OUTPUT_DEV_CTRL_FLAG_DATA; + } + + pthread_mutex_unlock(&out_dev->ctrl_mutex); + } + + return NULL; +} diff --git a/output_dev.h b/output_dev.h index f0a97bb..01da2f6 100644 --- a/output_dev.h +++ b/output_dev.h @@ -26,7 +26,9 @@ typedef struct output_dev { uint32_t max_events; uint32_t events_count; - struct input_event events_list; + struct input_event *events_list; } output_dev_t; -int create_output_dev(const char* uinput_path, const char* name, output_dev_type_t type); \ No newline at end of file +int create_output_dev(const char* uinput_path, const char* name, output_dev_type_t type); + +void *output_dev_thread_func(void *ptr); \ No newline at end of file diff --git a/rogue_enemy.h b/rogue_enemy.h index 25dcd0b..50dc525 100644 --- a/rogue_enemy.h +++ b/rogue_enemy.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include