1 /*****************************************************************************
2 * open.m: MacOS X plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: open.m,v 1.32 2003/05/08 17:13:22 massiot Exp $
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Christophe Massiot <massiot@via.ecp.fr>
9 * Derk-Jan Hartman <thedj@users.sourceforge.net>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
30 #include <sys/param.h> /* for MAXPATHLEN */
34 #include <IOKit/IOKitLib.h>
35 #include <IOKit/IOBSD.h>
36 #include <IOKit/storage/IOMedia.h>
37 #include <IOKit/storage/IOCDMedia.h>
38 #include <IOKit/storage/IODVDMedia.h>
44 /*****************************************************************************
45 * GetEjectableMediaOfClass
46 *****************************************************************************/
47 NSArray *GetEjectableMediaOfClass( const char *psz_class )
49 io_object_t next_media;
50 mach_port_t master_port;
51 kern_return_t kern_result;
52 NSArray *o_devices = nil;
53 NSMutableArray *p_list = nil;
54 io_iterator_t media_iterator;
55 CFMutableDictionaryRef classes_to_match;
57 kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
58 if( kern_result != KERN_SUCCESS )
63 classes_to_match = IOServiceMatching( psz_class );
64 if( classes_to_match == NULL )
69 CFDictionarySetValue( classes_to_match, CFSTR( kIOMediaEjectable ),
72 kern_result = IOServiceGetMatchingServices( master_port, classes_to_match,
74 if( kern_result != KERN_SUCCESS )
79 p_list = [NSMutableArray arrayWithCapacity: 1];
81 next_media = IOIteratorNext( media_iterator );
82 if( next_media != NULL )
85 size_t dev_path_length;
86 CFTypeRef str_bsd_path;
90 str_bsd_path = IORegistryEntryCreateCFProperty( next_media,
94 if( str_bsd_path == NULL )
96 IOObjectRelease( next_media );
100 snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
101 dev_path_length = strlen( psz_buf );
103 if( CFStringGetCString( str_bsd_path,
104 (char*)&psz_buf + dev_path_length,
105 sizeof(psz_buf) - dev_path_length,
106 kCFStringEncodingASCII ) )
108 [p_list addObject: [NSString stringWithCString: psz_buf]];
111 CFRelease( str_bsd_path );
113 IOObjectRelease( next_media );
115 } while( ( next_media = IOIteratorNext( media_iterator ) ) != NULL );
118 IOObjectRelease( media_iterator );
120 o_devices = [NSArray arrayWithArray: p_list];
125 /*****************************************************************************
126 * VLCOpen implementation
127 *****************************************************************************/
128 @implementation VLCOpen
132 intf_thread_t * p_intf = [NSApp getIntf];
134 [o_panel setTitle: _NS("Open Source")];
135 [o_mrl_lbl setTitle: _NS("Media Resource Locator (MRL)")];
136 [o_ckbox_enqueue setTitle: _NS("Only enqueue in playlist, do not play")];
138 [o_btn_ok setTitle: _NS("OK")];
139 [o_btn_cancel setTitle: _NS("Cancel")];
141 [[o_tabview tabViewItemAtIndex: 0] setLabel: _NS("File")];
142 [[o_tabview tabViewItemAtIndex: 1] setLabel: _NS("Disc")];
143 [[o_tabview tabViewItemAtIndex: 2] setLabel: _NS("Network")];
145 [o_file_btn_browse setTitle: _NS("Browse...")];
146 [o_file_stream setTitle: _NS("Treat as a pipe rather than as a file")];
148 [o_disc_device_lbl setStringValue: _NS("Device name")];
149 [o_disc_title_lbl setStringValue: _NS("Title")];
150 [o_disc_chapter_lbl setStringValue: _NS("Chapter")];
151 [o_disc_videots_btn_browse setTitle: _NS("Browse...")];
152 [o_disc_dvd_menus setTitle: _NS("Use DVD menus")];
154 [[o_disc_type cellAtRow:0 column:0] setTitle: _NS("VIDEO_TS folder")];
155 [[o_disc_type cellAtRow:1 column:0] setTitle: _NS("DVD")];
156 [[o_disc_type cellAtRow:2 column:0] setTitle: _NS("VCD")];
158 [o_net_udp_port_lbl setStringValue: _NS("Port")];
159 [o_net_udpm_addr_lbl setStringValue: _NS("Address")];
160 [o_net_udpm_port_lbl setStringValue: _NS("Port")];
161 [o_net_http_url_lbl setStringValue: _NS("URL")];
163 [[o_net_mode cellAtRow:0 column:0] setTitle: _NS("UDP/RTP")];
164 [[o_net_mode cellAtRow:1 column:0] setTitle: _NS("UDP/RTP Multicast")];
165 [[o_net_mode cellAtRow:2 column:0] setTitle: _NS("HTTP/FTP/MMS")];
167 [o_net_udp_port setIntValue: config_GetInt( p_intf, "server-port" )];
168 [o_net_udp_port_stp setIntValue: config_GetInt( p_intf, "server-port" )];
170 [o_file_sub_ckbox setTitle: _NS("Load subtitles file:")];
171 [o_file_sub_btn_settings setTitle: _NS("Settings...")];
172 [o_file_sub_btn_browse setTitle: _NS("Browse...")];
173 [o_file_sub_override setTitle: _NS("Override")];
174 [o_file_sub_delay_lbl setStringValue: _NS("delay")];
175 [o_file_sub_delay_stp setEnabled: NO];
176 [o_file_sub_fps_lbl setStringValue: _NS("fps")];
177 [o_file_sub_fps_stp setEnabled: NO];
178 [o_file_sub_ok_btn setStringValue: _NS("OK")];
180 [[NSNotificationCenter defaultCenter] addObserver: self
181 selector: @selector(openFilePathChanged:)
182 name: NSControlTextDidChangeNotification
183 object: o_file_path];
185 [[NSNotificationCenter defaultCenter] addObserver: self
186 selector: @selector(openDiscInfoChanged:)
187 name: NSControlTextDidChangeNotification
188 object: o_disc_device];
189 [[NSNotificationCenter defaultCenter] addObserver: self
190 selector: @selector(openDiscInfoChanged:)
191 name: NSControlTextDidChangeNotification
192 object: o_disc_title];
193 [[NSNotificationCenter defaultCenter] addObserver: self
194 selector: @selector(openDiscInfoChanged:)
195 name: NSControlTextDidChangeNotification
196 object: o_disc_chapter];
197 [[NSNotificationCenter defaultCenter] addObserver: self
198 selector: @selector(openDiscInfoChanged:)
199 name: NSControlTextDidChangeNotification
200 object: o_disc_videots_folder];
202 [[NSNotificationCenter defaultCenter] addObserver: self
203 selector: @selector(openNetInfoChanged:)
204 name: NSControlTextDidChangeNotification
205 object: o_net_udp_port];
206 [[NSNotificationCenter defaultCenter] addObserver: self
207 selector: @selector(openNetInfoChanged:)
208 name: NSControlTextDidChangeNotification
209 object: o_net_udpm_addr];
210 [[NSNotificationCenter defaultCenter] addObserver: self
211 selector: @selector(openNetInfoChanged:)
212 name: NSControlTextDidChangeNotification
213 object: o_net_udpm_port];
214 [[NSNotificationCenter defaultCenter] addObserver: self
215 selector: @selector(openNetInfoChanged:)
216 name: NSControlTextDidChangeNotification
217 object: o_net_http_url];
220 - (void)openTarget:(int)i_type
224 [o_tabview selectTabViewItemAtIndex: i_type];
225 [o_file_sub_ckbox setState: NSOffState];
227 i_result = [NSApp runModalForWindow: o_panel];
232 intf_thread_t * p_intf = [NSApp getIntf];
234 NSString *o_source = [o_mrl stringValue];
235 BOOL b_enq = [o_ckbox_enqueue state] == NSOnState ? YES : NO;
236 NSString *subPath = [o_file_sub_path stringValue];
238 [o_playlist appendArray:
239 [NSArray arrayWithObject: o_source] atPos: -1 enqueue:b_enq];
241 if (([o_file_sub_ckbox state] == NSOnState) && !([subPath isEqualTo: @""]))
243 config_PutPsz( p_intf, "sub-file", strdup( [subPath cString] ) );
244 if ( [o_file_sub_override state] )
246 config_PutInt( p_intf, "sub-delay", (int)( [o_file_sub_delay intValue] * 10 ) );
247 config_PutFloat( p_intf, "sub-fps", [o_file_sub_fps floatValue] );
252 config_PutPsz( p_intf, "sub-file", "" );
253 config_PutInt( p_intf, "sub-delay", 0 );
254 config_PutFloat( p_intf, "sub-fps", 0.0 );
259 - (void)tabView:(NSTabView *)o_tv didSelectTabViewItem:(NSTabViewItem *)o_tvi
261 NSString *o_label = [o_tvi label];
263 if( [o_label isEqualToString: _NS("File")] )
265 [self openFilePathChanged: nil];
267 else if( [o_label isEqualToString: _NS("Disc")] )
269 [self openDiscTypeChanged: nil];
271 else if( [o_label isEqualToString: _NS("Network")] )
273 [self openNetModeChanged: nil];
277 - (IBAction)openFileGeneric:(id)sender
279 [self openFilePathChanged: nil];
280 [self openTarget: 0];
283 - (IBAction)openDisc:(id)sender
285 [self openDiscTypeChanged: nil];
286 [self openTarget: 1];
289 - (IBAction)openNet:(id)sender
291 [self openNetModeChanged: nil];
292 [self openTarget: 2];
295 - (void)openFilePathChanged:(NSNotification *)o_notification
297 NSString *o_mrl_string;
298 NSString *o_filename = [o_file_path stringValue];
299 NSString *o_ext = [o_filename pathExtension];
300 vlc_bool_t b_stream = [o_file_stream state];
303 [[NSFileManager defaultManager] fileExistsAtPath:o_filename isDirectory:&b_dir];
307 o_mrl_string = [NSString stringWithFormat: @"dir:%@", o_filename];
309 else if( [o_ext isEqualToString: @"bin"] ||
310 [o_ext isEqualToString: @"cue"] ||
311 [o_ext isEqualToString: @"vob"] ||
312 [o_ext isEqualToString: @"iso"] )
314 o_mrl_string = o_filename;
318 o_mrl_string = [NSString stringWithFormat: @"%s://%@",
319 b_stream ? "stream" : "file",
322 [o_mrl setStringValue: o_mrl_string];
325 - (IBAction)openFileBrowse:(id)sender
327 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
329 [o_open_panel setAllowsMultipleSelection: NO];
330 [o_open_panel setCanChooseDirectories: YES];
331 [o_open_panel setTitle: _NS("Open File")];
332 [o_open_panel setPrompt: _NS("Open")];
334 [o_open_panel beginSheetForDirectory:nil
337 modalForWindow:[sender window]
339 didEndSelector: @selector(pathChosenInPanel:
345 - (void)pathChosenInPanel: (NSOpenPanel *) sheet withReturn:(int)returnCode contextInfo:(void *)contextInfo
347 if (returnCode == NSFileHandlingPanelOKButton)
349 NSString *o_filename = [[sheet filenames] objectAtIndex: 0];
350 [o_file_path setStringValue: o_filename];
351 [self openFilePathChanged: nil];
355 - (IBAction)openFileStreamChanged:(id)sender
357 [self openFilePathChanged: nil];
360 - (IBAction)openDiscTypeChanged:(id)sender
363 vlc_bool_t b_device, b_menus, b_title_chapter;
365 [o_disc_device removeAllItems];
366 b_title_chapter = ![o_disc_dvd_menus state];
368 o_type = [[o_disc_type selectedCell] title];
370 if ( [o_type isEqualToString: _NS("VIDEO_TS folder")] )
372 b_device = 0; b_menus = 1;
378 const char *psz_class = NULL;
381 if ( [o_type isEqualToString: _NS("VCD")] )
383 psz_class = kIOCDMediaClass;
385 b_menus = 0; b_title_chapter = 1;
386 [o_disc_dvd_menus setState: FALSE];
390 psz_class = kIODVDMediaClass;
395 o_devices = GetEjectableMediaOfClass( psz_class );
396 if ( o_devices != nil )
398 int i_devices = [o_devices count];
404 for( i = 0; i < i_devices; i++ )
407 addItemWithObjectValue: [o_devices objectAtIndex: i]];
410 [o_disc_device selectItemAtIndex: 0];
414 [o_disc_device setStringValue:
415 [NSString stringWithFormat: _NS("No %@s found"), o_disc]];
420 [o_disc_device setEnabled: b_device];
421 [o_disc_title setEnabled: b_title_chapter];
422 [o_disc_title_stp setEnabled: b_title_chapter];
423 [o_disc_chapter setEnabled: b_title_chapter];
424 [o_disc_chapter_stp setEnabled: b_title_chapter];
425 [o_disc_videots_folder setEnabled: !b_device];
426 [o_disc_videots_btn_browse setEnabled: !b_device];
427 [o_disc_dvd_menus setEnabled: b_menus];
429 [self openDiscInfoChanged: nil];
432 - (IBAction)openDiscStepperChanged:(id)sender
434 int i_tag = [sender tag];
438 [o_disc_title setIntValue: [o_disc_title_stp intValue]];
440 else if( i_tag == 1 )
442 [o_disc_chapter setIntValue: [o_disc_chapter_stp intValue]];
445 [self openDiscInfoChanged: nil];
448 - (void)openDiscInfoChanged:(NSNotification *)o_notification
453 NSString *o_mrl_string;
454 int i_title, i_chapter;
457 o_type = [[o_disc_type selectedCell] title];
458 o_device = [o_disc_device stringValue];
459 i_title = [o_disc_title intValue];
460 i_chapter = [o_disc_chapter intValue];
461 o_videots = [o_disc_videots_folder stringValue];
462 b_menus = [o_disc_dvd_menus state];
464 if ( [o_type isEqualToString: _NS("VCD")] )
466 if ( [o_device isEqualToString:
467 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
469 o_mrl_string = [NSString stringWithFormat: @"vcd://%@@%i,%i",
470 o_device, i_title, i_chapter];
472 else if ( [o_type isEqualToString: _NS("DVD")] )
474 if ( [o_device isEqualToString:
475 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
478 o_mrl_string = [NSString stringWithFormat: @"dvdplay://%@",
481 o_mrl_string = [NSString stringWithFormat: @"dvdold://%@@%i,%i",
482 o_device, i_title, i_chapter];
484 else /* VIDEO_TS folder */
487 o_mrl_string = [NSString stringWithFormat: @"dvdplay://%@",
490 o_mrl_string = [NSString stringWithFormat: @"dvdread://%@@%i,%i",
491 o_videots, i_title, i_chapter];
494 [o_mrl setStringValue: o_mrl_string];
497 - (IBAction)openDiscMenusChanged:(id)sender
499 [self openDiscInfoChanged: nil];
500 [self openDiscTypeChanged: nil];
503 - (IBAction)openVTSBrowse:(id)sender
505 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
507 [o_open_panel setAllowsMultipleSelection: NO];
508 [o_open_panel setCanChooseFiles: NO];
509 [o_open_panel setCanChooseDirectories: YES];
510 [o_open_panel setTitle: _NS("Open VIDEO_TS Directory")];
511 [o_open_panel setPrompt: _NS("Open")];
513 if( [o_open_panel runModalForDirectory: nil
514 file: nil types: nil] == NSOKButton )
516 NSString *o_dirname = [[o_open_panel filenames] objectAtIndex: 0];
517 [o_disc_videots_folder setStringValue: o_dirname];
518 [self openDiscInfoChanged: nil];
522 - (IBAction)openNetModeChanged:(id)sender
529 o_mode = [[o_net_mode selectedCell] title];
531 if( [o_mode isEqualToString: _NS("UDP/RTP")] ) b_udp = TRUE;
532 else if( [o_mode isEqualToString: _NS("UDP/RTP Multicast")] ) b_udpm = TRUE;
533 else if( [o_mode isEqualToString: _NS("HTTP/FTP/MMS")] ) b_http = TRUE;
535 [o_net_udp_port setEnabled: b_udp];
536 [o_net_udp_port_stp setEnabled: b_udp];
537 [o_net_udpm_addr setEnabled: b_udpm];
538 [o_net_udpm_port setEnabled: b_udpm];
539 [o_net_udpm_port_stp setEnabled: b_udpm];
540 [o_net_http_url setEnabled: b_http];
542 [self openNetInfoChanged: nil];
545 - (IBAction)openNetStepperChanged:(id)sender
547 int i_tag = [sender tag];
551 [o_net_udp_port setIntValue: [o_net_udp_port_stp intValue]];
553 else if( i_tag == 1 )
555 [o_net_udpm_port setIntValue: [o_net_udpm_port_stp intValue]];
558 [self openNetInfoChanged: nil];
561 - (void)openNetInfoChanged:(NSNotification *)o_notification
564 NSString *o_mrl_string = [NSString string];
565 intf_thread_t * p_intf = [NSApp getIntf];
567 o_mode = [[o_net_mode selectedCell] title];
569 if( [o_mode isEqualToString: _NS("UDP/RTP")] )
571 int i_port = [o_net_udp_port intValue];
573 o_mrl_string = [NSString stringWithString: @"udp://"];
575 if( i_port != config_GetInt( p_intf, "server-port" ) )
578 [o_mrl_string stringByAppendingFormat: @"@:%i", i_port];
581 else if( [o_mode isEqualToString: _NS("UDP/RTP Multicast")] )
583 NSString *o_addr = [o_net_udpm_addr stringValue];
584 int i_port = [o_net_udpm_port intValue];
586 o_mrl_string = [NSString stringWithFormat: @"udp://@%@", o_addr];
588 if( i_port != config_GetInt( p_intf, "server-port" ) )
591 [o_mrl_string stringByAppendingFormat: @":%i", i_port];
594 else if( [o_mode isEqualToString: _NS("HTTP/FTP/MMS")] )
596 NSString *o_url = [o_net_http_url stringValue];
598 if ( ![o_url hasPrefix:@"http:"] && ![o_url hasPrefix:@"ftp:"]
599 && ![o_url hasPrefix:@"mms"] )
600 o_mrl_string = [NSString stringWithFormat: @"http://%@", o_url];
602 o_mrl_string = o_url;
605 [o_mrl setStringValue: o_mrl_string];
608 - (IBAction)openFile:(id)sender
610 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
612 [o_open_panel setAllowsMultipleSelection: YES];
613 [o_open_panel setCanChooseDirectories: YES];
614 [o_open_panel setTitle: _NS("Open File")];
615 [o_open_panel setPrompt: _NS("Open")];
617 if( [o_open_panel runModalForDirectory: nil
618 file: nil types: nil] == NSOKButton )
620 intf_thread_t * p_intf = [NSApp getIntf];
621 config_PutPsz( p_intf, "sout", NULL );
623 NSArray *o_values = [[o_open_panel filenames]
624 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
626 [o_playlist appendArray: o_values atPos: -1 enqueue:NO];
627 config_PutPsz( p_intf, "sub-file", "" );
628 config_PutInt( p_intf, "sub-delay", 0 );
629 config_PutFloat( p_intf, "sub-fps", 0.0 );
630 config_PutPsz( p_intf, "sout", "" );
634 - (IBAction)subsChanged:(id)sender
636 if ([o_file_sub_ckbox state] == NSOnState)
638 [o_file_sub_btn_settings setEnabled:YES];
642 [o_file_sub_btn_settings setEnabled:NO];
646 - (IBAction)subSettings:(id)sender
648 [NSApp beginSheet: o_file_sub_sheet
649 modalForWindow: [sender window]
655 - (IBAction)subFileBrowse:(id)sender
657 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
659 [o_open_panel setAllowsMultipleSelection: NO];
660 [o_open_panel setTitle: _NS("Open File")];
661 [o_open_panel setPrompt: _NS("Open")];
663 if( [o_open_panel runModalForDirectory: nil
664 file: nil types: nil] == NSOKButton )
666 NSString *o_filename = [[o_open_panel filenames] objectAtIndex: 0];
667 [o_file_sub_path setStringValue: o_filename];
671 - (IBAction)subOverride:(id)sender
673 BOOL b_state = [o_file_sub_override state];
674 [o_file_sub_delay setEnabled: b_state];
675 [o_file_sub_delay_stp setEnabled: b_state];
676 [o_file_sub_fps setEnabled: b_state];
677 [o_file_sub_fps_stp setEnabled: b_state];
680 - (IBAction)subDelayStepperChanged:(id)sender
682 [o_file_sub_delay setIntValue: [o_file_sub_delay_stp intValue]];
685 - (IBAction)subFpsStepperChanged:(id)sender;
687 [o_file_sub_fps setFloatValue: [o_file_sub_fps_stp floatValue]];
690 - (IBAction)subCloseSheet:(id)sender
692 [o_file_sub_sheet orderOut:sender];
693 [NSApp endSheet: o_file_sub_sheet];
696 - (IBAction)panelCancel:(id)sender
698 [NSApp stopModalWithCode: 0];
701 - (IBAction)panelOk:(id)sender
703 if( [[o_mrl stringValue] length] )
705 [NSApp stopModalWithCode: 1];