1 /*****************************************************************************
2 * controls.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2002-2005 the VideoLAN team
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>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <stdlib.h> /* malloc(), free() */
31 #include <sys/param.h> /* for MAXPATHLEN */
40 /*****************************************************************************
41 * VLCControls implementation
42 *****************************************************************************/
43 @implementation VLCControls
45 - (IBAction)play:(id)sender
48 intf_thread_t * p_intf = VLCIntf;
49 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
53 vlc_mutex_lock( &p_playlist->object_lock );
54 if( p_playlist->i_size <= 0 )
56 vlc_mutex_unlock( &p_playlist->object_lock );
57 vlc_object_release( p_playlist );
58 [o_main intfOpenFileGeneric: (id)sender];
62 vlc_mutex_unlock( &p_playlist->object_lock );
63 vlc_object_release( p_playlist );
67 val.i_int = config_GetInt( p_intf, "key-play-pause" );
68 var_Set( p_intf->p_vlc, "key-pressed", val );
71 - (IBAction)stop:(id)sender
74 intf_thread_t * p_intf = VLCIntf;
75 val.i_int = config_GetInt( p_intf, "key-stop" );
76 var_Set( p_intf->p_vlc, "key-pressed", val );
79 - (IBAction)faster:(id)sender
82 intf_thread_t * p_intf = VLCIntf;
83 val.i_int = config_GetInt( p_intf, "key-faster" );
84 var_Set( p_intf->p_vlc, "key-pressed", val );
87 - (IBAction)slower:(id)sender
90 intf_thread_t * p_intf = VLCIntf;
91 val.i_int = config_GetInt( p_intf, "key-slower" );
92 var_Set( p_intf->p_vlc, "key-pressed", val );
95 - (IBAction)prev:(id)sender
98 intf_thread_t * p_intf = VLCIntf;
99 val.i_int = config_GetInt( p_intf, "key-prev" );
100 var_Set( p_intf->p_vlc, "key-pressed", val );
103 - (IBAction)next:(id)sender
106 intf_thread_t * p_intf = VLCIntf;
107 val.i_int = config_GetInt( p_intf, "key-next" );
108 var_Set( p_intf->p_vlc, "key-pressed", val );
111 - (IBAction)random:(id)sender
114 intf_thread_t * p_intf = VLCIntf;
115 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
117 if( p_playlist == NULL )
122 var_Get( p_playlist, "random", &val );
123 val.b_bool = !val.b_bool;
124 var_Set( p_playlist, "random", val );
127 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Random On" ) );
131 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Random Off" ) );
134 p_intf->p_sys->b_playmode_update = VLC_TRUE;
135 p_intf->p_sys->b_intf_update = VLC_TRUE;
136 vlc_object_release( p_playlist );
139 - (IBAction)repeat:(id)sender
142 intf_thread_t * p_intf = VLCIntf;
143 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
145 if( p_playlist == NULL )
150 var_Get( p_playlist, "repeat", &val );
153 var_Set( p_playlist, "loop", val );
155 val.b_bool = !val.b_bool;
156 var_Set( p_playlist, "repeat", val );
159 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
163 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
166 p_intf->p_sys->b_playmode_update = VLC_TRUE;
167 p_intf->p_sys->b_intf_update = VLC_TRUE;
168 vlc_object_release( p_playlist );
171 - (IBAction)loop:(id)sender
174 intf_thread_t * p_intf = VLCIntf;
175 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
177 if( p_playlist == NULL )
182 var_Get( p_playlist, "loop", &val );
185 var_Set( p_playlist, "repeat", val );
187 val.b_bool = !val.b_bool;
188 var_Set( p_playlist, "loop", val );
191 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
195 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
198 p_intf->p_sys->b_playmode_update = VLC_TRUE;
199 p_intf->p_sys->b_intf_update = VLC_TRUE;
200 vlc_object_release( p_playlist );
203 - (IBAction)forward:(id)sender
206 intf_thread_t * p_intf = VLCIntf;
207 val.i_int = config_GetInt( p_intf, "key-jump+short" );
208 var_Set( p_intf->p_vlc, "key-pressed", val );
211 - (IBAction)backward:(id)sender
214 intf_thread_t * p_intf = VLCIntf;
215 val.i_int = config_GetInt( p_intf, "key-jump-short" );
216 var_Set( p_intf->p_vlc, "key-pressed", val );
220 - (IBAction)volumeUp:(id)sender
223 intf_thread_t * p_intf = VLCIntf;
224 val.i_int = config_GetInt( p_intf, "key-vol-up" );
225 var_Set( p_intf->p_vlc, "key-pressed", val );
226 /* Manage volume status */
227 [o_main manageVolumeSlider];
230 - (IBAction)volumeDown:(id)sender
233 intf_thread_t * p_intf = VLCIntf;
234 val.i_int = config_GetInt( p_intf, "key-vol-down" );
235 var_Set( p_intf->p_vlc, "key-pressed", val );
236 /* Manage volume status */
237 [o_main manageVolumeSlider];
240 - (IBAction)mute:(id)sender
243 intf_thread_t * p_intf = VLCIntf;
244 val.i_int = config_GetInt( p_intf, "key-vol-mute" );
245 var_Set( p_intf->p_vlc, "key-pressed", val );
246 /* Manage volume status */
247 [o_main manageVolumeSlider];
250 - (IBAction)volumeSliderUpdated:(id)sender
252 intf_thread_t * p_intf = VLCIntf;
253 audio_volume_t i_volume = (audio_volume_t)[sender intValue];
254 int i_volume_step = 0;
255 i_volume_step = config_GetInt( p_intf->p_vlc, "volume-step" );
256 aout_VolumeSet( p_intf, i_volume * i_volume_step );
257 /* Manage volume status */
258 [o_main manageVolumeSlider];
261 - (IBAction)windowAction:(id)sender
263 id o_window = [NSApp keyWindow];
264 NSString *o_title = [sender title];
265 NSArray *o_windows = [NSApp orderedWindows];
266 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
267 vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT,
272 id o_embedded_vout_list = [[VLCMain sharedInstance] getEmbeddedList];
273 while ((o_window = [o_enumerator nextObject]))
275 id o_vout_view = nil;
276 /* We have an embedded vout */
277 if( [o_embedded_vout_list windowContainsEmbedded: o_window] )
279 o_vout_view = [o_embedded_vout_list getViewForWindow: o_window];
281 /* We have a detached Vout */
282 else if( [[o_window className] isEqualToString: @"VLCWindow"] )
284 o_vout_view = [o_window getVoutView];
289 if( [o_title isEqualToString: _NS("Half Size") ] )
290 [o_vout_view scaleWindowWithFactor: 0.5];
291 else if( [o_title isEqualToString: _NS("Normal Size") ] )
292 [o_vout_view scaleWindowWithFactor: 1.0];
293 else if( [o_title isEqualToString: _NS("Double Size") ] )
294 [o_vout_view scaleWindowWithFactor: 2.0];
295 else if( [o_title isEqualToString: _NS("Float on Top") ] )
296 [o_vout_view toggleFloatOnTop];
297 else if( [o_title isEqualToString: _NS("Fit to Screen") ] )
299 if( ![o_window isZoomed] )
300 [o_window performZoom:self];
302 else if( [o_title isEqualToString: _NS("Snapshot") ] )
304 [o_vout_view snapshot];
308 [o_vout_view toggleFullscreen];
313 vlc_object_release( (vlc_object_t *)p_vout );
317 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
320 if( p_playlist && ( [o_title isEqualToString: _NS("Fullscreen")] ||
321 [sender isKindOfClass:[NSButton class]] ) )
324 var_Get( p_playlist, "fullscreen", &val );
325 var_Set( p_playlist, "fullscreen", (vlc_value_t)!val.b_bool );
327 if( p_playlist ) vlc_object_release( (vlc_object_t *)p_playlist );
332 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
333 target:(vlc_object_t *)p_object
334 var:(const char *)psz_variable
335 selector:(SEL)pf_callback
337 vlc_value_t val, text;
338 int i_type = var_Type( p_object, psz_variable );
340 switch( i_type & VLC_VAR_TYPE )
344 case VLC_VAR_VARIABLE:
346 case VLC_VAR_INTEGER:
349 /* Variable doesn't exist or isn't handled */
353 /* Make sure we want to display the variable */
354 if( i_type & VLC_VAR_HASCHOICE )
356 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
357 if( val.i_int == 0 ) return;
358 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
362 /* Get the descriptive name of the variable */
363 var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
364 [o_mi setTitle: [[VLCMain sharedInstance] localizedString: text.psz_string ?
365 text.psz_string : strdup( psz_variable ) ]];
367 var_Get( p_object, psz_variable, &val );
368 if( i_type & VLC_VAR_HASCHOICE )
370 NSMenu *o_menu = [o_mi submenu];
372 [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
373 var:psz_variable selector:pf_callback];
375 if( text.psz_string ) free( text.psz_string );
380 switch( i_type & VLC_VAR_TYPE )
383 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
384 Value: val ofType: i_type];
385 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
389 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
390 Value: val ofType: i_type];
391 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
392 if( !( i_type & VLC_VAR_ISCOMMAND ) )
393 [o_mi setState: val.b_bool ? TRUE : FALSE ];
397 if( text.psz_string ) free( text.psz_string );
401 if( ( i_type & VLC_VAR_TYPE ) == VLC_VAR_STRING ) free( val.psz_string );
402 if( text.psz_string ) free( text.psz_string );
406 - (void)setupVarMenu:(NSMenu *)o_menu
407 forMenuItem: (NSMenuItem *)o_parent
408 target:(vlc_object_t *)p_object
409 var:(const char *)psz_variable
410 selector:(SEL)pf_callback
412 vlc_value_t val, val_list, text_list;
413 int i_type, i, i_nb_items;
415 /* remove previous items */
416 i_nb_items = [o_menu numberOfItems];
417 for( i = 0; i < i_nb_items; i++ )
419 [o_menu removeItemAtIndex: 0];
422 /* Check the type of the object variable */
423 i_type = var_Type( p_object, psz_variable );
425 /* Make sure we want to display the variable */
426 if( i_type & VLC_VAR_HASCHOICE )
428 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
429 if( val.i_int == 0 ) return;
430 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
438 switch( i_type & VLC_VAR_TYPE )
442 case VLC_VAR_VARIABLE:
444 case VLC_VAR_INTEGER:
447 /* Variable doesn't exist or isn't handled */
451 if( var_Get( p_object, psz_variable, &val ) < 0 )
456 if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
457 &val_list, &text_list ) < 0 )
459 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
463 /* make (un)sensitive */
464 [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
466 for( i = 0; i < val_list.p_list->i_count; i++ )
468 vlc_value_t another_val;
470 NSString *o_title = @"";
473 switch( i_type & VLC_VAR_TYPE )
476 another_val.psz_string =
477 strdup(val_list.p_list->p_values[i].psz_string);
479 o_title = [[VLCMain sharedInstance] localizedString: text_list.p_list->p_values[i].psz_string ?
480 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
482 o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
483 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
484 Value: another_val ofType: i_type];
485 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
486 [o_lmi setTarget: self];
488 if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) && !( i_type & VLC_VAR_ISCOMMAND ) )
489 [o_lmi setState: TRUE ];
493 case VLC_VAR_INTEGER:
495 o_title = text_list.p_list->p_values[i].psz_string ?
496 [[VLCMain sharedInstance] localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
497 [NSString stringWithFormat: @"%d",
498 val_list.p_list->p_values[i].i_int];
500 o_lmi = [[o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""] retain ];
501 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
502 Value: val_list.p_list->p_values[i] ofType: i_type];
503 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[ o_data retain]]];
504 [o_lmi setTarget: self];
506 if( val_list.p_list->p_values[i].i_int == val.i_int && !( i_type & VLC_VAR_ISCOMMAND ) )
507 [o_lmi setState: TRUE ];
515 /* clean up everything */
516 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
517 var_Change( p_object, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
520 - (IBAction)toggleVar:(id)sender
522 NSMenuItem *o_mi = (NSMenuItem *)sender;
523 VLCMenuExt *o_data = [[o_mi representedObject] pointerValue];
524 [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
525 toTarget: self withObject: o_data];
530 - (int)toggleVarThread: (id)_o_data
532 vlc_object_t *p_object;
533 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
534 VLCMenuExt *o_data = (VLCMenuExt *)_o_data;
536 vlc_thread_set_priority( VLCIntf , VLC_THREAD_PRIORITY_LOW );
538 p_object = (vlc_object_t *)vlc_object_get( VLCIntf,
541 if( p_object != NULL )
543 var_Set( p_object, strdup([o_data name]), [o_data value] );
544 vlc_object_release( p_object );
554 @implementation VLCControls (NSMenuValidation)
556 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
558 BOOL bEnabled = TRUE;
560 intf_thread_t * p_intf = VLCIntf;
561 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
564 if( p_playlist != NULL )
566 vlc_mutex_lock( &p_playlist->object_lock );
570 #define p_input p_playlist->p_input
572 if( [[o_mi title] isEqualToString: _NS("Faster")] ||
573 [[o_mi title] isEqualToString: _NS("Slower")] )
575 if( p_input != NULL )
577 bEnabled = p_input->input.b_can_pace_control;
584 else if( [[o_mi title] isEqualToString: _NS("Stop")] )
586 if( p_input == NULL )
590 [o_main setupMenus]; /* Make sure input menu is up to date */
592 else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
593 [[o_mi title] isEqualToString: _NS("Next")] )
595 bEnabled = p_playlist->i_size > 1;
597 else if( [[o_mi title] isEqualToString: _NS("Random")] )
600 var_Get( p_playlist, "random", &val );
601 i_state = val.b_bool ? NSOnState : NSOffState;
602 [o_mi setState: i_state];
604 else if( [[o_mi title] isEqualToString: _NS("Repeat One")] )
607 var_Get( p_playlist, "repeat", &val );
608 i_state = val.b_bool ? NSOnState : NSOffState;
609 [o_mi setState: i_state];
611 else if( [[o_mi title] isEqualToString: _NS("Repeat All")] )
614 var_Get( p_playlist, "loop", &val );
615 i_state = val.b_bool ? NSOnState : NSOffState;
616 [o_mi setState: i_state];
618 else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
619 [[o_mi title] isEqualToString: _NS("Step Backward")] )
621 if( p_input != NULL )
623 var_Get( p_input, "seekable", &val);
624 bEnabled = val.b_bool;
627 else if( [[o_mi title] isEqualToString: _NS("Mute")] )
629 [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
630 [o_main setupMenus]; /* Make sure audio menu is up to date */
632 else if( [[o_mi title] isEqualToString: _NS("Half Size")] ||
633 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
634 [[o_mi title] isEqualToString: _NS("Double Size")] ||
635 [[o_mi title] isEqualToString: _NS("Fit to Screen")] ||
636 [[o_mi title] isEqualToString: _NS("Snapshot")] ||
637 [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
638 [[o_mi title] isEqualToString: _NS("Float on Top")] )
641 NSArray *o_windows = [NSApp orderedWindows];
642 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
645 vout_thread_t *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
649 if( [[o_mi title] isEqualToString: _NS("Float on Top")] )
651 var_Get( p_vout, "video-on-top", &val );
652 [o_mi setState: val.b_bool ? NSOnState : NSOffState];
655 while( (o_window = [o_enumerator nextObject]))
657 if( [[o_window className] isEqualToString: @"VLCWindow"] ||
658 [[[VLCMain sharedInstance] getEmbeddedList]
659 windowContainsEmbedded: o_window])
665 vlc_object_release( (vlc_object_t *)p_vout );
667 else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] )
669 var_Get( p_playlist, "fullscreen", &val );
670 [o_mi setState: val.b_bool];
673 [o_main setupMenus]; /* Make sure video menu is up to date */
676 vlc_mutex_unlock( &p_playlist->object_lock );
677 vlc_object_release( p_playlist );
684 /*****************************************************************************
685 * VLCMenuExt implementation
686 *****************************************************************************
687 * Object connected to a playlistitem which remembers the data belonging to
688 * the variable of the autogenerated menu
689 *****************************************************************************/
690 @implementation VLCMenuExt
692 - (id)initWithVar: (const char *)_psz_name Object: (int)i_id
693 Value: (vlc_value_t)val ofType: (int)_i_type
699 psz_name = strdup( _psz_name );