From f9f5b67690c4f89b53b8ed16c68bd7dc35d62906 Mon Sep 17 00:00:00 2001 From: Denis Date: Sun, 17 Dec 2023 00:05:09 +0100 Subject: [PATCH] use hidraw dev to change leds --- dev_in.c | 23 +++++-- input_dev.h | 10 ++-- legion_go.c | 4 +- rog_ally.c | 170 ++++++++++++++++++++++++---------------------------- 4 files changed, 103 insertions(+), 104 deletions(-) diff --git a/dev_in.c b/dev_in.c index 6b62f22..8db5a0c 100644 --- a/dev_in.c +++ b/dev_in.c @@ -23,6 +23,8 @@ typedef struct dev_in_iio { typedef struct dev_in_hidraw { dev_hidraw_t *hidrawdev; + + hidraw_callbacks_t callbacks; } dev_in_hidraw_t; typedef struct dev_in_ev { @@ -266,7 +268,7 @@ static void hidraw_close_device(dev_in_hidraw_t *const out_hidraw) { dev_hidraw_close(out_hidraw->hidrawdev); } -static void handle_rumble_device(dev_in_ev_t *const in_dev, const out_message_rumble_t *const in_rumble_msg) { +static void handle_rumble_device(const dev_in_settings_t *const conf, dev_in_ev_t *const in_dev, const out_message_rumble_t *const in_rumble_msg) { if (!in_dev->has_rumble_support) { return; } @@ -315,10 +317,18 @@ static void handle_rumble_device(dev_in_ev_t *const in_dev, const out_message_ru } } -static void handle_rumble(dev_in_t *const in_devs, size_t in_devs_count, const out_message_rumble_t *const in_rumble_msg) { +static void handle_rumble(const dev_in_settings_t *const conf, dev_in_t *const in_devs, size_t in_devs_count, const out_message_rumble_t *const in_rumble_msg) { for (size_t i = 0; i < in_devs_count; ++i) { if (in_devs[i].type == DEV_IN_TYPE_EV) { - handle_rumble_device(&in_devs[i].dev.evdev, in_rumble_msg); + handle_rumble_device(conf, &in_devs[i].dev.evdev, in_rumble_msg); + } + } +} + +static void handle_leds(const dev_in_settings_t *const conf, dev_in_t *const in_devs, size_t in_devs_count, const out_message_leds_t *const in_rumble_msg) { + for (size_t i = 0; i < in_devs_count; ++i) { + if (in_devs[i].type == DEV_IN_TYPE_HIDRAW) { + } } } @@ -443,6 +453,7 @@ void* dev_in_thread_func(void *ptr) { &devices[i].dev.hidraw ); if (open_res == 0) { + devices[i].dev.hidraw.callbacks = dev_in_data->input_dev_decl->dev[i]->map.hidraw_callbacks; devices[i].type = DEV_IN_TYPE_HIDRAW; } } @@ -473,7 +484,7 @@ void* dev_in_thread_func(void *ptr) { const ssize_t out_message_pipe_read_res = read(out_message_fd, (void*)&out_msg, sizeof(out_message_t)); if (out_message_pipe_read_res == sizeof(out_message_t)) { if (out_msg.type == OUT_MSG_TYPE_RUMBLE) { - handle_rumble(devices, max_devices, &out_msg.data.rumble); + handle_rumble(&dev_in_data->settings, devices, max_devices, &out_msg.data.rumble); } else if (out_msg.type == OUT_MSG_TYPE_LEDS) { // first inform the platform const int platform_leds_res = dev_in_data->input_dev_decl->leds_fn( @@ -485,7 +496,7 @@ void* dev_in_thread_func(void *ptr) { fprintf(stderr, "Error in changing platform LEDs: %d\n", platform_leds_res); } - // TODO: handle_leds() + handle_leds(&dev_in_data->settings, devices, max_devices, &out_msg.data.leds); } } else { fprintf(stderr, "Error reading from out_message_pipe_fd: got %zu bytes, expected %zu bytes\n", out_message_pipe_read_res, sizeof(out_message_t)); @@ -553,7 +564,7 @@ void* dev_in_thread_func(void *ptr) { 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( + controller_msg_count = dev_in_data->input_dev_decl->dev[i]->map.hidraw_callbacks.map_callback( &dev_in_data->settings, dev_hidraw_get_fd(devices[i].dev.hidraw.hidrawdev), &controller_msg[0], diff --git a/input_dev.h b/input_dev.h index e647197..377c689 100644 --- a/input_dev.h +++ b/input_dev.h @@ -19,7 +19,6 @@ typedef struct evdev_collected { * constructed from that data. */ typedef int (*ev_map)(const dev_in_settings_t *const conf, const evdev_collected_t *const e, in_message_t *const messages, size_t messages_len, void* user_data); -typedef int (*hidraw_map)(const dev_in_settings_t *const conf, int hidraw_fd, in_message_t *const messages, size_t messages_len, void* user_data); typedef enum input_dev_type { input_dev_type_uinput, @@ -41,13 +40,14 @@ typedef struct iio_filters { const char name[256]; } iio_filters_t; -typedef int (*hidraw_set_leds)(uint8_t r, uint8_t g, uint8_t b, void* user_data); - -typedef int (*hidraw_rumble)(uint8_t left_motor, uint8_t right_motor, void* user_data); +typedef int (*hidraw_map)(const dev_in_settings_t *const conf, int hidraw_fd, in_message_t *const messages, size_t messages_len, void* user_data); +typedef int (*hidraw_set_leds)(const dev_in_settings_t *const conf, int hidraw_fd, uint8_t r, uint8_t g, uint8_t b, void* user_data); +typedef int (*hidraw_rumble)(const dev_in_settings_t *const conf, int hidraw_fd, uint8_t left_motor, uint8_t right_motor, void* user_data); typedef struct hidraw_callbacks { hidraw_set_leds leds_callback; hidraw_rumble rumble_callback; + hidraw_map map_callback; } hidraw_callbacks_t; typedef struct iio_settings { @@ -69,7 +69,7 @@ typedef struct input_dev { union input_dev_map { iio_settings_t iio_settings; ev_map ev_input_map_fn; - hidraw_map hidraw_input_map_fn; + hidraw_callbacks_t hidraw_callbacks; } map; } input_dev_t; diff --git a/legion_go.c b/legion_go.c index 9852643..6f15eb0 100644 --- a/legion_go.c +++ b/legion_go.c @@ -82,7 +82,9 @@ static input_dev_t in_hidraw_dev = { }, .user_data = (void*)&llg_hidraw_user_data, .map = { - .hidraw_input_map_fn = llg_hidraw_map, + .hidraw_callbacks = { + .map_callback = llg_hidraw_map, + } }, }; diff --git a/rog_ally.c b/rog_ally.c index 87ac317..8b201aa 100644 --- a/rog_ally.c +++ b/rog_ally.c @@ -6,6 +6,26 @@ #include #include +enum rc71l_leds_mode { + ROG_ALLY_MODE_STATIC = 0, + ROG_ALLY_MODE_BREATHING = 1, + ROG_ALLY_MODE_COLOR_CYCLE = 2, + ROG_ALLY_MODE_RAINBOW = 3, + ROG_ALLY_MODE_STROBING = 10, + ROG_ALLY_MODE_DIRECT = 0xFF, +} rc71l_leds_mode_t; + +enum rc71l_leds_speed { + ROG_ALLY_SPEED_MIN = 0xE1, + ROG_ALLY_SPEED_MED = 0xEB, + ROG_ALLY_SPEED_MAX = 0xF5 +} rc71l_leds_speed_t; + +enum rc71l_leds_direction { + ROG_ALLY_DIRECTION_RIGHT = 0x00, + ROG_ALLY_DIRECTION_LEFT = 0x01 +} rc71l_leds_direction_t; + typedef enum rc71l_platform_mode { rc71l_platform_mode_hidraw, rc71l_platform_mode_linux_and_asusctl, @@ -297,12 +317,6 @@ static int asus_kbd_ev_map( return written_msg; } -static hidraw_filters_t n_key_hidraw_filters = { - .pid = 0x1abe, - .vid = 0x0b05, - .rdesc_size = 167, // 48 83 167 -}; - static input_dev_t in_iio_dev = { .dev_type = input_dev_type_iio, .filters = { @@ -385,25 +399,63 @@ static input_dev_t in_xbox_dev = { } }; -enum rc71l_leds_mode { - ROG_ALLY_MODE_STATIC = 0, - ROG_ALLY_MODE_BREATHING = 1, - ROG_ALLY_MODE_COLOR_CYCLE = 2, - ROG_ALLY_MODE_RAINBOW = 3, - ROG_ALLY_MODE_STROBING = 10, - ROG_ALLY_MODE_DIRECT = 0xFF, -} rc71l_leds_mode_t; +static int rc71l_hidraw_map(const dev_in_settings_t *const conf, int hidraw_fd, in_message_t *const messages, size_t messages_len, void* user_data) { + return 0; +} -enum rc71l_leds_speed { - ROG_ALLY_SPEED_MIN = 0xE1, - ROG_ALLY_SPEED_MED = 0xEB, - ROG_ALLY_SPEED_MAX = 0xF5 -} rc71l_leds_speed_t; +static int rc71l_hidraw_rumble(const dev_in_settings_t *const conf, int hidraw_fd, uint8_t left_motor, uint8_t right_motor, void* user_data) { + return 0; +} -enum rc71l_leds_direction { - ROG_ALLY_DIRECTION_RIGHT = 0x00, - ROG_ALLY_DIRECTION_LEFT = 0x01 -} rc71l_leds_direction_t; +static int rc71l_hidraw_set_leds(const dev_in_settings_t *const conf, int hidraw_fd, uint8_t r, uint8_t g, uint8_t b, void* user_data) { + const uint8_t brightness_buf[] = { + 0x5A, 0xBA, 0xC5, 0xC4, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + const uint8_t colors_buf[] = { + 0x5A, 0xB3, 0x00, ROG_ALLY_MODE_STATIC, r, g, b, 0x00, ROG_ALLY_DIRECTION_RIGHT, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + if (write(hidraw_fd, brightness_buf, sizeof(brightness_buf)) != 64) { + fprintf(stderr, "Unable to send LEDs brightness (1) command change: %d\n", errno); + goto rc71l_hidraw_set_leds_err; + } + + if (write(hidraw_fd, colors_buf, sizeof(colors_buf)) != 64) { + fprintf(stderr, "Unable to send LEDs color command change (1)\n"); + goto rc71l_hidraw_set_leds_err; + } + + return 0; + +rc71l_hidraw_set_leds_err: + return -EIO; +} + +static input_dev_t nkey_dev = { + .dev_type = input_dev_type_hidraw, + .filters = { + .hidraw = { + .pid = 0x1abe, + .vid = 0x0b05, + .rdesc_size = 167, // 48 83 167 + } + }, + .user_data = NULL, + .map = { + .hidraw_callbacks = { + .leds_callback = rc71l_hidraw_set_leds, + .rumble_callback = rc71l_hidraw_rumble, + .map_callback = rc71l_hidraw_map, + } + } +}; static int rc71l_platform_init(const dev_in_settings_t *const conf, void** platform_data) { int res = -EINVAL; @@ -420,22 +472,6 @@ static int rc71l_platform_init(const dev_in_settings_t *const conf, void** platf // setup asus keyboard(s) user_data asus_userdata.udev = udev_new(); - res = dev_hidraw_open(&n_key_hidraw_filters, &platform->platform.hidraw); - if (res != 0) { - fprintf(stderr, "Unable to open the ROG ally hidraw main device...\n"); - free(*platform_data); - *platform_data = NULL; - goto rc71l_platform_init_err; - } - - platform->platform_mode = rc71l_platform_mode_hidraw; - - const int fd = dev_hidraw_get_fd(platform->platform.hidraw); - - platform->mode = 0; - platform->modes_count = 2; - printf("ROG Ally platform will be managed over hidraw. I'm sorry fluke.\n"); - rc71l_platform_init_err: return res; } @@ -454,58 +490,7 @@ static void rc71l_platform_deinit(const dev_in_settings_t *const conf, void** pl } static int rc71l_platform_leds(const dev_in_settings_t *const conf, uint8_t r, uint8_t g, uint8_t b, void* platform_data) { - rc71l_platform_t *const platform = (rc71l_platform_t *)platform_data; - if (platform == NULL) { - return -EINVAL; - } - - const uint8_t brightness_buf[] = { - 0x5A, 0xBA, 0xC5, 0xC4, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - }; - - uint8_t colors_buf[] = { - 0x5A, 0xB3, 0x00, ROG_ALLY_MODE_STATIC, r, g, b, 0x00, ROG_ALLY_DIRECTION_RIGHT, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - }; - - if ((platform->platform_mode == rc71l_platform_mode_hidraw) && (platform->platform.hidraw != NULL)) { - int fd = dev_hidraw_get_fd(platform->platform.hidraw); - - if (write(fd, brightness_buf, sizeof(brightness_buf)) != 64) { - fprintf(stderr, "Unable to send LEDs brightness (1) command change -- attempt to reacquire hidraw\n"); - - dev_hidraw_close(platform->platform.hidraw); - - const int reopen_res = dev_hidraw_open(&n_key_hidraw_filters, &platform->platform.hidraw); - if (reopen_res != 0) { - fprintf(stderr, "Unable to (re)open the ROG ally hidraw main device...\n"); - goto rc71l_platform_leds_err; - } else { - printf("ROG ally hidraw main device reacquired\n"); - fd = dev_hidraw_get_fd(platform->platform.hidraw); - - if (write(fd, brightness_buf, sizeof(brightness_buf)) != 64) { - fprintf(stderr, "Unable to send LEDs brightness (1) command change after hidraw reset -- giving up\n"); - goto rc71l_platform_leds_err; - } - } - } - - if (write(fd, colors_buf, sizeof(colors_buf)) != 64) { - fprintf(stderr, "Unable to send LEDs color command change (1)\n"); - goto rc71l_platform_leds_err; - } - - return 0; - } - -rc71l_platform_leds_err: - return -EINVAL; + return 0; } input_dev_composite_t rc71l_composite = { @@ -515,8 +500,9 @@ input_dev_composite_t rc71l_composite = { &in_asus_kb_1_dev, &in_asus_kb_2_dev, &in_asus_kb_3_dev, + &nkey_dev, }, - .dev_count = 5, + .dev_count = 6, .init_fn = rc71l_platform_init, .deinit_fn = rc71l_platform_deinit, .leds_fn = rc71l_platform_leds,