From 109452968762175b255d3a46d2447bf103022a68 Mon Sep 17 00:00:00 2001
From: awiouy <awiouy@gmail.com>
Date: Wed, 7 Nov 2018 07:49:31 +0100
Subject: [PATCH] libreelec: kodi hooks

---
 playback/src/player.rs      | 44 ++++++++++++++++++++++++++-----------
 src/player_event_handler.rs |  8 +++++--
 2 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/playback/src/player.rs b/playback/src/player.rs
index ab1a8ab..0aa0630 100644
--- a/playback/src/player.rs
+++ b/playback/src/player.rs
@@ -49,15 +49,18 @@ enum PlayerCommand {
 pub enum PlayerEvent {
     Started {
         track_id: SpotifyId,
+        new_state: String,
     },
 
     Changed {
         old_track_id: SpotifyId,
         new_track_id: SpotifyId,
+        new_state: String,
     },
 
     Stopped {
         track_id: SpotifyId,
+        new_state: String,
     },
 }
 
@@ -413,11 +416,18 @@ impl PlayerInternal {
                                 | PlayerState::EndOfTrack {
                                     track_id: old_track_id,
                                     ..
-                                } => self.send_event(PlayerEvent::Changed {
-                                    old_track_id: old_track_id,
-                                    new_track_id: track_id,
-                                }),
-                                _ => self.send_event(PlayerEvent::Started { track_id }),
+                                } => {
+                                    let new_state = "play".to_string();
+                                    self.send_event(PlayerEvent::Changed {
+                                        old_track_id: old_track_id,
+                                        new_track_id: track_id,
+                                        new_state: new_state,
+                                    });
+                                },
+                                _ => {
+                                    let new_state = "play".to_string();
+                                    self.send_event(PlayerEvent::Started { track_id, new_state });
+                                },
                             }
 
                             self.start_sink();
@@ -443,13 +453,18 @@ impl PlayerInternal {
                                 | PlayerState::EndOfTrack {
                                     track_id: old_track_id,
                                     ..
-                                } => self.send_event(PlayerEvent::Changed {
-                                    old_track_id: old_track_id,
-                                    new_track_id: track_id,
-                                }),
+                                } => {
+                                    let new_state = "pause".to_string();
+                                    self.send_event(PlayerEvent::Changed {
+                                        old_track_id: old_track_id,
+                                        new_track_id: track_id,
+                                        new_state: new_state,
+                                    })
+                                },
                                 _ => (),
                             }
-                            self.send_event(PlayerEvent::Stopped { track_id });
+                            let new_state = "pause".to_string();
+                            self.send_event(PlayerEvent::Stopped { track_id, new_state });
                         }
                     }
 
@@ -474,7 +489,8 @@ impl PlayerInternal {
                 if let PlayerState::Paused { track_id, .. } = self.state {
                     self.state.paused_to_playing();
 
-                    self.send_event(PlayerEvent::Started { track_id });
+                    let new_state = "play".to_string();
+                    self.send_event(PlayerEvent::Started { track_id, new_state });
                     self.start_sink();
                 } else {
                     warn!("Player::play called from invalid state");
@@ -486,7 +502,8 @@ impl PlayerInternal {
                     self.state.playing_to_paused();
 
                     self.stop_sink_if_running();
-                    self.send_event(PlayerEvent::Stopped { track_id });
+                    let new_state = "pause".to_string();
+                    self.send_event(PlayerEvent::Stopped { track_id, new_state });
                 } else {
                     warn!("Player::pause called from invalid state");
                 }
@@ -497,7 +514,8 @@ impl PlayerInternal {
                 | PlayerState::Paused { track_id, .. }
                 | PlayerState::EndOfTrack { track_id } => {
                     self.stop_sink_if_running();
-                    self.send_event(PlayerEvent::Stopped { track_id });
+                    let new_state = "stop".to_string();
+                    self.send_event(PlayerEvent::Stopped { track_id, new_state });
                     self.state = PlayerState::Stopped;
                 }
                 PlayerState::Stopped => {
diff --git a/src/player_event_handler.rs b/src/player_event_handler.rs
index 1e682b9..3b478f1 100644
--- a/src/player_event_handler.rs
+++ b/src/player_event_handler.rs
@@ -19,18 +19,22 @@ pub fn run_program_on_events(event: PlayerEvent, onevent: &str) -> io::Result<Ch
         PlayerEvent::Changed {
             old_track_id,
             new_track_id,
+            new_state,
         } => {
             env_vars.insert("PLAYER_EVENT", "change".to_string());
             env_vars.insert("OLD_TRACK_ID", old_track_id.to_base62());
             env_vars.insert("TRACK_ID", new_track_id.to_base62());
+            env_vars.insert("STATE", new_state.to_string());
         }
-        PlayerEvent::Started { track_id } => {
+        PlayerEvent::Started { track_id, new_state } => {
             env_vars.insert("PLAYER_EVENT", "start".to_string());
             env_vars.insert("TRACK_ID", track_id.to_base62());
+            env_vars.insert("STATE", new_state.to_string());
         }
-        PlayerEvent::Stopped { track_id } => {
+        PlayerEvent::Stopped { track_id, new_state } => {
             env_vars.insert("PLAYER_EVENT", "stop".to_string());
             env_vars.insert("TRACK_ID", track_id.to_base62());
+            env_vars.insert("STATE", new_state.to_string());
         }
     }
     run_program(onevent, env_vars)
