drumstick 2.0.0
alsatimer.h
Go to the documentation of this file.
1/*
2 MIDI Sequencer C++ library
3 Copyright (C) 2006-2020, 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
40class TimerQuery;
41class TimerId;
42class TimerGlobalInfo;
43
53class DRUMSTICK_EXPORT TimerInfo
54{
55 friend class Timer;
56
57public:
58 TimerInfo();
59 TimerInfo(const TimerInfo& other);
60 explicit TimerInfo(const snd_timer_info_t* other);
61 virtual ~TimerInfo();
63 TimerInfo& operator=(const TimerInfo& other);
64 int getSizeOfInfo() const;
65
66 bool isSlave();
67 int getCard();
68 QString getId();
69 QString getName();
70 long getResolution();
71 long getFrequency();
72
73protected:
74 long getTicks() __attribute__((deprecated));
75
76private:
77 snd_timer_info_t *m_Info;
78};
79
85class DRUMSTICK_EXPORT TimerId
86{
87 friend class TimerQuery;
88 friend class TimerGlobalInfo;
89 friend class QueueTimer;
90
91public:
92 TimerId();
93 TimerId(const TimerId& other);
94 explicit TimerId(const snd_timer_id_t *other);
95 TimerId(int cls, int scls, int card, int dev, int sdev);
96 virtual ~TimerId();
97 TimerId* clone();
98 TimerId& operator=(const TimerId& other);
99 int getSizeOfInfo() const;
100
101 void setClass(int devclass);
102 int getClass();
103 void setSlaveClass(int devsclass);
104 int getSlaveClass();
105 void setCard(int card);
106 int getCard();
107 void setDevice(int device);
108 int getDevice();
109 void setSubdevice(int subdevice);
110 int getSubdevice();
111
112private:
113 snd_timer_id_t *m_Info;
114};
115
119typedef QList<TimerId> TimerIdList;
120
126class DRUMSTICK_EXPORT TimerGlobalInfo
127{
128 friend class TimerQuery;
129
130public:
132 TimerGlobalInfo(const TimerGlobalInfo& other);
133 explicit TimerGlobalInfo(const snd_timer_ginfo_t* other);
134 virtual ~TimerGlobalInfo();
137 int getSizeOfInfo() const;
138
139 void setTimerId(const TimerId& tid);
141 unsigned int getFlags();
142 int getCard();
143 QString getId();
144 QString getName();
145 unsigned long getResolution();
146 unsigned long getMinResolution();
147 unsigned long getMaxResolution();
148 unsigned int getClients();
149
150private:
151 snd_timer_ginfo_t* m_Info;
152 TimerId m_Id;
153};
154
160class DRUMSTICK_EXPORT TimerQuery
161{
162public:
163 TimerQuery(const QString& deviceName, int openMode);
164 TimerQuery(const QString& deviceName, int openMode, snd_config_t* conf);
165 virtual ~TimerQuery();
170 TimerIdList getTimers() const { return m_timers; }
171 TimerGlobalInfo& getGlobalInfo();
172 void setGlobalParams(snd_timer_gparams_t* params);
173 void getGlobalParams(snd_timer_gparams_t* params);
174 void getGlobalStatus(snd_timer_gstatus_t* status);
175
176protected:
177 void readTimers();
178 void freeTimers();
179
180private:
181 snd_timer_query_t *m_Info;
182 TimerIdList m_timers;
183 TimerGlobalInfo m_GlobalInfo;
184};
185
191class DRUMSTICK_EXPORT TimerParams
192{
193 friend class Timer;
194
195public:
196 TimerParams();
197 TimerParams(const TimerParams& other);
198 explicit TimerParams(const snd_timer_params_t* other);
199 virtual ~TimerParams();
201 TimerParams& operator=(const TimerParams& other);
202 int getSizeOfInfo() const;
203
204 void setAutoStart(bool auto_start);
205 bool getAutoStart();
206 void setExclusive(bool exclusive);
207 bool getExclusive();
208 void setEarlyEvent(bool early_event);
209 bool getEarlyEvent();
210 void setTicks(long ticks);
211 long getTicks();
212 void setQueueSize(long queue_size);
213 long getQueueSize();
214 void setFilter(unsigned int filter);
215 unsigned int getFilter();
216
217private:
218 snd_timer_params_t* m_Info;
219};
220
226class DRUMSTICK_EXPORT TimerStatus
227{
228 friend class Timer;
229
230public:
231 TimerStatus();
232 TimerStatus(const TimerStatus& other);
233 explicit TimerStatus(const snd_timer_status_t* other);
234 virtual ~TimerStatus();
236 TimerStatus& operator=(const TimerStatus& other);
237 int getSizeOfInfo() const;
238
239 snd_htimestamp_t getTimestamp();
240 long getResolution();
241 long getLost();
242 long getOverrun();
243 long getQueue();
244
245private:
246 snd_timer_status_t* m_Info;
247};
248
255class DRUMSTICK_EXPORT TimerEventHandler
256{
257public:
259 virtual ~TimerEventHandler() = default;
265 virtual void handleTimerEvent(int ticks, int msecs) = 0;
266};
267
273class DRUMSTICK_EXPORT Timer : public QObject
274{
275 Q_OBJECT
276
277private:
281 class TimerInputThread : public QThread
282 {
283 public:
285 TimerInputThread(Timer* t, int timeout)
286 : QThread(),
287 m_timer(t),
288 m_Wait(timeout),
289 m_Stopped(false) {}
291 virtual ~TimerInputThread() = default;
292 void run() override;
293 bool stopped();
294 void stop();
295 private:
296 Timer* m_timer;
297 int m_Wait;
298 bool m_Stopped;
299 QReadWriteLock m_mutex;
300 };
301
302public:
303 Timer(int cls, int scls, int card, int dev, int sdev, int openMode, QObject* parent = nullptr);
304 Timer(const QString& deviceName, int openMode, QObject* parent = nullptr);
305 Timer(const QString& deviceName, int openMode, snd_config_t* config, QObject* parent = nullptr);
306 Timer(TimerId& id, int openMode, QObject* parent = nullptr);
307 virtual ~Timer();
308
309 static TimerId bestGlobalTimerId();
310 static Timer* bestGlobalTimer(int openMode, QObject* parent = nullptr);
315 snd_timer_t* getHandle() { return m_Info; }
316 TimerInfo& getTimerInfo();
317 TimerStatus& getTimerStatus();
318 void setTimerParams(const TimerParams& params);
319
320 void start();
321 void stop();
322 void continueRunning();
323
324 void addAsyncTimerHandler(snd_async_callback_t callback, void *private_data);
325 int getPollDescriptorsCount();
326 void pollDescriptors(struct pollfd *pfds, unsigned int space);
327 void pollDescriptorsRevents(struct pollfd *pfds, unsigned int nfds, unsigned short *revents);
328 ssize_t read(void *buffer, size_t size);
329 snd_timer_t* getTimerHandle();
334 void setHandler(TimerEventHandler* h) { m_handler = h; }
335 void startEvents();
336 void stopEvents();
337
338protected:
339 void doEvents();
340
341signals:
349 void timerExpired(int ticks, int msecs);
350
351private:
352 snd_timer_t *m_Info;
353 snd_async_handler_t *m_asyncHandler;
354 TimerEventHandler* m_handler;
355 QPointer<TimerInputThread> m_thread;
356 TimerInfo m_TimerInfo;
357 TimerStatus m_TimerStatus;
358 QString m_deviceName;
359 snd_htimestamp_t m_last_time;
360};
361
363
364}} /* namespace drumstick::ALSA */
365
366#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:256
virtual void handleTimerEvent(int ticks, int msecs)=0
Timer event handler.
virtual ~TimerEventHandler()=default
Destructor.
Global timer information container.
Definition alsatimer.h:127
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:86
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:54
long getTicks() __attribute__((deprecated))
Gets the maximum timer ticks.
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.
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:192
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:161
TimerIdList getTimers() const
Gets the list of available timers.
Definition alsatimer.h:170
TimerQuery(const QString &deviceName, int openMode)
Constructor.
ALSA Timer status container.
Definition alsatimer.h:227
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:334
void stop()
Stop rolling the timer.
snd_timer_t * getHandle()
Gets the ALSA timer object.
Definition alsatimer.h:315
QList< TimerId > TimerIdList
List of timer identifiers.
Definition alsatimer.h:119
Drumstick visibility macros.
Drumstick ALSA library wrapper.
Drumstick common.