21 #include <QReadLocker> 22 #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);
138 snd_timer_info_copy(m_Info, other.m_Info);
149 return (snd_timer_info_is_slave(m_Info) != 0);
159 return snd_timer_info_get_card(m_Info);
169 return QString(snd_timer_info_get_id(m_Info));
179 return QString(snd_timer_info_get_name(m_Info));
189 return snd_timer_info_get_resolution(m_Info);
202 return 1000000000L / res;
214 return snd_timer_info_sizeof();
225 return snd_timer_info_get_ticks(m_Info);
233 snd_timer_id_malloc(&m_Info);
242 snd_timer_id_malloc(&m_Info);
243 snd_timer_id_copy(m_Info, other);
258 snd_timer_id_malloc(&m_Info);
259 snd_timer_id_copy(m_Info, other.m_Info);
278 snd_timer_id_malloc(&m_Info);
291 snd_timer_id_free(m_Info);
312 snd_timer_id_copy(m_Info, other.m_Info);
335 snd_timer_id_set_class(m_Info, devclass);
346 return snd_timer_id_get_class(m_Info);
356 snd_timer_id_set_sclass(m_Info, devsclass);
366 return snd_timer_id_get_sclass(m_Info);
376 snd_timer_id_set_card(m_Info, card);
386 return snd_timer_id_get_card(m_Info);
396 snd_timer_id_set_device(m_Info, device);
406 return snd_timer_id_get_device(m_Info);
416 snd_timer_id_set_subdevice (m_Info, subdevice);
426 return snd_timer_id_get_subdevice(m_Info);
436 return snd_timer_id_sizeof();
447 deviceName.toLocal8Bit().data(),
462 deviceName.toLocal8Bit().data(),
473 snd_timer_query_close(m_Info);
483 snd_timer_id_set_class(tid.m_Info, SND_TIMER_CLASS_NONE);
486 int rc = snd_timer_query_next_device(m_Info, tid.m_Info);
487 if ((rc < 0) || (tid.
getClass() < 0)) {
490 m_timers.append(tid);
510 snd_timer_query_info(m_Info, m_GlobalInfo.m_Info);
521 snd_timer_query_params(m_Info, params);
531 snd_timer_query_params(m_Info, params);
541 snd_timer_query_status(m_Info, status);
549 snd_timer_ginfo_malloc(&m_Info);
558 snd_timer_ginfo_malloc(&m_Info);
559 snd_timer_ginfo_copy(m_Info, other);
568 snd_timer_ginfo_malloc(&m_Info);
569 snd_timer_ginfo_copy(m_Info, other.m_Info);
577 snd_timer_ginfo_free(m_Info);
598 snd_timer_ginfo_copy(m_Info, other.m_Info);
610 snd_timer_ginfo_set_tid (m_Info, m_Id.m_Info);
620 m_Id =
TimerId(snd_timer_ginfo_get_tid (m_Info));
631 return snd_timer_ginfo_get_flags (m_Info);
641 return snd_timer_ginfo_get_card (m_Info);
651 return QString(snd_timer_ginfo_get_id (m_Info));
661 return QString(snd_timer_ginfo_get_name (m_Info));
671 return snd_timer_ginfo_get_resolution (m_Info);
681 return snd_timer_ginfo_get_resolution_min (m_Info);
691 return snd_timer_ginfo_get_resolution_max(m_Info);
701 return snd_timer_ginfo_get_clients(m_Info);
711 return snd_timer_ginfo_sizeof();
719 snd_timer_params_malloc (&m_Info);
728 snd_timer_params_malloc (&m_Info);
729 snd_timer_params_copy (m_Info, other);
738 snd_timer_params_malloc (&m_Info);
739 snd_timer_params_copy (m_Info, other.m_Info);
748 snd_timer_params_free (m_Info);
769 snd_timer_params_copy (m_Info, other.m_Info);
780 snd_timer_params_set_auto_start (m_Info, auto_start ? 1 : 0);
790 return (snd_timer_params_get_auto_start (m_Info) != 0);
800 snd_timer_params_set_exclusive (m_Info, exclusive ? 1 : 0);
810 return (snd_timer_params_get_exclusive (m_Info) != 0);
820 snd_timer_params_set_early_event (m_Info, early_event ? 1 : 0);
830 return (snd_timer_params_get_early_event (m_Info) != 0);
840 snd_timer_params_set_ticks (m_Info, ticks);
850 return snd_timer_params_get_ticks (m_Info);
860 snd_timer_params_set_queue_size (m_Info, queue_size);
870 return snd_timer_params_get_queue_size (m_Info);
880 snd_timer_params_set_filter (m_Info, filter);
890 return snd_timer_params_get_filter (m_Info);
900 return snd_timer_params_sizeof();
908 snd_timer_status_malloc (&m_Info);
917 snd_timer_status_malloc (&m_Info);
918 snd_timer_status_copy (m_Info, other);
927 snd_timer_status_malloc (&m_Info);
928 snd_timer_status_copy (m_Info, other.m_Info);
936 snd_timer_status_free (m_Info);
957 snd_timer_status_copy (m_Info, other.m_Info);
968 return snd_timer_status_get_timestamp (m_Info);
978 return snd_timer_status_get_resolution (m_Info);
988 return snd_timer_status_get_lost (m_Info);
998 return snd_timer_status_get_overrun (m_Info);
1008 return snd_timer_status_get_queue (m_Info);
1018 return snd_timer_status_sizeof();
1034 m_asyncHandler(NULL),
1037 m_deviceName(deviceName)
1039 CHECK_ERROR( snd_timer_open( &m_Info, m_deviceName.toLocal8Bit().data(),
1058 m_asyncHandler(NULL),
1061 m_deviceName(deviceName)
1064 m_deviceName.toLocal8Bit().data(),
1081 m_asyncHandler(NULL),
1085 m_deviceName = QString(
"hw:CLASS=%1,SCLASS=%2,CARD=%3,DEV=%4,SUBDEV=%5")
1087 .arg(
id.getSlaveClass())
1089 .arg(
id.getDevice())
1090 .arg(
id.getSubdevice());
1092 m_deviceName.toLocal8Bit().data(),
1112 int openMode,
QObject* parent )
1114 m_asyncHandler(NULL),
1118 m_deviceName = QString(
"hw:CLASS=%1,SCLASS=%2,CARD=%3,DEV=%4,SUBDEV=%5")
1125 m_deviceName.toLocal8Bit().data(),
1135 if (m_thread != NULL)
1148 CHECK_WARNING(snd_async_add_timer_handler(&m_asyncHandler, m_Info, callback, private_data));
1158 return snd_async_handler_get_timer(m_asyncHandler);
1168 return snd_timer_poll_descriptors_count(m_Info);
1179 CHECK_WARNING(snd_timer_poll_descriptors(m_Info, pfds, space));
1191 CHECK_WARNING(snd_timer_poll_descriptors_revents(m_Info, pfds, nfds, revents));
1201 snd_timer_info (m_Info, m_TimerInfo.m_Info);
1222 CHECK_WARNING( snd_timer_status(m_Info, m_TimerStatus.m_Info) );
1223 return m_TimerStatus;
1262 return snd_timer_read(m_Info, buffer, size);
1276 snd_timer_tread_t tr;
1277 while (
read(&tr,
sizeof(tr)) ==
sizeof(tr) ) {
1278 int msecs = ((tr.tstamp.tv_sec - m_last_time.tv_sec) * 1000) +
1279 round((tr.tstamp.tv_nsec - m_last_time.tv_nsec) / 1000000.0);
1280 m_last_time = tr.tstamp;
1281 if ( m_handler != NULL )
1294 if (m_thread == NULL) {
1295 m_thread =
new TimerInputThread(
this, 500);
1306 if (m_thread != NULL) {
1308 while (!m_thread->wait(500) && (counter < 10)) {
1311 if (!m_thread->isFinished()) {
1312 m_thread->terminate();
1329 snd_timer_info_t* info;
1330 long res, best_res = LONG_MAX;
1333 SND_TIMER_GLOBAL_SYSTEM
1334 , SND_TIMER_GLOBAL_RTC
1335 #ifdef SND_TIMER_GLOBAL_HPET 1336 , SND_TIMER_GLOBAL_HPET
1338 #ifdef SND_TIMER_GLOBAL_HRTIMER 1339 , SND_TIMER_GLOBAL_HRTIMER
1342 int max_global_timers =
sizeof(test_devs)/
sizeof(
int);
1343 int clas = SND_TIMER_CLASS_GLOBAL;
1344 int scls = SND_TIMER_SCLASS_NONE;
1346 int dev = SND_TIMER_GLOBAL_SYSTEM;
1351 snd_timer_info_alloca(&info);
1354 id.setSlaveClass(scls);
1357 id.setSubdevice(sdev);
1359 for( i = 0; i < max_global_timers; ++i )
1362 sprintf( timername,
"hw:CLASS=%i,SCLASS=%i,CARD=%i,DEV=%i,SUBDEV=%i",
1363 clas, scls, card, dev, sdev );
1364 err = snd_timer_open(&timer, timername, SND_TIMER_OPEN_NONBLOCK);
1365 if (err < 0)
continue;
1366 err = snd_timer_info(timer, info);
1368 is_slave = snd_timer_info_is_slave(info);
1369 res = snd_timer_info_get_resolution(info);
1370 if ((is_slave == 0) && (best_res > res)) {
1375 snd_timer_close(timer);
1391 return new Timer(
id, openMode, parent);
1398 Timer::TimerInputThread::run()
1402 if (m_timer == NULL)
return;
1404 count = m_timer->getPollDescriptorsCount();
1405 fds = (pollfd *) calloc(count,
sizeof(
struct pollfd));
1407 qWarning() <<
"allocation error!";
1410 fds->events = POLLIN;
1414 while (!stopped() && (m_timer != NULL)) {
1415 m_timer->pollDescriptors(fds, count);
1416 if ((err = poll(fds, count, m_Wait)) < 0) {
1417 qWarning() <<
"poll error " << err <<
"(" << strerror(err) <<
")";
1421 qWarning() <<
"timer time out";
1424 m_timer->doEvents();
1427 qWarning() <<
"exception in input thread";
1437 Timer::TimerInputThread::stopped()
1439 QReadLocker locker(&m_mutex);
1447 Timer::TimerInputThread::stop()
1449 QWriteLocker locker(&m_mutex);
TimerGlobalInfo & operator=(const TimerGlobalInfo &other)
Assignment operator.
QString getName()
Gets the timer name.
long getResolution()
Gets the resolution in us.
TimerStatus & operator=(const TimerStatus &other)
Assignment operator.
void setDevice(int device)
Sets the device number.
void doEvents()
Internal function to deliver the timer events using one of the two available methods:
TimerId & getTimerId()
Gets the timer identifier.
virtual ~TimerInfo()
Destructor.
void setQueueSize(long queue_size)
Sets the queue size (32-1024)
TimerId & operator=(const TimerId &other)
Assignment operator.
long getOverrun()
Gets the overrun count.
void setTicks(long ticks)
Sets the timer ticks.
void getGlobalParams(snd_timer_gparams_t *params)
Gets the global timer parameters.
static Timer * bestGlobalTimer(int openMode, QObject *parent=0)
Check and return the best available global Timer in the system, meaning the timer with higher frequen...
snd_htimestamp_t getTimestamp()
Gets the high resolution time-stamp.
bool getEarlyEvent()
Gets the timer early event.
void setAutoStart(bool auto_start)
Sets the automatic start flag.
void pollDescriptors(struct pollfd *pfds, unsigned int space)
Gets poll descriptors.
int getSizeOfInfo() const
Gets the size of the ALSA timer global info object.
QString getId()
Gets the timer ID string.
void setGlobalParams(snd_timer_gparams_t *params)
Sets the global parameters.
void addAsyncTimerHandler(snd_async_callback_t callback, void *private_data)
Adds an asynchronous timer handler function.
unsigned int getClients()
Gets current timer clients.
void getGlobalStatus(snd_timer_gstatus_t *status)
Gets the global timer status.
virtual ~TimerParams()
Destructor.
TimerStatus * clone()
Copy the current object.
int getClass()
Gets the class identifier.
TimerInfo * clone()
Copy the current object.
bool getExclusive()
Gets the timer's exclusive flag.
int getSubdevice()
Gets the subdevice number.
void setCard(int card)
Sets the card number.
long getResolution()
Gets the timer resolution (timer period in nanoseconds)
The QObject class is the base class of all Qt objects.
unsigned long getResolution()
Gets the timer resolution in ns.
Classes managing ALSA Timers.
void setExclusive(bool exclusive)
Sets the exclusive flag.
int getCard()
Gets the card number.
void freeTimers()
Release the internal list of timers.
TimerGlobalInfo * clone()
Copy the current object.
QString getName()
Gets the timer name.
long getFrequency()
Gets the timer frequency in Hz.
void setClass(int devclass)
Set the class identifier.
long getQueueSize()
Gets the queue size.
TimerQuery(const QString &deviceName, int openMode)
Constructor.
ssize_t read(void *buffer, size_t size)
Read bytes from the timer handle.
TimerId * clone()
Copy the object.
int getSlaveClass()
Gets the slave class.
TimerParams()
Default constructor.
void continueRunning()
Continue rolling the timer.
ALSA Timer information container.
ALSA Timer status container.
ALSA Timer parameters container.
void pollDescriptorsRevents(struct pollfd *pfds, unsigned int nfds, unsigned short *revents)
Gets returned events from poll descriptors.
void start()
Start rolling the timer.
unsigned long getMinResolution()
Gets timer minimal resolution in ns.
bool isSlave()
Check if the timer is slave (depends on another device)
virtual ~TimerGlobalInfo()
Destructor.
void setTimerId(const TimerId &tid)
Sets the timer identifier.
void stop()
Stop rolling the timer.
void setTimerParams(const TimerParams ¶ms)
Sets the timer parameters.
int getCard()
Gets the card number.
TimerInfo & getTimerInfo()
Gets the timer info object.
void setFilter(unsigned int filter)
Sets the event filter.
int getCard()
Gets the card number.
Timer(int cls, int scls, int card, int dev, int sdev, int openMode, QObject *parent=0)
Constructor.
unsigned int getFlags()
Gets the flags.
TimerParams & operator=(const TimerParams &other)
Assignment operator.
long getTicks() __attribute__((deprecated))
Gets the maximum timer ticks.
long getLost()
Gets the master tick lost count.
TimerGlobalInfo & getGlobalInfo()
Get a TimerGlobalInfo object.
unsigned long getMaxResolution()
Gets timer maximal resolution in ns.
TimerInfo & operator=(const TimerInfo &other)
Assignment operator.
unsigned int getFilter()
Gets the event filter.
int getSizeOfInfo() const
Gets the size of the ALSA timer parameters object.
static TimerId bestGlobalTimerId()
Check and return the best available global TimerId in the system, meaning the timer with higher frequ...
long getTicks()
Gets the timer ticks.
void setSubdevice(int subdevice)
Sets the subdevice number.
Global timer information container.
int getSizeOfInfo() const
Gets the size of the ALSA timer info object.
virtual ~Timer()
Destructor.
long getQueue()
Gets the count of used queue elements.
virtual ~TimerQuery()
Destructor.
int getPollDescriptorsCount()
Gets the count of poll descriptors.
int getSizeOfInfo() const
Gets the size of the ALSA timer ID object.
void startEvents()
Starts the events dispatching thread.
void readTimers()
Enumerate the available timers storing the results into an internal list.
QString getId()
Gets the string identifier.
virtual void handleTimerEvent(int ticks, int msecs)=0
Timer event handler.
TimerStatus & getTimerStatus()
Gets the timer status.
TimerGlobalInfo()
Default constructor.
int getDevice()
Gets the device number.
void stopEvents()
Stops the events dispatching thread.
#define CHECK_ERROR(x)
This macro calls the check error function.
bool getAutoStart()
Gets the automatic start flag.
#define CHECK_WARNING(x)
This macro calls the check warning function.
void setSlaveClass(int devsclass)
Sets the Slave class.
int getSizeOfInfo() const
Gets the size of the ALSA timer status object.
void timerExpired(int ticks, int msecs)
This signal is emitted when the timer has expired, if there is not an event hander installed.
TimerStatus()
Default constructor.
virtual ~TimerId()
Destructor.
ALSA Timer identifier container.
virtual ~TimerStatus()
Destructor.
void setEarlyEvent(bool early_event)
Sets the timer early event.
snd_timer_t * getTimerHandle()
Gets the ALSA timer handle.
TimerParams * clone()
Copy the current object.