Skip to content

Commit

Permalink
Log controller event details in 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 event details
from scrcpy is helpful for achieving that.
  • Loading branch information
intgr committed Jun 17, 2021
1 parent 506f918 commit 83e824f
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/src/android/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -194,7 +196,10 @@ 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,
Expand Down
86 changes: 86 additions & 0 deletions app/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -45,9 +73,67 @@ controller_destroy(struct controller *controller) {
receiver_destroy(&controller->receiver);
}

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);
Expand Down

0 comments on commit 83e824f

Please sign in to comment.