-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcursor.rs
More file actions
204 lines (171 loc) · 5.99 KB
/
Copy pathcursor.rs
File metadata and controls
204 lines (171 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use std::convert::TryFrom;
use anyhow::{Context, Result, bail};
use gtk::{gdk, glib, prelude::*};
use gtk4 as gtk;
use qemu_display::Cursor as QemuCursor;
const CURSOR_BYTES_PER_PIXEL: usize = 4;
const HIDDEN_CURSOR_PIXEL: [u8; CURSOR_BYTES_PER_PIXEL] = [0, 0, 0, 0];
#[derive(Clone, Debug)]
pub(super) struct GuestCursor {
width: i32,
height: i32,
hotspot_x: i32,
hotspot_y: i32,
rgba: Vec<u8>,
}
impl GuestCursor {
/// QEMU sends cursor pixels as ARGB32 words. On little-endian Linux the
/// byte order in memory is BGRA, so we normalize to plain RGBA before
/// handing the texture to GTK.
pub(super) fn from_qemu(cursor: QemuCursor) -> Result<Option<Self>> {
let width = u32::try_from(cursor.width).context("negative guest cursor width")?;
let height = u32::try_from(cursor.height).context("negative guest cursor height")?;
if width == 0 || height == 0 {
return Ok(None);
}
let expected_len = usize::try_from(width)
.context("invalid guest cursor width")?
.checked_mul(usize::try_from(height).context("invalid guest cursor height")?)
.and_then(|pixels| pixels.checked_mul(CURSOR_BYTES_PER_PIXEL))
.context("guest cursor size overflow")?;
if cursor.data.len() < expected_len {
bail!(
"guest cursor payload is too short for {}x{} pixels",
cursor.width,
cursor.height
);
}
let max_hotspot_x = cursor.width.saturating_sub(1);
let max_hotspot_y = cursor.height.saturating_sub(1);
Ok(Some(Self {
width: cursor.width,
height: cursor.height,
hotspot_x: cursor.hot_x.clamp(0, max_hotspot_x),
hotspot_y: cursor.hot_y.clamp(0, max_hotspot_y),
rgba: argb32_words_to_rgba(&cursor.data[..expected_len]),
}))
}
/// The cursor texture plus hotspot, for rendering inside the scene.
pub(super) fn to_texture(&self) -> (gdk::Texture, i32, i32) {
let bytes = glib::Bytes::from_owned(self.rgba.clone());
let texture = gdk::MemoryTexture::new(
self.width,
self.height,
gdk::MemoryFormat::R8g8b8a8,
&bytes,
self.stride(),
);
(texture.upcast(), self.hotspot_x, self.hotspot_y)
}
fn stride(&self) -> usize {
usize::try_from(self.width).unwrap_or(0) * CURSOR_BYTES_PER_PIXEL
}
}
fn argb32_words_to_rgba(src: &[u8]) -> Vec<u8> {
let mut rgba = Vec::with_capacity(src.len());
for pixel in src.chunks_exact(CURSOR_BYTES_PER_PIXEL) {
#[cfg(target_endian = "little")]
rgba.extend_from_slice(&[pixel[2], pixel[1], pixel[0], pixel[3]]);
#[cfg(target_endian = "big")]
rgba.extend_from_slice(&[pixel[1], pixel[2], pixel[3], pixel[0]]);
}
rgba
}
pub(super) struct CursorState {
visible: bool,
capture_hidden: bool,
active_cursor: Option<gdk::Cursor>,
hidden_cursor: Option<gdk::Cursor>,
}
impl Default for CursorState {
fn default() -> Self {
Self {
visible: true,
capture_hidden: false,
active_cursor: None,
hidden_cursor: None,
}
}
}
impl CursorState {
/// While a guest shape is defined the scene draws it (see `cursor_scene`),
/// so the widget cursor only needs to keep the host cursor out of the way.
pub(super) fn set_shape(&mut self, has_guest_shape: bool) {
self.active_cursor = if has_guest_shape {
Some(self.hidden_cursor())
} else {
None
};
}
pub(super) fn set_visible(&mut self, visible: bool) {
self.visible = visible;
}
pub(super) fn set_capture_hidden(&mut self, capture_hidden: bool) {
self.capture_hidden = capture_hidden;
}
pub(super) fn apply_to_widget(&mut self, widget: &impl IsA<gtk::Widget>) {
if !self.visible || self.capture_hidden {
let hidden = self.hidden_cursor();
widget.set_cursor(Some(&hidden));
return;
}
widget.set_cursor(self.active_cursor.as_ref());
}
fn hidden_cursor(&mut self) -> gdk::Cursor {
self.hidden_cursor
.get_or_insert_with(|| {
let bytes = glib::Bytes::from_static(&HIDDEN_CURSOR_PIXEL);
let texture = gdk::MemoryTexture::new(
1,
1,
gdk::MemoryFormat::R8g8b8a8,
&bytes,
CURSOR_BYTES_PER_PIXEL,
);
gdk::Cursor::from_texture(&texture, 0, 0, None)
})
.clone()
}
}
#[cfg(test)]
mod tests {
use super::{GuestCursor, argb32_words_to_rgba};
use qemu_display::Cursor as QemuCursor;
#[test]
fn empty_guest_cursor_clears_the_shape() {
let cursor = GuestCursor::from_qemu(QemuCursor {
width: 0,
height: 16,
hot_x: 0,
hot_y: 0,
data: Vec::new(),
})
.expect("empty guest cursor should be accepted");
assert!(cursor.is_none());
}
#[test]
fn guest_cursor_requires_enough_argb_bytes() {
let error = GuestCursor::from_qemu(QemuCursor {
width: 2,
height: 2,
hot_x: 0,
hot_y: 0,
data: vec![0; 15],
})
.expect_err("short guest cursor payload should be rejected");
assert!(error.to_string().contains("too short"));
}
#[test]
fn argb32_black_stays_opaque_black() {
let rgba = argb32_words_to_rgba(&[0x00, 0x00, 0x00, 0xff]);
assert_eq!(rgba, vec![0x00, 0x00, 0x00, 0xff]);
}
#[test]
fn argb32_color_channels_are_swizzled_to_rgba() {
let rgba = argb32_words_to_rgba(&[0x12, 0x34, 0x56, 0x78]);
#[cfg(target_endian = "little")]
assert_eq!(rgba, vec![0x56, 0x34, 0x12, 0x78]);
#[cfg(target_endian = "big")]
assert_eq!(rgba, vec![0x34, 0x56, 0x78, 0x12]);
}
}