#ifndef __EXTB_IRSEND__H
#define __EXTB_IRSEND__H

#include <vector>
#define __STL_CONFIG_H
#include <vdr/thread.h>

///////////////////////////////////////////////////////////////////////////////
// Want more debugstuff comment out follow line
//#define MOREDEBUGMSG
///////////////////////////////////////////////////////////////////////////////
#ifdef MOREDEBUGMSG 
 #define moredsyslog(a...) dsyslog(a...)
#else 
 #define moredsyslog(a...) void() 
#endif
///////////////////////////////////////////////////////////////////////////////






struct cLircQueue
 : public std::vector<char*>
{
  virtual ~cLircQueue()
  {
    iterator i = begin();
    const_iterator e = end();
    for(;e!=i;++i)
        free(*i);
    clear();
  }

  inline size_t max_size() const 
  {
    return 32;
  }

  inline bool add(char* szCmd)
  {
    if(size()<max_size())
    {
      push_back(szCmd);
      return true;
    }  
    return false;
  }
  
};

class cLirc
  : private cThread
{
  char* m_szDevice;
  int m_fd;
  static volatile bool m_bTimeout;

  volatile bool m_bShutdown;
  mutable cMutex    m_Mutex;
  cCondVar  m_condVar;
  cLircQueue m_queue;
  volatile unsigned int m_nQueued;
protected:  
  static void sigalrm(int sig);
  const char *read_string(void);
  int send_packet(const char *packet);

  virtual void Action(void);

  inline bool reopen(void) { return open(m_szDevice);}
public:
  cLirc();
  virtual ~cLirc();
  
  bool open(const char* szDevice);
  inline bool isopen() const { return -1 != m_fd; }
  void close(void);
  void send(const char *cmd);
  void sendmore(const char *cmd);

  void flush();
};


class cLircPtr
{
protected:
  cLirc* m_pLirc;
public:  
  cLircPtr(cLirc* pLirc)
  :m_pLirc(pLirc) { }
};
#endif
