File indexing completed on 2025-02-23 04:35:17
0001 // SPDX-FileCopyrightText: 2014 Sujith Haridasan <sujith.haridasan@kdemail.net> 0002 // SPDX-FileCopyrightText: 2014 Ashish Madeti <ashishmadeti@gmail.com> 0003 // SPDX-FileCopyrightText: 2016 Matthieu Gallien <matthieu_gallien@yahoo.fr> 0004 // SPDX-FileCopyrightText: 2022-2023 Bart De Vries <bart@mogwai.be> 0005 // SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com> 0006 // SPDX-License-Identifier: GPL-3.0-or-later 0007 0008 #include "mpris2.h" 0009 0010 #include "mpris2logging.h" 0011 #include "videocontroller.h" 0012 0013 #if !defined Q_OS_ANDROID 0014 #include <QDBusConnection> 0015 #endif 0016 0017 #if defined Q_OS_WIN 0018 #include <Windows.h> 0019 #else 0020 #include <unistd.h> 0021 #endif 0022 0023 #if !defined Q_OS_ANDROID 0024 #include "mediaplayer2.h" 0025 #include "mediaplayer2player.h" 0026 #endif 0027 0028 Mpris2::Mpris2(QObject *parent) 0029 : QObject(parent) 0030 , m_audioPlayer(dynamic_cast<VideoController *>(parent)) 0031 { 0032 qCDebug(Mpris2Log) << "Mpris2::Mpris2()"; 0033 0034 #if !defined Q_OS_ANDROID 0035 initDBusService(); 0036 #endif 0037 } 0038 0039 void Mpris2::initDBusService() 0040 { 0041 qCDebug(Mpris2Log) << "Mpris2::initDBusService()"; 0042 #if !defined Q_OS_ANDROID 0043 0044 QString tryPlayerName = QStringLiteral("PlasmaTube"); 0045 QString mpris2Name(QStringLiteral("org.mpris.MediaPlayer2.") + tryPlayerName); 0046 0047 bool success = QDBusConnection::sessionBus().registerService(mpris2Name); 0048 0049 // If the above failed, it's likely because we're not the first instance 0050 // or the name is already taken. In that event the MPRIS2 spec wants the 0051 // following: 0052 if (!success) { 0053 #if defined Q_OS_WIN 0054 tryPlayerName = tryPlayerName + QLatin1String("".instance) + QString::number(GetCurrentProcessId()); 0055 #else 0056 tryPlayerName = tryPlayerName + QLatin1String(".instance") + QString::number(getpid()); 0057 #endif 0058 success = QDBusConnection::sessionBus().registerService(QStringLiteral("org.mpris.MediaPlayer2.") + tryPlayerName); 0059 } 0060 0061 if (success) { 0062 m_playerName = tryPlayerName; 0063 if (!m_mp2) { 0064 m_mp2 = std::make_unique<MediaPlayer2>(m_audioPlayer, this); 0065 m_mp2p = std::make_unique<MediaPlayer2Player>(m_audioPlayer, this); 0066 } 0067 0068 QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/mpris/MediaPlayer2"), this, QDBusConnection::ExportAdaptors); 0069 qCDebug(Mpris2Log) << "Mpris2::initDBusService() success"; 0070 } 0071 #endif 0072 } 0073 0074 bool Mpris2::unregisterDBusService() 0075 { 0076 bool success = true; 0077 #if !defined Q_OS_ANDROID 0078 QString oldMpris2Name(QStringLiteral("org.mpris.MediaPlayer2.") + m_playerName); 0079 success = QDBusConnection::sessionBus().unregisterService(oldMpris2Name); 0080 0081 if (success) { 0082 m_playerName = QLatin1String(""); 0083 } 0084 #endif 0085 return success; 0086 } 0087 0088 Mpris2::~Mpris2() 0089 { 0090 qCDebug(Mpris2Log) << "Mpris2::~Mpris2()"; 0091 unregisterDBusService(); 0092 }