2019-05-31 14:55:11 +02:00
|
|
|
#include "control_msg.h"
|
|
|
|
|
2019-11-27 21:11:40 +01:00
|
|
|
#include <assert.h>
|
2021-06-08 19:14:20 +03:00
|
|
|
#include <inttypes.h>
|
2021-01-24 15:14:53 +01:00
|
|
|
#include <stdlib.h>
|
2019-05-31 14:55:11 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2022-08-03 15:13:16 +02:00
|
|
|
#include "util/binary.h"
|
2019-11-24 11:53:00 +01:00
|
|
|
#include "util/log.h"
|
2021-11-12 23:12:51 +01:00
|
|
|
#include "util/str.h"
|
2019-05-31 14:55:11 +02:00
|
|
|
|
2021-06-08 19:14:20 +03: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",
|
2022-09-27 14:12:27 +02:00
|
|
|
"pointer-down",
|
2021-06-08 19:14:20 +03:00
|
|
|
"pointer-up",
|
|
|
|
"hover-move",
|
|
|
|
"scroll",
|
2021-12-04 09:25:36 +01:00
|
|
|
"hover-enter",
|
2021-06-08 19:14:20 +03:00
|
|
|
"hover-exit",
|
|
|
|
"btn-press",
|
|
|
|
"btn-release",
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *const screen_power_mode_labels[] = {
|
|
|
|
"off",
|
|
|
|
"doze",
|
|
|
|
"normal",
|
|
|
|
"doze-suspend",
|
|
|
|
"suspend",
|
|
|
|
};
|
|
|
|
|
2021-11-29 09:30:57 +01:00
|
|
|
static const char *const copy_key_labels[] = {
|
|
|
|
"none",
|
|
|
|
"copy",
|
|
|
|
"cut",
|
|
|
|
};
|
|
|
|
|
2022-11-08 17:15:08 +01:00
|
|
|
static inline const char *
|
|
|
|
get_well_known_pointer_id_name(uint64_t pointer_id) {
|
|
|
|
switch (pointer_id) {
|
|
|
|
case POINTER_ID_MOUSE:
|
|
|
|
return "mouse";
|
|
|
|
case POINTER_ID_GENERIC_FINGER:
|
|
|
|
return "finger";
|
|
|
|
case POINTER_ID_VIRTUAL_MOUSE:
|
|
|
|
return "vmouse";
|
|
|
|
case POINTER_ID_VIRTUAL_FINGER:
|
|
|
|
return "vfinger";
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 14:55:11 +02:00
|
|
|
static void
|
2021-10-30 15:20:39 +02:00
|
|
|
write_position(uint8_t *buf, const struct sc_position *position) {
|
2022-02-12 09:12:46 +01:00
|
|
|
sc_write32be(&buf[0], position->point.x);
|
|
|
|
sc_write32be(&buf[4], position->point.y);
|
|
|
|
sc_write16be(&buf[8], position->screen_size.width);
|
|
|
|
sc_write16be(&buf[10], position->screen_size.height);
|
2019-05-31 14:55:11 +02:00
|
|
|
}
|
|
|
|
|
2021-12-04 10:54:21 +08:00
|
|
|
// write length (4 bytes) + string (non null-terminated)
|
2019-05-31 14:55:11 +02:00
|
|
|
static size_t
|
|
|
|
write_string(const char *utf8, size_t max_len, unsigned char *buf) {
|
2021-11-12 23:08:19 +01:00
|
|
|
size_t len = sc_str_utf8_truncation_index(utf8, max_len);
|
2022-02-12 09:12:46 +01:00
|
|
|
sc_write32be(buf, len);
|
2020-06-04 21:42:09 +02:00
|
|
|
memcpy(&buf[4], utf8, len);
|
|
|
|
return 4 + len;
|
2019-05-31 14:55:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_control_msg_serialize(const struct sc_control_msg *msg, unsigned char *buf) {
|
2019-05-31 14:55:11 +02:00
|
|
|
buf[0] = msg->type;
|
|
|
|
switch (msg->type) {
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_INJECT_KEYCODE:
|
2019-05-31 14:55:11 +02:00
|
|
|
buf[1] = msg->inject_keycode.action;
|
2022-02-12 09:12:46 +01:00
|
|
|
sc_write32be(&buf[2], msg->inject_keycode.keycode);
|
|
|
|
sc_write32be(&buf[6], msg->inject_keycode.repeat);
|
|
|
|
sc_write32be(&buf[10], msg->inject_keycode.metastate);
|
2020-06-11 04:40:52 -04:00
|
|
|
return 14;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_INJECT_TEXT: {
|
2020-04-13 19:37:34 +02:00
|
|
|
size_t len =
|
|
|
|
write_string(msg->inject_text.text,
|
2022-01-26 21:31:30 +01:00
|
|
|
SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH, &buf[1]);
|
2019-05-31 14:55:11 +02:00
|
|
|
return 1 + len;
|
|
|
|
}
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT:
|
2019-09-15 16:16:17 +02:00
|
|
|
buf[1] = msg->inject_touch_event.action;
|
2022-02-12 09:12:46 +01:00
|
|
|
sc_write64be(&buf[2], msg->inject_touch_event.pointer_id);
|
2019-09-15 16:16:17 +02:00
|
|
|
write_position(&buf[10], &msg->inject_touch_event.position);
|
|
|
|
uint16_t pressure =
|
2022-08-03 15:17:44 +02:00
|
|
|
sc_float_to_u16fp(msg->inject_touch_event.pressure);
|
2022-02-12 09:12:46 +01:00
|
|
|
sc_write16be(&buf[22], pressure);
|
2023-01-29 22:14:05 +01:00
|
|
|
sc_write32be(&buf[24], msg->inject_touch_event.action_button);
|
|
|
|
sc_write32be(&buf[28], msg->inject_touch_event.buttons);
|
|
|
|
return 32;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT:
|
2019-05-31 14:55:11 +02:00
|
|
|
write_position(&buf[1], &msg->inject_scroll_event.position);
|
2022-07-03 07:02:17 +00:00
|
|
|
int16_t hscroll =
|
|
|
|
sc_float_to_i16fp(msg->inject_scroll_event.hscroll);
|
|
|
|
int16_t vscroll =
|
|
|
|
sc_float_to_i16fp(msg->inject_scroll_event.vscroll);
|
|
|
|
sc_write16be(&buf[13], (uint16_t) hscroll);
|
|
|
|
sc_write16be(&buf[15], (uint16_t) vscroll);
|
|
|
|
sc_write32be(&buf[17], msg->inject_scroll_event.buttons);
|
|
|
|
return 21;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON:
|
2021-04-16 18:37:50 +02:00
|
|
|
buf[1] = msg->inject_keycode.action;
|
|
|
|
return 2;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_GET_CLIPBOARD:
|
2021-11-29 09:30:57 +01:00
|
|
|
buf[1] = msg->get_clipboard.copy_key;
|
|
|
|
return 2;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_SET_CLIPBOARD:
|
2022-02-12 09:12:46 +01:00
|
|
|
sc_write64be(&buf[1], msg->set_clipboard.sequence);
|
2021-11-20 11:50:33 +01:00
|
|
|
buf[9] = !!msg->set_clipboard.paste;
|
2020-05-25 18:41:05 +02:00
|
|
|
size_t len = write_string(msg->set_clipboard.text,
|
2022-01-26 21:31:30 +01:00
|
|
|
SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH,
|
2021-11-20 11:50:33 +01:00
|
|
|
&buf[10]);
|
|
|
|
return 10 + len;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE:
|
2019-03-15 20:23:30 +01:00
|
|
|
buf[1] = msg->set_screen_power_mode.mode;
|
|
|
|
return 2;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL:
|
|
|
|
case SC_CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL:
|
|
|
|
case SC_CONTROL_MSG_TYPE_COLLAPSE_PANELS:
|
|
|
|
case SC_CONTROL_MSG_TYPE_ROTATE_DEVICE:
|
2019-05-31 14:55:11 +02:00
|
|
|
// no additional data
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
LOGW("Unknown message type: %u", (unsigned) msg->type);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 19:14:20 +03:00
|
|
|
void
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_control_msg_log(const struct sc_control_msg *msg) {
|
2021-06-08 19:14:20 +03:00
|
|
|
#define LOG_CMSG(fmt, ...) LOGV("input: " fmt, ## __VA_ARGS__)
|
|
|
|
switch (msg->type) {
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_INJECT_KEYCODE:
|
2021-06-08 19:14:20 +03:00
|
|
|
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;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_INJECT_TEXT:
|
2021-06-08 19:14:20 +03:00
|
|
|
LOG_CMSG("text \"%s\"", msg->inject_text.text);
|
|
|
|
break;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT: {
|
2021-06-08 19:14:20 +03:00
|
|
|
int action = msg->inject_touch_event.action
|
|
|
|
& AMOTION_EVENT_ACTION_MASK;
|
|
|
|
uint64_t id = msg->inject_touch_event.pointer_id;
|
2022-11-08 17:15:08 +01:00
|
|
|
const char *pointer_name = get_well_known_pointer_id_name(id);
|
|
|
|
if (pointer_name) {
|
2021-06-08 19:14:20 +03:00
|
|
|
// string pointer id
|
|
|
|
LOG_CMSG("touch [id=%s] %-4s position=%" PRIi32 ",%" PRIi32
|
2023-01-29 22:14:05 +01:00
|
|
|
" pressure=%f action_button=%06lx buttons=%06lx",
|
2022-11-08 17:15:08 +01:00
|
|
|
pointer_name,
|
2021-06-08 19:14:20 +03:00
|
|
|
MOTIONEVENT_ACTION_LABEL(action),
|
|
|
|
msg->inject_touch_event.position.point.x,
|
|
|
|
msg->inject_touch_event.position.point.y,
|
|
|
|
msg->inject_touch_event.pressure,
|
2023-01-29 22:14:05 +01:00
|
|
|
(long) msg->inject_touch_event.action_button,
|
2021-06-08 19:14:20 +03:00
|
|
|
(long) msg->inject_touch_event.buttons);
|
|
|
|
} else {
|
|
|
|
// numeric pointer id
|
2021-06-20 01:30:06 +02:00
|
|
|
LOG_CMSG("touch [id=%" PRIu64_ "] %-4s position=%" PRIi32 ",%"
|
2023-01-29 22:14:05 +01:00
|
|
|
PRIi32 " pressure=%f action_button=%06lx"
|
|
|
|
" buttons=%06lx",
|
2021-06-08 19:14:20 +03:00
|
|
|
id,
|
|
|
|
MOTIONEVENT_ACTION_LABEL(action),
|
|
|
|
msg->inject_touch_event.position.point.x,
|
|
|
|
msg->inject_touch_event.position.point.y,
|
|
|
|
msg->inject_touch_event.pressure,
|
2023-01-29 22:14:05 +01:00
|
|
|
(long) msg->inject_touch_event.action_button,
|
2021-06-08 19:14:20 +03:00
|
|
|
(long) msg->inject_touch_event.buttons);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT:
|
2022-07-03 07:02:17 +00:00
|
|
|
LOG_CMSG("scroll position=%" PRIi32 ",%" PRIi32 " hscroll=%f"
|
|
|
|
" vscroll=%f buttons=%06lx",
|
2021-06-08 19:14:20 +03:00
|
|
|
msg->inject_scroll_event.position.point.x,
|
|
|
|
msg->inject_scroll_event.position.point.y,
|
|
|
|
msg->inject_scroll_event.hscroll,
|
2021-12-31 10:38:05 +01:00
|
|
|
msg->inject_scroll_event.vscroll,
|
|
|
|
(long) msg->inject_scroll_event.buttons);
|
2021-06-08 19:14:20 +03:00
|
|
|
break;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON:
|
2021-06-08 19:14:20 +03:00
|
|
|
LOG_CMSG("back-or-screen-on %s",
|
|
|
|
KEYEVENT_ACTION_LABEL(msg->inject_keycode.action));
|
|
|
|
break;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_GET_CLIPBOARD:
|
2021-11-29 09:30:57 +01:00
|
|
|
LOG_CMSG("get clipboard copy_key=%s",
|
|
|
|
copy_key_labels[msg->get_clipboard.copy_key]);
|
|
|
|
break;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_SET_CLIPBOARD:
|
2021-11-20 11:50:33 +01:00
|
|
|
LOG_CMSG("clipboard %" PRIu64_ " %s \"%s\"",
|
|
|
|
msg->set_clipboard.sequence,
|
2021-11-29 09:05:53 +01:00
|
|
|
msg->set_clipboard.paste ? "paste" : "nopaste",
|
2021-06-08 19:14:20 +03:00
|
|
|
msg->set_clipboard.text);
|
|
|
|
break;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE:
|
2021-06-08 19:14:20 +03:00
|
|
|
LOG_CMSG("power mode %s",
|
|
|
|
SCREEN_POWER_MODE_LABEL(msg->set_screen_power_mode.mode));
|
|
|
|
break;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL:
|
2021-06-08 19:14:20 +03:00
|
|
|
LOG_CMSG("expand notification panel");
|
|
|
|
break;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL:
|
2021-06-08 19:14:20 +03:00
|
|
|
LOG_CMSG("expand settings panel");
|
|
|
|
break;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_COLLAPSE_PANELS:
|
2021-06-08 19:14:20 +03:00
|
|
|
LOG_CMSG("collapse panels");
|
|
|
|
break;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_ROTATE_DEVICE:
|
2021-06-08 19:14:20 +03:00
|
|
|
LOG_CMSG("rotate device");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_CMSG("unknown type: %u", (unsigned) msg->type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 14:55:11 +02:00
|
|
|
void
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_control_msg_destroy(struct sc_control_msg *msg) {
|
2019-05-31 14:55:11 +02:00
|
|
|
switch (msg->type) {
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_INJECT_TEXT:
|
2021-01-24 15:14:53 +01:00
|
|
|
free(msg->inject_text.text);
|
2019-05-31 14:55:11 +02:00
|
|
|
break;
|
2022-01-26 21:31:30 +01:00
|
|
|
case SC_CONTROL_MSG_TYPE_SET_CLIPBOARD:
|
2021-01-24 15:14:53 +01:00
|
|
|
free(msg->set_clipboard.text);
|
2019-05-31 14:55:11 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// do nothing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|