summaryrefslogtreecommitdiff
path: root/src/model
diff options
context:
space:
mode:
Diffstat (limited to 'src/model')
-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
4 files changed, 330 insertions, 0 deletions
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;
+};