1

i have this c++ code that tries to connect to the org.gnome.SettingsDaemon.MediaKeys dbus service and listen to its MediaPlayerKeyPressed signal to get notified when a media key is pressed using qt and qdbus. My code complies and runs but i don't recieve any signals, so if anyone could tell me what exactly am i doing wrong i would be so grateful.

the code:

#include <QtWidgets/QApplication>
#include <QtDBus/QtDBus>
#include <QtWidgets/QLabel>
#include <QtCore/QString>
#include "mediaHandler.h"

#define SERVICE_NAME "org.gnome.SettingsDaemon.MediaKeys"
#define OBJECT_PATH "/org/gnome/SettingsDaemon/MediaKeys"
#define INTERFACE_NAME "org.gnome.SettingsDaemon.MediaKeys"
#define SIGNAL_NAME "MediaPlayerKeyPressed"

void MediaHandler::MediaPlayerKeyPressed(QString name, QString key)
{
    qDebug("test %s", name.toStdString().c_str());
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QDBusInterface interface(SERVICE_NAME,
                             OBJECT_PATH,
                             INTERFACE_NAME,
                             QDBusConnection::sessionBus());
    if (!interface.isValid())
    {
        qDebug("interface is not valid\n");
        qDebug("%s\n", qPrintable(QDBusConnection::sessionBus().lastError().message()));
        exit(1);
    }

    qDebug("interface is valid");
    QDBusReply<void> reply = interface.call("GrabMediaPlayerKeys", "test", (unsigned int)0);
    if (!reply.isValid())
    {
        qDebug("Reply was: invalid");
        qDebug("%s", qPrintable(reply.error().message()));
        exit(1);
    }

    qDebug("Reply was: valid");

    MediaHandler mediaHandler;

    if (!QObject::connect(&interface, SIGNAL(MediaPlayerKeyPressed(QString, QString)),
                          &mediaHandler, SLOT(MediaPlayerKeyPressed(QString, QString))))
    {
        qDebug("error1:%s",
               qPrintable(QDBusConnection::sessionBus().lastError().message()));
    }
    else
    {
        qDebug("connected");
    }

    return app.exec();
}

i tried testing the service with dbus-send using the GrabMediaPlayerKeys method and it works fine, so i think the problem is somewhere in my code.

  • If you run `dbus-monitor --session` or `busctl monitor --user`, does it show your program correctly calling GrabMediaPlayerKeys, and does it show the KeyPressed d-bus signals being issued? – user1686 Jun 21 '23 at 05:30
  • @user1686 so I hadn't tried that before, as I didn't know I could see what happens on the bus in real-time so thanks for that, it'll be very helpful. But yeah I can see the GrabMediaPlayerKeys call that my process makes. – Hazem Gamal Jun 22 '23 at 07:10

0 Answers0