1 /*****************************************************************************
2 * controls.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Christophe Massiot <massiot@via.ecp.fr>
9 * Derk-Jan Hartman <hartman at videolan dot org>
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 */
39 /*****************************************************************************
40 * VLCControls implementation
41 *****************************************************************************/
42 @implementation VLCControls
44 - (IBAction)play:(id)sender
47 playlist_t * p_playlist;
48 intf_thread_t * p_intf = [NSApp getIntf];
49 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
52 val.i_int = PLAYING_S;
55 var_Get( p_input, "state", &val );
57 if( p_input && val.i_int != PAUSE_S )
59 vout_OSDMessage( p_intf, _( "Pause" ) );
61 var_Set( p_input, "state", val );
65 p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
69 vlc_mutex_lock( &p_playlist->object_lock );
70 if( p_playlist->i_size )
72 vlc_mutex_unlock( &p_playlist->object_lock );
73 vout_OSDMessage( p_intf, _( "Play" ) );
74 playlist_Play( p_playlist );
75 vlc_object_release( p_playlist );
79 vlc_mutex_unlock( &p_playlist->object_lock );
80 vlc_object_release( p_playlist );
81 [o_open openFileGeneric: nil];
85 if( p_input ) vlc_object_release( p_input );
88 - (IBAction)stop:(id)sender
90 intf_thread_t * p_intf = [NSApp getIntf];
91 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
93 if( p_playlist != NULL )
95 vout_OSDMessage( p_intf, _( "Stop" ) );
96 playlist_Stop( p_playlist );
97 vlc_object_release( p_playlist );
101 - (IBAction)faster:(id)sender
103 intf_thread_t * p_intf = [NSApp getIntf];
104 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
106 if( p_input != NULL )
108 vlc_value_t val; val.b_bool = VLC_TRUE;
110 var_Set( p_input, "rate-faster", val );
111 vout_OSDMessage( p_intf, _( "Faster" ) );
112 vlc_object_release( p_input );
116 - (IBAction)slower:(id)sender
118 intf_thread_t * p_intf = [NSApp getIntf];
119 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
121 if( p_input != NULL )
123 vlc_value_t val; val.b_bool = VLC_TRUE;
125 var_Set( p_input, "rate-slower", val );
126 vout_OSDMessage( p_intf, _( "Slower" ) );
127 vlc_object_release( p_input );
131 - (IBAction)prev:(id)sender
133 intf_thread_t * p_intf = [NSApp getIntf];
134 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
138 playlist_Prev( p_playlist );
139 vlc_object_release( p_playlist );
140 vout_OSDMessage( p_intf, _( "Previous" ) );
144 - (IBAction)next:(id)sender
146 intf_thread_t * p_intf = [NSApp getIntf];
147 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
151 playlist_Next( p_playlist );
152 vlc_object_release( p_playlist );
153 vout_OSDMessage( p_intf, _( "Next" ) );
157 - (IBAction)random:(id)sender
159 intf_thread_t * p_intf = [NSApp getIntf];
161 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
163 if( p_playlist == NULL )
168 var_Get( p_playlist, "random", &val );
169 val.b_bool = !val.b_bool;
170 var_Set( p_playlist, "random", val );
173 vout_OSDMessage( p_intf, _( "Random On" ) );
177 vout_OSDMessage( p_intf, _( "Random Off" ) );
180 p_intf->p_sys->b_playlist_update = VLC_TRUE;
181 p_intf->p_sys->b_intf_update = VLC_TRUE;
182 vlc_object_release( p_playlist );
185 - (IBAction)repeat:(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 var_Get( p_playlist, "repeat", &val );
199 var_Set( p_playlist, "loop", val );
201 val.b_bool = !val.b_bool;
202 var_Set( p_playlist, "repeat", val );
205 vout_OSDMessage( p_intf, _( "Repeat All" ) );
209 vout_OSDMessage( p_intf, _( "Repeat Off" ) );
212 p_intf->p_sys->b_playlist_update = VLC_TRUE;
213 p_intf->p_sys->b_intf_update = VLC_TRUE;
214 vlc_object_release( p_playlist );
217 - (IBAction)loop:(id)sender
219 intf_thread_t * p_intf = [NSApp getIntf];
221 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
223 if( p_playlist == NULL )
228 var_Get( p_playlist, "loop", &val );
231 var_Set( p_playlist, "repeat", val );
233 val.b_bool = !val.b_bool;
234 var_Set( p_playlist, "loop", val );
237 vout_OSDMessage( p_intf, _( "Repeat One" ) );
241 vout_OSDMessage( p_intf, _( "Repeat Off" ) );
244 p_intf->p_sys->b_playlist_update = VLC_TRUE;
245 p_intf->p_sys->b_intf_update = VLC_TRUE;
246 vlc_object_release( p_playlist );
249 - (IBAction)forward:(id)sender
251 intf_thread_t * p_intf = [NSApp getIntf];
252 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
254 if( p_input != NULL )
257 time.i_time = 10 * 1000000;
258 var_Set( p_input, "time-offset", time );
259 vout_OSDMessage( p_intf, _( "Jump +10 Seconds" ) );
260 vlc_object_release( p_input );
264 - (IBAction)backward:(id)sender
266 intf_thread_t * p_intf = [NSApp getIntf];
267 input_thread_t * p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
269 if( p_input != NULL )
272 time.i_time = -10 * 1000000;
273 var_Set( p_input, "time-offset", time );
274 vout_OSDMessage( p_intf, _( "Jump -10 Seconds" ) );
275 vlc_object_release( p_input );
280 - (IBAction)volumeUp:(id)sender
282 intf_thread_t * p_intf = [NSApp getIntf];
284 if( p_intf->p_sys->b_mute )
289 aout_VolumeUp( p_intf, 1, NULL );
291 [self updateVolumeSlider];
294 - (IBAction)volumeDown:(id)sender
296 intf_thread_t * p_intf = [NSApp getIntf];
298 if( p_intf->p_sys->b_mute )
303 aout_VolumeDown( p_intf, 1, NULL );
305 [self updateVolumeSlider];
308 - (IBAction)mute:(id)sender
310 intf_thread_t * p_intf = [NSApp getIntf];
311 audio_volume_t i_volume;
313 aout_VolumeMute( p_intf, &i_volume );
314 p_intf->p_sys->b_mute = ( i_volume == 0 );
316 [self updateVolumeSlider];
319 - (IBAction)volumeSliderUpdated:(id)sender
321 intf_thread_t * p_intf = [NSApp getIntf];
322 audio_volume_t i_volume = (audio_volume_t)[sender intValue];
324 aout_VolumeSet( p_intf, i_volume * AOUT_VOLUME_STEP );
327 - (void)updateVolumeSlider
329 intf_thread_t * p_intf = [NSApp getIntf];
330 audio_volume_t i_volume;
332 aout_VolumeGet( p_intf, &i_volume );
334 [o_volumeslider setFloatValue: (float)(i_volume / AOUT_VOLUME_STEP)];
336 vout_OSDMessage( p_intf, "Vol %d%%", i_volume*100/AOUT_VOLUME_MAX );
339 - (IBAction)windowAction:(id)sender
341 id o_window = [NSApp keyWindow];
342 NSString *o_title = [sender title];
343 NSArray *o_windows = [NSApp orderedWindows];
344 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
345 vout_thread_t *p_vout = vlc_object_find( [NSApp getIntf], VLC_OBJECT_VOUT,
350 while ((o_window = [o_enumerator nextObject]))
352 if( [[o_window className] isEqualToString: @"VLCWindow"] )
354 if( [o_title isEqualToString: _NS("Half Size") ] )
355 [o_window scaleWindowWithFactor: 0.5];
356 else if( [o_title isEqualToString: _NS("Normal Size") ] )
357 [o_window scaleWindowWithFactor: 1.0];
358 else if( [o_title isEqualToString: _NS("Double Size") ] )
359 [o_window scaleWindowWithFactor: 2.0];
360 else if( [o_title isEqualToString: _NS("Float on Top") ] )
361 [o_window toggleFloatOnTop];
362 else if( [o_title isEqualToString: _NS("Fit to Screen") ] )
364 if( ![o_window isZoomed] )
365 [o_window performZoom:self];
369 [o_window toggleFullscreen];
374 vlc_object_release( (vlc_object_t *)p_vout );
378 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
379 target:(vlc_object_t *)p_object
380 var:(const char *)psz_variable
381 selector:(SEL)pf_callback
383 vlc_value_t val, text;
384 int i_type = var_Type( p_object, psz_variable );
386 switch( i_type & VLC_VAR_TYPE )
390 case VLC_VAR_VARIABLE:
392 case VLC_VAR_INTEGER:
395 /* Variable doesn't exist or isn't handled */
399 /* Make sure we want to display the variable */
400 if( i_type & VLC_VAR_HASCHOICE )
402 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
403 if( val.i_int == 0 ) return;
404 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
408 /* Get the descriptive name of the variable */
409 var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
410 [o_mi setTitle: [NSApp localizedString: text.psz_string ?
411 text.psz_string : strdup( psz_variable ) ]];
413 var_Get( p_object, psz_variable, &val );
414 if( i_type & VLC_VAR_HASCHOICE )
416 NSMenu *o_menu = [o_mi submenu];
418 [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
419 var:psz_variable selector:pf_callback];
421 if( text.psz_string ) free( text.psz_string );
426 switch( i_type & VLC_VAR_TYPE )
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]]];
435 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
436 Value: val ofType: i_type];
437 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
438 [o_mi setState: val.b_bool ? TRUE : FALSE ];
442 if( text.psz_string ) free( text.psz_string );
446 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
447 if( text.psz_string ) free( text.psz_string );
451 - (void)setupVarMenu:(NSMenu *)o_menu
452 forMenuItem: (NSMenuItem *)o_parent
453 target:(vlc_object_t *)p_object
454 var:(const char *)psz_variable
455 selector:(SEL)pf_callback
457 vlc_value_t val, val_list, text_list;
458 int i_type, i, i_nb_items;
460 /* remove previous items */
461 i_nb_items = [o_menu numberOfItems];
462 for( i = 0; i < i_nb_items; i++ )
464 [o_menu removeItemAtIndex: 0];
467 /* Check the type of the object variable */
468 i_type = var_Type( p_object, psz_variable );
470 /* Make sure we want to display the variable */
471 if( i_type & VLC_VAR_HASCHOICE )
473 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
474 if( val.i_int == 0 ) return;
475 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
483 switch( i_type & VLC_VAR_TYPE )
487 case VLC_VAR_VARIABLE:
489 case VLC_VAR_INTEGER:
492 /* Variable doesn't exist or isn't handled */
496 if( var_Get( p_object, psz_variable, &val ) < 0 )
501 if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
502 &val_list, &text_list ) < 0 )
504 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
508 /* make (un)sensitive */
509 [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
511 for( i = 0; i < val_list.p_list->i_count; i++ )
513 vlc_value_t another_val;
515 NSString *o_title = @"";
518 switch( i_type & VLC_VAR_TYPE )
520 case VLC_VAR_VARIABLE:
522 /* This is causing crashes for the moment.
523 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
524 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
526 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
527 Value: val ofType: i_type];
528 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
531 NSMenu *o_menu = [o_lmi submenu];
533 [self setupVarMenu: o_menu forMenuItem: o_lmi target:p_object
534 var:psz_variable selector:pf_callback];
539 another_val.psz_string =
540 strdup(val_list.p_list->p_values[i].psz_string);
542 o_title = [NSApp localizedString: text_list.p_list->p_values[i].psz_string ?
543 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
545 o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
546 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
547 Value: another_val ofType: i_type];
548 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
549 [o_lmi setTarget: self];
551 if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) )
552 [o_lmi setState: TRUE ];
556 case VLC_VAR_INTEGER:
558 o_title = text_list.p_list->p_values[i].psz_string ?
559 [NSApp localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
560 [NSString stringWithFormat: @"%d",
561 val_list.p_list->p_values[i].i_int];
563 o_lmi = [[o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""] retain ];
564 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
565 Value: val_list.p_list->p_values[i] ofType: i_type];
566 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[ o_data retain]]];
567 [o_lmi setTarget: self];
569 if( val_list.p_list->p_values[i].i_int == val.i_int )
570 [o_lmi setState: TRUE ];
578 /* clean up everything */
579 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
580 var_Change( p_object, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
583 - (IBAction)toggleVar:(id)sender
585 NSMenuItem *o_mi = (NSMenuItem *)sender;
586 VLCMenuExt *o_data = [[o_mi representedObject] pointerValue];
587 [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
588 toTarget: self withObject: o_data];
593 - (int)toggleVarThread: (id)_o_data
595 vlc_object_t *p_object;
596 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
597 VLCMenuExt *o_data = (VLCMenuExt *)_o_data;
599 vlc_thread_set_priority( [NSApp getIntf] , VLC_THREAD_PRIORITY_LOW );
601 p_object = (vlc_object_t *)vlc_object_get( [NSApp getIntf],
604 if( p_object != NULL )
606 var_Set( p_object, strdup([o_data name]), [o_data value] );
607 vlc_object_release( p_object );
617 @implementation VLCControls (NSMenuValidation)
619 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
621 BOOL bEnabled = TRUE;
623 intf_thread_t * p_intf = [NSApp getIntf];
624 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
627 if( p_playlist != NULL )
629 vlc_mutex_lock( &p_playlist->object_lock );
632 #define p_input p_playlist->p_input
634 if( [[o_mi title] isEqualToString: _NS("Faster")] ||
635 [[o_mi title] isEqualToString: _NS("Slower")] )
637 if( p_playlist != NULL && p_input != NULL )
639 vlc_mutex_lock( &p_input->stream.stream_lock );
640 bEnabled = p_input->stream.b_pace_control;
641 vlc_mutex_unlock( &p_input->stream.stream_lock );
648 else if( [[o_mi title] isEqualToString: _NS("Stop")] )
650 if( p_playlist == NULL || p_input == NULL )
655 else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
656 [[o_mi title] isEqualToString: _NS("Next")] )
658 if( p_playlist == NULL )
664 bEnabled = p_playlist->i_size > 1;
666 if( p_input != NULL )
668 vlc_mutex_lock( &p_input->stream.stream_lock );
669 bEnabled |= p_input->stream.i_area_nb > 1;
670 vlc_mutex_unlock( &p_input->stream.stream_lock );
674 else if( [[o_mi title] isEqualToString: _NS("Random")] )
677 var_Get( p_playlist, "random", &val );
678 i_state = val.b_bool ? NSOnState : NSOffState;
679 [o_mi setState: i_state];
681 else if( [[o_mi title] isEqualToString: _NS("Repeat One")] )
684 var_Get( p_playlist, "repeat", &val );
685 i_state = val.b_bool ? NSOnState : NSOffState;
686 [o_mi setState: i_state];
688 else if( [[o_mi title] isEqualToString: _NS("Repeat All")] )
691 var_Get( p_playlist, "loop", &val );
692 i_state = val.b_bool ? NSOnState : NSOffState;
693 [o_mi setState: i_state];
695 else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
696 [[o_mi title] isEqualToString: _NS("Step Backward")] )
698 if( p_playlist != NULL && p_input != NULL )
700 vlc_mutex_lock( &p_input->stream.stream_lock );
701 bEnabled = p_input->stream.b_seekable;
702 vlc_mutex_unlock( &p_input->stream.stream_lock );
709 else if( [[o_mi title] isEqualToString: _NS("Mute")] )
711 [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
713 else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
714 [[o_mi title] isEqualToString: _NS("Half Size")] ||
715 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
716 [[o_mi title] isEqualToString: _NS("Double Size")] ||
717 [[o_mi title] isEqualToString: _NS("Fit to Screen")] ||
718 [[o_mi title] isEqualToString: _NS("Float on Top")] )
721 NSArray *o_windows = [NSApp orderedWindows];
722 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
725 if ( [[o_mi title] isEqualToString: _NS("Float on Top")] )
727 int i_state = config_GetInt( p_playlist, "video-on-top" ) ?
728 NSOnState : NSOffState;
729 [o_mi setState: i_state];
732 vout_thread_t *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
736 while ((o_window = [o_enumerator nextObject]))
738 if( [[o_window className] isEqualToString: @"VLCWindow"] )
744 vlc_object_release( (vlc_object_t *)p_vout );
748 if( p_playlist != NULL )
750 vlc_mutex_unlock( &p_playlist->object_lock );
751 vlc_object_release( p_playlist );
759 /*****************************************************************************
760 * VLCMenuExt implementation
761 *****************************************************************************
762 * Object connected to a playlistitem which remembers the data belonging to
763 * the variable of the autogenerated menu
764 *****************************************************************************/
765 @implementation VLCMenuExt
767 - (id)initWithVar: (const char *)_psz_name Object: (int)i_id
768 Value: (vlc_value_t)val ofType: (int)_i_type
774 psz_name = strdup( _psz_name );