-
Notifications
You must be signed in to change notification settings - Fork 39
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
Showing
2 changed files
with
225 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
const { Gio, GLib } = imports.gi; | ||
|
||
// Toggle pause | ||
// dbus-send --print-reply --dest=${currentPlayer} /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause | ||
|
||
// Next | ||
// dbus-send --print-reply --dest=${currentPlayer} /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next | ||
|
||
// Previous | ||
// dbus-send --print-reply --dest=${currentPlayer} /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous | ||
|
||
// List names | ||
// dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.ListNames | ||
|
||
// Player state | ||
// dbus-send --print-reply --dest=${player} /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'PlaybackStatus' | ||
|
||
// Metadata | ||
// dbus-send --print-reply --dest=${player} /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' | ||
|
||
let connection = Gio.DBus.session; | ||
|
||
const playbackAction = (action, player) => { | ||
return new Promise((resolve, reject) => { | ||
connection.call( | ||
player, | ||
"/org/mpris/MediaPlayer2", | ||
"org.mpris.MediaPlayer2.Player", | ||
action, | ||
null, | ||
null, | ||
Gio.DBusCallFlags.NONE, | ||
-1, | ||
null, | ||
(connection, res) => { | ||
try { | ||
connection.call_finish(res); | ||
resolve(); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
); | ||
}); | ||
}; | ||
|
||
const getPlayers = () => { | ||
return new Promise((resolve, reject) => { | ||
connection.call( | ||
"org.freedesktop.DBus", | ||
"/org/freedesktop/DBus", | ||
"org.freedesktop.DBus", | ||
"ListNames", | ||
null, | ||
null, | ||
Gio.DBusCallFlags.NONE, | ||
-1, | ||
null, | ||
(connection, res) => { | ||
try { | ||
let rawReply = connection | ||
.call_finish(res) | ||
.get_child_value(0); | ||
let services = rawReply.deepUnpack(); | ||
players = services.filter((service) => { | ||
if (service.includes("org.mpris.MediaPlayer2")) { | ||
return true; | ||
} | ||
}); | ||
resolve(players); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
); | ||
}); | ||
}; | ||
|
||
const getMetadata = (player) => { | ||
log("Getting metadata"); | ||
return new Promise((resolve, reject) => { | ||
connection.call( | ||
player, | ||
"/org/mpris/MediaPlayer2", | ||
"org.freedesktop.DBus.Properties", | ||
"Get", | ||
new GLib.Variant("(ss)", [ | ||
"org.mpris.MediaPlayer2.Player", | ||
"Metadata", | ||
]), | ||
null, | ||
Gio.DBusCallFlags.NONE, | ||
-1, | ||
null, | ||
(connection, res) => { | ||
try { | ||
let metaData = connection | ||
.call_finish(res) | ||
.recursiveUnpack()[0]; | ||
let title = metaData["xesam:title"]; | ||
let artist = metaData["xesam:artist"]; | ||
log("Got metadata"); | ||
resolve([title, artist]); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
); | ||
}); | ||
}; | ||
|
||
const getPlaybackStatus = (player) => { | ||
log("Getting playback status"); | ||
return new Promise((resolve, reject) => { | ||
connection.call( | ||
player, | ||
"/org/mpris/MediaPlayer2", | ||
"org.freedesktop.DBus.Properties", | ||
"Get", | ||
new GLib.Variant("(ss)", [ | ||
"org.mpris.MediaPlayer2.Player", | ||
"PlaybackStatus", | ||
]), | ||
null, | ||
Gio.DBusCallFlags.NONE, | ||
-1, | ||
null, | ||
(connection, res) => { | ||
try { | ||
let playbackStatus = connection | ||
.call_finish(res) | ||
.recursiveUnpack()[0]; | ||
log("Getting metadata"); | ||
resolve(playbackStatus); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
); | ||
}); | ||
}; |
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