@@ -6,52 +6,56 @@ use std::{
66 time:: { Duration , Instant } ,
77} ;
88
9+ use lazy_static:: lazy_static;
910use minifb:: { Window , WindowOptions } ;
10-
1111use winit:: { event_loop:: EventLoop , window:: Window as WinitWindow } ;
1212
1313use entrypoint:: attach_console;
1414
15-
1615const TIMEOUT_DURATION : Duration = Duration :: from_secs ( 15 ) ;
1716const 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
2228fn 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
2632fn 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