drumstick 0.5.0
alsatimer.h
Go to the documentation of this file.
1/*
2 MIDI Sequencer C++ library
3 Copyright (C) 2006-2010, 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 2 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 along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18*/
19
20#ifndef DRUMSTICK_ALSATIMER_H
21#define DRUMSTICK_ALSATIMER_H
22
23#include "drumstickcommon.h"
24#include <QList>
25#include <QThread>
26#include <QReadWriteLock>
27#include <QPointer>
28
36namespace drumstick {
37
38class TimerQuery;
39class TimerId;
40class TimerGlobalInfo;
41
47class DRUMSTICK_EXPORT TimerInfo
48{
49 friend class Timer;
50
51public:
52 TimerInfo();
53 TimerInfo(const TimerInfo& other);
54 TimerInfo(const snd_timer_info_t* other);
55 virtual ~TimerInfo();
56 TimerInfo* clone();
57 TimerInfo& operator=(const TimerInfo& other);
58 int getSizeOfInfo() const;
59
60 bool isSlave();
61 int getCard();
62 QString getId();
63 QString getName();
64 long getResolution();
65 long getFrequency();
66
67protected:
68 long getTicks() __attribute__((deprecated));
69
70private:
71 snd_timer_info_t *m_Info;
72};
73
79class DRUMSTICK_EXPORT TimerId
80{
81 friend class TimerQuery;
82 friend class TimerGlobalInfo;
83 friend class QueueTimer;
84
85public:
86 TimerId();
87 TimerId(const TimerId& other);
88 TimerId(const snd_timer_id_t *other);
89 TimerId(int cls, int scls, int card, int dev, int sdev);
90 virtual ~TimerId();
91 TimerId* clone();
92 TimerId& operator=(const TimerId& other);
93 int getSizeOfInfo() const;
94
95 void setClass(int devclass);
96 int getClass();
97 void setSlaveClass(int devsclass);
98 int getSlaveClass();
99 void setCard(int card);
100 int getCard();
101 void setDevice(int device);
102 int getDevice();
103 void setSubdevice(int subdevice);
104 int getSubdevice();
105
106private:
107 snd_timer_id_t *m_Info;
108};
109
113typedef QList<TimerId> TimerIdList;
114
120class DRUMSTICK_EXPORT TimerGlobalInfo
121{
122 friend class TimerQuery;
123
124public:
126 TimerGlobalInfo(const TimerGlobalInfo& other);
127 TimerGlobalInfo(const snd_timer_ginfo_t* other);
128 virtual ~TimerGlobalInfo();
129 TimerGlobalInfo* clone();
130 TimerGlobalInfo& operator=(const TimerGlobalInfo& other);
131 int getSizeOfInfo() const;
132
133 void setTimerId(const TimerId& tid);
134 TimerId& getTimerId();
135 unsigned int getFlags();
136 int getCard();
137 QString getId();
138 QString getName();
139 unsigned long getResolution();
140 unsigned long getMinResolution();
141 unsigned long getMaxResolution();
142 unsigned int getClients();
143
144private:
145 snd_timer_ginfo_t* m_Info;
146 TimerId m_Id;
147};
148
154class DRUMSTICK_EXPORT TimerQuery
155{
156public:
157 TimerQuery(const QString& deviceName, int openMode);
158 TimerQuery(const QString& deviceName, int openMode, snd_config_t* conf);
159 virtual ~TimerQuery();
164 TimerIdList getTimers() const { return m_timers; }
165 TimerGlobalInfo& getGlobalInfo();
166 void setGlobalParams(snd_timer_gparams_t* params);
167 void getGlobalParams(snd_timer_gparams_t* params);
168 void getGlobalStatus(snd_timer_gstatus_t* status);
169
170protected:
171 void readTimers();
172 void freeTimers();
173
174private:
175 snd_timer_query_t *m_Info;
176 TimerIdList m_timers;
177 TimerGlobalInfo m_GlobalInfo;
178};
179
185class DRUMSTICK_EXPORT TimerParams
186{
187 friend class Timer;
188
189public:
190 TimerParams();
191 TimerParams(const TimerParams& other);
192 TimerParams(const snd_timer_params_t* other);
193 virtual ~TimerParams();
194 TimerParams* clone();
195 TimerParams& operator=(const TimerParams& other);
196 int getSizeOfInfo() const;
197
198 void setAutoStart(bool auto_start);
199 bool getAutoStart();
200 void setExclusive(bool exclusive);
201 bool getExclusive();
202 void setEarlyEvent(bool early_event);
203 bool getEarlyEvent();
204 void setTicks(long ticks);
205 long getTicks();
206 void setQueueSize(long queue_size);
207 long getQueueSize();
208 void setFilter(unsigned int filter);
209 unsigned int getFilter();
210
211private:
212 snd_timer_params_t* m_Info;
213};
214
220class DRUMSTICK_EXPORT TimerStatus
221{
222 friend class Timer;
223
224public:
225 TimerStatus();
226 TimerStatus(const TimerStatus& other);
227 TimerStatus(const snd_timer_status_t* other);
228 virtual ~TimerStatus();
229 TimerStatus* clone();
230 TimerStatus& operator=(const TimerStatus& other);
231 int getSizeOfInfo() const;
232
233 snd_htimestamp_t getTimestamp();
234 long getResolution();
235 long getLost();
236 long getOverrun();
237 long getQueue();
238
239private:
240 snd_timer_status_t* m_Info;
241};
242
249class DRUMSTICK_EXPORT TimerEventHandler
250{
251public:
259 virtual void handleTimerEvent(int ticks, int msecs) = 0;
260};
261
267class DRUMSTICK_EXPORT Timer : public QObject
268{
269 Q_OBJECT
270
271private:
275 class TimerInputThread : public QThread
276 {
277 public:
279 TimerInputThread(Timer* t, int timeout)
280 : QThread(),
281 m_timer(t),
282 m_Wait(timeout),
283 m_Stopped(false) {}
285 virtual ~TimerInputThread() {}
286 virtual void run();
287 bool stopped();
288 void stop();
289 private:
290 Timer* m_timer;
291 int m_Wait;
292 bool m_Stopped;
293 QReadWriteLock m_mutex;
294 };
295
296public:
297 Timer(int cls, int scls, int card, int dev, int sdev, int openMode, QObject* parent = 0);
298 Timer(const QString& deviceName, int openMode, QObject* parent = 0);
299 Timer(const QString& deviceName, int openMode, snd_config_t* config, QObject* parent = 0);
300 Timer(TimerId& id, int openMode, QObject* parent = 0);
301 virtual ~Timer();
302
303 static TimerId bestGlobalTimerId();
304 static Timer* bestGlobalTimer(int openMode, QObject* parent = 0);
309 snd_timer_t* getHandle() { return m_Info; }
310 TimerInfo& getTimerInfo();
311 TimerStatus& getTimerStatus();
312 void setTimerParams(const TimerParams& params);
313
314 void start();
315 void stop();
316 void continueRunning();
317
318 void addAsyncTimerHandler(snd_async_callback_t callback, void *private_data);
319 int getPollDescriptorsCount();
320 void pollDescriptors(struct pollfd *pfds, unsigned int space);
321 void pollDescriptorsRevents(struct pollfd *pfds, unsigned int nfds, unsigned short *revents);
322 ssize_t read(void *buffer, size_t size);
323 snd_timer_t* getTimerHandle();
328 void setHandler(TimerEventHandler* h) { m_handler = h; }
329 void startEvents();
330 void stopEvents();
331
332protected:
333 void doEvents();
334
335signals:
343 void timerExpired(int ticks, int msecs);
344
345private:
346 snd_timer_t *m_Info;
347 snd_async_handler_t *m_asyncHandler;
348 TimerEventHandler* m_handler;
349 QPointer<TimerInputThread> m_thread;
350 TimerInfo m_TimerInfo;
351 TimerStatus m_TimerStatus;
352 QString m_deviceName;
353 snd_htimestamp_t m_last_time;
354};
355
356} /* namespace drumstick */
357
360#endif /* DRUMSTICK_ALSATIMER_H */
QList< TimerId > TimerIdList
List of timer identifiers.
Definition: alsatimer.h:113
The QObject class is the base class of all Qt objects.
The QThread class provides platform-independent threads.
Queue timer container.
Definition: alsaqueue.h:158
ALSA Timer events handler.
Definition: alsatimer.h:250
virtual void handleTimerEvent(int ticks, int msecs)=0
Timer event handler.
virtual ~TimerEventHandler()
Destructor.
Definition: alsatimer.h:253
Global timer information container.
Definition: alsatimer.h:121
ALSA Timer identifier container.
Definition: alsatimer.h:80
ALSA Timer information container.
Definition: alsatimer.h:48
ALSA Timer parameters container.
Definition: alsatimer.h:186
ALSA Timer inquiry helper.
Definition: alsatimer.h:155
TimerIdList getTimers() const
Gets the list of available timers.
Definition: alsatimer.h:164
ALSA Timer status container.
Definition: alsatimer.h:221
ALSA Timer management.
Definition: alsatimer.h:268
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:328
snd_timer_t * getHandle()
Gets the ALSA timer object.
Definition: alsatimer.h:309
Common functionality.