]> git.sesse.net Git - vlc/blob - modules/gui/macosx/intf.m
d7093f8c8e0a674aad8aad199b5233609395192f
[vlc] / modules / gui / macosx / intf.m
1 /*****************************************************************************
2  * intf.m: MacOS X interface plugin
3  *****************************************************************************
4  * Copyright (C) 2002-2003 VideoLAN
5  * $Id: intf.m,v 1.42 2003/01/31 02:53:52 jlj Exp $
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  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <sys/param.h>                                    /* for MAXPATHLEN */
31 #include <string.h>
32
33 #include "intf.h"
34 #include "vout.h"
35 #include "prefs.h"
36 #include "playlist.h"
37
38 /*****************************************************************************
39  * Local prototypes.
40  *****************************************************************************/
41 static void Run ( intf_thread_t *p_intf );
42
43 /*****************************************************************************
44  * OpenIntf: initialize interface
45  *****************************************************************************/
46 int E_(OpenIntf) ( vlc_object_t *p_this )
47 {   
48     intf_thread_t *p_intf = (intf_thread_t*) p_this;
49
50     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
51     if( p_intf->p_sys == NULL )
52     {
53         return( 1 );
54     }
55
56     memset( p_intf->p_sys, 0, sizeof( *p_intf->p_sys ) );
57
58     p_intf->p_sys->o_pool = [[NSAutoreleasePool alloc] init];
59     p_intf->p_sys->o_sendport = [[NSPort port] retain];
60
61     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
62
63     p_intf->pf_run = Run;
64
65     [[VLCApplication sharedApplication] autorelease];
66     [NSApp initIntlSupport];
67     [NSApp setIntf: p_intf];
68
69     [NSBundle loadNibNamed: @"MainMenu" owner: NSApp];
70
71     return( 0 );
72 }
73
74 /*****************************************************************************
75  * CloseIntf: destroy interface
76  *****************************************************************************/
77 void E_(CloseIntf) ( vlc_object_t *p_this )
78 {
79     intf_thread_t *p_intf = (intf_thread_t*) p_this;
80
81     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
82
83     config_SaveConfigFile( p_intf, MODULE_STRING );
84
85     [p_intf->p_sys->o_sendport release];
86     [p_intf->p_sys->o_pool release];
87
88     free( p_intf->p_sys );
89 }
90
91 /*****************************************************************************
92  * Run: main loop
93  *****************************************************************************/
94 static void Run( intf_thread_t *p_intf )
95 {
96     /* Do it again - for some unknown reason, vlc_thread_create() often
97      * fails to go to real-time priority with the first launched thread
98      * (???) --Meuuh */
99     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_LOW );
100
101     [NSApp run];
102 }
103
104 /*****************************************************************************
105  * VLCApplication implementation 
106  *****************************************************************************/
107 @implementation VLCApplication
108
109 - (id)init
110 {
111     /* default encoding: ISO-8859-1 */
112     i_encoding = NSISOLatin1StringEncoding;
113
114     return( [super init] );
115 }
116
117 - (void)initIntlSupport
118 {
119     char *psz_lang = getenv( "LANG" );
120
121     if( psz_lang == NULL )
122     {
123         return;
124     }
125
126     if( strncmp( psz_lang, "pl", 2 ) == 0 )
127     {
128         i_encoding = NSISOLatin2StringEncoding;
129     }
130     else if( strncmp( psz_lang, "ja", 2 ) == 0 ) 
131     {
132         i_encoding = NSJapaneseEUCStringEncoding;
133     }
134     else if( strncmp( psz_lang, "ru", 2 ) == 0 )
135     {
136 #define CFSENC2NSSENC(e) CFStringConvertEncodingToNSStringEncoding(e)
137         i_encoding = CFSENC2NSSENC( kCFStringEncodingKOI8_R ); 
138 #undef CFSENC2NSSENC
139     }
140 }
141
142 - (NSString *)localizedString:(char *)psz
143 {
144     NSString * o_str = nil;
145
146     if( psz != NULL )
147     {
148         UInt32 uiLength = (UInt32)strlen( psz );
149         NSData * o_data = [NSData dataWithBytes: psz length: uiLength];
150         o_str = [[[NSString alloc] initWithData: o_data
151                                        encoding: i_encoding] autorelease];
152     }
153
154     return( o_str );
155 }
156
157 - (void)setIntf:(intf_thread_t *)_p_intf
158 {
159     p_intf = _p_intf;
160 }
161
162 - (intf_thread_t *)getIntf
163 {
164     return( p_intf );
165 }
166
167 - (void)terminate:(id)sender
168 {
169     p_intf->p_vlc->b_die = VLC_TRUE;
170 }
171
172 @end
173
174 int ExecuteOnMainThread( id target, SEL sel, void * p_arg )
175 {
176     int i_ret = 0;
177
178     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
179
180     if( [target respondsToSelector: @selector(performSelectorOnMainThread:
181                                              withObject:waitUntilDone:)] )
182     {
183         [target performSelectorOnMainThread: sel
184                 withObject: [NSValue valueWithPointer: p_arg]
185                 waitUntilDone: YES];
186     }
187     else if( NSApp != nil && [NSApp respondsToSelector: @selector(getIntf)] ) 
188     {
189         NSValue * o_v1;
190         NSValue * o_v2;
191         NSArray * o_array;
192         NSPort * o_recv_port;
193         NSInvocation * o_inv;
194         NSPortMessage * o_msg;
195         intf_thread_t * p_intf;
196         NSConditionLock * o_lock;
197         NSMethodSignature * o_sig;
198
199         id * val[] = { &o_lock, &o_v2 };
200
201         p_intf = (intf_thread_t *)[NSApp getIntf];
202
203         o_recv_port = [[NSPort port] retain];
204         o_v1 = [NSValue valueWithPointer: val]; 
205         o_v2 = [NSValue valueWithPointer: p_arg];
206
207         o_sig = [target methodSignatureForSelector: sel];
208         o_inv = [NSInvocation invocationWithMethodSignature: o_sig];
209         [o_inv setArgument: &o_v1 atIndex: 2];
210         [o_inv setTarget: target];
211         [o_inv setSelector: sel];
212
213         o_array = [NSArray arrayWithObject:
214             [NSData dataWithBytes: &o_inv length: sizeof(o_inv)]];
215         o_msg = [[NSPortMessage alloc]
216             initWithSendPort: p_intf->p_sys->o_sendport
217             receivePort: o_recv_port components: o_array];
218
219         o_lock = [[NSConditionLock alloc] initWithCondition: 0];
220         [o_msg sendBeforeDate: [NSDate distantPast]];
221         [o_lock lockWhenCondition: 1];
222         [o_lock unlock];
223         [o_lock release];
224
225         [o_msg release];
226         [o_recv_port release];
227     } 
228     else
229     {
230         i_ret = 1;
231     }
232
233     [o_pool release];
234
235     return( i_ret );
236 }
237
238 /*****************************************************************************
239  * VLCMain implementation 
240  *****************************************************************************/
241 @implementation VLCMain
242
243 - (void)awakeFromNib
244 {
245     [o_window setTitle: _NS("VLC - Controller")];
246     [o_window setExcludedFromWindowsMenu: TRUE];
247
248     /* button controls */
249     [o_btn_playlist setToolTip: _NS("Playlist")];
250     [o_btn_prev setToolTip: _NS("Previous")];
251     [o_btn_slower setToolTip: _NS("Slower")];
252     [o_btn_play setToolTip: _NS("Play")];
253     [o_btn_stop setToolTip: _NS("Stop")];
254     [o_btn_faster setToolTip: _NS("Faster")];
255     [o_btn_next setToolTip: _NS("Next")];
256     [o_btn_prefs setToolTip: _NS("Preferences")];
257     [o_volumeslider setToolTip: _NS("Volume")];
258     [o_timeslider setToolTip: _NS("Position")];
259
260     /* messages panel */ 
261     [o_msgs_panel setDelegate: self];
262     [o_msgs_panel setTitle: _NS("Messages")];
263     [o_msgs_panel setExcludedFromWindowsMenu: TRUE];
264     [o_msgs_btn_ok setTitle: _NS("Close")];
265
266     /* main menu */
267     [o_mi_about setTitle: _NS("About VLC Media Player")];
268     [o_mi_prefs setTitle: _NS("Preferences")];
269     [o_mi_hide setTitle: _NS("Hide VLC")];
270     [o_mi_hide_others setTitle: _NS("Hide Others")];
271     [o_mi_show_all setTitle: _NS("Show All")];
272     [o_mi_quit setTitle: _NS("Quit VLC")];
273
274     [o_mu_file setTitle: _NS("File")];
275     [o_mi_open_generic setTitle: _NS("Open...")];
276     [o_mi_open_file setTitle: _NS("Open File...")];
277     [o_mi_open_disc setTitle: _NS("Open Disc...")];
278     [o_mi_open_net setTitle: _NS("Open Network...")];
279     [o_mi_open_recent setTitle: _NS("Open Recent")];
280     [o_mi_open_recent_cm setTitle: _NS("Clear Menu")];
281
282     [o_mu_edit setTitle: _NS("Edit")];
283     [o_mi_cut setTitle: _NS("Cut")];
284     [o_mi_copy setTitle: _NS("Copy")];
285     [o_mi_paste setTitle: _NS("Paste")];
286     [o_mi_clear setTitle: _NS("Clear")];
287     [o_mi_select_all setTitle: _NS("Select All")];
288
289     [o_mu_controls setTitle: _NS("Controls")];
290     [o_mi_play setTitle: _NS("Play")];
291     [o_mi_stop setTitle: _NS("Stop")];
292     [o_mi_faster setTitle: _NS("Faster")];
293     [o_mi_slower setTitle: _NS("Slower")];
294     [o_mi_previous setTitle: _NS("Previous")];
295     [o_mi_next setTitle: _NS("Next")];
296     [o_mi_loop setTitle: _NS("Loop")];
297     [o_mi_program setTitle: _NS("Program")];
298     [o_mi_title setTitle: _NS("Title")];
299     [o_mi_chapter setTitle: _NS("Chapter")];
300     [o_mi_language setTitle: _NS("Language")];
301     [o_mi_subtitle setTitle: _NS("Subtitles")];
302     
303     [o_mu_audio setTitle: _NS("Audio")];
304     [o_mi_vol_up setTitle: _NS("Volume Up")];
305     [o_mi_vol_down setTitle: _NS("Volume Down")];
306     [o_mi_mute setTitle: _NS("Mute")];
307     [o_mi_channels setTitle: _NS("Channels")];
308     [o_mi_device setTitle: _NS("Device")];
309     
310     [o_mu_video setTitle: _NS("Video")];
311     [o_mi_fullscreen setTitle: _NS("Fullscreen")];
312     [o_mi_screen setTitle: _NS("Screen")];
313     [o_mi_deinterlace setTitle: _NS("Deinterlace")];
314
315     [o_mu_window setTitle: _NS("Window")];
316     [o_mi_minimize setTitle: _NS("Minimize Window")];
317     [o_mi_close_window setTitle: _NS("Close Window")];
318     [o_mi_controller setTitle: _NS("Controller")];
319     [o_mi_playlist setTitle: _NS("Playlist")];
320     [o_mi_messages setTitle: _NS("Messages")];
321
322     [o_mi_bring_atf setTitle: _NS("Bring All to Front")];
323
324     [o_mu_help setTitle: _NS("Help")];
325     [o_mi_readme setTitle: _NS("ReadMe...")];
326     [o_mi_reportabug setTitle: _NS("Report A Bug")];
327     [o_mi_website setTitle: _NS("VideoLAN Website")];
328     [o_mi_license setTitle: _NS("License")];
329
330     /* dock menu */
331     [o_dmi_play setTitle: _NS("Play")];
332     [o_dmi_stop setTitle: _NS("Stop")];
333
334     /* error panel */
335     [o_error setTitle: _NS("Error")];
336     [o_err_lbl setStringValue: _NS("An error has occurred which probably prevented the execution of your request:")];
337     [o_err_bug_lbl setStringValue: _NS("If you believe that it is a bug, please follow the instructions at:")]; 
338     [o_err_btn_msgs setTitle: _NS("Open Messages Window")];
339     [o_err_btn_dismiss setTitle: _NS("Dismiss")];
340
341     [self setSubmenusEnabled: FALSE];
342     [self manageVolumeSlider];
343 }
344
345 - (void)applicationWillFinishLaunching:(NSNotification *)o_notification
346 {
347     intf_thread_t * p_intf = [NSApp getIntf];
348
349     o_msg_lock = [[NSLock alloc] init];
350     o_msg_arr = [[NSMutableArray arrayWithCapacity: 200] retain];
351
352     o_img_play = [[NSImage imageNamed: @"play"] retain];
353     o_img_pause = [[NSImage imageNamed: @"pause"] retain];
354
355     [p_intf->p_sys->o_sendport setDelegate: self];
356     [[NSRunLoop currentRunLoop] 
357         addPort: p_intf->p_sys->o_sendport
358         forMode: NSDefaultRunLoopMode];
359
360     [NSTimer scheduledTimerWithTimeInterval: 0.1
361         target: self selector: @selector(manageIntf:)
362         userInfo: nil repeats: TRUE];
363
364     [NSThread detachNewThreadSelector: @selector(manage)
365         toTarget: self withObject: nil];
366 }
367
368 - (BOOL)application:(NSApplication *)o_app openFile:(NSString *)o_filename
369 {
370     [o_playlist appendArray:
371         [NSArray arrayWithObject: o_filename] atPos: -1 enqueue: NO];
372
373     return( TRUE );
374 }
375
376 - (void)manage
377 {
378     NSDate * o_sleep_date;
379     intf_thread_t * p_intf = [NSApp getIntf];
380     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
381
382     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_LOW );
383
384     while( !p_intf->b_die )
385     {
386         playlist_t * p_playlist;
387
388         vlc_mutex_lock( &p_intf->change_lock );
389
390         p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, 
391                                               FIND_ANYWHERE );
392
393         if( p_playlist != NULL )
394         {
395             vlc_mutex_lock( &p_playlist->object_lock );
396
397             [self manage: p_playlist];
398
399             vlc_mutex_unlock( &p_playlist->object_lock );
400             vlc_object_release( p_playlist );
401         }
402
403         vlc_mutex_unlock( &p_intf->change_lock );
404
405         o_sleep_date = [NSDate dateWithTimeIntervalSinceNow: 0.1];
406         [NSThread sleepUntilDate: o_sleep_date];
407     }
408
409     [self terminate];
410
411     [o_pool release];
412 }
413
414 - (void)manage:(playlist_t *)p_playlist
415 {
416     intf_thread_t * p_intf = [NSApp getIntf];
417
418 #define p_input p_playlist->p_input
419
420     if( p_input )
421     {
422         vout_thread_t   * p_vout  = NULL;
423         aout_instance_t * p_aout  = NULL; 
424         vlc_bool_t b_need_menus = VLC_FALSE;
425
426         vlc_mutex_lock( &p_input->stream.stream_lock );
427
428         if( !p_input->b_die )
429         {
430             /* New input or stream map change */
431             if( p_input->stream.b_changed )
432             {
433                 p_intf->p_sys->b_playing = 1;
434                 [self manageMode: p_playlist];
435                 b_need_menus = VLC_TRUE;
436             }
437
438             if( p_intf->p_sys->i_part !=
439                 p_input->stream.p_selected_area->i_part )
440             {
441                 p_intf->p_sys->b_chapter_update = 1;
442                 b_need_menus = VLC_TRUE;
443             }
444
445             p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT,
446                                               FIND_ANYWHERE );
447             if( p_aout != NULL )
448             {
449                 vlc_value_t val;
450                 audio_volume_t i_volume;
451
452                 if( var_Get( (vlc_object_t *)p_aout, "intf-change", &val )
453                     >= 0 && val.b_bool )
454                 {
455                     p_intf->p_sys->b_aout_update = 1;
456                     b_need_menus = VLC_TRUE;
457                 }
458
459                 aout_VolumeGet( p_aout, &i_volume );
460                 vlc_object_release( (vlc_object_t *)p_aout );
461
462                 p_intf->p_sys->b_mute = ( i_volume == 0 );
463             }
464
465             p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
466                                               FIND_ANYWHERE );
467             if( p_vout != NULL )
468             {
469                 vlc_value_t val;
470
471                 if( var_Get( (vlc_object_t *)p_vout, "intf-change", &val )
472                     >= 0 && val.b_bool )
473                 {
474                     p_intf->p_sys->b_vout_update = 1;
475                     b_need_menus = VLC_TRUE;
476                 }
477
478                 vlc_object_release( (vlc_object_t *)p_vout );
479             } 
480
481             if( b_need_menus )
482             {
483                 [self setupMenus: p_input];
484             }
485         }
486
487         vlc_mutex_unlock( &p_input->stream.stream_lock );
488     }
489     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
490     {
491         p_intf->p_sys->b_playing = 0;
492         [self manageMode: p_playlist];
493     }
494
495 #undef p_input
496 }
497
498 - (void)manageMode:(playlist_t *)p_playlist
499 {
500     intf_thread_t * p_intf = [NSApp getIntf];
501
502     if( p_playlist->p_input != NULL )
503     {
504         /* get ready for menu regeneration */
505         p_intf->p_sys->b_program_update = 1;
506         p_intf->p_sys->b_title_update = 1;
507         p_intf->p_sys->b_chapter_update = 1;
508         p_intf->p_sys->b_audio_update = 1;
509         p_intf->p_sys->b_spu_update = 1;
510         p_intf->p_sys->i_part = 0;
511
512         p_playlist->p_input->stream.b_changed = 0;
513         msg_Dbg( p_intf, "stream has changed, refreshing interface" );
514     }
515     else
516     {
517         vout_thread_t * p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
518                                                           FIND_ANYWHERE );
519         if( p_vout != NULL )
520         {
521             vlc_object_detach( p_vout );
522             vlc_object_release( p_vout );
523
524             vlc_mutex_unlock( &p_playlist->object_lock );
525             vout_Destroy( p_vout );
526             vlc_mutex_lock( &p_playlist->object_lock );
527         }
528
529         aout_instance_t * p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT,
530                                                             FIND_ANYWHERE );
531         if( p_aout != NULL )
532         {
533             vlc_object_detach( (vlc_object_t *)p_aout );
534             vlc_object_release( (vlc_object_t *)p_aout );
535             aout_Delete( p_aout ); 
536         }
537     }
538
539     p_intf->p_sys->b_intf_update = VLC_TRUE;
540 }
541
542 - (void)manageIntf:(NSTimer *)o_timer
543 {
544     intf_thread_t * p_intf = [NSApp getIntf];
545
546     if( p_intf->p_vlc->b_die == VLC_TRUE )
547     {
548         [o_timer invalidate];
549         return;
550     }
551
552     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
553                                                        FIND_ANYWHERE );
554
555     if( p_playlist == NULL )
556     {
557         return;
558     }
559
560     vlc_mutex_lock( &p_playlist->object_lock );
561
562 #define p_input p_playlist->p_input
563
564     if( p_input != NULL )
565     {
566         vlc_mutex_lock( &p_input->stream.stream_lock );
567     }
568
569     if( p_intf->p_sys->b_intf_update )
570     {
571         vlc_bool_t b_input = VLC_FALSE;
572         vlc_bool_t b_plmul = VLC_FALSE;
573         vlc_bool_t b_control = VLC_FALSE;
574         vlc_bool_t b_seekable = VLC_FALSE;
575         vlc_bool_t b_chapters = VLC_FALSE;
576
577         b_plmul = p_playlist->i_size > 1;
578
579         if( ( b_input = ( p_input != NULL ) ) )
580         {
581             /* seekable streams */
582             b_seekable = p_input->stream.b_seekable;
583
584             /* control buttons for free pace streams */
585             b_control = p_input->stream.b_pace_control; 
586
587             /* chapters */
588             b_chapters = p_input->stream.p_selected_area->i_part_nb > 1; 
589
590             /* play status */
591             p_intf->p_sys->b_play_status = 
592                 p_input->stream.control.i_status != PAUSE_S;
593         }
594         else
595         {
596             /* play status */
597             p_intf->p_sys->b_play_status = VLC_FALSE;
598
599             [self setSubmenusEnabled: FALSE];
600         }
601
602         [self playStatusUpdated: p_intf->p_sys->b_play_status];
603
604         [o_btn_stop setEnabled: b_input];
605         [o_btn_faster setEnabled: b_control];
606         [o_btn_slower setEnabled: b_control];
607         [o_btn_prev setEnabled: (b_plmul || b_chapters)];
608         [o_btn_next setEnabled: (b_plmul || b_chapters)];
609
610         [o_timeslider setFloatValue: 0.0];
611         [o_timeslider setEnabled: b_seekable];
612         [o_timefield setStringValue: @"0:00:00"];
613
614         [self manageVolumeSlider];
615
616         p_intf->p_sys->b_intf_update = VLC_FALSE;
617     }
618
619 #define p_area p_input->stream.p_selected_area
620
621     if( p_intf->p_sys->b_playing && p_input != NULL )
622     {
623         vlc_bool_t b_field_update = VLC_TRUE;
624
625         if( !p_input->b_die && ( p_intf->p_sys->b_play_status !=
626             ( p_input->stream.control.i_status != PAUSE_S ) ) ) 
627         {
628             p_intf->p_sys->b_play_status =
629                 !p_intf->p_sys->b_play_status;
630
631             [self playStatusUpdated: p_intf->p_sys->b_play_status]; 
632         }
633
634         if( p_input->stream.b_seekable )
635         {
636             if( f_slider == f_slider_old )
637             {
638                 float f_updated = ( 100. * p_area->i_tell ) /
639                                            p_area->i_size;
640
641                 if( f_slider != f_updated )
642                 {
643                     [o_timeslider setFloatValue: f_updated];
644                 }
645             }
646             else
647             {
648                 off_t i_seek = ( f_slider * p_area->i_size ) / 100;
649
650                 /* release the lock to be able to seek */
651                 vlc_mutex_unlock( &p_input->stream.stream_lock );
652                 input_Seek( p_input, i_seek, INPUT_SEEK_SET );
653                 vlc_mutex_lock( &p_input->stream.stream_lock );
654
655                 /* update the old value */
656                 f_slider_old = f_slider; 
657
658                 b_field_update = VLC_FALSE;
659             }
660         }
661
662         if( b_field_update )
663         {
664             NSString * o_time;
665             char psz_time[ OFFSETTOTIME_MAX_SIZE ];
666
667             input_OffsetToTime( p_input, psz_time, p_area->i_tell );
668
669             o_time = [NSString stringWithCString: psz_time];
670             [o_timefield setStringValue: o_time];
671         }
672
673         /* disable screen saver */
674         UpdateSystemActivity( UsrActivity );
675     }
676
677 #undef p_area
678
679     if( p_input != NULL )
680     {
681         vlc_mutex_unlock( &p_input->stream.stream_lock );
682     }
683
684 #undef p_input
685
686     vlc_mutex_unlock( &p_playlist->object_lock );
687     vlc_object_release( p_playlist );
688
689     [self updateMessageArray];
690 }
691
692 - (void)updateMessageArray
693 {
694     int i_start, i_stop;
695     intf_thread_t * p_intf = [NSApp getIntf];
696
697     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
698     i_stop = *p_intf->p_sys->p_sub->pi_stop;
699     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
700
701     if( p_intf->p_sys->p_sub->i_start != i_stop )
702     {
703         NSColor *o_white = [NSColor whiteColor];
704         NSColor *o_red = [NSColor redColor];
705         NSColor *o_yellow = [NSColor yellowColor];
706         NSColor *o_gray = [NSColor grayColor];
707
708         NSColor * pp_color[4] = { o_white, o_red, o_yellow, o_gray };
709         static const char * ppsz_type[4] = { ": ", " error: ",
710                                              " warning: ", " debug: " };
711
712         for( i_start = p_intf->p_sys->p_sub->i_start;
713              i_start != i_stop;
714              i_start = (i_start+1) % VLC_MSG_QSIZE )
715         {
716             NSString *o_msg;
717             NSDictionary *o_attr;
718             NSAttributedString *o_msg_color;
719
720             int i_type = p_intf->p_sys->p_sub->p_msg[i_start].i_type;
721
722             [o_msg_lock lock];
723
724             if( [o_msg_arr count] + 2 > 200 )
725             {
726                 unsigned rid[] = { 0, 1 };
727                 [o_msg_arr removeObjectsFromIndices: (unsigned *)&rid
728                            numIndices: sizeof(rid)/sizeof(rid[0])];
729             }
730
731             o_attr = [NSDictionary dictionaryWithObject: o_gray
732                 forKey: NSForegroundColorAttributeName];
733             o_msg = [NSString stringWithFormat: @"%s%s",
734                 p_intf->p_sys->p_sub->p_msg[i_start].psz_module,
735                 ppsz_type[i_type]];
736             o_msg_color = [[NSAttributedString alloc]
737                 initWithString: o_msg attributes: o_attr];
738             [o_msg_arr addObject: [o_msg_color autorelease]];
739
740             o_attr = [NSDictionary dictionaryWithObject: pp_color[i_type]
741                 forKey: NSForegroundColorAttributeName];
742             o_msg = [NSString stringWithFormat: @"%s\n",
743                 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg];
744             o_msg_color = [[NSAttributedString alloc]
745                 initWithString: o_msg attributes: o_attr];
746             [o_msg_arr addObject: [o_msg_color autorelease]];
747
748             [o_msg_lock unlock];
749
750             if( i_type == 1 )
751             {
752                 NSString *o_my_msg = [NSString stringWithFormat: @"%s: %s\n",
753                     p_intf->p_sys->p_sub->p_msg[i_start].psz_module,
754                     p_intf->p_sys->p_sub->p_msg[i_start].psz_msg];
755
756                 NSRange s_r = NSMakeRange( [[o_err_msg string] length], 0 );
757                 [o_err_msg setEditable: YES];
758                 [o_err_msg setSelectedRange: s_r];
759                 [o_err_msg insertText: o_my_msg];
760
761                 [o_error makeKeyAndOrderFront: self];
762                 [o_err_msg setEditable: NO];
763             }
764         }
765
766         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
767         p_intf->p_sys->p_sub->i_start = i_start;
768         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
769     }
770 }
771
772 - (void)playStatusUpdated:(BOOL)b_pause
773 {
774     if( b_pause )
775     {
776         [o_btn_play setImage: o_img_pause];
777         [o_btn_play setToolTip: _NS("Pause")];
778         [o_mi_play setTitle: _NS("Pause")];
779         [o_dmi_play setTitle: _NS("Pause")];
780     }
781     else
782     {
783         [o_btn_play setImage: o_img_play];
784         [o_btn_play setToolTip: _NS("Play")];
785         [o_mi_play setTitle: _NS("Play")];
786         [o_dmi_play setTitle: _NS("Play")];
787     }
788 }
789
790 - (void)setSubmenusEnabled:(BOOL)b_enabled
791 {
792     [o_mi_program setEnabled: b_enabled];
793     [o_mi_title setEnabled: b_enabled];
794     [o_mi_chapter setEnabled: b_enabled];
795     [o_mi_language setEnabled: b_enabled];
796     [o_mi_subtitle setEnabled: b_enabled];
797     [o_mi_channels setEnabled: b_enabled];
798     [o_mi_device setEnabled: b_enabled];
799     [o_mi_screen setEnabled: b_enabled];
800 }
801
802 - (void)manageVolumeSlider
803 {
804     audio_volume_t i_volume;
805     vlc_bool_t b_audio = VLC_FALSE;
806     intf_thread_t * p_intf = [NSApp getIntf];
807
808     aout_instance_t * p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT, 
809                                                         FIND_ANYWHERE );
810     if( p_aout != NULL )
811     {
812         b_audio = VLC_TRUE;
813
814         aout_VolumeGet( p_aout, &i_volume );
815         vlc_object_release( (vlc_object_t *)p_aout );
816     }
817     else
818     {
819         i_volume = (audio_volume_t)config_GetInt( p_intf, "volume" );
820     }
821
822     [o_volumeslider setFloatValue: (float)i_volume / AOUT_VOLUME_STEP]; 
823     [o_volumeslider setEnabled: b_audio];
824
825     p_intf->p_sys->b_mute = ( i_volume == 0 );
826 }
827
828 - (void)terminate
829 {
830     NSEvent * o_event;
831     vout_thread_t * p_vout;
832     playlist_t * p_playlist;
833     intf_thread_t * p_intf = [NSApp getIntf];
834
835     /*
836      * Free playlists
837      */
838     msg_Dbg( p_intf, "removing all playlists" );
839     while( (p_playlist = vlc_object_find( p_intf->p_vlc, VLC_OBJECT_PLAYLIST,
840                                           FIND_CHILD )) )
841     {
842         vlc_object_detach( p_playlist );
843         vlc_object_release( p_playlist );
844         playlist_Destroy( p_playlist );
845     }
846
847     /*
848      * Free video outputs
849      */
850     msg_Dbg( p_intf, "removing all video outputs" );
851     while( (p_vout = vlc_object_find( p_intf->p_vlc, 
852                                       VLC_OBJECT_VOUT, FIND_CHILD )) )
853     {
854         vlc_object_detach( p_vout );
855         vlc_object_release( p_vout );
856         vout_Destroy( p_vout );
857     }
858
859     if( o_img_pause != nil )
860     {
861         [o_img_pause release];
862         o_img_pause = nil;
863     }
864
865     if( o_img_play != nil )
866     {
867         [o_img_play release];
868         o_img_play = nil;
869     }
870
871     if( o_msg_arr != nil )
872     {
873         [o_msg_arr removeAllObjects];
874         [o_msg_arr release];
875         o_msg_arr = nil;
876     }
877
878     if( o_msg_lock != nil )
879     {
880         [o_msg_lock release];
881         o_msg_lock = nil;
882     }
883
884     if( o_prefs != nil )
885     {
886         [o_prefs release];
887         o_prefs = nil;
888     }
889
890     [NSApp stop: nil];
891
892     /* write cached user defaults to disk */
893     [[NSUserDefaults standardUserDefaults] synchronize];
894
895     /* send a dummy event to break out of the event loop */
896     o_event = [NSEvent mouseEventWithType: NSLeftMouseDown
897                 location: NSMakePoint( 1, 1 ) modifierFlags: 0
898                 timestamp: 1 windowNumber: [[NSApp mainWindow] windowNumber]
899                 context: [NSGraphicsContext currentContext] eventNumber: 1
900                 clickCount: 1 pressure: 0.0];
901     [NSApp postEvent: o_event atStart: YES];
902 }
903
904 - (void)setupMenus:(input_thread_t *)p_input
905 {
906     unsigned int i, i_nb_items;
907     NSMenuItem * o_item;
908     NSString * o_menu_title;
909     char psz_title[ 256 ];
910
911     es_descriptor_t * p_audio_es = NULL;
912     es_descriptor_t * p_spu_es = NULL;
913
914     intf_thread_t * p_intf = [NSApp getIntf];
915
916     p_intf->p_sys->b_chapter_update |= p_intf->p_sys->b_title_update;
917     p_intf->p_sys->b_audio_update |= p_intf->p_sys->b_title_update |
918                                      p_intf->p_sys->b_program_update;
919     p_intf->p_sys->b_spu_update |= p_intf->p_sys->b_title_update |
920                                    p_intf->p_sys->b_program_update;
921
922     if( p_intf->p_sys->b_program_update )
923     {
924         NSMenu * o_program;
925         SEL pf_toggle_program;
926         pgrm_descriptor_t * p_pgrm;
927
928         if( p_input->stream.p_new_program )
929         {
930             p_pgrm = p_input->stream.p_new_program;
931         }
932         else
933         {
934             p_pgrm = p_input->stream.p_selected_program;
935         }
936
937         o_program = [o_mi_program submenu];
938         pf_toggle_program = @selector(toggleProgram:);
939
940         /* remove previous program items */
941         i_nb_items = [o_program numberOfItems];
942         for( i = 0; i < i_nb_items; i++ )
943         {
944             [o_program removeItemAtIndex: 0];
945         }
946
947         /* make (un)sensitive */
948         [o_mi_program setEnabled: 
949             p_input->stream.i_pgrm_number > 1];
950
951         /* add program items */
952         for( i = 0 ; i < p_input->stream.i_pgrm_number ; i++ )
953         {
954             snprintf( psz_title, sizeof(psz_title), "id %d",
955                 p_input->stream.pp_programs[i]->i_number );
956             psz_title[sizeof(psz_title) - 1] = '\0';
957
958             o_menu_title = [NSString stringWithCString: psz_title];
959
960             o_item = [o_program addItemWithTitle: o_menu_title
961                 action: pf_toggle_program keyEquivalent: @""];
962             [o_item setTag: p_input->stream.pp_programs[i]->i_number];
963             [o_item setTarget: o_controls];
964
965             if( p_pgrm == p_input->stream.pp_programs[i] )
966             {
967                 [o_item setState: NSOnState];
968             }
969         }
970
971         p_intf->p_sys->b_program_update = 0;
972     }
973
974     if( p_intf->p_sys->b_title_update )
975     {
976         NSMenu * o_title;
977         SEL pf_toggle_title;
978
979         o_title = [o_mi_title submenu];
980         pf_toggle_title = @selector(toggleTitle:);
981
982         /* remove previous title items */
983         i_nb_items = [o_title numberOfItems];
984         for( i = 0; i < i_nb_items; i++ )
985         {
986             [o_title removeItemAtIndex: 0];
987         }
988
989         /* make (un)sensitive */
990         [o_mi_title setEnabled: 
991             p_input->stream.i_area_nb > 1];
992
993         /* add title items */
994         for( i = 1 ; i < p_input->stream.i_area_nb ; i++ )
995         {
996             snprintf( psz_title, sizeof(psz_title), "Title %d (%d)", i,
997                 p_input->stream.pp_areas[i]->i_part_nb );
998             psz_title[sizeof(psz_title) - 1] = '\0';
999
1000             o_menu_title = [NSString stringWithCString: psz_title];
1001
1002             o_item = [o_title addItemWithTitle: o_menu_title
1003                 action: pf_toggle_title keyEquivalent: @""];
1004             [o_item setTag: i];
1005             [o_item setTarget: o_controls];
1006
1007             if( ( p_input->stream.pp_areas[i] ==
1008                 p_input->stream.p_selected_area ) )
1009             {
1010                 [o_item setState: NSOnState];
1011             }
1012         }
1013
1014         p_intf->p_sys->b_title_update = 0;
1015     }
1016
1017     if( p_intf->p_sys->b_chapter_update )
1018     {
1019         NSMenu * o_chapter;
1020         SEL pf_toggle_chapter;
1021
1022         o_chapter = [o_mi_chapter submenu];
1023         pf_toggle_chapter = @selector(toggleChapter:);
1024
1025         /* remove previous chapter items */
1026         i_nb_items = [o_chapter numberOfItems];
1027         for( i = 0; i < i_nb_items; i++ )
1028         {
1029             [o_chapter removeItemAtIndex: 0];
1030         }
1031
1032         /* make (un)sensitive */
1033         [o_mi_chapter setEnabled: 
1034             p_input->stream.p_selected_area->i_part_nb > 1];
1035
1036         /* add chapter items */
1037         for( i = 0 ; i < p_input->stream.p_selected_area->i_part_nb ; i++ )
1038         {
1039             snprintf( psz_title, sizeof(psz_title), "Chapter %d", i + 1 );
1040             psz_title[sizeof(psz_title) - 1] = '\0';
1041
1042             o_menu_title = [NSString stringWithCString: psz_title];
1043
1044             o_item = [o_chapter addItemWithTitle: o_menu_title
1045                 action: pf_toggle_chapter keyEquivalent: @""];
1046             [o_item setTag: i + 1];
1047             [o_item setTarget: o_controls];
1048
1049             if( ( p_input->stream.p_selected_area->i_part == i + 1 ) )
1050             {
1051                 [o_item setState: NSOnState];
1052             }
1053         }
1054
1055         p_intf->p_sys->i_part =
1056                 p_input->stream.p_selected_area->i_part;
1057
1058         p_intf->p_sys->b_chapter_update = 0;
1059     }
1060
1061     for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
1062     {
1063         if( p_input->stream.pp_selected_es[i]->i_cat == AUDIO_ES )
1064         {
1065             p_audio_es = p_input->stream.pp_selected_es[i];
1066         }
1067         else if( p_input->stream.pp_selected_es[i]->i_cat == SPU_ES )
1068         {
1069             p_spu_es = p_input->stream.pp_selected_es[i];
1070         }
1071     }
1072
1073     if( p_intf->p_sys->b_audio_update )
1074     {
1075         [self setupLangMenu: p_input mi: o_mi_language es: p_audio_es
1076             category: AUDIO_ES selector: @selector(toggleLanguage:)];
1077
1078         p_intf->p_sys->b_audio_update = 0;
1079     }
1080
1081     if( p_intf->p_sys->b_spu_update )
1082     {
1083         [self setupLangMenu: p_input mi: o_mi_subtitle es: p_spu_es
1084             category: SPU_ES selector: @selector(toggleLanguage:)];
1085
1086         p_intf->p_sys->b_spu_update = 0;
1087     }
1088
1089     if ( p_intf->p_sys->b_aout_update )
1090     {
1091         aout_instance_t * p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT,
1092                                                     FIND_ANYWHERE );
1093
1094         if ( p_aout != NULL )
1095         {
1096             vlc_value_t val;
1097             val.b_bool = 0;
1098
1099             var_Set( (vlc_object_t *)p_aout, "intf-change", val );
1100
1101             [self setupVarMenu: o_mi_channels target: (vlc_object_t *)p_aout
1102                 var: "audio-channels" selector: @selector(toggleVar:)];
1103
1104             [self setupVarMenu: o_mi_device target: (vlc_object_t *)p_aout
1105                 var: "audio-device" selector: @selector(toggleVar:)];
1106
1107             vlc_object_release( (vlc_object_t *)p_aout );
1108         }
1109
1110         p_intf->p_sys->b_aout_update = 0;
1111     }
1112
1113     if( p_intf->p_sys->b_vout_update )
1114     {
1115         vout_thread_t * p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
1116                                                           FIND_ANYWHERE );
1117
1118         if ( p_vout != NULL )
1119         {
1120             vlc_value_t val;
1121             val.b_bool = 0;
1122
1123             var_Set( (vlc_object_t *)p_vout, "intf-change", val );
1124
1125             [self setupVarMenu: o_mi_screen target: (vlc_object_t *)p_vout
1126                 var: "video-device" selector: @selector(toggleVar:)];
1127
1128             vlc_object_release( (vlc_object_t *)p_vout );
1129
1130             [o_mi_close_window setEnabled: TRUE];
1131         }
1132
1133         p_intf->p_sys->b_vout_update = 0;
1134     }
1135
1136 #undef p_input
1137 }
1138
1139 - (void)setupLangMenu:(input_thread_t *)p_input
1140                       mi:(NSMenuItem *)o_mi
1141                       es:(es_descriptor_t *)p_es
1142                       category:(int)i_cat
1143                       selector:(SEL)pf_callback
1144 {
1145     unsigned int i, i_nb_items;
1146     NSMenu * o_menu = [o_mi submenu];
1147
1148     /* remove previous language items */
1149     i_nb_items = [o_menu numberOfItems];
1150     for( i = 0; i < i_nb_items; i++ )
1151     {
1152         [o_menu removeItemAtIndex: 0];
1153     }
1154
1155     /* make sensitive : we can't change it after we build the menu, and
1156      * before, we don't yet how many items we will have. So make it
1157      * always sensitive. --Meuuh */
1158     [o_mi setEnabled: TRUE];
1159
1160 #if 0
1161     /* We do not use this code, because you need to start stop .avi for
1162      * it to work, so not very useful now  --hartman */
1163     if ( o_mi == o_mi_subtitle ) {
1164         NSLog(@"testing");
1165         [o_mi setEnabled: TRUE ];
1166         NSMenuItem * o_lmi;
1167         NSString * o_title;
1168         o_title = _NS("Load from file..");
1169         o_lmi = [o_menu addItemWithTitle: o_title
1170                  action: pf_callback keyEquivalent: @""];
1171         [o_lmi setTag: 2000];
1172         [o_lmi setTarget: o_controls];
1173     }
1174 #endif
1175
1176 #define ES p_input->stream.pp_es[i]
1177     for( i = 0 ; i < p_input->stream.i_es_number ; i++ )
1178     {
1179         if( ( ES->i_cat == i_cat ) &&
1180             ( !ES->p_pgrm ||
1181               ES->p_pgrm ==
1182                  p_input->stream.p_selected_program ) )
1183         {
1184             NSMenuItem * o_lmi;
1185             NSString * o_title;
1186
1187             if( *ES->psz_desc )
1188             {
1189                 o_title = [NSString stringWithCString: ES->psz_desc];
1190             }
1191             else
1192             {
1193                 char psz_title[ 256 ];
1194
1195                 snprintf( psz_title, sizeof(psz_title), "Language 0x%x",
1196                           ES->i_id );
1197                 psz_title[sizeof(psz_title) - 1] = '\0';
1198
1199                 o_title = [NSString stringWithCString: psz_title];
1200             }
1201
1202             o_lmi = [o_menu addItemWithTitle: o_title
1203                 action: pf_callback keyEquivalent: @""];
1204             [o_lmi setRepresentedObject: 
1205                 [NSValue valueWithPointer: ES]];
1206             [o_lmi setTarget: o_controls];
1207             [o_lmi setTag: i_cat];
1208
1209             if( /*p_es == ES*/ ES->p_decoder_fifo != NULL )
1210             {
1211                 [o_lmi setState: NSOnState];
1212             }
1213         }
1214     }
1215 #undef ES
1216 }
1217
1218 - (void)setupVarMenu:(NSMenuItem *)o_mi
1219                      target:(vlc_object_t *)p_object
1220                      var:(const char *)psz_variable
1221                      selector:(SEL)pf_callback
1222 {
1223     int i, i_nb_items;
1224     NSMenu * o_menu = [o_mi submenu];
1225     vlc_value_t val;
1226     char * psz_value;
1227
1228     /* remove previous items */
1229     i_nb_items = [o_menu numberOfItems];
1230     for( i = 0; i < i_nb_items; i++ )
1231     {
1232         [o_menu removeItemAtIndex: 0];
1233     }
1234
1235     if ( var_Get( p_object, psz_variable, &val ) < 0 )
1236     {
1237         return;
1238     }
1239     psz_value = val.psz_string;
1240
1241     if ( var_Change( p_object, psz_variable,
1242                      VLC_VAR_GETLIST, &val ) < 0 )
1243     {
1244         free( psz_value );
1245         return;
1246     }
1247
1248     /* make (un)sensitive */
1249     [o_mi setEnabled: ( val.p_list->i_count > 0 )];
1250
1251     for ( i = 0; i < val.p_list->i_count; i++ )
1252     {
1253         NSMenuItem * o_lmi;
1254         NSString * o_title;
1255
1256         o_title = [NSString stringWithCString: 
1257             val.p_list->p_values[i].psz_string];
1258         o_lmi = [o_menu addItemWithTitle: o_title
1259                  action: pf_callback keyEquivalent: @""];
1260         /* FIXME: this isn't 64-bit clean ! */
1261         [o_lmi setTag: (int)psz_variable];
1262         [o_lmi setRepresentedObject:
1263             [NSValue valueWithPointer: p_object]];
1264         [o_lmi setTarget: o_controls];
1265
1266         if ( !strcmp( psz_value, val.p_list->p_values[i].psz_string ) )
1267             [o_lmi setState: NSOnState];
1268     }
1269
1270     var_Change( p_object, psz_variable, VLC_VAR_FREELIST,
1271                 &val );
1272
1273     free( psz_value );
1274 }
1275
1276 - (IBAction)clearRecentItems:(id)sender
1277 {
1278     [[NSDocumentController sharedDocumentController]
1279                           clearRecentDocuments: nil];
1280 }
1281
1282 - (void)openRecentItem:(id)sender
1283 {
1284     [self application: nil openFile: [sender title]]; 
1285 }
1286
1287 - (IBAction)viewPreferences:(id)sender
1288 {
1289     if( o_prefs == nil )
1290     {
1291         o_prefs = [[VLCPrefs alloc] init];
1292     }
1293
1294     [o_prefs createPrefPanel: @"main"];
1295 }
1296
1297 - (IBAction)timesliderUpdate:(id)sender
1298 {
1299     float f_updated;
1300
1301     switch( [[NSApp currentEvent] type] )
1302     {
1303         case NSLeftMouseUp:
1304         case NSLeftMouseDown:
1305             f_slider = [sender floatValue];
1306             return;
1307
1308         case NSLeftMouseDragged:
1309             f_updated = [sender floatValue];
1310             break;
1311
1312         default:
1313             return;
1314     }
1315
1316     intf_thread_t * p_intf = [NSApp getIntf];
1317
1318     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1319                                                        FIND_ANYWHERE );
1320
1321     if( p_playlist == NULL )
1322     {
1323         return;
1324     }
1325
1326     vlc_mutex_lock( &p_playlist->object_lock );
1327
1328     if( p_playlist->p_input != NULL )
1329     {
1330         off_t i_tell;
1331         NSString * o_time;
1332         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
1333
1334 #define p_area p_playlist->p_input->stream.p_selected_area
1335         vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
1336         i_tell = f_updated / 100. * p_area->i_size;
1337         input_OffsetToTime( p_playlist->p_input, psz_time, i_tell );
1338         vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
1339 #undef p_area
1340
1341         o_time = [NSString stringWithCString: psz_time];
1342         [o_timefield setStringValue: o_time]; 
1343     }
1344
1345     vlc_mutex_unlock( &p_playlist->object_lock );
1346     vlc_object_release( p_playlist );
1347 }
1348
1349 - (IBAction)closeError:(id)sender
1350 {
1351     [o_err_msg setString: @""];
1352     [o_error performClose: self];
1353 }
1354
1355 - (IBAction)openReadMe:(id)sender
1356 {
1357     NSString * o_path = [[NSBundle mainBundle] 
1358         pathForResource: @"README.MacOSX" ofType: @"rtf"]; 
1359
1360     [[NSWorkspace sharedWorkspace] openFile: o_path 
1361                                    withApplication: @"TextEdit"];
1362 }
1363
1364 - (IBAction)reportABug:(id)sender
1365 {
1366     NSURL * o_url = [NSURL URLWithString: 
1367         @"http://www.videolan.org/support/bug-reporting.html"];
1368
1369     [[NSWorkspace sharedWorkspace] openURL: o_url];
1370 }
1371
1372 - (IBAction)openWebsite:(id)sender
1373 {
1374     NSURL * o_url = [NSURL URLWithString: @"http://www.videolan.org"];
1375
1376     [[NSWorkspace sharedWorkspace] openURL: o_url];
1377 }
1378
1379 - (IBAction)openLicense:(id)sender
1380 {
1381     NSString * o_path = [[NSBundle mainBundle] 
1382         pathForResource: @"COPYING" ofType: nil];
1383
1384     [[NSWorkspace sharedWorkspace] openFile: o_path 
1385                                    withApplication: @"TextEdit"];
1386 }
1387
1388 - (void)windowDidBecomeKey:(NSNotification *)o_notification
1389 {
1390     if( [o_notification object] == o_msgs_panel )
1391     {
1392         id o_msg;
1393         NSEnumerator * o_enum;
1394
1395         [o_messages setString: @""]; 
1396
1397         [o_msg_lock lock];
1398
1399         o_enum = [o_msg_arr objectEnumerator];
1400
1401         while( ( o_msg = [o_enum nextObject] ) != nil )
1402         {
1403             [o_messages insertText: o_msg];
1404         }
1405
1406         [o_msg_lock unlock];
1407     }
1408 }
1409
1410 @end
1411
1412 @implementation VLCMain (NSMenuValidation)
1413
1414 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
1415 {
1416     BOOL bEnabled = TRUE;
1417
1418     /* Recent Items Menu */
1419
1420     if( [[o_mi title] isEqualToString: _NS("Clear Menu")] )
1421     {
1422         NSMenu * o_menu = [o_mi_open_recent submenu];
1423         int i_nb_items = [o_menu numberOfItems];
1424         NSArray * o_docs = [[NSDocumentController sharedDocumentController]
1425                                                        recentDocumentURLs];
1426         UInt32 i_nb_docs = [o_docs count];
1427
1428         if( i_nb_items > 1 )
1429         {
1430             while( --i_nb_items )
1431             {
1432                 [o_menu removeItemAtIndex: 0];
1433             }
1434         }
1435
1436         if( i_nb_docs > 0 )
1437         {
1438             NSURL * o_url;
1439             NSString * o_doc;
1440
1441             [o_menu insertItem: [NSMenuItem separatorItem] atIndex: 0];
1442
1443             while( TRUE )
1444             {
1445                 i_nb_docs--;
1446
1447                 o_url = [o_docs objectAtIndex: i_nb_docs];
1448
1449                 if( [o_url isFileURL] )
1450                 {
1451                     o_doc = [o_url path];
1452                 }
1453                 else
1454                 {
1455                     o_doc = [o_url absoluteString];
1456                 }
1457
1458                 [o_menu insertItemWithTitle: o_doc
1459                     action: @selector(openRecentItem:)
1460                     keyEquivalent: @"" atIndex: 0]; 
1461
1462                 if( i_nb_docs == 0 )
1463                 {
1464                     break;
1465                 }
1466             } 
1467         }
1468         else
1469         {
1470             bEnabled = FALSE;
1471         }
1472     }
1473
1474     return( bEnabled );
1475 }
1476
1477 @end
1478
1479 @implementation VLCMain (Internal)
1480
1481 - (void)handlePortMessage:(NSPortMessage *)o_msg
1482 {
1483     id ** val;
1484     NSData * o_data;
1485     NSValue * o_value;
1486     NSInvocation * o_inv;
1487     NSConditionLock * o_lock;
1488  
1489     o_data = [[o_msg components] lastObject];
1490     o_inv = *((NSInvocation **)[o_data bytes]); 
1491     [o_inv getArgument: &o_value atIndex: 2];
1492     val = (id **)[o_value pointerValue];
1493     [o_inv setArgument: val[1] atIndex: 2];
1494     o_lock = *(val[0]);
1495
1496     [o_lock lock];
1497     [o_inv invoke];
1498     [o_lock unlockWithCondition: 1];
1499 }
1500
1501 @end