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
->notifier
);
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
);
21 notifier_remove(&peer
->notifier
);
24 bool qemu_clipboard_peer_owns(QemuClipboardPeer
*peer
,
25 QemuClipboardSelection selection
)
27 QemuClipboardInfo
*info
= qemu_clipboard_info(selection
);
29 return info
&& info
->owner
== peer
;
32 void qemu_clipboard_peer_release(QemuClipboardPeer
*peer
,
33 QemuClipboardSelection selection
)
35 g_autoptr(QemuClipboardInfo
) info
= NULL
;
37 if (qemu_clipboard_peer_owns(peer
, selection
)) {
38 /* set empty clipboard info */
39 info
= qemu_clipboard_info_new(NULL
, selection
);
40 qemu_clipboard_update(info
);
44 bool qemu_clipboard_check_serial(QemuClipboardInfo
*info
, bool client
)
46 if (!info
->has_serial
||
47 !cbinfo
[info
->selection
] ||
48 !cbinfo
[info
->selection
]->has_serial
) {
53 return cbinfo
[info
->selection
]->serial
>= info
->serial
;
55 return cbinfo
[info
->selection
]->serial
> info
->serial
;
59 void qemu_clipboard_update(QemuClipboardInfo
*info
)
61 QemuClipboardNotify notify
= {
62 .type
= QEMU_CLIPBOARD_UPDATE_INFO
,
65 assert(info
->selection
< QEMU_CLIPBOARD_SELECTION__COUNT
);
67 notifier_list_notify(&clipboard_notifiers
, ¬ify
);
69 qemu_clipboard_info_unref(cbinfo
[info
->selection
]);
70 cbinfo
[info
->selection
] = qemu_clipboard_info_ref(info
);
73 QemuClipboardInfo
*qemu_clipboard_info(QemuClipboardSelection selection
)
75 assert(selection
< QEMU_CLIPBOARD_SELECTION__COUNT
);
77 return cbinfo
[selection
];
80 QemuClipboardInfo
*qemu_clipboard_info_new(QemuClipboardPeer
*owner
,
81 QemuClipboardSelection selection
)
83 QemuClipboardInfo
*info
= g_new0(QemuClipboardInfo
, 1);
86 info
->selection
= selection
;
92 QemuClipboardInfo
*qemu_clipboard_info_ref(QemuClipboardInfo
*info
)
98 void qemu_clipboard_info_unref(QemuClipboardInfo
*info
)
107 if (info
->refcount
> 0) {
111 for (type
= 0; type
< QEMU_CLIPBOARD_TYPE__COUNT
; type
++) {
112 g_free(info
->types
[type
].data
);
117 void qemu_clipboard_request(QemuClipboardInfo
*info
,
118 QemuClipboardType type
)
120 if (info
->types
[type
].data
||
121 info
->types
[type
].requested
||
122 !info
->types
[type
].available
||
126 info
->types
[type
].requested
= true;
127 info
->owner
->request(info
, type
);
130 void qemu_clipboard_reset_serial(void)
132 QemuClipboardNotify notify
= { .type
= QEMU_CLIPBOARD_RESET_SERIAL
};
134 notifier_list_notify(&clipboard_notifiers
, ¬ify
);
137 void qemu_clipboard_set_data(QemuClipboardPeer
*peer
,
138 QemuClipboardInfo
*info
,
139 QemuClipboardType type
,
145 info
->owner
!= peer
) {
149 g_free(info
->types
[type
].data
);
150 info
->types
[type
].data
= g_memdup(data
, size
);
151 info
->types
[type
].size
= size
;
152 info
->types
[type
].available
= true;
155 qemu_clipboard_update(info
);