diff options
Diffstat (limited to 'src/modules/mediaplayer/MpMprisInterface.cpp')
| -rw-r--r-- | src/modules/mediaplayer/MpMprisInterface.cpp | 412 |
1 files changed, 209 insertions, 203 deletions
diff --git a/src/modules/mediaplayer/MpMprisInterface.cpp b/src/modules/mediaplayer/MpMprisInterface.cpp index 6c40d47ec..30367487f 100644 --- a/src/modules/mediaplayer/MpMprisInterface.cpp +++ b/src/modules/mediaplayer/MpMprisInterface.cpp @@ -23,7 +23,7 @@ //============================================================================= #include "MpMprisInterface.h" -#if (defined(COMPILE_DBUS_SUPPORT) && !defined(COMPILE_ON_WINDOWS) && !defined(COMPILE_ON_MAC) && !defined(COMPILE_ON_MINGW)) +#if(defined(COMPILE_DBUS_SUPPORT) && !defined(COMPILE_ON_WINDOWS) && !defined(COMPILE_ON_MAC) && !defined(COMPILE_ON_MINGW)) #include "KviLocale.h" /* @@ -42,9 +42,9 @@ struct MPRISPlayerStatus int RepeatCurrent; int RepeatPlaylist; }; -Q_DECLARE_METATYPE( MPRISPlayerStatus ) +Q_DECLARE_METATYPE(MPRISPlayerStatus) -QDBusArgument &operator<<(QDBusArgument &argument, const MPRISPlayerStatus &status) +QDBusArgument & operator<<(QDBusArgument & argument, const MPRISPlayerStatus & status) { argument.beginStructure(); argument << status.Play << status.Random << status.RepeatCurrent << status.RepeatPlaylist; @@ -52,7 +52,7 @@ QDBusArgument &operator<<(QDBusArgument &argument, const MPRISPlayerStatus &stat return argument; } -const QDBusArgument &operator>>(const QDBusArgument &argument, MPRISPlayerStatus &status) +const QDBusArgument & operator>>(const QDBusArgument & argument, MPRISPlayerStatus & status) { argument.beginStructure(); argument >> status.Play >> status.Random >> status.RepeatCurrent >> status.RepeatPlaylist; @@ -61,7 +61,7 @@ const QDBusArgument &operator>>(const QDBusArgument &argument, MPRISPlayerStatus } MpMprisInterface::MpMprisInterface() -: MpInterface() + : MpInterface() { qDBusRegisterMetaType<MPRISPlayerStatus>(); } @@ -73,25 +73,26 @@ MpMprisInterface::~MpMprisInterface() int MpMprisInterface::detect(bool) { QDBusReply<QStringList> reply = QDBusConnection::sessionBus().interface()->registeredServiceNames(); - if (!reply.isValid()) /* something fishy with dbus, it won't work */ + if(!reply.isValid()) /* something fishy with dbus, it won't work */ return 0; - foreach (QString name, reply.value()) - if (name == m_szServiceName) /* player is running */ + foreach(QString name, reply.value()) + if(name == m_szServiceName) /* player is running */ return 100; - return 1; /* dbus works, player may be closed */ + return 1; /* dbus works, player may be closed */ } -#define MPRIS_SIMPLE_CALL(__path, __action) \ - QDBusInterface dbus_iface(m_szServiceName, __path, \ - "org.freedesktop.MediaPlayer", QDBusConnection::sessionBus()); \ - QDBusMessage reply = dbus_iface.call(QDBus::Block, __action); \ - if (reply.type() == QDBusMessage::ErrorMessage) { \ - QDBusError err = reply; \ +#define MPRIS_SIMPLE_CALL(__path, __action) \ + QDBusInterface dbus_iface(m_szServiceName, __path, \ + "org.freedesktop.MediaPlayer", QDBusConnection::sessionBus()); \ + QDBusMessage reply = dbus_iface.call(QDBus::Block, __action); \ + if(reply.type() == QDBusMessage::ErrorMessage) \ + { \ + QDBusError err = reply; \ qDebug("Error: %s\n%s\n", qPrintable(err.name()), qPrintable(err.message())); \ - return false; \ - } \ + return false; \ + } \ return true; bool MpMprisInterface::prev() @@ -124,42 +125,47 @@ bool MpMprisInterface::quit() MPRIS_SIMPLE_CALL("/", "Quit") } -#define MPRIS_CALL_METHOD(__method, __return_if_fail) \ - QDBusInterface dbus_iface(m_szServiceName, "/Player", \ - "org.freedesktop.MediaPlayer", QDBusConnection::sessionBus()); \ - QDBusMessage reply = dbus_iface.call(QDBus::Block, __method); \ - if (reply.type() == QDBusMessage::ErrorMessage) { \ - QDBusError err = reply; \ +#define MPRIS_CALL_METHOD(__method, __return_if_fail) \ + QDBusInterface dbus_iface(m_szServiceName, "/Player", \ + "org.freedesktop.MediaPlayer", QDBusConnection::sessionBus()); \ + QDBusMessage reply = dbus_iface.call(QDBus::Block, __method); \ + if(reply.type() == QDBusMessage::ErrorMessage) \ + { \ + QDBusError err = reply; \ qDebug("Error: %s\n%s\n", qPrintable(err.name()), qPrintable(err.message())); \ - return __return_if_fail; \ + return __return_if_fail; \ } #define MPRIS_GET_METADATA_FIELD(__field, __return_type, __return_if_fail) \ - if (this->status() != MpInterface::Playing) \ - return __return_if_fail; \ - MPRIS_CALL_METHOD("GetMetadata", __return_if_fail) \ - foreach (QVariant w, reply.arguments()) { \ - QDBusArgument arg = qvariant_cast<QDBusArgument>(w); \ - QVariant v = qdbus_cast<QVariantMap>(arg); \ - if (v.userType() == QVariant::Map) { \ - const QVariantMap map = v.toMap(); \ - QVariantMap::ConstIterator it = map.find(__field); \ - if (it != map.end() && it.key() == __field) { \ - return it.value().value< __return_type >(); \ - } \ - } \ - } \ + if(this->status() != MpInterface::Playing) \ + return __return_if_fail; \ + MPRIS_CALL_METHOD("GetMetadata", __return_if_fail) \ + foreach(QVariant w, reply.arguments()) \ + { \ + QDBusArgument arg = qvariant_cast<QDBusArgument>(w); \ + QVariant v = qdbus_cast<QVariantMap>(arg); \ + if(v.userType() == QVariant::Map) \ + { \ + const QVariantMap map = v.toMap(); \ + QVariantMap::ConstIterator it = map.find(__field); \ + if(it != map.end() && it.key() == __field) \ + { \ + return it.value().value<__return_type>(); \ + } \ + } \ + } \ return __return_if_fail; -#define MPRIS_CALL_METHOD_WITH_ARG(__method, __arg, __return_if_fail) \ - QDBusInterface dbus_iface(m_szServiceName, "/Player", \ - "org.freedesktop.MediaPlayer", QDBusConnection::sessionBus()); \ - QDBusMessage reply = dbus_iface.call(QDBus::Block, __method, __arg); \ - if (reply.type() == QDBusMessage::ErrorMessage) { \ - QDBusError err = reply; \ - qDebug("Error: %s\n%s\n", qPrintable(err.name()), qPrintable(err.message())); \ - return __return_if_fail; \ - } +#define MPRIS_CALL_METHOD_WITH_ARG(__method, __arg, __return_if_fail) \ + QDBusInterface dbus_iface(m_szServiceName, "/Player", \ + "org.freedesktop.MediaPlayer", QDBusConnection::sessionBus()); \ + QDBusMessage reply = dbus_iface.call(QDBus::Block, __method, __arg); \ + if(reply.type() == QDBusMessage::ErrorMessage) \ + { \ + QDBusError err = reply; \ + qDebug("Error: %s\n%s\n", qPrintable(err.name()), qPrintable(err.message())); \ + return __return_if_fail; \ + } MpInterface::PlayerStatus MpMprisInterface::status() { @@ -172,38 +178,46 @@ MpInterface::PlayerStatus MpMprisInterface::status() status = qdbus_cast<MPRISPlayerStatus>(reply.arguments().first()); - switch (status.Play) { - case 0: return MpInterface::Playing; - case 1: return MpInterface::Paused; - case 2: return MpInterface::Stopped; - default: return MpInterface::Unknown; + switch(status.Play) + { + case 0: + return MpInterface::Playing; + case 1: + return MpInterface::Paused; + case 2: + return MpInterface::Stopped; + default: + return MpInterface::Unknown; } } QString MpMprisInterface::nowPlaying() { - if (this->status() != MpInterface::Playing) + if(this->status() != MpInterface::Playing) return ""; MPRIS_CALL_METHOD("GetMetadata", "") QString artist; QString title; - foreach (QVariant w, reply.arguments()) { + foreach(QVariant w, reply.arguments()) + { QDBusArgument arg = qvariant_cast<QDBusArgument>(w); QVariant v = qdbus_cast<QVariantMap>(arg); - if (v.userType() == QVariant::Map) { - const QVariantMap map = v.toMap(); - QVariantMap::ConstIterator it = map.constBegin(); - for ( ; it != map.constEnd(); ++it) { /* maybe do some configurable formatting */ - if (it.key() == "artist") + if(v.userType() == QVariant::Map) + { + const QVariantMap map = v.toMap(); + QVariantMap::ConstIterator it = map.constBegin(); + for(; it != map.constEnd(); ++it) + { /* maybe do some configurable formatting */ + if(it.key() == "artist") artist = it.value().toString(); - else if (it.key() == "title") + else if(it.key() == "title") title = it.value().toString(); } } } - if (artist.length() && title.length()) + if(artist.length() && title.length()) return artist + " - " + title; else return ""; @@ -213,13 +227,16 @@ QString MpMprisInterface::mrl() { MPRIS_CALL_METHOD("GetMetadata", "") - foreach (QVariant w, reply.arguments()) { + foreach(QVariant w, reply.arguments()) + { QDBusArgument arg = qvariant_cast<QDBusArgument>(w); QVariant v = qdbus_cast<QVariantMap>(arg); - if (v.userType() == QVariant::Map) { - const QVariantMap map = v.toMap(); - QVariantMap::ConstIterator it = map.find("location"); - if (it != map.end() && it.key() == "location") { + if(v.userType() == QVariant::Map) + { + const QVariantMap map = v.toMap(); + QVariantMap::ConstIterator it = map.find("location"); + if(it != map.end() && it.key() == "location") + { return it.value().toString(); } } @@ -267,9 +284,9 @@ int MpMprisInterface::sampleRate() MPRIS_GET_METADATA_FIELD("audio-samplerate", int, -1) } -bool MpMprisInterface::setVol(kvs_int_t &iVol) +bool MpMprisInterface::setVol(kvs_int_t & iVol) { - MPRIS_CALL_METHOD_WITH_ARG("VolumeSet", QVariant((int)(100*iVol/255)), false); + MPRIS_CALL_METHOD_WITH_ARG("VolumeSet", QVariant((int)(100 * iVol / 255)), false); return true; } @@ -278,7 +295,7 @@ int MpMprisInterface::getVol() MPRIS_CALL_METHOD("VolumeGet", -1) int iVol = reply.arguments().first().toInt(); - return iVol * 255 /100; + return iVol * 255 / 100; } int MpMprisInterface::position() @@ -291,14 +308,17 @@ int MpMprisInterface::length() { MPRIS_CALL_METHOD("GetMetadata", -1) - foreach (QVariant w, reply.arguments()) { + foreach(QVariant w, reply.arguments()) + { QDBusArgument arg = qvariant_cast<QDBusArgument>(w); QVariant v = qdbus_cast<QVariantMap>(arg); - if (v.userType() == QVariant::Map) { - const QVariantMap map = v.toMap(); - QVariantMap::ConstIterator it = map.constBegin(); - for ( ; it != map.constEnd(); ++it) { - if (it.key() == "mtime") + if(v.userType() == QVariant::Map) + { + const QVariantMap map = v.toMap(); + QVariantMap::ConstIterator it = map.constBegin(); + for(; it != map.constEnd(); ++it) + { + if(it.key() == "mtime") return it.value().toInt(); } } @@ -306,7 +326,7 @@ int MpMprisInterface::length() return -1; } -bool MpMprisInterface::jumpTo(kvs_int_t &iPos) +bool MpMprisInterface::jumpTo(kvs_int_t & iPos) { MPRIS_CALL_METHOD_WITH_ARG("PositionSet", QVariant((int)iPos), false) return true; @@ -314,18 +334,15 @@ bool MpMprisInterface::jumpTo(kvs_int_t &iPos) /* audacious interface */ MP_IMPLEMENT_DESCRIPTOR( - MpAudaciousInterface, - "audacious", - __tr2qs_ctx( - "An interface for the Audacious media player.\n" \ - "Download it from http://audacious-media-player.org\n" - , - "mediaplayer" - ) -) + MpAudaciousInterface, + "audacious", + __tr2qs_ctx( + "An interface for the Audacious media player.\n" + "Download it from http://audacious-media-player.org\n", + "mediaplayer")) MpAudaciousInterface::MpAudaciousInterface() -: MpMprisInterface() + : MpMprisInterface() { m_szServiceName = "org.mpris.audacious"; } @@ -333,15 +350,14 @@ MpAudaciousInterface::MpAudaciousInterface() int MpAudaciousInterface::getPlayListPos() { QDBusInterface dbus_iface("org.mpris.audacious", "/org/atheme/audacious", - "org.atheme.audacious", QDBusConnection::sessionBus()); + "org.atheme.audacious", QDBusConnection::sessionBus()); QDBusReply<uint> pos = dbus_iface.call(QDBus::Block, "Position"); return pos; } - bool MpAudaciousInterface::quit() { - if (MpMprisInterface::quit()) + if(MpMprisInterface::quit()) return true; /* compatibility with older versions */ @@ -352,20 +368,24 @@ QString MpAudaciousInterface::mrl() { MPRIS_CALL_METHOD("GetMetadata", "") - foreach (QVariant w, reply.arguments()) { + foreach(QVariant w, reply.arguments()) + { QDBusArgument arg = qvariant_cast<QDBusArgument>(w); QVariant v = qdbus_cast<QVariantMap>(arg); - if (v.userType() == QVariant::Map) { - const QVariantMap map = v.toMap(); - QVariantMap::ConstIterator it = map.find("location"); - if (it != map.end() && it.key() == "location") { + if(v.userType() == QVariant::Map) + { + const QVariantMap map = v.toMap(); + QVariantMap::ConstIterator it = map.find("location"); + if(it != map.end() && it.key() == "location") + { return it.value().toString(); } /* Some audacious versions send URI instead of location */ it = map.find("URI"); - if (it != map.end() && it.key() == "URI") { - return it.value().toString(); - } + if(it != map.end() && it.key() == "URI") + { + return it.value().toString(); + } } } return ""; @@ -375,42 +395,50 @@ MpInterface::PlayerStatus MpAudaciousInterface::status() { MpInterface::PlayerStatus status; status = MpMprisInterface::status(); - if (status != MpInterface::Unknown) + if(status != MpInterface::Unknown) return status; /* compatibility with older versions */ QDBusInterface dbus_iface(m_szServiceName, "/Player", - "org.freedesktop.MediaPlayer", QDBusConnection::sessionBus()); - if (!dbus_iface.isValid()) + "org.freedesktop.MediaPlayer", QDBusConnection::sessionBus()); + if(!dbus_iface.isValid()) return MpInterface::Unknown; QDBusMessage reply = dbus_iface.call(QDBus::Block, "GetStatus"); - switch (reply.arguments().first().toInt()) { - case 0: return MpInterface::Playing; - case 1: return MpInterface::Paused; - case 2: return MpInterface::Stopped; - default: return MpInterface::Unknown; + switch(reply.arguments().first().toInt()) + { + case 0: + return MpInterface::Playing; + case 1: + return MpInterface::Paused; + case 2: + return MpInterface::Stopped; + default: + return MpInterface::Unknown; } } int MpAudaciousInterface::length() { int length = MpMprisInterface::length(); - if (length != -1) + if(length != -1) return length; /* compatibility with older versions */ MPRIS_CALL_METHOD("GetMetadata", -1) - foreach (QVariant w, reply.arguments()) { + foreach(QVariant w, reply.arguments()) + { QDBusArgument arg = qvariant_cast<QDBusArgument>(w); QVariant v = qdbus_cast<QVariantMap>(arg); - if (v.userType() == QVariant::Map) { - const QVariantMap map = v.toMap(); - QVariantMap::ConstIterator it = map.constBegin(); - for ( ; it != map.constEnd(); ++it) { - if (it.key() == "length") + if(v.userType() == QVariant::Map) + { + const QVariantMap map = v.toMap(); + QVariantMap::ConstIterator it = map.constBegin(); + for(; it != map.constEnd(); ++it) + { + if(it.key() == "length") return it.value().toInt(); } } @@ -418,15 +446,15 @@ int MpAudaciousInterface::length() return -1; } -#define AUDACIOUS_GET_TUPLE_FIELD(__field) \ - if (this->status() != MpInterface::Playing) \ - return ""; \ - QDBusInterface dbus_iface("org.mpris.audacious", "/org/atheme/audacious", \ - "org.atheme.audacious", QDBusConnection::sessionBus()); \ - QList<QVariant> args; \ - args << (uint)this->getPlayListPos() << QString(__field); \ +#define AUDACIOUS_GET_TUPLE_FIELD(__field) \ + if(this->status() != MpInterface::Playing) \ + return ""; \ + QDBusInterface dbus_iface("org.mpris.audacious", "/org/atheme/audacious", \ + "org.atheme.audacious", QDBusConnection::sessionBus()); \ + QList<QVariant> args; \ + args << (uint) this->getPlayListPos() << QString(__field); \ QDBusReply<QDBusVariant> reply = dbus_iface.callWithArgumentList(QDBus::Block, "SongTuple", args); \ - return reply.value().variant().toString(); \ + return reply.value().variant().toString(); QString MpAudaciousInterface::year() { @@ -440,18 +468,15 @@ QString MpAudaciousInterface::mediaType() /* BMPx interface */ MP_IMPLEMENT_DESCRIPTOR( - MpBmpxInterface, - "bmpx", - __tr2qs_ctx( - "An interface for BMPx.\n" \ - "Download it from http://sourceforge.net/projects/beepmp\n" - , - "mediaplayer" - ) -) + MpBmpxInterface, + "bmpx", + __tr2qs_ctx( + "An interface for BMPx.\n" + "Download it from http://sourceforge.net/projects/beepmp\n", + "mediaplayer")) MpBmpxInterface::MpBmpxInterface() -: MpMprisInterface() + : MpMprisInterface() { m_szServiceName = "org.mpris.bmp"; } @@ -464,72 +489,62 @@ MpInterface::PlayerStatus MpBmpxInterface::status() /* Amarok2 interface */ MP_IMPLEMENT_DESCRIPTOR( - MpAmarok2Interface, - "amarok2", - __tr2qs_ctx( - "An interface for Amarok2.\n" \ - "Download it from http://amarok.kde.org\n" - , - "mediaplayer" - ) -) + MpAmarok2Interface, + "amarok2", + __tr2qs_ctx( + "An interface for Amarok2.\n" + "Download it from http://amarok.kde.org\n", + "mediaplayer")) MpAmarok2Interface::MpAmarok2Interface() -: MpMprisInterface() + : MpMprisInterface() { m_szServiceName = "org.mpris.amarok"; } /* Qmmp interface */ MP_IMPLEMENT_DESCRIPTOR( - MpQmmpInterface, - "Qmmp", - __tr2qs_ctx( - "An interface for Qmmp.\n" \ - "Download it from http://qmmp.ylsoftware.com\n" - , - "mediaplayer" - ) -) + MpQmmpInterface, + "Qmmp", + __tr2qs_ctx( + "An interface for Qmmp.\n" + "Download it from http://qmmp.ylsoftware.com\n", + "mediaplayer")) MpQmmpInterface::MpQmmpInterface() -: MpMprisInterface() + : MpMprisInterface() { - m_szServiceName = "org.mpris.qmmp"; + m_szServiceName = "org.mpris.qmmp"; } /* xmms2 interface */ MP_IMPLEMENT_DESCRIPTOR( - MpXmms2Interface, - "xmms2", - __tr2qs_ctx( - "An interface for the XMMS2 media player.\n" \ - "Download it from http://xmms2.org\n", - "mediaplayer" - ) -) + MpXmms2Interface, + "xmms2", + __tr2qs_ctx( + "An interface for the XMMS2 media player.\n" + "Download it from http://xmms2.org\n", + "mediaplayer")) MpXmms2Interface::MpXmms2Interface() -: MpMprisInterface() + : MpMprisInterface() { m_szServiceName = "org.mpris.xmms2"; } /* mozilla songbird interface */ MP_IMPLEMENT_DESCRIPTOR( - MpSongbirdInterface, - "songbird", - __tr2qs_ctx( - "An interface for the Mozilla Songbird media player.\n" \ - "Download it from http://www.getsongbird.com.\n" \ - "To use it you have to install also the MPRIS addon " \ - "available at http://addons.songbirdnest.com/addon/1626.\n", - "mediaplayer" - ) -) + MpSongbirdInterface, + "songbird", + __tr2qs_ctx( + "An interface for the Mozilla Songbird media player.\n" + "Download it from http://www.getsongbird.com.\n" + "To use it you have to install also the MPRIS addon " + "available at http://addons.songbirdnest.com/addon/1626.\n", + "mediaplayer")) MpSongbirdInterface::MpSongbirdInterface() -: MpMprisInterface() + : MpMprisInterface() { m_szServiceName = "org.mpris.songbird"; } @@ -541,56 +556,47 @@ MpInterface::PlayerStatus MpSongbirdInterface::status() /* Totem interface */ MP_IMPLEMENT_DESCRIPTOR( - MpTotemInterface, - "totem", - __tr2qs_ctx( - "An interface for Totem.\n" \ - "Download it from http://projects.gnome.org/totem/\n" - , - "mediaplayer" - ) -) + MpTotemInterface, + "totem", + __tr2qs_ctx( + "An interface for Totem.\n" + "Download it from http://projects.gnome.org/totem/\n", + "mediaplayer")) MpTotemInterface::MpTotemInterface() -: MpMprisInterface() + : MpMprisInterface() { m_szServiceName = "org.mpris.Totem"; } /* Vlc interface */ MP_IMPLEMENT_DESCRIPTOR( - MpVlcInterface, - "vlc", - __tr2qs_ctx( - "An interface for VLC.\n" \ - "Download it from http://www.videolan.org/\n" \ - "You need to manually enable the D-Bus control\n" \ - "interface in the VLC preferences\n" \ - , - "mediaplayer" - ) -) + MpVlcInterface, + "vlc", + __tr2qs_ctx( + "An interface for VLC.\n" + "Download it from http://www.videolan.org/\n" + "You need to manually enable the D-Bus control\n" + "interface in the VLC preferences\n", + "mediaplayer")) MpVlcInterface::MpVlcInterface() -: MpMprisInterface() + : MpMprisInterface() { m_szServiceName = "org.mpris.vlc"; } /* Clementine interface */ MP_IMPLEMENT_DESCRIPTOR( - MpClementineInterface, - "clementine", - __tr2qs_ctx( - "An interface for Clementine.\n" \ - "Download it from http://www.clementine-player.org/\n" \ - , - "mediaplayer" - ) -) + MpClementineInterface, + "clementine", + __tr2qs_ctx( + "An interface for Clementine.\n" + "Download it from http://www.clementine-player.org/\n", + "mediaplayer")) MpClementineInterface::MpClementineInterface() -: MpMprisInterface() + : MpMprisInterface() { m_szServiceName = "org.mpris.clementine"; } |
