-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.rs
183 lines (168 loc) · 5.65 KB
/
main.rs
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
use anyhow::{Context, Result};
use minifb::{Window, WindowOptions};
use tokio::{self, net::TcpStream};
use tracing::Level;
use vnc::{PixelFormat, Rect, VncConnector, VncEvent, X11Event};
struct CanvasUtils {
window: Window,
video: Vec<u32>,
width: u32,
height: u32,
}
impl CanvasUtils {
fn new() -> Result<Self> {
Ok(Self {
window: Window::new(
"mstsc-rs Remote Desktop in Rust",
800_usize,
600_usize,
WindowOptions::default(),
)
.with_context(|| "Unable to create window".to_string())?,
video: vec![],
width: 800,
height: 600,
})
}
fn init(&mut self, width: u32, height: u32) -> Result<()> {
let mut window = Window::new(
"mstsc-rs Remote Desktop in Rust",
width as usize,
height as usize,
WindowOptions::default(),
)
.with_context(|| "Unable to create window")?;
window.limit_update_rate(Some(std::time::Duration::from_micros(16600)));
self.window = window;
self.width = width;
self.height = height;
self.video.resize(height as usize * width as usize, 0);
Ok(())
}
fn draw(&mut self, rect: Rect, data: Vec<u8>) -> Result<()> {
// since we set the PixelFormat as bgra
// the pixels must be sent in [blue, green, red, alpha] in the network order
let mut s_idx = 0;
for y in rect.y..rect.y + rect.height {
let mut d_idx = y as usize * self.width as usize + rect.x as usize;
for _ in rect.x..rect.x + rect.width {
self.video[d_idx] =
u32::from_le_bytes(data[s_idx..s_idx + 4].try_into().unwrap()) & 0x00_ff_ff_ff;
s_idx += 4;
d_idx += 1;
}
}
Ok(())
}
fn flush(&mut self) -> Result<()> {
self.window
.update_with_buffer(&self.video, self.width as usize, self.height as usize)
.with_context(|| "Unable to update screen buffer")?;
Ok(())
}
fn copy(&mut self, dst: Rect, src: Rect) -> Result<()> {
println!("Copy");
let mut tmp = vec![0; src.width as usize * src.height as usize];
let mut tmp_idx = 0;
for y in 0..src.height as usize {
let mut s_idx = (src.y as usize + y) * self.width as usize + src.x as usize;
for _ in 0..src.width {
tmp[tmp_idx] = self.video[s_idx];
tmp_idx += 1;
s_idx += 1;
}
}
tmp_idx = 0;
for y in 0..src.height as usize {
let mut d_idx = (dst.y as usize + y) * self.width as usize + dst.x as usize;
for _ in 0..src.width {
self.video[d_idx] = tmp[tmp_idx];
tmp_idx += 1;
d_idx += 1;
}
}
Ok(())
}
fn close(&self) {}
fn hande_vnc_event(&mut self, event: VncEvent) -> Result<()> {
match event {
VncEvent::SetResolution(screen) => {
tracing::info!("Resize {:?}", screen);
self.init(screen.width as u32, screen.height as u32)?
}
VncEvent::RawImage(rect, data) => {
self.draw(rect, data)?;
}
VncEvent::Bell => {
tracing::warn!("Bell event got, but ignore it");
}
VncEvent::SetPixelFormat(_) => unreachable!(),
VncEvent::Copy(dst, src) => {
self.copy(dst, src)?;
}
VncEvent::JpegImage(_rect, _data) => {
tracing::warn!("Jpeg event got, but ignore it");
}
VncEvent::SetCursor(rect, data) => {
if rect.width != 0 {
self.draw(rect, data)?;
}
}
VncEvent::Text(string) => {
tracing::info!("Got clipboard message {}", string);
}
_ => tracing::error!("{:?}", event),
}
Ok(())
}
}
#[tokio::main]
async fn main() -> Result<()> {
// Create tracing subscriber
#[cfg(debug_assertions)]
let subscriber = tracing_subscriber::fmt()
.pretty()
.with_max_level(Level::TRACE)
.finish();
#[cfg(not(debug_assertions))]
let subscriber = tracing_subscriber::fmt()
.pretty()
.with_max_level(Level::INFO)
.finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
let tcp = TcpStream::connect("127.0.0.1:5900").await?;
let vnc = VncConnector::new(tcp)
.set_auth_method(async move { Ok("123".to_string()) })
.add_encoding(vnc::VncEncoding::Tight)
.add_encoding(vnc::VncEncoding::Zrle)
.add_encoding(vnc::VncEncoding::CopyRect)
.add_encoding(vnc::VncEncoding::Raw)
.allow_shared(true)
.set_pixel_format(PixelFormat::bgra())
.build()?
.try_start()
.await?
.finish()?;
let mut canvas = CanvasUtils::new()?;
let mut now = std::time::Instant::now();
loop {
match vnc.poll_event().await {
Ok(Some(e)) => {
let _ = canvas.hande_vnc_event(e);
}
Ok(None) => (),
Err(e) => {
tracing::error!("{}", e.to_string());
break;
}
}
if now.elapsed().as_millis() > 16 {
let _ = canvas.flush();
let _ = vnc.input(X11Event::Refresh).await;
now = std::time::Instant::now();
}
}
canvas.close();
let _ = vnc.close().await;
Ok(())
}