LKAUS67VU5SLG5O5MTSO6N4VVT5OB2X4VNR6HYLIJ3ZOAXRQ4Q2QC
WX2TUKTGV5XK5F5GMEUDU2IHT3LRW2IQRE2BRAH76CTJIVUFMQCAC
KWR2TS2D7YN4LCGMQZ35V4WBLQDHAVSZJV3SZAIR5HQRSJAWLWCQC
K3I54HXNNCTEA7L6UGWIVBFB5TPZVZZXDBRSR2YZDIKREXQVWRIQC
R5ZNUPT57LKKPGEWOAHLSRE4VA47SDZD33OXCFFGVIXJLWXI5Y7AC
CPONUGXG2EFQI5VTBCP74GUGHWNORQ7FCKIOVXOIKGIYWZE66UEAC
JVO3ABY7VTFBUPUEPNXVSGWLCBJLPDHLSLZJEW2LSEOFUR5Z4RIAC
JUNCSETUHRAPHZYVBESYW4LTKL6EW3X4G73ILT736PWDXB45Z6GQC
#[derive(Serialize, Deserialize, Debug)]
pub enum Server {
CursorMotion(CursorMotion),
CursorClick(CursorClick),
Keyboard(Keyboard),
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CursorMotion {
pub x: i32,
pub y: i32,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CursorClick {
pub button: CursorButton,
pub state: bool,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy)]
pub enum CursorButton { Left, Right, Middle }
fn send_to(&self, buf: &str, addr: &SocketAddr) -> Option<usize> {
self.udp_socket.send_to(buf.as_bytes(), &addr).unwrap()
fn send_to(&self, events: &[event::Server], addr: &SocketAddr) -> Option<usize> {
let msg = serde_json::to_string(&events).unwrap();
// Debug
self.bytes.set(self.bytes.get() + msg.len() + 28);
self.packets.set(self.packets.get() + 1);
println!("message: {}", msg);
println!("bytes: {} | packets: {}", self.bytes.get(), self.packets.get());
self.udp_socket.send_to(msg.as_bytes(), &addr).unwrap()
Some((len, addr)) =>
Some((String::from(str::from_utf8(&buf[..len]).unwrap()), addr)),
Some((len, addr)) => {
let buf = &buf[..len];
Some((match serde_json::from_slice(buf) {
Ok(event) => event,
Err(e) => {
println!("{:?}: {}", e, String::from_utf8_lossy(buf));
return None;
},
}, addr))
},
Some(Event::MotionNotify(e)) => {
self.update_cursor(e.x_root, e.y_root);
},
Some(Event::KeyPress(e)) => {
let keysym = self.display.keycode_to_keysym(e.keycode as u8, 0);
Some(Event::MotionNotify(event)) =>
self.update_cursor(event.x_root, event.y_root),
Some(Event::KeyPress(event)) => {
let keysym = self.display.keycode_to_keysym(event.keycode as u8, 0);
Some(Event::KeyRelease(e)) => {
let keysym = self.display.keycode_to_keysym(e.keycode as u8, 0);
self.send_to_all(&format!("key_up {}", keysym));
Some(Event::KeyRelease(event)) => {
let keysym = self.display.keycode_to_keysym(event.keycode as u8, 0);
self.send_to_all(&[event::Server::Keyboard(event::Keyboard {
key: match keysym {
_ => event::Key::Space,
},
state: false,
})]);
Some(Event::ButtonPress(e)) => {
self.send_to_all(&format!("button_up {}", e.button));
Some(Event::ButtonPress(event)) => {
self.send_to_all(&[event::Server::CursorClick(event::CursorClick {
button: match event.button {
_ => event::CursorButton::Left,
},
state: true,
})]);
Some(Event::ButtonRelease(e)) => {
self.send_to_all(&format!("button_up {}", e.button));
Some(Event::ButtonRelease(event)) => {
self.send_to_all(&[event::Server::CursorClick(event::CursorClick {
button: match event.button {
_ => event::CursorButton::Left,
},
state: false,
})]);
Some((msg, addr)) => println!("{}", msg),
Some((events, addr)) => for event in events { match event {
event::Server::CursorMotion(event) =>
self.display.move_cursor(event.x, event.y),
event::Server::CursorClick(event) =>
self.display.set_button(event.button, event.state),
event::Server::Keyboard(event) =>
self.display.set_key(event.key, event.state),
} },
pub fn move_cursor(&mut self, x: i32, y: i32) {
unsafe { (self.xlib.XWarpPointer)(
self.display, 0, self.root, 0, 0, 0, 0, x, y
) };
unsafe fn consume_event(&self) {
let mut event = mem::uninitialized();
(self.xlib.XNextEvent)(self.display, &mut event);
}
pub fn move_cursor(&self, x: i32, y: i32) {
unsafe {
(self.xlib.XWarpPointer)(self.display, 0, self.root, 0, 0, 0, 0, x, y);
self.consume_event();
};
pub fn set_key(&mut self, key: Key, is_press: bool) {
pub fn set_button(&self, button: CursorButton, state: bool) {
println!("Fake click");
let button = match button {
CursorButton::Left => Button1,
CursorButton::Middle => Button3, // guessing
CursorButton::Right => Button2, // guessing
};
let keysym = match key {
Key::ControlL => XK_Control_L,
Key::ControlR => XK_Control_R,
Key::AltL => XK_Alt_L,
Key::AltR => XK_Alt_R,
Key::ShiftL => XK_Shift_L,
Key::ShiftR => XK_Shift_R,
Key::SuperL => XK_Super_L,
Key::SuperR => XK_Super_L,
Key::CapsLock => XK_Caps_Lock,
Key::Space => XK_space,
Key::Enter => XK_Return,
Key::Tab => XK_Tab,
Key::Backspace => XK_BackSpace,
Key::Delete => XK_Delete,
Key::Num0 => XK_0,
Key::Num1 => XK_1,
Key::Num2 => XK_2,
Key::Num3 => XK_3,
Key::Num4 => XK_4,
Key::Num5 => XK_5,
Key::Num6 => XK_6,
Key::Num7 => XK_7,
Key::Num8 => XK_8,
Key::Num9 => XK_9,
Key::A => XK_A,
Key::B => XK_B,
Key::C => XK_C,
Key::D => XK_D,
Key::E => XK_E,
Key::F => XK_F,
Key::G => XK_G,
Key::H => XK_H,
Key::I => XK_I,
Key::J => XK_J,
Key::K => XK_K,
Key::L => XK_L,
Key::M => XK_M,
Key::N => XK_N,
Key::O => XK_O,
Key::P => XK_P,
Key::Q => XK_Q,
Key::R => XK_R,
Key::S => XK_S,
Key::T => XK_T,
Key::U => XK_U,
Key::V => XK_V,
Key::W => XK_W,
Key::Y => XK_Y,
Key::X => XK_X,
Key::Z => XK_Z,
Key::F1 => XK_F1,
Key::F2 => XK_F2,
Key::F3 => XK_F3,
Key::F4 => XK_F4,
Key::F5 => XK_F5,
Key::F6 => XK_F6,
Key::F7 => XK_F7,
Key::F8 => XK_F8,
Key::F9 => XK_F9,
Key::F10 => XK_F10,
Key::F11 => XK_F11,
Key::F12 => XK_F12,
};
(self.xtest.XTestFakeButtonEvent)(self.display, button, state as i32, xlib::CurrentTime);
self.consume_event();
};
}
pub fn set_key(&self, key: Key, state: bool) {
let keysym = match key {
Key::ControlL => XK_Control_L,
Key::ControlR => XK_Control_R,
Key::AltL => XK_Alt_L,
Key::AltR => XK_Alt_R,
Key::ShiftL => XK_Shift_L,
Key::ShiftR => XK_Shift_R,
Key::SuperL => XK_Super_L,
Key::SuperR => XK_Super_L,
Key::CapsLock => XK_Caps_Lock,
Key::Space => XK_space,
Key::Enter => XK_Return,
Key::Tab => XK_Tab,
Key::Backspace => XK_BackSpace,
Key::Delete => XK_Delete,
Key::Num0 => XK_0,
Key::Num1 => XK_1,
Key::Num2 => XK_2,
Key::Num3 => XK_3,
Key::Num4 => XK_4,
Key::Num5 => XK_5,
Key::Num6 => XK_6,
Key::Num7 => XK_7,
Key::Num8 => XK_8,
Key::Num9 => XK_9,
Key::A => XK_A,
Key::B => XK_B,
Key::C => XK_C,
Key::D => XK_D,
Key::E => XK_E,
Key::F => XK_F,
Key::G => XK_G,
Key::H => XK_H,
Key::I => XK_I,
Key::J => XK_J,
Key::K => XK_K,
Key::L => XK_L,
Key::M => XK_M,
Key::N => XK_N,
Key::O => XK_O,
Key::P => XK_P,
Key::Q => XK_Q,
Key::R => XK_R,
Key::S => XK_S,
Key::T => XK_T,
Key::U => XK_U,
Key::V => XK_V,
Key::W => XK_W,
Key::Y => XK_Y,
Key::X => XK_X,
Key::Z => XK_Z,
Key::F1 => XK_F1,
Key::F2 => XK_F2,
Key::F3 => XK_F3,
Key::F4 => XK_F4,
Key::F5 => XK_F5,
Key::F6 => XK_F6,
Key::F7 => XK_F7,
Key::F8 => XK_F8,
Key::F9 => XK_F9,
Key::F10 => XK_F10,
Key::F11 => XK_F11,
Key::F12 => XK_F12,
};