2021-05-19 08:39:33 +03:00
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "ui/clipboard.h"
|
|
|
|
|
|
|
|
static NotifierList clipboard_notifiers =
|
|
|
|
NOTIFIER_LIST_INITIALIZER(clipboard_notifiers);
|
|
|
|
|
2021-08-05 16:57:02 +03:00
|
|
|
static QemuClipboardInfo *cbinfo[QEMU_CLIPBOARD_SELECTION__COUNT];
|
|
|
|
|
2021-05-19 08:39:33 +03:00
|
|
|
void qemu_clipboard_peer_register(QemuClipboardPeer *peer)
|
|
|
|
{
|
2021-07-19 18:42:15 +03:00
|
|
|
notifier_list_add(&clipboard_notifiers, &peer->notifier);
|
2021-05-19 08:39:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_clipboard_peer_unregister(QemuClipboardPeer *peer)
|
|
|
|
{
|
2021-08-05 16:57:05 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < QEMU_CLIPBOARD_SELECTION__COUNT; i++) {
|
|
|
|
qemu_clipboard_peer_release(peer, i);
|
|
|
|
}
|
2021-07-19 18:42:15 +03:00
|
|
|
notifier_remove(&peer->notifier);
|
2021-05-19 08:39:33 +03:00
|
|
|
}
|
|
|
|
|
2021-08-05 16:57:03 +03:00
|
|
|
bool qemu_clipboard_peer_owns(QemuClipboardPeer *peer,
|
|
|
|
QemuClipboardSelection selection)
|
|
|
|
{
|
|
|
|
QemuClipboardInfo *info = qemu_clipboard_info(selection);
|
|
|
|
|
|
|
|
return info && info->owner == peer;
|
|
|
|
}
|
|
|
|
|
2021-08-05 16:57:04 +03:00
|
|
|
void qemu_clipboard_peer_release(QemuClipboardPeer *peer,
|
|
|
|
QemuClipboardSelection selection)
|
|
|
|
{
|
|
|
|
g_autoptr(QemuClipboardInfo) info = NULL;
|
|
|
|
|
|
|
|
if (qemu_clipboard_peer_owns(peer, selection)) {
|
|
|
|
/* set empty clipboard info */
|
|
|
|
info = qemu_clipboard_info_new(NULL, selection);
|
|
|
|
qemu_clipboard_update(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 14:19:13 +03:00
|
|
|
bool qemu_clipboard_check_serial(QemuClipboardInfo *info, bool client)
|
|
|
|
{
|
|
|
|
if (!info->has_serial ||
|
|
|
|
!cbinfo[info->selection] ||
|
|
|
|
!cbinfo[info->selection]->has_serial) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client) {
|
|
|
|
return cbinfo[info->selection]->serial >= info->serial;
|
|
|
|
} else {
|
|
|
|
return cbinfo[info->selection]->serial > info->serial;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 08:39:33 +03:00
|
|
|
void qemu_clipboard_update(QemuClipboardInfo *info)
|
|
|
|
{
|
2021-07-19 18:42:15 +03:00
|
|
|
QemuClipboardNotify notify = {
|
|
|
|
.type = QEMU_CLIPBOARD_UPDATE_INFO,
|
|
|
|
.info = info,
|
|
|
|
};
|
2021-08-05 16:57:02 +03:00
|
|
|
g_autoptr(QemuClipboardInfo) old = NULL;
|
2021-07-19 18:42:15 +03:00
|
|
|
|
2021-08-05 16:57:02 +03:00
|
|
|
assert(info->selection < QEMU_CLIPBOARD_SELECTION__COUNT);
|
|
|
|
|
2021-07-19 18:42:15 +03:00
|
|
|
notifier_list_notify(&clipboard_notifiers, ¬ify);
|
2021-08-05 16:57:02 +03:00
|
|
|
|
|
|
|
old = cbinfo[info->selection];
|
|
|
|
cbinfo[info->selection] = qemu_clipboard_info_ref(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
QemuClipboardInfo *qemu_clipboard_info(QemuClipboardSelection selection)
|
|
|
|
{
|
|
|
|
assert(selection < QEMU_CLIPBOARD_SELECTION__COUNT);
|
|
|
|
|
|
|
|
return cbinfo[selection];
|
2021-05-19 08:39:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QemuClipboardInfo *qemu_clipboard_info_new(QemuClipboardPeer *owner,
|
|
|
|
QemuClipboardSelection selection)
|
|
|
|
{
|
|
|
|
QemuClipboardInfo *info = g_new0(QemuClipboardInfo, 1);
|
|
|
|
|
|
|
|
info->owner = owner;
|
|
|
|
info->selection = selection;
|
|
|
|
info->refcount = 1;
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
QemuClipboardInfo *qemu_clipboard_info_ref(QemuClipboardInfo *info)
|
|
|
|
{
|
|
|
|
info->refcount++;
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_clipboard_info_unref(QemuClipboardInfo *info)
|
|
|
|
{
|
|
|
|
uint32_t type;
|
|
|
|
|
|
|
|
if (!info) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->refcount--;
|
|
|
|
if (info->refcount > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (type = 0; type < QEMU_CLIPBOARD_TYPE__COUNT; type++) {
|
|
|
|
g_free(info->types[type].data);
|
|
|
|
}
|
|
|
|
g_free(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_clipboard_request(QemuClipboardInfo *info,
|
|
|
|
QemuClipboardType type)
|
|
|
|
{
|
|
|
|
if (info->types[type].data ||
|
|
|
|
info->types[type].requested ||
|
|
|
|
!info->types[type].available ||
|
|
|
|
!info->owner)
|
|
|
|
return;
|
|
|
|
|
|
|
|
info->types[type].requested = true;
|
|
|
|
info->owner->request(info, type);
|
|
|
|
}
|
|
|
|
|
2021-07-19 18:49:56 +03:00
|
|
|
void qemu_clipboard_reset_serial(void)
|
|
|
|
{
|
|
|
|
QemuClipboardNotify notify = { .type = QEMU_CLIPBOARD_RESET_SERIAL };
|
|
|
|
|
|
|
|
notifier_list_notify(&clipboard_notifiers, ¬ify);
|
|
|
|
}
|
|
|
|
|
2021-05-19 08:39:33 +03:00
|
|
|
void qemu_clipboard_set_data(QemuClipboardPeer *peer,
|
|
|
|
QemuClipboardInfo *info,
|
|
|
|
QemuClipboardType type,
|
|
|
|
uint32_t size,
|
2021-06-16 17:19:54 +03:00
|
|
|
const void *data,
|
2021-05-19 08:39:33 +03:00
|
|
|
bool update)
|
|
|
|
{
|
|
|
|
if (!info ||
|
|
|
|
info->owner != peer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(info->types[type].data);
|
|
|
|
info->types[type].data = g_memdup(data, size);
|
|
|
|
info->types[type].size = size;
|
|
|
|
info->types[type].available = true;
|
|
|
|
|
|
|
|
if (update) {
|
|
|
|
qemu_clipboard_update(info);
|
|
|
|
}
|
|
|
|
}
|