diff options
| author | Calvin Morrison <calvin@pobox.com> | 2026-05-17 23:38:18 -0400 |
|---|---|---|
| committer | Calvin Morrison <calvin@pobox.com> | 2026-05-17 23:38:18 -0400 |
| commit | 3909a06fe6786027c83e3adaef89c343cde4fbe3 (patch) | |
| tree | f5ea87c98b50764c8ad87e598f274319df0e74db /src/model | |
| parent | 311b69c14203860ab6d98bd11633eea287c8f04e (diff) | |
ALSA backend, backend switcher, UX fixesalsa-wip
- ALSA backend: AlsaModel, AlsaDevice, AlsaMixerWindow, AlsaTray, AlsaPopup
- BackendController: live pulse↔alsa switching, preserves window position/visibility
- PA shutdown fix: stop mainloop thread before disconnect/free to prevent crash
- Exit crash fix: BackendController deleted explicitly before TQApplication exits
- Waiting-for-PulseAudio placeholder when PA not connected
- Preferences: live backend stack (Pulse/ALSA), Startup/Exit card with autostart,
Recording card, no-tabs greying, remove stale "requires restart" label
- Recording tray: track source outputs directly; fix LED init in popup;
level meter forwarding for monitor-source recordings via m_monitorSourceToSink
- TmixTray: fix config group General→Tray for scroll/mute/popup settings;
RecordingTray suppresses right-click menu
- audiodevice.h: add recordingActive() virtual accessor
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/alsadevice.cpp | 187 | ||||
| -rw-r--r-- | src/model/alsadevice.h | 67 | ||||
| -rw-r--r-- | src/model/alsamodel.cpp | 150 | ||||
| -rw-r--r-- | src/model/alsamodel.h | 54 | ||||
| -rw-r--r-- | src/model/audiodevice.h | 1 | ||||
| -rw-r--r-- | src/model/pulsedevice.h | 7 | ||||
| -rw-r--r-- | src/model/pulsemodel.cpp | 54 | ||||
| -rw-r--r-- | src/model/pulsemodel.h | 1 |
8 files changed, 500 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/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 de91831..24c1af8 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()) ); @@ -586,6 +585,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 ); @@ -619,6 +624,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; @@ -661,14 +668,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; |
