drumstick  1.1.3
alsatimer.h
Go to the documentation of this file.
1 /*
2  MIDI Sequencer C++ library
3  Copyright (C) 2006-2019, 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
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef DRUMSTICK_ALSATIMER_H
20 #define DRUMSTICK_ALSATIMER_H
21 
22 #include "drumstickcommon.h"
23 #include <QList>
24 #include <QThread>
25 #include <QReadWriteLock>
26 #include <QPointer>
27 
35 namespace drumstick {
36 
37 class TimerQuery;
38 class TimerId;
39 class TimerGlobalInfo;
40 
46 class DRUMSTICK_EXPORT TimerInfo
47 {
48  friend class Timer;
49 
50 public:
51  TimerInfo();
52  TimerInfo(const TimerInfo& other);
53  TimerInfo(const snd_timer_info_t* other);
54  virtual ~TimerInfo();
55  TimerInfo* clone();
56  TimerInfo& operator=(const TimerInfo& other);
57  int getSizeOfInfo() const;
58 
59  bool isSlave();
60  int getCard();
61  QString getId();
62  QString getName();
63  long getResolution();
64  long getFrequency();
65 
66 protected:
67  long getTicks() __attribute__((deprecated));
68 
69 private:
70  snd_timer_info_t *m_Info;
71 };
72 
78 class DRUMSTICK_EXPORT TimerId
79 {
80  friend class TimerQuery;
81  friend class TimerGlobalInfo;
82  friend class QueueTimer;
83 
84 public:
85  TimerId();
86  TimerId(const TimerId& other);
87  TimerId(const snd_timer_id_t *other);
88  TimerId(int cls, int scls, int card, int dev, int sdev);
89  virtual ~TimerId();
90  TimerId* clone();
91  TimerId& operator=(const TimerId& other);
92  int getSizeOfInfo() const;
93 
94  void setClass(int devclass);
95  int getClass();
96  void setSlaveClass(int devsclass);
97  int getSlaveClass();
98  void setCard(int card);
99  int getCard();
100  void setDevice(int device);
101  int getDevice();
102  void setSubdevice(int subdevice);
103  int getSubdevice();
104 
105 private:
106  snd_timer_id_t *m_Info;
107 };
108 
112 typedef QList<TimerId> TimerIdList;
113 
119 class DRUMSTICK_EXPORT TimerGlobalInfo
120 {
121  friend class TimerQuery;
122 
123 public:
124  TimerGlobalInfo();
125  TimerGlobalInfo(const TimerGlobalInfo& other);
126  TimerGlobalInfo(const snd_timer_ginfo_t* other);
127  virtual ~TimerGlobalInfo();
128  TimerGlobalInfo* clone();
129  TimerGlobalInfo& operator=(const TimerGlobalInfo& other);
130  int getSizeOfInfo() const;
131 
132  void setTimerId(const TimerId& tid);
133  TimerId& getTimerId();
134  unsigned int getFlags();
135  int getCard();
136  QString getId();
137  QString getName();
138  unsigned long getResolution();
139  unsigned long getMinResolution();
140  unsigned long getMaxResolution();
141  unsigned int getClients();
142 
143 private:
144  snd_timer_ginfo_t* m_Info;
145  TimerId m_Id;
146 };
147 
153 class DRUMSTICK_EXPORT TimerQuery
154 {
155 public:
156  TimerQuery(const QString& deviceName, int openMode);
157  TimerQuery(const QString& deviceName, int openMode, snd_config_t* conf);
158  virtual ~TimerQuery();
163  TimerIdList getTimers() const { return m_timers; }
164  TimerGlobalInfo& getGlobalInfo();
165  void setGlobalParams(snd_timer_gparams_t* params);
166  void getGlobalParams(snd_timer_gparams_t* params);
167  void getGlobalStatus(snd_timer_gstatus_t* status);
168 
169 protected:
170  void readTimers();
171  void freeTimers();
172 
173 private:
174  snd_timer_query_t *m_Info;
175  TimerIdList m_timers;
176  TimerGlobalInfo m_GlobalInfo;
177 };
178 
184 class DRUMSTICK_EXPORT TimerParams
185 {
186  friend class Timer;
187 
188 public:
189  TimerParams();
190  TimerParams(const TimerParams& other);
191  TimerParams(const snd_timer_params_t* other);
192  virtual ~TimerParams();
193  TimerParams* clone();
194  TimerParams& operator=(const TimerParams& other);
195  int getSizeOfInfo() const;
196 
197  void setAutoStart(bool auto_start);
198  bool getAutoStart();
199  void setExclusive(bool exclusive);
200  bool getExclusive();
201  void setEarlyEvent(bool early_event);
202  bool getEarlyEvent();
203  void setTicks(long ticks);
204  long getTicks();
205  void setQueueSize(long queue_size);
206  long getQueueSize();
207  void setFilter(unsigned int filter);
208  unsigned int getFilter();
209 
210 private:
211  snd_timer_params_t* m_Info;
212 };
213 
219 class DRUMSTICK_EXPORT TimerStatus
220 {
221  friend class Timer;
222 
223 public:
224  TimerStatus();
225  TimerStatus(const TimerStatus& other);
226  TimerStatus(const snd_timer_status_t* other);
227  virtual ~TimerStatus();
228  TimerStatus* clone();
229  TimerStatus& operator=(const TimerStatus& other);
230  int getSizeOfInfo() const;
231 
232  snd_htimestamp_t getTimestamp();
233  long getResolution();
234  long getLost();
235  long getOverrun();
236  long getQueue();
237 
238 private:
239  snd_timer_status_t* m_Info;
240 };
241 
248 class DRUMSTICK_EXPORT TimerEventHandler
249 {
250 public:
252  virtual ~TimerEventHandler() {}
258  virtual void handleTimerEvent(int ticks, int msecs) = 0;
259 };
260 
266 class DRUMSTICK_EXPORT Timer : public QObject
267 {
268  Q_OBJECT
269 
270 private:
274  class TimerInputThread : public QThread
275  {
276  public:
278  TimerInputThread(Timer* t, int timeout)
279  : QThread(),
280  m_timer(t),
281  m_Wait(timeout),
282  m_Stopped(false) {}
284  virtual ~TimerInputThread() {}
285  virtual void run();
286  bool stopped();
287  void stop();
288  private:
289  Timer* m_timer;
290  int m_Wait;
291  bool m_Stopped;
292  QReadWriteLock m_mutex;
293  };
294 
295 public:
296  Timer(int cls, int scls, int card, int dev, int sdev, int openMode, QObject* parent = 0);
297  Timer(const QString& deviceName, int openMode, QObject* parent = 0);
298  Timer(const QString& deviceName, int openMode, snd_config_t* config, QObject* parent = 0);
299  Timer(TimerId& id, int openMode, QObject* parent = 0);
300  virtual ~Timer();
301 
302  static TimerId bestGlobalTimerId();
303  static Timer* bestGlobalTimer(int openMode, QObject* parent = 0);
308  snd_timer_t* getHandle() { return m_Info; }
309  TimerInfo& getTimerInfo();
310  TimerStatus& getTimerStatus();
311  void setTimerParams(const TimerParams& params);
312 
313  void start();
314  void stop();
315  void continueRunning();
316 
317  void addAsyncTimerHandler(snd_async_callback_t callback, void *private_data);
318  int getPollDescriptorsCount();
319  void pollDescriptors(struct pollfd *pfds, unsigned int space);
320  void pollDescriptorsRevents(struct pollfd *pfds, unsigned int nfds, unsigned short *revents);
321  ssize_t read(void *buffer, size_t size);
322  snd_timer_t* getTimerHandle();
327  void setHandler(TimerEventHandler* h) { m_handler = h; }
328  void startEvents();
329  void stopEvents();
330 
331 protected:
332  void doEvents();
333 
334 signals:
342  void timerExpired(int ticks, int msecs);
343 
344 private:
345  snd_timer_t *m_Info;
346  snd_async_handler_t *m_asyncHandler;
347  TimerEventHandler* m_handler;
348  QPointer<TimerInputThread> m_thread;
349  TimerInfo m_TimerInfo;
350  TimerStatus m_TimerStatus;
351  QString m_deviceName;
352  snd_htimestamp_t m_last_time;
353 };
354 
355 } /* namespace drumstick */
356 
359 #endif /* DRUMSTICK_ALSATIMER_H */
QObject
drumstick::Timer::getHandle
snd_timer_t * getHandle()
Gets the ALSA timer object.
Definition: alsatimer.h:308
drumstick::TimerIdList
QList< TimerId > TimerIdList
List of timer identifiers.
Definition: alsatimer.h:112
drumstick::TimerGlobalInfo
Global timer information container.
Definition: alsatimer.h:119
drumstick::TimerStatus
ALSA Timer status container.
Definition: alsatimer.h:219
drumstick::TimerQuery::getTimers
TimerIdList getTimers() const
Gets the list of available timers.
Definition: alsatimer.h:163
drumstick::TimerParams
ALSA Timer parameters container.
Definition: alsatimer.h:184
drumstick::TimerId
ALSA Timer identifier container.
Definition: alsatimer.h:78
drumstick::TimerQuery
ALSA Timer inquiry helper.
Definition: alsatimer.h:153
drumstick::TimerEventHandler::~TimerEventHandler
virtual ~TimerEventHandler()
Destructor.
Definition: alsatimer.h:252
drumstick::TimerEventHandler
ALSA Timer events handler.
Definition: alsatimer.h:248
drumstick::QueueTimer
Queue timer container.
Definition: alsaqueue.h:156
drumstick::TimerInfo
ALSA Timer information container.
Definition: alsatimer.h:46
QThread
drumstick::Timer::setHandler
void setHandler(TimerEventHandler *h)
Sets an event handler providing a method to be called when a timer expires.
Definition: alsatimer.h:327
drumstickcommon.h
drumstick::Timer
ALSA Timer management.
Definition: alsatimer.h:266