Skip to content

Commit

Permalink
Add support for user input
Browse files Browse the repository at this point in the history
  • Loading branch information
katyo committed Sep 23, 2022
1 parent a8a0034 commit d8ee74a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
27 changes: 23 additions & 4 deletions examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use image::GenericImageView;
use log::info;
use rfb::encodings::RawEncoding;
use rfb::rfb::{
FramebufferUpdate, KeyEvent, PixelFormat, ProtoVersion, Rectangle, SecurityType, SecurityTypes,
FramebufferUpdate, KeyEvent, PixelFormat, PointerEvent, ProtoVersion, Rectangle, SecurityType,
SecurityTypes,
};
use rfb::{
pixel_formats::rgb_888,
Expand Down Expand Up @@ -220,8 +221,8 @@ fn generate_pixels(img: Image, big_endian: bool, rgb_order: (u8, u8, u8)) -> Vec
#[async_trait]
impl Server for ExampleServer {
async fn get_framebuffer_update(&self) -> FramebufferUpdate {
let pixels_width = 1024;
let pixels_height = 768;
let pixels_width = WIDTH;
let pixels_height = HEIGHT;
let pixels = generate_pixels(self.display, self.big_endian, self.rgb_order);
let r = Rectangle::new(
0,
Expand All @@ -233,5 +234,23 @@ impl Server for ExampleServer {
FramebufferUpdate::new(vec![r])
}

async fn key_event(&self, _ke: KeyEvent) {}
async fn key_event(&self, ke: KeyEvent) {
log::info!(
"Key {:?} {}",
ke.keysym(),
if ke.is_pressed() {
"pressed"
} else {
"reselased"
}
);
}

async fn pointer_event(&self, pe: PointerEvent) {
log::info!("Pointer {:?} {:?}", pe.position, pe.pressed);
}

async fn cut_text(&self, t: String) {
log::info!("Cut {:?}", t);
}
}
30 changes: 15 additions & 15 deletions src/rfb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ impl FramebufferUpdate {
}

#[derive(Debug, Copy, Clone)]
pub(crate) struct Position {
x: u16,
y: u16,
pub struct Position {
pub x: u16,
pub y: u16,
}

impl ReadMessage for Position {
Expand All @@ -242,9 +242,9 @@ impl ReadMessage for Position {
}

#[derive(Debug, Copy, Clone)]
pub(crate) struct Resolution {
width: u16,
height: u16,
pub struct Resolution {
pub width: u16,
pub height: u16,
}

impl ReadMessage for Resolution {
Expand All @@ -271,9 +271,9 @@ impl WriteMessage for Resolution {
}

pub struct Rectangle {
position: Position,
dimensions: Resolution,
data: Box<dyn Encoding>,
pub position: Position,
pub dimensions: Resolution,
pub data: Box<dyn Encoding>,
}

impl Rectangle {
Expand Down Expand Up @@ -650,9 +650,9 @@ impl ReadMessage for ClientMessage {
#[derive(Debug)]
#[allow(dead_code)]
pub struct FramebufferUpdateRequest {
incremental: bool,
position: Position,
resolution: Resolution,
pub incremental: bool,
pub position: Position,
pub resolution: Resolution,
}

#[derive(Debug, Copy, Clone)]
Expand All @@ -677,7 +677,7 @@ impl KeyEvent {
}

bitflags! {
struct MouseButtons: u8 {
pub struct MouseButtons: u8 {
const LEFT = 1 << 0;
const MIDDLE = 1 << 1;
const RIGHT = 1 << 2;
Expand All @@ -691,8 +691,8 @@ bitflags! {
#[derive(Debug)]
#[allow(dead_code)]
pub struct PointerEvent {
position: Position,
pressed: MouseButtons,
pub position: Position,
pub pressed: MouseButtons,
}

impl ReadMessage for PointerEvent {
Expand Down
9 changes: 7 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use tokio::net::{TcpListener, TcpStream};
use tokio::sync::Mutex;

use crate::rfb::{
ClientInit, ClientMessage, FramebufferUpdate, KeyEvent, PixelFormat, ProtoVersion, ReadMessage,
SecurityResult, SecurityType, SecurityTypes, ServerInit, WriteMessage,
ClientInit, ClientMessage, FramebufferUpdate, KeyEvent, PixelFormat, PointerEvent,
ProtoVersion, ReadMessage, SecurityResult, SecurityType, SecurityTypes, ServerInit,
WriteMessage,
};

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

impl<S: Server> VncServer<S> {
Expand Down Expand Up @@ -209,9 +212,11 @@ impl<S: Server> VncServer<S> {
}
ClientMessage::PointerEvent(pe) => {
trace!("Rx [{:?}: PointerEvent={:?}", addr, pe);
self.server.pointer_event(pe).await;
}
ClientMessage::ClientCutText(t) => {
trace!("Rx [{:?}: ClientCutText={:?}", addr, t);
self.server.cut_text(t).await;
}
},
Err(e) => {
Expand Down

0 comments on commit d8ee74a

Please sign in to comment.