21#include <QWriteLocker>
90 snd_timer_info_malloc(&m_Info);
99 snd_timer_info_malloc(&m_Info);
100 snd_timer_info_copy(m_Info, other);
109 snd_timer_info_malloc(&m_Info);
110 snd_timer_info_copy(m_Info, other.m_Info);
118 snd_timer_info_free(m_Info);
141 snd_timer_info_copy(m_Info, other.m_Info);
152 return (snd_timer_info_is_slave(m_Info) != 0);
162 return snd_timer_info_get_card(m_Info);
172 return QString(snd_timer_info_get_id(m_Info));
182 return QString(snd_timer_info_get_name(m_Info));
192 return snd_timer_info_get_resolution(m_Info);
205 return 1000000000L / res;
217 return snd_timer_info_sizeof();
228 return snd_timer_info_get_ticks(m_Info);
236 snd_timer_id_malloc(&m_Info);
245 snd_timer_id_malloc(&m_Info);
246 snd_timer_id_copy(m_Info, other);
261 snd_timer_id_malloc(&m_Info);
262 snd_timer_id_copy(m_Info, other.m_Info);
281 snd_timer_id_malloc(&m_Info);
294 snd_timer_id_free(m_Info);
317 snd_timer_id_copy(m_Info, other.m_Info);
340 snd_timer_id_set_class(m_Info, devclass);
351 return snd_timer_id_get_class(m_Info);
361 snd_timer_id_set_sclass(m_Info, devsclass);
371 return snd_timer_id_get_sclass(m_Info);
381 snd_timer_id_set_card(m_Info, card);
391 return snd_timer_id_get_card(m_Info);
401 snd_timer_id_set_device(m_Info, device);
411 return snd_timer_id_get_device(m_Info);
421 snd_timer_id_set_subdevice (m_Info, subdevice);
431 return snd_timer_id_get_subdevice(m_Info);
441 return snd_timer_id_sizeof();
452 deviceName.toLocal8Bit().data(),
467 deviceName.toLocal8Bit().data(),
478 snd_timer_query_close(m_Info);
488 snd_timer_id_set_class(tid.m_Info, SND_TIMER_CLASS_NONE);
491 int rc = snd_timer_query_next_device(m_Info, tid.m_Info);
492 if ((rc < 0) || (tid.
getClass() < 0)) {
495 m_timers.append(tid);
515 snd_timer_query_info(m_Info, m_GlobalInfo.m_Info);
526 snd_timer_query_params(m_Info, params);
536 snd_timer_query_params(m_Info, params);
546 snd_timer_query_status(m_Info, status);
554 snd_timer_ginfo_malloc(&m_Info);
563 snd_timer_ginfo_malloc(&m_Info);
564 snd_timer_ginfo_copy(m_Info, other);
573 snd_timer_ginfo_malloc(&m_Info);
574 snd_timer_ginfo_copy(m_Info, other.m_Info);
582 snd_timer_ginfo_free(m_Info);
605 snd_timer_ginfo_copy(m_Info, other.m_Info);
617 snd_timer_ginfo_set_tid (m_Info, m_Id.m_Info);
627 m_Id =
TimerId(snd_timer_ginfo_get_tid (m_Info));
638 return snd_timer_ginfo_get_flags (m_Info);
648 return snd_timer_ginfo_get_card (m_Info);
658 return QString(snd_timer_ginfo_get_id (m_Info));
668 return QString(snd_timer_ginfo_get_name (m_Info));
678 return snd_timer_ginfo_get_resolution (m_Info);
688 return snd_timer_ginfo_get_resolution_min (m_Info);
698 return snd_timer_ginfo_get_resolution_max(m_Info);
708 return snd_timer_ginfo_get_clients(m_Info);
718 return snd_timer_ginfo_sizeof();
726 snd_timer_params_malloc (&m_Info);
735 snd_timer_params_malloc (&m_Info);
736 snd_timer_params_copy (m_Info, other);
745 snd_timer_params_malloc (&m_Info);
746 snd_timer_params_copy (m_Info, other.m_Info);
754 snd_timer_params_free (m_Info);
777 snd_timer_params_copy (m_Info, other.m_Info);
788 snd_timer_params_set_auto_start (m_Info, auto_start ? 1 : 0);
798 return (snd_timer_params_get_auto_start (m_Info) != 0);
808 snd_timer_params_set_exclusive (m_Info, exclusive ? 1 : 0);
818 return (snd_timer_params_get_exclusive (m_Info) != 0);
828 snd_timer_params_set_early_event (m_Info, early_event ? 1 : 0);
838 return (snd_timer_params_get_early_event (m_Info) != 0);
848 snd_timer_params_set_ticks (m_Info, ticks);
858 return snd_timer_params_get_ticks (m_Info);
868 snd_timer_params_set_queue_size (m_Info, queue_size);
878 return snd_timer_params_get_queue_size (m_Info);
888 snd_timer_params_set_filter (m_Info, filter);
898 return snd_timer_params_get_filter (m_Info);
908 return snd_timer_params_sizeof();
916 snd_timer_status_malloc (&m_Info);
925 snd_timer_status_malloc (&m_Info);
926 snd_timer_status_copy (m_Info, other);
935 snd_timer_status_malloc (&m_Info);
936 snd_timer_status_copy (m_Info, other.m_Info);
944 snd_timer_status_free (m_Info);
967 snd_timer_status_copy (m_Info, other.m_Info);
978 return snd_timer_status_get_timestamp (m_Info);
988 return snd_timer_status_get_resolution (m_Info);
998 return snd_timer_status_get_lost (m_Info);
1008 return snd_timer_status_get_overrun (m_Info);
1018 return snd_timer_status_get_queue (m_Info);
1028 return snd_timer_status_sizeof();
1044 m_asyncHandler(nullptr),
1047 m_deviceName(deviceName)
1068 m_asyncHandler(nullptr),
1071 m_deviceName(deviceName)
1074 m_deviceName.toLocal8Bit().data(),
1091 m_asyncHandler(nullptr),
1095 m_deviceName = QString(
"hw:CLASS=%1,SCLASS=%2,CARD=%3,DEV=%4,SUBDEV=%5")
1097 .arg(
id.getSlaveClass())
1099 .arg(
id.getDevice())
1100 .arg(
id.getSubdevice());
1102 m_deviceName.toLocal8Bit().data(),
1122 int openMode,
QObject* parent )
1124 m_asyncHandler(nullptr),
1128 m_deviceName = QString(
"hw:CLASS=%1,SCLASS=%2,CARD=%3,DEV=%4,SUBDEV=%5")
1135 m_deviceName.toLocal8Bit().data(),
1145 if (m_thread !=
nullptr)
1168 return snd_async_handler_get_timer(m_asyncHandler);
1178 return snd_timer_poll_descriptors_count(m_Info);
1211 snd_timer_info (m_Info, m_TimerInfo.m_Info);
1233 return m_TimerStatus;
1272 return snd_timer_read(m_Info, buffer, size);
1286 snd_timer_tread_t tr;
1287 while (
read(&tr,
sizeof(tr)) ==
sizeof(tr) ) {
1288 int msecs = ((tr.tstamp.tv_sec - m_last_time.tv_sec) * 1000) +
1289 round((tr.tstamp.tv_nsec - m_last_time.tv_nsec) / 1000000.0);
1290 m_last_time = tr.tstamp;
1291 if ( m_handler !=
nullptr )
1304 if (m_thread ==
nullptr) {
1305 m_thread =
new TimerInputThread(
this, 500);
1316 if (m_thread !=
nullptr) {
1318 while (!m_thread->wait(500) && (counter < 10)) {
1321 if (!m_thread->isFinished()) {
1322 m_thread->terminate();
1339 snd_timer_info_t* info;
1340 long res, best_res = LONG_MAX;
1343 SND_TIMER_GLOBAL_SYSTEM
1344 , SND_TIMER_GLOBAL_RTC
1345#ifdef SND_TIMER_GLOBAL_HPET
1346 , SND_TIMER_GLOBAL_HPET
1348#ifdef SND_TIMER_GLOBAL_HRTIMER
1349 , SND_TIMER_GLOBAL_HRTIMER
1352 int max_global_timers =
sizeof(test_devs)/
sizeof(
int);
1353 int clas = SND_TIMER_CLASS_GLOBAL;
1354 int scls = SND_TIMER_SCLASS_NONE;
1356 int dev = SND_TIMER_GLOBAL_SYSTEM;
1361 snd_timer_info_alloca(&info);
1364 id.setSlaveClass(scls);
1367 id.setSubdevice(sdev);
1369 for( i = 0; i < max_global_timers; ++i )
1372 sprintf( timername,
"hw:CLASS=%i,SCLASS=%i,CARD=%i,DEV=%i,SUBDEV=%i",
1373 clas, scls, card, dev, sdev );
1374 err = snd_timer_open(&timer, timername, SND_TIMER_OPEN_NONBLOCK);
1375 if (err < 0)
continue;
1376 err = snd_timer_info(timer, info);
1378 is_slave = snd_timer_info_is_slave(info);
1379 res = snd_timer_info_get_resolution(info);
1380 if ((is_slave == 0) && (best_res > res)) {
1385 snd_timer_close(timer);
1401 return new Timer(
id, openMode, parent);
1408Timer::TimerInputThread::run()
1412 if (m_timer ==
nullptr)
return;
1414 count = m_timer->getPollDescriptorsCount();
1415 fds = (pollfd *) calloc(count,
sizeof(
struct pollfd));
1416 if (fds ==
nullptr) {
1417 qWarning() <<
"allocation error!";
1420 fds->events = POLLIN;
1424 while (!stopped() && (m_timer !=
nullptr)) {
1425 m_timer->pollDescriptors(fds, count);
1426 if ((err = poll(fds, count, m_Wait)) < 0) {
1427 qWarning() <<
"poll error " << err <<
"(" << strerror(err) <<
")";
1432 qWarning() <<
"timer time out";
1436 m_timer->doEvents();
1439 qWarning() <<
"exception in input thread";
1449Timer::TimerInputThread::stopped()
1451 QReadLocker locker(&m_mutex);
1459Timer::TimerInputThread::stop()
1461 QWriteLocker locker(&m_mutex);
Classes managing ALSA Timers.
The QObject class is the base class of all Qt objects.
virtual void handleTimerEvent(int ticks, int msecs)=0
Timer event handler.
Global timer information container.
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.
virtual ~TimerGlobalInfo()
Destructor.
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.
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.
virtual ~TimerId()
Destructor.
void setDevice(int device)
Sets the device number.
ALSA Timer information container.
int getSizeOfInfo() const
Gets the size of the ALSA timer info object.
bool isSlave()
Check if the timer is slave (depends on another device)
virtual ~TimerInfo()
Destructor.
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.
void setFilter(unsigned int filter)
Sets the event filter.
void setEarlyEvent(bool early_event)
Sets the timer early event.
virtual ~TimerParams()
Destructor.
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)
void getGlobalStatus(snd_timer_gstatus_t *status)
Gets the global timer status.
virtual ~TimerQuery()
Destructor.
void readTimers()
Enumerate the available timers storing the results into an internal list.
TimerQuery(const QString &deviceName, int openMode)
Constructor.
void freeTimers()
Release the internal list of timers.
TimerGlobalInfo & getGlobalInfo()
Get a TimerGlobalInfo object.
void setGlobalParams(snd_timer_gparams_t *params)
Sets the global parameters.
void getGlobalParams(snd_timer_gparams_t *params)
Gets the global timer parameters.
ALSA Timer status container.
long getLost()
Gets the master tick lost count.
virtual ~TimerStatus()
Destructor.
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.
void stopEvents()
Stops the events dispatching thread.
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...
int getPollDescriptorsCount()
Gets the count of poll descriptors.
void continueRunning()
Continue rolling the timer.
TimerStatus & getTimerStatus()
Gets the timer status.
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...
virtual ~Timer()
Destructor.
snd_timer_t * getTimerHandle()
Gets the ALSA timer handle.
void start()
Start rolling the timer.
void setTimerParams(const TimerParams ¶ms)
Sets the timer parameters.
void stop()
Stop rolling the timer.
void addAsyncTimerHandler(snd_async_callback_t callback, void *private_data)
Adds an asynchronous timer handler function.
ssize_t read(void *buffer, size_t size)
Read bytes from the timer handle.
TimerInfo & getTimerInfo()
Gets the timer info object.
void doEvents()
Internal function to deliver the timer events using one of the two available methods:
void startEvents()
Starts the events dispatching thread.
void pollDescriptorsRevents(struct pollfd *pfds, unsigned int nfds, unsigned short *revents)
Gets returned events from poll descriptors.
void pollDescriptors(struct pollfd *pfds, unsigned int space)
Gets poll descriptors.
Error checking functions and macros.
#define DRUMSTICK_ALSA_CHECK_WARNING(x)
This macro calls the check warning function.
#define DRUMSTICK_ALSA_CHECK_ERROR(x)
This macro calls the check error function.