Skip to content

Commit

Permalink
[awm2] Dedicated utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
codyd51 committed Dec 20, 2022
1 parent 248b6cd commit 5cc6bfc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
30 changes: 3 additions & 27 deletions rust_programs/awm2/src/desktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ use mouse_driver_messages::MousePacket;
use file_manager_messages::str_from_u8_nul_utf8_unchecked;
use kb_driver_messages::{KeyEventType, KeyboardPacket};
use lazy_static::lazy_static;
use rand::rngs::SmallRng;
use rand::RngCore;
use rand::{Rng, SeedableRng};
use rand::prelude::*;

#[cfg(target_os = "axle")]
pub extern crate libc;
Expand All @@ -38,33 +36,12 @@ mod conditional_imports {
pub use axle_rt::amc_message_send;
}
#[cfg(not(target_os = "axle"))]
mod conditional_imports {
pub use std::time::{SystemTime, UNIX_EPOCH};
}
mod conditional_imports {}

use crate::desktop::conditional_imports::*;
use crate::utils::{get_timestamp, random_color, random_color_with_rng};
use crate::window::Window;

fn get_timestamp() -> u64 {
#[cfg(target_os = "axle")]
return unsafe { libc::ms_since_boot() as u64 };
#[cfg(not(target_os = "axle"))]
return SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis() as u64;
}

fn random_color() -> Color {
let seed = get_timestamp();
let mut rng = SmallRng::seed_from_u64(seed);
Color::new(rng.gen(), rng.gen(), rng.gen())
}

fn random_color_with_rng(rng: &mut SmallRng) -> Color {
Color::new(rng.gen(), rng.gen(), rng.gen())
}

fn send_left_click_event(window: &Rc<Window>, mouse_pos: Point) {
let mouse_within_window = window.frame().translate_point(mouse_pos);
let mouse_within_content_view = window.content_frame().translate_point(mouse_within_window);
Expand Down Expand Up @@ -424,7 +401,6 @@ pub struct Desktop {
impl Desktop {
pub fn new(video_memory_layer: Rc<Box<dyn LikeLayerSlice>>) -> Self {
let desktop_frame = Rect::with_size(video_memory_layer.frame().size);
video_memory_layer.fill_rect(desktop_frame, Color::yellow(), StrokeThickness::Filled);

let desktop_background_layer = Box::new(SingleFramebufferLayer::new(desktop_frame.size));
let screen_buffer_layer = Box::new(SingleFramebufferLayer::new(desktop_frame.size));
Expand Down
1 change: 1 addition & 0 deletions rust_programs/awm2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

mod desktop;
mod effects;
mod utils;
mod window;

extern crate alloc;
Expand Down
35 changes: 35 additions & 0 deletions rust_programs/awm2/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use agx_definitions::Color;
use rand::rngs::SmallRng;
use rand::RngCore;
use rand::{Rng, SeedableRng};

#[cfg(target_os = "axle")]
pub extern crate libc;
#[cfg(target_os = "axle")]
mod conditional_imports {}
#[cfg(not(target_os = "axle"))]
mod conditional_imports {
pub use std::time::{SystemTime, UNIX_EPOCH};
}

use crate::utils::conditional_imports::*;

pub fn get_timestamp() -> u64 {
#[cfg(target_os = "axle")]
return unsafe { libc::ms_since_boot() as u64 };
#[cfg(not(target_os = "axle"))]
return SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis() as u64;
}

pub fn random_color() -> Color {
let seed = get_timestamp();
let mut rng = SmallRng::seed_from_u64(seed);
Color::new(rng.gen(), rng.gen(), rng.gen())
}

pub fn random_color_with_rng(rng: &mut SmallRng) -> Color {
Color::new(rng.gen(), rng.gen(), rng.gen())
}

0 comments on commit 5cc6bfc

Please sign in to comment.