OSDN Git Service

MacGui: ScanController modification so it skips the opening scan choice sheet and...
[handbrake-jp/handbrake-jp-git.git] / macosx / DriveDetector.m
1 /* DriveDetector.m $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.m0k.org/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include <paths.h>
8 #include <IOKit/IOKitLib.h>
9 #include <IOKit/IOBSD.h>
10 #include <IOKit/storage/IOMedia.h>
11 #include <IOKit/storage/IODVDMedia.h>
12
13 #include "DriveDetector.h"
14 #include "hb.h"
15
16 @interface DriveDetector (Private)
17
18 - (void) detectTimer: (NSTimer *) timer;
19
20 @end
21
22 @implementation DriveDetector
23
24 - (void) dealloc
25 {
26     [fDrives release];
27     [super dealloc];
28 }
29
30 - (id) initWithCallback: (id) target selector: (SEL) selector
31 {
32     fTarget   = target;
33     fSelector = selector;
34     
35     fCount  = -1;
36     fDrives = [[NSMutableDictionary alloc] initWithCapacity: 1];
37     
38     return self;
39 }
40
41 - (void) run
42 {
43         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DisableDvdAutoDetect"] == 0)
44         {
45                 /* Set up a timer to check devices every second */
46                 fTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self
47                                                                                                 selector: @selector( detectTimer: ) userInfo: nil repeats: YES];
48                 [[NSRunLoop currentRunLoop] addTimer: fTimer forMode:
49                         NSModalPanelRunLoopMode];
50                 
51                 /* Do a first update right away */
52                 [fTimer fire];
53         }
54 }
55
56 - (void) stop
57 {
58     [fTimer invalidate];
59 }
60
61 - (void) detectTimer: (NSTimer *) timer
62 {
63     /* Scan DVD drives (stolen from VLC) */
64     io_object_t            next_media;
65     mach_port_t            master_port;
66     kern_return_t          kern_result;
67     io_iterator_t          media_iterator;
68     CFMutableDictionaryRef classes_to_match;
69
70     kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
71     if( kern_result != KERN_SUCCESS )
72     {
73         return;
74     }
75
76     classes_to_match = IOServiceMatching( kIODVDMediaClass );
77     if( classes_to_match == NULL )
78     {
79         return;
80     }
81
82     CFDictionarySetValue( classes_to_match, CFSTR( kIOMediaEjectableKey ),
83                           kCFBooleanTrue );
84
85     kern_result = IOServiceGetMatchingServices( master_port,
86             classes_to_match, &media_iterator );
87     if( kern_result != KERN_SUCCESS )
88     {
89         return;
90     }
91
92     [fDrives removeAllObjects];
93
94     next_media = IOIteratorNext( media_iterator );
95     if( next_media )
96     {
97         char * name;
98         char psz_buf[0x32];
99         size_t dev_path_length;
100         CFTypeRef str_bsd_path;
101         do
102         {
103             str_bsd_path =
104                 IORegistryEntryCreateCFProperty( next_media,
105                                                  CFSTR( kIOBSDNameKey ),
106                                                  kCFAllocatorDefault,
107                                                  0 );
108             if( str_bsd_path == NULL )
109             {
110                 IOObjectRelease( next_media );
111                 continue;
112             }
113
114             snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
115             dev_path_length = strlen( psz_buf );
116
117             if( CFStringGetCString( (CFStringRef) str_bsd_path,
118                                     (char*)&psz_buf + dev_path_length,
119                                     sizeof(psz_buf) - dev_path_length,
120                                     kCFStringEncodingASCII ) )
121             {
122                 if( ( name = hb_dvd_name( psz_buf ) ) )
123                 {
124                     [fDrives setObject: [NSString stringWithCString: psz_buf]
125                         forKey: [NSString stringWithCString: name]];
126                 }
127             }
128
129             CFRelease( str_bsd_path );
130
131             IOObjectRelease( next_media );
132
133         } while( ( next_media = IOIteratorNext( media_iterator ) ) );
134     }
135
136     IOObjectRelease( media_iterator );
137
138     if( [fDrives count] != fCount )
139     {
140         [fTarget performSelector: fSelector withObject: fDrives];
141         fCount = [fDrives count];
142     }
143 }
144
145 @end