drumstick 2.10.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-2024, 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
22extern "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
33namespace drumstick { namespace ALSA {
34
39
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
50class TimerQuery;
51class TimerId;
52class TimerGlobalInfo;
53
63class DRUMSTICK_ALSA_EXPORT TimerInfo
64{
65 friend class Timer;
66
67public:
68 TimerInfo();
69 TimerInfo(const TimerInfo& other);
70 explicit TimerInfo(const snd_timer_info_t* other);
71 virtual ~TimerInfo();
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
83protected:
84 Q_DECL_DEPRECATED long getTicks();
85
86private:
87 snd_timer_info_t *m_Info;
88};
89
95class DRUMSTICK_ALSA_EXPORT TimerId
96{
97 friend class TimerQuery;
98 friend class TimerGlobalInfo;
99 friend class QueueTimer;
100
101public:
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
122private:
123 snd_timer_id_t *m_Info;
124};
125
129typedef QList<TimerId> TimerIdList;
130
136class DRUMSTICK_ALSA_EXPORT TimerGlobalInfo
137{
138 friend class TimerQuery;
139
140public:
142 TimerGlobalInfo(const TimerGlobalInfo& other);
143 explicit TimerGlobalInfo(const snd_timer_ginfo_t* other);
144 virtual ~TimerGlobalInfo();
147 int getSizeOfInfo() const;
148
149 void setTimerId(const TimerId& tid);
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
160private:
161 snd_timer_ginfo_t* m_Info;
162 TimerId m_Id;
163};
164
170class DRUMSTICK_ALSA_EXPORT TimerQuery
171{
172public:
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
186protected:
187 void readTimers();
188 void freeTimers();
189
190private:
191 snd_timer_query_t *m_Info;
192 TimerIdList m_timers;
193 TimerGlobalInfo m_GlobalInfo;
194};
195
201class DRUMSTICK_ALSA_EXPORT TimerParams
202{
203 friend class Timer;
204
205public:
206 TimerParams();
207 TimerParams(const TimerParams& other);
208 explicit TimerParams(const snd_timer_params_t* other);
209 virtual ~TimerParams();
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
227private:
228 snd_timer_params_t* m_Info;
229};
230
236class DRUMSTICK_ALSA_EXPORT TimerStatus
237{
238 friend class Timer;
239
240public:
241 TimerStatus();
242 TimerStatus(const TimerStatus& other);
243 explicit TimerStatus(const snd_timer_status_t* other);
244 virtual ~TimerStatus();
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
255private:
256 snd_timer_status_t* m_Info;
257};
258
265class DRUMSTICK_ALSA_EXPORT TimerEventHandler
266{
267public:
269 virtual ~TimerEventHandler() = default;
275 virtual void handleTimerEvent(int ticks, int msecs) = 0;
276};
277
283class DRUMSTICK_ALSA_EXPORT Timer : public QObject
284{
285 Q_OBJECT
286
287private:
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
312public:
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
348protected:
349 void doEvents();
350
351Q_SIGNALS:
359 void timerExpired(int ticks, int msecs);
360
361private:
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
373
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.
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
unsigned int getFlags()
Gets the flags.
TimerGlobalInfo()
Default constructor.
unsigned int getClients()
Gets current timer clients.
int getSizeOfInfo() const
Gets the size of the ALSA timer global info object.
TimerGlobalInfo & operator=(const TimerGlobalInfo &other)
Assignment operator.
QString getId()
Gets the timer ID string.
unsigned long getMinResolution()
Gets timer minimal resolution in ns.
void setTimerId(const TimerId &tid)
Sets the timer identifier.
TimerGlobalInfo * clone()
Copy the current object.
TimerId & getTimerId()
Gets the timer identifier.
unsigned long getMaxResolution()
Gets timer maximal resolution in ns.
QString getName()
Gets the timer name.
int getCard()
Gets the card number.
unsigned long getResolution()
Gets the timer resolution in ns.
ALSA Timer identifier container.
Definition alsatimer.h:96
int getDevice()
Gets the device number.
int getSlaveClass()
Gets the slave class.
int getSizeOfInfo() const
Gets the size of the ALSA timer ID object.
void setSubdevice(int subdevice)
Sets the subdevice number.
TimerId * clone()
Copy the object.
void setCard(int card)
Sets the card number.
int getClass()
Gets the class identifier.
TimerId & operator=(const TimerId &other)
Assignment operator.
int getSubdevice()
Gets the subdevice number.
void setClass(int devclass)
Set the class identifier.
void setSlaveClass(int devsclass)
Sets the Slave class.
int getCard()
Gets the card number.
void setDevice(int device)
Sets the device number.
ALSA Timer information container.
Definition alsatimer.h:64
int getSizeOfInfo() const
Gets the size of the ALSA timer info object.
bool isSlave()
Check if the timer is slave (depends on another device)
QString getId()
Gets the string identifier.
long getResolution()
Gets the timer resolution (timer period in nanoseconds)
QString getName()
Gets the timer name.
Q_DECL_DEPRECATED long getTicks()
Gets the maximum timer ticks.
TimerInfo & operator=(const TimerInfo &other)
Assignment operator.
int getCard()
Gets the card number.
long getFrequency()
Gets the timer frequency in Hz.
TimerInfo * clone()
Copy the current object.
ALSA Timer parameters container.
Definition alsatimer.h:202
void setFilter(unsigned int filter)
Sets the event filter.
void setEarlyEvent(bool early_event)
Sets the timer early event.
int getSizeOfInfo() const
Gets the size of the ALSA timer parameters object.
TimerParams & operator=(const TimerParams &other)
Assignment operator.
void setAutoStart(bool auto_start)
Sets the automatic start flag.
bool getExclusive()
Gets the timer's exclusive flag.
long getQueueSize()
Gets the queue size.
TimerParams()
Default constructor.
bool getEarlyEvent()
Gets the timer early event.
void setExclusive(bool exclusive)
Sets the exclusive flag.
bool getAutoStart()
Gets the automatic start flag.
void setTicks(long ticks)
Sets the timer ticks.
long getTicks()
Gets the timer ticks.
TimerParams * clone()
Copy the current object.
unsigned int getFilter()
Gets the event filter.
void setQueueSize(long queue_size)
Sets the queue size (32-1024)
ALSA Timer inquiry helper.
Definition alsatimer.h:171
TimerIdList getTimers() const
Gets the list of available timers.
Definition alsatimer.h:180
TimerQuery(const QString &deviceName, int openMode)
Constructor.
ALSA Timer status container.
Definition alsatimer.h:237
long getLost()
Gets the master tick lost count.
TimerStatus()
Default constructor.
int getSizeOfInfo() const
Gets the size of the ALSA timer status object.
long getOverrun()
Gets the overrun count.
long getResolution()
Gets the resolution in us.
TimerStatus & operator=(const TimerStatus &other)
Assignment operator.
TimerStatus * clone()
Copy the current object.
snd_htimestamp_t getTimestamp()
Gets the high resolution time-stamp.
long getQueue()
Gets the count of used queue elements.
Timer(int cls, int scls, int card, int dev, int sdev, int openMode, QObject *parent=nullptr)
Constructor.
static TimerId bestGlobalTimerId()
Check and return the best available global TimerId in the system, meaning the timer with higher frequ...
void timerExpired(int ticks, int msecs)
This signal is emitted when the timer has expired, if there is not an event hander installed.
static Timer * bestGlobalTimer(int openMode, QObject *parent=nullptr)
Check and return the best available global Timer in the system, meaning the timer with higher frequen...
void setHandler(TimerEventHandler *h)
Sets an event handler providing a method to be called when a timer expires.
Definition alsatimer.h:344
void stop()
Stop rolling the timer.
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 ALSA library wrapper.
Drumstick common.