1 #include "qemu/osdep.h"
2 #include "ui/clipboard.h"
5 static NotifierList clipboard_notifiers
=
6 NOTIFIER_LIST_INITIALIZER(clipboard_notifiers
);
8 static QemuClipboardInfo
*cbinfo
[QEMU_CLIPBOARD_SELECTION__COUNT
];
10 void qemu_clipboard_peer_register(QemuClipboardPeer
*peer
)
12 notifier_list_add(&clipboard_notifiers
, &peer
->notifier
);
15 void qemu_clipboard_peer_unregister(QemuClipboardPeer
*peer
)
19 for (i
= 0; i
< QEMU_CLIPBOARD_SELECTION__COUNT
; i
++) {
20 qemu_clipboard_peer_release(peer
, i
);
22 notifier_remove(&peer
->notifier
);
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 bool qemu_clipboard_check_serial(QemuClipboardInfo
*info
, bool client
)
49 if (!info
->has_serial
||
50 !cbinfo
[info
->selection
] ||
51 !cbinfo
[info
->selection
]->has_serial
) {
52 trace_clipboard_check_serial(-1, -1, true);
57 ok
= info
->serial
>= cbinfo
[info
->selection
]->serial
;
59 ok
= info
->serial
> cbinfo
[info
->selection
]->serial
;
62 trace_clipboard_check_serial(cbinfo
[info
->selection
]->serial
, info
->serial
, ok
);
66 void qemu_clipboard_update(QemuClipboardInfo
*info
)
68 QemuClipboardNotify notify
= {
69 .type
= QEMU_CLIPBOARD_UPDATE_INFO
,
72 assert(info
->selection
< QEMU_CLIPBOARD_SELECTION__COUNT
);
74 notifier_list_notify(&clipboard_notifiers
, ¬ify
);
76 if (cbinfo
[info
->selection
] != info
) {
77 qemu_clipboard_info_unref(cbinfo
[info
->selection
]);
78 cbinfo
[info
->selection
] = qemu_clipboard_info_ref(info
);
82 QemuClipboardInfo
*qemu_clipboard_info(QemuClipboardSelection selection
)
84 assert(selection
< QEMU_CLIPBOARD_SELECTION__COUNT
);
86 return cbinfo
[selection
];
89 QemuClipboardInfo
*qemu_clipboard_info_new(QemuClipboardPeer
*owner
,
90 QemuClipboardSelection selection
)
92 QemuClipboardInfo
*info
= g_new0(QemuClipboardInfo
, 1);
95 info
->selection
= selection
;
101 QemuClipboardInfo
*qemu_clipboard_info_ref(QemuClipboardInfo
*info
)
107 void qemu_clipboard_info_unref(QemuClipboardInfo
*info
)
116 if (info
->refcount
> 0) {
120 for (type
= 0; type
< QEMU_CLIPBOARD_TYPE__COUNT
; type
++) {
121 g_free(info
->types
[type
].data
);
126 void qemu_clipboard_request(QemuClipboardInfo
*info
,
127 QemuClipboardType type
)
129 if (info
->types
[type
].data
||
130 info
->types
[type
].requested
||
131 !info
->types
[type
].available
||
135 info
->types
[type
].requested
= true;
136 info
->owner
->request(info
, type
);
139 void qemu_clipboard_reset_serial(void)
141 QemuClipboardNotify notify
= { .type
= QEMU_CLIPBOARD_RESET_SERIAL
};
144 for (i
= 0; i
< QEMU_CLIPBOARD_SELECTION__COUNT
; i
++) {
145 QemuClipboardInfo
*info
= qemu_clipboard_info(i
);
150 notifier_list_notify(&clipboard_notifiers
, ¬ify
);
153 void qemu_clipboard_set_data(QemuClipboardPeer
*peer
,
154 QemuClipboardInfo
*info
,
155 QemuClipboardType type
,
161 info
->owner
!= peer
) {
165 g_free(info
->types
[type
].data
);
166 info
->types
[type
].data
= g_memdup(data
, size
);
167 info
->types
[type
].size
= size
;
168 info
->types
[type
].available
= true;
171 qemu_clipboard_update(info
);