]> git.sesse.net Git - vlc/blob - modules/gui/macosx/intf.m
* core/playlist: PLAYLIST_PAUSE, playlist_IsPlaying, playlist_IsEmpty
[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.38 2003/01/29 11:34:11 jlj 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     [o_window setTitle: _NS("VLC - Controller")];
191     [o_window setExcludedFromWindowsMenu: TRUE];
192
193     /* button controls */
194     [o_btn_playlist setToolTip: _NS("Playlist")];
195     [o_btn_prev setToolTip: _NS("Previous")];
196     [o_btn_slowmotion setToolTip: _NS("Slowmotion")];
197     [o_btn_play setToolTip: _NS("Play")];
198     [o_btn_stop setToolTip: _NS("Stop")];
199     [o_btn_fastforward setToolTip: _NS("Fast Forward")];
200     [o_btn_fastforward setPeriodicDelay: 0.0 interval: 1];
201     [o_btn_next setToolTip: _NS("Next")];
202     [o_btn_prefs setToolTip: _NS("Preferences")];
203     [o_volumeslider setToolTip: _NS("Volume")];
204     [o_timeslider setToolTip: _NS("Position")];
205     
206     /* messages panel */ 
207     [o_msgs_panel setDelegate: self];
208     [o_msgs_panel setTitle: _NS("Messages")];
209     [o_msgs_panel setExcludedFromWindowsMenu: TRUE];
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")];
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     [o_mu_help setTitle: _NS("Help")];
271     [o_mi_readme setTitle: _NS("ReadMe...")];
272     [o_mi_reportabug setTitle: _NS("Report A Bug")];
273     [o_mi_website setTitle: _NS("VideoLAN Website")];
274     [o_mi_license setTitle: _NS("License")];
275
276     /* dock menu */
277     [o_dmi_play setTitle: _NS("Play")];
278     [o_dmi_stop setTitle: _NS("Stop")];
279
280     /* error panel */
281     [o_error setTitle: _NS("Error")];
282     [o_err_lbl setStringValue: _NS("An error has occurred which probably prevented the execution of your request :")];
283     [o_err_bug_lbl setStringValue: _NS("If you believe that it is a bug, please follow the instructions at :")];
284     [o_err_btn_msgs setTitle: _NS("Open Messages Window")];
285     [o_err_btn_dismiss setTitle: _NS("Dismiss")];
286
287     [self manageMode];
288 }
289
290 - (void)applicationWillFinishLaunching:(NSNotification *)o_notification
291 {
292     intf_thread_t * p_intf = [NSApp getIntf];
293
294     f_slider_old = f_slider = 0.0;
295
296     o_msg_lock = [[NSLock alloc] init];
297     o_msg_arr = [[NSMutableArray arrayWithCapacity: 200] retain];
298
299     [NSThread detachNewThreadSelector: @selector(manage)
300         toTarget: self withObject: nil];
301
302     [p_intf->p_sys->o_sendport setDelegate: self];
303     [[NSRunLoop currentRunLoop] 
304         addPort: p_intf->p_sys->o_sendport
305         forMode: NSDefaultRunLoopMode];
306 }
307
308 - (BOOL)application:(NSApplication *)o_app openFile:(NSString *)o_filename
309 {
310     [o_playlist appendArray:
311         [NSArray arrayWithObject: o_filename] atPos: -1 enqueue: NO];
312
313     return( TRUE );
314 }
315
316 - (void)manage
317 {
318     NSDate * o_sleep_date;
319     intf_thread_t * p_intf = [NSApp getIntf];
320     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
321
322     while( !p_intf->b_die )
323     {
324         int i_start, i_stop;
325
326         vlc_mutex_lock( &p_intf->change_lock );
327
328         /* update the input */
329         if( p_intf->p_sys->p_input == NULL )
330         {
331             p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
332                                                               FIND_ANYWHERE );
333             [self setControlItems];
334         }
335         else if( p_intf->p_sys->p_input->b_dead )
336         {
337             vlc_object_release( p_intf->p_sys->p_input );
338             p_intf->p_sys->p_input = NULL;
339
340             if( p_intf->p_sys->b_stopping )
341             {
342                 vout_thread_t * p_vout = vlc_object_find( p_intf, 
343                                                           VLC_OBJECT_VOUT,
344                                                           FIND_ANYWHERE );
345
346                 if( p_vout != NULL )
347                 {
348                     vlc_object_detach( p_vout );
349                     vlc_object_release( p_vout );
350                     vout_Destroy( p_vout );
351                 }
352                 
353                 p_intf->p_sys->b_stopping = 0;
354                 [self setControlItems];
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             [self setControlItems];
420                         
421             vlc_mutex_unlock( &p_input->stream.stream_lock );
422         }
423         else if( p_intf->p_sys->b_playing && !p_intf->b_die )
424         {
425             [self displayTime];
426             [self manageMode];
427             p_intf->p_sys->b_playing = 0;
428         }
429
430         /* update the log window */
431         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
432         i_stop = *p_intf->p_sys->p_sub->pi_stop;
433         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
434
435         if( p_intf->p_sys->p_sub->i_start != i_stop )
436         {
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             NSColor * pp_color[4] = { o_white, o_red, o_yellow, o_gray };
443             static const char * ppsz_type[4] = { ": ", " error: ", 
444                                                  " warning: ", " debug: " }; 
445
446             for( i_start = p_intf->p_sys->p_sub->i_start;
447                  i_start != i_stop;
448                  i_start = (i_start+1) % VLC_MSG_QSIZE )
449             {
450                 NSString *o_msg;
451                 NSDictionary *o_attr;
452                 NSAttributedString *o_msg_color;
453
454                 int i_type = p_intf->p_sys->p_sub->p_msg[i_start].i_type;
455
456                 [o_msg_lock lock];
457
458                 if( [o_msg_arr count] + 2 > 200 )
459                 {
460                     unsigned rid[] = { 0, 1 };
461                     [o_msg_arr removeObjectsFromIndices: (unsigned *)&rid
462                                numIndices: sizeof(rid)/sizeof(rid[0])];
463                 }
464
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_msg_arr addObject: [o_msg_color autorelease]];
473
474                 o_attr = [NSDictionary dictionaryWithObject: pp_color[i_type]
475                     forKey: NSForegroundColorAttributeName];
476                 o_msg = [NSString stringWithFormat: @"%s\n",
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_msg_arr addObject: [o_msg_color autorelease]];
481
482                 [o_msg_lock unlock];
483
484                 if ( i_type == 1 )
485                 {
486                     /* Error panel */
487                     NSString *o_my_msg =
488                         [NSString stringWithFormat: @"%s: %s\n",
489                          p_intf->p_sys->p_sub->p_msg[i_start].psz_module,
490                          p_intf->p_sys->p_sub->p_msg[i_start].psz_msg];
491
492                     [o_err_msg setEditable: YES];
493                     [o_err_msg setSelectedRange:
494                                 NSMakeRange( [[o_err_msg string] length], 0 )];
495                     [o_err_msg insertText: o_my_msg];
496
497                     [o_error makeKeyAndOrderFront: self];
498                     [o_err_msg setEditable: NO];
499                 }
500             }
501
502             vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
503             p_intf->p_sys->p_sub->i_start = i_start;
504             vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
505         }
506
507         vlc_mutex_unlock( &p_intf->change_lock );
508
509         o_sleep_date = [NSDate dateWithTimeIntervalSinceNow: 0.1];
510         [NSThread sleepUntilDate: o_sleep_date];
511     }
512
513     [self terminate];
514
515     [o_pool release];
516 }
517
518 - (void)terminate
519 {
520     NSEvent * pEvent;
521     vout_thread_t * p_vout;
522     playlist_t * p_playlist;
523     intf_thread_t * p_intf = [NSApp getIntf];
524
525     /* release input */
526     if( p_intf->p_sys->p_input )
527     {
528         vlc_object_release( p_intf->p_sys->p_input );
529         p_intf->p_sys->p_input = NULL;
530     }
531
532     /*
533      * Free playlists
534      */
535     msg_Dbg( p_intf, "removing all playlists" );
536     while( (p_playlist = vlc_object_find( p_intf->p_vlc, VLC_OBJECT_PLAYLIST,
537                                           FIND_CHILD )) )
538     {
539         vlc_object_detach( p_playlist );
540         vlc_object_release( p_playlist );
541         playlist_Destroy( p_playlist );
542     }
543
544     /*
545      * Free video outputs
546      */
547     msg_Dbg( p_intf, "removing all video outputs" );
548     while( (p_vout = vlc_object_find( p_intf->p_vlc, 
549                                       VLC_OBJECT_VOUT, FIND_CHILD )) )
550     {
551         vlc_object_detach( p_vout );
552         vlc_object_release( p_vout );
553         vout_Destroy( p_vout );
554     }
555
556     if( o_msg_arr != nil )
557     {
558         [o_msg_arr removeAllObjects];
559         [o_msg_arr release];
560         o_msg_arr = nil;
561     }
562
563     if( o_msg_lock != nil )
564     {
565         [o_msg_lock release];
566         o_msg_lock = nil;
567     }
568
569     if( o_prefs != nil )
570     {
571         [o_prefs release];
572         o_prefs = nil;
573     }
574
575     [NSApp stop: nil];
576
577     /* write cached user defaults to disk */
578     [[NSUserDefaults standardUserDefaults] synchronize];
579
580     /* send a dummy event to break out of the event loop */
581     pEvent = [NSEvent mouseEventWithType: NSLeftMouseDown
582                 location: NSMakePoint( 1, 1 ) modifierFlags: 0
583                 timestamp: 1 windowNumber: [[NSApp mainWindow] windowNumber]
584                 context: [NSGraphicsContext currentContext] eventNumber: 1
585                 clickCount: 1 pressure: 0.0];
586     [NSApp postEvent: pEvent atStart: YES];
587 }
588
589 - (void)manageMode
590 {
591     vlc_bool_t b_input;
592     vlc_bool_t b_control = 0;
593     intf_thread_t * p_intf = [NSApp getIntf];
594
595     if( ( b_input = ( p_intf->p_sys->p_input != NULL ) ) )
596     {
597         /* control buttons for free pace streams */
598         b_control = p_intf->p_sys->p_input->stream.b_pace_control;
599
600         /* get ready for menu regeneration */
601         p_intf->p_sys->b_program_update = 1;
602         p_intf->p_sys->b_title_update = 1;
603         p_intf->p_sys->b_chapter_update = 1;
604         p_intf->p_sys->b_audio_update = 1;
605         p_intf->p_sys->b_spu_update = 1;
606         p_intf->p_sys->i_part = 0;
607
608         p_intf->p_sys->p_input->stream.b_changed = 0;
609         msg_Dbg( p_intf, "stream has changed, refreshing interface" );
610     }
611     else
612     {
613         /* unsensitize menus */
614         [o_mi_program setEnabled: FALSE];
615         [o_mi_title setEnabled: FALSE];
616         [o_mi_chapter setEnabled: FALSE];
617         [o_mi_language setEnabled: FALSE];
618         [o_mi_subtitle setEnabled: FALSE];
619         [o_mi_channels setEnabled: FALSE];
620         [o_mi_device setEnabled: FALSE];
621         [o_mi_screen setEnabled: FALSE];
622         [o_mi_close_window setEnabled: FALSE];
623     }
624     [self setControlItems];
625 }
626
627 - (void)setControlItems {
628     intf_thread_t * p_intf = [NSApp getIntf];
629     vlc_bool_t b_input;
630     vlc_bool_t b_plmul = NO;
631     vlc_bool_t b_control = NO;
632     vlc_bool_t b_chapters = NO;
633     input_area_t *  p_area;
634     playlist_t * p_playlist = NULL;
635     NSImage *playImage = [NSImage imageNamed:@"play"];
636     NSImage *pauseImage = [NSImage imageNamed:@"pause"];
637     
638     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, 
639                                               FIND_ANYWHERE ); 
640     if( p_playlist != NULL )
641     {
642         vlc_mutex_lock( &p_playlist->object_lock );
643         b_plmul = p_playlist->i_size > 1;
644         vlc_mutex_unlock( &p_playlist->object_lock );
645         vlc_object_release( p_playlist );
646     }
647     
648     if ( b_input = ( p_intf->p_sys->p_input != NULL ) )
649     {
650         /* control buttons for free pace streams */
651         b_control = p_intf->p_sys->p_input->stream.b_pace_control;
652         p_area = p_intf->p_sys->p_input->stream.p_selected_area;
653         if ( p_area->i_part_nb > 1 )
654             b_chapters = YES;
655     }
656     
657     /* set control items */
658     [o_btn_stop setEnabled: b_input];
659     [o_btn_fastforward setEnabled: b_control];
660     [o_btn_slowmotion setEnabled: b_control];
661     [o_btn_prev setEnabled: (b_plmul || b_chapters) ];
662     [o_btn_next setEnabled: (b_plmul || b_chapters) ];
663     [o_controls setVolumeSlider];
664     [o_timeslider setEnabled: b_input];
665     
666     if ( p_intf->p_sys->p_input != NULL &&
667                 p_intf->p_sys->p_input->stream.control.i_status != PAUSE_S)
668     {
669         [o_btn_play setImage: pauseImage];
670         [o_btn_play setToolTip: _NS("Pause")];
671         [o_mi_play setTitle: _NS("Pause")];
672         [o_dmi_play setTitle: _NS("Pause")];
673     }
674     else
675     {
676         [o_btn_play setImage: playImage];
677         [o_btn_play setToolTip: _NS("Play")];
678         [o_mi_play setTitle: _NS("Play")];
679         [o_dmi_play setTitle: _NS("Play")];
680     }
681 }
682
683 - (void)setupMenus
684 {
685     unsigned int i, i_nb_items;
686     NSMenuItem * o_item;
687     NSString * o_menu_title;
688     char psz_title[ 256 ];
689
690     es_descriptor_t * p_audio_es = NULL;
691     es_descriptor_t * p_spu_es = NULL;
692
693     intf_thread_t * p_intf = [NSApp getIntf];
694
695     p_intf->p_sys->b_chapter_update |= p_intf->p_sys->b_title_update;
696     p_intf->p_sys->b_audio_update |= p_intf->p_sys->b_title_update |
697                                      p_intf->p_sys->b_program_update;
698     p_intf->p_sys->b_spu_update |= p_intf->p_sys->b_title_update |
699                                    p_intf->p_sys->b_program_update;
700
701 #define p_input (p_intf->p_sys->p_input)
702
703     if( p_intf->p_sys->b_program_update )
704     {
705         NSMenu * o_program;
706         SEL pf_toggle_program;
707         pgrm_descriptor_t * p_pgrm;
708
709         if( p_input->stream.p_new_program )
710         {
711             p_pgrm = p_input->stream.p_new_program;
712         }
713         else
714         {
715             p_pgrm = p_input->stream.p_selected_program;
716         }
717
718         o_program = [o_mi_program submenu];
719         pf_toggle_program = @selector(toggleProgram:);
720
721         /* remove previous program items */
722         i_nb_items = [o_program numberOfItems];
723         for( i = 0; i < i_nb_items; i++ )
724         {
725             [o_program removeItemAtIndex: 0];
726         }
727
728         /* make (un)sensitive */
729         [o_mi_program setEnabled: 
730             p_input->stream.i_pgrm_number > 1];
731
732         /* add program items */
733         for( i = 0 ; i < p_input->stream.i_pgrm_number ; i++ )
734         {
735             snprintf( psz_title, sizeof(psz_title), "id %d",
736                 p_input->stream.pp_programs[i]->i_number );
737             psz_title[sizeof(psz_title) - 1] = '\0';
738
739             o_menu_title = [NSString stringWithCString: psz_title];
740
741             o_item = [o_program addItemWithTitle: o_menu_title
742                 action: pf_toggle_program keyEquivalent: @""];
743             [o_item setTag: p_input->stream.pp_programs[i]->i_number];
744             [o_item setTarget: o_controls];
745
746             if( p_pgrm == p_input->stream.pp_programs[i] )
747             {
748                 [o_item setState: NSOnState];
749             }
750         }
751
752         p_intf->p_sys->b_program_update = 0;
753     }
754
755     if( p_intf->p_sys->b_title_update )
756     {
757         NSMenu * o_title;
758         SEL pf_toggle_title;
759
760         o_title = [o_mi_title submenu];
761         pf_toggle_title = @selector(toggleTitle:);
762
763         /* remove previous title items */
764         i_nb_items = [o_title numberOfItems];
765         for( i = 0; i < i_nb_items; i++ )
766         {
767             [o_title removeItemAtIndex: 0];
768         }
769
770         /* make (un)sensitive */
771         [o_mi_title setEnabled: 
772             p_input->stream.i_area_nb > 1];
773
774         /* add title items */
775         for( i = 1 ; i < p_input->stream.i_area_nb ; i++ )
776         {
777             snprintf( psz_title, sizeof(psz_title), "Title %d (%d)", i,
778                 p_input->stream.pp_areas[i]->i_part_nb );
779             psz_title[sizeof(psz_title) - 1] = '\0';
780
781             o_menu_title = [NSString stringWithCString: psz_title];
782
783             o_item = [o_title addItemWithTitle: o_menu_title
784                 action: pf_toggle_title keyEquivalent: @""];
785             [o_item setTag: i];
786             [o_item setTarget: o_controls];
787
788             if( ( p_input->stream.pp_areas[i] ==
789                 p_input->stream.p_selected_area ) )
790             {
791                 [o_item setState: NSOnState];
792             }
793         }
794
795         p_intf->p_sys->b_title_update = 0;
796     }
797
798     if( p_intf->p_sys->b_chapter_update )
799     {
800         NSMenu * o_chapter;
801         SEL pf_toggle_chapter;
802
803         o_chapter = [o_mi_chapter submenu];
804         pf_toggle_chapter = @selector(toggleChapter:);
805
806         /* remove previous chapter items */
807         i_nb_items = [o_chapter numberOfItems];
808         for( i = 0; i < i_nb_items; i++ )
809         {
810             [o_chapter removeItemAtIndex: 0];
811         }
812
813         /* make (un)sensitive */
814         [o_mi_chapter setEnabled: 
815             p_input->stream.p_selected_area->i_part_nb > 1];
816
817         /* add chapter items */
818         for( i = 0 ; i < p_input->stream.p_selected_area->i_part_nb ; i++ )
819         {
820             snprintf( psz_title, sizeof(psz_title), "Chapter %d", i + 1 );
821             psz_title[sizeof(psz_title) - 1] = '\0';
822
823             o_menu_title = [NSString stringWithCString: psz_title];
824
825             o_item = [o_chapter addItemWithTitle: o_menu_title
826                 action: pf_toggle_chapter keyEquivalent: @""];
827             [o_item setTag: i + 1];
828             [o_item setTarget: o_controls];
829
830             if( ( p_input->stream.p_selected_area->i_part == i + 1 ) )
831             {
832                 [o_item setState: NSOnState];
833             }
834         }
835
836         p_intf->p_sys->i_part =
837                 p_input->stream.p_selected_area->i_part;
838
839         p_intf->p_sys->b_chapter_update = 0;
840     }
841
842     for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
843     {
844         if( p_input->stream.pp_selected_es[i]->i_cat == AUDIO_ES )
845         {
846             p_audio_es = p_input->stream.pp_selected_es[i];
847         }
848         else if( p_input->stream.pp_selected_es[i]->i_cat == SPU_ES )
849         {
850             p_spu_es = p_input->stream.pp_selected_es[i];
851         }
852     }
853
854     vlc_mutex_unlock( &p_input->stream.stream_lock );
855
856     if( p_intf->p_sys->b_audio_update )
857     {
858         [self setupLangMenu: o_mi_language es: p_audio_es
859             category: AUDIO_ES selector: @selector(toggleLanguage:)];
860
861         p_intf->p_sys->b_audio_update = 0;
862     }
863
864     if( p_intf->p_sys->b_spu_update )
865     {
866         [self setupLangMenu: o_mi_subtitle es: p_spu_es
867             category: SPU_ES selector: @selector(toggleLanguage:)];
868
869         p_intf->p_sys->b_spu_update = 0;
870     }
871
872     if ( p_intf->p_sys->b_aout_update )
873     {
874         aout_instance_t * p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT,
875                                                     FIND_ANYWHERE );
876
877         if ( p_aout != NULL )
878         {
879             vlc_value_t val;
880             val.b_bool = 0;
881
882             var_Set( (vlc_object_t *)p_aout, "intf-change", val );
883
884             [self setupVarMenu: o_mi_channels target: (vlc_object_t *)p_aout
885                 var: "audio-channels" selector: @selector(toggleVar:)];
886
887             [self setupVarMenu: o_mi_device target: (vlc_object_t *)p_aout
888                 var: "audio-device" selector: @selector(toggleVar:)];
889
890             vlc_object_release( (vlc_object_t *)p_aout );
891         }
892
893         p_intf->p_sys->b_aout_update = 0;
894     }
895
896     if ( p_intf->p_sys->b_vout_update )
897     {
898         vout_thread_t * p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
899                                                   FIND_ANYWHERE );
900
901         if ( p_vout != NULL )
902         {
903             vlc_value_t val;
904             val.b_bool = 0;
905
906             var_Set( (vlc_object_t *)p_vout, "intf-change", val );
907
908             [self setupVarMenu: o_mi_screen target: (vlc_object_t *)p_vout
909                 var: "video-device" selector: @selector(toggleVar:)];
910
911             vlc_object_release( (vlc_object_t *)p_vout );
912
913             [o_mi_close_window setEnabled: TRUE];
914         }
915
916         p_intf->p_sys->b_vout_update = 0;
917     }
918
919     vlc_mutex_lock( &p_input->stream.stream_lock );
920
921 #undef p_input
922 }
923
924 - (void)setupLangMenu:(NSMenuItem *)o_mi
925                       es:(es_descriptor_t *)p_es
926                       category:(int)i_cat
927                       selector:(SEL)pf_callback
928 {
929     unsigned int i, i_nb_items;
930     NSMenu * o_menu = [o_mi submenu];
931     intf_thread_t * p_intf = [NSApp getIntf];
932
933     /* remove previous language items */
934     i_nb_items = [o_menu numberOfItems];
935     for( i = 0; i < i_nb_items; i++ )
936     {
937         [o_menu removeItemAtIndex: 0];
938     }
939
940     /* make sensitive : we can't change it after we build the menu, and
941      * before, we don't yet how many items we will have. So make it
942      * always sensitive. --Meuuh */
943     [o_mi setEnabled: TRUE];
944
945 #if 0
946     /* We do not use this code, because you need to start stop .avi for
947      * it to work, so not very useful now  --hartman */
948     if ( o_mi == o_mi_subtitle ) {
949         NSLog(@"testing");
950         [o_mi setEnabled: TRUE ];
951         NSMenuItem * o_lmi;
952         NSString * o_title;
953         o_title = _NS("Load from file..");
954         o_lmi = [o_menu addItemWithTitle: o_title
955                  action: pf_callback keyEquivalent: @""];
956         [o_lmi setTag: 2000];
957         [o_lmi setTarget: o_controls];
958     }
959 #endif
960
961     vlc_mutex_lock( &p_intf->p_sys->p_input->stream.stream_lock );
962
963 #define ES p_intf->p_sys->p_input->stream.pp_es[i]
964     for( i = 0 ; i < p_intf->p_sys->p_input->stream.i_es_number ; i++ )
965     {
966         if( ( ES->i_cat == i_cat ) &&
967             ( !ES->p_pgrm ||
968               ES->p_pgrm ==
969                  p_intf->p_sys->p_input->stream.p_selected_program ) )
970         {
971             NSMenuItem * o_lmi;
972             NSString * o_title;
973
974             if( *ES->psz_desc )
975             {
976                 o_title = [NSString stringWithCString: ES->psz_desc];
977             }
978             else
979             {
980                 char psz_title[ 256 ];
981
982                 snprintf( psz_title, sizeof(psz_title), "Language 0x%x",
983                           ES->i_id );
984                 psz_title[sizeof(psz_title) - 1] = '\0';
985
986                 o_title = [NSString stringWithCString: psz_title];
987             }
988
989             o_lmi = [o_menu addItemWithTitle: o_title
990                 action: pf_callback keyEquivalent: @""];
991             [o_lmi setRepresentedObject: 
992                 [NSValue valueWithPointer: ES]];
993             [o_lmi setTarget: o_controls];
994             [o_lmi setTag: i_cat];
995
996             if( /*p_es == ES*/ ES->p_decoder_fifo != NULL )
997             {
998                 [o_lmi setState: NSOnState];
999             }
1000         }
1001     }
1002 #undef ES
1003
1004     vlc_mutex_unlock( &p_intf->p_sys->p_input->stream.stream_lock );
1005 }
1006
1007 - (void)setupVarMenu:(NSMenuItem *)o_mi
1008                      target:(vlc_object_t *)p_object
1009                      var:(const char *)psz_variable
1010                      selector:(SEL)pf_callback
1011 {
1012     int i, i_nb_items;
1013     NSMenu * o_menu = [o_mi submenu];
1014     vlc_value_t val;
1015     char * psz_value;
1016
1017     /* remove previous items */
1018     i_nb_items = [o_menu numberOfItems];
1019     for( i = 0; i < i_nb_items; i++ )
1020     {
1021         [o_menu removeItemAtIndex: 0];
1022     }
1023
1024     if ( var_Get( p_object, psz_variable, &val ) < 0 )
1025     {
1026         return;
1027     }
1028     psz_value = val.psz_string;
1029
1030     if ( var_Change( p_object, psz_variable,
1031                      VLC_VAR_GETLIST, &val ) < 0 )
1032     {
1033         free( psz_value );
1034         return;
1035     }
1036
1037     /* make (un)sensitive */
1038     [o_mi setEnabled: ( val.p_list->i_count > 0 )];
1039
1040     for ( i = 0; i < val.p_list->i_count; i++ )
1041     {
1042         NSMenuItem * o_lmi;
1043         NSString * o_title;
1044
1045         o_title = [NSString stringWithCString: val.p_list->p_values[i].psz_string];
1046         o_lmi = [o_menu addItemWithTitle: o_title
1047                  action: pf_callback keyEquivalent: @""];
1048         /* FIXME: this isn't 64-bit clean ! */
1049         [o_lmi setTag: (int)psz_variable];
1050         [o_lmi setRepresentedObject:
1051             [NSValue valueWithPointer: p_object]];
1052         [o_lmi setTarget: o_controls];
1053
1054         if ( !strcmp( psz_value, val.p_list->p_values[i].psz_string ) )
1055             [o_lmi setState: NSOnState];
1056     }
1057
1058     var_Change( p_object, psz_variable, VLC_VAR_FREELIST,
1059                 &val );
1060
1061     free( psz_value );
1062 }
1063
1064 - (IBAction)clearRecentItems:(id)sender
1065 {
1066     [[NSDocumentController sharedDocumentController]
1067                           clearRecentDocuments: nil];
1068 }
1069
1070 - (void)openRecentItem:(id)sender
1071 {
1072     [self application: nil openFile: [sender title]]; 
1073 }
1074
1075 - (IBAction)viewPreferences:(id)sender
1076 {
1077     if( o_prefs == nil )
1078     {
1079         o_prefs = [[VLCPrefs alloc] init];
1080     }
1081
1082     [o_prefs createPrefPanel: @"main"];
1083 }
1084
1085 - (IBAction)timesliderUpdate:(id)sender
1086 {
1087     switch( [[NSApp currentEvent] type] )
1088     {
1089         case NSLeftMouseDown:
1090             f_slider = [sender floatValue];
1091             [self displayTime];
1092             break;
1093             
1094         case NSLeftMouseDragged:
1095             f_slider = [sender floatValue];
1096             [self displayTime];
1097             break;
1098
1099         case NSLeftMouseUp:
1100             f_slider = [sender floatValue];
1101             [self displayTime];
1102             intf_thread_t * p_intf = [NSApp getIntf];
1103             input_thread_t * p_input = p_intf->p_sys->p_input;
1104             vlc_mutex_unlock( &p_input->stream.stream_lock );
1105             break;
1106
1107         default:
1108             break;
1109     }
1110 }
1111
1112 - (void)displayTime
1113 {
1114     intf_thread_t * p_intf = [NSApp getIntf];
1115     input_thread_t * p_input = p_intf->p_sys->p_input;
1116
1117     if( p_input == NULL )
1118     {
1119         [o_timeslider setEnabled: FALSE];
1120         [o_timeslider setFloatValue: 0.0];
1121         [o_timefield setStringValue: @"0:00:00"]; 
1122
1123         return;
1124     }
1125
1126 #define p_area p_input->stream.p_selected_area
1127
1128     if( p_input->stream.b_changed )
1129     {
1130         [o_timeslider setEnabled: p_input->stream.b_seekable];
1131     }
1132     else if( p_intf->p_sys->b_playing )
1133     {
1134         NSString * o_time;
1135         char psz_time[ OFFSETTOTIME_MAX_SIZE ];
1136
1137         input_OffsetToTime( p_input, psz_time, p_area->i_tell );
1138
1139         o_time = [NSString stringWithCString: psz_time];
1140         [o_timefield setStringValue: o_time];
1141
1142         if( p_input->stream.b_seekable )
1143         {
1144             if( f_slider == f_slider_old )
1145             {
1146                 float f_updated = ( 100. * p_area->i_tell ) /
1147                                            p_area->i_size;
1148
1149                 if( f_slider != f_updated )
1150                 {
1151                     [o_timeslider setFloatValue: f_updated];
1152                 }
1153             }
1154             else
1155             {
1156                 off_t i_seek = ( f_slider * p_area->i_size ) / 100;
1157
1158                 /* release the lock to be able to seek */
1159                 vlc_mutex_unlock( &p_input->stream.stream_lock );
1160                 input_Seek( p_input, i_seek, INPUT_SEEK_SET );
1161                 vlc_mutex_lock( &p_input->stream.stream_lock ); 
1162
1163                 /* Update the old value */
1164                 f_slider_old = f_slider;
1165             }
1166         }
1167     }
1168 #undef p_area
1169 }
1170
1171 - (IBAction)closeError:(id)sender
1172 {
1173     /* Error panel */
1174     [o_err_msg setString: @""];
1175     [o_error performClose: self];
1176 }
1177
1178 - (IBAction)openReadMe:(id)sender
1179 {
1180     NSString * o_path = [[NSBundle mainBundle] 
1181         pathForResource: @"README.MacOSX" ofType: @"rtf"]; 
1182        
1183     [[NSWorkspace sharedWorkspace] openFile: o_path 
1184                                    withApplication: @"TextEdit"];
1185 }
1186
1187 - (IBAction)reportABug:(id)sender
1188 {
1189     NSURL * o_url = [NSURL URLWithString: 
1190         @"http://www.videolan.org/support/bug-reporting.html"];
1191     
1192     [[NSWorkspace sharedWorkspace] openURL: o_url];
1193 }
1194
1195 - (IBAction)openWebsite:(id)sender
1196 {
1197     NSURL * o_url = [NSURL URLWithString: @"http://www.videolan.org"];
1198     
1199     [[NSWorkspace sharedWorkspace] openURL: o_url];
1200 }
1201
1202 - (IBAction)openLicense:(id)sender
1203 {
1204     NSString * o_path = [[NSBundle mainBundle] 
1205         pathForResource: @"COPYING" ofType: nil];
1206        
1207     [[NSWorkspace sharedWorkspace] openFile: o_path 
1208                                    withApplication: @"TextEdit"];
1209 }
1210
1211 - (void)windowDidBecomeKey:(NSNotification *)o_notification
1212 {
1213     if( [o_notification object] == o_msgs_panel )
1214     {
1215         id o_msg;
1216         NSEnumerator * o_enum;
1217
1218         [o_messages setString: @""]; 
1219
1220         [o_msg_lock lock];
1221
1222         o_enum = [o_msg_arr objectEnumerator];
1223
1224         while( ( o_msg = [o_enum nextObject] ) != nil )
1225         {
1226             [o_messages insertText: o_msg];
1227         }
1228
1229         [o_msg_lock unlock];
1230     }
1231 }
1232
1233 @end
1234
1235 @implementation VLCMain (NSMenuValidation)
1236
1237 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
1238 {
1239     BOOL bEnabled = TRUE;
1240
1241     /* Recent Items Menu */
1242
1243     if( [[o_mi title] isEqualToString: _NS("Clear Menu")] )
1244     {
1245         NSMenu * o_menu = [o_mi_open_recent submenu];
1246         int i_nb_items = [o_menu numberOfItems];
1247         NSArray * o_docs = [[NSDocumentController sharedDocumentController]
1248                                                        recentDocumentURLs];
1249         UInt32 i_nb_docs = [o_docs count];
1250
1251         if( i_nb_items > 1 )
1252         {
1253             while( --i_nb_items )
1254             {
1255                 [o_menu removeItemAtIndex: 0];
1256             }
1257         }
1258
1259         if( i_nb_docs > 0 )
1260         {
1261             NSURL * o_url;
1262             NSString * o_doc;
1263
1264             [o_menu insertItem: [NSMenuItem separatorItem] atIndex: 0];
1265
1266             while( TRUE )
1267             {
1268                 i_nb_docs--;
1269
1270                 o_url = [o_docs objectAtIndex: i_nb_docs];
1271
1272                 if( [o_url isFileURL] )
1273                 {
1274                     o_doc = [o_url path];
1275                 }
1276                 else
1277                 {
1278                     o_doc = [o_url absoluteString];
1279                 }
1280
1281                 [o_menu insertItemWithTitle: o_doc
1282                     action: @selector(openRecentItem:)
1283                     keyEquivalent: @"" atIndex: 0]; 
1284
1285                 if( i_nb_docs == 0 )
1286                 {
1287                     break;
1288                 }
1289             } 
1290         }
1291         else
1292         {
1293             bEnabled = FALSE;
1294         }
1295     }
1296
1297     return( bEnabled );
1298 }
1299
1300 @end
1301
1302 @implementation VLCMain (Internal)
1303
1304 - (void)handlePortMessage:(NSPortMessage *)o_msg
1305 {
1306     NSData * o_data;
1307     NSValue * o_value;
1308     NSInvocation * o_inv;
1309     vout_thread_t * p_vout;
1310  
1311     o_data = [[o_msg components] lastObject];
1312     o_inv = *((NSInvocation **)[o_data bytes]); 
1313     [o_inv getArgument: &o_value atIndex: 2];
1314     p_vout = (vout_thread_t *)[o_value pointerValue];
1315
1316     [p_vout->p_sys->o_lock lock];
1317     [o_inv invoke];
1318     [p_vout->p_sys->o_lock unlockWithCondition: 1];
1319 }
1320
1321 @end