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