summaryrefslogtreecommitdiff
path: root/src/model/pulsedevice.cpp
blob: e0f84ab2185674f26a3c3beef9e52e28ca1989f7 (plain)
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
#include "pulsedevice.h"
#include "../ui/devicewidget.h"
#include <tqwidget.h>

PulseDevice::PulseDevice( AudioDevice::Category cat, uint32_t paIndex, TQObject *parent )
    : AudioDevice(parent),
      m_category(cat), m_paIndex(paIndex),
      m_volume(0), m_muted(false),
      m_context(0), m_mainloop(0)
{
}

void PulseDevice::setPAContext( pa_context *ctx, pa_threaded_mainloop *mainloop )
{
    m_context  = ctx;
    m_mainloop = mainloop;
}

void PulseDevice::update( const TQString &name, int volume, bool muted )
{
    bool nameChange   = ( name   != m_name   );
    bool volumeChange = ( volume != m_volume );
    bool muteChange   = ( muted  != m_muted  );

    m_name   = name;
    m_volume = volume;
    m_muted  = muted;

    if ( nameChange )   emit nameChanged( m_name );
    if ( volumeChange ) emit volumeChanged( m_volume );
    if ( muteChange )   emit muteChanged( m_muted );
}

// ---- write back to PA -------------------------------------------------------

static pa_volume_t percentToPA( int pct )
{
    if ( pct <= 0 )   return 0;
    if ( pct >= 100 ) return PA_VOLUME_NORM;
    return (pa_volume_t)( (double)pct / 100.0 * PA_VOLUME_NORM + 0.5 );
}

void PulseDevice::setVolume( int v )
{
    if ( !m_context ) return;
    v = v < 0 ? 0 : v > 100 ? 100 : v;

    pa_cvolume cv;
    pa_cvolume_set( &cv, 2, percentToPA(v) );

    pa_threaded_mainloop_lock( m_mainloop );
    pa_operation *op = 0;
    switch ( m_category ) {
        case Output:
            op = pa_context_set_sink_volume_by_index( m_context, m_paIndex, &cv, 0, 0 );
            break;
        case Input:
            op = pa_context_set_source_volume_by_index( m_context, m_paIndex, &cv, 0, 0 );
            break;
        case Playback:
            op = pa_context_set_sink_input_volume( m_context, m_paIndex, &cv, 0, 0 );
            break;
        case Recording:
            op = pa_context_set_source_output_volume( m_context, m_paIndex, &cv, 0, 0 );
            break;
    }
    if ( op ) pa_operation_unref( op );
    pa_threaded_mainloop_unlock( m_mainloop );
}

void PulseDevice::setMuted( bool m )
{
    if ( !m_context ) return;

    pa_threaded_mainloop_lock( m_mainloop );
    pa_operation *op = 0;
    int mute = m ? 1 : 0;
    switch ( m_category ) {
        case Output:
            op = pa_context_set_sink_mute_by_index( m_context, m_paIndex, mute, 0, 0 );
            break;
        case Input:
            op = pa_context_set_source_mute_by_index( m_context, m_paIndex, mute, 0, 0 );
            break;
        case Playback:
            op = pa_context_set_sink_input_mute( m_context, m_paIndex, mute, 0, 0 );
            break;
        case Recording:
            op = pa_context_set_source_output_mute( m_context, m_paIndex, mute, 0, 0 );
            break;
    }
    if ( op ) pa_operation_unref( op );
    pa_threaded_mainloop_unlock( m_mainloop );
}

TQWidget *PulseDevice::createWidget( TQWidget *parent )
{
    return new DeviceWidget( this, parent );
}

#include "pulsedevice.moc"