-
Notifications
You must be signed in to change notification settings - Fork 6
/
volumecontroller.cpp
133 lines (113 loc) · 3.7 KB
/
volumecontroller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* libwatchfish - library with common functionality for SailfishOS smartwatch connector programs.
* Copyright (C) 2016 Javier S. Pedro <[email protected]>
*
* 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);
}
}