From b8aa12486e1e1b15856a0073cac2e4281b88f95f Mon Sep 17 00:00:00 2001 From: Slic3rPE Date: Tue, 17 Dec 2019 13:08:17 +0100 Subject: [PATCH] macos device detection --- src/slic3r/CMakeLists.txt | 8 +++ src/slic3r/GUI/RemovableDriveManager.cpp | 2 + src/slic3r/GUI/RemovableDriveManager.hpp | 1 + src/slic3r/GUI/RemovableDriveManagerMM.h | 1 + src/slic3r/GUI/RemovableDriveManagerMM.mm | 86 ++++++++++++++++++++++- 5 files changed, 96 insertions(+), 2 deletions(-) diff --git a/src/slic3r/CMakeLists.txt b/src/slic3r/CMakeLists.txt index a06a8cf1d..5b55b96b4 100644 --- a/src/slic3r/CMakeLists.txt +++ b/src/slic3r/CMakeLists.txt @@ -172,6 +172,9 @@ if (APPLE) GUI/RemovableDriveManagerMM.mm GUI/RemovableDriveManagerMM.h ) + #DK + FIND_LIBRARY(DISKARBITRATION_LIBRARY DiskArbitration) + endif () add_library(libslic3r_gui STATIC ${SLIC3R_GUI_SOURCES}) @@ -179,6 +182,11 @@ add_library(libslic3r_gui STATIC ${SLIC3R_GUI_SOURCES}) encoding_check(libslic3r_gui) target_link_libraries(libslic3r_gui libslic3r avrdude cereal imgui GLEW::GLEW OpenGL::GL OpenGL::GLU hidapi) +#DK +if(APPLE) + target_link_libraries(libslic3r_gui ${DISKARBITRATION_LIBRARY}) +endif() + if (SLIC3R_PCH AND NOT SLIC3R_SYNTAXONLY) add_precompiled_header(libslic3r_gui pchheader.hpp FORCEINCLUDE) endif () diff --git a/src/slic3r/GUI/RemovableDriveManager.cpp b/src/slic3r/GUI/RemovableDriveManager.cpp index 52a8c2b77..6e13a59b5 100644 --- a/src/slic3r/GUI/RemovableDriveManager.cpp +++ b/src/slic3r/GUI/RemovableDriveManager.cpp @@ -354,6 +354,7 @@ void RemovableDriveManager::eject_drive(const std::string &path) // but neither triggers "succesful safe removal messege" std::string command = ""; #if __APPLE__ + //m_rdmmm->eject_device(path); command = "diskutil unmount "; #else command = "umount "; @@ -365,6 +366,7 @@ void RemovableDriveManager::eject_drive(const std::string &path) std::cerr<<"Ejecting failed\n"; return; } + m_did_eject = true; m_current_drives.erase(it); diff --git a/src/slic3r/GUI/RemovableDriveManager.hpp b/src/slic3r/GUI/RemovableDriveManager.hpp index 2e9fc9f4e..1b337338e 100644 --- a/src/slic3r/GUI/RemovableDriveManager.hpp +++ b/src/slic3r/GUI/RemovableDriveManager.hpp @@ -96,6 +96,7 @@ public: ~RDMMMWrapper(); void register_window(); void list_devices(); + void eject_device(const std::string &path); void log(const std::string &msg); protected: void *m_imp; diff --git a/src/slic3r/GUI/RemovableDriveManagerMM.h b/src/slic3r/GUI/RemovableDriveManagerMM.h index 299941545..71239dba3 100644 --- a/src/slic3r/GUI/RemovableDriveManagerMM.h +++ b/src/slic3r/GUI/RemovableDriveManagerMM.h @@ -6,4 +6,5 @@ -(void) add_unmount_observer; -(void) on_device_unmount: (NSNotification*) notification; -(NSArray*) list_dev; +-(void)eject_drive:(NSString *)path; @end diff --git a/src/slic3r/GUI/RemovableDriveManagerMM.mm b/src/slic3r/GUI/RemovableDriveManagerMM.mm index 3bb017a3a..01d38b185 100644 --- a/src/slic3r/GUI/RemovableDriveManagerMM.mm +++ b/src/slic3r/GUI/RemovableDriveManagerMM.mm @@ -1,6 +1,7 @@ #import "RemovableDriveManager.hpp" #import "RemovableDriveManagerMM.h" #import +#import @implementation RemovableDriveManagerMM @@ -27,9 +28,81 @@ } -(NSArray*) list_dev { - NSArray* devices = [[NSWorkspace sharedWorkspace] mountedRemovableMedia]; - return devices; + // DEPRICATED: + //NSArray* devices = [[NSWorkspace sharedWorkspace] mountedRemovableMedia]; + //return devices; + NSArray *mountedRemovableMedia = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:nil options:NSVolumeEnumerationSkipHiddenVolumes]; + NSMutableArray *result = [NSMutableArray array]; + for(NSURL *volURL in mountedRemovableMedia) + { + int err = 0; + DADiskRef disk; + DASessionRef session; + CFDictionaryRef descDict; + session = DASessionCreate(NULL); + if (session == NULL) { + err = EINVAL; + } + if (err == 0) { + disk = DADiskCreateFromVolumePath(NULL,session,(CFURLRef)volURL); + if (session == NULL) { + err = EINVAL; + } + } + if (err == 0) { + descDict = DADiskCopyDescription(disk); + if (descDict == NULL) { + err = EINVAL; + } + } + if (err == 0) { + CFTypeRef mediaEjectableKey = CFDictionaryGetValue(descDict,kDADiskDescriptionMediaEjectableKey); + BOOL ejectable = [mediaEjectableKey boolValue]; + CFTypeRef deviceProtocolName = CFDictionaryGetValue(descDict,kDADiskDescriptionDeviceProtocolKey); + CFTypeRef deviceModelKey = CFDictionaryGetValue(descDict, kDADiskDescriptionDeviceModelKey); + if (mediaEjectableKey != NULL) + { + BOOL op = ejectable && (CFEqual(deviceProtocolName, CFSTR("USB")) || CFEqual(deviceModelKey, CFSTR("SD Card Reader"))); + //!CFEqual(deviceModelKey, CFSTR("Disk Image")); + // + if (op) { + [result addObject:volURL.path]; + } + } + } + if (descDict != NULL) { + CFRelease(descDict); + } + + + } + return result; +} +-(void)eject_drive:(NSString *)path +{ + DADiskRef disk; + DASessionRef session; + NSURL *url = [[NSURL alloc] initFileURLWithPath:path]; + int err = 0; + session = DASessionCreate(NULL); + if (session == NULL) { + err = EINVAL; + } + if (err == 0) { + disk = DADiskCreateFromVolumePath(NULL,session,(CFURLRef)url); + } + if( err == 0) + { + DADiskUnmount(disk, kDADiskUnmountOptionDefault, + NULL, NULL); + } + if (disk != NULL) { + CFRelease(disk); + } + if (session != NULL) { + CFRelease(session); + } } namespace Slic3r { namespace GUI { @@ -66,6 +139,15 @@ void RDMMMWrapper::log(const std::string &msg) { NSLog(@"%s", msg.c_str()); } +void RDMMMWrapper::eject_device(const std::string &path) +{ + if(m_imp) + { + NSString * pth = [NSString stringWithCString:path.c_str() + encoding:[NSString defaultCStringEncoding]]; + [m_imp eject_drive:pth]; + } +} }}//namespace Slicer::GUI /*