ROGueENEMY/main.c
2023-11-02 13:11:02 +01:00

83 lines
No EOL
2.2 KiB
C

#include <signal.h>
#include "imu_output.h"
#include "gamepad_output.h"
output_dev_t imu_dev = {
.fd = -1,
.ctrl_mutex = PTHREAD_MUTEX_INITIALIZER,
.crtl_flags = 0x00000000U,
};
output_dev_t gamepadd_dev = {
.fd = -1,
.ctrl_mutex = PTHREAD_MUTEX_INITIALIZER,
.crtl_flags = 0x00000000U,
};
void request_termination() {
pthread_mutex_lock(&imu_dev.ctrl_mutex);
imu_dev.crtl_flags |= OUTPUT_DEV_CTRL_FLAG_EXIT;
pthread_mutex_unlock(&imu_dev.ctrl_mutex);
pthread_mutex_lock(&gamepadd_dev.ctrl_mutex);
gamepadd_dev.crtl_flags |= OUTPUT_DEV_CTRL_FLAG_EXIT;
pthread_mutex_unlock(&gamepadd_dev.ctrl_mutex);
}
void sig_handler(int signo)
{
if (signo == SIGINT) {
request_termination();
printf("received SIGINT\n");
}
}
int main(int argc, char ** argv) {
imu_dev.fd = create_imu("/dev/uinput", "Virtual IMU - ROGueENEMY");
if (imu_dev.fd < 0) {
fprintf(stderr, "Unable to create IMU virtual device\n");
return EXIT_FAILURE;
}
gamepadd_dev.fd = create_gamepad("/dev/uinput", "Virtual Controller - ROGueENEMY");
if (gamepadd_dev.fd < 0) {
close(imu_dev.fd);
fprintf(stderr, "Unable to create gamepad virtual device\n");
return EXIT_FAILURE;
}
__sighandler_t sigint_hndl = signal(SIGINT, sig_handler);
if (sigint_hndl == SIG_ERR) {
fprintf(stderr, "Error registering SIGINT handler\n");
return EXIT_FAILURE;
}
int ret = 0;
pthread_t imu_thread, gamepad_thread;
int imu_thread_creation = pthread_create(&imu_thread, NULL, imu_thread_func, (void*)(&imu_dev));
if (imu_thread_creation != 0) {
fprintf(stderr, "Error creating IMU output thread: %d\n", imu_thread_creation);
ret = -1;
request_termination();
goto imu_thread_err;
}
int gamepad_thread_creation = pthread_create(&gamepad_thread, NULL, gamepad_thread_func, (void*)(&gamepadd_dev));
if (gamepad_thread_creation != 0) {
fprintf(stderr, "Error creating gamepad output thread: %d\n", gamepad_thread_creation);
ret = -1;
request_termination();
goto gamepad_thread_err;
}
pthread_join(gamepad_thread, NULL);
gamepad_thread_err:
pthread_join(imu_thread, NULL);
imu_thread_err:
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}