File: alsaport.cpp

package info (click to toggle)
libdrumstick 2.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,876 kB
  • sloc: cpp: 25,685; xml: 122; sh: 14; makefile: 5
file content (1276 lines) | stat: -rw-r--r-- 27,030 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
/*
    MIDI Sequencer C++ library
    Copyright (C) 2006-2024, Pedro Lopez-Cabanillas <plcl@users.sf.net>

    This library is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <QMetaMethod>

#include "errorcheck.h"
#include <drumstick/alsaclient.h>
#include <drumstick/alsaqueue.h>

/**
 * @file alsaport.cpp
 * Implementation of classes managing ALSA Sequencer ports.
 */

namespace drumstick {
namespace ALSA {

/**
 * @addtogroup ALSAPort
 * @{
 *
 * Ports are the endpoints of the MIDI connections.
 *
 * Ports can be readable, writable, or both. They can be private for the owner
 * application, or can be subscribed by a third application.
 *
 * The ALSA sequencer readable ports are equivalent to the MIDI OUT ports in the
 * real world. Similarly, the writable ports are equivalent to the MIDI IN ones.
 *
 * Classes:
 *
 * PortInfo is a container to retrieve and change some properties about the ALSA
 * MIDI ports.
 *
 * MidiPort represents an ALSA MIDI port.
 *
 * @see https://www.alsa-project.org/alsa-doc/alsa-lib/group___seq_port.html
 * @}
 */

/**
 * Default constructor.
 */
PortInfo::PortInfo()
{
    snd_seq_port_info_malloc(&m_Info);
}

/**
 * Copy constructor
 * @param other Another PortInfo object reference
 */
PortInfo::PortInfo(const PortInfo& other)
{
    snd_seq_port_info_malloc(&m_Info);
    snd_seq_port_info_copy(m_Info, other.m_Info);
    m_ReadSubscribers = other.m_ReadSubscribers;
    m_WriteSubscribers = other.m_WriteSubscribers;
    m_ClientName = other.m_ClientName;
}

/**
 * Constructor
 * @param other An ALSA port info object pointer
 */
PortInfo::PortInfo(snd_seq_port_info_t* other)
{
    snd_seq_port_info_malloc(&m_Info);
    snd_seq_port_info_copy(m_Info, other);
}

/**
 * Constructor
 * @param seq A MidiClient instance
 * @param client An existing client number
 * @param port An existing port number
 */
PortInfo::PortInfo(MidiClient* seq, const int client, const int port)
{
    snd_seq_port_info_malloc(&m_Info);
    DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_get_any_port_info(seq->getHandle(), client, port, m_Info));
}

/**
 * Constructor
 * @param seq A MidiClient instance
 * @param port An existing port number
 */
PortInfo::PortInfo(MidiClient* seq, const int port)
{
    snd_seq_port_info_malloc(&m_Info);
    DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_get_port_info(seq->getHandle(), port, m_Info));
}

/**
 * Destructor
 */
PortInfo::~PortInfo()
{
    snd_seq_port_info_free(m_Info);
    freeSubscribers();
}

/**
 * Copy the current object
 * @return A pointer to the new object
 */
PortInfo* PortInfo::clone()
{
    return new PortInfo(m_Info);
}

/**
 * Assignment operator
 * @param other Another PortInfo object reference
 * @return This object
 */
PortInfo& PortInfo::operator=(const PortInfo& other)
{
    if (this == &other)
        return *this;
    snd_seq_port_info_copy(m_Info, other.m_Info);
    m_ReadSubscribers = other.m_ReadSubscribers;
    m_WriteSubscribers = other.m_WriteSubscribers;
    m_ClientName = other.m_ClientName;
    return *this;
}

/**
 * Gets the client number
 * @return The client number
 * @see setClient()
 */
int
PortInfo::getClient()
{
    return snd_seq_port_info_get_client(m_Info);
}

/**
 * Gets the port number
 * @return The port number
 * @see setPort()
 */
int
PortInfo::getPort()
{
    return snd_seq_port_info_get_port(m_Info);
}

/**
 * Gets the client name.
 * @return the client name
 * @see setClientName()
 */
QString PortInfo::getClientName() const
{
    return m_ClientName;
}

/**
 * Gets the address record for this port
 * @return A pointer to the address record
 * @see setAddr()
 */
const snd_seq_addr_t*
PortInfo::getAddr()
{
    return snd_seq_port_info_get_addr(m_Info);
}

/**
 * Gets the port name
 * @return The port name
 * @see setName()
 */
QString
PortInfo::getName()
{
    return QString(snd_seq_port_info_get_name(m_Info));
}

/**
 * Gets the capabilities bitmap
 * @return The capabilities bitmap
 * @see setCapability()
 */
unsigned int
PortInfo::getCapability()
{
    return snd_seq_port_info_get_capability(m_Info);
}

/**
 * Gets the port type
 * @return The port type
 * @see setType()
 */
unsigned int
PortInfo::getType()
{
    return snd_seq_port_info_get_type(m_Info);
}

/**
 * Gets the MIDI channels
 * @return The MIDI channels
 * @see setMidiChannels()
 */
int
PortInfo::getMidiChannels()
{
    return snd_seq_port_info_get_midi_channels(m_Info);
}

/**
 * Gets the MIDI voices
 * @return The MIDI voices
 * @see setMidiVoices()
 */
int
PortInfo::getMidiVoices()
{
    return snd_seq_port_info_get_midi_voices(m_Info);
}

/**
 * Gets the synth voices
 * @return The synth voices
 * @see setSynthVoices()
 */
int
PortInfo::getSynthVoices()
{
    return snd_seq_port_info_get_synth_voices(m_Info);
}

/**
 * Get the number of read subscriptions.
 * @return The number of read subscriptions.
 */
int
PortInfo::getReadUse()
{
    return snd_seq_port_info_get_read_use(m_Info);
}

/**
 * Gets the number of write subscriptions.
 * @return The number of write subscriptions.
 */
int
PortInfo::getWriteUse()
{
    return snd_seq_port_info_get_write_use(m_Info);
}

/**
 * Gets the port-specified mode.
 * @return The port-specified mode.
 * @see setPortSpecified()
 */
int
PortInfo::getPortSpecified()
{
    return snd_seq_port_info_get_port_specified(m_Info);
}

/**
 * Sets the client number
 * @param client The client number
 * @see getClient()
 */
void
PortInfo::setClient(int client)
{
    snd_seq_port_info_set_client(m_Info, client);
}

/**
 * Set the port number
 * @param port The port number
 * @see getPort()
 */
void
PortInfo::setPort(int port)
{
    snd_seq_port_info_set_port(m_Info, port);
}

/**
 * Sets the address record
 * @param addr An address record pointer
 * @see getAddr()
 */
void
PortInfo::setAddr(const snd_seq_addr_t* addr)
{
    snd_seq_port_info_set_addr(m_Info, addr);
}

/**
 * Sets the port name
 * @param name The port name
 * @see getName()
 */
void
PortInfo::setName(QString const& name)
{
    snd_seq_port_info_set_name(m_Info, name.toLocal8Bit().data());
}

/**
 * Sets the capability bitmap.
 *
 * Each port has the capability bitmaps to specify the access of
 * the port from other clients. The capability bit flags are:
 * <ul>
 * <li>SND_SEQ_PORT_CAP_READ Readable from this port</li>
 * <li>SND_SEQ_PORT_CAP_WRITE Writable to this port</li>
 * <li>SND_SEQ_PORT_CAP_DUPLEX Read/write duplex access is supported</li>
 * <li>SND_SEQ_PORT_CAP_SUBS_READ Read subscription is allowed</li>
 * <li>SND_SEQ_PORT_CAP_SUBS_WRITE Write subscription is allowed</li>
 * <li>SND_SEQ_PORT_CAP_NO_EXPORT Subscription management from 3rd clients is disallowed</li>
 * </ul>
 * @param capability The capability bitmap.
 * @see getCapability()
 */
void
PortInfo::setCapability(unsigned int capability)
{
    snd_seq_port_info_set_capability(m_Info, capability);
}

/**
 * Sets the port type.
 *
 * The port type is defined combining some of the type bit flags:
 * <ul>
 * <li>SND_SEQ_PORT_TYPE_SPECIFIC Hardware specific port</li>
 * <li>SND_SEQ_PORT_TYPE_MIDI_GENERIC Generic MIDI device</li>
 * <li>SND_SEQ_PORT_TYPE_MIDI_GM General MIDI compatible device</li>
 * <li>SND_SEQ_PORT_TYPE_MIDI_GM2 General MIDI 2 compatible device</li>
 * <li>SND_SEQ_PORT_TYPE_MIDI_GS GS compatible device</li>
 * <li>SND_SEQ_PORT_TYPE_MIDI_XG XG compatible device</li>
 * <li>SND_SEQ_PORT_TYPE_MIDI_MT32 MT-32 compatible device</li>
 * <li>SND_SEQ_PORT_TYPE_HARDWARE Implemented in hardware</li>
 * <li>SND_SEQ_PORT_TYPE_SOFTWARE Implemented in software</li>
 * <li>SND_SEQ_PORT_TYPE_SYNTHESIZER Generates sound</li>
 * <li>SND_SEQ_PORT_TYPE_PORT Connects to other device(s)</li>
 * <li>SND_SEQ_PORT_TYPE_APPLICATION Application (sequencer/editor)</li>
 * </ul>
 * @param type The port type bitmap
 * @see getType()
 */
void
PortInfo::setType(unsigned int type)
{
    snd_seq_port_info_set_type(m_Info, type);
}

/**
 * Set the MIDI channels
 * @param channels The MIDI channels
 * @see getMidiChannels()
 */
void
PortInfo::setMidiChannels(int channels)
{
    snd_seq_port_info_set_midi_channels(m_Info, channels);
}

/**
 * Sets the MIDI voices
 * @param voices The MIDI voices
 * @see getMidiVoices()
 */
void
PortInfo::setMidiVoices(int voices)
{
    snd_seq_port_info_set_midi_voices(m_Info, voices);
}

/**
 * Sets the synth voices
 * @param voices The synth voices
 * @see getSynthVoices()
 */
void
PortInfo::setSynthVoices(int voices)
{
    snd_seq_port_info_set_synth_voices(m_Info, voices);
}

/**
 * Sets the port-specified mode
 * @param val The port-specified mode.
 * @see getPortSpecified()
 */
void
PortInfo::setPortSpecified(int val)
{
    snd_seq_port_info_set_port_specified(m_Info, val);
}

/**
 * Gets the list of read subscribers
 * @return The list of read subscribers
 */
SubscribersList
PortInfo::getReadSubscribers() const
{
    return m_ReadSubscribers; // copy
}

/**
 * Gets the list of write subscribers
 * @return The list of write subscribers
 */
SubscribersList
PortInfo::getWriteSubscribers() const
{
    return m_WriteSubscribers; // copy
}

/**
 * Obtains the port subscribers lists
 * @param seq An opened MidiClient instance
 */
void
PortInfo::readSubscribers(MidiClient* seq)
{
    Subscriber subs;
    snd_seq_addr_t tmp;
    freeSubscribers();
    tmp.client = getClient();
    tmp.port = getPort();
    // Read subs
    subs.setType(SND_SEQ_QUERY_SUBS_READ);
    subs.setIndex(0);
    subs.setRoot(&tmp);
    while (snd_seq_query_port_subscribers(seq->getHandle(), subs.m_Info) >= 0)
    {
        m_ReadSubscribers.append(subs);
        subs.setIndex(subs.getIndex() + 1);
    }
    // Write subs
    subs.setType(SND_SEQ_QUERY_SUBS_WRITE);
    subs.setIndex(0);
    subs.setRoot(&tmp);
    while (snd_seq_query_port_subscribers(seq->getHandle(), subs.m_Info) >= 0)
    {
        m_WriteSubscribers.append(subs);
        subs.setIndex(subs.getIndex() + 1);
    }
}

/**
 * Releases the subscribers lists
 */
void
PortInfo::freeSubscribers()
{
    m_ReadSubscribers.clear();
    m_WriteSubscribers.clear();
}

/**
 * Sets the client name.
 * @see getClientName()
 * @param name Client name
 */
void PortInfo::setClientName(QString name)
{
    m_ClientName = name;
}

/**
 * Gets the size of the ALSA info object
 * @return The size of the object
 */
int
PortInfo::getSizeOfInfo() const
{
    return  snd_seq_port_info_sizeof();
}

/**
 * Gets the timestamping mode
 * @return The timestamping mode
 * @see setTimestamping()
 */
bool
PortInfo::getTimestamping()
{
    return (snd_seq_port_info_get_timestamping(m_Info) == 1);
}

/**
 * Gets the timestamping real mode
 * @return The timestamping real mode
 * @see setTimestampReal()
 */
bool
PortInfo::getTimestampReal()
{
    return (snd_seq_port_info_get_timestamp_real(m_Info) == 1);
}

/**
 * Gets the timestamping queue number
 * @return The timestamping queue number
 * @see setTimestampQueue()
 */
int
PortInfo::getTimestampQueue()
{
    return snd_seq_port_info_get_timestamp_queue(m_Info);
}

/**
 * Sets the timestamping mode
 * @param value The timestamping mode
 * @see getTimestamping()
 */
void
PortInfo::setTimestamping(bool value)
{
    snd_seq_port_info_set_timestamping(m_Info, value?1:0);
}

/**
 * Sets the timestamping real mode
 * @param value The timestamping real mode
 * @see getTimestampReal()
 */
void
PortInfo::setTimestampReal(bool value)
{
    snd_seq_port_info_set_timestamp_real(m_Info, value?1:0);
}

/**
 * Sets the timestamp queue number
 * @param queueId The timestamp queue number
 * @see getTimestampQueue()
 */
void
PortInfo::setTimestampQueue(int queueId)
{
    snd_seq_port_info_set_timestamp_queue(m_Info, queueId);
}


/**
 * Constructor
 * @param parent An optional parent object
 */
MidiPort::MidiPort( QObject* parent ) :
    QObject( parent ),
    m_MidiClient( nullptr ),
    m_Attached( false )
{}

/**
 * Destructor.
 *
 * All subscriptions are released.
 */
MidiPort::~MidiPort()
{
    unsubscribeAll();
    detach();
    freeSubscriptions();
}

/**
 * Gets the PortInfo object pointer
 * @return the PortInfo object pointer
 */
PortInfo*
MidiPort::getPortInfo()
{
    return &m_Info;
}

/**
 * Gets the list of susbcriptions
 * @return  The list of susbcriptions
 */
SubscriptionsList
MidiPort::getSubscriptions() const
{
    return m_Subscriptions;
}

/**
 * Releases the lists of subscriptions.
 */
void
MidiPort::freeSubscriptions()
{
    m_Subscriptions.clear();
}

/**
 * Sets the MidiClient
 * @param seq A MidiClient object pointer
 */
void
MidiPort::setMidiClient( MidiClient* seq )
{
    if (m_MidiClient != seq)
    {
        m_MidiClient = seq;
        Q_EMIT midiClientChanged( this, m_MidiClient );
        applyPortInfo();
    }
}

/**
 * Subscribe a Subscription object
 * @param subs A Subscription object pointer
 */
void
MidiPort::subscribe(Subscription* subs)
{
    static const QMetaMethod subscribedSignal = QMetaMethod::fromSignal(&MidiPort::subscribed);
    subs->subscribe(m_MidiClient);
    m_Subscriptions.append(*subs);
    if (isSignalConnected(subscribedSignal)) {
        Q_EMIT subscribed(this, subs->clone());
    }
}

/**
 * Unsubscribe a Subscription object
 * @param subs A Subscription object pointer
 */
void
MidiPort::unsubscribe( Subscription* subs )
{
    Subscription subs2;
    if (m_MidiClient == nullptr)
    {
        return;
    }
    subs->unsubscribe(m_MidiClient);
    SubscriptionsList::iterator it;
    for(it = m_Subscriptions.begin(); it != m_Subscriptions.end(); ++it)
    {
        subs2 = (*it);
        if ((subs2.getSender()->client == subs->getSender()->client) &&
                (subs2.getSender()->port == subs->getSender()->port) &&
                (subs2.getDest()->client == subs->getDest()->client) &&
                (subs2.getDest()->port == subs->getDest()->port))
        {
            m_Subscriptions.erase(it);
            break;
        }
    }
}

/**
 * Subscribe to another port destination
 * @param info A PortInfo object pointer
 */
void
MidiPort::subscribeTo( PortInfo* info )
{
    Subscription subs;
    subs.setSender(m_Info.getAddr());
    subs.setDest(info->getAddr());
    subscribe(&subs);
}

/**
 * Susbcribe to another port destination
 * @param client ALSA client number
 * @param port ALSA port number
 */
void
MidiPort::subscribeTo( int client, int port )
{
    Subscription subs;
    snd_seq_addr addr;
    addr.client = client;
    addr.port = port;
    subs.setSender(m_Info.getAddr());
    subs.setDest(&addr);
    subscribe(&subs);
}

/**
 * Subscribe to another port destination
 * @param name A string representing a client:port pair
 */
void
MidiPort::subscribeTo( QString const& name )
{
    Subscription subs;
    snd_seq_addr addr;
    if ((m_MidiClient != nullptr) && (m_MidiClient->getHandle() != nullptr))
    {
        subs.setSender(m_Info.getAddr());
        if (m_MidiClient->parseAddress(name, addr)) {
            subs.setDest(&addr);
            subscribe(&subs);
        }
    }
}

/**
 * Unsubscribe a destination port
 * @param name A string representing a client:port pair
 */
void
MidiPort::unsubscribeTo( QString const& name )
{
    Subscription subs;
    snd_seq_addr addr;
    if ((m_MidiClient != nullptr) && (m_MidiClient->getHandle() != nullptr))
    {
        subs.setSender(m_Info.getAddr());
        if (m_MidiClient->parseAddress(name, addr)) {
            subs.setDest(&addr);
            unsubscribe(&subs);
        }
    }
}

/**
 * Unsubscribe a destination port
 * @param port A PortInfo object pointer
 */
void
MidiPort::unsubscribeTo( PortInfo* port )
{
    Subscription subs;
    if ((m_MidiClient != nullptr) && (m_MidiClient->getHandle() != nullptr))
    {
        subs.setSender(m_Info.getAddr());
        subs.setDest(port->getAddr());
        unsubscribe(&subs);
    }
}

/**
 * Unsubscribe a destination port
 * @param addr An ALSA address record pointer
 */
void
MidiPort::unsubscribeTo( const snd_seq_addr_t* addr )
{
    Subscription subs;
    if ((m_MidiClient != nullptr) && (m_MidiClient->getHandle() != nullptr))
    {
        subs.setSender(m_Info.getAddr());
        subs.setDest(addr);
        unsubscribe(&subs);
    }
}

/**
 * Subscribe a source port
 * @param port A PortInfo object pointer
 */
void
MidiPort::subscribeFrom( PortInfo* port )
{
    Subscription subs;
    subs.setSender( port->getAddr() );
    subs.setDest( m_Info.getAddr() );
    subscribe(&subs);
}

/**
 * Subscribe a source port
 * @param client ALSA client number
 * @param port ALSA port number
 */
void
MidiPort::subscribeFrom( int client, int port )
{
    Subscription subs;
    snd_seq_addr addr;
    addr.client = client;
    addr.port = port;
    subs.setSender(&addr);
    subs.setDest(m_Info.getAddr());
    subscribe(&subs);
}

/**
 * Subscribe a source port
 * @param name A string representing a client:port pair
 */
void
MidiPort::subscribeFrom( QString const& name )
{
    Subscription subs;
    snd_seq_addr addr;
    if ((m_MidiClient != nullptr) && (m_MidiClient->getHandle() != nullptr))
    {
        if (m_MidiClient->parseAddress(name, addr)) {
            subs.setSender(&addr);
            subs.setDest(m_Info.getAddr());
            subscribe(&subs);
        }
    }
}

/**
 * Unsubscribe a source port
 * @param name A string representing a client:port pair
 */
void
MidiPort::unsubscribeFrom( QString const& name )
{
    Subscription subs;
    snd_seq_addr addr;
    if ((m_MidiClient != nullptr) && (m_MidiClient->getHandle() != nullptr))
    {
        if (m_MidiClient->parseAddress(name, addr)) {
            subs.setSender(&addr);
            subs.setDest(m_Info.getAddr());
            unsubscribe(&subs);
        }
    }
}

/**
 * Unsubscribe a source port
 * @param port A PortInfo object pointer
 */
void
MidiPort::unsubscribeFrom( PortInfo* port )
{
    Subscription subs;
    if ((m_MidiClient != nullptr) && (m_MidiClient->getHandle() != nullptr))
    {
        subs.setSender(port->getAddr());
        subs.setDest(m_Info.getAddr());
        unsubscribe(&subs);
    }
}

/**
 * Unsubscribe a source port
 * @param addr An ALSA address record pointer
 */
void
MidiPort::unsubscribeFrom( const snd_seq_addr_t* addr )
{
    Subscription subs;
    if ((m_MidiClient != nullptr) && (m_MidiClient->getHandle() != nullptr))
    {
        subs.setSender(addr);
        subs.setDest(m_Info.getAddr());
        unsubscribe(&subs);
    }
}

/**
 * Subscribe from the System:announce port
 */
void
MidiPort::subscribeFromAnnounce()
{
    subscribeFrom(SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE);
}

/**
 * Unsubscribe all subscriptions
 */
void
MidiPort::unsubscribeAll()
{
    if (m_MidiClient == nullptr) {
        return;
    }
    SubscriptionsList::Iterator it;
    for( it = m_Subscriptions.begin(); it != m_Subscriptions.end(); ++it) {
        Subscription s = (*it);
        s.unsubscribe(m_MidiClient);
    }
    m_Subscriptions.clear();
}

/**
 * Applies all the the delayed PortInfo changes to the MIDI port object
 */
void
MidiPort::applyPortInfo()
{
    if (m_Attached && (m_MidiClient != nullptr) && (m_MidiClient->isOpened()))
    {
        DRUMSTICK_ALSA_CHECK_WARNING(snd_seq_set_port_info( m_MidiClient->getHandle(),
        		                             m_Info.getPort(), m_Info.m_Info ));
    }
}

/**
 * Gets the port name
 * @return The port name
 */
QString
MidiPort::getPortName()
{
    return m_Info.getName();
}

/**
 * Sets the port name
 * @param newName The new port name
 */
void
MidiPort::setPortName( QString const& newName )
{
    m_Info.setName(newName);
    applyPortInfo();
}

/**
 * Gets the port number
 * @return The port number
 */
int
MidiPort::getPortId()
{
    return m_Info.getPort();
}

/**
 * Gets the port capabilities
 * @return The capabilities bitmap
 * @see PortInfo::getCapability()
 */
unsigned int
MidiPort::getCapability()
{
    return m_Info.getCapability();
}

/**
 * Sets the port capabilities
 * @param newValue The capabilities bitmap
 * @see PortInfo::setCapability()
 */
void
MidiPort::setCapability(unsigned int newValue)
{
    m_Info.setCapability(newValue);
    applyPortInfo();
}

/**
 * Gets the port type
 * @return The port type bitmap
 * @see PortInfo::getType()
 */
unsigned int
MidiPort::getPortType()
{
    return m_Info.getType();
}

/**
 * Sets the port type bitmap
 * @param newValue The port type flags bitmap
 * @see PortInfo::setType()
 */
void
MidiPort::setPortType( unsigned int newValue)
{
    m_Info.setType( newValue );
    applyPortInfo();
}

/**
 * Gets the MIDI channels
 * @return The MIDI channels
 */
int
MidiPort::getMidiChannels()
{
    return m_Info.getMidiChannels();
}

/**
 * Sets the MIDI channels
 * @param newValue The MIDI channels
 */
void
MidiPort::setMidiChannels(int newValue)
{
    m_Info.setMidiChannels( newValue );
    applyPortInfo();
}

/**
 * Gets the MIDI voices
 * @return The MIDI voices
 */
int
MidiPort::getMidiVoices()
{
    return m_Info.getMidiVoices();
}

/**
 * Sets the MIDI voices
 * @param newValue The MIDI voices
 */
void
MidiPort::setMidiVoices(int newValue)
{
    m_Info.setMidiVoices( newValue );
    applyPortInfo();
}

/**
 * Gets the synth voices
 * @return The synth voices
 */
int
MidiPort::getSynthVoices()
{
    return m_Info.getSynthVoices();
}

/**
 * Sets the synth voices
 * @param newValue The synth voices
 */
void
MidiPort::setSynthVoices(int newValue)
{
    m_Info.setSynthVoices( newValue );
    applyPortInfo();
}

/**
 * Gets the timestamping mode
 * @return The timestamping mode
 */
bool
MidiPort::getTimestamping()
{
	return m_Info.getTimestamping();
}

/**
 * Gets the timestamp real mode
 * @return The timestamp real mode
 */
bool
MidiPort::getTimestampReal()
{
	return m_Info.getTimestampReal();
}

/**
 * Gets the timestamp queue number
 * @return The timestamp queue number
 */
int
MidiPort::getTimestampQueue()
{
	return m_Info.getTimestampQueue();
}

/**
 * Sets the timestamping mode
 * @param value The timestamping mode
 */
void
MidiPort::setTimestamping(bool value)
{
	m_Info.setTimestamping(value);
	applyPortInfo();
}

/**
 * Sets the timestamp real mode
 * @param value The timestamp real mode
 */
void
MidiPort::setTimestampReal(bool value)
{
	m_Info.setTimestampReal(value);
	applyPortInfo();
}

/**
 * Sets the timestamp queue number
 * @param queueId The queue number
 */
void
MidiPort::setTimestampQueue(int queueId)
{
	m_Info.setTimestampQueue(queueId);
	applyPortInfo();
}

/**
 * Attach the port to a MidiClient instance
 * @param seq A MidiClient object pointer
 */
void
MidiPort::attach( MidiClient* seq )
{
    if (!m_Attached && (seq != nullptr)) {
        m_MidiClient = seq;
        m_MidiClient->portAttach(this);
        m_Attached = true;
        Q_EMIT attached(this);
    }
}

/**
 * Detach the port from any MidiClient instance previously attached
 */
void
MidiPort::detach()
{
    if (m_Attached && (m_MidiClient != nullptr)) {
        m_MidiClient->portDetach(this);
        m_Attached = false;
        Q_EMIT detached(this);
    }
}

/**
 * Update the subscribers list in the PortInfo member
 */
void
MidiPort::updateSubscribers()
{
    m_Info.readSubscribers(m_MidiClient);
}

/**
 * Gets the list of read subscribers
 * @return The list of read subscribers
 */
PortInfoList
MidiPort::getReadSubscribers()
{
    const SubscribersList subs(m_Info.getReadSubscribers());
    PortInfoList lst;
    SubscribersList::ConstIterator it;
    for(it = subs.constBegin(); it != subs.constEnd(); ++it) {
        Subscriber s = *it;
        int client = s.getAddr()->client;
        if ((client != SND_SEQ_CLIENT_SYSTEM) && (client != m_Info.getClient())) {
            int port = s.getAddr()->port;
            PortInfo p(m_MidiClient, client, port);
            if ((p.getCapability() & SND_SEQ_PORT_CAP_NO_EXPORT) == 0) {
            	p.setClientName(m_MidiClient->getClientName(client));
                lst << p;
            }
        }
    }
    return lst;
}

/**
 * Gets the list of write subscribers
 * @return The list of write subscribers
 */
PortInfoList
MidiPort::getWriteSubscribers()
{
    const SubscribersList subs(m_Info.getWriteSubscribers());
    PortInfoList lst;
    SubscribersList::ConstIterator it;
    for(it = subs.constBegin(); it != subs.constEnd(); ++it) {
        Subscriber s = *it;
        int client = s.getAddr()->client;
        if ((client != SND_SEQ_CLIENT_SYSTEM) && (client != m_Info.getClient())) {
            int port = s.getAddr()->port;
            PortInfo p(m_MidiClient, client, port);
            if ((p.getCapability() & SND_SEQ_PORT_CAP_NO_EXPORT) == 0) {
            	p.setClientName(m_MidiClient->getClientName(client));
                lst << p;
            }
        }
    }
    return lst;
}

/**
 * Checks if the provided address is included in the port list
 * @param addr ALSA address record pointer
 * @param lst List of port information containers
 * @return True if the address is found
 */
bool
MidiPort::containsAddress(const snd_seq_addr_t* addr, const PortInfoList& lst)
{
    PortInfoList::ConstIterator i;
    for( i = lst.begin(); i != lst.end(); ++i) {
        PortInfo p = *i;
        if ((p.getAddr()->client == addr->client) &&
            (p.getAddr()->port == addr->port)) {
            return true;
        }
    }
    return false;
}

/**
 * Update the write subscriptions
 * @param ports List of writable ports to be subscribed
 */
void
MidiPort::updateConnectionsTo(const PortInfoList& ports)
{
    PortInfoList subs(getReadSubscribers());
    PortInfoList::ConstIterator i;
    for (i = subs.constBegin(); i != subs.constEnd(); ++i) {
        PortInfo s = *i;
        if (!containsAddress(s.getAddr(), ports)) {
            unsubscribeTo(s.getAddr());
        }
    }
    for (i = ports.constBegin(); i != ports.constEnd(); ++i) {
        PortInfo p = *i;
        if (!containsAddress(p.getAddr(), subs)) {
            subscribeTo(&p);
        }
    }
}

/**
 * Update the read susbcriptions
 * @param ports List of readable ports to be subscribed
 */
void
MidiPort::updateConnectionsFrom(const PortInfoList& ports)
{
    PortInfoList subs(getWriteSubscribers());
    PortInfoList::ConstIterator i;
    for (i = subs.constBegin(); i != subs.constEnd(); ++i) {
        PortInfo s = *i;
        if (!containsAddress(s.getAddr(), ports)) {
            unsubscribeFrom(s.getAddr());
        }
    }
    for (i = ports.constBegin(); i != ports.constEnd(); ++i) {
        PortInfo p = *i;
        if (!containsAddress(p.getAddr(), subs)) {
            subscribeFrom(&p);
        }
    }
}

} // namespace ALSA
} // namespace drumstick