mpris implementation, function cleanup
This commit is contained in:
parent
8106f0ac2e
commit
fe73e34833
5 changed files with 69 additions and 41 deletions
6
index.js
6
index.js
|
@ -1,5 +1,5 @@
|
|||
require('v8-compile-cache');
|
||||
const { app, screen } = require('electron');
|
||||
const { app } = require('electron');
|
||||
|
||||
// Analytics for debugging.
|
||||
const ElectronSentry = require("@sentry/electron");
|
||||
|
@ -72,6 +72,10 @@ function CreateWindow() {
|
|||
/** CIDER **/
|
||||
}
|
||||
|
||||
if (process.platform === "linux") {
|
||||
app.commandLine.appendSwitch('disable-features', 'MediaSessionService');
|
||||
}
|
||||
|
||||
|
||||
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* App Event Handlers
|
||||
|
|
|
@ -5,10 +5,9 @@ const express = require("express");
|
|||
const path = require("path");
|
||||
const windowStateKeeper = require("electron-window-state");
|
||||
const os = require('os');
|
||||
const Store = require("electron-store");
|
||||
const store = new Store();
|
||||
const yt = require('youtube-search-without-api-key');
|
||||
const discord = require('./discordrpc');
|
||||
const mpris = require('./mpris');
|
||||
|
||||
// Analytics for debugging.
|
||||
const ElectronSentry = require("@sentry/electron");
|
||||
|
@ -16,7 +15,7 @@ ElectronSentry.init({dsn: "https://68c422bfaaf44dea880b86aad5a820d2@o954055.inge
|
|||
|
||||
const CiderBase = {
|
||||
async Start() {
|
||||
this.clientPort = await getPort({port : 9000});
|
||||
this.clientPort = await getPort({port: 9000});
|
||||
this.CreateBrowserWindow()
|
||||
},
|
||||
clientPort: 0,
|
||||
|
@ -85,14 +84,14 @@ const CiderBase = {
|
|||
)
|
||||
|
||||
win.webContents.session.webRequest.onBeforeSendHeaders(async (details, callback) => {
|
||||
if(details.url == "https://buy.itunes.apple.com/account/web/info"){
|
||||
if (details.url === "https://buy.itunes.apple.com/account/web/info") {
|
||||
details.requestHeaders['sec-fetch-site'] = 'same-site';
|
||||
details.requestHeaders['DNT'] = '1';
|
||||
let itspod = await win.webContents.executeJavaScript(`window.localStorage.getItem("music.ampwebplay.itspod")`)
|
||||
if (itspod != null)
|
||||
details.requestHeaders['Cookie'] = `itspod=${itspod}`
|
||||
}
|
||||
callback({ requestHeaders: details.requestHeaders })
|
||||
callback({requestHeaders: details.requestHeaders})
|
||||
})
|
||||
|
||||
let location = `http://localhost:${CiderBase.clientPort}/`
|
||||
|
@ -187,9 +186,10 @@ const CiderBase = {
|
|||
// Set window Handler
|
||||
win.webContents.setWindowOpenHandler(({url}) => {
|
||||
if (url.includes("apple") || url.includes("localhost")) {
|
||||
return { action: "allow"}
|
||||
return {action: "allow"}
|
||||
}
|
||||
shell.openExternal(url).catch(() => {})
|
||||
shell.openExternal(url).catch(() => {
|
||||
})
|
||||
return {
|
||||
action: 'deny'
|
||||
}
|
||||
|
@ -202,13 +202,17 @@ const CiderBase = {
|
|||
|
||||
win.webContents.setZoomFactor(screen.getPrimaryDisplay().scaleFactor)
|
||||
|
||||
mpris.connect(win)
|
||||
|
||||
// Discord
|
||||
discord.connect('911790844204437504');
|
||||
ipcMain.on('playbackStateDidChange', (_event, a) => {
|
||||
discord.updateActivity(a)
|
||||
mpris.updateState(a)
|
||||
});
|
||||
ipcMain.on('nowPlayingItemDidChange', (_event, a) => {
|
||||
discord.updateActivity(a)
|
||||
mpris.updateAttributes(a)
|
||||
});
|
||||
|
||||
return win
|
||||
|
|
|
@ -2,16 +2,21 @@ const {app} = require('electron'),
|
|||
DiscordRPC = require('discord-rpc')
|
||||
|
||||
module.exports = {
|
||||
|
||||
/**
|
||||
* Connects to Discord RPC
|
||||
* @param {string} clientId
|
||||
*/
|
||||
connect: function (clientId) {
|
||||
app.discord = {isConnected: false};
|
||||
if (!app.cfg.get('general.discord_rpc')) return;
|
||||
|
||||
DiscordRPC.register(clientId) // Apparently needed for ask to join, join, spectate etc.
|
||||
const client = new DiscordRPC.Client({ transport: "ipc" });
|
||||
app.discord = Object.assign(client,{error: false, activityCache: null, isConnected: false});
|
||||
const client = new DiscordRPC.Client({transport: "ipc"});
|
||||
app.discord = Object.assign(client, {error: false, activityCache: null, isConnected: false});
|
||||
|
||||
// Login to Discord
|
||||
app.discord.login({ clientId })
|
||||
app.discord.login({clientId})
|
||||
.then(() => {
|
||||
app.discord.isConnected = true;
|
||||
})
|
||||
|
@ -34,6 +39,9 @@ module.exports = {
|
|||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Disconnects from Discord RPC
|
||||
*/
|
||||
disconnect: function () {
|
||||
if (!app.cfg.get('general.discord_rpc') || !app.discord.isConnected) return;
|
||||
|
||||
|
@ -47,6 +55,10 @@ module.exports = {
|
|||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets the activity of the client
|
||||
* @param {object} attributes
|
||||
*/
|
||||
updateActivity: function (attributes) {
|
||||
if (!app.cfg.get('general.discord_rpc')) return;
|
||||
|
||||
|
@ -65,16 +77,18 @@ module.exports = {
|
|||
state: `by ${attributes.artistName}`,
|
||||
startTimestamp: attributes.startTime,
|
||||
endTimestamp: attributes.endTime,
|
||||
largeImageKey: (attributes.artwork.url.replace('{w}', '512').replace('{h}', '512') ) ?? 'cider',
|
||||
largeImageKey: (attributes.artwork.url.replace('{w}', '512').replace('{h}', '512')) ?? 'cider',
|
||||
largeImageText: attributes.albumName,
|
||||
smallImageKey: (attributes.status ? 'play' : 'pause'),
|
||||
smallImageText: (attributes.status ? 'Playing': 'Paused'),
|
||||
smallImageText: (attributes.status ? 'Playing' : 'Paused'),
|
||||
instance: true,
|
||||
buttons: [
|
||||
{label: "Listen on Cider", url: listenURL},
|
||||
]
|
||||
};
|
||||
if (ActivityObject.largeImageKey == "" || ActivityObject.largeImageKey == null) {ActivityObject.largeImageKey = "cider"}
|
||||
if (ActivityObject.largeImageKey == "" || ActivityObject.largeImageKey == null) {
|
||||
ActivityObject.largeImageKey = "cider"
|
||||
}
|
||||
//console.log(`[LinkHandler] Listening URL has been set to: ${listenURL}`);
|
||||
|
||||
// if (app.cfg.get('general.discordClearActivityOnPause')) {
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
const {app} = require('electron');
|
||||
|
||||
module.export = () => {
|
||||
if (process.platform === "linux") app.commandLine.appendSwitch('disable-features', 'MediaSessionService');
|
||||
}
|
|
@ -1,16 +1,16 @@
|
|||
const {app} = require('electron'),
|
||||
Player = require('mpris-service');
|
||||
|
||||
// Remember to use playerctl when debugging this.
|
||||
// I'm just putting this here as I keep forgetting the command.
|
||||
// Copied from AME
|
||||
|
||||
let mediaPlayer;
|
||||
let mediaPlayer = null;
|
||||
|
||||
module.exports = {
|
||||
|
||||
/**
|
||||
* Connects to the MPRIS interface.
|
||||
* @param {Object} win - The BrowserWindow.
|
||||
*/
|
||||
connect: (win) => {
|
||||
if (process.platform !== "linux") return;
|
||||
|
||||
const Player = require('mpris-service');
|
||||
|
||||
mediaPlayer = Player({
|
||||
name: 'Cider',
|
||||
identity: 'Cider',
|
||||
|
@ -24,7 +24,7 @@ module.exports = {
|
|||
let pos_atr = {durationInMillis: 0};
|
||||
mediaPlayer.getPosition = function () {
|
||||
const durationInMicro = pos_atr.durationInMillis * 1000;
|
||||
const percentage = parseFloat(0) || 0;
|
||||
const percentage = parseFloat("0") || 0;
|
||||
return durationInMicro * percentage;
|
||||
}
|
||||
|
||||
|
@ -51,6 +51,10 @@ module.exports = {
|
|||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Updates the MPRIS interface.
|
||||
* @param {Object} attributes - The attributes of the track.
|
||||
*/
|
||||
updateAttributes: (attributes) => {
|
||||
if (process.platform !== "linux") return;
|
||||
|
||||
|
@ -71,6 +75,10 @@ module.exports = {
|
|||
mediaPlayer.metadata = MetaData
|
||||
},
|
||||
|
||||
/**
|
||||
* Updates the playback state of the MPRIS interface.
|
||||
* @param {Object} attributes - The attributes of the track.
|
||||
*/
|
||||
updateState: (attributes) => {
|
||||
if (process.platform !== "linux") return;
|
||||
|
||||
|
@ -94,6 +102,9 @@ module.exports = {
|
|||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Closes the MPRIS interface.
|
||||
*/
|
||||
clearActivity: () => {
|
||||
if (process.platform !== "linux") return;
|
||||
mediaPlayer.metadata = {'mpris:trackid': '/org/mpris/MediaPlayer2/TrackList/NoTrack'}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue