1 /*****************************************************************************
2 * controls.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: controls.m,v 1.44 2003/07/09 01:30:41 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 */
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 NSLog(@"next-chapter: %d, all: %d", p_area->i_part, p_area->i_part_nb);
207 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
208 vlc_mutex_unlock( &p_playlist->object_lock );
209 var_Get( p_playlist->p_input, "next-chapter", &val );
210 var_Set( p_playlist->p_input, "next-chapter", val );
212 else if( p_area->i_id < p_playlist->p_input->stream.i_area_nb && p_playlist->p_input->stream.i_area_nb > 1 )
214 NSLog(@"next-title: %d, all: %d", p_area->i_id, p_playlist->p_input->stream.i_area_nb );
215 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
216 vlc_mutex_unlock( &p_playlist->object_lock );
217 var_Get( p_playlist->p_input, "next-title", &val );
218 var_Set( p_playlist->p_input, "next-title", val );
223 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
224 vlc_mutex_unlock( &p_playlist->object_lock );
225 playlist_Next( p_playlist );
229 vlc_object_release( p_playlist );
232 - (IBAction)loop:(id)sender
234 intf_thread_t * p_intf = [NSApp getIntf];
236 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
238 if( p_playlist == NULL )
243 config_PutInt( p_playlist, "loop",
244 !config_GetInt( p_playlist, "loop" ) );
246 vlc_object_release( p_playlist );
249 - (IBAction)forward:(id)sender
251 intf_thread_t * p_intf = [NSApp getIntf];
252 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
254 if( p_playlist == NULL || p_playlist->p_input == NULL )
256 if ( p_playlist != NULL ) vlc_object_release( p_playlist );
260 input_Seek( p_playlist->p_input, 5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
261 vlc_object_release( p_playlist );
264 - (IBAction)backward:(id)sender
266 intf_thread_t * p_intf = [NSApp getIntf];
267 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
269 if( p_playlist == NULL || p_playlist->p_input == NULL )
271 if ( p_playlist != NULL ) vlc_object_release( p_playlist );
275 input_Seek( p_playlist->p_input, -5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
276 vlc_object_release( p_playlist );
279 - (IBAction)volumeUp:(id)sender
281 intf_thread_t * p_intf = [NSApp getIntf];
283 if( p_intf->p_sys->b_mute )
288 aout_VolumeUp( p_intf, 1, NULL );
290 [self updateVolumeSlider];
293 - (IBAction)volumeDown:(id)sender
295 intf_thread_t * p_intf = [NSApp getIntf];
297 if( p_intf->p_sys->b_mute )
302 aout_VolumeDown( p_intf, 1, NULL );
304 [self updateVolumeSlider];
307 - (IBAction)mute:(id)sender
309 intf_thread_t * p_intf = [NSApp getIntf];
310 audio_volume_t i_volume;
312 aout_VolumeMute( p_intf, &i_volume );
313 p_intf->p_sys->b_mute = ( i_volume == 0 );
315 [self updateVolumeSlider];
318 - (IBAction)volumeSliderUpdated:(id)sender
320 intf_thread_t * p_intf = [NSApp getIntf];
321 audio_volume_t i_volume = (audio_volume_t)[sender intValue];
323 aout_VolumeSet( p_intf, i_volume * AOUT_VOLUME_STEP );
326 - (void)updateVolumeSlider
328 intf_thread_t * p_intf = [NSApp getIntf];
329 audio_volume_t i_volume;
331 aout_VolumeGet( p_intf, &i_volume );
333 [o_volumeslider setFloatValue: (float)(i_volume / AOUT_VOLUME_STEP)];
336 - (IBAction)windowAction:(id)sender
338 id o_window = [NSApp keyWindow];
339 NSString *o_title = [sender title];
340 NSArray *o_windows = [NSApp windows];
341 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
342 vout_thread_t *p_vout = vlc_object_find( [NSApp getIntf], VLC_OBJECT_VOUT,
347 while ((o_window = [o_enumerator nextObject]))
349 if( [[o_window className] isEqualToString: @"VLCWindow"] )
351 if( [o_title isEqualToString: _NS("Fullscreen") ] )
352 [o_window toggleFullscreen];
353 else if( [o_title isEqualToString: _NS("Half Size") ] )
354 [o_window scaleWindowWithFactor: 0.5];
355 else if( [o_title isEqualToString: _NS("Normal Size") ] )
356 [o_window scaleWindowWithFactor: 1.0];
357 else if( [o_title isEqualToString: _NS("Double Size") ] )
358 [o_window scaleWindowWithFactor: 2.0];
359 else if( [o_title isEqualToString: _NS("Float On Top") ] )
360 [o_window toggleFloatOnTop];
361 else if( [o_title isEqualToString: _NS("Fit To Screen") ] )
363 if( ![o_window isZoomed] )
364 [o_window performZoom:self];
368 vlc_object_release( (vlc_object_t *)p_vout );
372 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
373 target:(vlc_object_t *)p_object
374 var:(const char *)psz_variable
375 selector:(SEL)pf_callback
377 vlc_value_t val, text;
378 int i_type = var_Type( p_object, psz_variable );
380 switch( i_type & VLC_VAR_TYPE )
384 case VLC_VAR_VARIABLE:
386 case VLC_VAR_INTEGER:
389 /* Variable doesn't exist or isn't handled */
393 /* Make sure we want to display the variable */
394 if( i_type & VLC_VAR_HASCHOICE )
396 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
397 if( val.i_int == 0 ) return;
398 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
402 /* Get the descriptive name of the variable */
403 var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
404 [o_mi setTitle: [NSApp localizedString: text.psz_string ?
405 text.psz_string : strdup( psz_variable ) ]];
407 var_Get( p_object, psz_variable, &val );
408 if( i_type & VLC_VAR_HASCHOICE )
410 NSMenu *o_menu = [o_mi submenu];
412 [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
413 var:psz_variable selector:pf_callback];
415 if( text.psz_string ) free( text.psz_string );
420 switch( i_type & VLC_VAR_TYPE )
423 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
424 Value: val ofType: i_type];
425 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
429 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
430 Value: val ofType: i_type];
431 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
432 [o_mi setState: val.b_bool ? TRUE : FALSE ];
436 if( text.psz_string ) free( text.psz_string );
440 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
441 if( text.psz_string ) free( text.psz_string );
445 - (void)setupVarMenu:(NSMenu *)o_menu
446 forMenuItem: (NSMenuItem *)o_parent
447 target:(vlc_object_t *)p_object
448 var:(const char *)psz_variable
449 selector:(SEL)pf_callback
451 vlc_value_t val, val_list, text_list;
452 int i_type, i, i_nb_items;
454 /* remove previous items */
455 i_nb_items = [o_menu numberOfItems];
456 for( i = 0; i < i_nb_items; i++ )
458 [o_menu removeItemAtIndex: 0];
461 /* Check the type of the object variable */
462 i_type = var_Type( p_object, psz_variable );
464 /* Make sure we want to display the variable */
465 if( i_type & VLC_VAR_HASCHOICE )
467 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
468 if( val.i_int == 0 ) return;
469 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
477 switch( i_type & VLC_VAR_TYPE )
481 case VLC_VAR_VARIABLE:
483 case VLC_VAR_INTEGER:
486 /* Variable doesn't exist or isn't handled */
490 if( var_Get( p_object, psz_variable, &val ) < 0 )
495 if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
496 &val_list, &text_list ) < 0 )
498 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
502 /* make (un)sensitive */
503 [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
505 for( i = 0; i < val_list.p_list->i_count; i++ )
507 vlc_value_t another_val;
509 NSString *o_title = @"";
512 switch( i_type & VLC_VAR_TYPE )
514 case VLC_VAR_VARIABLE:
516 /* This is causing crashes for the moment.
517 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
518 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
520 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
521 Value: val ofType: i_type];
522 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
525 NSMenu *o_menu = [o_lmi submenu];
527 [self setupVarMenu: o_menu forMenuItem: o_lmi target:p_object
528 var:psz_variable selector:pf_callback];
533 another_val.psz_string =
534 strdup(val_list.p_list->p_values[i].psz_string);
536 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
537 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
539 o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
540 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
541 Value: another_val ofType: i_type];
542 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
543 [o_lmi setTarget: self];
545 if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) )
546 [o_lmi setState: TRUE ];
550 case VLC_VAR_INTEGER:
552 o_title = text_list.p_list->p_values[i].psz_string ?
553 [NSApp localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
554 [NSString stringWithFormat: @"%d",
555 val_list.p_list->p_values[i].i_int];
557 o_lmi = [[o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""] retain ];
558 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
559 Value: val_list.p_list->p_values[i] ofType: i_type];
560 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[ o_data retain]]];
561 [o_lmi setTarget: self];
563 if( val_list.p_list->p_values[i].i_int == val.i_int )
564 [o_lmi setState: TRUE ];
572 /* clean up everything */
573 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
574 var_Change( p_object, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
577 - (IBAction)toggleVar:(id)sender
579 NSMenuItem *o_mi = (NSMenuItem *)sender;
580 VLCMenuExt *o_data = [[o_mi representedObject] pointerValue];
581 [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
582 toTarget: self withObject: o_data];
587 - (int)toggleVarThread: (id)_o_data
589 vlc_object_t *p_object;
590 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
591 VLCMenuExt *o_data = (VLCMenuExt *)_o_data;
593 vlc_thread_set_priority( [NSApp getIntf] , VLC_THREAD_PRIORITY_LOW );
595 p_object = (vlc_object_t *)vlc_object_get( [NSApp getIntf],
598 if( p_object != NULL )
600 var_Set( p_object, strdup([o_data name]), [o_data value] );
601 vlc_object_release( p_object );
611 @implementation VLCControls (NSMenuValidation)
613 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
615 BOOL bEnabled = TRUE;
616 intf_thread_t * p_intf = [NSApp getIntf];
618 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
621 if( p_playlist != NULL )
623 vlc_mutex_lock( &p_playlist->object_lock );
626 #define p_input p_playlist->p_input
628 if( [[o_mi title] isEqualToString: _NS("Faster")] ||
629 [[o_mi title] isEqualToString: _NS("Slower")] )
631 if( p_playlist != NULL && p_input != NULL )
633 vlc_mutex_lock( &p_input->stream.stream_lock );
634 bEnabled = p_input->stream.b_pace_control;
635 vlc_mutex_unlock( &p_input->stream.stream_lock );
642 else if( [[o_mi title] isEqualToString: _NS("Stop")] )
644 if( p_playlist == NULL || p_input == NULL )
649 else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
650 [[o_mi title] isEqualToString: _NS("Next")] )
652 if( p_playlist == NULL )
658 bEnabled = p_playlist->i_size > 1;
660 if( p_input != NULL )
662 vlc_mutex_lock( &p_input->stream.stream_lock );
663 bEnabled |= p_input->stream.i_area_nb > 1;
664 vlc_mutex_unlock( &p_input->stream.stream_lock );
668 else if( [[o_mi title] isEqualToString: _NS("Loop")] )
670 int i_state = config_GetInt( p_playlist, "loop" ) ?
671 NSOnState : NSOffState;
673 [o_mi setState: i_state];
675 else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
676 [[o_mi title] isEqualToString: _NS("Step Backward")] )
678 if( p_playlist != NULL && p_input != NULL )
680 vlc_mutex_lock( &p_input->stream.stream_lock );
681 bEnabled = p_input->stream.b_seekable;
682 vlc_mutex_unlock( &p_input->stream.stream_lock );
689 else if( [[o_mi title] isEqualToString: _NS("Mute")] )
691 [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
693 else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
694 [[o_mi title] isEqualToString: _NS("Half Size")] ||
695 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
696 [[o_mi title] isEqualToString: _NS("Double Size")] ||
697 [[o_mi title] isEqualToString: _NS("Fit To Screen")] ||
698 [[o_mi title] isEqualToString: _NS("Float On Top")] )
701 NSArray *o_windows = [NSApp windows];
702 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
705 if ( [[o_mi title] isEqualToString: _NS("Float On Top")] )
707 int i_state = config_GetInt( p_playlist, "macosx-float" ) ?
708 NSOnState : NSOffState;
709 [o_mi setState: i_state];
712 vout_thread_t *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
716 while ((o_window = [o_enumerator nextObject]))
718 if( [[o_window className] isEqualToString: @"VLCWindow"] )
724 vlc_object_release( (vlc_object_t *)p_vout );
728 if( p_playlist != NULL )
730 vlc_mutex_unlock( &p_playlist->object_lock );
731 vlc_object_release( p_playlist );
739 /*****************************************************************************
740 * VLCMenuExt implementation
741 *****************************************************************************
742 * Object connected to a playlistitem which remembers the data belonging to
743 * the variable of the autogenerated menu
744 *****************************************************************************/
745 @implementation VLCMenuExt
747 - (id)initWithVar: (const char *)_psz_name Object: (int)i_id
748 Value: (vlc_value_t)val ofType: (int)_i_type
754 psz_name = strdup( _psz_name );