1 /*****************************************************************************
2 * controls.m: MacOS X interface plugin
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: controls.m,v 1.17 2003/01/29 11:34:11 jlj 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 */
36 #include <vlc/input.h>
38 #include <Cocoa/Cocoa.h>
43 /*****************************************************************************
44 * VLCControls interface
45 *****************************************************************************/
46 @interface VLCControls : NSObject
50 IBOutlet id o_mi_mute;
51 IBOutlet id o_volumeslider;
54 - (IBAction)play:(id)sender;
55 - (IBAction)stop:(id)sender;
56 - (IBAction)faster:(id)sender;
57 - (IBAction)slower:(id)sender;
59 - (IBAction)prev:(id)sender;
60 - (IBAction)next:(id)sender;
61 - (IBAction)loop:(id)sender;
63 - (IBAction)volumeUp:(id)sender;
64 - (IBAction)volumeDown:(id)sender;
65 - (IBAction)mute:(id)sender;
66 - (IBAction)volumeSliderUpdate:(id)sender;
67 - (IBAction)fullscreen:(id)sender;
68 - (IBAction)deinterlace:(id)sender;
70 - (IBAction)toggleProgram:(id)sender;
71 - (IBAction)toggleTitle:(id)sender;
72 - (IBAction)toggleChapter:(id)sender;
73 - (IBAction)toggleLanguage:(id)sender;
74 - (IBAction)toggleVar:(id)sender;
76 - (void)setVolumeSlider;
80 /*****************************************************************************
81 * VLCControls implementation
82 *****************************************************************************/
83 @implementation VLCControls
85 - (IBAction)play:(id)sender
87 intf_thread_t * p_intf = [NSApp getIntf];
89 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
91 if( p_playlist == NULL )
96 if( playlist_IsPlaying( p_playlist ) )
98 playlist_Pause( p_playlist );
99 vlc_object_release( p_playlist );
105 b_empty = playlist_IsEmpty( p_playlist );
106 vlc_object_release( p_playlist );
110 playlist_Play( p_playlist );
114 [o_open openFile: nil];
119 - (IBAction)stop:(id)sender
121 intf_thread_t * p_intf = [NSApp getIntf];
123 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
125 if( p_playlist == NULL )
130 playlist_Stop( p_playlist );
131 vlc_object_release( p_playlist );
133 p_intf->p_sys->b_stopping = 1;
136 - (IBAction)faster:(id)sender
138 intf_thread_t * p_intf = [NSApp getIntf];
140 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
142 if( p_playlist == NULL )
147 vlc_mutex_lock( &p_playlist->object_lock );
148 if( p_playlist->p_input != NULL )
150 input_SetStatus( p_playlist->p_input, INPUT_STATUS_FASTER );
152 vlc_mutex_unlock( &p_playlist->object_lock );
154 vlc_object_release( p_playlist );
157 - (IBAction)slower:(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 vlc_mutex_lock( &p_playlist->object_lock );
169 if( p_playlist->p_input != NULL )
171 input_SetStatus( p_playlist->p_input, INPUT_STATUS_SLOWER );
173 vlc_mutex_unlock( &p_playlist->object_lock );
175 vlc_object_release( p_playlist );
178 - (IBAction)prev:(id)sender
180 intf_thread_t * p_intf = [NSApp getIntf];
182 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
184 if( p_playlist == NULL )
189 vlc_mutex_lock( &p_playlist->object_lock );
191 if( p_playlist->p_input == NULL )
193 vlc_mutex_unlock( &p_playlist->object_lock );
194 vlc_object_release( p_playlist );
198 vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
200 #define p_area p_playlist->p_input->stream.p_selected_area
202 if( p_area->i_part_nb > 1 && p_area->i_part > 1 )
206 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
207 input_ChangeArea( p_playlist->p_input, p_area );
208 vlc_mutex_unlock( &p_playlist->object_lock );
210 p_intf->p_sys->b_chapter_update = VLC_TRUE;
214 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
215 vlc_mutex_unlock( &p_playlist->object_lock );
216 playlist_Prev( p_playlist );
221 vlc_object_release( p_playlist );
224 - (IBAction)next:(id)sender
226 intf_thread_t * p_intf = [NSApp getIntf];
228 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
230 if( p_playlist == NULL )
235 vlc_mutex_lock( &p_playlist->object_lock );
237 if( p_playlist->p_input == NULL )
239 vlc_mutex_unlock( &p_playlist->object_lock );
240 vlc_object_release( p_playlist );
244 vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
246 #define p_area p_playlist->p_input->stream.p_selected_area
248 if( p_area->i_part_nb > 1 && p_area->i_part + 1 < p_area->i_part_nb )
252 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
253 input_ChangeArea( p_playlist->p_input, p_area );
254 vlc_mutex_unlock( &p_playlist->object_lock );
256 p_intf->p_sys->b_chapter_update = VLC_TRUE;
260 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
261 vlc_mutex_unlock( &p_playlist->object_lock );
262 playlist_Next( p_playlist );
267 vlc_object_release( p_playlist );
270 - (IBAction)loop:(id)sender
272 intf_thread_t * p_intf = [NSApp getIntf];
274 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
276 if( p_playlist == NULL )
281 config_PutInt( p_playlist, "loop",
282 !config_GetInt( p_playlist, "loop" ) );
284 vlc_object_release( p_playlist );
287 - (IBAction)volumeUp:(id)sender
289 intf_thread_t * p_intf = [NSApp getIntf];
290 aout_instance_t * p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT,
292 if ( p_aout != NULL )
294 if (p_intf->p_sys->b_mute)
296 [self mute:o_mi_mute];
298 aout_VolumeUp( p_aout, 1, NULL );
299 vlc_object_release( (vlc_object_t *)p_aout );
301 [self setVolumeSlider];
304 - (IBAction)volumeDown:(id)sender
306 intf_thread_t * p_intf = [NSApp getIntf];
307 aout_instance_t * p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT,
309 if ( p_aout != NULL )
311 if (p_intf->p_sys->b_mute)
313 [self mute:o_mi_mute];
315 aout_VolumeDown( p_aout, 1, NULL );
316 vlc_object_release( (vlc_object_t *)p_aout );
318 [self setVolumeSlider];
321 - (IBAction)mute:(id)sender
323 intf_thread_t * p_intf = [NSApp getIntf];
324 aout_instance_t * p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT,
326 audio_volume_t i_volume;
328 if ( p_aout != NULL )
330 aout_VolumeMute( p_aout, &i_volume );
331 vlc_object_release( (vlc_object_t *)p_aout );
334 p_intf->p_sys->b_mute = (i_volume == 0);
335 [o_mi_mute setState: p_intf->p_sys->b_mute ? NSOnState : NSOffState];
336 [o_volumeslider setEnabled: p_intf->p_sys->b_mute ? FALSE : TRUE];
337 [self setVolumeSlider];
340 - (IBAction)volumeSliderUpdate:(id)sender
342 intf_thread_t * p_intf = [NSApp getIntf];
343 aout_instance_t * p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT,
345 audio_volume_t i_volume;
347 if ( p_aout != NULL )
349 i_volume = (int) [sender floatValue];
350 aout_VolumeSet( p_aout, i_volume * AOUT_VOLUME_STEP);
351 vlc_object_release( (vlc_object_t *)p_aout );
355 - (void)setVolumeSlider
357 intf_thread_t * p_intf = [NSApp getIntf];
358 aout_instance_t * p_aout = vlc_object_find( p_intf, VLC_OBJECT_AOUT,
360 audio_volume_t i_volume;
362 if ( p_aout != NULL )
364 aout_VolumeGet( p_aout, &i_volume );
365 vlc_object_release( (vlc_object_t *)p_aout );
366 [o_volumeslider setFloatValue: (float) (i_volume / AOUT_VOLUME_STEP)];
370 [o_volumeslider setFloatValue: config_GetInt( p_intf, "volume" )];
374 - (IBAction)fullscreen:(id)sender
376 id o_window = [NSApp keyWindow];
377 NSArray *o_windows = [NSApp windows];
378 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
380 while ((o_window = [o_enumerator nextObject]))
382 if( [[o_window className] isEqualToString: @"VLCWindow"] )
384 [o_window toggleFullscreen];
389 - (IBAction)deinterlace:(id)sender
391 intf_thread_t * p_intf = [NSApp getIntf];
392 NSMenuItem *o_mi = (NSMenuItem *)sender;
393 NSString *mode = [o_mi title];
397 psz_filter = config_GetPsz( p_intf, "filter" );
399 if( [mode isEqualToString: @"none"] )
401 config_PutPsz( p_intf, "filter", "" );
405 if( !psz_filter || !*psz_filter )
407 config_PutPsz( p_intf, "filter", "deinterlace" );
411 if( strstr( psz_filter, "deinterlace" ) == NULL )
413 psz_filter = realloc( psz_filter, strlen( psz_filter ) + 20 );
414 strcat( psz_filter, ",deinterlace" );
416 config_PutPsz( p_intf, "filter", psz_filter );
423 /* now restart all video stream */
424 if( p_intf->p_sys->p_input )
426 vout_thread_t *p_vout;
427 vlc_mutex_lock( &p_intf->p_sys->p_input->stream.stream_lock );
429 /* Warn the vout we are about to change the filter chain */
430 p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
434 p_vout->b_filter_change = VLC_TRUE;
435 vlc_object_release( p_vout );
438 #define ES p_intf->p_sys->p_input->stream.pp_es[i]
439 for( i = 0 ; i < p_intf->p_sys->p_input->stream.i_es_number ; i++ )
441 if( ( ES->i_cat == VIDEO_ES ) &&
442 ES->p_decoder_fifo != NULL )
444 input_UnselectES( p_intf->p_sys->p_input, ES );
445 input_SelectES( p_intf->p_sys->p_input, ES );
449 vlc_mutex_unlock( &p_intf->p_sys->p_input->stream.stream_lock );
452 if( ![mode isEqualToString: @"none"] )
454 vout_thread_t *p_vout;
455 p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
461 val.psz_string = (char *)[mode cString];
462 if( var_Set( p_vout, "deinterlace-mode", val ) != VLC_SUCCESS )
463 config_PutPsz( p_intf, "deinterlace-mode", (char *)[mode cString] );
465 vlc_object_release( p_vout );
468 config_PutPsz( p_intf, "deinterlace-mode", (char *)[mode cString] );
473 - (IBAction)toggleProgram:(id)sender
475 NSMenuItem * o_mi = (NSMenuItem *)sender;
476 intf_thread_t * p_intf = [NSApp getIntf];
478 if( [o_mi state] == NSOffState )
480 u16 i_program_id = [o_mi tag];
482 input_ChangeProgram( p_intf->p_sys->p_input, i_program_id );
483 input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PLAY );
487 - (IBAction)toggleTitle:(id)sender
489 NSMenuItem * o_mi = (NSMenuItem *)sender;
490 intf_thread_t * p_intf = [NSApp getIntf];
492 if( [o_mi state] == NSOffState )
494 int i_title = [o_mi tag];
496 #define p_input p_intf->p_sys->p_input
497 input_ChangeArea( p_input, p_input->stream.pp_areas[i_title] );
498 input_SetStatus( p_input, INPUT_STATUS_PLAY );
503 - (IBAction)toggleChapter:(id)sender
505 NSMenuItem * o_mi = (NSMenuItem *)sender;
506 intf_thread_t * p_intf = [NSApp getIntf];
508 if( [o_mi state] == NSOffState )
510 int i_chapter = [o_mi tag];
512 #define p_input p_intf->p_sys->p_input
513 p_input->stream.p_selected_area->i_part = i_chapter;
514 input_ChangeArea( p_input, p_input->stream.p_selected_area );
515 input_SetStatus( p_input, INPUT_STATUS_PLAY );
520 - (IBAction)toggleLanguage:(id)sender
522 NSMenuItem * o_mi = (NSMenuItem *)sender;
523 intf_thread_t * p_intf = [NSApp getIntf];
526 /* We do not use this code, because you need to start stop .avi for
527 * it to work, so not very useful now --hartman */
528 if ( [o_mi state] == NSOffState && [o_mi tag] == 2000 )
530 NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
532 [o_open_panel setAllowsMultipleSelection: NO];
533 [o_open_panel setTitle: _NS("Open subtitlefile")];
534 [o_open_panel setPrompt: _NS("Open")];
536 if( [o_open_panel runModalForDirectory: nil
537 file: nil types: nil] == NSOKButton )
539 NSString *o_filename = [[o_open_panel filenames] objectAtIndex: 0];
540 config_PutPsz( p_intf, "sub-file", strdup( [o_filename cString] ));
545 #define p_input p_intf->p_sys->p_input
547 if( !p_intf->p_sys->b_audio_update )
549 NSValue * o_value = [o_mi representedObject];
550 es_descriptor_t * p_es = [o_value pointerValue];
552 if( [o_mi state] == NSOnState )
554 /* we just have one ES to disable */
555 input_ToggleES( p_input, p_es, 0 );
560 int i_cat = [o_mi tag];
562 vlc_mutex_lock( &p_input->stream.stream_lock );
564 #define ES p_input->stream.pp_selected_es[i]
566 /* unselect the selected ES in the same class */
567 for( i = 0; i < p_input->stream.i_selected_es_number; i++ )
569 if( ES->i_cat == i_cat )
571 vlc_mutex_unlock( &p_input->stream.stream_lock );
572 input_ToggleES( p_input, ES, 0 );
573 vlc_mutex_lock( &p_input->stream.stream_lock );
580 vlc_mutex_unlock( &p_input->stream.stream_lock );
582 input_ToggleES( p_input, p_es, 1 );
589 - (IBAction)toggleVar:(id)sender
591 NSMenuItem * o_mi = (NSMenuItem *)sender;
593 if( [o_mi state] == NSOffState )
595 const char * psz_variable = (const char *)[o_mi tag];
596 const char * psz_value = [[o_mi title] cString];
597 vlc_object_t * p_object = (vlc_object_t *)
598 [[o_mi representedObject] pointerValue];
600 /* psz_string sucks */
601 val.psz_string = (char *)psz_value;
603 if ( var_Set( p_object, psz_variable, val ) < 0 )
605 msg_Warn( p_object, "cannot set variable (%s)", psz_value );
612 @implementation VLCControls (NSMenuValidation)
614 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
616 BOOL bEnabled = TRUE;
617 NSMenu * o_menu = [o_mi menu];
618 intf_thread_t * p_intf = [NSApp getIntf];
620 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
623 if( p_playlist != NULL )
625 vlc_mutex_lock( &p_playlist->object_lock );
628 if( [[o_mi title] isEqualToString: _NS("Faster")] ||
629 [[o_mi title] isEqualToString: _NS("Slower")] )
631 if( p_playlist != NULL && p_playlist->p_input != NULL )
633 vlc_mutex_lock( &p_playlist->p_input->stream.stream_lock );
634 bEnabled = p_playlist->p_input->stream.b_pace_control;
635 vlc_mutex_unlock( &p_playlist->p_input->stream.stream_lock );
642 else if( [[o_mi title] isEqualToString: _NS("Stop")] )
644 if( p_playlist == NULL || p_playlist->p_input == NULL )
649 else if( [[o_mi title] isEqualToString: _NS("Previous")] ||
650 [[o_mi title] isEqualToString: _NS("Next")] )
652 if( p_playlist == NULL )
658 bEnabled = p_playlist->i_size > 1;
661 else if( [[o_mi title] isEqualToString: _NS("Loop")] )
663 int i_state = config_GetInt( p_playlist, "loop" ) ?
664 NSOnState : NSOffState;
666 [o_mi setState: i_state];
668 else if( [[o_mi title] isEqualToString: _NS("Fullscreen")] )
671 NSArray *o_windows = [NSApp windows];
672 NSEnumerator *o_enumerator = [o_windows objectEnumerator];
675 while ((o_window = [o_enumerator nextObject]))
677 if( [[o_window className] isEqualToString: @"VLCWindow"] )
684 else if( o_menu != nil &&
685 [[o_menu title] isEqualToString: _NS("Deinterlace")] )
687 char * psz_filter = config_GetPsz( p_intf, "filter" );
689 if( psz_filter != NULL )
693 psz_filter = config_GetPsz( p_intf, "deinterlace-mode" );
696 if( psz_filter != NULL )
698 if( strcmp( psz_filter, [[o_mi title] lossyCString] ) == 0 )
700 [o_mi setState: NSOnState];
704 [o_mi setState: NSOffState];
711 [o_mi setState: NSOffState];
715 if( p_playlist != NULL )
717 vlc_mutex_unlock( &p_playlist->object_lock );
718 vlc_object_release( p_playlist );