From 56f678489271c4bd1230fc138a53e41524345115 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 31 Jan 2023 15:57:56 +0900 Subject: [PATCH 1/5] Fix where display doesn't work when no password Previous implementation only worked if MPD was accessed with a password. This commit fixes no-password access of MPD. --- src/mpd_handler.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mpd_handler.rs b/src/mpd_handler.rs index b93226c..5064680 100644 --- a/src/mpd_handler.rs +++ b/src/mpd_handler.rs @@ -263,10 +263,10 @@ impl MPDHandler { current_binary_size: 0, poll_state: PollState::None, stream, - password, + password: password.clone(), error_text: String::new(), can_authenticate: true, - is_authenticated: false, + is_authenticated: password.is_empty(), can_get_album_art: true, can_get_album_art_in_dir: true, can_get_status: true, From d1590bee0aff62619d354c2494398dc945828197 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 31 Jan 2023 15:59:19 +0900 Subject: [PATCH 2/5] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index af255cd..dea052f 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,9 @@ counter, and the filename currently being played Currently, the dependency "ggez 0.8.1" [fails to render album art](https://github.com/Stephen-Seo/mpd_info_screen/issues/1) on my machines using this program. Version 0.4.1 cannot be published to https://crates.io due to this version referring to a git commit as a dependency. Once ggez has released a new version with the commit that fixes this bug, this repository will be updated to use that version. +The `devel` branch has a fix for mpd\_info\_screen not displaying properly when +no password is provided and MPD can be accessed without a password. + ## Unicode Support By default, unicode characters will not display properly. Build the project with From e0be69df81bf3c0d2c8622e5173a7d8de083e9bd Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 31 Jan 2023 16:19:35 +0900 Subject: [PATCH 3/5] Impl passing password via file --- src/main.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 7018214..2f697f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,8 @@ use ggez::event::winit_event::{ElementState, KeyboardInput, ModifiersState}; use ggez::event::{self, ControlFlow, EventHandler}; use ggez::input::keyboard::{self, KeyInput}; use ggez::{ContextBuilder, GameError}; +use std::fs::File; +use std::io::Read; use std::net::Ipv4Addr; use std::path::PathBuf; use std::thread; @@ -35,6 +37,8 @@ pub struct Opt { disable_show_filename: bool, #[structopt(long = "pprompt", help = "input password via prompt")] enable_prompt_password: bool, + #[structopt(long = "pfile", help = "read password from file")] + password_file: Option, #[structopt( long = "no-scale-fill", help = "don't scale-fill the album art to the window" @@ -58,9 +62,26 @@ pub struct Opt { } fn main() -> Result<(), String> { - let opt = Opt::from_args(); + let mut opt = Opt::from_args(); println!("Got host addr == {}, port == {}", opt.host, opt.port); + // Read password from file if exists, error otherwise. + if let Some(psswd_file_path) = opt.password_file.as_ref() { + let mut file = File::open(psswd_file_path).expect("pfile/password_file should exist"); + let mut content: String = String::new(); + + file.read_to_string(&mut content) + .expect("Should be able to read from pfile/password_file"); + + if content.ends_with("\r\n") { + content.truncate(content.len() - 2); + } else if content.ends_with('\n') { + content.truncate(content.len() - 1); + } + + opt.password = Some(content); + } + let (mut ctx, event_loop) = ContextBuilder::new("mpd_info_screen", "Stephen Seo") .window_setup(WindowSetup { title: "mpd info screen".into(), From a1706913e62229be40be24dabf1cebd311e75e37 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sat, 4 Feb 2023 20:41:47 +0900 Subject: [PATCH 4/5] Fix timer bg not updating This commit fixes the bg mesh (slightly-transparent-black-bg for text) not updating when the timer-text changes size. --- src/display.rs | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/display.rs b/src/display.rs index 974317c..9e7cd16 100644 --- a/src/display.rs +++ b/src/display.rs @@ -213,11 +213,17 @@ pub struct MPDDisplay { album_string_cache: String, album_transform: Transform, timer_text: Text, + timer_text_len: usize, timer_transform: Transform, timer_x: f32, timer_y: f32, timer: f64, length: f64, + cached_filename_y: f32, + cached_album_y: f32, + cached_artist_y: f32, + cached_title_y: f32, + cached_timer_y: f32, text_bg_mesh: Option, hide_text: bool, tried_album_art_in_dir: bool, @@ -247,11 +253,17 @@ impl MPDDisplay { title_text: Text::default(), title_transform: Transform::default(), timer_text: Text::new("0"), + timer_text_len: 0, timer_transform: Transform::default(), timer_x: INIT_FONT_SIZE_X, timer_y: INIT_FONT_SIZE_Y, timer: 0.0, length: 0.0, + cached_filename_y: 0.0f32, + cached_album_y: 0.0f32, + cached_artist_y: 0.0f32, + cached_title_y: 0.0f32, + cached_timer_y: 0.0f32, text_bg_mesh: None, hide_text: false, tried_album_art_in_dir: false, @@ -459,12 +471,6 @@ impl MPDDisplay { let mut offset_y: f32 = drawable_size.1; - let mut filename_y: f32 = 0.0; - let mut album_y: f32 = 0.0; - let mut artist_y: f32 = 0.0; - let mut title_y: f32 = 0.0; - let mut timer_y: f32 = 0.0; - let set_transform = |text: &mut Text, transform: &mut Transform, offset_y: &mut f32, @@ -551,7 +557,7 @@ impl MPDDisplay { &mut self.filename_text, &mut self.filename_transform, &mut offset_y, - &mut filename_y, + &mut self.cached_filename_y, true, false, false, @@ -571,7 +577,7 @@ impl MPDDisplay { &mut self.album_text, &mut self.album_transform, &mut offset_y, - &mut album_y, + &mut self.cached_album_y, true, false, true, @@ -585,7 +591,7 @@ impl MPDDisplay { &mut self.artist_text, &mut self.artist_transform, &mut offset_y, - &mut artist_y, + &mut self.cached_artist_y, true, true, false, @@ -605,7 +611,7 @@ impl MPDDisplay { &mut self.title_text, &mut self.title_transform, &mut offset_y, - &mut title_y, + &mut self.cached_title_y, true, false, false, @@ -624,7 +630,7 @@ impl MPDDisplay { &mut self.timer_text, &mut self.timer_transform, &mut offset_y, - &mut timer_y, + &mut self.cached_timer_y, false, false, false, @@ -632,6 +638,12 @@ impl MPDDisplay { &mut self.timer_y, ); + self.update_bg_mesh(ctx)?; + + Ok(()) + } + + fn update_bg_mesh(&mut self, ctx: &mut Context) -> GameResult<()> { let filename_dimensions = self .filename_text .dimensions(ctx) @@ -659,7 +671,7 @@ impl MPDDisplay { DrawMode::fill(), Rect { x: TEXT_X_OFFSET, - y: filename_y, + y: self.cached_filename_y, w: filename_dimensions.w, h: filename_dimensions.h, }, @@ -671,7 +683,7 @@ impl MPDDisplay { DrawMode::fill(), Rect { x: TEXT_X_OFFSET, - y: album_y, + y: self.cached_album_y, w: album_dimensions.w, h: album_dimensions.h, }, @@ -683,7 +695,7 @@ impl MPDDisplay { DrawMode::fill(), Rect { x: TEXT_X_OFFSET, - y: artist_y, + y: self.cached_artist_y, w: artist_dimensions.w, h: artist_dimensions.h, }, @@ -695,7 +707,7 @@ impl MPDDisplay { DrawMode::fill(), Rect { x: TEXT_X_OFFSET, - y: title_y, + y: self.cached_title_y, w: title_dimensions.w, h: title_dimensions.h, }, @@ -709,7 +721,7 @@ impl MPDDisplay { DrawMode::fill(), Rect { x: TEXT_X_OFFSET, - y: timer_y, + y: self.cached_timer_y, w: timer_dimensions.w, h: timer_dimensions.h, }, @@ -918,11 +930,16 @@ impl EventHandler for MPDDisplay { let delta = ctx.time.delta(); self.timer += delta.as_secs_f64(); let timer_diff = seconds_to_time(self.length - self.timer); + let timer_diff_len = timer_diff.len(); self.timer_text = Text::new(timer_diff); self.timer_text.set_scale(PxScale { x: self.timer_x, y: self.timer_y, }); + if timer_diff_len != self.timer_text_len { + self.timer_text_len = timer_diff_len; + self.update_bg_mesh(ctx)?; + } Ok(()) } From aa6fb750e7ac405a5330f04093e69a6c95dc5d94 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sat, 4 Feb 2023 21:32:09 +0900 Subject: [PATCH 5/5] Refactor "no-password-fix" Refactors the fix for the use case of when MPD requires no password. --- src/mpd_handler.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/mpd_handler.rs b/src/mpd_handler.rs index 5064680..80828af 100644 --- a/src/mpd_handler.rs +++ b/src/mpd_handler.rs @@ -250,6 +250,8 @@ impl MPDHandler { ) .map_err(|_| String::from("Failed to get TCP connection (is MPD running?)"))?; + let password_is_empty = password.is_empty(); + let s = MPDHandler { state: Arc::new(RwLock::new(MPDHandlerState { art_data: Vec::new(), @@ -263,10 +265,10 @@ impl MPDHandler { current_binary_size: 0, poll_state: PollState::None, stream, - password: password.clone(), + password, error_text: String::new(), can_authenticate: true, - is_authenticated: password.is_empty(), + is_authenticated: password_is_empty, can_get_album_art: true, can_get_album_art_in_dir: true, can_get_status: true,