From 65c7ea9fbf4886fa3d6127c5fa699520f30f386c Mon Sep 17 00:00:00 2001 From: Calvin Morrison Date: Mon, 18 May 2026 00:00:17 -0400 Subject: OSS backend for FreeBSD support Adds a complete OSS audio backend alongside PulseAudio and ALSA: - OssDevice/OssModel: enumerate /dev/mixer*, poll for changes at 500ms - Full UI layer: mixer window, system tray, mini popup, per-channel widgets - Fake mute (saves/restores volume since OSS has no mute bit) - BackendController and PreferencesDlg updated; backend combo now uses m_backendIds list so any combination of compiled-in backends works Co-Authored-By: Claude Sonnet 4.6 --- src/model/ossdevice.cpp | 89 ++++++++++++++++++++++++++++++ src/model/ossdevice.h | 51 ++++++++++++++++++ src/model/ossmodel.cpp | 140 ++++++++++++++++++++++++++++++++++++++++++++++++ src/model/ossmodel.h | 50 +++++++++++++++++ 4 files changed, 330 insertions(+) create mode 100644 src/model/ossdevice.cpp create mode 100644 src/model/ossdevice.h create mode 100644 src/model/ossmodel.cpp create mode 100644 src/model/ossmodel.h (limited to 'src/model') 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 +#include +#include +#include + +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 +#include + +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 +#include +#include +#include +#include +#include +#include +#include + +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 it(m_devices); *it; ++it ) + (*it)->syncFromOss(); +} + +void OssModel::close() +{ + if ( m_timer ) m_timer->stop(); + m_devices.clear(); + m_cards.clear(); + for ( TQValueListIterator it = m_fds.begin(); it != m_fds.end(); ++it ) + ::close(*it); + m_fds.clear(); +} + +TQPtrList OssModel::devicesForCard( int cardIdx ) const +{ + TQPtrList result; + for ( TQPtrListIterator it(m_devices); *it; ++it ) + if ( (*it)->cardIndex() == cardIdx ) + result.append(*it); + return result; +} + +OssDevice *OssModel::defaultOutput() const +{ + for ( TQPtrListIterator it(m_devices); *it; ++it ) + if ( (*it)->category() == OssDevice::Output && (*it)->name() == "vol" ) + return *it; + for ( TQPtrListIterator 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 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 +#include +#include +#include + +#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 cards() const { return m_cards; } + TQPtrList devices() const { return m_devices; } + TQPtrList 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 m_cards; + TQPtrList m_devices; + TQValueList m_fds; // one open fd per card, owned here + TQTimer *m_timer; +}; -- cgit v1.2.3