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