Skip to content

Commit

Permalink
add: switch current display on warpzone
Browse files Browse the repository at this point in the history
  • Loading branch information
luftaquila committed Apr 19, 2024
1 parent 861680f commit b7cab50
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 50 deletions.
14 changes: 8 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,25 @@ impl Client {
cid: Uuid::new_v4(),
}));

let mut client_mut = client.borrow_mut();

// set displays
client.borrow_mut().disp = DisplayInfo::all()
client_mut.disp = DisplayInfo::all()
.unwrap()
.into_iter()
.map(|disp| Rc::new(RefCell::new(Display::from(disp))))
.collect::<Vec<Rc<RefCell<Display>>>>();

// set client reference for displays
for disp in client.borrow_mut().disp.iter_mut() {
disp.borrow_mut().owner = Some(Rc::downgrade(&client))
for disp in client_mut.disp.iter_mut() {
disp.borrow_mut().owner = Some(Rc::downgrade(&client));
}

// set cid from cid.txt
let cid = client.borrow().get_cid().unwrap();
client.borrow_mut().cid = cid;
let cid = client_mut.get_cid().unwrap();
client_mut.cid = cid;

Ok(client)
Ok(client.clone())
}

pub fn connect(&mut self, server: &str) -> Result<(), Error> {
Expand Down
131 changes: 87 additions & 44 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::cell::RefCell;
use std::fs;
use std::io::{Error, ErrorKind::*, Read};
use std::io::{Error, ErrorKind::*, Read, Write};
use std::net::TcpListener;
use std::rc::Rc;
use std::rc::{Rc, Weak};

use display_info::DisplayInfo;
use rdev::*;
Expand All @@ -15,7 +15,7 @@ pub struct Server {
tcp: TcpListener,
clients: Vec<Rc<RefCell<Client>>>,
displays: Vec<Rc<RefCell<Display>>>,
current: Rc<RefCell<Option<Rc<RefCell<Display>>>>>,
current: Rc<RefCell<Option<Weak<RefCell<Display>>>>>,
}

impl Server {
Expand Down Expand Up @@ -368,13 +368,14 @@ impl Server {
}

pub fn capture(self) -> Result<(), Error> {
let current_disp = self.current.clone();

grab(move |event| -> Option<Event> {
match self.current.clone().borrow().as_ref() {
let mut current = current_disp.borrow_mut();

match *current {
/* if there is no current display, for the first time */
None => {
let cur = self.current.clone();
let mut cur = cur.borrow_mut();

match event.event_type {
EventType::MouseMove { x, y } => {
/* identify current display if mouse moves */
Expand All @@ -392,57 +393,99 @@ impl Server {
&& y > d.y.into()
&& y < (d.y + d.height as i32).into()
{
// TODO: must be dropped before change current display
*cur = Some(disp.clone());
*current = Some(Rc::downgrade(disp));
break;
}
}
}
_ => {} // else, just ignore
}
return Some(event);
}

return Some(event);
}

match event.event_type {
EventType::MouseMove { x, y } => {
/* TODO: check if we are in warpzone */
Some(ref current_weak) => {
let cur = current_weak.upgrade().unwrap();
let cur = cur.borrow();

}

EventType::KeyPress(key) => {}
match event.event_type {
EventType::MouseMove { x, y } => {
/* check if we are in warpzone */
for warpzone in cur.warpzones.iter() {
match warpzone.direction {
ZoneDirection::HorizontalLeft => {
if y >= warpzone.start.into()
&& y <= warpzone.end.into()
&& x <= cur.x.into()
{
drop(current);
current = current_disp.borrow_mut();
*current = Some(warpzone.to.clone());
break;
}
}
ZoneDirection::HorizontalRight => {
if y >= warpzone.start.into()
&& y <= warpzone.end.into()
&& x >= (cur.x + cur.width as i32).into()
{
drop(current);
current = current_disp.borrow_mut();
*current = Some(warpzone.to.clone());
break;
}
}
ZoneDirection::VerticalUp => {
if x >= warpzone.start.into()
&& x <= warpzone.end.into()
&& y <= cur.y.into()
{
drop(current);
current = current_disp.borrow_mut();
*current = Some(warpzone.to.clone());
break;
}
}
ZoneDirection::VerticalDown => {
if x >= warpzone.start.into()
&& x <= warpzone.end.into()
&& y >= (cur.y + cur.height as i32).into()
{
drop(current);
current = current_disp.borrow_mut();
*current = Some(warpzone.to.clone());
break;
}
}
}
}
/* translate MouseMove event coordinate.. if needed */
}

EventType::KeyRelease(key) => {}
/* do nothing; maybe handle Input Source for key events.. */
_ => {} // EventType::KeyPress(key) => {}
// EventType::KeyRelease(key) => {}
// EventType::ButtonPress(button) => {}
// EventType::ButtonRelease(button) => {}
// EventType::Wheel { delta_x, delta_y } => {}
};

EventType::ButtonPress(button) => {}
/* check current display owner */
match cur.owner_type {
DisplayOwnerType::SERVER => {
return Some(event);
}
DisplayOwnerType::CLIENT => {
/* transmit event to client */

EventType::ButtonRelease(button) => {}
let encoded = bincode::serialize(&event).unwrap();
let size = encoded.len().to_be_bytes();

EventType::Wheel { delta_x, delta_y } => {}
}
/* ignore event in host system */
return None;
}
}
}
};

println!("[EVT] {:?}", event);

let encoded = bincode::serialize(&event).unwrap();
let size = encoded.len().to_be_bytes();

// match stream.write_all(&encoded.len().to_be_bytes()) {
// Ok(_) => (),
// Err(e) => {
// eprintln!("[ERR] TCP stream write failed: {}", e);
// return;
// }
// }
//
// match stream.write_all(&encoded) {
// Ok(_) => (),
// Err(e) => {
// eprintln!("[ERR] TCP stream write failed: {}", e);
// return;
// }
// }
Some(event)
})
.map_err(|e| Error::new(Other, format!("event capture error: {:?}", e)))?;
Expand Down

0 comments on commit b7cab50

Please sign in to comment.