drumstick 0.5.0
alsaclient.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_ALSACLIENT_H
21#define DRUMSTICK_ALSACLIENT_H
22
23#include "alsaport.h"
24#include <QPointer>
25#include <QThread>
26#include <QReadWriteLock>
27
35
36namespace drumstick {
37
38class MidiQueue;
39class MidiClient;
40class SequencerEvent;
41class SequencerInputThread;
42class RemoveEvents;
43
50class DRUMSTICK_EXPORT ClientInfo
51{
52 friend class MidiClient;
53
54public:
55 ClientInfo();
56 ClientInfo(const ClientInfo& other);
57 ClientInfo(snd_seq_client_info_t* other);
58 ClientInfo(MidiClient* seq, int id);
59 virtual ~ClientInfo();
61 ClientInfo& operator=(const ClientInfo& other);
62 int getSizeOfInfo() const;
63
64 int getClientId();
65 snd_seq_client_type_t getClientType();
66 QString getName();
67 bool getBroadcastFilter();
68 bool getErrorBounce();
69 int getNumPorts();
70 int getEventLost();
71 void setClient(int client);
72 void setName(QString name);
73 void setBroadcastFilter(bool val);
74 void setErrorBounce(bool val);
75 PortInfoList getPorts() const;
76
77#if SND_LIB_VERSION > 0x010010
78 void addFilter(int eventType);
79 bool isFiltered(int eventType);
80 void clearFilter();
81 void removeFilter(int eventType);
82#endif
83
84protected:
85 void readPorts(MidiClient* seq);
86 void freePorts();
87
88 const unsigned char* getEventFilter() __attribute__((deprecated));
89 void setEventFilter(unsigned char* filter) __attribute__((deprecated));
90
91private:
92 snd_seq_client_info_t* m_Info;
93 PortInfoList m_Ports;
94};
95
99typedef QList<ClientInfo> ClientInfoList;
100
107class DRUMSTICK_EXPORT SystemInfo
108{
109 friend class MidiClient;
110
111public:
112 SystemInfo();
113 SystemInfo(const SystemInfo& other);
114 SystemInfo(snd_seq_system_info_t* other);
115 SystemInfo(MidiClient* seq);
116 virtual ~SystemInfo();
117 SystemInfo* clone();
118 SystemInfo& operator=(const SystemInfo& other);
119 int getSizeOfInfo() const;
120
121 int getMaxClients();
122 int getMaxPorts();
123 int getMaxQueues();
124 int getMaxChannels();
125 int getCurrentQueues();
126 int getCurrentClients();
127
128private:
129 snd_seq_system_info_t* m_Info;
130};
131
138class DRUMSTICK_EXPORT PoolInfo
139{
140 friend class MidiClient;
141
142public:
143 PoolInfo();
144 PoolInfo(const PoolInfo& other);
145 PoolInfo(snd_seq_client_pool_t* other);
146 PoolInfo(MidiClient* seq);
147 virtual ~PoolInfo();
148 PoolInfo* clone();
149 PoolInfo& operator=(const PoolInfo& other);
150 int getSizeOfInfo() const;
151
152 int getClientId();
153 int getInputFree();
154 int getInputPool();
155 int getOutputFree();
156 int getOutputPool();
157 int getOutputRoom();
158 void setInputPool(int size);
159 void setOutputPool(int size);
160 void setOutputRoom(int size);
161
162private:
163 snd_seq_client_pool_t* m_Info;
164};
165
175class DRUMSTICK_EXPORT SequencerEventHandler
176{
177public:
180
191};
192
198class DRUMSTICK_EXPORT MidiClient : public QObject
199{
200 Q_OBJECT
201public:
202 MidiClient( QObject* parent = 0 );
203 virtual ~MidiClient();
204
205 void open( const QString deviceName = "default",
206 const int openMode = SND_SEQ_OPEN_DUPLEX,
207 const bool blockMode = false );
208 void open( snd_config_t* conf,
209 const QString deviceName = "default",
210 const int openMode = SND_SEQ_OPEN_DUPLEX,
211 const bool blockMode = false );
212 void close();
213 void startSequencerInput();
214 void stopSequencerInput();
217 MidiQueue* createQueue(QString const& name);
219 MidiQueue* useQueue(int queue_id);
220 MidiQueue* useQueue(const QString& name);
222 void portAttach(MidiPort* port);
223 void portDetach(MidiPort* port);
224 void detachAllPorts();
225 void addEventFilter(int evtype);
226 void output(SequencerEvent* ev, bool async = false, int timeout = -1);
227 void outputDirect(SequencerEvent* ev, bool async = false, int timeout = -1);
229 void drainOutput(bool async = false, int timeout = -1);
230 void synchronizeOutput();
231
232 int getClientId();
233 snd_seq_type_t getSequencerType();
235 snd_seq_t* getHandle() { return m_SeqHandle; }
237 bool isOpened() { return (m_SeqHandle != NULL); }
238
239 size_t getOutputBufferSize();
240 void setOutputBufferSize(size_t newSize);
241 size_t getInputBufferSize();
242 void setInputBufferSize(size_t newSize);
244 QString getDeviceName() { return m_DeviceName; }
246 int getOpenMode() { return m_OpenMode; }
248 bool getBlockMode() { return m_BlockMode; }
249 void setBlockMode(bool newValue);
250 QString getClientName();
251 QString getClientName(const int clientId);
252 void setClientName(QString const& newName);
253 bool getBroadcastFilter();
254 void setBroadcastFilter(bool newValue);
255 bool getErrorBounce();
256 void setErrorBounce(bool newValue);
257
258 ClientInfo& getThisClientInfo();
259 void setThisClientInfo(const ClientInfo& val);
260 MidiPortList getMidiPorts() const;
261 ClientInfoList getAvailableClients();
262 PortInfoList getAvailableInputs();
263 PortInfoList getAvailableOutputs();
264 SystemInfo& getSystemInfo();
265 QList<int> getAvailableQueues();
266
267 PoolInfo& getPoolInfo();
268 void setPoolInfo(const PoolInfo& info);
269 void setPoolInput(int size);
270 void setPoolOutput(int size);
271 void setPoolOutputRoom(int size);
272 void resetPoolInput();
273 void resetPoolOutput();
274 void dropInput();
275 void dropInputBuffer();
276 void dropOutput();
277 void dropOutputBuffer();
278 void removeEvents(const RemoveEvents* spec);
279 SequencerEvent* extractOutput();
280 int outputPending();
281 int inputPending(bool fetch);
282 int getQueueId(const QString& name);
283
284 void addListener(QObject* listener);
285 void removeListener(QObject* listener);
286 void setEventsEnabled(const bool bEnabled);
288 bool getEventsEnabled() const { return m_eventsEnabled; }
290 void setHandler(SequencerEventHandler* handler) { m_handler = handler; }
291 bool parseAddress( const QString& straddr, snd_seq_addr& result );
292 void setRealTimeInput(bool enabled);
293 bool realTimeInputEnabled();
294
295signals:
298
299protected:
300 void doEvents();
301 void applyClientInfo();
302 void readClients();
303 void freeClients();
305 PortInfoList filterPorts(unsigned int filter);
306
307 /* low level public functions */
308 const char * _getDeviceName();
309 int getPollDescriptorsCount(short events);
310 int pollDescriptors(struct pollfd *pfds, unsigned int space, short events);
311 unsigned short pollDescriptorsRevents(struct pollfd *pfds, unsigned int nfds);
312
313 /* mid level functions */
314 void _setClientName( const char *name );
315 int createSimplePort( const char *name,
316 unsigned int caps,
317 unsigned int type );
318 void deleteSimplePort( int port );
319 void connectFrom(int myport, int client, int port);
320 void connectTo(int myport, int client, int port);
321 void disconnectFrom(int myport, int client, int port);
322 void disconnectTo(int myport, int client, int port);
323
324private:
326 bool m_eventsEnabled;
327 bool m_BlockMode;
328 bool m_NeedRefreshClientList;
329 int m_OpenMode;
330 QString m_DeviceName;
331 snd_seq_t* m_SeqHandle;
332 QPointer<SequencerInputThread> m_Thread;
333 QPointer<MidiQueue> m_Queue;
334 SequencerEventHandler* m_handler;
335
336 ClientInfo m_Info;
337 ClientInfoList m_ClientList;
338 MidiPortList m_Ports;
339 PortInfoList m_OutputsAvail;
340 PortInfoList m_InputsAvail;
341 QObjectList m_listeners;
342 SystemInfo m_sysInfo;
343 PoolInfo m_poolInfo;
344};
345
346#if SND_LIB_VERSION > 0x010004
347DRUMSTICK_EXPORT QString getRuntimeALSALibraryVersion();
348DRUMSTICK_EXPORT int getRuntimeALSALibraryNumber();
349#endif
350DRUMSTICK_EXPORT QString getRuntimeALSADriverVersion();
351DRUMSTICK_EXPORT int getRuntimeALSADriverNumber();
352
353} /* namespace drumstick */
354
356
357#endif // DRUMSTICK_ALSACLIENT_H
QList< ClientInfo > ClientInfoList
List of sequencer client information.
Definition alsaclient.h:99
DRUMSTICK_EXPORT QString getRuntimeALSADriverVersion()
Gets the runtime ALSA drivers version string.
DRUMSTICK_EXPORT int getRuntimeALSADriverNumber()
Gets the runtime ALSA drivers version number.
Classes managing ALSA Sequencer ports.
QList< MidiPort * > MidiPortList
List of Ports instances.
Definition alsaport.h:215
QList< PortInfo > PortInfoList
List of port information objects.
Definition alsaport.h:112
The QObject class is the base class of all Qt objects.
Client information.
Definition alsaclient.h:51
void setBroadcastFilter(bool val)
Sets the broadcast filter.
int getSizeOfInfo() const
Gets the size of the internal object.
bool getErrorBounce()
Gets the client's error bounce.
void readPorts(MidiClient *seq)
Read the client ports.
void setName(QString name)
Sets the client name.
const unsigned char * getEventFilter() __attribute__((deprecated))
Gets the client's event filter.
snd_seq_client_type_t getClientType()
Gets the client's type.
void setErrorBounce(bool val)
Sets the error bounce.
ClientInfo()
Default constructor.
bool getBroadcastFilter()
Gets the client's broadcast filter.
int getNumPorts()
Gets the client's port count.
void setClient(int client)
Sets the client identifier number.
ClientInfo & operator=(const ClientInfo &other)
Assignment operator.
QString getName()
Gets the client's name.
int getClientId()
Gets the client's numeric identifier.
void setEventFilter(unsigned char *filter) __attribute__((deprecated))
Sets the event filter.
int getEventLost()
Gets the number of lost events.
PortInfoList getPorts() const
Gets the ports list.
void freePorts()
Release the ports list.
ClientInfo * clone()
Clone the client info object.
This class manages event input from the ALSA sequencer.
Client management.
Definition alsaclient.h:199
void outputBuffer(SequencerEvent *ev)
Output an event using the library output buffer, without draining the buffer.
MidiQueue * useQueue(int queue_id)
Create a new MidiQueue instance using a queue already existing in the system, associating it to the c...
MidiQueue * createQueue()
Create and return a new MidiQueue associated to this client.
QString getDeviceName()
Returns the name of the sequencer device.
Definition alsaclient.h:244
bool getEventsEnabled() const
Returns true if the events mode of delivery has been enabled.
Definition alsaclient.h:288
int createSimplePort(const char *name, unsigned int caps, unsigned int type)
Create an ALSA sequencer port, without using MidiPort.
void startSequencerInput()
Starts reading events from the ALSA sequencer.
void readClients()
Reads the ALSA sequencer's clients list.
int pollDescriptors(struct pollfd *pfds, unsigned int space, short events)
Get poll descriptors.
void _setClientName(const char *name)
Sets the client name.
void disconnectTo(int myport, int client, int port)
Unsubscribe one port to another arbitrary sequencer client:port.
snd_seq_type_t getSequencerType()
Returns the type snd_seq_type_t of the given sequencer handle.
PortInfoList filterPorts(unsigned int filter)
Gets a list of the available user ports in the system, filtered by the given bitmap of desired capabi...
void applyClientInfo()
This internal method applies the ClientInfo data to the ALSA sequencer client.
void close()
Close the sequencer device.
MidiQueue * getQueue()
Get the MidiQueue instance associated to this client.
int getPollDescriptorsCount(short events)
Returns the number of poll descriptors.
void detachAllPorts()
Detach all the ports belonging to this client.
void stopSequencerInput()
Stops reading events from the ALSA sequencer.
void deleteSimplePort(int port)
Remove an ALSA sequencer port.
snd_seq_t * getHandle()
Returns the sequencer handler managed by ALSA.
Definition alsaclient.h:235
void output(SequencerEvent *ev, bool async=false, int timeout=-1)
Output an event using the library output buffer.
int getOpenMode()
Returns the last open mode used in open()
Definition alsaclient.h:246
MidiPort * createPort()
Create and attach a new MidiPort instance to this client.
void portDetach(MidiPort *port)
Detach a MidiPort instance from this client.
const char * _getDeviceName()
Gets the internal sequencer device name.
void setHandler(SequencerEventHandler *handler)
Sets a sequencer event handler enabling the callback delivery mode.
Definition alsaclient.h:290
MidiClient(QObject *parent=0)
Constructor.
int getClientId()
Gets the client ID.
void doEvents()
Dispatch the events received from the Sequencer.
void open(const QString deviceName="default", const int openMode=SND_SEQ_OPEN_DUPLEX, const bool blockMode=false)
Open the sequencer device.
void disconnectFrom(int myport, int client, int port)
Unsubscribe one port from another arbitrary sequencer client:port.
void synchronizeOutput()
Wait until all sent events are processed.
void updateAvailablePorts()
Update the internal lists of user ports.
void freeClients()
Releases the list of ALSA sequencer's clients.
unsigned short pollDescriptorsRevents(struct pollfd *pfds, unsigned int nfds)
Gets the number of returned events from poll descriptors.
void connectFrom(int myport, int client, int port)
Subscribe one port from another arbitrary sequencer client:port.
void eventReceived(SequencerEvent *ev)
Signal emitted when an event is received.
void portAttach(MidiPort *port)
Attach a MidiPort instance to this client.
bool getBlockMode()
Returns the last block mode used in open()
Definition alsaclient.h:248
void addEventFilter(int evtype)
Add an event filter to the client.
void outputDirect(SequencerEvent *ev, bool async=false, int timeout=-1)
Output an event directly to the sequencer.
void drainOutput(bool async=false, int timeout=-1)
Drain the library output buffer.
bool isOpened()
Returns true if the sequencer is opened.
Definition alsaclient.h:237
void connectTo(int myport, int client, int port)
Subscribe one port to another arbitrary sequencer client:port.
Port management.
Definition alsaport.h:120
Queue management.
Definition alsaqueue.h:189
Sequencer Pool information.
Definition alsaclient.h:139
int getInputFree()
Gets the available size on input pool.
void setOutputRoom(int size)
Sets the output room size.
PoolInfo & operator=(const PoolInfo &other)
Assignment operator.
int getSizeOfInfo() const
Gets the size of the client pool object.
int getOutputRoom()
Gets the output room size.
PoolInfo()
Default constructor.
int getInputPool()
Gets the input pool size.
void setInputPool(int size)
Set the input pool size.
int getClientId()
Gets the client ID for this object.
int getOutputFree()
Gets the available size on output pool.
PoolInfo * clone()
Clone the pool info obeject.
void setOutputPool(int size)
Sets the output pool size.
int getOutputPool()
Gets the output pool size.
Auxiliary class to remove events from an ALSA queue.
Definition alsaevent.h:587
Sequencer events handler.
Definition alsaclient.h:176
virtual ~SequencerEventHandler()
Destructor.
Definition alsaclient.h:179
virtual void handleSequencerEvent(SequencerEvent *ev)=0
Callback function to be implemented by the derived class.
Base class for the event's hierarchy.
Definition alsaevent.h:54
System information.
Definition alsaclient.h:108
int getSizeOfInfo() const
Get the system's info object size.
int getMaxQueues()
Get the system's maximum number of queues.
int getCurrentQueues()
Get the system's current number of queues.
int getMaxPorts()
Get the system's maximum number of ports.
int getMaxChannels()
Get the system's maximum number of channels.
SystemInfo * clone()
Clone the system info object.
SystemInfo & operator=(const SystemInfo &other)
Assignment operator.
int getCurrentClients()
Get the system's current number of clients.
int getMaxClients()
Get the system's maximum number of clients.
SystemInfo()
Default constructor.