Skip to content

Commit

Permalink
add: winit warp prototype on server side
Browse files Browse the repository at this point in the history
  • Loading branch information
luftaquila committed Apr 21, 2024
1 parent 5cbdb89 commit 6488d60
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,16 @@ impl Client {
let stream = self.tcp.as_ref().unwrap();
let mut stream = stream.borrow_mut();

/* transmit client info to server */
tcp_stream_write!(stream, self);

/* TODO: receive warpzone info from server */

Ok(())
}

pub fn listen(&self) -> Result<(), Error> {
// TODO: implement from here
// TODO: handle events from server

Ok(())
}
Expand Down
51 changes: 37 additions & 14 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
cell::RefCell,
cell::{Cell, RefCell},
fs,
io::{Error, ErrorKind::*, Read, Write},
mem,
Expand All @@ -8,10 +8,19 @@ use std::{
};

use display_info::DisplayInfo;
use pixels::{Pixels, SurfaceTexture};
use rdev::*;
use winit::{
dpi::{PhysicalPosition, PhysicalSize},
event::Event,
event_loop::{ControlFlow, EventLoop},
platform::run_return::EventLoopExtRunReturn,
window::WindowBuilder,
};

use crate::{
add_warpzone, client::*, display::*, tcp_stream_read, tcp_stream_write, utils::config_dir,
add_warpzone, client::*, create_warpgate, display::*, tcp_stream_read, tcp_stream_write,
utils::config_dir, warp,
};

pub struct Server {
Expand Down Expand Up @@ -269,8 +278,12 @@ impl Server {
break;
}

/* client and displays verified; set client network info */
/* client and its displays are verified */
let tcp = Rc::new(RefCell::new(stream.try_clone().unwrap()));

/* TODO: tell warpzones to client */

/* set client network */
client.tcp = Some(tcp);
client.ip = Some(stream.peer_addr().unwrap());
clients_verified[i] = true;
Expand Down Expand Up @@ -318,7 +331,14 @@ impl Server {
pub fn capture(self) -> Result<(), Error> {
let current_disp = self.current.clone();

grab(move |event| -> Option<Event> {
let on_warp = Cell::new(false);

grab(move |event| -> Option<rdev::Event> {
/* skip event on rdev if cursor is warped */
if on_warp.get() {
return Some(event);
}

let mut current = current_disp.borrow_mut();

match *current {
Expand Down Expand Up @@ -365,8 +385,7 @@ impl Server {
&& x <= cur.x.into()
{
drop(current);
current = current_disp.borrow_mut();
*current = Some(warpzone.to.clone());
*current_disp.borrow_mut() = Some(warpzone.to.clone());
break;
}
}
Expand All @@ -376,8 +395,7 @@ impl Server {
&& x >= (cur.x + cur.width as i32).into()
{
drop(current);
current = current_disp.borrow_mut();
*current = Some(warpzone.to.clone());
*current_disp.borrow_mut() = Some(warpzone.to.clone());
break;
}
}
Expand All @@ -387,8 +405,7 @@ impl Server {
&& y <= cur.y.into()
{
drop(current);
current = current_disp.borrow_mut();
*current = Some(warpzone.to.clone());
*current_disp.borrow_mut() = Some(warpzone.to.clone());
break;
}
}
Expand All @@ -398,8 +415,7 @@ impl Server {
&& y >= (cur.y + cur.height as i32).into()
{
drop(current);
current = current_disp.borrow_mut();
*current = Some(warpzone.to.clone());
*current_disp.borrow_mut() = Some(warpzone.to.clone());
break;
}
}
Expand All @@ -423,15 +439,22 @@ impl Server {
}

DisplayOwnerType::CLIENT => {
/* transmit event to client */
/* prepare client tcp stream */
let owner = cur.owner.as_ref().and_then(|o| o.upgrade()).unwrap();
let owner = owner.borrow_mut();

let tcp = owner.tcp.as_ref().unwrap();
let mut stream = tcp.borrow_mut();

/* tell client exact warp point */
tcp_stream_write!(stream, event);

/* warp to client; hide cursor at server */
on_warp.set(true);
// TODO: create warpgate earlier; to reduce delay
let (window, mut el) = create_warpgate!();
warp!(window, el);
on_warp.set(false);

/* ignore event in host system */
return None;
}
Expand Down
47 changes: 47 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,50 @@ macro_rules! tcp_stream_read {
}
}};
}

#[macro_export]
macro_rules! create_warpgate {
() => {{
let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_transparent(true)
.with_decorations(false)
.with_inner_size(PhysicalSize::new(1, 1))
.with_position(PhysicalPosition::new(0, 0))
.with_visible(false)
.build(&event_loop)
.unwrap();

let mut pixels = Pixels::new(1, 1, SurfaceTexture::new(1, 1, &window)).unwrap();

for pixel in pixels.frame_mut().chunks_exact_mut(4) {
pixel.copy_from_slice(&[0x00, 0x00, 0x00, 0x80]);
}

window.set_inner_size(PhysicalSize::new(100, 100));
window.set_cursor_visible(false);
window.set_visible(true);

pixels.render().unwrap();

(window, event_loop)
}};
}

#[macro_export]
macro_rules! warp {
($window:expr, $event_loop:expr) => {
$window.request_redraw();

$event_loop.run_return(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;

println!("winit: {:?}", event);

$window
.set_cursor_position(PhysicalPosition::new(50, 50))
.unwrap();
// *control_flow = ControlFlow::Exit;
});
};
}

0 comments on commit 6488d60

Please sign in to comment.