drumstick  1.0.2
maccommon.cpp
00001 /*
00002     Drumstick RT (realtime MIDI In/Out)
00003     Copyright (C) 2009-2015 Pedro Lopez-Cabanillas <plcl@users.sf.net>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License along
00016     with this program; if not, write to the Free Software Foundation, Inc.,
00017     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 */
00019 
00020 #include "maccommon.h"
00021 
00022 #if QT_VERSION < QT_VERSION_CHECK(5,2,0)
00023     QString CFStringToQString(CFStringRef str)
00024     {
00025         if (!str)
00026             return QString();
00027         CFIndex length = CFStringGetLength(str);
00028         const UniChar *chars = CFStringGetCharactersPtr(str);
00029         if (chars)
00030             return QString(reinterpret_cast<const QChar *>(chars), length);
00031         QVarLengthArray<UniChar> buffer(length);
00032         CFStringGetCharacters(str, CFRangeMake(0, length), buffer.data());
00033         return QString(reinterpret_cast<const QChar *>(buffer.constData()), length);
00034     }
00035 #endif
00036 
00037 QString getEndpointName(MIDIEndpointRef endpoint)
00038 {
00039     QString result;
00040     CFStringRef str = 0;
00041     MIDIObjectGetStringProperty (endpoint, kMIDIPropertyName, &str);
00042     if (str != 0) {
00043         result = QString::fromCFString(str);
00044         CFRelease(str);
00045         str = 0;
00046     }
00047     MIDIEntityRef entity = 0;
00048     MIDIEndpointGetEntity(endpoint, &entity);
00049     if (entity == 0)
00050         return result;
00051     if (result.isEmpty()) {
00052         MIDIObjectGetStringProperty (entity, kMIDIPropertyName, &str);
00053         if (str != 0) {
00054             result = QString::fromCFString(str);
00055             CFRelease(str);
00056             str = 0;
00057         }
00058     }
00059     MIDIDeviceRef device = 0;
00060     MIDIEntityGetDevice (entity, &device);
00061     if (device == 0)
00062         return result;
00063     MIDIObjectGetStringProperty (device, kMIDIPropertyName, &str);
00064     if (str != 0) {
00065         QString s = QString::fromCFString(str);
00066         CFRelease (str);
00067         str = 0;
00068         if (!result.startsWith(s, Qt::CaseInsensitive) )
00069             result = (s + ' ' + result).trimmed();
00070     }
00071     return result;
00072 }
00073 
00074 /* QString getEndpointName(MIDIEndpointRef endpoint)
00075 {
00076     QString result;
00077     CFStringRef str = 0;
00078     MIDIObjectGetStringProperty (endpoint, kMIDIPropertyName, &str);
00079     if (str != 0) {
00080         result = QString::fromCFString(str);
00081         CFRelease(str);
00082         str = 0;
00083     }
00084     MIDIEntityRef entity = 0;
00085     MIDIEndpointGetEntity(endpoint, &entity);
00086     if (entity == 0)
00087         return result;
00088     if (result.isEmpty()) {
00089         MIDIObjectGetStringProperty (entity, kMIDIPropertyName, &str);
00090         if (str != 0) {
00091             result = QString::fromCFString(str);
00092             CFRelease(str);
00093             str = 0;
00094         }
00095     }
00096     MIDIDeviceRef device = 0;
00097     MIDIEntityGetDevice (entity, &device);
00098     if (device == 0)
00099         return result;
00100     MIDIObjectGetStringProperty (device, kMIDIPropertyName, &str);
00101     if (str != 0) {
00102         QString s =QString::fromCFString(str);
00103         CFRelease (str);
00104         str = 0;
00105         if (!result.startsWith(s, Qt::CaseInsensitive) )
00106             result = (s + ' ' + result).trimmed();
00107     }
00108     return result;
00109 }
00110 */