From 3fd36c828942346f814f4fd91209e9093c7eca78 Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Tue, 8 Jun 2021 19:14:20 +0300 Subject: [PATCH] Log controller event details in verbose log I wanted to automate a few actions using 'adb shell input' commands, but it requires pixel coordinates. Logging event details from scrcpy is helpful for achieving that. --- app/src/android/input.h | 8 +++- app/src/controller.c | 86 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/app/src/android/input.h b/app/src/android/input.h index 30c4bcb991..47d4352bf2 100644 --- a/app/src/android/input.h +++ b/app/src/android/input.h @@ -91,6 +91,8 @@ enum android_input_event_type { /** * Key event actions. + * + * Also keep `android_keyevent_action_descriptions` updated. */ enum android_keyevent_action { /** The key has been pressed down. */ @@ -194,7 +196,11 @@ enum android_keyevent_flags { */ #define AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT 8 -/** Motion event actions */ +/** + * Motion event actions + * + * Also keep `android_motionevent_action_descriptions` updated. + */ enum android_motionevent_action { /** Bit mask of the parts of the action code that are the action itself. */ AMOTION_EVENT_ACTION_MASK = 0xff, diff --git a/app/src/controller.c b/app/src/controller.c index 38b5e7027f..cfa4c0c4a4 100644 --- a/app/src/controller.c +++ b/app/src/controller.c @@ -4,6 +4,34 @@ #include "util/log.h" +/** + * Map an "enum" value from array to a string, without crashing on an + * out-of-bounds value. + */ +#define ENUM_TO_DESCRIPTION(descriptions, value) \ + ((size_t) (value) < (sizeof(descriptions)/sizeof(descriptions[0])) ? descriptions[value] : "???") + +static const char *android_keyevent_action_descriptions[] = { + "down", + "up", + "multi", +}; +static const char *android_motionevent_action_descriptions[] = { + "down", + "up", + "move", + "cancel", + "outside", + "ponter-down", + "pointer-up", + "hover-move", + "scroll", + "hover-enter" + "hover-exit", + "btn-press", + "btn-release", +}; + bool controller_init(struct controller *controller, socket_t control_socket) { cbuf_init(&controller->queue); @@ -45,9 +73,67 @@ controller_destroy(struct controller *controller) { receiver_destroy(&controller->receiver); } +static void +control_msg_log(const struct control_msg *msg) { + switch(msg->type) { + case CONTROL_MSG_TYPE_INJECT_KEYCODE: + LOGV("Event: key %s code=%u repeat=%u meta=%06x", + ENUM_TO_DESCRIPTION(android_keyevent_action_descriptions, + msg->inject_keycode.action), + msg->inject_keycode.keycode, + msg->inject_keycode.repeat, + msg->inject_keycode.metastate + ); + break; + case CONTROL_MSG_TYPE_INJECT_TEXT: + LOGV("Event: text \"%s\"", msg->inject_text.text); + break; + case CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT: + LOGV("Event: touch %s position=%ux%u pressure=%f buttons=%06x", + ENUM_TO_DESCRIPTION(android_keyevent_action_descriptions, + msg->inject_touch_event.action & AMOTION_EVENT_ACTION_MASK), + msg->inject_touch_event.position.point.x, + msg->inject_touch_event.position.point.y, + msg->inject_touch_event.pressure, + msg->inject_touch_event.buttons + ); + break; + case CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT: + // TODO +// write_position(&buf[1], &msg->inject_scroll_event.position); +// buffer_write32be(&buf[13], +// (uint32_t) msg->inject_scroll_event.hscroll); +// buffer_write32be(&buf[17], +// (uint32_t) msg->inject_scroll_event.vscroll); + case CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON: + // TODO +// buf[1] = msg->inject_keycode.action; + case CONTROL_MSG_TYPE_SET_CLIPBOARD: { + // TODO +// buf[1] = !!msg->set_clipboard.paste; +// size_t len = write_string(msg->set_clipboard.text, +// CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH, +// &buf[2]); + } + case CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE: + // TODO +// buf[1] = msg->set_screen_power_mode.mode; + case CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL: + case CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL: + case CONTROL_MSG_TYPE_COLLAPSE_PANELS: + case CONTROL_MSG_TYPE_GET_CLIPBOARD: + case CONTROL_MSG_TYPE_ROTATE_DEVICE: + // TODO + // no additional data + default: + LOGV("Event: unknown type: %u", (unsigned) msg->type); + } +} + bool controller_push_msg(struct controller *controller, const struct control_msg *msg) { + control_msg_log(msg); sc_mutex_lock(&controller->mutex); bool was_empty = cbuf_is_empty(&controller->queue); bool res = cbuf_push(&controller->queue, *msg);