]> git.sesse.net Git - vlc/blob - modules/gui/macosx/intf.m
* modules/gui/macosx: Fixed crashes with the new submenus when used on a
[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.77 2003/05/08 17:13:22 massiot 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_mu_program setTitle: _NS("Program")];
333     [o_mi_title setTitle: _NS("Title")];
334     [o_mu_title setTitle: _NS("Title")];
335     [o_mi_chapter setTitle: _NS("Chapter")];
336     [o_mu_chapter setTitle: _NS("Chapter")];
337     
338     [o_mu_audio setTitle: _NS("Audio")];
339     [o_mi_vol_up setTitle: _NS("Volume Up")];
340     [o_mi_vol_down setTitle: _NS("Volume Down")];
341     [o_mi_mute setTitle: _NS("Mute")];
342     [o_mi_audiotrack setTitle: _NS("Audio Track")];
343     [o_mu_audiotrack setTitle: _NS("Audio Track")];
344     [o_mi_channels setTitle: _NS("Channels")];
345     [o_mu_channels setTitle: _NS("Channels")];
346     [o_mi_device setTitle: _NS("Device")];
347     [o_mu_device setTitle: _NS("Device")];
348     
349     [o_mu_video setTitle: _NS("Video")];
350     [o_mi_half_window setTitle: _NS("Half Size")];
351     [o_mi_normal_window setTitle: _NS("Normal Size")];
352     [o_mi_double_window setTitle: _NS("Double Size")];
353     [o_mi_fittoscreen setTitle: _NS("Fit To Screen")];
354     [o_mi_fullscreen setTitle: _NS("Fullscreen")];
355     [o_mi_floatontop setTitle: _NS("Float On Top")];
356     [o_mi_videotrack setTitle: _NS("Video Track")];
357     [o_mu_videotrack setTitle: _NS("Video Track")];
358     [o_mi_screen setTitle: _NS("Screen")];
359     [o_mu_screen setTitle: _NS("Screen")];
360     [o_mi_subtitle setTitle: _NS("Subtitles")];
361     [o_mu_subtitle setTitle: _NS("Subtitles")];
362     [o_mi_deinterlace setTitle: _NS("Deinterlace")];
363     [o_mu_deinterlace setTitle: _NS("Deinterlace")];
364
365     [o_mu_window setTitle: _NS("Window")];
366     [o_mi_minimize setTitle: _NS("Minimize Window")];
367     [o_mi_close_window setTitle: _NS("Close Window")];
368     [o_mi_controller setTitle: _NS("Controller")];
369     [o_mi_playlist setTitle: _NS("Playlist")];
370     [o_mi_info setTitle: _NS("Info")];
371     [o_mi_messages setTitle: _NS("Messages")];
372
373     [o_mi_bring_atf setTitle: _NS("Bring All to Front")];
374
375     [o_mu_help setTitle: _NS("Help")];
376     [o_mi_readme setTitle: _NS("ReadMe...")];
377     [o_mi_reportabug setTitle: _NS("Report a Bug")];
378     [o_mi_website setTitle: _NS("VideoLAN Website")];
379     [o_mi_license setTitle: _NS("License")];
380
381     /* dock menu */
382     [o_dmi_play setTitle: _NS("Play")];
383     [o_dmi_stop setTitle: _NS("Stop")];
384     [o_dmi_next setTitle: _NS("Next")];
385     [o_dmi_previous setTitle: _NS("Previous")];
386
387     /* error panel */
388     [o_error setTitle: _NS("Error")];
389     [o_err_lbl setStringValue: _NS("An error has occurred which probably prevented the execution of your request:")];
390     [o_err_bug_lbl setStringValue: _NS("If you believe that it is a bug, please follow the instructions at:")]; 
391     [o_err_btn_msgs setTitle: _NS("Open Messages Window")];
392     [o_err_btn_dismiss setTitle: _NS("Dismiss")];
393
394     [o_info_window setTitle: _NS("Info")];
395
396     [self setSubmenusEnabled: FALSE];
397     [self manageVolumeSlider];
398 }
399
400 - (void)applicationWillFinishLaunching:(NSNotification *)o_notification
401 {
402     intf_thread_t * p_intf = [NSApp getIntf];
403
404     o_msg_lock = [[NSLock alloc] init];
405     o_msg_arr = [[NSMutableArray arrayWithCapacity: 200] retain];
406
407     o_img_play = [[NSImage imageNamed: @"play"] retain];
408     o_img_pause = [[NSImage imageNamed: @"pause"] retain];
409
410     [p_intf->p_sys->o_sendport setDelegate: self];
411     [[NSRunLoop currentRunLoop] 
412         addPort: p_intf->p_sys->o_sendport
413         forMode: NSDefaultRunLoopMode];
414
415     [NSTimer scheduledTimerWithTimeInterval: 0.5
416         target: self selector: @selector(manageIntf:)
417         userInfo: nil repeats: FALSE];
418
419     [NSThread detachNewThreadSelector: @selector(manage)
420         toTarget: self withObject: nil];
421
422     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_LOW );
423 }
424
425 - (BOOL)application:(NSApplication *)o_app openFile:(NSString *)o_filename
426 {
427     intf_thread_t * p_intf = [NSApp getIntf];
428     
429     [o_playlist appendArray:
430         [NSArray arrayWithObject: o_filename] atPos: -1 enqueue: NO];
431     
432     config_PutPsz( [NSApp getIntf], "sub-file", "" );
433     config_PutInt( p_intf, "sub-delay", 0 );
434     config_PutFloat( p_intf, "sub-fps", 0.0 );
435     config_PutPsz( p_intf, "sout", "" );
436             
437     return( TRUE );
438 }
439
440 - (id)getControls
441 {
442     if ( o_controls )
443     {
444         return o_controls;
445     }
446     return nil;
447 }
448
449 - (void)manage
450 {
451     NSDate * o_sleep_date;
452     intf_thread_t * p_intf = [NSApp getIntf];
453     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
454
455     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_LOW );
456
457     while( !p_intf->b_die )
458     {
459         playlist_t * p_playlist;
460
461         vlc_mutex_lock( &p_intf->change_lock );
462
463         p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, 
464                                               FIND_ANYWHERE );
465
466         if( p_playlist != NULL )
467         {
468             vlc_mutex_lock( &p_playlist->object_lock );
469             
470             [self manage: p_playlist];
471             
472             vlc_mutex_unlock( &p_playlist->object_lock );
473             vlc_object_release( p_playlist );
474         }
475
476         vlc_mutex_unlock( &p_intf->change_lock );
477
478         o_sleep_date = [NSDate dateWithTimeIntervalSinceNow: .5];
479         [NSThread sleepUntilDate: o_sleep_date];
480     }
481
482     [self terminate];
483
484     [o_pool release];
485 }
486
487 - (void)manage:(playlist_t *)p_playlist
488 {
489     vlc_value_t val;
490
491     intf_thread_t * p_intf = [NSApp getIntf];
492
493     if( var_Get( (vlc_object_t *)p_playlist, "intf-change", &val ) >= 0 &&
494         val.b_bool )
495     {
496         p_intf->p_sys->b_playlist_update = VLC_TRUE;
497         p_intf->p_sys->b_intf_update = VLC_TRUE;
498     }
499
500 #define p_input p_playlist->p_input
501
502     if( p_input )
503     {
504         vout_thread_t   * p_vout  = NULL;
505         aout_instance_t * p_aout  = NULL; 
506         vlc_bool_t b_need_menus = VLC_FALSE;
507
508         vlc_mutex_lock( &p_input->stream.stream_lock );
509
510         if( !p_input->b_die )
511         {
512             audio_volume_t i_volume;
513
514             /* New input or stream map change */
515             if( p_input->stream.b_changed )
516             {
517                 p_intf->p_sys->b_playing = 1;
518                 [self manageMode: p_playlist];
519                 b_need_menus = VLC_TRUE;
520             }
521
522             if( p_intf->p_sys->i_part !=
523                 p_input->stream.p_selected_area->i_part )
524             {
525                 p_intf->p_sys->b_chapter_update = 1;
526                 b_need_menus = VLC_TRUE;
527             }
528
529             p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT,
530                                               FIND_ANYWHERE );
531             if( p_aout != NULL )
532             {
533                 vlc_value_t val;
534
535                 if( var_Get( (vlc_object_t *)p_aout, "intf-change", &val )
536                     >= 0 && val.b_bool )
537                 {
538                     p_intf->p_sys->b_aout_update = 1;
539                     b_need_menus = VLC_TRUE;
540                 }
541
542                 vlc_object_release( (vlc_object_t *)p_aout );
543             }
544
545             aout_VolumeGet( p_intf, &i_volume );
546             p_intf->p_sys->b_mute = ( i_volume == 0 );
547
548             p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
549                                               FIND_ANYWHERE );
550             if( p_vout != NULL )
551             {
552                 vlc_value_t val;
553
554                 if( var_Get( (vlc_object_t *)p_vout, "intf-change", &val )
555                     >= 0 && val.b_bool )
556                 {
557                     p_intf->p_sys->b_vout_update = 1;
558                     b_need_menus = VLC_TRUE;
559                 }
560
561                 vlc_object_release( (vlc_object_t *)p_vout );
562             } 
563
564             if( b_need_menus )
565             {
566                 [self setupMenus: p_input];
567             }
568         }
569
570         vlc_mutex_unlock( &p_input->stream.stream_lock );
571     }
572     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
573     {
574         p_intf->p_sys->b_playing = 0;
575         [self manageMode: p_playlist];
576     }
577
578 #undef p_input
579 }
580
581 - (void)manageMode:(playlist_t *)p_playlist
582 {
583     intf_thread_t * p_intf = [NSApp getIntf];
584
585     if( p_playlist->p_input != NULL )
586     {
587         /* get ready for menu regeneration */
588         p_intf->p_sys->b_program_update = 1;
589         p_intf->p_sys->b_title_update = 1;
590         p_intf->p_sys->b_chapter_update = 1;
591         p_intf->p_sys->b_audio_update = 1;
592         p_intf->p_sys->b_video_update = 1;
593         p_intf->p_sys->b_spu_update = 1;
594         p_intf->p_sys->b_current_title_update = 1;
595         p_intf->p_sys->i_part = 0;
596
597         p_playlist->p_input->stream.b_changed = 0;
598         
599         msg_Dbg( p_intf, "stream has changed, refreshing interface" );
600     }
601     else
602     {
603         vout_thread_t * p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
604                                                           FIND_ANYWHERE );
605         if( p_vout != NULL )
606         {
607             vlc_object_detach( p_vout );
608             vlc_object_release( p_vout );
609
610             vlc_mutex_unlock( &p_playlist->object_lock );
611             vout_Destroy( p_vout );
612             vlc_mutex_lock( &p_playlist->object_lock );
613         }
614     }
615
616     p_intf->p_sys->b_intf_update = VLC_TRUE;
617 }
618
619 - (void)manageIntf:(NSTimer *)o_timer
620 {
621     intf_thread_t * p_intf = [NSApp getIntf];
622
623     if( p_intf->p_vlc->b_die == VLC_TRUE )
624     {
625         [o_timer invalidate];
626         return;
627     }
628
629     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
630                                                        FIND_ANYWHERE );
631
632     if( p_playlist == NULL )
633     {
634         return;
635     }
636
637     if ( p_intf->p_sys->b_playlist_update )
638     {
639         vlc_value_t val;
640
641         val.b_bool = 0;
642         var_Set( (vlc_object_t *)p_playlist, "intf-change", val );
643
644         [o_playlist playlistUpdated];
645
646         p_intf->p_sys->b_playlist_update = VLC_FALSE;
647     }
648
649     if( p_intf->p_sys->b_current_title_update )
650     {
651         vout_thread_t   *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
652                                               FIND_ANYWHERE );
653         if( p_vout != NULL )
654         {
655             id o_vout_wnd;
656             NSEnumerator * o_enum = [[NSApp windows] objectEnumerator];
657             
658             while( ( o_vout_wnd = [o_enum nextObject] ) )
659             {
660                 if( [[o_vout_wnd className] isEqualToString: @"VLCWindow"] )
661                 {
662                     [o_vout_wnd updateTitle];
663                 }
664             }
665             vlc_object_release( (vlc_object_t *)p_vout );
666         }
667         [o_playlist updateRowSelection];
668         [o_info updateInfo];
669
670         p_intf->p_sys->b_current_title_update = FALSE;
671     }
672
673     vlc_mutex_lock( &p_playlist->object_lock );
674
675 #define p_input p_playlist->p_input
676
677     if( p_input != NULL )
678     {
679         vlc_mutex_lock( &p_input->stream.stream_lock );
680     }
681
682     if( p_intf->p_sys->b_intf_update )
683     {
684         vlc_bool_t b_input = VLC_FALSE;
685         vlc_bool_t b_plmul = VLC_FALSE;
686         vlc_bool_t b_control = VLC_FALSE;
687         vlc_bool_t b_seekable = VLC_FALSE;
688         vlc_bool_t b_chapters = VLC_FALSE;
689
690         b_plmul = p_playlist->i_size > 1;
691
692         if( ( b_input = ( p_input != NULL ) ) )
693         {
694             /* seekable streams */
695             b_seekable = p_input->stream.b_seekable;
696
697             /* control buttons for free pace streams */
698             b_control = p_input->stream.b_pace_control; 
699
700             /* chapters */
701             b_chapters = p_input->stream.p_selected_area->i_part_nb > 1; 
702
703             /* play status */
704             p_intf->p_sys->b_play_status = 
705                 p_input->stream.control.i_status != PAUSE_S;
706         }
707         else
708         {
709             /* play status */
710             p_intf->p_sys->b_play_status = VLC_FALSE;
711
712             [self setSubmenusEnabled: FALSE];
713         }
714
715         [self playStatusUpdated: p_intf->p_sys->b_play_status];
716
717         [o_btn_stop setEnabled: b_input];
718         [o_btn_faster setEnabled: b_control];
719         [o_btn_slower setEnabled: b_control];
720         [o_btn_prev setEnabled: (b_plmul || b_chapters)];
721         [o_btn_next setEnabled: (b_plmul || b_chapters)];
722
723         [o_timeslider setFloatValue: 0.0];
724         [o_timeslider setEnabled: b_seekable];
725         [o_timefield setStringValue: @"0:00:00"];
726
727         [self manageVolumeSlider];
728
729         p_intf->p_sys->b_intf_update = VLC_FALSE;
730     }
731
732 #define p_area p_input->stream.p_selected_area
733
734     if( p_intf->p_sys->b_playing && p_input != NULL )
735     {
736         vlc_bool_t b_field_update = VLC_TRUE;
737
738         if( !p_input->b_die && ( p_intf->p_sys->b_play_status !=
739             ( p_input->stream.control.i_status != PAUSE_S ) ) ) 
740         {
741             p_intf->p_sys->b_play_status =
742                 !p_intf->p_sys->b_play_status;
743
744             [self playStatusUpdated: p_intf->p_sys->b_play_status]; 
745         }
746
747         if( p_input->stream.b_seekable )
748         {
749             if( f_slider == f_slider_old )
750             {
751                 float f_updated = ( 10000. * p_area->i_tell ) /
752                                            p_area->i_size;
753
754                 if( f_slider != f_updated )
755                 {
756                     [o_timeslider setFloatValue: f_updated];
757                 }
758             }
759             else
760             {
761                 off_t i_seek = ( f_slider * p_area->i_size ) / 10000;
762
763                 /* release the lock to be able to seek */
764                 vlc_mutex_unlock( &p_input->stream.stream_lock );
765                 input_Seek( p_input, i_seek, INPUT_SEEK_SET );
766                 vlc_mutex_lock( &p_input->stream.stream_lock );
767
768                 /* update the old value */
769                 f_slider_old = f_slider; 
770
771                 b_field_update = VLC_FALSE;
772             }
773         }
774
775         if( b_field_update )
776         {
777             NSString * o_time;
778             char psz_time[ OFFSETTOTIME_MAX_SIZE ];
779
780             input_OffsetToTime( p_input, psz_time, p_area->i_tell );
781
782             o_time = [NSString stringWithCString: psz_time];
783             [o_timefield setStringValue: o_time];
784         }
785
786         /* disable screen saver */
787         UpdateSystemActivity( UsrActivity );
788     }
789
790 #undef p_area
791
792     if( p_input != NULL )
793     {
794         vlc_mutex_unlock( &p_input->stream.stream_lock );
795     }
796
797 #undef p_input
798
799     vlc_mutex_unlock( &p_playlist->object_lock );
800     vlc_object_release( p_playlist );
801
802     [self updateMessageArray];
803
804     [NSTimer scheduledTimerWithTimeInterval: 0.5
805         target: self selector: @selector(manageIntf:)
806         userInfo: nil repeats: FALSE];
807 }
808
809 - (void)updateMessageArray
810 {
811     int i_start, i_stop;
812     intf_thread_t * p_intf = [NSApp getIntf];
813
814     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
815     i_stop = *p_intf->p_sys->p_sub->pi_stop;
816     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
817
818     if( p_intf->p_sys->p_sub->i_start != i_stop )
819     {
820         NSColor *o_white = [NSColor whiteColor];
821         NSColor *o_red = [NSColor redColor];
822         NSColor *o_yellow = [NSColor yellowColor];
823         NSColor *o_gray = [NSColor grayColor];
824
825         NSColor * pp_color[4] = { o_white, o_red, o_yellow, o_gray };
826         static const char * ppsz_type[4] = { ": ", " error: ",
827                                              " warning: ", " debug: " };
828
829         for( i_start = p_intf->p_sys->p_sub->i_start;
830              i_start != i_stop;
831              i_start = (i_start+1) % VLC_MSG_QSIZE )
832         {
833             NSString *o_msg;
834             NSDictionary *o_attr;
835             NSAttributedString *o_msg_color;
836
837             int i_type = p_intf->p_sys->p_sub->p_msg[i_start].i_type;
838
839             [o_msg_lock lock];
840
841             if( [o_msg_arr count] + 2 > 400 )
842             {
843                 unsigned rid[] = { 0, 1 };
844                 [o_msg_arr removeObjectsFromIndices: (unsigned *)&rid
845                            numIndices: sizeof(rid)/sizeof(rid[0])];
846             }
847
848             o_attr = [NSDictionary dictionaryWithObject: o_gray
849                 forKey: NSForegroundColorAttributeName];
850             o_msg = [NSString stringWithFormat: @"%s%s",
851                 p_intf->p_sys->p_sub->p_msg[i_start].psz_module,
852                 ppsz_type[i_type]];
853             o_msg_color = [[NSAttributedString alloc]
854                 initWithString: o_msg attributes: o_attr];
855             [o_msg_arr addObject: [o_msg_color autorelease]];
856
857             o_attr = [NSDictionary dictionaryWithObject: pp_color[i_type]
858                 forKey: NSForegroundColorAttributeName];
859             o_msg = [NSString stringWithFormat: @"%s\n",
860                 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg];
861             o_msg_color = [[NSAttributedString alloc]
862                 initWithString: o_msg attributes: o_attr];
863             [o_msg_arr addObject: [o_msg_color autorelease]];
864
865             [o_msg_lock unlock];
866
867             if( i_type == 1 )
868             {
869                 NSString *o_my_msg = [NSString stringWithFormat: @"%s: %s\n",
870                     p_intf->p_sys->p_sub->p_msg[i_start].psz_module,
871                     p_intf->p_sys->p_sub->p_msg[i_start].psz_msg];
872
873                 NSRange s_r = NSMakeRange( [[o_err_msg string] length], 0 );
874                 [o_err_msg setEditable: YES];
875                 [o_err_msg setSelectedRange: s_r];
876                 [o_err_msg insertText: o_my_msg];
877
878                 [o_error makeKeyAndOrderFront: self];
879                 [o_err_msg setEditable: NO];
880             }
881         }
882
883         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
884         p_intf->p_sys->p_sub->i_start = i_start;
885         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
886     }
887 }
888
889 - (void)playStatusUpdated:(BOOL)b_pause
890 {
891     if( b_pause )
892     {
893         [o_btn_play setImage: o_img_pause];
894         [o_btn_play setToolTip: _NS("Pause")];
895         [o_mi_play setTitle: _NS("Pause")];
896         [o_dmi_play setTitle: _NS("Pause")];
897     }
898     else
899     {
900         [o_btn_play setImage: o_img_play];
901         [o_btn_play setToolTip: _NS("Play")];
902         [o_mi_play setTitle: _NS("Play")];
903         [o_dmi_play setTitle: _NS("Play")];
904     }
905 }
906
907 - (void)setSubmenusEnabled:(BOOL)b_enabled
908 {
909     [o_mi_program setEnabled: b_enabled];
910     [o_mi_title setEnabled: b_enabled];
911     [o_mi_chapter setEnabled: b_enabled];
912     [o_mi_audiotrack setEnabled: b_enabled];
913     [o_mi_videotrack setEnabled: b_enabled];
914     [o_mi_subtitle setEnabled: b_enabled];
915     [o_mi_channels setEnabled: b_enabled];
916     [o_mi_device setEnabled: b_enabled];
917     [o_mi_screen setEnabled: b_enabled];
918 }
919
920 - (void)manageVolumeSlider
921 {
922     audio_volume_t i_volume;
923     intf_thread_t * p_intf = [NSApp getIntf];
924
925     aout_VolumeGet( p_intf, &i_volume );
926
927     [o_volumeslider setFloatValue: (float)i_volume / AOUT_VOLUME_STEP]; 
928     [o_volumeslider setEnabled: 1];
929
930     p_intf->p_sys->b_mute = ( i_volume == 0 );
931 }
932
933 - (void)terminate
934 {
935     NSEvent * o_event;
936     vout_thread_t * p_vout;
937     playlist_t * p_playlist;
938     intf_thread_t * p_intf = [NSApp getIntf];
939
940     /*
941      * Free playlists
942      */
943     msg_Dbg( p_intf, "removing all playlists" );
944     while( (p_playlist = vlc_object_find( p_intf->p_vlc, VLC_OBJECT_PLAYLIST,
945                                           FIND_CHILD )) )
946     {
947         vlc_object_detach( p_playlist );
948         vlc_object_release( p_playlist );
949         playlist_Destroy( p_playlist );
950     }
951
952     /*
953      * Free video outputs
954      */
955     msg_Dbg( p_intf, "removing all video outputs" );
956     while( (p_vout = vlc_object_find( p_intf->p_vlc, 
957                                       VLC_OBJECT_VOUT, FIND_CHILD )) )
958     {
959         vlc_object_detach( p_vout );
960         vlc_object_release( p_vout );
961         vout_Destroy( p_vout );
962     }
963
964     if( o_img_pause != nil )
965     {
966         [o_img_pause release];
967         o_img_pause = nil;
968     }
969
970     if( o_img_play != nil )
971     {
972         [o_img_play release];
973         o_img_play = nil;
974     }
975
976     if( o_msg_arr != nil )
977     {
978         [o_msg_arr removeAllObjects];
979         [o_msg_arr release];
980         o_msg_arr = nil;
981     }
982
983     if( o_msg_lock != nil )
984     {
985         [o_msg_lock release];
986         o_msg_lock = nil;
987     }
988
989     if( o_prefs != nil )
990     {
991         [o_prefs release];
992         o_prefs = nil;
993     }
994
995     [NSApp stop: nil];
996
997     /* write cached user defaults to disk */
998     [[NSUserDefaults standardUserDefaults] synchronize];
999
1000     /* send a dummy event to break out of the event loop */
1001     o_event = [NSEvent mouseEventWithType: NSLeftMouseDown
1002                 location: NSMakePoint( 1, 1 ) modifierFlags: 0
1003                 timestamp: 1 windowNumber: [[NSApp mainWindow] windowNumber]
1004                 context: [NSGraphicsContext currentContext] eventNumber: 1
1005                 clickCount: 1 pressure: 0.0];
1006     [NSApp postEvent: o_event atStart: YES];
1007 }
1008
1009 - (void)setupMenus:(input_thread_t *)p_input
1010 {
1011     intf_thread_t * p_intf = [NSApp getIntf];
1012
1013     p_intf->p_sys->b_chapter_update |= p_intf->p_sys->b_title_update;
1014     p_intf->p_sys->b_audio_update |= p_intf->p_sys->b_title_update |
1015                                      p_intf->p_sys->b_program_update;
1016     p_intf->p_sys->b_spu_update |= p_intf->p_sys->b_title_update |
1017                                    p_intf->p_sys->b_program_update;
1018     p_intf->p_sys->b_video_update |= p_intf->p_sys->b_program_update |
1019                                      p_intf->p_sys->b_program_update;
1020
1021     if( p_intf->p_sys->b_program_update )
1022     {
1023         [self setupVarMenu: o_mi_program target: (vlc_object_t *)p_input
1024             var: "program" selector: @selector(toggleVar:)];
1025
1026         p_intf->p_sys->b_program_update = 0;
1027     }
1028
1029     if( p_intf->p_sys->b_title_update )
1030     {
1031         [self setupVarMenu: o_mi_title target: (vlc_object_t *)p_input
1032             var: "title" selector: @selector(toggleVar:)];
1033
1034         p_intf->p_sys->b_title_update = 0;
1035     }
1036
1037     if( p_intf->p_sys->b_chapter_update )
1038     {
1039         [self setupVarMenu: o_mi_chapter target: (vlc_object_t *)p_input
1040             var: "chapter" selector: @selector(toggleVar:)];\
1041         
1042         p_intf->p_sys->i_part = p_input->stream.p_selected_area->i_part;
1043         p_intf->p_sys->b_chapter_update = 0;
1044     }
1045
1046     if( p_intf->p_sys->b_audio_update )
1047     {
1048         [self setupVarMenu: o_mi_audiotrack target: (vlc_object_t *)p_input
1049             var: "audio-es" selector: @selector(toggleVar:)];
1050
1051         p_intf->p_sys->b_audio_update = 0;
1052     }
1053
1054     if( p_intf->p_sys->b_video_update )
1055     {
1056         [self setupVarMenu: o_mi_videotrack target: (vlc_object_t *)p_input
1057             var: "video-es" selector: @selector(toggleVar:)];
1058
1059         p_intf->p_sys->b_video_update = 0;
1060     }
1061
1062     if( p_intf->p_sys->b_spu_update )
1063     {
1064         [self setupVarMenu: o_mi_subtitle target: (vlc_object_t *)p_input
1065             var: "spu-es" selector: @selector(toggleVar:)];
1066
1067         p_intf->p_sys->b_spu_update = 0;
1068     }
1069
1070     if ( p_intf->p_sys->b_aout_update )
1071     {
1072         aout_instance_t * p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT,
1073                                                     FIND_ANYWHERE );
1074
1075         if ( p_aout != NULL )
1076         {
1077             vlc_value_t val;
1078             val.b_bool = 0;
1079             
1080             [self setupVarMenu: o_mi_channels target: (vlc_object_t *)p_aout
1081                 var: "audio-channels" selector: @selector(toggleVar:)];
1082
1083             [self setupVarMenu: o_mi_device target: (vlc_object_t *)p_aout
1084                 var: "audio-device" selector: @selector(toggleVar:)];
1085             var_Set( (vlc_object_t *)p_aout, "intf-change", val );
1086             
1087             vlc_object_release( (vlc_object_t *)p_aout );
1088         }
1089         p_intf->p_sys->b_aout_update = 0;
1090     }
1091
1092     if( p_intf->p_sys->b_vout_update )
1093     {
1094         vout_thread_t * p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
1095                                                           FIND_ANYWHERE );
1096
1097         if ( p_vout != NULL )
1098         {
1099             vlc_value_t val;
1100             val.b_bool = 0;
1101
1102             [self setupVarMenu: o_mi_screen target: (vlc_object_t *)p_vout
1103                 var: "video-device" selector: @selector(toggleVar:)];
1104             var_Set( (vlc_object_t *)p_vout, "intf-change", val );
1105             
1106             vlc_object_release( (vlc_object_t *)p_vout );
1107             [o_mi_close_window setEnabled: TRUE];
1108         }
1109         
1110         p_intf->p_sys->b_vout_update = 0;
1111     }
1112 }
1113
1114 - (void)setupVarMenu:(NSMenuItem *)o_mi
1115                      target:(vlc_object_t *)p_object
1116                      var:(const char *)psz_variable
1117                      selector:(SEL)pf_callback
1118 {
1119     int i, i_nb_items, i_value;
1120     NSMenu * o_menu = [o_mi submenu];
1121     vlc_value_t val, text;
1122     
1123     [o_mi setTag: (int)psz_variable];
1124     
1125     /* remove previous items */
1126     i_nb_items = [o_menu numberOfItems];
1127     for( i = 0; i < i_nb_items; i++ )
1128     {
1129         [o_menu removeItemAtIndex: 0];
1130     }
1131
1132     if ( var_Get( p_object, psz_variable, &val ) < 0 )
1133     {
1134         return;
1135     }
1136     i_value = val.i_int;
1137
1138     if ( var_Change( p_object, psz_variable,
1139                      VLC_VAR_GETLIST, &val, &text ) < 0 )
1140     {
1141         return;
1142     }
1143
1144     /* make (un)sensitive */
1145     [o_mi setEnabled: ( val.p_list->i_count > 1 )];
1146
1147     for ( i = 0; i < val.p_list->i_count; i++ )
1148     {
1149         NSMenuItem * o_lmi;
1150         NSString * o_title;
1151 NSLog(@"%d, %s", i_value, psz_variable);
1152
1153         if ( text.p_list->p_values[i].psz_string )
1154         {
1155             o_title = [NSApp localizedString: text.p_list->p_values[i].psz_string];
1156         }
1157         else
1158         {
1159             o_title = [NSString stringWithFormat: @"%s - %d",
1160                         strdup(psz_variable), val.p_list->p_values[i].i_int ];
1161         }
1162         o_lmi = [o_menu addItemWithTitle: [o_title copy]
1163                 action: pf_callback keyEquivalent: @""];
1164         
1165         /* FIXME: this isn't 64-bit clean ! */
1166         [o_lmi setTag: val.p_list->p_values[i].i_int];
1167         [o_lmi setRepresentedObject:
1168             [NSValue valueWithPointer: p_object]];
1169         [o_lmi setTarget: o_controls];
1170
1171         if ( i_value == val.p_list->p_values[i].i_int )
1172             [o_lmi setState: NSOnState];
1173     }
1174
1175     var_Change( p_object, psz_variable, VLC_VAR_FREELIST,
1176                 &val, &text );
1177
1178 }
1179
1180 - (IBAction)clearRecentItems:(id)sender
1181 {
1182     [[NSDocumentController sharedDocumentController]
1183                           clearRecentDocuments: nil];
1184 }
1185
1186 - (void)openRecentItem:(id)sender
1187 {
1188     [self application: nil openFile: [sender title]]; 
1189 }
1190
1191 - (IBAction)viewPreferences:(id)sender
1192 {
1193     if( o_prefs == nil )
1194     {
1195         o_prefs = [[VLCPrefs alloc] init];
1196     }
1197
1198     [o_prefs createPrefPanel: @"main"];
1199 }
1200
1201 - (IBAction)timesliderUpdate:(id)sender
1202 {
1203     float f_updated;
1204
1205     switch( [[NSApp currentEvent] type] )
1206     {
1207         case NSLeftMouseUp:
1208         case NSLeftMouseDown:
1209             f_slider = [sender floatValue];
1210             return;
1211
1212         case NSLeftMouseDragged:
1213             f_updated = [sender floatValue];
1214             break;
1215
1216         default:
1217             return;
1218     }
1219
1220     intf_thread_t * p_intf = [NSApp getIntf];
1221
1222     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1223                                                        FIND_ANYWHERE );
1224
1225     if( p_playlist == NULL )
1226     {
1227         return;
1228     }
1229
1230     vlc_mutex_lock( &p_playlist->object_lock );
1231
1232     if( p_playlist->p_input != NULL )
1233     {
1234         off_t i_tell;
1235         NSString * o_time;
1236         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
1237
1238 #define p_area p_playlist->p_input->stream.p_selected_area
1239         vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
1240         i_tell = f_updated / 10000. * p_area->i_size;
1241         input_OffsetToTime( p_playlist->p_input, psz_time, i_tell );
1242         vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
1243 #undef p_area
1244
1245         o_time = [NSString stringWithCString: psz_time];
1246         [o_timefield setStringValue: o_time]; 
1247     }
1248
1249     vlc_mutex_unlock( &p_playlist->object_lock );
1250     vlc_object_release( p_playlist );
1251 }
1252
1253 - (IBAction)closeError:(id)sender
1254 {
1255     [o_err_msg setString: @""];
1256     [o_error performClose: self];
1257 }
1258
1259 - (IBAction)openReadMe:(id)sender
1260 {
1261     NSString * o_path = [[NSBundle mainBundle] 
1262         pathForResource: @"README.MacOSX" ofType: @"rtf"]; 
1263
1264     [[NSWorkspace sharedWorkspace] openFile: o_path 
1265                                    withApplication: @"TextEdit"];
1266 }
1267
1268 - (IBAction)reportABug:(id)sender
1269 {
1270     NSURL * o_url = [NSURL URLWithString: 
1271         @"http://www.videolan.org/support/bug-reporting.html"];
1272
1273     [[NSWorkspace sharedWorkspace] openURL: o_url];
1274 }
1275
1276 - (IBAction)openWebsite:(id)sender
1277 {
1278     NSURL * o_url = [NSURL URLWithString: @"http://www.videolan.org"];
1279
1280     [[NSWorkspace sharedWorkspace] openURL: o_url];
1281 }
1282
1283 - (IBAction)openLicense:(id)sender
1284 {
1285     NSString * o_path = [[NSBundle mainBundle] 
1286         pathForResource: @"COPYING" ofType: nil];
1287
1288     [[NSWorkspace sharedWorkspace] openFile: o_path 
1289                                    withApplication: @"TextEdit"];
1290 }
1291
1292 - (IBAction)openCrashLog:(id)sender
1293 {
1294     NSString * o_path = [@"~/Library/Logs/CrashReporter/VLC.crash.log"
1295                                     stringByExpandingTildeInPath]; 
1296
1297     
1298     if ( [[NSFileManager defaultManager] fileExistsAtPath: o_path ] )
1299     {
1300         [[NSWorkspace sharedWorkspace] openFile: o_path 
1301                                     withApplication: @"Console"];
1302     }
1303     else
1304     {
1305         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.") );
1306
1307     }
1308 }
1309
1310 - (void)windowDidBecomeKey:(NSNotification *)o_notification
1311 {
1312     if( [o_notification object] == o_msgs_panel )
1313     {
1314         id o_msg;
1315         NSEnumerator * o_enum;
1316
1317         [o_messages setString: @""]; 
1318
1319         [o_msg_lock lock];
1320
1321         o_enum = [o_msg_arr objectEnumerator];
1322
1323         while( ( o_msg = [o_enum nextObject] ) != nil )
1324         {
1325             [o_messages insertText: o_msg];
1326         }
1327
1328         [o_msg_lock unlock];
1329     }
1330 }
1331
1332 @end
1333
1334 @implementation VLCMain (NSMenuValidation)
1335
1336 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
1337 {
1338     BOOL bEnabled = TRUE;
1339
1340     /* Recent Items Menu */
1341
1342     if( [[o_mi title] isEqualToString: _NS("Clear Menu")] )
1343     {
1344         NSMenu * o_menu = [o_mi_open_recent submenu];
1345         int i_nb_items = [o_menu numberOfItems];
1346         NSArray * o_docs = [[NSDocumentController sharedDocumentController]
1347                                                        recentDocumentURLs];
1348         UInt32 i_nb_docs = [o_docs count];
1349
1350         if( i_nb_items > 1 )
1351         {
1352             while( --i_nb_items )
1353             {
1354                 [o_menu removeItemAtIndex: 0];
1355             }
1356         }
1357
1358         if( i_nb_docs > 0 )
1359         {
1360             NSURL * o_url;
1361             NSString * o_doc;
1362
1363             [o_menu insertItem: [NSMenuItem separatorItem] atIndex: 0];
1364
1365             while( TRUE )
1366             {
1367                 i_nb_docs--;
1368
1369                 o_url = [o_docs objectAtIndex: i_nb_docs];
1370
1371                 if( [o_url isFileURL] )
1372                 {
1373                     o_doc = [o_url path];
1374                 }
1375                 else
1376                 {
1377                     o_doc = [o_url absoluteString];
1378                 }
1379
1380                 [o_menu insertItemWithTitle: o_doc
1381                     action: @selector(openRecentItem:)
1382                     keyEquivalent: @"" atIndex: 0]; 
1383
1384                 if( i_nb_docs == 0 )
1385                 {
1386                     break;
1387                 }
1388             } 
1389         }
1390         else
1391         {
1392             bEnabled = FALSE;
1393         }
1394     }
1395
1396     return( bEnabled );
1397 }
1398
1399 @end
1400
1401 @implementation VLCMain (Internal)
1402
1403 - (void)handlePortMessage:(NSPortMessage *)o_msg
1404 {
1405     id ** val;
1406     NSData * o_data;
1407     NSValue * o_value;
1408     NSInvocation * o_inv;
1409     NSConditionLock * o_lock;
1410  
1411     o_data = [[o_msg components] lastObject];
1412     o_inv = *((NSInvocation **)[o_data bytes]); 
1413     [o_inv getArgument: &o_value atIndex: 2];
1414     val = (id **)[o_value pointerValue];
1415     [o_inv setArgument: val[1] atIndex: 2];
1416     o_lock = *(val[0]);
1417
1418     [o_lock lock];
1419     [o_inv invoke];
1420     [o_lock unlockWithCondition: 1];
1421 }
1422
1423 @end