]> git.sesse.net Git - vlc/blob - modules/gui/macosx/open.m
* ./modules/gui/macosx/open.m: compile fix
[vlc] / modules / gui / macosx / open.m
1 /*****************************************************************************
2  * open.m: MacOS X plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: open.m,v 1.2 2002/08/06 23:43:58 jlj Exp $
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net> 
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <sys/param.h>                                    /* for MAXPATHLEN */
29 #include <string.h>
30
31 #include <paths.h>
32 #include <IOKit/IOKitLib.h>
33 #include <IOKit/IOBSD.h>
34 #include <IOKit/storage/IOMedia.h>
35 #include <IOKit/storage/IOCDMedia.h>
36 #include <IOKit/storage/IODVDMedia.h>
37
38 #import <Cocoa/Cocoa.h>
39
40 #include <vlc/vlc.h>
41 #include <vlc/intf.h>
42
43 #include "netutils.h"
44
45 #import "intf.h"
46 #import "playlist.h"
47 #import "open.h"
48
49 /*****************************************************************************
50  * GetEjectableMediaOfClass 
51  *****************************************************************************/
52 NSArray *GetEjectableMediaOfClass( const char *psz_class )
53 {
54     io_object_t next_media;
55     mach_port_t master_port;
56     kern_return_t kern_result;
57     NSArray *o_devices = nil;
58     NSMutableArray *p_list = nil;
59     io_iterator_t media_iterator;
60     CFMutableDictionaryRef classes_to_match;
61
62     kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
63     if( kern_result != KERN_SUCCESS )
64     {
65         return( nil );
66     }
67     
68     classes_to_match = IOServiceMatching( psz_class );
69     if( classes_to_match == NULL )
70     {
71         return( nil );
72     }
73     
74     CFDictionarySetValue( classes_to_match, CFSTR( kIOMediaEjectable ), 
75                           kCFBooleanTrue );
76     
77     kern_result = IOServiceGetMatchingServices( master_port, classes_to_match, 
78                                                 &media_iterator );
79     if( kern_result != KERN_SUCCESS )
80     {
81         return( nil );
82     }
83
84     p_list = [NSMutableArray arrayWithCapacity: 1];
85     
86     next_media = IOIteratorNext( media_iterator );
87     if( next_media != NULL )
88     {
89         char psz_buf[0x32];
90         size_t dev_path_length;
91         CFTypeRef str_bsd_path;
92     
93         do
94         {
95             str_bsd_path = IORegistryEntryCreateCFProperty( next_media,
96                                                             CFSTR( kIOBSDName ),
97                                                             kCFAllocatorDefault,
98                                                             0 );
99             if( str_bsd_path == NULL )
100             {
101                 IOObjectRelease( next_media );
102                 continue;
103             }
104             
105             snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
106             dev_path_length = strlen( psz_buf );
107             
108             if( CFStringGetCString( str_bsd_path,
109                                     (char*)&psz_buf + dev_path_length,
110                                     sizeof(psz_buf) - dev_path_length,
111                                     kCFStringEncodingASCII ) )
112             {
113                 [p_list addObject: [NSString stringWithCString: psz_buf]];
114             }
115             
116             CFRelease( str_bsd_path );
117             
118             IOObjectRelease( next_media );
119         
120         } while( ( next_media = IOIteratorNext( media_iterator ) ) != NULL );
121     }
122     
123     IOObjectRelease( media_iterator );
124
125     o_devices = [NSArray arrayWithArray: p_list];
126
127     return( o_devices );
128 }
129
130 /*****************************************************************************
131  * VLCOpen implementation 
132  *****************************************************************************/
133 @implementation VLCOpen
134
135 - (void)awakeFromNib
136 {
137     [o_disc_panel setTitle: _NS("Open Disc")];
138     [o_disc_btn_ok setTitle: _NS("OK")];
139     [o_disc_btn_cancel setTitle: _NS("Cancel")];
140     [o_disc_lbl_type setTitle: _NS("Disc type")];
141     [o_disc_lbl_sp setTitle: _NS("Starting position")];
142     [o_disc_title setTitle: _NS("Title")];
143     [o_disc_chapter setTitle: _NS("Chapter")];
144
145     [o_net_panel setTitle: _NS("Open Network")];
146     [o_net_box_mode setTitle: _NS("Network mode")];
147     [o_net_box_addr setTitle: _NS("Address")];
148     [o_net_port_lbl setStringValue: _NS("Port")];
149
150     [o_quickly_panel setTitle: _NS("Open Quickly")];
151     [o_quickly_btn_ok setTitle: _NS("OK")];
152     [o_quickly_btn_cancel setTitle: _NS("Cancel")];
153 }
154
155 - (IBAction)openDisc:(id)sender
156 {
157     int i_result;
158
159     [self openDiscTypeChanged: nil];
160     
161     [o_disc_panel makeKeyAndOrderFront: self];
162     i_result = [NSApp runModalForWindow: o_disc_panel];
163     [o_disc_panel close];
164
165     if( i_result )
166     {
167         NSString *o_source;
168
169         NSString *o_type = [[o_disc_type selectedCell] title];
170         NSString *o_device = [o_disc_device stringValue];
171         int i_title = [o_disc_title intValue];
172         int i_chapter = [o_disc_chapter intValue];
173
174         o_source = [NSString stringWithFormat: @"%@:%@@%d,%d",
175             [o_type lowercaseString], o_device, i_title, i_chapter];
176
177         [o_playlist appendArray: 
178             [NSArray arrayWithObject: o_source] atPos: -1];
179     }
180 }
181
182 - (IBAction)openDiscTypeChanged:(id)sender
183 {
184     NSString *o_type;
185     NSArray *o_devices;
186     const char *psz_class = NULL;
187     
188     [o_disc_device removeAllItems];
189     
190     o_type = [[o_disc_type selectedCell] title];
191
192     if( [o_type isEqualToString: @"DVD"] )
193     {
194         psz_class = kIODVDMediaClass;
195     }
196     else
197     {
198         psz_class = kIOCDMediaClass;
199     }
200     
201     o_devices = GetEjectableMediaOfClass( psz_class );
202     if( o_devices != nil )
203     {
204         int i_devices = [o_devices count];
205         
206         if( i_devices )
207         {
208             int i;
209         
210             for( i = 0; i < i_devices; i++ )
211             {
212                 [o_disc_device 
213                     addItemWithObjectValue: [o_devices objectAtIndex: i]];
214             }
215             
216             [o_disc_device selectItemAtIndex: 0];
217             [o_disc_btn_ok setEnabled: TRUE];
218         }
219         else
220         {
221             [o_disc_device setStringValue: 
222                 [NSString stringWithFormat: @"No %@s found", o_type]];
223             [o_disc_btn_ok setEnabled: FALSE];
224         }
225     }
226 }
227
228 - (IBAction)openFile:(id)sender
229 {
230     NSOpenPanel *o_panel = [NSOpenPanel openPanel];
231     
232     [o_panel setAllowsMultipleSelection: YES];
233
234     if( [o_panel runModalForDirectory: nil 
235             file: nil types: nil] == NSOKButton )
236     {
237         [o_playlist appendArray: [o_panel filenames] atPos: -1];
238     }
239 }
240
241 - (IBAction)openNet:(id)sender
242 {
243     int i_result;
244     intf_thread_t * p_intf = [NSApp getIntf];
245
246     [o_net_panel makeKeyAndOrderFront: self];
247     i_result = [NSApp runModalForWindow: o_net_panel];
248     [o_net_panel close];
249
250     if( i_result )
251     {
252         NSString * o_source = nil;
253         UInt32 i_port = [o_net_port intValue];
254         NSString * o_addr = [o_net_address stringValue];
255         NSString * o_mode = [[o_net_mode selectedCell] title];
256
257         if( i_port > 65536 )
258         {
259             NSBeep();
260             return;
261         }
262
263         if( [o_mode isEqualToString: @"UDP"] )
264         {
265             o_source = [NSString 
266                 stringWithFormat: @"udp:@:%i", i_port];
267         } 
268         else if( [o_mode isEqualToString: @"UDP Multicase"] )
269         {
270             o_source = [NSString 
271                 stringWithFormat: @"udp:@%@:%i", o_addr, i_port];
272         }
273         else if( [o_mode isEqualToString: @"Channel server"] )
274         {
275             if( p_intf->p_vlc->p_channel == NULL )
276             {
277                 network_ChannelCreate( p_intf );
278             }
279
280             config_PutPsz( p_intf, "channel-server", 
281                            (char *)[o_addr lossyCString] );
282             config_PutInt( p_intf, "channel-port", i_port );
283
284             p_intf->p_sys->b_playing = 1;
285         }
286         else if( [o_mode isEqualToString: @"HTTP"] )
287         {
288             o_source = o_addr;
289         }
290
291         if( o_source != nil )
292         {
293             [o_playlist appendArray:
294                 [NSArray arrayWithObject: o_source] atPos: -1];
295         }
296     }
297 }
298
299 - (IBAction)openNetModeChanged:(id)sender
300 {
301     NSString * o_mode;
302     SInt32 i_port = 1234;
303     NSString * o_addr = nil;
304
305     o_mode = [[o_net_mode selectedCell] title];
306
307     if( [o_mode isEqualToString: @"UDP Multicast"] )
308     {
309         o_addr = @"";
310     }
311     else if( [o_mode isEqualToString: @"Channel server"] )
312     {
313         o_addr = @"localhost";
314         i_port = 6010;
315     }
316     else if( [o_mode isEqualToString: @"HTTP"] )
317     {
318         o_addr = @"http://";
319         i_port = -1;
320     }
321
322     if( o_addr != nil )
323     {
324         [o_net_address setEnabled: TRUE];
325         [o_net_address setStringValue: o_addr];
326     }
327     else
328     {
329         [o_net_address setEnabled: FALSE];
330     }
331
332     if( i_port > -1 )
333     {
334         [o_net_port setEnabled: TRUE];
335         [o_net_port_stp setEnabled: TRUE];
336         [o_net_port setIntValue: i_port];
337     }
338     else
339     {
340         [o_net_port setEnabled: FALSE];
341         [o_net_port_stp setEnabled: FALSE];
342     }
343 }
344
345 - (IBAction)openQuickly:(id)sender
346 {
347     int i_result;
348
349     [o_quickly_source setStringValue: @""];
350     [o_quickly_panel makeKeyAndOrderFront: self];
351     i_result = [NSApp runModalForWindow: o_quickly_panel];
352     [o_quickly_panel close];
353
354     if( i_result )
355     {
356         NSString * o_source;
357
358         o_source = [o_quickly_source stringValue];
359
360         if( [o_source length] > 0 )
361         {
362             [o_playlist appendArray: 
363                 [NSArray arrayWithObject: o_source] atPos: -1];
364         }
365         else
366         {
367             NSBeep();
368         }
369     }
370 }
371
372 - (IBAction)panelCancel:(id)sender
373 {
374     [NSApp stopModalWithCode: 0];
375 }
376
377 - (IBAction)panelOk:(id)sender
378 {
379     [NSApp stopModalWithCode: 1];
380 }
381
382 @end