diff options
author | titer <[email protected]> | 2006-03-18 21:57:13 +0000 |
---|---|---|
committer | titer <[email protected]> | 2006-03-18 21:57:13 +0000 |
commit | e3cd96683d5137f1aefef861ae40f7d0de93a7cb (patch) | |
tree | 4a1419cdab7b188853655f56d45a44e81d73cdba | |
parent | 111307fa9186021aef60d28e6b707c93190d5407 (diff) |
Moves drive detection to a seperate class
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@43 b64f7644-9d1e-0410-96f1-a4d463321fa5
-rw-r--r-- | macosx/Controller.mm | 14 | ||||
-rw-r--r-- | macosx/DriveDetector.h | 21 | ||||
-rw-r--r-- | macosx/DriveDetector.m | 128 | ||||
-rw-r--r-- | macosx/ExpressController.h | 4 | ||||
-rw-r--r-- | macosx/ExpressController.m | 18 | ||||
-rw-r--r-- | macosx/HandBrake.xcodeproj/project.pbxproj | 38 | ||||
-rw-r--r-- | macosx/ScanController.h | 4 | ||||
-rw-r--r-- | macosx/ScanController.mm | 106 |
8 files changed, 206 insertions, 127 deletions
diff --git a/macosx/Controller.mm b/macosx/Controller.mm index af6042334..a71f121f0 100644 --- a/macosx/Controller.mm +++ b/macosx/Controller.mm @@ -260,14 +260,6 @@ static int FormatSettings[3][4] = switch( s.state ) { case HB_STATE_IDLE: - /* If the scan panel is currently showing, recheck for DVD - drives. The clean way would be to use - NSWorkspaceDidMountNotification, but for some reason the - notifications don't work when having the sheet attached */ - if( [fWindow attachedSheet] == fScanPanel ) - { - [fScanController DetectDrives: NULL]; - } break; case HB_STATE_SCANNING: @@ -433,11 +425,7 @@ static int FormatSettings[3][4] = - (IBAction) ShowScanPanel: (id) sender { - [NSApp beginSheet: fScanPanel modalForWindow: fWindow - modalDelegate: NULL didEndSelector: NULL contextInfo: NULL]; - [NSApp runModalForWindow: fScanPanel]; - [NSApp endSheet: fScanPanel]; - [fScanPanel orderOut: self]; + [fScanController Show]; } - (BOOL) windowShouldClose: (id) sender diff --git a/macosx/DriveDetector.h b/macosx/DriveDetector.h new file mode 100644 index 000000000..09d589adb --- /dev/null +++ b/macosx/DriveDetector.h @@ -0,0 +1,21 @@ +/* DriveDetector.h $ + + This file is part of the HandBrake source code. + Homepage: <http://handbrake.m0k.org/>. + It may be used under the terms of the GNU General Public License. */ + +#import <Cocoa/Cocoa.h> + +@interface DriveDetector : NSObject +{ + id fTarget; + SEL fSelector; + + int fCount; + NSMutableArray * fDrives; + NSTimer * fTimer; +} + +- (id) initWithCallback: (id) target selector: (SEL) selector; + +@end diff --git a/macosx/DriveDetector.m b/macosx/DriveDetector.m new file mode 100644 index 000000000..26958a964 --- /dev/null +++ b/macosx/DriveDetector.m @@ -0,0 +1,128 @@ +/* DriveDetector.m $ + + This file is part of the HandBrake source code. + Homepage: <http://handbrake.m0k.org/>. + It may be used under the terms of the GNU General Public License. */ + +#include <paths.h> +#include <IOKit/IOKitLib.h> +#include <IOKit/IOBSD.h> +#include <IOKit/storage/IOMedia.h> +#include <IOKit/storage/IODVDMedia.h> + +#include "DriveDetector.h" + +@interface DriveDetector (Private) + +- (void) detectTimer: (NSTimer *) timer; + +@end + +@implementation DriveDetector + +- (void) dealloc +{ + [fTimer invalidate]; + [super dealloc]; +} + +- (id) initWithCallback: (id) target selector: (SEL) selector +{ + fTarget = target; + fSelector = selector; + + fCount = -1; + fDrives = [[NSMutableArray alloc] initWithCapacity: 1]; + + /* Set up a timer to check devices every second */ + fTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self + selector: @selector( detectTimer: ) userInfo: nil repeats: YES]; + [[NSRunLoop currentRunLoop] addTimer: fTimer forMode: + NSModalPanelRunLoopMode]; + + /* Do a first update right away */ + [fTimer fire]; + + return self; +} + +- (void) detectTimer: (NSTimer *) timer +{ + /* Scan DVD drives (stolen from VLC) */ + io_object_t next_media; + mach_port_t master_port; + kern_return_t kern_result; + io_iterator_t media_iterator; + CFMutableDictionaryRef classes_to_match; + + kern_result = IOMasterPort( MACH_PORT_NULL, &master_port ); + if( kern_result != KERN_SUCCESS ) + { + return; + } + + classes_to_match = IOServiceMatching( kIODVDMediaClass ); + if( classes_to_match == NULL ) + { + return; + } + + CFDictionarySetValue( classes_to_match, CFSTR( kIOMediaEjectableKey ), + kCFBooleanTrue ); + + kern_result = IOServiceGetMatchingServices( master_port, + classes_to_match, &media_iterator ); + if( kern_result != KERN_SUCCESS ) + { + return; + } + + [fDrives removeAllObjects]; + + next_media = IOIteratorNext( media_iterator ); + if( next_media ) + { + char psz_buf[0x32]; + size_t dev_path_length; + CFTypeRef str_bsd_path; + do + { + str_bsd_path = + IORegistryEntryCreateCFProperty( next_media, + CFSTR( kIOBSDNameKey ), + kCFAllocatorDefault, + 0 ); + if( str_bsd_path == NULL ) + { + IOObjectRelease( next_media ); + continue; + } + + snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' ); + dev_path_length = strlen( psz_buf ); + + if( CFStringGetCString( (CFStringRef) str_bsd_path, + (char*)&psz_buf + dev_path_length, + sizeof(psz_buf) - dev_path_length, + kCFStringEncodingASCII ) ) + { + [fDrives addObject: [NSString stringWithCString: psz_buf]]; + } + + CFRelease( str_bsd_path ); + + IOObjectRelease( next_media ); + + } while( ( next_media = IOIteratorNext( media_iterator ) ) ); + } + + IOObjectRelease( media_iterator ); + + if( [fDrives count] != fCount ) + { + [fTarget performSelector: fSelector withObject: fDrives]; + fCount = [fDrives count]; + } +} + +@end diff --git a/macosx/ExpressController.h b/macosx/ExpressController.h index 040728745..f4ef02130 100644 --- a/macosx/ExpressController.h +++ b/macosx/ExpressController.h @@ -3,6 +3,8 @@ #import <Cocoa/Cocoa.h> #import "hb.h" +@class DriveDetector; + @interface ExpressController : NSObject { @@ -28,6 +30,8 @@ IBOutlet NSProgressIndicator * fConvertIndicator; NSMutableArray * fConvertCheckArray; NSString * fConvertFolderString; + + DriveDetector * fDriveDetector; } - (void) openShow: (id) sender; diff --git a/macosx/ExpressController.m b/macosx/ExpressController.m index cad330872..fb881fa52 100644 --- a/macosx/ExpressController.m +++ b/macosx/ExpressController.m @@ -1,7 +1,9 @@ #import "ExpressController.h" +#import "DriveDetector.h" @interface ExpressController (Private) +- (void) openUpdateDrives: (NSArray *) drives; - (void) openBrowseDidEnd: (NSOpenPanel *) sheet returnCode: (int) returnCode contextInfo: (void *) contextInfo; - (void) openEnable: (BOOL) b; @@ -20,6 +22,8 @@ - (void) awakeFromNib { /* Show the "Open DVD" interface */ + fDriveDetector = [[DriveDetector alloc] initWithCallback: self + selector: @selector( openUpdateDrives: )]; [self openEnable: YES]; [fWindow setContentSize: [fOpenView frame].size]; [fWindow setContentView: fOpenView]; @@ -126,6 +130,9 @@ [fWindow setContentView: fEmptyView]; [fWindow setFrame: frame display: YES animate: YES]; [fWindow setContentView: fOpenView]; + + fDriveDetector = [[DriveDetector alloc] initWithCallback: self + selector: @selector( openUpdateDrives: )]; } - (void) openMatrixChanged: (id) sender @@ -215,6 +222,16 @@ @implementation ExpressController (Private) +- (void) openUpdateDrives: (NSArray *) drives +{ + [fOpenPopUp removeAllItems]; + [fOpenPopUp addItemsWithTitles: drives]; + if( [fOpenPopUp numberOfItems] ) + { + [fOpenPopUp selectItemAtIndex: 0]; + } +} + - (void) openBrowseDidEnd: (NSOpenPanel *) sheet returnCode: (int) returnCode contextInfo: (void *) contextInfo { @@ -275,6 +292,7 @@ if( hb_list_count( fList ) ) { + [fDriveDetector release]; [self convertShow]; } break; diff --git a/macosx/HandBrake.xcodeproj/project.pbxproj b/macosx/HandBrake.xcodeproj/project.pbxproj index d62cb1f21..de5348438 100644 --- a/macosx/HandBrake.xcodeproj/project.pbxproj +++ b/macosx/HandBrake.xcodeproj/project.pbxproj @@ -11,6 +11,10 @@ 4D1EA2F60993B0CA00FDC1A2 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; }; 4D1EA3010993B13700FDC1A2 /* Express.nib in Resources */ = {isa = PBXBuildFile; fileRef = 4D1EA3000993B13700FDC1A2 /* Express.nib */; }; 4D1EA31C0993B24700FDC1A2 /* ExpressController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D1EA31B0993B24700FDC1A2 /* ExpressController.m */; }; + 4D2AE78B09CCB24C007E18F6 /* DriveDetector.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D2AE78909CCB24C007E18F6 /* DriveDetector.m */; }; + 4D2AEA1A09CCB332007E18F6 /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4DEB2024052B055F00C39CA9 /* IOKit.framework */; }; + 4D2AEA2909CCB8F9007E18F6 /* DriveDetector.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D2AE78909CCB24C007E18F6 /* DriveDetector.m */; }; + 4D2AEA2A09CCB8FC007E18F6 /* DriveDetector.h in Headers */ = {isa = PBXBuildFile; fileRef = 4D2AE78A09CCB24C007E18F6 /* DriveDetector.h */; }; 4D3ECC2709A4917000B2E45F /* WhiteBox.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D3ECC2609A4917000B2E45F /* WhiteBox.m */; }; 4DD93F8F082036E8008E1322 /* Controller.h in Headers */ = {isa = PBXBuildFile; fileRef = 4DF3C8CB052889CD00A80101 /* Controller.h */; }; 4DD93F90082036E8008E1322 /* PictureGLView.h in Headers */ = {isa = PBXBuildFile; fileRef = 4D85758F052B78E300C39CA9 /* PictureGLView.h */; }; @@ -77,6 +81,8 @@ 4D1EA31B0993B24700FDC1A2 /* ExpressController.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = ExpressController.m; sourceTree = "<group>"; }; 4D1FD381073D19CE00E46515 /* PictureController.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = PictureController.h; sourceTree = "<group>"; }; 4D1FD382073D19CE00E46515 /* PictureController.mm */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.objcpp; path = PictureController.mm; sourceTree = "<group>"; }; + 4D2AE78909CCB24C007E18F6 /* DriveDetector.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = DriveDetector.m; sourceTree = "<group>"; }; + 4D2AE78A09CCB24C007E18F6 /* DriveDetector.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = DriveDetector.h; sourceTree = "<group>"; }; 4D3ECC2509A4917000B2E45F /* WhiteBox.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = WhiteBox.h; sourceTree = "<group>"; }; 4D3ECC2609A4917000B2E45F /* WhiteBox.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = WhiteBox.m; sourceTree = "<group>"; }; 4D85758E052B78E300C39CA9 /* PictureGLView.mm */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.objcpp; path = PictureGLView.mm; sourceTree = "<group>"; }; @@ -101,6 +107,7 @@ buildActionMask = 2147483647; files = ( 4D1EA2F60993B0CA00FDC1A2 /* Cocoa.framework in Frameworks */, + 4D2AEA1A09CCB332007E18F6 /* IOKit.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -136,29 +143,12 @@ 4DE93A3607F5A2C900F3C78F /* PrefsController.m */, 4D3ECC2509A4917000B2E45F /* WhiteBox.h */, 4D3ECC2609A4917000B2E45F /* WhiteBox.m */, + 4D2AE78909CCB24C007E18F6 /* DriveDetector.m */, + 4D2AE78A09CCB24C007E18F6 /* DriveDetector.h */, ); name = Classes; sourceTree = "<group>"; }; - 1058C7A0FEA54F0111CA2CBB /* Linked Frameworks */ = { - isa = PBXGroup; - children = ( - 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */, - 4DEB2024052B055F00C39CA9 /* IOKit.framework */, - 4DDE9724052B7B2B00C39CA9 /* OpenGL.framework */, - ); - name = "Linked Frameworks"; - sourceTree = "<group>"; - }; - 1058C7A2FEA54F0111CA2CBB /* Other Frameworks */ = { - isa = PBXGroup; - children = ( - 29B97325FDCFA39411CA2CEA /* Foundation.framework */, - 29B97324FDCFA39411CA2CEA /* AppKit.framework */, - ); - name = "Other Frameworks"; - sourceTree = "<group>"; - }; 19C28FACFE9D520D11CA2CBB /* Products */ = { isa = PBXGroup; children = ( @@ -204,8 +194,11 @@ 29B97323FDCFA39411CA2CEA /* Frameworks */ = { isa = PBXGroup; children = ( - 1058C7A0FEA54F0111CA2CBB /* Linked Frameworks */, - 1058C7A2FEA54F0111CA2CBB /* Other Frameworks */, + 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */, + 4DEB2024052B055F00C39CA9 /* IOKit.framework */, + 4DDE9724052B7B2B00C39CA9 /* OpenGL.framework */, + 29B97325FDCFA39411CA2CEA /* Foundation.framework */, + 29B97324FDCFA39411CA2CEA /* AppKit.framework */, ); name = Frameworks; sourceTree = "<group>"; @@ -223,6 +216,7 @@ 4DD93F92082036E8008E1322 /* PictureController.h in Headers */, 4DD93F93082036E8008E1322 /* QueueController.h in Headers */, 4DD93F94082036E8008E1322 /* PrefsController.h in Headers */, + 4D2AEA2A09CCB8FC007E18F6 /* DriveDetector.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -396,6 +390,7 @@ 4D1EA2EA0993B09A00FDC1A2 /* main.mm in Sources */, 4D1EA31C0993B24700FDC1A2 /* ExpressController.m in Sources */, 4D3ECC2709A4917000B2E45F /* WhiteBox.m in Sources */, + 4D2AE78B09CCB24C007E18F6 /* DriveDetector.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -410,6 +405,7 @@ 4DD93F9E082036E8008E1322 /* PictureController.mm in Sources */, 4DD93F9F082036E8008E1322 /* QueueController.mm in Sources */, 4DD93FA0082036E8008E1322 /* PrefsController.m in Sources */, + 4D2AEA2909CCB8F9007E18F6 /* DriveDetector.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/macosx/ScanController.h b/macosx/ScanController.h index d038e6eea..c5a8de7e0 100644 --- a/macosx/ScanController.h +++ b/macosx/ScanController.h @@ -25,13 +25,11 @@ IBOutlet NSProgressIndicator * fIndicator; IBOutlet NSButton * fCancelButton; IBOutlet NSButton * fOpenButton; - - uint64_t fLastCheck; } - (void) TranslateStrings; - (void) SetHandle: (hb_handle_t *) handle; -- (void) DetectDrives: (NSNotification *) notification; +- (void) Show; - (void) UpdateUI: (hb_state_t *) state; - (IBAction) MatrixChanged: (id) sender; diff --git a/macosx/ScanController.mm b/macosx/ScanController.mm index 5a4075416..a956ea83b 100644 --- a/macosx/ScanController.mm +++ b/macosx/ScanController.mm @@ -11,6 +11,7 @@ #include <IOKit/storage/IODVDMedia.h> #include "ScanController.h" +#include "DriveDetector.h" #define _(a) NSLocalizedString(a,nil) @@ -29,106 +30,31 @@ - (void) SetHandle: (hb_handle_t *) handle { fHandle = handle; - fLastCheck = 0; [self TranslateStrings]; [fStatusField setStringValue: @""]; } -- (void) DetectDrives: (NSNotification *) notification +- (void) Show { - if( [fMatrix isEnabled] == NO ) - { - /* We're scanning */ - return; - } - if( hb_get_date() < fLastCheck + 1000 ) - { - /* Don't check more than every second */ - return; - } - fLastCheck = hb_get_date(); - - /* Scan DVD drives (stolen from VLC) */ - io_object_t next_media; - mach_port_t master_port; - kern_return_t kern_result; - io_iterator_t media_iterator; - CFMutableDictionaryRef classes_to_match; - - kern_result = IOMasterPort( MACH_PORT_NULL, &master_port ); - if( kern_result != KERN_SUCCESS ) - { - return; - } - - classes_to_match = IOServiceMatching( kIODVDMediaClass ); - if( classes_to_match == NULL ) - { - return; - } - - CFDictionarySetValue( classes_to_match, CFSTR( kIOMediaEjectableKey ), - kCFBooleanTrue ); - - kern_result = IOServiceGetMatchingServices( master_port, - classes_to_match, &media_iterator ); - if( kern_result != KERN_SUCCESS ) - { - return; - } - - NSMutableArray * drivesList; - drivesList = [NSMutableArray arrayWithCapacity: 1]; - - next_media = IOIteratorNext( media_iterator ); - if( next_media ) - { - char psz_buf[0x32]; - size_t dev_path_length; - CFTypeRef str_bsd_path; - do - { - str_bsd_path = - IORegistryEntryCreateCFProperty( next_media, - CFSTR( kIOBSDNameKey ), - kCFAllocatorDefault, - 0 ); - if( str_bsd_path == NULL ) - { - IOObjectRelease( next_media ); - continue; - } - - snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' ); - dev_path_length = strlen( psz_buf ); - - if( CFStringGetCString( (CFStringRef) str_bsd_path, - (char*)&psz_buf + dev_path_length, - sizeof(psz_buf) - dev_path_length, - kCFStringEncodingASCII ) ) - { - [drivesList addObject: - [NSString stringWithCString: psz_buf]]; - } - - CFRelease( str_bsd_path ); - - IOObjectRelease( next_media ); - - } while( ( next_media = IOIteratorNext( media_iterator ) ) ); - } + DriveDetector * driveDetector; + driveDetector = [[DriveDetector alloc] initWithCallback: self + selector: @selector( UpdatePopup: )]; + + [NSApp beginSheet: fPanel modalForWindow: fWindow + modalDelegate: nil didEndSelector: nil contextInfo: nil]; + [NSApp runModalForWindow: fPanel]; + [NSApp endSheet: fPanel]; + [fPanel orderOut: self]; - IOObjectRelease( media_iterator ); + [driveDetector release]; +} +- (void) UpdatePopup: (NSArray *) drives +{ [fDetectedPopUp removeAllItems]; - for( unsigned i = 0; i < [drivesList count]; i++ ) - { - [[fDetectedPopUp menu] addItemWithTitle: - [drivesList objectAtIndex: i] action: nil - keyEquivalent: @""]; - } + [fDetectedPopUp addItemsWithTitles: drives]; [self MatrixChanged: self]; } |