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