drumstick 0.5.0
alsatimer.h
Go to the documentation of this file.
00001 /*
00002     MIDI Sequencer C++ library 
00003     Copyright (C) 2006-2010, Pedro Lopez-Cabanillas <plcl@users.sf.net>
00004  
00005     This library is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009  
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014  
00015     You should have received a copy of the GNU General Public License along 
00016     with this program; if not, write to the Free Software Foundation, Inc., 
00017     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.    
00018 */
00019 
00020 #ifndef DRUMSTICK_ALSATIMER_H
00021 #define DRUMSTICK_ALSATIMER_H
00022 
00023 #include "drumstickcommon.h"
00024 #include <QList>
00025 #include <QThread>
00026 #include <QReadWriteLock>
00027 #include <QPointer>
00028 
00036 namespace drumstick {
00037 
00038 class TimerQuery;
00039 class TimerId;
00040 class TimerGlobalInfo;
00041 
00047 class DRUMSTICK_EXPORT TimerInfo
00048 {
00049     friend class Timer;
00050 
00051 public:
00052     TimerInfo();
00053     TimerInfo(const TimerInfo& other);
00054     TimerInfo(const snd_timer_info_t* other);
00055     virtual ~TimerInfo();
00056     TimerInfo* clone();
00057     TimerInfo& operator=(const TimerInfo& other);
00058     int getSizeOfInfo() const;
00059     
00060     bool isSlave();
00061     int getCard();
00062     QString getId();
00063     QString getName();
00064     long getResolution();
00065     long getFrequency();
00066 
00067 protected:
00068     long getTicks() __attribute__((deprecated));
00069     
00070 private:
00071     snd_timer_info_t *m_Info;
00072 };
00073 
00079 class DRUMSTICK_EXPORT TimerId
00080 {
00081     friend class TimerQuery;
00082     friend class TimerGlobalInfo;
00083     friend class QueueTimer;
00084 
00085 public:
00086     TimerId();
00087     TimerId(const TimerId& other);
00088     TimerId(const snd_timer_id_t *other);
00089     TimerId(int cls, int scls, int card, int dev, int sdev);
00090     virtual ~TimerId();
00091     TimerId* clone();
00092     TimerId& operator=(const TimerId& other);
00093     int getSizeOfInfo() const;
00094 
00095     void setClass(int devclass);
00096     int getClass();
00097     void setSlaveClass(int devsclass);
00098     int getSlaveClass();
00099     void setCard(int card);
00100     int getCard();
00101     void setDevice(int device);
00102     int getDevice();
00103     void setSubdevice(int subdevice);
00104     int getSubdevice();
00105 
00106 private:
00107     snd_timer_id_t *m_Info;
00108 };
00109 
00113 typedef QList<TimerId> TimerIdList;
00114 
00120 class DRUMSTICK_EXPORT TimerGlobalInfo
00121 {
00122     friend class TimerQuery;
00123 
00124 public:
00125     TimerGlobalInfo();
00126     TimerGlobalInfo(const TimerGlobalInfo& other);
00127     TimerGlobalInfo(const snd_timer_ginfo_t* other);
00128     virtual ~TimerGlobalInfo();
00129     TimerGlobalInfo* clone();
00130     TimerGlobalInfo& operator=(const TimerGlobalInfo& other);
00131     int getSizeOfInfo() const;
00132 
00133     void setTimerId(const TimerId& tid);
00134     TimerId& getTimerId();
00135     unsigned int getFlags();
00136     int getCard();
00137     QString getId();
00138     QString getName();
00139     unsigned long getResolution();
00140     unsigned long getMinResolution();
00141     unsigned long getMaxResolution();
00142     unsigned int getClients();
00143 
00144 private:
00145     snd_timer_ginfo_t* m_Info;
00146     TimerId m_Id;
00147 };
00148 
00154 class DRUMSTICK_EXPORT TimerQuery
00155 {
00156 public:
00157     TimerQuery(const QString& deviceName, int openMode);
00158     TimerQuery(const QString& deviceName, int openMode, snd_config_t* conf);
00159     virtual ~TimerQuery();
00164     TimerIdList getTimers() const { return m_timers; }
00165     TimerGlobalInfo& getGlobalInfo();
00166     void setGlobalParams(snd_timer_gparams_t* params);
00167     void getGlobalParams(snd_timer_gparams_t* params);
00168     void getGlobalStatus(snd_timer_gstatus_t* status);
00169 
00170 protected:
00171     void readTimers();
00172     void freeTimers();
00173 
00174 private:
00175     snd_timer_query_t *m_Info;
00176     TimerIdList m_timers;
00177     TimerGlobalInfo m_GlobalInfo;
00178 };
00179 
00185 class DRUMSTICK_EXPORT TimerParams
00186 {
00187     friend class Timer;
00188 
00189 public:
00190     TimerParams();
00191     TimerParams(const TimerParams& other);
00192     TimerParams(const snd_timer_params_t* other);
00193     virtual ~TimerParams();
00194     TimerParams* clone();
00195     TimerParams& operator=(const TimerParams& other);
00196     int getSizeOfInfo() const;
00197 
00198     void setAutoStart(bool auto_start);
00199     bool getAutoStart();
00200     void setExclusive(bool exclusive);
00201     bool getExclusive();
00202     void setEarlyEvent(bool early_event);
00203     bool getEarlyEvent();
00204     void setTicks(long ticks);
00205     long getTicks();
00206     void setQueueSize(long queue_size);
00207     long getQueueSize();
00208     void setFilter(unsigned int filter);
00209     unsigned int getFilter();
00210 
00211 private:
00212     snd_timer_params_t* m_Info;
00213 };
00214 
00220 class DRUMSTICK_EXPORT TimerStatus
00221 {
00222     friend class Timer;
00223 
00224 public:
00225     TimerStatus();
00226     TimerStatus(const TimerStatus& other);
00227     TimerStatus(const snd_timer_status_t* other);
00228     virtual ~TimerStatus();
00229     TimerStatus* clone();
00230     TimerStatus& operator=(const TimerStatus& other);
00231     int getSizeOfInfo() const;
00232 
00233     snd_htimestamp_t getTimestamp();
00234     long getResolution();
00235     long getLost();
00236     long getOverrun();
00237     long getQueue();
00238 
00239 private:
00240     snd_timer_status_t* m_Info;
00241 };
00242 
00249 class DRUMSTICK_EXPORT TimerEventHandler
00250 {
00251 public:
00253     virtual ~TimerEventHandler() {}
00259     virtual void handleTimerEvent(int ticks, int msecs) = 0;
00260 };
00261 
00267 class DRUMSTICK_EXPORT Timer : public QObject
00268 {
00269     Q_OBJECT
00270     
00271 private:
00275     class TimerInputThread : public QThread
00276     {
00277     public: 
00279        TimerInputThread(Timer* t, int timeout)
00280            : QThread(),
00281            m_timer(t),
00282            m_Wait(timeout),
00283            m_Stopped(false) {}
00285        virtual ~TimerInputThread() {}
00286        virtual void run();
00287        bool stopped();
00288        void stop();       
00289     private:
00290         Timer* m_timer;
00291         int m_Wait;
00292         bool m_Stopped;
00293         QReadWriteLock m_mutex;        
00294     };
00295 
00296 public:
00297     Timer(int cls, int scls, int card, int dev, int sdev, int openMode, QObject* parent = 0);
00298     Timer(const QString& deviceName, int openMode, QObject* parent = 0);
00299     Timer(const QString& deviceName, int openMode, snd_config_t* config, QObject* parent = 0);
00300     Timer(TimerId& id, int openMode, QObject* parent = 0);
00301     virtual ~Timer();
00302     
00303     static TimerId bestGlobalTimerId();
00304     static Timer* bestGlobalTimer(int openMode, QObject* parent = 0);
00309     snd_timer_t* getHandle() { return m_Info; }
00310     TimerInfo& getTimerInfo();
00311     TimerStatus& getTimerStatus();
00312     void setTimerParams(const TimerParams& params);
00313     
00314     void start();
00315     void stop();
00316     void continueRunning();
00317 
00318     void addAsyncTimerHandler(snd_async_callback_t callback, void *private_data);
00319     int getPollDescriptorsCount();
00320     void pollDescriptors(struct pollfd *pfds, unsigned int space);
00321     void pollDescriptorsRevents(struct pollfd *pfds, unsigned int nfds, unsigned short *revents);
00322     ssize_t read(void *buffer, size_t size);
00323     snd_timer_t* getTimerHandle();
00328     void setHandler(TimerEventHandler* h) { m_handler = h; }
00329     void startEvents();
00330     void stopEvents();
00331     
00332 protected:
00333     void doEvents();
00334 
00335 signals:    
00343     void timerExpired(int ticks, int msecs);
00344 
00345 private:
00346     snd_timer_t *m_Info;
00347     snd_async_handler_t *m_asyncHandler;
00348     TimerEventHandler* m_handler;
00349     QPointer<TimerInputThread> m_thread;
00350     TimerInfo m_TimerInfo;
00351     TimerStatus m_TimerStatus;
00352     QString m_deviceName;
00353     snd_htimestamp_t m_last_time;
00354 };
00355 
00356 } /* namespace drumstick */
00357 
00360 #endif /* DRUMSTICK_ALSATIMER_H */