* reselect current playlist item when reordering items.
[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.69 2003/04/06 23:21:13 massiot Exp $
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Derk-Jan Hartman <thedj@users.sourceforge.net>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <sys/param.h>                                    /* for MAXPATHLEN */
31 #include <string.h>
32
33 #include "intf.h"
34 #include "vout.h"
35 #include "prefs.h"
36 #include "playlist.h"
37 #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     [o_playlist appendArray:
416         [NSArray arrayWithObject: o_filename] atPos: -1 enqueue: NO];
417
418     return( TRUE );
419 }
420
421 - (id)getControls
422 {
423     if ( o_controls )
424     {
425         return o_controls;
426     }
427     return nil;
428 }
429
430 - (void)manage
431 {
432     NSDate * o_sleep_date;
433     intf_thread_t * p_intf = [NSApp getIntf];
434     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
435
436     vlc_thread_set_priority( p_intf, VLC_THREAD_PRIORITY_LOW );
437
438     while( !p_intf->b_die )
439     {
440         playlist_t * p_playlist;
441
442         vlc_mutex_lock( &p_intf->change_lock );
443
444         p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, 
445                                               FIND_ANYWHERE );
446
447         if( p_playlist != NULL )
448         {
449             vlc_mutex_lock( &p_playlist->object_lock );
450             
451             [self manage: p_playlist];
452             
453             vlc_mutex_unlock( &p_playlist->object_lock );
454             vlc_object_release( p_playlist );
455         }
456
457         vlc_mutex_unlock( &p_intf->change_lock );
458
459         o_sleep_date = [NSDate dateWithTimeIntervalSinceNow: .5];
460         [NSThread sleepUntilDate: o_sleep_date];
461     }
462
463     [self terminate];
464
465     [o_pool release];
466 }
467
468 - (void)manage:(playlist_t *)p_playlist
469 {
470     vlc_value_t val;
471
472     intf_thread_t * p_intf = [NSApp getIntf];
473
474     if( var_Get( (vlc_object_t *)p_playlist, "intf-change", &val ) >= 0 &&
475         val.b_bool )
476     {
477         p_intf->p_sys->b_playlist_update = VLC_TRUE;
478         p_intf->p_sys->b_intf_update = VLC_TRUE;
479     }
480
481 #define p_input p_playlist->p_input
482
483     if( p_input )
484     {
485         vout_thread_t   * p_vout  = NULL;
486         aout_instance_t * p_aout  = NULL; 
487         vlc_bool_t b_need_menus = VLC_FALSE;
488
489         vlc_mutex_lock( &p_input->stream.stream_lock );
490
491         if( !p_input->b_die )
492         {
493             audio_volume_t i_volume;
494
495             /* New input or stream map change */
496             if( p_input->stream.b_changed )
497             {
498                 p_intf->p_sys->b_playing = 1;
499                 [self manageMode: p_playlist];
500                 b_need_menus = VLC_TRUE;
501             }
502
503             if( p_intf->p_sys->i_part !=
504                 p_input->stream.p_selected_area->i_part )
505             {
506                 p_intf->p_sys->b_chapter_update = 1;
507                 b_need_menus = VLC_TRUE;
508             }
509
510             p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT,
511                                               FIND_ANYWHERE );
512             if( p_aout != NULL )
513             {
514                 vlc_value_t val;
515
516                 if( var_Get( (vlc_object_t *)p_aout, "intf-change", &val )
517                     >= 0 && val.b_bool )
518                 {
519                     p_intf->p_sys->b_aout_update = 1;
520                     b_need_menus = VLC_TRUE;
521                 }
522
523                 vlc_object_release( (vlc_object_t *)p_aout );
524             }
525
526             aout_VolumeGet( p_intf, &i_volume );
527             p_intf->p_sys->b_mute = ( i_volume == 0 );
528
529             p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
530                                               FIND_ANYWHERE );
531             if( p_vout != NULL )
532             {
533                 vlc_value_t val;
534
535                 if( var_Get( (vlc_object_t *)p_vout, "intf-change", &val )
536                     >= 0 && val.b_bool )
537                 {
538                     p_intf->p_sys->b_vout_update = 1;
539                     b_need_menus = VLC_TRUE;
540                 }
541
542                 vlc_object_release( (vlc_object_t *)p_vout );
543             } 
544
545             if( b_need_menus )
546             {
547                 [self setupMenus: p_input];
548             }
549         }
550
551         vlc_mutex_unlock( &p_input->stream.stream_lock );
552     }
553     else if( p_intf->p_sys->b_playing && !p_intf->b_die )
554     {
555         p_intf->p_sys->b_playing = 0;
556         [self manageMode: p_playlist];
557     }
558
559 #undef p_input
560 }
561
562 - (void)manageMode:(playlist_t *)p_playlist
563 {
564     intf_thread_t * p_intf = [NSApp getIntf];
565
566     if( p_playlist->p_input != NULL )
567     {
568         /* get ready for menu regeneration */
569         p_intf->p_sys->b_program_update = 1;
570         p_intf->p_sys->b_title_update = 1;
571         p_intf->p_sys->b_chapter_update = 1;
572         p_intf->p_sys->b_audio_update = 1;
573         p_intf->p_sys->b_spu_update = 1;
574         p_intf->p_sys->b_current_title_update = 1;
575         p_intf->p_sys->i_part = 0;
576
577         p_playlist->p_input->stream.b_changed = 0;
578         
579         msg_Dbg( p_intf, "stream has changed, refreshing interface" );
580     }
581     else
582     {
583         vout_thread_t * p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
584                                                           FIND_ANYWHERE );
585         if( p_vout != NULL )
586         {
587             vlc_object_detach( p_vout );
588             vlc_object_release( p_vout );
589
590             vlc_mutex_unlock( &p_playlist->object_lock );
591             vout_Destroy( p_vout );
592             vlc_mutex_lock( &p_playlist->object_lock );
593         }
594     }
595
596     p_intf->p_sys->b_intf_update = VLC_TRUE;
597 }
598
599 - (void)manageIntf:(NSTimer *)o_timer
600 {
601     intf_thread_t * p_intf = [NSApp getIntf];
602
603     if( p_intf->p_vlc->b_die == VLC_TRUE )
604     {
605         [o_timer invalidate];
606         return;
607     }
608
609     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
610                                                        FIND_ANYWHERE );
611
612     if( p_playlist == NULL )
613     {
614         return;
615     }
616
617     if ( p_intf->p_sys->b_playlist_update )
618     {
619         vlc_value_t val;
620
621         val.b_bool = 0;
622         var_Set( (vlc_object_t *)p_playlist, "intf-change", val );
623
624         [o_playlist playlistUpdated];
625
626         p_intf->p_sys->b_playlist_update = VLC_FALSE;
627     }
628
629     if( p_intf->p_sys->b_current_title_update )
630     {
631         id o_vout_wnd;
632
633         NSEnumerator * o_enum = [[NSApp windows] objectEnumerator];
634         
635         while( ( o_vout_wnd = [o_enum nextObject] ) )
636         {
637             if( [[o_vout_wnd className] isEqualToString: @"VLCWindow"] )
638             {
639                 [o_vout_wnd updateTitle];
640             }
641         }
642
643         [o_playlist updateRowSelection];
644         [o_info updateInfo];
645
646         p_intf->p_sys->b_current_title_update = FALSE;
647     }
648
649     vlc_mutex_lock( &p_playlist->object_lock );
650
651 #define p_input p_playlist->p_input
652
653     if( p_input != NULL )
654     {
655         vlc_mutex_lock( &p_input->stream.stream_lock );
656     }
657
658     if( p_intf->p_sys->b_intf_update )
659     {
660         vlc_bool_t b_input = VLC_FALSE;
661         vlc_bool_t b_plmul = VLC_FALSE;
662         vlc_bool_t b_control = VLC_FALSE;
663         vlc_bool_t b_seekable = VLC_FALSE;
664         vlc_bool_t b_chapters = VLC_FALSE;
665
666         b_plmul = p_playlist->i_size > 1;
667
668         if( ( b_input = ( p_input != NULL ) ) )
669         {
670             /* seekable streams */
671             b_seekable = p_input->stream.b_seekable;
672
673             /* control buttons for free pace streams */
674             b_control = p_input->stream.b_pace_control; 
675
676             /* chapters */
677             b_chapters = p_input->stream.p_selected_area->i_part_nb > 1; 
678
679             /* play status */
680             p_intf->p_sys->b_play_status = 
681                 p_input->stream.control.i_status != PAUSE_S;
682         }
683         else
684         {
685             /* play status */
686             p_intf->p_sys->b_play_status = VLC_FALSE;
687
688             [self setSubmenusEnabled: FALSE];
689         }
690
691         [self playStatusUpdated: p_intf->p_sys->b_play_status];
692
693         [o_btn_stop setEnabled: b_input];
694         [o_btn_faster setEnabled: b_control];
695         [o_btn_slower setEnabled: b_control];
696         [o_btn_prev setEnabled: (b_plmul || b_chapters)];
697         [o_btn_next setEnabled: (b_plmul || b_chapters)];
698
699         [o_timeslider setFloatValue: 0.0];
700         [o_timeslider setEnabled: b_seekable];
701         [o_timefield setStringValue: @"0:00:00"];
702
703         [self manageVolumeSlider];
704
705         p_intf->p_sys->b_intf_update = VLC_FALSE;
706     }
707
708 #define p_area p_input->stream.p_selected_area
709
710     if( p_intf->p_sys->b_playing && p_input != NULL )
711     {
712         vlc_bool_t b_field_update = VLC_TRUE;
713
714         if( !p_input->b_die && ( p_intf->p_sys->b_play_status !=
715             ( p_input->stream.control.i_status != PAUSE_S ) ) ) 
716         {
717             p_intf->p_sys->b_play_status =
718                 !p_intf->p_sys->b_play_status;
719
720             [self playStatusUpdated: p_intf->p_sys->b_play_status]; 
721         }
722
723         if( p_input->stream.b_seekable )
724         {
725             if( f_slider == f_slider_old )
726             {
727                 float f_updated = ( 10000. * p_area->i_tell ) /
728                                            p_area->i_size;
729
730                 if( f_slider != f_updated )
731                 {
732                     [o_timeslider setFloatValue: f_updated];
733                 }
734             }
735             else
736             {
737                 off_t i_seek = ( f_slider * p_area->i_size ) / 10000;
738
739                 /* release the lock to be able to seek */
740                 vlc_mutex_unlock( &p_input->stream.stream_lock );
741                 input_Seek( p_input, i_seek, INPUT_SEEK_SET );
742                 vlc_mutex_lock( &p_input->stream.stream_lock );
743
744                 /* update the old value */
745                 f_slider_old = f_slider; 
746
747                 b_field_update = VLC_FALSE;
748             }
749         }
750
751         if( b_field_update )
752         {
753             NSString * o_time;
754             char psz_time[ OFFSETTOTIME_MAX_SIZE ];
755
756             input_OffsetToTime( p_input, psz_time, p_area->i_tell );
757
758             o_time = [NSString stringWithCString: psz_time];
759             [o_timefield setStringValue: o_time];
760         }
761
762         /* disable screen saver */
763         UpdateSystemActivity( UsrActivity );
764     }
765
766 #undef p_area
767
768     if( p_input != NULL )
769     {
770         vlc_mutex_unlock( &p_input->stream.stream_lock );
771     }
772
773 #undef p_input
774
775     vlc_mutex_unlock( &p_playlist->object_lock );
776     vlc_object_release( p_playlist );
777
778     [self updateMessageArray];
779
780     [NSTimer scheduledTimerWithTimeInterval: 0.5
781         target: self selector: @selector(manageIntf:)
782         userInfo: nil repeats: FALSE];
783 }
784
785 - (void)updateMessageArray
786 {
787     int i_start, i_stop;
788     intf_thread_t * p_intf = [NSApp getIntf];
789
790     vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
791     i_stop = *p_intf->p_sys->p_sub->pi_stop;
792     vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
793
794     if( p_intf->p_sys->p_sub->i_start != i_stop )
795     {
796         NSColor *o_white = [NSColor whiteColor];
797         NSColor *o_red = [NSColor redColor];
798         NSColor *o_yellow = [NSColor yellowColor];
799         NSColor *o_gray = [NSColor grayColor];
800
801         NSColor * pp_color[4] = { o_white, o_red, o_yellow, o_gray };
802         static const char * ppsz_type[4] = { ": ", " error: ",
803                                              " warning: ", " debug: " };
804
805         for( i_start = p_intf->p_sys->p_sub->i_start;
806              i_start != i_stop;
807              i_start = (i_start+1) % VLC_MSG_QSIZE )
808         {
809             NSString *o_msg;
810             NSDictionary *o_attr;
811             NSAttributedString *o_msg_color;
812
813             int i_type = p_intf->p_sys->p_sub->p_msg[i_start].i_type;
814
815             [o_msg_lock lock];
816
817             if( [o_msg_arr count] + 2 > 400 )
818             {
819                 unsigned rid[] = { 0, 1 };
820                 [o_msg_arr removeObjectsFromIndices: (unsigned *)&rid
821                            numIndices: sizeof(rid)/sizeof(rid[0])];
822             }
823
824             o_attr = [NSDictionary dictionaryWithObject: o_gray
825                 forKey: NSForegroundColorAttributeName];
826             o_msg = [NSString stringWithFormat: @"%s%s",
827                 p_intf->p_sys->p_sub->p_msg[i_start].psz_module,
828                 ppsz_type[i_type]];
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_attr = [NSDictionary dictionaryWithObject: pp_color[i_type]
834                 forKey: NSForegroundColorAttributeName];
835             o_msg = [NSString stringWithFormat: @"%s\n",
836                 p_intf->p_sys->p_sub->p_msg[i_start].psz_msg];
837             o_msg_color = [[NSAttributedString alloc]
838                 initWithString: o_msg attributes: o_attr];
839             [o_msg_arr addObject: [o_msg_color autorelease]];
840
841             [o_msg_lock unlock];
842
843             if( i_type == 1 )
844             {
845                 NSString *o_my_msg = [NSString stringWithFormat: @"%s: %s\n",
846                     p_intf->p_sys->p_sub->p_msg[i_start].psz_module,
847                     p_intf->p_sys->p_sub->p_msg[i_start].psz_msg];
848
849                 NSRange s_r = NSMakeRange( [[o_err_msg string] length], 0 );
850                 [o_err_msg setEditable: YES];
851                 [o_err_msg setSelectedRange: s_r];
852                 [o_err_msg insertText: o_my_msg];
853
854                 [o_error makeKeyAndOrderFront: self];
855                 [o_err_msg setEditable: NO];
856             }
857         }
858
859         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
860         p_intf->p_sys->p_sub->i_start = i_start;
861         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
862     }
863 }
864
865 - (void)playStatusUpdated:(BOOL)b_pause
866 {
867     if( b_pause )
868     {
869         [o_btn_play setImage: o_img_pause];
870         [o_btn_play setToolTip: _NS("Pause")];
871         [o_mi_play setTitle: _NS("Pause")];
872         [o_dmi_play setTitle: _NS("Pause")];
873     }
874     else
875     {
876         [o_btn_play setImage: o_img_play];
877         [o_btn_play setToolTip: _NS("Play")];
878         [o_mi_play setTitle: _NS("Play")];
879         [o_dmi_play setTitle: _NS("Play")];
880     }
881 }
882
883 - (void)setSubmenusEnabled:(BOOL)b_enabled
884 {
885     [o_mi_program setEnabled: b_enabled];
886     [o_mi_title setEnabled: b_enabled];
887     [o_mi_chapter setEnabled: b_enabled];
888     [o_mi_language setEnabled: b_enabled];
889     [o_mi_subtitle setEnabled: b_enabled];
890     [o_mi_channels setEnabled: b_enabled];
891     [o_mi_device setEnabled: b_enabled];
892     [o_mi_screen setEnabled: b_enabled];
893 }
894
895 - (void)manageVolumeSlider
896 {
897     audio_volume_t i_volume;
898     intf_thread_t * p_intf = [NSApp getIntf];
899
900     aout_VolumeGet( p_intf, &i_volume );
901
902     [o_volumeslider setFloatValue: (float)i_volume / AOUT_VOLUME_STEP]; 
903     [o_volumeslider setEnabled: 1];
904
905     p_intf->p_sys->b_mute = ( i_volume == 0 );
906 }
907
908 - (void)terminate
909 {
910     NSEvent * o_event;
911     vout_thread_t * p_vout;
912     playlist_t * p_playlist;
913     intf_thread_t * p_intf = [NSApp getIntf];
914
915     /*
916      * Free playlists
917      */
918     msg_Dbg( p_intf, "removing all playlists" );
919     while( (p_playlist = vlc_object_find( p_intf->p_vlc, VLC_OBJECT_PLAYLIST,
920                                           FIND_CHILD )) )
921     {
922         vlc_object_detach( p_playlist );
923         vlc_object_release( p_playlist );
924         playlist_Destroy( p_playlist );
925     }
926
927     /*
928      * Free video outputs
929      */
930     msg_Dbg( p_intf, "removing all video outputs" );
931     while( (p_vout = vlc_object_find( p_intf->p_vlc, 
932                                       VLC_OBJECT_VOUT, FIND_CHILD )) )
933     {
934         vlc_object_detach( p_vout );
935         vlc_object_release( p_vout );
936         vout_Destroy( p_vout );
937     }
938
939     if( o_img_pause != nil )
940     {
941         [o_img_pause release];
942         o_img_pause = nil;
943     }
944
945     if( o_img_play != nil )
946     {
947         [o_img_play release];
948         o_img_play = nil;
949     }
950
951     if( o_msg_arr != nil )
952     {
953         [o_msg_arr removeAllObjects];
954         [o_msg_arr release];
955         o_msg_arr = nil;
956     }
957
958     if( o_msg_lock != nil )
959     {
960         [o_msg_lock release];
961         o_msg_lock = nil;
962     }
963
964     if( o_prefs != nil )
965     {
966         [o_prefs release];
967         o_prefs = nil;
968     }
969
970     [NSApp stop: nil];
971
972     /* write cached user defaults to disk */
973     [[NSUserDefaults standardUserDefaults] synchronize];
974
975     /* send a dummy event to break out of the event loop */
976     o_event = [NSEvent mouseEventWithType: NSLeftMouseDown
977                 location: NSMakePoint( 1, 1 ) modifierFlags: 0
978                 timestamp: 1 windowNumber: [[NSApp mainWindow] windowNumber]
979                 context: [NSGraphicsContext currentContext] eventNumber: 1
980                 clickCount: 1 pressure: 0.0];
981     [NSApp postEvent: o_event atStart: YES];
982 }
983
984 - (void)setupMenus:(input_thread_t *)p_input
985 {
986     unsigned int i, i_nb_items;
987     NSMenuItem * o_item;
988     NSString * o_menu_title;
989     char psz_title[ 256 ];
990
991     es_descriptor_t * p_audio_es = NULL;
992     es_descriptor_t * p_spu_es = NULL;
993
994     intf_thread_t * p_intf = [NSApp getIntf];
995
996     p_intf->p_sys->b_chapter_update |= p_intf->p_sys->b_title_update;
997     p_intf->p_sys->b_audio_update |= p_intf->p_sys->b_title_update |
998                                      p_intf->p_sys->b_program_update;
999     p_intf->p_sys->b_spu_update |= p_intf->p_sys->b_title_update |
1000                                    p_intf->p_sys->b_program_update;
1001
1002     if( p_intf->p_sys->b_program_update )
1003     {
1004         NSMenu * o_program;
1005         SEL pf_toggle_program;
1006         pgrm_descriptor_t * p_pgrm;
1007
1008         if( p_input->stream.p_new_program )
1009         {
1010             p_pgrm = p_input->stream.p_new_program;
1011         }
1012         else
1013         {
1014             p_pgrm = p_input->stream.p_selected_program;
1015         }
1016
1017         o_program = [o_mi_program submenu];
1018         pf_toggle_program = @selector(toggleProgram:);
1019
1020         /* remove previous program items */
1021         i_nb_items = [o_program numberOfItems];
1022         for( i = 0; i < i_nb_items; i++ )
1023         {
1024             [o_program removeItemAtIndex: 0];
1025         }
1026
1027         /* make (un)sensitive */
1028         [o_mi_program setEnabled: 
1029             p_input->stream.i_pgrm_number > 1];
1030
1031         /* add program items */
1032         for( i = 0 ; i < p_input->stream.i_pgrm_number ; i++ )
1033         {
1034             snprintf( psz_title, sizeof(psz_title), "id %d",
1035                 p_input->stream.pp_programs[i]->i_number );
1036             psz_title[sizeof(psz_title) - 1] = '\0';
1037
1038             o_menu_title = [NSApp localizedString: psz_title];
1039
1040             o_item = [o_program addItemWithTitle: o_menu_title
1041                 action: pf_toggle_program keyEquivalent: @""];
1042             [o_item setTag: p_input->stream.pp_programs[i]->i_number];
1043             [o_item setTarget: o_controls];
1044
1045             if( p_pgrm == p_input->stream.pp_programs[i] )
1046             {
1047                 [o_item setState: NSOnState];
1048             }
1049         }
1050
1051         p_intf->p_sys->b_program_update = 0;
1052     }
1053
1054     if( p_intf->p_sys->b_title_update )
1055     {
1056         NSMenu * o_title;
1057         SEL pf_toggle_title;
1058
1059         o_title = [o_mi_title submenu];
1060         pf_toggle_title = @selector(toggleTitle:);
1061
1062         /* remove previous title items */
1063         i_nb_items = [o_title numberOfItems];
1064         for( i = 0; i < i_nb_items; i++ )
1065         {
1066             [o_title removeItemAtIndex: 0];
1067         }
1068
1069         /* make (un)sensitive */
1070         [o_mi_title setEnabled: 
1071             p_input->stream.i_area_nb > 1];
1072
1073         /* add title items */
1074         for( i = 1 ; i < p_input->stream.i_area_nb ; i++ )
1075         {
1076             snprintf( psz_title, sizeof(psz_title), "Title %d (%d)", i,
1077                 p_input->stream.pp_areas[i]->i_part_nb );
1078             psz_title[sizeof(psz_title) - 1] = '\0';
1079
1080             o_menu_title = [NSApp localizedString: psz_title];
1081
1082             o_item = [o_title addItemWithTitle: o_menu_title
1083                 action: pf_toggle_title keyEquivalent: @""];
1084             [o_item setTag: i];
1085             [o_item setTarget: o_controls];
1086
1087             if( ( p_input->stream.pp_areas[i] ==
1088                 p_input->stream.p_selected_area ) )
1089             {
1090                 [o_item setState: NSOnState];
1091             }
1092         }
1093
1094         p_intf->p_sys->b_title_update = 0;
1095     }
1096
1097     if( p_intf->p_sys->b_chapter_update )
1098     {
1099         NSMenu * o_chapter;
1100         SEL pf_toggle_chapter;
1101
1102         o_chapter = [o_mi_chapter submenu];
1103         pf_toggle_chapter = @selector(toggleChapter:);
1104
1105         /* remove previous chapter items */
1106         i_nb_items = [o_chapter numberOfItems];
1107         for( i = 0; i < i_nb_items; i++ )
1108         {
1109             [o_chapter removeItemAtIndex: 0];
1110         }
1111
1112         /* make (un)sensitive */
1113         [o_mi_chapter setEnabled: 
1114             p_input->stream.p_selected_area->i_part_nb > 1];
1115
1116         /* add chapter items */
1117         for( i = 0 ; i < p_input->stream.p_selected_area->i_part_nb ; i++ )
1118         {
1119             snprintf( psz_title, sizeof(psz_title), "Chapter %d", i + 1 );
1120             psz_title[sizeof(psz_title) - 1] = '\0';
1121
1122             o_menu_title = [NSApp localizedString: psz_title];
1123
1124             o_item = [o_chapter addItemWithTitle: o_menu_title
1125                 action: pf_toggle_chapter keyEquivalent: @""];
1126             [o_item setTag: i + 1];
1127             [o_item setTarget: o_controls];
1128
1129             if( ( p_input->stream.p_selected_area->i_part == i + 1 ) )
1130             {
1131                 [o_item setState: NSOnState];
1132             }
1133         }
1134
1135         p_intf->p_sys->i_part =
1136                 p_input->stream.p_selected_area->i_part;
1137
1138         p_intf->p_sys->b_chapter_update = 0;
1139     }
1140
1141     for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
1142     {
1143         if( p_input->stream.pp_selected_es[i]->i_cat == AUDIO_ES )
1144         {
1145             p_audio_es = p_input->stream.pp_selected_es[i];
1146         }
1147         else if( p_input->stream.pp_selected_es[i]->i_cat == SPU_ES )
1148         {
1149             p_spu_es = p_input->stream.pp_selected_es[i];
1150         }
1151     }
1152
1153     if( p_intf->p_sys->b_audio_update )
1154     {
1155         [self setupLangMenu: p_input mi: o_mi_language es: p_audio_es
1156             category: AUDIO_ES selector: @selector(toggleLanguage:)];
1157
1158         p_intf->p_sys->b_audio_update = 0;
1159     }
1160
1161     if( p_intf->p_sys->b_spu_update )
1162     {
1163         [self setupLangMenu: p_input mi: o_mi_subtitle es: p_spu_es
1164             category: SPU_ES selector: @selector(toggleLanguage:)];
1165
1166         p_intf->p_sys->b_spu_update = 0;
1167     }
1168
1169     if ( p_intf->p_sys->b_aout_update )
1170     {
1171         aout_instance_t * p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT,
1172                                                     FIND_ANYWHERE );
1173
1174         if ( p_aout != NULL )
1175         {
1176             vlc_value_t val;
1177             val.b_bool = 0;
1178
1179             var_Set( (vlc_object_t *)p_aout, "intf-change", val );
1180
1181             [self setupVarMenu: o_mi_channels target: (vlc_object_t *)p_aout
1182                 var: "audio-channels" selector: @selector(toggleVar:)];
1183
1184             [self setupVarMenu: o_mi_device target: (vlc_object_t *)p_aout
1185                 var: "audio-device" selector: @selector(toggleVar:)];
1186
1187             vlc_object_release( (vlc_object_t *)p_aout );
1188         }
1189
1190         p_intf->p_sys->b_aout_update = 0;
1191     }
1192
1193     if( p_intf->p_sys->b_vout_update )
1194     {
1195         vout_thread_t * p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
1196                                                           FIND_ANYWHERE );
1197
1198         if ( p_vout != NULL )
1199         {
1200             vlc_value_t val;
1201             val.b_bool = 0;
1202
1203             var_Set( (vlc_object_t *)p_vout, "intf-change", val );
1204
1205             [self setupVarMenu: o_mi_screen target: (vlc_object_t *)p_vout
1206                 var: "video-device" selector: @selector(toggleVar:)];
1207
1208             vlc_object_release( (vlc_object_t *)p_vout );
1209
1210             [o_mi_close_window setEnabled: TRUE];
1211         }
1212
1213         p_intf->p_sys->b_vout_update = 0;
1214     }
1215
1216 #undef p_input
1217 }
1218
1219 - (void)setupLangMenu:(input_thread_t *)p_input
1220                       mi:(NSMenuItem *)o_mi
1221                       es:(es_descriptor_t *)p_es
1222                       category:(int)i_cat
1223                       selector:(SEL)pf_callback
1224 {
1225     unsigned int i, i_nb_items;
1226     NSMenu * o_menu = [o_mi submenu];
1227
1228     /* remove previous language items */
1229     i_nb_items = [o_menu numberOfItems];
1230     for( i = 0; i < i_nb_items; i++ )
1231     {
1232         [o_menu removeItemAtIndex: 0];
1233     }
1234
1235     /* make sensitive : we can't change it after we build the menu, and
1236      * before, we don't yet how many items we will have. So make it
1237      * always sensitive. --Meuuh */
1238     [o_mi setEnabled: TRUE];
1239
1240 #if 0
1241     /* We do not use this code, because you need to start stop .avi for
1242      * it to work, so not very useful now  --hartman */
1243     if ( o_mi == o_mi_subtitle ) {
1244         NSLog(@"testing");
1245         [o_mi setEnabled: TRUE ];
1246         NSMenuItem * o_lmi;
1247         NSString * o_title;
1248         o_title = _NS("Load from file..");
1249         o_lmi = [o_menu addItemWithTitle: o_title
1250                  action: pf_callback keyEquivalent: @""];
1251         [o_lmi setTag: 2000];
1252         [o_lmi setTarget: o_controls];
1253     }
1254 #endif
1255
1256 #define ES p_input->stream.pp_es[i]
1257     for( i = 0 ; i < p_input->stream.i_es_number ; i++ )
1258     {
1259         if( ( ES->i_cat == i_cat ) &&
1260             ( !ES->p_pgrm ||
1261               ES->p_pgrm ==
1262                  p_input->stream.p_selected_program ) )
1263         {
1264             NSMenuItem * o_lmi;
1265             NSString * o_title;
1266
1267             if( *ES->psz_desc )
1268             {
1269                 o_title = [NSApp localizedString: ES->psz_desc];
1270             }
1271             else
1272             {
1273                 char psz_title[ 256 ];
1274
1275                 snprintf( psz_title, sizeof(psz_title), _("Language 0x%x"),
1276                           ES->i_id );
1277                 psz_title[sizeof(psz_title) - 1] = '\0';
1278
1279                 o_title = [NSApp localizedString: psz_title];
1280             }
1281
1282             o_lmi = [o_menu addItemWithTitle: o_title
1283                 action: pf_callback keyEquivalent: @""];
1284             [o_lmi setRepresentedObject: 
1285                 [NSValue valueWithPointer: ES]];
1286             [o_lmi setTarget: o_controls];
1287             [o_lmi setTag: i_cat];
1288
1289             if( /*p_es == ES*/ ES->p_decoder_fifo != NULL )
1290             {
1291                 [o_lmi setState: NSOnState];
1292             }
1293         }
1294     }
1295 #undef ES
1296 }
1297
1298 - (void)setupVarMenu:(NSMenuItem *)o_mi
1299                      target:(vlc_object_t *)p_object
1300                      var:(const char *)psz_variable
1301                      selector:(SEL)pf_callback
1302 {
1303     int i, i_nb_items;
1304     NSMenu * o_menu = [o_mi submenu];
1305     vlc_value_t val;
1306     char * psz_value;
1307
1308     /* remove previous items */
1309     i_nb_items = [o_menu numberOfItems];
1310     for( i = 0; i < i_nb_items; i++ )
1311     {
1312         [o_menu removeItemAtIndex: 0];
1313     }
1314
1315     if ( var_Get( p_object, psz_variable, &val ) < 0 )
1316     {
1317         return;
1318     }
1319     psz_value = val.psz_string;
1320
1321     if ( var_Change( p_object, psz_variable,
1322                      VLC_VAR_GETLIST, &val ) < 0 )
1323     {
1324         free( psz_value );
1325         return;
1326     }
1327
1328     /* make (un)sensitive */
1329     [o_mi setEnabled: ( val.p_list->i_count > 0 )];
1330
1331     for ( i = 0; i < val.p_list->i_count; i++ )
1332     {
1333         NSMenuItem * o_lmi;
1334         NSString * o_title;
1335
1336         o_title = [NSApp localizedString: val.p_list->p_values[i].psz_string];
1337         o_lmi = [o_menu addItemWithTitle: o_title
1338                  action: pf_callback keyEquivalent: @""];
1339         /* FIXME: this isn't 64-bit clean ! */
1340         [o_lmi setTag: (int)psz_variable];
1341         [o_lmi setRepresentedObject:
1342             [NSValue valueWithPointer: p_object]];
1343         [o_lmi setTarget: o_controls];
1344
1345         if ( !strcmp( psz_value, val.p_list->p_values[i].psz_string ) )
1346             [o_lmi setState: NSOnState];
1347     }
1348
1349     var_Change( p_object, psz_variable, VLC_VAR_FREELIST,
1350                 &val );
1351
1352     free( psz_value );
1353 }
1354
1355 - (IBAction)clearRecentItems:(id)sender
1356 {
1357     [[NSDocumentController sharedDocumentController]
1358                           clearRecentDocuments: nil];
1359 }
1360
1361 - (void)openRecentItem:(id)sender
1362 {
1363     [self application: nil openFile: [sender title]]; 
1364 }
1365
1366 - (IBAction)viewPreferences:(id)sender
1367 {
1368     if( o_prefs == nil )
1369     {
1370         o_prefs = [[VLCPrefs alloc] init];
1371     }
1372
1373     [o_prefs createPrefPanel: @"main"];
1374 }
1375
1376 - (IBAction)timesliderUpdate:(id)sender
1377 {
1378     float f_updated;
1379
1380     switch( [[NSApp currentEvent] type] )
1381     {
1382         case NSLeftMouseUp:
1383         case NSLeftMouseDown:
1384             f_slider = [sender floatValue];
1385             return;
1386
1387         case NSLeftMouseDragged:
1388             f_updated = [sender floatValue];
1389             break;
1390
1391         default:
1392             return;
1393     }
1394
1395     intf_thread_t * p_intf = [NSApp getIntf];
1396
1397     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1398                                                        FIND_ANYWHERE );
1399
1400     if( p_playlist == NULL )
1401     {
1402         return;
1403     }
1404
1405     vlc_mutex_lock( &p_playlist->object_lock );
1406
1407     if( p_playlist->p_input != NULL )
1408     {
1409         off_t i_tell;
1410         NSString * o_time;
1411         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
1412
1413 #define p_area p_playlist->p_input->stream.p_selected_area
1414         vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
1415         i_tell = f_updated / 10000. * p_area->i_size;
1416         input_OffsetToTime( p_playlist->p_input, psz_time, i_tell );
1417         vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
1418 #undef p_area
1419
1420         o_time = [NSString stringWithCString: psz_time];
1421         [o_timefield setStringValue: o_time]; 
1422     }
1423
1424     vlc_mutex_unlock( &p_playlist->object_lock );
1425     vlc_object_release( p_playlist );
1426 }
1427
1428 - (IBAction)closeError:(id)sender
1429 {
1430     [o_err_msg setString: @""];
1431     [o_error performClose: self];
1432 }
1433
1434 - (IBAction)openReadMe:(id)sender
1435 {
1436     NSString * o_path = [[NSBundle mainBundle] 
1437         pathForResource: @"README.MacOSX" ofType: @"rtf"]; 
1438
1439     [[NSWorkspace sharedWorkspace] openFile: o_path 
1440                                    withApplication: @"TextEdit"];
1441 }
1442
1443 - (IBAction)reportABug:(id)sender
1444 {
1445     NSURL * o_url = [NSURL URLWithString: 
1446         @"http://www.videolan.org/support/bug-reporting.html"];
1447
1448     [[NSWorkspace sharedWorkspace] openURL: o_url];
1449 }
1450
1451 - (IBAction)openWebsite:(id)sender
1452 {
1453     NSURL * o_url = [NSURL URLWithString: @"http://www.videolan.org"];
1454
1455     [[NSWorkspace sharedWorkspace] openURL: o_url];
1456 }
1457
1458 - (IBAction)openLicense:(id)sender
1459 {
1460     NSString * o_path = [[NSBundle mainBundle] 
1461         pathForResource: @"COPYING" ofType: nil];
1462
1463     [[NSWorkspace sharedWorkspace] openFile: o_path 
1464                                    withApplication: @"TextEdit"];
1465 }
1466
1467 - (IBAction)openCrashLog:(id)sender
1468 {
1469     NSString * o_path = [@"~/Library/Logs/CrashReporter/VLC.crash.log"
1470                                     stringByExpandingTildeInPath]; 
1471
1472     
1473     if ( [[NSFileManager defaultManager] fileExistsAtPath: o_path ] )
1474     {
1475         [[NSWorkspace sharedWorkspace] openFile: o_path 
1476                                     withApplication: @"Console"];
1477     }
1478     else
1479     {
1480         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.") );
1481
1482     }
1483 }
1484
1485 - (void)windowDidBecomeKey:(NSNotification *)o_notification
1486 {
1487     if( [o_notification object] == o_msgs_panel )
1488     {
1489         id o_msg;
1490         NSEnumerator * o_enum;
1491
1492         [o_messages setString: @""]; 
1493
1494         [o_msg_lock lock];
1495
1496         o_enum = [o_msg_arr objectEnumerator];
1497
1498         while( ( o_msg = [o_enum nextObject] ) != nil )
1499         {
1500             [o_messages insertText: o_msg];
1501         }
1502
1503         [o_msg_lock unlock];
1504     }
1505 }
1506
1507 @end
1508
1509 @implementation VLCMain (NSMenuValidation)
1510
1511 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
1512 {
1513     BOOL bEnabled = TRUE;
1514
1515     /* Recent Items Menu */
1516
1517     if( [[o_mi title] isEqualToString: _NS("Clear Menu")] )
1518     {
1519         NSMenu * o_menu = [o_mi_open_recent submenu];
1520         int i_nb_items = [o_menu numberOfItems];
1521         NSArray * o_docs = [[NSDocumentController sharedDocumentController]
1522                                                        recentDocumentURLs];
1523         UInt32 i_nb_docs = [o_docs count];
1524
1525         if( i_nb_items > 1 )
1526         {
1527             while( --i_nb_items )
1528             {
1529                 [o_menu removeItemAtIndex: 0];
1530             }
1531         }
1532
1533         if( i_nb_docs > 0 )
1534         {
1535             NSURL * o_url;
1536             NSString * o_doc;
1537
1538             [o_menu insertItem: [NSMenuItem separatorItem] atIndex: 0];
1539
1540             while( TRUE )
1541             {
1542                 i_nb_docs--;
1543
1544                 o_url = [o_docs objectAtIndex: i_nb_docs];
1545
1546                 if( [o_url isFileURL] )
1547                 {
1548                     o_doc = [o_url path];
1549                 }
1550                 else
1551                 {
1552                     o_doc = [o_url absoluteString];
1553                 }
1554
1555                 [o_menu insertItemWithTitle: o_doc
1556                     action: @selector(openRecentItem:)
1557                     keyEquivalent: @"" atIndex: 0]; 
1558
1559                 if( i_nb_docs == 0 )
1560                 {
1561                     break;
1562                 }
1563             } 
1564         }
1565         else
1566         {
1567             bEnabled = FALSE;
1568         }
1569     }
1570
1571     return( bEnabled );
1572 }
1573
1574 @end
1575
1576 @implementation VLCMain (Internal)
1577
1578 - (void)handlePortMessage:(NSPortMessage *)o_msg
1579 {
1580     id ** val;
1581     NSData * o_data;
1582     NSValue * o_value;
1583     NSInvocation * o_inv;
1584     NSConditionLock * o_lock;
1585  
1586     o_data = [[o_msg components] lastObject];
1587     o_inv = *((NSInvocation **)[o_data bytes]); 
1588     [o_inv getArgument: &o_value atIndex: 2];
1589     val = (id **)[o_value pointerValue];
1590     [o_inv setArgument: val[1] atIndex: 2];
1591     o_lock = *(val[0]);
1592
1593     [o_lock lock];
1594     [o_inv invoke];
1595     [o_lock unlockWithCondition: 1];
1596 }
1597
1598 @end