Skip to content

Commit

Permalink
[awm2] Hosted awm can save and replay user input
Browse files Browse the repository at this point in the history
  • Loading branch information
codyd51 committed Dec 4, 2022
1 parent e6783f1 commit ffcceb1
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 21 deletions.
62 changes: 60 additions & 2 deletions rust_programs/awm2/src/desktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,45 @@ impl Desktop {
}

Self::copy_rect(buffer, vmem, mouse_rect);
/*
{
let font_size = Size::new(8, 12);
let dest_slice = self.video_memory_layer.get_slice(self.desktop_frame);
let messages = vec![
"I love you!",
"Happy birthday",
"Have a crate day!",
"Happy birthday my love",
"Caper says Arf!",
"We love you",
"You are fantastic",
"My lovely mushroom",
"I can't wait to spend more years with you",
"31 kisses",
"Lisa it's your birthday!",
"What a what a what a star",
"Yem",
"You're a honey",
"My birthday honey",
"You're my absolute love",
];
let msg = messages[self.rng.gen_range(0..messages.len())];
let chosen_origin = Point::new(
self.rng.gen_range(
0..self.desktop_frame.max_x() - (msg.len() as isize) * font_size.width,
),
self.rng
.gen_range(0..self.desktop_frame.max_y() - font_size.height),
);
let mut cursor = chosen_origin;
//println!("writing msg {msg} at {chosen_origin}");
let title_text_color = Color::new(self.rng.gen(), self.rng.gen(), self.rng.gen());
for ch in msg.chars() {
dest_slice.draw_char(ch, cursor, title_text_color, font_size);
cursor.x += font_size.width;
}
}
*/
}

fn copy_rect(src: &mut dyn LikeLayerSlice, dst: &mut dyn LikeLayerSlice, rect: Rect) {
Expand Down Expand Up @@ -1081,12 +1120,31 @@ impl Desktop {

#[cfg(test)]
mod test {
#![feature(test)]
use crate::desktop::{Desktop, DesktopElement, Window};
use agx_definitions::{Point, Rect, SingleFramebufferLayer, Size};
use agx_definitions::{LikeLayerSlice, Point, Rect, SingleFramebufferLayer, Size};
use alloc::rc::Rc;
use awm_messages::AwmCreateWindow;
use image::{GenericImage, GenericImageView, ImageBuffer, RgbImage, Rgba};
use image::codecs::gif::GifEncoder;
use image::{
save_buffer_with_format, GenericImage, GenericImageView, ImageBuffer, RgbImage, Rgba,
};
use libgui::PixelLayer;
use mouse_driver_messages::MousePacket;
use std::cell::RefCell;
use std::fs;
use std::fs::OpenOptions;
use std::iter::zip;
use test::Bencher;
use winit::event::Event;
extern crate test;
use winit::event_loop::{ControlFlow, EventLoop};

fn get_desktop_with_size(screen_size: Size) -> Desktop {
let mut vmem = SingleFramebufferLayer::new(screen_size);
let layer_as_trait_object = Rc::new(vmem.get_full_slice());
Desktop::new(Rc::clone(&layer_as_trait_object))
}

fn get_desktop() -> Desktop {
let screen_size = Size::new(1000, 1000);
Expand Down
77 changes: 58 additions & 19 deletions rust_programs/awm2/src/main_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use awm_messages::{AwmCreateWindow, AwmWindowUpdateTitle};
use libgui::PixelLayer;
use mouse_driver_messages::MousePacket;
use pixels::{Error, Pixels, SurfaceTexture};
use std::error;
use std::fs::OpenOptions;
use std::io::Write;
use std::mem::transmute;
use std::{error, fs};
use winit::event::{MouseButton, MouseScrollDelta};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::{Window, WindowBuilder};
Expand All @@ -21,20 +23,36 @@ use winit::{

pub fn main() -> Result<(), Box<dyn error::Error>> {
let event_loop = EventLoop::new();
let size = Size::new(1024, 768);
let mut layer = Rc::new(Box::new(PixelLayer::new("Hosted awm", &event_loop, size)));
let desktop_size = Size::new(1024, 768);
let mut layer = Rc::new(Box::new(PixelLayer::new(
"Hosted awm",
&event_loop,
desktop_size,
)));

let layer_as_trait_object = Rc::new(layer.get_slice(Rect::with_size(size)));
let layer_as_trait_object = Rc::new(layer.get_slice(Rect::with_size(desktop_size)));
let mut desktop = Desktop::new(Rc::clone(&layer_as_trait_object));
desktop.draw_background();
// Start off by drawing a blank canvas consisting of the desktop background
desktop.blit_background();
desktop.commit_entire_buffer_to_video_memory();

//let mut capture_file = None;
let mut capture_file = Some(
OpenOptions::new()
.write(true)
//.append(true)
.create(true)
.open("./capture.txt")
.unwrap(),
);
println!("capture_file {:?}", &capture_file.as_ref());

/*
let w1 = desktop.spawn_window(
"Window 0",
&AwmCreateWindow::new(Size::new(100, 100)),
Some(Point::new(200, 200)),
&AwmCreateWindow::new(Size::new(900, 400)),
Some(Point::new(100, 100)),
);
w1.content_layer
.borrow_mut()
Expand Down Expand Up @@ -123,6 +141,9 @@ pub fn main() -> Result<(), Box<dyn error::Error>> {
ElementState::Pressed => match button {
MouseButton::Left => {
is_left_click_down = true;
if let Some(capture_file) = &mut capture_file {
writeln!(capture_file, "[MouseDown]").unwrap();
}
desktop.handle_mouse_absolute_update(
None,
get_mouse_status_byte(is_left_click_down),
Expand All @@ -133,11 +154,13 @@ pub fn main() -> Result<(), Box<dyn error::Error>> {
ElementState::Released => match button {
MouseButton::Left => {
is_left_click_down = false;
desktop.handle_mouse_update(&MousePacket::new(
0,
0,
if let Some(capture_file) = &mut capture_file {
writeln!(capture_file, "[MouseUp]").unwrap();
}
desktop.handle_mouse_absolute_update(
None,
get_mouse_status_byte(is_left_click_down),
));
);
}
_ => (),
},
Expand All @@ -156,17 +179,25 @@ pub fn main() -> Result<(), Box<dyn error::Error>> {
if let None = last_cursor_pos {
desktop.set_cursor_pos(mouse_pos);
last_cursor_pos = Some(mouse_pos);
if let Some(capture_file) = &mut capture_file {
writeln!(capture_file, "[SetMousePos]").unwrap();
writeln!(capture_file, "{}, {}", mouse_pos.x, mouse_pos.y);
}
}
let rel_x = mouse_pos.x - last_cursor_pos.unwrap().x;
let rel_y = mouse_pos.y - last_cursor_pos.unwrap().y;
//println!("CursorMoved {rel_x:?}, {rel_y:?}");
desktop.handle_mouse_update(&MousePacket::new(
rel_x as i8,
rel_y as i8,
//let rel_x = mouse_pos.x - last_cursor_pos.unwrap().x;
//let rel_y = mouse_pos.y - last_cursor_pos.unwrap().y;
//println!("Synthetic mouse event {rel_x:?}, {rel_y:?}");
desktop.handle_mouse_absolute_update(
Some(mouse_pos),
get_mouse_status_byte(is_left_click_down),
));
);

last_cursor_pos = Some(mouse_pos);
if let Some(capture_file) = &mut capture_file {
writeln!(capture_file, "[MouseMoved]").unwrap();
//writeln!(capture_file, "{} {}", rel_x as i8, rel_y as i8).unwrap();
writeln!(capture_file, "{}, {}", mouse_pos.x, mouse_pos.y);
}
}
WindowEvent::CursorLeft { device_id } => {
last_cursor_pos = None;
Expand All @@ -180,13 +211,16 @@ pub fn main() -> Result<(), Box<dyn error::Error>> {
match key_code {
VirtualKeyCode::A => {
w1.render_remote_layer();
/*
desktop.handle_window_updated_title(
"Window 0",
&AwmWindowUpdateTitle::new("New Title"),
);
*/
}
VirtualKeyCode::B => w2.render_remote_layer(),
VirtualKeyCode::C => w3.render_remote_layer(),
//VirtualKeyCode::B => w2.render_remote_layer(),
//VirtualKeyCode::C => w3.render_remote_layer(),
VirtualKeyCode::D => desktop.test(),
VirtualKeyCode::E => {
if input.state == ElementState::Released {
Expand All @@ -208,6 +242,11 @@ pub fn main() -> Result<(), Box<dyn error::Error>> {
}
_ => {}
}
/*
desktop.draw_frame();
let pixel_buffer = layer.pixel_buffer.borrow();
pixel_buffer.render();
*/
}
_ => {}
}
Expand Down

0 comments on commit ffcceb1

Please sign in to comment.