drumstick  0.5.0
alsatimer.h
Go to the documentation of this file.
1 /*
2  MIDI Sequencer C++ library
3  Copyright (C) 2006-2010, Pedro Lopez-Cabanillas <plcl@users.sf.net>
4 
5  This library is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 #ifndef DRUMSTICK_ALSATIMER_H
21 #define DRUMSTICK_ALSATIMER_H
22 
23 #include "drumstickcommon.h"
24 #include <QList>
25 #include <QThread>
26 #include <QReadWriteLock>
27 #include <QPointer>
28 
36 namespace drumstick {
37 
38 class TimerQuery;
39 class TimerId;
40 class TimerGlobalInfo;
41 
47 class DRUMSTICK_EXPORT TimerInfo
48 {
49  friend class Timer;
50 
51 public:
52  TimerInfo();
53  TimerInfo(const TimerInfo& other);
54  TimerInfo(const snd_timer_info_t* other);
55  virtual ~TimerInfo();
56  TimerInfo* clone();
57  TimerInfo& operator=(const TimerInfo& other);
58  int getSizeOfInfo() const;
59 
60  bool isSlave();
61  int getCard();
62  QString getId();
63  QString getName();
64  long getResolution();
65  long getFrequency();
66 
67 protected:
68  long getTicks() __attribute__((deprecated));
69 
70 private:
71  snd_timer_info_t *m_Info;
72 };
73 
79 class DRUMSTICK_EXPORT TimerId
80 {
81  friend class TimerQuery;
82  friend class TimerGlobalInfo;
83  friend class QueueTimer;
84 
85 public:
86  TimerId();
87  TimerId(const TimerId& other);
88  TimerId(const snd_timer_id_t *other);
89  TimerId(int cls, int scls, int card, int dev, int sdev);
90  virtual ~TimerId();
91  TimerId* clone();
92  TimerId& operator=(const TimerId& other);
93  int getSizeOfInfo() const;
94 
95  void setClass(int devclass);
96  int getClass();
97  void setSlaveClass(int devsclass);
98  int getSlaveClass();
99  void setCard(int card);
100  int getCard();
101  void setDevice(int device);
102  int getDevice();
103  void setSubdevice(int subdevice);
104  int getSubdevice();
105 
106 private:
107  snd_timer_id_t *m_Info;
108 };
109 
113 typedef QList<TimerId> TimerIdList;
114 
120 class DRUMSTICK_EXPORT TimerGlobalInfo
121 {
122  friend class TimerQuery;
123 
124 public:
125  TimerGlobalInfo();
126  TimerGlobalInfo(const TimerGlobalInfo& other);
127  TimerGlobalInfo(const snd_timer_ginfo_t* other);
128  virtual ~TimerGlobalInfo();
129  TimerGlobalInfo* clone();
130  TimerGlobalInfo& operator=(const TimerGlobalInfo& other);
131  int getSizeOfInfo() const;
132 
133  void setTimerId(const TimerId& tid);
134  TimerId& getTimerId();
135  unsigned int getFlags();
136  int getCard();
137  QString getId();
138  QString getName();
139  unsigned long getResolution();
140  unsigned long getMinResolution();
141  unsigned long getMaxResolution();
142  unsigned int getClients();
143 
144 private:
145  snd_timer_ginfo_t* m_Info;
146  TimerId m_Id;
147 };
148 
154 class DRUMSTICK_EXPORT TimerQuery
155 {
156 public:
157  TimerQuery(const QString& deviceName, int openMode);
158  TimerQuery(const QString& deviceName, int openMode, snd_config_t* conf);
159  virtual ~TimerQuery();
164  TimerIdList getTimers() const { return m_timers; }
165  TimerGlobalInfo& getGlobalInfo();
166  void setGlobalParams(snd_timer_gparams_t* params);
167  void getGlobalParams(snd_timer_gparams_t* params);
168  void getGlobalStatus(snd_timer_gstatus_t* status);
169 
170 protected:
171  void readTimers();
172  void freeTimers();
173 
174 private:
175  snd_timer_query_t *m_Info;
176  TimerIdList m_timers;
177  TimerGlobalInfo m_GlobalInfo;
178 };
179 
185 class DRUMSTICK_EXPORT TimerParams
186 {
187  friend class Timer;
188 
189 public:
190  TimerParams();
191  TimerParams(const TimerParams& other);
192  TimerParams(const snd_timer_params_t* other);
193  virtual ~TimerParams();
194  TimerParams* clone();
195  TimerParams& operator=(const TimerParams& other);
196  int getSizeOfInfo() const;
197 
198  void setAutoStart(bool auto_start);
199  bool getAutoStart();
200  void setExclusive(bool exclusive);
201  bool getExclusive();
202  void setEarlyEvent(bool early_event);
203  bool getEarlyEvent();
204  void setTicks(long ticks);
205  long getTicks();
206  void setQueueSize(long queue_size);
207  long getQueueSize();
208  void setFilter(unsigned int filter);
209  unsigned int getFilter();
210 
211 private:
212  snd_timer_params_t* m_Info;
213 };
214 
220 class DRUMSTICK_EXPORT TimerStatus
221 {
222  friend class Timer;
223 
224 public:
225  TimerStatus();
226  TimerStatus(const TimerStatus& other);
227  TimerStatus(const snd_timer_status_t* other);
228  virtual ~TimerStatus();
229  TimerStatus* clone();
230  TimerStatus& operator=(const TimerStatus& other);
231  int getSizeOfInfo() const;
232 
233  snd_htimestamp_t getTimestamp();
234  long getResolution();
235  long getLost();
236  long getOverrun();
237  long getQueue();
238 
239 private:
240  snd_timer_status_t* m_Info;
241 };
242 
249 class DRUMSTICK_EXPORT TimerEventHandler
250 {
251 public:
253  virtual ~TimerEventHandler() {}
259  virtual void handleTimerEvent(int ticks, int msecs) = 0;
260 };
261 
267 class DRUMSTICK_EXPORT Timer : public QObject
268 {
269  Q_OBJECT
270 
271 private:
275  class TimerInputThread : public QThread
276  {
277  public:
279  TimerInputThread(Timer* t, int timeout)
280  : QThread(),
281  m_timer(t),
282  m_Wait(timeout),
283  m_Stopped(false) {}
285  virtual ~TimerInputThread() {}
286  virtual void run();
287  bool stopped();
288  void stop();
289  private:
290  Timer* m_timer;
291  int m_Wait;
292  bool m_Stopped;
293  QReadWriteLock m_mutex;
294  };
295 
296 public:
297  Timer(int cls, int scls, int card, int dev, int sdev, int openMode, QObject* parent = 0);
298  Timer(const QString& deviceName, int openMode, QObject* parent = 0);
299  Timer(const QString& deviceName, int openMode, snd_config_t* config, QObject* parent = 0);
300  Timer(TimerId& id, int openMode, QObject* parent = 0);
301  virtual ~Timer();
302 
303  static TimerId bestGlobalTimerId();
304  static Timer* bestGlobalTimer(int openMode, QObject* parent = 0);
309  snd_timer_t* getHandle() { return m_Info; }
310  TimerInfo& getTimerInfo();
311  TimerStatus& getTimerStatus();
312  void setTimerParams(const TimerParams& params);
313 
314  void start();
315  void stop();
316  void continueRunning();
317 
318  void addAsyncTimerHandler(snd_async_callback_t callback, void *private_data);
319  int getPollDescriptorsCount();
320  void pollDescriptors(struct pollfd *pfds, unsigned int space);
321  void pollDescriptorsRevents(struct pollfd *pfds, unsigned int nfds, unsigned short *revents);
322  ssize_t read(void *buffer, size_t size);
323  snd_timer_t* getTimerHandle();
328  void setHandler(TimerEventHandler* h) { m_handler = h; }
329  void startEvents();
330  void stopEvents();
331 
332 protected:
333  void doEvents();
334 
335 signals:
343  void timerExpired(int ticks, int msecs);
344 
345 private:
346  snd_timer_t *m_Info;
347  snd_async_handler_t *m_asyncHandler;
348  TimerEventHandler* m_handler;
349  QPointer<TimerInputThread> m_thread;
350  TimerInfo m_TimerInfo;
351  TimerStatus m_TimerStatus;
352  QString m_deviceName;
353  snd_htimestamp_t m_last_time;
354 };
355 
356 } /* namespace drumstick */
357 
360 #endif /* DRUMSTICK_ALSATIMER_H */
TimerIdList getTimers() const
Gets the list of available timers.
Definition: alsatimer.h:164
ALSA Timer management.
Definition: alsatimer.h:267
virtual ~TimerEventHandler()
Destructor.
Definition: alsatimer.h:253
The QObject class is the base class of all Qt objects.
void setHandler(TimerEventHandler *h)
Sets an event handler providing a method to be called when a timer expires.
Definition: alsatimer.h:328
snd_timer_t * getHandle()
Gets the ALSA timer object.
Definition: alsatimer.h:309
Queue timer container.
Definition: alsaqueue.h:157
ALSA Timer information container.
Definition: alsatimer.h:47
ALSA Timer status container.
Definition: alsatimer.h:220
ALSA Timer parameters container.
Definition: alsatimer.h:185
Common functionality.
Global timer information container.
Definition: alsatimer.h:120
ALSA Timer events handler.
Definition: alsatimer.h:249
QList< TimerId > TimerIdList
List of timer identifiers.
Definition: alsatimer.h:113
ALSA Timer inquiry helper.
Definition: alsatimer.h:154
The QThread class provides platform-independent threads.
ALSA Timer identifier container.
Definition: alsatimer.h:79