Code refactor

This commit is contained in:
Denis 2023-12-06 00:32:51 +01:00
parent 89f348f7c1
commit d6e6162db5
No known key found for this signature in database
GPG key ID: DD9B63F805CF5C03
21 changed files with 1342 additions and 1251 deletions

146
main.c
View file

@ -8,8 +8,6 @@
logic_t global_logic;
static output_dev_t out_gamepadd_dev = {
.gamepad_fd = -1,
.imu_fd = -1,
.logic = &global_logic,
};
@ -68,6 +66,24 @@ static input_dev_t in_xbox_dev = {
.ev_input_filter_fn = input_filter_identity,
};
int start_input_devices(pthread_t *out_threads, input_dev_t **in_devs, uint16_t in_count) {
int ret = 0;
uint16_t i = 0;
for (i = 0; i < in_count; ++i) {
const int thread_creation = pthread_create(&out_threads[i], NULL, input_dev_thread_func, (void*)(in_devs[i]));
if (thread_creation != 0) {
fprintf(stderr, "Error creating input thread for device %d: %d\n", (int)i, thread_creation);
ret = -1;
// TODO: logic_request_termination(&global_logic);
goto start_input_devices_err;
}
}
start_input_devices_err:
return ret;
}
void sig_handler(int signo)
{
if (signo == SIGINT) {
@ -77,50 +93,35 @@ void sig_handler(int signo)
}
int main(int argc, char ** argv) {
int ret = 0;
const int logic_creation_res = logic_create(&global_logic);
if (logic_creation_res < 0) {
fprintf(stderr, "Unable to create logic: %d", logic_creation_res);
return EXIT_FAILURE;
}
int imu_fd = create_output_dev("/dev/uinput", output_dev_imu);
if (imu_fd < 0) {
fprintf(stderr, "Unable to create IMU virtual device\n");
return EXIT_FAILURE;
input_dev_t *in_devs[] = {
&in_xbox_dev,
&in_iio_dev,
&in_asus_kb_1_dev,
&in_asus_kb_2_dev,
&in_asus_kb_3_dev,
};
const uint16_t input_dev_count = (sizeof(in_devs) / sizeof(input_dev_t *));
pthread_t *out_threads = malloc(sizeof(pthread_t) * input_dev_count);
ret = start_input_devices(out_threads, in_devs, input_dev_count);
if (ret != 0) {
fprintf(stderr, "Unable to start input devices: %d -- terminating...\n", ret);
goto input_threads_err;
}
int gamepad_fd = create_output_dev("/dev/uinput", output_dev_gamepad);
if (gamepad_fd < 0) {
close(imu_fd);
fprintf(stderr, "Unable to create gamepad virtual device\n");
return EXIT_FAILURE;
}
int mouse_fd = create_output_dev("/dev/uinput", output_dev_mouse);
if (mouse_fd < 0) {
close(gamepad_fd);
close(imu_fd);
fprintf(stderr, "Unable to create mouse virtual device\n");
return EXIT_FAILURE;
}
out_gamepadd_dev.gamepad_fd = gamepad_fd;
out_gamepadd_dev.imu_fd = imu_fd;
out_gamepadd_dev.mouse_fd = mouse_fd;
/*
__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 gamepad_thread;
pthread_t xbox_thread, asus_kb_1_thread, asus_kb_2_thread, asus_kb_3_thread, iio_thread;
const int gamepad_thread_creation = pthread_create(&gamepad_thread, NULL, output_dev_thread_func, (void*)(&out_gamepadd_dev));
if (gamepad_thread_creation != 0) {
fprintf(stderr, "Error creating gamepad output thread: %d\n", gamepad_thread_creation);
@ -129,69 +130,22 @@ int main(int argc, char ** argv) {
goto gamepad_thread_err;
}
const int xbox_thread_creation = pthread_create(&xbox_thread, NULL, input_dev_thread_func, (void*)(&in_xbox_dev));
if (xbox_thread_creation != 0) {
fprintf(stderr, "Error creating xbox input thread: %d\n", xbox_thread_creation);
ret = -1;
logic_request_termination(&global_logic);
goto xbox_drv_thread_err;
/*
// TODO: once the application is able to exit de-comment this
__sighandler_t sigint_hndl = signal(SIGINT, sig_handler);
if (sigint_hndl == SIG_ERR) {
fprintf(stderr, "Error registering SIGINT handler\n");
return EXIT_FAILURE;
}
*/
for (uint16_t j = 0; j < input_dev_count; ++j) {
pthread_join(out_threads[j], NULL);
}
const int asus_kb_1_thread_creation = pthread_create(&asus_kb_1_thread, NULL, input_dev_thread_func, (void*)(&in_asus_kb_1_dev));
if (asus_kb_1_thread_creation != 0) {
fprintf(stderr, "Error creating asus keyboard (1) input thread: %d\n", asus_kb_1_thread_creation);
ret = -1;
logic_request_termination(&global_logic);
goto asus_kb_1_thread_err;
}
const int asus_kb_2_thread_creation = pthread_create(&asus_kb_2_thread, NULL, input_dev_thread_func, (void*)(&in_asus_kb_2_dev));
if (asus_kb_2_thread_creation != 0) {
fprintf(stderr, "Error creating asus keyboard (2) input thread: %d\n", asus_kb_2_thread_creation);
ret = -1;
logic_request_termination(&global_logic);
goto asus_kb_2_thread_err;
}
const int asus_kb_3_thread_creation = pthread_create(&asus_kb_3_thread, NULL, input_dev_thread_func, (void*)(&in_asus_kb_3_dev));
if (asus_kb_3_thread_creation != 0) {
fprintf(stderr, "Error creating asus keyboard (3) input thread: %d\n", asus_kb_3_thread_creation);
ret = -1;
logic_request_termination(&global_logic);
goto asus_kb_3_thread_err;
}
const int iio_thread_creation = pthread_create(&iio_thread, NULL, input_dev_thread_func, (void*)(&in_iio_dev));
if (iio_thread_creation != 0) {
fprintf(stderr, "Error creating iio input thread: %d\n", asus_kb_3_thread_creation);
ret = -1;
logic_request_termination(&global_logic);
goto iio_thread_err;
}
pthread_join(iio_thread, NULL);
iio_thread_err:
pthread_join(asus_kb_3_thread, NULL);
asus_kb_3_thread_err:
pthread_join(asus_kb_2_thread, NULL);
asus_kb_2_thread_err:
pthread_join(asus_kb_1_thread, NULL);
asus_kb_1_thread_err:
pthread_join(xbox_thread, NULL);
xbox_drv_thread_err:
pthread_join(gamepad_thread, NULL);
gamepad_thread_err:
ioctl(gamepad_fd, UI_DEV_DESTROY);
close(gamepad_fd);
// TODO: free(imu_dev.events_list);
// TODO: free(gamepadd_dev.events_list);
input_threads_err:
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}