Skip to content

Commit b7556e2

Browse files
author
Jason Mobarak
committed
make fallible so we can remove the splash pid
1 parent 9b89e0d commit b7556e2

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

entrypoint/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ name = "swift-console-splash"
2121
path = "src/splash.rs"
2222
required-features = ["splash"]
2323

24+
[dependencies.lazy_static]
25+
optional = true
26+
version = "1.4.0"
27+
2428
[dependencies.winit]
2529
optional = true
2630
version = "0.26.1"
@@ -41,7 +45,7 @@ version = "0.16"
4145

4246
[features]
4347
entrypoint = ["pyo3", "windows"]
44-
splash = ["minifb", "image", "winit", "windows"]
48+
splash = ["minifb", "image", "winit", "windows", "lazy_static"]
4549

4650
[target]
4751
[target."cfg(target_os = \"windows\")"]

entrypoint/src/splash.rs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,56 @@ use std::{
66
time::{Duration, Instant},
77
};
88

9+
use lazy_static::lazy_static;
910
use minifb::{Window, WindowOptions};
10-
1111
use winit::{event_loop::EventLoop, window::Window as WinitWindow};
1212

1313
use entrypoint::attach_console;
1414

15-
1615
const TIMEOUT_DURATION: Duration = Duration::from_secs(15);
1716
const TEMP_FILENAME: &str = "swiftnav_console";
1817

19-
pub type Error = Box<dyn std::error::Error>;
20-
pub type Result<T> = std::result::Result<T, Error>;
18+
type Error = Box<dyn std::error::Error>;
19+
type Result<T> = std::result::Result<T, Error>;
20+
21+
lazy_static! {
22+
static ref PID_FILE: PathBuf = {
23+
let pid = std::process::id();
24+
std::env::temp_dir().join(format!("{TEMP_FILENAME}.{pid}")).into()
25+
};
26+
}
2127

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

2632
fn create_temp_file() -> Result<PathBuf> {
27-
let pid = std::process::id();
28-
let temp_filename = std::env::temp_dir().join(format!("{TEMP_FILENAME}.{pid}"));
29-
std::fs::File::create(&temp_filename)?;
30-
Ok(temp_filename)
33+
std::fs::File::create(&*PID_FILE)?;
34+
Ok(PID_FILE.clone())
3135
}
3236

33-
fn main() {
37+
fn launch_splash() -> Result<()> {
3438
attach_console();
3539
let logo = include_bytes!("../../resources/images/splash.jpg");
3640
let image = image::io::Reader::with_format(
3741
std::io::BufReader::new(Cursor::new(logo)),
3842
image::ImageFormat::Jpeg,
3943
)
40-
.decode()
41-
.unwrap();
44+
.decode()?;
4245
let u32_buffer: Vec<u32> = image
4346
.as_bytes()
4447
.chunks(3)
4548
.map(|v| rgb8_3_to_rgb32(v[0], v[1], v[2]))
4649
.collect();
47-
let current_monitor = WinitWindow::new(&EventLoop::new())
48-
.unwrap()
50+
let current_monitor = WinitWindow::new(&EventLoop::new())?
4951
.current_monitor()
50-
.unwrap();
52+
.ok_or_else(||{
53+
Into::<Error>::into(String::from("could not get current monitor"))
54+
})?;
5155
let size = current_monitor.size();
52-
let pos_x = ((size.width as f64 / current_monitor.scale_factor() - image.width() as f64) / 2.0)
56+
let pos_x = ((size.width as f64 - image.width() as f64) / 2.0)
5357
as isize;
54-
let pos_y = ((size.height as f64 / current_monitor.scale_factor() - image.height() as f64)
58+
let pos_y = ((size.height as f64 - image.height() as f64)
5559
/ 2.0) as isize;
5660

5761
let mut window = Window::new(
@@ -65,15 +69,25 @@ fn main() {
6569
none: true,
6670
..WindowOptions::default()
6771
},
68-
)
69-
.expect("unable to open window");
72+
)?;
7073

71-
let temp_filename = create_temp_file().unwrap();
74+
let temp_filename = create_temp_file()?;
7275
let now = Instant::now();
7376
while window.is_open() && now.elapsed() < TIMEOUT_DURATION && temp_filename.exists() {
7477
window
75-
.update_with_buffer(&u32_buffer, image.width() as usize, image.height() as usize)
76-
.unwrap();
78+
.update_with_buffer(&u32_buffer, image.width() as usize, image.height() as usize)?;
7779
window.set_position(pos_x, pos_y);
7880
}
81+
82+
Ok(())
83+
}
84+
85+
fn main() -> Result<()> {
86+
let result = launch_splash();
87+
if let Err(ref err) = result {
88+
eprint!("Error launching splash screen: {err}");
89+
}
90+
// Try to remove the file, don't care about the result
91+
let _result = std::fs::remove_file(&*PID_FILE);
92+
result
7993
}

0 commit comments

Comments
 (0)