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