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