Skip to content

Commit

Permalink
add: create warpzones for server system displays
Browse files Browse the repository at this point in the history
  • Loading branch information
luftaquila committed Apr 29, 2024
1 parent 2d615ed commit 4f2e19d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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(())
Expand Down
39 changes: 39 additions & 0 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::io::{Error, ErrorKind::*};

use display_info::DisplayInfo;
use rand;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -124,3 +126,40 @@ impl Display {
}
}
}

pub fn create_warpzones(a: &mut Vec<Display>, b: &mut Vec<Display>, 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(())
}
17 changes: 14 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Server {

impl Server {
pub fn new() -> Result<Server, Error> {
let disp: Vec<Display> = DisplayInfo::all()
let mut disp: Vec<Display> = DisplayInfo::all()
.expect("[ERR] failed to get system displays")
.into_iter()
.map(|x| Display::from(x, SERVER_CID))
Expand All @@ -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())),
Expand Down
3 changes: 1 addition & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 4f2e19d

Please sign in to comment.