From 3909a06fe6786027c83e3adaef89c343cde4fbe3 Mon Sep 17 00:00:00 2001 From: Calvin Morrison Date: Sun, 17 May 2026 23:38:18 -0400 Subject: ALSA backend, backend switcher, UX fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- src/model/pulsemodel.cpp | 54 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 18 deletions(-) (limited to 'src/model/pulsemodel.cpp') 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 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::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::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)) ); + } } } } -- cgit v1.2.3