-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dff17e1
commit 2d78914
Showing
3 changed files
with
126 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,116 @@ | ||
const { Gio } = imports.gi; | ||
|
||
// thanks esenliyim - https://github.com/esenliyim/sp-tray/blob/master/panelButton.js | ||
// https://wiki.gnome.org/Gjs/Examples/DBusClient | ||
// https://www.andyholmes.ca/articles/dbus-in-gjs.html#high-level-proxies | ||
const path = '/org/mpris/MediaPlayer2'; | ||
const playerInterface = ` | ||
<node> | ||
<interface name="org.mpris.MediaPlayer2.Player"> | ||
<property name="PlaybackStatus" type="s" access="read" /> | ||
<method name="Next" /> | ||
<method name="Previous" /> | ||
<method name="PlayPause" /> | ||
</interface> | ||
</node> | ||
`; | ||
|
||
// Declare the proxy class based on the interface | ||
const PlayerProxy = Gio.DBusProxy.makeProxyWrapper(playerInterface); | ||
|
||
/** | ||
* A Gio.DBuxProxy of the `org.mpris.MediaPlayer2.Player` interface. | ||
* @typedef {Object} MediaPlayerProxy | ||
* @property {string} PlaybackStatus | ||
*/ | ||
|
||
/** | ||
* @callback playerConsumer | ||
* @param {MediaPlayer} player | ||
*/ | ||
|
||
/** | ||
* A non-player specific wrapper of a proxy for a media player. | ||
* @todo Write the documentation. | ||
*/ | ||
class MediaPlayer { | ||
/** | ||
* @param {playerConsumer=} onAppeared | ||
* @param {playerConsumer=} onVanished | ||
* @param {playerConsumer=} onPropertiesChanged | ||
*/ | ||
constructor(onAppeared, onVanished, onPropertiesChanged) { | ||
this.onAppeared = onAppeared; | ||
this.onVanished = onVanished; | ||
this.onPropertiesChanged = onPropertiesChanged; | ||
|
||
this.proxy = null; | ||
this.proxySignals = []; | ||
} | ||
|
||
/** | ||
* @param {string} dest - The well-known name | ||
*/ | ||
setupProxy(dest = 'org.mpris.MediaPlayer2.spotify') { | ||
if (this.proxy) | ||
return; | ||
|
||
try { | ||
// Get the MediaPlayer instance from the bus | ||
this.proxy = new PlayerProxy(Gio.DBus.session, dest, path); | ||
} catch (e) { | ||
logError(e); | ||
return; | ||
} | ||
|
||
this.proxySignals.push(this.proxy.connect( | ||
"g-properties-changed", | ||
(proxy, changed, invalidated) => { | ||
this.onPropertiesChanged?.(this); | ||
} | ||
)); | ||
|
||
// https://www.andyholmes.ca/articles/dbus-in-gjs.html#low-level-proxies | ||
this.proxySignals.push(this.proxy.connect( | ||
"notify::g-name-owner", | ||
(proxy, pspec) => { | ||
if (proxy.g_name_owner === null) { | ||
log(`${proxy.g_name} has vanished`); | ||
this.onVanished?.(this); | ||
} else { | ||
log(`${proxy.g_name} has appeared`); | ||
this.onAppeared?.(this); | ||
} | ||
} | ||
)); | ||
} | ||
|
||
destroy() { | ||
this.proxySignals.forEach(signal => this.proxy.disconnect(signal)); | ||
} | ||
|
||
get isActive() { | ||
return this.proxy.g_name_owner !== null; | ||
} | ||
|
||
get isPlaying() { | ||
return this.isActive && this.proxy.PlaybackStatus === 'Playing'; | ||
} | ||
|
||
next() { | ||
this.proxy.NextSync(); | ||
} | ||
|
||
previous() { | ||
this.proxy.PreviousSync(); | ||
} | ||
|
||
playPause() { | ||
this.proxy.PlayPauseSync(); | ||
} | ||
} | ||
const { Gio } = imports.gi; | ||
|
||
// thanks esenliyim - https://github.com/esenliyim/sp-tray/blob/master/panelButton.js | ||
// https://wiki.gnome.org/Gjs/Examples/DBusClient | ||
// https://www.andyholmes.ca/articles/dbus-in-gjs.html#high-level-proxies | ||
const path = '/org/mpris/MediaPlayer2'; | ||
const playerInterface = ` | ||
<node> | ||
<interface name="org.mpris.MediaPlayer2.Player"> | ||
<property name="PlaybackStatus" type="s" access="read" /> | ||
<method name="Next" /> | ||
<method name="Previous" /> | ||
<method name="PlayPause" /> | ||
</interface> | ||
</node> | ||
`; | ||
|
||
// Declare the proxy class based on the interface | ||
const PlayerProxy = Gio.DBusProxy.makeProxyWrapper(playerInterface); | ||
|
||
/** | ||
* A Gio.DBuxProxy of the `org.mpris.MediaPlayer2.Player` interface. | ||
* @typedef {Object} MediaPlayerProxy | ||
* @property {string} PlaybackStatus | ||
*/ | ||
|
||
/** | ||
* @callback playerConsumer | ||
* @param {MediaPlayer} player | ||
*/ | ||
|
||
/** | ||
* A non-player specific wrapper of a proxy for a media player. | ||
* @todo Write the documentation. | ||
*/ | ||
class MediaPlayer { | ||
// this.onAppeared = onAppeared; | ||
// this.onVanished = onVanished; | ||
// this.onPropertiesChanged = onPropertiesChanged; | ||
|
||
|
||
/** | ||
* @param {playerConsumer=} onAppeared | ||
* @param {playerConsumer=} onVanished | ||
* @param {playerConsumer=} onPropertiesChanged | ||
*/ | ||
constructor(onAppeared, onVanished, onPropertiesChanged) { | ||
this.onAppeared = onAppeared; | ||
this.onVanished = onVanished; | ||
this.onPropertiesChanged = onPropertiesChanged; | ||
|
||
this.proxy = null; | ||
this.proxySignals = []; | ||
} | ||
|
||
/** | ||
* @param {string} dest - The well-known name | ||
*/ | ||
setupProxy(dest = 'org.mpris.MediaPlayer2.spotify') { | ||
if (this.proxy) | ||
return; | ||
|
||
try { | ||
// Get the MediaPlayer instance from the bus | ||
this.proxy = new PlayerProxy(Gio.DBus.session, dest, path); | ||
} catch (e) { | ||
logError(e); | ||
return; | ||
} | ||
|
||
this.proxySignals.push(this.proxy.connect( | ||
"g-properties-changed", | ||
(proxy, changed, invalidated) => { | ||
this.onPropertiesChanged?.(this); | ||
} | ||
)); | ||
|
||
// https://www.andyholmes.ca/articles/dbus-in-gjs.html#low-level-proxies | ||
this.proxySignals.push(this.proxy.connect( | ||
"notify::g-name-owner", | ||
(proxy, pspec) => { | ||
if (proxy.g_name_owner === null) { | ||
log(`${proxy.g_name} has vanished`); | ||
this.onVanished?.(this); | ||
} else { | ||
log(`${proxy.g_name} has appeared`); | ||
this.onAppeared?.(this); | ||
} | ||
} | ||
)); | ||
} | ||
|
||
destroy() { | ||
this.proxySignals.forEach(signal => this.proxy.disconnect(signal)); | ||
} | ||
|
||
get isActive() { | ||
return this.proxy.g_name_owner !== null; | ||
} | ||
|
||
get isPlaying() { | ||
return this.isActive && this.proxy.PlaybackStatus === 'Playing'; | ||
} | ||
|
||
next() { | ||
this.proxy.NextSync(); | ||
} | ||
|
||
previous() { | ||
this.proxy.PreviousSync(); | ||
} | ||
|
||
playPause() { | ||
this.proxy.PlayPauseSync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters