Skip to content

Commit

Permalink
Added env variables to shell events (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed May 26, 2021
1 parent cf0774a commit 90a53e6
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions player/src/main/java/xyz/gianlu/librespot/player/ShellEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public ShellEvents(@NotNull Configuration conf) {
this.conf = conf;
}

private void exec(String command) {
private void exec(String command, String... envp) {
if (!this.conf.enabled)
return;

if (command == null || command.trim().isEmpty())
return;

try {
int exitCode = runtime.exec(command.trim()).waitFor();
int exitCode = runtime.exec(command.trim(), envp).waitFor();
LOGGER.trace("Executed shell command: {} -> {}", command, exitCode);
} catch (IOException | InterruptedException ex) {
LOGGER.error("Failed executing command: {}", command, ex);
Expand All @@ -57,12 +57,16 @@ private void exec(String command) {

@Override
public void onContextChanged(@NotNull Player player, @NotNull String newUri) {
exec(conf.onContextChanged);
exec(conf.onContextChanged, "CONTEXT_URI=" + newUri);
}

@Override
public void onTrackChanged(@NotNull Player player, @NotNull PlayableId id, @Nullable MetadataWrapper metadata) {
exec(conf.onTrackChanged);
exec(conf.onTrackChanged, "TRACK_URI=" + id.toSpotifyUri(),
"NAME=" + (metadata == null ? "" : metadata.getName()),
"ARTIST=" + (metadata == null ? "" : metadata.getArtist()),
"ALBUM=" + (metadata == null ? "" : metadata.getAlbumName()),
"DURATION=" + (metadata == null ? "" : metadata.duration()));
}

@Override
Expand All @@ -72,22 +76,24 @@ public void onPlaybackEnded(@NotNull Player player) {

@Override
public void onPlaybackPaused(@NotNull Player player, long trackTime) {
exec(conf.onPlaybackPaused);
exec(conf.onPlaybackPaused, "POSITION=" + trackTime);
}

@Override
public void onPlaybackResumed(@NotNull Player player, long trackTime) {
exec(conf.onPlaybackResumed);
exec(conf.onPlaybackResumed, "POSITION=" + trackTime);
}

@Override
public void onTrackSeeked(@NotNull Player player, long trackTime) {
exec(conf.onTrackSeeked);
exec(conf.onTrackSeeked, "POSITION=" + trackTime);
}

@Override
public void onMetadataAvailable(@NotNull Player player, @NotNull MetadataWrapper metadata) {
exec(conf.onMetadataAvailable);
exec(conf.onMetadataAvailable, "TRACK_URI=" + metadata.id.toSpotifyUri(),
"NAME=" + metadata.getName(), "ARTIST=" + metadata.getArtist(),
"ALBUM=" + metadata.getAlbumName(), "DURATION=" + metadata.duration());
}

@Override
Expand All @@ -101,7 +107,7 @@ public void onInactiveSession(@NotNull Player player, boolean timeout) {

@Override
public void onVolumeChanged(@NotNull Player player, @Range(from = 0, to = 1) float volume) {
exec(conf.onVolumeChanged);
exec(conf.onVolumeChanged, "VOLUME=" + Math.round(volume * 100f));
}

@Override
Expand Down

0 comments on commit 90a53e6

Please sign in to comment.