1 /*****************************************************************************
2 * controls.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: controls.m,v 1.50 2003/10/31 15:54:53 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( 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( 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( 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( 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( 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( 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( 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( p_intf, _( "Shuffle On" ) );
238 vout_OSDMessage( 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( p_intf, _( "Repeat On" ) );
264 vout_OSDMessage( 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( p_intf, _( "Loop On" ) );
290 vout_OSDMessage( 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( 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];
417 vlc_object_release( (vlc_object_t *)p_vout );
421 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
422 target:(vlc_object_t *)p_object
423 var:(const char *)psz_variable
424 selector:(SEL)pf_callback
426 vlc_value_t val, text;
427 int i_type = var_Type( p_object, psz_variable );
429 switch( i_type & VLC_VAR_TYPE )
433 case VLC_VAR_VARIABLE:
435 case VLC_VAR_INTEGER:
438 /* Variable doesn't exist or isn't handled */
442 /* Make sure we want to display the variable */
443 if( i_type & VLC_VAR_HASCHOICE )
445 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
446 if( val.i_int == 0 ) return;
447 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
451 /* Get the descriptive name of the variable */
452 var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
453 [o_mi setTitle: [NSApp localizedString: text.psz_string ?
454 text.psz_string : strdup( psz_variable ) ]];
456 var_Get( p_object, psz_variable, &val );
457 if( i_type & VLC_VAR_HASCHOICE )
459 NSMenu *o_menu = [o_mi submenu];
461 [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
462 var:psz_variable selector:pf_callback];
464 if( text.psz_string ) free( text.psz_string );
469 switch( i_type & VLC_VAR_TYPE )
472 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
473 Value: val ofType: i_type];
474 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
478 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
479 Value: val ofType: i_type];
480 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
481 [o_mi setState: val.b_bool ? TRUE : FALSE ];
485 if( text.psz_string ) free( text.psz_string );
489 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
490 if( text.psz_string ) free( text.psz_string );
494 - (void)setupVarMenu:(NSMenu *)o_menu
495 forMenuItem: (NSMenuItem *)o_parent
496 target:(vlc_object_t *)p_object
497 var:(const char *)psz_variable
498 selector:(SEL)pf_callback
500 vlc_value_t val, val_list, text_list;
501 int i_type, i, i_nb_items;
503 /* remove previous items */
504 i_nb_items = [o_menu numberOfItems];
505 for( i = 0; i < i_nb_items; i++ )
507 [o_menu removeItemAtIndex: 0];
510 /* Check the type of the object variable */
511 i_type = var_Type( p_object, psz_variable );
513 /* Make sure we want to display the variable */
514 if( i_type & VLC_VAR_HASCHOICE )
516 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
517 if( val.i_int == 0 ) return;
518 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
526 switch( i_type & VLC_VAR_TYPE )
530 case VLC_VAR_VARIABLE:
532 case VLC_VAR_INTEGER:
535 /* Variable doesn't exist or isn't handled */
539 if( var_Get( p_object, psz_variable, &val ) < 0 )
544 if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
545 &val_list, &text_list ) < 0 )
547 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
551 /* make (un)sensitive */
552 [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
554 for( i = 0; i < val_list.p_list->i_count; i++ )
556 vlc_value_t another_val;
558 NSString *o_title = @"";
561 switch( i_type & VLC_VAR_TYPE )
563 case VLC_VAR_VARIABLE:
565 /* This is causing crashes for the moment.
566 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
567 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
569 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
570 Value: val ofType: i_type];
571 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
574 NSMenu *o_menu = [o_lmi submenu];
576 [self setupVarMenu: o_menu forMenuItem: o_lmi target:p_object
577 var:psz_variable selector:pf_callback];
582 another_val.psz_string =
583 strdup(val_list.p_list->p_values[i].psz_string);
585 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
586 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
588 o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
589 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
590 Value: another_val ofType: i_type];
591 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
592 [o_lmi setTarget: self];
594 if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) )
595 [o_lmi setState: TRUE ];
599 case VLC_VAR_INTEGER:
601 o_title = text_list.p_list->p_values[i].psz_string ?
602 [NSApp localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
603 [NSString stringWithFormat: @"%d",
604 val_list.p_list->p_values[i].i_int];
606 o_lmi = [[o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""] retain ];
607 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
608 Value: val_list.p_list->p_values[i] ofType: i_type];
609 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[ o_data retain]]];
610 [o_lmi setTarget: self];
612 if( val_list.p_list->p_values[i].i_int == val.i_int )
613 [o_lmi setState: TRUE ];
621 /* clean up everything */
622 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
623 var_Change( p_object, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
626 - (IBAction)toggleVar:(id)sender
628 NSMenuItem *o_mi = (NSMenuItem *)sender;
629 VLCMenuExt *o_data = [[o_mi representedObject] pointerValue];
630 [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
631 toTarget: self withObject: o_data];
636 - (int)toggleVarThread: (id)_o_data
638 vlc_object_t *p_object;
639 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
640 VLCMenuExt *o_data = (VLCMenuExt *)_o_data;
642 vlc_thread_set_priority( [NSApp getIntf] , VLC_THREAD_PRIORITY_LOW );
644 p_object = (vlc_object_t *)vlc_object_get( [NSApp getIntf],
647 if( p_object != NULL )
649 var_Set( p_object, strdup([o_data name]), [o_data value] );
650 vlc_object_release( p_object );
660 @implementation VLCControls (NSMenuValidation)
662 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
664 BOOL bEnabled = TRUE;
666 intf_thread_t * p_intf = [NSApp getIntf];
667 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
670 if( p_playlist != NULL )
672 vlc_mutex_lock( &p_playlist->object_lock );
675 #define p_input p_playlist->p_input
677 if( [[o_mi title] isEqualToString: _NS("Faster")] ||
678 [[o_mi title] isEqualToString: _NS("Slower")] )
680 if( p_playlist != NULL && p_input != NULL )
682 vlc_mutex_lock( &p_input->stream.stream_lock );
683 bEnabled = p_input->stream.b_pace_control;
684 vlc_mutex_unlock( &p_input->stream.stream_lock );
691 else if( [[o_mi title] isEqualToString: _NS("Stop")] )
693 if( p_playlist == NULL || p_input == NULL )
698 else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
699 [[o_mi title] isEqualToString: _NS("Next")] )
701 if( p_playlist == NULL )
707 bEnabled = p_playlist->i_size > 1;
709 if( p_input != NULL )
711 vlc_mutex_lock( &p_input->stream.stream_lock );
712 bEnabled |= p_input->stream.i_area_nb > 1;
713 vlc_mutex_unlock( &p_input->stream.stream_lock );
717 else if( [[o_mi title] isEqualToString: _NS("Shuffle")] )
720 var_Get( p_playlist, "random", &val );
721 i_state = val.b_bool ? NSOnState : NSOffState;
722 [o_mi setState: i_state];
724 else if( [[o_mi title] isEqualToString: _NS("Repeat Item")] )
727 var_Get( p_playlist, "repeat", &val );
728 i_state = val.b_bool ? NSOnState : NSOffState;
729 [o_mi setState: i_state];
731 else if( [[o_mi title] isEqualToString: _NS("Repeat Playlist")] )
734 var_Get( p_playlist, "loop", &val );
735 i_state = val.b_bool ? NSOnState : NSOffState;
736 [o_mi setState: i_state];
738 else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
739 [[o_mi title] isEqualToString: _NS("Step Backward")] )
741 if( p_playlist != NULL && p_input != NULL )
743 vlc_mutex_lock( &p_input->stream.stream_lock );
744 bEnabled = p_input->stream.b_seekable;
745 vlc_mutex_unlock( &p_input->stream.stream_lock );
752 else if( [[o_mi title] isEqualToString: _NS("Mute")] )
754 [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
756 else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
757 [[o_mi title] isEqualToString: _NS("Half Size")] ||
758 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
759 [[o_mi title] isEqualToString: _NS("Double Size")] ||
760 [[o_mi title] isEqualToString: _NS("Fit To Screen")] ||
761 [[o_mi title] isEqualToString: _NS("Float On Top")] )
764 NSArray *o_windows = [NSApp windows];
765 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
768 if ( [[o_mi title] isEqualToString: _NS("Float On Top")] )
770 int i_state = config_GetInt( p_playlist, "macosx-float" ) ?
771 NSOnState : NSOffState;
772 [o_mi setState: i_state];
775 vout_thread_t *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
779 while ((o_window = [o_enumerator nextObject]))
781 if( [[o_window className] isEqualToString: @"VLCWindow"] )
787 vlc_object_release( (vlc_object_t *)p_vout );
791 if( p_playlist != NULL )
793 vlc_mutex_unlock( &p_playlist->object_lock );
794 vlc_object_release( p_playlist );
802 /*****************************************************************************
803 * VLCMenuExt implementation
804 *****************************************************************************
805 * Object connected to a playlistitem which remembers the data belonging to
806 * the variable of the autogenerated menu
807 *****************************************************************************/
808 @implementation VLCMenuExt
810 - (id)initWithVar: (const char *)_psz_name Object: (int)i_id
811 Value: (vlc_value_t)val ofType: (int)_i_type
817 psz_name = strdup( _psz_name );