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