Rename hid_event to hid_input

The sc_hid_event structure represents HID input data. Rename it so that
we can add other hid event structs without confusion.

PR #5270 <https://github.com/Genymobile/scrcpy/pull/5270>
This commit is contained in:
Romain Vimont 2024-09-06 23:08:08 +02:00
parent 6e9b0d7d4c
commit f6219d2640
11 changed files with 116 additions and 115 deletions

View File

@ -7,7 +7,7 @@
#define SC_HID_MAX_SIZE 8
struct sc_hid_event {
struct sc_hid_input {
uint16_t hid_id;
uint8_t data[SC_HID_MAX_SIZE];
uint8_t size;

View File

@ -21,7 +21,7 @@
// keyboard support, though OS could support more keys via modifying the report
// desc. 6 should be enough for scrcpy.
#define SC_HID_KEYBOARD_MAX_KEYS 6
#define SC_HID_KEYBOARD_EVENT_SIZE \
#define SC_HID_KEYBOARD_INPUT_SIZE \
(SC_HID_KEYBOARD_INDEX_KEYS + SC_HID_KEYBOARD_MAX_KEYS)
#define SC_HID_RESERVED 0x00
@ -125,7 +125,7 @@ const size_t SC_HID_KEYBOARD_REPORT_DESC_LEN =
sizeof(SC_HID_KEYBOARD_REPORT_DESC);
/**
* A keyboard HID event is 8 bytes long:
* A keyboard HID input report is 8 bytes long:
*
* - byte 0: modifiers (1 flag per modifier key, 8 possible modifier keys)
* - byte 1: reserved (always 0)
@ -199,11 +199,11 @@ const size_t SC_HID_KEYBOARD_REPORT_DESC_LEN =
*/
static void
sc_hid_keyboard_event_init(struct sc_hid_event *hid_event) {
hid_event->hid_id = SC_HID_ID_KEYBOARD;
hid_event->size = SC_HID_KEYBOARD_EVENT_SIZE;
sc_hid_keyboard_input_init(struct sc_hid_input *hid_input) {
hid_input->hid_id = SC_HID_ID_KEYBOARD;
hid_input->size = SC_HID_KEYBOARD_INPUT_SIZE;
uint8_t *data = hid_event->data;
uint8_t *data = hid_input->data;
data[SC_HID_KEYBOARD_INDEX_MODS] = SC_HID_MOD_NONE;
data[1] = SC_HID_RESERVED;
@ -251,8 +251,8 @@ scancode_is_modifier(enum sc_scancode scancode) {
}
bool
sc_hid_keyboard_event_from_key(struct sc_hid_keyboard *hid,
struct sc_hid_event *hid_event,
sc_hid_keyboard_generate_input_from_key(struct sc_hid_keyboard *hid,
struct sc_hid_input *hid_input,
const struct sc_key_event *event) {
enum sc_scancode scancode = event->scancode;
assert(scancode >= 0);
@ -265,7 +265,7 @@ sc_hid_keyboard_event_from_key(struct sc_hid_keyboard *hid,
return false;
}
sc_hid_keyboard_event_init(hid_event);
sc_hid_keyboard_input_init(hid_input);
uint16_t mods = sc_hid_mod_from_sdl_keymod(event->mods_state);
@ -276,9 +276,9 @@ sc_hid_keyboard_event_from_key(struct sc_hid_keyboard *hid,
hid->keys[scancode] ? "true" : "false");
}
hid_event->data[SC_HID_KEYBOARD_INDEX_MODS] = mods;
hid_input->data[SC_HID_KEYBOARD_INDEX_MODS] = mods;
uint8_t *keys_data = &hid_event->data[SC_HID_KEYBOARD_INDEX_KEYS];
uint8_t *keys_data = &hid_input->data[SC_HID_KEYBOARD_INDEX_KEYS];
// Re-calculate pressed keys every time
int keys_pressed_count = 0;
for (int i = 0; i < SC_HID_KEYBOARD_KEYS; ++i) {
@ -309,7 +309,7 @@ end:
}
bool
sc_hid_keyboard_event_from_mods(struct sc_hid_event *event,
sc_hid_keyboard_generate_input_from_mods(struct sc_hid_input *hid_input,
uint16_t mods_state) {
bool capslock = mods_state & SC_MOD_CAPS;
bool numlock = mods_state & SC_MOD_NUM;
@ -318,15 +318,15 @@ sc_hid_keyboard_event_from_mods(struct sc_hid_event *event,
return false;
}
sc_hid_keyboard_event_init(event);
sc_hid_keyboard_input_init(hid_input);
unsigned i = 0;
if (capslock) {
event->data[SC_HID_KEYBOARD_INDEX_KEYS + i] = SC_SCANCODE_CAPSLOCK;
hid_input->data[SC_HID_KEYBOARD_INDEX_KEYS + i] = SC_SCANCODE_CAPSLOCK;
++i;
}
if (numlock) {
event->data[SC_HID_KEYBOARD_INDEX_KEYS + i] = SC_SCANCODE_NUMLOCK;
hid_input->data[SC_HID_KEYBOARD_INDEX_KEYS + i] = SC_SCANCODE_NUMLOCK;
++i;
}

View File

@ -39,12 +39,12 @@ void
sc_hid_keyboard_init(struct sc_hid_keyboard *hid);
bool
sc_hid_keyboard_event_from_key(struct sc_hid_keyboard *hid,
struct sc_hid_event *hid_event,
sc_hid_keyboard_generate_input_from_key(struct sc_hid_keyboard *hid,
struct sc_hid_input *hid_input,
const struct sc_key_event *event);
bool
sc_hid_keyboard_event_from_mods(struct sc_hid_event *event,
sc_hid_keyboard_generate_input_from_mods(struct sc_hid_input *hid_input,
uint16_t mods_state);
#endif

View File

@ -2,7 +2,7 @@
// 1 byte for buttons + padding, 1 byte for X position, 1 byte for Y position,
// 1 byte for wheel motion
#define SC_HID_MOUSE_EVENT_SIZE 4
#define SC_HID_MOUSE_INPUT_SIZE 4
/**
* Mouse descriptor from the specification:
@ -84,7 +84,7 @@ const size_t SC_HID_MOUSE_REPORT_DESC_LEN =
sizeof(SC_HID_MOUSE_REPORT_DESC);
/**
* A mouse HID event is 4 bytes long:
* A mouse HID input report is 4 bytes long:
*
* - byte 0: buttons state
* - byte 1: relative x motion (signed byte from -127 to 127)
@ -125,11 +125,10 @@ const size_t SC_HID_MOUSE_REPORT_DESC_LEN =
*/
static void
sc_hid_mouse_event_init(struct sc_hid_event *hid_event) {
hid_event->hid_id = SC_HID_ID_MOUSE;
hid_event->size = SC_HID_MOUSE_EVENT_SIZE;
// Leave hid_event->data uninitialized, it will be fully initialized by
// callers
sc_hid_mouse_input_init(struct sc_hid_input *hid_input) {
hid_input->hid_id = SC_HID_ID_MOUSE;
hid_input->size = SC_HID_MOUSE_INPUT_SIZE;
// Leave ->data uninitialized, it will be fully initialized by callers
}
static uint8_t
@ -154,11 +153,11 @@ sc_hid_buttons_from_buttons_state(uint8_t buttons_state) {
}
void
sc_hid_mouse_event_from_motion(struct sc_hid_event *hid_event,
sc_hid_mouse_generate_input_from_motion(struct sc_hid_input *hid_input,
const struct sc_mouse_motion_event *event) {
sc_hid_mouse_event_init(hid_event);
sc_hid_mouse_input_init(hid_input);
uint8_t *data = hid_event->data;
uint8_t *data = hid_input->data;
data[0] = sc_hid_buttons_from_buttons_state(event->buttons_state);
data[1] = CLAMP(event->xrel, -127, 127);
data[2] = CLAMP(event->yrel, -127, 127);
@ -166,11 +165,11 @@ sc_hid_mouse_event_from_motion(struct sc_hid_event *hid_event,
}
void
sc_hid_mouse_event_from_click(struct sc_hid_event *hid_event,
sc_hid_mouse_generate_input_from_click(struct sc_hid_input *hid_input,
const struct sc_mouse_click_event *event) {
sc_hid_mouse_event_init(hid_event);
sc_hid_mouse_input_init(hid_input);
uint8_t *data = hid_event->data;
uint8_t *data = hid_input->data;
data[0] = sc_hid_buttons_from_buttons_state(event->buttons_state);
data[1] = 0; // no x motion
data[2] = 0; // no y motion
@ -178,11 +177,11 @@ sc_hid_mouse_event_from_click(struct sc_hid_event *hid_event,
}
void
sc_hid_mouse_event_from_scroll(struct sc_hid_event *hid_event,
sc_hid_mouse_generate_input_from_scroll(struct sc_hid_input *hid_input,
const struct sc_mouse_scroll_event *event) {
sc_hid_mouse_event_init(hid_event);
sc_hid_mouse_input_init(hid_input);
uint8_t *data = hid_event->data;
uint8_t *data = hid_input->data;
data[0] = 0; // buttons state irrelevant (and unknown)
data[1] = 0; // no x motion
data[2] = 0; // no y motion

View File

@ -14,15 +14,15 @@ extern const uint8_t SC_HID_MOUSE_REPORT_DESC[];
extern const size_t SC_HID_MOUSE_REPORT_DESC_LEN;
void
sc_hid_mouse_event_from_motion(struct sc_hid_event *hid_event,
sc_hid_mouse_generate_input_from_motion(struct sc_hid_input *hid_input,
const struct sc_mouse_motion_event *event);
void
sc_hid_mouse_event_from_click(struct sc_hid_event *hid_event,
sc_hid_mouse_generate_input_from_click(struct sc_hid_input *hid_input,
const struct sc_mouse_click_event *event);
void
sc_hid_mouse_event_from_scroll(struct sc_hid_event *hid_event,
sc_hid_mouse_generate_input_from_scroll(struct sc_hid_input *hid_input,
const struct sc_mouse_scroll_event *event);
#endif

View File

@ -11,14 +11,14 @@
static void
sc_keyboard_uhid_send_input(struct sc_keyboard_uhid *kb,
const struct sc_hid_event *event) {
const struct sc_hid_input *hid_input) {
struct sc_control_msg msg;
msg.type = SC_CONTROL_MSG_TYPE_UHID_INPUT;
msg.uhid_input.id = event->hid_id;
msg.uhid_input.id = hid_input->hid_id;
assert(event->size <= SC_HID_MAX_SIZE);
memcpy(msg.uhid_input.data, event->data, event->size);
msg.uhid_input.size = event->size;
assert(hid_input->size <= SC_HID_MAX_SIZE);
memcpy(msg.uhid_input.data, hid_input->data, hid_input->size);
msg.uhid_input.size = hid_input->size;
if (!sc_controller_push_msg(kb->controller, &msg)) {
LOGE("Could not send UHID_INPUT message (key)");
@ -37,14 +37,14 @@ sc_keyboard_uhid_synchronize_mod(struct sc_keyboard_uhid *kb) {
// or HID output anyway
kb->device_mod = mod;
struct sc_hid_event hid_event;
if (!sc_hid_keyboard_event_from_mods(&hid_event, diff)) {
struct sc_hid_input hid_input;
if (!sc_hid_keyboard_generate_input_from_mods(&hid_input, diff)) {
return;
}
LOGV("HID keyboard state synchronized");
sc_keyboard_uhid_send_input(kb, &hid_event);
sc_keyboard_uhid_send_input(kb, &hid_input);
}
}
@ -64,10 +64,10 @@ sc_key_processor_process_key(struct sc_key_processor *kp,
struct sc_keyboard_uhid *kb = DOWNCAST(kp);
struct sc_hid_event hid_event;
struct sc_hid_input hid_input;
// Not all keys are supported, just ignore unsupported keys
if (sc_hid_keyboard_event_from_key(&kb->hid, &hid_event, event)) {
if (sc_hid_keyboard_generate_input_from_key(&kb->hid, &hid_input, event)) {
if (event->scancode == SC_SCANCODE_CAPSLOCK) {
kb->device_mod ^= SC_MOD_CAPS;
} else if (event->scancode == SC_SCANCODE_NUMLOCK) {
@ -77,7 +77,7 @@ sc_key_processor_process_key(struct sc_key_processor *kp,
// change the modifiers)
sc_keyboard_uhid_synchronize_mod(kb);
}
sc_keyboard_uhid_send_input(kb, &hid_event);
sc_keyboard_uhid_send_input(kb, &hid_input);
}
}

View File

@ -9,14 +9,15 @@
static void
sc_mouse_uhid_send_input(struct sc_mouse_uhid *mouse,
const struct sc_hid_event *event, const char *name) {
const struct sc_hid_input *hid_input,
const char *name) {
struct sc_control_msg msg;
msg.type = SC_CONTROL_MSG_TYPE_UHID_INPUT;
msg.uhid_input.id = event->hid_id;
msg.uhid_input.id = hid_input->hid_id;
assert(event->size <= SC_HID_MAX_SIZE);
memcpy(msg.uhid_input.data, event->data, event->size);
msg.uhid_input.size = event->size;
assert(hid_input->size <= SC_HID_MAX_SIZE);
memcpy(msg.uhid_input.data, hid_input->data, hid_input->size);
msg.uhid_input.size = hid_input->size;
if (!sc_controller_push_msg(mouse->controller, &msg)) {
LOGE("Could not send UHID_INPUT message (%s)", name);
@ -28,10 +29,10 @@ sc_mouse_processor_process_mouse_motion(struct sc_mouse_processor *mp,
const struct sc_mouse_motion_event *event) {
struct sc_mouse_uhid *mouse = DOWNCAST(mp);
struct sc_hid_event hid_event;
sc_hid_mouse_event_from_motion(&hid_event, event);
struct sc_hid_input hid_input;
sc_hid_mouse_generate_input_from_motion(&hid_input, event);
sc_mouse_uhid_send_input(mouse, &hid_event, "mouse motion");
sc_mouse_uhid_send_input(mouse, &hid_input, "mouse motion");
}
static void
@ -39,10 +40,10 @@ sc_mouse_processor_process_mouse_click(struct sc_mouse_processor *mp,
const struct sc_mouse_click_event *event) {
struct sc_mouse_uhid *mouse = DOWNCAST(mp);
struct sc_hid_event hid_event;
sc_hid_mouse_event_from_click(&hid_event, event);
struct sc_hid_input hid_input;
sc_hid_mouse_generate_input_from_click(&hid_input, event);
sc_mouse_uhid_send_input(mouse, &hid_event, "mouse click");
sc_mouse_uhid_send_input(mouse, &hid_input, "mouse click");
}
static void
@ -50,10 +51,10 @@ sc_mouse_processor_process_mouse_scroll(struct sc_mouse_processor *mp,
const struct sc_mouse_scroll_event *event) {
struct sc_mouse_uhid *mouse = DOWNCAST(mp);
struct sc_hid_event hid_event;
sc_hid_mouse_event_from_scroll(&hid_event, event);
struct sc_hid_input hid_input;
sc_hid_mouse_generate_input_from_scroll(&hid_input, event);
sc_mouse_uhid_send_input(mouse, &hid_event, "mouse scroll");
sc_mouse_uhid_send_input(mouse, &hid_input, "mouse scroll");
}
bool

View File

@ -20,14 +20,14 @@
#define SC_AOA_EVENT_QUEUE_LIMIT 60
static void
sc_hid_event_log(const struct sc_hid_event *event) {
// HID Event: [00] FF FF FF FF...
assert(event->size);
char *hex = sc_str_to_hex_string(event->data, event->size);
sc_hid_input_log(const struct sc_hid_input *hid_input) {
// HID input: [00] FF FF FF FF...
assert(hid_input->size);
char *hex = sc_str_to_hex_string(hid_input->data, hid_input->size);
if (!hex) {
return;
}
LOGV("HID Event: [%" PRIu16 "] %s", event->hid_id, hex);
LOGV("HID input: [%" PRIu16 "] %s", hid_input->hid_id, hex);
free(hex);
}
@ -129,16 +129,17 @@ sc_aoa_set_hid_report_desc(struct sc_aoa *aoa, uint16_t accessory_id,
}
static bool
sc_aoa_send_hid_event(struct sc_aoa *aoa, const struct sc_hid_event *event) {
sc_aoa_send_hid_event(struct sc_aoa *aoa,
const struct sc_hid_input *hid_input) {
uint8_t request_type = LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR;
uint8_t request = ACCESSORY_SEND_HID_EVENT;
// <https://source.android.com/devices/accessories/aoa2.html#hid-support>
// value (arg0): accessory assigned ID for the HID device
// index (arg1): 0 (unused)
uint16_t value = event->hid_id;
uint16_t value = hid_input->hid_id;
uint16_t index = 0;
unsigned char *data = (uint8_t *) event->data; // discard const
uint16_t length = event->size;
unsigned char *data = (uint8_t *) hid_input->data; // discard const
uint16_t length = hid_input->size;
int result = libusb_control_transfer(aoa->usb->handle, request_type,
request, value, index, data, length,
DEFAULT_TIMEOUT);
@ -195,11 +196,11 @@ sc_aoa_setup_hid(struct sc_aoa *aoa, uint16_t accessory_id,
}
bool
sc_aoa_push_hid_event_with_ack_to_wait(struct sc_aoa *aoa,
const struct sc_hid_event *event,
sc_aoa_push_input_with_ack_to_wait(struct sc_aoa *aoa,
const struct sc_hid_input *hid_input,
uint64_t ack_to_wait) {
if (sc_get_log_level() <= SC_LOG_LEVEL_VERBOSE) {
sc_hid_event_log(event);
sc_hid_input_log(hid_input);
}
sc_mutex_lock(&aoa->mutex);
@ -213,7 +214,7 @@ sc_aoa_push_hid_event_with_ack_to_wait(struct sc_aoa *aoa,
struct sc_aoa_event *aoa_event =
sc_vecdeque_push_hole_noresize(&aoa->queue);
aoa_event->type = SC_AOA_EVENT_TYPE_INPUT;
aoa_event->input.hid = *event;
aoa_event->input.hid = *hid_input;
aoa_event->input.ack_to_wait = ack_to_wait;
pushed = true;

View File

@ -31,7 +31,7 @@ struct sc_aoa_event {
uint16_t hid_id;
} close;
struct {
struct sc_hid_event hid;
struct sc_hid_input hid;
uint64_t ack_to_wait;
} input;
};
@ -82,13 +82,13 @@ bool
sc_aoa_push_close(struct sc_aoa *aoa, uint16_t accessory_id);
bool
sc_aoa_push_hid_event_with_ack_to_wait(struct sc_aoa *aoa,
const struct sc_hid_event *event,
sc_aoa_push_input_with_ack_to_wait(struct sc_aoa *aoa,
const struct sc_hid_input *hid_input,
uint64_t ack_to_wait);
static inline bool
sc_aoa_push_hid_event(struct sc_aoa *aoa, const struct sc_hid_event *event) {
return sc_aoa_push_hid_event_with_ack_to_wait(aoa, event,
sc_aoa_push_input(struct sc_aoa *aoa, const struct sc_hid_input *hid_input) {
return sc_aoa_push_input_with_ack_to_wait(aoa, hid_input,
SC_SEQUENCE_INVALID);
}

View File

@ -10,14 +10,14 @@
static bool
push_mod_lock_state(struct sc_keyboard_aoa *kb, uint16_t mods_state) {
struct sc_hid_event hid_event;
if (!sc_hid_keyboard_event_from_mods(&hid_event, mods_state)) {
struct sc_hid_input hid_input;
if (!sc_hid_keyboard_generate_input_from_mods(&hid_input, mods_state)) {
// Nothing to do
return true;
}
if (!sc_aoa_push_hid_event(kb->aoa, &hid_event)) {
LOGW("Could not request HID event (mod lock state)");
if (!sc_aoa_push_input(kb->aoa, &hid_input)) {
LOGW("Could not push HID input (mod lock state)");
return false;
}
@ -38,10 +38,10 @@ sc_key_processor_process_key(struct sc_key_processor *kp,
struct sc_keyboard_aoa *kb = DOWNCAST(kp);
struct sc_hid_event hid_event;
struct sc_hid_input hid_input;
// Not all keys are supported, just ignore unsupported keys
if (sc_hid_keyboard_event_from_key(&kb->hid, &hid_event, event)) {
if (sc_hid_keyboard_generate_input_from_key(&kb->hid, &hid_input, event)) {
if (!kb->mod_lock_synchronized) {
// Inject CAPSLOCK and/or NUMLOCK if necessary to synchronize
// keyboard state
@ -55,9 +55,9 @@ sc_key_processor_process_key(struct sc_key_processor *kp,
// synchronization is acknowledged by the server, otherwise it could
// paste the old clipboard content.
if (!sc_aoa_push_hid_event_with_ack_to_wait(kb->aoa, &hid_event,
if (!sc_aoa_push_input_with_ack_to_wait(kb->aoa, &hid_input,
ack_to_wait)) {
LOGW("Could not request HID event (key)");
LOGW("Could not push HID input (key)");
}
}
}

View File

@ -14,11 +14,11 @@ sc_mouse_processor_process_mouse_motion(struct sc_mouse_processor *mp,
const struct sc_mouse_motion_event *event) {
struct sc_mouse_aoa *mouse = DOWNCAST(mp);
struct sc_hid_event hid_event;
sc_hid_mouse_event_from_motion(&hid_event, event);
struct sc_hid_input hid_input;
sc_hid_mouse_generate_input_from_motion(&hid_input, event);
if (!sc_aoa_push_hid_event(mouse->aoa, &hid_event)) {
LOGW("Could not request HID event (mouse motion)");
if (!sc_aoa_push_input(mouse->aoa, &hid_input)) {
LOGW("Could not push HID input (mouse motion)");
}
}
@ -27,11 +27,11 @@ sc_mouse_processor_process_mouse_click(struct sc_mouse_processor *mp,
const struct sc_mouse_click_event *event) {
struct sc_mouse_aoa *mouse = DOWNCAST(mp);
struct sc_hid_event hid_event;
sc_hid_mouse_event_from_click(&hid_event, event);
struct sc_hid_input hid_input;
sc_hid_mouse_generate_input_from_click(&hid_input, event);
if (!sc_aoa_push_hid_event(mouse->aoa, &hid_event)) {
LOGW("Could not request HID event (mouse click)");
if (!sc_aoa_push_input(mouse->aoa, &hid_input)) {
LOGW("Could not push HID input (mouse click)");
}
}
@ -40,11 +40,11 @@ sc_mouse_processor_process_mouse_scroll(struct sc_mouse_processor *mp,
const struct sc_mouse_scroll_event *event) {
struct sc_mouse_aoa *mouse = DOWNCAST(mp);
struct sc_hid_event hid_event;
sc_hid_mouse_event_from_scroll(&hid_event, event);
struct sc_hid_input hid_input;
sc_hid_mouse_generate_input_from_scroll(&hid_input, event);
if (!sc_aoa_push_hid_event(mouse->aoa, &hid_event)) {
LOGW("Could not request HID event (mouse scroll)");
if (!sc_aoa_push_input(mouse->aoa, &hid_input)) {
LOGW("Could not push HID input (mouse scroll)");
}
}