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