Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion entrypoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ edition = "2018"
name = "swift-console"
version = "0.1.0"

[lib]
name = "entrypoint"
bench = false

[[bin]]
bench = false
name = "swift-console"
Expand All @@ -17,6 +21,10 @@ name = "swift-console-splash"
path = "src/splash.rs"
required-features = ["splash"]

[dependencies.lazy_static]
optional = true
version = "1.4.0"

[dependencies.winit]
optional = true
version = "0.26.1"
Expand All @@ -37,7 +45,7 @@ version = "0.16"

[features]
entrypoint = ["pyo3", "windows"]
splash = ["minifb", "image", "winit"]
splash = ["minifb", "image", "winit", "windows", "lazy_static"]

[target]
[target."cfg(target_os = \"windows\")"]
Expand Down
2 changes: 2 additions & 0 deletions entrypoint/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ fn main() -> Result<()> {
WindowsResource::new()
.set_icon("../resources/images/icon.ico")
.compile()?;

println!("cargo:rerun-if-changed=../resources/images/splash.jpg");
}
Ok(())
}
9 changes: 9 additions & 0 deletions entrypoint/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub fn attach_console() {
#[cfg(target_os = "windows")]
{
use windows::Win32::System::Console::AttachConsole;
unsafe {
AttachConsole(u32::MAX).as_bool();
}
}
}
12 changes: 2 additions & 10 deletions entrypoint/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,9 @@ type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
use pyo3::prelude::*;
use pyo3::types::PyTuple;

const SWIFT_CONSOLE_PID: &str = "SWIFT_CONSOLE_PID";
use entrypoint::attach_console;

fn attach_console() {
#[cfg(target_os = "windows")]
{
use windows::Win32::System::Console::AttachConsole;
unsafe {
AttachConsole(u32::MAX).as_bool();
}
}
}
const SWIFT_CONSOLE_PID: &str = "SWIFT_CONSOLE_PID";

fn handle_wayland() {
#[cfg(target_os = "linux")]
Expand Down
76 changes: 50 additions & 26 deletions entrypoint/src/splash.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,66 @@
#![cfg_attr(target_os = "windows", windows_subsystem = "windows")]
use minifb::{Window, WindowOptions};

use std::{
io::Cursor,
path::PathBuf,
time::{Duration, Instant},
};

use lazy_static::lazy_static;
use minifb::{Window, WindowOptions};
use winit::{event_loop::EventLoop, window::Window as WinitWindow};
const TIMEOUT_DURATION: Duration = Duration::from_secs(15);

use entrypoint::attach_console;

const TIMEOUT_DURATION: Duration = Duration::from_secs(30);
const TEMP_FILENAME: &str = "swiftnav_console";

pub type Error = Box<dyn std::error::Error>;
pub type Result<T> = std::result::Result<T, Error>;
type Error = Box<dyn std::error::Error>;
type Result<T> = std::result::Result<T, Error>;

lazy_static! {
static ref PID_FILE: PathBuf = {
let pid = std::process::id();
std::env::temp_dir()
.join(format!("{TEMP_FILENAME}.{pid}"))
.into()
};
}

fn rgb8_3_to_rgb32(r: u8, g: u8, b: u8) -> u32 {
((r as u32) << 16) | ((g as u32) << 8) | (b as u32)
}

fn create_temp_file() -> Result<PathBuf> {
let pid = std::process::id();
let temp_filename = std::env::temp_dir().join(format!("{TEMP_FILENAME}.{pid}"));
std::fs::File::create(&temp_filename)?;
Ok(temp_filename)
std::fs::File::create(&*PID_FILE)?;
Ok(PID_FILE.clone())
}

fn main() {
let logo = include_bytes!("../../resources/images/LogoBackground.jpg");
fn launch_splash() -> Result<()> {
attach_console();
let logo = include_bytes!("../../resources/images/splash.jpg");
let image = image::io::Reader::with_format(
std::io::BufReader::new(Cursor::new(logo)),
image::ImageFormat::Jpeg,
)
.decode()
.unwrap();
.decode()?;
let u32_buffer: Vec<u32> = image
.as_bytes()
.chunks(3)
.map(|v| rgb8_3_to_rgb32(v[0], v[1], v[2]))
.collect();
let current_monitor = WinitWindow::new(&EventLoop::new())
.unwrap()
let current_monitor = WinitWindow::new(&EventLoop::new())?
.current_monitor()
.unwrap();
.ok_or_else(|| Into::<Error>::into(String::from("could not get current monitor")))?;
let size = current_monitor.size();
let pos_x = ((size.width as f64 / current_monitor.scale_factor() - image.width() as f64) / 2.0)
as isize;
let pos_y = ((size.height as f64 / current_monitor.scale_factor() - image.height() as f64)
/ 2.0) as isize;
let (width, height) = if cfg!(target_os = "macos") {
let size = size.to_logical::<f64>(current_monitor.scale_factor());
(size.width, size.height)
} else {
(size.width as f64, size.height as f64)
};
let pos_x = ((width - image.width() as f64) / 2.0) as isize;
let pos_y = ((height - image.height() as f64) / 2.0) as isize;

let mut window = Window::new(
"",
Expand All @@ -53,19 +69,27 @@ fn main() {
WindowOptions {
title: false,
borderless: true,
topmost: true,
none: true,
..WindowOptions::default()
},
)
.expect("unable to open window");
)?;

let temp_filename = create_temp_file().unwrap();
let temp_filename = create_temp_file()?;
let now = Instant::now();
while window.is_open() && now.elapsed() < TIMEOUT_DURATION && temp_filename.exists() {
window
.update_with_buffer(&u32_buffer, image.width() as usize, image.height() as usize)
.unwrap();
window.update_with_buffer(&u32_buffer, image.width() as usize, image.height() as usize)?;
window.set_position(pos_x, pos_y);
}

Ok(())
}

fn main() -> Result<()> {
let result = launch_splash();
if let Err(ref err) = result {
eprint!("Error launching splash screen: {err}");
}
// Try to remove the file, don't care about the result
let _result = std::fs::remove_file(&*PID_FILE);
result
}
1 change: 1 addition & 0 deletions resources/images/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Console splash source material: https://docs.google.com/drawings/d/1n9LqRhym0tb6ubIviUbDLuADAX4GPtj6DT8R9KcmYe0/edit
Binary file modified resources/images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/images/splash.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.