2019-05-31 12:55:11 +00:00
|
|
|
#include "control_msg.h"
|
|
|
|
|
2019-11-27 20:11:40 +00:00
|
|
|
#include <assert.h>
|
2021-06-08 16:14:20 +00:00
|
|
|
#include <inttypes.h>
|
2021-01-24 14:14:53 +00:00
|
|
|
#include <stdlib.h>
|
2019-05-31 12:55:11 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-11-24 10:53:00 +00:00
|
|
|
#include "util/buffer_util.h"
|
|
|
|
#include "util/log.h"
|
|
|
|
#include "util/str_util.h"
|
2019-05-31 12:55:11 +00:00
|
|
|
|
2021-06-08 16:14:20 +00:00
|
|
|
/**
|
|
|
|
* Map an enum value to a string based on an array, without crashing on an
|
|
|
|
* out-of-bounds index.
|
|
|
|
*/
|
|
|
|
#define ENUM_TO_LABEL(labels, value) \
|
|
|
|
((size_t) (value) < ARRAY_LEN(labels) ? labels[value] : "???")
|
|
|
|
|
|
|
|
#define KEYEVENT_ACTION_LABEL(value) \
|
|
|
|
ENUM_TO_LABEL(android_keyevent_action_labels, value)
|
|
|
|
|
|
|
|
#define MOTIONEVENT_ACTION_LABEL(value) \
|
|
|
|
ENUM_TO_LABEL(android_motionevent_action_labels, value)
|
|
|
|
|
|
|
|
#define SCREEN_POWER_MODE_LABEL(value) \
|
|
|
|
ENUM_TO_LABEL(screen_power_mode_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",
|
|
|
|
};
|
|
|
|
|
2019-05-31 12:55:11 +00:00
|
|
|
static void
|
2021-10-30 13:20:39 +00:00
|
|
|
write_position(uint8_t *buf, const struct sc_position *position) {
|
2019-05-31 12:55:11 +00:00
|
|
|
buffer_write32be(&buf[0], position->point.x);
|
|
|
|
buffer_write32be(&buf[4], position->point.y);
|
|
|
|
buffer_write16be(&buf[8], position->screen_size.width);
|
|
|
|
buffer_write16be(&buf[10], position->screen_size.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write length (2 bytes) + string (non nul-terminated)
|
|
|
|
static size_t
|
|
|
|
write_string(const char *utf8, size_t max_len, unsigned char *buf) {
|
|
|
|
size_t len = utf8_truncation_index(utf8, max_len);
|
2020-06-04 19:42:09 +00:00
|
|
|
buffer_write32be(buf, len);
|
|
|
|
memcpy(&buf[4], utf8, len);
|
|
|
|
return 4 + len;
|
2019-05-31 12:55:11 +00:00
|
|
|
}
|
|
|
|
|
2019-09-15 14:16:17 +00:00
|
|
|
static uint16_t
|
|
|
|
to_fixed_point_16(float f) {
|
2019-11-27 20:11:40 +00:00
|
|
|
assert(f >= 0.0f && f <= 1.0f);
|
2019-09-15 14:16:17 +00:00
|
|
|
uint32_t u = f * 0x1p16f; // 2^16
|
|
|
|
if (u >= 0xffff) {
|
|
|
|
u = 0xffff;
|
|
|
|
}
|
|
|
|
return (uint16_t) u;
|
|
|
|
}
|
|
|
|
|
2019-05-31 12:55:11 +00:00
|
|
|
size_t
|
|
|
|
control_msg_serialize(const struct control_msg *msg, unsigned char *buf) {
|
|
|
|
buf[0] = msg->type;
|
|
|
|
switch (msg->type) {
|
|
|
|
case CONTROL_MSG_TYPE_INJECT_KEYCODE:
|
|
|
|
buf[1] = msg->inject_keycode.action;
|
|
|
|
buffer_write32be(&buf[2], msg->inject_keycode.keycode);
|
2020-06-11 08:40:52 +00:00
|
|
|
buffer_write32be(&buf[6], msg->inject_keycode.repeat);
|
|
|
|
buffer_write32be(&buf[10], msg->inject_keycode.metastate);
|
|
|
|
return 14;
|
2019-05-31 12:55:11 +00:00
|
|
|
case CONTROL_MSG_TYPE_INJECT_TEXT: {
|
2020-04-13 17:37:34 +00:00
|
|
|
size_t len =
|
|
|
|
write_string(msg->inject_text.text,
|
|
|
|
CONTROL_MSG_INJECT_TEXT_MAX_LENGTH, &buf[1]);
|
2019-05-31 12:55:11 +00:00
|
|
|
return 1 + len;
|
|
|
|
}
|
2019-09-15 14:16:17 +00:00
|
|
|
case CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT:
|
|
|
|
buf[1] = msg->inject_touch_event.action;
|
|
|
|
buffer_write64be(&buf[2], msg->inject_touch_event.pointer_id);
|
|
|
|
write_position(&buf[10], &msg->inject_touch_event.position);
|
|
|
|
uint16_t pressure =
|
|
|
|
to_fixed_point_16(msg->inject_touch_event.pressure);
|
|
|
|
buffer_write16be(&buf[22], pressure);
|
2019-10-03 18:14:12 +00:00
|
|
|
buffer_write32be(&buf[24], msg->inject_touch_event.buttons);
|
|
|
|
return 28;
|
2019-05-31 12:55:11 +00:00
|
|
|
case CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT:
|
|
|
|
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);
|
|
|
|
return 21;
|
2021-04-16 16:37:50 +00:00
|
|
|
case CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON:
|
|
|
|
buf[1] = msg->inject_keycode.action;
|
|
|
|
return 2;
|
2019-05-31 12:55:11 +00:00
|
|
|
case CONTROL_MSG_TYPE_SET_CLIPBOARD: {
|
2020-05-25 18:58:24 +00:00
|
|
|
buf[1] = !!msg->set_clipboard.paste;
|
2020-05-25 16:41:05 +00:00
|
|
|
size_t len = write_string(msg->set_clipboard.text,
|
2019-05-31 12:55:11 +00:00
|
|
|
CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH,
|
2020-05-25 18:58:24 +00:00
|
|
|
&buf[2]);
|
|
|
|
return 2 + len;
|
2019-05-31 12:55:11 +00:00
|
|
|
}
|
2019-03-15 19:23:30 +00:00
|
|
|
case CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE:
|
|
|
|
buf[1] = msg->set_screen_power_mode.mode;
|
|
|
|
return 2;
|
2019-05-31 12:55:11 +00:00
|
|
|
case CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL:
|
2021-04-17 12:32:18 +00:00
|
|
|
case CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL:
|
2021-04-17 12:26:54 +00:00
|
|
|
case CONTROL_MSG_TYPE_COLLAPSE_PANELS:
|
2019-05-31 12:55:11 +00:00
|
|
|
case CONTROL_MSG_TYPE_GET_CLIPBOARD:
|
2019-12-04 18:55:28 +00:00
|
|
|
case CONTROL_MSG_TYPE_ROTATE_DEVICE:
|
2019-05-31 12:55:11 +00:00
|
|
|
// no additional data
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
LOGW("Unknown message type: %u", (unsigned) msg->type);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 16:14:20 +00:00
|
|
|
void
|
|
|
|
control_msg_log(const struct control_msg *msg) {
|
|
|
|
#define LOG_CMSG(fmt, ...) LOGV("input: " fmt, ## __VA_ARGS__)
|
|
|
|
switch (msg->type) {
|
|
|
|
case CONTROL_MSG_TYPE_INJECT_KEYCODE:
|
|
|
|
LOG_CMSG("key %-4s code=%d repeat=%" PRIu32 " meta=%06lx",
|
|
|
|
KEYEVENT_ACTION_LABEL(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:
|
|
|
|
LOG_CMSG("text \"%s\"", msg->inject_text.text);
|
|
|
|
break;
|
|
|
|
case CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT: {
|
|
|
|
int action = msg->inject_touch_event.action
|
|
|
|
& AMOTION_EVENT_ACTION_MASK;
|
|
|
|
uint64_t id = msg->inject_touch_event.pointer_id;
|
|
|
|
if (id == POINTER_ID_MOUSE || id == POINTER_ID_VIRTUAL_FINGER) {
|
|
|
|
// string pointer id
|
|
|
|
LOG_CMSG("touch [id=%s] %-4s position=%" PRIi32 ",%" PRIi32
|
|
|
|
" pressure=%g buttons=%06lx",
|
|
|
|
id == POINTER_ID_MOUSE ? "mouse" : "vfinger",
|
|
|
|
MOTIONEVENT_ACTION_LABEL(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);
|
|
|
|
} else {
|
|
|
|
// numeric pointer id
|
2021-06-19 23:30:06 +00:00
|
|
|
#ifndef __WIN32
|
|
|
|
# define PRIu64_ PRIu64
|
|
|
|
#else
|
|
|
|
# define PRIu64_ "I64u" // Windows...
|
|
|
|
#endif
|
|
|
|
LOG_CMSG("touch [id=%" PRIu64_ "] %-4s position=%" PRIi32 ",%"
|
2021-06-08 16:14:20 +00:00
|
|
|
PRIi32 " pressure=%g buttons=%06lx",
|
|
|
|
id,
|
|
|
|
MOTIONEVENT_ACTION_LABEL(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:
|
|
|
|
LOG_CMSG("scroll position=%" PRIi32 ",%" 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:
|
|
|
|
LOG_CMSG("back-or-screen-on %s",
|
|
|
|
KEYEVENT_ACTION_LABEL(msg->inject_keycode.action));
|
|
|
|
break;
|
|
|
|
case CONTROL_MSG_TYPE_SET_CLIPBOARD:
|
|
|
|
LOG_CMSG("clipboard %s \"%s\"",
|
|
|
|
msg->set_clipboard.paste ? "paste" : "copy",
|
|
|
|
msg->set_clipboard.text);
|
|
|
|
break;
|
|
|
|
case CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE:
|
|
|
|
LOG_CMSG("power mode %s",
|
|
|
|
SCREEN_POWER_MODE_LABEL(msg->set_screen_power_mode.mode));
|
|
|
|
break;
|
|
|
|
case CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL:
|
|
|
|
LOG_CMSG("expand notification panel");
|
|
|
|
break;
|
|
|
|
case CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL:
|
|
|
|
LOG_CMSG("expand settings panel");
|
|
|
|
break;
|
|
|
|
case CONTROL_MSG_TYPE_COLLAPSE_PANELS:
|
|
|
|
LOG_CMSG("collapse panels");
|
|
|
|
break;
|
|
|
|
case CONTROL_MSG_TYPE_GET_CLIPBOARD:
|
|
|
|
LOG_CMSG("get clipboard");
|
|
|
|
break;
|
|
|
|
case CONTROL_MSG_TYPE_ROTATE_DEVICE:
|
|
|
|
LOG_CMSG("rotate device");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_CMSG("unknown type: %u", (unsigned) msg->type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 12:55:11 +00:00
|
|
|
void
|
|
|
|
control_msg_destroy(struct control_msg *msg) {
|
|
|
|
switch (msg->type) {
|
|
|
|
case CONTROL_MSG_TYPE_INJECT_TEXT:
|
2021-01-24 14:14:53 +00:00
|
|
|
free(msg->inject_text.text);
|
2019-05-31 12:55:11 +00:00
|
|
|
break;
|
|
|
|
case CONTROL_MSG_TYPE_SET_CLIPBOARD:
|
2021-01-24 14:14:53 +00:00
|
|
|
free(msg->set_clipboard.text);
|
2019-05-31 12:55:11 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// do nothing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|