diff options
author | Javier <dev.git@javispedro.com> | 2016-09-02 19:11:27 +0200 |
---|---|---|
committer | Javier <dev.git@javispedro.com> | 2016-09-02 19:11:27 +0200 |
commit | 01e9344b49c1b4877a0a15a620477fb78b3a5d37 (patch) | |
tree | 5cfb75573359cd84e830046c456ee02a4c15e6b4 | |
parent | bbbc4b88c8bfb69dd652e1ffd2f6f8afdb2fe0fc (diff) | |
download | libwatchfish-01e9344b49c1b4877a0a15a620477fb78b3a5d37.tar.gz libwatchfish-01e9344b49c1b4877a0a15a620477fb78b3a5d37.zip |
add partial volume controller
-rw-r--r-- | com.Meego.MainVolume2.xml | 13 | ||||
-rw-r--r-- | libwatchfish.pri | 11 | ||||
-rw-r--r-- | volumecontroller.cpp | 133 | ||||
-rw-r--r-- | volumecontroller.h | 65 | ||||
-rw-r--r-- | volumecontroller_p.h | 54 |
5 files changed, 274 insertions, 2 deletions
diff --git a/com.Meego.MainVolume2.xml b/com.Meego.MainVolume2.xml new file mode 100644 index 0000000..9b5b941 --- /dev/null +++ b/com.Meego.MainVolume2.xml @@ -0,0 +1,13 @@ +<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" +"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> +<node> + <interface name="com.Meego.MainVolume2"> + <property name="StepCount" type="u" access="read"/> + <property name="CurrentStep" type="u" access="readwrite"/> + <signal name="StepsUpdated"> + <arg name="StepCount" type="u" direction="out"/> + <arg name="CurrentStep" type="u" direction="out"/> + </signal> + </interface> +</node> + diff --git a/libwatchfish.pri b/libwatchfish.pri index 8a256c2..24a3c66 100644 --- a/libwatchfish.pri +++ b/libwatchfish.pri @@ -14,8 +14,9 @@ contains(WATCHFISH_FEATURES, walltime) { contains(WATCHFISH_FEATURES, music) { PKGCONFIG += mpris-qt5 - HEADERS += $$PWD/musiccontroller.h $$PWD/musiccontroller_p.h - SOURCES += $$PWD/musiccontroller.cpp + HEADERS += $$PWD/musiccontroller.h $$PWD/musiccontroller_p.h $$PWD/volumecontroller.h + SOURCES += $$PWD/musiccontroller.cpp $$PWD/volumecontroller.cpp + DBUS_INTERFACES += com.Meego.MainVolume2.xml } contains(WATCHFISH_FEATURES, calendar) { @@ -29,3 +30,9 @@ contains(WATCHFISH_FEATURES, voicecall) { SOURCES += $$PWD/voicecallcontroller.cpp DBUS_INTERFACES += org.nemomobile.voicecall.VoiceCallManager.xml org.nemomobile.voicecall.VoiceCall.xml } + +HEADERS += \ + $$PWD/volumecontroller_p.h + +DISTFILES += \ + $$PWD/com.Meego.MainVolume2.xml diff --git a/volumecontroller.cpp b/volumecontroller.cpp new file mode 100644 index 0000000..5fc19f5 --- /dev/null +++ b/volumecontroller.cpp @@ -0,0 +1,133 @@ +/* + * libwatchfish - library with common functionality for SailfishOS smartwatch connector programs. + * Copyright (C) 2016 Javier S. Pedro <dev.git@javispedro.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "volumecontroller.h" +#include "volumecontroller_p.h" + +namespace watchfish +{ + +Q_LOGGING_CATEGORY(volumeControllerCat, "watchfish-VolumeController") + +static const char *VOLUME_SERVICE = "com.Meego.MainVolume2"; +static const char *VOLUME_PATH = "/com/meego/mainvolume2"; + +VolumeControllerPrivate::VolumeControllerPrivate(VolumeController *q) + : volume(0), + curValue(0), curMaximum(0), + q_ptr(q) +{ + const QString address = connectionAddress(); + QDBusConnection connection = QDBusConnection::connectToPeer(address, "pulse"); + + volume = new ComMeegoMainVolume2Interface(VOLUME_SERVICE, VOLUME_PATH, connection, this); + + connect(volume, &ComMeegoMainVolume2Interface::StepsUpdated, + this, &VolumeControllerPrivate::handleStepsUpdated); + if (volume->isValid()) { + handleStepsUpdated(volume->stepCount(), volume->currentStep()); + } +} + +VolumeControllerPrivate::~VolumeControllerPrivate() +{ +} + +QString VolumeControllerPrivate::connectionAddress() +{ + if (qEnvironmentVariableIsSet("PULSE_DBUS_SERVER")) { + return QString::fromLocal8Bit(qgetenv("PULSE_DBUS_SERVER")); + } else { + QDBusConnection bus = QDBusConnection::sessionBus(); + QDBusMessage message = QDBusMessage::createMethodCall("org.pulseaudio.Server", "/org/pulseaudio/server_lookup1", + "org.freedesktop.DBus.Properties", "Get"); + message.setArguments(QVariantList() << "org.PulseAudio.ServerLookup1" << "Address"); + QDBusMessage reply = bus.call(message); + if (reply.type() == QDBusMessage::ReplyMessage && reply.arguments().count() > 0) { + return reply.arguments().first().value<QDBusVariant>().variant().toString(); + } + } + + return QString(); +} + +void VolumeControllerPrivate::handleStepsUpdated(uint StepCount, uint CurrentStep) +{ + Q_Q(VolumeController); + qCDebug(volumeControllerCat) << "updated stuff"; + const int oldValue = curValue, oldMaximum = curMaximum; + curValue = CurrentStep; + curMaximum = StepCount; + if (oldMaximum != 0) { + qCDebug(volumeControllerCat) << "Volume updated" << curValue << "/" << curMaximum; + } + if (curMaximum != oldMaximum) { + emit q->maximumChanged(); + } + if (curValue != oldValue) { + emit q->valueChanged(); + } +} + +VolumeController::VolumeController(QObject *parent) + : QObject(parent), d_ptr(new VolumeControllerPrivate(this)) +{ +} + +VolumeController::~VolumeController() +{ +} + +int VolumeController::value() const +{ + Q_D(const VolumeController); + return d->volume->currentStep(); +} + +int VolumeController::minimum() const +{ + return 0; +} + +int VolumeController::maximum() const +{ + Q_D(const VolumeController); + return d->volume->stepCount(); +} + +void VolumeController::setValue(int v) +{ + Q_D(const VolumeController); + qCDebug(volumeControllerCat) << "Setting volume to" << v; + if (!d->volume->isValid()) { + qCWarning(volumeControllerCat) << "Lost connection to volume interface"; + } + d->volume->setCurrentStep(v); +} + +void VolumeController::up() +{ + setValue(value() + 1); +} + +void VolumeController::down() +{ + setValue(value() - 1); +} + +} diff --git a/volumecontroller.h b/volumecontroller.h new file mode 100644 index 0000000..d92d6a2 --- /dev/null +++ b/volumecontroller.h @@ -0,0 +1,65 @@ +/* + * libwatchfish - library with common functionality for SailfishOS smartwatch connector programs. + * Copyright (C) 2016 Javier S. Pedro <dev.git@javispedro.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef WATCHFISH_VOLUMECONTROLLER_H +#define WATCHFISH_VOLUMECONTROLLER_H + +#include <QtCore/QLoggingCategory> + +namespace watchfish +{ + +Q_DECLARE_LOGGING_CATEGORY(volumeControllerCat) + +class VolumeControllerPrivate; + +class VolumeController : public QObject +{ + Q_OBJECT + Q_DECLARE_PRIVATE(VolumeController) + + Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged) + Q_PROPERTY(int minimum READ minimum NOTIFY minimumChanged) + Q_PROPERTY(int maximum READ maximum NOTIFY maximumChanged) + +public: + explicit VolumeController(QObject *parent = 0); + ~VolumeController(); + + int value() const; + int minimum() const; + int maximum() const; + + void setValue(int v); + +public slots: + void up(); + void down(); + +signals: + void valueChanged(); + void minimumChanged(); + void maximumChanged(); + +private: + VolumeControllerPrivate * const d_ptr; +}; + +} + +#endif // WATCHFISH_VOLUMECONTROLLER_H diff --git a/volumecontroller_p.h b/volumecontroller_p.h new file mode 100644 index 0000000..bd3babc --- /dev/null +++ b/volumecontroller_p.h @@ -0,0 +1,54 @@ +/* + * libwatchfish - library with common functionality for SailfishOS smartwatch connector programs. + * Copyright (C) 2016 Javier S. Pedro <dev.git@javispedro.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef WATCHFISH_VOLUMECONTROLLER_P_H +#define WATCHFISH_VOLUMECONTROLLER_P_H + +#include <QDBusConnection> +#include "mainvolume2_interface.h" +#include "volumecontroller.h" + +namespace watchfish +{ + +class VolumeControllerPrivate : public QObject +{ + Q_OBJECT + +public: + explicit VolumeControllerPrivate(VolumeController *q); + ~VolumeControllerPrivate(); + + ComMeegoMainVolume2Interface *volume; + + int curValue; + int curMaximum; + + static QString connectionAddress(); + +public slots: + void handleStepsUpdated(uint StepCount, uint CurrentStep); + +private: + VolumeController * const q_ptr; + Q_DECLARE_PUBLIC(VolumeController) +}; + +} + +#endif // WATCHFISH_VOLUMECONTROLLER_P_H |