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