1 /*****************************************************************************
2 * open.m: MacOS X plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: open.m,v 1.37 2003/09/09 13:51:45 hartman 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>
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( kIOMediaEjectable ),
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,
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 = [NSApp getIntf];
135 [o_panel setTitle: _NS("Open Source")];
136 [o_mrl_lbl setTitle: _NS("Media Resource Locator (MRL)")];
137 [o_ckbox_enqueue setTitle: _NS("Only enqueue in playlist, do not play")];
139 [o_btn_ok setTitle: _NS("OK")];
140 [o_btn_cancel setTitle: _NS("Cancel")];
142 [[o_tabview tabViewItemAtIndex: 0] setLabel: _NS("File")];
143 [[o_tabview tabViewItemAtIndex: 1] setLabel: _NS("Disc")];
144 [[o_tabview tabViewItemAtIndex: 2] setLabel: _NS("Network")];
146 [o_file_btn_browse setTitle: _NS("Browse...")];
147 [o_file_stream setTitle: _NS("Treat as a pipe rather than as a file")];
149 [o_disc_device_lbl setStringValue: _NS("Device name")];
150 [o_disc_title_lbl setStringValue: _NS("Title")];
151 [o_disc_chapter_lbl setStringValue: _NS("Chapter")];
152 [o_disc_videots_btn_browse setTitle: _NS("Browse...")];
153 [o_disc_dvd_menus setTitle: _NS("Use DVD menus")];
155 [[o_disc_type cellAtRow:0 column:0] setTitle: _NS("VIDEO_TS folder")];
156 [[o_disc_type cellAtRow:1 column:0] setTitle: _NS("DVD")];
157 [[o_disc_type cellAtRow:2 column:0] setTitle: _NS("VCD")];
158 [[o_disc_type cellAtRow:3 column:0] setTitle: _NS("Audio CD")];
160 [o_net_udp_port_lbl setStringValue: _NS("Port")];
161 [o_net_udpm_addr_lbl setStringValue: _NS("Address")];
162 [o_net_udpm_port_lbl setStringValue: _NS("Port")];
163 [o_net_http_url_lbl setStringValue: _NS("URL")];
165 [[o_net_mode cellAtRow:0 column:0] setTitle: _NS("UDP/RTP")];
166 [[o_net_mode cellAtRow:1 column:0] setTitle: _NS("UDP/RTP Multicast")];
167 [[o_net_mode cellAtRow:2 column:0] setTitle: _NS("HTTP/FTP/MMS")];
169 [o_net_udp_port setIntValue: config_GetInt( p_intf, "server-port" )];
170 [o_net_udp_port_stp setIntValue: config_GetInt( p_intf, "server-port" )];
172 [o_file_sub_ckbox setTitle: _NS("Load subtitles file:")];
173 [o_file_sub_btn_settings setTitle: _NS("Settings...")];
174 [o_file_sub_btn_browse setTitle: _NS("Browse...")];
175 [o_file_sub_override setTitle: _NS("Override")];
176 [o_file_sub_delay_lbl setStringValue: _NS("delay")];
177 [o_file_sub_delay_stp setEnabled: NO];
178 [o_file_sub_fps_lbl setStringValue: _NS("fps")];
179 [o_file_sub_fps_stp setEnabled: NO];
180 [o_file_sub_ok_btn setStringValue: _NS("OK")];
182 [[NSNotificationCenter defaultCenter] addObserver: self
183 selector: @selector(openFilePathChanged:)
184 name: NSControlTextDidChangeNotification
185 object: o_file_path];
187 [[NSNotificationCenter defaultCenter] addObserver: self
188 selector: @selector(openDiscInfoChanged:)
189 name: NSControlTextDidChangeNotification
190 object: o_disc_device];
191 [[NSNotificationCenter defaultCenter] addObserver: self
192 selector: @selector(openDiscInfoChanged:)
193 name: NSControlTextDidChangeNotification
194 object: o_disc_title];
195 [[NSNotificationCenter defaultCenter] addObserver: self
196 selector: @selector(openDiscInfoChanged:)
197 name: NSControlTextDidChangeNotification
198 object: o_disc_chapter];
199 [[NSNotificationCenter defaultCenter] addObserver: self
200 selector: @selector(openDiscInfoChanged:)
201 name: NSControlTextDidChangeNotification
202 object: o_disc_videots_folder];
204 [[NSNotificationCenter defaultCenter] addObserver: self
205 selector: @selector(openNetInfoChanged:)
206 name: NSControlTextDidChangeNotification
207 object: o_net_udp_port];
208 [[NSNotificationCenter defaultCenter] addObserver: self
209 selector: @selector(openNetInfoChanged:)
210 name: NSControlTextDidChangeNotification
211 object: o_net_udpm_addr];
212 [[NSNotificationCenter defaultCenter] addObserver: self
213 selector: @selector(openNetInfoChanged:)
214 name: NSControlTextDidChangeNotification
215 object: o_net_udpm_port];
216 [[NSNotificationCenter defaultCenter] addObserver: self
217 selector: @selector(openNetInfoChanged:)
218 name: NSControlTextDidChangeNotification
219 object: o_net_http_url];
222 - (void)openTarget:(int)i_type
226 [o_tabview selectTabViewItemAtIndex: i_type];
227 [o_file_sub_ckbox setState: NSOffState];
229 i_result = [NSApp runModalForWindow: o_panel];
234 NSMutableDictionary *o_dic;
235 NSMutableArray *o_options = [NSMutableArray array];
236 BOOL b_enq = [o_ckbox_enqueue state] == NSOnState ? YES : NO;
237 o_dic = [NSMutableDictionary dictionaryWithObject: [o_mrl stringValue] forKey: @"ITEM_URL"];
239 if( [o_file_sub_ckbox state] == NSOnState )
241 [o_options addObject: [NSString stringWithFormat: @"sub-file=%s", [[o_file_sub_path stringValue] UTF8String]]];
242 if( [o_file_sub_override state] == NSOnState )
244 [o_options addObject: [NSString stringWithFormat: @"sub-delay=%i", (int)( [o_file_sub_delay intValue] * 10 )]];
245 [o_options addObject: [NSString stringWithFormat: @"sub-fps=%f", [o_file_sub_fps floatValue]]];
248 if( [o_output_ckbox state] == NSOnState )
250 [o_options addObject: [NSString stringWithFormat: @"sout=%@", [(VLCOutput *)o_sout_options getMRL]]];
252 [o_dic setObject: (NSArray *)[o_options copy] forKey: @"ITEM_OPTIONS"];
253 [o_playlist appendArray: [NSArray arrayWithObject: o_dic] atPos: -1 enqueue:b_enq];
257 - (void)tabView:(NSTabView *)o_tv didSelectTabViewItem:(NSTabViewItem *)o_tvi
259 NSString *o_label = [o_tvi label];
261 if( [o_label isEqualToString: _NS("File")] )
263 [self openFilePathChanged: nil];
265 else if( [o_label isEqualToString: _NS("Disc")] )
267 [self openDiscTypeChanged: nil];
269 else if( [o_label isEqualToString: _NS("Network")] )
271 [self openNetModeChanged: nil];
275 - (IBAction)openFileGeneric:(id)sender
277 [self openFilePathChanged: nil];
278 [self openTarget: 0];
281 - (IBAction)openDisc:(id)sender
283 [self openDiscTypeChanged: nil];
284 [self openTarget: 1];
287 - (IBAction)openNet:(id)sender
289 [self openNetModeChanged: nil];
290 [self openTarget: 2];
293 - (void)openFilePathChanged:(NSNotification *)o_notification
295 NSString *o_mrl_string;
296 NSString *o_filename = [o_file_path stringValue];
297 NSString *o_ext = [o_filename pathExtension];
298 vlc_bool_t b_stream = [o_file_stream state];
301 [[NSFileManager defaultManager] fileExistsAtPath:o_filename isDirectory:&b_dir];
305 o_mrl_string = [NSString stringWithFormat: @"dir:%@", o_filename];
307 else if( [o_ext isEqualToString: @"bin"] ||
308 [o_ext isEqualToString: @"cue"] ||
309 [o_ext isEqualToString: @"vob"] ||
310 [o_ext isEqualToString: @"iso"] )
312 o_mrl_string = o_filename;
316 o_mrl_string = [NSString stringWithFormat: @"%s://%@",
317 b_stream ? "stream" : "file",
320 [o_mrl setStringValue: o_mrl_string];
323 - (IBAction)openFileBrowse:(id)sender
325 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
327 [o_open_panel setAllowsMultipleSelection: NO];
328 [o_open_panel setCanChooseDirectories: YES];
329 [o_open_panel setTitle: _NS("Open File")];
330 [o_open_panel setPrompt: _NS("Open")];
332 [o_open_panel beginSheetForDirectory:nil
335 modalForWindow:[sender window]
337 didEndSelector: @selector(pathChosenInPanel:
343 - (void)pathChosenInPanel: (NSOpenPanel *) sheet withReturn:(int)returnCode contextInfo:(void *)contextInfo
345 if (returnCode == NSFileHandlingPanelOKButton)
347 NSString *o_filename = [[sheet filenames] objectAtIndex: 0];
348 [o_file_path setStringValue: o_filename];
349 [self openFilePathChanged: nil];
353 - (IBAction)openFileStreamChanged:(id)sender
355 [self openFilePathChanged: nil];
358 - (IBAction)openDiscTypeChanged:(id)sender
361 vlc_bool_t b_device, b_menus, b_title_chapter;
363 [o_disc_device removeAllItems];
364 b_title_chapter = ![o_disc_dvd_menus state];
366 o_type = [[o_disc_type selectedCell] title];
368 if ( [o_type isEqualToString: _NS("VIDEO_TS folder")] )
370 b_device = 0; b_menus = 1;
376 const char *psz_class = NULL;
379 if ( [o_type isEqualToString: _NS("VCD")] )
381 psz_class = kIOCDMediaClass;
383 b_menus = 0; b_title_chapter = 1;
384 [o_disc_dvd_menus setState: FALSE];
386 else if ( [o_type isEqualToString: _NS("Audio CD")])
388 psz_class = kIOCDMediaClass;
390 b_menus = 0; b_title_chapter = 0;
391 [o_disc_dvd_menus setState: FALSE];
395 psz_class = kIODVDMediaClass;
400 o_devices = GetEjectableMediaOfClass( psz_class );
401 if ( o_devices != nil )
403 int i_devices = [o_devices count];
409 for( i = 0; i < i_devices; i++ )
412 addItemWithObjectValue: [o_devices objectAtIndex: i]];
415 [o_disc_device selectItemAtIndex: 0];
419 [o_disc_device setStringValue:
420 [NSString stringWithFormat: _NS("No %@s found"), o_disc]];
425 [o_disc_device setEnabled: b_device];
426 [o_disc_title setEnabled: b_title_chapter];
427 [o_disc_title_stp setEnabled: b_title_chapter];
428 [o_disc_chapter setEnabled: b_title_chapter];
429 [o_disc_chapter_stp setEnabled: b_title_chapter];
430 [o_disc_videots_folder setEnabled: !b_device];
431 [o_disc_videots_btn_browse setEnabled: !b_device];
432 [o_disc_dvd_menus setEnabled: b_menus];
434 [self openDiscInfoChanged: nil];
437 - (IBAction)openDiscStepperChanged:(id)sender
439 int i_tag = [sender tag];
443 [o_disc_title setIntValue: [o_disc_title_stp intValue]];
445 else if( i_tag == 1 )
447 [o_disc_chapter setIntValue: [o_disc_chapter_stp intValue]];
450 [self openDiscInfoChanged: nil];
453 - (void)openDiscInfoChanged:(NSNotification *)o_notification
458 NSString *o_mrl_string;
459 int i_title, i_chapter;
462 o_type = [[o_disc_type selectedCell] title];
463 o_device = [o_disc_device stringValue];
464 i_title = [o_disc_title intValue];
465 i_chapter = [o_disc_chapter intValue];
466 o_videots = [o_disc_videots_folder stringValue];
467 b_menus = [o_disc_dvd_menus state];
469 if ( [o_type isEqualToString: _NS("VCD")] )
471 if ( [o_device isEqualToString:
472 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
474 o_mrl_string = [NSString stringWithFormat: @"vcd://%@@%i,%i",
475 o_device, i_title, i_chapter];
477 else if ( [o_type isEqualToString: _NS("Audio CD")] )
479 if ( [o_device isEqualToString:
480 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
482 o_mrl_string = [NSString stringWithFormat: @"cdda://%@",
485 else if ( [o_type isEqualToString: _NS("DVD")] )
487 if ( [o_device isEqualToString:
488 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
491 o_mrl_string = [NSString stringWithFormat: @"dvdplay://%@",
494 o_mrl_string = [NSString stringWithFormat: @"dvdold://%@@%i,%i",
495 o_device, i_title, i_chapter];
497 else /* VIDEO_TS folder */
500 o_mrl_string = [NSString stringWithFormat: @"dvdplay://%@",
503 o_mrl_string = [NSString stringWithFormat: @"dvdread://%@@%i,%i",
504 o_videots, i_title, i_chapter];
507 [o_mrl setStringValue: o_mrl_string];
510 - (IBAction)openDiscMenusChanged:(id)sender
512 [self openDiscInfoChanged: nil];
513 [self openDiscTypeChanged: nil];
516 - (IBAction)openVTSBrowse:(id)sender
518 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
520 [o_open_panel setAllowsMultipleSelection: NO];
521 [o_open_panel setCanChooseFiles: NO];
522 [o_open_panel setCanChooseDirectories: YES];
523 [o_open_panel setTitle: _NS("Open VIDEO_TS Directory")];
524 [o_open_panel setPrompt: _NS("Open")];
526 if( [o_open_panel runModalForDirectory: nil
527 file: nil types: nil] == NSOKButton )
529 NSString *o_dirname = [[o_open_panel filenames] objectAtIndex: 0];
530 [o_disc_videots_folder setStringValue: o_dirname];
531 [self openDiscInfoChanged: nil];
535 - (IBAction)openNetModeChanged:(id)sender
542 o_mode = [[o_net_mode selectedCell] title];
544 if( [o_mode isEqualToString: _NS("UDP/RTP")] ) b_udp = TRUE;
545 else if( [o_mode isEqualToString: _NS("UDP/RTP Multicast")] ) b_udpm = TRUE;
546 else if( [o_mode isEqualToString: _NS("HTTP/FTP/MMS")] ) b_http = TRUE;
548 [o_net_udp_port setEnabled: b_udp];
549 [o_net_udp_port_stp setEnabled: b_udp];
550 [o_net_udpm_addr setEnabled: b_udpm];
551 [o_net_udpm_port setEnabled: b_udpm];
552 [o_net_udpm_port_stp setEnabled: b_udpm];
553 [o_net_http_url setEnabled: b_http];
555 [self openNetInfoChanged: nil];
558 - (IBAction)openNetStepperChanged:(id)sender
560 int i_tag = [sender tag];
564 [o_net_udp_port setIntValue: [o_net_udp_port_stp intValue]];
566 else if( i_tag == 1 )
568 [o_net_udpm_port setIntValue: [o_net_udpm_port_stp intValue]];
571 [self openNetInfoChanged: nil];
574 - (void)openNetInfoChanged:(NSNotification *)o_notification
577 NSString *o_mrl_string = [NSString string];
578 intf_thread_t * p_intf = [NSApp getIntf];
580 o_mode = [[o_net_mode selectedCell] title];
582 if( [o_mode isEqualToString: _NS("UDP/RTP")] )
584 int i_port = [o_net_udp_port intValue];
586 o_mrl_string = [NSString stringWithString: @"udp://"];
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("UDP/RTP Multicast")] )
596 NSString *o_addr = [o_net_udpm_addr stringValue];
597 int i_port = [o_net_udpm_port intValue];
599 o_mrl_string = [NSString stringWithFormat: @"udp://@%@", o_addr];
601 if( i_port != config_GetInt( p_intf, "server-port" ) )
604 [o_mrl_string stringByAppendingFormat: @":%i", i_port];
607 else if( [o_mode isEqualToString: _NS("HTTP/FTP/MMS")] )
609 NSString *o_url = [o_net_http_url stringValue];
611 if ( ![o_url hasPrefix:@"http:"] && ![o_url hasPrefix:@"ftp:"]
612 && ![o_url hasPrefix:@"mms"] )
613 o_mrl_string = [NSString stringWithFormat: @"http://%@", o_url];
615 o_mrl_string = o_url;
618 [o_mrl setStringValue: o_mrl_string];
621 - (IBAction)openFile:(id)sender
623 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
626 [o_open_panel setAllowsMultipleSelection: YES];
627 [o_open_panel setCanChooseDirectories: YES];
628 [o_open_panel setTitle: _NS("Open File")];
629 [o_open_panel setPrompt: _NS("Open")];
631 if( [o_open_panel runModalForDirectory: nil
632 file: nil types: nil] == NSOKButton )
634 NSArray *o_array = [NSArray array];
635 NSArray *o_values = [[o_open_panel filenames]
636 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
638 for( i = 0; i < [o_values count]; i++)
641 o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
642 o_array = [o_array arrayByAddingObject: o_dic];
644 [o_playlist appendArray: o_array atPos: -1 enqueue:NO];
648 - (IBAction)subsChanged:(id)sender
650 if ([o_file_sub_ckbox state] == NSOnState)
652 [o_file_sub_btn_settings setEnabled:YES];
656 [o_file_sub_btn_settings setEnabled:NO];
660 - (IBAction)subSettings:(id)sender
662 [NSApp beginSheet: o_file_sub_sheet
663 modalForWindow: [sender window]
669 - (IBAction)subFileBrowse:(id)sender
671 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
673 [o_open_panel setAllowsMultipleSelection: NO];
674 [o_open_panel setTitle: _NS("Open File")];
675 [o_open_panel setPrompt: _NS("Open")];
677 if( [o_open_panel runModalForDirectory: nil
678 file: nil types: nil] == NSOKButton )
680 NSString *o_filename = [[o_open_panel filenames] objectAtIndex: 0];
681 [o_file_sub_path setStringValue: o_filename];
685 - (IBAction)subOverride:(id)sender
687 BOOL b_state = [o_file_sub_override state];
688 [o_file_sub_delay setEnabled: b_state];
689 [o_file_sub_delay_stp setEnabled: b_state];
690 [o_file_sub_fps setEnabled: b_state];
691 [o_file_sub_fps_stp setEnabled: b_state];
694 - (IBAction)subDelayStepperChanged:(id)sender
696 [o_file_sub_delay setIntValue: [o_file_sub_delay_stp intValue]];
699 - (IBAction)subFpsStepperChanged:(id)sender;
701 [o_file_sub_fps setFloatValue: [o_file_sub_fps_stp floatValue]];
704 - (IBAction)subCloseSheet:(id)sender
706 [o_file_sub_sheet orderOut:sender];
707 [NSApp endSheet: o_file_sub_sheet];
710 - (IBAction)panelCancel:(id)sender
712 [NSApp stopModalWithCode: 0];
715 - (IBAction)panelOk:(id)sender
717 if( [[o_mrl stringValue] length] )
719 [NSApp stopModalWithCode: 1];