drumstick  1.1.0
alsaclient.h
Go to the documentation of this file.
1 /*
2  MIDI Sequencer C++ library
3  Copyright (C) 2006-2016, 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 
36 namespace drumstick {
37 
38 class MidiQueue;
39 class MidiClient;
40 class SequencerEvent;
41 class SequencerInputThread;
42 class RemoveEvents;
43 
50 class DRUMSTICK_EXPORT ClientInfo
51 {
52  friend class MidiClient;
53 
54 public:
55  ClientInfo();
56  ClientInfo(const ClientInfo& other);
57  ClientInfo(snd_seq_client_info_t* other);
58  ClientInfo(MidiClient* seq, int id);
59  virtual ~ClientInfo();
60  ClientInfo* clone();
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 
84 protected:
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 
91 private:
92  snd_seq_client_info_t* m_Info;
93  PortInfoList m_Ports;
94 };
95 
99 typedef QList<ClientInfo> ClientInfoList;
100 
107 class DRUMSTICK_EXPORT SystemInfo
108 {
109  friend class MidiClient;
110 
111 public:
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 
128 private:
129  snd_seq_system_info_t* m_Info;
130 };
131 
138 class DRUMSTICK_EXPORT PoolInfo
139 {
140  friend class MidiClient;
141 
142 public:
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 
162 private:
163  snd_seq_client_pool_t* m_Info;
164 };
165 
175 class DRUMSTICK_EXPORT SequencerEventHandler
176 {
177 public:
180 
190  virtual void handleSequencerEvent(SequencerEvent* ev) = 0;
191 };
192 
198 class DRUMSTICK_EXPORT MidiClient : public QObject
199 {
200  Q_OBJECT
201 public:
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();
215  MidiPort* createPort();
216  MidiQueue* createQueue();
217  MidiQueue* createQueue(QString const& name);
218  MidiQueue* getQueue();
219  MidiQueue* useQueue(int queue_id);
220  MidiQueue* useQueue(const QString& name);
221  MidiQueue* useQueue(MidiQueue* queue);
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);
228  void outputBuffer(SequencerEvent* ev);
229  void drainOutput(bool async = false, int timeout = -1);
230  void synchronizeOutput();
231 
232  int getClientId();
233  snd_seq_type_t getSequencerType();
234  snd_seq_t* getHandle();
235  bool isOpened();
236 
237  size_t getOutputBufferSize();
238  void setOutputBufferSize(size_t newSize);
239  size_t getInputBufferSize();
240  void setInputBufferSize(size_t newSize);
241  QString getDeviceName();
242  int getOpenMode();
243  bool getBlockMode();
244  void setBlockMode(bool newValue);
245  QString getClientName();
246  QString getClientName(const int clientId);
247  void setClientName(QString const& newName);
248  bool getBroadcastFilter();
249  void setBroadcastFilter(bool newValue);
250  bool getErrorBounce();
251  void setErrorBounce(bool newValue);
252 
253  ClientInfo& getThisClientInfo();
254  void setThisClientInfo(const ClientInfo& val);
255  MidiPortList getMidiPorts() const;
256  ClientInfoList getAvailableClients();
257  PortInfoList getAvailableInputs();
258  PortInfoList getAvailableOutputs();
259  SystemInfo& getSystemInfo();
260  QList<int> getAvailableQueues();
261 
262  PoolInfo& getPoolInfo();
263  void setPoolInfo(const PoolInfo& info);
264  void setPoolInput(int size);
265  void setPoolOutput(int size);
266  void setPoolOutputRoom(int size);
267  void resetPoolInput();
268  void resetPoolOutput();
269  void dropInput();
270  void dropInputBuffer();
271  void dropOutput();
272  void dropOutputBuffer();
273  void removeEvents(const RemoveEvents* spec);
274  SequencerEvent* extractOutput();
275  int outputPending();
276  int inputPending(bool fetch);
277  int getQueueId(const QString& name);
278 
279  void addListener(QObject* listener);
280  void removeListener(QObject* listener);
281  void setEventsEnabled(const bool bEnabled);
282  bool getEventsEnabled() const;
283  void setHandler(SequencerEventHandler* handler);
284  bool parseAddress( const QString& straddr, snd_seq_addr& result );
285  void setRealTimeInput(bool enabled);
286  bool realTimeInputEnabled();
287 
288 signals:
290  void eventReceived(SequencerEvent* ev);
291 
292 protected:
293  void doEvents();
294  void applyClientInfo();
295  void readClients();
296  void freeClients();
297  void updateAvailablePorts();
298  PortInfoList filterPorts(unsigned int filter);
299 
300  /* low level public functions */
301  const char * _getDeviceName();
302  int getPollDescriptorsCount(short events);
303  int pollDescriptors(struct pollfd *pfds, unsigned int space, short events);
304  unsigned short pollDescriptorsRevents(struct pollfd *pfds, unsigned int nfds);
305 
306  /* mid level functions */
307  void _setClientName( const char *name );
308  int createSimplePort( const char *name,
309  unsigned int caps,
310  unsigned int type );
311  void deleteSimplePort( int port );
312  void connectFrom(int myport, int client, int port);
313  void connectTo(int myport, int client, int port);
314  void disconnectFrom(int myport, int client, int port);
315  void disconnectTo(int myport, int client, int port);
316 
317 private:
318  class SequencerInputThread;
319  class MidiClientPrivate;
320  MidiClientPrivate *d;
321 };
322 
323 #if SND_LIB_VERSION > 0x010004
324 DRUMSTICK_EXPORT QString getRuntimeALSALibraryVersion();
325 DRUMSTICK_EXPORT int getRuntimeALSALibraryNumber();
326 #endif
327 DRUMSTICK_EXPORT QString getRuntimeALSADriverVersion();
328 DRUMSTICK_EXPORT int getRuntimeALSADriverNumber();
329 
330 } /* namespace drumstick */
331 
334 #endif // DRUMSTICK_ALSACLIENT_H