From bb49c112db88b7812fb61853beef3e6cd6769bff Mon Sep 17 00:00:00 2001 From: Mastermindzh Date: Wed, 30 Oct 2019 23:42:08 +0100 Subject: [PATCH] added player status and worked on getting the integrations working with i3: https://github.com/Mastermindzh/dotfiles/commit/9714b2fa1d670108ce811d5511fd3b7a43180647 --- README.md | 2 +- src/constants/statuses.js | 4 ++++ src/main.js | 4 ++++ src/preload.js | 16 ++++++++++++++++ src/scripts/express.js | 17 +++++++++++++++++ src/scripts/mediaInfo.js | 15 +++++++++++++++ 6 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/constants/statuses.js diff --git a/README.md b/README.md index 6d7ef33..53afb3a 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ When I started this project there weren't any Linux apps that offered Tidal's "h ## Integrations -- [i3 blocks config]() - My dotfiles where I use this app to fetch currently playing music +- [i3 blocks config](https://github.com/Mastermindzh/dotfiles/commit/9714b2fa1d670108ce811d5511fd3b7a43180647) - My dotfiles where I use this app to fetch currently playing music (direct commit) ## Why not extend existing projects? diff --git a/src/constants/statuses.js b/src/constants/statuses.js new file mode 100644 index 0000000..33ae17a --- /dev/null +++ b/src/constants/statuses.js @@ -0,0 +1,4 @@ +module.exports = { + playing: "playing", + paused: "paused", +}; diff --git a/src/main.js b/src/main.js index f32ac80..993c3a3 100644 --- a/src/main.js +++ b/src/main.js @@ -83,3 +83,7 @@ app.on("activate", function() { ipcMain.on("update-info", (event, arg) => { mediaInfoModule.update(arg); }); + +ipcMain.on("update-status", (event, arg) => { + mediaInfoModule.updateStatus(arg); +}); diff --git a/src/preload.js b/src/preload.js index 69d9006..fdae001 100644 --- a/src/preload.js +++ b/src/preload.js @@ -4,6 +4,7 @@ const { settings } = require("./scripts/settings"); const { ipcRenderer } = require("electron"); const { app } = require("electron").remote; const { downloadFile } = require("./scripts/download"); +const statuses = require("./constants/statuses"); const hotkeys = require("./scripts/hotkeys"); const globalEvents = require("./constants/globalEvents"); const notifier = require("node-notifier"); @@ -210,6 +211,19 @@ function addIPCEventListeners() { }); } +/** + * Update the current status of tidal (e.g playing or paused) + */ +function updateStatus() { + const play = elements.get("play"); + let status = statuses.paused; + // if play button is NOT visible tidal is playing + if (!play) { + status = statuses.playing; + } + ipcRenderer.send("update-status", status); +} + /** * Watch for song changes and update title + notify */ @@ -218,6 +232,8 @@ setInterval(function() { const artists = elements.getText("artists"); const songDashArtistTitle = `${title} - ${artists}`; + updateStatus(); + if (getTitle() !== songDashArtistTitle) { setTitle(songDashArtistTitle); diff --git a/src/scripts/express.js b/src/scripts/express.js index f0cc71a..cc6852b 100644 --- a/src/scripts/express.js +++ b/src/scripts/express.js @@ -2,10 +2,20 @@ const express = require("express"); const { mediaInfo } = require("./mediaInfo"); const settingsModule = require("./settings"); const globalEvents = require("./../constants/globalEvents"); +const statuses = require("./../constants/statuses"); + const expressModule = {}; var fs = require("fs"); +/** + * Function to enable tidal-hifi's express api + */ expressModule.run = function(mainWindow) { + /** + * Shorthand to handle a fire and forget global event + * @param {*} res + * @param {*} action + */ function handleGlobalEvent(res, action) { mainWindow.webContents.send("globalEvent", action); res.sendStatus(200); @@ -18,6 +28,13 @@ expressModule.run = function(mainWindow) { expressApp.get("/pause", (req, res) => handleGlobalEvent(res, globalEvents.pause)); expressApp.get("/next", (req, res) => handleGlobalEvent(res, globalEvents.next)); expressApp.get("/previous", (req, res) => handleGlobalEvent(res, globalEvents.previous)); + expressApp.get("/playpause", (req, res) => { + if (mediaInfo.status == statuses.playing) { + handleGlobalEvent(res, globalEvents.pause); + } else { + handleGlobalEvent(res, globalEvents.play); + } + }); expressApp.get("/image", (req, res) => { var stream = fs.createReadStream(mediaInfo.icon); stream.on("open", function() { diff --git a/src/scripts/mediaInfo.js b/src/scripts/mediaInfo.js index cab34ed..79f6dc4 100644 --- a/src/scripts/mediaInfo.js +++ b/src/scripts/mediaInfo.js @@ -1,18 +1,33 @@ +const statuses = require("./../constants/statuses"); + const mediaInfo = { title: "", artist: "", icon: "", + status: statuses.paused, }; const mediaInfoModule = { mediaInfo, }; +/** + * Update artist and song info in the mediaInfo constant + */ mediaInfoModule.update = function(arg) { mediaInfo.title = propOrDefault(arg.title); mediaInfo.artist = propOrDefault(arg.message); mediaInfo.icon = propOrDefault(arg.icon); }; +/** + * Update tidal's status in the mediaInfo constant + */ +mediaInfoModule.updateStatus = function(status) { + if (Object.values(statuses).includes(status)) { + mediaInfo.status = status; + } +}; + /** * Return the property or a default value * @param {*} prop property to check