1 /*****************************************************************************
2 * open.m: MacOS X plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: open.m,v 1.1 2002/08/04 17:23:43 sam Exp $
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <sys/param.h> /* for MAXPATHLEN */
32 #include <IOKit/IOKitLib.h>
33 #include <IOKit/IOBSD.h>
34 #include <IOKit/storage/IOMedia.h>
35 #include <IOKit/storage/IOCDMedia.h>
36 #include <IOKit/storage/IODVDMedia.h>
38 #import <Cocoa/Cocoa.h>
49 /*****************************************************************************
50 * GetEjectableMediaOfClass
51 *****************************************************************************/
52 NSArray *GetEjectableMediaOfClass( const char *psz_class )
54 io_object_t next_media;
55 mach_port_t master_port;
56 kern_return_t kern_result;
57 NSArray *o_devices = nil;
58 NSMutableArray *p_list = nil;
59 io_iterator_t media_iterator;
60 CFMutableDictionaryRef classes_to_match;
62 kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
63 if( kern_result != KERN_SUCCESS )
68 classes_to_match = IOServiceMatching( psz_class );
69 if( classes_to_match == NULL )
74 CFDictionarySetValue( classes_to_match, CFSTR( kIOMediaEjectable ),
77 kern_result = IOServiceGetMatchingServices( master_port, classes_to_match,
79 if( kern_result != KERN_SUCCESS )
84 p_list = [NSMutableArray arrayWithCapacity: 1];
86 next_media = IOIteratorNext( media_iterator );
87 if( next_media != NULL )
90 size_t dev_path_length;
91 CFTypeRef str_bsd_path;
95 str_bsd_path = IORegistryEntryCreateCFProperty( next_media,
99 if( str_bsd_path == NULL )
101 IOObjectRelease( next_media );
105 snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
106 dev_path_length = strlen( psz_buf );
108 if( CFStringGetCString( str_bsd_path,
109 (char*)&psz_buf + dev_path_length,
110 sizeof(psz_buf) - dev_path_length,
111 kCFStringEncodingASCII ) )
113 [p_list addObject: [NSString stringWithCString: psz_buf]];
116 CFRelease( str_bsd_path );
118 IOObjectRelease( next_media );
120 } while( ( next_media = IOIteratorNext( media_iterator ) ) != NULL );
123 IOObjectRelease( media_iterator );
125 o_devices = [NSArray arrayWithArray: p_list];
130 /*****************************************************************************
131 * VLCOpen implementation
132 *****************************************************************************/
133 @implementation VLCOpen
137 [o_disc_panel setTitle: _NS("Open Disc")];
138 [o_disc_btn_ok setTitle: _NS("OK")];
139 [o_disc_btn_cancel setTitle: _NS("Cancel")];
140 [o_disc_lbl_type setTitle: _NS("Disc type")];
141 [o_disc_lbl_sp setTitle: _NS("Starting position")];
142 [o_disc_title setTitle: _NS("Title")];
143 [o_disc_chapter setTitle: _NS("Chapter")];
145 [o_net_panel setTitle: _NS("Open Network")];
146 [o_net_box_mode setTitle: _NS("Network mode")];
147 [o_net_box_addr setTitle: _NS("Address")];
148 [o_net_port_lbl setStringValue: _NS("Port")];
150 [o_quickly_panel setTitle: _NS("Open Quickly")];
151 [o_quickly_btn_ok setTitle: _NS("OK")];
152 [o_quickly_btn_cancel setTitle: _NS("Cancel")];
155 - (IBAction)openDisc:(id)sender
159 [self openDiscTypeChanged: nil];
161 [o_disc_panel makeKeyAndOrderFront: self];
162 i_result = [NSApp runModalForWindow: o_disc_panel];
163 [o_disc_panel close];
169 NSString *o_type = [[o_disc_type selectedCell] title];
170 NSString *o_device = [o_disc_device stringValue];
171 int i_title = [o_disc_title intValue];
172 int i_chapter = [o_disc_chapter intValue];
174 o_source = [NSString stringWithFormat: @"%@:%@@%d,%d",
175 [o_type lowercaseString], o_device, i_title, i_chapter];
177 [o_playlist appendArray:
178 [NSArray arrayWithObject: o_source] atPos: -1];
182 - (IBAction)openDiscTypeChanged:(id)sender
186 const char *psz_class = NULL;
188 [o_disc_device removeAllItems];
190 o_type = [[o_disc_type selectedCell] title];
192 if( [o_type isEqualToString: @"DVD"] )
194 psz_class = kIODVDMediaClass;
198 psz_class = kIOCDMediaClass;
201 o_devices = GetEjectableMediaOfClass( psz_class );
202 if( o_devices != nil )
204 int i_devices = [o_devices count];
210 for( i = 0; i < i_devices; i++ )
213 addItemWithObjectValue: [o_devices objectAtIndex: i]];
216 [o_disc_device selectItemAtIndex: 0];
217 [o_disc_btn_ok setEnabled: TRUE];
221 [o_disc_device setStringValue:
222 [NSString stringWithFormat: @"No %@s found", o_type]];
223 [o_disc_btn_ok setEnabled: FALSE];
228 - (IBAction)openFile:(id)sender
230 NSOpenPanel *o_panel = [NSOpenPanel openPanel];
232 [o_panel setAllowsMultipleSelection: YES];
234 if( [o_panel runModalForDirectory: nil
235 file: nil types: nil] == NSOKButton )
237 [o_playlist appendArray: [o_panel filenames] atPos: -1];
241 - (IBAction)openNet:(id)sender
244 intf_thread_t * p_intf = [NSApp getIntf];
246 [o_net_panel makeKeyAndOrderFront: self];
247 i_result = [NSApp runModalForWindow: o_net_panel];
252 NSString * o_source = nil;
253 UInt32 i_port = [o_net_port intValue];
254 NSString * o_addr = [o_net_address stringValue];
255 NSString * o_mode = [[o_net_mode selectedCell] title];
263 if( [o_mode isEqualToString: @"UDP"] )
266 stringWithFormat: @"udp:@:%i", i_port];
268 else if( [o_mode isEqualToString: @"UDP Multicase"] )
271 stringWithFormat: @"udp:@%@:%i", o_addr, i_port];
273 else if( [o_mode isEqualToString: @"Channel server"] )
275 if( p_intf->p_vlc->p_channel == NULL )
277 network_ChannelCreate( p_intf );
280 config_PutPsz( p_intf, "channel-server",
281 (char *)[o_addr lossyCString] );
282 config_PutInt( p_intf, "channel-port", i_port );
284 p_intf->p_sys->b_playing = 1;
286 else if( [o_mode isEqualToString: @"HTTP"] )
291 if( o_source != nil )
293 [o_playlist appendArray:
294 [NSArray arrayWithObject: o_source] atPos: -1];
299 - (IBAction)openNetModeChanged:(id)sender
302 SInt32 i_port = 1234;
303 NSString * o_addr = nil;
305 o_mode = [[o_net_mode selectedCell] title];
307 if( [o_mode isEqualToString: @"UDP Multicast"] )
311 else if( [o_mode isEqualToString: @"Channel server"] )
313 o_addr = @"localhost";
316 else if( [o_mode isEqualToString: @"HTTP"] )
324 [o_net_address setEnabled: TRUE];
325 [o_net_address setStringValue: o_addr];
329 [o_net_address setEnabled: FALSE];
334 [o_net_port setEnabled: TRUE];
335 [o_net_port_stp setEnabled: TRUE];
336 [o_net_port setIntValue: i_port];
340 [o_net_port setEnabled: FALSE];
341 [o_net_port_stp setEnabled: FALSE];
345 - (IBAction)openQuickly:(id)sender
349 [o_quickly_source setStringValue: @""];
350 [o_quickly_panel makeKeyAndOrderFront: self];
351 i_result = [NSApp runModalForWindow: o_quickly_panel];
352 [o_quickly_panel close];
358 o_source = [o_quickly_source stringValue];
360 if( [o_source length] > 0 )
362 [o_playlist appendArray:
363 [NSArray arrayWithObject: o_source] atPos: -1];
372 - (IBAction)panelCancel:(id)sender
374 [NSApp stopModalWithCode: 0];
377 - (IBAction)panelOk:(id)sender
379 [NSApp stopModalWithCode: 1];