File: sound.cpp

package info (click to toggle)
ukui-settings-daemon 4.0.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,324 kB
  • sloc: cpp: 39,120; ansic: 3,240; xml: 1,570; sh: 88; makefile: 4
file content (131 lines) | stat: -rw-r--r-- 3,737 bytes parent folder | download | duplicates (2)
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
/* -*- Mode: C++; indent-tabs-mode: nil; tab-width: 4 -*-
 * -*- coding: utf-8 -*-
 *
 * Copyright (C) 2023 KylinSoft Co., Ltd.
 *
 * 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
 * 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 "sound.h"
#include <QVariant>
extern "C" {
#include "clib-syslog.h"
}

#define SOUND_SCHEMA   "org.ukui.SettingsDaemon.plugins.media-keys-state"

#define UKUI_SOUND "org.ukui.sound"
#define MAX_VOLUME_NORMAL 100

static QString s_sinkVolume = "sink-volume";

static QString s_sinkMute = "sink-mute";

static QString s_sinkVolumeIncrease = "volume-increase-value";

Q_GLOBAL_STATIC(Sound, s_sound)
Sound::Sound(QObject *parent) : QObject(parent)
{
    if (!m_pulseAudio) {
        m_pulseAudio = new PulseAudioManager();
        connect(m_pulseAudio, SIGNAL(sinkVolumeChanged(int)), this, SLOT(doSinkVolumeChanged(int)), Qt::DirectConnection);
        connect(m_pulseAudio, SIGNAL(sinkMuteChanged(bool)), this, SLOT(doSinkMuteChanged(bool)), Qt::DirectConnection);
        m_pulseAudio->connectPulseContext();
    }

    if (QGSettings::isSchemaInstalled(QByteArray(SOUND_SCHEMA))) {
        m_soundSettings = new QGSettings(SOUND_SCHEMA);
        connect(m_soundSettings, SIGNAL(changed(QString)), this, SLOT(doSettingsChanged(const QString&)));
    }
}

Sound::~Sound()
{
    if (m_pulseAudio) {
        disconnect(m_pulseAudio, 0);
        m_pulseAudio->deleteLater();
        m_pulseAudio = nullptr;
    }
    if (m_soundSettings) {
        disconnect(m_soundSettings, SIGNAL(changed(QString)), this, SLOT(doSettingsChanged(const QString&)));
        m_soundSettings->deleteLater();
        m_soundSettings = nullptr;
    }
}

int Sound::getSinkVolume()
{
    return m_pulseAudio->getSinkVolume();
}

bool Sound::getSinkMute()
{
    return m_pulseAudio->getSinkMute();
}

bool Sound::getSourceMute()
{
    return m_pulseAudio->getSourceMute();
}

Sound* Sound::self()
{
    return s_sound;
}

void Sound::setSinkVolume(int volume)
{
    m_pulseAudio->setSinkVolume(volume);
}

void Sound::setSinkMute(bool mute)
{
    m_pulseAudio->setSinkMute(mute);
}

void Sound::setSourceMute(bool mute)
{
    m_pulseAudio->setSourceMute(mute);
}

void Sound::doSinkVolumeChanged(int value)
{
    if (m_soundSettings && m_soundSettings->keys().contains(s_sinkVolume)) {
        if (value != m_soundSettings->get(s_sinkVolume).toInt()) {
            m_soundSettings->set(s_sinkVolume, value);
            USD_LOG(LOG_DEBUG, "volume is %d", value);
        }
    } else {
        USD_LOG(LOG_DEBUG, "Sound settings is null or is not contains sink volume");
    }
}

void Sound::doSinkMuteChanged(bool value)
{
    if (m_soundSettings && m_soundSettings->keys().contains(s_sinkMute)) {
        if (value != m_soundSettings->get(s_sinkMute).toBool()) {
            m_soundSettings->set(s_sinkMute, value);
        }
    } else {
        USD_LOG(LOG_DEBUG, "Sound settings is null or is not contains sink mute");
    }
}

void Sound::doSettingsChanged(const QString &key)
{
    if (key == s_sinkVolume) {
        setSinkVolume(m_soundSettings->get(s_sinkVolume).toInt());
    } else if (key == s_sinkMute){
        setSinkMute(m_soundSettings->get(s_sinkMute).toBool());
    }
}