]> git.sesse.net Git - vlc/blob - modules/gui/macosx/controls.m
Mac OS X gui: Sanitize vlc object retaining/releasing.
[vlc] / modules / gui / macosx / controls.m
1 /*****************************************************************************
2  * controls.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2002-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Derk-Jan Hartman <hartman at videolan dot org>
10  *          Benjamin Pracht <bigben at videolan doit org>
11  *          Felix K\9fhne <fkuehne at videolan dot org>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include <stdlib.h>                                      /* malloc(), free() */
32 #include <sys/param.h>                                    /* for MAXPATHLEN */
33 #include <string.h>
34
35 #import "intf.h"
36 #import "vout.h"
37 #import "open.h"
38 #import "controls.h"
39 #import "playlist.h"
40 #include <vlc_osd.h>
41
42 /*****************************************************************************
43  * VLCControls implementation 
44  *****************************************************************************/
45 @implementation VLCControls
46
47 - (id)init
48 {
49     [super init];
50     o_fs_panel = [[VLCFSPanel alloc] init];
51     return self;
52 }
53
54 - (void)awakeFromNib
55 {
56     [o_specificTime_mi setTitle: _NS("Jump To Time")];
57     [o_specificTime_cancel_btn setTitle: _NS("Cancel")];
58     [o_specificTime_ok_btn setTitle: _NS("OK")];
59     [o_specificTime_sec_lbl setStringValue: _NS("sec.")];
60     [o_specificTime_goTo_lbl setStringValue: _NS("Jump to time")];
61 }
62
63 - (IBAction)play:(id)sender
64 {
65     vlc_value_t val;
66     intf_thread_t * p_intf = VLCIntf;
67     playlist_t * p_playlist = pl_Yield( p_intf );
68
69     vlc_mutex_lock( &p_playlist->object_lock );
70     if( playlist_IsEmpty( p_playlist ) )
71     {
72         vlc_mutex_unlock( &p_playlist->object_lock );
73         vlc_object_release( p_playlist );
74         [o_main intfOpenFileGeneric: (id)sender];
75     }
76     else
77     {
78         vlc_mutex_unlock( &p_playlist->object_lock );
79         vlc_object_release( p_playlist );
80     }
81
82     val.i_int = config_GetInt( p_intf, "key-play-pause" );
83     var_Set( p_intf->p_libvlc, "key-pressed", val );
84 }
85
86 /* Small helper method */
87
88 -(id) getVoutView
89 {
90     id o_window;
91     id o_vout_view = nil;
92     id o_embedded_vout_list = [[VLCMain sharedInstance] getEmbeddedList];
93     NSEnumerator *o_enumerator = [[NSApp orderedWindows] objectEnumerator];
94     while( !o_vout_view && ( o_window = [o_enumerator nextObject] ) )
95     {
96         /* We have an embedded vout */
97         if( [o_embedded_vout_list windowContainsEmbedded: o_window] )
98         {
99             o_vout_view = [o_embedded_vout_list getViewForWindow: o_window];
100         }
101         /* We have a detached vout */
102         else if( [[o_window className] isEqualToString: @"VLCVoutWindow"] )
103         {
104             msg_Dbg( VLCIntf, "detached vout controls.m call getVoutView" );
105             o_vout_view = [o_window getVoutView];
106         }
107     }
108     return o_vout_view;
109 }
110
111
112 - (IBAction)stop:(id)sender
113 {
114     vlc_value_t val;
115     intf_thread_t * p_intf = VLCIntf;
116     val.i_int = config_GetInt( p_intf, "key-stop" );
117     var_Set( p_intf->p_libvlc, "key-pressed", val );
118 }
119
120 - (IBAction)faster:(id)sender
121 {
122     vlc_value_t val;
123     intf_thread_t * p_intf = VLCIntf;
124     val.i_int = config_GetInt( p_intf, "key-faster" );
125     var_Set( p_intf->p_libvlc, "key-pressed", val );
126 }
127
128 - (IBAction)slower:(id)sender
129 {
130     vlc_value_t val;
131     intf_thread_t * p_intf = VLCIntf;
132     val.i_int = config_GetInt( p_intf, "key-slower" );
133     var_Set( p_intf->p_libvlc, "key-pressed", val );
134 }
135
136 - (IBAction)prev:(id)sender
137 {
138     vlc_value_t val;
139     intf_thread_t * p_intf = VLCIntf;
140     val.i_int = config_GetInt( p_intf, "key-prev" );
141     var_Set( p_intf->p_libvlc, "key-pressed", val );
142 }
143
144 - (IBAction)next:(id)sender
145 {
146     vlc_value_t val;
147     intf_thread_t * p_intf = VLCIntf;
148     val.i_int = config_GetInt( p_intf, "key-next" );
149     var_Set( p_intf->p_libvlc, "key-pressed", val );
150 }
151
152 - (IBAction)random:(id)sender
153 {
154     vlc_value_t val;
155     intf_thread_t * p_intf = VLCIntf;
156     playlist_t * p_playlist = pl_Yield( p_intf );
157
158     var_Get( p_playlist, "random", &val );
159     val.b_bool = !val.b_bool;
160     var_Set( p_playlist, "random", val );
161     if( val.b_bool )
162     {
163         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Random On" ) );
164         config_PutInt( p_playlist, "random", 1 );
165     }
166     else
167     {
168         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Random Off" ) );
169         config_PutInt( p_playlist, "random", 0 );
170     }
171
172     p_intf->p_sys->b_playmode_update = VLC_TRUE;
173     p_intf->p_sys->b_intf_update = VLC_TRUE;
174     vlc_object_release( p_playlist );
175 }
176
177 /* three little ugly helpers */
178 - (void)repeatOne
179 {
180     [o_btn_repeat setImage: [NSImage imageNamed:@"repeat_single_embedded_blue"]];
181     [o_btn_repeat setAlternateImage: [NSImage imageNamed:@"repeat_embedded_blue"]];
182 }
183 - (void)repeatAll
184 {
185     [o_btn_repeat setImage: [NSImage imageNamed:@"repeat_embedded_blue"]];
186     [o_btn_repeat setAlternateImage: [NSImage imageNamed:@"repeat_embedded"]];
187 }
188 - (void)repeatOff
189 {
190     [o_btn_repeat setImage: [NSImage imageNamed:@"repeat_embedded"]];
191     [o_btn_repeat setAlternateImage: [NSImage imageNamed:@"repeat_single_embedded_blue"]];
192 }
193 - (void)shuffle
194 {
195     vlc_value_t val;
196     playlist_t *p_playlist = pl_Yield( VLCIntf );
197     var_Get( p_playlist, "random", &val );
198     [o_btn_shuffle setState: val.b_bool];
199     vlc_object_release( p_playlist );
200 }
201
202 - (IBAction)repeatButtonAction:(id)sender
203 {
204     vlc_value_t looping,repeating;
205     intf_thread_t * p_intf = VLCIntf;
206     playlist_t * p_playlist = pl_Yield( p_intf );
207
208     var_Get( p_playlist, "repeat", &repeating );
209     var_Get( p_playlist, "loop", &looping );
210
211     if( !repeating.b_bool && !looping.b_bool )
212     {
213         /* was: no repeating at all, switching to Repeat One */
214         
215         /* set our button's look */
216         [self repeatOne];
217         
218         /* prepare core communication */
219         repeating.b_bool = VLC_TRUE;
220         looping.b_bool = VLC_FALSE;
221         config_PutInt( p_playlist, "repeat", 1 );
222         config_PutInt( p_playlist, "loop", 0 ); 
223         
224         /* show the change */
225         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
226     }
227     else if( repeating.b_bool && !looping.b_bool )
228     {
229         /* was: Repeat One, switching to Repeat All */
230         
231         /* set our button's look */
232         [self repeatAll];
233         
234         /* prepare core communication */
235         repeating.b_bool = VLC_FALSE;
236         looping.b_bool = VLC_TRUE;
237         config_PutInt( p_playlist, "repeat", 0 ); 
238         config_PutInt( p_playlist, "loop", 1 ); 
239         
240         /* show the change */
241         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
242     }
243     else
244     {
245         /* was: Repeat All or bug in VLC, switching to Repeat Off */
246         
247         /* set our button's look */
248         [self repeatOff];
249         
250         /* prepare core communication */
251         repeating.b_bool = VLC_FALSE;
252         looping.b_bool = VLC_FALSE;
253         config_PutInt( p_playlist, "repeat", 0 ); 
254         config_PutInt( p_playlist, "loop", 0 ); 
255         
256         /* show the change */
257         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
258     }
259
260     /* communicate with core and the main intf loop */
261     var_Set( p_playlist, "repeat", repeating );
262     var_Set( p_playlist, "loop", looping );    
263     p_intf->p_sys->b_playmode_update = VLC_TRUE;
264     p_intf->p_sys->b_intf_update = VLC_TRUE;
265
266     vlc_object_release( p_playlist );
267 }
268
269
270 - (IBAction)repeat:(id)sender
271 {
272     vlc_value_t val;
273     intf_thread_t * p_intf = VLCIntf;
274     playlist_t * p_playlist = pl_Yield( p_intf );
275
276     var_Get( p_playlist, "repeat", &val );
277     if (!val.b_bool)
278     {
279         var_Set( p_playlist, "loop", val );
280     }
281     val.b_bool = !val.b_bool;
282     var_Set( p_playlist, "repeat", val );
283     if( val.b_bool )
284     {
285         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
286         config_PutInt( p_playlist, "repeat", 1 );
287     }
288     else
289     {
290         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
291         config_PutInt( p_playlist, "repeat", 0 );
292     }
293     
294     p_intf->p_sys->b_playmode_update = VLC_TRUE;
295     p_intf->p_sys->b_intf_update = VLC_TRUE;
296     vlc_object_release( p_playlist );
297 }
298
299 - (IBAction)loop:(id)sender
300 {
301     vlc_value_t val;
302     intf_thread_t * p_intf = VLCIntf;
303     playlist_t * p_playlist = pl_Yield( p_intf );
304
305     var_Get( p_playlist, "loop", &val );
306     if (!val.b_bool)
307     {
308         var_Set( p_playlist, "repeat", val );
309     }
310     val.b_bool = !val.b_bool;
311     var_Set( p_playlist, "loop", val );
312     if( val.b_bool )
313     {
314         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
315         config_PutInt( p_playlist, "loop", 1 );
316     }
317     else
318     {
319         vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
320         config_PutInt( p_playlist, "loop", 0 );
321     }
322
323     p_intf->p_sys->b_playmode_update = VLC_TRUE;
324     p_intf->p_sys->b_intf_update = VLC_TRUE;
325     vlc_object_release( p_playlist );
326 }
327
328 - (IBAction)forward:(id)sender
329 {
330     vlc_value_t val;
331     intf_thread_t * p_intf = VLCIntf;
332     val.i_int = config_GetInt( p_intf, "key-jump+short" );
333     var_Set( p_intf->p_libvlc, "key-pressed", val );
334 }
335
336 - (IBAction)backward:(id)sender
337 {
338     vlc_value_t val;
339     intf_thread_t * p_intf = VLCIntf;
340     val.i_int = config_GetInt( p_intf, "key-jump-short" );
341     var_Set( p_intf->p_libvlc, "key-pressed", val );
342 }
343
344
345 - (IBAction)volumeUp:(id)sender
346 {
347     vlc_value_t val;
348     intf_thread_t * p_intf = VLCIntf;
349     val.i_int = config_GetInt( p_intf, "key-vol-up" );
350     var_Set( p_intf->p_libvlc, "key-pressed", val );
351     /* Manage volume status */
352     [o_main manageVolumeSlider];
353 }
354
355 - (IBAction)volumeDown:(id)sender
356 {
357     vlc_value_t val;
358     intf_thread_t * p_intf = VLCIntf;
359     val.i_int = config_GetInt( p_intf, "key-vol-down" );
360     var_Set( p_intf->p_libvlc, "key-pressed", val );
361     /* Manage volume status */
362     [o_main manageVolumeSlider];
363 }
364
365 - (IBAction)mute:(id)sender
366 {
367     vlc_value_t val;
368     intf_thread_t * p_intf = VLCIntf;
369     val.i_int = config_GetInt( p_intf, "key-vol-mute" );
370     var_Set( p_intf->p_libvlc, "key-pressed", val );
371     /* Manage volume status */
372     [o_main manageVolumeSlider];
373 }
374
375 - (IBAction)volumeSliderUpdated:(id)sender
376 {
377     intf_thread_t * p_intf = VLCIntf;
378     audio_volume_t i_volume = (audio_volume_t)[sender intValue];
379     int i_volume_step = 0;
380     i_volume_step = config_GetInt( p_intf->p_libvlc, "volume-step" );
381     aout_VolumeSet( p_intf, i_volume * i_volume_step );
382     /* Manage volume status */
383     [o_main manageVolumeSlider];
384 }
385
386 - (IBAction)showPosition: (id)sender
387 {
388     vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT,
389                                              FIND_ANYWHERE );
390     if( p_vout != NULL )
391     {
392         vlc_value_t val;
393         intf_thread_t * p_intf = VLCIntf;
394         val.i_int = config_GetInt( p_intf, "key-position" );
395         var_Set( p_intf, "key-pressed", val );
396         vlc_object_release( (vlc_object_t *)p_vout );
397     }
398 }
399
400 - (IBAction)toogleFullscreen:(id)sender {
401     NSMenuItem *o_mi = [[NSMenuItem alloc] initWithTitle: _NS("Fullscreen") action: nil keyEquivalent:@""];             
402     [self windowAction: [o_mi autorelease]]; 
403 }
404
405 - (BOOL) isFullscreen {
406     id o_vout_view = [self getVoutView];
407     if( o_vout_view )
408     {
409         return [o_vout_view isFullscreen];
410     }
411     return NO;
412 }
413
414 - (IBAction)windowAction:(id)sender
415 {
416     NSString *o_title = [sender title];
417
418     vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT,
419                                               FIND_ANYWHERE );
420     if( p_vout != NULL )
421     {
422         id o_vout_view = [self getVoutView];
423         if( o_vout_view )
424         {
425             if( [o_title isEqualToString: _NS("Half Size") ] )
426                 [o_vout_view scaleWindowWithFactor: 0.5 animate: YES];
427             else if( [o_title isEqualToString: _NS("Normal Size") ] )
428                 [o_vout_view scaleWindowWithFactor: 1.0 animate: YES];
429             else if( [o_title isEqualToString: _NS("Double Size") ] )
430                 [o_vout_view scaleWindowWithFactor: 2.0 animate: YES];
431             else if( [o_title isEqualToString: _NS("Float on Top") ] )
432                 [o_vout_view toggleFloatOnTop];
433             else if( [o_title isEqualToString: _NS("Fit to Screen") ] )
434             {
435                 id o_window = [o_vout_view getWindow];
436                 if( ![o_window isZoomed] )
437                     [o_window performZoom:self];
438             }
439             else if( [o_title isEqualToString: _NS("Snapshot") ] )
440             {
441                 [o_vout_view snapshot];
442             }
443             else
444             {
445                 [o_vout_view toggleFullscreen];
446             }
447         }
448         vlc_object_release( (vlc_object_t *)p_vout );
449     }
450     else
451     {
452         playlist_t * p_playlist = pl_Yield( VLCIntf );
453
454         if( [o_title isEqualToString: _NS("Fullscreen")] ||
455             [sender isKindOfClass:[NSButton class]] )
456         {
457             vlc_value_t val;
458             var_Get( p_playlist, "fullscreen", &val );
459             var_Set( p_playlist, "fullscreen", (vlc_value_t)!val.b_bool );
460         }
461
462         vlc_object_release( p_playlist );
463     }
464
465 }
466
467 - (BOOL)keyEvent:(NSEvent *)o_event
468 {
469     BOOL eventHandled = NO;
470     unichar key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
471
472     if( key )
473     {
474         vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT,
475                                               FIND_ANYWHERE );
476         if( p_vout != NULL )
477         {
478             /* Escape */
479             if( key == (unichar) 0x1b )
480             {
481                 id o_vout_view = [self getVoutView];
482                 if( o_vout_view && [o_vout_view isFullscreen] )
483                 {
484                     [o_vout_view toggleFullscreen];
485                     eventHandled = YES;
486                 }
487             }
488             else if( key == ' ' )
489             {
490                 [self play:self];
491                 eventHandled = YES;
492             }
493             vlc_object_release( (vlc_object_t *)p_vout );
494         }
495     }
496     return eventHandled;
497 }
498
499 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
500                     target:(vlc_object_t *)p_object
501                     var:(const char *)psz_variable
502                     selector:(SEL)pf_callback
503 {
504     vlc_value_t val, text;
505     int i_type = var_Type( p_object, psz_variable );
506
507     switch( i_type & VLC_VAR_TYPE )
508     {
509     case VLC_VAR_VOID:
510     case VLC_VAR_BOOL:
511     case VLC_VAR_VARIABLE:
512     case VLC_VAR_STRING:
513     case VLC_VAR_INTEGER:
514         break;
515     default:
516         /* Variable doesn't exist or isn't handled */
517         return;
518     }
519     
520     /* Make sure we want to display the variable */
521     if( i_type & VLC_VAR_HASCHOICE )
522     {
523         var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
524         if( val.i_int == 0 ) return;
525         if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
526             return;
527     }
528     
529     /* Get the descriptive name of the variable */
530     var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
531     [o_mi setTitle: [[VLCMain sharedInstance] localizedString: text.psz_string ?
532                                         text.psz_string : strdup( psz_variable ) ]];
533
534     var_Get( p_object, psz_variable, &val );
535     if( i_type & VLC_VAR_HASCHOICE )
536     {
537         NSMenu *o_menu = [o_mi submenu];
538
539         [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
540                         var:psz_variable selector:pf_callback];
541         
542         if( text.psz_string ) free( text.psz_string );
543         return;
544     }
545
546     VLCMenuExt *o_data;
547     switch( i_type & VLC_VAR_TYPE )
548     {
549     case VLC_VAR_VOID:
550         o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
551                 Value: val ofType: i_type];
552         [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
553         break;
554
555     case VLC_VAR_BOOL:
556         o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
557                 Value: val ofType: i_type];
558         [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
559         if( !( i_type & VLC_VAR_ISCOMMAND ) )
560             [o_mi setState: val.b_bool ? TRUE : FALSE ];
561         break;
562
563     default:
564         if( text.psz_string ) free( text.psz_string );
565         return;
566     }
567
568     if( ( i_type & VLC_VAR_TYPE ) == VLC_VAR_STRING ) free( val.psz_string );
569     if( text.psz_string ) free( text.psz_string );
570 }
571
572
573 - (void)setupVarMenu:(NSMenu *)o_menu
574                     forMenuItem: (NSMenuItem *)o_parent
575                     target:(vlc_object_t *)p_object
576                     var:(const char *)psz_variable
577                     selector:(SEL)pf_callback
578 {
579     vlc_value_t val, val_list, text_list;
580     int i_type, i, i_nb_items;
581
582     /* remove previous items */
583     i_nb_items = [o_menu numberOfItems];
584     for( i = 0; i < i_nb_items; i++ )
585     {
586         [o_menu removeItemAtIndex: 0];
587     }
588
589     /* Check the type of the object variable */
590     i_type = var_Type( p_object, psz_variable );
591
592     /* Make sure we want to display the variable */
593     if( i_type & VLC_VAR_HASCHOICE )
594     {
595         var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
596         if( val.i_int == 0 ) return;
597         if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
598             return;
599     }
600     else
601     {
602         return;
603     }
604
605     switch( i_type & VLC_VAR_TYPE )
606     {
607     case VLC_VAR_VOID:
608     case VLC_VAR_BOOL:
609     case VLC_VAR_VARIABLE:
610     case VLC_VAR_STRING:
611     case VLC_VAR_INTEGER:
612         break;
613     default:
614         /* Variable doesn't exist or isn't handled */
615         return;
616     }
617
618     if( var_Get( p_object, psz_variable, &val ) < 0 )
619     {
620         return;
621     }
622
623     if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
624                     &val_list, &text_list ) < 0 )
625     {
626         if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
627         return;
628     }
629
630     /* make (un)sensitive */
631     [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
632
633     for( i = 0; i < val_list.p_list->i_count; i++ )
634     {
635         vlc_value_t another_val;
636         NSMenuItem * o_lmi;
637         NSString *o_title = @"";
638         VLCMenuExt *o_data;
639
640         switch( i_type & VLC_VAR_TYPE )
641         {
642         case VLC_VAR_STRING:
643             another_val.psz_string =
644                 strdup(val_list.p_list->p_values[i].psz_string);
645
646             o_title = [[VLCMain sharedInstance] localizedString: text_list.p_list->p_values[i].psz_string ?
647                 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
648
649             o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
650             o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
651                     Value: another_val ofType: i_type];
652             [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
653             [o_lmi setTarget: self];
654
655             if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) && !( i_type & VLC_VAR_ISCOMMAND ) )
656                 [o_lmi setState: TRUE ];
657
658             break;
659
660         case VLC_VAR_INTEGER:
661
662              o_title = text_list.p_list->p_values[i].psz_string ?
663                                  [[VLCMain sharedInstance] localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
664                                  [NSString stringWithFormat: @"%d",
665                                  val_list.p_list->p_values[i].i_int];
666
667             o_lmi = [[o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""] retain ];
668             o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
669                     Value: val_list.p_list->p_values[i] ofType: i_type];
670             [o_lmi setRepresentedObject: [NSValue valueWithPointer:[ o_data retain]]];
671             [o_lmi setTarget: self];
672
673             if( val_list.p_list->p_values[i].i_int == val.i_int && !( i_type & VLC_VAR_ISCOMMAND ) )
674                 [o_lmi setState: TRUE ];
675             break;
676
677         default:
678           break;
679         }
680     }
681
682     /* clean up everything */
683     if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
684     var_Change( p_object, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
685 }
686
687 - (IBAction)toggleVar:(id)sender
688 {
689     NSMenuItem *o_mi = (NSMenuItem *)sender;
690     VLCMenuExt *o_data = [[o_mi representedObject] pointerValue];
691     [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
692         toTarget: self withObject: o_data];
693
694     return;
695 }
696
697 - (int)toggleVarThread: (id)_o_data
698 {
699     vlc_object_t *p_object;
700     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
701     VLCMenuExt *o_data = (VLCMenuExt *)_o_data;
702
703     vlc_thread_set_priority( VLCIntf , VLC_THREAD_PRIORITY_LOW );
704
705     p_object = (vlc_object_t *)vlc_object_get( VLCIntf,
706                                     [o_data objectID] );
707
708     if( p_object != NULL )
709     {
710         var_Set( p_object, strdup([o_data name]), [o_data value] );
711         vlc_object_release( p_object );
712         [o_pool release];
713         return VLC_TRUE;
714     }
715     [o_pool release];
716     return VLC_EGENERIC;
717 }
718
719 - (IBAction)goToSpecificTime:(id)sender
720 {
721     if( sender == o_specificTime_cancel_btn )
722     {
723         [NSApp endSheet: o_specificTime_win];
724         [o_specificTime_win close];
725     }
726     else if( sender == o_specificTime_ok_btn )
727     {
728         input_thread_t * p_input = (input_thread_t *)vlc_object_find( VLCIntf, \
729             VLC_OBJECT_INPUT, FIND_ANYWHERE );
730         if( p_input )
731         {
732             unsigned int timeInSec = 0;
733             NSString * fieldContent = [o_specificTime_enter_fld stringValue];
734             if( [[fieldContent componentsSeparatedByString: @":"] count] > 1 && 
735                 [[fieldContent componentsSeparatedByString: @":"] count] <= 3 )
736             {
737                 NSArray * ourTempArray = \
738                     [fieldContent componentsSeparatedByString: @":"];
739
740                 if( [[fieldContent componentsSeparatedByString: @":"] count] == 3 )
741                 {
742                     timeInSec += ([[ourTempArray objectAtIndex: 0] intValue] * 3600); //h
743                     timeInSec += ([[ourTempArray objectAtIndex: 1] intValue] * 60); //m
744                     timeInSec += [[ourTempArray objectAtIndex: 2] intValue];        //s
745                 }
746                 else
747                 {
748                     timeInSec += ([[ourTempArray objectAtIndex: 0] intValue] * 60); //m
749                     timeInSec += [[ourTempArray objectAtIndex: 1] intValue]; //s
750                 }
751             }
752             else
753                 timeInSec = [fieldContent intValue];
754
755             input_Control( p_input, INPUT_SET_TIME, (int64_t)(timeInSec * 1000000));
756             vlc_object_release( p_input );
757         }
758     
759         [NSApp endSheet: o_specificTime_win];
760         [o_specificTime_win close];
761     }
762     else
763     {
764         input_thread_t * p_input = (input_thread_t *)vlc_object_find( VLCIntf, \
765             VLC_OBJECT_INPUT, FIND_ANYWHERE );
766         if( p_input )
767         {
768             /* we can obviously only do that if an input is available */
769             vlc_value_t pos, length;
770             var_Get( p_input, "time", &pos );
771             [o_specificTime_enter_fld setIntValue: (pos.i_time / 1000000)];
772             var_Get( p_input, "length", &length );
773             [o_specificTime_stepper setMaxValue: (length.i_time / 1000000)];
774
775             [NSApp beginSheet: o_specificTime_win modalForWindow: \
776                 [NSApp mainWindow] modalDelegate: self didEndSelector: nil \
777                 contextInfo: nil];
778             [o_specificTime_win makeKeyWindow];
779             vlc_object_release( p_input );
780         }
781     }
782 }
783
784 - (id)getFSPanel
785 {
786     if( o_fs_panel )
787         return o_fs_panel;
788     else
789     {
790         msg_Err( VLCIntf, "FSPanel is nil" );
791         return NULL;
792     }
793 }
794
795 @end
796
797 @implementation VLCControls (NSMenuValidation)
798
799 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
800 {
801     BOOL bEnabled = TRUE;
802     vlc_value_t val;
803     intf_thread_t * p_intf = VLCIntf;
804     playlist_t * p_playlist = pl_Yield( p_intf );
805
806     vlc_mutex_lock( &p_playlist->object_lock );
807
808 #define p_input p_playlist->p_input
809
810     if( [[o_mi title] isEqualToString: _NS("Faster")] ||
811         [[o_mi title] isEqualToString: _NS("Slower")] )
812     {
813         if( p_input != NULL )
814         {
815             bEnabled = p_input->b_can_pace_control;
816         }
817         else
818         {
819             bEnabled = FALSE;
820         }
821     }
822     else if( [[o_mi title] isEqualToString: _NS("Stop")] )
823     {
824         if( p_input == NULL )
825         {
826             bEnabled = FALSE;
827         }
828         [o_main setupMenus]; /* Make sure input menu is up to date */
829     }
830     else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
831              [[o_mi title] isEqualToString: _NS("Next")] )
832     {
833             /** \todo fix i_size use */
834             bEnabled = p_playlist->items.i_size > 1;
835     }
836     else if( [[o_mi title] isEqualToString: _NS("Random")] )
837     {
838         int i_state;
839         var_Get( p_playlist, "random", &val );
840         i_state = val.b_bool ? NSOnState : NSOffState;
841         [o_mi setState: i_state];
842     }
843     else if( [[o_mi title] isEqualToString: _NS("Repeat One")] )
844     {
845         int i_state;
846         var_Get( p_playlist, "repeat", &val );
847         i_state = val.b_bool ? NSOnState : NSOffState;
848         [o_mi setState: i_state];
849     }
850     else if( [[o_mi title] isEqualToString: _NS("Repeat All")] )
851     {
852         int i_state;
853         var_Get( p_playlist, "loop", &val );
854         i_state = val.b_bool ? NSOnState : NSOffState;
855         [o_mi setState: i_state];
856     }
857     else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
858              [[o_mi title] isEqualToString: _NS("Step Backward")] ||
859              [[o_mi title] isEqualToString: _NS("Jump To Time")])
860     {
861         if( p_input != NULL )
862         {
863             var_Get( p_input, "seekable", &val);
864             bEnabled = val.b_bool;
865         }
866         else bEnabled = FALSE;
867     }
868     else if( [[o_mi title] isEqualToString: _NS("Mute")] )
869     {
870         [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
871         [o_main setupMenus]; /* Make sure audio menu is up to date */
872     }
873     else if( [[o_mi title] isEqualToString: _NS("Half Size")] ||
874                 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
875                 [[o_mi title] isEqualToString: _NS("Double Size")] ||
876                 [[o_mi title] isEqualToString: _NS("Fit to Screen")] ||
877                 [[o_mi title] isEqualToString: _NS("Snapshot")] ||
878                 [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
879                 [[o_mi title] isEqualToString: _NS("Float on Top")] )
880     {
881         id o_window;
882         NSArray *o_windows = [NSApp orderedWindows];
883         NSEnumerator *o_enumerator = [o_windows objectEnumerator];
884         bEnabled = FALSE;
885         
886         vout_thread_t   *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
887                                               FIND_ANYWHERE );
888         if( p_vout != NULL )
889         {
890             if( [[o_mi title] isEqualToString: _NS("Float on Top")] )
891             {
892                 var_Get( p_vout, "video-on-top", &val );
893                 [o_mi setState: val.b_bool ?  NSOnState : NSOffState];
894             }
895
896             while( (o_window = [o_enumerator nextObject]))
897             {
898                 if( [[o_window className] isEqualToString: @"VLCVoutWindow"] ||
899                             [[[VLCMain sharedInstance] getEmbeddedList]
900                             windowContainsEmbedded: o_window])
901                 {
902                     bEnabled = TRUE;
903                     break;
904                 }
905             }
906             vlc_object_release( (vlc_object_t *)p_vout );
907         }
908         else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] )
909         {
910             var_Get( p_playlist, "fullscreen", &val );
911             [o_mi setState: val.b_bool];
912             bEnabled = TRUE;
913         }
914         [o_main setupMenus]; /* Make sure video menu is up to date */
915     }
916
917     vlc_mutex_unlock( &p_playlist->object_lock );
918     vlc_object_release( p_playlist );
919
920     return( bEnabled );
921 }
922
923 @end
924
925 /*****************************************************************************
926  * VLCMenuExt implementation 
927  *****************************************************************************
928  * Object connected to a playlistitem which remembers the data belonging to
929  * the variable of the autogenerated menu
930  *****************************************************************************/
931 @implementation VLCMenuExt
932
933 - (id)initWithVar: (const char *)_psz_name Object: (int)i_id
934         Value: (vlc_value_t)val ofType: (int)_i_type
935 {
936     self = [super init];
937
938     if( self != nil )
939     {
940         psz_name = strdup( _psz_name );
941         i_object_id = i_id;
942         value = val;
943         i_type = _i_type;
944     }
945
946     return( self );
947 }
948
949 - (void)dealloc
950 {
951     free( psz_name );
952     [super dealloc];
953 }
954
955 - (char *)name
956 {
957     return psz_name;
958 }
959
960 - (int)objectID
961 {
962     return i_object_id;
963 }
964
965 - (vlc_value_t)value
966 {
967     return value;
968 }
969
970 - (int)type
971 {
972     return i_type;
973 }
974
975 @end
976
977
978 /*****************************************************************************
979  * VLCTimeField implementation 
980  *****************************************************************************
981  * we need this to catch our click-event in the controller window
982  *****************************************************************************/
983
984 @implementation VLCTimeField
985 - (void)mouseDown: (NSEvent *)ourEvent
986 {
987     if( [ourEvent clickCount] > 1 )
988         [[[VLCMain sharedInstance] getControls] goToSpecificTime: nil];
989 }
990 @end