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