1 #include "qemu/osdep.h"
2 #include "ui/clipboard.h"
4 static NotifierList clipboard_notifiers
=
5 NOTIFIER_LIST_INITIALIZER(clipboard_notifiers
);
7 static QemuClipboardInfo
*cbinfo
[QEMU_CLIPBOARD_SELECTION__COUNT
];
9 void qemu_clipboard_peer_register(QemuClipboardPeer
*peer
)
11 notifier_list_add(&clipboard_notifiers
, &peer
->update
);
14 void qemu_clipboard_peer_unregister(QemuClipboardPeer
*peer
)
18 for (i
= 0; i
< QEMU_CLIPBOARD_SELECTION__COUNT
; i
++) {
19 qemu_clipboard_peer_release(peer
, i
);
22 notifier_remove(&peer
->update
);
25 bool qemu_clipboard_peer_owns(QemuClipboardPeer
*peer
,
26 QemuClipboardSelection selection
)
28 QemuClipboardInfo
*info
= qemu_clipboard_info(selection
);
30 return info
&& info
->owner
== peer
;
33 void qemu_clipboard_peer_release(QemuClipboardPeer
*peer
,
34 QemuClipboardSelection selection
)
36 g_autoptr(QemuClipboardInfo
) info
= NULL
;
38 if (qemu_clipboard_peer_owns(peer
, selection
)) {
39 /* set empty clipboard info */
40 info
= qemu_clipboard_info_new(NULL
, selection
);
41 qemu_clipboard_update(info
);
45 void qemu_clipboard_update(QemuClipboardInfo
*info
)
47 g_autoptr(QemuClipboardInfo
) old
= NULL
;
48 assert(info
->selection
< QEMU_CLIPBOARD_SELECTION__COUNT
);
50 notifier_list_notify(&clipboard_notifiers
, info
);
52 old
= cbinfo
[info
->selection
];
53 cbinfo
[info
->selection
] = qemu_clipboard_info_ref(info
);
56 QemuClipboardInfo
*qemu_clipboard_info(QemuClipboardSelection selection
)
58 assert(selection
< QEMU_CLIPBOARD_SELECTION__COUNT
);
60 return cbinfo
[selection
];
63 QemuClipboardInfo
*qemu_clipboard_info_new(QemuClipboardPeer
*owner
,
64 QemuClipboardSelection selection
)
66 QemuClipboardInfo
*info
= g_new0(QemuClipboardInfo
, 1);
69 info
->selection
= selection
;
75 QemuClipboardInfo
*qemu_clipboard_info_ref(QemuClipboardInfo
*info
)
81 void qemu_clipboard_info_unref(QemuClipboardInfo
*info
)
90 if (info
->refcount
> 0) {
94 for (type
= 0; type
< QEMU_CLIPBOARD_TYPE__COUNT
; type
++) {
95 g_free(info
->types
[type
].data
);
100 void qemu_clipboard_request(QemuClipboardInfo
*info
,
101 QemuClipboardType type
)
103 if (info
->types
[type
].data
||
104 info
->types
[type
].requested
||
105 !info
->types
[type
].available
||
109 info
->types
[type
].requested
= true;
110 info
->owner
->request(info
, type
);
113 void qemu_clipboard_set_data(QemuClipboardPeer
*peer
,
114 QemuClipboardInfo
*info
,
115 QemuClipboardType type
,
121 info
->owner
!= peer
) {
125 g_free(info
->types
[type
].data
);
126 info
->types
[type
].data
= g_memdup(data
, size
);
127 info
->types
[type
].size
= size
;
128 info
->types
[type
].available
= true;
131 qemu_clipboard_update(info
);