1 /*****************************************************************************
2 * controls.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: controls.m,v 1.52 2003/11/06 18:55:22 hartman Exp $
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Christophe Massiot <massiot@via.ecp.fr>
9 * Derk-Jan Hartman <thedj@users.sourceforge.net>
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
46 intf_thread_t * p_intf = [NSApp getIntf];
48 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
50 if( p_playlist == NULL )
55 if( playlist_IsPlaying( p_playlist ) )
57 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Pause" ) );
58 playlist_Pause( p_playlist );
59 vlc_object_release( p_playlist );
63 if( !playlist_IsEmpty( p_playlist ) )
65 playlist_Play( p_playlist );
66 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Play" ) );
67 vlc_object_release( p_playlist );
71 vlc_object_release( p_playlist );
72 [o_open openFileGeneric: nil];
77 - (IBAction)stop:(id)sender
79 intf_thread_t * p_intf = [NSApp getIntf];
80 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
82 if( p_playlist != NULL )
84 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Stop" ) );
85 playlist_Stop( p_playlist );
86 vlc_object_release( p_playlist );
90 - (IBAction)faster:(id)sender
92 intf_thread_t * p_intf = [NSApp getIntf];
93 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
97 vlc_value_t val; val.b_bool = VLC_TRUE;
99 var_Set( p_input, "rate-faster", val );
100 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Faster" ) );
101 vlc_object_release( p_input );
105 - (IBAction)slower:(id)sender
107 intf_thread_t * p_intf = [NSApp getIntf];
108 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
110 if( p_input != NULL )
112 vlc_value_t val; val.b_bool = VLC_TRUE;
114 var_Set( p_input, "rate-slower", val );
115 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Slower" ) );
116 vlc_object_release( p_input );
120 - (IBAction)prev:(id)sender
123 intf_thread_t * p_intf = [NSApp getIntf];
125 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
127 if( p_playlist == NULL )
132 vlc_mutex_lock( &p_playlist->object_lock );
134 if( p_playlist->p_input == NULL )
136 vlc_mutex_unlock( &p_playlist->object_lock );
137 vlc_object_release( p_playlist );
141 vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
142 val.b_bool = VLC_TRUE;
144 #define p_area p_playlist->p_input->stream.p_selected_area
145 if( p_area->i_part > 0 && p_area->i_part_nb > 1)
147 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
148 var_Set( p_playlist->p_input, "prev-chapter", val );
149 vlc_mutex_unlock( &p_playlist->object_lock );
151 else if( p_area->i_id > 1 )
153 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
154 var_Set( p_playlist->p_input, "prev-title", val );
155 vlc_mutex_unlock( &p_playlist->object_lock );
159 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
160 vlc_mutex_unlock( &p_playlist->object_lock );
161 playlist_Prev( p_playlist );
165 vlc_object_release( p_playlist );
166 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Previous" ) );
169 - (IBAction)next:(id)sender
172 intf_thread_t * p_intf = [NSApp getIntf];
174 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
176 if( p_playlist == NULL )
181 vlc_mutex_lock( &p_playlist->object_lock );
183 if( p_playlist->p_input == NULL )
185 vlc_mutex_unlock( &p_playlist->object_lock );
186 vlc_object_release( p_playlist );
190 vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
191 val.b_bool = VLC_TRUE;
193 #define p_area p_playlist->p_input->stream.p_selected_area
194 if( p_area->i_part < p_area->i_part_nb - 1 && p_area->i_part_nb > 1 )
196 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
197 var_Set( p_playlist->p_input, "next-chapter", val );
198 vlc_mutex_unlock( &p_playlist->object_lock );
200 else if( p_area->i_id < p_playlist->p_input->stream.i_area_nb && p_playlist->p_input->stream.i_area_nb > 1 )
202 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
203 var_Set( p_playlist->p_input, "next-title", val );
204 vlc_mutex_unlock( &p_playlist->object_lock );
208 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
209 vlc_mutex_unlock( &p_playlist->object_lock );
210 playlist_Next( p_playlist );
214 vlc_object_release( p_playlist );
215 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Next" ) );
218 - (IBAction)random:(id)sender
220 intf_thread_t * p_intf = [NSApp getIntf];
222 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
224 if( p_playlist == NULL )
229 var_Get( p_playlist, "random", &val );
230 val.b_bool = !val.b_bool;
231 var_Set( p_playlist, "random", val );
234 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Shuffle On" ) );
238 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Shuffle Off" ) );
241 vlc_object_release( p_playlist );
244 - (IBAction)repeat:(id)sender
246 intf_thread_t * p_intf = [NSApp getIntf];
248 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
250 if( p_playlist == NULL )
255 var_Get( p_playlist, "repeat", &val );
256 val.b_bool = !val.b_bool;
257 var_Set( p_playlist, "repeat", val );
260 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Repeat On" ) );
264 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Repeat Off" ) );
267 vlc_object_release( p_playlist );
270 - (IBAction)loop:(id)sender
272 intf_thread_t * p_intf = [NSApp getIntf];
274 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
276 if( p_playlist == NULL )
281 var_Get( p_playlist, "loop", &val );
282 val.b_bool = !val.b_bool;
283 var_Set( p_playlist, "loop", val );
286 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Loop On" ) );
290 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Loop Off" ) );
293 vlc_object_release( p_playlist );
296 - (IBAction)forward:(id)sender
298 intf_thread_t * p_intf = [NSApp getIntf];
299 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
301 if( p_input != NULL )
304 time.i_time = 5 * 1000000;
305 var_Set( p_input, "time-offset", time );
306 vlc_object_release( p_input );
310 - (IBAction)backward:(id)sender
312 intf_thread_t * p_intf = [NSApp getIntf];
313 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
315 if( p_input != NULL )
318 time.i_time = -5 * 1000000;
319 var_Set( p_input, "time-offset", time );
320 vlc_object_release( p_input );
324 - (IBAction)volumeUp:(id)sender
326 intf_thread_t * p_intf = [NSApp getIntf];
328 if( p_intf->p_sys->b_mute )
333 aout_VolumeUp( p_intf, 1, NULL );
335 [self updateVolumeSlider];
338 - (IBAction)volumeDown:(id)sender
340 intf_thread_t * p_intf = [NSApp getIntf];
342 if( p_intf->p_sys->b_mute )
347 aout_VolumeDown( p_intf, 1, NULL );
349 [self updateVolumeSlider];
352 - (IBAction)mute:(id)sender
354 intf_thread_t * p_intf = [NSApp getIntf];
355 audio_volume_t i_volume;
357 aout_VolumeMute( p_intf, &i_volume );
358 p_intf->p_sys->b_mute = ( i_volume == 0 );
360 [self updateVolumeSlider];
363 - (IBAction)volumeSliderUpdated:(id)sender
365 intf_thread_t * p_intf = [NSApp getIntf];
366 audio_volume_t i_volume = (audio_volume_t)[sender intValue];
368 aout_VolumeSet( p_intf, i_volume * AOUT_VOLUME_STEP );
371 - (void)updateVolumeSlider
373 intf_thread_t * p_intf = [NSApp getIntf];
374 audio_volume_t i_volume;
377 aout_VolumeGet( p_intf, &i_volume );
379 [o_volumeslider setFloatValue: (float)(i_volume / AOUT_VOLUME_STEP)];
381 sprintf( string, "Vol %d%%", i_volume*100/AOUT_VOLUME_MAX );
382 vout_OSDMessage( (vlc_object_t *)p_intf, string );
385 - (IBAction)windowAction:(id)sender
387 id o_window = [NSApp keyWindow];
388 NSString *o_title = [sender title];
389 NSArray *o_windows = [NSApp windows];
390 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
391 vout_thread_t *p_vout = vlc_object_find( [NSApp getIntf], VLC_OBJECT_VOUT,
396 while ((o_window = [o_enumerator nextObject]))
398 if( [[o_window className] isEqualToString: @"VLCWindow"] )
400 if( [o_title isEqualToString: _NS("Fullscreen") ] )
401 [o_window toggleFullscreen];
402 else if( [o_title isEqualToString: _NS("Half Size") ] )
403 [o_window scaleWindowWithFactor: 0.5];
404 else if( [o_title isEqualToString: _NS("Normal Size") ] )
405 [o_window scaleWindowWithFactor: 1.0];
406 else if( [o_title isEqualToString: _NS("Double Size") ] )
407 [o_window scaleWindowWithFactor: 2.0];
408 else if( [o_title isEqualToString: _NS("Float On Top") ] )
409 [o_window toggleFloatOnTop];
410 else if( [o_title isEqualToString: _NS("Fit To Screen") ] )
412 if( ![o_window isZoomed] )
413 [o_window performZoom:self];
418 vlc_object_release( (vlc_object_t *)p_vout );
422 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
423 target:(vlc_object_t *)p_object
424 var:(const char *)psz_variable
425 selector:(SEL)pf_callback
427 vlc_value_t val, text;
428 int i_type = var_Type( p_object, psz_variable );
430 switch( i_type & VLC_VAR_TYPE )
434 case VLC_VAR_VARIABLE:
436 case VLC_VAR_INTEGER:
439 /* Variable doesn't exist or isn't handled */
443 /* Make sure we want to display the variable */
444 if( i_type & VLC_VAR_HASCHOICE )
446 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
447 if( val.i_int == 0 ) return;
448 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
452 /* Get the descriptive name of the variable */
453 var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
454 [o_mi setTitle: [NSApp localizedString: text.psz_string ?
455 text.psz_string : strdup( psz_variable ) ]];
457 var_Get( p_object, psz_variable, &val );
458 if( i_type & VLC_VAR_HASCHOICE )
460 NSMenu *o_menu = [o_mi submenu];
462 [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
463 var:psz_variable selector:pf_callback];
465 if( text.psz_string ) free( text.psz_string );
470 switch( i_type & VLC_VAR_TYPE )
473 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
474 Value: val ofType: i_type];
475 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
479 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
480 Value: val ofType: i_type];
481 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
482 [o_mi setState: val.b_bool ? TRUE : FALSE ];
486 if( text.psz_string ) free( text.psz_string );
490 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
491 if( text.psz_string ) free( text.psz_string );
495 - (void)setupVarMenu:(NSMenu *)o_menu
496 forMenuItem: (NSMenuItem *)o_parent
497 target:(vlc_object_t *)p_object
498 var:(const char *)psz_variable
499 selector:(SEL)pf_callback
501 vlc_value_t val, val_list, text_list;
502 int i_type, i, i_nb_items;
504 /* remove previous items */
505 i_nb_items = [o_menu numberOfItems];
506 for( i = 0; i < i_nb_items; i++ )
508 [o_menu removeItemAtIndex: 0];
511 /* Check the type of the object variable */
512 i_type = var_Type( p_object, psz_variable );
514 /* Make sure we want to display the variable */
515 if( i_type & VLC_VAR_HASCHOICE )
517 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
518 if( val.i_int == 0 ) return;
519 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
527 switch( i_type & VLC_VAR_TYPE )
531 case VLC_VAR_VARIABLE:
533 case VLC_VAR_INTEGER:
536 /* Variable doesn't exist or isn't handled */
540 if( var_Get( p_object, psz_variable, &val ) < 0 )
545 if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
546 &val_list, &text_list ) < 0 )
548 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
552 /* make (un)sensitive */
553 [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
555 for( i = 0; i < val_list.p_list->i_count; i++ )
557 vlc_value_t another_val;
559 NSString *o_title = @"";
562 switch( i_type & VLC_VAR_TYPE )
564 case VLC_VAR_VARIABLE:
566 /* This is causing crashes for the moment.
567 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
568 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
570 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
571 Value: val ofType: i_type];
572 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
575 NSMenu *o_menu = [o_lmi submenu];
577 [self setupVarMenu: o_menu forMenuItem: o_lmi target:p_object
578 var:psz_variable selector:pf_callback];
583 another_val.psz_string =
584 strdup(val_list.p_list->p_values[i].psz_string);
586 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
587 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
589 o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
590 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
591 Value: another_val ofType: i_type];
592 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
593 [o_lmi setTarget: self];
595 if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) )
596 [o_lmi setState: TRUE ];
600 case VLC_VAR_INTEGER:
602 o_title = text_list.p_list->p_values[i].psz_string ?
603 [NSApp localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
604 [NSString stringWithFormat: @"%d",
605 val_list.p_list->p_values[i].i_int];
607 o_lmi = [[o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""] retain ];
608 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
609 Value: val_list.p_list->p_values[i] ofType: i_type];
610 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[ o_data retain]]];
611 [o_lmi setTarget: self];
613 if( val_list.p_list->p_values[i].i_int == val.i_int )
614 [o_lmi setState: TRUE ];
622 /* clean up everything */
623 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
624 var_Change( p_object, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
627 - (IBAction)toggleVar:(id)sender
629 NSMenuItem *o_mi = (NSMenuItem *)sender;
630 VLCMenuExt *o_data = [[o_mi representedObject] pointerValue];
631 [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
632 toTarget: self withObject: o_data];
637 - (int)toggleVarThread: (id)_o_data
639 vlc_object_t *p_object;
640 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
641 VLCMenuExt *o_data = (VLCMenuExt *)_o_data;
643 vlc_thread_set_priority( [NSApp getIntf] , VLC_THREAD_PRIORITY_LOW );
645 p_object = (vlc_object_t *)vlc_object_get( [NSApp getIntf],
648 if( p_object != NULL )
650 var_Set( p_object, strdup([o_data name]), [o_data value] );
651 vlc_object_release( p_object );
661 @implementation VLCControls (NSMenuValidation)
663 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
665 BOOL bEnabled = TRUE;
667 intf_thread_t * p_intf = [NSApp getIntf];
668 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
671 if( p_playlist != NULL )
673 vlc_mutex_lock( &p_playlist->object_lock );
676 #define p_input p_playlist->p_input
678 if( [[o_mi title] isEqualToString: _NS("Faster")] ||
679 [[o_mi title] isEqualToString: _NS("Slower")] )
681 if( p_playlist != NULL && p_input != NULL )
683 vlc_mutex_lock( &p_input->stream.stream_lock );
684 bEnabled = p_input->stream.b_pace_control;
685 vlc_mutex_unlock( &p_input->stream.stream_lock );
692 else if( [[o_mi title] isEqualToString: _NS("Stop")] )
694 if( p_playlist == NULL || p_input == NULL )
699 else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
700 [[o_mi title] isEqualToString: _NS("Next")] )
702 if( p_playlist == NULL )
708 bEnabled = p_playlist->i_size > 1;
710 if( p_input != NULL )
712 vlc_mutex_lock( &p_input->stream.stream_lock );
713 bEnabled |= p_input->stream.i_area_nb > 1;
714 vlc_mutex_unlock( &p_input->stream.stream_lock );
718 else if( [[o_mi title] isEqualToString: _NS("Shuffle")] )
721 var_Get( p_playlist, "random", &val );
722 i_state = val.b_bool ? NSOnState : NSOffState;
723 [o_mi setState: i_state];
725 else if( [[o_mi title] isEqualToString: _NS("Repeat Item")] )
728 var_Get( p_playlist, "repeat", &val );
729 i_state = val.b_bool ? NSOnState : NSOffState;
730 [o_mi setState: i_state];
732 else if( [[o_mi title] isEqualToString: _NS("Repeat Playlist")] )
735 var_Get( p_playlist, "loop", &val );
736 i_state = val.b_bool ? NSOnState : NSOffState;
737 [o_mi setState: i_state];
739 else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
740 [[o_mi title] isEqualToString: _NS("Step Backward")] )
742 if( p_playlist != NULL && p_input != NULL )
744 vlc_mutex_lock( &p_input->stream.stream_lock );
745 bEnabled = p_input->stream.b_seekable;
746 vlc_mutex_unlock( &p_input->stream.stream_lock );
753 else if( [[o_mi title] isEqualToString: _NS("Mute")] )
755 [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
757 else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
758 [[o_mi title] isEqualToString: _NS("Half Size")] ||
759 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
760 [[o_mi title] isEqualToString: _NS("Double Size")] ||
761 [[o_mi title] isEqualToString: _NS("Fit To Screen")] ||
762 [[o_mi title] isEqualToString: _NS("Float On Top")] )
765 NSArray *o_windows = [NSApp windows];
766 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
769 if ( [[o_mi title] isEqualToString: _NS("Float On Top")] )
771 int i_state = config_GetInt( p_playlist, "macosx-float" ) ?
772 NSOnState : NSOffState;
773 [o_mi setState: i_state];
776 vout_thread_t *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
780 while ((o_window = [o_enumerator nextObject]))
782 if( [[o_window className] isEqualToString: @"VLCWindow"] )
788 vlc_object_release( (vlc_object_t *)p_vout );
792 if( p_playlist != NULL )
794 vlc_mutex_unlock( &p_playlist->object_lock );
795 vlc_object_release( p_playlist );
803 /*****************************************************************************
804 * VLCMenuExt implementation
805 *****************************************************************************
806 * Object connected to a playlistitem which remembers the data belonging to
807 * the variable of the autogenerated menu
808 *****************************************************************************/
809 @implementation VLCMenuExt
811 - (id)initWithVar: (const char *)_psz_name Object: (int)i_id
812 Value: (vlc_value_t)val ofType: (int)_i_type
818 psz_name = strdup( _psz_name );