1 /*****************************************************************************
2 * controls.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: controls.m,v 1.46 2003/07/27 14:10:02 massiot 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 */
38 /*****************************************************************************
39 * VLCControls implementation
40 *****************************************************************************/
41 @implementation VLCControls
43 - (IBAction)play:(id)sender
45 intf_thread_t * p_intf = [NSApp getIntf];
47 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
49 if( p_playlist == NULL )
54 if( playlist_IsPlaying( p_playlist ) )
56 playlist_Pause( p_playlist );
57 vlc_object_release( p_playlist );
61 if( !playlist_IsEmpty( p_playlist ) )
63 playlist_Play( p_playlist );
64 vlc_object_release( p_playlist );
68 vlc_object_release( p_playlist );
69 [o_open openFileGeneric: nil];
74 - (IBAction)stop:(id)sender
76 intf_thread_t * p_intf = [NSApp getIntf];
78 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
80 if( p_playlist == NULL )
85 playlist_Stop( p_playlist );
86 vlc_object_release( p_playlist );
89 - (IBAction)faster:(id)sender
91 intf_thread_t * p_intf = [NSApp getIntf];
93 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
95 if( p_playlist == NULL )
100 vlc_mutex_lock( &p_playlist->object_lock );
101 if( p_playlist->p_input != NULL )
103 input_SetStatus( p_playlist->p_input, INPUT_STATUS_FASTER );
105 vlc_mutex_unlock( &p_playlist->object_lock );
107 vlc_object_release( p_playlist );
110 - (IBAction)slower:(id)sender
112 intf_thread_t * p_intf = [NSApp getIntf];
114 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
116 if( p_playlist == NULL )
121 vlc_mutex_lock( &p_playlist->object_lock );
122 if( p_playlist->p_input != NULL )
124 input_SetStatus( p_playlist->p_input, INPUT_STATUS_SLOWER );
126 vlc_mutex_unlock( &p_playlist->object_lock );
128 vlc_object_release( p_playlist );
131 - (IBAction)prev:(id)sender
134 intf_thread_t * p_intf = [NSApp getIntf];
136 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
138 if( p_playlist == NULL )
143 vlc_mutex_lock( &p_playlist->object_lock );
145 if( p_playlist->p_input == NULL )
147 vlc_mutex_unlock( &p_playlist->object_lock );
148 vlc_object_release( p_playlist );
152 vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
154 #define p_area p_playlist->p_input->stream.p_selected_area
155 if( p_area->i_part > 0 && p_area->i_part_nb > 1)
157 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
158 vlc_mutex_unlock( &p_playlist->object_lock );
159 var_Get( p_playlist->p_input, "prev-chapter", &val );
160 var_Set( p_playlist->p_input, "prev-chapter", val );
162 else if( p_area->i_id > 1 )
164 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
165 vlc_mutex_unlock( &p_playlist->object_lock );
166 var_Get( p_playlist->p_input, "prev-title", &val );
167 var_Set( p_playlist->p_input, "prev-title", val );
171 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
172 vlc_mutex_unlock( &p_playlist->object_lock );
173 playlist_Prev( p_playlist );
177 vlc_object_release( p_playlist );
180 - (IBAction)next:(id)sender
183 intf_thread_t * p_intf = [NSApp getIntf];
185 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
187 if( p_playlist == NULL )
192 vlc_mutex_lock( &p_playlist->object_lock );
194 if( p_playlist->p_input == NULL )
196 vlc_mutex_unlock( &p_playlist->object_lock );
197 vlc_object_release( p_playlist );
201 vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
203 #define p_area p_playlist->p_input->stream.p_selected_area
204 if( p_area->i_part < p_area->i_part_nb && p_area->i_part_nb > 1 )
206 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
207 vlc_mutex_unlock( &p_playlist->object_lock );
208 var_Get( p_playlist->p_input, "next-chapter", &val );
209 var_Set( p_playlist->p_input, "next-chapter", val );
211 else if( p_area->i_id < p_playlist->p_input->stream.i_area_nb && p_playlist->p_input->stream.i_area_nb > 1 )
213 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
214 vlc_mutex_unlock( &p_playlist->object_lock );
215 var_Get( p_playlist->p_input, "next-title", &val );
216 var_Set( p_playlist->p_input, "next-title", val );
220 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
221 vlc_mutex_unlock( &p_playlist->object_lock );
222 playlist_Next( p_playlist );
226 vlc_object_release( p_playlist );
229 - (IBAction)loop:(id)sender
231 intf_thread_t * p_intf = [NSApp getIntf];
233 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
235 if( p_playlist == NULL )
240 config_PutInt( p_playlist, "loop",
241 !config_GetInt( p_playlist, "loop" ) );
243 vlc_object_release( p_playlist );
246 - (IBAction)forward:(id)sender
248 intf_thread_t * p_intf = [NSApp getIntf];
249 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
251 if( p_playlist == NULL || p_playlist->p_input == NULL )
253 if ( p_playlist != NULL ) vlc_object_release( p_playlist );
257 playlist_Play( p_playlist );
258 input_Seek( p_playlist->p_input, 5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
259 vlc_object_release( p_playlist );
262 - (IBAction)backward:(id)sender
264 intf_thread_t * p_intf = [NSApp getIntf];
265 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
267 if( p_playlist == NULL || p_playlist->p_input == NULL )
269 if ( p_playlist != NULL ) vlc_object_release( p_playlist );
273 playlist_Play( p_playlist );
274 input_Seek( p_playlist->p_input, -5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
275 vlc_object_release( p_playlist );
278 - (IBAction)volumeUp:(id)sender
280 intf_thread_t * p_intf = [NSApp getIntf];
282 if( p_intf->p_sys->b_mute )
287 aout_VolumeUp( p_intf, 1, NULL );
289 [self updateVolumeSlider];
292 - (IBAction)volumeDown:(id)sender
294 intf_thread_t * p_intf = [NSApp getIntf];
296 if( p_intf->p_sys->b_mute )
301 aout_VolumeDown( p_intf, 1, NULL );
303 [self updateVolumeSlider];
306 - (IBAction)mute:(id)sender
308 intf_thread_t * p_intf = [NSApp getIntf];
309 audio_volume_t i_volume;
311 aout_VolumeMute( p_intf, &i_volume );
312 p_intf->p_sys->b_mute = ( i_volume == 0 );
314 [self updateVolumeSlider];
317 - (IBAction)volumeSliderUpdated:(id)sender
319 intf_thread_t * p_intf = [NSApp getIntf];
320 audio_volume_t i_volume = (audio_volume_t)[sender intValue];
322 aout_VolumeSet( p_intf, i_volume * AOUT_VOLUME_STEP );
325 - (void)updateVolumeSlider
327 intf_thread_t * p_intf = [NSApp getIntf];
328 audio_volume_t i_volume;
330 aout_VolumeGet( p_intf, &i_volume );
332 [o_volumeslider setFloatValue: (float)(i_volume / AOUT_VOLUME_STEP)];
335 - (IBAction)windowAction:(id)sender
337 id o_window = [NSApp keyWindow];
338 NSString *o_title = [sender title];
339 NSArray *o_windows = [NSApp windows];
340 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
341 vout_thread_t *p_vout = vlc_object_find( [NSApp getIntf], VLC_OBJECT_VOUT,
346 while ((o_window = [o_enumerator nextObject]))
348 if( [[o_window className] isEqualToString: @"VLCWindow"] )
350 if( [o_title isEqualToString: _NS("Fullscreen") ] )
351 [o_window toggleFullscreen];
352 else if( [o_title isEqualToString: _NS("Half Size") ] )
353 [o_window scaleWindowWithFactor: 0.5];
354 else if( [o_title isEqualToString: _NS("Normal Size") ] )
355 [o_window scaleWindowWithFactor: 1.0];
356 else if( [o_title isEqualToString: _NS("Double Size") ] )
357 [o_window scaleWindowWithFactor: 2.0];
358 else if( [o_title isEqualToString: _NS("Float On Top") ] )
359 [o_window toggleFloatOnTop];
360 else if( [o_title isEqualToString: _NS("Fit To Screen") ] )
362 if( ![o_window isZoomed] )
363 [o_window performZoom:self];
367 vlc_object_release( (vlc_object_t *)p_vout );
371 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
372 target:(vlc_object_t *)p_object
373 var:(const char *)psz_variable
374 selector:(SEL)pf_callback
376 vlc_value_t val, text;
377 int i_type = var_Type( p_object, psz_variable );
379 switch( i_type & VLC_VAR_TYPE )
383 case VLC_VAR_VARIABLE:
385 case VLC_VAR_INTEGER:
388 /* Variable doesn't exist or isn't handled */
392 /* Make sure we want to display the variable */
393 if( i_type & VLC_VAR_HASCHOICE )
395 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
396 if( val.i_int == 0 ) return;
397 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
401 /* Get the descriptive name of the variable */
402 var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
403 [o_mi setTitle: [NSApp localizedString: text.psz_string ?
404 text.psz_string : strdup( psz_variable ) ]];
406 var_Get( p_object, psz_variable, &val );
407 if( i_type & VLC_VAR_HASCHOICE )
409 NSMenu *o_menu = [o_mi submenu];
411 [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
412 var:psz_variable selector:pf_callback];
414 if( text.psz_string ) free( text.psz_string );
419 switch( i_type & VLC_VAR_TYPE )
422 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
423 Value: val ofType: i_type];
424 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
428 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
429 Value: val ofType: i_type];
430 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
431 [o_mi setState: val.b_bool ? TRUE : FALSE ];
435 if( text.psz_string ) free( text.psz_string );
439 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
440 if( text.psz_string ) free( text.psz_string );
444 - (void)setupVarMenu:(NSMenu *)o_menu
445 forMenuItem: (NSMenuItem *)o_parent
446 target:(vlc_object_t *)p_object
447 var:(const char *)psz_variable
448 selector:(SEL)pf_callback
450 vlc_value_t val, val_list, text_list;
451 int i_type, i, i_nb_items;
453 /* remove previous items */
454 i_nb_items = [o_menu numberOfItems];
455 for( i = 0; i < i_nb_items; i++ )
457 [o_menu removeItemAtIndex: 0];
460 /* Check the type of the object variable */
461 i_type = var_Type( p_object, psz_variable );
463 /* Make sure we want to display the variable */
464 if( i_type & VLC_VAR_HASCHOICE )
466 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
467 if( val.i_int == 0 ) return;
468 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
476 switch( i_type & VLC_VAR_TYPE )
480 case VLC_VAR_VARIABLE:
482 case VLC_VAR_INTEGER:
485 /* Variable doesn't exist or isn't handled */
489 if( var_Get( p_object, psz_variable, &val ) < 0 )
494 if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
495 &val_list, &text_list ) < 0 )
497 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
501 /* make (un)sensitive */
502 [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
504 for( i = 0; i < val_list.p_list->i_count; i++ )
506 vlc_value_t another_val;
508 NSString *o_title = @"";
511 switch( i_type & VLC_VAR_TYPE )
513 case VLC_VAR_VARIABLE:
515 /* This is causing crashes for the moment.
516 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
517 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
519 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
520 Value: val ofType: i_type];
521 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
524 NSMenu *o_menu = [o_lmi submenu];
526 [self setupVarMenu: o_menu forMenuItem: o_lmi target:p_object
527 var:psz_variable selector:pf_callback];
532 another_val.psz_string =
533 strdup(val_list.p_list->p_values[i].psz_string);
535 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
536 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
538 o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
539 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
540 Value: another_val ofType: i_type];
541 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
542 [o_lmi setTarget: self];
544 if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) )
545 [o_lmi setState: TRUE ];
549 case VLC_VAR_INTEGER:
551 o_title = text_list.p_list->p_values[i].psz_string ?
552 [NSApp localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
553 [NSString stringWithFormat: @"%d",
554 val_list.p_list->p_values[i].i_int];
556 o_lmi = [[o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""] retain ];
557 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
558 Value: val_list.p_list->p_values[i] ofType: i_type];
559 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[ o_data retain]]];
560 [o_lmi setTarget: self];
562 if( val_list.p_list->p_values[i].i_int == val.i_int )
563 [o_lmi setState: TRUE ];
571 /* clean up everything */
572 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
573 var_Change( p_object, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
576 - (IBAction)toggleVar:(id)sender
578 NSMenuItem *o_mi = (NSMenuItem *)sender;
579 VLCMenuExt *o_data = [[o_mi representedObject] pointerValue];
580 [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
581 toTarget: self withObject: o_data];
586 - (int)toggleVarThread: (id)_o_data
588 vlc_object_t *p_object;
589 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
590 VLCMenuExt *o_data = (VLCMenuExt *)_o_data;
592 vlc_thread_set_priority( [NSApp getIntf] , VLC_THREAD_PRIORITY_LOW );
594 p_object = (vlc_object_t *)vlc_object_get( [NSApp getIntf],
597 if( p_object != NULL )
599 var_Set( p_object, strdup([o_data name]), [o_data value] );
600 vlc_object_release( p_object );
610 @implementation VLCControls (NSMenuValidation)
612 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
614 BOOL bEnabled = TRUE;
615 intf_thread_t * p_intf = [NSApp getIntf];
617 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
620 if( p_playlist != NULL )
622 vlc_mutex_lock( &p_playlist->object_lock );
625 #define p_input p_playlist->p_input
627 if( [[o_mi title] isEqualToString: _NS("Faster")] ||
628 [[o_mi title] isEqualToString: _NS("Slower")] )
630 if( p_playlist != NULL && p_input != NULL )
632 vlc_mutex_lock( &p_input->stream.stream_lock );
633 bEnabled = p_input->stream.b_pace_control;
634 vlc_mutex_unlock( &p_input->stream.stream_lock );
641 else if( [[o_mi title] isEqualToString: _NS("Stop")] )
643 if( p_playlist == NULL || p_input == NULL )
648 else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
649 [[o_mi title] isEqualToString: _NS("Next")] )
651 if( p_playlist == NULL )
657 bEnabled = p_playlist->i_size > 1;
659 if( p_input != NULL )
661 vlc_mutex_lock( &p_input->stream.stream_lock );
662 bEnabled |= p_input->stream.i_area_nb > 1;
663 vlc_mutex_unlock( &p_input->stream.stream_lock );
667 else if( [[o_mi title] isEqualToString: _NS("Loop")] )
669 int i_state = config_GetInt( p_playlist, "loop" ) ?
670 NSOnState : NSOffState;
672 [o_mi setState: i_state];
674 else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
675 [[o_mi title] isEqualToString: _NS("Step Backward")] )
677 if( p_playlist != NULL && p_input != NULL )
679 vlc_mutex_lock( &p_input->stream.stream_lock );
680 bEnabled = p_input->stream.b_seekable;
681 vlc_mutex_unlock( &p_input->stream.stream_lock );
688 else if( [[o_mi title] isEqualToString: _NS("Mute")] )
690 [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
692 else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
693 [[o_mi title] isEqualToString: _NS("Half Size")] ||
694 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
695 [[o_mi title] isEqualToString: _NS("Double Size")] ||
696 [[o_mi title] isEqualToString: _NS("Fit To Screen")] ||
697 [[o_mi title] isEqualToString: _NS("Float On Top")] )
700 NSArray *o_windows = [NSApp windows];
701 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
704 if ( [[o_mi title] isEqualToString: _NS("Float On Top")] )
706 int i_state = config_GetInt( p_playlist, "macosx-float" ) ?
707 NSOnState : NSOffState;
708 [o_mi setState: i_state];
711 vout_thread_t *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
715 while ((o_window = [o_enumerator nextObject]))
717 if( [[o_window className] isEqualToString: @"VLCWindow"] )
723 vlc_object_release( (vlc_object_t *)p_vout );
727 if( p_playlist != NULL )
729 vlc_mutex_unlock( &p_playlist->object_lock );
730 vlc_object_release( p_playlist );
738 /*****************************************************************************
739 * VLCMenuExt implementation
740 *****************************************************************************
741 * Object connected to a playlistitem which remembers the data belonging to
742 * the variable of the autogenerated menu
743 *****************************************************************************/
744 @implementation VLCMenuExt
746 - (id)initWithVar: (const char *)_psz_name Object: (int)i_id
747 Value: (vlc_value_t)val ofType: (int)_i_type
753 psz_name = strdup( _psz_name );