Add DBUS support for media keys

This commit is contained in:
Vasily
2019-06-05 19:57:44 +02:00
committed by username223
parent 263759a0df
commit f958026453
3 changed files with 69 additions and 3 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
#pragma once
#include <easylogging++.h>
#include <vector>
#include <QString>
+59
View File
@@ -0,0 +1,59 @@
#include "media_keys.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QStringList>
#include <QtDBus/QtDBus>
namespace keyboardinput
{
void callMethodForAllMediaPlayerAvailableServices( const QString& method )
{
if ( !QDBusConnection::sessionBus().isConnected() )
{
LOG( ERROR ) << "Media Keys: Unable to connect to DBUS session bus.";
return;
}
QDBusReply<QStringList> names
= QDBusConnection::sessionBus().interface()->registeredServiceNames();
if ( !names.isValid() )
{
LOG( ERROR )
<< "Media Keys: Error getting DBUS registered service names:";
LOG( ERROR ) << names.error().message();
}
foreach ( QString name, names.value() )
{
if ( name.contains( "org.mpris.MediaPlayer2" ) )
{
QDBusInterface iface( name,
"/org/mpris/MediaPlayer2",
"org.mpris.MediaPlayer2.Player",
QDBusConnection::sessionBus() );
iface.call( method );
}
}
}
void sendMediaNextSong()
{
callMethodForAllMediaPlayerAvailableServices( "Next" );
}
void sendMediaPreviousSong()
{
callMethodForAllMediaPlayerAvailableServices( "Previous" );
}
void sendMediaPausePlay()
{
callMethodForAllMediaPlayerAvailableServices( "PlayPause" );
}
void sendMediaStopSong()
{
callMethodForAllMediaPlayerAvailableServices( "Stop" );
}
} // namespace keyboardinput