Skip to content

Commit 1902281

Browse files
committed
Add support for user input
1 parent a8a0034 commit 1902281

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

examples/server.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use image::GenericImageView;
1313
use log::info;
1414
use rfb::encodings::RawEncoding;
1515
use rfb::rfb::{
16-
FramebufferUpdate, KeyEvent, PixelFormat, ProtoVersion, Rectangle, SecurityType, SecurityTypes,
16+
FramebufferUpdate, KeyEvent, PixelFormat, PointerEvent, ProtoVersion, Rectangle, SecurityType,
17+
SecurityTypes,
1718
};
1819
use rfb::{
1920
pixel_formats::rgb_888,
@@ -108,8 +109,8 @@ async fn main() -> Result<()> {
108109
name: "rfb-example-server".to_string(),
109110
};
110111
let data = VncServerData {
111-
width: WIDTH as u16,
112-
height: HEIGHT as u16,
112+
width: WIDTH as _,
113+
height: HEIGHT as _,
113114
input_pixel_format: pf.clone(),
114115
};
115116
let server = ExampleServer {
@@ -220,8 +221,8 @@ fn generate_pixels(img: Image, big_endian: bool, rgb_order: (u8, u8, u8)) -> Vec
220221
#[async_trait]
221222
impl Server for ExampleServer {
222223
async fn get_framebuffer_update(&self) -> FramebufferUpdate {
223-
let pixels_width = 1024;
224-
let pixels_height = 768;
224+
let pixels_width = WIDTH as _;
225+
let pixels_height = HEIGHT as _;
225226
let pixels = generate_pixels(self.display, self.big_endian, self.rgb_order);
226227
let r = Rectangle::new(
227228
0,
@@ -233,5 +234,23 @@ impl Server for ExampleServer {
233234
FramebufferUpdate::new(vec![r])
234235
}
235236

236-
async fn key_event(&self, _ke: KeyEvent) {}
237+
async fn key_event(&self, ke: KeyEvent) {
238+
log::info!(
239+
"Key {:?} {}",
240+
ke.keysym(),
241+
if ke.is_pressed() {
242+
"pressed"
243+
} else {
244+
"reselased"
245+
}
246+
);
247+
}
248+
249+
async fn pointer_event(&self, pe: PointerEvent) {
250+
log::info!("Pointer {:?} {:?}", pe.position, pe.pressed);
251+
}
252+
253+
async fn cut_text(&self, t: String) {
254+
log::info!("Cut {:?}", t);
255+
}
237256
}

src/rfb.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ impl FramebufferUpdate {
224224
}
225225

226226
#[derive(Debug, Copy, Clone)]
227-
pub(crate) struct Position {
228-
x: u16,
229-
y: u16,
227+
pub struct Position {
228+
pub x: u16,
229+
pub y: u16,
230230
}
231231

232232
impl ReadMessage for Position {
@@ -242,9 +242,9 @@ impl ReadMessage for Position {
242242
}
243243

244244
#[derive(Debug, Copy, Clone)]
245-
pub(crate) struct Resolution {
246-
width: u16,
247-
height: u16,
245+
pub struct Resolution {
246+
pub width: u16,
247+
pub height: u16,
248248
}
249249

250250
impl ReadMessage for Resolution {
@@ -271,9 +271,9 @@ impl WriteMessage for Resolution {
271271
}
272272

273273
pub struct Rectangle {
274-
position: Position,
275-
dimensions: Resolution,
276-
data: Box<dyn Encoding>,
274+
pub position: Position,
275+
pub dimensions: Resolution,
276+
pub data: Box<dyn Encoding>,
277277
}
278278

279279
impl Rectangle {
@@ -650,9 +650,9 @@ impl ReadMessage for ClientMessage {
650650
#[derive(Debug)]
651651
#[allow(dead_code)]
652652
pub struct FramebufferUpdateRequest {
653-
incremental: bool,
654-
position: Position,
655-
resolution: Resolution,
653+
pub incremental: bool,
654+
pub position: Position,
655+
pub resolution: Resolution,
656656
}
657657

658658
#[derive(Debug, Copy, Clone)]
@@ -677,7 +677,7 @@ impl KeyEvent {
677677
}
678678

679679
bitflags! {
680-
struct MouseButtons: u8 {
680+
pub struct MouseButtons: u8 {
681681
const LEFT = 1 << 0;
682682
const MIDDLE = 1 << 1;
683683
const RIGHT = 1 << 2;
@@ -691,8 +691,8 @@ bitflags! {
691691
#[derive(Debug)]
692692
#[allow(dead_code)]
693693
pub struct PointerEvent {
694-
position: Position,
695-
pressed: MouseButtons,
694+
pub position: Position,
695+
pub pressed: MouseButtons,
696696
}
697697

698698
impl ReadMessage for PointerEvent {

src/server.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ use tokio::net::{TcpListener, TcpStream};
1414
use tokio::sync::Mutex;
1515

1616
use crate::rfb::{
17-
ClientInit, ClientMessage, FramebufferUpdate, KeyEvent, PixelFormat, ProtoVersion, ReadMessage,
18-
SecurityResult, SecurityType, SecurityTypes, ServerInit, WriteMessage,
17+
ClientInit, ClientMessage, FramebufferUpdate, KeyEvent, PixelFormat, PointerEvent,
18+
ProtoVersion, ReadMessage, SecurityResult, SecurityType, SecurityTypes, ServerInit,
19+
WriteMessage,
1920
};
2021

2122
/// Immutable state
@@ -47,6 +48,8 @@ pub struct VncServer<S: Server> {
4748
pub trait Server: Sync + Send + Clone + 'static {
4849
async fn get_framebuffer_update(&self) -> FramebufferUpdate;
4950
async fn key_event(&self, _ke: KeyEvent) {}
51+
async fn pointer_event(&self, _pe: PointerEvent) {}
52+
async fn cut_text(&self, _t: String) {}
5053
}
5154

5255
impl<S: Server> VncServer<S> {
@@ -209,9 +212,11 @@ impl<S: Server> VncServer<S> {
209212
}
210213
ClientMessage::PointerEvent(pe) => {
211214
trace!("Rx [{:?}: PointerEvent={:?}", addr, pe);
215+
self.server.pointer_event(pe).await;
212216
}
213217
ClientMessage::ClientCutText(t) => {
214218
trace!("Rx [{:?}: ClientCutText={:?}", addr, t);
219+
self.server.cut_text(t).await;
215220
}
216221
},
217222
Err(e) => {

0 commit comments

Comments
 (0)