Clementine-audio-player-Mac.../src/devices/macdevicelister.mm

349 lines
10 KiB
Plaintext
Raw Normal View History

#include "macdevicelister.h"
#include <CoreFoundation/CFRunLoop.h>
#include <DiskArbitration/DiskArbitration.h>
2010-07-24 20:34:22 +02:00
#include <IOKit/kext/KextManager.h>
#include <IOKit/usb/IOUSBLib.h>
#import <AppKit/NSWorkspace.h>
2010-07-24 20:34:22 +02:00
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSNotification.h>
2010-07-24 20:34:22 +02:00
#import <Foundation/NSPathUtilities.h>
#import <Foundation/NSString.h>
2010-07-24 20:34:22 +02:00
#import <Foundation/NSURL.h>
#include <QtDebug>
#include <QString>
#include <QStringList>
2010-07-24 20:34:22 +02:00
MacDeviceLister::MacDeviceLister() {
}
2010-07-24 20:34:22 +02:00
MacDeviceLister::~MacDeviceLister() {
2010-07-25 03:29:22 +02:00
CFRelease(loop_session_);
}
2010-07-24 20:34:22 +02:00
void MacDeviceLister::Init() {
[[NSAutoreleasePool alloc] init];
CFRunLoopRef run_loop = CFRunLoopGetCurrent();
2010-07-25 03:29:22 +02:00
loop_session_ = DASessionCreate(kCFAllocatorDefault);
2010-07-24 20:34:22 +02:00
DARegisterDiskAppearedCallback(
2010-07-25 03:29:22 +02:00
loop_session_, kDADiskDescriptionMatchVolumeMountable, &DiskAddedCallback, reinterpret_cast<void*>(this));
2010-07-24 20:34:22 +02:00
DARegisterDiskDisappearedCallback(
2010-07-25 03:29:22 +02:00
loop_session_, NULL, &DiskRemovedCallback, reinterpret_cast<void*>(this));
DASessionScheduleWithRunLoop(loop_session_, run_loop, kCFRunLoopDefaultMode);
2010-07-24 20:34:22 +02:00
CFRunLoopRun();
}
2010-07-24 20:34:22 +02:00
// IOKit helpers.
namespace {
2010-07-24 20:34:22 +02:00
CFTypeRef GetUSBRegistryEntry(io_object_t device, CFStringRef key) {
io_iterator_t it;
if (IORegistryEntryGetParentIterator(device, kIOServicePlane, &it) == KERN_SUCCESS) {
io_object_t next;
while ((next = IOIteratorNext(it))) {
CFTypeRef registry_entry = (CFStringRef)IORegistryEntryCreateCFProperty(
next, key, kCFAllocatorDefault, 0);
if (registry_entry) {
IOObjectRelease(next);
IOObjectRelease(it);
return registry_entry;
}
2010-07-24 21:02:49 +02:00
CFTypeRef ret = GetUSBRegistryEntry(next, key);
if (ret) {
IOObjectRelease(next);
IOObjectRelease(it);
return ret;
}
IOObjectRelease(next);
2010-07-24 20:34:22 +02:00
}
}
2010-07-24 20:34:22 +02:00
IOObjectRelease(it);
return NULL;
}
2010-07-24 20:34:22 +02:00
QString GetUSBRegistryEntryString(io_object_t device, CFStringRef key) {
CFStringRef registry_string = (CFStringRef)GetUSBRegistryEntry(device, key);
if (registry_string) {
QString ret = QString::fromUtf8([(NSString*)registry_string UTF8String]);
CFRelease(registry_string);
return ret;
}
return NULL;
}
2010-07-24 21:02:49 +02:00
quint64 GetUSBRegistryEntryInt64(io_object_t device, CFStringRef key) {
2010-07-24 20:34:22 +02:00
CFNumberRef registry_num = (CFNumberRef)GetUSBRegistryEntry(device, key);
if (registry_num) {
qint64 ret = -1;
Boolean result = CFNumberGetValue(registry_num, kCFNumberLongLongType, &ret);
if (!result || ret < 0) {
return 0;
} else {
return ret;
}
}
return 0;
}
2010-07-24 20:34:22 +02:00
NSObject* GetPropertyForDevice(io_object_t device, NSString* key) {
CFMutableDictionaryRef properties;
kern_return_t ret = IORegistryEntryCreateCFProperties(
device, &properties, kCFAllocatorDefault, 0);
2010-07-24 20:34:22 +02:00
if (ret != KERN_SUCCESS) {
return nil;
}
2010-07-24 20:34:22 +02:00
NSDictionary* dict = (NSDictionary*)properties;
NSObject* prop = [dict objectForKey:key];
if (prop) {
return prop;
}
2010-07-24 20:34:22 +02:00
io_object_t parent;
ret = IORegistryEntryGetParentEntry(device, kIOServicePlane, &parent);
if (ret == KERN_SUCCESS) {
return GetPropertyForDevice(parent, key);
}
2010-07-24 20:34:22 +02:00
return nil;
}
2010-07-24 20:34:22 +02:00
QString GetIconForDevice(io_object_t device) {
NSDictionary* media_icon = (NSDictionary*)GetPropertyForDevice(device, @"IOMediaIcon");
if (media_icon) {
NSString* bundle = (NSString*)[media_icon objectForKey:@"CFBundleIdentifier"];
NSString* file = (NSString*)[media_icon objectForKey:@"IOBundleResourceFile"];
NSURL* bundle_url = (NSURL*)KextManagerCreateURLForBundleIdentifier(
kCFAllocatorDefault, (CFStringRef)bundle);
QString path = QString::fromUtf8([[bundle_url path] UTF8String]);
path += "/Contents/Resources/";
path += [file UTF8String];
return path;
}
return QString();
}
2010-07-24 20:34:22 +02:00
QString GetSerialForDevice(io_object_t device) {
return QString(
"USB/" + GetUSBRegistryEntryString(device, CFSTR(kUSBSerialNumberString)));
}
2010-07-24 21:02:49 +02:00
QString FindDeviceProperty(const QString& bsd_name, CFStringRef property) {
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
DADiskRef disk = DADiskCreateFromBSDName(
kCFAllocatorDefault, session, bsd_name.toAscii().constData());
io_object_t device = DADiskCopyIOMedia(disk);
QString ret = GetUSBRegistryEntryString(device, property);
IOObjectRelease(device);
CFRelease(disk);
CFRelease(session);
return ret;
}
2010-07-24 20:34:22 +02:00
}
2010-07-24 20:34:22 +02:00
void MacDeviceLister::DiskAddedCallback(DADiskRef disk, void* context) {
MacDeviceLister* me = reinterpret_cast<MacDeviceLister*>(context);
NSDictionary* properties = (NSDictionary*)DADiskCopyDescription(disk);
NSURL* volume_path =
[[properties objectForKey:(NSString*)kDADiskDescriptionVolumePathKey] copy];
if (volume_path) {
io_object_t device = DADiskCopyIOMedia(disk);
QString vendor = GetUSBRegistryEntryString(device, CFSTR(kUSBVendorString));
QString product = GetUSBRegistryEntryString(device, CFSTR(kUSBProductString));
CFMutableDictionaryRef properties;
kern_return_t ret = IORegistryEntryCreateCFProperties(
device, &properties, kCFAllocatorDefault, 0);
if (ret == KERN_SUCCESS) {
NSDictionary* dict = (NSDictionary*)properties;
if ([[dict objectForKey:@"Removable"] intValue] == 1) {
QString serial = GetSerialForDevice(device);
me->current_devices_[serial] = QString(DADiskGetBSDName(disk));
emit me->DeviceAdded(serial);
}
}
2010-07-24 20:34:22 +02:00
IOObjectRelease(device);
}
}
2010-07-24 20:34:22 +02:00
void MacDeviceLister::DiskRemovedCallback(DADiskRef disk, void* context) {
MacDeviceLister* me = reinterpret_cast<MacDeviceLister*>(context);
// We cannot access the USB tree when the disk is removed but we still get
// the BSD disk name.
for (QMap<QString, QString>::iterator it = me->current_devices_.begin();
it != me->current_devices_.end(); ++it) {
2010-07-24 21:02:49 +02:00
if (it.value() == QString::fromLocal8Bit(DADiskGetBSDName(disk))) {
2010-07-24 20:34:22 +02:00
emit me->DeviceRemoved(it.key());
me->current_devices_.erase(it);
break;
}
}
}
2010-07-24 20:34:22 +02:00
QString MacDeviceLister::MakeFriendlyName(const QString& serial) {
QString bsd_name = current_devices_[serial];
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
DADiskRef disk = DADiskCreateFromBSDName(
kCFAllocatorDefault, session, bsd_name.toAscii().constData());
2010-07-24 20:34:22 +02:00
io_object_t device = DADiskCopyIOMedia(disk);
QString vendor = GetUSBRegistryEntryString(device, CFSTR(kUSBVendorString));
QString product = GetUSBRegistryEntryString(device, CFSTR(kUSBProductString));
IOObjectRelease(device);
2010-07-24 20:34:22 +02:00
CFRelease(disk);
CFRelease(session);
2010-07-24 21:02:49 +02:00
if (vendor.isEmpty()) {
return product;
}
2010-07-24 20:34:22 +02:00
return vendor + " " + product;
}
2010-07-24 20:34:22 +02:00
QUrl MacDeviceLister::MakeDeviceUrl(const QString& serial) {
QString bsd_name = current_devices_[serial];
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
DADiskRef disk = DADiskCreateFromBSDName(
kCFAllocatorDefault, session, bsd_name.toAscii().constData());
NSDictionary* properties = (NSDictionary*)DADiskCopyDescription(disk);
NSURL* volume_path =
[[properties objectForKey:(NSString*)kDADiskDescriptionVolumePathKey] copy];
QString path = [[volume_path path] UTF8String];
2010-07-24 21:02:49 +02:00
QUrl ret = MakeUrlFromLocalPath(path);
2010-07-24 20:34:22 +02:00
CFRelease(disk);
CFRelease(session);
return ret;
}
2010-07-24 20:34:22 +02:00
QStringList MacDeviceLister::DeviceUniqueIDs() {
return current_devices_.keys();
}
2010-07-24 20:34:22 +02:00
QStringList MacDeviceLister::DeviceIcons(const QString& serial) {
QString bsd_name = current_devices_[serial];
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
DADiskRef disk = DADiskCreateFromBSDName(
kCFAllocatorDefault, session, bsd_name.toAscii().constData());
2010-07-24 20:34:22 +02:00
io_object_t device = DADiskCopyIOMedia(disk);
QString icon = GetIconForDevice(device);
IOObjectRelease(device);
CFRelease(disk);
CFRelease(session);
QStringList ret;
if (!icon.isEmpty()) {
ret << icon;
}
2010-07-24 20:34:22 +02:00
return ret;
}
2010-07-24 20:34:22 +02:00
QString MacDeviceLister::DeviceManufacturer(const QString& serial){
2010-07-24 21:02:49 +02:00
return FindDeviceProperty(current_devices_[serial], CFSTR(kUSBVendorString));
2010-07-24 20:34:22 +02:00
}
2010-07-24 20:34:22 +02:00
QString MacDeviceLister::DeviceModel(const QString& serial){
2010-07-24 21:02:49 +02:00
return FindDeviceProperty(current_devices_[serial], CFSTR(kUSBProductString));
}
2010-07-24 20:34:22 +02:00
quint64 MacDeviceLister::DeviceCapacity(const QString& serial){
QString bsd_name = current_devices_[serial];
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
DADiskRef disk = DADiskCreateFromBSDName(
kCFAllocatorDefault, session, bsd_name.toAscii().constData());
2010-07-24 20:34:22 +02:00
io_object_t device = DADiskCopyIOMedia(disk);
2010-07-24 20:34:22 +02:00
NSNumber* capacity = (NSNumber*)GetPropertyForDevice(device, @"Size");
2010-07-24 20:34:22 +02:00
quint64 ret = [capacity unsignedLongLongValue];
2010-07-24 20:34:22 +02:00
IOObjectRelease(device);
2010-07-24 20:34:22 +02:00
CFRelease(disk);
CFRelease(session);
2010-07-24 20:34:22 +02:00
return ret;
}
2010-07-24 20:34:22 +02:00
quint64 MacDeviceLister::DeviceFreeSpace(const QString& serial){
QString bsd_name = current_devices_[serial];
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
DADiskRef disk = DADiskCreateFromBSDName(
kCFAllocatorDefault, session, bsd_name.toAscii().constData());
NSDictionary* properties = (NSDictionary*)DADiskCopyDescription(disk);
NSURL* volume_path =
[[properties objectForKey:(NSString*)kDADiskDescriptionVolumePathKey] copy];
FSRef ref;
OSStatus err = FSPathMakeRef(
(const UInt8*)[[volume_path path] fileSystemRepresentation], &ref, NULL);
if (err == noErr) {
FSCatalogInfo catalog;
err = FSGetCatalogInfo(&ref, kFSCatInfoVolume, &catalog, NULL, NULL, NULL);
if (err == noErr) {
FSVolumeRefNum volume_ref = catalog.volume;
FSVolumeInfo info;
err = FSGetVolumeInfo(
volume_ref, 0, NULL, kFSVolInfoSizes, &info, NULL, NULL);
UInt64 free_bytes = info.freeBytes;
return free_bytes;
}
}
return 0;
}
QVariantMap MacDeviceLister::DeviceHardwareInfo(const QString& id){return QVariantMap();}
2010-07-25 03:29:22 +02:00
void MacDeviceLister::UnmountDevice(const QString& serial) {
QString bsd_name = current_devices_[serial];
DADiskRef disk = DADiskCreateFromBSDName(
kCFAllocatorDefault, loop_session_, bsd_name.toAscii().constData());
DADiskUnmount(disk, kDADiskUnmountOptionDefault, &DiskUnmountCallback, this);
CFRelease(disk);
}
void MacDeviceLister::DiskUnmountCallback(
DADiskRef disk, DADissenterRef dissenter, void* context) {
MacDeviceLister* me = reinterpret_cast<MacDeviceLister*>(context);
if (dissenter) {
qDebug() << "Another app blocked the unmount";
} else {
DiskRemovedCallback(disk, context);
}
}