diff --git a/dbus.js b/dbus.js index 97dbc4b..7c5307a 100644 --- a/dbus.js +++ b/dbus.js @@ -27,23 +27,76 @@ module.exports = function(appName){ }); } } + class DBusMPRIS extends EventEmitter{ + constructor(appName) { + super(); + this._player = Player({ + name: appName, + identity: appName, + supportedUriSchemes: ['http'], + supportedMimeTypes: ['application/www-url'], + desktopEntry: appName + }); + } + on(name, func){ + this._player.on(name, func); + } + set position(pos) { + if (pos !== this.position) this._player.position = pos; + } + get position() { + return this._player.position; + } + set playbackStatus(stat){ + if (stat !== this.playbackStatus) this._player.playbackStatus = stat; + } + get playbackStatus(){ + return this._player.playbackStatus; + } + set volume(vol) { + if (vol !== this.volume) this._player.volume = vol; + } + get volume() { + return this._player.volume; + } + set shuffle(shuff){ + if (shuff !== this.shuffle) this._player.shuffle = shuff; + } + get shuffle(){ + return this._player.shuffle; + } + set repeat(rep){ + if (rep !== this.repeat) this._player.repeat = rep; + } + get repeat() { + return this._player.repeat; + } + set metadata (met) { + if ( + (met && !this.metadata) || + (this.metadata && this.metadata['xesam:url'] !== met['xesam:url']) + ) { + this._player.metadata = met; + } + } + get metadata(){ + return this._player.metadata; + } + objectPath(str) { + return this._player.objectPath(str); + } + } return { mediakeys: new DBusMediaKeys(bus), notifications: { notify: (summary, body, icon) => { - console.log(summary, body, icon); + console.log(`notify('${summary}','${body.replace(/\n/g, '\\n')}','${icon}')`); notification.summary = summary; notification.body = body; notification.icon = icon; notification.push(); } }, - mpris: Player({ - name: appName, - identity: appName, - supportedUriSchemes: ['http'], - supportedMimeTypes: ['application/www-url'], - desktopEntry: appName - }) + mpris: new DBusMPRIS(appName) }; }; diff --git a/spotifywebplayer b/spotifywebplayer deleted file mode 100755 index 2dabdb6..0000000 --- a/spotifywebplayer +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -OLDDIR=$(pwd) -cd $DIR -$DIR/libs/electron/spotifywebplayer $DIR/. & -cd $OLDDIR diff --git a/windows/spotify/controller.js b/windows/spotify/controller.js index 5a3372c..2c97a56 100755 --- a/windows/spotify/controller.js +++ b/windows/spotify/controller.js @@ -8,22 +8,20 @@ module.exports = (function() { const dbus = props.dbus; updateMetadata = function(info){ if (dbus && info.track){ - if (!dbus.mpris.metadata || (dbus.mpris.metadata && dbus.mpris.metadata['xesam:url'] !== info.track.url)) { - dbus.mpris.metadata = { - 'mpris:trackid': dbus.mpris.objectPath('track/' + info.track.trackNumber), - 'mpris:length': info.track.length, - 'mpris:artUrl': info.track.art, - 'xesam:title': info.track.name.replace(/(\'| - .*| \(.*)/i, ''), //Remove long track titles - 'xesam:album': info.track.album.replace(/(\'| - .*| \(.*)/i, ''), //Remove long album names - 'xesam:artist': info.track.artists, - 'xesam:url': info.track.url - }; - } - if (dbus.mpris.volume != info.volume) dbus.mpris.volume = info.volume; - if (info.track.position && dbus.mpris.position != info.track.position) dbus.mpris.position = info.track.position; - if (dbus.mpris.playbackStatus != info.status) dbus.mpris.playbackStatus = info.status; - if (dbus.mpris.shuffle != info.shuffle) dbus.mpris.shuffle = info.shuffle; - if (dbus.mpris.repeat != info.repeat) dbus.mpris.repeat = info.repeat; + dbus.mpris.metadata = { + 'mpris:trackid': dbus.mpris.objectPath('track/' + info.track.trackNumber), + 'mpris:length': info.track.length, + 'mpris:artUrl': info.track.art, + 'xesam:title': info.track.name.replace(/(\'| - .*| \(.*)/i, ''), //Remove long track titles + 'xesam:album': info.track.album.replace(/(\'| - .*| \(.*)/i, ''), //Remove long album names + 'xesam:artist': info.track.artists, + 'xesam:url': info.track.url + }; + dbus.mpris.volume = info.volume; + dbus.mpris.position = info.track.position; + dbus.mpris.playbackStatus = info.status; + dbus.mpris.shuffle = info.shuffle; + dbus.mpris.repeat = info.repeat; } } class Controller extends EventEmitter { @@ -114,28 +112,24 @@ module.exports = (function() { }); if (dbus) { - console.log('setup!'); - dbus.mediakeys.on('Play', () => { - console.log('RECEIVED!!!'); - this.playPause(); - }); - dbus.mediakeys.on('Stop', this.stop); - dbus.mediakeys.on('Next', this.next); - dbus.mediakeys.on('Previous', this.previous); + dbus.mediakeys.on('Play', () => this.playPause()); + dbus.mediakeys.on('Stop', () => this.stop()); + dbus.mediakeys.on('Next', () => this.next()); + dbus.mediakeys.on('Previous', () => this.previous()); - dbus.mpris.on('Play', this.play); - dbus.mpris.on('PlayPause', this.playPause); - dbus.mpris.on('Next', this.next); - dbus.mpris.on('Previous', this.previous); - dbus.mpris.on('Stop', this.stop); - dbus.mpris.on('OpenUri', (e) => {if(e.uri.indexOf('spotify:track:') > -1){this.playTrack(e.uri)}}); - dbus.mpris.on('Quit', () => { this.emit('Quit'); }); - dbus.mpris.on('Raise', () => { this.emit('Raise'); }); - dbus.mpris.on('Volume', (volume) => {this.setVolume(volume);}); - dbus.mpris.on('Shuffle', (shuffle) => {this.setShuffle(shuffle);}); - dbus.mpris.on('Loop', (loop) => {this.setLoop(loop);}); - dbus.mpris.on('Seek', (mms) => {this.seek(mms.delta/1000);}); - dbus.mpris.on('SetPosition', (track,pos) => {console.log('SetPosition not yet implemented')}); + dbus.mpris.on('play', () => this.play()); + dbus.mpris.on('playpause', () => this.playPause()); + dbus.mpris.on('next', () => this.next()); + dbus.mpris.on('previous', this.previous); + dbus.mpris.on('stop', this.stop); + dbus.mpris.on('openuri', (e) => {if(e.uri.indexOf('spotify:track:') > -1){this.playTrack(e.uri)}}); + dbus.mpris.on('quit', () => { this.emit('Quit'); }); + dbus.mpris.on('raise', () => { this.emit('Raise'); }); + dbus.mpris.on('volume', (volume) => {this.setVolume(volume);}); + dbus.mpris.on('shuffle', (shuffle) => {this.setShuffle(shuffle);}); + dbus.mpris.on('loopStatus', (loop) => {this.setLoop(loop);}); + dbus.mpris.on('seek', (mms) => {this.seek(mms.delta/1000);}); + dbus.mpris.on('position', (track,pos) => {console.log('SetPosition not yet implemented')}); } } pause() {