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