1 /*****************************************************************************
2 * controls.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: controls.m,v 1.41 2003/06/03 22:21:46 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
133 intf_thread_t * p_intf = [NSApp getIntf];
135 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
137 if( p_playlist == NULL )
142 vlc_mutex_lock( &p_playlist->object_lock );
144 if( p_playlist->p_input == NULL )
146 vlc_mutex_unlock( &p_playlist->object_lock );
147 vlc_object_release( p_playlist );
151 vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
153 #define p_area p_playlist->p_input->stream.p_selected_area
155 if( p_area->i_part_nb > 1 && p_area->i_part > 1 )
159 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
160 input_ChangeArea( p_playlist->p_input, p_area );
161 vlc_mutex_unlock( &p_playlist->object_lock );
163 p_intf->p_sys->b_input_update = VLC_TRUE;
167 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
168 vlc_mutex_unlock( &p_playlist->object_lock );
169 playlist_Prev( p_playlist );
174 vlc_object_release( p_playlist );
177 - (IBAction)next:(id)sender
179 intf_thread_t * p_intf = [NSApp getIntf];
181 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
183 if( p_playlist == NULL )
188 vlc_mutex_lock( &p_playlist->object_lock );
190 if( p_playlist->p_input == NULL )
192 vlc_mutex_unlock( &p_playlist->object_lock );
193 vlc_object_release( p_playlist );
197 vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
199 #define p_area p_playlist->p_input->stream.p_selected_area
201 if( p_area->i_part_nb > 1 && p_area->i_part + 1 < p_area->i_part_nb )
205 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
206 input_ChangeArea( p_playlist->p_input, p_area );
207 vlc_mutex_unlock( &p_playlist->object_lock );
209 p_intf->p_sys->b_input_update = VLC_TRUE;
213 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
214 vlc_mutex_unlock( &p_playlist->object_lock );
215 playlist_Next( p_playlist );
220 vlc_object_release( p_playlist );
223 - (IBAction)loop:(id)sender
225 intf_thread_t * p_intf = [NSApp getIntf];
227 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
229 if( p_playlist == NULL )
234 config_PutInt( p_playlist, "loop",
235 !config_GetInt( p_playlist, "loop" ) );
237 vlc_object_release( p_playlist );
240 - (IBAction)forward:(id)sender
242 intf_thread_t * p_intf = [NSApp getIntf];
243 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
245 if( p_playlist == NULL || p_playlist->p_input == NULL )
247 if ( p_playlist != NULL ) vlc_object_release( p_playlist );
251 input_Seek( p_playlist->p_input, 5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
252 vlc_object_release( p_playlist );
255 - (IBAction)backward:(id)sender
257 intf_thread_t * p_intf = [NSApp getIntf];
258 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
260 if( p_playlist == NULL || p_playlist->p_input == NULL )
262 if ( p_playlist != NULL ) vlc_object_release( p_playlist );
266 input_Seek( p_playlist->p_input, -5, INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
267 vlc_object_release( p_playlist );
270 - (IBAction)volumeUp:(id)sender
272 intf_thread_t * p_intf = [NSApp getIntf];
274 if( p_intf->p_sys->b_mute )
279 aout_VolumeUp( p_intf, 1, NULL );
281 [self updateVolumeSlider];
284 - (IBAction)volumeDown:(id)sender
286 intf_thread_t * p_intf = [NSApp getIntf];
288 if( p_intf->p_sys->b_mute )
293 aout_VolumeDown( p_intf, 1, NULL );
295 [self updateVolumeSlider];
298 - (IBAction)mute:(id)sender
300 intf_thread_t * p_intf = [NSApp getIntf];
301 audio_volume_t i_volume;
303 aout_VolumeMute( p_intf, &i_volume );
304 p_intf->p_sys->b_mute = ( i_volume == 0 );
306 [self updateVolumeSlider];
309 - (IBAction)volumeSliderUpdated:(id)sender
311 intf_thread_t * p_intf = [NSApp getIntf];
312 audio_volume_t i_volume = (audio_volume_t)[sender intValue];
314 aout_VolumeSet( p_intf, i_volume * AOUT_VOLUME_STEP );
317 - (void)updateVolumeSlider
319 intf_thread_t * p_intf = [NSApp getIntf];
320 audio_volume_t i_volume;
322 aout_VolumeGet( p_intf, &i_volume );
324 [o_volumeslider setFloatValue: (float)(i_volume / AOUT_VOLUME_STEP)];
327 - (IBAction)windowAction:(id)sender
329 id o_window = [NSApp keyWindow];
330 NSString *o_title = [sender title];
331 NSArray *o_windows = [NSApp windows];
332 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
333 vout_thread_t *p_vout = vlc_object_find( [NSApp getIntf], VLC_OBJECT_VOUT,
338 while ((o_window = [o_enumerator nextObject]))
340 if( [[o_window className] isEqualToString: @"VLCWindow"] )
342 if( [o_title isEqualToString: _NS("Fullscreen") ] )
343 [o_window toggleFullscreen];
344 else if( [o_title isEqualToString: _NS("Half Size") ] )
345 [o_window scaleWindowWithFactor: 0.5];
346 else if( [o_title isEqualToString: _NS("Normal Size") ] )
347 [o_window scaleWindowWithFactor: 1.0];
348 else if( [o_title isEqualToString: _NS("Double Size") ] )
349 [o_window scaleWindowWithFactor: 2.0];
350 else if( [o_title isEqualToString: _NS("Float On Top") ] )
351 [o_window toggleFloatOnTop];
352 else if( [o_title isEqualToString: _NS("Fit To Screen") ] )
354 if( ![o_window isZoomed] )
355 [o_window performZoom:self];
359 vlc_object_release( (vlc_object_t *)p_vout );
363 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
364 target:(vlc_object_t *)p_object
365 var:(const char *)psz_variable
366 selector:(SEL)pf_callback
368 vlc_value_t val, text;
369 int i_type = var_Type( p_object, psz_variable );
371 switch( i_type & VLC_VAR_TYPE )
375 case VLC_VAR_VARIABLE:
377 case VLC_VAR_INTEGER:
380 /* Variable doesn't exist or isn't handled */
384 /* Make sure we want to display the variable */
385 if( i_type & VLC_VAR_HASCHOICE )
387 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
388 if( val.i_int == 0 ) return;
389 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
393 /* Get the descriptive name of the variable */
394 var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
395 [o_mi setTitle: [NSApp localizedString: text.psz_string ?
396 text.psz_string : strdup( psz_variable ) ]];
398 var_Get( p_object, psz_variable, &val );
399 if( i_type & VLC_VAR_HASCHOICE )
401 NSMenu *o_menu = [o_mi submenu];
403 [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
404 var:psz_variable selector:pf_callback];
406 if( text.psz_string ) free( text.psz_string );
411 switch( i_type & VLC_VAR_TYPE )
414 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
415 Value: val ofType: i_type];
416 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
420 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
421 Value: val ofType: i_type];
422 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
423 [o_mi setState: val.b_bool ? TRUE : FALSE ];
427 if( text.psz_string ) free( text.psz_string );
431 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
432 if( text.psz_string ) free( text.psz_string );
436 - (void)setupVarMenu:(NSMenu *)o_menu
437 forMenuItem: (NSMenuItem *)o_parent
438 target:(vlc_object_t *)p_object
439 var:(const char *)psz_variable
440 selector:(SEL)pf_callback
442 vlc_value_t val, val_list, text_list;
443 int i_type, i, i_nb_items;
445 /* remove previous items */
446 i_nb_items = [o_menu numberOfItems];
447 for( i = 0; i < i_nb_items; i++ )
449 [o_menu removeItemAtIndex: 0];
452 /* Check the type of the object variable */
453 i_type = var_Type( p_object, psz_variable );
455 /* Make sure we want to display the variable */
456 if( i_type & VLC_VAR_HASCHOICE )
458 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
459 if( val.i_int == 0 ) return;
460 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
468 switch( i_type & VLC_VAR_TYPE )
472 case VLC_VAR_VARIABLE:
474 case VLC_VAR_INTEGER:
477 /* Variable doesn't exist or isn't handled */
481 if( var_Get( p_object, psz_variable, &val ) < 0 )
486 if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
487 &val_list, &text_list ) < 0 )
489 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
493 /* make (un)sensitive */
494 [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
496 for( i = 0; i < val_list.p_list->i_count; i++ )
498 vlc_value_t another_val;
500 NSString *o_title = @"";
503 switch( i_type & VLC_VAR_TYPE )
505 case VLC_VAR_VARIABLE:
507 /* This is causing crashes for the moment.
508 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
509 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
511 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
512 Value: val ofType: i_type];
513 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
516 NSMenu *o_menu = [o_lmi submenu];
518 [self setupVarMenu: o_menu forMenuItem: o_lmi target:p_object
519 var:psz_variable selector:pf_callback];
524 another_val.psz_string =
525 strdup(val_list.p_list->p_values[i].psz_string);
527 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
528 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
530 o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
531 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
532 Value: another_val ofType: i_type];
533 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
534 [o_lmi setTarget: self];
536 if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) )
537 [o_lmi setState: TRUE ];
541 case VLC_VAR_INTEGER:
543 o_title = text_list.p_list->p_values[i].psz_string ?
544 [NSApp localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
545 [NSString stringWithFormat: @"%d",
546 val_list.p_list->p_values[i].i_int];
548 o_lmi = [[o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""] retain ];
549 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
550 Value: val_list.p_list->p_values[i] ofType: i_type];
551 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[ o_data retain]]];
552 [o_lmi setTarget: self];
554 if( val_list.p_list->p_values[i].i_int == val.i_int )
555 [o_lmi setState: TRUE ];
563 /* clean up everything */
564 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
565 var_Change( p_object, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
568 - (IBAction)toggleVar:(id)sender
570 NSMenuItem *o_mi = (NSMenuItem *)sender;
571 VLCMenuExt *o_data = [[o_mi representedObject] pointerValue];
572 [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
573 toTarget: self withObject: o_data];
578 - (int)toggleVarThread: (id)_o_data
580 vlc_object_t *p_object;
581 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
582 VLCMenuExt *o_data = (VLCMenuExt *)_o_data;
584 vlc_thread_set_priority( [NSApp getIntf] , VLC_THREAD_PRIORITY_LOW );
586 p_object = (vlc_object_t *)vlc_object_get( [NSApp getIntf],
589 if( p_object != NULL )
591 var_Set( p_object, strdup([o_data name]), [o_data value] );
592 vlc_object_release( p_object );
602 @implementation VLCControls (NSMenuValidation)
604 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
606 BOOL bEnabled = TRUE;
607 intf_thread_t * p_intf = [NSApp getIntf];
609 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
612 if( p_playlist != NULL )
614 vlc_mutex_lock( &p_playlist->object_lock );
617 #define p_input p_playlist->p_input
619 if( [[o_mi title] isEqualToString: _NS("Faster")] ||
620 [[o_mi title] isEqualToString: _NS("Slower")] )
622 if( p_playlist != NULL && p_input != NULL )
624 vlc_mutex_lock( &p_input->stream.stream_lock );
625 bEnabled = p_input->stream.b_pace_control;
626 vlc_mutex_unlock( &p_input->stream.stream_lock );
633 else if( [[o_mi title] isEqualToString: _NS("Stop")] )
635 if( p_playlist == NULL || p_input == NULL )
640 else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
641 [[o_mi title] isEqualToString: _NS("Next")] )
643 if( p_playlist == NULL )
649 bEnabled = p_playlist->i_size > 1;
651 if( p_input != NULL )
653 vlc_mutex_lock( &p_input->stream.stream_lock );
654 bEnabled |= p_input->stream.p_selected_area->i_part_nb > 1;
655 vlc_mutex_unlock( &p_input->stream.stream_lock );
659 else if( [[o_mi title] isEqualToString: _NS("Loop")] )
661 int i_state = config_GetInt( p_playlist, "loop" ) ?
662 NSOnState : NSOffState;
664 [o_mi setState: i_state];
666 else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
667 [[o_mi title] isEqualToString: _NS("Step Backward")] )
669 if( p_playlist != NULL && p_input != NULL )
671 vlc_mutex_lock( &p_input->stream.stream_lock );
672 bEnabled = p_input->stream.b_seekable;
673 vlc_mutex_unlock( &p_input->stream.stream_lock );
680 else if( [[o_mi title] isEqualToString: _NS("Mute")] )
682 [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
684 else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
685 [[o_mi title] isEqualToString: _NS("Half Size")] ||
686 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
687 [[o_mi title] isEqualToString: _NS("Double Size")] ||
688 [[o_mi title] isEqualToString: _NS("Fit To Screen")] ||
689 [[o_mi title] isEqualToString: _NS("Float On Top")] )
692 NSArray *o_windows = [NSApp windows];
693 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
696 if ( [[o_mi title] isEqualToString: _NS("Float On Top")] )
698 int i_state = config_GetInt( p_playlist, "macosx-float" ) ?
699 NSOnState : NSOffState;
700 [o_mi setState: i_state];
703 vout_thread_t *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
707 while ((o_window = [o_enumerator nextObject]))
709 if( [[o_window className] isEqualToString: @"VLCWindow"] )
715 vlc_object_release( (vlc_object_t *)p_vout );
719 if( p_playlist != NULL )
721 vlc_mutex_unlock( &p_playlist->object_lock );
722 vlc_object_release( p_playlist );
730 /*****************************************************************************
731 * VLCMenuExt implementation
732 *****************************************************************************
733 * Object connected to a playlistitem which remembers the data belonging to
734 * the variable of the autogenerated menu
735 *****************************************************************************/
736 @implementation VLCMenuExt
738 - (id)initWithVar: (const char *)_psz_name Object: (int)i_id
739 Value: (vlc_value_t)val ofType: (int)_i_type
745 psz_name = strdup( _psz_name );