]> git.sesse.net Git - vlc/blob - plugins/macosx/intf_open.m
* ./plugins/gtk/gnome_callbacks.c: fixed a crash when activating preferences
[vlc] / plugins / macosx / intf_open.m
1 /*****************************************************************************
2  * intf_open.c: MacOS X plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: intf_open.m,v 1.3.2.1 2002/06/02 22:32:46 massiot 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 <paths.h>
28 #include <IOKit/IOKitLib.h>
29 #include <IOKit/IOBSD.h>
30 #include <IOKit/storage/IOMedia.h>
31 #include <IOKit/storage/IOCDMedia.h>
32 #include <IOKit/storage/IODVDMedia.h>
33
34 #import "intf_open.h"
35 #import "intf_vlc_wrapper.h"
36
37 /*****************************************************************************
38  * GetEjectableMediaOfClass 
39  *****************************************************************************/
40 NSArray *GetEjectableMediaOfClass( const char *psz_class )
41 {
42     io_object_t next_media;
43     mach_port_t master_port;
44     kern_return_t kern_result;
45     NSArray *o_devices = nil;
46     NSMutableArray *p_list = nil;
47     io_iterator_t media_iterator;
48     CFMutableDictionaryRef classes_to_match;
49
50     kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
51     if( kern_result != KERN_SUCCESS )
52     {
53         return( nil );
54     }
55     
56     classes_to_match = IOServiceMatching( psz_class );
57     if( classes_to_match == NULL )
58     {
59         return( nil );
60     }
61     
62     CFDictionarySetValue( classes_to_match, CFSTR( kIOMediaEjectable ), 
63                           kCFBooleanTrue );
64     
65     kern_result = IOServiceGetMatchingServices( master_port, classes_to_match, 
66                                                 &media_iterator );
67     if( kern_result != KERN_SUCCESS )
68     {
69         return( nil );
70     }
71
72     p_list = [NSMutableArray arrayWithCapacity: 1];
73     
74     next_media = IOIteratorNext( media_iterator );
75     if( next_media != NULL )
76     {
77         char psz_buf[0x32];
78         size_t dev_path_length;
79         CFTypeRef str_bsd_path;
80     
81         do
82         {
83             str_bsd_path = IORegistryEntryCreateCFProperty( next_media,
84                                                             CFSTR( kIOBSDName ),
85                                                             kCFAllocatorDefault,
86                                                             0 );
87             if( str_bsd_path == NULL )
88             {
89                 IOObjectRelease( next_media );
90                 continue;
91             }
92             
93             snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
94             dev_path_length = strlen( psz_buf );
95             
96             if( CFStringGetCString( str_bsd_path,
97                                     (char*)&psz_buf + dev_path_length,
98                                     sizeof(psz_buf) - dev_path_length,
99                                     kCFStringEncodingASCII ) )
100             {
101                 [p_list addObject: [NSString stringWithCString: psz_buf]];
102             }
103             
104             CFRelease( str_bsd_path );
105             
106             IOObjectRelease( next_media );
107         
108         } while( ( next_media = IOIteratorNext( media_iterator ) ) != NULL );
109     }
110     
111     IOObjectRelease( media_iterator );
112
113     o_devices = [NSArray arrayWithArray: p_list];
114
115     return( o_devices );
116 }
117
118 /*****************************************************************************
119  * Intf_Open implementation 
120  *****************************************************************************/
121 @implementation Intf_Open
122
123 static Intf_Open *o_open = nil;
124
125 - (id)init
126 {
127     if( o_open == nil )
128     {
129         o_open = [super init];
130     }
131
132     return( o_open );
133 }
134
135 + (Intf_Open *)instance
136 {
137     return( o_open );
138 }
139
140 - (void)awakeFromNib
141 {
142     [o_net_server_addr setEnabled: NSOffState];
143     [o_net_server_addr_label setStringValue: @"Address"];
144     [o_net_server_port setEnabled: NSOnState];
145     [o_net_server_port setIntValue: 1234];
146     [o_net_server_pstepper setEnabled: NSOnState];
147     [o_net_server_pstepper setIntValue: [o_net_server_port intValue]];
148 }
149
150 - (IBAction)openDisc:(id)sender
151 {
152     int i_result;
153
154     [self openDiscTypeChanged: nil];
155     
156     [o_disc_panel makeKeyAndOrderFront: self];
157     i_result = [NSApp runModalForWindow: o_disc_panel];
158     [o_disc_panel close];
159
160     if( i_result )
161     {
162         NSString *o_type = [[o_disc_type selectedCell] title];
163         NSString *o_device = [o_disc_device stringValue];
164         int i_title = [o_disc_title intValue];
165         int i_chapter = [o_disc_chapter intValue];
166         
167         [[Intf_VLCWrapper instance] openDisc: [o_type lowercaseString]
168             device: o_device title: i_title chapter: i_chapter];
169     }
170 }
171
172 - (IBAction)openDiscTypeChanged:(id)sender
173 {
174     NSString *o_type;
175     NSArray *o_devices;
176     const char *psz_class = NULL;
177     
178     [o_disc_device removeAllItems];
179     
180     o_type = [[o_disc_type selectedCell] title];
181
182     if( [o_type isEqualToString: @"DVD"] )
183     {
184         psz_class = kIODVDMediaClass;
185     }
186     else
187     {
188         psz_class = kIOCDMediaClass;
189     }
190     
191     o_devices = GetEjectableMediaOfClass( psz_class );
192     if( o_devices != nil )
193     {
194         int i_devices = [o_devices count];
195         
196         if( i_devices )
197         {
198             int i;
199         
200             for( i = 0; i < i_devices; i++ )
201             {
202                 [o_disc_device 
203                     addItemWithObjectValue: [o_devices objectAtIndex: i]];
204             }
205             
206             [o_disc_device selectItemAtIndex: 0];
207         }
208         else
209         {
210             [o_disc_device setStringValue: 
211                 [NSString stringWithFormat: @"No %@s found", o_type]];
212         }
213     }
214 }
215
216 - (IBAction)openFile:(id)sender
217 {
218     NSOpenPanel *o_panel = [NSOpenPanel openPanel];
219     
220     [o_panel setAllowsMultipleSelection: YES];
221
222     if( [o_panel runModalForDirectory: nil 
223             file: nil types: nil] == NSOKButton )
224     {
225         [[Intf_VLCWrapper instance] openFiles: [o_panel filenames]];
226     }
227 }
228
229 - (IBAction)openNet:(id)sender
230 {
231     int i_result;
232
233     [o_net_panel makeKeyAndOrderFront: self];
234     i_result = [NSApp runModalForWindow: o_net_panel];
235     [o_net_panel close];
236
237     if( i_result )
238     {
239         NSString *o_protocol;
240         int i_port = [o_net_server_port intValue];
241         NSString *o_addr = [o_net_server_addr stringValue];
242
243         o_protocol = [[o_net_protocol selectedCell] title];
244
245         if( [o_protocol isEqualToString: @"UDP"] )
246         {
247             [[Intf_VLCWrapper instance] openNet: @"" port: i_port]; 
248         }
249         else if( [o_protocol isEqualToString: @"UDP - multicast"] ) 
250         {
251             [[Intf_VLCWrapper instance] openNet: o_addr port: i_port]; 
252         }
253         else if( [o_protocol isEqualToString: @"Channel server"] ) 
254         {
255             [[Intf_VLCWrapper instance] openNetChannel: o_addr port: i_port]; 
256         }
257         else if( [o_protocol isEqualToString: @"HTTP"] ) 
258         {
259             [[Intf_VLCWrapper instance] openNetHTTP: o_addr]; 
260         }
261     }
262 }
263
264 - (IBAction)openNetProtocol:(id)sender
265 {
266     NSString *o_protocol;
267
268     o_protocol = [[o_net_protocol selectedCell] title];
269     
270     if( [o_protocol isEqualToString: @"UDP"] )
271     {
272         [o_net_server_addr setEnabled: NSOffState];
273         [o_net_server_port setEnabled: NSOnState];
274         [o_net_server_port setIntValue: 1234];
275         [o_net_server_pstepper setEnabled: NSOnState];
276     }
277     else if( [o_protocol isEqualToString: @"UDP - multicast"] ) 
278     {
279         [o_net_server_addr setEnabled: NSOnState];
280         [o_net_server_addr_label setStringValue: @"Mult. addr."];
281         [o_net_server_port setEnabled: NSOnState];
282         [o_net_server_port setIntValue: 1234];
283         [o_net_server_pstepper setEnabled: NSOnState];
284     }
285     else if( [o_protocol isEqualToString: @"Channel server"] ) 
286     {
287         [o_net_server_addr setEnabled: NSOnState];
288         [o_net_server_addr_label setStringValue: @"Server"];
289         [o_net_server_addr setStringValue: @"vlcs"];
290         [o_net_server_port setEnabled: NSOnState];
291         [o_net_server_port setIntValue: 6010];
292         [o_net_server_pstepper setEnabled: NSOnState];
293     }
294     else if( [o_protocol isEqualToString: @"HTTP"] ) 
295     {
296         [o_net_server_addr setEnabled: NSOnState];
297         [o_net_server_addr_label setStringValue: @"URL"];
298         [o_net_server_addr setStringValue: @"http://"];
299         [o_net_server_port setEnabled: NSOffState];
300         [o_net_server_pstepper setEnabled: NSOffState];
301     }
302     [o_net_server_pstepper setIntValue: [o_net_server_port intValue]];
303 }
304
305 - (IBAction)panelCancel:(id)sender
306 {
307     [NSApp stopModalWithCode: 0];
308 }
309
310 - (IBAction)panelOk:(id)sender
311 {
312     [NSApp stopModalWithCode: 1];
313 }
314
315 @end