diff --git a/src/client.rs b/src/client.rs index f139d81..bbff1d7 100644 --- a/src/client.rs +++ b/src/client.rs @@ -42,7 +42,7 @@ impl Client { let disp_cnt: u32 = deserialize(&buffer).unwrap(); if disp_cnt < 1 { - return Err(Error::new(NotConnected, "[ERR] authorization failed")); + return Err(Error::new(ConnectionRefused, "[ERR] authorization failed")); } /* receive server's current display configurations */ @@ -58,7 +58,7 @@ impl Client { tcp_stream_read!(self.tcp, buffer); if let HandshakeStatus::HandshakeErr = deserialize(&buffer).unwrap() { - return Err(Error::new(InvalidData, "[ERR] attach request rejected")); + return Err(Error::new(ConnectionRefused, "[ERR] request rejected")); }; Ok(()) diff --git a/src/display.rs b/src/display.rs index 46b4917..d791112 100644 --- a/src/display.rs +++ b/src/display.rs @@ -1,3 +1,5 @@ +use std::io::{Error, ErrorKind::*}; + use display_info::DisplayInfo; use rand; use serde::{Deserialize, Serialize}; @@ -124,3 +126,40 @@ impl Display { } } } + +pub fn create_warpzones(a: &mut Vec, b: &mut Vec, eq: bool) -> Result<(), Error> { + for (i, disp) in a.iter_mut().enumerate() { + for (j, target) in b.iter_mut().enumerate() { + if eq && i >= j { + continue; + } + + /* check overlapping */ + if disp.is_overlap(target.clone()) { + return Err(Error::new( + InvalidInput, + "[ERR] two displays are overlapping", + )); + } + + /* add warpzone to each other if touching */ + if let Some((start, end, direction)) = disp.is_touch(target.clone()) { + disp.warpzones.push(WarpZone { + start, + end, + direction, + to: target.owner, + }); + + target.warpzones.push(WarpZone { + start, + end, + direction: direction.reverse(), + to: disp.owner, + }); + } + } + } + + Ok(()) +} diff --git a/src/server.rs b/src/server.rs index 1aec1eb..e96b06a 100644 --- a/src/server.rs +++ b/src/server.rs @@ -25,7 +25,7 @@ pub struct Server { impl Server { pub fn new() -> Result { - let disp: Vec = DisplayInfo::all() + let mut disp: Vec = DisplayInfo::all() .expect("[ERR] failed to get system displays") .into_iter() .map(|x| Display::from(x, SERVER_CID)) @@ -37,9 +37,20 @@ impl Server { let system = disp.iter().map(|x| x.id).collect(); let current = disp.iter().find(|x| x.is_primary).unwrap_or(&disp[0]).id; - let displays = Arc::new(RwLock::new(disp.into_iter().map(|x| (x.id, x)).collect())); + let displays = Arc::new(RwLock::new( + disp.iter().map(|x| (x.id, x.clone())).collect(), + )); + + let mut dummy = disp.clone(); + + /* create warpzone twice with reverse order to write correctly in disp, not dummy */ + if let Err(_) = create_warpzones(&mut disp, &mut dummy, true) { + return Err(Error::new(InvalidData, "[ERR] system display init failed")); + }; - // TODO: set warpzones + if let Err(_) = create_warpzones(&mut dummy, &mut disp, true) { + return Err(Error::new(InvalidData, "[ERR] system display init failed")); + }; Ok(Server { clients: Arc::new(RwLock::new(HashMap::new())), diff --git a/src/utils.rs b/src/utils.rs index c3c8fce..991231a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -9,9 +9,8 @@ pub enum HandshakeStatus { pub fn print_displays() { println!("[INF] detected system displays:"); - let displays = DisplayInfo::all().unwrap(); - for display in displays { + for display in DisplayInfo::all().unwrap() { println!(" {:?}", display); } }