Skip to content

Commit 0a47460

Browse files
committed
Add support for user input
1 parent 68ee9f3 commit 0a47460

3 files changed

Lines changed: 50 additions & 20 deletions

File tree

examples/server.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55
// Copyright 2022 Oxide Computer Company
66

7-
use anyhow::{anyhow, bail, Result};
7+
use anyhow::{bail, Result};
88
use async_trait::async_trait;
99
use clap::{Parser, ValueEnum};
1010
use env_logger;
@@ -13,7 +13,8 @@ use image::GenericImageView;
1313
use log::info;
1414
use rfb::encodings::RawEncoding;
1515
use rfb::rfb::{
16-
FramebufferUpdate, PixelFormat, ProtoVersion, Rectangle, SecurityType, SecurityTypes,
16+
FramebufferUpdate, KeyEvent, PixelFormat, PointerEvent, ProtoVersion, Rectangle, SecurityType,
17+
SecurityTypes,
1718
};
1819
use rfb::{
1920
pixel_formats::rgb_888,
@@ -224,4 +225,24 @@ impl Server for ExampleServer {
224225
let r = Rectangle::new(0, 0, 1024, 768, Box::new(RawEncoding::new(pixels)));
225226
FramebufferUpdate::new(vec![r])
226227
}
228+
229+
async fn handle_key_event(&self, ke: KeyEvent) {
230+
log::info!(
231+
"Key {:?} {}",
232+
ke.key,
233+
if ke.is_pressed {
234+
"pressed"
235+
} else {
236+
"reselased"
237+
}
238+
);
239+
}
240+
241+
async fn handle_pointer_event(&self, pe: PointerEvent) {
242+
log::info!("Pointer {:?} {:?}", pe.position, pe.pressed);
243+
}
244+
245+
async fn handle_cut_text(&self, t: String) {
246+
log::info!("Cut {:?}", t);
247+
}
227248
}

src/rfb.rs

Lines changed: 17 additions & 17 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 {
@@ -645,20 +645,20 @@ impl ReadMessage for ClientMessage {
645645
#[derive(Debug)]
646646
#[allow(dead_code)]
647647
pub struct FramebufferUpdateRequest {
648-
incremental: bool,
649-
position: Position,
650-
resolution: Resolution,
648+
pub incremental: bool,
649+
pub position: Position,
650+
pub resolution: Resolution,
651651
}
652652

653653
#[derive(Debug)]
654654
#[allow(dead_code)]
655655
pub struct KeyEvent {
656-
is_pressed: bool,
657-
key: Keysym,
656+
pub is_pressed: bool,
657+
pub key: Keysym,
658658
}
659659

660660
bitflags! {
661-
struct MouseButtons: u8 {
661+
pub struct MouseButtons: u8 {
662662
const LEFT = 1 << 0;
663663
const MIDDLE = 1 << 1;
664664
const RIGHT = 1 << 2;
@@ -672,8 +672,8 @@ bitflags! {
672672
#[derive(Debug)]
673673
#[allow(dead_code)]
674674
pub struct PointerEvent {
675-
position: Position,
676-
pressed: MouseButtons,
675+
pub position: Position,
676+
pub pressed: MouseButtons,
677677
}
678678

679679
impl ReadMessage for PointerEvent {

src/server.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55
// Copyright 2022 Oxide Computer Company
66

7-
use anyhow::{anyhow, bail, Result};
7+
use anyhow::{bail, Result};
88
use async_trait::async_trait;
99
use log::{debug, error, info, trace};
1010
use std::marker::{Send, Sync};
@@ -49,6 +49,9 @@ pub struct VncServer<S: Server> {
4949
#[async_trait]
5050
pub trait Server: Sync + Send + Clone + 'static {
5151
async fn get_framebuffer_update(&self) -> FramebufferUpdate;
52+
async fn handle_key_event(&self, _ke: crate::rfb::KeyEvent) {}
53+
async fn handle_pointer_event(&self, _pe: crate::rfb::PointerEvent) {}
54+
async fn handle_cut_text(&self, _t: String) {}
5255
}
5356

5457
impl<S: Server> VncServer<S> {
@@ -204,12 +207,18 @@ impl<S: Server> VncServer<S> {
204207
}
205208
KeyEvent(ke) => {
206209
trace!("Rx [{:?}]: KeyEvent={:?}", addr, ke);
210+
211+
self.server.handle_key_event(ke).await;
207212
}
208213
PointerEvent(pe) => {
209214
trace!("Rx [{:?}: PointerEvent={:?}", addr, pe);
215+
216+
self.server.handle_pointer_event(pe).await;
210217
}
211218
ClientCutText(t) => {
212219
trace!("Rx [{:?}: ClientCutText={:?}", addr, t);
220+
221+
self.server.handle_cut_text(t).await;
213222
}
214223
},
215224
Err(e) => {

0 commit comments

Comments
 (0)