Skip to content

Commit

Permalink
Rewrote extension.js with better logic
Browse files Browse the repository at this point in the history
Isolated only essential dbus related functions in dbus.js
Moved utility functions to utils.js
  • Loading branch information
sakithb committed Sep 3, 2021
1 parent 9f2316b commit 94c9fb1
Show file tree
Hide file tree
Showing 5 changed files with 421 additions and 621 deletions.
3 changes: 2 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ if [ "$1" == "-p" ] || [ "$2" == "-p" ]; then
zip -d Release.zip .gitignore;
zip -d Release.zip build.sh;
VERSION=$(cat metadata.json | grep '\"version\"' | sed 's/[^0-9]*//g');
mv Release.zip "Release_v$VERSION.zip";
mkdir -p "./builds";
mv Release.zip "./builds/Release_v$VERSION.zip";
fi

if [ "$1" == "-r" ] || [ "$2" == "-r" ]; then
Expand Down
139 changes: 12 additions & 127 deletions dbus.js
Original file line number Diff line number Diff line change
@@ -1,140 +1,25 @@
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'
const { Gio } = imports.gi;

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"];
let id = metaData["mpris:trackid"];
// log("Got metadata");
resolve([title, artist, id]);
} catch (e) {
reject(e);
}
}
);
});
};

const getPlaybackStatus = (player) => {
// log("Getting playback status");
var dbusMethod = (destination, object, interface, method, parameters = null) => {
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",
]),
destination,
object,
interface,
method,
parameters,
null,
Gio.DBusCallFlags.NONE,
-1,
null,
(connection, res) => {
(con, res) => {
try {
let playbackStatus = connection
.call_finish(res)
.recursiveUnpack()[0];
// log("Getting metadata");
resolve(playbackStatus);
} catch (e) {
reject(e);
let val = con.call_finish(res).recursiveUnpack();
resolve(val);
} catch (error) {
reject(error);
}
}
);
Expand Down
Loading

0 comments on commit 94c9fb1

Please sign in to comment.