1 /*****************************************************************************
2 * controls.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: controls.m,v 1.48 2003/09/20 13:46:00 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];
77 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
79 if( p_playlist != NULL )
81 playlist_Stop( p_playlist );
82 vlc_object_release( p_playlist );
86 - (IBAction)faster:(id)sender
88 intf_thread_t * p_intf = [NSApp getIntf];
89 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
93 vlc_value_t val; val.b_bool = VLC_TRUE;
95 var_Set( p_input, "rate-faster", val );
96 vlc_object_release( p_input );
100 - (IBAction)slower:(id)sender
102 intf_thread_t * p_intf = [NSApp getIntf];
103 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
105 if( p_input != NULL )
107 vlc_value_t val; val.b_bool = VLC_TRUE;
109 var_Set( p_input, "rate-slower", val );
110 vlc_object_release( p_input );
114 - (IBAction)prev:(id)sender
117 intf_thread_t * p_intf = [NSApp getIntf];
119 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
121 if( p_playlist == NULL )
126 vlc_mutex_lock( &p_playlist->object_lock );
128 if( p_playlist->p_input == NULL )
130 vlc_mutex_unlock( &p_playlist->object_lock );
131 vlc_object_release( p_playlist );
135 vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
136 val.b_bool = VLC_TRUE;
138 #define p_area p_playlist->p_input->stream.p_selected_area
139 if( p_area->i_part > 0 && p_area->i_part_nb > 1)
141 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
142 var_Set( p_playlist->p_input, "prev-chapter", val );
143 vlc_mutex_unlock( &p_playlist->object_lock );
145 else if( p_area->i_id > 1 )
147 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
148 var_Set( p_playlist->p_input, "prev-title", val );
149 vlc_mutex_unlock( &p_playlist->object_lock );
153 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
154 vlc_mutex_unlock( &p_playlist->object_lock );
155 playlist_Prev( p_playlist );
159 vlc_object_release( p_playlist );
162 - (IBAction)next:(id)sender
165 intf_thread_t * p_intf = [NSApp getIntf];
167 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
169 if( p_playlist == NULL )
174 vlc_mutex_lock( &p_playlist->object_lock );
176 if( p_playlist->p_input == NULL )
178 vlc_mutex_unlock( &p_playlist->object_lock );
179 vlc_object_release( p_playlist );
183 vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
184 val.b_bool = VLC_TRUE;
186 #define p_area p_playlist->p_input->stream.p_selected_area
187 if( p_area->i_part < p_area->i_part_nb - 1 && p_area->i_part_nb > 1 )
189 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
190 var_Set( p_playlist->p_input, "next-chapter", val );
191 vlc_mutex_unlock( &p_playlist->object_lock );
193 else if( p_area->i_id < p_playlist->p_input->stream.i_area_nb && p_playlist->p_input->stream.i_area_nb > 1 )
195 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
196 var_Set( p_playlist->p_input, "next-title", val );
197 vlc_mutex_unlock( &p_playlist->object_lock );
201 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
202 vlc_mutex_unlock( &p_playlist->object_lock );
203 playlist_Next( p_playlist );
207 vlc_object_release( p_playlist );
210 - (IBAction)loop:(id)sender
212 intf_thread_t * p_intf = [NSApp getIntf];
214 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
216 if( p_playlist == NULL )
221 config_PutInt( p_playlist, "loop",
222 !config_GetInt( p_playlist, "loop" ) );
224 vlc_object_release( p_playlist );
227 - (IBAction)forward:(id)sender
229 intf_thread_t * p_intf = [NSApp getIntf];
230 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
232 if( p_input != NULL )
236 var_Set( p_input, "time-offset", time );
237 vlc_object_release( p_input );
241 - (IBAction)backward:(id)sender
243 intf_thread_t * p_intf = [NSApp getIntf];
244 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
246 if( p_input != NULL )
250 var_Set( p_input, "time-offset", time );
251 vlc_object_release( p_input );
255 - (IBAction)volumeUp:(id)sender
257 intf_thread_t * p_intf = [NSApp getIntf];
259 if( p_intf->p_sys->b_mute )
264 aout_VolumeUp( p_intf, 1, NULL );
266 [self updateVolumeSlider];
269 - (IBAction)volumeDown:(id)sender
271 intf_thread_t * p_intf = [NSApp getIntf];
273 if( p_intf->p_sys->b_mute )
278 aout_VolumeDown( p_intf, 1, NULL );
280 [self updateVolumeSlider];
283 - (IBAction)mute:(id)sender
285 intf_thread_t * p_intf = [NSApp getIntf];
286 audio_volume_t i_volume;
288 aout_VolumeMute( p_intf, &i_volume );
289 p_intf->p_sys->b_mute = ( i_volume == 0 );
291 [self updateVolumeSlider];
294 - (IBAction)volumeSliderUpdated:(id)sender
296 intf_thread_t * p_intf = [NSApp getIntf];
297 audio_volume_t i_volume = (audio_volume_t)[sender intValue];
299 aout_VolumeSet( p_intf, i_volume * AOUT_VOLUME_STEP );
302 - (void)updateVolumeSlider
304 intf_thread_t * p_intf = [NSApp getIntf];
305 audio_volume_t i_volume;
307 aout_VolumeGet( p_intf, &i_volume );
309 [o_volumeslider setFloatValue: (float)(i_volume / AOUT_VOLUME_STEP)];
312 - (IBAction)windowAction:(id)sender
314 id o_window = [NSApp keyWindow];
315 NSString *o_title = [sender title];
316 NSArray *o_windows = [NSApp windows];
317 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
318 vout_thread_t *p_vout = vlc_object_find( [NSApp getIntf], VLC_OBJECT_VOUT,
323 while ((o_window = [o_enumerator nextObject]))
325 if( [[o_window className] isEqualToString: @"VLCWindow"] )
327 if( [o_title isEqualToString: _NS("Fullscreen") ] )
328 [o_window toggleFullscreen];
329 else if( [o_title isEqualToString: _NS("Half Size") ] )
330 [o_window scaleWindowWithFactor: 0.5];
331 else if( [o_title isEqualToString: _NS("Normal Size") ] )
332 [o_window scaleWindowWithFactor: 1.0];
333 else if( [o_title isEqualToString: _NS("Double Size") ] )
334 [o_window scaleWindowWithFactor: 2.0];
335 else if( [o_title isEqualToString: _NS("Float On Top") ] )
336 [o_window toggleFloatOnTop];
337 else if( [o_title isEqualToString: _NS("Fit To Screen") ] )
339 if( ![o_window isZoomed] )
340 [o_window performZoom:self];
344 vlc_object_release( (vlc_object_t *)p_vout );
348 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
349 target:(vlc_object_t *)p_object
350 var:(const char *)psz_variable
351 selector:(SEL)pf_callback
353 vlc_value_t val, text;
354 int i_type = var_Type( p_object, psz_variable );
356 switch( i_type & VLC_VAR_TYPE )
360 case VLC_VAR_VARIABLE:
362 case VLC_VAR_INTEGER:
365 /* Variable doesn't exist or isn't handled */
369 /* Make sure we want to display the variable */
370 if( i_type & VLC_VAR_HASCHOICE )
372 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
373 if( val.i_int == 0 ) return;
374 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
378 /* Get the descriptive name of the variable */
379 var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
380 [o_mi setTitle: [NSApp localizedString: text.psz_string ?
381 text.psz_string : strdup( psz_variable ) ]];
383 var_Get( p_object, psz_variable, &val );
384 if( i_type & VLC_VAR_HASCHOICE )
386 NSMenu *o_menu = [o_mi submenu];
388 [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
389 var:psz_variable selector:pf_callback];
391 if( text.psz_string ) free( text.psz_string );
396 switch( i_type & VLC_VAR_TYPE )
399 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
400 Value: val ofType: i_type];
401 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
405 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
406 Value: val ofType: i_type];
407 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
408 [o_mi setState: val.b_bool ? TRUE : FALSE ];
412 if( text.psz_string ) free( text.psz_string );
416 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
417 if( text.psz_string ) free( text.psz_string );
421 - (void)setupVarMenu:(NSMenu *)o_menu
422 forMenuItem: (NSMenuItem *)o_parent
423 target:(vlc_object_t *)p_object
424 var:(const char *)psz_variable
425 selector:(SEL)pf_callback
427 vlc_value_t val, val_list, text_list;
428 int i_type, i, i_nb_items;
430 /* remove previous items */
431 i_nb_items = [o_menu numberOfItems];
432 for( i = 0; i < i_nb_items; i++ )
434 [o_menu removeItemAtIndex: 0];
437 /* Check the type of the object variable */
438 i_type = var_Type( p_object, psz_variable );
440 /* Make sure we want to display the variable */
441 if( i_type & VLC_VAR_HASCHOICE )
443 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
444 if( val.i_int == 0 ) return;
445 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
453 switch( i_type & VLC_VAR_TYPE )
457 case VLC_VAR_VARIABLE:
459 case VLC_VAR_INTEGER:
462 /* Variable doesn't exist or isn't handled */
466 if( var_Get( p_object, psz_variable, &val ) < 0 )
471 if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
472 &val_list, &text_list ) < 0 )
474 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
478 /* make (un)sensitive */
479 [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
481 for( i = 0; i < val_list.p_list->i_count; i++ )
483 vlc_value_t another_val;
485 NSString *o_title = @"";
488 switch( i_type & VLC_VAR_TYPE )
490 case VLC_VAR_VARIABLE:
492 /* This is causing crashes for the moment.
493 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
494 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
496 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
497 Value: val ofType: i_type];
498 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
501 NSMenu *o_menu = [o_lmi submenu];
503 [self setupVarMenu: o_menu forMenuItem: o_lmi target:p_object
504 var:psz_variable selector:pf_callback];
509 another_val.psz_string =
510 strdup(val_list.p_list->p_values[i].psz_string);
512 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
513 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
515 o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
516 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
517 Value: another_val ofType: i_type];
518 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
519 [o_lmi setTarget: self];
521 if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) )
522 [o_lmi setState: TRUE ];
526 case VLC_VAR_INTEGER:
528 o_title = text_list.p_list->p_values[i].psz_string ?
529 [NSApp localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
530 [NSString stringWithFormat: @"%d",
531 val_list.p_list->p_values[i].i_int];
533 o_lmi = [[o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""] retain ];
534 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
535 Value: val_list.p_list->p_values[i] ofType: i_type];
536 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[ o_data retain]]];
537 [o_lmi setTarget: self];
539 if( val_list.p_list->p_values[i].i_int == val.i_int )
540 [o_lmi setState: TRUE ];
548 /* clean up everything */
549 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
550 var_Change( p_object, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
553 - (IBAction)toggleVar:(id)sender
555 NSMenuItem *o_mi = (NSMenuItem *)sender;
556 VLCMenuExt *o_data = [[o_mi representedObject] pointerValue];
557 [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
558 toTarget: self withObject: o_data];
563 - (int)toggleVarThread: (id)_o_data
565 vlc_object_t *p_object;
566 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
567 VLCMenuExt *o_data = (VLCMenuExt *)_o_data;
569 vlc_thread_set_priority( [NSApp getIntf] , VLC_THREAD_PRIORITY_LOW );
571 p_object = (vlc_object_t *)vlc_object_get( [NSApp getIntf],
574 if( p_object != NULL )
576 var_Set( p_object, strdup([o_data name]), [o_data value] );
577 vlc_object_release( p_object );
587 @implementation VLCControls (NSMenuValidation)
589 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
591 BOOL bEnabled = TRUE;
592 intf_thread_t * p_intf = [NSApp getIntf];
594 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
597 if( p_playlist != NULL )
599 vlc_mutex_lock( &p_playlist->object_lock );
602 #define p_input p_playlist->p_input
604 if( [[o_mi title] isEqualToString: _NS("Faster")] ||
605 [[o_mi title] isEqualToString: _NS("Slower")] )
607 if( p_playlist != NULL && p_input != NULL )
609 vlc_mutex_lock( &p_input->stream.stream_lock );
610 bEnabled = p_input->stream.b_pace_control;
611 vlc_mutex_unlock( &p_input->stream.stream_lock );
618 else if( [[o_mi title] isEqualToString: _NS("Stop")] )
620 if( p_playlist == NULL || p_input == NULL )
625 else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
626 [[o_mi title] isEqualToString: _NS("Next")] )
628 if( p_playlist == NULL )
634 bEnabled = p_playlist->i_size > 1;
636 if( p_input != NULL )
638 vlc_mutex_lock( &p_input->stream.stream_lock );
639 bEnabled |= p_input->stream.i_area_nb > 1;
640 vlc_mutex_unlock( &p_input->stream.stream_lock );
644 else if( [[o_mi title] isEqualToString: _NS("Loop")] )
646 int i_state = config_GetInt( p_playlist, "loop" ) ?
647 NSOnState : NSOffState;
649 [o_mi setState: i_state];
651 else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
652 [[o_mi title] isEqualToString: _NS("Step Backward")] )
654 if( p_playlist != NULL && p_input != NULL )
656 vlc_mutex_lock( &p_input->stream.stream_lock );
657 bEnabled = p_input->stream.b_seekable;
658 vlc_mutex_unlock( &p_input->stream.stream_lock );
665 else if( [[o_mi title] isEqualToString: _NS("Mute")] )
667 [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
669 else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
670 [[o_mi title] isEqualToString: _NS("Half Size")] ||
671 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
672 [[o_mi title] isEqualToString: _NS("Double Size")] ||
673 [[o_mi title] isEqualToString: _NS("Fit To Screen")] ||
674 [[o_mi title] isEqualToString: _NS("Float On Top")] )
677 NSArray *o_windows = [NSApp windows];
678 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
681 if ( [[o_mi title] isEqualToString: _NS("Float On Top")] )
683 int i_state = config_GetInt( p_playlist, "macosx-float" ) ?
684 NSOnState : NSOffState;
685 [o_mi setState: i_state];
688 vout_thread_t *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
692 while ((o_window = [o_enumerator nextObject]))
694 if( [[o_window className] isEqualToString: @"VLCWindow"] )
700 vlc_object_release( (vlc_object_t *)p_vout );
704 if( p_playlist != NULL )
706 vlc_mutex_unlock( &p_playlist->object_lock );
707 vlc_object_release( p_playlist );
715 /*****************************************************************************
716 * VLCMenuExt implementation
717 *****************************************************************************
718 * Object connected to a playlistitem which remembers the data belonging to
719 * the variable of the autogenerated menu
720 *****************************************************************************/
721 @implementation VLCMenuExt
723 - (id)initWithVar: (const char *)_psz_name Object: (int)i_id
724 Value: (vlc_value_t)val ofType: (int)_i_type
730 psz_name = strdup( _psz_name );