2019-05-31 20:55:11 +08:00
|
|
|
#include "device_msg.h"
|
2019-05-30 06:24:26 +08:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-11-24 18:53:00 +08:00
|
|
|
#include "util/buffer_util.h"
|
|
|
|
#include "util/log.h"
|
2019-05-30 06:24:26 +08:00
|
|
|
|
|
|
|
ssize_t
|
2019-05-31 20:55:11 +08:00
|
|
|
device_msg_deserialize(const unsigned char *buf, size_t len,
|
|
|
|
struct device_msg *msg) {
|
2020-06-05 03:42:09 +08:00
|
|
|
if (len < 5) {
|
2019-05-30 06:24:26 +08:00
|
|
|
// at least type + empty string length
|
|
|
|
return 0; // not available
|
|
|
|
}
|
|
|
|
|
2019-05-31 20:55:11 +08:00
|
|
|
msg->type = buf[0];
|
|
|
|
switch (msg->type) {
|
|
|
|
case DEVICE_MSG_TYPE_CLIPBOARD: {
|
2020-06-05 03:42:09 +08:00
|
|
|
size_t clipboard_len = buffer_read32be(&buf[1]);
|
|
|
|
if (clipboard_len > len - 5) {
|
2019-05-30 06:24:26 +08:00
|
|
|
return 0; // not available
|
|
|
|
}
|
|
|
|
char *text = SDL_malloc(clipboard_len + 1);
|
|
|
|
if (!text) {
|
|
|
|
LOGW("Could not allocate text for clipboard");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (clipboard_len) {
|
2020-06-05 03:42:09 +08:00
|
|
|
memcpy(text, &buf[5], clipboard_len);
|
2019-05-30 06:24:26 +08:00
|
|
|
}
|
|
|
|
text[clipboard_len] = '\0';
|
|
|
|
|
2019-05-31 20:55:11 +08:00
|
|
|
msg->clipboard.text = text;
|
2020-06-05 03:42:09 +08:00
|
|
|
return 5 + clipboard_len;
|
2019-05-30 06:24:26 +08:00
|
|
|
}
|
|
|
|
default:
|
2019-05-31 20:55:11 +08:00
|
|
|
LOGW("Unknown device message type: %d", (int) msg->type);
|
2019-05-30 06:24:26 +08:00
|
|
|
return -1; // error, we cannot recover
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-05-31 20:55:11 +08:00
|
|
|
device_msg_destroy(struct device_msg *msg) {
|
|
|
|
if (msg->type == DEVICE_MSG_TYPE_CLIPBOARD) {
|
|
|
|
SDL_free(msg->clipboard.text);
|
2019-05-30 06:24:26 +08:00
|
|
|
}
|
|
|
|
}
|