#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;
};
