From a0666cba2c430794f1f3e0de33415236a69b177e Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 13 Dec 2023 18:16:54 +0100 Subject: [PATCH] idead --- dev_in.c | 77 ++++++++++++++++++++++++++++++++++---------------------- dev_in.h | 2 -- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/dev_in.c b/dev_in.c index 99554b5..4956b18 100644 --- a/dev_in.c +++ b/dev_in.c @@ -328,6 +328,9 @@ void* dev_in_thread_func(void *ptr) { const size_t max_devices = dev_in_data->input_dev_decl->dev_count; + in_message_t* controller_msg = malloc(sizeof(in_message_t) * 4); + size_t controller_msg_avail = controller_msg == NULL ? 0 : 4; + dev_in_t* const devices = malloc(sizeof(dev_in_t) * max_devices); if (devices == NULL) { fprintf(stderr, "Unable to allocate memory to hold devices -- aborting input thread\n"); @@ -463,40 +466,54 @@ void* dev_in_thread_func(void *ptr) { continue; } - in_message_t controller_msg[MAX_IN_MESSAGES]; - size_t controller_msg_avail = sizeof(controller_msg) / sizeof(in_message_t); - int controller_msg_count = -EIO; + int controller_msg_count = -ENOMEM; + while (controller_msg_count == -ENOMEM) { + // the following part fills controller_msg and writes in controller_msg_count an error or the number of messages to be sent to the output device + if (devices[i].type == DEV_IN_TYPE_EV) { + evdev_collected_t coll = { + .ev_count = 0 + }; - // the following part fills controller_msg and writes in controller_msg_count an error or the number of messages to be sent to the output device - if (devices[i].type == DEV_IN_TYPE_EV) { - evdev_collected_t coll = { - .ev_count = 0 - }; + controller_msg_count = fill_message_from_evdev(&devices[i].dev.evdev, &coll); + if (controller_msg_count != 0) { + fprintf(stderr, "Unable to fill input_event(s) for device %zd: %d -- Will reconnect the device\n", i, controller_msg_count); + evdev_close_device(&devices[i].dev.evdev); + devices[i].type = DEV_IN_TYPE_NONE; + continue; + } - controller_msg_count = fill_message_from_evdev(&devices[i].dev.evdev, &coll); - if (controller_msg_count != 0) { - fprintf(stderr, "Unable to fill input_event(s) for device %zd: %d -- Will reconnect the device\n", i, controller_msg_count); - evdev_close_device(&devices[i].dev.evdev); - devices[i].type = DEV_IN_TYPE_NONE; - continue; + controller_msg_count = dev_in_data->input_dev_decl->dev[i]->map.ev_input_map_fn(&coll, controller_msg, controller_msg_avail, dev_in_data->input_dev_decl->dev[i]->user_data); + } else if (devices[i].type == DEV_IN_TYPE_IIO) { + controller_msg_count = map_message_from_iio(&devices[i].dev.iio, controller_msg, controller_msg_avail); + if (controller_msg_count != 0) { + fprintf(stderr, "Error in performing operations for device %zd: %d -- Will reconnect to the device\n", i, controller_msg_count); + iio_close_device(&devices[i].dev.iio); + devices[i].type = DEV_IN_TYPE_NONE; + continue; + } + } else if (devices[i].type == DEV_IN_TYPE_HIDRAW) { + controller_msg_count = dev_in_data->input_dev_decl->dev[i]->map.hidraw_input_map_fn(dev_hidraw_get_fd(devices[i].dev.hidraw.hidrawdev), controller_msg, controller_msg_avail, dev_in_data->input_dev_decl->dev[i]->user_data); + if (controller_msg_count != 0) { + fprintf(stderr, "Error in performing operations for device %zd: %d -- Will reconnect to the device\n", i, controller_msg_count); + hidraw_close_device(&devices[i].dev.hidraw); + devices[i].type = DEV_IN_TYPE_NONE; + continue; + } } - controller_msg_count = dev_in_data->input_dev_decl->dev[i]->map.ev_input_map_fn(&coll, &controller_msg[0], controller_msg_avail, dev_in_data->input_dev_decl->dev[i]->user_data); - } else if (devices[i].type == DEV_IN_TYPE_IIO) { - controller_msg_count = map_message_from_iio(&devices[i].dev.iio, &controller_msg[0], controller_msg_avail); - if (controller_msg_count != 0) { - fprintf(stderr, "Error in performing operations for device %zd: %d -- Will reconnect to the device\n", i, controller_msg_count); - iio_close_device(&devices[i].dev.iio); - devices[i].type = DEV_IN_TYPE_NONE; - continue; - } - } else if (devices[i].type == DEV_IN_TYPE_HIDRAW) { - controller_msg_count = dev_in_data->input_dev_decl->dev[i]->map.hidraw_input_map_fn(dev_hidraw_get_fd(devices[i].dev.hidraw.hidrawdev), &controller_msg[0], controller_msg_avail, dev_in_data->input_dev_decl->dev[i]->user_data); - if (controller_msg_count != 0) { - fprintf(stderr, "Error in performing operations for device %zd: %d -- Will reconnect to the device\n", i, controller_msg_count); - hidraw_close_device(&devices[i].dev.hidraw); - devices[i].type = DEV_IN_TYPE_NONE; - continue; + // attempt to acquire more memory + if (controller_msg_count == -ENOMEM) { + printf("Map function reported not enough memory, allocating new one...\n"); + const size_t tmp_controller_msg_avail = controller_msg_avail * 2; + in_message_t *tmp_controller_msg = malloc(sizeof(in_message_t) * tmp_controller_msg_avail); + if (tmp_controller_msg == NULL) { + fprintf(stderr, "Could not allocate new memory -- will retry\n"); + } else { + printf("Allocated new memory to fill the buffer\n"); + free(controller_msg); + controller_msg = tmp_controller_msg; + controller_msg_avail = tmp_controller_msg_avail; + } } } diff --git a/dev_in.h b/dev_in.h index d66c0cf..1fd470f 100644 --- a/dev_in.h +++ b/dev_in.h @@ -4,8 +4,6 @@ #include "message.h" #include "input_dev.h" -#define MAX_IN_MESSAGES 8 - #define DEV_IN_FLAG_EXIT 0x00000001U typedef struct dev_in_data {