-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmusiccontroller_sailfish.cpp
More file actions
149 lines (128 loc) · 4.63 KB
/
musiccontroller_sailfish.cpp
File metadata and controls
149 lines (128 loc) · 4.63 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* libwatchfish - library with common functionality for SailfishOS smartwatch connector programs.
* Copyright (C) 2015 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/>.
*/
// Volume control for Sailfish OS via PulseAudio P2P DBus (com.Meego.MainVolume2).
// Volume is step-based (0..StepCount), returned as int directly.
#include <QDBusMessage>
#include <QDBusReply>
#include "musiccontroller.h"
#include "musiccontroller_p.h"
namespace watchfish
{
MusicControllerPrivate::~MusicControllerPrivate()
{
if (_pulseBus != nullptr) {
qDebug() << "Disconnecting from PulseAudio P2P DBus";
QDBusConnection::disconnectFromBus("org.PulseAudio1");
delete _pulseBus;
}
}
void MusicControllerPrivate::connectPulseBus()
{
if (_pulseBus) {
if (!_pulseBus->isConnected()) {
delete _pulseBus;
} else {
return;
}
}
QDBusMessage call = QDBusMessage::createMethodCall(
"org.PulseAudio1", "/org/pulseaudio/server_lookup1",
"org.freedesktop.DBus.Properties", "Get");
call << "org.PulseAudio.ServerLookup1" << "Address";
QDBusReply<QDBusVariant> lookupReply = QDBusConnection::sessionBus().call(call);
if (!lookupReply.isValid()) {
qDebug() << "Cannot connect to PulseAudio bus";
return;
}
qDebug() << "PulseAudio Bus address:" << lookupReply.value().variant().toString();
_pulseBus = new QDBusConnection(
QDBusConnection::connectToPeer(lookupReply.value().variant().toString(), "org.PulseAudio1"));
if (_maxVolume == 0) {
call = QDBusMessage::createMethodCall(
"com.Meego.MainVolume2", "/com/meego/mainvolume2",
"org.freedesktop.DBus.Properties", "Get");
call << "com.Meego.MainVolume2" << "StepCount";
QDBusReply<QDBusVariant> volumeMaxReply = _pulseBus->call(call);
if (volumeMaxReply.isValid()) {
_maxVolume = volumeMaxReply.value().variant().toUInt();
qDebug() << "Max volume:" << _maxVolume;
} else {
qWarning() << "Could not read volume max, cannot adjust volume:"
<< volumeMaxReply.error().message();
}
}
}
int MusicController::volume() const
{
if (d_ptr->_pulseBus == nullptr) {
qWarning() << "PulseAudio bus not available";
return -1;
}
QDBusMessage call = QDBusMessage::createMethodCall(
"com.Meego.MainVolume2", "/com/meego/mainvolume2",
"org.freedesktop.DBus.Properties", "Get");
call << "com.Meego.MainVolume2" << "CurrentStep";
QDBusReply<QDBusVariant> volumeReply = d_ptr->_pulseBus->call(call);
if (volumeReply.isValid()) {
return static_cast<int>(volumeReply.value().variant().toUInt());
}
return -1;
}
void MusicController::setVolume(const uint newVolume)
{
qDebug() << "Setting volume:" << newVolume;
d_ptr->connectPulseBus();
if (d_ptr->_pulseBus == nullptr) {
qWarning() << "PulseAudio bus not available";
return;
}
QDBusMessage call = QDBusMessage::createMethodCall(
"com.Meego.MainVolume2", "/com/meego/mainvolume2",
"org.freedesktop.DBus.Properties", "Set");
call << "com.Meego.MainVolume2" << "CurrentStep"
<< QVariant::fromValue(QDBusVariant(newVolume));
QDBusError err = d_ptr->_pulseBus->call(call);
if (err.isValid()) {
qWarning() << err.message();
}
}
void MusicController::volumeUp()
{
d_ptr->connectPulseBus();
int curVolume = volume();
if (curVolume < 0) {
return;
}
uint newVolume = static_cast<uint>(curVolume) + 1;
if (newVolume >= d_ptr->_maxVolume) {
qDebug() << "Cannot increase volume beyond maximum" << d_ptr->_maxVolume;
return;
}
setVolume(newVolume);
}
void MusicController::volumeDown()
{
d_ptr->connectPulseBus();
int curVolume = volume();
if (curVolume <= 0) {
qDebug() << "Cannot decrease volume beyond 0";
return;
}
setVolume(static_cast<uint>(curVolume) - 1);
}
} // namespace watchfish