* support for cmdline args and better error handling

* make config.toml optional

* more unwraps removed
This commit is contained in:
Ferdinand Schober
2023-06-10 15:45:19 +02:00
committed by GitHub
parent 225ef818a2
commit a1100cc8b2
3 changed files with 27 additions and 25 deletions

View File

@@ -171,8 +171,7 @@ enum VirtualInput {
}
impl VirtualInput {
/// main loop handling udp packets
fn consume_event(&self, event: Event) -> std::io::Result<()> {
fn consume_event(&self, event: Event) -> Result<(),()> {
match event {
Event::Pointer(e) => match e {
PointerEvent::Motion {
@@ -196,7 +195,7 @@ impl VirtualInput {
button,
state,
} => {
let state: ButtonState = state.try_into().unwrap();
let state: ButtonState = state.try_into()?;
match self {
VirtualInput::Wlroots {
pointer,
@@ -211,7 +210,7 @@ impl VirtualInput {
}
}
PointerEvent::Axis { time, axis, value } => {
let axis: Axis = (axis as u32).try_into().unwrap();
let axis: Axis = (axis as u32).try_into()?;
match self {
VirtualInput::Wlroots {
pointer,

View File

@@ -211,7 +211,7 @@ impl TryFrom<Vec<u8>> for PointerEvent {
match event_type {
PointerEventType::MOTION => {
let time = match data.get(2..6) {
Some(d) => u32::from_be_bytes(d.try_into().unwrap()),
Some(d) => u32::from_be_bytes(d.try_into()?),
None => {
return Err(Box::new(ProtocolError {
msg: "Expected 4 Bytes at index 2".into(),
@@ -219,7 +219,7 @@ impl TryFrom<Vec<u8>> for PointerEvent {
}
};
let relative_x = match data.get(6..14) {
Some(d) => f64::from_be_bytes(d.try_into().unwrap()),
Some(d) => f64::from_be_bytes(d.try_into()?),
None => {
return Err(Box::new(ProtocolError {
msg: "Expected 8 Bytes at index 6".into(),
@@ -227,7 +227,7 @@ impl TryFrom<Vec<u8>> for PointerEvent {
}
};
let relative_y = match data.get(14..22) {
Some(d) => f64::from_be_bytes(d.try_into().unwrap()),
Some(d) => f64::from_be_bytes(d.try_into()?),
None => {
return Err(Box::new(ProtocolError {
msg: "Expected 8 Bytes at index 14".into(),
@@ -242,7 +242,7 @@ impl TryFrom<Vec<u8>> for PointerEvent {
}
PointerEventType::BUTTON => {
let time = match data.get(2..6) {
Some(d) => u32::from_be_bytes(d.try_into().unwrap()),
Some(d) => u32::from_be_bytes(d.try_into()?),
None => {
return Err(Box::new(ProtocolError {
msg: "Expected 4 Bytes at index 2".into(),
@@ -250,7 +250,7 @@ impl TryFrom<Vec<u8>> for PointerEvent {
}
};
let button = match data.get(6..10) {
Some(d) => u32::from_be_bytes(d.try_into().unwrap()),
Some(d) => u32::from_be_bytes(d.try_into()?),
None => {
return Err(Box::new(ProtocolError {
msg: "Expected 4 Bytes at index 10".into(),
@@ -258,7 +258,7 @@ impl TryFrom<Vec<u8>> for PointerEvent {
}
};
let state = match data.get(10..14) {
Some(d) => u32::from_be_bytes(d.try_into().unwrap()),
Some(d) => u32::from_be_bytes(d.try_into()?),
None => {
return Err(Box::new(ProtocolError {
msg: "Expected 4 Bytes at index 14".into(),
@@ -273,7 +273,7 @@ impl TryFrom<Vec<u8>> for PointerEvent {
}
PointerEventType::AXIS => {
let time = match data.get(2..6) {
Some(d) => u32::from_be_bytes(d.try_into().unwrap()),
Some(d) => u32::from_be_bytes(d.try_into()?),
None => {
return Err(Box::new(ProtocolError {
msg: "Expected 4 Bytes at index 2".into(),
@@ -289,7 +289,7 @@ impl TryFrom<Vec<u8>> for PointerEvent {
}
};
let value = match data.get(7..15) {
Some(d) => f64::from_be_bytes(d.try_into().unwrap()),
Some(d) => f64::from_be_bytes(d.try_into()?),
None => {
return Err(Box::new(ProtocolError {
msg: "Expected 8 Bytes at index 7".into(),
@@ -354,7 +354,7 @@ impl TryFrom<Vec<u8>> for KeyboardEvent {
match event_type {
KeyboardEventType::KEY => {
let time = match data.get(2..6) {
Some(d) => u32::from_be_bytes(d.try_into().unwrap()),
Some(d) => u32::from_be_bytes(d.try_into()?),
None => {
return Err(Box::new(ProtocolError {
msg: "Expected 4 Bytes at index 6".into(),
@@ -362,7 +362,7 @@ impl TryFrom<Vec<u8>> for KeyboardEvent {
}
};
let key = match data.get(6..10) {
Some(d) => u32::from_be_bytes(d.try_into().unwrap()),
Some(d) => u32::from_be_bytes(d.try_into()?),
None => {
return Err(Box::new(ProtocolError {
msg: "Expected 4 Bytes at index 10".into(),
@@ -381,7 +381,7 @@ impl TryFrom<Vec<u8>> for KeyboardEvent {
}
KeyboardEventType::MODIFIERS => {
let mods_depressed = match data.get(2..6) {
Some(d) => u32::from_be_bytes(d.try_into().unwrap()),
Some(d) => u32::from_be_bytes(d.try_into()?),
None => {
return Err(Box::new(ProtocolError {
msg: "Expected 4 Bytes at index 6".into(),
@@ -389,7 +389,7 @@ impl TryFrom<Vec<u8>> for KeyboardEvent {
}
};
let mods_latched = match data.get(6..10) {
Some(d) => u32::from_be_bytes(d.try_into().unwrap()),
Some(d) => u32::from_be_bytes(d.try_into()?),
None => {
return Err(Box::new(ProtocolError {
msg: "Expected 4 Bytes at index 10".into(),
@@ -397,7 +397,7 @@ impl TryFrom<Vec<u8>> for KeyboardEvent {
}
};
let mods_locked = match data.get(10..14) {
Some(d) => u32::from_be_bytes(d.try_into().unwrap()),
Some(d) => u32::from_be_bytes(d.try_into()?),
None => {
return Err(Box::new(ProtocolError {
msg: "Expected 4 Bytes at index 14".into(),
@@ -405,7 +405,7 @@ impl TryFrom<Vec<u8>> for KeyboardEvent {
}
};
let group = match data.get(14..18) {
Some(d) => u32::from_be_bytes(d.try_into().unwrap()),
Some(d) => u32::from_be_bytes(d.try_into()?),
None => {
return Err(Box::new(ProtocolError {
msg: "Expected 4 Bytes at index 18".into(),

View File

@@ -35,27 +35,28 @@ pub struct Server {
}
impl Server {
fn handle_request(&self, mut stream: TcpStream) {
fn handle_request(&self, mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
let mut buf = [0u8; 4];
stream.read_exact(&mut buf).unwrap();
stream.read_exact(&mut buf)?;
match Request::try_from(buf) {
Ok(Request::KeyMap) => {
let data = self.data.read().unwrap();
let buf = data.get(&Request::KeyMap);
match buf {
None => {
stream.write(&0u32.to_ne_bytes()).unwrap();
stream.write(&0u32.to_ne_bytes())?;
}
Some(buf) => {
stream.write(&buf[..].len().to_ne_bytes()).unwrap();
stream.write(&buf[..]).unwrap();
stream.write(&buf[..].len().to_ne_bytes())?;
stream.write(&buf[..])?;
}
}
stream.flush().unwrap();
stream.flush()?;
}
Ok(Request::Connect) => todo!(),
Err(msg) => eprintln!("{}", msg),
}
Ok(())
}
pub fn listen(port: u16) -> Result<(Server, JoinHandle<()>), Box<dyn Error>> {
@@ -70,7 +71,9 @@ impl Server {
for stream in listen_socket.incoming() {
match stream {
Ok(stream) => {
server.handle_request(stream);
if let Err(e) = server.handle_request(stream) {
eprintln!("{}", e);
}
}
Err(e) => {
eprintln!("{}", e);