#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();

    TQString lastError() const { return m_lastError; }

    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;
    TQString              m_lastError;
};
