2013-07-29 17:43:13 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
|
2013-07-29 17:43:13 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2013-07-29 17:43:13 +01:00
|
|
|
*/
|
|
|
|
|
2013-08-13 05:35:58 +01:00
|
|
|
#include "mpriscontrolplugin.h"
|
2013-07-29 17:43:13 +01:00
|
|
|
|
2013-09-03 01:14:27 +01:00
|
|
|
#include <QDBusArgument>
|
2013-07-29 17:43:13 +01:00
|
|
|
#include <QDBusMessage>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QDBusReply>
|
2015-05-05 00:40:04 +01:00
|
|
|
#include <QDBusServiceWatcher>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <qdbusconnectioninterface.h>
|
2013-07-29 17:43:13 +01:00
|
|
|
|
2014-09-22 01:37:10 +01:00
|
|
|
#include <KPluginFactory>
|
|
|
|
|
2014-06-14 15:34:00 +01:00
|
|
|
#include <core/device.h>
|
2019-06-09 16:28:49 +01:00
|
|
|
#include <dbushelper.h>
|
2020-01-20 16:36:44 +00:00
|
|
|
|
2023-07-24 13:18:47 +01:00
|
|
|
#include "generated/systeminterfaces/dbusproperties.h"
|
|
|
|
#include "generated/systeminterfaces/mprisplayer.h"
|
|
|
|
#include "generated/systeminterfaces/mprisroot.h"
|
2020-05-26 17:55:47 +01:00
|
|
|
#include "plugin_mpris_debug.h"
|
2013-11-06 21:16:55 +00:00
|
|
|
|
2019-06-12 21:16:54 +01:00
|
|
|
K_PLUGIN_CLASS_WITH_JSON(MprisControlPlugin, "kdeconnect_mpriscontrol.json")
|
2013-08-13 05:35:58 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
MprisPlayer::MprisPlayer(const QString &serviceName, const QString &dbusObjectPath, const QDBusConnection &busConnection)
|
2018-03-16 12:44:16 +00:00
|
|
|
: m_serviceName(serviceName)
|
|
|
|
, m_propertiesInterface(new OrgFreedesktopDBusPropertiesInterface(serviceName, dbusObjectPath, busConnection))
|
|
|
|
, m_mediaPlayer2PlayerInterface(new OrgMprisMediaPlayer2PlayerInterface(serviceName, dbusObjectPath, busConnection))
|
|
|
|
{
|
|
|
|
m_mediaPlayer2PlayerInterface->setTimeout(500);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
MprisControlPlugin::MprisControlPlugin(QObject *parent, const QVariantList &args)
|
2013-08-13 05:35:58 +01:00
|
|
|
: KdeConnectPlugin(parent, args)
|
2015-05-05 00:40:04 +01:00
|
|
|
, prevVolume(-1)
|
2013-07-29 17:43:13 +01:00
|
|
|
{
|
2022-04-12 06:40:03 +01:00
|
|
|
m_watcher = new QDBusServiceWatcher(QString(), QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, this);
|
2013-07-30 19:21:06 +01:00
|
|
|
|
2016-04-08 00:12:10 +01:00
|
|
|
// TODO: QDBusConnectionInterface::serviceOwnerChanged is deprecated, maybe query org.freedesktop.DBus directly?
|
2022-04-12 06:40:03 +01:00
|
|
|
connect(QDBusConnection::sessionBus().interface(), &QDBusConnectionInterface::serviceOwnerChanged, this, &MprisControlPlugin::serviceOwnerChanged);
|
2013-07-30 19:21:06 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Add existing interfaces
|
2022-04-12 06:40:03 +01:00
|
|
|
const QStringList services = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
|
2022-09-10 22:23:52 +01:00
|
|
|
for (const QString &service : services) {
|
2016-04-08 00:12:10 +01:00
|
|
|
// The string doesn't matter, it just needs to be empty/non-empty
|
2016-11-26 14:38:08 +00:00
|
|
|
serviceOwnerChanged(service, QLatin1String(""), QStringLiteral("1"));
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
2015-09-11 10:27:18 +01:00
|
|
|
}
|
|
|
|
|
2016-04-08 00:12:10 +01:00
|
|
|
// Copied from the mpris2 dataengine in the plasma-workspace repository
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisControlPlugin::serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner)
|
2015-09-11 10:27:18 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!serviceName.startsWith(QStringLiteral("org.mpris.MediaPlayer2.")))
|
|
|
|
return;
|
|
|
|
if (serviceName.startsWith(QStringLiteral("org.mpris.MediaPlayer2.kdeconnect.")))
|
|
|
|
return;
|
mpriscontrol plugin should ignore playerctld
playerctld (https://github.com/altdesktop/playerctl/issues/161) is a proxy daemon for the currently active player by playerctl, which facilitates managing mpris players, forwarding requests to the currently active/last active player, and sorting out troubles with selecting the correct player manually.
Unfortunately, it also creates an annoying issue with kdeconnect: when playing media on the phone, kdeconnect publishes the state to the computer through the mprisremote plugin - then, playerctld picks it up as active player, and registers its own mpris media player. As a result, the mpriscontrol plugin sees this as a running media player, and in turn, publishes the state back to the phone, essentially creating another media session on the phone, resulting in two notifications. As playerctld is _always_ only a proxy to another media player (or kdeconnect), it can safely be ignored, just like kdeconnect itself already is. This commit adds an if check doing exactly that.
2020-05-20 22:12:33 +01:00
|
|
|
// playerctld is a only a proxy to other media players, and can thus safely be ignored
|
2022-09-10 22:23:52 +01:00
|
|
|
if (serviceName == QStringLiteral("org.mpris.MediaPlayer2.playerctld"))
|
|
|
|
return;
|
2016-04-08 00:12:10 +01:00
|
|
|
|
|
|
|
if (!oldOwner.isEmpty()) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_MPRIS) << "MPRIS service" << serviceName << "just went offline";
|
|
|
|
removePlayer(serviceName);
|
2015-09-11 10:27:18 +01:00
|
|
|
}
|
2013-07-30 19:21:06 +01:00
|
|
|
|
2016-04-08 00:12:10 +01:00
|
|
|
if (!newOwner.isEmpty()) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_MPRIS) << "MPRIS service" << serviceName << "just came online";
|
|
|
|
addPlayer(serviceName);
|
2015-09-11 10:27:18 +01:00
|
|
|
}
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisControlPlugin::addPlayer(const QString &service)
|
2013-07-30 19:21:06 +01:00
|
|
|
{
|
2018-03-16 12:44:16 +00:00
|
|
|
const QString mediaPlayerObjectPath = QStringLiteral("/org/mpris/MediaPlayer2");
|
|
|
|
|
2022-04-12 06:40:03 +01:00
|
|
|
OrgMprisMediaPlayer2Interface iface(service, mediaPlayerObjectPath, QDBusConnection::sessionBus());
|
2020-01-20 16:36:44 +00:00
|
|
|
QString identity = iface.identity();
|
|
|
|
|
2015-11-11 19:00:59 +00:00
|
|
|
if (identity.isEmpty()) {
|
|
|
|
identity = service.mid(sizeof("org.mpris.MediaPlayer2"));
|
|
|
|
}
|
2017-08-02 14:42:01 +01:00
|
|
|
|
|
|
|
QString uniqueName = identity;
|
2018-03-17 13:37:11 +00:00
|
|
|
for (int i = 2; playerList.contains(uniqueName); ++i) {
|
|
|
|
uniqueName = identity + QLatin1String(" [") + QString::number(i) + QLatin1Char(']');
|
2017-08-02 14:42:01 +01:00
|
|
|
}
|
|
|
|
|
2022-04-12 06:40:03 +01:00
|
|
|
MprisPlayer player(service, mediaPlayerObjectPath, QDBusConnection::sessionBus());
|
2018-03-16 12:44:16 +00:00
|
|
|
|
|
|
|
playerList.insert(uniqueName, player);
|
2013-08-10 04:21:55 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(player.propertiesInterface(), &OrgFreedesktopDBusPropertiesInterface::PropertiesChanged, this, &MprisControlPlugin::propertiesChanged);
|
|
|
|
connect(player.mediaPlayer2PlayerInterface(), &OrgMprisMediaPlayer2PlayerInterface::Seeked, this, &MprisControlPlugin::seeked);
|
2013-08-10 04:21:55 +01:00
|
|
|
|
2018-03-16 12:44:16 +00:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_MPRIS) << "Mpris addPlayer" << service << "->" << uniqueName;
|
|
|
|
sendPlayerList();
|
2014-12-13 22:41:53 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisControlPlugin::seeked(qlonglong position)
|
|
|
|
{
|
|
|
|
// qCDebug(KDECONNECT_PLUGIN_MPRIS) << "Seeked in player";
|
|
|
|
OrgMprisMediaPlayer2PlayerInterface *mediaPlayer2PlayerInterface = (OrgMprisMediaPlayer2PlayerInterface *)sender();
|
2018-03-16 12:44:16 +00:00
|
|
|
const auto end = playerList.constEnd();
|
2022-09-10 22:23:52 +01:00
|
|
|
const auto it = std::find_if(playerList.constBegin(), end, [mediaPlayer2PlayerInterface](const MprisPlayer &player) {
|
2018-03-16 12:44:16 +00:00
|
|
|
return (player.mediaPlayer2PlayerInterface() == mediaPlayer2PlayerInterface);
|
|
|
|
});
|
|
|
|
if (it == end) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_MPRIS) << "Seeked signal received for no longer tracked service" << mediaPlayer2PlayerInterface->service();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
const QString &playerName = it.key();
|
2016-06-21 19:07:12 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS,
|
|
|
|
{{QStringLiteral("pos"), position / 1000}, // Send milis instead of nanos
|
|
|
|
{QStringLiteral("player"), playerName}});
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
|
|
|
|
2023-07-28 08:11:43 +01:00
|
|
|
void MprisControlPlugin::propertiesChanged(const QString & /*propertyInterface*/, const QVariantMap &properties)
|
2013-07-30 19:21:06 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
OrgFreedesktopDBusPropertiesInterface *propertiesInterface = (OrgFreedesktopDBusPropertiesInterface *)sender();
|
2018-03-16 12:44:16 +00:00
|
|
|
const auto end = playerList.constEnd();
|
2022-09-10 22:23:52 +01:00
|
|
|
const auto it = std::find_if(playerList.constBegin(), end, [propertiesInterface](const MprisPlayer &player) {
|
2018-03-16 12:44:16 +00:00
|
|
|
return (player.propertiesInterface() == propertiesInterface);
|
|
|
|
});
|
|
|
|
if (it == end) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_MPRIS) << "PropertiesChanged signal received for no longer tracked service" << propertiesInterface->service();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
OrgMprisMediaPlayer2PlayerInterface *const mediaPlayer2PlayerInterface = it.value().mediaPlayer2PlayerInterface();
|
|
|
|
const QString &playerName = it.key();
|
2018-03-16 12:44:16 +00:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS);
|
2013-08-10 04:21:55 +01:00
|
|
|
bool somethingToSend = false;
|
2016-11-26 14:38:08 +00:00
|
|
|
if (properties.contains(QStringLiteral("Volume"))) {
|
2022-09-10 22:23:52 +01:00
|
|
|
int volume = (int)(properties[QStringLiteral("Volume")].toDouble() * 100);
|
2013-08-10 04:21:55 +01:00
|
|
|
if (volume != prevVolume) {
|
2022-09-10 22:23:52 +01:00
|
|
|
np.set(QStringLiteral("volume"), volume);
|
2013-08-10 04:21:55 +01:00
|
|
|
prevVolume = volume;
|
|
|
|
somethingToSend = true;
|
|
|
|
}
|
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (properties.contains(QStringLiteral("Metadata"))) {
|
2023-03-26 15:13:55 +01:00
|
|
|
QDBusArgument aux = qvariant_cast<QDBusArgument>(properties[QStringLiteral("Metadata")]);
|
2013-08-10 04:21:55 +01:00
|
|
|
QVariantMap nowPlayingMap;
|
2023-03-26 15:13:55 +01:00
|
|
|
aux >> nowPlayingMap;
|
2013-08-10 04:21:55 +01:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
mprisPlayerMetadataToNetworkPacket(np, nowPlayingMap);
|
2017-11-22 18:13:15 +00:00
|
|
|
somethingToSend = true;
|
2013-08-10 04:21:55 +01:00
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (properties.contains(QStringLiteral("PlaybackStatus"))) {
|
|
|
|
bool playing = (properties[QStringLiteral("PlaybackStatus")].toString() == QLatin1String("Playing"));
|
|
|
|
np.set(QStringLiteral("isPlaying"), playing);
|
2013-08-10 04:21:55 +01:00
|
|
|
somethingToSend = true;
|
|
|
|
}
|
2021-06-11 00:41:40 +01:00
|
|
|
if (properties.contains(QStringLiteral("LoopStatus"))) {
|
|
|
|
np.set(QStringLiteral("loopStatus"), properties[QStringLiteral("LoopStatus")]);
|
|
|
|
somethingToSend = true;
|
|
|
|
}
|
|
|
|
if (properties.contains(QStringLiteral("Shuffle"))) {
|
|
|
|
np.set(QStringLiteral("shuffle"), properties[QStringLiteral("Shuffle")].toBool());
|
|
|
|
somethingToSend = true;
|
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (properties.contains(QStringLiteral("CanPause"))) {
|
|
|
|
np.set(QStringLiteral("canPause"), properties[QStringLiteral("CanPause")].toBool());
|
2016-08-26 10:10:36 +01:00
|
|
|
somethingToSend = true;
|
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (properties.contains(QStringLiteral("CanPlay"))) {
|
|
|
|
np.set(QStringLiteral("canPlay"), properties[QStringLiteral("CanPlay")].toBool());
|
2016-08-26 10:10:36 +01:00
|
|
|
somethingToSend = true;
|
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (properties.contains(QStringLiteral("CanGoNext"))) {
|
|
|
|
np.set(QStringLiteral("canGoNext"), properties[QStringLiteral("CanGoNext")].toBool());
|
2016-08-26 10:10:36 +01:00
|
|
|
somethingToSend = true;
|
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (properties.contains(QStringLiteral("CanGoPrevious"))) {
|
|
|
|
np.set(QStringLiteral("canGoPrevious"), properties[QStringLiteral("CanGoPrevious")].toBool());
|
2016-08-26 10:10:36 +01:00
|
|
|
somethingToSend = true;
|
|
|
|
}
|
2013-08-10 04:21:55 +01:00
|
|
|
|
|
|
|
if (somethingToSend) {
|
2018-03-16 12:44:16 +00:00
|
|
|
np.set(QStringLiteral("player"), playerName);
|
2023-06-07 19:20:21 +01:00
|
|
|
// Always also update the position if can seek
|
|
|
|
bool canSeek = mediaPlayer2PlayerInterface->canSeek();
|
|
|
|
np.set(QStringLiteral("canSeek"), canSeek);
|
|
|
|
if (canSeek) {
|
2018-03-16 12:44:16 +00:00
|
|
|
long long pos = mediaPlayer2PlayerInterface->position();
|
2022-09-10 22:23:52 +01:00
|
|
|
np.set(QStringLiteral("pos"), pos / 1000); // Send milis instead of nanos
|
2014-12-14 02:44:20 +00:00
|
|
|
}
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2013-08-10 04:21:55 +01:00
|
|
|
}
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisControlPlugin::removePlayer(const QString &serviceName)
|
2013-07-30 19:21:06 +01:00
|
|
|
{
|
2018-03-16 12:44:16 +00:00
|
|
|
const auto end = playerList.end();
|
2022-09-10 22:23:52 +01:00
|
|
|
const auto it = std::find_if(playerList.begin(), end, [serviceName](const MprisPlayer &player) {
|
2018-03-16 12:44:16 +00:00
|
|
|
return (player.serviceName() == serviceName);
|
|
|
|
});
|
|
|
|
if (it == end) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_MPRIS) << "Could not find player for serviceName" << serviceName;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
const QString &playerName = it.key();
|
2018-03-16 12:44:16 +00:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_MPRIS) << "Mpris removePlayer" << serviceName << "->" << playerName;
|
|
|
|
|
|
|
|
playerList.erase(it);
|
|
|
|
|
2013-08-10 04:21:55 +01:00
|
|
|
sendPlayerList();
|
2013-07-29 17:43:13 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool MprisControlPlugin::sendAlbumArt(const NetworkPacket &np)
|
2018-03-04 12:47:29 +00:00
|
|
|
{
|
|
|
|
const QString player = np.get<QString>(QStringLiteral("player"));
|
|
|
|
auto it = playerList.find(player);
|
|
|
|
bool valid_player = (it != playerList.end());
|
|
|
|
if (!valid_player) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Get mpris information
|
|
|
|
auto &mprisInterface = *it.value().mediaPlayer2PlayerInterface();
|
2018-03-04 12:47:29 +00:00
|
|
|
QVariantMap nowPlayingMap = mprisInterface.metadata();
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Check if the supplied album art url indeed belongs to this mpris player
|
2018-03-04 12:47:29 +00:00
|
|
|
QUrl playerAlbumArtUrl{nowPlayingMap[QStringLiteral("mpris:artUrl")].toString()};
|
|
|
|
QString requestedAlbumArtUrl = np.get<QString>(QStringLiteral("albumArtUrl"));
|
|
|
|
if (!playerAlbumArtUrl.isValid() || playerAlbumArtUrl != QUrl(requestedAlbumArtUrl)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Only support sending local files
|
2019-06-10 15:40:28 +01:00
|
|
|
if (playerAlbumArtUrl.scheme() != QStringLiteral("file")) {
|
2018-03-04 12:47:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Open the file to send
|
2018-03-04 12:47:29 +00:00
|
|
|
QSharedPointer<QFile> art{new QFile(playerAlbumArtUrl.toLocalFile())};
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Send the album art as payload
|
2018-03-04 12:47:29 +00:00
|
|
|
NetworkPacket answer(PACKET_TYPE_MPRIS);
|
|
|
|
answer.set(QStringLiteral("transferringAlbumArt"), true);
|
|
|
|
answer.set(QStringLiteral("player"), player);
|
|
|
|
answer.set(QStringLiteral("albumArtUrl"), requestedAlbumArtUrl);
|
|
|
|
answer.setPayload(art, art->size());
|
|
|
|
sendPacket(answer);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-31 08:25:45 +01:00
|
|
|
void MprisControlPlugin::receivePacket(const NetworkPacket &np)
|
2013-07-29 17:43:13 +01:00
|
|
|
{
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.has(QStringLiteral("playerList"))) {
|
2023-07-31 08:25:45 +01:00
|
|
|
return; // Whoever sent this is an mpris client and not an mpris control!
|
2013-09-03 01:06:56 +01:00
|
|
|
}
|
2013-07-29 17:43:13 +01:00
|
|
|
|
2018-03-04 12:47:29 +00:00
|
|
|
if (np.has(QStringLiteral("albumArtUrl"))) {
|
2023-07-31 08:25:45 +01:00
|
|
|
sendAlbumArt(np);
|
|
|
|
return;
|
2018-03-04 12:47:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Send the player list
|
2016-11-26 14:38:08 +00:00
|
|
|
const QString player = np.get<QString>(QStringLiteral("player"));
|
2018-03-16 12:44:16 +00:00
|
|
|
auto it = playerList.find(player);
|
|
|
|
bool valid_player = (it != playerList.end());
|
2016-11-26 14:38:08 +00:00
|
|
|
if (!valid_player || np.get<bool>(QStringLiteral("requestPlayerList"))) {
|
2013-08-13 05:35:58 +01:00
|
|
|
sendPlayerList();
|
2013-08-10 04:21:55 +01:00
|
|
|
if (!valid_player) {
|
2023-07-31 08:25:45 +01:00
|
|
|
return;
|
2013-08-10 04:21:55 +01:00
|
|
|
}
|
|
|
|
}
|
2013-07-29 17:43:13 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Do something to the mpris interface
|
|
|
|
const QString &serviceName = it.value().serviceName();
|
2018-03-16 12:44:16 +00:00
|
|
|
// turn from pointer to reference to keep the patch diff small,
|
|
|
|
// actual patch would change all "mprisInterface." into "mprisInterface->"
|
2022-09-10 22:23:52 +01:00
|
|
|
auto &mprisInterface = *it.value().mediaPlayer2PlayerInterface();
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.has(QStringLiteral("action"))) {
|
2022-09-10 22:23:52 +01:00
|
|
|
const QString &action = np.get<QString>(QStringLiteral("action"));
|
|
|
|
// qCDebug(KDECONNECT_PLUGIN_MPRIS) << "Calling action" << action << "in" << serviceName;
|
|
|
|
// TODO: Check for valid actions, currently we trust anything the other end sends us
|
2013-08-10 04:21:55 +01:00
|
|
|
mprisInterface.call(action);
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
2021-06-11 00:41:40 +01:00
|
|
|
if (np.has(QStringLiteral("setLoopStatus"))) {
|
2022-09-10 22:23:52 +01:00
|
|
|
const QString &loopStatus = np.get<QString>(QStringLiteral("setLoopStatus"));
|
2021-06-11 00:41:40 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_MPRIS) << "Setting loopStatus" << loopStatus << "to" << serviceName;
|
|
|
|
mprisInterface.setLoopStatus(loopStatus);
|
|
|
|
}
|
|
|
|
if (np.has(QStringLiteral("setShuffle"))) {
|
|
|
|
bool shuffle = np.get<bool>(QStringLiteral("setShuffle"));
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_MPRIS) << "Setting shuffle" << shuffle << "to" << serviceName;
|
|
|
|
mprisInterface.setShuffle(shuffle);
|
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.has(QStringLiteral("setVolume"))) {
|
2022-09-10 22:23:52 +01:00
|
|
|
double volume = np.get<int>(QStringLiteral("setVolume")) / 100.f;
|
2018-03-16 12:44:16 +00:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_MPRIS) << "Setting volume" << volume << "to" << serviceName;
|
2013-08-10 04:21:55 +01:00
|
|
|
mprisInterface.setVolume(volume);
|
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.has(QStringLiteral("Seek"))) {
|
|
|
|
int offset = np.get<int>(QStringLiteral("Seek"));
|
2022-09-10 22:23:52 +01:00
|
|
|
// qCDebug(KDECONNECT_PLUGIN_MPRIS) << "Seeking" << offset << "to" << serviceName;
|
2013-10-29 19:17:42 +00:00
|
|
|
mprisInterface.Seek(offset);
|
|
|
|
}
|
2013-08-10 04:21:55 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (np.has(QStringLiteral("SetPosition"))) {
|
|
|
|
qlonglong position = np.get<qlonglong>(QStringLiteral("SetPosition"), 0) * 1000;
|
2014-12-14 02:44:20 +00:00
|
|
|
qlonglong seek = position - mprisInterface.position();
|
2022-09-10 22:23:52 +01:00
|
|
|
// qCDebug(KDECONNECT_PLUGIN_MPRIS) << "Setting position by seeking" << seek << "to" << serviceName;
|
2014-12-14 02:44:20 +00:00
|
|
|
mprisInterface.Seek(seek);
|
2014-12-13 22:41:53 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Send something read from the mpris interface
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket answer(PACKET_TYPE_MPRIS);
|
2013-08-10 04:21:55 +01:00
|
|
|
bool somethingToSend = false;
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.get<bool>(QStringLiteral("requestNowPlaying"))) {
|
2013-08-10 04:21:55 +01:00
|
|
|
QVariantMap nowPlayingMap = mprisInterface.metadata();
|
2018-03-04 19:48:51 +00:00
|
|
|
mprisPlayerMetadataToNetworkPacket(answer, nowPlayingMap);
|
2015-11-11 19:03:00 +00:00
|
|
|
|
2014-12-14 02:44:20 +00:00
|
|
|
qlonglong pos = mprisInterface.position();
|
2022-09-10 22:23:52 +01:00
|
|
|
answer.set(QStringLiteral("pos"), pos / 1000);
|
2013-08-10 04:21:55 +01:00
|
|
|
|
|
|
|
bool playing = (mprisInterface.playbackStatus() == QLatin1String("Playing"));
|
2016-11-26 14:38:08 +00:00
|
|
|
answer.set(QStringLiteral("isPlaying"), playing);
|
2013-08-10 04:21:55 +01:00
|
|
|
|
2016-11-26 14:38:08 +00:00
|
|
|
answer.set(QStringLiteral("canPause"), mprisInterface.canPause());
|
|
|
|
answer.set(QStringLiteral("canPlay"), mprisInterface.canPlay());
|
|
|
|
answer.set(QStringLiteral("canGoNext"), mprisInterface.canGoNext());
|
|
|
|
answer.set(QStringLiteral("canGoPrevious"), mprisInterface.canGoPrevious());
|
|
|
|
answer.set(QStringLiteral("canSeek"), mprisInterface.canSeek());
|
2016-08-26 10:10:36 +01:00
|
|
|
|
2021-06-11 00:41:40 +01:00
|
|
|
// LoopStatus is an optional field
|
|
|
|
if (mprisInterface.property("LoopStatus").isValid()) {
|
2022-09-10 22:23:52 +01:00
|
|
|
const QString &loopStatus = mprisInterface.loopStatus();
|
|
|
|
answer.set(QStringLiteral("loopStatus"), loopStatus);
|
2021-06-11 00:41:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shuffle is an optional field
|
|
|
|
if (mprisInterface.property("Shuffle").isValid()) {
|
|
|
|
bool shuffle = mprisInterface.shuffle();
|
2022-09-10 22:23:52 +01:00
|
|
|
answer.set(QStringLiteral("shuffle"), shuffle);
|
2021-06-11 00:41:40 +01:00
|
|
|
}
|
|
|
|
|
2013-08-10 04:21:55 +01:00
|
|
|
somethingToSend = true;
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.get<bool>(QStringLiteral("requestVolume"))) {
|
2013-08-10 04:21:55 +01:00
|
|
|
int volume = (int)(mprisInterface.volume() * 100);
|
2022-09-10 22:23:52 +01:00
|
|
|
answer.set(QStringLiteral("volume"), volume);
|
2013-08-10 04:21:55 +01:00
|
|
|
somethingToSend = true;
|
|
|
|
}
|
2018-03-16 12:44:16 +00:00
|
|
|
|
2013-08-10 04:21:55 +01:00
|
|
|
if (somethingToSend) {
|
2016-11-26 14:38:08 +00:00
|
|
|
answer.set(QStringLiteral("player"), player);
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(answer);
|
2013-07-29 17:43:13 +01:00
|
|
|
}
|
|
|
|
}
|
2013-07-30 19:21:06 +01:00
|
|
|
|
2013-08-13 05:35:58 +01:00
|
|
|
void MprisControlPlugin::sendPlayerList()
|
2013-07-30 19:21:06 +01:00
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS);
|
2022-09-10 22:23:52 +01:00
|
|
|
np.set(QStringLiteral("playerList"), playerList.keys());
|
2018-03-04 12:47:29 +00:00
|
|
|
np.set(QStringLiteral("supportAlbumArtPayload"), true);
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
2014-06-16 19:02:07 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisControlPlugin::mprisPlayerMetadataToNetworkPacket(NetworkPacket &np, const QVariantMap &nowPlayingMap) const
|
|
|
|
{
|
2017-11-22 18:13:15 +00:00
|
|
|
QString title = nowPlayingMap[QStringLiteral("xesam:title")].toString();
|
2023-03-25 14:13:31 +00:00
|
|
|
QString artist = nowPlayingMap[QStringLiteral("xesam:artist")].toStringList().join(QLatin1String(", "));
|
2017-11-22 18:13:15 +00:00
|
|
|
QString album = nowPlayingMap[QStringLiteral("xesam:album")].toString();
|
2017-12-21 12:53:28 +00:00
|
|
|
QString albumArtUrl = nowPlayingMap[QStringLiteral("mpris:artUrl")].toString();
|
2019-12-19 17:07:32 +00:00
|
|
|
QUrl fileUrl = nowPlayingMap[QStringLiteral("xesam:url")].toUrl();
|
|
|
|
|
|
|
|
if (title.isEmpty() && artist.isEmpty() && fileUrl.isLocalFile()) {
|
|
|
|
title = fileUrl.fileName();
|
|
|
|
|
|
|
|
QStringList splitUrl = fileUrl.path().split(QDir::separator());
|
|
|
|
if (album.isEmpty() && splitUrl.size() > 1) {
|
|
|
|
album = splitUrl.at(splitUrl.size() - 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 18:13:15 +00:00
|
|
|
np.set(QStringLiteral("title"), title);
|
|
|
|
np.set(QStringLiteral("artist"), artist);
|
|
|
|
np.set(QStringLiteral("album"), album);
|
2017-12-21 12:53:28 +00:00
|
|
|
np.set(QStringLiteral("albumArtUrl"), albumArtUrl);
|
2017-11-22 18:13:15 +00:00
|
|
|
|
|
|
|
bool hasLength = false;
|
2022-09-10 22:23:52 +01:00
|
|
|
long long length = nowPlayingMap[QStringLiteral("mpris:length")].toLongLong(&hasLength) / 1000; // nanoseconds to milliseconds
|
2017-11-22 18:13:15 +00:00
|
|
|
if (!hasLength) {
|
|
|
|
length = -1;
|
|
|
|
}
|
|
|
|
np.set(QStringLiteral("length"), length);
|
2020-01-18 17:26:16 +00:00
|
|
|
np.set(QStringLiteral("url"), fileUrl);
|
2017-11-22 18:13:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 09:15:11 +01:00
|
|
|
#include "moc_mpriscontrolplugin.cpp"
|
2014-06-16 19:02:07 +01:00
|
|
|
#include "mpriscontrolplugin.moc"
|