From 47a171fca7fa053fe14417a89bd6a451de701a25 Mon Sep 17 00:00:00 2001 From: John Maguire Date: Thu, 11 Aug 2011 21:37:09 +0200 Subject: [PATCH] Detect cd insertion on mac. --- src/devices/cddadevice.cpp | 2 +- src/devices/devicemanager.cpp | 3 +++ src/devices/macdevicelister.h | 4 ++++ src/devices/macdevicelister.mm | 36 ++++++++++++++++++++++++++++++++-- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/devices/cddadevice.cpp b/src/devices/cddadevice.cpp index e4d56f199..9a68dcfa3 100644 --- a/src/devices/cddadevice.cpp +++ b/src/devices/cddadevice.cpp @@ -42,7 +42,7 @@ CddaDevice::~CddaDevice(){ void CddaDevice::Init() { QMutexLocker locker(&mutex_init_); - cdio_ = cdio_open (unique_id_.toLocal8Bit().constData(), DRIVER_DEVICE); + cdio_ = cdio_open (url_.path().toLocal8Bit().constData(), DRIVER_DEVICE); if (cdio_ == NULL) { return; } diff --git a/src/devices/devicemanager.cpp b/src/devices/devicemanager.cpp index ff1958041..a7994d046 100644 --- a/src/devices/devicemanager.cpp +++ b/src/devices/devicemanager.cpp @@ -186,7 +186,10 @@ DeviceManager::DeviceManager(BackgroundThread* database, connected_devices_model_ = new DeviceStateFilterModel(this); connected_devices_model_->setSourceModel(this); + // CD devices are detected via the DiskArbitration framework instead on Darwin. +#ifndef Q_OS_DARWIN AddLister(new CddaLister); +#endif #ifdef HAVE_DEVICEKIT AddLister(new DeviceKitLister); #endif diff --git a/src/devices/macdevicelister.h b/src/devices/macdevicelister.h index bc2c425ec..75cc99080 100644 --- a/src/devices/macdevicelister.h +++ b/src/devices/macdevicelister.h @@ -4,6 +4,7 @@ #include "devicelister.h" #include +#include #include #include @@ -64,11 +65,14 @@ class MacDeviceLister : public DeviceLister { quint64 GetFreeSpace(const QUrl& url); quint64 GetCapacity(const QUrl& url); + bool IsCDDevice(const QString& serial) const; + DASessionRef loop_session_; CFRunLoopRef run_loop_; QMap current_devices_; QMap mtp_devices_; + QSet cd_devices_; QMutex libmtp_mutex_; diff --git a/src/devices/macdevicelister.mm b/src/devices/macdevicelister.mm index 1d945b82a..6019f4115 100644 --- a/src/devices/macdevicelister.mm +++ b/src/devices/macdevicelister.mm @@ -25,6 +25,7 @@ #include #include #include +#include #import #import @@ -335,6 +336,16 @@ void MacDeviceLister::DiskAddedCallback(DADiskRef disk, void* context) { MacDeviceLister* me = reinterpret_cast(context); NSDictionary* properties = (NSDictionary*)DADiskCopyDescription(disk); + + NSString* kind = [properties objectForKey:(NSString*)kDADiskDescriptionMediaKindKey]; + if (kind && strcmp([kind UTF8String], kIOCDMediaClass) == 0) { + // CD inserted. + QString bsd_name = QString::fromAscii(DADiskGetBSDName(disk)); + me->cd_devices_ << bsd_name; + emit me->DeviceAdded(bsd_name); + return; + } + NSURL* volume_path = [[properties objectForKey:(NSString*)kDADiskDescriptionVolumePathKey] copy]; @@ -583,7 +594,7 @@ void MacDeviceLister::USBDeviceRemovedCallback(void* refcon, io_iterator_t it) { device.product = QString::fromUtf8([product UTF8String]); device.vendor_id = [vendor_id unsignedShortValue]; device.product_id = [product_id unsignedShortValue]; - + me->RemovedMTPDevice(serial); } } @@ -611,6 +622,10 @@ bool IsMTPSerial(const QString& serial) { return serial.startsWith("MTP"); } +bool MacDeviceLister::IsCDDevice(const QString& serial) const { + return cd_devices_.contains(serial); +} + QString MacDeviceLister::MakeFriendlyName(const QString& serial) { if (IsMTPSerial(serial)) { const MTPDevice& device = mtp_devices_[serial]; @@ -620,12 +635,25 @@ QString MacDeviceLister::MakeFriendlyName(const QString& serial) { return device.vendor + " " + device.product; } } - QString bsd_name = current_devices_[serial]; + + QString bsd_name = IsCDDevice(serial) ? *cd_devices_.find(serial) : current_devices_[serial]; DASessionRef session = DASessionCreate(kCFAllocatorDefault); DADiskRef disk = DADiskCreateFromBSDName( kCFAllocatorDefault, session, bsd_name.toAscii().constData()); io_object_t device = DADiskCopyIOMedia(disk); + + if (IsCDDevice(serial)) { + NSDictionary* properties = (NSDictionary*)DADiskCopyDescription(disk); + NSLog(@"%@", properties); + + NSString* device_name = (NSString*)[properties objectForKey:(NSString*)kDADiskDescriptionMediaNameKey]; + + CFRelease(disk); + CFRelease(session); + return QString::fromUtf8([device_name UTF8String]); + } + QString vendor = GetUSBRegistryEntryString(device, CFSTR(kUSBVendorString)); QString product = GetUSBRegistryEntryString(device, CFSTR(kUSBProductString)); IOObjectRelease(device); @@ -653,6 +681,10 @@ QList MacDeviceLister::MakeDeviceUrls(const QString& serial) { return QList() << url; } + if (IsCDDevice(serial)) { + return QList() << QString("cdda:///dev/r" + serial); + } + QString bsd_name = current_devices_[serial]; DASessionRef session = DASessionCreate(kCFAllocatorDefault); DADiskRef disk = DADiskCreateFromBSDName(