1 /*****************************************************************************
2 * controls.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: controls.m,v 1.54 2003/11/15 22:42:16 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
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 vlc_object_release( p_playlist );
183 - (IBAction)repeat:(id)sender
185 intf_thread_t * p_intf = [NSApp getIntf];
187 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
189 if( p_playlist == NULL )
194 var_Get( p_playlist, "repeat", &val );
195 val.b_bool = !val.b_bool;
196 var_Set( p_playlist, "repeat", val );
199 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Repeat On" ) );
203 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Repeat Off" ) );
206 vlc_object_release( p_playlist );
209 - (IBAction)loop:(id)sender
211 intf_thread_t * p_intf = [NSApp getIntf];
213 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
215 if( p_playlist == NULL )
220 var_Get( p_playlist, "loop", &val );
221 val.b_bool = !val.b_bool;
222 var_Set( p_playlist, "loop", val );
225 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Loop On" ) );
229 vout_OSDMessage( (vlc_object_t *)p_intf, _( "Loop Off" ) );
232 vlc_object_release( p_playlist );
235 - (IBAction)forward:(id)sender
237 intf_thread_t * p_intf = [NSApp getIntf];
238 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
240 if( p_input != NULL )
243 time.i_time = 10 * 1000000;
244 var_Set( p_input, "time-offset", time );
245 vlc_object_release( p_input );
249 - (IBAction)backward:(id)sender
251 intf_thread_t * p_intf = [NSApp getIntf];
252 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
254 if( p_input != NULL )
257 time.i_time = -10 * 1000000;
258 var_Set( p_input, "time-offset", time );
259 vlc_object_release( p_input );
263 - (IBAction)volumeUp:(id)sender
265 intf_thread_t * p_intf = [NSApp getIntf];
267 if( p_intf->p_sys->b_mute )
272 aout_VolumeUp( p_intf, 1, NULL );
274 [self updateVolumeSlider];
277 - (IBAction)volumeDown:(id)sender
279 intf_thread_t * p_intf = [NSApp getIntf];
281 if( p_intf->p_sys->b_mute )
286 aout_VolumeDown( p_intf, 1, NULL );
288 [self updateVolumeSlider];
291 - (IBAction)mute:(id)sender
293 intf_thread_t * p_intf = [NSApp getIntf];
294 audio_volume_t i_volume;
296 aout_VolumeMute( p_intf, &i_volume );
297 p_intf->p_sys->b_mute = ( i_volume == 0 );
299 [self updateVolumeSlider];
302 - (IBAction)volumeSliderUpdated:(id)sender
304 intf_thread_t * p_intf = [NSApp getIntf];
305 audio_volume_t i_volume = (audio_volume_t)[sender intValue];
307 aout_VolumeSet( p_intf, i_volume * AOUT_VOLUME_STEP );
310 - (void)updateVolumeSlider
312 intf_thread_t * p_intf = [NSApp getIntf];
313 audio_volume_t i_volume;
316 aout_VolumeGet( p_intf, &i_volume );
318 [o_volumeslider setFloatValue: (float)(i_volume / AOUT_VOLUME_STEP)];
320 sprintf( string, "Vol %d%%", i_volume*100/AOUT_VOLUME_MAX );
321 vout_OSDMessage( (vlc_object_t *)p_intf, string );
324 - (IBAction)windowAction:(id)sender
326 id o_window = [NSApp keyWindow];
327 NSString *o_title = [sender title];
328 NSArray *o_windows = [NSApp windows];
329 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
330 vout_thread_t *p_vout = vlc_object_find( [NSApp getIntf], VLC_OBJECT_VOUT,
335 while ((o_window = [o_enumerator nextObject]))
337 if( [[o_window className] isEqualToString: @"VLCWindow"] )
339 if( [o_title isEqualToString: _NS("Half Size") ] )
340 [o_window scaleWindowWithFactor: 0.5];
341 else if( [o_title isEqualToString: _NS("Normal Size") ] )
342 [o_window scaleWindowWithFactor: 1.0];
343 else if( [o_title isEqualToString: _NS("Double Size") ] )
344 [o_window scaleWindowWithFactor: 2.0];
345 else if( [o_title isEqualToString: _NS("Float On Top") ] )
346 [o_window toggleFloatOnTop];
347 else if( [o_title isEqualToString: _NS("Fit To Screen") ] )
349 if( ![o_window isZoomed] )
350 [o_window performZoom:self];
353 [o_window toggleFullscreen];
357 vlc_object_release( (vlc_object_t *)p_vout );
361 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
362 target:(vlc_object_t *)p_object
363 var:(const char *)psz_variable
364 selector:(SEL)pf_callback
366 vlc_value_t val, text;
367 int i_type = var_Type( p_object, psz_variable );
369 switch( i_type & VLC_VAR_TYPE )
373 case VLC_VAR_VARIABLE:
375 case VLC_VAR_INTEGER:
378 /* Variable doesn't exist or isn't handled */
382 /* Make sure we want to display the variable */
383 if( i_type & VLC_VAR_HASCHOICE )
385 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
386 if( val.i_int == 0 ) return;
387 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
391 /* Get the descriptive name of the variable */
392 var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
393 [o_mi setTitle: [NSApp localizedString: text.psz_string ?
394 text.psz_string : strdup( psz_variable ) ]];
396 var_Get( p_object, psz_variable, &val );
397 if( i_type & VLC_VAR_HASCHOICE )
399 NSMenu *o_menu = [o_mi submenu];
401 [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
402 var:psz_variable selector:pf_callback];
404 if( text.psz_string ) free( text.psz_string );
409 switch( i_type & VLC_VAR_TYPE )
412 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
413 Value: val ofType: i_type];
414 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
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]]];
421 [o_mi setState: val.b_bool ? TRUE : FALSE ];
425 if( text.psz_string ) free( text.psz_string );
429 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
430 if( text.psz_string ) free( text.psz_string );
434 - (void)setupVarMenu:(NSMenu *)o_menu
435 forMenuItem: (NSMenuItem *)o_parent
436 target:(vlc_object_t *)p_object
437 var:(const char *)psz_variable
438 selector:(SEL)pf_callback
440 vlc_value_t val, val_list, text_list;
441 int i_type, i, i_nb_items;
443 /* remove previous items */
444 i_nb_items = [o_menu numberOfItems];
445 for( i = 0; i < i_nb_items; i++ )
447 [o_menu removeItemAtIndex: 0];
450 /* Check the type of the object variable */
451 i_type = var_Type( p_object, psz_variable );
453 /* Make sure we want to display the variable */
454 if( i_type & VLC_VAR_HASCHOICE )
456 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
457 if( val.i_int == 0 ) return;
458 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
466 switch( i_type & VLC_VAR_TYPE )
470 case VLC_VAR_VARIABLE:
472 case VLC_VAR_INTEGER:
475 /* Variable doesn't exist or isn't handled */
479 if( var_Get( p_object, psz_variable, &val ) < 0 )
484 if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
485 &val_list, &text_list ) < 0 )
487 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
491 /* make (un)sensitive */
492 [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
494 for( i = 0; i < val_list.p_list->i_count; i++ )
496 vlc_value_t another_val;
498 NSString *o_title = @"";
501 switch( i_type & VLC_VAR_TYPE )
503 case VLC_VAR_VARIABLE:
505 /* This is causing crashes for the moment.
506 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
507 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
509 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
510 Value: val ofType: i_type];
511 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
514 NSMenu *o_menu = [o_lmi submenu];
516 [self setupVarMenu: o_menu forMenuItem: o_lmi target:p_object
517 var:psz_variable selector:pf_callback];
522 another_val.psz_string =
523 strdup(val_list.p_list->p_values[i].psz_string);
525 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
526 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
528 o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
529 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
530 Value: another_val ofType: i_type];
531 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
532 [o_lmi setTarget: self];
534 if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) )
535 [o_lmi setState: TRUE ];
539 case VLC_VAR_INTEGER:
541 o_title = text_list.p_list->p_values[i].psz_string ?
542 [NSApp localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
543 [NSString stringWithFormat: @"%d",
544 val_list.p_list->p_values[i].i_int];
546 o_lmi = [[o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""] retain ];
547 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
548 Value: val_list.p_list->p_values[i] ofType: i_type];
549 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[ o_data retain]]];
550 [o_lmi setTarget: self];
552 if( val_list.p_list->p_values[i].i_int == val.i_int )
553 [o_lmi setState: TRUE ];
561 /* clean up everything */
562 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
563 var_Change( p_object, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
566 - (IBAction)toggleVar:(id)sender
568 NSMenuItem *o_mi = (NSMenuItem *)sender;
569 VLCMenuExt *o_data = [[o_mi representedObject] pointerValue];
570 [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
571 toTarget: self withObject: o_data];
576 - (int)toggleVarThread: (id)_o_data
578 vlc_object_t *p_object;
579 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
580 VLCMenuExt *o_data = (VLCMenuExt *)_o_data;
582 vlc_thread_set_priority( [NSApp getIntf] , VLC_THREAD_PRIORITY_LOW );
584 p_object = (vlc_object_t *)vlc_object_get( [NSApp getIntf],
587 if( p_object != NULL )
589 var_Set( p_object, strdup([o_data name]), [o_data value] );
590 vlc_object_release( p_object );
600 @implementation VLCControls (NSMenuValidation)
602 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
604 BOOL bEnabled = TRUE;
606 intf_thread_t * p_intf = [NSApp getIntf];
607 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
610 if( p_playlist != NULL )
612 vlc_mutex_lock( &p_playlist->object_lock );
615 #define p_input p_playlist->p_input
617 if( [[o_mi title] isEqualToString: _NS("Faster")] ||
618 [[o_mi title] isEqualToString: _NS("Slower")] )
620 if( p_playlist != NULL && p_input != NULL )
622 vlc_mutex_lock( &p_input->stream.stream_lock );
623 bEnabled = p_input->stream.b_pace_control;
624 vlc_mutex_unlock( &p_input->stream.stream_lock );
631 else if( [[o_mi title] isEqualToString: _NS("Stop")] )
633 if( p_playlist == NULL || p_input == NULL )
638 else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
639 [[o_mi title] isEqualToString: _NS("Next")] )
641 if( p_playlist == NULL )
647 bEnabled = p_playlist->i_size > 1;
649 if( p_input != NULL )
651 vlc_mutex_lock( &p_input->stream.stream_lock );
652 bEnabled |= p_input->stream.i_area_nb > 1;
653 vlc_mutex_unlock( &p_input->stream.stream_lock );
657 else if( [[o_mi title] isEqualToString: _NS("Shuffle")] )
660 var_Get( p_playlist, "random", &val );
661 i_state = val.b_bool ? NSOnState : NSOffState;
662 [o_mi setState: i_state];
664 else if( [[o_mi title] isEqualToString: _NS("Repeat Item")] )
667 var_Get( p_playlist, "repeat", &val );
668 i_state = val.b_bool ? NSOnState : NSOffState;
669 [o_mi setState: i_state];
671 else if( [[o_mi title] isEqualToString: _NS("Repeat Playlist")] )
674 var_Get( p_playlist, "loop", &val );
675 i_state = val.b_bool ? NSOnState : NSOffState;
676 [o_mi setState: i_state];
678 else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
679 [[o_mi title] isEqualToString: _NS("Step Backward")] )
681 if( p_playlist != NULL && p_input != NULL )
683 vlc_mutex_lock( &p_input->stream.stream_lock );
684 bEnabled = p_input->stream.b_seekable;
685 vlc_mutex_unlock( &p_input->stream.stream_lock );
692 else if( [[o_mi title] isEqualToString: _NS("Mute")] )
694 [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
696 else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
697 [[o_mi title] isEqualToString: _NS("Half Size")] ||
698 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
699 [[o_mi title] isEqualToString: _NS("Double Size")] ||
700 [[o_mi title] isEqualToString: _NS("Fit To Screen")] ||
701 [[o_mi title] isEqualToString: _NS("Float On Top")] )
704 NSArray *o_windows = [NSApp windows];
705 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
708 if ( [[o_mi title] isEqualToString: _NS("Float On Top")] )
710 int i_state = config_GetInt( p_playlist, "macosx-float" ) ?
711 NSOnState : NSOffState;
712 [o_mi setState: i_state];
715 vout_thread_t *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
719 while ((o_window = [o_enumerator nextObject]))
721 if( [[o_window className] isEqualToString: @"VLCWindow"] )
727 vlc_object_release( (vlc_object_t *)p_vout );
731 if( p_playlist != NULL )
733 vlc_mutex_unlock( &p_playlist->object_lock );
734 vlc_object_release( p_playlist );
742 /*****************************************************************************
743 * VLCMenuExt implementation
744 *****************************************************************************
745 * Object connected to a playlistitem which remembers the data belonging to
746 * the variable of the autogenerated menu
747 *****************************************************************************/
748 @implementation VLCMenuExt
750 - (id)initWithVar: (const char *)_psz_name Object: (int)i_id
751 Value: (vlc_value_t)val ofType: (int)_i_type
757 psz_name = strdup( _psz_name );