1 /*****************************************************************************
2 * open.m: MacOS X module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
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>
45 /*****************************************************************************
46 * GetEjectableMediaOfClass
47 *****************************************************************************/
48 NSArray *GetEjectableMediaOfClass( const char *psz_class )
50 io_object_t next_media;
51 mach_port_t master_port;
52 kern_return_t kern_result;
53 NSArray *o_devices = nil;
54 NSMutableArray *p_list = nil;
55 io_iterator_t media_iterator;
56 CFMutableDictionaryRef classes_to_match;
58 kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
59 if( kern_result != KERN_SUCCESS )
64 classes_to_match = IOServiceMatching( psz_class );
65 if( classes_to_match == NULL )
70 CFDictionarySetValue( classes_to_match, CFSTR( kIOMediaEjectableKey ),
73 kern_result = IOServiceGetMatchingServices( master_port, classes_to_match,
75 if( kern_result != KERN_SUCCESS )
80 p_list = [NSMutableArray arrayWithCapacity: 1];
82 next_media = IOIteratorNext( media_iterator );
83 if( next_media != NULL )
86 size_t dev_path_length;
87 CFTypeRef str_bsd_path;
91 str_bsd_path = IORegistryEntryCreateCFProperty( next_media,
92 CFSTR( kIOBSDNameKey ),
95 if( str_bsd_path == NULL )
97 IOObjectRelease( next_media );
101 snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
102 dev_path_length = strlen( psz_buf );
104 if( CFStringGetCString( str_bsd_path,
105 (char*)&psz_buf + dev_path_length,
106 sizeof(psz_buf) - dev_path_length,
107 kCFStringEncodingASCII ) )
109 [p_list addObject: [NSString stringWithCString: psz_buf]];
112 CFRelease( str_bsd_path );
114 IOObjectRelease( next_media );
116 } while( ( next_media = IOIteratorNext( media_iterator ) ) != NULL );
119 IOObjectRelease( media_iterator );
121 o_devices = [NSArray arrayWithArray: p_list];
126 /*****************************************************************************
127 * VLCOpen implementation
128 *****************************************************************************/
129 @implementation VLCOpen
133 intf_thread_t * p_intf = VLCIntf;
135 [o_panel setTitle: _NS("Open Source")];
136 [o_mrl_lbl setTitle: _NS("Media Resource Locator (MRL)")];
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")];
157 [[o_disc_type cellAtRow:3 column:0] setTitle: _NS("Audio CD")];
159 [o_net_udp_port_lbl setStringValue: _NS("Port")];
160 [o_net_udpm_addr_lbl setStringValue: _NS("Address")];
161 [o_net_udpm_port_lbl setStringValue: _NS("Port")];
162 [o_net_http_url_lbl setStringValue: _NS("URL")];
164 [[o_net_mode cellAtRow:0 column:0] setTitle: _NS("UDP/RTP")];
165 [[o_net_mode cellAtRow:1 column:0] setTitle: _NS("UDP/RTP Multicast")];
166 [[o_net_mode cellAtRow:2 column:0] setTitle: _NS("HTTP/FTP/MMS")];
168 [o_net_udp_port setIntValue: config_GetInt( p_intf, "server-port" )];
169 [o_net_udp_port_stp setIntValue: config_GetInt( p_intf, "server-port" )];
171 [o_file_sub_ckbox setTitle: _NS("Load subtitles file:")];
172 [o_file_sub_btn_settings setTitle: _NS("Settings...")];
173 [o_file_sub_btn_browse setTitle: _NS("Browse...")];
174 [o_file_sub_override setTitle: _NS("Override")];
175 [o_file_sub_delay_lbl setStringValue: _NS("delay")];
176 [o_file_sub_delay_stp setEnabled: NO];
177 [o_file_sub_fps_lbl setStringValue: _NS("fps")];
178 [o_file_sub_fps_stp setEnabled: NO];
179 [o_file_sub_ok_btn setStringValue: _NS("OK")];
181 [[NSNotificationCenter defaultCenter] addObserver: self
182 selector: @selector(openFilePathChanged:)
183 name: NSControlTextDidChangeNotification
184 object: o_file_path];
186 [[NSNotificationCenter defaultCenter] addObserver: self
187 selector: @selector(openDiscInfoChanged:)
188 name: NSControlTextDidChangeNotification
189 object: o_disc_device];
190 [[NSNotificationCenter defaultCenter] addObserver: self
191 selector: @selector(openDiscInfoChanged:)
192 name: NSControlTextDidChangeNotification
193 object: o_disc_title];
194 [[NSNotificationCenter defaultCenter] addObserver: self
195 selector: @selector(openDiscInfoChanged:)
196 name: NSControlTextDidChangeNotification
197 object: o_disc_chapter];
198 [[NSNotificationCenter defaultCenter] addObserver: self
199 selector: @selector(openDiscInfoChanged:)
200 name: NSControlTextDidChangeNotification
201 object: o_disc_videots_folder];
203 [[NSNotificationCenter defaultCenter] addObserver: self
204 selector: @selector(openNetInfoChanged:)
205 name: NSControlTextDidChangeNotification
206 object: o_net_udp_port];
207 [[NSNotificationCenter defaultCenter] addObserver: self
208 selector: @selector(openNetInfoChanged:)
209 name: NSControlTextDidChangeNotification
210 object: o_net_udpm_addr];
211 [[NSNotificationCenter defaultCenter] addObserver: self
212 selector: @selector(openNetInfoChanged:)
213 name: NSControlTextDidChangeNotification
214 object: o_net_udpm_port];
215 [[NSNotificationCenter defaultCenter] addObserver: self
216 selector: @selector(openNetInfoChanged:)
217 name: NSControlTextDidChangeNotification
218 object: o_net_http_url];
221 - (void)openTarget:(int)i_type
225 [o_tabview selectTabViewItemAtIndex: i_type];
226 [o_file_sub_ckbox setState: NSOffState];
228 i_result = [NSApp runModalForWindow: o_panel];
233 NSMutableDictionary *o_dic;
234 NSMutableArray *o_options = [NSMutableArray array];
237 o_dic = [NSMutableDictionary dictionaryWithObject: [o_mrl stringValue] forKey: @"ITEM_URL"];
238 if( [o_file_sub_ckbox state] == NSOnState )
240 [o_options addObject: [NSString stringWithFormat: @"sub-file=%s", [[o_file_sub_path stringValue] UTF8String]]];
241 if( [o_file_sub_override state] == NSOnState )
243 [o_options addObject: [NSString stringWithFormat: @"sub-delay=%i", (int)( [o_file_sub_delay intValue] * 10 )]];
244 [o_options addObject: [NSString stringWithFormat: @"sub-fps=%f", [o_file_sub_fps floatValue]]];
247 if( [o_output_ckbox state] == NSOnState )
249 for (i = 0 ; i < [[o_sout_options getMRL] count] ; i++)
251 [o_options addObject: [NSString stringWithString:
252 [[(VLCOutput *)o_sout_options getMRL] objectAtIndex: i]]];
255 [o_dic setObject: (NSArray *)[o_options copy] forKey: @"ITEM_OPTIONS"];
256 [o_playlist appendArray: [NSArray arrayWithObject: o_dic] atPos: -1 enqueue:NO];
260 - (void)tabView:(NSTabView *)o_tv didSelectTabViewItem:(NSTabViewItem *)o_tvi
262 NSString *o_label = [o_tvi label];
264 if( [o_label isEqualToString: _NS("File")] )
266 [self openFilePathChanged: nil];
268 else if( [o_label isEqualToString: _NS("Disc")] )
270 [self openDiscTypeChanged: nil];
272 else if( [o_label isEqualToString: _NS("Network")] )
274 [self openNetModeChanged: nil];
278 - (IBAction)openFileGeneric:(id)sender
280 [self openFilePathChanged: nil];
281 [self openTarget: 0];
284 - (IBAction)openDisc:(id)sender
286 [self openDiscTypeChanged: nil];
287 [self openTarget: 1];
290 - (IBAction)openNet:(id)sender
292 [self openNetModeChanged: nil];
293 [self openTarget: 2];
296 - (void)openFilePathChanged:(NSNotification *)o_notification
298 NSString *o_mrl_string;
299 NSString *o_filename = [o_file_path stringValue];
300 NSString *o_ext = [o_filename pathExtension];
301 vlc_bool_t b_stream = [o_file_stream state];
304 [[NSFileManager defaultManager] fileExistsAtPath:o_filename isDirectory:&b_dir];
308 o_mrl_string = [NSString stringWithFormat: @"dir:%@", o_filename];
310 else if( [o_ext isEqualToString: @"bin"] ||
311 [o_ext isEqualToString: @"cue"] ||
312 [o_ext isEqualToString: @"vob"] ||
313 [o_ext isEqualToString: @"iso"] )
315 o_mrl_string = o_filename;
319 o_mrl_string = [NSString stringWithFormat: @"%s://%@",
320 b_stream ? "stream" : "file",
323 [o_mrl setStringValue: o_mrl_string];
326 - (IBAction)openFileBrowse:(id)sender
328 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
330 [o_open_panel setAllowsMultipleSelection: NO];
331 [o_open_panel setCanChooseDirectories: YES];
332 [o_open_panel setTitle: _NS("Open File")];
333 [o_open_panel setPrompt: _NS("Open")];
335 [o_open_panel beginSheetForDirectory:nil
338 modalForWindow:[sender window]
340 didEndSelector: @selector(pathChosenInPanel:
346 - (void)pathChosenInPanel: (NSOpenPanel *) sheet withReturn:(int)returnCode contextInfo:(void *)contextInfo
348 if (returnCode == NSFileHandlingPanelOKButton)
350 NSString *o_filename = [[sheet filenames] objectAtIndex: 0];
351 [o_file_path setStringValue: o_filename];
352 [self openFilePathChanged: nil];
356 - (IBAction)openFileStreamChanged:(id)sender
358 [self openFilePathChanged: nil];
361 - (IBAction)openDiscTypeChanged:(id)sender
364 vlc_bool_t b_device, b_menus, b_title_chapter;
366 [o_disc_device removeAllItems];
367 b_title_chapter = ![o_disc_dvd_menus state];
369 o_type = [[o_disc_type selectedCell] title];
371 if ( [o_type isEqualToString: _NS("VIDEO_TS folder")] )
373 b_device = 0; b_menus = 1;
379 const char *psz_class = NULL;
382 if ( [o_type isEqualToString: _NS("VCD")] )
384 psz_class = kIOCDMediaClass;
386 b_menus = 0; b_title_chapter = 1;
387 [o_disc_dvd_menus setState: FALSE];
389 else if ( [o_type isEqualToString: _NS("Audio CD")])
391 psz_class = kIOCDMediaClass;
393 b_menus = 0; b_title_chapter = 0;
394 [o_disc_dvd_menus setState: FALSE];
398 psz_class = kIODVDMediaClass;
403 o_devices = GetEjectableMediaOfClass( psz_class );
404 if ( o_devices != nil )
406 int i_devices = [o_devices count];
412 for( i = 0; i < i_devices; i++ )
415 addItemWithObjectValue: [o_devices objectAtIndex: i]];
418 [o_disc_device selectItemAtIndex: 0];
422 [o_disc_device setStringValue:
423 [NSString stringWithFormat: _NS("No %@s found"), o_disc]];
428 [o_disc_device setEnabled: b_device];
429 [o_disc_title setEnabled: b_title_chapter];
430 [o_disc_title_stp setEnabled: b_title_chapter];
431 [o_disc_chapter setEnabled: b_title_chapter];
432 [o_disc_chapter_stp setEnabled: b_title_chapter];
433 [o_disc_videots_folder setEnabled: !b_device];
434 [o_disc_videots_btn_browse setEnabled: !b_device];
435 [o_disc_dvd_menus setEnabled: b_menus];
437 [self openDiscInfoChanged: nil];
440 - (IBAction)openDiscStepperChanged:(id)sender
442 int i_tag = [sender tag];
446 [o_disc_title setIntValue: [o_disc_title_stp intValue]];
448 else if( i_tag == 1 )
450 [o_disc_chapter setIntValue: [o_disc_chapter_stp intValue]];
453 [self openDiscInfoChanged: nil];
456 - (void)openDiscInfoChanged:(NSNotification *)o_notification
461 NSString *o_mrl_string;
462 int i_title, i_chapter;
465 o_type = [[o_disc_type selectedCell] title];
466 o_device = [o_disc_device stringValue];
467 i_title = [o_disc_title intValue];
468 i_chapter = [o_disc_chapter intValue];
469 o_videots = [o_disc_videots_folder stringValue];
470 b_menus = [o_disc_dvd_menus state];
472 if ( [o_type isEqualToString: _NS("VCD")] )
474 if ( [o_device isEqualToString:
475 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
477 o_mrl_string = [NSString stringWithFormat: @"vcd://%@@%i,%i",
478 o_device, i_title, i_chapter];
480 else if ( [o_type isEqualToString: _NS("Audio CD")] )
482 if ( [o_device isEqualToString:
483 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
485 o_mrl_string = [NSString stringWithFormat: @"cdda://%@",
488 else if ( [o_type isEqualToString: _NS("DVD")] )
490 if ( [o_device isEqualToString:
491 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
494 o_mrl_string = [NSString stringWithFormat: @"dvdnav://%@",
497 o_mrl_string = [NSString stringWithFormat: @"dvdread://%@@%i,%i",
498 o_device, i_title, i_chapter];
500 else /* VIDEO_TS folder */
503 o_mrl_string = [NSString stringWithFormat: @"dvdnav://%@",
506 o_mrl_string = [NSString stringWithFormat: @"dvdread://%@@%i,%i",
507 o_videots, i_title, i_chapter];
510 [o_mrl setStringValue: o_mrl_string];
513 - (IBAction)openDiscMenusChanged:(id)sender
515 [self openDiscInfoChanged: nil];
516 [self openDiscTypeChanged: nil];
519 - (IBAction)openVTSBrowse:(id)sender
521 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
523 [o_open_panel setAllowsMultipleSelection: NO];
524 [o_open_panel setCanChooseFiles: NO];
525 [o_open_panel setCanChooseDirectories: YES];
526 [o_open_panel setTitle: _NS("Open VIDEO_TS Directory")];
527 [o_open_panel setPrompt: _NS("Open")];
529 if( [o_open_panel runModalForDirectory: nil
530 file: nil types: nil] == NSOKButton )
532 NSString *o_dirname = [[o_open_panel filenames] objectAtIndex: 0];
533 [o_disc_videots_folder setStringValue: o_dirname];
534 [self openDiscInfoChanged: nil];
538 - (IBAction)openNetModeChanged:(id)sender
545 o_mode = [[o_net_mode selectedCell] title];
547 if( [o_mode isEqualToString: _NS("UDP/RTP")] ) b_udp = TRUE;
548 else if( [o_mode isEqualToString: _NS("UDP/RTP Multicast")] ) b_udpm = TRUE;
549 else if( [o_mode isEqualToString: _NS("HTTP/FTP/MMS")] ) b_http = TRUE;
551 [o_net_udp_port setEnabled: b_udp];
552 [o_net_udp_port_stp setEnabled: b_udp];
553 [o_net_udpm_addr setEnabled: b_udpm];
554 [o_net_udpm_port setEnabled: b_udpm];
555 [o_net_udpm_port_stp setEnabled: b_udpm];
556 [o_net_http_url setEnabled: b_http];
558 [self openNetInfoChanged: nil];
561 - (IBAction)openNetStepperChanged:(id)sender
563 int i_tag = [sender tag];
567 [o_net_udp_port setIntValue: [o_net_udp_port_stp intValue]];
569 else if( i_tag == 1 )
571 [o_net_udpm_port setIntValue: [o_net_udpm_port_stp intValue]];
574 [self openNetInfoChanged: nil];
577 - (void)openNetInfoChanged:(NSNotification *)o_notification
580 NSString *o_mrl_string = [NSString string];
581 intf_thread_t * p_intf = VLCIntf;
583 o_mode = [[o_net_mode selectedCell] title];
585 if( [o_mode isEqualToString: _NS("UDP/RTP")] )
587 int i_port = [o_net_udp_port intValue];
589 o_mrl_string = [NSString stringWithString: @"udp://"];
591 if( i_port != config_GetInt( p_intf, "server-port" ) )
594 [o_mrl_string stringByAppendingFormat: @"@:%i", i_port];
597 else if( [o_mode isEqualToString: _NS("UDP/RTP Multicast")] )
599 NSString *o_addr = [o_net_udpm_addr stringValue];
600 int i_port = [o_net_udpm_port intValue];
602 o_mrl_string = [NSString stringWithFormat: @"udp://@%@", o_addr];
604 if( i_port != config_GetInt( p_intf, "server-port" ) )
607 [o_mrl_string stringByAppendingFormat: @":%i", i_port];
610 else if( [o_mode isEqualToString: _NS("HTTP/FTP/MMS")] )
612 NSString *o_url = [o_net_http_url stringValue];
614 if ( ![o_url hasPrefix:@"http:"] && ![o_url hasPrefix:@"ftp:"]
615 && ![o_url hasPrefix:@"mms"] )
616 o_mrl_string = [NSString stringWithFormat: @"http://%@", o_url];
618 o_mrl_string = o_url;
621 [o_mrl setStringValue: o_mrl_string];
624 - (IBAction)openFile:(id)sender
626 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
629 [o_open_panel setAllowsMultipleSelection: YES];
630 [o_open_panel setCanChooseDirectories: YES];
631 [o_open_panel setTitle: _NS("Open File")];
632 [o_open_panel setPrompt: _NS("Open")];
634 if( [o_open_panel runModalForDirectory: nil
635 file: nil types: nil] == NSOKButton )
637 NSArray *o_array = [NSArray array];
638 NSArray *o_values = [[o_open_panel filenames]
639 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
641 for( i = 0; i < (int)[o_values count]; i++)
644 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
645 o_array = [o_array arrayByAddingObject: o_dic];
647 [o_playlist appendArray: o_array atPos: -1 enqueue:NO];
651 - (IBAction)subsChanged:(id)sender
653 if ([o_file_sub_ckbox state] == NSOnState)
655 [o_file_sub_btn_settings setEnabled:YES];
659 [o_file_sub_btn_settings setEnabled:NO];
663 - (IBAction)subSettings:(id)sender
665 [NSApp beginSheet: o_file_sub_sheet
666 modalForWindow: [sender window]
672 - (IBAction)subFileBrowse:(id)sender
674 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
676 [o_open_panel setAllowsMultipleSelection: NO];
677 [o_open_panel setTitle: _NS("Open File")];
678 [o_open_panel setPrompt: _NS("Open")];
680 if( [o_open_panel runModalForDirectory: nil
681 file: nil types: nil] == NSOKButton )
683 NSString *o_filename = [[o_open_panel filenames] objectAtIndex: 0];
684 [o_file_sub_path setStringValue: o_filename];
688 - (IBAction)subOverride:(id)sender
690 BOOL b_state = [o_file_sub_override state];
691 [o_file_sub_delay setEnabled: b_state];
692 [o_file_sub_delay_stp setEnabled: b_state];
693 [o_file_sub_fps setEnabled: b_state];
694 [o_file_sub_fps_stp setEnabled: b_state];
697 - (IBAction)subDelayStepperChanged:(id)sender
699 [o_file_sub_delay setIntValue: [o_file_sub_delay_stp intValue]];
702 - (IBAction)subFpsStepperChanged:(id)sender;
704 [o_file_sub_fps setFloatValue: [o_file_sub_fps_stp floatValue]];
707 - (IBAction)subCloseSheet:(id)sender
709 [o_file_sub_sheet orderOut:sender];
710 [NSApp endSheet: o_file_sub_sheet];
713 - (IBAction)panelCancel:(id)sender
715 [NSApp stopModalWithCode: 0];
718 - (IBAction)panelOk:(id)sender
720 if( [[o_mrl stringValue] length] )
722 [NSApp stopModalWithCode: 1];