1 /*****************************************************************************
2 * controls.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002-2003 VideoLAN
5 * $Id: controls.m,v 1.36 2003/05/06 20:12:28 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_chapter_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_chapter_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];
354 vlc_object_release( (vlc_object_t *)p_vout );
358 - (IBAction)deinterlace:(id)sender
360 intf_thread_t * p_intf = [NSApp getIntf];
361 BOOL bEnable = [sender state] == NSOffState;
363 if( bEnable && ![[sender title] isEqualToString: @"none"] )
365 config_PutPsz( p_intf, "filter", "deinterlace" );
366 config_PutPsz( p_intf, "deinterlace-mode",
367 [[sender title] lossyCString] );
371 config_PutPsz( p_intf, "filter", NULL );
375 - (IBAction)toggleProgram:(id)sender
377 NSMenuItem * o_mi = (NSMenuItem *)sender;
378 intf_thread_t * p_intf = [NSApp getIntf];
380 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
382 if( p_playlist == NULL )
387 vlc_mutex_lock( &p_playlist->object_lock );
389 if( p_playlist->p_input == NULL )
391 vlc_mutex_unlock( &p_playlist->object_lock );
392 vlc_object_release( p_playlist );
396 if( [o_mi state] == NSOffState )
398 u16 i_program_id = [o_mi tag];
400 input_ChangeProgram( p_playlist->p_input, i_program_id );
401 input_SetStatus( p_playlist->p_input, INPUT_STATUS_PLAY );
404 vlc_mutex_unlock( &p_playlist->object_lock );
405 vlc_object_release( p_playlist );
408 - (IBAction)toggleTitle:(id)sender
410 NSMenuItem * o_mi = (NSMenuItem *)sender;
411 intf_thread_t * p_intf = [NSApp getIntf];
413 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
415 if( p_playlist == NULL )
420 vlc_mutex_lock( &p_playlist->object_lock );
422 if( p_playlist->p_input == NULL )
424 vlc_mutex_unlock( &p_playlist->object_lock );
425 vlc_object_release( p_playlist );
429 if( [o_mi state] == NSOffState )
431 int i_title = [o_mi tag];
433 #define p_input p_playlist->p_input
434 input_ChangeArea( p_input, p_input->stream.pp_areas[i_title] );
435 input_SetStatus( p_input, INPUT_STATUS_PLAY );
439 vlc_mutex_unlock( &p_playlist->object_lock );
440 vlc_object_release( p_playlist );
443 - (IBAction)toggleChapter:(id)sender
445 NSMenuItem * o_mi = (NSMenuItem *)sender;
446 intf_thread_t * p_intf = [NSApp getIntf];
448 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
450 if( p_playlist == NULL )
455 vlc_mutex_lock( &p_playlist->object_lock );
457 if( p_playlist->p_input == NULL )
459 vlc_mutex_unlock( &p_playlist->object_lock );
460 vlc_object_release( p_playlist );
464 if( [o_mi state] == NSOffState )
466 int i_chapter = [o_mi tag];
468 #define p_input p_playlist->p_input
469 p_input->stream.p_selected_area->i_part = i_chapter;
470 input_ChangeArea( p_input, p_input->stream.p_selected_area );
471 input_SetStatus( p_input, INPUT_STATUS_PLAY );
475 vlc_mutex_unlock( &p_playlist->object_lock );
476 vlc_object_release( p_playlist );
479 - (IBAction)toggleLanguage:(id)sender
481 NSMenuItem * o_mi = (NSMenuItem *)sender;
482 intf_thread_t * p_intf = [NSApp getIntf];
484 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
486 if( p_playlist == NULL )
491 vlc_mutex_lock( &p_playlist->object_lock );
493 if( p_playlist->p_input == NULL )
495 vlc_mutex_unlock( &p_playlist->object_lock );
496 vlc_object_release( p_playlist );
501 /* We do not use this code, because you need to start stop .avi for
502 * it to work, so not very useful now --hartman */
503 if ( [o_mi state] == NSOffState && [o_mi tag] == 2000 )
505 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
507 [o_open_panel setAllowsMultipleSelection: NO];
508 [o_open_panel setTitle: _NS("Open subtitle file")];
509 [o_open_panel setPrompt: _NS("Open")];
511 if( [o_open_panel runModalForDirectory: nil
512 file: nil types: nil] == NSOKButton )
514 NSString *o_filename = [[o_open_panel filenames] objectAtIndex: 0];
515 config_PutPsz( p_intf, "sub-file", strdup( [o_filename cString] ));
520 #define p_input p_playlist->p_input
522 if( !p_intf->p_sys->b_audio_update )
524 NSValue * o_value = [o_mi representedObject];
525 es_descriptor_t * p_es = [o_value pointerValue];
527 if( [o_mi state] == NSOnState )
529 /* we just have one ES to disable */
530 input_ToggleES( p_input, p_es, 0 );
535 int i_cat = [o_mi tag];
537 vlc_mutex_lock( &p_input->stream.stream_lock );
539 #define ES p_input->stream.pp_selected_es[i]
541 /* unselect the selected ES in the same class */
542 for( i = 0; i < p_input->stream.i_selected_es_number; i++ )
544 if( ES->i_cat == i_cat )
546 vlc_mutex_unlock( &p_input->stream.stream_lock );
547 input_ToggleES( p_input, ES, 0 );
548 vlc_mutex_lock( &p_input->stream.stream_lock );
555 vlc_mutex_unlock( &p_input->stream.stream_lock );
557 input_ToggleES( p_input, p_es, 1 );
563 vlc_mutex_unlock( &p_playlist->object_lock );
564 vlc_object_release( p_playlist );
567 - (IBAction)toggleVar:(id)sender
569 NSMenuItem *o_mi = (NSMenuItem *)sender;
570 NSMenu *o_mu = [o_mi menu];
572 if( [o_mi state] == NSOffState )
574 const char * psz_variable = (const char *)
575 [[[o_mu supermenu] itemWithTitle: [o_mu title]] tag];
576 vlc_object_t * p_object = (vlc_object_t *)
577 [[o_mi representedObject] pointerValue];
579 val.i_int = (int)[o_mi tag];
581 if ( var_Set( p_object, psz_variable, val ) < 0 )
583 msg_Warn( p_object, "cannot set variable %s: with %d", psz_variable, val.i_int );
585 if (psz_variable) free(psz_variable);
591 @implementation VLCControls (NSMenuValidation)
593 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
595 BOOL bEnabled = TRUE;
596 NSMenu * o_menu = [o_mi menu];
597 intf_thread_t * p_intf = [NSApp getIntf];
599 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
602 if( p_playlist != NULL )
604 vlc_mutex_lock( &p_playlist->object_lock );
607 #define p_input p_playlist->p_input
609 if( [[o_mi title] isEqualToString: _NS("Faster")] ||
610 [[o_mi title] isEqualToString: _NS("Slower")] )
612 if( p_playlist != NULL && p_input != NULL )
614 vlc_mutex_lock( &p_input->stream.stream_lock );
615 bEnabled = p_input->stream.b_pace_control;
616 vlc_mutex_unlock( &p_input->stream.stream_lock );
623 else if( [[o_mi title] isEqualToString: _NS("Stop")] )
625 if( p_playlist == NULL || p_input == NULL )
630 else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
631 [[o_mi title] isEqualToString: _NS("Next")] )
633 if( p_playlist == NULL )
639 bEnabled = p_playlist->i_size > 1;
641 if( p_input != NULL )
643 vlc_mutex_lock( &p_input->stream.stream_lock );
644 bEnabled |= p_input->stream.p_selected_area->i_part_nb > 1;
645 vlc_mutex_unlock( &p_input->stream.stream_lock );
649 else if( [[o_mi title] isEqualToString: _NS("Loop")] )
651 int i_state = config_GetInt( p_playlist, "loop" ) ?
652 NSOnState : NSOffState;
654 [o_mi setState: i_state];
656 else if( [[o_mi title] isEqualToString: _NS("Step Forward")] ||
657 [[o_mi title] isEqualToString: _NS("Step Backward")] )
659 if( p_playlist != NULL && p_input != NULL )
661 vlc_mutex_lock( &p_input->stream.stream_lock );
662 bEnabled = p_input->stream.b_seekable;
663 vlc_mutex_unlock( &p_input->stream.stream_lock );
670 else if( [[o_mi title] isEqualToString: _NS("Mute")] )
672 [o_mi setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
674 else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] ||
675 [[o_mi title] isEqualToString: _NS("Half Size")] ||
676 [[o_mi title] isEqualToString: _NS("Normal Size")] ||
677 [[o_mi title] isEqualToString: _NS("Double Size")] ||
678 [[o_mi title] isEqualToString: _NS("Float On Top")] )
681 NSArray *o_windows = [NSApp windows];
682 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
685 if ( [[o_mi title] isEqualToString: _NS("Float On Top")] )
687 int i_state = config_GetInt( p_playlist, "macosx-float" ) ?
688 NSOnState : NSOffState;
689 [o_mi setState: i_state];
692 vout_thread_t *p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
696 while ((o_window = [o_enumerator nextObject]))
698 if( [[o_window className] isEqualToString: @"VLCWindow"] )
704 vlc_object_release( (vlc_object_t *)p_vout );
707 else if( [[o_mi title] isEqualToString: _NS("Float On Top")] )
712 else if( o_menu != nil &&
713 [[o_menu title] isEqualToString: _NS("Deinterlace")] )
715 char * psz_filter = config_GetPsz( p_intf, "filter" );
717 if( psz_filter != NULL )
721 psz_filter = config_GetPsz( p_intf, "deinterlace-mode" );
724 if( psz_filter != NULL )
726 if( strcmp( psz_filter, [[o_mi title] lossyCString] ) == 0 )
728 [o_mi setState: NSOnState];
732 [o_mi setState: NSOffState];
739 if( [[o_mi title] isEqualToString: @"none"] )
741 [o_mi setState: NSOnState];
745 [o_mi setState: NSOffState];
750 if( p_playlist != NULL )
752 vlc_mutex_unlock( &p_playlist->object_lock );
753 vlc_object_release( p_playlist );