xref: /openbmc/qemu/ui/clipboard.c (revision 7e3e20d89129614f4a7b2451fe321cc6ccca3b76)
1660e8d0fSGerd Hoffmann #include "qemu/osdep.h"
2660e8d0fSGerd Hoffmann #include "ui/clipboard.h"
3660e8d0fSGerd Hoffmann 
4660e8d0fSGerd Hoffmann static NotifierList clipboard_notifiers =
5660e8d0fSGerd Hoffmann     NOTIFIER_LIST_INITIALIZER(clipboard_notifiers);
6660e8d0fSGerd Hoffmann 
7660e8d0fSGerd Hoffmann void qemu_clipboard_peer_register(QemuClipboardPeer *peer)
8660e8d0fSGerd Hoffmann {
9660e8d0fSGerd Hoffmann     notifier_list_add(&clipboard_notifiers, &peer->update);
10660e8d0fSGerd Hoffmann }
11660e8d0fSGerd Hoffmann 
12660e8d0fSGerd Hoffmann void qemu_clipboard_peer_unregister(QemuClipboardPeer *peer)
13660e8d0fSGerd Hoffmann {
14660e8d0fSGerd Hoffmann     notifier_remove(&peer->update);
15660e8d0fSGerd Hoffmann }
16660e8d0fSGerd Hoffmann 
17660e8d0fSGerd Hoffmann void qemu_clipboard_update(QemuClipboardInfo *info)
18660e8d0fSGerd Hoffmann {
19660e8d0fSGerd Hoffmann     notifier_list_notify(&clipboard_notifiers, info);
20660e8d0fSGerd Hoffmann }
21660e8d0fSGerd Hoffmann 
22660e8d0fSGerd Hoffmann QemuClipboardInfo *qemu_clipboard_info_new(QemuClipboardPeer *owner,
23660e8d0fSGerd Hoffmann                                            QemuClipboardSelection selection)
24660e8d0fSGerd Hoffmann {
25660e8d0fSGerd Hoffmann     QemuClipboardInfo *info = g_new0(QemuClipboardInfo, 1);
26660e8d0fSGerd Hoffmann 
27660e8d0fSGerd Hoffmann     info->owner = owner;
28660e8d0fSGerd Hoffmann     info->selection = selection;
29660e8d0fSGerd Hoffmann     info->refcount = 1;
30660e8d0fSGerd Hoffmann 
31660e8d0fSGerd Hoffmann     return info;
32660e8d0fSGerd Hoffmann }
33660e8d0fSGerd Hoffmann 
34660e8d0fSGerd Hoffmann QemuClipboardInfo *qemu_clipboard_info_ref(QemuClipboardInfo *info)
35660e8d0fSGerd Hoffmann {
36660e8d0fSGerd Hoffmann     info->refcount++;
37660e8d0fSGerd Hoffmann     return info;
38660e8d0fSGerd Hoffmann }
39660e8d0fSGerd Hoffmann 
40660e8d0fSGerd Hoffmann void qemu_clipboard_info_unref(QemuClipboardInfo *info)
41660e8d0fSGerd Hoffmann {
42660e8d0fSGerd Hoffmann     uint32_t type;
43660e8d0fSGerd Hoffmann 
44660e8d0fSGerd Hoffmann     if (!info) {
45660e8d0fSGerd Hoffmann         return;
46660e8d0fSGerd Hoffmann     }
47660e8d0fSGerd Hoffmann 
48660e8d0fSGerd Hoffmann     info->refcount--;
49660e8d0fSGerd Hoffmann     if (info->refcount > 0) {
50660e8d0fSGerd Hoffmann         return;
51660e8d0fSGerd Hoffmann     }
52660e8d0fSGerd Hoffmann 
53660e8d0fSGerd Hoffmann     for (type = 0; type < QEMU_CLIPBOARD_TYPE__COUNT; type++) {
54660e8d0fSGerd Hoffmann         g_free(info->types[type].data);
55660e8d0fSGerd Hoffmann     }
56660e8d0fSGerd Hoffmann     g_free(info);
57660e8d0fSGerd Hoffmann }
58660e8d0fSGerd Hoffmann 
59660e8d0fSGerd Hoffmann void qemu_clipboard_request(QemuClipboardInfo *info,
60660e8d0fSGerd Hoffmann                             QemuClipboardType type)
61660e8d0fSGerd Hoffmann {
62660e8d0fSGerd Hoffmann     if (info->types[type].data ||
63660e8d0fSGerd Hoffmann         info->types[type].requested ||
64660e8d0fSGerd Hoffmann         !info->types[type].available ||
65660e8d0fSGerd Hoffmann         !info->owner)
66660e8d0fSGerd Hoffmann         return;
67660e8d0fSGerd Hoffmann 
68660e8d0fSGerd Hoffmann     info->types[type].requested = true;
69660e8d0fSGerd Hoffmann     info->owner->request(info, type);
70660e8d0fSGerd Hoffmann }
71660e8d0fSGerd Hoffmann 
72660e8d0fSGerd Hoffmann void qemu_clipboard_set_data(QemuClipboardPeer *peer,
73660e8d0fSGerd Hoffmann                              QemuClipboardInfo *info,
74660e8d0fSGerd Hoffmann                              QemuClipboardType type,
75660e8d0fSGerd Hoffmann                              uint32_t size,
76*7e3e20d8SAkihiko Odaki                              const void *data,
77660e8d0fSGerd Hoffmann                              bool update)
78660e8d0fSGerd Hoffmann {
79660e8d0fSGerd Hoffmann     if (!info ||
80660e8d0fSGerd Hoffmann         info->owner != peer) {
81660e8d0fSGerd Hoffmann         return;
82660e8d0fSGerd Hoffmann     }
83660e8d0fSGerd Hoffmann 
84660e8d0fSGerd Hoffmann     g_free(info->types[type].data);
85660e8d0fSGerd Hoffmann     info->types[type].data = g_memdup(data, size);
86660e8d0fSGerd Hoffmann     info->types[type].size = size;
87660e8d0fSGerd Hoffmann     info->types[type].available = true;
88660e8d0fSGerd Hoffmann 
89660e8d0fSGerd Hoffmann     if (update) {
90660e8d0fSGerd Hoffmann         qemu_clipboard_update(info);
91660e8d0fSGerd Hoffmann     }
92660e8d0fSGerd Hoffmann }
93