]> git.sesse.net Git - vlc/blob - modules/gui/macosx/controls.m
90d274994ee26f84080d4ef4724893a2086e4875
[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 #include <vlc_keys.h>
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: @"VLCVoutWindow"] )
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)showPosition: (id)sender
388 {
389     vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT,
390                                              FIND_ANYWHERE );
391     if( p_vout != NULL )
392     {
393         vlc_value_t val;
394         intf_thread_t * p_intf = VLCIntf;
395         val.i_int = config_GetInt( p_intf, "key-position" );
396         var_Set( p_intf, "key-pressed", val );
397         vlc_object_release( (vlc_object_t *)p_vout );
398     }
399 }
400
401 - (IBAction)toogleFullscreen:(id)sender {
402     NSMenuItem *o_mi = [[NSMenuItem alloc] initWithTitle: _NS("Fullscreen") action: nil keyEquivalent:@""];             
403     [self windowAction: [o_mi autorelease]]; 
404 }
405
406 - (BOOL) isFullscreen {
407     id o_vout_view = [self getVoutView];
408     if( o_vout_view )
409     {
410         return [o_vout_view isFullscreen];
411     }
412     return NO;
413 }
414
415 - (IBAction)windowAction:(id)sender
416 {
417     NSString *o_title = [sender title];
418
419     vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT,
420                                               FIND_ANYWHERE );
421     if( p_vout != NULL )
422     {
423         id o_vout_view = [self getVoutView];
424         if( o_vout_view )
425         {
426             if( [o_title isEqualToString: _NS("Half Size") ] )
427                 [o_vout_view scaleWindowWithFactor: 0.5 animate: YES];
428             else if( [o_title isEqualToString: _NS("Normal Size") ] )
429                 [o_vout_view scaleWindowWithFactor: 1.0 animate: YES];
430             else if( [o_title isEqualToString: _NS("Double Size") ] )
431                 [o_vout_view scaleWindowWithFactor: 2.0 animate: YES];
432             else if( [o_title isEqualToString: _NS("Float on Top") ] )
433                 [o_vout_view toggleFloatOnTop];
434             else if( [o_title isEqualToString: _NS("Fit to Screen") ] )
435             {
436                 id o_window = [o_vout_view getWindow];
437                 if( ![o_window isZoomed] )
438                     [o_window performZoom:self];
439             }
440             else if( [o_title isEqualToString: _NS("Snapshot") ] )
441             {
442                 [o_vout_view snapshot];
443             }
444             else
445             {
446                 [o_vout_view toggleFullscreen];
447             }
448         }
449         vlc_object_release( (vlc_object_t *)p_vout );
450     }
451     else
452     {
453         playlist_t * p_playlist = pl_Yield( VLCIntf );
454
455         if( [o_title isEqualToString: _NS("Fullscreen")] ||
456             [sender isKindOfClass:[NSButton class]] )
457         {
458             vlc_value_t val;
459             var_Get( p_playlist, "fullscreen", &val );
460             var_Set( p_playlist, "fullscreen", (vlc_value_t)!val.b_bool );
461         }
462
463         vlc_object_release( p_playlist );
464     }
465
466 }
467
468 - (void)scrollWheel:(NSEvent *)theEvent
469 {
470     intf_thread_t * p_intf = VLCIntf;
471     float f_yabsvalue = [theEvent deltaY] > 0.0f ? [theEvent deltaY] : -[theEvent deltaY];
472     float f_xabsvalue = [theEvent deltaX] > 0.0f ? [theEvent deltaX] : -[theEvent deltaX];
473     int i, i_yvlckey, i_xvlckey;
474
475     if ([theEvent deltaY] < 0.0f)
476         i_yvlckey = KEY_MOUSEWHEELDOWN;
477     else
478         i_yvlckey = KEY_MOUSEWHEELUP;
479
480     if ([theEvent deltaX] < 0.0f)
481         i_xvlckey = KEY_MOUSEWHEELRIGHT;
482     else
483         i_xvlckey = KEY_MOUSEWHEELLEFT;
484
485     /* Send multiple key event, depending on the intensity of the event */
486     for (i = 0; i < (int)(f_yabsvalue/4.+1.) && f_yabsvalue > 0.05 ; i++)
487         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_yvlckey );
488
489     /* Prioritize Y event (sound volume) over X event */
490     if (f_yabsvalue < 0.05)
491     {
492         for (i = 0; i < (int)(f_xabsvalue/6.+1.) && f_xabsvalue > 0.05; i++)
493          var_SetInteger( p_intf->p_libvlc, "key-pressed", i_xvlckey );
494     }
495 }
496
497 - (BOOL)keyEvent:(NSEvent *)o_event
498 {
499     BOOL eventHandled = NO;
500     unichar key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
501
502     if( key )
503     {
504         vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT,
505                                               FIND_ANYWHERE );
506         if( p_vout != NULL )
507         {
508             /* Escape */
509             if( key == (unichar) 0x1b )
510             {
511                 id o_vout_view = [self getVoutView];
512                 if( o_vout_view && [o_vout_view isFullscreen] )
513                 {
514                     [o_vout_view toggleFullscreen];
515                     eventHandled = YES;
516                 }
517             }
518             else if( key == ' ' )
519             {
520                 [self play:self];
521                 eventHandled = YES;
522             }
523             vlc_object_release( (vlc_object_t *)p_vout );
524         }
525     }
526     return eventHandled;
527 }
528
529 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
530                     target:(vlc_object_t *)p_object
531                     var:(const char *)psz_variable
532                     selector:(SEL)pf_callback
533 {
534     vlc_value_t val, text;
535     int i_type = var_Type( p_object, psz_variable );
536
537     switch( i_type & VLC_VAR_TYPE )
538     {
539     case VLC_VAR_VOID:
540     case VLC_VAR_BOOL:
541     case VLC_VAR_VARIABLE:
542     case VLC_VAR_STRING:
543     case VLC_VAR_INTEGER:
544         break;
545     default:
546         /* Variable doesn't exist or isn't handled */
547         return;
548     }
549     
550     /* Make sure we want to display the variable */
551     if( i_type & VLC_VAR_HASCHOICE )
552     {
553         var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
554         if( val.i_int == 0 ) return;
555         if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
556             return;
557     }
558     
559     /* Get the descriptive name of the variable */
560     var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
561     [o_mi setTitle: [[VLCMain sharedInstance] localizedString: text.psz_string ?
562                                         text.psz_string : strdup( psz_variable ) ]];
563
564     var_Get( p_object, psz_variable, &val );
565     if( i_type & VLC_VAR_HASCHOICE )
566     {
567         NSMenu *o_menu = [o_mi submenu];
568
569         [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
570                         var:psz_variable selector:pf_callback];
571         
572         if( text.psz_string ) free( text.psz_string );
573         return;
574     }
575
576     VLCMenuExt *o_data;
577     switch( i_type & VLC_VAR_TYPE )
578     {
579     case VLC_VAR_VOID:
580         o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
581                 Value: val ofType: i_type];
582         [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
583         break;
584
585     case VLC_VAR_BOOL:
586         o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
587                 Value: val ofType: i_type];
588         [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
589         if( !( i_type & VLC_VAR_ISCOMMAND ) )
590             [o_mi setState: val.b_bool ? TRUE : FALSE ];
591         break;
592
593     default:
594         if( text.psz_string ) free( text.psz_string );
595         return;
596     }
597
598     if( ( i_type & VLC_VAR_TYPE ) == VLC_VAR_STRING ) free( val.psz_string );
599     if( text.psz_string ) free( text.psz_string );
600 }
601
602
603 - (void)setupVarMenu:(NSMenu *)o_menu
604                     forMenuItem: (NSMenuItem *)o_parent
605                     target:(vlc_object_t *)p_object
606                     var:(const char *)psz_variable
607                     selector:(SEL)pf_callback
608 {
609     vlc_value_t val, val_list, text_list;
610     int i_type, i, i_nb_items;
611
612     /* remove previous items */
613     i_nb_items = [o_menu numberOfItems];
614     for( i = 0; i < i_nb_items; i++ )
615     {
616         [o_menu removeItemAtIndex: 0];
617     }
618
619     /* Check the type of the object variable */
620     i_type = var_Type( p_object, psz_variable );
621
622     /* Make sure we want to display the variable */
623     if( i_type & VLC_VAR_HASCHOICE )
624     {
625         var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
626         if( val.i_int == 0 ) return;
627         if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
628             return;
629     }
630     else
631     {
632         return;
633     }
634
635     switch( i_type & VLC_VAR_TYPE )
636     {
637     case VLC_VAR_VOID:
638     case VLC_VAR_BOOL:
639     case VLC_VAR_VARIABLE:
640     case VLC_VAR_STRING:
641     case VLC_VAR_INTEGER:
642         break;
643     default:
644         /* Variable doesn't exist or isn't handled */
645         return;
646     }
647
648     if( var_Get( p_object, psz_variable, &val ) < 0 )
649     {
650         return;
651     }
652
653     if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
654                     &val_list, &text_list ) < 0 )
655     {
656         if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
657         return;
658     }
659
660     /* make (un)sensitive */
661     [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
662
663     for( i = 0; i < val_list.p_list->i_count; i++ )
664     {
665         vlc_value_t another_val;
666         NSMenuItem * o_lmi;
667         NSString *o_title = @"";
668         VLCMenuExt *o_data;
669
670         switch( i_type & VLC_VAR_TYPE )
671         {
672         case VLC_VAR_STRING:
673             another_val.psz_string =
674                 strdup(val_list.p_list->p_values[i].psz_string);
675
676             o_title = [[VLCMain sharedInstance] localizedString: text_list.p_list->p_values[i].psz_string ?
677                 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
678
679             o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
680             o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
681                     Value: another_val ofType: i_type];
682             [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
683             [o_lmi setTarget: self];
684
685             if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) && !( i_type & VLC_VAR_ISCOMMAND ) )
686                 [o_lmi setState: TRUE ];
687
688             break;
689
690         case VLC_VAR_INTEGER:
691
692              o_title = text_list.p_list->p_values[i].psz_string ?
693                                  [[VLCMain sharedInstance] localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
694                                  [NSString stringWithFormat: @"%d",
695                                  val_list.p_list->p_values[i].i_int];
696
697             o_lmi = [[o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""] retain ];
698             o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
699                     Value: val_list.p_list->p_values[i] ofType: i_type];
700             [o_lmi setRepresentedObject: [NSValue valueWithPointer:[ o_data retain]]];
701             [o_lmi setTarget: self];
702
703             if( val_list.p_list->p_values[i].i_int == val.i_int && !( i_type & VLC_VAR_ISCOMMAND ) )
704                 [o_lmi setState: TRUE ];
705             break;
706
707         default:
708           break;
709         }
710     }
711
712     /* clean up everything */
713     if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
714     var_Change( p_object, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
715 }
716
717 - (IBAction)toggleVar:(id)sender
718 {
719     NSMenuItem *o_mi = (NSMenuItem *)sender;
720     VLCMenuExt *o_data = [[o_mi representedObject] pointerValue];
721     [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
722         toTarget: self withObject: o_data];
723
724     return;
725 }
726
727 - (int)toggleVarThread: (id)_o_data
728 {
729     vlc_object_t *p_object;
730     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
731     VLCMenuExt *o_data = (VLCMenuExt *)_o_data;
732
733     vlc_thread_set_priority( VLCIntf , VLC_THREAD_PRIORITY_LOW );
734
735     p_object = (vlc_object_t *)vlc_object_get( VLCIntf,
736                                     [o_data objectID] );
737
738     if( p_object != NULL )
739     {
740         var_Set( p_object, strdup([o_data name]), [o_data value] );
741         vlc_object_release( p_object );
742         [o_pool release];
743         return VLC_TRUE;
744     }
745     [o_pool release];
746     return VLC_EGENERIC;
747 }
748
749 - (IBAction)goToSpecificTime:(id)sender
750 {
751     if( sender == o_specificTime_cancel_btn )
752     {
753         [NSApp endSheet: o_specificTime_win];
754         [o_specificTime_win close];
755     }
756     else if( sender == o_specificTime_ok_btn )
757     {
758         input_thread_t * p_input = (input_thread_t *)vlc_object_find( VLCIntf, \
759             VLC_OBJECT_INPUT, FIND_ANYWHERE );
760         if( p_input )
761         {
762             unsigned int timeInSec = 0;
763             NSString * fieldContent = [o_specificTime_enter_fld stringValue];
764             if( [[fieldContent componentsSeparatedByString: @":"] count] > 1 && 
765                 [[fieldContent componentsSeparatedByString: @":"] count] <= 3 )
766             {
767                 NSArray * ourTempArray = \
768                     [fieldContent componentsSeparatedByString: @":"];
769
770                 if( [[fieldContent componentsSeparatedByString: @":"] count] == 3 )
771                 {
772                     timeInSec += ([[ourTempArray objectAtIndex: 0] intValue] * 3600); //h
773                     timeInSec += ([[ourTempArray objectAtIndex: 1] intValue] * 60); //m
774                     timeInSec += [[ourTempArray objectAtIndex: 2] intValue];        //s
775                 }
776                 else
777                 {
778                     timeInSec += ([[ourTempArray objectAtIndex: 0] intValue] * 60); //m
779                     timeInSec += [[ourTempArray objectAtIndex: 1] intValue]; //s
780                 }
781             }
782             else
783                 timeInSec = [fieldContent intValue];
784
785             input_Control( p_input, INPUT_SET_TIME, (int64_t)(timeInSec * 1000000));
786             vlc_object_release( p_input );
787         }
788     
789         [NSApp endSheet: o_specificTime_win];
790         [o_specificTime_win close];
791     }
792     else
793     {
794         input_thread_t * p_input = (input_thread_t *)vlc_object_find( VLCIntf, \
795             VLC_OBJECT_INPUT, FIND_ANYWHERE );
796         if( p_input )
797         {
798             /* we can obviously only do that if an input is available */
799             vlc_value_t pos, length;
800             var_Get( p_input, "time", &pos );
801             [o_specificTime_enter_fld setIntValue: (pos.i_time / 1000000)];
802             var_Get( p_input, "length", &length );
803             [o_specificTime_stepper setMaxValue: (length.i_time / 1000000)];
804
805             [NSApp beginSheet: o_specificTime_win modalForWindow: \
806                 [NSApp mainWindow] modalDelegate: self didEndSelector: nil \
807                 contextInfo: nil];
808             [o_specificTime_win makeKeyWindow];
809             vlc_object_release( p_input );
810         }
811     }
812 }
813
814 - (id)getFSPanel
815 {
816     if( o_fs_panel )
817         return o_fs_panel;
818     else
819     {
820         msg_Err( VLCIntf, "FSPanel is nil" );
821         return NULL;
822     }
823 }
824
825 @end
826
827 @implementation VLCControls (NSMenuValidation)
828
829 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
830 {
831     BOOL bEnabled = TRUE;
832     vlc_value_t val;
833     intf_thread_t * p_intf = VLCIntf;
834     playlist_t * p_playlist = pl_Yield( p_intf );
835
836     vlc_mutex_lock( &p_playlist->object_lock );
837
838 #define p_input p_playlist->p_input
839
840     if( [[o_mi title] isEqualToString: _NS("Faster")] ||
841         [[o_mi title] isEqualToString: _NS("Slower")] )
842     {
843         if( p_input != NULL )
844         {
845             bEnabled = p_input->b_can_pace_control;
846         }
847         else
848         {
849             bEnabled = FALSE;
850         }
851     }
852     else if( [[o_mi title] isEqualToString: _NS("Stop")] )
853     {
854         if( p_input == NULL )
855         {
856             bEnabled = FALSE;
857         }
858         [o_main setupMenus]; /* Make sure input menu is up to date */
859     }
860     else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
861              [[o_mi title] isEqualToString: _NS("Next")] )
862     {
863         /** \todo fix i_size use */
864         bEnabled = p_playlist->items.i_size > 1;
865     }
866     else if( [[o_mi title] isEqualToString: _NS("Random")] )
867     {
868         int i_state;
869         var_Get( p_playlist, "random", &val );
870         i_state = val.b_bool ? NSOnState : NSOffState;
871         [o_mi setState: i_state];
872     }
873     else if( [[o_mi title] isEqualToString: _NS("Repeat One")] )
874     {
875         int i_state;
876         var_Get( p_playlist, "repeat", &val );
877         i_state = val.b_bool ? NSOnState : NSOffState;
878         [o_mi setState: i_state];
879     }
880     else if( [[o_mi title] isEqualToString: _NS("Repeat All")] )
881     {
882         int i_state;
883         var_Get( p_playlist, "loop", &val );
884         i_state = val.b_bool ? NSOnState : NSOffState;
885         [o_mi setState: i_state];
886     }
887     else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
888              [[o_mi title] isEqualToString: _NS("Step Backward")] ||
889              [[o_mi title] isEqualToString: _NS("Jump To Time")])
890     {
891         if( p_input != NULL )
892         {
893             var_Get( p_input, "seekable", &val);
894             bEnabled = val.b_bool;
895         }
896         else bEnabled = FALSE;
897     }
898     else if( [[o_mi title] isEqualToString: _NS("Mute")] )
899     {
900         [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
901         [o_main setupMenus]; /* Make sure audio menu is up to date */
902     }
903     else if( [[o_mi title] isEqualToString: _NS("Half Size")] ||
904                 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
905                 [[o_mi title] isEqualToString: _NS("Double Size")] ||
906                 [[o_mi title] isEqualToString: _NS("Fit to Screen")] ||
907                 [[o_mi title] isEqualToString: _NS("Snapshot")] ||
908                 [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
909                 [[o_mi title] isEqualToString: _NS("Float on Top")] )
910     {
911         id o_window;
912         NSArray *o_windows = [NSApp orderedWindows];
913         NSEnumerator *o_enumerator = [o_windows objectEnumerator];
914         bEnabled = FALSE;
915         
916         vout_thread_t   *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
917                                               FIND_ANYWHERE );
918         if( p_vout != NULL )
919         {
920             if( [[o_mi title] isEqualToString: _NS("Float on Top")] )
921             {
922                 var_Get( p_vout, "video-on-top", &val );
923                 [o_mi setState: val.b_bool ?  NSOnState : NSOffState];
924             }
925
926             while( (o_window = [o_enumerator nextObject]))
927             {
928                 if( [[o_window className] isEqualToString: @"VLCVoutWindow"] ||
929                             [[[VLCMain sharedInstance] getEmbeddedList]
930                             windowContainsEmbedded: o_window])
931                 {
932                     bEnabled = TRUE;
933                     break;
934                 }
935             }
936             vlc_object_release( (vlc_object_t *)p_vout );
937         }
938         else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] )
939         {
940             var_Get( p_playlist, "fullscreen", &val );
941             [o_mi setState: val.b_bool];
942             bEnabled = TRUE;
943         }
944         [o_main setupMenus]; /* Make sure video menu is up to date */
945     }
946
947     vlc_mutex_unlock( &p_playlist->object_lock );
948     vlc_object_release( p_playlist );
949
950     return( bEnabled );
951 }
952
953 @end
954
955 /*****************************************************************************
956  * VLCMenuExt implementation 
957  *****************************************************************************
958  * Object connected to a playlistitem which remembers the data belonging to
959  * the variable of the autogenerated menu
960  *****************************************************************************/
961 @implementation VLCMenuExt
962
963 - (id)initWithVar: (const char *)_psz_name Object: (int)i_id
964         Value: (vlc_value_t)val ofType: (int)_i_type
965 {
966     self = [super init];
967
968     if( self != nil )
969     {
970         psz_name = strdup( _psz_name );
971         i_object_id = i_id;
972         value = val;
973         i_type = _i_type;
974     }
975
976     return( self );
977 }
978
979 - (void)dealloc
980 {
981     free( psz_name );
982     [super dealloc];
983 }
984
985 - (char *)name
986 {
987     return psz_name;
988 }
989
990 - (int)objectID
991 {
992     return i_object_id;
993 }
994
995 - (vlc_value_t)value
996 {
997     return value;
998 }
999
1000 - (int)type
1001 {
1002     return i_type;
1003 }
1004
1005 @end
1006
1007
1008 /*****************************************************************************
1009  * VLCTimeField implementation 
1010  *****************************************************************************
1011  * we need this to catch our click-event in the controller window
1012  *****************************************************************************/
1013
1014 @implementation VLCTimeField
1015 - (void)mouseDown: (NSEvent *)ourEvent
1016 {
1017     if( [ourEvent clickCount] > 1 )
1018         [[[VLCMain sharedInstance] getControls] goToSpecificTime: nil];
1019 }
1020 @end