1 /*****************************************************************************
2 * controls.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2002-2005 the VideoLAN team
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>
10 * Benjamin Pracht <bigben at videolan doit org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <stdlib.h> /* malloc(), free() */
31 #include <sys/param.h> /* for MAXPATHLEN */
40 /*****************************************************************************
41 * VLCControls implementation
42 *****************************************************************************/
43 @implementation VLCControls
47 [o_specificTime_mi setTitle: _NS("Go To Position")];
48 [o_specificTime_cancel_btn setTitle: _NS("Cancel")];
49 [o_specificTime_ok_btn setTitle: _NS("OK")];
50 [o_specificTime_sec_lbl setStringValue: _NS("sec.")];
51 [o_specificTime_goTo_lbl setStringValue: _NS("Go to specific position")];
54 - (IBAction)play:(id)sender
57 intf_thread_t * p_intf = VLCIntf;
58 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
62 vlc_mutex_lock( &p_playlist->object_lock );
63 if( p_playlist->i_size <= 0 )
65 vlc_mutex_unlock( &p_playlist->object_lock );
66 vlc_object_release( p_playlist );
67 [o_main intfOpenFileGeneric: (id)sender];
71 vlc_mutex_unlock( &p_playlist->object_lock );
72 vlc_object_release( p_playlist );
76 val.i_int = config_GetInt( p_intf, "key-play-pause" );
77 var_Set( p_intf->p_vlc, "key-pressed", val );
80 - (IBAction)stop:(id)sender
83 intf_thread_t * p_intf = VLCIntf;
84 val.i_int = config_GetInt( p_intf, "key-stop" );
85 var_Set( p_intf->p_vlc, "key-pressed", val );
88 - (IBAction)faster:(id)sender
91 intf_thread_t * p_intf = VLCIntf;
92 val.i_int = config_GetInt( p_intf, "key-faster" );
93 var_Set( p_intf->p_vlc, "key-pressed", val );
96 - (IBAction)slower:(id)sender
99 intf_thread_t * p_intf = VLCIntf;
100 val.i_int = config_GetInt( p_intf, "key-slower" );
101 var_Set( p_intf->p_vlc, "key-pressed", val );
104 - (IBAction)prev:(id)sender
107 intf_thread_t * p_intf = VLCIntf;
108 val.i_int = config_GetInt( p_intf, "key-prev" );
109 var_Set( p_intf->p_vlc, "key-pressed", val );
112 - (IBAction)next:(id)sender
115 intf_thread_t * p_intf = VLCIntf;
116 val.i_int = config_GetInt( p_intf, "key-next" );
117 var_Set( p_intf->p_vlc, "key-pressed", val );
120 - (IBAction)random:(id)sender
123 intf_thread_t * p_intf = VLCIntf;
124 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
126 if( p_playlist == NULL )
131 var_Get( p_playlist, "random", &val );
132 val.b_bool = !val.b_bool;
133 var_Set( p_playlist, "random", val );
136 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Random On" ) );
140 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Random Off" ) );
143 p_intf->p_sys->b_playmode_update = VLC_TRUE;
144 p_intf->p_sys->b_intf_update = VLC_TRUE;
145 vlc_object_release( p_playlist );
148 - (IBAction)repeat:(id)sender
151 intf_thread_t * p_intf = VLCIntf;
152 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
154 if( p_playlist == NULL )
159 var_Get( p_playlist, "repeat", &val );
162 var_Set( p_playlist, "loop", val );
164 val.b_bool = !val.b_bool;
165 var_Set( p_playlist, "repeat", val );
168 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat One" ) );
172 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
175 p_intf->p_sys->b_playmode_update = VLC_TRUE;
176 p_intf->p_sys->b_intf_update = VLC_TRUE;
177 vlc_object_release( p_playlist );
180 - (IBAction)loop:(id)sender
183 intf_thread_t * p_intf = VLCIntf;
184 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
186 if( p_playlist == NULL )
191 var_Get( p_playlist, "loop", &val );
194 var_Set( p_playlist, "repeat", val );
196 val.b_bool = !val.b_bool;
197 var_Set( p_playlist, "loop", val );
200 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat All" ) );
204 vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Repeat Off" ) );
207 p_intf->p_sys->b_playmode_update = VLC_TRUE;
208 p_intf->p_sys->b_intf_update = VLC_TRUE;
209 vlc_object_release( p_playlist );
212 - (IBAction)forward:(id)sender
215 intf_thread_t * p_intf = VLCIntf;
216 val.i_int = config_GetInt( p_intf, "key-jump+short" );
217 var_Set( p_intf->p_vlc, "key-pressed", val );
220 - (IBAction)backward:(id)sender
223 intf_thread_t * p_intf = VLCIntf;
224 val.i_int = config_GetInt( p_intf, "key-jump-short" );
225 var_Set( p_intf->p_vlc, "key-pressed", val );
229 - (IBAction)volumeUp:(id)sender
232 intf_thread_t * p_intf = VLCIntf;
233 val.i_int = config_GetInt( p_intf, "key-vol-up" );
234 var_Set( p_intf->p_vlc, "key-pressed", val );
235 /* Manage volume status */
236 [o_main manageVolumeSlider];
239 - (IBAction)volumeDown:(id)sender
242 intf_thread_t * p_intf = VLCIntf;
243 val.i_int = config_GetInt( p_intf, "key-vol-down" );
244 var_Set( p_intf->p_vlc, "key-pressed", val );
245 /* Manage volume status */
246 [o_main manageVolumeSlider];
249 - (IBAction)mute:(id)sender
252 intf_thread_t * p_intf = VLCIntf;
253 val.i_int = config_GetInt( p_intf, "key-vol-mute" );
254 var_Set( p_intf->p_vlc, "key-pressed", val );
255 /* Manage volume status */
256 [o_main manageVolumeSlider];
259 - (IBAction)volumeSliderUpdated:(id)sender
261 intf_thread_t * p_intf = VLCIntf;
262 audio_volume_t i_volume = (audio_volume_t)[sender intValue];
263 int i_volume_step = 0;
264 i_volume_step = config_GetInt( p_intf->p_vlc, "volume-step" );
265 aout_VolumeSet( p_intf, i_volume * i_volume_step );
266 /* Manage volume status */
267 [o_main manageVolumeSlider];
270 - (IBAction)windowAction:(id)sender
272 id o_window = [NSApp keyWindow];
273 NSString *o_title = [sender title];
274 NSArray *o_windows = [NSApp orderedWindows];
275 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
276 vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT,
281 id o_embedded_vout_list = [[VLCMain sharedInstance] getEmbeddedList];
282 while ((o_window = [o_enumerator nextObject]))
284 id o_vout_view = nil;
285 /* We have an embedded vout */
286 if( [o_embedded_vout_list windowContainsEmbedded: o_window] )
288 o_vout_view = [o_embedded_vout_list getViewForWindow: o_window];
290 /* We have a detached Vout */
291 else if( [[o_window className] isEqualToString: @"VLCWindow"] )
293 msg_Dbg( VLCIntf, "detached vout controls.m call getVoutView" );
294 o_vout_view = [o_window getVoutView];
299 if( [o_title isEqualToString: _NS("Half Size") ] )
300 [o_vout_view scaleWindowWithFactor: 0.5];
301 else if( [o_title isEqualToString: _NS("Normal Size") ] )
302 [o_vout_view scaleWindowWithFactor: 1.0];
303 else if( [o_title isEqualToString: _NS("Double Size") ] )
304 [o_vout_view scaleWindowWithFactor: 2.0];
305 else if( [o_title isEqualToString: _NS("Float on Top") ] )
306 [o_vout_view toggleFloatOnTop];
307 else if( [o_title isEqualToString: _NS("Fit to Screen") ] )
309 if( ![o_window isZoomed] )
310 [o_window performZoom:self];
312 else if( [o_title isEqualToString: _NS("Snapshot") ] )
314 [o_vout_view snapshot];
318 [o_vout_view toggleFullscreen];
323 vlc_object_release( (vlc_object_t *)p_vout );
327 playlist_t * p_playlist = vlc_object_find( VLCIntf, VLC_OBJECT_PLAYLIST,
330 if( p_playlist && ( [o_title isEqualToString: _NS("Fullscreen")] ||
331 [sender isKindOfClass:[NSButton class]] ) )
334 var_Get( p_playlist, "fullscreen", &val );
335 var_Set( p_playlist, "fullscreen", (vlc_value_t)!val.b_bool );
337 if( p_playlist ) vlc_object_release( (vlc_object_t *)p_playlist );
342 - (void)setupVarMenuItem:(NSMenuItem *)o_mi
343 target:(vlc_object_t *)p_object
344 var:(const char *)psz_variable
345 selector:(SEL)pf_callback
347 vlc_value_t val, text;
348 int i_type = var_Type( p_object, psz_variable );
350 switch( i_type & VLC_VAR_TYPE )
354 case VLC_VAR_VARIABLE:
356 case VLC_VAR_INTEGER:
359 /* Variable doesn't exist or isn't handled */
363 /* Make sure we want to display the variable */
364 if( i_type & VLC_VAR_HASCHOICE )
366 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
367 if( val.i_int == 0 ) return;
368 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
372 /* Get the descriptive name of the variable */
373 var_Change( p_object, psz_variable, VLC_VAR_GETTEXT, &text, NULL );
374 [o_mi setTitle: [[VLCMain sharedInstance] localizedString: text.psz_string ?
375 text.psz_string : strdup( psz_variable ) ]];
377 var_Get( p_object, psz_variable, &val );
378 if( i_type & VLC_VAR_HASCHOICE )
380 NSMenu *o_menu = [o_mi submenu];
382 [self setupVarMenu: o_menu forMenuItem: o_mi target:p_object
383 var:psz_variable selector:pf_callback];
385 if( text.psz_string ) free( text.psz_string );
390 switch( i_type & VLC_VAR_TYPE )
393 o_data = [[VLCMenuExt alloc] initWithVar: psz_variable Object: p_object->i_object_id
394 Value: val ofType: i_type];
395 [o_mi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
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]]];
402 if( !( i_type & VLC_VAR_ISCOMMAND ) )
403 [o_mi setState: val.b_bool ? TRUE : FALSE ];
407 if( text.psz_string ) free( text.psz_string );
411 if( ( i_type & VLC_VAR_TYPE ) == VLC_VAR_STRING ) free( val.psz_string );
412 if( text.psz_string ) free( text.psz_string );
416 - (void)setupVarMenu:(NSMenu *)o_menu
417 forMenuItem: (NSMenuItem *)o_parent
418 target:(vlc_object_t *)p_object
419 var:(const char *)psz_variable
420 selector:(SEL)pf_callback
422 vlc_value_t val, val_list, text_list;
423 int i_type, i, i_nb_items;
425 /* remove previous items */
426 i_nb_items = [o_menu numberOfItems];
427 for( i = 0; i < i_nb_items; i++ )
429 [o_menu removeItemAtIndex: 0];
432 /* Check the type of the object variable */
433 i_type = var_Type( p_object, psz_variable );
435 /* Make sure we want to display the variable */
436 if( i_type & VLC_VAR_HASCHOICE )
438 var_Change( p_object, psz_variable, VLC_VAR_CHOICESCOUNT, &val, NULL );
439 if( val.i_int == 0 ) return;
440 if( (i_type & VLC_VAR_TYPE) != VLC_VAR_VARIABLE && val.i_int == 1 )
448 switch( i_type & VLC_VAR_TYPE )
452 case VLC_VAR_VARIABLE:
454 case VLC_VAR_INTEGER:
457 /* Variable doesn't exist or isn't handled */
461 if( var_Get( p_object, psz_variable, &val ) < 0 )
466 if( var_Change( p_object, psz_variable, VLC_VAR_GETLIST,
467 &val_list, &text_list ) < 0 )
469 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
473 /* make (un)sensitive */
474 [o_parent setEnabled: ( val_list.p_list->i_count > 1 )];
476 for( i = 0; i < val_list.p_list->i_count; i++ )
478 vlc_value_t another_val;
480 NSString *o_title = @"";
483 switch( i_type & VLC_VAR_TYPE )
486 another_val.psz_string =
487 strdup(val_list.p_list->p_values[i].psz_string);
489 o_title = [[VLCMain sharedInstance] localizedString: text_list.p_list->p_values[i].psz_string ?
490 text_list.p_list->p_values[i].psz_string : val_list.p_list->p_values[i].psz_string ];
492 o_lmi = [o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""];
493 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
494 Value: another_val ofType: i_type];
495 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[o_data retain]]];
496 [o_lmi setTarget: self];
498 if( !strcmp( val.psz_string, val_list.p_list->p_values[i].psz_string ) && !( i_type & VLC_VAR_ISCOMMAND ) )
499 [o_lmi setState: TRUE ];
503 case VLC_VAR_INTEGER:
505 o_title = text_list.p_list->p_values[i].psz_string ?
506 [[VLCMain sharedInstance] localizedString: strdup( text_list.p_list->p_values[i].psz_string )] :
507 [NSString stringWithFormat: @"%d",
508 val_list.p_list->p_values[i].i_int];
510 o_lmi = [[o_menu addItemWithTitle: o_title action: pf_callback keyEquivalent: @""] retain ];
511 o_data = [[VLCMenuExt alloc] initWithVar: strdup(psz_variable) Object: p_object->i_object_id
512 Value: val_list.p_list->p_values[i] ofType: i_type];
513 [o_lmi setRepresentedObject: [NSValue valueWithPointer:[ o_data retain]]];
514 [o_lmi setTarget: self];
516 if( val_list.p_list->p_values[i].i_int == val.i_int && !( i_type & VLC_VAR_ISCOMMAND ) )
517 [o_lmi setState: TRUE ];
525 /* clean up everything */
526 if( (i_type & VLC_VAR_TYPE) == VLC_VAR_STRING ) free( val.psz_string );
527 var_Change( p_object, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
530 - (IBAction)toggleVar:(id)sender
532 NSMenuItem *o_mi = (NSMenuItem *)sender;
533 VLCMenuExt *o_data = [[o_mi representedObject] pointerValue];
534 [NSThread detachNewThreadSelector: @selector(toggleVarThread:)
535 toTarget: self withObject: o_data];
540 - (int)toggleVarThread: (id)_o_data
542 vlc_object_t *p_object;
543 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
544 VLCMenuExt *o_data = (VLCMenuExt *)_o_data;
546 vlc_thread_set_priority( VLCIntf , VLC_THREAD_PRIORITY_LOW );
548 p_object = (vlc_object_t *)vlc_object_get( VLCIntf,
551 if( p_object != NULL )
553 var_Set( p_object, strdup([o_data name]), [o_data value] );
554 vlc_object_release( p_object );
562 - (IBAction)goToSpecificTime:(id)sender
564 if( sender == o_specificTime_cancel_btn )
566 [NSApp endSheet: o_specificTime_win];
567 [o_specificTime_win close];
569 else if( sender == o_specificTime_ok_btn )
571 input_thread_t * p_input = (input_thread_t *)vlc_object_find( VLCIntf, \
572 VLC_OBJECT_INPUT, FIND_ANYWHERE );
575 input_Control( p_input, INPUT_SET_TIME, \
576 (int64_t)([o_specificTime_enter_fld intValue] * 1000000));
577 vlc_object_release( p_input );
580 [NSApp endSheet: o_specificTime_win];
581 [o_specificTime_win close];
585 input_thread_t * p_input = (input_thread_t *)vlc_object_find( VLCIntf, \
586 VLC_OBJECT_INPUT, FIND_ANYWHERE );
589 /* we can obviously only do that if an input is available */
590 vlc_value_t pos, length;
591 var_Get( p_input, "time", &pos );
592 [o_specificTime_enter_fld setIntValue: (pos.i_time / 1000000)];
593 var_Get( p_input, "length", &length );
594 [o_specificTime_stepper setMaxValue: (length.i_time / 1000000)];
596 [NSApp beginSheet: o_specificTime_win modalForWindow: \
597 [NSApp mainWindow] modalDelegate: self didEndSelector: nil \
599 [o_specificTime_win makeKeyWindow];
600 vlc_object_release( p_input );
607 @implementation VLCControls (NSMenuValidation)
609 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
611 BOOL bEnabled = TRUE;
613 intf_thread_t * p_intf = VLCIntf;
614 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
617 if( p_playlist != NULL )
619 vlc_mutex_lock( &p_playlist->object_lock );
623 #define p_input p_playlist->p_input
625 if( [[o_mi title] isEqualToString: _NS("Faster")] ||
626 [[o_mi title] isEqualToString: _NS("Slower")] )
628 if( p_input != NULL )
630 bEnabled = p_input->input.b_can_pace_control;
637 else if( [[o_mi title] isEqualToString: _NS("Stop")] )
639 if( p_input == NULL )
643 [o_main setupMenus]; /* Make sure input menu is up to date */
645 else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
646 [[o_mi title] isEqualToString: _NS("Next")] )
648 bEnabled = p_playlist->i_size > 1;
650 else if( [[o_mi title] isEqualToString: _NS("Random")] )
653 var_Get( p_playlist, "random", &val );
654 i_state = val.b_bool ? NSOnState : NSOffState;
655 [o_mi setState: i_state];
657 else if( [[o_mi title] isEqualToString: _NS("Repeat One")] )
660 var_Get( p_playlist, "repeat", &val );
661 i_state = val.b_bool ? NSOnState : NSOffState;
662 [o_mi setState: i_state];
664 else if( [[o_mi title] isEqualToString: _NS("Repeat All")] )
667 var_Get( p_playlist, "loop", &val );
668 i_state = val.b_bool ? NSOnState : NSOffState;
669 [o_mi setState: i_state];
671 else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
672 [[o_mi title] isEqualToString: _NS("Step Backward")] )
674 if( p_input != NULL )
676 var_Get( p_input, "seekable", &val);
677 bEnabled = val.b_bool;
680 else if( [[o_mi title] isEqualToString: _NS("Mute")] )
682 [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
683 [o_main setupMenus]; /* Make sure audio menu is up to date */
685 else if( [[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("Snapshot")] ||
690 [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
691 [[o_mi title] isEqualToString: _NS("Float on Top")] )
694 NSArray *o_windows = [NSApp orderedWindows];
695 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
698 vout_thread_t *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
702 if( [[o_mi title] isEqualToString: _NS("Float on Top")] )
704 var_Get( p_vout, "video-on-top", &val );
705 [o_mi setState: val.b_bool ? NSOnState : NSOffState];
708 while( (o_window = [o_enumerator nextObject]))
710 if( [[o_window className] isEqualToString: @"VLCWindow"] ||
711 [[[VLCMain sharedInstance] getEmbeddedList]
712 windowContainsEmbedded: o_window])
718 vlc_object_release( (vlc_object_t *)p_vout );
720 else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] )
722 var_Get( p_playlist, "fullscreen", &val );
723 [o_mi setState: val.b_bool];
726 [o_main setupMenus]; /* Make sure video menu is up to date */
729 vlc_mutex_unlock( &p_playlist->object_lock );
730 vlc_object_release( p_playlist );
737 /*****************************************************************************
738 * VLCMenuExt implementation
739 *****************************************************************************
740 * Object connected to a playlistitem which remembers the data belonging to
741 * the variable of the autogenerated menu
742 *****************************************************************************/
743 @implementation VLCMenuExt
745 - (id)initWithVar: (const char *)_psz_name Object: (int)i_id
746 Value: (vlc_value_t)val ofType: (int)_i_type
752 psz_name = strdup( _psz_name );
790 /*****************************************************************************
791 * VLCTimeField implementation
792 *****************************************************************************
793 * we need this to catch our click-event in the controller window
794 *****************************************************************************/
796 @implementation VLCTimeField
797 - (void)mouseDown: (NSEvent *)ourEvent
799 if( [ourEvent clickCount] > 1 )
800 [[[VLCMain sharedInstance] getControls] goToSpecificTime: nil];