drumstick  1.1.3
alsaclient.h
Go to the documentation of this file.
1 /*
2  MIDI Sequencer C++ library
3  Copyright (C) 2006-2019, 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
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef DRUMSTICK_ALSACLIENT_H
20 #define DRUMSTICK_ALSACLIENT_H
21 
22 #include "alsaport.h"
23 #include <QPointer>
24 #include <QThread>
25 #include <QReadWriteLock>
26 
35 namespace drumstick {
36 
37 class MidiQueue;
38 class MidiClient;
39 class SequencerEvent;
40 class SequencerInputThread;
41 class RemoveEvents;
42 
49 class DRUMSTICK_EXPORT ClientInfo
50 {
51  friend class MidiClient;
52 
53 public:
54  ClientInfo();
55  ClientInfo(const ClientInfo& other);
56  ClientInfo(snd_seq_client_info_t* other);
57  ClientInfo(MidiClient* seq, int id);
58  virtual ~ClientInfo();
59  ClientInfo* clone();
60  ClientInfo& operator=(const ClientInfo& other);
61  int getSizeOfInfo() const;
62 
63  int getClientId();
64  snd_seq_client_type_t getClientType();
65  QString getName();
66  bool getBroadcastFilter();
67  bool getErrorBounce();
68  int getNumPorts();
69  int getEventLost();
70  void setClient(int client);
71  void setName(QString name);
72  void setBroadcastFilter(bool val);
73  void setErrorBounce(bool val);
74  PortInfoList getPorts() const;
75 
76 #if SND_LIB_VERSION > 0x010010
77  void addFilter(int eventType);
78  bool isFiltered(int eventType);
79  void clearFilter();
80  void removeFilter(int eventType);
81 #endif
82 
83 protected:
84  void readPorts(MidiClient* seq);
85  void freePorts();
86 
87  const unsigned char* getEventFilter() __attribute__((deprecated));
88  void setEventFilter(unsigned char* filter) __attribute__((deprecated));
89 
90 private:
91  snd_seq_client_info_t* m_Info;
92  PortInfoList m_Ports;
93 };
94 
98 typedef QList<ClientInfo> ClientInfoList;
99 
106 class DRUMSTICK_EXPORT SystemInfo
107 {
108  friend class MidiClient;
109 
110 public:
111  SystemInfo();
112  SystemInfo(const SystemInfo& other);
113  SystemInfo(snd_seq_system_info_t* other);
114  SystemInfo(MidiClient* seq);
115  virtual ~SystemInfo();
116  SystemInfo* clone();
117  SystemInfo& operator=(const SystemInfo& other);
118  int getSizeOfInfo() const;
119 
120  int getMaxClients();
121  int getMaxPorts();
122  int getMaxQueues();
123  int getMaxChannels();
124  int getCurrentQueues();
125  int getCurrentClients();
126 
127 private:
128  snd_seq_system_info_t* m_Info;
129 };
130 
137 class DRUMSTICK_EXPORT PoolInfo
138 {
139  friend class MidiClient;
140 
141 public:
142  PoolInfo();
143  PoolInfo(const PoolInfo& other);
144  PoolInfo(snd_seq_client_pool_t* other);
145  PoolInfo(MidiClient* seq);
146  virtual ~PoolInfo();
147  PoolInfo* clone();
148  PoolInfo& operator=(const PoolInfo& other);
149  int getSizeOfInfo() const;
150 
151  int getClientId();
152  int getInputFree();
153  int getInputPool();
154  int getOutputFree();
155  int getOutputPool();
156  int getOutputRoom();
157  void setInputPool(int size);
158  void setOutputPool(int size);
159  void setOutputRoom(int size);
160 
161 private:
162  snd_seq_client_pool_t* m_Info;
163 };
164 
174 class DRUMSTICK_EXPORT SequencerEventHandler
175 {
176 public:
179 
189  virtual void handleSequencerEvent(SequencerEvent* ev) = 0;
190 };
191 
197 class DRUMSTICK_EXPORT MidiClient : public QObject
198 {
199  Q_OBJECT
200 public:
201  MidiClient( QObject* parent = 0 );
202  virtual ~MidiClient();
203 
204  void open( const QString deviceName = "default",
205  const int openMode = SND_SEQ_OPEN_DUPLEX,
206  const bool blockMode = false );
207  void open( snd_config_t* conf,
208  const QString deviceName = "default",
209  const int openMode = SND_SEQ_OPEN_DUPLEX,
210  const bool blockMode = false );
211  void close();
212  void startSequencerInput();
213  void stopSequencerInput();
214  MidiPort* createPort();
215  MidiQueue* createQueue();
216  MidiQueue* createQueue(QString const& name);
217  MidiQueue* getQueue();
218  MidiQueue* useQueue(int queue_id);
219  MidiQueue* useQueue(const QString& name);
220  MidiQueue* useQueue(MidiQueue* queue);
221  void portAttach(MidiPort* port);
222  void portDetach(MidiPort* port);
223  void detachAllPorts();
224  void addEventFilter(int evtype);
225  void output(SequencerEvent* ev, bool async = false, int timeout = -1);
226  void outputDirect(SequencerEvent* ev, bool async = false, int timeout = -1);
227  void outputBuffer(SequencerEvent* ev);
228  void drainOutput(bool async = false, int timeout = -1);
229  void synchronizeOutput();
230 
231  int getClientId();
232  snd_seq_type_t getSequencerType();
233  snd_seq_t* getHandle();
234  bool isOpened();
235 
236  size_t getOutputBufferSize();
237  void setOutputBufferSize(size_t newSize);
238  size_t getInputBufferSize();
239  void setInputBufferSize(size_t newSize);
240  QString getDeviceName();
241  int getOpenMode();
242  bool getBlockMode();
243  void setBlockMode(bool newValue);
244  QString getClientName();
245  QString getClientName(const int clientId);
246  void setClientName(QString const& newName);
247  bool getBroadcastFilter();
248  void setBroadcastFilter(bool newValue);
249  bool getErrorBounce();
250  void setErrorBounce(bool newValue);
251 
252  ClientInfo& getThisClientInfo();
253  void setThisClientInfo(const ClientInfo& val);
254  MidiPortList getMidiPorts() const;
255  ClientInfoList getAvailableClients();
256  PortInfoList getAvailableInputs();
257  PortInfoList getAvailableOutputs();
258  SystemInfo& getSystemInfo();
259  QList<int> getAvailableQueues();
260 
261  PoolInfo& getPoolInfo();
262  void setPoolInfo(const PoolInfo& info);
263  void setPoolInput(int size);
264  void setPoolOutput(int size);
265  void setPoolOutputRoom(int size);
266  void resetPoolInput();
267  void resetPoolOutput();
268  void dropInput();
269  void dropInputBuffer();
270  void dropOutput();
271  void dropOutputBuffer();
272  void removeEvents(const RemoveEvents* spec);
273  SequencerEvent* extractOutput();
274  int outputPending();
275  int inputPending(bool fetch);
276  int getQueueId(const QString& name);
277 
278  void addListener(QObject* listener);
279  void removeListener(QObject* listener);
280  void setEventsEnabled(const bool bEnabled);
281  bool getEventsEnabled() const;
282  void setHandler(SequencerEventHandler* handler);
283  bool parseAddress( const QString& straddr, snd_seq_addr& result );
284  void setRealTimeInput(bool enabled);
285  bool realTimeInputEnabled();
286 
287 signals:
289  void eventReceived(SequencerEvent* ev);
290 
291 protected:
292  void doEvents();
293  void applyClientInfo();
294  void readClients();
295  void freeClients();
296  void updateAvailablePorts();
297  PortInfoList filterPorts(unsigned int filter);
298 
299  /* low level public functions */
300  const char * _getDeviceName();
301  int getPollDescriptorsCount(short events);
302  int pollDescriptors(struct pollfd *pfds, unsigned int space, short events);
303  unsigned short pollDescriptorsRevents(struct pollfd *pfds, unsigned int nfds);
304 
305  /* mid level functions */
306  void _setClientName( const char *name );
307  int createSimplePort( const char *name,
308  unsigned int caps,
309  unsigned int type );
310  void deleteSimplePort( int port );
311  void connectFrom(int myport, int client, int port);
312  void connectTo(int myport, int client, int port);
313  void disconnectFrom(int myport, int client, int port);
314  void disconnectTo(int myport, int client, int port);
315 
316 private:
317  class SequencerInputThread;
318  class MidiClientPrivate;
319  MidiClientPrivate *d;
320 };
321 
322 #if SND_LIB_VERSION > 0x010004
323 DRUMSTICK_EXPORT QString getRuntimeALSALibraryVersion();
324 DRUMSTICK_EXPORT int getRuntimeALSALibraryNumber();
325 #endif
326 DRUMSTICK_EXPORT QString getRuntimeALSADriverVersion();
327 DRUMSTICK_EXPORT int getRuntimeALSADriverNumber();
328 
329 } /* namespace drumstick */
330 
333 #endif // DRUMSTICK_ALSACLIENT_H
QList< ClientInfo > ClientInfoList
List of sequencer client information.
Definition: alsaclient.h:98
This class manages event input from the ALSA sequencer.
Definition: alsaclient.cpp:185
The QObject class is the base class of all Qt objects.
Client information.
Definition: alsaclient.h:49
Base class for the event&#39;s hierarchy.
Definition: alsaevent.h:52
Queue management.
Definition: alsaqueue.h:187
QList< PortInfo > PortInfoList
List of port information objects.
Definition: alsaport.h:111
Client management.
Definition: alsaclient.h:197
Sequencer Pool information.
Definition: alsaclient.h:137
Classes managing ALSA Sequencer ports.
Sequencer events handler.
Definition: alsaclient.h:174
int getRuntimeALSADriverNumber()
Gets the runtime ALSA drivers version number.
Port management.
Definition: alsaport.h:118
Auxiliary class to remove events from an ALSA queue.
Definition: alsaevent.h:585
System information.
Definition: alsaclient.h:106
virtual ~SequencerEventHandler()
Destructor.
Definition: alsaclient.h:178
QString getRuntimeALSADriverVersion()
Gets the runtime ALSA drivers version string.
QList< MidiPort * > MidiPortList
List of Ports instances.
Definition: alsaport.h:214