]> git.sesse.net Git - vlc/blob - modules/gui/macosx/open.m
Redesign the Capture Device panel
[vlc] / modules / gui / macosx / open.m
1 /*****************************************************************************
2  * open.m: MacOS X module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Derk-Jan Hartman <thedj@users.sourceforge.net>
10  *          Benjamin Pracht <bigben at videolan dot org>
11  *          Felix Kühne <fkuehne at videolan dot org>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include <stdlib.h>                                      /* malloc(), free() */
32 #include <sys/param.h>                                    /* for MAXPATHLEN */
33 #include <string.h>
34
35 #include <paths.h>
36 #include <IOKit/IOKitLib.h>
37 #include <IOKit/IOBSD.h>
38 #include <IOKit/storage/IOMedia.h>
39 #include <IOKit/storage/IOCDMedia.h>
40 #include <IOKit/storage/IODVDMedia.h>
41
42 #import "intf.h"
43 #import "playlist.h"
44 #import "open.h"
45 #import "output.h"
46 #import "eyetv.h"
47
48 #define setEyeTVUnconnected \
49 [o_capture_lbl setStringValue: _NS("No device connected")]; \
50 [o_capture_long_lbl setStringValue: _NS("VLC could not detect any EyeTV compatible device.\n\nCheck the device's connection, make sure that the latest EyeTV software is installed and try again.")]; \
51 [o_capture_lbl displayIfNeeded]; \
52 [o_capture_long_lbl displayIfNeeded]; \
53 [self showCaptureView: o_capture_label_view]
54
55
56 /*****************************************************************************
57  * GetEjectableMediaOfClass
58  *****************************************************************************/
59 NSArray *GetEjectableMediaOfClass( const char *psz_class )
60 {
61     io_object_t next_media;
62     mach_port_t master_port;
63     kern_return_t kern_result;
64     NSArray *o_devices = nil;
65     NSMutableArray *p_list = nil;
66     io_iterator_t media_iterator;
67     CFMutableDictionaryRef classes_to_match;
68
69     kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
70     if( kern_result != KERN_SUCCESS )
71     {
72         return( nil );
73     }
74  
75     classes_to_match = IOServiceMatching( psz_class );
76     if( classes_to_match == NULL )
77     {
78         return( nil );
79     }
80  
81     CFDictionarySetValue( classes_to_match, CFSTR( kIOMediaEjectableKey ),
82                           kCFBooleanTrue );
83  
84     kern_result = IOServiceGetMatchingServices( master_port, classes_to_match,
85                                                 &media_iterator );
86     if( kern_result != KERN_SUCCESS )
87     {
88         return( nil );
89     }
90
91     p_list = [NSMutableArray arrayWithCapacity: 1];
92  
93     next_media = IOIteratorNext( media_iterator );
94     if( next_media )
95     {
96         char psz_buf[0x32];
97         size_t dev_path_length;
98         CFTypeRef str_bsd_path;
99  
100         do
101         {
102             str_bsd_path = IORegistryEntryCreateCFProperty( next_media,
103                                                             CFSTR( kIOBSDNameKey ),
104                                                             kCFAllocatorDefault,
105                                                             0 );
106             if( str_bsd_path == NULL )
107             {
108                 IOObjectRelease( next_media );
109                 continue;
110             }
111  
112             snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
113             dev_path_length = strlen( psz_buf );
114  
115             if( CFStringGetCString( str_bsd_path,
116                                     (char*)&psz_buf + dev_path_length,
117                                     sizeof(psz_buf) - dev_path_length,
118                                     kCFStringEncodingASCII ) )
119             {
120                 [p_list addObject: [NSString stringWithUTF8String: psz_buf]];
121             }
122  
123             CFRelease( str_bsd_path );
124  
125             IOObjectRelease( next_media );
126  
127         } while( ( next_media = IOIteratorNext( media_iterator ) ) );
128     }
129  
130     IOObjectRelease( media_iterator );
131
132     o_devices = [NSArray arrayWithArray: p_list];
133
134     return( o_devices );
135 }
136
137 /*****************************************************************************
138  * VLCOpen implementation
139  *****************************************************************************/
140 @implementation VLCOpen
141
142 static VLCOpen *_o_sharedMainInstance = nil;
143
144 + (VLCOpen *)sharedInstance
145 {
146     return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
147 }
148
149 - (id)init
150 {
151     if( _o_sharedMainInstance) {
152         [self dealloc];
153     } else {
154         _o_sharedMainInstance = [super init];
155     }
156  
157     return _o_sharedMainInstance;
158 }
159
160 - (void)awakeFromNib
161 {
162     intf_thread_t * p_intf = VLCIntf;
163
164     [o_panel setTitle: _NS("Open Source")];
165     [o_mrl_lbl setTitle: _NS("Media Resource Locator (MRL)")];
166
167     [o_btn_ok setTitle: _NS("OK")];
168     [o_btn_cancel setTitle: _NS("Cancel")];
169
170     [[o_tabview tabViewItemAtIndex: 0] setLabel: _NS("File")];
171     [[o_tabview tabViewItemAtIndex: 1] setLabel: _NS("Disc")];
172     [[o_tabview tabViewItemAtIndex: 2] setLabel: _NS("Network")];
173     [[o_tabview tabViewItemAtIndex: 3] setLabel: _NS("Capture")];
174
175     [o_file_btn_browse setTitle: _NS("Browse...")];
176     [o_file_stream setTitle: _NS("Treat as a pipe rather than as a file")];
177
178     [o_disc_device_lbl setStringValue: _NS("Device name")];
179     [o_disc_title_lbl setStringValue: _NS("Title")];
180     [o_disc_chapter_lbl setStringValue: _NS("Chapter")];
181     [o_disc_videots_btn_browse setTitle: _NS("Browse...")];
182     [o_disc_dvd_menus setTitle: _NS("No DVD menus")];
183
184     [[o_disc_type cellAtRow:0 column:0] setTitle: _NS("VIDEO_TS directory")];
185     [[o_disc_type cellAtRow:1 column:0] setTitle: _NS("DVD")];
186     [[o_disc_type cellAtRow:2 column:0] setTitle: _NS("VCD")];
187     [[o_disc_type cellAtRow:3 column:0] setTitle: _NS("Audio CD")];
188
189     [o_net_udp_port_lbl setStringValue: _NS("Port")];
190     [o_net_udpm_addr_lbl setStringValue: _NS("Address")];
191     [o_net_udpm_port_lbl setStringValue: _NS("Port")];
192     [o_net_http_url_lbl setStringValue: _NS("URL")];
193
194     [[o_net_mode cellAtRow:0 column:0] setTitle: _NS("UDP/RTP")];
195     [[o_net_mode cellAtRow:1 column:0] setTitle: _NS("UDP/RTP Multicast")];
196     [[o_net_mode cellAtRow:2 column:0] setTitle: _NS("HTTP/FTP/MMS/RTSP")];
197     [o_net_timeshift_ckbox setTitle: _NS("Allow timeshifting")];
198
199     [o_net_udp_port setIntValue: config_GetInt( p_intf, "server-port" )];
200     [o_net_udp_port_stp setIntValue: config_GetInt( p_intf, "server-port" )];
201  
202     [o_eyetv_chn_bgbar setUsesThreadedAnimation: YES];
203
204     /* FIXME: implement Capturing l10n here completely */
205     [o_capture_mode_pop removeAllItems];
206     if( MACOS_VERSION > 10.4f )
207         [o_capture_mode_pop addItemWithTitle: _NS("iSight")];
208     [o_capture_mode_pop addItemWithTitle: _NS("Screen")];
209     [o_capture_mode_pop addItemWithTitle: @"EyeTV"];
210     [o_screen_lbl setStringValue: _NS("Screen Capture Input")];
211     [o_screen_long_lbl setStringValue: _NS("This facility allows you to process your screen's output.")];
212     
213     [self setSubPanel];
214
215
216     [[NSNotificationCenter defaultCenter] addObserver: self
217         selector: @selector(openFilePathChanged:)
218         name: NSControlTextDidChangeNotification
219         object: o_file_path];
220
221     [[NSNotificationCenter defaultCenter] addObserver: self
222         selector: @selector(openDiscInfoChanged:)
223         name: NSControlTextDidChangeNotification
224         object: o_disc_device];
225     [[NSNotificationCenter defaultCenter] addObserver: self
226         selector: @selector(openDiscInfoChanged:)
227         name: NSControlTextDidChangeNotification
228         object: o_disc_title];
229     [[NSNotificationCenter defaultCenter] addObserver: self
230         selector: @selector(openDiscInfoChanged:)
231         name: NSControlTextDidChangeNotification
232         object: o_disc_chapter];
233     [[NSNotificationCenter defaultCenter] addObserver: self
234         selector: @selector(openDiscInfoChanged:)
235         name: NSControlTextDidChangeNotification
236         object: o_disc_videots_folder];
237
238     [[NSNotificationCenter defaultCenter] addObserver: self
239         selector: @selector(openNetInfoChanged:)
240         name: NSControlTextDidChangeNotification
241         object: o_net_udp_port];
242     [[NSNotificationCenter defaultCenter] addObserver: self
243         selector: @selector(openNetInfoChanged:)
244         name: NSControlTextDidChangeNotification
245         object: o_net_udpm_addr];
246     [[NSNotificationCenter defaultCenter] addObserver: self
247         selector: @selector(openNetInfoChanged:)
248         name: NSControlTextDidChangeNotification
249         object: o_net_udpm_port];
250     [[NSNotificationCenter defaultCenter] addObserver: self
251         selector: @selector(openNetInfoChanged:)
252         name: NSControlTextDidChangeNotification
253         object: o_net_http_url];
254
255     [[NSNotificationCenter defaultCenter] addObserver: self
256                                              selector: @selector(screenFPSChanged:)
257                                                  name: NSControlTextDidChangeNotification
258                                                object: o_screen_fps_fld];
259
260     [[NSDistributedNotificationCenter defaultCenter] addObserver: self
261                                                         selector: @selector(eyetvChanged:)
262                                                             name: NULL
263                                                           object: @"VLCEyeTVSupport"
264                                               suspensionBehavior: NSNotificationSuspensionBehaviorDeliverImmediately];
265  
266     /* register clicks on text fields */
267     [[NSNotificationCenter defaultCenter] addObserver: self
268                                              selector: @selector(textFieldWasClicked:)
269                                                  name: @"VLCOpenTextFieldWasClicked"
270                                                object: nil];
271 }
272
273 - (void)setSubPanel
274 {
275     intf_thread_t * p_intf = VLCIntf;
276     int i_index;
277     module_config_t * p_item;
278
279     [o_file_sub_ckbox setTitle: _NS("Load subtitles file:")];
280     [o_file_sub_btn_settings setTitle: _NS("Settings...")];
281     [o_file_sub_btn_browse setTitle: _NS("Browse...")];
282     [o_file_sub_override setTitle: _NS("Override parametters")];
283     [o_file_sub_delay_lbl setStringValue: _NS("Delay")];
284     [o_file_sub_delay_stp setEnabled: NO];
285     [o_file_sub_fps_lbl setStringValue: _NS("FPS")];
286     [o_file_sub_fps_stp setEnabled: NO];
287     [o_file_sub_encoding_lbl setStringValue: _NS("Subtitles encoding")];
288     [o_file_sub_encoding_pop removeAllItems];
289     [o_file_sub_size_lbl setStringValue: _NS("Font size")];
290     [o_file_sub_size_pop removeAllItems];
291     [o_file_sub_align_lbl setStringValue: _NS("Subtitles alignment")];
292     [o_file_sub_align_pop removeAllItems];
293     [o_file_sub_ok_btn setStringValue: _NS("OK")];
294     [o_file_sub_font_box setTitle: _NS("Font Properties")];
295     [o_file_sub_file_box setTitle: _NS("Subtitle File")];
296
297     p_item = config_FindConfig( VLC_OBJECT(p_intf), "subsdec-encoding" );
298
299     if( p_item )
300     {
301         for( i_index = 0; p_item->ppsz_list && p_item->ppsz_list[i_index];
302              i_index++ )
303         {
304             [o_file_sub_encoding_pop addItemWithTitle:
305                 [NSString stringWithUTF8String: p_item->ppsz_list[i_index]]];
306         }
307         [o_file_sub_encoding_pop selectItemWithTitle:
308                 [NSString stringWithUTF8String: p_item->value.psz]];
309     }
310
311     p_item = config_FindConfig( VLC_OBJECT(p_intf), "subsdec-align" );
312
313     if ( p_item )
314     {
315         for ( i_index = 0; i_index < p_item->i_list; i_index++ )
316         {
317             [o_file_sub_align_pop addItemWithTitle:
318                 [NSString stringWithUTF8String:
319                 p_item->ppsz_list_text[i_index]]];
320         }
321         [o_file_sub_align_pop selectItemAtIndex: p_item->value.i];
322     }
323
324     p_item = config_FindConfig( VLC_OBJECT(p_intf), "freetype-rel-fontsize" );
325
326     if ( p_item )
327     {
328         for ( i_index = 0; i_index < p_item->i_list; i_index++ )
329         {
330             [o_file_sub_size_pop addItemWithTitle:
331                 [NSString stringWithUTF8String:
332                 p_item->ppsz_list_text[i_index]]];
333             if ( p_item->value.i == p_item->pi_list[i_index] )
334             {
335                 [o_file_sub_size_pop selectItemAtIndex: i_index];
336             }
337         }
338     }
339 }
340
341 - (void)openTarget:(int)i_type
342 {
343     int i_result;
344     intf_thread_t * p_intf = VLCIntf;
345
346     b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
347
348     [o_tabview selectTabViewItemAtIndex: i_type];
349     [o_file_sub_ckbox setState: NSOffState];
350  
351     i_result = [NSApp runModalForWindow: o_panel];
352     [o_panel close];
353
354     if( i_result )
355     {
356         NSMutableDictionary *o_dic;
357         NSMutableArray *o_options = [NSMutableArray array];
358         unsigned int i;
359
360         o_dic = [NSMutableDictionary dictionaryWithObject: [o_mrl stringValue] forKey: @"ITEM_URL"];
361         if( [o_file_sub_ckbox state] == NSOnState )
362         {
363             module_config_t * p_item;
364
365             [o_options addObject: [NSString stringWithFormat: @"sub-file=%@", [o_file_sub_path stringValue]]];
366             if( [o_file_sub_override state] == NSOnState )
367             {
368                 [o_options addObject: [NSString stringWithFormat: @"sub-delay=%i", (int)( [o_file_sub_delay intValue] * 10 )]];
369                 [o_options addObject: [NSString stringWithFormat: @"sub-fps=%f", [o_file_sub_fps floatValue]]];
370             }
371             [o_options addObject: [NSString stringWithFormat:
372                     @"subsdec-encoding=%@",
373                     [o_file_sub_encoding_pop titleOfSelectedItem]]];
374             [o_options addObject: [NSString stringWithFormat:
375                     @"subsdec-align=%i",
376                     [o_file_sub_align_pop indexOfSelectedItem]]];
377
378             p_item = config_FindConfig( VLC_OBJECT(p_intf),
379                                             "freetype-rel-fontsize" );
380
381             if ( p_item )
382             {
383                 [o_options addObject: [NSString stringWithFormat:
384                     @"freetype-rel-fontsize=%i",
385                     p_item->pi_list[[o_file_sub_size_pop indexOfSelectedItem]]]];
386             }
387         }
388         if( [o_output_ckbox state] == NSOnState )
389         {
390             for (i = 0 ; i < [[o_sout_options getMRL] count] ; i++)
391             {
392                 [o_options addObject: [NSString stringWithString:
393                       [[(VLCOutput *)o_sout_options getMRL] objectAtIndex: i]]];
394             }
395         }
396         if( [o_net_timeshift_ckbox state] == NSOnState )
397         {
398             [o_options addObject: [NSString stringWithString:
399                                                 @"access-filter=timeshift"]];
400         }
401         [o_dic setObject: (NSArray *)[o_options copy] forKey: @"ITEM_OPTIONS"];
402         if( b_autoplay )
403             [o_playlist appendArray: [NSArray arrayWithObject: o_dic] atPos: -1 enqueue:NO];
404         else
405             [o_playlist appendArray: [NSArray arrayWithObject: o_dic] atPos: -1 enqueue:YES];
406     }
407 }
408
409 - (void)tabView:(NSTabView *)o_tv didSelectTabViewItem:(NSTabViewItem *)o_tvi
410 {
411     NSString *o_label = [o_tvi label];
412
413     if( [o_label isEqualToString: _NS("File")] )
414     {
415         [self openFilePathChanged: nil];
416     }
417     else if( [o_label isEqualToString: _NS("Disc")] )
418     {
419         [self openDiscTypeChanged: nil];
420     }
421     else if( [o_label isEqualToString: _NS("Network")] )
422     {
423         [self openNetInfoChanged: nil];
424     }
425     else if( [o_label isEqualToString: _NS("Capture")] )
426     {
427         [self openCaptureModeChanged: nil];
428     }
429 }
430
431 - (void)openFileGeneric
432 {
433     [self openFilePathChanged: nil];
434     [self openTarget: 0];
435 }
436
437 - (void)openDisc
438 {
439     [self openDiscTypeChanged: nil];
440     [self openTarget: 1];
441 }
442
443 - (void)openNet
444 {
445     [self openNetInfoChanged: nil];
446     [self openTarget: 2];
447 }
448
449 - (void)openCapture
450 {
451     [self openCaptureModeChanged: nil];
452     [self showCaptureView: o_capture_label_view];
453     [self openTarget: 3];
454 }
455
456 - (void)openFilePathChanged:(NSNotification *)o_notification
457 {
458     NSString *o_mrl_string;
459     NSString *o_filename = [o_file_path stringValue];
460     NSString *o_ext = [o_filename pathExtension];
461     bool b_stream = [o_file_stream state];
462     BOOL b_dir = NO;
463  
464     [[NSFileManager defaultManager] fileExistsAtPath:o_filename isDirectory:&b_dir];
465
466     if( b_dir )
467     {
468         o_mrl_string = [NSString stringWithFormat: @"dir:%@", o_filename];
469     }
470     else if( [o_ext isEqualToString: @"bin"] ||
471         [o_ext isEqualToString: @"cue"] ||
472         [o_ext isEqualToString: @"vob"] ||
473         [o_ext isEqualToString: @"iso"] )
474     {
475         o_mrl_string = o_filename;
476     }
477     else
478     {
479         o_mrl_string = [NSString stringWithFormat: @"%s://%@",
480                         b_stream ? "stream" : "file",
481                         o_filename];
482     }
483     [o_mrl setStringValue: o_mrl_string];
484 }
485
486 - (IBAction)openFileBrowse:(id)sender
487 {
488     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
489  
490     [o_open_panel setAllowsMultipleSelection: NO];
491     [o_open_panel setCanChooseDirectories: YES];
492     [o_open_panel setTitle: _NS("Open File")];
493     [o_open_panel setPrompt: _NS("Open")];
494
495     [o_open_panel beginSheetForDirectory:nil
496         file:nil
497         types:nil
498         modalForWindow:[sender window]
499         modalDelegate: self
500         didEndSelector: @selector(pathChosenInPanel:
501                         withReturn:
502                         contextInfo:)
503         contextInfo: nil];
504 }
505
506 - (void)pathChosenInPanel: (NSOpenPanel *) sheet withReturn:(int)returnCode contextInfo:(void  *)contextInfo
507 {
508     if (returnCode == NSFileHandlingPanelOKButton)
509     {
510         NSString *o_filename = [[sheet filenames] objectAtIndex: 0];
511         [o_file_path setStringValue: o_filename];
512         [self openFilePathChanged: nil];
513     }
514 }
515
516 - (IBAction)openFileStreamChanged:(id)sender
517 {
518     [self openFilePathChanged: nil];
519 }
520
521 - (IBAction)openDiscTypeChanged:(id)sender
522 {
523     NSString *o_type;
524     BOOL b_device, b_no_menus, b_title_chapter;
525  
526     [o_disc_device removeAllItems];
527     b_title_chapter = ![o_disc_dvd_menus state];
528  
529     o_type = [[o_disc_type selectedCell] title];
530
531     if ( [o_type isEqualToString: _NS("VIDEO_TS directory")] )
532     {
533         b_device = NO; b_no_menus = YES;
534     }
535     else
536     {
537         NSArray *o_devices;
538         NSString *o_disc;
539         const char *psz_class = NULL;
540         b_device = YES;
541
542         if ( [o_type isEqualToString: _NS("VCD")] )
543         {
544             psz_class = kIOCDMediaClass;
545             o_disc = o_type;
546             b_no_menus = NO; b_title_chapter = YES;
547                 }
548         else if ( [o_type isEqualToString: _NS("Audio CD")])
549         {
550             psz_class = kIOCDMediaClass;
551             o_disc = o_type;
552             b_no_menus = NO; b_title_chapter = NO;
553         }
554         else
555         {
556             psz_class = kIODVDMediaClass;
557             o_disc = o_type;
558             b_no_menus = YES;
559         }
560  
561         o_devices = GetEjectableMediaOfClass( psz_class );
562         if ( o_devices != nil )
563         {
564             int i_devices = [o_devices count];
565  
566             if ( i_devices )
567             {
568                                 for( int i = 0; i < i_devices; i++ )
569                 {
570                     [o_disc_device
571                         addItemWithObjectValue: [o_devices objectAtIndex: i]];
572                 }
573
574                 [o_disc_device selectItemAtIndex: 0];
575             }
576             else
577             {
578                 [o_disc_device setStringValue:
579                     [NSString stringWithFormat: _NS("No %@s found"), o_disc]];
580             }
581         }
582     }
583
584     [o_disc_device setEnabled: b_device];
585     [o_disc_title setEnabled: b_title_chapter];
586     [o_disc_title_stp setEnabled: b_title_chapter];
587     [o_disc_chapter setEnabled: b_title_chapter];
588     [o_disc_chapter_stp setEnabled: b_title_chapter];
589     [o_disc_videots_folder setEnabled: !b_device];
590     [o_disc_videots_btn_browse setEnabled: !b_device];
591     [o_disc_dvd_menus setEnabled: b_no_menus];
592
593     [self openDiscInfoChanged: nil];
594 }
595
596 - (IBAction)openDiscStepperChanged:(id)sender
597 {
598     int i_tag = [sender tag];
599
600     if( i_tag == 0 )
601     {
602         [o_disc_title setIntValue: [o_disc_title_stp intValue]];
603     }
604     else if( i_tag == 1 )
605     {
606         [o_disc_chapter setIntValue: [o_disc_chapter_stp intValue]];
607     }
608
609     [self openDiscInfoChanged: nil];
610 }
611
612 - (void)openDiscInfoChanged:(NSNotification *)o_notification
613 {
614     NSString *o_type;
615     NSString *o_device;
616     NSString *o_videots;
617     NSString *o_mrl_string;
618     int i_title, i_chapter;
619     BOOL b_no_menus;
620
621     o_type = [[o_disc_type selectedCell] title];
622     o_device = [o_disc_device stringValue];
623     i_title = [o_disc_title intValue];
624     i_chapter = [o_disc_chapter intValue];
625     o_videots = [o_disc_videots_folder stringValue];
626     b_no_menus = [o_disc_dvd_menus state];
627
628     if ( [o_type isEqualToString: _NS("VCD")] )
629     {
630         if ( [o_device isEqualToString:
631                 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
632             o_device = @"";
633         o_mrl_string = [NSString stringWithFormat: @"vcd://%@@%i:%i",
634                         o_device, i_title, i_chapter];
635     }
636     else if ( [o_type isEqualToString: _NS("Audio CD")] )
637     {
638         if ( [o_device isEqualToString:
639                 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
640             o_device = @"";
641         o_mrl_string = [NSString stringWithFormat: @"cdda://%@",
642                         o_device];
643     }
644     else if ( [o_type isEqualToString: _NS("DVD")] )
645     {
646         if ( [o_device isEqualToString:
647                 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
648             o_device = @"";
649         if ( b_no_menus )
650             o_mrl_string = [NSString stringWithFormat: @"dvdread://%@@%i:%i-",
651                             o_device, i_title, i_chapter];
652         else
653                         o_mrl_string = [NSString stringWithFormat: @"dvdnav://%@",
654                             o_device];
655             
656     }
657     else /* VIDEO_TS folder */
658     {
659         if ( b_no_menus )
660             o_mrl_string = [NSString stringWithFormat: @"dvdread://%@@%i:%i",
661                             o_videots, i_title, i_chapter];
662         else
663                         o_mrl_string = [NSString stringWithFormat: @"dvdnav://%@",
664                             o_videots];            
665     }
666
667     [o_mrl setStringValue: o_mrl_string];
668 }
669
670 - (IBAction)openDiscMenusChanged:(id)sender
671 {
672     [self openDiscInfoChanged: nil];
673     [self openDiscTypeChanged: nil];
674 }
675
676 - (IBAction)openVTSBrowse:(id)sender
677 {
678     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
679
680     [o_open_panel setAllowsMultipleSelection: NO];
681     [o_open_panel setCanChooseFiles: NO];
682     [o_open_panel setCanChooseDirectories: YES];
683     [o_open_panel setTitle: _NS("Open VIDEO_TS Directory")];
684     [o_open_panel setPrompt: _NS("Open")];
685
686     if( [o_open_panel runModalForDirectory: nil
687             file: nil types: nil] == NSOKButton )
688     {
689         NSString *o_dirname = [[o_open_panel filenames] objectAtIndex: 0];
690         [o_disc_videots_folder setStringValue: o_dirname];
691         [self openDiscInfoChanged: nil];
692     }
693 }
694
695 - (void)textFieldWasClicked:(NSNotification *)o_notification
696 {
697     if( [o_notification object] == o_net_udp_port )
698         [o_net_mode selectCellAtRow: 0 column: 0];
699     else if( [o_notification object] == o_net_udpm_addr ||
700              [o_notification object] == o_net_udpm_port )
701         [o_net_mode selectCellAtRow: 1 column: 0];
702     else
703         [o_net_mode selectCellAtRow: 2 column: 0];
704
705     [self openNetInfoChanged: nil];
706 }
707
708 - (IBAction)openNetModeChanged:(id)sender
709 {
710     if( [[sender selectedCell] tag] == 0 )
711         [o_panel makeFirstResponder: o_net_udp_port];
712     else if ( [[sender selectedCell] tag] == 1 )
713         [o_panel makeFirstResponder: o_net_udpm_addr];
714     else
715         [o_panel makeFirstResponder: o_net_http_url];
716
717     [self openNetInfoChanged: nil];
718 }
719
720 - (IBAction)openNetStepperChanged:(id)sender
721 {
722     int i_tag = [sender tag];
723
724     if( i_tag == 0 )
725     {
726         [o_net_udp_port setIntValue: [o_net_udp_port_stp intValue]];
727         [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCOpenTextFieldWasClicked"
728                                                             object: o_net_udp_port];
729         [o_panel makeFirstResponder: o_net_udp_port];
730     }
731     else if( i_tag == 1 )
732     {
733         [o_net_udpm_port setIntValue: [o_net_udpm_port_stp intValue]];
734         [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCOpenTextFieldWasClicked"
735                                                             object: o_net_udpm_port];
736         [o_panel makeFirstResponder: o_net_udpm_port];
737     }
738
739     [self openNetInfoChanged: nil];
740 }
741
742 - (void)openNetInfoChanged:(NSNotification *)o_notification
743 {
744     NSString *o_mode;
745     NSString *o_mrl_string = [NSString string];
746     intf_thread_t * p_intf = VLCIntf;
747
748     o_mode = [[o_net_mode selectedCell] title];
749
750     if( [o_mode isEqualToString: _NS("UDP/RTP")] )
751     {
752         int i_port = [o_net_udp_port intValue];
753
754         o_mrl_string = [NSString stringWithString: @"udp://"];
755
756         if( i_port != config_GetInt( p_intf, "server-port" ) )
757         {
758             o_mrl_string =
759                 [o_mrl_string stringByAppendingFormat: @"@:%i", i_port];
760         }
761     }
762     else if( [o_mode isEqualToString: _NS("UDP/RTP Multicast")] )
763     {
764         NSString *o_addr = [o_net_udpm_addr stringValue];
765         int i_port = [o_net_udpm_port intValue];
766
767         o_mrl_string = [NSString stringWithFormat: @"udp://@%@", o_addr];
768
769         if( i_port != config_GetInt( p_intf, "server-port" ) )
770         {
771             o_mrl_string =
772                 [o_mrl_string stringByAppendingFormat: @":%i", i_port];
773         }
774     }
775     else if( [o_mode isEqualToString: _NS("HTTP/FTP/MMS/RTSP")] )
776     {
777         NSString *o_url = [o_net_http_url stringValue];
778
779         if ( ![o_url hasPrefix:@"http:"] && ![o_url hasPrefix:@"ftp:"]
780               && ![o_url hasPrefix:@"mms"] && ![o_url hasPrefix:@"rtsp"] )
781             o_mrl_string = [NSString stringWithFormat: @"http://%@", o_url];
782         else
783             o_mrl_string = o_url;
784     }
785     [o_mrl setStringValue: o_mrl_string];
786 }
787
788 - (void)openFile
789 {
790     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
791     int i;
792     b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
793  
794     [o_open_panel setAllowsMultipleSelection: YES];
795     [o_open_panel setCanChooseDirectories: YES];
796     [o_open_panel setTitle: _NS("Open File")];
797     [o_open_panel setPrompt: _NS("Open")];
798  
799     if( [o_open_panel runModalForDirectory: nil
800             file: nil types: nil] == NSOKButton )
801     {
802         NSArray *o_array = [NSArray array];
803         NSArray *o_values = [[o_open_panel filenames]
804                 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
805
806         for( i = 0; i < (int)[o_values count]; i++)
807         {
808             NSDictionary *o_dic;
809             o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
810             o_array = [o_array arrayByAddingObject: o_dic];
811         }
812         if( b_autoplay )
813             [o_playlist appendArray: o_array atPos: -1 enqueue:NO];
814         else
815             [o_playlist appendArray: o_array atPos: -1 enqueue:YES];
816     }
817 }
818
819 - (void)showCaptureView: theView
820 {
821     NSRect o_view_rect;
822     o_view_rect = [theView frame];
823     if( o_currentCaptureView )
824     {
825         [o_currentCaptureView removeFromSuperviewWithoutNeedingDisplay];
826         [o_currentCaptureView release];
827     }
828     [theView setFrame: NSMakeRect( 0, 10, o_view_rect.size.width, o_view_rect.size.height)];
829     [theView setNeedsDisplay: YES];
830     [theView setAutoresizesSubviews: YES];
831     [[[o_tabview tabViewItemAtIndex: 3] view] addSubview: theView];
832     [theView displayIfNeeded];
833     o_currentCaptureView = theView;
834     [o_currentCaptureView retain];
835 }
836
837 - (IBAction)openCaptureModeChanged:(id)sender
838 {
839     if( [[[o_capture_mode_pop selectedItem] title] isEqualToString: @"EyeTV"] )
840     {
841         if( [[[VLCMain sharedInstance] getEyeTVController] isEyeTVrunning] == YES )
842         {
843             if( [[[VLCMain sharedInstance] getEyeTVController] isDeviceConnected] == YES )
844             {
845                 [self showCaptureView: o_eyetv_running_view];
846                 [self setupChannelInfo];
847             }
848             else
849             {
850                 setEyeTVUnconnected;
851             }
852         }
853         else
854             [self showCaptureView: o_eyetv_notLaunched_view];
855         [o_mrl setStringValue: @""];
856     } 
857     else if( [[[o_capture_mode_pop selectedItem] title] isEqualToString: @"Screen"] )
858     {
859         [self showCaptureView: o_screen_view];
860         [o_mrl setStringValue: [NSString stringWithFormat:@"screen:// :screen-fps=%@", [o_screen_fps_fld stringValue]]];
861     }
862     else if( [[[o_capture_mode_pop selectedItem] title] isEqualToString: @"iSight"] )
863     {
864         [o_capture_lbl setStringValue: _NS("iSight Capture Input")];
865         [o_capture_long_lbl setStringValue: _NS("This facility allows you to process your iSight's input signal.\n\nNo settings are available in this version, so you will be provided a 640px*480px raw video stream.\n\nLive Audio input is not supported.")];
866         [o_capture_lbl displayIfNeeded];
867         [o_capture_long_lbl displayIfNeeded];
868         
869         [self showCaptureView: o_capture_label_view];
870         [o_mrl setStringValue: @"qtcapture://"];
871     }
872 }
873
874 - (IBAction)openCaptureStepperChanged:(id)sender
875 {
876     [o_screen_fps_fld setIntValue: [o_screen_fps_stp intValue]];
877     [o_panel makeFirstResponder: o_screen_fps_fld];
878     [o_mrl setStringValue: [NSString stringWithFormat:@"screen:// :screen-fps=%@", [o_screen_fps_fld stringValue]]];
879 }
880
881 - (IBAction)eyetvSwitchChannel:(id)sender
882 {
883     if( sender == o_eyetv_nextProgram_btn )
884     {
885         int chanNum = [[[VLCMain sharedInstance] getEyeTVController] switchChannelUp: YES];
886         [o_eyetv_channels_pop selectItemWithTag:chanNum];
887         [o_mrl setStringValue: [NSString stringWithFormat:@"eyetv:// :eyetv-channel=%d", chanNum]];
888     }
889     else if( sender == o_eyetv_previousProgram_btn )
890     {
891         int chanNum = [[[VLCMain sharedInstance] getEyeTVController] switchChannelUp: NO];
892         [o_eyetv_channels_pop selectItemWithTag:chanNum];
893         [o_mrl setStringValue: [NSString stringWithFormat:@"eyetv:// :eyetv-channel=%d", chanNum]];
894     }
895     else if( sender == o_eyetv_channels_pop )
896     {
897         int chanNum = [[sender selectedItem] tag];
898         [[[VLCMain sharedInstance] getEyeTVController] selectChannel:chanNum];
899         [o_mrl setStringValue: [NSString stringWithFormat:@"eyetv:// :eyetv-channel=%d", chanNum]];
900     }
901     else
902         msg_Err( VLCIntf, "eyetvSwitchChannel sent by unknown object" );
903 }
904
905 - (IBAction)eyetvLaunch:(id)sender
906 {
907     [[[VLCMain sharedInstance] getEyeTVController] launchEyeTV];
908 }
909
910 - (void)eyetvChanged:(NSNotification *)o_notification
911 {
912     if( [[o_notification name] isEqualToString: @"DeviceAdded"] )
913     {
914         msg_Dbg( VLCIntf, "eyetv device was added" );
915         [self showCaptureView: o_eyetv_running_view];
916         [self setupChannelInfo];
917     }
918     else if( [[o_notification name] isEqualToString: @"DeviceRemoved"] )
919     {
920         /* leave the channel selection like that,
921          * switch to our "no device" tab */
922         msg_Dbg( VLCIntf, "eyetv device was removed" );
923         setEyeTVUnconnected;
924     }
925     else if( [[o_notification name] isEqualToString: @"PluginQuit"] )
926     {
927         /* switch to the "launch eyetv" tab */
928         msg_Dbg( VLCIntf, "eyetv was terminated" );
929         [self showCaptureView: o_eyetv_notLaunched_view];
930     }
931     else if( [[o_notification name] isEqualToString: @"PluginInit"] )
932     {
933         /* we got no device yet */
934         msg_Dbg( VLCIntf, "eyetv was launched, no device yet" );
935         setEyeTVUnconnected;
936     }
937     else
938         msg_Warn( VLCIntf, "unknown external notify '%s' received", [[o_notification name] UTF8String] );
939 }
940
941 /* little helper method, since this code needs to be run by multiple objects */
942 - (void)setupChannelInfo
943 {
944     /* set up channel selection */
945     [o_eyetv_channels_pop removeAllItems];
946     [o_eyetv_chn_bgbar setHidden: NO];
947     [o_eyetv_chn_bgbar animate: self];
948     [o_eyetv_chn_status_txt setStringValue: _NS("Retrieving Channel Info...")];
949     [o_eyetv_chn_status_txt setHidden: NO];
950  
951     /* retrieve info */
952     NSEnumerator *channels = [[[VLCMain sharedInstance] getEyeTVController] allChannels];
953     int x = -2;
954     [[[o_eyetv_channels_pop menu] addItemWithTitle: _NS("Composite input")
955                                                action: nil
956                                         keyEquivalent: @""] setTag:x++];
957     [[[o_eyetv_channels_pop menu] addItemWithTitle: _NS("S-Video input")
958                                                action: nil
959                                         keyEquivalent: @""] setTag:x++];
960     if( channels ) 
961     {
962         NSString *channel;
963         [[o_eyetv_channels_pop menu] addItem: [NSMenuItem separatorItem]];
964         while( channel = [channels nextObject] )
965         {
966             /* we have to add items this way, because we accept duplicates
967              * additionally, we save a bit of time */
968             [[[o_eyetv_channels_pop menu] addItemWithTitle: channel
969                                                    action: nil
970                                             keyEquivalent: @""] setTag:++x];
971         }
972         /* make Tuner the default */
973         [o_eyetv_channels_pop selectItemWithTag:[[[VLCMain sharedInstance] getEyeTVController] currentChannel]];
974     }
975  
976     /* clean up GUI */
977     [o_eyetv_chn_bgbar setHidden: YES];
978     [o_eyetv_chn_status_txt setHidden: YES];
979 }
980
981 - (IBAction)subsChanged:(id)sender
982 {
983     if ([o_file_sub_ckbox state] == NSOnState)
984     {
985         [o_file_sub_btn_settings setEnabled:YES];
986     }
987     else
988     {
989         [o_file_sub_btn_settings setEnabled:NO];
990     }
991 }
992
993 - (IBAction)subSettings:(id)sender
994 {
995     [NSApp beginSheet: o_file_sub_sheet
996         modalForWindow: [sender window]
997         modalDelegate: self
998         didEndSelector: NULL
999         contextInfo: nil];
1000 }
1001
1002 - (IBAction)subFileBrowse:(id)sender
1003 {
1004     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
1005  
1006     [o_open_panel setAllowsMultipleSelection: NO];
1007     [o_open_panel setTitle: _NS("Open File")];
1008     [o_open_panel setPrompt: _NS("Open")];
1009
1010     if( [o_open_panel runModalForDirectory: nil
1011             file: nil types: nil] == NSOKButton )
1012     {
1013         NSString *o_filename = [[o_open_panel filenames] objectAtIndex: 0];
1014         [o_file_sub_path setStringValue: o_filename];
1015     }
1016 }
1017
1018 - (IBAction)subOverride:(id)sender
1019 {
1020     BOOL b_state = [o_file_sub_override state];
1021     [o_file_sub_delay setEnabled: b_state];
1022     [o_file_sub_delay_stp setEnabled: b_state];
1023     [o_file_sub_fps setEnabled: b_state];
1024     [o_file_sub_fps_stp setEnabled: b_state];
1025 }
1026
1027 - (IBAction)subDelayStepperChanged:(id)sender
1028 {
1029     [o_file_sub_delay setIntValue: [o_file_sub_delay_stp intValue]];
1030 }
1031
1032 - (IBAction)subFpsStepperChanged:(id)sender;
1033 {
1034     [o_file_sub_fps setFloatValue: [o_file_sub_fps_stp floatValue]];
1035 }
1036
1037 - (IBAction)subCloseSheet:(id)sender
1038 {
1039     [o_file_sub_sheet orderOut:sender];
1040     [NSApp endSheet: o_file_sub_sheet];
1041 }
1042
1043 - (IBAction)panelCancel:(id)sender
1044 {
1045     [NSApp stopModalWithCode: 0];
1046 }
1047
1048 - (IBAction)panelOk:(id)sender
1049 {
1050     if( [[o_mrl stringValue] length] )
1051     {
1052         [NSApp stopModalWithCode: 1];
1053     }
1054     else
1055     {
1056         NSBeep();
1057     }
1058 }
1059
1060 @end
1061
1062 @implementation VLCOpenTextField
1063
1064 - (void)mouseDown:(NSEvent *)theEvent
1065 {
1066     [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCOpenTextFieldWasClicked"
1067                                                         object: self];
1068     [super mouseDown: theEvent];
1069 }
1070
1071 @end