1 /*****************************************************************************
2 * controls.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: controls.m,v 1.43 2003/06/30 01:52:57 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 p_intf->p_sys->b_input_update = VLC_TRUE;
164 else if( p_area->i_id > 1 )
166 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
167 vlc_mutex_unlock( &p_playlist->object_lock );
168 var_Get( p_playlist->p_input, "prev-title", &val );
169 var_Set( p_playlist->p_input, "prev-title", val );
171 p_intf->p_sys->b_input_update = VLC_TRUE;
175 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
176 vlc_mutex_unlock( &p_playlist->object_lock );
177 playlist_Prev( p_playlist );
181 vlc_object_release( p_playlist );
184 - (IBAction)next:(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 vlc_mutex_lock( &p_playlist->object_lock );
198 if( p_playlist->p_input == NULL )
200 vlc_mutex_unlock( &p_playlist->object_lock );
201 vlc_object_release( p_playlist );
205 vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
207 #define p_area p_playlist->p_input->stream.p_selected_area
208 if( p_area->i_part < p_area->i_part_nb && p_area->i_part_nb > 1 )
210 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
211 vlc_mutex_unlock( &p_playlist->object_lock );
212 var_Get( p_playlist->p_input, "next-chapter", &val );
213 var_Set( p_playlist->p_input, "next-chapter", val );
215 p_intf->p_sys->b_input_update = VLC_TRUE;
217 else if( p_area->i_id < p_playlist->p_input->stream.i_area_nb )
219 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
220 vlc_mutex_unlock( &p_playlist->object_lock );
221 var_Get( p_playlist->p_input, "next-title", &val );
222 var_Set( p_playlist->p_input, "next-title", val );
224 p_intf->p_sys->b_input_update = VLC_TRUE;
228 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
229 vlc_mutex_unlock( &p_playlist->object_lock );
230 playlist_Next( p_playlist );
234 vlc_object_release( p_playlist );
237 - (IBAction)loop:(id)sender
239 intf_thread_t * p_intf = [NSApp getIntf];
241 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
243 if( p_playlist == NULL )
248 config_PutInt( p_playlist, "loop",
249 !config_GetInt( p_playlist, "loop" ) );
251 vlc_object_release( p_playlist );
254 - (IBAction)forward:(id)sender
256 intf_thread_t * p_intf = [NSApp getIntf];
257 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
259 if( p_playlist == NULL || p_playlist->p_input == NULL )
261 if ( p_playlist != NULL ) vlc_object_release( p_playlist );
265 input_Seek( p_playlist->p_input, 5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
266 vlc_object_release( p_playlist );
269 - (IBAction)backward:(id)sender
271 intf_thread_t * p_intf = [NSApp getIntf];
272 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
274 if( p_playlist == NULL || p_playlist->p_input == NULL )
276 if ( p_playlist != NULL ) vlc_object_release( p_playlist );
280 input_Seek( p_playlist->p_input, -5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
281 vlc_object_release( p_playlist );
284 - (IBAction)volumeUp:(id)sender
286 intf_thread_t * p_intf = [NSApp getIntf];
288 if( p_intf->p_sys->b_mute )
293 aout_VolumeUp( p_intf, 1, NULL );
295 [self updateVolumeSlider];
298 - (IBAction)volumeDown:(id)sender
300 intf_thread_t * p_intf = [NSApp getIntf];
302 if( p_intf->p_sys->b_mute )
307 aout_VolumeDown( p_intf, 1, NULL );
309 [self updateVolumeSlider];
312 - (IBAction)mute:(id)sender
314 intf_thread_t * p_intf = [NSApp getIntf];
315 audio_volume_t i_volume;
317 aout_VolumeMute( p_intf, &i_volume );
318 p_intf->p_sys->b_mute = ( i_volume == 0 );
320 [self updateVolumeSlider];
323 - (IBAction)volumeSliderUpdated:(id)sender
325 intf_thread_t * p_intf = [NSApp getIntf];
326 audio_volume_t i_volume = (audio_volume_t)[sender intValue];
328 aout_VolumeSet( p_intf, i_volume * AOUT_VOLUME_STEP );
331 - (void)updateVolumeSlider
333 intf_thread_t * p_intf = [NSApp getIntf];
334 audio_volume_t i_volume;
336 aout_VolumeGet( p_intf, &i_volume );
338 [o_volumeslider setFloatValue: (float)(i_volume / AOUT_VOLUME_STEP)];
341 - (IBAction)windowAction:(id)sender
343 id o_window = [NSApp keyWindow];
344 NSString *o_title = [sender title];
345 NSArray *o_windows = [NSApp windows];
346 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
347 vout_thread_t *p_vout = vlc_object_find( [NSApp getIntf], VLC_OBJECT_VOUT,
352 while ((o_window = [o_enumerator nextObject]))
354 if( [[o_window className] isEqualToString: @"VLCWindow"] )
356 if( [o_title isEqualToString: _NS("Fullscreen") ] )
357 [o_window toggleFullscreen];
358 else if( [o_title isEqualToString: _NS("Half Size") ] )
359 [o_window scaleWindowWithFactor: 0.5];
360 else if( [o_title isEqualToString: _NS("Normal Size") ] )
361 [o_window scaleWindowWithFactor: 1.0];
362 else if( [o_title isEqualToString: _NS("Double Size") ] )
363 [o_window scaleWindowWithFactor: 2.0];
364 else if( [o_title isEqualToString: _NS("Float On Top") ] )
365 [o_window toggleFloatOnTop];
366 else if( [o_title isEqualToString: _NS("Fit To Screen") ] )
368 if( ![o_window isZoomed] )
369 [o_window performZoom:self];
373 vlc_object_release( (vlc_object_t *)p_vout );
377 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
378 target:(vlc_object_t *)p_object
379 var:(const char *)psz_variable
380 selector:(SEL)pf_callback
382 vlc_value_t val, text;
383 int i_type = var_Type( p_object, psz_variable );
385 switch( i_type & VLC_VAR_TYPE )
389 case VLC_VAR_VARIABLE:
391 case VLC_VAR_INTEGER:
394 /* Variable doesn't exist or isn't handled */
398 /* Make sure we want to display the variable */
399 if( i_type & VLC_VAR_HASCHOICE )
401 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
402 if( val.i_int == 0 ) return;
403 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
407 /* Get the descriptive name of the variable */
408 var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
409 [o_mi setTitle: [NSApp localizedString: text.psz_string ?
410 text.psz_string : strdup( psz_variable ) ]];
412 var_Get( p_object, psz_variable, &val );
413 if( i_type & VLC_VAR_HASCHOICE )
415 NSMenu *o_menu = [o_mi submenu];
417 [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
418 var:psz_variable selector:pf_callback];
420 if( text.psz_string ) free( text.psz_string );
425 switch( i_type & VLC_VAR_TYPE )
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]]];
434 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
435 Value: val ofType: i_type];
436 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
437 [o_mi setState: val.b_bool ? TRUE : FALSE ];
441 if( text.psz_string ) free( text.psz_string );
445 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
446 if( text.psz_string ) free( text.psz_string );
450 - (void)setupVarMenu:(NSMenu *)o_menu
451 forMenuItem: (NSMenuItem *)o_parent
452 target:(vlc_object_t *)p_object
453 var:(const char *)psz_variable
454 selector:(SEL)pf_callback
456 vlc_value_t val, val_list, text_list;
457 int i_type, i, i_nb_items;
459 /* remove previous items */
460 i_nb_items = [o_menu numberOfItems];
461 for( i = 0; i < i_nb_items; i++ )
463 [o_menu removeItemAtIndex: 0];
466 /* Check the type of the object variable */
467 i_type = var_Type( p_object, psz_variable );
469 /* Make sure we want to display the variable */
470 if( i_type & VLC_VAR_HASCHOICE )
472 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
473 if( val.i_int == 0 ) return;
474 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
482 switch( i_type & VLC_VAR_TYPE )
486 case VLC_VAR_VARIABLE:
488 case VLC_VAR_INTEGER:
491 /* Variable doesn't exist or isn't handled */
495 if( var_Get( p_object, psz_variable, &val ) < 0 )
500 if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
501 &val_list, &text_list ) < 0 )
503 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
507 /* make (un)sensitive */
508 [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
510 for( i = 0; i < val_list.p_list->i_count; i++ )
512 vlc_value_t another_val;
514 NSString *o_title = @"";
517 switch( i_type & VLC_VAR_TYPE )
519 case VLC_VAR_VARIABLE:
521 /* This is causing crashes for the moment.
522 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
523 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
525 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
526 Value: val ofType: i_type];
527 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
530 NSMenu *o_menu = [o_lmi submenu];
532 [self setupVarMenu: o_menu forMenuItem: o_lmi target:p_object
533 var:psz_variable selector:pf_callback];
538 another_val.psz_string =
539 strdup(val_list.p_list->p_values[i].psz_string);
541 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
542 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
544 o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
545 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
546 Value: another_val ofType: i_type];
547 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
548 [o_lmi setTarget: self];
550 if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) )
551 [o_lmi setState: TRUE ];
555 case VLC_VAR_INTEGER:
557 o_title = text_list.p_list->p_values[i].psz_string ?
558 [NSApp localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
559 [NSString stringWithFormat: @"%d",
560 val_list.p_list->p_values[i].i_int];
562 o_lmi = [[o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""] retain ];
563 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
564 Value: val_list.p_list->p_values[i] ofType: i_type];
565 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[ o_data retain]]];
566 [o_lmi setTarget: self];
568 if( val_list.p_list->p_values[i].i_int == val.i_int )
569 [o_lmi setState: TRUE ];
577 /* clean up everything */
578 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
579 var_Change( p_object, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
582 - (IBAction)toggleVar:(id)sender
584 NSMenuItem *o_mi = (NSMenuItem *)sender;
585 VLCMenuExt *o_data = [[o_mi representedObject] pointerValue];
586 [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
587 toTarget: self withObject: o_data];
592 - (int)toggleVarThread: (id)_o_data
594 vlc_object_t *p_object;
595 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
596 VLCMenuExt *o_data = (VLCMenuExt *)_o_data;
598 vlc_thread_set_priority( [NSApp getIntf] , VLC_THREAD_PRIORITY_LOW );
600 p_object = (vlc_object_t *)vlc_object_get( [NSApp getIntf],
603 if( p_object != NULL )
605 var_Set( p_object, strdup([o_data name]), [o_data value] );
606 vlc_object_release( p_object );
616 @implementation VLCControls (NSMenuValidation)
618 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
620 BOOL bEnabled = TRUE;
621 intf_thread_t * p_intf = [NSApp getIntf];
623 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
626 if( p_playlist != NULL )
628 vlc_mutex_lock( &p_playlist->object_lock );
631 #define p_input p_playlist->p_input
633 if( [[o_mi title] isEqualToString: _NS("Faster")] ||
634 [[o_mi title] isEqualToString: _NS("Slower")] )
636 if( p_playlist != NULL && p_input != NULL )
638 vlc_mutex_lock( &p_input->stream.stream_lock );
639 bEnabled = p_input->stream.b_pace_control;
640 vlc_mutex_unlock( &p_input->stream.stream_lock );
647 else if( [[o_mi title] isEqualToString: _NS("Stop")] )
649 if( p_playlist == NULL || p_input == NULL )
654 else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
655 [[o_mi title] isEqualToString: _NS("Next")] )
657 if( p_playlist == NULL )
663 bEnabled = p_playlist->i_size > 1;
665 if( p_input != NULL )
667 vlc_mutex_lock( &p_input->stream.stream_lock );
668 bEnabled |= p_input->stream.i_area_nb > 1;
669 vlc_mutex_unlock( &p_input->stream.stream_lock );
673 else if( [[o_mi title] isEqualToString: _NS("Loop")] )
675 int i_state = config_GetInt( p_playlist, "loop" ) ?
676 NSOnState : NSOffState;
678 [o_mi setState: i_state];
680 else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
681 [[o_mi title] isEqualToString: _NS("Step Backward")] )
683 if( p_playlist != NULL && p_input != NULL )
685 vlc_mutex_lock( &p_input->stream.stream_lock );
686 bEnabled = p_input->stream.b_seekable;
687 vlc_mutex_unlock( &p_input->stream.stream_lock );
694 else if( [[o_mi title] isEqualToString: _NS("Mute")] )
696 [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
698 else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
699 [[o_mi title] isEqualToString: _NS("Half Size")] ||
700 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
701 [[o_mi title] isEqualToString: _NS("Double Size")] ||
702 [[o_mi title] isEqualToString: _NS("Fit To Screen")] ||
703 [[o_mi title] isEqualToString: _NS("Float On Top")] )
706 NSArray *o_windows = [NSApp windows];
707 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
710 if ( [[o_mi title] isEqualToString: _NS("Float On Top")] )
712 int i_state = config_GetInt( p_playlist, "macosx-float" ) ?
713 NSOnState : NSOffState;
714 [o_mi setState: i_state];
717 vout_thread_t *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
721 while ((o_window = [o_enumerator nextObject]))
723 if( [[o_window className] isEqualToString: @"VLCWindow"] )
729 vlc_object_release( (vlc_object_t *)p_vout );
733 if( p_playlist != NULL )
735 vlc_mutex_unlock( &p_playlist->object_lock );
736 vlc_object_release( p_playlist );
744 /*****************************************************************************
745 * VLCMenuExt implementation
746 *****************************************************************************
747 * Object connected to a playlistitem which remembers the data belonging to
748 * the variable of the autogenerated menu
749 *****************************************************************************/
750 @implementation VLCMenuExt
752 - (id)initWithVar: (const char *)_psz_name Object: (int)i_id
753 Value: (vlc_value_t)val ofType: (int)_i_type
759 psz_name = strdup( _psz_name );