Add GET_CLIPBOARD device event
Add the first device event, used to forward the device clipboard to the computer.
This commit is contained in:
48
app/src/device_event.c
Normal file
48
app/src/device_event.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#include "device_event.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <SDL2/SDL_assert.h>
|
||||
|
||||
#include "buffer_util.h"
|
||||
#include "log.h"
|
||||
|
||||
ssize_t
|
||||
device_event_deserialize(const unsigned char *buf, size_t len,
|
||||
struct device_event *event) {
|
||||
if (len < 3) {
|
||||
// at least type + empty string length
|
||||
return 0; // not available
|
||||
}
|
||||
|
||||
event->type = buf[0];
|
||||
switch (event->type) {
|
||||
case DEVICE_EVENT_TYPE_GET_CLIPBOARD: {
|
||||
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';
|
||||
|
||||
event->clipboard_event.text = text;
|
||||
return 3 + clipboard_len;
|
||||
}
|
||||
default:
|
||||
LOGW("Unsupported device event type: %d", (int) event->type);
|
||||
return -1; // error, we cannot recover
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
device_event_destroy(struct device_event *event) {
|
||||
if (event->type == DEVICE_EVENT_TYPE_GET_CLIPBOARD) {
|
||||
SDL_free(event->clipboard_event.text);
|
||||
}
|
||||
}
|
||||
33
app/src/device_event.h
Normal file
33
app/src/device_event.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef DEVICEEVENT_H
|
||||
#define DEVICEEVENT_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define DEVICE_EVENT_QUEUE_SIZE 64
|
||||
#define DEVICE_EVENT_TEXT_MAX_LENGTH 4093
|
||||
#define DEVICE_EVENT_SERIALIZED_MAX_SIZE (3 + DEVICE_EVENT_TEXT_MAX_LENGTH)
|
||||
|
||||
enum device_event_type {
|
||||
DEVICE_EVENT_TYPE_GET_CLIPBOARD,
|
||||
};
|
||||
|
||||
struct device_event {
|
||||
enum device_event_type type;
|
||||
union {
|
||||
struct {
|
||||
char *text; // owned, to be freed by SDL_free()
|
||||
} clipboard_event;
|
||||
};
|
||||
};
|
||||
|
||||
// return the number of bytes consumed (0 for no event available, -1 on error)
|
||||
ssize_t
|
||||
device_event_deserialize(const unsigned char *buf, size_t len,
|
||||
struct device_event *event);
|
||||
|
||||
void
|
||||
device_event_destroy(struct device_event *event);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user