]> git.sesse.net Git - vlc/blob - modules/gui/macosx/open.m
174c0b4b8c3e64ac33f7345f74d2c01625faf4bd
[vlc] / modules / gui / macosx / open.m
1 /*****************************************************************************
2  * open.m: Open dialogues for VLC's MacOS X port
3  *****************************************************************************
4  * Copyright (C) 2002-2009 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 Paul 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         p_intf = VLCIntf;
156     }
157  
158     return _o_sharedMainInstance;
159 }
160
161 - (void)awakeFromNib
162 {
163     [o_panel setTitle: _NS("Open Source")];
164     [o_mrl_lbl setTitle: _NS("Media Resource Locator (MRL)")];
165
166     [o_btn_ok setTitle: _NS("Open")];
167     [o_btn_cancel setTitle: _NS("Cancel")];
168
169     [[o_tabview tabViewItemAtIndex: 0] setLabel: _NS("File")];
170     [[o_tabview tabViewItemAtIndex: 1] setLabel: _NS("Disc")];
171     [[o_tabview tabViewItemAtIndex: 2] setLabel: _NS("Network")];
172     [[o_tabview tabViewItemAtIndex: 3] setLabel: _NS("Capture")];
173
174     [o_file_btn_browse setTitle: _NS("Browse...")];
175     [o_file_stream setTitle: _NS("Treat as a pipe rather than as a file")];
176
177     [o_disc_device_lbl setStringValue: _NS("Device name")];
178     [o_disc_title_lbl setStringValue: _NS("Title")];
179     [o_disc_chapter_lbl setStringValue: _NS("Chapter")];
180     [o_disc_videots_btn_browse setTitle: _NS("Browse...")];
181     [o_disc_dvd_menus setTitle: _NS("No DVD menus")];
182
183     [[o_disc_type cellAtRow:0 column:0] setTitle: _NS("VIDEO_TS folder")];
184     [[o_disc_type cellAtRow:1 column:0] setTitle: _NS("DVD")];
185     [[o_disc_type cellAtRow:2 column:0] setTitle: _NS("VCD")];
186     [[o_disc_type cellAtRow:3 column:0] setTitle: _NS("Audio CD")];
187
188     [o_net_udp_port_lbl setStringValue: _NS("Port")];
189     [o_net_udpm_addr_lbl setStringValue: _NS("IP Address")];
190     [o_net_udpm_port_lbl setStringValue: _NS("Port")];
191     [o_net_http_url_lbl setStringValue: _NS("URL")];
192     [o_net_help_lbl setStringValue: _NS("To Open a usual network stream (HTTP, RTSP, MMS, FTP, etc.), just enter the URL in the field above. If you want to open a RTP or UDP stream, press the button below.")];
193     [o_net_help_udp_lbl setStringValue: _NS("If you want to open a multicast stream, enter the respective IP address given by the stream provider. In unicast mode, VLC use your machine's IP automatically.\n\nTo open a stream using a different protocol, just press Cancel to close this sheet.")];
194     [o_net_udp_cancel_btn setTitle: _NS("Cancel")];
195     [o_net_udp_ok_btn setTitle: _NS("Open")];
196     [o_net_openUDP_btn setTitle: _NS("Open RTP/UDP Stream")];
197
198     [[o_net_mode cellAtRow:0 column:0] setTitle: _NS("UDP/RTP")];
199     [[o_net_mode cellAtRow:1 column:0] setTitle: _NS("UDP/RTP Multicast")];
200
201     [o_net_udp_port setIntValue: config_GetInt( p_intf, "server-port" )];
202     [o_net_udp_port_stp setIntValue: config_GetInt( p_intf, "server-port" )];
203
204     [o_eyetv_chn_bgbar setUsesThreadedAnimation: YES];
205
206     [o_capture_mode_pop removeAllItems];
207     [o_capture_mode_pop addItemWithTitle: @"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     [o_screen_fps_lbl setStringValue: _NS("Frames per Second:")];
213     [o_screen_left_lbl setStringValue: _NS("Subscreen left:")];
214     [o_screen_top_lbl setStringValue: _NS("Subscreen top:")];
215     [o_screen_width_lbl setStringValue: _NS("Subscreen width:")];
216     [o_screen_height_lbl setStringValue: _NS("Subscreen height:")];
217     [o_eyetv_currentChannel_lbl setStringValue: _NS("Current channel:")];
218     [o_eyetv_previousProgram_btn setTitle: _NS("Previous Channel")];
219     [o_eyetv_nextProgram_btn setTitle: _NS("Next Channel")];
220     [o_eyetv_chn_status_txt setStringValue: _NS("Retrieving Channel Info...")];
221     [o_eyetv_noInstance_lbl setStringValue: _NS("EyeTV is not launched")];
222     [o_eyetv_noInstanceLong_lbl setStringValue: _NS("VLC could not connect to EyeTV.\nMake sure that you installed VLC's EyeTV plugin.")];
223     [o_eyetv_launchEyeTV_btn setTitle: _NS("Launch EyeTV now")];
224     [o_eyetv_getPlugin_btn setTitle: _NS("Download Plugin")];
225
226     [self setSubPanel];
227
228     [[NSNotificationCenter defaultCenter] addObserver: self
229         selector: @selector(openFilePathChanged:)
230         name: NSControlTextDidChangeNotification
231         object: o_file_path];
232
233     [[NSNotificationCenter defaultCenter] addObserver: self
234         selector: @selector(openDiscInfoChanged:)
235         name: NSControlTextDidChangeNotification
236         object: o_disc_device];
237     [[NSNotificationCenter defaultCenter] addObserver: self
238         selector: @selector(openDiscInfoChanged:)
239         name: NSControlTextDidChangeNotification
240         object: o_disc_title];
241     [[NSNotificationCenter defaultCenter] addObserver: self
242         selector: @selector(openDiscInfoChanged:)
243         name: NSControlTextDidChangeNotification
244         object: o_disc_chapter];
245     [[NSNotificationCenter defaultCenter] addObserver: self
246         selector: @selector(openDiscInfoChanged:)
247         name: NSControlTextDidChangeNotification
248         object: o_disc_videots_folder];
249
250     [[NSNotificationCenter defaultCenter] addObserver: self
251         selector: @selector(openNetInfoChanged:)
252         name: NSControlTextDidChangeNotification
253         object: o_net_udp_port];
254     [[NSNotificationCenter defaultCenter] addObserver: self
255         selector: @selector(openNetInfoChanged:)
256         name: NSControlTextDidChangeNotification
257         object: o_net_udpm_addr];
258     [[NSNotificationCenter defaultCenter] addObserver: self
259         selector: @selector(openNetInfoChanged:)
260         name: NSControlTextDidChangeNotification
261         object: o_net_udpm_port];
262     [[NSNotificationCenter defaultCenter] addObserver: self
263         selector: @selector(openNetInfoChanged:)
264         name: NSControlTextDidChangeNotification
265         object: o_net_http_url];
266
267     [[NSDistributedNotificationCenter defaultCenter] addObserver: self
268                                                         selector: @selector(eyetvChanged:)
269                                                             name: NULL
270                                                           object: @"VLCEyeTVSupport"
271                                               suspensionBehavior: NSNotificationSuspensionBehaviorDeliverImmediately];
272
273     [[NSNotificationCenter defaultCenter] addObserver: self
274                                              selector: @selector(screenFPSfieldChanged:)
275                                                  name: NSControlTextDidChangeNotification
276                                                object: o_screen_fps_fld];
277
278     /* register clicks on text fields */
279     [[NSNotificationCenter defaultCenter] addObserver: self
280                                              selector: @selector(textFieldWasClicked:)
281                                                  name: @"VLCOpenTextFieldWasClicked"
282                                                object: nil];
283 }
284
285 - (void)setSubPanel
286 {
287     int i_index;
288     module_config_t * p_item;
289
290     [o_file_sub_ckbox setTitle: _NS("Load subtitles file:")];
291     [o_file_sub_btn_settings setTitle: _NS("Settings...")];
292     [o_file_sub_btn_browse setTitle: _NS("Browse...")];
293     [o_file_sub_override setTitle: _NS("Override parametters")];
294     [o_file_sub_delay_lbl setStringValue: _NS("Delay")];
295     [o_file_sub_delay_stp setEnabled: NO];
296     [o_file_sub_fps_lbl setStringValue: _NS("FPS")];
297     [o_file_sub_fps_stp setEnabled: NO];
298     [o_file_sub_encoding_lbl setStringValue: _NS("Subtitles encoding")];
299     [o_file_sub_encoding_pop removeAllItems];
300     [o_file_sub_size_lbl setStringValue: _NS("Font size")];
301     [o_file_sub_size_pop removeAllItems];
302     [o_file_sub_align_lbl setStringValue: _NS("Subtitles alignment")];
303     [o_file_sub_align_pop removeAllItems];
304     [o_file_sub_ok_btn setStringValue: _NS("OK")];
305     [o_file_sub_font_box setTitle: _NS("Font Properties")];
306     [o_file_sub_file_box setTitle: _NS("Subtitle File")];
307
308     p_item = config_FindConfig( VLC_OBJECT(p_intf), "subsdec-encoding" );
309
310     if( p_item )
311     {
312         for( i_index = 0; p_item->ppsz_list && p_item->ppsz_list[i_index];
313              i_index++ )
314         {
315             [o_file_sub_encoding_pop addItemWithTitle:
316                 [NSString stringWithUTF8String: p_item->ppsz_list[i_index]]];
317         }
318         [o_file_sub_encoding_pop selectItemWithTitle:
319                 [NSString stringWithUTF8String: p_item->value.psz]];
320     }
321
322     p_item = config_FindConfig( VLC_OBJECT(p_intf), "subsdec-align" );
323
324     if ( p_item )
325     {
326         for ( i_index = 0; i_index < p_item->i_list; i_index++ )
327         {
328             [o_file_sub_align_pop addItemWithTitle:
329                 [NSString stringWithUTF8String:
330                 p_item->ppsz_list_text[i_index]]];
331         }
332         [o_file_sub_align_pop selectItemAtIndex: p_item->value.i];
333     }
334
335     p_item = config_FindConfig( VLC_OBJECT(p_intf), "freetype-rel-fontsize" );
336
337     if ( p_item )
338     {
339         for ( i_index = 0; i_index < p_item->i_list; i_index++ )
340         {
341             [o_file_sub_size_pop addItemWithTitle:
342                 [NSString stringWithUTF8String:
343                 p_item->ppsz_list_text[i_index]]];
344             if ( p_item->value.i == p_item->pi_list[i_index] )
345             {
346                 [o_file_sub_size_pop selectItemAtIndex: i_index];
347             }
348         }
349     }
350 }
351
352 - (void)openTarget:(int)i_type
353 {
354     int i_result;
355
356     b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
357
358     [o_tabview selectTabViewItemAtIndex: i_type];
359     [o_file_sub_ckbox setState: NSOffState];
360  
361     i_result = [NSApp runModalForWindow: o_panel];
362     [o_panel close];
363
364     if( i_result )
365     {
366         NSMutableDictionary *o_dic;
367         NSMutableArray *o_options = [NSMutableArray array];
368         unsigned int i;
369
370         o_dic = [NSMutableDictionary dictionaryWithObject: [o_mrl stringValue] forKey: @"ITEM_URL"];
371         if( [o_file_sub_ckbox state] == NSOnState )
372         {
373             module_config_t * p_item;
374
375             [o_options addObject: [NSString stringWithFormat: @"sub-file=%@", [o_file_sub_path stringValue]]];
376             if( [o_file_sub_override state] == NSOnState )
377             {
378                 [o_options addObject: [NSString stringWithFormat: @"sub-delay=%i", (int)( [o_file_sub_delay intValue] * 10 )]];
379                 [o_options addObject: [NSString stringWithFormat: @"sub-fps=%f", [o_file_sub_fps floatValue]]];
380             }
381             [o_options addObject: [NSString stringWithFormat:
382                     @"subsdec-encoding=%@",
383                     [o_file_sub_encoding_pop titleOfSelectedItem]]];
384             [o_options addObject: [NSString stringWithFormat:
385                     @"subsdec-align=%i",
386                     [o_file_sub_align_pop indexOfSelectedItem]]];
387
388             p_item = config_FindConfig( VLC_OBJECT(p_intf),
389                                             "freetype-rel-fontsize" );
390
391             if ( p_item )
392             {
393                 [o_options addObject: [NSString stringWithFormat:
394                     @"freetype-rel-fontsize=%i",
395                     p_item->pi_list[[o_file_sub_size_pop indexOfSelectedItem]]]];
396             }
397         }
398         if( [o_output_ckbox state] == NSOnState )
399         {
400             for (i = 0 ; i < [[o_sout_options getMRL] count] ; i++)
401             {
402                 [o_options addObject: [NSString stringWithString:
403                       [[(VLCOutput *)o_sout_options getMRL] objectAtIndex: i]]];
404             }
405         }
406         if( [[[o_tabview selectedTabViewItem] label] isEqualToString: _NS("Capture")] )
407         {
408             if( [[[o_capture_mode_pop selectedItem] title] isEqualToString: _NS("Screen")] )
409                 [o_options addObject: [NSString stringWithFormat: @"screen-fps=%f", [o_screen_fps_fld floatValue]]];
410                 [o_options addObject: [NSString stringWithFormat: @"screen-left=%i", [o_screen_left_fld intValue]]];
411                 [o_options addObject: [NSString stringWithFormat: @"screen-top=%i", [o_screen_top_fld intValue]]];
412                 [o_options addObject: [NSString stringWithFormat: @"screen-width=%i", [o_screen_width_fld intValue]]];
413                 [o_options addObject: [NSString stringWithFormat: @"screen-height=%i", [o_screen_height_fld intValue]]];
414                 if( [o_screen_follow_mouse_ckb intValue] == YES )
415                     [o_options addObject: @"screen-follow-mouse"];
416                 else
417                     [o_options addObject: @"no-screen-follow-mouse"];
418         }
419
420         /* apply the options to our item(s) */
421         [o_dic setObject: (NSArray *)[o_options copy] forKey: @"ITEM_OPTIONS"];
422         if( b_autoplay )
423             [o_playlist appendArray: [NSArray arrayWithObject: o_dic] atPos: -1 enqueue:NO];
424         else
425             [o_playlist appendArray: [NSArray arrayWithObject: o_dic] atPos: -1 enqueue:YES];
426     }
427 }
428
429 - (void)tabView:(NSTabView *)o_tv didSelectTabViewItem:(NSTabViewItem *)o_tvi
430 {
431     NSString *o_label = [o_tvi label];
432
433     if( [o_label isEqualToString: _NS("File")] )
434     {
435         [self openFilePathChanged: nil];
436     }
437     else if( [o_label isEqualToString: _NS("Disc")] )
438     {
439         [self openDiscTypeChanged: nil];
440     }
441     else if( [o_label isEqualToString: _NS("Network")] )
442     {
443         [self openNetInfoChanged: nil];
444     }
445     else if( [o_label isEqualToString: _NS("Capture")] )
446     {
447         [self openCaptureModeChanged: nil];
448     }
449 }
450
451 - (void)openFileGeneric
452 {
453     [self openFilePathChanged: nil];
454     [self openTarget: 0];
455 }
456
457 - (void)openDisc
458 {
459     [self openDiscTypeChanged: nil];
460     [self openTarget: 1];
461 }
462
463 - (void)openNet
464 {
465     [self openNetInfoChanged: nil];
466     [self openTarget: 2];
467 }
468
469 - (void)openCapture
470 {
471     [self openCaptureModeChanged: nil];
472     [self showCaptureView: o_capture_label_view];
473     [self openTarget: 3];
474 }
475
476 - (void)openFilePathChanged:(NSNotification *)o_notification
477 {
478     NSString *o_mrl_string;
479     NSString *o_filename = [o_file_path stringValue];
480     NSString *o_ext = [o_filename pathExtension];
481     bool b_stream = [o_file_stream state];
482     BOOL b_dir = NO;
483  
484     [[NSFileManager defaultManager] fileExistsAtPath:o_filename isDirectory:&b_dir];
485
486     if( b_dir )
487     {
488         o_mrl_string = [NSString stringWithFormat: @"directory://%@/", o_filename];
489     }
490     else if( [o_ext isEqualToString: @"bin"] ||
491         [o_ext isEqualToString: @"cue"] ||
492         [o_ext isEqualToString: @"vob"] ||
493         [o_ext isEqualToString: @"iso"] )
494     {
495         o_mrl_string = o_filename;
496     }
497     else
498     {
499         o_mrl_string = [NSString stringWithFormat: @"%s://%@",
500                         b_stream ? "stream" : "file",
501                         o_filename];
502     }
503     [o_mrl setStringValue: o_mrl_string];
504 }
505
506 - (IBAction)openFileBrowse:(id)sender
507 {
508     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
509  
510     [o_open_panel setAllowsMultipleSelection: NO];
511     [o_open_panel setCanChooseDirectories: YES];
512     [o_open_panel setTitle: _NS("Open File")];
513     [o_open_panel setPrompt: _NS("Open")];
514
515     [o_open_panel beginSheetForDirectory:nil
516         file:nil
517         types:nil
518         modalForWindow:[sender window]
519         modalDelegate: self
520         didEndSelector: @selector(pathChosenInPanel:
521                         withReturn:
522                         contextInfo:)
523         contextInfo: nil];
524 }
525
526 - (void)pathChosenInPanel: (NSOpenPanel *) sheet withReturn:(int)returnCode contextInfo:(void  *)contextInfo
527 {
528     if (returnCode == NSFileHandlingPanelOKButton)
529     {
530         NSString *o_filename = [[sheet filenames] objectAtIndex: 0];
531         [o_file_path setStringValue: o_filename];
532         [self openFilePathChanged: nil];
533     }
534 }
535
536 - (IBAction)openFileStreamChanged:(id)sender
537 {
538     [self openFilePathChanged: nil];
539 }
540
541 - (IBAction)openDiscTypeChanged:(id)sender
542 {
543     NSString *o_type;
544     BOOL b_device, b_no_menus, b_title_chapter;
545  
546     [o_disc_device removeAllItems];
547     b_title_chapter = ![o_disc_dvd_menus state];
548  
549     o_type = [[o_disc_type selectedCell] title];
550
551     if ( [o_type isEqualToString: _NS("VIDEO_TS directory")] )
552     {
553         b_device = NO; b_no_menus = YES;
554     }
555     else
556     {
557         NSArray *o_devices;
558         NSString *o_disc;
559         const char *psz_class = NULL;
560         b_device = YES;
561
562         if ( [o_type isEqualToString: _NS("VCD")] )
563         {
564             psz_class = kIOCDMediaClass;
565             o_disc = o_type;
566             b_no_menus = NO; b_title_chapter = YES;
567                 }
568         else if ( [o_type isEqualToString: _NS("Audio CD")])
569         {
570             psz_class = kIOCDMediaClass;
571             o_disc = o_type;
572             b_no_menus = NO; b_title_chapter = NO;
573         }
574         else
575         {
576             psz_class = kIODVDMediaClass;
577             o_disc = o_type;
578             b_no_menus = YES;
579         }
580  
581         o_devices = GetEjectableMediaOfClass( psz_class );
582         if ( o_devices != nil )
583         {
584             int i_devices = [o_devices count];
585  
586             if ( i_devices )
587             {
588                                 for( int i = 0; i < i_devices; i++ )
589                 {
590                     [o_disc_device
591                         addItemWithObjectValue: [o_devices objectAtIndex: i]];
592                 }
593
594                 [o_disc_device selectItemAtIndex: 0];
595             }
596             else
597             {
598                 [o_disc_device setStringValue:
599                     [NSString stringWithFormat: _NS("No %@s found"), o_disc]];
600             }
601         }
602     }
603
604     [o_disc_device setEnabled: b_device];
605     [o_disc_title setEnabled: b_title_chapter];
606     [o_disc_title_stp setEnabled: b_title_chapter];
607     [o_disc_chapter setEnabled: b_title_chapter];
608     [o_disc_chapter_stp setEnabled: b_title_chapter];
609     [o_disc_videots_folder setEnabled: !b_device];
610     [o_disc_videots_btn_browse setEnabled: !b_device];
611     [o_disc_dvd_menus setEnabled: b_no_menus];
612
613     [self openDiscInfoChanged: nil];
614 }
615
616 - (IBAction)openDiscStepperChanged:(id)sender
617 {
618     int i_tag = [sender tag];
619
620     if( i_tag == 0 )
621     {
622         [o_disc_title setIntValue: [o_disc_title_stp intValue]];
623     }
624     else if( i_tag == 1 )
625     {
626         [o_disc_chapter setIntValue: [o_disc_chapter_stp intValue]];
627     }
628
629     [self openDiscInfoChanged: nil];
630 }
631
632 - (void)openDiscInfoChanged:(NSNotification *)o_notification
633 {
634     NSString *o_type;
635     NSString *o_device;
636     NSString *o_videots;
637     NSString *o_mrl_string;
638     int i_title, i_chapter;
639     BOOL b_no_menus;
640
641     o_type = [[o_disc_type selectedCell] title];
642     o_device = [o_disc_device stringValue];
643     i_title = [o_disc_title intValue];
644     i_chapter = [o_disc_chapter intValue];
645     o_videots = [o_disc_videots_folder stringValue];
646     b_no_menus = [o_disc_dvd_menus state];
647
648     if ( [o_type isEqualToString: _NS("VCD")] )
649     {
650         if ( [o_device isEqualToString:
651                 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
652             o_device = @"";
653         o_mrl_string = [NSString stringWithFormat: @"vcd://%@@%i:%i",
654                         o_device, i_title, i_chapter];
655     }
656     else if ( [o_type isEqualToString: _NS("Audio CD")] )
657     {
658         if ( [o_device isEqualToString:
659                 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
660             o_device = @"";
661         o_mrl_string = [NSString stringWithFormat: @"cdda://%@",
662                         o_device];
663     }
664     else if ( [o_type isEqualToString: _NS("DVD")] )
665     {
666         if ( [o_device isEqualToString:
667                 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
668             o_device = @"";
669         if ( b_no_menus )
670             o_mrl_string = [NSString stringWithFormat: @"dvdread://%@@%i:%i-",
671                             o_device, i_title, i_chapter];
672         else
673                         o_mrl_string = [NSString stringWithFormat: @"dvdnav://%@",
674                             o_device];
675             
676     }
677     else /* VIDEO_TS folder */
678     {
679         if ( b_no_menus )
680             o_mrl_string = [NSString stringWithFormat: @"dvdread://%@@%i:%i",
681                             o_videots, i_title, i_chapter];
682         else
683                         o_mrl_string = [NSString stringWithFormat: @"dvdnav://%@",
684                             o_videots];            
685     }
686
687     [o_mrl setStringValue: o_mrl_string];
688 }
689
690 - (IBAction)openDiscMenusChanged:(id)sender
691 {
692     [self openDiscInfoChanged: nil];
693     [self openDiscTypeChanged: nil];
694 }
695
696 - (IBAction)openVTSBrowse:(id)sender
697 {
698     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
699
700     [o_open_panel setAllowsMultipleSelection: NO];
701     [o_open_panel setCanChooseFiles: NO];
702     [o_open_panel setCanChooseDirectories: YES];
703     [o_open_panel setTitle: _NS("Open VIDEO_TS Directory")];
704     [o_open_panel setPrompt: _NS("Open")];
705
706     if( [o_open_panel runModalForDirectory: nil
707             file: nil types: nil] == NSOKButton )
708     {
709         NSString *o_dirname = [[o_open_panel filenames] objectAtIndex: 0];
710         [o_disc_videots_folder setStringValue: o_dirname];
711         [self openDiscInfoChanged: nil];
712     }
713 }
714
715 - (void)textFieldWasClicked:(NSNotification *)o_notification
716 {
717     if( [o_notification object] == o_net_udp_port )
718         [o_net_mode selectCellAtRow: 0 column: 0];
719     else if( [o_notification object] == o_net_udpm_addr ||
720              [o_notification object] == o_net_udpm_port )
721         [o_net_mode selectCellAtRow: 1 column: 0];
722     else
723         [o_net_mode selectCellAtRow: 2 column: 0];
724
725     [self openNetInfoChanged: nil];
726 }
727
728 - (IBAction)openNetModeChanged:(id)sender
729 {
730     if( [[sender selectedCell] tag] == 0 )
731         [o_panel makeFirstResponder: o_net_udp_port];
732     else if ( [[sender selectedCell] tag] == 1 )
733         [o_panel makeFirstResponder: o_net_udpm_addr];
734     else
735         msg_Warn( p_intf, "Unknown sender tried to change UDP/RTP mode" );
736
737
738     [self openNetInfoChanged: nil];
739 }
740
741 - (IBAction)openNetStepperChanged:(id)sender
742 {
743     int i_tag = [sender tag];
744
745     if( i_tag == 0 )
746     {
747         [o_net_udp_port setIntValue: [o_net_udp_port_stp intValue]];
748         [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCOpenTextFieldWasClicked"
749                                                             object: o_net_udp_port];
750         [o_panel makeFirstResponder: o_net_udp_port];
751     }
752     else if( i_tag == 1 )
753     {
754         [o_net_udpm_port setIntValue: [o_net_udpm_port_stp intValue]];
755         [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCOpenTextFieldWasClicked"
756                                                             object: o_net_udpm_port];
757         [o_panel makeFirstResponder: o_net_udpm_port];
758     }
759
760     [self openNetInfoChanged: nil];
761 }
762
763 - (void)openNetInfoChanged:(NSNotification *)o_notification
764 {
765     NSString *o_mrl_string = [NSString string];
766
767     if( [o_net_udp_panel isVisible] )
768     {
769         NSString *o_mode;
770         o_mode = [[o_net_mode selectedCell] title];
771
772         if( [o_mode isEqualToString: _NS("UDP/RTP")] )
773         {
774             int i_port = [o_net_udp_port intValue];
775
776             o_mrl_string = [NSString stringWithString: @"udp://"];
777
778             if( i_port != config_GetInt( p_intf, "server-port" ) )
779             {
780                 o_mrl_string =
781                     [o_mrl_string stringByAppendingFormat: @"@:%i", i_port];
782             }
783         }
784         else if( [o_mode isEqualToString: _NS("UDP/RTP Multicast")] )
785         {
786             NSString *o_addr = [o_net_udpm_addr stringValue];
787             int i_port = [o_net_udpm_port intValue];
788
789             o_mrl_string = [NSString stringWithFormat: @"udp://@%@", o_addr];
790
791             if( i_port != config_GetInt( p_intf, "server-port" ) )
792             {
793                 o_mrl_string =
794                     [o_mrl_string stringByAppendingFormat: @":%i", i_port];
795             }
796         }
797     }
798     else
799     {
800         NSString *o_url = [o_net_http_url stringValue];
801
802         if ( ![o_url hasPrefix:@"http:"] && ![o_url hasPrefix:@"ftp:"]
803               && ![o_url hasPrefix:@"mms"] && ![o_url hasPrefix:@"rtsp"] )
804             o_mrl_string = [NSString stringWithFormat: @"http://%@", o_url];
805         else
806             o_mrl_string = o_url;
807     }
808     [o_mrl setStringValue: o_mrl_string];
809 }
810
811 - (IBAction)openNetUDPButtonAction:(id)sender
812 {
813     if( sender == o_net_openUDP_btn )
814     {
815         [NSApp beginSheet: o_net_udp_panel
816            modalForWindow: o_panel
817             modalDelegate: self
818            didEndSelector: NULL
819               contextInfo: nil];
820     }
821     else if( sender == o_net_udp_cancel_btn )
822     {
823         [o_net_udp_panel orderOut: sender];
824         [NSApp endSheet: o_net_udp_panel];
825     }
826     else if( sender == o_net_udp_ok_btn )
827     {
828         NSString *o_mrl_string = [NSString string];
829         if( [[[o_net_mode selectedCell] title] isEqualToString: _NS("UDP/RTP")] )
830         {
831             int i_port = [o_net_udp_port intValue];
832             
833             o_mrl_string = [NSString stringWithString: @"udp://"];
834             
835             if( i_port != config_GetInt( p_intf, "server-port" ) )
836             {
837                 o_mrl_string =
838                 [o_mrl_string stringByAppendingFormat: @"@:%i", i_port];
839             }
840         }
841         else if( [[[o_net_mode selectedCell] title] isEqualToString: _NS("UDP/RTP Multicast")] )
842         {
843             NSString *o_addr = [o_net_udpm_addr stringValue];
844             int i_port = [o_net_udpm_port intValue];
845             
846             o_mrl_string = [NSString stringWithFormat: @"udp://@%@", o_addr];
847             
848             if( i_port != config_GetInt( p_intf, "server-port" ) )
849             {
850                 o_mrl_string =
851                 [o_mrl_string stringByAppendingFormat: @":%i", i_port];
852             }
853         }
854         [o_mrl setStringValue: o_mrl_string];
855         [o_net_udp_panel orderOut: sender];
856         [NSApp endSheet: o_net_udp_panel];
857     }
858 }
859     
860 - (void)openFile
861 {
862     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
863     int i;
864     b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
865  
866     [o_open_panel setAllowsMultipleSelection: YES];
867     [o_open_panel setCanChooseDirectories: YES];
868     [o_open_panel setTitle: _NS("Open File")];
869     [o_open_panel setPrompt: _NS("Open")];
870  
871     if( [o_open_panel runModalForDirectory: nil
872             file: nil types: nil] == NSOKButton )
873     {
874         NSArray *o_array = [NSArray array];
875         NSArray *o_values = [[o_open_panel filenames]
876                 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
877
878         for( i = 0; i < (int)[o_values count]; i++)
879         {
880             NSDictionary *o_dic;
881             o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
882             o_array = [o_array arrayByAddingObject: o_dic];
883         }
884         if( b_autoplay )
885             [o_playlist appendArray: o_array atPos: -1 enqueue:NO];
886         else
887             [o_playlist appendArray: o_array atPos: -1 enqueue:YES];
888     }
889 }
890
891 - (void)showCaptureView: theView
892 {
893     NSRect o_view_rect;
894     o_view_rect = [theView frame];
895     if( o_currentCaptureView )
896     {
897         [o_currentCaptureView removeFromSuperviewWithoutNeedingDisplay];
898         [o_currentCaptureView release];
899     }
900     [theView setFrame: NSMakeRect( 0, -10, o_view_rect.size.width, o_view_rect.size.height)];
901     [theView setNeedsDisplay: YES];
902     [theView setAutoresizesSubviews: YES];
903     [[[o_tabview tabViewItemAtIndex: 3] view] addSubview: theView];
904     [theView displayIfNeeded];
905     o_currentCaptureView = theView;
906     [o_currentCaptureView retain];
907 }
908
909 - (IBAction)openCaptureModeChanged:(id)sender
910 {
911     if( [[[o_capture_mode_pop selectedItem] title] isEqualToString: @"EyeTV"] )
912     {
913         if( [[[VLCMain sharedInstance] getEyeTVController] isEyeTVrunning] == YES )
914         {
915             if( [[[VLCMain sharedInstance] getEyeTVController] isDeviceConnected] == YES )
916             {
917                 [self showCaptureView: o_eyetv_running_view];
918                 [self setupChannelInfo];
919             }
920             else
921             {
922                 setEyeTVUnconnected;
923             }
924         }
925         else
926             [self showCaptureView: o_eyetv_notLaunched_view];
927         [o_mrl setStringValue: @""];
928     } 
929     else if( [[[o_capture_mode_pop selectedItem] title] isEqualToString: _NS("Screen")] )
930     {
931         [self showCaptureView: o_screen_view];
932         [o_mrl setStringValue: @"screen://"];
933         [o_screen_height_fld setIntValue: config_GetInt( p_intf, "screen-height" )];
934         [o_screen_width_fld setIntValue: config_GetInt( p_intf, "screen-width" )];
935         [o_screen_fps_fld setFloatValue: config_GetFloat( p_intf, "screen-fps" )];
936         [o_screen_left_fld setIntValue: config_GetInt( p_intf, "screen-left" )];
937         [o_screen_top_fld setIntValue: config_GetInt( p_intf, "screen-top" )];
938         [o_screen_follow_mouse_ckb setIntValue: config_GetInt( p_intf, "screen-follow-mouse" )];
939     }
940     else if( [[[o_capture_mode_pop selectedItem] title] isEqualToString: @"iSight"] )
941     {
942         [o_capture_lbl setStringValue: _NS("iSight Capture Input")];
943         [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.")];
944         [o_capture_lbl displayIfNeeded];
945         [o_capture_long_lbl displayIfNeeded];
946         
947         [self showCaptureView: o_capture_label_view];
948         [o_mrl setStringValue: @"qtcapture://"];
949     }
950 }
951
952 - (IBAction)screenStepperChanged:(id)sender
953 {
954     [o_screen_fps_fld setFloatValue: [o_screen_fps_stp floatValue]];
955     [o_panel makeFirstResponder: o_screen_fps_fld];
956     [o_mrl setStringValue: @"screen://"];
957 }
958
959 - (void)screenFPSfieldChanged:(NSNotification *)o_notification
960 {
961     [o_screen_fps_stp setFloatValue: [o_screen_fps_fld floatValue]];
962     if( [[o_screen_fps_fld stringValue] isEqualToString: @""] )
963         [o_screen_fps_fld setFloatValue: 1.0];
964     [o_mrl setStringValue: @"screen://"];
965 }
966
967 - (IBAction)eyetvSwitchChannel:(id)sender
968 {
969     if( sender == o_eyetv_nextProgram_btn )
970     {
971         int chanNum = [[[VLCMain sharedInstance] getEyeTVController] switchChannelUp: YES];
972         [o_eyetv_channels_pop selectItemWithTag:chanNum];
973         [o_mrl setStringValue: [NSString stringWithFormat:@"eyetv:// :eyetv-channel=%d", chanNum]];
974     }
975     else if( sender == o_eyetv_previousProgram_btn )
976     {
977         int chanNum = [[[VLCMain sharedInstance] getEyeTVController] switchChannelUp: NO];
978         [o_eyetv_channels_pop selectItemWithTag:chanNum];
979         [o_mrl setStringValue: [NSString stringWithFormat:@"eyetv:// :eyetv-channel=%d", chanNum]];
980     }
981     else if( sender == o_eyetv_channels_pop )
982     {
983         int chanNum = [[sender selectedItem] tag];
984         [[[VLCMain sharedInstance] getEyeTVController] selectChannel:chanNum];
985         [o_mrl setStringValue: [NSString stringWithFormat:@"eyetv:// :eyetv-channel=%d", chanNum]];
986     }
987     else
988         msg_Err( VLCIntf, "eyetvSwitchChannel sent by unknown object" );
989 }
990
991 - (IBAction)eyetvLaunch:(id)sender
992 {
993     [[[VLCMain sharedInstance] getEyeTVController] launchEyeTV];
994 }
995
996 - (IBAction)eyetvGetPlugin:(id)sender
997 {
998     [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: @"http://www.videolan.org/vlc/eyetv"]];
999 }
1000
1001 - (void)eyetvChanged:(NSNotification *)o_notification
1002 {
1003     if( [[o_notification name] isEqualToString: @"DeviceAdded"] )
1004     {
1005         msg_Dbg( VLCIntf, "eyetv device was added" );
1006         [self showCaptureView: o_eyetv_running_view];
1007         [self setupChannelInfo];
1008     }
1009     else if( [[o_notification name] isEqualToString: @"DeviceRemoved"] )
1010     {
1011         /* leave the channel selection like that,
1012          * switch to our "no device" tab */
1013         msg_Dbg( VLCIntf, "eyetv device was removed" );
1014         setEyeTVUnconnected;
1015     }
1016     else if( [[o_notification name] isEqualToString: @"PluginQuit"] )
1017     {
1018         /* switch to the "launch eyetv" tab */
1019         msg_Dbg( VLCIntf, "eyetv was terminated" );
1020         [self showCaptureView: o_eyetv_notLaunched_view];
1021     }
1022     else if( [[o_notification name] isEqualToString: @"PluginInit"] )
1023     {
1024         /* we got no device yet */
1025         msg_Dbg( VLCIntf, "eyetv was launched, no device yet" );
1026         setEyeTVUnconnected;
1027     }
1028     else
1029         msg_Warn( VLCIntf, "unknown external notify '%s' received", [[o_notification name] UTF8String] );
1030 }    
1031
1032 /* little helper method, since this code needs to be run by multiple objects */
1033 - (void)setupChannelInfo
1034 {
1035     /* set up channel selection */
1036     [o_eyetv_channels_pop removeAllItems];
1037     [o_eyetv_chn_bgbar setHidden: NO];
1038     [o_eyetv_chn_bgbar animate: self];
1039     [o_eyetv_chn_status_txt setStringValue: _NS("Retrieving Channel Info...")];
1040     [o_eyetv_chn_status_txt setHidden: NO];
1041  
1042     /* retrieve info */
1043     NSEnumerator *channels = [[[VLCMain sharedInstance] getEyeTVController] allChannels];
1044     int x = -2;
1045     [[[o_eyetv_channels_pop menu] addItemWithTitle: _NS("Composite input")
1046                                                action: nil
1047                                         keyEquivalent: @""] setTag:x++];
1048     [[[o_eyetv_channels_pop menu] addItemWithTitle: _NS("S-Video input")
1049                                                action: nil
1050                                         keyEquivalent: @""] setTag:x++];
1051     if( channels ) 
1052     {
1053         NSString *channel;
1054         [[o_eyetv_channels_pop menu] addItem: [NSMenuItem separatorItem]];
1055         while( channel = [channels nextObject] )
1056         {
1057             /* we have to add items this way, because we accept duplicates
1058              * additionally, we save a bit of time */
1059             [[[o_eyetv_channels_pop menu] addItemWithTitle: channel
1060                                                    action: nil
1061                                             keyEquivalent: @""] setTag:++x];
1062         }
1063         /* make Tuner the default */
1064         [o_eyetv_channels_pop selectItemWithTag:[[[VLCMain sharedInstance] getEyeTVController] currentChannel]];
1065     }
1066  
1067     /* clean up GUI */
1068     [o_eyetv_chn_bgbar setHidden: YES];
1069     [o_eyetv_chn_status_txt setHidden: YES];
1070 }
1071
1072 - (IBAction)subsChanged:(id)sender
1073 {
1074     if ([o_file_sub_ckbox state] == NSOnState)
1075     {
1076         [o_file_sub_btn_settings setEnabled:YES];
1077     }
1078     else
1079     {
1080         [o_file_sub_btn_settings setEnabled:NO];
1081     }
1082 }
1083
1084 - (IBAction)subSettings:(id)sender
1085 {
1086     [NSApp beginSheet: o_file_sub_sheet
1087         modalForWindow: [sender window]
1088         modalDelegate: self
1089         didEndSelector: NULL
1090         contextInfo: nil];
1091 }
1092
1093 - (IBAction)subCloseSheet:(id)sender
1094 {
1095     [o_file_sub_sheet orderOut:sender];
1096     [NSApp endSheet: o_file_sub_sheet];
1097 }
1098     
1099 - (IBAction)subFileBrowse:(id)sender
1100 {
1101     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
1102  
1103     [o_open_panel setAllowsMultipleSelection: NO];
1104     [o_open_panel setTitle: _NS("Open File")];
1105     [o_open_panel setPrompt: _NS("Open")];
1106
1107     if( [o_open_panel runModalForDirectory: nil
1108             file: nil types: nil] == NSOKButton )
1109     {
1110         NSString *o_filename = [[o_open_panel filenames] objectAtIndex: 0];
1111         [o_file_sub_path setStringValue: o_filename];
1112     }
1113 }
1114
1115 - (IBAction)subOverride:(id)sender
1116 {
1117     BOOL b_state = [o_file_sub_override state];
1118     [o_file_sub_delay setEnabled: b_state];
1119     [o_file_sub_delay_stp setEnabled: b_state];
1120     [o_file_sub_fps setEnabled: b_state];
1121     [o_file_sub_fps_stp setEnabled: b_state];
1122 }
1123
1124 - (IBAction)subDelayStepperChanged:(id)sender
1125 {
1126     [o_file_sub_delay setIntValue: [o_file_sub_delay_stp intValue]];
1127 }
1128
1129 - (IBAction)subFpsStepperChanged:(id)sender;
1130 {
1131     [o_file_sub_fps setFloatValue: [o_file_sub_fps_stp floatValue]];
1132 }
1133
1134 - (IBAction)panelCancel:(id)sender
1135 {
1136     [NSApp stopModalWithCode: 0];
1137 }
1138
1139 - (IBAction)panelOk:(id)sender
1140 {
1141     if( [[o_mrl stringValue] length] )
1142     {
1143         [NSApp stopModalWithCode: 1];
1144     }
1145     else
1146     {
1147         NSBeep();
1148     }
1149 }
1150
1151 @end
1152
1153 @implementation VLCOpenTextField
1154
1155 - (void)mouseDown:(NSEvent *)theEvent
1156 {
1157     [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCOpenTextFieldWasClicked"
1158                                                         object: self];
1159     [super mouseDown: theEvent];
1160 }
1161
1162 @end