summaryrefslogtreecommitdiff
path: root/src/model
diff options
context:
space:
mode:
authorCalvin Morrison <calvin@pobox.com>2026-05-22 10:16:15 -0400
committerCalvin Morrison <calvin@pobox.com>2026-05-22 10:16:15 -0400
commitcdf72828e599a63fca4a0f1ceeb3145fba581aa3 (patch)
tree16ee02f0ce1e317a09fd4c2a3f9fbdc04aadfe60 /src/model
parentb30b234afa3ec18156e432b9f2a7ad2ca80cc672 (diff)
parentd9876849f0208291cc7010522e85593703ff52bd (diff)
Merge branch 'and-the-oss-you-rode-in-on'
Diffstat (limited to 'src/model')
-rw-r--r--src/model/alsadevice.cpp187
-rw-r--r--src/model/alsadevice.h67
-rw-r--r--src/model/alsamodel.cpp150
-rw-r--r--src/model/alsamodel.h54
-rw-r--r--src/model/audiodevice.h1
-rw-r--r--src/model/ossdevice.cpp89
-rw-r--r--src/model/ossdevice.h51
-rw-r--r--src/model/ossmodel.cpp140
-rw-r--r--src/model/ossmodel.h50
-rw-r--r--src/model/pulsedevice.h7
-rw-r--r--src/model/pulsemodel.cpp54
-rw-r--r--src/model/pulsemodel.h1
12 files changed, 830 insertions, 21 deletions
diff --git a/src/model/alsadevice.cpp b/src/model/alsadevice.cpp
new file mode 100644
index 0000000..4a03e7f
--- /dev/null
+++ b/src/model/alsadevice.cpp
@@ -0,0 +1,187 @@
+#include "alsadevice.h"
+#include <algorithm>
+#include <cmath>
+
+AlsaDevice::AlsaDevice( Category cat, int cardIndex, snd_mixer_elem_t *elem,
+ const TQString &cardName, TQObject *parent )
+ : TQObject(parent), m_category(cat), m_type(Slider), m_cardIndex(cardIndex),
+ m_elem(elem), m_cardName(cardName),
+ m_stereo(false), m_hasMute(false), m_volMin(0), m_volMax(0),
+ m_volume(0), m_muted(false), m_pan(0), m_enumId(0)
+{
+ m_name = TQString::fromUtf8( snd_mixer_selem_get_name(elem) );
+
+ if ( snd_mixer_selem_is_enumerated(elem) ) {
+ m_type = Enum;
+ int n = snd_mixer_selem_get_enum_items(elem);
+ for ( int i = 0; i < n; ++i ) {
+ char buf[128];
+ if ( snd_mixer_selem_get_enum_item_name(elem, i, sizeof(buf), buf) >= 0 )
+ m_enumValues.append( TQString::fromUtf8(buf) );
+ }
+ } else if ( snd_mixer_selem_has_playback_volume(elem) ||
+ snd_mixer_selem_has_capture_volume(elem) ) {
+ m_type = Slider;
+ if ( cat == Output ) {
+ m_stereo = snd_mixer_selem_has_playback_channel(elem, SND_MIXER_SCHN_FRONT_RIGHT);
+ m_hasMute = snd_mixer_selem_has_playback_switch(elem);
+ snd_mixer_selem_get_playback_volume_range(elem, &m_volMin, &m_volMax);
+ } else {
+ m_stereo = snd_mixer_selem_has_capture_channel(elem, SND_MIXER_SCHN_FRONT_RIGHT);
+ m_hasMute = snd_mixer_selem_has_capture_switch(elem);
+ snd_mixer_selem_get_capture_volume_range(elem, &m_volMin, &m_volMax);
+ }
+ } else {
+ m_type = Switch;
+ m_hasMute = true;
+ }
+
+ snd_mixer_elem_set_callback(elem, elemCallback);
+ snd_mixer_elem_set_callback_private(elem, this);
+
+ syncFromAlsa();
+}
+
+int AlsaDevice::elemCallback( snd_mixer_elem_t *elem, unsigned int /*mask*/ )
+{
+ AlsaDevice *self = static_cast<AlsaDevice*>( snd_mixer_elem_get_callback_private(elem) );
+ if ( self ) self->syncFromAlsa();
+ return 0;
+}
+
+void AlsaDevice::syncFromAlsa()
+{
+ if ( m_type == Enum ) {
+ unsigned int idx = 0;
+ snd_mixer_selem_get_enum_item(m_elem, SND_MIXER_SCHN_FRONT_LEFT, &idx);
+ int newId = (int)idx;
+ if ( newId != m_enumId ) { m_enumId = newId; emit enumChanged(m_enumId); }
+ return;
+ }
+
+ if ( m_type == Switch ) {
+ int sw = 1;
+ if ( m_category == Output && snd_mixer_selem_has_playback_switch(m_elem) )
+ snd_mixer_selem_get_playback_switch(m_elem, SND_MIXER_SCHN_FRONT_LEFT, &sw);
+ else if ( m_category == Input && snd_mixer_selem_has_capture_switch(m_elem) )
+ snd_mixer_selem_get_capture_switch(m_elem, SND_MIXER_SCHN_FRONT_LEFT, &sw);
+ else if ( snd_mixer_selem_has_playback_switch(m_elem) )
+ snd_mixer_selem_get_playback_switch(m_elem, SND_MIXER_SCHN_FRONT_LEFT, &sw);
+ bool newMute = (sw == 0);
+ if ( newMute != m_muted ) { m_muted = newMute; emit muteChanged(m_muted); }
+ return;
+ }
+
+ // Slider
+ long range = m_volMax - m_volMin;
+ if ( range <= 0 ) return;
+
+ long l = m_volMin, r = m_volMin;
+ int sw = 1;
+
+ if ( m_category == Output ) {
+ snd_mixer_selem_get_playback_volume(m_elem, SND_MIXER_SCHN_FRONT_LEFT, &l);
+ if ( m_stereo )
+ snd_mixer_selem_get_playback_volume(m_elem, SND_MIXER_SCHN_FRONT_RIGHT, &r);
+ else
+ r = l;
+ if ( m_hasMute )
+ snd_mixer_selem_get_playback_switch(m_elem, SND_MIXER_SCHN_FRONT_LEFT, &sw);
+ } else {
+ snd_mixer_selem_get_capture_volume(m_elem, SND_MIXER_SCHN_FRONT_LEFT, &l);
+ if ( m_stereo )
+ snd_mixer_selem_get_capture_volume(m_elem, SND_MIXER_SCHN_FRONT_RIGHT, &r);
+ else
+ r = l;
+ if ( m_hasMute )
+ snd_mixer_selem_get_capture_switch(m_elem, SND_MIXER_SCHN_FRONT_LEFT, &sw);
+ }
+
+ long lv = l - m_volMin, rv = r - m_volMin;
+ long mx = std::max(lv, rv);
+
+ int newVol = (int)std::lround( (double)mx / range * 100.0 );
+ bool newMute = (sw == 0);
+ int newPan = 0;
+ if ( m_stereo && mx > 0 ) {
+ if ( lv >= rv )
+ newPan = (int)std::lround( -50.0 * (1.0 - (double)rv / lv) );
+ else
+ newPan = (int)std::lround( 50.0 * (1.0 - (double)lv / rv) );
+ }
+
+ if ( newVol != m_volume ) { m_volume = newVol; emit volumeChanged(m_volume); }
+ if ( newMute != m_muted ) { m_muted = newMute; emit muteChanged(m_muted); }
+ if ( newPan != m_pan ) { m_pan = newPan; emit panChanged(m_pan); }
+}
+
+long AlsaDevice::toAlsaVol( int pct ) const
+{
+ if ( pct <= 0 ) return m_volMin;
+ if ( pct >= 100 ) return m_volMax;
+ return m_volMin + (long)std::lround( (double)pct / 100.0 * (m_volMax - m_volMin) );
+}
+
+void AlsaDevice::pushVolume()
+{
+ long vol = toAlsaVol( m_volume );
+ double lf = m_pan >= 0 ? (50.0 - m_pan) / 50.0 : 1.0;
+ double rf = m_pan <= 0 ? (50.0 + m_pan) / 50.0 : 1.0;
+ long lv = (long)(vol * lf + 0.5);
+ long rv = (long)(vol * rf + 0.5);
+
+ if ( m_category == Output ) {
+ snd_mixer_selem_set_playback_volume(m_elem, SND_MIXER_SCHN_FRONT_LEFT, lv);
+ if ( m_stereo )
+ snd_mixer_selem_set_playback_volume(m_elem, SND_MIXER_SCHN_FRONT_RIGHT, rv);
+ } else {
+ snd_mixer_selem_set_capture_volume(m_elem, SND_MIXER_SCHN_FRONT_LEFT, lv);
+ if ( m_stereo )
+ snd_mixer_selem_set_capture_volume(m_elem, SND_MIXER_SCHN_FRONT_RIGHT, rv);
+ }
+}
+
+void AlsaDevice::setVolume( int v )
+{
+ v = v < 0 ? 0 : v > 100 ? 100 : v;
+ m_volume = v;
+ emit volumeChanged(v);
+ pushVolume();
+}
+
+void AlsaDevice::setPan( int p )
+{
+ p = p < -50 ? -50 : p > 50 ? 50 : p;
+ m_pan = p;
+ emit panChanged(p);
+ pushVolume();
+}
+
+void AlsaDevice::setMuted( bool m )
+{
+ m_muted = m;
+ int sw = m ? 0 : 1;
+ if ( m_type == Switch || m_type == Slider ) {
+ if ( snd_mixer_selem_has_playback_switch(m_elem) )
+ snd_mixer_selem_set_playback_switch_all(m_elem, sw);
+ if ( snd_mixer_selem_has_capture_switch(m_elem) )
+ snd_mixer_selem_set_capture_switch_all(m_elem, sw);
+ }
+ emit muteChanged(m);
+}
+
+void AlsaDevice::setEnumId( int id )
+{
+ if ( m_type != Enum ) return;
+ if ( id < 0 || id >= (int)m_enumValues.count() ) return;
+ m_enumId = id;
+ emit enumChanged(id);
+ for ( int ch = 0; ch <= SND_MIXER_SCHN_LAST; ++ch ) {
+ snd_mixer_selem_channel_id_t chId = (snd_mixer_selem_channel_id_t)ch;
+ if ( snd_mixer_selem_has_playback_channel(m_elem, chId) ||
+ snd_mixer_selem_has_capture_channel(m_elem, chId) )
+ snd_mixer_selem_set_enum_item(m_elem, chId, (unsigned int)id);
+ }
+}
+
+#include "alsadevice.moc"
diff --git a/src/model/alsadevice.h b/src/model/alsadevice.h
new file mode 100644
index 0000000..cc26801
--- /dev/null
+++ b/src/model/alsadevice.h
@@ -0,0 +1,67 @@
+#pragma once
+
+#include <tqobject.h>
+#include <tqstring.h>
+#include <tqstringlist.h>
+#include <alsa/asoundlib.h>
+
+class AlsaDevice : public TQObject
+{
+ TQ_OBJECT
+public:
+ enum Category { Output, Input, Neutral };
+ enum Type { Slider, Switch, Enum };
+
+ AlsaDevice( Category cat, int cardIndex, snd_mixer_elem_t *elem,
+ const TQString &cardName, TQObject *parent = 0 );
+
+ TQString name() const { return m_name; }
+ TQString cardName() const { return m_cardName; }
+ int cardIndex() const { return m_cardIndex; }
+ Category category() const { return m_category; }
+ Type type() const { return m_type; }
+
+ // Slider
+ int volume() const { return m_volume; }
+ bool muted() const { return m_muted; }
+ int pan() const { return m_pan; }
+ bool stereo() const { return m_stereo; }
+ bool hasMute() const { return m_hasMute; }
+ void setVolume( int v );
+ void setMuted( bool m );
+ void setPan( int p );
+
+ // Enum
+ TQStringList enumValues() const { return m_enumValues; }
+ int enumId() const { return m_enumId; }
+ void setEnumId( int id );
+
+ void syncFromAlsa();
+ static int elemCallback( snd_mixer_elem_t *elem, unsigned int mask );
+
+signals:
+ void volumeChanged( int v );
+ void muteChanged( bool m );
+ void panChanged( int p );
+ void enumChanged( int id );
+
+private:
+ long toAlsaVol( int pct ) const;
+ void pushVolume();
+
+ Category m_category;
+ Type m_type;
+ int m_cardIndex;
+ snd_mixer_elem_t *m_elem;
+ TQString m_name;
+ TQString m_cardName;
+ bool m_stereo;
+ bool m_hasMute;
+ long m_volMin;
+ long m_volMax;
+ int m_volume;
+ bool m_muted;
+ int m_pan;
+ TQStringList m_enumValues;
+ int m_enumId;
+};
diff --git a/src/model/alsamodel.cpp b/src/model/alsamodel.cpp
new file mode 100644
index 0000000..ce3caec
--- /dev/null
+++ b/src/model/alsamodel.cpp
@@ -0,0 +1,150 @@
+#include "alsamodel.h"
+#include <tqsocketnotifier.h>
+#include <tdeconfig.h>
+#include <tdeglobal.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+AlsaModel::AlsaModel( TQObject *parent )
+ : TQObject(parent)
+{
+ m_devices.setAutoDelete(true);
+ m_notifiers.setAutoDelete(true);
+}
+
+AlsaModel::~AlsaModel()
+{
+ close();
+}
+
+bool AlsaModel::open()
+{
+ int card = -1;
+ while ( snd_card_next(&card) == 0 && card >= 0 )
+ openCard(card);
+ emit ready();
+ return !m_devices.isEmpty();
+}
+
+void AlsaModel::openCard( int card )
+{
+ char hwname[32];
+ snprintf(hwname, sizeof(hwname), "hw:%d", card);
+
+ snd_mixer_t *mixer = 0;
+ if ( snd_mixer_open(&mixer, 0) < 0 ) return;
+ if ( snd_mixer_attach(mixer, hwname) < 0 ) { snd_mixer_close(mixer); return; }
+ if ( snd_mixer_selem_register(mixer, 0, 0) < 0 ) { snd_mixer_close(mixer); return; }
+ if ( snd_mixer_load(mixer) < 0 ) { snd_mixer_close(mixer); return; }
+
+ char *cardName = 0;
+ snd_card_get_name(card, &cardName);
+ TQString qName = cardName ? TQString::fromUtf8(cardName) : TQString("Card %1").arg(card);
+ free(cardName);
+
+ AlsaCard ac;
+ ac.index = card;
+ ac.name = qName;
+ m_cards.append(ac);
+
+ for ( snd_mixer_elem_t *elem = snd_mixer_first_elem(mixer);
+ elem; elem = snd_mixer_elem_next(elem) ) {
+ if ( !snd_mixer_selem_is_active(elem) ) continue;
+ if ( snd_mixer_selem_is_enumerated(elem) )
+ addElement(card, qName, elem, AlsaDevice::Neutral);
+ else if ( snd_mixer_selem_has_capture_volume(elem) )
+ addElement(card, qName, elem, AlsaDevice::Input);
+ else if ( snd_mixer_selem_has_playback_volume(elem) )
+ addElement(card, qName, elem, AlsaDevice::Output);
+ else if ( snd_mixer_selem_has_playback_switch(elem) )
+ addElement(card, qName, elem, AlsaDevice::Output);
+ else if ( snd_mixer_selem_has_capture_switch(elem) )
+ addElement(card, qName, elem, AlsaDevice::Input);
+ else if ( snd_mixer_selem_has_common_switch(elem) )
+ addElement(card, qName, elem, AlsaDevice::Neutral);
+ }
+
+ int count = snd_mixer_poll_descriptors_count(mixer);
+ if ( count > 0 ) {
+ struct pollfd *fds = new struct pollfd[count];
+ snd_mixer_poll_descriptors(mixer, fds, count);
+ for ( int i = 0; i < count; i++ ) {
+ m_fdToMixer[fds[i].fd] = mixer;
+ TQSocketNotifier *sn = new TQSocketNotifier(fds[i].fd, TQSocketNotifier::Read, this);
+ connect(sn, TQ_SIGNAL(activated(int)), this, TQ_SLOT(onMixerFd(int)));
+ m_notifiers.append(sn);
+ }
+ delete[] fds;
+ }
+
+ m_mixers.append(mixer);
+}
+
+void AlsaModel::addElement( int card, const TQString &cardName,
+ snd_mixer_elem_t *elem, AlsaDevice::Category cat )
+{
+ AlsaDevice *dev = new AlsaDevice(cat, card, elem, cardName, this);
+ m_devices.append(dev);
+ emit deviceAdded(dev);
+}
+
+void AlsaModel::onMixerFd( int fd )
+{
+ snd_mixer_t *mixer = m_fdToMixer[fd];
+ if ( mixer ) snd_mixer_handle_events(mixer);
+}
+
+TQPtrList<AlsaDevice> AlsaModel::devicesForCard( int cardIndex ) const
+{
+ TQPtrList<AlsaDevice> result;
+ for ( TQPtrListIterator<AlsaDevice> it(m_devices); *it; ++it )
+ if ( (*it)->cardIndex() == cardIndex )
+ result.append(*it);
+ return result;
+}
+
+AlsaDevice *AlsaModel::defaultOutput() const
+{
+ for ( TQPtrListIterator<AlsaDevice> it(m_devices); *it; ++it )
+ if ( (*it)->category() == AlsaDevice::Output )
+ return *it;
+ return 0;
+}
+
+AlsaDevice *AlsaModel::preferredOutput() const
+{
+ TDEConfig *cfg = TDEGlobal::config();
+ cfg->setGroup("Tray");
+ int card = cfg->readNumEntry("TrayCard", -1);
+ TQString name = cfg->readEntry("TrayDevice", "");
+ if ( card >= 0 && !name.isEmpty() ) {
+ for ( TQPtrListIterator<AlsaDevice> it(m_devices); *it; ++it )
+ if ( (*it)->cardIndex() == card && (*it)->name() == name &&
+ (*it)->category() == AlsaDevice::Output )
+ return *it;
+ }
+ return defaultOutput();
+}
+
+void AlsaModel::setPreferredOutput( int cardIndex, const TQString &name )
+{
+ TDEConfig *cfg = TDEGlobal::config();
+ cfg->setGroup("Tray");
+ cfg->writeEntry("TrayCard", cardIndex);
+ cfg->writeEntry("TrayDevice", name);
+ cfg->sync();
+ emit preferredOutputChanged( preferredOutput() );
+}
+
+void AlsaModel::close()
+{
+ m_notifiers.clear();
+ m_devices.clear();
+ m_fdToMixer.clear();
+ for ( TQValueListIterator<snd_mixer_t*> it = m_mixers.begin(); it != m_mixers.end(); ++it )
+ snd_mixer_close(*it);
+ m_mixers.clear();
+ m_cards.clear();
+}
+
+#include "alsamodel.moc"
diff --git a/src/model/alsamodel.h b/src/model/alsamodel.h
new file mode 100644
index 0000000..fc8a0f0
--- /dev/null
+++ b/src/model/alsamodel.h
@@ -0,0 +1,54 @@
+#pragma once
+
+#include <tqobject.h>
+#include <tqptrlist.h>
+#include <tqvaluelist.h>
+#include <tqmap.h>
+#include <tqstring.h>
+#include <alsa/asoundlib.h>
+
+#include "alsadevice.h"
+
+class TQSocketNotifier;
+
+struct AlsaCard {
+ int index;
+ TQString name;
+};
+
+class AlsaModel : public TQObject
+{
+ TQ_OBJECT
+public:
+ explicit AlsaModel( TQObject *parent = 0 );
+ ~AlsaModel();
+
+ bool open();
+ void close();
+
+ TQValueList<AlsaCard> cards() const { return m_cards; }
+ TQPtrList<AlsaDevice> devices() const { return m_devices; }
+ TQPtrList<AlsaDevice> devicesForCard( int cardIndex ) const;
+ AlsaDevice *defaultOutput() const;
+ AlsaDevice *preferredOutput() const;
+ void setPreferredOutput( int cardIndex, const TQString &name );
+
+signals:
+ void deviceAdded( AlsaDevice *dev );
+ void preferredOutputChanged( AlsaDevice *dev );
+ void ready();
+
+private slots:
+ void onMixerFd( int fd );
+
+private:
+ void openCard( int card );
+ void addElement( int card, const TQString &cardName,
+ snd_mixer_elem_t *elem, AlsaDevice::Category cat );
+
+ TQValueList<AlsaCard> m_cards;
+ TQPtrList<AlsaDevice> m_devices;
+ TQPtrList<TQSocketNotifier> m_notifiers;
+ TQValueList<snd_mixer_t*> m_mixers;
+ TQMap<int, snd_mixer_t*> m_fdToMixer;
+};
diff --git a/src/model/audiodevice.h b/src/model/audiodevice.h
index 854ba35..051ae4a 100644
--- a/src/model/audiodevice.h
+++ b/src/model/audiodevice.h
@@ -27,6 +27,7 @@ public:
virtual int volume() const = 0; // 0-100
virtual bool muted() const = 0;
virtual int pan() const = 0; // -50 (left) .. 0 .. +50 (right)
+ virtual bool recordingActive() const = 0; // true when ≥1 stream is capturing from this input
virtual void setVolume( int v ) = 0;
virtual void setMuted( bool m ) = 0;
diff --git a/src/model/ossdevice.cpp b/src/model/ossdevice.cpp
new file mode 100644
index 0000000..6a3b70a
--- /dev/null
+++ b/src/model/ossdevice.cpp
@@ -0,0 +1,89 @@
+#include "ossdevice.h"
+#include <sys/soundcard.h>
+#include <sys/ioctl.h>
+#include <algorithm>
+#include <cmath>
+
+OssDevice::OssDevice( Category cat, int cardIdx, const TQString &cardPath,
+ int fd, int channelIdx, const TQString &name, bool stereo,
+ TQObject *parent )
+ : TQObject(parent), m_fd(fd), m_cardIdx(cardIdx), m_cardPath(cardPath),
+ m_channelIdx(channelIdx), m_name(name), m_category(cat),
+ m_stereo(stereo), m_volume(0), m_savedVol(50), m_muted(false), m_pan(0)
+{
+ syncFromOss();
+}
+
+void OssDevice::syncFromOss()
+{
+ if ( m_fd < 0 ) return;
+ int val = 0;
+ if ( ::ioctl(m_fd, MIXER_READ(m_channelIdx), &val) < 0 ) return;
+
+ int left = val & 0xff;
+ int right = (val >> 8) & 0xff;
+ int mx = std::max(left, right);
+
+ if ( m_muted ) return; // don't overwrite display while muted
+
+ int newVol = mx;
+ int newPan = 0;
+ if ( m_stereo && mx > 0 ) {
+ if ( left >= right )
+ newPan = (int)std::lround( -50.0 * (1.0 - (double)right / left) );
+ else
+ newPan = (int)std::lround( 50.0 * (1.0 - (double)left / right) );
+ }
+
+ if ( newVol != m_volume ) { m_volume = newVol; emit volumeChanged(m_volume); }
+ if ( newPan != m_pan ) { m_pan = newPan; emit panChanged(m_pan); }
+}
+
+void OssDevice::pushVolume()
+{
+ if ( m_fd < 0 ) return;
+ int vol = m_muted ? 0 : m_volume;
+ double lf = m_pan >= 0 ? (50.0 - m_pan) / 50.0 : 1.0;
+ double rf = m_pan <= 0 ? (50.0 + m_pan) / 50.0 : 1.0;
+ int lv = (int)(vol * lf + 0.5);
+ int rv = (int)(vol * rf + 0.5);
+ if ( lv > 100 ) lv = 100;
+ if ( rv > 100 ) rv = 100;
+ int val = lv | (rv << 8);
+ ::ioctl(m_fd, MIXER_WRITE(m_channelIdx), &val);
+}
+
+void OssDevice::setVolume( int v )
+{
+ v = v < 0 ? 0 : v > 100 ? 100 : v;
+ m_volume = v;
+ if ( !m_muted ) m_savedVol = v;
+ emit volumeChanged(v);
+ pushVolume();
+}
+
+void OssDevice::setMuted( bool m )
+{
+ if ( m == m_muted ) return;
+ if ( m ) {
+ if ( m_volume > 0 ) m_savedVol = m_volume;
+ m_muted = true;
+ pushVolume();
+ } else {
+ m_muted = false;
+ m_volume = m_savedVol;
+ pushVolume();
+ emit volumeChanged(m_volume);
+ }
+ emit muteChanged(m_muted);
+}
+
+void OssDevice::setPan( int p )
+{
+ p = p < -50 ? -50 : p > 50 ? 50 : p;
+ m_pan = p;
+ emit panChanged(p);
+ pushVolume();
+}
+
+#include "ossdevice.moc"
diff --git a/src/model/ossdevice.h b/src/model/ossdevice.h
new file mode 100644
index 0000000..66a0d1b
--- /dev/null
+++ b/src/model/ossdevice.h
@@ -0,0 +1,51 @@
+#pragma once
+
+#include <tqobject.h>
+#include <tqstring.h>
+
+class OssDevice : public TQObject
+{
+ TQ_OBJECT
+public:
+ enum Category { Output, Input };
+
+ OssDevice( Category cat, int cardIdx, const TQString &cardPath,
+ int fd, int channelIdx, const TQString &name, bool stereo,
+ TQObject *parent = 0 );
+
+ TQString name() const { return m_name; }
+ TQString cardPath() const { return m_cardPath; }
+ int cardIndex() const { return m_cardIdx; }
+ Category category() const { return m_category; }
+ int volume() const { return m_volume; }
+ bool muted() const { return m_muted; }
+ int pan() const { return m_pan; }
+ bool stereo() const { return m_stereo; }
+ bool hasMute() const { return true; }
+
+ void setVolume( int v );
+ void setMuted( bool m );
+ void setPan( int p );
+
+ void syncFromOss();
+
+signals:
+ void volumeChanged( int v );
+ void muteChanged( bool m );
+ void panChanged( int p );
+
+private:
+ void pushVolume();
+
+ int m_fd; // borrowed from OssModel — do not close
+ int m_cardIdx;
+ TQString m_cardPath;
+ int m_channelIdx;
+ TQString m_name;
+ Category m_category;
+ bool m_stereo;
+ int m_volume;
+ int m_savedVol; // pre-mute volume for fake-mute
+ bool m_muted;
+ int m_pan;
+};
diff --git a/src/model/ossmodel.cpp b/src/model/ossmodel.cpp
new file mode 100644
index 0000000..35aa842
--- /dev/null
+++ b/src/model/ossmodel.cpp
@@ -0,0 +1,140 @@
+#include "ossmodel.h"
+#include <sys/soundcard.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <tdeconfig.h>
+#include <tdeglobal.h>
+#include <tqtimer.h>
+
+static const char *ossChannelNames[] = SOUND_DEVICE_NAMES;
+
+OssModel::OssModel( TQObject *parent )
+ : TQObject(parent), m_timer(0)
+{
+ m_devices.setAutoDelete(true);
+ m_timer = new TQTimer(this);
+ connect(m_timer, TQ_SIGNAL(timeout()), this, TQ_SLOT(onPollTimer()));
+}
+
+OssModel::~OssModel()
+{
+ close();
+}
+
+bool OssModel::open()
+{
+ openCard(0, "/dev/mixer");
+ for ( int i = 1; i < 16; ++i ) {
+ char path[32];
+ snprintf(path, sizeof(path), "/dev/mixer%d", i);
+ int probe = ::open(path, O_RDONLY);
+ if ( probe < 0 ) break;
+ ::close(probe);
+ openCard(i, TQString::fromLocal8Bit(path));
+ }
+ if ( m_devices.isEmpty() ) return false;
+ m_timer->start(500);
+ emit ready();
+ return true;
+}
+
+void OssModel::openCard( int cardIdx, const TQString &path )
+{
+ int fd = ::open(path.local8Bit().data(), O_RDWR);
+ if ( fd < 0 ) return;
+
+ mixer_info info;
+ TQString cardName;
+ if ( ::ioctl(fd, SOUND_MIXER_INFO, &info) == 0 )
+ cardName = TQString::fromLocal8Bit(info.name);
+ else
+ cardName = path;
+
+ int devmask = 0, stmask = 0, recmask = 0;
+ ::ioctl(fd, SOUND_MIXER_READ_DEVMASK, &devmask);
+ ::ioctl(fd, SOUND_MIXER_READ_STEREODEVS, &stmask);
+ ::ioctl(fd, SOUND_MIXER_READ_RECMASK, &recmask);
+
+ OssCard card;
+ card.index = cardIdx;
+ card.path = path;
+ card.name = cardName;
+ m_cards.append(card);
+ m_fds.append(fd);
+
+ for ( int i = 0; i < SOUND_MIXER_NRDEVICES; ++i ) {
+ if ( !(devmask & (1 << i)) ) continue;
+ OssDevice::Category cat = (recmask & (1 << i))
+ ? OssDevice::Input : OssDevice::Output;
+ bool stereo = (stmask & (1 << i)) != 0;
+ TQString name = TQString::fromLocal8Bit(ossChannelNames[i]);
+ OssDevice *dev = new OssDevice(cat, cardIdx, path, fd, i, name, stereo, this);
+ m_devices.append(dev);
+ emit deviceAdded(dev);
+ }
+}
+
+void OssModel::onPollTimer()
+{
+ for ( TQPtrListIterator<OssDevice> it(m_devices); *it; ++it )
+ (*it)->syncFromOss();
+}
+
+void OssModel::close()
+{
+ if ( m_timer ) m_timer->stop();
+ m_devices.clear();
+ m_cards.clear();
+ for ( TQValueListIterator<int> it = m_fds.begin(); it != m_fds.end(); ++it )
+ ::close(*it);
+ m_fds.clear();
+}
+
+TQPtrList<OssDevice> OssModel::devicesForCard( int cardIdx ) const
+{
+ TQPtrList<OssDevice> result;
+ for ( TQPtrListIterator<OssDevice> it(m_devices); *it; ++it )
+ if ( (*it)->cardIndex() == cardIdx )
+ result.append(*it);
+ return result;
+}
+
+OssDevice *OssModel::defaultOutput() const
+{
+ for ( TQPtrListIterator<OssDevice> it(m_devices); *it; ++it )
+ if ( (*it)->category() == OssDevice::Output && (*it)->name() == "vol" )
+ return *it;
+ for ( TQPtrListIterator<OssDevice> it(m_devices); *it; ++it )
+ if ( (*it)->category() == OssDevice::Output )
+ return *it;
+ return 0;
+}
+
+OssDevice *OssModel::preferredOutput() const
+{
+ TDEConfig *cfg = TDEGlobal::config();
+ cfg->setGroup("OssTray");
+ int card = cfg->readNumEntry("TrayCard", -1);
+ TQString name = cfg->readEntry("TrayDevice", "");
+ if ( card >= 0 && !name.isEmpty() ) {
+ for ( TQPtrListIterator<OssDevice> it(m_devices); *it; ++it )
+ if ( (*it)->cardIndex() == card && (*it)->name() == name &&
+ (*it)->category() == OssDevice::Output )
+ return *it;
+ }
+ return defaultOutput();
+}
+
+void OssModel::setPreferredOutput( int cardIdx, const TQString &name )
+{
+ TDEConfig *cfg = TDEGlobal::config();
+ cfg->setGroup("OssTray");
+ cfg->writeEntry("TrayCard", cardIdx);
+ cfg->writeEntry("TrayDevice", name);
+ cfg->sync();
+ emit preferredOutputChanged( preferredOutput() );
+}
+
+#include "ossmodel.moc"
diff --git a/src/model/ossmodel.h b/src/model/ossmodel.h
new file mode 100644
index 0000000..8c3db8b
--- /dev/null
+++ b/src/model/ossmodel.h
@@ -0,0 +1,50 @@
+#pragma once
+
+#include <tqobject.h>
+#include <tqptrlist.h>
+#include <tqvaluelist.h>
+#include <tqstring.h>
+
+#include "ossdevice.h"
+
+class TQTimer;
+
+struct OssCard {
+ int index;
+ TQString path;
+ TQString name;
+};
+
+class OssModel : public TQObject
+{
+ TQ_OBJECT
+public:
+ explicit OssModel( TQObject *parent = 0 );
+ ~OssModel();
+
+ bool open();
+ void close();
+
+ TQValueList<OssCard> cards() const { return m_cards; }
+ TQPtrList<OssDevice> devices() const { return m_devices; }
+ TQPtrList<OssDevice> devicesForCard( int cardIdx ) const;
+ OssDevice *defaultOutput() const;
+ OssDevice *preferredOutput() const;
+ void setPreferredOutput( int cardIdx, const TQString &name );
+
+signals:
+ void deviceAdded( OssDevice *dev );
+ void preferredOutputChanged( OssDevice *dev );
+ void ready();
+
+private slots:
+ void onPollTimer();
+
+private:
+ void openCard( int cardIdx, const TQString &path );
+
+ TQValueList<OssCard> m_cards;
+ TQPtrList<OssDevice> m_devices;
+ TQValueList<int> m_fds; // one open fd per card, owned here
+ TQTimer *m_timer;
+};
diff --git a/src/model/pulsedevice.h b/src/model/pulsedevice.h
index 289f2ed..92e2ac9 100644
--- a/src/model/pulsedevice.h
+++ b/src/model/pulsedevice.h
@@ -16,9 +16,10 @@ public:
TQString name() const { return m_name; }
TQString iconName() const { return m_iconName; }
Category category() const { return m_category; }
- int volume() const { return m_volume; }
- bool muted() const { return m_muted; }
- int pan() const { return m_pan; }
+ int volume() const { return m_volume; }
+ bool muted() const { return m_muted; }
+ int pan() const { return m_pan; }
+ bool recordingActive() const { return m_recordingCount > 0; }
uint32_t paIndex() const { return m_paIndex; }
uint32_t sinkIndex() const { return m_sinkIndex; }
TQString monitorName() const { return m_monitorName; }
diff --git a/src/model/pulsemodel.cpp b/src/model/pulsemodel.cpp
index 8d03a6a..4e73473 100644
--- a/src/model/pulsemodel.cpp
+++ b/src/model/pulsemodel.cpp
@@ -32,7 +32,8 @@ struct PAEvent : public TQCustomEvent {
TQString monitorName;
TQString iconName;
TQString paName; // raw PA name (sinks/sources only, for default tracking)
- uint32_t parentIndex; // for Playback: parent sink PA index
+ uint32_t parentIndex; // for Playback: parent sink; for Recording: parent source
+ uint32_t monitorSourceIdx; // for Output: PA index of the monitor source
uint32_t cardIdx; // for Output/Input: owning card PA index
TQString activePortName;
TQValueList<PulseDevicePort> devPorts;
@@ -45,7 +46,7 @@ struct PAEvent : public TQCustomEvent {
: TQCustomEvent(PA_EVENT), kind(k), cat(c), paIndex(idx),
name(n), volume(vol), muted(m), channels(ch), pan(p),
monitorName(mon), iconName(icon), paName(pname), parentIndex(parent),
- cardIdx(PA_INVALID_INDEX) {}
+ monitorSourceIdx(PA_INVALID_INDEX), cardIdx(PA_INVALID_INDEX) {}
};
struct PACardEvent : public TQCustomEvent {
@@ -216,26 +217,22 @@ bool PulseModel::open()
void PulseModel::close()
{
- bool alreadyDead = false;
- if ( m_context ) {
- pa_context_state_t state = pa_context_get_state( m_context );
- alreadyDead = ( state == PA_CONTEXT_FAILED ||
- state == PA_CONTEXT_TERMINATED );
+ // Stop the PA thread first. After this returns the thread is dead and
+ // no callbacks can fire, so we can touch PA objects without locking.
+ if ( m_mainloop ) {
+ pa_threaded_mainloop_stop( m_mainloop );
}
- forEachDevice( [alreadyDead](PulseDevice *dev) { dev->detach( alreadyDead ); } );
+ // With the thread stopped, detach devices without locking.
+ forEachDevice( [](PulseDevice *dev) { dev->detach( true ); } );
if ( m_context ) {
- if ( !alreadyDead ) {
- pa_threaded_mainloop_lock( m_mainloop );
- pa_context_disconnect( m_context );
- pa_threaded_mainloop_unlock( m_mainloop );
- }
+ pa_context_set_state_callback( m_context, 0, 0 );
+ pa_context_disconnect( m_context );
pa_context_unref( m_context );
m_context = 0;
}
if ( m_mainloop ) {
- pa_threaded_mainloop_stop( m_mainloop );
pa_threaded_mainloop_free( m_mainloop );
m_mainloop = 0;
}
@@ -324,7 +321,8 @@ void PulseModel::sinkInfoCb( pa_context *, const pa_sink_info *info, int eol, vo
TQString::fromUtf8( pa_proplist_gets( info->proplist, PA_PROP_DEVICE_ICON_NAME ) ? : "" ),
PA_INVALID_INDEX,
TQString::fromUtf8( info->name ) );
- ev->cardIdx = info->card;
+ ev->cardIdx = info->card;
+ ev->monitorSourceIdx = info->monitor_source;
ev->activePortName = info->active_port
? TQString::fromUtf8( info->active_port->name ) : TQString();
for ( uint32_t i = 0; i < info->n_ports; ++i ) {
@@ -515,6 +513,7 @@ void PulseModel::customEvent( TQCustomEvent *e )
m_sinksByName.clear();
m_sourcesByName.clear();
m_sourceOutputToSource.clear();
+ m_monitorSourceToSink.clear();
m_cards.clear();
close();
TQTimer::singleShot( 2000, this, TQ_SLOT(reconnect()) );
@@ -585,6 +584,12 @@ void PulseModel::customEvent( TQCustomEvent *e )
m_sinkCard.remove( ev->paIndex );
m_sinkActivePort.remove( ev->paIndex );
m_sinkPorts.remove( ev->paIndex );
+ // Remove monitor→sink mapping for this sink.
+ TQMap<uint32_t,uint32_t>::Iterator mi = m_monitorSourceToSink.begin();
+ while ( mi != m_monitorSourceToSink.end() ) {
+ if ( mi.data() == ev->paIndex ) { m_monitorSourceToSink.remove(mi); break; }
+ ++mi;
+ }
} else if ( ev->cat == AudioDevice::Input ) {
m_sourceCard.remove( ev->paIndex );
m_sourceActivePort.remove( ev->paIndex );
@@ -618,6 +623,8 @@ void PulseModel::customEvent( TQCustomEvent *e )
if ( ev->cat == AudioDevice::Output ) {
m_sinkCard[ev->paIndex] = ev->cardIdx;
m_sinkPorts[ev->paIndex] = ev->devPorts;
+ if ( ev->monitorSourceIdx != PA_INVALID_INDEX )
+ m_monitorSourceToSink[ev->monitorSourceIdx] = ev->paIndex;
TQString oldPort = m_sinkActivePort.contains(ev->paIndex)
? m_sinkActivePort[ev->paIndex] : TQString();
m_sinkActivePort[ev->paIndex] = ev->activePortName;
@@ -660,14 +667,25 @@ void PulseModel::customEvent( TQCustomEvent *e )
emit defaultOutputChanged( dev );
if ( ev->cat == AudioDevice::Input && ev->paName == m_defaultSourceName )
emit defaultInputChanged( dev );
- // Track which source a new recording stream is attached to.
+ // Forward Recording device level from its parent source (best-effort).
if ( ev->cat == AudioDevice::Recording && ev->parentIndex != PA_INVALID_INDEX ) {
m_sourceOutputToSource.insert( ev->paIndex, ev->parentIndex );
PulseDevice *src = findDevice( m_sources, ev->parentIndex );
if ( src ) {
+ // Parent is a real hardware input (microphone).
src->adjustRecordingCount( +1 );
- connect( src, TQ_SIGNAL(levelChanged(float)),
- dev, TQ_SIGNAL(levelChanged(float)) );
+ connect( src, TQ_SIGNAL(levelChanged(float)),
+ dev, TQ_SIGNAL(levelChanged(float)) );
+ } else {
+ // Parent may be a monitor source — forward the parent sink's level.
+ TQMap<uint32_t,uint32_t>::ConstIterator si =
+ m_monitorSourceToSink.find( ev->parentIndex );
+ if ( si != m_monitorSourceToSink.end() ) {
+ PulseDevice *sink = findDevice( m_sinks, si.data() );
+ if ( sink )
+ connect( sink, TQ_SIGNAL(levelChanged(float)),
+ dev, TQ_SIGNAL(levelChanged(float)) );
+ }
}
}
}
diff --git a/src/model/pulsemodel.h b/src/model/pulsemodel.h
index 2adfee7..befdd52 100644
--- a/src/model/pulsemodel.h
+++ b/src/model/pulsemodel.h
@@ -140,6 +140,7 @@ private:
TQString m_defaultSinkName;
TQString m_defaultSourceName;
TQMap<uint32_t, uint32_t> m_sourceOutputToSource; // soIdx → sourceIdx
+ TQMap<uint32_t, uint32_t> m_monitorSourceToSink; // monitorSourceIdx → sinkIdx
TQValueList<PulseCardInfo> m_cards;