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