Skip to content

Commit

Permalink
Add special characters (horrible TMP)
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteOtter committed Jul 26, 2024
1 parent ef8646b commit 8f8762d
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 18 deletions.
203 changes: 190 additions & 13 deletions isototest/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
//! # Action
//!
//! This module handles interactions between the VncClient and VncServer.
use std::{
io::{self, Read},
thread::sleep,
time::Duration,
};
use std::{thread::sleep, time::Duration};

use vnc::{client::VncClient, ClientKeyEvent, VncError, X11Event};

Expand All @@ -33,17 +29,148 @@ pub async fn write_to_console(
framerate: Option<f64>,
) -> Result<(), VncError> {
// Translate each character to a keycode
let mut keycode: Result<u32, VncError>;
let mut keycode: u32;
let mut keyevent: ClientKeyEvent;
let mut x_event: X11Event;

for ch in text.chars() {
// Translate each character to its corresponding keycode.
keycode = char_to_keycode(ch);
keycode = match char_to_keycode(ch) {
Ok(code) => code,
Err(e) => return Err(e),
};

// Setup press events.
let mut keyevent: ClientKeyEvent = ClientKeyEvent {
keycode: match keycode {
Ok(code) => code,
// HACK: Check for charadcters requiring a modifier.
// TODO/FIXME: - Turn client.input call into a macro.
// - Find better solution for the mod checking.
// - Unify this if block if it is kept.
// - Remove `clone` call!
// - Remove redundant sleep calls!
if add_shift(ch).is_some() {
keyevent = add_shift(ch).unwrap();
x_event = X11Event::KeyEvent(keyevent.clone());

// Press modifier
match client.input(x_event).await {
Ok(_) => {}
Err(e) => return Err(e),
}
let timer = match framerate_to_nanos(framerate) {
Ok(nanos) => nanos,
Err(e) => return Err(e),
};
sleep(timer);

// Press button for character
keyevent.keycode = keycode;
x_event = X11Event::KeyEvent(keyevent.clone());

match client.input(x_event.clone()).await {
Ok(_) => {}
Err(e) => return Err(e),
}
let timer = match framerate_to_nanos(framerate) {
Ok(nanos) => nanos,
Err(e) => return Err(e),
};
sleep(timer);

// Release button
keyevent.down = false;

match client.input(x_event).await {
Ok(_) => {}
Err(e) => return Err(e),
}
let timer = match framerate_to_nanos(framerate) {
Ok(nanos) => nanos,
Err(e) => return Err(e),
};
sleep(timer);

// Release shift
keyevent.keycode = KeyCode::LSHIFT as u32;
keyevent.down = false;
x_event = X11Event::KeyEvent(keyevent);

match client.input(x_event).await {
Ok(_) => {}
Err(e) => return Err(e),
}
// FIXME: Remove this
let timer = match framerate_to_nanos(framerate) {
Ok(nanos) => nanos,
Err(e) => return Err(e),
};
sleep(timer);

// TODO: make this go away.
continue;
}
if add_ctrl(ch).is_some() {
keyevent = add_ctrl(ch).unwrap();
x_event = X11Event::KeyEvent(keyevent.clone());

// Press modifier
match client.input(x_event).await {
Ok(_) => {}
Err(e) => return Err(e),
}
let timer = match framerate_to_nanos(framerate) {
Ok(nanos) => nanos,
Err(e) => return Err(e),
};
sleep(timer);

// Press button for character
keyevent.keycode = keycode;
x_event = X11Event::KeyEvent(keyevent.clone());

match client.input(x_event.clone()).await {
Ok(_) => {}
Err(e) => return Err(e),
}
let timer = match framerate_to_nanos(framerate) {
Ok(nanos) => nanos,
Err(e) => return Err(e),
};
sleep(timer);

// Release button
keyevent.down = false;

match client.input(x_event).await {
Ok(_) => {}
Err(e) => return Err(e),
},
}
let timer = match framerate_to_nanos(framerate) {
Ok(nanos) => nanos,
Err(e) => return Err(e),
};
sleep(timer);

// Release shift
keyevent.keycode = KeyCode::LCTRL as u32;
keyevent.down = false;
x_event = X11Event::KeyEvent(keyevent);

match client.input(x_event).await {
Ok(_) => {}
Err(e) => return Err(e),
}
let timer = match framerate_to_nanos(framerate) {
Ok(nanos) => nanos,
Err(e) => return Err(e),
};
sleep(timer);

// TODO: make this go away.
continue;
}

// Setup press events.
keyevent = ClientKeyEvent {
keycode,
down: true,
};
let mut x_event: X11Event = X11Event::KeyEvent(keyevent);
Expand All @@ -56,7 +183,7 @@ pub async fn write_to_console(
// NOTE: Is this really necessary?
// Setup key release events.
keyevent = ClientKeyEvent {
keycode: keycode.unwrap(),
keycode,
down: false,
};
x_event = X11Event::KeyEvent(keyevent);
Expand All @@ -80,6 +207,56 @@ pub async fn read_screen(client: &VncClient) -> Result<(), VncError> {
todo!("Not implemented yet!")
}

// HACK
/// Check if the fiven char requires SHIFT to be pressed.
///
/// # Parameters
///
/// * c: `char` - The character to be checked.
///
/// # Returns
///
/// * `Some(ClientKeyEvent)` - Returns a `ClientKeyEvent` using the `LSHIFT` KeyCode.
/// * `None` - if the char does not require it.
fn add_shift(c: char) -> Option<ClientKeyEvent> {
const MOD_CHARS: &[char] = &[
'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '{', '}', '|', ':', '"', '<',
'>', '?', '~',
];

if c.is_ascii_uppercase() || MOD_CHARS.contains(&c) {
let keyevent: ClientKeyEvent = ClientKeyEvent {
keycode: KeyCode::LSHIFT as u32,
down: true,
};
return Some(keyevent);
}
None
}

// HACK
/// Check if the given char requires CTRL or similar.
///
/// # Parameters
///
/// * c: `char` - The character to check.
///
/// # Returns
///
/// * `Some(ClientKeyEvent)` - Returns a LCTRL `KeyEvent` if the character requires it.
/// * `None` - If the char does not require it.
fn add_ctrl(c: char) -> Option<ClientKeyEvent> {
let ascii_value = c as u8;
if ascii_value <= 0x1F || ascii_value == 0x7F {
let keyevent: ClientKeyEvent = ClientKeyEvent {
keycode: KeyCode::LCTRL as u32,
down: true,
};
return Some(keyevent);
}
None
}

/// Calculate the nanoseconds in between signals.
///
/// Required as not to overflow the server's input buffer.
Expand Down
39 changes: 34 additions & 5 deletions isototest/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ pub enum KeyCode {
ENQ = 05,
ACK = 06,
BEL = 07,
BckSpc = 08,
HorTab = 09,
LineFeed = 10,
BckSpc = 0xff08,
HorTab = 0xff09,
LineFeed = 0xff0d,
VerTab = 11,
FormFeed = 12,
CarrRet = 13,
Expand All @@ -34,13 +34,42 @@ pub enum KeyCode {
CANCEL = 24,
EndMed = 25,
SUB = 26,
ESC = 27,
ESC = 0xff1b,
FileSep = 28,
GroupSep = 29,
RecSep = 30,
UnitSep = 31,
DEL = 127,
DEL = 0xffff,
SPACE = 32,
INSERT = 0xff63,
HOME = 0xff50,
END = 0xff57,
PAGEUP = 0xff55,
PAGEDOWN = 0xff56,
LEFT = 0xff51,
UP = 0xff52,
RIGHT = 0xff53,
DOWN = 0xff54,
F1 = 0xffbe,
F2 = 0xffbf,
F3 = 0xffc0,
F4 = 0xffc1,
F5 = 0xffc2,
F6 = 0xffc3,
F7 = 0xffc4,
F8 = 0xffc5,
F9 = 0xffc6,
F10 = 0xffc7,
F11 = 0xffc8,
F12 = 0xffc9,
LSHIFT = 0xffe1,
RSHIFT = 0xffe2,
LCTRL = 0xffe3,
RCTRL = 0xffe4,
LMETA = 0xffe7,
RMETA = 0xffe8,
LALT = 0xffe9,
RALT = 0xffea,
ExcMrk = 33,
DblQuote = 34,
Pound = 35,
Expand Down

0 comments on commit 8f8762d

Please sign in to comment.