Skip to content

Commit

Permalink
Log input event details to verbose log
Browse files Browse the repository at this point in the history
I wanted to automate a few actions using 'adb shell input' commands, but
it requires pixel coordinates. Logging input event details from scrcpy
is helpful for achieving that.
  • Loading branch information
intgr committed Jun 17, 2021
1 parent 2e392db commit 85200bd
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
102 changes: 102 additions & 0 deletions app/src/control_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,41 @@
#include "util/log.h"
#include "util/str_util.h"

/**
* Map an enum value to a string based on an array, without crashing on an
* out-of-bounds value.
*/
#define ENUM_TO_LABEL(labels, value) \
((size_t) (value) < ARRAY_LEN(labels) ? labels[value] : "???")

static const char *const android_keyevent_action_labels[] = {
"down",
"up",
"multi",
};
static const char *const android_motionevent_action_labels[] = {
"down",
"up",
"move",
"cancel",
"outside",
"ponter-down",
"pointer-up",
"hover-move",
"scroll",
"hover-enter"
"hover-exit",
"btn-press",
"btn-release",
};
static const char *const screen_power_mode_labels[] = {
"off",
"doze",
"normal",
"doze-suspend",
"suspend",
};

static void
write_position(uint8_t *buf, const struct position *position) {
buffer_write32be(&buf[0], position->point.x);
Expand Down Expand Up @@ -93,6 +128,73 @@ control_msg_serialize(const struct control_msg *msg, unsigned char *buf) {
}
}

void
control_msg_log(const struct control_msg *msg) {
switch (msg->type) {
case CONTROL_MSG_TYPE_INJECT_KEYCODE:
LOGV("Input: key %-4s code=%d repeat=%" PRIu32 " meta=%06lx",
ENUM_TO_LABEL(android_keyevent_action_labels,
msg->inject_keycode.action),
(int) msg->inject_keycode.keycode,
msg->inject_keycode.repeat,
(long) msg->inject_keycode.metastate);
break;
case CONTROL_MSG_TYPE_INJECT_TEXT:
LOGV("Input: text \"%s\"", msg->inject_text.text);
break;
case CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT: {
size_t action = msg->inject_touch_event.action & AMOTION_EVENT_ACTION_MASK;
LOGV("Input: touch %-4s position=%" PRIi32 "x%" PRIi32 " pressure=%g buttons=%06lx",
ENUM_TO_LABEL(android_motionevent_action_labels, action),
msg->inject_touch_event.position.point.x,
msg->inject_touch_event.position.point.y,
msg->inject_touch_event.pressure,
(long) msg->inject_touch_event.buttons);
break;
}
case CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT:
LOGV("Input: scroll position=%" PRIi32 "x%" PRIi32 " hscroll=%" PRIi32 " vscroll=%" PRIi32,
msg->inject_scroll_event.position.point.x,
msg->inject_scroll_event.position.point.y,
msg->inject_scroll_event.hscroll,
msg->inject_scroll_event.vscroll);
break;
case CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON:
LOGV("Input: back-or-screen-on: %s",
ENUM_TO_LABEL(android_keyevent_action_labels,
msg->inject_keycode.action));
break;
case CONTROL_MSG_TYPE_SET_CLIPBOARD:
LOGV("Input: clipboard %s \"%s\"",
msg->set_clipboard.paste ? "paste" : "copy",
msg->set_clipboard.text);
break;
case CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE:
LOGV("Input: power mode %s",
ENUM_TO_LABEL(screen_power_mode_labels,
msg->set_screen_power_mode.mode));
break;
case CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL:
LOGV("Input: expand notification panel");
break;
case CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL:
LOGV("Input: expand settings panel");
break;
case CONTROL_MSG_TYPE_COLLAPSE_PANELS:
LOGV("Input: collapse panels");
break;
case CONTROL_MSG_TYPE_GET_CLIPBOARD:
LOGV("Input: get clipboard");
break;
case CONTROL_MSG_TYPE_ROTATE_DEVICE:
LOGV("Input: rotate device");
break;
default:
LOGV("Input: unknown type: %u", (unsigned) msg->type);
break;
}
}

void
control_msg_destroy(struct control_msg *msg) {
switch (msg->type) {
Expand Down
3 changes: 3 additions & 0 deletions app/src/control_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ struct control_msg {
size_t
control_msg_serialize(const struct control_msg *msg, unsigned char *buf);

void
control_msg_log(const struct control_msg *msg);

void
control_msg_destroy(struct control_msg *msg);

Expand Down
1 change: 1 addition & 0 deletions app/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ controller_destroy(struct controller *controller) {
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);
Expand Down

0 comments on commit 85200bd

Please sign in to comment.