File: ossengine.cpp

package info (click to toggle)
lxqt-panel 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 15,748 kB
  • sloc: cpp: 27,548; xml: 798; makefile: 19
file content (127 lines) | stat: -rw-r--r-- 3,256 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
/* BEGIN_COMMON_COPYRIGHT_HEADER
 * (c)LGPL2+
 *
 * LXQt - a lightweight, Qt based, desktop toolset
 * https://lxqt.org
 *
 * Copyright: 2014 LXQt team
 * Authors:
 *   Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
 *
 * This program or library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * END_COMMON_COPYRIGHT_HEADER */

#include "ossengine.h"
#include "audiodevice.h"
#include <QDebug>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <cerrno>

#if defined(__FreeBSD__) || defined(__NetBSD__)
#include <sys/soundcard.h>
#elif defined(__linux__) || defined(__Linux__)
#include <linux/soundcard.h>
#else
#error "Not supported platform"
#endif

OssEngine::OssEngine(QObject *parent) :
    AudioEngine(parent),
    m_mixer(-1),
    m_device(nullptr),
    m_leftVolume(0),
    m_rightVolume(0)
{
    qDebug() << "OssEngine";
    initMixer();
}

OssEngine::~OssEngine()
{
    if(m_mixer >= 0)
        close(m_mixer);
}

void OssEngine::initMixer() {
    m_mixer = open ("/dev/mixer", O_RDWR, 0);
    if (m_mixer < 0) {
      qDebug() << "/dev/mixer cannot be opened";
      return;
    }
    qDebug() << "InitMixer:" << m_mixer;

    m_device = new AudioDevice(Sink, this);
    m_device->setName(QStringLiteral("Master"));
    m_device->setIndex(0);
    m_device->setDescription(QStringLiteral("Master Volume"));
    m_device->setMuteNoCommit(false);
    updateVolume();

    m_sinks.append(m_device);
    emit sinkListChanged();
}

void OssEngine::updateVolume() {
    if(m_mixer < 0 || !m_device)
        return;
    int volumes;
    if(ioctl(m_mixer, MIXER_READ(SOUND_MIXER_VOLUME), &volumes) < 0) {
        qDebug() << "updateVolume() failed" << errno;
    }
    m_leftVolume = volumes & 0xff; // left
    m_rightVolume = (volumes >> 8) & 0xff; // right
    qDebug() << "volume:" << m_leftVolume << m_rightVolume;

    m_device->setVolumeNoCommit(m_leftVolume);
}

void OssEngine::setVolume(int volume) {
    if(m_mixer < 0)
        return;
    int volumes = (volume << 8) + volume;
    if(ioctl(m_mixer, MIXER_WRITE(SOUND_MIXER_VOLUME), &volumes) < 0) {
        qDebug() << "setVolume() failed" << errno;
    }
    else {
        qDebug() << "setVolume()" << volume;
    }
}

void OssEngine::commitDeviceVolume(AudioDevice *device)
{
    if (!device)
        return;
    setVolume(device->volume());
}

void OssEngine::setMute(AudioDevice * /*device*/, bool state)
{
  if(state)
      setVolume(0);
  else
      setVolume(m_leftVolume);
}

void OssEngine::setIgnoreMaxVolume(bool /*ignore*/)
{
  // TODO
}