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>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
30 #include <sys/param.h> /* for MAXPATHLEN */
39 /*****************************************************************************
40 * VLCControls implementation
41 *****************************************************************************/
42 @implementation VLCControls
44 - (IBAction)play:(id)sender
47 intf_thread_t * p_intf = VLCIntf;
48 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
52 vlc_mutex_lock( &p_playlist->object_lock );
53 if( p_playlist->i_size <= 0 )
55 vlc_mutex_unlock( &p_playlist->object_lock );
56 vlc_object_release( p_playlist );
57 [o_main intfOpenFileGeneric: (id)sender];
61 vlc_mutex_unlock( &p_playlist->object_lock );
62 vlc_object_release( p_playlist );
66 val.i_int = config_GetInt( p_intf, "key-play-pause" );
67 var_Set( p_intf->p_vlc, "key-pressed", val );
70 - (IBAction)stop:(id)sender
73 intf_thread_t * p_intf = VLCIntf;
74 val.i_int = config_GetInt( p_intf, "key-stop" );
75 var_Set( p_intf->p_vlc, "key-pressed", val );
78 - (IBAction)faster:(id)sender
81 intf_thread_t * p_intf = VLCIntf;
82 val.i_int = config_GetInt( p_intf, "key-faster" );
83 var_Set( p_intf->p_vlc, "key-pressed", val );
86 - (IBAction)slower:(id)sender
89 intf_thread_t * p_intf = VLCIntf;
90 val.i_int = config_GetInt( p_intf, "key-slower" );
91 var_Set( p_intf->p_vlc, "key-pressed", val );
94 - (IBAction)prev:(id)sender
97 intf_thread_t * p_intf = VLCIntf;
98 val.i_int = config_GetInt( p_intf, "key-prev" );
99 var_Set( p_intf->p_vlc, "key-pressed", val );
102 - (IBAction)next:(id)sender
105 intf_thread_t * p_intf = VLCIntf;
106 val.i_int = config_GetInt( p_intf, "key-next" );
107 var_Set( p_intf->p_vlc, "key-pressed", val );
110 - (IBAction)random:(id)sender
113 intf_thread_t * p_intf = VLCIntf;
114 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
116 if( p_playlist == NULL )
121 var_Get( p_playlist, "random", &val );
122 val.b_bool = !val.b_bool;
123 var_Set( p_playlist, "random", val );
126 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Random On" ) );
130 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Random Off" ) );
133 p_intf->p_sys->b_playmode_update = VLC_TRUE;
134 p_intf->p_sys->b_intf_update = VLC_TRUE;
135 vlc_object_release( p_playlist );
138 - (IBAction)repeat:(id)sender
141 intf_thread_t * p_intf = VLCIntf;
142 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
144 if( p_playlist == NULL )
149 var_Get( p_playlist, "repeat", &val );
152 var_Set( p_playlist, "loop", val );
154 val.b_bool = !val.b_bool;
155 var_Set( p_playlist, "repeat", val );
158 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
162 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
165 p_intf->p_sys->b_playmode_update = VLC_TRUE;
166 p_intf->p_sys->b_intf_update = VLC_TRUE;
167 vlc_object_release( p_playlist );
170 - (IBAction)loop:(id)sender
173 intf_thread_t * p_intf = VLCIntf;
174 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
176 if( p_playlist == NULL )
181 var_Get( p_playlist, "loop", &val );
184 var_Set( p_playlist, "repeat", val );
186 val.b_bool = !val.b_bool;
187 var_Set( p_playlist, "loop", val );
190 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
194 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
197 p_intf->p_sys->b_playmode_update = VLC_TRUE;
198 p_intf->p_sys->b_intf_update = VLC_TRUE;
199 vlc_object_release( p_playlist );
202 - (IBAction)forward:(id)sender
205 intf_thread_t * p_intf = VLCIntf;
206 val.i_int = config_GetInt( p_intf, "key-jump+10sec" );
207 var_Set( p_intf->p_vlc, "key-pressed", val );
210 - (IBAction)backward:(id)sender
213 intf_thread_t * p_intf = VLCIntf;
214 val.i_int = config_GetInt( p_intf, "key-jump-10sec" );
215 var_Set( p_intf->p_vlc, "key-pressed", val );
219 - (IBAction)volumeUp:(id)sender
222 intf_thread_t * p_intf = VLCIntf;
223 val.i_int = config_GetInt( p_intf, "key-vol-up" );
224 var_Set( p_intf->p_vlc, "key-pressed", val );
225 /* Manage volume status */
226 [o_main manageVolumeSlider];
229 - (IBAction)volumeDown:(id)sender
232 intf_thread_t * p_intf = VLCIntf;
233 val.i_int = config_GetInt( p_intf, "key-vol-down" );
234 var_Set( p_intf->p_vlc, "key-pressed", val );
235 /* Manage volume status */
236 [o_main manageVolumeSlider];
239 - (IBAction)mute:(id)sender
242 intf_thread_t * p_intf = VLCIntf;
243 val.i_int = config_GetInt( p_intf, "key-vol-mute" );
244 var_Set( p_intf->p_vlc, "key-pressed", val );
245 /* Manage volume status */
246 [o_main manageVolumeSlider];
249 - (IBAction)volumeSliderUpdated:(id)sender
251 intf_thread_t * p_intf = VLCIntf;
252 audio_volume_t i_volume = (audio_volume_t)[sender intValue];
253 int i_volume_step = 0;
254 i_volume_step = config_GetInt( p_intf->p_vlc, "volume-step" );
255 aout_VolumeSet( p_intf, i_volume * i_volume_step );
256 /* Manage volume status */
257 [o_main manageVolumeSlider];
260 - (IBAction)windowAction:(id)sender
262 id o_window = [NSApp keyWindow];
263 NSString *o_title = [sender title];
264 NSArray *o_windows = [NSApp orderedWindows];
265 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
266 vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT,
271 while ((o_window = [o_enumerator nextObject]))
273 if( [[o_window className] isEqualToString: @"VLCWindow"] )
275 if( [o_title isEqualToString: _NS("Half Size") ] )
276 [o_window scaleWindowWithFactor: 0.5];
277 else if( [o_title isEqualToString: _NS("Normal Size") ] )
278 [o_window scaleWindowWithFactor: 1.0];
279 else if( [o_title isEqualToString: _NS("Double Size") ] )
280 [o_window scaleWindowWithFactor: 2.0];
281 else if( [o_title isEqualToString: _NS("Float on Top") ] )
282 [o_window toggleFloatOnTop];
283 else if( [o_title isEqualToString: _NS("Fit to Screen") ] )
285 if( ![o_window isZoomed] )
286 [o_window performZoom:self];
288 else if( [o_title isEqualToString: _NS("Snapshot") ] )
294 [o_window toggleFullscreen];
299 vlc_object_release( (vlc_object_t *)p_vout );
303 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
306 if( p_playlist && ( [o_title isEqualToString: _NS("Fullscreen")] ||
307 [sender isKindOfClass:[NSButton class]] ) )
310 var_Get( p_playlist, "fullscreen", &val );
311 var_Set( p_playlist, "fullscreen", (vlc_value_t)!val.b_bool );
313 if( p_playlist ) vlc_object_release( (vlc_object_t *)p_playlist );
318 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
319 target:(vlc_object_t *)p_object
320 var:(const char *)psz_variable
321 selector:(SEL)pf_callback
323 vlc_value_t val, text;
324 int i_type = var_Type( p_object, psz_variable );
326 switch( i_type & VLC_VAR_TYPE )
330 case VLC_VAR_VARIABLE:
332 case VLC_VAR_INTEGER:
335 /* Variable doesn't exist or isn't handled */
339 /* Make sure we want to display the variable */
340 if( i_type & VLC_VAR_HASCHOICE )
342 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
343 if( val.i_int == 0 ) return;
344 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
348 /* Get the descriptive name of the variable */
349 var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
350 [o_mi setTitle: [[VLCMain sharedInstance] localizedString: text.psz_string ?
351 text.psz_string : strdup( psz_variable ) ]];
353 var_Get( p_object, psz_variable, &val );
354 if( i_type & VLC_VAR_HASCHOICE )
356 NSMenu *o_menu = [o_mi submenu];
358 [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
359 var:psz_variable selector:pf_callback];
361 if( text.psz_string ) free( text.psz_string );
366 switch( i_type & VLC_VAR_TYPE )
369 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
370 Value: val ofType: i_type];
371 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
375 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
376 Value: val ofType: i_type];
377 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
378 if( !( i_type & VLC_VAR_ISCOMMAND ) )
379 [o_mi setState: val.b_bool ? TRUE : FALSE ];
383 if( text.psz_string ) free( text.psz_string );
387 if( ( i_type & VLC_VAR_TYPE ) == VLC_VAR_STRING ) free( val.psz_string );
388 if( text.psz_string ) free( text.psz_string );
392 - (void)setupVarMenu:(NSMenu *)o_menu
393 forMenuItem: (NSMenuItem *)o_parent
394 target:(vlc_object_t *)p_object
395 var:(const char *)psz_variable
396 selector:(SEL)pf_callback
398 vlc_value_t val, val_list, text_list;
399 int i_type, i, i_nb_items;
401 /* remove previous items */
402 i_nb_items = [o_menu numberOfItems];
403 for( i = 0; i < i_nb_items; i++ )
405 [o_menu removeItemAtIndex: 0];
408 /* Check the type of the object variable */
409 i_type = var_Type( p_object, psz_variable );
411 /* Make sure we want to display the variable */
412 if( i_type & VLC_VAR_HASCHOICE )
414 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
415 if( val.i_int == 0 ) return;
416 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
424 switch( i_type & VLC_VAR_TYPE )
428 case VLC_VAR_VARIABLE:
430 case VLC_VAR_INTEGER:
433 /* Variable doesn't exist or isn't handled */
437 if( var_Get( p_object, psz_variable, &val ) < 0 )
442 if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
443 &val_list, &text_list ) < 0 )
445 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
449 /* make (un)sensitive */
450 [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
452 for( i = 0; i < val_list.p_list->i_count; i++ )
454 vlc_value_t another_val;
456 NSString *o_title = @"";
459 switch( i_type & VLC_VAR_TYPE )
462 another_val.psz_string =
463 strdup(val_list.p_list->p_values[i].psz_string);
465 o_title = [[VLCMain sharedInstance] localizedString: text_list.p_list->p_values[i].psz_string ?
466 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
468 o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
469 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
470 Value: another_val ofType: i_type];
471 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
472 [o_lmi setTarget: self];
474 if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) && !( i_type & VLC_VAR_ISCOMMAND ) )
475 [o_lmi setState: TRUE ];
479 case VLC_VAR_INTEGER:
481 o_title = text_list.p_list->p_values[i].psz_string ?
482 [[VLCMain sharedInstance] localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
483 [NSString stringWithFormat: @"%d",
484 val_list.p_list->p_values[i].i_int];
486 o_lmi = [[o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""] retain ];
487 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
488 Value: val_list.p_list->p_values[i] ofType: i_type];
489 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[ o_data retain]]];
490 [o_lmi setTarget: self];
492 if( val_list.p_list->p_values[i].i_int == val.i_int && !( i_type & VLC_VAR_ISCOMMAND ) )
493 [o_lmi setState: TRUE ];
501 /* clean up everything */
502 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
503 var_Change( p_object, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
506 - (IBAction)toggleVar:(id)sender
508 NSMenuItem *o_mi = (NSMenuItem *)sender;
509 VLCMenuExt *o_data = [[o_mi representedObject] pointerValue];
510 [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
511 toTarget: self withObject: o_data];
516 - (int)toggleVarThread: (id)_o_data
518 vlc_object_t *p_object;
519 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
520 VLCMenuExt *o_data = (VLCMenuExt *)_o_data;
522 vlc_thread_set_priority( VLCIntf , VLC_THREAD_PRIORITY_LOW );
524 p_object = (vlc_object_t *)vlc_object_get( VLCIntf,
527 if( p_object != NULL )
529 var_Set( p_object, strdup([o_data name]), [o_data value] );
530 vlc_object_release( p_object );
540 @implementation VLCControls (NSMenuValidation)
542 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
544 BOOL bEnabled = TRUE;
546 intf_thread_t * p_intf = VLCIntf;
547 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
550 if( p_playlist != NULL )
552 vlc_mutex_lock( &p_playlist->object_lock );
556 #define p_input p_playlist->p_input
558 if( [[o_mi title] isEqualToString: _NS("Faster")] ||
559 [[o_mi title] isEqualToString: _NS("Slower")] )
561 if( p_input != NULL )
563 bEnabled = p_input->input.b_can_pace_control;
570 else if( [[o_mi title] isEqualToString: _NS("Stop")] )
572 if( p_input == NULL )
576 [o_main setupMenus]; /* Make sure input menu is up to date */
578 else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
579 [[o_mi title] isEqualToString: _NS("Next")] )
581 bEnabled = p_playlist->i_size > 1;
583 else if( [[o_mi title] isEqualToString: _NS("Random")] )
586 var_Get( p_playlist, "random", &val );
587 i_state = val.b_bool ? NSOnState : NSOffState;
588 [o_mi setState: i_state];
590 else if( [[o_mi title] isEqualToString: _NS("Repeat One")] )
593 var_Get( p_playlist, "repeat", &val );
594 i_state = val.b_bool ? NSOnState : NSOffState;
595 [o_mi setState: i_state];
597 else if( [[o_mi title] isEqualToString: _NS("Repeat All")] )
600 var_Get( p_playlist, "loop", &val );
601 i_state = val.b_bool ? NSOnState : NSOffState;
602 [o_mi setState: i_state];
604 else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
605 [[o_mi title] isEqualToString: _NS("Step Backward")] )
607 if( p_input != NULL )
609 var_Get( p_input, "seekable", &val);
610 bEnabled = val.b_bool;
613 else if( [[o_mi title] isEqualToString: _NS("Mute")] )
615 [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
616 [o_main setupMenus]; /* Make sure audio menu is up to date */
618 else if( [[o_mi title] isEqualToString: _NS("Half Size")] ||
619 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
620 [[o_mi title] isEqualToString: _NS("Double Size")] ||
621 [[o_mi title] isEqualToString: _NS("Fit to Screen")] ||
622 [[o_mi title] isEqualToString: _NS("Snapshot")] ||
623 [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
624 [[o_mi title] isEqualToString: _NS("Float on Top")] )
627 NSArray *o_windows = [NSApp orderedWindows];
628 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
631 vout_thread_t *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
635 if( [[o_mi title] isEqualToString: _NS("Float on Top")] )
637 var_Get( p_vout, "video-on-top", &val );
638 [o_mi setState: val.b_bool ? NSOnState : NSOffState];
641 while( (o_window = [o_enumerator nextObject]))
643 if( [[o_window className] isEqualToString: @"VLCWindow"] )
649 vlc_object_release( (vlc_object_t *)p_vout );
651 else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] )
653 var_Get( p_playlist, "fullscreen", &val );
654 [o_mi setState: val.b_bool];
657 [o_main setupMenus]; /* Make sure video menu is up to date */
660 vlc_mutex_unlock( &p_playlist->object_lock );
661 vlc_object_release( p_playlist );
668 /*****************************************************************************
669 * VLCMenuExt implementation
670 *****************************************************************************
671 * Object connected to a playlistitem which remembers the data belonging to
672 * the variable of the autogenerated menu
673 *****************************************************************************/
674 @implementation VLCMenuExt
676 - (id)initWithVar: (const char *)_psz_name Object: (int)i_id
677 Value: (vlc_value_t)val ofType: (int)_i_type
683 psz_name = strdup( _psz_name );