drumstick  2.9.0
C++ MIDI libraries using Qt objects, idioms, and style.
alsatimer.h
Go to the documentation of this file.
1 /*
2  MIDI Sequencer C++ library
3  Copyright (C) 2006-2023, 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 3 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 extern "C" {
23  #include <alsa/asoundlib.h>
24 }
25 
26 #include <QObject>
27 #include <QList>
28 #include <QThread>
29 #include <QReadWriteLock>
30 #include <QPointer>
31 #include "macros.h"
32 
33 namespace drumstick { namespace ALSA {
34 
40 #if defined(DRUMSTICK_STATIC)
41 #define DRUMSTICK_ALSA_EXPORT
42 #else
43 #if defined(drumstick_alsa_EXPORTS)
44 #define DRUMSTICK_ALSA_EXPORT Q_DECL_EXPORT
45 #else
46 #define DRUMSTICK_ALSA_EXPORT Q_DECL_IMPORT
47 #endif
48 #endif
49 
50 class TimerQuery;
51 class TimerId;
52 class TimerGlobalInfo;
53 
63 class DRUMSTICK_ALSA_EXPORT TimerInfo
64 {
65  friend class Timer;
66 
67 public:
68  TimerInfo();
69  TimerInfo(const TimerInfo& other);
70  explicit TimerInfo(const snd_timer_info_t* other);
71  virtual ~TimerInfo();
72  TimerInfo* clone();
73  TimerInfo& operator=(const TimerInfo& other);
74  int getSizeOfInfo() const;
75 
76  bool isSlave();
77  int getCard();
78  QString getId();
79  QString getName();
80  long getResolution();
81  long getFrequency();
82 
83 protected:
84  Q_DECL_DEPRECATED long getTicks();
85 
86 private:
87  snd_timer_info_t *m_Info;
88 };
89 
95 class DRUMSTICK_ALSA_EXPORT TimerId
96 {
97  friend class TimerQuery;
98  friend class TimerGlobalInfo;
99  friend class QueueTimer;
100 
101 public:
102  TimerId();
103  TimerId(const TimerId& other);
104  explicit TimerId(const snd_timer_id_t *other);
105  TimerId(int cls, int scls, int card, int dev, int sdev);
106  virtual ~TimerId();
107  TimerId* clone();
108  TimerId& operator=(const TimerId& other);
109  int getSizeOfInfo() const;
110 
111  void setClass(int devclass);
112  int getClass();
113  void setSlaveClass(int devsclass);
114  int getSlaveClass();
115  void setCard(int card);
116  int getCard();
117  void setDevice(int device);
118  int getDevice();
119  void setSubdevice(int subdevice);
120  int getSubdevice();
121 
122 private:
123  snd_timer_id_t *m_Info;
124 };
125 
129 typedef QList<TimerId> TimerIdList;
130 
136 class DRUMSTICK_ALSA_EXPORT TimerGlobalInfo
137 {
138  friend class TimerQuery;
139 
140 public:
141  TimerGlobalInfo();
142  TimerGlobalInfo(const TimerGlobalInfo& other);
143  explicit TimerGlobalInfo(const snd_timer_ginfo_t* other);
144  virtual ~TimerGlobalInfo();
145  TimerGlobalInfo* clone();
146  TimerGlobalInfo& operator=(const TimerGlobalInfo& other);
147  int getSizeOfInfo() const;
148 
149  void setTimerId(const TimerId& tid);
150  TimerId& getTimerId();
151  unsigned int getFlags();
152  int getCard();
153  QString getId();
154  QString getName();
155  unsigned long getResolution();
156  unsigned long getMinResolution();
157  unsigned long getMaxResolution();
158  unsigned int getClients();
159 
160 private:
161  snd_timer_ginfo_t* m_Info;
162  TimerId m_Id;
163 };
164 
170 class DRUMSTICK_ALSA_EXPORT TimerQuery
171 {
172 public:
173  TimerQuery(const QString& deviceName, int openMode);
174  TimerQuery(const QString& deviceName, int openMode, snd_config_t* conf);
175  virtual ~TimerQuery();
180  TimerIdList getTimers() const { return m_timers; }
181  TimerGlobalInfo& getGlobalInfo();
182  void setGlobalParams(snd_timer_gparams_t* params);
183  void getGlobalParams(snd_timer_gparams_t* params);
184  void getGlobalStatus(snd_timer_gstatus_t* status);
185 
186 protected:
187  void readTimers();
188  void freeTimers();
189 
190 private:
191  snd_timer_query_t *m_Info;
192  TimerIdList m_timers;
193  TimerGlobalInfo m_GlobalInfo;
194 };
195 
201 class DRUMSTICK_ALSA_EXPORT TimerParams
202 {
203  friend class Timer;
204 
205 public:
206  TimerParams();
207  TimerParams(const TimerParams& other);
208  explicit TimerParams(const snd_timer_params_t* other);
209  virtual ~TimerParams();
210  TimerParams* clone();
211  TimerParams& operator=(const TimerParams& other);
212  int getSizeOfInfo() const;
213 
214  void setAutoStart(bool auto_start);
215  bool getAutoStart();
216  void setExclusive(bool exclusive);
217  bool getExclusive();
218  void setEarlyEvent(bool early_event);
219  bool getEarlyEvent();
220  void setTicks(long ticks);
221  long getTicks();
222  void setQueueSize(long queue_size);
223  long getQueueSize();
224  void setFilter(unsigned int filter);
225  unsigned int getFilter();
226 
227 private:
228  snd_timer_params_t* m_Info;
229 };
230 
236 class DRUMSTICK_ALSA_EXPORT TimerStatus
237 {
238  friend class Timer;
239 
240 public:
241  TimerStatus();
242  TimerStatus(const TimerStatus& other);
243  explicit TimerStatus(const snd_timer_status_t* other);
244  virtual ~TimerStatus();
245  TimerStatus* clone();
246  TimerStatus& operator=(const TimerStatus& other);
247  int getSizeOfInfo() const;
248 
249  snd_htimestamp_t getTimestamp();
250  long getResolution();
251  long getLost();
252  long getOverrun();
253  long getQueue();
254 
255 private:
256  snd_timer_status_t* m_Info;
257 };
258 
265 class DRUMSTICK_ALSA_EXPORT TimerEventHandler
266 {
267 public:
269  virtual ~TimerEventHandler() = default;
275  virtual void handleTimerEvent(int ticks, int msecs) = 0;
276 };
277 
283 class DRUMSTICK_ALSA_EXPORT Timer : public QObject
284 {
285  Q_OBJECT
286 
287 private:
291  class TimerInputThread : public QThread
292  {
293  public:
295  TimerInputThread(Timer* t, int timeout)
296  : QThread(),
297  m_timer(t),
298  m_Wait(timeout),
299  m_Stopped(false) {}
301  virtual ~TimerInputThread() = default;
302  void run() override;
303  bool stopped();
304  void stop();
305  private:
306  Timer* m_timer;
307  int m_Wait;
308  bool m_Stopped;
309  QReadWriteLock m_mutex;
310  };
311 
312 public:
313  Timer(int cls, int scls, int card, int dev, int sdev, int openMode, QObject* parent = nullptr);
314  Timer(const QString& deviceName, int openMode, QObject* parent = nullptr);
315  Timer(const QString& deviceName, int openMode, snd_config_t* config, QObject* parent = nullptr);
316  Timer(TimerId& id, int openMode, QObject* parent = nullptr);
317  virtual ~Timer();
318 
319  static TimerId bestGlobalTimerId();
320  static Timer* bestGlobalTimer(int openMode, QObject* parent = nullptr);
325  snd_timer_t* getHandle() { return m_Info; }
326  TimerInfo& getTimerInfo();
327  TimerStatus& getTimerStatus();
328  void setTimerParams(const TimerParams& params);
329 
330  void start();
331  void stop();
332  void continueRunning();
333 
334  void addAsyncTimerHandler(snd_async_callback_t callback, void *private_data);
335  int getPollDescriptorsCount();
336  void pollDescriptors(struct pollfd *pfds, unsigned int space);
337  void pollDescriptorsRevents(struct pollfd *pfds, unsigned int nfds, unsigned short *revents);
338  ssize_t read(void *buffer, size_t size);
339  snd_timer_t* getTimerHandle();
344  void setHandler(TimerEventHandler* h) { m_handler = h; }
345  void startEvents();
346  void stopEvents();
347 
348 protected:
349  void doEvents();
350 
351 Q_SIGNALS:
359  void timerExpired(int ticks, int msecs);
360 
361 private:
362  snd_timer_t *m_Info;
363  snd_async_handler_t *m_asyncHandler;
364  TimerEventHandler* m_handler;
365  QPointer<TimerInputThread> m_thread;
366  TimerInfo m_TimerInfo;
367  TimerStatus m_TimerStatus;
368  QString m_deviceName;
369  snd_htimestamp_t m_last_time;
370 };
371 
374 }} /* namespace drumstick::ALSA */
375 
376 #endif /* DRUMSTICK_ALSATIMER_H */
The QObject class is the base class of all Qt objects.
The QThread class provides platform-independent threads.
Queue timer container.
Definition: alsaqueue.h:170
ALSA Timer events handler.
Definition: alsatimer.h:266
virtual void handleTimerEvent(int ticks, int msecs)=0
Timer event handler.
virtual ~TimerEventHandler()=default
Destructor.
Global timer information container.
Definition: alsatimer.h:137
ALSA Timer identifier container.
Definition: alsatimer.h:96
ALSA Timer information container.
Definition: alsatimer.h:64
ALSA Timer parameters container.
Definition: alsatimer.h:202
ALSA Timer inquiry helper.
Definition: alsatimer.h:171
TimerIdList getTimers() const
Gets the list of available timers.
Definition: alsatimer.h:180
ALSA Timer status container.
Definition: alsatimer.h:237
ALSA Timer management.
Definition: alsatimer.h:284
void timerExpired(int ticks, int msecs)
This signal is emitted when the timer has expired, if there is not an event hander installed.
void setHandler(TimerEventHandler *h)
Sets an event handler providing a method to be called when a timer expires.
Definition: alsatimer.h:344
snd_timer_t * getHandle()
Gets the ALSA timer object.
Definition: alsatimer.h:325
QList< TimerId > TimerIdList
List of timer identifiers.
Definition: alsatimer.h:129
Drumstick common.
Definition: alsaclient.cpp:68