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 15, 2022
1 parent 68ee9f3 commit 0a47460
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
25 changes: 23 additions & 2 deletions examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// Copyright 2022 Oxide Computer Company

use anyhow::{anyhow, bail, Result};
use anyhow::{bail, Result};
use async_trait::async_trait;
use clap::{Parser, ValueEnum};
use env_logger;
Expand All @@ -13,7 +13,8 @@ use image::GenericImageView;
use log::info;
use rfb::encodings::RawEncoding;
use rfb::rfb::{
FramebufferUpdate, PixelFormat, ProtoVersion, Rectangle, SecurityType, SecurityTypes,
FramebufferUpdate, KeyEvent, PixelFormat, PointerEvent, ProtoVersion, Rectangle, SecurityType,
SecurityTypes,
};
use rfb::{
pixel_formats::rgb_888,
Expand Down Expand Up @@ -224,4 +225,24 @@ impl Server for ExampleServer {
let r = Rectangle::new(0, 0, 1024, 768, Box::new(RawEncoding::new(pixels)));
FramebufferUpdate::new(vec![r])
}

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

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

async fn handle_cut_text(&self, t: String) {
log::info!("Cut {:?}", t);
}
}
34 changes: 17 additions & 17 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 @@ -645,20 +645,20 @@ 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)]
#[allow(dead_code)]
pub struct KeyEvent {
is_pressed: bool,
key: Keysym,
pub is_pressed: bool,
pub key: Keysym,
}

bitflags! {
struct MouseButtons: u8 {
pub struct MouseButtons: u8 {
const LEFT = 1 << 0;
const MIDDLE = 1 << 1;
const RIGHT = 1 << 2;
Expand All @@ -672,8 +672,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
11 changes: 10 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// Copyright 2022 Oxide Computer Company

use anyhow::{anyhow, bail, Result};
use anyhow::{bail, Result};
use async_trait::async_trait;
use log::{debug, error, info, trace};
use std::marker::{Send, Sync};
Expand Down Expand Up @@ -49,6 +49,9 @@ pub struct VncServer<S: Server> {
#[async_trait]
pub trait Server: Sync + Send + Clone + 'static {
async fn get_framebuffer_update(&self) -> FramebufferUpdate;
async fn handle_key_event(&self, _ke: crate::rfb::KeyEvent) {}
async fn handle_pointer_event(&self, _pe: crate::rfb::PointerEvent) {}
async fn handle_cut_text(&self, _t: String) {}
}

impl<S: Server> VncServer<S> {
Expand Down Expand Up @@ -204,12 +207,18 @@ impl<S: Server> VncServer<S> {
}
KeyEvent(ke) => {
trace!("Rx [{:?}]: KeyEvent={:?}", addr, ke);

self.server.handle_key_event(ke).await;
}
PointerEvent(pe) => {
trace!("Rx [{:?}: PointerEvent={:?}", addr, pe);

self.server.handle_pointer_event(pe).await;
}
ClientCutText(t) => {
trace!("Rx [{:?}: ClientCutText={:?}", addr, t);

self.server.handle_cut_text(t).await;
}
},
Err(e) => {
Expand Down

0 comments on commit 0a47460

Please sign in to comment.