1 /*****************************************************************************
2 * controls.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: controls.m,v 1.57 2003/12/08 19:50:22 gbazin 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
47 playlist_t * p_playlist;
48 intf_thread_t * p_intf = [NSApp getIntf];
49 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
52 val.i_int = PLAYING_S;
55 var_Get( p_input, "state", &val );
57 if( p_input && val.i_int != PAUSE_S )
59 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Pause" ) );
61 var_Set( p_input, "state", val );
65 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
69 vlc_mutex_lock( &p_playlist->object_lock );
70 if( p_playlist->i_size )
72 vlc_mutex_unlock( &p_playlist->object_lock );
73 vout_OSDMessage( VLC_OBJECT(p_intf), _( "Play" ) );
74 playlist_Play( p_playlist );
75 vlc_object_release( p_playlist );
79 vlc_mutex_unlock( &p_playlist->object_lock );
80 vlc_object_release( p_playlist );
81 [o_open openFileGeneric: nil];
85 if( p_input ) vlc_object_release( p_input );
88 - (IBAction)stop:(id)sender
90 intf_thread_t * p_intf = [NSApp getIntf];
91 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
93 if( p_playlist != NULL )
95 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Stop" ) );
96 playlist_Stop( p_playlist );
97 vlc_object_release( p_playlist );
101 - (IBAction)faster:(id)sender
103 intf_thread_t * p_intf = [NSApp getIntf];
104 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
106 if( p_input != NULL )
108 vlc_value_t val; val.b_bool = VLC_TRUE;
110 var_Set( p_input, "rate-faster", val );
111 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Faster" ) );
112 vlc_object_release( p_input );
116 - (IBAction)slower:(id)sender
118 intf_thread_t * p_intf = [NSApp getIntf];
119 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
121 if( p_input != NULL )
123 vlc_value_t val; val.b_bool = VLC_TRUE;
125 var_Set( p_input, "rate-slower", val );
126 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Slower" ) );
127 vlc_object_release( p_input );
131 - (IBAction)prev:(id)sender
133 intf_thread_t * p_intf = [NSApp getIntf];
134 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
138 playlist_Prev( p_playlist );
139 vlc_object_release( p_playlist );
140 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Previous" ) );
144 - (IBAction)next:(id)sender
146 intf_thread_t * p_intf = [NSApp getIntf];
147 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
151 playlist_Next( p_playlist );
152 vlc_object_release( p_playlist );
153 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Next" ) );
157 - (IBAction)random:(id)sender
159 intf_thread_t * p_intf = [NSApp getIntf];
161 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
163 if( p_playlist == NULL )
168 var_Get( p_playlist, "random", &val );
169 val.b_bool = !val.b_bool;
170 var_Set( p_playlist, "random", val );
173 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Shuffle On" ) );
177 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Shuffle Off" ) );
180 p_intf->p_sys->b_playlist_update = VLC_TRUE;
181 p_intf->p_sys->b_intf_update = VLC_TRUE;
182 vlc_object_release( p_playlist );
185 - (IBAction)repeat:(id)sender
187 intf_thread_t * p_intf = [NSApp getIntf];
189 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
191 if( p_playlist == NULL )
196 var_Get( p_playlist, "repeat", &val );
197 val.b_bool = !val.b_bool;
198 var_Set( p_playlist, "repeat", val );
201 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Repeat On" ) );
205 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Repeat Off" ) );
208 p_intf->p_sys->b_playlist_update = VLC_TRUE;
209 p_intf->p_sys->b_intf_update = VLC_TRUE;
210 vlc_object_release( p_playlist );
213 - (IBAction)loop:(id)sender
215 intf_thread_t * p_intf = [NSApp getIntf];
217 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
219 if( p_playlist == NULL )
224 var_Get( p_playlist, "loop", &val );
225 val.b_bool = !val.b_bool;
226 var_Set( p_playlist, "loop", val );
229 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Loop On" ) );
233 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Loop Off" ) );
236 p_intf->p_sys->b_playlist_update = VLC_TRUE;
237 p_intf->p_sys->b_intf_update = VLC_TRUE;
238 vlc_object_release( p_playlist );
241 - (IBAction)forward:(id)sender
243 intf_thread_t * p_intf = [NSApp getIntf];
244 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
246 if( p_input != NULL )
249 time.i_time = 10 * 1000000;
250 var_Set( p_input, "time-offset", time );
251 vlc_object_release( p_input );
255 - (IBAction)backward:(id)sender
257 intf_thread_t * p_intf = [NSApp getIntf];
258 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
260 if( p_input != NULL )
263 time.i_time = -10 * 1000000;
264 var_Set( p_input, "time-offset", time );
265 vlc_object_release( p_input );
269 - (IBAction)volumeUp:(id)sender
271 intf_thread_t * p_intf = [NSApp getIntf];
273 if( p_intf->p_sys->b_mute )
278 aout_VolumeUp( p_intf, 1, NULL );
280 [self updateVolumeSlider];
283 - (IBAction)volumeDown:(id)sender
285 intf_thread_t * p_intf = [NSApp getIntf];
287 if( p_intf->p_sys->b_mute )
292 aout_VolumeDown( p_intf, 1, NULL );
294 [self updateVolumeSlider];
297 - (IBAction)mute:(id)sender
299 intf_thread_t * p_intf = [NSApp getIntf];
300 audio_volume_t i_volume;
302 aout_VolumeMute( p_intf, &i_volume );
303 p_intf->p_sys->b_mute = ( i_volume == 0 );
305 [self updateVolumeSlider];
308 - (IBAction)volumeSliderUpdated:(id)sender
310 intf_thread_t * p_intf = [NSApp getIntf];
311 audio_volume_t i_volume = (audio_volume_t)[sender intValue];
313 aout_VolumeSet( p_intf, i_volume * AOUT_VOLUME_STEP );
316 - (void)updateVolumeSlider
318 intf_thread_t * p_intf = [NSApp getIntf];
319 audio_volume_t i_volume;
322 aout_VolumeGet( p_intf, &i_volume );
324 [o_volumeslider setFloatValue: (float)(i_volume / AOUT_VOLUME_STEP)];
326 sprintf( string, "Vol %d%%", i_volume*100/AOUT_VOLUME_MAX );
327 vout_OSDMessage( (vlc_object_t *)p_intf, string );
330 - (IBAction)windowAction:(id)sender
332 id o_window = [NSApp keyWindow];
333 NSString *o_title = [sender title];
334 NSArray *o_windows = [NSApp orderedWindows];
335 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
336 vout_thread_t *p_vout = vlc_object_find( [NSApp getIntf], VLC_OBJECT_VOUT,
341 while ((o_window = [o_enumerator nextObject]))
343 if( [[o_window className] isEqualToString: @"VLCWindow"] )
345 if( [o_title isEqualToString: _NS("Half Size") ] )
346 [o_window scaleWindowWithFactor: 0.5];
347 else if( [o_title isEqualToString: _NS("Normal Size") ] )
348 [o_window scaleWindowWithFactor: 1.0];
349 else if( [o_title isEqualToString: _NS("Double Size") ] )
350 [o_window scaleWindowWithFactor: 2.0];
351 else if( [o_title isEqualToString: _NS("Float On Top") ] )
352 [o_window toggleFloatOnTop];
353 else if( [o_title isEqualToString: _NS("Fit To Screen") ] )
355 if( ![o_window isZoomed] )
356 [o_window performZoom:self];
359 [o_window toggleFullscreen];
363 vlc_object_release( (vlc_object_t *)p_vout );
367 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
368 target:(vlc_object_t *)p_object
369 var:(const char *)psz_variable
370 selector:(SEL)pf_callback
372 vlc_value_t val, text;
373 int i_type = var_Type( p_object, psz_variable );
375 switch( i_type & VLC_VAR_TYPE )
379 case VLC_VAR_VARIABLE:
381 case VLC_VAR_INTEGER:
384 /* Variable doesn't exist or isn't handled */
388 /* Make sure we want to display the variable */
389 if( i_type & VLC_VAR_HASCHOICE )
391 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
392 if( val.i_int == 0 ) return;
393 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
397 /* Get the descriptive name of the variable */
398 var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
399 [o_mi setTitle: [NSApp localizedString: text.psz_string ?
400 text.psz_string : strdup( psz_variable ) ]];
402 var_Get( p_object, psz_variable, &val );
403 if( i_type & VLC_VAR_HASCHOICE )
405 NSMenu *o_menu = [o_mi submenu];
407 [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
408 var:psz_variable selector:pf_callback];
410 if( text.psz_string ) free( text.psz_string );
415 switch( i_type & VLC_VAR_TYPE )
418 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
419 Value: val ofType: i_type];
420 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
424 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
425 Value: val ofType: i_type];
426 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
427 [o_mi setState: val.b_bool ? TRUE : FALSE ];
431 if( text.psz_string ) free( text.psz_string );
435 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
436 if( text.psz_string ) free( text.psz_string );
440 - (void)setupVarMenu:(NSMenu *)o_menu
441 forMenuItem: (NSMenuItem *)o_parent
442 target:(vlc_object_t *)p_object
443 var:(const char *)psz_variable
444 selector:(SEL)pf_callback
446 vlc_value_t val, val_list, text_list;
447 int i_type, i, i_nb_items;
449 /* remove previous items */
450 i_nb_items = [o_menu numberOfItems];
451 for( i = 0; i < i_nb_items; i++ )
453 [o_menu removeItemAtIndex: 0];
456 /* Check the type of the object variable */
457 i_type = var_Type( p_object, psz_variable );
459 /* Make sure we want to display the variable */
460 if( i_type & VLC_VAR_HASCHOICE )
462 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
463 if( val.i_int == 0 ) return;
464 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
472 switch( i_type & VLC_VAR_TYPE )
476 case VLC_VAR_VARIABLE:
478 case VLC_VAR_INTEGER:
481 /* Variable doesn't exist or isn't handled */
485 if( var_Get( p_object, psz_variable, &val ) < 0 )
490 if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
491 &val_list, &text_list ) < 0 )
493 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
497 /* make (un)sensitive */
498 [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
500 for( i = 0; i < val_list.p_list->i_count; i++ )
502 vlc_value_t another_val;
504 NSString *o_title = @"";
507 switch( i_type & VLC_VAR_TYPE )
509 case VLC_VAR_VARIABLE:
511 /* This is causing crashes for the moment.
512 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
513 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
515 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
516 Value: val ofType: i_type];
517 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
520 NSMenu *o_menu = [o_lmi submenu];
522 [self setupVarMenu: o_menu forMenuItem: o_lmi target:p_object
523 var:psz_variable selector:pf_callback];
528 another_val.psz_string =
529 strdup(val_list.p_list->p_values[i].psz_string);
531 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
532 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
534 o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
535 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
536 Value: another_val ofType: i_type];
537 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
538 [o_lmi setTarget: self];
540 if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) )
541 [o_lmi setState: TRUE ];
545 case VLC_VAR_INTEGER:
547 o_title = text_list.p_list->p_values[i].psz_string ?
548 [NSApp localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
549 [NSString stringWithFormat: @"%d",
550 val_list.p_list->p_values[i].i_int];
552 o_lmi = [[o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""] retain ];
553 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
554 Value: val_list.p_list->p_values[i] ofType: i_type];
555 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[ o_data retain]]];
556 [o_lmi setTarget: self];
558 if( val_list.p_list->p_values[i].i_int == val.i_int )
559 [o_lmi setState: TRUE ];
567 /* clean up everything */
568 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
569 var_Change( p_object, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
572 - (IBAction)toggleVar:(id)sender
574 NSMenuItem *o_mi = (NSMenuItem *)sender;
575 VLCMenuExt *o_data = [[o_mi representedObject] pointerValue];
576 [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
577 toTarget: self withObject: o_data];
582 - (int)toggleVarThread: (id)_o_data
584 vlc_object_t *p_object;
585 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
586 VLCMenuExt *o_data = (VLCMenuExt *)_o_data;
588 vlc_thread_set_priority( [NSApp getIntf] , VLC_THREAD_PRIORITY_LOW );
590 p_object = (vlc_object_t *)vlc_object_get( [NSApp getIntf],
593 if( p_object != NULL )
595 var_Set( p_object, strdup([o_data name]), [o_data value] );
596 vlc_object_release( p_object );
606 @implementation VLCControls (NSMenuValidation)
608 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
610 BOOL bEnabled = TRUE;
612 intf_thread_t * p_intf = [NSApp getIntf];
613 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
616 if( p_playlist != NULL )
618 vlc_mutex_lock( &p_playlist->object_lock );
621 #define p_input p_playlist->p_input
623 if( [[o_mi title] isEqualToString: _NS("Faster")] ||
624 [[o_mi title] isEqualToString: _NS("Slower")] )
626 if( p_playlist != NULL && p_input != NULL )
628 vlc_mutex_lock( &p_input->stream.stream_lock );
629 bEnabled = p_input->stream.b_pace_control;
630 vlc_mutex_unlock( &p_input->stream.stream_lock );
637 else if( [[o_mi title] isEqualToString: _NS("Stop")] )
639 if( p_playlist == NULL || p_input == NULL )
644 else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
645 [[o_mi title] isEqualToString: _NS("Next")] )
647 if( p_playlist == NULL )
653 bEnabled = p_playlist->i_size > 1;
655 if( p_input != NULL )
657 vlc_mutex_lock( &p_input->stream.stream_lock );
658 bEnabled |= p_input->stream.i_area_nb > 1;
659 vlc_mutex_unlock( &p_input->stream.stream_lock );
663 else if( [[o_mi title] isEqualToString: _NS("Shuffle")] )
666 var_Get( p_playlist, "random", &val );
667 i_state = val.b_bool ? NSOnState : NSOffState;
668 [o_mi setState: i_state];
670 else if( [[o_mi title] isEqualToString: _NS("Repeat Item")] )
673 var_Get( p_playlist, "repeat", &val );
674 i_state = val.b_bool ? NSOnState : NSOffState;
675 [o_mi setState: i_state];
677 else if( [[o_mi title] isEqualToString: _NS("Repeat Playlist")] )
680 var_Get( p_playlist, "loop", &val );
681 i_state = val.b_bool ? NSOnState : NSOffState;
682 [o_mi setState: i_state];
684 else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
685 [[o_mi title] isEqualToString: _NS("Step Backward")] )
687 if( p_playlist != NULL && p_input != NULL )
689 vlc_mutex_lock( &p_input->stream.stream_lock );
690 bEnabled = p_input->stream.b_seekable;
691 vlc_mutex_unlock( &p_input->stream.stream_lock );
698 else if( [[o_mi title] isEqualToString: _NS("Mute")] )
700 [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
702 else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
703 [[o_mi title] isEqualToString: _NS("Half Size")] ||
704 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
705 [[o_mi title] isEqualToString: _NS("Double Size")] ||
706 [[o_mi title] isEqualToString: _NS("Fit To Screen")] ||
707 [[o_mi title] isEqualToString: _NS("Float On Top")] )
710 NSArray *o_windows = [NSApp orderedWindows];
711 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
714 if ( [[o_mi title] isEqualToString: _NS("Float On Top")] )
716 int i_state = config_GetInt( p_playlist, "video-on-top" ) ?
717 NSOnState : NSOffState;
718 [o_mi setState: i_state];
721 vout_thread_t *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
725 while ((o_window = [o_enumerator nextObject]))
727 if( [[o_window className] isEqualToString: @"VLCWindow"] )
733 vlc_object_release( (vlc_object_t *)p_vout );
737 if( p_playlist != NULL )
739 vlc_mutex_unlock( &p_playlist->object_lock );
740 vlc_object_release( p_playlist );
748 /*****************************************************************************
749 * VLCMenuExt implementation
750 *****************************************************************************
751 * Object connected to a playlistitem which remembers the data belonging to
752 * the variable of the autogenerated menu
753 *****************************************************************************/
754 @implementation VLCMenuExt
756 - (id)initWithVar: (const char *)_psz_name Object: (int)i_id
757 Value: (vlc_value_t)val ofType: (int)_i_type
763 psz_name = strdup( _psz_name );