1
0
mirror of https://github.com/clementine-player/Clementine synced 2024-12-17 20:09:50 +01:00

Detect cd insertion on mac.

This commit is contained in:
John Maguire 2011-08-11 21:37:09 +02:00
parent 6ddf9fa41b
commit 47a171fca7
4 changed files with 42 additions and 3 deletions

View File

@ -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;
}

View File

@ -186,7 +186,10 @@ DeviceManager::DeviceManager(BackgroundThread<Database>* 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

View File

@ -4,6 +4,7 @@
#include "devicelister.h"
#include <QMutex>
#include <QSet>
#include <QThread>
#include <DiskArbitration/DADisk.h>
@ -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<QString, QString> current_devices_;
QMap<QString, MTPDevice> mtp_devices_;
QSet<QString> cd_devices_;
QMutex libmtp_mutex_;

View File

@ -25,6 +25,7 @@
#include <IOKit/IOCFPlugin.h>
#include <IOKit/usb/IOUSBLib.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/storage/IOCDMedia.h>
#import <AppKit/NSWorkspace.h>
#import <Foundation/NSAutoreleasePool.h>
@ -335,6 +336,16 @@ void MacDeviceLister::DiskAddedCallback(DADiskRef disk, void* context) {
MacDeviceLister* me = reinterpret_cast<MacDeviceLister*>(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];
@ -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<QUrl> MacDeviceLister::MakeDeviceUrls(const QString& serial) {
return QList<QUrl>() << url;
}
if (IsCDDevice(serial)) {
return QList<QUrl>() << QString("cdda:///dev/r" + serial);
}
QString bsd_name = current_devices_[serial];
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
DADiskRef disk = DADiskCreateFromBSDName(