diff --git a/kodi/devicepluginkodi.cpp b/kodi/devicepluginkodi.cpp index ce0f2f53..8f581aa4 100644 --- a/kodi/devicepluginkodi.cpp +++ b/kodi/devicepluginkodi.cpp @@ -104,9 +104,7 @@ DeviceManager::DeviceSetupStatus DevicePluginKodi::setupDevice(Device *device) connect(kodi, &Kodi::actionExecuted, this, &DevicePluginKodi::onActionExecuted); connect(kodi, &Kodi::versionDataReceived, this, &DevicePluginKodi::versionDataReceived); connect(kodi, &Kodi::updateDataReceived, this, &DevicePluginKodi::onSetupFinished); - connect(kodi, &Kodi::onPlayerPlay, this, &DevicePluginKodi::onPlayerPlay); - connect(kodi, &Kodi::onPlayerPause, this, &DevicePluginKodi::onPlayerPause); - connect(kodi, &Kodi::onPlayerStop, this, &DevicePluginKodi::onPlayerStop); + connect(kodi, &Kodi::playbackStatusChanged, this, &DevicePluginKodi::onPlaybackStatusChanged); m_kodis.insert(kodi, device); m_asyncSetups.append(kodi); @@ -167,25 +165,25 @@ DeviceManager::DeviceError DevicePluginKodi::executeAction(Device *device, const } else if (action.actionTypeId() == kodiAudioLibraryActionTypeId) { kodi->audioLibrary(action.param(kodiAudioCommandParamTypeId).value().toString(), action.id()); return DeviceManager::DeviceErrorAsync; - } else if(action.actionTypeId() == skipBackActionTypeId) { + } else if(action.actionTypeId() == kodiSkipBackActionTypeId) { kodi->pressButton("skipprevious", action.id()); return DeviceManager::DeviceErrorAsync; - } else if(action.actionTypeId() == rewindActionTypeId) { + } else if(action.actionTypeId() == kodiRewindActionTypeId) { kodi->pressButton("rewind", action.id()); return DeviceManager::DeviceErrorAsync; - } else if(action.actionTypeId() == stopActionTypeId) { + } else if(action.actionTypeId() == kodiStopActionTypeId) { kodi->pressButton("stop", action.id()); return DeviceManager::DeviceErrorAsync; - } else if(action.actionTypeId() == playActionTypeId) { + } else if(action.actionTypeId() == kodiPlayActionTypeId) { kodi->pressButton("play", action.id()); return DeviceManager::DeviceErrorAsync; - } else if(action.actionTypeId() == pauseActionTypeId) { + } else if(action.actionTypeId() == kodiPauseActionTypeId) { kodi->pressButton("pause", action.id()); return DeviceManager::DeviceErrorAsync; - } else if(action.actionTypeId() == fastForwardActionTypeId) { + } else if(action.actionTypeId() == kodiFastForwardActionTypeId) { kodi->pressButton("fastforward", action.id()); return DeviceManager::DeviceErrorAsync; - } else if(action.actionTypeId() == skipNextActionTypeId) { + } else if(action.actionTypeId() == kodiSkipNextActionTypeId) { kodi->pressButton("skipnext", action.id()); return DeviceManager::DeviceErrorAsync; } else { @@ -309,24 +307,18 @@ void DevicePluginKodi::onSetupFinished(const QVariantMap &data) kodi->showNotification("Connected", 2000, "info", ActionId()); } -void DevicePluginKodi::onPlayerPlay() +void DevicePluginKodi::onPlaybackStatusChanged(const QString &playbackStatus) { Kodi *kodi = static_cast(sender()); Device *device = m_kodis.value(kodi); - emit emitEvent(Event(kodiOnPlayerPlayEventTypeId, device->id())); -} - -void DevicePluginKodi::onPlayerPause() -{ - Kodi *kodi = static_cast(sender()); - Device *device = m_kodis.value(kodi); - emit emitEvent(Event(kodiOnPlayerPauseEventTypeId, device->id())); -} - -void DevicePluginKodi::onPlayerStop() -{ - Kodi *kodi = static_cast(sender()); - Device *device = m_kodis.value(kodi); - emit emitEvent(Event(kodiOnPlayerStopEventTypeId, device->id())); + device->setStateValue(kodiPlaybackStatusStateTypeId, playbackStatus); + // legacy events + if (playbackStatus == "PLAYING") { + emit emitEvent(Event(kodiOnPlayerPlayEventTypeId, device->id())); + } else if (playbackStatus == "PAUSED") { + emit emitEvent(Event(kodiOnPlayerPauseEventTypeId, device->id())); + } else { + emit emitEvent(Event(kodiOnPlayerStopEventTypeId, device->id())); + } } diff --git a/kodi/devicepluginkodi.h b/kodi/devicepluginkodi.h index 5e592a48..fe5b4c55 100644 --- a/kodi/devicepluginkodi.h +++ b/kodi/devicepluginkodi.h @@ -61,9 +61,7 @@ private slots: void versionDataReceived(const QVariantMap &data); void onSetupFinished(const QVariantMap &data); - void onPlayerPlay(); - void onPlayerPause(); - void onPlayerStop(); + void onPlaybackStatusChanged(const QString &playbackStatus); }; #endif // DEVICEPLUGINKODI_H diff --git a/kodi/kodi.cpp b/kodi/kodi.cpp index 7a1fc0d8..4bb23266 100644 --- a/kodi/kodi.cpp +++ b/kodi/kodi.cpp @@ -21,6 +21,8 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "kodi.h" +#include +#include "extern-plugininfo.h" Kodi::Kodi(const QHostAddress &hostAddress, const int &port, QObject *parent) : QObject(parent), @@ -36,9 +38,9 @@ Kodi::Kodi(const QHostAddress &hostAddress, const int &port, QObject *parent) : connect(m_jsonHandler, &KodiJsonHandler::versionDataReceived, this, &Kodi::versionDataReceived); connect(m_jsonHandler, &KodiJsonHandler::updateDataReceived, this, &Kodi::updateDataReceived); connect(m_jsonHandler, &KodiJsonHandler::updateDataReceived, this, &Kodi::onUpdateFinished); - connect(m_jsonHandler, &KodiJsonHandler::onPlayerPlay, this, &Kodi::onPlayerPlay); - connect(m_jsonHandler, &KodiJsonHandler::onPlayerPause, this, &Kodi::onPlayerPause); - connect(m_jsonHandler, &KodiJsonHandler::onPlayerStop, this, &Kodi::onPlayerStop); + connect(m_jsonHandler, &KodiJsonHandler::playbackStatusChanged, this, &Kodi::playbackStatusChanged); + connect(m_jsonHandler, &KodiJsonHandler::activePlayersChanged, this, &Kodi::activePlayersChanged); + connect(m_jsonHandler, &KodiJsonHandler::playerPropertiesReveived, this, &Kodi::playerPropertiesReceived); } QHostAddress Kodi::hostAddress() const @@ -157,6 +159,9 @@ void Kodi::update() params.insert("properties", properties); m_jsonHandler->sendData("Application.GetProperties", params, ActionId()); + + params.clear(); + m_jsonHandler->sendData("Player.GetActivePlayers", params, ActionId()); } void Kodi::checkVersion() @@ -185,6 +190,7 @@ void Kodi::onVolumeChanged(const int &volume, const bool &muted) void Kodi::onUpdateFinished(const QVariantMap &data) { + qCDebug(dcKodi()) << "update finished:" << data; if (data.contains("volume")) { m_volume = data.value("volume").toInt(); } @@ -193,3 +199,32 @@ void Kodi::onUpdateFinished(const QVariantMap &data) } emit stateChanged(); } + +void Kodi::activePlayersChanged(const QVariantList &data) +{ + qCDebug(dcKodi()) << "active players changed" << data.count(); + m_activePlayerCount = data.count(); + if (m_activePlayerCount == 0) { + emit playbackStatusChanged("STOPPED"); + return; + } + int activePlayer = data.first().toMap().value("playerid").toInt(); + QVariantMap params; + params.insert("playerid", activePlayer); + QVariantList properties; + properties.append("speed"); + params.insert("properties", properties); + m_jsonHandler->sendData("Player.GetProperties", params, ActionId()); +} + +void Kodi::playerPropertiesReceived(const QVariantMap &properties) +{ + qCDebug(dcKodi()) << "player props received" << properties; + if (m_activePlayerCount > 0) { + if (properties.value("speed").toDouble() > 0) { + emit playbackStatusChanged("PLAYING"); + } else { + emit playbackStatusChanged("PAUSED"); + } + } +} diff --git a/kodi/kodi.h b/kodi/kodi.h index b555d153..9259dcdd 100644 --- a/kodi/kodi.h +++ b/kodi/kodi.h @@ -66,6 +66,7 @@ private: KodiJsonHandler *m_jsonHandler; bool m_muted; int m_volume; + int m_activePlayerCount = 0; // if it's > 0, there is something playing (either music or video or slideshow) signals: void connectionStatusChanged(); @@ -73,16 +74,13 @@ signals: void actionExecuted(const ActionId &actionId, const bool &success); void updateDataReceived(const QVariantMap &data); void versionDataReceived(const QVariantMap &data); - - void onPlayerPlay(); - void onPlayerPause(); - void onPlayerStop(); + void playbackStatusChanged(const QString &playbackState); private slots: void onVolumeChanged(const int &volume, const bool &muted); void onUpdateFinished(const QVariantMap &data); - - + void activePlayersChanged(const QVariantList &data); + void playerPropertiesReceived(const QVariantMap &properties); }; #endif // KODI_H diff --git a/kodi/kodijsonhandler.cpp b/kodi/kodijsonhandler.cpp index 740853d8..a498f084 100644 --- a/kodi/kodijsonhandler.cpp +++ b/kodi/kodijsonhandler.cpp @@ -51,17 +51,17 @@ void KodiJsonHandler::sendData(const QString &method, const QVariantMap ¶ms, void KodiJsonHandler::processNotification(const QString &method, const QVariantMap ¶ms) { - //qCDebug(dcKodi) << "got notification" << method; + qCDebug(dcKodi) << "got notification" << method; if (method == "Application.OnVolumeChanged") { QVariantMap data = params.value("data").toMap(); emit volumeChanged(data.value("volume").toInt(), data.value("muted").toBool()); } else if (method == "Player.OnPlay") { - emit onPlayerPlay(); + emit playbackStatusChanged("PLAYING"); } else if (method == "Player.OnPause") { - emit onPlayerPause(); + emit playbackStatusChanged("PAUSED"); } else if (method == "Player.OnStop") { - emit onPlayerStop(); + emit playbackStatusChanged("STOPPED"); } } @@ -81,17 +81,34 @@ void KodiJsonHandler::processRequestResponse(const KodiReply &reply, const QVari if (response.contains("error")) { //qCDebug(dcKodi) << QJsonDocument::fromVariant(response).toJson(); qCWarning(dcKodi) << "got error response for request " << reply.method() << ":" << response.value("error").toMap().value("message").toString(); + return; } if (reply.method() == "Application.GetProperties") { //qCDebug(dcKodi) << "got update response" << reply.method(); emit updateDataReceived(response.value("result").toMap()); + return; } if (reply.method() == "JSONRPC.Version") { qCDebug(dcKodi) << "got version response" << reply.method(); emit versionDataReceived(response.value("result").toMap()); + return; } + + if (reply.method() == "Player.GetActivePlayers") { + qCDebug(dcKodi) << "Active players changed" << response; + emit activePlayersChanged(response.value("result").toList()); + return; + } + + if (reply.method() == "Player.GetProperties") { + qCDebug(dcKodi) << "Player properties received" << response; + emit playerPropertiesReveived(response.value("result").toMap()); + return; + } + + qCDebug(dcKodi()) << "unhandled reply" << reply.method() << response; } void KodiJsonHandler::processResponse(const QByteArray &data) diff --git a/kodi/kodijsonhandler.h b/kodi/kodijsonhandler.h index 942f29f0..7897f36f 100644 --- a/kodi/kodijsonhandler.h +++ b/kodi/kodijsonhandler.h @@ -53,10 +53,10 @@ signals: void actionExecuted(const ActionId &actionId, const bool &success); void updateDataReceived(const QVariantMap &data); void versionDataReceived(const QVariantMap &data); + void activePlayersChanged(const QVariantList &data); + void playerPropertiesReveived(const QVariantMap &properties); - void onPlayerPlay(); - void onPlayerPause(); - void onPlayerStop(); + void playbackStatusChanged(const QString &playbackStatus); private slots: void processResponse(const QByteArray &data);