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