1 /*****************************************************************************
2 * open.m: MacOS X plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: open.m,v 1.30 2003/04/30 23:58:56 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>
46 /*****************************************************************************
47 * GetEjectableMediaOfClass
48 *****************************************************************************/
49 NSArray *GetEjectableMediaOfClass( const char *psz_class )
51 io_object_t next_media;
52 mach_port_t master_port;
53 kern_return_t kern_result;
54 NSArray *o_devices = nil;
55 NSMutableArray *p_list = nil;
56 io_iterator_t media_iterator;
57 CFMutableDictionaryRef classes_to_match;
59 kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
60 if( kern_result != KERN_SUCCESS )
65 classes_to_match = IOServiceMatching( psz_class );
66 if( classes_to_match == NULL )
71 CFDictionarySetValue( classes_to_match, CFSTR( kIOMediaEjectable ),
74 kern_result = IOServiceGetMatchingServices( master_port, classes_to_match,
76 if( kern_result != KERN_SUCCESS )
81 p_list = [NSMutableArray arrayWithCapacity: 1];
83 next_media = IOIteratorNext( media_iterator );
84 if( next_media != NULL )
87 size_t dev_path_length;
88 CFTypeRef str_bsd_path;
92 str_bsd_path = IORegistryEntryCreateCFProperty( next_media,
96 if( str_bsd_path == NULL )
98 IOObjectRelease( next_media );
102 snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
103 dev_path_length = strlen( psz_buf );
105 if( CFStringGetCString( str_bsd_path,
106 (char*)&psz_buf + dev_path_length,
107 sizeof(psz_buf) - dev_path_length,
108 kCFStringEncodingASCII ) )
110 [p_list addObject: [NSString stringWithCString: psz_buf]];
113 CFRelease( str_bsd_path );
115 IOObjectRelease( next_media );
117 } while( ( next_media = IOIteratorNext( media_iterator ) ) != NULL );
120 IOObjectRelease( media_iterator );
122 o_devices = [NSArray arrayWithArray: p_list];
127 /*****************************************************************************
128 * VLCOpen implementation
129 *****************************************************************************/
130 @implementation VLCOpen
134 intf_thread_t * p_intf = [NSApp getIntf];
136 [o_panel setTitle: _NS("Open Source")];
137 [o_mrl_lbl setTitle: _NS("Media Resource Locator (MRL)")];
138 [o_ckbox_enqueue setTitle: _NS("Only enqueue in playlist, do not play")];
140 [o_btn_ok setTitle: _NS("OK")];
141 [o_btn_cancel setTitle: _NS("Cancel")];
143 [[o_tabview tabViewItemAtIndex: 0] setLabel: _NS("File")];
144 [[o_tabview tabViewItemAtIndex: 1] setLabel: _NS("Disc")];
145 [[o_tabview tabViewItemAtIndex: 2] setLabel: _NS("Network")];
147 [o_file_btn_browse setTitle: _NS("Browse...")];
148 [o_file_stream setTitle: _NS("Treat as a pipe rather than as a file")];
150 [o_disc_device_lbl setStringValue: _NS("Device name")];
151 [o_disc_title_lbl setStringValue: _NS("Title")];
152 [o_disc_chapter_lbl setStringValue: _NS("Chapter")];
153 [o_disc_videots_btn_browse setTitle: _NS("Browse...")];
154 [o_disc_dvd_menus setTitle: _NS("Use DVD menus (EXPERIMENTAL)")];
156 [[o_disc_type cellAtRow:0 column:0] setTitle: _NS("VIDEO_TS folder")];
157 [[o_disc_type cellAtRow:1 column:0] setTitle: _NS("DVD")];
158 [[o_disc_type cellAtRow:2 column:0] setTitle: _NS("VCD")];
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 intf_thread_t * p_intf = [NSApp getIntf];
236 NSString *o_source = [o_mrl stringValue];
237 BOOL b_enq = [o_ckbox_enqueue state] == NSOnState ? YES : NO;
238 NSString *subPath = [o_file_sub_path stringValue];
240 [o_playlist appendArray:
241 [NSArray arrayWithObject: o_source] atPos: -1 enqueue:b_enq];
243 if (([o_file_sub_ckbox state] == NSOnState) && !([subPath isEqualTo: @""]))
245 config_PutPsz( p_intf, "sub-file", strdup( [subPath cString] ) );
246 if ( [o_file_sub_override state] )
248 config_PutInt( p_intf, "sub-delay", (int)( [o_file_sub_delay intValue] * 10 ) );
249 config_PutFloat( p_intf, "sub-fps", [o_file_sub_fps floatValue] );
254 config_PutPsz( p_intf, "sub-file", "" );
255 config_PutInt( p_intf, "sub-delay", 0 );
256 config_PutFloat( p_intf, "sub-fps", 0.0 );
261 - (void)tabView:(NSTabView *)o_tv didSelectTabViewItem:(NSTabViewItem *)o_tvi
263 NSString *o_label = [o_tvi label];
265 if( [o_label isEqualToString: _NS("File")] )
267 [self openFilePathChanged: nil];
269 else if( [o_label isEqualToString: _NS("Disc")] )
271 [self openDiscTypeChanged: nil];
273 else if( [o_label isEqualToString: _NS("Network")] )
275 [self openNetModeChanged: nil];
279 - (IBAction)openFileGeneric:(id)sender
281 [self openFilePathChanged: nil];
282 [self openTarget: 0];
285 - (IBAction)openDisc:(id)sender
287 [self openDiscTypeChanged: nil];
288 [self openTarget: 1];
291 - (IBAction)openNet:(id)sender
293 [self openNetModeChanged: nil];
294 [self openTarget: 2];
297 - (void)openFilePathChanged:(NSNotification *)o_notification
299 NSString *o_mrl_string;
300 NSString *o_filename = [o_file_path stringValue];
301 NSString *o_ext = [o_filename pathExtension];
302 vlc_bool_t b_stream = [o_file_stream state];
305 [[NSFileManager defaultManager] fileExistsAtPath:o_filename isDirectory:&b_dir];
309 o_mrl_string = [NSString stringWithFormat: @"dir:%@", o_filename];
311 else if( [o_ext isEqualToString: @"bin"] ||
312 [o_ext isEqualToString: @"cue"] ||
313 [o_ext isEqualToString: @"vob"] ||
314 [o_ext isEqualToString: @"iso"] )
316 o_mrl_string = o_filename;
320 o_mrl_string = [NSString stringWithFormat: @"%s://%@",
321 b_stream ? "stream" : "file",
324 [o_mrl setStringValue: o_mrl_string];
327 - (IBAction)openFileBrowse:(id)sender
329 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
331 [o_open_panel setAllowsMultipleSelection: NO];
332 [o_open_panel setCanChooseDirectories: YES];
333 [o_open_panel setTitle: _NS("Open File")];
334 [o_open_panel setPrompt: _NS("Open")];
336 [o_open_panel beginSheetForDirectory:nil
339 modalForWindow:[sender window]
341 didEndSelector: @selector(pathChosenInPanel:
347 - (void)pathChosenInPanel: (NSOpenPanel *) sheet withReturn:(int)returnCode contextInfo:(void *)contextInfo
349 if (returnCode == NSFileHandlingPanelOKButton)
351 NSString *o_filename = [[sheet filenames] objectAtIndex: 0];
352 [o_file_path setStringValue: o_filename];
353 [self openFilePathChanged: nil];
357 - (IBAction)openFileStreamChanged:(id)sender
359 [self openFilePathChanged: nil];
362 - (IBAction)openDiscTypeChanged:(id)sender
365 vlc_bool_t b_device, b_menus, b_title_chapter;
367 [o_disc_device removeAllItems];
368 b_title_chapter = ![o_disc_dvd_menus state];
370 o_type = [[o_disc_type selectedCell] title];
372 if ( [o_type isEqualToString: _NS("VIDEO_TS folder")] )
374 b_device = 0; b_menus = 1;
380 const char *psz_class = NULL;
383 if ( [o_type isEqualToString: _NS("VCD")] )
385 psz_class = kIOCDMediaClass;
387 b_menus = 0; b_title_chapter = 1;
388 [o_disc_dvd_menus setState: FALSE];
392 psz_class = kIODVDMediaClass;
397 o_devices = GetEjectableMediaOfClass( psz_class );
398 if ( o_devices != nil )
400 int i_devices = [o_devices count];
406 for( i = 0; i < i_devices; i++ )
409 addItemWithObjectValue: [o_devices objectAtIndex: i]];
412 [o_disc_device selectItemAtIndex: 0];
416 [o_disc_device setStringValue:
417 [NSString stringWithFormat: _NS("No %@s found"), o_disc]];
422 [o_disc_device setEnabled: b_device];
423 [o_disc_title setEnabled: b_title_chapter];
424 [o_disc_title_stp setEnabled: b_title_chapter];
425 [o_disc_chapter setEnabled: b_title_chapter];
426 [o_disc_chapter_stp setEnabled: b_title_chapter];
427 [o_disc_videots_folder setEnabled: !b_device];
428 [o_disc_videots_btn_browse setEnabled: !b_device];
429 [o_disc_dvd_menus setEnabled: b_menus];
431 [self openDiscInfoChanged: nil];
434 - (IBAction)openDiscStepperChanged:(id)sender
436 int i_tag = [sender tag];
440 [o_disc_title setIntValue: [o_disc_title_stp intValue]];
442 else if( i_tag == 1 )
444 [o_disc_chapter setIntValue: [o_disc_chapter_stp intValue]];
447 [self openDiscInfoChanged: nil];
450 - (void)openDiscInfoChanged:(NSNotification *)o_notification
455 NSString *o_mrl_string;
456 int i_title, i_chapter;
459 o_type = [[o_disc_type selectedCell] title];
460 o_device = [o_disc_device stringValue];
461 i_title = [o_disc_title intValue];
462 i_chapter = [o_disc_chapter intValue];
463 o_videots = [o_disc_videots_folder stringValue];
464 b_menus = [o_disc_dvd_menus state];
466 if ( [o_type isEqualToString: _NS("VCD")] )
468 if ( [o_device isEqualToString:
469 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
471 o_mrl_string = [NSString stringWithFormat: @"vcd://%@@%i,%i",
472 o_device, i_title, i_chapter];
474 else if ( [o_type isEqualToString: _NS("DVD")] )
476 if ( [o_device isEqualToString:
477 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
480 o_mrl_string = [NSString stringWithFormat: @"dvdplay://%@",
483 o_mrl_string = [NSString stringWithFormat: @"dvdold://%@@%i,%i",
484 o_device, i_title, i_chapter];
486 else /* VIDEO_TS folder */
489 o_mrl_string = [NSString stringWithFormat: @"dvdplay://%@",
492 o_mrl_string = [NSString stringWithFormat: @"dvdread://%@@%i,%i",
493 o_videots, i_title, i_chapter];
496 [o_mrl setStringValue: o_mrl_string];
499 - (IBAction)openDiscMenusChanged:(id)sender
501 [self openDiscInfoChanged: nil];
502 [self openDiscTypeChanged: nil];
505 - (IBAction)openVTSBrowse:(id)sender
507 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
509 [o_open_panel setAllowsMultipleSelection: NO];
510 [o_open_panel setCanChooseFiles: NO];
511 [o_open_panel setCanChooseDirectories: YES];
512 [o_open_panel setTitle: _NS("Open VIDEO_TS Directory")];
513 [o_open_panel setPrompt: _NS("Open")];
515 if( [o_open_panel runModalForDirectory: nil
516 file: nil types: nil] == NSOKButton )
518 NSString *o_dirname = [[o_open_panel filenames] objectAtIndex: 0];
519 [o_disc_videots_folder setStringValue: o_dirname];
520 [self openDiscInfoChanged: nil];
524 - (IBAction)openNetModeChanged:(id)sender
531 o_mode = [[o_net_mode selectedCell] title];
533 if( [o_mode isEqualToString: _NS("UDP/RTP")] ) b_udp = TRUE;
534 else if( [o_mode isEqualToString: _NS("UDP/RTP Multicast")] ) b_udpm = TRUE;
535 else if( [o_mode isEqualToString: _NS("HTTP/FTP/MMS")] ) b_http = TRUE;
537 [o_net_udp_port setEnabled: b_udp];
538 [o_net_udp_port_stp setEnabled: b_udp];
539 [o_net_udpm_addr setEnabled: b_udpm];
540 [o_net_udpm_port setEnabled: b_udpm];
541 [o_net_udpm_port_stp setEnabled: b_udpm];
542 [o_net_http_url setEnabled: b_http];
544 [self openNetInfoChanged: nil];
547 - (IBAction)openNetStepperChanged:(id)sender
549 int i_tag = [sender tag];
553 [o_net_udp_port setIntValue: [o_net_udp_port_stp intValue]];
555 else if( i_tag == 1 )
557 [o_net_udpm_port setIntValue: [o_net_udpm_port_stp intValue]];
560 [self openNetInfoChanged: nil];
563 - (void)openNetInfoChanged:(NSNotification *)o_notification
566 NSString *o_mrl_string = [NSString string];
567 intf_thread_t * p_intf = [NSApp getIntf];
569 o_mode = [[o_net_mode selectedCell] title];
571 if( [o_mode isEqualToString: _NS("UDP/RTP")] )
573 int i_port = [o_net_udp_port intValue];
575 o_mrl_string = [NSString stringWithString: @"udp://"];
577 if( i_port != config_GetInt( p_intf, "server-port" ) )
580 [o_mrl_string stringByAppendingFormat: @"@:%i", i_port];
583 else if( [o_mode isEqualToString: _NS("UDP/RTP Multicast")] )
585 NSString *o_addr = [o_net_udpm_addr stringValue];
586 int i_port = [o_net_udpm_port intValue];
588 o_mrl_string = [NSString stringWithFormat: @"udp://@%@", o_addr];
590 if( i_port != config_GetInt( p_intf, "server-port" ) )
593 [o_mrl_string stringByAppendingFormat: @":%i", i_port];
596 else if( [o_mode isEqualToString: _NS("HTTP/FTP/MMS")] )
598 NSString *o_url = [o_net_http_url stringValue];
600 if ( ![o_url hasPrefix:@"http:"] && ![o_url hasPrefix:@"ftp:"]
601 && ![o_url hasPrefix:@"mms"] )
602 o_mrl_string = [NSString stringWithFormat: @"http://%@", o_url];
604 o_mrl_string = o_url;
607 [o_mrl setStringValue: o_mrl_string];
610 - (IBAction)openFile:(id)sender
612 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
614 [o_open_panel setAllowsMultipleSelection: YES];
615 [o_open_panel setCanChooseDirectories: YES];
616 [o_open_panel setTitle: _NS("Open File")];
617 [o_open_panel setPrompt: _NS("Open")];
619 if( [o_open_panel runModalForDirectory: nil
620 file: nil types: nil] == NSOKButton )
622 intf_thread_t * p_intf = [NSApp getIntf];
623 config_PutPsz( p_intf, "sout", NULL );
625 NSArray *o_values = [[o_open_panel filenames]
626 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
628 [o_playlist appendArray: o_values atPos: -1 enqueue:NO];
629 config_PutPsz( p_intf, "sub-file", "" );
630 config_PutInt( p_intf, "sub-delay", 0 );
631 config_PutFloat( p_intf, "sub-fps", 0.0 );
632 config_PutPsz( p_intf, "sout", "" );
636 - (IBAction)subsChanged:(id)sender
638 if ([o_file_sub_ckbox state] == NSOnState)
640 [o_file_sub_btn_settings setEnabled:YES];
644 [o_file_sub_btn_settings setEnabled:NO];
648 - (IBAction)subSettings:(id)sender
650 [NSApp beginSheet: o_file_sub_sheet
651 modalForWindow: [sender window]
657 - (IBAction)subFileBrowse:(id)sender
659 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
661 [o_open_panel setAllowsMultipleSelection: NO];
662 [o_open_panel setTitle: _NS("Open File")];
663 [o_open_panel setPrompt: _NS("Open")];
665 if( [o_open_panel runModalForDirectory: nil
666 file: nil types: nil] == NSOKButton )
668 NSString *o_filename = [[o_open_panel filenames] objectAtIndex: 0];
669 [o_file_sub_path setStringValue: o_filename];
673 - (IBAction)subOverride:(id)sender
675 BOOL b_state = [o_file_sub_override state];
676 [o_file_sub_delay setEnabled: b_state];
677 [o_file_sub_delay_stp setEnabled: b_state];
678 [o_file_sub_fps setEnabled: b_state];
679 [o_file_sub_fps_stp setEnabled: b_state];
682 - (IBAction)subDelayStepperChanged:(id)sender
684 [o_file_sub_delay setIntValue: [o_file_sub_delay_stp intValue]];
687 - (IBAction)subFpsStepperChanged:(id)sender;
689 [o_file_sub_fps setFloatValue: [o_file_sub_fps_stp floatValue]];
692 - (IBAction)subCloseSheet:(id)sender
694 [o_file_sub_sheet orderOut:sender];
695 [NSApp endSheet: o_file_sub_sheet];
698 - (IBAction)panelCancel:(id)sender
700 [NSApp stopModalWithCode: 0];
703 - (IBAction)panelOk:(id)sender
705 if( [[o_mrl stringValue] length] )
707 [NSApp stopModalWithCode: 1];