ccgauche / ytermusic

An in terminal youtube music client with focus on privacy, simplicity and performance
Apache License 2.0
360 stars 19 forks source link

Keyboard media controls not working #90

Open ZeroByter opened 3 months ago

ZeroByter commented 3 months ago

While playing a song, trying to pause/play songs, skip forward to the next stop, or going back to the start of the song (or last song if pressed twice quickly) via keyboard buttons doesn't work. I'm on Ubuntu.

ccgauche commented 3 months ago

Hello thanks for your feedback! Are you talking about DBus controls or shortcuts in the app ? What terminal are you using? Can you send the ytermusic log?

ZeroByter commented 3 months ago

Not sure what is DBus controls, I'm referring to physical keyboard buttons (sometimes fn+key buttons) to pause/play, next track and previous track.

I actually cloned your repo and tried to implement this myself but couldn't get it to work, even though I think it should work, here is what I tried:

Added line PushKeyboardEnhancementFlags(KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES); in mod.rs

pub fn run(&mut self, updater: &Receiver<ManagerMessage>) -> Result<(), io::Error> {
        // setup terminal
        enable_raw_mode()?;
        let mut stdout = io::stdout();
        execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
        let backend = CrosstermBackend::new(stdout);
        let mut terminal = Terminal::new(backend)?;

        // create app and run it
        let tick_rate = Duration::from_millis(250);

        // V Added this line here V
        PushKeyboardEnhancementFlags(KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES);
        // ^ Added this line here ^

        let mut last_tick = Instant::now();
        'a: loop {

Added lines in music_player.rs

fn on_key_press(&mut self, key: KeyEvent, _: &ratatui::layout::Rect) -> EventResponse {
        match key.code {
            KeyCode::Media(media_key) => match media_key {
                MediaKeyCode::PlayPause => {
                    SoundAction::PlayPause.apply_sound_action(self);
                    EventResponse::None
                }
                _ => EventResponse::None,
            },
        ...

I'm actually not that familiar with Rust, so I tried making a new empty Rust project just to see if I could at all capture these MediaKeyCodes but I couldn't, perhaps it's a problem with crossterm? Or with Linux? I haven't tried on Windows yet.

Where is the log file? I'll happily send it.

ccgauche commented 3 months ago

I don't know how FN + FX keys are handled but it think they should use DBus too (A linux protocol for media actions cross-desktop). To confirm I've been using souvlaki underneath so maybe you should look there. Thanks for the investigation!

ZeroByter commented 3 months ago

I just googled souvlaki and it looks promising, I'll give it a try later today and if I hopefully get it to work, will send a PR :) Thanks for the help!

Edited update: I just found the souvlaki code in the project and messed around with it and I just now realized what is the DBus protocol that you are talking about, but that is not what I am referring to.

I am referring to these keyboard buttons:

I confirmed in a new standalone project, kebyoard media buttons are not captured by souvlaki

ZeroByter commented 3 months ago

Created a crossterm bug report about this: https://github.com/crossterm-rs/crossterm/issues/897