TTKMusicPlayer  4.2.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
qdevicewatcher_mac.cpp
Go to the documentation of this file.
1 #include "qdevicewatcher_p.h"
2 #include <QtCore/QStringList>
3 
4 #include <CoreServices/CoreServices.h>
5 #include <IOKit/storage/IOCDMedia.h>
6 #include <IOKit/storage/IODVDMedia.h>
7 
8 static QStringList sDevices; //disk list, or mount point list?
9 
10 static void onDiskAppear(DADiskRef disk, void *context)
11 {
12  QString disk_name = DADiskGetBSDName(disk);
13  if (sDevices.contains(disk_name))
14  return;
15 
16  sDevices.append(disk_name);
18  p->emitDeviceAdded(disk_name);
19 }
20 
21 static void onDiskDisappear(DADiskRef disk, void *context)
22 {
23  QString disk_name = DADiskGetBSDName(disk);
24  sDevices.removeAll(disk_name); //erase?
26  p->emitDeviceRemoved(disk_name);
27 }
28 
30 {
31  stop();
32 }
33 
35 {
36  if (!initialize())
37  return false;
38  QThread::start();
39  return true;
40 }
41 
43 {
44  mStop = true;
45  wait();
46  //DAUnregisterApprovalCallback
47  DAUnregisterCallback(mSession, (void*)onDiskAppear, this);
48  DAUnregisterCallback(mSession, (void*)onDiskDisappear, this);
49  return true;
50 }
51 
53 {
54 }
55 
57 {
58  //get sDevices
59  //FSGetVolumeInfo()
60  mSession = DASessionCreate(kCFAllocatorDefault);
61  if (!mSession) {
62  qWarning("Failed to create Disk Arbitration session");
63  return false;
64  }
65 
66  DARegisterDiskAppearedCallback(mSession, nullptr, onDiskAppear, this);
67  DARegisterDiskDisappearedCallback(mSession, nullptr, onDiskDisappear, this);
68  return true;
69 }
70 
71 void QDeviceWatcherPrivate::run()
72 {
73  mStop = false;
74 
75  DASessionScheduleWithRunLoop(mSession, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
76  SInt32 result;
77  do {
78  result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1, true);
79  } while (!mStop && result);
80 
81  DASessionUnscheduleFromRunLoop(mSession, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
82 }
#define TTKStaticCast(x, y)
Definition: ttkglobal.h:231
static void onDiskDisappear(DADiskRef disk, void *context)
void emitDeviceAdded(const QString &dev)
static QStringList sDevices
static void onDiskAppear(DADiskRef disk, void *context)
void emitDeviceRemoved(const QString &dev)
The class of the device watcher private.