2019-05-31 12:55:11 +00:00
|
|
|
#include "device_msg.h"
|
2019-05-29 22:24:26 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <SDL2/SDL_assert.h>
|
|
|
|
|
|
|
|
#include "buffer_util.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
ssize_t
|
2019-05-31 12:55:11 +00:00
|
|
|
device_msg_deserialize(const unsigned char *buf, size_t len,
|
|
|
|
struct device_msg *msg) {
|
2019-05-29 22:24:26 +00:00
|
|
|
if (len < 3) {
|
|
|
|
// at least type + empty string length
|
|
|
|
return 0; // not available
|
|
|
|
}
|
|
|
|
|
2019-05-31 12:55:11 +00:00
|
|
|
msg->type = buf[0];
|
|
|
|
switch (msg->type) {
|
|
|
|
case DEVICE_MSG_TYPE_CLIPBOARD: {
|
2019-05-29 22:24:26 +00:00
|
|
|
uint16_t clipboard_len = buffer_read16be(&buf[1]);
|
|
|
|
if (clipboard_len > len - 3) {
|
|
|
|
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) {
|
|
|
|
memcpy(text, &buf[3], clipboard_len);
|
|
|
|
}
|
|
|
|
text[clipboard_len] = '\0';
|
|
|
|
|
2019-05-31 12:55:11 +00:00
|
|
|
msg->clipboard.text = text;
|
2019-05-29 22:24:26 +00:00
|
|
|
return 3 + clipboard_len;
|
|
|
|
}
|
|
|
|
default:
|
2019-05-31 12:55:11 +00:00
|
|
|
LOGW("Unknown device message type: %d", (int) msg->type);
|
2019-05-29 22:24:26 +00:00
|
|
|
return -1; // error, we cannot recover
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-05-31 12:55:11 +00:00
|
|
|
device_msg_destroy(struct device_msg *msg) {
|
|
|
|
if (msg->type == DEVICE_MSG_TYPE_CLIPBOARD) {
|
|
|
|
SDL_free(msg->clipboard.text);
|
2019-05-29 22:24:26 +00:00
|
|
|
}
|
|
|
|
}
|